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