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