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