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