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