llvm.org GIT mirror llvm / 5a6c1a8
Strengthen the "non-constant stride must dominate loop preheader" check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64703 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 10 years ago
5 changed file(s) with 102 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
8787 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
8888 const SCEVHandle &Conc,
8989 ScalarEvolution &SE) const = 0;
90
91 /// dominates - Return true if elements that makes up this SCEV dominates
92 /// the specified basic block.
93 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
9094
9195 /// print - Print out the internal representation of this scalar to the
9296 /// specified stream. This should really only be used for debugging
122126 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
123127 const SCEVHandle &Conc,
124128 ScalarEvolution &SE) const;
129
130 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
131 return true;
132 }
125133
126134 /// Methods for support type inquiry through isa, cast, and dyn_cast:
127135 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
1919 class ConstantInt;
2020 class ConstantRange;
2121 class APInt;
22 class DominatorTree;
2223
2324 enum SCEVTypes {
2425 // These should be ordered in terms of increasing complexity to make the
5758 return this;
5859 }
5960
61 bool dominates(BasicBlock *BB, DominatorTree *DT) const {
62 return true;
63 }
64
6065 virtual void print(std::ostream &OS) const;
6166 void print(std::ostream *OS) const { if (OS) print(*OS); }
6267
99104 return SE.getTruncateExpr(H, Ty);
100105 }
101106
107 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
108
102109 virtual void print(std::ostream &OS) const;
103110 void print(std::ostream *OS) const { if (OS) print(*OS); }
104111
141148 return SE.getZeroExtendExpr(H, Ty);
142149 }
143150
151 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
152
144153 virtual void print(std::ostream &OS) const;
145154 void print(std::ostream *OS) const { if (OS) print(*OS); }
146155
182191 return this;
183192 return SE.getSignExtendExpr(H, Ty);
184193 }
194
195 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
185196
186197 virtual void print(std::ostream &OS) const;
187198 void print(std::ostream *OS) const { if (OS) print(*OS); }
249260 const SCEVHandle &Conc,
250261 ScalarEvolution &SE) const;
251262
263 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
264
252265 virtual const char *getOperationStr() const = 0;
253266
254267 virtual const Type *getType() const { return getOperand(0)->getType(); }
342355 return SE.getUDivExpr(L, R);
343356 }
344357
358 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
345359
346360 virtual const Type *getType() const;
347361
436450 const SCEVHandle &Conc,
437451 ScalarEvolution &SE) const;
438452
453 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
454
439455 virtual void print(std::ostream &OS) const;
440456 void print(std::ostream *OS) const { if (OS) print(*OS); }
441457
516532 if (&*Sym == this) return Conc;
517533 return this;
518534 }
535
536 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
519537
520538 virtual const Type *getType() const;
521539
6565 #include "llvm/GlobalVariable.h"
6666 #include "llvm/Instructions.h"
6767 #include "llvm/Analysis/ConstantFolding.h"
68 #include "llvm/Analysis/Dominators.h"
6869 #include "llvm/Analysis/LoopInfo.h"
6970 #include "llvm/Assembly/Writer.h"
7071 #include "llvm/Transforms/Scalar.h"
204205 SCEVTruncates->erase(std::make_pair(Op, Ty));
205206 }
206207
208 bool SCEVTruncateExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
209 return Op->dominates(BB, DT);
210 }
211
207212 void SCEVTruncateExpr::print(std::ostream &OS) const {
208213 OS << "(truncate " << *Op << " to " << *Ty << ")";
209214 }
226231 SCEVZeroExtends->erase(std::make_pair(Op, Ty));
227232 }
228233
234 bool SCEVZeroExtendExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
235 return Op->dominates(BB, DT);
236 }
237
229238 void SCEVZeroExtendExpr::print(std::ostream &OS) const {
230239 OS << "(zeroextend " << *Op << " to " << *Ty << ")";
231240 }
246255
247256 SCEVSignExtendExpr::~SCEVSignExtendExpr() {
248257 SCEVSignExtends->erase(std::make_pair(Op, Ty));
258 }
259
260 bool SCEVSignExtendExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
261 return Op->dominates(BB, DT);
249262 }
250263
251264 void SCEVSignExtendExpr::print(std::ostream &OS) const {
305318 return this;
306319 }
307320
321 bool SCEVCommutativeExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
322 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
323 if (!getOperand(i)->dominates(BB, DT))
324 return false;
325 }
326 return true;
327 }
328
308329
309330 // SCEVUDivs - Only allow the creation of one SCEVUDivExpr for any particular
310331 // input. Don't use a SCEVHandle here, or else the object will never be
316337 SCEVUDivs->erase(std::make_pair(LHS, RHS));
317338 }
318339
340 bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
341 return LHS->dominates(BB, DT) && RHS->dominates(BB, DT);
342 }
343
319344 void SCEVUDivExpr::print(std::ostream &OS) const {
320345 OS << "(" << *LHS << " /u " << *RHS << ")";
321346 }
335360 std::vector(Operands.begin(),
336361 Operands.end())));
337362 }
363
364 bool SCEVAddRecExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
365 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
366 if (!getOperand(i)->dominates(BB, DT))
367 return false;
368 }
369 return true;
370 }
371
338372
339373 SCEVHandle SCEVAddRecExpr::
340374 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
387421 // invariant if they are not contained in the specified loop.
388422 if (Instruction *I = dyn_cast(V))
389423 return !L->contains(I->getParent());
424 return true;
425 }
426
427 bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
428 if (Instruction *I = dyn_cast(getValue()))
429 return DT->dominates(I->getParent(), BB);
390430 return true;
391431 }
392432
437437 Start = SE->getAddExpr(Start, AddRec->getOperand(0));
438438
439439 if (!isa(AddRec->getOperand(1))) {
440 // If stride is an instruction, make sure it dominates the loop header.
440 // If stride is an instruction, make sure it dominates the loop preheader.
441441 // Otherwise we could end up with a use before def situation.
442 if (SCEVUnknown *SU = dyn_cast(AddRec->getOperand(1))) {
443 if (Instruction *I = dyn_cast(SU->getValue())) {
444 BasicBlock *StrideBB = I->getParent();
445 BasicBlock *Preheader = L->getLoopPreheader();
446 if (!DT->dominates(StrideBB, Preheader))
447 return false;
448 }
449 }
442 BasicBlock *Preheader = L->getLoopPreheader();
443 if (!AddRec->getOperand(1)->dominates(Preheader, DT))
444 return false;
450445
451446 DOUT << "[" << L->getHeader()->getName()
452447 << "] Variable stride: " << *AddRec << "\n";
0 ; RUN: llvm-as < %s | opt -loop-reduce | llvm-dis
1 ; PR3399
2
3 @g_53 = external global i32 ; [#uses=1]
4
5 define i32 @foo() nounwind {
6 bb5.thread:
7 br label %bb
8
9 bb: ; preds = %bb5, %bb5.thread
10 %indvar = phi i32 [ 0, %bb5.thread ], [ %indvar.next, %bb5 ] ; [#uses=2]
11 br i1 false, label %bb5, label %bb1
12
13 bb1: ; preds = %bb
14 %l_2.0.reg2mem.0 = sub i32 0, %indvar ; [#uses=1]
15 %0 = volatile load i32* @g_53, align 4 ; [#uses=1]
16 %1 = trunc i32 %l_2.0.reg2mem.0 to i16 ; [#uses=1]
17 %2 = trunc i32 %0 to i16 ; [#uses=1]
18 %3 = mul i16 %2, %1 ; [#uses=1]
19 %4 = icmp eq i16 %3, 0 ; [#uses=1]
20 br i1 %4, label %bb7, label %bb2
21
22 bb2: ; preds = %bb2, %bb1
23 br label %bb2
24
25 bb5: ; preds = %bb
26 %indvar.next = add i32 %indvar, 1 ; [#uses=1]
27 br label %bb
28
29 bb7: ; preds = %bb1
30 ret i32 1
31 }