Skip to content

Commit c165591

Browse files
committed
[IndVars] Avoid unnecessary truncate for zext nneg use
When performing sext IV widening, if one of the narrow uses is in a zext nneg, we can treat it like an sext and avoid the insertion of a trunc.
1 parent 24e80d4 commit c165591

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU, SCEVExpander &Rewri
17621762
};
17631763

17641764
// Our raison d'etre! Eliminate sign and zero extension.
1765-
if ((isa<SExtInst>(DU.NarrowUse) && canWidenBySExt()) ||
1765+
if ((match(DU.NarrowUse, m_SExtLike(m_Value())) && canWidenBySExt()) ||
17661766
(isa<ZExtInst>(DU.NarrowUse) && canWidenByZExt())) {
17671767
Value *NewDef = DU.WideDef;
17681768
if (DU.NarrowUse->getType() != WideType) {

llvm/test/Transforms/IndVarSimplify/widen-nonnegative.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,7 @@ define void @zext_nneg_add_nsw(ptr %A, i32 %offset, i32 %M) {
331331
; CHECK: for.body:
332332
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
333333
; CHECK-NEXT: [[TMP1:%.*]] = add nsw i64 [[INDVARS_IV]], [[TMP0]]
334-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
335-
; CHECK-NEXT: [[IDXPROM_US:%.*]] = zext nneg i32 [[TMP2]] to i64
336-
; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[IDXPROM_US]]
334+
; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP1]]
337335
; CHECK-NEXT: tail call void @use_ptr(ptr [[ARRAYIDX_US]])
338336
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
339337
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
@@ -658,9 +656,7 @@ define void @zext_nneg_mul_nsw(ptr %A, i32 %multiple, i32 %M) {
658656
; CHECK: for.body:
659657
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
660658
; CHECK-NEXT: [[TMP1:%.*]] = mul nsw i64 [[INDVARS_IV]], [[TMP0]]
661-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
662-
; CHECK-NEXT: [[IDXPROM_US:%.*]] = zext nneg i32 [[TMP2]] to i64
663-
; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[IDXPROM_US]]
659+
; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP1]]
664660
; CHECK-NEXT: tail call void @use_ptr(ptr [[ARRAYIDX_US]])
665661
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
666662
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]

0 commit comments

Comments
 (0)