llvm.org GIT mirror llvm / 78db186
Reapply 105540, 105542, and 105548, and revert r105732. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106304 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
4 changed file(s) with 78 addition(s) and 165 deletion(s). Raw diff Collapse all Expand all
5353 /// The ScalarEvolution's BumpPtrAllocator holds the data.
5454 FoldingSetNodeIDRef FastID;
5555
56 /// AllocationSequenceNumber - This is used as a deterministic tie
57 /// breaker when sorting SCEVs.
58 unsigned AllocationSequenceNumber;
59
5660 // The SCEV baseclass this node corresponds to
5761 const unsigned short SCEVType;
5862
6771 protected:
6872 virtual ~SCEV();
6973 public:
70 explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
71 FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
74 explicit SCEV(const FoldingSetNodeIDRef ID, unsigned num, unsigned SCEVTy) :
75 FastID(ID), AllocationSequenceNumber(num),
76 SCEVType(SCEVTy), SubclassData(0) {}
7277
7378 unsigned getSCEVType() const { return SCEVType; }
79
80 /// getAllocationSequenceNumber - Return an arbitrary value which can be
81 /// used to deterministically order a sequence of SCEVs.
82 unsigned getAllocationSequenceNumber() const {
83 return AllocationSequenceNumber;
84 }
7485
7586 /// Profile - FoldingSet support.
7687 void Profile(FoldingSetNodeID& ID) { ID = FastID; }
662673 private:
663674 FoldingSet UniqueSCEVs;
664675 BumpPtrAllocator SCEVAllocator;
676 unsigned CurAllocationSequenceNumber;
665677 };
666678 }
667679
3636 friend class ScalarEvolution;
3737
3838 ConstantInt *V;
39 SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
40 SCEV(ID, scConstant), V(v) {}
39 SCEVConstant(const FoldingSetNodeIDRef ID, unsigned Num, ConstantInt *v)
40 : SCEV(ID, Num, scConstant), V(v) {}
4141 public:
4242 ConstantInt *getValue() const { return V; }
4343
8080 const SCEV *Op;
8181 const Type *Ty;
8282
83 SCEVCastExpr(const FoldingSetNodeIDRef ID,
83 SCEVCastExpr(const FoldingSetNodeIDRef ID, unsigned Num,
8484 unsigned SCEVTy, const SCEV *op, const Type *ty);
8585
8686 public:
119119 class SCEVTruncateExpr : public SCEVCastExpr {
120120 friend class ScalarEvolution;
121121
122 SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
122 SCEVTruncateExpr(const FoldingSetNodeIDRef ID, unsigned Num,
123123 const SCEV *op, const Type *ty);
124124
125125 public:
139139 class SCEVZeroExtendExpr : public SCEVCastExpr {
140140 friend class ScalarEvolution;
141141
142 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
142 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, unsigned Num,
143143 const SCEV *op, const Type *ty);
144144
145145 public:
159159 class SCEVSignExtendExpr : public SCEVCastExpr {
160160 friend class ScalarEvolution;
161161
162 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
162 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, unsigned Num,
163163 const SCEV *op, const Type *ty);
164164
165165 public:
186186 const SCEV *const *Operands;
187187 size_t NumOperands;
188188
189 SCEVNAryExpr(const FoldingSetNodeIDRef ID,
189 SCEVNAryExpr(const FoldingSetNodeIDRef ID, unsigned Num,
190190 enum SCEVTypes T, const SCEV *const *O, size_t N)
191 : SCEV(ID, T), Operands(O), NumOperands(N) {}
191 : SCEV(ID, Num, T), Operands(O), NumOperands(N) {}
192192
193193 public:
194194 size_t getNumOperands() const { return NumOperands; }
261261 ///
262262 class SCEVCommutativeExpr : public SCEVNAryExpr {
263263 protected:
264 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
264 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID, unsigned Num,
265265 enum SCEVTypes T, const SCEV *const *O, size_t N)
266 : SCEVNAryExpr(ID, T, O, N) {}
266 : SCEVNAryExpr(ID, Num, T, O, N) {}
267267
268268 public:
269269 virtual const char *getOperationStr() const = 0;
287287 class SCEVAddExpr : public SCEVCommutativeExpr {
288288 friend class ScalarEvolution;
289289
290 SCEVAddExpr(const FoldingSetNodeIDRef ID,
290 SCEVAddExpr(const FoldingSetNodeIDRef ID, unsigned Num,
291291 const SCEV *const *O, size_t N)
292 : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
292 : SCEVCommutativeExpr(ID, Num, scAddExpr, O, N) {
293293 }
294294
295295 public:
315315 class SCEVMulExpr : public SCEVCommutativeExpr {
316316 friend class ScalarEvolution;
317317
318 SCEVMulExpr(const FoldingSetNodeIDRef ID,
318 SCEVMulExpr(const FoldingSetNodeIDRef ID, unsigned Num,
319319 const SCEV *const *O, size_t N)
320 : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
320 : SCEVCommutativeExpr(ID, Num, scMulExpr, O, N) {
321321 }
322322
323323 public:
339339
340340 const SCEV *LHS;
341341 const SCEV *RHS;
342 SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
343 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
342 SCEVUDivExpr(const FoldingSetNodeIDRef ID, unsigned Num,
343 const SCEV *lhs, const SCEV *rhs)
344 : SCEV(ID, Num, scUDivExpr), LHS(lhs), RHS(rhs) {}
344345
345346 public:
346347 const SCEV *getLHS() const { return LHS; }
389390
390391 const Loop *L;
391392
392 SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
393 SCEVAddRecExpr(const FoldingSetNodeIDRef ID, unsigned Num,
393394 const SCEV *const *O, size_t N, const Loop *l)
394 : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {
395 : SCEVNAryExpr(ID, Num, scAddRecExpr, O, N), L(l) {
395396 for (size_t i = 0, e = NumOperands; i != e; ++i)
396397 assert(Operands[i]->isLoopInvariant(l) &&
397398 "Operands of AddRec must be loop-invariant!");
471472 class SCEVSMaxExpr : public SCEVCommutativeExpr {
472473 friend class ScalarEvolution;
473474
474 SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
475 SCEVSMaxExpr(const FoldingSetNodeIDRef ID, unsigned Num,
475476 const SCEV *const *O, size_t N)
476 : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
477 : SCEVCommutativeExpr(ID, Num, scSMaxExpr, O, N) {
477478 // Max never overflows.
478479 setHasNoUnsignedWrap(true);
479480 setHasNoSignedWrap(true);
496497 class SCEVUMaxExpr : public SCEVCommutativeExpr {
497498 friend class ScalarEvolution;
498499
499 SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
500 SCEVUMaxExpr(const FoldingSetNodeIDRef ID, unsigned Num,
500501 const SCEV *const *O, size_t N)
501 : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
502 : SCEVCommutativeExpr(ID, Num, scUMaxExpr, O, N) {
502503 // Max never overflows.
503504 setHasNoUnsignedWrap(true);
504505 setHasNoSignedWrap(true);
523524 friend class ScalarEvolution;
524525
525526 Value *V;
526 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *v) :
527 SCEV(ID, scUnknown), V(v) {}
527 SCEVUnknown(const FoldingSetNodeIDRef ID, unsigned Num, Value *v)
528 : SCEV(ID, Num, scUnknown), V(v) {}
528529
529530 public:
530531 Value *getValue() const { return V; }
140140 }
141141
142142 SCEVCouldNotCompute::SCEVCouldNotCompute() :
143 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {}
143 SCEV(FoldingSetNodeIDRef(), 0, scCouldNotCompute) {}
144144
145145 bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
146146 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
176176 ID.AddPointer(V);
177177 void *IP = 0;
178178 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
179 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
179 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator),
180 CurAllocationSequenceNumber++,
181 V);
180182 UniqueSCEVs.InsertNode(S, IP);
181183 return S;
182184 }
197199 WriteAsOperand(OS, V, false);
198200 }
199201
200 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
202 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, unsigned Num,
201203 unsigned SCEVTy, const SCEV *op, const Type *ty)
202 : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
204 : SCEV(ID, Num, SCEVTy), Op(op), Ty(ty) {}
203205
204206 bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
205207 return Op->dominates(BB, DT);
209211 return Op->properlyDominates(BB, DT);
210212 }
211213
212 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
214 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, unsigned Num,
213215 const SCEV *op, const Type *ty)
214 : SCEVCastExpr(ID, scTruncate, op, ty) {
216 : SCEVCastExpr(ID, Num, scTruncate, op, ty) {
215217 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
216218 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
217219 "Cannot truncate non-integer value!");
221223 OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
222224 }
223225
224 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
226 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, unsigned Num,
225227 const SCEV *op, const Type *ty)
226 : SCEVCastExpr(ID, scZeroExtend, op, ty) {
228 : SCEVCastExpr(ID, Num, scZeroExtend, op, ty) {
227229 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
228230 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
229231 "Cannot zero extend non-integer value!");
233235 OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
234236 }
235237
236 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
238 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, unsigned Num,
237239 const SCEV *op, const Type *ty)
238 : SCEVCastExpr(ID, scSignExtend, op, ty) {
240 : SCEVCastExpr(ID, Num, scSignExtend, op, ty) {
239241 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
240242 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
241243 "Cannot sign extend non-integer value!");
504506 return false;
505507
506508 // Primarily, sort the SCEVs by their getSCEVType().
507 if (LHS->getSCEVType() != RHS->getSCEVType())
508 return LHS->getSCEVType() < RHS->getSCEVType();
509
510 // Aside from the getSCEVType() ordering, the particular ordering
511 // isn't very important except that it's beneficial to be consistent,
512 // so that (a + b) and (b + a) don't end up as different expressions.
513
514 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
515 // not as complete as it could be.
516 if (const SCEVUnknown *LU = dyn_cast(LHS)) {
517 const SCEVUnknown *RU = cast(RHS);
518
519 // Order pointer values after integer values. This helps SCEVExpander
520 // form GEPs.
521 if (LU->getType()->isPointerTy() && !RU->getType()->isPointerTy())
522 return false;
523 if (RU->getType()->isPointerTy() && !LU->getType()->isPointerTy())
524 return true;
525
526 // Compare getValueID values.
527 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
528 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
529
530 // Sort arguments by their position.
531 if (const Argument *LA = dyn_cast(LU->getValue())) {
532 const Argument *RA = cast(RU->getValue());
533 return LA->getArgNo() < RA->getArgNo();
534 }
535
536 // For instructions, compare their loop depth, and their opcode.
537 // This is pretty loose.
538 if (Instruction *LV = dyn_cast(LU->getValue())) {
539 Instruction *RV = cast(RU->getValue());
540
541 // Compare loop depths.
542 if (LI->getLoopDepth(LV->getParent()) !=
543 LI->getLoopDepth(RV->getParent()))
544 return LI->getLoopDepth(LV->getParent()) <
545 LI->getLoopDepth(RV->getParent());
546
547 // Compare opcodes.
548 if (LV->getOpcode() != RV->getOpcode())
549 return LV->getOpcode() < RV->getOpcode();
550
551 // Compare the number of operands.
552 if (LV->getNumOperands() != RV->getNumOperands())
553 return LV->getNumOperands() < RV->getNumOperands();
554 }
555
556 return false;
557 }
558
559 // Compare constant values.
560 if (const SCEVConstant *LC = dyn_cast(LHS)) {
561 const SCEVConstant *RC = cast(RHS);
562 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
563 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
564 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
565 }
566
567 // Compare addrec loop depths.
568 if (const SCEVAddRecExpr *LA = dyn_cast(LHS)) {
569 const SCEVAddRecExpr *RA = cast(RHS);
570 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
571 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
572 }
573
574 // Lexicographically compare n-ary expressions.
575 if (const SCEVNAryExpr *LC = dyn_cast(LHS)) {
576 const SCEVNAryExpr *RC = cast(RHS);
577 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
578 if (i >= RC->getNumOperands())
579 return false;
580 if (operator()(LC->getOperand(i), RC->getOperand(i)))
581 return true;
582 if (operator()(RC->getOperand(i), LC->getOperand(i)))
583 return false;
584 }
585 return LC->getNumOperands() < RC->getNumOperands();
586 }
587
588 // Lexicographically compare udiv expressions.
589 if (const SCEVUDivExpr *LC = dyn_cast(LHS)) {
590 const SCEVUDivExpr *RC = cast(RHS);
591 if (operator()(LC->getLHS(), RC->getLHS()))
592 return true;
593 if (operator()(RC->getLHS(), LC->getLHS()))
594 return false;
595 if (operator()(LC->getRHS(), RC->getRHS()))
596 return true;
597 if (operator()(RC->getRHS(), LC->getRHS()))
598 return false;
599 return false;
600 }
601
602 // Compare cast expressions by operand.
603 if (const SCEVCastExpr *LC = dyn_cast(LHS)) {
604 const SCEVCastExpr *RC = cast(RHS);
605 return operator()(LC->getOperand(), RC->getOperand());
606 }
607
608 llvm_unreachable("Unknown SCEV kind!");
609 return false;
509 unsigned LST = LHS->getSCEVType();
510 unsigned RST = RHS->getSCEVType();
511 if (LST != RST)
512 return LST < RST;
513
514 // Then, pick an arbitrary deterministic sort.
515 return LHS->getAllocationSequenceNumber() <
516 RHS->getAllocationSequenceNumber();
610517 }
611518 };
612519 }
624531 static void GroupByComplexity(SmallVectorImpl &Ops,
625532 LoopInfo *LI) {
626533 if (Ops.size() < 2) return; // Noop
534
535 SCEVComplexityCompare Comp(LI);
536
627537 if (Ops.size() == 2) {
628538 // This is the common case, which also happens to be trivially simple.
629539 // Special case it.
630 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
540 if (Comp(Ops[1], Ops[0]))
631541 std::swap(Ops[0], Ops[1]);
632542 return;
633543 }
634544
635 // Do the rough sort by complexity.
636 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
637
638 // Now that we are sorted by complexity, group elements of the same
639 // complexity. Note that this is, at worst, N^2, but the vector is likely to
640 // be extremely short in practice. Note that we take this approach because we
641 // do not want to depend on the addresses of the objects we are grouping.
642 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
643 const SCEV *S = Ops[i];
644 unsigned Complexity = S->getSCEVType();
645
646 // If there are any objects of the same complexity and same value as this
647 // one, group them.
648 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
649 if (Ops[j] == S) { // Found a duplicate.
650 // Move it to immediately after i'th element.
651 std::swap(Ops[i+1], Ops[j]);
652 ++i; // no need to rescan it.
653 if (i == e-2) return; // Done!
654 }
655 }
656 }
545 std::stable_sort(Ops.begin(), Ops.end(), Comp);
657546 }
658547
659548
847736 // Recompute the insert position, as it may have been invalidated.
848737 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
849738 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
739 CurAllocationSequenceNumber++,
850740 Op, Ty);
851741 UniqueSCEVs.InsertNode(S, IP);
852742 return S;
982872 // Recompute the insert position, as it may have been invalidated.
983873 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
984874 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
875 CurAllocationSequenceNumber++,
985876 Op, Ty);
986877 UniqueSCEVs.InsertNode(S, IP);
987878 return S;
11171008 // Recompute the insert position, as it may have been invalidated.
11181009 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
11191010 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1011 CurAllocationSequenceNumber++,
11201012 Op, Ty);
11211013 UniqueSCEVs.InsertNode(S, IP);
11221014 return S;
16181510 const SCEV **O = SCEVAllocator.Allocate(Ops.size());
16191511 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
16201512 S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator),
1513 CurAllocationSequenceNumber++,
16211514 O, Ops.size());
16221515 UniqueSCEVs.InsertNode(S, IP);
16231516 }
18201713 const SCEV **O = SCEVAllocator.Allocate(Ops.size());
18211714 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
18221715 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
1716 CurAllocationSequenceNumber++,
18231717 O, Ops.size());
18241718 UniqueSCEVs.InsertNode(S, IP);
18251719 }
19241818 void *IP = 0;
19251819 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
19261820 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
1821 CurAllocationSequenceNumber++,
19271822 LHS, RHS);
19281823 UniqueSCEVs.InsertNode(S, IP);
19291824 return S;
20351930 const SCEV **O = SCEVAllocator.Allocate(Operands.size());
20361931 std::uninitialized_copy(Operands.begin(), Operands.end(), O);
20371932 S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator),
1933 CurAllocationSequenceNumber++,
20381934 O, Operands.size(), L);
20391935 UniqueSCEVs.InsertNode(S, IP);
20401936 }
21432039 const SCEV **O = SCEVAllocator.Allocate(Ops.size());
21442040 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
21452041 SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator),
2042 CurAllocationSequenceNumber++,
21462043 O, Ops.size());
21472044 UniqueSCEVs.InsertNode(S, IP);
21482045 return S;
22482145 const SCEV **O = SCEVAllocator.Allocate(Ops.size());
22492146 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
22502147 SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator),
2148 CurAllocationSequenceNumber++,
22512149 O, Ops.size());
22522150 UniqueSCEVs.InsertNode(S, IP);
22532151 return S;
23282226 ID.AddPointer(V);
23292227 void *IP = 0;
23302228 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2331 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V);
2229 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator),
2230 CurAllocationSequenceNumber++,
2231 V);
23322232 UniqueSCEVs.InsertNode(S, IP);
23332233 return S;
23342234 }
56815581 //===----------------------------------------------------------------------===//
56825582
56835583 ScalarEvolution::ScalarEvolution()
5684 : FunctionPass(&ID) {
5584 : FunctionPass(&ID), CurAllocationSequenceNumber(0) {
56855585 }
56865586
56875587 bool ScalarEvolution::runOnFunction(Function &F) {
2121 ret i32 %j.0.lcssa
2222 }
2323
24 ; CHECK: backedge-taken count is (-2147483632 + ((-1 + (-1 * %{{[xy]}})) smax (-1 + (-1 * %{{[xy]}}))))
24 ; CHECK: backedge-taken count is (-2147483632 + ((-1 + (-1 * %y)) smax (-1 + (-1 * %x))))
2525