Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1 | //===- GVNHoist.cpp - Hoist scalar and load expressions -------------------===// |
| 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 |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass hoists expressions from branches to a common dominator. It uses |
| 10 | // GVN (global value numbering) to discover expressions computing the same |
Aditya Kumar | f24939b | 2016-08-13 11:56:50 +0000 | [diff] [blame] | 11 | // values. The primary goals of code-hoisting are: |
| 12 | // 1. To reduce the code size. |
| 13 | // 2. In some cases reduce critical path (by exposing more ILP). |
| 14 | // |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 15 | // The algorithm factors out the reachability of values such that multiple |
| 16 | // queries to find reachability of values are fast. This is based on finding the |
| 17 | // ANTIC points in the CFG which do not change during hoisting. The ANTIC points |
| 18 | // are basically the dominance-frontiers in the inverse graph. So we introduce a |
| 19 | // data structure (CHI nodes) to keep track of values flowing out of a basic |
| 20 | // block. We only do this for values with multiple occurrences in the function |
| 21 | // as they are the potential hoistable candidates. This approach allows us to |
| 22 | // hoist instructions to a basic block with more than two successors, as well as |
| 23 | // deal with infinite loops in a trivial way. |
| 24 | // |
| 25 | // Limitations: This pass does not hoist fully redundant expressions because |
| 26 | // they are already handled by GVN-PRE. It is advisable to run gvn-hoist before |
| 27 | // and after gvn-pre because gvn-pre creates opportunities for more instructions |
| 28 | // to be hoisted. |
| 29 | // |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 30 | // Hoisting may affect the performance in some cases. To mitigate that, hoisting |
| 31 | // is disabled in the following cases. |
| 32 | // 1. Scalars across calls. |
| 33 | // 2. geps when corresponding load/store cannot be hoisted. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/DenseSet.h" |
| 38 | #include "llvm/ADT/STLExtras.h" |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SmallVector.h" |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/iterator_range.h" |
| 43 | #include "llvm/Analysis/AliasAnalysis.h" |
Nikolai Bozhenov | 9e4a1c3 | 2017-04-18 13:25:49 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/GlobalsModRef.h" |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/IteratedDominanceFrontier.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Daniel Berlin | 554dcd8 | 2017-04-11 20:06:36 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/MemorySSA.h" |
| 48 | #include "llvm/Analysis/MemorySSAUpdater.h" |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/PostDominators.h" |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 51 | #include "llvm/IR/Argument.h" |
| 52 | #include "llvm/IR/BasicBlock.h" |
| 53 | #include "llvm/IR/CFG.h" |
| 54 | #include "llvm/IR/Constants.h" |
| 55 | #include "llvm/IR/Dominators.h" |
| 56 | #include "llvm/IR/Function.h" |
| 57 | #include "llvm/IR/InstrTypes.h" |
| 58 | #include "llvm/IR/Instruction.h" |
| 59 | #include "llvm/IR/Instructions.h" |
Reid Kleckner | 0e8c4bb | 2017-09-07 23:27:44 +0000 | [diff] [blame] | 60 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Intrinsics.h" |
| 62 | #include "llvm/IR/LLVMContext.h" |
| 63 | #include "llvm/IR/PassManager.h" |
| 64 | #include "llvm/IR/Use.h" |
| 65 | #include "llvm/IR/User.h" |
| 66 | #include "llvm/IR/Value.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 67 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 68 | #include "llvm/Pass.h" |
| 69 | #include "llvm/Support/Casting.h" |
| 70 | #include "llvm/Support/CommandLine.h" |
| 71 | #include "llvm/Support/Debug.h" |
| 72 | #include "llvm/Support/raw_ostream.h" |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 73 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 74 | #include "llvm/Transforms/Scalar/GVN.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 75 | #include "llvm/Transforms/Utils/Local.h" |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 76 | #include <algorithm> |
| 77 | #include <cassert> |
| 78 | #include <iterator> |
| 79 | #include <memory> |
| 80 | #include <utility> |
| 81 | #include <vector> |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 82 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 83 | using namespace llvm; |
| 84 | |
| 85 | #define DEBUG_TYPE "gvn-hoist" |
| 86 | |
| 87 | STATISTIC(NumHoisted, "Number of instructions hoisted"); |
| 88 | STATISTIC(NumRemoved, "Number of instructions removed"); |
| 89 | STATISTIC(NumLoadsHoisted, "Number of loads hoisted"); |
| 90 | STATISTIC(NumLoadsRemoved, "Number of loads removed"); |
| 91 | STATISTIC(NumStoresHoisted, "Number of stores hoisted"); |
| 92 | STATISTIC(NumStoresRemoved, "Number of stores removed"); |
| 93 | STATISTIC(NumCallsHoisted, "Number of calls hoisted"); |
| 94 | STATISTIC(NumCallsRemoved, "Number of calls removed"); |
| 95 | |
| 96 | static cl::opt<int> |
| 97 | MaxHoistedThreshold("gvn-max-hoisted", cl::Hidden, cl::init(-1), |
| 98 | cl::desc("Max number of instructions to hoist " |
| 99 | "(default unlimited = -1)")); |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 100 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 101 | static cl::opt<int> MaxNumberOfBBSInPath( |
| 102 | "gvn-hoist-max-bbs", cl::Hidden, cl::init(4), |
| 103 | cl::desc("Max number of basic blocks on the path between " |
| 104 | "hoisting locations (default = 4, unlimited = -1)")); |
| 105 | |
Sebastian Pop | 38422b1 | 2016-07-26 00:15:08 +0000 | [diff] [blame] | 106 | static cl::opt<int> MaxDepthInBB( |
| 107 | "gvn-hoist-max-depth", cl::Hidden, cl::init(100), |
| 108 | cl::desc("Hoist instructions from the beginning of the BB up to the " |
| 109 | "maximum specified depth (default = 100, unlimited = -1)")); |
| 110 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 111 | static cl::opt<int> |
| 112 | MaxChainLength("gvn-hoist-max-chain-length", cl::Hidden, cl::init(10), |
| 113 | cl::desc("Maximum length of dependent chains to hoist " |
| 114 | "(default = 10, unlimited = -1)")); |
Sebastian Pop | 2aadad7 | 2016-08-03 20:54:38 +0000 | [diff] [blame] | 115 | |
Daniel Berlin | dcb004f | 2017-03-02 23:06:46 +0000 | [diff] [blame] | 116 | namespace llvm { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 117 | |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 118 | using BBSideEffectsSet = DenseMap<const BasicBlock *, bool>; |
| 119 | using SmallVecInsn = SmallVector<Instruction *, 4>; |
| 120 | using SmallVecImplInsn = SmallVectorImpl<Instruction *>; |
| 121 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 122 | // Each element of a hoisting list contains the basic block where to hoist and |
| 123 | // a list of instructions to be hoisted. |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 124 | using HoistingPointInfo = std::pair<BasicBlock *, SmallVecInsn>; |
| 125 | |
| 126 | using HoistingPointList = SmallVector<HoistingPointInfo, 4>; |
| 127 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 128 | // A map from a pair of VNs to all the instructions with those VNs. |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 129 | using VNType = std::pair<unsigned, unsigned>; |
| 130 | |
| 131 | using VNtoInsns = DenseMap<VNType, SmallVector<Instruction *, 4>>; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 132 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 133 | // CHI keeps information about values flowing out of a basic block. It is |
| 134 | // similar to PHI but in the inverse graph, and used for outgoing values on each |
| 135 | // edge. For conciseness, it is computed only for instructions with multiple |
| 136 | // occurrences in the CFG because they are the only hoistable candidates. |
| 137 | // A (CHI[{V, B, I1}, {V, C, I2}] |
| 138 | // / \ |
| 139 | // / \ |
| 140 | // B(I1) C (I2) |
| 141 | // The Value number for both I1 and I2 is V, the CHI node will save the |
| 142 | // instruction as well as the edge where the value is flowing to. |
| 143 | struct CHIArg { |
| 144 | VNType VN; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 145 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 146 | // Edge destination (shows the direction of flow), may not be where the I is. |
| 147 | BasicBlock *Dest; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 148 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 149 | // The instruction (VN) which uses the values flowing out of CHI. |
| 150 | Instruction *I; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 151 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 152 | bool operator==(const CHIArg &A) { return VN == A.VN; } |
| 153 | bool operator!=(const CHIArg &A) { return !(*this == A); } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 156 | using CHIIt = SmallVectorImpl<CHIArg>::iterator; |
| 157 | using CHIArgs = iterator_range<CHIIt>; |
| 158 | using OutValuesType = DenseMap<BasicBlock *, SmallVector<CHIArg, 2>>; |
| 159 | using InValuesType = |
| 160 | DenseMap<BasicBlock *, SmallVector<std::pair<VNType, Instruction *>, 2>>; |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 161 | |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 162 | // An invalid value number Used when inserting a single value number into |
| 163 | // VNtoInsns. |
Reid Kleckner | 3498ad1 | 2016-07-18 18:53:50 +0000 | [diff] [blame] | 164 | enum : unsigned { InvalidVN = ~2U }; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 165 | |
| 166 | // Records all scalar instructions candidate for code hoisting. |
| 167 | class InsnInfo { |
| 168 | VNtoInsns VNtoScalars; |
| 169 | |
| 170 | public: |
| 171 | // Inserts I and its value number in VNtoScalars. |
| 172 | void insert(Instruction *I, GVN::ValueTable &VN) { |
| 173 | // Scalar instruction. |
| 174 | unsigned V = VN.lookupOrAdd(I); |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 175 | VNtoScalars[{V, InvalidVN}].push_back(I); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | const VNtoInsns &getVNTable() const { return VNtoScalars; } |
| 179 | }; |
| 180 | |
| 181 | // Records all load instructions candidate for code hoisting. |
| 182 | class LoadInfo { |
| 183 | VNtoInsns VNtoLoads; |
| 184 | |
| 185 | public: |
| 186 | // Insert Load and the value number of its memory address in VNtoLoads. |
| 187 | void insert(LoadInst *Load, GVN::ValueTable &VN) { |
| 188 | if (Load->isSimple()) { |
| 189 | unsigned V = VN.lookupOrAdd(Load->getPointerOperand()); |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 190 | VNtoLoads[{V, InvalidVN}].push_back(Load); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | const VNtoInsns &getVNTable() const { return VNtoLoads; } |
| 195 | }; |
| 196 | |
| 197 | // Records all store instructions candidate for code hoisting. |
| 198 | class StoreInfo { |
| 199 | VNtoInsns VNtoStores; |
| 200 | |
| 201 | public: |
| 202 | // Insert the Store and a hash number of the store address and the stored |
| 203 | // value in VNtoStores. |
| 204 | void insert(StoreInst *Store, GVN::ValueTable &VN) { |
| 205 | if (!Store->isSimple()) |
| 206 | return; |
| 207 | // Hash the store address and the stored value. |
| 208 | Value *Ptr = Store->getPointerOperand(); |
| 209 | Value *Val = Store->getValueOperand(); |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 210 | VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | const VNtoInsns &getVNTable() const { return VNtoStores; } |
| 214 | }; |
| 215 | |
| 216 | // Records all call instructions candidate for code hoisting. |
| 217 | class CallInfo { |
| 218 | VNtoInsns VNtoCallsScalars; |
| 219 | VNtoInsns VNtoCallsLoads; |
| 220 | VNtoInsns VNtoCallsStores; |
| 221 | |
| 222 | public: |
| 223 | // Insert Call and its value numbering in one of the VNtoCalls* containers. |
| 224 | void insert(CallInst *Call, GVN::ValueTable &VN) { |
| 225 | // A call that doesNotAccessMemory is handled as a Scalar, |
| 226 | // onlyReadsMemory will be handled as a Load instruction, |
| 227 | // all other calls will be handled as stores. |
| 228 | unsigned V = VN.lookupOrAdd(Call); |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 229 | auto Entry = std::make_pair(V, InvalidVN); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 230 | |
| 231 | if (Call->doesNotAccessMemory()) |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 232 | VNtoCallsScalars[Entry].push_back(Call); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 233 | else if (Call->onlyReadsMemory()) |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 234 | VNtoCallsLoads[Entry].push_back(Call); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 235 | else |
David Majnemer | 04c7c22 | 2016-07-18 06:11:37 +0000 | [diff] [blame] | 236 | VNtoCallsStores[Entry].push_back(Call); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 240 | const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 241 | const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; } |
| 242 | }; |
| 243 | |
David Majnemer | 68623a0 | 2016-07-25 02:21:25 +0000 | [diff] [blame] | 244 | static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) { |
| 245 | static const unsigned KnownIDs[] = { |
| 246 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 247 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
| 248 | LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load, |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 249 | LLVMContext::MD_invariant_group, LLVMContext::MD_access_group}; |
Florian Hahn | 406f1ff | 2018-08-24 11:40:04 +0000 | [diff] [blame] | 250 | combineMetadata(ReplInst, I, KnownIDs, true); |
David Majnemer | 68623a0 | 2016-07-25 02:21:25 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 253 | // This pass hoists common computations across branches sharing common |
| 254 | // dominator. The primary goal is to reduce the code size, and in some |
| 255 | // cases reduce critical path (by exposing more ILP). |
| 256 | class GVNHoist { |
| 257 | public: |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 258 | GVNHoist(DominatorTree *DT, PostDominatorTree *PDT, AliasAnalysis *AA, |
| 259 | MemoryDependenceResults *MD, MemorySSA *MSSA) |
| 260 | : DT(DT), PDT(PDT), AA(AA), MD(MD), MSSA(MSSA), |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 261 | MSSAUpdater(std::make_unique<MemorySSAUpdater>(MSSA)) {} |
Aditya Kumar | 07cb304 | 2016-11-29 14:34:01 +0000 | [diff] [blame] | 262 | |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 263 | bool run(Function &F) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 264 | NumFuncArgs = F.arg_size(); |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 265 | VN.setDomTree(DT); |
| 266 | VN.setAliasAnalysis(AA); |
| 267 | VN.setMemDep(MD); |
| 268 | bool Res = false; |
Sebastian Pop | 4ba7c88 | 2016-08-03 20:54:36 +0000 | [diff] [blame] | 269 | // Perform DFS Numbering of instructions. |
| 270 | unsigned BBI = 0; |
| 271 | for (const BasicBlock *BB : depth_first(&F.getEntryBlock())) { |
| 272 | DFSNumber[BB] = ++BBI; |
| 273 | unsigned I = 0; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 274 | for (auto &Inst : *BB) |
Sebastian Pop | 4ba7c88 | 2016-08-03 20:54:36 +0000 | [diff] [blame] | 275 | DFSNumber[&Inst] = ++I; |
| 276 | } |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 277 | |
Sebastian Pop | 2aadad7 | 2016-08-03 20:54:38 +0000 | [diff] [blame] | 278 | int ChainLength = 0; |
| 279 | |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 280 | // FIXME: use lazy evaluation of VN to avoid the fix-point computation. |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 281 | while (true) { |
Sebastian Pop | 2aadad7 | 2016-08-03 20:54:38 +0000 | [diff] [blame] | 282 | if (MaxChainLength != -1 && ++ChainLength >= MaxChainLength) |
| 283 | return Res; |
| 284 | |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 285 | auto HoistStat = hoistExpressions(F); |
Sebastian Pop | 5d3822f | 2016-08-03 20:54:33 +0000 | [diff] [blame] | 286 | if (HoistStat.first + HoistStat.second == 0) |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 287 | return Res; |
Sebastian Pop | 5d3822f | 2016-08-03 20:54:33 +0000 | [diff] [blame] | 288 | |
| 289 | if (HoistStat.second > 0) |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 290 | // To address a limitation of the current GVN, we need to rerun the |
Sebastian Pop | 5d3822f | 2016-08-03 20:54:33 +0000 | [diff] [blame] | 291 | // hoisting after we hoisted loads or stores in order to be able to |
| 292 | // hoist all scalars dependent on the hoisted ld/st. |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 293 | VN.clear(); |
Sebastian Pop | 5d3822f | 2016-08-03 20:54:33 +0000 | [diff] [blame] | 294 | |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 295 | Res = true; |
| 296 | } |
| 297 | |
| 298 | return Res; |
| 299 | } |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 300 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 301 | // Copied from NewGVN.cpp |
| 302 | // This function provides global ranking of operations so that we can place |
| 303 | // them in a canonical order. Note that rank alone is not necessarily enough |
| 304 | // for a complete ordering, as constants all have the same rank. However, |
| 305 | // generally, we will simplify an operation with all constants so that it |
| 306 | // doesn't matter what order they appear in. |
| 307 | unsigned int rank(const Value *V) const { |
| 308 | // Prefer constants to undef to anything else |
| 309 | // Undef is a constant, have to check it first. |
| 310 | // Prefer smaller constants to constantexprs |
| 311 | if (isa<ConstantExpr>(V)) |
| 312 | return 2; |
| 313 | if (isa<UndefValue>(V)) |
| 314 | return 1; |
| 315 | if (isa<Constant>(V)) |
| 316 | return 0; |
| 317 | else if (auto *A = dyn_cast<Argument>(V)) |
| 318 | return 3 + A->getArgNo(); |
| 319 | |
| 320 | // Need to shift the instruction DFS by number of arguments + 3 to account |
| 321 | // for the constant and argument ranking above. |
| 322 | auto Result = DFSNumber.lookup(V); |
| 323 | if (Result > 0) |
| 324 | return 4 + NumFuncArgs + Result; |
| 325 | // Unreachable or something else, just return a really large number. |
| 326 | return ~0; |
| 327 | } |
| 328 | |
Daniel Berlin | 65af45d | 2016-07-25 17:24:22 +0000 | [diff] [blame] | 329 | private: |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 330 | GVN::ValueTable VN; |
| 331 | DominatorTree *DT; |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 332 | PostDominatorTree *PDT; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 333 | AliasAnalysis *AA; |
| 334 | MemoryDependenceResults *MD; |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 335 | MemorySSA *MSSA; |
Daniel Berlin | 17e8d0e | 2017-02-22 22:19:55 +0000 | [diff] [blame] | 336 | std::unique_ptr<MemorySSAUpdater> MSSAUpdater; |
Sebastian Pop | 91d4a30 | 2016-07-26 00:15:10 +0000 | [diff] [blame] | 337 | DenseMap<const Value *, unsigned> DFSNumber; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 338 | BBSideEffectsSet BBSideEffects; |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 339 | DenseSet<const BasicBlock *> HoistBarrier; |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 340 | SmallVector<BasicBlock *, 32> IDFBlocks; |
| 341 | unsigned NumFuncArgs; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 342 | const bool HoistingGeps = false; |
David Majnemer | aa24178 | 2016-07-18 00:35:01 +0000 | [diff] [blame] | 343 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 344 | enum InsKind { Unknown, Scalar, Load, Store }; |
| 345 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 346 | // Return true when there are exception handling in BB. |
| 347 | bool hasEH(const BasicBlock *BB) { |
| 348 | auto It = BBSideEffects.find(BB); |
| 349 | if (It != BBSideEffects.end()) |
| 350 | return It->second; |
| 351 | |
| 352 | if (BB->isEHPad() || BB->hasAddressTaken()) { |
| 353 | BBSideEffects[BB] = true; |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | if (BB->getTerminator()->mayThrow()) { |
| 358 | BBSideEffects[BB] = true; |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | BBSideEffects[BB] = false; |
| 363 | return false; |
| 364 | } |
| 365 | |
Sebastian Pop | 5f0d0e6 | 2016-08-25 11:55:47 +0000 | [diff] [blame] | 366 | // Return true when a successor of BB dominates A. |
| 367 | bool successorDominate(const BasicBlock *BB, const BasicBlock *A) { |
Chandler Carruth | 96fc1de | 2018-08-26 08:41:15 +0000 | [diff] [blame] | 368 | for (const BasicBlock *Succ : successors(BB)) |
Sebastian Pop | 5f0d0e6 | 2016-08-25 11:55:47 +0000 | [diff] [blame] | 369 | if (DT->dominates(Succ, A)) |
| 370 | return true; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 371 | |
Sebastian Pop | 5f0d0e6 | 2016-08-25 11:55:47 +0000 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 375 | // Return true when I1 appears before I2 in the instructions of BB. |
Sebastian Pop | 91d4a30 | 2016-07-26 00:15:10 +0000 | [diff] [blame] | 376 | bool firstInBB(const Instruction *I1, const Instruction *I2) { |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 377 | assert(I1->getParent() == I2->getParent()); |
Sebastian Pop | 91d4a30 | 2016-07-26 00:15:10 +0000 | [diff] [blame] | 378 | unsigned I1DFS = DFSNumber.lookup(I1); |
| 379 | unsigned I2DFS = DFSNumber.lookup(I2); |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 380 | assert(I1DFS && I2DFS); |
Sebastian Pop | 91d4a30 | 2016-07-26 00:15:10 +0000 | [diff] [blame] | 381 | return I1DFS < I2DFS; |
Daniel Berlin | 40765a6 | 2016-07-25 18:19:49 +0000 | [diff] [blame] | 382 | } |
Sebastian Pop | 91d4a30 | 2016-07-26 00:15:10 +0000 | [diff] [blame] | 383 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 384 | // Return true when there are memory uses of Def in BB. |
| 385 | bool hasMemoryUse(const Instruction *NewPt, MemoryDef *Def, |
| 386 | const BasicBlock *BB) { |
| 387 | const MemorySSA::AccessList *Acc = MSSA->getBlockAccesses(BB); |
| 388 | if (!Acc) |
| 389 | return false; |
| 390 | |
| 391 | Instruction *OldPt = Def->getMemoryInst(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 392 | const BasicBlock *OldBB = OldPt->getParent(); |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 393 | const BasicBlock *NewBB = NewPt->getParent(); |
| 394 | bool ReachedNewPt = false; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 395 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 396 | for (const MemoryAccess &MA : *Acc) |
| 397 | if (const MemoryUse *MU = dyn_cast<MemoryUse>(&MA)) { |
| 398 | Instruction *Insn = MU->getMemoryInst(); |
Sebastian Pop | 1531f30 | 2016-09-22 17:22:58 +0000 | [diff] [blame] | 399 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 400 | // Do not check whether MU aliases Def when MU occurs after OldPt. |
| 401 | if (BB == OldBB && firstInBB(OldPt, Insn)) |
| 402 | break; |
| 403 | |
| 404 | // Do not check whether MU aliases Def when MU occurs before NewPt. |
| 405 | if (BB == NewBB) { |
| 406 | if (!ReachedNewPt) { |
| 407 | if (firstInBB(Insn, NewPt)) |
| 408 | continue; |
| 409 | ReachedNewPt = true; |
| 410 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 411 | } |
Daniel Berlin | dcb004f | 2017-03-02 23:06:46 +0000 | [diff] [blame] | 412 | if (MemorySSAUtil::defClobbersUseOrDef(Def, MU, *AA)) |
Hans Wennborg | c7957ef | 2016-09-22 21:20:53 +0000 | [diff] [blame] | 413 | return true; |
| 414 | } |
Sebastian Pop | 8e6e331 | 2016-09-22 15:33:51 +0000 | [diff] [blame] | 415 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 416 | return false; |
| 417 | } |
| 418 | |
Davide Italiano | 32504cf | 2017-09-05 20:49:41 +0000 | [diff] [blame] | 419 | bool hasEHhelper(const BasicBlock *BB, const BasicBlock *SrcBB, |
| 420 | int &NBBsOnAllPaths) { |
| 421 | // Stop walk once the limit is reached. |
| 422 | if (NBBsOnAllPaths == 0) |
| 423 | return true; |
| 424 | |
| 425 | // Impossible to hoist with exceptions on the path. |
| 426 | if (hasEH(BB)) |
| 427 | return true; |
| 428 | |
| 429 | // No such instruction after HoistBarrier in a basic block was |
| 430 | // selected for hoisting so instructions selected within basic block with |
| 431 | // a hoist barrier can be hoisted. |
| 432 | if ((BB != SrcBB) && HoistBarrier.count(BB)) |
| 433 | return true; |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 438 | // Return true when there are exception handling or loads of memory Def |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 439 | // between Def and NewPt. This function is only called for stores: Def is |
| 440 | // the MemoryDef of the store to be hoisted. |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 441 | |
| 442 | // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and |
| 443 | // return true when the counter NBBsOnAllPaths reaces 0, except when it is |
| 444 | // initialized to -1 which is unlimited. |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 445 | bool hasEHOrLoadsOnPath(const Instruction *NewPt, MemoryDef *Def, |
| 446 | int &NBBsOnAllPaths) { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 447 | const BasicBlock *NewBB = NewPt->getParent(); |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 448 | const BasicBlock *OldBB = Def->getBlock(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 449 | assert(DT->dominates(NewBB, OldBB) && "invalid path"); |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 450 | assert(DT->dominates(Def->getDefiningAccess()->getBlock(), NewBB) && |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 451 | "def does not dominate new hoisting point"); |
| 452 | |
| 453 | // Walk all basic blocks reachable in depth-first iteration on the inverse |
| 454 | // CFG from OldBB to NewBB. These blocks are all the blocks that may be |
| 455 | // executed between the execution of NewBB and OldBB. Hoisting an expression |
| 456 | // from OldBB into NewBB has to be safe on all execution paths. |
| 457 | for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) { |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 458 | const BasicBlock *BB = *I; |
| 459 | if (BB == NewBB) { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 460 | // Stop traversal when reaching HoistPt. |
| 461 | I.skipChildren(); |
| 462 | continue; |
| 463 | } |
| 464 | |
Davide Italiano | 32504cf | 2017-09-05 20:49:41 +0000 | [diff] [blame] | 465 | if (hasEHhelper(BB, OldBB, NBBsOnAllPaths)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 466 | return true; |
| 467 | |
| 468 | // Check that we do not move a store past loads. |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 469 | if (hasMemoryUse(NewPt, Def, BB)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 470 | return true; |
| 471 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 472 | // -1 is unlimited number of blocks on all paths. |
| 473 | if (NBBsOnAllPaths != -1) |
| 474 | --NBBsOnAllPaths; |
| 475 | |
| 476 | ++I; |
| 477 | } |
| 478 | |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // Return true when there are exception handling between HoistPt and BB. |
| 483 | // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and |
| 484 | // return true when the counter NBBsOnAllPaths reaches 0, except when it is |
| 485 | // initialized to -1 which is unlimited. |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 486 | bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *SrcBB, |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 487 | int &NBBsOnAllPaths) { |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 488 | assert(DT->dominates(HoistPt, SrcBB) && "Invalid path"); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 489 | |
| 490 | // Walk all basic blocks reachable in depth-first iteration on |
| 491 | // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the |
| 492 | // blocks that may be executed between the execution of NewHoistPt and |
| 493 | // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe |
| 494 | // on all execution paths. |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 495 | for (auto I = idf_begin(SrcBB), E = idf_end(SrcBB); I != E;) { |
| 496 | const BasicBlock *BB = *I; |
| 497 | if (BB == HoistPt) { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 498 | // Stop traversal when reaching NewHoistPt. |
| 499 | I.skipChildren(); |
| 500 | continue; |
| 501 | } |
| 502 | |
Davide Italiano | 32504cf | 2017-09-05 20:49:41 +0000 | [diff] [blame] | 503 | if (hasEHhelper(BB, SrcBB, NBBsOnAllPaths)) |
Aditya Kumar | 314ebe0 | 2016-11-29 14:36:27 +0000 | [diff] [blame] | 504 | return true; |
| 505 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 506 | // -1 is unlimited number of blocks on all paths. |
| 507 | if (NBBsOnAllPaths != -1) |
| 508 | --NBBsOnAllPaths; |
| 509 | |
| 510 | ++I; |
| 511 | } |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | // Return true when it is safe to hoist a memory load or store U from OldPt |
| 517 | // to NewPt. |
| 518 | bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt, |
| 519 | MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 520 | // In place hoisting is safe. |
| 521 | if (NewPt == OldPt) |
| 522 | return true; |
| 523 | |
| 524 | const BasicBlock *NewBB = NewPt->getParent(); |
| 525 | const BasicBlock *OldBB = OldPt->getParent(); |
| 526 | const BasicBlock *UBB = U->getBlock(); |
| 527 | |
| 528 | // Check for dependences on the Memory SSA. |
| 529 | MemoryAccess *D = U->getDefiningAccess(); |
| 530 | BasicBlock *DBB = D->getBlock(); |
| 531 | if (DT->properlyDominates(NewBB, DBB)) |
| 532 | // Cannot move the load or store to NewBB above its definition in DBB. |
| 533 | return false; |
| 534 | |
| 535 | if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D)) |
David Majnemer | 4c66a71 | 2016-07-18 00:34:58 +0000 | [diff] [blame] | 536 | if (auto *UD = dyn_cast<MemoryUseOrDef>(D)) |
Alexandros Lamprineas | 592cc78 | 2018-07-23 09:42:35 +0000 | [diff] [blame] | 537 | if (!firstInBB(UD->getMemoryInst(), NewPt)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 538 | // Cannot move the load or store to NewPt above its definition in D. |
| 539 | return false; |
| 540 | |
| 541 | // Check for unsafe hoistings due to side effects. |
| 542 | if (K == InsKind::Store) { |
Simon Pilgrim | 57e8f0b | 2019-10-21 17:15:49 +0000 | [diff] [blame] | 543 | if (hasEHOrLoadsOnPath(NewPt, cast<MemoryDef>(U), NBBsOnAllPaths)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 544 | return false; |
| 545 | } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths)) |
| 546 | return false; |
| 547 | |
| 548 | if (UBB == NewBB) { |
| 549 | if (DT->properlyDominates(DBB, NewBB)) |
| 550 | return true; |
| 551 | assert(UBB == DBB); |
| 552 | assert(MSSA->locallyDominates(D, U)); |
| 553 | } |
| 554 | |
| 555 | // No side effects: it is safe to hoist. |
| 556 | return true; |
| 557 | } |
| 558 | |
Sebastian Pop | 5f0d0e6 | 2016-08-25 11:55:47 +0000 | [diff] [blame] | 559 | // Return true when it is safe to hoist scalar instructions from all blocks in |
| 560 | // WL to HoistBB. |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 561 | bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB, |
Sebastian Pop | 5f0d0e6 | 2016-08-25 11:55:47 +0000 | [diff] [blame] | 562 | int &NBBsOnAllPaths) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 563 | return !hasEHOnPath(HoistBB, BB, NBBsOnAllPaths); |
| 564 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 565 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 566 | // In the inverse CFG, the dominance frontier of basic block (BB) is the |
| 567 | // point where ANTIC needs to be computed for instructions which are going |
| 568 | // to be hoisted. Since this point does not change during gvn-hoist, |
| 569 | // we compute it only once (on demand). |
| 570 | // The ides is inspired from: |
| 571 | // "Partial Redundancy Elimination in SSA Form" |
| 572 | // ROBERT KENNEDY, SUN CHAN, SHIN-MING LIU, RAYMOND LO, PENG TU and FRED CHOW |
Hiroshi Inoue | c8e9245 | 2018-01-29 05:17:03 +0000 | [diff] [blame] | 573 | // They use similar idea in the forward graph to find fully redundant and |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 574 | // partially redundant expressions, here it is used in the inverse graph to |
| 575 | // find fully anticipable instructions at merge point (post-dominator in |
| 576 | // the inverse CFG). |
| 577 | // Returns the edge via which an instruction in BB will get the values from. |
| 578 | |
| 579 | // Returns true when the values are flowing out to each edge. |
Chandler Carruth | e303c87 | 2018-10-15 10:42:50 +0000 | [diff] [blame] | 580 | bool valueAnticipable(CHIArgs C, Instruction *TI) const { |
Vedant Kumar | 5a0872c | 2018-05-16 23:20:42 +0000 | [diff] [blame] | 581 | if (TI->getNumSuccessors() > (unsigned)size(C)) |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 582 | return false; // Not enough args in this CHI. |
| 583 | |
| 584 | for (auto CHI : C) { |
| 585 | BasicBlock *Dest = CHI.Dest; |
| 586 | // Find if all the edges have values flowing out of BB. |
Chandler Carruth | 96fc1de | 2018-08-26 08:41:15 +0000 | [diff] [blame] | 587 | bool Found = llvm::any_of( |
| 588 | successors(TI), [Dest](const BasicBlock *BB) { return BB == Dest; }); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 589 | if (!Found) |
Sebastian Pop | 5f0d0e6 | 2016-08-25 11:55:47 +0000 | [diff] [blame] | 590 | return false; |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 591 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 592 | return true; |
| 593 | } |
| 594 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 595 | // Check if it is safe to hoist values tracked by CHI in the range |
| 596 | // [Begin, End) and accumulate them in Safe. |
| 597 | void checkSafety(CHIArgs C, BasicBlock *BB, InsKind K, |
| 598 | SmallVectorImpl<CHIArg> &Safe) { |
Aditya Kumar | 314ebe0 | 2016-11-29 14:36:27 +0000 | [diff] [blame] | 599 | int NumBBsOnAllPaths = MaxNumberOfBBSInPath; |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 600 | for (auto CHI : C) { |
| 601 | Instruction *Insn = CHI.I; |
| 602 | if (!Insn) // No instruction was inserted in this CHI. |
| 603 | continue; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 604 | if (K == InsKind::Scalar) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 605 | if (safeToHoistScalar(BB, Insn->getParent(), NumBBsOnAllPaths)) |
| 606 | Safe.push_back(CHI); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 607 | } else { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 608 | MemoryUseOrDef *UD = MSSA->getMemoryAccess(Insn); |
| 609 | if (safeToHoistLdSt(BB->getTerminator(), Insn, UD, K, NumBBsOnAllPaths)) |
| 610 | Safe.push_back(CHI); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 611 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 612 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 615 | using RenameStackType = DenseMap<VNType, SmallVector<Instruction *, 2>>; |
| 616 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 617 | // Push all the VNs corresponding to BB into RenameStack. |
| 618 | void fillRenameStack(BasicBlock *BB, InValuesType &ValueBBs, |
| 619 | RenameStackType &RenameStack) { |
| 620 | auto it1 = ValueBBs.find(BB); |
| 621 | if (it1 != ValueBBs.end()) { |
| 622 | // Iterate in reverse order to keep lower ranked values on the top. |
| 623 | for (std::pair<VNType, Instruction *> &VI : reverse(it1->second)) { |
| 624 | // Get the value of instruction I |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 625 | LLVM_DEBUG(dbgs() << "\nPushing on stack: " << *VI.second); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 626 | RenameStack[VI.first].push_back(VI.second); |
| 627 | } |
| 628 | } |
| 629 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 630 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 631 | void fillChiArgs(BasicBlock *BB, OutValuesType &CHIBBs, |
| 632 | RenameStackType &RenameStack) { |
| 633 | // For each *predecessor* (because Post-DOM) of BB check if it has a CHI |
| 634 | for (auto Pred : predecessors(BB)) { |
| 635 | auto P = CHIBBs.find(Pred); |
| 636 | if (P == CHIBBs.end()) { |
| 637 | continue; |
| 638 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 639 | LLVM_DEBUG(dbgs() << "\nLooking at CHIs in: " << Pred->getName();); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 640 | // A CHI is found (BB -> Pred is an edge in the CFG) |
| 641 | // Pop the stack until Top(V) = Ve. |
| 642 | auto &VCHI = P->second; |
| 643 | for (auto It = VCHI.begin(), E = VCHI.end(); It != E;) { |
| 644 | CHIArg &C = *It; |
| 645 | if (!C.Dest) { |
| 646 | auto si = RenameStack.find(C.VN); |
| 647 | // The Basic Block where CHI is must dominate the value we want to |
| 648 | // track in a CHI. In the PDom walk, there can be values in the |
| 649 | // stack which are not control dependent e.g., nested loop. |
| 650 | if (si != RenameStack.end() && si->second.size() && |
Aditya Kumar | 1f90cae | 2018-01-04 07:47:24 +0000 | [diff] [blame] | 651 | DT->properlyDominates(Pred, si->second.back()->getParent())) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 652 | C.Dest = BB; // Assign the edge |
| 653 | C.I = si->second.pop_back_val(); // Assign the argument |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 654 | LLVM_DEBUG(dbgs() |
| 655 | << "\nCHI Inserted in BB: " << C.Dest->getName() << *C.I |
| 656 | << ", VN: " << C.VN.first << ", " << C.VN.second); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 657 | } |
| 658 | // Move to next CHI of a different value |
| 659 | It = std::find_if(It, VCHI.end(), |
| 660 | [It](CHIArg &A) { return A != *It; }); |
| 661 | } else |
| 662 | ++It; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Walk the post-dominator tree top-down and use a stack for each value to |
| 668 | // store the last value you see. When you hit a CHI from a given edge, the |
| 669 | // value to use as the argument is at the top of the stack, add the value to |
| 670 | // CHI and pop. |
| 671 | void insertCHI(InValuesType &ValueBBs, OutValuesType &CHIBBs) { |
| 672 | auto Root = PDT->getNode(nullptr); |
| 673 | if (!Root) |
| 674 | return; |
| 675 | // Depth first walk on PDom tree to fill the CHIargs at each PDF. |
| 676 | RenameStackType RenameStack; |
| 677 | for (auto Node : depth_first(Root)) { |
| 678 | BasicBlock *BB = Node->getBlock(); |
| 679 | if (!BB) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 680 | continue; |
| 681 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 682 | // Collect all values in BB and push to stack. |
| 683 | fillRenameStack(BB, ValueBBs, RenameStack); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 684 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 685 | // Fill outgoing values in each CHI corresponding to BB. |
| 686 | fillChiArgs(BB, CHIBBs, RenameStack); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 690 | // Walk all the CHI-nodes to find ones which have a empty-entry and remove |
| 691 | // them Then collect all the instructions which are safe to hoist and see if |
| 692 | // they form a list of anticipable values. OutValues contains CHIs |
| 693 | // corresponding to each basic block. |
| 694 | void findHoistableCandidates(OutValuesType &CHIBBs, InsKind K, |
| 695 | HoistingPointList &HPL) { |
| 696 | auto cmpVN = [](const CHIArg &A, const CHIArg &B) { return A.VN < B.VN; }; |
| 697 | |
| 698 | // CHIArgs now have the outgoing values, so check for anticipability and |
| 699 | // accumulate hoistable candidates in HPL. |
| 700 | for (std::pair<BasicBlock *, SmallVector<CHIArg, 2>> &A : CHIBBs) { |
| 701 | BasicBlock *BB = A.first; |
| 702 | SmallVectorImpl<CHIArg> &CHIs = A.second; |
| 703 | // Vector of PHIs contains PHIs for different instructions. |
| 704 | // Sort the args according to their VNs, such that identical |
| 705 | // instructions are together. |
Fangrui Song | efd94c5 | 2019-04-23 14:51:27 +0000 | [diff] [blame] | 706 | llvm::stable_sort(CHIs, cmpVN); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 707 | auto TI = BB->getTerminator(); |
| 708 | auto B = CHIs.begin(); |
| 709 | // [PreIt, PHIIt) form a range of CHIs which have identical VNs. |
| 710 | auto PHIIt = std::find_if(CHIs.begin(), CHIs.end(), |
| 711 | [B](CHIArg &A) { return A != *B; }); |
| 712 | auto PrevIt = CHIs.begin(); |
| 713 | while (PrevIt != PHIIt) { |
| 714 | // Collect values which satisfy safety checks. |
| 715 | SmallVector<CHIArg, 2> Safe; |
| 716 | // We check for safety first because there might be multiple values in |
| 717 | // the same path, some of which are not safe to be hoisted, but overall |
| 718 | // each edge has at least one value which can be hoisted, making the |
| 719 | // value anticipable along that path. |
| 720 | checkSafety(make_range(PrevIt, PHIIt), BB, K, Safe); |
| 721 | |
| 722 | // List of safe values should be anticipable at TI. |
| 723 | if (valueAnticipable(make_range(Safe.begin(), Safe.end()), TI)) { |
| 724 | HPL.push_back({BB, SmallVecInsn()}); |
| 725 | SmallVecInsn &V = HPL.back().second; |
| 726 | for (auto B : Safe) |
| 727 | V.push_back(B.I); |
| 728 | } |
| 729 | |
| 730 | // Check other VNs |
| 731 | PrevIt = PHIIt; |
| 732 | PHIIt = std::find_if(PrevIt, CHIs.end(), |
| 733 | [PrevIt](CHIArg &A) { return A != *PrevIt; }); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // Compute insertion points for each values which can be fully anticipated at |
| 739 | // a dominator. HPL contains all such values. |
| 740 | void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL, |
| 741 | InsKind K) { |
| 742 | // Sort VNs based on their rankings |
| 743 | std::vector<VNType> Ranks; |
| 744 | for (const auto &Entry : Map) { |
| 745 | Ranks.push_back(Entry.first); |
| 746 | } |
| 747 | |
| 748 | // TODO: Remove fully-redundant expressions. |
| 749 | // Get instruction from the Map, assume that all the Instructions |
| 750 | // with same VNs have same rank (this is an approximation). |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 751 | llvm::sort(Ranks, [this, &Map](const VNType &r1, const VNType &r2) { |
| 752 | return (rank(*Map.lookup(r1).begin()) < rank(*Map.lookup(r2).begin())); |
| 753 | }); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 754 | |
| 755 | // - Sort VNs according to their rank, and start with lowest ranked VN |
| 756 | // - Take a VN and for each instruction with same VN |
| 757 | // - Find the dominance frontier in the inverse graph (PDF) |
| 758 | // - Insert the chi-node at PDF |
| 759 | // - Remove the chi-nodes with missing entries |
| 760 | // - Remove values from CHI-nodes which do not truly flow out, e.g., |
| 761 | // modified along the path. |
| 762 | // - Collect the remaining values that are still anticipable |
| 763 | SmallVector<BasicBlock *, 2> IDFBlocks; |
| 764 | ReverseIDFCalculator IDFs(*PDT); |
| 765 | OutValuesType OutValue; |
| 766 | InValuesType InValue; |
| 767 | for (const auto &R : Ranks) { |
| 768 | const SmallVecInsn &V = Map.lookup(R); |
| 769 | if (V.size() < 2) |
| 770 | continue; |
| 771 | const VNType &VN = R; |
| 772 | SmallPtrSet<BasicBlock *, 2> VNBlocks; |
| 773 | for (auto &I : V) { |
| 774 | BasicBlock *BBI = I->getParent(); |
| 775 | if (!hasEH(BBI)) |
| 776 | VNBlocks.insert(BBI); |
| 777 | } |
| 778 | // Compute the Post Dominance Frontiers of each basic block |
| 779 | // The dominance frontier of a live block X in the reverse |
| 780 | // control graph is the set of blocks upon which X is control |
| 781 | // dependent. The following sequence computes the set of blocks |
| 782 | // which currently have dead terminators that are control |
| 783 | // dependence sources of a block which is in NewLiveBlocks. |
| 784 | IDFs.setDefiningBlocks(VNBlocks); |
Alexandros Lamprineas | 54d56f2 | 2018-09-12 14:28:23 +0000 | [diff] [blame] | 785 | IDFBlocks.clear(); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 786 | IDFs.calculate(IDFBlocks); |
| 787 | |
| 788 | // Make a map of BB vs instructions to be hoisted. |
| 789 | for (unsigned i = 0; i < V.size(); ++i) { |
| 790 | InValue[V[i]->getParent()].push_back(std::make_pair(VN, V[i])); |
| 791 | } |
| 792 | // Insert empty CHI node for this VN. This is used to factor out |
| 793 | // basic blocks where the ANTIC can potentially change. |
Alexandros Lamprineas | 484bd13 | 2018-08-28 11:07:54 +0000 | [diff] [blame] | 794 | for (auto IDFB : IDFBlocks) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 795 | for (unsigned i = 0; i < V.size(); ++i) { |
| 796 | CHIArg C = {VN, nullptr, nullptr}; |
Aditya Kumar | 49c03b1 | 2017-12-13 19:40:07 +0000 | [diff] [blame] | 797 | // Ignore spurious PDFs. |
Alexandros Lamprineas | 54d56f2 | 2018-09-12 14:28:23 +0000 | [diff] [blame] | 798 | if (DT->properlyDominates(IDFB, V[i]->getParent())) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 799 | OutValue[IDFB].push_back(C); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 800 | LLVM_DEBUG(dbgs() << "\nInsertion a CHI for BB: " << IDFB->getName() |
| 801 | << ", for Insn: " << *V[i]); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // Insert CHI args at each PDF to iterate on factored graph of |
| 808 | // control dependence. |
| 809 | insertCHI(InValue, OutValue); |
| 810 | // Using the CHI args inserted at each PDF, find fully anticipable values. |
| 811 | findHoistableCandidates(OutValue, K, HPL); |
| 812 | } |
| 813 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 814 | // Return true when all operands of Instr are available at insertion point |
| 815 | // HoistPt. When limiting the number of hoisted expressions, one could hoist |
| 816 | // a load without hoisting its access function. So before hoisting any |
| 817 | // expression, make sure that all its operands are available at insert point. |
| 818 | bool allOperandsAvailable(const Instruction *I, |
| 819 | const BasicBlock *HoistPt) const { |
David Majnemer | 4c66a71 | 2016-07-18 00:34:58 +0000 | [diff] [blame] | 820 | for (const Use &Op : I->operands()) |
| 821 | if (const auto *Inst = dyn_cast<Instruction>(&Op)) |
| 822 | if (!DT->dominates(Inst->getParent(), HoistPt)) |
| 823 | return false; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 824 | |
| 825 | return true; |
| 826 | } |
| 827 | |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 828 | // Same as allOperandsAvailable with recursive check for GEP operands. |
| 829 | bool allGepOperandsAvailable(const Instruction *I, |
| 830 | const BasicBlock *HoistPt) const { |
| 831 | for (const Use &Op : I->operands()) |
| 832 | if (const auto *Inst = dyn_cast<Instruction>(&Op)) |
| 833 | if (!DT->dominates(Inst->getParent(), HoistPt)) { |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 834 | if (const GetElementPtrInst *GepOp = |
| 835 | dyn_cast<GetElementPtrInst>(Inst)) { |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 836 | if (!allGepOperandsAvailable(GepOp, HoistPt)) |
| 837 | return false; |
| 838 | // Gep is available if all operands of GepOp are available. |
| 839 | } else { |
| 840 | // Gep is not available if it has operands other than GEPs that are |
| 841 | // defined in blocks not dominating HoistPt. |
| 842 | return false; |
| 843 | } |
| 844 | } |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | // Make all operands of the GEP available. |
| 849 | void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt, |
| 850 | const SmallVecInsn &InstructionsToHoist, |
| 851 | Instruction *Gep) const { |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 852 | assert(allGepOperandsAvailable(Gep, HoistPt) && |
| 853 | "GEP operands not available"); |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 854 | |
| 855 | Instruction *ClonedGep = Gep->clone(); |
| 856 | for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i) |
| 857 | if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) { |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 858 | // Check whether the operand is already available. |
| 859 | if (DT->dominates(Op->getParent(), HoistPt)) |
| 860 | continue; |
| 861 | |
| 862 | // As a GEP can refer to other GEPs, recursively make all the operands |
| 863 | // of this GEP available at HoistPt. |
| 864 | if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op)) |
| 865 | makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp); |
| 866 | } |
| 867 | |
| 868 | // Copy Gep and replace its uses in Repl with ClonedGep. |
| 869 | ClonedGep->insertBefore(HoistPt->getTerminator()); |
| 870 | |
| 871 | // Conservatively discard any optimization hints, they may differ on the |
| 872 | // other paths. |
| 873 | ClonedGep->dropUnknownNonDebugMetadata(); |
| 874 | |
| 875 | // If we have optimization hints which agree with each other along different |
| 876 | // paths, preserve them. |
| 877 | for (const Instruction *OtherInst : InstructionsToHoist) { |
| 878 | const GetElementPtrInst *OtherGep; |
| 879 | if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst)) |
| 880 | OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand()); |
| 881 | else |
| 882 | OtherGep = cast<GetElementPtrInst>( |
| 883 | cast<StoreInst>(OtherInst)->getPointerOperand()); |
Peter Collingbourne | 8f1dd5c | 2016-09-07 23:39:04 +0000 | [diff] [blame] | 884 | ClonedGep->andIRFlags(OtherGep); |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | // Replace uses of Gep with ClonedGep in Repl. |
| 888 | Repl->replaceUsesOfWith(Gep, ClonedGep); |
| 889 | } |
| 890 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 891 | void updateAlignment(Instruction *I, Instruction *Repl) { |
| 892 | if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) { |
Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 893 | ReplacementLoad->setAlignment(MaybeAlign(std::min( |
| 894 | ReplacementLoad->getAlignment(), cast<LoadInst>(I)->getAlignment()))); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 895 | ++NumLoadsRemoved; |
| 896 | } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) { |
| 897 | ReplacementStore->setAlignment( |
Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 898 | MaybeAlign(std::min(ReplacementStore->getAlignment(), |
| 899 | cast<StoreInst>(I)->getAlignment()))); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 900 | ++NumStoresRemoved; |
| 901 | } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) { |
| 902 | ReplacementAlloca->setAlignment( |
Guillaume Chatelet | ab11b91 | 2019-09-30 13:34:44 +0000 | [diff] [blame] | 903 | MaybeAlign(std::max(ReplacementAlloca->getAlignment(), |
| 904 | cast<AllocaInst>(I)->getAlignment()))); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 905 | } else if (isa<CallInst>(Repl)) { |
| 906 | ++NumCallsRemoved; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | // Remove all the instructions in Candidates and replace their usage with Repl. |
| 911 | // Returns the number of instructions removed. |
| 912 | unsigned rauw(const SmallVecInsn &Candidates, Instruction *Repl, |
| 913 | MemoryUseOrDef *NewMemAcc) { |
| 914 | unsigned NR = 0; |
| 915 | for (Instruction *I : Candidates) { |
| 916 | if (I != Repl) { |
| 917 | ++NR; |
| 918 | updateAlignment(I, Repl); |
| 919 | if (NewMemAcc) { |
| 920 | // Update the uses of the old MSSA access with NewMemAcc. |
| 921 | MemoryAccess *OldMA = MSSA->getMemoryAccess(I); |
| 922 | OldMA->replaceAllUsesWith(NewMemAcc); |
| 923 | MSSAUpdater->removeMemoryAccess(OldMA); |
| 924 | } |
| 925 | |
| 926 | Repl->andIRFlags(I); |
| 927 | combineKnownMetadata(Repl, I); |
| 928 | I->replaceAllUsesWith(Repl); |
| 929 | // Also invalidate the Alias Analysis cache. |
| 930 | MD->removeInstruction(I); |
| 931 | I->eraseFromParent(); |
| 932 | } |
| 933 | } |
| 934 | return NR; |
| 935 | } |
| 936 | |
| 937 | // Replace all Memory PHI usage with NewMemAcc. |
| 938 | void raMPHIuw(MemoryUseOrDef *NewMemAcc) { |
| 939 | SmallPtrSet<MemoryPhi *, 4> UsePhis; |
| 940 | for (User *U : NewMemAcc->users()) |
| 941 | if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(U)) |
| 942 | UsePhis.insert(Phi); |
| 943 | |
| 944 | for (MemoryPhi *Phi : UsePhis) { |
| 945 | auto In = Phi->incoming_values(); |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 946 | if (llvm::all_of(In, [&](Use &U) { return U == NewMemAcc; })) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 947 | Phi->replaceAllUsesWith(NewMemAcc); |
| 948 | MSSAUpdater->removeMemoryAccess(Phi); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | // Remove all other instructions and replace them with Repl. |
| 954 | unsigned removeAndReplace(const SmallVecInsn &Candidates, Instruction *Repl, |
| 955 | BasicBlock *DestBB, bool MoveAccess) { |
| 956 | MemoryUseOrDef *NewMemAcc = MSSA->getMemoryAccess(Repl); |
| 957 | if (MoveAccess && NewMemAcc) { |
| 958 | // The definition of this ld/st will not change: ld/st hoisting is |
| 959 | // legal when the ld/st is not moved past its current definition. |
Alina Sbirlea | 5c5cf89 | 2019-11-20 16:09:37 -0800 | [diff] [blame] | 960 | MSSAUpdater->moveToPlace(NewMemAcc, DestBB, |
| 961 | MemorySSA::BeforeTerminator); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Replace all other instructions with Repl with memory access NewMemAcc. |
| 965 | unsigned NR = rauw(Candidates, Repl, NewMemAcc); |
| 966 | |
| 967 | // Remove MemorySSA phi nodes with the same arguments. |
| 968 | if (NewMemAcc) |
| 969 | raMPHIuw(NewMemAcc); |
| 970 | return NR; |
| 971 | } |
| 972 | |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 973 | // In the case Repl is a load or a store, we make all their GEPs |
| 974 | // available: GEPs are not hoisted by default to avoid the address |
| 975 | // computations to be hoisted without the associated load or store. |
| 976 | bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt, |
| 977 | const SmallVecInsn &InstructionsToHoist) const { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 978 | // Check whether the GEP of a ld/st can be synthesized at HoistPt. |
David Majnemer | bd21012 | 2016-07-20 21:05:01 +0000 | [diff] [blame] | 979 | GetElementPtrInst *Gep = nullptr; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 980 | Instruction *Val = nullptr; |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 981 | if (auto *Ld = dyn_cast<LoadInst>(Repl)) { |
David Majnemer | bd21012 | 2016-07-20 21:05:01 +0000 | [diff] [blame] | 982 | Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand()); |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 983 | } else if (auto *St = dyn_cast<StoreInst>(Repl)) { |
David Majnemer | bd21012 | 2016-07-20 21:05:01 +0000 | [diff] [blame] | 984 | Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand()); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 985 | Val = dyn_cast<Instruction>(St->getValueOperand()); |
Sebastian Pop | 31fd506 | 2016-07-21 23:22:10 +0000 | [diff] [blame] | 986 | // Check that the stored value is available. |
Sebastian Pop | 0e2cec0 | 2016-07-22 00:07:01 +0000 | [diff] [blame] | 987 | if (Val) { |
| 988 | if (isa<GetElementPtrInst>(Val)) { |
| 989 | // Check whether we can compute the GEP at HoistPt. |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 990 | if (!allGepOperandsAvailable(Val, HoistPt)) |
Sebastian Pop | 0e2cec0 | 2016-07-22 00:07:01 +0000 | [diff] [blame] | 991 | return false; |
| 992 | } else if (!DT->dominates(Val->getParent(), HoistPt)) |
| 993 | return false; |
| 994 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 997 | // Check whether we can compute the Gep at HoistPt. |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 998 | if (!Gep || !allGepOperandsAvailable(Gep, HoistPt)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 1001 | makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1002 | |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 1003 | if (Val && isa<GetElementPtrInst>(Val)) |
| 1004 | makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1005 | |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
| 1009 | std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) { |
| 1010 | unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0; |
| 1011 | for (const HoistingPointInfo &HP : HPL) { |
| 1012 | // Find out whether we already have one of the instructions in HoistPt, |
| 1013 | // in which case we do not have to move it. |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1014 | BasicBlock *DestBB = HP.first; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1015 | const SmallVecInsn &InstructionsToHoist = HP.second; |
| 1016 | Instruction *Repl = nullptr; |
| 1017 | for (Instruction *I : InstructionsToHoist) |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1018 | if (I->getParent() == DestBB) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1019 | // If there are two instructions in HoistPt to be hoisted in place: |
| 1020 | // update Repl to be the first one, such that we can rename the uses |
| 1021 | // of the second based on the first. |
Sebastian Pop | 586d3ea | 2016-07-27 05:13:52 +0000 | [diff] [blame] | 1022 | if (!Repl || firstInBB(I, Repl)) |
| 1023 | Repl = I; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1024 | |
Daniel Berlin | f75fd1b | 2016-08-11 20:32:43 +0000 | [diff] [blame] | 1025 | // Keep track of whether we moved the instruction so we know whether we |
| 1026 | // should move the MemoryAccess. |
| 1027 | bool MoveAccess = true; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1028 | if (Repl) { |
| 1029 | // Repl is already in HoistPt: it remains in place. |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1030 | assert(allOperandsAvailable(Repl, DestBB) && |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1031 | "instruction depends on operands that are not available"); |
Daniel Berlin | f75fd1b | 2016-08-11 20:32:43 +0000 | [diff] [blame] | 1032 | MoveAccess = false; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1033 | } else { |
| 1034 | // When we do not find Repl in HoistPt, select the first in the list |
| 1035 | // and move it to HoistPt. |
| 1036 | Repl = InstructionsToHoist.front(); |
| 1037 | |
| 1038 | // We can move Repl in HoistPt only when all operands are available. |
| 1039 | // The order in which hoistings are done may influence the availability |
| 1040 | // of operands. |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1041 | if (!allOperandsAvailable(Repl, DestBB)) { |
Sebastian Pop | 429740a | 2016-08-04 23:49:05 +0000 | [diff] [blame] | 1042 | // When HoistingGeps there is nothing more we can do to make the |
| 1043 | // operands available: just continue. |
| 1044 | if (HoistingGeps) |
| 1045 | continue; |
| 1046 | |
| 1047 | // When not HoistingGeps we need to copy the GEPs. |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1048 | if (!makeGepOperandsAvailable(Repl, DestBB, InstructionsToHoist)) |
Sebastian Pop | 429740a | 2016-08-04 23:49:05 +0000 | [diff] [blame] | 1049 | continue; |
| 1050 | } |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 1051 | |
Sebastian Pop | 5d3822f | 2016-08-03 20:54:33 +0000 | [diff] [blame] | 1052 | // Move the instruction at the end of HoistPt. |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1053 | Instruction *Last = DestBB->getTerminator(); |
Eli Friedman | c6885fc | 2016-12-07 19:55:59 +0000 | [diff] [blame] | 1054 | MD->removeInstruction(Repl); |
Sebastian Pop | 4ba7c88 | 2016-08-03 20:54:36 +0000 | [diff] [blame] | 1055 | Repl->moveBefore(Last); |
| 1056 | |
| 1057 | DFSNumber[Repl] = DFSNumber[Last]++; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1060 | NR += removeAndReplace(InstructionsToHoist, Repl, DestBB, MoveAccess); |
Daniel Berlin | f75fd1b | 2016-08-11 20:32:43 +0000 | [diff] [blame] | 1061 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1062 | if (isa<LoadInst>(Repl)) |
| 1063 | ++NL; |
| 1064 | else if (isa<StoreInst>(Repl)) |
| 1065 | ++NS; |
| 1066 | else if (isa<CallInst>(Repl)) |
| 1067 | ++NC; |
| 1068 | else // Scalar |
| 1069 | ++NI; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Alina Sbirlea | 5c5cf89 | 2019-11-20 16:09:37 -0800 | [diff] [blame] | 1072 | if (MSSA && VerifyMemorySSA) |
| 1073 | MSSA->verifyMemorySSA(); |
| 1074 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1075 | NumHoisted += NL + NS + NC + NI; |
| 1076 | NumRemoved += NR; |
| 1077 | NumLoadsHoisted += NL; |
| 1078 | NumStoresHoisted += NS; |
| 1079 | NumCallsHoisted += NC; |
| 1080 | return {NI, NL + NC + NS}; |
| 1081 | } |
| 1082 | |
| 1083 | // Hoist all expressions. Returns Number of scalars hoisted |
| 1084 | // and number of non-scalars hoisted. |
| 1085 | std::pair<unsigned, unsigned> hoistExpressions(Function &F) { |
| 1086 | InsnInfo II; |
| 1087 | LoadInfo LI; |
| 1088 | StoreInfo SI; |
| 1089 | CallInfo CI; |
| 1090 | for (BasicBlock *BB : depth_first(&F.getEntryBlock())) { |
Sebastian Pop | 38422b1 | 2016-07-26 00:15:08 +0000 | [diff] [blame] | 1091 | int InstructionNb = 0; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1092 | for (Instruction &I1 : *BB) { |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 1093 | // If I1 cannot guarantee progress, subsequent instructions |
| 1094 | // in BB cannot be hoisted anyways. |
| 1095 | if (!isGuaranteedToTransferExecutionToSuccessor(&I1)) { |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1096 | HoistBarrier.insert(BB); |
| 1097 | break; |
Geoff Berry | 635e505 | 2017-04-10 20:45:17 +0000 | [diff] [blame] | 1098 | } |
Sebastian Pop | 38422b1 | 2016-07-26 00:15:08 +0000 | [diff] [blame] | 1099 | // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting |
| 1100 | // deeper may increase the register pressure and compilation time. |
| 1101 | if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB) |
| 1102 | break; |
| 1103 | |
Sebastian Pop | 440f15b | 2016-09-22 14:45:40 +0000 | [diff] [blame] | 1104 | // Do not value number terminator instructions. |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 1105 | if (I1.isTerminator()) |
Sebastian Pop | 440f15b | 2016-09-22 14:45:40 +0000 | [diff] [blame] | 1106 | break; |
| 1107 | |
David Majnemer | 4c66a71 | 2016-07-18 00:34:58 +0000 | [diff] [blame] | 1108 | if (auto *Load = dyn_cast<LoadInst>(&I1)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1109 | LI.insert(Load, VN); |
David Majnemer | 4c66a71 | 2016-07-18 00:34:58 +0000 | [diff] [blame] | 1110 | else if (auto *Store = dyn_cast<StoreInst>(&I1)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1111 | SI.insert(Store, VN); |
David Majnemer | 4c66a71 | 2016-07-18 00:34:58 +0000 | [diff] [blame] | 1112 | else if (auto *Call = dyn_cast<CallInst>(&I1)) { |
| 1113 | if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1114 | if (isa<DbgInfoIntrinsic>(Intr) || |
Dan Gohman | 2c74fe9 | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 1115 | Intr->getIntrinsicID() == Intrinsic::assume || |
| 1116 | Intr->getIntrinsicID() == Intrinsic::sideeffect) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1117 | continue; |
| 1118 | } |
Hans Wennborg | 19c0be9 | 2017-03-01 17:15:08 +0000 | [diff] [blame] | 1119 | if (Call->mayHaveSideEffects()) |
| 1120 | break; |
Matt Arsenault | 6ad9773 | 2016-08-04 20:52:57 +0000 | [diff] [blame] | 1121 | |
| 1122 | if (Call->isConvergent()) |
| 1123 | break; |
| 1124 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1125 | CI.insert(Call, VN); |
Sebastian Pop | 55c3007 | 2016-07-27 05:48:12 +0000 | [diff] [blame] | 1126 | } else if (HoistingGeps || !isa<GetElementPtrInst>(&I1)) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1127 | // Do not hoist scalars past calls that may write to memory because |
| 1128 | // that could result in spills later. geps are handled separately. |
| 1129 | // TODO: We can relax this for targets like AArch64 as they have more |
| 1130 | // registers than X86. |
| 1131 | II.insert(&I1, VN); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | HoistingPointList HPL; |
| 1136 | computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar); |
| 1137 | computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load); |
| 1138 | computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store); |
| 1139 | computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar); |
| 1140 | computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load); |
| 1141 | computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store); |
| 1142 | return hoist(HPL); |
| 1143 | } |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1144 | }; |
| 1145 | |
| 1146 | class GVNHoistLegacyPass : public FunctionPass { |
| 1147 | public: |
| 1148 | static char ID; |
| 1149 | |
| 1150 | GVNHoistLegacyPass() : FunctionPass(ID) { |
| 1151 | initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 1152 | } |
| 1153 | |
| 1154 | bool runOnFunction(Function &F) override { |
Paul Robinson | 2d23c02 | 2016-07-19 22:57:14 +0000 | [diff] [blame] | 1155 | if (skipFunction(F)) |
| 1156 | return false; |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1157 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1158 | auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1159 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 1160 | auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep(); |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 1161 | auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1162 | |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1163 | GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1164 | return G.run(F); |
| 1165 | } |
| 1166 | |
| 1167 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1168 | AU.addRequired<DominatorTreeWrapperPass>(); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1169 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1170 | AU.addRequired<AAResultsWrapperPass>(); |
| 1171 | AU.addRequired<MemoryDependenceWrapperPass>(); |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 1172 | AU.addRequired<MemorySSAWrapperPass>(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1173 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 1174 | AU.addPreserved<MemorySSAWrapperPass>(); |
Nikolai Bozhenov | 9e4a1c3 | 2017-04-18 13:25:49 +0000 | [diff] [blame] | 1175 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Alina Sbirlea | 4ae74cc | 2019-11-12 14:07:32 -0800 | [diff] [blame] | 1176 | AU.addPreserved<AAResultsWrapperPass>(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1177 | } |
| 1178 | }; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 1179 | |
| 1180 | } // end namespace llvm |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1181 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 1182 | PreservedAnalyses GVNHoistPass::run(Function &F, FunctionAnalysisManager &AM) { |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1183 | DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1184 | PostDominatorTree &PDT = AM.getResult<PostDominatorTreeAnalysis>(F); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1185 | AliasAnalysis &AA = AM.getResult<AAManager>(F); |
| 1186 | MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F); |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 1187 | MemorySSA &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA(); |
Aditya Kumar | dfa8741 | 2017-09-13 05:28:03 +0000 | [diff] [blame] | 1188 | GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1189 | if (!G.run(F)) |
| 1190 | return PreservedAnalyses::all(); |
| 1191 | |
| 1192 | PreservedAnalyses PA; |
| 1193 | PA.preserve<DominatorTreeAnalysis>(); |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 1194 | PA.preserve<MemorySSAAnalysis>(); |
Nikolai Bozhenov | 9e4a1c3 | 2017-04-18 13:25:49 +0000 | [diff] [blame] | 1195 | PA.preserve<GlobalsAA>(); |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1196 | return PA; |
| 1197 | } |
| 1198 | |
| 1199 | char GVNHoistLegacyPass::ID = 0; |
Eugene Zelenko | 3b87939 | 2017-10-13 21:17:07 +0000 | [diff] [blame] | 1200 | |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1201 | INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist", |
| 1202 | "Early GVN Hoisting of Expressions", false, false) |
| 1203 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass) |
Daniel Berlin | ea02eee | 2016-08-23 05:42:41 +0000 | [diff] [blame] | 1204 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1205 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Alexander Ivchenko | 836eac3 | 2018-02-08 11:45:36 +0000 | [diff] [blame] | 1206 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
Sebastian Pop | 4177480 | 2016-07-15 13:45:20 +0000 | [diff] [blame] | 1207 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 1208 | INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist", |
| 1209 | "Early GVN Hoisting of Expressions", false, false) |
| 1210 | |
| 1211 | FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); } |