• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MergedLoadStoreMotion.cpp - merge and hoist/sink load/stores -------===//
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 //! \file
11 //! \brief This pass performs merges of loads and stores on both sides of a
12 //  diamond (hammock). It hoists the loads and sinks the stores.
13 //
14 // The algorithm iteratively hoists two loads to the same address out of a
15 // diamond (hammock) and merges them into a single load in the header. Similar
16 // it sinks and merges two stores to the tail block (footer). The algorithm
17 // iterates over the instructions of one side of the diamond and attempts to
18 // find a matching load/store on the other side. It hoists / sinks when it
19 // thinks it safe to do so.  This optimization helps with eg. hiding load
20 // latencies, triggering if-conversion, and reducing static code size.
21 //
22 //===----------------------------------------------------------------------===//
23 //
24 //
25 // Example:
26 // Diamond shaped code before merge:
27 //
28 //            header:
29 //                     br %cond, label %if.then, label %if.else
30 //                        +                    +
31 //                       +                      +
32 //                      +                        +
33 //            if.then:                         if.else:
34 //               %lt = load %addr_l               %le = load %addr_l
35 //               <use %lt>                        <use %le>
36 //               <...>                            <...>
37 //               store %st, %addr_s               store %se, %addr_s
38 //               br label %if.end                 br label %if.end
39 //                     +                         +
40 //                      +                       +
41 //                       +                     +
42 //            if.end ("footer"):
43 //                     <...>
44 //
45 // Diamond shaped code after merge:
46 //
47 //            header:
48 //                     %l = load %addr_l
49 //                     br %cond, label %if.then, label %if.else
50 //                        +                    +
51 //                       +                      +
52 //                      +                        +
53 //            if.then:                         if.else:
54 //               <use %l>                         <use %l>
55 //               <...>                            <...>
56 //               br label %if.end                 br label %if.end
57 //                      +                        +
58 //                       +                      +
59 //                        +                    +
60 //            if.end ("footer"):
61 //                     %s.sink = phi [%st, if.then], [%se, if.else]
62 //                     <...>
63 //                     store %s.sink, %addr_s
64 //                     <...>
65 //
66 //
67 //===----------------------- TODO -----------------------------------------===//
68 //
69 // 1) Generalize to regions other than diamonds
70 // 2) Be more aggressive merging memory operations
71 // Note that both changes require register pressure control
72 //
73 //===----------------------------------------------------------------------===//
74 
75 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
76 #include "llvm/ADT/Statistic.h"
77 #include "llvm/Analysis/AliasAnalysis.h"
78 #include "llvm/Analysis/CFG.h"
79 #include "llvm/Analysis/GlobalsModRef.h"
80 #include "llvm/Analysis/Loads.h"
81 #include "llvm/Analysis/MemoryBuiltins.h"
82 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
83 #include "llvm/Analysis/ValueTracking.h"
84 #include "llvm/IR/Metadata.h"
85 #include "llvm/IR/PatternMatch.h"
86 #include "llvm/Support/Debug.h"
87 #include "llvm/Support/raw_ostream.h"
88 #include "llvm/Transforms/Scalar.h"
89 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
90 #include "llvm/Transforms/Utils/SSAUpdater.h"
91 
92 using namespace llvm;
93 
94 #define DEBUG_TYPE "mldst-motion"
95 
96 namespace {
97 //===----------------------------------------------------------------------===//
98 //                         MergedLoadStoreMotion Pass
99 //===----------------------------------------------------------------------===//
100 class MergedLoadStoreMotion {
101   MemoryDependenceResults *MD = nullptr;
102   AliasAnalysis *AA = nullptr;
103 
104   // The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
105   // where Size0 and Size1 are the #instructions on the two sides of
106   // the diamond. The constant chosen here is arbitrary. Compiler Time
107   // Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl.
108   const int MagicCompileTimeControl = 250;
109 
110 public:
111   bool run(Function &F, MemoryDependenceResults *MD, AliasAnalysis &AA);
112 
113 private:
114   ///
115   /// \brief Remove instruction from parent and update memory dependence
116   /// analysis.
117   ///
118   void removeInstruction(Instruction *Inst);
119   BasicBlock *getDiamondTail(BasicBlock *BB);
120   bool isDiamondHead(BasicBlock *BB);
121   // Routines for hoisting loads
122   bool isLoadHoistBarrierInRange(const Instruction &Start,
123                                  const Instruction &End, LoadInst *LI,
124                                  bool SafeToLoadUnconditionally);
125   LoadInst *canHoistFromBlock(BasicBlock *BB, LoadInst *LI);
126   void hoistInstruction(BasicBlock *BB, Instruction *HoistCand,
127                         Instruction *ElseInst);
128   bool isSafeToHoist(Instruction *I) const;
129   bool hoistLoad(BasicBlock *BB, LoadInst *HoistCand, LoadInst *ElseInst);
130   bool mergeLoads(BasicBlock *BB);
131   // Routines for sinking stores
132   StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI);
133   PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1);
134   bool isStoreSinkBarrierInRange(const Instruction &Start,
135                                  const Instruction &End, MemoryLocation Loc);
136   bool sinkStore(BasicBlock *BB, StoreInst *SinkCand, StoreInst *ElseInst);
137   bool mergeStores(BasicBlock *BB);
138 };
139 } // end anonymous namespace
140 
141 ///
142 /// \brief Remove instruction from parent and update memory dependence analysis.
143 ///
removeInstruction(Instruction * Inst)144 void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
145   // Notify the memory dependence analysis.
146   if (MD) {
147     MD->removeInstruction(Inst);
148     if (auto *LI = dyn_cast<LoadInst>(Inst))
149       MD->invalidateCachedPointerInfo(LI->getPointerOperand());
150     if (Inst->getType()->isPtrOrPtrVectorTy()) {
151       MD->invalidateCachedPointerInfo(Inst);
152     }
153   }
154   Inst->eraseFromParent();
155 }
156 
157 ///
158 /// \brief Return tail block of a diamond.
159 ///
getDiamondTail(BasicBlock * BB)160 BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
161   assert(isDiamondHead(BB) && "Basic block is not head of a diamond");
162   return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor();
163 }
164 
165 ///
166 /// \brief True when BB is the head of a diamond (hammock)
167 ///
isDiamondHead(BasicBlock * BB)168 bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
169   if (!BB)
170     return false;
171   auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
172   if (!BI || !BI->isConditional())
173     return false;
174 
175   BasicBlock *Succ0 = BI->getSuccessor(0);
176   BasicBlock *Succ1 = BI->getSuccessor(1);
177 
178   if (!Succ0->getSinglePredecessor())
179     return false;
180   if (!Succ1->getSinglePredecessor())
181     return false;
182 
183   BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
184   BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
185   // Ignore triangles.
186   if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
187     return false;
188   return true;
189 }
190 
191 ///
192 /// \brief True when instruction is a hoist barrier for a load
193 ///
194 /// Whenever an instruction could possibly modify the value
195 /// being loaded or protect against the load from happening
196 /// it is considered a hoist barrier.
197 ///
isLoadHoistBarrierInRange(const Instruction & Start,const Instruction & End,LoadInst * LI,bool SafeToLoadUnconditionally)198 bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(
199     const Instruction &Start, const Instruction &End, LoadInst *LI,
200     bool SafeToLoadUnconditionally) {
201   if (!SafeToLoadUnconditionally)
202     for (const Instruction &Inst :
203          make_range(Start.getIterator(), End.getIterator()))
204       if (!isGuaranteedToTransferExecutionToSuccessor(&Inst))
205         return true;
206   MemoryLocation Loc = MemoryLocation::get(LI);
207   return AA->canInstructionRangeModRef(Start, End, Loc, MRI_Mod);
208 }
209 
210 ///
211 /// \brief Decide if a load can be hoisted
212 ///
213 /// When there is a load in \p BB to the same address as \p LI
214 /// and it can be hoisted from \p BB, return that load.
215 /// Otherwise return Null.
216 ///
canHoistFromBlock(BasicBlock * BB1,LoadInst * Load0)217 LoadInst *MergedLoadStoreMotion::canHoistFromBlock(BasicBlock *BB1,
218                                                    LoadInst *Load0) {
219   BasicBlock *BB0 = Load0->getParent();
220   BasicBlock *Head = BB0->getSinglePredecessor();
221   bool SafeToLoadUnconditionally = isSafeToLoadUnconditionally(
222       Load0->getPointerOperand(), Load0->getAlignment(),
223       Load0->getModule()->getDataLayout(),
224       /*ScanFrom=*/Head->getTerminator());
225   for (BasicBlock::iterator BBI = BB1->begin(), BBE = BB1->end(); BBI != BBE;
226        ++BBI) {
227     Instruction *Inst = &*BBI;
228 
229     // Only merge and hoist loads when their result in used only in BB
230     auto *Load1 = dyn_cast<LoadInst>(Inst);
231     if (!Load1 || Inst->isUsedOutsideOfBlock(BB1))
232       continue;
233 
234     MemoryLocation Loc0 = MemoryLocation::get(Load0);
235     MemoryLocation Loc1 = MemoryLocation::get(Load1);
236     if (Load0->isSameOperationAs(Load1) && AA->isMustAlias(Loc0, Loc1) &&
237         !isLoadHoistBarrierInRange(BB1->front(), *Load1, Load1,
238                                    SafeToLoadUnconditionally) &&
239         !isLoadHoistBarrierInRange(BB0->front(), *Load0, Load0,
240                                    SafeToLoadUnconditionally)) {
241       return Load1;
242     }
243   }
244   return nullptr;
245 }
246 
247 ///
248 /// \brief Merge two equivalent instructions \p HoistCand and \p ElseInst into
249 /// \p BB
250 ///
251 /// BB is the head of a diamond
252 ///
hoistInstruction(BasicBlock * BB,Instruction * HoistCand,Instruction * ElseInst)253 void MergedLoadStoreMotion::hoistInstruction(BasicBlock *BB,
254                                              Instruction *HoistCand,
255                                              Instruction *ElseInst) {
256   DEBUG(dbgs() << " Hoist Instruction into BB \n"; BB->dump();
257         dbgs() << "Instruction Left\n"; HoistCand->dump(); dbgs() << "\n";
258         dbgs() << "Instruction Right\n"; ElseInst->dump(); dbgs() << "\n");
259   // Hoist the instruction.
260   assert(HoistCand->getParent() != BB);
261 
262   // Intersect optional metadata.
263   HoistCand->intersectOptionalDataWith(ElseInst);
264   HoistCand->dropUnknownNonDebugMetadata();
265 
266   // Prepend point for instruction insert
267   Instruction *HoistPt = BB->getTerminator();
268 
269   // Merged instruction
270   Instruction *HoistedInst = HoistCand->clone();
271 
272   // Hoist instruction.
273   HoistedInst->insertBefore(HoistPt);
274 
275   HoistCand->replaceAllUsesWith(HoistedInst);
276   removeInstruction(HoistCand);
277   // Replace the else block instruction.
278   ElseInst->replaceAllUsesWith(HoistedInst);
279   removeInstruction(ElseInst);
280 }
281 
282 ///
283 /// \brief Return true if no operand of \p I is defined in I's parent block
284 ///
isSafeToHoist(Instruction * I) const285 bool MergedLoadStoreMotion::isSafeToHoist(Instruction *I) const {
286   BasicBlock *Parent = I->getParent();
287   for (Use &U : I->operands())
288     if (auto *Instr = dyn_cast<Instruction>(&U))
289       if (Instr->getParent() == Parent)
290         return false;
291   return true;
292 }
293 
294 ///
295 /// \brief Merge two equivalent loads and GEPs and hoist into diamond head
296 ///
hoistLoad(BasicBlock * BB,LoadInst * L0,LoadInst * L1)297 bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
298                                       LoadInst *L1) {
299   // Only one definition?
300   auto *A0 = dyn_cast<Instruction>(L0->getPointerOperand());
301   auto *A1 = dyn_cast<Instruction>(L1->getPointerOperand());
302   if (A0 && A1 && A0->isIdenticalTo(A1) && isSafeToHoist(A0) &&
303       A0->hasOneUse() && (A0->getParent() == L0->getParent()) &&
304       A1->hasOneUse() && (A1->getParent() == L1->getParent()) &&
305       isa<GetElementPtrInst>(A0)) {
306     DEBUG(dbgs() << "Hoist Instruction into BB \n"; BB->dump();
307           dbgs() << "Instruction Left\n"; L0->dump(); dbgs() << "\n";
308           dbgs() << "Instruction Right\n"; L1->dump(); dbgs() << "\n");
309     hoistInstruction(BB, A0, A1);
310     hoistInstruction(BB, L0, L1);
311     return true;
312   }
313   return false;
314 }
315 
316 ///
317 /// \brief Try to hoist two loads to same address into diamond header
318 ///
319 /// Starting from a diamond head block, iterate over the instructions in one
320 /// successor block and try to match a load in the second successor.
321 ///
mergeLoads(BasicBlock * BB)322 bool MergedLoadStoreMotion::mergeLoads(BasicBlock *BB) {
323   bool MergedLoads = false;
324   assert(isDiamondHead(BB));
325   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
326   BasicBlock *Succ0 = BI->getSuccessor(0);
327   BasicBlock *Succ1 = BI->getSuccessor(1);
328   // #Instructions in Succ1 for Compile Time Control
329   int Size1 = Succ1->size();
330   int NLoads = 0;
331   for (BasicBlock::iterator BBI = Succ0->begin(), BBE = Succ0->end();
332        BBI != BBE;) {
333     Instruction *I = &*BBI;
334     ++BBI;
335 
336     // Don't move non-simple (atomic, volatile) loads.
337     auto *L0 = dyn_cast<LoadInst>(I);
338     if (!L0 || !L0->isSimple() || L0->isUsedOutsideOfBlock(Succ0))
339       continue;
340 
341     ++NLoads;
342     if (NLoads * Size1 >= MagicCompileTimeControl)
343       break;
344     if (LoadInst *L1 = canHoistFromBlock(Succ1, L0)) {
345       bool Res = hoistLoad(BB, L0, L1);
346       MergedLoads |= Res;
347       // Don't attempt to hoist above loads that had not been hoisted.
348       if (!Res)
349         break;
350     }
351   }
352   return MergedLoads;
353 }
354 
355 ///
356 /// \brief True when instruction is a sink barrier for a store
357 /// located in Loc
358 ///
359 /// Whenever an instruction could possibly read or modify the
360 /// value being stored or protect against the store from
361 /// happening it is considered a sink barrier.
362 ///
isStoreSinkBarrierInRange(const Instruction & Start,const Instruction & End,MemoryLocation Loc)363 bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
364                                                       const Instruction &End,
365                                                       MemoryLocation Loc) {
366   for (const Instruction &Inst :
367        make_range(Start.getIterator(), End.getIterator()))
368     if (Inst.mayThrow())
369       return true;
370   return AA->canInstructionRangeModRef(Start, End, Loc, MRI_ModRef);
371 }
372 
373 ///
374 /// \brief Check if \p BB contains a store to the same address as \p SI
375 ///
376 /// \return The store in \p  when it is safe to sink. Otherwise return Null.
377 ///
canSinkFromBlock(BasicBlock * BB1,StoreInst * Store0)378 StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
379                                                    StoreInst *Store0) {
380   DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n");
381   BasicBlock *BB0 = Store0->getParent();
382   for (Instruction &Inst : reverse(*BB1)) {
383     auto *Store1 = dyn_cast<StoreInst>(&Inst);
384     if (!Store1)
385       continue;
386 
387     MemoryLocation Loc0 = MemoryLocation::get(Store0);
388     MemoryLocation Loc1 = MemoryLocation::get(Store1);
389     if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
390         !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
391         !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
392       return Store1;
393     }
394   }
395   return nullptr;
396 }
397 
398 ///
399 /// \brief Create a PHI node in BB for the operands of S0 and S1
400 ///
getPHIOperand(BasicBlock * BB,StoreInst * S0,StoreInst * S1)401 PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
402                                               StoreInst *S1) {
403   // Create a phi if the values mismatch.
404   Value *Opd1 = S0->getValueOperand();
405   Value *Opd2 = S1->getValueOperand();
406   if (Opd1 == Opd2)
407     return nullptr;
408 
409   auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink",
410                                 &BB->front());
411   NewPN->addIncoming(Opd1, S0->getParent());
412   NewPN->addIncoming(Opd2, S1->getParent());
413   if (MD && NewPN->getType()->getScalarType()->isPointerTy())
414     MD->invalidateCachedPointerInfo(NewPN);
415   return NewPN;
416 }
417 
418 ///
419 /// \brief Merge two stores to same address and sink into \p BB
420 ///
421 /// Also sinks GEP instruction computing the store address
422 ///
sinkStore(BasicBlock * BB,StoreInst * S0,StoreInst * S1)423 bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
424                                       StoreInst *S1) {
425   // Only one definition?
426   auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
427   auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
428   if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
429       (A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
430       (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) {
431     DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump();
432           dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n";
433           dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n");
434     // Hoist the instruction.
435     BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
436     // Intersect optional metadata.
437     S0->intersectOptionalDataWith(S1);
438     S0->dropUnknownNonDebugMetadata();
439 
440     // Create the new store to be inserted at the join point.
441     StoreInst *SNew = cast<StoreInst>(S0->clone());
442     Instruction *ANew = A0->clone();
443     SNew->insertBefore(&*InsertPt);
444     ANew->insertBefore(SNew);
445 
446     assert(S0->getParent() == A0->getParent());
447     assert(S1->getParent() == A1->getParent());
448 
449     // New PHI operand? Use it.
450     if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
451       SNew->setOperand(0, NewPN);
452     removeInstruction(S0);
453     removeInstruction(S1);
454     A0->replaceAllUsesWith(ANew);
455     removeInstruction(A0);
456     A1->replaceAllUsesWith(ANew);
457     removeInstruction(A1);
458     return true;
459   }
460   return false;
461 }
462 
463 ///
464 /// \brief True when two stores are equivalent and can sink into the footer
465 ///
466 /// Starting from a diamond tail block, iterate over the instructions in one
467 /// predecessor block and try to match a store in the second predecessor.
468 ///
mergeStores(BasicBlock * T)469 bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
470 
471   bool MergedStores = false;
472   assert(T && "Footer of a diamond cannot be empty");
473 
474   pred_iterator PI = pred_begin(T), E = pred_end(T);
475   assert(PI != E);
476   BasicBlock *Pred0 = *PI;
477   ++PI;
478   BasicBlock *Pred1 = *PI;
479   ++PI;
480   // tail block  of a diamond/hammock?
481   if (Pred0 == Pred1)
482     return false; // No.
483   if (PI != E)
484     return false; // No. More than 2 predecessors.
485 
486   // #Instructions in Succ1 for Compile Time Control
487   int Size1 = Pred1->size();
488   int NStores = 0;
489 
490   for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
491        RBI != RBE;) {
492 
493     Instruction *I = &*RBI;
494     ++RBI;
495 
496     // Don't sink non-simple (atomic, volatile) stores.
497     auto *S0 = dyn_cast<StoreInst>(I);
498     if (!S0 || !S0->isSimple())
499       continue;
500 
501     ++NStores;
502     if (NStores * Size1 >= MagicCompileTimeControl)
503       break;
504     if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
505       bool Res = sinkStore(T, S0, S1);
506       MergedStores |= Res;
507       // Don't attempt to sink below stores that had to stick around
508       // But after removal of a store and some of its feeding
509       // instruction search again from the beginning since the iterator
510       // is likely stale at this point.
511       if (!Res)
512         break;
513       RBI = Pred0->rbegin();
514       RBE = Pred0->rend();
515       DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump());
516     }
517   }
518   return MergedStores;
519 }
520 
run(Function & F,MemoryDependenceResults * MD,AliasAnalysis & AA)521 bool MergedLoadStoreMotion::run(Function &F, MemoryDependenceResults *MD,
522                                 AliasAnalysis &AA) {
523   this->MD = MD;
524   this->AA = &AA;
525 
526   bool Changed = false;
527   DEBUG(dbgs() << "Instruction Merger\n");
528 
529   // Merge unconditional branches, allowing PRE to catch more
530   // optimization opportunities.
531   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
532     BasicBlock *BB = &*FI++;
533 
534     // Hoist equivalent loads and sink stores
535     // outside diamonds when possible
536     if (isDiamondHead(BB)) {
537       Changed |= mergeLoads(BB);
538       Changed |= mergeStores(getDiamondTail(BB));
539     }
540   }
541   return Changed;
542 }
543 
544 namespace {
545 class MergedLoadStoreMotionLegacyPass : public FunctionPass {
546 public:
547   static char ID; // Pass identification, replacement for typeid
MergedLoadStoreMotionLegacyPass()548   MergedLoadStoreMotionLegacyPass() : FunctionPass(ID) {
549     initializeMergedLoadStoreMotionLegacyPassPass(
550         *PassRegistry::getPassRegistry());
551   }
552 
553   ///
554   /// \brief Run the transformation for each function
555   ///
runOnFunction(Function & F)556   bool runOnFunction(Function &F) override {
557     if (skipFunction(F))
558       return false;
559     MergedLoadStoreMotion Impl;
560     auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
561     return Impl.run(F, MDWP ? &MDWP->getMemDep() : nullptr,
562                     getAnalysis<AAResultsWrapperPass>().getAAResults());
563   }
564 
565 private:
566   // This transformation requires dominator postdominator info
getAnalysisUsage(AnalysisUsage & AU) const567   void getAnalysisUsage(AnalysisUsage &AU) const override {
568     AU.setPreservesCFG();
569     AU.addRequired<AAResultsWrapperPass>();
570     AU.addPreserved<GlobalsAAWrapperPass>();
571     AU.addPreserved<MemoryDependenceWrapperPass>();
572   }
573 };
574 
575 char MergedLoadStoreMotionLegacyPass::ID = 0;
576 } // anonymous namespace
577 
578 ///
579 /// \brief createMergedLoadStoreMotionPass - The public interface to this file.
580 ///
createMergedLoadStoreMotionPass()581 FunctionPass *llvm::createMergedLoadStoreMotionPass() {
582   return new MergedLoadStoreMotionLegacyPass();
583 }
584 
585 INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
586                       "MergedLoadStoreMotion", false, false)
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)587 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
588 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
589 INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
590                     "MergedLoadStoreMotion", false, false)
591 
592 PreservedAnalyses
593 MergedLoadStoreMotionPass::run(Function &F, AnalysisManager<Function> &AM) {
594   MergedLoadStoreMotion Impl;
595   auto *MD = AM.getCachedResult<MemoryDependenceAnalysis>(F);
596   auto &AA = AM.getResult<AAManager>(F);
597   if (!Impl.run(F, MD, AA))
598     return PreservedAnalyses::all();
599 
600   // FIXME: This should also 'preserve the CFG'.
601   PreservedAnalyses PA;
602   PA.preserve<GlobalsAA>();
603   PA.preserve<MemoryDependenceAnalysis>();
604   return PA;
605 }
606