1 //===- ConstantHoisting.cpp - Prepare code for expensive constants --------===// 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 // This pass identifies expensive constants to hoist and coalesces them to 11 // better prepare it for SelectionDAG-based code generation. This works around 12 // the limitations of the basic-block-at-a-time approach. 13 // 14 // First it scans all instructions for integer constants and calculates its 15 // cost. If the constant can be folded into the instruction (the cost is 16 // TCC_Free) or the cost is just a simple operation (TCC_BASIC), then we don't 17 // consider it expensive and leave it alone. This is the default behavior and 18 // the default implementation of getIntImmCost will always return TCC_Free. 19 // 20 // If the cost is more than TCC_BASIC, then the integer constant can't be folded 21 // into the instruction and it might be beneficial to hoist the constant. 22 // Similar constants are coalesced to reduce register pressure and 23 // materialization code. 24 // 25 // When a constant is hoisted, it is also hidden behind a bitcast to force it to 26 // be live-out of the basic block. Otherwise the constant would be just 27 // duplicated and each basic block would have its own copy in the SelectionDAG. 28 // The SelectionDAG recognizes such constants as opaque and doesn't perform 29 // certain transformations on them, which would create a new expensive constant. 30 // 31 // This optimization is only applied to integer constants in instructions and 32 // simple (this means not nested) constant cast expressions. For example: 33 // %0 = load i64* inttoptr (i64 big_constant to i64*) 34 //===----------------------------------------------------------------------===// 35 36 #include "llvm/Transforms/Scalar/ConstantHoisting.h" 37 #include "llvm/ADT/SmallSet.h" 38 #include "llvm/ADT/SmallVector.h" 39 #include "llvm/ADT/Statistic.h" 40 #include "llvm/IR/Constants.h" 41 #include "llvm/IR/GetElementPtrTypeIterator.h" 42 #include "llvm/IR/IntrinsicInst.h" 43 #include "llvm/Pass.h" 44 #include "llvm/Support/Debug.h" 45 #include "llvm/Support/raw_ostream.h" 46 #include "llvm/Transforms/Scalar.h" 47 #include <tuple> 48 49 using namespace llvm; 50 using namespace consthoist; 51 52 #define DEBUG_TYPE "consthoist" 53 54 STATISTIC(NumConstantsHoisted, "Number of constants hoisted"); 55 STATISTIC(NumConstantsRebased, "Number of constants rebased"); 56 57 static cl::opt<bool> ConstHoistWithBlockFrequency( 58 "consthoist-with-block-frequency", cl::init(true), cl::Hidden, 59 cl::desc("Enable the use of the block frequency analysis to reduce the " 60 "chance to execute const materialization more frequently than " 61 "without hoisting.")); 62 63 namespace { 64 /// \brief The constant hoisting pass. 65 class ConstantHoistingLegacyPass : public FunctionPass { 66 public: 67 static char ID; // Pass identification, replacement for typeid 68 ConstantHoistingLegacyPass() : FunctionPass(ID) { 69 initializeConstantHoistingLegacyPassPass(*PassRegistry::getPassRegistry()); 70 } 71 72 bool runOnFunction(Function &Fn) override; 73 74 StringRef getPassName() const override { return "Constant Hoisting"; } 75 76 void getAnalysisUsage(AnalysisUsage &AU) const override { 77 AU.setPreservesCFG(); 78 if (ConstHoistWithBlockFrequency) 79 AU.addRequired<BlockFrequencyInfoWrapperPass>(); 80 AU.addRequired<DominatorTreeWrapperPass>(); 81 AU.addRequired<TargetTransformInfoWrapperPass>(); 82 } 83 84 void releaseMemory() override { Impl.releaseMemory(); } 85 86 private: 87 ConstantHoistingPass Impl; 88 }; 89 } 90 91 char ConstantHoistingLegacyPass::ID = 0; 92 INITIALIZE_PASS_BEGIN(ConstantHoistingLegacyPass, "consthoist", 93 "Constant Hoisting", false, false) 94 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) 95 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 96 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 97 INITIALIZE_PASS_END(ConstantHoistingLegacyPass, "consthoist", 98 "Constant Hoisting", false, false) 99 100 FunctionPass *llvm::createConstantHoistingPass() { 101 return new ConstantHoistingLegacyPass(); 102 } 103 104 /// \brief Perform the constant hoisting optimization for the given function. 105 bool ConstantHoistingLegacyPass::runOnFunction(Function &Fn) { 106 if (skipFunction(Fn)) 107 return false; 108 109 DEBUG(dbgs() << "********** Begin Constant Hoisting **********\n"); 110 DEBUG(dbgs() << "********** Function: " << Fn.getName() << '\n'); 111 112 bool MadeChange = 113 Impl.runImpl(Fn, getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn), 114 getAnalysis<DominatorTreeWrapperPass>().getDomTree(), 115 ConstHoistWithBlockFrequency 116 ? &getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI() 117 : nullptr, 118 Fn.getEntryBlock()); 119 120 if (MadeChange) { 121 DEBUG(dbgs() << "********** Function after Constant Hoisting: " 122 << Fn.getName() << '\n'); 123 DEBUG(dbgs() << Fn); 124 } 125 DEBUG(dbgs() << "********** End Constant Hoisting **********\n"); 126 127 return MadeChange; 128 } 129 130 131 /// \brief Find the constant materialization insertion point. 132 Instruction *ConstantHoistingPass::findMatInsertPt(Instruction *Inst, 133 unsigned Idx) const { 134 // If the operand is a cast instruction, then we have to materialize the 135 // constant before the cast instruction. 136 if (Idx != ~0U) { 137 Value *Opnd = Inst->getOperand(Idx); 138 if (auto CastInst = dyn_cast<Instruction>(Opnd)) 139 if (CastInst->isCast()) 140 return CastInst; 141 } 142 143 // The simple and common case. This also includes constant expressions. 144 if (!isa<PHINode>(Inst) && !Inst->isEHPad()) 145 return Inst; 146 147 // We can't insert directly before a phi node or an eh pad. Insert before 148 // the terminator of the incoming or dominating block. 149 assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!"); 150 if (Idx != ~0U && isa<PHINode>(Inst)) 151 return cast<PHINode>(Inst)->getIncomingBlock(Idx)->getTerminator(); 152 153 // This must be an EH pad. Iterate over immediate dominators until we find a 154 // non-EH pad. We need to skip over catchswitch blocks, which are both EH pads 155 // and terminators. 156 auto IDom = DT->getNode(Inst->getParent())->getIDom(); 157 while (IDom->getBlock()->isEHPad()) { 158 assert(Entry != IDom->getBlock() && "eh pad in entry block"); 159 IDom = IDom->getIDom(); 160 } 161 162 return IDom->getBlock()->getTerminator(); 163 } 164 165 /// \brief Given \p BBs as input, find another set of BBs which collectively 166 /// dominates \p BBs and have the minimal sum of frequencies. Return the BB 167 /// set found in \p BBs. 168 static void findBestInsertionSet(DominatorTree &DT, BlockFrequencyInfo &BFI, 169 BasicBlock *Entry, 170 SmallPtrSet<BasicBlock *, 8> &BBs) { 171 assert(!BBs.count(Entry) && "Assume Entry is not in BBs"); 172 // Nodes on the current path to the root. 173 SmallPtrSet<BasicBlock *, 8> Path; 174 // Candidates includes any block 'BB' in set 'BBs' that is not strictly 175 // dominated by any other blocks in set 'BBs', and all nodes in the path 176 // in the dominator tree from Entry to 'BB'. 177 SmallPtrSet<BasicBlock *, 16> Candidates; 178 for (auto BB : BBs) { 179 Path.clear(); 180 // Walk up the dominator tree until Entry or another BB in BBs 181 // is reached. Insert the nodes on the way to the Path. 182 BasicBlock *Node = BB; 183 // The "Path" is a candidate path to be added into Candidates set. 184 bool isCandidate = false; 185 do { 186 Path.insert(Node); 187 if (Node == Entry || Candidates.count(Node)) { 188 isCandidate = true; 189 break; 190 } 191 assert(DT.getNode(Node)->getIDom() && 192 "Entry doens't dominate current Node"); 193 Node = DT.getNode(Node)->getIDom()->getBlock(); 194 } while (!BBs.count(Node)); 195 196 // If isCandidate is false, Node is another Block in BBs dominating 197 // current 'BB'. Drop the nodes on the Path. 198 if (!isCandidate) 199 continue; 200 201 // Add nodes on the Path into Candidates. 202 Candidates.insert(Path.begin(), Path.end()); 203 } 204 205 // Sort the nodes in Candidates in top-down order and save the nodes 206 // in Orders. 207 unsigned Idx = 0; 208 SmallVector<BasicBlock *, 16> Orders; 209 Orders.push_back(Entry); 210 while (Idx != Orders.size()) { 211 BasicBlock *Node = Orders[Idx++]; 212 for (auto ChildDomNode : DT.getNode(Node)->getChildren()) { 213 if (Candidates.count(ChildDomNode->getBlock())) 214 Orders.push_back(ChildDomNode->getBlock()); 215 } 216 } 217 218 // Visit Orders in bottom-up order. 219 typedef std::pair<SmallPtrSet<BasicBlock *, 16>, BlockFrequency> 220 InsertPtsCostPair; 221 // InsertPtsMap is a map from a BB to the best insertion points for the 222 // subtree of BB (subtree not including the BB itself). 223 DenseMap<BasicBlock *, InsertPtsCostPair> InsertPtsMap; 224 InsertPtsMap.reserve(Orders.size() + 1); 225 for (auto RIt = Orders.rbegin(); RIt != Orders.rend(); RIt++) { 226 BasicBlock *Node = *RIt; 227 bool NodeInBBs = BBs.count(Node); 228 SmallPtrSet<BasicBlock *, 16> &InsertPts = InsertPtsMap[Node].first; 229 BlockFrequency &InsertPtsFreq = InsertPtsMap[Node].second; 230 231 // Return the optimal insert points in BBs. 232 if (Node == Entry) { 233 BBs.clear(); 234 if (InsertPtsFreq > BFI.getBlockFreq(Node) || 235 (InsertPtsFreq == BFI.getBlockFreq(Node) && InsertPts.size() > 1)) 236 BBs.insert(Entry); 237 else 238 BBs.insert(InsertPts.begin(), InsertPts.end()); 239 break; 240 } 241 242 BasicBlock *Parent = DT.getNode(Node)->getIDom()->getBlock(); 243 // Initially, ParentInsertPts is empty and ParentPtsFreq is 0. Every child 244 // will update its parent's ParentInsertPts and ParentPtsFreq. 245 SmallPtrSet<BasicBlock *, 16> &ParentInsertPts = InsertPtsMap[Parent].first; 246 BlockFrequency &ParentPtsFreq = InsertPtsMap[Parent].second; 247 // Choose to insert in Node or in subtree of Node. 248 // Don't hoist to EHPad because we may not find a proper place to insert 249 // in EHPad. 250 // If the total frequency of InsertPts is the same as the frequency of the 251 // target Node, and InsertPts contains more than one nodes, choose hoisting 252 // to reduce code size. 253 if (NodeInBBs || 254 (!Node->isEHPad() && 255 (InsertPtsFreq > BFI.getBlockFreq(Node) || 256 (InsertPtsFreq == BFI.getBlockFreq(Node) && InsertPts.size() > 1)))) { 257 ParentInsertPts.insert(Node); 258 ParentPtsFreq += BFI.getBlockFreq(Node); 259 } else { 260 ParentInsertPts.insert(InsertPts.begin(), InsertPts.end()); 261 ParentPtsFreq += InsertPtsFreq; 262 } 263 } 264 } 265 266 /// \brief Find an insertion point that dominates all uses. 267 SmallPtrSet<Instruction *, 8> ConstantHoistingPass::findConstantInsertionPoint( 268 const ConstantInfo &ConstInfo) const { 269 assert(!ConstInfo.RebasedConstants.empty() && "Invalid constant info entry."); 270 // Collect all basic blocks. 271 SmallPtrSet<BasicBlock *, 8> BBs; 272 SmallPtrSet<Instruction *, 8> InsertPts; 273 for (auto const &RCI : ConstInfo.RebasedConstants) 274 for (auto const &U : RCI.Uses) 275 BBs.insert(findMatInsertPt(U.Inst, U.OpndIdx)->getParent()); 276 277 if (BBs.count(Entry)) { 278 InsertPts.insert(&Entry->front()); 279 return InsertPts; 280 } 281 282 if (BFI) { 283 findBestInsertionSet(*DT, *BFI, Entry, BBs); 284 for (auto BB : BBs) { 285 BasicBlock::iterator InsertPt = BB->begin(); 286 for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt) 287 ; 288 InsertPts.insert(&*InsertPt); 289 } 290 return InsertPts; 291 } 292 293 while (BBs.size() >= 2) { 294 BasicBlock *BB, *BB1, *BB2; 295 BB1 = *BBs.begin(); 296 BB2 = *std::next(BBs.begin()); 297 BB = DT->findNearestCommonDominator(BB1, BB2); 298 if (BB == Entry) { 299 InsertPts.insert(&Entry->front()); 300 return InsertPts; 301 } 302 BBs.erase(BB1); 303 BBs.erase(BB2); 304 BBs.insert(BB); 305 } 306 assert((BBs.size() == 1) && "Expected only one element."); 307 Instruction &FirstInst = (*BBs.begin())->front(); 308 InsertPts.insert(findMatInsertPt(&FirstInst)); 309 return InsertPts; 310 } 311 312 313 /// \brief Record constant integer ConstInt for instruction Inst at operand 314 /// index Idx. 315 /// 316 /// The operand at index Idx is not necessarily the constant integer itself. It 317 /// could also be a cast instruction or a constant expression that uses the 318 // constant integer. 319 void ConstantHoistingPass::collectConstantCandidates( 320 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx, 321 ConstantInt *ConstInt) { 322 unsigned Cost; 323 // Ask the target about the cost of materializing the constant for the given 324 // instruction and operand index. 325 if (auto IntrInst = dyn_cast<IntrinsicInst>(Inst)) 326 Cost = TTI->getIntImmCost(IntrInst->getIntrinsicID(), Idx, 327 ConstInt->getValue(), ConstInt->getType()); 328 else 329 Cost = TTI->getIntImmCost(Inst->getOpcode(), Idx, ConstInt->getValue(), 330 ConstInt->getType()); 331 332 // Ignore cheap integer constants. 333 if (Cost > TargetTransformInfo::TCC_Basic) { 334 ConstCandMapType::iterator Itr; 335 bool Inserted; 336 std::tie(Itr, Inserted) = ConstCandMap.insert(std::make_pair(ConstInt, 0)); 337 if (Inserted) { 338 ConstCandVec.push_back(ConstantCandidate(ConstInt)); 339 Itr->second = ConstCandVec.size() - 1; 340 } 341 ConstCandVec[Itr->second].addUser(Inst, Idx, Cost); 342 DEBUG(if (isa<ConstantInt>(Inst->getOperand(Idx))) 343 dbgs() << "Collect constant " << *ConstInt << " from " << *Inst 344 << " with cost " << Cost << '\n'; 345 else 346 dbgs() << "Collect constant " << *ConstInt << " indirectly from " 347 << *Inst << " via " << *Inst->getOperand(Idx) << " with cost " 348 << Cost << '\n'; 349 ); 350 } 351 } 352 353 354 /// \brief Check the operand for instruction Inst at index Idx. 355 void ConstantHoistingPass::collectConstantCandidates( 356 ConstCandMapType &ConstCandMap, Instruction *Inst, unsigned Idx) { 357 Value *Opnd = Inst->getOperand(Idx); 358 359 // Visit constant integers. 360 if (auto ConstInt = dyn_cast<ConstantInt>(Opnd)) { 361 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt); 362 return; 363 } 364 365 // Visit cast instructions that have constant integers. 366 if (auto CastInst = dyn_cast<Instruction>(Opnd)) { 367 // Only visit cast instructions, which have been skipped. All other 368 // instructions should have already been visited. 369 if (!CastInst->isCast()) 370 return; 371 372 if (auto *ConstInt = dyn_cast<ConstantInt>(CastInst->getOperand(0))) { 373 // Pretend the constant is directly used by the instruction and ignore 374 // the cast instruction. 375 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt); 376 return; 377 } 378 } 379 380 // Visit constant expressions that have constant integers. 381 if (auto ConstExpr = dyn_cast<ConstantExpr>(Opnd)) { 382 // Only visit constant cast expressions. 383 if (!ConstExpr->isCast()) 384 return; 385 386 if (auto ConstInt = dyn_cast<ConstantInt>(ConstExpr->getOperand(0))) { 387 // Pretend the constant is directly used by the instruction and ignore 388 // the constant expression. 389 collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt); 390 return; 391 } 392 } 393 } 394 395 396 /// \brief Scan the instruction for expensive integer constants and record them 397 /// in the constant candidate vector. 398 void ConstantHoistingPass::collectConstantCandidates( 399 ConstCandMapType &ConstCandMap, Instruction *Inst) { 400 // Skip all cast instructions. They are visited indirectly later on. 401 if (Inst->isCast()) 402 return; 403 404 // Can't handle inline asm. Skip it. 405 if (auto Call = dyn_cast<CallInst>(Inst)) 406 if (isa<InlineAsm>(Call->getCalledValue())) 407 return; 408 409 // Switch cases must remain constant, and if the value being tested is 410 // constant the entire thing should disappear. 411 if (isa<SwitchInst>(Inst)) 412 return; 413 414 // Static allocas (constant size in the entry block) are handled by 415 // prologue/epilogue insertion so they're free anyway. We definitely don't 416 // want to make them non-constant. 417 auto AI = dyn_cast<AllocaInst>(Inst); 418 if (AI && AI->isStaticAlloca()) 419 return; 420 421 // Constants in GEPs that index into a struct type should not be hoisted. 422 if (isa<GetElementPtrInst>(Inst)) { 423 gep_type_iterator GTI = gep_type_begin(Inst); 424 425 // Collect constant for first operand. 426 collectConstantCandidates(ConstCandMap, Inst, 0); 427 // Scan rest operands. 428 for (unsigned Idx = 1, E = Inst->getNumOperands(); Idx != E; ++Idx, ++GTI) { 429 // Only collect constants that index into a non struct type. 430 if (!GTI.isStruct()) { 431 collectConstantCandidates(ConstCandMap, Inst, Idx); 432 } 433 } 434 return; 435 } 436 437 // Scan all operands. 438 for (unsigned Idx = 0, E = Inst->getNumOperands(); Idx != E; ++Idx) { 439 collectConstantCandidates(ConstCandMap, Inst, Idx); 440 } // end of for all operands 441 } 442 443 /// \brief Collect all integer constants in the function that cannot be folded 444 /// into an instruction itself. 445 void ConstantHoistingPass::collectConstantCandidates(Function &Fn) { 446 ConstCandMapType ConstCandMap; 447 for (BasicBlock &BB : Fn) 448 for (Instruction &Inst : BB) 449 collectConstantCandidates(ConstCandMap, &Inst); 450 } 451 452 // This helper function is necessary to deal with values that have different 453 // bit widths (APInt Operator- does not like that). If the value cannot be 454 // represented in uint64 we return an "empty" APInt. This is then interpreted 455 // as the value is not in range. 456 static llvm::Optional<APInt> calculateOffsetDiff(const APInt &V1, 457 const APInt &V2) { 458 llvm::Optional<APInt> Res = None; 459 unsigned BW = V1.getBitWidth() > V2.getBitWidth() ? 460 V1.getBitWidth() : V2.getBitWidth(); 461 uint64_t LimVal1 = V1.getLimitedValue(); 462 uint64_t LimVal2 = V2.getLimitedValue(); 463 464 if (LimVal1 == ~0ULL || LimVal2 == ~0ULL) 465 return Res; 466 467 uint64_t Diff = LimVal1 - LimVal2; 468 return APInt(BW, Diff, true); 469 } 470 471 // From a list of constants, one needs to picked as the base and the other 472 // constants will be transformed into an offset from that base constant. The 473 // question is which we can pick best? For example, consider these constants 474 // and their number of uses: 475 // 476 // Constants| 2 | 4 | 12 | 42 | 477 // NumUses | 3 | 2 | 8 | 7 | 478 // 479 // Selecting constant 12 because it has the most uses will generate negative 480 // offsets for constants 2 and 4 (i.e. -10 and -8 respectively). If negative 481 // offsets lead to less optimal code generation, then there might be better 482 // solutions. Suppose immediates in the range of 0..35 are most optimally 483 // supported by the architecture, then selecting constant 2 is most optimal 484 // because this will generate offsets: 0, 2, 10, 40. Offsets 0, 2 and 10 are in 485 // range 0..35, and thus 3 + 2 + 8 = 13 uses are in range. Selecting 12 would 486 // have only 8 uses in range, so choosing 2 as a base is more optimal. Thus, in 487 // selecting the base constant the range of the offsets is a very important 488 // factor too that we take into account here. This algorithm calculates a total 489 // costs for selecting a constant as the base and substract the costs if 490 // immediates are out of range. It has quadratic complexity, so we call this 491 // function only when we're optimising for size and there are less than 100 492 // constants, we fall back to the straightforward algorithm otherwise 493 // which does not do all the offset calculations. 494 unsigned 495 ConstantHoistingPass::maximizeConstantsInRange(ConstCandVecType::iterator S, 496 ConstCandVecType::iterator E, 497 ConstCandVecType::iterator &MaxCostItr) { 498 unsigned NumUses = 0; 499 500 if(!Entry->getParent()->optForSize() || std::distance(S,E) > 100) { 501 for (auto ConstCand = S; ConstCand != E; ++ConstCand) { 502 NumUses += ConstCand->Uses.size(); 503 if (ConstCand->CumulativeCost > MaxCostItr->CumulativeCost) 504 MaxCostItr = ConstCand; 505 } 506 return NumUses; 507 } 508 509 DEBUG(dbgs() << "== Maximize constants in range ==\n"); 510 int MaxCost = -1; 511 for (auto ConstCand = S; ConstCand != E; ++ConstCand) { 512 auto Value = ConstCand->ConstInt->getValue(); 513 Type *Ty = ConstCand->ConstInt->getType(); 514 int Cost = 0; 515 NumUses += ConstCand->Uses.size(); 516 DEBUG(dbgs() << "= Constant: " << ConstCand->ConstInt->getValue() << "\n"); 517 518 for (auto User : ConstCand->Uses) { 519 unsigned Opcode = User.Inst->getOpcode(); 520 unsigned OpndIdx = User.OpndIdx; 521 Cost += TTI->getIntImmCost(Opcode, OpndIdx, Value, Ty); 522 DEBUG(dbgs() << "Cost: " << Cost << "\n"); 523 524 for (auto C2 = S; C2 != E; ++C2) { 525 llvm::Optional<APInt> Diff = calculateOffsetDiff( 526 C2->ConstInt->getValue(), 527 ConstCand->ConstInt->getValue()); 528 if (Diff) { 529 const int ImmCosts = 530 TTI->getIntImmCodeSizeCost(Opcode, OpndIdx, Diff.getValue(), Ty); 531 Cost -= ImmCosts; 532 DEBUG(dbgs() << "Offset " << Diff.getValue() << " " 533 << "has penalty: " << ImmCosts << "\n" 534 << "Adjusted cost: " << Cost << "\n"); 535 } 536 } 537 } 538 DEBUG(dbgs() << "Cumulative cost: " << Cost << "\n"); 539 if (Cost > MaxCost) { 540 MaxCost = Cost; 541 MaxCostItr = ConstCand; 542 DEBUG(dbgs() << "New candidate: " << MaxCostItr->ConstInt->getValue() 543 << "\n"); 544 } 545 } 546 return NumUses; 547 } 548 549 /// \brief Find the base constant within the given range and rebase all other 550 /// constants with respect to the base constant. 551 void ConstantHoistingPass::findAndMakeBaseConstant( 552 ConstCandVecType::iterator S, ConstCandVecType::iterator E) { 553 auto MaxCostItr = S; 554 unsigned NumUses = maximizeConstantsInRange(S, E, MaxCostItr); 555 556 // Don't hoist constants that have only one use. 557 if (NumUses <= 1) 558 return; 559 560 ConstantInfo ConstInfo; 561 ConstInfo.BaseConstant = MaxCostItr->ConstInt; 562 Type *Ty = ConstInfo.BaseConstant->getType(); 563 564 // Rebase the constants with respect to the base constant. 565 for (auto ConstCand = S; ConstCand != E; ++ConstCand) { 566 APInt Diff = ConstCand->ConstInt->getValue() - 567 ConstInfo.BaseConstant->getValue(); 568 Constant *Offset = Diff == 0 ? nullptr : ConstantInt::get(Ty, Diff); 569 ConstInfo.RebasedConstants.push_back( 570 RebasedConstantInfo(std::move(ConstCand->Uses), Offset)); 571 } 572 ConstantVec.push_back(std::move(ConstInfo)); 573 } 574 575 /// \brief Finds and combines constant candidates that can be easily 576 /// rematerialized with an add from a common base constant. 577 void ConstantHoistingPass::findBaseConstants() { 578 // Sort the constants by value and type. This invalidates the mapping! 579 std::sort(ConstCandVec.begin(), ConstCandVec.end(), 580 [](const ConstantCandidate &LHS, const ConstantCandidate &RHS) { 581 if (LHS.ConstInt->getType() != RHS.ConstInt->getType()) 582 return LHS.ConstInt->getType()->getBitWidth() < 583 RHS.ConstInt->getType()->getBitWidth(); 584 return LHS.ConstInt->getValue().ult(RHS.ConstInt->getValue()); 585 }); 586 587 // Simple linear scan through the sorted constant candidate vector for viable 588 // merge candidates. 589 auto MinValItr = ConstCandVec.begin(); 590 for (auto CC = std::next(ConstCandVec.begin()), E = ConstCandVec.end(); 591 CC != E; ++CC) { 592 if (MinValItr->ConstInt->getType() == CC->ConstInt->getType()) { 593 // Check if the constant is in range of an add with immediate. 594 APInt Diff = CC->ConstInt->getValue() - MinValItr->ConstInt->getValue(); 595 if ((Diff.getBitWidth() <= 64) && 596 TTI->isLegalAddImmediate(Diff.getSExtValue())) 597 continue; 598 } 599 // We either have now a different constant type or the constant is not in 600 // range of an add with immediate anymore. 601 findAndMakeBaseConstant(MinValItr, CC); 602 // Start a new base constant search. 603 MinValItr = CC; 604 } 605 // Finalize the last base constant search. 606 findAndMakeBaseConstant(MinValItr, ConstCandVec.end()); 607 } 608 609 /// \brief Updates the operand at Idx in instruction Inst with the result of 610 /// instruction Mat. If the instruction is a PHI node then special 611 /// handling for duplicate values form the same incoming basic block is 612 /// required. 613 /// \return The update will always succeed, but the return value indicated if 614 /// Mat was used for the update or not. 615 static bool updateOperand(Instruction *Inst, unsigned Idx, Instruction *Mat) { 616 if (auto PHI = dyn_cast<PHINode>(Inst)) { 617 // Check if any previous operand of the PHI node has the same incoming basic 618 // block. This is a very odd case that happens when the incoming basic block 619 // has a switch statement. In this case use the same value as the previous 620 // operand(s), otherwise we will fail verification due to different values. 621 // The values are actually the same, but the variable names are different 622 // and the verifier doesn't like that. 623 BasicBlock *IncomingBB = PHI->getIncomingBlock(Idx); 624 for (unsigned i = 0; i < Idx; ++i) { 625 if (PHI->getIncomingBlock(i) == IncomingBB) { 626 Value *IncomingVal = PHI->getIncomingValue(i); 627 Inst->setOperand(Idx, IncomingVal); 628 return false; 629 } 630 } 631 } 632 633 Inst->setOperand(Idx, Mat); 634 return true; 635 } 636 637 /// \brief Emit materialization code for all rebased constants and update their 638 /// users. 639 void ConstantHoistingPass::emitBaseConstants(Instruction *Base, 640 Constant *Offset, 641 const ConstantUser &ConstUser) { 642 Instruction *Mat = Base; 643 if (Offset) { 644 Instruction *InsertionPt = findMatInsertPt(ConstUser.Inst, 645 ConstUser.OpndIdx); 646 Mat = BinaryOperator::Create(Instruction::Add, Base, Offset, 647 "const_mat", InsertionPt); 648 649 DEBUG(dbgs() << "Materialize constant (" << *Base->getOperand(0) 650 << " + " << *Offset << ") in BB " 651 << Mat->getParent()->getName() << '\n' << *Mat << '\n'); 652 Mat->setDebugLoc(ConstUser.Inst->getDebugLoc()); 653 } 654 Value *Opnd = ConstUser.Inst->getOperand(ConstUser.OpndIdx); 655 656 // Visit constant integer. 657 if (isa<ConstantInt>(Opnd)) { 658 DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n'); 659 if (!updateOperand(ConstUser.Inst, ConstUser.OpndIdx, Mat) && Offset) 660 Mat->eraseFromParent(); 661 DEBUG(dbgs() << "To : " << *ConstUser.Inst << '\n'); 662 return; 663 } 664 665 // Visit cast instruction. 666 if (auto CastInst = dyn_cast<Instruction>(Opnd)) { 667 assert(CastInst->isCast() && "Expected an cast instruction!"); 668 // Check if we already have visited this cast instruction before to avoid 669 // unnecessary cloning. 670 Instruction *&ClonedCastInst = ClonedCastMap[CastInst]; 671 if (!ClonedCastInst) { 672 ClonedCastInst = CastInst->clone(); 673 ClonedCastInst->setOperand(0, Mat); 674 ClonedCastInst->insertAfter(CastInst); 675 // Use the same debug location as the original cast instruction. 676 ClonedCastInst->setDebugLoc(CastInst->getDebugLoc()); 677 DEBUG(dbgs() << "Clone instruction: " << *CastInst << '\n' 678 << "To : " << *ClonedCastInst << '\n'); 679 } 680 681 DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n'); 682 updateOperand(ConstUser.Inst, ConstUser.OpndIdx, ClonedCastInst); 683 DEBUG(dbgs() << "To : " << *ConstUser.Inst << '\n'); 684 return; 685 } 686 687 // Visit constant expression. 688 if (auto ConstExpr = dyn_cast<ConstantExpr>(Opnd)) { 689 Instruction *ConstExprInst = ConstExpr->getAsInstruction(); 690 ConstExprInst->setOperand(0, Mat); 691 ConstExprInst->insertBefore(findMatInsertPt(ConstUser.Inst, 692 ConstUser.OpndIdx)); 693 694 // Use the same debug location as the instruction we are about to update. 695 ConstExprInst->setDebugLoc(ConstUser.Inst->getDebugLoc()); 696 697 DEBUG(dbgs() << "Create instruction: " << *ConstExprInst << '\n' 698 << "From : " << *ConstExpr << '\n'); 699 DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n'); 700 if (!updateOperand(ConstUser.Inst, ConstUser.OpndIdx, ConstExprInst)) { 701 ConstExprInst->eraseFromParent(); 702 if (Offset) 703 Mat->eraseFromParent(); 704 } 705 DEBUG(dbgs() << "To : " << *ConstUser.Inst << '\n'); 706 return; 707 } 708 } 709 710 /// \brief Hoist and hide the base constant behind a bitcast and emit 711 /// materialization code for derived constants. 712 bool ConstantHoistingPass::emitBaseConstants() { 713 bool MadeChange = false; 714 for (auto const &ConstInfo : ConstantVec) { 715 // Hoist and hide the base constant behind a bitcast. 716 SmallPtrSet<Instruction *, 8> IPSet = findConstantInsertionPoint(ConstInfo); 717 assert(!IPSet.empty() && "IPSet is empty"); 718 719 unsigned UsesNum = 0; 720 unsigned ReBasesNum = 0; 721 for (Instruction *IP : IPSet) { 722 IntegerType *Ty = ConstInfo.BaseConstant->getType(); 723 Instruction *Base = 724 new BitCastInst(ConstInfo.BaseConstant, Ty, "const", IP); 725 DEBUG(dbgs() << "Hoist constant (" << *ConstInfo.BaseConstant 726 << ") to BB " << IP->getParent()->getName() << '\n' 727 << *Base << '\n'); 728 729 // Emit materialization code for all rebased constants. 730 unsigned Uses = 0; 731 for (auto const &RCI : ConstInfo.RebasedConstants) { 732 for (auto const &U : RCI.Uses) { 733 Uses++; 734 BasicBlock *OrigMatInsertBB = 735 findMatInsertPt(U.Inst, U.OpndIdx)->getParent(); 736 // If Base constant is to be inserted in multiple places, 737 // generate rebase for U using the Base dominating U. 738 if (IPSet.size() == 1 || 739 DT->dominates(Base->getParent(), OrigMatInsertBB)) { 740 emitBaseConstants(Base, RCI.Offset, U); 741 ReBasesNum++; 742 } 743 } 744 } 745 UsesNum = Uses; 746 747 // Use the same debug location as the last user of the constant. 748 assert(!Base->use_empty() && "The use list is empty!?"); 749 assert(isa<Instruction>(Base->user_back()) && 750 "All uses should be instructions."); 751 Base->setDebugLoc(cast<Instruction>(Base->user_back())->getDebugLoc()); 752 } 753 (void)UsesNum; 754 (void)ReBasesNum; 755 // Expect all uses are rebased after rebase is done. 756 assert(UsesNum == ReBasesNum && "Not all uses are rebased"); 757 758 NumConstantsHoisted++; 759 760 // Base constant is also included in ConstInfo.RebasedConstants, so 761 // deduct 1 from ConstInfo.RebasedConstants.size(). 762 NumConstantsRebased = ConstInfo.RebasedConstants.size() - 1; 763 764 MadeChange = true; 765 } 766 return MadeChange; 767 } 768 769 /// \brief Check all cast instructions we made a copy of and remove them if they 770 /// have no more users. 771 void ConstantHoistingPass::deleteDeadCastInst() const { 772 for (auto const &I : ClonedCastMap) 773 if (I.first->use_empty()) 774 I.first->eraseFromParent(); 775 } 776 777 /// \brief Optimize expensive integer constants in the given function. 778 bool ConstantHoistingPass::runImpl(Function &Fn, TargetTransformInfo &TTI, 779 DominatorTree &DT, BlockFrequencyInfo *BFI, 780 BasicBlock &Entry) { 781 this->TTI = &TTI; 782 this->DT = &DT; 783 this->BFI = BFI; 784 this->Entry = &Entry; 785 // Collect all constant candidates. 786 collectConstantCandidates(Fn); 787 788 // There are no constant candidates to worry about. 789 if (ConstCandVec.empty()) 790 return false; 791 792 // Combine constants that can be easily materialized with an add from a common 793 // base constant. 794 findBaseConstants(); 795 796 // There are no constants to emit. 797 if (ConstantVec.empty()) 798 return false; 799 800 // Finally hoist the base constant and emit materialization code for dependent 801 // constants. 802 bool MadeChange = emitBaseConstants(); 803 804 // Cleanup dead instructions. 805 deleteDeadCastInst(); 806 807 return MadeChange; 808 } 809 810 PreservedAnalyses ConstantHoistingPass::run(Function &F, 811 FunctionAnalysisManager &AM) { 812 auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 813 auto &TTI = AM.getResult<TargetIRAnalysis>(F); 814 auto BFI = ConstHoistWithBlockFrequency 815 ? &AM.getResult<BlockFrequencyAnalysis>(F) 816 : nullptr; 817 if (!runImpl(F, TTI, DT, BFI, F.getEntryBlock())) 818 return PreservedAnalyses::all(); 819 820 PreservedAnalyses PA; 821 PA.preserveSet<CFGAnalyses>(); 822 return PA; 823 } 824