1 //===- GVNHoist.cpp - Hoist scalar and load expressions -------------------===// 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 hoists expressions from branches to a common dominator. It uses 11 // GVN (global value numbering) to discover expressions computing the same 12 // values. The primary goal is to reduce the code size, and in some 13 // cases reduce critical path (by exposing more ILP). 14 // Hoisting may affect the performance in some cases. To mitigate that, hoisting 15 // is disabled in the following cases. 16 // 1. Scalars across calls. 17 // 2. geps when corresponding load/store cannot be hoisted. 18 //===----------------------------------------------------------------------===// 19 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/SmallPtrSet.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/Analysis/ValueTracking.h" 24 #include "llvm/Transforms/Scalar.h" 25 #include "llvm/Transforms/Scalar/GVN.h" 26 #include "llvm/Transforms/Utils/Local.h" 27 #include "llvm/Transforms/Utils/MemorySSA.h" 28 29 using namespace llvm; 30 31 #define DEBUG_TYPE "gvn-hoist" 32 33 STATISTIC(NumHoisted, "Number of instructions hoisted"); 34 STATISTIC(NumRemoved, "Number of instructions removed"); 35 STATISTIC(NumLoadsHoisted, "Number of loads hoisted"); 36 STATISTIC(NumLoadsRemoved, "Number of loads removed"); 37 STATISTIC(NumStoresHoisted, "Number of stores hoisted"); 38 STATISTIC(NumStoresRemoved, "Number of stores removed"); 39 STATISTIC(NumCallsHoisted, "Number of calls hoisted"); 40 STATISTIC(NumCallsRemoved, "Number of calls removed"); 41 42 static cl::opt<int> 43 MaxHoistedThreshold("gvn-max-hoisted", cl::Hidden, cl::init(-1), 44 cl::desc("Max number of instructions to hoist " 45 "(default unlimited = -1)")); 46 static cl::opt<int> MaxNumberOfBBSInPath( 47 "gvn-hoist-max-bbs", cl::Hidden, cl::init(4), 48 cl::desc("Max number of basic blocks on the path between " 49 "hoisting locations (default = 4, unlimited = -1)")); 50 51 static cl::opt<int> MaxDepthInBB( 52 "gvn-hoist-max-depth", cl::Hidden, cl::init(100), 53 cl::desc("Hoist instructions from the beginning of the BB up to the " 54 "maximum specified depth (default = 100, unlimited = -1)")); 55 56 namespace { 57 58 // Provides a sorting function based on the execution order of two instructions. 59 struct SortByDFSIn { 60 private: 61 DenseMap<const Value *, unsigned> &DFSNumber; 62 63 public: 64 SortByDFSIn(DenseMap<const Value *, unsigned> &D) : DFSNumber(D) {} 65 66 // Returns true when A executes before B. 67 bool operator()(const Instruction *A, const Instruction *B) const { 68 // FIXME: libc++ has a std::sort() algorithm that will call the compare 69 // function on the same element. Once PR20837 is fixed and some more years 70 // pass by and all the buildbots have moved to a corrected std::sort(), 71 // enable the following assert: 72 // 73 // assert(A != B); 74 75 const BasicBlock *BA = A->getParent(); 76 const BasicBlock *BB = B->getParent(); 77 unsigned ADFS, BDFS; 78 if (BA == BB) { 79 ADFS = DFSNumber.lookup(A); 80 BDFS = DFSNumber.lookup(B); 81 } else { 82 ADFS = DFSNumber.lookup(BA); 83 BDFS = DFSNumber.lookup(BB); 84 } 85 assert (ADFS && BDFS); 86 return ADFS < BDFS; 87 } 88 }; 89 90 // A map from a pair of VNs to all the instructions with those VNs. 91 typedef DenseMap<std::pair<unsigned, unsigned>, SmallVector<Instruction *, 4>> 92 VNtoInsns; 93 // An invalid value number Used when inserting a single value number into 94 // VNtoInsns. 95 enum : unsigned { InvalidVN = ~2U }; 96 97 // Records all scalar instructions candidate for code hoisting. 98 class InsnInfo { 99 VNtoInsns VNtoScalars; 100 101 public: 102 // Inserts I and its value number in VNtoScalars. 103 void insert(Instruction *I, GVN::ValueTable &VN) { 104 // Scalar instruction. 105 unsigned V = VN.lookupOrAdd(I); 106 VNtoScalars[{V, InvalidVN}].push_back(I); 107 } 108 109 const VNtoInsns &getVNTable() const { return VNtoScalars; } 110 }; 111 112 // Records all load instructions candidate for code hoisting. 113 class LoadInfo { 114 VNtoInsns VNtoLoads; 115 116 public: 117 // Insert Load and the value number of its memory address in VNtoLoads. 118 void insert(LoadInst *Load, GVN::ValueTable &VN) { 119 if (Load->isSimple()) { 120 unsigned V = VN.lookupOrAdd(Load->getPointerOperand()); 121 VNtoLoads[{V, InvalidVN}].push_back(Load); 122 } 123 } 124 125 const VNtoInsns &getVNTable() const { return VNtoLoads; } 126 }; 127 128 // Records all store instructions candidate for code hoisting. 129 class StoreInfo { 130 VNtoInsns VNtoStores; 131 132 public: 133 // Insert the Store and a hash number of the store address and the stored 134 // value in VNtoStores. 135 void insert(StoreInst *Store, GVN::ValueTable &VN) { 136 if (!Store->isSimple()) 137 return; 138 // Hash the store address and the stored value. 139 Value *Ptr = Store->getPointerOperand(); 140 Value *Val = Store->getValueOperand(); 141 VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store); 142 } 143 144 const VNtoInsns &getVNTable() const { return VNtoStores; } 145 }; 146 147 // Records all call instructions candidate for code hoisting. 148 class CallInfo { 149 VNtoInsns VNtoCallsScalars; 150 VNtoInsns VNtoCallsLoads; 151 VNtoInsns VNtoCallsStores; 152 153 public: 154 // Insert Call and its value numbering in one of the VNtoCalls* containers. 155 void insert(CallInst *Call, GVN::ValueTable &VN) { 156 // A call that doesNotAccessMemory is handled as a Scalar, 157 // onlyReadsMemory will be handled as a Load instruction, 158 // all other calls will be handled as stores. 159 unsigned V = VN.lookupOrAdd(Call); 160 auto Entry = std::make_pair(V, InvalidVN); 161 162 if (Call->doesNotAccessMemory()) 163 VNtoCallsScalars[Entry].push_back(Call); 164 else if (Call->onlyReadsMemory()) 165 VNtoCallsLoads[Entry].push_back(Call); 166 else 167 VNtoCallsStores[Entry].push_back(Call); 168 } 169 170 const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; } 171 172 const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; } 173 174 const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; } 175 }; 176 177 typedef DenseMap<const BasicBlock *, bool> BBSideEffectsSet; 178 typedef SmallVector<Instruction *, 4> SmallVecInsn; 179 typedef SmallVectorImpl<Instruction *> SmallVecImplInsn; 180 181 static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) { 182 static const unsigned KnownIDs[] = { 183 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, 184 LLVMContext::MD_noalias, LLVMContext::MD_range, 185 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load, 186 LLVMContext::MD_invariant_group}; 187 combineMetadata(ReplInst, I, KnownIDs); 188 } 189 190 // This pass hoists common computations across branches sharing common 191 // dominator. The primary goal is to reduce the code size, and in some 192 // cases reduce critical path (by exposing more ILP). 193 class GVNHoist { 194 public: 195 GVNHoist(DominatorTree *Dt, AliasAnalysis *Aa, MemoryDependenceResults *Md, 196 bool OptForMinSize) 197 : DT(Dt), AA(Aa), MD(Md), OptForMinSize(OptForMinSize), 198 HoistingGeps(OptForMinSize), HoistedCtr(0) {} 199 bool run(Function &F) { 200 VN.setDomTree(DT); 201 VN.setAliasAnalysis(AA); 202 VN.setMemDep(MD); 203 bool Res = false; 204 MemorySSA M(F, AA, DT); 205 MSSA = &M; 206 // Perform DFS Numbering of instructions. 207 unsigned BBI = 0; 208 for (const BasicBlock *BB : depth_first(&F.getEntryBlock())) { 209 DFSNumber[BB] = ++BBI; 210 unsigned I = 0; 211 for (auto &Inst: *BB) 212 DFSNumber[&Inst] = ++I; 213 } 214 215 // FIXME: use lazy evaluation of VN to avoid the fix-point computation. 216 while (1) { 217 auto HoistStat = hoistExpressions(F); 218 if (HoistStat.first + HoistStat.second == 0) 219 return Res; 220 221 if (HoistStat.second > 0) 222 // To address a limitation of the current GVN, we need to rerun the 223 // hoisting after we hoisted loads or stores in order to be able to 224 // hoist all scalars dependent on the hoisted ld/st. 225 VN.clear(); 226 227 Res = true; 228 } 229 230 return Res; 231 } 232 private: 233 GVN::ValueTable VN; 234 DominatorTree *DT; 235 AliasAnalysis *AA; 236 MemoryDependenceResults *MD; 237 const bool OptForMinSize; 238 const bool HoistingGeps; 239 DenseMap<const Value *, unsigned> DFSNumber; 240 BBSideEffectsSet BBSideEffects; 241 MemorySSA *MSSA; 242 int HoistedCtr; 243 244 enum InsKind { Unknown, Scalar, Load, Store }; 245 246 // Return true when there are exception handling in BB. 247 bool hasEH(const BasicBlock *BB) { 248 auto It = BBSideEffects.find(BB); 249 if (It != BBSideEffects.end()) 250 return It->second; 251 252 if (BB->isEHPad() || BB->hasAddressTaken()) { 253 BBSideEffects[BB] = true; 254 return true; 255 } 256 257 if (BB->getTerminator()->mayThrow()) { 258 BBSideEffects[BB] = true; 259 return true; 260 } 261 262 BBSideEffects[BB] = false; 263 return false; 264 } 265 266 // Return true when all paths from A to the end of the function pass through 267 // either B or C. 268 bool hoistingFromAllPaths(const BasicBlock *A, const BasicBlock *B, 269 const BasicBlock *C) { 270 // We fully copy the WL in order to be able to remove items from it. 271 SmallPtrSet<const BasicBlock *, 2> WL; 272 WL.insert(B); 273 WL.insert(C); 274 275 for (auto It = df_begin(A), E = df_end(A); It != E;) { 276 // There exists a path from A to the exit of the function if we are still 277 // iterating in DF traversal and we removed all instructions from the work 278 // list. 279 if (WL.empty()) 280 return false; 281 282 const BasicBlock *BB = *It; 283 if (WL.erase(BB)) { 284 // Stop DFS traversal when BB is in the work list. 285 It.skipChildren(); 286 continue; 287 } 288 289 // Check for end of function, calls that do not return, etc. 290 if (!isGuaranteedToTransferExecutionToSuccessor(BB->getTerminator())) 291 return false; 292 293 // Increment DFS traversal when not skipping children. 294 ++It; 295 } 296 297 return true; 298 } 299 300 /* Return true when I1 appears before I2 in the instructions of BB. */ 301 bool firstInBB(const Instruction *I1, const Instruction *I2) { 302 assert (I1->getParent() == I2->getParent()); 303 unsigned I1DFS = DFSNumber.lookup(I1); 304 unsigned I2DFS = DFSNumber.lookup(I2); 305 assert (I1DFS && I2DFS); 306 return I1DFS < I2DFS; 307 } 308 309 // Return true when there are users of Def in BB. 310 bool hasMemoryUseOnPath(MemoryAccess *Def, const BasicBlock *BB, 311 const Instruction *OldPt) { 312 const BasicBlock *DefBB = Def->getBlock(); 313 const BasicBlock *OldBB = OldPt->getParent(); 314 315 for (User *U : Def->users()) 316 if (auto *MU = dyn_cast<MemoryUse>(U)) { 317 // FIXME: MU->getBlock() does not get updated when we move the instruction. 318 BasicBlock *UBB = MU->getMemoryInst()->getParent(); 319 // Only analyze uses in BB. 320 if (BB != UBB) 321 continue; 322 323 // A use in the same block as the Def is on the path. 324 if (UBB == DefBB) { 325 assert(MSSA->locallyDominates(Def, MU) && "def not dominating use"); 326 return true; 327 } 328 329 if (UBB != OldBB) 330 return true; 331 332 // It is only harmful to hoist when the use is before OldPt. 333 if (firstInBB(MU->getMemoryInst(), OldPt)) 334 return true; 335 } 336 337 return false; 338 } 339 340 // Return true when there are exception handling or loads of memory Def 341 // between OldPt and NewPt. 342 343 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and 344 // return true when the counter NBBsOnAllPaths reaces 0, except when it is 345 // initialized to -1 which is unlimited. 346 bool hasEHOrLoadsOnPath(const Instruction *NewPt, const Instruction *OldPt, 347 MemoryAccess *Def, int &NBBsOnAllPaths) { 348 const BasicBlock *NewBB = NewPt->getParent(); 349 const BasicBlock *OldBB = OldPt->getParent(); 350 assert(DT->dominates(NewBB, OldBB) && "invalid path"); 351 assert(DT->dominates(Def->getBlock(), NewBB) && 352 "def does not dominate new hoisting point"); 353 354 // Walk all basic blocks reachable in depth-first iteration on the inverse 355 // CFG from OldBB to NewBB. These blocks are all the blocks that may be 356 // executed between the execution of NewBB and OldBB. Hoisting an expression 357 // from OldBB into NewBB has to be safe on all execution paths. 358 for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) { 359 if (*I == NewBB) { 360 // Stop traversal when reaching HoistPt. 361 I.skipChildren(); 362 continue; 363 } 364 365 // Impossible to hoist with exceptions on the path. 366 if (hasEH(*I)) 367 return true; 368 369 // Check that we do not move a store past loads. 370 if (hasMemoryUseOnPath(Def, *I, OldPt)) 371 return true; 372 373 // Stop walk once the limit is reached. 374 if (NBBsOnAllPaths == 0) 375 return true; 376 377 // -1 is unlimited number of blocks on all paths. 378 if (NBBsOnAllPaths != -1) 379 --NBBsOnAllPaths; 380 381 ++I; 382 } 383 384 return false; 385 } 386 387 // Return true when there are exception handling between HoistPt and BB. 388 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and 389 // return true when the counter NBBsOnAllPaths reaches 0, except when it is 390 // initialized to -1 which is unlimited. 391 bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *BB, 392 int &NBBsOnAllPaths) { 393 assert(DT->dominates(HoistPt, BB) && "Invalid path"); 394 395 // Walk all basic blocks reachable in depth-first iteration on 396 // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the 397 // blocks that may be executed between the execution of NewHoistPt and 398 // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe 399 // on all execution paths. 400 for (auto I = idf_begin(BB), E = idf_end(BB); I != E;) { 401 if (*I == HoistPt) { 402 // Stop traversal when reaching NewHoistPt. 403 I.skipChildren(); 404 continue; 405 } 406 407 // Impossible to hoist with exceptions on the path. 408 if (hasEH(*I)) 409 return true; 410 411 // Stop walk once the limit is reached. 412 if (NBBsOnAllPaths == 0) 413 return true; 414 415 // -1 is unlimited number of blocks on all paths. 416 if (NBBsOnAllPaths != -1) 417 --NBBsOnAllPaths; 418 419 ++I; 420 } 421 422 return false; 423 } 424 425 // Return true when it is safe to hoist a memory load or store U from OldPt 426 // to NewPt. 427 bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt, 428 MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) { 429 430 // In place hoisting is safe. 431 if (NewPt == OldPt) 432 return true; 433 434 const BasicBlock *NewBB = NewPt->getParent(); 435 const BasicBlock *OldBB = OldPt->getParent(); 436 const BasicBlock *UBB = U->getBlock(); 437 438 // Check for dependences on the Memory SSA. 439 MemoryAccess *D = U->getDefiningAccess(); 440 BasicBlock *DBB = D->getBlock(); 441 if (DT->properlyDominates(NewBB, DBB)) 442 // Cannot move the load or store to NewBB above its definition in DBB. 443 return false; 444 445 if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D)) 446 if (auto *UD = dyn_cast<MemoryUseOrDef>(D)) 447 if (firstInBB(NewPt, UD->getMemoryInst())) 448 // Cannot move the load or store to NewPt above its definition in D. 449 return false; 450 451 // Check for unsafe hoistings due to side effects. 452 if (K == InsKind::Store) { 453 if (hasEHOrLoadsOnPath(NewPt, OldPt, D, NBBsOnAllPaths)) 454 return false; 455 } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths)) 456 return false; 457 458 if (UBB == NewBB) { 459 if (DT->properlyDominates(DBB, NewBB)) 460 return true; 461 assert(UBB == DBB); 462 assert(MSSA->locallyDominates(D, U)); 463 } 464 465 // No side effects: it is safe to hoist. 466 return true; 467 } 468 469 // Return true when it is safe to hoist scalar instructions from BB1 and BB2 470 // to HoistBB. 471 bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB1, 472 const BasicBlock *BB2, int &NBBsOnAllPaths) { 473 // Check that the hoisted expression is needed on all paths. When HoistBB 474 // already contains an instruction to be hoisted, the expression is needed 475 // on all paths. Enable scalar hoisting at -Oz as it is safe to hoist 476 // scalars to a place where they are partially needed. 477 if (!OptForMinSize && BB1 != HoistBB && 478 !hoistingFromAllPaths(HoistBB, BB1, BB2)) 479 return false; 480 481 if (hasEHOnPath(HoistBB, BB1, NBBsOnAllPaths) || 482 hasEHOnPath(HoistBB, BB2, NBBsOnAllPaths)) 483 return false; 484 485 // Safe to hoist scalars from BB1 and BB2 to HoistBB. 486 return true; 487 } 488 489 // Each element of a hoisting list contains the basic block where to hoist and 490 // a list of instructions to be hoisted. 491 typedef std::pair<BasicBlock *, SmallVecInsn> HoistingPointInfo; 492 typedef SmallVector<HoistingPointInfo, 4> HoistingPointList; 493 494 // Partition InstructionsToHoist into a set of candidates which can share a 495 // common hoisting point. The partitions are collected in HPL. IsScalar is 496 // true when the instructions in InstructionsToHoist are scalars. IsLoad is 497 // true when the InstructionsToHoist are loads, false when they are stores. 498 void partitionCandidates(SmallVecImplInsn &InstructionsToHoist, 499 HoistingPointList &HPL, InsKind K) { 500 // No need to sort for two instructions. 501 if (InstructionsToHoist.size() > 2) { 502 SortByDFSIn Pred(DFSNumber); 503 std::sort(InstructionsToHoist.begin(), InstructionsToHoist.end(), Pred); 504 } 505 506 int NBBsOnAllPaths = MaxNumberOfBBSInPath; 507 508 SmallVecImplInsn::iterator II = InstructionsToHoist.begin(); 509 SmallVecImplInsn::iterator Start = II; 510 Instruction *HoistPt = *II; 511 BasicBlock *HoistBB = HoistPt->getParent(); 512 MemoryUseOrDef *UD; 513 if (K != InsKind::Scalar) 514 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(HoistPt)); 515 516 for (++II; II != InstructionsToHoist.end(); ++II) { 517 Instruction *Insn = *II; 518 BasicBlock *BB = Insn->getParent(); 519 BasicBlock *NewHoistBB; 520 Instruction *NewHoistPt; 521 522 if (BB == HoistBB) { 523 NewHoistBB = HoistBB; 524 NewHoistPt = firstInBB(Insn, HoistPt) ? Insn : HoistPt; 525 } else { 526 NewHoistBB = DT->findNearestCommonDominator(HoistBB, BB); 527 if (NewHoistBB == BB) 528 NewHoistPt = Insn; 529 else if (NewHoistBB == HoistBB) 530 NewHoistPt = HoistPt; 531 else 532 NewHoistPt = NewHoistBB->getTerminator(); 533 } 534 535 if (K == InsKind::Scalar) { 536 if (safeToHoistScalar(NewHoistBB, HoistBB, BB, NBBsOnAllPaths)) { 537 // Extend HoistPt to NewHoistPt. 538 HoistPt = NewHoistPt; 539 HoistBB = NewHoistBB; 540 continue; 541 } 542 } else { 543 // When NewBB already contains an instruction to be hoisted, the 544 // expression is needed on all paths. 545 // Check that the hoisted expression is needed on all paths: it is 546 // unsafe to hoist loads to a place where there may be a path not 547 // loading from the same address: for instance there may be a branch on 548 // which the address of the load may not be initialized. 549 if ((HoistBB == NewHoistBB || BB == NewHoistBB || 550 hoistingFromAllPaths(NewHoistBB, HoistBB, BB)) && 551 // Also check that it is safe to move the load or store from HoistPt 552 // to NewHoistPt, and from Insn to NewHoistPt. 553 safeToHoistLdSt(NewHoistPt, HoistPt, UD, K, NBBsOnAllPaths) && 554 safeToHoistLdSt(NewHoistPt, Insn, 555 cast<MemoryUseOrDef>(MSSA->getMemoryAccess(Insn)), 556 K, NBBsOnAllPaths)) { 557 // Extend HoistPt to NewHoistPt. 558 HoistPt = NewHoistPt; 559 HoistBB = NewHoistBB; 560 continue; 561 } 562 } 563 564 // At this point it is not safe to extend the current hoisting to 565 // NewHoistPt: save the hoisting list so far. 566 if (std::distance(Start, II) > 1) 567 HPL.push_back({HoistBB, SmallVecInsn(Start, II)}); 568 569 // Start over from BB. 570 Start = II; 571 if (K != InsKind::Scalar) 572 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(*Start)); 573 HoistPt = Insn; 574 HoistBB = BB; 575 NBBsOnAllPaths = MaxNumberOfBBSInPath; 576 } 577 578 // Save the last partition. 579 if (std::distance(Start, II) > 1) 580 HPL.push_back({HoistBB, SmallVecInsn(Start, II)}); 581 } 582 583 // Initialize HPL from Map. 584 void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL, 585 InsKind K) { 586 for (const auto &Entry : Map) { 587 if (MaxHoistedThreshold != -1 && ++HoistedCtr > MaxHoistedThreshold) 588 return; 589 590 const SmallVecInsn &V = Entry.second; 591 if (V.size() < 2) 592 continue; 593 594 // Compute the insertion point and the list of expressions to be hoisted. 595 SmallVecInsn InstructionsToHoist; 596 for (auto I : V) 597 if (!hasEH(I->getParent())) 598 InstructionsToHoist.push_back(I); 599 600 if (!InstructionsToHoist.empty()) 601 partitionCandidates(InstructionsToHoist, HPL, K); 602 } 603 } 604 605 // Return true when all operands of Instr are available at insertion point 606 // HoistPt. When limiting the number of hoisted expressions, one could hoist 607 // a load without hoisting its access function. So before hoisting any 608 // expression, make sure that all its operands are available at insert point. 609 bool allOperandsAvailable(const Instruction *I, 610 const BasicBlock *HoistPt) const { 611 for (const Use &Op : I->operands()) 612 if (const auto *Inst = dyn_cast<Instruction>(&Op)) 613 if (!DT->dominates(Inst->getParent(), HoistPt)) 614 return false; 615 616 return true; 617 } 618 619 // Same as allOperandsAvailable with recursive check for GEP operands. 620 bool allGepOperandsAvailable(const Instruction *I, 621 const BasicBlock *HoistPt) const { 622 for (const Use &Op : I->operands()) 623 if (const auto *Inst = dyn_cast<Instruction>(&Op)) 624 if (!DT->dominates(Inst->getParent(), HoistPt)) { 625 if (const GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Inst)) { 626 if (!allGepOperandsAvailable(GepOp, HoistPt)) 627 return false; 628 // Gep is available if all operands of GepOp are available. 629 } else { 630 // Gep is not available if it has operands other than GEPs that are 631 // defined in blocks not dominating HoistPt. 632 return false; 633 } 634 } 635 return true; 636 } 637 638 // Make all operands of the GEP available. 639 void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt, 640 const SmallVecInsn &InstructionsToHoist, 641 Instruction *Gep) const { 642 assert(allGepOperandsAvailable(Gep, HoistPt) && "GEP operands not available"); 643 644 Instruction *ClonedGep = Gep->clone(); 645 for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i) 646 if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) { 647 648 // Check whether the operand is already available. 649 if (DT->dominates(Op->getParent(), HoistPt)) 650 continue; 651 652 // As a GEP can refer to other GEPs, recursively make all the operands 653 // of this GEP available at HoistPt. 654 if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op)) 655 makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp); 656 } 657 658 // Copy Gep and replace its uses in Repl with ClonedGep. 659 ClonedGep->insertBefore(HoistPt->getTerminator()); 660 661 // Conservatively discard any optimization hints, they may differ on the 662 // other paths. 663 ClonedGep->dropUnknownNonDebugMetadata(); 664 665 // If we have optimization hints which agree with each other along different 666 // paths, preserve them. 667 for (const Instruction *OtherInst : InstructionsToHoist) { 668 const GetElementPtrInst *OtherGep; 669 if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst)) 670 OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand()); 671 else 672 OtherGep = cast<GetElementPtrInst>( 673 cast<StoreInst>(OtherInst)->getPointerOperand()); 674 ClonedGep->intersectOptionalDataWith(OtherGep); 675 } 676 677 // Replace uses of Gep with ClonedGep in Repl. 678 Repl->replaceUsesOfWith(Gep, ClonedGep); 679 } 680 681 // In the case Repl is a load or a store, we make all their GEPs 682 // available: GEPs are not hoisted by default to avoid the address 683 // computations to be hoisted without the associated load or store. 684 bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt, 685 const SmallVecInsn &InstructionsToHoist) const { 686 // Check whether the GEP of a ld/st can be synthesized at HoistPt. 687 GetElementPtrInst *Gep = nullptr; 688 Instruction *Val = nullptr; 689 if (auto *Ld = dyn_cast<LoadInst>(Repl)) { 690 Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand()); 691 } else if (auto *St = dyn_cast<StoreInst>(Repl)) { 692 Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand()); 693 Val = dyn_cast<Instruction>(St->getValueOperand()); 694 // Check that the stored value is available. 695 if (Val) { 696 if (isa<GetElementPtrInst>(Val)) { 697 // Check whether we can compute the GEP at HoistPt. 698 if (!allGepOperandsAvailable(Val, HoistPt)) 699 return false; 700 } else if (!DT->dominates(Val->getParent(), HoistPt)) 701 return false; 702 } 703 } 704 705 // Check whether we can compute the Gep at HoistPt. 706 if (!Gep || !allGepOperandsAvailable(Gep, HoistPt)) 707 return false; 708 709 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep); 710 711 if (Val && isa<GetElementPtrInst>(Val)) 712 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val); 713 714 return true; 715 } 716 717 std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) { 718 unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0; 719 for (const HoistingPointInfo &HP : HPL) { 720 // Find out whether we already have one of the instructions in HoistPt, 721 // in which case we do not have to move it. 722 BasicBlock *HoistPt = HP.first; 723 const SmallVecInsn &InstructionsToHoist = HP.second; 724 Instruction *Repl = nullptr; 725 for (Instruction *I : InstructionsToHoist) 726 if (I->getParent() == HoistPt) 727 // If there are two instructions in HoistPt to be hoisted in place: 728 // update Repl to be the first one, such that we can rename the uses 729 // of the second based on the first. 730 if (!Repl || firstInBB(I, Repl)) 731 Repl = I; 732 733 if (Repl) { 734 // Repl is already in HoistPt: it remains in place. 735 assert(allOperandsAvailable(Repl, HoistPt) && 736 "instruction depends on operands that are not available"); 737 } else { 738 // When we do not find Repl in HoistPt, select the first in the list 739 // and move it to HoistPt. 740 Repl = InstructionsToHoist.front(); 741 742 // We can move Repl in HoistPt only when all operands are available. 743 // When not HoistingGeps we need to copy the GEPs now. 744 // The order in which hoistings are done may influence the availability 745 // of operands. 746 if (!allOperandsAvailable(Repl, HoistPt) && !HoistingGeps && 747 !makeGepOperandsAvailable(Repl, HoistPt, InstructionsToHoist)) 748 continue; 749 750 // Move the instruction at the end of HoistPt. 751 Instruction *Last = HoistPt->getTerminator(); 752 Repl->moveBefore(Last); 753 754 DFSNumber[Repl] = DFSNumber[Last]++; 755 } 756 757 MemoryAccess *NewMemAcc = nullptr; 758 if (MemoryAccess *MA = MSSA->getMemoryAccess(Repl)) { 759 if (MemoryUseOrDef *OldMemAcc = dyn_cast<MemoryUseOrDef>(MA)) { 760 // The definition of this ld/st will not change: ld/st hoisting is 761 // legal when the ld/st is not moved past its current definition. 762 MemoryAccess *Def = OldMemAcc->getDefiningAccess(); 763 NewMemAcc = MSSA->createMemoryAccessInBB(Repl, Def, HoistPt, 764 MemorySSA::End); 765 OldMemAcc->replaceAllUsesWith(NewMemAcc); 766 MSSA->removeMemoryAccess(OldMemAcc); 767 } 768 } 769 770 if (isa<LoadInst>(Repl)) 771 ++NL; 772 else if (isa<StoreInst>(Repl)) 773 ++NS; 774 else if (isa<CallInst>(Repl)) 775 ++NC; 776 else // Scalar 777 ++NI; 778 779 // Remove and rename all other instructions. 780 for (Instruction *I : InstructionsToHoist) 781 if (I != Repl) { 782 ++NR; 783 if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) { 784 ReplacementLoad->setAlignment( 785 std::min(ReplacementLoad->getAlignment(), 786 cast<LoadInst>(I)->getAlignment())); 787 ++NumLoadsRemoved; 788 } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) { 789 ReplacementStore->setAlignment( 790 std::min(ReplacementStore->getAlignment(), 791 cast<StoreInst>(I)->getAlignment())); 792 ++NumStoresRemoved; 793 } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) { 794 ReplacementAlloca->setAlignment( 795 std::max(ReplacementAlloca->getAlignment(), 796 cast<AllocaInst>(I)->getAlignment())); 797 } else if (isa<CallInst>(Repl)) { 798 ++NumCallsRemoved; 799 } 800 801 if (NewMemAcc) { 802 // Update the uses of the old MSSA access with NewMemAcc. 803 MemoryAccess *OldMA = MSSA->getMemoryAccess(I); 804 OldMA->replaceAllUsesWith(NewMemAcc); 805 MSSA->removeMemoryAccess(OldMA); 806 } 807 808 Repl->intersectOptionalDataWith(I); 809 combineKnownMetadata(Repl, I); 810 I->replaceAllUsesWith(Repl); 811 I->eraseFromParent(); 812 } 813 814 // Remove MemorySSA phi nodes with the same arguments. 815 if (NewMemAcc) { 816 SmallPtrSet<MemoryPhi *, 4> UsePhis; 817 for (User *U : NewMemAcc->users()) 818 if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(U)) 819 UsePhis.insert(Phi); 820 821 for (auto *Phi : UsePhis) { 822 auto In = Phi->incoming_values(); 823 if (std::all_of(In.begin(), In.end(), 824 [&](Use &U){return U == NewMemAcc;})) { 825 Phi->replaceAllUsesWith(NewMemAcc); 826 MSSA->removeMemoryAccess(Phi); 827 } 828 } 829 } 830 } 831 832 NumHoisted += NL + NS + NC + NI; 833 NumRemoved += NR; 834 NumLoadsHoisted += NL; 835 NumStoresHoisted += NS; 836 NumCallsHoisted += NC; 837 return {NI, NL + NC + NS}; 838 } 839 840 // Hoist all expressions. Returns Number of scalars hoisted 841 // and number of non-scalars hoisted. 842 std::pair<unsigned, unsigned> hoistExpressions(Function &F) { 843 InsnInfo II; 844 LoadInfo LI; 845 StoreInfo SI; 846 CallInfo CI; 847 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) { 848 int InstructionNb = 0; 849 for (Instruction &I1 : *BB) { 850 // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting 851 // deeper may increase the register pressure and compilation time. 852 if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB) 853 break; 854 855 if (auto *Load = dyn_cast<LoadInst>(&I1)) 856 LI.insert(Load, VN); 857 else if (auto *Store = dyn_cast<StoreInst>(&I1)) 858 SI.insert(Store, VN); 859 else if (auto *Call = dyn_cast<CallInst>(&I1)) { 860 if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) { 861 if (isa<DbgInfoIntrinsic>(Intr) || 862 Intr->getIntrinsicID() == Intrinsic::assume) 863 continue; 864 } 865 if (Call->mayHaveSideEffects()) { 866 if (!OptForMinSize) 867 break; 868 // We may continue hoisting across calls which write to memory. 869 if (Call->mayThrow()) 870 break; 871 } 872 CI.insert(Call, VN); 873 } else if (HoistingGeps || !isa<GetElementPtrInst>(&I1)) 874 // Do not hoist scalars past calls that may write to memory because 875 // that could result in spills later. geps are handled separately. 876 // TODO: We can relax this for targets like AArch64 as they have more 877 // registers than X86. 878 II.insert(&I1, VN); 879 } 880 } 881 882 HoistingPointList HPL; 883 computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar); 884 computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load); 885 computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store); 886 computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar); 887 computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load); 888 computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store); 889 return hoist(HPL); 890 } 891 }; 892 893 class GVNHoistLegacyPass : public FunctionPass { 894 public: 895 static char ID; 896 897 GVNHoistLegacyPass() : FunctionPass(ID) { 898 initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry()); 899 } 900 901 bool runOnFunction(Function &F) override { 902 if (skipFunction(F)) 903 return false; 904 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 905 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); 906 auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep(); 907 908 GVNHoist G(&DT, &AA, &MD, F.optForMinSize()); 909 return G.run(F); 910 } 911 912 void getAnalysisUsage(AnalysisUsage &AU) const override { 913 AU.addRequired<DominatorTreeWrapperPass>(); 914 AU.addRequired<AAResultsWrapperPass>(); 915 AU.addRequired<MemoryDependenceWrapperPass>(); 916 AU.addPreserved<DominatorTreeWrapperPass>(); 917 } 918 }; 919 } // namespace 920 921 PreservedAnalyses GVNHoistPass::run(Function &F, 922 AnalysisManager<Function> &AM) { 923 DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F); 924 AliasAnalysis &AA = AM.getResult<AAManager>(F); 925 MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F); 926 927 GVNHoist G(&DT, &AA, &MD, F.optForMinSize()); 928 if (!G.run(F)) 929 return PreservedAnalyses::all(); 930 931 PreservedAnalyses PA; 932 PA.preserve<DominatorTreeAnalysis>(); 933 return PA; 934 } 935 936 char GVNHoistLegacyPass::ID = 0; 937 INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist", 938 "Early GVN Hoisting of Expressions", false, false) 939 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass) 940 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 941 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 942 INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist", 943 "Early GVN Hoisting of Expressions", false, false) 944 945 FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); } 946