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