Skip to content

Commit 4b22a92

Browse files
authored
[IndVars] Mark truncs as nuw/nsw (#88686)
When inserting truncs during IV widening, mark the trunc as either nuw or nsw depending on whether zext or sext widening was used. For non-negative IVs both nuw and nsw apply.
1 parent 5b9af38 commit 4b22a92

16 files changed

+84
-70
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,8 +2004,18 @@ class IRBuilderBase {
20042004
// Instruction creation methods: Cast/Conversion Operators
20052005
//===--------------------------------------------------------------------===//
20062006

2007-
Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
2008-
return CreateCast(Instruction::Trunc, V, DestTy, Name);
2007+
Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2008+
bool IsNUW = false, bool IsNSW = false) {
2009+
if (V->getType() == DestTy)
2010+
return V;
2011+
if (Value *Folded = Folder.FoldCast(Instruction::Trunc, V, DestTy))
2012+
return Folded;
2013+
Instruction *I = CastInst::Create(Instruction::Trunc, V, DestTy);
2014+
if (IsNUW)
2015+
I->setHasNoUnsignedWrap();
2016+
if (IsNSW)
2017+
I->setHasNoSignedWrap();
2018+
return Insert(I, Name);
20092019
}
20102020

20112021
Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "",

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ class WidenIV {
11531153

11541154
Instruction *widenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter,
11551155
PHINode *OrigPhi, PHINode *WidePhi);
1156+
void truncateIVUse(NarrowIVDefUse DU);
11561157

11571158
bool widenLoopCompare(NarrowIVDefUse DU);
11581159
bool widenWithVariantUse(NarrowIVDefUse DU);
@@ -1569,15 +1570,18 @@ WidenIV::WidenedRecTy WidenIV::getWideRecurrence(WidenIV::NarrowIVDefUse DU) {
15691570

15701571
/// This IV user cannot be widened. Replace this use of the original narrow IV
15711572
/// with a truncation of the new wide IV to isolate and eliminate the narrow IV.
1572-
static void truncateIVUse(WidenIV::NarrowIVDefUse DU, DominatorTree *DT,
1573-
LoopInfo *LI) {
1573+
void WidenIV::truncateIVUse(NarrowIVDefUse DU) {
15741574
auto *InsertPt = getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI);
15751575
if (!InsertPt)
15761576
return;
15771577
LLVM_DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef << " for user "
15781578
<< *DU.NarrowUse << "\n");
1579+
ExtendKind ExtKind = getExtendKind(DU.NarrowDef);
15791580
IRBuilder<> Builder(InsertPt);
1580-
Value *Trunc = Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType());
1581+
Value *Trunc =
1582+
Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType(), "",
1583+
DU.NeverNegative || ExtKind == ExtendKind::Zero,
1584+
DU.NeverNegative || ExtKind == ExtendKind::Sign);
15811585
DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
15821586
}
15831587

@@ -1826,14 +1830,21 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
18261830
assert(ExtendKindMap.count(DU.NarrowDef) &&
18271831
"Should already know the kind of extension used to widen NarrowDef");
18281832

1833+
// This narrow use can be widened by a sext if it's non-negative or its narrow
1834+
// def was widened by a sext. Same for zext.
1835+
bool CanWidenBySExt =
1836+
DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1837+
bool CanWidenByZExt =
1838+
DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Zero;
1839+
18291840
// Stop traversing the def-use chain at inner-loop phis or post-loop phis.
18301841
if (PHINode *UsePhi = dyn_cast<PHINode>(DU.NarrowUse)) {
18311842
if (LI->getLoopFor(UsePhi->getParent()) != L) {
18321843
// For LCSSA phis, sink the truncate outside the loop.
18331844
// After SimplifyCFG most loop exit targets have a single predecessor.
18341845
// Otherwise fall back to a truncate within the loop.
18351846
if (UsePhi->getNumOperands() != 1)
1836-
truncateIVUse(DU, DT, LI);
1847+
truncateIVUse(DU);
18371848
else {
18381849
// Widening the PHI requires us to insert a trunc. The logical place
18391850
// for this trunc is in the same BB as the PHI. This is not possible if
@@ -1847,7 +1858,8 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
18471858
WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0));
18481859
BasicBlock *WidePhiBB = WidePhi->getParent();
18491860
IRBuilder<> Builder(WidePhiBB, WidePhiBB->getFirstInsertionPt());
1850-
Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType());
1861+
Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType(), "",
1862+
CanWidenByZExt, CanWidenBySExt);
18511863
UsePhi->replaceAllUsesWith(Trunc);
18521864
DeadInsts.emplace_back(UsePhi);
18531865
LLVM_DEBUG(dbgs() << "INDVARS: Widen lcssa phi " << *UsePhi << " to "
@@ -1857,26 +1869,18 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
18571869
}
18581870
}
18591871

