xref: /llvm-project/llvm/lib/Transforms/Scalar/GVNHoist.cpp (revision bd21012c6c01ea3f46d0533a09c3fc3c182c3759)
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) const {
578     // Check whether the GEP of a ld/st can be synthesized at HoistPt.
579     GetElementPtrInst *Gep = nullptr;
580     Instruction *Val = nullptr;
581     if (auto *Ld = dyn_cast<LoadInst>(Repl))
582       Gep = dyn_cast<GetElementPtrInst>(Ld->getPointerOperand());
583     if (auto *St = dyn_cast<StoreInst>(Repl)) {
584       Gep = dyn_cast<GetElementPtrInst>(St->getPointerOperand());
585       Val = dyn_cast<Instruction>(St->getValueOperand());
586     }
587 
588     if (!Gep)
589       return false;
590 
591     // PHIs may only be inserted at the start of a block.
592     if (Val && isa<PHINode>(Val))
593       return false;
594 
595     // Check whether we can compute the Gep at HoistPt.
596     if (!allOperandsAvailable(Gep, HoistPt))
597       return false;
598 
599     // Also check that the stored value is available.
600     if (Val && !allOperandsAvailable(Val, HoistPt))
601       return false;
602 
603     // Copy the gep before moving the ld/st.
604     Instruction *ClonedGep = Gep->clone();
605     ClonedGep->insertBefore(HoistPt->getTerminator());
606     Repl->replaceUsesOfWith(Gep, ClonedGep);
607 
608     // Also copy Val.
609     if (Val) {
610       Instruction *ClonedVal = Val->clone();
611       ClonedVal->insertBefore(HoistPt->getTerminator());
612       Repl->replaceUsesOfWith(Val, ClonedVal);
613     }
614 
615     return true;
616   }
617 
618   std::pair<unsigned, unsigned> hoist(HoistingPointList &HPL) {
619     unsigned NI = 0, NL = 0, NS = 0, NC = 0, NR = 0;
620     for (const HoistingPointInfo &HP : HPL) {
621       // Find out whether we already have one of the instructions in HoistPt,
622       // in which case we do not have to move it.
623       BasicBlock *HoistPt = HP.first;
624       const SmallVecInsn &InstructionsToHoist = HP.second;
625       Instruction *Repl = nullptr;
626       for (Instruction *I : InstructionsToHoist)
627         if (I->getParent() == HoistPt) {
628           // If there are two instructions in HoistPt to be hoisted in place:
629           // update Repl to be the first one, such that we can rename the uses
630           // of the second based on the first.
631           Repl = !Repl ? I : firstOfTwo(Repl, I);
632         }
633 
634       if (Repl) {
635         // Repl is already in HoistPt: it remains in place.
636         assert(allOperandsAvailable(Repl, HoistPt) &&
637                "instruction depends on operands that are not available");
638       } else {
639         // When we do not find Repl in HoistPt, select the first in the list
640         // and move it to HoistPt.
641         Repl = InstructionsToHoist.front();
642 
643         // We can move Repl in HoistPt only when all operands are available.
644         // The order in which hoistings are done may influence the availability
645         // of operands.
646         if (!allOperandsAvailable(Repl, HoistPt) &&
647             !makeOperandsAvailable(Repl, HoistPt))
648           continue;
649         Repl->moveBefore(HoistPt->getTerminator());
650       }
651 
652       if (isa<LoadInst>(Repl))
653         ++NL;
654       else if (isa<StoreInst>(Repl))
655         ++NS;
656       else if (isa<CallInst>(Repl))
657         ++NC;
658       else // Scalar
659         ++NI;
660 
661       // Remove and rename all other instructions.
662       for (Instruction *I : InstructionsToHoist)
663         if (I != Repl) {
664           ++NR;
665           if (isa<LoadInst>(Repl))
666             ++NumLoadsRemoved;
667           else if (isa<StoreInst>(Repl))
668             ++NumStoresRemoved;
669           else if (isa<CallInst>(Repl))
670             ++NumCallsRemoved;
671           I->replaceAllUsesWith(Repl);
672           I->eraseFromParent();
673         }
674     }
675 
676     NumHoisted += NL + NS + NC + NI;
677     NumRemoved += NR;
678     NumLoadsHoisted += NL;
679     NumStoresHoisted += NS;
680     NumCallsHoisted += NC;
681     return {NI, NL + NC + NS};
682   }
683 
684   // Hoist all expressions. Returns Number of scalars hoisted
685   // and number of non-scalars hoisted.
686   std::pair<unsigned, unsigned> hoistExpressions(Function &F) {
687     InsnInfo II;
688     LoadInfo LI;
689     StoreInfo SI;
690     CallInfo CI;
691     for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
692       for (Instruction &I1 : *BB) {
693         if (auto *Load = dyn_cast<LoadInst>(&I1))
694           LI.insert(Load, VN);
695         else if (auto *Store = dyn_cast<StoreInst>(&I1))
696           SI.insert(Store, VN);
697         else if (auto *Call = dyn_cast<CallInst>(&I1)) {
698           if (auto *Intr = dyn_cast<IntrinsicInst>(Call)) {
699             if (isa<DbgInfoIntrinsic>(Intr) ||
700                 Intr->getIntrinsicID() == Intrinsic::assume)
701               continue;
702           }
703           if (Call->mayHaveSideEffects()) {
704             if (!OptForMinSize)
705               break;
706             // We may continue hoisting across calls which write to memory.
707             if (Call->mayThrow())
708               break;
709           }
710           CI.insert(Call, VN);
711         } else if (OptForMinSize || !isa<GetElementPtrInst>(&I1))
712           // Do not hoist scalars past calls that may write to memory because
713           // that could result in spills later. geps are handled separately.
714           // TODO: We can relax this for targets like AArch64 as they have more
715           // registers than X86.
716           II.insert(&I1, VN);
717       }
718     }
719 
720     HoistingPointList HPL;
721     computeInsertionPoints(II.getVNTable(), HPL, InsKind::Scalar);
722     computeInsertionPoints(LI.getVNTable(), HPL, InsKind::Load);
723     computeInsertionPoints(SI.getVNTable(), HPL, InsKind::Store);
724     computeInsertionPoints(CI.getScalarVNTable(), HPL, InsKind::Scalar);
725     computeInsertionPoints(CI.getLoadVNTable(), HPL, InsKind::Load);
726     computeInsertionPoints(CI.getStoreVNTable(), HPL, InsKind::Store);
727     return hoist(HPL);
728   }
729 
730   bool run(Function &F) {
731     VN.setDomTree(DT);
732     VN.setAliasAnalysis(AA);
733     VN.setMemDep(MD);
734     bool Res = false;
735 
736     unsigned I = 0;
737     for (const BasicBlock *BB : depth_first(&F.getEntryBlock()))
738       DFSNumber.insert({BB, ++I});
739 
740     // FIXME: use lazy evaluation of VN to avoid the fix-point computation.
741     while (1) {
742       // FIXME: only compute MemorySSA once. We need to update the analysis in
743       // the same time as transforming the code.
744       MemorySSA M(F, AA, DT);
745       MSSA = &M;
746 
747       auto HoistStat = hoistExpressions(F);
748       if (HoistStat.first + HoistStat.second == 0) {
749         return Res;
750       }
751       if (HoistStat.second > 0) {
752         // To address a limitation of the current GVN, we need to rerun the
753         // hoisting after we hoisted loads in order to be able to hoist all
754         // scalars dependent on the hoisted loads. Same for stores.
755         VN.clear();
756       }
757       Res = true;
758     }
759 
760     return Res;
761   }
762 };
763 
764 class GVNHoistLegacyPass : public FunctionPass {
765 public:
766   static char ID;
767 
768   GVNHoistLegacyPass() : FunctionPass(ID) {
769     initializeGVNHoistLegacyPassPass(*PassRegistry::getPassRegistry());
770   }
771 
772   bool runOnFunction(Function &F) override {
773     if (skipFunction(F))
774       return false;
775     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
776     auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
777     auto &MD = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
778 
779     GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
780     return G.run(F);
781   }
782 
783   void getAnalysisUsage(AnalysisUsage &AU) const override {
784     AU.addRequired<DominatorTreeWrapperPass>();
785     AU.addRequired<AAResultsWrapperPass>();
786     AU.addRequired<MemoryDependenceWrapperPass>();
787     AU.addPreserved<DominatorTreeWrapperPass>();
788   }
789 };
790 } // namespace
791 
792 PreservedAnalyses GVNHoistPass::run(Function &F,
793                                     AnalysisManager<Function> &AM) {
794   DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
795   AliasAnalysis &AA = AM.getResult<AAManager>(F);
796   MemoryDependenceResults &MD = AM.getResult<MemoryDependenceAnalysis>(F);
797 
798   GVNHoist G(&DT, &AA, &MD, F.optForMinSize());
799   if (!G.run(F))
800     return PreservedAnalyses::all();
801 
802   PreservedAnalyses PA;
803   PA.preserve<DominatorTreeAnalysis>();
804   return PA;
805 }
806 
807 char GVNHoistLegacyPass::ID = 0;
808 INITIALIZE_PASS_BEGIN(GVNHoistLegacyPass, "gvn-hoist",
809                       "Early GVN Hoisting of Expressions", false, false)
810 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
811 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
812 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
813 INITIALIZE_PASS_END(GVNHoistLegacyPass, "gvn-hoist",
814                     "Early GVN Hoisting of Expressions", false, false)
815 
816 FunctionPass *llvm::createGVNHoistPass() { return new GVNHoistLegacyPass(); }
817