1 //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass performs global value numbering to eliminate fully redundant
10 // instructions. It also performs simple dead load elimination.
12 // Note that this pass does the value numbering itself; it does not use the
13 // ValueNumbering analysis passes.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Scalar/GVN.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/AliasAnalysis.h"
30 #include "llvm/Analysis/AssumptionCache.h"
31 #include "llvm/Analysis/CFG.h"
32 #include "llvm/Analysis/DomTreeUpdater.h"
33 #include "llvm/Analysis/GlobalsModRef.h"
34 #include "llvm/Analysis/InstructionSimplify.h"
35 #include "llvm/Analysis/LoopInfo.h"
36 #include "llvm/Analysis/MemoryBuiltins.h"
37 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
38 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
39 #include "llvm/Analysis/PHITransAddr.h"
40 #include "llvm/Analysis/TargetLibraryInfo.h"
41 #include "llvm/Analysis/ValueTracking.h"
42 #include "llvm/Config/llvm-config.h"
43 #include "llvm/IR/Attributes.h"
44 #include "llvm/IR/BasicBlock.h"
45 #include "llvm/IR/CallSite.h"
46 #include "llvm/IR/Constant.h"
47 #include "llvm/IR/Constants.h"
48 #include "llvm/IR/DataLayout.h"
49 #include "llvm/IR/DebugInfoMetadata.h"
50 #include "llvm/IR/DebugLoc.h"
51 #include "llvm/IR/Dominators.h"
52 #include "llvm/IR/Function.h"
53 #include "llvm/IR/InstrTypes.h"
54 #include "llvm/IR/Instruction.h"
55 #include "llvm/IR/Instructions.h"
56 #include "llvm/IR/IntrinsicInst.h"
57 #include "llvm/IR/Intrinsics.h"
58 #include "llvm/IR/LLVMContext.h"
59 #include "llvm/IR/Metadata.h"
60 #include "llvm/IR/Module.h"
61 #include "llvm/IR/Operator.h"
62 #include "llvm/IR/PassManager.h"
63 #include "llvm/IR/PatternMatch.h"
64 #include "llvm/IR/Type.h"
65 #include "llvm/IR/Use.h"
66 #include "llvm/IR/Value.h"
67 #include "llvm/InitializePasses.h"
68 #include "llvm/Pass.h"
69 #include "llvm/Support/Casting.h"
70 #include "llvm/Support/CommandLine.h"
71 #include "llvm/Support/Compiler.h"
72 #include "llvm/Support/Debug.h"
73 #include "llvm/Support/raw_ostream.h"
74 #include "llvm/Transforms/Utils.h"
75 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
76 #include "llvm/Transforms/Utils/Local.h"
77 #include "llvm/Transforms/Utils/SSAUpdater.h"
78 #include "llvm/Transforms/Utils/VNCoercion.h"
86 using namespace llvm::gvn;
87 using namespace llvm::VNCoercion;
88 using namespace PatternMatch;
90 #define DEBUG_TYPE "gvn"
92 STATISTIC(NumGVNInstr, "Number of instructions deleted");
93 STATISTIC(NumGVNLoad, "Number of loads deleted");
94 STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
95 STATISTIC(NumGVNBlocks, "Number of blocks merged");
96 STATISTIC(NumGVNSimpl, "Number of instructions simplified");
97 STATISTIC(NumGVNEqProp, "Number of equalities propagated");
98 STATISTIC(NumPRELoad, "Number of loads PRE'd");
100 static cl::opt<bool> EnablePRE("enable-pre",
101 cl::init(true), cl::Hidden);
102 static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
103 static cl::opt<bool> EnableMemDep("enable-gvn-memdep", cl::init(true));
105 // Maximum allowed recursion depth.
106 static cl::opt<uint32_t>
107 MaxRecurseDepth("gvn-max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore,
108 cl::desc("Max recurse depth in GVN (default = 1000)"));
110 static cl::opt<uint32_t> MaxNumDeps(
111 "gvn-max-num-deps", cl::Hidden, cl::init(100), cl::ZeroOrMore,
112 cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
114 struct llvm::GVN::Expression {
116 Type *type = nullptr;
117 bool commutative = false;
118 SmallVector<uint32_t, 4> varargs;
120 Expression(uint32_t o = ~2U) : opcode(o) {}
122 bool operator==(const Expression &other) const {
123 if (opcode != other.opcode)
125 if (opcode == ~0U || opcode == ~1U)
127 if (type != other.type)
129 if (varargs != other.varargs)
134 friend hash_code hash_value(const Expression &Value) {
136 Value.opcode, Value.type,
137 hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
143 template <> struct DenseMapInfo<GVN::Expression> {
144 static inline GVN::Expression getEmptyKey() { return ~0U; }
145 static inline GVN::Expression getTombstoneKey() { return ~1U; }
147 static unsigned getHashValue(const GVN::Expression &e) {
148 using llvm::hash_value;
150 return static_cast<unsigned>(hash_value(e));
153 static bool isEqual(const GVN::Expression &LHS, const GVN::Expression &RHS) {
158 } // end namespace llvm
160 /// Represents a particular available value that we know how to materialize.
161 /// Materialization of an AvailableValue never fails. An AvailableValue is
162 /// implicitly associated with a rematerialization point which is the
163 /// location of the instruction from which it was formed.
164 struct llvm::gvn::AvailableValue {
166 SimpleVal, // A simple offsetted value that is accessed.
167 LoadVal, // A value produced by a load.
168 MemIntrin, // A memory intrinsic which is loaded from.
169 UndefVal // A UndefValue representing a value from dead block (which
170 // is not yet physically removed from the CFG).
173 /// V - The value that is live out of the block.
174 PointerIntPair<Value *, 2, ValType> Val;
176 /// Offset - The byte offset in Val that is interesting for the load query.
179 static AvailableValue get(Value *V, unsigned Offset = 0) {
181 Res.Val.setPointer(V);
182 Res.Val.setInt(SimpleVal);
187 static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
189 Res.Val.setPointer(MI);
190 Res.Val.setInt(MemIntrin);
195 static AvailableValue getLoad(LoadInst *LI, unsigned Offset = 0) {
197 Res.Val.setPointer(LI);
198 Res.Val.setInt(LoadVal);
203 static AvailableValue getUndef() {
205 Res.Val.setPointer(nullptr);
206 Res.Val.setInt(UndefVal);
211 bool isSimpleValue() const { return Val.getInt() == SimpleVal; }
212 bool isCoercedLoadValue() const { return Val.getInt() == LoadVal; }
213 bool isMemIntrinValue() const { return Val.getInt() == MemIntrin; }
214 bool isUndefValue() const { return Val.getInt() == UndefVal; }
216 Value *getSimpleValue() const {
217 assert(isSimpleValue() && "Wrong accessor");
218 return Val.getPointer();
221 LoadInst *getCoercedLoadValue() const {
222 assert(isCoercedLoadValue() && "Wrong accessor");
223 return cast<LoadInst>(Val.getPointer());
226 MemIntrinsic *getMemIntrinValue() const {
227 assert(isMemIntrinValue() && "Wrong accessor");
228 return cast<MemIntrinsic>(Val.getPointer());
231 /// Emit code at the specified insertion point to adjust the value defined
232 /// here to the specified type. This handles various coercion cases.
233 Value *MaterializeAdjustedValue(LoadInst *LI, Instruction *InsertPt,
237 /// Represents an AvailableValue which can be rematerialized at the end of
238 /// the associated BasicBlock.
239 struct llvm::gvn::AvailableValueInBlock {
240 /// BB - The basic block in question.
241 BasicBlock *BB = nullptr;
243 /// AV - The actual available value
246 static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) {
247 AvailableValueInBlock Res;
249 Res.AV = std::move(AV);
253 static AvailableValueInBlock get(BasicBlock *BB, Value *V,
254 unsigned Offset = 0) {
255 return get(BB, AvailableValue::get(V, Offset));
258 static AvailableValueInBlock getUndef(BasicBlock *BB) {
259 return get(BB, AvailableValue::getUndef());
262 /// Emit code at the end of this block to adjust the value defined here to
263 /// the specified type. This handles various coercion cases.
264 Value *MaterializeAdjustedValue(LoadInst *LI, GVN &gvn) const {
265 return AV.MaterializeAdjustedValue(LI, BB->getTerminator(), gvn);
269 //===----------------------------------------------------------------------===//
270 // ValueTable Internal Functions
271 //===----------------------------------------------------------------------===//
273 GVN::Expression GVN::ValueTable::createExpr(Instruction *I) {
275 e.type = I->getType();
276 e.opcode = I->getOpcode();
277 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
279 e.varargs.push_back(lookupOrAdd(*OI));
280 if (I->isCommutative()) {
281 // Ensure that commutative instructions that only differ by a permutation
282 // of their operands get the same value number by sorting the operand value
283 // numbers. Since all commutative instructions have two operands it is more
284 // efficient to sort by hand rather than using, say, std::sort.
285 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
286 if (e.varargs[0] > e.varargs[1])
287 std::swap(e.varargs[0], e.varargs[1]);
288 e.commutative = true;
291 if (CmpInst *C = dyn_cast<CmpInst>(I)) {
292 // Sort the operand value numbers so x<y and y>x get the same value number.
293 CmpInst::Predicate Predicate = C->getPredicate();
294 if (e.varargs[0] > e.varargs[1]) {
295 std::swap(e.varargs[0], e.varargs[1]);
296 Predicate = CmpInst::getSwappedPredicate(Predicate);
298 e.opcode = (C->getOpcode() << 8) | Predicate;
299 e.commutative = true;
300 } else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) {
301 for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
303 e.varargs.push_back(*II);
309 GVN::Expression GVN::ValueTable::createCmpExpr(unsigned Opcode,
310 CmpInst::Predicate Predicate,
311 Value *LHS, Value *RHS) {
312 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
313 "Not a comparison!");
315 e.type = CmpInst::makeCmpResultType(LHS->getType());
316 e.varargs.push_back(lookupOrAdd(LHS));
317 e.varargs.push_back(lookupOrAdd(RHS));
319 // Sort the operand value numbers so x<y and y>x get the same value number.
320 if (e.varargs[0] > e.varargs[1]) {
321 std::swap(e.varargs[0], e.varargs[1]);
322 Predicate = CmpInst::getSwappedPredicate(Predicate);
324 e.opcode = (Opcode << 8) | Predicate;
325 e.commutative = true;
329 GVN::Expression GVN::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
330 assert(EI && "Not an ExtractValueInst?");
332 e.type = EI->getType();
335 WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
336 if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
337 // EI is an extract from one of our with.overflow intrinsics. Synthesize
338 // a semantically equivalent expression instead of an extract value
340 e.opcode = WO->getBinaryOp();
341 e.varargs.push_back(lookupOrAdd(WO->getLHS()));
342 e.varargs.push_back(lookupOrAdd(WO->getRHS()));
346 // Not a recognised intrinsic. Fall back to producing an extract value
348 e.opcode = EI->getOpcode();
349 for (Instruction::op_iterator OI = EI->op_begin(), OE = EI->op_end();
351 e.varargs.push_back(lookupOrAdd(*OI));
353 for (ExtractValueInst::idx_iterator II = EI->idx_begin(), IE = EI->idx_end();
355 e.varargs.push_back(*II);
360 //===----------------------------------------------------------------------===//
361 // ValueTable External Functions
362 //===----------------------------------------------------------------------===//
364 GVN::ValueTable::ValueTable() = default;
365 GVN::ValueTable::ValueTable(const ValueTable &) = default;
366 GVN::ValueTable::ValueTable(ValueTable &&) = default;
367 GVN::ValueTable::~ValueTable() = default;
368 GVN::ValueTable &GVN::ValueTable::operator=(const GVN::ValueTable &Arg) = default;
370 /// add - Insert a value into the table with a specified value number.
371 void GVN::ValueTable::add(Value *V, uint32_t num) {
372 valueNumbering.insert(std::make_pair(V, num));
373 if (PHINode *PN = dyn_cast<PHINode>(V))
374 NumberingPhi[num] = PN;
377 uint32_t GVN::ValueTable::lookupOrAddCall(CallInst *C) {
378 if (AA->doesNotAccessMemory(C)) {
379 Expression exp = createExpr(C);
380 uint32_t e = assignExpNewValueNum(exp).first;
381 valueNumbering[C] = e;
383 } else if (MD && AA->onlyReadsMemory(C)) {
384 Expression exp = createExpr(C);
385 auto ValNum = assignExpNewValueNum(exp);
387 valueNumbering[C] = ValNum.first;
391 MemDepResult local_dep = MD->getDependency(C);
393 if (!local_dep.isDef() && !local_dep.isNonLocal()) {
394 valueNumbering[C] = nextValueNumber;
395 return nextValueNumber++;
398 if (local_dep.isDef()) {
399 CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
401 if (local_cdep->getNumArgOperands() != C->getNumArgOperands()) {
402 valueNumbering[C] = nextValueNumber;
403 return nextValueNumber++;
406 for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
407 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
408 uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
410 valueNumbering[C] = nextValueNumber;
411 return nextValueNumber++;
415 uint32_t v = lookupOrAdd(local_cdep);
416 valueNumbering[C] = v;
421 const MemoryDependenceResults::NonLocalDepInfo &deps =
422 MD->getNonLocalCallDependency(C);
423 // FIXME: Move the checking logic to MemDep!
424 CallInst* cdep = nullptr;
426 // Check to see if we have a single dominating call instruction that is
428 for (unsigned i = 0, e = deps.size(); i != e; ++i) {
429 const NonLocalDepEntry *I = &deps[i];
430 if (I->getResult().isNonLocal())
433 // We don't handle non-definitions. If we already have a call, reject
434 // instruction dependencies.
435 if (!I->getResult().isDef() || cdep != nullptr) {
440 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst());
441 // FIXME: All duplicated with non-local case.
442 if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){
443 cdep = NonLocalDepCall;
452 valueNumbering[C] = nextValueNumber;
453 return nextValueNumber++;
456 if (cdep->getNumArgOperands() != C->getNumArgOperands()) {
457 valueNumbering[C] = nextValueNumber;
458 return nextValueNumber++;
460 for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
461 uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
462 uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
464 valueNumbering[C] = nextValueNumber;
465 return nextValueNumber++;
469 uint32_t v = lookupOrAdd(cdep);
470 valueNumbering[C] = v;
473 valueNumbering[C] = nextValueNumber;
474 return nextValueNumber++;
478 /// Returns true if a value number exists for the specified value.
479 bool GVN::ValueTable::exists(Value *V) const { return valueNumbering.count(V) != 0; }
481 /// lookup_or_add - Returns the value number for the specified value, assigning
482 /// it a new number if it did not have one before.
483 uint32_t GVN::ValueTable::lookupOrAdd(Value *V) {
484 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
485 if (VI != valueNumbering.end())
488 if (!isa<Instruction>(V)) {
489 valueNumbering[V] = nextValueNumber;
490 return nextValueNumber++;
493 Instruction* I = cast<Instruction>(V);
495 switch (I->getOpcode()) {
496 case Instruction::Call:
497 return lookupOrAddCall(cast<CallInst>(I));
498 case Instruction::FNeg:
499 case Instruction::Add:
500 case Instruction::FAdd:
501 case Instruction::Sub:
502 case Instruction::FSub:
503 case Instruction::Mul:
504 case Instruction::FMul:
505 case Instruction::UDiv:
506 case Instruction::SDiv:
507 case Instruction::FDiv:
508 case Instruction::URem:
509 case Instruction::SRem:
510 case Instruction::FRem:
511 case Instruction::Shl:
512 case Instruction::LShr:
513 case Instruction::AShr:
514 case Instruction::And:
515 case Instruction::Or:
516 case Instruction::Xor:
517 case Instruction::ICmp:
518 case Instruction::FCmp:
519 case Instruction::Trunc:
520 case Instruction::ZExt:
521 case Instruction::SExt:
522 case Instruction::FPToUI:
523 case Instruction::FPToSI:
524 case Instruction::UIToFP:
525 case Instruction::SIToFP:
526 case Instruction::FPTrunc:
527 case Instruction::FPExt:
528 case Instruction::PtrToInt:
529 case Instruction::IntToPtr:
530 case Instruction::AddrSpaceCast:
531 case Instruction::BitCast:
532 case Instruction::Select:
533 case Instruction::ExtractElement:
534 case Instruction::InsertElement:
535 case Instruction::ShuffleVector:
536 case Instruction::InsertValue:
537 case Instruction::GetElementPtr:
540 case Instruction::ExtractValue:
541 exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
543 case Instruction::PHI:
544 valueNumbering[V] = nextValueNumber;
545 NumberingPhi[nextValueNumber] = cast<PHINode>(V);
546 return nextValueNumber++;
548 valueNumbering[V] = nextValueNumber;
549 return nextValueNumber++;
552 uint32_t e = assignExpNewValueNum(exp).first;
553 valueNumbering[V] = e;
557 /// Returns the value number of the specified value. Fails if
558 /// the value has not yet been numbered.
559 uint32_t GVN::ValueTable::lookup(Value *V, bool Verify) const {
560 DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
562 assert(VI != valueNumbering.end() && "Value not numbered?");
565 return (VI != valueNumbering.end()) ? VI->second : 0;
568 /// Returns the value number of the given comparison,
569 /// assigning it a new number if it did not have one before. Useful when
570 /// we deduced the result of a comparison, but don't immediately have an
571 /// instruction realizing that comparison to hand.
572 uint32_t GVN::ValueTable::lookupOrAddCmp(unsigned Opcode,
573 CmpInst::Predicate Predicate,
574 Value *LHS, Value *RHS) {
575 Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
576 return assignExpNewValueNum(exp).first;
579 /// Remove all entries from the ValueTable.
580 void GVN::ValueTable::clear() {
581 valueNumbering.clear();
582 expressionNumbering.clear();
583 NumberingPhi.clear();
584 PhiTranslateTable.clear();
591 /// Remove a value from the value numbering.
592 void GVN::ValueTable::erase(Value *V) {
593 uint32_t Num = valueNumbering.lookup(V);
594 valueNumbering.erase(V);
595 // If V is PHINode, V <--> value number is an one-to-one mapping.
597 NumberingPhi.erase(Num);
600 /// verifyRemoved - Verify that the value is removed from all internal data
602 void GVN::ValueTable::verifyRemoved(const Value *V) const {
603 for (DenseMap<Value*, uint32_t>::const_iterator
604 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
605 assert(I->first != V && "Inst still occurs in value numbering map!");
609 //===----------------------------------------------------------------------===//
611 //===----------------------------------------------------------------------===//
613 PreservedAnalyses GVN::run(Function &F, FunctionAnalysisManager &AM) {
614 // FIXME: The order of evaluation of these 'getResult' calls is very
615 // significant! Re-ordering these variables will cause GVN when run alone to
616 // be less effective! We should fix memdep and basic-aa to not exhibit this
617 // behavior, but until then don't change the order here.
618 auto &AC = AM.getResult<AssumptionAnalysis>(F);
619 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
620 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
621 auto &AA = AM.getResult<AAManager>(F);
622 auto &MemDep = AM.getResult<MemoryDependenceAnalysis>(F);
623 auto *LI = AM.getCachedResult<LoopAnalysis>(F);
624 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
625 bool Changed = runImpl(F, AC, DT, TLI, AA, &MemDep, LI, &ORE);
627 return PreservedAnalyses::all();
628 PreservedAnalyses PA;
629 PA.preserve<DominatorTreeAnalysis>();
630 PA.preserve<GlobalsAA>();
631 PA.preserve<TargetLibraryAnalysis>();
633 PA.preserve<LoopAnalysis>();
637 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
638 LLVM_DUMP_METHOD void GVN::dump(DenseMap<uint32_t, Value*>& d) const {
640 for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
641 E = d.end(); I != E; ++I) {
642 errs() << I->first << "\n";
649 /// Return true if we can prove that the value
650 /// we're analyzing is fully available in the specified block. As we go, keep
651 /// track of which blocks we know are fully alive in FullyAvailableBlocks. This
652 /// map is actually a tri-state map with the following values:
653 /// 0) we know the block *is not* fully available.
654 /// 1) we know the block *is* fully available.
655 /// 2) we do not know whether the block is fully available or not, but we are
656 /// currently speculating that it will be.
657 /// 3) we are speculating for this block and have used that to speculate for
659 static bool IsValueFullyAvailableInBlock(BasicBlock *BB,
660 DenseMap<BasicBlock*, char> &FullyAvailableBlocks,
661 uint32_t RecurseDepth) {
662 if (RecurseDepth > MaxRecurseDepth)
665 // Optimistically assume that the block is fully available and check to see
666 // if we already know about this block in one lookup.
667 std::pair<DenseMap<BasicBlock*, char>::iterator, bool> IV =
668 FullyAvailableBlocks.insert(std::make_pair(BB, 2));
670 // If the entry already existed for this block, return the precomputed value.
672 // If this is a speculative "available" value, mark it as being used for
673 // speculation of other blocks.
674 if (IV.first->second == 2)
675 IV.first->second = 3;
676 return IV.first->second != 0;
679 // Otherwise, see if it is fully available in all predecessors.
680 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
682 // If this block has no predecessors, it isn't live-in here.
684 goto SpeculationFailure;
686 for (; PI != PE; ++PI)
687 // If the value isn't fully available in one of our predecessors, then it
688 // isn't fully available in this block either. Undo our previous
689 // optimistic assumption and bail out.
690 if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks,RecurseDepth+1))
691 goto SpeculationFailure;
695 // If we get here, we found out that this is not, after
696 // all, a fully-available block. We have a problem if we speculated on this and
697 // used the speculation to mark other blocks as available.
699 char &BBVal = FullyAvailableBlocks[BB];
701 // If we didn't speculate on this, just return with it set to false.
707 // If we did speculate on this value, we could have blocks set to 1 that are
708 // incorrect. Walk the (transitive) successors of this block and mark them as
710 SmallVector<BasicBlock*, 32> BBWorklist;
711 BBWorklist.push_back(BB);
714 BasicBlock *Entry = BBWorklist.pop_back_val();
715 // Note that this sets blocks to 0 (unavailable) if they happen to not
716 // already be in FullyAvailableBlocks. This is safe.
717 char &EntryVal = FullyAvailableBlocks[Entry];
718 if (EntryVal == 0) continue; // Already unavailable.
720 // Mark as unavailable.
723 BBWorklist.append(succ_begin(Entry), succ_end(Entry));
724 } while (!BBWorklist.empty());
729 /// Given a set of loads specified by ValuesPerBlock,
730 /// construct SSA form, allowing us to eliminate LI. This returns the value
731 /// that should be used at LI's definition site.
732 static Value *ConstructSSAForLoadSet(LoadInst *LI,
733 SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
735 // Check for the fully redundant, dominating load case. In this case, we can
736 // just use the dominating value directly.
737 if (ValuesPerBlock.size() == 1 &&
738 gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
740 assert(!ValuesPerBlock[0].AV.isUndefValue() &&
741 "Dead BB dominate this block");
742 return ValuesPerBlock[0].MaterializeAdjustedValue(LI, gvn);
745 // Otherwise, we have to construct SSA form.
746 SmallVector<PHINode*, 8> NewPHIs;
747 SSAUpdater SSAUpdate(&NewPHIs);
748 SSAUpdate.Initialize(LI->getType(), LI->getName());
750 for (const AvailableValueInBlock &AV : ValuesPerBlock) {
751 BasicBlock *BB = AV.BB;
753 if (SSAUpdate.HasValueForBlock(BB))
756 // If the value is the load that we will be eliminating, and the block it's
757 // available in is the block that the load is in, then don't add it as
758 // SSAUpdater will resolve the value to the relevant phi which may let it
759 // avoid phi construction entirely if there's actually only one value.
760 if (BB == LI->getParent() &&
761 ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == LI) ||
762 (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == LI)))
765 SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LI, gvn));
768 // Perform PHI construction.
769 return SSAUpdate.GetValueInMiddleOfBlock(LI->getParent());
772 Value *AvailableValue::MaterializeAdjustedValue(LoadInst *LI,
773 Instruction *InsertPt,
776 Type *LoadTy = LI->getType();
777 const DataLayout &DL = LI->getModule()->getDataLayout();
778 if (isSimpleValue()) {
779 Res = getSimpleValue();
780 if (Res->getType() != LoadTy) {
781 Res = getStoreValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
783 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset
784 << " " << *getSimpleValue() << '\n'
788 } else if (isCoercedLoadValue()) {
789 LoadInst *Load = getCoercedLoadValue();
790 if (Load->getType() == LoadTy && Offset == 0) {
793 Res = getLoadValueForLoad(Load, Offset, LoadTy, InsertPt, DL);
794 // We would like to use gvn.markInstructionForDeletion here, but we can't
795 // because the load is already memoized into the leader map table that GVN
796 // tracks. It is potentially possible to remove the load from the table,
797 // but then there all of the operations based on it would need to be
798 // rehashed. Just leave the dead load around.
799 gvn.getMemDep().removeInstruction(Load);
800 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset
801 << " " << *getCoercedLoadValue() << '\n'
805 } else if (isMemIntrinValue()) {
806 Res = getMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
808 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
809 << " " << *getMemIntrinValue() << '\n'
813 assert(isUndefValue() && "Should be UndefVal");
814 LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL Undef:\n";);
815 return UndefValue::get(LoadTy);
817 assert(Res && "failed to materialize?");
821 static bool isLifetimeStart(const Instruction *Inst) {
822 if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
823 return II->getIntrinsicID() == Intrinsic::lifetime_start;
827 /// Try to locate the three instruction involved in a missed
828 /// load-elimination case that is due to an intervening store.
829 static void reportMayClobberedLoad(LoadInst *LI, MemDepResult DepInfo,
831 OptimizationRemarkEmitter *ORE) {
834 User *OtherAccess = nullptr;
836 OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", LI);
837 R << "load of type " << NV("Type", LI->getType()) << " not eliminated"
840 for (auto *U : LI->getPointerOperand()->users())
841 if (U != LI && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
842 DT->dominates(cast<Instruction>(U), LI)) {
843 // FIXME: for now give up if there are multiple memory accesses that
844 // dominate the load. We need further analysis to decide which one is
845 // that we're forwarding from.
847 OtherAccess = nullptr;
853 R << " in favor of " << NV("OtherAccess", OtherAccess);
855 R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
860 bool GVN::AnalyzeLoadAvailability(LoadInst *LI, MemDepResult DepInfo,
861 Value *Address, AvailableValue &Res) {
862 assert((DepInfo.isDef() || DepInfo.isClobber()) &&
863 "expected a local dependence");
864 assert(LI->isUnordered() && "rules below are incorrect for ordered access");
866 const DataLayout &DL = LI->getModule()->getDataLayout();
868 Instruction *DepInst = DepInfo.getInst();
869 if (DepInfo.isClobber()) {
870 // If the dependence is to a store that writes to a superset of the bits
871 // read by the load, we can extract the bits we need for the load from the
873 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
874 // Can't forward from non-atomic to atomic without violating memory model.
875 if (Address && LI->isAtomic() <= DepSI->isAtomic()) {
877 analyzeLoadFromClobberingStore(LI->getType(), Address, DepSI, DL);
879 Res = AvailableValue::get(DepSI->getValueOperand(), Offset);
885 // Check to see if we have something like this:
888 // if we have this, replace the later with an extraction from the former.
889 if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) {
890 // If this is a clobber and L is the first instruction in its block, then
891 // we have the first instruction in the entry block.
892 // Can't forward from non-atomic to atomic without violating memory model.
893 if (DepLI != LI && Address && LI->isAtomic() <= DepLI->isAtomic()) {
895 analyzeLoadFromClobberingLoad(LI->getType(), Address, DepLI, DL);
898 Res = AvailableValue::getLoad(DepLI, Offset);
904 // If the clobbering value is a memset/memcpy/memmove, see if we can
905 // forward a value on from it.
906 if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
907 if (Address && !LI->isAtomic()) {
908 int Offset = analyzeLoadFromClobberingMemInst(LI->getType(), Address,
911 Res = AvailableValue::getMI(DepMI, Offset);
916 // Nothing known about this clobber, have to be conservative
918 // fast print dep, using operator<< on instruction is too slow.
919 dbgs() << "GVN: load "; LI->printAsOperand(dbgs());
920 dbgs() << " is clobbered by " << *DepInst << '\n';);
921 if (ORE->allowExtraAnalysis(DEBUG_TYPE))
922 reportMayClobberedLoad(LI, DepInfo, DT, ORE);
926 assert(DepInfo.isDef() && "follows from above");
928 // Loading the allocation -> undef.
929 if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
930 // Loading immediately after lifetime begin -> undef.
931 isLifetimeStart(DepInst)) {
932 Res = AvailableValue::get(UndefValue::get(LI->getType()));
936 // Loading from calloc (which zero initializes memory) -> zero
937 if (isCallocLikeFn(DepInst, TLI)) {
938 Res = AvailableValue::get(Constant::getNullValue(LI->getType()));
942 if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
943 // Reject loads and stores that are to the same address but are of
944 // different types if we have to. If the stored value is larger or equal to
945 // the loaded value, we can reuse it.
946 if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), LI->getType(),
950 // Can't forward from non-atomic to atomic without violating memory model.
951 if (S->isAtomic() < LI->isAtomic())
954 Res = AvailableValue::get(S->getValueOperand());
958 if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
959 // If the types mismatch and we can't handle it, reject reuse of the load.
960 // If the stored value is larger or equal to the loaded value, we can reuse
962 if (!canCoerceMustAliasedValueToLoad(LD, LI->getType(), DL))
965 // Can't forward from non-atomic to atomic without violating memory model.
966 if (LD->isAtomic() < LI->isAtomic())
969 Res = AvailableValue::getLoad(LD);
973 // Unknown def - must be conservative
975 // fast print dep, using operator<< on instruction is too slow.
976 dbgs() << "GVN: load "; LI->printAsOperand(dbgs());
977 dbgs() << " has unknown def " << *DepInst << '\n';);
981 void GVN::AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
982 AvailValInBlkVect &ValuesPerBlock,
983 UnavailBlkVect &UnavailableBlocks) {
984 // Filter out useless results (non-locals, etc). Keep track of the blocks
985 // where we have a value available in repl, also keep track of whether we see
986 // dependencies that produce an unknown value for the load (such as a call
987 // that could potentially clobber the load).
988 unsigned NumDeps = Deps.size();
989 for (unsigned i = 0, e = NumDeps; i != e; ++i) {
990 BasicBlock *DepBB = Deps[i].getBB();
991 MemDepResult DepInfo = Deps[i].getResult();
993 if (DeadBlocks.count(DepBB)) {
994 // Dead dependent mem-op disguise as a load evaluating the same value
995 // as the load in question.
996 ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1000 if (!DepInfo.isDef() && !DepInfo.isClobber()) {
1001 UnavailableBlocks.push_back(DepBB);
1005 // The address being loaded in this non-local block may not be the same as
1006 // the pointer operand of the load if PHI translation occurs. Make sure
1007 // to consider the right address.
1008 Value *Address = Deps[i].getAddress();
1011 if (AnalyzeLoadAvailability(LI, DepInfo, Address, AV)) {
1012 // subtlety: because we know this was a non-local dependency, we know
1013 // it's safe to materialize anywhere between the instruction within
1014 // DepInfo and the end of it's block.
1015 ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
1018 UnavailableBlocks.push_back(DepBB);
1022 assert(NumDeps == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1023 "post condition violation");
1026 bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
1027 UnavailBlkVect &UnavailableBlocks) {
1028 // Okay, we have *some* definitions of the value. This means that the value
1029 // is available in some of our (transitive) predecessors. Lets think about
1030 // doing PRE of this load. This will involve inserting a new load into the
1031 // predecessor when it's not available. We could do this in general, but
1032 // prefer to not increase code size. As such, we only do this when we know
1033 // that we only have to insert *one* load (which means we're basically moving
1034 // the load, not inserting a new one).
1036 SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
1037 UnavailableBlocks.end());
1039 // Let's find the first basic block with more than one predecessor. Walk
1040 // backwards through predecessors if needed.
1041 BasicBlock *LoadBB = LI->getParent();
1042 BasicBlock *TmpBB = LoadBB;
1043 bool IsSafeToSpeculativelyExecute = isSafeToSpeculativelyExecute(LI);
1045 // Check that there is no implicit control flow instructions above our load in
1046 // its block. If there is an instruction that doesn't always pass the
1047 // execution to the following instruction, then moving through it may become
1048 // invalid. For example:
1053 // guard(0 <= index && index < LEN);
1056 // It is illegal to move the array access to any point above the guard,
1057 // because if the index is out of bounds we should deoptimize rather than
1058 // access the array.
1059 // Check that there is no guard in this block above our instruction.
1060 if (!IsSafeToSpeculativelyExecute && ICF->isDominatedByICFIFromSameBlock(LI))
1062 while (TmpBB->getSinglePredecessor()) {
1063 TmpBB = TmpBB->getSinglePredecessor();
1064 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1066 if (Blockers.count(TmpBB))
1069 // If any of these blocks has more than one successor (i.e. if the edge we
1070 // just traversed was critical), then there are other paths through this
1071 // block along which the load may not be anticipated. Hoisting the load
1072 // above this block would be adding the load to execution paths along
1073 // which it was not previously executed.
1074 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1077 // Check that there is no implicit control flow in a block above.
1078 if (!IsSafeToSpeculativelyExecute && ICF->hasICF(TmpBB))
1085 // Check to see how many predecessors have the loaded value fully
1087 MapVector<BasicBlock *, Value *> PredLoads;
1088 DenseMap<BasicBlock*, char> FullyAvailableBlocks;
1089 for (const AvailableValueInBlock &AV : ValuesPerBlock)
1090 FullyAvailableBlocks[AV.BB] = true;
1091 for (BasicBlock *UnavailableBB : UnavailableBlocks)
1092 FullyAvailableBlocks[UnavailableBB] = false;
1094 SmallVector<BasicBlock *, 4> CriticalEdgePred;
1095 for (BasicBlock *Pred : predecessors(LoadBB)) {
1096 // If any predecessor block is an EH pad that does not allow non-PHI
1097 // instructions before the terminator, we can't PRE the load.
1098 if (Pred->getTerminator()->isEHPad()) {
1100 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1101 << Pred->getName() << "': " << *LI << '\n');
1105 if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
1109 if (Pred->getTerminator()->getNumSuccessors() != 1) {
1110 if (isa<IndirectBrInst>(Pred->getTerminator())) {
1112 dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1113 << Pred->getName() << "': " << *LI << '\n');
1117 // FIXME: Can we support the fallthrough edge?
1118 if (isa<CallBrInst>(Pred->getTerminator())) {
1120 dbgs() << "COULD NOT PRE LOAD BECAUSE OF CALLBR CRITICAL EDGE '"
1121 << Pred->getName() << "': " << *LI << '\n');
1125 if (LoadBB->isEHPad()) {
1127 dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1128 << Pred->getName() << "': " << *LI << '\n');
1132 CriticalEdgePred.push_back(Pred);
1134 // Only add the predecessors that will not be split for now.
1135 PredLoads[Pred] = nullptr;
1139 // Decide whether PRE is profitable for this load.
1140 unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
1141 assert(NumUnavailablePreds != 0 &&
1142 "Fully available value should already be eliminated!");
1144 // If this load is unavailable in multiple predecessors, reject it.
1145 // FIXME: If we could restructure the CFG, we could make a common pred with
1146 // all the preds that don't have an available LI and insert a new load into
1148 if (NumUnavailablePreds != 1)
1151 // Split critical edges, and update the unavailable predecessors accordingly.
1152 for (BasicBlock *OrigPred : CriticalEdgePred) {
1153 BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1154 assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1155 PredLoads[NewPred] = nullptr;
1156 LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1157 << LoadBB->getName() << '\n');
1160 // Check if the load can safely be moved to all the unavailable predecessors.
1161 bool CanDoPRE = true;
1162 const DataLayout &DL = LI->getModule()->getDataLayout();
1163 SmallVector<Instruction*, 8> NewInsts;
1164 for (auto &PredLoad : PredLoads) {
1165 BasicBlock *UnavailablePred = PredLoad.first;
1167 // Do PHI translation to get its value in the predecessor if necessary. The
1168 // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1169 // We do the translation for each edge we skipped by going from LI's block
1170 // to LoadBB, otherwise we might miss pieces needing translation.
1172 // If all preds have a single successor, then we know it is safe to insert
1173 // the load on the pred (?!?), so we can insert code to materialize the
1174 // pointer if it is not available.
1175 Value *LoadPtr = LI->getPointerOperand();
1176 BasicBlock *Cur = LI->getParent();
1177 while (Cur != LoadBB) {
1178 PHITransAddr Address(LoadPtr, DL, AC);
1179 LoadPtr = Address.PHITranslateWithInsertion(
1180 Cur, Cur->getSinglePredecessor(), *DT, NewInsts);
1185 Cur = Cur->getSinglePredecessor();
1189 PHITransAddr Address(LoadPtr, DL, AC);
1190 LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred, *DT,
1193 // If we couldn't find or insert a computation of this phi translated value,
1196 LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1197 << *LI->getPointerOperand() << "\n");
1202 PredLoad.second = LoadPtr;
1206 while (!NewInsts.empty()) {
1207 // Erase instructions generated by the failed PHI translation before
1208 // trying to number them. PHI translation might insert instructions
1209 // in basic blocks other than the current one, and we delete them
1210 // directly, as markInstructionForDeletion only allows removing from the
1211 // current basic block.
1212 NewInsts.pop_back_val()->eraseFromParent();
1214 // HINT: Don't revert the edge-splitting as following transformation may
1215 // also need to split these critical edges.
1216 return !CriticalEdgePred.empty();
1219 // Okay, we can eliminate this load by inserting a reload in the predecessor
1220 // and using PHI construction to get the value in the other predecessors, do
1222 LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *LI << '\n');
1223 LLVM_DEBUG(if (!NewInsts.empty()) dbgs()
1224 << "INSERTED " << NewInsts.size() << " INSTS: " << *NewInsts.back()
1227 // Assign value numbers to the new instructions.
1228 for (Instruction *I : NewInsts) {
1229 // Instructions that have been inserted in predecessor(s) to materialize
1230 // the load address do not retain their original debug locations. Doing
1231 // so could lead to confusing (but correct) source attributions.
1232 if (const DebugLoc &DL = I->getDebugLoc())
1233 I->setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));
1235 // FIXME: We really _ought_ to insert these value numbers into their
1236 // parent's availability map. However, in doing so, we risk getting into
1237 // ordering issues. If a block hasn't been processed yet, we would be
1238 // marking a value as AVAIL-IN, which isn't what we intend.
1242 for (const auto &PredLoad : PredLoads) {
1243 BasicBlock *UnavailablePred = PredLoad.first;
1244 Value *LoadPtr = PredLoad.second;
1246 auto *NewLoad = new LoadInst(
1247 LI->getType(), LoadPtr, LI->getName() + ".pre", LI->isVolatile(),
1248 MaybeAlign(LI->getAlignment()), LI->getOrdering(), LI->getSyncScopeID(),
1249 UnavailablePred->getTerminator());
1250 NewLoad->setDebugLoc(LI->getDebugLoc());
1252 // Transfer the old load's AA tags to the new load.
1254 LI->getAAMetadata(Tags);
1256 NewLoad->setAAMetadata(Tags);
1258 if (auto *MD = LI->getMetadata(LLVMContext::MD_invariant_load))
1259 NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1260 if (auto *InvGroupMD = LI->getMetadata(LLVMContext::MD_invariant_group))
1261 NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1262 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range))
1263 NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1265 // We do not propagate the old load's debug location, because the new
1266 // load now lives in a different BB, and we want to avoid a jumpy line
1268 // FIXME: How do we retain source locations without causing poor debugging
1271 // Add the newly created load.
1272 ValuesPerBlock.push_back(AvailableValueInBlock::get(UnavailablePred,
1274 MD->invalidateCachedPointerInfo(LoadPtr);
1275 LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1278 // Perform PHI construction.
1279 Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1280 LI->replaceAllUsesWith(V);
1281 if (isa<PHINode>(V))
1283 if (Instruction *I = dyn_cast<Instruction>(V))
1284 I->setDebugLoc(LI->getDebugLoc());
1285 if (V->getType()->isPtrOrPtrVectorTy())
1286 MD->invalidateCachedPointerInfo(V);
1287 markInstructionForDeletion(LI);
1289 return OptimizationRemark(DEBUG_TYPE, "LoadPRE", LI)
1290 << "load eliminated by PRE";
1296 static void reportLoadElim(LoadInst *LI, Value *AvailableValue,
1297 OptimizationRemarkEmitter *ORE) {
1298 using namespace ore;
1301 return OptimizationRemark(DEBUG_TYPE, "LoadElim", LI)
1302 << "load of type " << NV("Type", LI->getType()) << " eliminated"
1303 << setExtraArgs() << " in favor of "
1304 << NV("InfavorOfValue", AvailableValue);
1308 /// Attempt to eliminate a load whose dependencies are
1309 /// non-local by performing PHI construction.
1310 bool GVN::processNonLocalLoad(LoadInst *LI) {
1311 // non-local speculations are not allowed under asan.
1312 if (LI->getParent()->getParent()->hasFnAttribute(
1313 Attribute::SanitizeAddress) ||
1314 LI->getParent()->getParent()->hasFnAttribute(
1315 Attribute::SanitizeHWAddress))
1318 // Step 1: Find the non-local dependencies of the load.
1320 MD->getNonLocalPointerDependency(LI, Deps);
1322 // If we had to process more than one hundred blocks to find the
1323 // dependencies, this load isn't worth worrying about. Optimizing
1324 // it will be too expensive.
1325 unsigned NumDeps = Deps.size();
1326 if (NumDeps > MaxNumDeps)
1329 // If we had a phi translation failure, we'll have a single entry which is a
1330 // clobber in the current block. Reject this early.
1332 !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
1333 LLVM_DEBUG(dbgs() << "GVN: non-local load "; LI->printAsOperand(dbgs());
1334 dbgs() << " has unknown dependencies\n";);
1338 // If this load follows a GEP, see if we can PRE the indices before analyzing.
1339 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) {
1340 for (GetElementPtrInst::op_iterator OI = GEP->idx_begin(),
1341 OE = GEP->idx_end();
1343 if (Instruction *I = dyn_cast<Instruction>(OI->get()))
1344 performScalarPRE(I);
1347 // Step 2: Analyze the availability of the load
1348 AvailValInBlkVect ValuesPerBlock;
1349 UnavailBlkVect UnavailableBlocks;
1350 AnalyzeLoadAvailability(LI, Deps, ValuesPerBlock, UnavailableBlocks);
1352 // If we have no predecessors that produce a known value for this load, exit
1354 if (ValuesPerBlock.empty())
1357 // Step 3: Eliminate fully redundancy.
1359 // If all of the instructions we depend on produce a known value for this
1360 // load, then it is fully redundant and we can use PHI insertion to compute
1361 // its value. Insert PHIs and remove the fully redundant value now.
1362 if (UnavailableBlocks.empty()) {
1363 LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *LI << '\n');
1365 // Perform PHI construction.
1366 Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
1367 LI->replaceAllUsesWith(V);
1369 if (isa<PHINode>(V))
1371 if (Instruction *I = dyn_cast<Instruction>(V))
1372 // If instruction I has debug info, then we should not update it.
1373 // Also, if I has a null DebugLoc, then it is still potentially incorrect
1374 // to propagate LI's DebugLoc because LI may not post-dominate I.
1375 if (LI->getDebugLoc() && LI->getParent() == I->getParent())
1376 I->setDebugLoc(LI->getDebugLoc());
1377 if (V->getType()->isPtrOrPtrVectorTy())
1378 MD->invalidateCachedPointerInfo(V);
1379 markInstructionForDeletion(LI);
1381 reportLoadElim(LI, V, ORE);
1385 // Step 4: Eliminate partial redundancy.
1386 if (!EnablePRE || !EnableLoadPRE)
1389 return PerformLoadPRE(LI, ValuesPerBlock, UnavailableBlocks);
1392 static bool impliesEquivalanceIfTrue(CmpInst* Cmp) {
1393 if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_EQ)
1396 // Floating point comparisons can be equal, but not equivalent. Cases:
1397 // NaNs for unordered operators
1398 // +0.0 vs 0.0 for all operators
1399 if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
1400 (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
1401 Cmp->getFastMathFlags().noNaNs())) {
1402 Value *LHS = Cmp->getOperand(0);
1403 Value *RHS = Cmp->getOperand(1);
1404 // If we can prove either side non-zero, then equality must imply
1406 // FIXME: We should do this optimization if 'no signed zeros' is
1407 // applicable via an instruction-level fast-math-flag or some other
1408 // indicator that relaxed FP semantics are being used.
1409 if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1411 if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1413 // TODO: Handle vector floating point constants
1418 static bool impliesEquivalanceIfFalse(CmpInst* Cmp) {
1419 if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_NE)
1422 // Floating point comparisons can be equal, but not equivelent. Cases:
1423 // NaNs for unordered operators
1424 // +0.0 vs 0.0 for all operators
1425 if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE &&
1426 Cmp->getFastMathFlags().noNaNs()) ||
1427 Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) {
1428 Value *LHS = Cmp->getOperand(0);
1429 Value *RHS = Cmp->getOperand(1);
1430 // If we can prove either side non-zero, then equality must imply
1432 // FIXME: We should do this optimization if 'no signed zeros' is
1433 // applicable via an instruction-level fast-math-flag or some other
1434 // indicator that relaxed FP semantics are being used.
1435 if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1437 if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1439 // TODO: Handle vector floating point constants
1445 static bool hasUsersIn(Value *V, BasicBlock *BB) {
1446 for (User *U : V->users())
1447 if (isa<Instruction>(U) &&
1448 cast<Instruction>(U)->getParent() == BB)
1453 bool GVN::processAssumeIntrinsic(IntrinsicInst *IntrinsicI) {
1454 assert(IntrinsicI->getIntrinsicID() == Intrinsic::assume &&
1455 "This function can only be called with llvm.assume intrinsic");
1456 Value *V = IntrinsicI->getArgOperand(0);
1458 if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
1459 if (Cond->isZero()) {
1460 Type *Int8Ty = Type::getInt8Ty(V->getContext());
1461 // Insert a new store to null instruction before the load to indicate that
1462 // this code is not reachable. FIXME: We could insert unreachable
1463 // instruction directly because we can modify the CFG.
1464 new StoreInst(UndefValue::get(Int8Ty),
1465 Constant::getNullValue(Int8Ty->getPointerTo()),
1468 markInstructionForDeletion(IntrinsicI);
1470 } else if (isa<Constant>(V)) {
1471 // If it's not false, and constant, it must evaluate to true. This means our
1472 // assume is assume(true), and thus, pointless, and we don't want to do
1473 // anything more here.
1477 Constant *True = ConstantInt::getTrue(V->getContext());
1478 bool Changed = false;
1480 for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
1481 BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
1483 // This property is only true in dominated successors, propagateEquality
1484 // will check dominance for us.
1485 Changed |= propagateEquality(V, True, Edge, false);
1488 // We can replace assume value with true, which covers cases like this:
1489 // call void @llvm.assume(i1 %cmp)
1490 // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
1491 ReplaceOperandsWithMap[V] = True;
1493 // If we find an equality fact, canonicalize all dominated uses in this block
1494 // to one of the two values. We heuristically choice the "oldest" of the
1495 // two where age is determined by value number. (Note that propagateEquality
1496 // above handles the cross block case.)
1498 // Key case to cover are:
1500 // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
1501 // call void @llvm.assume(i1 %cmp)
1502 // ret float %0 ; will change it to ret float 3.000000e+00
1504 // %load = load float, float* %addr
1505 // %cmp = fcmp oeq float %load, %0
1506 // call void @llvm.assume(i1 %cmp)
1507 // ret float %load ; will change it to ret float %0
1508 if (auto *CmpI = dyn_cast<CmpInst>(V)) {
1509 if (impliesEquivalanceIfTrue(CmpI)) {
1510 Value *CmpLHS = CmpI->getOperand(0);
1511 Value *CmpRHS = CmpI->getOperand(1);
1512 // Heuristically pick the better replacement -- the choice of heuristic
1513 // isn't terribly important here, but the fact we canonicalize on some
1514 // replacement is for exposing other simplifications.
1515 // TODO: pull this out as a helper function and reuse w/existing
1516 // (slightly different) logic.
1517 if (isa<Constant>(CmpLHS) && !isa<Constant>(CmpRHS))
1518 std::swap(CmpLHS, CmpRHS);
1519 if (!isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))
1520 std::swap(CmpLHS, CmpRHS);
1521 if ((isa<Argument>(CmpLHS) && isa<Argument>(CmpRHS)) ||
1522 (isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))) {
1523 // Move the 'oldest' value to the right-hand side, using the value
1524 // number as a proxy for age.
1525 uint32_t LVN = VN.lookupOrAdd(CmpLHS);
1526 uint32_t RVN = VN.lookupOrAdd(CmpRHS);
1528 std::swap(CmpLHS, CmpRHS);
1531 // Handle degenerate case where we either haven't pruned a dead path or a
1532 // removed a trivial assume yet.
1533 if (isa<Constant>(CmpLHS) && isa<Constant>(CmpRHS))
1536 LLVM_DEBUG(dbgs() << "Replacing dominated uses of "
1537 << *CmpLHS << " with "
1538 << *CmpRHS << " in block "
1539 << IntrinsicI->getParent()->getName() << "\n");
1542 // Setup the replacement map - this handles uses within the same block
1543 if (hasUsersIn(CmpLHS, IntrinsicI->getParent()))
1544 ReplaceOperandsWithMap[CmpLHS] = CmpRHS;
1546 // NOTE: The non-block local cases are handled by the call to
1547 // propagateEquality above; this block is just about handling the block
1548 // local cases. TODO: There's a bunch of logic in propagateEqualiy which
1549 // isn't duplicated for the block local case, can we share it somehow?
1555 static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
1556 patchReplacementInstruction(I, Repl);
1557 I->replaceAllUsesWith(Repl);
1560 /// Attempt to eliminate a load, first by eliminating it
1561 /// locally, and then attempting non-local elimination if that fails.
1562 bool GVN::processLoad(LoadInst *L) {
1566 // This code hasn't been audited for ordered or volatile memory access
1567 if (!L->isUnordered())
1570 if (L->use_empty()) {
1571 markInstructionForDeletion(L);
1575 // ... to a pointer that has been loaded from before...
1576 MemDepResult Dep = MD->getDependency(L);
1578 // If it is defined in another block, try harder.
1579 if (Dep.isNonLocal())
1580 return processNonLocalLoad(L);
1582 // Only handle the local case below
1583 if (!Dep.isDef() && !Dep.isClobber()) {
1584 // This might be a NonFuncLocal or an Unknown
1586 // fast print dep, using operator<< on instruction is too slow.
1587 dbgs() << "GVN: load "; L->printAsOperand(dbgs());
1588 dbgs() << " has unknown dependence\n";);
1593 if (AnalyzeLoadAvailability(L, Dep, L->getPointerOperand(), AV)) {
1594 Value *AvailableValue = AV.MaterializeAdjustedValue(L, L, *this);
1596 // Replace the load!
1597 patchAndReplaceAllUsesWith(L, AvailableValue);
1598 markInstructionForDeletion(L);
1600 reportLoadElim(L, AvailableValue, ORE);
1601 // Tell MDA to rexamine the reused pointer since we might have more
1602 // information after forwarding it.
1603 if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
1604 MD->invalidateCachedPointerInfo(AvailableValue);
1611 /// Return a pair the first field showing the value number of \p Exp and the
1612 /// second field showing whether it is a value number newly created.
1613 std::pair<uint32_t, bool>
1614 GVN::ValueTable::assignExpNewValueNum(Expression &Exp) {
1615 uint32_t &e = expressionNumbering[Exp];
1616 bool CreateNewValNum = !e;
1617 if (CreateNewValNum) {
1618 Expressions.push_back(Exp);
1619 if (ExprIdx.size() < nextValueNumber + 1)
1620 ExprIdx.resize(nextValueNumber * 2);
1621 e = nextValueNumber;
1622 ExprIdx[nextValueNumber++] = nextExprNumber++;
1624 return {e, CreateNewValNum};
1627 /// Return whether all the values related with the same \p num are
1628 /// defined in \p BB.
1629 bool GVN::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
1631 LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
1632 while (Vals && Vals->BB == BB)
1637 /// Wrap phiTranslateImpl to provide caching functionality.
1638 uint32_t GVN::ValueTable::phiTranslate(const BasicBlock *Pred,
1639 const BasicBlock *PhiBlock, uint32_t Num,
1641 auto FindRes = PhiTranslateTable.find({Num, Pred});
1642 if (FindRes != PhiTranslateTable.end())
1643 return FindRes->second;
1644 uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, Gvn);
1645 PhiTranslateTable.insert({{Num, Pred}, NewNum});
1649 // Return true if the value number \p Num and NewNum have equal value.
1650 // Return false if the result is unknown.
1651 bool GVN::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
1652 const BasicBlock *Pred,
1653 const BasicBlock *PhiBlock, GVN &Gvn) {
1654 CallInst *Call = nullptr;
1655 LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
1657 Call = dyn_cast<CallInst>(Vals->Val);
1658 if (Call && Call->getParent() == PhiBlock)
1663 if (AA->doesNotAccessMemory(Call))
1666 if (!MD || !AA->onlyReadsMemory(Call))
1669 MemDepResult local_dep = MD->getDependency(Call);
1670 if (!local_dep.isNonLocal())
1673 const MemoryDependenceResults::NonLocalDepInfo &deps =
1674 MD->getNonLocalCallDependency(Call);
1676 // Check to see if the Call has no function local clobber.
1677 for (unsigned i = 0; i < deps.size(); i++) {
1678 if (deps[i].getResult().isNonFuncLocal())
1684 /// Translate value number \p Num using phis, so that it has the values of
1686 uint32_t GVN::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
1687 const BasicBlock *PhiBlock,
1688 uint32_t Num, GVN &Gvn) {
1689 if (PHINode *PN = NumberingPhi[Num]) {
1690 for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
1691 if (PN->getParent() == PhiBlock && PN->getIncomingBlock(i) == Pred)
1692 if (uint32_t TransVal = lookup(PN->getIncomingValue(i), false))
1698 // If there is any value related with Num is defined in a BB other than
1699 // PhiBlock, it cannot depend on a phi in PhiBlock without going through
1700 // a backedge. We can do an early exit in that case to save compile time.
1701 if (!areAllValsInBB(Num, PhiBlock, Gvn))
1704 if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
1706 Expression Exp = Expressions[ExprIdx[Num]];
1708 for (unsigned i = 0; i < Exp.varargs.size(); i++) {
1709 // For InsertValue and ExtractValue, some varargs are index numbers
1710 // instead of value numbers. Those index numbers should not be
1712 if ((i > 1 && Exp.opcode == Instruction::InsertValue) ||
1713 (i > 0 && Exp.opcode == Instruction::ExtractValue))
1715 Exp.varargs[i] = phiTranslate(Pred, PhiBlock, Exp.varargs[i], Gvn);
1718 if (Exp.commutative) {
1719 assert(Exp.varargs.size() == 2 && "Unsupported commutative expression!");
1720 if (Exp.varargs[0] > Exp.varargs[1]) {
1721 std::swap(Exp.varargs[0], Exp.varargs[1]);
1722 uint32_t Opcode = Exp.opcode >> 8;
1723 if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
1724 Exp.opcode = (Opcode << 8) |
1725 CmpInst::getSwappedPredicate(
1726 static_cast<CmpInst::Predicate>(Exp.opcode & 255));
1730 if (uint32_t NewNum = expressionNumbering[Exp]) {
1731 if (Exp.opcode == Instruction::Call && NewNum != Num)
1732 return areCallValsEqual(Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num;
1738 /// Erase stale entry from phiTranslate cache so phiTranslate can be computed
1740 void GVN::ValueTable::eraseTranslateCacheEntry(uint32_t Num,
1741 const BasicBlock &CurrBlock) {
1742 for (const BasicBlock *Pred : predecessors(&CurrBlock)) {
1743 auto FindRes = PhiTranslateTable.find({Num, Pred});
1744 if (FindRes != PhiTranslateTable.end())
1745 PhiTranslateTable.erase(FindRes);
1749 // In order to find a leader for a given value number at a
1750 // specific basic block, we first obtain the list of all Values for that number,
1751 // and then scan the list to find one whose block dominates the block in
1752 // question. This is fast because dominator tree queries consist of only
1753 // a few comparisons of DFS numbers.
1754 Value *GVN::findLeader(const BasicBlock *BB, uint32_t num) {
1755 LeaderTableEntry Vals = LeaderTable[num];
1756 if (!Vals.Val) return nullptr;
1758 Value *Val = nullptr;
1759 if (DT->dominates(Vals.BB, BB)) {
1761 if (isa<Constant>(Val)) return Val;
1764 LeaderTableEntry* Next = Vals.Next;
1766 if (DT->dominates(Next->BB, BB)) {
1767 if (isa<Constant>(Next->Val)) return Next->Val;
1768 if (!Val) Val = Next->Val;
1777 /// There is an edge from 'Src' to 'Dst'. Return
1778 /// true if every path from the entry block to 'Dst' passes via this edge. In
1779 /// particular 'Dst' must not be reachable via another edge from 'Src'.
1780 static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
1781 DominatorTree *DT) {
1782 // While in theory it is interesting to consider the case in which Dst has
1783 // more than one predecessor, because Dst might be part of a loop which is
1784 // only reachable from Src, in practice it is pointless since at the time
1785 // GVN runs all such loops have preheaders, which means that Dst will have
1786 // been changed to have only one predecessor, namely Src.
1787 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
1788 assert((!Pred || Pred == E.getStart()) &&
1789 "No edge between these basic blocks!");
1790 return Pred != nullptr;
1793 void GVN::assignBlockRPONumber(Function &F) {
1794 BlockRPONumber.clear();
1795 uint32_t NextBlockNumber = 1;
1796 ReversePostOrderTraversal<Function *> RPOT(&F);
1797 for (BasicBlock *BB : RPOT)
1798 BlockRPONumber[BB] = NextBlockNumber++;
1799 InvalidBlockRPONumbers = false;
1802 bool GVN::replaceOperandsForInBlockEquality(Instruction *Instr) const {
1803 bool Changed = false;
1804 for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
1805 Value *Operand = Instr->getOperand(OpNum);
1806 auto it = ReplaceOperandsWithMap.find(Operand);
1807 if (it != ReplaceOperandsWithMap.end()) {
1808 LLVM_DEBUG(dbgs() << "GVN replacing: " << *Operand << " with "
1809 << *it->second << " in instruction " << *Instr << '\n');
1810 Instr->setOperand(OpNum, it->second);
1817 /// The given values are known to be equal in every block
1818 /// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
1819 /// 'RHS' everywhere in the scope. Returns whether a change was made.
1820 /// If DominatesByEdge is false, then it means that we will propagate the RHS
1821 /// value starting from the end of Root.Start.
1822 bool GVN::propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root,
1823 bool DominatesByEdge) {
1824 SmallVector<std::pair<Value*, Value*>, 4> Worklist;
1825 Worklist.push_back(std::make_pair(LHS, RHS));
1826 bool Changed = false;
1827 // For speed, compute a conservative fast approximation to
1828 // DT->dominates(Root, Root.getEnd());
1829 const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
1831 while (!Worklist.empty()) {
1832 std::pair<Value*, Value*> Item = Worklist.pop_back_val();
1833 LHS = Item.first; RHS = Item.second;
1837 assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
1839 // Don't try to propagate equalities between constants.
1840 if (isa<Constant>(LHS) && isa<Constant>(RHS))
1843 // Prefer a constant on the right-hand side, or an Argument if no constants.
1844 if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
1845 std::swap(LHS, RHS);
1846 assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
1848 // If there is no obvious reason to prefer the left-hand side over the
1849 // right-hand side, ensure the longest lived term is on the right-hand side,
1850 // so the shortest lived term will be replaced by the longest lived.
1851 // This tends to expose more simplifications.
1852 uint32_t LVN = VN.lookupOrAdd(LHS);
1853 if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
1854 (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
1855 // Move the 'oldest' value to the right-hand side, using the value number
1856 // as a proxy for age.
1857 uint32_t RVN = VN.lookupOrAdd(RHS);
1859 std::swap(LHS, RHS);
1864 // If value numbering later sees that an instruction in the scope is equal
1865 // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
1866 // the invariant that instructions only occur in the leader table for their
1867 // own value number (this is used by removeFromLeaderTable), do not do this
1868 // if RHS is an instruction (if an instruction in the scope is morphed into
1869 // LHS then it will be turned into RHS by the next GVN iteration anyway, so
1870 // using the leader table is about compiling faster, not optimizing better).
1871 // The leader table only tracks basic blocks, not edges. Only add to if we
1872 // have the simple case where the edge dominates the end.
1873 if (RootDominatesEnd && !isa<Instruction>(RHS))
1874 addToLeaderTable(LVN, RHS, Root.getEnd());
1876 // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
1877 // LHS always has at least one use that is not dominated by Root, this will
1878 // never do anything if LHS has only one use.
1879 if (!LHS->hasOneUse()) {
1880 unsigned NumReplacements =
1882 ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
1883 : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
1885 Changed |= NumReplacements > 0;
1886 NumGVNEqProp += NumReplacements;
1887 // Cached information for anything that uses LHS will be invalid.
1889 MD->invalidateCachedPointerInfo(LHS);
1892 // Now try to deduce additional equalities from this one. For example, if
1893 // the known equality was "(A != B)" == "false" then it follows that A and B
1894 // are equal in the scope. Only boolean equalities with an explicit true or
1895 // false RHS are currently supported.
1896 if (!RHS->getType()->isIntegerTy(1))
1897 // Not a boolean equality - bail out.
1899 ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
1901 // RHS neither 'true' nor 'false' - bail out.
1903 // Whether RHS equals 'true'. Otherwise it equals 'false'.
1904 bool isKnownTrue = CI->isMinusOne();
1905 bool isKnownFalse = !isKnownTrue;
1907 // If "A && B" is known true then both A and B are known true. If "A || B"
1908 // is known false then both A and B are known false.
1910 if ((isKnownTrue && match(LHS, m_And(m_Value(A), m_Value(B)))) ||
1911 (isKnownFalse && match(LHS, m_Or(m_Value(A), m_Value(B))))) {
1912 Worklist.push_back(std::make_pair(A, RHS));
1913 Worklist.push_back(std::make_pair(B, RHS));
1917 // If we are propagating an equality like "(A == B)" == "true" then also
1918 // propagate the equality A == B. When propagating a comparison such as
1919 // "(A >= B)" == "true", replace all instances of "A < B" with "false".
1920 if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
1921 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
1923 // If "A == B" is known true, or "A != B" is known false, then replace
1924 // A with B everywhere in the scope. For floating point operations, we
1925 // have to be careful since equality does not always imply equivalance.
1926 if ((isKnownTrue && impliesEquivalanceIfTrue(Cmp)) ||
1927 (isKnownFalse && impliesEquivalanceIfFalse(Cmp)))
1928 Worklist.push_back(std::make_pair(Op0, Op1));
1930 // If "A >= B" is known true, replace "A < B" with false everywhere.
1931 CmpInst::Predicate NotPred = Cmp->getInversePredicate();
1932 Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
1933 // Since we don't have the instruction "A < B" immediately to hand, work
1934 // out the value number that it would have and use that to find an
1935 // appropriate instruction (if any).
1936 uint32_t NextNum = VN.getNextUnusedValueNumber();
1937 uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
1938 // If the number we were assigned was brand new then there is no point in
1939 // looking for an instruction realizing it: there cannot be one!
1940 if (Num < NextNum) {
1941 Value *NotCmp = findLeader(Root.getEnd(), Num);
1942 if (NotCmp && isa<Instruction>(NotCmp)) {
1943 unsigned NumReplacements =
1945 ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
1946 : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
1948 Changed |= NumReplacements > 0;
1949 NumGVNEqProp += NumReplacements;
1950 // Cached information for anything that uses NotCmp will be invalid.
1952 MD->invalidateCachedPointerInfo(NotCmp);
1955 // Ensure that any instruction in scope that gets the "A < B" value number
1956 // is replaced with false.
1957 // The leader table only tracks basic blocks, not edges. Only add to if we
1958 // have the simple case where the edge dominates the end.
1959 if (RootDominatesEnd)
1960 addToLeaderTable(Num, NotVal, Root.getEnd());
1969 /// When calculating availability, handle an instruction
1970 /// by inserting it into the appropriate sets
1971 bool GVN::processInstruction(Instruction *I) {
1972 // Ignore dbg info intrinsics.
1973 if (isa<DbgInfoIntrinsic>(I))
1976 // If the instruction can be easily simplified then do so now in preference
1977 // to value numbering it. Value numbering often exposes redundancies, for
1978 // example if it determines that %y is equal to %x then the instruction
1979 // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
1980 const DataLayout &DL = I->getModule()->getDataLayout();
1981 if (Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC})) {
1982 bool Changed = false;
1983 if (!I->use_empty()) {
1984 I->replaceAllUsesWith(V);
1987 if (isInstructionTriviallyDead(I, TLI)) {
1988 markInstructionForDeletion(I);
1992 if (MD && V->getType()->isPtrOrPtrVectorTy())
1993 MD->invalidateCachedPointerInfo(V);
1999 if (IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(I))
2000 if (IntrinsicI->getIntrinsicID() == Intrinsic::assume)
2001 return processAssumeIntrinsic(IntrinsicI);
2003 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
2004 if (processLoad(LI))
2007 unsigned Num = VN.lookupOrAdd(LI);
2008 addToLeaderTable(Num, LI, LI->getParent());
2012 // For conditional branches, we can perform simple conditional propagation on
2013 // the condition value itself.
2014 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
2015 if (!BI->isConditional())
2018 if (isa<Constant>(BI->getCondition()))
2019 return processFoldableCondBr(BI);
2021 Value *BranchCond = BI->getCondition();
2022 BasicBlock *TrueSucc = BI->getSuccessor(0);
2023 BasicBlock *FalseSucc = BI->getSuccessor(1);
2024 // Avoid multiple edges early.
2025 if (TrueSucc == FalseSucc)
2028 BasicBlock *Parent = BI->getParent();
2029 bool Changed = false;
2031 Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
2032 BasicBlockEdge TrueE(Parent, TrueSucc);
2033 Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
2035 Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
2036 BasicBlockEdge FalseE(Parent, FalseSucc);
2037 Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
2042 // For switches, propagate the case values into the case destinations.
2043 if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2044 Value *SwitchCond = SI->getCondition();
2045 BasicBlock *Parent = SI->getParent();
2046 bool Changed = false;
2048 // Remember how many outgoing edges there are to every successor.
2049 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
2050 for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
2051 ++SwitchEdges[SI->getSuccessor(i)];
2053 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2055 BasicBlock *Dst = i->getCaseSuccessor();
2056 // If there is only a single edge, propagate the case value into it.
2057 if (SwitchEdges.lookup(Dst) == 1) {
2058 BasicBlockEdge E(Parent, Dst);
2059 Changed |= propagateEquality(SwitchCond, i->getCaseValue(), E, true);
2065 // Instructions with void type don't return a value, so there's
2066 // no point in trying to find redundancies in them.
2067 if (I->getType()->isVoidTy())
2070 uint32_t NextNum = VN.getNextUnusedValueNumber();
2071 unsigned Num = VN.lookupOrAdd(I);
2073 // Allocations are always uniquely numbered, so we can save time and memory
2074 // by fast failing them.
2075 if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
2076 addToLeaderTable(Num, I, I->getParent());
2080 // If the number we were assigned was a brand new VN, then we don't
2081 // need to do a lookup to see if the number already exists
2082 // somewhere in the domtree: it can't!
2083 if (Num >= NextNum) {
2084 addToLeaderTable(Num, I, I->getParent());
2088 // Perform fast-path value-number based elimination of values inherited from
2090 Value *Repl = findLeader(I->getParent(), Num);
2092 // Failure, just remember this instance for future use.
2093 addToLeaderTable(Num, I, I->getParent());
2095 } else if (Repl == I) {
2096 // If I was the result of a shortcut PRE, it might already be in the table
2097 // and the best replacement for itself. Nothing to do.
2102 patchAndReplaceAllUsesWith(I, Repl);
2103 if (MD && Repl->getType()->isPtrOrPtrVectorTy())
2104 MD->invalidateCachedPointerInfo(Repl);
2105 markInstructionForDeletion(I);
2109 /// runOnFunction - This is the main transformation entry point for a function.
2110 bool GVN::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2111 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2112 MemoryDependenceResults *RunMD, LoopInfo *LI,
2113 OptimizationRemarkEmitter *RunORE) {
2118 VN.setAliasAnalysis(&RunAA);
2120 ImplicitControlFlowTracking ImplicitCFT(DT);
2125 InvalidBlockRPONumbers = true;
2127 bool Changed = false;
2128 bool ShouldContinue = true;
2130 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2131 // Merge unconditional branches, allowing PRE to catch more
2132 // optimization opportunities.
2133 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
2134 BasicBlock *BB = &*FI++;
2136 bool removedBlock = MergeBlockIntoPredecessor(BB, &DTU, LI, nullptr, MD);
2140 Changed |= removedBlock;
2143 unsigned Iteration = 0;
2144 while (ShouldContinue) {
2145 LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
2146 ShouldContinue = iterateOnFunction(F);
2147 Changed |= ShouldContinue;
2152 // Fabricate val-num for dead-code in order to suppress assertion in
2154 assignValNumForDeadCode();
2155 bool PREChanged = true;
2156 while (PREChanged) {
2157 PREChanged = performPRE(F);
2158 Changed |= PREChanged;
2162 // FIXME: Should perform GVN again after PRE does something. PRE can move
2163 // computations into blocks where they become fully redundant. Note that
2164 // we can't do this until PRE's critical edge splitting updates memdep.
2165 // Actually, when this happens, we should just fully integrate PRE into GVN.
2167 cleanupGlobalSets();
2168 // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2175 bool GVN::processBlock(BasicBlock *BB) {
2176 // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2177 // (and incrementing BI before processing an instruction).
2178 assert(InstrsToErase.empty() &&
2179 "We expect InstrsToErase to be empty across iterations");
2180 if (DeadBlocks.count(BB))
2183 // Clearing map before every BB because it can be used only for single BB.
2184 ReplaceOperandsWithMap.clear();
2185 bool ChangedFunction = false;
2187 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
2189 if (!ReplaceOperandsWithMap.empty())
2190 ChangedFunction |= replaceOperandsForInBlockEquality(&*BI);
2191 ChangedFunction |= processInstruction(&*BI);
2193 if (InstrsToErase.empty()) {
2198 // If we need some instructions deleted, do it now.
2199 NumGVNInstr += InstrsToErase.size();
2201 // Avoid iterator invalidation.
2202 bool AtStart = BI == BB->begin();
2206 for (auto *I : InstrsToErase) {
2207 assert(I->getParent() == BB && "Removing instruction from wrong block?");
2208 LLVM_DEBUG(dbgs() << "GVN removed: " << *I << '\n');
2209 salvageDebugInfo(*I);
2210 if (MD) MD->removeInstruction(I);
2211 LLVM_DEBUG(verifyRemoved(I));
2212 ICF->removeInstruction(I);
2213 I->eraseFromParent();
2215 InstrsToErase.clear();
2223 return ChangedFunction;
2226 // Instantiate an expression in a predecessor that lacked it.
2227 bool GVN::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2228 BasicBlock *Curr, unsigned int ValNo) {
2229 // Because we are going top-down through the block, all value numbers
2230 // will be available in the predecessor by the time we need them. Any
2231 // that weren't originally present will have been instantiated earlier
2233 bool success = true;
2234 for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
2235 Value *Op = Instr->getOperand(i);
2236 if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
2238 // This could be a newly inserted instruction, in which case, we won't
2239 // find a value number, and should give up before we hurt ourselves.
2240 // FIXME: Rewrite the infrastructure to let it easier to value number
2241 // and process newly inserted instructions.
2242 if (!VN.exists(Op)) {
2247 VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
2248 if (Value *V = findLeader(Pred, TValNo)) {
2249 Instr->setOperand(i, V);
2256 // Fail out if we encounter an operand that is not available in
2257 // the PRE predecessor. This is typically because of loads which
2258 // are not value numbered precisely.
2262 Instr->insertBefore(Pred->getTerminator());
2263 Instr->setName(Instr->getName() + ".pre");
2264 Instr->setDebugLoc(Instr->getDebugLoc());
2266 unsigned Num = VN.lookupOrAdd(Instr);
2269 // Update the availability map to include the new instruction.
2270 addToLeaderTable(Num, Instr, Pred);
2274 bool GVN::performScalarPRE(Instruction *CurInst) {
2275 if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
2276 isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2277 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2278 isa<DbgInfoIntrinsic>(CurInst))
2281 // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2282 // sinking the compare again, and it would force the code generator to
2283 // move the i1 from processor flags or predicate registers into a general
2284 // purpose register.
2285 if (isa<CmpInst>(CurInst))
2288 // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
2289 // sinking the addressing mode computation back to its uses. Extending the
2290 // GEP's live range increases the register pressure, and therefore it can
2291 // introduce unnecessary spills.
2293 // This doesn't prevent Load PRE. PHI translation will make the GEP available
2294 // to the load by moving it to the predecessor block if necessary.
2295 if (isa<GetElementPtrInst>(CurInst))
2298 // We don't currently value number ANY inline asm calls.
2299 if (auto *CallB = dyn_cast<CallBase>(CurInst))
2300 if (CallB->isInlineAsm())
2303 uint32_t ValNo = VN.lookup(CurInst);
2305 // Look for the predecessors for PRE opportunities. We're
2306 // only trying to solve the basic diamond case, where
2307 // a value is computed in the successor and one predecessor,
2308 // but not the other. We also explicitly disallow cases
2309 // where the successor is its own predecessor, because they're
2310 // more complicated to get right.
2311 unsigned NumWith = 0;
2312 unsigned NumWithout = 0;
2313 BasicBlock *PREPred = nullptr;
2314 BasicBlock *CurrentBlock = CurInst->getParent();
2316 // Update the RPO numbers for this function.
2317 if (InvalidBlockRPONumbers)
2318 assignBlockRPONumber(*CurrentBlock->getParent());
2320 SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap;
2321 for (BasicBlock *P : predecessors(CurrentBlock)) {
2322 // We're not interested in PRE where blocks with predecessors that are
2324 if (!DT->isReachableFromEntry(P)) {
2328 // It is not safe to do PRE when P->CurrentBlock is a loop backedge, and
2329 // when CurInst has operand defined in CurrentBlock (so it may be defined
2330 // by phi in the loop header).
2331 assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&
2332 "Invalid BlockRPONumber map.");
2333 if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock] &&
2334 llvm::any_of(CurInst->operands(), [&](const Use &U) {
2335 if (auto *Inst = dyn_cast<Instruction>(U.get()))
2336 return Inst->getParent() == CurrentBlock;
2343 uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
2344 Value *predV = findLeader(P, TValNo);
2346 predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
2349 } else if (predV == CurInst) {
2350 /* CurInst dominates this predecessor. */
2354 predMap.push_back(std::make_pair(predV, P));
2359 // Don't do PRE when it might increase code size, i.e. when
2360 // we would need to insert instructions in more than one pred.
2361 if (NumWithout > 1 || NumWith == 0)
2364 // We may have a case where all predecessors have the instruction,
2365 // and we just need to insert a phi node. Otherwise, perform
2367 Instruction *PREInstr = nullptr;
2369 if (NumWithout != 0) {
2370 if (!isSafeToSpeculativelyExecute(CurInst)) {
2371 // It is only valid to insert a new instruction if the current instruction
2372 // is always executed. An instruction with implicit control flow could
2373 // prevent us from doing it. If we cannot speculate the execution, then
2374 // PRE should be prohibited.
2375 if (ICF->isDominatedByICFIFromSameBlock(CurInst))
2379 // Don't do PRE across indirect branch.
2380 if (isa<IndirectBrInst>(PREPred->getTerminator()))
2383 // Don't do PRE across callbr.
2384 // FIXME: Can we do this across the fallthrough edge?
2385 if (isa<CallBrInst>(PREPred->getTerminator()))
2388 // We can't do PRE safely on a critical edge, so instead we schedule
2389 // the edge to be split and perform the PRE the next time we iterate
2391 unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
2392 if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
2393 toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
2396 // We need to insert somewhere, so let's give it a shot
2397 PREInstr = CurInst->clone();
2398 if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
2399 // If we failed insertion, make sure we remove the instruction.
2400 LLVM_DEBUG(verifyRemoved(PREInstr));
2401 PREInstr->deleteValue();
2406 // Either we should have filled in the PRE instruction, or we should
2407 // not have needed insertions.
2408 assert(PREInstr != nullptr || NumWithout == 0);
2412 // Create a PHI to make the value available in this block.
2414 PHINode::Create(CurInst->getType(), predMap.size(),
2415 CurInst->getName() + ".pre-phi", &CurrentBlock->front());
2416 for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
2417 if (Value *V = predMap[i].first) {
2418 // If we use an existing value in this phi, we have to patch the original
2419 // value because the phi will be used to replace a later value.
2420 patchReplacementInstruction(CurInst, V);
2421 Phi->addIncoming(V, predMap[i].second);
2423 Phi->addIncoming(PREInstr, PREPred);
2427 // After creating a new PHI for ValNo, the phi translate result for ValNo will
2428 // be changed, so erase the related stale entries in phi translate cache.
2429 VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
2430 addToLeaderTable(ValNo, Phi, CurrentBlock);
2431 Phi->setDebugLoc(CurInst->getDebugLoc());
2432 CurInst->replaceAllUsesWith(Phi);
2433 if (MD && Phi->getType()->isPtrOrPtrVectorTy())
2434 MD->invalidateCachedPointerInfo(Phi);
2436 removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
2438 LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
2440 MD->removeInstruction(CurInst);
2441 LLVM_DEBUG(verifyRemoved(CurInst));
2442 // FIXME: Intended to be markInstructionForDeletion(CurInst), but it causes
2443 // some assertion failures.
2444 ICF->removeInstruction(CurInst);
2445 CurInst->eraseFromParent();
2451 /// Perform a purely local form of PRE that looks for diamond
2452 /// control flow patterns and attempts to perform simple PRE at the join point.
2453 bool GVN::performPRE(Function &F) {
2454 bool Changed = false;
2455 for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
2456 // Nothing to PRE in the entry block.
2457 if (CurrentBlock == &F.getEntryBlock())
2460 // Don't perform PRE on an EH pad.
2461 if (CurrentBlock->isEHPad())
2464 for (BasicBlock::iterator BI = CurrentBlock->begin(),
2465 BE = CurrentBlock->end();
2467 Instruction *CurInst = &*BI++;
2468 Changed |= performScalarPRE(CurInst);
2472 if (splitCriticalEdges())
2478 /// Split the critical edge connecting the given two blocks, and return
2479 /// the block inserted to the critical edge.
2480 BasicBlock *GVN::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
2482 SplitCriticalEdge(Pred, Succ, CriticalEdgeSplittingOptions(DT, LI));
2484 MD->invalidateCachedPredecessors();
2485 InvalidBlockRPONumbers = true;
2489 /// Split critical edges found during the previous
2490 /// iteration that may enable further optimization.
2491 bool GVN::splitCriticalEdges() {
2492 if (toSplit.empty())
2495 std::pair<Instruction *, unsigned> Edge = toSplit.pop_back_val();
2496 SplitCriticalEdge(Edge.first, Edge.second,
2497 CriticalEdgeSplittingOptions(DT, LI));
2498 } while (!toSplit.empty());
2499 if (MD) MD->invalidateCachedPredecessors();
2500 InvalidBlockRPONumbers = true;
2504 /// Executes one iteration of GVN
2505 bool GVN::iterateOnFunction(Function &F) {
2506 cleanupGlobalSets();
2508 // Top-down walk of the dominator tree
2509 bool Changed = false;
2510 // Needed for value numbering with phi construction to work.
2511 // RPOT walks the graph in its constructor and will not be invalidated during
2513 ReversePostOrderTraversal<Function *> RPOT(&F);
2515 for (BasicBlock *BB : RPOT)
2516 Changed |= processBlock(BB);
2521 void GVN::cleanupGlobalSets() {
2523 LeaderTable.clear();
2524 BlockRPONumber.clear();
2525 TableAllocator.Reset();
2527 InvalidBlockRPONumbers = true;
2530 /// Verify that the specified instruction does not occur in our
2531 /// internal data structures.
2532 void GVN::verifyRemoved(const Instruction *Inst) const {
2533 VN.verifyRemoved(Inst);
2535 // Walk through the value number scope to make sure the instruction isn't
2536 // ferreted away in it.
2537 for (DenseMap<uint32_t, LeaderTableEntry>::const_iterator
2538 I = LeaderTable.begin(), E = LeaderTable.end(); I != E; ++I) {
2539 const LeaderTableEntry *Node = &I->second;
2540 assert(Node->Val != Inst && "Inst still in value numbering scope!");
2542 while (Node->Next) {
2544 assert(Node->Val != Inst && "Inst still in value numbering scope!");
2549 /// BB is declared dead, which implied other blocks become dead as well. This
2550 /// function is to add all these blocks to "DeadBlocks". For the dead blocks'
2551 /// live successors, update their phi nodes by replacing the operands
2552 /// corresponding to dead blocks with UndefVal.
2553 void GVN::addDeadBlock(BasicBlock *BB) {
2554 SmallVector<BasicBlock *, 4> NewDead;
2555 SmallSetVector<BasicBlock *, 4> DF;
2557 NewDead.push_back(BB);
2558 while (!NewDead.empty()) {
2559 BasicBlock *D = NewDead.pop_back_val();
2560 if (DeadBlocks.count(D))
2563 // All blocks dominated by D are dead.
2564 SmallVector<BasicBlock *, 8> Dom;
2565 DT->getDescendants(D, Dom);
2566 DeadBlocks.insert(Dom.begin(), Dom.end());
2568 // Figure out the dominance-frontier(D).
2569 for (BasicBlock *B : Dom) {
2570 for (BasicBlock *S : successors(B)) {
2571 if (DeadBlocks.count(S))
2574 bool AllPredDead = true;
2575 for (BasicBlock *P : predecessors(S))
2576 if (!DeadBlocks.count(P)) {
2577 AllPredDead = false;
2582 // S could be proved dead later on. That is why we don't update phi
2583 // operands at this moment.
2586 // While S is not dominated by D, it is dead by now. This could take
2587 // place if S already have a dead predecessor before D is declared
2589 NewDead.push_back(S);
2595 // For the dead blocks' live successors, update their phi nodes by replacing
2596 // the operands corresponding to dead blocks with UndefVal.
2597 for(SmallSetVector<BasicBlock *, 4>::iterator I = DF.begin(), E = DF.end();
2600 if (DeadBlocks.count(B))
2603 // First, split the critical edges. This might also create additional blocks
2604 // to preserve LoopSimplify form and adjust edges accordingly.
2605 SmallVector<BasicBlock *, 4> Preds(pred_begin(B), pred_end(B));
2606 for (BasicBlock *P : Preds) {
2607 if (!DeadBlocks.count(P))
2610 if (llvm::any_of(successors(P),
2611 [B](BasicBlock *Succ) { return Succ == B; }) &&
2612 isCriticalEdge(P->getTerminator(), B)) {
2613 if (BasicBlock *S = splitCriticalEdges(P, B))
2614 DeadBlocks.insert(P = S);
2618 // Now undef the incoming values from the dead predecessors.
2619 for (BasicBlock *P : predecessors(B)) {
2620 if (!DeadBlocks.count(P))
2622 for (PHINode &Phi : B->phis()) {
2623 Phi.setIncomingValueForBlock(P, UndefValue::get(Phi.getType()));
2625 MD->invalidateCachedPointerInfo(&Phi);
2631 // If the given branch is recognized as a foldable branch (i.e. conditional
2632 // branch with constant condition), it will perform following analyses and
2634 // 1) If the dead out-coming edge is a critical-edge, split it. Let
2635 // R be the target of the dead out-coming edge.
2636 // 1) Identify the set of dead blocks implied by the branch's dead outcoming
2637 // edge. The result of this step will be {X| X is dominated by R}
2638 // 2) Identify those blocks which haves at least one dead predecessor. The
2639 // result of this step will be dominance-frontier(R).
2640 // 3) Update the PHIs in DF(R) by replacing the operands corresponding to
2641 // dead blocks with "UndefVal" in an hope these PHIs will optimized away.
2643 // Return true iff *NEW* dead code are found.
2644 bool GVN::processFoldableCondBr(BranchInst *BI) {
2645 if (!BI || BI->isUnconditional())
2648 // If a branch has two identical successors, we cannot declare either dead.
2649 if (BI->getSuccessor(0) == BI->getSuccessor(1))
2652 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
2656 BasicBlock *DeadRoot =
2657 Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
2658 if (DeadBlocks.count(DeadRoot))
2661 if (!DeadRoot->getSinglePredecessor())
2662 DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
2664 addDeadBlock(DeadRoot);
2668 // performPRE() will trigger assert if it comes across an instruction without
2669 // associated val-num. As it normally has far more live instructions than dead
2670 // instructions, it makes more sense just to "fabricate" a val-number for the
2671 // dead code than checking if instruction involved is dead or not.
2672 void GVN::assignValNumForDeadCode() {
2673 for (BasicBlock *BB : DeadBlocks) {
2674 for (Instruction &Inst : *BB) {
2675 unsigned ValNum = VN.lookupOrAdd(&Inst);
2676 addToLeaderTable(ValNum, &Inst, BB);
2681 class llvm::gvn::GVNLegacyPass : public FunctionPass {
2683 static char ID; // Pass identification, replacement for typeid
2685 explicit GVNLegacyPass(bool NoMemDepAnalysis = !EnableMemDep)
2686 : FunctionPass(ID), NoMemDepAnalysis(NoMemDepAnalysis) {
2687 initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry());
2690 bool runOnFunction(Function &F) override {
2691 if (skipFunction(F))
2694 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
2696 return Impl.runImpl(
2697 F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
2698 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
2699 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
2700 getAnalysis<AAResultsWrapperPass>().getAAResults(),
2703 : &getAnalysis<MemoryDependenceWrapperPass>().getMemDep(),
2704 LIWP ? &LIWP->getLoopInfo() : nullptr,
2705 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE());
2708 void getAnalysisUsage(AnalysisUsage &AU) const override {
2709 AU.addRequired<AssumptionCacheTracker>();
2710 AU.addRequired<DominatorTreeWrapperPass>();
2711 AU.addRequired<TargetLibraryInfoWrapperPass>();
2712 AU.addRequired<LoopInfoWrapperPass>();
2713 if (!NoMemDepAnalysis)
2714 AU.addRequired<MemoryDependenceWrapperPass>();
2715 AU.addRequired<AAResultsWrapperPass>();
2717 AU.addPreserved<DominatorTreeWrapperPass>();
2718 AU.addPreserved<GlobalsAAWrapperPass>();
2719 AU.addPreserved<TargetLibraryInfoWrapperPass>();
2720 AU.addPreserved<LoopInfoWrapperPass>();
2721 AU.addPreservedID(LoopSimplifyID);
2722 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
2726 bool NoMemDepAnalysis;
2730 char GVNLegacyPass::ID = 0;
2732 INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
2733 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
2734 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
2735 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2736 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2737 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2738 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
2739 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
2740 INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
2742 // The public interface to this file...
2743 FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) {
2744 return new GVNLegacyPass(NoMemDepAnalysis);