1860-
// This narrow use can be widened by a sext if it's non-negative or its narrow
1861-
// def was widened by a sext. Same for zext.
1862-
auto canWidenBySExt = [&]() {
1863-
return DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Sign;
1864-
};
1865-
auto canWidenByZExt = [&]() {
1866-
return DU.NeverNegative || getExtendKind(DU.NarrowDef) == ExtendKind::Zero;
1867-
};
1868-
18691872
// Our raison d'etre! Eliminate sign and zero extension.
1870-
if ((match(DU.NarrowUse, m_SExtLike(m_Value())) && canWidenBySExt()) ||
1871-
(isa<ZExtInst>(DU.NarrowUse) && canWidenByZExt())) {
1873+
if ((match(DU.NarrowUse, m_SExtLike(m_Value())) && CanWidenBySExt) ||
1874+
(isa<ZExtInst>(DU.NarrowUse) && CanWidenByZExt)) {
18721875
Value *NewDef = DU.WideDef;
18731876
if (DU.NarrowUse->getType() != WideType) {
18741877
unsigned CastWidth = SE->getTypeSizeInBits(DU.NarrowUse->getType());
18751878
unsigned IVWidth = SE->getTypeSizeInBits(WideType);
18761879
if (CastWidth < IVWidth) {
18771880
// The cast isn't as wide as the IV, so insert a Trunc.
18781881
IRBuilder<> Builder(DU.NarrowUse);
1879-
NewDef = Builder.CreateTrunc(DU.WideDef, DU.NarrowUse->getType());
1882+
NewDef = Builder.CreateTrunc(DU.WideDef, DU.NarrowUse->getType(), "",
1883+
CanWidenByZExt, CanWidenBySExt);
18801884
}
18811885
else {
18821886
// A wider extend was hidden behind a narrower one. This may induce
@@ -1975,7 +1979,7 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
19751979
// This user does not evaluate to a recurrence after widening, so don't
19761980
// follow it. Instead insert a Trunc to kill off the original use,
19771981
// eventually isolating the original narrow IV so it can be removed.
1978-
truncateIVUse(DU, DT, LI);
1982+
truncateIVUse(DU);
19791983
return nullptr;
19801984
}
19811985

llvm/test/Transforms/IndVarSimplify/AArch64/widen-loop-comp.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ define i32 @test1() {
4141
; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN:%.*]], label [[FOR_COND]]
4242
; CHECK: if.then:
4343
; CHECK-NEXT: [[I_05_LCSSA_WIDE:%.*]] = phi i64 [ [[INDVARS_IV]], [[FOR_BODY]] ]
44-
; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[I_05_LCSSA_WIDE]] to i32
44+
; CHECK-NEXT: [[TMP5:%.*]] = trunc nuw nsw i64 [[I_05_LCSSA_WIDE]] to i32
4545
; CHECK-NEXT: store i32 [[TMP5]], ptr @idx, align 4
4646
; CHECK-NEXT: br label [[FOR_END:%.*]]
4747
; CHECK: for.cond.for.end.loopexit_crit_edge:
@@ -237,7 +237,7 @@ define i32 @test4(i32 %a) {
237237
; CHECK-NEXT: [[CONV3:%.*]] = trunc i32 [[OR]] to i8
238238
; CHECK-NEXT: [[CALL:%.*]] = call i32 @fn1(i8 signext [[CONV3]])
239239
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nsw i32 [[INDVARS_IV]], -1
240-
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[INDVARS_IV_NEXT]] to i8
240+
; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw i32 [[INDVARS_IV_NEXT]] to i8
241241
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[TMP0]], -14
242242
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END:%.*]]
243243
; CHECK: for.end:
@@ -466,7 +466,7 @@ define i32 @test9(ptr %a, i32 %b, i32 %init) {
466466
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
467467
; CHECK-NEXT: [[ADD]] = add nsw i32 [[SUM_0]], [[TMP1]]
468468
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
469-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
469+
; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[INDVARS_IV_NEXT]] to i32
470470
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 0, [[TMP2]]
471471
; CHECK-NEXT: br i1 [[CMP2]], label [[FOR_COND]], label [[FOR_END]]
472472
; CHECK: for.end:
@@ -997,7 +997,7 @@ define i32 @test16_unsigned_neg(i32 %start, ptr %p, ptr %q, i32 %x) {
997997
; CHECK: loop:
998998
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
999999
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[INDVARS_IV]], 0
1000-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[INDVARS_IV]] to i32
1000+
; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i64 [[INDVARS_IV]] to i32
10011001
; CHECK-NEXT: [[FOO:%.*]] = add i32 [[TMP1]], -1
10021002
; CHECK-NEXT: br i1 [[COND]], label [[EXIT:%.*]], label [[GUARDED:%.*]]
10031003
; CHECK: guarded:

