blob: 2ddec453edbcb01a02e20cedbbbfafcc060f2a43 [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
12// values. The primary goal is to reduce the code size, and in some
13// cases reduce critical path (by exposing more ILP).
14// Hoisting may affect the performance in some cases. To mitigate that, hoisting
15// is disabled in the following cases.
16// 1. Scalars across calls.
17// 2. geps when corresponding load/store cannot be hoisted.
18//===----------------------------------------------------------------------===//
19
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/ValueTracking.h"
24#include "llvm/Transforms/Scalar.h"
25#include "llvm/Transforms/Scalar/GVN.h"
David Majnemer68623a02016-07-25 02:21:25 +000026#include "llvm/Transforms/Utils/Local.h"
Sebastian Pop41774802016-07-15 13:45:20 +000027#include "llvm/Transforms/Utils/MemorySSA.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "gvn-hoist"
32
33STATISTIC(NumHoisted, "Number of instructions hoisted");
34STATISTIC(NumRemoved, "Number of instructions removed");
35STATISTIC(NumLoadsHoisted, "Number of loads hoisted");
36STATISTIC(NumLoadsRemoved, "Number of loads removed");
37STATISTIC(NumStoresHoisted, "Number of stores hoisted");
38STATISTIC(NumStoresRemoved, "Number of stores removed");
39STATISTIC(NumCallsHoisted, "Number of calls hoisted");
40STATISTIC(NumCallsRemoved, "Number of calls removed");
41
42static cl::opt<int>
43 MaxHoistedThreshold("gvn-max-hoisted", cl::Hidden, cl::init(-1),
44 cl::desc("Max number of instructions to hoist "
45 "(default unlimited = -1)"));
46static cl::opt<int> MaxNumberOfBBSInPath(
47 "gvn-hoist-max-bbs", cl::Hidden, cl::init(4),
48 cl::desc("Max number of basic blocks on the path between "
49 "hoisting locations (default = 4, unlimited = -1)"));
50
Sebastian Pop38422b12016-07-26 00:15:08 +000051static cl::opt<int> MaxDepthInBB(
52 "gvn-hoist-max-depth", cl::Hidden, cl::init(100),
53 cl::desc("Hoist instructions from the beginning of the BB up to the "
54 "maximum specified depth (default = 100, unlimited = -1)"));
55
Sebastian Pop41774802016-07-15 13:45:20 +000056namespace {
57
58// Provides a sorting function based on the execution order of two instructions.
59struct SortByDFSIn {
60private:
Sebastian Pop91d4a302016-07-26 00:15:10 +000061 DenseMap<const Value *, unsigned> &DFSNumber;
Sebastian Pop41774802016-07-15 13:45:20 +000062
63public:
Sebastian Pop91d4a302016-07-26 00:15:10 +000064 SortByDFSIn(DenseMap<const Value *, unsigned> &D) : DFSNumber(D) {}
Sebastian Pop41774802016-07-15 13:45:20 +000065
66 // Returns true when A executes before B.
67 bool operator()(const Instruction *A, const Instruction *B) const {
68 // FIXME: libc++ has a std::sort() algorithm that will call the compare
69 // function on the same element. Once PR20837 is fixed and some more years
70 // pass by and all the buildbots have moved to a corrected std::sort(),
71 // enable the following assert:
72 //
73 // assert(A != B);
74
Sebastian Pop4ba7c882016-08-03 20:54:36 +000075 const BasicBlock *BA = A->getParent();
76 const BasicBlock *BB = B->getParent();
77 unsigned ADFS, BDFS;
78 if (BA == BB) {
79 ADFS = DFSNumber.lookup(A);
80 BDFS = DFSNumber.lookup(B);
81 } else {
82 ADFS = DFSNumber.lookup(BA);
83 BDFS = DFSNumber.lookup(BB);
84 }
George Burgess IV9cf05462016-07-27 06:34:53 +000085 assert (ADFS && BDFS);
Sebastian Pop91d4a302016-07-26 00:15:10 +000086 return ADFS < BDFS;
Sebastian Pop41774802016-07-15 13:45:20 +000087 }
88};
89
David Majnemer04c7c222016-07-18 06:11:37 +000090// A map from a pair of VNs to all the instructions with those VNs.
91typedef DenseMap<std::pair<unsigned, unsigned>, SmallVector<Instruction *, 4>>
92 VNtoInsns;
93// An invalid value number Used when inserting a single value number into
94// VNtoInsns.
Reid Kleckner3498ad12016-07-18 18:53:50 +000095enum : unsigned { InvalidVN = ~2U };
Sebastian Pop41774802016-07-15 13:45:20 +000096
97// Records all scalar instructions candidate for code hoisting.
98class InsnInfo {
99 VNtoInsns VNtoScalars;
100
101public:
102 // Inserts I and its value number in VNtoScalars.
103 void insert(Instruction *I, GVN::ValueTable &VN) {
104 // Scalar instruction.
105 unsigned V = VN.lookupOrAdd(I);
David Majnemer04c7c222016-07-18 06:11:37 +0000106 VNtoScalars[{V, InvalidVN}].push_back(I);
Sebastian Pop41774802016-07-15 13:45:20 +0000107 }
108
109 const VNtoInsns &getVNTable() const { return VNtoScalars; }
110};
111
112// Records all load instructions candidate for code hoisting.
113class LoadInfo {
114 VNtoInsns VNtoLoads;
115
116public:
117 // Insert Load and the value number of its memory address in VNtoLoads.
118 void insert(LoadInst *Load, GVN::ValueTable &VN) {
119 if (Load->isSimple()) {
120 unsigned V = VN.lookupOrAdd(Load->getPointerOperand());
David Majnemer04c7c222016-07-18 06:11:37 +0000121 VNtoLoads[{V, InvalidVN}].push_back(Load);
Sebastian Pop41774802016-07-15 13:45:20 +0000122 }
123 }
124
125 const VNtoInsns &getVNTable() const { return VNtoLoads; }
126};
127
128// Records all store instructions candidate for code hoisting.
129class StoreInfo {
130 VNtoInsns VNtoStores;
131
132public:
133 // Insert the Store and a hash number of the store address and the stored
134 // value in VNtoStores.
135 void insert(StoreInst *Store, GVN::ValueTable &VN) {
136 if (!Store->isSimple())
137 return;
138 // Hash the store address and the stored value.
139 Value *Ptr = Store->getPointerOperand();
140 Value *Val = Store->getValueOperand();
David Majnemer04c7c222016-07-18 06:11:37 +0000141 VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store);
Sebastian Pop41774802016-07-15 13:45:20 +0000142 }
143
144 const VNtoInsns &getVNTable() const { return VNtoStores; }
145};
146
147// Records all call instructions candidate for code hoisting.
148class CallInfo {
149 VNtoInsns VNtoCallsScalars;
150 VNtoInsns VNtoCallsLoads;
151 VNtoInsns VNtoCallsStores;
152
153public:
154 // Insert Call and its value numbering in one of the VNtoCalls* containers.
155 void insert(CallInst *Call, GVN::ValueTable &VN) {
156 // A call that doesNotAccessMemory is handled as a Scalar,
157 // onlyReadsMemory will be handled as a Load instruction,
158 // all other calls will be handled as stores.
159 unsigned V = VN.lookupOrAdd(Call);
David Majnemer04c7c222016-07-18 06:11:37 +0000160 auto Entry = std::make_pair(V, InvalidVN);
Sebastian Pop41774802016-07-15 13:45:20 +0000161
162 if (Call->doesNotAccessMemory())
David Majnemer04c7c222016-07-18 06:11:37 +0000163 VNtoCallsScalars[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000164 else if (Call->onlyReadsMemory())
David Majnemer04c7c222016-07-18 06:11:37 +0000165 VNtoCallsLoads[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000166 else
David Majnemer04c7c222016-07-18 06:11:37 +0000167 VNtoCallsStores[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000168 }
169
170 const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; }
171
172 const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; }
173
174 const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; }
175};
176
177typedef DenseMap<const BasicBlock *, bool> BBSideEffectsSet;
178typedef SmallVector<Instruction *, 4> SmallVecInsn;
179typedef SmallVectorImpl<Instruction *> SmallVecImplInsn;
180
David Majnemer68623a02016-07-25 02:21:25 +0000181static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
182 static const unsigned KnownIDs[] = {
183 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
184 LLVMContext::MD_noalias, LLVMContext::MD_range,
185 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
186 LLVMContext::MD_invariant_group};
187 combineMetadata(ReplInst, I, KnownIDs);
188}
189
Sebastian Pop41774802016-07-15 13:45:20 +0000190// This pass hoists common computations across branches sharing common
191// dominator. The primary goal is to reduce the code size, and in some
192// cases reduce critical path (by exposing more ILP).
193class GVNHoist {
194public:
Daniel Berlin65af45d2016-07-25 17:24:22 +0000195 GVNHoist(DominatorTree *Dt, AliasAnalysis *Aa, MemoryDependenceResults *Md,
196 bool OptForMinSize)
Sebastian Pop55c30072016-07-27 05:48:12 +0000197 : DT(Dt), AA(Aa), MD(Md), OptForMinSize(OptForMinSize),
198 HoistingGeps(OptForMinSize), HoistedCtr(0) {}
Daniel Berlin65af45d2016-07-25 17:24:22 +0000199 bool run(Function &F) {
200 VN.setDomTree(DT);
201 VN.setAliasAnalysis(AA);
202 VN.setMemDep(MD);
203 bool Res = false;
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000204 MemorySSA M(F, AA, DT);
205 MSSA = &M;
Sebastian Pop4ba7c882016-08-03 20:54:36 +0000206 // Perform DFS Numbering of instructions.
207 unsigned BBI = 0;
208 for (const BasicBlock *BB : depth_first(&F.getEntryBlock())) {
209 DFSNumber[BB] = ++BBI;
210 unsigned I = 0;
211 for (auto &Inst: *BB)
212 DFSNumber[&Inst] = ++I;
213 }
Daniel Berlin65af45d2016-07-25 17:24:22 +0000214
Daniel Berlin65af45d2016-07-25 17:24:22 +0000215 // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
216 while (1) {
Daniel Berlin65af45d2016-07-25 17:24:22 +0000217 auto HoistStat = hoistExpressions(F);
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000218 if (HoistStat.first + HoistStat.second == 0)
Daniel Berlin65af45d2016-07-25 17:24:22 +0000219 return Res;
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000220
221 if (HoistStat.second > 0)
Daniel Berlin65af45d2016-07-25 17:24:22 +0000222 // To address a limitation of the current GVN, we need to rerun the
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000223 // hoisting after we hoisted loads or stores in order to be able to
224 // hoist all scalars dependent on the hoisted ld/st.
Daniel Berlin65af45d2016-07-25 17:24:22 +0000225 VN.clear();
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000226
Daniel Berlin65af45d2016-07-25 17:24:22 +0000227 Res = true;
228 }
229
230 return Res;
231 }
232private:
Sebastian Pop41774802016-07-15 13:45:20 +0000233 GVN::ValueTable VN;
234 DominatorTree *DT;
235 AliasAnalysis *AA;
236 MemoryDependenceResults *MD;
237 const bool OptForMinSize;
Sebastian Pop55c30072016-07-27 05:48:12 +0000238 const bool HoistingGeps;
Sebastian Pop91d4a302016-07-26 00:15:10 +0000239 DenseMap<const Value *, unsigned> DFSNumber;
Sebastian Pop41774802016-07-15 13:45:20 +0000240 BBSideEffectsSet BBSideEffects;
241 MemorySSA *MSSA;
David Majnemeraa241782016-07-18 00:35:01 +0000242 int HoistedCtr;
243
Sebastian Pop41774802016-07-15 13:45:20 +0000244 enum InsKind { Unknown, Scalar, Load, Store };
245
Sebastian Pop41774802016-07-15 13:45:20 +0000246 // Return true when there are exception handling in BB.
247 bool hasEH(const BasicBlock *BB) {
248 auto It = BBSideEffects.find(BB);
249 if (It != BBSideEffects.end())
250 return It->second;
251
252 if (BB->isEHPad() || BB->hasAddressTaken()) {
253 BBSideEffects[BB] = true;
254 return true;
255 }
256
257 if (BB->getTerminator()->mayThrow()) {
258 BBSideEffects[BB] = true;
259 return true;
260 }
261
262 BBSideEffects[BB] = false;
263 return false;
264 }
265
266 // Return true when all paths from A to the end of the function pass through
267 // either B or C.
268 bool hoistingFromAllPaths(const BasicBlock *A, const BasicBlock *B,
269 const BasicBlock *C) {
270 // We fully copy the WL in order to be able to remove items from it.
271 SmallPtrSet<const BasicBlock *, 2> WL;
272 WL.insert(B);
273 WL.insert(C);
274
275 for (auto It = df_begin(A), E = df_end(A); It != E;) {
276 // There exists a path from A to the exit of the function if we are still
277 // iterating in DF traversal and we removed all instructions from the work
278 // list.
279 if (WL.empty())
280 return false;
281
282 const BasicBlock *BB = *It;
283 if (WL.erase(BB)) {
284 // Stop DFS traversal when BB is in the work list.
285 It.skipChildren();
286 continue;
287 }
288
289 // Check for end of function, calls that do not return, etc.
290 if (!isGuaranteedToTransferExecutionToSuccessor(BB->getTerminator()))
291 return false;
292
293 // Increment DFS traversal when not skipping children.
294 ++It;
295 }
296
297 return true;
298 }
299
300 /* Return true when I1 appears before I2 in the instructions of BB. */
Sebastian Pop91d4a302016-07-26 00:15:10 +0000301 bool firstInBB(const Instruction *I1, const Instruction *I2) {
302 assert (I1->getParent() == I2->getParent());
303 unsigned I1DFS = DFSNumber.lookup(I1);
304 unsigned I2DFS = DFSNumber.lookup(I2);
305 assert (I1DFS && I2DFS);
306 return I1DFS < I2DFS;
Daniel Berlin40765a62016-07-25 18:19:49 +0000307 }
Sebastian Pop91d4a302016-07-26 00:15:10 +0000308
Sebastian Pop41774802016-07-15 13:45:20 +0000309 // Return true when there are users of Def in BB.
310 bool hasMemoryUseOnPath(MemoryAccess *Def, const BasicBlock *BB,
311 const Instruction *OldPt) {
Sebastian Pop41774802016-07-15 13:45:20 +0000312 const BasicBlock *DefBB = Def->getBlock();
313 const BasicBlock *OldBB = OldPt->getParent();
314
David Majnemer4c66a712016-07-18 00:34:58 +0000315 for (User *U : Def->users())
316 if (auto *MU = dyn_cast<MemoryUse>(U)) {
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000317 // FIXME: MU->getBlock() does not get updated when we move the instruction.
318 BasicBlock *UBB = MU->getMemoryInst()->getParent();
Sebastian Pop41774802016-07-15 13:45:20 +0000319 // Only analyze uses in BB.
320 if (BB != UBB)
321 continue;
322
323 // A use in the same block as the Def is on the path.
324 if (UBB == DefBB) {
David Majnemer4c66a712016-07-18 00:34:58 +0000325 assert(MSSA->locallyDominates(Def, MU) && "def not dominating use");
Sebastian Pop41774802016-07-15 13:45:20 +0000326 return true;
327 }
328
329 if (UBB != OldBB)
330 return true;
331
332 // It is only harmful to hoist when the use is before OldPt.
Sebastian Pop91d4a302016-07-26 00:15:10 +0000333 if (firstInBB(MU->getMemoryInst(), OldPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000334 return true;
335 }
336
337 return false;
338 }
339
340 // Return true when there are exception handling or loads of memory Def
341 // between OldPt and NewPt.
342
343 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
344 // return true when the counter NBBsOnAllPaths reaces 0, except when it is
345 // initialized to -1 which is unlimited.
346 bool hasEHOrLoadsOnPath(const Instruction *NewPt, const Instruction *OldPt,
347 MemoryAccess *Def, int &NBBsOnAllPaths) {
348 const BasicBlock *NewBB = NewPt->getParent();
349 const BasicBlock *OldBB = OldPt->getParent();
350 assert(DT->dominates(NewBB, OldBB) && "invalid path");
351 assert(DT->dominates(Def->getBlock(), NewBB) &&
352 "def does not dominate new hoisting point");
353
354 // Walk all basic blocks reachable in depth-first iteration on the inverse
355 // CFG from OldBB to NewBB. These blocks are all the blocks that may be
356 // executed between the execution of NewBB and OldBB. Hoisting an expression
357 // from OldBB into NewBB has to be safe on all execution paths.
358 for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
359 if (*I == NewBB) {
360 // Stop traversal when reaching HoistPt.
361 I.skipChildren();
362 continue;
363 }
364
365 // Impossible to hoist with exceptions on the path.
366 if (hasEH(*I))
367 return true;
368
369 // Check that we do not move a store past loads.
370 if (hasMemoryUseOnPath(Def, *I, OldPt))
371 return true;
372
373 // Stop walk once the limit is reached.
374 if (NBBsOnAllPaths == 0)
375 return true;
376
377 // -1 is unlimited number of blocks on all paths.
378 if (NBBsOnAllPaths != -1)
379 --NBBsOnAllPaths;
380
381 ++I;
382 }
383
384 return false;
385 }
386
387 // Return true when there are exception handling between HoistPt and BB.
388 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
389 // return true when the counter NBBsOnAllPaths reaches 0, except when it is
390 // initialized to -1 which is unlimited.
391 bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *BB,
392 int &NBBsOnAllPaths) {
393 assert(DT->dominates(HoistPt, BB) && "Invalid path");
394
395 // Walk all basic blocks reachable in depth-first iteration on
396 // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
397 // blocks that may be executed between the execution of NewHoistPt and
398 // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
399 // on all execution paths.
400 for (auto I = idf_begin(BB), E = idf_end(BB); I != E;) {
401 if (*I == HoistPt) {
402 // Stop traversal when reaching NewHoistPt.
403 I.skipChildren();
404 continue;
405 }
406
407 // Impossible to hoist with exceptions on the path.
408 if (hasEH(*I))
409 return true;
410
411 // Stop walk once the limit is reached.
412 if (NBBsOnAllPaths == 0)
413 return true;
414
415 // -1 is unlimited number of blocks on all paths.
416 if (NBBsOnAllPaths != -1)
417 --NBBsOnAllPaths;
418
419 ++I;
420 }
421
422 return false;
423 }
424
425 // Return true when it is safe to hoist a memory load or store U from OldPt
426 // to NewPt.
427 bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
428 MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) {
429
430 // In place hoisting is safe.
431 if (NewPt == OldPt)
432 return true;
433
434 const BasicBlock *NewBB = NewPt->getParent();
435 const BasicBlock *OldBB = OldPt->getParent();
436 const BasicBlock *UBB = U->getBlock();
437
438 // Check for dependences on the Memory SSA.
439 MemoryAccess *D = U->getDefiningAccess();
440 BasicBlock *DBB = D->getBlock();
441 if (DT->properlyDominates(NewBB, DBB))
442 // Cannot move the load or store to NewBB above its definition in DBB.
443 return false;
444
445 if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
David Majnemer4c66a712016-07-18 00:34:58 +0000446 if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
Sebastian Pop91d4a302016-07-26 00:15:10 +0000447 if (firstInBB(NewPt, UD->getMemoryInst()))
Sebastian Pop41774802016-07-15 13:45:20 +0000448 // Cannot move the load or store to NewPt above its definition in D.
449 return false;
450
451 // Check for unsafe hoistings due to side effects.
452 if (K == InsKind::Store) {
453 if (hasEHOrLoadsOnPath(NewPt, OldPt, D, NBBsOnAllPaths))
454 return false;
455 } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
456 return false;
457
458 if (UBB == NewBB) {
459 if (DT->properlyDominates(DBB, NewBB))
460 return true;
461 assert(UBB == DBB);
462 assert(MSSA->locallyDominates(D, U));
463 }
464
465 // No side effects: it is safe to hoist.
466 return true;
467 }
468
469 // Return true when it is safe to hoist scalar instructions from BB1 and BB2
470 // to HoistBB.
471 bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB1,
472 const BasicBlock *BB2, int &NBBsOnAllPaths) {
473 // Check that the hoisted expression is needed on all paths. When HoistBB
474 // already contains an instruction to be hoisted, the expression is needed
475 // on all paths. Enable scalar hoisting at -Oz as it is safe to hoist
476 // scalars to a place where they are partially needed.
477 if (!OptForMinSize && BB1 != HoistBB &&
478 !hoistingFromAllPaths(HoistBB, BB1, BB2))
479 return false;
480
481 if (hasEHOnPath(HoistBB, BB1, NBBsOnAllPaths) ||
482 hasEHOnPath(HoistBB, BB2, NBBsOnAllPaths))
483 return false;
484
485 // Safe to hoist scalars from BB1 and BB2 to HoistBB.
486 return true;
487 }
488
489 // Each element of a hoisting list contains the basic block where to hoist and
490 // a list of instructions to be hoisted.
491 typedef std::pair<BasicBlock *, SmallVecInsn> HoistingPointInfo;
492 typedef SmallVector<HoistingPointInfo, 4> HoistingPointList;
493
494 // Partition InstructionsToHoist into a set of candidates which can share a
495 // common hoisting point. The partitions are collected in HPL. IsScalar is
496 // true when the instructions in InstructionsToHoist are scalars. IsLoad is
497 // true when the InstructionsToHoist are loads, false when they are stores.
498 void partitionCandidates(SmallVecImplInsn &InstructionsToHoist,
499 HoistingPointList &HPL, InsKind K) {
500 // No need to sort for two instructions.
501 if (InstructionsToHoist.size() > 2) {
502 SortByDFSIn Pred(DFSNumber);
503 std::sort(InstructionsToHoist.begin(), InstructionsToHoist.end(), Pred);
504 }
505
506 int NBBsOnAllPaths = MaxNumberOfBBSInPath;
507
508 SmallVecImplInsn::iterator II = InstructionsToHoist.begin();
509 SmallVecImplInsn::iterator Start = II;
510 Instruction *HoistPt = *II;
511 BasicBlock *HoistBB = HoistPt->getParent();
512 MemoryUseOrDef *UD;
513 if (K != InsKind::Scalar)
514 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(HoistPt));
515
516 for (++II; II != InstructionsToHoist.end(); ++II) {
517 Instruction *Insn = *II;
518 BasicBlock *BB = Insn->getParent();
519 BasicBlock *NewHoistBB;
520 Instruction *NewHoistPt;
521
522 if (BB == HoistBB) {
523 NewHoistBB = HoistBB;
Sebastian Pop91d4a302016-07-26 00:15:10 +0000524 NewHoistPt = firstInBB(Insn, HoistPt) ? Insn : HoistPt;
Sebastian Pop41774802016-07-15 13:45:20 +0000525 } else {
526 NewHoistBB = DT->findNearestCommonDominator(HoistBB, BB);
527 if (NewHoistBB == BB)
528 NewHoistPt = Insn;
529 else if (NewHoistBB == HoistBB)
530 NewHoistPt = HoistPt;
531 else
532 NewHoistPt = NewHoistBB->getTerminator();
533 }
534
535 if (K == InsKind::Scalar) {
536 if (safeToHoistScalar(NewHoistBB, HoistBB, BB, NBBsOnAllPaths)) {
537 // Extend HoistPt to NewHoistPt.
538 HoistPt = NewHoistPt;
539 HoistBB = NewHoistBB;
540 continue;
541 }
542 } else {
543 // When NewBB already contains an instruction to be hoisted, the
544 // expression is needed on all paths.
545 // Check that the hoisted expression is needed on all paths: it is
546 // unsafe to hoist loads to a place where there may be a path not
547 // loading from the same address: for instance there may be a branch on
548 // which the address of the load may not be initialized.
549 if ((HoistBB == NewHoistBB || BB == NewHoistBB ||
550 hoistingFromAllPaths(NewHoistBB, HoistBB, BB)) &&
551 // Also check that it is safe to move the load or store from HoistPt
552 // to NewHoistPt, and from Insn to NewHoistPt.
553 safeToHoistLdSt(NewHoistPt, HoistPt, UD, K, NBBsOnAllPaths) &&
554 safeToHoistLdSt(NewHoistPt, Insn,
555 cast<MemoryUseOrDef>(MSSA->getMemoryAccess(Insn)),
556 K, NBBsOnAllPaths)) {
557 // Extend HoistPt to NewHoistPt.
558 HoistPt = NewHoistPt;
559 HoistBB = NewHoistBB;
560 continue;
561 }
562 }
563
564 // At this point it is not safe to extend the current hoisting to
565 // NewHoistPt: save the hoisting list so far.
566 if (std::distance(Start, II) > 1)
David Majnemer4c66a712016-07-18 00:34:58 +0000567 HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
Sebastian Pop41774802016-07-15 13:45:20 +0000568
569 // Start over from BB.
570 Start = II;
571 if (K != InsKind::Scalar)
572 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(*Start));
573 HoistPt = Insn;
574 HoistBB = BB;
575 NBBsOnAllPaths = MaxNumberOfBBSInPath;
576 }
577
578 // Save the last partition.
579 if (std::distance(Start, II) > 1)
David Majnemer4c66a712016-07-18 00:34:58 +0000580 HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
Sebastian Pop41774802016-07-15 13:45:20 +0000581 }
582
583 // Initialize HPL from Map.
584 void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
585 InsKind K) {
David Majnemer4c66a712016-07-18 00:34:58 +0000586 for (const auto &Entry : Map) {
Sebastian Pop41774802016-07-15 13:45:20 +0000587 if (MaxHoistedThreshold != -1 && ++HoistedCtr > MaxHoistedThreshold)
588 return;
589
David Majnemer4c66a712016-07-18 00:34:58 +0000590 const SmallVecInsn &V = Entry.second;
Sebastian Pop41774802016-07-15 13:45:20 +0000591 if (V.size() < 2)
592 continue;
593
594 // Compute the insertion point and the list of expressions to be hoisted.
595 SmallVecInsn InstructionsToHoist;
596 for (auto I : V)
597 if (!hasEH(I->getParent()))
598 InstructionsToHoist.push_back(I);
599
David Majnemer4c66a712016-07-18 00:34:58 +0000600 if (!InstructionsToHoist.empty())
Sebastian Pop41774802016-07-15 13:45:20 +0000601 partitionCandidates(InstructionsToHoist, HPL, K);
602 }
603 }
604
605 // Return true when all operands of Instr are available at insertion point
606 // HoistPt. When limiting the number of hoisted expressions, one could hoist
607 // a load without hoisting its access function. So before hoisting any
608 // expression, make sure that all its operands are available at insert point.
609 bool allOperandsAvailable(const Instruction *I,
610 const BasicBlock *HoistPt) const {
David Majnemer4c66a712016-07-18 00:34:58 +0000611 for (const Use &Op : I->operands())
612 if (const auto *Inst = dyn_cast<Instruction>(&Op))
613 if (!DT->dominates(Inst->getParent(), HoistPt))
614 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000615
616 return true;
617 }
618
Sebastian Pop55c30072016-07-27 05:48:12 +0000619 // Same as allOperandsAvailable with recursive check for GEP operands.
620 bool allGepOperandsAvailable(const Instruction *I,
621 const BasicBlock *HoistPt) const {
622 for (const Use &Op : I->operands())
623 if (const auto *Inst = dyn_cast<Instruction>(&Op))
624 if (!DT->dominates(Inst->getParent(), HoistPt)) {
625 if (const GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Inst)) {
626 if (!allGepOperandsAvailable(GepOp, HoistPt))
627 return false;
628 // Gep is available if all operands of GepOp are available.
629 } else {
630 // Gep is not available if it has operands other than GEPs that are
631 // defined in blocks not dominating HoistPt.
632 return false;
633 }
634 }
635 return true;
636 }
637
638 // Make all operands of the GEP available.
639 void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
640 const SmallVecInsn &InstructionsToHoist,
641 Instruction *Gep) const {
642 assert(allGepOperandsAvailable(Gep, HoistPt) && "GEP operands not available");
643
644 Instruction *ClonedGep = Gep->clone();
645 for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i)
646 if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) {
647
648 // Check whether the operand is already available.
649 if (DT->dominates(Op->getParent(), HoistPt))
650 continue;
651
652 // As a GEP can refer to other GEPs, recursively make all the operands
653 // of this GEP available at HoistPt.
654 if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op))
655 makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp);
656 }
657
658 // Copy Gep and replace its uses in Repl with ClonedGep.
659 ClonedGep->insertBefore(HoistPt->getTerminator());
660
661 // Conservatively discard any optimization hints, they may differ on the
662 // other paths.
663 ClonedGep->dropUnknownNonDebugMetadata();
664
665 // If we have optimization hints which agree with each other along different
666 // paths, preserve them.
667 for (const Instruction *OtherInst : InstructionsToHoist) {
668 const GetElementPtrInst *OtherGep;
669 if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
670 OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
671 else
672 OtherGep = cast<GetElementPtrInst>(
673 cast<StoreInst>(OtherInst)->getPointerOperand());
674 ClonedGep->intersectOptionalDataWith(OtherGep);
675 }
676
677 // Replace uses of Gep with ClonedGep in Repl.
678 Repl->replaceUsesOfWith(Gep, ClonedGep);
679 }
680
681 // In the case Repl is a load or a store, we make all their GEPs
682 // available: GEPs are not hoisted by default to avoid the address
683 // computations to be hoisted without the associated load or store.
684 bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
685 const SmallVecInsn &InstructionsToHoist) const {
Sebastian Pop41774802016-07-15 13:45:20 +0000686 // Check whether the GEP of a ld/st can be synthesized at HoistPt.
David Majnemerbd210122016-07-20 21:05:01 +0000687 GetElementPtrInst *Gep = nullptr;
Sebastian Pop41774802016-07-15 13:45:20 +0000688 Instruction *Val = nullptr;
Sebastian Pop55c30072016-07-27 05:48:12 +0000689 if (auto *Ld = dyn_cast<LoadInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000690 Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
Sebastian Pop55c30072016-07-27 05:48:12 +0000691 } else if (auto *St = dyn_cast<StoreInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000692 Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
Sebastian Pop41774802016-07-15 13:45:20 +0000693 Val = dyn_cast<Instruction>(St->getValueOperand());
Sebastian Pop31fd5062016-07-21 23:22:10 +0000694 // Check that the stored value is available.
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000695 if (Val) {
696 if (isa<GetElementPtrInst>(Val)) {
697 // Check whether we can compute the GEP at HoistPt.
Sebastian Pop55c30072016-07-27 05:48:12 +0000698 if (!allGepOperandsAvailable(Val, HoistPt))
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000699 return false;
700 } else if (!DT->dominates(Val->getParent(), HoistPt))
701 return false;
702 }
Sebastian Pop41774802016-07-15 13:45:20 +0000703 }
704
Sebastian Pop41774802016-07-15 13:45:20 +0000705 // Check whether we can compute the Gep at HoistPt.
Sebastian Pop55c30072016-07-27 05:48:12 +0000706 if (!Gep || !allGepOperandsAvailable(Gep, HoistPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000707 return false;
708
Sebastian Pop55c30072016-07-27 05:48:12 +0000709 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep);
Sebastian Pop41774802016-07-15 13:45:20 +0000710
Sebastian Pop55c30072016-07-27 05:48:12 +0000711 if (Val && isa<GetElementPtrInst>(Val))
712 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val);
Sebastian Pop41774802016-07-15 13:45:20 +0000713
714 return true;
715 }
716
717 std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
718 unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
719 for (const HoistingPointInfo &HP : HPL) {
720 // Find out whether we already have one of the instructions in HoistPt,
721 // in which case we do not have to move it.
722 BasicBlock *HoistPt = HP.first;
723 const SmallVecInsn &InstructionsToHoist = HP.second;
724 Instruction *Repl = nullptr;
725 for (Instruction *I : InstructionsToHoist)
Sebastian Pop586d3ea2016-07-27 05:13:52 +0000726 if (I->getParent() == HoistPt)
Sebastian Pop41774802016-07-15 13:45:20 +0000727 // If there are two instructions in HoistPt to be hoisted in place:
728 // update Repl to be the first one, such that we can rename the uses
729 // of the second based on the first.
Sebastian Pop586d3ea2016-07-27 05:13:52 +0000730 if (!Repl || firstInBB(I, Repl))
731 Repl = I;
Sebastian Pop41774802016-07-15 13:45:20 +0000732
733 if (Repl) {
734 // Repl is already in HoistPt: it remains in place.
735 assert(allOperandsAvailable(Repl, HoistPt) &&
736 "instruction depends on operands that are not available");
737 } else {
738 // When we do not find Repl in HoistPt, select the first in the list
739 // and move it to HoistPt.
740 Repl = InstructionsToHoist.front();
741
742 // We can move Repl in HoistPt only when all operands are available.
Sebastian Pop55c30072016-07-27 05:48:12 +0000743 // When not HoistingGeps we need to copy the GEPs now.
Sebastian Pop41774802016-07-15 13:45:20 +0000744 // The order in which hoistings are done may influence the availability
745 // of operands.
Sebastian Pop55c30072016-07-27 05:48:12 +0000746 if (!allOperandsAvailable(Repl, HoistPt) && !HoistingGeps &&
747 !makeGepOperandsAvailable(Repl, HoistPt, InstructionsToHoist))
Sebastian Pop41774802016-07-15 13:45:20 +0000748 continue;
Sebastian Pop55c30072016-07-27 05:48:12 +0000749
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000750 // Move the instruction at the end of HoistPt.
Sebastian Pop4ba7c882016-08-03 20:54:36 +0000751 Instruction *Last = HoistPt->getTerminator();
752 Repl->moveBefore(Last);
753
754 DFSNumber[Repl] = DFSNumber[Last]++;
Sebastian Pop41774802016-07-15 13:45:20 +0000755 }
756
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000757 MemoryAccess *NewMemAcc = nullptr;
758 if (MemoryAccess *MA = MSSA->getMemoryAccess(Repl)) {
759 if (MemoryUseOrDef *OldMemAcc = dyn_cast<MemoryUseOrDef>(MA)) {
760 // The definition of this ld/st will not change: ld/st hoisting is
761 // legal when the ld/st is not moved past its current definition.
762 MemoryAccess *Def = OldMemAcc->getDefiningAccess();
763 NewMemAcc = MSSA->createMemoryAccessInBB(Repl, Def, HoistPt,
764 MemorySSA::End);
765 OldMemAcc->replaceAllUsesWith(NewMemAcc);
766 MSSA->removeMemoryAccess(OldMemAcc);
767 }
768 }
769
Sebastian Pop41774802016-07-15 13:45:20 +0000770 if (isa<LoadInst>(Repl))
771 ++NL;
772 else if (isa<StoreInst>(Repl))
773 ++NS;
774 else if (isa<CallInst>(Repl))
775 ++NC;
776 else // Scalar
777 ++NI;
778
779 // Remove and rename all other instructions.
780 for (Instruction *I : InstructionsToHoist)
781 if (I != Repl) {
782 ++NR;
David Majnemer47285692016-07-25 02:21:23 +0000783 if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
784 ReplacementLoad->setAlignment(
785 std::min(ReplacementLoad->getAlignment(),
786 cast<LoadInst>(I)->getAlignment()));
Sebastian Pop41774802016-07-15 13:45:20 +0000787 ++NumLoadsRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000788 } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
789 ReplacementStore->setAlignment(
790 std::min(ReplacementStore->getAlignment(),
791 cast<StoreInst>(I)->getAlignment()));
Sebastian Pop41774802016-07-15 13:45:20 +0000792 ++NumStoresRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000793 } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
794 ReplacementAlloca->setAlignment(
795 std::max(ReplacementAlloca->getAlignment(),
796 cast<AllocaInst>(I)->getAlignment()));
797 } else if (isa<CallInst>(Repl)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000798 ++NumCallsRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000799 }
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000800
801 if (NewMemAcc) {
802 // Update the uses of the old MSSA access with NewMemAcc.
803 MemoryAccess *OldMA = MSSA->getMemoryAccess(I);
804 OldMA->replaceAllUsesWith(NewMemAcc);
805 MSSA->removeMemoryAccess(OldMA);
806 }
807
David Majnemer4808f262016-07-21 05:59:53 +0000808 Repl->intersectOptionalDataWith(I);
David Majnemer68623a02016-07-25 02:21:25 +0000809 combineKnownMetadata(Repl, I);
Sebastian Pop41774802016-07-15 13:45:20 +0000810 I->replaceAllUsesWith(Repl);
811 I->eraseFromParent();
812 }
Sebastian Pop5d3822f2016-08-03 20:54:33 +0000813
814 // Remove MemorySSA phi nodes with the same arguments.
815 if (NewMemAcc) {
816 SmallPtrSet<MemoryPhi *, 4> UsePhis;
817 for (User *U : NewMemAcc->users())
818 if (MemoryPhi *Phi = dyn_cast<MemoryPhi>(U))
819 UsePhis.insert(Phi);
820
821 for (auto *Phi : UsePhis) {
822 auto In = Phi->incoming_values();
823 if (std::all_of(In.begin(), In.end(),
824 [&](Use &U){return U == NewMemAcc;})) {
825 Phi->replaceAllUsesWith(NewMemAcc);
826 MSSA->removeMemoryAccess(Phi);
827 }
828 }
829 }
Sebastian Pop41774802016-07-15 13:45:20 +0000830 }
831
832 NumHoisted += NL + NS + NC + NI;
833 NumRemoved += NR;
834 NumLoadsHoisted += NL;
835 NumStoresHoisted += NS;
836 NumCallsHoisted += NC;
837 return {NI, NL + NC + NS};
838 }
839
840 // Hoist all expressions. Returns Number of scalars hoisted
841 // and number of non-scalars hoisted.
842 std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
843 InsnInfo II;
844 LoadInfo LI;
845 StoreInfo SI;
846 CallInfo CI;
847 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
Sebastian Pop38422b12016-07-26 00:15:08 +0000848 int InstructionNb = 0;
Sebastian Pop41774802016-07-15 13:45:20 +0000849 for (Instruction &I1 : *BB) {
Sebastian Pop38422b12016-07-26 00:15:08 +0000850 // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting
851 // deeper may increase the register pressure and compilation time.
852 if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB)
853 break;
854
David Majnemer4c66a712016-07-18 00:34:58 +0000855 if (auto *Load = dyn_cast<LoadInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000856 LI.insert(Load, VN);
David Majnemer4c66a712016-07-18 00:34:58 +0000857 else if (auto *Store = dyn_cast<StoreInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000858 SI.insert(Store, VN);
David Majnemer4c66a712016-07-18 00:34:58 +0000859 else if (auto *Call = dyn_cast<CallInst>(&I1)) {
860 if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000861 if (isa<DbgInfoIntrinsic>(Intr) ||
862 Intr->getIntrinsicID() == Intrinsic::assume)
863 continue;
864 }
865 if (Call->mayHaveSideEffects()) {
866 if (!OptForMinSize)
867 break;
868 // We may continue hoisting across calls which write to memory.
869 if (Call->mayThrow())
870 break;
871 }
872 CI.insert(Call, VN);
Sebastian Pop55c30072016-07-27 05:48:12 +0000873 } else if (HoistingGeps || !isa<GetElementPtrInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000874 // Do not hoist scalars past calls that may write to memory because
875 // that could result in spills later. geps are handled separately.
876 // TODO: We can relax this for targets like AArch64 as they have more
877 // registers than X86.
878 II.insert(&I1, VN);
879 }
880 }
881
882 HoistingPointList HPL;
883 computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
884 computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
885 computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
886 computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
887 computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
888 computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
889 return hoist(HPL);
890 }
Sebastian Pop41774802016-07-15 13:45:20 +0000891};
892
893class GVNHoistLegacyPass : public FunctionPass {
894public:
895 static char ID;
896
897 GVNHoistLegacyPass() : FunctionPass(ID) {
898 initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
899 }
900
901 bool runOnFunction(Function &F) override {
Paul Robinson2d23c022016-07-19 22:57:14 +0000902 if (skipFunction(F))
903 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000904 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
905 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
906 auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
907
908 GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
909 return G.run(F);
910 }
911
912 void getAnalysisUsage(AnalysisUsage &AU) const override {
913 AU.addRequired<DominatorTreeWrapperPass>();
914 AU.addRequired<AAResultsWrapperPass>();
915 AU.addRequired<MemoryDependenceWrapperPass>();
916 AU.addPreserved<DominatorTreeWrapperPass>();
917 }
918};
919} // namespace
920
921PreservedAnalyses GVNHoistPass::run(Function &F,
922 AnalysisManager<Function> &AM) {
923 DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
924 AliasAnalysis &AA = AM.getResult<AAManager>(F);
925 MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
926
927 GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
928 if (!G.run(F))
929 return PreservedAnalyses::all();
930
931 PreservedAnalyses PA;
932 PA.preserve<DominatorTreeAnalysis>();
933 return PA;
934}
935
936char GVNHoistLegacyPass::ID = 0;
937INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
938 "Early GVN Hoisting of Expressions", false, false)
939INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
940INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
941INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
942INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
943 "Early GVN Hoisting of Expressions", false, false)
944
945FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }