#define DEBUG_TYPE "loop-simplifycfg"
static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding",
- cl::init(false));
+ cl::init(true));
STATISTIC(NumTerminatorsFolded,
"Number of terminators folded to unconditional branches");
Loop &L;
LoopInfo &LI;
DominatorTree &DT;
+ MemorySSAUpdater *MSSAU;
// Whether or not the current loop will still exist after terminator constant
// folding will be done. In theory, there are two ways how it can happen:
// the one-input Phi because it is a LCSSA Phi.
bool PreserveLCSSAPhi = !L.contains(Succ);
Succ->removePredecessor(BB, PreserveLCSSAPhi);
+ if (MSSAU)
+ MSSAU->removeEdge(BB, Succ);
} else
++TheOnlySuccDuplicates;
bool PreserveLCSSAPhi = !L.contains(TheOnlySucc);
for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup)
TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi);
+ if (MSSAU && TheOnlySuccDuplicates > 1)
+ MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc);
IRBuilder<> Builder(BB->getContext());
Instruction *Term = BB->getTerminator();
}
public:
- ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT)
- : L(L), LI(LI), DT(DT) {}
+ ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT,
+ MemorySSAUpdater *MSSAU)
+ : L(L), LI(LI), DT(DT), MSSAU(MSSAU) {}
bool run() {
assert(L.getLoopLatch() && "Should be single latch!");
/// Turn branches and switches with known constant conditions into unconditional
/// branches.
-static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI) {
+static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI,
+ MemorySSAUpdater *MSSAU) {
if (!EnableTermFolding)
return false;
if (!L.getLoopLatch())
return false;
- ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT);
+ ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, MSSAU);
return BranchFolder.run();
}
bool Changed = false;
// Constant-fold terminators with known constant conditions.
- Changed |= constantFoldTerminators(L, DT, LI);
+ Changed |= constantFoldTerminators(L, DT, LI, MSSAU);
// Eliminate unconditional branches by merging blocks into their predecessors.
Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU);