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