diff options
Diffstat (limited to 'lib/Transforms/Utils/LoopSimplify.cpp')
| -rw-r--r-- | lib/Transforms/Utils/LoopSimplify.cpp | 80 | 
1 files changed, 41 insertions, 39 deletions
diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp index c832a4b36f500..90dfabaeb3564 100644 --- a/lib/Transforms/Utils/LoopSimplify.cpp +++ b/lib/Transforms/Utils/LoopSimplify.cpp @@ -57,8 +57,10 @@  #include "llvm/IR/Instructions.h"  #include "llvm/IR/IntrinsicInst.h"  #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h"  #include "llvm/IR/Type.h"  #include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h"  #include "llvm/Transforms/Utils/BasicBlockUtils.h"  #include "llvm/Transforms/Utils/Local.h"  #include "llvm/Transforms/Utils/LoopUtils.h" @@ -113,6 +115,14 @@ static void placeSplitBlockCarefully(BasicBlock *NewBB,  BasicBlock *llvm::InsertPreheaderForLoop(Loop *L, Pass *PP) {    BasicBlock *Header = L->getHeader(); +  // Get analyses that we try to update. +  auto *AA = PP->getAnalysisIfAvailable<AliasAnalysis>(); +  auto *DTWP = PP->getAnalysisIfAvailable<DominatorTreeWrapperPass>(); +  auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; +  auto *LIWP = PP->getAnalysisIfAvailable<LoopInfoWrapperPass>(); +  auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr; +  bool PreserveLCSSA = PP->mustPreserveAnalysisID(LCSSAID); +    // Compute the set of predecessors of the loop that are not in the loop.    SmallVector<BasicBlock*, 8> OutsideBlocks;    for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); @@ -131,15 +141,8 @@ BasicBlock *llvm::InsertPreheaderForLoop(Loop *L, Pass *PP) {    // Split out the loop pre-header.    BasicBlock *PreheaderBB; -  if (!Header->isLandingPad()) { -    PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader", -                                         PP); -  } else { -    SmallVector<BasicBlock*, 2> NewBBs; -    SplitLandingPadPredecessors(Header, OutsideBlocks, ".preheader", -                                ".split-lp", PP, NewBBs); -    PreheaderBB = NewBBs[0]; -  } +  PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader", +                                       AA, DT, LI, PreserveLCSSA);    PreheaderBB->getTerminator()->setDebugLoc(                                        Header->getFirstNonPHI()->getDebugLoc()); @@ -157,7 +160,9 @@ BasicBlock *llvm::InsertPreheaderForLoop(Loop *L, Pass *PP) {  ///  /// This method is used to split exit blocks that have predecessors outside of  /// the loop. -static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit, Pass *PP) { +static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit, +                                        AliasAnalysis *AA, DominatorTree *DT, +                                        LoopInfo *LI, Pass *PP) {    SmallVector<BasicBlock*, 8> LoopBlocks;    for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {      BasicBlock *P = *I; @@ -172,15 +177,10 @@ static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit, Pass *PP) {    assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?");    BasicBlock *NewExitBB = nullptr; -  if (Exit->isLandingPad()) { -    SmallVector<BasicBlock*, 2> NewBBs; -    SplitLandingPadPredecessors(Exit, LoopBlocks, -                                ".loopexit", ".nonloopexit", -                                PP, NewBBs); -    NewExitBB = NewBBs[0]; -  } else { -    NewExitBB = SplitBlockPredecessors(Exit, LoopBlocks, ".loopexit", PP); -  } +  bool PreserveLCSSA = PP->mustPreserveAnalysisID(LCSSAID); + +  NewExitBB = SplitBlockPredecessors(Exit, LoopBlocks, ".loopexit", AA, DT, +                                     LI, PreserveLCSSA);    DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "                 << NewExitBB->getName() << "\n"); @@ -211,10 +211,11 @@ static void addBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock,  static PHINode *findPHIToPartitionLoops(Loop *L, AliasAnalysis *AA,                                          DominatorTree *DT,                                          AssumptionCache *AC) { +  const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();    for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) {      PHINode *PN = cast<PHINode>(I);      ++I; -    if (Value *V = SimplifyInstruction(PN, nullptr, nullptr, DT, AC)) { +    if (Value *V = SimplifyInstruction(PN, DL, nullptr, DT, AC)) {        // This is a degenerate PHI already, don't modify it!        PN->replaceAllUsesWith(V);        if (AA) AA->deleteValue(PN); @@ -287,9 +288,11 @@ static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,    if (SE)      SE->forgetLoop(L); +  bool PreserveLCSSA = PP->mustPreserveAnalysisID(LCSSAID); +    BasicBlock *Header = L->getHeader(); -  BasicBlock *NewBB = -    SplitBlockPredecessors(Header, OuterLoopPreds,  ".outer", PP); +  BasicBlock *NewBB = SplitBlockPredecessors(Header, OuterLoopPreds, ".outer", +                                             AA, DT, LI, PreserveLCSSA);    // Make sure that NewBB is put someplace intelligent, which doesn't mess up    // code layout too horribly. @@ -460,7 +463,7 @@ static BasicBlock *insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader,    // Update Loop Information - we know that this block is now in the current    // loop and all parent loops. -  L->addBasicBlockToLoop(BEBlock, LI->getBase()); +  L->addBasicBlockToLoop(BEBlock, *LI);    // Update dominator information    DT->splitBlock(BEBlock); @@ -476,7 +479,7 @@ static BasicBlock *insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader,  /// explicit if they accepted the analysis directly and then updated it.  static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist,                              AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI, -                            ScalarEvolution *SE, Pass *PP, const DataLayout *DL, +                            ScalarEvolution *SE, Pass *PP,                              AssumptionCache *AC) {    bool Changed = false;  ReprocessLoop: @@ -567,7 +570,7 @@ ReprocessLoop:        // Must be exactly this loop: no subloops, parent loops, or non-loop preds        // allowed.        if (!L->contains(*PI)) { -        if (rewriteLoopExitBlock(L, ExitBlock, PP)) { +        if (rewriteLoopExitBlock(L, ExitBlock, AA, DT, LI, PP)) {            ++NumInserted;            Changed = true;          } @@ -608,13 +611,15 @@ ReprocessLoop:      }    } +  const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); +    // Scan over the PHI nodes in the loop header.  Since they now have only two    // incoming values (the loop is canonicalized), we may have simplified the PHI    // down to 'X = phi [X, Y]', which should be replaced with 'Y'.    PHINode *PN;    for (BasicBlock::iterator I = L->getHeader()->begin();         (PN = dyn_cast<PHINode>(I++)); ) -    if (Value *V = SimplifyInstruction(PN, nullptr, nullptr, DT, AC)) { +    if (Value *V = SimplifyInstruction(PN, DL, nullptr, DT, AC)) {        if (AA) AA->deleteValue(PN);        if (SE) SE->forgetValue(PN);        PN->replaceAllUsesWith(V); @@ -676,7 +681,8 @@ ReprocessLoop:        // The block has now been cleared of all instructions except for        // a comparison and a conditional branch. SimplifyCFG may be able        // to fold it now. -      if (!FoldBranchToCommonDest(BI, DL)) continue; +      if (!FoldBranchToCommonDest(BI)) +        continue;        // Success. The block is now dead, so remove it from the loop,        // update the dominator tree and delete it. @@ -714,7 +720,7 @@ ReprocessLoop:  bool llvm::simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, Pass *PP,                          AliasAnalysis *AA, ScalarEvolution *SE, -                        const DataLayout *DL, AssumptionCache *AC) { +                        AssumptionCache *AC) {    bool Changed = false;    // Worklist maintains our depth-first queue of loops in this nest to process. @@ -726,13 +732,12 @@ bool llvm::simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, Pass *PP,    // order. We can use this simple process because loops form a tree.    for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {      Loop *L2 = Worklist[Idx]; -    for (Loop::iterator I = L2->begin(), E = L2->end(); I != E; ++I) -      Worklist.push_back(*I); +    Worklist.append(L2->begin(), L2->end());    }    while (!Worklist.empty())      Changed |= simplifyOneLoop(Worklist.pop_back_val(), Worklist, AA, DT, LI, -                               SE, PP, DL, AC); +                               SE, PP, AC);    return Changed;  } @@ -750,7 +755,6 @@ namespace {      DominatorTree *DT;      LoopInfo *LI;      ScalarEvolution *SE; -    const DataLayout *DL;      AssumptionCache *AC;      bool runOnFunction(Function &F) override; @@ -762,8 +766,8 @@ namespace {        AU.addRequired<DominatorTreeWrapperPass>();        AU.addPreserved<DominatorTreeWrapperPass>(); -      AU.addRequired<LoopInfo>(); -      AU.addPreserved<LoopInfo>(); +      AU.addRequired<LoopInfoWrapperPass>(); +      AU.addPreserved<LoopInfoWrapperPass>();        AU.addPreserved<AliasAnalysis>();        AU.addPreserved<ScalarEvolution>(); @@ -781,7 +785,7 @@ INITIALIZE_PASS_BEGIN(LoopSimplify, "loop-simplify",                  "Canonicalize natural loops", false, false)  INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)  INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) -INITIALIZE_PASS_DEPENDENCY(LoopInfo) +INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)  INITIALIZE_PASS_END(LoopSimplify, "loop-simplify",                  "Canonicalize natural loops", false, false) @@ -795,16 +799,14 @@ Pass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); }  bool LoopSimplify::runOnFunction(Function &F) {    bool Changed = false;    AA = getAnalysisIfAvailable<AliasAnalysis>(); -  LI = &getAnalysis<LoopInfo>(); +  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();    DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();    SE = getAnalysisIfAvailable<ScalarEvolution>(); -  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); -  DL = DLP ? &DLP->getDataLayout() : nullptr;    AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);    // Simplify each loop nest in the function.    for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) -    Changed |= simplifyLoop(*I, DT, LI, this, AA, SE, DL, AC); +    Changed |= simplifyLoop(*I, DT, LI, this, AA, SE, AC);    return Changed;  }  | 
