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