1 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Peephole optimize the CFG. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/Local.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SetVector.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/Analysis/ConstantFolding.h" 22 #include "llvm/Analysis/InstructionSimplify.h" 23 #include "llvm/Analysis/TargetTransformInfo.h" 24 #include "llvm/Analysis/ValueTracking.h" 25 #include "llvm/IR/CFG.h" 26 #include "llvm/IR/ConstantRange.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/DerivedTypes.h" 30 #include "llvm/IR/GlobalVariable.h" 31 #include "llvm/IR/IRBuilder.h" 32 #include "llvm/IR/Instructions.h" 33 #include "llvm/IR/IntrinsicInst.h" 34 #include "llvm/IR/LLVMContext.h" 35 #include "llvm/IR/MDBuilder.h" 36 #include "llvm/IR/Metadata.h" 37 #include "llvm/IR/Module.h" 38 #include "llvm/IR/NoFolder.h" 39 #include "llvm/IR/Operator.h" 40 #include "llvm/IR/PatternMatch.h" 41 #include "llvm/IR/Type.h" 42 #include "llvm/Support/CommandLine.h" 43 #include "llvm/Support/Debug.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 46 #include "llvm/Transforms/Utils/Local.h" 47 #include "llvm/Transforms/Utils/ValueMapper.h" 48 #include <algorithm> 49 #include <map> 50 #include <set> 51 using namespace llvm; 52 using namespace PatternMatch; 53 54 #define DEBUG_TYPE "simplifycfg" 55 56 // Chosen as 2 so as to be cheap, but still to have enough power to fold 57 // a select, so the "clamp" idiom (of a min followed by a max) will be caught. 58 // To catch this, we need to fold a compare and a select, hence '2' being the 59 // minimum reasonable default. 60 static cl::opt<unsigned> 61 PHINodeFoldingThreshold("phi-node-folding-threshold", cl::Hidden, cl::init(2), 62 cl::desc("Control the amount of phi node folding to perform (default = 2)")); 63 64 static cl::opt<bool> 65 DupRet("simplifycfg-dup-ret", cl::Hidden, cl::init(false), 66 cl::desc("Duplicate return instructions into unconditional branches")); 67 68 static cl::opt<bool> 69 SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true), 70 cl::desc("Sink common instructions down to the end block")); 71 72 static cl::opt<bool> HoistCondStores( 73 "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true), 74 cl::desc("Hoist conditional stores if an unconditional store precedes")); 75 76 STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps"); 77 STATISTIC(NumLinearMaps, "Number of switch instructions turned into linear mapping"); 78 STATISTIC(NumLookupTables, "Number of switch instructions turned into lookup tables"); 79 STATISTIC(NumLookupTablesHoles, "Number of switch instructions turned into lookup tables (holes checked)"); 80 STATISTIC(NumTableCmpReuses, "Number of reused switch table lookup compares"); 81 STATISTIC(NumSinkCommons, "Number of common instructions sunk down to the end block"); 82 STATISTIC(NumSpeculations, "Number of speculative executed instructions"); 83 84 namespace { 85 // The first field contains the value that the switch produces when a certain 86 // case group is selected, and the second field is a vector containing the 87 // cases composing the case group. 88 typedef SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2> 89 SwitchCaseResultVectorTy; 90 // The first field contains the phi node that generates a result of the switch 91 // and the second field contains the value generated for a certain case in the 92 // switch for that PHI. 93 typedef SmallVector<std::pair<PHINode *, Constant *>, 4> SwitchCaseResultsTy; 94 95 /// ValueEqualityComparisonCase - Represents a case of a switch. 96 struct ValueEqualityComparisonCase { 97 ConstantInt *Value; 98 BasicBlock *Dest; 99 100 ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest) 101 : Value(Value), Dest(Dest) {} 102 103 bool operator<(ValueEqualityComparisonCase RHS) const { 104 // Comparing pointers is ok as we only rely on the order for uniquing. 105 return Value < RHS.Value; 106 } 107 108 bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; } 109 }; 110 111 class SimplifyCFGOpt { 112 const TargetTransformInfo &TTI; 113 const DataLayout &DL; 114 unsigned BonusInstThreshold; 115 AssumptionCache *AC; 116 Value *isValueEqualityComparison(TerminatorInst *TI); 117 BasicBlock *GetValueEqualityComparisonCases(TerminatorInst *TI, 118 std::vector<ValueEqualityComparisonCase> &Cases); 119 bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI, 120 BasicBlock *Pred, 121 IRBuilder<> &Builder); 122 bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI, 123 IRBuilder<> &Builder); 124 125 bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder); 126 bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder); 127 bool SimplifyCleanupReturn(CleanupReturnInst *RI); 128 bool SimplifyUnreachable(UnreachableInst *UI); 129 bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder); 130 bool SimplifyIndirectBr(IndirectBrInst *IBI); 131 bool SimplifyUncondBranch(BranchInst *BI, IRBuilder <> &Builder); 132 bool SimplifyCondBranch(BranchInst *BI, IRBuilder <>&Builder); 133 134 public: 135 SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout &DL, 136 unsigned BonusInstThreshold, AssumptionCache *AC) 137 : TTI(TTI), DL(DL), BonusInstThreshold(BonusInstThreshold), AC(AC) {} 138 bool run(BasicBlock *BB); 139 }; 140 } 141 142 /// Return true if it is safe to merge these two 143 /// terminator instructions together. 144 static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) { 145 if (SI1 == SI2) return false; // Can't merge with self! 146 147 // It is not safe to merge these two switch instructions if they have a common 148 // successor, and if that successor has a PHI node, and if *that* PHI node has 149 // conflicting incoming values from the two switch blocks. 150 BasicBlock *SI1BB = SI1->getParent(); 151 BasicBlock *SI2BB = SI2->getParent(); 152 SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB)); 153 154 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I) 155 if (SI1Succs.count(*I)) 156 for (BasicBlock::iterator BBI = (*I)->begin(); 157 isa<PHINode>(BBI); ++BBI) { 158 PHINode *PN = cast<PHINode>(BBI); 159 if (PN->getIncomingValueForBlock(SI1BB) != 160 PN->getIncomingValueForBlock(SI2BB)) 161 return false; 162 } 163 164 return true; 165 } 166 167 /// Return true if it is safe and profitable to merge these two terminator 168 /// instructions together, where SI1 is an unconditional branch. PhiNodes will 169 /// store all PHI nodes in common successors. 170 static bool isProfitableToFoldUnconditional(BranchInst *SI1, 171 BranchInst *SI2, 172 Instruction *Cond, 173 SmallVectorImpl<PHINode*> &PhiNodes) { 174 if (SI1 == SI2) return false; // Can't merge with self! 175 assert(SI1->isUnconditional() && SI2->isConditional()); 176 177 // We fold the unconditional branch if we can easily update all PHI nodes in 178 // common successors: 179 // 1> We have a constant incoming value for the conditional branch; 180 // 2> We have "Cond" as the incoming value for the unconditional branch; 181 // 3> SI2->getCondition() and Cond have same operands. 182 CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition()); 183 if (!Ci2) return false; 184 if (!(Cond->getOperand(0) == Ci2->getOperand(0) && 185 Cond->getOperand(1) == Ci2->getOperand(1)) && 186 !(Cond->getOperand(0) == Ci2->getOperand(1) && 187 Cond->getOperand(1) == Ci2->getOperand(0))) 188 return false; 189 190 BasicBlock *SI1BB = SI1->getParent(); 191 BasicBlock *SI2BB = SI2->getParent(); 192 SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB)); 193 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I) 194 if (SI1Succs.count(*I)) 195 for (BasicBlock::iterator BBI = (*I)->begin(); 196 isa<PHINode>(BBI); ++BBI) { 197 PHINode *PN = cast<PHINode>(BBI); 198 if (PN->getIncomingValueForBlock(SI1BB) != Cond || 199 !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB))) 200 return false; 201 PhiNodes.push_back(PN); 202 } 203 return true; 204 } 205 206 /// Update PHI nodes in Succ to indicate that there will now be entries in it 207 /// from the 'NewPred' block. The values that will be flowing into the PHI nodes 208 /// will be the same as those coming in from ExistPred, an existing predecessor 209 /// of Succ. 210 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred, 211 BasicBlock *ExistPred) { 212 if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do 213 214 PHINode *PN; 215 for (BasicBlock::iterator I = Succ->begin(); 216 (PN = dyn_cast<PHINode>(I)); ++I) 217 PN->addIncoming(PN->getIncomingValueForBlock(ExistPred), NewPred); 218 } 219 220 /// Compute an abstract "cost" of speculating the given instruction, 221 /// which is assumed to be safe to speculate. TCC_Free means cheap, 222 /// TCC_Basic means less cheap, and TCC_Expensive means prohibitively 223 /// expensive. 224 static unsigned ComputeSpeculationCost(const User *I, 225 const TargetTransformInfo &TTI) { 226 assert(isSafeToSpeculativelyExecute(I) && 227 "Instruction is not safe to speculatively execute!"); 228 return TTI.getUserCost(I); 229 } 230 231 /// If we have a merge point of an "if condition" as accepted above, 232 /// return true if the specified value dominates the block. We 233 /// don't handle the true generality of domination here, just a special case 234 /// which works well enough for us. 235 /// 236 /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to 237 /// see if V (which must be an instruction) and its recursive operands 238 /// that do not dominate BB have a combined cost lower than CostRemaining and 239 /// are non-trapping. If both are true, the instruction is inserted into the 240 /// set and true is returned. 241 /// 242 /// The cost for most non-trapping instructions is defined as 1 except for 243 /// Select whose cost is 2. 244 /// 245 /// After this function returns, CostRemaining is decreased by the cost of 246 /// V plus its non-dominating operands. If that cost is greater than 247 /// CostRemaining, false is returned and CostRemaining is undefined. 248 static bool DominatesMergePoint(Value *V, BasicBlock *BB, 249 SmallPtrSetImpl<Instruction*> *AggressiveInsts, 250 unsigned &CostRemaining, 251 const TargetTransformInfo &TTI) { 252 Instruction *I = dyn_cast<Instruction>(V); 253 if (!I) { 254 // Non-instructions all dominate instructions, but not all constantexprs 255 // can be executed unconditionally. 256 if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) 257 if (C->canTrap()) 258 return false; 259 return true; 260 } 261 BasicBlock *PBB = I->getParent(); 262 263 // We don't want to allow weird loops that might have the "if condition" in 264 // the bottom of this block. 265 if (PBB == BB) return false; 266 267 // If this instruction is defined in a block that contains an unconditional 268 // branch to BB, then it must be in the 'conditional' part of the "if 269 // statement". If not, it definitely dominates the region. 270 BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator()); 271 if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB) 272 return true; 273 274 // If we aren't allowing aggressive promotion anymore, then don't consider 275 // instructions in the 'if region'. 276 if (!AggressiveInsts) return false; 277 278 // If we have seen this instruction before, don't count it again. 279 if (AggressiveInsts->count(I)) return true; 280 281 // Okay, it looks like the instruction IS in the "condition". Check to 282 // see if it's a cheap instruction to unconditionally compute, and if it 283 // only uses stuff defined outside of the condition. If so, hoist it out. 284 if (!isSafeToSpeculativelyExecute(I)) 285 return false; 286 287 unsigned Cost = ComputeSpeculationCost(I, TTI); 288 289 if (Cost > CostRemaining) 290 return false; 291 292 CostRemaining -= Cost; 293 294 // Okay, we can only really hoist these out if their operands do 295 // not take us over the cost threshold. 296 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) 297 if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining, TTI)) 298 return false; 299 // Okay, it's safe to do this! Remember this instruction. 300 AggressiveInsts->insert(I); 301 return true; 302 } 303 304 /// Extract ConstantInt from value, looking through IntToPtr 305 /// and PointerNullValue. Return NULL if value is not a constant int. 306 static ConstantInt *GetConstantInt(Value *V, const DataLayout &DL) { 307 // Normal constant int. 308 ConstantInt *CI = dyn_cast<ConstantInt>(V); 309 if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy()) 310 return CI; 311 312 // This is some kind of pointer constant. Turn it into a pointer-sized 313 // ConstantInt if possible. 314 IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType(V->getType())); 315 316 // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*). 317 if (isa<ConstantPointerNull>(V)) 318 return ConstantInt::get(PtrTy, 0); 319 320 // IntToPtr const int. 321 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 322 if (CE->getOpcode() == Instruction::IntToPtr) 323 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) { 324 // The constant is very likely to have the right type already. 325 if (CI->getType() == PtrTy) 326 return CI; 327 else 328 return cast<ConstantInt> 329 (ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false)); 330 } 331 return nullptr; 332 } 333 334 namespace { 335 336 /// Given a chain of or (||) or and (&&) comparison of a value against a 337 /// constant, this will try to recover the information required for a switch 338 /// structure. 339 /// It will depth-first traverse the chain of comparison, seeking for patterns 340 /// like %a == 12 or %a < 4 and combine them to produce a set of integer 341 /// representing the different cases for the switch. 342 /// Note that if the chain is composed of '||' it will build the set of elements 343 /// that matches the comparisons (i.e. any of this value validate the chain) 344 /// while for a chain of '&&' it will build the set elements that make the test 345 /// fail. 346 struct ConstantComparesGatherer { 347 const DataLayout &DL; 348 Value *CompValue; /// Value found for the switch comparison 349 Value *Extra; /// Extra clause to be checked before the switch 350 SmallVector<ConstantInt *, 8> Vals; /// Set of integers to match in switch 351 unsigned UsedICmps; /// Number of comparisons matched in the and/or chain 352 353 /// Construct and compute the result for the comparison instruction Cond 354 ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) 355 : DL(DL), CompValue(nullptr), Extra(nullptr), UsedICmps(0) { 356 gather(Cond); 357 } 358 359 /// Prevent copy 360 ConstantComparesGatherer(const ConstantComparesGatherer &) = delete; 361 ConstantComparesGatherer & 362 operator=(const ConstantComparesGatherer &) = delete; 363 364 private: 365 366 /// Try to set the current value used for the comparison, it succeeds only if 367 /// it wasn't set before or if the new value is the same as the old one 368 bool setValueOnce(Value *NewVal) { 369 if(CompValue && CompValue != NewVal) return false; 370 CompValue = NewVal; 371 return (CompValue != nullptr); 372 } 373 374 /// Try to match Instruction "I" as a comparison against a constant and 375 /// populates the array Vals with the set of values that match (or do not 376 /// match depending on isEQ). 377 /// Return false on failure. On success, the Value the comparison matched 378 /// against is placed in CompValue. 379 /// If CompValue is already set, the function is expected to fail if a match 380 /// is found but the value compared to is different. 381 bool matchInstruction(Instruction *I, bool isEQ) { 382 // If this is an icmp against a constant, handle this as one of the cases. 383 ICmpInst *ICI; 384 ConstantInt *C; 385 if (!((ICI = dyn_cast<ICmpInst>(I)) && 386 (C = GetConstantInt(I->getOperand(1), DL)))) { 387 return false; 388 } 389 390 Value *RHSVal; 391 ConstantInt *RHSC; 392 393 // Pattern match a special case 394 // (x & ~2^x) == y --> x == y || x == y|2^x 395 // This undoes a transformation done by instcombine to fuse 2 compares. 396 if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ:ICmpInst::ICMP_NE)) { 397 if (match(ICI->getOperand(0), 398 m_And(m_Value(RHSVal), m_ConstantInt(RHSC)))) { 399 APInt Not = ~RHSC->getValue(); 400 if (Not.isPowerOf2()) { 401 // If we already have a value for the switch, it has to match! 402 if(!setValueOnce(RHSVal)) 403 return false; 404 405 Vals.push_back(C); 406 Vals.push_back(ConstantInt::get(C->getContext(), 407 C->getValue() | Not)); 408 UsedICmps++; 409 return true; 410 } 411 } 412 413 // If we already have a value for the switch, it has to match! 414 if(!setValueOnce(ICI->getOperand(0))) 415 return false; 416 417 UsedICmps++; 418 Vals.push_back(C); 419 return ICI->getOperand(0); 420 } 421 422 // If we have "x ult 3", for example, then we can add 0,1,2 to the set. 423 ConstantRange Span = ConstantRange::makeAllowedICmpRegion( 424 ICI->getPredicate(), C->getValue()); 425 426 // Shift the range if the compare is fed by an add. This is the range 427 // compare idiom as emitted by instcombine. 428 Value *CandidateVal = I->getOperand(0); 429 if(match(I->getOperand(0), m_Add(m_Value(RHSVal), m_ConstantInt(RHSC)))) { 430 Span = Span.subtract(RHSC->getValue()); 431 CandidateVal = RHSVal; 432 } 433 434 // If this is an and/!= check, then we are looking to build the set of 435 // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into 436 // x != 0 && x != 1. 437 if (!isEQ) 438 Span = Span.inverse(); 439 440 // If there are a ton of values, we don't want to make a ginormous switch. 441 if (Span.getSetSize().ugt(8) || Span.isEmptySet()) { 442 return false; 443 } 444 445 // If we already have a value for the switch, it has to match! 446 if(!setValueOnce(CandidateVal)) 447 return false; 448 449 // Add all values from the range to the set 450 for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp) 451 Vals.push_back(ConstantInt::get(I->getContext(), Tmp)); 452 453 UsedICmps++; 454 return true; 455 456 } 457 458 /// Given a potentially 'or'd or 'and'd together collection of icmp 459 /// eq/ne/lt/gt instructions that compare a value against a constant, extract 460 /// the value being compared, and stick the list constants into the Vals 461 /// vector. 462 /// One "Extra" case is allowed to differ from the other. 463 void gather(Value *V) { 464 Instruction *I = dyn_cast<Instruction>(V); 465 bool isEQ = (I->getOpcode() == Instruction::Or); 466 467 // Keep a stack (SmallVector for efficiency) for depth-first traversal 468 SmallVector<Value *, 8> DFT; 469 470 // Initialize 471 DFT.push_back(V); 472 473 while(!DFT.empty()) { 474 V = DFT.pop_back_val(); 475 476 if (Instruction *I = dyn_cast<Instruction>(V)) { 477 // If it is a || (or && depending on isEQ), process the operands. 478 if (I->getOpcode() == (isEQ ? Instruction::Or : Instruction::And)) { 479 DFT.push_back(I->getOperand(1)); 480 DFT.push_back(I->getOperand(0)); 481 continue; 482 } 483 484 // Try to match the current instruction 485 if (matchInstruction(I, isEQ)) 486 // Match succeed, continue the loop 487 continue; 488 } 489 490 // One element of the sequence of || (or &&) could not be match as a 491 // comparison against the same value as the others. 492 // We allow only one "Extra" case to be checked before the switch 493 if (!Extra) { 494 Extra = V; 495 continue; 496 } 497 // Failed to parse a proper sequence, abort now 498 CompValue = nullptr; 499 break; 500 } 501 } 502 }; 503 504 } 505 506 static void EraseTerminatorInstAndDCECond(TerminatorInst *TI) { 507 Instruction *Cond = nullptr; 508 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 509 Cond = dyn_cast<Instruction>(SI->getCondition()); 510 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 511 if (BI->isConditional()) 512 Cond = dyn_cast<Instruction>(BI->getCondition()); 513 } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) { 514 Cond = dyn_cast<Instruction>(IBI->getAddress()); 515 } 516 517 TI->eraseFromParent(); 518 if (Cond) RecursivelyDeleteTriviallyDeadInstructions(Cond); 519 } 520 521 /// Return true if the specified terminator checks 522 /// to see if a value is equal to constant integer value. 523 Value *SimplifyCFGOpt::isValueEqualityComparison(TerminatorInst *TI) { 524 Value *CV = nullptr; 525 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 526 // Do not permit merging of large switch instructions into their 527 // predecessors unless there is only one predecessor. 528 if (SI->getNumSuccessors()*std::distance(pred_begin(SI->getParent()), 529 pred_end(SI->getParent())) <= 128) 530 CV = SI->getCondition(); 531 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) 532 if (BI->isConditional() && BI->getCondition()->hasOneUse()) 533 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) { 534 if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL)) 535 CV = ICI->getOperand(0); 536 } 537 538 // Unwrap any lossless ptrtoint cast. 539 if (CV) { 540 if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) { 541 Value *Ptr = PTII->getPointerOperand(); 542 if (PTII->getType() == DL.getIntPtrType(Ptr->getType())) 543 CV = Ptr; 544 } 545 } 546 return CV; 547 } 548 549 /// Given a value comparison instruction, 550 /// decode all of the 'cases' that it represents and return the 'default' block. 551 BasicBlock *SimplifyCFGOpt:: 552 GetValueEqualityComparisonCases(TerminatorInst *TI, 553 std::vector<ValueEqualityComparisonCase> 554 &Cases) { 555 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 556 Cases.reserve(SI->getNumCases()); 557 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i) 558 Cases.push_back(ValueEqualityComparisonCase(i.getCaseValue(), 559 i.getCaseSuccessor())); 560 return SI->getDefaultDest(); 561 } 562 563 BranchInst *BI = cast<BranchInst>(TI); 564 ICmpInst *ICI = cast<ICmpInst>(BI->getCondition()); 565 BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE); 566 Cases.push_back(ValueEqualityComparisonCase(GetConstantInt(ICI->getOperand(1), 567 DL), 568 Succ)); 569 return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ); 570 } 571 572 573 /// Given a vector of bb/value pairs, remove any entries 574 /// in the list that match the specified block. 575 static void EliminateBlockCases(BasicBlock *BB, 576 std::vector<ValueEqualityComparisonCase> &Cases) { 577 Cases.erase(std::remove(Cases.begin(), Cases.end(), BB), Cases.end()); 578 } 579 580 /// Return true if there are any keys in C1 that exist in C2 as well. 581 static bool 582 ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1, 583 std::vector<ValueEqualityComparisonCase > &C2) { 584 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2; 585 586 // Make V1 be smaller than V2. 587 if (V1->size() > V2->size()) 588 std::swap(V1, V2); 589 590 if (V1->size() == 0) return false; 591 if (V1->size() == 1) { 592 // Just scan V2. 593 ConstantInt *TheVal = (*V1)[0].Value; 594 for (unsigned i = 0, e = V2->size(); i != e; ++i) 595 if (TheVal == (*V2)[i].Value) 596 return true; 597 } 598 599 // Otherwise, just sort both lists and compare element by element. 600 array_pod_sort(V1->begin(), V1->end()); 601 array_pod_sort(V2->begin(), V2->end()); 602 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size(); 603 while (i1 != e1 && i2 != e2) { 604 if ((*V1)[i1].Value == (*V2)[i2].Value) 605 return true; 606 if ((*V1)[i1].Value < (*V2)[i2].Value) 607 ++i1; 608 else 609 ++i2; 610 } 611 return false; 612 } 613 614 /// If TI is known to be a terminator instruction and its block is known to 615 /// only have a single predecessor block, check to see if that predecessor is 616 /// also a value comparison with the same value, and if that comparison 617 /// determines the outcome of this comparison. If so, simplify TI. This does a 618 /// very limited form of jump threading. 619 bool SimplifyCFGOpt:: 620 SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI, 621 BasicBlock *Pred, 622 IRBuilder<> &Builder) { 623 Value *PredVal = isValueEqualityComparison(Pred->getTerminator()); 624 if (!PredVal) return false; // Not a value comparison in predecessor. 625 626 Value *ThisVal = isValueEqualityComparison(TI); 627 assert(ThisVal && "This isn't a value comparison!!"); 628 if (ThisVal != PredVal) return false; // Different predicates. 629 630 // TODO: Preserve branch weight metadata, similarly to how 631 // FoldValueComparisonIntoPredecessors preserves it. 632 633 // Find out information about when control will move from Pred to TI's block. 634 std::vector<ValueEqualityComparisonCase> PredCases; 635 BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(), 636 PredCases); 637 EliminateBlockCases(PredDef, PredCases); // Remove default from cases. 638 639 // Find information about how control leaves this block. 640 std::vector<ValueEqualityComparisonCase> ThisCases; 641 BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases); 642 EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases. 643 644 // If TI's block is the default block from Pred's comparison, potentially 645 // simplify TI based on this knowledge. 646 if (PredDef == TI->getParent()) { 647 // If we are here, we know that the value is none of those cases listed in 648 // PredCases. If there are any cases in ThisCases that are in PredCases, we 649 // can simplify TI. 650 if (!ValuesOverlap(PredCases, ThisCases)) 651 return false; 652 653 if (isa<BranchInst>(TI)) { 654 // Okay, one of the successors of this condbr is dead. Convert it to a 655 // uncond br. 656 assert(ThisCases.size() == 1 && "Branch can only have one case!"); 657 // Insert the new branch. 658 Instruction *NI = Builder.CreateBr(ThisDef); 659 (void) NI; 660 661 // Remove PHI node entries for the dead edge. 662 ThisCases[0].Dest->removePredecessor(TI->getParent()); 663 664 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator() 665 << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n"); 666 667 EraseTerminatorInstAndDCECond(TI); 668 return true; 669 } 670 671 SwitchInst *SI = cast<SwitchInst>(TI); 672 // Okay, TI has cases that are statically dead, prune them away. 673 SmallPtrSet<Constant*, 16> DeadCases; 674 for (unsigned i = 0, e = PredCases.size(); i != e; ++i) 675 DeadCases.insert(PredCases[i].Value); 676 677 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator() 678 << "Through successor TI: " << *TI); 679 680 // Collect branch weights into a vector. 681 SmallVector<uint32_t, 8> Weights; 682 MDNode *MD = SI->getMetadata(LLVMContext::MD_prof); 683 bool HasWeight = MD && (MD->getNumOperands() == 2 + SI->getNumCases()); 684 if (HasWeight) 685 for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; 686 ++MD_i) { 687 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i)); 688 Weights.push_back(CI->getValue().getZExtValue()); 689 } 690 for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) { 691 --i; 692 if (DeadCases.count(i.getCaseValue())) { 693 if (HasWeight) { 694 std::swap(Weights[i.getCaseIndex()+1], Weights.back()); 695 Weights.pop_back(); 696 } 697 i.getCaseSuccessor()->removePredecessor(TI->getParent()); 698 SI->removeCase(i); 699 } 700 } 701 if (HasWeight && Weights.size() >= 2) 702 SI->setMetadata(LLVMContext::MD_prof, 703 MDBuilder(SI->getParent()->getContext()). 704 createBranchWeights(Weights)); 705 706 DEBUG(dbgs() << "Leaving: " << *TI << "\n"); 707 return true; 708 } 709 710 // Otherwise, TI's block must correspond to some matched value. Find out 711 // which value (or set of values) this is. 712 ConstantInt *TIV = nullptr; 713 BasicBlock *TIBB = TI->getParent(); 714 for (unsigned i = 0, e = PredCases.size(); i != e; ++i) 715 if (PredCases[i].Dest == TIBB) { 716 if (TIV) 717 return false; // Cannot handle multiple values coming to this block. 718 TIV = PredCases[i].Value; 719 } 720 assert(TIV && "No edge from pred to succ?"); 721 722 // Okay, we found the one constant that our value can be if we get into TI's 723 // BB. Find out which successor will unconditionally be branched to. 724 BasicBlock *TheRealDest = nullptr; 725 for (unsigned i = 0, e = ThisCases.size(); i != e; ++i) 726 if (ThisCases[i].Value == TIV) { 727 TheRealDest = ThisCases[i].Dest; 728 break; 729 } 730 731 // If not handled by any explicit cases, it is handled by the default case. 732 if (!TheRealDest) TheRealDest = ThisDef; 733 734 // Remove PHI node entries for dead edges. 735 BasicBlock *CheckEdge = TheRealDest; 736 for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI) 737 if (*SI != CheckEdge) 738 (*SI)->removePredecessor(TIBB); 739 else 740 CheckEdge = nullptr; 741 742 // Insert the new branch. 743 Instruction *NI = Builder.CreateBr(TheRealDest); 744 (void) NI; 745 746 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator() 747 << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n"); 748 749 EraseTerminatorInstAndDCECond(TI); 750 return true; 751 } 752 753 namespace { 754 /// This class implements a stable ordering of constant 755 /// integers that does not depend on their address. This is important for 756 /// applications that sort ConstantInt's to ensure uniqueness. 757 struct ConstantIntOrdering { 758 bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const { 759 return LHS->getValue().ult(RHS->getValue()); 760 } 761 }; 762 } 763 764 static int ConstantIntSortPredicate(ConstantInt *const *P1, 765 ConstantInt *const *P2) { 766 const ConstantInt *LHS = *P1; 767 const ConstantInt *RHS = *P2; 768 if (LHS->getValue().ult(RHS->getValue())) 769 return 1; 770 if (LHS->getValue() == RHS->getValue()) 771 return 0; 772 return -1; 773 } 774 775 static inline bool HasBranchWeights(const Instruction* I) { 776 MDNode *ProfMD = I->getMetadata(LLVMContext::MD_prof); 777 if (ProfMD && ProfMD->getOperand(0)) 778 if (MDString* MDS = dyn_cast<MDString>(ProfMD->getOperand(0))) 779 return MDS->getString().equals("branch_weights"); 780 781 return false; 782 } 783 784 /// Get Weights of a given TerminatorInst, the default weight is at the front 785 /// of the vector. If TI is a conditional eq, we need to swap the branch-weight 786 /// metadata. 787 static void GetBranchWeights(TerminatorInst *TI, 788 SmallVectorImpl<uint64_t> &Weights) { 789 MDNode *MD = TI->getMetadata(LLVMContext::MD_prof); 790 assert(MD); 791 for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) { 792 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i)); 793 Weights.push_back(CI->getValue().getZExtValue()); 794 } 795 796 // If TI is a conditional eq, the default case is the false case, 797 // and the corresponding branch-weight data is at index 2. We swap the 798 // default weight to be the first entry. 799 if (BranchInst* BI = dyn_cast<BranchInst>(TI)) { 800 assert(Weights.size() == 2); 801 ICmpInst *ICI = cast<ICmpInst>(BI->getCondition()); 802 if (ICI->getPredicate() == ICmpInst::ICMP_EQ) 803 std::swap(Weights.front(), Weights.back()); 804 } 805 } 806 807 /// Keep halving the weights until all can fit in uint32_t. 808 static void FitWeights(MutableArrayRef<uint64_t> Weights) { 809 uint64_t Max = *std::max_element(Weights.begin(), Weights.end()); 810 if (Max > UINT_MAX) { 811 unsigned Offset = 32 - countLeadingZeros(Max); 812 for (uint64_t &I : Weights) 813 I >>= Offset; 814 } 815 } 816 817 /// The specified terminator is a value equality comparison instruction 818 /// (either a switch or a branch on "X == c"). 819 /// See if any of the predecessors of the terminator block are value comparisons 820 /// on the same value. If so, and if safe to do so, fold them together. 821 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(TerminatorInst *TI, 822 IRBuilder<> &Builder) { 823 BasicBlock *BB = TI->getParent(); 824 Value *CV = isValueEqualityComparison(TI); // CondVal 825 assert(CV && "Not a comparison?"); 826 bool Changed = false; 827 828 SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB)); 829 while (!Preds.empty()) { 830 BasicBlock *Pred = Preds.pop_back_val(); 831 832 // See if the predecessor is a comparison with the same value. 833 TerminatorInst *PTI = Pred->getTerminator(); 834 Value *PCV = isValueEqualityComparison(PTI); // PredCondVal 835 836 if (PCV == CV && SafeToMergeTerminators(TI, PTI)) { 837 // Figure out which 'cases' to copy from SI to PSI. 838 std::vector<ValueEqualityComparisonCase> BBCases; 839 BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases); 840 841 std::vector<ValueEqualityComparisonCase> PredCases; 842 BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases); 843 844 // Based on whether the default edge from PTI goes to BB or not, fill in 845 // PredCases and PredDefault with the new switch cases we would like to 846 // build. 847 SmallVector<BasicBlock*, 8> NewSuccessors; 848 849 // Update the branch weight metadata along the way 850 SmallVector<uint64_t, 8> Weights; 851 bool PredHasWeights = HasBranchWeights(PTI); 852 bool SuccHasWeights = HasBranchWeights(TI); 853 854 if (PredHasWeights) { 855 GetBranchWeights(PTI, Weights); 856 // branch-weight metadata is inconsistent here. 857 if (Weights.size() != 1 + PredCases.size()) 858 PredHasWeights = SuccHasWeights = false; 859 } else if (SuccHasWeights) 860 // If there are no predecessor weights but there are successor weights, 861 // populate Weights with 1, which will later be scaled to the sum of 862 // successor's weights 863 Weights.assign(1 + PredCases.size(), 1); 864 865 SmallVector<uint64_t, 8> SuccWeights; 866 if (SuccHasWeights) { 867 GetBranchWeights(TI, SuccWeights); 868 // branch-weight metadata is inconsistent here. 869 if (SuccWeights.size() != 1 + BBCases.size()) 870 PredHasWeights = SuccHasWeights = false; 871 } else if (PredHasWeights) 872 SuccWeights.assign(1 + BBCases.size(), 1); 873 874 if (PredDefault == BB) { 875 // If this is the default destination from PTI, only the edges in TI 876 // that don't occur in PTI, or that branch to BB will be activated. 877 std::set<ConstantInt*, ConstantIntOrdering> PTIHandled; 878 for (unsigned i = 0, e = PredCases.size(); i != e; ++i) 879 if (PredCases[i].Dest != BB) 880 PTIHandled.insert(PredCases[i].Value); 881 else { 882 // The default destination is BB, we don't need explicit targets. 883 std::swap(PredCases[i], PredCases.back()); 884 885 if (PredHasWeights || SuccHasWeights) { 886 // Increase weight for the default case. 887 Weights[0] += Weights[i+1]; 888 std::swap(Weights[i+1], Weights.back()); 889 Weights.pop_back(); 890 } 891 892 PredCases.pop_back(); 893 --i; --e; 894 } 895 896 // Reconstruct the new switch statement we will be building. 897 if (PredDefault != BBDefault) { 898 PredDefault->removePredecessor(Pred); 899 PredDefault = BBDefault; 900 NewSuccessors.push_back(BBDefault); 901 } 902 903 unsigned CasesFromPred = Weights.size(); 904 uint64_t ValidTotalSuccWeight = 0; 905 for (unsigned i = 0, e = BBCases.size(); i != e; ++i) 906 if (!PTIHandled.count(BBCases[i].Value) && 907 BBCases[i].Dest != BBDefault) { 908 PredCases.push_back(BBCases[i]); 909 NewSuccessors.push_back(BBCases[i].Dest); 910 if (SuccHasWeights || PredHasWeights) { 911 // The default weight is at index 0, so weight for the ith case 912 // should be at index i+1. Scale the cases from successor by 913 // PredDefaultWeight (Weights[0]). 914 Weights.push_back(Weights[0] * SuccWeights[i+1]); 915 ValidTotalSuccWeight += SuccWeights[i+1]; 916 } 917 } 918 919 if (SuccHasWeights || PredHasWeights) { 920 ValidTotalSuccWeight += SuccWeights[0]; 921 // Scale the cases from predecessor by ValidTotalSuccWeight. 922 for (unsigned i = 1; i < CasesFromPred; ++i) 923 Weights[i] *= ValidTotalSuccWeight; 924 // Scale the default weight by SuccDefaultWeight (SuccWeights[0]). 925 Weights[0] *= SuccWeights[0]; 926 } 927 } else { 928 // If this is not the default destination from PSI, only the edges 929 // in SI that occur in PSI with a destination of BB will be 930 // activated. 931 std::set<ConstantInt*, ConstantIntOrdering> PTIHandled; 932 std::map<ConstantInt*, uint64_t> WeightsForHandled; 933 for (unsigned i = 0, e = PredCases.size(); i != e; ++i) 934 if (PredCases[i].Dest == BB) { 935 PTIHandled.insert(PredCases[i].Value); 936 937 if (PredHasWeights || SuccHasWeights) { 938 WeightsForHandled[PredCases[i].Value] = Weights[i+1]; 939 std::swap(Weights[i+1], Weights.back()); 940 Weights.pop_back(); 941 } 942 943 std::swap(PredCases[i], PredCases.back()); 944 PredCases.pop_back(); 945 --i; --e; 946 } 947 948 // Okay, now we know which constants were sent to BB from the 949 // predecessor. Figure out where they will all go now. 950 for (unsigned i = 0, e = BBCases.size(); i != e; ++i) 951 if (PTIHandled.count(BBCases[i].Value)) { 952 // If this is one we are capable of getting... 953 if (PredHasWeights || SuccHasWeights) 954 Weights.push_back(WeightsForHandled[BBCases[i].Value]); 955 PredCases.push_back(BBCases[i]); 956 NewSuccessors.push_back(BBCases[i].Dest); 957 PTIHandled.erase(BBCases[i].Value);// This constant is taken care of 958 } 959 960 // If there are any constants vectored to BB that TI doesn't handle, 961 // they must go to the default destination of TI. 962 for (std::set<ConstantInt*, ConstantIntOrdering>::iterator I = 963 PTIHandled.begin(), 964 E = PTIHandled.end(); I != E; ++I) { 965 if (PredHasWeights || SuccHasWeights) 966 Weights.push_back(WeightsForHandled[*I]); 967 PredCases.push_back(ValueEqualityComparisonCase(*I, BBDefault)); 968 NewSuccessors.push_back(BBDefault); 969 } 970 } 971 972 // Okay, at this point, we know which new successor Pred will get. Make 973 // sure we update the number of entries in the PHI nodes for these 974 // successors. 975 for (BasicBlock *NewSuccessor : NewSuccessors) 976 AddPredecessorToBlock(NewSuccessor, Pred, BB); 977 978 Builder.SetInsertPoint(PTI); 979 // Convert pointer to int before we switch. 980 if (CV->getType()->isPointerTy()) { 981 CV = Builder.CreatePtrToInt(CV, DL.getIntPtrType(CV->getType()), 982 "magicptr"); 983 } 984 985 // Now that the successors are updated, create the new Switch instruction. 986 SwitchInst *NewSI = Builder.CreateSwitch(CV, PredDefault, 987 PredCases.size()); 988 NewSI->setDebugLoc(PTI->getDebugLoc()); 989 for (ValueEqualityComparisonCase &V : PredCases) 990 NewSI->addCase(V.Value, V.Dest); 991 992 if (PredHasWeights || SuccHasWeights) { 993 // Halve the weights if any of them cannot fit in an uint32_t 994 FitWeights(Weights); 995 996 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end()); 997 998 NewSI->setMetadata(LLVMContext::MD_prof, 999 MDBuilder(BB->getContext()). 1000 createBranchWeights(MDWeights)); 1001 } 1002 1003 EraseTerminatorInstAndDCECond(PTI); 1004 1005 // Okay, last check. If BB is still a successor of PSI, then we must 1006 // have an infinite loop case. If so, add an infinitely looping block 1007 // to handle the case to preserve the behavior of the code. 1008 BasicBlock *InfLoopBlock = nullptr; 1009 for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i) 1010 if (NewSI->getSuccessor(i) == BB) { 1011 if (!InfLoopBlock) { 1012 // Insert it at the end of the function, because it's either code, 1013 // or it won't matter if it's hot. :) 1014 InfLoopBlock = BasicBlock::Create(BB->getContext(), 1015 "infloop", BB->getParent()); 1016 BranchInst::Create(InfLoopBlock, InfLoopBlock); 1017 } 1018 NewSI->setSuccessor(i, InfLoopBlock); 1019 } 1020 1021 Changed = true; 1022 } 1023 } 1024 return Changed; 1025 } 1026 1027 // If we would need to insert a select that uses the value of this invoke 1028 // (comments in HoistThenElseCodeToIf explain why we would need to do this), we 1029 // can't hoist the invoke, as there is nowhere to put the select in this case. 1030 static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2, 1031 Instruction *I1, Instruction *I2) { 1032 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) { 1033 PHINode *PN; 1034 for (BasicBlock::iterator BBI = SI->begin(); 1035 (PN = dyn_cast<PHINode>(BBI)); ++BBI) { 1036 Value *BB1V = PN->getIncomingValueForBlock(BB1); 1037 Value *BB2V = PN->getIncomingValueForBlock(BB2); 1038 if (BB1V != BB2V && (BB1V==I1 || BB2V==I2)) { 1039 return false; 1040 } 1041 } 1042 } 1043 return true; 1044 } 1045 1046 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I); 1047 1048 /// Given a conditional branch that goes to BB1 and BB2, hoist any common code 1049 /// in the two blocks up into the branch block. The caller of this function 1050 /// guarantees that BI's block dominates BB1 and BB2. 1051 static bool HoistThenElseCodeToIf(BranchInst *BI, 1052 const TargetTransformInfo &TTI) { 1053 // This does very trivial matching, with limited scanning, to find identical 1054 // instructions in the two blocks. In particular, we don't want to get into 1055 // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As 1056 // such, we currently just scan for obviously identical instructions in an 1057 // identical order. 1058 BasicBlock *BB1 = BI->getSuccessor(0); // The true destination. 1059 BasicBlock *BB2 = BI->getSuccessor(1); // The false destination 1060 1061 BasicBlock::iterator BB1_Itr = BB1->begin(); 1062 BasicBlock::iterator BB2_Itr = BB2->begin(); 1063 1064 Instruction *I1 = BB1_Itr++, *I2 = BB2_Itr++; 1065 // Skip debug info if it is not identical. 1066 DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1); 1067 DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2); 1068 if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) { 1069 while (isa<DbgInfoIntrinsic>(I1)) 1070 I1 = BB1_Itr++; 1071 while (isa<DbgInfoIntrinsic>(I2)) 1072 I2 = BB2_Itr++; 1073 } 1074 if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) || 1075 (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))) 1076 return false; 1077 1078 BasicBlock *BIParent = BI->getParent(); 1079 1080 bool Changed = false; 1081 do { 1082 // If we are hoisting the terminator instruction, don't move one (making a 1083 // broken BB), instead clone it, and remove BI. 1084 if (isa<TerminatorInst>(I1)) 1085 goto HoistTerminator; 1086 1087 if (!TTI.isProfitableToHoist(I1) || !TTI.isProfitableToHoist(I2)) 1088 return Changed; 1089 1090 // For a normal instruction, we just move one to right before the branch, 1091 // then replace all uses of the other with the first. Finally, we remove 1092 // the now redundant second instruction. 1093 BIParent->getInstList().splice(BI, BB1->getInstList(), I1); 1094 if (!I2->use_empty()) 1095 I2->replaceAllUsesWith(I1); 1096 I1->intersectOptionalDataWith(I2); 1097 unsigned KnownIDs[] = { 1098 LLVMContext::MD_tbaa, 1099 LLVMContext::MD_range, 1100 LLVMContext::MD_fpmath, 1101 LLVMContext::MD_invariant_load, 1102 LLVMContext::MD_nonnull 1103 }; 1104 combineMetadata(I1, I2, KnownIDs); 1105 I2->eraseFromParent(); 1106 Changed = true; 1107 1108 I1 = BB1_Itr++; 1109 I2 = BB2_Itr++; 1110 // Skip debug info if it is not identical. 1111 DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1); 1112 DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2); 1113 if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) { 1114 while (isa<DbgInfoIntrinsic>(I1)) 1115 I1 = BB1_Itr++; 1116 while (isa<DbgInfoIntrinsic>(I2)) 1117 I2 = BB2_Itr++; 1118 } 1119 } while (I1->isIdenticalToWhenDefined(I2)); 1120 1121 return true; 1122 1123 HoistTerminator: 1124 // It may not be possible to hoist an invoke. 1125 if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)) 1126 return Changed; 1127 1128 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) { 1129 PHINode *PN; 1130 for (BasicBlock::iterator BBI = SI->begin(); 1131 (PN = dyn_cast<PHINode>(BBI)); ++BBI) { 1132 Value *BB1V = PN->getIncomingValueForBlock(BB1); 1133 Value *BB2V = PN->getIncomingValueForBlock(BB2); 1134 if (BB1V == BB2V) 1135 continue; 1136 1137 // Check for passingValueIsAlwaysUndefined here because we would rather 1138 // eliminate undefined control flow then converting it to a select. 1139 if (passingValueIsAlwaysUndefined(BB1V, PN) || 1140 passingValueIsAlwaysUndefined(BB2V, PN)) 1141 return Changed; 1142 1143 if (isa<ConstantExpr>(BB1V) && !isSafeToSpeculativelyExecute(BB1V)) 1144 return Changed; 1145 if (isa<ConstantExpr>(BB2V) && !isSafeToSpeculativelyExecute(BB2V)) 1146 return Changed; 1147 } 1148 } 1149 1150 // Okay, it is safe to hoist the terminator. 1151 Instruction *NT = I1->clone(); 1152 BIParent->getInstList().insert(BI, NT); 1153 if (!NT->getType()->isVoidTy()) { 1154 I1->replaceAllUsesWith(NT); 1155 I2->replaceAllUsesWith(NT); 1156 NT->takeName(I1); 1157 } 1158 1159 IRBuilder<true, NoFolder> Builder(NT); 1160 // Hoisting one of the terminators from our successor is a great thing. 1161 // Unfortunately, the successors of the if/else blocks may have PHI nodes in 1162 // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI 1163 // nodes, so we insert select instruction to compute the final result. 1164 std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects; 1165 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) { 1166 PHINode *PN; 1167 for (BasicBlock::iterator BBI = SI->begin(); 1168 (PN = dyn_cast<PHINode>(BBI)); ++BBI) { 1169 Value *BB1V = PN->getIncomingValueForBlock(BB1); 1170 Value *BB2V = PN->getIncomingValueForBlock(BB2); 1171 if (BB1V == BB2V) continue; 1172 1173 // These values do not agree. Insert a select instruction before NT 1174 // that determines the right value. 1175 SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)]; 1176 if (!SI) 1177 SI = cast<SelectInst> 1178 (Builder.CreateSelect(BI->getCondition(), BB1V, BB2V, 1179 BB1V->getName()+"."+BB2V->getName())); 1180 1181 // Make the PHI node use the select for all incoming values for BB1/BB2 1182 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 1183 if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2) 1184 PN->setIncomingValue(i, SI); 1185 } 1186 } 1187 1188 // Update any PHI nodes in our new successors. 1189 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) 1190 AddPredecessorToBlock(*SI, BIParent, BB1); 1191 1192 EraseTerminatorInstAndDCECond(BI); 1193 return true; 1194 } 1195 1196 /// Given an unconditional branch that goes to BBEnd, 1197 /// check whether BBEnd has only two predecessors and the other predecessor 1198 /// ends with an unconditional branch. If it is true, sink any common code 1199 /// in the two predecessors to BBEnd. 1200 static bool SinkThenElseCodeToEnd(BranchInst *BI1) { 1201 assert(BI1->isUnconditional()); 1202 BasicBlock *BB1 = BI1->getParent(); 1203 BasicBlock *BBEnd = BI1->getSuccessor(0); 1204 1205 // Check that BBEnd has two predecessors and the other predecessor ends with 1206 // an unconditional branch. 1207 pred_iterator PI = pred_begin(BBEnd), PE = pred_end(BBEnd); 1208 BasicBlock *Pred0 = *PI++; 1209 if (PI == PE) // Only one predecessor. 1210 return false; 1211 BasicBlock *Pred1 = *PI++; 1212 if (PI != PE) // More than two predecessors. 1213 return false; 1214 BasicBlock *BB2 = (Pred0 == BB1) ? Pred1 : Pred0; 1215 BranchInst *BI2 = dyn_cast<BranchInst>(BB2->getTerminator()); 1216 if (!BI2 || !BI2->isUnconditional()) 1217 return false; 1218 1219 // Gather the PHI nodes in BBEnd. 1220 SmallDenseMap<std::pair<Value *, Value *>, PHINode *> JointValueMap; 1221 Instruction *FirstNonPhiInBBEnd = nullptr; 1222 for (BasicBlock::iterator I = BBEnd->begin(), E = BBEnd->end(); I != E; ++I) { 1223 if (PHINode *PN = dyn_cast<PHINode>(I)) { 1224 Value *BB1V = PN->getIncomingValueForBlock(BB1); 1225 Value *BB2V = PN->getIncomingValueForBlock(BB2); 1226 JointValueMap[std::make_pair(BB1V, BB2V)] = PN; 1227 } else { 1228 FirstNonPhiInBBEnd = &*I; 1229 break; 1230 } 1231 } 1232 if (!FirstNonPhiInBBEnd) 1233 return false; 1234 1235 // This does very trivial matching, with limited scanning, to find identical 1236 // instructions in the two blocks. We scan backward for obviously identical 1237 // instructions in an identical order. 1238 BasicBlock::InstListType::reverse_iterator RI1 = BB1->getInstList().rbegin(), 1239 RE1 = BB1->getInstList().rend(), 1240 RI2 = BB2->getInstList().rbegin(), 1241 RE2 = BB2->getInstList().rend(); 1242 // Skip debug info. 1243 while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1; 1244 if (RI1 == RE1) 1245 return false; 1246 while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2; 1247 if (RI2 == RE2) 1248 return false; 1249 // Skip the unconditional branches. 1250 ++RI1; 1251 ++RI2; 1252 1253 bool Changed = false; 1254 while (RI1 != RE1 && RI2 != RE2) { 1255 // Skip debug info. 1256 while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1; 1257 if (RI1 == RE1) 1258 return Changed; 1259 while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2; 1260 if (RI2 == RE2) 1261 return Changed; 1262 1263 Instruction *I1 = &*RI1, *I2 = &*RI2; 1264 auto InstPair = std::make_pair(I1, I2); 1265 // I1 and I2 should have a single use in the same PHI node, and they 1266 // perform the same operation. 1267 // Cannot move control-flow-involving, volatile loads, vaarg, etc. 1268 if (isa<PHINode>(I1) || isa<PHINode>(I2) || 1269 isa<TerminatorInst>(I1) || isa<TerminatorInst>(I2) || 1270 I1->isEHPad() || I2->isEHPad() || 1271 isa<AllocaInst>(I1) || isa<AllocaInst>(I2) || 1272 I1->mayHaveSideEffects() || I2->mayHaveSideEffects() || 1273 I1->mayReadOrWriteMemory() || I2->mayReadOrWriteMemory() || 1274 !I1->hasOneUse() || !I2->hasOneUse() || 1275 !JointValueMap.count(InstPair)) 1276 return Changed; 1277 1278 // Check whether we should swap the operands of ICmpInst. 1279 // TODO: Add support of communativity. 1280 ICmpInst *ICmp1 = dyn_cast<ICmpInst>(I1), *ICmp2 = dyn_cast<ICmpInst>(I2); 1281 bool SwapOpnds = false; 1282 if (ICmp1 && ICmp2 && 1283 ICmp1->getOperand(0) != ICmp2->getOperand(0) && 1284 ICmp1->getOperand(1) != ICmp2->getOperand(1) && 1285 (ICmp1->getOperand(0) == ICmp2->getOperand(1) || 1286 ICmp1->getOperand(1) == ICmp2->getOperand(0))) { 1287 ICmp2->swapOperands(); 1288 SwapOpnds = true; 1289 } 1290 if (!I1->isSameOperationAs(I2)) { 1291 if (SwapOpnds) 1292 ICmp2->swapOperands(); 1293 return Changed; 1294 } 1295 1296 // The operands should be either the same or they need to be generated 1297 // with a PHI node after sinking. We only handle the case where there is 1298 // a single pair of different operands. 1299 Value *DifferentOp1 = nullptr, *DifferentOp2 = nullptr; 1300 unsigned Op1Idx = ~0U; 1301 for (unsigned I = 0, E = I1->getNumOperands(); I != E; ++I) { 1302 if (I1->getOperand(I) == I2->getOperand(I)) 1303 continue; 1304 // Early exit if we have more-than one pair of different operands or if 1305 // we need a PHI node to replace a constant. 1306 if (Op1Idx != ~0U || 1307 isa<Constant>(I1->getOperand(I)) || 1308 isa<Constant>(I2->getOperand(I))) { 1309 // If we can't sink the instructions, undo the swapping. 1310 if (SwapOpnds) 1311 ICmp2->swapOperands(); 1312 return Changed; 1313 } 1314 DifferentOp1 = I1->getOperand(I); 1315 Op1Idx = I; 1316 DifferentOp2 = I2->getOperand(I); 1317 } 1318 1319 DEBUG(dbgs() << "SINK common instructions " << *I1 << "\n"); 1320 DEBUG(dbgs() << " " << *I2 << "\n"); 1321 1322 // We insert the pair of different operands to JointValueMap and 1323 // remove (I1, I2) from JointValueMap. 1324 if (Op1Idx != ~0U) { 1325 auto &NewPN = JointValueMap[std::make_pair(DifferentOp1, DifferentOp2)]; 1326 if (!NewPN) { 1327 NewPN = 1328 PHINode::Create(DifferentOp1->getType(), 2, 1329 DifferentOp1->getName() + ".sink", BBEnd->begin()); 1330 NewPN->addIncoming(DifferentOp1, BB1); 1331 NewPN->addIncoming(DifferentOp2, BB2); 1332 DEBUG(dbgs() << "Create PHI node " << *NewPN << "\n";); 1333 } 1334 // I1 should use NewPN instead of DifferentOp1. 1335 I1->setOperand(Op1Idx, NewPN); 1336 } 1337 PHINode *OldPN = JointValueMap[InstPair]; 1338 JointValueMap.erase(InstPair); 1339 1340 // We need to update RE1 and RE2 if we are going to sink the first 1341 // instruction in the basic block down. 1342 bool UpdateRE1 = (I1 == BB1->begin()), UpdateRE2 = (I2 == BB2->begin()); 1343 // Sink the instruction. 1344 BBEnd->getInstList().splice(FirstNonPhiInBBEnd, BB1->getInstList(), I1); 1345 if (!OldPN->use_empty()) 1346 OldPN->replaceAllUsesWith(I1); 1347 OldPN->eraseFromParent(); 1348 1349 if (!I2->use_empty()) 1350 I2->replaceAllUsesWith(I1); 1351 I1->intersectOptionalDataWith(I2); 1352 // TODO: Use combineMetadata here to preserve what metadata we can 1353 // (analogous to the hoisting case above). 1354 I2->eraseFromParent(); 1355 1356 if (UpdateRE1) 1357 RE1 = BB1->getInstList().rend(); 1358 if (UpdateRE2) 1359 RE2 = BB2->getInstList().rend(); 1360 FirstNonPhiInBBEnd = I1; 1361 NumSinkCommons++; 1362 Changed = true; 1363 } 1364 return Changed; 1365 } 1366 1367 /// \brief Determine if we can hoist sink a sole store instruction out of a 1368 /// conditional block. 1369 /// 1370 /// We are looking for code like the following: 1371 /// BrBB: 1372 /// store i32 %add, i32* %arrayidx2 1373 /// ... // No other stores or function calls (we could be calling a memory 1374 /// ... // function). 1375 /// %cmp = icmp ult %x, %y 1376 /// br i1 %cmp, label %EndBB, label %ThenBB 1377 /// ThenBB: 1378 /// store i32 %add5, i32* %arrayidx2 1379 /// br label EndBB 1380 /// EndBB: 1381 /// ... 1382 /// We are going to transform this into: 1383 /// BrBB: 1384 /// store i32 %add, i32* %arrayidx2 1385 /// ... // 1386 /// %cmp = icmp ult %x, %y 1387 /// %add.add5 = select i1 %cmp, i32 %add, %add5 1388 /// store i32 %add.add5, i32* %arrayidx2 1389 /// ... 1390 /// 1391 /// \return The pointer to the value of the previous store if the store can be 1392 /// hoisted into the predecessor block. 0 otherwise. 1393 static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB, 1394 BasicBlock *StoreBB, BasicBlock *EndBB) { 1395 StoreInst *StoreToHoist = dyn_cast<StoreInst>(I); 1396 if (!StoreToHoist) 1397 return nullptr; 1398 1399 // Volatile or atomic. 1400 if (!StoreToHoist->isSimple()) 1401 return nullptr; 1402 1403 Value *StorePtr = StoreToHoist->getPointerOperand(); 1404 1405 // Look for a store to the same pointer in BrBB. 1406 unsigned MaxNumInstToLookAt = 10; 1407 for (BasicBlock::reverse_iterator RI = BrBB->rbegin(), 1408 RE = BrBB->rend(); RI != RE && (--MaxNumInstToLookAt); ++RI) { 1409 Instruction *CurI = &*RI; 1410 1411 // Could be calling an instruction that effects memory like free(). 1412 if (CurI->mayHaveSideEffects() && !isa<StoreInst>(CurI)) 1413 return nullptr; 1414 1415 StoreInst *SI = dyn_cast<StoreInst>(CurI); 1416 // Found the previous store make sure it stores to the same location. 1417 if (SI && SI->getPointerOperand() == StorePtr) 1418 // Found the previous store, return its value operand. 1419 return SI->getValueOperand(); 1420 else if (SI) 1421 return nullptr; // Unknown store. 1422 } 1423 1424 return nullptr; 1425 } 1426 1427 /// \brief Speculate a conditional basic block flattening the CFG. 1428 /// 1429 /// Note that this is a very risky transform currently. Speculating 1430 /// instructions like this is most often not desirable. Instead, there is an MI 1431 /// pass which can do it with full awareness of the resource constraints. 1432 /// However, some cases are "obvious" and we should do directly. An example of 1433 /// this is speculating a single, reasonably cheap instruction. 1434 /// 1435 /// There is only one distinct advantage to flattening the CFG at the IR level: 1436 /// it makes very common but simplistic optimizations such as are common in 1437 /// instcombine and the DAG combiner more powerful by removing CFG edges and 1438 /// modeling their effects with easier to reason about SSA value graphs. 1439 /// 1440 /// 1441 /// An illustration of this transform is turning this IR: 1442 /// \code 1443 /// BB: 1444 /// %cmp = icmp ult %x, %y 1445 /// br i1 %cmp, label %EndBB, label %ThenBB 1446 /// ThenBB: 1447 /// %sub = sub %x, %y 1448 /// br label BB2 1449 /// EndBB: 1450 /// %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ] 1451 /// ... 1452 /// \endcode 1453 /// 1454 /// Into this IR: 1455 /// \code 1456 /// BB: 1457 /// %cmp = icmp ult %x, %y 1458 /// %sub = sub %x, %y 1459 /// %cond = select i1 %cmp, 0, %sub 1460 /// ... 1461 /// \endcode 1462 /// 1463 /// \returns true if the conditional block is removed. 1464 static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB, 1465 const TargetTransformInfo &TTI) { 1466 // Be conservative for now. FP select instruction can often be expensive. 1467 Value *BrCond = BI->getCondition(); 1468 if (isa<FCmpInst>(BrCond)) 1469 return false; 1470 1471 BasicBlock *BB = BI->getParent(); 1472 BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0); 1473 1474 // If ThenBB is actually on the false edge of the conditional branch, remember 1475 // to swap the select operands later. 1476 bool Invert = false; 1477 if (ThenBB != BI->getSuccessor(0)) { 1478 assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?"); 1479 Invert = true; 1480 } 1481 assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block"); 1482 1483 // Keep a count of how many times instructions are used within CondBB when 1484 // they are candidates for sinking into CondBB. Specifically: 1485 // - They are defined in BB, and 1486 // - They have no side effects, and 1487 // - All of their uses are in CondBB. 1488 SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts; 1489 1490 unsigned SpeculationCost = 0; 1491 Value *SpeculatedStoreValue = nullptr; 1492 StoreInst *SpeculatedStore = nullptr; 1493 for (BasicBlock::iterator BBI = ThenBB->begin(), 1494 BBE = std::prev(ThenBB->end()); 1495 BBI != BBE; ++BBI) { 1496 Instruction *I = BBI; 1497 // Skip debug info. 1498 if (isa<DbgInfoIntrinsic>(I)) 1499 continue; 1500 1501 // Only speculatively execute a single instruction (not counting the 1502 // terminator) for now. 1503 ++SpeculationCost; 1504 if (SpeculationCost > 1) 1505 return false; 1506 1507 // Don't hoist the instruction if it's unsafe or expensive. 1508 if (!isSafeToSpeculativelyExecute(I) && 1509 !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore( 1510 I, BB, ThenBB, EndBB)))) 1511 return false; 1512 if (!SpeculatedStoreValue && 1513 ComputeSpeculationCost(I, TTI) > 1514 PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic) 1515 return false; 1516 1517 // Store the store speculation candidate. 1518 if (SpeculatedStoreValue) 1519 SpeculatedStore = cast<StoreInst>(I); 1520 1521 // Do not hoist the instruction if any of its operands are defined but not 1522 // used in BB. The transformation will prevent the operand from 1523 // being sunk into the use block. 1524 for (User::op_iterator i = I->op_begin(), e = I->op_end(); 1525 i != e; ++i) { 1526 Instruction *OpI = dyn_cast<Instruction>(*i); 1527 if (!OpI || OpI->getParent() != BB || 1528 OpI->mayHaveSideEffects()) 1529 continue; // Not a candidate for sinking. 1530 1531 ++SinkCandidateUseCounts[OpI]; 1532 } 1533 } 1534 1535 // Consider any sink candidates which are only used in CondBB as costs for 1536 // speculation. Note, while we iterate over a DenseMap here, we are summing 1537 // and so iteration order isn't significant. 1538 for (SmallDenseMap<Instruction *, unsigned, 4>::iterator I = 1539 SinkCandidateUseCounts.begin(), E = SinkCandidateUseCounts.end(); 1540 I != E; ++I) 1541 if (I->first->getNumUses() == I->second) { 1542 ++SpeculationCost; 1543 if (SpeculationCost > 1) 1544 return false; 1545 } 1546 1547 // Check that the PHI nodes can be converted to selects. 1548 bool HaveRewritablePHIs = false; 1549 for (BasicBlock::iterator I = EndBB->begin(); 1550 PHINode *PN = dyn_cast<PHINode>(I); ++I) { 1551 Value *OrigV = PN->getIncomingValueForBlock(BB); 1552 Value *ThenV = PN->getIncomingValueForBlock(ThenBB); 1553 1554 // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf. 1555 // Skip PHIs which are trivial. 1556 if (ThenV == OrigV) 1557 continue; 1558 1559 // Don't convert to selects if we could remove undefined behavior instead. 1560 if (passingValueIsAlwaysUndefined(OrigV, PN) || 1561 passingValueIsAlwaysUndefined(ThenV, PN)) 1562 return false; 1563 1564 HaveRewritablePHIs = true; 1565 ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV); 1566 ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV); 1567 if (!OrigCE && !ThenCE) 1568 continue; // Known safe and cheap. 1569 1570 if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE)) || 1571 (OrigCE && !isSafeToSpeculativelyExecute(OrigCE))) 1572 return false; 1573 unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0; 1574 unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0; 1575 unsigned MaxCost = 2 * PHINodeFoldingThreshold * 1576 TargetTransformInfo::TCC_Basic; 1577 if (OrigCost + ThenCost > MaxCost) 1578 return false; 1579 1580 // Account for the cost of an unfolded ConstantExpr which could end up 1581 // getting expanded into Instructions. 1582 // FIXME: This doesn't account for how many operations are combined in the 1583 // constant expression. 1584 ++SpeculationCost; 1585 if (SpeculationCost > 1) 1586 return false; 1587 } 1588 1589 // If there are no PHIs to process, bail early. This helps ensure idempotence 1590 // as well. 1591 if (!HaveRewritablePHIs && !(HoistCondStores && SpeculatedStoreValue)) 1592 return false; 1593 1594 // If we get here, we can hoist the instruction and if-convert. 1595 DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";); 1596 1597 // Insert a select of the value of the speculated store. 1598 if (SpeculatedStoreValue) { 1599 IRBuilder<true, NoFolder> Builder(BI); 1600 Value *TrueV = SpeculatedStore->getValueOperand(); 1601 Value *FalseV = SpeculatedStoreValue; 1602 if (Invert) 1603 std::swap(TrueV, FalseV); 1604 Value *S = Builder.CreateSelect(BrCond, TrueV, FalseV, TrueV->getName() + 1605 "." + FalseV->getName()); 1606 SpeculatedStore->setOperand(0, S); 1607 } 1608 1609 // Hoist the instructions. 1610 BB->getInstList().splice(BI, ThenBB->getInstList(), ThenBB->begin(), 1611 std::prev(ThenBB->end())); 1612 1613 // Insert selects and rewrite the PHI operands. 1614 IRBuilder<true, NoFolder> Builder(BI); 1615 for (BasicBlock::iterator I = EndBB->begin(); 1616 PHINode *PN = dyn_cast<PHINode>(I); ++I) { 1617 unsigned OrigI = PN->getBasicBlockIndex(BB); 1618 unsigned ThenI = PN->getBasicBlockIndex(ThenBB); 1619 Value *OrigV = PN->getIncomingValue(OrigI); 1620 Value *ThenV = PN->getIncomingValue(ThenI); 1621 1622 // Skip PHIs which are trivial. 1623 if (OrigV == ThenV) 1624 continue; 1625 1626 // Create a select whose true value is the speculatively executed value and 1627 // false value is the preexisting value. Swap them if the branch 1628 // destinations were inverted. 1629 Value *TrueV = ThenV, *FalseV = OrigV; 1630 if (Invert) 1631 std::swap(TrueV, FalseV); 1632 Value *V = Builder.CreateSelect(BrCond, TrueV, FalseV, 1633 TrueV->getName() + "." + FalseV->getName()); 1634 PN->setIncomingValue(OrigI, V); 1635 PN->setIncomingValue(ThenI, V); 1636 } 1637 1638 ++NumSpeculations; 1639 return true; 1640 } 1641 1642 /// \returns True if this block contains a CallInst with the NoDuplicate 1643 /// attribute. 1644 static bool HasNoDuplicateCall(const BasicBlock *BB) { 1645 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 1646 const CallInst *CI = dyn_cast<CallInst>(I); 1647 if (!CI) 1648 continue; 1649 if (CI->cannotDuplicate()) 1650 return true; 1651 } 1652 return false; 1653 } 1654 1655 /// Return true if we can thread a branch across this block. 1656 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) { 1657 BranchInst *BI = cast<BranchInst>(BB->getTerminator()); 1658 unsigned Size = 0; 1659 1660 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) { 1661 if (isa<DbgInfoIntrinsic>(BBI)) 1662 continue; 1663 if (Size > 10) return false; // Don't clone large BB's. 1664 ++Size; 1665 1666 // We can only support instructions that do not define values that are 1667 // live outside of the current basic block. 1668 for (User *U : BBI->users()) { 1669 Instruction *UI = cast<Instruction>(U); 1670 if (UI->getParent() != BB || isa<PHINode>(UI)) return false; 1671 } 1672 1673 // Looks ok, continue checking. 1674 } 1675 1676 return true; 1677 } 1678 1679 /// If we have a conditional branch on a PHI node value that is defined in the 1680 /// same block as the branch and if any PHI entries are constants, thread edges 1681 /// corresponding to that entry to be branches to their ultimate destination. 1682 static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL) { 1683 BasicBlock *BB = BI->getParent(); 1684 PHINode *PN = dyn_cast<PHINode>(BI->getCondition()); 1685 // NOTE: we currently cannot transform this case if the PHI node is used 1686 // outside of the block. 1687 if (!PN || PN->getParent() != BB || !PN->hasOneUse()) 1688 return false; 1689 1690 // Degenerate case of a single entry PHI. 1691 if (PN->getNumIncomingValues() == 1) { 1692 FoldSingleEntryPHINodes(PN->getParent()); 1693 return true; 1694 } 1695 1696 // Now we know that this block has multiple preds and two succs. 1697 if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false; 1698 1699 if (HasNoDuplicateCall(BB)) return false; 1700 1701 // Okay, this is a simple enough basic block. See if any phi values are 1702 // constants. 1703 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 1704 ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i)); 1705 if (!CB || !CB->getType()->isIntegerTy(1)) continue; 1706 1707 // Okay, we now know that all edges from PredBB should be revectored to 1708 // branch to RealDest. 1709 BasicBlock *PredBB = PN->getIncomingBlock(i); 1710 BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue()); 1711 1712 if (RealDest == BB) continue; // Skip self loops. 1713 // Skip if the predecessor's terminator is an indirect branch. 1714 if (isa<IndirectBrInst>(PredBB->getTerminator())) continue; 1715 1716 // The dest block might have PHI nodes, other predecessors and other 1717 // difficult cases. Instead of being smart about this, just insert a new 1718 // block that jumps to the destination block, effectively splitting 1719 // the edge we are about to create. 1720 BasicBlock *EdgeBB = BasicBlock::Create(BB->getContext(), 1721 RealDest->getName()+".critedge", 1722 RealDest->getParent(), RealDest); 1723 BranchInst::Create(RealDest, EdgeBB); 1724 1725 // Update PHI nodes. 1726 AddPredecessorToBlock(RealDest, EdgeBB, BB); 1727 1728 // BB may have instructions that are being threaded over. Clone these 1729 // instructions into EdgeBB. We know that there will be no uses of the 1730 // cloned instructions outside of EdgeBB. 1731 BasicBlock::iterator InsertPt = EdgeBB->begin(); 1732 DenseMap<Value*, Value*> TranslateMap; // Track translated values. 1733 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) { 1734 if (PHINode *PN = dyn_cast<PHINode>(BBI)) { 1735 TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB); 1736 continue; 1737 } 1738 // Clone the instruction. 1739 Instruction *N = BBI->clone(); 1740 if (BBI->hasName()) N->setName(BBI->getName()+".c"); 1741 1742 // Update operands due to translation. 1743 for (User::op_iterator i = N->op_begin(), e = N->op_end(); 1744 i != e; ++i) { 1745 DenseMap<Value*, Value*>::iterator PI = TranslateMap.find(*i); 1746 if (PI != TranslateMap.end()) 1747 *i = PI->second; 1748 } 1749 1750 // Check for trivial simplification. 1751 if (Value *V = SimplifyInstruction(N, DL)) { 1752 TranslateMap[BBI] = V; 1753 delete N; // Instruction folded away, don't need actual inst 1754 } else { 1755 // Insert the new instruction into its new home. 1756 EdgeBB->getInstList().insert(InsertPt, N); 1757 if (!BBI->use_empty()) 1758 TranslateMap[BBI] = N; 1759 } 1760 } 1761 1762 // Loop over all of the edges from PredBB to BB, changing them to branch 1763 // to EdgeBB instead. 1764 TerminatorInst *PredBBTI = PredBB->getTerminator(); 1765 for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i) 1766 if (PredBBTI->getSuccessor(i) == BB) { 1767 BB->removePredecessor(PredBB); 1768 PredBBTI->setSuccessor(i, EdgeBB); 1769 } 1770 1771 // Recurse, simplifying any other constants. 1772 return FoldCondBranchOnPHI(BI, DL) | true; 1773 } 1774 1775 return false; 1776 } 1777 1778 /// Given a BB that starts with the specified two-entry PHI node, 1779 /// see if we can eliminate it. 1780 static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, 1781 const DataLayout &DL) { 1782 // Ok, this is a two entry PHI node. Check to see if this is a simple "if 1783 // statement", which has a very simple dominance structure. Basically, we 1784 // are trying to find the condition that is being branched on, which 1785 // subsequently causes this merge to happen. We really want control 1786 // dependence information for this check, but simplifycfg can't keep it up 1787 // to date, and this catches most of the cases we care about anyway. 1788 BasicBlock *BB = PN->getParent(); 1789 BasicBlock *IfTrue, *IfFalse; 1790 Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse); 1791 if (!IfCond || 1792 // Don't bother if the branch will be constant folded trivially. 1793 isa<ConstantInt>(IfCond)) 1794 return false; 1795 1796 // Okay, we found that we can merge this two-entry phi node into a select. 1797 // Doing so would require us to fold *all* two entry phi nodes in this block. 1798 // At some point this becomes non-profitable (particularly if the target 1799 // doesn't support cmov's). Only do this transformation if there are two or 1800 // fewer PHI nodes in this block. 1801 unsigned NumPhis = 0; 1802 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I) 1803 if (NumPhis > 2) 1804 return false; 1805 1806 // Loop over the PHI's seeing if we can promote them all to select 1807 // instructions. While we are at it, keep track of the instructions 1808 // that need to be moved to the dominating block. 1809 SmallPtrSet<Instruction*, 4> AggressiveInsts; 1810 unsigned MaxCostVal0 = PHINodeFoldingThreshold, 1811 MaxCostVal1 = PHINodeFoldingThreshold; 1812 MaxCostVal0 *= TargetTransformInfo::TCC_Basic; 1813 MaxCostVal1 *= TargetTransformInfo::TCC_Basic; 1814 1815 for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) { 1816 PHINode *PN = cast<PHINode>(II++); 1817 if (Value *V = SimplifyInstruction(PN, DL)) { 1818 PN->replaceAllUsesWith(V); 1819 PN->eraseFromParent(); 1820 continue; 1821 } 1822 1823 if (!DominatesMergePoint(PN->getIncomingValue(0), BB, &AggressiveInsts, 1824 MaxCostVal0, TTI) || 1825 !DominatesMergePoint(PN->getIncomingValue(1), BB, &AggressiveInsts, 1826 MaxCostVal1, TTI)) 1827 return false; 1828 } 1829 1830 // If we folded the first phi, PN dangles at this point. Refresh it. If 1831 // we ran out of PHIs then we simplified them all. 1832 PN = dyn_cast<PHINode>(BB->begin()); 1833 if (!PN) return true; 1834 1835 // Don't fold i1 branches on PHIs which contain binary operators. These can 1836 // often be turned into switches and other things. 1837 if (PN->getType()->isIntegerTy(1) && 1838 (isa<BinaryOperator>(PN->getIncomingValue(0)) || 1839 isa<BinaryOperator>(PN->getIncomingValue(1)) || 1840 isa<BinaryOperator>(IfCond))) 1841 return false; 1842 1843 // If we all PHI nodes are promotable, check to make sure that all 1844 // instructions in the predecessor blocks can be promoted as well. If 1845 // not, we won't be able to get rid of the control flow, so it's not 1846 // worth promoting to select instructions. 1847 BasicBlock *DomBlock = nullptr; 1848 BasicBlock *IfBlock1 = PN->getIncomingBlock(0); 1849 BasicBlock *IfBlock2 = PN->getIncomingBlock(1); 1850 if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) { 1851 IfBlock1 = nullptr; 1852 } else { 1853 DomBlock = *pred_begin(IfBlock1); 1854 for (BasicBlock::iterator I = IfBlock1->begin();!isa<TerminatorInst>(I);++I) 1855 if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) { 1856 // This is not an aggressive instruction that we can promote. 1857 // Because of this, we won't be able to get rid of the control 1858 // flow, so the xform is not worth it. 1859 return false; 1860 } 1861 } 1862 1863 if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) { 1864 IfBlock2 = nullptr; 1865 } else { 1866 DomBlock = *pred_begin(IfBlock2); 1867 for (BasicBlock::iterator I = IfBlock2->begin();!isa<TerminatorInst>(I);++I) 1868 if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) { 1869 // This is not an aggressive instruction that we can promote. 1870 // Because of this, we won't be able to get rid of the control 1871 // flow, so the xform is not worth it. 1872 return false; 1873 } 1874 } 1875 1876 DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond << " T: " 1877 << IfTrue->getName() << " F: " << IfFalse->getName() << "\n"); 1878 1879 // If we can still promote the PHI nodes after this gauntlet of tests, 1880 // do all of the PHI's now. 1881 Instruction *InsertPt = DomBlock->getTerminator(); 1882 IRBuilder<true, NoFolder> Builder(InsertPt); 1883 1884 // Move all 'aggressive' instructions, which are defined in the 1885 // conditional parts of the if's up to the dominating block. 1886 if (IfBlock1) 1887 DomBlock->getInstList().splice(InsertPt, 1888 IfBlock1->getInstList(), IfBlock1->begin(), 1889 IfBlock1->getTerminator()); 1890 if (IfBlock2) 1891 DomBlock->getInstList().splice(InsertPt, 1892 IfBlock2->getInstList(), IfBlock2->begin(), 1893 IfBlock2->getTerminator()); 1894 1895 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { 1896 // Change the PHI node into a select instruction. 1897 Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse); 1898 Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue); 1899 1900 SelectInst *NV = 1901 cast<SelectInst>(Builder.CreateSelect(IfCond, TrueVal, FalseVal, "")); 1902 PN->replaceAllUsesWith(NV); 1903 NV->takeName(PN); 1904 PN->eraseFromParent(); 1905 } 1906 1907 // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement 1908 // has been flattened. Change DomBlock to jump directly to our new block to 1909 // avoid other simplifycfg's kicking in on the diamond. 1910 TerminatorInst *OldTI = DomBlock->getTerminator(); 1911 Builder.SetInsertPoint(OldTI); 1912 Builder.CreateBr(BB); 1913 OldTI->eraseFromParent(); 1914 return true; 1915 } 1916 1917 /// If we found a conditional branch that goes to two returning blocks, 1918 /// try to merge them together into one return, 1919 /// introducing a select if the return values disagree. 1920 static bool SimplifyCondBranchToTwoReturns(BranchInst *BI, 1921 IRBuilder<> &Builder) { 1922 assert(BI->isConditional() && "Must be a conditional branch"); 1923 BasicBlock *TrueSucc = BI->getSuccessor(0); 1924 BasicBlock *FalseSucc = BI->getSuccessor(1); 1925 ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator()); 1926 ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator()); 1927 1928 // Check to ensure both blocks are empty (just a return) or optionally empty 1929 // with PHI nodes. If there are other instructions, merging would cause extra 1930 // computation on one path or the other. 1931 if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator()) 1932 return false; 1933 if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator()) 1934 return false; 1935 1936 Builder.SetInsertPoint(BI); 1937 // Okay, we found a branch that is going to two return nodes. If 1938 // there is no return value for this function, just change the 1939 // branch into a return. 1940 if (FalseRet->getNumOperands() == 0) { 1941 TrueSucc->removePredecessor(BI->getParent()); 1942 FalseSucc->removePredecessor(BI->getParent()); 1943 Builder.CreateRetVoid(); 1944 EraseTerminatorInstAndDCECond(BI); 1945 return true; 1946 } 1947 1948 // Otherwise, figure out what the true and false return values are 1949 // so we can insert a new select instruction. 1950 Value *TrueValue = TrueRet->getReturnValue(); 1951 Value *FalseValue = FalseRet->getReturnValue(); 1952 1953 // Unwrap any PHI nodes in the return blocks. 1954 if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue)) 1955 if (TVPN->getParent() == TrueSucc) 1956 TrueValue = TVPN->getIncomingValueForBlock(BI->getParent()); 1957 if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue)) 1958 if (FVPN->getParent() == FalseSucc) 1959 FalseValue = FVPN->getIncomingValueForBlock(BI->getParent()); 1960 1961 // In order for this transformation to be safe, we must be able to 1962 // unconditionally execute both operands to the return. This is 1963 // normally the case, but we could have a potentially-trapping 1964 // constant expression that prevents this transformation from being 1965 // safe. 1966 if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue)) 1967 if (TCV->canTrap()) 1968 return false; 1969 if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue)) 1970 if (FCV->canTrap()) 1971 return false; 1972 1973 // Okay, we collected all the mapped values and checked them for sanity, and 1974 // defined to really do this transformation. First, update the CFG. 1975 TrueSucc->removePredecessor(BI->getParent()); 1976 FalseSucc->removePredecessor(BI->getParent()); 1977 1978 // Insert select instructions where needed. 1979 Value *BrCond = BI->getCondition(); 1980 if (TrueValue) { 1981 // Insert a select if the results differ. 1982 if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) { 1983 } else if (isa<UndefValue>(TrueValue)) { 1984 TrueValue = FalseValue; 1985 } else { 1986 TrueValue = Builder.CreateSelect(BrCond, TrueValue, 1987 FalseValue, "retval"); 1988 } 1989 } 1990 1991 Value *RI = !TrueValue ? 1992 Builder.CreateRetVoid() : Builder.CreateRet(TrueValue); 1993 1994 (void) RI; 1995 1996 DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:" 1997 << "\n " << *BI << "NewRet = " << *RI 1998 << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc); 1999 2000 EraseTerminatorInstAndDCECond(BI); 2001 2002 return true; 2003 } 2004 2005 /// Given a conditional BranchInstruction, retrieve the probabilities of the 2006 /// branch taking each edge. Fills in the two APInt parameters and returns true, 2007 /// or returns false if no or invalid metadata was found. 2008 static bool ExtractBranchMetadata(BranchInst *BI, 2009 uint64_t &ProbTrue, uint64_t &ProbFalse) { 2010 assert(BI->isConditional() && 2011 "Looking for probabilities on unconditional branch?"); 2012 MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof); 2013 if (!ProfileData || ProfileData->getNumOperands() != 3) return false; 2014 ConstantInt *CITrue = 2015 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)); 2016 ConstantInt *CIFalse = 2017 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2)); 2018 if (!CITrue || !CIFalse) return false; 2019 ProbTrue = CITrue->getValue().getZExtValue(); 2020 ProbFalse = CIFalse->getValue().getZExtValue(); 2021 return true; 2022 } 2023 2024 /// Return true if the given instruction is available 2025 /// in its predecessor block. If yes, the instruction will be removed. 2026 static bool checkCSEInPredecessor(Instruction *Inst, BasicBlock *PB) { 2027 if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst)) 2028 return false; 2029 for (BasicBlock::iterator I = PB->begin(), E = PB->end(); I != E; I++) { 2030 Instruction *PBI = &*I; 2031 // Check whether Inst and PBI generate the same value. 2032 if (Inst->isIdenticalTo(PBI)) { 2033 Inst->replaceAllUsesWith(PBI); 2034 Inst->eraseFromParent(); 2035 return true; 2036 } 2037 } 2038 return false; 2039 } 2040 2041 /// If this basic block is simple enough, and if a predecessor branches to us 2042 /// and one of our successors, fold the block into the predecessor and use 2043 /// logical operations to pick the right destination. 2044 bool llvm::FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold) { 2045 BasicBlock *BB = BI->getParent(); 2046 2047 Instruction *Cond = nullptr; 2048 if (BI->isConditional()) 2049 Cond = dyn_cast<Instruction>(BI->getCondition()); 2050 else { 2051 // For unconditional branch, check for a simple CFG pattern, where 2052 // BB has a single predecessor and BB's successor is also its predecessor's 2053 // successor. If such pattern exisits, check for CSE between BB and its 2054 // predecessor. 2055 if (BasicBlock *PB = BB->getSinglePredecessor()) 2056 if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator())) 2057 if (PBI->isConditional() && 2058 (BI->getSuccessor(0) == PBI->getSuccessor(0) || 2059 BI->getSuccessor(0) == PBI->getSuccessor(1))) { 2060 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); 2061 I != E; ) { 2062 Instruction *Curr = I++; 2063 if (isa<CmpInst>(Curr)) { 2064 Cond = Curr; 2065 break; 2066 } 2067 // Quit if we can't remove this instruction. 2068 if (!checkCSEInPredecessor(Curr, PB)) 2069 return false; 2070 } 2071 } 2072 2073 if (!Cond) 2074 return false; 2075 } 2076 2077 if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) || 2078 Cond->getParent() != BB || !Cond->hasOneUse()) 2079 return false; 2080 2081 // Make sure the instruction after the condition is the cond branch. 2082 BasicBlock::iterator CondIt = Cond; ++CondIt; 2083 2084 // Ignore dbg intrinsics. 2085 while (isa<DbgInfoIntrinsic>(CondIt)) ++CondIt; 2086 2087 if (&*CondIt != BI) 2088 return false; 2089 2090 // Only allow this transformation if computing the condition doesn't involve 2091 // too many instructions and these involved instructions can be executed 2092 // unconditionally. We denote all involved instructions except the condition 2093 // as "bonus instructions", and only allow this transformation when the 2094 // number of the bonus instructions does not exceed a certain threshold. 2095 unsigned NumBonusInsts = 0; 2096 for (auto I = BB->begin(); Cond != I; ++I) { 2097 // Ignore dbg intrinsics. 2098 if (isa<DbgInfoIntrinsic>(I)) 2099 continue; 2100 if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(I)) 2101 return false; 2102 // I has only one use and can be executed unconditionally. 2103 Instruction *User = dyn_cast<Instruction>(I->user_back()); 2104 if (User == nullptr || User->getParent() != BB) 2105 return false; 2106 // I is used in the same BB. Since BI uses Cond and doesn't have more slots 2107 // to use any other instruction, User must be an instruction between next(I) 2108 // and Cond. 2109 ++NumBonusInsts; 2110 // Early exits once we reach the limit. 2111 if (NumBonusInsts > BonusInstThreshold) 2112 return false; 2113 } 2114 2115 // Cond is known to be a compare or binary operator. Check to make sure that 2116 // neither operand is a potentially-trapping constant expression. 2117 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0))) 2118 if (CE->canTrap()) 2119 return false; 2120 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1))) 2121 if (CE->canTrap()) 2122 return false; 2123 2124 // Finally, don't infinitely unroll conditional loops. 2125 BasicBlock *TrueDest = BI->getSuccessor(0); 2126 BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr; 2127 if (TrueDest == BB || FalseDest == BB) 2128 return false; 2129 2130 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 2131 BasicBlock *PredBlock = *PI; 2132 BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator()); 2133 2134 // Check that we have two conditional branches. If there is a PHI node in 2135 // the common successor, verify that the same value flows in from both 2136 // blocks. 2137 SmallVector<PHINode*, 4> PHIs; 2138 if (!PBI || PBI->isUnconditional() || 2139 (BI->isConditional() && 2140 !SafeToMergeTerminators(BI, PBI)) || 2141 (!BI->isConditional() && 2142 !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs))) 2143 continue; 2144 2145 // Determine if the two branches share a common destination. 2146 Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd; 2147 bool InvertPredCond = false; 2148 2149 if (BI->isConditional()) { 2150 if (PBI->getSuccessor(0) == TrueDest) 2151 Opc = Instruction::Or; 2152 else if (PBI->getSuccessor(1) == FalseDest) 2153 Opc = Instruction::And; 2154 else if (PBI->getSuccessor(0) == FalseDest) 2155 Opc = Instruction::And, InvertPredCond = true; 2156 else if (PBI->getSuccessor(1) == TrueDest) 2157 Opc = Instruction::Or, InvertPredCond = true; 2158 else 2159 continue; 2160 } else { 2161 if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest) 2162 continue; 2163 } 2164 2165 DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB); 2166 IRBuilder<> Builder(PBI); 2167 2168 // If we need to invert the condition in the pred block to match, do so now. 2169 if (InvertPredCond) { 2170 Value *NewCond = PBI->getCondition(); 2171 2172 if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) { 2173 CmpInst *CI = cast<CmpInst>(NewCond); 2174 CI->setPredicate(CI->getInversePredicate()); 2175 } else { 2176 NewCond = Builder.CreateNot(NewCond, 2177 PBI->getCondition()->getName()+".not"); 2178 } 2179 2180 PBI->setCondition(NewCond); 2181 PBI->swapSuccessors(); 2182 } 2183 2184 // If we have bonus instructions, clone them into the predecessor block. 2185 // Note that there may be multiple predecessor blocks, so we cannot move 2186 // bonus instructions to a predecessor block. 2187 ValueToValueMapTy VMap; // maps original values to cloned values 2188 // We already make sure Cond is the last instruction before BI. Therefore, 2189 // all instructions before Cond other than DbgInfoIntrinsic are bonus 2190 // instructions. 2191 for (auto BonusInst = BB->begin(); Cond != BonusInst; ++BonusInst) { 2192 if (isa<DbgInfoIntrinsic>(BonusInst)) 2193 continue; 2194 Instruction *NewBonusInst = BonusInst->clone(); 2195 RemapInstruction(NewBonusInst, VMap, 2196 RF_NoModuleLevelChanges | RF_IgnoreMissingEntries); 2197 VMap[BonusInst] = NewBonusInst; 2198 2199 // If we moved a load, we cannot any longer claim any knowledge about 2200 // its potential value. The previous information might have been valid 2201 // only given the branch precondition. 2202 // For an analogous reason, we must also drop all the metadata whose 2203 // semantics we don't understand. 2204 NewBonusInst->dropUnknownNonDebugMetadata(); 2205 2206 PredBlock->getInstList().insert(PBI, NewBonusInst); 2207 NewBonusInst->takeName(BonusInst); 2208 BonusInst->setName(BonusInst->getName() + ".old"); 2209 } 2210 2211 // Clone Cond into the predecessor basic block, and or/and the 2212 // two conditions together. 2213 Instruction *New = Cond->clone(); 2214 RemapInstruction(New, VMap, 2215 RF_NoModuleLevelChanges | RF_IgnoreMissingEntries); 2216 PredBlock->getInstList().insert(PBI, New); 2217 New->takeName(Cond); 2218 Cond->setName(New->getName() + ".old"); 2219 2220 if (BI->isConditional()) { 2221 Instruction *NewCond = 2222 cast<Instruction>(Builder.CreateBinOp(Opc, PBI->getCondition(), 2223 New, "or.cond")); 2224 PBI->setCondition(NewCond); 2225 2226 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight; 2227 bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight, 2228 PredFalseWeight); 2229 bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight, 2230 SuccFalseWeight); 2231 SmallVector<uint64_t, 8> NewWeights; 2232 2233 if (PBI->getSuccessor(0) == BB) { 2234 if (PredHasWeights && SuccHasWeights) { 2235 // PBI: br i1 %x, BB, FalseDest 2236 // BI: br i1 %y, TrueDest, FalseDest 2237 //TrueWeight is TrueWeight for PBI * TrueWeight for BI. 2238 NewWeights.push_back(PredTrueWeight * SuccTrueWeight); 2239 //FalseWeight is FalseWeight for PBI * TotalWeight for BI + 2240 // TrueWeight for PBI * FalseWeight for BI. 2241 // We assume that total weights of a BranchInst can fit into 32 bits. 2242 // Therefore, we will not have overflow using 64-bit arithmetic. 2243 NewWeights.push_back(PredFalseWeight * (SuccFalseWeight + 2244 SuccTrueWeight) + PredTrueWeight * SuccFalseWeight); 2245 } 2246 AddPredecessorToBlock(TrueDest, PredBlock, BB); 2247 PBI->setSuccessor(0, TrueDest); 2248 } 2249 if (PBI->getSuccessor(1) == BB) { 2250 if (PredHasWeights && SuccHasWeights) { 2251 // PBI: br i1 %x, TrueDest, BB 2252 // BI: br i1 %y, TrueDest, FalseDest 2253 //TrueWeight is TrueWeight for PBI * TotalWeight for BI + 2254 // FalseWeight for PBI * TrueWeight for BI. 2255 NewWeights.push_back(PredTrueWeight * (SuccFalseWeight + 2256 SuccTrueWeight) + PredFalseWeight * SuccTrueWeight); 2257 //FalseWeight is FalseWeight for PBI * FalseWeight for BI. 2258 NewWeights.push_back(PredFalseWeight * SuccFalseWeight); 2259 } 2260 AddPredecessorToBlock(FalseDest, PredBlock, BB); 2261 PBI->setSuccessor(1, FalseDest); 2262 } 2263 if (NewWeights.size() == 2) { 2264 // Halve the weights if any of them cannot fit in an uint32_t 2265 FitWeights(NewWeights); 2266 2267 SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),NewWeights.end()); 2268 PBI->setMetadata(LLVMContext::MD_prof, 2269 MDBuilder(BI->getContext()). 2270 createBranchWeights(MDWeights)); 2271 } else 2272 PBI->setMetadata(LLVMContext::MD_prof, nullptr); 2273 } else { 2274 // Update PHI nodes in the common successors. 2275 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) { 2276 ConstantInt *PBI_C = cast<ConstantInt>( 2277 PHIs[i]->getIncomingValueForBlock(PBI->getParent())); 2278 assert(PBI_C->getType()->isIntegerTy(1)); 2279 Instruction *MergedCond = nullptr; 2280 if (PBI->getSuccessor(0) == TrueDest) { 2281 // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value) 2282 // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value) 2283 // is false: !PBI_Cond and BI_Value 2284 Instruction *NotCond = 2285 cast<Instruction>(Builder.CreateNot(PBI->getCondition(), 2286 "not.cond")); 2287 MergedCond = 2288 cast<Instruction>(Builder.CreateBinOp(Instruction::And, 2289 NotCond, New, 2290 "and.cond")); 2291 if (PBI_C->isOne()) 2292 MergedCond = 2293 cast<Instruction>(Builder.CreateBinOp(Instruction::Or, 2294 PBI->getCondition(), MergedCond, 2295 "or.cond")); 2296 } else { 2297 // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C) 2298 // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond) 2299 // is false: PBI_Cond and BI_Value 2300 MergedCond = 2301 cast<Instruction>(Builder.CreateBinOp(Instruction::And, 2302 PBI->getCondition(), New, 2303 "and.cond")); 2304 if (PBI_C->isOne()) { 2305 Instruction *NotCond = 2306 cast<Instruction>(Builder.CreateNot(PBI->getCondition(), 2307 "not.cond")); 2308 MergedCond = 2309 cast<Instruction>(Builder.CreateBinOp(Instruction::Or, 2310 NotCond, MergedCond, 2311 "or.cond")); 2312 } 2313 } 2314 // Update PHI Node. 2315 PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()), 2316 MergedCond); 2317 } 2318 // Change PBI from Conditional to Unconditional. 2319 BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI); 2320 EraseTerminatorInstAndDCECond(PBI); 2321 PBI = New_PBI; 2322 } 2323 2324 // TODO: If BB is reachable from all paths through PredBlock, then we 2325 // could replace PBI's branch probabilities with BI's. 2326 2327 // Copy any debug value intrinsics into the end of PredBlock. 2328 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 2329 if (isa<DbgInfoIntrinsic>(*I)) 2330 I->clone()->insertBefore(PBI); 2331 2332 return true; 2333 } 2334 return false; 2335 } 2336 2337 /// If we have a conditional branch as a predecessor of another block, 2338 /// this function tries to simplify it. We know 2339 /// that PBI and BI are both conditional branches, and BI is in one of the 2340 /// successor blocks of PBI - PBI branches to BI. 2341 static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) { 2342 assert(PBI->isConditional() && BI->isConditional()); 2343 BasicBlock *BB = BI->getParent(); 2344 2345 // If this block ends with a branch instruction, and if there is a 2346 // predecessor that ends on a branch of the same condition, make 2347 // this conditional branch redundant. 2348 if (PBI->getCondition() == BI->getCondition() && 2349 PBI->getSuccessor(0) != PBI->getSuccessor(1)) { 2350 // Okay, the outcome of this conditional branch is statically 2351 // knowable. If this block had a single pred, handle specially. 2352 if (BB->getSinglePredecessor()) { 2353 // Turn this into a branch on constant. 2354 bool CondIsTrue = PBI->getSuccessor(0) == BB; 2355 BI->setCondition(ConstantInt::get(Type::getInt1Ty(BB->getContext()), 2356 CondIsTrue)); 2357 return true; // Nuke the branch on constant. 2358 } 2359 2360 // Otherwise, if there are multiple predecessors, insert a PHI that merges 2361 // in the constant and simplify the block result. Subsequent passes of 2362 // simplifycfg will thread the block. 2363 if (BlockIsSimpleEnoughToThreadThrough(BB)) { 2364 pred_iterator PB = pred_begin(BB), PE = pred_end(BB); 2365 PHINode *NewPN = PHINode::Create(Type::getInt1Ty(BB->getContext()), 2366 std::distance(PB, PE), 2367 BI->getCondition()->getName() + ".pr", 2368 BB->begin()); 2369 // Okay, we're going to insert the PHI node. Since PBI is not the only 2370 // predecessor, compute the PHI'd conditional value for all of the preds. 2371 // Any predecessor where the condition is not computable we keep symbolic. 2372 for (pred_iterator PI = PB; PI != PE; ++PI) { 2373 BasicBlock *P = *PI; 2374 if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) && 2375 PBI != BI && PBI->isConditional() && 2376 PBI->getCondition() == BI->getCondition() && 2377 PBI->getSuccessor(0) != PBI->getSuccessor(1)) { 2378 bool CondIsTrue = PBI->getSuccessor(0) == BB; 2379 NewPN->addIncoming(ConstantInt::get(Type::getInt1Ty(BB->getContext()), 2380 CondIsTrue), P); 2381 } else { 2382 NewPN->addIncoming(BI->getCondition(), P); 2383 } 2384 } 2385 2386 BI->setCondition(NewPN); 2387 return true; 2388 } 2389 } 2390 2391 // If this is a conditional branch in an empty block, and if any 2392 // predecessors are a conditional branch to one of our destinations, 2393 // fold the conditions into logical ops and one cond br. 2394 BasicBlock::iterator BBI = BB->begin(); 2395 // Ignore dbg intrinsics. 2396 while (isa<DbgInfoIntrinsic>(BBI)) 2397 ++BBI; 2398 if (&*BBI != BI) 2399 return false; 2400 2401 2402 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BI->getCondition())) 2403 if (CE->canTrap()) 2404 return false; 2405 2406 int PBIOp, BIOp; 2407 if (PBI->getSuccessor(0) == BI->getSuccessor(0)) 2408 PBIOp = BIOp = 0; 2409 else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) 2410 PBIOp = 0, BIOp = 1; 2411 else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) 2412 PBIOp = 1, BIOp = 0; 2413 else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) 2414 PBIOp = BIOp = 1; 2415 else 2416 return false; 2417 2418 // Check to make sure that the other destination of this branch 2419 // isn't BB itself. If so, this is an infinite loop that will 2420 // keep getting unwound. 2421 if (PBI->getSuccessor(PBIOp) == BB) 2422 return false; 2423 2424 // Do not perform this transformation if it would require 2425 // insertion of a large number of select instructions. For targets 2426 // without predication/cmovs, this is a big pessimization. 2427 2428 // Also do not perform this transformation if any phi node in the common 2429 // destination block can trap when reached by BB or PBB (PR17073). In that 2430 // case, it would be unsafe to hoist the operation into a select instruction. 2431 2432 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp); 2433 unsigned NumPhis = 0; 2434 for (BasicBlock::iterator II = CommonDest->begin(); 2435 isa<PHINode>(II); ++II, ++NumPhis) { 2436 if (NumPhis > 2) // Disable this xform. 2437 return false; 2438 2439 PHINode *PN = cast<PHINode>(II); 2440 Value *BIV = PN->getIncomingValueForBlock(BB); 2441 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BIV)) 2442 if (CE->canTrap()) 2443 return false; 2444 2445 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent()); 2446 Value *PBIV = PN->getIncomingValue(PBBIdx); 2447 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PBIV)) 2448 if (CE->canTrap()) 2449 return false; 2450 } 2451 2452 // Finally, if everything is ok, fold the branches to logical ops. 2453 BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1); 2454 2455 DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent() 2456 << "AND: " << *BI->getParent()); 2457 2458 2459 // If OtherDest *is* BB, then BB is a basic block with a single conditional 2460 // branch in it, where one edge (OtherDest) goes back to itself but the other 2461 // exits. We don't *know* that the program avoids the infinite loop 2462 // (even though that seems likely). If we do this xform naively, we'll end up 2463 // recursively unpeeling the loop. Since we know that (after the xform is 2464 // done) that the block *is* infinite if reached, we just make it an obviously 2465 // infinite loop with no cond branch. 2466 if (OtherDest == BB) { 2467 // Insert it at the end of the function, because it's either code, 2468 // or it won't matter if it's hot. :) 2469 BasicBlock *InfLoopBlock = BasicBlock::Create(BB->getContext(), 2470 "infloop", BB->getParent()); 2471 BranchInst::Create(InfLoopBlock, InfLoopBlock); 2472 OtherDest = InfLoopBlock; 2473 } 2474 2475 DEBUG(dbgs() << *PBI->getParent()->getParent()); 2476 2477 // BI may have other predecessors. Because of this, we leave 2478 // it alone, but modify PBI. 2479 2480 // Make sure we get to CommonDest on True&True directions. 2481 Value *PBICond = PBI->getCondition(); 2482 IRBuilder<true, NoFolder> Builder(PBI); 2483 if (PBIOp) 2484 PBICond = Builder.CreateNot(PBICond, PBICond->getName()+".not"); 2485 2486 Value *BICond = BI->getCondition(); 2487 if (BIOp) 2488 BICond = Builder.CreateNot(BICond, BICond->getName()+".not"); 2489 2490 // Merge the conditions. 2491 Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge"); 2492 2493 // Modify PBI to branch on the new condition to the new dests. 2494 PBI->setCondition(Cond); 2495 PBI->setSuccessor(0, CommonDest); 2496 PBI->setSuccessor(1, OtherDest); 2497 2498 // Update branch weight for PBI. 2499 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight; 2500 bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight, 2501 PredFalseWeight); 2502 bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight, 2503 SuccFalseWeight); 2504 if (PredHasWeights && SuccHasWeights) { 2505 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight; 2506 uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight; 2507 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight; 2508 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight; 2509 // The weight to CommonDest should be PredCommon * SuccTotal + 2510 // PredOther * SuccCommon. 2511 // The weight to OtherDest should be PredOther * SuccOther. 2512 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) + 2513 PredOther * SuccCommon, 2514 PredOther * SuccOther}; 2515 // Halve the weights if any of them cannot fit in an uint32_t 2516 FitWeights(NewWeights); 2517 2518 PBI->setMetadata(LLVMContext::MD_prof, 2519 MDBuilder(BI->getContext()) 2520 .createBranchWeights(NewWeights[0], NewWeights[1])); 2521 } 2522 2523 // OtherDest may have phi nodes. If so, add an entry from PBI's 2524 // block that are identical to the entries for BI's block. 2525 AddPredecessorToBlock(OtherDest, PBI->getParent(), BB); 2526 2527 // We know that the CommonDest already had an edge from PBI to 2528 // it. If it has PHIs though, the PHIs may have different 2529 // entries for BB and PBI's BB. If so, insert a select to make 2530 // them agree. 2531 PHINode *PN; 2532 for (BasicBlock::iterator II = CommonDest->begin(); 2533 (PN = dyn_cast<PHINode>(II)); ++II) { 2534 Value *BIV = PN->getIncomingValueForBlock(BB); 2535 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent()); 2536 Value *PBIV = PN->getIncomingValue(PBBIdx); 2537 if (BIV != PBIV) { 2538 // Insert a select in PBI to pick the right value. 2539 Value *NV = cast<SelectInst> 2540 (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName()+".mux")); 2541 PN->setIncomingValue(PBBIdx, NV); 2542 } 2543 } 2544 2545 DEBUG(dbgs() << "INTO: " << *PBI->getParent()); 2546 DEBUG(dbgs() << *PBI->getParent()->getParent()); 2547 2548 // This basic block is probably dead. We know it has at least 2549 // one fewer predecessor. 2550 return true; 2551 } 2552 2553 // Simplifies a terminator by replacing it with a branch to TrueBB if Cond is 2554 // true or to FalseBB if Cond is false. 2555 // Takes care of updating the successors and removing the old terminator. 2556 // Also makes sure not to introduce new successors by assuming that edges to 2557 // non-successor TrueBBs and FalseBBs aren't reachable. 2558 static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond, 2559 BasicBlock *TrueBB, BasicBlock *FalseBB, 2560 uint32_t TrueWeight, 2561 uint32_t FalseWeight){ 2562 // Remove any superfluous successor edges from the CFG. 2563 // First, figure out which successors to preserve. 2564 // If TrueBB and FalseBB are equal, only try to preserve one copy of that 2565 // successor. 2566 BasicBlock *KeepEdge1 = TrueBB; 2567 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr; 2568 2569 // Then remove the rest. 2570 for (BasicBlock *Succ : OldTerm->successors()) { 2571 // Make sure only to keep exactly one copy of each edge. 2572 if (Succ == KeepEdge1) 2573 KeepEdge1 = nullptr; 2574 else if (Succ == KeepEdge2) 2575 KeepEdge2 = nullptr; 2576 else 2577 Succ->removePredecessor(OldTerm->getParent()); 2578 } 2579 2580 IRBuilder<> Builder(OldTerm); 2581 Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc()); 2582 2583 // Insert an appropriate new terminator. 2584 if (!KeepEdge1 && !KeepEdge2) { 2585 if (TrueBB == FalseBB) 2586 // We were only looking for one successor, and it was present. 2587 // Create an unconditional branch to it. 2588 Builder.CreateBr(TrueBB); 2589 else { 2590 // We found both of the successors we were looking for. 2591 // Create a conditional branch sharing the condition of the select. 2592 BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB); 2593 if (TrueWeight != FalseWeight) 2594 NewBI->setMetadata(LLVMContext::MD_prof, 2595 MDBuilder(OldTerm->getContext()). 2596 createBranchWeights(TrueWeight, FalseWeight)); 2597 } 2598 } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) { 2599 // Neither of the selected blocks were successors, so this 2600 // terminator must be unreachable. 2601 new UnreachableInst(OldTerm->getContext(), OldTerm); 2602 } else { 2603 // One of the selected values was a successor, but the other wasn't. 2604 // Insert an unconditional branch to the one that was found; 2605 // the edge to the one that wasn't must be unreachable. 2606 if (!KeepEdge1) 2607 // Only TrueBB was found. 2608 Builder.CreateBr(TrueBB); 2609 else 2610 // Only FalseBB was found. 2611 Builder.CreateBr(FalseBB); 2612 } 2613 2614 EraseTerminatorInstAndDCECond(OldTerm); 2615 return true; 2616 } 2617 2618 // Replaces 2619 // (switch (select cond, X, Y)) on constant X, Y 2620 // with a branch - conditional if X and Y lead to distinct BBs, 2621 // unconditional otherwise. 2622 static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select) { 2623 // Check for constant integer values in the select. 2624 ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue()); 2625 ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue()); 2626 if (!TrueVal || !FalseVal) 2627 return false; 2628 2629 // Find the relevant condition and destinations. 2630 Value *Condition = Select->getCondition(); 2631 BasicBlock *TrueBB = SI->findCaseValue(TrueVal).getCaseSuccessor(); 2632 BasicBlock *FalseBB = SI->findCaseValue(FalseVal).getCaseSuccessor(); 2633 2634 // Get weight for TrueBB and FalseBB. 2635 uint32_t TrueWeight = 0, FalseWeight = 0; 2636 SmallVector<uint64_t, 8> Weights; 2637 bool HasWeights = HasBranchWeights(SI); 2638 if (HasWeights) { 2639 GetBranchWeights(SI, Weights); 2640 if (Weights.size() == 1 + SI->getNumCases()) { 2641 TrueWeight = (uint32_t)Weights[SI->findCaseValue(TrueVal). 2642 getSuccessorIndex()]; 2643 FalseWeight = (uint32_t)Weights[SI->findCaseValue(FalseVal). 2644 getSuccessorIndex()]; 2645 } 2646 } 2647 2648 // Perform the actual simplification. 2649 return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, 2650 TrueWeight, FalseWeight); 2651 } 2652 2653 // Replaces 2654 // (indirectbr (select cond, blockaddress(@fn, BlockA), 2655 // blockaddress(@fn, BlockB))) 2656 // with 2657 // (br cond, BlockA, BlockB). 2658 static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI) { 2659 // Check that both operands of the select are block addresses. 2660 BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue()); 2661 BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue()); 2662 if (!TBA || !FBA) 2663 return false; 2664 2665 // Extract the actual blocks. 2666 BasicBlock *TrueBB = TBA->getBasicBlock(); 2667 BasicBlock *FalseBB = FBA->getBasicBlock(); 2668 2669 // Perform the actual simplification. 2670 return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB, 2671 0, 0); 2672 } 2673 2674 /// This is called when we find an icmp instruction 2675 /// (a seteq/setne with a constant) as the only instruction in a 2676 /// block that ends with an uncond branch. We are looking for a very specific 2677 /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In 2678 /// this case, we merge the first two "or's of icmp" into a switch, but then the 2679 /// default value goes to an uncond block with a seteq in it, we get something 2680 /// like: 2681 /// 2682 /// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ] 2683 /// DEFAULT: 2684 /// %tmp = icmp eq i8 %A, 92 2685 /// br label %end 2686 /// end: 2687 /// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ] 2688 /// 2689 /// We prefer to split the edge to 'end' so that there is a true/false entry to 2690 /// the PHI, merging the third icmp into the switch. 2691 static bool TryToSimplifyUncondBranchWithICmpInIt( 2692 ICmpInst *ICI, IRBuilder<> &Builder, const DataLayout &DL, 2693 const TargetTransformInfo &TTI, unsigned BonusInstThreshold, 2694 AssumptionCache *AC) { 2695 BasicBlock *BB = ICI->getParent(); 2696 2697 // If the block has any PHIs in it or the icmp has multiple uses, it is too 2698 // complex. 2699 if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse()) return false; 2700 2701 Value *V = ICI->getOperand(0); 2702 ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1)); 2703 2704 // The pattern we're looking for is where our only predecessor is a switch on 2705 // 'V' and this block is the default case for the switch. In this case we can 2706 // fold the compared value into the switch to simplify things. 2707 BasicBlock *Pred = BB->getSinglePredecessor(); 2708 if (!Pred || !isa<SwitchInst>(Pred->getTerminator())) return false; 2709 2710 SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator()); 2711 if (SI->getCondition() != V) 2712 return false; 2713 2714 // If BB is reachable on a non-default case, then we simply know the value of 2715 // V in this block. Substitute it and constant fold the icmp instruction 2716 // away. 2717 if (SI->getDefaultDest() != BB) { 2718 ConstantInt *VVal = SI->findCaseDest(BB); 2719 assert(VVal && "Should have a unique destination value"); 2720 ICI->setOperand(0, VVal); 2721 2722 if (Value *V = SimplifyInstruction(ICI, DL)) { 2723 ICI->replaceAllUsesWith(V); 2724 ICI->eraseFromParent(); 2725 } 2726 // BB is now empty, so it is likely to simplify away. 2727 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 2728 } 2729 2730 // Ok, the block is reachable from the default dest. If the constant we're 2731 // comparing exists in one of the other edges, then we can constant fold ICI 2732 // and zap it. 2733 if (SI->findCaseValue(Cst) != SI->case_default()) { 2734 Value *V; 2735 if (ICI->getPredicate() == ICmpInst::ICMP_EQ) 2736 V = ConstantInt::getFalse(BB->getContext()); 2737 else 2738 V = ConstantInt::getTrue(BB->getContext()); 2739 2740 ICI->replaceAllUsesWith(V); 2741 ICI->eraseFromParent(); 2742 // BB is now empty, so it is likely to simplify away. 2743 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 2744 } 2745 2746 // The use of the icmp has to be in the 'end' block, by the only PHI node in 2747 // the block. 2748 BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0); 2749 PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back()); 2750 if (PHIUse == nullptr || PHIUse != &SuccBlock->front() || 2751 isa<PHINode>(++BasicBlock::iterator(PHIUse))) 2752 return false; 2753 2754 // If the icmp is a SETEQ, then the default dest gets false, the new edge gets 2755 // true in the PHI. 2756 Constant *DefaultCst = ConstantInt::getTrue(BB->getContext()); 2757 Constant *NewCst = ConstantInt::getFalse(BB->getContext()); 2758 2759 if (ICI->getPredicate() == ICmpInst::ICMP_EQ) 2760 std::swap(DefaultCst, NewCst); 2761 2762 // Replace ICI (which is used by the PHI for the default value) with true or 2763 // false depending on if it is EQ or NE. 2764 ICI->replaceAllUsesWith(DefaultCst); 2765 ICI->eraseFromParent(); 2766 2767 // Okay, the switch goes to this block on a default value. Add an edge from 2768 // the switch to the merge point on the compared value. 2769 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "switch.edge", 2770 BB->getParent(), BB); 2771 SmallVector<uint64_t, 8> Weights; 2772 bool HasWeights = HasBranchWeights(SI); 2773 if (HasWeights) { 2774 GetBranchWeights(SI, Weights); 2775 if (Weights.size() == 1 + SI->getNumCases()) { 2776 // Split weight for default case to case for "Cst". 2777 Weights[0] = (Weights[0]+1) >> 1; 2778 Weights.push_back(Weights[0]); 2779 2780 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end()); 2781 SI->setMetadata(LLVMContext::MD_prof, 2782 MDBuilder(SI->getContext()). 2783 createBranchWeights(MDWeights)); 2784 } 2785 } 2786 SI->addCase(Cst, NewBB); 2787 2788 // NewBB branches to the phi block, add the uncond branch and the phi entry. 2789 Builder.SetInsertPoint(NewBB); 2790 Builder.SetCurrentDebugLocation(SI->getDebugLoc()); 2791 Builder.CreateBr(SuccBlock); 2792 PHIUse->addIncoming(NewCst, NewBB); 2793 return true; 2794 } 2795 2796 /// The specified branch is a conditional branch. 2797 /// Check to see if it is branching on an or/and chain of icmp instructions, and 2798 /// fold it into a switch instruction if so. 2799 static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder, 2800 const DataLayout &DL) { 2801 Instruction *Cond = dyn_cast<Instruction>(BI->getCondition()); 2802 if (!Cond) return false; 2803 2804 // Change br (X == 0 | X == 1), T, F into a switch instruction. 2805 // If this is a bunch of seteq's or'd together, or if it's a bunch of 2806 // 'setne's and'ed together, collect them. 2807 2808 // Try to gather values from a chain of and/or to be turned into a switch 2809 ConstantComparesGatherer ConstantCompare(Cond, DL); 2810 // Unpack the result 2811 SmallVectorImpl<ConstantInt*> &Values = ConstantCompare.Vals; 2812 Value *CompVal = ConstantCompare.CompValue; 2813 unsigned UsedICmps = ConstantCompare.UsedICmps; 2814 Value *ExtraCase = ConstantCompare.Extra; 2815 2816 // If we didn't have a multiply compared value, fail. 2817 if (!CompVal) return false; 2818 2819 // Avoid turning single icmps into a switch. 2820 if (UsedICmps <= 1) 2821 return false; 2822 2823 bool TrueWhenEqual = (Cond->getOpcode() == Instruction::Or); 2824 2825 // There might be duplicate constants in the list, which the switch 2826 // instruction can't handle, remove them now. 2827 array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate); 2828 Values.erase(std::unique(Values.begin(), Values.end()), Values.end()); 2829 2830 // If Extra was used, we require at least two switch values to do the 2831 // transformation. A switch with one value is just a conditional branch. 2832 if (ExtraCase && Values.size() < 2) return false; 2833 2834 // TODO: Preserve branch weight metadata, similarly to how 2835 // FoldValueComparisonIntoPredecessors preserves it. 2836 2837 // Figure out which block is which destination. 2838 BasicBlock *DefaultBB = BI->getSuccessor(1); 2839 BasicBlock *EdgeBB = BI->getSuccessor(0); 2840 if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB); 2841 2842 BasicBlock *BB = BI->getParent(); 2843 2844 DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size() 2845 << " cases into SWITCH. BB is:\n" << *BB); 2846 2847 // If there are any extra values that couldn't be folded into the switch 2848 // then we evaluate them with an explicit branch first. Split the block 2849 // right before the condbr to handle it. 2850 if (ExtraCase) { 2851 BasicBlock *NewBB = BB->splitBasicBlock(BI, "switch.early.test"); 2852 // Remove the uncond branch added to the old block. 2853 TerminatorInst *OldTI = BB->getTerminator(); 2854 Builder.SetInsertPoint(OldTI); 2855 2856 if (TrueWhenEqual) 2857 Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB); 2858 else 2859 Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB); 2860 2861 OldTI->eraseFromParent(); 2862 2863 // If there are PHI nodes in EdgeBB, then we need to add a new entry to them 2864 // for the edge we just added. 2865 AddPredecessorToBlock(EdgeBB, BB, NewBB); 2866 2867 DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase 2868 << "\nEXTRABB = " << *BB); 2869 BB = NewBB; 2870 } 2871 2872 Builder.SetInsertPoint(BI); 2873 // Convert pointer to int before we switch. 2874 if (CompVal->getType()->isPointerTy()) { 2875 CompVal = Builder.CreatePtrToInt( 2876 CompVal, DL.getIntPtrType(CompVal->getType()), "magicptr"); 2877 } 2878 2879 // Create the new switch instruction now. 2880 SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size()); 2881 2882 // Add all of the 'cases' to the switch instruction. 2883 for (unsigned i = 0, e = Values.size(); i != e; ++i) 2884 New->addCase(Values[i], EdgeBB); 2885 2886 // We added edges from PI to the EdgeBB. As such, if there were any 2887 // PHI nodes in EdgeBB, they need entries to be added corresponding to 2888 // the number of edges added. 2889 for (BasicBlock::iterator BBI = EdgeBB->begin(); 2890 isa<PHINode>(BBI); ++BBI) { 2891 PHINode *PN = cast<PHINode>(BBI); 2892 Value *InVal = PN->getIncomingValueForBlock(BB); 2893 for (unsigned i = 0, e = Values.size()-1; i != e; ++i) 2894 PN->addIncoming(InVal, BB); 2895 } 2896 2897 // Erase the old branch instruction. 2898 EraseTerminatorInstAndDCECond(BI); 2899 2900 DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n'); 2901 return true; 2902 } 2903 2904 bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) { 2905 // If this is a trivial landing pad that just continues unwinding the caught 2906 // exception then zap the landing pad, turning its invokes into calls. 2907 BasicBlock *BB = RI->getParent(); 2908 LandingPadInst *LPInst = dyn_cast<LandingPadInst>(BB->getFirstNonPHI()); 2909 if (RI->getValue() != LPInst) 2910 // Not a landing pad, or the resume is not unwinding the exception that 2911 // caused control to branch here. 2912 return false; 2913 2914 // Check that there are no other instructions except for debug intrinsics. 2915 BasicBlock::iterator I = LPInst, E = RI; 2916 while (++I != E) 2917 if (!isa<DbgInfoIntrinsic>(I)) 2918 return false; 2919 2920 // Turn all invokes that unwind here into calls and delete the basic block. 2921 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) { 2922 BasicBlock *Pred = *PI++; 2923 removeUnwindEdge(Pred); 2924 } 2925 2926 // The landingpad is now unreachable. Zap it. 2927 BB->eraseFromParent(); 2928 return true; 2929 } 2930 2931 bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) { 2932 // If this is a trivial cleanup pad that executes no instructions, it can be 2933 // eliminated. If the cleanup pad continues to the caller, any predecessor 2934 // that is an EH pad will be updated to continue to the caller and any 2935 // predecessor that terminates with an invoke instruction will have its invoke 2936 // instruction converted to a call instruction. If the cleanup pad being 2937 // simplified does not continue to the caller, each predecessor will be 2938 // updated to continue to the unwind destination of the cleanup pad being 2939 // simplified. 2940 BasicBlock *BB = RI->getParent(); 2941 Instruction *CPInst = dyn_cast<CleanupPadInst>(BB->getFirstNonPHI()); 2942 if (!CPInst) 2943 // This isn't an empty cleanup. 2944 return false; 2945 2946 // Check that there are no other instructions except for debug intrinsics. 2947 BasicBlock::iterator I = CPInst, E = RI; 2948 while (++I != E) 2949 if (!isa<DbgInfoIntrinsic>(I)) 2950 return false; 2951 2952 // If the cleanup return we are simplifying unwinds to the caller, this 2953 // will set UnwindDest to nullptr. 2954 BasicBlock *UnwindDest = RI->getUnwindDest(); 2955 2956 // We're about to remove BB from the control flow. Before we do, sink any 2957 // PHINodes into the unwind destination. Doing this before changing the 2958 // control flow avoids some potentially slow checks, since we can currently 2959 // be certain that UnwindDest and BB have no common predecessors (since they 2960 // are both EH pads). 2961 if (UnwindDest) { 2962 // First, go through the PHI nodes in UnwindDest and update any nodes that 2963 // reference the block we are removing 2964 for (BasicBlock::iterator I = UnwindDest->begin(), 2965 IE = UnwindDest->getFirstNonPHI(); 2966 I != IE; ++I) { 2967 PHINode *DestPN = cast<PHINode>(I); 2968 2969 int Idx = DestPN->getBasicBlockIndex(BB); 2970 // Since BB unwinds to UnwindDest, it has to be in the PHI node. 2971 assert(Idx != -1); 2972 // This PHI node has an incoming value that corresponds to a control 2973 // path through the cleanup pad we are removing. If the incoming 2974 // value is in the cleanup pad, it must be a PHINode (because we 2975 // verified above that the block is otherwise empty). Otherwise, the 2976 // value is either a constant or a value that dominates the cleanup 2977 // pad being removed. 2978 // 2979 // Because BB and UnwindDest are both EH pads, all of their 2980 // predecessors must unwind to these blocks, and since no instruction 2981 // can have multiple unwind destinations, there will be no overlap in 2982 // incoming blocks between SrcPN and DestPN. 2983 Value *SrcVal = DestPN->getIncomingValue(Idx); 2984 PHINode *SrcPN = dyn_cast<PHINode>(SrcVal); 2985 2986 // Remove the entry for the block we are deleting. 2987 DestPN->removeIncomingValue(Idx, false); 2988 2989 if (SrcPN && SrcPN->getParent() == BB) { 2990 // If the incoming value was a PHI node in the cleanup pad we are 2991 // removing, we need to merge that PHI node's incoming values into 2992 // DestPN. 2993 for (unsigned SrcIdx = 0, SrcE = SrcPN->getNumIncomingValues(); 2994 SrcIdx != SrcE; ++SrcIdx) { 2995 DestPN->addIncoming(SrcPN->getIncomingValue(SrcIdx), 2996 SrcPN->getIncomingBlock(SrcIdx)); 2997 } 2998 } else { 2999 // Otherwise, the incoming value came from above BB and 3000 // so we can just reuse it. We must associate all of BB's 3001 // predecessors with this value. 3002 for (auto *pred : predecessors(BB)) { 3003 DestPN->addIncoming(SrcVal, pred); 3004 } 3005 } 3006 } 3007 3008 // Sink any remaining PHI nodes directly into UnwindDest. 3009 Instruction *InsertPt = UnwindDest->getFirstNonPHI(); 3010 for (BasicBlock::iterator I = BB->begin(), IE = BB->getFirstNonPHI(); 3011 I != IE;) { 3012 // The iterator must be incremented here because the instructions are 3013 // being moved to another block. 3014 PHINode *PN = cast<PHINode>(I++); 3015 if (PN->use_empty()) 3016 // If the PHI node has no uses, just leave it. It will be erased 3017 // when we erase BB below. 3018 continue; 3019 3020 // Otherwise, sink this PHI node into UnwindDest. 3021 // Any predecessors to UnwindDest which are not already represented 3022 // must be back edges which inherit the value from the path through 3023 // BB. In this case, the PHI value must reference itself. 3024 for (auto *pred : predecessors(UnwindDest)) 3025 if (pred != BB) 3026 PN->addIncoming(PN, pred); 3027 PN->moveBefore(InsertPt); 3028 } 3029 } 3030 3031 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) { 3032 // The iterator must be updated here because we are removing this pred. 3033 BasicBlock *PredBB = *PI++; 3034 if (UnwindDest == nullptr) { 3035 removeUnwindEdge(PredBB); 3036 } else { 3037 TerminatorInst *TI = PredBB->getTerminator(); 3038 TI->replaceUsesOfWith(BB, UnwindDest); 3039 } 3040 } 3041 3042 // The cleanup pad is now unreachable. Zap it. 3043 BB->eraseFromParent(); 3044 return true; 3045 } 3046 3047 bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) { 3048 BasicBlock *BB = RI->getParent(); 3049 if (!BB->getFirstNonPHIOrDbg()->isTerminator()) return false; 3050 3051 // Find predecessors that end with branches. 3052 SmallVector<BasicBlock*, 8> UncondBranchPreds; 3053 SmallVector<BranchInst*, 8> CondBranchPreds; 3054 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 3055 BasicBlock *P = *PI; 3056 TerminatorInst *PTI = P->getTerminator(); 3057 if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) { 3058 if (BI->isUnconditional()) 3059 UncondBranchPreds.push_back(P); 3060 else 3061 CondBranchPreds.push_back(BI); 3062 } 3063 } 3064 3065 // If we found some, do the transformation! 3066 if (!UncondBranchPreds.empty() && DupRet) { 3067 while (!UncondBranchPreds.empty()) { 3068 BasicBlock *Pred = UncondBranchPreds.pop_back_val(); 3069 DEBUG(dbgs() << "FOLDING: " << *BB 3070 << "INTO UNCOND BRANCH PRED: " << *Pred); 3071 (void)FoldReturnIntoUncondBranch(RI, BB, Pred); 3072 } 3073 3074 // If we eliminated all predecessors of the block, delete the block now. 3075 if (pred_empty(BB)) 3076 // We know there are no successors, so just nuke the block. 3077 BB->eraseFromParent(); 3078 3079 return true; 3080 } 3081 3082 // Check out all of the conditional branches going to this return 3083 // instruction. If any of them just select between returns, change the 3084 // branch itself into a select/return pair. 3085 while (!CondBranchPreds.empty()) { 3086 BranchInst *BI = CondBranchPreds.pop_back_val(); 3087 3088 // Check to see if the non-BB successor is also a return block. 3089 if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) && 3090 isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) && 3091 SimplifyCondBranchToTwoReturns(BI, Builder)) 3092 return true; 3093 } 3094 return false; 3095 } 3096 3097 bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) { 3098 BasicBlock *BB = UI->getParent(); 3099 3100 bool Changed = false; 3101 3102 // If there are any instructions immediately before the unreachable that can 3103 // be removed, do so. 3104 while (UI != BB->begin()) { 3105 BasicBlock::iterator BBI = UI; 3106 --BBI; 3107 // Do not delete instructions that can have side effects which might cause 3108 // the unreachable to not be reachable; specifically, calls and volatile 3109 // operations may have this effect. 3110 if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI)) break; 3111 3112 if (BBI->mayHaveSideEffects()) { 3113 if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { 3114 if (SI->isVolatile()) 3115 break; 3116 } else if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { 3117 if (LI->isVolatile()) 3118 break; 3119 } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(BBI)) { 3120 if (RMWI->isVolatile()) 3121 break; 3122 } else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) { 3123 if (CXI->isVolatile()) 3124 break; 3125 } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) && 3126 !isa<LandingPadInst>(BBI)) { 3127 break; 3128 } 3129 // Note that deleting LandingPad's here is in fact okay, although it 3130 // involves a bit of subtle reasoning. If this inst is a LandingPad, 3131 // all the predecessors of this block will be the unwind edges of Invokes, 3132 // and we can therefore guarantee this block will be erased. 3133 } 3134 3135 // Delete this instruction (any uses are guaranteed to be dead) 3136 if (!BBI->use_empty()) 3137 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); 3138 BBI->eraseFromParent(); 3139 Changed = true; 3140 } 3141 3142 // If the unreachable instruction is the first in the block, take a gander 3143 // at all of the predecessors of this instruction, and simplify them. 3144 if (&BB->front() != UI) return Changed; 3145 3146 SmallVector<BasicBlock*, 8> Preds(pred_begin(BB), pred_end(BB)); 3147 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 3148 TerminatorInst *TI = Preds[i]->getTerminator(); 3149 IRBuilder<> Builder(TI); 3150 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 3151 if (BI->isUnconditional()) { 3152 if (BI->getSuccessor(0) == BB) { 3153 new UnreachableInst(TI->getContext(), TI); 3154 TI->eraseFromParent(); 3155 Changed = true; 3156 } 3157 } else { 3158 if (BI->getSuccessor(0) == BB) { 3159 Builder.CreateBr(BI->getSuccessor(1)); 3160 EraseTerminatorInstAndDCECond(BI); 3161 } else if (BI->getSuccessor(1) == BB) { 3162 Builder.CreateBr(BI->getSuccessor(0)); 3163 EraseTerminatorInstAndDCECond(BI); 3164 Changed = true; 3165 } 3166 } 3167 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 3168 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); 3169 i != e; ++i) 3170 if (i.getCaseSuccessor() == BB) { 3171 BB->removePredecessor(SI->getParent()); 3172 SI->removeCase(i); 3173 --i; --e; 3174 Changed = true; 3175 } 3176 } else if ((isa<InvokeInst>(TI) && 3177 cast<InvokeInst>(TI)->getUnwindDest() == BB) || 3178 isa<CatchEndPadInst>(TI) || isa<TerminatePadInst>(TI)) { 3179 removeUnwindEdge(TI->getParent()); 3180 Changed = true; 3181 } else if (isa<CleanupReturnInst>(TI) || isa<CleanupEndPadInst>(TI) || 3182 isa<CatchReturnInst>(TI)) { 3183 new UnreachableInst(TI->getContext(), TI); 3184 TI->eraseFromParent(); 3185 Changed = true; 3186 } 3187 // TODO: If TI is a CatchPadInst, then (BB must be its normal dest and) 3188 // we can eliminate it, redirecting its preds to its unwind successor, 3189 // or to the next outer handler if the removed catch is the last for its 3190 // catchendpad. 3191 } 3192 3193 // If this block is now dead, remove it. 3194 if (pred_empty(BB) && 3195 BB != &BB->getParent()->getEntryBlock()) { 3196 // We know there are no successors, so just nuke the block. 3197 BB->eraseFromParent(); 3198 return true; 3199 } 3200 3201 return Changed; 3202 } 3203 3204 static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) { 3205 assert(Cases.size() >= 1); 3206 3207 array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate); 3208 for (size_t I = 1, E = Cases.size(); I != E; ++I) { 3209 if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1) 3210 return false; 3211 } 3212 return true; 3213 } 3214 3215 /// Turn a switch with two reachable destinations into an integer range 3216 /// comparison and branch. 3217 static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) { 3218 assert(SI->getNumCases() > 1 && "Degenerate switch?"); 3219 3220 bool HasDefault = 3221 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg()); 3222 3223 // Partition the cases into two sets with different destinations. 3224 BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr; 3225 BasicBlock *DestB = nullptr; 3226 SmallVector <ConstantInt *, 16> CasesA; 3227 SmallVector <ConstantInt *, 16> CasesB; 3228 3229 for (SwitchInst::CaseIt I : SI->cases()) { 3230 BasicBlock *Dest = I.getCaseSuccessor(); 3231 if (!DestA) DestA = Dest; 3232 if (Dest == DestA) { 3233 CasesA.push_back(I.getCaseValue()); 3234 continue; 3235 } 3236 if (!DestB) DestB = Dest; 3237 if (Dest == DestB) { 3238 CasesB.push_back(I.getCaseValue()); 3239 continue; 3240 } 3241 return false; // More than two destinations. 3242 } 3243 3244 assert(DestA && DestB && "Single-destination switch should have been folded."); 3245 assert(DestA != DestB); 3246 assert(DestB != SI->getDefaultDest()); 3247 assert(!CasesB.empty() && "There must be non-default cases."); 3248 assert(!CasesA.empty() || HasDefault); 3249 3250 // Figure out if one of the sets of cases form a contiguous range. 3251 SmallVectorImpl<ConstantInt *> *ContiguousCases = nullptr; 3252 BasicBlock *ContiguousDest = nullptr; 3253 BasicBlock *OtherDest = nullptr; 3254 if (!CasesA.empty() && CasesAreContiguous(CasesA)) { 3255 ContiguousCases = &CasesA; 3256 ContiguousDest = DestA; 3257 OtherDest = DestB; 3258 } else if (CasesAreContiguous(CasesB)) { 3259 ContiguousCases = &CasesB; 3260 ContiguousDest = DestB; 3261 OtherDest = DestA; 3262 } else 3263 return false; 3264 3265 // Start building the compare and branch. 3266 3267 Constant *Offset = ConstantExpr::getNeg(ContiguousCases->back()); 3268 Constant *NumCases = ConstantInt::get(Offset->getType(), ContiguousCases->size()); 3269 3270 Value *Sub = SI->getCondition(); 3271 if (!Offset->isNullValue()) 3272 Sub = Builder.CreateAdd(Sub, Offset, Sub->getName() + ".off"); 3273 3274 Value *Cmp; 3275 // If NumCases overflowed, then all possible values jump to the successor. 3276 if (NumCases->isNullValue() && !ContiguousCases->empty()) 3277 Cmp = ConstantInt::getTrue(SI->getContext()); 3278 else 3279 Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch"); 3280 BranchInst *NewBI = Builder.CreateCondBr(Cmp, ContiguousDest, OtherDest); 3281 3282 // Update weight for the newly-created conditional branch. 3283 if (HasBranchWeights(SI)) { 3284 SmallVector<uint64_t, 8> Weights; 3285 GetBranchWeights(SI, Weights); 3286 if (Weights.size() == 1 + SI->getNumCases()) { 3287 uint64_t TrueWeight = 0; 3288 uint64_t FalseWeight = 0; 3289 for (size_t I = 0, E = Weights.size(); I != E; ++I) { 3290 if (SI->getSuccessor(I) == ContiguousDest) 3291 TrueWeight += Weights[I]; 3292 else 3293 FalseWeight += Weights[I]; 3294 } 3295 while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) { 3296 TrueWeight /= 2; 3297 FalseWeight /= 2; 3298 } 3299 NewBI->setMetadata(LLVMContext::MD_prof, 3300 MDBuilder(SI->getContext()).createBranchWeights( 3301 (uint32_t)TrueWeight, (uint32_t)FalseWeight)); 3302 } 3303 } 3304 3305 // Prune obsolete incoming values off the successors' PHI nodes. 3306 for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) { 3307 unsigned PreviousEdges = ContiguousCases->size(); 3308 if (ContiguousDest == SI->getDefaultDest()) ++PreviousEdges; 3309 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I) 3310 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent()); 3311 } 3312 for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) { 3313 unsigned PreviousEdges = SI->getNumCases() - ContiguousCases->size(); 3314 if (OtherDest == SI->getDefaultDest()) ++PreviousEdges; 3315 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I) 3316 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent()); 3317 } 3318 3319 // Drop the switch. 3320 SI->eraseFromParent(); 3321 3322 return true; 3323 } 3324 3325 /// Compute masked bits for the condition of a switch 3326 /// and use it to remove dead cases. 3327 static bool EliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC, 3328 const DataLayout &DL) { 3329 Value *Cond = SI->getCondition(); 3330 unsigned Bits = Cond->getType()->getIntegerBitWidth(); 3331 APInt KnownZero(Bits, 0), KnownOne(Bits, 0); 3332 computeKnownBits(Cond, KnownZero, KnownOne, DL, 0, AC, SI); 3333 3334 // Gather dead cases. 3335 SmallVector<ConstantInt*, 8> DeadCases; 3336 for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) { 3337 if ((I.getCaseValue()->getValue() & KnownZero) != 0 || 3338 (I.getCaseValue()->getValue() & KnownOne) != KnownOne) { 3339 DeadCases.push_back(I.getCaseValue()); 3340 DEBUG(dbgs() << "SimplifyCFG: switch case '" 3341 << I.getCaseValue() << "' is dead.\n"); 3342 } 3343 } 3344 3345 // If we can prove that the cases must cover all possible values, the 3346 // default destination becomes dead and we can remove it. If we know some 3347 // of the bits in the value, we can use that to more precisely compute the 3348 // number of possible unique case values. 3349 bool HasDefault = 3350 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg()); 3351 const unsigned NumUnknownBits = Bits - 3352 (KnownZero.Or(KnownOne)).countPopulation(); 3353 assert(NumUnknownBits <= Bits); 3354 if (HasDefault && DeadCases.empty() && 3355 NumUnknownBits < 64 /* avoid overflow */ && 3356 SI->getNumCases() == (1ULL << NumUnknownBits)) { 3357 DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n"); 3358 BasicBlock *NewDefault = SplitBlockPredecessors(SI->getDefaultDest(), 3359 SI->getParent(), ""); 3360 SI->setDefaultDest(NewDefault); 3361 SplitBlock(NewDefault, NewDefault->begin()); 3362 auto *OldTI = NewDefault->getTerminator(); 3363 new UnreachableInst(SI->getContext(), OldTI); 3364 EraseTerminatorInstAndDCECond(OldTI); 3365 return true; 3366 } 3367 3368 SmallVector<uint64_t, 8> Weights; 3369 bool HasWeight = HasBranchWeights(SI); 3370 if (HasWeight) { 3371 GetBranchWeights(SI, Weights); 3372 HasWeight = (Weights.size() == 1 + SI->getNumCases()); 3373 } 3374 3375 // Remove dead cases from the switch. 3376 for (unsigned I = 0, E = DeadCases.size(); I != E; ++I) { 3377 SwitchInst::CaseIt Case = SI->findCaseValue(DeadCases[I]); 3378 assert(Case != SI->case_default() && 3379 "Case was not found. Probably mistake in DeadCases forming."); 3380 if (HasWeight) { 3381 std::swap(Weights[Case.getCaseIndex()+1], Weights.back()); 3382 Weights.pop_back(); 3383 } 3384 3385 // Prune unused values from PHI nodes. 3386 Case.getCaseSuccessor()->removePredecessor(SI->getParent()); 3387 SI->removeCase(Case); 3388 } 3389 if (HasWeight && Weights.size() >= 2) { 3390 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end()); 3391 SI->setMetadata(LLVMContext::MD_prof, 3392 MDBuilder(SI->getParent()->getContext()). 3393 createBranchWeights(MDWeights)); 3394 } 3395 3396 return !DeadCases.empty(); 3397 } 3398 3399 /// If BB would be eligible for simplification by 3400 /// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated 3401 /// by an unconditional branch), look at the phi node for BB in the successor 3402 /// block and see if the incoming value is equal to CaseValue. If so, return 3403 /// the phi node, and set PhiIndex to BB's index in the phi node. 3404 static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue, 3405 BasicBlock *BB, 3406 int *PhiIndex) { 3407 if (BB->getFirstNonPHIOrDbg() != BB->getTerminator()) 3408 return nullptr; // BB must be empty to be a candidate for simplification. 3409 if (!BB->getSinglePredecessor()) 3410 return nullptr; // BB must be dominated by the switch. 3411 3412 BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()); 3413 if (!Branch || !Branch->isUnconditional()) 3414 return nullptr; // Terminator must be unconditional branch. 3415 3416 BasicBlock *Succ = Branch->getSuccessor(0); 3417 3418 BasicBlock::iterator I = Succ->begin(); 3419 while (PHINode *PHI = dyn_cast<PHINode>(I++)) { 3420 int Idx = PHI->getBasicBlockIndex(BB); 3421 assert(Idx >= 0 && "PHI has no entry for predecessor?"); 3422 3423 Value *InValue = PHI->getIncomingValue(Idx); 3424 if (InValue != CaseValue) continue; 3425 3426 *PhiIndex = Idx; 3427 return PHI; 3428 } 3429 3430 return nullptr; 3431 } 3432 3433 /// Try to forward the condition of a switch instruction to a phi node 3434 /// dominated by the switch, if that would mean that some of the destination 3435 /// blocks of the switch can be folded away. 3436 /// Returns true if a change is made. 3437 static bool ForwardSwitchConditionToPHI(SwitchInst *SI) { 3438 typedef DenseMap<PHINode*, SmallVector<int,4> > ForwardingNodesMap; 3439 ForwardingNodesMap ForwardingNodes; 3440 3441 for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) { 3442 ConstantInt *CaseValue = I.getCaseValue(); 3443 BasicBlock *CaseDest = I.getCaseSuccessor(); 3444 3445 int PhiIndex; 3446 PHINode *PHI = FindPHIForConditionForwarding(CaseValue, CaseDest, 3447 &PhiIndex); 3448 if (!PHI) continue; 3449 3450 ForwardingNodes[PHI].push_back(PhiIndex); 3451 } 3452 3453 bool Changed = false; 3454 3455 for (ForwardingNodesMap::iterator I = ForwardingNodes.begin(), 3456 E = ForwardingNodes.end(); I != E; ++I) { 3457 PHINode *Phi = I->first; 3458 SmallVectorImpl<int> &Indexes = I->second; 3459 3460 if (Indexes.size() < 2) continue; 3461 3462 for (size_t I = 0, E = Indexes.size(); I != E; ++I) 3463 Phi->setIncomingValue(Indexes[I], SI->getCondition()); 3464 Changed = true; 3465 } 3466 3467 return Changed; 3468 } 3469 3470 /// Return true if the backend will be able to handle 3471 /// initializing an array of constants like C. 3472 static bool ValidLookupTableConstant(Constant *C) { 3473 if (C->isThreadDependent()) 3474 return false; 3475 if (C->isDLLImportDependent()) 3476 return false; 3477 3478 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) 3479 return CE->isGEPWithNoNotionalOverIndexing(); 3480 3481 return isa<ConstantFP>(C) || 3482 isa<ConstantInt>(C) || 3483 isa<ConstantPointerNull>(C) || 3484 isa<GlobalValue>(C) || 3485 isa<UndefValue>(C); 3486 } 3487 3488 /// If V is a Constant, return it. Otherwise, try to look up 3489 /// its constant value in ConstantPool, returning 0 if it's not there. 3490 static Constant *LookupConstant(Value *V, 3491 const SmallDenseMap<Value*, Constant*>& ConstantPool) { 3492 if (Constant *C = dyn_cast<Constant>(V)) 3493 return C; 3494 return ConstantPool.lookup(V); 3495 } 3496 3497 /// Try to fold instruction I into a constant. This works for 3498 /// simple instructions such as binary operations where both operands are 3499 /// constant or can be replaced by constants from the ConstantPool. Returns the 3500 /// resulting constant on success, 0 otherwise. 3501 static Constant * 3502 ConstantFold(Instruction *I, const DataLayout &DL, 3503 const SmallDenseMap<Value *, Constant *> &ConstantPool) { 3504 if (SelectInst *Select = dyn_cast<SelectInst>(I)) { 3505 Constant *A = LookupConstant(Select->getCondition(), ConstantPool); 3506 if (!A) 3507 return nullptr; 3508 if (A->isAllOnesValue()) 3509 return LookupConstant(Select->getTrueValue(), ConstantPool); 3510 if (A->isNullValue()) 3511 return LookupConstant(Select->getFalseValue(), ConstantPool); 3512 return nullptr; 3513 } 3514 3515 SmallVector<Constant *, 4> COps; 3516 for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) { 3517 if (Constant *A = LookupConstant(I->getOperand(N), ConstantPool)) 3518 COps.push_back(A); 3519 else 3520 return nullptr; 3521 } 3522 3523 if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) { 3524 return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0], 3525 COps[1], DL); 3526 } 3527 3528 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), COps, DL); 3529 } 3530 3531 /// Try to determine the resulting constant values in phi nodes 3532 /// at the common destination basic block, *CommonDest, for one of the case 3533 /// destionations CaseDest corresponding to value CaseVal (0 for the default 3534 /// case), of a switch instruction SI. 3535 static bool 3536 GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest, 3537 BasicBlock **CommonDest, 3538 SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res, 3539 const DataLayout &DL) { 3540 // The block from which we enter the common destination. 3541 BasicBlock *Pred = SI->getParent(); 3542 3543 // If CaseDest is empty except for some side-effect free instructions through 3544 // which we can constant-propagate the CaseVal, continue to its successor. 3545 SmallDenseMap<Value*, Constant*> ConstantPool; 3546 ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal)); 3547 for (BasicBlock::iterator I = CaseDest->begin(), E = CaseDest->end(); I != E; 3548 ++I) { 3549 if (TerminatorInst *T = dyn_cast<TerminatorInst>(I)) { 3550 // If the terminator is a simple branch, continue to the next block. 3551 if (T->getNumSuccessors() != 1) 3552 return false; 3553 Pred = CaseDest; 3554 CaseDest = T->getSuccessor(0); 3555 } else if (isa<DbgInfoIntrinsic>(I)) { 3556 // Skip debug intrinsic. 3557 continue; 3558 } else if (Constant *C = ConstantFold(I, DL, ConstantPool)) { 3559 // Instruction is side-effect free and constant. 3560 3561 // If the instruction has uses outside this block or a phi node slot for 3562 // the block, it is not safe to bypass the instruction since it would then 3563 // no longer dominate all its uses. 3564 for (auto &Use : I->uses()) { 3565 User *User = Use.getUser(); 3566 if (Instruction *I = dyn_cast<Instruction>(User)) 3567 if (I->getParent() == CaseDest) 3568 continue; 3569 if (PHINode *Phi = dyn_cast<PHINode>(User)) 3570 if (Phi->getIncomingBlock(Use) == CaseDest) 3571 continue; 3572 return false; 3573 } 3574 3575 ConstantPool.insert(std::make_pair(I, C)); 3576 } else { 3577 break; 3578 } 3579 } 3580 3581 // If we did not have a CommonDest before, use the current one. 3582 if (!*CommonDest) 3583 *CommonDest = CaseDest; 3584 // If the destination isn't the common one, abort. 3585 if (CaseDest != *CommonDest) 3586 return false; 3587 3588 // Get the values for this case from phi nodes in the destination block. 3589 BasicBlock::iterator I = (*CommonDest)->begin(); 3590 while (PHINode *PHI = dyn_cast<PHINode>(I++)) { 3591 int Idx = PHI->getBasicBlockIndex(Pred); 3592 if (Idx == -1) 3593 continue; 3594 3595 Constant *ConstVal = LookupConstant(PHI->getIncomingValue(Idx), 3596 ConstantPool); 3597 if (!ConstVal) 3598 return false; 3599 3600 // Be conservative about which kinds of constants we support. 3601 if (!ValidLookupTableConstant(ConstVal)) 3602 return false; 3603 3604 Res.push_back(std::make_pair(PHI, ConstVal)); 3605 } 3606 3607 return Res.size() > 0; 3608 } 3609 3610 // Helper function used to add CaseVal to the list of cases that generate 3611 // Result. 3612 static void MapCaseToResult(ConstantInt *CaseVal, 3613 SwitchCaseResultVectorTy &UniqueResults, 3614 Constant *Result) { 3615 for (auto &I : UniqueResults) { 3616 if (I.first == Result) { 3617 I.second.push_back(CaseVal); 3618 return; 3619 } 3620 } 3621 UniqueResults.push_back(std::make_pair(Result, 3622 SmallVector<ConstantInt*, 4>(1, CaseVal))); 3623 } 3624 3625 // Helper function that initializes a map containing 3626 // results for the PHI node of the common destination block for a switch 3627 // instruction. Returns false if multiple PHI nodes have been found or if 3628 // there is not a common destination block for the switch. 3629 static bool InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, 3630 BasicBlock *&CommonDest, 3631 SwitchCaseResultVectorTy &UniqueResults, 3632 Constant *&DefaultResult, 3633 const DataLayout &DL) { 3634 for (auto &I : SI->cases()) { 3635 ConstantInt *CaseVal = I.getCaseValue(); 3636 3637 // Resulting value at phi nodes for this case value. 3638 SwitchCaseResultsTy Results; 3639 if (!GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results, 3640 DL)) 3641 return false; 3642 3643 // Only one value per case is permitted 3644 if (Results.size() > 1) 3645 return false; 3646 MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second); 3647 3648 // Check the PHI consistency. 3649 if (!PHI) 3650 PHI = Results[0].first; 3651 else if (PHI != Results[0].first) 3652 return false; 3653 } 3654 // Find the default result value. 3655 SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults; 3656 BasicBlock *DefaultDest = SI->getDefaultDest(); 3657 GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults, 3658 DL); 3659 // If the default value is not found abort unless the default destination 3660 // is unreachable. 3661 DefaultResult = 3662 DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr; 3663 if ((!DefaultResult && 3664 !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()))) 3665 return false; 3666 3667 return true; 3668 } 3669 3670 // Helper function that checks if it is possible to transform a switch with only 3671 // two cases (or two cases + default) that produces a result into a select. 3672 // Example: 3673 // switch (a) { 3674 // case 10: %0 = icmp eq i32 %a, 10 3675 // return 10; %1 = select i1 %0, i32 10, i32 4 3676 // case 20: ----> %2 = icmp eq i32 %a, 20 3677 // return 2; %3 = select i1 %2, i32 2, i32 %1 3678 // default: 3679 // return 4; 3680 // } 3681 static Value * 3682 ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector, 3683 Constant *DefaultResult, Value *Condition, 3684 IRBuilder<> &Builder) { 3685 assert(ResultVector.size() == 2 && 3686 "We should have exactly two unique results at this point"); 3687 // If we are selecting between only two cases transform into a simple 3688 // select or a two-way select if default is possible. 3689 if (ResultVector[0].second.size() == 1 && 3690 ResultVector[1].second.size() == 1) { 3691 ConstantInt *const FirstCase = ResultVector[0].second[0]; 3692 ConstantInt *const SecondCase = ResultVector[1].second[0]; 3693 3694 bool DefaultCanTrigger = DefaultResult; 3695 Value *SelectValue = ResultVector[1].first; 3696 if (DefaultCanTrigger) { 3697 Value *const ValueCompare = 3698 Builder.CreateICmpEQ(Condition, SecondCase, "switch.selectcmp"); 3699 SelectValue = Builder.CreateSelect(ValueCompare, ResultVector[1].first, 3700 DefaultResult, "switch.select"); 3701 } 3702 Value *const ValueCompare = 3703 Builder.CreateICmpEQ(Condition, FirstCase, "switch.selectcmp"); 3704 return Builder.CreateSelect(ValueCompare, ResultVector[0].first, SelectValue, 3705 "switch.select"); 3706 } 3707 3708 return nullptr; 3709 } 3710 3711 // Helper function to cleanup a switch instruction that has been converted into 3712 // a select, fixing up PHI nodes and basic blocks. 3713 static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI, 3714 Value *SelectValue, 3715 IRBuilder<> &Builder) { 3716 BasicBlock *SelectBB = SI->getParent(); 3717 while (PHI->getBasicBlockIndex(SelectBB) >= 0) 3718 PHI->removeIncomingValue(SelectBB); 3719 PHI->addIncoming(SelectValue, SelectBB); 3720 3721 Builder.CreateBr(PHI->getParent()); 3722 3723 // Remove the switch. 3724 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) { 3725 BasicBlock *Succ = SI->getSuccessor(i); 3726 3727 if (Succ == PHI->getParent()) 3728 continue; 3729 Succ->removePredecessor(SelectBB); 3730 } 3731 SI->eraseFromParent(); 3732 } 3733 3734 /// If the switch is only used to initialize one or more 3735 /// phi nodes in a common successor block with only two different 3736 /// constant values, replace the switch with select. 3737 static bool SwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder, 3738 AssumptionCache *AC, const DataLayout &DL) { 3739 Value *const Cond = SI->getCondition(); 3740 PHINode *PHI = nullptr; 3741 BasicBlock *CommonDest = nullptr; 3742 Constant *DefaultResult; 3743 SwitchCaseResultVectorTy UniqueResults; 3744 // Collect all the cases that will deliver the same value from the switch. 3745 if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult, 3746 DL)) 3747 return false; 3748 // Selects choose between maximum two values. 3749 if (UniqueResults.size() != 2) 3750 return false; 3751 assert(PHI != nullptr && "PHI for value select not found"); 3752 3753 Builder.SetInsertPoint(SI); 3754 Value *SelectValue = ConvertTwoCaseSwitch( 3755 UniqueResults, 3756 DefaultResult, Cond, Builder); 3757 if (SelectValue) { 3758 RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder); 3759 return true; 3760 } 3761 // The switch couldn't be converted into a select. 3762 return false; 3763 } 3764 3765 namespace { 3766 /// This class represents a lookup table that can be used to replace a switch. 3767 class SwitchLookupTable { 3768 public: 3769 /// Create a lookup table to use as a switch replacement with the contents 3770 /// of Values, using DefaultValue to fill any holes in the table. 3771 SwitchLookupTable( 3772 Module &M, uint64_t TableSize, ConstantInt *Offset, 3773 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values, 3774 Constant *DefaultValue, const DataLayout &DL); 3775 3776 /// Build instructions with Builder to retrieve the value at 3777 /// the position given by Index in the lookup table. 3778 Value *BuildLookup(Value *Index, IRBuilder<> &Builder); 3779 3780 /// Return true if a table with TableSize elements of 3781 /// type ElementType would fit in a target-legal register. 3782 static bool WouldFitInRegister(const DataLayout &DL, uint64_t TableSize, 3783 Type *ElementType); 3784 3785 private: 3786 // Depending on the contents of the table, it can be represented in 3787 // different ways. 3788 enum { 3789 // For tables where each element contains the same value, we just have to 3790 // store that single value and return it for each lookup. 3791 SingleValueKind, 3792 3793 // For tables where there is a linear relationship between table index 3794 // and values. We calculate the result with a simple multiplication 3795 // and addition instead of a table lookup. 3796 LinearMapKind, 3797 3798 // For small tables with integer elements, we can pack them into a bitmap 3799 // that fits into a target-legal register. Values are retrieved by 3800 // shift and mask operations. 3801 BitMapKind, 3802 3803 // The table is stored as an array of values. Values are retrieved by load 3804 // instructions from the table. 3805 ArrayKind 3806 } Kind; 3807 3808 // For SingleValueKind, this is the single value. 3809 Constant *SingleValue; 3810 3811 // For BitMapKind, this is the bitmap. 3812 ConstantInt *BitMap; 3813 IntegerType *BitMapElementTy; 3814 3815 // For LinearMapKind, these are the constants used to derive the value. 3816 ConstantInt *LinearOffset; 3817 ConstantInt *LinearMultiplier; 3818 3819 // For ArrayKind, this is the array. 3820 GlobalVariable *Array; 3821 }; 3822 } 3823 3824 SwitchLookupTable::SwitchLookupTable( 3825 Module &M, uint64_t TableSize, ConstantInt *Offset, 3826 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values, 3827 Constant *DefaultValue, const DataLayout &DL) 3828 : SingleValue(nullptr), BitMap(nullptr), BitMapElementTy(nullptr), 3829 LinearOffset(nullptr), LinearMultiplier(nullptr), Array(nullptr) { 3830 assert(Values.size() && "Can't build lookup table without values!"); 3831 assert(TableSize >= Values.size() && "Can't fit values in table!"); 3832 3833 // If all values in the table are equal, this is that value. 3834 SingleValue = Values.begin()->second; 3835 3836 Type *ValueType = Values.begin()->second->getType(); 3837 3838 // Build up the table contents. 3839 SmallVector<Constant*, 64> TableContents(TableSize); 3840 for (size_t I = 0, E = Values.size(); I != E; ++I) { 3841 ConstantInt *CaseVal = Values[I].first; 3842 Constant *CaseRes = Values[I].second; 3843 assert(CaseRes->getType() == ValueType); 3844 3845 uint64_t Idx = (CaseVal->getValue() - Offset->getValue()) 3846 .getLimitedValue(); 3847 TableContents[Idx] = CaseRes; 3848 3849 if (CaseRes != SingleValue) 3850 SingleValue = nullptr; 3851 } 3852 3853 // Fill in any holes in the table with the default result. 3854 if (Values.size() < TableSize) { 3855 assert(DefaultValue && 3856 "Need a default value to fill the lookup table holes."); 3857 assert(DefaultValue->getType() == ValueType); 3858 for (uint64_t I = 0; I < TableSize; ++I) { 3859 if (!TableContents[I]) 3860 TableContents[I] = DefaultValue; 3861 } 3862 3863 if (DefaultValue != SingleValue) 3864 SingleValue = nullptr; 3865 } 3866 3867 // If each element in the table contains the same value, we only need to store 3868 // that single value. 3869 if (SingleValue) { 3870 Kind = SingleValueKind; 3871 return; 3872 } 3873 3874 // Check if we can derive the value with a linear transformation from the 3875 // table index. 3876 if (isa<IntegerType>(ValueType)) { 3877 bool LinearMappingPossible = true; 3878 APInt PrevVal; 3879 APInt DistToPrev; 3880 assert(TableSize >= 2 && "Should be a SingleValue table."); 3881 // Check if there is the same distance between two consecutive values. 3882 for (uint64_t I = 0; I < TableSize; ++I) { 3883 ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]); 3884 if (!ConstVal) { 3885 // This is an undef. We could deal with it, but undefs in lookup tables 3886 // are very seldom. It's probably not worth the additional complexity. 3887 LinearMappingPossible = false; 3888 break; 3889 } 3890 APInt Val = ConstVal->getValue(); 3891 if (I != 0) { 3892 APInt Dist = Val - PrevVal; 3893 if (I == 1) { 3894 DistToPrev = Dist; 3895 } else if (Dist != DistToPrev) { 3896 LinearMappingPossible = false; 3897 break; 3898 } 3899 } 3900 PrevVal = Val; 3901 } 3902 if (LinearMappingPossible) { 3903 LinearOffset = cast<ConstantInt>(TableContents[0]); 3904 LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev); 3905 Kind = LinearMapKind; 3906 ++NumLinearMaps; 3907 return; 3908 } 3909 } 3910 3911 // If the type is integer and the table fits in a register, build a bitmap. 3912 if (WouldFitInRegister(DL, TableSize, ValueType)) { 3913 IntegerType *IT = cast<IntegerType>(ValueType); 3914 APInt TableInt(TableSize * IT->getBitWidth(), 0); 3915 for (uint64_t I = TableSize; I > 0; --I) { 3916 TableInt <<= IT->getBitWidth(); 3917 // Insert values into the bitmap. Undef values are set to zero. 3918 if (!isa<UndefValue>(TableContents[I - 1])) { 3919 ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]); 3920 TableInt |= Val->getValue().zext(TableInt.getBitWidth()); 3921 } 3922 } 3923 BitMap = ConstantInt::get(M.getContext(), TableInt); 3924 BitMapElementTy = IT; 3925 Kind = BitMapKind; 3926 ++NumBitMaps; 3927 return; 3928 } 3929 3930 // Store the table in an array. 3931 ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize); 3932 Constant *Initializer = ConstantArray::get(ArrayTy, TableContents); 3933 3934 Array = new GlobalVariable(M, ArrayTy, /*constant=*/ true, 3935 GlobalVariable::PrivateLinkage, 3936 Initializer, 3937 "switch.table"); 3938 Array->setUnnamedAddr(true); 3939 Kind = ArrayKind; 3940 } 3941 3942 Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) { 3943 switch (Kind) { 3944 case SingleValueKind: 3945 return SingleValue; 3946 case LinearMapKind: { 3947 // Derive the result value from the input value. 3948 Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(), 3949 false, "switch.idx.cast"); 3950 if (!LinearMultiplier->isOne()) 3951 Result = Builder.CreateMul(Result, LinearMultiplier, "switch.idx.mult"); 3952 if (!LinearOffset->isZero()) 3953 Result = Builder.CreateAdd(Result, LinearOffset, "switch.offset"); 3954 return Result; 3955 } 3956 case BitMapKind: { 3957 // Type of the bitmap (e.g. i59). 3958 IntegerType *MapTy = BitMap->getType(); 3959 3960 // Cast Index to the same type as the bitmap. 3961 // Note: The Index is <= the number of elements in the table, so 3962 // truncating it to the width of the bitmask is safe. 3963 Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy, "switch.cast"); 3964 3965 // Multiply the shift amount by the element width. 3966 ShiftAmt = Builder.CreateMul(ShiftAmt, 3967 ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()), 3968 "switch.shiftamt"); 3969 3970 // Shift down. 3971 Value *DownShifted = Builder.CreateLShr(BitMap, ShiftAmt, 3972 "switch.downshift"); 3973 // Mask off. 3974 return Builder.CreateTrunc(DownShifted, BitMapElementTy, 3975 "switch.masked"); 3976 } 3977 case ArrayKind: { 3978 // Make sure the table index will not overflow when treated as signed. 3979 IntegerType *IT = cast<IntegerType>(Index->getType()); 3980 uint64_t TableSize = Array->getInitializer()->getType() 3981 ->getArrayNumElements(); 3982 if (TableSize > (1ULL << (IT->getBitWidth() - 1))) 3983 Index = Builder.CreateZExt(Index, 3984 IntegerType::get(IT->getContext(), 3985 IT->getBitWidth() + 1), 3986 "switch.tableidx.zext"); 3987 3988 Value *GEPIndices[] = { Builder.getInt32(0), Index }; 3989 Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array, 3990 GEPIndices, "switch.gep"); 3991 return Builder.CreateLoad(GEP, "switch.load"); 3992 } 3993 } 3994 llvm_unreachable("Unknown lookup table kind!"); 3995 } 3996 3997 bool SwitchLookupTable::WouldFitInRegister(const DataLayout &DL, 3998 uint64_t TableSize, 3999 Type *ElementType) { 4000 auto *IT = dyn_cast<IntegerType>(ElementType); 4001 if (!IT) 4002 return false; 4003 // FIXME: If the type is wider than it needs to be, e.g. i8 but all values 4004 // are <= 15, we could try to narrow the type. 4005 4006 // Avoid overflow, fitsInLegalInteger uses unsigned int for the width. 4007 if (TableSize >= UINT_MAX/IT->getBitWidth()) 4008 return false; 4009 return DL.fitsInLegalInteger(TableSize * IT->getBitWidth()); 4010 } 4011 4012 /// Determine whether a lookup table should be built for this switch, based on 4013 /// the number of cases, size of the table, and the types of the results. 4014 static bool 4015 ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize, 4016 const TargetTransformInfo &TTI, const DataLayout &DL, 4017 const SmallDenseMap<PHINode *, Type *> &ResultTypes) { 4018 if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10) 4019 return false; // TableSize overflowed, or mul below might overflow. 4020 4021 bool AllTablesFitInRegister = true; 4022 bool HasIllegalType = false; 4023 for (const auto &I : ResultTypes) { 4024 Type *Ty = I.second; 4025 4026 // Saturate this flag to true. 4027 HasIllegalType = HasIllegalType || !TTI.isTypeLegal(Ty); 4028 4029 // Saturate this flag to false. 4030 AllTablesFitInRegister = AllTablesFitInRegister && 4031 SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty); 4032 4033 // If both flags saturate, we're done. NOTE: This *only* works with 4034 // saturating flags, and all flags have to saturate first due to the 4035 // non-deterministic behavior of iterating over a dense map. 4036 if (HasIllegalType && !AllTablesFitInRegister) 4037 break; 4038 } 4039 4040 // If each table would fit in a register, we should build it anyway. 4041 if (AllTablesFitInRegister) 4042 return true; 4043 4044 // Don't build a table that doesn't fit in-register if it has illegal types. 4045 if (HasIllegalType) 4046 return false; 4047 4048 // The table density should be at least 40%. This is the same criterion as for 4049 // jump tables, see SelectionDAGBuilder::handleJTSwitchCase. 4050 // FIXME: Find the best cut-off. 4051 return SI->getNumCases() * 10 >= TableSize * 4; 4052 } 4053 4054 /// Try to reuse the switch table index compare. Following pattern: 4055 /// \code 4056 /// if (idx < tablesize) 4057 /// r = table[idx]; // table does not contain default_value 4058 /// else 4059 /// r = default_value; 4060 /// if (r != default_value) 4061 /// ... 4062 /// \endcode 4063 /// Is optimized to: 4064 /// \code 4065 /// cond = idx < tablesize; 4066 /// if (cond) 4067 /// r = table[idx]; 4068 /// else 4069 /// r = default_value; 4070 /// if (cond) 4071 /// ... 4072 /// \endcode 4073 /// Jump threading will then eliminate the second if(cond). 4074 static void reuseTableCompare(User *PhiUser, BasicBlock *PhiBlock, 4075 BranchInst *RangeCheckBranch, Constant *DefaultValue, 4076 const SmallVectorImpl<std::pair<ConstantInt*, Constant*> >& Values) { 4077 4078 ICmpInst *CmpInst = dyn_cast<ICmpInst>(PhiUser); 4079 if (!CmpInst) 4080 return; 4081 4082 // We require that the compare is in the same block as the phi so that jump 4083 // threading can do its work afterwards. 4084 if (CmpInst->getParent() != PhiBlock) 4085 return; 4086 4087 Constant *CmpOp1 = dyn_cast<Constant>(CmpInst->getOperand(1)); 4088 if (!CmpOp1) 4089 return; 4090 4091 Value *RangeCmp = RangeCheckBranch->getCondition(); 4092 Constant *TrueConst = ConstantInt::getTrue(RangeCmp->getType()); 4093 Constant *FalseConst = ConstantInt::getFalse(RangeCmp->getType()); 4094 4095 // Check if the compare with the default value is constant true or false. 4096 Constant *DefaultConst = ConstantExpr::getICmp(CmpInst->getPredicate(), 4097 DefaultValue, CmpOp1, true); 4098 if (DefaultConst != TrueConst && DefaultConst != FalseConst) 4099 return; 4100 4101 // Check if the compare with the case values is distinct from the default 4102 // compare result. 4103 for (auto ValuePair : Values) { 4104 Constant *CaseConst = ConstantExpr::getICmp(CmpInst->getPredicate(), 4105 ValuePair.second, CmpOp1, true); 4106 if (!CaseConst || CaseConst == DefaultConst) 4107 return; 4108 assert((CaseConst == TrueConst || CaseConst == FalseConst) && 4109 "Expect true or false as compare result."); 4110 } 4111 4112 // Check if the branch instruction dominates the phi node. It's a simple 4113 // dominance check, but sufficient for our needs. 4114 // Although this check is invariant in the calling loops, it's better to do it 4115 // at this late stage. Practically we do it at most once for a switch. 4116 BasicBlock *BranchBlock = RangeCheckBranch->getParent(); 4117 for (auto PI = pred_begin(PhiBlock), E = pred_end(PhiBlock); PI != E; ++PI) { 4118 BasicBlock *Pred = *PI; 4119 if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock) 4120 return; 4121 } 4122 4123 if (DefaultConst == FalseConst) { 4124 // The compare yields the same result. We can replace it. 4125 CmpInst->replaceAllUsesWith(RangeCmp); 4126 ++NumTableCmpReuses; 4127 } else { 4128 // The compare yields the same result, just inverted. We can replace it. 4129 Value *InvertedTableCmp = BinaryOperator::CreateXor(RangeCmp, 4130 ConstantInt::get(RangeCmp->getType(), 1), "inverted.cmp", 4131 RangeCheckBranch); 4132 CmpInst->replaceAllUsesWith(InvertedTableCmp); 4133 ++NumTableCmpReuses; 4134 } 4135 } 4136 4137 /// If the switch is only used to initialize one or more phi nodes in a common 4138 /// successor block with different constant values, replace the switch with 4139 /// lookup tables. 4140 static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder, 4141 const DataLayout &DL, 4142 const TargetTransformInfo &TTI) { 4143 assert(SI->getNumCases() > 1 && "Degenerate switch?"); 4144 4145 // Only build lookup table when we have a target that supports it. 4146 if (!TTI.shouldBuildLookupTables()) 4147 return false; 4148 4149 // FIXME: If the switch is too sparse for a lookup table, perhaps we could 4150 // split off a dense part and build a lookup table for that. 4151 4152 // FIXME: This creates arrays of GEPs to constant strings, which means each 4153 // GEP needs a runtime relocation in PIC code. We should just build one big 4154 // string and lookup indices into that. 4155 4156 // Ignore switches with less than three cases. Lookup tables will not make them 4157 // faster, so we don't analyze them. 4158 if (SI->getNumCases() < 3) 4159 return false; 4160 4161 // Figure out the corresponding result for each case value and phi node in the 4162 // common destination, as well as the min and max case values. 4163 assert(SI->case_begin() != SI->case_end()); 4164 SwitchInst::CaseIt CI = SI->case_begin(); 4165 ConstantInt *MinCaseVal = CI.getCaseValue(); 4166 ConstantInt *MaxCaseVal = CI.getCaseValue(); 4167 4168 BasicBlock *CommonDest = nullptr; 4169 typedef SmallVector<std::pair<ConstantInt*, Constant*>, 4> ResultListTy; 4170 SmallDenseMap<PHINode*, ResultListTy> ResultLists; 4171 SmallDenseMap<PHINode*, Constant*> DefaultResults; 4172 SmallDenseMap<PHINode*, Type*> ResultTypes; 4173 SmallVector<PHINode*, 4> PHIs; 4174 4175 for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) { 4176 ConstantInt *CaseVal = CI.getCaseValue(); 4177 if (CaseVal->getValue().slt(MinCaseVal->getValue())) 4178 MinCaseVal = CaseVal; 4179 if (CaseVal->getValue().sgt(MaxCaseVal->getValue())) 4180 MaxCaseVal = CaseVal; 4181 4182 // Resulting value at phi nodes for this case value. 4183 typedef SmallVector<std::pair<PHINode*, Constant*>, 4> ResultsTy; 4184 ResultsTy Results; 4185 if (!GetCaseResults(SI, CaseVal, CI.getCaseSuccessor(), &CommonDest, 4186 Results, DL)) 4187 return false; 4188 4189 // Append the result from this case to the list for each phi. 4190 for (const auto &I : Results) { 4191 PHINode *PHI = I.first; 4192 Constant *Value = I.second; 4193 if (!ResultLists.count(PHI)) 4194 PHIs.push_back(PHI); 4195 ResultLists[PHI].push_back(std::make_pair(CaseVal, Value)); 4196 } 4197 } 4198 4199 // Keep track of the result types. 4200 for (PHINode *PHI : PHIs) { 4201 ResultTypes[PHI] = ResultLists[PHI][0].second->getType(); 4202 } 4203 4204 uint64_t NumResults = ResultLists[PHIs[0]].size(); 4205 APInt RangeSpread = MaxCaseVal->getValue() - MinCaseVal->getValue(); 4206 uint64_t TableSize = RangeSpread.getLimitedValue() + 1; 4207 bool TableHasHoles = (NumResults < TableSize); 4208 4209 // If the table has holes, we need a constant result for the default case 4210 // or a bitmask that fits in a register. 4211 SmallVector<std::pair<PHINode*, Constant*>, 4> DefaultResultsList; 4212 bool HasDefaultResults = GetCaseResults(SI, nullptr, SI->getDefaultDest(), 4213 &CommonDest, DefaultResultsList, DL); 4214 4215 bool NeedMask = (TableHasHoles && !HasDefaultResults); 4216 if (NeedMask) { 4217 // As an extra penalty for the validity test we require more cases. 4218 if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark). 4219 return false; 4220 if (!DL.fitsInLegalInteger(TableSize)) 4221 return false; 4222 } 4223 4224 for (const auto &I : DefaultResultsList) { 4225 PHINode *PHI = I.first; 4226 Constant *Result = I.second; 4227 DefaultResults[PHI] = Result; 4228 } 4229 4230 if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes)) 4231 return false; 4232 4233 // Create the BB that does the lookups. 4234 Module &Mod = *CommonDest->getParent()->getParent(); 4235 BasicBlock *LookupBB = BasicBlock::Create(Mod.getContext(), 4236 "switch.lookup", 4237 CommonDest->getParent(), 4238 CommonDest); 4239 4240 // Compute the table index value. 4241 Builder.SetInsertPoint(SI); 4242 Value *TableIndex = Builder.CreateSub(SI->getCondition(), MinCaseVal, 4243 "switch.tableidx"); 4244 4245 // Compute the maximum table size representable by the integer type we are 4246 // switching upon. 4247 unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits(); 4248 uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize; 4249 assert(MaxTableSize >= TableSize && 4250 "It is impossible for a switch to have more entries than the max " 4251 "representable value of its input integer type's size."); 4252 4253 // If the default destination is unreachable, or if the lookup table covers 4254 // all values of the conditional variable, branch directly to the lookup table 4255 // BB. Otherwise, check that the condition is within the case range. 4256 const bool DefaultIsReachable = 4257 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg()); 4258 const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize); 4259 BranchInst *RangeCheckBranch = nullptr; 4260 4261 if (!DefaultIsReachable || GeneratingCoveredLookupTable) { 4262 Builder.CreateBr(LookupBB); 4263 // Note: We call removeProdecessor later since we need to be able to get the 4264 // PHI value for the default case in case we're using a bit mask. 4265 } else { 4266 Value *Cmp = Builder.CreateICmpULT(TableIndex, ConstantInt::get( 4267 MinCaseVal->getType(), TableSize)); 4268 RangeCheckBranch = Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest()); 4269 } 4270 4271 // Populate the BB that does the lookups. 4272 Builder.SetInsertPoint(LookupBB); 4273 4274 if (NeedMask) { 4275 // Before doing the lookup we do the hole check. 4276 // The LookupBB is therefore re-purposed to do the hole check 4277 // and we create a new LookupBB. 4278 BasicBlock *MaskBB = LookupBB; 4279 MaskBB->setName("switch.hole_check"); 4280 LookupBB = BasicBlock::Create(Mod.getContext(), 4281 "switch.lookup", 4282 CommonDest->getParent(), 4283 CommonDest); 4284 4285 // Make the mask's bitwidth at least 8bit and a power-of-2 to avoid 4286 // unnecessary illegal types. 4287 uint64_t TableSizePowOf2 = NextPowerOf2(std::max(7ULL, TableSize - 1ULL)); 4288 APInt MaskInt(TableSizePowOf2, 0); 4289 APInt One(TableSizePowOf2, 1); 4290 // Build bitmask; fill in a 1 bit for every case. 4291 const ResultListTy &ResultList = ResultLists[PHIs[0]]; 4292 for (size_t I = 0, E = ResultList.size(); I != E; ++I) { 4293 uint64_t Idx = (ResultList[I].first->getValue() - 4294 MinCaseVal->getValue()).getLimitedValue(); 4295 MaskInt |= One << Idx; 4296 } 4297 ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt); 4298 4299 // Get the TableIndex'th bit of the bitmask. 4300 // If this bit is 0 (meaning hole) jump to the default destination, 4301 // else continue with table lookup. 4302 IntegerType *MapTy = TableMask->getType(); 4303 Value *MaskIndex = Builder.CreateZExtOrTrunc(TableIndex, MapTy, 4304 "switch.maskindex"); 4305 Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, 4306 "switch.shifted"); 4307 Value *LoBit = Builder.CreateTrunc(Shifted, 4308 Type::getInt1Ty(Mod.getContext()), 4309 "switch.lobit"); 4310 Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest()); 4311 4312 Builder.SetInsertPoint(LookupBB); 4313 AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent()); 4314 } 4315 4316 if (!DefaultIsReachable || GeneratingCoveredLookupTable) { 4317 // We cached PHINodes in PHIs, to avoid accessing deleted PHINodes later, 4318 // do not delete PHINodes here. 4319 SI->getDefaultDest()->removePredecessor(SI->getParent(), 4320 /*DontDeleteUselessPHIs=*/true); 4321 } 4322 4323 bool ReturnedEarly = false; 4324 for (size_t I = 0, E = PHIs.size(); I != E; ++I) { 4325 PHINode *PHI = PHIs[I]; 4326 const ResultListTy &ResultList = ResultLists[PHI]; 4327 4328 // If using a bitmask, use any value to fill the lookup table holes. 4329 Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI]; 4330 SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL); 4331 4332 Value *Result = Table.BuildLookup(TableIndex, Builder); 4333 4334 // If the result is used to return immediately from the function, we want to 4335 // do that right here. 4336 if (PHI->hasOneUse() && isa<ReturnInst>(*PHI->user_begin()) && 4337 PHI->user_back() == CommonDest->getFirstNonPHIOrDbg()) { 4338 Builder.CreateRet(Result); 4339 ReturnedEarly = true; 4340 break; 4341 } 4342 4343 // Do a small peephole optimization: re-use the switch table compare if 4344 // possible. 4345 if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) { 4346 BasicBlock *PhiBlock = PHI->getParent(); 4347 // Search for compare instructions which use the phi. 4348 for (auto *User : PHI->users()) { 4349 reuseTableCompare(User, PhiBlock, RangeCheckBranch, DV, ResultList); 4350 } 4351 } 4352 4353 PHI->addIncoming(Result, LookupBB); 4354 } 4355 4356 if (!ReturnedEarly) 4357 Builder.CreateBr(CommonDest); 4358 4359 // Remove the switch. 4360 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) { 4361 BasicBlock *Succ = SI->getSuccessor(i); 4362 4363 if (Succ == SI->getDefaultDest()) 4364 continue; 4365 Succ->removePredecessor(SI->getParent()); 4366 } 4367 SI->eraseFromParent(); 4368 4369 ++NumLookupTables; 4370 if (NeedMask) 4371 ++NumLookupTablesHoles; 4372 return true; 4373 } 4374 4375 bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) { 4376 BasicBlock *BB = SI->getParent(); 4377 4378 if (isValueEqualityComparison(SI)) { 4379 // If we only have one predecessor, and if it is a branch on this value, 4380 // see if that predecessor totally determines the outcome of this switch. 4381 if (BasicBlock *OnlyPred = BB->getSinglePredecessor()) 4382 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder)) 4383 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4384 4385 Value *Cond = SI->getCondition(); 4386 if (SelectInst *Select = dyn_cast<SelectInst>(Cond)) 4387 if (SimplifySwitchOnSelect(SI, Select)) 4388 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4389 4390 // If the block only contains the switch, see if we can fold the block 4391 // away into any preds. 4392 BasicBlock::iterator BBI = BB->begin(); 4393 // Ignore dbg intrinsics. 4394 while (isa<DbgInfoIntrinsic>(BBI)) 4395 ++BBI; 4396 if (SI == &*BBI) 4397 if (FoldValueComparisonIntoPredecessors(SI, Builder)) 4398 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4399 } 4400 4401 // Try to transform the switch into an icmp and a branch. 4402 if (TurnSwitchRangeIntoICmp(SI, Builder)) 4403 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4404 4405 // Remove unreachable cases. 4406 if (EliminateDeadSwitchCases(SI, AC, DL)) 4407 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4408 4409 if (SwitchToSelect(SI, Builder, AC, DL)) 4410 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4411 4412 if (ForwardSwitchConditionToPHI(SI)) 4413 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4414 4415 if (SwitchToLookupTable(SI, Builder, DL, TTI)) 4416 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4417 4418 return false; 4419 } 4420 4421 bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) { 4422 BasicBlock *BB = IBI->getParent(); 4423 bool Changed = false; 4424 4425 // Eliminate redundant destinations. 4426 SmallPtrSet<Value *, 8> Succs; 4427 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { 4428 BasicBlock *Dest = IBI->getDestination(i); 4429 if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) { 4430 Dest->removePredecessor(BB); 4431 IBI->removeDestination(i); 4432 --i; --e; 4433 Changed = true; 4434 } 4435 } 4436 4437 if (IBI->getNumDestinations() == 0) { 4438 // If the indirectbr has no successors, change it to unreachable. 4439 new UnreachableInst(IBI->getContext(), IBI); 4440 EraseTerminatorInstAndDCECond(IBI); 4441 return true; 4442 } 4443 4444 if (IBI->getNumDestinations() == 1) { 4445 // If the indirectbr has one successor, change it to a direct branch. 4446 BranchInst::Create(IBI->getDestination(0), IBI); 4447 EraseTerminatorInstAndDCECond(IBI); 4448 return true; 4449 } 4450 4451 if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) { 4452 if (SimplifyIndirectBrOnSelect(IBI, SI)) 4453 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4454 } 4455 return Changed; 4456 } 4457 4458 /// Given an block with only a single landing pad and a unconditional branch 4459 /// try to find another basic block which this one can be merged with. This 4460 /// handles cases where we have multiple invokes with unique landing pads, but 4461 /// a shared handler. 4462 /// 4463 /// We specifically choose to not worry about merging non-empty blocks 4464 /// here. That is a PRE/scheduling problem and is best solved elsewhere. In 4465 /// practice, the optimizer produces empty landing pad blocks quite frequently 4466 /// when dealing with exception dense code. (see: instcombine, gvn, if-else 4467 /// sinking in this file) 4468 /// 4469 /// This is primarily a code size optimization. We need to avoid performing 4470 /// any transform which might inhibit optimization (such as our ability to 4471 /// specialize a particular handler via tail commoning). We do this by not 4472 /// merging any blocks which require us to introduce a phi. Since the same 4473 /// values are flowing through both blocks, we don't loose any ability to 4474 /// specialize. If anything, we make such specialization more likely. 4475 /// 4476 /// TODO - This transformation could remove entries from a phi in the target 4477 /// block when the inputs in the phi are the same for the two blocks being 4478 /// merged. In some cases, this could result in removal of the PHI entirely. 4479 static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI, 4480 BasicBlock *BB) { 4481 auto Succ = BB->getUniqueSuccessor(); 4482 assert(Succ); 4483 // If there's a phi in the successor block, we'd likely have to introduce 4484 // a phi into the merged landing pad block. 4485 if (isa<PHINode>(*Succ->begin())) 4486 return false; 4487 4488 for (BasicBlock *OtherPred : predecessors(Succ)) { 4489 if (BB == OtherPred) 4490 continue; 4491 BasicBlock::iterator I = OtherPred->begin(); 4492 LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(I); 4493 if (!LPad2 || !LPad2->isIdenticalTo(LPad)) 4494 continue; 4495 for (++I; isa<DbgInfoIntrinsic>(I); ++I) {} 4496 BranchInst *BI2 = dyn_cast<BranchInst>(I); 4497 if (!BI2 || !BI2->isIdenticalTo(BI)) 4498 continue; 4499 4500 // We've found an identical block. Update our predeccessors to take that 4501 // path instead and make ourselves dead. 4502 SmallSet<BasicBlock *, 16> Preds; 4503 Preds.insert(pred_begin(BB), pred_end(BB)); 4504 for (BasicBlock *Pred : Preds) { 4505 InvokeInst *II = cast<InvokeInst>(Pred->getTerminator()); 4506 assert(II->getNormalDest() != BB && 4507 II->getUnwindDest() == BB && "unexpected successor"); 4508 II->setUnwindDest(OtherPred); 4509 } 4510 4511 // The debug info in OtherPred doesn't cover the merged control flow that 4512 // used to go through BB. We need to delete it or update it. 4513 for (auto I = OtherPred->begin(), E = OtherPred->end(); 4514 I != E;) { 4515 Instruction &Inst = *I; I++; 4516 if (isa<DbgInfoIntrinsic>(Inst)) 4517 Inst.eraseFromParent(); 4518 } 4519 4520 SmallSet<BasicBlock *, 16> Succs; 4521 Succs.insert(succ_begin(BB), succ_end(BB)); 4522 for (BasicBlock *Succ : Succs) { 4523 Succ->removePredecessor(BB); 4524 } 4525 4526 IRBuilder<> Builder(BI); 4527 Builder.CreateUnreachable(); 4528 BI->eraseFromParent(); 4529 return true; 4530 } 4531 return false; 4532 } 4533 4534 bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder){ 4535 BasicBlock *BB = BI->getParent(); 4536 4537 if (SinkCommon && SinkThenElseCodeToEnd(BI)) 4538 return true; 4539 4540 // If the Terminator is the only non-phi instruction, simplify the block. 4541 BasicBlock::iterator I = BB->getFirstNonPHIOrDbg(); 4542 if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() && 4543 TryToSimplifyUncondBranchFromEmptyBlock(BB)) 4544 return true; 4545 4546 // If the only instruction in the block is a seteq/setne comparison 4547 // against a constant, try to simplify the block. 4548 if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) 4549 if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) { 4550 for (++I; isa<DbgInfoIntrinsic>(I); ++I) 4551 ; 4552 if (I->isTerminator() && 4553 TryToSimplifyUncondBranchWithICmpInIt(ICI, Builder, DL, TTI, 4554 BonusInstThreshold, AC)) 4555 return true; 4556 } 4557 4558 // See if we can merge an empty landing pad block with another which is 4559 // equivalent. 4560 if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(I)) { 4561 for (++I; isa<DbgInfoIntrinsic>(I); ++I) {} 4562 if (I->isTerminator() && 4563 TryToMergeLandingPad(LPad, BI, BB)) 4564 return true; 4565 } 4566 4567 // If this basic block is ONLY a compare and a branch, and if a predecessor 4568 // branches to us and our successor, fold the comparison into the 4569 // predecessor and use logical operations to update the incoming value 4570 // for PHI nodes in common successor. 4571 if (FoldBranchToCommonDest(BI, BonusInstThreshold)) 4572 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4573 return false; 4574 } 4575 4576 4577 bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) { 4578 BasicBlock *BB = BI->getParent(); 4579 4580 // Conditional branch 4581 if (isValueEqualityComparison(BI)) { 4582 // If we only have one predecessor, and if it is a branch on this value, 4583 // see if that predecessor totally determines the outcome of this 4584 // switch. 4585 if (BasicBlock *OnlyPred = BB->getSinglePredecessor()) 4586 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder)) 4587 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4588 4589 // This block must be empty, except for the setcond inst, if it exists. 4590 // Ignore dbg intrinsics. 4591 BasicBlock::iterator I = BB->begin(); 4592 // Ignore dbg intrinsics. 4593 while (isa<DbgInfoIntrinsic>(I)) 4594 ++I; 4595 if (&*I == BI) { 4596 if (FoldValueComparisonIntoPredecessors(BI, Builder)) 4597 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4598 } else if (&*I == cast<Instruction>(BI->getCondition())){ 4599 ++I; 4600 // Ignore dbg intrinsics. 4601 while (isa<DbgInfoIntrinsic>(I)) 4602 ++I; 4603 if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder)) 4604 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4605 } 4606 } 4607 4608 // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction. 4609 if (SimplifyBranchOnICmpChain(BI, Builder, DL)) 4610 return true; 4611 4612 // If this basic block is ONLY a compare and a branch, and if a predecessor 4613 // branches to us and one of our successors, fold the comparison into the 4614 // predecessor and use logical operations to pick the right destination. 4615 if (FoldBranchToCommonDest(BI, BonusInstThreshold)) 4616 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4617 4618 // We have a conditional branch to two blocks that are only reachable 4619 // from BI. We know that the condbr dominates the two blocks, so see if 4620 // there is any identical code in the "then" and "else" blocks. If so, we 4621 // can hoist it up to the branching block. 4622 if (BI->getSuccessor(0)->getSinglePredecessor()) { 4623 if (BI->getSuccessor(1)->getSinglePredecessor()) { 4624 if (HoistThenElseCodeToIf(BI, TTI)) 4625 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4626 } else { 4627 // If Successor #1 has multiple preds, we may be able to conditionally 4628 // execute Successor #0 if it branches to Successor #1. 4629 TerminatorInst *Succ0TI = BI->getSuccessor(0)->getTerminator(); 4630 if (Succ0TI->getNumSuccessors() == 1 && 4631 Succ0TI->getSuccessor(0) == BI->getSuccessor(1)) 4632 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), TTI)) 4633 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4634 } 4635 } else if (BI->getSuccessor(1)->getSinglePredecessor()) { 4636 // If Successor #0 has multiple preds, we may be able to conditionally 4637 // execute Successor #1 if it branches to Successor #0. 4638 TerminatorInst *Succ1TI = BI->getSuccessor(1)->getTerminator(); 4639 if (Succ1TI->getNumSuccessors() == 1 && 4640 Succ1TI->getSuccessor(0) == BI->getSuccessor(0)) 4641 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), TTI)) 4642 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4643 } 4644 4645 // If this is a branch on a phi node in the current block, thread control 4646 // through this block if any PHI node entries are constants. 4647 if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition())) 4648 if (PN->getParent() == BI->getParent()) 4649 if (FoldCondBranchOnPHI(BI, DL)) 4650 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4651 4652 // Scan predecessor blocks for conditional branches. 4653 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 4654 if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) 4655 if (PBI != BI && PBI->isConditional()) 4656 if (SimplifyCondBranchToCondBranch(PBI, BI)) 4657 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true; 4658 4659 return false; 4660 } 4661 4662 /// Check if passing a value to an instruction will cause undefined behavior. 4663 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) { 4664 Constant *C = dyn_cast<Constant>(V); 4665 if (!C) 4666 return false; 4667 4668 if (I->use_empty()) 4669 return false; 4670 4671 if (C->isNullValue()) { 4672 // Only look at the first use, avoid hurting compile time with long uselists 4673 User *Use = *I->user_begin(); 4674 4675 // Now make sure that there are no instructions in between that can alter 4676 // control flow (eg. calls) 4677 for (BasicBlock::iterator i = ++BasicBlock::iterator(I); &*i != Use; ++i) 4678 if (i == I->getParent()->end() || i->mayHaveSideEffects()) 4679 return false; 4680 4681 // Look through GEPs. A load from a GEP derived from NULL is still undefined 4682 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use)) 4683 if (GEP->getPointerOperand() == I) 4684 return passingValueIsAlwaysUndefined(V, GEP); 4685 4686 // Look through bitcasts. 4687 if (BitCastInst *BC = dyn_cast<BitCastInst>(Use)) 4688 return passingValueIsAlwaysUndefined(V, BC); 4689 4690 // Load from null is undefined. 4691 if (LoadInst *LI = dyn_cast<LoadInst>(Use)) 4692 if (!LI->isVolatile()) 4693 return LI->getPointerAddressSpace() == 0; 4694 4695 // Store to null is undefined. 4696 if (StoreInst *SI = dyn_cast<StoreInst>(Use)) 4697 if (!SI->isVolatile()) 4698 return SI->getPointerAddressSpace() == 0 && SI->getPointerOperand() == I; 4699 } 4700 return false; 4701 } 4702 4703 /// If BB has an incoming value that will always trigger undefined behavior 4704 /// (eg. null pointer dereference), remove the branch leading here. 4705 static bool removeUndefIntroducingPredecessor(BasicBlock *BB) { 4706 for (BasicBlock::iterator i = BB->begin(); 4707 PHINode *PHI = dyn_cast<PHINode>(i); ++i) 4708 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) 4709 if (passingValueIsAlwaysUndefined(PHI->getIncomingValue(i), PHI)) { 4710 TerminatorInst *T = PHI->getIncomingBlock(i)->getTerminator(); 4711 IRBuilder<> Builder(T); 4712 if (BranchInst *BI = dyn_cast<BranchInst>(T)) { 4713 BB->removePredecessor(PHI->getIncomingBlock(i)); 4714 // Turn uncoditional branches into unreachables and remove the dead 4715 // destination from conditional branches. 4716 if (BI->isUnconditional()) 4717 Builder.CreateUnreachable(); 4718 else 4719 Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) : 4720 BI->getSuccessor(0)); 4721 BI->eraseFromParent(); 4722 return true; 4723 } 4724 // TODO: SwitchInst. 4725 } 4726 4727 return false; 4728 } 4729 4730 bool SimplifyCFGOpt::run(BasicBlock *BB) { 4731 bool Changed = false; 4732 4733 assert(BB && BB->getParent() && "Block not embedded in function!"); 4734 assert(BB->getTerminator() && "Degenerate basic block encountered!"); 4735 4736 // Remove basic blocks that have no predecessors (except the entry block)... 4737 // or that just have themself as a predecessor. These are unreachable. 4738 if ((pred_empty(BB) && 4739 BB != &BB->getParent()->getEntryBlock()) || 4740 BB->getSinglePredecessor() == BB) { 4741 DEBUG(dbgs() << "Removing BB: \n" << *BB); 4742 DeleteDeadBlock(BB); 4743 return true; 4744 } 4745 4746 // Check to see if we can constant propagate this terminator instruction 4747 // away... 4748 Changed |= ConstantFoldTerminator(BB, true); 4749 4750 // Check for and eliminate duplicate PHI nodes in this block. 4751 Changed |= EliminateDuplicatePHINodes(BB); 4752 4753 // Check for and remove branches that will always cause undefined behavior. 4754 Changed |= removeUndefIntroducingPredecessor(BB); 4755 4756 // Merge basic blocks into their predecessor if there is only one distinct 4757 // pred, and if there is only one distinct successor of the predecessor, and 4758 // if there are no PHI nodes. 4759 // 4760 if (MergeBlockIntoPredecessor(BB)) 4761 return true; 4762 4763 IRBuilder<> Builder(BB); 4764 4765 // If there is a trivial two-entry PHI node in this basic block, and we can 4766 // eliminate it, do so now. 4767 if (PHINode *PN = dyn_cast<PHINode>(BB->begin())) 4768 if (PN->getNumIncomingValues() == 2) 4769 Changed |= FoldTwoEntryPHINode(PN, TTI, DL); 4770 4771 Builder.SetInsertPoint(BB->getTerminator()); 4772 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { 4773 if (BI->isUnconditional()) { 4774 if (SimplifyUncondBranch(BI, Builder)) return true; 4775 } else { 4776 if (SimplifyCondBranch(BI, Builder)) return true; 4777 } 4778 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { 4779 if (SimplifyReturn(RI, Builder)) return true; 4780 } else if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) { 4781 if (SimplifyResume(RI, Builder)) return true; 4782 } else if (CleanupReturnInst *RI = 4783 dyn_cast<CleanupReturnInst>(BB->getTerminator())) { 4784 if (SimplifyCleanupReturn(RI)) return true; 4785 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { 4786 if (SimplifySwitch(SI, Builder)) return true; 4787 } else if (UnreachableInst *UI = 4788 dyn_cast<UnreachableInst>(BB->getTerminator())) { 4789 if (SimplifyUnreachable(UI)) return true; 4790 } else if (IndirectBrInst *IBI = 4791 dyn_cast<IndirectBrInst>(BB->getTerminator())) { 4792 if (SimplifyIndirectBr(IBI)) return true; 4793 } 4794 4795 return Changed; 4796 } 4797 4798 /// This function is used to do simplification of a CFG. 4799 /// For example, it adjusts branches to branches to eliminate the extra hop, 4800 /// eliminates unreachable basic blocks, and does other "peephole" optimization 4801 /// of the CFG. It returns true if a modification was made. 4802 /// 4803 bool llvm::SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, 4804 unsigned BonusInstThreshold, AssumptionCache *AC) { 4805 return SimplifyCFGOpt(TTI, BB->getModule()->getDataLayout(), 4806 BonusInstThreshold, AC).run(BB); 4807 } 4808