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 #define DEBUG_TYPE "lcssa"
00031 #include "llvm/Transforms/Scalar.h"
00032 #include "llvm/ADT/STLExtras.h"
00033 #include "llvm/ADT/Statistic.h"
00034 #include "llvm/Analysis/Dominators.h"
00035 #include "llvm/Analysis/LoopPass.h"
00036 #include "llvm/Analysis/ScalarEvolution.h"
00037 #include "llvm/IR/Constants.h"
00038 #include "llvm/IR/Function.h"
00039 #include "llvm/IR/Instructions.h"
00040 #include "llvm/Pass.h"
00041 #include "llvm/Support/PredIteratorCache.h"
00042 #include "llvm/Transforms/Utils/SSAUpdater.h"
00043 using namespace llvm;
00044 
00045 STATISTIC(NumLCSSA, "Number of live out of a loop variables");
00046 
00047 namespace {
00048   struct LCSSA : public LoopPass {
00049     static char ID; // Pass identification, replacement for typeid
00050     LCSSA() : LoopPass(ID) {
00051       initializeLCSSAPass(*PassRegistry::getPassRegistry());
00052     }
00053 
00054     // Cached analysis information for the current function.
00055     DominatorTree *DT;
00056     LoopInfo *LI;
00057     ScalarEvolution *SE;
00058     std::vector<BasicBlock*> LoopBlocks;
00059     PredIteratorCache PredCache;
00060     Loop *L;
00061     
00062     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
00063 
00064     /// This transformation requires natural loop information & requires that
00065     /// loop preheaders be inserted into the CFG.  It maintains both of these,
00066     /// as well as the CFG.  It also requires dominator information.
00067     ///
00068     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00069       AU.setPreservesCFG();
00070 
00071       AU.addRequired<DominatorTree>();
00072       AU.addRequired<LoopInfo>();
00073       AU.addPreservedID(LoopSimplifyID);
00074       AU.addPreserved<ScalarEvolution>();
00075     }
00076   private:
00077     bool ProcessInstruction(Instruction *Inst,
00078                             const SmallVectorImpl<BasicBlock*> &ExitBlocks);
00079     
00080     /// verifyAnalysis() - Verify loop nest.
00081     virtual void verifyAnalysis() const {
00082       // Check the special guarantees that LCSSA makes.
00083       assert(L->isLCSSAForm(*DT) && "LCSSA form not preserved!");
00084     }
00085 
00086     /// inLoop - returns true if the given block is within the current loop
00087     bool inLoop(BasicBlock *B) const {
00088       return std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), B);
00089     }
00090   };
00091 }
00092   
00093 char LCSSA::ID = 0;
00094 INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
00095 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
00096 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
00097 INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
00098 
00099 Pass *llvm::createLCSSAPass() { return new LCSSA(); }
00100 char &llvm::LCSSAID = LCSSA::ID;
00101 
00102 
00103 /// BlockDominatesAnExit - Return true if the specified block dominates at least
00104 /// one of the blocks in the specified list.
00105 static bool BlockDominatesAnExit(BasicBlock *BB,
00106                                  const SmallVectorImpl<BasicBlock*> &ExitBlocks,
00107                                  DominatorTree *DT) {
00108   DomTreeNode *DomNode = DT->getNode(BB);
00109   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00110     if (DT->dominates(DomNode, DT->getNode(ExitBlocks[i])))
00111       return true;
00112 
00113   return false;
00114 }
00115 
00116 
00117 /// runOnFunction - Process all loops in the function, inner-most out.
00118 bool LCSSA::runOnLoop(Loop *TheLoop, LPPassManager &LPM) {
00119   L = TheLoop;
00120   
00121   DT = &getAnalysis<DominatorTree>();
00122   LI = &getAnalysis<LoopInfo>();
00123   SE = getAnalysisIfAvailable<ScalarEvolution>();
00124 
00125   // Get the set of exiting blocks.
00126   SmallVector<BasicBlock*, 8> ExitBlocks;
00127   L->getExitBlocks(ExitBlocks);
00128   
00129   if (ExitBlocks.empty())
00130     return false;
00131   
00132   // Speed up queries by creating a sorted vector of blocks.
00133   LoopBlocks.clear();
00134   LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
00135   array_pod_sort(LoopBlocks.begin(), LoopBlocks.end());
00136   
00137   // Look at all the instructions in the loop, checking to see if they have uses
00138   // outside the loop.  If so, rewrite those uses.
00139   bool MadeChange = false;
00140   
00141   for (Loop::block_iterator BBI = L->block_begin(), E = L->block_end();
00142        BBI != E; ++BBI) {
00143     BasicBlock *BB = *BBI;
00144     
00145     // For large loops, avoid use-scanning by using dominance information:  In
00146     // particular, if a block does not dominate any of the loop exits, then none
00147     // of the values defined in the block could be used outside the loop.
00148     if (!BlockDominatesAnExit(BB, ExitBlocks, DT))
00149       continue;
00150     
00151     for (BasicBlock::iterator I = BB->begin(), E = BB->end();
00152          I != E; ++I) {
00153       // Reject two common cases fast: instructions with no uses (like stores)
00154       // and instructions with one use that is in the same block as this.
00155       if (I->use_empty() ||
00156           (I->hasOneUse() && I->use_back()->getParent() == BB &&
00157            !isa<PHINode>(I->use_back())))
00158         continue;
00159       
00160       MadeChange |= ProcessInstruction(I, ExitBlocks);
00161     }
00162   }
00163 
00164   // If we modified the code, remove any caches about the loop from SCEV to
00165   // avoid dangling entries.
00166   // FIXME: This is a big hammer, can we clear the cache more selectively?
00167   if (SE && MadeChange)
00168     SE->forgetLoop(L);
00169   
00170   assert(L->isLCSSAForm(*DT));
00171   PredCache.clear();
00172 
00173   return MadeChange;
00174 }
00175 
00176 /// isExitBlock - Return true if the specified block is in the list.
00177 static bool isExitBlock(BasicBlock *BB,
00178                         const SmallVectorImpl<BasicBlock*> &ExitBlocks) {
00179   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00180     if (ExitBlocks[i] == BB)
00181       return true;
00182   return false;
00183 }
00184 
00185 /// ProcessInstruction - Given an instruction in the loop, check to see if it
00186 /// has any uses that are outside the current loop.  If so, insert LCSSA PHI
00187 /// nodes and rewrite the uses.
00188 bool LCSSA::ProcessInstruction(Instruction *Inst,
00189                                const SmallVectorImpl<BasicBlock*> &ExitBlocks) {
00190   SmallVector<Use*, 16> UsesToRewrite;
00191   
00192   BasicBlock *InstBB = Inst->getParent();
00193   
00194   for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
00195        UI != E; ++UI) {
00196     User *U = *UI;
00197     BasicBlock *UserBB = cast<Instruction>(U)->getParent();
00198     if (PHINode *PN = dyn_cast<PHINode>(U))
00199       UserBB = PN->getIncomingBlock(UI);
00200     
00201     if (InstBB != UserBB && !inLoop(UserBB))
00202       UsesToRewrite.push_back(&UI.getUse());
00203   }
00204 
00205   // If there are no uses outside the loop, exit with no change.
00206   if (UsesToRewrite.empty()) return false;
00207   
00208   ++NumLCSSA; // We are applying the transformation
00209 
00210   // Invoke instructions are special in that their result value is not available
00211   // along their unwind edge. The code below tests to see whether DomBB dominates
00212   // the value, so adjust DomBB to the normal destination block, which is
00213   // effectively where the value is first usable.
00214   BasicBlock *DomBB = Inst->getParent();
00215   if (InvokeInst *Inv = dyn_cast<InvokeInst>(Inst))
00216     DomBB = Inv->getNormalDest();
00217 
00218   DomTreeNode *DomNode = DT->getNode(DomBB);
00219 
00220   SmallVector<PHINode*, 16> AddedPHIs;
00221 
00222   SSAUpdater SSAUpdate;
00223   SSAUpdate.Initialize(Inst->getType(), Inst->getName());
00224   
00225   // Insert the LCSSA phi's into all of the exit blocks dominated by the
00226   // value, and add them to the Phi's map.
00227   for (SmallVectorImpl<BasicBlock*>::const_iterator BBI = ExitBlocks.begin(),
00228       BBE = ExitBlocks.end(); BBI != BBE; ++BBI) {
00229     BasicBlock *ExitBB = *BBI;
00230     if (!DT->dominates(DomNode, DT->getNode(ExitBB))) continue;
00231     
00232     // If we already inserted something for this BB, don't reprocess it.
00233     if (SSAUpdate.HasValueForBlock(ExitBB)) continue;
00234     
00235     PHINode *PN = PHINode::Create(Inst->getType(),
00236                                   PredCache.GetNumPreds(ExitBB),
00237                                   Inst->getName()+".lcssa",
00238                                   ExitBB->begin());
00239 
00240     // Add inputs from inside the loop for this PHI.
00241     for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
00242       PN->addIncoming(Inst, *PI);
00243 
00244       // If the exit block has a predecessor not within the loop, arrange for
00245       // the incoming value use corresponding to that predecessor to be
00246       // rewritten in terms of a different LCSSA PHI.
00247       if (!inLoop(*PI))
00248         UsesToRewrite.push_back(
00249           &PN->getOperandUse(
00250             PN->getOperandNumForIncomingValue(PN->getNumIncomingValues()-1)));
00251     }
00252 
00253     AddedPHIs.push_back(PN);
00254     
00255     // Remember that this phi makes the value alive in this block.
00256     SSAUpdate.AddAvailableValue(ExitBB, PN);
00257   }
00258 
00259   // Rewrite all uses outside the loop in terms of the new PHIs we just
00260   // inserted.
00261   for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
00262     // If this use is in an exit block, rewrite to use the newly inserted PHI.
00263     // This is required for correctness because SSAUpdate doesn't handle uses in
00264     // the same block.  It assumes the PHI we inserted is at the end of the
00265     // block.
00266     Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
00267     BasicBlock *UserBB = User->getParent();
00268     if (PHINode *PN = dyn_cast<PHINode>(User))
00269       UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
00270 
00271     if (isa<PHINode>(UserBB->begin()) &&
00272         isExitBlock(UserBB, ExitBlocks)) {
00273       // Tell the VHs that the uses changed. This updates SCEV's caches.
00274       if (UsesToRewrite[i]->get()->hasValueHandle())
00275         ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
00276       UsesToRewrite[i]->set(UserBB->begin());
00277       continue;
00278     }
00279     
00280     // Otherwise, do full PHI insertion.
00281     SSAUpdate.RewriteUse(*UsesToRewrite[i]);
00282   }
00283 
00284   // Remove PHI nodes that did not have any uses rewritten.
00285   for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
00286     if (AddedPHIs[i]->use_empty())
00287       AddedPHIs[i]->eraseFromParent();
00288   }
00289   
00290   return true;
00291 }
00292