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 | |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/RecyclingAllocator.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.h" |
Lenny Maiorani | 9eefc81 | 2014-09-20 13:29:20 +0000 | [diff] [blame] | 32 | #include <deque> |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 34 | using namespace llvm::PatternMatch; |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 35 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "early-cse" |
| 37 | |
Chris Lattner | 4cb3654 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 38 | STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd"); |
| 39 | STATISTIC(NumCSE, "Number of instructions CSE'd"); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 40 | STATISTIC(NumCSELoad, "Number of load instructions CSE'd"); |
| 41 | STATISTIC(NumCSECall, "Number of call instructions CSE'd"); |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 42 | STATISTIC(NumDSE, "Number of trivial dead stores removed"); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 43 | |
| 44 | static unsigned getHash(const void *V) { |
| 45 | return DenseMapInfo<const void*>::getHashValue(V); |
| 46 | } |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +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 | }; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 77 | } |
| 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 | }; |
| 90 | } |
| 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 | if (isa<OverflowingBinaryOperator>(BinOp)) { |
| 102 | // Hash the overflow behavior |
| 103 | unsigned Overflow = |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 104 | BinOp->hasNoSignedWrap() * OverflowingBinaryOperator::NoSignedWrap | |
| 105 | BinOp->hasNoUnsignedWrap() * |
| 106 | OverflowingBinaryOperator::NoUnsignedWrap; |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 107 | return hash_combine(BinOp->getOpcode(), Overflow, LHS, RHS); |
| 108 | } |
| 109 | |
| 110 | return hash_combine(BinOp->getOpcode(), LHS, RHS); |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 113 | if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) { |
| 114 | Value *LHS = CI->getOperand(0); |
| 115 | Value *RHS = CI->getOperand(1); |
| 116 | CmpInst::Predicate Pred = CI->getPredicate(); |
| 117 | if (Inst->getOperand(0) > Inst->getOperand(1)) { |
| 118 | std::swap(LHS, RHS); |
| 119 | Pred = CI->getSwappedPredicate(); |
| 120 | } |
| 121 | return hash_combine(Inst->getOpcode(), Pred, LHS, RHS); |
| 122 | } |
| 123 | |
| 124 | if (CastInst *CI = dyn_cast<CastInst>(Inst)) |
| 125 | return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0)); |
| 126 | |
| 127 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst)) |
| 128 | return hash_combine(EVI->getOpcode(), EVI->getOperand(0), |
| 129 | hash_combine_range(EVI->idx_begin(), EVI->idx_end())); |
| 130 | |
| 131 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst)) |
| 132 | return hash_combine(IVI->getOpcode(), IVI->getOperand(0), |
| 133 | IVI->getOperand(1), |
| 134 | hash_combine_range(IVI->idx_begin(), IVI->idx_end())); |
| 135 | |
| 136 | assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) || |
| 137 | isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) || |
| 138 | isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) || |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 139 | isa<ShuffleVectorInst>(Inst)) && |
| 140 | "Invalid/unknown instruction"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 142 | // Mix in the opcode. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 143 | return hash_combine( |
| 144 | Inst->getOpcode(), |
| 145 | hash_combine_range(Inst->value_op_begin(), Inst->value_op_end())); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 148 | bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 149 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
| 150 | |
| 151 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 152 | return LHSI == RHSI; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 153 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 154 | if (LHSI->getOpcode() != RHSI->getOpcode()) |
| 155 | return false; |
| 156 | if (LHSI->isIdenticalTo(RHSI)) |
| 157 | return true; |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 158 | |
| 159 | // If we're not strictly identical, we still might be a commutable instruction |
| 160 | if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) { |
| 161 | if (!LHSBinOp->isCommutative()) |
| 162 | return false; |
| 163 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 164 | assert(isa<BinaryOperator>(RHSI) && |
| 165 | "same opcode, but different instruction type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 166 | BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI); |
| 167 | |
| 168 | // Check overflow attributes |
| 169 | if (isa<OverflowingBinaryOperator>(LHSBinOp)) { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 170 | assert(isa<OverflowingBinaryOperator>(RHSBinOp) && |
| 171 | "same opcode, but different operator type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 172 | if (LHSBinOp->hasNoUnsignedWrap() != RHSBinOp->hasNoUnsignedWrap() || |
| 173 | LHSBinOp->hasNoSignedWrap() != RHSBinOp->hasNoSignedWrap()) |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Commuted equality |
| 178 | return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) && |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 179 | LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 180 | } |
| 181 | if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 182 | assert(isa<CmpInst>(RHSI) && |
| 183 | "same opcode, but different instruction type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 184 | CmpInst *RHSCmp = cast<CmpInst>(RHSI); |
| 185 | // Commuted equality |
| 186 | return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) && |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 187 | LHSCmp->getOperand(1) == RHSCmp->getOperand(0) && |
| 188 | LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate(); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return false; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 194 | //===----------------------------------------------------------------------===// |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 195 | // CallValue |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
| 197 | |
| 198 | namespace { |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 199 | /// \brief Struct representing the available call values in the scoped hash |
| 200 | /// table. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 201 | struct CallValue { |
| 202 | Instruction *Inst; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 203 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 204 | CallValue(Instruction *I) : Inst(I) { |
| 205 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 206 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 207 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 208 | bool isSentinel() const { |
| 209 | return Inst == DenseMapInfo<Instruction *>::getEmptyKey() || |
| 210 | Inst == DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 211 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 212 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 213 | static bool canHandle(Instruction *Inst) { |
| 214 | // Don't value number anything that returns void. |
| 215 | if (Inst->getType()->isVoidTy()) |
| 216 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 217 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 218 | CallInst *CI = dyn_cast<CallInst>(Inst); |
| 219 | if (!CI || !CI->onlyReadsMemory()) |
| 220 | return false; |
| 221 | return true; |
| 222 | } |
| 223 | }; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | namespace llvm { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 227 | template <> struct DenseMapInfo<CallValue> { |
| 228 | static inline CallValue getEmptyKey() { |
| 229 | return DenseMapInfo<Instruction *>::getEmptyKey(); |
| 230 | } |
| 231 | static inline CallValue getTombstoneKey() { |
| 232 | return DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 233 | } |
| 234 | static unsigned getHashValue(CallValue Val); |
| 235 | static bool isEqual(CallValue LHS, CallValue RHS); |
| 236 | }; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 237 | } |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 239 | unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 240 | Instruction *Inst = Val.Inst; |
| 241 | // Hash in all of the operands as pointers. |
| 242 | unsigned Res = 0; |
Chris Lattner | 16ca19f | 2011-01-03 18:43:03 +0000 | [diff] [blame] | 243 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) { |
| 244 | assert(!Inst->getOperand(i)->getType()->isMetadataTy() && |
| 245 | "Cannot value number calls with metadata operands"); |
Eli Friedman | 154a967 | 2011-10-12 22:00:26 +0000 | [diff] [blame] | 246 | Res ^= getHash(Inst->getOperand(i)) << (i & 0xF); |
Chris Lattner | 16ca19f | 2011-01-03 18:43:03 +0000 | [diff] [blame] | 247 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 248 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 249 | // Mix in the opcode. |
| 250 | return (Res << 1) ^ Inst->getOpcode(); |
| 251 | } |
| 252 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 253 | bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 254 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 255 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 256 | return LHSI == RHSI; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 257 | return LHSI->isIdenticalTo(RHSI); |
| 258 | } |
| 259 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 261 | // EarlyCSE implementation |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 262 | //===----------------------------------------------------------------------===// |
| 263 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 264 | namespace { |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 265 | /// \brief A simple and fast domtree-based CSE pass. |
| 266 | /// |
| 267 | /// This pass does a simple depth-first walk over the dominator tree, |
| 268 | /// eliminating trivially redundant instructions and using instsimplify to |
| 269 | /// canonicalize things as it goes. It is intended to be fast and catch obvious |
| 270 | /// cases so that instcombine and other passes are more effective. It is |
| 271 | /// 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] | 272 | class EarlyCSE { |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 273 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 274 | Function &F; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 275 | const DataLayout *DL; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 276 | const TargetLibraryInfo &TLI; |
| 277 | const TargetTransformInfo &TTI; |
| 278 | DominatorTree &DT; |
| 279 | AssumptionCache &AC; |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 280 | typedef RecyclingAllocator< |
| 281 | BumpPtrAllocator, ScopedHashTableVal<SimpleValue, Value *>> AllocatorTy; |
| 282 | typedef ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>, |
Chris Lattner | d815f69 | 2011-01-03 01:42:46 +0000 | [diff] [blame] | 283 | AllocatorTy> ScopedHTType; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 284 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 285 | /// \brief A scoped hash table of the current values of all of our simple |
| 286 | /// scalar expressions. |
| 287 | /// |
| 288 | /// As we walk down the domtree, we look to see if instructions are in this: |
| 289 | /// if so, we replace them with what we find, otherwise we insert them so |
| 290 | /// that dominated values can succeed in their lookup. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 291 | ScopedHTType AvailableValues; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 292 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 293 | /// \brief A scoped hash table of the current values of loads. |
| 294 | /// |
| 295 | /// This allows us to get efficient access to dominating loads when we have |
| 296 | /// a fully redundant load. In addition to the most recent load, we keep |
| 297 | /// track of a generation count of the read, which is compared against the |
| 298 | /// current generation count. The current generation count is incremented |
| 299 | /// after every possibly writing memory operation, which ensures that we only |
| 300 | /// CSE loads with other loads that have no intervening store. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 301 | typedef RecyclingAllocator< |
| 302 | BumpPtrAllocator, |
| 303 | ScopedHashTableVal<Value *, std::pair<Value *, unsigned>>> |
| 304 | LoadMapAllocator; |
| 305 | typedef ScopedHashTable<Value *, std::pair<Value *, unsigned>, |
| 306 | DenseMapInfo<Value *>, LoadMapAllocator> LoadHTType; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 307 | LoadHTType AvailableLoads; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 308 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 309 | /// \brief A scoped hash table of the current values of read-only call |
| 310 | /// values. |
| 311 | /// |
| 312 | /// It uses the same generation count as loads. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 313 | typedef ScopedHashTable<CallValue, std::pair<Value *, unsigned>> CallHTType; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 314 | CallHTType AvailableCalls; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 315 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 316 | /// \brief This is the current generation of the memory value. |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 317 | unsigned CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 318 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 319 | /// \brief Set up the EarlyCSE runner for a particular function. |
| 320 | EarlyCSE(Function &F, const DataLayout *DL, const TargetLibraryInfo &TLI, |
| 321 | const TargetTransformInfo &TTI, DominatorTree &DT, |
| 322 | AssumptionCache &AC) |
| 323 | : F(F), DL(DL), TLI(TLI), TTI(TTI), DT(DT), AC(AC), CurrentGeneration(0) { |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 326 | bool run(); |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 327 | |
| 328 | private: |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 329 | // Almost a POD, but needs to call the constructors for the scoped hash |
| 330 | // tables so that a new scope gets pushed on. These are RAII so that the |
| 331 | // scope gets popped when the NodeScope is destroyed. |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 332 | class NodeScope { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 333 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 334 | NodeScope(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads, |
| 335 | CallHTType &AvailableCalls) |
| 336 | : Scope(AvailableValues), LoadScope(AvailableLoads), |
| 337 | CallScope(AvailableCalls) {} |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 338 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 339 | private: |
| 340 | NodeScope(const NodeScope &) LLVM_DELETED_FUNCTION; |
| 341 | void operator=(const NodeScope &) LLVM_DELETED_FUNCTION; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 342 | |
| 343 | ScopedHTType::ScopeTy Scope; |
| 344 | LoadHTType::ScopeTy LoadScope; |
| 345 | CallHTType::ScopeTy CallScope; |
| 346 | }; |
| 347 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 348 | // Contains all the needed information to create a stack for doing a depth |
| 349 | // first tranversal of the tree. This includes scopes for values, loads, and |
| 350 | // calls as well as the generation. There is a child iterator so that the |
| 351 | // children do not need to be store spearately. |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 352 | class StackNode { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 353 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 354 | StackNode(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads, |
| 355 | CallHTType &AvailableCalls, unsigned cg, DomTreeNode *n, |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 356 | DomTreeNode::iterator child, DomTreeNode::iterator end) |
| 357 | : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child), |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 358 | EndIter(end), Scopes(AvailableValues, AvailableLoads, AvailableCalls), |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 359 | Processed(false) {} |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 360 | |
| 361 | // Accessors. |
| 362 | unsigned currentGeneration() { return CurrentGeneration; } |
| 363 | unsigned childGeneration() { return ChildGeneration; } |
| 364 | void childGeneration(unsigned generation) { ChildGeneration = generation; } |
| 365 | DomTreeNode *node() { return Node; } |
| 366 | DomTreeNode::iterator childIter() { return ChildIter; } |
| 367 | DomTreeNode *nextChild() { |
| 368 | DomTreeNode *child = *ChildIter; |
| 369 | ++ChildIter; |
| 370 | return child; |
| 371 | } |
| 372 | DomTreeNode::iterator end() { return EndIter; } |
| 373 | bool isProcessed() { return Processed; } |
| 374 | void process() { Processed = true; } |
| 375 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 376 | private: |
| 377 | StackNode(const StackNode &) LLVM_DELETED_FUNCTION; |
| 378 | void operator=(const StackNode &) LLVM_DELETED_FUNCTION; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 379 | |
| 380 | // Members. |
| 381 | unsigned CurrentGeneration; |
| 382 | unsigned ChildGeneration; |
| 383 | DomTreeNode *Node; |
| 384 | DomTreeNode::iterator ChildIter; |
| 385 | DomTreeNode::iterator EndIter; |
| 386 | NodeScope Scopes; |
| 387 | bool Processed; |
| 388 | }; |
| 389 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 390 | /// \brief Wrapper class to handle memory instructions, including loads, |
| 391 | /// stores and intrinsic loads and stores defined by the target. |
| 392 | class ParseMemoryInst { |
| 393 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 394 | ParseMemoryInst(Instruction *Inst, const TargetTransformInfo &TTI) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 395 | : Load(false), Store(false), Vol(false), MayReadFromMemory(false), |
| 396 | MayWriteToMemory(false), MatchingId(-1), Ptr(nullptr) { |
| 397 | MayReadFromMemory = Inst->mayReadFromMemory(); |
| 398 | MayWriteToMemory = Inst->mayWriteToMemory(); |
| 399 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 400 | MemIntrinsicInfo Info; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 401 | if (!TTI.getTgtMemIntrinsic(II, Info)) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 402 | return; |
| 403 | if (Info.NumMemRefs == 1) { |
| 404 | Store = Info.WriteMem; |
| 405 | Load = Info.ReadMem; |
| 406 | MatchingId = Info.MatchingId; |
| 407 | MayReadFromMemory = Info.ReadMem; |
| 408 | MayWriteToMemory = Info.WriteMem; |
| 409 | Vol = Info.Vol; |
| 410 | Ptr = Info.PtrVal; |
| 411 | } |
| 412 | } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 413 | Load = true; |
| 414 | Vol = !LI->isSimple(); |
| 415 | Ptr = LI->getPointerOperand(); |
| 416 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 417 | Store = true; |
| 418 | Vol = !SI->isSimple(); |
| 419 | Ptr = SI->getPointerOperand(); |
| 420 | } |
| 421 | } |
| 422 | bool isLoad() { return Load; } |
| 423 | bool isStore() { return Store; } |
| 424 | bool isVolatile() { return Vol; } |
| 425 | bool isMatchingMemLoc(const ParseMemoryInst &Inst) { |
| 426 | return Ptr == Inst.Ptr && MatchingId == Inst.MatchingId; |
| 427 | } |
| 428 | bool isValid() { return Ptr != nullptr; } |
| 429 | int getMatchingId() { return MatchingId; } |
| 430 | Value *getPtr() { return Ptr; } |
| 431 | bool mayReadFromMemory() { return MayReadFromMemory; } |
| 432 | bool mayWriteToMemory() { return MayWriteToMemory; } |
| 433 | |
| 434 | private: |
| 435 | bool Load; |
| 436 | bool Store; |
| 437 | bool Vol; |
| 438 | bool MayReadFromMemory; |
| 439 | bool MayWriteToMemory; |
| 440 | // For regular (non-intrinsic) loads/stores, this is set to -1. For |
| 441 | // intrinsic loads/stores, the id is retrieved from the corresponding |
| 442 | // field in the MemIntrinsicInfo structure. That field contains |
| 443 | // non-negative values only. |
| 444 | int MatchingId; |
| 445 | Value *Ptr; |
| 446 | }; |
| 447 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 448 | bool processNode(DomTreeNode *Node); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 449 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 450 | Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const { |
| 451 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
| 452 | return LI; |
| 453 | else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 454 | return SI->getValueOperand(); |
| 455 | assert(isa<IntrinsicInst>(Inst) && "Instruction not supported"); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 456 | return TTI.getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst), |
| 457 | ExpectedType); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 458 | } |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 459 | }; |
| 460 | } |
| 461 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 462 | bool EarlyCSE::processNode(DomTreeNode *Node) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 463 | BasicBlock *BB = Node->getBlock(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 464 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 465 | // If this block has a single predecessor, then the predecessor is the parent |
| 466 | // of the domtree node and all of the live out memory values are still current |
| 467 | // in this block. If this block has multiple predecessors, then they could |
| 468 | // have invalidated the live-out memory values of our parent value. For now, |
| 469 | // just be conservative and invalidate memory if this block has multiple |
| 470 | // predecessors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 471 | if (!BB->getSinglePredecessor()) |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 472 | ++CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 474 | /// LastStore - Keep track of the last non-volatile store that we saw... for |
| 475 | /// as long as there in no instruction that reads memory. If we see a store |
| 476 | /// to the same location, we delete the dead store. This zaps trivial dead |
| 477 | /// stores which can occur in bitfield code among other things. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 478 | Instruction *LastStore = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 480 | bool Changed = false; |
| 481 | |
| 482 | // See if any instructions in the block can be eliminated. If so, do it. If |
| 483 | // not, add them to AvailableValues. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 484 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 485 | Instruction *Inst = I++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 487 | // Dead instructions should just be removed. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 488 | if (isInstructionTriviallyDead(Inst, &TLI)) { |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 489 | DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n'); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 490 | Inst->eraseFromParent(); |
| 491 | Changed = true; |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 492 | ++NumSimplify; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 493 | continue; |
| 494 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 495 | |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 496 | // Skip assume intrinsics, they don't really have side effects (although |
| 497 | // they're marked as such to ensure preservation of control dependencies), |
| 498 | // and this pass will not disturb any of the assumption's control |
| 499 | // dependencies. |
| 500 | if (match(Inst, m_Intrinsic<Intrinsic::assume>())) { |
| 501 | DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n'); |
| 502 | continue; |
| 503 | } |
| 504 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 505 | // If the instruction can be simplified (e.g. X+0 = X) then replace it with |
| 506 | // its simpler value. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 507 | if (Value *V = SimplifyInstruction(Inst, DL, &TLI, &DT, &AC)) { |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 508 | DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n'); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 509 | Inst->replaceAllUsesWith(V); |
| 510 | Inst->eraseFromParent(); |
| 511 | Changed = true; |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 512 | ++NumSimplify; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 513 | continue; |
| 514 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 515 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 516 | // If this is a simple instruction that we can value number, process it. |
| 517 | if (SimpleValue::canHandle(Inst)) { |
| 518 | // See if the instruction has an available value. If so, use it. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 519 | if (Value *V = AvailableValues.lookup(Inst)) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 520 | DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n'); |
| 521 | Inst->replaceAllUsesWith(V); |
| 522 | Inst->eraseFromParent(); |
| 523 | Changed = true; |
| 524 | ++NumCSE; |
| 525 | continue; |
| 526 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 527 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 528 | // Otherwise, just remember that this value is available. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 529 | AvailableValues.insert(Inst, Inst); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 530 | continue; |
| 531 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 532 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 533 | ParseMemoryInst MemInst(Inst, TTI); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 534 | // If this is a non-volatile load, process it. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 535 | if (MemInst.isValid() && MemInst.isLoad()) { |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 536 | // Ignore volatile loads. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 537 | if (MemInst.isVolatile()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 538 | LastStore = nullptr; |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 539 | continue; |
| 540 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 542 | // If we have an available version of this load, and if it is the right |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 543 | // generation, replace this instruction. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 544 | std::pair<Value *, unsigned> InVal = |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 545 | AvailableLoads.lookup(MemInst.getPtr()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 546 | if (InVal.first != nullptr && InVal.second == CurrentGeneration) { |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 547 | Value *Op = getOrCreateResult(InVal.first, Inst->getType()); |
| 548 | if (Op != nullptr) { |
| 549 | DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst |
| 550 | << " to: " << *InVal.first << '\n'); |
| 551 | if (!Inst->use_empty()) |
| 552 | Inst->replaceAllUsesWith(Op); |
| 553 | Inst->eraseFromParent(); |
| 554 | Changed = true; |
| 555 | ++NumCSELoad; |
| 556 | continue; |
| 557 | } |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 558 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 559 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 560 | // Otherwise, remember that we have this instruction. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 561 | AvailableLoads.insert(MemInst.getPtr(), std::pair<Value *, unsigned>( |
| 562 | Inst, CurrentGeneration)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 563 | LastStore = nullptr; |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 564 | continue; |
| 565 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 567 | // If this instruction may read from memory, forget LastStore. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 568 | // Load/store intrinsics will indicate both a read and a write to |
| 569 | // memory. The target may override this (e.g. so that a store intrinsic |
| 570 | // does not read from memory, and thus will be treated the same as a |
| 571 | // regular store for commoning purposes). |
| 572 | if (Inst->mayReadFromMemory() && |
| 573 | !(MemInst.isValid() && !MemInst.mayReadFromMemory())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 574 | LastStore = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 576 | // If this is a read-only call, process it. |
| 577 | if (CallValue::canHandle(Inst)) { |
| 578 | // If we have an available version of this call, and if it is the right |
| 579 | // generation, replace this instruction. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 580 | std::pair<Value *, unsigned> InVal = AvailableCalls.lookup(Inst); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 581 | if (InVal.first != nullptr && InVal.second == CurrentGeneration) { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 582 | DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst |
| 583 | << " to: " << *InVal.first << '\n'); |
| 584 | if (!Inst->use_empty()) |
| 585 | Inst->replaceAllUsesWith(InVal.first); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 586 | Inst->eraseFromParent(); |
| 587 | Changed = true; |
| 588 | ++NumCSECall; |
| 589 | continue; |
| 590 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 592 | // Otherwise, remember that we have this instruction. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 593 | AvailableCalls.insert( |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 594 | Inst, std::pair<Value *, unsigned>(Inst, CurrentGeneration)); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 595 | continue; |
| 596 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 597 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 598 | // Okay, this isn't something we can CSE at all. Check to see if it is |
| 599 | // something that could modify memory. If so, our available memory values |
| 600 | // cannot be used so bump the generation count. |
Chris Lattner | e0e32a9 | 2011-01-03 03:46:34 +0000 | [diff] [blame] | 601 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 602 | ++CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 603 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 604 | if (MemInst.isValid() && MemInst.isStore()) { |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 605 | // We do a trivial form of DSE if there are two stores to the same |
| 606 | // location with no intervening loads. Delete the earlier store. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 607 | if (LastStore) { |
| 608 | ParseMemoryInst LastStoreMemInst(LastStore, TTI); |
| 609 | if (LastStoreMemInst.isMatchingMemLoc(MemInst)) { |
| 610 | DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore |
| 611 | << " due to: " << *Inst << '\n'); |
| 612 | LastStore->eraseFromParent(); |
| 613 | Changed = true; |
| 614 | ++NumDSE; |
| 615 | LastStore = nullptr; |
| 616 | } |
Philip Reames | 018dbf1 | 2014-11-18 17:46:32 +0000 | [diff] [blame] | 617 | // fallthrough - we can exploit information about this store |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 618 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 620 | // Okay, we just invalidated anything we knew about loaded values. Try |
| 621 | // to salvage *something* by remembering that the stored value is a live |
| 622 | // version of the pointer. It is safe to forward from volatile stores |
| 623 | // to non-volatile loads, so we don't have to check for volatility of |
| 624 | // the store. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 625 | AvailableLoads.insert(MemInst.getPtr(), std::pair<Value *, unsigned>( |
| 626 | Inst, CurrentGeneration)); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 628 | // Remember that this was the last store we saw for DSE. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 629 | if (!MemInst.isVolatile()) |
| 630 | LastStore = Inst; |
Chris Lattner | e0e32a9 | 2011-01-03 03:46:34 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 633 | } |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 635 | return Changed; |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 636 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 637 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 638 | bool EarlyCSE::run() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 639 | // Note, deque is being used here because there is significant performance |
| 640 | // gains over vector when the container becomes very large due to the |
| 641 | // specific access patterns. For more information see the mailing list |
| 642 | // discussion on this: |
Lenny Maiorani | 9eefc81 | 2014-09-20 13:29:20 +0000 | [diff] [blame] | 643 | // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html |
| 644 | std::deque<StackNode *> nodesToProcess; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 645 | |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 646 | bool Changed = false; |
| 647 | |
| 648 | // Process the root node. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 649 | nodesToProcess.push_back(new StackNode( |
| 650 | AvailableValues, AvailableLoads, AvailableCalls, CurrentGeneration, |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 651 | DT.getRootNode(), DT.getRootNode()->begin(), DT.getRootNode()->end())); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 652 | |
| 653 | // Save the current generation. |
| 654 | unsigned LiveOutGeneration = CurrentGeneration; |
| 655 | |
| 656 | // Process the stack. |
| 657 | while (!nodesToProcess.empty()) { |
| 658 | // Grab the first item off the stack. Set the current generation, remove |
| 659 | // the node from the stack, and process it. |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 660 | StackNode *NodeToProcess = nodesToProcess.back(); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 661 | |
| 662 | // Initialize class members. |
| 663 | CurrentGeneration = NodeToProcess->currentGeneration(); |
| 664 | |
| 665 | // Check if the node needs to be processed. |
| 666 | if (!NodeToProcess->isProcessed()) { |
| 667 | // Process the node. |
| 668 | Changed |= processNode(NodeToProcess->node()); |
| 669 | NodeToProcess->childGeneration(CurrentGeneration); |
| 670 | NodeToProcess->process(); |
| 671 | } else if (NodeToProcess->childIter() != NodeToProcess->end()) { |
| 672 | // Push the next child onto the stack. |
| 673 | DomTreeNode *child = NodeToProcess->nextChild(); |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 674 | nodesToProcess.push_back( |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 675 | new StackNode(AvailableValues, AvailableLoads, AvailableCalls, |
| 676 | NodeToProcess->childGeneration(), child, child->begin(), |
| 677 | child->end())); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 678 | } else { |
| 679 | // It has been processed, and there are no more children to process, |
| 680 | // so delete it and pop it off the stack. |
| 681 | delete NodeToProcess; |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 682 | nodesToProcess.pop_back(); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 683 | } |
| 684 | } // while (!nodes...) |
| 685 | |
| 686 | // Reset the current generation. |
| 687 | CurrentGeneration = LiveOutGeneration; |
| 688 | |
| 689 | return Changed; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 690 | } |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 691 | |
| 692 | namespace { |
| 693 | /// \brief A simple and fast domtree-based CSE pass. |
| 694 | /// |
| 695 | /// This pass does a simple depth-first walk over the dominator tree, |
| 696 | /// eliminating trivially redundant instructions and using instsimplify to |
| 697 | /// canonicalize things as it goes. It is intended to be fast and catch obvious |
| 698 | /// cases so that instcombine and other passes are more effective. It is |
| 699 | /// expected that a later pass of GVN will catch the interesting/hard cases. |
| 700 | class EarlyCSELegacyPass : public FunctionPass { |
| 701 | public: |
| 702 | static char ID; |
| 703 | |
| 704 | EarlyCSELegacyPass() : FunctionPass(ID) { |
| 705 | initializeEarlyCSELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 706 | } |
| 707 | |
| 708 | bool runOnFunction(Function &F) override { |
| 709 | if (skipOptnoneFunction(F)) |
| 710 | return false; |
| 711 | |
| 712 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 713 | auto *DL = DLP ? &DLP->getDataLayout() : nullptr; |
| 714 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 715 | auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 716 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 717 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
| 718 | |
| 719 | EarlyCSE CSE(F, DL, TLI, TTI, DT, AC); |
| 720 | |
| 721 | return CSE.run(); |
| 722 | } |
| 723 | |
| 724 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 725 | AU.addRequired<AssumptionCacheTracker>(); |
| 726 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 727 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 728 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 729 | AU.setPreservesCFG(); |
| 730 | } |
| 731 | }; |
| 732 | } |
| 733 | |
| 734 | char EarlyCSELegacyPass::ID = 0; |
| 735 | |
| 736 | FunctionPass *llvm::createEarlyCSEPass() { return new EarlyCSELegacyPass(); } |
| 737 | |
| 738 | INITIALIZE_PASS_BEGIN(EarlyCSELegacyPass, "early-cse", "Early CSE", false, |
| 739 | false) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 740 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 741 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 742 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 743 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 744 | INITIALIZE_PASS_END(EarlyCSELegacyPass, "early-cse", "Early CSE", false, false) |