1 //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs global value numbering to eliminate fully redundant
11 // instructions. It also performs simple dead load elimination.
13 // Note that this pass does the value numbering itself; it does not use the
14 // ValueNumbering analysis passes.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Scalar/GVN.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DepthFirstIterator.h"
21 #include "llvm/ADT/Hashing.h"
22 #include "llvm/ADT/MapVector.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/AssumptionCache.h"
29 #include "llvm/Analysis/CFG.h"
30 #include "llvm/Analysis/ConstantFolding.h"
31 #include "llvm/Analysis/GlobalsModRef.h"
32 #include "llvm/Analysis/InstructionSimplify.h"
33 #include "llvm/Analysis/Loads.h"
34 #include "llvm/Analysis/MemoryBuiltins.h"
35 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
36 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
37 #include "llvm/Analysis/PHITransAddr.h"
38 #include "llvm/Analysis/TargetLibraryInfo.h"
39 #include "llvm/Analysis/ValueTracking.h"
40 #include "llvm/IR/DataLayout.h"
41 #include "llvm/IR/Dominators.h"
42 #include "llvm/IR/GlobalVariable.h"
43 #include "llvm/IR/IRBuilder.h"
44 #include "llvm/IR/IntrinsicInst.h"
45 #include "llvm/IR/LLVMContext.h"
46 #include "llvm/IR/Metadata.h"
47 #include "llvm/IR/PatternMatch.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/Debug.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
52 #include "llvm/Transforms/Utils/Local.h"
53 #include "llvm/Transforms/Utils/SSAUpdater.h"
56 using namespace llvm::gvn;
57 using namespace PatternMatch;
59 #define DEBUG_TYPE "gvn"
61 STATISTIC(NumGVNInstr, "Number of instructions deleted");
62 STATISTIC(NumGVNLoad, "Number of loads deleted");
63 STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
64 STATISTIC(NumGVNBlocks, "Number of blocks merged");
65 STATISTIC(NumGVNSimpl, "Number of instructions simplified");
66 STATISTIC(NumGVNEqProp, "Number of equalities propagated");
67 STATISTIC(NumPRELoad, "Number of loads PRE'd");
69 static cl::opt<bool> EnablePRE("enable-pre",
70 cl::init(true), cl::Hidden);
71 static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
73 // Maximum allowed recursion depth.
74 static cl::opt<uint32_t>
75 MaxRecurseDepth("max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore,
76 cl::desc("Max recurse depth (default = 1000)"));
78 struct llvm::GVN::Expression {
81 SmallVector<uint32_t, 4> varargs;
83 Expression(uint32_t o = ~2U) : opcode(o) {}
85 bool operator==(const Expression &other) const {
86 if (opcode != other.opcode)
88 if (opcode == ~0U || opcode == ~1U)
90 if (type != other.type)
92 if (varargs != other.varargs)
97 friend hash_code hash_value(const Expression &Value) {
99 Value.opcode, Value.type,
100 hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
105 template <> struct DenseMapInfo<GVN::Expression> {
106 static inline GVN::Expression getEmptyKey() { return ~0U; }
108 static inline GVN::Expression getTombstoneKey() { return ~1U; }
110 static unsigned getHashValue(const GVN::Expression &e) {
111 using llvm::hash_value;
112 return static_cast<unsigned>(hash_value(e));
114 static bool isEqual(const GVN::Expression &LHS, const GVN::Expression &RHS) {
118 } // End llvm namespace.
120 /// Represents a particular available value that we know how to materialize.
121 /// Materialization of an AvailableValue never fails. An AvailableValue is
122 /// implicitly associated with a rematerialization point which is the
123 /// location of the instruction from which it was formed.
124 struct llvm::gvn::AvailableValue {
126 SimpleVal, // A simple offsetted value that is accessed.
127 LoadVal, // A value produced by a load.
128 MemIntrin, // A memory intrinsic which is loaded from.
129 UndefVal // A UndefValue representing a value from dead block (which
130 // is not yet physically removed from the CFG).
133 /// V - The value that is live out of the block.
134 PointerIntPair<Value *, 2, ValType> Val;
136 /// Offset - The byte offset in Val that is interesting for the load query.
139 static AvailableValue get(Value *V, unsigned Offset = 0) {
141 Res.Val.setPointer(V);
142 Res.Val.setInt(SimpleVal);
147 static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
149 Res.Val.setPointer(MI);
150 Res.Val.setInt(MemIntrin);
155 static AvailableValue getLoad(LoadInst *LI, unsigned Offset = 0) {
157 Res.Val.setPointer(LI);
158 Res.Val.setInt(LoadVal);
163 static AvailableValue getUndef() {
165 Res.Val.setPointer(nullptr);
166 Res.Val.setInt(UndefVal);
171 bool isSimpleValue() const { return Val.getInt() == SimpleVal; }
172 bool isCoercedLoadValue() const { return Val.getInt() == LoadVal; }
173 bool isMemIntrinValue() const { return Val.getInt() == MemIntrin; }
174 bool isUndefValue() const { return Val.getInt() == UndefVal; }
176 Value *getSimpleValue() const {
177 assert(isSimpleValue() && "Wrong accessor");
178 return Val.getPointer();
181 LoadInst *getCoercedLoadValue() const {
182 assert(isCoercedLoadValue() && "Wrong accessor");
183 return cast<LoadInst>(Val.getPointer());
186 MemIntrinsic *getMemIntrinValue() const {
187 assert(isMemIntrinValue() && "Wrong accessor");
188 return cast<MemIntrinsic>(Val.getPointer());
191 /// Emit code at the specified insertion point to adjust the value defined
192 /// here to the specified type. This handles various coercion cases.
193 Value *MaterializeAdjustedValue(LoadInst *LI, Instruction *InsertPt,
197 /// Represents an AvailableValue which can be rematerialized at the end of
198 /// the associated BasicBlock.
199 struct llvm::gvn::AvailableValueInBlock {
200 /// BB - The basic block in question.
203 /// AV - The actual available value
206 static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) {
207 AvailableValueInBlock Res;
209 Res.AV = std::move(AV);
213 static AvailableValueInBlock get(BasicBlock *BB, Value *V,
214 unsigned Offset = 0) {
215 return get(BB, AvailableValue::get(V, Offset));
217 static AvailableValueInBlock getUndef(BasicBlock *BB) {
218 return get(BB, AvailableValue::getUndef());
221 /// Emit code at the end of this block to adjust the value defined here to
222 /// the specified type. This handles various coercion cases.
223 Value *MaterializeAdjustedValue(LoadInst *LI, GVN &gvn) const {
224 return AV.MaterializeAdjustedValue(LI, BB->getTerminator(), gvn);
228 //===----------------------------------------------------------------------===//
229 // ValueTable Internal Functions
230 //===----------------------------------------------------------------------===//
232 GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
234 e.type = I->getType();
235 e.opcode = I->getOpcode();
236 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
238 e.varargs.push_back(lookupOrAdd(*OI));
239 if (I->isCommutative()) {
240 // Ensure that commutative instructions that only differ by a permutation
241 // of their operands get the same value number by sorting the operand value
242 // numbers. Since all commutative instructions have two operands it is more
243 // efficient to sort by hand rather than using, say, std::sort.
244 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
245 if (e.varargs[0] > e.varargs[1])
246 std::swap(e.varargs[0], e.varargs[1]);
249 if (CmpInst *C = dyn_cast<CmpInst>(I)) {
250 // Sort the operand value numbers so x<y and y>x get the same value number.
251 CmpInst::Predicate Predicate = C->getPredicate();
252 if (e.varargs[0] > e.varargs[1]) {
253 std::swap(e.varargs[0], e.varargs[1]);
254 Predicate = CmpInst::getSwappedPredicate(Predicate);
256 e.opcode = (C->getOpcode() << 8) | Predicate;
257 } else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) {
258 for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
260 e.varargs.push_back(*II);
266 GVN::Expression GVN::ValueTable::createCmpExpr(unsigned Opcode,
267 CmpInst::Predicate Predicate,
268 Value *LHS, Value *RHS) {
269 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
270 "Not a comparison!");
272 e.type = CmpInst::makeCmpResultType(LHS->getType());
273 e.varargs.push_back(lookupOrAdd(LHS));
274 e.varargs.push_back(lookupOrAdd(RHS));
276 // Sort the operand value numbers so x<y and y>x get the same value number.
277 if (e.varargs[0] > e.varargs[1]) {
278 std::swap(e.varargs[0], e.varargs[1]);
279 Predicate = CmpInst::getSwappedPredicate(Predicate);
281 e.opcode = (Opcode << 8) | Predicate;
285 GVN::Expression GVN::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
286 assert(EI && "Not an ExtractValueInst?");
288 e.type = EI->getType();
291 IntrinsicInst *I = dyn_cast<IntrinsicInst>(EI->getAggregateOperand());
292 if (I != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0 ) {
293 // EI might be an extract from one of our recognised intrinsics. If it
294 // is we'll synthesize a semantically equivalent expression instead on
295 // an extract value expression.
296 switch (I->getIntrinsicID()) {
297 case Intrinsic::sadd_with_overflow:
298 case Intrinsic::uadd_with_overflow:
299 e.opcode = Instruction::Add;
301 case Intrinsic::ssub_with_overflow:
302 case Intrinsic::usub_with_overflow:
303 e.opcode = Instruction::Sub;
305 case Intrinsic::smul_with_overflow:
306 case Intrinsic::umul_with_overflow:
307 e.opcode = Instruction::Mul;
314 // Intrinsic recognized. Grab its args to finish building the expression.
315 assert(I->getNumArgOperands() == 2 &&
316 "Expect two args for recognised intrinsics.");
317 e.varargs.push_back(lookupOrAdd(I->getArgOperand(0)));
318 e.varargs.push_back(lookupOrAdd(I->getArgOperand(1)));
323 // Not a recognised intrinsic. Fall back to producing an extract value
325 e.opcode = EI->getOpcode();
326 for (Instruction::op_iterator OI = EI->op_begin(), OE = EI->op_end();
328 e.varargs.push_back(lookupOrAdd(*OI));
330 for (ExtractValueInst::idx_iterator II = EI->idx_begin(), IE = EI->idx_end();
332 e.varargs.push_back(*II);
337 //===----------------------------------------------------------------------===//
338 // ValueTable External Functions
339 //===----------------------------------------------------------------------===//
341 GVN::ValueTable::ValueTable() : nextValueNumber(1) {}
342 GVN::ValueTable::ValueTable(const ValueTable &) = default;
343 GVN::ValueTable::ValueTable(ValueTable &&) = default;
344 GVN::ValueTable::~ValueTable() = default;
346 /// add - Insert a value into the table with a specified value number.
347 void GVN::ValueTable::add(Value *V, uint32_t num) {
348 valueNumbering.insert(std::make_pair(V, num));
351 uint32_t GVN::ValueTable::lookupOrAddCall(CallInst *C) {
352 if (AA->doesNotAccessMemory(C)) {
353 Expression exp = createExpr(C);
354 uint32_t &e = expressionNumbering[exp];
355 if (!e) e = nextValueNumber++;
356 valueNumbering[C] = e;
358 } else if (AA->onlyReadsMemory(C)) {
359 Expression exp = createExpr(C);
360 uint32_t &e = expressionNumbering[exp];
362 e = nextValueNumber++;
363 valueNumbering[C] = e;
367 e = nextValueNumber++;
368 valueNumbering[C] = e;
372 MemDepResult local_dep = MD->getDependency(C);
374 if (!local_dep.isDef() && !local_dep.isNonLocal()) {
375 valueNumbering[C] = nextValueNumber;
376 return nextValueNumber++;
379 if (local_dep.isDef()) {
380 CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
382 if (local_cdep->getNumArgOperands() != C->getNumArgOperands()) {
383 valueNumbering[C] = nextValueNumber;
384 return nextValueNumber++;
387 for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
388 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
389 uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
391 valueNumbering[C] = nextValueNumber;
392 return nextValueNumber++;
396 uint32_t v = lookupOrAdd(local_cdep);
397 valueNumbering[C] = v;
402 const MemoryDependenceResults::NonLocalDepInfo &deps =
403 MD->getNonLocalCallDependency(CallSite(C));
404 // FIXME: Move the checking logic to MemDep!
405 CallInst* cdep = nullptr;
407 // Check to see if we have a single dominating call instruction that is
409 for (unsigned i = 0, e = deps.size(); i != e; ++i) {
410 const NonLocalDepEntry *I = &deps[i];
411 if (I->getResult().isNonLocal())
414 // We don't handle non-definitions. If we already have a call, reject
415 // instruction dependencies.
416 if (!I->getResult().isDef() || cdep != nullptr) {
421 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst());
422 // FIXME: All duplicated with non-local case.
423 if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){
424 cdep = NonLocalDepCall;
433 valueNumbering[C] = nextValueNumber;
434 return nextValueNumber++;
437 if (cdep->getNumArgOperands() != C->getNumArgOperands()) {
438 valueNumbering[C] = nextValueNumber;
439 return nextValueNumber++;
441 for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
442 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
443 uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
445 valueNumbering[C] = nextValueNumber;
446 return nextValueNumber++;
450 uint32_t v = lookupOrAdd(cdep);
451 valueNumbering[C] = v;
455 valueNumbering[C] = nextValueNumber;
456 return nextValueNumber++;
460 /// Returns true if a value number exists for the specified value.
461 bool GVN::ValueTable::exists(Value *V) const { return valueNumbering.count(V) != 0; }
463 /// lookup_or_add - Returns the value number for the specified value, assigning
464 /// it a new number if it did not have one before.
465 uint32_t GVN::ValueTable::lookupOrAdd(Value *V) {
466 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
467 if (VI != valueNumbering.end())
470 if (!isa<Instruction>(V)) {
471 valueNumbering[V] = nextValueNumber;
472 return nextValueNumber++;
475 Instruction* I = cast<Instruction>(V);
477 switch (I->getOpcode()) {
478 case Instruction::Call:
479 return lookupOrAddCall(cast<CallInst>(I));
480 case Instruction::Add:
481 case Instruction::FAdd:
482 case Instruction::Sub:
483 case Instruction::FSub:
484 case Instruction::Mul:
485 case Instruction::FMul:
486 case Instruction::UDiv:
487 case Instruction::SDiv:
488 case Instruction::FDiv:
489 case Instruction::URem:
490 case Instruction::SRem:
491 case Instruction::FRem:
492 case Instruction::Shl:
493 case Instruction::LShr:
494 case Instruction::AShr:
495 case Instruction::And:
496 case Instruction::Or:
497 case Instruction::Xor:
498 case Instruction::ICmp:
499 case Instruction::FCmp:
500 case Instruction::Trunc:
501 case Instruction::ZExt:
502 case Instruction::SExt:
503 case Instruction::FPToUI:
504 case Instruction::FPToSI:
505 case Instruction::UIToFP:
506 case Instruction::SIToFP:
507 case Instruction::FPTrunc:
508 case Instruction::FPExt:
509 case Instruction::PtrToInt:
510 case Instruction::IntToPtr:
511 case Instruction::BitCast:
512 case Instruction::Select:
513 case Instruction::ExtractElement:
514 case Instruction::InsertElement:
515 case Instruction::ShuffleVector:
516 case Instruction::InsertValue:
517 case Instruction::GetElementPtr:
520 case Instruction::ExtractValue:
521 exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
524 valueNumbering[V] = nextValueNumber;
525 return nextValueNumber++;
528 uint32_t& e = expressionNumbering[exp];
529 if (!e) e = nextValueNumber++;
530 valueNumbering[V] = e;
534 /// Returns the value number of the specified value. Fails if
535 /// the value has not yet been numbered.
536 uint32_t GVN::ValueTable::lookup(Value *V) const {
537 DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
538 assert(VI != valueNumbering.end() && "Value not numbered?");
542 /// Returns the value number of the given comparison,
543 /// assigning it a new number if it did not have one before. Useful when
544 /// we deduced the result of a comparison, but don't immediately have an
545 /// instruction realizing that comparison to hand.
546 uint32_t GVN::ValueTable::lookupOrAddCmp(unsigned Opcode,
547 CmpInst::Predicate Predicate,
548 Value *LHS, Value *RHS) {
549 Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
550 uint32_t& e = expressionNumbering[exp];
551 if (!e) e = nextValueNumber++;
555 /// Remove all entries from the ValueTable.
556 void GVN::ValueTable::clear() {
557 valueNumbering.clear();
558 expressionNumbering.clear();
562 /// Remove a value from the value numbering.
563 void GVN::ValueTable::erase(Value *V) {
564 valueNumbering.erase(V);
567 /// verifyRemoved - Verify that the value is removed from all internal data
569 void GVN::ValueTable::verifyRemoved(const Value *V) const {
570 for (DenseMap<Value*, uint32_t>::const_iterator
571 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
572 assert(I->first != V && "Inst still occurs in value numbering map!");
576 //===----------------------------------------------------------------------===//
578 //===----------------------------------------------------------------------===//
580 PreservedAnalyses GVN::run(Function &F, FunctionAnalysisManager &AM) {
581 // FIXME: The order of evaluation of these 'getResult' calls is very
582 // significant! Re-ordering these variables will cause GVN when run alone to
583 // be less effective! We should fix memdep and basic-aa to not exhibit this
584 // behavior, but until then don't change the order here.
585 auto &AC = AM.getResult<AssumptionAnalysis>(F);
586 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
587 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
588 auto &AA = AM.getResult<AAManager>(F);
589 auto &MemDep = AM.getResult<MemoryDependenceAnalysis>(F);
590 auto *LI = AM.getCachedResult<LoopAnalysis>(F);
591 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
592 bool Changed = runImpl(F, AC, DT, TLI, AA, &MemDep, LI, &ORE);
594 return PreservedAnalyses::all();
595 PreservedAnalyses PA;
596 PA.preserve<DominatorTreeAnalysis>();
597 PA.preserve<GlobalsAA>();
602 void GVN::dump(DenseMap<uint32_t, Value*>& d) {
604 for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
605 E = d.end(); I != E; ++I) {
606 errs() << I->first << "\n";
612 /// Return true if we can prove that the value
613 /// we're analyzing is fully available in the specified block. As we go, keep
614 /// track of which blocks we know are fully alive in FullyAvailableBlocks. This
615 /// map is actually a tri-state map with the following values:
616 /// 0) we know the block *is not* fully available.
617 /// 1) we know the block *is* fully available.
618 /// 2) we do not know whether the block is fully available or not, but we are
619 /// currently speculating that it will be.
620 /// 3) we are speculating for this block and have used that to speculate for
622 static bool IsValueFullyAvailableInBlock(BasicBlock *BB,
623 DenseMap<BasicBlock*, char> &FullyAvailableBlocks,
624 uint32_t RecurseDepth) {
625 if (RecurseDepth > MaxRecurseDepth)
628 // Optimistically assume that the block is fully available and check to see
629 // if we already know about this block in one lookup.
630 std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV =
631 FullyAvailableBlocks.insert(std::make_pair(BB, 2));
633 // If the entry already existed for this block, return the precomputed value.
635 // If this is a speculative "available" value, mark it as being used for
636 // speculation of other blocks.
637 if (IV.first->second == 2)
638 IV.first->second = 3;
639 return IV.first->second != 0;
642 // Otherwise, see if it is fully available in all predecessors.
643 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
645 // If this block has no predecessors, it isn't live-in here.
647 goto SpeculationFailure;
649 for (; PI != PE; ++PI)
650 // If the value isn't fully available in one of our predecessors, then it
651 // isn't fully available in this block either. Undo our previous
652 // optimistic assumption and bail out.
653 if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks,RecurseDepth+1))
654 goto SpeculationFailure;
658 // If we get here, we found out that this is not, after
659 // all, a fully-available block. We have a problem if we speculated on this and
660 // used the speculation to mark other blocks as available.
662 char &BBVal = FullyAvailableBlocks[BB];
664 // If we didn't speculate on this, just return with it set to false.
670 // If we did speculate on this value, we could have blocks set to 1 that are
671 // incorrect. Walk the (transitive) successors of this block and mark them as
673 SmallVector<BasicBlock*, 32> BBWorklist;
674 BBWorklist.push_back(BB);
677 BasicBlock *Entry = BBWorklist.pop_back_val();
678 // Note that this sets blocks to 0 (unavailable) if they happen to not
679 // already be in FullyAvailableBlocks. This is safe.
680 char &EntryVal = FullyAvailableBlocks[Entry];
681 if (EntryVal == 0) continue; // Already unavailable.
683 // Mark as unavailable.
686 BBWorklist.append(succ_begin(Entry), succ_end(Entry));
687 } while (!BBWorklist.empty());
693 /// Return true if CoerceAvailableValueToLoadType will succeed.
694 static bool CanCoerceMustAliasedValueToLoad(Value *StoredVal,
696 const DataLayout &DL) {
697 // If the loaded or stored value is an first class array or struct, don't try
698 // to transform them. We need to be able to bitcast to integer.
699 if (LoadTy->isStructTy() || LoadTy->isArrayTy() ||
700 StoredVal->getType()->isStructTy() ||
701 StoredVal->getType()->isArrayTy())
704 // The store has to be at least as big as the load.
705 if (DL.getTypeSizeInBits(StoredVal->getType()) <
706 DL.getTypeSizeInBits(LoadTy))
712 /// If we saw a store of a value to memory, and
713 /// then a load from a must-aliased pointer of a different type, try to coerce
714 /// the stored value. LoadedTy is the type of the load we want to replace.
715 /// IRB is IRBuilder used to insert new instructions.
717 /// If we can't do it, return null.
718 static Value *CoerceAvailableValueToLoadType(Value *StoredVal, Type *LoadedTy,
720 const DataLayout &DL) {
721 assert(CanCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
722 "precondition violation - materialization can't fail");
724 if (auto *C = dyn_cast<Constant>(StoredVal))
725 if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
726 StoredVal = FoldedStoredVal;
728 // If this is already the right type, just return it.
729 Type *StoredValTy = StoredVal->getType();
731 uint64_t StoredValSize = DL.getTypeSizeInBits(StoredValTy);
732 uint64_t LoadedValSize = DL.getTypeSizeInBits(LoadedTy);
734 // If the store and reload are the same size, we can always reuse it.
735 if (StoredValSize == LoadedValSize) {
736 // Pointer to Pointer -> use bitcast.
737 if (StoredValTy->getScalarType()->isPointerTy() &&
738 LoadedTy->getScalarType()->isPointerTy()) {
739 StoredVal = IRB.CreateBitCast(StoredVal, LoadedTy);
741 // Convert source pointers to integers, which can be bitcast.
742 if (StoredValTy->getScalarType()->isPointerTy()) {
743 StoredValTy = DL.getIntPtrType(StoredValTy);
744 StoredVal = IRB.CreatePtrToInt(StoredVal, StoredValTy);
747 Type *TypeToCastTo = LoadedTy;
748 if (TypeToCastTo->getScalarType()->isPointerTy())
749 TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
751 if (StoredValTy != TypeToCastTo)
752 StoredVal = IRB.CreateBitCast(StoredVal, TypeToCastTo);
754 // Cast to pointer if the load needs a pointer type.
755 if (LoadedTy->getScalarType()->isPointerTy())
756 StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy);
759 if (auto *C = dyn_cast<ConstantExpr>(StoredVal))
760 if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
761 StoredVal = FoldedStoredVal;
766 // If the loaded value is smaller than the available value, then we can
767 // extract out a piece from it. If the available value is too small, then we
768 // can't do anything.
769 assert(StoredValSize >= LoadedValSize &&
770 "CanCoerceMustAliasedValueToLoad fail");
772 // Convert source pointers to integers, which can be manipulated.
773 if (StoredValTy->getScalarType()->isPointerTy()) {
774 StoredValTy = DL.getIntPtrType(StoredValTy);
775 StoredVal = IRB.CreatePtrToInt(StoredVal, StoredValTy);
778 // Convert vectors and fp to integer, which can be manipulated.
779 if (!StoredValTy->isIntegerTy()) {
780 StoredValTy = IntegerType::get(StoredValTy->getContext(), StoredValSize);
781 StoredVal = IRB.CreateBitCast(StoredVal, StoredValTy);
784 // If this is a big-endian system, we need to shift the value down to the low
785 // bits so that a truncate will work.
786 if (DL.isBigEndian()) {
787 uint64_t ShiftAmt = DL.getTypeStoreSizeInBits(StoredValTy) -
788 DL.getTypeStoreSizeInBits(LoadedTy);
789 StoredVal = IRB.CreateLShr(StoredVal, ShiftAmt, "tmp");
792 // Truncate the integer to the right size now.
793 Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
794 StoredVal = IRB.CreateTrunc(StoredVal, NewIntTy, "trunc");
796 if (LoadedTy != NewIntTy) {
797 // If the result is a pointer, inttoptr.
798 if (LoadedTy->getScalarType()->isPointerTy())
799 StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy, "inttoptr");
801 // Otherwise, bitcast.
802 StoredVal = IRB.CreateBitCast(StoredVal, LoadedTy, "bitcast");
805 if (auto *C = dyn_cast<Constant>(StoredVal))
806 if (auto *FoldedStoredVal = ConstantFoldConstant(C, DL))
807 StoredVal = FoldedStoredVal;
812 /// This function is called when we have a
813 /// memdep query of a load that ends up being a clobbering memory write (store,
814 /// memset, memcpy, memmove). This means that the write *may* provide bits used
815 /// by the load but we can't be sure because the pointers don't mustalias.
817 /// Check this case to see if there is anything more we can do before we give
818 /// up. This returns -1 if we have to give up, or a byte number in the stored
819 /// value of the piece that feeds the load.
820 static int AnalyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
822 uint64_t WriteSizeInBits,
823 const DataLayout &DL) {
824 // If the loaded or stored value is a first class array or struct, don't try
825 // to transform them. We need to be able to bitcast to integer.
826 if (LoadTy->isStructTy() || LoadTy->isArrayTy())
829 int64_t StoreOffset = 0, LoadOffset = 0;
831 GetPointerBaseWithConstantOffset(WritePtr, StoreOffset, DL);
832 Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, DL);
833 if (StoreBase != LoadBase)
836 // If the load and store are to the exact same address, they should have been
837 // a must alias. AA must have gotten confused.
838 // FIXME: Study to see if/when this happens. One case is forwarding a memset
839 // to a load from the base of the memset.
841 // If the load and store don't overlap at all, the store doesn't provide
842 // anything to the load. In this case, they really don't alias at all, AA
843 // must have gotten confused.
844 uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy);
846 if ((WriteSizeInBits & 7) | (LoadSize & 7))
848 uint64_t StoreSize = WriteSizeInBits / 8; // Convert to bytes.
852 bool isAAFailure = false;
853 if (StoreOffset < LoadOffset)
854 isAAFailure = StoreOffset+int64_t(StoreSize) <= LoadOffset;
856 isAAFailure = LoadOffset+int64_t(LoadSize) <= StoreOffset;
861 // If the Load isn't completely contained within the stored bits, we don't
862 // have all the bits to feed it. We could do something crazy in the future
863 // (issue a smaller load then merge the bits in) but this seems unlikely to be
865 if (StoreOffset > LoadOffset ||
866 StoreOffset+StoreSize < LoadOffset+LoadSize)
869 // Okay, we can do this transformation. Return the number of bytes into the
870 // store that the load is.
871 return LoadOffset-StoreOffset;
874 /// This function is called when we have a
875 /// memdep query of a load that ends up being a clobbering store.
876 static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
878 // Cannot handle reading from store of first-class aggregate yet.
879 if (DepSI->getValueOperand()->getType()->isStructTy() ||
880 DepSI->getValueOperand()->getType()->isArrayTy())
883 const DataLayout &DL = DepSI->getModule()->getDataLayout();
884 Value *StorePtr = DepSI->getPointerOperand();
885 uint64_t StoreSize =DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
886 return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
887 StorePtr, StoreSize, DL);
890 /// This function is called when we have a
891 /// memdep query of a load that ends up being clobbered by another load. See if
892 /// the other load can feed into the second load.
893 static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
894 LoadInst *DepLI, const DataLayout &DL){
895 // Cannot handle reading from store of first-class aggregate yet.
896 if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
899 Value *DepPtr = DepLI->getPointerOperand();
900 uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType());
901 int R = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
902 if (R != -1) return R;
904 // If we have a load/load clobber an DepLI can be widened to cover this load,
905 // then we should widen it!
906 int64_t LoadOffs = 0;
907 const Value *LoadBase =
908 GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, DL);
909 unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
911 unsigned Size = MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
912 LoadBase, LoadOffs, LoadSize, DepLI);
913 if (Size == 0) return -1;
915 // Check non-obvious conditions enforced by MDA which we rely on for being
916 // able to materialize this potentially available value
917 assert(DepLI->isSimple() && "Cannot widen volatile/atomic load!");
918 assert(DepLI->getType()->isIntegerTy() && "Can't widen non-integer load");
920 return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size*8, DL);
925 static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
927 const DataLayout &DL) {
928 // If the mem operation is a non-constant size, we can't handle it.
929 ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
930 if (!SizeCst) return -1;
931 uint64_t MemSizeInBits = SizeCst->getZExtValue()*8;
933 // If this is memset, we just need to see if the offset is valid in the size
935 if (MI->getIntrinsicID() == Intrinsic::memset)
936 return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
939 // If we have a memcpy/memmove, the only case we can handle is if this is a
940 // copy from constant memory. In that case, we can read directly from the
942 MemTransferInst *MTI = cast<MemTransferInst>(MI);
944 Constant *Src = dyn_cast<Constant>(MTI->getSource());
947 GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Src, DL));
948 if (!GV || !GV->isConstant()) return -1;
950 // See if the access is within the bounds of the transfer.
951 int Offset = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
952 MI->getDest(), MemSizeInBits, DL);
956 unsigned AS = Src->getType()->getPointerAddressSpace();
957 // Otherwise, see if we can constant fold a load from the constant with the
958 // offset applied as appropriate.
959 Src = ConstantExpr::getBitCast(Src,
960 Type::getInt8PtrTy(Src->getContext(), AS));
961 Constant *OffsetCst =
962 ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
963 Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
965 Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
966 if (ConstantFoldLoadFromConstPtr(Src, LoadTy, DL))
972 /// This function is called when we have a
973 /// memdep query of a load that ends up being a clobbering store. This means
974 /// that the store provides bits used by the load but we the pointers don't
975 /// mustalias. Check this case to see if there is anything more we can do
976 /// before we give up.
977 static Value *GetStoreValueForLoad(Value *SrcVal, unsigned Offset,
979 Instruction *InsertPt, const DataLayout &DL){
980 LLVMContext &Ctx = SrcVal->getType()->getContext();
982 uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
983 uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
985 IRBuilder<> Builder(InsertPt);
987 // Compute which bits of the stored value are being used by the load. Convert
988 // to an integer type to start with.
989 if (SrcVal->getType()->getScalarType()->isPointerTy())
990 SrcVal = Builder.CreatePtrToInt(SrcVal,
991 DL.getIntPtrType(SrcVal->getType()));
992 if (!SrcVal->getType()->isIntegerTy())
993 SrcVal = Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize*8));
995 // Shift the bits to the least significant depending on endianness.
997 if (DL.isLittleEndian())
1000 ShiftAmt = (StoreSize-LoadSize-Offset)*8;
1003 SrcVal = Builder.CreateLShr(SrcVal, ShiftAmt);
1005 if (LoadSize != StoreSize)
1006 SrcVal = Builder.CreateTrunc(SrcVal, IntegerType::get(Ctx, LoadSize*8));
1008 return CoerceAvailableValueToLoadType(SrcVal, LoadTy, Builder, DL);
1011 /// This function is called when we have a
1012 /// memdep query of a load that ends up being a clobbering load. This means
1013 /// that the load *may* provide bits used by the load but we can't be sure
1014 /// because the pointers don't mustalias. Check this case to see if there is
1015 /// anything more we can do before we give up.
1016 static Value *GetLoadValueForLoad(LoadInst *SrcVal, unsigned Offset,
1017 Type *LoadTy, Instruction *InsertPt,
1019 const DataLayout &DL = SrcVal->getModule()->getDataLayout();
1020 // If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
1021 // widen SrcVal out to a larger load.
1022 unsigned SrcValStoreSize = DL.getTypeStoreSize(SrcVal->getType());
1023 unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
1024 if (Offset+LoadSize > SrcValStoreSize) {
1025 assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
1026 assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
1027 // If we have a load/load clobber an DepLI can be widened to cover this
1028 // load, then we should widen it to the next power of 2 size big enough!
1029 unsigned NewLoadSize = Offset+LoadSize;
1030 if (!isPowerOf2_32(NewLoadSize))
1031 NewLoadSize = NextPowerOf2(NewLoadSize);
1033 Value *PtrVal = SrcVal->getPointerOperand();
1035 // Insert the new load after the old load. This ensures that subsequent
1036 // memdep queries will find the new load. We can't easily remove the old
1037 // load completely because it is already in the value numbering table.
1038 IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
1040 IntegerType::get(LoadTy->getContext(), NewLoadSize*8);
1041 DestPTy = PointerType::get(DestPTy,
1042 PtrVal->getType()->getPointerAddressSpace());
1043 Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
1044 PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
1045 LoadInst *NewLoad = Builder.CreateLoad(PtrVal);
1046 NewLoad->takeName(SrcVal);
1047 NewLoad->setAlignment(SrcVal->getAlignment());
1049 DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
1050 DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
1052 // Replace uses of the original load with the wider load. On a big endian
1053 // system, we need to shift down to get the relevant bits.
1054 Value *RV = NewLoad;
1055 if (DL.isBigEndian())
1056 RV = Builder.CreateLShr(RV, (NewLoadSize - SrcValStoreSize) * 8);
1057 RV = Builder.CreateTrunc(RV, SrcVal->getType());
1058 SrcVal->replaceAllUsesWith(RV);
1060 // We would like to use gvn.markInstructionForDeletion here, but we can't
1061 // because the load is already memoized into the leader map table that GVN
1062 // tracks. It is potentially possible to remove the load from the table,
1063 // but then there all of the operations based on it would need to be
1064 // rehashed. Just leave the dead load around.
1065 gvn.getMemDep().removeInstruction(SrcVal);
1069 return GetStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
1073 /// This function is called when we have a
1074 /// memdep query of a load that ends up being a clobbering mem intrinsic.
1075 static Value *GetMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
1076 Type *LoadTy, Instruction *InsertPt,
1077 const DataLayout &DL){
1078 LLVMContext &Ctx = LoadTy->getContext();
1079 uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy)/8;
1081 IRBuilder<> Builder(InsertPt);
1083 // We know that this method is only called when the mem transfer fully
1084 // provides the bits for the load.
1085 if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
1086 // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
1087 // independently of what the offset is.
1088 Value *Val = MSI->getValue();
1090 Val = Builder.CreateZExt(Val, IntegerType::get(Ctx, LoadSize*8));
1092 Value *OneElt = Val;
1094 // Splat the value out to the right number of bits.
1095 for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize; ) {
1096 // If we can double the number of bytes set, do it.
1097 if (NumBytesSet*2 <= LoadSize) {
1098 Value *ShVal = Builder.CreateShl(Val, NumBytesSet*8);
1099 Val = Builder.CreateOr(Val, ShVal);
1104 // Otherwise insert one byte at a time.
1105 Value *ShVal = Builder.CreateShl(Val, 1*8);
1106 Val = Builder.CreateOr(OneElt, ShVal);
1110 return CoerceAvailableValueToLoadType(Val, LoadTy, Builder, DL);
1113 // Otherwise, this is a memcpy/memmove from a constant global.
1114 MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
1115 Constant *Src = cast<Constant>(MTI->getSource());
1116 unsigned AS = Src->getType()->getPointerAddressSpace();
1118 // Otherwise, see if we can constant fold a load from the constant with the
1119 // offset applied as appropriate.
1120 Src = ConstantExpr::getBitCast(Src,
1121 Type::getInt8PtrTy(Src->getContext(), AS));
1122 Constant *OffsetCst =
1123 ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
1124 Src = ConstantExpr::getGetElementPtr(Type::getInt8Ty(Src->getContext()), Src,
1126 Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
1127 return ConstantFoldLoadFromConstPtr(Src, LoadTy, DL);
1131 /// Given a set of loads specified by ValuesPerBlock,
1132 /// construct SSA form, allowing us to eliminate LI. This returns the value
1133 /// that should be used at LI's definition site.
1134 static Value *ConstructSSAForLoadSet(LoadInst *LI,
1135 SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
1137 // Check for the fully redundant, dominating load case. In this case, we can
1138 // just use the dominating value directly.
1139 if (ValuesPerBlock.size() == 1 &&
1140 gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
1142 assert(!ValuesPerBlock[0].AV.isUndefValue() &&
1143 "Dead BB dominate this block");
1144 return ValuesPerBlock[0].MaterializeAdjustedValue(LI, gvn);
1147 // Otherwise, we have to construct SSA form.
1148 SmallVector<PHINode*, 8> NewPHIs;
1149 SSAUpdater SSAUpdate(&NewPHIs);
1150 SSAUpdate.Initialize(LI->getType(), LI->getName());
1152 for (const AvailableValueInBlock &AV : ValuesPerBlock) {
1153 BasicBlock *BB = AV.BB;
1155 if (SSAUpdate.HasValueForBlock(BB))
1158 SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LI, gvn));
1161 // Perform PHI construction.
1162 return SSAUpdate.GetValueInMiddleOfBlock(LI->getParent());
1165 Value *AvailableValue::MaterializeAdjustedValue(LoadInst *LI,
1166 Instruction *InsertPt,
1169 Type *LoadTy = LI->getType();
1170 const DataLayout &DL = LI->getModule()->getDataLayout();
1171 if (isSimpleValue()) {
1172 Res = getSimpleValue();
1173 if (Res->getType() != LoadTy) {
1174 Res = GetStoreValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
1176 DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset << " "
1177 << *getSimpleValue() << '\n'
1178 << *Res << '\n' << "\n\n\n");
1180 } else if (isCoercedLoadValue()) {
1181 LoadInst *Load = getCoercedLoadValue();
1182 if (Load->getType() == LoadTy && Offset == 0) {
1185 Res = GetLoadValueForLoad(Load, Offset, LoadTy, InsertPt, gvn);
1187 DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset << " "
1188 << *getCoercedLoadValue() << '\n'
1189 << *Res << '\n' << "\n\n\n");
1191 } else if (isMemIntrinValue()) {
1192 Res = GetMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
1194 DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
1195 << " " << *getMemIntrinValue() << '\n'
1196 << *Res << '\n' << "\n\n\n");
1198 assert(isUndefValue() && "Should be UndefVal");
1199 DEBUG(dbgs() << "GVN COERCED NONLOCAL Undef:\n";);
1200 return UndefValue::get(LoadTy);
1202 assert(Res && "failed to materialize?");
1206 static bool isLifetimeStart(const Instruction *Inst) {
1207 if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
1208 return II->getIntrinsicID() == Intrinsic::lifetime_start;
1212 /// \brief Try to locate the three instruction involved in a missed
1213 /// load-elimination case that is due to an intervening store.
1214 static void reportMayClobberedLoad(LoadInst *LI, MemDepResult DepInfo,
1216 OptimizationRemarkEmitter *ORE) {
1217 using namespace ore;
1218 User *OtherAccess = nullptr;
1220 OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", LI);
1221 R << "load of type " << NV("Type", LI->getType()) << " not eliminated"
1224 for (auto *U : LI->getPointerOperand()->users())
1225 if (U != LI && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
1226 DT->dominates(cast<Instruction>(U), LI)) {
1227 // FIXME: for now give up if there are multiple memory accesses that
1228 // dominate the load. We need further analysis to decide which one is
1229 // that we're forwarding from.
1231 OtherAccess = nullptr;
1237 R << " in favor of " << NV("OtherAccess", OtherAccess);
1239 R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
1244 bool GVN::AnalyzeLoadAvailability(LoadInst *LI, MemDepResult DepInfo,
1245 Value *Address, AvailableValue &Res) {
1247 assert((DepInfo.isDef() || DepInfo.isClobber()) &&
1248 "expected a local dependence");
1249 assert(LI->isUnordered() && "rules below are incorrect for ordered access");
1251 const DataLayout &DL = LI->getModule()->getDataLayout();
1253 if (DepInfo.isClobber()) {
1254 // If the dependence is to a store that writes to a superset of the bits
1255 // read by the load, we can extract the bits we need for the load from the
1257 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInfo.getInst())) {
1258 // Can't forward from non-atomic to atomic without violating memory model.
1259 if (Address && LI->isAtomic() <= DepSI->isAtomic()) {
1261 AnalyzeLoadFromClobberingStore(LI->getType(), Address, DepSI);
1263 Res = AvailableValue::get(DepSI->getValueOperand(), Offset);
1269 // Check to see if we have something like this:
1272 // if we have this, replace the later with an extraction from the former.
1273 if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInfo.getInst())) {
1274 // If this is a clobber and L is the first instruction in its block, then
1275 // we have the first instruction in the entry block.
1276 // Can't forward from non-atomic to atomic without violating memory model.
1277 if (DepLI != LI && Address && LI->isAtomic() <= DepLI->isAtomic()) {
1279 AnalyzeLoadFromClobberingLoad(LI->getType(), Address, DepLI, DL);
1282 Res = AvailableValue::getLoad(DepLI, Offset);
1288 // If the clobbering value is a memset/memcpy/memmove, see if we can
1289 // forward a value on from it.
1290 if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInfo.getInst())) {
1291 if (Address && !LI->isAtomic()) {
1292 int Offset = AnalyzeLoadFromClobberingMemInst(LI->getType(), Address,
1295 Res = AvailableValue::getMI(DepMI, Offset);
1300 // Nothing known about this clobber, have to be conservative
1302 // fast print dep, using operator<< on instruction is too slow.
1303 dbgs() << "GVN: load ";
1304 LI->printAsOperand(dbgs());
1305 Instruction *I = DepInfo.getInst();
1306 dbgs() << " is clobbered by " << *I << '\n';
1309 if (ORE->allowExtraAnalysis())
1310 reportMayClobberedLoad(LI, DepInfo, DT, ORE);
1314 assert(DepInfo.isDef() && "follows from above");
1316 Instruction *DepInst = DepInfo.getInst();
1318 // Loading the allocation -> undef.
1319 if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
1320 // Loading immediately after lifetime begin -> undef.
1321 isLifetimeStart(DepInst)) {
1322 Res = AvailableValue::get(UndefValue::get(LI->getType()));
1326 // Loading from calloc (which zero initializes memory) -> zero
1327 if (isCallocLikeFn(DepInst, TLI)) {
1328 Res = AvailableValue::get(Constant::getNullValue(LI->getType()));
1332 if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1333 // Reject loads and stores that are to the same address but are of
1334 // different types if we have to. If the stored value is larger or equal to
1335 // the loaded value, we can reuse it.
1336 if (S->getValueOperand()->getType() != LI->getType() &&
1337 !CanCoerceMustAliasedValueToLoad(S->getValueOperand(),
1341 // Can't forward from non-atomic to atomic without violating memory model.
1342 if (S->isAtomic() < LI->isAtomic())
1345 Res = AvailableValue::get(S->getValueOperand());
1349 if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1350 // If the types mismatch and we can't handle it, reject reuse of the load.
1351 // If the stored value is larger or equal to the loaded value, we can reuse
1353 if (LD->getType() != LI->getType() &&
1354 !CanCoerceMustAliasedValueToLoad(LD, LI->getType(), DL))
1357 // Can't forward from non-atomic to atomic without violating memory model.
1358 if (LD->isAtomic() < LI->isAtomic())
1361 Res = AvailableValue::getLoad(LD);
1365 // Unknown def - must be conservative
1367 // fast print dep, using operator<< on instruction is too slow.
1368 dbgs() << "GVN: load ";
1369 LI->printAsOperand(dbgs());
1370 dbgs() << " has unknown def " << *DepInst << '\n';
1375 void GVN::AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
1376 AvailValInBlkVect &ValuesPerBlock,
1377 UnavailBlkVect &UnavailableBlocks) {
1379 // Filter out useless results (non-locals, etc). Keep track of the blocks
1380 // where we have a value available in repl, also keep track of whether we see
1381 // dependencies that produce an unknown value for the load (such as a call
1382 // that could potentially clobber the load).
1383 unsigned NumDeps = Deps.size();
1384 for (unsigned i = 0, e = NumDeps; i != e; ++i) {
1385 BasicBlock *DepBB = Deps[i].getBB();
1386 MemDepResult DepInfo = Deps[i].getResult();
1388 if (DeadBlocks.count(DepBB)) {
1389 // Dead dependent mem-op disguise as a load evaluating the same value
1390 // as the load in question.
1391 ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1395 if (!DepInfo.isDef() && !DepInfo.isClobber()) {
1396 UnavailableBlocks.push_back(DepBB);
1400 // The address being loaded in this non-local block may not be the same as
1401 // the pointer operand of the load if PHI translation occurs. Make sure
1402 // to consider the right address.
1403 Value *Address = Deps[i].getAddress();
1406 if (AnalyzeLoadAvailability(LI, DepInfo, Address, AV)) {
1407 // subtlety: because we know this was a non-local dependency, we know
1408 // it's safe to materialize anywhere between the instruction within
1409 // DepInfo and the end of it's block.
1410 ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
1413 UnavailableBlocks.push_back(DepBB);
1417 assert(NumDeps == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1418 "post condition violation");
1421 bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
1422 UnavailBlkVect &UnavailableBlocks) {
1423 // Okay, we have *some* definitions of the value. This means that the value
1424 // is available in some of our (transitive) predecessors. Lets think about
1425 // doing PRE of this load. This will involve inserting a new load into the
1426 // predecessor when it's not available. We could do this in general, but
1427 // prefer to not increase code size. As such, we only do this when we know
1428 // that we only have to insert *one* load (which means we're basically moving
1429 // the load, not inserting a new one).
1431 SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
1432 UnavailableBlocks.end());
1434 // Let's find the first basic block with more than one predecessor. Walk
1435 // backwards through predecessors if needed.
1436 BasicBlock *LoadBB = LI->getParent();
1437 BasicBlock *TmpBB = LoadBB;
1439 while (TmpBB->getSinglePredecessor()) {
1440 TmpBB = TmpBB->getSinglePredecessor();
1441 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1443 if (Blockers.count(TmpBB))
1446 // If any of these blocks has more than one successor (i.e. if the edge we
1447 // just traversed was critical), then there are other paths through this
1448 // block along which the load may not be anticipated. Hoisting the load
1449 // above this block would be adding the load to execution paths along
1450 // which it was not previously executed.
1451 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1458 // Check to see how many predecessors have the loaded value fully
1460 MapVector<BasicBlock *, Value *> PredLoads;
1461 DenseMap<BasicBlock*, char> FullyAvailableBlocks;
1462 for (const AvailableValueInBlock &AV : ValuesPerBlock)
1463 FullyAvailableBlocks[AV.BB] = true;
1464 for (BasicBlock *UnavailableBB : UnavailableBlocks)
1465 FullyAvailableBlocks[UnavailableBB] = false;
1467 SmallVector<BasicBlock *, 4> CriticalEdgePred;
1468 for (BasicBlock *Pred : predecessors(LoadBB)) {
1469 // If any predecessor block is an EH pad that does not allow non-PHI
1470 // instructions before the terminator, we can't PRE the load.
1471 if (Pred->getTerminator()->isEHPad()) {
1473 << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1474 << Pred->getName() << "': " << *LI << '\n');
1478 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
1482 if (Pred->getTerminator()->getNumSuccessors() != 1) {
1483 if (isa<IndirectBrInst>(Pred->getTerminator())) {
1484 DEBUG(dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1485 << Pred->getName() << "': " << *LI << '\n');
1489 if (LoadBB->isEHPad()) {
1491 << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1492 << Pred->getName() << "': " << *LI << '\n');
1496 CriticalEdgePred.push_back(Pred);
1498 // Only add the predecessors that will not be split for now.
1499 PredLoads[Pred] = nullptr;
1503 // Decide whether PRE is profitable for this load.
1504 unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
1505 assert(NumUnavailablePreds != 0 &&
1506 "Fully available value should already be eliminated!");
1508 // If this load is unavailable in multiple predecessors, reject it.
1509 // FIXME: If we could restructure the CFG, we could make a common pred with
1510 // all the preds that don't have an available LI and insert a new load into
1512 if (NumUnavailablePreds != 1)
1515 // Split critical edges, and update the unavailable predecessors accordingly.
1516 for (BasicBlock *OrigPred : CriticalEdgePred) {
1517 BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1518 assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1519 PredLoads[NewPred] = nullptr;
1520 DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1521 << LoadBB->getName() << '\n');
1524 // Check if the load can safely be moved to all the unavailable predecessors.
1525 bool CanDoPRE = true;
1526 const DataLayout &DL = LI->getModule()->getDataLayout();
1527 SmallVector<Instruction*, 8> NewInsts;
1528 for (auto &PredLoad : PredLoads) {
1529 BasicBlock *UnavailablePred = PredLoad.first;
1531 // Do PHI translation to get its value in the predecessor if necessary. The
1532 // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1534 // If all preds have a single successor, then we know it is safe to insert
1535 // the load on the pred (?!?), so we can insert code to materialize the
1536 // pointer if it is not available.
1537 PHITransAddr Address(LI->getPointerOperand(), DL, AC);
1538 Value *LoadPtr = nullptr;
1539 LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred,
1542 // If we couldn't find or insert a computation of this phi translated value,
1545 DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1546 << *LI->getPointerOperand() << "\n");
1551 PredLoad.second = LoadPtr;
1555 while (!NewInsts.empty()) {
1556 Instruction *I = NewInsts.pop_back_val();
1557 if (MD) MD->removeInstruction(I);
1558 I->eraseFromParent();
1560 // HINT: Don't revert the edge-splitting as following transformation may
1561 // also need to split these critical edges.
1562 return !CriticalEdgePred.empty();
1565 // Okay, we can eliminate this load by inserting a reload in the predecessor
1566 // and using PHI construction to get the value in the other predecessors, do
1568 DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *LI << '\n');
1569 DEBUG(if (!NewInsts.empty())
1570 dbgs() << "INSERTED " << NewInsts.size() << " INSTS: "
1571 << *NewInsts.back() << '\n');
1573 // Assign value numbers to the new instructions.
1574 for (Instruction *I : NewInsts) {
1575 // Instructions that have been inserted in predecessor(s) to materialize
1576 // the load address do not retain their original debug locations. Doing
1577 // so could lead to confusing (but correct) source attributions.
1578 // FIXME: How do we retain source locations without causing poor debugging
1580 I->setDebugLoc(DebugLoc());
1582 // FIXME: We really _ought_ to insert these value numbers into their
1583 // parent's availability map. However, in doing so, we risk getting into
1584 // ordering issues. If a block hasn't been processed yet, we would be
1585 // marking a value as AVAIL-IN, which isn't what we intend.
1589 for (const auto &PredLoad : PredLoads) {
1590 BasicBlock *UnavailablePred = PredLoad.first;
1591 Value *LoadPtr = PredLoad.second;
1593 auto *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre",
1594 LI->isVolatile(), LI->getAlignment(),
1595 LI->getOrdering(), LI->getSynchScope(),
1596 UnavailablePred->getTerminator());
1598 // Transfer the old load's AA tags to the new load.
1600 LI->getAAMetadata(Tags);
1602 NewLoad->setAAMetadata(Tags);
1604 if (auto *MD = LI->getMetadata(LLVMContext::MD_invariant_load))
1605 NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1606 if (auto *InvGroupMD = LI->getMetadata(LLVMContext::MD_invariant_group))
1607 NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1608 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range))
1609 NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1611 // We do not propagate the old load's debug location, because the new
1612 // load now lives in a different BB, and we want to avoid a jumpy line
1614 // FIXME: How do we retain source locations without causing poor debugging
1617 // Add the newly created load.
1618 ValuesPerBlock.push_back(AvailableValueInBlock::get(UnavailablePred,
1620 MD->invalidateCachedPointerInfo(LoadPtr);
1621 DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1624 // Perform PHI construction.
1625 Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1626 LI->replaceAllUsesWith(V);
1627 if (isa<PHINode>(V))
1629 if (Instruction *I = dyn_cast<Instruction>(V))
1630 I->setDebugLoc(LI->getDebugLoc());
1631 if (V->getType()->getScalarType()->isPointerTy())
1632 MD->invalidateCachedPointerInfo(V);
1633 markInstructionForDeletion(LI);
1634 ORE->emit(OptimizationRemark(DEBUG_TYPE, "LoadPRE", LI)
1635 << "load eliminated by PRE");
1640 static void reportLoadElim(LoadInst *LI, Value *AvailableValue,
1641 OptimizationRemarkEmitter *ORE) {
1642 using namespace ore;
1643 ORE->emit(OptimizationRemark(DEBUG_TYPE, "LoadElim", LI)
1644 << "load of type " << NV("Type", LI->getType()) << " eliminated"
1645 << setExtraArgs() << " in favor of "
1646 << NV("InfavorOfValue", AvailableValue));
1649 /// Attempt to eliminate a load whose dependencies are
1650 /// non-local by performing PHI construction.
1651 bool GVN::processNonLocalLoad(LoadInst *LI) {
1652 // non-local speculations are not allowed under asan.
1653 if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeAddress))
1656 // Step 1: Find the non-local dependencies of the load.
1658 MD->getNonLocalPointerDependency(LI, Deps);
1660 // If we had to process more than one hundred blocks to find the
1661 // dependencies, this load isn't worth worrying about. Optimizing
1662 // it will be too expensive.
1663 unsigned NumDeps = Deps.size();
1667 // If we had a phi translation failure, we'll have a single entry which is a
1668 // clobber in the current block. Reject this early.
1670 !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
1672 dbgs() << "GVN: non-local load ";
1673 LI->printAsOperand(dbgs());
1674 dbgs() << " has unknown dependencies\n";
1679 // If this load follows a GEP, see if we can PRE the indices before analyzing.
1680 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) {
1681 for (GetElementPtrInst::op_iterator OI = GEP->idx_begin(),
1682 OE = GEP->idx_end();
1684 if (Instruction *I = dyn_cast<Instruction>(OI->get()))
1685 performScalarPRE(I);
1688 // Step 2: Analyze the availability of the load
1689 AvailValInBlkVect ValuesPerBlock;
1690 UnavailBlkVect UnavailableBlocks;
1691 AnalyzeLoadAvailability(LI, Deps, ValuesPerBlock, UnavailableBlocks);
1693 // If we have no predecessors that produce a known value for this load, exit
1695 if (ValuesPerBlock.empty())
1698 // Step 3: Eliminate fully redundancy.
1700 // If all of the instructions we depend on produce a known value for this
1701 // load, then it is fully redundant and we can use PHI insertion to compute
1702 // its value. Insert PHIs and remove the fully redundant value now.
1703 if (UnavailableBlocks.empty()) {
1704 DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *LI << '\n');
1706 // Perform PHI construction.
1707 Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1708 LI->replaceAllUsesWith(V);
1710 if (isa<PHINode>(V))
1712 if (Instruction *I = dyn_cast<Instruction>(V))
1713 // If instruction I has debug info, then we should not update it.
1714 // Also, if I has a null DebugLoc, then it is still potentially incorrect
1715 // to propagate LI's DebugLoc because LI may not post-dominate I.
1716 if (LI->getDebugLoc() && ValuesPerBlock.size() != 1)
1717 I->setDebugLoc(LI->getDebugLoc());
1718 if (V->getType()->getScalarType()->isPointerTy())
1719 MD->invalidateCachedPointerInfo(V);
1720 markInstructionForDeletion(LI);
1722 reportLoadElim(LI, V, ORE);
1726 // Step 4: Eliminate partial redundancy.
1727 if (!EnablePRE || !EnableLoadPRE)
1730 return PerformLoadPRE(LI, ValuesPerBlock, UnavailableBlocks);
1733 bool GVN::processAssumeIntrinsic(IntrinsicInst *IntrinsicI) {
1734 assert(IntrinsicI->getIntrinsicID() == Intrinsic::assume &&
1735 "This function can only be called with llvm.assume intrinsic");
1736 Value *V = IntrinsicI->getArgOperand(0);
1738 if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
1739 if (Cond->isZero()) {
1740 Type *Int8Ty = Type::getInt8Ty(V->getContext());
1741 // Insert a new store to null instruction before the load to indicate that
1742 // this code is not reachable. FIXME: We could insert unreachable
1743 // instruction directly because we can modify the CFG.
1744 new StoreInst(UndefValue::get(Int8Ty),
1745 Constant::getNullValue(Int8Ty->getPointerTo()),
1748 markInstructionForDeletion(IntrinsicI);
1752 Constant *True = ConstantInt::getTrue(V->getContext());
1753 bool Changed = false;
1755 for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
1756 BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
1758 // This property is only true in dominated successors, propagateEquality
1759 // will check dominance for us.
1760 Changed |= propagateEquality(V, True, Edge, false);
1763 // We can replace assume value with true, which covers cases like this:
1764 // call void @llvm.assume(i1 %cmp)
1765 // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
1766 ReplaceWithConstMap[V] = True;
1768 // If one of *cmp *eq operand is const, adding it to map will cover this:
1769 // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
1770 // call void @llvm.assume(i1 %cmp)
1771 // ret float %0 ; will change it to ret float 3.000000e+00
1772 if (auto *CmpI = dyn_cast<CmpInst>(V)) {
1773 if (CmpI->getPredicate() == CmpInst::Predicate::ICMP_EQ ||
1774 CmpI->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
1775 (CmpI->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
1776 CmpI->getFastMathFlags().noNaNs())) {
1777 Value *CmpLHS = CmpI->getOperand(0);
1778 Value *CmpRHS = CmpI->getOperand(1);
1779 if (isa<Constant>(CmpLHS))
1780 std::swap(CmpLHS, CmpRHS);
1781 auto *RHSConst = dyn_cast<Constant>(CmpRHS);
1783 // If only one operand is constant.
1784 if (RHSConst != nullptr && !isa<Constant>(CmpLHS))
1785 ReplaceWithConstMap[CmpLHS] = RHSConst;
1791 static void patchReplacementInstruction(Instruction *I, Value *Repl) {
1792 auto *ReplInst = dyn_cast<Instruction>(Repl);
1796 // Patch the replacement so that it is not more restrictive than the value
1798 // Note that if 'I' is a load being replaced by some operation,
1799 // for example, by an arithmetic operation, then andIRFlags()
1800 // would just erase all math flags from the original arithmetic
1801 // operation, which is clearly not wanted and not needed.
1802 if (!isa<LoadInst>(I))
1803 ReplInst->andIRFlags(I);
1805 // FIXME: If both the original and replacement value are part of the
1806 // same control-flow region (meaning that the execution of one
1807 // guarantees the execution of the other), then we can combine the
1808 // noalias scopes here and do better than the general conservative
1809 // answer used in combineMetadata().
1811 // In general, GVN unifies expressions over different control-flow
1812 // regions, and so we need a conservative combination of the noalias
1814 static const unsigned KnownIDs[] = {
1815 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
1816 LLVMContext::MD_noalias, LLVMContext::MD_range,
1817 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
1818 LLVMContext::MD_invariant_group};
1819 combineMetadata(ReplInst, I, KnownIDs);
1822 static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
1823 patchReplacementInstruction(I, Repl);
1824 I->replaceAllUsesWith(Repl);
1827 /// Attempt to eliminate a load, first by eliminating it
1828 /// locally, and then attempting non-local elimination if that fails.
1829 bool GVN::processLoad(LoadInst *L) {
1833 // This code hasn't been audited for ordered or volatile memory access
1834 if (!L->isUnordered())
1837 if (L->use_empty()) {
1838 markInstructionForDeletion(L);
1842 // ... to a pointer that has been loaded from before...
1843 MemDepResult Dep = MD->getDependency(L);
1845 // If it is defined in another block, try harder.
1846 if (Dep.isNonLocal())
1847 return processNonLocalLoad(L);
1849 // Only handle the local case below
1850 if (!Dep.isDef() && !Dep.isClobber()) {
1851 // This might be a NonFuncLocal or an Unknown
1853 // fast print dep, using operator<< on instruction is too slow.
1854 dbgs() << "GVN: load ";
1855 L->printAsOperand(dbgs());
1856 dbgs() << " has unknown dependence\n";
1862 if (AnalyzeLoadAvailability(L, Dep, L->getPointerOperand(), AV)) {
1863 Value *AvailableValue = AV.MaterializeAdjustedValue(L, L, *this);
1865 // Replace the load!
1866 patchAndReplaceAllUsesWith(L, AvailableValue);
1867 markInstructionForDeletion(L);
1869 reportLoadElim(L, AvailableValue, ORE);
1870 // Tell MDA to rexamine the reused pointer since we might have more
1871 // information after forwarding it.
1872 if (MD && AvailableValue->getType()->getScalarType()->isPointerTy())
1873 MD->invalidateCachedPointerInfo(AvailableValue);
1880 // In order to find a leader for a given value number at a
1881 // specific basic block, we first obtain the list of all Values for that number,
1882 // and then scan the list to find one whose block dominates the block in
1883 // question. This is fast because dominator tree queries consist of only
1884 // a few comparisons of DFS numbers.
1885 Value *GVN::findLeader(const BasicBlock *BB, uint32_t num) {
1886 LeaderTableEntry Vals = LeaderTable[num];
1887 if (!Vals.Val) return nullptr;
1889 Value *Val = nullptr;
1890 if (DT->dominates(Vals.BB, BB)) {
1892 if (isa<Constant>(Val)) return Val;
1895 LeaderTableEntry* Next = Vals.Next;
1897 if (DT->dominates(Next->BB, BB)) {
1898 if (isa<Constant>(Next->Val)) return Next->Val;
1899 if (!Val) Val = Next->Val;
1908 /// There is an edge from 'Src' to 'Dst'. Return
1909 /// true if every path from the entry block to 'Dst' passes via this edge. In
1910 /// particular 'Dst' must not be reachable via another edge from 'Src'.
1911 static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
1912 DominatorTree *DT) {
1913 // While in theory it is interesting to consider the case in which Dst has
1914 // more than one predecessor, because Dst might be part of a loop which is
1915 // only reachable from Src, in practice it is pointless since at the time
1916 // GVN runs all such loops have preheaders, which means that Dst will have
1917 // been changed to have only one predecessor, namely Src.
1918 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
1919 assert((!Pred || Pred == E.getStart()) &&
1920 "No edge between these basic blocks!");
1921 return Pred != nullptr;
1924 // Tries to replace instruction with const, using information from
1925 // ReplaceWithConstMap.
1926 bool GVN::replaceOperandsWithConsts(Instruction *Instr) const {
1927 bool Changed = false;
1928 for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
1929 Value *Operand = Instr->getOperand(OpNum);
1930 auto it = ReplaceWithConstMap.find(Operand);
1931 if (it != ReplaceWithConstMap.end()) {
1932 assert(!isa<Constant>(Operand) &&
1933 "Replacing constants with constants is invalid");
1934 DEBUG(dbgs() << "GVN replacing: " << *Operand << " with " << *it->second
1935 << " in instruction " << *Instr << '\n');
1936 Instr->setOperand(OpNum, it->second);
1943 /// The given values are known to be equal in every block
1944 /// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
1945 /// 'RHS' everywhere in the scope. Returns whether a change was made.
1946 /// If DominatesByEdge is false, then it means that we will propagate the RHS
1947 /// value starting from the end of Root.Start.
1948 bool GVN::propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root,
1949 bool DominatesByEdge) {
1950 SmallVector<std::pair<Value*, Value*>, 4> Worklist;
1951 Worklist.push_back(std::make_pair(LHS, RHS));
1952 bool Changed = false;
1953 // For speed, compute a conservative fast approximation to
1954 // DT->dominates(Root, Root.getEnd());
1955 const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
1957 while (!Worklist.empty()) {
1958 std::pair<Value*, Value*> Item = Worklist.pop_back_val();
1959 LHS = Item.first; RHS = Item.second;
1963 assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
1965 // Don't try to propagate equalities between constants.
1966 if (isa<Constant>(LHS) && isa<Constant>(RHS))
1969 // Prefer a constant on the right-hand side, or an Argument if no constants.
1970 if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
1971 std::swap(LHS, RHS);
1972 assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
1974 // If there is no obvious reason to prefer the left-hand side over the
1975 // right-hand side, ensure the longest lived term is on the right-hand side,
1976 // so the shortest lived term will be replaced by the longest lived.
1977 // This tends to expose more simplifications.
1978 uint32_t LVN = VN.lookupOrAdd(LHS);
1979 if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
1980 (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
1981 // Move the 'oldest' value to the right-hand side, using the value number
1982 // as a proxy for age.
1983 uint32_t RVN = VN.lookupOrAdd(RHS);
1985 std::swap(LHS, RHS);
1990 // If value numbering later sees that an instruction in the scope is equal
1991 // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
1992 // the invariant that instructions only occur in the leader table for their
1993 // own value number (this is used by removeFromLeaderTable), do not do this
1994 // if RHS is an instruction (if an instruction in the scope is morphed into
1995 // LHS then it will be turned into RHS by the next GVN iteration anyway, so
1996 // using the leader table is about compiling faster, not optimizing better).
1997 // The leader table only tracks basic blocks, not edges. Only add to if we
1998 // have the simple case where the edge dominates the end.
1999 if (RootDominatesEnd && !isa<Instruction>(RHS))
2000 addToLeaderTable(LVN, RHS, Root.getEnd());
2002 // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
2003 // LHS always has at least one use that is not dominated by Root, this will
2004 // never do anything if LHS has only one use.
2005 if (!LHS->hasOneUse()) {
2006 unsigned NumReplacements =
2008 ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
2009 : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
2011 Changed |= NumReplacements > 0;
2012 NumGVNEqProp += NumReplacements;
2015 // Now try to deduce additional equalities from this one. For example, if
2016 // the known equality was "(A != B)" == "false" then it follows that A and B
2017 // are equal in the scope. Only boolean equalities with an explicit true or
2018 // false RHS are currently supported.
2019 if (!RHS->getType()->isIntegerTy(1))
2020 // Not a boolean equality - bail out.
2022 ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
2024 // RHS neither 'true' nor 'false' - bail out.
2026 // Whether RHS equals 'true'. Otherwise it equals 'false'.
2027 bool isKnownTrue = CI->isAllOnesValue();
2028 bool isKnownFalse = !isKnownTrue;
2030 // If "A && B" is known true then both A and B are known true. If "A || B"
2031 // is known false then both A and B are known false.
2033 if ((isKnownTrue && match(LHS, m_And(m_Value(A), m_Value(B)))) ||
2034 (isKnownFalse && match(LHS, m_Or(m_Value(A), m_Value(B))))) {
2035 Worklist.push_back(std::make_pair(A, RHS));
2036 Worklist.push_back(std::make_pair(B, RHS));
2040 // If we are propagating an equality like "(A == B)" == "true" then also
2041 // propagate the equality A == B. When propagating a comparison such as
2042 // "(A >= B)" == "true", replace all instances of "A < B" with "false".
2043 if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
2044 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
2046 // If "A == B" is known true, or "A != B" is known false, then replace
2047 // A with B everywhere in the scope.
2048 if ((isKnownTrue && Cmp->getPredicate() == CmpInst::ICMP_EQ) ||
2049 (isKnownFalse && Cmp->getPredicate() == CmpInst::ICMP_NE))
2050 Worklist.push_back(std::make_pair(Op0, Op1));
2052 // Handle the floating point versions of equality comparisons too.
2053 if ((isKnownTrue && Cmp->getPredicate() == CmpInst::FCMP_OEQ) ||
2054 (isKnownFalse && Cmp->getPredicate() == CmpInst::FCMP_UNE)) {
2056 // Floating point -0.0 and 0.0 compare equal, so we can only
2057 // propagate values if we know that we have a constant and that
2058 // its value is non-zero.
2060 // FIXME: We should do this optimization if 'no signed zeros' is
2061 // applicable via an instruction-level fast-math-flag or some other
2062 // indicator that relaxed FP semantics are being used.
2064 if (isa<ConstantFP>(Op1) && !cast<ConstantFP>(Op1)->isZero())
2065 Worklist.push_back(std::make_pair(Op0, Op1));
2068 // If "A >= B" is known true, replace "A < B" with false everywhere.
2069 CmpInst::Predicate NotPred = Cmp->getInversePredicate();
2070 Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
2071 // Since we don't have the instruction "A < B" immediately to hand, work
2072 // out the value number that it would have and use that to find an
2073 // appropriate instruction (if any).
2074 uint32_t NextNum = VN.getNextUnusedValueNumber();
2075 uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
2076 // If the number we were assigned was brand new then there is no point in
2077 // looking for an instruction realizing it: there cannot be one!
2078 if (Num < NextNum) {
2079 Value *NotCmp = findLeader(Root.getEnd(), Num);
2080 if (NotCmp && isa<Instruction>(NotCmp)) {
2081 unsigned NumReplacements =
2083 ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
2084 : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
2086 Changed |= NumReplacements > 0;
2087 NumGVNEqProp += NumReplacements;
2090 // Ensure that any instruction in scope that gets the "A < B" value number
2091 // is replaced with false.
2092 // The leader table only tracks basic blocks, not edges. Only add to if we
2093 // have the simple case where the edge dominates the end.
2094 if (RootDominatesEnd)
2095 addToLeaderTable(Num, NotVal, Root.getEnd());
2104 /// When calculating availability, handle an instruction
2105 /// by inserting it into the appropriate sets
2106 bool GVN::processInstruction(Instruction *I) {
2107 // Ignore dbg info intrinsics.
2108 if (isa<DbgInfoIntrinsic>(I))
2111 // If the instruction can be easily simplified then do so now in preference
2112 // to value numbering it. Value numbering often exposes redundancies, for
2113 // example if it determines that %y is equal to %x then the instruction
2114 // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
2115 const DataLayout &DL = I->getModule()->getDataLayout();
2116 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
2117 bool Changed = false;
2118 if (!I->use_empty()) {
2119 I->replaceAllUsesWith(V);
2122 if (isInstructionTriviallyDead(I, TLI)) {
2123 markInstructionForDeletion(I);
2127 if (MD && V->getType()->getScalarType()->isPointerTy())
2128 MD->invalidateCachedPointerInfo(V);
2134 if (IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(I))
2135 if (IntrinsicI->getIntrinsicID() == Intrinsic::assume)
2136 return processAssumeIntrinsic(IntrinsicI);
2138 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
2139 if (processLoad(LI))
2142 unsigned Num = VN.lookupOrAdd(LI);
2143 addToLeaderTable(Num, LI, LI->getParent());
2147 // For conditional branches, we can perform simple conditional propagation on
2148 // the condition value itself.
2149 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
2150 if (!BI->isConditional())
2153 if (isa<Constant>(BI->getCondition()))
2154 return processFoldableCondBr(BI);
2156 Value *BranchCond = BI->getCondition();
2157 BasicBlock *TrueSucc = BI->getSuccessor(0);
2158 BasicBlock *FalseSucc = BI->getSuccessor(1);
2159 // Avoid multiple edges early.
2160 if (TrueSucc == FalseSucc)
2163 BasicBlock *Parent = BI->getParent();
2164 bool Changed = false;
2166 Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
2167 BasicBlockEdge TrueE(Parent, TrueSucc);
2168 Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
2170 Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
2171 BasicBlockEdge FalseE(Parent, FalseSucc);
2172 Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
2177 // For switches, propagate the case values into the case destinations.
2178 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2179 Value *SwitchCond = SI->getCondition();
2180 BasicBlock *Parent = SI->getParent();
2181 bool Changed = false;
2183 // Remember how many outgoing edges there are to every successor.
2184 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
2185 for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
2186 ++SwitchEdges[SI->getSuccessor(i)];
2188 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2190 BasicBlock *Dst = i.getCaseSuccessor();
2191 // If there is only a single edge, propagate the case value into it.
2192 if (SwitchEdges.lookup(Dst) == 1) {
2193 BasicBlockEdge E(Parent, Dst);
2194 Changed |= propagateEquality(SwitchCond, i.getCaseValue(), E, true);
2200 // Instructions with void type don't return a value, so there's
2201 // no point in trying to find redundancies in them.
2202 if (I->getType()->isVoidTy())
2205 uint32_t NextNum = VN.getNextUnusedValueNumber();
2206 unsigned Num = VN.lookupOrAdd(I);
2208 // Allocations are always uniquely numbered, so we can save time and memory
2209 // by fast failing them.
2210 if (isa<AllocaInst>(I) || isa<TerminatorInst>(I) || isa<PHINode>(I)) {
2211 addToLeaderTable(Num, I, I->getParent());
2215 // If the number we were assigned was a brand new VN, then we don't
2216 // need to do a lookup to see if the number already exists
2217 // somewhere in the domtree: it can't!
2218 if (Num >= NextNum) {
2219 addToLeaderTable(Num, I, I->getParent());
2223 // Perform fast-path value-number based elimination of values inherited from
2225 Value *Repl = findLeader(I->getParent(), Num);
2227 // Failure, just remember this instance for future use.
2228 addToLeaderTable(Num, I, I->getParent());
2230 } else if (Repl == I) {
2231 // If I was the result of a shortcut PRE, it might already be in the table
2232 // and the best replacement for itself. Nothing to do.
2237 patchAndReplaceAllUsesWith(I, Repl);
2238 if (MD && Repl->getType()->getScalarType()->isPointerTy())
2239 MD->invalidateCachedPointerInfo(Repl);
2240 markInstructionForDeletion(I);
2244 /// runOnFunction - This is the main transformation entry point for a function.
2245 bool GVN::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2246 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2247 MemoryDependenceResults *RunMD, LoopInfo *LI,
2248 OptimizationRemarkEmitter *RunORE) {
2253 VN.setAliasAnalysis(&RunAA);
2258 bool Changed = false;
2259 bool ShouldContinue = true;
2261 // Merge unconditional branches, allowing PRE to catch more
2262 // optimization opportunities.
2263 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
2264 BasicBlock *BB = &*FI++;
2266 bool removedBlock = MergeBlockIntoPredecessor(BB, DT, LI, MD);
2270 Changed |= removedBlock;
2273 unsigned Iteration = 0;
2274 while (ShouldContinue) {
2275 DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
2276 ShouldContinue = iterateOnFunction(F);
2277 Changed |= ShouldContinue;
2282 // Fabricate val-num for dead-code in order to suppress assertion in
2284 assignValNumForDeadCode();
2285 bool PREChanged = true;
2286 while (PREChanged) {
2287 PREChanged = performPRE(F);
2288 Changed |= PREChanged;
2292 // FIXME: Should perform GVN again after PRE does something. PRE can move
2293 // computations into blocks where they become fully redundant. Note that
2294 // we can't do this until PRE's critical edge splitting updates memdep.
2295 // Actually, when this happens, we should just fully integrate PRE into GVN.
2297 cleanupGlobalSets();
2298 // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2305 bool GVN::processBlock(BasicBlock *BB) {
2306 // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2307 // (and incrementing BI before processing an instruction).
2308 assert(InstrsToErase.empty() &&
2309 "We expect InstrsToErase to be empty across iterations");
2310 if (DeadBlocks.count(BB))
2313 // Clearing map before every BB because it can be used only for single BB.
2314 ReplaceWithConstMap.clear();
2315 bool ChangedFunction = false;
2317 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
2319 if (!ReplaceWithConstMap.empty())
2320 ChangedFunction |= replaceOperandsWithConsts(&*BI);
2321 ChangedFunction |= processInstruction(&*BI);
2323 if (InstrsToErase.empty()) {
2328 // If we need some instructions deleted, do it now.
2329 NumGVNInstr += InstrsToErase.size();
2331 // Avoid iterator invalidation.
2332 bool AtStart = BI == BB->begin();
2336 for (SmallVectorImpl<Instruction *>::iterator I = InstrsToErase.begin(),
2337 E = InstrsToErase.end(); I != E; ++I) {
2338 DEBUG(dbgs() << "GVN removed: " << **I << '\n');
2339 if (MD) MD->removeInstruction(*I);
2340 DEBUG(verifyRemoved(*I));
2341 (*I)->eraseFromParent();
2343 InstrsToErase.clear();
2351 return ChangedFunction;
2354 // Instantiate an expression in a predecessor that lacked it.
2355 bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2356 unsigned int ValNo) {
2357 // Because we are going top-down through the block, all value numbers
2358 // will be available in the predecessor by the time we need them. Any
2359 // that weren't originally present will have been instantiated earlier
2361 bool success = true;
2362 for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
2363 Value *Op = Instr->getOperand(i);
2364 if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
2366 // This could be a newly inserted instruction, in which case, we won't
2367 // find a value number, and should give up before we hurt ourselves.
2368 // FIXME: Rewrite the infrastructure to let it easier to value number
2369 // and process newly inserted instructions.
2370 if (!VN.exists(Op)) {
2374 if (Value *V = findLeader(Pred, VN.lookup(Op))) {
2375 Instr->setOperand(i, V);
2382 // Fail out if we encounter an operand that is not available in
2383 // the PRE predecessor. This is typically because of loads which
2384 // are not value numbered precisely.
2388 Instr->insertBefore(Pred->getTerminator());
2389 Instr->setName(Instr->getName() + ".pre");
2390 Instr->setDebugLoc(Instr->getDebugLoc());
2391 VN.add(Instr, ValNo);
2393 // Update the availability map to include the new instruction.
2394 addToLeaderTable(ValNo, Instr, Pred);
2398 bool GVN::performScalarPRE(Instruction *CurInst) {
2399 if (isa<AllocaInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
2400 isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2401 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2402 isa<DbgInfoIntrinsic>(CurInst))
2405 // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2406 // sinking the compare again, and it would force the code generator to
2407 // move the i1 from processor flags or predicate registers into a general
2408 // purpose register.
2409 if (isa<CmpInst>(CurInst))
2412 // We don't currently value number ANY inline asm calls.
2413 if (CallInst *CallI = dyn_cast<CallInst>(CurInst))
2414 if (CallI->isInlineAsm())
2417 uint32_t ValNo = VN.lookup(CurInst);
2419 // Look for the predecessors for PRE opportunities. We're
2420 // only trying to solve the basic diamond case, where
2421 // a value is computed in the successor and one predecessor,
2422 // but not the other. We also explicitly disallow cases
2423 // where the successor is its own predecessor, because they're
2424 // more complicated to get right.
2425 unsigned NumWith = 0;
2426 unsigned NumWithout = 0;
2427 BasicBlock *PREPred = nullptr;
2428 BasicBlock *CurrentBlock = CurInst->getParent();
2430 SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap;
2431 for (BasicBlock *P : predecessors(CurrentBlock)) {
2432 // We're not interested in PRE where the block is its
2433 // own predecessor, or in blocks with predecessors
2434 // that are not reachable.
2435 if (P == CurrentBlock) {
2438 } else if (!DT->isReachableFromEntry(P)) {
2443 Value *predV = findLeader(P, ValNo);
2445 predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
2448 } else if (predV == CurInst) {
2449 /* CurInst dominates this predecessor. */
2453 predMap.push_back(std::make_pair(predV, P));
2458 // Don't do PRE when it might increase code size, i.e. when
2459 // we would need to insert instructions in more than one pred.
2460 if (NumWithout > 1 || NumWith == 0)
2463 // We may have a case where all predecessors have the instruction,
2464 // and we just need to insert a phi node. Otherwise, perform
2466 Instruction *PREInstr = nullptr;
2468 if (NumWithout != 0) {
2469 // Don't do PRE across indirect branch.
2470 if (isa<IndirectBrInst>(PREPred->getTerminator()))
2473 // We can't do PRE safely on a critical edge, so instead we schedule
2474 // the edge to be split and perform the PRE the next time we iterate
2476 unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
2477 if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
2478 toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
2481 // We need to insert somewhere, so let's give it a shot
2482 PREInstr = CurInst->clone();
2483 if (!performScalarPREInsertion(PREInstr, PREPred, ValNo)) {
2484 // If we failed insertion, make sure we remove the instruction.
2485 DEBUG(verifyRemoved(PREInstr));
2491 // Either we should have filled in the PRE instruction, or we should
2492 // not have needed insertions.
2493 assert (PREInstr != nullptr || NumWithout == 0);
2497 // Create a PHI to make the value available in this block.
2499 PHINode::Create(CurInst->getType(), predMap.size(),
2500 CurInst->getName() + ".pre-phi", &CurrentBlock->front());
2501 for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
2502 if (Value *V = predMap[i].first)
2503 Phi->addIncoming(V, predMap[i].second);
2505 Phi->addIncoming(PREInstr, PREPred);
2509 addToLeaderTable(ValNo, Phi, CurrentBlock);
2510 Phi->setDebugLoc(CurInst->getDebugLoc());
2511 CurInst->replaceAllUsesWith(Phi);
2512 if (MD && Phi->getType()->getScalarType()->isPointerTy())
2513 MD->invalidateCachedPointerInfo(Phi);
2515 removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
2517 DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
2519 MD->removeInstruction(CurInst);
2520 DEBUG(verifyRemoved(CurInst));
2521 CurInst->eraseFromParent();
2527 /// Perform a purely local form of PRE that looks for diamond
2528 /// control flow patterns and attempts to perform simple PRE at the join point.
2529 bool GVN::performPRE(Function &F) {
2530 bool Changed = false;
2531 for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
2532 // Nothing to PRE in the entry block.
2533 if (CurrentBlock == &F.getEntryBlock())
2536 // Don't perform PRE on an EH pad.
2537 if (CurrentBlock->isEHPad())
2540 for (BasicBlock::iterator BI = CurrentBlock->begin(),
2541 BE = CurrentBlock->end();
2543 Instruction *CurInst = &*BI++;
2544 Changed |= performScalarPRE(CurInst);
2548 if (splitCriticalEdges())
2554 /// Split the critical edge connecting the given two blocks, and return
2555 /// the block inserted to the critical edge.
2556 BasicBlock *GVN::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
2558 SplitCriticalEdge(Pred, Succ, CriticalEdgeSplittingOptions(DT));
2560 MD->invalidateCachedPredecessors();
2564 /// Split critical edges found during the previous
2565 /// iteration that may enable further optimization.
2566 bool GVN::splitCriticalEdges() {
2567 if (toSplit.empty())
2570 std::pair<TerminatorInst*, unsigned> Edge = toSplit.pop_back_val();
2571 SplitCriticalEdge(Edge.first, Edge.second,
2572 CriticalEdgeSplittingOptions(DT));
2573 } while (!toSplit.empty());
2574 if (MD) MD->invalidateCachedPredecessors();
2578 /// Executes one iteration of GVN
2579 bool GVN::iterateOnFunction(Function &F) {
2580 cleanupGlobalSets();
2582 // Top-down walk of the dominator tree
2583 bool Changed = false;
2584 // Save the blocks this function have before transformation begins. GVN may
2585 // split critical edge, and hence may invalidate the RPO/DT iterator.
2587 std::vector<BasicBlock *> BBVect;
2588 BBVect.reserve(256);
2589 // Needed for value numbering with phi construction to work.
2590 ReversePostOrderTraversal<Function *> RPOT(&F);
2591 for (ReversePostOrderTraversal<Function *>::rpo_iterator RI = RPOT.begin(),
2594 BBVect.push_back(*RI);
2596 for (std::vector<BasicBlock *>::iterator I = BBVect.begin(), E = BBVect.end();
2598 Changed |= processBlock(*I);
2603 void GVN::cleanupGlobalSets() {
2605 LeaderTable.clear();
2606 TableAllocator.Reset();
2609 /// Verify that the specified instruction does not occur in our
2610 /// internal data structures.
2611 void GVN::verifyRemoved(const Instruction *Inst) const {
2612 VN.verifyRemoved(Inst);
2614 // Walk through the value number scope to make sure the instruction isn't
2615 // ferreted away in it.
2616 for (DenseMap<uint32_t, LeaderTableEntry>::const_iterator
2617 I = LeaderTable.begin(), E = LeaderTable.end(); I != E; ++I) {
2618 const LeaderTableEntry *Node = &I->second;
2619 assert(Node->Val != Inst && "Inst still in value numbering scope!");
2621 while (Node->Next) {
2623 assert(Node->Val != Inst && "Inst still in value numbering scope!");
2628 /// BB is declared dead, which implied other blocks become dead as well. This
2629 /// function is to add all these blocks to "DeadBlocks". For the dead blocks'
2630 /// live successors, update their phi nodes by replacing the operands
2631 /// corresponding to dead blocks with UndefVal.
2632 void GVN::addDeadBlock(BasicBlock *BB) {
2633 SmallVector<BasicBlock *, 4> NewDead;
2634 SmallSetVector<BasicBlock *, 4> DF;
2636 NewDead.push_back(BB);
2637 while (!NewDead.empty()) {
2638 BasicBlock *D = NewDead.pop_back_val();
2639 if (DeadBlocks.count(D))
2642 // All blocks dominated by D are dead.
2643 SmallVector<BasicBlock *, 8> Dom;
2644 DT->getDescendants(D, Dom);
2645 DeadBlocks.insert(Dom.begin(), Dom.end());
2647 // Figure out the dominance-frontier(D).
2648 for (BasicBlock *B : Dom) {
2649 for (BasicBlock *S : successors(B)) {
2650 if (DeadBlocks.count(S))
2653 bool AllPredDead = true;
2654 for (BasicBlock *P : predecessors(S))
2655 if (!DeadBlocks.count(P)) {
2656 AllPredDead = false;
2661 // S could be proved dead later on. That is why we don't update phi
2662 // operands at this moment.
2665 // While S is not dominated by D, it is dead by now. This could take
2666 // place if S already have a dead predecessor before D is declared
2668 NewDead.push_back(S);
2674 // For the dead blocks' live successors, update their phi nodes by replacing
2675 // the operands corresponding to dead blocks with UndefVal.
2676 for(SmallSetVector<BasicBlock *, 4>::iterator I = DF.begin(), E = DF.end();
2679 if (DeadBlocks.count(B))
2682 SmallVector<BasicBlock *, 4> Preds(pred_begin(B), pred_end(B));
2683 for (BasicBlock *P : Preds) {
2684 if (!DeadBlocks.count(P))
2687 if (isCriticalEdge(P->getTerminator(), GetSuccessorNumber(P, B))) {
2688 if (BasicBlock *S = splitCriticalEdges(P, B))
2689 DeadBlocks.insert(P = S);
2692 for (BasicBlock::iterator II = B->begin(); isa<PHINode>(II); ++II) {
2693 PHINode &Phi = cast<PHINode>(*II);
2694 Phi.setIncomingValue(Phi.getBasicBlockIndex(P),
2695 UndefValue::get(Phi.getType()));
2701 // If the given branch is recognized as a foldable branch (i.e. conditional
2702 // branch with constant condition), it will perform following analyses and
2704 // 1) If the dead out-coming edge is a critical-edge, split it. Let
2705 // R be the target of the dead out-coming edge.
2706 // 1) Identify the set of dead blocks implied by the branch's dead outcoming
2707 // edge. The result of this step will be {X| X is dominated by R}
2708 // 2) Identify those blocks which haves at least one dead predecessor. The
2709 // result of this step will be dominance-frontier(R).
2710 // 3) Update the PHIs in DF(R) by replacing the operands corresponding to
2711 // dead blocks with "UndefVal" in an hope these PHIs will optimized away.
2713 // Return true iff *NEW* dead code are found.
2714 bool GVN::processFoldableCondBr(BranchInst *BI) {
2715 if (!BI || BI->isUnconditional())
2718 // If a branch has two identical successors, we cannot declare either dead.
2719 if (BI->getSuccessor(0) == BI->getSuccessor(1))
2722 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
2726 BasicBlock *DeadRoot =
2727 Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
2728 if (DeadBlocks.count(DeadRoot))
2731 if (!DeadRoot->getSinglePredecessor())
2732 DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
2734 addDeadBlock(DeadRoot);
2738 // performPRE() will trigger assert if it comes across an instruction without
2739 // associated val-num. As it normally has far more live instructions than dead
2740 // instructions, it makes more sense just to "fabricate" a val-number for the
2741 // dead code than checking if instruction involved is dead or not.
2742 void GVN::assignValNumForDeadCode() {
2743 for (BasicBlock *BB : DeadBlocks) {
2744 for (Instruction &Inst : *BB) {
2745 unsigned ValNum = VN.lookupOrAdd(&Inst);
2746 addToLeaderTable(ValNum, &Inst, BB);
2751 class llvm::gvn::GVNLegacyPass : public FunctionPass {
2753 static char ID; // Pass identification, replacement for typeid
2754 explicit GVNLegacyPass(bool NoLoads = false)
2755 : FunctionPass(ID), NoLoads(NoLoads) {
2756 initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry());
2759 bool runOnFunction(Function &F) override {
2760 if (skipFunction(F))
2763 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
2765 return Impl.runImpl(
2766 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
2767 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
2768 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
2769 getAnalysis<AAResultsWrapperPass>().getAAResults(),
2771 : &getAnalysis<MemoryDependenceWrapperPass>().getMemDep(),
2772 LIWP ? &LIWP->getLoopInfo() : nullptr,
2773 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE());
2776 void getAnalysisUsage(AnalysisUsage &AU) const override {
2777 AU.addRequired<AssumptionCacheTracker>();
2778 AU.addRequired<DominatorTreeWrapperPass>();
2779 AU.addRequired<TargetLibraryInfoWrapperPass>();
2781 AU.addRequired<MemoryDependenceWrapperPass>();
2782 AU.addRequired<AAResultsWrapperPass>();
2784 AU.addPreserved<DominatorTreeWrapperPass>();
2785 AU.addPreserved<GlobalsAAWrapperPass>();
2786 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
2794 char GVNLegacyPass::ID = 0;
2796 // The public interface to this file...
2797 FunctionPass *llvm::createGVNPass(bool NoLoads) {
2798 return new GVNLegacyPass(NoLoads);
2801 INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
2802 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
2803 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
2804 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2805 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2806 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2807 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
2808 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
2809 INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)