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/Constants.h"
00033 #include "llvm/Pass.h"
00034 #include "llvm/Function.h"
00035 #include "llvm/Instructions.h"
00036 #include "llvm/ADT/SetVector.h"
00037 #include "llvm/ADT/Statistic.h"
00038 #include "llvm/Analysis/Dominators.h"
00039 #include "llvm/Analysis/LoopPass.h"
00040 #include "llvm/Analysis/ScalarEvolution.h"
00041 #include "llvm/Support/CFG.h"
00042 #include "llvm/Support/Compiler.h"
00043 #include <algorithm>
00044 #include <map>
00045 using namespace llvm;
00046 
00047 STATISTIC(NumLCSSA, "Number of live out of a loop variables");
00048 
00049 namespace {
00050   struct VISIBILITY_HIDDEN LCSSA : public LoopPass {
00051     static char ID; // Pass identification, replacement for typeid
00052     LCSSA() : LoopPass(&ID) {}
00053 
00054     // Cached analysis information for the current function.
00055     LoopInfo *LI;
00056     DominatorTree *DT;
00057     std::vector<BasicBlock*> LoopBlocks;
00058     
00059     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
00060 
00061     void ProcessInstruction(Instruction* Instr,
00062                             const SmallVector<BasicBlock*, 8>& exitBlocks);
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       AU.addRequiredID(LoopSimplifyID);
00071       AU.addPreservedID(LoopSimplifyID);
00072       AU.addRequired<LoopInfo>();
00073       AU.addPreserved<LoopInfo>();
00074       AU.addRequired<DominatorTree>();
00075       AU.addPreserved<ScalarEvolution>();
00076       AU.addPreserved<DominatorTree>();
00077 
00078       // Request DominanceFrontier now, even though LCSSA does
00079       // not use it. This allows Pass Manager to schedule Dominance
00080       // Frontier early enough such that one LPPassManager can handle
00081       // multiple loop transformation passes.
00082       AU.addRequired<DominanceFrontier>(); 
00083       AU.addPreserved<DominanceFrontier>();
00084     }
00085   private:
00086     void getLoopValuesUsedOutsideLoop(Loop *L,
00087                                       SetVector<Instruction*> &AffectedValues);
00088 
00089     Value *GetValueForBlock(DomTreeNode *BB, Instruction *OrigInst,
00090                             DenseMap<DomTreeNode*, Value*> &Phis);
00091 
00092     /// inLoop - returns true if the given block is within the current loop
00093     bool inLoop(BasicBlock* B) {
00094       return std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), B);
00095     }
00096   };
00097 }
00098   
00099 char LCSSA::ID = 0;
00100 static RegisterPass<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass");
00101 
00102 Pass *llvm::createLCSSAPass() { return new LCSSA(); }
00103 const PassInfo *const llvm::LCSSAID = &X;
00104 
00105 /// runOnFunction - Process all loops in the function, inner-most out.
00106 bool LCSSA::runOnLoop(Loop *L, LPPassManager &LPM) {
00107   
00108   LI = &LPM.getAnalysis<LoopInfo>();
00109   DT = &getAnalysis<DominatorTree>();
00110 
00111   // Speed up queries by creating a sorted list of blocks
00112   LoopBlocks.clear();
00113   LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
00114   std::sort(LoopBlocks.begin(), LoopBlocks.end());
00115   
00116   SetVector<Instruction*> AffectedValues;
00117   getLoopValuesUsedOutsideLoop(L, AffectedValues);
00118   
00119   // If no values are affected, we can save a lot of work, since we know that
00120   // nothing will be changed.
00121   if (AffectedValues.empty())
00122     return false;
00123   
00124   SmallVector<BasicBlock*, 8> exitBlocks;
00125   L->getExitBlocks(exitBlocks);  
00126   
00127   // Iterate over all affected values for this loop and insert Phi nodes
00128   // for them in the appropriate exit blocks
00129   
00130   for (SetVector<Instruction*>::iterator I = AffectedValues.begin(),
00131        E = AffectedValues.end(); I != E; ++I)
00132     ProcessInstruction(*I, exitBlocks);
00133   
00134   assert(L->isLCSSAForm());
00135   
00136   return true;
00137 }
00138 
00139 /// processInstruction - Given a live-out instruction, insert LCSSA Phi nodes,
00140 /// eliminate all out-of-loop uses.
00141 void LCSSA::ProcessInstruction(Instruction *Instr,
00142                                const SmallVector<BasicBlock*, 8>& exitBlocks) {
00143   ++NumLCSSA; // We are applying the transformation
00144 
00145   // Keep track of the blocks that have the value available already.
00146   DenseMap<DomTreeNode*, Value*> Phis;
00147 
00148   DomTreeNode *InstrNode = DT->getNode(Instr->getParent());
00149 
00150   // Insert the LCSSA phi's into the exit blocks (dominated by the value), and
00151   // add them to the Phi's map.
00152   for (SmallVector<BasicBlock*, 8>::const_iterator BBI = exitBlocks.begin(),
00153       BBE = exitBlocks.end(); BBI != BBE; ++BBI) {
00154     BasicBlock *BB = *BBI;
00155     DomTreeNode *ExitBBNode = DT->getNode(BB);
00156     Value *&Phi = Phis[ExitBBNode];
00157     if (!Phi && DT->dominates(InstrNode, ExitBBNode)) {
00158       PHINode *PN = PHINode::Create(Instr->getType(), Instr->getName()+".lcssa",
00159                                     BB->begin());
00160       PN->reserveOperandSpace(std::distance(pred_begin(BB), pred_end(BB)));
00161 
00162       // Remember that this phi makes the value alive in this block.
00163       Phi = PN;
00164 
00165       // Add inputs from inside the loop for this PHI.
00166       for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
00167         PN->addIncoming(Instr, *PI);
00168     }
00169   }
00170   
00171   
00172   // Record all uses of Instr outside the loop.  We need to rewrite these.  The
00173   // LCSSA phis won't be included because they use the value in the loop.
00174   for (Value::use_iterator UI = Instr->use_begin(), E = Instr->use_end();
00175        UI != E;) {
00176     BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
00177     if (PHINode *P = dyn_cast<PHINode>(*UI)) {
00178       UserBB = P->getIncomingBlock(UI);
00179     }
00180     
00181     // If the user is in the loop, don't rewrite it!
00182     if (UserBB == Instr->getParent() || inLoop(UserBB)) {
00183       ++UI;
00184       continue;
00185     }
00186     
00187     // Otherwise, patch up uses of the value with the appropriate LCSSA Phi,
00188     // inserting PHI nodes into join points where needed.
00189     Value *Val = GetValueForBlock(DT->getNode(UserBB), Instr, Phis);
00190     
00191     // Preincrement the iterator to avoid invalidating it when we change the
00192     // value.
00193     Use &U = UI.getUse();
00194     ++UI;
00195     U.set(Val);
00196   }
00197 }
00198 
00199 /// getLoopValuesUsedOutsideLoop - Return any values defined in the loop that
00200 /// are used by instructions outside of it.
00201 void LCSSA::getLoopValuesUsedOutsideLoop(Loop *L,
00202                                       SetVector<Instruction*> &AffectedValues) {
00203   // FIXME: For large loops, we may be able to avoid a lot of use-scanning
00204   // by using dominance information.  In particular, if a block does not
00205   // dominate any of the loop exits, then none of the values defined in the
00206   // block could be used outside the loop.
00207   for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
00208        BB != E; ++BB) {
00209     for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I)
00210       for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
00211            ++UI) {
00212         BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
00213         if (PHINode* p = dyn_cast<PHINode>(*UI)) {
00214           UserBB = p->getIncomingBlock(UI);
00215         }
00216         
00217         if (*BB != UserBB && !inLoop(UserBB)) {
00218           AffectedValues.insert(I);
00219           break;
00220         }
00221       }
00222   }
00223 }
00224 
00225 /// GetValueForBlock - Get the value to use within the specified basic block.
00226 /// available values are in Phis.
00227 Value *LCSSA::GetValueForBlock(DomTreeNode *BB, Instruction *OrigInst,
00228                                DenseMap<DomTreeNode*, Value*> &Phis) {
00229   // If there is no dominator info for this BB, it is unreachable.
00230   if (BB == 0)
00231     return UndefValue::get(OrigInst->getType());
00232                                  
00233   // If we have already computed this value, return the previously computed val.
00234   if (Phis.count(BB)) return Phis[BB];
00235 
00236   DomTreeNode *IDom = BB->getIDom();
00237 
00238   // Otherwise, there are two cases: we either have to insert a PHI node or we
00239   // don't.  We need to insert a PHI node if this block is not dominated by one
00240   // of the exit nodes from the loop (the loop could have multiple exits, and
00241   // though the value defined *inside* the loop dominated all its uses, each
00242   // exit by itself may not dominate all the uses).
00243   //
00244   // The simplest way to check for this condition is by checking to see if the
00245   // idom is in the loop.  If so, we *know* that none of the exit blocks
00246   // dominate this block.  Note that we *know* that the block defining the
00247   // original instruction is in the idom chain, because if it weren't, then the
00248   // original value didn't dominate this use.
00249   if (!inLoop(IDom->getBlock())) {
00250     // Idom is not in the loop, we must still be "below" the exit block and must
00251     // be fully dominated by the value live in the idom.
00252     Value* val = GetValueForBlock(IDom, OrigInst, Phis);
00253     Phis.insert(std::make_pair(BB, val));
00254     return val;
00255   }
00256   
00257   BasicBlock *BBN = BB->getBlock();
00258   
00259   // Otherwise, the idom is the loop, so we need to insert a PHI node.  Do so
00260   // now, then get values to fill in the incoming values for the PHI.
00261   PHINode *PN = PHINode::Create(OrigInst->getType(),
00262                                 OrigInst->getName() + ".lcssa", BBN->begin());
00263   PN->reserveOperandSpace(std::distance(pred_begin(BBN), pred_end(BBN)));
00264   Phis.insert(std::make_pair(BB, PN));
00265                                  
00266   // Fill in the incoming values for the block.
00267   for (pred_iterator PI = pred_begin(BBN), E = pred_end(BBN); PI != E; ++PI)
00268     PN->addIncoming(GetValueForBlock(DT->getNode(*PI), OrigInst, Phis), *PI);
00269   return PN;
00270 }
00271