LLVM API Documentation

LICM.cpp

Go to the documentation of this file.
00001 //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
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 performs loop invariant code motion, attempting to remove as much
00011 // code from the body of a loop as possible.  It does this by either hoisting
00012 // code into the preheader block, or by sinking code to the exit blocks if it is
00013 // safe.  This pass also promotes must-aliased memory locations in the loop to
00014 // live in registers, thus hoisting and sinking "invariant" loads and stores.
00015 //
00016 // This pass uses alias analysis for two purposes:
00017 //
00018 //  1. Moving loop invariant loads and calls out of loops.  If we can determine
00019 //     that a load or call inside of a loop never aliases anything stored to,
00020 //     we can hoist it or sink it like any other instruction.
00021 //  2. Scalar Promotion of Memory - If there is a store instruction inside of
00022 //     the loop, we try to move the store to happen AFTER the loop instead of
00023 //     inside of the loop.  This can only happen if a few conditions are true:
00024 //       A. The pointer stored through is loop invariant
00025 //       B. There are no stores or loads in the loop which _may_ alias the
00026 //          pointer.  There are no calls in the loop which mod/ref the pointer.
00027 //     If these conditions are true, we can promote the loads and stores in the
00028 //     loop of the pointer to use a temporary alloca'd variable.  We then use
00029 //     the mem2reg functionality to construct the appropriate SSA form for the
00030 //     variable.
00031 //
00032 //===----------------------------------------------------------------------===//
00033 
00034 #define DEBUG_TYPE "licm"
00035 #include "llvm/Transforms/Scalar.h"
00036 #include "llvm/Constants.h"
00037 #include "llvm/DerivedTypes.h"
00038 #include "llvm/Instructions.h"
00039 #include "llvm/Target/TargetData.h"
00040 #include "llvm/Analysis/LoopInfo.h"
00041 #include "llvm/Analysis/LoopPass.h"
00042 #include "llvm/Analysis/AliasAnalysis.h"
00043 #include "llvm/Analysis/AliasSetTracker.h"
00044 #include "llvm/Analysis/Dominators.h"
00045 #include "llvm/Analysis/ScalarEvolution.h"
00046 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
00047 #include "llvm/Support/CFG.h"
00048 #include "llvm/Support/Compiler.h"
00049 #include "llvm/Support/CommandLine.h"
00050 #include "llvm/Support/Debug.h"
00051 #include "llvm/ADT/Statistic.h"
00052 #include <algorithm>
00053 using namespace llvm;
00054 
00055 STATISTIC(NumSunk      , "Number of instructions sunk out of loop");
00056 STATISTIC(NumHoisted   , "Number of instructions hoisted out of loop");
00057 STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
00058 STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk");
00059 STATISTIC(NumPromoted  , "Number of memory locations promoted to registers");
00060 
00061 static cl::opt<bool>
00062 DisablePromotion("disable-licm-promotion", cl::Hidden,
00063                  cl::desc("Disable memory promotion in LICM pass"));
00064 
00065 // This feature is currently disabled by default because CodeGen is not yet capable
00066 // of rematerializing these constants in PIC mode, so it can lead to degraded
00067 // performance. Compile test/CodeGen/X86/remat-constant.ll with
00068 // -relocation-model=pic to see an example of this.
00069 static cl::opt<bool>
00070 EnableLICMConstantMotion("enable-licm-constant-variables", cl::Hidden,
00071                          cl::desc("Enable hoisting/sinking of constant "
00072                                   "global variables"));
00073 
00074 namespace {
00075   struct VISIBILITY_HIDDEN LICM : public LoopPass {
00076     static char ID; // Pass identification, replacement for typeid
00077     LICM() : LoopPass(&ID) {}
00078 
00079     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
00080 
00081     /// This transformation requires natural loop information & requires that
00082     /// loop preheaders be inserted into the CFG...
00083     ///
00084     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00085       AU.setPreservesCFG();
00086       AU.addRequiredID(LoopSimplifyID);
00087       AU.addRequired<LoopInfo>();
00088       AU.addRequired<DominatorTree>();
00089       AU.addRequired<DominanceFrontier>();  // For scalar promotion (mem2reg)
00090       AU.addRequired<AliasAnalysis>();
00091       AU.addPreserved<ScalarEvolution>();
00092       AU.addPreserved<DominanceFrontier>();
00093     }
00094 
00095     bool doFinalization() {
00096       // Free the values stored in the map
00097       for (std::map<Loop *, AliasSetTracker *>::iterator
00098              I = LoopToAliasMap.begin(), E = LoopToAliasMap.end(); I != E; ++I)
00099         delete I->second;
00100 
00101       LoopToAliasMap.clear();
00102       return false;
00103     }
00104 
00105   private:
00106     // Various analyses that we use...
00107     AliasAnalysis *AA;       // Current AliasAnalysis information
00108     LoopInfo      *LI;       // Current LoopInfo
00109     DominatorTree *DT;       // Dominator Tree for the current Loop...
00110     DominanceFrontier *DF;   // Current Dominance Frontier
00111 
00112     // State that is updated as we process loops
00113     bool Changed;            // Set to true when we change anything.
00114     BasicBlock *Preheader;   // The preheader block of the current loop...
00115     Loop *CurLoop;           // The current loop we are working on...
00116     AliasSetTracker *CurAST; // AliasSet information for the current loop...
00117     std::map<Loop *, AliasSetTracker *> LoopToAliasMap;
00118 
00119     /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
00120     void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L);
00121 
00122     /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
00123     /// set.
00124     void deleteAnalysisValue(Value *V, Loop *L);
00125 
00126     /// SinkRegion - Walk the specified region of the CFG (defined by all blocks
00127     /// dominated by the specified block, and that are in the current loop) in
00128     /// reverse depth first order w.r.t the DominatorTree.  This allows us to
00129     /// visit uses before definitions, allowing us to sink a loop body in one
00130     /// pass without iteration.
00131     ///
00132     void SinkRegion(DomTreeNode *N);
00133 
00134     /// HoistRegion - Walk the specified region of the CFG (defined by all
00135     /// blocks dominated by the specified block, and that are in the current
00136     /// loop) in depth first order w.r.t the DominatorTree.  This allows us to
00137     /// visit definitions before uses, allowing us to hoist a loop body in one
00138     /// pass without iteration.
00139     ///
00140     void HoistRegion(DomTreeNode *N);
00141 
00142     /// inSubLoop - Little predicate that returns true if the specified basic
00143     /// block is in a subloop of the current one, not the current one itself.
00144     ///
00145     bool inSubLoop(BasicBlock *BB) {
00146       assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
00147       for (Loop::iterator I = CurLoop->begin(), E = CurLoop->end(); I != E; ++I)
00148         if ((*I)->contains(BB))
00149           return true;  // A subloop actually contains this block!
00150       return false;
00151     }
00152 
00153     /// isExitBlockDominatedByBlockInLoop - This method checks to see if the
00154     /// specified exit block of the loop is dominated by the specified block
00155     /// that is in the body of the loop.  We use these constraints to
00156     /// dramatically limit the amount of the dominator tree that needs to be
00157     /// searched.
00158     bool isExitBlockDominatedByBlockInLoop(BasicBlock *ExitBlock,
00159                                            BasicBlock *BlockInLoop) const {
00160       // If the block in the loop is the loop header, it must be dominated!
00161       BasicBlock *LoopHeader = CurLoop->getHeader();
00162       if (BlockInLoop == LoopHeader)
00163         return true;
00164 
00165       DomTreeNode *BlockInLoopNode = DT->getNode(BlockInLoop);
00166       DomTreeNode *IDom            = DT->getNode(ExitBlock);
00167 
00168       // Because the exit block is not in the loop, we know we have to get _at
00169       // least_ its immediate dominator.
00170       do {
00171         // Get next Immediate Dominator.
00172         IDom = IDom->getIDom();
00173 
00174         // If we have got to the header of the loop, then the instructions block
00175         // did not dominate the exit node, so we can't hoist it.
00176         if (IDom->getBlock() == LoopHeader)
00177           return false;
00178 
00179       } while (IDom != BlockInLoopNode);
00180 
00181       return true;
00182     }
00183 
00184     /// sink - When an instruction is found to only be used outside of the loop,
00185     /// this function moves it to the exit blocks and patches up SSA form as
00186     /// needed.
00187     ///
00188     void sink(Instruction &I);
00189 
00190     /// hoist - When an instruction is found to only use loop invariant operands
00191     /// that is safe to hoist, this instruction is called to do the dirty work.
00192     ///
00193     void hoist(Instruction &I);
00194 
00195     /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it
00196     /// is not a trapping instruction or if it is a trapping instruction and is
00197     /// guaranteed to execute.
00198     ///
00199     bool isSafeToExecuteUnconditionally(Instruction &I);
00200 
00201     /// pointerInvalidatedByLoop - Return true if the body of this loop may
00202     /// store into the memory location pointed to by V.
00203     ///
00204     bool pointerInvalidatedByLoop(Value *V, unsigned Size) {
00205       // Check to see if any of the basic blocks in CurLoop invalidate *V.
00206       return CurAST->getAliasSetForPointer(V, Size).isMod();
00207     }
00208 
00209     bool canSinkOrHoistInst(Instruction &I);
00210     bool isLoopInvariantInst(Instruction &I);
00211     bool isNotUsedInLoop(Instruction &I);
00212 
00213     /// PromoteValuesInLoop - Look at the stores in the loop and promote as many
00214     /// to scalars as we can.
00215     ///
00216     void PromoteValuesInLoop();
00217 
00218     /// FindPromotableValuesInLoop - Check the current loop for stores to
00219     /// definite pointers, which are not loaded and stored through may aliases.
00220     /// If these are found, create an alloca for the value, add it to the
00221     /// PromotedValues list, and keep track of the mapping from value to
00222     /// alloca...
00223     ///
00224     void FindPromotableValuesInLoop(
00225                    std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues,
00226                                     std::map<Value*, AllocaInst*> &Val2AlMap);
00227   };
00228 }
00229 
00230 char LICM::ID = 0;
00231 static RegisterPass<LICM> X("licm", "Loop Invariant Code Motion");
00232 
00233 LoopPass *llvm::createLICMPass() { return new LICM(); }
00234 
00235 /// Hoist expressions out of the specified loop. Note, alias info for inner
00236 /// loop is not preserved so it is not a good idea to run LICM multiple 
00237 /// times on one loop.
00238 ///
00239 bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
00240   Changed = false;
00241 
00242   // Get our Loop and Alias Analysis information...
00243   LI = &getAnalysis<LoopInfo>();
00244   AA = &getAnalysis<AliasAnalysis>();
00245   DF = &getAnalysis<DominanceFrontier>();
00246   DT = &getAnalysis<DominatorTree>();
00247 
00248   CurAST = new AliasSetTracker(*AA);
00249   // Collect Alias info from subloops
00250   for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end();
00251        LoopItr != LoopItrE; ++LoopItr) {
00252     Loop *InnerL = *LoopItr;
00253     AliasSetTracker *InnerAST = LoopToAliasMap[InnerL];
00254     assert (InnerAST && "Where is my AST?");
00255 
00256     // What if InnerLoop was modified by other passes ?
00257     CurAST->add(*InnerAST);
00258   }
00259   
00260   CurLoop = L;
00261 
00262   // Get the preheader block to move instructions into...
00263   Preheader = L->getLoopPreheader();
00264   assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
00265 
00266   // Loop over the body of this loop, looking for calls, invokes, and stores.
00267   // Because subloops have already been incorporated into AST, we skip blocks in
00268   // subloops.
00269   //
00270   for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
00271        I != E; ++I) {
00272     BasicBlock *BB = *I;
00273     if (LI->getLoopFor(BB) == L)        // Ignore blocks in subloops...
00274       CurAST->add(*BB);                 // Incorporate the specified basic block
00275   }
00276 
00277   // We want to visit all of the instructions in this loop... that are not parts
00278   // of our subloops (they have already had their invariants hoisted out of
00279   // their loop, into this loop, so there is no need to process the BODIES of
00280   // the subloops).
00281   //
00282   // Traverse the body of the loop in depth first order on the dominator tree so
00283   // that we are guaranteed to see definitions before we see uses.  This allows
00284   // us to sink instructions in one pass, without iteration.  After sinking
00285   // instructions, we perform another pass to hoist them out of the loop.
00286   //
00287   SinkRegion(DT->getNode(L->getHeader()));
00288   HoistRegion(DT->getNode(L->getHeader()));
00289 
00290   // Now that all loop invariants have been removed from the loop, promote any
00291   // memory references to scalars that we can...
00292   if (!DisablePromotion)
00293     PromoteValuesInLoop();
00294 
00295   // Clear out loops state information for the next iteration
00296   CurLoop = 0;
00297   Preheader = 0;
00298 
00299   LoopToAliasMap[L] = CurAST;
00300   return Changed;
00301 }
00302 
00303 /// SinkRegion - Walk the specified region of the CFG (defined by all blocks
00304 /// dominated by the specified block, and that are in the current loop) in
00305 /// reverse depth first order w.r.t the DominatorTree.  This allows us to visit
00306 /// uses before definitions, allowing us to sink a loop body in one pass without
00307 /// iteration.
00308 ///
00309 void LICM::SinkRegion(DomTreeNode *N) {
00310   assert(N != 0 && "Null dominator tree node?");
00311   BasicBlock *BB = N->getBlock();
00312 
00313   // If this subregion is not in the top level loop at all, exit.
00314   if (!CurLoop->contains(BB)) return;
00315 
00316   // We are processing blocks in reverse dfo, so process children first...
00317   const std::vector<DomTreeNode*> &Children = N->getChildren();
00318   for (unsigned i = 0, e = Children.size(); i != e; ++i)
00319     SinkRegion(Children[i]);
00320 
00321   // Only need to process the contents of this block if it is not part of a
00322   // subloop (which would already have been processed).
00323   if (inSubLoop(BB)) return;
00324 
00325   for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) {
00326     Instruction &I = *--II;
00327 
00328     // Check to see if we can sink this instruction to the exit blocks
00329     // of the loop.  We can do this if the all users of the instruction are
00330     // outside of the loop.  In this case, it doesn't even matter if the
00331     // operands of the instruction are loop invariant.
00332     //
00333     if (isNotUsedInLoop(I) && canSinkOrHoistInst(I)) {
00334       ++II;
00335       sink(I);
00336     }
00337   }
00338 }
00339 
00340 
00341 /// HoistRegion - Walk the specified region of the CFG (defined by all blocks
00342 /// dominated by the specified block, and that are in the current loop) in depth
00343 /// first order w.r.t the DominatorTree.  This allows us to visit definitions
00344 /// before uses, allowing us to hoist a loop body in one pass without iteration.
00345 ///
00346 void LICM::HoistRegion(DomTreeNode *N) {
00347   assert(N != 0 && "Null dominator tree node?");
00348   BasicBlock *BB = N->getBlock();
00349 
00350   // If this subregion is not in the top level loop at all, exit.
00351   if (!CurLoop->contains(BB)) return;
00352 
00353   // Only need to process the contents of this block if it is not part of a
00354   // subloop (which would already have been processed).
00355   if (!inSubLoop(BB))
00356     for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) {
00357       Instruction &I = *II++;
00358 
00359       // Try hoisting the instruction out to the preheader.  We can only do this
00360       // if all of the operands of the instruction are loop invariant and if it
00361       // is safe to hoist the instruction.
00362       //
00363       if (isLoopInvariantInst(I) && canSinkOrHoistInst(I) &&
00364           isSafeToExecuteUnconditionally(I))
00365         hoist(I);
00366       }
00367 
00368   const std::vector<DomTreeNode*> &Children = N->getChildren();
00369   for (unsigned i = 0, e = Children.size(); i != e; ++i)
00370     HoistRegion(Children[i]);
00371 }
00372 
00373 /// canSinkOrHoistInst - Return true if the hoister and sinker can handle this
00374 /// instruction.
00375 ///
00376 bool LICM::canSinkOrHoistInst(Instruction &I) {
00377   // Loads have extra constraints we have to verify before we can hoist them.
00378   if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
00379     if (LI->isVolatile())
00380       return false;        // Don't hoist volatile loads!
00381 
00382     // Loads from constant memory are always safe to move, even if they end up
00383     // in the same alias set as something that ends up being modified.
00384     if (EnableLICMConstantMotion &&
00385         AA->pointsToConstantMemory(LI->getOperand(0)))
00386       return true;
00387     
00388     // Don't hoist loads which have may-aliased stores in loop.
00389     unsigned Size = 0;
00390     if (LI->getType()->isSized())
00391       Size = AA->getTargetData().getTypeStoreSize(LI->getType());
00392     return !pointerInvalidatedByLoop(LI->getOperand(0), Size);
00393   } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
00394     // Handle obvious cases efficiently.
00395     AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI);
00396     if (Behavior == AliasAnalysis::DoesNotAccessMemory)
00397       return true;
00398     else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
00399       // If this call only reads from memory and there are no writes to memory
00400       // in the loop, we can hoist or sink the call as appropriate.
00401       bool FoundMod = false;
00402       for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
00403            I != E; ++I) {
00404         AliasSet &AS = *I;
00405         if (!AS.isForwardingAliasSet() && AS.isMod()) {
00406           FoundMod = true;
00407           break;
00408         }
00409       }
00410       if (!FoundMod) return true;
00411     }
00412 
00413     // FIXME: This should use mod/ref information to see if we can hoist or sink
00414     // the call.
00415 
00416     return false;
00417   }
00418 
00419   // Otherwise these instructions are hoistable/sinkable
00420   return isa<BinaryOperator>(I) || isa<CastInst>(I) ||
00421          isa<SelectInst>(I) || isa<GetElementPtrInst>(I) || isa<CmpInst>(I) ||
00422          isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) ||
00423          isa<ShuffleVectorInst>(I);
00424 }
00425 
00426 /// isNotUsedInLoop - Return true if the only users of this instruction are
00427 /// outside of the loop.  If this is true, we can sink the instruction to the
00428 /// exit blocks of the loop.
00429 ///
00430 bool LICM::isNotUsedInLoop(Instruction &I) {
00431   for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI) {
00432     Instruction *User = cast<Instruction>(*UI);
00433     if (PHINode *PN = dyn_cast<PHINode>(User)) {
00434       // PHI node uses occur in predecessor blocks!
00435       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00436         if (PN->getIncomingValue(i) == &I)
00437           if (CurLoop->contains(PN->getIncomingBlock(i)))
00438             return false;
00439     } else if (CurLoop->contains(User->getParent())) {
00440       return false;
00441     }
00442   }
00443   return true;
00444 }
00445 
00446 
00447 /// isLoopInvariantInst - Return true if all operands of this instruction are
00448 /// loop invariant.  We also filter out non-hoistable instructions here just for
00449 /// efficiency.
00450 ///
00451 bool LICM::isLoopInvariantInst(Instruction &I) {
00452   // The instruction is loop invariant if all of its operands are loop-invariant
00453   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
00454     if (!CurLoop->isLoopInvariant(I.getOperand(i)))
00455       return false;
00456 
00457   // If we got this far, the instruction is loop invariant!
00458   return true;
00459 }
00460 
00461 /// sink - When an instruction is found to only be used outside of the loop,
00462 /// this function moves it to the exit blocks and patches up SSA form as needed.
00463 /// This method is guaranteed to remove the original instruction from its
00464 /// position, and may either delete it or move it to outside of the loop.
00465 ///
00466 void LICM::sink(Instruction &I) {
00467   DOUT << "LICM sinking instruction: " << I;
00468 
00469   SmallVector<BasicBlock*, 8> ExitBlocks;
00470   CurLoop->getExitBlocks(ExitBlocks);
00471 
00472   if (isa<LoadInst>(I)) ++NumMovedLoads;
00473   else if (isa<CallInst>(I)) ++NumMovedCalls;
00474   ++NumSunk;
00475   Changed = true;
00476 
00477   // The case where there is only a single exit node of this loop is common
00478   // enough that we handle it as a special (more efficient) case.  It is more
00479   // efficient to handle because there are no PHI nodes that need to be placed.
00480   if (ExitBlocks.size() == 1) {
00481     if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[0], I.getParent())) {
00482       // Instruction is not used, just delete it.
00483       CurAST->deleteValue(&I);
00484       if (!I.use_empty())  // If I has users in unreachable blocks, eliminate.
00485         I.replaceAllUsesWith(UndefValue::get(I.getType()));
00486       I.eraseFromParent();
00487     } else {
00488       // Move the instruction to the start of the exit block, after any PHI
00489       // nodes in it.
00490       I.removeFromParent();
00491 
00492       BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI();
00493       ExitBlocks[0]->getInstList().insert(InsertPt, &I);
00494     }
00495   } else if (ExitBlocks.empty()) {
00496     // The instruction is actually dead if there ARE NO exit blocks.
00497     CurAST->deleteValue(&I);
00498     if (!I.use_empty())  // If I has users in unreachable blocks, eliminate.
00499       I.replaceAllUsesWith(UndefValue::get(I.getType()));
00500     I.eraseFromParent();
00501   } else {
00502     // Otherwise, if we have multiple exits, use the PromoteMem2Reg function to
00503     // do all of the hard work of inserting PHI nodes as necessary.  We convert
00504     // the value into a stack object to get it to do this.
00505 
00506     // Firstly, we create a stack object to hold the value...
00507     AllocaInst *AI = 0;
00508 
00509     if (I.getType() != Type::VoidTy) {
00510       AI = new AllocaInst(I.getType(), 0, I.getName(),
00511                           I.getParent()->getParent()->getEntryBlock().begin());
00512       CurAST->add(AI);
00513     }
00514 
00515     // Secondly, insert load instructions for each use of the instruction
00516     // outside of the loop.
00517     while (!I.use_empty()) {
00518       Instruction *U = cast<Instruction>(I.use_back());
00519 
00520       // If the user is a PHI Node, we actually have to insert load instructions
00521       // in all predecessor blocks, not in the PHI block itself!
00522       if (PHINode *UPN = dyn_cast<PHINode>(U)) {
00523         // Only insert into each predecessor once, so that we don't have
00524         // different incoming values from the same block!
00525         std::map<BasicBlock*, Value*> InsertedBlocks;
00526         for (unsigned i = 0, e = UPN->getNumIncomingValues(); i != e; ++i)
00527           if (UPN->getIncomingValue(i) == &I) {
00528             BasicBlock *Pred = UPN->getIncomingBlock(i);
00529             Value *&PredVal = InsertedBlocks[Pred];
00530             if (!PredVal) {
00531               // Insert a new load instruction right before the terminator in
00532               // the predecessor block.
00533               PredVal = new LoadInst(AI, "", Pred->getTerminator());
00534               CurAST->add(cast<LoadInst>(PredVal));
00535             }
00536 
00537             UPN->setIncomingValue(i, PredVal);
00538           }
00539 
00540       } else {
00541         LoadInst *L = new LoadInst(AI, "", U);
00542         U->replaceUsesOfWith(&I, L);
00543         CurAST->add(L);
00544       }
00545     }
00546 
00547     // Thirdly, insert a copy of the instruction in each exit block of the loop
00548     // that is dominated by the instruction, storing the result into the memory
00549     // location.  Be careful not to insert the instruction into any particular
00550     // basic block more than once.
00551     std::set<BasicBlock*> InsertedBlocks;
00552     BasicBlock *InstOrigBB = I.getParent();
00553 
00554     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00555       BasicBlock *ExitBlock = ExitBlocks[i];
00556 
00557       if (isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB)) {
00558         // If we haven't already processed this exit block, do so now.
00559         if (InsertedBlocks.insert(ExitBlock).second) {
00560           // Insert the code after the last PHI node...
00561           BasicBlock::iterator InsertPt = ExitBlock->getFirstNonPHI();
00562 
00563           // If this is the first exit block processed, just move the original
00564           // instruction, otherwise clone the original instruction and insert
00565           // the copy.
00566           Instruction *New;
00567           if (InsertedBlocks.size() == 1) {
00568             I.removeFromParent();
00569             ExitBlock->getInstList().insert(InsertPt, &I);
00570             New = &I;
00571           } else {
00572             New = I.clone();
00573             CurAST->copyValue(&I, New);
00574             if (!I.getName().empty())
00575               New->setName(I.getName()+".le");
00576             ExitBlock->getInstList().insert(InsertPt, New);
00577           }
00578 
00579           // Now that we have inserted the instruction, store it into the alloca
00580           if (AI) new StoreInst(New, AI, InsertPt);
00581         }
00582       }
00583     }
00584 
00585     // If the instruction doesn't dominate any exit blocks, it must be dead.
00586     if (InsertedBlocks.empty()) {
00587       CurAST->deleteValue(&I);
00588       I.eraseFromParent();
00589     }
00590 
00591     // Finally, promote the fine value to SSA form.
00592     if (AI) {
00593       std::vector<AllocaInst*> Allocas;
00594       Allocas.push_back(AI);
00595       PromoteMemToReg(Allocas, *DT, *DF, CurAST);
00596     }
00597   }
00598 }
00599 
00600 /// hoist - When an instruction is found to only use loop invariant operands
00601 /// that is safe to hoist, this instruction is called to do the dirty work.
00602 ///
00603 void LICM::hoist(Instruction &I) {
00604   DOUT << "LICM hoisting to " << Preheader->getName() << ": " << I;
00605 
00606   // Remove the instruction from its current basic block... but don't delete the
00607   // instruction.
00608   I.removeFromParent();
00609 
00610   // Insert the new node in Preheader, before the terminator.
00611   Preheader->getInstList().insert(Preheader->getTerminator(), &I);
00612 
00613   if (isa<LoadInst>(I)) ++NumMovedLoads;
00614   else if (isa<CallInst>(I)) ++NumMovedCalls;
00615   ++NumHoisted;
00616   Changed = true;
00617 }
00618 
00619 /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it is
00620 /// not a trapping instruction or if it is a trapping instruction and is
00621 /// guaranteed to execute.
00622 ///
00623 bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) {
00624   // If it is not a trapping instruction, it is always safe to hoist.
00625   if (!Inst.isTrapping()) return true;
00626 
00627   // Otherwise we have to check to make sure that the instruction dominates all
00628   // of the exit blocks.  If it doesn't, then there is a path out of the loop
00629   // which does not execute this instruction, so we can't hoist it.
00630 
00631   // If the instruction is in the header block for the loop (which is very
00632   // common), it is always guaranteed to dominate the exit blocks.  Since this
00633   // is a common case, and can save some work, check it now.
00634   if (Inst.getParent() == CurLoop->getHeader())
00635     return true;
00636 
00637   // It's always safe to load from a global or alloca.
00638   if (isa<LoadInst>(Inst))
00639     if (isa<AllocationInst>(Inst.getOperand(0)) ||
00640         isa<GlobalVariable>(Inst.getOperand(0)))
00641       return true;
00642 
00643   // Get the exit blocks for the current loop.
00644   SmallVector<BasicBlock*, 8> ExitBlocks;
00645   CurLoop->getExitBlocks(ExitBlocks);
00646 
00647   // For each exit block, get the DT node and walk up the DT until the
00648   // instruction's basic block is found or we exit the loop.
00649   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00650     if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[i], Inst.getParent()))
00651       return false;
00652 
00653   return true;
00654 }
00655 
00656 
00657 /// PromoteValuesInLoop - Try to promote memory values to scalars by sinking
00658 /// stores out of the loop and moving loads to before the loop.  We do this by
00659 /// looping over the stores in the loop, looking for stores to Must pointers
00660 /// which are loop invariant.  We promote these memory locations to use allocas
00661 /// instead.  These allocas can easily be raised to register values by the
00662 /// PromoteMem2Reg functionality.
00663 ///
00664 void LICM::PromoteValuesInLoop() {
00665   // PromotedValues - List of values that are promoted out of the loop.  Each
00666   // value has an alloca instruction for it, and a canonical version of the
00667   // pointer.
00668   std::vector<std::pair<AllocaInst*, Value*> > PromotedValues;
00669   std::map<Value*, AllocaInst*> ValueToAllocaMap; // Map of ptr to alloca
00670 
00671   FindPromotableValuesInLoop(PromotedValues, ValueToAllocaMap);
00672   if (ValueToAllocaMap.empty()) return;   // If there are values to promote.
00673 
00674   Changed = true;
00675   NumPromoted += PromotedValues.size();
00676 
00677   std::vector<Value*> PointerValueNumbers;
00678 
00679   // Emit a copy from the value into the alloca'd value in the loop preheader
00680   TerminatorInst *LoopPredInst = Preheader->getTerminator();
00681   for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) {
00682     Value *Ptr = PromotedValues[i].second;
00683 
00684     // If we are promoting a pointer value, update alias information for the
00685     // inserted load.
00686     Value *LoadValue = 0;
00687     if (isa<PointerType>(cast<PointerType>(Ptr->getType())->getElementType())) {
00688       // Locate a load or store through the pointer, and assign the same value
00689       // to LI as we are loading or storing.  Since we know that the value is
00690       // stored in this loop, this will always succeed.
00691       for (Value::use_iterator UI = Ptr->use_begin(), E = Ptr->use_end();
00692            UI != E; ++UI)
00693         if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
00694           LoadValue = LI;
00695           break;
00696         } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
00697           if (SI->getOperand(1) == Ptr) {
00698             LoadValue = SI->getOperand(0);
00699             break;
00700           }
00701         }
00702       assert(LoadValue && "No store through the pointer found!");
00703       PointerValueNumbers.push_back(LoadValue);  // Remember this for later.
00704     }
00705 
00706     // Load from the memory we are promoting.
00707     LoadInst *LI = new LoadInst(Ptr, Ptr->getName()+".promoted", LoopPredInst);
00708 
00709     if (LoadValue) CurAST->copyValue(LoadValue, LI);
00710 
00711     // Store into the temporary alloca.
00712     new StoreInst(LI, PromotedValues[i].first, LoopPredInst);
00713   }
00714 
00715   // Scan the basic blocks in the loop, replacing uses of our pointers with
00716   // uses of the allocas in question.
00717   //
00718   for (Loop::block_iterator I = CurLoop->block_begin(),
00719          E = CurLoop->block_end(); I != E; ++I) {
00720     BasicBlock *BB = *I;
00721     // Rewrite all loads and stores in the block of the pointer...
00722     for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
00723       if (LoadInst *L = dyn_cast<LoadInst>(II)) {
00724         std::map<Value*, AllocaInst*>::iterator
00725           I = ValueToAllocaMap.find(L->getOperand(0));
00726         if (I != ValueToAllocaMap.end())
00727           L->setOperand(0, I->second);    // Rewrite load instruction...
00728       } else if (StoreInst *S = dyn_cast<StoreInst>(II)) {
00729         std::map<Value*, AllocaInst*>::iterator
00730           I = ValueToAllocaMap.find(S->getOperand(1));
00731         if (I != ValueToAllocaMap.end())
00732           S->setOperand(1, I->second);    // Rewrite store instruction...
00733       }
00734     }
00735   }
00736 
00737   // Now that the body of the loop uses the allocas instead of the original
00738   // memory locations, insert code to copy the alloca value back into the
00739   // original memory location on all exits from the loop.  Note that we only
00740   // want to insert one copy of the code in each exit block, though the loop may
00741   // exit to the same block more than once.
00742   //
00743   SmallPtrSet<BasicBlock*, 16> ProcessedBlocks;
00744 
00745   SmallVector<BasicBlock*, 8> ExitBlocks;
00746   CurLoop->getExitBlocks(ExitBlocks);
00747   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00748     if (!ProcessedBlocks.insert(ExitBlocks[i]))
00749       continue;
00750   
00751     // Copy all of the allocas into their memory locations.
00752     BasicBlock::iterator BI = ExitBlocks[i]->getFirstNonPHI();
00753     Instruction *InsertPos = BI;
00754     unsigned PVN = 0;
00755     for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) {
00756       // Load from the alloca.
00757       LoadInst *LI = new LoadInst(PromotedValues[i].first, "", InsertPos);
00758 
00759       // If this is a pointer type, update alias info appropriately.
00760       if (isa<PointerType>(LI->getType()))
00761         CurAST->copyValue(PointerValueNumbers[PVN++], LI);
00762 
00763       // Store into the memory we promoted.
00764       new StoreInst(LI, PromotedValues[i].second, InsertPos);
00765     }
00766   }
00767 
00768   // Now that we have done the deed, use the mem2reg functionality to promote
00769   // all of the new allocas we just created into real SSA registers.
00770   //
00771   std::vector<AllocaInst*> PromotedAllocas;
00772   PromotedAllocas.reserve(PromotedValues.size());
00773   for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i)
00774     PromotedAllocas.push_back(PromotedValues[i].first);
00775   PromoteMemToReg(PromotedAllocas, *DT, *DF, CurAST);
00776 }
00777 
00778 /// FindPromotableValuesInLoop - Check the current loop for stores to definite
00779 /// pointers, which are not loaded and stored through may aliases and are safe
00780 /// for promotion.  If these are found, create an alloca for the value, add it 
00781 /// to the PromotedValues list, and keep track of the mapping from value to 
00782 /// alloca. 
00783 void LICM::FindPromotableValuesInLoop(
00784                    std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues,
00785                              std::map<Value*, AllocaInst*> &ValueToAllocaMap) {
00786   Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin();
00787 
00788   SmallVector<BasicBlock*, 4> ExitingBlocks;
00789   CurLoop->getExitingBlocks(ExitingBlocks);
00790 
00791   // Loop over all of the alias sets in the tracker object.
00792   for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
00793        I != E; ++I) {
00794     AliasSet &AS = *I;
00795     // We can promote this alias set if it has a store, if it is a "Must" alias
00796     // set, if the pointer is loop invariant, and if we are not eliminating any
00797     // volatile loads or stores.
00798     if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
00799         AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->first))
00800       continue;
00801     
00802     assert(!AS.empty() &&
00803            "Must alias set should have at least one pointer element in it!");
00804     Value *V = AS.begin()->first;
00805 
00806     // Check that all of the pointers in the alias set have the same type.  We
00807     // cannot (yet) promote a memory location that is loaded and stored in
00808     // different sizes.
00809     {
00810       bool PointerOk = true;
00811       for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I)
00812         if (V->getType() != I->first->getType()) {
00813           PointerOk = false;
00814           break;
00815         }
00816       if (!PointerOk)
00817         continue;
00818     }
00819 
00820     // It isn't safe to promote a load/store from the loop if the load/store is
00821     // conditional.  For example, turning:
00822     //
00823     //    for () { if (c) *P += 1; }
00824     //
00825     // into:
00826     //
00827     //    tmp = *P;  for () { if (c) tmp +=1; } *P = tmp;
00828     //
00829     // is not safe, because *P may only be valid to access if 'c' is true.
00830     // 
00831     // It is safe to promote P if all uses are direct load/stores and if at
00832     // least one is guaranteed to be executed.
00833     bool GuaranteedToExecute = false;
00834     bool InvalidInst = false;
00835     for (Value::use_iterator UI = V->use_begin(), UE = V->use_end();
00836          UI != UE; ++UI) {
00837       // Ignore instructions not in this loop.
00838       Instruction *Use = dyn_cast<Instruction>(*UI);
00839       if (!Use || !CurLoop->contains(Use->getParent()))
00840         continue;
00841 
00842       if (!isa<LoadInst>(Use) && !isa<StoreInst>(Use)) {
00843         InvalidInst = true;
00844         break;
00845       }
00846       
00847       if (!GuaranteedToExecute)
00848         GuaranteedToExecute = isSafeToExecuteUnconditionally(*Use);
00849     }
00850 
00851     // If there is an non-load/store instruction in the loop, we can't promote
00852     // it.  If there isn't a guaranteed-to-execute instruction, we can't
00853     // promote.
00854     if (InvalidInst || !GuaranteedToExecute)
00855       continue;
00856     
00857     const Type *Ty = cast<PointerType>(V->getType())->getElementType();
00858     AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart);
00859     PromotedValues.push_back(std::make_pair(AI, V));
00860 
00861     // Update the AST and alias analysis.
00862     CurAST->copyValue(V, AI);
00863 
00864     for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I)
00865       ValueToAllocaMap.insert(std::make_pair(I->first, AI));
00866 
00867     DOUT << "LICM: Promoting value: " << *V << "\n";
00868   }
00869 }
00870 
00871 /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
00872 void LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) {
00873   AliasSetTracker *AST = LoopToAliasMap[L];
00874   if (!AST)
00875     return;
00876 
00877   AST->copyValue(From, To);
00878 }
00879 
00880 /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
00881 /// set.
00882 void LICM::deleteAnalysisValue(Value *V, Loop *L) {
00883   AliasSetTracker *AST = LoopToAliasMap[L];
00884   if (!AST)
00885     return;
00886 
00887   AST->deleteValue(V);
00888 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.