blob: 19b8dd76942187b43389f8068d11c9c4ad971b59 [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 Pop91d4a302016-07-26 00:15:10 +000075 unsigned ADFS = DFSNumber.lookup(A);
76 unsigned BDFS = DFSNumber.lookup(B);
George Burgess IV9cf05462016-07-27 06:34:53 +000077 assert (ADFS && BDFS);
Sebastian Pop91d4a302016-07-26 00:15:10 +000078 return ADFS < BDFS;
Sebastian Pop41774802016-07-15 13:45:20 +000079 }
80};
81
David Majnemer04c7c222016-07-18 06:11:37 +000082// A map from a pair of VNs to all the instructions with those VNs.
83typedef DenseMap<std::pair<unsigned, unsigned>, SmallVector<Instruction *, 4>>
84 VNtoInsns;
85// An invalid value number Used when inserting a single value number into
86// VNtoInsns.
Reid Kleckner3498ad12016-07-18 18:53:50 +000087enum : unsigned { InvalidVN = ~2U };
Sebastian Pop41774802016-07-15 13:45:20 +000088
89// Records all scalar instructions candidate for code hoisting.
90class InsnInfo {
91 VNtoInsns VNtoScalars;
92
93public:
94 // Inserts I and its value number in VNtoScalars.
95 void insert(Instruction *I, GVN::ValueTable &VN) {
96 // Scalar instruction.
97 unsigned V = VN.lookupOrAdd(I);
David Majnemer04c7c222016-07-18 06:11:37 +000098 VNtoScalars[{V, InvalidVN}].push_back(I);
Sebastian Pop41774802016-07-15 13:45:20 +000099 }
100
101 const VNtoInsns &getVNTable() const { return VNtoScalars; }
102};
103
104// Records all load instructions candidate for code hoisting.
105class LoadInfo {
106 VNtoInsns VNtoLoads;
107
108public:
109 // Insert Load and the value number of its memory address in VNtoLoads.
110 void insert(LoadInst *Load, GVN::ValueTable &VN) {
111 if (Load->isSimple()) {
112 unsigned V = VN.lookupOrAdd(Load->getPointerOperand());
David Majnemer04c7c222016-07-18 06:11:37 +0000113 VNtoLoads[{V, InvalidVN}].push_back(Load);
Sebastian Pop41774802016-07-15 13:45:20 +0000114 }
115 }
116
117 const VNtoInsns &getVNTable() const { return VNtoLoads; }
118};
119
120// Records all store instructions candidate for code hoisting.
121class StoreInfo {
122 VNtoInsns VNtoStores;
123
124public:
125 // Insert the Store and a hash number of the store address and the stored
126 // value in VNtoStores.
127 void insert(StoreInst *Store, GVN::ValueTable &VN) {
128 if (!Store->isSimple())
129 return;
130 // Hash the store address and the stored value.
131 Value *Ptr = Store->getPointerOperand();
132 Value *Val = Store->getValueOperand();
David Majnemer04c7c222016-07-18 06:11:37 +0000133 VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store);
Sebastian Pop41774802016-07-15 13:45:20 +0000134 }
135
136 const VNtoInsns &getVNTable() const { return VNtoStores; }
137};
138
139// Records all call instructions candidate for code hoisting.
140class CallInfo {
141 VNtoInsns VNtoCallsScalars;
142 VNtoInsns VNtoCallsLoads;
143 VNtoInsns VNtoCallsStores;
144
145public:
146 // Insert Call and its value numbering in one of the VNtoCalls* containers.
147 void insert(CallInst *Call, GVN::ValueTable &VN) {
148 // A call that doesNotAccessMemory is handled as a Scalar,
149 // onlyReadsMemory will be handled as a Load instruction,
150 // all other calls will be handled as stores.
151 unsigned V = VN.lookupOrAdd(Call);
David Majnemer04c7c222016-07-18 06:11:37 +0000152 auto Entry = std::make_pair(V, InvalidVN);
Sebastian Pop41774802016-07-15 13:45:20 +0000153
154 if (Call->doesNotAccessMemory())
David Majnemer04c7c222016-07-18 06:11:37 +0000155 VNtoCallsScalars[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000156 else if (Call->onlyReadsMemory())
David Majnemer04c7c222016-07-18 06:11:37 +0000157 VNtoCallsLoads[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000158 else
David Majnemer04c7c222016-07-18 06:11:37 +0000159 VNtoCallsStores[Entry].push_back(Call);
Sebastian Pop41774802016-07-15 13:45:20 +0000160 }
161
162 const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; }
163
164 const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; }
165
166 const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; }
167};
168
169typedef DenseMap<const BasicBlock *, bool> BBSideEffectsSet;
170typedef SmallVector<Instruction *, 4> SmallVecInsn;
171typedef SmallVectorImpl<Instruction *> SmallVecImplInsn;
172
David Majnemer68623a02016-07-25 02:21:25 +0000173static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
174 static const unsigned KnownIDs[] = {
175 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
176 LLVMContext::MD_noalias, LLVMContext::MD_range,
177 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
178 LLVMContext::MD_invariant_group};
179 combineMetadata(ReplInst, I, KnownIDs);
180}
181
Sebastian Pop41774802016-07-15 13:45:20 +0000182// This pass hoists common computations across branches sharing common
183// dominator. The primary goal is to reduce the code size, and in some
184// cases reduce critical path (by exposing more ILP).
185class GVNHoist {
186public:
Daniel Berlin65af45d2016-07-25 17:24:22 +0000187 GVNHoist(DominatorTree *Dt, AliasAnalysis *Aa, MemoryDependenceResults *Md,
188 bool OptForMinSize)
Sebastian Pop55c30072016-07-27 05:48:12 +0000189 : DT(Dt), AA(Aa), MD(Md), OptForMinSize(OptForMinSize),
190 HoistingGeps(OptForMinSize), HoistedCtr(0) {}
Daniel Berlin65af45d2016-07-25 17:24:22 +0000191 bool run(Function &F) {
192 VN.setDomTree(DT);
193 VN.setAliasAnalysis(AA);
194 VN.setMemDep(MD);
195 bool Res = false;
196
Daniel Berlin65af45d2016-07-25 17:24:22 +0000197 // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
198 while (1) {
199 // FIXME: only compute MemorySSA once. We need to update the analysis in
200 // the same time as transforming the code.
201 MemorySSA M(F, AA, DT);
202 MSSA = &M;
203
Sebastian Pop91d4a302016-07-26 00:15:10 +0000204 // Perform DFS Numbering of instructions.
205 unsigned I = 0;
206 for (const BasicBlock *BB : depth_first(&F.getEntryBlock()))
207 for (auto &Inst: *BB)
208 DFSNumber.insert({&Inst, ++I});
209
Daniel Berlin65af45d2016-07-25 17:24:22 +0000210 auto HoistStat = hoistExpressions(F);
211 if (HoistStat.first + HoistStat.second == 0) {
212 return Res;
213 }
214 if (HoistStat.second > 0) {
215 // To address a limitation of the current GVN, we need to rerun the
216 // hoisting after we hoisted loads in order to be able to hoist all
217 // scalars dependent on the hoisted loads. Same for stores.
218 VN.clear();
219 }
220 Res = true;
Sebastian Pop91d4a302016-07-26 00:15:10 +0000221
222 // DFS numbers change when instructions are hoisted: clear and recompute.
223 DFSNumber.clear();
Daniel Berlin65af45d2016-07-25 17:24:22 +0000224 }
225
226 return Res;
227 }
228private:
Sebastian Pop41774802016-07-15 13:45:20 +0000229 GVN::ValueTable VN;
230 DominatorTree *DT;
231 AliasAnalysis *AA;
232 MemoryDependenceResults *MD;
233 const bool OptForMinSize;
Sebastian Pop55c30072016-07-27 05:48:12 +0000234 const bool HoistingGeps;
Sebastian Pop91d4a302016-07-26 00:15:10 +0000235 DenseMap<const Value *, unsigned> DFSNumber;
Sebastian Pop41774802016-07-15 13:45:20 +0000236 BBSideEffectsSet BBSideEffects;
237 MemorySSA *MSSA;
David Majnemeraa241782016-07-18 00:35:01 +0000238 int HoistedCtr;
239
Sebastian Pop41774802016-07-15 13:45:20 +0000240 enum InsKind { Unknown, Scalar, Load, Store };
241
Sebastian Pop41774802016-07-15 13:45:20 +0000242 // Return true when there are exception handling in BB.
243 bool hasEH(const BasicBlock *BB) {
244 auto It = BBSideEffects.find(BB);
245 if (It != BBSideEffects.end())
246 return It->second;
247
248 if (BB->isEHPad() || BB->hasAddressTaken()) {
249 BBSideEffects[BB] = true;
250 return true;
251 }
252
253 if (BB->getTerminator()->mayThrow()) {
254 BBSideEffects[BB] = true;
255 return true;
256 }
257
258 BBSideEffects[BB] = false;
259 return false;
260 }
261
262 // Return true when all paths from A to the end of the function pass through
263 // either B or C.
264 bool hoistingFromAllPaths(const BasicBlock *A, const BasicBlock *B,
265 const BasicBlock *C) {
266 // We fully copy the WL in order to be able to remove items from it.
267 SmallPtrSet<const BasicBlock *, 2> WL;
268 WL.insert(B);
269 WL.insert(C);
270
271 for (auto It = df_begin(A), E = df_end(A); It != E;) {
272 // There exists a path from A to the exit of the function if we are still
273 // iterating in DF traversal and we removed all instructions from the work
274 // list.
275 if (WL.empty())
276 return false;
277
278 const BasicBlock *BB = *It;
279 if (WL.erase(BB)) {
280 // Stop DFS traversal when BB is in the work list.
281 It.skipChildren();
282 continue;
283 }
284
285 // Check for end of function, calls that do not return, etc.
286 if (!isGuaranteedToTransferExecutionToSuccessor(BB->getTerminator()))
287 return false;
288
289 // Increment DFS traversal when not skipping children.
290 ++It;
291 }
292
293 return true;
294 }
295
296 /* Return true when I1 appears before I2 in the instructions of BB. */
Sebastian Pop91d4a302016-07-26 00:15:10 +0000297 bool firstInBB(const Instruction *I1, const Instruction *I2) {
298 assert (I1->getParent() == I2->getParent());
299 unsigned I1DFS = DFSNumber.lookup(I1);
300 unsigned I2DFS = DFSNumber.lookup(I2);
301 assert (I1DFS && I2DFS);
302 return I1DFS < I2DFS;
Daniel Berlin40765a62016-07-25 18:19:49 +0000303 }
Sebastian Pop91d4a302016-07-26 00:15:10 +0000304
Sebastian Pop41774802016-07-15 13:45:20 +0000305 // Return true when there are users of Def in BB.
306 bool hasMemoryUseOnPath(MemoryAccess *Def, const BasicBlock *BB,
307 const Instruction *OldPt) {
Sebastian Pop41774802016-07-15 13:45:20 +0000308 const BasicBlock *DefBB = Def->getBlock();
309 const BasicBlock *OldBB = OldPt->getParent();
310
David Majnemer4c66a712016-07-18 00:34:58 +0000311 for (User *U : Def->users())
312 if (auto *MU = dyn_cast<MemoryUse>(U)) {
313 BasicBlock *UBB = MU->getBlock();
Sebastian Pop41774802016-07-15 13:45:20 +0000314 // Only analyze uses in BB.
315 if (BB != UBB)
316 continue;
317
318 // A use in the same block as the Def is on the path.
319 if (UBB == DefBB) {
David Majnemer4c66a712016-07-18 00:34:58 +0000320 assert(MSSA->locallyDominates(Def, MU) && "def not dominating use");
Sebastian Pop41774802016-07-15 13:45:20 +0000321 return true;
322 }
323
324 if (UBB != OldBB)
325 return true;
326
327 // It is only harmful to hoist when the use is before OldPt.
Sebastian Pop91d4a302016-07-26 00:15:10 +0000328 if (firstInBB(MU->getMemoryInst(), OldPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000329 return true;
330 }
331
332 return false;
333 }
334
335 // Return true when there are exception handling or loads of memory Def
336 // between OldPt and NewPt.
337
338 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
339 // return true when the counter NBBsOnAllPaths reaces 0, except when it is
340 // initialized to -1 which is unlimited.
341 bool hasEHOrLoadsOnPath(const Instruction *NewPt, const Instruction *OldPt,
342 MemoryAccess *Def, int &NBBsOnAllPaths) {
343 const BasicBlock *NewBB = NewPt->getParent();
344 const BasicBlock *OldBB = OldPt->getParent();
345 assert(DT->dominates(NewBB, OldBB) && "invalid path");
346 assert(DT->dominates(Def->getBlock(), NewBB) &&
347 "def does not dominate new hoisting point");
348
349 // Walk all basic blocks reachable in depth-first iteration on the inverse
350 // CFG from OldBB to NewBB. These blocks are all the blocks that may be
351 // executed between the execution of NewBB and OldBB. Hoisting an expression
352 // from OldBB into NewBB has to be safe on all execution paths.
353 for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
354 if (*I == NewBB) {
355 // Stop traversal when reaching HoistPt.
356 I.skipChildren();
357 continue;
358 }
359
360 // Impossible to hoist with exceptions on the path.
361 if (hasEH(*I))
362 return true;
363
364 // Check that we do not move a store past loads.
365 if (hasMemoryUseOnPath(Def, *I, OldPt))
366 return true;
367
368 // Stop walk once the limit is reached.
369 if (NBBsOnAllPaths == 0)
370 return true;
371
372 // -1 is unlimited number of blocks on all paths.
373 if (NBBsOnAllPaths != -1)
374 --NBBsOnAllPaths;
375
376 ++I;
377 }
378
379 return false;
380 }
381
382 // Return true when there are exception handling between HoistPt and BB.
383 // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
384 // return true when the counter NBBsOnAllPaths reaches 0, except when it is
385 // initialized to -1 which is unlimited.
386 bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *BB,
387 int &NBBsOnAllPaths) {
388 assert(DT->dominates(HoistPt, BB) && "Invalid path");
389
390 // Walk all basic blocks reachable in depth-first iteration on
391 // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
392 // blocks that may be executed between the execution of NewHoistPt and
393 // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
394 // on all execution paths.
395 for (auto I = idf_begin(BB), E = idf_end(BB); I != E;) {
396 if (*I == HoistPt) {
397 // Stop traversal when reaching NewHoistPt.
398 I.skipChildren();
399 continue;
400 }
401
402 // Impossible to hoist with exceptions on the path.
403 if (hasEH(*I))
404 return true;
405
406 // Stop walk once the limit is reached.
407 if (NBBsOnAllPaths == 0)
408 return true;
409
410 // -1 is unlimited number of blocks on all paths.
411 if (NBBsOnAllPaths != -1)
412 --NBBsOnAllPaths;
413
414 ++I;
415 }
416
417 return false;
418 }
419
420 // Return true when it is safe to hoist a memory load or store U from OldPt
421 // to NewPt.
422 bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
423 MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) {
424
425 // In place hoisting is safe.
426 if (NewPt == OldPt)
427 return true;
428
429 const BasicBlock *NewBB = NewPt->getParent();
430 const BasicBlock *OldBB = OldPt->getParent();
431 const BasicBlock *UBB = U->getBlock();
432
433 // Check for dependences on the Memory SSA.
434 MemoryAccess *D = U->getDefiningAccess();
435 BasicBlock *DBB = D->getBlock();
436 if (DT->properlyDominates(NewBB, DBB))
437 // Cannot move the load or store to NewBB above its definition in DBB.
438 return false;
439
440 if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
David Majnemer4c66a712016-07-18 00:34:58 +0000441 if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
Sebastian Pop91d4a302016-07-26 00:15:10 +0000442 if (firstInBB(NewPt, UD->getMemoryInst()))
Sebastian Pop41774802016-07-15 13:45:20 +0000443 // Cannot move the load or store to NewPt above its definition in D.
444 return false;
445
446 // Check for unsafe hoistings due to side effects.
447 if (K == InsKind::Store) {
448 if (hasEHOrLoadsOnPath(NewPt, OldPt, D, NBBsOnAllPaths))
449 return false;
450 } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
451 return false;
452
453 if (UBB == NewBB) {
454 if (DT->properlyDominates(DBB, NewBB))
455 return true;
456 assert(UBB == DBB);
457 assert(MSSA->locallyDominates(D, U));
458 }
459
460 // No side effects: it is safe to hoist.
461 return true;
462 }
463
464 // Return true when it is safe to hoist scalar instructions from BB1 and BB2
465 // to HoistBB.
466 bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB1,
467 const BasicBlock *BB2, int &NBBsOnAllPaths) {
468 // Check that the hoisted expression is needed on all paths. When HoistBB
469 // already contains an instruction to be hoisted, the expression is needed
470 // on all paths. Enable scalar hoisting at -Oz as it is safe to hoist
471 // scalars to a place where they are partially needed.
472 if (!OptForMinSize && BB1 != HoistBB &&
473 !hoistingFromAllPaths(HoistBB, BB1, BB2))
474 return false;
475
476 if (hasEHOnPath(HoistBB, BB1, NBBsOnAllPaths) ||
477 hasEHOnPath(HoistBB, BB2, NBBsOnAllPaths))
478 return false;
479
480 // Safe to hoist scalars from BB1 and BB2 to HoistBB.
481 return true;
482 }
483
484 // Each element of a hoisting list contains the basic block where to hoist and
485 // a list of instructions to be hoisted.
486 typedef std::pair<BasicBlock *, SmallVecInsn> HoistingPointInfo;
487 typedef SmallVector<HoistingPointInfo, 4> HoistingPointList;
488
489 // Partition InstructionsToHoist into a set of candidates which can share a
490 // common hoisting point. The partitions are collected in HPL. IsScalar is
491 // true when the instructions in InstructionsToHoist are scalars. IsLoad is
492 // true when the InstructionsToHoist are loads, false when they are stores.
493 void partitionCandidates(SmallVecImplInsn &InstructionsToHoist,
494 HoistingPointList &HPL, InsKind K) {
495 // No need to sort for two instructions.
496 if (InstructionsToHoist.size() > 2) {
497 SortByDFSIn Pred(DFSNumber);
498 std::sort(InstructionsToHoist.begin(), InstructionsToHoist.end(), Pred);
499 }
500
501 int NBBsOnAllPaths = MaxNumberOfBBSInPath;
502
503 SmallVecImplInsn::iterator II = InstructionsToHoist.begin();
504 SmallVecImplInsn::iterator Start = II;
505 Instruction *HoistPt = *II;
506 BasicBlock *HoistBB = HoistPt->getParent();
507 MemoryUseOrDef *UD;
508 if (K != InsKind::Scalar)
509 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(HoistPt));
510
511 for (++II; II != InstructionsToHoist.end(); ++II) {
512 Instruction *Insn = *II;
513 BasicBlock *BB = Insn->getParent();
514 BasicBlock *NewHoistBB;
515 Instruction *NewHoistPt;
516
517 if (BB == HoistBB) {
518 NewHoistBB = HoistBB;
Sebastian Pop91d4a302016-07-26 00:15:10 +0000519 NewHoistPt = firstInBB(Insn, HoistPt) ? Insn : HoistPt;
Sebastian Pop41774802016-07-15 13:45:20 +0000520 } else {
521 NewHoistBB = DT->findNearestCommonDominator(HoistBB, BB);
522 if (NewHoistBB == BB)
523 NewHoistPt = Insn;
524 else if (NewHoistBB == HoistBB)
525 NewHoistPt = HoistPt;
526 else
527 NewHoistPt = NewHoistBB->getTerminator();
528 }
529
530 if (K == InsKind::Scalar) {
531 if (safeToHoistScalar(NewHoistBB, HoistBB, BB, NBBsOnAllPaths)) {
532 // Extend HoistPt to NewHoistPt.
533 HoistPt = NewHoistPt;
534 HoistBB = NewHoistBB;
535 continue;
536 }
537 } else {
538 // When NewBB already contains an instruction to be hoisted, the
539 // expression is needed on all paths.
540 // Check that the hoisted expression is needed on all paths: it is
541 // unsafe to hoist loads to a place where there may be a path not
542 // loading from the same address: for instance there may be a branch on
543 // which the address of the load may not be initialized.
544 if ((HoistBB == NewHoistBB || BB == NewHoistBB ||
545 hoistingFromAllPaths(NewHoistBB, HoistBB, BB)) &&
546 // Also check that it is safe to move the load or store from HoistPt
547 // to NewHoistPt, and from Insn to NewHoistPt.
548 safeToHoistLdSt(NewHoistPt, HoistPt, UD, K, NBBsOnAllPaths) &&
549 safeToHoistLdSt(NewHoistPt, Insn,
550 cast<MemoryUseOrDef>(MSSA->getMemoryAccess(Insn)),
551 K, NBBsOnAllPaths)) {
552 // Extend HoistPt to NewHoistPt.
553 HoistPt = NewHoistPt;
554 HoistBB = NewHoistBB;
555 continue;
556 }
557 }
558
559 // At this point it is not safe to extend the current hoisting to
560 // NewHoistPt: save the hoisting list so far.
561 if (std::distance(Start, II) > 1)
David Majnemer4c66a712016-07-18 00:34:58 +0000562 HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
Sebastian Pop41774802016-07-15 13:45:20 +0000563
564 // Start over from BB.
565 Start = II;
566 if (K != InsKind::Scalar)
567 UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(*Start));
568 HoistPt = Insn;
569 HoistBB = BB;
570 NBBsOnAllPaths = MaxNumberOfBBSInPath;
571 }
572
573 // Save the last partition.
574 if (std::distance(Start, II) > 1)
David Majnemer4c66a712016-07-18 00:34:58 +0000575 HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
Sebastian Pop41774802016-07-15 13:45:20 +0000576 }
577
578 // Initialize HPL from Map.
579 void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
580 InsKind K) {
David Majnemer4c66a712016-07-18 00:34:58 +0000581 for (const auto &Entry : Map) {
Sebastian Pop41774802016-07-15 13:45:20 +0000582 if (MaxHoistedThreshold != -1 && ++HoistedCtr > MaxHoistedThreshold)
583 return;
584
David Majnemer4c66a712016-07-18 00:34:58 +0000585 const SmallVecInsn &V = Entry.second;
Sebastian Pop41774802016-07-15 13:45:20 +0000586 if (V.size() < 2)
587 continue;
588
589 // Compute the insertion point and the list of expressions to be hoisted.
590 SmallVecInsn InstructionsToHoist;
591 for (auto I : V)
592 if (!hasEH(I->getParent()))
593 InstructionsToHoist.push_back(I);
594
David Majnemer4c66a712016-07-18 00:34:58 +0000595 if (!InstructionsToHoist.empty())
Sebastian Pop41774802016-07-15 13:45:20 +0000596 partitionCandidates(InstructionsToHoist, HPL, K);
597 }
598 }
599
600 // Return true when all operands of Instr are available at insertion point
601 // HoistPt. When limiting the number of hoisted expressions, one could hoist
602 // a load without hoisting its access function. So before hoisting any
603 // expression, make sure that all its operands are available at insert point.
604 bool allOperandsAvailable(const Instruction *I,
605 const BasicBlock *HoistPt) const {
David Majnemer4c66a712016-07-18 00:34:58 +0000606 for (const Use &Op : I->operands())
607 if (const auto *Inst = dyn_cast<Instruction>(&Op))
608 if (!DT->dominates(Inst->getParent(), HoistPt))
609 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000610
611 return true;
612 }
613
Sebastian Pop55c30072016-07-27 05:48:12 +0000614 // Same as allOperandsAvailable with recursive check for GEP operands.
615 bool allGepOperandsAvailable(const Instruction *I,
616 const BasicBlock *HoistPt) const {
617 for (const Use &Op : I->operands())
618 if (const auto *Inst = dyn_cast<Instruction>(&Op))
619 if (!DT->dominates(Inst->getParent(), HoistPt)) {
620 if (const GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Inst)) {
621 if (!allGepOperandsAvailable(GepOp, HoistPt))
622 return false;
623 // Gep is available if all operands of GepOp are available.
624 } else {
625 // Gep is not available if it has operands other than GEPs that are
626 // defined in blocks not dominating HoistPt.
627 return false;
628 }
629 }
630 return true;
631 }
632
633 // Make all operands of the GEP available.
634 void makeGepsAvailable(Instruction *Repl, BasicBlock *HoistPt,
635 const SmallVecInsn &InstructionsToHoist,
636 Instruction *Gep) const {
637 assert(allGepOperandsAvailable(Gep, HoistPt) && "GEP operands not available");
638
639 Instruction *ClonedGep = Gep->clone();
640 for (unsigned i = 0, e = Gep->getNumOperands(); i != e; ++i)
641 if (Instruction *Op = dyn_cast<Instruction>(Gep->getOperand(i))) {
642
643 // Check whether the operand is already available.
644 if (DT->dominates(Op->getParent(), HoistPt))
645 continue;
646
647 // As a GEP can refer to other GEPs, recursively make all the operands
648 // of this GEP available at HoistPt.
649 if (GetElementPtrInst *GepOp = dyn_cast<GetElementPtrInst>(Op))
650 makeGepsAvailable(ClonedGep, HoistPt, InstructionsToHoist, GepOp);
651 }
652
653 // Copy Gep and replace its uses in Repl with ClonedGep.
654 ClonedGep->insertBefore(HoistPt->getTerminator());
655
656 // Conservatively discard any optimization hints, they may differ on the
657 // other paths.
658 ClonedGep->dropUnknownNonDebugMetadata();
659
660 // If we have optimization hints which agree with each other along different
661 // paths, preserve them.
662 for (const Instruction *OtherInst : InstructionsToHoist) {
663 const GetElementPtrInst *OtherGep;
664 if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
665 OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
666 else
667 OtherGep = cast<GetElementPtrInst>(
668 cast<StoreInst>(OtherInst)->getPointerOperand());
669 ClonedGep->intersectOptionalDataWith(OtherGep);
670 }
671
672 // Replace uses of Gep with ClonedGep in Repl.
673 Repl->replaceUsesOfWith(Gep, ClonedGep);
674 }
675
676 // In the case Repl is a load or a store, we make all their GEPs
677 // available: GEPs are not hoisted by default to avoid the address
678 // computations to be hoisted without the associated load or store.
679 bool makeGepOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
680 const SmallVecInsn &InstructionsToHoist) const {
Sebastian Pop41774802016-07-15 13:45:20 +0000681 // Check whether the GEP of a ld/st can be synthesized at HoistPt.
David Majnemerbd210122016-07-20 21:05:01 +0000682 GetElementPtrInst *Gep = nullptr;
Sebastian Pop41774802016-07-15 13:45:20 +0000683 Instruction *Val = nullptr;
Sebastian Pop55c30072016-07-27 05:48:12 +0000684 if (auto *Ld = dyn_cast<LoadInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000685 Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
Sebastian Pop55c30072016-07-27 05:48:12 +0000686 } else if (auto *St = dyn_cast<StoreInst>(Repl)) {
David Majnemerbd210122016-07-20 21:05:01 +0000687 Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
Sebastian Pop41774802016-07-15 13:45:20 +0000688 Val = dyn_cast<Instruction>(St->getValueOperand());
Sebastian Pop31fd5062016-07-21 23:22:10 +0000689 // Check that the stored value is available.
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000690 if (Val) {
691 if (isa<GetElementPtrInst>(Val)) {
692 // Check whether we can compute the GEP at HoistPt.
Sebastian Pop55c30072016-07-27 05:48:12 +0000693 if (!allGepOperandsAvailable(Val, HoistPt))
Sebastian Pop0e2cec02016-07-22 00:07:01 +0000694 return false;
695 } else if (!DT->dominates(Val->getParent(), HoistPt))
696 return false;
697 }
Sebastian Pop41774802016-07-15 13:45:20 +0000698 }
699
Sebastian Pop41774802016-07-15 13:45:20 +0000700 // Check whether we can compute the Gep at HoistPt.
Sebastian Pop55c30072016-07-27 05:48:12 +0000701 if (!Gep || !allGepOperandsAvailable(Gep, HoistPt))
Sebastian Pop41774802016-07-15 13:45:20 +0000702 return false;
703
Sebastian Pop55c30072016-07-27 05:48:12 +0000704 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Gep);
Sebastian Pop41774802016-07-15 13:45:20 +0000705
Sebastian Pop55c30072016-07-27 05:48:12 +0000706 if (Val && isa<GetElementPtrInst>(Val))
707 makeGepsAvailable(Repl, HoistPt, InstructionsToHoist, Val);
Sebastian Pop41774802016-07-15 13:45:20 +0000708
709 return true;
710 }
711
712 std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
713 unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
714 for (const HoistingPointInfo &HP : HPL) {
715 // Find out whether we already have one of the instructions in HoistPt,
716 // in which case we do not have to move it.
717 BasicBlock *HoistPt = HP.first;
718 const SmallVecInsn &InstructionsToHoist = HP.second;
719 Instruction *Repl = nullptr;
720 for (Instruction *I : InstructionsToHoist)
Sebastian Pop586d3ea2016-07-27 05:13:52 +0000721 if (I->getParent() == HoistPt)
Sebastian Pop41774802016-07-15 13:45:20 +0000722 // If there are two instructions in HoistPt to be hoisted in place:
723 // update Repl to be the first one, such that we can rename the uses
724 // of the second based on the first.
Sebastian Pop586d3ea2016-07-27 05:13:52 +0000725 if (!Repl || firstInBB(I, Repl))
726 Repl = I;
Sebastian Pop41774802016-07-15 13:45:20 +0000727
728 if (Repl) {
729 // Repl is already in HoistPt: it remains in place.
730 assert(allOperandsAvailable(Repl, HoistPt) &&
731 "instruction depends on operands that are not available");
732 } else {
733 // When we do not find Repl in HoistPt, select the first in the list
734 // and move it to HoistPt.
735 Repl = InstructionsToHoist.front();
736
737 // We can move Repl in HoistPt only when all operands are available.
Sebastian Pop55c30072016-07-27 05:48:12 +0000738 // When not HoistingGeps we need to copy the GEPs now.
Sebastian Pop41774802016-07-15 13:45:20 +0000739 // The order in which hoistings are done may influence the availability
740 // of operands.
Sebastian Pop55c30072016-07-27 05:48:12 +0000741 if (!allOperandsAvailable(Repl, HoistPt) && !HoistingGeps &&
742 !makeGepOperandsAvailable(Repl, HoistPt, InstructionsToHoist))
Sebastian Pop41774802016-07-15 13:45:20 +0000743 continue;
Sebastian Pop55c30072016-07-27 05:48:12 +0000744
Sebastian Pop41774802016-07-15 13:45:20 +0000745 Repl->moveBefore(HoistPt->getTerminator());
746 }
747
748 if (isa<LoadInst>(Repl))
749 ++NL;
750 else if (isa<StoreInst>(Repl))
751 ++NS;
752 else if (isa<CallInst>(Repl))
753 ++NC;
754 else // Scalar
755 ++NI;
756
757 // Remove and rename all other instructions.
758 for (Instruction *I : InstructionsToHoist)
759 if (I != Repl) {
760 ++NR;
David Majnemer47285692016-07-25 02:21:23 +0000761 if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
762 ReplacementLoad->setAlignment(
763 std::min(ReplacementLoad->getAlignment(),
764 cast<LoadInst>(I)->getAlignment()));
Sebastian Pop41774802016-07-15 13:45:20 +0000765 ++NumLoadsRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000766 } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
767 ReplacementStore->setAlignment(
768 std::min(ReplacementStore->getAlignment(),
769 cast<StoreInst>(I)->getAlignment()));
Sebastian Pop41774802016-07-15 13:45:20 +0000770 ++NumStoresRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000771 } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
772 ReplacementAlloca->setAlignment(
773 std::max(ReplacementAlloca->getAlignment(),
774 cast<AllocaInst>(I)->getAlignment()));
775 } else if (isa<CallInst>(Repl)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000776 ++NumCallsRemoved;
David Majnemer47285692016-07-25 02:21:23 +0000777 }
David Majnemer4808f262016-07-21 05:59:53 +0000778 Repl->intersectOptionalDataWith(I);
David Majnemer68623a02016-07-25 02:21:25 +0000779 combineKnownMetadata(Repl, I);
Sebastian Pop41774802016-07-15 13:45:20 +0000780 I->replaceAllUsesWith(Repl);
781 I->eraseFromParent();
782 }
783 }
784
785 NumHoisted += NL + NS + NC + NI;
786 NumRemoved += NR;
787 NumLoadsHoisted += NL;
788 NumStoresHoisted += NS;
789 NumCallsHoisted += NC;
790 return {NI, NL + NC + NS};
791 }
792
793 // Hoist all expressions. Returns Number of scalars hoisted
794 // and number of non-scalars hoisted.
795 std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
796 InsnInfo II;
797 LoadInfo LI;
798 StoreInfo SI;
799 CallInfo CI;
800 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
Sebastian Pop38422b12016-07-26 00:15:08 +0000801 int InstructionNb = 0;
Sebastian Pop41774802016-07-15 13:45:20 +0000802 for (Instruction &I1 : *BB) {
Sebastian Pop38422b12016-07-26 00:15:08 +0000803 // Only hoist the first instructions in BB up to MaxDepthInBB. Hoisting
804 // deeper may increase the register pressure and compilation time.
805 if (MaxDepthInBB != -1 && InstructionNb++ >= MaxDepthInBB)
806 break;
807
David Majnemer4c66a712016-07-18 00:34:58 +0000808 if (auto *Load = dyn_cast<LoadInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000809 LI.insert(Load, VN);
David Majnemer4c66a712016-07-18 00:34:58 +0000810 else if (auto *Store = dyn_cast<StoreInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000811 SI.insert(Store, VN);
David Majnemer4c66a712016-07-18 00:34:58 +0000812 else if (auto *Call = dyn_cast<CallInst>(&I1)) {
813 if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
Sebastian Pop41774802016-07-15 13:45:20 +0000814 if (isa<DbgInfoIntrinsic>(Intr) ||
815 Intr->getIntrinsicID() == Intrinsic::assume)
816 continue;
817 }
818 if (Call->mayHaveSideEffects()) {
819 if (!OptForMinSize)
820 break;
821 // We may continue hoisting across calls which write to memory.
822 if (Call->mayThrow())
823 break;
824 }
825 CI.insert(Call, VN);
Sebastian Pop55c30072016-07-27 05:48:12 +0000826 } else if (HoistingGeps || !isa<GetElementPtrInst>(&I1))
Sebastian Pop41774802016-07-15 13:45:20 +0000827 // Do not hoist scalars past calls that may write to memory because
828 // that could result in spills later. geps are handled separately.
829 // TODO: We can relax this for targets like AArch64 as they have more
830 // registers than X86.
831 II.insert(&I1, VN);
832 }
833 }
834
835 HoistingPointList HPL;
836 computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
837 computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
838 computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
839 computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
840 computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
841 computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
842 return hoist(HPL);
843 }
Sebastian Pop41774802016-07-15 13:45:20 +0000844};
845
846class GVNHoistLegacyPass : public FunctionPass {
847public:
848 static char ID;
849
850 GVNHoistLegacyPass() : FunctionPass(ID) {
851 initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
852 }
853
854 bool runOnFunction(Function &F) override {
Paul Robinson2d23c022016-07-19 22:57:14 +0000855 if (skipFunction(F))
856 return false;
Sebastian Pop41774802016-07-15 13:45:20 +0000857 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
858 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
859 auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
860
861 GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
862 return G.run(F);
863 }
864
865 void getAnalysisUsage(AnalysisUsage &AU) const override {
866 AU.addRequired<DominatorTreeWrapperPass>();
867 AU.addRequired<AAResultsWrapperPass>();
868 AU.addRequired<MemoryDependenceWrapperPass>();
869 AU.addPreserved<DominatorTreeWrapperPass>();
870 }
871};
872} // namespace
873
874PreservedAnalyses GVNHoistPass::run(Function &F,
875 AnalysisManager<Function> &AM) {
876 DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
877 AliasAnalysis &AA = AM.getResult<AAManager>(F);
878 MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
879
880 GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
881 if (!G.run(F))
882 return PreservedAnalyses::all();
883
884 PreservedAnalyses PA;
885 PA.preserve<DominatorTreeAnalysis>();
886 return PA;
887}
888
889char GVNHoistLegacyPass::ID = 0;
890INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
891 "Early GVN Hoisting of Expressions", false, false)
892INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
893INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
894INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
895INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
896 "Early GVN Hoisting of Expressions", false, false)
897
898FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }