xref: /llvm-project/llvm/lib/Transforms/Scalar/GVNHoist.cpp (revision 65af45de03d520093fe032b89f570684274d518b)
1 //===- 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"
26 #include "llvm/Transforms/Utils/Local.h"
27 #include "llvm/Transforms/Utils/MemorySSA.h"
28 
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "gvn-hoist"
32 
33 STATISTIC(NumHoisted, "Number of instructions hoisted");
34 STATISTIC(NumRemoved, "Number of instructions removed");
35 STATISTIC(NumLoadsHoisted, "Number of loads hoisted");
36 STATISTIC(NumLoadsRemoved, "Number of loads removed");
37 STATISTIC(NumStoresHoisted, "Number of stores hoisted");
38 STATISTIC(NumStoresRemoved, "Number of stores removed");
39 STATISTIC(NumCallsHoisted, "Number of calls hoisted");
40 STATISTIC(NumCallsRemoved, "Number of calls removed");
41 
42 static 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)"));
46 static 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 
51 namespace {
52 
53 // Provides a sorting function based on the execution order of two instructions.
54 struct SortByDFSIn {
55 private:
56   DenseMap<const BasicBlock *, unsigned> &DFSNumber;
57 
58 public:
59   SortByDFSIn(DenseMap<const BasicBlock *, unsigned> &D) : DFSNumber(D) {}
60 
61   // Returns true when A executes before B.
62   bool operator()(const Instruction *A, const Instruction *B) const {
63     // FIXME: libc++ has a std::sort() algorithm that will call the compare
64     // function on the same element.  Once PR20837 is fixed and some more years
65     // pass by and all the buildbots have moved to a corrected std::sort(),
66     // enable the following assert:
67     //
68     // assert(A != B);
69 
70     const BasicBlock *BA = A->getParent();
71     const BasicBlock *BB = B->getParent();
72     unsigned NA = DFSNumber[BA];
73     unsigned NB = DFSNumber[BB];
74     if (NA < NB)
75       return true;
76     if (NA == NB) {
77       // Sort them in the order they occur in the same basic block.
78       BasicBlock::const_iterator AI(A), BI(B);
79       return std::distance(AI, BI) < 0;
80     }
81     return false;
82   }
83 };
84 
85 // A map from a pair of VNs to all the instructions with those VNs.
86 typedef DenseMap<std::pair<unsigned, unsigned>, SmallVector<Instruction *, 4>>
87     VNtoInsns;
88 // An invalid value number Used when inserting a single value number into
89 // VNtoInsns.
90 enum : unsigned { InvalidVN = ~2U };
91 
92 // Records all scalar instructions candidate for code hoisting.
93 class InsnInfo {
94   VNtoInsns VNtoScalars;
95 
96 public:
97   // Inserts I and its value number in VNtoScalars.
98   void insert(Instruction *I, GVN::ValueTable &VN) {
99     // Scalar instruction.
100     unsigned V = VN.lookupOrAdd(I);
101     VNtoScalars[{V, InvalidVN}].push_back(I);
102   }
103 
104   const VNtoInsns &getVNTable() const { return VNtoScalars; }
105 };
106 
107 // Records all load instructions candidate for code hoisting.
108 class LoadInfo {
109   VNtoInsns VNtoLoads;
110 
111 public:
112   // Insert Load and the value number of its memory address in VNtoLoads.
113   void insert(LoadInst *Load, GVN::ValueTable &VN) {
114     if (Load->isSimple()) {
115       unsigned V = VN.lookupOrAdd(Load->getPointerOperand());
116       VNtoLoads[{V, InvalidVN}].push_back(Load);
117     }
118   }
119 
120   const VNtoInsns &getVNTable() const { return VNtoLoads; }
121 };
122 
123 // Records all store instructions candidate for code hoisting.
124 class StoreInfo {
125   VNtoInsns VNtoStores;
126 
127 public:
128   // Insert the Store and a hash number of the store address and the stored
129   // value in VNtoStores.
130   void insert(StoreInst *Store, GVN::ValueTable &VN) {
131     if (!Store->isSimple())
132       return;
133     // Hash the store address and the stored value.
134     Value *Ptr = Store->getPointerOperand();
135     Value *Val = Store->getValueOperand();
136     VNtoStores[{VN.lookupOrAdd(Ptr), VN.lookupOrAdd(Val)}].push_back(Store);
137   }
138 
139   const VNtoInsns &getVNTable() const { return VNtoStores; }
140 };
141 
142 // Records all call instructions candidate for code hoisting.
143 class CallInfo {
144   VNtoInsns VNtoCallsScalars;
145   VNtoInsns VNtoCallsLoads;
146   VNtoInsns VNtoCallsStores;
147 
148 public:
149   // Insert Call and its value numbering in one of the VNtoCalls* containers.
150   void insert(CallInst *Call, GVN::ValueTable &VN) {
151     // A call that doesNotAccessMemory is handled as a Scalar,
152     // onlyReadsMemory will be handled as a Load instruction,
153     // all other calls will be handled as stores.
154     unsigned V = VN.lookupOrAdd(Call);
155     auto Entry = std::make_pair(V, InvalidVN);
156 
157     if (Call->doesNotAccessMemory())
158       VNtoCallsScalars[Entry].push_back(Call);
159     else if (Call->onlyReadsMemory())
160       VNtoCallsLoads[Entry].push_back(Call);
161     else
162       VNtoCallsStores[Entry].push_back(Call);
163   }
164 
165   const VNtoInsns &getScalarVNTable() const { return VNtoCallsScalars; }
166 
167   const VNtoInsns &getLoadVNTable() const { return VNtoCallsLoads; }
168 
169   const VNtoInsns &getStoreVNTable() const { return VNtoCallsStores; }
170 };
171 
172 typedef DenseMap<const BasicBlock *, bool> BBSideEffectsSet;
173 typedef SmallVector<Instruction *, 4> SmallVecInsn;
174 typedef SmallVectorImpl<Instruction *> SmallVecImplInsn;
175 
176 static void combineKnownMetadata(Instruction *ReplInst, Instruction *I) {
177   static const unsigned KnownIDs[] = {
178       LLVMContext::MD_tbaa,           LLVMContext::MD_alias_scope,
179       LLVMContext::MD_noalias,        LLVMContext::MD_range,
180       LLVMContext::MD_fpmath,         LLVMContext::MD_invariant_load,
181       LLVMContext::MD_invariant_group};
182   combineMetadata(ReplInst, I, KnownIDs);
183 }
184 
185 // This pass hoists common computations across branches sharing common
186 // dominator. The primary goal is to reduce the code size, and in some
187 // cases reduce critical path (by exposing more ILP).
188 class GVNHoist {
189 public:
190   GVNHoist(DominatorTree *Dt, AliasAnalysis *Aa, MemoryDependenceResults *Md,
191            bool OptForMinSize)
192       : DT(Dt), AA(Aa), MD(Md), OptForMinSize(OptForMinSize), HoistedCtr(0) {}
193   bool run(Function &F) {
194     VN.setDomTree(DT);
195     VN.setAliasAnalysis(AA);
196     VN.setMemDep(MD);
197     bool Res = false;
198 
199     unsigned I = 0;
200     for (const BasicBlock *BB : depth_first(&F.getEntryBlock()))
201       DFSNumber.insert({BB, ++I});
202 
203     // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
204     while (1) {
205       // FIXME: only compute MemorySSA once. We need to update the analysis in
206       // the same time as transforming the code.
207       MemorySSA M(F, AA, DT);
208       MSSA = &M;
209 
210       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;
221     }
222 
223     return Res;
224   }
225 private:
226   GVN::ValueTable VN;
227   DominatorTree *DT;
228   AliasAnalysis *AA;
229   MemoryDependenceResults *MD;
230   const bool OptForMinSize;
231   DenseMap<const BasicBlock *, unsigned> DFSNumber;
232   BBSideEffectsSet BBSideEffects;
233   MemorySSA *MSSA;
234   int HoistedCtr;
235 
236   enum InsKind { Unknown, Scalar, Load, Store };
237 
238   // Return true when there are exception handling in BB.
239   bool hasEH(const BasicBlock *BB) {
240     auto It = BBSideEffects.find(BB);
241     if (It != BBSideEffects.end())
242       return It->second;
243 
244     if (BB->isEHPad() || BB->hasAddressTaken()) {
245       BBSideEffects[BB] = true;
246       return true;
247     }
248 
249     if (BB->getTerminator()->mayThrow()) {
250       BBSideEffects[BB] = true;
251       return true;
252     }
253 
254     BBSideEffects[BB] = false;
255     return false;
256   }
257 
258   // Return true when all paths from A to the end of the function pass through
259   // either B or C.
260   bool hoistingFromAllPaths(const BasicBlock *A, const BasicBlock *B,
261                             const BasicBlock *C) {
262     // We fully copy the WL in order to be able to remove items from it.
263     SmallPtrSet<const BasicBlock *, 2> WL;
264     WL.insert(B);
265     WL.insert(C);
266 
267     for (auto It = df_begin(A), E = df_end(A); It != E;) {
268       // There exists a path from A to the exit of the function if we are still
269       // iterating in DF traversal and we removed all instructions from the work
270       // list.
271       if (WL.empty())
272         return false;
273 
274       const BasicBlock *BB = *It;
275       if (WL.erase(BB)) {
276         // Stop DFS traversal when BB is in the work list.
277         It.skipChildren();
278         continue;
279       }
280 
281       // Check for end of function, calls that do not return, etc.
282       if (!isGuaranteedToTransferExecutionToSuccessor(BB->getTerminator()))
283         return false;
284 
285       // Increment DFS traversal when not skipping children.
286       ++It;
287     }
288 
289     return true;
290   }
291 
292   /* Return true when I1 appears before I2 in the instructions of BB.  */
293   bool firstInBB(BasicBlock *BB, const Instruction *I1, const Instruction *I2) {
294     for (Instruction &I : *BB) {
295       if (&I == I1)
296         return true;
297       if (&I == I2)
298         return false;
299     }
300 
301     llvm_unreachable("I1 and I2 not found in BB");
302   }
303   // Return true when there are users of Def in BB.
304   bool hasMemoryUseOnPath(MemoryAccess *Def, const BasicBlock *BB,
305                           const Instruction *OldPt) {
306     const BasicBlock *DefBB = Def->getBlock();
307     const BasicBlock *OldBB = OldPt->getParent();
308 
309     for (User *U : Def->users())
310       if (auto *MU = dyn_cast<MemoryUse>(U)) {
311         BasicBlock *UBB = MU->getBlock();
312         // Only analyze uses in BB.
313         if (BB != UBB)
314           continue;
315 
316         // A use in the same block as the Def is on the path.
317         if (UBB == DefBB) {
318           assert(MSSA->locallyDominates(Def, MU) && "def not dominating use");
319           return true;
320         }
321 
322         if (UBB != OldBB)
323           return true;
324 
325         // It is only harmful to hoist when the use is before OldPt.
326         if (firstInBB(UBB, MU->getMemoryInst(), OldPt))
327           return true;
328       }
329 
330     return false;
331   }
332 
333   // Return true when there are exception handling or loads of memory Def
334   // between OldPt and NewPt.
335 
336   // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
337   // return true when the counter NBBsOnAllPaths reaces 0, except when it is
338   // initialized to -1 which is unlimited.
339   bool hasEHOrLoadsOnPath(const Instruction *NewPt, const Instruction *OldPt,
340                           MemoryAccess *Def, int &NBBsOnAllPaths) {
341     const BasicBlock *NewBB = NewPt->getParent();
342     const BasicBlock *OldBB = OldPt->getParent();
343     assert(DT->dominates(NewBB, OldBB) && "invalid path");
344     assert(DT->dominates(Def->getBlock(), NewBB) &&
345            "def does not dominate new hoisting point");
346 
347     // Walk all basic blocks reachable in depth-first iteration on the inverse
348     // CFG from OldBB to NewBB. These blocks are all the blocks that may be
349     // executed between the execution of NewBB and OldBB. Hoisting an expression
350     // from OldBB into NewBB has to be safe on all execution paths.
351     for (auto I = idf_begin(OldBB), E = idf_end(OldBB); I != E;) {
352       if (*I == NewBB) {
353         // Stop traversal when reaching HoistPt.
354         I.skipChildren();
355         continue;
356       }
357 
358       // Impossible to hoist with exceptions on the path.
359       if (hasEH(*I))
360         return true;
361 
362       // Check that we do not move a store past loads.
363       if (hasMemoryUseOnPath(Def, *I, OldPt))
364         return true;
365 
366       // Stop walk once the limit is reached.
367       if (NBBsOnAllPaths == 0)
368         return true;
369 
370       // -1 is unlimited number of blocks on all paths.
371       if (NBBsOnAllPaths != -1)
372         --NBBsOnAllPaths;
373 
374       ++I;
375     }
376 
377     return false;
378   }
379 
380   // Return true when there are exception handling between HoistPt and BB.
381   // Decrement by 1 NBBsOnAllPaths for each block between HoistPt and BB, and
382   // return true when the counter NBBsOnAllPaths reaches 0, except when it is
383   // initialized to -1 which is unlimited.
384   bool hasEHOnPath(const BasicBlock *HoistPt, const BasicBlock *BB,
385                    int &NBBsOnAllPaths) {
386     assert(DT->dominates(HoistPt, BB) && "Invalid path");
387 
388     // Walk all basic blocks reachable in depth-first iteration on
389     // the inverse CFG from BBInsn to NewHoistPt. These blocks are all the
390     // blocks that may be executed between the execution of NewHoistPt and
391     // BBInsn. Hoisting an expression from BBInsn into NewHoistPt has to be safe
392     // on all execution paths.
393     for (auto I = idf_begin(BB), E = idf_end(BB); I != E;) {
394       if (*I == HoistPt) {
395         // Stop traversal when reaching NewHoistPt.
396         I.skipChildren();
397         continue;
398       }
399 
400       // Impossible to hoist with exceptions on the path.
401       if (hasEH(*I))
402         return true;
403 
404       // Stop walk once the limit is reached.
405       if (NBBsOnAllPaths == 0)
406         return true;
407 
408       // -1 is unlimited number of blocks on all paths.
409       if (NBBsOnAllPaths != -1)
410         --NBBsOnAllPaths;
411 
412       ++I;
413     }
414 
415     return false;
416   }
417 
418   // Return true when it is safe to hoist a memory load or store U from OldPt
419   // to NewPt.
420   bool safeToHoistLdSt(const Instruction *NewPt, const Instruction *OldPt,
421                        MemoryUseOrDef *U, InsKind K, int &NBBsOnAllPaths) {
422 
423     // In place hoisting is safe.
424     if (NewPt == OldPt)
425       return true;
426 
427     const BasicBlock *NewBB = NewPt->getParent();
428     const BasicBlock *OldBB = OldPt->getParent();
429     const BasicBlock *UBB = U->getBlock();
430 
431     // Check for dependences on the Memory SSA.
432     MemoryAccess *D = U->getDefiningAccess();
433     BasicBlock *DBB = D->getBlock();
434     if (DT->properlyDominates(NewBB, DBB))
435       // Cannot move the load or store to NewBB above its definition in DBB.
436       return false;
437 
438     if (NewBB == DBB && !MSSA->isLiveOnEntryDef(D))
439       if (auto *UD = dyn_cast<MemoryUseOrDef>(D))
440         if (firstInBB(DBB, NewPt, UD->getMemoryInst()))
441           // Cannot move the load or store to NewPt above its definition in D.
442           return false;
443 
444     // Check for unsafe hoistings due to side effects.
445     if (K == InsKind::Store) {
446       if (hasEHOrLoadsOnPath(NewPt, OldPt, D, NBBsOnAllPaths))
447         return false;
448     } else if (hasEHOnPath(NewBB, OldBB, NBBsOnAllPaths))
449       return false;
450 
451     if (UBB == NewBB) {
452       if (DT->properlyDominates(DBB, NewBB))
453         return true;
454       assert(UBB == DBB);
455       assert(MSSA->locallyDominates(D, U));
456     }
457 
458     // No side effects: it is safe to hoist.
459     return true;
460   }
461 
462   // Return true when it is safe to hoist scalar instructions from BB1 and BB2
463   // to HoistBB.
464   bool safeToHoistScalar(const BasicBlock *HoistBB, const BasicBlock *BB1,
465                          const BasicBlock *BB2, int &NBBsOnAllPaths) {
466     // Check that the hoisted expression is needed on all paths.  When HoistBB
467     // already contains an instruction to be hoisted, the expression is needed
468     // on all paths.  Enable scalar hoisting at -Oz as it is safe to hoist
469     // scalars to a place where they are partially needed.
470     if (!OptForMinSize && BB1 != HoistBB &&
471         !hoistingFromAllPaths(HoistBB, BB1, BB2))
472       return false;
473 
474     if (hasEHOnPath(HoistBB, BB1, NBBsOnAllPaths) ||
475         hasEHOnPath(HoistBB, BB2, NBBsOnAllPaths))
476       return false;
477 
478     // Safe to hoist scalars from BB1 and BB2 to HoistBB.
479     return true;
480   }
481 
482   // Each element of a hoisting list contains the basic block where to hoist and
483   // a list of instructions to be hoisted.
484   typedef std::pair<BasicBlock *, SmallVecInsn> HoistingPointInfo;
485   typedef SmallVector<HoistingPointInfo, 4> HoistingPointList;
486 
487   // Partition InstructionsToHoist into a set of candidates which can share a
488   // common hoisting point. The partitions are collected in HPL. IsScalar is
489   // true when the instructions in InstructionsToHoist are scalars. IsLoad is
490   // true when the InstructionsToHoist are loads, false when they are stores.
491   void partitionCandidates(SmallVecImplInsn &InstructionsToHoist,
492                            HoistingPointList &HPL, InsKind K) {
493     // No need to sort for two instructions.
494     if (InstructionsToHoist.size() > 2) {
495       SortByDFSIn Pred(DFSNumber);
496       std::sort(InstructionsToHoist.begin(), InstructionsToHoist.end(), Pred);
497     }
498 
499     int NBBsOnAllPaths = MaxNumberOfBBSInPath;
500 
501     SmallVecImplInsn::iterator II = InstructionsToHoist.begin();
502     SmallVecImplInsn::iterator Start = II;
503     Instruction *HoistPt = *II;
504     BasicBlock *HoistBB = HoistPt->getParent();
505     MemoryUseOrDef *UD;
506     if (K != InsKind::Scalar)
507       UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(HoistPt));
508 
509     for (++II; II != InstructionsToHoist.end(); ++II) {
510       Instruction *Insn = *II;
511       BasicBlock *BB = Insn->getParent();
512       BasicBlock *NewHoistBB;
513       Instruction *NewHoistPt;
514 
515       if (BB == HoistBB) {
516         NewHoistBB = HoistBB;
517         NewHoistPt = firstInBB(BB, Insn, HoistPt) ? Insn : HoistPt;
518       } else {
519         NewHoistBB = DT->findNearestCommonDominator(HoistBB, BB);
520         if (NewHoistBB == BB)
521           NewHoistPt = Insn;
522         else if (NewHoistBB == HoistBB)
523           NewHoistPt = HoistPt;
524         else
525           NewHoistPt = NewHoistBB->getTerminator();
526       }
527 
528       if (K == InsKind::Scalar) {
529         if (safeToHoistScalar(NewHoistBB, HoistBB, BB, NBBsOnAllPaths)) {
530           // Extend HoistPt to NewHoistPt.
531           HoistPt = NewHoistPt;
532           HoistBB = NewHoistBB;
533           continue;
534         }
535       } else {
536         // When NewBB already contains an instruction to be hoisted, the
537         // expression is needed on all paths.
538         // Check that the hoisted expression is needed on all paths: it is
539         // unsafe to hoist loads to a place where there may be a path not
540         // loading from the same address: for instance there may be a branch on
541         // which the address of the load may not be initialized.
542         if ((HoistBB == NewHoistBB || BB == NewHoistBB ||
543              hoistingFromAllPaths(NewHoistBB, HoistBB, BB)) &&
544             // Also check that it is safe to move the load or store from HoistPt
545             // to NewHoistPt, and from Insn to NewHoistPt.
546             safeToHoistLdSt(NewHoistPt, HoistPt, UD, K, NBBsOnAllPaths) &&
547             safeToHoistLdSt(NewHoistPt, Insn,
548                             cast<MemoryUseOrDef>(MSSA->getMemoryAccess(Insn)),
549                             K, NBBsOnAllPaths)) {
550           // Extend HoistPt to NewHoistPt.
551           HoistPt = NewHoistPt;
552           HoistBB = NewHoistBB;
553           continue;
554         }
555       }
556 
557       // At this point it is not safe to extend the current hoisting to
558       // NewHoistPt: save the hoisting list so far.
559       if (std::distance(Start, II) > 1)
560         HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
561 
562       // Start over from BB.
563       Start = II;
564       if (K != InsKind::Scalar)
565         UD = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(*Start));
566       HoistPt = Insn;
567       HoistBB = BB;
568       NBBsOnAllPaths = MaxNumberOfBBSInPath;
569     }
570 
571     // Save the last partition.
572     if (std::distance(Start, II) > 1)
573       HPL.push_back({HoistBB, SmallVecInsn(Start, II)});
574   }
575 
576   // Initialize HPL from Map.
577   void computeInsertionPoints(const VNtoInsns &Map, HoistingPointList &HPL,
578                               InsKind K) {
579     for (const auto &Entry : Map) {
580       if (MaxHoistedThreshold != -1 && ++HoistedCtr > MaxHoistedThreshold)
581         return;
582 
583       const SmallVecInsn &V = Entry.second;
584       if (V.size() < 2)
585         continue;
586 
587       // Compute the insertion point and the list of expressions to be hoisted.
588       SmallVecInsn InstructionsToHoist;
589       for (auto I : V)
590         if (!hasEH(I->getParent()))
591           InstructionsToHoist.push_back(I);
592 
593       if (!InstructionsToHoist.empty())
594         partitionCandidates(InstructionsToHoist, HPL, K);
595     }
596   }
597 
598   // Return true when all operands of Instr are available at insertion point
599   // HoistPt. When limiting the number of hoisted expressions, one could hoist
600   // a load without hoisting its access function. So before hoisting any
601   // expression, make sure that all its operands are available at insert point.
602   bool allOperandsAvailable(const Instruction *I,
603                             const BasicBlock *HoistPt) const {
604     for (const Use &Op : I->operands())
605       if (const auto *Inst = dyn_cast<Instruction>(&Op))
606         if (!DT->dominates(Inst->getParent(), HoistPt))
607           return false;
608 
609     return true;
610   }
611 
612   Instruction *firstOfTwo(Instruction *I, Instruction *J) const {
613     for (Instruction &I1 : *I->getParent())
614       if (&I1 == I || &I1 == J)
615         return &I1;
616     llvm_unreachable("Both I and J must be from same BB");
617   }
618 
619   bool makeOperandsAvailable(Instruction *Repl, BasicBlock *HoistPt,
620                              const SmallVecInsn &InstructionsToHoist) const {
621     // Check whether the GEP of a ld/st can be synthesized at HoistPt.
622     GetElementPtrInst *Gep = nullptr;
623     Instruction *Val = nullptr;
624     if (auto *Ld = dyn_cast<LoadInst>(Repl))
625       Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
626     if (auto *St = dyn_cast<StoreInst>(Repl)) {
627       Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
628       Val = dyn_cast<Instruction>(St->getValueOperand());
629       // Check that the stored value is available.
630       if (Val) {
631         if (isa<GetElementPtrInst>(Val)) {
632           // Check whether we can compute the GEP at HoistPt.
633           if (!allOperandsAvailable(Val, HoistPt))
634             return false;
635         } else if (!DT->dominates(Val->getParent(), HoistPt))
636           return false;
637       }
638     }
639 
640     // Check whether we can compute the Gep at HoistPt.
641     if (!Gep || !allOperandsAvailable(Gep, HoistPt))
642       return false;
643 
644     // Copy the gep before moving the ld/st.
645     Instruction *ClonedGep = Gep->clone();
646     ClonedGep->insertBefore(HoistPt->getTerminator());
647     // Conservatively discard any optimization hints, they may differ on the
648     // other paths.
649     for (Instruction *OtherInst : InstructionsToHoist) {
650       GetElementPtrInst *OtherGep;
651       if (auto *OtherLd = dyn_cast<LoadInst>(OtherInst))
652         OtherGep = cast<GetElementPtrInst>(OtherLd->getPointerOperand());
653       else
654         OtherGep = cast<GetElementPtrInst>(
655             cast<StoreInst>(OtherInst)->getPointerOperand());
656       ClonedGep->intersectOptionalDataWith(OtherGep);
657       combineKnownMetadata(ClonedGep, OtherGep);
658     }
659     Repl->replaceUsesOfWith(Gep, ClonedGep);
660 
661     // Also copy Val when it is a GEP.
662     if (Val && isa<GetElementPtrInst>(Val)) {
663       Instruction *ClonedVal = Val->clone();
664       ClonedVal->insertBefore(HoistPt->getTerminator());
665       // Conservatively discard any optimization hints, they may differ on the
666       // other paths.
667       for (Instruction *OtherInst : InstructionsToHoist) {
668         auto *OtherVal =
669             cast<Instruction>(cast<StoreInst>(OtherInst)->getValueOperand());
670         ClonedVal->intersectOptionalDataWith(OtherVal);
671         combineKnownMetadata(ClonedVal, OtherVal);
672       }
673       Repl->replaceUsesOfWith(Val, ClonedVal);
674     }
675 
676     return true;
677   }
678 
679   std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
680     unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
681     for (const HoistingPointInfo &HP : HPL) {
682       // Find out whether we already have one of the instructions in HoistPt,
683       // in which case we do not have to move it.
684       BasicBlock *HoistPt = HP.first;
685       const SmallVecInsn &InstructionsToHoist = HP.second;
686       Instruction *Repl = nullptr;
687       for (Instruction *I : InstructionsToHoist)
688         if (I->getParent() == HoistPt) {
689           // If there are two instructions in HoistPt to be hoisted in place:
690           // update Repl to be the first one, such that we can rename the uses
691           // of the second based on the first.
692           Repl = !Repl ? I : firstOfTwo(Repl, I);
693         }
694 
695       if (Repl) {
696         // Repl is already in HoistPt: it remains in place.
697         assert(allOperandsAvailable(Repl, HoistPt) &&
698                "instruction depends on operands that are not available");
699       } else {
700         // When we do not find Repl in HoistPt, select the first in the list
701         // and move it to HoistPt.
702         Repl = InstructionsToHoist.front();
703 
704         // We can move Repl in HoistPt only when all operands are available.
705         // The order in which hoistings are done may influence the availability
706         // of operands.
707         if (!allOperandsAvailable(Repl, HoistPt) &&
708             !makeOperandsAvailable(Repl, HoistPt, InstructionsToHoist))
709           continue;
710         Repl->moveBefore(HoistPt->getTerminator());
711         // TBAA may differ on one of the other paths, we need to get rid of
712         // anything which might conflict.
713       }
714 
715       if (isa<LoadInst>(Repl))
716         ++NL;
717       else if (isa<StoreInst>(Repl))
718         ++NS;
719       else if (isa<CallInst>(Repl))
720         ++NC;
721       else // Scalar
722         ++NI;
723 
724       // Remove and rename all other instructions.
725       for (Instruction *I : InstructionsToHoist)
726         if (I != Repl) {
727           ++NR;
728           if (auto *ReplacementLoad = dyn_cast<LoadInst>(Repl)) {
729             ReplacementLoad->setAlignment(
730                 std::min(ReplacementLoad->getAlignment(),
731                          cast<LoadInst>(I)->getAlignment()));
732             ++NumLoadsRemoved;
733           } else if (auto *ReplacementStore = dyn_cast<StoreInst>(Repl)) {
734             ReplacementStore->setAlignment(
735                 std::min(ReplacementStore->getAlignment(),
736                          cast<StoreInst>(I)->getAlignment()));
737             ++NumStoresRemoved;
738           } else if (auto *ReplacementAlloca = dyn_cast<AllocaInst>(Repl)) {
739             ReplacementAlloca->setAlignment(
740                 std::max(ReplacementAlloca->getAlignment(),
741                          cast<AllocaInst>(I)->getAlignment()));
742           } else if (isa<CallInst>(Repl)) {
743             ++NumCallsRemoved;
744           }
745           Repl->intersectOptionalDataWith(I);
746           combineKnownMetadata(Repl, I);
747           I->replaceAllUsesWith(Repl);
748           I->eraseFromParent();
749         }
750     }
751 
752     NumHoisted += NL + NS + NC + NI;
753     NumRemoved += NR;
754     NumLoadsHoisted += NL;
755     NumStoresHoisted += NS;
756     NumCallsHoisted += NC;
757     return {NI, NL + NC + NS};
758   }
759 
760   // Hoist all expressions. Returns Number of scalars hoisted
761   // and number of non-scalars hoisted.
762   std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
763     InsnInfo II;
764     LoadInfo LI;
765     StoreInfo SI;
766     CallInfo CI;
767     for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
768       for (Instruction &I1 : *BB) {
769         if (auto *Load = dyn_cast<LoadInst>(&I1))
770           LI.insert(Load, VN);
771         else if (auto *Store = dyn_cast<StoreInst>(&I1))
772           SI.insert(Store, VN);
773         else if (auto *Call = dyn_cast<CallInst>(&I1)) {
774           if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
775             if (isa<DbgInfoIntrinsic>(Intr) ||
776                 Intr->getIntrinsicID() == Intrinsic::assume)
777               continue;
778           }
779           if (Call->mayHaveSideEffects()) {
780             if (!OptForMinSize)
781               break;
782             // We may continue hoisting across calls which write to memory.
783             if (Call->mayThrow())
784               break;
785           }
786           CI.insert(Call, VN);
787         } else if (OptForMinSize || !isa<GetElementPtrInst>(&I1))
788           // Do not hoist scalars past calls that may write to memory because
789           // that could result in spills later. geps are handled separately.
790           // TODO: We can relax this for targets like AArch64 as they have more
791           // registers than X86.
792           II.insert(&I1, VN);
793       }
794     }
795 
796     HoistingPointList HPL;
797     computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
798     computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
799     computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
800     computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
801     computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
802     computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
803     return hoist(HPL);
804   }
805 };
806 
807 class GVNHoistLegacyPass : public FunctionPass {
808 public:
809   static char ID;
810 
811   GVNHoistLegacyPass() : FunctionPass(ID) {
812     initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
813   }
814 
815   bool runOnFunction(Function &F) override {
816     if (skipFunction(F))
817       return false;
818     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
819     auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
820     auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
821 
822     GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
823     return G.run(F);
824   }
825 
826   void getAnalysisUsage(AnalysisUsage &AU) const override {
827     AU.addRequired<DominatorTreeWrapperPass>();
828     AU.addRequired<AAResultsWrapperPass>();
829     AU.addRequired<MemoryDependenceWrapperPass>();
830     AU.addPreserved<DominatorTreeWrapperPass>();
831   }
832 };
833 } // namespace
834 
835 PreservedAnalyses GVNHoistPass::run(Function &F,
836                                     AnalysisManager<Function> &AM) {
837   DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
838   AliasAnalysis &AA = AM.getResult<AAManager>(F);
839   MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
840 
841   GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
842   if (!G.run(F))
843     return PreservedAnalyses::all();
844 
845   PreservedAnalyses PA;
846   PA.preserve<DominatorTreeAnalysis>();
847   return PA;
848 }
849 
850 char GVNHoistLegacyPass::ID = 0;
851 INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
852                       "Early GVN Hoisting of Expressions", false, false)
853 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
854 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
855 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
856 INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
857                     "Early GVN Hoisting of Expressions", false, false)
858 
859 FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }
860