LLVM API Documentation

SimplifyCFG.cpp
Go to the documentation of this file.
00001 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
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 // Peephole optimize the CFG.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Transforms/Utils/Local.h"
00015 #include "llvm/ADT/DenseMap.h"
00016 #include "llvm/ADT/STLExtras.h"
00017 #include "llvm/ADT/SetVector.h"
00018 #include "llvm/ADT/SmallPtrSet.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/ADT/Statistic.h"
00021 #include "llvm/Analysis/ConstantFolding.h"
00022 #include "llvm/Analysis/InstructionSimplify.h"
00023 #include "llvm/Analysis/TargetTransformInfo.h"
00024 #include "llvm/Analysis/ValueTracking.h"
00025 #include "llvm/IR/CFG.h"
00026 #include "llvm/IR/ConstantRange.h"
00027 #include "llvm/IR/Constants.h"
00028 #include "llvm/IR/DataLayout.h"
00029 #include "llvm/IR/DerivedTypes.h"
00030 #include "llvm/IR/GlobalVariable.h"
00031 #include "llvm/IR/IRBuilder.h"
00032 #include "llvm/IR/Instructions.h"
00033 #include "llvm/IR/IntrinsicInst.h"
00034 #include "llvm/IR/LLVMContext.h"
00035 #include "llvm/IR/MDBuilder.h"
00036 #include "llvm/IR/Metadata.h"
00037 #include "llvm/IR/Module.h"
00038 #include "llvm/IR/NoFolder.h"
00039 #include "llvm/IR/Operator.h"
00040 #include "llvm/IR/PatternMatch.h"
00041 #include "llvm/IR/Type.h"
00042 #include "llvm/Support/CommandLine.h"
00043 #include "llvm/Support/Debug.h"
00044 #include "llvm/Support/raw_ostream.h"
00045 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00046 #include "llvm/Transforms/Utils/Local.h"
00047 #include <algorithm>
00048 #include <map>
00049 #include <set>
00050 using namespace llvm;
00051 using namespace PatternMatch;
00052 
00053 #define DEBUG_TYPE "simplifycfg"
00054 
00055 static cl::opt<unsigned>
00056 PHINodeFoldingThreshold("phi-node-folding-threshold", cl::Hidden, cl::init(1),
00057    cl::desc("Control the amount of phi node folding to perform (default = 1)"));
00058 
00059 static cl::opt<bool>
00060 DupRet("simplifycfg-dup-ret", cl::Hidden, cl::init(false),
00061        cl::desc("Duplicate return instructions into unconditional branches"));
00062 
00063 static cl::opt<bool>
00064 SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true),
00065        cl::desc("Sink common instructions down to the end block"));
00066 
00067 static cl::opt<bool> HoistCondStores(
00068     "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),
00069     cl::desc("Hoist conditional stores if an unconditional store precedes"));
00070 
00071 STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
00072 STATISTIC(NumLookupTables, "Number of switch instructions turned into lookup tables");
00073 STATISTIC(NumLookupTablesHoles, "Number of switch instructions turned into lookup tables (holes checked)");
00074 STATISTIC(NumSinkCommons, "Number of common instructions sunk down to the end block");
00075 STATISTIC(NumSpeculations, "Number of speculative executed instructions");
00076 
00077 namespace {
00078   /// ValueEqualityComparisonCase - Represents a case of a switch.
00079   struct ValueEqualityComparisonCase {
00080     ConstantInt *Value;
00081     BasicBlock *Dest;
00082 
00083     ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
00084       : Value(Value), Dest(Dest) {}
00085 
00086     bool operator<(ValueEqualityComparisonCase RHS) const {
00087       // Comparing pointers is ok as we only rely on the order for uniquing.
00088       return Value < RHS.Value;
00089     }
00090 
00091     bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; }
00092   };
00093 
00094 class SimplifyCFGOpt {
00095   const TargetTransformInfo &TTI;
00096   const DataLayout *const DL;
00097   AssumptionTracker *AT;
00098   Value *isValueEqualityComparison(TerminatorInst *TI);
00099   BasicBlock *GetValueEqualityComparisonCases(TerminatorInst *TI,
00100                                std::vector<ValueEqualityComparisonCase> &Cases);
00101   bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
00102                                                      BasicBlock *Pred,
00103                                                      IRBuilder<> &Builder);
00104   bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
00105                                            IRBuilder<> &Builder);
00106 
00107   bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
00108   bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
00109   bool SimplifyUnreachable(UnreachableInst *UI);
00110   bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
00111   bool SimplifyIndirectBr(IndirectBrInst *IBI);
00112   bool SimplifyUncondBranch(BranchInst *BI, IRBuilder <> &Builder);
00113   bool SimplifyCondBranch(BranchInst *BI, IRBuilder <>&Builder);
00114 
00115 public:
00116   SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout *DL,
00117                  AssumptionTracker *AT)
00118       : TTI(TTI), DL(DL), AT(AT) {}
00119   bool run(BasicBlock *BB);
00120 };
00121 }
00122 
00123 /// SafeToMergeTerminators - Return true if it is safe to merge these two
00124 /// terminator instructions together.
00125 ///
00126 static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
00127   if (SI1 == SI2) return false;  // Can't merge with self!
00128 
00129   // It is not safe to merge these two switch instructions if they have a common
00130   // successor, and if that successor has a PHI node, and if *that* PHI node has
00131   // conflicting incoming values from the two switch blocks.
00132   BasicBlock *SI1BB = SI1->getParent();
00133   BasicBlock *SI2BB = SI2->getParent();
00134   SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
00135 
00136   for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
00137     if (SI1Succs.count(*I))
00138       for (BasicBlock::iterator BBI = (*I)->begin();
00139            isa<PHINode>(BBI); ++BBI) {
00140         PHINode *PN = cast<PHINode>(BBI);
00141         if (PN->getIncomingValueForBlock(SI1BB) !=
00142             PN->getIncomingValueForBlock(SI2BB))
00143           return false;
00144       }
00145 
00146   return true;
00147 }
00148 
00149 /// isProfitableToFoldUnconditional - Return true if it is safe and profitable
00150 /// to merge these two terminator instructions together, where SI1 is an
00151 /// unconditional branch. PhiNodes will store all PHI nodes in common
00152 /// successors.
00153 ///
00154 static bool isProfitableToFoldUnconditional(BranchInst *SI1,
00155                                           BranchInst *SI2,
00156                                           Instruction *Cond,
00157                                           SmallVectorImpl<PHINode*> &PhiNodes) {
00158   if (SI1 == SI2) return false;  // Can't merge with self!
00159   assert(SI1->isUnconditional() && SI2->isConditional());
00160 
00161   // We fold the unconditional branch if we can easily update all PHI nodes in
00162   // common successors:
00163   // 1> We have a constant incoming value for the conditional branch;
00164   // 2> We have "Cond" as the incoming value for the unconditional branch;
00165   // 3> SI2->getCondition() and Cond have same operands.
00166   CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
00167   if (!Ci2) return false;
00168   if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
00169         Cond->getOperand(1) == Ci2->getOperand(1)) &&
00170       !(Cond->getOperand(0) == Ci2->getOperand(1) &&
00171         Cond->getOperand(1) == Ci2->getOperand(0)))
00172     return false;
00173 
00174   BasicBlock *SI1BB = SI1->getParent();
00175   BasicBlock *SI2BB = SI2->getParent();
00176   SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
00177   for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
00178     if (SI1Succs.count(*I))
00179       for (BasicBlock::iterator BBI = (*I)->begin();
00180            isa<PHINode>(BBI); ++BBI) {
00181         PHINode *PN = cast<PHINode>(BBI);
00182         if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
00183             !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
00184           return false;
00185         PhiNodes.push_back(PN);
00186       }
00187   return true;
00188 }
00189 
00190 /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
00191 /// now be entries in it from the 'NewPred' block.  The values that will be
00192 /// flowing into the PHI nodes will be the same as those coming in from
00193 /// ExistPred, an existing predecessor of Succ.
00194 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
00195                                   BasicBlock *ExistPred) {
00196   if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
00197 
00198   PHINode *PN;
00199   for (BasicBlock::iterator I = Succ->begin();
00200        (PN = dyn_cast<PHINode>(I)); ++I)
00201     PN->addIncoming(PN->getIncomingValueForBlock(ExistPred), NewPred);
00202 }
00203 
00204 /// ComputeSpeculationCost - Compute an abstract "cost" of speculating the
00205 /// given instruction, which is assumed to be safe to speculate. 1 means
00206 /// cheap, 2 means less cheap, and UINT_MAX means prohibitively expensive.
00207 static unsigned ComputeSpeculationCost(const User *I, const DataLayout *DL) {
00208   assert(isSafeToSpeculativelyExecute(I, DL) &&
00209          "Instruction is not safe to speculatively execute!");
00210   switch (Operator::getOpcode(I)) {
00211   default:
00212     // In doubt, be conservative.
00213     return UINT_MAX;
00214   case Instruction::GetElementPtr:
00215     // GEPs are cheap if all indices are constant.
00216     if (!cast<GEPOperator>(I)->hasAllConstantIndices())
00217       return UINT_MAX;
00218     return 1;
00219   case Instruction::ExtractValue:
00220   case Instruction::Load:
00221   case Instruction::Add:
00222   case Instruction::Sub:
00223   case Instruction::And:
00224   case Instruction::Or:
00225   case Instruction::Xor:
00226   case Instruction::Shl:
00227   case Instruction::LShr:
00228   case Instruction::AShr:
00229   case Instruction::ICmp:
00230   case Instruction::Trunc:
00231   case Instruction::ZExt:
00232   case Instruction::SExt:
00233   case Instruction::BitCast:
00234   case Instruction::ExtractElement:
00235   case Instruction::InsertElement:
00236     return 1; // These are all cheap.
00237 
00238   case Instruction::Call:
00239   case Instruction::Select:
00240     return 2;
00241   }
00242 }
00243 
00244 /// DominatesMergePoint - If we have a merge point of an "if condition" as
00245 /// accepted above, return true if the specified value dominates the block.  We
00246 /// don't handle the true generality of domination here, just a special case
00247 /// which works well enough for us.
00248 ///
00249 /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
00250 /// see if V (which must be an instruction) and its recursive operands
00251 /// that do not dominate BB have a combined cost lower than CostRemaining and
00252 /// are non-trapping.  If both are true, the instruction is inserted into the
00253 /// set and true is returned.
00254 ///
00255 /// The cost for most non-trapping instructions is defined as 1 except for
00256 /// Select whose cost is 2.
00257 ///
00258 /// After this function returns, CostRemaining is decreased by the cost of
00259 /// V plus its non-dominating operands.  If that cost is greater than
00260 /// CostRemaining, false is returned and CostRemaining is undefined.
00261 static bool DominatesMergePoint(Value *V, BasicBlock *BB,
00262                                 SmallPtrSetImpl<Instruction*> *AggressiveInsts,
00263                                 unsigned &CostRemaining,
00264                                 const DataLayout *DL) {
00265   Instruction *I = dyn_cast<Instruction>(V);
00266   if (!I) {
00267     // Non-instructions all dominate instructions, but not all constantexprs
00268     // can be executed unconditionally.
00269     if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
00270       if (C->canTrap())
00271         return false;
00272     return true;
00273   }
00274   BasicBlock *PBB = I->getParent();
00275 
00276   // We don't want to allow weird loops that might have the "if condition" in
00277   // the bottom of this block.
00278   if (PBB == BB) return false;
00279 
00280   // If this instruction is defined in a block that contains an unconditional
00281   // branch to BB, then it must be in the 'conditional' part of the "if
00282   // statement".  If not, it definitely dominates the region.
00283   BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
00284   if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB)
00285     return true;
00286 
00287   // If we aren't allowing aggressive promotion anymore, then don't consider
00288   // instructions in the 'if region'.
00289   if (!AggressiveInsts) return false;
00290 
00291   // If we have seen this instruction before, don't count it again.
00292   if (AggressiveInsts->count(I)) return true;
00293 
00294   // Okay, it looks like the instruction IS in the "condition".  Check to
00295   // see if it's a cheap instruction to unconditionally compute, and if it
00296   // only uses stuff defined outside of the condition.  If so, hoist it out.
00297   if (!isSafeToSpeculativelyExecute(I, DL))
00298     return false;
00299 
00300   unsigned Cost = ComputeSpeculationCost(I, DL);
00301 
00302   if (Cost > CostRemaining)
00303     return false;
00304 
00305   CostRemaining -= Cost;
00306 
00307   // Okay, we can only really hoist these out if their operands do
00308   // not take us over the cost threshold.
00309   for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
00310     if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining, DL))
00311       return false;
00312   // Okay, it's safe to do this!  Remember this instruction.
00313   AggressiveInsts->insert(I);
00314   return true;
00315 }
00316 
00317 /// GetConstantInt - Extract ConstantInt from value, looking through IntToPtr
00318 /// and PointerNullValue. Return NULL if value is not a constant int.
00319 static ConstantInt *GetConstantInt(Value *V, const DataLayout *DL) {
00320   // Normal constant int.
00321   ConstantInt *CI = dyn_cast<ConstantInt>(V);
00322   if (CI || !DL || !isa<Constant>(V) || !V->getType()->isPointerTy())
00323     return CI;
00324 
00325   // This is some kind of pointer constant. Turn it into a pointer-sized
00326   // ConstantInt if possible.
00327   IntegerType *PtrTy = cast<IntegerType>(DL->getIntPtrType(V->getType()));
00328 
00329   // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
00330   if (isa<ConstantPointerNull>(V))
00331     return ConstantInt::get(PtrTy, 0);
00332 
00333   // IntToPtr const int.
00334   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00335     if (CE->getOpcode() == Instruction::IntToPtr)
00336       if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
00337         // The constant is very likely to have the right type already.
00338         if (CI->getType() == PtrTy)
00339           return CI;
00340         else
00341           return cast<ConstantInt>
00342             (ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false));
00343       }
00344   return nullptr;
00345 }
00346 
00347 /// GatherConstantCompares - Given a potentially 'or'd or 'and'd together
00348 /// collection of icmp eq/ne instructions that compare a value against a
00349 /// constant, return the value being compared, and stick the constant into the
00350 /// Values vector.
00351 static Value *
00352 GatherConstantCompares(Value *V, std::vector<ConstantInt*> &Vals, Value *&Extra,
00353                        const DataLayout *DL, bool isEQ, unsigned &UsedICmps) {
00354   Instruction *I = dyn_cast<Instruction>(V);
00355   if (!I) return nullptr;
00356 
00357   // If this is an icmp against a constant, handle this as one of the cases.
00358   if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
00359     if (ConstantInt *C = GetConstantInt(I->getOperand(1), DL)) {
00360       Value *RHSVal;
00361       ConstantInt *RHSC;
00362 
00363       if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ:ICmpInst::ICMP_NE)) {
00364         // (x & ~2^x) == y --> x == y || x == y|2^x
00365         // This undoes a transformation done by instcombine to fuse 2 compares.
00366         if (match(ICI->getOperand(0),
00367                   m_And(m_Value(RHSVal), m_ConstantInt(RHSC)))) {
00368           APInt Not = ~RHSC->getValue();
00369           if (Not.isPowerOf2()) {
00370             Vals.push_back(C);
00371             Vals.push_back(
00372                 ConstantInt::get(C->getContext(), C->getValue() | Not));
00373             UsedICmps++;
00374             return RHSVal;
00375           }
00376         }
00377 
00378         UsedICmps++;
00379         Vals.push_back(C);
00380         return I->getOperand(0);
00381       }
00382 
00383       // If we have "x ult 3" comparison, for example, then we can add 0,1,2 to
00384       // the set.
00385       ConstantRange Span =
00386         ConstantRange::makeICmpRegion(ICI->getPredicate(), C->getValue());
00387 
00388       // Shift the range if the compare is fed by an add. This is the range
00389       // compare idiom as emitted by instcombine.
00390       bool hasAdd =
00391           match(I->getOperand(0), m_Add(m_Value(RHSVal), m_ConstantInt(RHSC)));
00392       if (hasAdd)
00393         Span = Span.subtract(RHSC->getValue());
00394 
00395       // If this is an and/!= check then we want to optimize "x ugt 2" into
00396       // x != 0 && x != 1.
00397       if (!isEQ)
00398         Span = Span.inverse();
00399 
00400       // If there are a ton of values, we don't want to make a ginormous switch.
00401       if (Span.getSetSize().ugt(8) || Span.isEmptySet())
00402         return nullptr;
00403 
00404       for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
00405         Vals.push_back(ConstantInt::get(V->getContext(), Tmp));
00406       UsedICmps++;
00407       return hasAdd ? RHSVal : I->getOperand(0);
00408     }
00409     return nullptr;
00410   }
00411 
00412   // Otherwise, we can only handle an | or &, depending on isEQ.
00413   if (I->getOpcode() != (isEQ ? Instruction::Or : Instruction::And))
00414     return nullptr;
00415 
00416   unsigned NumValsBeforeLHS = Vals.size();
00417   unsigned UsedICmpsBeforeLHS = UsedICmps;
00418   if (Value *LHS = GatherConstantCompares(I->getOperand(0), Vals, Extra, DL,
00419                                           isEQ, UsedICmps)) {
00420     unsigned NumVals = Vals.size();
00421     unsigned UsedICmpsBeforeRHS = UsedICmps;
00422     if (Value *RHS = GatherConstantCompares(I->getOperand(1), Vals, Extra, DL,
00423                                             isEQ, UsedICmps)) {
00424       if (LHS == RHS)
00425         return LHS;
00426       Vals.resize(NumVals);
00427       UsedICmps = UsedICmpsBeforeRHS;
00428     }
00429 
00430     // The RHS of the or/and can't be folded in and we haven't used "Extra" yet,
00431     // set it and return success.
00432     if (Extra == nullptr || Extra == I->getOperand(1)) {
00433       Extra = I->getOperand(1);
00434       return LHS;
00435     }
00436 
00437     Vals.resize(NumValsBeforeLHS);
00438     UsedICmps = UsedICmpsBeforeLHS;
00439     return nullptr;
00440   }
00441 
00442   // If the LHS can't be folded in, but Extra is available and RHS can, try to
00443   // use LHS as Extra.
00444   if (Extra == nullptr || Extra == I->getOperand(0)) {
00445     Value *OldExtra = Extra;
00446     Extra = I->getOperand(0);
00447     if (Value *RHS = GatherConstantCompares(I->getOperand(1), Vals, Extra, DL,
00448                                             isEQ, UsedICmps))
00449       return RHS;
00450     assert(Vals.size() == NumValsBeforeLHS);
00451     Extra = OldExtra;
00452   }
00453 
00454   return nullptr;
00455 }
00456 
00457 static void EraseTerminatorInstAndDCECond(TerminatorInst *TI) {
00458   Instruction *Cond = nullptr;
00459   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
00460     Cond = dyn_cast<Instruction>(SI->getCondition());
00461   } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
00462     if (BI->isConditional())
00463       Cond = dyn_cast<Instruction>(BI->getCondition());
00464   } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
00465     Cond = dyn_cast<Instruction>(IBI->getAddress());
00466   }
00467 
00468   TI->eraseFromParent();
00469   if (Cond) RecursivelyDeleteTriviallyDeadInstructions(Cond);
00470 }
00471 
00472 /// isValueEqualityComparison - Return true if the specified terminator checks
00473 /// to see if a value is equal to constant integer value.
00474 Value *SimplifyCFGOpt::isValueEqualityComparison(TerminatorInst *TI) {
00475   Value *CV = nullptr;
00476   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
00477     // Do not permit merging of large switch instructions into their
00478     // predecessors unless there is only one predecessor.
00479     if (SI->getNumSuccessors()*std::distance(pred_begin(SI->getParent()),
00480                                              pred_end(SI->getParent())) <= 128)
00481       CV = SI->getCondition();
00482   } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
00483     if (BI->isConditional() && BI->getCondition()->hasOneUse())
00484       if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
00485         if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
00486           CV = ICI->getOperand(0);
00487 
00488   // Unwrap any lossless ptrtoint cast.
00489   if (DL && CV) {
00490     if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
00491       Value *Ptr = PTII->getPointerOperand();
00492       if (PTII->getType() == DL->getIntPtrType(Ptr->getType()))
00493         CV = Ptr;
00494     }
00495   }
00496   return CV;
00497 }
00498 
00499 /// GetValueEqualityComparisonCases - Given a value comparison instruction,
00500 /// decode all of the 'cases' that it represents and return the 'default' block.
00501 BasicBlock *SimplifyCFGOpt::
00502 GetValueEqualityComparisonCases(TerminatorInst *TI,
00503                                 std::vector<ValueEqualityComparisonCase>
00504                                                                        &Cases) {
00505   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
00506     Cases.reserve(SI->getNumCases());
00507     for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
00508       Cases.push_back(ValueEqualityComparisonCase(i.getCaseValue(),
00509                                                   i.getCaseSuccessor()));
00510     return SI->getDefaultDest();
00511   }
00512 
00513   BranchInst *BI = cast<BranchInst>(TI);
00514   ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
00515   BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
00516   Cases.push_back(ValueEqualityComparisonCase(GetConstantInt(ICI->getOperand(1),
00517                                                              DL),
00518                                               Succ));
00519   return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
00520 }
00521 
00522 
00523 /// EliminateBlockCases - Given a vector of bb/value pairs, remove any entries
00524 /// in the list that match the specified block.
00525 static void EliminateBlockCases(BasicBlock *BB,
00526                               std::vector<ValueEqualityComparisonCase> &Cases) {
00527   Cases.erase(std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
00528 }
00529 
00530 /// ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as
00531 /// well.
00532 static bool
00533 ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
00534               std::vector<ValueEqualityComparisonCase > &C2) {
00535   std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
00536 
00537   // Make V1 be smaller than V2.
00538   if (V1->size() > V2->size())
00539     std::swap(V1, V2);
00540 
00541   if (V1->size() == 0) return false;
00542   if (V1->size() == 1) {
00543     // Just scan V2.
00544     ConstantInt *TheVal = (*V1)[0].Value;
00545     for (unsigned i = 0, e = V2->size(); i != e; ++i)
00546       if (TheVal == (*V2)[i].Value)
00547         return true;
00548   }
00549 
00550   // Otherwise, just sort both lists and compare element by element.
00551   array_pod_sort(V1->begin(), V1->end());
00552   array_pod_sort(V2->begin(), V2->end());
00553   unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
00554   while (i1 != e1 && i2 != e2) {
00555     if ((*V1)[i1].Value == (*V2)[i2].Value)
00556       return true;
00557     if ((*V1)[i1].Value < (*V2)[i2].Value)
00558       ++i1;
00559     else
00560       ++i2;
00561   }
00562   return false;
00563 }
00564 
00565 /// SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a
00566 /// terminator instruction and its block is known to only have a single
00567 /// predecessor block, check to see if that predecessor is also a value
00568 /// comparison with the same value, and if that comparison determines the
00569 /// outcome of this comparison.  If so, simplify TI.  This does a very limited
00570 /// form of jump threading.
00571 bool SimplifyCFGOpt::
00572 SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
00573                                               BasicBlock *Pred,
00574                                               IRBuilder<> &Builder) {
00575   Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
00576   if (!PredVal) return false;  // Not a value comparison in predecessor.
00577 
00578   Value *ThisVal = isValueEqualityComparison(TI);
00579   assert(ThisVal && "This isn't a value comparison!!");
00580   if (ThisVal != PredVal) return false;  // Different predicates.
00581 
00582   // TODO: Preserve branch weight metadata, similarly to how
00583   // FoldValueComparisonIntoPredecessors preserves it.
00584 
00585   // Find out information about when control will move from Pred to TI's block.
00586   std::vector<ValueEqualityComparisonCase> PredCases;
00587   BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(),
00588                                                         PredCases);
00589   EliminateBlockCases(PredDef, PredCases);  // Remove default from cases.
00590 
00591   // Find information about how control leaves this block.
00592   std::vector<ValueEqualityComparisonCase> ThisCases;
00593   BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
00594   EliminateBlockCases(ThisDef, ThisCases);  // Remove default from cases.
00595 
00596   // If TI's block is the default block from Pred's comparison, potentially
00597   // simplify TI based on this knowledge.
00598   if (PredDef == TI->getParent()) {
00599     // If we are here, we know that the value is none of those cases listed in
00600     // PredCases.  If there are any cases in ThisCases that are in PredCases, we
00601     // can simplify TI.
00602     if (!ValuesOverlap(PredCases, ThisCases))
00603       return false;
00604 
00605     if (isa<BranchInst>(TI)) {
00606       // Okay, one of the successors of this condbr is dead.  Convert it to a
00607       // uncond br.
00608       assert(ThisCases.size() == 1 && "Branch can only have one case!");
00609       // Insert the new branch.
00610       Instruction *NI = Builder.CreateBr(ThisDef);
00611       (void) NI;
00612 
00613       // Remove PHI node entries for the dead edge.
00614       ThisCases[0].Dest->removePredecessor(TI->getParent());
00615 
00616       DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
00617            << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
00618 
00619       EraseTerminatorInstAndDCECond(TI);
00620       return true;
00621     }
00622 
00623     SwitchInst *SI = cast<SwitchInst>(TI);
00624     // Okay, TI has cases that are statically dead, prune them away.
00625     SmallPtrSet<Constant*, 16> DeadCases;
00626     for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
00627       DeadCases.insert(PredCases[i].Value);
00628 
00629     DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
00630                  << "Through successor TI: " << *TI);
00631 
00632     // Collect branch weights into a vector.
00633     SmallVector<uint32_t, 8> Weights;
00634     MDNode* MD = SI->getMetadata(LLVMContext::MD_prof);
00635     bool HasWeight = MD && (MD->getNumOperands() == 2 + SI->getNumCases());
00636     if (HasWeight)
00637       for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
00638            ++MD_i) {
00639         ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i));
00640         assert(CI);
00641         Weights.push_back(CI->getValue().getZExtValue());
00642       }
00643     for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
00644       --i;
00645       if (DeadCases.count(i.getCaseValue())) {
00646         if (HasWeight) {
00647           std::swap(Weights[i.getCaseIndex()+1], Weights.back());
00648           Weights.pop_back();
00649         }
00650         i.getCaseSuccessor()->removePredecessor(TI->getParent());
00651         SI->removeCase(i);
00652       }
00653     }
00654     if (HasWeight && Weights.size() >= 2)
00655       SI->setMetadata(LLVMContext::MD_prof,
00656                       MDBuilder(SI->getParent()->getContext()).
00657                       createBranchWeights(Weights));
00658 
00659     DEBUG(dbgs() << "Leaving: " << *TI << "\n");
00660     return true;
00661   }
00662 
00663   // Otherwise, TI's block must correspond to some matched value.  Find out
00664   // which value (or set of values) this is.
00665   ConstantInt *TIV = nullptr;
00666   BasicBlock *TIBB = TI->getParent();
00667   for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
00668     if (PredCases[i].Dest == TIBB) {
00669       if (TIV)
00670         return false;  // Cannot handle multiple values coming to this block.
00671       TIV = PredCases[i].Value;
00672     }
00673   assert(TIV && "No edge from pred to succ?");
00674 
00675   // Okay, we found the one constant that our value can be if we get into TI's
00676   // BB.  Find out which successor will unconditionally be branched to.
00677   BasicBlock *TheRealDest = nullptr;
00678   for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
00679     if (ThisCases[i].Value == TIV) {
00680       TheRealDest = ThisCases[i].Dest;
00681       break;
00682     }
00683 
00684   // If not handled by any explicit cases, it is handled by the default case.
00685   if (!TheRealDest) TheRealDest = ThisDef;
00686 
00687   // Remove PHI node entries for dead edges.
00688   BasicBlock *CheckEdge = TheRealDest;
00689   for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
00690     if (*SI != CheckEdge)
00691       (*SI)->removePredecessor(TIBB);
00692     else
00693       CheckEdge = nullptr;
00694 
00695   // Insert the new branch.
00696   Instruction *NI = Builder.CreateBr(TheRealDest);
00697   (void) NI;
00698 
00699   DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
00700             << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
00701 
00702   EraseTerminatorInstAndDCECond(TI);
00703   return true;
00704 }
00705 
00706 namespace {
00707   /// ConstantIntOrdering - This class implements a stable ordering of constant
00708   /// integers that does not depend on their address.  This is important for
00709   /// applications that sort ConstantInt's to ensure uniqueness.
00710   struct ConstantIntOrdering {
00711     bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
00712       return LHS->getValue().ult(RHS->getValue());
00713     }
00714   };
00715 }
00716 
00717 static int ConstantIntSortPredicate(ConstantInt *const *P1,
00718                                     ConstantInt *const *P2) {
00719   const ConstantInt *LHS = *P1;
00720   const ConstantInt *RHS = *P2;
00721   if (LHS->getValue().ult(RHS->getValue()))
00722     return 1;
00723   if (LHS->getValue() == RHS->getValue())
00724     return 0;
00725   return -1;
00726 }
00727 
00728 static inline bool HasBranchWeights(const Instruction* I) {
00729   MDNode* ProfMD = I->getMetadata(LLVMContext::MD_prof);
00730   if (ProfMD && ProfMD->getOperand(0))
00731     if (MDString* MDS = dyn_cast<MDString>(ProfMD->getOperand(0)))
00732       return MDS->getString().equals("branch_weights");
00733 
00734   return false;
00735 }
00736 
00737 /// Get Weights of a given TerminatorInst, the default weight is at the front
00738 /// of the vector. If TI is a conditional eq, we need to swap the branch-weight
00739 /// metadata.
00740 static void GetBranchWeights(TerminatorInst *TI,
00741                              SmallVectorImpl<uint64_t> &Weights) {
00742   MDNode* MD = TI->getMetadata(LLVMContext::MD_prof);
00743   assert(MD);
00744   for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
00745     ConstantInt *CI = cast<ConstantInt>(MD->getOperand(i));
00746     Weights.push_back(CI->getValue().getZExtValue());
00747   }
00748 
00749   // If TI is a conditional eq, the default case is the false case,
00750   // and the corresponding branch-weight data is at index 2. We swap the
00751   // default weight to be the first entry.
00752   if (BranchInst* BI = dyn_cast<BranchInst>(TI)) {
00753     assert(Weights.size() == 2);
00754     ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
00755     if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
00756       std::swap(Weights.front(), Weights.back());
00757   }
00758 }
00759 
00760 /// Keep halving the weights until all can fit in uint32_t.
00761 static void FitWeights(MutableArrayRef<uint64_t> Weights) {
00762   uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
00763   if (Max > UINT_MAX) {
00764     unsigned Offset = 32 - countLeadingZeros(Max);
00765     for (uint64_t &I : Weights)
00766       I >>= Offset;
00767   }
00768 }
00769 
00770 /// FoldValueComparisonIntoPredecessors - The specified terminator is a value
00771 /// equality comparison instruction (either a switch or a branch on "X == c").
00772 /// See if any of the predecessors of the terminator block are value comparisons
00773 /// on the same value.  If so, and if safe to do so, fold them together.
00774 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
00775                                                          IRBuilder<> &Builder) {
00776   BasicBlock *BB = TI->getParent();
00777   Value *CV = isValueEqualityComparison(TI);  // CondVal
00778   assert(CV && "Not a comparison?");
00779   bool Changed = false;
00780 
00781   SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));
00782   while (!Preds.empty()) {
00783     BasicBlock *Pred = Preds.pop_back_val();
00784 
00785     // See if the predecessor is a comparison with the same value.
00786     TerminatorInst *PTI = Pred->getTerminator();
00787     Value *PCV = isValueEqualityComparison(PTI);  // PredCondVal
00788 
00789     if (PCV == CV && SafeToMergeTerminators(TI, PTI)) {
00790       // Figure out which 'cases' to copy from SI to PSI.
00791       std::vector<ValueEqualityComparisonCase> BBCases;
00792       BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
00793 
00794       std::vector<ValueEqualityComparisonCase> PredCases;
00795       BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
00796 
00797       // Based on whether the default edge from PTI goes to BB or not, fill in
00798       // PredCases and PredDefault with the new switch cases we would like to
00799       // build.
00800       SmallVector<BasicBlock*, 8> NewSuccessors;
00801 
00802       // Update the branch weight metadata along the way
00803       SmallVector<uint64_t, 8> Weights;
00804       bool PredHasWeights = HasBranchWeights(PTI);
00805       bool SuccHasWeights = HasBranchWeights(TI);
00806 
00807       if (PredHasWeights) {
00808         GetBranchWeights(PTI, Weights);
00809         // branch-weight metadata is inconsistent here.
00810         if (Weights.size() != 1 + PredCases.size())
00811           PredHasWeights = SuccHasWeights = false;
00812       } else if (SuccHasWeights)
00813         // If there are no predecessor weights but there are successor weights,
00814         // populate Weights with 1, which will later be scaled to the sum of
00815         // successor's weights
00816         Weights.assign(1 + PredCases.size(), 1);
00817 
00818       SmallVector<uint64_t, 8> SuccWeights;
00819       if (SuccHasWeights) {
00820         GetBranchWeights(TI, SuccWeights);
00821         // branch-weight metadata is inconsistent here.
00822         if (SuccWeights.size() != 1 + BBCases.size())
00823           PredHasWeights = SuccHasWeights = false;
00824       } else if (PredHasWeights)
00825         SuccWeights.assign(1 + BBCases.size(), 1);
00826 
00827       if (PredDefault == BB) {
00828         // If this is the default destination from PTI, only the edges in TI
00829         // that don't occur in PTI, or that branch to BB will be activated.
00830         std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
00831         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
00832           if (PredCases[i].Dest != BB)
00833             PTIHandled.insert(PredCases[i].Value);
00834           else {
00835             // The default destination is BB, we don't need explicit targets.
00836             std::swap(PredCases[i], PredCases.back());
00837 
00838             if (PredHasWeights || SuccHasWeights) {
00839               // Increase weight for the default case.
00840               Weights[0] += Weights[i+1];
00841               std::swap(Weights[i+1], Weights.back());
00842               Weights.pop_back();
00843             }
00844 
00845             PredCases.pop_back();
00846             --i; --e;
00847           }
00848 
00849         // Reconstruct the new switch statement we will be building.
00850         if (PredDefault != BBDefault) {
00851           PredDefault->removePredecessor(Pred);
00852           PredDefault = BBDefault;
00853           NewSuccessors.push_back(BBDefault);
00854         }
00855 
00856         unsigned CasesFromPred = Weights.size();
00857         uint64_t ValidTotalSuccWeight = 0;
00858         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
00859           if (!PTIHandled.count(BBCases[i].Value) &&
00860               BBCases[i].Dest != BBDefault) {
00861             PredCases.push_back(BBCases[i]);
00862             NewSuccessors.push_back(BBCases[i].Dest);
00863             if (SuccHasWeights || PredHasWeights) {
00864               // The default weight is at index 0, so weight for the ith case
00865               // should be at index i+1. Scale the cases from successor by
00866               // PredDefaultWeight (Weights[0]).
00867               Weights.push_back(Weights[0] * SuccWeights[i+1]);
00868               ValidTotalSuccWeight += SuccWeights[i+1];
00869             }
00870           }
00871 
00872         if (SuccHasWeights || PredHasWeights) {
00873           ValidTotalSuccWeight += SuccWeights[0];
00874           // Scale the cases from predecessor by ValidTotalSuccWeight.
00875           for (unsigned i = 1; i < CasesFromPred; ++i)
00876             Weights[i] *= ValidTotalSuccWeight;
00877           // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
00878           Weights[0] *= SuccWeights[0];
00879         }
00880       } else {
00881         // If this is not the default destination from PSI, only the edges
00882         // in SI that occur in PSI with a destination of BB will be
00883         // activated.
00884         std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
00885         std::map<ConstantInt*, uint64_t> WeightsForHandled;
00886         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
00887           if (PredCases[i].Dest == BB) {
00888             PTIHandled.insert(PredCases[i].Value);
00889 
00890             if (PredHasWeights || SuccHasWeights) {
00891               WeightsForHandled[PredCases[i].Value] = Weights[i+1];
00892               std::swap(Weights[i+1], Weights.back());
00893               Weights.pop_back();
00894             }
00895 
00896             std::swap(PredCases[i], PredCases.back());
00897             PredCases.pop_back();
00898             --i; --e;
00899           }
00900 
00901         // Okay, now we know which constants were sent to BB from the
00902         // predecessor.  Figure out where they will all go now.
00903         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
00904           if (PTIHandled.count(BBCases[i].Value)) {
00905             // If this is one we are capable of getting...
00906             if (PredHasWeights || SuccHasWeights)
00907               Weights.push_back(WeightsForHandled[BBCases[i].Value]);
00908             PredCases.push_back(BBCases[i]);
00909             NewSuccessors.push_back(BBCases[i].Dest);
00910             PTIHandled.erase(BBCases[i].Value);// This constant is taken care of
00911           }
00912 
00913         // If there are any constants vectored to BB that TI doesn't handle,
00914         // they must go to the default destination of TI.
00915         for (std::set<ConstantInt*, ConstantIntOrdering>::iterator I =
00916                                     PTIHandled.begin(),
00917                E = PTIHandled.end(); I != E; ++I) {
00918           if (PredHasWeights || SuccHasWeights)
00919             Weights.push_back(WeightsForHandled[*I]);
00920           PredCases.push_back(ValueEqualityComparisonCase(*I, BBDefault));
00921           NewSuccessors.push_back(BBDefault);
00922         }
00923       }
00924 
00925       // Okay, at this point, we know which new successor Pred will get.  Make
00926       // sure we update the number of entries in the PHI nodes for these
00927       // successors.
00928       for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i)
00929         AddPredecessorToBlock(NewSuccessors[i], Pred, BB);
00930 
00931       Builder.SetInsertPoint(PTI);
00932       // Convert pointer to int before we switch.
00933       if (CV->getType()->isPointerTy()) {
00934         assert(DL && "Cannot switch on pointer without DataLayout");
00935         CV = Builder.CreatePtrToInt(CV, DL->getIntPtrType(CV->getType()),
00936                                     "magicptr");
00937       }
00938 
00939       // Now that the successors are updated, create the new Switch instruction.
00940       SwitchInst *NewSI = Builder.CreateSwitch(CV, PredDefault,
00941                                                PredCases.size());
00942       NewSI->setDebugLoc(PTI->getDebugLoc());
00943       for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
00944         NewSI->addCase(PredCases[i].Value, PredCases[i].Dest);
00945 
00946       if (PredHasWeights || SuccHasWeights) {
00947         // Halve the weights if any of them cannot fit in an uint32_t
00948         FitWeights(Weights);
00949 
00950         SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
00951 
00952         NewSI->setMetadata(LLVMContext::MD_prof,
00953                            MDBuilder(BB->getContext()).
00954                            createBranchWeights(MDWeights));
00955       }
00956 
00957       EraseTerminatorInstAndDCECond(PTI);
00958 
00959       // Okay, last check.  If BB is still a successor of PSI, then we must
00960       // have an infinite loop case.  If so, add an infinitely looping block
00961       // to handle the case to preserve the behavior of the code.
00962       BasicBlock *InfLoopBlock = nullptr;
00963       for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
00964         if (NewSI->getSuccessor(i) == BB) {
00965           if (!InfLoopBlock) {
00966             // Insert it at the end of the function, because it's either code,
00967             // or it won't matter if it's hot. :)
00968             InfLoopBlock = BasicBlock::Create(BB->getContext(),
00969                                               "infloop", BB->getParent());
00970             BranchInst::Create(InfLoopBlock, InfLoopBlock);
00971           }
00972           NewSI->setSuccessor(i, InfLoopBlock);
00973         }
00974 
00975       Changed = true;
00976     }
00977   }
00978   return Changed;
00979 }
00980 
00981 // isSafeToHoistInvoke - If we would need to insert a select that uses the
00982 // value of this invoke (comments in HoistThenElseCodeToIf explain why we
00983 // would need to do this), we can't hoist the invoke, as there is nowhere
00984 // to put the select in this case.
00985 static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
00986                                 Instruction *I1, Instruction *I2) {
00987   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
00988     PHINode *PN;
00989     for (BasicBlock::iterator BBI = SI->begin();
00990          (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
00991       Value *BB1V = PN->getIncomingValueForBlock(BB1);
00992       Value *BB2V = PN->getIncomingValueForBlock(BB2);
00993       if (BB1V != BB2V && (BB1V==I1 || BB2V==I2)) {
00994         return false;
00995       }
00996     }
00997   }
00998   return true;
00999 }
01000 
01001 /// HoistThenElseCodeToIf - Given a conditional branch that goes to BB1 and
01002 /// BB2, hoist any common code in the two blocks up into the branch block.  The
01003 /// caller of this function guarantees that BI's block dominates BB1 and BB2.
01004 static bool HoistThenElseCodeToIf(BranchInst *BI, const DataLayout *DL) {
01005   // This does very trivial matching, with limited scanning, to find identical
01006   // instructions in the two blocks.  In particular, we don't want to get into
01007   // O(M*N) situations here where M and N are the sizes of BB1 and BB2.  As
01008   // such, we currently just scan for obviously identical instructions in an
01009   // identical order.
01010   BasicBlock *BB1 = BI->getSuccessor(0);  // The true destination.
01011   BasicBlock *BB2 = BI->getSuccessor(1);  // The false destination
01012 
01013   BasicBlock::iterator BB1_Itr = BB1->begin();
01014   BasicBlock::iterator BB2_Itr = BB2->begin();
01015 
01016   Instruction *I1 = BB1_Itr++, *I2 = BB2_Itr++;
01017   // Skip debug info if it is not identical.
01018   DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
01019   DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
01020   if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
01021     while (isa<DbgInfoIntrinsic>(I1))
01022       I1 = BB1_Itr++;
01023     while (isa<DbgInfoIntrinsic>(I2))
01024       I2 = BB2_Itr++;
01025   }
01026   if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) ||
01027       (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
01028     return false;
01029 
01030   BasicBlock *BIParent = BI->getParent();
01031 
01032   bool Changed = false;
01033   do {
01034     // If we are hoisting the terminator instruction, don't move one (making a
01035     // broken BB), instead clone it, and remove BI.
01036     if (isa<TerminatorInst>(I1))
01037       goto HoistTerminator;
01038 
01039     // For a normal instruction, we just move one to right before the branch,
01040     // then replace all uses of the other with the first.  Finally, we remove
01041     // the now redundant second instruction.
01042     BIParent->getInstList().splice(BI, BB1->getInstList(), I1);
01043     if (!I2->use_empty())
01044       I2->replaceAllUsesWith(I1);
01045     I1->intersectOptionalDataWith(I2);
01046     unsigned KnownIDs[] = {
01047       LLVMContext::MD_tbaa,
01048       LLVMContext::MD_range,
01049       LLVMContext::MD_fpmath,
01050       LLVMContext::MD_invariant_load
01051     };
01052     combineMetadata(I1, I2, KnownIDs);
01053     I2->eraseFromParent();
01054     Changed = true;
01055 
01056     I1 = BB1_Itr++;
01057     I2 = BB2_Itr++;
01058     // Skip debug info if it is not identical.
01059     DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
01060     DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
01061     if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
01062       while (isa<DbgInfoIntrinsic>(I1))
01063         I1 = BB1_Itr++;
01064       while (isa<DbgInfoIntrinsic>(I2))
01065         I2 = BB2_Itr++;
01066     }
01067   } while (I1->isIdenticalToWhenDefined(I2));
01068 
01069   return true;
01070 
01071 HoistTerminator:
01072   // It may not be possible to hoist an invoke.
01073   if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
01074     return Changed;
01075 
01076   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
01077     PHINode *PN;
01078     for (BasicBlock::iterator BBI = SI->begin();
01079          (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
01080       Value *BB1V = PN->getIncomingValueForBlock(BB1);
01081       Value *BB2V = PN->getIncomingValueForBlock(BB2);
01082       if (BB1V == BB2V)
01083         continue;
01084 
01085       if (isa<ConstantExpr>(BB1V) && !isSafeToSpeculativelyExecute(BB1V, DL))
01086         return Changed;
01087       if (isa<ConstantExpr>(BB2V) && !isSafeToSpeculativelyExecute(BB2V, DL))
01088         return Changed;
01089     }
01090   }
01091 
01092   // Okay, it is safe to hoist the terminator.
01093   Instruction *NT = I1->clone();
01094   BIParent->getInstList().insert(BI, NT);
01095   if (!NT->getType()->isVoidTy()) {
01096     I1->replaceAllUsesWith(NT);
01097     I2->replaceAllUsesWith(NT);
01098     NT->takeName(I1);
01099   }
01100 
01101   IRBuilder<true, NoFolder> Builder(NT);
01102   // Hoisting one of the terminators from our successor is a great thing.
01103   // Unfortunately, the successors of the if/else blocks may have PHI nodes in
01104   // them.  If they do, all PHI entries for BB1/BB2 must agree for all PHI
01105   // nodes, so we insert select instruction to compute the final result.
01106   std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects;
01107   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
01108     PHINode *PN;
01109     for (BasicBlock::iterator BBI = SI->begin();
01110          (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
01111       Value *BB1V = PN->getIncomingValueForBlock(BB1);
01112       Value *BB2V = PN->getIncomingValueForBlock(BB2);
01113       if (BB1V == BB2V) continue;
01114 
01115       // These values do not agree.  Insert a select instruction before NT
01116       // that determines the right value.
01117       SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
01118       if (!SI)
01119         SI = cast<SelectInst>
01120           (Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
01121                                 BB1V->getName()+"."+BB2V->getName()));
01122 
01123       // Make the PHI node use the select for all incoming values for BB1/BB2
01124       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
01125         if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
01126           PN->setIncomingValue(i, SI);
01127     }
01128   }
01129 
01130   // Update any PHI nodes in our new successors.
01131   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
01132     AddPredecessorToBlock(*SI, BIParent, BB1);
01133 
01134   EraseTerminatorInstAndDCECond(BI);
01135   return true;
01136 }
01137 
01138 /// SinkThenElseCodeToEnd - Given an unconditional branch that goes to BBEnd,
01139 /// check whether BBEnd has only two predecessors and the other predecessor
01140 /// ends with an unconditional branch. If it is true, sink any common code
01141 /// in the two predecessors to BBEnd.
01142 static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
01143   assert(BI1->isUnconditional());
01144   BasicBlock *BB1 = BI1->getParent();
01145   BasicBlock *BBEnd = BI1->getSuccessor(0);
01146 
01147   // Check that BBEnd has two predecessors and the other predecessor ends with
01148   // an unconditional branch.
01149   pred_iterator PI = pred_begin(BBEnd), PE = pred_end(BBEnd);
01150   BasicBlock *Pred0 = *PI++;
01151   if (PI == PE) // Only one predecessor.
01152     return false;
01153   BasicBlock *Pred1 = *PI++;
01154   if (PI != PE) // More than two predecessors.
01155     return false;
01156   BasicBlock *BB2 = (Pred0 == BB1) ? Pred1 : Pred0;
01157   BranchInst *BI2 = dyn_cast<BranchInst>(BB2->getTerminator());
01158   if (!BI2 || !BI2->isUnconditional())
01159     return false;
01160 
01161   // Gather the PHI nodes in BBEnd.
01162   std::map<Value*, std::pair<Value*, PHINode*> > MapValueFromBB1ToBB2;
01163   Instruction *FirstNonPhiInBBEnd = nullptr;
01164   for (BasicBlock::iterator I = BBEnd->begin(), E = BBEnd->end();
01165        I != E; ++I) {
01166     if (PHINode *PN = dyn_cast<PHINode>(I)) {
01167       Value *BB1V = PN->getIncomingValueForBlock(BB1);
01168       Value *BB2V = PN->getIncomingValueForBlock(BB2);
01169       MapValueFromBB1ToBB2[BB1V] = std::make_pair(BB2V, PN);
01170     } else {
01171       FirstNonPhiInBBEnd = &*I;
01172       break;
01173     }
01174   }
01175   if (!FirstNonPhiInBBEnd)
01176     return false;
01177 
01178 
01179   // This does very trivial matching, with limited scanning, to find identical
01180   // instructions in the two blocks.  We scan backward for obviously identical
01181   // instructions in an identical order.
01182   BasicBlock::InstListType::reverse_iterator RI1 = BB1->getInstList().rbegin(),
01183       RE1 = BB1->getInstList().rend(), RI2 = BB2->getInstList().rbegin(),
01184       RE2 = BB2->getInstList().rend();
01185   // Skip debug info.
01186   while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1;
01187   if (RI1 == RE1)
01188     return false;
01189   while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2;
01190   if (RI2 == RE2)
01191     return false;
01192   // Skip the unconditional branches.
01193   ++RI1;
01194   ++RI2;
01195 
01196   bool Changed = false;
01197   while (RI1 != RE1 && RI2 != RE2) {
01198     // Skip debug info.
01199     while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1;
01200     if (RI1 == RE1)
01201       return Changed;
01202     while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2;
01203     if (RI2 == RE2)
01204       return Changed;
01205 
01206     Instruction *I1 = &*RI1, *I2 = &*RI2;
01207     // I1 and I2 should have a single use in the same PHI node, and they
01208     // perform the same operation.
01209     // Cannot move control-flow-involving, volatile loads, vaarg, etc.
01210     if (isa<PHINode>(I1) || isa<PHINode>(I2) ||
01211         isa<TerminatorInst>(I1) || isa<TerminatorInst>(I2) ||
01212         isa<LandingPadInst>(I1) || isa<LandingPadInst>(I2) ||
01213         isa<AllocaInst>(I1) || isa<AllocaInst>(I2) ||
01214         I1->mayHaveSideEffects() || I2->mayHaveSideEffects() ||
01215         I1->mayReadOrWriteMemory() || I2->mayReadOrWriteMemory() ||
01216         !I1->hasOneUse() || !I2->hasOneUse() ||
01217         MapValueFromBB1ToBB2.find(I1) == MapValueFromBB1ToBB2.end() ||
01218         MapValueFromBB1ToBB2[I1].first != I2)
01219       return Changed;
01220 
01221     // Check whether we should swap the operands of ICmpInst.
01222     ICmpInst *ICmp1 = dyn_cast<ICmpInst>(I1), *ICmp2 = dyn_cast<ICmpInst>(I2);
01223     bool SwapOpnds = false;
01224     if (ICmp1 && ICmp2 &&
01225         ICmp1->getOperand(0) != ICmp2->getOperand(0) &&
01226         ICmp1->getOperand(1) != ICmp2->getOperand(1) &&
01227         (ICmp1->getOperand(0) == ICmp2->getOperand(1) ||
01228          ICmp1->getOperand(1) == ICmp2->getOperand(0))) {
01229       ICmp2->swapOperands();
01230       SwapOpnds = true;
01231     }
01232     if (!I1->isSameOperationAs(I2)) {
01233       if (SwapOpnds)
01234         ICmp2->swapOperands();
01235       return Changed;
01236     }
01237 
01238     // The operands should be either the same or they need to be generated
01239     // with a PHI node after sinking. We only handle the case where there is
01240     // a single pair of different operands.
01241     Value *DifferentOp1 = nullptr, *DifferentOp2 = nullptr;
01242     unsigned Op1Idx = 0;
01243     for (unsigned I = 0, E = I1->getNumOperands(); I != E; ++I) {
01244       if (I1->getOperand(I) == I2->getOperand(I))
01245         continue;
01246       // Early exit if we have more-than one pair of different operands or
01247       // the different operand is already in MapValueFromBB1ToBB2.
01248       // Early exit if we need a PHI node to replace a constant.
01249       if (DifferentOp1 ||
01250           MapValueFromBB1ToBB2.find(I1->getOperand(I)) !=
01251           MapValueFromBB1ToBB2.end() ||
01252           isa<Constant>(I1->getOperand(I)) ||
01253           isa<Constant>(I2->getOperand(I))) {
01254         // If we can't sink the instructions, undo the swapping.
01255         if (SwapOpnds)
01256           ICmp2->swapOperands();
01257         return Changed;
01258       }
01259       DifferentOp1 = I1->getOperand(I);
01260       Op1Idx = I;
01261       DifferentOp2 = I2->getOperand(I);
01262     }
01263 
01264     // We insert the pair of different operands to MapValueFromBB1ToBB2 and
01265     // remove (I1, I2) from MapValueFromBB1ToBB2.
01266     if (DifferentOp1) {
01267       PHINode *NewPN = PHINode::Create(DifferentOp1->getType(), 2,
01268                                        DifferentOp1->getName() + ".sink",
01269                                        BBEnd->begin());
01270       MapValueFromBB1ToBB2[DifferentOp1] = std::make_pair(DifferentOp2, NewPN);
01271       // I1 should use NewPN instead of DifferentOp1.
01272       I1->setOperand(Op1Idx, NewPN);
01273       NewPN->addIncoming(DifferentOp1, BB1);
01274       NewPN->addIncoming(DifferentOp2, BB2);
01275       DEBUG(dbgs() << "Create PHI node " << *NewPN << "\n";);
01276     }
01277     PHINode *OldPN = MapValueFromBB1ToBB2[I1].second;
01278     MapValueFromBB1ToBB2.erase(I1);
01279 
01280     DEBUG(dbgs() << "SINK common instructions " << *I1 << "\n";);
01281     DEBUG(dbgs() << "                         " << *I2 << "\n";);
01282     // We need to update RE1 and RE2 if we are going to sink the first
01283     // instruction in the basic block down.
01284     bool UpdateRE1 = (I1 == BB1->begin()), UpdateRE2 = (I2 == BB2->begin());
01285     // Sink the instruction.
01286     BBEnd->getInstList().splice(FirstNonPhiInBBEnd, BB1->getInstList(), I1);
01287     if (!OldPN->use_empty())
01288       OldPN->replaceAllUsesWith(I1);
01289     OldPN->eraseFromParent();
01290 
01291     if (!I2->use_empty())
01292       I2->replaceAllUsesWith(I1);
01293     I1->intersectOptionalDataWith(I2);
01294     I2->eraseFromParent();
01295 
01296     if (UpdateRE1)
01297       RE1 = BB1->getInstList().rend();
01298     if (UpdateRE2)
01299       RE2 = BB2->getInstList().rend();
01300     FirstNonPhiInBBEnd = I1;
01301     NumSinkCommons++;
01302     Changed = true;
01303   }
01304   return Changed;
01305 }
01306 
01307 /// \brief Determine if we can hoist sink a sole store instruction out of a
01308 /// conditional block.
01309 ///
01310 /// We are looking for code like the following:
01311 ///   BrBB:
01312 ///     store i32 %add, i32* %arrayidx2
01313 ///     ... // No other stores or function calls (we could be calling a memory
01314 ///     ... // function).
01315 ///     %cmp = icmp ult %x, %y
01316 ///     br i1 %cmp, label %EndBB, label %ThenBB
01317 ///   ThenBB:
01318 ///     store i32 %add5, i32* %arrayidx2
01319 ///     br label EndBB
01320 ///   EndBB:
01321 ///     ...
01322 ///   We are going to transform this into:
01323 ///   BrBB:
01324 ///     store i32 %add, i32* %arrayidx2
01325 ///     ... //
01326 ///     %cmp = icmp ult %x, %y
01327 ///     %add.add5 = select i1 %cmp, i32 %add, %add5
01328 ///     store i32 %add.add5, i32* %arrayidx2
01329 ///     ...
01330 ///
01331 /// \return The pointer to the value of the previous store if the store can be
01332 ///         hoisted into the predecessor block. 0 otherwise.
01333 static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
01334                                      BasicBlock *StoreBB, BasicBlock *EndBB) {
01335   StoreInst *StoreToHoist = dyn_cast<StoreInst>(I);
01336   if (!StoreToHoist)
01337     return nullptr;
01338 
01339   // Volatile or atomic.
01340   if (!StoreToHoist->isSimple())
01341     return nullptr;
01342 
01343   Value *StorePtr = StoreToHoist->getPointerOperand();
01344 
01345   // Look for a store to the same pointer in BrBB.
01346   unsigned MaxNumInstToLookAt = 10;
01347   for (BasicBlock::reverse_iterator RI = BrBB->rbegin(),
01348        RE = BrBB->rend(); RI != RE && (--MaxNumInstToLookAt); ++RI) {
01349     Instruction *CurI = &*RI;
01350 
01351     // Could be calling an instruction that effects memory like free().
01352     if (CurI->mayHaveSideEffects() && !isa<StoreInst>(CurI))
01353       return nullptr;
01354 
01355     StoreInst *SI = dyn_cast<StoreInst>(CurI);
01356     // Found the previous store make sure it stores to the same location.
01357     if (SI && SI->getPointerOperand() == StorePtr)
01358       // Found the previous store, return its value operand.
01359       return SI->getValueOperand();
01360     else if (SI)
01361       return nullptr; // Unknown store.
01362   }
01363 
01364   return nullptr;
01365 }
01366 
01367 /// \brief Speculate a conditional basic block flattening the CFG.
01368 ///
01369 /// Note that this is a very risky transform currently. Speculating
01370 /// instructions like this is most often not desirable. Instead, there is an MI
01371 /// pass which can do it with full awareness of the resource constraints.
01372 /// However, some cases are "obvious" and we should do directly. An example of
01373 /// this is speculating a single, reasonably cheap instruction.
01374 ///
01375 /// There is only one distinct advantage to flattening the CFG at the IR level:
01376 /// it makes very common but simplistic optimizations such as are common in
01377 /// instcombine and the DAG combiner more powerful by removing CFG edges and
01378 /// modeling their effects with easier to reason about SSA value graphs.
01379 ///
01380 ///
01381 /// An illustration of this transform is turning this IR:
01382 /// \code
01383 ///   BB:
01384 ///     %cmp = icmp ult %x, %y
01385 ///     br i1 %cmp, label %EndBB, label %ThenBB
01386 ///   ThenBB:
01387 ///     %sub = sub %x, %y
01388 ///     br label BB2
01389 ///   EndBB:
01390 ///     %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]
01391 ///     ...
01392 /// \endcode
01393 ///
01394 /// Into this IR:
01395 /// \code
01396 ///   BB:
01397 ///     %cmp = icmp ult %x, %y
01398 ///     %sub = sub %x, %y
01399 ///     %cond = select i1 %cmp, 0, %sub
01400 ///     ...
01401 /// \endcode
01402 ///
01403 /// \returns true if the conditional block is removed.
01404 static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
01405                                    const DataLayout *DL) {
01406   // Be conservative for now. FP select instruction can often be expensive.
01407   Value *BrCond = BI->getCondition();
01408   if (isa<FCmpInst>(BrCond))
01409     return false;
01410 
01411   BasicBlock *BB = BI->getParent();
01412   BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
01413 
01414   // If ThenBB is actually on the false edge of the conditional branch, remember
01415   // to swap the select operands later.
01416   bool Invert = false;
01417   if (ThenBB != BI->getSuccessor(0)) {
01418     assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
01419     Invert = true;
01420   }
01421   assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
01422 
01423   // Keep a count of how many times instructions are used within CondBB when
01424   // they are candidates for sinking into CondBB. Specifically:
01425   // - They are defined in BB, and
01426   // - They have no side effects, and
01427   // - All of their uses are in CondBB.
01428   SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
01429 
01430   unsigned SpeculationCost = 0;
01431   Value *SpeculatedStoreValue = nullptr;
01432   StoreInst *SpeculatedStore = nullptr;
01433   for (BasicBlock::iterator BBI = ThenBB->begin(),
01434                             BBE = std::prev(ThenBB->end());
01435        BBI != BBE; ++BBI) {
01436     Instruction *I = BBI;
01437     // Skip debug info.
01438     if (isa<DbgInfoIntrinsic>(I))
01439       continue;
01440 
01441     // Only speculatively execution a single instruction (not counting the
01442     // terminator) for now.
01443     ++SpeculationCost;
01444     if (SpeculationCost > 1)
01445       return false;
01446 
01447     // Don't hoist the instruction if it's unsafe or expensive.
01448     if (!isSafeToSpeculativelyExecute(I, DL) &&
01449         !(HoistCondStores &&
01450           (SpeculatedStoreValue = isSafeToSpeculateStore(I, BB, ThenBB,
01451                                                          EndBB))))
01452       return false;
01453     if (!SpeculatedStoreValue &&
01454         ComputeSpeculationCost(I, DL) > PHINodeFoldingThreshold)
01455       return false;
01456 
01457     // Store the store speculation candidate.
01458     if (SpeculatedStoreValue)
01459       SpeculatedStore = cast<StoreInst>(I);
01460 
01461     // Do not hoist the instruction if any of its operands are defined but not
01462     // used in BB. The transformation will prevent the operand from
01463     // being sunk into the use block.
01464     for (User::op_iterator i = I->op_begin(), e = I->op_end();
01465          i != e; ++i) {
01466       Instruction *OpI = dyn_cast<Instruction>(*i);
01467       if (!OpI || OpI->getParent() != BB ||
01468           OpI->mayHaveSideEffects())
01469         continue; // Not a candidate for sinking.
01470 
01471       ++SinkCandidateUseCounts[OpI];
01472     }
01473   }
01474 
01475   // Consider any sink candidates which are only used in CondBB as costs for
01476   // speculation. Note, while we iterate over a DenseMap here, we are summing
01477   // and so iteration order isn't significant.
01478   for (SmallDenseMap<Instruction *, unsigned, 4>::iterator I =
01479            SinkCandidateUseCounts.begin(), E = SinkCandidateUseCounts.end();
01480        I != E; ++I)
01481     if (I->first->getNumUses() == I->second) {
01482       ++SpeculationCost;
01483       if (SpeculationCost > 1)
01484         return false;
01485     }
01486 
01487   // Check that the PHI nodes can be converted to selects.
01488   bool HaveRewritablePHIs = false;
01489   for (BasicBlock::iterator I = EndBB->begin();
01490        PHINode *PN = dyn_cast<PHINode>(I); ++I) {
01491     Value *OrigV = PN->getIncomingValueForBlock(BB);
01492     Value *ThenV = PN->getIncomingValueForBlock(ThenBB);
01493 
01494     // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf.
01495     // Skip PHIs which are trivial.
01496     if (ThenV == OrigV)
01497       continue;
01498 
01499     HaveRewritablePHIs = true;
01500     ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV);
01501     ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV);
01502     if (!OrigCE && !ThenCE)
01503       continue; // Known safe and cheap.
01504 
01505     if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE, DL)) ||
01506         (OrigCE && !isSafeToSpeculativelyExecute(OrigCE, DL)))
01507       return false;
01508     unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, DL) : 0;
01509     unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, DL) : 0;
01510     if (OrigCost + ThenCost > 2 * PHINodeFoldingThreshold)
01511       return false;
01512 
01513     // Account for the cost of an unfolded ConstantExpr which could end up
01514     // getting expanded into Instructions.
01515     // FIXME: This doesn't account for how many operations are combined in the
01516     // constant expression.
01517     ++SpeculationCost;
01518     if (SpeculationCost > 1)
01519       return false;
01520   }
01521 
01522   // If there are no PHIs to process, bail early. This helps ensure idempotence
01523   // as well.
01524   if (!HaveRewritablePHIs && !(HoistCondStores && SpeculatedStoreValue))
01525     return false;
01526 
01527   // If we get here, we can hoist the instruction and if-convert.
01528   DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
01529 
01530   // Insert a select of the value of the speculated store.
01531   if (SpeculatedStoreValue) {
01532     IRBuilder<true, NoFolder> Builder(BI);
01533     Value *TrueV = SpeculatedStore->getValueOperand();
01534     Value *FalseV = SpeculatedStoreValue;
01535     if (Invert)
01536       std::swap(TrueV, FalseV);
01537     Value *S = Builder.CreateSelect(BrCond, TrueV, FalseV, TrueV->getName() +
01538                                     "." + FalseV->getName());
01539     SpeculatedStore->setOperand(0, S);
01540   }
01541 
01542   // Hoist the instructions.
01543   BB->getInstList().splice(BI, ThenBB->getInstList(), ThenBB->begin(),
01544                            std::prev(ThenBB->end()));
01545 
01546   // Insert selects and rewrite the PHI operands.
01547   IRBuilder<true, NoFolder> Builder(BI);
01548   for (BasicBlock::iterator I = EndBB->begin();
01549        PHINode *PN = dyn_cast<PHINode>(I); ++I) {
01550     unsigned OrigI = PN->getBasicBlockIndex(BB);
01551     unsigned ThenI = PN->getBasicBlockIndex(ThenBB);
01552     Value *OrigV = PN->getIncomingValue(OrigI);
01553     Value *ThenV = PN->getIncomingValue(ThenI);
01554 
01555     // Skip PHIs which are trivial.
01556     if (OrigV == ThenV)
01557       continue;
01558 
01559     // Create a select whose true value is the speculatively executed value and
01560     // false value is the preexisting value. Swap them if the branch
01561     // destinations were inverted.
01562     Value *TrueV = ThenV, *FalseV = OrigV;
01563     if (Invert)
01564       std::swap(TrueV, FalseV);
01565     Value *V = Builder.CreateSelect(BrCond, TrueV, FalseV,
01566                                     TrueV->getName() + "." + FalseV->getName());
01567     PN->setIncomingValue(OrigI, V);
01568     PN->setIncomingValue(ThenI, V);
01569   }
01570 
01571   ++NumSpeculations;
01572   return true;
01573 }
01574 
01575 /// \returns True if this block contains a CallInst with the NoDuplicate
01576 /// attribute.
01577 static bool HasNoDuplicateCall(const BasicBlock *BB) {
01578   for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
01579     const CallInst *CI = dyn_cast<CallInst>(I);
01580     if (!CI)
01581       continue;
01582     if (CI->cannotDuplicate())
01583       return true;
01584   }
01585   return false;
01586 }
01587 
01588 /// BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch
01589 /// across this block.
01590 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
01591   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
01592   unsigned Size = 0;
01593 
01594   for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
01595     if (isa<DbgInfoIntrinsic>(BBI))
01596       continue;
01597     if (Size > 10) return false;  // Don't clone large BB's.
01598     ++Size;
01599 
01600     // We can only support instructions that do not define values that are
01601     // live outside of the current basic block.
01602     for (User *U : BBI->users()) {
01603       Instruction *UI = cast<Instruction>(U);
01604       if (UI->getParent() != BB || isa<PHINode>(UI)) return false;
01605     }
01606 
01607     // Looks ok, continue checking.
01608   }
01609 
01610   return true;
01611 }
01612 
01613 /// FoldCondBranchOnPHI - If we have a conditional branch on a PHI node value
01614 /// that is defined in the same block as the branch and if any PHI entries are
01615 /// constants, thread edges corresponding to that entry to be branches to their
01616 /// ultimate destination.
01617 static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout *DL) {
01618   BasicBlock *BB = BI->getParent();
01619   PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
01620   // NOTE: we currently cannot transform this case if the PHI node is used
01621   // outside of the block.
01622   if (!PN || PN->getParent() != BB || !PN->hasOneUse())
01623     return false;
01624 
01625   // Degenerate case of a single entry PHI.
01626   if (PN->getNumIncomingValues() == 1) {
01627     FoldSingleEntryPHINodes(PN->getParent());
01628     return true;
01629   }
01630 
01631   // Now we know that this block has multiple preds and two succs.
01632   if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false;
01633 
01634   if (HasNoDuplicateCall(BB)) return false;
01635 
01636   // Okay, this is a simple enough basic block.  See if any phi values are
01637   // constants.
01638   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
01639     ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
01640     if (!CB || !CB->getType()->isIntegerTy(1)) continue;
01641 
01642     // Okay, we now know that all edges from PredBB should be revectored to
01643     // branch to RealDest.
01644     BasicBlock *PredBB = PN->getIncomingBlock(i);
01645     BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
01646 
01647     if (RealDest == BB) continue;  // Skip self loops.
01648     // Skip if the predecessor's terminator is an indirect branch.
01649     if (isa<IndirectBrInst>(PredBB->getTerminator())) continue;
01650 
01651     // The dest block might have PHI nodes, other predecessors and other
01652     // difficult cases.  Instead of being smart about this, just insert a new
01653     // block that jumps to the destination block, effectively splitting
01654     // the edge we are about to create.
01655     BasicBlock *EdgeBB = BasicBlock::Create(BB->getContext(),
01656                                             RealDest->getName()+".critedge",
01657                                             RealDest->getParent(), RealDest);
01658     BranchInst::Create(RealDest, EdgeBB);
01659 
01660     // Update PHI nodes.
01661     AddPredecessorToBlock(RealDest, EdgeBB, BB);
01662 
01663     // BB may have instructions that are being threaded over.  Clone these
01664     // instructions into EdgeBB.  We know that there will be no uses of the
01665     // cloned instructions outside of EdgeBB.
01666     BasicBlock::iterator InsertPt = EdgeBB->begin();
01667     DenseMap<Value*, Value*> TranslateMap;  // Track translated values.
01668     for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
01669       if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
01670         TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
01671         continue;
01672       }
01673       // Clone the instruction.
01674       Instruction *N = BBI->clone();
01675       if (BBI->hasName()) N->setName(BBI->getName()+".c");
01676 
01677       // Update operands due to translation.
01678       for (User::op_iterator i = N->op_begin(), e = N->op_end();
01679            i != e; ++i) {
01680         DenseMap<Value*, Value*>::iterator PI = TranslateMap.find(*i);
01681         if (PI != TranslateMap.end())
01682           *i = PI->second;
01683       }
01684 
01685       // Check for trivial simplification.
01686       if (Value *V = SimplifyInstruction(N, DL)) {
01687         TranslateMap[BBI] = V;
01688         delete N;   // Instruction folded away, don't need actual inst
01689       } else {
01690         // Insert the new instruction into its new home.
01691         EdgeBB->getInstList().insert(InsertPt, N);
01692         if (!BBI->use_empty())
01693           TranslateMap[BBI] = N;
01694       }
01695     }
01696 
01697     // Loop over all of the edges from PredBB to BB, changing them to branch
01698     // to EdgeBB instead.
01699     TerminatorInst *PredBBTI = PredBB->getTerminator();
01700     for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
01701       if (PredBBTI->getSuccessor(i) == BB) {
01702         BB->removePredecessor(PredBB);
01703         PredBBTI->setSuccessor(i, EdgeBB);
01704       }
01705 
01706     // Recurse, simplifying any other constants.
01707     return FoldCondBranchOnPHI(BI, DL) | true;
01708   }
01709 
01710   return false;
01711 }
01712 
01713 /// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
01714 /// PHI node, see if we can eliminate it.
01715 static bool FoldTwoEntryPHINode(PHINode *PN, const DataLayout *DL) {
01716   // Ok, this is a two entry PHI node.  Check to see if this is a simple "if
01717   // statement", which has a very simple dominance structure.  Basically, we
01718   // are trying to find the condition that is being branched on, which
01719   // subsequently causes this merge to happen.  We really want control
01720   // dependence information for this check, but simplifycfg can't keep it up
01721   // to date, and this catches most of the cases we care about anyway.
01722   BasicBlock *BB = PN->getParent();
01723   BasicBlock *IfTrue, *IfFalse;
01724   Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
01725   if (!IfCond ||
01726       // Don't bother if the branch will be constant folded trivially.
01727       isa<ConstantInt>(IfCond))
01728     return false;
01729 
01730   // Okay, we found that we can merge this two-entry phi node into a select.
01731   // Doing so would require us to fold *all* two entry phi nodes in this block.
01732   // At some point this becomes non-profitable (particularly if the target
01733   // doesn't support cmov's).  Only do this transformation if there are two or
01734   // fewer PHI nodes in this block.
01735   unsigned NumPhis = 0;
01736   for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
01737     if (NumPhis > 2)
01738       return false;
01739 
01740   // Loop over the PHI's seeing if we can promote them all to select
01741   // instructions.  While we are at it, keep track of the instructions
01742   // that need to be moved to the dominating block.
01743   SmallPtrSet<Instruction*, 4> AggressiveInsts;
01744   unsigned MaxCostVal0 = PHINodeFoldingThreshold,
01745            MaxCostVal1 = PHINodeFoldingThreshold;
01746 
01747   for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
01748     PHINode *PN = cast<PHINode>(II++);
01749     if (Value *V = SimplifyInstruction(PN, DL)) {
01750       PN->replaceAllUsesWith(V);
01751       PN->eraseFromParent();
01752       continue;
01753     }
01754 
01755     if (!DominatesMergePoint(PN->getIncomingValue(0), BB, &AggressiveInsts,
01756                              MaxCostVal0, DL) ||
01757         !DominatesMergePoint(PN->getIncomingValue(1), BB, &AggressiveInsts,
01758                              MaxCostVal1, DL))
01759       return false;
01760   }
01761 
01762   // If we folded the first phi, PN dangles at this point.  Refresh it.  If
01763   // we ran out of PHIs then we simplified them all.
01764   PN = dyn_cast<PHINode>(BB->begin());
01765   if (!PN) return true;
01766 
01767   // Don't fold i1 branches on PHIs which contain binary operators.  These can
01768   // often be turned into switches and other things.
01769   if (PN->getType()->isIntegerTy(1) &&
01770       (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
01771        isa<BinaryOperator>(PN->getIncomingValue(1)) ||
01772        isa<BinaryOperator>(IfCond)))
01773     return false;
01774 
01775   // If we all PHI nodes are promotable, check to make sure that all
01776   // instructions in the predecessor blocks can be promoted as well.  If
01777   // not, we won't be able to get rid of the control flow, so it's not
01778   // worth promoting to select instructions.
01779   BasicBlock *DomBlock = nullptr;
01780   BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
01781   BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
01782   if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
01783     IfBlock1 = nullptr;
01784   } else {
01785     DomBlock = *pred_begin(IfBlock1);
01786     for (BasicBlock::iterator I = IfBlock1->begin();!isa<TerminatorInst>(I);++I)
01787       if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) {
01788         // This is not an aggressive instruction that we can promote.
01789         // Because of this, we won't be able to get rid of the control
01790         // flow, so the xform is not worth it.
01791         return false;
01792       }
01793   }
01794 
01795   if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
01796     IfBlock2 = nullptr;
01797   } else {
01798     DomBlock = *pred_begin(IfBlock2);
01799     for (BasicBlock::iterator I = IfBlock2->begin();!isa<TerminatorInst>(I);++I)
01800       if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) {
01801         // This is not an aggressive instruction that we can promote.
01802         // Because of this, we won't be able to get rid of the control
01803         // flow, so the xform is not worth it.
01804         return false;
01805       }
01806   }
01807 
01808   DEBUG(dbgs() << "FOUND IF CONDITION!  " << *IfCond << "  T: "
01809                << IfTrue->getName() << "  F: " << IfFalse->getName() << "\n");
01810 
01811   // If we can still promote the PHI nodes after this gauntlet of tests,
01812   // do all of the PHI's now.
01813   Instruction *InsertPt = DomBlock->getTerminator();
01814   IRBuilder<true, NoFolder> Builder(InsertPt);
01815 
01816   // Move all 'aggressive' instructions, which are defined in the
01817   // conditional parts of the if's up to the dominating block.
01818   if (IfBlock1)
01819     DomBlock->getInstList().splice(InsertPt,
01820                                    IfBlock1->getInstList(), IfBlock1->begin(),
01821                                    IfBlock1->getTerminator());
01822   if (IfBlock2)
01823     DomBlock->getInstList().splice(InsertPt,
01824                                    IfBlock2->getInstList(), IfBlock2->begin(),
01825                                    IfBlock2->getTerminator());
01826 
01827   while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
01828     // Change the PHI node into a select instruction.
01829     Value *TrueVal  = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
01830     Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
01831 
01832     SelectInst *NV =
01833       cast<SelectInst>(Builder.CreateSelect(IfCond, TrueVal, FalseVal, ""));
01834     PN->replaceAllUsesWith(NV);
01835     NV->takeName(PN);
01836     PN->eraseFromParent();
01837   }
01838 
01839   // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
01840   // has been flattened.  Change DomBlock to jump directly to our new block to
01841   // avoid other simplifycfg's kicking in on the diamond.
01842   TerminatorInst *OldTI = DomBlock->getTerminator();
01843   Builder.SetInsertPoint(OldTI);
01844   Builder.CreateBr(BB);
01845   OldTI->eraseFromParent();
01846   return true;
01847 }
01848 
01849 /// SimplifyCondBranchToTwoReturns - If we found a conditional branch that goes
01850 /// to two returning blocks, try to merge them together into one return,
01851 /// introducing a select if the return values disagree.
01852 static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
01853                                            IRBuilder<> &Builder) {
01854   assert(BI->isConditional() && "Must be a conditional branch");
01855   BasicBlock *TrueSucc = BI->getSuccessor(0);
01856   BasicBlock *FalseSucc = BI->getSuccessor(1);
01857   ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
01858   ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator());
01859 
01860   // Check to ensure both blocks are empty (just a return) or optionally empty
01861   // with PHI nodes.  If there are other instructions, merging would cause extra
01862   // computation on one path or the other.
01863   if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
01864     return false;
01865   if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
01866     return false;
01867 
01868   Builder.SetInsertPoint(BI);
01869   // Okay, we found a branch that is going to two return nodes.  If
01870   // there is no return value for this function, just change the
01871   // branch into a return.
01872   if (FalseRet->getNumOperands() == 0) {
01873     TrueSucc->removePredecessor(BI->getParent());
01874     FalseSucc->removePredecessor(BI->getParent());
01875     Builder.CreateRetVoid();
01876     EraseTerminatorInstAndDCECond(BI);
01877     return true;
01878   }
01879 
01880   // Otherwise, figure out what the true and false return values are
01881   // so we can insert a new select instruction.
01882   Value *TrueValue = TrueRet->getReturnValue();
01883   Value *FalseValue = FalseRet->getReturnValue();
01884 
01885   // Unwrap any PHI nodes in the return blocks.
01886   if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
01887     if (TVPN->getParent() == TrueSucc)
01888       TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
01889   if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
01890     if (FVPN->getParent() == FalseSucc)
01891       FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
01892 
01893   // In order for this transformation to be safe, we must be able to
01894   // unconditionally execute both operands to the return.  This is
01895   // normally the case, but we could have a potentially-trapping
01896   // constant expression that prevents this transformation from being
01897   // safe.
01898   if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
01899     if (TCV->canTrap())
01900       return false;
01901   if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
01902     if (FCV->canTrap())
01903       return false;
01904 
01905   // Okay, we collected all the mapped values and checked them for sanity, and
01906   // defined to really do this transformation.  First, update the CFG.
01907   TrueSucc->removePredecessor(BI->getParent());
01908   FalseSucc->removePredecessor(BI->getParent());
01909 
01910   // Insert select instructions where needed.
01911   Value *BrCond = BI->getCondition();
01912   if (TrueValue) {
01913     // Insert a select if the results differ.
01914     if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
01915     } else if (isa<UndefValue>(TrueValue)) {
01916       TrueValue = FalseValue;
01917     } else {
01918       TrueValue = Builder.CreateSelect(BrCond, TrueValue,
01919                                        FalseValue, "retval");
01920     }
01921   }
01922 
01923   Value *RI = !TrueValue ?
01924     Builder.CreateRetVoid() : Builder.CreateRet(TrueValue);
01925 
01926   (void) RI;
01927 
01928   DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
01929                << "\n  " << *BI << "NewRet = " << *RI
01930                << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc);
01931 
01932   EraseTerminatorInstAndDCECond(BI);
01933 
01934   return true;
01935 }
01936 
01937 /// ExtractBranchMetadata - Given a conditional BranchInstruction, retrieve the
01938 /// probabilities of the branch taking each edge. Fills in the two APInt
01939 /// parameters and return true, or returns false if no or invalid metadata was
01940 /// found.
01941 static bool ExtractBranchMetadata(BranchInst *BI,
01942                                   uint64_t &ProbTrue, uint64_t &ProbFalse) {
01943   assert(BI->isConditional() &&
01944          "Looking for probabilities on unconditional branch?");
01945   MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
01946   if (!ProfileData || ProfileData->getNumOperands() != 3) return false;
01947   ConstantInt *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1));
01948   ConstantInt *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2));
01949   if (!CITrue || !CIFalse) return false;
01950   ProbTrue = CITrue->getValue().getZExtValue();
01951   ProbFalse = CIFalse->getValue().getZExtValue();
01952   return true;
01953 }
01954 
01955 /// checkCSEInPredecessor - Return true if the given instruction is available
01956 /// in its predecessor block. If yes, the instruction will be removed.
01957 ///
01958 static bool checkCSEInPredecessor(Instruction *Inst, BasicBlock *PB) {
01959   if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
01960     return false;
01961   for (BasicBlock::iterator I = PB->begin(), E = PB->end(); I != E; I++) {
01962     Instruction *PBI = &*I;
01963     // Check whether Inst and PBI generate the same value.
01964     if (Inst->isIdenticalTo(PBI)) {
01965       Inst->replaceAllUsesWith(PBI);
01966       Inst->eraseFromParent();
01967       return true;
01968     }
01969   }
01970   return false;
01971 }
01972 
01973 /// FoldBranchToCommonDest - If this basic block is simple enough, and if a
01974 /// predecessor branches to us and one of our successors, fold the block into
01975 /// the predecessor and use logical operations to pick the right destination.
01976 bool llvm::FoldBranchToCommonDest(BranchInst *BI, const DataLayout *DL) {
01977   BasicBlock *BB = BI->getParent();
01978 
01979   Instruction *Cond = nullptr;
01980   if (BI->isConditional())
01981     Cond = dyn_cast<Instruction>(BI->getCondition());
01982   else {
01983     // For unconditional branch, check for a simple CFG pattern, where
01984     // BB has a single predecessor and BB's successor is also its predecessor's
01985     // successor. If such pattern exisits, check for CSE between BB and its
01986     // predecessor.
01987     if (BasicBlock *PB = BB->getSinglePredecessor())
01988       if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
01989         if (PBI->isConditional() &&
01990             (BI->getSuccessor(0) == PBI->getSuccessor(0) ||
01991              BI->getSuccessor(0) == PBI->getSuccessor(1))) {
01992           for (BasicBlock::iterator I = BB->begin(), E = BB->end();
01993                I != E; ) {
01994             Instruction *Curr = I++;
01995             if (isa<CmpInst>(Curr)) {
01996               Cond = Curr;
01997               break;
01998             }
01999             // Quit if we can't remove this instruction.
02000             if (!checkCSEInPredecessor(Curr, PB))
02001               return false;
02002           }
02003         }
02004 
02005     if (!Cond)
02006       return false;
02007   }
02008 
02009   if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
02010       Cond->getParent() != BB || !Cond->hasOneUse())
02011   return false;
02012 
02013   // Only allow this if the condition is a simple instruction that can be
02014   // executed unconditionally.  It must be in the same block as the branch, and
02015   // must be at the front of the block.
02016   BasicBlock::iterator FrontIt = BB->front();
02017 
02018   // Ignore dbg intrinsics.
02019   while (isa<DbgInfoIntrinsic>(FrontIt)) ++FrontIt;
02020 
02021   // Allow a single instruction to be hoisted in addition to the compare
02022   // that feeds the branch.  We later ensure that any values that _it_ uses
02023   // were also live in the predecessor, so that we don't unnecessarily create
02024   // register pressure or inhibit out-of-order execution.
02025   Instruction *BonusInst = nullptr;
02026   if (&*FrontIt != Cond &&
02027       FrontIt->hasOneUse() && FrontIt->user_back() == Cond &&
02028       isSafeToSpeculativelyExecute(FrontIt, DL)) {
02029     BonusInst = &*FrontIt;
02030     ++FrontIt;
02031 
02032     // Ignore dbg intrinsics.
02033     while (isa<DbgInfoIntrinsic>(FrontIt)) ++FrontIt;
02034   }
02035 
02036   // Only a single bonus inst is allowed.
02037   if (&*FrontIt != Cond)
02038     return false;
02039 
02040   // Make sure the instruction after the condition is the cond branch.
02041   BasicBlock::iterator CondIt = Cond; ++CondIt;
02042 
02043   // Ignore dbg intrinsics.
02044   while (isa<DbgInfoIntrinsic>(CondIt)) ++CondIt;
02045 
02046   if (&*CondIt != BI)
02047     return false;
02048 
02049   // Cond is known to be a compare or binary operator.  Check to make sure that
02050   // neither operand is a potentially-trapping constant expression.
02051   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
02052     if (CE->canTrap())
02053       return false;
02054   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
02055     if (CE->canTrap())
02056       return false;
02057 
02058   // Finally, don't infinitely unroll conditional loops.
02059   BasicBlock *TrueDest  = BI->getSuccessor(0);
02060   BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr;
02061   if (TrueDest == BB || FalseDest == BB)
02062     return false;
02063 
02064   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
02065     BasicBlock *PredBlock = *PI;
02066     BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
02067 
02068     // Check that we have two conditional branches.  If there is a PHI node in
02069     // the common successor, verify that the same value flows in from both
02070     // blocks.
02071     SmallVector<PHINode*, 4> PHIs;
02072     if (!PBI || PBI->isUnconditional() ||
02073         (BI->isConditional() &&
02074          !SafeToMergeTerminators(BI, PBI)) ||
02075         (!BI->isConditional() &&
02076          !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
02077       continue;
02078 
02079     // Determine if the two branches share a common destination.
02080     Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
02081     bool InvertPredCond = false;
02082 
02083     if (BI->isConditional()) {
02084       if (PBI->getSuccessor(0) == TrueDest)
02085         Opc = Instruction::Or;
02086       else if (PBI->getSuccessor(1) == FalseDest)
02087         Opc = Instruction::And;
02088       else if (PBI->getSuccessor(0) == FalseDest)
02089         Opc = Instruction::And, InvertPredCond = true;
02090       else if (PBI->getSuccessor(1) == TrueDest)
02091         Opc = Instruction::Or, InvertPredCond = true;
02092       else
02093         continue;
02094     } else {
02095       if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
02096         continue;
02097     }
02098 
02099     DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
02100     IRBuilder<> Builder(PBI);
02101 
02102     // If we need to invert the condition in the pred block to match, do so now.
02103     if (InvertPredCond) {
02104       Value *NewCond = PBI->getCondition();
02105 
02106       if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
02107         CmpInst *CI = cast<CmpInst>(NewCond);
02108         CI->setPredicate(CI->getInversePredicate());
02109       } else {
02110         NewCond = Builder.CreateNot(NewCond,
02111                                     PBI->getCondition()->getName()+".not");
02112       }
02113 
02114       PBI->setCondition(NewCond);
02115       PBI->swapSuccessors();
02116     }
02117 
02118     // If we have a bonus inst, clone it into the predecessor block.
02119     Instruction *NewBonus = nullptr;
02120     if (BonusInst) {
02121       NewBonus = BonusInst->clone();
02122 
02123       // If we moved a load, we cannot any longer claim any knowledge about
02124       // its potential value. The previous information might have been valid
02125       // only given the branch precondition.
02126       // For an analogous reason, we must also drop all the metadata whose
02127       // semantics we don't understand.
02128       NewBonus->dropUnknownMetadata(LLVMContext::MD_dbg);
02129 
02130       PredBlock->getInstList().insert(PBI, NewBonus);
02131       NewBonus->takeName(BonusInst);
02132       BonusInst->setName(BonusInst->getName()+".old");
02133     }
02134 
02135     // Clone Cond into the predecessor basic block, and or/and the
02136     // two conditions together.
02137     Instruction *New = Cond->clone();
02138     if (BonusInst) New->replaceUsesOfWith(BonusInst, NewBonus);
02139     PredBlock->getInstList().insert(PBI, New);
02140     New->takeName(Cond);
02141     Cond->setName(New->getName()+".old");
02142 
02143     if (BI->isConditional()) {
02144       Instruction *NewCond =
02145         cast<Instruction>(Builder.CreateBinOp(Opc, PBI->getCondition(),
02146                                             New, "or.cond"));
02147       PBI->setCondition(NewCond);
02148 
02149       uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
02150       bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
02151                                                   PredFalseWeight);
02152       bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
02153                                                   SuccFalseWeight);
02154       SmallVector<uint64_t, 8> NewWeights;
02155 
02156       if (PBI->getSuccessor(0) == BB) {
02157         if (PredHasWeights && SuccHasWeights) {
02158           // PBI: br i1 %x, BB, FalseDest
02159           // BI:  br i1 %y, TrueDest, FalseDest
02160           //TrueWeight is TrueWeight for PBI * TrueWeight for BI.
02161           NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
02162           //FalseWeight is FalseWeight for PBI * TotalWeight for BI +
02163           //               TrueWeight for PBI * FalseWeight for BI.
02164           // We assume that total weights of a BranchInst can fit into 32 bits.
02165           // Therefore, we will not have overflow using 64-bit arithmetic.
02166           NewWeights.push_back(PredFalseWeight * (SuccFalseWeight +
02167                SuccTrueWeight) + PredTrueWeight * SuccFalseWeight);
02168         }
02169         AddPredecessorToBlock(TrueDest, PredBlock, BB);
02170         PBI->setSuccessor(0, TrueDest);
02171       }
02172       if (PBI->getSuccessor(1) == BB) {
02173         if (PredHasWeights && SuccHasWeights) {
02174           // PBI: br i1 %x, TrueDest, BB
02175           // BI:  br i1 %y, TrueDest, FalseDest
02176           //TrueWeight is TrueWeight for PBI * TotalWeight for BI +
02177           //              FalseWeight for PBI * TrueWeight for BI.
02178           NewWeights.push_back(PredTrueWeight * (SuccFalseWeight +
02179               SuccTrueWeight) + PredFalseWeight * SuccTrueWeight);
02180           //FalseWeight is FalseWeight for PBI * FalseWeight for BI.
02181           NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
02182         }
02183         AddPredecessorToBlock(FalseDest, PredBlock, BB);
02184         PBI->setSuccessor(1, FalseDest);
02185       }
02186       if (NewWeights.size() == 2) {
02187         // Halve the weights if any of them cannot fit in an uint32_t
02188         FitWeights(NewWeights);
02189 
02190         SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),NewWeights.end());
02191         PBI->setMetadata(LLVMContext::MD_prof,
02192                          MDBuilder(BI->getContext()).
02193                          createBranchWeights(MDWeights));
02194       } else
02195         PBI->setMetadata(LLVMContext::MD_prof, nullptr);
02196     } else {
02197       // Update PHI nodes in the common successors.
02198       for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
02199         ConstantInt *PBI_C = cast<ConstantInt>(
02200           PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
02201         assert(PBI_C->getType()->isIntegerTy(1));
02202         Instruction *MergedCond = nullptr;
02203         if (PBI->getSuccessor(0) == TrueDest) {
02204           // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
02205           // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
02206           //       is false: !PBI_Cond and BI_Value
02207           Instruction *NotCond =
02208             cast<Instruction>(Builder.CreateNot(PBI->getCondition(),
02209                                 "not.cond"));
02210           MergedCond =
02211             cast<Instruction>(Builder.CreateBinOp(Instruction::And,
02212                                 NotCond, New,
02213                                 "and.cond"));
02214           if (PBI_C->isOne())
02215             MergedCond =
02216               cast<Instruction>(Builder.CreateBinOp(Instruction::Or,
02217                                   PBI->getCondition(), MergedCond,
02218                                   "or.cond"));
02219         } else {
02220           // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
02221           // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
02222           //       is false: PBI_Cond and BI_Value
02223           MergedCond =
02224             cast<Instruction>(Builder.CreateBinOp(Instruction::And,
02225                                 PBI->getCondition(), New,
02226                                 "and.cond"));
02227           if (PBI_C->isOne()) {
02228             Instruction *NotCond =
02229               cast<Instruction>(Builder.CreateNot(PBI->getCondition(),
02230                                   "not.cond"));
02231             MergedCond =
02232               cast<Instruction>(Builder.CreateBinOp(Instruction::Or,
02233                                   NotCond, MergedCond,
02234                                   "or.cond"));
02235           }
02236         }
02237         // Update PHI Node.
02238         PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()),
02239                                   MergedCond);
02240       }
02241       // Change PBI from Conditional to Unconditional.
02242       BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
02243       EraseTerminatorInstAndDCECond(PBI);
02244       PBI = New_PBI;
02245     }
02246 
02247     // TODO: If BB is reachable from all paths through PredBlock, then we
02248     // could replace PBI's branch probabilities with BI's.
02249 
02250     // Copy any debug value intrinsics into the end of PredBlock.
02251     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
02252       if (isa<DbgInfoIntrinsic>(*I))
02253         I->clone()->insertBefore(PBI);
02254 
02255     return true;
02256   }
02257   return false;
02258 }
02259 
02260 /// SimplifyCondBranchToCondBranch - If we have a conditional branch as a
02261 /// predecessor of another block, this function tries to simplify it.  We know
02262 /// that PBI and BI are both conditional branches, and BI is in one of the
02263 /// successor blocks of PBI - PBI branches to BI.
02264 static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
02265   assert(PBI->isConditional() && BI->isConditional());
02266   BasicBlock *BB = BI->getParent();
02267 
02268   // If this block ends with a branch instruction, and if there is a
02269   // predecessor that ends on a branch of the same condition, make
02270   // this conditional branch redundant.
02271   if (PBI->getCondition() == BI->getCondition() &&
02272       PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
02273     // Okay, the outcome of this conditional branch is statically
02274     // knowable.  If this block had a single pred, handle specially.
02275     if (BB->getSinglePredecessor()) {
02276       // Turn this into a branch on constant.
02277       bool CondIsTrue = PBI->getSuccessor(0) == BB;
02278       BI->setCondition(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
02279                                         CondIsTrue));
02280       return true;  // Nuke the branch on constant.
02281     }
02282 
02283     // Otherwise, if there are multiple predecessors, insert a PHI that merges
02284     // in the constant and simplify the block result.  Subsequent passes of
02285     // simplifycfg will thread the block.
02286     if (BlockIsSimpleEnoughToThreadThrough(BB)) {
02287       pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
02288       PHINode *NewPN = PHINode::Create(Type::getInt1Ty(BB->getContext()),
02289                                        std::distance(PB, PE),
02290                                        BI->getCondition()->getName() + ".pr",
02291                                        BB->begin());
02292       // Okay, we're going to insert the PHI node.  Since PBI is not the only
02293       // predecessor, compute the PHI'd conditional value for all of the preds.
02294       // Any predecessor where the condition is not computable we keep symbolic.
02295       for (pred_iterator PI = PB; PI != PE; ++PI) {
02296         BasicBlock *P = *PI;
02297         if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) &&
02298             PBI != BI && PBI->isConditional() &&
02299             PBI->getCondition() == BI->getCondition() &&
02300             PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
02301           bool CondIsTrue = PBI->getSuccessor(0) == BB;
02302           NewPN->addIncoming(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
02303                                               CondIsTrue), P);
02304         } else {
02305           NewPN->addIncoming(BI->getCondition(), P);
02306         }
02307       }
02308 
02309       BI->setCondition(NewPN);
02310       return true;
02311     }
02312   }
02313 
02314   // If this is a conditional branch in an empty block, and if any
02315   // predecessors are a conditional branch to one of our destinations,
02316   // fold the conditions into logical ops and one cond br.
02317   BasicBlock::iterator BBI = BB->begin();
02318   // Ignore dbg intrinsics.
02319   while (isa<DbgInfoIntrinsic>(BBI))
02320     ++BBI;
02321   if (&*BBI != BI)
02322     return false;
02323 
02324 
02325   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BI->getCondition()))
02326     if (CE->canTrap())
02327       return false;
02328 
02329   int PBIOp, BIOp;
02330   if (PBI->getSuccessor(0) == BI->getSuccessor(0))
02331     PBIOp = BIOp = 0;
02332   else if (PBI->getSuccessor(0) == BI->getSuccessor(1))
02333     PBIOp = 0, BIOp = 1;
02334   else if (PBI->getSuccessor(1) == BI->getSuccessor(0))
02335     PBIOp = 1, BIOp = 0;
02336   else if (PBI->getSuccessor(1) == BI->getSuccessor(1))
02337     PBIOp = BIOp = 1;
02338   else
02339     return false;
02340 
02341   // Check to make sure that the other destination of this branch
02342   // isn't BB itself.  If so, this is an infinite loop that will
02343   // keep getting unwound.
02344   if (PBI->getSuccessor(PBIOp) == BB)
02345     return false;
02346 
02347   // Do not perform this transformation if it would require
02348   // insertion of a large number of select instructions. For targets
02349   // without predication/cmovs, this is a big pessimization.
02350 
02351   // Also do not perform this transformation if any phi node in the common
02352   // destination block can trap when reached by BB or PBB (PR17073). In that
02353   // case, it would be unsafe to hoist the operation into a select instruction.
02354 
02355   BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
02356   unsigned NumPhis = 0;
02357   for (BasicBlock::iterator II = CommonDest->begin();
02358        isa<PHINode>(II); ++II, ++NumPhis) {
02359     if (NumPhis > 2) // Disable this xform.
02360       return false;
02361 
02362     PHINode *PN = cast<PHINode>(II);
02363     Value *BIV = PN->getIncomingValueForBlock(BB);
02364     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BIV))
02365       if (CE->canTrap())
02366         return false;
02367 
02368     unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
02369     Value *PBIV = PN->getIncomingValue(PBBIdx);
02370     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PBIV))
02371       if (CE->canTrap())
02372         return false;
02373   }
02374 
02375   // Finally, if everything is ok, fold the branches to logical ops.
02376   BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
02377 
02378   DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
02379                << "AND: " << *BI->getParent());
02380 
02381 
02382   // If OtherDest *is* BB, then BB is a basic block with a single conditional
02383   // branch in it, where one edge (OtherDest) goes back to itself but the other
02384   // exits.  We don't *know* that the program avoids the infinite loop
02385   // (even though that seems likely).  If we do this xform naively, we'll end up
02386   // recursively unpeeling the loop.  Since we know that (after the xform is
02387   // done) that the block *is* infinite if reached, we just make it an obviously
02388   // infinite loop with no cond branch.
02389   if (OtherDest == BB) {
02390     // Insert it at the end of the function, because it's either code,
02391     // or it won't matter if it's hot. :)
02392     BasicBlock *InfLoopBlock = BasicBlock::Create(BB->getContext(),
02393                                                   "infloop", BB->getParent());
02394     BranchInst::Create(InfLoopBlock, InfLoopBlock);
02395     OtherDest = InfLoopBlock;
02396   }
02397 
02398   DEBUG(dbgs() << *PBI->getParent()->getParent());
02399 
02400   // BI may have other predecessors.  Because of this, we leave
02401   // it alone, but modify PBI.
02402 
02403   // Make sure we get to CommonDest on True&True directions.
02404   Value *PBICond = PBI->getCondition();
02405   IRBuilder<true, NoFolder> Builder(PBI);
02406   if (PBIOp)
02407     PBICond = Builder.CreateNot(PBICond, PBICond->getName()+".not");
02408 
02409   Value *BICond = BI->getCondition();
02410   if (BIOp)
02411     BICond = Builder.CreateNot(BICond, BICond->getName()+".not");
02412 
02413   // Merge the conditions.
02414   Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
02415 
02416   // Modify PBI to branch on the new condition to the new dests.
02417   PBI->setCondition(Cond);
02418   PBI->setSuccessor(0, CommonDest);
02419   PBI->setSuccessor(1, OtherDest);
02420 
02421   // Update branch weight for PBI.
02422   uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
02423   bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
02424                                               PredFalseWeight);
02425   bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
02426                                               SuccFalseWeight);
02427   if (PredHasWeights && SuccHasWeights) {
02428     uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
02429     uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
02430     uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
02431     uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
02432     // The weight to CommonDest should be PredCommon * SuccTotal +
02433     //                                    PredOther * SuccCommon.
02434     // The weight to OtherDest should be PredOther * SuccOther.
02435     SmallVector<uint64_t, 2> NewWeights;
02436     NewWeights.push_back(PredCommon * (SuccCommon + SuccOther) +
02437                          PredOther * SuccCommon);
02438     NewWeights.push_back(PredOther * SuccOther);
02439     // Halve the weights if any of them cannot fit in an uint32_t
02440     FitWeights(NewWeights);
02441 
02442     SmallVector<uint32_t, 2> MDWeights(NewWeights.begin(),NewWeights.end());
02443     PBI->setMetadata(LLVMContext::MD_prof,
02444                      MDBuilder(BI->getContext()).
02445                      createBranchWeights(MDWeights));
02446   }
02447 
02448   // OtherDest may have phi nodes.  If so, add an entry from PBI's
02449   // block that are identical to the entries for BI's block.
02450   AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
02451 
02452   // We know that the CommonDest already had an edge from PBI to
02453   // it.  If it has PHIs though, the PHIs may have different
02454   // entries for BB and PBI's BB.  If so, insert a select to make
02455   // them agree.
02456   PHINode *PN;
02457   for (BasicBlock::iterator II = CommonDest->begin();
02458        (PN = dyn_cast<PHINode>(II)); ++II) {
02459     Value *BIV = PN->getIncomingValueForBlock(BB);
02460     unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
02461     Value *PBIV = PN->getIncomingValue(PBBIdx);
02462     if (BIV != PBIV) {
02463       // Insert a select in PBI to pick the right value.
02464       Value *NV = cast<SelectInst>
02465         (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName()+".mux"));
02466       PN->setIncomingValue(PBBIdx, NV);
02467     }
02468   }
02469 
02470   DEBUG(dbgs() << "INTO: " << *PBI->getParent());
02471   DEBUG(dbgs() << *PBI->getParent()->getParent());
02472 
02473   // This basic block is probably dead.  We know it has at least
02474   // one fewer predecessor.
02475   return true;
02476 }
02477 
02478 // SimplifyTerminatorOnSelect - Simplifies a terminator by replacing it with a
02479 // branch to TrueBB if Cond is true or to FalseBB if Cond is false.
02480 // Takes care of updating the successors and removing the old terminator.
02481 // Also makes sure not to introduce new successors by assuming that edges to
02482 // non-successor TrueBBs and FalseBBs aren't reachable.
02483 static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond,
02484                                        BasicBlock *TrueBB, BasicBlock *FalseBB,
02485                                        uint32_t TrueWeight,
02486                                        uint32_t FalseWeight){
02487   // Remove any superfluous successor edges from the CFG.
02488   // First, figure out which successors to preserve.
02489   // If TrueBB and FalseBB are equal, only try to preserve one copy of that
02490   // successor.
02491   BasicBlock *KeepEdge1 = TrueBB;
02492   BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
02493 
02494   // Then remove the rest.
02495   for (unsigned I = 0, E = OldTerm->getNumSuccessors(); I != E; ++I) {
02496     BasicBlock *Succ = OldTerm->getSuccessor(I);
02497     // Make sure only to keep exactly one copy of each edge.
02498     if (Succ == KeepEdge1)
02499       KeepEdge1 = nullptr;
02500     else if (Succ == KeepEdge2)
02501       KeepEdge2 = nullptr;
02502     else
02503       Succ->removePredecessor(OldTerm->getParent());
02504   }
02505 
02506   IRBuilder<> Builder(OldTerm);
02507   Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
02508 
02509   // Insert an appropriate new terminator.
02510   if (!KeepEdge1 && !KeepEdge2) {
02511     if (TrueBB == FalseBB)
02512       // We were only looking for one successor, and it was present.
02513       // Create an unconditional branch to it.
02514       Builder.CreateBr(TrueBB);
02515     else {
02516       // We found both of the successors we were looking for.
02517       // Create a conditional branch sharing the condition of the select.
02518       BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
02519       if (TrueWeight != FalseWeight)
02520         NewBI->setMetadata(LLVMContext::MD_prof,
02521                            MDBuilder(OldTerm->getContext()).
02522                            createBranchWeights(TrueWeight, FalseWeight));
02523     }
02524   } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
02525     // Neither of the selected blocks were successors, so this
02526     // terminator must be unreachable.
02527     new UnreachableInst(OldTerm->getContext(), OldTerm);
02528   } else {
02529     // One of the selected values was a successor, but the other wasn't.
02530     // Insert an unconditional branch to the one that was found;
02531     // the edge to the one that wasn't must be unreachable.
02532     if (!KeepEdge1)
02533       // Only TrueBB was found.
02534       Builder.CreateBr(TrueBB);
02535     else
02536       // Only FalseBB was found.
02537       Builder.CreateBr(FalseBB);
02538   }
02539 
02540   EraseTerminatorInstAndDCECond(OldTerm);
02541   return true;
02542 }
02543 
02544 // SimplifySwitchOnSelect - Replaces
02545 //   (switch (select cond, X, Y)) on constant X, Y
02546 // with a branch - conditional if X and Y lead to distinct BBs,
02547 // unconditional otherwise.
02548 static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select) {
02549   // Check for constant integer values in the select.
02550   ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
02551   ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
02552   if (!TrueVal || !FalseVal)
02553     return false;
02554 
02555   // Find the relevant condition and destinations.
02556   Value *Condition = Select->getCondition();
02557   BasicBlock *TrueBB = SI->findCaseValue(TrueVal).getCaseSuccessor();
02558   BasicBlock *FalseBB = SI->findCaseValue(FalseVal).getCaseSuccessor();
02559 
02560   // Get weight for TrueBB and FalseBB.
02561   uint32_t TrueWeight = 0, FalseWeight = 0;
02562   SmallVector<uint64_t, 8> Weights;
02563   bool HasWeights = HasBranchWeights(SI);
02564   if (HasWeights) {
02565     GetBranchWeights(SI, Weights);
02566     if (Weights.size() == 1 + SI->getNumCases()) {
02567       TrueWeight = (uint32_t)Weights[SI->findCaseValue(TrueVal).
02568                                      getSuccessorIndex()];
02569       FalseWeight = (uint32_t)Weights[SI->findCaseValue(FalseVal).
02570                                       getSuccessorIndex()];
02571     }
02572   }
02573 
02574   // Perform the actual simplification.
02575   return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB,
02576                                     TrueWeight, FalseWeight);
02577 }
02578 
02579 // SimplifyIndirectBrOnSelect - Replaces
02580 //   (indirectbr (select cond, blockaddress(@fn, BlockA),
02581 //                             blockaddress(@fn, BlockB)))
02582 // with
02583 //   (br cond, BlockA, BlockB).
02584 static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI) {
02585   // Check that both operands of the select are block addresses.
02586   BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
02587   BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
02588   if (!TBA || !FBA)
02589     return false;
02590 
02591   // Extract the actual blocks.
02592   BasicBlock *TrueBB = TBA->getBasicBlock();
02593   BasicBlock *FalseBB = FBA->getBasicBlock();
02594 
02595   // Perform the actual simplification.
02596   return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB,
02597                                     0, 0);
02598 }
02599 
02600 /// TryToSimplifyUncondBranchWithICmpInIt - This is called when we find an icmp
02601 /// instruction (a seteq/setne with a constant) as the only instruction in a
02602 /// block that ends with an uncond branch.  We are looking for a very specific
02603 /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified.  In
02604 /// this case, we merge the first two "or's of icmp" into a switch, but then the
02605 /// default value goes to an uncond block with a seteq in it, we get something
02606 /// like:
02607 ///
02608 ///   switch i8 %A, label %DEFAULT [ i8 1, label %end    i8 2, label %end ]
02609 /// DEFAULT:
02610 ///   %tmp = icmp eq i8 %A, 92
02611 ///   br label %end
02612 /// end:
02613 ///   ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
02614 ///
02615 /// We prefer to split the edge to 'end' so that there is a true/false entry to
02616 /// the PHI, merging the third icmp into the switch.
02617 static bool TryToSimplifyUncondBranchWithICmpInIt(
02618     ICmpInst *ICI, IRBuilder<> &Builder, const TargetTransformInfo &TTI,
02619     const DataLayout *DL, AssumptionTracker *AT) {
02620   BasicBlock *BB = ICI->getParent();
02621 
02622   // If the block has any PHIs in it or the icmp has multiple uses, it is too
02623   // complex.
02624   if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse()) return false;
02625 
02626   Value *V = ICI->getOperand(0);
02627   ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
02628 
02629   // The pattern we're looking for is where our only predecessor is a switch on
02630   // 'V' and this block is the default case for the switch.  In this case we can
02631   // fold the compared value into the switch to simplify things.
02632   BasicBlock *Pred = BB->getSinglePredecessor();
02633   if (!Pred || !isa<SwitchInst>(Pred->getTerminator())) return false;
02634 
02635   SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
02636   if (SI->getCondition() != V)
02637     return false;
02638 
02639   // If BB is reachable on a non-default case, then we simply know the value of
02640   // V in this block.  Substitute it and constant fold the icmp instruction
02641   // away.
02642   if (SI->getDefaultDest() != BB) {
02643     ConstantInt *VVal = SI->findCaseDest(BB);
02644     assert(VVal && "Should have a unique destination value");
02645     ICI->setOperand(0, VVal);
02646 
02647     if (Value *V = SimplifyInstruction(ICI, DL)) {
02648       ICI->replaceAllUsesWith(V);
02649       ICI->eraseFromParent();
02650     }
02651     // BB is now empty, so it is likely to simplify away.
02652     return SimplifyCFG(BB, TTI, DL, AT) | true;
02653   }
02654 
02655   // Ok, the block is reachable from the default dest.  If the constant we're
02656   // comparing exists in one of the other edges, then we can constant fold ICI
02657   // and zap it.
02658   if (SI->findCaseValue(Cst) != SI->case_default()) {
02659     Value *V;
02660     if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
02661       V = ConstantInt::getFalse(BB->getContext());
02662     else
02663       V = ConstantInt::getTrue(BB->getContext());
02664 
02665     ICI->replaceAllUsesWith(V);
02666     ICI->eraseFromParent();
02667     // BB is now empty, so it is likely to simplify away.
02668     return SimplifyCFG(BB, TTI, DL, AT) | true;
02669   }
02670 
02671   // The use of the icmp has to be in the 'end' block, by the only PHI node in
02672   // the block.
02673   BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
02674   PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back());
02675   if (PHIUse == nullptr || PHIUse != &SuccBlock->front() ||
02676       isa<PHINode>(++BasicBlock::iterator(PHIUse)))
02677     return false;
02678 
02679   // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
02680   // true in the PHI.
02681   Constant *DefaultCst = ConstantInt::getTrue(BB->getContext());
02682   Constant *NewCst     = ConstantInt::getFalse(BB->getContext());
02683 
02684   if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
02685     std::swap(DefaultCst, NewCst);
02686 
02687   // Replace ICI (which is used by the PHI for the default value) with true or
02688   // false depending on if it is EQ or NE.
02689   ICI->replaceAllUsesWith(DefaultCst);
02690   ICI->eraseFromParent();
02691 
02692   // Okay, the switch goes to this block on a default value.  Add an edge from
02693   // the switch to the merge point on the compared value.
02694   BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "switch.edge",
02695                                          BB->getParent(), BB);
02696   SmallVector<uint64_t, 8> Weights;
02697   bool HasWeights = HasBranchWeights(SI);
02698   if (HasWeights) {
02699     GetBranchWeights(SI, Weights);
02700     if (Weights.size() == 1 + SI->getNumCases()) {
02701       // Split weight for default case to case for "Cst".
02702       Weights[0] = (Weights[0]+1) >> 1;
02703       Weights.push_back(Weights[0]);
02704 
02705       SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
02706       SI->setMetadata(LLVMContext::MD_prof,
02707                       MDBuilder(SI->getContext()).
02708                       createBranchWeights(MDWeights));
02709     }
02710   }
02711   SI->addCase(Cst, NewBB);
02712 
02713   // NewBB branches to the phi block, add the uncond branch and the phi entry.
02714   Builder.SetInsertPoint(NewBB);
02715   Builder.SetCurrentDebugLocation(SI->getDebugLoc());
02716   Builder.CreateBr(SuccBlock);
02717   PHIUse->addIncoming(NewCst, NewBB);
02718   return true;
02719 }
02720 
02721 /// SimplifyBranchOnICmpChain - The specified branch is a conditional branch.
02722 /// Check to see if it is branching on an or/and chain of icmp instructions, and
02723 /// fold it into a switch instruction if so.
02724 static bool SimplifyBranchOnICmpChain(BranchInst *BI, const DataLayout *DL,
02725                                       IRBuilder<> &Builder) {
02726   Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
02727   if (!Cond) return false;
02728 
02729 
02730   // Change br (X == 0 | X == 1), T, F into a switch instruction.
02731   // If this is a bunch of seteq's or'd together, or if it's a bunch of
02732   // 'setne's and'ed together, collect them.
02733   Value *CompVal = nullptr;
02734   std::vector<ConstantInt*> Values;
02735   bool TrueWhenEqual = true;
02736   Value *ExtraCase = nullptr;
02737   unsigned UsedICmps = 0;
02738 
02739   if (Cond->getOpcode() == Instruction::Or) {
02740     CompVal = GatherConstantCompares(Cond, Values, ExtraCase, DL, true,
02741                                      UsedICmps);
02742   } else if (Cond->getOpcode() == Instruction::And) {
02743     CompVal = GatherConstantCompares(Cond, Values, ExtraCase, DL, false,
02744                                      UsedICmps);
02745     TrueWhenEqual = false;
02746   }
02747 
02748   // If we didn't have a multiply compared value, fail.
02749   if (!CompVal) return false;
02750 
02751   // Avoid turning single icmps into a switch.
02752   if (UsedICmps <= 1)
02753     return false;
02754 
02755   // There might be duplicate constants in the list, which the switch
02756   // instruction can't handle, remove them now.
02757   array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
02758   Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
02759 
02760   // If Extra was used, we require at least two switch values to do the
02761   // transformation.  A switch with one value is just an cond branch.
02762   if (ExtraCase && Values.size() < 2) return false;
02763 
02764   // TODO: Preserve branch weight metadata, similarly to how
02765   // FoldValueComparisonIntoPredecessors preserves it.
02766 
02767   // Figure out which block is which destination.
02768   BasicBlock *DefaultBB = BI->getSuccessor(1);
02769   BasicBlock *EdgeBB    = BI->getSuccessor(0);
02770   if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
02771 
02772   BasicBlock *BB = BI->getParent();
02773 
02774   DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
02775                << " cases into SWITCH.  BB is:\n" << *BB);
02776 
02777   // If there are any extra values that couldn't be folded into the switch
02778   // then we evaluate them with an explicit branch first.  Split the block
02779   // right before the condbr to handle it.
02780   if (ExtraCase) {
02781     BasicBlock *NewBB = BB->splitBasicBlock(BI, "switch.early.test");
02782     // Remove the uncond branch added to the old block.
02783     TerminatorInst *OldTI = BB->getTerminator();
02784     Builder.SetInsertPoint(OldTI);
02785 
02786     if (TrueWhenEqual)
02787       Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
02788     else
02789       Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
02790 
02791     OldTI->eraseFromParent();
02792 
02793     // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
02794     // for the edge we just added.
02795     AddPredecessorToBlock(EdgeBB, BB, NewBB);
02796 
02797     DEBUG(dbgs() << "  ** 'icmp' chain unhandled condition: " << *ExtraCase
02798           << "\nEXTRABB = " << *BB);
02799     BB = NewBB;
02800   }
02801 
02802   Builder.SetInsertPoint(BI);
02803   // Convert pointer to int before we switch.
02804   if (CompVal->getType()->isPointerTy()) {
02805     assert(DL && "Cannot switch on pointer without DataLayout");
02806     CompVal = Builder.CreatePtrToInt(CompVal,
02807                                      DL->getIntPtrType(CompVal->getType()),
02808                                      "magicptr");
02809   }
02810 
02811   // Create the new switch instruction now.
02812   SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
02813 
02814   // Add all of the 'cases' to the switch instruction.
02815   for (unsigned i = 0, e = Values.size(); i != e; ++i)
02816     New->addCase(Values[i], EdgeBB);
02817 
02818   // We added edges from PI to the EdgeBB.  As such, if there were any
02819   // PHI nodes in EdgeBB, they need entries to be added corresponding to
02820   // the number of edges added.
02821   for (BasicBlock::iterator BBI = EdgeBB->begin();
02822        isa<PHINode>(BBI); ++BBI) {
02823     PHINode *PN = cast<PHINode>(BBI);
02824     Value *InVal = PN->getIncomingValueForBlock(BB);
02825     for (unsigned i = 0, e = Values.size()-1; i != e; ++i)
02826       PN->addIncoming(InVal, BB);
02827   }
02828 
02829   // Erase the old branch instruction.
02830   EraseTerminatorInstAndDCECond(BI);
02831 
02832   DEBUG(dbgs() << "  ** 'icmp' chain result is:\n" << *BB << '\n');
02833   return true;
02834 }
02835 
02836 bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
02837   // If this is a trivial landing pad that just continues unwinding the caught
02838   // exception then zap the landing pad, turning its invokes into calls.
02839   BasicBlock *BB = RI->getParent();
02840   LandingPadInst *LPInst = dyn_cast<LandingPadInst>(BB->getFirstNonPHI());
02841   if (RI->getValue() != LPInst)
02842     // Not a landing pad, or the resume is not unwinding the exception that
02843     // caused control to branch here.
02844     return false;
02845 
02846   // Check that there are no other instructions except for debug intrinsics.
02847   BasicBlock::iterator I = LPInst, E = RI;
02848   while (++I != E)
02849     if (!isa<DbgInfoIntrinsic>(I))
02850       return false;
02851 
02852   // Turn all invokes that unwind here into calls and delete the basic block.
02853   bool InvokeRequiresTableEntry = false;
02854   bool Changed = false;
02855   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
02856     InvokeInst *II = cast<InvokeInst>((*PI++)->getTerminator());
02857 
02858     if (II->hasFnAttr(Attribute::UWTable)) {
02859       // Don't remove an `invoke' instruction if the ABI requires an entry into
02860       // the table.
02861       InvokeRequiresTableEntry = true;
02862       continue;
02863     }
02864 
02865     SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3);
02866 
02867     // Insert a call instruction before the invoke.
02868     CallInst *Call = CallInst::Create(II->getCalledValue(), Args, "", II);
02869     Call->takeName(II);
02870     Call->setCallingConv(II->getCallingConv());
02871     Call->setAttributes(II->getAttributes());
02872     Call->setDebugLoc(II->getDebugLoc());
02873 
02874     // Anything that used the value produced by the invoke instruction now uses
02875     // the value produced by the call instruction.  Note that we do this even
02876     // for void functions and calls with no uses so that the callgraph edge is
02877     // updated.
02878     II->replaceAllUsesWith(Call);
02879     BB->removePredecessor(II->getParent());
02880 
02881     // Insert a branch to the normal destination right before the invoke.
02882     BranchInst::Create(II->getNormalDest(), II);
02883 
02884     // Finally, delete the invoke instruction!
02885     II->eraseFromParent();
02886     Changed = true;
02887   }
02888 
02889   if (!InvokeRequiresTableEntry)
02890     // The landingpad is now unreachable.  Zap it.
02891     BB->eraseFromParent();
02892 
02893   return Changed;
02894 }
02895 
02896 bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
02897   BasicBlock *BB = RI->getParent();
02898   if (!BB->getFirstNonPHIOrDbg()->isTerminator()) return false;
02899 
02900   // Find predecessors that end with branches.
02901   SmallVector<BasicBlock*, 8> UncondBranchPreds;
02902   SmallVector<BranchInst*, 8> CondBranchPreds;
02903   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
02904     BasicBlock *P = *PI;
02905     TerminatorInst *PTI = P->getTerminator();
02906     if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
02907       if (BI->isUnconditional())
02908         UncondBranchPreds.push_back(P);
02909       else
02910         CondBranchPreds.push_back(BI);
02911     }
02912   }
02913 
02914   // If we found some, do the transformation!
02915   if (!UncondBranchPreds.empty() && DupRet) {
02916     while (!UncondBranchPreds.empty()) {
02917       BasicBlock *Pred = UncondBranchPreds.pop_back_val();
02918       DEBUG(dbgs() << "FOLDING: " << *BB
02919             << "INTO UNCOND BRANCH PRED: " << *Pred);
02920       (void)FoldReturnIntoUncondBranch(RI, BB, Pred);
02921     }
02922 
02923     // If we eliminated all predecessors of the block, delete the block now.
02924     if (pred_begin(BB) == pred_end(BB))
02925       // We know there are no successors, so just nuke the block.
02926       BB->eraseFromParent();
02927 
02928     return true;
02929   }
02930 
02931   // Check out all of the conditional branches going to this return
02932   // instruction.  If any of them just select between returns, change the
02933   // branch itself into a select/return pair.
02934   while (!CondBranchPreds.empty()) {
02935     BranchInst *BI = CondBranchPreds.pop_back_val();
02936 
02937     // Check to see if the non-BB successor is also a return block.
02938     if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) &&
02939         isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) &&
02940         SimplifyCondBranchToTwoReturns(BI, Builder))
02941       return true;
02942   }
02943   return false;
02944 }
02945 
02946 bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) {
02947   BasicBlock *BB = UI->getParent();
02948 
02949   bool Changed = false;
02950 
02951   // If there are any instructions immediately before the unreachable that can
02952   // be removed, do so.
02953   while (UI != BB->begin()) {
02954     BasicBlock::iterator BBI = UI;
02955     --BBI;
02956     // Do not delete instructions that can have side effects which might cause
02957     // the unreachable to not be reachable; specifically, calls and volatile
02958     // operations may have this effect.
02959     if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI)) break;
02960 
02961     if (BBI->mayHaveSideEffects()) {
02962       if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
02963         if (SI->isVolatile())
02964           break;
02965       } else if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
02966         if (LI->isVolatile())
02967           break;
02968       } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
02969         if (RMWI->isVolatile())
02970           break;
02971       } else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
02972         if (CXI->isVolatile())
02973           break;
02974       } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
02975                  !isa<LandingPadInst>(BBI)) {
02976         break;
02977       }
02978       // Note that deleting LandingPad's here is in fact okay, although it
02979       // involves a bit of subtle reasoning. If this inst is a LandingPad,
02980       // all the predecessors of this block will be the unwind edges of Invokes,
02981       // and we can therefore guarantee this block will be erased.
02982     }
02983 
02984     // Delete this instruction (any uses are guaranteed to be dead)
02985     if (!BBI->use_empty())
02986       BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
02987     BBI->eraseFromParent();
02988     Changed = true;
02989   }
02990 
02991   // If the unreachable instruction is the first in the block, take a gander
02992   // at all of the predecessors of this instruction, and simplify them.
02993   if (&BB->front() != UI) return Changed;
02994 
02995   SmallVector<BasicBlock*, 8> Preds(pred_begin(BB), pred_end(BB));
02996   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
02997     TerminatorInst *TI = Preds[i]->getTerminator();
02998     IRBuilder<> Builder(TI);
02999     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
03000       if (BI->isUnconditional()) {
03001         if (BI->getSuccessor(0) == BB) {
03002           new UnreachableInst(TI->getContext(), TI);
03003           TI->eraseFromParent();
03004           Changed = true;
03005         }
03006       } else {
03007         if (BI->getSuccessor(0) == BB) {
03008           Builder.CreateBr(BI->getSuccessor(1));
03009           EraseTerminatorInstAndDCECond(BI);
03010         } else if (BI->getSuccessor(1) == BB) {
03011           Builder.CreateBr(BI->getSuccessor(0));
03012           EraseTerminatorInstAndDCECond(BI);
03013           Changed = true;
03014         }
03015       }
03016     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
03017       for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
03018            i != e; ++i)
03019         if (i.getCaseSuccessor() == BB) {
03020           BB->removePredecessor(SI->getParent());
03021           SI->removeCase(i);
03022           --i; --e;
03023           Changed = true;
03024         }
03025       // If the default value is unreachable, figure out the most popular
03026       // destination and make it the default.
03027       if (SI->getDefaultDest() == BB) {
03028         std::map<BasicBlock*, std::pair<unsigned, unsigned> > Popularity;
03029         for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
03030              i != e; ++i) {
03031           std::pair<unsigned, unsigned> &entry =
03032               Popularity[i.getCaseSuccessor()];
03033           if (entry.first == 0) {
03034             entry.first = 1;
03035             entry.second = i.getCaseIndex();
03036           } else {
03037             entry.first++;
03038           }
03039         }
03040 
03041         // Find the most popular block.
03042         unsigned MaxPop = 0;
03043         unsigned MaxIndex = 0;
03044         BasicBlock *MaxBlock = nullptr;
03045         for (std::map<BasicBlock*, std::pair<unsigned, unsigned> >::iterator
03046              I = Popularity.begin(), E = Popularity.end(); I != E; ++I) {
03047           if (I->second.first > MaxPop ||
03048               (I->second.first == MaxPop && MaxIndex > I->second.second)) {
03049             MaxPop = I->second.first;
03050             MaxIndex = I->second.second;
03051             MaxBlock = I->first;
03052           }
03053         }
03054         if (MaxBlock) {
03055           // Make this the new default, allowing us to delete any explicit
03056           // edges to it.
03057           SI->setDefaultDest(MaxBlock);
03058           Changed = true;
03059 
03060           // If MaxBlock has phinodes in it, remove MaxPop-1 entries from
03061           // it.
03062           if (isa<PHINode>(MaxBlock->begin()))
03063             for (unsigned i = 0; i != MaxPop-1; ++i)
03064               MaxBlock->removePredecessor(SI->getParent());
03065 
03066           for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
03067                i != e; ++i)
03068             if (i.getCaseSuccessor() == MaxBlock) {
03069               SI->removeCase(i);
03070               --i; --e;
03071             }
03072         }
03073       }
03074     } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
03075       if (II->getUnwindDest() == BB) {
03076         // Convert the invoke to a call instruction.  This would be a good
03077         // place to note that the call does not throw though.
03078         BranchInst *BI = Builder.CreateBr(II->getNormalDest());
03079         II->removeFromParent();   // Take out of symbol table
03080 
03081         // Insert the call now...
03082         SmallVector<Value*, 8> Args(II->op_begin(), II->op_end()-3);
03083         Builder.SetInsertPoint(BI);
03084         CallInst *CI = Builder.CreateCall(II->getCalledValue(),
03085                                           Args, II->getName());
03086         CI->setCallingConv(II->getCallingConv());
03087         CI->setAttributes(II->getAttributes());
03088         // If the invoke produced a value, the call does now instead.
03089         II->replaceAllUsesWith(CI);
03090         delete II;
03091         Changed = true;
03092       }
03093     }
03094   }
03095 
03096   // If this block is now dead, remove it.
03097   if (pred_begin(BB) == pred_end(BB) &&
03098       BB != &BB->getParent()->getEntryBlock()) {
03099     // We know there are no successors, so just nuke the block.
03100     BB->eraseFromParent();
03101     return true;
03102   }
03103 
03104   return Changed;
03105 }
03106 
03107 /// TurnSwitchRangeIntoICmp - Turns a switch with that contains only a
03108 /// integer range comparison into a sub, an icmp and a branch.
03109 static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) {
03110   assert(SI->getNumCases() > 1 && "Degenerate switch?");
03111 
03112   // Make sure all cases point to the same destination and gather the values.
03113   SmallVector<ConstantInt *, 16> Cases;
03114   SwitchInst::CaseIt I = SI->case_begin();
03115   Cases.push_back(I.getCaseValue());
03116   SwitchInst::CaseIt PrevI = I++;
03117   for (SwitchInst::CaseIt E = SI->case_end(); I != E; PrevI = I++) {
03118     if (PrevI.getCaseSuccessor() != I.getCaseSuccessor())
03119       return false;
03120     Cases.push_back(I.getCaseValue());
03121   }
03122   assert(Cases.size() == SI->getNumCases() && "Not all cases gathered");
03123 
03124   // Sort the case values, then check if they form a range we can transform.
03125   array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate);
03126   for (unsigned I = 1, E = Cases.size(); I != E; ++I) {
03127     if (Cases[I-1]->getValue() != Cases[I]->getValue()+1)
03128       return false;
03129   }
03130 
03131   Constant *Offset = ConstantExpr::getNeg(Cases.back());
03132   Constant *NumCases = ConstantInt::get(Offset->getType(), SI->getNumCases());
03133 
03134   Value *Sub = SI->getCondition();
03135   if (!Offset->isNullValue())
03136     Sub = Builder.CreateAdd(Sub, Offset, Sub->getName()+".off");
03137   Value *Cmp;
03138   // If NumCases overflowed, then all possible values jump to the successor.
03139   if (NumCases->isNullValue() && SI->getNumCases() != 0)
03140     Cmp = ConstantInt::getTrue(SI->getContext());
03141   else
03142     Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
03143   BranchInst *NewBI = Builder.CreateCondBr(
03144       Cmp, SI->case_begin().getCaseSuccessor(), SI->getDefaultDest());
03145 
03146   // Update weight for the newly-created conditional branch.
03147   SmallVector<uint64_t, 8> Weights;
03148   bool HasWeights = HasBranchWeights(SI);
03149   if (HasWeights) {
03150     GetBranchWeights(SI, Weights);
03151     if (Weights.size() == 1 + SI->getNumCases()) {
03152       // Combine all weights for the cases to be the true weight of NewBI.
03153       // We assume that the sum of all weights for a Terminator can fit into 32
03154       // bits.
03155       uint32_t NewTrueWeight = 0;
03156       for (unsigned I = 1, E = Weights.size(); I != E; ++I)
03157         NewTrueWeight += (uint32_t)Weights[I];
03158       NewBI->setMetadata(LLVMContext::MD_prof,
03159                          MDBuilder(SI->getContext()).
03160                          createBranchWeights(NewTrueWeight,
03161                                              (uint32_t)Weights[0]));
03162     }
03163   }
03164 
03165   // Prune obsolete incoming values off the successor's PHI nodes.
03166   for (BasicBlock::iterator BBI = SI->case_begin().getCaseSuccessor()->begin();
03167        isa<PHINode>(BBI); ++BBI) {
03168     for (unsigned I = 0, E = SI->getNumCases()-1; I != E; ++I)
03169       cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
03170   }
03171   SI->eraseFromParent();
03172 
03173   return true;
03174 }
03175 
03176 /// EliminateDeadSwitchCases - Compute masked bits for the condition of a switch
03177 /// and use it to remove dead cases.
03178 static bool EliminateDeadSwitchCases(SwitchInst *SI, const DataLayout *DL,
03179                                      AssumptionTracker *AT) {
03180   Value *Cond = SI->getCondition();
03181   unsigned Bits = Cond->getType()->getIntegerBitWidth();
03182   APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
03183   computeKnownBits(Cond, KnownZero, KnownOne, DL, 0, AT, SI);
03184 
03185   // Gather dead cases.
03186   SmallVector<ConstantInt*, 8> DeadCases;
03187   for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
03188     if ((I.getCaseValue()->getValue() & KnownZero) != 0 ||
03189         (I.getCaseValue()->getValue() & KnownOne) != KnownOne) {
03190       DeadCases.push_back(I.getCaseValue());
03191       DEBUG(dbgs() << "SimplifyCFG: switch case '"
03192                    << I.getCaseValue() << "' is dead.\n");
03193     }
03194   }
03195 
03196   SmallVector<uint64_t, 8> Weights;
03197   bool HasWeight = HasBranchWeights(SI);
03198   if (HasWeight) {
03199     GetBranchWeights(SI, Weights);
03200     HasWeight = (Weights.size() == 1 + SI->getNumCases());
03201   }
03202 
03203   // Remove dead cases from the switch.
03204   for (unsigned I = 0, E = DeadCases.size(); I != E; ++I) {
03205     SwitchInst::CaseIt Case = SI->findCaseValue(DeadCases[I]);
03206     assert(Case != SI->case_default() &&
03207            "Case was not found. Probably mistake in DeadCases forming.");
03208     if (HasWeight) {
03209       std::swap(Weights[Case.getCaseIndex()+1], Weights.back());
03210       Weights.pop_back();
03211     }
03212 
03213     // Prune unused values from PHI nodes.
03214     Case.getCaseSuccessor()->removePredecessor(SI->getParent());
03215     SI->removeCase(Case);
03216   }
03217   if (HasWeight && Weights.size() >= 2) {
03218     SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
03219     SI->setMetadata(LLVMContext::MD_prof,
03220                     MDBuilder(SI->getParent()->getContext()).
03221                     createBranchWeights(MDWeights));
03222   }
03223 
03224   return !DeadCases.empty();
03225 }
03226 
03227 /// FindPHIForConditionForwarding - If BB would be eligible for simplification
03228 /// by TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
03229 /// by an unconditional branch), look at the phi node for BB in the successor
03230 /// block and see if the incoming value is equal to CaseValue. If so, return
03231 /// the phi node, and set PhiIndex to BB's index in the phi node.
03232 static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue,
03233                                               BasicBlock *BB,
03234                                               int *PhiIndex) {
03235   if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
03236     return nullptr; // BB must be empty to be a candidate for simplification.
03237   if (!BB->getSinglePredecessor())
03238     return nullptr; // BB must be dominated by the switch.
03239 
03240   BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
03241   if (!Branch || !Branch->isUnconditional())
03242     return nullptr; // Terminator must be unconditional branch.
03243 
03244   BasicBlock *Succ = Branch->getSuccessor(0);
03245 
03246   BasicBlock::iterator I = Succ->begin();
03247   while (PHINode *PHI = dyn_cast<PHINode>(I++)) {
03248     int Idx = PHI->getBasicBlockIndex(BB);
03249     assert(Idx >= 0 && "PHI has no entry for predecessor?");
03250 
03251     Value *InValue = PHI->getIncomingValue(Idx);
03252     if (InValue != CaseValue) continue;
03253 
03254     *PhiIndex = Idx;
03255     return PHI;
03256   }
03257 
03258   return nullptr;
03259 }
03260 
03261 /// ForwardSwitchConditionToPHI - Try to forward the condition of a switch
03262 /// instruction to a phi node dominated by the switch, if that would mean that
03263 /// some of the destination blocks of the switch can be folded away.
03264 /// Returns true if a change is made.
03265 static bool ForwardSwitchConditionToPHI(SwitchInst *SI) {
03266   typedef DenseMap<PHINode*, SmallVector<int,4> > ForwardingNodesMap;
03267   ForwardingNodesMap ForwardingNodes;
03268 
03269   for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
03270     ConstantInt *CaseValue = I.getCaseValue();
03271     BasicBlock *CaseDest = I.getCaseSuccessor();
03272 
03273     int PhiIndex;
03274     PHINode *PHI = FindPHIForConditionForwarding(CaseValue, CaseDest,
03275                                                  &PhiIndex);
03276     if (!PHI) continue;
03277 
03278     ForwardingNodes[PHI].push_back(PhiIndex);
03279   }
03280 
03281   bool Changed = false;
03282 
03283   for (ForwardingNodesMap::iterator I = ForwardingNodes.begin(),
03284        E = ForwardingNodes.end(); I != E; ++I) {
03285     PHINode *Phi = I->first;
03286     SmallVectorImpl<int> &Indexes = I->second;
03287 
03288     if (Indexes.size() < 2) continue;
03289 
03290     for (size_t I = 0, E = Indexes.size(); I != E; ++I)
03291       Phi->setIncomingValue(Indexes[I], SI->getCondition());
03292     Changed = true;
03293   }
03294 
03295   return Changed;
03296 }
03297 
03298 /// ValidLookupTableConstant - Return true if the backend will be able to handle
03299 /// initializing an array of constants like C.
03300 static bool ValidLookupTableConstant(Constant *C) {
03301   if (C->isThreadDependent())
03302     return false;
03303   if (C->isDLLImportDependent())
03304     return false;
03305 
03306   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
03307     return CE->isGEPWithNoNotionalOverIndexing();
03308 
03309   return isa<ConstantFP>(C) ||
03310       isa<ConstantInt>(C) ||
03311       isa<ConstantPointerNull>(C) ||
03312       isa<GlobalValue>(C) ||
03313       isa<UndefValue>(C);
03314 }
03315 
03316 /// LookupConstant - If V is a Constant, return it. Otherwise, try to look up
03317 /// its constant value in ConstantPool, returning 0 if it's not there.
03318 static Constant *LookupConstant(Value *V,
03319                          const SmallDenseMap<Value*, Constant*>& ConstantPool) {
03320   if (Constant *C = dyn_cast<Constant>(V))
03321     return C;
03322   return ConstantPool.lookup(V);
03323 }
03324 
03325 /// ConstantFold - Try to fold instruction I into a constant. This works for
03326 /// simple instructions such as binary operations where both operands are
03327 /// constant or can be replaced by constants from the ConstantPool. Returns the
03328 /// resulting constant on success, 0 otherwise.
03329 static Constant *
03330 ConstantFold(Instruction *I,
03331              const SmallDenseMap<Value *, Constant *> &ConstantPool,
03332              const DataLayout *DL) {
03333   if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
03334     Constant *A = LookupConstant(Select->getCondition(), ConstantPool);
03335     if (!A)
03336       return nullptr;
03337     if (A->isAllOnesValue())
03338       return LookupConstant(Select->getTrueValue(), ConstantPool);
03339     if (A->isNullValue())
03340       return LookupConstant(Select->getFalseValue(), ConstantPool);
03341     return nullptr;
03342   }
03343 
03344   SmallVector<Constant *, 4> COps;
03345   for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) {
03346     if (Constant *A = LookupConstant(I->getOperand(N), ConstantPool))
03347       COps.push_back(A);
03348     else
03349       return nullptr;
03350   }
03351 
03352   if (CmpInst *Cmp = dyn_cast<CmpInst>(I))
03353     return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0],
03354                                            COps[1], DL);
03355 
03356   return ConstantFoldInstOperands(I->getOpcode(), I->getType(), COps, DL);
03357 }
03358 
03359 /// GetCaseResults - Try to determine the resulting constant values in phi nodes
03360 /// at the common destination basic block, *CommonDest, for one of the case
03361 /// destionations CaseDest corresponding to value CaseVal (0 for the default
03362 /// case), of a switch instruction SI.
03363 static bool
03364 GetCaseResults(SwitchInst *SI,
03365                ConstantInt *CaseVal,
03366                BasicBlock *CaseDest,
03367                BasicBlock **CommonDest,
03368                SmallVectorImpl<std::pair<PHINode *, Constant *> > &Res,
03369                const DataLayout *DL) {
03370   // The block from which we enter the common destination.
03371   BasicBlock *Pred = SI->getParent();
03372 
03373   // If CaseDest is empty except for some side-effect free instructions through
03374   // which we can constant-propagate the CaseVal, continue to its successor.
03375   SmallDenseMap<Value*, Constant*> ConstantPool;
03376   ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
03377   for (BasicBlock::iterator I = CaseDest->begin(), E = CaseDest->end(); I != E;
03378        ++I) {
03379     if (TerminatorInst *T = dyn_cast<TerminatorInst>(I)) {
03380       // If the terminator is a simple branch, continue to the next block.
03381       if (T->getNumSuccessors() != 1)
03382         return false;
03383       Pred = CaseDest;
03384       CaseDest = T->getSuccessor(0);
03385     } else if (isa<DbgInfoIntrinsic>(I)) {
03386       // Skip debug intrinsic.
03387       continue;
03388     } else if (Constant *C = ConstantFold(I, ConstantPool, DL)) {
03389       // Instruction is side-effect free and constant.
03390       ConstantPool.insert(std::make_pair(I, C));
03391     } else {
03392       break;
03393     }
03394   }
03395 
03396   // If we did not have a CommonDest before, use the current one.
03397   if (!*CommonDest)
03398     *CommonDest = CaseDest;
03399   // If the destination isn't the common one, abort.
03400   if (CaseDest != *CommonDest)
03401     return false;
03402 
03403   // Get the values for this case from phi nodes in the destination block.
03404   BasicBlock::iterator I = (*CommonDest)->begin();
03405   while (PHINode *PHI = dyn_cast<PHINode>(I++)) {
03406     int Idx = PHI->getBasicBlockIndex(Pred);
03407     if (Idx == -1)
03408       continue;
03409 
03410     Constant *ConstVal = LookupConstant(PHI->getIncomingValue(Idx),
03411                                         ConstantPool);
03412     if (!ConstVal)
03413       return false;
03414 
03415     // Note: If the constant comes from constant-propagating the case value
03416     // through the CaseDest basic block, it will be safe to remove the
03417     // instructions in that block. They cannot be used (except in the phi nodes
03418     // we visit) outside CaseDest, because that block does not dominate its
03419     // successor. If it did, we would not be in this phi node.
03420 
03421     // Be conservative about which kinds of constants we support.
03422     if (!ValidLookupTableConstant(ConstVal))
03423       return false;
03424 
03425     Res.push_back(std::make_pair(PHI, ConstVal));
03426   }
03427 
03428   return Res.size() > 0;
03429 }
03430 
03431 namespace {
03432   /// SwitchLookupTable - This class represents a lookup table that can be used
03433   /// to replace a switch.
03434   class SwitchLookupTable {
03435   public:
03436     /// SwitchLookupTable - Create a lookup table to use as a switch replacement
03437     /// with the contents of Values, using DefaultValue to fill any holes in the
03438     /// table.
03439     SwitchLookupTable(Module &M,
03440                       uint64_t TableSize,
03441                       ConstantInt *Offset,
03442              const SmallVectorImpl<std::pair<ConstantInt*, Constant*> >& Values,
03443                       Constant *DefaultValue,
03444                       const DataLayout *DL);
03445 
03446     /// BuildLookup - Build instructions with Builder to retrieve the value at
03447     /// the position given by Index in the lookup table.
03448     Value *BuildLookup(Value *Index, IRBuilder<> &Builder);
03449 
03450     /// WouldFitInRegister - Return true if a table with TableSize elements of
03451     /// type ElementType would fit in a target-legal register.
03452     static bool WouldFitInRegister(const DataLayout *DL,
03453                                    uint64_t TableSize,
03454                                    const Type *ElementType);
03455 
03456   private:
03457     // Depending on the contents of the table, it can be represented in
03458     // different ways.
03459     enum {
03460       // For tables where each element contains the same value, we just have to
03461       // store that single value and return it for each lookup.
03462       SingleValueKind,
03463 
03464       // For small tables with integer elements, we can pack them into a bitmap
03465       // that fits into a target-legal register. Values are retrieved by
03466       // shift and mask operations.
03467       BitMapKind,
03468 
03469       // The table is stored as an array of values. Values are retrieved by load
03470       // instructions from the table.
03471       ArrayKind
03472     } Kind;
03473 
03474     // For SingleValueKind, this is the single value.
03475     Constant *SingleValue;
03476 
03477     // For BitMapKind, this is the bitmap.
03478     ConstantInt *BitMap;
03479     IntegerType *BitMapElementTy;
03480 
03481     // For ArrayKind, this is the array.
03482     GlobalVariable *Array;
03483   };
03484 }
03485 
03486 SwitchLookupTable::SwitchLookupTable(Module &M,
03487                                      uint64_t TableSize,
03488                                      ConstantInt *Offset,
03489              const SmallVectorImpl<std::pair<ConstantInt*, Constant*> >& Values,
03490                                      Constant *DefaultValue,
03491                                      const DataLayout *DL)
03492     : SingleValue(nullptr), BitMap(nullptr), BitMapElementTy(nullptr),
03493       Array(nullptr) {
03494   assert(Values.size() && "Can't build lookup table without values!");
03495   assert(TableSize >= Values.size() && "Can't fit values in table!");
03496 
03497   // If all values in the table are equal, this is that value.
03498   SingleValue = Values.begin()->second;
03499 
03500   Type *ValueType = Values.begin()->second->getType();
03501 
03502   // Build up the table contents.
03503   SmallVector<Constant*, 64> TableContents(TableSize);
03504   for (size_t I = 0, E = Values.size(); I != E; ++I) {
03505     ConstantInt *CaseVal = Values[I].first;
03506     Constant *CaseRes = Values[I].second;
03507     assert(CaseRes->getType() == ValueType);
03508 
03509     uint64_t Idx = (CaseVal->getValue() - Offset->getValue())
03510                    .getLimitedValue();
03511     TableContents[Idx] = CaseRes;
03512 
03513     if (CaseRes != SingleValue)
03514       SingleValue = nullptr;
03515   }
03516 
03517   // Fill in any holes in the table with the default result.
03518   if (Values.size() < TableSize) {
03519     assert(DefaultValue &&
03520            "Need a default value to fill the lookup table holes.");
03521     assert(DefaultValue->getType() == ValueType);
03522     for (uint64_t I = 0; I < TableSize; ++I) {
03523       if (!TableContents[I])
03524         TableContents[I] = DefaultValue;
03525     }
03526 
03527     if (DefaultValue != SingleValue)
03528       SingleValue = nullptr;
03529   }
03530 
03531   // If each element in the table contains the same value, we only need to store
03532   // that single value.
03533   if (SingleValue) {
03534     Kind = SingleValueKind;
03535     return;
03536   }
03537 
03538   // If the type is integer and the table fits in a register, build a bitmap.
03539   if (WouldFitInRegister(DL, TableSize, ValueType)) {
03540     IntegerType *IT = cast<IntegerType>(ValueType);
03541     APInt TableInt(TableSize * IT->getBitWidth(), 0);
03542     for (uint64_t I = TableSize; I > 0; --I) {
03543       TableInt <<= IT->getBitWidth();
03544       // Insert values into the bitmap. Undef values are set to zero.
03545       if (!isa<UndefValue>(TableContents[I - 1])) {
03546         ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
03547         TableInt |= Val->getValue().zext(TableInt.getBitWidth());
03548       }
03549     }
03550     BitMap = ConstantInt::get(M.getContext(), TableInt);
03551     BitMapElementTy = IT;
03552     Kind = BitMapKind;
03553     ++NumBitMaps;
03554     return;
03555   }
03556 
03557   // Store the table in an array.
03558   ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
03559   Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
03560 
03561   Array = new GlobalVariable(M, ArrayTy, /*constant=*/ true,
03562                              GlobalVariable::PrivateLinkage,
03563                              Initializer,
03564                              "switch.table");
03565   Array->setUnnamedAddr(true);
03566   Kind = ArrayKind;
03567 }
03568 
03569 Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {
03570   switch (Kind) {
03571     case SingleValueKind:
03572       return SingleValue;
03573     case BitMapKind: {
03574       // Type of the bitmap (e.g. i59).
03575       IntegerType *MapTy = BitMap->getType();
03576 
03577       // Cast Index to the same type as the bitmap.
03578       // Note: The Index is <= the number of elements in the table, so
03579       // truncating it to the width of the bitmask is safe.
03580       Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy, "switch.cast");
03581 
03582       // Multiply the shift amount by the element width.
03583       ShiftAmt = Builder.CreateMul(ShiftAmt,
03584                       ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()),
03585                                    "switch.shiftamt");
03586 
03587       // Shift down.
03588       Value *DownShifted = Builder.CreateLShr(BitMap, ShiftAmt,
03589                                               "switch.downshift");
03590       // Mask off.
03591       return Builder.CreateTrunc(DownShifted, BitMapElementTy,
03592                                  "switch.masked");
03593     }
03594     case ArrayKind: {
03595       // Make sure the table index will not overflow when treated as signed.
03596       IntegerType *IT = cast<IntegerType>(Index->getType());
03597       uint64_t TableSize = Array->getInitializer()->getType()
03598                                 ->getArrayNumElements();
03599       if (TableSize > (1ULL << (IT->getBitWidth() - 1)))
03600         Index = Builder.CreateZExt(Index,
03601                                    IntegerType::get(IT->getContext(),
03602                                                     IT->getBitWidth() + 1),
03603                                    "switch.tableidx.zext");
03604 
03605       Value *GEPIndices[] = { Builder.getInt32(0), Index };
03606       Value *GEP = Builder.CreateInBoundsGEP(Array, GEPIndices,
03607                                              "switch.gep");
03608       return Builder.CreateLoad(GEP, "switch.load");
03609     }
03610   }
03611   llvm_unreachable("Unknown lookup table kind!");
03612 }
03613 
03614 bool SwitchLookupTable::WouldFitInRegister(const DataLayout *DL,
03615                                            uint64_t TableSize,
03616                                            const Type *ElementType) {
03617   if (!DL)
03618     return false;
03619   const IntegerType *IT = dyn_cast<IntegerType>(ElementType);
03620   if (!IT)
03621     return false;
03622   // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
03623   // are <= 15, we could try to narrow the type.
03624 
03625   // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
03626   if (TableSize >= UINT_MAX/IT->getBitWidth())
03627     return false;
03628   return DL->fitsInLegalInteger(TableSize * IT->getBitWidth());
03629 }
03630 
03631 /// ShouldBuildLookupTable - Determine whether a lookup table should be built
03632 /// for this switch, based on the number of cases, size of the table and the
03633 /// types of the results.
03634 static bool ShouldBuildLookupTable(SwitchInst *SI,
03635                                    uint64_t TableSize,
03636                                    const TargetTransformInfo &TTI,
03637                                    const DataLayout *DL,
03638                             const SmallDenseMap<PHINode*, Type*>& ResultTypes) {
03639   if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
03640     return false; // TableSize overflowed, or mul below might overflow.
03641 
03642   bool AllTablesFitInRegister = true;
03643   bool HasIllegalType = false;
03644   for (SmallDenseMap<PHINode*, Type*>::const_iterator I = ResultTypes.begin(),
03645        E = ResultTypes.end(); I != E; ++I) {
03646     Type *Ty = I->second;
03647 
03648     // Saturate this flag to true.
03649     HasIllegalType = HasIllegalType || !TTI.isTypeLegal(Ty);
03650 
03651     // Saturate this flag to false.
03652     AllTablesFitInRegister = AllTablesFitInRegister &&
03653       SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
03654 
03655     // If both flags saturate, we're done. NOTE: This *only* works with
03656     // saturating flags, and all flags have to saturate first due to the
03657     // non-deterministic behavior of iterating over a dense map.
03658     if (HasIllegalType && !AllTablesFitInRegister)
03659       break;
03660   }
03661 
03662   // If each table would fit in a register, we should build it anyway.
03663   if (AllTablesFitInRegister)
03664     return true;
03665 
03666   // Don't build a table that doesn't fit in-register if it has illegal types.
03667   if (HasIllegalType)
03668     return false;
03669 
03670   // The table density should be at least 40%. This is the same criterion as for
03671   // jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
03672   // FIXME: Find the best cut-off.
03673   return SI->getNumCases() * 10 >= TableSize * 4;
03674 }
03675 
03676 /// SwitchToLookupTable - If the switch is only used to initialize one or more
03677 /// phi nodes in a common successor block with different constant values,
03678 /// replace the switch with lookup tables.
03679 static bool SwitchToLookupTable(SwitchInst *SI,
03680                                 IRBuilder<> &Builder,
03681                                 const TargetTransformInfo &TTI,
03682                                 const DataLayout* DL) {
03683   assert(SI->getNumCases() > 1 && "Degenerate switch?");
03684 
03685   // Only build lookup table when we have a target that supports it.
03686   if (!TTI.shouldBuildLookupTables())
03687     return false;
03688 
03689   // FIXME: If the switch is too sparse for a lookup table, perhaps we could
03690   // split off a dense part and build a lookup table for that.
03691 
03692   // FIXME: This creates arrays of GEPs to constant strings, which means each
03693   // GEP needs a runtime relocation in PIC code. We should just build one big
03694   // string and lookup indices into that.
03695 
03696   // Ignore switches with less than three cases. Lookup tables will not make them
03697   // faster, so we don't analyze them.
03698   if (SI->getNumCases() < 3)
03699     return false;
03700 
03701   // Figure out the corresponding result for each case value and phi node in the
03702   // common destination, as well as the the min and max case values.
03703   assert(SI->case_begin() != SI->case_end());
03704   SwitchInst::CaseIt CI = SI->case_begin();
03705   ConstantInt *MinCaseVal = CI.getCaseValue();
03706   ConstantInt *MaxCaseVal = CI.getCaseValue();
03707 
03708   BasicBlock *CommonDest = nullptr;
03709   typedef SmallVector<std::pair<ConstantInt*, Constant*>, 4> ResultListTy;
03710   SmallDenseMap<PHINode*, ResultListTy> ResultLists;
03711   SmallDenseMap<PHINode*, Constant*> DefaultResults;
03712   SmallDenseMap<PHINode*, Type*> ResultTypes;
03713   SmallVector<PHINode*, 4> PHIs;
03714 
03715   for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
03716     ConstantInt *CaseVal = CI.getCaseValue();
03717     if (CaseVal->getValue().slt(MinCaseVal->getValue()))
03718       MinCaseVal = CaseVal;
03719     if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
03720       MaxCaseVal = CaseVal;
03721 
03722     // Resulting value at phi nodes for this case value.
03723     typedef SmallVector<std::pair<PHINode*, Constant*>, 4> ResultsTy;
03724     ResultsTy Results;
03725     if (!GetCaseResults(SI, CaseVal, CI.getCaseSuccessor(), &CommonDest,
03726                         Results, DL))
03727       return false;
03728 
03729     // Append the result from this case to the list for each phi.
03730     for (ResultsTy::iterator I = Results.begin(), E = Results.end(); I!=E; ++I) {
03731       if (!ResultLists.count(I->first))
03732         PHIs.push_back(I->first);
03733       ResultLists[I->first].push_back(std::make_pair(CaseVal, I->second));
03734     }
03735   }
03736 
03737   // Keep track of the result types.
03738   for (size_t I = 0, E = PHIs.size(); I != E; ++I) {
03739     PHINode *PHI = PHIs[I];
03740     ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
03741   }
03742 
03743   uint64_t NumResults = ResultLists[PHIs[0]].size();
03744   APInt RangeSpread = MaxCaseVal->getValue() - MinCaseVal->getValue();
03745   uint64_t TableSize = RangeSpread.getLimitedValue() + 1;
03746   bool TableHasHoles = (NumResults < TableSize);
03747 
03748   // If the table has holes, we need a constant result for the default case
03749   // or a bitmask that fits in a register.
03750   SmallVector<std::pair<PHINode*, Constant*>, 4> DefaultResultsList;
03751   bool HasDefaultResults = false;
03752   if (TableHasHoles) {
03753     HasDefaultResults = GetCaseResults(SI, nullptr, SI->getDefaultDest(),
03754                                        &CommonDest, DefaultResultsList, DL);
03755   }
03756   bool NeedMask = (TableHasHoles && !HasDefaultResults);
03757   if (NeedMask) {
03758     // As an extra penalty for the validity test we require more cases.
03759     if (SI->getNumCases() < 4)  // FIXME: Find best threshold value (benchmark).
03760       return false;
03761     if (!(DL && DL->fitsInLegalInteger(TableSize)))
03762       return false;
03763   }
03764 
03765   for (size_t I = 0, E = DefaultResultsList.size(); I != E; ++I) {
03766     PHINode *PHI = DefaultResultsList[I].first;
03767     Constant *Result = DefaultResultsList[I].second;
03768     DefaultResults[PHI] = Result;
03769   }
03770 
03771   if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
03772     return false;
03773 
03774   // Create the BB that does the lookups.
03775   Module &Mod = *CommonDest->getParent()->getParent();
03776   BasicBlock *LookupBB = BasicBlock::Create(Mod.getContext(),
03777                                             "switch.lookup",
03778                                             CommonDest->getParent(),
03779                                             CommonDest);
03780 
03781   // Compute the table index value.
03782   Builder.SetInsertPoint(SI);
03783   Value *TableIndex = Builder.CreateSub(SI->getCondition(), MinCaseVal,
03784                                         "switch.tableidx");
03785 
03786   // Compute the maximum table size representable by the integer type we are
03787   // switching upon.
03788   unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
03789   uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
03790   assert(MaxTableSize >= TableSize &&
03791          "It is impossible for a switch to have more entries than the max "
03792          "representable value of its input integer type's size.");
03793 
03794   // If we have a fully covered lookup table, unconditionally branch to the
03795   // lookup table BB. Otherwise, check if the condition value is within the case
03796   // range. If it is so, branch to the new BB. Otherwise branch to SI's default
03797   // destination.
03798   const bool GeneratingCoveredLookupTable = MaxTableSize == TableSize;
03799   if (GeneratingCoveredLookupTable) {
03800     Builder.CreateBr(LookupBB);
03801     // We cached PHINodes in PHIs, to avoid accessing deleted PHINodes later,
03802     // do not delete PHINodes here.
03803     SI->getDefaultDest()->removePredecessor(SI->getParent(),
03804                                             true/*DontDeleteUselessPHIs*/);
03805   } else {
03806     Value *Cmp = Builder.CreateICmpULT(TableIndex, ConstantInt::get(
03807                                        MinCaseVal->getType(), TableSize));
03808     Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
03809   }
03810 
03811   // Populate the BB that does the lookups.
03812   Builder.SetInsertPoint(LookupBB);
03813 
03814   if (NeedMask) {
03815     // Before doing the lookup we do the hole check.
03816     // The LookupBB is therefore re-purposed to do the hole check
03817     // and we create a new LookupBB.
03818     BasicBlock *MaskBB = LookupBB;
03819     MaskBB->setName("switch.hole_check");
03820     LookupBB = BasicBlock::Create(Mod.getContext(),
03821                                   "switch.lookup",
03822                                   CommonDest->getParent(),
03823                                   CommonDest);
03824 
03825     // Build bitmask; fill in a 1 bit for every case.
03826     APInt MaskInt(TableSize, 0);
03827     APInt One(TableSize, 1);
03828     const ResultListTy &ResultList = ResultLists[PHIs[0]];
03829     for (size_t I = 0, E = ResultList.size(); I != E; ++I) {
03830       uint64_t Idx = (ResultList[I].first->getValue() -
03831                       MinCaseVal->getValue()).getLimitedValue();
03832       MaskInt |= One << Idx;
03833     }
03834     ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt);
03835 
03836     // Get the TableIndex'th bit of the bitmask.
03837     // If this bit is 0 (meaning hole) jump to the default destination,
03838     // else continue with table lookup.
03839     IntegerType *MapTy = TableMask->getType();
03840     Value *MaskIndex = Builder.CreateZExtOrTrunc(TableIndex, MapTy,
03841                                                  "switch.maskindex");
03842     Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex,
03843                                         "switch.shifted");
03844     Value *LoBit = Builder.CreateTrunc(Shifted,
03845                                        Type::getInt1Ty(Mod.getContext()),
03846                                        "switch.lobit");
03847     Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
03848 
03849     Builder.SetInsertPoint(LookupBB);
03850     AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
03851   }
03852 
03853   bool ReturnedEarly = false;
03854   for (size_t I = 0, E = PHIs.size(); I != E; ++I) {
03855     PHINode *PHI = PHIs[I];
03856 
03857     // If using a bitmask, use any value to fill the lookup table holes.
03858     Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
03859     SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultLists[PHI],
03860                             DV, DL);
03861 
03862     Value *Result = Table.BuildLookup(TableIndex, Builder);
03863 
03864     // If the result is used to return immediately from the function, we want to
03865     // do that right here.
03866     if (PHI->hasOneUse() && isa<ReturnInst>(*PHI->user_begin()) &&
03867         PHI->user_back() == CommonDest->getFirstNonPHIOrDbg()) {
03868       Builder.CreateRet(Result);
03869       ReturnedEarly = true;
03870       break;
03871     }
03872 
03873     PHI->addIncoming(Result, LookupBB);
03874   }
03875 
03876   if (!ReturnedEarly)
03877     Builder.CreateBr(CommonDest);
03878 
03879   // Remove the switch.
03880   for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
03881     BasicBlock *Succ = SI->getSuccessor(i);
03882 
03883     if (Succ == SI->getDefaultDest())
03884       continue;
03885     Succ->removePredecessor(SI->getParent());
03886   }
03887   SI->eraseFromParent();
03888 
03889   ++NumLookupTables;
03890   if (NeedMask)
03891     ++NumLookupTablesHoles;
03892   return true;
03893 }
03894 
03895 bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
03896   BasicBlock *BB = SI->getParent();
03897 
03898   if (isValueEqualityComparison(SI)) {
03899     // If we only have one predecessor, and if it is a branch on this value,
03900     // see if that predecessor totally determines the outcome of this switch.
03901     if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
03902       if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
03903         return SimplifyCFG(BB, TTI, DL, AT) | true;
03904 
03905     Value *Cond = SI->getCondition();
03906     if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
03907       if (SimplifySwitchOnSelect(SI, Select))
03908         return SimplifyCFG(BB, TTI, DL, AT) | true;
03909 
03910     // If the block only contains the switch, see if we can fold the block
03911     // away into any preds.
03912     BasicBlock::iterator BBI = BB->begin();
03913     // Ignore dbg intrinsics.
03914     while (isa<DbgInfoIntrinsic>(BBI))
03915       ++BBI;
03916     if (SI == &*BBI)
03917       if (FoldValueComparisonIntoPredecessors(SI, Builder))
03918         return SimplifyCFG(BB, TTI, DL, AT) | true;
03919   }
03920 
03921   // Try to transform the switch into an icmp and a branch.
03922   if (TurnSwitchRangeIntoICmp(SI, Builder))
03923     return SimplifyCFG(BB, TTI, DL, AT) | true;
03924 
03925   // Remove unreachable cases.
03926   if (EliminateDeadSwitchCases(SI, DL, AT))
03927     return SimplifyCFG(BB, TTI, DL, AT) | true;
03928 
03929   if (ForwardSwitchConditionToPHI(SI))
03930     return SimplifyCFG(BB, TTI, DL, AT) | true;
03931 
03932   if (SwitchToLookupTable(SI, Builder, TTI, DL))
03933     return SimplifyCFG(BB, TTI, DL, AT) | true;
03934 
03935   return false;
03936 }
03937 
03938 bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) {
03939   BasicBlock *BB = IBI->getParent();
03940   bool Changed = false;
03941 
03942   // Eliminate redundant destinations.
03943   SmallPtrSet<Value *, 8> Succs;
03944   for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
03945     BasicBlock *Dest = IBI->getDestination(i);
03946     if (!Dest->hasAddressTaken() || !Succs.insert(Dest)) {
03947       Dest->removePredecessor(BB);
03948       IBI->removeDestination(i);
03949       --i; --e;
03950       Changed = true;
03951     }
03952   }
03953 
03954   if (IBI->getNumDestinations() == 0) {
03955     // If the indirectbr has no successors, change it to unreachable.
03956     new UnreachableInst(IBI->getContext(), IBI);
03957     EraseTerminatorInstAndDCECond(IBI);
03958     return true;
03959   }
03960 
03961   if (IBI->getNumDestinations() == 1) {
03962     // If the indirectbr has one successor, change it to a direct branch.
03963     BranchInst::Create(IBI->getDestination(0), IBI);
03964     EraseTerminatorInstAndDCECond(IBI);
03965     return true;
03966   }
03967 
03968   if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
03969     if (SimplifyIndirectBrOnSelect(IBI, SI))
03970       return SimplifyCFG(BB, TTI, DL, AT) | true;
03971   }
03972   return Changed;
03973 }
03974 
03975 bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder){
03976   BasicBlock *BB = BI->getParent();
03977 
03978   if (SinkCommon && SinkThenElseCodeToEnd(BI))
03979     return true;
03980 
03981   // If the Terminator is the only non-phi instruction, simplify the block.
03982   BasicBlock::iterator I = BB->getFirstNonPHIOrDbg();
03983   if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
03984       TryToSimplifyUncondBranchFromEmptyBlock(BB))
03985     return true;
03986 
03987   // If the only instruction in the block is a seteq/setne comparison
03988   // against a constant, try to simplify the block.
03989   if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
03990     if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
03991       for (++I; isa<DbgInfoIntrinsic>(I); ++I)
03992         ;
03993       if (I->isTerminator() &&
03994           TryToSimplifyUncondBranchWithICmpInIt(ICI, Builder, TTI, DL, AT))
03995         return true;
03996     }
03997 
03998   // If this basic block is ONLY a compare and a branch, and if a predecessor
03999   // branches to us and our successor, fold the comparison into the
04000   // predecessor and use logical operations to update the incoming value
04001   // for PHI nodes in common successor.
04002   if (FoldBranchToCommonDest(BI, DL))
04003     return SimplifyCFG(BB, TTI, DL, AT) | true;
04004   return false;
04005 }
04006 
04007 
04008 bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
04009   BasicBlock *BB = BI->getParent();
04010 
04011   // Conditional branch
04012   if (isValueEqualityComparison(BI)) {
04013     // If we only have one predecessor, and if it is a branch on this value,
04014     // see if that predecessor totally determines the outcome of this
04015     // switch.
04016     if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
04017       if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
04018         return SimplifyCFG(BB, TTI, DL, AT) | true;
04019 
04020     // This block must be empty, except for the setcond inst, if it exists.
04021     // Ignore dbg intrinsics.
04022     BasicBlock::iterator I = BB->begin();
04023     // Ignore dbg intrinsics.
04024     while (isa<DbgInfoIntrinsic>(I))
04025       ++I;
04026     if (&*I == BI) {
04027       if (FoldValueComparisonIntoPredecessors(BI, Builder))
04028         return SimplifyCFG(BB, TTI, DL, AT) | true;
04029     } else if (&*I == cast<Instruction>(BI->getCondition())){
04030       ++I;
04031       // Ignore dbg intrinsics.
04032       while (isa<DbgInfoIntrinsic>(I))
04033         ++I;
04034       if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
04035         return SimplifyCFG(BB, TTI, DL, AT) | true;
04036     }
04037   }
04038 
04039   // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
04040   if (SimplifyBranchOnICmpChain(BI, DL, Builder))
04041     return true;
04042 
04043   // If this basic block is ONLY a compare and a branch, and if a predecessor
04044   // branches to us and one of our successors, fold the comparison into the
04045   // predecessor and use logical operations to pick the right destination.
04046   if (FoldBranchToCommonDest(BI, DL))
04047     return SimplifyCFG(BB, TTI, DL, AT) | true;
04048 
04049   // We have a conditional branch to two blocks that are only reachable
04050   // from BI.  We know that the condbr dominates the two blocks, so see if
04051   // there is any identical code in the "then" and "else" blocks.  If so, we
04052   // can hoist it up to the branching block.
04053   if (BI->getSuccessor(0)->getSinglePredecessor()) {
04054     if (BI->getSuccessor(1)->getSinglePredecessor()) {
04055       if (HoistThenElseCodeToIf(BI, DL))
04056         return SimplifyCFG(BB, TTI, DL, AT) | true;
04057     } else {
04058       // If Successor #1 has multiple preds, we may be able to conditionally
04059       // execute Successor #0 if it branches to Successor #1.
04060       TerminatorInst *Succ0TI = BI->getSuccessor(0)->getTerminator();
04061       if (Succ0TI->getNumSuccessors() == 1 &&
04062           Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
04063         if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), DL))
04064           return SimplifyCFG(BB, TTI, DL, AT) | true;
04065     }
04066   } else if (BI->getSuccessor(1)->getSinglePredecessor()) {
04067     // If Successor #0 has multiple preds, we may be able to conditionally
04068     // execute Successor #1 if it branches to Successor #0.
04069     TerminatorInst *Succ1TI = BI->getSuccessor(1)->getTerminator();
04070     if (Succ1TI->getNumSuccessors() == 1 &&
04071         Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
04072       if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), DL))
04073         return SimplifyCFG(BB, TTI, DL, AT) | true;
04074   }
04075 
04076   // If this is a branch on a phi node in the current block, thread control
04077   // through this block if any PHI node entries are constants.
04078   if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
04079     if (PN->getParent() == BI->getParent())
04080       if (FoldCondBranchOnPHI(BI, DL))
04081         return SimplifyCFG(BB, TTI, DL, AT) | true;
04082 
04083   // Scan predecessor blocks for conditional branches.
04084   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
04085     if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
04086       if (PBI != BI && PBI->isConditional())
04087         if (SimplifyCondBranchToCondBranch(PBI, BI))
04088           return SimplifyCFG(BB, TTI, DL, AT) | true;
04089 
04090   return false;
04091 }
04092 
04093 /// Check if passing a value to an instruction will cause undefined behavior.
04094 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
04095   Constant *C = dyn_cast<Constant>(V);
04096   if (!C)
04097     return false;
04098 
04099   if (I->use_empty())
04100     return false;
04101 
04102   if (C->isNullValue()) {
04103     // Only look at the first use, avoid hurting compile time with long uselists
04104     User *Use = *I->user_begin();
04105 
04106     // Now make sure that there are no instructions in between that can alter
04107     // control flow (eg. calls)
04108     for (BasicBlock::iterator i = ++BasicBlock::iterator(I); &*i != Use; ++i)
04109       if (i == I->getParent()->end() || i->mayHaveSideEffects())
04110         return false;
04111 
04112     // Look through GEPs. A load from a GEP derived from NULL is still undefined
04113     if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
04114       if (GEP->getPointerOperand() == I)
04115         return passingValueIsAlwaysUndefined(V, GEP);
04116 
04117     // Look through bitcasts.
04118     if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
04119       return passingValueIsAlwaysUndefined(V, BC);
04120 
04121     // Load from null is undefined.
04122     if (LoadInst *LI = dyn_cast<LoadInst>(Use))
04123       if (!LI->isVolatile())
04124         return LI->getPointerAddressSpace() == 0;
04125 
04126     // Store to null is undefined.
04127     if (StoreInst *SI = dyn_cast<StoreInst>(Use))
04128       if (!SI->isVolatile())
04129         return SI->getPointerAddressSpace() == 0 && SI->getPointerOperand() == I;
04130   }
04131   return false;
04132 }
04133 
04134 /// If BB has an incoming value that will always trigger undefined behavior
04135 /// (eg. null pointer dereference), remove the branch leading here.
04136 static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
04137   for (BasicBlock::iterator i = BB->begin();
04138        PHINode *PHI = dyn_cast<PHINode>(i); ++i)
04139     for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
04140       if (passingValueIsAlwaysUndefined(PHI->getIncomingValue(i), PHI)) {
04141         TerminatorInst *T = PHI->getIncomingBlock(i)->getTerminator();
04142         IRBuilder<> Builder(T);
04143         if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
04144           BB->removePredecessor(PHI->getIncomingBlock(i));
04145           // Turn uncoditional branches into unreachables and remove the dead
04146           // destination from conditional branches.
04147           if (BI->isUnconditional())
04148             Builder.CreateUnreachable();
04149           else
04150             Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) :
04151                                                          BI->getSuccessor(0));
04152           BI->eraseFromParent();
04153           return true;
04154         }
04155         // TODO: SwitchInst.
04156       }
04157 
04158   return false;
04159 }
04160 
04161 bool SimplifyCFGOpt::run(BasicBlock *BB) {
04162   bool Changed = false;
04163 
04164   assert(BB && BB->getParent() && "Block not embedded in function!");
04165   assert(BB->getTerminator() && "Degenerate basic block encountered!");
04166 
04167   // Remove basic blocks that have no predecessors (except the entry block)...
04168   // or that just have themself as a predecessor.  These are unreachable.
04169   if ((pred_begin(BB) == pred_end(BB) &&
04170        BB != &BB->getParent()->getEntryBlock()) ||
04171       BB->getSinglePredecessor() == BB) {
04172     DEBUG(dbgs() << "Removing BB: \n" << *BB);
04173     DeleteDeadBlock(BB);
04174     return true;
04175   }
04176 
04177   // Check to see if we can constant propagate this terminator instruction
04178   // away...
04179   Changed |= ConstantFoldTerminator(BB, true);
04180 
04181   // Check for and eliminate duplicate PHI nodes in this block.
04182   Changed |= EliminateDuplicatePHINodes(BB);
04183 
04184   // Check for and remove branches that will always cause undefined behavior.
04185   Changed |= removeUndefIntroducingPredecessor(BB);
04186 
04187   // Merge basic blocks into their predecessor if there is only one distinct
04188   // pred, and if there is only one distinct successor of the predecessor, and
04189   // if there are no PHI nodes.
04190   //
04191   if (MergeBlockIntoPredecessor(BB))
04192     return true;
04193 
04194   IRBuilder<> Builder(BB);
04195 
04196   // If there is a trivial two-entry PHI node in this basic block, and we can
04197   // eliminate it, do so now.
04198   if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
04199     if (PN->getNumIncomingValues() == 2)
04200       Changed |= FoldTwoEntryPHINode(PN, DL);
04201 
04202   Builder.SetInsertPoint(BB->getTerminator());
04203   if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
04204     if (BI->isUnconditional()) {
04205       if (SimplifyUncondBranch(BI, Builder)) return true;
04206     } else {
04207       if (SimplifyCondBranch(BI, Builder)) return true;
04208     }
04209   } else if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
04210     if (SimplifyReturn(RI, Builder)) return true;
04211   } else if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
04212     if (SimplifyResume(RI, Builder)) return true;
04213   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
04214     if (SimplifySwitch(SI, Builder)) return true;
04215   } else if (UnreachableInst *UI =
04216                dyn_cast<UnreachableInst>(BB->getTerminator())) {
04217     if (SimplifyUnreachable(UI)) return true;
04218   } else if (IndirectBrInst *IBI =
04219                dyn_cast<IndirectBrInst>(BB->getTerminator())) {
04220     if (SimplifyIndirectBr(IBI)) return true;
04221   }
04222 
04223   return Changed;
04224 }
04225 
04226 /// SimplifyCFG - This function is used to do simplification of a CFG.  For
04227 /// example, it adjusts branches to branches to eliminate the extra hop, it
04228 /// eliminates unreachable basic blocks, and does other "peephole" optimization
04229 /// of the CFG.  It returns true if a modification was made.
04230 ///
04231 bool llvm::SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
04232                        const DataLayout *DL, AssumptionTracker *AT) {
04233   return SimplifyCFGOpt(TTI, DL, AT).run(BB);
04234 }