xref: /llvm-project/llvm/lib/Transforms/Scalar/EarlyCSE.cpp (revision ba3a1774a900da08cf8312b35e2662d4e06ee4af)
1 //===- EarlyCSE.cpp - Simple and fast CSE pass ----------------------------===//
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 a simple dominator tree walk that eliminates trivially
10 // redundant instructions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Scalar/EarlyCSE.h"
15 #include "llvm/ADT/DenseMapInfo.h"
16 #include "llvm/ADT/Hashing.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/ScopedHashTable.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AssumptionCache.h"
23 #include "llvm/Analysis/GlobalsModRef.h"
24 #include "llvm/Analysis/GuardUtils.h"
25 #include "llvm/Analysis/InstructionSimplify.h"
26 #include "llvm/Analysis/MemorySSA.h"
27 #include "llvm/Analysis/MemorySSAUpdater.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/Analysis/TargetTransformInfo.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/InstrTypes.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/IntrinsicInst.h"
40 #include "llvm/IR/Intrinsics.h"
41 #include "llvm/IR/LLVMContext.h"
42 #include "llvm/IR/PassManager.h"
43 #include "llvm/IR/PatternMatch.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/IR/Use.h"
46 #include "llvm/IR/Value.h"
47 #include "llvm/InitializePasses.h"
48 #include "llvm/Pass.h"
49 #include "llvm/Support/Allocator.h"
50 #include "llvm/Support/AtomicOrdering.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/Debug.h"
53 #include "llvm/Support/DebugCounter.h"
54 #include "llvm/Support/RecyclingAllocator.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Transforms/Scalar.h"
57 #include "llvm/Transforms/Utils/GuardUtils.h"
58 #include "llvm/Transforms/Utils/Local.h"
59 #include <cassert>
60 #include <deque>
61 #include <memory>
62 #include <utility>
63 
64 using namespace llvm;
65 using namespace llvm::PatternMatch;
66 
67 #define DEBUG_TYPE "early-cse"
68 
69 STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
70 STATISTIC(NumCSE,      "Number of instructions CSE'd");
71 STATISTIC(NumCSECVP,   "Number of compare instructions CVP'd");
72 STATISTIC(NumCSELoad,  "Number of load instructions CSE'd");
73 STATISTIC(NumCSECall,  "Number of call instructions CSE'd");
74 STATISTIC(NumDSE,      "Number of trivial dead stores removed");
75 
76 DEBUG_COUNTER(CSECounter, "early-cse",
77               "Controls which instructions are removed");
78 
79 static cl::opt<unsigned> EarlyCSEMssaOptCap(
80     "earlycse-mssa-optimization-cap", cl::init(500), cl::Hidden,
81     cl::desc("Enable imprecision in EarlyCSE in pathological cases, in exchange "
82              "for faster compile. Caps the MemorySSA clobbering calls."));
83 
84 static cl::opt<bool> EarlyCSEDebugHash(
85     "earlycse-debug-hash", cl::init(false), cl::Hidden,
86     cl::desc("Perform extra assertion checking to verify that SimpleValue's hash "
87              "function is well-behaved w.r.t. its isEqual predicate"));
88 
89 //===----------------------------------------------------------------------===//
90 // SimpleValue
91 //===----------------------------------------------------------------------===//
92 
93 namespace {
94 
95 /// Struct representing the available values in the scoped hash table.
96 struct SimpleValue {
97   Instruction *Inst;
98 
99   SimpleValue(Instruction *I) : Inst(I) {
100     assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
101   }
102 
103   bool isSentinel() const {
104     return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
105            Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
106   }
107 
108   static bool canHandle(Instruction *Inst) {
109     // This can only handle non-void readnone functions.
110     if (CallInst *CI = dyn_cast<CallInst>(Inst))
111       return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy();
112     return isa<CastInst>(Inst) || isa<UnaryOperator>(Inst) ||
113            isa<BinaryOperator>(Inst) || isa<GetElementPtrInst>(Inst) ||
114            isa<CmpInst>(Inst) || isa<SelectInst>(Inst) ||
115            isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
116            isa<ShuffleVectorInst>(Inst) || isa<ExtractValueInst>(Inst) ||
117            isa<InsertValueInst>(Inst);
118   }
119 };
120 
121 } // end anonymous namespace
122 
123 namespace llvm {
124 
125 template <> struct DenseMapInfo<SimpleValue> {
126   static inline SimpleValue getEmptyKey() {
127     return DenseMapInfo<Instruction *>::getEmptyKey();
128   }
129 
130   static inline SimpleValue getTombstoneKey() {
131     return DenseMapInfo<Instruction *>::getTombstoneKey();
132   }
133 
134   static unsigned getHashValue(SimpleValue Val);
135   static bool isEqual(SimpleValue LHS, SimpleValue RHS);
136 };
137 
138 } // end namespace llvm
139 
140 /// Match a 'select' including an optional 'not's of the condition.
141 static bool matchSelectWithOptionalNotCond(Value *V, Value *&Cond, Value *&A,
142                                            Value *&B,
143                                            SelectPatternFlavor &Flavor) {
144   // Return false if V is not even a select.
145   if (!match(V, m_Select(m_Value(Cond), m_Value(A), m_Value(B))))
146     return false;
147 
148   // Look through a 'not' of the condition operand by swapping A/B.
149   Value *CondNot;
150   if (match(Cond, m_Not(m_Value(CondNot)))) {
151     Cond = CondNot;
152     std::swap(A, B);
153   }
154 
155   // Set flavor if we find a match, or set it to unknown otherwise; in
156   // either case, return true to indicate that this is a select we can
157   // process.
158   if (auto *CmpI = dyn_cast<ICmpInst>(Cond))
159     Flavor = matchDecomposedSelectPattern(CmpI, A, B, A, B).Flavor;
160   else
161     Flavor = SPF_UNKNOWN;
162 
163   return true;
164 }
165 
166 static unsigned getHashValueImpl(SimpleValue Val) {
167   Instruction *Inst = Val.Inst;
168   // Hash in all of the operands as pointers.
169   if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst)) {
170     Value *LHS = BinOp->getOperand(0);
171     Value *RHS = BinOp->getOperand(1);
172     if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1))
173       std::swap(LHS, RHS);
174 
175     return hash_combine(BinOp->getOpcode(), LHS, RHS);
176   }
177 
178   if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
179     // Compares can be commuted by swapping the comparands and
180     // updating the predicate.  Choose the form that has the
181     // comparands in sorted order, or in the case of a tie, the
182     // one with the lower predicate.
183     Value *LHS = CI->getOperand(0);
184     Value *RHS = CI->getOperand(1);
185     CmpInst::Predicate Pred = CI->getPredicate();
186     CmpInst::Predicate SwappedPred = CI->getSwappedPredicate();
187     if (std::tie(LHS, Pred) > std::tie(RHS, SwappedPred)) {
188       std::swap(LHS, RHS);
189       Pred = SwappedPred;
190     }
191     return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
192   }
193 
194   // Hash general selects to allow matching commuted true/false operands.
195   SelectPatternFlavor SPF;
196   Value *Cond, *A, *B;
197   if (matchSelectWithOptionalNotCond(Inst, Cond, A, B, SPF)) {
198     // Hash min/max/abs (cmp + select) to allow for commuted operands.
199     // Min/max may also have non-canonical compare predicate (eg, the compare for
200     // smin may use 'sgt' rather than 'slt'), and non-canonical operands in the
201     // compare.
202     // TODO: We should also detect FP min/max.
203     if (SPF == SPF_SMIN || SPF == SPF_SMAX ||
204         SPF == SPF_UMIN || SPF == SPF_UMAX) {
205       if (A > B)
206         std::swap(A, B);
207       return hash_combine(Inst->getOpcode(), SPF, A, B);
208     }
209     if (SPF == SPF_ABS || SPF == SPF_NABS) {
210       // ABS/NABS always puts the input in A and its negation in B.
211       return hash_combine(Inst->getOpcode(), SPF, A, B);
212     }
213 
214     // Hash general selects to allow matching commuted true/false operands.
215 
216     // If we do not have a compare as the condition, just hash in the condition.
217     CmpInst::Predicate Pred;
218     Value *X, *Y;
219     if (!match(Cond, m_Cmp(Pred, m_Value(X), m_Value(Y))))
220       return hash_combine(Inst->getOpcode(), Cond, A, B);
221 
222     // Similar to cmp normalization (above) - canonicalize the predicate value:
223     // select (icmp Pred, X, Y), A, B --> select (icmp InvPred, X, Y), B, A
224     if (CmpInst::getInversePredicate(Pred) < Pred) {
225       Pred = CmpInst::getInversePredicate(Pred);
226       std::swap(A, B);
227     }
228     return hash_combine(Inst->getOpcode(), Pred, X, Y, A, B);
229   }
230 
231   if (CastInst *CI = dyn_cast<CastInst>(Inst))
232     return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0));
233 
234   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst))
235     return hash_combine(EVI->getOpcode(), EVI->getOperand(0),
236                         hash_combine_range(EVI->idx_begin(), EVI->idx_end()));
237 
238   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst))
239     return hash_combine(IVI->getOpcode(), IVI->getOperand(0),
240                         IVI->getOperand(1),
241                         hash_combine_range(IVI->idx_begin(), IVI->idx_end()));
242 
243   assert((isa<CallInst>(Inst) || isa<GetElementPtrInst>(Inst) ||
244           isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
245           isa<ShuffleVectorInst>(Inst) || isa<UnaryOperator>(Inst)) &&
246          "Invalid/unknown instruction");
247 
248   // Mix in the opcode.
249   return hash_combine(
250       Inst->getOpcode(),
251       hash_combine_range(Inst->value_op_begin(), Inst->value_op_end()));
252 }
253 
254 unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
255 #ifndef NDEBUG
256   // If -earlycse-debug-hash was specified, return a constant -- this
257   // will force all hashing to collide, so we'll exhaustively search
258   // the table for a match, and the assertion in isEqual will fire if
259   // there's a bug causing equal keys to hash differently.
260   if (EarlyCSEDebugHash)
261     return 0;
262 #endif
263   return getHashValueImpl(Val);
264 }
265 
266 static bool isEqualImpl(SimpleValue LHS, SimpleValue RHS) {
267   Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
268 
269   if (LHS.isSentinel() || RHS.isSentinel())
270     return LHSI == RHSI;
271 
272   if (LHSI->getOpcode() != RHSI->getOpcode())
273     return false;
274   if (LHSI->isIdenticalToWhenDefined(RHSI))
275     return true;
276 
277   // If we're not strictly identical, we still might be a commutable instruction
278   if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) {
279     if (!LHSBinOp->isCommutative())
280       return false;
281 
282     assert(isa<BinaryOperator>(RHSI) &&
283            "same opcode, but different instruction type?");
284     BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI);
285 
286     // Commuted equality
287     return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) &&
288            LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0);
289   }
290   if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) {
291     assert(isa<CmpInst>(RHSI) &&
292            "same opcode, but different instruction type?");
293     CmpInst *RHSCmp = cast<CmpInst>(RHSI);
294     // Commuted equality
295     return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) &&
296            LHSCmp->getOperand(1) == RHSCmp->getOperand(0) &&
297            LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
298   }
299 
300   // Min/max/abs can occur with commuted operands, non-canonical predicates,
301   // and/or non-canonical operands.
302   // Selects can be non-trivially equivalent via inverted conditions and swaps.
303   SelectPatternFlavor LSPF, RSPF;
304   Value *CondL, *CondR, *LHSA, *RHSA, *LHSB, *RHSB;
305   if (matchSelectWithOptionalNotCond(LHSI, CondL, LHSA, LHSB, LSPF) &&
306       matchSelectWithOptionalNotCond(RHSI, CondR, RHSA, RHSB, RSPF)) {
307     if (LSPF == RSPF) {
308       // TODO: We should also detect FP min/max.
309       if (LSPF == SPF_SMIN || LSPF == SPF_SMAX ||
310           LSPF == SPF_UMIN || LSPF == SPF_UMAX)
311         return ((LHSA == RHSA && LHSB == RHSB) ||
312                 (LHSA == RHSB && LHSB == RHSA));
313 
314       if (LSPF == SPF_ABS || LSPF == SPF_NABS) {
315         // Abs results are placed in a defined order by matchSelectPattern.
316         return LHSA == RHSA && LHSB == RHSB;
317       }
318 
319       // select Cond, A, B <--> select not(Cond), B, A
320       if (CondL == CondR && LHSA == RHSA && LHSB == RHSB)
321         return true;
322     }
323 
324     // If the true/false operands are swapped and the conditions are compares
325     // with inverted predicates, the selects are equal:
326     // select (icmp Pred, X, Y), A, B <--> select (icmp InvPred, X, Y), B, A
327     //
328     // This also handles patterns with a double-negation in the sense of not +
329     // inverse, because we looked through a 'not' in the matching function and
330     // swapped A/B:
331     // select (cmp Pred, X, Y), A, B <--> select (not (cmp InvPred, X, Y)), B, A
332     //
333     // This intentionally does NOT handle patterns with a double-negation in
334     // the sense of not + not, because doing so could result in values
335     // comparing
336     // as equal that hash differently in the min/max/abs cases like:
337     // select (cmp slt, X, Y), X, Y <--> select (not (not (cmp slt, X, Y))), X, Y
338     //   ^ hashes as min                  ^ would not hash as min
339     // In the context of the EarlyCSE pass, however, such cases never reach
340     // this code, as we simplify the double-negation before hashing the second
341     // select (and so still succeed at CSEing them).
342     if (LHSA == RHSB && LHSB == RHSA) {
343       CmpInst::Predicate PredL, PredR;
344       Value *X, *Y;
345       if (match(CondL, m_Cmp(PredL, m_Value(X), m_Value(Y))) &&
346           match(CondR, m_Cmp(PredR, m_Specific(X), m_Specific(Y))) &&
347           CmpInst::getInversePredicate(PredL) == PredR)
348         return true;
349     }
350   }
351 
352   return false;
353 }
354 
355 bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
356   // These comparisons are nontrivial, so assert that equality implies
357   // hash equality (DenseMap demands this as an invariant).
358   bool Result = isEqualImpl(LHS, RHS);
359   assert(!Result || (LHS.isSentinel() && LHS.Inst == RHS.Inst) ||
360          getHashValueImpl(LHS) == getHashValueImpl(RHS));
361   return Result;
362 }
363 
364 //===----------------------------------------------------------------------===//
365 // CallValue
366 //===----------------------------------------------------------------------===//
367 
368 namespace {
369 
370 /// Struct representing the available call values in the scoped hash
371 /// table.
372 struct CallValue {
373   Instruction *Inst;
374 
375   CallValue(Instruction *I) : Inst(I) {
376     assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
377   }
378 
379   bool isSentinel() const {
380     return Inst == DenseMapInfo<Instruction *>::getEmptyKey() ||
381            Inst == DenseMapInfo<Instruction *>::getTombstoneKey();
382   }
383 
384   static bool canHandle(Instruction *Inst) {
385     // Don't value number anything that returns void.
386     if (Inst->getType()->isVoidTy())
387       return false;
388 
389     CallInst *CI = dyn_cast<CallInst>(Inst);
390     if (!CI || !CI->onlyReadsMemory())
391       return false;
392     return true;
393   }
394 };
395 
396 } // end anonymous namespace
397 
398 namespace llvm {
399 
400 template <> struct DenseMapInfo<CallValue> {
401   static inline CallValue getEmptyKey() {
402     return DenseMapInfo<Instruction *>::getEmptyKey();
403   }
404 
405   static inline CallValue getTombstoneKey() {
406     return DenseMapInfo<Instruction *>::getTombstoneKey();
407   }
408 
409   static unsigned getHashValue(CallValue Val);
410   static bool isEqual(CallValue LHS, CallValue RHS);
411 };
412 
413 } // end namespace llvm
414 
415 unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) {
416   Instruction *Inst = Val.Inst;
417   // Hash all of the operands as pointers and mix in the opcode.
418   return hash_combine(
419       Inst->getOpcode(),
420       hash_combine_range(Inst->value_op_begin(), Inst->value_op_end()));
421 }
422 
423 bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
424   Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
425   if (LHS.isSentinel() || RHS.isSentinel())
426     return LHSI == RHSI;
427   return LHSI->isIdenticalTo(RHSI);
428 }
429 
430 //===----------------------------------------------------------------------===//
431 // EarlyCSE implementation
432 //===----------------------------------------------------------------------===//
433 
434 namespace {
435 
436 /// A simple and fast domtree-based CSE pass.
437 ///
438 /// This pass does a simple depth-first walk over the dominator tree,
439 /// eliminating trivially redundant instructions and using instsimplify to
440 /// canonicalize things as it goes. It is intended to be fast and catch obvious
441 /// cases so that instcombine and other passes are more effective. It is
442 /// expected that a later pass of GVN will catch the interesting/hard cases.
443 class EarlyCSE {
444 public:
445   const TargetLibraryInfo &TLI;
446   const TargetTransformInfo &TTI;
447   DominatorTree &DT;
448   AssumptionCache &AC;
449   const SimplifyQuery SQ;
450   MemorySSA *MSSA;
451   std::unique_ptr<MemorySSAUpdater> MSSAUpdater;
452 
453   using AllocatorTy =
454       RecyclingAllocator<BumpPtrAllocator,
455                          ScopedHashTableVal<SimpleValue, Value *>>;
456   using ScopedHTType =
457       ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>,
458                       AllocatorTy>;
459 
460   /// A scoped hash table of the current values of all of our simple
461   /// scalar expressions.
462   ///
463   /// As we walk down the domtree, we look to see if instructions are in this:
464   /// if so, we replace them with what we find, otherwise we insert them so
465   /// that dominated values can succeed in their lookup.
466   ScopedHTType AvailableValues;
467 
468   /// A scoped hash table of the current values of previously encountered
469   /// memory locations.
470   ///
471   /// This allows us to get efficient access to dominating loads or stores when
472   /// we have a fully redundant load.  In addition to the most recent load, we
473   /// keep track of a generation count of the read, which is compared against
474   /// the current generation count.  The current generation count is incremented
475   /// after every possibly writing memory operation, which ensures that we only
476   /// CSE loads with other loads that have no intervening store.  Ordering
477   /// events (such as fences or atomic instructions) increment the generation
478   /// count as well; essentially, we model these as writes to all possible
479   /// locations.  Note that atomic and/or volatile loads and stores can be
480   /// present the table; it is the responsibility of the consumer to inspect
481   /// the atomicity/volatility if needed.
482   struct LoadValue {
483     Instruction *DefInst = nullptr;
484     unsigned Generation = 0;
485     int MatchingId = -1;
486     bool IsAtomic = false;
487 
488     LoadValue() = default;
489     LoadValue(Instruction *Inst, unsigned Generation, unsigned MatchingId,
490               bool IsAtomic)
491         : DefInst(Inst), Generation(Generation), MatchingId(MatchingId),
492           IsAtomic(IsAtomic) {}
493   };
494 
495   using LoadMapAllocator =
496       RecyclingAllocator<BumpPtrAllocator,
497                          ScopedHashTableVal<Value *, LoadValue>>;
498   using LoadHTType =
499       ScopedHashTable<Value *, LoadValue, DenseMapInfo<Value *>,
500                       LoadMapAllocator>;
501 
502   LoadHTType AvailableLoads;
503 
504   // A scoped hash table mapping memory locations (represented as typed
505   // addresses) to generation numbers at which that memory location became
506   // (henceforth indefinitely) invariant.
507   using InvariantMapAllocator =
508       RecyclingAllocator<BumpPtrAllocator,
509                          ScopedHashTableVal<MemoryLocation, unsigned>>;
510   using InvariantHTType =
511       ScopedHashTable<MemoryLocation, unsigned, DenseMapInfo<MemoryLocation>,
512                       InvariantMapAllocator>;
513   InvariantHTType AvailableInvariants;
514 
515   /// A scoped hash table of the current values of read-only call
516   /// values.
517   ///
518   /// It uses the same generation count as loads.
519   using CallHTType =
520       ScopedHashTable<CallValue, std::pair<Instruction *, unsigned>>;
521   CallHTType AvailableCalls;
522 
523   /// This is the current generation of the memory value.
524   unsigned CurrentGeneration = 0;
525 
526   /// Set up the EarlyCSE runner for a particular function.
527   EarlyCSE(const DataLayout &DL, const TargetLibraryInfo &TLI,
528            const TargetTransformInfo &TTI, DominatorTree &DT,
529            AssumptionCache &AC, MemorySSA *MSSA)
530       : TLI(TLI), TTI(TTI), DT(DT), AC(AC), SQ(DL, &TLI, &DT, &AC), MSSA(MSSA),
531         MSSAUpdater(std::make_unique<MemorySSAUpdater>(MSSA)) {}
532 
533   bool run();
534 
535 private:
536   unsigned ClobberCounter = 0;
537   // Almost a POD, but needs to call the constructors for the scoped hash
538   // tables so that a new scope gets pushed on. These are RAII so that the
539   // scope gets popped when the NodeScope is destroyed.
540   class NodeScope {
541   public:
542     NodeScope(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads,
543               InvariantHTType &AvailableInvariants, CallHTType &AvailableCalls)
544       : Scope(AvailableValues), LoadScope(AvailableLoads),
545         InvariantScope(AvailableInvariants), CallScope(AvailableCalls) {}
546     NodeScope(const NodeScope &) = delete;
547     NodeScope &operator=(const NodeScope &) = delete;
548 
549   private:
550     ScopedHTType::ScopeTy Scope;
551     LoadHTType::ScopeTy LoadScope;
552     InvariantHTType::ScopeTy InvariantScope;
553     CallHTType::ScopeTy CallScope;
554   };
555 
556   // Contains all the needed information to create a stack for doing a depth
557   // first traversal of the tree. This includes scopes for values, loads, and
558   // calls as well as the generation. There is a child iterator so that the
559   // children do not need to be store separately.
560   class StackNode {
561   public:
562     StackNode(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads,
563               InvariantHTType &AvailableInvariants, CallHTType &AvailableCalls,
564               unsigned cg, DomTreeNode *n, DomTreeNode::iterator child,
565               DomTreeNode::iterator end)
566         : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child),
567           EndIter(end),
568           Scopes(AvailableValues, AvailableLoads, AvailableInvariants,
569                  AvailableCalls)
570           {}
571     StackNode(const StackNode &) = delete;
572     StackNode &operator=(const StackNode &) = delete;
573 
574     // Accessors.
575     unsigned currentGeneration() { return CurrentGeneration; }
576     unsigned childGeneration() { return ChildGeneration; }
577     void childGeneration(unsigned generation) { ChildGeneration = generation; }
578     DomTreeNode *node() { return Node; }
579     DomTreeNode::iterator childIter() { return ChildIter; }
580 
581     DomTreeNode *nextChild() {
582       DomTreeNode *child = *ChildIter;
583       ++ChildIter;
584       return child;
585     }
586 
587     DomTreeNode::iterator end() { return EndIter; }
588     bool isProcessed() { return Processed; }
589     void process() { Processed = true; }
590 
591   private:
592     unsigned CurrentGeneration;
593     unsigned ChildGeneration;
594     DomTreeNode *Node;
595     DomTreeNode::iterator ChildIter;
596     DomTreeNode::iterator EndIter;
597     NodeScope Scopes;
598     bool Processed = false;
599   };
600 
601   /// Wrapper class to handle memory instructions, including loads,
602   /// stores and intrinsic loads and stores defined by the target.
603   class ParseMemoryInst {
604   public:
605     ParseMemoryInst(Instruction *Inst, const TargetTransformInfo &TTI)
606       : Inst(Inst) {
607       if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
608         if (TTI.getTgtMemIntrinsic(II, Info))
609           IsTargetMemInst = true;
610     }
611 
612     bool isLoad() const {
613       if (IsTargetMemInst) return Info.ReadMem;
614       return isa<LoadInst>(Inst);
615     }
616 
617     bool isStore() const {
618       if (IsTargetMemInst) return Info.WriteMem;
619       return isa<StoreInst>(Inst);
620     }
621 
622     bool isAtomic() const {
623       if (IsTargetMemInst)
624         return Info.Ordering != AtomicOrdering::NotAtomic;
625       return Inst->isAtomic();
626     }
627 
628     bool isUnordered() const {
629       if (IsTargetMemInst)
630         return Info.isUnordered();
631 
632       if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
633         return LI->isUnordered();
634       } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
635         return SI->isUnordered();
636       }
637       // Conservative answer
638       return !Inst->isAtomic();
639     }
640 
641     bool isVolatile() const {
642       if (IsTargetMemInst)
643         return Info.IsVolatile;
644 
645       if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
646         return LI->isVolatile();
647       } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
648         return SI->isVolatile();
649       }
650       // Conservative answer
651       return true;
652     }
653 
654     bool isInvariantLoad() const {
655       if (auto *LI = dyn_cast<LoadInst>(Inst))
656         return LI->hasMetadata(LLVMContext::MD_invariant_load);
657       return false;
658     }
659 
660     bool isMatchingMemLoc(const ParseMemoryInst &Inst) const {
661       return (getPointerOperand() == Inst.getPointerOperand() &&
662               getMatchingId() == Inst.getMatchingId());
663     }
664 
665     bool isValid() const { return getPointerOperand() != nullptr; }
666 
667     // For regular (non-intrinsic) loads/stores, this is set to -1. For
668     // intrinsic loads/stores, the id is retrieved from the corresponding
669     // field in the MemIntrinsicInfo structure.  That field contains
670     // non-negative values only.
671     int getMatchingId() const {
672       if (IsTargetMemInst) return Info.MatchingId;
673       return -1;
674     }
675 
676     Value *getPointerOperand() const {
677       if (IsTargetMemInst) return Info.PtrVal;
678       return getLoadStorePointerOperand(Inst);
679     }
680 
681     bool mayReadFromMemory() const {
682       if (IsTargetMemInst) return Info.ReadMem;
683       return Inst->mayReadFromMemory();
684     }
685 
686     bool mayWriteToMemory() const {
687       if (IsTargetMemInst) return Info.WriteMem;
688       return Inst->mayWriteToMemory();
689     }
690 
691   private:
692     bool IsTargetMemInst = false;
693     MemIntrinsicInfo Info;
694     Instruction *Inst;
695   };
696 
697   bool processNode(DomTreeNode *Node);
698 
699   bool handleBranchCondition(Instruction *CondInst, const BranchInst *BI,
700                              const BasicBlock *BB, const BasicBlock *Pred);
701 
702   Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const {
703     if (auto *LI = dyn_cast<LoadInst>(Inst))
704       return LI;
705     if (auto *SI = dyn_cast<StoreInst>(Inst))
706       return SI->getValueOperand();
707     assert(isa<IntrinsicInst>(Inst) && "Instruction not supported");
708     return TTI.getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst),
709                                                  ExpectedType);
710   }
711 
712   /// Return true if the instruction is known to only operate on memory
713   /// provably invariant in the given "generation".
714   bool isOperatingOnInvariantMemAt(Instruction *I, unsigned GenAt);
715 
716   bool isSameMemGeneration(unsigned EarlierGeneration, unsigned LaterGeneration,
717                            Instruction *EarlierInst, Instruction *LaterInst);
718 
719   void removeMSSA(Instruction &Inst) {
720     if (!MSSA)
721       return;
722     if (VerifyMemorySSA)
723       MSSA->verifyMemorySSA();
724     // Removing a store here can leave MemorySSA in an unoptimized state by
725     // creating MemoryPhis that have identical arguments and by creating
726     // MemoryUses whose defining access is not an actual clobber. The phi case
727     // is handled by MemorySSA when passing OptimizePhis = true to
728     // removeMemoryAccess.  The non-optimized MemoryUse case is lazily updated
729     // by MemorySSA's getClobberingMemoryAccess.
730     MSSAUpdater->removeMemoryAccess(&Inst, true);
731   }
732 };
733 
734 } // end anonymous namespace
735 
736 /// Determine if the memory referenced by LaterInst is from the same heap
737 /// version as EarlierInst.
738 /// This is currently called in two scenarios:
739 ///
740 ///   load p
741 ///   ...
742 ///   load p
743 ///
744 /// and
745 ///
746 ///   x = load p
747 ///   ...
748 ///   store x, p
749 ///
750 /// in both cases we want to verify that there are no possible writes to the
751 /// memory referenced by p between the earlier and later instruction.
752 bool EarlyCSE::isSameMemGeneration(unsigned EarlierGeneration,
753                                    unsigned LaterGeneration,
754                                    Instruction *EarlierInst,
755                                    Instruction *LaterInst) {
756   // Check the simple memory generation tracking first.
757   if (EarlierGeneration == LaterGeneration)
758     return true;
759 
760   if (!MSSA)
761     return false;
762 
763   // If MemorySSA has determined that one of EarlierInst or LaterInst does not
764   // read/write memory, then we can safely return true here.
765   // FIXME: We could be more aggressive when checking doesNotAccessMemory(),
766   // onlyReadsMemory(), mayReadFromMemory(), and mayWriteToMemory() in this pass
767   // by also checking the MemorySSA MemoryAccess on the instruction.  Initial
768   // experiments suggest this isn't worthwhile, at least for C/C++ code compiled
769   // with the default optimization pipeline.
770   auto *EarlierMA = MSSA->getMemoryAccess(EarlierInst);
771   if (!EarlierMA)
772     return true;
773   auto *LaterMA = MSSA->getMemoryAccess(LaterInst);
774   if (!LaterMA)
775     return true;
776 
777   // Since we know LaterDef dominates LaterInst and EarlierInst dominates
778   // LaterInst, if LaterDef dominates EarlierInst then it can't occur between
779   // EarlierInst and LaterInst and neither can any other write that potentially
780   // clobbers LaterInst.
781   MemoryAccess *LaterDef;
782   if (ClobberCounter < EarlyCSEMssaOptCap) {
783     LaterDef = MSSA->getWalker()->getClobberingMemoryAccess(LaterInst);
784     ClobberCounter++;
785   } else
786     LaterDef = LaterMA->getDefiningAccess();
787 
788   return MSSA->dominates(LaterDef, EarlierMA);
789 }
790 
791 bool EarlyCSE::isOperatingOnInvariantMemAt(Instruction *I, unsigned GenAt) {
792   // A location loaded from with an invariant_load is assumed to *never* change
793   // within the visible scope of the compilation.
794   if (auto *LI = dyn_cast<LoadInst>(I))
795     if (LI->hasMetadata(LLVMContext::MD_invariant_load))
796       return true;
797 
798   auto MemLocOpt = MemoryLocation::getOrNone(I);
799   if (!MemLocOpt)
800     // "target" intrinsic forms of loads aren't currently known to
801     // MemoryLocation::get.  TODO
802     return false;
803   MemoryLocation MemLoc = *MemLocOpt;
804   if (!AvailableInvariants.count(MemLoc))
805     return false;
806 
807   // Is the generation at which this became invariant older than the
808   // current one?
809   return AvailableInvariants.lookup(MemLoc) <= GenAt;
810 }
811 
812 bool EarlyCSE::handleBranchCondition(Instruction *CondInst,
813                                      const BranchInst *BI, const BasicBlock *BB,
814                                      const BasicBlock *Pred) {
815   assert(BI->isConditional() && "Should be a conditional branch!");
816   assert(BI->getCondition() == CondInst && "Wrong condition?");
817   assert(BI->getSuccessor(0) == BB || BI->getSuccessor(1) == BB);
818   auto *TorF = (BI->getSuccessor(0) == BB)
819                    ? ConstantInt::getTrue(BB->getContext())
820                    : ConstantInt::getFalse(BB->getContext());
821   auto MatchBinOp = [](Instruction *I, unsigned Opcode) {
822     if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(I))
823       return BOp->getOpcode() == Opcode;
824     return false;
825   };
826   // If the condition is AND operation, we can propagate its operands into the
827   // true branch. If it is OR operation, we can propagate them into the false
828   // branch.
829   unsigned PropagateOpcode =
830       (BI->getSuccessor(0) == BB) ? Instruction::And : Instruction::Or;
831 
832   bool MadeChanges = false;
833   SmallVector<Instruction *, 4> WorkList;
834   SmallPtrSet<Instruction *, 4> Visited;
835   WorkList.push_back(CondInst);
836   while (!WorkList.empty()) {
837     Instruction *Curr = WorkList.pop_back_val();
838 
839     AvailableValues.insert(Curr, TorF);
840     LLVM_DEBUG(dbgs() << "EarlyCSE CVP: Add conditional value for '"
841                       << Curr->getName() << "' as " << *TorF << " in "
842                       << BB->getName() << "\n");
843     if (!DebugCounter::shouldExecute(CSECounter)) {
844       LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
845     } else {
846       // Replace all dominated uses with the known value.
847       if (unsigned Count = replaceDominatedUsesWith(Curr, TorF, DT,
848                                                     BasicBlockEdge(Pred, BB))) {
849         NumCSECVP += Count;
850         MadeChanges = true;
851       }
852     }
853 
854     if (MatchBinOp(Curr, PropagateOpcode))
855       for (auto &Op : cast<BinaryOperator>(Curr)->operands())
856         if (Instruction *OPI = dyn_cast<Instruction>(Op))
857           if (SimpleValue::canHandle(OPI) && Visited.insert(OPI).second)
858             WorkList.push_back(OPI);
859   }
860 
861   return MadeChanges;
862 }
863 
864 bool EarlyCSE::processNode(DomTreeNode *Node) {
865   bool Changed = false;
866   BasicBlock *BB = Node->getBlock();
867 
868   // If this block has a single predecessor, then the predecessor is the parent
869   // of the domtree node and all of the live out memory values are still current
870   // in this block.  If this block has multiple predecessors, then they could
871   // have invalidated the live-out memory values of our parent value.  For now,
872   // just be conservative and invalidate memory if this block has multiple
873   // predecessors.
874   if (!BB->getSinglePredecessor())
875     ++CurrentGeneration;
876 
877   // If this node has a single predecessor which ends in a conditional branch,
878   // we can infer the value of the branch condition given that we took this
879   // path.  We need the single predecessor to ensure there's not another path
880   // which reaches this block where the condition might hold a different
881   // value.  Since we're adding this to the scoped hash table (like any other
882   // def), it will have been popped if we encounter a future merge block.
883   if (BasicBlock *Pred = BB->getSinglePredecessor()) {
884     auto *BI = dyn_cast<BranchInst>(Pred->getTerminator());
885     if (BI && BI->isConditional()) {
886       auto *CondInst = dyn_cast<Instruction>(BI->getCondition());
887       if (CondInst && SimpleValue::canHandle(CondInst))
888         Changed |= handleBranchCondition(CondInst, BI, BB, Pred);
889     }
890   }
891 
892   /// LastStore - Keep track of the last non-volatile store that we saw... for
893   /// as long as there in no instruction that reads memory.  If we see a store
894   /// to the same location, we delete the dead store.  This zaps trivial dead
895   /// stores which can occur in bitfield code among other things.
896   Instruction *LastStore = nullptr;
897 
898   // See if any instructions in the block can be eliminated.  If so, do it.  If
899   // not, add them to AvailableValues.
900   for (Instruction &Inst : make_early_inc_range(BB->getInstList())) {
901     // Dead instructions should just be removed.
902     if (isInstructionTriviallyDead(&Inst, &TLI)) {
903       LLVM_DEBUG(dbgs() << "EarlyCSE DCE: " << Inst << '\n');
904       if (!DebugCounter::shouldExecute(CSECounter)) {
905         LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
906         continue;
907       }
908 
909       salvageDebugInfoOrMarkUndef(Inst);
910       removeMSSA(Inst);
911       Inst.eraseFromParent();
912       Changed = true;
913       ++NumSimplify;
914       continue;
915     }
916 
917     // Skip assume intrinsics, they don't really have side effects (although
918     // they're marked as such to ensure preservation of control dependencies),
919     // and this pass will not bother with its removal. However, we should mark
920     // its condition as true for all dominated blocks.
921     if (match(&Inst, m_Intrinsic<Intrinsic::assume>())) {
922       auto *CondI =
923           dyn_cast<Instruction>(cast<CallInst>(Inst).getArgOperand(0));
924       if (CondI && SimpleValue::canHandle(CondI)) {
925         LLVM_DEBUG(dbgs() << "EarlyCSE considering assumption: " << Inst
926                           << '\n');
927         AvailableValues.insert(CondI, ConstantInt::getTrue(BB->getContext()));
928       } else
929         LLVM_DEBUG(dbgs() << "EarlyCSE skipping assumption: " << Inst << '\n');
930       continue;
931     }
932 
933     // Skip sideeffect intrinsics, for the same reason as assume intrinsics.
934     if (match(&Inst, m_Intrinsic<Intrinsic::sideeffect>())) {
935       LLVM_DEBUG(dbgs() << "EarlyCSE skipping sideeffect: " << Inst << '\n');
936       continue;
937     }
938 
939     // We can skip all invariant.start intrinsics since they only read memory,
940     // and we can forward values across it. For invariant starts without
941     // invariant ends, we can use the fact that the invariantness never ends to
942     // start a scope in the current generaton which is true for all future
943     // generations.  Also, we dont need to consume the last store since the
944     // semantics of invariant.start allow us to perform   DSE of the last
945     // store, if there was a store following invariant.start. Consider:
946     //
947     // store 30, i8* p
948     // invariant.start(p)
949     // store 40, i8* p
950     // We can DSE the store to 30, since the store 40 to invariant location p
951     // causes undefined behaviour.
952     if (match(&Inst, m_Intrinsic<Intrinsic::invariant_start>())) {
953       // If there are any uses, the scope might end.
954       if (!Inst.use_empty())
955         continue;
956       MemoryLocation MemLoc =
957           MemoryLocation::getForArgument(&cast<CallInst>(Inst), 1, TLI);
958       // Don't start a scope if we already have a better one pushed
959       if (!AvailableInvariants.count(MemLoc))
960         AvailableInvariants.insert(MemLoc, CurrentGeneration);
961       continue;
962     }
963 
964     if (isGuard(&Inst)) {
965       if (auto *CondI =
966               dyn_cast<Instruction>(cast<CallInst>(Inst).getArgOperand(0))) {
967         if (SimpleValue::canHandle(CondI)) {
968           // Do we already know the actual value of this condition?
969           if (auto *KnownCond = AvailableValues.lookup(CondI)) {
970             // Is the condition known to be true?
971             if (isa<ConstantInt>(KnownCond) &&
972                 cast<ConstantInt>(KnownCond)->isOne()) {
973               LLVM_DEBUG(dbgs()
974                          << "EarlyCSE removing guard: " << Inst << '\n');
975               removeMSSA(Inst);
976               Inst.eraseFromParent();
977               Changed = true;
978               continue;
979             } else
980               // Use the known value if it wasn't true.
981               cast<CallInst>(Inst).setArgOperand(0, KnownCond);
982           }
983           // The condition we're on guarding here is true for all dominated
984           // locations.
985           AvailableValues.insert(CondI, ConstantInt::getTrue(BB->getContext()));
986         }
987       }
988 
989       // Guard intrinsics read all memory, but don't write any memory.
990       // Accordingly, don't update the generation but consume the last store (to
991       // avoid an incorrect DSE).
992       LastStore = nullptr;
993       continue;
994     }
995 
996     // If the instruction can be simplified (e.g. X+0 = X) then replace it with
997     // its simpler value.
998     if (Value *V = SimplifyInstruction(&Inst, SQ)) {
999       LLVM_DEBUG(dbgs() << "EarlyCSE Simplify: " << Inst << "  to: " << *V
1000                         << '\n');
1001       if (!DebugCounter::shouldExecute(CSECounter)) {
1002         LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
1003       } else {
1004         bool Killed = false;
1005         if (!Inst.use_empty()) {
1006           Inst.replaceAllUsesWith(V);
1007           Changed = true;
1008         }
1009         if (isInstructionTriviallyDead(&Inst, &TLI)) {
1010           removeMSSA(Inst);
1011           Inst.eraseFromParent();
1012           Changed = true;
1013           Killed = true;
1014         }
1015         if (Changed)
1016           ++NumSimplify;
1017         if (Killed)
1018           continue;
1019       }
1020     }
1021 
1022     // If this is a simple instruction that we can value number, process it.
1023     if (SimpleValue::canHandle(&Inst)) {
1024       // See if the instruction has an available value.  If so, use it.
1025       if (Value *V = AvailableValues.lookup(&Inst)) {
1026         LLVM_DEBUG(dbgs() << "EarlyCSE CSE: " << Inst << "  to: " << *V
1027                           << '\n');
1028         if (!DebugCounter::shouldExecute(CSECounter)) {
1029           LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
1030           continue;
1031         }
1032         if (auto *I = dyn_cast<Instruction>(V))
1033           I->andIRFlags(&Inst);
1034         Inst.replaceAllUsesWith(V);
1035         removeMSSA(Inst);
1036         Inst.eraseFromParent();
1037         Changed = true;
1038         ++NumCSE;
1039         continue;
1040       }
1041 
1042       // Otherwise, just remember that this value is available.
1043       AvailableValues.insert(&Inst, &Inst);
1044       continue;
1045     }
1046 
1047     ParseMemoryInst MemInst(&Inst, TTI);
1048     // If this is a non-volatile load, process it.
1049     if (MemInst.isValid() && MemInst.isLoad()) {
1050       // (conservatively) we can't peak past the ordering implied by this
1051       // operation, but we can add this load to our set of available values
1052       if (MemInst.isVolatile() || !MemInst.isUnordered()) {
1053         LastStore = nullptr;
1054         ++CurrentGeneration;
1055       }
1056 
1057       if (MemInst.isInvariantLoad()) {
1058         // If we pass an invariant load, we know that memory location is
1059         // indefinitely constant from the moment of first dereferenceability.
1060         // We conservatively treat the invariant_load as that moment.  If we
1061         // pass a invariant load after already establishing a scope, don't
1062         // restart it since we want to preserve the earliest point seen.
1063         auto MemLoc = MemoryLocation::get(&Inst);
1064         if (!AvailableInvariants.count(MemLoc))
1065           AvailableInvariants.insert(MemLoc, CurrentGeneration);
1066       }
1067 
1068       // If we have an available version of this load, and if it is the right
1069       // generation or the load is known to be from an invariant location,
1070       // replace this instruction.
1071       //
1072       // If either the dominating load or the current load are invariant, then
1073       // we can assume the current load loads the same value as the dominating
1074       // load.
1075       LoadValue InVal = AvailableLoads.lookup(MemInst.getPointerOperand());
1076       if (InVal.DefInst != nullptr &&
1077           InVal.MatchingId == MemInst.getMatchingId() &&
1078           // We don't yet handle removing loads with ordering of any kind.
1079           !MemInst.isVolatile() && MemInst.isUnordered() &&
1080           // We can't replace an atomic load with one which isn't also atomic.
1081           InVal.IsAtomic >= MemInst.isAtomic() &&
1082           (isOperatingOnInvariantMemAt(&Inst, InVal.Generation) ||
1083            isSameMemGeneration(InVal.Generation, CurrentGeneration,
1084                                InVal.DefInst, &Inst))) {
1085         Value *Op = getOrCreateResult(InVal.DefInst, Inst.getType());
1086         if (Op != nullptr) {
1087           LLVM_DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << Inst
1088                             << "  to: " << *InVal.DefInst << '\n');
1089           if (!DebugCounter::shouldExecute(CSECounter)) {
1090             LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
1091             continue;
1092           }
1093           if (!Inst.use_empty())
1094             Inst.replaceAllUsesWith(Op);
1095           removeMSSA(Inst);
1096           Inst.eraseFromParent();
1097           Changed = true;
1098           ++NumCSELoad;
1099           continue;
1100         }
1101       }
1102 
1103       // Otherwise, remember that we have this instruction.
1104       AvailableLoads.insert(MemInst.getPointerOperand(),
1105                             LoadValue(&Inst, CurrentGeneration,
1106                                       MemInst.getMatchingId(),
1107                                       MemInst.isAtomic()));
1108       LastStore = nullptr;
1109       continue;
1110     }
1111 
1112     // If this instruction may read from memory or throw (and potentially read
1113     // from memory in the exception handler), forget LastStore.  Load/store
1114     // intrinsics will indicate both a read and a write to memory.  The target
1115     // may override this (e.g. so that a store intrinsic does not read from
1116     // memory, and thus will be treated the same as a regular store for
1117     // commoning purposes).
1118     if ((Inst.mayReadFromMemory() || Inst.mayThrow()) &&
1119         !(MemInst.isValid() && !MemInst.mayReadFromMemory()))
1120       LastStore = nullptr;
1121 
1122     // If this is a read-only call, process it.
1123     if (CallValue::canHandle(&Inst)) {
1124       // If we have an available version of this call, and if it is the right
1125       // generation, replace this instruction.
1126       std::pair<Instruction *, unsigned> InVal = AvailableCalls.lookup(&Inst);
1127       if (InVal.first != nullptr &&
1128           isSameMemGeneration(InVal.second, CurrentGeneration, InVal.first,
1129                               &Inst)) {
1130         LLVM_DEBUG(dbgs() << "EarlyCSE CSE CALL: " << Inst
1131                           << "  to: " << *InVal.first << '\n');
1132         if (!DebugCounter::shouldExecute(CSECounter)) {
1133           LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
1134           continue;
1135         }
1136         if (!Inst.use_empty())
1137           Inst.replaceAllUsesWith(InVal.first);
1138         removeMSSA(Inst);
1139         Inst.eraseFromParent();
1140         Changed = true;
1141         ++NumCSECall;
1142         continue;
1143       }
1144 
1145       // Otherwise, remember that we have this instruction.
1146       AvailableCalls.insert(&Inst, std::make_pair(&Inst, CurrentGeneration));
1147       continue;
1148     }
1149 
1150     // A release fence requires that all stores complete before it, but does
1151     // not prevent the reordering of following loads 'before' the fence.  As a
1152     // result, we don't need to consider it as writing to memory and don't need
1153     // to advance the generation.  We do need to prevent DSE across the fence,
1154     // but that's handled above.
1155     if (auto *FI = dyn_cast<FenceInst>(&Inst))
1156       if (FI->getOrdering() == AtomicOrdering::Release) {
1157         assert(Inst.mayReadFromMemory() && "relied on to prevent DSE above");
1158         continue;
1159       }
1160 
1161     // write back DSE - If we write back the same value we just loaded from
1162     // the same location and haven't passed any intervening writes or ordering
1163     // operations, we can remove the write.  The primary benefit is in allowing
1164     // the available load table to remain valid and value forward past where
1165     // the store originally was.
1166     if (MemInst.isValid() && MemInst.isStore()) {
1167       LoadValue InVal = AvailableLoads.lookup(MemInst.getPointerOperand());
1168       if (InVal.DefInst &&
1169           InVal.DefInst == getOrCreateResult(&Inst, InVal.DefInst->getType()) &&
1170           InVal.MatchingId == MemInst.getMatchingId() &&
1171           // We don't yet handle removing stores with ordering of any kind.
1172           !MemInst.isVolatile() && MemInst.isUnordered() &&
1173           (isOperatingOnInvariantMemAt(&Inst, InVal.Generation) ||
1174            isSameMemGeneration(InVal.Generation, CurrentGeneration,
1175                                InVal.DefInst, &Inst))) {
1176         // It is okay to have a LastStore to a different pointer here if MemorySSA
1177         // tells us that the load and store are from the same memory generation.
1178         // In that case, LastStore should keep its present value since we're
1179         // removing the current store.
1180         assert((!LastStore ||
1181                 ParseMemoryInst(LastStore, TTI).getPointerOperand() ==
1182                     MemInst.getPointerOperand() ||
1183                 MSSA) &&
1184                "can't have an intervening store if not using MemorySSA!");
1185         LLVM_DEBUG(dbgs() << "EarlyCSE DSE (writeback): " << Inst << '\n');
1186         if (!DebugCounter::shouldExecute(CSECounter)) {
1187           LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
1188           continue;
1189         }
1190         removeMSSA(Inst);
1191         Inst.eraseFromParent();
1192         Changed = true;
1193         ++NumDSE;
1194         // We can avoid incrementing the generation count since we were able
1195         // to eliminate this store.
1196         continue;
1197       }
1198     }
1199 
1200     // Okay, this isn't something we can CSE at all.  Check to see if it is
1201     // something that could modify memory.  If so, our available memory values
1202     // cannot be used so bump the generation count.
1203     if (Inst.mayWriteToMemory()) {
1204       ++CurrentGeneration;
1205 
1206       if (MemInst.isValid() && MemInst.isStore()) {
1207         // We do a trivial form of DSE if there are two stores to the same
1208         // location with no intervening loads.  Delete the earlier store.
1209         // At the moment, we don't remove ordered stores, but do remove
1210         // unordered atomic stores.  There's no special requirement (for
1211         // unordered atomics) about removing atomic stores only in favor of
1212         // other atomic stores since we were going to execute the non-atomic
1213         // one anyway and the atomic one might never have become visible.
1214         if (LastStore) {
1215           ParseMemoryInst LastStoreMemInst(LastStore, TTI);
1216           assert(LastStoreMemInst.isUnordered() &&
1217                  !LastStoreMemInst.isVolatile() &&
1218                  "Violated invariant");
1219           if (LastStoreMemInst.isMatchingMemLoc(MemInst)) {
1220             LLVM_DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore
1221                               << "  due to: " << Inst << '\n');
1222             if (!DebugCounter::shouldExecute(CSECounter)) {
1223               LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
1224             } else {
1225               removeMSSA(*LastStore);
1226               LastStore->eraseFromParent();
1227               Changed = true;
1228               ++NumDSE;
1229               LastStore = nullptr;
1230             }
1231           }
1232           // fallthrough - we can exploit information about this store
1233         }
1234 
1235         // Okay, we just invalidated anything we knew about loaded values.  Try
1236         // to salvage *something* by remembering that the stored value is a live
1237         // version of the pointer.  It is safe to forward from volatile stores
1238         // to non-volatile loads, so we don't have to check for volatility of
1239         // the store.
1240         AvailableLoads.insert(MemInst.getPointerOperand(),
1241                               LoadValue(&Inst, CurrentGeneration,
1242                                         MemInst.getMatchingId(),
1243                                         MemInst.isAtomic()));
1244 
1245         // Remember that this was the last unordered store we saw for DSE. We
1246         // don't yet handle DSE on ordered or volatile stores since we don't
1247         // have a good way to model the ordering requirement for following
1248         // passes  once the store is removed.  We could insert a fence, but
1249         // since fences are slightly stronger than stores in their ordering,
1250         // it's not clear this is a profitable transform. Another option would
1251         // be to merge the ordering with that of the post dominating store.
1252         if (MemInst.isUnordered() && !MemInst.isVolatile())
1253           LastStore = &Inst;
1254         else
1255           LastStore = nullptr;
1256       }
1257     }
1258   }
1259 
1260   return Changed;
1261 }
1262 
1263 bool EarlyCSE::run() {
1264   // Note, deque is being used here because there is significant performance
1265   // gains over vector when the container becomes very large due to the
1266   // specific access patterns. For more information see the mailing list
1267   // discussion on this:
1268   // http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html
1269   std::deque<StackNode *> nodesToProcess;
1270 
1271   bool Changed = false;
1272 
1273   // Process the root node.
1274   nodesToProcess.push_back(new StackNode(
1275       AvailableValues, AvailableLoads, AvailableInvariants, AvailableCalls,
1276       CurrentGeneration, DT.getRootNode(),
1277       DT.getRootNode()->begin(), DT.getRootNode()->end()));
1278 
1279   assert(!CurrentGeneration && "Create a new EarlyCSE instance to rerun it.");
1280 
1281   // Process the stack.
1282   while (!nodesToProcess.empty()) {
1283     // Grab the first item off the stack. Set the current generation, remove
1284     // the node from the stack, and process it.
1285     StackNode *NodeToProcess = nodesToProcess.back();
1286 
1287     // Initialize class members.
1288     CurrentGeneration = NodeToProcess->currentGeneration();
1289 
1290     // Check if the node needs to be processed.
1291     if (!NodeToProcess->isProcessed()) {
1292       // Process the node.
1293       Changed |= processNode(NodeToProcess->node());
1294       NodeToProcess->childGeneration(CurrentGeneration);
1295       NodeToProcess->process();
1296     } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
1297       // Push the next child onto the stack.
1298       DomTreeNode *child = NodeToProcess->nextChild();
1299       nodesToProcess.push_back(
1300           new StackNode(AvailableValues, AvailableLoads, AvailableInvariants,
1301                         AvailableCalls, NodeToProcess->childGeneration(),
1302                         child, child->begin(), child->end()));
1303     } else {
1304       // It has been processed, and there are no more children to process,
1305       // so delete it and pop it off the stack.
1306       delete NodeToProcess;
1307       nodesToProcess.pop_back();
1308     }
1309   } // while (!nodes...)
1310 
1311   return Changed;
1312 }
1313 
1314 PreservedAnalyses EarlyCSEPass::run(Function &F,
1315                                     FunctionAnalysisManager &AM) {
1316   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1317   auto &TTI = AM.getResult<TargetIRAnalysis>(F);
1318   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1319   auto &AC = AM.getResult<AssumptionAnalysis>(F);
1320   auto *MSSA =
1321       UseMemorySSA ? &AM.getResult<MemorySSAAnalysis>(F).getMSSA() : nullptr;
1322 
1323   EarlyCSE CSE(F.getParent()->getDataLayout(), TLI, TTI, DT, AC, MSSA);
1324 
1325   if (!CSE.run())
1326     return PreservedAnalyses::all();
1327 
1328   PreservedAnalyses PA;
1329   PA.preserveSet<CFGAnalyses>();
1330   PA.preserve<GlobalsAA>();
1331   if (UseMemorySSA)
1332     PA.preserve<MemorySSAAnalysis>();
1333   return PA;
1334 }
1335 
1336 namespace {
1337 
1338 /// A simple and fast domtree-based CSE pass.
1339 ///
1340 /// This pass does a simple depth-first walk over the dominator tree,
1341 /// eliminating trivially redundant instructions and using instsimplify to
1342 /// canonicalize things as it goes. It is intended to be fast and catch obvious
1343 /// cases so that instcombine and other passes are more effective. It is
1344 /// expected that a later pass of GVN will catch the interesting/hard cases.
1345 template<bool UseMemorySSA>
1346 class EarlyCSELegacyCommonPass : public FunctionPass {
1347 public:
1348   static char ID;
1349 
1350   EarlyCSELegacyCommonPass() : FunctionPass(ID) {
1351     if (UseMemorySSA)
1352       initializeEarlyCSEMemSSALegacyPassPass(*PassRegistry::getPassRegistry());
1353     else
1354       initializeEarlyCSELegacyPassPass(*PassRegistry::getPassRegistry());
1355   }
1356 
1357   bool runOnFunction(Function &F) override {
1358     if (skipFunction(F))
1359       return false;
1360 
1361     auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1362     auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1363     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1364     auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1365     auto *MSSA =
1366         UseMemorySSA ? &getAnalysis<MemorySSAWrapperPass>().getMSSA() : nullptr;
1367 
1368     EarlyCSE CSE(F.getParent()->getDataLayout(), TLI, TTI, DT, AC, MSSA);
1369 
1370     return CSE.run();
1371   }
1372 
1373   void getAnalysisUsage(AnalysisUsage &AU) const override {
1374     AU.addRequired<AssumptionCacheTracker>();
1375     AU.addRequired<DominatorTreeWrapperPass>();
1376     AU.addRequired<TargetLibraryInfoWrapperPass>();
1377     AU.addRequired<TargetTransformInfoWrapperPass>();
1378     if (UseMemorySSA) {
1379       AU.addRequired<MemorySSAWrapperPass>();
1380       AU.addPreserved<MemorySSAWrapperPass>();
1381     }
1382     AU.addPreserved<GlobalsAAWrapperPass>();
1383     AU.addPreserved<AAResultsWrapperPass>();
1384     AU.setPreservesCFG();
1385   }
1386 };
1387 
1388 } // end anonymous namespace
1389 
1390 using EarlyCSELegacyPass = EarlyCSELegacyCommonPass</*UseMemorySSA=*/false>;
1391 
1392 template<>
1393 char EarlyCSELegacyPass::ID = 0;
1394 
1395 INITIALIZE_PASS_BEGIN(EarlyCSELegacyPass, "early-cse", "Early CSE", false,
1396                       false)
1397 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
1398 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1399 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1400 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1401 INITIALIZE_PASS_END(EarlyCSELegacyPass, "early-cse", "Early CSE", false, false)
1402 
1403 using EarlyCSEMemSSALegacyPass =
1404     EarlyCSELegacyCommonPass</*UseMemorySSA=*/true>;
1405 
1406 template<>
1407 char EarlyCSEMemSSALegacyPass::ID = 0;
1408 
1409 FunctionPass *llvm::createEarlyCSEPass(bool UseMemorySSA) {
1410   if (UseMemorySSA)
1411     return new EarlyCSEMemSSALegacyPass();
1412   else
1413     return new EarlyCSELegacyPass();
1414 }
1415 
1416 INITIALIZE_PASS_BEGIN(EarlyCSEMemSSALegacyPass, "early-cse-memssa",
1417                       "Early CSE w/ MemorySSA", false, false)
1418 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
1419 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1420 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1421 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1422 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
1423 INITIALIZE_PASS_END(EarlyCSEMemSSALegacyPass, "early-cse-memssa",
1424                     "Early CSE w/ MemorySSA", false, false)
1425