LLVM API Documentation

LoopUnswitch.cpp
Go to the documentation of this file.
00001 //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===//
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 that contain branches on loop-invariant conditions
00011 // to have multiple loops.  For example, it turns the left into the right code:
00012 //
00013 //  for (...)                  if (lic)
00014 //    A                          for (...)
00015 //    if (lic)                     A; B; C
00016 //      B                      else
00017 //    C                          for (...)
00018 //                                 A; C
00019 //
00020 // This can increase the size of the code exponentially (doubling it every time
00021 // a loop is unswitched) so we only unswitch if the resultant code will be
00022 // smaller than a threshold.
00023 //
00024 // This pass expects LICM to be run before it to hoist invariant conditions out
00025 // of the loop, to make the unswitching opportunity obvious.
00026 //
00027 //===----------------------------------------------------------------------===//
00028 
00029 #define DEBUG_TYPE "loop-unswitch"
00030 #include "llvm/Transforms/Scalar.h"
00031 #include "llvm/ADT/STLExtras.h"
00032 #include "llvm/ADT/SmallPtrSet.h"
00033 #include "llvm/ADT/Statistic.h"
00034 #include "llvm/Analysis/CodeMetrics.h"
00035 #include "llvm/Analysis/Dominators.h"
00036 #include "llvm/Analysis/InstructionSimplify.h"
00037 #include "llvm/Analysis/LoopInfo.h"
00038 #include "llvm/Analysis/LoopPass.h"
00039 #include "llvm/Analysis/ScalarEvolution.h"
00040 #include "llvm/Analysis/TargetTransformInfo.h"
00041 #include "llvm/IR/Constants.h"
00042 #include "llvm/IR/DerivedTypes.h"
00043 #include "llvm/IR/Function.h"
00044 #include "llvm/IR/Instructions.h"
00045 #include "llvm/Support/CommandLine.h"
00046 #include "llvm/Support/Debug.h"
00047 #include "llvm/Support/raw_ostream.h"
00048 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00049 #include "llvm/Transforms/Utils/Cloning.h"
00050 #include "llvm/Transforms/Utils/Local.h"
00051 #include <algorithm>
00052 #include <map>
00053 #include <set>
00054 using namespace llvm;
00055 
00056 STATISTIC(NumBranches, "Number of branches unswitched");
00057 STATISTIC(NumSwitches, "Number of switches unswitched");
00058 STATISTIC(NumSelects , "Number of selects unswitched");
00059 STATISTIC(NumTrivial , "Number of unswitches that are trivial");
00060 STATISTIC(NumSimplify, "Number of simplifications of unswitched code");
00061 STATISTIC(TotalInsts,  "Total number of instructions analyzed");
00062 
00063 // The specific value of 100 here was chosen based only on intuition and a
00064 // few specific examples.
00065 static cl::opt<unsigned>
00066 Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"),
00067           cl::init(100), cl::Hidden);
00068 
00069 namespace {
00070 
00071   class LUAnalysisCache {
00072 
00073     typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> >
00074       UnswitchedValsMap;
00075 
00076     typedef UnswitchedValsMap::iterator UnswitchedValsIt;
00077 
00078     struct LoopProperties {
00079       unsigned CanBeUnswitchedCount;
00080       unsigned SizeEstimation;
00081       UnswitchedValsMap UnswitchedVals;
00082     };
00083 
00084     // Here we use std::map instead of DenseMap, since we need to keep valid
00085     // LoopProperties pointer for current loop for better performance.
00086     typedef std::map<const Loop*, LoopProperties> LoopPropsMap;
00087     typedef LoopPropsMap::iterator LoopPropsMapIt;
00088 
00089     LoopPropsMap LoopsProperties;
00090     UnswitchedValsMap* CurLoopInstructions;
00091     LoopProperties* CurrentLoopProperties;
00092 
00093     // Max size of code we can produce on remained iterations.
00094     unsigned MaxSize;
00095 
00096     public:
00097 
00098       LUAnalysisCache() :
00099         CurLoopInstructions(NULL), CurrentLoopProperties(NULL),
00100         MaxSize(Threshold)
00101       {}
00102 
00103       // Analyze loop. Check its size, calculate is it possible to unswitch
00104       // it. Returns true if we can unswitch this loop.
00105       bool countLoop(const Loop* L, const TargetTransformInfo &TTI);
00106 
00107       // Clean all data related to given loop.
00108       void forgetLoop(const Loop* L);
00109 
00110       // Mark case value as unswitched.
00111       // Since SI instruction can be partly unswitched, in order to avoid
00112       // extra unswitching in cloned loops keep track all unswitched values.
00113       void setUnswitched(const SwitchInst* SI, const Value* V);
00114 
00115       // Check was this case value unswitched before or not.
00116       bool isUnswitched(const SwitchInst* SI, const Value* V);
00117 
00118       // Clone all loop-unswitch related loop properties.
00119       // Redistribute unswitching quotas.
00120       // Note, that new loop data is stored inside the VMap.
00121       void cloneData(const Loop* NewLoop, const Loop* OldLoop,
00122                      const ValueToValueMapTy& VMap);
00123   };
00124 
00125   class LoopUnswitch : public LoopPass {
00126     LoopInfo *LI;  // Loop information
00127     LPPassManager *LPM;
00128 
00129     // LoopProcessWorklist - Used to check if second loop needs processing
00130     // after RewriteLoopBodyWithConditionConstant rewrites first loop.
00131     std::vector<Loop*> LoopProcessWorklist;
00132 
00133     LUAnalysisCache BranchesInfo;
00134 
00135     bool OptimizeForSize;
00136     bool redoLoop;
00137 
00138     Loop *currentLoop;
00139     DominatorTree *DT;
00140     BasicBlock *loopHeader;
00141     BasicBlock *loopPreheader;
00142 
00143     // LoopBlocks contains all of the basic blocks of the loop, including the
00144     // preheader of the loop, the body of the loop, and the exit blocks of the
00145     // loop, in that order.
00146     std::vector<BasicBlock*> LoopBlocks;
00147     // NewBlocks contained cloned copy of basic blocks from LoopBlocks.
00148     std::vector<BasicBlock*> NewBlocks;
00149 
00150   public:
00151     static char ID; // Pass ID, replacement for typeid
00152     explicit LoopUnswitch(bool Os = false) :
00153       LoopPass(ID), OptimizeForSize(Os), redoLoop(false),
00154       currentLoop(NULL), DT(NULL), loopHeader(NULL),
00155       loopPreheader(NULL) {
00156         initializeLoopUnswitchPass(*PassRegistry::getPassRegistry());
00157       }
00158 
00159     bool runOnLoop(Loop *L, LPPassManager &LPM);
00160     bool processCurrentLoop();
00161 
00162     /// This transformation requires natural loop information & requires that
00163     /// loop preheaders be inserted into the CFG.
00164     ///
00165     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00166       AU.addRequiredID(LoopSimplifyID);
00167       AU.addPreservedID(LoopSimplifyID);
00168       AU.addRequired<LoopInfo>();
00169       AU.addPreserved<LoopInfo>();
00170       AU.addRequiredID(LCSSAID);
00171       AU.addPreservedID(LCSSAID);
00172       AU.addPreserved<DominatorTree>();
00173       AU.addPreserved<ScalarEvolution>();
00174       AU.addRequired<TargetTransformInfo>();
00175     }
00176 
00177   private:
00178 
00179     virtual void releaseMemory() {
00180       BranchesInfo.forgetLoop(currentLoop);
00181     }
00182 
00183     /// RemoveLoopFromWorklist - If the specified loop is on the loop worklist,
00184     /// remove it.
00185     void RemoveLoopFromWorklist(Loop *L) {
00186       std::vector<Loop*>::iterator I = std::find(LoopProcessWorklist.begin(),
00187                                                  LoopProcessWorklist.end(), L);
00188       if (I != LoopProcessWorklist.end())
00189         LoopProcessWorklist.erase(I);
00190     }
00191 
00192     void initLoopData() {
00193       loopHeader = currentLoop->getHeader();
00194       loopPreheader = currentLoop->getLoopPreheader();
00195     }
00196 
00197     /// Split all of the edges from inside the loop to their exit blocks.
00198     /// Update the appropriate Phi nodes as we do so.
00199     void SplitExitEdges(Loop *L, const SmallVector<BasicBlock *, 8> &ExitBlocks);
00200 
00201     bool UnswitchIfProfitable(Value *LoopCond, Constant *Val);
00202     void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
00203                                   BasicBlock *ExitBlock);
00204     void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L);
00205 
00206     void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
00207                                               Constant *Val, bool isEqual);
00208 
00209     void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
00210                                         BasicBlock *TrueDest,
00211                                         BasicBlock *FalseDest,
00212                                         Instruction *InsertPt);
00213 
00214     void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L);
00215     void RemoveBlockIfDead(BasicBlock *BB,
00216                            std::vector<Instruction*> &Worklist, Loop *l);
00217     void RemoveLoopFromHierarchy(Loop *L);
00218     bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = 0,
00219                                     BasicBlock **LoopExit = 0);
00220 
00221   };
00222 }
00223 
00224 // Analyze loop. Check its size, calculate is it possible to unswitch
00225 // it. Returns true if we can unswitch this loop.
00226 bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI) {
00227 
00228   std::pair<LoopPropsMapIt, bool> InsertRes =
00229       LoopsProperties.insert(std::make_pair(L, LoopProperties()));
00230 
00231   LoopProperties& Props = InsertRes.first->second;
00232 
00233   if (InsertRes.second) {
00234     // New loop.
00235 
00236     // Limit the number of instructions to avoid causing significant code
00237     // expansion, and the number of basic blocks, to avoid loops with
00238     // large numbers of branches which cause loop unswitching to go crazy.
00239     // This is a very ad-hoc heuristic.
00240 
00241     // FIXME: This is overly conservative because it does not take into
00242     // consideration code simplification opportunities and code that can
00243     // be shared by the resultant unswitched loops.
00244     CodeMetrics Metrics;
00245     for (Loop::block_iterator I = L->block_begin(),
00246            E = L->block_end();
00247          I != E; ++I)
00248       Metrics.analyzeBasicBlock(*I, TTI);
00249 
00250     Props.SizeEstimation = std::min(Metrics.NumInsts, Metrics.NumBlocks * 5);
00251     Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation);
00252     MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount;
00253 
00254     if (Metrics.notDuplicatable) {
00255       DEBUG(dbgs() << "NOT unswitching loop %"
00256             << L->getHeader()->getName() << ", contents cannot be "
00257             << "duplicated!\n");
00258       return false;
00259     }
00260   }
00261 
00262   if (!Props.CanBeUnswitchedCount) {
00263     DEBUG(dbgs() << "NOT unswitching loop %"
00264           << L->getHeader()->getName() << ", cost too high: "
00265           << L->getBlocks().size() << "\n");
00266 
00267     return false;
00268   }
00269 
00270   // Be careful. This links are good only before new loop addition.
00271   CurrentLoopProperties = &Props;
00272   CurLoopInstructions = &Props.UnswitchedVals;
00273 
00274   return true;
00275 }
00276 
00277 // Clean all data related to given loop.
00278 void LUAnalysisCache::forgetLoop(const Loop* L) {
00279 
00280   LoopPropsMapIt LIt = LoopsProperties.find(L);
00281 
00282   if (LIt != LoopsProperties.end()) {
00283     LoopProperties& Props = LIt->second;
00284     MaxSize += Props.CanBeUnswitchedCount * Props.SizeEstimation;
00285     LoopsProperties.erase(LIt);
00286   }
00287 
00288   CurrentLoopProperties = NULL;
00289   CurLoopInstructions = NULL;
00290 }
00291 
00292 // Mark case value as unswitched.
00293 // Since SI instruction can be partly unswitched, in order to avoid
00294 // extra unswitching in cloned loops keep track all unswitched values.
00295 void LUAnalysisCache::setUnswitched(const SwitchInst* SI, const Value* V) {
00296   (*CurLoopInstructions)[SI].insert(V);
00297 }
00298 
00299 // Check was this case value unswitched before or not.
00300 bool LUAnalysisCache::isUnswitched(const SwitchInst* SI, const Value* V) {
00301   return (*CurLoopInstructions)[SI].count(V);
00302 }
00303 
00304 // Clone all loop-unswitch related loop properties.
00305 // Redistribute unswitching quotas.
00306 // Note, that new loop data is stored inside the VMap.
00307 void LUAnalysisCache::cloneData(const Loop* NewLoop, const Loop* OldLoop,
00308                      const ValueToValueMapTy& VMap) {
00309 
00310   LoopProperties& NewLoopProps = LoopsProperties[NewLoop];
00311   LoopProperties& OldLoopProps = *CurrentLoopProperties;
00312   UnswitchedValsMap& Insts = OldLoopProps.UnswitchedVals;
00313 
00314   // Reallocate "can-be-unswitched quota"
00315 
00316   --OldLoopProps.CanBeUnswitchedCount;
00317   unsigned Quota = OldLoopProps.CanBeUnswitchedCount;
00318   NewLoopProps.CanBeUnswitchedCount = Quota / 2;
00319   OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2;
00320 
00321   NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation;
00322 
00323   // Clone unswitched values info:
00324   // for new loop switches we clone info about values that was
00325   // already unswitched and has redundant successors.
00326   for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) {
00327     const SwitchInst* OldInst = I->first;
00328     Value* NewI = VMap.lookup(OldInst);
00329     const SwitchInst* NewInst = cast_or_null<SwitchInst>(NewI);
00330     assert(NewInst && "All instructions that are in SrcBB must be in VMap.");
00331 
00332     NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst];
00333   }
00334 }
00335 
00336 char LoopUnswitch::ID = 0;
00337 INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops",
00338                       false, false)
00339 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
00340 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
00341 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
00342 INITIALIZE_PASS_DEPENDENCY(LCSSA)
00343 INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops",
00344                       false, false)
00345 
00346 Pass *llvm::createLoopUnswitchPass(bool Os) {
00347   return new LoopUnswitch(Os);
00348 }
00349 
00350 /// FindLIVLoopCondition - Cond is a condition that occurs in L.  If it is
00351 /// invariant in the loop, or has an invariant piece, return the invariant.
00352 /// Otherwise, return null.
00353 static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) {
00354 
00355   // We started analyze new instruction, increment scanned instructions counter.
00356   ++TotalInsts;
00357 
00358   // We can never unswitch on vector conditions.
00359   if (Cond->getType()->isVectorTy())
00360     return 0;
00361 
00362   // Constants should be folded, not unswitched on!
00363   if (isa<Constant>(Cond)) return 0;
00364 
00365   // TODO: Handle: br (VARIANT|INVARIANT).
00366 
00367   // Hoist simple values out.
00368   if (L->makeLoopInvariant(Cond, Changed))
00369     return Cond;
00370 
00371   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond))
00372     if (BO->getOpcode() == Instruction::And ||
00373         BO->getOpcode() == Instruction::Or) {
00374       // If either the left or right side is invariant, we can unswitch on this,
00375       // which will cause the branch to go away in one loop and the condition to
00376       // simplify in the other one.
00377       if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed))
00378         return LHS;
00379       if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed))
00380         return RHS;
00381     }
00382 
00383   return 0;
00384 }
00385 
00386 bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
00387   LI = &getAnalysis<LoopInfo>();
00388   LPM = &LPM_Ref;
00389   DT = getAnalysisIfAvailable<DominatorTree>();
00390   currentLoop = L;
00391   Function *F = currentLoop->getHeader()->getParent();
00392   bool Changed = false;
00393   do {
00394     assert(currentLoop->isLCSSAForm(*DT));
00395     redoLoop = false;
00396     Changed |= processCurrentLoop();
00397   } while(redoLoop);
00398 
00399   if (Changed) {
00400     // FIXME: Reconstruct dom info, because it is not preserved properly.
00401     if (DT)
00402       DT->runOnFunction(*F);
00403   }
00404   return Changed;
00405 }
00406 
00407 /// processCurrentLoop - Do actual work and unswitch loop if possible
00408 /// and profitable.
00409 bool LoopUnswitch::processCurrentLoop() {
00410   bool Changed = false;
00411 
00412   initLoopData();
00413 
00414   // If LoopSimplify was unable to form a preheader, don't do any unswitching.
00415   if (!loopPreheader)
00416     return false;
00417 
00418   // Loops with indirectbr cannot be cloned.
00419   if (!currentLoop->isSafeToClone())
00420     return false;
00421 
00422   // Without dedicated exits, splitting the exit edge may fail.
00423   if (!currentLoop->hasDedicatedExits())
00424     return false;
00425 
00426   LLVMContext &Context = loopHeader->getContext();
00427 
00428   // Probably we reach the quota of branches for this loop. If so
00429   // stop unswitching.
00430   if (!BranchesInfo.countLoop(currentLoop, getAnalysis<TargetTransformInfo>()))
00431     return false;
00432 
00433   // Loop over all of the basic blocks in the loop.  If we find an interior
00434   // block that is branching on a loop-invariant condition, we can unswitch this
00435   // loop.
00436   for (Loop::block_iterator I = currentLoop->block_begin(),
00437          E = currentLoop->block_end(); I != E; ++I) {
00438     TerminatorInst *TI = (*I)->getTerminator();
00439     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
00440       // If this isn't branching on an invariant condition, we can't unswitch
00441       // it.
00442       if (BI->isConditional()) {
00443         // See if this, or some part of it, is loop invariant.  If so, we can
00444         // unswitch on it if we desire.
00445         Value *LoopCond = FindLIVLoopCondition(BI->getCondition(),
00446                                                currentLoop, Changed);
00447         if (LoopCond && UnswitchIfProfitable(LoopCond,
00448                                              ConstantInt::getTrue(Context))) {
00449           ++NumBranches;
00450           return true;
00451         }
00452       }
00453     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
00454       Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
00455                                              currentLoop, Changed);
00456       unsigned NumCases = SI->getNumCases();
00457       if (LoopCond && NumCases) {
00458         // Find a value to unswitch on:
00459         // FIXME: this should chose the most expensive case!
00460         // FIXME: scan for a case with a non-critical edge?
00461         Constant *UnswitchVal = NULL;
00462 
00463         // Do not process same value again and again.
00464         // At this point we have some cases already unswitched and
00465         // some not yet unswitched. Let's find the first not yet unswitched one.
00466         for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
00467              i != e; ++i) {
00468           Constant* UnswitchValCandidate = i.getCaseValue();
00469           if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
00470             UnswitchVal = UnswitchValCandidate;
00471             break;
00472           }
00473         }
00474 
00475         if (!UnswitchVal)
00476           continue;
00477 
00478         if (UnswitchIfProfitable(LoopCond, UnswitchVal)) {
00479           ++NumSwitches;
00480           return true;
00481         }
00482       }
00483     }
00484 
00485     // Scan the instructions to check for unswitchable values.
00486     for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end();
00487          BBI != E; ++BBI)
00488       if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
00489         Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
00490                                                currentLoop, Changed);
00491         if (LoopCond && UnswitchIfProfitable(LoopCond,
00492                                              ConstantInt::getTrue(Context))) {
00493           ++NumSelects;
00494           return true;
00495         }
00496       }
00497   }
00498   return Changed;
00499 }
00500 
00501 /// isTrivialLoopExitBlock - Check to see if all paths from BB exit the
00502 /// loop with no side effects (including infinite loops).
00503 ///
00504 /// If true, we return true and set ExitBB to the block we
00505 /// exit through.
00506 ///
00507 static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB,
00508                                          BasicBlock *&ExitBB,
00509                                          std::set<BasicBlock*> &Visited) {
00510   if (!Visited.insert(BB).second) {
00511     // Already visited. Without more analysis, this could indicate an infinite
00512     // loop.
00513     return false;
00514   } else if (!L->contains(BB)) {
00515     // Otherwise, this is a loop exit, this is fine so long as this is the
00516     // first exit.
00517     if (ExitBB != 0) return false;
00518     ExitBB = BB;
00519     return true;
00520   }
00521 
00522   // Otherwise, this is an unvisited intra-loop node.  Check all successors.
00523   for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
00524     // Check to see if the successor is a trivial loop exit.
00525     if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
00526       return false;
00527   }
00528 
00529   // Okay, everything after this looks good, check to make sure that this block
00530   // doesn't include any side effects.
00531   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
00532     if (I->mayHaveSideEffects())
00533       return false;
00534 
00535   return true;
00536 }
00537 
00538 /// isTrivialLoopExitBlock - Return true if the specified block unconditionally
00539 /// leads to an exit from the specified loop, and has no side-effects in the
00540 /// process.  If so, return the block that is exited to, otherwise return null.
00541 static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) {
00542   std::set<BasicBlock*> Visited;
00543   Visited.insert(L->getHeader());  // Branches to header make infinite loops.
00544   BasicBlock *ExitBB = 0;
00545   if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited))
00546     return ExitBB;
00547   return 0;
00548 }
00549 
00550 /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is
00551 /// trivial: that is, that the condition controls whether or not the loop does
00552 /// anything at all.  If this is a trivial condition, unswitching produces no
00553 /// code duplications (equivalently, it produces a simpler loop and a new empty
00554 /// loop, which gets deleted).
00555 ///
00556 /// If this is a trivial condition, return true, otherwise return false.  When
00557 /// returning true, this sets Cond and Val to the condition that controls the
00558 /// trivial condition: when Cond dynamically equals Val, the loop is known to
00559 /// exit.  Finally, this sets LoopExit to the BB that the loop exits to when
00560 /// Cond == Val.
00561 ///
00562 bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val,
00563                                        BasicBlock **LoopExit) {
00564   BasicBlock *Header = currentLoop->getHeader();
00565   TerminatorInst *HeaderTerm = Header->getTerminator();
00566   LLVMContext &Context = Header->getContext();
00567 
00568   BasicBlock *LoopExitBB = 0;
00569   if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) {
00570     // If the header block doesn't end with a conditional branch on Cond, we
00571     // can't handle it.
00572     if (!BI->isConditional() || BI->getCondition() != Cond)
00573       return false;
00574 
00575     // Check to see if a successor of the branch is guaranteed to
00576     // exit through a unique exit block without having any
00577     // side-effects.  If so, determine the value of Cond that causes it to do
00578     // this.
00579     if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
00580                                              BI->getSuccessor(0)))) {
00581       if (Val) *Val = ConstantInt::getTrue(Context);
00582     } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
00583                                                     BI->getSuccessor(1)))) {
00584       if (Val) *Val = ConstantInt::getFalse(Context);
00585     }
00586   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
00587     // If this isn't a switch on Cond, we can't handle it.
00588     if (SI->getCondition() != Cond) return false;
00589 
00590     // Check to see if a successor of the switch is guaranteed to go to the
00591     // latch block or exit through a one exit block without having any
00592     // side-effects.  If so, determine the value of Cond that causes it to do
00593     // this.
00594     // Note that we can't trivially unswitch on the default case or
00595     // on already unswitched cases.
00596     for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
00597          i != e; ++i) {
00598       BasicBlock* LoopExitCandidate;
00599       if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
00600                                                i.getCaseSuccessor()))) {
00601         // Okay, we found a trivial case, remember the value that is trivial.
00602         ConstantInt* CaseVal = i.getCaseValue();
00603 
00604         // Check that it was not unswitched before, since already unswitched
00605         // trivial vals are looks trivial too.
00606         if (BranchesInfo.isUnswitched(SI, CaseVal))
00607           continue;
00608         LoopExitBB = LoopExitCandidate;
00609         if (Val) *Val = CaseVal;
00610         break;
00611       }
00612     }
00613   }
00614 
00615   // If we didn't find a single unique LoopExit block, or if the loop exit block
00616   // contains phi nodes, this isn't trivial.
00617   if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin()))
00618     return false;   // Can't handle this.
00619 
00620   if (LoopExit) *LoopExit = LoopExitBB;
00621 
00622   // We already know that nothing uses any scalar values defined inside of this
00623   // loop.  As such, we just have to check to see if this loop will execute any
00624   // side-effecting instructions (e.g. stores, calls, volatile loads) in the
00625   // part of the loop that the code *would* execute.  We already checked the
00626   // tail, check the header now.
00627   for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
00628     if (I->mayHaveSideEffects())
00629       return false;
00630   return true;
00631 }
00632 
00633 /// UnswitchIfProfitable - We have found that we can unswitch currentLoop when
00634 /// LoopCond == Val to simplify the loop.  If we decide that this is profitable,
00635 /// unswitch the loop, reprocess the pieces, then return true.
00636 bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val) {
00637   Function *F = loopHeader->getParent();
00638   Constant *CondVal = 0;
00639   BasicBlock *ExitBlock = 0;
00640 
00641   if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) {
00642     // If the condition is trivial, always unswitch. There is no code growth
00643     // for this case.
00644     UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock);
00645     return true;
00646   }
00647 
00648   // Check to see if it would be profitable to unswitch current loop.
00649 
00650   // Do not do non-trivial unswitch while optimizing for size.
00651   if (OptimizeForSize ||
00652       F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
00653                                       Attribute::OptimizeForSize))
00654     return false;
00655 
00656   UnswitchNontrivialCondition(LoopCond, Val, currentLoop);
00657   return true;
00658 }
00659 
00660 /// CloneLoop - Recursively clone the specified loop and all of its children,
00661 /// mapping the blocks with the specified map.
00662 static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
00663                        LoopInfo *LI, LPPassManager *LPM) {
00664   Loop *New = new Loop();
00665   LPM->insertLoop(New, PL);
00666 
00667   // Add all of the blocks in L to the new loop.
00668   for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
00669        I != E; ++I)
00670     if (LI->getLoopFor(*I) == L)
00671       New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), LI->getBase());
00672 
00673   // Add all of the subloops to the new loop.
00674   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
00675     CloneLoop(*I, New, VM, LI, LPM);
00676 
00677   return New;
00678 }
00679 
00680 /// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values
00681 /// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest.  Insert the
00682 /// code immediately before InsertPt.
00683 void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
00684                                                   BasicBlock *TrueDest,
00685                                                   BasicBlock *FalseDest,
00686                                                   Instruction *InsertPt) {
00687   // Insert a conditional branch on LIC to the two preheaders.  The original
00688   // code is the true version and the new code is the false version.
00689   Value *BranchVal = LIC;
00690   if (!isa<ConstantInt>(Val) ||
00691       Val->getType() != Type::getInt1Ty(LIC->getContext()))
00692     BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val);
00693   else if (Val != ConstantInt::getTrue(Val->getContext()))
00694     // We want to enter the new loop when the condition is true.
00695     std::swap(TrueDest, FalseDest);
00696 
00697   // Insert the new branch.
00698   BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
00699 
00700   // If either edge is critical, split it. This helps preserve LoopSimplify
00701   // form for enclosing loops.
00702   SplitCriticalEdge(BI, 0, this, false, false, true);
00703   SplitCriticalEdge(BI, 1, this, false, false, true);
00704 }
00705 
00706 /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
00707 /// condition in it (a cond branch from its header block to its latch block,
00708 /// where the path through the loop that doesn't execute its body has no
00709 /// side-effects), unswitch it.  This doesn't involve any code duplication, just
00710 /// moving the conditional branch outside of the loop and updating loop info.
00711 void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond,
00712                                             Constant *Val,
00713                                             BasicBlock *ExitBlock) {
00714   DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %"
00715         << loopHeader->getName() << " [" << L->getBlocks().size()
00716         << " blocks] in Function " << L->getHeader()->getParent()->getName()
00717         << " on cond: " << *Val << " == " << *Cond << "\n");
00718 
00719   // First step, split the preheader, so that we know that there is a safe place
00720   // to insert the conditional branch.  We will change loopPreheader to have a
00721   // conditional branch on Cond.
00722   BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, this);
00723 
00724   // Now that we have a place to insert the conditional branch, create a place
00725   // to branch to: this is the exit block out of the loop that we should
00726   // short-circuit to.
00727 
00728   // Split this block now, so that the loop maintains its exit block, and so
00729   // that the jump from the preheader can execute the contents of the exit block
00730   // without actually branching to it (the exit block should be dominated by the
00731   // loop header, not the preheader).
00732   assert(!L->contains(ExitBlock) && "Exit block is in the loop?");
00733   BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin(), this);
00734 
00735   // Okay, now we have a position to branch from and a position to branch to,
00736   // insert the new conditional branch.
00737   EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH,
00738                                  loopPreheader->getTerminator());
00739   LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L);
00740   loopPreheader->getTerminator()->eraseFromParent();
00741 
00742   // We need to reprocess this loop, it could be unswitched again.
00743   redoLoop = true;
00744 
00745   // Now that we know that the loop is never entered when this condition is a
00746   // particular value, rewrite the loop with this info.  We know that this will
00747   // at least eliminate the old branch.
00748   RewriteLoopBodyWithConditionConstant(L, Cond, Val, false);
00749   ++NumTrivial;
00750 }
00751 
00752 /// SplitExitEdges - Split all of the edges from inside the loop to their exit
00753 /// blocks.  Update the appropriate Phi nodes as we do so.
00754 void LoopUnswitch::SplitExitEdges(Loop *L,
00755                                 const SmallVector<BasicBlock *, 8> &ExitBlocks){
00756 
00757   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00758     BasicBlock *ExitBlock = ExitBlocks[i];
00759     SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock),
00760                                        pred_end(ExitBlock));
00761 
00762     // Although SplitBlockPredecessors doesn't preserve loop-simplify in
00763     // general, if we call it on all predecessors of all exits then it does.
00764     if (!ExitBlock->isLandingPad()) {
00765       SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa", this);
00766     } else {
00767       SmallVector<BasicBlock*, 2> NewBBs;
00768       SplitLandingPadPredecessors(ExitBlock, Preds, ".us-lcssa", ".us-lcssa",
00769                                   this, NewBBs);
00770     }
00771   }
00772 }
00773 
00774 /// UnswitchNontrivialCondition - We determined that the loop is profitable
00775 /// to unswitch when LIC equal Val.  Split it into loop versions and test the
00776 /// condition outside of either loop.  Return the loops created as Out1/Out2.
00777 void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
00778                                                Loop *L) {
00779   Function *F = loopHeader->getParent();
00780   DEBUG(dbgs() << "loop-unswitch: Unswitching loop %"
00781         << loopHeader->getName() << " [" << L->getBlocks().size()
00782         << " blocks] in Function " << F->getName()
00783         << " when '" << *Val << "' == " << *LIC << "\n");
00784 
00785   if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
00786     SE->forgetLoop(L);
00787 
00788   LoopBlocks.clear();
00789   NewBlocks.clear();
00790 
00791   // First step, split the preheader and exit blocks, and add these blocks to
00792   // the LoopBlocks list.
00793   BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, this);
00794   LoopBlocks.push_back(NewPreheader);
00795 
00796   // We want the loop to come after the preheader, but before the exit blocks.
00797   LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
00798 
00799   SmallVector<BasicBlock*, 8> ExitBlocks;
00800   L->getUniqueExitBlocks(ExitBlocks);
00801 
00802   // Split all of the edges from inside the loop to their exit blocks.  Update
00803   // the appropriate Phi nodes as we do so.
00804   SplitExitEdges(L, ExitBlocks);
00805 
00806   // The exit blocks may have been changed due to edge splitting, recompute.
00807   ExitBlocks.clear();
00808   L->getUniqueExitBlocks(ExitBlocks);
00809 
00810   // Add exit blocks to the loop blocks.
00811   LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
00812 
00813   // Next step, clone all of the basic blocks that make up the loop (including
00814   // the loop preheader and exit blocks), keeping track of the mapping between
00815   // the instructions and blocks.
00816   NewBlocks.reserve(LoopBlocks.size());
00817   ValueToValueMapTy VMap;
00818   for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
00819     BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F);
00820 
00821     NewBlocks.push_back(NewBB);
00822     VMap[LoopBlocks[i]] = NewBB;  // Keep the BB mapping.
00823     LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L);
00824   }
00825 
00826   // Splice the newly inserted blocks into the function right before the
00827   // original preheader.
00828   F->getBasicBlockList().splice(NewPreheader, F->getBasicBlockList(),
00829                                 NewBlocks[0], F->end());
00830 
00831   // Now we create the new Loop object for the versioned loop.
00832   Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM);
00833 
00834   // Recalculate unswitching quota, inherit simplified switches info for NewBB,
00835   // Probably clone more loop-unswitch related loop properties.
00836   BranchesInfo.cloneData(NewLoop, L, VMap);
00837 
00838   Loop *ParentLoop = L->getParentLoop();
00839   if (ParentLoop) {
00840     // Make sure to add the cloned preheader and exit blocks to the parent loop
00841     // as well.
00842     ParentLoop->addBasicBlockToLoop(NewBlocks[0], LI->getBase());
00843   }
00844 
00845   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00846     BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]);
00847     // The new exit block should be in the same loop as the old one.
00848     if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i]))
00849       ExitBBLoop->addBasicBlockToLoop(NewExit, LI->getBase());
00850 
00851     assert(NewExit->getTerminator()->getNumSuccessors() == 1 &&
00852            "Exit block should have been split to have one successor!");
00853     BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0);
00854 
00855     // If the successor of the exit block had PHI nodes, add an entry for
00856     // NewExit.
00857     PHINode *PN;
00858     for (BasicBlock::iterator I = ExitSucc->begin(); isa<PHINode>(I); ++I) {
00859       PN = cast<PHINode>(I);
00860       Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]);
00861       ValueToValueMapTy::iterator It = VMap.find(V);
00862       if (It != VMap.end()) V = It->second;
00863       PN->addIncoming(V, NewExit);
00864     }
00865 
00866     if (LandingPadInst *LPad = NewExit->getLandingPadInst()) {
00867       PN = PHINode::Create(LPad->getType(), 0, "",
00868                            ExitSucc->getFirstInsertionPt());
00869 
00870       for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc);
00871            I != E; ++I) {
00872         BasicBlock *BB = *I;
00873         LandingPadInst *LPI = BB->getLandingPadInst();
00874         LPI->replaceAllUsesWith(PN);
00875         PN->addIncoming(LPI, BB);
00876       }
00877     }
00878   }
00879 
00880   // Rewrite the code to refer to itself.
00881   for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
00882     for (BasicBlock::iterator I = NewBlocks[i]->begin(),
00883            E = NewBlocks[i]->end(); I != E; ++I)
00884       RemapInstruction(I, VMap,RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
00885 
00886   // Rewrite the original preheader to select between versions of the loop.
00887   BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator());
00888   assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
00889          "Preheader splitting did not work correctly!");
00890 
00891   // Emit the new branch that selects between the two versions of this loop.
00892   EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR);
00893   LPM->deleteSimpleAnalysisValue(OldBR, L);
00894   OldBR->eraseFromParent();
00895 
00896   LoopProcessWorklist.push_back(NewLoop);
00897   redoLoop = true;
00898 
00899   // Keep a WeakVH holding onto LIC.  If the first call to RewriteLoopBody
00900   // deletes the instruction (for example by simplifying a PHI that feeds into
00901   // the condition that we're unswitching on), we don't rewrite the second
00902   // iteration.
00903   WeakVH LICHandle(LIC);
00904 
00905   // Now we rewrite the original code to know that the condition is true and the
00906   // new code to know that the condition is false.
00907   RewriteLoopBodyWithConditionConstant(L, LIC, Val, false);
00908 
00909   // It's possible that simplifying one loop could cause the other to be
00910   // changed to another value or a constant.  If its a constant, don't simplify
00911   // it.
00912   if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop &&
00913       LICHandle && !isa<Constant>(LICHandle))
00914     RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true);
00915 }
00916 
00917 /// RemoveFromWorklist - Remove all instances of I from the worklist vector
00918 /// specified.
00919 static void RemoveFromWorklist(Instruction *I,
00920                                std::vector<Instruction*> &Worklist) {
00921 
00922   Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I),
00923                  Worklist.end());
00924 }
00925 
00926 /// ReplaceUsesOfWith - When we find that I really equals V, remove I from the
00927 /// program, replacing all uses with V and update the worklist.
00928 static void ReplaceUsesOfWith(Instruction *I, Value *V,
00929                               std::vector<Instruction*> &Worklist,
00930                               Loop *L, LPPassManager *LPM) {
00931   DEBUG(dbgs() << "Replace with '" << *V << "': " << *I);
00932 
00933   // Add uses to the worklist, which may be dead now.
00934   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
00935     if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
00936       Worklist.push_back(Use);
00937 
00938   // Add users to the worklist which may be simplified now.
00939   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
00940        UI != E; ++UI)
00941     Worklist.push_back(cast<Instruction>(*UI));
00942   LPM->deleteSimpleAnalysisValue(I, L);
00943   RemoveFromWorklist(I, Worklist);
00944   I->replaceAllUsesWith(V);
00945   I->eraseFromParent();
00946   ++NumSimplify;
00947 }
00948 
00949 /// RemoveBlockIfDead - If the specified block is dead, remove it, update loop
00950 /// information, and remove any dead successors it has.
00951 ///
00952 void LoopUnswitch::RemoveBlockIfDead(BasicBlock *BB,
00953                                      std::vector<Instruction*> &Worklist,
00954                                      Loop *L) {
00955   if (pred_begin(BB) != pred_end(BB)) {
00956     // This block isn't dead, since an edge to BB was just removed, see if there
00957     // are any easy simplifications we can do now.
00958     if (BasicBlock *Pred = BB->getSinglePredecessor()) {
00959       // If it has one pred, fold phi nodes in BB.
00960       while (isa<PHINode>(BB->begin()))
00961         ReplaceUsesOfWith(BB->begin(),
00962                           cast<PHINode>(BB->begin())->getIncomingValue(0),
00963                           Worklist, L, LPM);
00964 
00965       // If this is the header of a loop and the only pred is the latch, we now
00966       // have an unreachable loop.
00967       if (Loop *L = LI->getLoopFor(BB))
00968         if (loopHeader == BB && L->contains(Pred)) {
00969           // Remove the branch from the latch to the header block, this makes
00970           // the header dead, which will make the latch dead (because the header
00971           // dominates the latch).
00972           LPM->deleteSimpleAnalysisValue(Pred->getTerminator(), L);
00973           Pred->getTerminator()->eraseFromParent();
00974           new UnreachableInst(BB->getContext(), Pred);
00975 
00976           // The loop is now broken, remove it from LI.
00977           RemoveLoopFromHierarchy(L);
00978 
00979           // Reprocess the header, which now IS dead.
00980           RemoveBlockIfDead(BB, Worklist, L);
00981           return;
00982         }
00983 
00984       // If pred ends in a uncond branch, add uncond branch to worklist so that
00985       // the two blocks will get merged.
00986       if (BranchInst *BI = dyn_cast<BranchInst>(Pred->getTerminator()))
00987         if (BI->isUnconditional())
00988           Worklist.push_back(BI);
00989     }
00990     return;
00991   }
00992 
00993   DEBUG(dbgs() << "Nuking dead block: " << *BB);
00994 
00995   // Remove the instructions in the basic block from the worklist.
00996   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
00997     RemoveFromWorklist(I, Worklist);
00998 
00999     // Anything that uses the instructions in this basic block should have their
01000     // uses replaced with undefs.
01001     // If I is not void type then replaceAllUsesWith undef.
01002     // This allows ValueHandlers and custom metadata to adjust itself.
01003     if (!I->getType()->isVoidTy())
01004       I->replaceAllUsesWith(UndefValue::get(I->getType()));
01005   }
01006 
01007   // If this is the edge to the header block for a loop, remove the loop and
01008   // promote all subloops.
01009   if (Loop *BBLoop = LI->getLoopFor(BB)) {
01010     if (BBLoop->getLoopLatch() == BB) {
01011       RemoveLoopFromHierarchy(BBLoop);
01012       if (currentLoop == BBLoop) {
01013         currentLoop = 0;
01014         redoLoop = false;
01015       }
01016     }
01017   }
01018 
01019   // Remove the block from the loop info, which removes it from any loops it
01020   // was in.
01021   LI->removeBlock(BB);
01022 
01023 
01024   // Remove phi node entries in successors for this block.
01025   TerminatorInst *TI = BB->getTerminator();
01026   SmallVector<BasicBlock*, 4> Succs;
01027   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
01028     Succs.push_back(TI->getSuccessor(i));
01029     TI->getSuccessor(i)->removePredecessor(BB);
01030   }
01031 
01032   // Unique the successors, remove anything with multiple uses.
01033   array_pod_sort(Succs.begin(), Succs.end());
01034   Succs.erase(std::unique(Succs.begin(), Succs.end()), Succs.end());
01035 
01036   // Remove the basic block, including all of the instructions contained in it.
01037   LPM->deleteSimpleAnalysisValue(BB, L);
01038   BB->eraseFromParent();
01039   // Remove successor blocks here that are not dead, so that we know we only
01040   // have dead blocks in this list.  Nondead blocks have a way of becoming dead,
01041   // then getting removed before we revisit them, which is badness.
01042   //
01043   for (unsigned i = 0; i != Succs.size(); ++i)
01044     if (pred_begin(Succs[i]) != pred_end(Succs[i])) {
01045       // One exception is loop headers.  If this block was the preheader for a
01046       // loop, then we DO want to visit the loop so the loop gets deleted.
01047       // We know that if the successor is a loop header, that this loop had to
01048       // be the preheader: the case where this was the latch block was handled
01049       // above and headers can only have two predecessors.
01050       if (!LI->isLoopHeader(Succs[i])) {
01051         Succs.erase(Succs.begin()+i);
01052         --i;
01053       }
01054     }
01055 
01056   for (unsigned i = 0, e = Succs.size(); i != e; ++i)
01057     RemoveBlockIfDead(Succs[i], Worklist, L);
01058 }
01059 
01060 /// RemoveLoopFromHierarchy - We have discovered that the specified loop has
01061 /// become unwrapped, either because the backedge was deleted, or because the
01062 /// edge into the header was removed.  If the edge into the header from the
01063 /// latch block was removed, the loop is unwrapped but subloops are still alive,
01064 /// so they just reparent loops.  If the loops are actually dead, they will be
01065 /// removed later.
01066 void LoopUnswitch::RemoveLoopFromHierarchy(Loop *L) {
01067   LPM->deleteLoopFromQueue(L);
01068   RemoveLoopFromWorklist(L);
01069 }
01070 
01071 // RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
01072 // the value specified by Val in the specified loop, or we know it does NOT have
01073 // that value.  Rewrite any uses of LIC or of properties correlated to it.
01074 void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
01075                                                         Constant *Val,
01076                                                         bool IsEqual) {
01077   assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
01078 
01079   // FIXME: Support correlated properties, like:
01080   //  for (...)
01081   //    if (li1 < li2)
01082   //      ...
01083   //    if (li1 > li2)
01084   //      ...
01085 
01086   // FOLD boolean conditions (X|LIC), (X&LIC).  Fold conditional branches,
01087   // selects, switches.
01088   std::vector<Instruction*> Worklist;
01089   LLVMContext &Context = Val->getContext();
01090 
01091 
01092   // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC
01093   // in the loop with the appropriate one directly.
01094   if (IsEqual || (isa<ConstantInt>(Val) &&
01095       Val->getType()->isIntegerTy(1))) {
01096     Value *Replacement;
01097     if (IsEqual)
01098       Replacement = Val;
01099     else
01100       Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()),
01101                                      !cast<ConstantInt>(Val)->getZExtValue());
01102 
01103     for (Value::use_iterator UI = LIC->use_begin(), E = LIC->use_end();
01104          UI != E; ++UI) {
01105       Instruction *U = dyn_cast<Instruction>(*UI);
01106       if (!U || !L->contains(U))
01107         continue;
01108       Worklist.push_back(U);
01109     }
01110 
01111     for (std::vector<Instruction*>::iterator UI = Worklist.begin();
01112          UI != Worklist.end(); ++UI)
01113       (*UI)->replaceUsesOfWith(LIC, Replacement);
01114 
01115     SimplifyCode(Worklist, L);
01116     return;
01117   }
01118 
01119   // Otherwise, we don't know the precise value of LIC, but we do know that it
01120   // is certainly NOT "Val".  As such, simplify any uses in the loop that we
01121   // can.  This case occurs when we unswitch switch statements.
01122   for (Value::use_iterator UI = LIC->use_begin(), E = LIC->use_end();
01123        UI != E; ++UI) {
01124     Instruction *U = dyn_cast<Instruction>(*UI);
01125     if (!U || !L->contains(U))
01126       continue;
01127 
01128     Worklist.push_back(U);
01129 
01130     // TODO: We could do other simplifications, for example, turning
01131     // 'icmp eq LIC, Val' -> false.
01132 
01133     // If we know that LIC is not Val, use this info to simplify code.
01134     SwitchInst *SI = dyn_cast<SwitchInst>(U);
01135     if (SI == 0 || !isa<ConstantInt>(Val)) continue;
01136 
01137     SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val));
01138     // Default case is live for multiple values.
01139     if (DeadCase == SI->case_default()) continue;
01140 
01141     // Found a dead case value.  Don't remove PHI nodes in the
01142     // successor if they become single-entry, those PHI nodes may
01143     // be in the Users list.
01144 
01145     BasicBlock *Switch = SI->getParent();
01146     BasicBlock *SISucc = DeadCase.getCaseSuccessor();
01147     BasicBlock *Latch = L->getLoopLatch();
01148 
01149     BranchesInfo.setUnswitched(SI, Val);
01150 
01151     if (!SI->findCaseDest(SISucc)) continue;  // Edge is critical.
01152     // If the DeadCase successor dominates the loop latch, then the
01153     // transformation isn't safe since it will delete the sole predecessor edge
01154     // to the latch.
01155     if (Latch && DT->dominates(SISucc, Latch))
01156       continue;
01157 
01158     // FIXME: This is a hack.  We need to keep the successor around
01159     // and hooked up so as to preserve the loop structure, because
01160     // trying to update it is complicated.  So instead we preserve the
01161     // loop structure and put the block on a dead code path.
01162     SplitEdge(Switch, SISucc, this);
01163     // Compute the successors instead of relying on the return value
01164     // of SplitEdge, since it may have split the switch successor
01165     // after PHI nodes.
01166     BasicBlock *NewSISucc = DeadCase.getCaseSuccessor();
01167     BasicBlock *OldSISucc = *succ_begin(NewSISucc);
01168     // Create an "unreachable" destination.
01169     BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
01170                                            Switch->getParent(),
01171                                            OldSISucc);
01172     new UnreachableInst(Context, Abort);
01173     // Force the new case destination to branch to the "unreachable"
01174     // block while maintaining a (dead) CFG edge to the old block.
01175     NewSISucc->getTerminator()->eraseFromParent();
01176     BranchInst::Create(Abort, OldSISucc,
01177                        ConstantInt::getTrue(Context), NewSISucc);
01178     // Release the PHI operands for this edge.
01179     for (BasicBlock::iterator II = NewSISucc->begin();
01180          PHINode *PN = dyn_cast<PHINode>(II); ++II)
01181       PN->setIncomingValue(PN->getBasicBlockIndex(Switch),
01182                            UndefValue::get(PN->getType()));
01183     // Tell the domtree about the new block. We don't fully update the
01184     // domtree here -- instead we force it to do a full recomputation
01185     // after the pass is complete -- but we do need to inform it of
01186     // new blocks.
01187     if (DT)
01188       DT->addNewBlock(Abort, NewSISucc);
01189   }
01190 
01191   SimplifyCode(Worklist, L);
01192 }
01193 
01194 /// SimplifyCode - Okay, now that we have simplified some instructions in the
01195 /// loop, walk over it and constant prop, dce, and fold control flow where
01196 /// possible.  Note that this is effectively a very simple loop-structure-aware
01197 /// optimizer.  During processing of this loop, L could very well be deleted, so
01198 /// it must not be used.
01199 ///
01200 /// FIXME: When the loop optimizer is more mature, separate this out to a new
01201 /// pass.
01202 ///
01203 void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
01204   while (!Worklist.empty()) {
01205     Instruction *I = Worklist.back();
01206     Worklist.pop_back();
01207 
01208     // Simple DCE.
01209     if (isInstructionTriviallyDead(I)) {
01210       DEBUG(dbgs() << "Remove dead instruction '" << *I);
01211 
01212       // Add uses to the worklist, which may be dead now.
01213       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
01214         if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
01215           Worklist.push_back(Use);
01216       LPM->deleteSimpleAnalysisValue(I, L);
01217       RemoveFromWorklist(I, Worklist);
01218       I->eraseFromParent();
01219       ++NumSimplify;
01220       continue;
01221     }
01222 
01223     // See if instruction simplification can hack this up.  This is common for
01224     // things like "select false, X, Y" after unswitching made the condition be
01225     // 'false'.  TODO: update the domtree properly so we can pass it here.
01226     if (Value *V = SimplifyInstruction(I))
01227       if (LI->replacementPreservesLCSSAForm(I, V)) {
01228         ReplaceUsesOfWith(I, V, Worklist, L, LPM);
01229         continue;
01230       }
01231 
01232     // Special case hacks that appear commonly in unswitched code.
01233     if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
01234       if (BI->isUnconditional()) {
01235         // If BI's parent is the only pred of the successor, fold the two blocks
01236         // together.
01237         BasicBlock *Pred = BI->getParent();
01238         BasicBlock *Succ = BI->getSuccessor(0);
01239         BasicBlock *SinglePred = Succ->getSinglePredecessor();
01240         if (!SinglePred) continue;  // Nothing to do.
01241         assert(SinglePred == Pred && "CFG broken");
01242 
01243         DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- "
01244               << Succ->getName() << "\n");
01245 
01246         // Resolve any single entry PHI nodes in Succ.
01247         while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
01248           ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
01249 
01250         // If Succ has any successors with PHI nodes, update them to have
01251         // entries coming from Pred instead of Succ.
01252         Succ->replaceAllUsesWith(Pred);
01253 
01254         // Move all of the successor contents from Succ to Pred.
01255         Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
01256                                    Succ->end());
01257         LPM->deleteSimpleAnalysisValue(BI, L);
01258         BI->eraseFromParent();
01259         RemoveFromWorklist(BI, Worklist);
01260 
01261         // Remove Succ from the loop tree.
01262         LI->removeBlock(Succ);
01263         LPM->deleteSimpleAnalysisValue(Succ, L);
01264         Succ->eraseFromParent();
01265         ++NumSimplify;
01266         continue;
01267       }
01268 
01269       if (ConstantInt *CB = dyn_cast<ConstantInt>(BI->getCondition())){
01270         // Conditional branch.  Turn it into an unconditional branch, then
01271         // remove dead blocks.
01272         continue;  // FIXME: Enable.
01273 
01274         DEBUG(dbgs() << "Folded branch: " << *BI);
01275         BasicBlock *DeadSucc = BI->getSuccessor(CB->getZExtValue());
01276         BasicBlock *LiveSucc = BI->getSuccessor(!CB->getZExtValue());
01277         DeadSucc->removePredecessor(BI->getParent(), true);
01278         Worklist.push_back(BranchInst::Create(LiveSucc, BI));
01279         LPM->deleteSimpleAnalysisValue(BI, L);
01280         BI->eraseFromParent();
01281         RemoveFromWorklist(BI, Worklist);
01282         ++NumSimplify;
01283 
01284         RemoveBlockIfDead(DeadSucc, Worklist, L);
01285       }
01286       continue;
01287     }
01288   }
01289 }