llvm.org GIT mirror llvm / 2044941
Move replaceCongruentIVs into SCEVExapander and bias toward "expanded" IVs. Indvars previously chose randomly between congruent IVs. Now it will bias the decision toward IVs that SCEVExpander likes to create. This was not done to fix any problem, it's just a welcome side effect of factoring code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141633 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Trick 8 years ago
4 changed file(s) with 129 addition(s) and 88 deletion(s). Raw diff Collapse all Expand all
7171 typedef IRBuilder BuilderType;
7272 BuilderType Builder;
7373
74 #ifndef NDEBUG
75 const char *DebugType;
76 #endif
77
7478 friend struct SCEVVisitor;
7579
7680 public:
7882 explicit SCEVExpander(ScalarEvolution &se, const char *name)
7983 : SE(se), IVName(name), IVIncInsertLoop(0), IVIncInsertPos(0),
8084 CanonicalMode(true), LSRMode(false),
81 Builder(se.getContext(), TargetFolder(se.TD)) {}
85 Builder(se.getContext(), TargetFolder(se.TD)) {
86 #ifndef NDEBUG
87 DebugType = "";
88 #endif
89 }
90
91 #ifndef NDEBUG
92 void setDebugType(const char* s) { DebugType = s; }
93 #endif
8294
8395 /// clear - Erase the contents of the InsertedExpressions map so that users
8496 /// trying to expand the same expression into multiple BasicBlocks or
95107 /// starts at zero and steps by one on each iteration.
96108 PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
97109
110 /// hoistStep - Utility for hoisting an IV increment.
111 static bool hoistStep(Instruction *IncV, Instruction *InsertPos,
112 const DominatorTree *DT);
113
114 /// replaceCongruentIVs - replace congruent phis with their most canonical
115 /// representative. Return the number of phis eliminated.
116 unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
117 SmallVectorImpl &DeadInsts);
118
98119 /// expandCodeFor - Insert code to directly compute the specified SCEV
99120 /// expression into the program. The inserted code is inserted into the
100121 /// specified block.
14641464
14651465 return V;
14661466 }
1467
1468 /// hoistStep - Attempt to hoist an IV increment above a potential use.
1469 ///
1470 /// To successfully hoist, two criteria must be met:
1471 /// - IncV operands dominate InsertPos and
1472 /// - InsertPos dominates IncV
1473 ///
1474 /// Meeting the second condition means that we don't need to check all of IncV's
1475 /// existing uses (it's moving up in the domtree).
1476 ///
1477 /// This does not yet recursively hoist the operands, although that would
1478 /// not be difficult.
1479 ///
1480 /// This does not require a SCEVExpander instance and could be replaced by a
1481 /// general code-insertion helper.
1482 bool SCEVExpander::hoistStep(Instruction *IncV, Instruction *InsertPos,
1483 const DominatorTree *DT) {
1484 if (DT->dominates(IncV, InsertPos))
1485 return true;
1486
1487 if (!DT->dominates(InsertPos->getParent(), IncV->getParent()))
1488 return false;
1489
1490 if (IncV->mayHaveSideEffects())
1491 return false;
1492
1493 // Attempt to hoist IncV
1494 for (User::op_iterator OI = IncV->op_begin(), OE = IncV->op_end();
1495 OI != OE; ++OI) {
1496 Instruction *OInst = dyn_cast(OI);
1497 if (OInst && !DT->dominates(OInst, InsertPos))
1498 return false;
1499 }
1500 IncV->moveBefore(InsertPos);
1501 return true;
1502 }
1503
1504 /// replaceCongruentIVs - Check for congruent phis in this loop header and
1505 /// replace them with their most canonical representative. Return the number of
1506 /// phis eliminated.
1507 ///
1508 /// This does not depend on any SCEVExpander state but should be used in
1509 /// the same context that SCEVExpander is used.
1510 unsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1511 SmallVectorImpl &DeadInsts) {
1512 unsigned NumElim = 0;
1513 DenseMap ExprToIVMap;
1514 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ++I) {
1515 PHINode *Phi = cast(I);
1516 if (!SE.isSCEVable(Phi->getType()))
1517 continue;
1518
1519 PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)];
1520 if (!OrigPhiRef) {
1521 OrigPhiRef = Phi;
1522 continue;
1523 }
1524
1525 // If one phi derives from the other via GEPs, types may differ.
1526 // We could consider adding a bitcast here to handle it.
1527 if (OrigPhiRef->getType() != Phi->getType())
1528 continue;
1529
1530 if (BasicBlock *LatchBlock = L->getLoopLatch()) {
1531 Instruction *OrigInc =
1532 cast(OrigPhiRef->getIncomingValueForBlock(LatchBlock));
1533 Instruction *IsomorphicInc =
1534 cast(Phi->getIncomingValueForBlock(LatchBlock));
1535
1536 // If this phi is more canonical, swap it with the original.
1537 if (!isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L,
1538 OrigPhiRef->getType())
1539 && isExpandedAddRecExprPHI(Phi, IsomorphicInc, L, Phi->getType())) {
1540 std::swap(OrigPhiRef, Phi);
1541 std::swap(OrigInc, IsomorphicInc);
1542 }
1543 // Replacing the congruent phi is sufficient because acyclic redundancy
1544 // elimination, CSE/GVN, should handle the rest. However, once SCEV proves
1545 // that a phi is congruent, it's often the head of an IV user cycle that
1546 // is isomorphic with the original phi. So it's worth eagerly cleaning up
1547 // the common case of a single IV increment.
1548 if (OrigInc != IsomorphicInc &&
1549 OrigInc->getType() == IsomorphicInc->getType() &&
1550 SE.getSCEV(OrigInc) == SE.getSCEV(IsomorphicInc) &&
1551 hoistStep(OrigInc, IsomorphicInc, DT)) {
1552 DEBUG_WITH_TYPE(DebugType, dbgs()
1553 << "INDVARS: Eliminated congruent iv.inc: "
1554 << *IsomorphicInc << '\n');
1555 IsomorphicInc->replaceAllUsesWith(OrigInc);
1556 DeadInsts.push_back(IsomorphicInc);
1557 }
1558 }
1559 DEBUG_WITH_TYPE(DebugType, dbgs()
1560 << "INDVARS: Eliminated congruent iv: " << *Phi << '\n');
1561 ++NumElim;
1562 Phi->replaceAllUsesWith(OrigPhiRef);
1563 DeadInsts.push_back(Phi);
1564 }
1565 return NumElim;
1566 }
118118
119119 void SimplifyAndExtend(Loop *L, SCEVExpander &Rewriter, LPPassManager &LPM);
120120
121 void SimplifyCongruentIVs(Loop *L);
122
123121 void RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter);
124122
125123 void RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter);
917915 return WideBO;
918916 }
919917 llvm_unreachable(0);
920 }
921
922 /// HoistStep - Attempt to hoist an IV increment above a potential use.
923 ///
924 /// To successfully hoist, two criteria must be met:
925 /// - IncV operands dominate InsertPos and
926 /// - InsertPos dominates IncV
927 ///
928 /// Meeting the second condition means that we don't need to check all of IncV's
929 /// existing uses (it's moving up in the domtree).
930 ///
931 /// This does not yet recursively hoist the operands, although that would
932 /// not be difficult.
933 static bool HoistStep(Instruction *IncV, Instruction *InsertPos,
934 const DominatorTree *DT)
935 {
936 if (DT->dominates(IncV, InsertPos))
937 return true;
938
939 if (!DT->dominates(InsertPos->getParent(), IncV->getParent()))
940 return false;
941
942 if (IncV->mayHaveSideEffects())
943 return false;
944
945 // Attempt to hoist IncV
946 for (User::op_iterator OI = IncV->op_begin(), OE = IncV->op_end();
947 OI != OE; ++OI) {
948 Instruction *OInst = dyn_cast(OI);
949 if (OInst && !DT->dominates(OInst, InsertPos))
950 return false;
951 }
952 IncV->moveBefore(InsertPos);
953 return true;
954918 }
955919
956920 /// No-wrap operations can transfer sign extension of their result to their
10831047 // Reuse the IV increment that SCEVExpander created as long as it dominates
10841048 // NarrowUse.
10851049 Instruction *WideUse = 0;
1086 if (WideAddRec == WideIncExpr && HoistStep(WideInc, DU.NarrowUse, DT)) {
1050 if (WideAddRec == WideIncExpr
1051 && SCEVExpander::hoistStep(WideInc, DU.NarrowUse, DT))
10871052 WideUse = WideInc;
1088 }
10891053 else {
10901054 WideUse = CloneIVUser(DU);
10911055 if (!WideUse)
12551219 }
12561220 }
12571221 WideIVMap.clear();
1258 }
1259 }
1260
1261 /// SimplifyCongruentIVs - Check for congruent phis in this loop header and
1262 /// replace them with their chosen representative.
1263 ///
1264 void IndVarSimplify::SimplifyCongruentIVs(Loop *L) {
1265 DenseMap ExprToIVMap;
1266 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ++I) {
1267 PHINode *Phi = cast(I);
1268 if (!SE->isSCEVable(Phi->getType()))
1269 continue;
1270
1271 const SCEV *S = SE->getSCEV(Phi);
1272 std::pair::const_iterator, bool> Tmp =
1273 ExprToIVMap.insert(std::make_pair(S, Phi));
1274 if (Tmp.second)
1275 continue;
1276 PHINode *OrigPhi = Tmp.first->second;
1277
1278 // If one phi derives from the other via GEPs, types may differ.
1279 if (OrigPhi->getType() != Phi->getType())
1280 continue;
1281
1282 // Replacing the congruent phi is sufficient because acyclic redundancy
1283 // elimination, CSE/GVN, should handle the rest. However, once SCEV proves
1284 // that a phi is congruent, it's almost certain to be the head of an IV
1285 // user cycle that is isomorphic with the original phi. So it's worth
1286 // eagerly cleaning up the common case of a single IV increment.
1287 if (BasicBlock *LatchBlock = L->getLoopLatch()) {
1288 Instruction *OrigInc =
1289 cast(OrigPhi->getIncomingValueForBlock(LatchBlock));
1290 Instruction *IsomorphicInc =
1291 cast(Phi->getIncomingValueForBlock(LatchBlock));
1292 if (OrigInc != IsomorphicInc &&
1293 OrigInc->getType() == IsomorphicInc->getType() &&
1294 SE->getSCEV(OrigInc) == SE->getSCEV(IsomorphicInc) &&
1295 HoistStep(OrigInc, IsomorphicInc, DT)) {
1296 DEBUG(dbgs() << "INDVARS: Eliminated congruent iv.inc: "
1297 << *IsomorphicInc << '\n');
1298 IsomorphicInc->replaceAllUsesWith(OrigInc);
1299 DeadInsts.push_back(IsomorphicInc);
1300 }
1301 }
1302 DEBUG(dbgs() << "INDVARS: Eliminated congruent iv: " << *Phi << '\n');
1303 ++NumElimIV;
1304 Phi->replaceAllUsesWith(OrigPhi);
1305 DeadInsts.push_back(Phi);
13061222 }
13071223 }
13081224
18471763
18481764 // Create a rewriter object which we'll use to transform the code with.
18491765 SCEVExpander Rewriter(*SE, "indvars");
1766 #ifndef NDEBUG
1767 Rewriter.setDebugType(DEBUG_TYPE);
1768 #endif
18501769
18511770 // Eliminate redundant IV users.
18521771 //
18741793
18751794 // Eliminate redundant IV cycles.
18761795 if (!EnableIVRewrite)
1877 SimplifyCongruentIVs(L);
1796 NumElimIV += Rewriter.replaceCongruentIVs(L, DT, DeadInsts);
18781797
18791798 // Compute the type of the largest recurrence expression, and decide whether
18801799 // a canonical induction variable should be inserted.
278278 ; CHECK: phi i32
279279 ; CHECK: phi i32
280280 ; CHECK-NOT: phi
281 ; CHECK: add i32
281282 ; CHECK: add i32
282283 ; CHECK: add i32
283284 ; CHECK-NOT: add