Skip to content

Commit 11405df

Browse files
committed
Merge 64579 from mainline.
Fix pr3571: If stride is a value defined by an instruction, make sure it dominates the loop preheader. When IV users are strength reduced, the stride is inserted into the preheader. It could create a use before def situation. llvm-svn: 64789
1 parent bc5b640 commit 11405df

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static bool containsAddRecFromDifferentLoop(SCEVHandle S, Loop *L) {
400400
/// outer loop of the current loop.
401401
static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
402402
SCEVHandle &Start, SCEVHandle &Stride,
403-
ScalarEvolution *SE) {
403+
ScalarEvolution *SE, DominatorTree *DT) {
404404
SCEVHandle TheAddRec = Start; // Initialize to zero.
405405

406406
// If the outer level is an AddExpr, the operands are all start values except
@@ -437,9 +437,21 @@ static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
437437

438438
Start = SE->getAddExpr(Start, AddRec->getOperand(0));
439439

440-
if (!isa<SCEVConstant>(AddRec->getOperand(1)))
440+
if (!isa<SCEVConstant>(AddRec->getOperand(1))) {
441+
// If stride is an instruction, make sure it dominates the loop header.
442+
// Otherwise we could end up with a use before def situation.
443+
if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(AddRec->getOperand(1))) {
444+
if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
445+
BasicBlock *StrideBB = I->getParent();
446+
BasicBlock *Preheader = L->getLoopPreheader();
447+
if (!DT->dominates(StrideBB, Preheader))
448+
return false;
449+
}
450+
}
451+
441452
DOUT << "[" << L->getHeader()->getName()
442453
<< "] Variable stride: " << *AddRec << "\n";
454+
}
443455

444456
Stride = AddRec->getOperand(1);
445457
return true;
@@ -547,7 +559,7 @@ bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
547559
// Get the start and stride for this expression.
548560
SCEVHandle Start = SE->getIntegerSCEV(0, ISE->getType());
549561
SCEVHandle Stride = Start;
550-
if (!getSCEVStartAndStride(ISE, L, Start, Stride, SE))
562+
if (!getSCEVStartAndStride(ISE, L, Start, Stride, SE, DT))
551563
return false; // Non-reducible symbolic expression, bail out.
552564

553565
std::vector<Instruction *> IUsers;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: llvm-as < %s | opt -loop-reduce | llvm-dis
2+
; PR3571
3+
4+
target triple = "i386-mingw32"
5+
define void @_ZNK18qdesigner_internal10TreeWidget12drawBranchesEP8QPainterRK5QRectRK11QModelIndex() nounwind {
6+
entry:
7+
br label %_ZNK11QModelIndex7isValidEv.exit.i
8+
9+
bb.i: ; preds = %_ZNK11QModelIndex7isValidEv.exit.i
10+
%indvar.next = add i32 %result.0.i, 1 ; <i32> [#uses=1]
11+
br label %_ZNK11QModelIndex7isValidEv.exit.i
12+
13+
_ZNK11QModelIndex7isValidEv.exit.i: ; preds = %bb.i, %entry
14+
%result.0.i = phi i32 [ 0, %entry ], [ %indvar.next, %bb.i ] ; <i32> [#uses=2]
15+
%0 = load i32** null, align 4 ; <%struct.QAbstractItemDelegate*> [#uses=0]
16+
br i1 false, label %_ZN18qdesigner_internalL5levelEP18QAbstractItemModelRK11QModelIndex.exit, label %bb.i
17+
18+
_ZN18qdesigner_internalL5levelEP18QAbstractItemModelRK11QModelIndex.exit: ; preds = %_ZNK11QModelIndex7isValidEv.exit.i
19+
%1 = call i32 @_ZNK9QTreeView11indentationEv(i32* null) nounwind ; <i32> [#uses=1]
20+
%2 = mul i32 %1, %result.0.i ; <i32> [#uses=1]
21+
%3 = add i32 %2, -2 ; <i32> [#uses=1]
22+
%4 = add i32 %3, 0 ; <i32> [#uses=1]
23+
store i32 %4, i32* null, align 8
24+
unreachable
25+
}
26+
27+
declare i32 @_ZNK9QTreeView11indentationEv(i32*)

0 commit comments

Comments
 (0)