191bc56edSDimitry Andric //===- MergedLoadStoreMotion.cpp - merge and hoist/sink load/stores -------===//
291bc56edSDimitry Andric //
391bc56edSDimitry Andric //                     The LLVM Compiler Infrastructure
491bc56edSDimitry Andric //
591bc56edSDimitry Andric // This file is distributed under the University of Illinois Open Source
691bc56edSDimitry Andric // License. See LICENSE.TXT for details.
791bc56edSDimitry Andric //
891bc56edSDimitry Andric //===----------------------------------------------------------------------===//
991bc56edSDimitry Andric //
1091bc56edSDimitry Andric //! \file
114ba319b5SDimitry Andric //! This pass performs merges of loads and stores on both sides of a
1291bc56edSDimitry Andric //  diamond (hammock). It hoists the loads and sinks the stores.
1391bc56edSDimitry Andric //
1491bc56edSDimitry Andric // The algorithm iteratively hoists two loads to the same address out of a
1591bc56edSDimitry Andric // diamond (hammock) and merges them into a single load in the header. Similar
1691bc56edSDimitry Andric // it sinks and merges two stores to the tail block (footer). The algorithm
1791bc56edSDimitry Andric // iterates over the instructions of one side of the diamond and attempts to
1891bc56edSDimitry Andric // find a matching load/store on the other side. It hoists / sinks when it
1991bc56edSDimitry Andric // thinks it safe to do so.  This optimization helps with eg. hiding load
2091bc56edSDimitry Andric // latencies, triggering if-conversion, and reducing static code size.
2191bc56edSDimitry Andric //
227a7e6055SDimitry Andric // NOTE: This code no longer performs load hoisting, it is subsumed by GVNHoist.
237a7e6055SDimitry Andric //
2491bc56edSDimitry Andric //===----------------------------------------------------------------------===//
2591bc56edSDimitry Andric //
2691bc56edSDimitry Andric //
2791bc56edSDimitry Andric // Example:
2891bc56edSDimitry Andric // Diamond shaped code before merge:
2991bc56edSDimitry Andric //
3091bc56edSDimitry Andric //            header:
3191bc56edSDimitry Andric //                     br %cond, label %if.then, label %if.else
329cac79b3SDimitry Andric //                        +                    +
339cac79b3SDimitry Andric //                       +                      +
349cac79b3SDimitry Andric //                      +                        +
3591bc56edSDimitry Andric //            if.then:                         if.else:
3691bc56edSDimitry Andric //               %lt = load %addr_l               %le = load %addr_l
3791bc56edSDimitry Andric //               <use %lt>                        <use %le>
3891bc56edSDimitry Andric //               <...>                            <...>
3991bc56edSDimitry Andric //               store %st, %addr_s               store %se, %addr_s
4091bc56edSDimitry Andric //               br label %if.end                 br label %if.end
419cac79b3SDimitry Andric //                     +                         +
429cac79b3SDimitry Andric //                      +                       +
439cac79b3SDimitry Andric //                       +                     +
4491bc56edSDimitry Andric //            if.end ("footer"):
4591bc56edSDimitry Andric //                     <...>
4691bc56edSDimitry Andric //
4791bc56edSDimitry Andric // Diamond shaped code after merge:
4891bc56edSDimitry Andric //
4991bc56edSDimitry Andric //            header:
5091bc56edSDimitry Andric //                     %l = load %addr_l
5191bc56edSDimitry Andric //                     br %cond, label %if.then, label %if.else
529cac79b3SDimitry Andric //                        +                    +
539cac79b3SDimitry Andric //                       +                      +
549cac79b3SDimitry Andric //                      +                        +
5591bc56edSDimitry Andric //            if.then:                         if.else:
5691bc56edSDimitry Andric //               <use %l>                         <use %l>
5791bc56edSDimitry Andric //               <...>                            <...>
5891bc56edSDimitry Andric //               br label %if.end                 br label %if.end
599cac79b3SDimitry Andric //                      +                        +
609cac79b3SDimitry Andric //                       +                      +
619cac79b3SDimitry Andric //                        +                    +
6291bc56edSDimitry Andric //            if.end ("footer"):
6391bc56edSDimitry Andric //                     %s.sink = phi [%st, if.then], [%se, if.else]
6491bc56edSDimitry Andric //                     <...>
6591bc56edSDimitry Andric //                     store %s.sink, %addr_s
6691bc56edSDimitry Andric //                     <...>
6791bc56edSDimitry Andric //
6891bc56edSDimitry Andric //
6991bc56edSDimitry Andric //===----------------------- TODO -----------------------------------------===//
7091bc56edSDimitry Andric //
7191bc56edSDimitry Andric // 1) Generalize to regions other than diamonds
7291bc56edSDimitry Andric // 2) Be more aggressive merging memory operations
7391bc56edSDimitry Andric // Note that both changes require register pressure control
7491bc56edSDimitry Andric //
7591bc56edSDimitry Andric //===----------------------------------------------------------------------===//
7691bc56edSDimitry Andric 
773ca95b02SDimitry Andric #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
7891bc56edSDimitry Andric #include "llvm/ADT/Statistic.h"
7991bc56edSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
8091bc56edSDimitry Andric #include "llvm/Analysis/CFG.h"
817d523365SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
8291bc56edSDimitry Andric #include "llvm/Analysis/Loads.h"
833ca95b02SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
8491bc56edSDimitry Andric #include "llvm/IR/Metadata.h"
8591bc56edSDimitry Andric #include "llvm/Support/Debug.h"
86ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
873ca95b02SDimitry Andric #include "llvm/Transforms/Scalar.h"
8891bc56edSDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
897d523365SDimitry Andric 
9091bc56edSDimitry Andric using namespace llvm;
9191bc56edSDimitry Andric 
9291bc56edSDimitry Andric #define DEBUG_TYPE "mldst-motion"
9391bc56edSDimitry Andric 
943ca95b02SDimitry Andric namespace {
9591bc56edSDimitry Andric //===----------------------------------------------------------------------===//
9691bc56edSDimitry Andric //                         MergedLoadStoreMotion Pass
9791bc56edSDimitry Andric //===----------------------------------------------------------------------===//
983ca95b02SDimitry Andric class MergedLoadStoreMotion {
993ca95b02SDimitry Andric   AliasAnalysis *AA = nullptr;
10091bc56edSDimitry Andric 
1013ca95b02SDimitry Andric   // The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
1023ca95b02SDimitry Andric   // where Size0 and Size1 are the #instructions on the two sides of
1033ca95b02SDimitry Andric   // the diamond. The constant chosen here is arbitrary. Compiler Time
1043ca95b02SDimitry Andric   // Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl.
1053ca95b02SDimitry Andric   const int MagicCompileTimeControl = 250;
10691bc56edSDimitry Andric 
10791bc56edSDimitry Andric public:
1084ba319b5SDimitry Andric   bool run(Function &F, AliasAnalysis &AA);
10991bc56edSDimitry Andric 
11091bc56edSDimitry Andric private:
11191bc56edSDimitry Andric   BasicBlock *getDiamondTail(BasicBlock *BB);
11291bc56edSDimitry Andric   bool isDiamondHead(BasicBlock *BB);
11391bc56edSDimitry Andric   // Routines for sinking stores
11491bc56edSDimitry Andric   StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI);
11591bc56edSDimitry Andric   PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1);
11639d628a0SDimitry Andric   bool isStoreSinkBarrierInRange(const Instruction &Start,
1178f0fd8f6SDimitry Andric                                  const Instruction &End, MemoryLocation Loc);
11891bc56edSDimitry Andric   bool sinkStore(BasicBlock *BB, StoreInst *SinkCand, StoreInst *ElseInst);
11991bc56edSDimitry Andric   bool mergeStores(BasicBlock *BB);
12091bc56edSDimitry Andric };
1213ca95b02SDimitry Andric } // end anonymous namespace
12291bc56edSDimitry Andric 
12391bc56edSDimitry Andric ///
1244ba319b5SDimitry Andric /// Return tail block of a diamond.
12591bc56edSDimitry Andric ///
getDiamondTail(BasicBlock * BB)12691bc56edSDimitry Andric BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
12791bc56edSDimitry Andric   assert(isDiamondHead(BB) && "Basic block is not head of a diamond");
1283ca95b02SDimitry Andric   return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor();
12991bc56edSDimitry Andric }
13091bc56edSDimitry Andric 
13191bc56edSDimitry Andric ///
1324ba319b5SDimitry Andric /// True when BB is the head of a diamond (hammock)
13391bc56edSDimitry Andric ///
isDiamondHead(BasicBlock * BB)13491bc56edSDimitry Andric bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
13591bc56edSDimitry Andric   if (!BB)
13691bc56edSDimitry Andric     return false;
1373ca95b02SDimitry Andric   auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
1383ca95b02SDimitry Andric   if (!BI || !BI->isConditional())
13991bc56edSDimitry Andric     return false;
14091bc56edSDimitry Andric 
14191bc56edSDimitry Andric   BasicBlock *Succ0 = BI->getSuccessor(0);
14291bc56edSDimitry Andric   BasicBlock *Succ1 = BI->getSuccessor(1);
14391bc56edSDimitry Andric 
1443ca95b02SDimitry Andric   if (!Succ0->getSinglePredecessor())
14591bc56edSDimitry Andric     return false;
1463ca95b02SDimitry Andric   if (!Succ1->getSinglePredecessor())
14791bc56edSDimitry Andric     return false;
14891bc56edSDimitry Andric 
1493ca95b02SDimitry Andric   BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
1503ca95b02SDimitry Andric   BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
15191bc56edSDimitry Andric   // Ignore triangles.
1523ca95b02SDimitry Andric   if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
15391bc56edSDimitry Andric     return false;
15491bc56edSDimitry Andric   return true;
15591bc56edSDimitry Andric }
15691bc56edSDimitry Andric 
15791bc56edSDimitry Andric 
15891bc56edSDimitry Andric ///
1594ba319b5SDimitry Andric /// True when instruction is a sink barrier for a store
16039d628a0SDimitry Andric /// located in Loc
16191bc56edSDimitry Andric ///
16239d628a0SDimitry Andric /// Whenever an instruction could possibly read or modify the
16339d628a0SDimitry Andric /// value being stored or protect against the store from
16439d628a0SDimitry Andric /// happening it is considered a sink barrier.
16539d628a0SDimitry Andric ///
isStoreSinkBarrierInRange(const Instruction & Start,const Instruction & End,MemoryLocation Loc)16639d628a0SDimitry Andric bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
16739d628a0SDimitry Andric                                                       const Instruction &End,
1688f0fd8f6SDimitry Andric                                                       MemoryLocation Loc) {
1693ca95b02SDimitry Andric   for (const Instruction &Inst :
1703ca95b02SDimitry Andric        make_range(Start.getIterator(), End.getIterator()))
1713ca95b02SDimitry Andric     if (Inst.mayThrow())
1723ca95b02SDimitry Andric       return true;
1732cab237bSDimitry Andric   return AA->canInstructionRangeModRef(Start, End, Loc, ModRefInfo::ModRef);
17491bc56edSDimitry Andric }
17591bc56edSDimitry Andric 
17691bc56edSDimitry Andric ///
1774ba319b5SDimitry Andric /// Check if \p BB contains a store to the same address as \p SI
17891bc56edSDimitry Andric ///
17991bc56edSDimitry Andric /// \return The store in \p  when it is safe to sink. Otherwise return Null.
18091bc56edSDimitry Andric ///
canSinkFromBlock(BasicBlock * BB1,StoreInst * Store0)18139d628a0SDimitry Andric StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
18239d628a0SDimitry Andric                                                    StoreInst *Store0) {
1834ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n");
184b09980d1SDimitry Andric   BasicBlock *BB0 = Store0->getParent();
1853ca95b02SDimitry Andric   for (Instruction &Inst : reverse(*BB1)) {
1863ca95b02SDimitry Andric     auto *Store1 = dyn_cast<StoreInst>(&Inst);
1873ca95b02SDimitry Andric     if (!Store1)
18839d628a0SDimitry Andric       continue;
18939d628a0SDimitry Andric 
1908f0fd8f6SDimitry Andric     MemoryLocation Loc0 = MemoryLocation::get(Store0);
1918f0fd8f6SDimitry Andric     MemoryLocation Loc1 = MemoryLocation::get(Store1);
19239d628a0SDimitry Andric     if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
1933ca95b02SDimitry Andric         !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
1943ca95b02SDimitry Andric         !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
19539d628a0SDimitry Andric       return Store1;
19691bc56edSDimitry Andric     }
19791bc56edSDimitry Andric   }
19839d628a0SDimitry Andric   return nullptr;
19991bc56edSDimitry Andric }
20091bc56edSDimitry Andric 
20191bc56edSDimitry Andric ///
2024ba319b5SDimitry Andric /// Create a PHI node in BB for the operands of S0 and S1
20391bc56edSDimitry Andric ///
getPHIOperand(BasicBlock * BB,StoreInst * S0,StoreInst * S1)20491bc56edSDimitry Andric PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
20591bc56edSDimitry Andric                                               StoreInst *S1) {
20691bc56edSDimitry Andric   // Create a phi if the values mismatch.
20791bc56edSDimitry Andric   Value *Opd1 = S0->getValueOperand();
20891bc56edSDimitry Andric   Value *Opd2 = S1->getValueOperand();
2093ca95b02SDimitry Andric   if (Opd1 == Opd2)
2103ca95b02SDimitry Andric     return nullptr;
2113ca95b02SDimitry Andric 
2123ca95b02SDimitry Andric   auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink",
2137d523365SDimitry Andric                                 &BB->front());
214*b5893f02SDimitry Andric   NewPN->applyMergedLocation(S0->getDebugLoc(), S1->getDebugLoc());
21591bc56edSDimitry Andric   NewPN->addIncoming(Opd1, S0->getParent());
21691bc56edSDimitry Andric   NewPN->addIncoming(Opd2, S1->getParent());
21791bc56edSDimitry Andric   return NewPN;
21891bc56edSDimitry Andric }
21991bc56edSDimitry Andric 
22091bc56edSDimitry Andric ///
2214ba319b5SDimitry Andric /// Merge two stores to same address and sink into \p BB
22291bc56edSDimitry Andric ///
22391bc56edSDimitry Andric /// Also sinks GEP instruction computing the store address
22491bc56edSDimitry Andric ///
sinkStore(BasicBlock * BB,StoreInst * S0,StoreInst * S1)22591bc56edSDimitry Andric bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
22691bc56edSDimitry Andric                                       StoreInst *S1) {
22791bc56edSDimitry Andric   // Only one definition?
2283ca95b02SDimitry Andric   auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
2293ca95b02SDimitry Andric   auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
23091bc56edSDimitry Andric   if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
23191bc56edSDimitry Andric       (A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
23291bc56edSDimitry Andric       (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) {
2334ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump();
23491bc56edSDimitry Andric                dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n";
23591bc56edSDimitry Andric                dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n");
23691bc56edSDimitry Andric     // Hoist the instruction.
23791bc56edSDimitry Andric     BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
23891bc56edSDimitry Andric     // Intersect optional metadata.
239d88c1a5aSDimitry Andric     S0->andIRFlags(S1);
2407d523365SDimitry Andric     S0->dropUnknownNonDebugMetadata();
24191bc56edSDimitry Andric 
24291bc56edSDimitry Andric     // Create the new store to be inserted at the join point.
2433ca95b02SDimitry Andric     StoreInst *SNew = cast<StoreInst>(S0->clone());
24491bc56edSDimitry Andric     Instruction *ANew = A0->clone();
2457d523365SDimitry Andric     SNew->insertBefore(&*InsertPt);
24691bc56edSDimitry Andric     ANew->insertBefore(SNew);
24791bc56edSDimitry Andric 
24891bc56edSDimitry Andric     assert(S0->getParent() == A0->getParent());
24991bc56edSDimitry Andric     assert(S1->getParent() == A1->getParent());
25091bc56edSDimitry Andric 
25191bc56edSDimitry Andric     // New PHI operand? Use it.
2523ca95b02SDimitry Andric     if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
25391bc56edSDimitry Andric       SNew->setOperand(0, NewPN);
2544ba319b5SDimitry Andric     S0->eraseFromParent();
2554ba319b5SDimitry Andric     S1->eraseFromParent();
25691bc56edSDimitry Andric     A0->replaceAllUsesWith(ANew);
2574ba319b5SDimitry Andric     A0->eraseFromParent();
25891bc56edSDimitry Andric     A1->replaceAllUsesWith(ANew);
2594ba319b5SDimitry Andric     A1->eraseFromParent();
26091bc56edSDimitry Andric     return true;
26191bc56edSDimitry Andric   }
26291bc56edSDimitry Andric   return false;
26391bc56edSDimitry Andric }
26491bc56edSDimitry Andric 
26591bc56edSDimitry Andric ///
2664ba319b5SDimitry Andric /// True when two stores are equivalent and can sink into the footer
26791bc56edSDimitry Andric ///
26891bc56edSDimitry Andric /// Starting from a diamond tail block, iterate over the instructions in one
26991bc56edSDimitry Andric /// predecessor block and try to match a store in the second predecessor.
27091bc56edSDimitry Andric ///
mergeStores(BasicBlock * T)27191bc56edSDimitry Andric bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
27291bc56edSDimitry Andric 
27391bc56edSDimitry Andric   bool MergedStores = false;
27491bc56edSDimitry Andric   assert(T && "Footer of a diamond cannot be empty");
27591bc56edSDimitry Andric 
27691bc56edSDimitry Andric   pred_iterator PI = pred_begin(T), E = pred_end(T);
27791bc56edSDimitry Andric   assert(PI != E);
27891bc56edSDimitry Andric   BasicBlock *Pred0 = *PI;
27991bc56edSDimitry Andric   ++PI;
28091bc56edSDimitry Andric   BasicBlock *Pred1 = *PI;
28191bc56edSDimitry Andric   ++PI;
28291bc56edSDimitry Andric   // tail block  of a diamond/hammock?
28391bc56edSDimitry Andric   if (Pred0 == Pred1)
28491bc56edSDimitry Andric     return false; // No.
28591bc56edSDimitry Andric   if (PI != E)
28691bc56edSDimitry Andric     return false; // No. More than 2 predecessors.
28791bc56edSDimitry Andric 
28891bc56edSDimitry Andric   // #Instructions in Succ1 for Compile Time Control
2894ba319b5SDimitry Andric   auto InstsNoDbg = Pred1->instructionsWithoutDebug();
2904ba319b5SDimitry Andric   int Size1 = std::distance(InstsNoDbg.begin(), InstsNoDbg.end());
29191bc56edSDimitry Andric   int NStores = 0;
29291bc56edSDimitry Andric 
29391bc56edSDimitry Andric   for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
29491bc56edSDimitry Andric        RBI != RBE;) {
29591bc56edSDimitry Andric 
29691bc56edSDimitry Andric     Instruction *I = &*RBI;
29791bc56edSDimitry Andric     ++RBI;
29839d628a0SDimitry Andric 
2993ca95b02SDimitry Andric     // Don't sink non-simple (atomic, volatile) stores.
3003ca95b02SDimitry Andric     auto *S0 = dyn_cast<StoreInst>(I);
3013ca95b02SDimitry Andric     if (!S0 || !S0->isSimple())
30291bc56edSDimitry Andric       continue;
30391bc56edSDimitry Andric 
30491bc56edSDimitry Andric     ++NStores;
30591bc56edSDimitry Andric     if (NStores * Size1 >= MagicCompileTimeControl)
30691bc56edSDimitry Andric       break;
30791bc56edSDimitry Andric     if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
30891bc56edSDimitry Andric       bool Res = sinkStore(T, S0, S1);
30991bc56edSDimitry Andric       MergedStores |= Res;
31091bc56edSDimitry Andric       // Don't attempt to sink below stores that had to stick around
31191bc56edSDimitry Andric       // But after removal of a store and some of its feeding
31291bc56edSDimitry Andric       // instruction search again from the beginning since the iterator
31391bc56edSDimitry Andric       // is likely stale at this point.
31491bc56edSDimitry Andric       if (!Res)
31591bc56edSDimitry Andric         break;
31691bc56edSDimitry Andric       RBI = Pred0->rbegin();
31791bc56edSDimitry Andric       RBE = Pred0->rend();
3184ba319b5SDimitry Andric       LLVM_DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump());
31991bc56edSDimitry Andric     }
32091bc56edSDimitry Andric   }
32191bc56edSDimitry Andric   return MergedStores;
32291bc56edSDimitry Andric }
3237d523365SDimitry Andric 
run(Function & F,AliasAnalysis & AA)3244ba319b5SDimitry Andric bool MergedLoadStoreMotion::run(Function &F, AliasAnalysis &AA) {
3253ca95b02SDimitry Andric   this->AA = &AA;
32691bc56edSDimitry Andric 
32791bc56edSDimitry Andric   bool Changed = false;
3284ba319b5SDimitry Andric   LLVM_DEBUG(dbgs() << "Instruction Merger\n");
32991bc56edSDimitry Andric 
33091bc56edSDimitry Andric   // Merge unconditional branches, allowing PRE to catch more
33191bc56edSDimitry Andric   // optimization opportunities.
33291bc56edSDimitry Andric   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
3337d523365SDimitry Andric     BasicBlock *BB = &*FI++;
33491bc56edSDimitry Andric 
33591bc56edSDimitry Andric     // Hoist equivalent loads and sink stores
33691bc56edSDimitry Andric     // outside diamonds when possible
33791bc56edSDimitry Andric     if (isDiamondHead(BB)) {
33891bc56edSDimitry Andric       Changed |= mergeStores(getDiamondTail(BB));
33991bc56edSDimitry Andric     }
34091bc56edSDimitry Andric   }
34191bc56edSDimitry Andric   return Changed;
34291bc56edSDimitry Andric }
3433ca95b02SDimitry Andric 
3443ca95b02SDimitry Andric namespace {
3453ca95b02SDimitry Andric class MergedLoadStoreMotionLegacyPass : public FunctionPass {
3463ca95b02SDimitry Andric public:
3473ca95b02SDimitry Andric   static char ID; // Pass identification, replacement for typeid
MergedLoadStoreMotionLegacyPass()3483ca95b02SDimitry Andric   MergedLoadStoreMotionLegacyPass() : FunctionPass(ID) {
3493ca95b02SDimitry Andric     initializeMergedLoadStoreMotionLegacyPassPass(
3503ca95b02SDimitry Andric         *PassRegistry::getPassRegistry());
3513ca95b02SDimitry Andric   }
3523ca95b02SDimitry Andric 
3533ca95b02SDimitry Andric   ///
3544ba319b5SDimitry Andric   /// Run the transformation for each function
3553ca95b02SDimitry Andric   ///
runOnFunction(Function & F)3563ca95b02SDimitry Andric   bool runOnFunction(Function &F) override {
3573ca95b02SDimitry Andric     if (skipFunction(F))
3583ca95b02SDimitry Andric       return false;
3593ca95b02SDimitry Andric     MergedLoadStoreMotion Impl;
3604ba319b5SDimitry Andric     return Impl.run(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
3613ca95b02SDimitry Andric   }
3623ca95b02SDimitry Andric 
3633ca95b02SDimitry Andric private:
getAnalysisUsage(AnalysisUsage & AU) const3643ca95b02SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
3653ca95b02SDimitry Andric     AU.setPreservesCFG();
3663ca95b02SDimitry Andric     AU.addRequired<AAResultsWrapperPass>();
3673ca95b02SDimitry Andric     AU.addPreserved<GlobalsAAWrapperPass>();
3683ca95b02SDimitry Andric   }
3693ca95b02SDimitry Andric };
3703ca95b02SDimitry Andric 
3713ca95b02SDimitry Andric char MergedLoadStoreMotionLegacyPass::ID = 0;
3723ca95b02SDimitry Andric } // anonymous namespace
3733ca95b02SDimitry Andric 
3743ca95b02SDimitry Andric ///
3754ba319b5SDimitry Andric /// createMergedLoadStoreMotionPass - The public interface to this file.
3763ca95b02SDimitry Andric ///
createMergedLoadStoreMotionPass()3773ca95b02SDimitry Andric FunctionPass *llvm::createMergedLoadStoreMotionPass() {
3783ca95b02SDimitry Andric   return new MergedLoadStoreMotionLegacyPass();
3793ca95b02SDimitry Andric }
3803ca95b02SDimitry Andric 
3813ca95b02SDimitry Andric INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
3823ca95b02SDimitry Andric                       "MergedLoadStoreMotion", false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)3833ca95b02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
3843ca95b02SDimitry Andric INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
3853ca95b02SDimitry Andric                     "MergedLoadStoreMotion", false, false)
3863ca95b02SDimitry Andric 
3873ca95b02SDimitry Andric PreservedAnalyses
388d88c1a5aSDimitry Andric MergedLoadStoreMotionPass::run(Function &F, FunctionAnalysisManager &AM) {
3893ca95b02SDimitry Andric   MergedLoadStoreMotion Impl;
3903ca95b02SDimitry Andric   auto &AA = AM.getResult<AAManager>(F);
3914ba319b5SDimitry Andric   if (!Impl.run(F, AA))
3923ca95b02SDimitry Andric     return PreservedAnalyses::all();
3933ca95b02SDimitry Andric 
3943ca95b02SDimitry Andric   PreservedAnalyses PA;
3957a7e6055SDimitry Andric   PA.preserveSet<CFGAnalyses>();
3963ca95b02SDimitry Andric   PA.preserve<GlobalsAA>();
3973ca95b02SDimitry Andric   return PA;
3983ca95b02SDimitry Andric }
399