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