llvm/test/Transforms/IndVarSimplify/X86/iv-widen.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ define void @loop_0(ptr %a) {
2323
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[B18_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[B24:%.*]] ]
2424
; CHECK-NEXT: call void @use(i64 [[INDVARS_IV]])
2525
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
26-
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[INDVARS_IV]] to i32
26+
; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw nsw i64 [[INDVARS_IV]] to i32
2727
; CHECK-NEXT: [[O:%.*]] = getelementptr i32, ptr [[A:%.*]], i32 [[TMP0]]
2828
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[O]], align 4
2929
; CHECK-NEXT: [[T:%.*]] = icmp eq i32 [[V]], 0
@@ -37,7 +37,7 @@ define void @loop_0(ptr %a) {
3737
; CHECK-NEXT: ret void
3838
; CHECK: exit24:
3939
; CHECK-NEXT: [[DOT02_LCSSA_WIDE:%.*]] = phi i64 [ [[INDVARS_IV]], [[B18]] ]
40-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[DOT02_LCSSA_WIDE]] to i32
40+
; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw nsw i64 [[DOT02_LCSSA_WIDE]] to i32
4141
; CHECK-NEXT: call void @dummy(i32 [[TMP1]])
4242
; CHECK-NEXT: unreachable
4343
;
@@ -159,7 +159,7 @@ declare void @dummy(i32)
159159
declare void @dummy.i64(i64)
160160

161161

162-
define void @loop_2(i32 %size, i32 %nsteps, i32 %hsize, ptr %lined, i8 %tmp1) {
162+
define void @loop_2(i32 %size, i32 %nsteps, i32 %hsize, ptr %lined, i8 %arg) {
163163
; CHECK-LABEL: @loop_2(
164164
; CHECK-NEXT: entry:
165165
; CHECK-NEXT: [[CMP215:%.*]] = icmp sgt i32 [[SIZE:%.*]], 1
@@ -180,20 +180,20 @@ define void @loop_2(i32 %size, i32 %nsteps, i32 %hsize, ptr %lined, i8 %tmp1) {
180180
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 1, [[FOR_BODY2_PREHEADER]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY2]] ]
181181
; CHECK-NEXT: [[TMP4:%.*]] = add nsw i64 [[TMP3]], [[INDVARS_IV]]
182182
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr [[LINED:%.*]], i64 [[TMP4]]
183-
; CHECK-NEXT: store i8 [[TMP1:%.*]], ptr [[ADD_PTR]], align 1
183+
; CHECK-NEXT: store i8 [[ARG:%.*]], ptr [[ADD_PTR]], align 1
184184
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
185185
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[WIDE_TRIP_COUNT]]
186186
; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_BODY2]], label [[FOR_BODY3_PREHEADER:%.*]]
187187
; CHECK: for.body3.preheader:
188-
; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[TMP3]] to i32
188+
; CHECK-NEXT: [[TMP5:%.*]] = trunc nsw i64 [[TMP3]] to i32
189189
; CHECK-NEXT: [[TMP6:%.*]] = zext i32 [[TMP5]] to i64
190190
; CHECK-NEXT: [[WIDE_TRIP_COUNT7:%.*]] = zext i32 [[SIZE]] to i64
191191
; CHECK-NEXT: br label [[FOR_BODY3:%.*]]
192192
; CHECK: for.body3:
193193
; CHECK-NEXT: [[INDVARS_IV3:%.*]] = phi i64 [ 1, [[FOR_BODY3_PREHEADER]] ], [ [[INDVARS_IV_NEXT4:%.*]], [[FOR_BODY3]] ]
194194
; CHECK-NEXT: [[TMP7:%.*]] = add nuw nsw i64 [[TMP6]], [[INDVARS_IV3]]
195195
; CHECK-NEXT: [[ADD_PTR2:%.*]] = getelementptr inbounds i8, ptr [[LINED]], i64 [[TMP7]]
196-
; CHECK-NEXT: store i8 [[TMP1]], ptr [[ADD_PTR2]], align 1
196+
; CHECK-NEXT: store i8 [[ARG]], ptr [[ADD_PTR2]], align 1
197197
; CHECK-NEXT: [[INDVARS_IV_NEXT4]] = add nuw nsw i64 [[INDVARS_IV3]], 1
198198
; CHECK-NEXT: [[EXITCOND8:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT4]], [[WIDE_TRIP_COUNT7]]
199199
; CHECK-NEXT: br i1 [[EXITCOND8]], label [[FOR_BODY3]], label [[FOR_INC_LOOPEXIT:%.*]]
@@ -222,7 +222,7 @@ for.body2:
222222
%add4 = add nsw i32 %add, %k
223223
%idx.ext = sext i32 %add4 to i64
224224
%add.ptr = getelementptr inbounds i8, ptr %lined, i64 %idx.ext
225-
store i8 %tmp1, ptr %add.ptr, align 1
225+
store i8 %arg, ptr %add.ptr, align 1
226226
%inc = add nsw i32 %k, 1
227227
%cmp2 = icmp slt i32 %inc, %size
228228
br i1 %cmp2, label %for.body2, label %for.body3
@@ -233,7 +233,7 @@ for.body3:
233233
%add5 = add nuw i32 %add, %l
234234
%idx.ext2 = zext i32 %add5 to i64
235235
%add.ptr2 = getelementptr inbounds i8, ptr %lined, i64 %idx.ext2
236-
store i8 %tmp1, ptr %add.ptr2, align 1
236+
store i8 %arg, ptr %add.ptr2, align 1
237237
%inc2 = add nsw i32 %l, 1
238238
%cmp3 = icmp slt i32 %inc2, %size
239239
br i1 %cmp3, label %for.body3, label %for.inc

llvm/test/Transforms/IndVarSimplify/elim-extend.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ define void @nestedIV(ptr %address, i32 %limit) nounwind {
142142
; CHECK-NEXT: br i1 [[EXITCOND]], label [[INNERLOOP]], label [[INNEREXIT:%.*]]
143143
; CHECK: innerexit:
144144
; CHECK-NEXT: [[INNERCOUNT_LCSSA_WIDE:%.*]] = phi i64 [ [[INDVARS_IV_NEXT]], [[INNERLOOP]] ]
145-
; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[INNERCOUNT_LCSSA_WIDE]] to i32
145+
; CHECK-NEXT: [[TMP3:%.*]] = trunc nsw i64 [[INNERCOUNT_LCSSA_WIDE]] to i32
146146
; CHECK-NEXT: br label [[OUTERMERGE]]
147147
; CHECK: outermerge:
148148
; CHECK-NEXT: [[INNERCOUNT_MERGE]] = phi i32 [ [[TMP3]], [[INNEREXIT]] ], [ [[INNERCOUNT]], [[INNERPREHEADER]] ]

llvm/test/Transforms/IndVarSimplify/hoist-wide-inc-for-narrow-use-recompute-flags.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ define void @test_pr82243(ptr %f) {
1515
; CHECK-NEXT: [[GEP_IV_EXT:%.*]] = getelementptr i32, ptr [[F]], i64 [[INDVARS_IV]]
1616
; CHECK-NEXT: store i32 1, ptr [[GEP_IV_EXT]], align 4
1717
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], -1
18-
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
18+
; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw nsw i64 [[INDVARS_IV_NEXT]] to i32
1919
; CHECK-NEXT: [[SHL:%.*]] = shl i32 123, [[TMP0]]
2020
; CHECK-NEXT: [[GEP_SHL:%.*]] = getelementptr i32, ptr [[F]], i32 [[SHL]]
2121
; CHECK-NEXT: br label [[INNER_HEADER:%.*]]

