blob: 10cdcd59e4215044077c554ef8ff1b78b2ce0cff [file] [log] [blame]
Sebastian Pop41774802016-07-15 13:45:20 +00001//===- GVNHoist.cpp - Hoist scalar and load expressions -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass hoists expressions from branches to a common dominator. It uses
11// GVN (global value numbering) to discover expressions computing the same
Aditya Kumarf24939b2016-08-13 11:56:50 +000012// values. The primary goals of code-hoisting are:
13// 1. To reduce the code size.
14// 2. In some cases reduce critical path (by exposing more ILP).
15//
Aditya Kumardfa87412017-09-13 05:28:03 +000016// The algorithm factors out the reachability of values such that multiple
17// queries to find reachability of values are fast. This is based on finding the
18// ANTIC points in the CFG which do not change during hoisting. The ANTIC points
19// are basically the dominance-frontiers in the inverse graph. So we introduce a
20// data structure (CHI nodes) to keep track of values flowing out of a basic
21// block. We only do this for values with multiple occurrences in the function
22// as they are the potential hoistable candidates. This approach allows us to
23// hoist instructions to a basic block with more than two successors, as well as
24// deal with infinite loops in a trivial way.
25//
26// Limitations: This pass does not hoist fully redundant expressions because
27// they are already handled by GVN-PRE. It is advisable to run gvn-hoist before
28// and after gvn-pre because gvn-pre creates opportunities for more instructions
29// to be hoisted.
30//
Sebastian Pop41774802016-07-15 13:45:20 +000031// Hoisting may affect the performance in some cases. To mitigate that, hoisting
32// is disabled in the following cases.
33// 1. Scalars across calls.
34// 2. geps when corresponding load/store cannot be hoisted.
35//===----------------------------------------------------------------------===//
36
37#include "llvm/ADT/DenseMap.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000038#include "llvm/ADT/DenseSet.h"
39#include "llvm/ADT/STLExtras.h"
Sebastian Pop41774802016-07-15 13:45:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000041#include "llvm/ADT/SmallVector.h"
Sebastian Pop41774802016-07-15 13:45:20 +000042#include "llvm/ADT/Statistic.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000043#include "llvm/ADT/iterator_range.h"
44#include "llvm/Analysis/AliasAnalysis.h"
Nikolai Bozhenov9e4a1c32017-04-18 13:25:49 +000045#include "llvm/Analysis/GlobalsModRef.h"
Aditya Kumardfa87412017-09-13 05:28:03 +000046#include "llvm/Analysis/IteratedDominanceFrontier.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000047#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000048#include "llvm/Analysis/MemorySSA.h"
49#include "llvm/Analysis/MemorySSAUpdater.h"
Aditya Kumardfa87412017-09-13 05:28:03 +000050#include "llvm/Analysis/PostDominators.h"
David Blaikie31b98d22018-06-04 21:23:21 +000051#include "llvm/Transforms/Utils/Local.h"
Sebastian Pop41774802016-07-15 13:45:20 +000052#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000053#include "llvm/IR/Argument.h"
54#include "llvm/IR/BasicBlock.h"
55#include "llvm/IR/CFG.h"
56#include "llvm/IR/Constants.h"
57#include "llvm/IR/Dominators.h"
58#include "llvm/IR/Function.h"
59#include "llvm/IR/InstrTypes.h"
60#include "llvm/IR/Instruction.h"
61#include "llvm/IR/Instructions.h"
Reid Kleckner0e8c4bb2017-09-07 23:27:44 +000062#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000063#include "llvm/IR/Intrinsics.h"
64#include "llvm/IR/LLVMContext.h"
65#include "llvm/IR/PassManager.h"
66#include "llvm/IR/Use.h"
67#include "llvm/IR/User.h"
68#include "llvm/IR/Value.h"
69#include "llvm/Pass.h"
70#include "llvm/Support/Casting.h"
71#include "llvm/Support/CommandLine.h"
72#include "llvm/Support/Debug.h"
73#include "llvm/Support/raw_ostream.h"
Sebastian Pop41774802016-07-15 13:45:20 +000074#include "llvm/Transforms/Scalar.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000075#include "llvm/Transforms/Scalar/GVN.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000076#include <algorithm>
77#include <cassert>
78#include <iterator>
79#include <memory>
80#include <utility>
81#include <vector>
Aditya Kumardfa87412017-09-13 05:28:03 +000082
Sebastian Pop41774802016-07-15 13:45:20 +000083using namespace llvm;
84
85#define DEBUG_TYPE "gvn-hoist"
86
87STATISTIC(NumHoisted, "Number of instructions hoisted");
88STATISTIC(NumRemoved, "Number of instructions removed");
89STATISTIC(NumLoadsHoisted, "Number of loads hoisted");
90STATISTIC(NumLoadsRemoved, "Number of loads removed");
91STATISTIC(NumStoresHoisted, "Number of stores hoisted");
92STATISTIC(NumStoresRemoved, "Number of stores removed");
93STATISTIC(NumCallsHoisted, "Number of calls hoisted");
94STATISTIC(NumCallsRemoved, "Number of calls removed");
95
96static 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 Zelenko3b879392017-10-13 21:17:07 +0000100
Sebastian Pop41774802016-07-15 13:45:20 +0000101static 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 Pop38422b12016-07-26 00:15:08 +0000106static 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 Pop5ba9f242016-10-13 01:39:10 +0000111static 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 Pop2aadad72016-08-03 20:54:38 +0000115
Daniel Berlindcb004f2017-03-02 23:06:46 +0000116namespace llvm {
Sebastian Pop41774802016-07-15 13:45:20 +0000117
Eugene Zelenko3b879392017-10-13 21:17:07 +0000118using BBSideEffectsSet = DenseMap<const BasicBlock *, bool>;
119using SmallVecInsn = SmallVector<Instruction *, 4>;
120using SmallVecImplInsn = SmallVectorImpl<Instruction *>;
121
Aditya Kumardfa87412017-09-13 05:28:03 +0000122// Each element of a hoisting list contains the basic block where to hoist and
123// a list of instructions to be hoisted.
Eugene Zelenko3b879392017-10-13 21:17:07 +0000124using HoistingPointInfo = std::pair<BasicBlock *, SmallVecInsn>;
125
126using HoistingPointList = SmallVector<HoistingPointInfo, 4>;
127
Aditya Kumardfa87412017-09-13 05:28:03 +0000128// A map from a pair of VNs to all the instructions with those VNs.
Eugene Zelenko3b879392017-10-13 21:17:07 +0000129using VNType = std::pair<unsigned, unsigned>;
130
131using VNtoInsns = DenseMap<VNType, SmallVector<Instruction *, 4>>;
Sebastian Pop41774802016-07-15 13:45:20 +0000132
Aditya Kumardfa87412017-09-13 05:28:03 +0000133// 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.
143struct CHIArg {
144 VNType VN;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000145
Aditya Kumardfa87412017-09-13 05:28:03 +0000146 // Edge destination (shows the direction of flow), may not be where the I is.
147 BasicBlock *Dest;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000148
Aditya Kumardfa87412017-09-13 05:28:03 +0000149 // The instruction (VN) which uses the values flowing out of CHI.
150 Instruction *I;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000151
Aditya Kumardfa87412017-09-13 05:28:03 +0000152 bool operator==(const CHIArg &A) { return VN == A.VN; }
153 bool operator!=(const CHIArg &A) { return !(*this == A); }
Sebastian Pop41774802016-07-15 13:45:20 +0000154};
155
Eugene Zelenko3b879392017-10-13 21:17:07 +0000156using CHIIt = SmallVectorImpl<CHIArg>::iterator;
157using CHIArgs = iterator_range<CHIIt>;
Alexandros Lamprineas484bd132018-08-28 11:07:54 +0000158using CHICache = DenseMap<BasicBlock *, SmallPtrSet<Instruction *, 4>>;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000159using OutValuesType = DenseMap<BasicBlock *, SmallVector<CHIArg, 2>>;
160using InValuesType =
161 DenseMap<BasicBlock *, SmallVector<std::pair<VNType, Instruction *>, 2>>;
Aditya Kumardfa87412017-09-13 05:28:03 +0000162
David Majnemer04c7c222016-07-18 06:11:37 +0000163// An invalid value number Used when inserting a single value number into
164// VNtoInsns.
Reid Kleckner3498ad12016-07-18 18:53:50 +0000165enum : unsigned { InvalidVN = ~2U };
Sebastian Pop41774802016-07-15 13:45:20 +0000166
167// Records all scalar instructions candidate for code hoisting.
168class InsnInfo {
169 VNtoInsns VNtoScalars;
170
171public:
172 // Inserts I and its value number in VNtoScalars.
173 void insert(Instruction *I, GVN::ValueTable &VN) {
174 // Scalar instruction.
175 unsigned V = VN.lookupOrAdd(I);
David Majnemer04c7c222016-07-18 06:11:37 +0000176 VNtoScalars[{V, InvalidVN}].push_back(I);
Sebastian Pop41774802016-07-15 13:45:20 +0000177 }
178
179 const VNtoInsns &getVNTable() const { return VNtoScalars; }
180};
181
182// Records all load instructions candidate for code hoisting.
183class LoadInfo {
184 VNtoInsns VNtoLoads;
185
186public:
187 // Insert Load and the value number of its memory address in VNtoLoads.
188 void insert(LoadInst *Load, GVN::ValueTable &VN) {
189 if (Load->isSimple()) {
190 unsigned V = VN.lookupOrAdd(Load->getPointerOperand());
David Majnemer04c7c222016-07-18 06:11:37 +0000191 VNtoLoads[{V, InvalidVN}].push_back(Load);
Sebastian Pop41774802016-07-15 13:45:20 +0000192 }
193 }
194
195 const VNtoInsns &getVNTable() const { return VNtoLoads; }
196};
197
198// Records all store instructions candidate for code hoisting.
199class StoreInfo {
200 VNtoInsns VNtoStores;
201
202public:
203 // Insert the Store and a hash number of the store address and the stored
204 // value in VNtoStores.
205 void insert(StoreInst *Store, GVN::ValueTable &VN) {
206 if (!Store->isSimple())
207 return;
208 // Hash the store address and the stored value.
209 Value *Ptr = Store->getPointerOperand();
210 Value *Val = Store->getValueOperand();
David Majnemer04c7c222016-07-18 06:11:37 +0000211 VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store);
Sebastian Pop41774802016-07-15 13:45:20 +0000212 }
213
214 const VNtoInsns &getVNTable() const { return VNtoStores; }
215};
216
217// Records all call instructions candidate for code hoisting.
218class CallInfo {
219 VNtoInsns VNtoCallsScalars;
220 VNtoInsns VNtoCallsLoads;
221 VNtoInsns VNtoCallsStores;
222
223public:
224 // Insert Call and its value numbering in one of the VNtoCalls* containers.
225 void insert(CallInst *Call, GVN::ValueTable &VN) {
226 // A call that doesNotAccessMemory is handled as a Scalar,
227 // onlyReadsMemory will be handled as a Load instruction,
228 // all other calls will be handled as stores.
229 unsigned V = VN.lookupOrAdd(Call);
David Majnemer04c7c222016-07-18 06:11:37 +0000230 auto Entry = std::make_pair(V, InvalidVN);
Sebastian Pop41774802016-07-15 13:45:20 +0000231
232 if (Call->doesNotAccessMemory())
David Majnemer04c7c222016-07-18 06:11:37 +0000233 VNtoCallsScalars[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000234 else if (Call->onlyReadsMemory())
David Majnemer04c7c222016-07-18 06:11:37 +0000235 VNtoCallsLoads[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000236 else
David Majnemer04c7c222016-07-18 06:11:37 +0000237 VNtoCallsStores[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000238 }
239
240 const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; }
Sebastian Pop41774802016-07-15 13:45:20 +0000241 const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; }
Sebastian Pop41774802016-07-15 13:45:20 +0000242 const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; }
243};
244
David Majnemer68623a02016-07-25 02:21:25 +0000245static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
246 static const unsigned KnownIDs[] = {
247 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
248 LLVMContext::MD_noalias, LLVMContext::MD_range,
249 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
250 LLVMContext::MD_invariant_group};
Florian Hahn406f1ff2018-08-24 11:40:04 +0000251 combineMetadata(ReplInst, I, KnownIDs, true);
David Majnemer68623a02016-07-25 02:21:25 +0000252}
253
Sebastian Pop41774802016-07-15 13:45:20 +0000254// This pass hoists common computations across branches sharing common
255// dominator. The primary goal is to reduce the code size, and in some
256// cases reduce critical path (by exposing more ILP).
257class GVNHoist {
258public:
Aditya Kumardfa87412017-09-13 05:28:03 +0000259 GVNHoist(DominatorTree *DT, PostDominatorTree *PDT, AliasAnalysis *AA,
260 MemoryDependenceResults *MD, MemorySSA *MSSA)
261 : DT(DT), PDT(PDT), AA(AA), MD(MD), MSSA(MSSA),
Eugene Zelenko3b879392017-10-13 21:17:07 +0000262 MSSAUpdater(llvm::make_unique<MemorySSAUpdater>(MSSA)) {}
Aditya Kumar07cb3042016-11-29 14:34:01 +0000263
Daniel Berlin65af45d2016-07-25 17:24:22 +0000264 bool run(Function &F) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000265 NumFuncArgs = F.arg_size();
Daniel Berlin65af45d2016-07-25 17:24:22 +0000266 VN.setDomTree(DT);
267 VN.setAliasAnalysis(AA);
268 VN.setMemDep(MD);
269 bool Res = false;
Sebastian Pop4ba7c882016-08-03 20:54:36 +0000270 // Perform DFS Numbering of instructions.
271 unsigned BBI = 0;
272 for (const BasicBlock *BB : depth_first(&F.getEntryBlock())) {
273 DFSNumber[BB] = ++BBI;
274 unsigned I = 0;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000275 for (auto &Inst : *BB)
Sebastian Pop4ba7c882016-08-03 20:54:36 +0000276 DFSNumber[&Inst] = ++I;
277 }
Daniel Berlin65af45d2016-07-25 17:24:22 +0000278
Sebastian Pop2aadad72016-08-03 20:54:38 +0000279 int ChainLength = 0;
280
Daniel Berlin65af45d2016-07-25 17:24:22 +0000281 // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
Eugene Zelenko3b879392017-10-13 21:17:07 +0000282 while (true) {
Sebastian Pop2aadad72016-08-03 20:54:38 +0000283 if (MaxChainLength != -1 && ++ChainLength >= MaxChainLength)
284 return Res;
285
Daniel Berlin65af45d2016-07-25 17:24:22 +0000286 auto HoistStat = hoistExpressions(F);
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000287 if (HoistStat.first + HoistStat.second == 0)
Daniel Berlin65af45d2016-07-25 17:24:22 +0000288 return Res;
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000289
290 if (HoistStat.second > 0)
Daniel Berlin65af45d2016-07-25 17:24:22 +0000291 // To address a limitation of the current GVN, we need to rerun the
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000292 // hoisting after we hoisted loads or stores in order to be able to
293 // hoist all scalars dependent on the hoisted ld/st.
Daniel Berlin65af45d2016-07-25 17:24:22 +0000294 VN.clear();
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000295
Daniel Berlin65af45d2016-07-25 17:24:22 +0000296 Res = true;
297 }
298
299 return Res;
300 }
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000301
Aditya Kumardfa87412017-09-13 05:28:03 +0000302 // Copied from NewGVN.cpp
303 // This function provides global ranking of operations so that we can place
304 // them in a canonical order. Note that rank alone is not necessarily enough
305 // for a complete ordering, as constants all have the same rank. However,
306 // generally, we will simplify an operation with all constants so that it
307 // doesn't matter what order they appear in.
308 unsigned int rank(const Value *V) const {
309 // Prefer constants to undef to anything else
310 // Undef is a constant, have to check it first.
311 // Prefer smaller constants to constantexprs
312 if (isa<ConstantExpr>(V))
313 return 2;
314 if (isa<UndefValue>(V))
315 return 1;
316 if (isa<Constant>(V))
317 return 0;
318 else if (auto *A = dyn_cast<Argument>(V))
319 return 3 + A->getArgNo();
320
321 // Need to shift the instruction DFS by number of arguments + 3 to account
322 // for the constant and argument ranking above.
323 auto Result = DFSNumber.lookup(V);
324 if (Result > 0)
325 return 4 + NumFuncArgs + Result;
326 // Unreachable or something else, just return a really large number.
327 return ~0;
328 }
329
Daniel Berlin65af45d2016-07-25 17:24:22 +0000330private:
Sebastian Pop41774802016-07-15 13:45:20 +0000331 GVN::ValueTable VN;
332 DominatorTree *DT;
Aditya Kumardfa87412017-09-13 05:28:03 +0000333 PostDominatorTree *PDT;
Sebastian Pop41774802016-07-15 13:45:20 +0000334 AliasAnalysis *AA;
335 MemoryDependenceResults *MD;
Daniel Berlinea02eee2016-08-23 05:42:41 +0000336 MemorySSA *MSSA;
Daniel Berlin17e8d0e2017-02-22 22:19:55 +0000337 std::unique_ptr<MemorySSAUpdater> MSSAUpdater;
Sebastian Pop91d4a302016-07-26 00:15:10 +0000338 DenseMap<const Value *, unsigned> DFSNumber;
Sebastian Pop41774802016-07-15 13:45:20 +0000339 BBSideEffectsSet BBSideEffects;
Aditya Kumardfa87412017-09-13 05:28:03 +0000340 DenseSet<const BasicBlock *> HoistBarrier;
Aditya Kumardfa87412017-09-13 05:28:03 +0000341 SmallVector<BasicBlock *, 32> IDFBlocks;
342 unsigned NumFuncArgs;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000343 const bool HoistingGeps = false;
David Majnemeraa241782016-07-18 00:35:01 +0000344
Sebastian Pop41774802016-07-15 13:45:20 +0000345 enum InsKind { Unknown, Scalar, Load, Store };
346
Sebastian Pop41774802016-07-15 13:45:20 +0000347 // Return true when there are exception handling in BB.
348 bool hasEH(const BasicBlock *BB) {
349 auto It = BBSideEffects.find(BB);
350 if (It != BBSideEffects.end())
351 return It->second;
352
353 if (BB->isEHPad() || BB->hasAddressTaken()) {
354 BBSideEffects[BB] = true;
355 return true;
356 }
357
358 if (BB->getTerminator()->mayThrow()) {
359 BBSideEffects[BB] = true;
360 return true;
361 }
362
363 BBSideEffects[BB] = false;
364 return false;
365 }
366
Sebastian Pop5f0d0e62016-08-25 11:55:47 +0000367 // Return true when a successor of BB dominates A.
368 bool successorDominate(const BasicBlock *BB, const BasicBlock *A) {
Chandler Carruth96fc1de2018-08-26 08:41:15 +0000369 for (const BasicBlock *Succ : successors(BB))
Sebastian Pop5f0d0e62016-08-25 11:55:47 +0000370 if (DT->dominates(Succ, A))
371 return true;
Sebastian Pop41774802016-07-15 13:45:20 +0000372
Sebastian Pop5f0d0e62016-08-25 11:55:47 +0000373 return false;
374 }
375
Eugene Zelenko3b879392017-10-13 21:17:07 +0000376 // Return true when I1 appears before I2 in the instructions of BB.
Sebastian Pop91d4a302016-07-26 00:15:10 +0000377 bool firstInBB(const Instruction *I1, const Instruction *I2) {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000378 assert(I1->getParent() == I2->getParent());
Sebastian Pop91d4a302016-07-26 00:15:10 +0000379 unsigned I1DFS = DFSNumber.lookup(I1);
380 unsigned I2DFS = DFSNumber.lookup(I2);
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000381 assert(I1DFS && I2DFS);
Sebastian Pop91d4a302016-07-26 00:15:10 +0000382 return I1DFS < I2DFS;
Daniel Berlin40765a62016-07-25 18:19:49 +0000383 }
Sebastian Pop91d4a302016-07-26 00:15:10 +0000384
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000385 // Return true when there are memory uses of Def in BB.
386 bool hasMemoryUse(const Instruction *NewPt, MemoryDef *Def,
387 const BasicBlock *BB) {
388 const MemorySSA::AccessList *Acc = MSSA->getBlockAccesses(BB);
389 if (!Acc)
390 return false;
391
392 Instruction *OldPt = Def->getMemoryInst();
Sebastian Pop41774802016-07-15 13:45:20 +0000393 const BasicBlock *OldBB = OldPt->getParent();
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000394 const BasicBlock *NewBB = NewPt->getParent();
395 bool ReachedNewPt = false;
Sebastian Pop41774802016-07-15 13:45:20 +0000396
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000397 for (const MemoryAccess &MA : *Acc)
398 if (const MemoryUse *MU = dyn_cast<MemoryUse>(&MA)) {
399 Instruction *Insn = MU->getMemoryInst();
Sebastian Pop1531f302016-09-22 17:22:58 +0000400
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000401 // Do not check whether MU aliases Def when MU occurs after OldPt.
402 if (BB == OldBB && firstInBB(OldPt, Insn))
403 break;
404
405 // Do not check whether MU aliases Def when MU occurs before NewPt.
406 if (BB == NewBB) {
407 if (!ReachedNewPt) {
408 if (firstInBB(Insn, NewPt))
409 continue;
410 ReachedNewPt = true;
411 }
Sebastian Pop41774802016-07-15 13:45:20 +0000412 }
Daniel Berlindcb004f2017-03-02 23:06:46 +0000413 if (MemorySSAUtil::defClobbersUseOrDef(Def, MU, *AA))
Hans Wennborgc7957ef2016-09-22 21:20:53 +0000414 return true;
415 }
Sebastian Pop8e6e3312016-09-22 15:33:51 +0000416
Sebastian Pop41774802016-07-15 13:45:20 +0000417 return false;
418 }
419
Davide Italiano32504cf2017-09-05 20:49:41 +0000420 bool hasEHhelper(const BasicBlock *BB, const BasicBlock *SrcBB,
421 int &NBBsOnAllPaths) {
422 // Stop walk once the limit is reached.
423 if (NBBsOnAllPaths == 0)
424 return true;
425
426 // Impossible to hoist with exceptions on the path.
427 if (hasEH(BB))
428 return true;
429
430 // No such instruction after HoistBarrier in a basic block was
431 // selected for hoisting so instructions selected within basic block with
432 // a hoist barrier can be hoisted.
433 if ((BB != SrcBB) && HoistBarrier.count(BB))
434 return true;
435
436 return false;
437 }
438
Sebastian Pop41774802016-07-15 13:45:20 +0000439 // Return true when there are exception handling or loads of memory Def
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000440 // between Def and NewPt. This function is only called for stores: Def is
441 // the MemoryDef of the store to be hoisted.
Sebastian Pop41774802016-07-15 13:45:20 +0000442
443 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
444 // return true when the counter NBBsOnAllPaths reaces 0, except when it is
445 // initialized to -1 which is unlimited.
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000446 bool hasEHOrLoadsOnPath(const Instruction *NewPt, MemoryDef *Def,
447 int &NBBsOnAllPaths) {
Sebastian Pop41774802016-07-15 13:45:20 +0000448 const BasicBlock *NewBB = NewPt->getParent();
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000449 const BasicBlock *OldBB = Def->getBlock();
Sebastian Pop41774802016-07-15 13:45:20 +0000450 assert(DT->dominates(NewBB, OldBB) && "invalid path");
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000451 assert(DT->dominates(Def->getDefiningAccess()->getBlock(), NewBB) &&
Sebastian Pop41774802016-07-15 13:45:20 +0000452 "def does not dominate new hoisting point");
453
454 // Walk all basic blocks reachable in depth-first iteration on the inverse
455 // CFG from OldBB to NewBB. These blocks are all the blocks that may be
456 // executed between the execution of NewBB and OldBB. Hoisting an expression
457 // from OldBB into NewBB has to be safe on all execution paths.
458 for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
Geoff Berry635e5052017-04-10 20:45:17 +0000459 const BasicBlock *BB = *I;
460 if (BB == NewBB) {
Sebastian Pop41774802016-07-15 13:45:20 +0000461 // Stop traversal when reaching HoistPt.
462 I.skipChildren();
463 continue;
464 }
465
Davide Italiano32504cf2017-09-05 20:49:41 +0000466 if (hasEHhelper(BB, OldBB, NBBsOnAllPaths))
Sebastian Pop41774802016-07-15 13:45:20 +0000467 return true;
468
469 // Check that we do not move a store past loads.
Geoff Berry635e5052017-04-10 20:45:17 +0000470 if (hasMemoryUse(NewPt, Def, BB))
Sebastian Pop41774802016-07-15 13:45:20 +0000471 return true;
472
Sebastian Pop41774802016-07-15 13:45:20 +0000473 // -1 is unlimited number of blocks on all paths.
474 if (NBBsOnAllPaths != -1)
475 --NBBsOnAllPaths;
476
477 ++I;
478 }
479
480 return false;
481 }
482
483 // Return true when there are exception handling between HoistPt and BB.
484 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
485 // return true when the counter NBBsOnAllPaths reaches 0, except when it is
486 // initialized to -1 which is unlimited.
Geoff Berry635e5052017-04-10 20:45:17 +0000487 bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *SrcBB,
Sebastian Pop41774802016-07-15 13:45:20 +0000488 int &NBBsOnAllPaths) {
Geoff Berry635e5052017-04-10 20:45:17 +0000489 assert(DT->dominates(HoistPt, SrcBB) && "Invalid path");
Sebastian Pop41774802016-07-15 13:45:20 +0000490
491 // Walk all basic blocks reachable in depth-first iteration on
492 // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
493 // blocks that may be executed between the execution of NewHoistPt and
494 // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
495 // on all execution paths.
Geoff Berry635e5052017-04-10 20:45:17 +0000496 for (auto I = idf_begin(SrcBB), E = idf_end(SrcBB); I != E;) {
497 const BasicBlock *BB = *I;
498 if (BB == HoistPt) {
Sebastian Pop41774802016-07-15 13:45:20 +0000499 // Stop traversal when reaching NewHoistPt.
500 I.skipChildren();
501 continue;
502 }
503
Davide Italiano32504cf2017-09-05 20:49:41 +0000504 if (hasEHhelper(BB, SrcBB, NBBsOnAllPaths))
Aditya Kumar314ebe02016-11-29 14:36:27 +0000505 return true;
506
Sebastian Pop41774802016-07-15 13:45:20 +0000507 // -1 is unlimited number of blocks on all paths.
508 if (NBBsOnAllPaths != -1)
509 --NBBsOnAllPaths;
510
511 ++I;
512 }
513
514 return false;
515 }
516
517 // Return true when it is safe to hoist a memory load or store U from OldPt
518 // to NewPt.
519 bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
520 MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) {
Sebastian Pop41774802016-07-15 13:45:20 +0000521 // In place hoisting is safe.
522 if (NewPt == OldPt)
523 return true;
524
525 const BasicBlock *NewBB = NewPt->getParent();
526 const BasicBlock *OldBB = OldPt->getParent();
527 const BasicBlock *UBB = U->getBlock();
528
529 // Check for dependences on the Memory SSA.
530 MemoryAccess *D = U->getDefiningAccess();
531 BasicBlock *DBB = D->getBlock();
532 if (DT->properlyDominates(NewBB, DBB))
533 // Cannot move the load or store to NewBB above its definition in DBB.
534 return false;
535
536 if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
David Majnemer4c66a712016-07-18 00:34:58 +0000537 if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
Alexandros Lamprineas592cc782018-07-23 09:42:35 +0000538 if (!firstInBB(UD->getMemoryInst(), NewPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000539 // Cannot move the load or store to NewPt above its definition in D.
540 return false;
541
542 // Check for unsafe hoistings due to side effects.
543 if (K == InsKind::Store) {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000544 if (hasEHOrLoadsOnPath(NewPt, dyn_cast<MemoryDef>(U), NBBsOnAllPaths))
Sebastian Pop41774802016-07-15 13:45:20 +0000545 return false;
546 } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
547 return false;
548
549 if (UBB == NewBB) {
550 if (DT->properlyDominates(DBB, NewBB))
551 return true;
552 assert(UBB == DBB);
553 assert(MSSA->locallyDominates(D, U));
554 }
555
556 // No side effects: it is safe to hoist.
557 return true;
558 }
559
Sebastian Pop5f0d0e62016-08-25 11:55:47 +0000560 // Return true when it is safe to hoist scalar instructions from all blocks in
561 // WL to HoistBB.
Aditya Kumardfa87412017-09-13 05:28:03 +0000562 bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB,
Sebastian Pop5f0d0e62016-08-25 11:55:47 +0000563 int &NBBsOnAllPaths) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000564 return !hasEHOnPath(HoistBB, BB, NBBsOnAllPaths);
565 }
Sebastian Pop41774802016-07-15 13:45:20 +0000566
Aditya Kumardfa87412017-09-13 05:28:03 +0000567 // In the inverse CFG, the dominance frontier of basic block (BB) is the
568 // point where ANTIC needs to be computed for instructions which are going
569 // to be hoisted. Since this point does not change during gvn-hoist,
570 // we compute it only once (on demand).
571 // The ides is inspired from:
572 // "Partial Redundancy Elimination in SSA Form"
573 // ROBERT KENNEDY, SUN CHAN, SHIN-MING LIU, RAYMOND LO, PENG TU and FRED CHOW
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000574 // They use similar idea in the forward graph to find fully redundant and
Aditya Kumardfa87412017-09-13 05:28:03 +0000575 // partially redundant expressions, here it is used in the inverse graph to
576 // find fully anticipable instructions at merge point (post-dominator in
577 // the inverse CFG).
578 // Returns the edge via which an instruction in BB will get the values from.
579
580 // Returns true when the values are flowing out to each edge.
581 bool valueAnticipable(CHIArgs C, TerminatorInst *TI) const {
Vedant Kumar5a0872c2018-05-16 23:20:42 +0000582 if (TI->getNumSuccessors() > (unsigned)size(C))
Aditya Kumardfa87412017-09-13 05:28:03 +0000583 return false; // Not enough args in this CHI.
584
585 for (auto CHI : C) {
586 BasicBlock *Dest = CHI.Dest;
587 // Find if all the edges have values flowing out of BB.
Chandler Carruth96fc1de2018-08-26 08:41:15 +0000588 bool Found = llvm::any_of(
589 successors(TI), [Dest](const BasicBlock *BB) { return BB == Dest; });
Aditya Kumardfa87412017-09-13 05:28:03 +0000590 if (!Found)
Sebastian Pop5f0d0e62016-08-25 11:55:47 +0000591 return false;
Aditya Kumardfa87412017-09-13 05:28:03 +0000592 }
Sebastian Pop41774802016-07-15 13:45:20 +0000593 return true;
594 }
595
Aditya Kumardfa87412017-09-13 05:28:03 +0000596 // Check if it is safe to hoist values tracked by CHI in the range
597 // [Begin, End) and accumulate them in Safe.
598 void checkSafety(CHIArgs C, BasicBlock *BB, InsKind K,
599 SmallVectorImpl<CHIArg> &Safe) {
Aditya Kumar314ebe02016-11-29 14:36:27 +0000600 int NumBBsOnAllPaths = MaxNumberOfBBSInPath;
Aditya Kumardfa87412017-09-13 05:28:03 +0000601 for (auto CHI : C) {
602 Instruction *Insn = CHI.I;
603 if (!Insn) // No instruction was inserted in this CHI.
604 continue;
Sebastian Pop41774802016-07-15 13:45:20 +0000605 if (K == InsKind::Scalar) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000606 if (safeToHoistScalar(BB, Insn->getParent(), NumBBsOnAllPaths))
607 Safe.push_back(CHI);
Sebastian Pop41774802016-07-15 13:45:20 +0000608 } else {
Aditya Kumardfa87412017-09-13 05:28:03 +0000609 MemoryUseOrDef *UD = MSSA->getMemoryAccess(Insn);
610 if (safeToHoistLdSt(BB->getTerminator(), Insn, UD, K, NumBBsOnAllPaths))
611 Safe.push_back(CHI);
Sebastian Pop41774802016-07-15 13:45:20 +0000612 }
Sebastian Pop41774802016-07-15 13:45:20 +0000613 }
Sebastian Pop41774802016-07-15 13:45:20 +0000614 }
615
Eugene Zelenko3b879392017-10-13 21:17:07 +0000616 using RenameStackType = DenseMap<VNType, SmallVector<Instruction *, 2>>;
617
Aditya Kumardfa87412017-09-13 05:28:03 +0000618 // Push all the VNs corresponding to BB into RenameStack.
619 void fillRenameStack(BasicBlock *BB, InValuesType &ValueBBs,
620 RenameStackType &RenameStack) {
621 auto it1 = ValueBBs.find(BB);
622 if (it1 != ValueBBs.end()) {
623 // Iterate in reverse order to keep lower ranked values on the top.
624 for (std::pair<VNType, Instruction *> &VI : reverse(it1->second)) {
625 // Get the value of instruction I
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000626 LLVM_DEBUG(dbgs() << "\nPushing on stack: " << *VI.second);
Aditya Kumardfa87412017-09-13 05:28:03 +0000627 RenameStack[VI.first].push_back(VI.second);
628 }
629 }
630 }
Sebastian Pop41774802016-07-15 13:45:20 +0000631
Aditya Kumardfa87412017-09-13 05:28:03 +0000632 void fillChiArgs(BasicBlock *BB, OutValuesType &CHIBBs,
633 RenameStackType &RenameStack) {
634 // For each *predecessor* (because Post-DOM) of BB check if it has a CHI
635 for (auto Pred : predecessors(BB)) {
636 auto P = CHIBBs.find(Pred);
637 if (P == CHIBBs.end()) {
638 continue;
639 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000640 LLVM_DEBUG(dbgs() << "\nLooking at CHIs in: " << Pred->getName(););
Aditya Kumardfa87412017-09-13 05:28:03 +0000641 // A CHI is found (BB -> Pred is an edge in the CFG)
642 // Pop the stack until Top(V) = Ve.
643 auto &VCHI = P->second;
644 for (auto It = VCHI.begin(), E = VCHI.end(); It != E;) {
645 CHIArg &C = *It;
646 if (!C.Dest) {
647 auto si = RenameStack.find(C.VN);
648 // The Basic Block where CHI is must dominate the value we want to
649 // track in a CHI. In the PDom walk, there can be values in the
650 // stack which are not control dependent e.g., nested loop.
651 if (si != RenameStack.end() && si->second.size() &&
Aditya Kumar1f90cae2018-01-04 07:47:24 +0000652 DT->properlyDominates(Pred, si->second.back()->getParent())) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000653 C.Dest = BB; // Assign the edge
654 C.I = si->second.pop_back_val(); // Assign the argument
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000655 LLVM_DEBUG(dbgs()
656 << "\nCHI Inserted in BB: " << C.Dest->getName() << *C.I
657 << ", VN: " << C.VN.first << ", " << C.VN.second);
Aditya Kumardfa87412017-09-13 05:28:03 +0000658 }
659 // Move to next CHI of a different value
660 It = std::find_if(It, VCHI.end(),
661 [It](CHIArg &A) { return A != *It; });
662 } else
663 ++It;
664 }
665 }
666 }
667
668 // Walk the post-dominator tree top-down and use a stack for each value to
669 // store the last value you see. When you hit a CHI from a given edge, the
670 // value to use as the argument is at the top of the stack, add the value to
671 // CHI and pop.
672 void insertCHI(InValuesType &ValueBBs, OutValuesType &CHIBBs) {
673 auto Root = PDT->getNode(nullptr);
674 if (!Root)
675 return;
676 // Depth first walk on PDom tree to fill the CHIargs at each PDF.
677 RenameStackType RenameStack;
678 for (auto Node : depth_first(Root)) {
679 BasicBlock *BB = Node->getBlock();
680 if (!BB)
Sebastian Pop41774802016-07-15 13:45:20 +0000681 continue;
682
Aditya Kumardfa87412017-09-13 05:28:03 +0000683 // Collect all values in BB and push to stack.
684 fillRenameStack(BB, ValueBBs, RenameStack);
Sebastian Pop41774802016-07-15 13:45:20 +0000685
Aditya Kumardfa87412017-09-13 05:28:03 +0000686 // Fill outgoing values in each CHI corresponding to BB.
687 fillChiArgs(BB, CHIBBs, RenameStack);
Sebastian Pop41774802016-07-15 13:45:20 +0000688 }
689 }
690
Aditya Kumardfa87412017-09-13 05:28:03 +0000691 // Walk all the CHI-nodes to find ones which have a empty-entry and remove
692 // them Then collect all the instructions which are safe to hoist and see if
693 // they form a list of anticipable values. OutValues contains CHIs
694 // corresponding to each basic block.
695 void findHoistableCandidates(OutValuesType &CHIBBs, InsKind K,
696 HoistingPointList &HPL) {
697 auto cmpVN = [](const CHIArg &A, const CHIArg &B) { return A.VN < B.VN; };
698
699 // CHIArgs now have the outgoing values, so check for anticipability and
700 // accumulate hoistable candidates in HPL.
701 for (std::pair<BasicBlock *, SmallVector<CHIArg, 2>> &A : CHIBBs) {
702 BasicBlock *BB = A.first;
703 SmallVectorImpl<CHIArg> &CHIs = A.second;
704 // Vector of PHIs contains PHIs for different instructions.
705 // Sort the args according to their VNs, such that identical
706 // instructions are together.
Mandeep Singh Grangf83268b2017-10-30 19:42:41 +0000707 std::stable_sort(CHIs.begin(), CHIs.end(), cmpVN);
Aditya Kumardfa87412017-09-13 05:28:03 +0000708 auto TI = BB->getTerminator();
709 auto B = CHIs.begin();
710 // [PreIt, PHIIt) form a range of CHIs which have identical VNs.
711 auto PHIIt = std::find_if(CHIs.begin(), CHIs.end(),
712 [B](CHIArg &A) { return A != *B; });
713 auto PrevIt = CHIs.begin();
714 while (PrevIt != PHIIt) {
715 // Collect values which satisfy safety checks.
716 SmallVector<CHIArg, 2> Safe;
717 // We check for safety first because there might be multiple values in
718 // the same path, some of which are not safe to be hoisted, but overall
719 // each edge has at least one value which can be hoisted, making the
720 // value anticipable along that path.
721 checkSafety(make_range(PrevIt, PHIIt), BB, K, Safe);
722
723 // List of safe values should be anticipable at TI.
724 if (valueAnticipable(make_range(Safe.begin(), Safe.end()), TI)) {
725 HPL.push_back({BB, SmallVecInsn()});
726 SmallVecInsn &V = HPL.back().second;
727 for (auto B : Safe)
728 V.push_back(B.I);
729 }
730
731 // Check other VNs
732 PrevIt = PHIIt;
733 PHIIt = std::find_if(PrevIt, CHIs.end(),
734 [PrevIt](CHIArg &A) { return A != *PrevIt; });
735 }
736 }
737 }
738
739 // Compute insertion points for each values which can be fully anticipated at
740 // a dominator. HPL contains all such values.
741 void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
742 InsKind K) {
743 // Sort VNs based on their rankings
744 std::vector<VNType> Ranks;
745 for (const auto &Entry : Map) {
746 Ranks.push_back(Entry.first);
747 }
748
749 // TODO: Remove fully-redundant expressions.
750 // Get instruction from the Map, assume that all the Instructions
751 // with same VNs have same rank (this is an approximation).
Mandeep Singh Grang636d94d2018-04-13 19:47:57 +0000752 llvm::sort(Ranks.begin(), Ranks.end(),
753 [this, &Map](const VNType &r1, const VNType &r2) {
754 return (rank(*Map.lookup(r1).begin()) <
755 rank(*Map.lookup(r2).begin()));
756 });
Aditya Kumardfa87412017-09-13 05:28:03 +0000757
758 // - Sort VNs according to their rank, and start with lowest ranked VN
759 // - Take a VN and for each instruction with same VN
760 // - Find the dominance frontier in the inverse graph (PDF)
761 // - Insert the chi-node at PDF
762 // - Remove the chi-nodes with missing entries
763 // - Remove values from CHI-nodes which do not truly flow out, e.g.,
764 // modified along the path.
765 // - Collect the remaining values that are still anticipable
766 SmallVector<BasicBlock *, 2> IDFBlocks;
767 ReverseIDFCalculator IDFs(*PDT);
768 OutValuesType OutValue;
769 InValuesType InValue;
Alexandros Lamprineas484bd132018-08-28 11:07:54 +0000770 CHICache CachedCHIs;
Aditya Kumardfa87412017-09-13 05:28:03 +0000771 for (const auto &R : Ranks) {
772 const SmallVecInsn &V = Map.lookup(R);
773 if (V.size() < 2)
774 continue;
775 const VNType &VN = R;
776 SmallPtrSet<BasicBlock *, 2> VNBlocks;
777 for (auto &I : V) {
778 BasicBlock *BBI = I->getParent();
779 if (!hasEH(BBI))
780 VNBlocks.insert(BBI);
781 }
782 // Compute the Post Dominance Frontiers of each basic block
783 // The dominance frontier of a live block X in the reverse
784 // control graph is the set of blocks upon which X is control
785 // dependent. The following sequence computes the set of blocks
786 // which currently have dead terminators that are control
787 // dependence sources of a block which is in NewLiveBlocks.
788 IDFs.setDefiningBlocks(VNBlocks);
789 IDFs.calculate(IDFBlocks);
790
791 // Make a map of BB vs instructions to be hoisted.
792 for (unsigned i = 0; i < V.size(); ++i) {
793 InValue[V[i]->getParent()].push_back(std::make_pair(VN, V[i]));
794 }
795 // Insert empty CHI node for this VN. This is used to factor out
796 // basic blocks where the ANTIC can potentially change.
Alexandros Lamprineas484bd132018-08-28 11:07:54 +0000797 for (auto IDFB : IDFBlocks) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000798 for (unsigned i = 0; i < V.size(); ++i) {
799 CHIArg C = {VN, nullptr, nullptr};
Aditya Kumar49c03b12017-12-13 19:40:07 +0000800 // Ignore spurious PDFs.
Alexandros Lamprineas484bd132018-08-28 11:07:54 +0000801 if (DT->properlyDominates(IDFB, V[i]->getParent()) &&
802 CachedCHIs[IDFB].insert(V[i]).second) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000803 OutValue[IDFB].push_back(C);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000804 LLVM_DEBUG(dbgs() << "\nInsertion a CHI for BB: " << IDFB->getName()
805 << ", for Insn: " << *V[i]);
Aditya Kumardfa87412017-09-13 05:28:03 +0000806 }
807 }
808 }
809 }
810
811 // Insert CHI args at each PDF to iterate on factored graph of
812 // control dependence.
813 insertCHI(InValue, OutValue);
814 // Using the CHI args inserted at each PDF, find fully anticipable values.
815 findHoistableCandidates(OutValue, K, HPL);
816 }
817
Sebastian Pop41774802016-07-15 13:45:20 +0000818 // Return true when all operands of Instr are available at insertion point
819 // HoistPt. When limiting the number of hoisted expressions, one could hoist
820 // a load without hoisting its access function. So before hoisting any
821 // expression, make sure that all its operands are available at insert point.
822 bool allOperandsAvailable(const Instruction *I,
823 const BasicBlock *HoistPt) const {
David Majnemer4c66a712016-07-18 00:34:58 +0000824 for (const Use &Op : I->operands())
825 if (const auto *Inst = dyn_cast<Instruction>(&Op))
826 if (!DT->dominates(Inst->getParent(), HoistPt))
827 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000828
829 return true;
830 }
831
Sebastian Pop55c30072016-07-27 05:48:12 +0000832 // Same as allOperandsAvailable with recursive check for GEP operands.
833 bool allGepOperandsAvailable(const Instruction *I,
834 const BasicBlock *HoistPt) const {
835 for (const Use &Op : I->operands())
836 if (const auto *Inst = dyn_cast<Instruction>(&Op))
837 if (!DT->dominates(Inst->getParent(), HoistPt)) {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000838 if (const GetElementPtrInst *GepOp =
839 dyn_cast<GetElementPtrInst>(Inst)) {
Sebastian Pop55c30072016-07-27 05:48:12 +0000840 if (!allGepOperandsAvailable(GepOp, HoistPt))
841 return false;
842 // Gep is available if all operands of GepOp are available.
843 } else {
844 // Gep is not available if it has operands other than GEPs that are
845 // defined in blocks not dominating HoistPt.
846 return false;
847 }
848 }
849 return true;
850 }
851
852 // Make all operands of the GEP available.
853 void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
854 const SmallVecInsn &InstructionsToHoist,
855 Instruction *Gep) const {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000856 assert(allGepOperandsAvailable(Gep, HoistPt) &&
857 "GEP operands not available");
Sebastian Pop55c30072016-07-27 05:48:12 +0000858
859 Instruction *ClonedGep = Gep->clone();
860 for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i)
861 if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) {
Sebastian Pop55c30072016-07-27 05:48:12 +0000862 // Check whether the operand is already available.
863 if (DT->dominates(Op->getParent(), HoistPt))
864 continue;
865
866 // As a GEP can refer to other GEPs, recursively make all the operands
867 // of this GEP available at HoistPt.
868 if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op))
869 makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp);
870 }
871
872 // Copy Gep and replace its uses in Repl with ClonedGep.
873 ClonedGep->insertBefore(HoistPt->getTerminator());
874
875 // Conservatively discard any optimization hints, they may differ on the
876 // other paths.
877 ClonedGep->dropUnknownNonDebugMetadata();
878
879 // If we have optimization hints which agree with each other along different
880 // paths, preserve them.
881 for (const Instruction *OtherInst : InstructionsToHoist) {
882 const GetElementPtrInst *OtherGep;
883 if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
884 OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
885 else
886 OtherGep = cast<GetElementPtrInst>(
887 cast<StoreInst>(OtherInst)->getPointerOperand());
Peter Collingbourne8f1dd5c2016-09-07 23:39:04 +0000888 ClonedGep->andIRFlags(OtherGep);
Sebastian Pop55c30072016-07-27 05:48:12 +0000889 }
890
891 // Replace uses of Gep with ClonedGep in Repl.
892 Repl->replaceUsesOfWith(Gep, ClonedGep);
893 }
894
Aditya Kumardfa87412017-09-13 05:28:03 +0000895 void updateAlignment(Instruction *I, Instruction *Repl) {
896 if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
897 ReplacementLoad->setAlignment(
898 std::min(ReplacementLoad->getAlignment(),
899 cast<LoadInst>(I)->getAlignment()));
900 ++NumLoadsRemoved;
901 } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
902 ReplacementStore->setAlignment(
903 std::min(ReplacementStore->getAlignment(),
904 cast<StoreInst>(I)->getAlignment()));
905 ++NumStoresRemoved;
906 } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
907 ReplacementAlloca->setAlignment(
908 std::max(ReplacementAlloca->getAlignment(),
909 cast<AllocaInst>(I)->getAlignment()));
910 } else if (isa<CallInst>(Repl)) {
911 ++NumCallsRemoved;
912 }
913 }
914
915 // Remove all the instructions in Candidates and replace their usage with Repl.
916 // Returns the number of instructions removed.
917 unsigned rauw(const SmallVecInsn &Candidates, Instruction *Repl,
918 MemoryUseOrDef *NewMemAcc) {
919 unsigned NR = 0;
920 for (Instruction *I : Candidates) {
921 if (I != Repl) {
922 ++NR;
923 updateAlignment(I, Repl);
924 if (NewMemAcc) {
925 // Update the uses of the old MSSA access with NewMemAcc.
926 MemoryAccess *OldMA = MSSA->getMemoryAccess(I);
927 OldMA->replaceAllUsesWith(NewMemAcc);
928 MSSAUpdater->removeMemoryAccess(OldMA);
929 }
930
931 Repl->andIRFlags(I);
932 combineKnownMetadata(Repl, I);
933 I->replaceAllUsesWith(Repl);
934 // Also invalidate the Alias Analysis cache.
935 MD->removeInstruction(I);
936 I->eraseFromParent();
937 }
938 }
939 return NR;
940 }
941
942 // Replace all Memory PHI usage with NewMemAcc.
943 void raMPHIuw(MemoryUseOrDef *NewMemAcc) {
944 SmallPtrSet<MemoryPhi *, 4> UsePhis;
945 for (User *U : NewMemAcc->users())
946 if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(U))
947 UsePhis.insert(Phi);
948
949 for (MemoryPhi *Phi : UsePhis) {
950 auto In = Phi->incoming_values();
Eugene Zelenko3b879392017-10-13 21:17:07 +0000951 if (llvm::all_of(In, [&](Use &U) { return U == NewMemAcc; })) {
Aditya Kumardfa87412017-09-13 05:28:03 +0000952 Phi->replaceAllUsesWith(NewMemAcc);
953 MSSAUpdater->removeMemoryAccess(Phi);
954 }
955 }
956 }
957
958 // Remove all other instructions and replace them with Repl.
959 unsigned removeAndReplace(const SmallVecInsn &Candidates, Instruction *Repl,
960 BasicBlock *DestBB, bool MoveAccess) {
961 MemoryUseOrDef *NewMemAcc = MSSA->getMemoryAccess(Repl);
962 if (MoveAccess && NewMemAcc) {
963 // The definition of this ld/st will not change: ld/st hoisting is
964 // legal when the ld/st is not moved past its current definition.
965 MSSAUpdater->moveToPlace(NewMemAcc, DestBB, MemorySSA::End);
966 }
967
968 // Replace all other instructions with Repl with memory access NewMemAcc.
969 unsigned NR = rauw(Candidates, Repl, NewMemAcc);
970
971 // Remove MemorySSA phi nodes with the same arguments.
972 if (NewMemAcc)
973 raMPHIuw(NewMemAcc);
974 return NR;
975 }
976
Sebastian Pop55c30072016-07-27 05:48:12 +0000977 // In the case Repl is a load or a store, we make all their GEPs
978 // available: GEPs are not hoisted by default to avoid the address
979 // computations to be hoisted without the associated load or store.
980 bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
981 const SmallVecInsn &InstructionsToHoist) const {
Sebastian Pop41774802016-07-15 13:45:20 +0000982 // Check whether the GEP of a ld/st can be synthesized at HoistPt.
David Majnemerbd210122016-07-20 21:05:01 +0000983 GetElementPtrInst *Gep = nullptr;
Sebastian Pop41774802016-07-15 13:45:20 +0000984 Instruction *Val = nullptr;
Sebastian Pop55c30072016-07-27 05:48:12 +0000985 if (auto *Ld = dyn_cast<LoadInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000986 Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
Sebastian Pop55c30072016-07-27 05:48:12 +0000987 } else if (auto *St = dyn_cast<StoreInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000988 Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
Sebastian Pop41774802016-07-15 13:45:20 +0000989 Val = dyn_cast<Instruction>(St->getValueOperand());
Sebastian Pop31fd5062016-07-21 23:22:10 +0000990 // Check that the stored value is available.
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000991 if (Val) {
992 if (isa<GetElementPtrInst>(Val)) {
993 // Check whether we can compute the GEP at HoistPt.
Sebastian Pop55c30072016-07-27 05:48:12 +0000994 if (!allGepOperandsAvailable(Val, HoistPt))
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000995 return false;
996 } else if (!DT->dominates(Val->getParent(), HoistPt))
997 return false;
998 }
Sebastian Pop41774802016-07-15 13:45:20 +0000999 }
1000
Sebastian Pop41774802016-07-15 13:45:20 +00001001 // Check whether we can compute the Gep at HoistPt.
Sebastian Pop55c30072016-07-27 05:48:12 +00001002 if (!Gep || !allGepOperandsAvailable(Gep, HoistPt))
Sebastian Pop41774802016-07-15 13:45:20 +00001003 return false;
1004
Sebastian Pop55c30072016-07-27 05:48:12 +00001005 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep);
Sebastian Pop41774802016-07-15 13:45:20 +00001006
Sebastian Pop55c30072016-07-27 05:48:12 +00001007 if (Val && isa<GetElementPtrInst>(Val))
1008 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val);
Sebastian Pop41774802016-07-15 13:45:20 +00001009
1010 return true;
1011 }
1012
1013 std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
1014 unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
1015 for (const HoistingPointInfo &HP : HPL) {
1016 // Find out whether we already have one of the instructions in HoistPt,
1017 // in which case we do not have to move it.
Aditya Kumardfa87412017-09-13 05:28:03 +00001018 BasicBlock *DestBB = HP.first;
Sebastian Pop41774802016-07-15 13:45:20 +00001019 const SmallVecInsn &InstructionsToHoist = HP.second;
1020 Instruction *Repl = nullptr;
1021 for (Instruction *I : InstructionsToHoist)
Aditya Kumardfa87412017-09-13 05:28:03 +00001022 if (I->getParent() == DestBB)
Sebastian Pop41774802016-07-15 13:45:20 +00001023 // If there are two instructions in HoistPt to be hoisted in place:
1024 // update Repl to be the first one, such that we can rename the uses
1025 // of the second based on the first.
Sebastian Pop586d3ea2016-07-27 05:13:52 +00001026 if (!Repl || firstInBB(I, Repl))
1027 Repl = I;
Sebastian Pop41774802016-07-15 13:45:20 +00001028
Daniel Berlinf75fd1b2016-08-11 20:32:43 +00001029 // Keep track of whether we moved the instruction so we know whether we
1030 // should move the MemoryAccess.
1031 bool MoveAccess = true;
Sebastian Pop41774802016-07-15 13:45:20 +00001032 if (Repl) {
1033 // Repl is already in HoistPt: it remains in place.
Aditya Kumardfa87412017-09-13 05:28:03 +00001034 assert(allOperandsAvailable(Repl, DestBB) &&
Sebastian Pop41774802016-07-15 13:45:20 +00001035 "instruction depends on operands that are not available");
Daniel Berlinf75fd1b2016-08-11 20:32:43 +00001036 MoveAccess = false;
Sebastian Pop41774802016-07-15 13:45:20 +00001037 } else {
1038 // When we do not find Repl in HoistPt, select the first in the list
1039 // and move it to HoistPt.
1040 Repl = InstructionsToHoist.front();
1041
1042 // We can move Repl in HoistPt only when all operands are available.
1043 // The order in which hoistings are done may influence the availability
1044 // of operands.
Aditya Kumardfa87412017-09-13 05:28:03 +00001045 if (!allOperandsAvailable(Repl, DestBB)) {
Sebastian Pop429740a2016-08-04 23:49:05 +00001046 // When HoistingGeps there is nothing more we can do to make the
1047 // operands available: just continue.
1048 if (HoistingGeps)
1049 continue;
1050
1051 // When not HoistingGeps we need to copy the GEPs.
Aditya Kumardfa87412017-09-13 05:28:03 +00001052 if (!makeGepOperandsAvailable(Repl, DestBB, InstructionsToHoist))
Sebastian Pop429740a2016-08-04 23:49:05 +00001053 continue;
1054 }
Sebastian Pop55c30072016-07-27 05:48:12 +00001055
Sebastian Pop5d3822f2016-08-03 20:54:33 +00001056 // Move the instruction at the end of HoistPt.
Aditya Kumardfa87412017-09-13 05:28:03 +00001057 Instruction *Last = DestBB->getTerminator();
Eli Friedmanc6885fc2016-12-07 19:55:59 +00001058 MD->removeInstruction(Repl);
Sebastian Pop4ba7c882016-08-03 20:54:36 +00001059 Repl->moveBefore(Last);
1060
1061 DFSNumber[Repl] = DFSNumber[Last]++;
Sebastian Pop41774802016-07-15 13:45:20 +00001062 }
1063
Aditya Kumardfa87412017-09-13 05:28:03 +00001064 NR += removeAndReplace(InstructionsToHoist, Repl, DestBB, MoveAccess);
Daniel Berlinf75fd1b2016-08-11 20:32:43 +00001065
Sebastian Pop41774802016-07-15 13:45:20 +00001066 if (isa<LoadInst>(Repl))
1067 ++NL;
1068 else if (isa<StoreInst>(Repl))
1069 ++NS;
1070 else if (isa<CallInst>(Repl))
1071 ++NC;
1072 else // Scalar
1073 ++NI;
Sebastian Pop41774802016-07-15 13:45:20 +00001074 }
1075
1076 NumHoisted += NL + NS + NC + NI;
1077 NumRemoved += NR;
1078 NumLoadsHoisted += NL;
1079 NumStoresHoisted += NS;
1080 NumCallsHoisted += NC;
1081 return {NI, NL + NC + NS};
1082 }
1083
1084 // Hoist all expressions. Returns Number of scalars hoisted
1085 // and number of non-scalars hoisted.
1086 std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
1087 InsnInfo II;
1088 LoadInfo LI;
1089 StoreInfo SI;
1090 CallInfo CI;
1091 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
Sebastian Pop38422b12016-07-26 00:15:08 +00001092 int InstructionNb = 0;
Sebastian Pop41774802016-07-15 13:45:20 +00001093 for (Instruction &I1 : *BB) {
Geoff Berry635e5052017-04-10 20:45:17 +00001094 // If I1 cannot guarantee progress, subsequent instructions
1095 // in BB cannot be hoisted anyways.
1096 if (!isGuaranteedToTransferExecutionToSuccessor(&I1)) {
Aditya Kumardfa87412017-09-13 05:28:03 +00001097 HoistBarrier.insert(BB);
1098 break;
Geoff Berry635e5052017-04-10 20:45:17 +00001099 }
Sebastian Pop38422b12016-07-26 00:15:08 +00001100 // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting
1101 // deeper may increase the register pressure and compilation time.
1102 if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB)
1103 break;
1104
Sebastian Pop440f15b2016-09-22 14:45:40 +00001105 // Do not value number terminator instructions.
Chandler Carruth9ae926b2018-08-26 09:51:22 +00001106 if (I1.isTerminator())
Sebastian Pop440f15b2016-09-22 14:45:40 +00001107 break;
1108
David Majnemer4c66a712016-07-18 00:34:58 +00001109 if (auto *Load = dyn_cast<LoadInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +00001110 LI.insert(Load, VN);
David Majnemer4c66a712016-07-18 00:34:58 +00001111 else if (auto *Store = dyn_cast<StoreInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +00001112 SI.insert(Store, VN);
David Majnemer4c66a712016-07-18 00:34:58 +00001113 else if (auto *Call = dyn_cast<CallInst>(&I1)) {
1114 if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
Sebastian Pop41774802016-07-15 13:45:20 +00001115 if (isa<DbgInfoIntrinsic>(Intr) ||
Dan Gohman2c74fe92017-11-08 21:59:51 +00001116 Intr->getIntrinsicID() == Intrinsic::assume ||
1117 Intr->getIntrinsicID() == Intrinsic::sideeffect)
Sebastian Pop41774802016-07-15 13:45:20 +00001118 continue;
1119 }
Hans Wennborg19c0be92017-03-01 17:15:08 +00001120 if (Call->mayHaveSideEffects())
1121 break;
Matt Arsenault6ad97732016-08-04 20:52:57 +00001122
1123 if (Call->isConvergent())
1124 break;
1125
Sebastian Pop41774802016-07-15 13:45:20 +00001126 CI.insert(Call, VN);
Sebastian Pop55c30072016-07-27 05:48:12 +00001127 } else if (HoistingGeps || !isa<GetElementPtrInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +00001128 // Do not hoist scalars past calls that may write to memory because
1129 // that could result in spills later. geps are handled separately.
1130 // TODO: We can relax this for targets like AArch64 as they have more
1131 // registers than X86.
1132 II.insert(&I1, VN);
1133 }
1134 }
1135
1136 HoistingPointList HPL;
1137 computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
1138 computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
1139 computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
1140 computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
1141 computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
1142 computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
1143 return hoist(HPL);
1144 }
Sebastian Pop41774802016-07-15 13:45:20 +00001145};
1146
1147class GVNHoistLegacyPass : public FunctionPass {
1148public:
1149 static char ID;
1150
1151 GVNHoistLegacyPass() : FunctionPass(ID) {
1152 initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
1153 }
1154
1155 bool runOnFunction(Function &F) override {
Paul Robinson2d23c022016-07-19 22:57:14 +00001156 if (skipFunction(F))
1157 return false;
Sebastian Pop41774802016-07-15 13:45:20 +00001158 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Aditya Kumardfa87412017-09-13 05:28:03 +00001159 auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Sebastian Pop41774802016-07-15 13:45:20 +00001160 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
1161 auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
Daniel Berlinea02eee2016-08-23 05:42:41 +00001162 auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
Sebastian Pop41774802016-07-15 13:45:20 +00001163
Aditya Kumardfa87412017-09-13 05:28:03 +00001164 GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA);
Sebastian Pop41774802016-07-15 13:45:20 +00001165 return G.run(F);
1166 }
1167
1168 void getAnalysisUsage(AnalysisUsage &AU) const override {
1169 AU.addRequired<DominatorTreeWrapperPass>();
Aditya Kumardfa87412017-09-13 05:28:03 +00001170 AU.addRequired<PostDominatorTreeWrapperPass>();
Sebastian Pop41774802016-07-15 13:45:20 +00001171 AU.addRequired<AAResultsWrapperPass>();
1172 AU.addRequired<MemoryDependenceWrapperPass>();
Daniel Berlinea02eee2016-08-23 05:42:41 +00001173 AU.addRequired<MemorySSAWrapperPass>();
Sebastian Pop41774802016-07-15 13:45:20 +00001174 AU.addPreserved<DominatorTreeWrapperPass>();
Daniel Berlinea02eee2016-08-23 05:42:41 +00001175 AU.addPreserved<MemorySSAWrapperPass>();
Nikolai Bozhenov9e4a1c32017-04-18 13:25:49 +00001176 AU.addPreserved<GlobalsAAWrapperPass>();
Sebastian Pop41774802016-07-15 13:45:20 +00001177 }
1178};
Eugene Zelenko3b879392017-10-13 21:17:07 +00001179
1180} // end namespace llvm
Sebastian Pop41774802016-07-15 13:45:20 +00001181
Sebastian Pop5ba9f242016-10-13 01:39:10 +00001182PreservedAnalyses GVNHoistPass::run(Function &F, FunctionAnalysisManager &AM) {
Sebastian Pop41774802016-07-15 13:45:20 +00001183 DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
Aditya Kumardfa87412017-09-13 05:28:03 +00001184 PostDominatorTree &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
Sebastian Pop41774802016-07-15 13:45:20 +00001185 AliasAnalysis &AA = AM.getResult<AAManager>(F);
1186 MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
Daniel Berlinea02eee2016-08-23 05:42:41 +00001187 MemorySSA &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
Aditya Kumardfa87412017-09-13 05:28:03 +00001188 GVNHoist G(&DT, &PDT, &AA, &MD, &MSSA);
Sebastian Pop41774802016-07-15 13:45:20 +00001189 if (!G.run(F))
1190 return PreservedAnalyses::all();
1191
1192 PreservedAnalyses PA;
1193 PA.preserve<DominatorTreeAnalysis>();
Daniel Berlinea02eee2016-08-23 05:42:41 +00001194 PA.preserve<MemorySSAAnalysis>();
Nikolai Bozhenov9e4a1c32017-04-18 13:25:49 +00001195 PA.preserve<GlobalsAA>();
Sebastian Pop41774802016-07-15 13:45:20 +00001196 return PA;
1197}
1198
1199char GVNHoistLegacyPass::ID = 0;
Eugene Zelenko3b879392017-10-13 21:17:07 +00001200
Sebastian Pop41774802016-07-15 13:45:20 +00001201INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
1202 "Early GVN Hoisting of Expressions", false, false)
1203INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
Daniel Berlinea02eee2016-08-23 05:42:41 +00001204INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
Sebastian Pop41774802016-07-15 13:45:20 +00001205INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Alexander Ivchenko836eac32018-02-08 11:45:36 +00001206INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Sebastian Pop41774802016-07-15 13:45:20 +00001207INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1208INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
1209 "Early GVN Hoisting of Expressions", false, false)
1210
1211FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }