Skip to content

Commit f1efb8b

Browse files
author
Chen Zheng
committed
[SCEV][IndVarSimplify] insert point should not be block front.
The block front may be a PHI node, inserting a cast instructions like BitCast, PtrToInt, IntToPtr among PHIs is not right. Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D80975
1 parent fbef6c5 commit f1efb8b

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,8 +1435,12 @@ PHINode *WidenIV::createWideIV(SCEVExpander &Rewriter) {
14351435
// either find an existing phi or materialize a new one. Either way, we
14361436
// expect a well-formed cyclic phi-with-increments. i.e. any operand not part
14371437
// of the phi-SCC dominates the loop entry.
1438-
Instruction *InsertPt = &L->getHeader()->front();
1439-
WidePhi = cast<PHINode>(Rewriter.expandCodeFor(AddRec, WideType, InsertPt));
1438+
Instruction *InsertPt = &*L->getHeader()->getFirstInsertionPt();
1439+
WidePhi = dyn_cast<PHINode>(Rewriter.expandCodeFor(AddRec, WideType, InsertPt));
1440+
// If the wide phi is not a phi node, for example a cast node, like bitcast,
1441+
// inttoptr, ptrtoint, just skip for now.
1442+
if (!WidePhi)
1443+
return nullptr;
14401444

14411445
// Remembering the WideIV increment generated by SCEVExpander allows
14421446
// widenIVUse to reuse it when widening the narrow IV's increment. We don't

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,8 @@ SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
12921292
if (useSubtract)
12931293
Step = SE.getNegativeSCEV(Step);
12941294
// Expand the step somewhere that dominates the loop header.
1295-
Value *StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front());
1295+
Value *StepV = expandCodeFor(Step, IntTy,
1296+
&*L->getHeader()->getFirstInsertionPt());
12961297

12971298
// The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
12981299
// we actually do emit an addition. It does not apply if we emit a
@@ -1438,7 +1439,8 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
14381439
{
14391440
// Expand the step somewhere that dominates the loop header.
14401441
SCEVInsertPointGuard Guard(Builder, this);
1441-
StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front());
1442+
StepV = expandCodeFor(Step, IntTy,
1443+
&*L->getHeader()->getFirstInsertionPt());
14421444
}
14431445
Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
14441446
}
@@ -1870,11 +1872,6 @@ Value *SCEVExpander::expand(const SCEV *S) {
18701872
}
18711873
}
18721874

1873-
// IndVarSimplify sometimes sets the insertion point at the block start, even
1874-
// when there are PHIs at that point. We must correct for this.
1875-
if (isa<PHINode>(*InsertPt))
1876-
InsertPt = &*InsertPt->getParent()->getFirstInsertionPt();
1877-
18781875
// Check to see if we already expanded this here.
18791876
auto I = InsertedExpressions.find(std::make_pair(S, InsertPt));
18801877
if (I != InsertedExpressions.end())
@@ -1945,7 +1942,8 @@ SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
19451942
// Emit code for it.
19461943
SCEVInsertPointGuard Guard(Builder, this);
19471944
PHINode *V =
1948-
cast<PHINode>(expandCodeFor(H, nullptr, &L->getHeader()->front()));
1945+
cast<PHINode>(expandCodeFor(H, nullptr,
1946+
&*L->getHeader()->getFirstInsertionPt()));
19491947

19501948
return V;
19511949
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; RUN: opt < %s -indvars -S | FileCheck %s
2+
3+
target datalayout = "e-m:e-i64:64-n32:64"
4+
5+
define dso_local void @Widen_i32_i8ptr() local_unnamed_addr {
6+
; CHECK-LABEL: @Widen_i32_i8ptr(
7+
; CHECK: phi i8*
8+
; CHECK: phi i32
9+
entry:
10+
%ptrids = alloca [15 x i8*], align 8
11+
%arraydecay2032 = getelementptr inbounds [15 x i8*], [15 x i8*]* %ptrids, i64 0, i64 0
12+
store i8** %arraydecay2032, i8*** inttoptr (i64 8 to i8***), align 8
13+
br label %for.cond2106
14+
15+
for.cond2106: ; preds = %for.cond2106, %entry
16+
%gid.0 = phi i8* [ null, %entry ], [ %incdec.ptr, %for.cond2106 ]
17+
%i.0 = phi i32 [ 0, %entry ], [ %inc2117, %for.cond2106 ]
18+
%incdec.ptr = getelementptr inbounds i8, i8* %gid.0, i64 1
19+
%idxprom2114 = zext i32 %i.0 to i64
20+
%arrayidx2115 = getelementptr inbounds [15 x i8*], [15 x i8*]* %ptrids, i64 0, i64 %idxprom2114
21+
store i8* %gid.0, i8** %arrayidx2115, align 8
22+
%inc2117 = add nuw nsw i32 %i.0, 1
23+
br label %for.cond2106
24+
}

0 commit comments

Comments
 (0)