llvm/test/Transforms/IndVarSimplify/iv-sext.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ define void @t(ptr %pval1, ptr %peakWeight, ptr %nrgReducePeakrate, i32 %bandEdg
9999
; CHECK-NEXT: [[VAL35_LCSSA:%.*]] = phi float [ [[VAL35]], [[BB5]] ]
100100
; CHECK-NEXT: [[VAL31_LCSSA_WIDE:%.*]] = phi i64 [ [[INDVARS_IV_NEXT]], [[BB5]] ]
101101
; CHECK-NEXT: [[VAL30_LCSSA:%.*]] = phi float [ [[VAL30]], [[BB5]] ]
102-
; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[VAL31_LCSSA_WIDE]] to i32
102+
; CHECK-NEXT: [[TMP4:%.*]] = trunc nsw i64 [[VAL31_LCSSA_WIDE]] to i32
103103
; CHECK-NEXT: br label [[BB7]]
104104
; CHECK: bb7:
105105
; CHECK-NEXT: [[DISTERBHI_2_LCSSA]] = phi float [ [[VAL30_LCSSA]], [[BB5_BB7_CRIT_EDGE]] ], [ [[DISTERBHI_0_PH]], [[BB5_PREHEADER]] ]

llvm/test/Transforms/IndVarSimplify/iv-widen-elim-ext.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ define void @foo(ptr %A, ptr %B, ptr %C, i32 %N) {
2222
; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds i32, ptr [[C:%.*]], i64 [[TMP1]]
2323
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
2424
; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[TMP0]], [[TMP2]]
25-
; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP1]] to i32
25+
; CHECK-NEXT: [[TMP3:%.*]] = trunc nuw nsw i64 [[TMP1]] to i32
2626
; CHECK-NEXT: [[DIV0:%.*]] = udiv i32 5, [[TMP3]]
2727
; CHECK-NEXT: [[ADD4:%.*]] = add nsw i32 [[ADD3]], [[DIV0]]
2828
; CHECK-NEXT: [[ARRAYIDX5:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[INDVARS_IV]]
@@ -224,7 +224,7 @@ define i32 @foo3(i32 %M) {
224224
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
225225
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
226226
; CHECK-NEXT: [[TMP3:%.*]] = add nsw i64 [[INDVARS_IV]], [[TMP0]]
227-
; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[TMP3]] to i32
227+
; CHECK-NEXT: [[TMP4:%.*]] = trunc nsw i64 [[TMP3]] to i32
228228
; CHECK-NEXT: [[IDXPROM4:%.*]] = zext i32 [[TMP4]] to i64
229229
; CHECK-NEXT: [[ARRAYIDX5:%.*]] = getelementptr inbounds [100 x i32], ptr @a, i64 0, i64 [[IDXPROM4]]
230230
; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX5]], align 4
@@ -365,7 +365,7 @@ define i32 @foo5(ptr %input, i32 %length, ptr %in) {
365365
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ], [ 1, [[FOR_BODY_LR_PH]] ]
366366
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
367367
; CHECK-NEXT: [[TMP4:%.*]] = load i32, ptr [[INPUT]], align 8
368-
; CHECK-NEXT: [[TMP5:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
368+
; CHECK-NEXT: [[TMP5:%.*]] = trunc nuw nsw i64 [[INDVARS_IV_NEXT]] to i32
369369
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[TMP4]], [[TMP5]]
370370
; CHECK-NEXT: [[IDX_EXT:%.*]] = sext i32 [[MUL]] to i64
371371
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i32, ptr [[IN:%.*]], i64 [[IDX_EXT]]
@@ -514,7 +514,7 @@ define void @foo7(i32 %n, ptr %a, i32 %x) {
514514
; CHECK-NEXT: [[TMP2:%.*]] = shl nsw i64 [[INDVARS_IV]], 1
515515
; CHECK-NEXT: [[TMP3:%.*]] = or disjoint i64 [[TMP2]], 1
516516
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP3]]
517-
; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[INDVARS_IV]] to i32
517+
; CHECK-NEXT: [[TMP4:%.*]] = trunc nsw i64 [[INDVARS_IV]] to i32
518518
; CHECK-NEXT: store i32 [[TMP4]], ptr [[ARRAYIDX]], align 4
519519
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], [[TMP0]]
520520
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[INDVARS_IV_NEXT]], [[TMP1]]

