blob: 245fef986a355bf0d2c7a223aca2a7b605d1bdd7 [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:
Daniel Berlin40765a62016-07-25 18:19:49 +000061 DenseMap<const BasicBlock *, unsigned> &DFSNumber;
Sebastian Pop41774802016-07-15 13:45:20 +000062
63public:
Daniel Berlin40765a62016-07-25 18:19:49 +000064 SortByDFSIn(DenseMap<const BasicBlock *, 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
75 const BasicBlock *BA = A->getParent();
76 const BasicBlock *BB = B->getParent();
77 unsigned NA = DFSNumber[BA];
78 unsigned NB = DFSNumber[BB];
79 if (NA < NB)
80 return true;
81 if (NA == NB) {
Daniel Berlin40765a62016-07-25 18:19:49 +000082 // Sort them in the order they occur in the same basic block.
83 BasicBlock::const_iterator AI(A), BI(B);
84 return std::distance(AI, BI) < 0;
Sebastian Pop41774802016-07-15 13:45:20 +000085 }
86 return false;
87 }
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)
197 : DT(Dt), AA(Aa), MD(Md), OptForMinSize(OptForMinSize), HoistedCtr(0) {}
198 bool run(Function &F) {
199 VN.setDomTree(DT);
200 VN.setAliasAnalysis(AA);
201 VN.setMemDep(MD);
202 bool Res = false;
203
204 unsigned I = 0;
Daniel Berlin40765a62016-07-25 18:19:49 +0000205 for (const BasicBlock *BB : depth_first(&F.getEntryBlock()))
Daniel Berlin65af45d2016-07-25 17:24:22 +0000206 DFSNumber.insert({BB, ++I});
207
208 // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
209 while (1) {
210 // FIXME: only compute MemorySSA once. We need to update the analysis in
211 // the same time as transforming the code.
212 MemorySSA M(F, AA, DT);
213 MSSA = &M;
214
215 auto HoistStat = hoistExpressions(F);
216 if (HoistStat.first + HoistStat.second == 0) {
217 return Res;
218 }
219 if (HoistStat.second > 0) {
220 // To address a limitation of the current GVN, we need to rerun the
221 // hoisting after we hoisted loads in order to be able to hoist all
222 // scalars dependent on the hoisted loads. Same for stores.
223 VN.clear();
224 }
225 Res = true;
226 }
227
228 return Res;
229 }
230private:
Sebastian Pop41774802016-07-15 13:45:20 +0000231 GVN::ValueTable VN;
232 DominatorTree *DT;
233 AliasAnalysis *AA;
234 MemoryDependenceResults *MD;
235 const bool OptForMinSize;
Daniel Berlin40765a62016-07-25 18:19:49 +0000236 DenseMap<const BasicBlock *, unsigned> DFSNumber;
Sebastian Pop41774802016-07-15 13:45:20 +0000237 BBSideEffectsSet BBSideEffects;
238 MemorySSA *MSSA;
David Majnemeraa241782016-07-18 00:35:01 +0000239 int HoistedCtr;
240
Sebastian Pop41774802016-07-15 13:45:20 +0000241 enum InsKind { Unknown, Scalar, Load, Store };
242
Sebastian Pop41774802016-07-15 13:45:20 +0000243 // Return true when there are exception handling in BB.
244 bool hasEH(const BasicBlock *BB) {
245 auto It = BBSideEffects.find(BB);
246 if (It != BBSideEffects.end())
247 return It->second;
248
249 if (BB->isEHPad() || BB->hasAddressTaken()) {
250 BBSideEffects[BB] = true;
251 return true;
252 }
253
254 if (BB->getTerminator()->mayThrow()) {
255 BBSideEffects[BB] = true;
256 return true;
257 }
258
259 BBSideEffects[BB] = false;
260 return false;
261 }
262
263 // Return true when all paths from A to the end of the function pass through
264 // either B or C.
265 bool hoistingFromAllPaths(const BasicBlock *A, const BasicBlock *B,
266 const BasicBlock *C) {
267 // We fully copy the WL in order to be able to remove items from it.
268 SmallPtrSet<const BasicBlock *, 2> WL;
269 WL.insert(B);
270 WL.insert(C);
271
272 for (auto It = df_begin(A), E = df_end(A); It != E;) {
273 // There exists a path from A to the exit of the function if we are still
274 // iterating in DF traversal and we removed all instructions from the work
275 // list.
276 if (WL.empty())
277 return false;
278
279 const BasicBlock *BB = *It;
280 if (WL.erase(BB)) {
281 // Stop DFS traversal when BB is in the work list.
282 It.skipChildren();
283 continue;
284 }
285
286 // Check for end of function, calls that do not return, etc.
287 if (!isGuaranteedToTransferExecutionToSuccessor(BB->getTerminator()))
288 return false;
289
290 // Increment DFS traversal when not skipping children.
291 ++It;
292 }
293
294 return true;
295 }
296
297 /* Return true when I1 appears before I2 in the instructions of BB. */
Daniel Berlin40765a62016-07-25 18:19:49 +0000298 bool firstInBB(BasicBlock *BB, const Instruction *I1, const Instruction *I2) {
299 for (Instruction &I : *BB) {
300 if (&I == I1)
301 return true;
302 if (&I == I2)
303 return false;
304 }
Daniel Berlinf107f322016-07-25 17:24:27 +0000305
Daniel Berlin40765a62016-07-25 18:19:49 +0000306 llvm_unreachable("I1 and I2 not found in BB");
307 }
Sebastian Pop41774802016-07-15 13:45:20 +0000308 // Return true when there are users of Def in BB.
309 bool hasMemoryUseOnPath(MemoryAccess *Def, const BasicBlock *BB,
310 const Instruction *OldPt) {
Sebastian Pop41774802016-07-15 13:45:20 +0000311 const BasicBlock *DefBB = Def->getBlock();
312 const BasicBlock *OldBB = OldPt->getParent();
313
David Majnemer4c66a712016-07-18 00:34:58 +0000314 for (User *U : Def->users())
315 if (auto *MU = dyn_cast<MemoryUse>(U)) {
316 BasicBlock *UBB = MU->getBlock();
Sebastian Pop41774802016-07-15 13:45:20 +0000317 // Only analyze uses in BB.
318 if (BB != UBB)
319 continue;
320
321 // A use in the same block as the Def is on the path.
322 if (UBB == DefBB) {
David Majnemer4c66a712016-07-18 00:34:58 +0000323 assert(MSSA->locallyDominates(Def, MU) && "def not dominating use");
Sebastian Pop41774802016-07-15 13:45:20 +0000324 return true;
325 }
326
327 if (UBB != OldBB)
328 return true;
329
330 // It is only harmful to hoist when the use is before OldPt.
Daniel Berlin40765a62016-07-25 18:19:49 +0000331 if (firstInBB(UBB, MU->getMemoryInst(), OldPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000332 return true;
333 }
334
335 return false;
336 }
337
338 // Return true when there are exception handling or loads of memory Def
339 // between OldPt and NewPt.
340
341 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
342 // return true when the counter NBBsOnAllPaths reaces 0, except when it is
343 // initialized to -1 which is unlimited.
344 bool hasEHOrLoadsOnPath(const Instruction *NewPt, const Instruction *OldPt,
345 MemoryAccess *Def, int &NBBsOnAllPaths) {
346 const BasicBlock *NewBB = NewPt->getParent();
347 const BasicBlock *OldBB = OldPt->getParent();
348 assert(DT->dominates(NewBB, OldBB) && "invalid path");
349 assert(DT->dominates(Def->getBlock(), NewBB) &&
350 "def does not dominate new hoisting point");
351
352 // Walk all basic blocks reachable in depth-first iteration on the inverse
353 // CFG from OldBB to NewBB. These blocks are all the blocks that may be
354 // executed between the execution of NewBB and OldBB. Hoisting an expression
355 // from OldBB into NewBB has to be safe on all execution paths.
356 for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
357 if (*I == NewBB) {
358 // Stop traversal when reaching HoistPt.
359 I.skipChildren();
360 continue;
361 }
362
363 // Impossible to hoist with exceptions on the path.
364 if (hasEH(*I))
365 return true;
366
367 // Check that we do not move a store past loads.
368 if (hasMemoryUseOnPath(Def, *I, OldPt))
369 return true;
370
371 // Stop walk once the limit is reached.
372 if (NBBsOnAllPaths == 0)
373 return true;
374
375 // -1 is unlimited number of blocks on all paths.
376 if (NBBsOnAllPaths != -1)
377 --NBBsOnAllPaths;
378
379 ++I;
380 }
381
382 return false;
383 }
384
385 // Return true when there are exception handling between HoistPt and BB.
386 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
387 // return true when the counter NBBsOnAllPaths reaches 0, except when it is
388 // initialized to -1 which is unlimited.
389 bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *BB,
390 int &NBBsOnAllPaths) {
391 assert(DT->dominates(HoistPt, BB) && "Invalid path");
392
393 // Walk all basic blocks reachable in depth-first iteration on
394 // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
395 // blocks that may be executed between the execution of NewHoistPt and
396 // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
397 // on all execution paths.
398 for (auto I = idf_begin(BB), E = idf_end(BB); I != E;) {
399 if (*I == HoistPt) {
400 // Stop traversal when reaching NewHoistPt.
401 I.skipChildren();
402 continue;
403 }
404
405 // Impossible to hoist with exceptions on the path.
406 if (hasEH(*I))
407 return true;
408
409 // Stop walk once the limit is reached.
410 if (NBBsOnAllPaths == 0)
411 return true;
412
413 // -1 is unlimited number of blocks on all paths.
414 if (NBBsOnAllPaths != -1)
415 --NBBsOnAllPaths;
416
417 ++I;
418 }
419
420 return false;
421 }
422
423 // Return true when it is safe to hoist a memory load or store U from OldPt
424 // to NewPt.
425 bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
426 MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) {
427
428 // In place hoisting is safe.
429 if (NewPt == OldPt)
430 return true;
431
432 const BasicBlock *NewBB = NewPt->getParent();
433 const BasicBlock *OldBB = OldPt->getParent();
434 const BasicBlock *UBB = U->getBlock();
435
436 // Check for dependences on the Memory SSA.
437 MemoryAccess *D = U->getDefiningAccess();
438 BasicBlock *DBB = D->getBlock();
439 if (DT->properlyDominates(NewBB, DBB))
440 // Cannot move the load or store to NewBB above its definition in DBB.
441 return false;
442
443 if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
David Majnemer4c66a712016-07-18 00:34:58 +0000444 if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
Daniel Berlin40765a62016-07-25 18:19:49 +0000445 if (firstInBB(DBB, NewPt, UD->getMemoryInst()))
Sebastian Pop41774802016-07-15 13:45:20 +0000446 // Cannot move the load or store to NewPt above its definition in D.
447 return false;
448
449 // Check for unsafe hoistings due to side effects.
450 if (K == InsKind::Store) {
451 if (hasEHOrLoadsOnPath(NewPt, OldPt, D, NBBsOnAllPaths))
452 return false;
453 } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
454 return false;
455
456 if (UBB == NewBB) {
457 if (DT->properlyDominates(DBB, NewBB))
458 return true;
459 assert(UBB == DBB);
460 assert(MSSA->locallyDominates(D, U));
461 }
462
463 // No side effects: it is safe to hoist.
464 return true;
465 }
466
467 // Return true when it is safe to hoist scalar instructions from BB1 and BB2
468 // to HoistBB.
469 bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB1,
470 const BasicBlock *BB2, int &NBBsOnAllPaths) {
471 // Check that the hoisted expression is needed on all paths. When HoistBB
472 // already contains an instruction to be hoisted, the expression is needed
473 // on all paths. Enable scalar hoisting at -Oz as it is safe to hoist
474 // scalars to a place where they are partially needed.
475 if (!OptForMinSize && BB1 != HoistBB &&
476 !hoistingFromAllPaths(HoistBB, BB1, BB2))
477 return false;
478
479 if (hasEHOnPath(HoistBB, BB1, NBBsOnAllPaths) ||
480 hasEHOnPath(HoistBB, BB2, NBBsOnAllPaths))
481 return false;
482
483 // Safe to hoist scalars from BB1 and BB2 to HoistBB.
484 return true;
485 }
486
487 // Each element of a hoisting list contains the basic block where to hoist and
488 // a list of instructions to be hoisted.
489 typedef std::pair<BasicBlock *, SmallVecInsn> HoistingPointInfo;
490 typedef SmallVector<HoistingPointInfo, 4> HoistingPointList;
491
492 // Partition InstructionsToHoist into a set of candidates which can share a
493 // common hoisting point. The partitions are collected in HPL. IsScalar is
494 // true when the instructions in InstructionsToHoist are scalars. IsLoad is
495 // true when the InstructionsToHoist are loads, false when they are stores.
496 void partitionCandidates(SmallVecImplInsn &InstructionsToHoist,
497 HoistingPointList &HPL, InsKind K) {
498 // No need to sort for two instructions.
499 if (InstructionsToHoist.size() > 2) {
500 SortByDFSIn Pred(DFSNumber);
501 std::sort(InstructionsToHoist.begin(), InstructionsToHoist.end(), Pred);
502 }
503
504 int NBBsOnAllPaths = MaxNumberOfBBSInPath;
505
506 SmallVecImplInsn::iterator II = InstructionsToHoist.begin();
507 SmallVecImplInsn::iterator Start = II;
508 Instruction *HoistPt = *II;
509 BasicBlock *HoistBB = HoistPt->getParent();
510 MemoryUseOrDef *UD;
511 if (K != InsKind::Scalar)
512 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(HoistPt));
513
514 for (++II; II != InstructionsToHoist.end(); ++II) {
515 Instruction *Insn = *II;
516 BasicBlock *BB = Insn->getParent();
517 BasicBlock *NewHoistBB;
518 Instruction *NewHoistPt;
519
520 if (BB == HoistBB) {
521 NewHoistBB = HoistBB;
Daniel Berlin40765a62016-07-25 18:19:49 +0000522 NewHoistPt = firstInBB(BB, Insn, HoistPt) ? Insn : HoistPt;
Sebastian Pop41774802016-07-15 13:45:20 +0000523 } else {
524 NewHoistBB = DT->findNearestCommonDominator(HoistBB, BB);
525 if (NewHoistBB == BB)
526 NewHoistPt = Insn;
527 else if (NewHoistBB == HoistBB)
528 NewHoistPt = HoistPt;
529 else
530 NewHoistPt = NewHoistBB->getTerminator();
531 }
532
533 if (K == InsKind::Scalar) {
534 if (safeToHoistScalar(NewHoistBB, HoistBB, BB, NBBsOnAllPaths)) {
535 // Extend HoistPt to NewHoistPt.
536 HoistPt = NewHoistPt;
537 HoistBB = NewHoistBB;
538 continue;
539 }
540 } else {
541 // When NewBB already contains an instruction to be hoisted, the
542 // expression is needed on all paths.
543 // Check that the hoisted expression is needed on all paths: it is
544 // unsafe to hoist loads to a place where there may be a path not
545 // loading from the same address: for instance there may be a branch on
546 // which the address of the load may not be initialized.
547 if ((HoistBB == NewHoistBB || BB == NewHoistBB ||
548 hoistingFromAllPaths(NewHoistBB, HoistBB, BB)) &&
549 // Also check that it is safe to move the load or store from HoistPt
550 // to NewHoistPt, and from Insn to NewHoistPt.
551 safeToHoistLdSt(NewHoistPt, HoistPt, UD, K, NBBsOnAllPaths) &&
552 safeToHoistLdSt(NewHoistPt, Insn,
553 cast<MemoryUseOrDef>(MSSA->getMemoryAccess(Insn)),
554 K, NBBsOnAllPaths)) {
555 // Extend HoistPt to NewHoistPt.
556 HoistPt = NewHoistPt;
557 HoistBB = NewHoistBB;
558 continue;
559 }
560 }
561
562 // At this point it is not safe to extend the current hoisting to
563 // NewHoistPt: save the hoisting list so far.
564 if (std::distance(Start, II) > 1)
David Majnemer4c66a712016-07-18 00:34:58 +0000565 HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
Sebastian Pop41774802016-07-15 13:45:20 +0000566
567 // Start over from BB.
568 Start = II;
569 if (K != InsKind::Scalar)
570 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(*Start));
571 HoistPt = Insn;
572 HoistBB = BB;
573 NBBsOnAllPaths = MaxNumberOfBBSInPath;
574 }
575
576 // Save the last partition.
577 if (std::distance(Start, II) > 1)
David Majnemer4c66a712016-07-18 00:34:58 +0000578 HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
Sebastian Pop41774802016-07-15 13:45:20 +0000579 }
580
581 // Initialize HPL from Map.
582 void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
583 InsKind K) {
David Majnemer4c66a712016-07-18 00:34:58 +0000584 for (const auto &Entry : Map) {
Sebastian Pop41774802016-07-15 13:45:20 +0000585 if (MaxHoistedThreshold != -1 && ++HoistedCtr > MaxHoistedThreshold)
586 return;
587
David Majnemer4c66a712016-07-18 00:34:58 +0000588 const SmallVecInsn &V = Entry.second;
Sebastian Pop41774802016-07-15 13:45:20 +0000589 if (V.size() < 2)
590 continue;
591
592 // Compute the insertion point and the list of expressions to be hoisted.
593 SmallVecInsn InstructionsToHoist;
594 for (auto I : V)
595 if (!hasEH(I->getParent()))
596 InstructionsToHoist.push_back(I);
597
David Majnemer4c66a712016-07-18 00:34:58 +0000598 if (!InstructionsToHoist.empty())
Sebastian Pop41774802016-07-15 13:45:20 +0000599 partitionCandidates(InstructionsToHoist, HPL, K);
600 }
601 }
602
603 // Return true when all operands of Instr are available at insertion point
604 // HoistPt. When limiting the number of hoisted expressions, one could hoist
605 // a load without hoisting its access function. So before hoisting any
606 // expression, make sure that all its operands are available at insert point.
607 bool allOperandsAvailable(const Instruction *I,
608 const BasicBlock *HoistPt) const {
David Majnemer4c66a712016-07-18 00:34:58 +0000609 for (const Use &Op : I->operands())
610 if (const auto *Inst = dyn_cast<Instruction>(&Op))
611 if (!DT->dominates(Inst->getParent(), HoistPt))
612 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000613
614 return true;
615 }
616
617 Instruction *firstOfTwo(Instruction *I, Instruction *J) const {
618 for (Instruction &I1 : *I->getParent())
619 if (&I1 == I || &I1 == J)
620 return &I1;
621 llvm_unreachable("Both I and J must be from same BB");
622 }
623
David Majnemer825e4ab2016-07-21 07:16:26 +0000624 bool makeOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
625 const SmallVecInsn &InstructionsToHoist) const {
Sebastian Pop41774802016-07-15 13:45:20 +0000626 // Check whether the GEP of a ld/st can be synthesized at HoistPt.
David Majnemerbd210122016-07-20 21:05:01 +0000627 GetElementPtrInst *Gep = nullptr;
Sebastian Pop41774802016-07-15 13:45:20 +0000628 Instruction *Val = nullptr;
David Majnemer4c66a712016-07-18 00:34:58 +0000629 if (auto *Ld = dyn_cast<LoadInst>(Repl))
David Majnemerbd210122016-07-20 21:05:01 +0000630 Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
David Majnemer4c66a712016-07-18 00:34:58 +0000631 if (auto *St = dyn_cast<StoreInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000632 Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
Sebastian Pop41774802016-07-15 13:45:20 +0000633 Val = dyn_cast<Instruction>(St->getValueOperand());
Sebastian Pop31fd5062016-07-21 23:22:10 +0000634 // Check that the stored value is available.
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000635 if (Val) {
636 if (isa<GetElementPtrInst>(Val)) {
637 // Check whether we can compute the GEP at HoistPt.
638 if (!allOperandsAvailable(Val, HoistPt))
639 return false;
640 } else if (!DT->dominates(Val->getParent(), HoistPt))
641 return false;
642 }
Sebastian Pop41774802016-07-15 13:45:20 +0000643 }
644
Sebastian Pop41774802016-07-15 13:45:20 +0000645 // Check whether we can compute the Gep at HoistPt.
Sebastian Pop31fd5062016-07-21 23:22:10 +0000646 if (!Gep || !allOperandsAvailable(Gep, HoistPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000647 return false;
648
649 // Copy the gep before moving the ld/st.
650 Instruction *ClonedGep = Gep->clone();
651 ClonedGep->insertBefore(HoistPt->getTerminator());
David Majnemer4808f262016-07-21 05:59:53 +0000652 // Conservatively discard any optimization hints, they may differ on the
653 // other paths.
David Majnemer68623a02016-07-25 02:21:25 +0000654 for (Instruction *OtherInst : InstructionsToHoist) {
655 GetElementPtrInst *OtherGep;
David Majnemer825e4ab2016-07-21 07:16:26 +0000656 if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
657 OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
658 else
659 OtherGep = cast<GetElementPtrInst>(
660 cast<StoreInst>(OtherInst)->getPointerOperand());
661 ClonedGep->intersectOptionalDataWith(OtherGep);
David Majnemer68623a02016-07-25 02:21:25 +0000662 combineKnownMetadata(ClonedGep, OtherGep);
David Majnemer825e4ab2016-07-21 07:16:26 +0000663 }
David Majnemer04854ab2016-07-18 19:14:14 +0000664 Repl->replaceUsesOfWith(Gep, ClonedGep);
Sebastian Pop41774802016-07-15 13:45:20 +0000665
Sebastian Pop31fd5062016-07-21 23:22:10 +0000666 // Also copy Val when it is a GEP.
667 if (Val && isa<GetElementPtrInst>(Val)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000668 Instruction *ClonedVal = Val->clone();
669 ClonedVal->insertBefore(HoistPt->getTerminator());
David Majnemer4808f262016-07-21 05:59:53 +0000670 // Conservatively discard any optimization hints, they may differ on the
671 // other paths.
David Majnemer68623a02016-07-25 02:21:25 +0000672 for (Instruction *OtherInst : InstructionsToHoist) {
673 auto *OtherVal =
David Majnemer825e4ab2016-07-21 07:16:26 +0000674 cast<Instruction>(cast<StoreInst>(OtherInst)->getValueOperand());
675 ClonedVal->intersectOptionalDataWith(OtherVal);
David Majnemer68623a02016-07-25 02:21:25 +0000676 combineKnownMetadata(ClonedVal, OtherVal);
David Majnemer825e4ab2016-07-21 07:16:26 +0000677 }
David Majnemer04854ab2016-07-18 19:14:14 +0000678 Repl->replaceUsesOfWith(Val, ClonedVal);
Sebastian Pop41774802016-07-15 13:45:20 +0000679 }
680
681 return true;
682 }
683
684 std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
685 unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
686 for (const HoistingPointInfo &HP : HPL) {
687 // Find out whether we already have one of the instructions in HoistPt,
688 // in which case we do not have to move it.
689 BasicBlock *HoistPt = HP.first;
690 const SmallVecInsn &InstructionsToHoist = HP.second;
691 Instruction *Repl = nullptr;
692 for (Instruction *I : InstructionsToHoist)
693 if (I->getParent() == HoistPt) {
694 // If there are two instructions in HoistPt to be hoisted in place:
695 // update Repl to be the first one, such that we can rename the uses
696 // of the second based on the first.
697 Repl = !Repl ? I : firstOfTwo(Repl, I);
698 }
699
700 if (Repl) {
701 // Repl is already in HoistPt: it remains in place.
702 assert(allOperandsAvailable(Repl, HoistPt) &&
703 "instruction depends on operands that are not available");
704 } else {
705 // When we do not find Repl in HoistPt, select the first in the list
706 // and move it to HoistPt.
707 Repl = InstructionsToHoist.front();
708
709 // We can move Repl in HoistPt only when all operands are available.
710 // The order in which hoistings are done may influence the availability
711 // of operands.
712 if (!allOperandsAvailable(Repl, HoistPt) &&
David Majnemer825e4ab2016-07-21 07:16:26 +0000713 !makeOperandsAvailable(Repl, HoistPt, InstructionsToHoist))
Sebastian Pop41774802016-07-15 13:45:20 +0000714 continue;
715 Repl->moveBefore(HoistPt->getTerminator());
David Majnemer4808f262016-07-21 05:59:53 +0000716 // TBAA may differ on one of the other paths, we need to get rid of
717 // anything which might conflict.
Sebastian Pop41774802016-07-15 13:45:20 +0000718 }
719
720 if (isa<LoadInst>(Repl))
721 ++NL;
722 else if (isa<StoreInst>(Repl))
723 ++NS;
724 else if (isa<CallInst>(Repl))
725 ++NC;
726 else // Scalar
727 ++NI;
728
729 // Remove and rename all other instructions.
730 for (Instruction *I : InstructionsToHoist)
731 if (I != Repl) {
732 ++NR;
David Majnemer47285692016-07-25 02:21:23 +0000733 if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
734 ReplacementLoad->setAlignment(
735 std::min(ReplacementLoad->getAlignment(),
736 cast<LoadInst>(I)->getAlignment()));
Sebastian Pop41774802016-07-15 13:45:20 +0000737 ++NumLoadsRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000738 } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
739 ReplacementStore->setAlignment(
740 std::min(ReplacementStore->getAlignment(),
741 cast<StoreInst>(I)->getAlignment()));
Sebastian Pop41774802016-07-15 13:45:20 +0000742 ++NumStoresRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000743 } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
744 ReplacementAlloca->setAlignment(
745 std::max(ReplacementAlloca->getAlignment(),
746 cast<AllocaInst>(I)->getAlignment()));
747 } else if (isa<CallInst>(Repl)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000748 ++NumCallsRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000749 }
David Majnemer4808f262016-07-21 05:59:53 +0000750 Repl->intersectOptionalDataWith(I);
David Majnemer68623a02016-07-25 02:21:25 +0000751 combineKnownMetadata(Repl, I);
Sebastian Pop41774802016-07-15 13:45:20 +0000752 I->replaceAllUsesWith(Repl);
753 I->eraseFromParent();
754 }
755 }
756
757 NumHoisted += NL + NS + NC + NI;
758 NumRemoved += NR;
759 NumLoadsHoisted += NL;
760 NumStoresHoisted += NS;
761 NumCallsHoisted += NC;
762 return {NI, NL + NC + NS};
763 }
764
765 // Hoist all expressions. Returns Number of scalars hoisted
766 // and number of non-scalars hoisted.
767 std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
768 InsnInfo II;
769 LoadInfo LI;
770 StoreInfo SI;
771 CallInfo CI;
772 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
Sebastian Pop38422b12016-07-26 00:15:08 +0000773 int InstructionNb = 0;
Sebastian Pop41774802016-07-15 13:45:20 +0000774 for (Instruction &I1 : *BB) {
Sebastian Pop38422b12016-07-26 00:15:08 +0000775 // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting
776 // deeper may increase the register pressure and compilation time.
777 if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB)
778 break;
779
David Majnemer4c66a712016-07-18 00:34:58 +0000780 if (auto *Load = dyn_cast<LoadInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000781 LI.insert(Load, VN);
David Majnemer4c66a712016-07-18 00:34:58 +0000782 else if (auto *Store = dyn_cast<StoreInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000783 SI.insert(Store, VN);
David Majnemer4c66a712016-07-18 00:34:58 +0000784 else if (auto *Call = dyn_cast<CallInst>(&I1)) {
785 if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000786 if (isa<DbgInfoIntrinsic>(Intr) ||
787 Intr->getIntrinsicID() == Intrinsic::assume)
788 continue;
789 }
790 if (Call->mayHaveSideEffects()) {
791 if (!OptForMinSize)
792 break;
793 // We may continue hoisting across calls which write to memory.
794 if (Call->mayThrow())
795 break;
796 }
797 CI.insert(Call, VN);
798 } else if (OptForMinSize || !isa<GetElementPtrInst>(&I1))
799 // Do not hoist scalars past calls that may write to memory because
800 // that could result in spills later. geps are handled separately.
801 // TODO: We can relax this for targets like AArch64 as they have more
802 // registers than X86.
803 II.insert(&I1, VN);
804 }
805 }
806
807 HoistingPointList HPL;
808 computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
809 computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
810 computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
811 computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
812 computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
813 computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
814 return hoist(HPL);
815 }
Sebastian Pop41774802016-07-15 13:45:20 +0000816};
817
818class GVNHoistLegacyPass : public FunctionPass {
819public:
820 static char ID;
821
822 GVNHoistLegacyPass() : FunctionPass(ID) {
823 initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
824 }
825
826 bool runOnFunction(Function &F) override {
Paul Robinson2d23c022016-07-19 22:57:14 +0000827 if (skipFunction(F))
828 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000829 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
830 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
831 auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
832
833 GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
834 return G.run(F);
835 }
836
837 void getAnalysisUsage(AnalysisUsage &AU) const override {
838 AU.addRequired<DominatorTreeWrapperPass>();
839 AU.addRequired<AAResultsWrapperPass>();
840 AU.addRequired<MemoryDependenceWrapperPass>();
841 AU.addPreserved<DominatorTreeWrapperPass>();
842 }
843};
844} // namespace
845
846PreservedAnalyses GVNHoistPass::run(Function &F,
847 AnalysisManager<Function> &AM) {
848 DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
849 AliasAnalysis &AA = AM.getResult<AAManager>(F);
850 MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
851
852 GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
853 if (!G.run(F))
854 return PreservedAnalyses::all();
855
856 PreservedAnalyses PA;
857 PA.preserve<DominatorTreeAnalysis>();
858 return PA;
859}
860
861char GVNHoistLegacyPass::ID = 0;
862INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
863 "Early GVN Hoisting of Expressions", false, false)
864INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
865INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
866INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
867INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
868 "Early GVN Hoisting of Expressions", false, false)
869
870FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }