Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 1 | //===- EarlyCSE.cpp - Simple and fast CSE pass ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs a simple dominator tree walk that eliminates trivially |
| 11 | // redundant instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar/EarlyCSE.h" |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Hashing.h" |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ScopedHashTable.h" |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AssumptionCache.h" |
Geoff Berry | 354fac2 | 2016-04-28 14:59:27 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InstructionSimplify.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 27 | #include "llvm/IR/IntrinsicInst.h" |
| 28 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/RecyclingAllocator.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/Local.h" |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/MemorySSA.h" |
Lenny Maiorani | 9eefc81 | 2014-09-20 13:29:20 +0000 | [diff] [blame] | 36 | #include <deque> |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 38 | using namespace llvm::PatternMatch; |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 39 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "early-cse" |
| 41 | |
Chris Lattner | 4cb3654 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 42 | STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd"); |
| 43 | STATISTIC(NumCSE, "Number of instructions CSE'd"); |
Chad Rosier | 1a4bc11 | 2016-04-22 18:47:21 +0000 | [diff] [blame] | 44 | STATISTIC(NumCSECVP, "Number of compare instructions CVP'd"); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 45 | STATISTIC(NumCSELoad, "Number of load instructions CSE'd"); |
| 46 | STATISTIC(NumCSECall, "Number of call instructions CSE'd"); |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 47 | STATISTIC(NumDSE, "Number of trivial dead stores removed"); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 50 | // SimpleValue |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 53 | namespace { |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 54 | /// \brief Struct representing the available values in the scoped hash table. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 55 | struct SimpleValue { |
| 56 | Instruction *Inst; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 57 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 58 | SimpleValue(Instruction *I) : Inst(I) { |
| 59 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 60 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 62 | bool isSentinel() const { |
| 63 | return Inst == DenseMapInfo<Instruction *>::getEmptyKey() || |
| 64 | Inst == DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 65 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 66 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 67 | static bool canHandle(Instruction *Inst) { |
| 68 | // This can only handle non-void readnone functions. |
| 69 | if (CallInst *CI = dyn_cast<CallInst>(Inst)) |
| 70 | return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy(); |
| 71 | return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) || |
| 72 | isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) || |
| 73 | isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) || |
| 74 | isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) || |
| 75 | isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst); |
| 76 | } |
| 77 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 79 | |
| 80 | namespace llvm { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 81 | template <> struct DenseMapInfo<SimpleValue> { |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 82 | static inline SimpleValue getEmptyKey() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 83 | return DenseMapInfo<Instruction *>::getEmptyKey(); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 85 | static inline SimpleValue getTombstoneKey() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 86 | return DenseMapInfo<Instruction *>::getTombstoneKey(); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 88 | static unsigned getHashValue(SimpleValue Val); |
| 89 | static bool isEqual(SimpleValue LHS, SimpleValue RHS); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 90 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 93 | unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 94 | Instruction *Inst = Val.Inst; |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 95 | // Hash in all of the operands as pointers. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 96 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst)) { |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 97 | Value *LHS = BinOp->getOperand(0); |
| 98 | Value *RHS = BinOp->getOperand(1); |
| 99 | if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1)) |
| 100 | std::swap(LHS, RHS); |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 101 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 102 | return hash_combine(BinOp->getOpcode(), LHS, RHS); |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 105 | if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) { |
| 106 | Value *LHS = CI->getOperand(0); |
| 107 | Value *RHS = CI->getOperand(1); |
| 108 | CmpInst::Predicate Pred = CI->getPredicate(); |
| 109 | if (Inst->getOperand(0) > Inst->getOperand(1)) { |
| 110 | std::swap(LHS, RHS); |
| 111 | Pred = CI->getSwappedPredicate(); |
| 112 | } |
| 113 | return hash_combine(Inst->getOpcode(), Pred, LHS, RHS); |
| 114 | } |
| 115 | |
| 116 | if (CastInst *CI = dyn_cast<CastInst>(Inst)) |
| 117 | return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0)); |
| 118 | |
| 119 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst)) |
| 120 | return hash_combine(EVI->getOpcode(), EVI->getOperand(0), |
| 121 | hash_combine_range(EVI->idx_begin(), EVI->idx_end())); |
| 122 | |
| 123 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst)) |
| 124 | return hash_combine(IVI->getOpcode(), IVI->getOperand(0), |
| 125 | IVI->getOperand(1), |
| 126 | hash_combine_range(IVI->idx_begin(), IVI->idx_end())); |
| 127 | |
| 128 | assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) || |
| 129 | isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) || |
| 130 | isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) || |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 131 | isa<ShuffleVectorInst>(Inst)) && |
| 132 | "Invalid/unknown instruction"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 134 | // Mix in the opcode. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 135 | return hash_combine( |
| 136 | Inst->getOpcode(), |
| 137 | hash_combine_range(Inst->value_op_begin(), Inst->value_op_end())); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 140 | bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 141 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
| 142 | |
| 143 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 144 | return LHSI == RHSI; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 145 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 146 | if (LHSI->getOpcode() != RHSI->getOpcode()) |
| 147 | return false; |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 148 | if (LHSI->isIdenticalToWhenDefined(RHSI)) |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 149 | return true; |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 150 | |
| 151 | // If we're not strictly identical, we still might be a commutable instruction |
| 152 | if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) { |
| 153 | if (!LHSBinOp->isCommutative()) |
| 154 | return false; |
| 155 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 156 | assert(isa<BinaryOperator>(RHSI) && |
| 157 | "same opcode, but different instruction type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 158 | BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI); |
| 159 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 160 | // Commuted equality |
| 161 | return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) && |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 162 | LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 163 | } |
| 164 | if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 165 | assert(isa<CmpInst>(RHSI) && |
| 166 | "same opcode, but different instruction type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 167 | CmpInst *RHSCmp = cast<CmpInst>(RHSI); |
| 168 | // Commuted equality |
| 169 | return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) && |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 170 | LHSCmp->getOperand(1) == RHSCmp->getOperand(0) && |
| 171 | LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate(); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | return false; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 177 | //===----------------------------------------------------------------------===// |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 178 | // CallValue |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 179 | //===----------------------------------------------------------------------===// |
| 180 | |
| 181 | namespace { |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 182 | /// \brief Struct representing the available call values in the scoped hash |
| 183 | /// table. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 184 | struct CallValue { |
| 185 | Instruction *Inst; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 186 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 187 | CallValue(Instruction *I) : Inst(I) { |
| 188 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 189 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 190 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 191 | bool isSentinel() const { |
| 192 | return Inst == DenseMapInfo<Instruction *>::getEmptyKey() || |
| 193 | Inst == DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 194 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 195 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 196 | static bool canHandle(Instruction *Inst) { |
| 197 | // Don't value number anything that returns void. |
| 198 | if (Inst->getType()->isVoidTy()) |
| 199 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 200 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 201 | CallInst *CI = dyn_cast<CallInst>(Inst); |
| 202 | if (!CI || !CI->onlyReadsMemory()) |
| 203 | return false; |
| 204 | return true; |
| 205 | } |
| 206 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 207 | } |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 208 | |
| 209 | namespace llvm { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 210 | template <> struct DenseMapInfo<CallValue> { |
| 211 | static inline CallValue getEmptyKey() { |
| 212 | return DenseMapInfo<Instruction *>::getEmptyKey(); |
| 213 | } |
| 214 | static inline CallValue getTombstoneKey() { |
| 215 | return DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 216 | } |
| 217 | static unsigned getHashValue(CallValue Val); |
| 218 | static bool isEqual(CallValue LHS, CallValue RHS); |
| 219 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 220 | } |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 222 | unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 223 | Instruction *Inst = Val.Inst; |
Benjamin Kramer | 6ab86b1 | 2015-02-01 12:30:59 +0000 | [diff] [blame] | 224 | // Hash all of the operands as pointers and mix in the opcode. |
| 225 | return hash_combine( |
| 226 | Inst->getOpcode(), |
| 227 | hash_combine_range(Inst->value_op_begin(), Inst->value_op_end())); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 230 | bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 231 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 232 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 233 | return LHSI == RHSI; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 234 | return LHSI->isIdenticalTo(RHSI); |
| 235 | } |
| 236 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 238 | // EarlyCSE implementation |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 239 | //===----------------------------------------------------------------------===// |
| 240 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 241 | namespace { |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 242 | /// \brief A simple and fast domtree-based CSE pass. |
| 243 | /// |
| 244 | /// This pass does a simple depth-first walk over the dominator tree, |
| 245 | /// eliminating trivially redundant instructions and using instsimplify to |
| 246 | /// canonicalize things as it goes. It is intended to be fast and catch obvious |
| 247 | /// cases so that instcombine and other passes are more effective. It is |
| 248 | /// 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] | 249 | class EarlyCSE { |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 250 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 251 | const TargetLibraryInfo &TLI; |
| 252 | const TargetTransformInfo &TTI; |
| 253 | DominatorTree &DT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 254 | AssumptionCache &AC; |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 255 | MemorySSA *MSSA; |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 256 | typedef RecyclingAllocator< |
| 257 | BumpPtrAllocator, ScopedHashTableVal<SimpleValue, Value *>> AllocatorTy; |
| 258 | typedef ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>, |
Chris Lattner | d815f69 | 2011-01-03 01:42:46 +0000 | [diff] [blame] | 259 | AllocatorTy> ScopedHTType; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 260 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 261 | /// \brief A scoped hash table of the current values of all of our simple |
| 262 | /// scalar expressions. |
| 263 | /// |
| 264 | /// As we walk down the domtree, we look to see if instructions are in this: |
| 265 | /// if so, we replace them with what we find, otherwise we insert them so |
| 266 | /// that dominated values can succeed in their lookup. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 267 | ScopedHTType AvailableValues; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 268 | |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 269 | /// A scoped hash table of the current values of previously encounted memory |
| 270 | /// locations. |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 271 | /// |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 272 | /// This allows us to get efficient access to dominating loads or stores when |
| 273 | /// we have a fully redundant load. In addition to the most recent load, we |
| 274 | /// keep track of a generation count of the read, which is compared against |
| 275 | /// the current generation count. The current generation count is incremented |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 276 | /// after every possibly writing memory operation, which ensures that we only |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 277 | /// CSE loads with other loads that have no intervening store. Ordering |
| 278 | /// events (such as fences or atomic instructions) increment the generation |
| 279 | /// count as well; essentially, we model these as writes to all possible |
| 280 | /// locations. Note that atomic and/or volatile loads and stores can be |
| 281 | /// present the table; it is the responsibility of the consumer to inspect |
| 282 | /// the atomicity/volatility if needed. |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 283 | struct LoadValue { |
Philip Reames | 32b5518 | 2016-05-06 01:13:58 +0000 | [diff] [blame] | 284 | Instruction *DefInst; |
Arnaud A. de Grandmaison | 859b2ac | 2015-10-09 09:23:01 +0000 | [diff] [blame] | 285 | unsigned Generation; |
| 286 | int MatchingId; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 287 | bool IsAtomic; |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 288 | bool IsInvariant; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 289 | LoadValue() |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 290 | : DefInst(nullptr), Generation(0), MatchingId(-1), IsAtomic(false), |
| 291 | IsInvariant(false) {} |
Geoff Berry | 5ae272c | 2016-04-28 15:22:37 +0000 | [diff] [blame] | 292 | LoadValue(Instruction *Inst, unsigned Generation, unsigned MatchingId, |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 293 | bool IsAtomic, bool IsInvariant) |
| 294 | : DefInst(Inst), Generation(Generation), MatchingId(MatchingId), |
| 295 | IsAtomic(IsAtomic), IsInvariant(IsInvariant) {} |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 296 | }; |
| 297 | typedef RecyclingAllocator<BumpPtrAllocator, |
| 298 | ScopedHashTableVal<Value *, LoadValue>> |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 299 | LoadMapAllocator; |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 300 | typedef ScopedHashTable<Value *, LoadValue, DenseMapInfo<Value *>, |
| 301 | LoadMapAllocator> LoadHTType; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 302 | LoadHTType AvailableLoads; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 303 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 304 | /// \brief A scoped hash table of the current values of read-only call |
| 305 | /// values. |
| 306 | /// |
| 307 | /// It uses the same generation count as loads. |
Geoff Berry | 2f64c20 | 2016-05-13 17:54:58 +0000 | [diff] [blame] | 308 | typedef ScopedHashTable<CallValue, std::pair<Instruction *, unsigned>> |
| 309 | CallHTType; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 310 | CallHTType AvailableCalls; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 311 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 312 | /// \brief This is the current generation of the memory value. |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 313 | unsigned CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 314 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 315 | /// \brief Set up the EarlyCSE runner for a particular function. |
Benjamin Kramer | 6db3338 | 2015-10-15 15:08:58 +0000 | [diff] [blame] | 316 | EarlyCSE(const TargetLibraryInfo &TLI, const TargetTransformInfo &TTI, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 317 | DominatorTree &DT, AssumptionCache &AC, MemorySSA *MSSA) |
| 318 | : TLI(TLI), TTI(TTI), DT(DT), AC(AC), MSSA(MSSA), CurrentGeneration(0) {} |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 319 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 320 | bool run(); |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 321 | |
| 322 | private: |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 323 | // Almost a POD, but needs to call the constructors for the scoped hash |
| 324 | // tables so that a new scope gets pushed on. These are RAII so that the |
| 325 | // scope gets popped when the NodeScope is destroyed. |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 326 | class NodeScope { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 327 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 328 | NodeScope(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads, |
| 329 | CallHTType &AvailableCalls) |
| 330 | : Scope(AvailableValues), LoadScope(AvailableLoads), |
| 331 | CallScope(AvailableCalls) {} |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 332 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 333 | private: |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 334 | NodeScope(const NodeScope &) = delete; |
| 335 | void operator=(const NodeScope &) = delete; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 336 | |
| 337 | ScopedHTType::ScopeTy Scope; |
| 338 | LoadHTType::ScopeTy LoadScope; |
| 339 | CallHTType::ScopeTy CallScope; |
| 340 | }; |
| 341 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 342 | // 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] | 343 | // first traversal of the tree. This includes scopes for values, loads, and |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 344 | // 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] | 345 | // children do not need to be store separately. |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 346 | class StackNode { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 347 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 348 | StackNode(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads, |
| 349 | CallHTType &AvailableCalls, unsigned cg, DomTreeNode *n, |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 350 | DomTreeNode::iterator child, DomTreeNode::iterator end) |
| 351 | : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child), |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 352 | EndIter(end), Scopes(AvailableValues, AvailableLoads, AvailableCalls), |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 353 | Processed(false) {} |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 354 | |
| 355 | // Accessors. |
| 356 | unsigned currentGeneration() { return CurrentGeneration; } |
| 357 | unsigned childGeneration() { return ChildGeneration; } |
| 358 | void childGeneration(unsigned generation) { ChildGeneration = generation; } |
| 359 | DomTreeNode *node() { return Node; } |
| 360 | DomTreeNode::iterator childIter() { return ChildIter; } |
| 361 | DomTreeNode *nextChild() { |
| 362 | DomTreeNode *child = *ChildIter; |
| 363 | ++ChildIter; |
| 364 | return child; |
| 365 | } |
| 366 | DomTreeNode::iterator end() { return EndIter; } |
| 367 | bool isProcessed() { return Processed; } |
| 368 | void process() { Processed = true; } |
| 369 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 370 | private: |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 371 | StackNode(const StackNode &) = delete; |
| 372 | void operator=(const StackNode &) = delete; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 373 | |
| 374 | // Members. |
| 375 | unsigned CurrentGeneration; |
| 376 | unsigned ChildGeneration; |
| 377 | DomTreeNode *Node; |
| 378 | DomTreeNode::iterator ChildIter; |
| 379 | DomTreeNode::iterator EndIter; |
| 380 | NodeScope Scopes; |
| 381 | bool Processed; |
| 382 | }; |
| 383 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 384 | /// \brief Wrapper class to handle memory instructions, including loads, |
| 385 | /// stores and intrinsic loads and stores defined by the target. |
| 386 | class ParseMemoryInst { |
| 387 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 388 | ParseMemoryInst(Instruction *Inst, const TargetTransformInfo &TTI) |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 389 | : IsTargetMemInst(false), Inst(Inst) { |
| 390 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) |
| 391 | if (TTI.getTgtMemIntrinsic(II, Info) && Info.NumMemRefs == 1) |
| 392 | IsTargetMemInst = true; |
| 393 | } |
| 394 | bool isLoad() const { |
| 395 | if (IsTargetMemInst) return Info.ReadMem; |
| 396 | return isa<LoadInst>(Inst); |
| 397 | } |
| 398 | bool isStore() const { |
| 399 | if (IsTargetMemInst) return Info.WriteMem; |
| 400 | return isa<StoreInst>(Inst); |
| 401 | } |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 402 | bool isAtomic() const { |
| 403 | if (IsTargetMemInst) { |
| 404 | assert(Info.IsSimple && "need to refine IsSimple in TTI"); |
| 405 | return false; |
| 406 | } |
| 407 | return Inst->isAtomic(); |
| 408 | } |
| 409 | bool isUnordered() const { |
| 410 | if (IsTargetMemInst) { |
| 411 | assert(Info.IsSimple && "need to refine IsSimple in TTI"); |
| 412 | return true; |
| 413 | } |
| 414 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 415 | return LI->isUnordered(); |
| 416 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 417 | return SI->isUnordered(); |
| 418 | } |
| 419 | // Conservative answer |
| 420 | return !Inst->isAtomic(); |
| 421 | } |
| 422 | |
| 423 | bool isVolatile() const { |
| 424 | if (IsTargetMemInst) { |
| 425 | assert(Info.IsSimple && "need to refine IsSimple in TTI"); |
| 426 | return false; |
| 427 | } |
| 428 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 429 | return LI->isVolatile(); |
| 430 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 431 | return SI->isVolatile(); |
| 432 | } |
| 433 | // Conservative answer |
| 434 | return true; |
| 435 | } |
| 436 | |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 437 | bool isInvariantLoad() const { |
| 438 | if (auto *LI = dyn_cast<LoadInst>(Inst)) |
Sanjoy Das | 1ab2fad | 2016-06-16 21:00:57 +0000 | [diff] [blame] | 439 | return LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr; |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 440 | return false; |
| 441 | } |
Junmo Park | 80440eb | 2016-02-18 10:09:20 +0000 | [diff] [blame] | 442 | |
Arnaud A. de Grandmaison | 6fd488b | 2015-10-06 13:35:30 +0000 | [diff] [blame] | 443 | bool isMatchingMemLoc(const ParseMemoryInst &Inst) const { |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 444 | return (getPointerOperand() == Inst.getPointerOperand() && |
| 445 | getMatchingId() == Inst.getMatchingId()); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 446 | } |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 447 | bool isValid() const { return getPointerOperand() != nullptr; } |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 448 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 449 | // For regular (non-intrinsic) loads/stores, this is set to -1. For |
| 450 | // intrinsic loads/stores, the id is retrieved from the corresponding |
| 451 | // field in the MemIntrinsicInfo structure. That field contains |
| 452 | // non-negative values only. |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 453 | int getMatchingId() const { |
| 454 | if (IsTargetMemInst) return Info.MatchingId; |
| 455 | return -1; |
| 456 | } |
| 457 | Value *getPointerOperand() const { |
| 458 | if (IsTargetMemInst) return Info.PtrVal; |
| 459 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 460 | return LI->getPointerOperand(); |
| 461 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 462 | return SI->getPointerOperand(); |
| 463 | } |
| 464 | return nullptr; |
| 465 | } |
| 466 | bool mayReadFromMemory() const { |
| 467 | if (IsTargetMemInst) return Info.ReadMem; |
| 468 | return Inst->mayReadFromMemory(); |
| 469 | } |
| 470 | bool mayWriteToMemory() const { |
| 471 | if (IsTargetMemInst) return Info.WriteMem; |
| 472 | return Inst->mayWriteToMemory(); |
| 473 | } |
| 474 | |
| 475 | private: |
| 476 | bool IsTargetMemInst; |
| 477 | MemIntrinsicInfo Info; |
| 478 | Instruction *Inst; |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 479 | }; |
| 480 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 481 | bool processNode(DomTreeNode *Node); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 482 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 483 | Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const { |
Sanjay Patel | 1c9867d | 2017-01-03 00:16:24 +0000 | [diff] [blame] | 484 | if (auto *LI = dyn_cast<LoadInst>(Inst)) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 485 | return LI; |
Sanjay Patel | 1c9867d | 2017-01-03 00:16:24 +0000 | [diff] [blame] | 486 | if (auto *SI = dyn_cast<StoreInst>(Inst)) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 487 | return SI->getValueOperand(); |
| 488 | assert(isa<IntrinsicInst>(Inst) && "Instruction not supported"); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 489 | return TTI.getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst), |
| 490 | ExpectedType); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 491 | } |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 492 | |
| 493 | bool isSameMemGeneration(unsigned EarlierGeneration, unsigned LaterGeneration, |
| 494 | Instruction *EarlierInst, Instruction *LaterInst); |
| 495 | |
| 496 | void removeMSSA(Instruction *Inst) { |
| 497 | if (!MSSA) |
| 498 | return; |
Geoff Berry | 91e9a5c | 2016-10-25 16:18:47 +0000 | [diff] [blame] | 499 | // Removing a store here can leave MemorySSA in an unoptimized state by |
| 500 | // creating MemoryPhis that have identical arguments and by creating |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 501 | // MemoryUses whose defining access is not an actual clobber. We handle the |
Geoff Berry | 91e9a5c | 2016-10-25 16:18:47 +0000 | [diff] [blame] | 502 | // phi case eagerly here. The non-optimized MemoryUse case is lazily |
| 503 | // updated by MemorySSA getClobberingMemoryAccess. |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 504 | if (MemoryAccess *MA = MSSA->getMemoryAccess(Inst)) { |
| 505 | // Optimize MemoryPhi nodes that may become redundant by having all the |
| 506 | // same input values once MA is removed. |
| 507 | SmallVector<MemoryPhi *, 4> PhisToCheck; |
| 508 | SmallVector<MemoryAccess *, 8> WorkQueue; |
| 509 | WorkQueue.push_back(MA); |
| 510 | // Process MemoryPhi nodes in FIFO order using a ever-growing vector since |
| 511 | // we shouldn't be processing that many phis and this will avoid an |
| 512 | // allocation in almost all cases. |
| 513 | for (unsigned I = 0; I < WorkQueue.size(); ++I) { |
| 514 | MemoryAccess *WI = WorkQueue[I]; |
| 515 | |
| 516 | for (auto *U : WI->users()) |
| 517 | if (MemoryPhi *MP = dyn_cast<MemoryPhi>(U)) |
| 518 | PhisToCheck.push_back(MP); |
| 519 | |
| 520 | MSSA->removeMemoryAccess(WI); |
| 521 | |
| 522 | for (MemoryPhi *MP : PhisToCheck) { |
| 523 | MemoryAccess *FirstIn = MP->getIncomingValue(0); |
| 524 | if (all_of(MP->incoming_values(), |
| 525 | [=](Use &In) { return In == FirstIn; })) |
| 526 | WorkQueue.push_back(MP); |
| 527 | } |
| 528 | PhisToCheck.clear(); |
| 529 | } |
| 530 | } |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 531 | } |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 532 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 534 | |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 535 | /// Determine if the memory referenced by LaterInst is from the same heap |
| 536 | /// version as EarlierInst. |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 537 | /// This is currently called in two scenarios: |
| 538 | /// |
| 539 | /// load p |
| 540 | /// ... |
| 541 | /// load p |
| 542 | /// |
| 543 | /// and |
| 544 | /// |
| 545 | /// x = load p |
| 546 | /// ... |
| 547 | /// store x, p |
| 548 | /// |
| 549 | /// in both cases we want to verify that there are no possible writes to the |
| 550 | /// memory referenced by p between the earlier and later instruction. |
| 551 | bool EarlyCSE::isSameMemGeneration(unsigned EarlierGeneration, |
| 552 | unsigned LaterGeneration, |
| 553 | Instruction *EarlierInst, |
| 554 | Instruction *LaterInst) { |
| 555 | // Check the simple memory generation tracking first. |
| 556 | if (EarlierGeneration == LaterGeneration) |
| 557 | return true; |
| 558 | |
| 559 | if (!MSSA) |
| 560 | return false; |
| 561 | |
| 562 | // Since we know LaterDef dominates LaterInst and EarlierInst dominates |
| 563 | // LaterInst, if LaterDef dominates EarlierInst then it can't occur between |
| 564 | // EarlierInst and LaterInst and neither can any other write that potentially |
| 565 | // clobbers LaterInst. |
Geoff Berry | 91e9a5c | 2016-10-25 16:18:47 +0000 | [diff] [blame] | 566 | MemoryAccess *LaterDef = |
| 567 | MSSA->getWalker()->getClobberingMemoryAccess(LaterInst); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 568 | return MSSA->dominates(LaterDef, MSSA->getMemoryAccess(EarlierInst)); |
| 569 | } |
| 570 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 571 | bool EarlyCSE::processNode(DomTreeNode *Node) { |
Chad Rosier | 1a4bc11 | 2016-04-22 18:47:21 +0000 | [diff] [blame] | 572 | bool Changed = false; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 573 | BasicBlock *BB = Node->getBlock(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 574 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 575 | // If this block has a single predecessor, then the predecessor is the parent |
| 576 | // of the domtree node and all of the live out memory values are still current |
| 577 | // in this block. If this block has multiple predecessors, then they could |
| 578 | // have invalidated the live-out memory values of our parent value. For now, |
| 579 | // just be conservative and invalidate memory if this block has multiple |
| 580 | // predecessors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 581 | if (!BB->getSinglePredecessor()) |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 582 | ++CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 583 | |
Philip Reames | 7c78ef7 | 2015-05-22 23:53:24 +0000 | [diff] [blame] | 584 | // If this node has a single predecessor which ends in a conditional branch, |
| 585 | // 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] | 586 | // 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] | 587 | // which reaches this block where the condition might hold a different |
| 588 | // value. Since we're adding this to the scoped hash table (like any other |
| 589 | // def), it will have been popped if we encounter a future merge block. |
| 590 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 591 | if (auto *BI = dyn_cast<BranchInst>(Pred->getTerminator())) |
| 592 | if (BI->isConditional()) |
| 593 | if (auto *CondInst = dyn_cast<Instruction>(BI->getCondition())) |
| 594 | if (SimpleValue::canHandle(CondInst)) { |
| 595 | assert(BI->getSuccessor(0) == BB || BI->getSuccessor(1) == BB); |
| 596 | auto *ConditionalConstant = (BI->getSuccessor(0) == BB) ? |
| 597 | ConstantInt::getTrue(BB->getContext()) : |
| 598 | ConstantInt::getFalse(BB->getContext()); |
| 599 | AvailableValues.insert(CondInst, ConditionalConstant); |
| 600 | DEBUG(dbgs() << "EarlyCSE CVP: Add conditional value for '" |
| 601 | << CondInst->getName() << "' as " << *ConditionalConstant |
| 602 | << " in " << BB->getName() << "\n"); |
Chad Rosier | 1a4bc11 | 2016-04-22 18:47:21 +0000 | [diff] [blame] | 603 | // Replace all dominated uses with the known value. |
| 604 | if (unsigned Count = |
| 605 | replaceDominatedUsesWith(CondInst, ConditionalConstant, DT, |
| 606 | BasicBlockEdge(Pred, BB))) { |
| 607 | Changed = true; |
| 608 | NumCSECVP = NumCSECVP + Count; |
| 609 | } |
Philip Reames | 7c78ef7 | 2015-05-22 23:53:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 612 | /// LastStore - Keep track of the last non-volatile store that we saw... for |
| 613 | /// as long as there in no instruction that reads memory. If we see a store |
| 614 | /// to the same location, we delete the dead store. This zaps trivial dead |
| 615 | /// stores which can occur in bitfield code among other things. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 616 | Instruction *LastStore = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 617 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 618 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 619 | |
| 620 | // See if any instructions in the block can be eliminated. If so, do it. If |
| 621 | // not, add them to AvailableValues. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 622 | 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] | 623 | Instruction *Inst = &*I++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 625 | // Dead instructions should just be removed. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 626 | if (isInstructionTriviallyDead(Inst, &TLI)) { |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 627 | DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n'); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 628 | removeMSSA(Inst); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 629 | Inst->eraseFromParent(); |
| 630 | Changed = true; |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 631 | ++NumSimplify; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 632 | continue; |
| 633 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 634 | |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 635 | // Skip assume intrinsics, they don't really have side effects (although |
| 636 | // they're marked as such to ensure preservation of control dependencies), |
| 637 | // and this pass will not disturb any of the assumption's control |
| 638 | // dependencies. |
| 639 | if (match(Inst, m_Intrinsic<Intrinsic::assume>())) { |
| 640 | DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n'); |
| 641 | continue; |
| 642 | } |
| 643 | |
Anna Thomas | b2d12b8 | 2016-08-09 20:00:47 +0000 | [diff] [blame] | 644 | // Skip invariant.start intrinsics since they only read memory, and we can |
| 645 | // forward values across it. Also, we dont need to consume the last store |
| 646 | // since the semantics of invariant.start allow us to perform DSE of the |
| 647 | // last store, if there was a store following invariant.start. Consider: |
| 648 | // |
| 649 | // store 30, i8* p |
| 650 | // invariant.start(p) |
| 651 | // store 40, i8* p |
| 652 | // We can DSE the store to 30, since the store 40 to invariant location p |
| 653 | // causes undefined behaviour. |
| 654 | if (match(Inst, m_Intrinsic<Intrinsic::invariant_start>())) |
| 655 | continue; |
| 656 | |
Sanjoy Das | ee81b23 | 2016-04-29 21:52:58 +0000 | [diff] [blame] | 657 | if (match(Inst, m_Intrinsic<Intrinsic::experimental_guard>())) { |
Sanjoy Das | 107aefc | 2016-04-29 22:23:16 +0000 | [diff] [blame] | 658 | if (auto *CondI = |
| 659 | dyn_cast<Instruction>(cast<CallInst>(Inst)->getArgOperand(0))) { |
Sanjoy Das | ee81b23 | 2016-04-29 21:52:58 +0000 | [diff] [blame] | 660 | // The condition we're on guarding here is true for all dominated |
| 661 | // locations. |
| 662 | if (SimpleValue::canHandle(CondI)) |
| 663 | AvailableValues.insert(CondI, ConstantInt::getTrue(BB->getContext())); |
| 664 | } |
| 665 | |
| 666 | // Guard intrinsics read all memory, but don't write any memory. |
| 667 | // Accordingly, don't update the generation but consume the last store (to |
| 668 | // avoid an incorrect DSE). |
| 669 | LastStore = nullptr; |
| 670 | continue; |
| 671 | } |
| 672 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 673 | // If the instruction can be simplified (e.g. X+0 = X) then replace it with |
| 674 | // its simpler value. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 675 | if (Value *V = SimplifyInstruction(Inst, DL, &TLI, &DT, &AC)) { |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 676 | DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n'); |
David Majnemer | 130b9f9 | 2016-07-29 05:39:21 +0000 | [diff] [blame] | 677 | bool Killed = false; |
David Majnemer | b8da3a2 | 2016-06-25 00:04:10 +0000 | [diff] [blame] | 678 | if (!Inst->use_empty()) { |
| 679 | Inst->replaceAllUsesWith(V); |
| 680 | Changed = true; |
| 681 | } |
| 682 | if (isInstructionTriviallyDead(Inst, &TLI)) { |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 683 | removeMSSA(Inst); |
David Majnemer | b8da3a2 | 2016-06-25 00:04:10 +0000 | [diff] [blame] | 684 | Inst->eraseFromParent(); |
| 685 | Changed = true; |
David Majnemer | 130b9f9 | 2016-07-29 05:39:21 +0000 | [diff] [blame] | 686 | Killed = true; |
David Majnemer | b8da3a2 | 2016-06-25 00:04:10 +0000 | [diff] [blame] | 687 | } |
David Majnemer | 130b9f9 | 2016-07-29 05:39:21 +0000 | [diff] [blame] | 688 | if (Changed) |
David Majnemer | b8da3a2 | 2016-06-25 00:04:10 +0000 | [diff] [blame] | 689 | ++NumSimplify; |
David Majnemer | 130b9f9 | 2016-07-29 05:39:21 +0000 | [diff] [blame] | 690 | if (Killed) |
David Majnemer | b8da3a2 | 2016-06-25 00:04:10 +0000 | [diff] [blame] | 691 | continue; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 692 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 693 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 694 | // If this is a simple instruction that we can value number, process it. |
| 695 | if (SimpleValue::canHandle(Inst)) { |
| 696 | // See if the instruction has an available value. If so, use it. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 697 | if (Value *V = AvailableValues.lookup(Inst)) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 698 | DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n'); |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 699 | if (auto *I = dyn_cast<Instruction>(V)) |
| 700 | I->andIRFlags(Inst); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 701 | Inst->replaceAllUsesWith(V); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 702 | removeMSSA(Inst); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 703 | Inst->eraseFromParent(); |
| 704 | Changed = true; |
| 705 | ++NumCSE; |
| 706 | continue; |
| 707 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 708 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 709 | // Otherwise, just remember that this value is available. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 710 | AvailableValues.insert(Inst, Inst); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 711 | continue; |
| 712 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 713 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 714 | ParseMemoryInst MemInst(Inst, TTI); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 715 | // If this is a non-volatile load, process it. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 716 | if (MemInst.isValid() && MemInst.isLoad()) { |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 717 | // (conservatively) we can't peak past the ordering implied by this |
| 718 | // operation, but we can add this load to our set of available values |
| 719 | if (MemInst.isVolatile() || !MemInst.isUnordered()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 720 | LastStore = nullptr; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 721 | ++CurrentGeneration; |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 722 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 723 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 724 | // 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] | 725 | // generation or the load is known to be from an invariant location, |
| 726 | // replace this instruction. |
| 727 | // |
Geoff Berry | 64f5ed1 | 2016-08-31 17:45:31 +0000 | [diff] [blame] | 728 | // If either the dominating load or the current load are invariant, then |
| 729 | // we can assume the current load loads the same value as the dominating |
| 730 | // load. |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 731 | LoadValue InVal = AvailableLoads.lookup(MemInst.getPointerOperand()); |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 732 | if (InVal.DefInst != nullptr && |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 733 | InVal.MatchingId == MemInst.getMatchingId() && |
| 734 | // We don't yet handle removing loads with ordering of any kind. |
| 735 | !MemInst.isVolatile() && MemInst.isUnordered() && |
| 736 | // 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] | 737 | InVal.IsAtomic >= MemInst.isAtomic() && |
| 738 | (InVal.IsInvariant || MemInst.isInvariantLoad() || |
| 739 | isSameMemGeneration(InVal.Generation, CurrentGeneration, |
| 740 | InVal.DefInst, Inst))) { |
Philip Reames | 32b5518 | 2016-05-06 01:13:58 +0000 | [diff] [blame] | 741 | Value *Op = getOrCreateResult(InVal.DefInst, Inst->getType()); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 742 | if (Op != nullptr) { |
| 743 | DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst |
Philip Reames | 32b5518 | 2016-05-06 01:13:58 +0000 | [diff] [blame] | 744 | << " to: " << *InVal.DefInst << '\n'); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 745 | if (!Inst->use_empty()) |
| 746 | Inst->replaceAllUsesWith(Op); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 747 | removeMSSA(Inst); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 748 | Inst->eraseFromParent(); |
| 749 | Changed = true; |
| 750 | ++NumCSELoad; |
| 751 | continue; |
| 752 | } |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 753 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 754 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 755 | // Otherwise, remember that we have this instruction. |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 756 | AvailableLoads.insert( |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 757 | MemInst.getPointerOperand(), |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 758 | LoadValue(Inst, CurrentGeneration, MemInst.getMatchingId(), |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 759 | MemInst.isAtomic(), MemInst.isInvariantLoad())); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 760 | LastStore = nullptr; |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 761 | continue; |
| 762 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 764 | // If this instruction may read from memory, forget LastStore. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 765 | // Load/store intrinsics will indicate both a read and a write to |
| 766 | // memory. The target may override this (e.g. so that a store intrinsic |
| 767 | // does not read from memory, and thus will be treated the same as a |
| 768 | // regular store for commoning purposes). |
| 769 | if (Inst->mayReadFromMemory() && |
| 770 | !(MemInst.isValid() && !MemInst.mayReadFromMemory())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 771 | LastStore = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 773 | // If this is a read-only call, process it. |
| 774 | if (CallValue::canHandle(Inst)) { |
| 775 | // If we have an available version of this call, and if it is the right |
| 776 | // generation, replace this instruction. |
Geoff Berry | 2f64c20 | 2016-05-13 17:54:58 +0000 | [diff] [blame] | 777 | std::pair<Instruction *, unsigned> InVal = AvailableCalls.lookup(Inst); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 778 | if (InVal.first != nullptr && |
| 779 | isSameMemGeneration(InVal.second, CurrentGeneration, InVal.first, |
| 780 | Inst)) { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 781 | DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst |
| 782 | << " to: " << *InVal.first << '\n'); |
| 783 | if (!Inst->use_empty()) |
| 784 | Inst->replaceAllUsesWith(InVal.first); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 785 | removeMSSA(Inst); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 786 | Inst->eraseFromParent(); |
| 787 | Changed = true; |
| 788 | ++NumCSECall; |
| 789 | continue; |
| 790 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 792 | // Otherwise, remember that we have this instruction. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 793 | AvailableCalls.insert( |
Geoff Berry | 2f64c20 | 2016-05-13 17:54:58 +0000 | [diff] [blame] | 794 | Inst, std::pair<Instruction *, unsigned>(Inst, CurrentGeneration)); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 795 | continue; |
| 796 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 797 | |
Philip Reames | dfd890d | 2015-08-27 01:32:33 +0000 | [diff] [blame] | 798 | // A release fence requires that all stores complete before it, but does |
| 799 | // not prevent the reordering of following loads 'before' the fence. As a |
| 800 | // result, we don't need to consider it as writing to memory and don't need |
| 801 | // to advance the generation. We do need to prevent DSE across the fence, |
| 802 | // but that's handled above. |
| 803 | if (FenceInst *FI = dyn_cast<FenceInst>(Inst)) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 804 | if (FI->getOrdering() == AtomicOrdering::Release) { |
Philip Reames | dfd890d | 2015-08-27 01:32:33 +0000 | [diff] [blame] | 805 | assert(Inst->mayReadFromMemory() && "relied on to prevent DSE above"); |
| 806 | continue; |
| 807 | } |
| 808 | |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 809 | // write back DSE - If we write back the same value we just loaded from |
| 810 | // the same location and haven't passed any intervening writes or ordering |
| 811 | // operations, we can remove the write. The primary benefit is in allowing |
| 812 | // the available load table to remain valid and value forward past where |
| 813 | // the store originally was. |
| 814 | if (MemInst.isValid() && MemInst.isStore()) { |
| 815 | LoadValue InVal = AvailableLoads.lookup(MemInst.getPointerOperand()); |
Philip Reames | 32b5518 | 2016-05-06 01:13:58 +0000 | [diff] [blame] | 816 | if (InVal.DefInst && |
| 817 | InVal.DefInst == getOrCreateResult(Inst, InVal.DefInst->getType()) && |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 818 | InVal.MatchingId == MemInst.getMatchingId() && |
| 819 | // We don't yet handle removing stores with ordering of any kind. |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 820 | !MemInst.isVolatile() && MemInst.isUnordered() && |
| 821 | isSameMemGeneration(InVal.Generation, CurrentGeneration, |
| 822 | InVal.DefInst, Inst)) { |
| 823 | // It is okay to have a LastStore to a different pointer here if MemorySSA |
| 824 | // tells us that the load and store are from the same memory generation. |
| 825 | // In that case, LastStore should keep its present value since we're |
| 826 | // removing the current store. |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 827 | assert((!LastStore || |
| 828 | ParseMemoryInst(LastStore, TTI).getPointerOperand() == |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 829 | MemInst.getPointerOperand() || |
| 830 | MSSA) && |
| 831 | "can't have an intervening store if not using MemorySSA!"); |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 832 | DEBUG(dbgs() << "EarlyCSE DSE (writeback): " << *Inst << '\n'); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 833 | removeMSSA(Inst); |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 834 | Inst->eraseFromParent(); |
| 835 | Changed = true; |
| 836 | ++NumDSE; |
| 837 | // We can avoid incrementing the generation count since we were able |
| 838 | // to eliminate this store. |
| 839 | continue; |
| 840 | } |
| 841 | } |
| 842 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 843 | // Okay, this isn't something we can CSE at all. Check to see if it is |
| 844 | // something that could modify memory. If so, our available memory values |
| 845 | // cannot be used so bump the generation count. |
Chris Lattner | e0e32a9 | 2011-01-03 03:46:34 +0000 | [diff] [blame] | 846 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 847 | ++CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 848 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 849 | if (MemInst.isValid() && MemInst.isStore()) { |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 850 | // 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] | 851 | // location with no intervening loads. Delete the earlier store. |
| 852 | // At the moment, we don't remove ordered stores, but do remove |
| 853 | // unordered atomic stores. There's no special requirement (for |
| 854 | // unordered atomics) about removing atomic stores only in favor of |
| 855 | // other atomic stores since we we're going to execute the non-atomic |
| 856 | // one anyway and the atomic one might never have become visible. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 857 | if (LastStore) { |
| 858 | ParseMemoryInst LastStoreMemInst(LastStore, TTI); |
Philip Reames | 15145fb | 2015-12-17 18:50:50 +0000 | [diff] [blame] | 859 | assert(LastStoreMemInst.isUnordered() && |
| 860 | !LastStoreMemInst.isVolatile() && |
| 861 | "Violated invariant"); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 862 | if (LastStoreMemInst.isMatchingMemLoc(MemInst)) { |
| 863 | DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore |
| 864 | << " due to: " << *Inst << '\n'); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 865 | removeMSSA(LastStore); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 866 | LastStore->eraseFromParent(); |
| 867 | Changed = true; |
| 868 | ++NumDSE; |
| 869 | LastStore = nullptr; |
| 870 | } |
Philip Reames | 018dbf1 | 2014-11-18 17:46:32 +0000 | [diff] [blame] | 871 | // fallthrough - we can exploit information about this store |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 872 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 874 | // Okay, we just invalidated anything we knew about loaded values. Try |
| 875 | // to salvage *something* by remembering that the stored value is a live |
| 876 | // version of the pointer. It is safe to forward from volatile stores |
| 877 | // to non-volatile loads, so we don't have to check for volatility of |
| 878 | // the store. |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 879 | AvailableLoads.insert( |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 880 | MemInst.getPointerOperand(), |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 881 | LoadValue(Inst, CurrentGeneration, MemInst.getMatchingId(), |
Sanjoy Das | 1ab2fad | 2016-06-16 21:00:57 +0000 | [diff] [blame] | 882 | MemInst.isAtomic(), /*IsInvariant=*/false)); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 883 | |
Philip Reames | 15145fb | 2015-12-17 18:50:50 +0000 | [diff] [blame] | 884 | // Remember that this was the last unordered store we saw for DSE. We |
| 885 | // don't yet handle DSE on ordered or volatile stores since we don't |
| 886 | // have a good way to model the ordering requirement for following |
| 887 | // passes once the store is removed. We could insert a fence, but |
| 888 | // since fences are slightly stronger than stores in their ordering, |
| 889 | // it's not clear this is a profitable transform. Another option would |
| 890 | // be to merge the ordering with that of the post dominating store. |
| 891 | if (MemInst.isUnordered() && !MemInst.isVolatile()) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 892 | LastStore = Inst; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 893 | else |
| 894 | LastStore = nullptr; |
Chris Lattner | e0e32a9 | 2011-01-03 03:46:34 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 897 | } |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 899 | return Changed; |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 900 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 901 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 902 | bool EarlyCSE::run() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 903 | // Note, deque is being used here because there is significant performance |
| 904 | // gains over vector when the container becomes very large due to the |
| 905 | // specific access patterns. For more information see the mailing list |
| 906 | // discussion on this: |
Tanya Lattner | 0d28f80 | 2015-08-05 03:51:17 +0000 | [diff] [blame] | 907 | // 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] | 908 | std::deque<StackNode *> nodesToProcess; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 909 | |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 910 | bool Changed = false; |
| 911 | |
| 912 | // Process the root node. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 913 | nodesToProcess.push_back(new StackNode( |
| 914 | AvailableValues, AvailableLoads, AvailableCalls, CurrentGeneration, |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 915 | DT.getRootNode(), DT.getRootNode()->begin(), DT.getRootNode()->end())); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 916 | |
| 917 | // Save the current generation. |
| 918 | unsigned LiveOutGeneration = CurrentGeneration; |
| 919 | |
| 920 | // Process the stack. |
| 921 | while (!nodesToProcess.empty()) { |
| 922 | // Grab the first item off the stack. Set the current generation, remove |
| 923 | // the node from the stack, and process it. |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 924 | StackNode *NodeToProcess = nodesToProcess.back(); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 925 | |
| 926 | // Initialize class members. |
| 927 | CurrentGeneration = NodeToProcess->currentGeneration(); |
| 928 | |
| 929 | // Check if the node needs to be processed. |
| 930 | if (!NodeToProcess->isProcessed()) { |
| 931 | // Process the node. |
| 932 | Changed |= processNode(NodeToProcess->node()); |
| 933 | NodeToProcess->childGeneration(CurrentGeneration); |
| 934 | NodeToProcess->process(); |
| 935 | } else if (NodeToProcess->childIter() != NodeToProcess->end()) { |
| 936 | // Push the next child onto the stack. |
| 937 | DomTreeNode *child = NodeToProcess->nextChild(); |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 938 | nodesToProcess.push_back( |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 939 | new StackNode(AvailableValues, AvailableLoads, AvailableCalls, |
| 940 | NodeToProcess->childGeneration(), child, child->begin(), |
| 941 | child->end())); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 942 | } else { |
| 943 | // It has been processed, and there are no more children to process, |
| 944 | // so delete it and pop it off the stack. |
| 945 | delete NodeToProcess; |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 946 | nodesToProcess.pop_back(); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 947 | } |
| 948 | } // while (!nodes...) |
| 949 | |
| 950 | // Reset the current generation. |
| 951 | CurrentGeneration = LiveOutGeneration; |
| 952 | |
| 953 | return Changed; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 954 | } |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 955 | |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 956 | PreservedAnalyses EarlyCSEPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 957 | FunctionAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 958 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| 959 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
| 960 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 961 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 962 | auto *MSSA = |
| 963 | UseMemorySSA ? &AM.getResult<MemorySSAAnalysis>(F).getMSSA() : nullptr; |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 964 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 965 | EarlyCSE CSE(TLI, TTI, DT, AC, MSSA); |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 966 | |
| 967 | if (!CSE.run()) |
| 968 | return PreservedAnalyses::all(); |
| 969 | |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 970 | PreservedAnalyses PA; |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 971 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | 02861d8 | 2016-06-08 21:31:55 +0000 | [diff] [blame] | 972 | PA.preserve<GlobalsAA>(); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 973 | if (UseMemorySSA) |
| 974 | PA.preserve<MemorySSAAnalysis>(); |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 975 | return PA; |
| 976 | } |
| 977 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 978 | namespace { |
| 979 | /// \brief A simple and fast domtree-based CSE pass. |
| 980 | /// |
| 981 | /// This pass does a simple depth-first walk over the dominator tree, |
| 982 | /// eliminating trivially redundant instructions and using instsimplify to |
| 983 | /// canonicalize things as it goes. It is intended to be fast and catch obvious |
| 984 | /// cases so that instcombine and other passes are more effective. It is |
| 985 | /// 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] | 986 | template<bool UseMemorySSA> |
| 987 | class EarlyCSELegacyCommonPass : public FunctionPass { |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 988 | public: |
| 989 | static char ID; |
| 990 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 991 | EarlyCSELegacyCommonPass() : FunctionPass(ID) { |
| 992 | if (UseMemorySSA) |
| 993 | initializeEarlyCSEMemSSALegacyPassPass(*PassRegistry::getPassRegistry()); |
| 994 | else |
| 995 | initializeEarlyCSELegacyPassPass(*PassRegistry::getPassRegistry()); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 999 | if (skipFunction(F)) |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1000 | return false; |
| 1001 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1002 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 1003 | auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1004 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1005 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1006 | auto *MSSA = |
| 1007 | UseMemorySSA ? &getAnalysis<MemorySSAWrapperPass>().getMSSA() : nullptr; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1008 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1009 | EarlyCSE CSE(TLI, TTI, DT, AC, MSSA); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1010 | |
| 1011 | return CSE.run(); |
| 1012 | } |
| 1013 | |
| 1014 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1015 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1016 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 1017 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1018 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1019 | if (UseMemorySSA) { |
| 1020 | AU.addRequired<MemorySSAWrapperPass>(); |
| 1021 | AU.addPreserved<MemorySSAWrapperPass>(); |
| 1022 | } |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 1023 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1024 | AU.setPreservesCFG(); |
| 1025 | } |
| 1026 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 1027 | } |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1028 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1029 | using EarlyCSELegacyPass = EarlyCSELegacyCommonPass</*UseMemorySSA=*/false>; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1030 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1031 | template<> |
| 1032 | char EarlyCSELegacyPass::ID = 0; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1033 | |
| 1034 | INITIALIZE_PASS_BEGIN(EarlyCSELegacyPass, "early-cse", "Early CSE", false, |
| 1035 | false) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1036 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1037 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1038 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1039 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 1040 | INITIALIZE_PASS_END(EarlyCSELegacyPass, "early-cse", "Early CSE", false, false) |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1041 | |
| 1042 | using EarlyCSEMemSSALegacyPass = |
| 1043 | EarlyCSELegacyCommonPass</*UseMemorySSA=*/true>; |
| 1044 | |
| 1045 | template<> |
| 1046 | char EarlyCSEMemSSALegacyPass::ID = 0; |
| 1047 | |
| 1048 | FunctionPass *llvm::createEarlyCSEPass(bool UseMemorySSA) { |
| 1049 | if (UseMemorySSA) |
| 1050 | return new EarlyCSEMemSSALegacyPass(); |
| 1051 | else |
| 1052 | return new EarlyCSELegacyPass(); |
| 1053 | } |
| 1054 | |
| 1055 | INITIALIZE_PASS_BEGIN(EarlyCSEMemSSALegacyPass, "early-cse-memssa", |
| 1056 | "Early CSE w/ MemorySSA", false, false) |
| 1057 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1058 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1059 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1060 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 1061 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
| 1062 | INITIALIZE_PASS_END(EarlyCSEMemSSALegacyPass, "early-cse-memssa", |
| 1063 | "Early CSE w/ MemorySSA", false, false) |