llvm/test/Transforms/IndVarSimplify/lftr.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ define float @wide_trip_count_test3(ptr %b,
525525
; CHECK-NEXT: [[TMP0:%.*]] = add nsw i64 [[INDVARS_IV]], 20
526526
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds float, ptr [[B:%.*]], i64 [[TMP0]]
527527
; CHECK-NEXT: [[TEMP:%.*]] = load float, ptr [[ARRAYIDX]], align 4
528-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[INDVARS_IV]] to i32
528+
; CHECK-NEXT: [[TMP1:%.*]] = trunc nsw i64 [[INDVARS_IV]] to i32
529529
; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[TMP1]] to float
530530
; CHECK-NEXT: [[MUL:%.*]] = fmul float [[CONV]], [[TEMP]]
531531
; CHECK-NEXT: [[ADD1]] = fadd float [[SUM_07]], [[MUL]]
@@ -584,7 +584,7 @@ define float @wide_trip_count_test4(ptr %b,
584584
; CHECK-NEXT: [[TMP0:%.*]] = add nuw nsw i64 [[INDVARS_IV]], 20
585585
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds float, ptr [[B:%.*]], i64 [[TMP0]]
586586
; CHECK-NEXT: [[TEMP:%.*]] = load float, ptr [[ARRAYIDX]], align 4
587-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[INDVARS_IV]] to i32
587+
; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw nsw i64 [[INDVARS_IV]] to i32
588588
; CHECK-NEXT: [[CONV:%.*]] = sitofp i32 [[TMP1]] to float
589589
; CHECK-NEXT: [[MUL:%.*]] = fmul float [[CONV]], [[TEMP]]
590590
; CHECK-NEXT: [[ADD1]] = fadd float [[SUM_07]], [[MUL]]

llvm/test/Transforms/IndVarSimplify/no-iv-rewrite.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ define void @maxvisitor(i32 %limit, ptr %base) nounwind {
213213
; CHECK-NEXT: [[CMP19:%.*]] = icmp sgt i32 [[VAL]], [[MAX]]
214214
; CHECK-NEXT: br i1 [[CMP19]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
215215
; CHECK: if.then:
216-
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[INDVARS_IV]] to i32
216+
; CHECK-NEXT: [[TMP0:%.*]] = trunc nuw nsw i64 [[INDVARS_IV]] to i32
217217
; CHECK-NEXT: br label [[LOOP_INC]]
218218
; CHECK: if.else:
219219
; CHECK-NEXT: br label [[LOOP_INC]]

llvm/test/Transforms/IndVarSimplify/post-inc-range.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ define void @test_neg(ptr %array_length_ptr, ptr %base,
180180
; CHECK-NEXT: br label [[FOR_INC]]
181181
; CHECK: for.inc:
182182
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
183-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32
183+
; CHECK-NEXT: [[TMP2:%.*]] = trunc nuw i64 [[INDVARS_IV_NEXT]] to i32
184184
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP2]], [[LIMIT:%.*]]
185185
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END]]
186186
; CHECK: for.end:

llvm/test/Transforms/IndVarSimplify/pr25578.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ L1_header:
1313

1414
; CHECK: L2_header:
1515
; CHECK: %[[INDVAR:.*]] = phi i64
16-
; CHECK: %[[TRUNC:.*]] = trunc i64 %[[INDVAR]] to i32
16+
; CHECK: %[[TRUNC:.*]] = trunc nuw nsw i64 %[[INDVAR]] to i32
1717
L2_header:
1818
%i = phi i32 [ 0, %L1_header ], [ %i_next, %L2_latch ]
1919
%i_prom = sext i32 %i to i64

0 commit comments

Comments
 (0)