1 //===- GVNSink.cpp - sink expressions into successors ---------------------===// 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 /// \file GVNSink.cpp 11 /// This pass attempts to sink instructions into successors, reducing static 12 /// instruction count and enabling if-conversion. 13 /// 14 /// We use a variant of global value numbering to decide what can be sunk. 15 /// Consider: 16 /// 17 /// [ %a1 = add i32 %b, 1 ] [ %c1 = add i32 %d, 1 ] 18 /// [ %a2 = xor i32 %a1, 1 ] [ %c2 = xor i32 %c1, 1 ] 19 /// \ / 20 /// [ %e = phi i32 %a2, %c2 ] 21 /// [ add i32 %e, 4 ] 22 /// 23 /// 24 /// GVN would number %a1 and %c1 differently because they compute different 25 /// results - the VN of an instruction is a function of its opcode and the 26 /// transitive closure of its operands. This is the key property for hoisting 27 /// and CSE. 28 /// 29 /// What we want when sinking however is for a numbering that is a function of 30 /// the *uses* of an instruction, which allows us to answer the question "if I 31 /// replace %a1 with %c1, will it contribute in an equivalent way to all 32 /// successive instructions?". The PostValueTable class in GVN provides this 33 /// mapping. 34 // 35 //===----------------------------------------------------------------------===// 36 37 #include "llvm/ADT/ArrayRef.h" 38 #include "llvm/ADT/DenseMap.h" 39 #include "llvm/ADT/DenseMapInfo.h" 40 #include "llvm/ADT/DenseSet.h" 41 #include "llvm/ADT/Hashing.h" 42 #include "llvm/ADT/None.h" 43 #include "llvm/ADT/Optional.h" 44 #include "llvm/ADT/PostOrderIterator.h" 45 #include "llvm/ADT/STLExtras.h" 46 #include "llvm/ADT/SmallPtrSet.h" 47 #include "llvm/ADT/SmallVector.h" 48 #include "llvm/ADT/Statistic.h" 49 #include "llvm/ADT/StringExtras.h" 50 #include "llvm/Analysis/GlobalsModRef.h" 51 #include "llvm/IR/BasicBlock.h" 52 #include "llvm/IR/CFG.h" 53 #include "llvm/IR/Constants.h" 54 #include "llvm/IR/Function.h" 55 #include "llvm/IR/InstrTypes.h" 56 #include "llvm/IR/Instruction.h" 57 #include "llvm/IR/Instructions.h" 58 #include "llvm/IR/PassManager.h" 59 #include "llvm/IR/Type.h" 60 #include "llvm/IR/Use.h" 61 #include "llvm/IR/Value.h" 62 #include "llvm/Pass.h" 63 #include "llvm/Support/Allocator.h" 64 #include "llvm/Support/ArrayRecycler.h" 65 #include "llvm/Support/AtomicOrdering.h" 66 #include "llvm/Support/Casting.h" 67 #include "llvm/Support/Compiler.h" 68 #include "llvm/Support/Debug.h" 69 #include "llvm/Support/raw_ostream.h" 70 #include "llvm/Transforms/Scalar.h" 71 #include "llvm/Transforms/Scalar/GVN.h" 72 #include "llvm/Transforms/Scalar/GVNExpression.h" 73 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 74 #include "llvm/Transforms/Utils/Local.h" 75 #include <algorithm> 76 #include <cassert> 77 #include <cstddef> 78 #include <cstdint> 79 #include <iterator> 80 #include <utility> 81 82 using namespace llvm; 83 84 #define DEBUG_TYPE "gvn-sink" 85 86 STATISTIC(NumRemoved, "Number of instructions removed"); 87 88 namespace llvm { 89 namespace GVNExpression { 90 91 LLVM_DUMP_METHOD void Expression::dump() const { 92 print(dbgs()); 93 dbgs() << "\n"; 94 } 95 96 } // end namespace GVNExpression 97 } // end namespace llvm 98 99 namespace { 100 101 static bool isMemoryInst(const Instruction *I) { 102 return isa<LoadInst>(I) || isa<StoreInst>(I) || 103 (isa<InvokeInst>(I) && !cast<InvokeInst>(I)->doesNotAccessMemory()) || 104 (isa<CallInst>(I) && !cast<CallInst>(I)->doesNotAccessMemory()); 105 } 106 107 /// Iterates through instructions in a set of blocks in reverse order from the 108 /// first non-terminator. For example (assume all blocks have size n): 109 /// LockstepReverseIterator I([B1, B2, B3]); 110 /// *I-- = [B1[n], B2[n], B3[n]]; 111 /// *I-- = [B1[n-1], B2[n-1], B3[n-1]]; 112 /// *I-- = [B1[n-2], B2[n-2], B3[n-2]]; 113 /// ... 114 /// 115 /// It continues until all blocks have been exhausted. Use \c getActiveBlocks() 116 /// to 117 /// determine which blocks are still going and the order they appear in the 118 /// list returned by operator*. 119 class LockstepReverseIterator { 120 ArrayRef<BasicBlock *> Blocks; 121 SmallPtrSet<BasicBlock *, 4> ActiveBlocks; 122 SmallVector<Instruction *, 4> Insts; 123 bool Fail; 124 125 public: 126 LockstepReverseIterator(ArrayRef<BasicBlock *> Blocks) : Blocks(Blocks) { 127 reset(); 128 } 129 130 void reset() { 131 Fail = false; 132 ActiveBlocks.clear(); 133 for (BasicBlock *BB : Blocks) 134 ActiveBlocks.insert(BB); 135 Insts.clear(); 136 for (BasicBlock *BB : Blocks) { 137 if (BB->size() <= 1) { 138 // Block wasn't big enough - only contained a terminator. 139 ActiveBlocks.erase(BB); 140 continue; 141 } 142 Insts.push_back(BB->getTerminator()->getPrevNode()); 143 } 144 if (Insts.empty()) 145 Fail = true; 146 } 147 148 bool isValid() const { return !Fail; } 149 ArrayRef<Instruction *> operator*() const { return Insts; } 150 SmallPtrSet<BasicBlock *, 4> &getActiveBlocks() { return ActiveBlocks; } 151 152 void restrictToBlocks(SmallPtrSetImpl<BasicBlock *> &Blocks) { 153 for (auto II = Insts.begin(); II != Insts.end();) { 154 if (std::find(Blocks.begin(), Blocks.end(), (*II)->getParent()) == 155 Blocks.end()) { 156 ActiveBlocks.erase((*II)->getParent()); 157 II = Insts.erase(II); 158 } else { 159 ++II; 160 } 161 } 162 } 163 164 void operator--() { 165 if (Fail) 166 return; 167 SmallVector<Instruction *, 4> NewInsts; 168 for (auto *Inst : Insts) { 169 if (Inst == &Inst->getParent()->front()) 170 ActiveBlocks.erase(Inst->getParent()); 171 else 172 NewInsts.push_back(Inst->getPrevNode()); 173 } 174 if (NewInsts.empty()) { 175 Fail = true; 176 return; 177 } 178 Insts = NewInsts; 179 } 180 }; 181 182 //===----------------------------------------------------------------------===// 183 184 /// Candidate solution for sinking. There may be different ways to 185 /// sink instructions, differing in the number of instructions sunk, 186 /// the number of predecessors sunk from and the number of PHIs 187 /// required. 188 struct SinkingInstructionCandidate { 189 unsigned NumBlocks; 190 unsigned NumInstructions; 191 unsigned NumPHIs; 192 unsigned NumMemoryInsts; 193 int Cost = -1; 194 SmallVector<BasicBlock *, 4> Blocks; 195 196 void calculateCost(unsigned NumOrigPHIs, unsigned NumOrigBlocks) { 197 unsigned NumExtraPHIs = NumPHIs - NumOrigPHIs; 198 unsigned SplitEdgeCost = (NumOrigBlocks > NumBlocks) ? 2 : 0; 199 Cost = (NumInstructions * (NumBlocks - 1)) - 200 (NumExtraPHIs * 201 NumExtraPHIs) // PHIs are expensive, so make sure they're worth it. 202 - SplitEdgeCost; 203 } 204 205 bool operator>(const SinkingInstructionCandidate &Other) const { 206 return Cost > Other.Cost; 207 } 208 }; 209 210 #ifndef NDEBUG 211 raw_ostream &operator<<(raw_ostream &OS, const SinkingInstructionCandidate &C) { 212 OS << "<Candidate Cost=" << C.Cost << " #Blocks=" << C.NumBlocks 213 << " #Insts=" << C.NumInstructions << " #PHIs=" << C.NumPHIs << ">"; 214 return OS; 215 } 216 #endif 217 218 //===----------------------------------------------------------------------===// 219 220 /// Describes a PHI node that may or may not exist. These track the PHIs 221 /// that must be created if we sunk a sequence of instructions. It provides 222 /// a hash function for efficient equality comparisons. 223 class ModelledPHI { 224 SmallVector<Value *, 4> Values; 225 SmallVector<BasicBlock *, 4> Blocks; 226 227 public: 228 ModelledPHI() = default; 229 230 ModelledPHI(const PHINode *PN) { 231 // BasicBlock comes first so we sort by basic block pointer order, then by value pointer order. 232 SmallVector<std::pair<BasicBlock *, Value *>, 4> Ops; 233 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) 234 Ops.push_back({PN->getIncomingBlock(I), PN->getIncomingValue(I)}); 235 std::sort(Ops.begin(), Ops.end()); 236 for (auto &P : Ops) { 237 Blocks.push_back(P.first); 238 Values.push_back(P.second); 239 } 240 } 241 242 /// Create a dummy ModelledPHI that will compare unequal to any other ModelledPHI 243 /// without the same ID. 244 /// \note This is specifically for DenseMapInfo - do not use this! 245 static ModelledPHI createDummy(size_t ID) { 246 ModelledPHI M; 247 M.Values.push_back(reinterpret_cast<Value*>(ID)); 248 return M; 249 } 250 251 /// Create a PHI from an array of incoming values and incoming blocks. 252 template <typename VArray, typename BArray> 253 ModelledPHI(const VArray &V, const BArray &B) { 254 std::copy(V.begin(), V.end(), std::back_inserter(Values)); 255 std::copy(B.begin(), B.end(), std::back_inserter(Blocks)); 256 } 257 258 /// Create a PHI from [I[OpNum] for I in Insts]. 259 template <typename BArray> 260 ModelledPHI(ArrayRef<Instruction *> Insts, unsigned OpNum, const BArray &B) { 261 std::copy(B.begin(), B.end(), std::back_inserter(Blocks)); 262 for (auto *I : Insts) 263 Values.push_back(I->getOperand(OpNum)); 264 } 265 266 /// Restrict the PHI's contents down to only \c NewBlocks. 267 /// \c NewBlocks must be a subset of \c this->Blocks. 268 void restrictToBlocks(const SmallPtrSetImpl<BasicBlock *> &NewBlocks) { 269 auto BI = Blocks.begin(); 270 auto VI = Values.begin(); 271 while (BI != Blocks.end()) { 272 assert(VI != Values.end()); 273 if (std::find(NewBlocks.begin(), NewBlocks.end(), *BI) == 274 NewBlocks.end()) { 275 BI = Blocks.erase(BI); 276 VI = Values.erase(VI); 277 } else { 278 ++BI; 279 ++VI; 280 } 281 } 282 assert(Blocks.size() == NewBlocks.size()); 283 } 284 285 ArrayRef<Value *> getValues() const { return Values; } 286 287 bool areAllIncomingValuesSame() const { 288 return llvm::all_of(Values, [&](Value *V) { return V == Values[0]; }); 289 } 290 291 bool areAllIncomingValuesSameType() const { 292 return llvm::all_of( 293 Values, [&](Value *V) { return V->getType() == Values[0]->getType(); }); 294 } 295 296 bool areAnyIncomingValuesConstant() const { 297 return llvm::any_of(Values, [&](Value *V) { return isa<Constant>(V); }); 298 } 299 300 // Hash functor 301 unsigned hash() const { 302 return (unsigned)hash_combine_range(Values.begin(), Values.end()); 303 } 304 305 bool operator==(const ModelledPHI &Other) const { 306 return Values == Other.Values && Blocks == Other.Blocks; 307 } 308 }; 309 310 template <typename ModelledPHI> struct DenseMapInfo { 311 static inline ModelledPHI &getEmptyKey() { 312 static ModelledPHI Dummy = ModelledPHI::createDummy(0); 313 return Dummy; 314 } 315 316 static inline ModelledPHI &getTombstoneKey() { 317 static ModelledPHI Dummy = ModelledPHI::createDummy(1); 318 return Dummy; 319 } 320 321 static unsigned getHashValue(const ModelledPHI &V) { return V.hash(); } 322 323 static bool isEqual(const ModelledPHI &LHS, const ModelledPHI &RHS) { 324 return LHS == RHS; 325 } 326 }; 327 328 using ModelledPHISet = DenseSet<ModelledPHI, DenseMapInfo<ModelledPHI>>; 329 330 //===----------------------------------------------------------------------===// 331 // ValueTable 332 //===----------------------------------------------------------------------===// 333 // This is a value number table where the value number is a function of the 334 // *uses* of a value, rather than its operands. Thus, if VN(A) == VN(B) we know 335 // that the program would be equivalent if we replaced A with PHI(A, B). 336 //===----------------------------------------------------------------------===// 337 338 /// A GVN expression describing how an instruction is used. The operands 339 /// field of BasicExpression is used to store uses, not operands. 340 /// 341 /// This class also contains fields for discriminators used when determining 342 /// equivalence of instructions with sideeffects. 343 class InstructionUseExpr : public GVNExpression::BasicExpression { 344 unsigned MemoryUseOrder = -1; 345 bool Volatile = false; 346 347 public: 348 InstructionUseExpr(Instruction *I, ArrayRecycler<Value *> &R, 349 BumpPtrAllocator &A) 350 : GVNExpression::BasicExpression(I->getNumUses()) { 351 allocateOperands(R, A); 352 setOpcode(I->getOpcode()); 353 setType(I->getType()); 354 355 for (auto &U : I->uses()) 356 op_push_back(U.getUser()); 357 std::sort(op_begin(), op_end()); 358 } 359 360 void setMemoryUseOrder(unsigned MUO) { MemoryUseOrder = MUO; } 361 void setVolatile(bool V) { Volatile = V; } 362 363 hash_code getHashValue() const override { 364 return hash_combine(GVNExpression::BasicExpression::getHashValue(), 365 MemoryUseOrder, Volatile); 366 } 367 368 template <typename Function> hash_code getHashValue(Function MapFn) { 369 hash_code H = 370 hash_combine(getOpcode(), getType(), MemoryUseOrder, Volatile); 371 for (auto *V : operands()) 372 H = hash_combine(H, MapFn(V)); 373 return H; 374 } 375 }; 376 377 class ValueTable { 378 DenseMap<Value *, uint32_t> ValueNumbering; 379 DenseMap<GVNExpression::Expression *, uint32_t> ExpressionNumbering; 380 DenseMap<size_t, uint32_t> HashNumbering; 381 BumpPtrAllocator Allocator; 382 ArrayRecycler<Value *> Recycler; 383 uint32_t nextValueNumber = 1; 384 385 /// Create an expression for I based on its opcode and its uses. If I 386 /// touches or reads memory, the expression is also based upon its memory 387 /// order - see \c getMemoryUseOrder(). 388 InstructionUseExpr *createExpr(Instruction *I) { 389 InstructionUseExpr *E = 390 new (Allocator) InstructionUseExpr(I, Recycler, Allocator); 391 if (isMemoryInst(I)) 392 E->setMemoryUseOrder(getMemoryUseOrder(I)); 393 394 if (CmpInst *C = dyn_cast<CmpInst>(I)) { 395 CmpInst::Predicate Predicate = C->getPredicate(); 396 E->setOpcode((C->getOpcode() << 8) | Predicate); 397 } 398 return E; 399 } 400 401 /// Helper to compute the value number for a memory instruction 402 /// (LoadInst/StoreInst), including checking the memory ordering and 403 /// volatility. 404 template <class Inst> InstructionUseExpr *createMemoryExpr(Inst *I) { 405 if (isStrongerThanUnordered(I->getOrdering()) || I->isAtomic()) 406 return nullptr; 407 InstructionUseExpr *E = createExpr(I); 408 E->setVolatile(I->isVolatile()); 409 return E; 410 } 411 412 public: 413 ValueTable() = default; 414 415 /// Returns the value number for the specified value, assigning 416 /// it a new number if it did not have one before. 417 uint32_t lookupOrAdd(Value *V) { 418 auto VI = ValueNumbering.find(V); 419 if (VI != ValueNumbering.end()) 420 return VI->second; 421 422 if (!isa<Instruction>(V)) { 423 ValueNumbering[V] = nextValueNumber; 424 return nextValueNumber++; 425 } 426 427 Instruction *I = cast<Instruction>(V); 428 InstructionUseExpr *exp = nullptr; 429 switch (I->getOpcode()) { 430 case Instruction::Load: 431 exp = createMemoryExpr(cast<LoadInst>(I)); 432 break; 433 case Instruction::Store: 434 exp = createMemoryExpr(cast<StoreInst>(I)); 435 break; 436 case Instruction::Call: 437 case Instruction::Invoke: 438 case Instruction::Add: 439 case Instruction::FAdd: 440 case Instruction::Sub: 441 case Instruction::FSub: 442 case Instruction::Mul: 443 case Instruction::FMul: 444 case Instruction::UDiv: 445 case Instruction::SDiv: 446 case Instruction::FDiv: 447 case Instruction::URem: 448 case Instruction::SRem: 449 case Instruction::FRem: 450 case Instruction::Shl: 451 case Instruction::LShr: 452 case Instruction::AShr: 453 case Instruction::And: 454 case Instruction::Or: 455 case Instruction::Xor: 456 case Instruction::ICmp: 457 case Instruction::FCmp: 458 case Instruction::Trunc: 459 case Instruction::ZExt: 460 case Instruction::SExt: 461 case Instruction::FPToUI: 462 case Instruction::FPToSI: 463 case Instruction::UIToFP: 464 case Instruction::SIToFP: 465 case Instruction::FPTrunc: 466 case Instruction::FPExt: 467 case Instruction::PtrToInt: 468 case Instruction::IntToPtr: 469 case Instruction::BitCast: 470 case Instruction::Select: 471 case Instruction::ExtractElement: 472 case Instruction::InsertElement: 473 case Instruction::ShuffleVector: 474 case Instruction::InsertValue: 475 case Instruction::GetElementPtr: 476 exp = createExpr(I); 477 break; 478 default: 479 break; 480 } 481 482 if (!exp) { 483 ValueNumbering[V] = nextValueNumber; 484 return nextValueNumber++; 485 } 486 487 uint32_t e = ExpressionNumbering[exp]; 488 if (!e) { 489 hash_code H = exp->getHashValue([=](Value *V) { return lookupOrAdd(V); }); 490 auto I = HashNumbering.find(H); 491 if (I != HashNumbering.end()) { 492 e = I->second; 493 } else { 494 e = nextValueNumber++; 495 HashNumbering[H] = e; 496 ExpressionNumbering[exp] = e; 497 } 498 } 499 ValueNumbering[V] = e; 500 return e; 501 } 502 503 /// Returns the value number of the specified value. Fails if the value has 504 /// not yet been numbered. 505 uint32_t lookup(Value *V) const { 506 auto VI = ValueNumbering.find(V); 507 assert(VI != ValueNumbering.end() && "Value not numbered?"); 508 return VI->second; 509 } 510 511 /// Removes all value numberings and resets the value table. 512 void clear() { 513 ValueNumbering.clear(); 514 ExpressionNumbering.clear(); 515 HashNumbering.clear(); 516 Recycler.clear(Allocator); 517 nextValueNumber = 1; 518 } 519 520 /// \c Inst uses or touches memory. Return an ID describing the memory state 521 /// at \c Inst such that if getMemoryUseOrder(I1) == getMemoryUseOrder(I2), 522 /// the exact same memory operations happen after I1 and I2. 523 /// 524 /// This is a very hard problem in general, so we use domain-specific 525 /// knowledge that we only ever check for equivalence between blocks sharing a 526 /// single immediate successor that is common, and when determining if I1 == 527 /// I2 we will have already determined that next(I1) == next(I2). This 528 /// inductive property allows us to simply return the value number of the next 529 /// instruction that defines memory. 530 uint32_t getMemoryUseOrder(Instruction *Inst) { 531 auto *BB = Inst->getParent(); 532 for (auto I = std::next(Inst->getIterator()), E = BB->end(); 533 I != E && !I->isTerminator(); ++I) { 534 if (!isMemoryInst(&*I)) 535 continue; 536 if (isa<LoadInst>(&*I)) 537 continue; 538 CallInst *CI = dyn_cast<CallInst>(&*I); 539 if (CI && CI->onlyReadsMemory()) 540 continue; 541 InvokeInst *II = dyn_cast<InvokeInst>(&*I); 542 if (II && II->onlyReadsMemory()) 543 continue; 544 return lookupOrAdd(&*I); 545 } 546 return 0; 547 } 548 }; 549 550 //===----------------------------------------------------------------------===// 551 552 class GVNSink { 553 public: 554 GVNSink() = default; 555 556 bool run(Function &F) { 557 DEBUG(dbgs() << "GVNSink: running on function @" << F.getName() << "\n"); 558 559 unsigned NumSunk = 0; 560 ReversePostOrderTraversal<Function*> RPOT(&F); 561 for (auto *N : RPOT) 562 NumSunk += sinkBB(N); 563 564 return NumSunk > 0; 565 } 566 567 private: 568 ValueTable VN; 569 570 bool isInstructionBlacklisted(Instruction *I) { 571 // These instructions may change or break semantics if moved. 572 if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) || 573 I->getType()->isTokenTy()) 574 return true; 575 return false; 576 } 577 578 /// The main heuristic function. Analyze the set of instructions pointed to by 579 /// LRI and return a candidate solution if these instructions can be sunk, or 580 /// None otherwise. 581 Optional<SinkingInstructionCandidate> analyzeInstructionForSinking( 582 LockstepReverseIterator &LRI, unsigned &InstNum, unsigned &MemoryInstNum, 583 ModelledPHISet &NeededPHIs, SmallPtrSetImpl<Value *> &PHIContents); 584 585 /// Create a ModelledPHI for each PHI in BB, adding to PHIs. 586 void analyzeInitialPHIs(BasicBlock *BB, ModelledPHISet &PHIs, 587 SmallPtrSetImpl<Value *> &PHIContents) { 588 for (auto &I : *BB) { 589 auto *PN = dyn_cast<PHINode>(&I); 590 if (!PN) 591 return; 592 593 auto MPHI = ModelledPHI(PN); 594 PHIs.insert(MPHI); 595 for (auto *V : MPHI.getValues()) 596 PHIContents.insert(V); 597 } 598 } 599 600 /// The main instruction sinking driver. Set up state and try and sink 601 /// instructions into BBEnd from its predecessors. 602 unsigned sinkBB(BasicBlock *BBEnd); 603 604 /// Perform the actual mechanics of sinking an instruction from Blocks into 605 /// BBEnd, which is their only successor. 606 void sinkLastInstruction(ArrayRef<BasicBlock *> Blocks, BasicBlock *BBEnd); 607 608 /// Remove PHIs that all have the same incoming value. 609 void foldPointlessPHINodes(BasicBlock *BB) { 610 auto I = BB->begin(); 611 while (PHINode *PN = dyn_cast<PHINode>(I++)) { 612 if (!llvm::all_of(PN->incoming_values(), [&](const Value *V) { 613 return V == PN->getIncomingValue(0); 614 })) 615 continue; 616 if (PN->getIncomingValue(0) != PN) 617 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 618 else 619 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 620 PN->eraseFromParent(); 621 } 622 } 623 }; 624 625 Optional<SinkingInstructionCandidate> GVNSink::analyzeInstructionForSinking( 626 LockstepReverseIterator &LRI, unsigned &InstNum, unsigned &MemoryInstNum, 627 ModelledPHISet &NeededPHIs, SmallPtrSetImpl<Value *> &PHIContents) { 628 auto Insts = *LRI; 629 DEBUG(dbgs() << " -- Analyzing instruction set: [\n"; for (auto *I 630 : Insts) { 631 I->dump(); 632 } dbgs() << " ]\n";); 633 634 DenseMap<uint32_t, unsigned> VNums; 635 for (auto *I : Insts) { 636 uint32_t N = VN.lookupOrAdd(I); 637 DEBUG(dbgs() << " VN=" << utohexstr(N) << " for" << *I << "\n"); 638 if (N == ~0U) 639 return None; 640 VNums[N]++; 641 } 642 unsigned VNumToSink = 643 std::max_element(VNums.begin(), VNums.end(), 644 [](const std::pair<uint32_t, unsigned> &I, 645 const std::pair<uint32_t, unsigned> &J) { 646 return I.second < J.second; 647 }) 648 ->first; 649 650 if (VNums[VNumToSink] == 1) 651 // Can't sink anything! 652 return None; 653 654 // Now restrict the number of incoming blocks down to only those with 655 // VNumToSink. 656 auto &ActivePreds = LRI.getActiveBlocks(); 657 unsigned InitialActivePredSize = ActivePreds.size(); 658 SmallVector<Instruction *, 4> NewInsts; 659 for (auto *I : Insts) { 660 if (VN.lookup(I) != VNumToSink) 661 ActivePreds.erase(I->getParent()); 662 else 663 NewInsts.push_back(I); 664 } 665 for (auto *I : NewInsts) 666 if (isInstructionBlacklisted(I)) 667 return None; 668 669 // If we've restricted the incoming blocks, restrict all needed PHIs also 670 // to that set. 671 bool RecomputePHIContents = false; 672 if (ActivePreds.size() != InitialActivePredSize) { 673 ModelledPHISet NewNeededPHIs; 674 for (auto P : NeededPHIs) { 675 P.restrictToBlocks(ActivePreds); 676 NewNeededPHIs.insert(P); 677 } 678 NeededPHIs = NewNeededPHIs; 679 LRI.restrictToBlocks(ActivePreds); 680 RecomputePHIContents = true; 681 } 682 683 // The sunk instruction's results. 684 ModelledPHI NewPHI(NewInsts, ActivePreds); 685 686 // Does sinking this instruction render previous PHIs redundant? 687 if (NeededPHIs.find(NewPHI) != NeededPHIs.end()) { 688 NeededPHIs.erase(NewPHI); 689 RecomputePHIContents = true; 690 } 691 692 if (RecomputePHIContents) { 693 // The needed PHIs have changed, so recompute the set of all needed 694 // values. 695 PHIContents.clear(); 696 for (auto &PHI : NeededPHIs) 697 PHIContents.insert(PHI.getValues().begin(), PHI.getValues().end()); 698 } 699 700 // Is this instruction required by a later PHI that doesn't match this PHI? 701 // if so, we can't sink this instruction. 702 for (auto *V : NewPHI.getValues()) 703 if (PHIContents.count(V)) 704 // V exists in this PHI, but the whole PHI is different to NewPHI 705 // (else it would have been removed earlier). We cannot continue 706 // because this isn't representable. 707 return None; 708 709 // Which operands need PHIs? 710 // FIXME: If any of these fail, we should partition up the candidates to 711 // try and continue making progress. 712 Instruction *I0 = NewInsts[0]; 713 for (unsigned OpNum = 0, E = I0->getNumOperands(); OpNum != E; ++OpNum) { 714 ModelledPHI PHI(NewInsts, OpNum, ActivePreds); 715 if (PHI.areAllIncomingValuesSame()) 716 continue; 717 if (!canReplaceOperandWithVariable(I0, OpNum)) 718 // We can 't create a PHI from this instruction! 719 return None; 720 if (NeededPHIs.count(PHI)) 721 continue; 722 if (!PHI.areAllIncomingValuesSameType()) 723 return None; 724 // Don't create indirect calls! The called value is the final operand. 725 if ((isa<CallInst>(I0) || isa<InvokeInst>(I0)) && OpNum == E - 1 && 726 PHI.areAnyIncomingValuesConstant()) 727 return None; 728 729 NeededPHIs.reserve(NeededPHIs.size()); 730 NeededPHIs.insert(PHI); 731 PHIContents.insert(PHI.getValues().begin(), PHI.getValues().end()); 732 } 733 734 if (isMemoryInst(NewInsts[0])) 735 ++MemoryInstNum; 736 737 SinkingInstructionCandidate Cand; 738 Cand.NumInstructions = ++InstNum; 739 Cand.NumMemoryInsts = MemoryInstNum; 740 Cand.NumBlocks = ActivePreds.size(); 741 Cand.NumPHIs = NeededPHIs.size(); 742 for (auto *C : ActivePreds) 743 Cand.Blocks.push_back(C); 744 745 return Cand; 746 } 747 748 unsigned GVNSink::sinkBB(BasicBlock *BBEnd) { 749 DEBUG(dbgs() << "GVNSink: running on basic block "; 750 BBEnd->printAsOperand(dbgs()); dbgs() << "\n"); 751 SmallVector<BasicBlock *, 4> Preds; 752 for (auto *B : predecessors(BBEnd)) { 753 auto *T = B->getTerminator(); 754 if (isa<BranchInst>(T) || isa<SwitchInst>(T)) 755 Preds.push_back(B); 756 else 757 return 0; 758 } 759 if (Preds.size() < 2) 760 return 0; 761 std::sort(Preds.begin(), Preds.end()); 762 763 unsigned NumOrigPreds = Preds.size(); 764 // We can only sink instructions through unconditional branches. 765 for (auto I = Preds.begin(); I != Preds.end();) { 766 if ((*I)->getTerminator()->getNumSuccessors() != 1) 767 I = Preds.erase(I); 768 else 769 ++I; 770 } 771 772 LockstepReverseIterator LRI(Preds); 773 SmallVector<SinkingInstructionCandidate, 4> Candidates; 774 unsigned InstNum = 0, MemoryInstNum = 0; 775 ModelledPHISet NeededPHIs; 776 SmallPtrSet<Value *, 4> PHIContents; 777 analyzeInitialPHIs(BBEnd, NeededPHIs, PHIContents); 778 unsigned NumOrigPHIs = NeededPHIs.size(); 779 780 while (LRI.isValid()) { 781 auto Cand = analyzeInstructionForSinking(LRI, InstNum, MemoryInstNum, 782 NeededPHIs, PHIContents); 783 if (!Cand) 784 break; 785 Cand->calculateCost(NumOrigPHIs, Preds.size()); 786 Candidates.emplace_back(*Cand); 787 --LRI; 788 } 789 790 std::stable_sort( 791 Candidates.begin(), Candidates.end(), 792 [](const SinkingInstructionCandidate &A, 793 const SinkingInstructionCandidate &B) { return A > B; }); 794 DEBUG(dbgs() << " -- Sinking candidates:\n"; for (auto &C 795 : Candidates) dbgs() 796 << " " << C << "\n";); 797 798 // Pick the top candidate, as long it is positive! 799 if (Candidates.empty() || Candidates.front().Cost <= 0) 800 return 0; 801 auto C = Candidates.front(); 802 803 DEBUG(dbgs() << " -- Sinking: " << C << "\n"); 804 BasicBlock *InsertBB = BBEnd; 805 if (C.Blocks.size() < NumOrigPreds) { 806 DEBUG(dbgs() << " -- Splitting edge to "; BBEnd->printAsOperand(dbgs()); 807 dbgs() << "\n"); 808 InsertBB = SplitBlockPredecessors(BBEnd, C.Blocks, ".gvnsink.split"); 809 if (!InsertBB) { 810 DEBUG(dbgs() << " -- FAILED to split edge!\n"); 811 // Edge couldn't be split. 812 return 0; 813 } 814 } 815 816 for (unsigned I = 0; I < C.NumInstructions; ++I) 817 sinkLastInstruction(C.Blocks, InsertBB); 818 819 return C.NumInstructions; 820 } 821 822 void GVNSink::sinkLastInstruction(ArrayRef<BasicBlock *> Blocks, 823 BasicBlock *BBEnd) { 824 SmallVector<Instruction *, 4> Insts; 825 for (BasicBlock *BB : Blocks) 826 Insts.push_back(BB->getTerminator()->getPrevNode()); 827 Instruction *I0 = Insts.front(); 828 829 SmallVector<Value *, 4> NewOperands; 830 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) { 831 bool NeedPHI = llvm::any_of(Insts, [&I0, O](const Instruction *I) { 832 return I->getOperand(O) != I0->getOperand(O); 833 }); 834 if (!NeedPHI) { 835 NewOperands.push_back(I0->getOperand(O)); 836 continue; 837 } 838 839 // Create a new PHI in the successor block and populate it. 840 auto *Op = I0->getOperand(O); 841 assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!"); 842 auto *PN = PHINode::Create(Op->getType(), Insts.size(), 843 Op->getName() + ".sink", &BBEnd->front()); 844 for (auto *I : Insts) 845 PN->addIncoming(I->getOperand(O), I->getParent()); 846 NewOperands.push_back(PN); 847 } 848 849 // Arbitrarily use I0 as the new "common" instruction; remap its operands 850 // and move it to the start of the successor block. 851 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) 852 I0->getOperandUse(O).set(NewOperands[O]); 853 I0->moveBefore(&*BBEnd->getFirstInsertionPt()); 854 855 // Update metadata and IR flags. 856 for (auto *I : Insts) 857 if (I != I0) { 858 combineMetadataForCSE(I0, I); 859 I0->andIRFlags(I); 860 } 861 862 for (auto *I : Insts) 863 if (I != I0) 864 I->replaceAllUsesWith(I0); 865 foldPointlessPHINodes(BBEnd); 866 867 // Finally nuke all instructions apart from the common instruction. 868 for (auto *I : Insts) 869 if (I != I0) 870 I->eraseFromParent(); 871 872 NumRemoved += Insts.size() - 1; 873 } 874 875 //////////////////////////////////////////////////////////////////////////////// 876 // Pass machinery / boilerplate 877 878 class GVNSinkLegacyPass : public FunctionPass { 879 public: 880 static char ID; 881 882 GVNSinkLegacyPass() : FunctionPass(ID) { 883 initializeGVNSinkLegacyPassPass(*PassRegistry::getPassRegistry()); 884 } 885 886 bool runOnFunction(Function &F) override { 887 if (skipFunction(F)) 888 return false; 889 GVNSink G; 890 return G.run(F); 891 } 892 893 void getAnalysisUsage(AnalysisUsage &AU) const override { 894 AU.addPreserved<GlobalsAAWrapperPass>(); 895 } 896 }; 897 898 } // end anonymous namespace 899 900 PreservedAnalyses GVNSinkPass::run(Function &F, FunctionAnalysisManager &AM) { 901 GVNSink G; 902 if (!G.run(F)) 903 return PreservedAnalyses::all(); 904 905 PreservedAnalyses PA; 906 PA.preserve<GlobalsAA>(); 907 return PA; 908 } 909 910 char GVNSinkLegacyPass::ID = 0; 911 912 INITIALIZE_PASS_BEGIN(GVNSinkLegacyPass, "gvn-sink", 913 "Early GVN sinking of Expressions", false, false) 914 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 915 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) 916 INITIALIZE_PASS_END(GVNSinkLegacyPass, "gvn-sink", 917 "Early GVN sinking of Expressions", false, false) 918 919 FunctionPass *llvm::createGVNSinkPass() { return new GVNSinkLegacyPass(); } 920