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