LLVM API Documentation

LCSSA.cpp
Go to the documentation of this file.
00001 //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass transforms loops by placing phi nodes at the end of the loops for
00011 // all values that are live across the loop boundary.  For example, it turns
00012 // the left into the right code:
00013 // 
00014 // for (...)                for (...)
00015 //   if (c)                   if (c)
00016 //     X1 = ...                 X1 = ...
00017 //   else                     else
00018 //     X2 = ...                 X2 = ...
00019 //   X3 = phi(X1, X2)         X3 = phi(X1, X2)
00020 // ... = X3 + 4             X4 = phi(X3)
00021 //                          ... = X4 + 4
00022 //
00023 // This is still valid LLVM; the extra phi nodes are purely redundant, and will
00024 // be trivially eliminated by InstCombine.  The major benefit of this 
00025 // transformation is that it makes many other loop optimizations, such as 
00026 // LoopUnswitching, simpler.
00027 //
00028 //===----------------------------------------------------------------------===//
00029 
00030 #include "llvm/Transforms/Scalar.h"
00031 #include "llvm/ADT/STLExtras.h"
00032 #include "llvm/ADT/Statistic.h"
00033 #include "llvm/Analysis/AliasAnalysis.h"
00034 #include "llvm/Analysis/LoopPass.h"
00035 #include "llvm/Analysis/ScalarEvolution.h"
00036 #include "llvm/IR/Constants.h"
00037 #include "llvm/IR/Dominators.h"
00038 #include "llvm/IR/Function.h"
00039 #include "llvm/IR/Instructions.h"
00040 #include "llvm/IR/PredIteratorCache.h"
00041 #include "llvm/Pass.h"
00042 #include "llvm/Transforms/Utils/LoopUtils.h"
00043 #include "llvm/Transforms/Utils/SSAUpdater.h"
00044 using namespace llvm;
00045 
00046 #define DEBUG_TYPE "lcssa"
00047 
00048 STATISTIC(NumLCSSA, "Number of live out of a loop variables");
00049 
00050 /// Return true if the specified block is in the list.
00051 static bool isExitBlock(BasicBlock *BB,
00052                         const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
00053   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00054     if (ExitBlocks[i] == BB)
00055       return true;
00056   return false;
00057 }
00058 
00059 /// Given an instruction in the loop, check to see if it has any uses that are
00060 /// outside the current loop.  If so, insert LCSSA PHI nodes and rewrite the
00061 /// uses.
00062 static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT,
00063                                const SmallVectorImpl<BasicBlock *> &ExitBlocks,
00064                                PredIteratorCache &PredCache) {
00065   SmallVector<Use *, 16> UsesToRewrite;
00066 
00067   BasicBlock *InstBB = Inst.getParent();
00068 
00069   for (Use &U : Inst.uses()) {
00070     Instruction *User = cast<Instruction>(U.getUser());
00071     BasicBlock *UserBB = User->getParent();
00072     if (PHINode *PN = dyn_cast<PHINode>(User))
00073       UserBB = PN->getIncomingBlock(U);
00074 
00075     if (InstBB != UserBB && !L.contains(UserBB))
00076       UsesToRewrite.push_back(&U);
00077   }
00078 
00079   // If there are no uses outside the loop, exit with no change.
00080   if (UsesToRewrite.empty())
00081     return false;
00082 
00083   ++NumLCSSA; // We are applying the transformation
00084 
00085   // Invoke instructions are special in that their result value is not available
00086   // along their unwind edge. The code below tests to see whether DomBB
00087   // dominates
00088   // the value, so adjust DomBB to the normal destination block, which is
00089   // effectively where the value is first usable.
00090   BasicBlock *DomBB = Inst.getParent();
00091   if (InvokeInst *Inv = dyn_cast<InvokeInst>(&Inst))
00092     DomBB = Inv->getNormalDest();
00093 
00094   DomTreeNode *DomNode = DT.getNode(DomBB);
00095 
00096   SmallVector<PHINode *, 16> AddedPHIs;
00097 
00098   SSAUpdater SSAUpdate;
00099   SSAUpdate.Initialize(Inst.getType(), Inst.getName());
00100 
00101   // Insert the LCSSA phi's into all of the exit blocks dominated by the
00102   // value, and add them to the Phi's map.
00103   for (SmallVectorImpl<BasicBlock *>::const_iterator BBI = ExitBlocks.begin(),
00104                                                      BBE = ExitBlocks.end();
00105        BBI != BBE; ++BBI) {
00106     BasicBlock *ExitBB = *BBI;
00107     if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
00108       continue;
00109 
00110     // If we already inserted something for this BB, don't reprocess it.
00111     if (SSAUpdate.HasValueForBlock(ExitBB))
00112       continue;
00113 
00114     PHINode *PN = PHINode::Create(Inst.getType(), PredCache.GetNumPreds(ExitBB),
00115                                   Inst.getName() + ".lcssa", ExitBB->begin());
00116 
00117     // Add inputs from inside the loop for this PHI.
00118     for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
00119       PN->addIncoming(&Inst, *PI);
00120 
00121       // If the exit block has a predecessor not within the loop, arrange for
00122       // the incoming value use corresponding to that predecessor to be
00123       // rewritten in terms of a different LCSSA PHI.
00124       if (!L.contains(*PI))
00125         UsesToRewrite.push_back(
00126             &PN->getOperandUse(PN->getOperandNumForIncomingValue(
00127                  PN->getNumIncomingValues() - 1)));
00128     }
00129 
00130     AddedPHIs.push_back(PN);
00131 
00132     // Remember that this phi makes the value alive in this block.
00133     SSAUpdate.AddAvailableValue(ExitBB, PN);
00134   }
00135 
00136   // Rewrite all uses outside the loop in terms of the new PHIs we just
00137   // inserted.
00138   for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
00139     // If this use is in an exit block, rewrite to use the newly inserted PHI.
00140     // This is required for correctness because SSAUpdate doesn't handle uses in
00141     // the same block.  It assumes the PHI we inserted is at the end of the
00142     // block.
00143     Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
00144     BasicBlock *UserBB = User->getParent();
00145     if (PHINode *PN = dyn_cast<PHINode>(User))
00146       UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
00147 
00148     if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
00149       // Tell the VHs that the uses changed. This updates SCEV's caches.
00150       if (UsesToRewrite[i]->get()->hasValueHandle())
00151         ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
00152       UsesToRewrite[i]->set(UserBB->begin());
00153       continue;
00154     }
00155 
00156     // Otherwise, do full PHI insertion.
00157     SSAUpdate.RewriteUse(*UsesToRewrite[i]);
00158   }
00159 
00160   // Remove PHI nodes that did not have any uses rewritten.
00161   for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
00162     if (AddedPHIs[i]->use_empty())
00163       AddedPHIs[i]->eraseFromParent();
00164   }
00165 
00166   return true;
00167 }
00168 
00169 /// Return true if the specified block dominates at least
00170 /// one of the blocks in the specified list.
00171 static bool
00172 blockDominatesAnExit(BasicBlock *BB,
00173                      DominatorTree &DT,
00174                      const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
00175   DomTreeNode *DomNode = DT.getNode(BB);
00176   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00177     if (DT.dominates(DomNode, DT.getNode(ExitBlocks[i])))
00178       return true;
00179 
00180   return false;
00181 }
00182 
00183 bool llvm::formLCSSA(Loop &L, DominatorTree &DT, ScalarEvolution *SE) {
00184   bool Changed = false;
00185 
00186   // Get the set of exiting blocks.
00187   SmallVector<BasicBlock *, 8> ExitBlocks;
00188   L.getExitBlocks(ExitBlocks);
00189 
00190   if (ExitBlocks.empty())
00191     return false;
00192 
00193   PredIteratorCache PredCache;
00194 
00195   // Look at all the instructions in the loop, checking to see if they have uses
00196   // outside the loop.  If so, rewrite those uses.
00197   for (Loop::block_iterator BBI = L.block_begin(), BBE = L.block_end();
00198        BBI != BBE; ++BBI) {
00199     BasicBlock *BB = *BBI;
00200 
00201     // For large loops, avoid use-scanning by using dominance information:  In
00202     // particular, if a block does not dominate any of the loop exits, then none
00203     // of the values defined in the block could be used outside the loop.
00204     if (!blockDominatesAnExit(BB, DT, ExitBlocks))
00205       continue;
00206 
00207     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
00208       // Reject two common cases fast: instructions with no uses (like stores)
00209       // and instructions with one use that is in the same block as this.
00210       if (I->use_empty() ||
00211           (I->hasOneUse() && I->user_back()->getParent() == BB &&
00212            !isa<PHINode>(I->user_back())))
00213         continue;
00214 
00215       Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache);
00216     }
00217   }
00218 
00219   // If we modified the code, remove any caches about the loop from SCEV to
00220   // avoid dangling entries.
00221   // FIXME: This is a big hammer, can we clear the cache more selectively?
00222   if (SE && Changed)
00223     SE->forgetLoop(&L);
00224 
00225   assert(L.isLCSSAForm(DT));
00226 
00227   return Changed;
00228 }
00229 
00230 /// Process a loop nest depth first.
00231 bool llvm::formLCSSARecursively(Loop &L, DominatorTree &DT,
00232                                 ScalarEvolution *SE) {
00233   bool Changed = false;
00234 
00235   // Recurse depth-first through inner loops.
00236   for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
00237     Changed |= formLCSSARecursively(**LI, DT, SE);
00238 
00239   Changed |= formLCSSA(L, DT, SE);
00240   return Changed;
00241 }
00242 
00243 namespace {
00244 struct LCSSA : public FunctionPass {
00245   static char ID; // Pass identification, replacement for typeid
00246   LCSSA() : FunctionPass(ID) {
00247     initializeLCSSAPass(*PassRegistry::getPassRegistry());
00248   }
00249 
00250   // Cached analysis information for the current function.
00251   DominatorTree *DT;
00252   LoopInfo *LI;
00253   ScalarEvolution *SE;
00254 
00255   bool runOnFunction(Function &F) override;
00256 
00257   /// This transformation requires natural loop information & requires that
00258   /// loop preheaders be inserted into the CFG.  It maintains both of these,
00259   /// as well as the CFG.  It also requires dominator information.
00260   void getAnalysisUsage(AnalysisUsage &AU) const override {
00261     AU.setPreservesCFG();
00262 
00263     AU.addRequired<DominatorTreeWrapperPass>();
00264     AU.addRequired<LoopInfo>();
00265     AU.addPreservedID(LoopSimplifyID);
00266     AU.addPreserved<AliasAnalysis>();
00267     AU.addPreserved<ScalarEvolution>();
00268   }
00269 
00270 private:
00271   void verifyAnalysis() const override;
00272 };
00273 }
00274 
00275 char LCSSA::ID = 0;
00276 INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
00277 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00278 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
00279 INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
00280 
00281 Pass *llvm::createLCSSAPass() { return new LCSSA(); }
00282 char &llvm::LCSSAID = LCSSA::ID;
00283 
00284 
00285 /// Process all loops in the function, inner-most out.
00286 bool LCSSA::runOnFunction(Function &F) {
00287   bool Changed = false;
00288   LI = &getAnalysis<LoopInfo>();
00289   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
00290   SE = getAnalysisIfAvailable<ScalarEvolution>();
00291 
00292   // Simplify each loop nest in the function.
00293   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
00294     Changed |= formLCSSARecursively(**I, *DT, SE);
00295 
00296   return Changed;
00297 }
00298 
00299 static void verifyLoop(Loop &L, DominatorTree &DT) {
00300   // Recurse depth-first through inner loops.
00301   for (Loop::iterator LI = L.begin(), LE = L.end(); LI != LE; ++LI)
00302     verifyLoop(**LI, DT);
00303 
00304   // Check the special guarantees that LCSSA makes.
00305   //assert(L.isLCSSAForm(DT) && "LCSSA form not preserved!");
00306 }
00307 
00308 void LCSSA::verifyAnalysis() const {
00309   // Verify each loop nest in the function, assuming LI still points at that
00310   // function's loop info.
00311   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
00312     verifyLoop(**I, *DT);
00313 }