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