diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h --- a/llvm/include/llvm/Transforms/Utils/Local.h +++ b/llvm/include/llvm/Transforms/Utils/Local.h @@ -177,6 +177,20 @@ const SimplifyCFGOptions &Options = {}, ArrayRef LoopHeaders = {}); +/// Given a conditional branch that goes to BB1 and BB2, hoist any common code +/// in the two blocks up into the branch block. The caller of this function +/// guarantees that BI's block dominates BB1 and BB2. If EqTermsOnly is given, +/// only perform hoisting in case both blocks only contain a terminator. In that +/// case, only the original BI will be replaced and selects for PHIs are added. +bool hoistThenElseCodeToIf(BranchInst *BI, const TargetTransformInfo &TTI, + bool EqTermsOnly, DomTreeUpdater *DTU); + +/// Check whether BB's predecessors end with unconditional branches. If it is +/// true, sink any common code from the predecessors to BB. +/// We also allow one predecessor to end with conditional branch (but no more +/// than one). +bool sinkCommonCodeFromPredecessors(BasicBlock *BB, DomTreeUpdater *DTU); + /// This function is used to flatten a CFG. For example, it uses parallel-and /// and parallel-or mode to collapse if-conditions and merge if-regions with /// identical statements. diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -253,8 +253,6 @@ bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI, IRBuilder<> &Builder); - bool HoistThenElseCodeToIf(BranchInst *BI, const TargetTransformInfo &TTI, - bool EqTermsOnly); bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB, const TargetTransformInfo &TTI); bool SimplifyTerminatorOnSelect(Instruction *OldTerm, Value *Cond, @@ -1369,7 +1367,7 @@ } // If we would need to insert a select that uses the value of this invoke -// (comments in HoistThenElseCodeToIf explain why we would need to do this), we +// (comments in hoistThenElseCodeToIf explain why we would need to do this), we // can't hoist the invoke, as there is nowhere to put the select in this case. static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2, Instruction *I1, Instruction *I2) { @@ -1392,9 +1390,8 @@ /// guarantees that BI's block dominates BB1 and BB2. If EqTermsOnly is given, /// only perform hoisting in case both blocks only contain a terminator. In that /// case, only the original BI will be replaced and selects for PHIs are added. -bool SimplifyCFGOpt::HoistThenElseCodeToIf(BranchInst *BI, - const TargetTransformInfo &TTI, - bool EqTermsOnly) { +bool llvm::hoistThenElseCodeToIf(BranchInst *BI, const TargetTransformInfo &TTI, + bool EqTermsOnly, DomTreeUpdater *DTU) { // This does very trivial matching, with limited scanning, to find identical // instructions in the two blocks. In particular, we don't want to get into // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As @@ -1919,12 +1916,11 @@ } // end anonymous namespace -/// Check whether BB's predecessors end with unconditional branches. If it is -/// true, sink any common code from the predecessors to BB. -/// We also allow one predecessor to end with conditional branch (but no more -/// than one). -static bool SinkCommonCodeFromPredecessors(BasicBlock *BB, - DomTreeUpdater *DTU) { +// Check whether BB's predecessors end with unconditional branches. If it is +// true, sink any common code from the predecessors to BB. +// We also allow one predecessor to end with conditional branch (but no more +// than one). +bool llvm::sinkCommonCodeFromPredecessors(BasicBlock *BB, DomTreeUpdater *DTU) { // We support two situations: // (1) all incoming arcs are unconditional // (2) one incoming arc is conditional @@ -2171,7 +2167,7 @@ Value *OrigV = PN.getIncomingValueForBlock(BB); Value *ThenV = PN.getIncomingValueForBlock(ThenBB); - // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf. + // FIXME: Try to remove some of the duplication with hoistThenElseCodeToIf. // Skip PHIs which are trivial. if (ThenV == OrigV) continue; @@ -6464,7 +6460,7 @@ if (BI->getSuccessor(0)->getSinglePredecessor()) { if (BI->getSuccessor(1)->getSinglePredecessor()) { if (HoistCommon && - HoistThenElseCodeToIf(BI, TTI, !Options.HoistCommonInsts)) + hoistThenElseCodeToIf(BI, TTI, !Options.HoistCommonInsts, DTU)) return requestResimplify(); } else { // If Successor #1 has multiple preds, we may be able to conditionally @@ -6653,7 +6649,7 @@ return true; if (SinkCommon && Options.SinkCommonInsts) - Changed |= SinkCommonCodeFromPredecessors(BB, DTU); + Changed |= sinkCommonCodeFromPredecessors(BB, DTU); IRBuilder<> Builder(BB);