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