xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Scalar/GVN.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass performs global value numbering to eliminate fully redundant
10 // instructions.  It also performs simple dead load elimination.
11 //
12 // Note that this pass does the value numbering itself; it does not use the
13 // ValueNumbering analysis passes.
14 //
15 //===----------------------------------------------------------------------===//
16 
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/PostOrderIterator.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Analysis/AliasAnalysis.h"
29 #include "llvm/Analysis/AssumeBundleQueries.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/InstructionPrecedenceTracking.h"
35 #include "llvm/Analysis/InstructionSimplify.h"
36 #include "llvm/Analysis/LoopInfo.h"
37 #include "llvm/Analysis/MemoryBuiltins.h"
38 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
39 #include "llvm/Analysis/MemorySSA.h"
40 #include "llvm/Analysis/MemorySSAUpdater.h"
41 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
42 #include "llvm/Analysis/PHITransAddr.h"
43 #include "llvm/Analysis/TargetLibraryInfo.h"
44 #include "llvm/Analysis/ValueTracking.h"
45 #include "llvm/IR/Attributes.h"
46 #include "llvm/IR/BasicBlock.h"
47 #include "llvm/IR/Constant.h"
48 #include "llvm/IR/Constants.h"
49 #include "llvm/IR/DebugLoc.h"
50 #include "llvm/IR/Dominators.h"
51 #include "llvm/IR/Function.h"
52 #include "llvm/IR/InstrTypes.h"
53 #include "llvm/IR/Instruction.h"
54 #include "llvm/IR/Instructions.h"
55 #include "llvm/IR/IntrinsicInst.h"
56 #include "llvm/IR/LLVMContext.h"
57 #include "llvm/IR/Metadata.h"
58 #include "llvm/IR/Module.h"
59 #include "llvm/IR/PassManager.h"
60 #include "llvm/IR/PatternMatch.h"
61 #include "llvm/IR/Type.h"
62 #include "llvm/IR/Use.h"
63 #include "llvm/IR/Value.h"
64 #include "llvm/InitializePasses.h"
65 #include "llvm/Pass.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/CommandLine.h"
68 #include "llvm/Support/Compiler.h"
69 #include "llvm/Support/Debug.h"
70 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
72 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
73 #include "llvm/Transforms/Utils/Local.h"
74 #include "llvm/Transforms/Utils/SSAUpdater.h"
75 #include "llvm/Transforms/Utils/VNCoercion.h"
76 #include <algorithm>
77 #include <cassert>
78 #include <cstdint>
79 #include <optional>
80 #include <utility>
81 
82 using namespace llvm;
83 using namespace llvm::gvn;
84 using namespace llvm::VNCoercion;
85 using namespace PatternMatch;
86 
87 #define DEBUG_TYPE "gvn"
88 
89 STATISTIC(NumGVNInstr, "Number of instructions deleted");
90 STATISTIC(NumGVNLoad, "Number of loads deleted");
91 STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
92 STATISTIC(NumGVNBlocks, "Number of blocks merged");
93 STATISTIC(NumGVNSimpl, "Number of instructions simplified");
94 STATISTIC(NumGVNEqProp, "Number of equalities propagated");
95 STATISTIC(NumPRELoad, "Number of loads PRE'd");
96 STATISTIC(NumPRELoopLoad, "Number of loop loads PRE'd");
97 STATISTIC(NumPRELoadMoved2CEPred,
98           "Number of loads moved to predecessor of a critical edge in PRE");
99 
100 STATISTIC(IsValueFullyAvailableInBlockNumSpeculationsMax,
101           "Number of blocks speculated as available in "
102           "IsValueFullyAvailableInBlock(), max");
103 STATISTIC(MaxBBSpeculationCutoffReachedTimes,
104           "Number of times we we reached gvn-max-block-speculations cut-off "
105           "preventing further exploration");
106 
107 static cl::opt<bool> GVNEnablePRE("enable-pre", cl::init(true), cl::Hidden);
108 static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true));
109 static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre",
110                                             cl::init(true));
111 static cl::opt<bool>
112 GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre",
113                                 cl::init(false));
114 static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
115 
116 static cl::opt<uint32_t> MaxNumDeps(
117     "gvn-max-num-deps", cl::Hidden, cl::init(100),
118     cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
119 
120 // This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat.
121 static cl::opt<uint32_t> MaxBBSpeculations(
122     "gvn-max-block-speculations", cl::Hidden, cl::init(600),
123     cl::desc("Max number of blocks we're willing to speculate on (and recurse "
124              "into) when deducing if a value is fully available or not in GVN "
125              "(default = 600)"));
126 
127 static cl::opt<uint32_t> MaxNumVisitedInsts(
128     "gvn-max-num-visited-insts", cl::Hidden, cl::init(100),
129     cl::desc("Max number of visited instructions when trying to find "
130              "dominating value of select dependency (default = 100)"));
131 
132 static cl::opt<uint32_t> MaxNumInsnsPerBlock(
133     "gvn-max-num-insns", cl::Hidden, cl::init(100),
134     cl::desc("Max number of instructions to scan in each basic block in GVN "
135              "(default = 100)"));
136 
137 struct llvm::GVNPass::Expression {
138   uint32_t opcode;
139   bool commutative = false;
140   // The type is not necessarily the result type of the expression, it may be
141   // any additional type needed to disambiguate the expression.
142   Type *type = nullptr;
143   SmallVector<uint32_t, 4> varargs;
144 
145   Expression(uint32_t o = ~2U) : opcode(o) {}
146 
147   bool operator==(const Expression &other) const {
148     if (opcode != other.opcode)
149       return false;
150     if (opcode == ~0U || opcode == ~1U)
151       return true;
152     if (type != other.type)
153       return false;
154     if (varargs != other.varargs)
155       return false;
156     return true;
157   }
158 
159   friend hash_code hash_value(const Expression &Value) {
160     return hash_combine(
161         Value.opcode, Value.type,
162         hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
163   }
164 };
165 
166 namespace llvm {
167 
168 template <> struct DenseMapInfo<GVNPass::Expression> {
169   static inline GVNPass::Expression getEmptyKey() { return ~0U; }
170   static inline GVNPass::Expression getTombstoneKey() { return ~1U; }
171 
172   static unsigned getHashValue(const GVNPass::Expression &e) {
173     using llvm::hash_value;
174 
175     return static_cast<unsigned>(hash_value(e));
176   }
177 
178   static bool isEqual(const GVNPass::Expression &LHS,
179                       const GVNPass::Expression &RHS) {
180     return LHS == RHS;
181   }
182 };
183 
184 } // end namespace llvm
185 
186 /// Represents a particular available value that we know how to materialize.
187 /// Materialization of an AvailableValue never fails.  An AvailableValue is
188 /// implicitly associated with a rematerialization point which is the
189 /// location of the instruction from which it was formed.
190 struct llvm::gvn::AvailableValue {
191   enum class ValType {
192     SimpleVal, // A simple offsetted value that is accessed.
193     LoadVal,   // A value produced by a load.
194     MemIntrin, // A memory intrinsic which is loaded from.
195     UndefVal,  // A UndefValue representing a value from dead block (which
196                // is not yet physically removed from the CFG).
197     SelectVal, // A pointer select which is loaded from and for which the load
198                // can be replace by a value select.
199   };
200 
201   /// Val - The value that is live out of the block.
202   Value *Val;
203   /// Kind of the live-out value.
204   ValType Kind;
205 
206   /// Offset - The byte offset in Val that is interesting for the load query.
207   unsigned Offset = 0;
208   /// V1, V2 - The dominating non-clobbered values of SelectVal.
209   Value *V1 = nullptr, *V2 = nullptr;
210 
211   static AvailableValue get(Value *V, unsigned Offset = 0) {
212     AvailableValue Res;
213     Res.Val = V;
214     Res.Kind = ValType::SimpleVal;
215     Res.Offset = Offset;
216     return Res;
217   }
218 
219   static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
220     AvailableValue Res;
221     Res.Val = MI;
222     Res.Kind = ValType::MemIntrin;
223     Res.Offset = Offset;
224     return Res;
225   }
226 
227   static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) {
228     AvailableValue Res;
229     Res.Val = Load;
230     Res.Kind = ValType::LoadVal;
231     Res.Offset = Offset;
232     return Res;
233   }
234 
235   static AvailableValue getUndef() {
236     AvailableValue Res;
237     Res.Val = nullptr;
238     Res.Kind = ValType::UndefVal;
239     Res.Offset = 0;
240     return Res;
241   }
242 
243   static AvailableValue getSelect(SelectInst *Sel, Value *V1, Value *V2) {
244     AvailableValue Res;
245     Res.Val = Sel;
246     Res.Kind = ValType::SelectVal;
247     Res.Offset = 0;
248     Res.V1 = V1;
249     Res.V2 = V2;
250     return Res;
251   }
252 
253   bool isSimpleValue() const { return Kind == ValType::SimpleVal; }
254   bool isCoercedLoadValue() const { return Kind == ValType::LoadVal; }
255   bool isMemIntrinValue() const { return Kind == ValType::MemIntrin; }
256   bool isUndefValue() const { return Kind == ValType::UndefVal; }
257   bool isSelectValue() const { return Kind == ValType::SelectVal; }
258 
259   Value *getSimpleValue() const {
260     assert(isSimpleValue() && "Wrong accessor");
261     return Val;
262   }
263 
264   LoadInst *getCoercedLoadValue() const {
265     assert(isCoercedLoadValue() && "Wrong accessor");
266     return cast<LoadInst>(Val);
267   }
268 
269   MemIntrinsic *getMemIntrinValue() const {
270     assert(isMemIntrinValue() && "Wrong accessor");
271     return cast<MemIntrinsic>(Val);
272   }
273 
274   SelectInst *getSelectValue() const {
275     assert(isSelectValue() && "Wrong accessor");
276     return cast<SelectInst>(Val);
277   }
278 
279   /// Emit code at the specified insertion point to adjust the value defined
280   /// here to the specified type. This handles various coercion cases.
281   Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt,
282                                   GVNPass &gvn) const;
283 };
284 
285 /// Represents an AvailableValue which can be rematerialized at the end of
286 /// the associated BasicBlock.
287 struct llvm::gvn::AvailableValueInBlock {
288   /// BB - The basic block in question.
289   BasicBlock *BB = nullptr;
290 
291   /// AV - The actual available value
292   AvailableValue AV;
293 
294   static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) {
295     AvailableValueInBlock Res;
296     Res.BB = BB;
297     Res.AV = std::move(AV);
298     return Res;
299   }
300 
301   static AvailableValueInBlock get(BasicBlock *BB, Value *V,
302                                    unsigned Offset = 0) {
303     return get(BB, AvailableValue::get(V, Offset));
304   }
305 
306   static AvailableValueInBlock getUndef(BasicBlock *BB) {
307     return get(BB, AvailableValue::getUndef());
308   }
309 
310   static AvailableValueInBlock getSelect(BasicBlock *BB, SelectInst *Sel,
311                                          Value *V1, Value *V2) {
312     return get(BB, AvailableValue::getSelect(Sel, V1, V2));
313   }
314 
315   /// Emit code at the end of this block to adjust the value defined here to
316   /// the specified type. This handles various coercion cases.
317   Value *MaterializeAdjustedValue(LoadInst *Load, GVNPass &gvn) const {
318     return AV.MaterializeAdjustedValue(Load, BB->getTerminator(), gvn);
319   }
320 };
321 
322 //===----------------------------------------------------------------------===//
323 //                     ValueTable Internal Functions
324 //===----------------------------------------------------------------------===//
325 
326 GVNPass::Expression GVNPass::ValueTable::createExpr(Instruction *I) {
327   Expression e;
328   e.type = I->getType();
329   e.opcode = I->getOpcode();
330   if (const GCRelocateInst *GCR = dyn_cast<GCRelocateInst>(I)) {
331     // gc.relocate is 'special' call: its second and third operands are
332     // not real values, but indices into statepoint's argument list.
333     // Use the refered to values for purposes of identity.
334     e.varargs.push_back(lookupOrAdd(GCR->getOperand(0)));
335     e.varargs.push_back(lookupOrAdd(GCR->getBasePtr()));
336     e.varargs.push_back(lookupOrAdd(GCR->getDerivedPtr()));
337   } else {
338     for (Use &Op : I->operands())
339       e.varargs.push_back(lookupOrAdd(Op));
340   }
341   if (I->isCommutative()) {
342     // Ensure that commutative instructions that only differ by a permutation
343     // of their operands get the same value number by sorting the operand value
344     // numbers.  Since commutative operands are the 1st two operands it is more
345     // efficient to sort by hand rather than using, say, std::sort.
346     assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!");
347     if (e.varargs[0] > e.varargs[1])
348       std::swap(e.varargs[0], e.varargs[1]);
349     e.commutative = true;
350   }
351 
352   if (auto *C = dyn_cast<CmpInst>(I)) {
353     // Sort the operand value numbers so x<y and y>x get the same value number.
354     CmpInst::Predicate Predicate = C->getPredicate();
355     if (e.varargs[0] > e.varargs[1]) {
356       std::swap(e.varargs[0], e.varargs[1]);
357       Predicate = CmpInst::getSwappedPredicate(Predicate);
358     }
359     e.opcode = (C->getOpcode() << 8) | Predicate;
360     e.commutative = true;
361   } else if (auto *E = dyn_cast<InsertValueInst>(I)) {
362     e.varargs.append(E->idx_begin(), E->idx_end());
363   } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
364     ArrayRef<int> ShuffleMask = SVI->getShuffleMask();
365     e.varargs.append(ShuffleMask.begin(), ShuffleMask.end());
366   }
367 
368   return e;
369 }
370 
371 GVNPass::Expression GVNPass::ValueTable::createCmpExpr(
372     unsigned Opcode, CmpInst::Predicate Predicate, Value *LHS, Value *RHS) {
373   assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
374          "Not a comparison!");
375   Expression e;
376   e.type = CmpInst::makeCmpResultType(LHS->getType());
377   e.varargs.push_back(lookupOrAdd(LHS));
378   e.varargs.push_back(lookupOrAdd(RHS));
379 
380   // Sort the operand value numbers so x<y and y>x get the same value number.
381   if (e.varargs[0] > e.varargs[1]) {
382     std::swap(e.varargs[0], e.varargs[1]);
383     Predicate = CmpInst::getSwappedPredicate(Predicate);
384   }
385   e.opcode = (Opcode << 8) | Predicate;
386   e.commutative = true;
387   return e;
388 }
389 
390 GVNPass::Expression
391 GVNPass::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
392   assert(EI && "Not an ExtractValueInst?");
393   Expression e;
394   e.type = EI->getType();
395   e.opcode = 0;
396 
397   WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
398   if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
399     // EI is an extract from one of our with.overflow intrinsics. Synthesize
400     // a semantically equivalent expression instead of an extract value
401     // expression.
402     e.opcode = WO->getBinaryOp();
403     e.varargs.push_back(lookupOrAdd(WO->getLHS()));
404     e.varargs.push_back(lookupOrAdd(WO->getRHS()));
405     return e;
406   }
407 
408   // Not a recognised intrinsic. Fall back to producing an extract value
409   // expression.
410   e.opcode = EI->getOpcode();
411   for (Use &Op : EI->operands())
412     e.varargs.push_back(lookupOrAdd(Op));
413 
414   append_range(e.varargs, EI->indices());
415 
416   return e;
417 }
418 
419 GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) {
420   Expression E;
421   Type *PtrTy = GEP->getType()->getScalarType();
422   const DataLayout &DL = GEP->getModule()->getDataLayout();
423   unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy);
424   MapVector<Value *, APInt> VariableOffsets;
425   APInt ConstantOffset(BitWidth, 0);
426   if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
427     // Convert into offset representation, to recognize equivalent address
428     // calculations that use different type encoding.
429     LLVMContext &Context = GEP->getContext();
430     E.opcode = GEP->getOpcode();
431     E.type = nullptr;
432     E.varargs.push_back(lookupOrAdd(GEP->getPointerOperand()));
433     for (const auto &Pair : VariableOffsets) {
434       E.varargs.push_back(lookupOrAdd(Pair.first));
435       E.varargs.push_back(lookupOrAdd(ConstantInt::get(Context, Pair.second)));
436     }
437     if (!ConstantOffset.isZero())
438       E.varargs.push_back(
439           lookupOrAdd(ConstantInt::get(Context, ConstantOffset)));
440   } else {
441     // If converting to offset representation fails (for scalable vectors),
442     // fall back to type-based implementation:
443     E.opcode = GEP->getOpcode();
444     E.type = GEP->getSourceElementType();
445     for (Use &Op : GEP->operands())
446       E.varargs.push_back(lookupOrAdd(Op));
447   }
448   return E;
449 }
450 
451 //===----------------------------------------------------------------------===//
452 //                     ValueTable External Functions
453 //===----------------------------------------------------------------------===//
454 
455 GVNPass::ValueTable::ValueTable() = default;
456 GVNPass::ValueTable::ValueTable(const ValueTable &) = default;
457 GVNPass::ValueTable::ValueTable(ValueTable &&) = default;
458 GVNPass::ValueTable::~ValueTable() = default;
459 GVNPass::ValueTable &
460 GVNPass::ValueTable::operator=(const GVNPass::ValueTable &Arg) = default;
461 
462 /// add - Insert a value into the table with a specified value number.
463 void GVNPass::ValueTable::add(Value *V, uint32_t num) {
464   valueNumbering.insert(std::make_pair(V, num));
465   if (PHINode *PN = dyn_cast<PHINode>(V))
466     NumberingPhi[num] = PN;
467 }
468 
469 uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
470   // FIXME: Currently the calls which may access the thread id may
471   // be considered as not accessing the memory. But this is
472   // problematic for coroutines, since coroutines may resume in a
473   // different thread. So we disable the optimization here for the
474   // correctness. However, it may block many other correct
475   // optimizations. Revert this one when we detect the memory
476   // accessing kind more precisely.
477   if (C->getFunction()->isPresplitCoroutine()) {
478     valueNumbering[C] = nextValueNumber;
479     return nextValueNumber++;
480   }
481 
482   // Do not combine convergent calls since they implicitly depend on the set of
483   // threads that is currently executing, and they might be in different basic
484   // blocks.
485   if (C->isConvergent()) {
486     valueNumbering[C] = nextValueNumber;
487     return nextValueNumber++;
488   }
489 
490   if (AA->doesNotAccessMemory(C)) {
491     Expression exp = createExpr(C);
492     uint32_t e = assignExpNewValueNum(exp).first;
493     valueNumbering[C] = e;
494     return e;
495   }
496 
497   if (MD && AA->onlyReadsMemory(C)) {
498     Expression exp = createExpr(C);
499     auto ValNum = assignExpNewValueNum(exp);
500     if (ValNum.second) {
501       valueNumbering[C] = ValNum.first;
502       return ValNum.first;
503     }
504 
505     MemDepResult local_dep = MD->getDependency(C);
506 
507     if (!local_dep.isDef() && !local_dep.isNonLocal()) {
508       valueNumbering[C] =  nextValueNumber;
509       return nextValueNumber++;
510     }
511 
512     if (local_dep.isDef()) {
513       // For masked load/store intrinsics, the local_dep may actually be
514       // a normal load or store instruction.
515       CallInst *local_cdep = dyn_cast<CallInst>(local_dep.getInst());
516 
517       if (!local_cdep || local_cdep->arg_size() != C->arg_size()) {
518         valueNumbering[C] = nextValueNumber;
519         return nextValueNumber++;
520       }
521 
522       for (unsigned i = 0, e = C->arg_size(); i < e; ++i) {
523         uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
524         uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
525         if (c_vn != cd_vn) {
526           valueNumbering[C] = nextValueNumber;
527           return nextValueNumber++;
528         }
529       }
530 
531       uint32_t v = lookupOrAdd(local_cdep);
532       valueNumbering[C] = v;
533       return v;
534     }
535 
536     // Non-local case.
537     const MemoryDependenceResults::NonLocalDepInfo &deps =
538         MD->getNonLocalCallDependency(C);
539     // FIXME: Move the checking logic to MemDep!
540     CallInst* cdep = nullptr;
541 
542     // Check to see if we have a single dominating call instruction that is
543     // identical to C.
544     for (const NonLocalDepEntry &I : deps) {
545       if (I.getResult().isNonLocal())
546         continue;
547 
548       // We don't handle non-definitions.  If we already have a call, reject
549       // instruction dependencies.
550       if (!I.getResult().isDef() || cdep != nullptr) {
551         cdep = nullptr;
552         break;
553       }
554 
555       CallInst *NonLocalDepCall = dyn_cast<CallInst>(I.getResult().getInst());
556       // FIXME: All duplicated with non-local case.
557       if (NonLocalDepCall && DT->properlyDominates(I.getBB(), C->getParent())) {
558         cdep = NonLocalDepCall;
559         continue;
560       }
561 
562       cdep = nullptr;
563       break;
564     }
565 
566     if (!cdep) {
567       valueNumbering[C] = nextValueNumber;
568       return nextValueNumber++;
569     }
570 
571     if (cdep->arg_size() != C->arg_size()) {
572       valueNumbering[C] = nextValueNumber;
573       return nextValueNumber++;
574     }
575     for (unsigned i = 0, e = C->arg_size(); i < e; ++i) {
576       uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
577       uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
578       if (c_vn != cd_vn) {
579         valueNumbering[C] = nextValueNumber;
580         return nextValueNumber++;
581       }
582     }
583 
584     uint32_t v = lookupOrAdd(cdep);
585     valueNumbering[C] = v;
586     return v;
587   }
588 
589   valueNumbering[C] = nextValueNumber;
590   return nextValueNumber++;
591 }
592 
593 /// Returns true if a value number exists for the specified value.
594 bool GVNPass::ValueTable::exists(Value *V) const {
595   return valueNumbering.count(V) != 0;
596 }
597 
598 /// lookup_or_add - Returns the value number for the specified value, assigning
599 /// it a new number if it did not have one before.
600 uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
601   DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
602   if (VI != valueNumbering.end())
603     return VI->second;
604 
605   auto *I = dyn_cast<Instruction>(V);
606   if (!I) {
607     valueNumbering[V] = nextValueNumber;
608     return nextValueNumber++;
609   }
610 
611   Expression exp;
612   switch (I->getOpcode()) {
613     case Instruction::Call:
614       return lookupOrAddCall(cast<CallInst>(I));
615     case Instruction::FNeg:
616     case Instruction::Add:
617     case Instruction::FAdd:
618     case Instruction::Sub:
619     case Instruction::FSub:
620     case Instruction::Mul:
621     case Instruction::FMul:
622     case Instruction::UDiv:
623     case Instruction::SDiv:
624     case Instruction::FDiv:
625     case Instruction::URem:
626     case Instruction::SRem:
627     case Instruction::FRem:
628     case Instruction::Shl:
629     case Instruction::LShr:
630     case Instruction::AShr:
631     case Instruction::And:
632     case Instruction::Or:
633     case Instruction::Xor:
634     case Instruction::ICmp:
635     case Instruction::FCmp:
636     case Instruction::Trunc:
637     case Instruction::ZExt:
638     case Instruction::SExt:
639     case Instruction::FPToUI:
640     case Instruction::FPToSI:
641     case Instruction::UIToFP:
642     case Instruction::SIToFP:
643     case Instruction::FPTrunc:
644     case Instruction::FPExt:
645     case Instruction::PtrToInt:
646     case Instruction::IntToPtr:
647     case Instruction::AddrSpaceCast:
648     case Instruction::BitCast:
649     case Instruction::Select:
650     case Instruction::Freeze:
651     case Instruction::ExtractElement:
652     case Instruction::InsertElement:
653     case Instruction::ShuffleVector:
654     case Instruction::InsertValue:
655       exp = createExpr(I);
656       break;
657     case Instruction::GetElementPtr:
658       exp = createGEPExpr(cast<GetElementPtrInst>(I));
659       break;
660     case Instruction::ExtractValue:
661       exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
662       break;
663     case Instruction::PHI:
664       valueNumbering[V] = nextValueNumber;
665       NumberingPhi[nextValueNumber] = cast<PHINode>(V);
666       return nextValueNumber++;
667     default:
668       valueNumbering[V] = nextValueNumber;
669       return nextValueNumber++;
670   }
671 
672   uint32_t e = assignExpNewValueNum(exp).first;
673   valueNumbering[V] = e;
674   return e;
675 }
676 
677 /// Returns the value number of the specified value. Fails if
678 /// the value has not yet been numbered.
679 uint32_t GVNPass::ValueTable::lookup(Value *V, bool Verify) const {
680   DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
681   if (Verify) {
682     assert(VI != valueNumbering.end() && "Value not numbered?");
683     return VI->second;
684   }
685   return (VI != valueNumbering.end()) ? VI->second : 0;
686 }
687 
688 /// Returns the value number of the given comparison,
689 /// assigning it a new number if it did not have one before.  Useful when
690 /// we deduced the result of a comparison, but don't immediately have an
691 /// instruction realizing that comparison to hand.
692 uint32_t GVNPass::ValueTable::lookupOrAddCmp(unsigned Opcode,
693                                              CmpInst::Predicate Predicate,
694                                              Value *LHS, Value *RHS) {
695   Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
696   return assignExpNewValueNum(exp).first;
697 }
698 
699 /// Remove all entries from the ValueTable.
700 void GVNPass::ValueTable::clear() {
701   valueNumbering.clear();
702   expressionNumbering.clear();
703   NumberingPhi.clear();
704   PhiTranslateTable.clear();
705   nextValueNumber = 1;
706   Expressions.clear();
707   ExprIdx.clear();
708   nextExprNumber = 0;
709 }
710 
711 /// Remove a value from the value numbering.
712 void GVNPass::ValueTable::erase(Value *V) {
713   uint32_t Num = valueNumbering.lookup(V);
714   valueNumbering.erase(V);
715   // If V is PHINode, V <--> value number is an one-to-one mapping.
716   if (isa<PHINode>(V))
717     NumberingPhi.erase(Num);
718 }
719 
720 /// verifyRemoved - Verify that the value is removed from all internal data
721 /// structures.
722 void GVNPass::ValueTable::verifyRemoved(const Value *V) const {
723   assert(!valueNumbering.contains(V) &&
724          "Inst still occurs in value numbering map!");
725 }
726 
727 //===----------------------------------------------------------------------===//
728 //                                GVN Pass
729 //===----------------------------------------------------------------------===//
730 
731 bool GVNPass::isPREEnabled() const {
732   return Options.AllowPRE.value_or(GVNEnablePRE);
733 }
734 
735 bool GVNPass::isLoadPREEnabled() const {
736   return Options.AllowLoadPRE.value_or(GVNEnableLoadPRE);
737 }
738 
739 bool GVNPass::isLoadInLoopPREEnabled() const {
740   return Options.AllowLoadInLoopPRE.value_or(GVNEnableLoadInLoopPRE);
741 }
742 
743 bool GVNPass::isLoadPRESplitBackedgeEnabled() const {
744   return Options.AllowLoadPRESplitBackedge.value_or(
745       GVNEnableSplitBackedgeInLoadPRE);
746 }
747 
748 bool GVNPass::isMemDepEnabled() const {
749   return Options.AllowMemDep.value_or(GVNEnableMemDep);
750 }
751 
752 PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
753   // FIXME: The order of evaluation of these 'getResult' calls is very
754   // significant! Re-ordering these variables will cause GVN when run alone to
755   // be less effective! We should fix memdep and basic-aa to not exhibit this
756   // behavior, but until then don't change the order here.
757   auto &AC = AM.getResult<AssumptionAnalysis>(F);
758   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
759   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
760   auto &AA = AM.getResult<AAManager>(F);
761   auto *MemDep =
762       isMemDepEnabled() ? &AM.getResult<MemoryDependenceAnalysis>(F) : nullptr;
763   auto *LI = AM.getCachedResult<LoopAnalysis>(F);
764   auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
765   auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
766   bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
767                          MSSA ? &MSSA->getMSSA() : nullptr);
768   if (!Changed)
769     return PreservedAnalyses::all();
770   PreservedAnalyses PA;
771   PA.preserve<DominatorTreeAnalysis>();
772   PA.preserve<TargetLibraryAnalysis>();
773   if (MSSA)
774     PA.preserve<MemorySSAAnalysis>();
775   if (LI)
776     PA.preserve<LoopAnalysis>();
777   return PA;
778 }
779 
780 void GVNPass::printPipeline(
781     raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
782   static_cast<PassInfoMixin<GVNPass> *>(this)->printPipeline(
783       OS, MapClassName2PassName);
784 
785   OS << '<';
786   if (Options.AllowPRE != std::nullopt)
787     OS << (*Options.AllowPRE ? "" : "no-") << "pre;";
788   if (Options.AllowLoadPRE != std::nullopt)
789     OS << (*Options.AllowLoadPRE ? "" : "no-") << "load-pre;";
790   if (Options.AllowLoadPRESplitBackedge != std::nullopt)
791     OS << (*Options.AllowLoadPRESplitBackedge ? "" : "no-")
792        << "split-backedge-load-pre;";
793   if (Options.AllowMemDep != std::nullopt)
794     OS << (*Options.AllowMemDep ? "" : "no-") << "memdep";
795   OS << '>';
796 }
797 
798 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
799 LLVM_DUMP_METHOD void GVNPass::dump(DenseMap<uint32_t, Value *> &d) const {
800   errs() << "{\n";
801   for (auto &I : d) {
802     errs() << I.first << "\n";
803     I.second->dump();
804   }
805   errs() << "}\n";
806 }
807 #endif
808 
809 enum class AvailabilityState : char {
810   /// We know the block *is not* fully available. This is a fixpoint.
811   Unavailable = 0,
812   /// We know the block *is* fully available. This is a fixpoint.
813   Available = 1,
814   /// We do not know whether the block is fully available or not,
815   /// but we are currently speculating that it will be.
816   /// If it would have turned out that the block was, in fact, not fully
817   /// available, this would have been cleaned up into an Unavailable.
818   SpeculativelyAvailable = 2,
819 };
820 
821 /// Return true if we can prove that the value
822 /// we're analyzing is fully available in the specified block.  As we go, keep
823 /// track of which blocks we know are fully alive in FullyAvailableBlocks.  This
824 /// map is actually a tri-state map with the following values:
825 ///   0) we know the block *is not* fully available.
826 ///   1) we know the block *is* fully available.
827 ///   2) we do not know whether the block is fully available or not, but we are
828 ///      currently speculating that it will be.
829 static bool IsValueFullyAvailableInBlock(
830     BasicBlock *BB,
831     DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) {
832   SmallVector<BasicBlock *, 32> Worklist;
833   std::optional<BasicBlock *> UnavailableBB;
834 
835   // The number of times we didn't find an entry for a block in a map and
836   // optimistically inserted an entry marking block as speculatively available.
837   unsigned NumNewNewSpeculativelyAvailableBBs = 0;
838 
839 #ifndef NDEBUG
840   SmallSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs;
841   SmallVector<BasicBlock *, 32> AvailableBBs;
842 #endif
843 
844   Worklist.emplace_back(BB);
845   while (!Worklist.empty()) {
846     BasicBlock *CurrBB = Worklist.pop_back_val(); // LoadFO - depth-first!
847     // Optimistically assume that the block is Speculatively Available and check
848     // to see if we already know about this block in one lookup.
849     std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV =
850         FullyAvailableBlocks.try_emplace(
851             CurrBB, AvailabilityState::SpeculativelyAvailable);
852     AvailabilityState &State = IV.first->second;
853 
854     // Did the entry already exist for this block?
855     if (!IV.second) {
856       if (State == AvailabilityState::Unavailable) {
857         UnavailableBB = CurrBB;
858         break; // Backpropagate unavailability info.
859       }
860 
861 #ifndef NDEBUG
862       AvailableBBs.emplace_back(CurrBB);
863 #endif
864       continue; // Don't recurse further, but continue processing worklist.
865     }
866 
867     // No entry found for block.
868     ++NumNewNewSpeculativelyAvailableBBs;
869     bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations;
870 
871     // If we have exhausted our budget, mark this block as unavailable.
872     // Also, if this block has no predecessors, the value isn't live-in here.
873     if (OutOfBudget || pred_empty(CurrBB)) {
874       MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget;
875       State = AvailabilityState::Unavailable;
876       UnavailableBB = CurrBB;
877       break; // Backpropagate unavailability info.
878     }
879 
880     // Tentatively consider this block as speculatively available.
881 #ifndef NDEBUG
882     NewSpeculativelyAvailableBBs.insert(CurrBB);
883 #endif
884     // And further recurse into block's predecessors, in depth-first order!
885     Worklist.append(pred_begin(CurrBB), pred_end(CurrBB));
886   }
887 
888 #if LLVM_ENABLE_STATS
889   IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax(
890       NumNewNewSpeculativelyAvailableBBs);
891 #endif
892 
893   // If the block isn't marked as fixpoint yet
894   // (the Unavailable and Available states are fixpoints)
895   auto MarkAsFixpointAndEnqueueSuccessors =
896       [&](BasicBlock *BB, AvailabilityState FixpointState) {
897         auto It = FullyAvailableBlocks.find(BB);
898         if (It == FullyAvailableBlocks.end())
899           return; // Never queried this block, leave as-is.
900         switch (AvailabilityState &State = It->second) {
901         case AvailabilityState::Unavailable:
902         case AvailabilityState::Available:
903           return; // Don't backpropagate further, continue processing worklist.
904         case AvailabilityState::SpeculativelyAvailable: // Fix it!
905           State = FixpointState;
906 #ifndef NDEBUG
907           assert(NewSpeculativelyAvailableBBs.erase(BB) &&
908                  "Found a speculatively available successor leftover?");
909 #endif
910           // Queue successors for further processing.
911           Worklist.append(succ_begin(BB), succ_end(BB));
912           return;
913         }
914       };
915 
916   if (UnavailableBB) {
917     // Okay, we have encountered an unavailable block.
918     // Mark speculatively available blocks reachable from UnavailableBB as
919     // unavailable as well. Paths are terminated when they reach blocks not in
920     // FullyAvailableBlocks or they are not marked as speculatively available.
921     Worklist.clear();
922     Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB));
923     while (!Worklist.empty())
924       MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
925                                          AvailabilityState::Unavailable);
926   }
927 
928 #ifndef NDEBUG
929   Worklist.clear();
930   for (BasicBlock *AvailableBB : AvailableBBs)
931     Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB));
932   while (!Worklist.empty())
933     MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
934                                        AvailabilityState::Available);
935 
936   assert(NewSpeculativelyAvailableBBs.empty() &&
937          "Must have fixed all the new speculatively available blocks.");
938 #endif
939 
940   return !UnavailableBB;
941 }
942 
943 /// If the specified OldValue exists in ValuesPerBlock, replace its value with
944 /// NewValue.
945 static void replaceValuesPerBlockEntry(
946     SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock, Value *OldValue,
947     Value *NewValue) {
948   for (AvailableValueInBlock &V : ValuesPerBlock) {
949     if (V.AV.Val == OldValue)
950       V.AV.Val = NewValue;
951     if (V.AV.isSelectValue()) {
952       if (V.AV.V1 == OldValue)
953         V.AV.V1 = NewValue;
954       if (V.AV.V2 == OldValue)
955         V.AV.V2 = NewValue;
956     }
957   }
958 }
959 
960 /// Given a set of loads specified by ValuesPerBlock,
961 /// construct SSA form, allowing us to eliminate Load.  This returns the value
962 /// that should be used at Load's definition site.
963 static Value *
964 ConstructSSAForLoadSet(LoadInst *Load,
965                        SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
966                        GVNPass &gvn) {
967   // Check for the fully redundant, dominating load case.  In this case, we can
968   // just use the dominating value directly.
969   if (ValuesPerBlock.size() == 1 &&
970       gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
971                                                Load->getParent())) {
972     assert(!ValuesPerBlock[0].AV.isUndefValue() &&
973            "Dead BB dominate this block");
974     return ValuesPerBlock[0].MaterializeAdjustedValue(Load, gvn);
975   }
976 
977   // Otherwise, we have to construct SSA form.
978   SmallVector<PHINode*, 8> NewPHIs;
979   SSAUpdater SSAUpdate(&NewPHIs);
980   SSAUpdate.Initialize(Load->getType(), Load->getName());
981 
982   for (const AvailableValueInBlock &AV : ValuesPerBlock) {
983     BasicBlock *BB = AV.BB;
984 
985     if (AV.AV.isUndefValue())
986       continue;
987 
988     if (SSAUpdate.HasValueForBlock(BB))
989       continue;
990 
991     // If the value is the load that we will be eliminating, and the block it's
992     // available in is the block that the load is in, then don't add it as
993     // SSAUpdater will resolve the value to the relevant phi which may let it
994     // avoid phi construction entirely if there's actually only one value.
995     if (BB == Load->getParent() &&
996         ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == Load) ||
997          (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == Load)))
998       continue;
999 
1000     SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(Load, gvn));
1001   }
1002 
1003   // Perform PHI construction.
1004   return SSAUpdate.GetValueInMiddleOfBlock(Load->getParent());
1005 }
1006 
1007 Value *AvailableValue::MaterializeAdjustedValue(LoadInst *Load,
1008                                                 Instruction *InsertPt,
1009                                                 GVNPass &gvn) const {
1010   Value *Res;
1011   Type *LoadTy = Load->getType();
1012   const DataLayout &DL = Load->getModule()->getDataLayout();
1013   if (isSimpleValue()) {
1014     Res = getSimpleValue();
1015     if (Res->getType() != LoadTy) {
1016       Res = getValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
1017 
1018       LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset
1019                         << "  " << *getSimpleValue() << '\n'
1020                         << *Res << '\n'
1021                         << "\n\n\n");
1022     }
1023   } else if (isCoercedLoadValue()) {
1024     LoadInst *CoercedLoad = getCoercedLoadValue();
1025     if (CoercedLoad->getType() == LoadTy && Offset == 0) {
1026       Res = CoercedLoad;
1027       combineMetadataForCSE(CoercedLoad, Load, false);
1028     } else {
1029       Res = getValueForLoad(CoercedLoad, Offset, LoadTy, InsertPt, DL);
1030       // We are adding a new user for this load, for which the original
1031       // metadata may not hold. Additionally, the new load may have a different
1032       // size and type, so their metadata cannot be combined in any
1033       // straightforward way.
1034       // Drop all metadata that is not known to cause immediate UB on violation,
1035       // unless the load has !noundef, in which case all metadata violations
1036       // will be promoted to UB.
1037       // TODO: We can combine noalias/alias.scope metadata here, because it is
1038       // independent of the load type.
1039       if (!CoercedLoad->hasMetadata(LLVMContext::MD_noundef))
1040         CoercedLoad->dropUnknownNonDebugMetadata(
1041             {LLVMContext::MD_dereferenceable,
1042              LLVMContext::MD_dereferenceable_or_null,
1043              LLVMContext::MD_invariant_load, LLVMContext::MD_invariant_group});
1044       LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset
1045                         << "  " << *getCoercedLoadValue() << '\n'
1046                         << *Res << '\n'
1047                         << "\n\n\n");
1048     }
1049   } else if (isMemIntrinValue()) {
1050     Res = getMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
1051                                  InsertPt, DL);
1052     LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
1053                       << "  " << *getMemIntrinValue() << '\n'
1054                       << *Res << '\n'
1055                       << "\n\n\n");
1056   } else if (isSelectValue()) {
1057     // Introduce a new value select for a load from an eligible pointer select.
1058     SelectInst *Sel = getSelectValue();
1059     assert(V1 && V2 && "both value operands of the select must be present");
1060     Res = SelectInst::Create(Sel->getCondition(), V1, V2, "", Sel);
1061   } else {
1062     llvm_unreachable("Should not materialize value from dead block");
1063   }
1064   assert(Res && "failed to materialize?");
1065   return Res;
1066 }
1067 
1068 static bool isLifetimeStart(const Instruction *Inst) {
1069   if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
1070     return II->getIntrinsicID() == Intrinsic::lifetime_start;
1071   return false;
1072 }
1073 
1074 /// Assuming To can be reached from both From and Between, does Between lie on
1075 /// every path from From to To?
1076 static bool liesBetween(const Instruction *From, Instruction *Between,
1077                         const Instruction *To, DominatorTree *DT) {
1078   if (From->getParent() == Between->getParent())
1079     return DT->dominates(From, Between);
1080   SmallSet<BasicBlock *, 1> Exclusion;
1081   Exclusion.insert(Between->getParent());
1082   return !isPotentiallyReachable(From, To, &Exclusion, DT);
1083 }
1084 
1085 /// Try to locate the three instruction involved in a missed
1086 /// load-elimination case that is due to an intervening store.
1087 static void reportMayClobberedLoad(LoadInst *Load, MemDepResult DepInfo,
1088                                    DominatorTree *DT,
1089                                    OptimizationRemarkEmitter *ORE) {
1090   using namespace ore;
1091 
1092   Instruction *OtherAccess = nullptr;
1093 
1094   OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", Load);
1095   R << "load of type " << NV("Type", Load->getType()) << " not eliminated"
1096     << setExtraArgs();
1097 
1098   for (auto *U : Load->getPointerOperand()->users()) {
1099     if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1100       auto *I = cast<Instruction>(U);
1101       if (I->getFunction() == Load->getFunction() && DT->dominates(I, Load)) {
1102         // Use the most immediately dominating value
1103         if (OtherAccess) {
1104           if (DT->dominates(OtherAccess, I))
1105             OtherAccess = I;
1106           else
1107             assert(U == OtherAccess || DT->dominates(I, OtherAccess));
1108         } else
1109           OtherAccess = I;
1110       }
1111     }
1112   }
1113 
1114   if (!OtherAccess) {
1115     // There is no dominating use, check if we can find a closest non-dominating
1116     // use that lies between any other potentially available use and Load.
1117     for (auto *U : Load->getPointerOperand()->users()) {
1118       if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) {
1119         auto *I = cast<Instruction>(U);
1120         if (I->getFunction() == Load->getFunction() &&
1121             isPotentiallyReachable(I, Load, nullptr, DT)) {
1122           if (OtherAccess) {
1123             if (liesBetween(OtherAccess, I, Load, DT)) {
1124               OtherAccess = I;
1125             } else if (!liesBetween(I, OtherAccess, Load, DT)) {
1126               // These uses are both partially available at Load were it not for
1127               // the clobber, but neither lies strictly after the other.
1128               OtherAccess = nullptr;
1129               break;
1130             } // else: keep current OtherAccess since it lies between U and Load
1131           } else {
1132             OtherAccess = I;
1133           }
1134         }
1135       }
1136     }
1137   }
1138 
1139   if (OtherAccess)
1140     R << " in favor of " << NV("OtherAccess", OtherAccess);
1141 
1142   R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
1143 
1144   ORE->emit(R);
1145 }
1146 
1147 // Find non-clobbered value for Loc memory location in extended basic block
1148 // (chain of basic blocks with single predecessors) starting From instruction.
1149 static Value *findDominatingValue(const MemoryLocation &Loc, Type *LoadTy,
1150                                   Instruction *From, AAResults *AA) {
1151   uint32_t NumVisitedInsts = 0;
1152   BasicBlock *FromBB = From->getParent();
1153   BatchAAResults BatchAA(*AA);
1154   for (BasicBlock *BB = FromBB; BB; BB = BB->getSinglePredecessor())
1155     for (auto I = BB == FromBB ? From->getReverseIterator() : BB->rbegin(),
1156               E = BB->rend();
1157          I != E; ++I) {
1158       // Stop the search if limit is reached.
1159       if (++NumVisitedInsts > MaxNumVisitedInsts)
1160         return nullptr;
1161       Instruction *Inst = &*I;
1162       if (isModSet(BatchAA.getModRefInfo(Inst, Loc)))
1163         return nullptr;
1164       if (auto *LI = dyn_cast<LoadInst>(Inst))
1165         if (LI->getPointerOperand() == Loc.Ptr && LI->getType() == LoadTy)
1166           return LI;
1167     }
1168   return nullptr;
1169 }
1170 
1171 std::optional<AvailableValue>
1172 GVNPass::AnalyzeLoadAvailability(LoadInst *Load, MemDepResult DepInfo,
1173                                  Value *Address) {
1174   assert(Load->isUnordered() && "rules below are incorrect for ordered access");
1175   assert(DepInfo.isLocal() && "expected a local dependence");
1176 
1177   Instruction *DepInst = DepInfo.getInst();
1178 
1179   const DataLayout &DL = Load->getModule()->getDataLayout();
1180   if (DepInfo.isClobber()) {
1181     // If the dependence is to a store that writes to a superset of the bits
1182     // read by the load, we can extract the bits we need for the load from the
1183     // stored value.
1184     if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
1185       // Can't forward from non-atomic to atomic without violating memory model.
1186       if (Address && Load->isAtomic() <= DepSI->isAtomic()) {
1187         int Offset =
1188             analyzeLoadFromClobberingStore(Load->getType(), Address, DepSI, DL);
1189         if (Offset != -1)
1190           return AvailableValue::get(DepSI->getValueOperand(), Offset);
1191       }
1192     }
1193 
1194     // Check to see if we have something like this:
1195     //    load i32* P
1196     //    load i8* (P+1)
1197     // if we have this, replace the later with an extraction from the former.
1198     if (LoadInst *DepLoad = dyn_cast<LoadInst>(DepInst)) {
1199       // If this is a clobber and L is the first instruction in its block, then
1200       // we have the first instruction in the entry block.
1201       // Can't forward from non-atomic to atomic without violating memory model.
1202       if (DepLoad != Load && Address &&
1203           Load->isAtomic() <= DepLoad->isAtomic()) {
1204         Type *LoadType = Load->getType();
1205         int Offset = -1;
1206 
1207         // If MD reported clobber, check it was nested.
1208         if (DepInfo.isClobber() &&
1209             canCoerceMustAliasedValueToLoad(DepLoad, LoadType, DL)) {
1210           const auto ClobberOff = MD->getClobberOffset(DepLoad);
1211           // GVN has no deal with a negative offset.
1212           Offset = (ClobberOff == std::nullopt || *ClobberOff < 0)
1213                        ? -1
1214                        : *ClobberOff;
1215         }
1216         if (Offset == -1)
1217           Offset =
1218               analyzeLoadFromClobberingLoad(LoadType, Address, DepLoad, DL);
1219         if (Offset != -1)
1220           return AvailableValue::getLoad(DepLoad, Offset);
1221       }
1222     }
1223 
1224     // If the clobbering value is a memset/memcpy/memmove, see if we can
1225     // forward a value on from it.
1226     if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
1227       if (Address && !Load->isAtomic()) {
1228         int Offset = analyzeLoadFromClobberingMemInst(Load->getType(), Address,
1229                                                       DepMI, DL);
1230         if (Offset != -1)
1231           return AvailableValue::getMI(DepMI, Offset);
1232       }
1233     }
1234 
1235     // Nothing known about this clobber, have to be conservative
1236     LLVM_DEBUG(
1237         // fast print dep, using operator<< on instruction is too slow.
1238         dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
1239         dbgs() << " is clobbered by " << *DepInst << '\n';);
1240     if (ORE->allowExtraAnalysis(DEBUG_TYPE))
1241       reportMayClobberedLoad(Load, DepInfo, DT, ORE);
1242 
1243     return std::nullopt;
1244   }
1245   assert(DepInfo.isDef() && "follows from above");
1246 
1247   // Loading the alloca -> undef.
1248   // Loading immediately after lifetime begin -> undef.
1249   if (isa<AllocaInst>(DepInst) || isLifetimeStart(DepInst))
1250     return AvailableValue::get(UndefValue::get(Load->getType()));
1251 
1252   if (Constant *InitVal =
1253           getInitialValueOfAllocation(DepInst, TLI, Load->getType()))
1254     return AvailableValue::get(InitVal);
1255 
1256   if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
1257     // Reject loads and stores that are to the same address but are of
1258     // different types if we have to. If the stored value is convertable to
1259     // the loaded value, we can reuse it.
1260     if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), Load->getType(),
1261                                          DL))
1262       return std::nullopt;
1263 
1264     // Can't forward from non-atomic to atomic without violating memory model.
1265     if (S->isAtomic() < Load->isAtomic())
1266       return std::nullopt;
1267 
1268     return AvailableValue::get(S->getValueOperand());
1269   }
1270 
1271   if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
1272     // If the types mismatch and we can't handle it, reject reuse of the load.
1273     // If the stored value is larger or equal to the loaded value, we can reuse
1274     // it.
1275     if (!canCoerceMustAliasedValueToLoad(LD, Load->getType(), DL))
1276       return std::nullopt;
1277 
1278     // Can't forward from non-atomic to atomic without violating memory model.
1279     if (LD->isAtomic() < Load->isAtomic())
1280       return std::nullopt;
1281 
1282     return AvailableValue::getLoad(LD);
1283   }
1284 
1285   // Check if load with Addr dependent from select can be converted to select
1286   // between load values. There must be no instructions between the found
1287   // loads and DepInst that may clobber the loads.
1288   if (auto *Sel = dyn_cast<SelectInst>(DepInst)) {
1289     assert(Sel->getType() == Load->getPointerOperandType());
1290     auto Loc = MemoryLocation::get(Load);
1291     Value *V1 =
1292         findDominatingValue(Loc.getWithNewPtr(Sel->getTrueValue()),
1293                             Load->getType(), DepInst, getAliasAnalysis());
1294     if (!V1)
1295       return std::nullopt;
1296     Value *V2 =
1297         findDominatingValue(Loc.getWithNewPtr(Sel->getFalseValue()),
1298                             Load->getType(), DepInst, getAliasAnalysis());
1299     if (!V2)
1300       return std::nullopt;
1301     return AvailableValue::getSelect(Sel, V1, V2);
1302   }
1303 
1304   // Unknown def - must be conservative
1305   LLVM_DEBUG(
1306       // fast print dep, using operator<< on instruction is too slow.
1307       dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
1308       dbgs() << " has unknown def " << *DepInst << '\n';);
1309   return std::nullopt;
1310 }
1311 
1312 void GVNPass::AnalyzeLoadAvailability(LoadInst *Load, LoadDepVect &Deps,
1313                                       AvailValInBlkVect &ValuesPerBlock,
1314                                       UnavailBlkVect &UnavailableBlocks) {
1315   // Filter out useless results (non-locals, etc).  Keep track of the blocks
1316   // where we have a value available in repl, also keep track of whether we see
1317   // dependencies that produce an unknown value for the load (such as a call
1318   // that could potentially clobber the load).
1319   for (const auto &Dep : Deps) {
1320     BasicBlock *DepBB = Dep.getBB();
1321     MemDepResult DepInfo = Dep.getResult();
1322 
1323     if (DeadBlocks.count(DepBB)) {
1324       // Dead dependent mem-op disguise as a load evaluating the same value
1325       // as the load in question.
1326       ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
1327       continue;
1328     }
1329 
1330     if (!DepInfo.isLocal()) {
1331       UnavailableBlocks.push_back(DepBB);
1332       continue;
1333     }
1334 
1335     // The address being loaded in this non-local block may not be the same as
1336     // the pointer operand of the load if PHI translation occurs.  Make sure
1337     // to consider the right address.
1338     if (auto AV = AnalyzeLoadAvailability(Load, DepInfo, Dep.getAddress())) {
1339       // subtlety: because we know this was a non-local dependency, we know
1340       // it's safe to materialize anywhere between the instruction within
1341       // DepInfo and the end of it's block.
1342       ValuesPerBlock.push_back(
1343           AvailableValueInBlock::get(DepBB, std::move(*AV)));
1344     } else {
1345       UnavailableBlocks.push_back(DepBB);
1346     }
1347   }
1348 
1349   assert(Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() &&
1350          "post condition violation");
1351 }
1352 
1353 /// Given the following code, v1 is partially available on some edges, but not
1354 /// available on the edge from PredBB. This function tries to find if there is
1355 /// another identical load in the other successor of PredBB.
1356 ///
1357 ///      v0 = load %addr
1358 ///      br %LoadBB
1359 ///
1360 ///   LoadBB:
1361 ///      v1 = load %addr
1362 ///      ...
1363 ///
1364 ///   PredBB:
1365 ///      ...
1366 ///      br %cond, label %LoadBB, label %SuccBB
1367 ///
1368 ///   SuccBB:
1369 ///      v2 = load %addr
1370 ///      ...
1371 ///
1372 LoadInst *GVNPass::findLoadToHoistIntoPred(BasicBlock *Pred, BasicBlock *LoadBB,
1373                                            LoadInst *Load) {
1374   // For simplicity we handle a Pred has 2 successors only.
1375   auto *Term = Pred->getTerminator();
1376   if (Term->getNumSuccessors() != 2 || Term->isExceptionalTerminator())
1377     return nullptr;
1378   auto *SuccBB = Term->getSuccessor(0);
1379   if (SuccBB == LoadBB)
1380     SuccBB = Term->getSuccessor(1);
1381   if (!SuccBB->getSinglePredecessor())
1382     return nullptr;
1383 
1384   unsigned int NumInsts = MaxNumInsnsPerBlock;
1385   for (Instruction &Inst : *SuccBB) {
1386     if (Inst.isDebugOrPseudoInst())
1387       continue;
1388     if (--NumInsts == 0)
1389       return nullptr;
1390 
1391     if (!Inst.isIdenticalTo(Load))
1392       continue;
1393 
1394     MemDepResult Dep = MD->getDependency(&Inst);
1395     // If an identical load doesn't depends on any local instructions, it can
1396     // be safely moved to PredBB.
1397     // Also check for the implicit control flow instructions. See the comments
1398     // in PerformLoadPRE for details.
1399     if (Dep.isNonLocal() && !ICF->isDominatedByICFIFromSameBlock(&Inst))
1400       return cast<LoadInst>(&Inst);
1401 
1402     // Otherwise there is something in the same BB clobbers the memory, we can't
1403     // move this and later load to PredBB.
1404     return nullptr;
1405   }
1406 
1407   return nullptr;
1408 }
1409 
1410 void GVNPass::eliminatePartiallyRedundantLoad(
1411     LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1412     MapVector<BasicBlock *, Value *> &AvailableLoads,
1413     MapVector<BasicBlock *, LoadInst *> *CriticalEdgePredAndLoad) {
1414   for (const auto &AvailableLoad : AvailableLoads) {
1415     BasicBlock *UnavailableBlock = AvailableLoad.first;
1416     Value *LoadPtr = AvailableLoad.second;
1417 
1418     auto *NewLoad =
1419         new LoadInst(Load->getType(), LoadPtr, Load->getName() + ".pre",
1420                      Load->isVolatile(), Load->getAlign(), Load->getOrdering(),
1421                      Load->getSyncScopeID(), UnavailableBlock->getTerminator());
1422     NewLoad->setDebugLoc(Load->getDebugLoc());
1423     if (MSSAU) {
1424       auto *MSSA = MSSAU->getMemorySSA();
1425       // Get the defining access of the original load or use the load if it is a
1426       // MemoryDef (e.g. because it is volatile). The inserted loads are
1427       // guaranteed to load from the same definition.
1428       auto *LoadAcc = MSSA->getMemoryAccess(Load);
1429       auto *DefiningAcc =
1430           isa<MemoryDef>(LoadAcc) ? LoadAcc : LoadAcc->getDefiningAccess();
1431       auto *NewAccess = MSSAU->createMemoryAccessInBB(
1432           NewLoad, DefiningAcc, NewLoad->getParent(),
1433           MemorySSA::BeforeTerminator);
1434       if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess))
1435         MSSAU->insertDef(NewDef, /*RenameUses=*/true);
1436       else
1437         MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true);
1438     }
1439 
1440     // Transfer the old load's AA tags to the new load.
1441     AAMDNodes Tags = Load->getAAMetadata();
1442     if (Tags)
1443       NewLoad->setAAMetadata(Tags);
1444 
1445     if (auto *MD = Load->getMetadata(LLVMContext::MD_invariant_load))
1446       NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
1447     if (auto *InvGroupMD = Load->getMetadata(LLVMContext::MD_invariant_group))
1448       NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
1449     if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range))
1450       NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
1451     if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group))
1452       if (LI &&
1453           LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
1454         NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD);
1455 
1456     // We do not propagate the old load's debug location, because the new
1457     // load now lives in a different BB, and we want to avoid a jumpy line
1458     // table.
1459     // FIXME: How do we retain source locations without causing poor debugging
1460     // behavior?
1461 
1462     // Add the newly created load.
1463     ValuesPerBlock.push_back(
1464         AvailableValueInBlock::get(UnavailableBlock, NewLoad));
1465     MD->invalidateCachedPointerInfo(LoadPtr);
1466     LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
1467 
1468     // For PredBB in CriticalEdgePredAndLoad we need to replace the uses of old
1469     // load instruction with the new created load instruction.
1470     if (CriticalEdgePredAndLoad) {
1471       auto I = CriticalEdgePredAndLoad->find(UnavailableBlock);
1472       if (I != CriticalEdgePredAndLoad->end()) {
1473         ++NumPRELoadMoved2CEPred;
1474         ICF->insertInstructionTo(NewLoad, UnavailableBlock);
1475         LoadInst *OldLoad = I->second;
1476         combineMetadataForCSE(NewLoad, OldLoad, false);
1477         OldLoad->replaceAllUsesWith(NewLoad);
1478         replaceValuesPerBlockEntry(ValuesPerBlock, OldLoad, NewLoad);
1479         if (uint32_t ValNo = VN.lookup(OldLoad, false))
1480           removeFromLeaderTable(ValNo, OldLoad, OldLoad->getParent());
1481         VN.erase(OldLoad);
1482         removeInstruction(OldLoad);
1483       }
1484     }
1485   }
1486 
1487   // Perform PHI construction.
1488   Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
1489   // ConstructSSAForLoadSet is responsible for combining metadata.
1490   Load->replaceAllUsesWith(V);
1491   if (isa<PHINode>(V))
1492     V->takeName(Load);
1493   if (Instruction *I = dyn_cast<Instruction>(V))
1494     I->setDebugLoc(Load->getDebugLoc());
1495   if (V->getType()->isPtrOrPtrVectorTy())
1496     MD->invalidateCachedPointerInfo(V);
1497   markInstructionForDeletion(Load);
1498   ORE->emit([&]() {
1499     return OptimizationRemark(DEBUG_TYPE, "LoadPRE", Load)
1500            << "load eliminated by PRE";
1501   });
1502 }
1503 
1504 bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
1505                              UnavailBlkVect &UnavailableBlocks) {
1506   // Okay, we have *some* definitions of the value.  This means that the value
1507   // is available in some of our (transitive) predecessors.  Lets think about
1508   // doing PRE of this load.  This will involve inserting a new load into the
1509   // predecessor when it's not available.  We could do this in general, but
1510   // prefer to not increase code size.  As such, we only do this when we know
1511   // that we only have to insert *one* load (which means we're basically moving
1512   // the load, not inserting a new one).
1513 
1514   SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
1515                                         UnavailableBlocks.end());
1516 
1517   // Let's find the first basic block with more than one predecessor.  Walk
1518   // backwards through predecessors if needed.
1519   BasicBlock *LoadBB = Load->getParent();
1520   BasicBlock *TmpBB = LoadBB;
1521 
1522   // Check that there is no implicit control flow instructions above our load in
1523   // its block. If there is an instruction that doesn't always pass the
1524   // execution to the following instruction, then moving through it may become
1525   // invalid. For example:
1526   //
1527   // int arr[LEN];
1528   // int index = ???;
1529   // ...
1530   // guard(0 <= index && index < LEN);
1531   // use(arr[index]);
1532   //
1533   // It is illegal to move the array access to any point above the guard,
1534   // because if the index is out of bounds we should deoptimize rather than
1535   // access the array.
1536   // Check that there is no guard in this block above our instruction.
1537   bool MustEnsureSafetyOfSpeculativeExecution =
1538       ICF->isDominatedByICFIFromSameBlock(Load);
1539 
1540   while (TmpBB->getSinglePredecessor()) {
1541     TmpBB = TmpBB->getSinglePredecessor();
1542     if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1543       return false;
1544     if (Blockers.count(TmpBB))
1545       return false;
1546 
1547     // If any of these blocks has more than one successor (i.e. if the edge we
1548     // just traversed was critical), then there are other paths through this
1549     // block along which the load may not be anticipated.  Hoisting the load
1550     // above this block would be adding the load to execution paths along
1551     // which it was not previously executed.
1552     if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1553       return false;
1554 
1555     // Check that there is no implicit control flow in a block above.
1556     MustEnsureSafetyOfSpeculativeExecution =
1557         MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB);
1558   }
1559 
1560   assert(TmpBB);
1561   LoadBB = TmpBB;
1562 
1563   // Check to see how many predecessors have the loaded value fully
1564   // available.
1565   MapVector<BasicBlock *, Value *> PredLoads;
1566   DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks;
1567   for (const AvailableValueInBlock &AV : ValuesPerBlock)
1568     FullyAvailableBlocks[AV.BB] = AvailabilityState::Available;
1569   for (BasicBlock *UnavailableBB : UnavailableBlocks)
1570     FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable;
1571 
1572   // The edge from Pred to LoadBB is a critical edge will be splitted.
1573   SmallVector<BasicBlock *, 4> CriticalEdgePredSplit;
1574   // The edge from Pred to LoadBB is a critical edge, another successor of Pred
1575   // contains a load can be moved to Pred. This data structure maps the Pred to
1576   // the movable load.
1577   MapVector<BasicBlock *, LoadInst *> CriticalEdgePredAndLoad;
1578   for (BasicBlock *Pred : predecessors(LoadBB)) {
1579     // If any predecessor block is an EH pad that does not allow non-PHI
1580     // instructions before the terminator, we can't PRE the load.
1581     if (Pred->getTerminator()->isEHPad()) {
1582       LLVM_DEBUG(
1583           dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
1584                  << Pred->getName() << "': " << *Load << '\n');
1585       return false;
1586     }
1587 
1588     if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) {
1589       continue;
1590     }
1591 
1592     if (Pred->getTerminator()->getNumSuccessors() != 1) {
1593       if (isa<IndirectBrInst>(Pred->getTerminator())) {
1594         LLVM_DEBUG(
1595             dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
1596                    << Pred->getName() << "': " << *Load << '\n');
1597         return false;
1598       }
1599 
1600       if (LoadBB->isEHPad()) {
1601         LLVM_DEBUG(
1602             dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
1603                    << Pred->getName() << "': " << *Load << '\n');
1604         return false;
1605       }
1606 
1607       // Do not split backedge as it will break the canonical loop form.
1608       if (!isLoadPRESplitBackedgeEnabled())
1609         if (DT->dominates(LoadBB, Pred)) {
1610           LLVM_DEBUG(
1611               dbgs()
1612               << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
1613               << Pred->getName() << "': " << *Load << '\n');
1614           return false;
1615         }
1616 
1617       if (LoadInst *LI = findLoadToHoistIntoPred(Pred, LoadBB, Load))
1618         CriticalEdgePredAndLoad[Pred] = LI;
1619       else
1620         CriticalEdgePredSplit.push_back(Pred);
1621     } else {
1622       // Only add the predecessors that will not be split for now.
1623       PredLoads[Pred] = nullptr;
1624     }
1625   }
1626 
1627   // Decide whether PRE is profitable for this load.
1628   unsigned NumInsertPreds = PredLoads.size() + CriticalEdgePredSplit.size();
1629   unsigned NumUnavailablePreds = NumInsertPreds +
1630       CriticalEdgePredAndLoad.size();
1631   assert(NumUnavailablePreds != 0 &&
1632          "Fully available value should already be eliminated!");
1633   (void)NumUnavailablePreds;
1634 
1635   // If we need to insert new load in multiple predecessors, reject it.
1636   // FIXME: If we could restructure the CFG, we could make a common pred with
1637   // all the preds that don't have an available Load and insert a new load into
1638   // that one block.
1639   if (NumInsertPreds > 1)
1640       return false;
1641 
1642   // Now we know where we will insert load. We must ensure that it is safe
1643   // to speculatively execute the load at that points.
1644   if (MustEnsureSafetyOfSpeculativeExecution) {
1645     if (CriticalEdgePredSplit.size())
1646       if (!isSafeToSpeculativelyExecute(Load, LoadBB->getFirstNonPHI(), AC, DT))
1647         return false;
1648     for (auto &PL : PredLoads)
1649       if (!isSafeToSpeculativelyExecute(Load, PL.first->getTerminator(), AC,
1650                                         DT))
1651         return false;
1652     for (auto &CEP : CriticalEdgePredAndLoad)
1653       if (!isSafeToSpeculativelyExecute(Load, CEP.first->getTerminator(), AC,
1654                                         DT))
1655         return false;
1656   }
1657 
1658   // Split critical edges, and update the unavailable predecessors accordingly.
1659   for (BasicBlock *OrigPred : CriticalEdgePredSplit) {
1660     BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
1661     assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
1662     PredLoads[NewPred] = nullptr;
1663     LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
1664                       << LoadBB->getName() << '\n');
1665   }
1666 
1667   for (auto &CEP : CriticalEdgePredAndLoad)
1668     PredLoads[CEP.first] = nullptr;
1669 
1670   // Check if the load can safely be moved to all the unavailable predecessors.
1671   bool CanDoPRE = true;
1672   const DataLayout &DL = Load->getModule()->getDataLayout();
1673   SmallVector<Instruction*, 8> NewInsts;
1674   for (auto &PredLoad : PredLoads) {
1675     BasicBlock *UnavailablePred = PredLoad.first;
1676 
1677     // Do PHI translation to get its value in the predecessor if necessary.  The
1678     // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
1679     // We do the translation for each edge we skipped by going from Load's block
1680     // to LoadBB, otherwise we might miss pieces needing translation.
1681 
1682     // If all preds have a single successor, then we know it is safe to insert
1683     // the load on the pred (?!?), so we can insert code to materialize the
1684     // pointer if it is not available.
1685     Value *LoadPtr = Load->getPointerOperand();
1686     BasicBlock *Cur = Load->getParent();
1687     while (Cur != LoadBB) {
1688       PHITransAddr Address(LoadPtr, DL, AC);
1689       LoadPtr = Address.translateWithInsertion(Cur, Cur->getSinglePredecessor(),
1690                                                *DT, NewInsts);
1691       if (!LoadPtr) {
1692         CanDoPRE = false;
1693         break;
1694       }
1695       Cur = Cur->getSinglePredecessor();
1696     }
1697 
1698     if (LoadPtr) {
1699       PHITransAddr Address(LoadPtr, DL, AC);
1700       LoadPtr = Address.translateWithInsertion(LoadBB, UnavailablePred, *DT,
1701                                                NewInsts);
1702     }
1703     // If we couldn't find or insert a computation of this phi translated value,
1704     // we fail PRE.
1705     if (!LoadPtr) {
1706       LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
1707                         << *Load->getPointerOperand() << "\n");
1708       CanDoPRE = false;
1709       break;
1710     }
1711 
1712     PredLoad.second = LoadPtr;
1713   }
1714 
1715   if (!CanDoPRE) {
1716     while (!NewInsts.empty()) {
1717       // Erase instructions generated by the failed PHI translation before
1718       // trying to number them. PHI translation might insert instructions
1719       // in basic blocks other than the current one, and we delete them
1720       // directly, as markInstructionForDeletion only allows removing from the
1721       // current basic block.
1722       NewInsts.pop_back_val()->eraseFromParent();
1723     }
1724     // HINT: Don't revert the edge-splitting as following transformation may
1725     // also need to split these critical edges.
1726     return !CriticalEdgePredSplit.empty();
1727   }
1728 
1729   // Okay, we can eliminate this load by inserting a reload in the predecessor
1730   // and using PHI construction to get the value in the other predecessors, do
1731   // it.
1732   LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *Load << '\n');
1733   LLVM_DEBUG(if (!NewInsts.empty()) dbgs() << "INSERTED " << NewInsts.size()
1734                                            << " INSTS: " << *NewInsts.back()
1735                                            << '\n');
1736 
1737   // Assign value numbers to the new instructions.
1738   for (Instruction *I : NewInsts) {
1739     // Instructions that have been inserted in predecessor(s) to materialize
1740     // the load address do not retain their original debug locations. Doing
1741     // so could lead to confusing (but correct) source attributions.
1742     I->updateLocationAfterHoist();
1743 
1744     // FIXME: We really _ought_ to insert these value numbers into their
1745     // parent's availability map.  However, in doing so, we risk getting into
1746     // ordering issues.  If a block hasn't been processed yet, we would be
1747     // marking a value as AVAIL-IN, which isn't what we intend.
1748     VN.lookupOrAdd(I);
1749   }
1750 
1751   eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, PredLoads,
1752                                   &CriticalEdgePredAndLoad);
1753   ++NumPRELoad;
1754   return true;
1755 }
1756 
1757 bool GVNPass::performLoopLoadPRE(LoadInst *Load,
1758                                  AvailValInBlkVect &ValuesPerBlock,
1759                                  UnavailBlkVect &UnavailableBlocks) {
1760   if (!LI)
1761     return false;
1762 
1763   const Loop *L = LI->getLoopFor(Load->getParent());
1764   // TODO: Generalize to other loop blocks that dominate the latch.
1765   if (!L || L->getHeader() != Load->getParent())
1766     return false;
1767 
1768   BasicBlock *Preheader = L->getLoopPreheader();
1769   BasicBlock *Latch = L->getLoopLatch();
1770   if (!Preheader || !Latch)
1771     return false;
1772 
1773   Value *LoadPtr = Load->getPointerOperand();
1774   // Must be available in preheader.
1775   if (!L->isLoopInvariant(LoadPtr))
1776     return false;
1777 
1778   // We plan to hoist the load to preheader without introducing a new fault.
1779   // In order to do it, we need to prove that we cannot side-exit the loop
1780   // once loop header is first entered before execution of the load.
1781   if (ICF->isDominatedByICFIFromSameBlock(Load))
1782     return false;
1783 
1784   BasicBlock *LoopBlock = nullptr;
1785   for (auto *Blocker : UnavailableBlocks) {
1786     // Blockers from outside the loop are handled in preheader.
1787     if (!L->contains(Blocker))
1788       continue;
1789 
1790     // Only allow one loop block. Loop header is not less frequently executed
1791     // than each loop block, and likely it is much more frequently executed. But
1792     // in case of multiple loop blocks, we need extra information (such as block
1793     // frequency info) to understand whether it is profitable to PRE into
1794     // multiple loop blocks.
1795     if (LoopBlock)
1796       return false;
1797 
1798     // Do not sink into inner loops. This may be non-profitable.
1799     if (L != LI->getLoopFor(Blocker))
1800       return false;
1801 
1802     // Blocks that dominate the latch execute on every single iteration, maybe
1803     // except the last one. So PREing into these blocks doesn't make much sense
1804     // in most cases. But the blocks that do not necessarily execute on each
1805     // iteration are sometimes much colder than the header, and this is when
1806     // PRE is potentially profitable.
1807     if (DT->dominates(Blocker, Latch))
1808       return false;
1809 
1810     // Make sure that the terminator itself doesn't clobber.
1811     if (Blocker->getTerminator()->mayWriteToMemory())
1812       return false;
1813 
1814     LoopBlock = Blocker;
1815   }
1816 
1817   if (!LoopBlock)
1818     return false;
1819 
1820   // Make sure the memory at this pointer cannot be freed, therefore we can
1821   // safely reload from it after clobber.
1822   if (LoadPtr->canBeFreed())
1823     return false;
1824 
1825   // TODO: Support critical edge splitting if blocker has more than 1 successor.
1826   MapVector<BasicBlock *, Value *> AvailableLoads;
1827   AvailableLoads[LoopBlock] = LoadPtr;
1828   AvailableLoads[Preheader] = LoadPtr;
1829 
1830   LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOOP LOAD: " << *Load << '\n');
1831   eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, AvailableLoads,
1832                                   /*CriticalEdgePredAndLoad*/ nullptr);
1833   ++NumPRELoopLoad;
1834   return true;
1835 }
1836 
1837 static void reportLoadElim(LoadInst *Load, Value *AvailableValue,
1838                            OptimizationRemarkEmitter *ORE) {
1839   using namespace ore;
1840 
1841   ORE->emit([&]() {
1842     return OptimizationRemark(DEBUG_TYPE, "LoadElim", Load)
1843            << "load of type " << NV("Type", Load->getType()) << " eliminated"
1844            << setExtraArgs() << " in favor of "
1845            << NV("InfavorOfValue", AvailableValue);
1846   });
1847 }
1848 
1849 /// Attempt to eliminate a load whose dependencies are
1850 /// non-local by performing PHI construction.
1851 bool GVNPass::processNonLocalLoad(LoadInst *Load) {
1852   // non-local speculations are not allowed under asan.
1853   if (Load->getParent()->getParent()->hasFnAttribute(
1854           Attribute::SanitizeAddress) ||
1855       Load->getParent()->getParent()->hasFnAttribute(
1856           Attribute::SanitizeHWAddress))
1857     return false;
1858 
1859   // Step 1: Find the non-local dependencies of the load.
1860   LoadDepVect Deps;
1861   MD->getNonLocalPointerDependency(Load, Deps);
1862 
1863   // If we had to process more than one hundred blocks to find the
1864   // dependencies, this load isn't worth worrying about.  Optimizing
1865   // it will be too expensive.
1866   unsigned NumDeps = Deps.size();
1867   if (NumDeps > MaxNumDeps)
1868     return false;
1869 
1870   // If we had a phi translation failure, we'll have a single entry which is a
1871   // clobber in the current block.  Reject this early.
1872   if (NumDeps == 1 &&
1873       !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
1874     LLVM_DEBUG(dbgs() << "GVN: non-local load "; Load->printAsOperand(dbgs());
1875                dbgs() << " has unknown dependencies\n";);
1876     return false;
1877   }
1878 
1879   bool Changed = false;
1880   // If this load follows a GEP, see if we can PRE the indices before analyzing.
1881   if (GetElementPtrInst *GEP =
1882           dyn_cast<GetElementPtrInst>(Load->getOperand(0))) {
1883     for (Use &U : GEP->indices())
1884       if (Instruction *I = dyn_cast<Instruction>(U.get()))
1885         Changed |= performScalarPRE(I);
1886   }
1887 
1888   // Step 2: Analyze the availability of the load
1889   AvailValInBlkVect ValuesPerBlock;
1890   UnavailBlkVect UnavailableBlocks;
1891   AnalyzeLoadAvailability(Load, Deps, ValuesPerBlock, UnavailableBlocks);
1892 
1893   // If we have no predecessors that produce a known value for this load, exit
1894   // early.
1895   if (ValuesPerBlock.empty())
1896     return Changed;
1897 
1898   // Step 3: Eliminate fully redundancy.
1899   //
1900   // If all of the instructions we depend on produce a known value for this
1901   // load, then it is fully redundant and we can use PHI insertion to compute
1902   // its value.  Insert PHIs and remove the fully redundant value now.
1903   if (UnavailableBlocks.empty()) {
1904     LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *Load << '\n');
1905 
1906     // Perform PHI construction.
1907     Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
1908     // ConstructSSAForLoadSet is responsible for combining metadata.
1909     Load->replaceAllUsesWith(V);
1910 
1911     if (isa<PHINode>(V))
1912       V->takeName(Load);
1913     if (Instruction *I = dyn_cast<Instruction>(V))
1914       // If instruction I has debug info, then we should not update it.
1915       // Also, if I has a null DebugLoc, then it is still potentially incorrect
1916       // to propagate Load's DebugLoc because Load may not post-dominate I.
1917       if (Load->getDebugLoc() && Load->getParent() == I->getParent())
1918         I->setDebugLoc(Load->getDebugLoc());
1919     if (V->getType()->isPtrOrPtrVectorTy())
1920       MD->invalidateCachedPointerInfo(V);
1921     markInstructionForDeletion(Load);
1922     ++NumGVNLoad;
1923     reportLoadElim(Load, V, ORE);
1924     return true;
1925   }
1926 
1927   // Step 4: Eliminate partial redundancy.
1928   if (!isPREEnabled() || !isLoadPREEnabled())
1929     return Changed;
1930   if (!isLoadInLoopPREEnabled() && LI && LI->getLoopFor(Load->getParent()))
1931     return Changed;
1932 
1933   if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) ||
1934       PerformLoadPRE(Load, ValuesPerBlock, UnavailableBlocks))
1935     return true;
1936 
1937   return Changed;
1938 }
1939 
1940 static bool impliesEquivalanceIfTrue(CmpInst* Cmp) {
1941   if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_EQ)
1942     return true;
1943 
1944   // Floating point comparisons can be equal, but not equivalent.  Cases:
1945   // NaNs for unordered operators
1946   // +0.0 vs 0.0 for all operators
1947   if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
1948       (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
1949        Cmp->getFastMathFlags().noNaNs())) {
1950       Value *LHS = Cmp->getOperand(0);
1951       Value *RHS = Cmp->getOperand(1);
1952       // If we can prove either side non-zero, then equality must imply
1953       // equivalence.
1954       // FIXME: We should do this optimization if 'no signed zeros' is
1955       // applicable via an instruction-level fast-math-flag or some other
1956       // indicator that relaxed FP semantics are being used.
1957       if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1958         return true;
1959       if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1960         return true;
1961       // TODO: Handle vector floating point constants
1962   }
1963   return false;
1964 }
1965 
1966 static bool impliesEquivalanceIfFalse(CmpInst* Cmp) {
1967   if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_NE)
1968     return true;
1969 
1970   // Floating point comparisons can be equal, but not equivelent.  Cases:
1971   // NaNs for unordered operators
1972   // +0.0 vs 0.0 for all operators
1973   if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE &&
1974        Cmp->getFastMathFlags().noNaNs()) ||
1975       Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) {
1976       Value *LHS = Cmp->getOperand(0);
1977       Value *RHS = Cmp->getOperand(1);
1978       // If we can prove either side non-zero, then equality must imply
1979       // equivalence.
1980       // FIXME: We should do this optimization if 'no signed zeros' is
1981       // applicable via an instruction-level fast-math-flag or some other
1982       // indicator that relaxed FP semantics are being used.
1983       if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
1984         return true;
1985       if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
1986         return true;
1987       // TODO: Handle vector floating point constants
1988   }
1989   return false;
1990 }
1991 
1992 
1993 static bool hasUsersIn(Value *V, BasicBlock *BB) {
1994   return llvm::any_of(V->users(), [BB](User *U) {
1995     auto *I = dyn_cast<Instruction>(U);
1996     return I && I->getParent() == BB;
1997   });
1998 }
1999 
2000 bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
2001   Value *V = IntrinsicI->getArgOperand(0);
2002 
2003   if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
2004     if (Cond->isZero()) {
2005       Type *Int8Ty = Type::getInt8Ty(V->getContext());
2006       // Insert a new store to null instruction before the load to indicate that
2007       // this code is not reachable.  FIXME: We could insert unreachable
2008       // instruction directly because we can modify the CFG.
2009       auto *NewS = new StoreInst(PoisonValue::get(Int8Ty),
2010                                  Constant::getNullValue(Int8Ty->getPointerTo()),
2011                                  IntrinsicI);
2012       if (MSSAU) {
2013         const MemoryUseOrDef *FirstNonDom = nullptr;
2014         const auto *AL =
2015             MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent());
2016 
2017         // If there are accesses in the current basic block, find the first one
2018         // that does not come before NewS. The new memory access is inserted
2019         // after the found access or before the terminator if no such access is
2020         // found.
2021         if (AL) {
2022           for (const auto &Acc : *AL) {
2023             if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc))
2024               if (!Current->getMemoryInst()->comesBefore(NewS)) {
2025                 FirstNonDom = Current;
2026                 break;
2027               }
2028           }
2029         }
2030 
2031         // This added store is to null, so it will never executed and we can
2032         // just use the LiveOnEntry def as defining access.
2033         auto *NewDef =
2034             FirstNonDom ? MSSAU->createMemoryAccessBefore(
2035                               NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
2036                               const_cast<MemoryUseOrDef *>(FirstNonDom))
2037                         : MSSAU->createMemoryAccessInBB(
2038                               NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
2039                               NewS->getParent(), MemorySSA::BeforeTerminator);
2040 
2041         MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false);
2042       }
2043     }
2044     if (isAssumeWithEmptyBundle(*IntrinsicI)) {
2045       markInstructionForDeletion(IntrinsicI);
2046       return true;
2047     }
2048     return false;
2049   }
2050 
2051   if (isa<Constant>(V)) {
2052     // If it's not false, and constant, it must evaluate to true. This means our
2053     // assume is assume(true), and thus, pointless, and we don't want to do
2054     // anything more here.
2055     return false;
2056   }
2057 
2058   Constant *True = ConstantInt::getTrue(V->getContext());
2059   bool Changed = false;
2060 
2061   for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
2062     BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
2063 
2064     // This property is only true in dominated successors, propagateEquality
2065     // will check dominance for us.
2066     Changed |= propagateEquality(V, True, Edge, false);
2067   }
2068 
2069   // We can replace assume value with true, which covers cases like this:
2070   // call void @llvm.assume(i1 %cmp)
2071   // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
2072   ReplaceOperandsWithMap[V] = True;
2073 
2074   // Similarly, after assume(!NotV) we know that NotV == false.
2075   Value *NotV;
2076   if (match(V, m_Not(m_Value(NotV))))
2077     ReplaceOperandsWithMap[NotV] = ConstantInt::getFalse(V->getContext());
2078 
2079   // If we find an equality fact, canonicalize all dominated uses in this block
2080   // to one of the two values.  We heuristically choice the "oldest" of the
2081   // two where age is determined by value number. (Note that propagateEquality
2082   // above handles the cross block case.)
2083   //
2084   // Key case to cover are:
2085   // 1)
2086   // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
2087   // call void @llvm.assume(i1 %cmp)
2088   // ret float %0 ; will change it to ret float 3.000000e+00
2089   // 2)
2090   // %load = load float, float* %addr
2091   // %cmp = fcmp oeq float %load, %0
2092   // call void @llvm.assume(i1 %cmp)
2093   // ret float %load ; will change it to ret float %0
2094   if (auto *CmpI = dyn_cast<CmpInst>(V)) {
2095     if (impliesEquivalanceIfTrue(CmpI)) {
2096       Value *CmpLHS = CmpI->getOperand(0);
2097       Value *CmpRHS = CmpI->getOperand(1);
2098       // Heuristically pick the better replacement -- the choice of heuristic
2099       // isn't terribly important here, but the fact we canonicalize on some
2100       // replacement is for exposing other simplifications.
2101       // TODO: pull this out as a helper function and reuse w/existing
2102       // (slightly different) logic.
2103       if (isa<Constant>(CmpLHS) && !isa<Constant>(CmpRHS))
2104         std::swap(CmpLHS, CmpRHS);
2105       if (!isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))
2106         std::swap(CmpLHS, CmpRHS);
2107       if ((isa<Argument>(CmpLHS) && isa<Argument>(CmpRHS)) ||
2108           (isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))) {
2109         // Move the 'oldest' value to the right-hand side, using the value
2110         // number as a proxy for age.
2111         uint32_t LVN = VN.lookupOrAdd(CmpLHS);
2112         uint32_t RVN = VN.lookupOrAdd(CmpRHS);
2113         if (LVN < RVN)
2114           std::swap(CmpLHS, CmpRHS);
2115       }
2116 
2117       // Handle degenerate case where we either haven't pruned a dead path or a
2118       // removed a trivial assume yet.
2119       if (isa<Constant>(CmpLHS) && isa<Constant>(CmpRHS))
2120         return Changed;
2121 
2122       LLVM_DEBUG(dbgs() << "Replacing dominated uses of "
2123                  << *CmpLHS << " with "
2124                  << *CmpRHS << " in block "
2125                  << IntrinsicI->getParent()->getName() << "\n");
2126 
2127 
2128       // Setup the replacement map - this handles uses within the same block
2129       if (hasUsersIn(CmpLHS, IntrinsicI->getParent()))
2130         ReplaceOperandsWithMap[CmpLHS] = CmpRHS;
2131 
2132       // NOTE: The non-block local cases are handled by the call to
2133       // propagateEquality above; this block is just about handling the block
2134       // local cases.  TODO: There's a bunch of logic in propagateEqualiy which
2135       // isn't duplicated for the block local case, can we share it somehow?
2136     }
2137   }
2138   return Changed;
2139 }
2140 
2141 static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
2142   patchReplacementInstruction(I, Repl);
2143   I->replaceAllUsesWith(Repl);
2144 }
2145 
2146 /// Attempt to eliminate a load, first by eliminating it
2147 /// locally, and then attempting non-local elimination if that fails.
2148 bool GVNPass::processLoad(LoadInst *L) {
2149   if (!MD)
2150     return false;
2151 
2152   // This code hasn't been audited for ordered or volatile memory access
2153   if (!L->isUnordered())
2154     return false;
2155 
2156   if (L->use_empty()) {
2157     markInstructionForDeletion(L);
2158     return true;
2159   }
2160 
2161   // ... to a pointer that has been loaded from before...
2162   MemDepResult Dep = MD->getDependency(L);
2163 
2164   // If it is defined in another block, try harder.
2165   if (Dep.isNonLocal())
2166     return processNonLocalLoad(L);
2167 
2168   // Only handle the local case below
2169   if (!Dep.isLocal()) {
2170     // This might be a NonFuncLocal or an Unknown
2171     LLVM_DEBUG(
2172         // fast print dep, using operator<< on instruction is too slow.
2173         dbgs() << "GVN: load "; L->printAsOperand(dbgs());
2174         dbgs() << " has unknown dependence\n";);
2175     return false;
2176   }
2177 
2178   auto AV = AnalyzeLoadAvailability(L, Dep, L->getPointerOperand());
2179   if (!AV)
2180     return false;
2181 
2182   Value *AvailableValue = AV->MaterializeAdjustedValue(L, L, *this);
2183 
2184   // MaterializeAdjustedValue is responsible for combining metadata.
2185   L->replaceAllUsesWith(AvailableValue);
2186   markInstructionForDeletion(L);
2187   if (MSSAU)
2188     MSSAU->removeMemoryAccess(L);
2189   ++NumGVNLoad;
2190   reportLoadElim(L, AvailableValue, ORE);
2191   // Tell MDA to reexamine the reused pointer since we might have more
2192   // information after forwarding it.
2193   if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
2194     MD->invalidateCachedPointerInfo(AvailableValue);
2195   return true;
2196 }
2197 
2198 /// Return a pair the first field showing the value number of \p Exp and the
2199 /// second field showing whether it is a value number newly created.
2200 std::pair<uint32_t, bool>
2201 GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) {
2202   uint32_t &e = expressionNumbering[Exp];
2203   bool CreateNewValNum = !e;
2204   if (CreateNewValNum) {
2205     Expressions.push_back(Exp);
2206     if (ExprIdx.size() < nextValueNumber + 1)
2207       ExprIdx.resize(nextValueNumber * 2);
2208     e = nextValueNumber;
2209     ExprIdx[nextValueNumber++] = nextExprNumber++;
2210   }
2211   return {e, CreateNewValNum};
2212 }
2213 
2214 /// Return whether all the values related with the same \p num are
2215 /// defined in \p BB.
2216 bool GVNPass::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
2217                                          GVNPass &Gvn) {
2218   LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
2219   while (Vals && Vals->BB == BB)
2220     Vals = Vals->Next;
2221   return !Vals;
2222 }
2223 
2224 /// Wrap phiTranslateImpl to provide caching functionality.
2225 uint32_t GVNPass::ValueTable::phiTranslate(const BasicBlock *Pred,
2226                                            const BasicBlock *PhiBlock,
2227                                            uint32_t Num, GVNPass &Gvn) {
2228   auto FindRes = PhiTranslateTable.find({Num, Pred});
2229   if (FindRes != PhiTranslateTable.end())
2230     return FindRes->second;
2231   uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, Gvn);
2232   PhiTranslateTable.insert({{Num, Pred}, NewNum});
2233   return NewNum;
2234 }
2235 
2236 // Return true if the value number \p Num and NewNum have equal value.
2237 // Return false if the result is unknown.
2238 bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2239                                            const BasicBlock *Pred,
2240                                            const BasicBlock *PhiBlock,
2241                                            GVNPass &Gvn) {
2242   CallInst *Call = nullptr;
2243   LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
2244   while (Vals) {
2245     Call = dyn_cast<CallInst>(Vals->Val);
2246     if (Call && Call->getParent() == PhiBlock)
2247       break;
2248     Vals = Vals->Next;
2249   }
2250 
2251   if (AA->doesNotAccessMemory(Call))
2252     return true;
2253 
2254   if (!MD || !AA->onlyReadsMemory(Call))
2255     return false;
2256 
2257   MemDepResult local_dep = MD->getDependency(Call);
2258   if (!local_dep.isNonLocal())
2259     return false;
2260 
2261   const MemoryDependenceResults::NonLocalDepInfo &deps =
2262       MD->getNonLocalCallDependency(Call);
2263 
2264   // Check to see if the Call has no function local clobber.
2265   for (const NonLocalDepEntry &D : deps) {
2266     if (D.getResult().isNonFuncLocal())
2267       return true;
2268   }
2269   return false;
2270 }
2271 
2272 /// Translate value number \p Num using phis, so that it has the values of
2273 /// the phis in BB.
2274 uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
2275                                                const BasicBlock *PhiBlock,
2276                                                uint32_t Num, GVNPass &Gvn) {
2277   if (PHINode *PN = NumberingPhi[Num]) {
2278     for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
2279       if (PN->getParent() == PhiBlock && PN->getIncomingBlock(i) == Pred)
2280         if (uint32_t TransVal = lookup(PN->getIncomingValue(i), false))
2281           return TransVal;
2282     }
2283     return Num;
2284   }
2285 
2286   // If there is any value related with Num is defined in a BB other than
2287   // PhiBlock, it cannot depend on a phi in PhiBlock without going through
2288   // a backedge. We can do an early exit in that case to save compile time.
2289   if (!areAllValsInBB(Num, PhiBlock, Gvn))
2290     return Num;
2291 
2292   if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
2293     return Num;
2294   Expression Exp = Expressions[ExprIdx[Num]];
2295 
2296   for (unsigned i = 0; i < Exp.varargs.size(); i++) {
2297     // For InsertValue and ExtractValue, some varargs are index numbers
2298     // instead of value numbers. Those index numbers should not be
2299     // translated.
2300     if ((i > 1 && Exp.opcode == Instruction::InsertValue) ||
2301         (i > 0 && Exp.opcode == Instruction::ExtractValue) ||
2302         (i > 1 && Exp.opcode == Instruction::ShuffleVector))
2303       continue;
2304     Exp.varargs[i] = phiTranslate(Pred, PhiBlock, Exp.varargs[i], Gvn);
2305   }
2306 
2307   if (Exp.commutative) {
2308     assert(Exp.varargs.size() >= 2 && "Unsupported commutative instruction!");
2309     if (Exp.varargs[0] > Exp.varargs[1]) {
2310       std::swap(Exp.varargs[0], Exp.varargs[1]);
2311       uint32_t Opcode = Exp.opcode >> 8;
2312       if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
2313         Exp.opcode = (Opcode << 8) |
2314                      CmpInst::getSwappedPredicate(
2315                          static_cast<CmpInst::Predicate>(Exp.opcode & 255));
2316     }
2317   }
2318 
2319   if (uint32_t NewNum = expressionNumbering[Exp]) {
2320     if (Exp.opcode == Instruction::Call && NewNum != Num)
2321       return areCallValsEqual(Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num;
2322     return NewNum;
2323   }
2324   return Num;
2325 }
2326 
2327 /// Erase stale entry from phiTranslate cache so phiTranslate can be computed
2328 /// again.
2329 void GVNPass::ValueTable::eraseTranslateCacheEntry(
2330     uint32_t Num, const BasicBlock &CurrBlock) {
2331   for (const BasicBlock *Pred : predecessors(&CurrBlock))
2332     PhiTranslateTable.erase({Num, Pred});
2333 }
2334 
2335 // In order to find a leader for a given value number at a
2336 // specific basic block, we first obtain the list of all Values for that number,
2337 // and then scan the list to find one whose block dominates the block in
2338 // question.  This is fast because dominator tree queries consist of only
2339 // a few comparisons of DFS numbers.
2340 Value *GVNPass::findLeader(const BasicBlock *BB, uint32_t num) {
2341   LeaderTableEntry Vals = LeaderTable[num];
2342   if (!Vals.Val) return nullptr;
2343 
2344   Value *Val = nullptr;
2345   if (DT->dominates(Vals.BB, BB)) {
2346     Val = Vals.Val;
2347     if (isa<Constant>(Val)) return Val;
2348   }
2349 
2350   LeaderTableEntry* Next = Vals.Next;
2351   while (Next) {
2352     if (DT->dominates(Next->BB, BB)) {
2353       if (isa<Constant>(Next->Val)) return Next->Val;
2354       if (!Val) Val = Next->Val;
2355     }
2356 
2357     Next = Next->Next;
2358   }
2359 
2360   return Val;
2361 }
2362 
2363 /// There is an edge from 'Src' to 'Dst'.  Return
2364 /// true if every path from the entry block to 'Dst' passes via this edge.  In
2365 /// particular 'Dst' must not be reachable via another edge from 'Src'.
2366 static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
2367                                        DominatorTree *DT) {
2368   // While in theory it is interesting to consider the case in which Dst has
2369   // more than one predecessor, because Dst might be part of a loop which is
2370   // only reachable from Src, in practice it is pointless since at the time
2371   // GVN runs all such loops have preheaders, which means that Dst will have
2372   // been changed to have only one predecessor, namely Src.
2373   const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
2374   assert((!Pred || Pred == E.getStart()) &&
2375          "No edge between these basic blocks!");
2376   return Pred != nullptr;
2377 }
2378 
2379 void GVNPass::assignBlockRPONumber(Function &F) {
2380   BlockRPONumber.clear();
2381   uint32_t NextBlockNumber = 1;
2382   ReversePostOrderTraversal<Function *> RPOT(&F);
2383   for (BasicBlock *BB : RPOT)
2384     BlockRPONumber[BB] = NextBlockNumber++;
2385   InvalidBlockRPONumbers = false;
2386 }
2387 
2388 bool GVNPass::replaceOperandsForInBlockEquality(Instruction *Instr) const {
2389   bool Changed = false;
2390   for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
2391     Value *Operand = Instr->getOperand(OpNum);
2392     auto it = ReplaceOperandsWithMap.find(Operand);
2393     if (it != ReplaceOperandsWithMap.end()) {
2394       LLVM_DEBUG(dbgs() << "GVN replacing: " << *Operand << " with "
2395                         << *it->second << " in instruction " << *Instr << '\n');
2396       Instr->setOperand(OpNum, it->second);
2397       Changed = true;
2398     }
2399   }
2400   return Changed;
2401 }
2402 
2403 /// The given values are known to be equal in every block
2404 /// dominated by 'Root'.  Exploit this, for example by replacing 'LHS' with
2405 /// 'RHS' everywhere in the scope.  Returns whether a change was made.
2406 /// If DominatesByEdge is false, then it means that we will propagate the RHS
2407 /// value starting from the end of Root.Start.
2408 bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
2409                                 const BasicBlockEdge &Root,
2410                                 bool DominatesByEdge) {
2411   SmallVector<std::pair<Value*, Value*>, 4> Worklist;
2412   Worklist.push_back(std::make_pair(LHS, RHS));
2413   bool Changed = false;
2414   // For speed, compute a conservative fast approximation to
2415   // DT->dominates(Root, Root.getEnd());
2416   const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
2417 
2418   while (!Worklist.empty()) {
2419     std::pair<Value*, Value*> Item = Worklist.pop_back_val();
2420     LHS = Item.first; RHS = Item.second;
2421 
2422     if (LHS == RHS)
2423       continue;
2424     assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
2425 
2426     // Don't try to propagate equalities between constants.
2427     if (isa<Constant>(LHS) && isa<Constant>(RHS))
2428       continue;
2429 
2430     // Prefer a constant on the right-hand side, or an Argument if no constants.
2431     if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
2432       std::swap(LHS, RHS);
2433     assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
2434 
2435     // If there is no obvious reason to prefer the left-hand side over the
2436     // right-hand side, ensure the longest lived term is on the right-hand side,
2437     // so the shortest lived term will be replaced by the longest lived.
2438     // This tends to expose more simplifications.
2439     uint32_t LVN = VN.lookupOrAdd(LHS);
2440     if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
2441         (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
2442       // Move the 'oldest' value to the right-hand side, using the value number
2443       // as a proxy for age.
2444       uint32_t RVN = VN.lookupOrAdd(RHS);
2445       if (LVN < RVN) {
2446         std::swap(LHS, RHS);
2447         LVN = RVN;
2448       }
2449     }
2450 
2451     // If value numbering later sees that an instruction in the scope is equal
2452     // to 'LHS' then ensure it will be turned into 'RHS'.  In order to preserve
2453     // the invariant that instructions only occur in the leader table for their
2454     // own value number (this is used by removeFromLeaderTable), do not do this
2455     // if RHS is an instruction (if an instruction in the scope is morphed into
2456     // LHS then it will be turned into RHS by the next GVN iteration anyway, so
2457     // using the leader table is about compiling faster, not optimizing better).
2458     // The leader table only tracks basic blocks, not edges. Only add to if we
2459     // have the simple case where the edge dominates the end.
2460     if (RootDominatesEnd && !isa<Instruction>(RHS))
2461       addToLeaderTable(LVN, RHS, Root.getEnd());
2462 
2463     // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope.  As
2464     // LHS always has at least one use that is not dominated by Root, this will
2465     // never do anything if LHS has only one use.
2466     if (!LHS->hasOneUse()) {
2467       unsigned NumReplacements =
2468           DominatesByEdge
2469               ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
2470               : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
2471 
2472       Changed |= NumReplacements > 0;
2473       NumGVNEqProp += NumReplacements;
2474       // Cached information for anything that uses LHS will be invalid.
2475       if (MD)
2476         MD->invalidateCachedPointerInfo(LHS);
2477     }
2478 
2479     // Now try to deduce additional equalities from this one. For example, if
2480     // the known equality was "(A != B)" == "false" then it follows that A and B
2481     // are equal in the scope. Only boolean equalities with an explicit true or
2482     // false RHS are currently supported.
2483     if (!RHS->getType()->isIntegerTy(1))
2484       // Not a boolean equality - bail out.
2485       continue;
2486     ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
2487     if (!CI)
2488       // RHS neither 'true' nor 'false' - bail out.
2489       continue;
2490     // Whether RHS equals 'true'.  Otherwise it equals 'false'.
2491     bool isKnownTrue = CI->isMinusOne();
2492     bool isKnownFalse = !isKnownTrue;
2493 
2494     // If "A && B" is known true then both A and B are known true.  If "A || B"
2495     // is known false then both A and B are known false.
2496     Value *A, *B;
2497     if ((isKnownTrue && match(LHS, m_LogicalAnd(m_Value(A), m_Value(B)))) ||
2498         (isKnownFalse && match(LHS, m_LogicalOr(m_Value(A), m_Value(B))))) {
2499       Worklist.push_back(std::make_pair(A, RHS));
2500       Worklist.push_back(std::make_pair(B, RHS));
2501       continue;
2502     }
2503 
2504     // If we are propagating an equality like "(A == B)" == "true" then also
2505     // propagate the equality A == B.  When propagating a comparison such as
2506     // "(A >= B)" == "true", replace all instances of "A < B" with "false".
2507     if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
2508       Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
2509 
2510       // If "A == B" is known true, or "A != B" is known false, then replace
2511       // A with B everywhere in the scope.  For floating point operations, we
2512       // have to be careful since equality does not always imply equivalance.
2513       if ((isKnownTrue && impliesEquivalanceIfTrue(Cmp)) ||
2514           (isKnownFalse && impliesEquivalanceIfFalse(Cmp)))
2515         Worklist.push_back(std::make_pair(Op0, Op1));
2516 
2517       // If "A >= B" is known true, replace "A < B" with false everywhere.
2518       CmpInst::Predicate NotPred = Cmp->getInversePredicate();
2519       Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
2520       // Since we don't have the instruction "A < B" immediately to hand, work
2521       // out the value number that it would have and use that to find an
2522       // appropriate instruction (if any).
2523       uint32_t NextNum = VN.getNextUnusedValueNumber();
2524       uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
2525       // If the number we were assigned was brand new then there is no point in
2526       // looking for an instruction realizing it: there cannot be one!
2527       if (Num < NextNum) {
2528         Value *NotCmp = findLeader(Root.getEnd(), Num);
2529         if (NotCmp && isa<Instruction>(NotCmp)) {
2530           unsigned NumReplacements =
2531               DominatesByEdge
2532                   ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
2533                   : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
2534                                              Root.getStart());
2535           Changed |= NumReplacements > 0;
2536           NumGVNEqProp += NumReplacements;
2537           // Cached information for anything that uses NotCmp will be invalid.
2538           if (MD)
2539             MD->invalidateCachedPointerInfo(NotCmp);
2540         }
2541       }
2542       // Ensure that any instruction in scope that gets the "A < B" value number
2543       // is replaced with false.
2544       // The leader table only tracks basic blocks, not edges. Only add to if we
2545       // have the simple case where the edge dominates the end.
2546       if (RootDominatesEnd)
2547         addToLeaderTable(Num, NotVal, Root.getEnd());
2548 
2549       continue;
2550     }
2551   }
2552 
2553   return Changed;
2554 }
2555 
2556 /// When calculating availability, handle an instruction
2557 /// by inserting it into the appropriate sets
2558 bool GVNPass::processInstruction(Instruction *I) {
2559   // Ignore dbg info intrinsics.
2560   if (isa<DbgInfoIntrinsic>(I))
2561     return false;
2562 
2563   // If the instruction can be easily simplified then do so now in preference
2564   // to value numbering it.  Value numbering often exposes redundancies, for
2565   // example if it determines that %y is equal to %x then the instruction
2566   // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
2567   const DataLayout &DL = I->getModule()->getDataLayout();
2568   if (Value *V = simplifyInstruction(I, {DL, TLI, DT, AC})) {
2569     bool Changed = false;
2570     if (!I->use_empty()) {
2571       // Simplification can cause a special instruction to become not special.
2572       // For example, devirtualization to a willreturn function.
2573       ICF->removeUsersOf(I);
2574       I->replaceAllUsesWith(V);
2575       Changed = true;
2576     }
2577     if (isInstructionTriviallyDead(I, TLI)) {
2578       markInstructionForDeletion(I);
2579       Changed = true;
2580     }
2581     if (Changed) {
2582       if (MD && V->getType()->isPtrOrPtrVectorTy())
2583         MD->invalidateCachedPointerInfo(V);
2584       ++NumGVNSimpl;
2585       return true;
2586     }
2587   }
2588 
2589   if (auto *Assume = dyn_cast<AssumeInst>(I))
2590     return processAssumeIntrinsic(Assume);
2591 
2592   if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
2593     if (processLoad(Load))
2594       return true;
2595 
2596     unsigned Num = VN.lookupOrAdd(Load);
2597     addToLeaderTable(Num, Load, Load->getParent());
2598     return false;
2599   }
2600 
2601   // For conditional branches, we can perform simple conditional propagation on
2602   // the condition value itself.
2603   if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
2604     if (!BI->isConditional())
2605       return false;
2606 
2607     if (isa<Constant>(BI->getCondition()))
2608       return processFoldableCondBr(BI);
2609 
2610     Value *BranchCond = BI->getCondition();
2611     BasicBlock *TrueSucc = BI->getSuccessor(0);
2612     BasicBlock *FalseSucc = BI->getSuccessor(1);
2613     // Avoid multiple edges early.
2614     if (TrueSucc == FalseSucc)
2615       return false;
2616 
2617     BasicBlock *Parent = BI->getParent();
2618     bool Changed = false;
2619 
2620     Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
2621     BasicBlockEdge TrueE(Parent, TrueSucc);
2622     Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
2623 
2624     Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
2625     BasicBlockEdge FalseE(Parent, FalseSucc);
2626     Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
2627 
2628     return Changed;
2629   }
2630 
2631   // For switches, propagate the case values into the case destinations.
2632   if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
2633     Value *SwitchCond = SI->getCondition();
2634     BasicBlock *Parent = SI->getParent();
2635     bool Changed = false;
2636 
2637     // Remember how many outgoing edges there are to every successor.
2638     SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
2639     for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
2640       ++SwitchEdges[SI->getSuccessor(i)];
2641 
2642     for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
2643          i != e; ++i) {
2644       BasicBlock *Dst = i->getCaseSuccessor();
2645       // If there is only a single edge, propagate the case value into it.
2646       if (SwitchEdges.lookup(Dst) == 1) {
2647         BasicBlockEdge E(Parent, Dst);
2648         Changed |= propagateEquality(SwitchCond, i->getCaseValue(), E, true);
2649       }
2650     }
2651     return Changed;
2652   }
2653 
2654   // Instructions with void type don't return a value, so there's
2655   // no point in trying to find redundancies in them.
2656   if (I->getType()->isVoidTy())
2657     return false;
2658 
2659   uint32_t NextNum = VN.getNextUnusedValueNumber();
2660   unsigned Num = VN.lookupOrAdd(I);
2661 
2662   // Allocations are always uniquely numbered, so we can save time and memory
2663   // by fast failing them.
2664   if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
2665     addToLeaderTable(Num, I, I->getParent());
2666     return false;
2667   }
2668 
2669   // If the number we were assigned was a brand new VN, then we don't
2670   // need to do a lookup to see if the number already exists
2671   // somewhere in the domtree: it can't!
2672   if (Num >= NextNum) {
2673     addToLeaderTable(Num, I, I->getParent());
2674     return false;
2675   }
2676 
2677   // Perform fast-path value-number based elimination of values inherited from
2678   // dominators.
2679   Value *Repl = findLeader(I->getParent(), Num);
2680   if (!Repl) {
2681     // Failure, just remember this instance for future use.
2682     addToLeaderTable(Num, I, I->getParent());
2683     return false;
2684   }
2685 
2686   if (Repl == I) {
2687     // If I was the result of a shortcut PRE, it might already be in the table
2688     // and the best replacement for itself. Nothing to do.
2689     return false;
2690   }
2691 
2692   // Remove it!
2693   patchAndReplaceAllUsesWith(I, Repl);
2694   if (MD && Repl->getType()->isPtrOrPtrVectorTy())
2695     MD->invalidateCachedPointerInfo(Repl);
2696   markInstructionForDeletion(I);
2697   return true;
2698 }
2699 
2700 /// runOnFunction - This is the main transformation entry point for a function.
2701 bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2702                       const TargetLibraryInfo &RunTLI, AAResults &RunAA,
2703                       MemoryDependenceResults *RunMD, LoopInfo *LI,
2704                       OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
2705   AC = &RunAC;
2706   DT = &RunDT;
2707   VN.setDomTree(DT);
2708   TLI = &RunTLI;
2709   VN.setAliasAnalysis(&RunAA);
2710   MD = RunMD;
2711   ImplicitControlFlowTracking ImplicitCFT;
2712   ICF = &ImplicitCFT;
2713   this->LI = LI;
2714   VN.setMemDep(MD);
2715   ORE = RunORE;
2716   InvalidBlockRPONumbers = true;
2717   MemorySSAUpdater Updater(MSSA);
2718   MSSAU = MSSA ? &Updater : nullptr;
2719 
2720   bool Changed = false;
2721   bool ShouldContinue = true;
2722 
2723   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2724   // Merge unconditional branches, allowing PRE to catch more
2725   // optimization opportunities.
2726   for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
2727     bool removedBlock = MergeBlockIntoPredecessor(&BB, &DTU, LI, MSSAU, MD);
2728     if (removedBlock)
2729       ++NumGVNBlocks;
2730 
2731     Changed |= removedBlock;
2732   }
2733 
2734   unsigned Iteration = 0;
2735   while (ShouldContinue) {
2736     LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
2737     (void) Iteration;
2738     ShouldContinue = iterateOnFunction(F);
2739     Changed |= ShouldContinue;
2740     ++Iteration;
2741   }
2742 
2743   if (isPREEnabled()) {
2744     // Fabricate val-num for dead-code in order to suppress assertion in
2745     // performPRE().
2746     assignValNumForDeadCode();
2747     bool PREChanged = true;
2748     while (PREChanged) {
2749       PREChanged = performPRE(F);
2750       Changed |= PREChanged;
2751     }
2752   }
2753 
2754   // FIXME: Should perform GVN again after PRE does something.  PRE can move
2755   // computations into blocks where they become fully redundant.  Note that
2756   // we can't do this until PRE's critical edge splitting updates memdep.
2757   // Actually, when this happens, we should just fully integrate PRE into GVN.
2758 
2759   cleanupGlobalSets();
2760   // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
2761   // iteration.
2762   DeadBlocks.clear();
2763 
2764   if (MSSA && VerifyMemorySSA)
2765     MSSA->verifyMemorySSA();
2766 
2767   return Changed;
2768 }
2769 
2770 bool GVNPass::processBlock(BasicBlock *BB) {
2771   // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
2772   // (and incrementing BI before processing an instruction).
2773   assert(InstrsToErase.empty() &&
2774          "We expect InstrsToErase to be empty across iterations");
2775   if (DeadBlocks.count(BB))
2776     return false;
2777 
2778   // Clearing map before every BB because it can be used only for single BB.
2779   ReplaceOperandsWithMap.clear();
2780   bool ChangedFunction = false;
2781 
2782   // Since we may not have visited the input blocks of the phis, we can't
2783   // use our normal hash approach for phis.  Instead, simply look for
2784   // obvious duplicates.  The first pass of GVN will tend to create
2785   // identical phis, and the second or later passes can eliminate them.
2786   SmallPtrSet<PHINode *, 8> PHINodesToRemove;
2787   ChangedFunction |= EliminateDuplicatePHINodes(BB, PHINodesToRemove);
2788   for (PHINode *PN : PHINodesToRemove) {
2789     VN.erase(PN);
2790     removeInstruction(PN);
2791   }
2792 
2793   for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
2794        BI != BE;) {
2795     if (!ReplaceOperandsWithMap.empty())
2796       ChangedFunction |= replaceOperandsForInBlockEquality(&*BI);
2797     ChangedFunction |= processInstruction(&*BI);
2798 
2799     if (InstrsToErase.empty()) {
2800       ++BI;
2801       continue;
2802     }
2803 
2804     // If we need some instructions deleted, do it now.
2805     NumGVNInstr += InstrsToErase.size();
2806 
2807     // Avoid iterator invalidation.
2808     bool AtStart = BI == BB->begin();
2809     if (!AtStart)
2810       --BI;
2811 
2812     for (auto *I : InstrsToErase) {
2813       assert(I->getParent() == BB && "Removing instruction from wrong block?");
2814       LLVM_DEBUG(dbgs() << "GVN removed: " << *I << '\n');
2815       salvageKnowledge(I, AC);
2816       salvageDebugInfo(*I);
2817       removeInstruction(I);
2818     }
2819     InstrsToErase.clear();
2820 
2821     if (AtStart)
2822       BI = BB->begin();
2823     else
2824       ++BI;
2825   }
2826 
2827   return ChangedFunction;
2828 }
2829 
2830 // Instantiate an expression in a predecessor that lacked it.
2831 bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2832                                         BasicBlock *Curr, unsigned int ValNo) {
2833   // Because we are going top-down through the block, all value numbers
2834   // will be available in the predecessor by the time we need them.  Any
2835   // that weren't originally present will have been instantiated earlier
2836   // in this loop.
2837   bool success = true;
2838   for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
2839     Value *Op = Instr->getOperand(i);
2840     if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
2841       continue;
2842     // This could be a newly inserted instruction, in which case, we won't
2843     // find a value number, and should give up before we hurt ourselves.
2844     // FIXME: Rewrite the infrastructure to let it easier to value number
2845     // and process newly inserted instructions.
2846     if (!VN.exists(Op)) {
2847       success = false;
2848       break;
2849     }
2850     uint32_t TValNo =
2851         VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
2852     if (Value *V = findLeader(Pred, TValNo)) {
2853       Instr->setOperand(i, V);
2854     } else {
2855       success = false;
2856       break;
2857     }
2858   }
2859 
2860   // Fail out if we encounter an operand that is not available in
2861   // the PRE predecessor.  This is typically because of loads which
2862   // are not value numbered precisely.
2863   if (!success)
2864     return false;
2865 
2866   Instr->insertBefore(Pred->getTerminator());
2867   Instr->setName(Instr->getName() + ".pre");
2868   Instr->setDebugLoc(Instr->getDebugLoc());
2869 
2870   ICF->insertInstructionTo(Instr, Pred);
2871 
2872   unsigned Num = VN.lookupOrAdd(Instr);
2873   VN.add(Instr, Num);
2874 
2875   // Update the availability map to include the new instruction.
2876   addToLeaderTable(Num, Instr, Pred);
2877   return true;
2878 }
2879 
2880 bool GVNPass::performScalarPRE(Instruction *CurInst) {
2881   if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
2882       isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
2883       CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
2884       isa<DbgInfoIntrinsic>(CurInst))
2885     return false;
2886 
2887   // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
2888   // sinking the compare again, and it would force the code generator to
2889   // move the i1 from processor flags or predicate registers into a general
2890   // purpose register.
2891   if (isa<CmpInst>(CurInst))
2892     return false;
2893 
2894   // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
2895   // sinking the addressing mode computation back to its uses. Extending the
2896   // GEP's live range increases the register pressure, and therefore it can
2897   // introduce unnecessary spills.
2898   //
2899   // This doesn't prevent Load PRE. PHI translation will make the GEP available
2900   // to the load by moving it to the predecessor block if necessary.
2901   if (isa<GetElementPtrInst>(CurInst))
2902     return false;
2903 
2904   if (auto *CallB = dyn_cast<CallBase>(CurInst)) {
2905     // We don't currently value number ANY inline asm calls.
2906     if (CallB->isInlineAsm())
2907       return false;
2908   }
2909 
2910   uint32_t ValNo = VN.lookup(CurInst);
2911 
2912   // Look for the predecessors for PRE opportunities.  We're
2913   // only trying to solve the basic diamond case, where
2914   // a value is computed in the successor and one predecessor,
2915   // but not the other.  We also explicitly disallow cases
2916   // where the successor is its own predecessor, because they're
2917   // more complicated to get right.
2918   unsigned NumWith = 0;
2919   unsigned NumWithout = 0;
2920   BasicBlock *PREPred = nullptr;
2921   BasicBlock *CurrentBlock = CurInst->getParent();
2922 
2923   // Update the RPO numbers for this function.
2924   if (InvalidBlockRPONumbers)
2925     assignBlockRPONumber(*CurrentBlock->getParent());
2926 
2927   SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap;
2928   for (BasicBlock *P : predecessors(CurrentBlock)) {
2929     // We're not interested in PRE where blocks with predecessors that are
2930     // not reachable.
2931     if (!DT->isReachableFromEntry(P)) {
2932       NumWithout = 2;
2933       break;
2934     }
2935     // It is not safe to do PRE when P->CurrentBlock is a loop backedge.
2936     assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&
2937            "Invalid BlockRPONumber map.");
2938     if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock]) {
2939       NumWithout = 2;
2940       break;
2941     }
2942 
2943     uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
2944     Value *predV = findLeader(P, TValNo);
2945     if (!predV) {
2946       predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
2947       PREPred = P;
2948       ++NumWithout;
2949     } else if (predV == CurInst) {
2950       /* CurInst dominates this predecessor. */
2951       NumWithout = 2;
2952       break;
2953     } else {
2954       predMap.push_back(std::make_pair(predV, P));
2955       ++NumWith;
2956     }
2957   }
2958 
2959   // Don't do PRE when it might increase code size, i.e. when
2960   // we would need to insert instructions in more than one pred.
2961   if (NumWithout > 1 || NumWith == 0)
2962     return false;
2963 
2964   // We may have a case where all predecessors have the instruction,
2965   // and we just need to insert a phi node. Otherwise, perform
2966   // insertion.
2967   Instruction *PREInstr = nullptr;
2968 
2969   if (NumWithout != 0) {
2970     if (!isSafeToSpeculativelyExecute(CurInst)) {
2971       // It is only valid to insert a new instruction if the current instruction
2972       // is always executed. An instruction with implicit control flow could
2973       // prevent us from doing it. If we cannot speculate the execution, then
2974       // PRE should be prohibited.
2975       if (ICF->isDominatedByICFIFromSameBlock(CurInst))
2976         return false;
2977     }
2978 
2979     // Don't do PRE across indirect branch.
2980     if (isa<IndirectBrInst>(PREPred->getTerminator()))
2981       return false;
2982 
2983     // We can't do PRE safely on a critical edge, so instead we schedule
2984     // the edge to be split and perform the PRE the next time we iterate
2985     // on the function.
2986     unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
2987     if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
2988       toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
2989       return false;
2990     }
2991     // We need to insert somewhere, so let's give it a shot
2992     PREInstr = CurInst->clone();
2993     if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
2994       // If we failed insertion, make sure we remove the instruction.
2995 #ifndef NDEBUG
2996       verifyRemoved(PREInstr);
2997 #endif
2998       PREInstr->deleteValue();
2999       return false;
3000     }
3001   }
3002 
3003   // Either we should have filled in the PRE instruction, or we should
3004   // not have needed insertions.
3005   assert(PREInstr != nullptr || NumWithout == 0);
3006 
3007   ++NumGVNPRE;
3008 
3009   // Create a PHI to make the value available in this block.
3010   PHINode *Phi =
3011       PHINode::Create(CurInst->getType(), predMap.size(),
3012                       CurInst->getName() + ".pre-phi", &CurrentBlock->front());
3013   for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
3014     if (Value *V = predMap[i].first) {
3015       // If we use an existing value in this phi, we have to patch the original
3016       // value because the phi will be used to replace a later value.
3017       patchReplacementInstruction(CurInst, V);
3018       Phi->addIncoming(V, predMap[i].second);
3019     } else
3020       Phi->addIncoming(PREInstr, PREPred);
3021   }
3022 
3023   VN.add(Phi, ValNo);
3024   // After creating a new PHI for ValNo, the phi translate result for ValNo will
3025   // be changed, so erase the related stale entries in phi translate cache.
3026   VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
3027   addToLeaderTable(ValNo, Phi, CurrentBlock);
3028   Phi->setDebugLoc(CurInst->getDebugLoc());
3029   CurInst->replaceAllUsesWith(Phi);
3030   if (MD && Phi->getType()->isPtrOrPtrVectorTy())
3031     MD->invalidateCachedPointerInfo(Phi);
3032   VN.erase(CurInst);
3033   removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
3034 
3035   LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
3036   removeInstruction(CurInst);
3037   ++NumGVNInstr;
3038 
3039   return true;
3040 }
3041 
3042 /// Perform a purely local form of PRE that looks for diamond
3043 /// control flow patterns and attempts to perform simple PRE at the join point.
3044 bool GVNPass::performPRE(Function &F) {
3045   bool Changed = false;
3046   for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
3047     // Nothing to PRE in the entry block.
3048     if (CurrentBlock == &F.getEntryBlock())
3049       continue;
3050 
3051     // Don't perform PRE on an EH pad.
3052     if (CurrentBlock->isEHPad())
3053       continue;
3054 
3055     for (BasicBlock::iterator BI = CurrentBlock->begin(),
3056                               BE = CurrentBlock->end();
3057          BI != BE;) {
3058       Instruction *CurInst = &*BI++;
3059       Changed |= performScalarPRE(CurInst);
3060     }
3061   }
3062 
3063   if (splitCriticalEdges())
3064     Changed = true;
3065 
3066   return Changed;
3067 }
3068 
3069 /// Split the critical edge connecting the given two blocks, and return
3070 /// the block inserted to the critical edge.
3071 BasicBlock *GVNPass::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
3072   // GVN does not require loop-simplify, do not try to preserve it if it is not
3073   // possible.
3074   BasicBlock *BB = SplitCriticalEdge(
3075       Pred, Succ,
3076       CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify());
3077   if (BB) {
3078     if (MD)
3079       MD->invalidateCachedPredecessors();
3080     InvalidBlockRPONumbers = true;
3081   }
3082   return BB;
3083 }
3084 
3085 /// Split critical edges found during the previous
3086 /// iteration that may enable further optimization.
3087 bool GVNPass::splitCriticalEdges() {
3088   if (toSplit.empty())
3089     return false;
3090 
3091   bool Changed = false;
3092   do {
3093     std::pair<Instruction *, unsigned> Edge = toSplit.pop_back_val();
3094     Changed |= SplitCriticalEdge(Edge.first, Edge.second,
3095                                  CriticalEdgeSplittingOptions(DT, LI, MSSAU)) !=
3096                nullptr;
3097   } while (!toSplit.empty());
3098   if (Changed) {
3099     if (MD)
3100       MD->invalidateCachedPredecessors();
3101     InvalidBlockRPONumbers = true;
3102   }
3103   return Changed;
3104 }
3105 
3106 /// Executes one iteration of GVN
3107 bool GVNPass::iterateOnFunction(Function &F) {
3108   cleanupGlobalSets();
3109 
3110   // Top-down walk of the dominator tree
3111   bool Changed = false;
3112   // Needed for value numbering with phi construction to work.
3113   // RPOT walks the graph in its constructor and will not be invalidated during
3114   // processBlock.
3115   ReversePostOrderTraversal<Function *> RPOT(&F);
3116 
3117   for (BasicBlock *BB : RPOT)
3118     Changed |= processBlock(BB);
3119 
3120   return Changed;
3121 }
3122 
3123 void GVNPass::cleanupGlobalSets() {
3124   VN.clear();
3125   LeaderTable.clear();
3126   BlockRPONumber.clear();
3127   TableAllocator.Reset();
3128   ICF->clear();
3129   InvalidBlockRPONumbers = true;
3130 }
3131 
3132 void GVNPass::removeInstruction(Instruction *I) {
3133   if (MD) MD->removeInstruction(I);
3134   if (MSSAU)
3135     MSSAU->removeMemoryAccess(I);
3136 #ifndef NDEBUG
3137   verifyRemoved(I);
3138 #endif
3139   ICF->removeInstruction(I);
3140   I->eraseFromParent();
3141 }
3142 
3143 /// Verify that the specified instruction does not occur in our
3144 /// internal data structures.
3145 void GVNPass::verifyRemoved(const Instruction *Inst) const {
3146   VN.verifyRemoved(Inst);
3147 
3148   // Walk through the value number scope to make sure the instruction isn't
3149   // ferreted away in it.
3150   for (const auto &I : LeaderTable) {
3151     const LeaderTableEntry *Node = &I.second;
3152     assert(Node->Val != Inst && "Inst still in value numbering scope!");
3153 
3154     while (Node->Next) {
3155       Node = Node->Next;
3156       assert(Node->Val != Inst && "Inst still in value numbering scope!");
3157     }
3158   }
3159 }
3160 
3161 /// BB is declared dead, which implied other blocks become dead as well. This
3162 /// function is to add all these blocks to "DeadBlocks". For the dead blocks'
3163 /// live successors, update their phi nodes by replacing the operands
3164 /// corresponding to dead blocks with UndefVal.
3165 void GVNPass::addDeadBlock(BasicBlock *BB) {
3166   SmallVector<BasicBlock *, 4> NewDead;
3167   SmallSetVector<BasicBlock *, 4> DF;
3168 
3169   NewDead.push_back(BB);
3170   while (!NewDead.empty()) {
3171     BasicBlock *D = NewDead.pop_back_val();
3172     if (DeadBlocks.count(D))
3173       continue;
3174 
3175     // All blocks dominated by D are dead.
3176     SmallVector<BasicBlock *, 8> Dom;
3177     DT->getDescendants(D, Dom);
3178     DeadBlocks.insert(Dom.begin(), Dom.end());
3179 
3180     // Figure out the dominance-frontier(D).
3181     for (BasicBlock *B : Dom) {
3182       for (BasicBlock *S : successors(B)) {
3183         if (DeadBlocks.count(S))
3184           continue;
3185 
3186         bool AllPredDead = true;
3187         for (BasicBlock *P : predecessors(S))
3188           if (!DeadBlocks.count(P)) {
3189             AllPredDead = false;
3190             break;
3191           }
3192 
3193         if (!AllPredDead) {
3194           // S could be proved dead later on. That is why we don't update phi
3195           // operands at this moment.
3196           DF.insert(S);
3197         } else {
3198           // While S is not dominated by D, it is dead by now. This could take
3199           // place if S already have a dead predecessor before D is declared
3200           // dead.
3201           NewDead.push_back(S);
3202         }
3203       }
3204     }
3205   }
3206 
3207   // For the dead blocks' live successors, update their phi nodes by replacing
3208   // the operands corresponding to dead blocks with UndefVal.
3209   for (BasicBlock *B : DF) {
3210     if (DeadBlocks.count(B))
3211       continue;
3212 
3213     // First, split the critical edges. This might also create additional blocks
3214     // to preserve LoopSimplify form and adjust edges accordingly.
3215     SmallVector<BasicBlock *, 4> Preds(predecessors(B));
3216     for (BasicBlock *P : Preds) {
3217       if (!DeadBlocks.count(P))
3218         continue;
3219 
3220       if (llvm::is_contained(successors(P), B) &&
3221           isCriticalEdge(P->getTerminator(), B)) {
3222         if (BasicBlock *S = splitCriticalEdges(P, B))
3223           DeadBlocks.insert(P = S);
3224       }
3225     }
3226 
3227     // Now poison the incoming values from the dead predecessors.
3228     for (BasicBlock *P : predecessors(B)) {
3229       if (!DeadBlocks.count(P))
3230         continue;
3231       for (PHINode &Phi : B->phis()) {
3232         Phi.setIncomingValueForBlock(P, PoisonValue::get(Phi.getType()));
3233         if (MD)
3234           MD->invalidateCachedPointerInfo(&Phi);
3235       }
3236     }
3237   }
3238 }
3239 
3240 // If the given branch is recognized as a foldable branch (i.e. conditional
3241 // branch with constant condition), it will perform following analyses and
3242 // transformation.
3243 //  1) If the dead out-coming edge is a critical-edge, split it. Let
3244 //     R be the target of the dead out-coming edge.
3245 //  1) Identify the set of dead blocks implied by the branch's dead outcoming
3246 //     edge. The result of this step will be {X| X is dominated by R}
3247 //  2) Identify those blocks which haves at least one dead predecessor. The
3248 //     result of this step will be dominance-frontier(R).
3249 //  3) Update the PHIs in DF(R) by replacing the operands corresponding to
3250 //     dead blocks with "UndefVal" in an hope these PHIs will optimized away.
3251 //
3252 // Return true iff *NEW* dead code are found.
3253 bool GVNPass::processFoldableCondBr(BranchInst *BI) {
3254   if (!BI || BI->isUnconditional())
3255     return false;
3256 
3257   // If a branch has two identical successors, we cannot declare either dead.
3258   if (BI->getSuccessor(0) == BI->getSuccessor(1))
3259     return false;
3260 
3261   ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
3262   if (!Cond)
3263     return false;
3264 
3265   BasicBlock *DeadRoot =
3266       Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
3267   if (DeadBlocks.count(DeadRoot))
3268     return false;
3269 
3270   if (!DeadRoot->getSinglePredecessor())
3271     DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
3272 
3273   addDeadBlock(DeadRoot);
3274   return true;
3275 }
3276 
3277 // performPRE() will trigger assert if it comes across an instruction without
3278 // associated val-num. As it normally has far more live instructions than dead
3279 // instructions, it makes more sense just to "fabricate" a val-number for the
3280 // dead code than checking if instruction involved is dead or not.
3281 void GVNPass::assignValNumForDeadCode() {
3282   for (BasicBlock *BB : DeadBlocks) {
3283     for (Instruction &Inst : *BB) {
3284       unsigned ValNum = VN.lookupOrAdd(&Inst);
3285       addToLeaderTable(ValNum, &Inst, BB);
3286     }
3287   }
3288 }
3289 
3290 class llvm::gvn::GVNLegacyPass : public FunctionPass {
3291 public:
3292   static char ID; // Pass identification, replacement for typeid
3293 
3294   explicit GVNLegacyPass(bool NoMemDepAnalysis = !GVNEnableMemDep)
3295       : FunctionPass(ID), Impl(GVNOptions().setMemDep(!NoMemDepAnalysis)) {
3296     initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry());
3297   }
3298 
3299   bool runOnFunction(Function &F) override {
3300     if (skipFunction(F))
3301       return false;
3302 
3303     auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
3304 
3305     auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
3306     return Impl.runImpl(
3307         F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
3308         getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
3309         getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
3310         getAnalysis<AAResultsWrapperPass>().getAAResults(),
3311         Impl.isMemDepEnabled()
3312             ? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep()
3313             : nullptr,
3314         LIWP ? &LIWP->getLoopInfo() : nullptr,
3315         &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(),
3316         MSSAWP ? &MSSAWP->getMSSA() : nullptr);
3317   }
3318 
3319   void getAnalysisUsage(AnalysisUsage &AU) const override {
3320     AU.addRequired<AssumptionCacheTracker>();
3321     AU.addRequired<DominatorTreeWrapperPass>();
3322     AU.addRequired<TargetLibraryInfoWrapperPass>();
3323     AU.addRequired<LoopInfoWrapperPass>();
3324     if (Impl.isMemDepEnabled())
3325       AU.addRequired<MemoryDependenceWrapperPass>();
3326     AU.addRequired<AAResultsWrapperPass>();
3327     AU.addPreserved<DominatorTreeWrapperPass>();
3328     AU.addPreserved<GlobalsAAWrapperPass>();
3329     AU.addPreserved<TargetLibraryInfoWrapperPass>();
3330     AU.addPreserved<LoopInfoWrapperPass>();
3331     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
3332     AU.addPreserved<MemorySSAWrapperPass>();
3333   }
3334 
3335 private:
3336   GVNPass Impl;
3337 };
3338 
3339 char GVNLegacyPass::ID = 0;
3340 
3341 INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
3342 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
3343 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
3344 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
3345 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
3346 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
3347 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
3348 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
3349 INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
3350 
3351 // The public interface to this file...
3352 FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) {
3353   return new GVNLegacyPass(NoMemDepAnalysis);
3354 }
3355