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" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMapInfo.h" |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Hashing.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ScopedHashTable.h" |
Davide Italiano | 0dc4778 | 2017-06-14 19:29:53 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AssumptionCache.h" |
Geoff Berry | 354fac2 | 2016-04-28 14:59:27 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/GlobalsModRef.h" |
Max Kazantsev | 3c284bd | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/GuardUtils.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/InstructionSimplify.h" |
Daniel Berlin | 554dcd8 | 2017-04-11 20:06:36 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/MemorySSA.h" |
| 28 | #include "llvm/Analysis/MemorySSAUpdater.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/TargetTransformInfo.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.h" |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 33 | #include "llvm/IR/BasicBlock.h" |
| 34 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Function.h" |
| 38 | #include "llvm/IR/InstrTypes.h" |
| 39 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 41 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Intrinsics.h" |
| 43 | #include "llvm/IR/LLVMContext.h" |
| 44 | #include "llvm/IR/PassManager.h" |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 45 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Type.h" |
| 47 | #include "llvm/IR/Use.h" |
| 48 | #include "llvm/IR/Value.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 49 | #include "llvm/Pass.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Allocator.h" |
| 51 | #include "llvm/Support/AtomicOrdering.h" |
| 52 | #include "llvm/Support/Casting.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Debug.h" |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 54 | #include "llvm/Support/DebugCounter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 55 | #include "llvm/Support/RecyclingAllocator.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 56 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/Scalar.h" |
Max Kazantsev | 3c284bd | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/GuardUtils.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 59 | #include <cassert> |
Lenny Maiorani | 9eefc81 | 2014-09-20 13:29:20 +0000 | [diff] [blame] | 60 | #include <deque> |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 61 | #include <memory> |
| 62 | #include <utility> |
| 63 | |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 64 | using namespace llvm; |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 65 | using namespace llvm::PatternMatch; |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 66 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 67 | #define DEBUG_TYPE "early-cse" |
| 68 | |
Chris Lattner | 4cb3654 | 2011-01-03 03:28:23 +0000 | [diff] [blame] | 69 | STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd"); |
| 70 | STATISTIC(NumCSE, "Number of instructions CSE'd"); |
Chad Rosier | 1a4bc11 | 2016-04-22 18:47:21 +0000 | [diff] [blame] | 71 | STATISTIC(NumCSECVP, "Number of compare instructions CVP'd"); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 72 | STATISTIC(NumCSELoad, "Number of load instructions CSE'd"); |
| 73 | STATISTIC(NumCSECall, "Number of call instructions CSE'd"); |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 74 | STATISTIC(NumDSE, "Number of trivial dead stores removed"); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 75 | |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 76 | DEBUG_COUNTER(CSECounter, "early-cse", |
| 77 | "Controls which instructions are removed"); |
| 78 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 80 | // SimpleValue |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 81 | //===----------------------------------------------------------------------===// |
| 82 | |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 83 | namespace { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 84 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 85 | /// Struct representing the available values in the scoped hash table. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 86 | struct SimpleValue { |
| 87 | Instruction *Inst; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 88 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 89 | SimpleValue(Instruction *I) : Inst(I) { |
| 90 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 91 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 92 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 93 | bool isSentinel() const { |
| 94 | return Inst == DenseMapInfo<Instruction *>::getEmptyKey() || |
| 95 | Inst == DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 96 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 97 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 98 | static bool canHandle(Instruction *Inst) { |
| 99 | // This can only handle non-void readnone functions. |
| 100 | if (CallInst *CI = dyn_cast<CallInst>(Inst)) |
| 101 | return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy(); |
| 102 | return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) || |
| 103 | isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) || |
| 104 | isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) || |
| 105 | isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) || |
| 106 | isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst); |
| 107 | } |
| 108 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 109 | |
| 110 | } // end anonymous namespace |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 111 | |
| 112 | namespace llvm { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 113 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 114 | template <> struct DenseMapInfo<SimpleValue> { |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 115 | static inline SimpleValue getEmptyKey() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 116 | return DenseMapInfo<Instruction *>::getEmptyKey(); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 117 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 119 | static inline SimpleValue getTombstoneKey() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 120 | return DenseMapInfo<Instruction *>::getTombstoneKey(); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 121 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 123 | static unsigned getHashValue(SimpleValue Val); |
| 124 | static bool isEqual(SimpleValue LHS, SimpleValue RHS); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 125 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 126 | |
| 127 | } // end namespace llvm |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 129 | unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 130 | Instruction *Inst = Val.Inst; |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 131 | // Hash in all of the operands as pointers. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 132 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst)) { |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 133 | Value *LHS = BinOp->getOperand(0); |
| 134 | Value *RHS = BinOp->getOperand(1); |
| 135 | if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1)) |
| 136 | std::swap(LHS, RHS); |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 137 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 138 | return hash_combine(BinOp->getOpcode(), LHS, RHS); |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 141 | if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) { |
| 142 | Value *LHS = CI->getOperand(0); |
| 143 | Value *RHS = CI->getOperand(1); |
| 144 | CmpInst::Predicate Pred = CI->getPredicate(); |
| 145 | if (Inst->getOperand(0) > Inst->getOperand(1)) { |
| 146 | std::swap(LHS, RHS); |
| 147 | Pred = CI->getSwappedPredicate(); |
| 148 | } |
| 149 | return hash_combine(Inst->getOpcode(), Pred, LHS, RHS); |
| 150 | } |
| 151 | |
Sanjay Patel | 558a465 | 2017-12-13 22:57:35 +0000 | [diff] [blame] | 152 | // Hash min/max/abs (cmp + select) to allow for commuted operands. |
| 153 | // Min/max may also have non-canonical compare predicate (eg, the compare for |
| 154 | // smin may use 'sgt' rather than 'slt'), and non-canonical operands in the |
| 155 | // compare. |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 156 | Value *A, *B; |
| 157 | SelectPatternFlavor SPF = matchSelectPattern(Inst, A, B).Flavor; |
Sanjay Patel | 558a465 | 2017-12-13 22:57:35 +0000 | [diff] [blame] | 158 | // TODO: We should also detect FP min/max. |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 159 | if (SPF == SPF_SMIN || SPF == SPF_SMAX || |
Craig Topper | f14e62c | 2018-05-21 18:42:42 +0000 | [diff] [blame] | 160 | SPF == SPF_UMIN || SPF == SPF_UMAX) { |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 161 | if (A > B) |
| 162 | std::swap(A, B); |
| 163 | return hash_combine(Inst->getOpcode(), SPF, A, B); |
| 164 | } |
Craig Topper | f14e62c | 2018-05-21 18:42:42 +0000 | [diff] [blame] | 165 | if (SPF == SPF_ABS || SPF == SPF_NABS) { |
| 166 | // ABS/NABS always puts the input in A and its negation in B. |
| 167 | return hash_combine(Inst->getOpcode(), SPF, A, B); |
| 168 | } |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 169 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 170 | if (CastInst *CI = dyn_cast<CastInst>(Inst)) |
| 171 | return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0)); |
| 172 | |
| 173 | if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst)) |
| 174 | return hash_combine(EVI->getOpcode(), EVI->getOperand(0), |
| 175 | hash_combine_range(EVI->idx_begin(), EVI->idx_end())); |
| 176 | |
| 177 | if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst)) |
| 178 | return hash_combine(IVI->getOpcode(), IVI->getOperand(0), |
| 179 | IVI->getOperand(1), |
| 180 | hash_combine_range(IVI->idx_begin(), IVI->idx_end())); |
| 181 | |
| 182 | assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) || |
| 183 | isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) || |
| 184 | isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) || |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 185 | isa<ShuffleVectorInst>(Inst)) && |
| 186 | "Invalid/unknown instruction"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 02a9776 | 2011-01-03 01:10:08 +0000 | [diff] [blame] | 188 | // Mix in the opcode. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 189 | return hash_combine( |
| 190 | Inst->getOpcode(), |
| 191 | hash_combine_range(Inst->value_op_begin(), Inst->value_op_end())); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 194 | bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) { |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 195 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
| 196 | |
| 197 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 198 | return LHSI == RHSI; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 199 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 200 | if (LHSI->getOpcode() != RHSI->getOpcode()) |
| 201 | return false; |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 202 | if (LHSI->isIdenticalToWhenDefined(RHSI)) |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 203 | return true; |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 204 | |
| 205 | // If we're not strictly identical, we still might be a commutable instruction |
| 206 | if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) { |
| 207 | if (!LHSBinOp->isCommutative()) |
| 208 | return false; |
| 209 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 210 | assert(isa<BinaryOperator>(RHSI) && |
| 211 | "same opcode, but different instruction type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 212 | BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI); |
| 213 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 214 | // Commuted equality |
| 215 | return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) && |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 216 | LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 217 | } |
| 218 | if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 219 | assert(isa<CmpInst>(RHSI) && |
| 220 | "same opcode, but different instruction type?"); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 221 | CmpInst *RHSCmp = cast<CmpInst>(RHSI); |
| 222 | // Commuted equality |
| 223 | return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) && |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 224 | LHSCmp->getOperand(1) == RHSCmp->getOperand(0) && |
| 225 | LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate(); |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Sanjay Patel | 558a465 | 2017-12-13 22:57:35 +0000 | [diff] [blame] | 228 | // Min/max/abs can occur with commuted operands, non-canonical predicates, |
| 229 | // and/or non-canonical operands. |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 230 | Value *LHSA, *LHSB; |
| 231 | SelectPatternFlavor LSPF = matchSelectPattern(LHSI, LHSA, LHSB).Flavor; |
Sanjay Patel | 558a465 | 2017-12-13 22:57:35 +0000 | [diff] [blame] | 232 | // TODO: We should also detect FP min/max. |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 233 | if (LSPF == SPF_SMIN || LSPF == SPF_SMAX || |
Sanjay Patel | 558a465 | 2017-12-13 22:57:35 +0000 | [diff] [blame] | 234 | LSPF == SPF_UMIN || LSPF == SPF_UMAX || |
| 235 | LSPF == SPF_ABS || LSPF == SPF_NABS) { |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 236 | Value *RHSA, *RHSB; |
| 237 | SelectPatternFlavor RSPF = matchSelectPattern(RHSI, RHSA, RHSB).Flavor; |
Craig Topper | f14e62c | 2018-05-21 18:42:42 +0000 | [diff] [blame] | 238 | if (LSPF == RSPF) { |
| 239 | // Abs results are placed in a defined order by matchSelectPattern. |
| 240 | if (LSPF == SPF_ABS || LSPF == SPF_NABS) |
| 241 | return LHSA == RHSA && LHSB == RHSB; |
| 242 | return ((LHSA == RHSA && LHSB == RHSB) || |
| 243 | (LHSA == RHSB && LHSB == RHSA)); |
| 244 | } |
Sanjay Patel | 3c7a35d | 2017-12-13 21:58:15 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Michael Ilseman | 336cb79 | 2012-10-09 16:57:38 +0000 | [diff] [blame] | 247 | return false; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 251 | // CallValue |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 252 | //===----------------------------------------------------------------------===// |
| 253 | |
| 254 | namespace { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 255 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 256 | /// Struct representing the available call values in the scoped hash |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 257 | /// table. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 258 | struct CallValue { |
| 259 | Instruction *Inst; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 260 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 261 | CallValue(Instruction *I) : Inst(I) { |
| 262 | assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); |
| 263 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 264 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 265 | bool isSentinel() const { |
| 266 | return Inst == DenseMapInfo<Instruction *>::getEmptyKey() || |
| 267 | Inst == DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 268 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 269 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 270 | static bool canHandle(Instruction *Inst) { |
| 271 | // Don't value number anything that returns void. |
| 272 | if (Inst->getType()->isVoidTy()) |
| 273 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 274 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 275 | CallInst *CI = dyn_cast<CallInst>(Inst); |
| 276 | if (!CI || !CI->onlyReadsMemory()) |
| 277 | return false; |
| 278 | return true; |
| 279 | } |
| 280 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 281 | |
| 282 | } // end anonymous namespace |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 283 | |
| 284 | namespace llvm { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 285 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 286 | template <> struct DenseMapInfo<CallValue> { |
| 287 | static inline CallValue getEmptyKey() { |
| 288 | return DenseMapInfo<Instruction *>::getEmptyKey(); |
| 289 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 290 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 291 | static inline CallValue getTombstoneKey() { |
| 292 | return DenseMapInfo<Instruction *>::getTombstoneKey(); |
| 293 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 294 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 295 | static unsigned getHashValue(CallValue Val); |
| 296 | static bool isEqual(CallValue LHS, CallValue RHS); |
| 297 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 298 | |
| 299 | } // end namespace llvm |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 301 | unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 302 | Instruction *Inst = Val.Inst; |
Benjamin Kramer | 6ab86b1 | 2015-02-01 12:30:59 +0000 | [diff] [blame] | 303 | // Hash all of the operands as pointers and mix in the opcode. |
| 304 | return hash_combine( |
| 305 | Inst->getOpcode(), |
| 306 | hash_combine_range(Inst->value_op_begin(), Inst->value_op_end())); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 309 | bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 310 | Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 311 | if (LHS.isSentinel() || RHS.isSentinel()) |
| 312 | return LHSI == RHSI; |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 313 | return LHSI->isIdenticalTo(RHSI); |
| 314 | } |
| 315 | |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 316 | //===----------------------------------------------------------------------===// |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 317 | // EarlyCSE implementation |
Chris Lattner | 79d8306 | 2011-01-03 02:20:48 +0000 | [diff] [blame] | 318 | //===----------------------------------------------------------------------===// |
| 319 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 320 | namespace { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 321 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 322 | /// A simple and fast domtree-based CSE pass. |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 323 | /// |
| 324 | /// This pass does a simple depth-first walk over the dominator tree, |
| 325 | /// eliminating trivially redundant instructions and using instsimplify to |
| 326 | /// canonicalize things as it goes. It is intended to be fast and catch obvious |
| 327 | /// cases so that instcombine and other passes are more effective. It is |
| 328 | /// 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] | 329 | class EarlyCSE { |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 330 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 331 | const TargetLibraryInfo &TLI; |
| 332 | const TargetTransformInfo &TTI; |
| 333 | DominatorTree &DT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 334 | AssumptionCache &AC; |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 335 | const SimplifyQuery SQ; |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 336 | MemorySSA *MSSA; |
Daniel Berlin | 17e8d0e | 2017-02-22 22:19:55 +0000 | [diff] [blame] | 337 | std::unique_ptr<MemorySSAUpdater> MSSAUpdater; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 338 | |
| 339 | using AllocatorTy = |
| 340 | RecyclingAllocator<BumpPtrAllocator, |
| 341 | ScopedHashTableVal<SimpleValue, Value *>>; |
| 342 | using ScopedHTType = |
| 343 | ScopedHashTable<SimpleValue, Value *, DenseMapInfo<SimpleValue>, |
| 344 | AllocatorTy>; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 345 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 346 | /// A scoped hash table of the current values of all of our simple |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 347 | /// scalar expressions. |
| 348 | /// |
| 349 | /// As we walk down the domtree, we look to see if instructions are in this: |
| 350 | /// if so, we replace them with what we find, otherwise we insert them so |
| 351 | /// that dominated values can succeed in their lookup. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 352 | ScopedHTType AvailableValues; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 353 | |
Hiroshi Inoue | f209649 | 2018-06-14 05:41:49 +0000 | [diff] [blame] | 354 | /// A scoped hash table of the current values of previously encountered |
| 355 | /// memory locations. |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 356 | /// |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 357 | /// This allows us to get efficient access to dominating loads or stores when |
| 358 | /// we have a fully redundant load. In addition to the most recent load, we |
| 359 | /// keep track of a generation count of the read, which is compared against |
| 360 | /// the current generation count. The current generation count is incremented |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 361 | /// after every possibly writing memory operation, which ensures that we only |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 362 | /// CSE loads with other loads that have no intervening store. Ordering |
| 363 | /// events (such as fences or atomic instructions) increment the generation |
| 364 | /// count as well; essentially, we model these as writes to all possible |
| 365 | /// locations. Note that atomic and/or volatile loads and stores can be |
| 366 | /// present the table; it is the responsibility of the consumer to inspect |
| 367 | /// the atomicity/volatility if needed. |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 368 | struct LoadValue { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 369 | Instruction *DefInst = nullptr; |
| 370 | unsigned Generation = 0; |
| 371 | int MatchingId = -1; |
| 372 | bool IsAtomic = false; |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 373 | |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 374 | LoadValue() = default; |
Geoff Berry | 5ae272c | 2016-04-28 15:22:37 +0000 | [diff] [blame] | 375 | LoadValue(Instruction *Inst, unsigned Generation, unsigned MatchingId, |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 376 | bool IsAtomic) |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 377 | : DefInst(Inst), Generation(Generation), MatchingId(MatchingId), |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 378 | IsAtomic(IsAtomic) {} |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 379 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 380 | |
| 381 | using LoadMapAllocator = |
| 382 | RecyclingAllocator<BumpPtrAllocator, |
| 383 | ScopedHashTableVal<Value *, LoadValue>>; |
| 384 | using LoadHTType = |
| 385 | ScopedHashTable<Value *, LoadValue, DenseMapInfo<Value *>, |
| 386 | LoadMapAllocator>; |
| 387 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 388 | LoadHTType AvailableLoads; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 389 | |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 390 | // A scoped hash table mapping memory locations (represented as typed |
| 391 | // addresses) to generation numbers at which that memory location became |
| 392 | // (henceforth indefinitely) invariant. |
| 393 | using InvariantMapAllocator = |
| 394 | RecyclingAllocator<BumpPtrAllocator, |
| 395 | ScopedHashTableVal<MemoryLocation, unsigned>>; |
| 396 | using InvariantHTType = |
| 397 | ScopedHashTable<MemoryLocation, unsigned, DenseMapInfo<MemoryLocation>, |
| 398 | InvariantMapAllocator>; |
| 399 | InvariantHTType AvailableInvariants; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 400 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 401 | /// A scoped hash table of the current values of read-only call |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 402 | /// values. |
| 403 | /// |
| 404 | /// It uses the same generation count as loads. |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 405 | using CallHTType = |
| 406 | ScopedHashTable<CallValue, std::pair<Instruction *, unsigned>>; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 407 | CallHTType AvailableCalls; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 408 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 409 | /// This is the current generation of the memory value. |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 410 | unsigned CurrentGeneration = 0; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 411 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 412 | /// Set up the EarlyCSE runner for a particular function. |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 413 | EarlyCSE(const DataLayout &DL, const TargetLibraryInfo &TLI, |
| 414 | const TargetTransformInfo &TTI, DominatorTree &DT, |
| 415 | AssumptionCache &AC, MemorySSA *MSSA) |
| 416 | : TLI(TLI), TTI(TTI), DT(DT), AC(AC), SQ(DL, &TLI, &DT, &AC), MSSA(MSSA), |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 417 | MSSAUpdater(llvm::make_unique<MemorySSAUpdater>(MSSA)) {} |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 418 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 419 | bool run(); |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 420 | |
| 421 | private: |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 422 | // Almost a POD, but needs to call the constructors for the scoped hash |
| 423 | // tables so that a new scope gets pushed on. These are RAII so that the |
| 424 | // scope gets popped when the NodeScope is destroyed. |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 425 | class NodeScope { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 426 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 427 | NodeScope(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads, |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 428 | InvariantHTType &AvailableInvariants, CallHTType &AvailableCalls) |
| 429 | : Scope(AvailableValues), LoadScope(AvailableLoads), |
| 430 | InvariantScope(AvailableInvariants), CallScope(AvailableCalls) {} |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 431 | NodeScope(const NodeScope &) = delete; |
| 432 | NodeScope &operator=(const NodeScope &) = delete; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 433 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 434 | private: |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 435 | ScopedHTType::ScopeTy Scope; |
| 436 | LoadHTType::ScopeTy LoadScope; |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 437 | InvariantHTType::ScopeTy InvariantScope; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 438 | CallHTType::ScopeTy CallScope; |
| 439 | }; |
| 440 | |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 441 | // Contains all the needed information to create a stack for doing a depth |
Nick Lewycky | edd0a70 | 2016-09-07 01:49:41 +0000 | [diff] [blame] | 442 | // first traversal of the tree. This includes scopes for values, loads, and |
Chandler Carruth | 9dea5cd | 2015-01-24 11:44:32 +0000 | [diff] [blame] | 443 | // calls as well as the generation. There is a child iterator so that the |
Sanjoy Das | 5253a08 | 2016-04-27 01:44:31 +0000 | [diff] [blame] | 444 | // children do not need to be store separately. |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 445 | class StackNode { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 446 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 447 | StackNode(ScopedHTType &AvailableValues, LoadHTType &AvailableLoads, |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 448 | InvariantHTType &AvailableInvariants, CallHTType &AvailableCalls, |
| 449 | unsigned cg, DomTreeNode *n, DomTreeNode::iterator child, |
| 450 | DomTreeNode::iterator end) |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 451 | : CurrentGeneration(cg), ChildGeneration(cg), Node(n), ChildIter(child), |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 452 | EndIter(end), |
| 453 | Scopes(AvailableValues, AvailableLoads, AvailableInvariants, |
| 454 | AvailableCalls) |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 455 | {} |
| 456 | StackNode(const StackNode &) = delete; |
| 457 | StackNode &operator=(const StackNode &) = delete; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 458 | |
| 459 | // Accessors. |
| 460 | unsigned currentGeneration() { return CurrentGeneration; } |
| 461 | unsigned childGeneration() { return ChildGeneration; } |
| 462 | void childGeneration(unsigned generation) { ChildGeneration = generation; } |
| 463 | DomTreeNode *node() { return Node; } |
| 464 | DomTreeNode::iterator childIter() { return ChildIter; } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 465 | |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 466 | DomTreeNode *nextChild() { |
| 467 | DomTreeNode *child = *ChildIter; |
| 468 | ++ChildIter; |
| 469 | return child; |
| 470 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 471 | |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 472 | DomTreeNode::iterator end() { return EndIter; } |
| 473 | bool isProcessed() { return Processed; } |
| 474 | void process() { Processed = true; } |
| 475 | |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 476 | private: |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 477 | unsigned CurrentGeneration; |
| 478 | unsigned ChildGeneration; |
| 479 | DomTreeNode *Node; |
| 480 | DomTreeNode::iterator ChildIter; |
| 481 | DomTreeNode::iterator EndIter; |
| 482 | NodeScope Scopes; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 483 | bool Processed = false; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 486 | /// Wrapper class to handle memory instructions, including loads, |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 487 | /// stores and intrinsic loads and stores defined by the target. |
| 488 | class ParseMemoryInst { |
| 489 | public: |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 490 | ParseMemoryInst(Instruction *Inst, const TargetTransformInfo &TTI) |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 491 | : Inst(Inst) { |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 492 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) |
Matt Arsenault | 18bb24a | 2017-03-24 18:56:43 +0000 | [diff] [blame] | 493 | if (TTI.getTgtMemIntrinsic(II, Info)) |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 494 | IsTargetMemInst = true; |
| 495 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 496 | |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 497 | bool isLoad() const { |
| 498 | if (IsTargetMemInst) return Info.ReadMem; |
| 499 | return isa<LoadInst>(Inst); |
| 500 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 501 | |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 502 | bool isStore() const { |
| 503 | if (IsTargetMemInst) return Info.WriteMem; |
| 504 | return isa<StoreInst>(Inst); |
| 505 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 506 | |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 507 | bool isAtomic() const { |
Matt Arsenault | 18bb24a | 2017-03-24 18:56:43 +0000 | [diff] [blame] | 508 | if (IsTargetMemInst) |
| 509 | return Info.Ordering != AtomicOrdering::NotAtomic; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 510 | return Inst->isAtomic(); |
| 511 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 512 | |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 513 | bool isUnordered() const { |
Matt Arsenault | 18bb24a | 2017-03-24 18:56:43 +0000 | [diff] [blame] | 514 | if (IsTargetMemInst) |
| 515 | return Info.isUnordered(); |
| 516 | |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 517 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 518 | return LI->isUnordered(); |
| 519 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 520 | return SI->isUnordered(); |
| 521 | } |
| 522 | // Conservative answer |
| 523 | return !Inst->isAtomic(); |
| 524 | } |
| 525 | |
| 526 | bool isVolatile() const { |
Matt Arsenault | 18bb24a | 2017-03-24 18:56:43 +0000 | [diff] [blame] | 527 | if (IsTargetMemInst) |
| 528 | return Info.IsVolatile; |
| 529 | |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 530 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 531 | return LI->isVolatile(); |
| 532 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 533 | return SI->isVolatile(); |
| 534 | } |
| 535 | // Conservative answer |
| 536 | return true; |
| 537 | } |
| 538 | |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 539 | bool isInvariantLoad() const { |
| 540 | if (auto *LI = dyn_cast<LoadInst>(Inst)) |
Sanjoy Das | 1ab2fad | 2016-06-16 21:00:57 +0000 | [diff] [blame] | 541 | return LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr; |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 542 | return false; |
| 543 | } |
Junmo Park | 80440eb | 2016-02-18 10:09:20 +0000 | [diff] [blame] | 544 | |
Arnaud A. de Grandmaison | 6fd488b | 2015-10-06 13:35:30 +0000 | [diff] [blame] | 545 | bool isMatchingMemLoc(const ParseMemoryInst &Inst) const { |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 546 | return (getPointerOperand() == Inst.getPointerOperand() && |
| 547 | getMatchingId() == Inst.getMatchingId()); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 548 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 549 | |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 550 | bool isValid() const { return getPointerOperand() != nullptr; } |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 551 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 552 | // For regular (non-intrinsic) loads/stores, this is set to -1. For |
| 553 | // intrinsic loads/stores, the id is retrieved from the corresponding |
| 554 | // field in the MemIntrinsicInfo structure. That field contains |
| 555 | // non-negative values only. |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 556 | int getMatchingId() const { |
| 557 | if (IsTargetMemInst) return Info.MatchingId; |
| 558 | return -1; |
| 559 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 560 | |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 561 | Value *getPointerOperand() const { |
| 562 | if (IsTargetMemInst) return Info.PtrVal; |
Renato Golin | 038ede2 | 2018-03-09 21:05:58 +0000 | [diff] [blame] | 563 | return getLoadStorePointerOperand(Inst); |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 564 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 565 | |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 566 | bool mayReadFromMemory() const { |
| 567 | if (IsTargetMemInst) return Info.ReadMem; |
| 568 | return Inst->mayReadFromMemory(); |
| 569 | } |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 570 | |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 571 | bool mayWriteToMemory() const { |
| 572 | if (IsTargetMemInst) return Info.WriteMem; |
| 573 | return Inst->mayWriteToMemory(); |
| 574 | } |
| 575 | |
| 576 | private: |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 577 | bool IsTargetMemInst = false; |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 578 | MemIntrinsicInfo Info; |
| 579 | Instruction *Inst; |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 580 | }; |
| 581 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 582 | bool processNode(DomTreeNode *Node); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 583 | |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 584 | bool handleBranchCondition(Instruction *CondInst, const BranchInst *BI, |
| 585 | const BasicBlock *BB, const BasicBlock *Pred); |
| 586 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 587 | Value *getOrCreateResult(Value *Inst, Type *ExpectedType) const { |
Sanjay Patel | 1c9867d | 2017-01-03 00:16:24 +0000 | [diff] [blame] | 588 | if (auto *LI = dyn_cast<LoadInst>(Inst)) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 589 | return LI; |
Sanjay Patel | 1c9867d | 2017-01-03 00:16:24 +0000 | [diff] [blame] | 590 | if (auto *SI = dyn_cast<StoreInst>(Inst)) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 591 | return SI->getValueOperand(); |
| 592 | assert(isa<IntrinsicInst>(Inst) && "Instruction not supported"); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 593 | return TTI.getOrCreateResultFromMemIntrinsic(cast<IntrinsicInst>(Inst), |
| 594 | ExpectedType); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 595 | } |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 596 | |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 597 | /// Return true if the instruction is known to only operate on memory |
| 598 | /// provably invariant in the given "generation". |
| 599 | bool isOperatingOnInvariantMemAt(Instruction *I, unsigned GenAt); |
| 600 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 601 | bool isSameMemGeneration(unsigned EarlierGeneration, unsigned LaterGeneration, |
| 602 | Instruction *EarlierInst, Instruction *LaterInst); |
| 603 | |
| 604 | void removeMSSA(Instruction *Inst) { |
| 605 | if (!MSSA) |
| 606 | return; |
Geoff Berry | 91e9a5c | 2016-10-25 16:18:47 +0000 | [diff] [blame] | 607 | // Removing a store here can leave MemorySSA in an unoptimized state by |
| 608 | // creating MemoryPhis that have identical arguments and by creating |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 609 | // MemoryUses whose defining access is not an actual clobber. We handle the |
Geoff Berry | 91e9a5c | 2016-10-25 16:18:47 +0000 | [diff] [blame] | 610 | // phi case eagerly here. The non-optimized MemoryUse case is lazily |
| 611 | // updated by MemorySSA getClobberingMemoryAccess. |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 612 | if (MemoryAccess *MA = MSSA->getMemoryAccess(Inst)) { |
| 613 | // Optimize MemoryPhi nodes that may become redundant by having all the |
| 614 | // same input values once MA is removed. |
Davide Italiano | 0dc4778 | 2017-06-14 19:29:53 +0000 | [diff] [blame] | 615 | SmallSetVector<MemoryPhi *, 4> PhisToCheck; |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 616 | SmallVector<MemoryAccess *, 8> WorkQueue; |
| 617 | WorkQueue.push_back(MA); |
| 618 | // Process MemoryPhi nodes in FIFO order using a ever-growing vector since |
| 619 | // we shouldn't be processing that many phis and this will avoid an |
| 620 | // allocation in almost all cases. |
| 621 | for (unsigned I = 0; I < WorkQueue.size(); ++I) { |
| 622 | MemoryAccess *WI = WorkQueue[I]; |
| 623 | |
| 624 | for (auto *U : WI->users()) |
| 625 | if (MemoryPhi *MP = dyn_cast<MemoryPhi>(U)) |
Davide Italiano | 0dc4778 | 2017-06-14 19:29:53 +0000 | [diff] [blame] | 626 | PhisToCheck.insert(MP); |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 627 | |
Daniel Berlin | 17e8d0e | 2017-02-22 22:19:55 +0000 | [diff] [blame] | 628 | MSSAUpdater->removeMemoryAccess(WI); |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 629 | |
| 630 | for (MemoryPhi *MP : PhisToCheck) { |
| 631 | MemoryAccess *FirstIn = MP->getIncomingValue(0); |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 632 | if (llvm::all_of(MP->incoming_values(), |
| 633 | [=](Use &In) { return In == FirstIn; })) |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 634 | WorkQueue.push_back(MP); |
| 635 | } |
| 636 | PhisToCheck.clear(); |
| 637 | } |
| 638 | } |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 639 | } |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 640 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 641 | |
| 642 | } // end anonymous namespace |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 643 | |
Geoff Berry | 6815468 | 2016-10-24 15:54:00 +0000 | [diff] [blame] | 644 | /// Determine if the memory referenced by LaterInst is from the same heap |
| 645 | /// version as EarlierInst. |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 646 | /// This is currently called in two scenarios: |
| 647 | /// |
| 648 | /// load p |
| 649 | /// ... |
| 650 | /// load p |
| 651 | /// |
| 652 | /// and |
| 653 | /// |
| 654 | /// x = load p |
| 655 | /// ... |
| 656 | /// store x, p |
| 657 | /// |
| 658 | /// in both cases we want to verify that there are no possible writes to the |
| 659 | /// memory referenced by p between the earlier and later instruction. |
| 660 | bool EarlyCSE::isSameMemGeneration(unsigned EarlierGeneration, |
| 661 | unsigned LaterGeneration, |
| 662 | Instruction *EarlierInst, |
| 663 | Instruction *LaterInst) { |
| 664 | // Check the simple memory generation tracking first. |
| 665 | if (EarlierGeneration == LaterGeneration) |
| 666 | return true; |
| 667 | |
| 668 | if (!MSSA) |
| 669 | return false; |
| 670 | |
Geoff Berry | f7d5daa | 2017-07-14 20:13:21 +0000 | [diff] [blame] | 671 | // If MemorySSA has determined that one of EarlierInst or LaterInst does not |
| 672 | // read/write memory, then we can safely return true here. |
| 673 | // FIXME: We could be more aggressive when checking doesNotAccessMemory(), |
| 674 | // onlyReadsMemory(), mayReadFromMemory(), and mayWriteToMemory() in this pass |
| 675 | // by also checking the MemorySSA MemoryAccess on the instruction. Initial |
| 676 | // experiments suggest this isn't worthwhile, at least for C/C++ code compiled |
| 677 | // with the default optimization pipeline. |
| 678 | auto *EarlierMA = MSSA->getMemoryAccess(EarlierInst); |
| 679 | if (!EarlierMA) |
| 680 | return true; |
| 681 | auto *LaterMA = MSSA->getMemoryAccess(LaterInst); |
| 682 | if (!LaterMA) |
| 683 | return true; |
| 684 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 685 | // Since we know LaterDef dominates LaterInst and EarlierInst dominates |
| 686 | // LaterInst, if LaterDef dominates EarlierInst then it can't occur between |
| 687 | // EarlierInst and LaterInst and neither can any other write that potentially |
| 688 | // clobbers LaterInst. |
Geoff Berry | 91e9a5c | 2016-10-25 16:18:47 +0000 | [diff] [blame] | 689 | MemoryAccess *LaterDef = |
| 690 | MSSA->getWalker()->getClobberingMemoryAccess(LaterInst); |
Geoff Berry | f7d5daa | 2017-07-14 20:13:21 +0000 | [diff] [blame] | 691 | return MSSA->dominates(LaterDef, EarlierMA); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 694 | bool EarlyCSE::isOperatingOnInvariantMemAt(Instruction *I, unsigned GenAt) { |
| 695 | // A location loaded from with an invariant_load is assumed to *never* change |
| 696 | // within the visible scope of the compilation. |
| 697 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 698 | if (LI->getMetadata(LLVMContext::MD_invariant_load)) |
| 699 | return true; |
| 700 | |
| 701 | auto MemLocOpt = MemoryLocation::getOrNone(I); |
| 702 | if (!MemLocOpt) |
| 703 | // "target" intrinsic forms of loads aren't currently known to |
| 704 | // MemoryLocation::get. TODO |
| 705 | return false; |
| 706 | MemoryLocation MemLoc = *MemLocOpt; |
| 707 | if (!AvailableInvariants.count(MemLoc)) |
| 708 | return false; |
| 709 | |
| 710 | // Is the generation at which this became invariant older than the |
| 711 | // current one? |
| 712 | return AvailableInvariants.lookup(MemLoc) <= GenAt; |
| 713 | } |
| 714 | |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 715 | bool EarlyCSE::handleBranchCondition(Instruction *CondInst, |
| 716 | const BranchInst *BI, const BasicBlock *BB, |
| 717 | const BasicBlock *Pred) { |
| 718 | assert(BI->isConditional() && "Should be a conditional branch!"); |
| 719 | assert(BI->getCondition() == CondInst && "Wrong condition?"); |
| 720 | assert(BI->getSuccessor(0) == BB || BI->getSuccessor(1) == BB); |
| 721 | auto *TorF = (BI->getSuccessor(0) == BB) |
| 722 | ? ConstantInt::getTrue(BB->getContext()) |
| 723 | : ConstantInt::getFalse(BB->getContext()); |
Simon Pilgrim | dee9c67 | 2018-06-14 14:22:03 +0000 | [diff] [blame] | 724 | auto MatchBinOp = [](Instruction *I, unsigned Opcode) { |
Max Kazantsev | ff6d1c9 | 2018-06-14 13:02:13 +0000 | [diff] [blame] | 725 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(I)) |
Simon Pilgrim | dee9c67 | 2018-06-14 14:22:03 +0000 | [diff] [blame] | 726 | return BOp->getOpcode() == Opcode; |
Max Kazantsev | ff6d1c9 | 2018-06-14 13:02:13 +0000 | [diff] [blame] | 727 | return false; |
| 728 | }; |
| 729 | // If the condition is AND operation, we can propagate its operands into the |
| 730 | // true branch. If it is OR operation, we can propagate them into the false |
| 731 | // branch. |
Simon Pilgrim | dee9c67 | 2018-06-14 14:22:03 +0000 | [diff] [blame] | 732 | unsigned PropagateOpcode = |
| 733 | (BI->getSuccessor(0) == BB) ? Instruction::And : Instruction::Or; |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 734 | |
Max Kazantsev | ff6d1c9 | 2018-06-14 13:02:13 +0000 | [diff] [blame] | 735 | bool MadeChanges = false; |
| 736 | SmallVector<Instruction *, 4> WorkList; |
| 737 | SmallPtrSet<Instruction *, 4> Visited; |
| 738 | WorkList.push_back(CondInst); |
| 739 | while (!WorkList.empty()) { |
| 740 | Instruction *Curr = WorkList.pop_back_val(); |
| 741 | |
| 742 | AvailableValues.insert(Curr, TorF); |
| 743 | LLVM_DEBUG(dbgs() << "EarlyCSE CVP: Add conditional value for '" |
| 744 | << Curr->getName() << "' as " << *TorF << " in " |
| 745 | << BB->getName() << "\n"); |
| 746 | if (!DebugCounter::shouldExecute(CSECounter)) { |
| 747 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
| 748 | } else { |
| 749 | // Replace all dominated uses with the known value. |
| 750 | if (unsigned Count = replaceDominatedUsesWith(Curr, TorF, DT, |
| 751 | BasicBlockEdge(Pred, BB))) { |
| 752 | NumCSECVP += Count; |
| 753 | MadeChanges = true; |
| 754 | } |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 755 | } |
Max Kazantsev | ff6d1c9 | 2018-06-14 13:02:13 +0000 | [diff] [blame] | 756 | |
Simon Pilgrim | dee9c67 | 2018-06-14 14:22:03 +0000 | [diff] [blame] | 757 | if (MatchBinOp(Curr, PropagateOpcode)) |
Max Kazantsev | ff6d1c9 | 2018-06-14 13:02:13 +0000 | [diff] [blame] | 758 | for (auto &Op : cast<BinaryOperator>(Curr)->operands()) |
| 759 | if (Instruction *OPI = dyn_cast<Instruction>(Op)) |
| 760 | if (SimpleValue::canHandle(OPI) && Visited.insert(OPI).second) |
| 761 | WorkList.push_back(OPI); |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 762 | } |
Max Kazantsev | ff6d1c9 | 2018-06-14 13:02:13 +0000 | [diff] [blame] | 763 | |
| 764 | return MadeChanges; |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 767 | bool EarlyCSE::processNode(DomTreeNode *Node) { |
Chad Rosier | 1a4bc11 | 2016-04-22 18:47:21 +0000 | [diff] [blame] | 768 | bool Changed = false; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 769 | BasicBlock *BB = Node->getBlock(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 770 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 771 | // If this block has a single predecessor, then the predecessor is the parent |
| 772 | // of the domtree node and all of the live out memory values are still current |
| 773 | // in this block. If this block has multiple predecessors, then they could |
| 774 | // have invalidated the live-out memory values of our parent value. For now, |
| 775 | // just be conservative and invalidate memory if this block has multiple |
| 776 | // predecessors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 777 | if (!BB->getSinglePredecessor()) |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 778 | ++CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 779 | |
Philip Reames | 7c78ef7 | 2015-05-22 23:53:24 +0000 | [diff] [blame] | 780 | // If this node has a single predecessor which ends in a conditional branch, |
| 781 | // 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] | 782 | // 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] | 783 | // which reaches this block where the condition might hold a different |
| 784 | // value. Since we're adding this to the scoped hash table (like any other |
| 785 | // def), it will have been popped if we encounter a future merge block. |
Sanjay Patel | f1e1fba | 2017-03-15 20:25:05 +0000 | [diff] [blame] | 786 | if (BasicBlock *Pred = BB->getSinglePredecessor()) { |
| 787 | auto *BI = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 788 | if (BI && BI->isConditional()) { |
| 789 | auto *CondInst = dyn_cast<Instruction>(BI->getCondition()); |
Max Kazantsev | 0bad5be | 2018-05-31 08:08:34 +0000 | [diff] [blame] | 790 | if (CondInst && SimpleValue::canHandle(CondInst)) |
| 791 | Changed |= handleBranchCondition(CondInst, BI, BB, Pred); |
Sanjay Patel | f1e1fba | 2017-03-15 20:25:05 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
Philip Reames | 7c78ef7 | 2015-05-22 23:53:24 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 795 | /// LastStore - Keep track of the last non-volatile store that we saw... for |
| 796 | /// as long as there in no instruction that reads memory. If we see a store |
| 797 | /// to the same location, we delete the dead store. This zaps trivial dead |
| 798 | /// stores which can occur in bitfield code among other things. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 799 | Instruction *LastStore = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 800 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 801 | // See if any instructions in the block can be eliminated. If so, do it. If |
| 802 | // not, add them to AvailableValues. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 803 | 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] | 804 | Instruction *Inst = &*I++; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 806 | // Dead instructions should just be removed. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 807 | if (isInstructionTriviallyDead(Inst, &TLI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 808 | LLVM_DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 809 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 810 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 811 | continue; |
| 812 | } |
Petar Jovanovic | 1d26c7e | 2018-01-09 15:08:37 +0000 | [diff] [blame] | 813 | salvageDebugInfo(*Inst); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 814 | removeMSSA(Inst); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 815 | Inst->eraseFromParent(); |
| 816 | Changed = true; |
Chris Lattner | 8fac5db | 2011-01-02 23:19:45 +0000 | [diff] [blame] | 817 | ++NumSimplify; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 818 | continue; |
| 819 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 820 | |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 821 | // Skip assume intrinsics, they don't really have side effects (although |
| 822 | // they're marked as such to ensure preservation of control dependencies), |
Max Kazantsev | 531db9a | 2017-04-28 06:25:39 +0000 | [diff] [blame] | 823 | // and this pass will not bother with its removal. However, we should mark |
| 824 | // its condition as true for all dominated blocks. |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 825 | if (match(Inst, m_Intrinsic<Intrinsic::assume>())) { |
Max Kazantsev | 531db9a | 2017-04-28 06:25:39 +0000 | [diff] [blame] | 826 | auto *CondI = |
| 827 | dyn_cast<Instruction>(cast<CallInst>(Inst)->getArgOperand(0)); |
| 828 | if (CondI && SimpleValue::canHandle(CondI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 829 | LLVM_DEBUG(dbgs() << "EarlyCSE considering assumption: " << *Inst |
| 830 | << '\n'); |
Max Kazantsev | 531db9a | 2017-04-28 06:25:39 +0000 | [diff] [blame] | 831 | AvailableValues.insert(CondI, ConstantInt::getTrue(BB->getContext())); |
| 832 | } else |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 833 | LLVM_DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n'); |
Hal Finkel | 1e16fa3 | 2014-11-03 20:21:32 +0000 | [diff] [blame] | 834 | continue; |
| 835 | } |
| 836 | |
Dan Gohman | 2c74fe9 | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 837 | // Skip sideeffect intrinsics, for the same reason as assume intrinsics. |
| 838 | if (match(Inst, m_Intrinsic<Intrinsic::sideeffect>())) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 839 | LLVM_DEBUG(dbgs() << "EarlyCSE skipping sideeffect: " << *Inst << '\n'); |
Dan Gohman | 2c74fe9 | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 840 | continue; |
| 841 | } |
| 842 | |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 843 | // We can skip all invariant.start intrinsics since they only read memory, |
| 844 | // and we can forward values across it. For invariant starts without |
| 845 | // invariant ends, we can use the fact that the invariantness never ends to |
| 846 | // start a scope in the current generaton which is true for all future |
| 847 | // generations. Also, we dont need to consume the last store since the |
| 848 | // semantics of invariant.start allow us to perform DSE of the last |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 849 | // store, if there was a store following invariant.start. Consider: |
Anna Thomas | b2d12b8 | 2016-08-09 20:00:47 +0000 | [diff] [blame] | 850 | // |
| 851 | // store 30, i8* p |
| 852 | // invariant.start(p) |
| 853 | // store 40, i8* p |
| 854 | // We can DSE the store to 30, since the store 40 to invariant location p |
| 855 | // causes undefined behaviour. |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 856 | if (match(Inst, m_Intrinsic<Intrinsic::invariant_start>())) { |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 857 | // If there are any uses, the scope might end. |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 858 | if (!Inst->use_empty()) |
| 859 | continue; |
| 860 | auto *CI = cast<CallInst>(Inst); |
| 861 | MemoryLocation MemLoc = MemoryLocation::getForArgument(CI, 1, TLI); |
Philip Reames | 422024a | 2018-03-15 18:12:27 +0000 | [diff] [blame] | 862 | // Don't start a scope if we already have a better one pushed |
| 863 | if (!AvailableInvariants.count(MemLoc)) |
| 864 | AvailableInvariants.insert(MemLoc, CurrentGeneration); |
Anna Thomas | b2d12b8 | 2016-08-09 20:00:47 +0000 | [diff] [blame] | 865 | continue; |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 866 | } |
Anna Thomas | b2d12b8 | 2016-08-09 20:00:47 +0000 | [diff] [blame] | 867 | |
Max Kazantsev | 3c284bd | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 868 | if (isGuard(Inst)) { |
Sanjoy Das | 107aefc | 2016-04-29 22:23:16 +0000 | [diff] [blame] | 869 | if (auto *CondI = |
| 870 | dyn_cast<Instruction>(cast<CallInst>(Inst)->getArgOperand(0))) { |
Max Kazantsev | 0589d9f | 2017-04-28 06:05:48 +0000 | [diff] [blame] | 871 | if (SimpleValue::canHandle(CondI)) { |
| 872 | // Do we already know the actual value of this condition? |
| 873 | if (auto *KnownCond = AvailableValues.lookup(CondI)) { |
| 874 | // Is the condition known to be true? |
| 875 | if (isa<ConstantInt>(KnownCond) && |
Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 876 | cast<ConstantInt>(KnownCond)->isOne()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 877 | LLVM_DEBUG(dbgs() |
| 878 | << "EarlyCSE removing guard: " << *Inst << '\n'); |
Max Kazantsev | 0589d9f | 2017-04-28 06:05:48 +0000 | [diff] [blame] | 879 | removeMSSA(Inst); |
| 880 | Inst->eraseFromParent(); |
| 881 | Changed = true; |
| 882 | continue; |
| 883 | } else |
| 884 | // Use the known value if it wasn't true. |
| 885 | cast<CallInst>(Inst)->setArgOperand(0, KnownCond); |
| 886 | } |
| 887 | // The condition we're on guarding here is true for all dominated |
| 888 | // locations. |
Sanjoy Das | ee81b23 | 2016-04-29 21:52:58 +0000 | [diff] [blame] | 889 | AvailableValues.insert(CondI, ConstantInt::getTrue(BB->getContext())); |
Max Kazantsev | 0589d9f | 2017-04-28 06:05:48 +0000 | [diff] [blame] | 890 | } |
Sanjoy Das | ee81b23 | 2016-04-29 21:52:58 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // Guard intrinsics read all memory, but don't write any memory. |
| 894 | // Accordingly, don't update the generation but consume the last store (to |
| 895 | // avoid an incorrect DSE). |
| 896 | LastStore = nullptr; |
| 897 | continue; |
| 898 | } |
| 899 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 900 | // If the instruction can be simplified (e.g. X+0 = X) then replace it with |
| 901 | // its simpler value. |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 902 | if (Value *V = SimplifyInstruction(Inst, SQ)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 903 | LLVM_DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V |
| 904 | << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 905 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 906 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 907 | } else { |
| 908 | bool Killed = false; |
| 909 | if (!Inst->use_empty()) { |
| 910 | Inst->replaceAllUsesWith(V); |
| 911 | Changed = true; |
| 912 | } |
| 913 | if (isInstructionTriviallyDead(Inst, &TLI)) { |
| 914 | removeMSSA(Inst); |
| 915 | Inst->eraseFromParent(); |
| 916 | Changed = true; |
| 917 | Killed = true; |
| 918 | } |
| 919 | if (Changed) |
| 920 | ++NumSimplify; |
| 921 | if (Killed) |
| 922 | continue; |
David Majnemer | b8da3a2 | 2016-06-25 00:04:10 +0000 | [diff] [blame] | 923 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 924 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 925 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 926 | // If this is a simple instruction that we can value number, process it. |
| 927 | if (SimpleValue::canHandle(Inst)) { |
| 928 | // See if the instruction has an available value. If so, use it. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 929 | if (Value *V = AvailableValues.lookup(Inst)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 930 | LLVM_DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V |
| 931 | << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 932 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 933 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 934 | continue; |
| 935 | } |
David Majnemer | 9554c13 | 2016-04-22 06:37:45 +0000 | [diff] [blame] | 936 | if (auto *I = dyn_cast<Instruction>(V)) |
| 937 | I->andIRFlags(Inst); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 938 | Inst->replaceAllUsesWith(V); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 939 | removeMSSA(Inst); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 940 | Inst->eraseFromParent(); |
| 941 | Changed = true; |
| 942 | ++NumCSE; |
| 943 | continue; |
| 944 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 945 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 946 | // Otherwise, just remember that this value is available. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 947 | AvailableValues.insert(Inst, Inst); |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 948 | continue; |
| 949 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 950 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 951 | ParseMemoryInst MemInst(Inst, TTI); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 952 | // If this is a non-volatile load, process it. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 953 | if (MemInst.isValid() && MemInst.isLoad()) { |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 954 | // (conservatively) we can't peak past the ordering implied by this |
| 955 | // operation, but we can add this load to our set of available values |
| 956 | if (MemInst.isVolatile() || !MemInst.isUnordered()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 957 | LastStore = nullptr; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 958 | ++CurrentGeneration; |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 959 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 960 | |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 961 | if (MemInst.isInvariantLoad()) { |
| 962 | // If we pass an invariant load, we know that memory location is |
| 963 | // indefinitely constant from the moment of first dereferenceability. |
Philip Reames | 422024a | 2018-03-15 18:12:27 +0000 | [diff] [blame] | 964 | // We conservatively treat the invariant_load as that moment. If we |
| 965 | // pass a invariant load after already establishing a scope, don't |
| 966 | // restart it since we want to preserve the earliest point seen. |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 967 | auto MemLoc = MemoryLocation::get(Inst); |
Philip Reames | 422024a | 2018-03-15 18:12:27 +0000 | [diff] [blame] | 968 | if (!AvailableInvariants.count(MemLoc)) |
| 969 | AvailableInvariants.insert(MemLoc, CurrentGeneration); |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 972 | // If we have an available version of this load, and if it is the right |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 973 | // generation or the load is known to be from an invariant location, |
| 974 | // replace this instruction. |
| 975 | // |
Geoff Berry | 64f5ed1 | 2016-08-31 17:45:31 +0000 | [diff] [blame] | 976 | // If either the dominating load or the current load are invariant, then |
| 977 | // we can assume the current load loads the same value as the dominating |
| 978 | // load. |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 979 | LoadValue InVal = AvailableLoads.lookup(MemInst.getPointerOperand()); |
Sanjoy Das | 07c6521 | 2016-06-16 20:47:57 +0000 | [diff] [blame] | 980 | if (InVal.DefInst != nullptr && |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 981 | InVal.MatchingId == MemInst.getMatchingId() && |
| 982 | // We don't yet handle removing loads with ordering of any kind. |
| 983 | !MemInst.isVolatile() && MemInst.isUnordered() && |
| 984 | // We can't replace an atomic load with one which isn't also atomic. |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 985 | InVal.IsAtomic >= MemInst.isAtomic() && |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 986 | (isOperatingOnInvariantMemAt(Inst, InVal.Generation) || |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 987 | isSameMemGeneration(InVal.Generation, CurrentGeneration, |
| 988 | InVal.DefInst, Inst))) { |
Philip Reames | 32b5518 | 2016-05-06 01:13:58 +0000 | [diff] [blame] | 989 | Value *Op = getOrCreateResult(InVal.DefInst, Inst->getType()); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 990 | if (Op != nullptr) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 991 | LLVM_DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst |
| 992 | << " to: " << *InVal.DefInst << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 993 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 994 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 995 | continue; |
| 996 | } |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 997 | if (!Inst->use_empty()) |
| 998 | Inst->replaceAllUsesWith(Op); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 999 | removeMSSA(Inst); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1000 | Inst->eraseFromParent(); |
| 1001 | Changed = true; |
| 1002 | ++NumCSELoad; |
| 1003 | continue; |
| 1004 | } |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 1005 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 1007 | // Otherwise, remember that we have this instruction. |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 1008 | AvailableLoads.insert( |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 1009 | MemInst.getPointerOperand(), |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 1010 | LoadValue(Inst, CurrentGeneration, MemInst.getMatchingId(), |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 1011 | MemInst.isAtomic())); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1012 | LastStore = nullptr; |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 1013 | continue; |
| 1014 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1015 | |
Sanjoy Das | 6de072a | 2017-01-17 20:15:47 +0000 | [diff] [blame] | 1016 | // If this instruction may read from memory or throw (and potentially read |
| 1017 | // from memory in the exception handler), forget LastStore. Load/store |
| 1018 | // intrinsics will indicate both a read and a write to memory. The target |
| 1019 | // may override this (e.g. so that a store intrinsic does not read from |
| 1020 | // memory, and thus will be treated the same as a regular store for |
| 1021 | // commoning purposes). |
| 1022 | if ((Inst->mayReadFromMemory() || Inst->mayThrow()) && |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1023 | !(MemInst.isValid() && !MemInst.mayReadFromMemory())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1024 | LastStore = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 1026 | // If this is a read-only call, process it. |
| 1027 | if (CallValue::canHandle(Inst)) { |
| 1028 | // If we have an available version of this call, and if it is the right |
| 1029 | // generation, replace this instruction. |
Geoff Berry | 2f64c20 | 2016-05-13 17:54:58 +0000 | [diff] [blame] | 1030 | std::pair<Instruction *, unsigned> InVal = AvailableCalls.lookup(Inst); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1031 | if (InVal.first != nullptr && |
| 1032 | isSameMemGeneration(InVal.second, CurrentGeneration, InVal.first, |
| 1033 | Inst)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1034 | LLVM_DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst |
| 1035 | << " to: " << *InVal.first << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 1036 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1037 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 1038 | continue; |
| 1039 | } |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 1040 | if (!Inst->use_empty()) |
| 1041 | Inst->replaceAllUsesWith(InVal.first); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1042 | removeMSSA(Inst); |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 1043 | Inst->eraseFromParent(); |
| 1044 | Changed = true; |
| 1045 | ++NumCSECall; |
| 1046 | continue; |
| 1047 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 92bb0f9 | 2011-01-03 03:41:27 +0000 | [diff] [blame] | 1049 | // Otherwise, remember that we have this instruction. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1050 | AvailableCalls.insert( |
Geoff Berry | 2f64c20 | 2016-05-13 17:54:58 +0000 | [diff] [blame] | 1051 | Inst, std::pair<Instruction *, unsigned>(Inst, CurrentGeneration)); |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 1052 | continue; |
| 1053 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1054 | |
Philip Reames | dfd890d | 2015-08-27 01:32:33 +0000 | [diff] [blame] | 1055 | // A release fence requires that all stores complete before it, but does |
| 1056 | // not prevent the reordering of following loads 'before' the fence. As a |
| 1057 | // result, we don't need to consider it as writing to memory and don't need |
| 1058 | // to advance the generation. We do need to prevent DSE across the fence, |
| 1059 | // but that's handled above. |
| 1060 | if (FenceInst *FI = dyn_cast<FenceInst>(Inst)) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 1061 | if (FI->getOrdering() == AtomicOrdering::Release) { |
Philip Reames | dfd890d | 2015-08-27 01:32:33 +0000 | [diff] [blame] | 1062 | assert(Inst->mayReadFromMemory() && "relied on to prevent DSE above"); |
| 1063 | continue; |
| 1064 | } |
| 1065 | |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 1066 | // write back DSE - If we write back the same value we just loaded from |
| 1067 | // the same location and haven't passed any intervening writes or ordering |
| 1068 | // operations, we can remove the write. The primary benefit is in allowing |
| 1069 | // the available load table to remain valid and value forward past where |
| 1070 | // the store originally was. |
| 1071 | if (MemInst.isValid() && MemInst.isStore()) { |
| 1072 | LoadValue InVal = AvailableLoads.lookup(MemInst.getPointerOperand()); |
Philip Reames | 32b5518 | 2016-05-06 01:13:58 +0000 | [diff] [blame] | 1073 | if (InVal.DefInst && |
| 1074 | InVal.DefInst == getOrCreateResult(Inst, InVal.DefInst->getType()) && |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 1075 | InVal.MatchingId == MemInst.getMatchingId() && |
| 1076 | // We don't yet handle removing stores with ordering of any kind. |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1077 | !MemInst.isVolatile() && MemInst.isUnordered() && |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 1078 | (isOperatingOnInvariantMemAt(Inst, InVal.Generation) || |
| 1079 | isSameMemGeneration(InVal.Generation, CurrentGeneration, |
| 1080 | InVal.DefInst, Inst))) { |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1081 | // It is okay to have a LastStore to a different pointer here if MemorySSA |
| 1082 | // tells us that the load and store are from the same memory generation. |
| 1083 | // In that case, LastStore should keep its present value since we're |
| 1084 | // removing the current store. |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 1085 | assert((!LastStore || |
| 1086 | ParseMemoryInst(LastStore, TTI).getPointerOperand() == |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1087 | MemInst.getPointerOperand() || |
| 1088 | MSSA) && |
| 1089 | "can't have an intervening store if not using MemorySSA!"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1090 | LLVM_DEBUG(dbgs() << "EarlyCSE DSE (writeback): " << *Inst << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 1091 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1092 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 1093 | continue; |
| 1094 | } |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1095 | removeMSSA(Inst); |
Philip Reames | ae1f265b | 2015-12-16 01:01:30 +0000 | [diff] [blame] | 1096 | Inst->eraseFromParent(); |
| 1097 | Changed = true; |
| 1098 | ++NumDSE; |
| 1099 | // We can avoid incrementing the generation count since we were able |
| 1100 | // to eliminate this store. |
| 1101 | continue; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 1105 | // Okay, this isn't something we can CSE at all. Check to see if it is |
| 1106 | // something that could modify memory. If so, our available memory values |
| 1107 | // cannot be used so bump the generation count. |
Chris Lattner | e0e32a9 | 2011-01-03 03:46:34 +0000 | [diff] [blame] | 1108 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | b9a8efc | 2011-01-03 03:18:43 +0000 | [diff] [blame] | 1109 | ++CurrentGeneration; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1110 | |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1111 | if (MemInst.isValid() && MemInst.isStore()) { |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 1112 | // 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] | 1113 | // location with no intervening loads. Delete the earlier store. |
| 1114 | // At the moment, we don't remove ordered stores, but do remove |
| 1115 | // unordered atomic stores. There's no special requirement (for |
| 1116 | // unordered atomics) about removing atomic stores only in favor of |
| 1117 | // other atomic stores since we we're going to execute the non-atomic |
| 1118 | // one anyway and the atomic one might never have become visible. |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1119 | if (LastStore) { |
| 1120 | ParseMemoryInst LastStoreMemInst(LastStore, TTI); |
Philip Reames | 15145fb | 2015-12-17 18:50:50 +0000 | [diff] [blame] | 1121 | assert(LastStoreMemInst.isUnordered() && |
| 1122 | !LastStoreMemInst.isVolatile() && |
| 1123 | "Violated invariant"); |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1124 | if (LastStoreMemInst.isMatchingMemLoc(MemInst)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1125 | LLVM_DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore |
| 1126 | << " due to: " << *Inst << '\n'); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 1127 | if (!DebugCounter::shouldExecute(CSECounter)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1128 | LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n"); |
Geoff Berry | 5bf4a5e | 2018-04-06 18:47:33 +0000 | [diff] [blame] | 1129 | } else { |
| 1130 | removeMSSA(LastStore); |
| 1131 | LastStore->eraseFromParent(); |
| 1132 | Changed = true; |
| 1133 | ++NumDSE; |
| 1134 | LastStore = nullptr; |
| 1135 | } |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1136 | } |
Philip Reames | 018dbf1 | 2014-11-18 17:46:32 +0000 | [diff] [blame] | 1137 | // fallthrough - we can exploit information about this store |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 1138 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 9e5e9ed | 2011-01-03 04:17:24 +0000 | [diff] [blame] | 1140 | // Okay, we just invalidated anything we knew about loaded values. Try |
| 1141 | // to salvage *something* by remembering that the stored value is a live |
| 1142 | // version of the pointer. It is safe to forward from volatile stores |
| 1143 | // to non-volatile loads, so we don't have to check for volatility of |
| 1144 | // the store. |
Arnaud A. de Grandmaison | a6178a1 | 2015-10-07 07:41:29 +0000 | [diff] [blame] | 1145 | AvailableLoads.insert( |
Philip Reames | 9e5e2d6 | 2015-12-07 22:41:23 +0000 | [diff] [blame] | 1146 | MemInst.getPointerOperand(), |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 1147 | LoadValue(Inst, CurrentGeneration, MemInst.getMatchingId(), |
Philip Reames | ca587fe | 2018-03-15 17:29:32 +0000 | [diff] [blame] | 1148 | MemInst.isAtomic())); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1149 | |
Philip Reames | 15145fb | 2015-12-17 18:50:50 +0000 | [diff] [blame] | 1150 | // Remember that this was the last unordered store we saw for DSE. We |
| 1151 | // don't yet handle DSE on ordered or volatile stores since we don't |
| 1152 | // have a good way to model the ordering requirement for following |
| 1153 | // passes once the store is removed. We could insert a fence, but |
| 1154 | // since fences are slightly stronger than stores in their ordering, |
| 1155 | // it's not clear this is a profitable transform. Another option would |
| 1156 | // be to merge the ordering with that of the post dominating store. |
| 1157 | if (MemInst.isUnordered() && !MemInst.isVolatile()) |
Chad Rosier | f9327d6 | 2015-01-26 22:51:15 +0000 | [diff] [blame] | 1158 | LastStore = Inst; |
Philip Reames | 8fc2cbf | 2015-12-08 21:45:41 +0000 | [diff] [blame] | 1159 | else |
| 1160 | LastStore = nullptr; |
Chris Lattner | e0e32a9 | 2011-01-03 03:46:34 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 1163 | } |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 1165 | return Changed; |
Chris Lattner | 704541b | 2011-01-02 21:47:05 +0000 | [diff] [blame] | 1166 | } |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 1167 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1168 | bool EarlyCSE::run() { |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 1169 | // Note, deque is being used here because there is significant performance |
| 1170 | // gains over vector when the container becomes very large due to the |
| 1171 | // specific access patterns. For more information see the mailing list |
| 1172 | // discussion on this: |
Tanya Lattner | 0d28f80 | 2015-08-05 03:51:17 +0000 | [diff] [blame] | 1173 | // 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] | 1174 | std::deque<StackNode *> nodesToProcess; |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1175 | |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1176 | bool Changed = false; |
| 1177 | |
| 1178 | // Process the root node. |
Chandler Carruth | 7253bba | 2015-01-24 11:33:55 +0000 | [diff] [blame] | 1179 | nodesToProcess.push_back(new StackNode( |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 1180 | AvailableValues, AvailableLoads, AvailableInvariants, AvailableCalls, |
| 1181 | CurrentGeneration, DT.getRootNode(), |
| 1182 | DT.getRootNode()->begin(), DT.getRootNode()->end())); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1183 | |
| 1184 | // Save the current generation. |
| 1185 | unsigned LiveOutGeneration = CurrentGeneration; |
| 1186 | |
| 1187 | // Process the stack. |
| 1188 | while (!nodesToProcess.empty()) { |
| 1189 | // Grab the first item off the stack. Set the current generation, remove |
| 1190 | // the node from the stack, and process it. |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 1191 | StackNode *NodeToProcess = nodesToProcess.back(); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Initialize class members. |
| 1194 | CurrentGeneration = NodeToProcess->currentGeneration(); |
| 1195 | |
| 1196 | // Check if the node needs to be processed. |
| 1197 | if (!NodeToProcess->isProcessed()) { |
| 1198 | // Process the node. |
| 1199 | Changed |= processNode(NodeToProcess->node()); |
| 1200 | NodeToProcess->childGeneration(CurrentGeneration); |
| 1201 | NodeToProcess->process(); |
| 1202 | } else if (NodeToProcess->childIter() != NodeToProcess->end()) { |
| 1203 | // Push the next child onto the stack. |
| 1204 | DomTreeNode *child = NodeToProcess->nextChild(); |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 1205 | nodesToProcess.push_back( |
Philip Reames | 0adbb19 | 2018-03-14 21:35:06 +0000 | [diff] [blame] | 1206 | new StackNode(AvailableValues, AvailableLoads, AvailableInvariants, |
| 1207 | AvailableCalls, NodeToProcess->childGeneration(), |
| 1208 | child, child->begin(), child->end())); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1209 | } else { |
| 1210 | // It has been processed, and there are no more children to process, |
| 1211 | // so delete it and pop it off the stack. |
| 1212 | delete NodeToProcess; |
Michael Gottesman | 2bf0173 | 2013-12-05 18:42:12 +0000 | [diff] [blame] | 1213 | nodesToProcess.pop_back(); |
Lenny Maiorani | 8d670b8 | 2012-01-31 23:14:41 +0000 | [diff] [blame] | 1214 | } |
| 1215 | } // while (!nodes...) |
| 1216 | |
| 1217 | // Reset the current generation. |
| 1218 | CurrentGeneration = LiveOutGeneration; |
| 1219 | |
| 1220 | return Changed; |
Chris Lattner | 18ae543 | 2011-01-02 23:04:14 +0000 | [diff] [blame] | 1221 | } |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1222 | |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 1223 | PreservedAnalyses EarlyCSEPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 1224 | FunctionAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1225 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| 1226 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
| 1227 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1228 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1229 | auto *MSSA = |
| 1230 | UseMemorySSA ? &AM.getResult<MemorySSAAnalysis>(F).getMSSA() : nullptr; |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 1231 | |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 1232 | EarlyCSE CSE(F.getParent()->getDataLayout(), TLI, TTI, DT, AC, MSSA); |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (!CSE.run()) |
| 1235 | return PreservedAnalyses::all(); |
| 1236 | |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 1237 | PreservedAnalyses PA; |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 1238 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | 02861d8 | 2016-06-08 21:31:55 +0000 | [diff] [blame] | 1239 | PA.preserve<GlobalsAA>(); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1240 | if (UseMemorySSA) |
| 1241 | PA.preserve<MemorySSAAnalysis>(); |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 1242 | return PA; |
| 1243 | } |
| 1244 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1245 | namespace { |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 1246 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1247 | /// A simple and fast domtree-based CSE pass. |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1248 | /// |
| 1249 | /// This pass does a simple depth-first walk over the dominator tree, |
| 1250 | /// eliminating trivially redundant instructions and using instsimplify to |
| 1251 | /// canonicalize things as it goes. It is intended to be fast and catch obvious |
| 1252 | /// cases so that instcombine and other passes are more effective. It is |
| 1253 | /// expected that a later pass of GVN will catch the interesting/hard cases. |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1254 | template<bool UseMemorySSA> |
| 1255 | class EarlyCSELegacyCommonPass : public FunctionPass { |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1256 | public: |
| 1257 | static char ID; |
| 1258 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1259 | EarlyCSELegacyCommonPass() : FunctionPass(ID) { |
| 1260 | if (UseMemorySSA) |
| 1261 | initializeEarlyCSEMemSSALegacyPassPass(*PassRegistry::getPassRegistry()); |
| 1262 | else |
| 1263 | initializeEarlyCSELegacyPassPass(*PassRegistry::getPassRegistry()); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 1267 | if (skipFunction(F)) |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1268 | return false; |
| 1269 | |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1270 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 1271 | auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1272 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1273 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1274 | auto *MSSA = |
| 1275 | UseMemorySSA ? &getAnalysis<MemorySSAWrapperPass>().getMSSA() : nullptr; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1276 | |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 1277 | EarlyCSE CSE(F.getParent()->getDataLayout(), TLI, TTI, DT, AC, MSSA); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1278 | |
| 1279 | return CSE.run(); |
| 1280 | } |
| 1281 | |
| 1282 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1283 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1284 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 1285 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1286 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1287 | if (UseMemorySSA) { |
| 1288 | AU.addRequired<MemorySSAWrapperPass>(); |
| 1289 | AU.addPreserved<MemorySSAWrapperPass>(); |
| 1290 | } |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 1291 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1292 | AU.setPreservesCFG(); |
| 1293 | } |
| 1294 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 1295 | |
| 1296 | } // end anonymous namespace |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1297 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1298 | using EarlyCSELegacyPass = EarlyCSELegacyCommonPass</*UseMemorySSA=*/false>; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1299 | |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1300 | template<> |
| 1301 | char EarlyCSELegacyPass::ID = 0; |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1302 | |
| 1303 | INITIALIZE_PASS_BEGIN(EarlyCSELegacyPass, "early-cse", "Early CSE", false, |
| 1304 | false) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1305 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1306 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | d649c0a | 2015-01-27 01:34:14 +0000 | [diff] [blame] | 1307 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1308 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 1309 | INITIALIZE_PASS_END(EarlyCSELegacyPass, "early-cse", "Early CSE", false, false) |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1310 | |
| 1311 | using EarlyCSEMemSSALegacyPass = |
| 1312 | EarlyCSELegacyCommonPass</*UseMemorySSA=*/true>; |
| 1313 | |
| 1314 | template<> |
| 1315 | char EarlyCSEMemSSALegacyPass::ID = 0; |
| 1316 | |
| 1317 | FunctionPass *llvm::createEarlyCSEPass(bool UseMemorySSA) { |
| 1318 | if (UseMemorySSA) |
| 1319 | return new EarlyCSEMemSSALegacyPass(); |
| 1320 | else |
| 1321 | return new EarlyCSELegacyPass(); |
| 1322 | } |
| 1323 | |
| 1324 | INITIALIZE_PASS_BEGIN(EarlyCSEMemSSALegacyPass, "early-cse-memssa", |
| 1325 | "Early CSE w/ MemorySSA", false, false) |
| 1326 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1327 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Geoff Berry | 8d84605 | 2016-08-31 19:24:10 +0000 | [diff] [blame] | 1328 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 1329 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 1330 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
| 1331 | INITIALIZE_PASS_END(EarlyCSEMemSSALegacyPass, "early-cse-memssa", |
| 1332 | "Early CSE w/ MemorySSA", false, false) |