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