blob: acd3ef6791bed540d336a4c226e2f15d9d9df2d1 [file] [log] [blame]
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +00001//===- 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//
Daniel Berlin390dfde2017-01-24 19:55:36 +000022// NOTE: This code no longer performs load hoisting, it is subsumed by GVNHoist.
23//
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000024//===----------------------------------------------------------------------===//
25//
26//
27// Example:
28// Diamond shaped code before merge:
29//
30// header:
31// br %cond, label %if.then, label %if.else
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000032// + +
33// + +
34// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000035// if.then: if.else:
36// %lt = load %addr_l %le = load %addr_l
37// <use %lt> <use %le>
38// <...> <...>
39// store %st, %addr_s store %se, %addr_s
40// br label %if.end br label %if.end
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000041// + +
42// + +
43// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000044// if.end ("footer"):
45// <...>
46//
47// Diamond shaped code after merge:
48//
49// header:
50// %l = load %addr_l
51// br %cond, label %if.then, label %if.else
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000052// + +
53// + +
54// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000055// if.then: if.else:
56// <use %l> <use %l>
57// <...> <...>
58// br label %if.end br label %if.end
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000059// + +
60// + +
61// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000062// if.end ("footer"):
63// %s.sink = phi [%st, if.then], [%se, if.else]
64// <...>
65// store %s.sink, %addr_s
66// <...>
67//
68//
69//===----------------------- TODO -----------------------------------------===//
70//
71// 1) Generalize to regions other than diamonds
72// 2) Be more aggressive merging memory operations
73// Note that both changes require register pressure control
74//
75//===----------------------------------------------------------------------===//
76
Davide Italianob49aa5c2016-06-17 19:10:09 +000077#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000078#include "llvm/ADT/Statistic.h"
79#include "llvm/Analysis/AliasAnalysis.h"
80#include "llvm/Analysis/CFG.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000081#include "llvm/Analysis/GlobalsModRef.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000082#include "llvm/Analysis/Loads.h"
83#include "llvm/Analysis/MemoryBuiltins.h"
84#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Eli Friedman9f8031c2016-06-12 02:11:20 +000085#include "llvm/Analysis/ValueTracking.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000086#include "llvm/IR/Metadata.h"
87#include "llvm/IR/PatternMatch.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000088#include "llvm/Support/Debug.h"
Benjamin Kramerb85d3752015-03-23 18:45:56 +000089#include "llvm/Support/raw_ostream.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000090#include "llvm/Transforms/Scalar.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000091#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000092
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000093using namespace llvm;
94
95#define DEBUG_TYPE "mldst-motion"
96
Benjamin Kramer4d098922016-07-10 11:28:51 +000097namespace {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000098//===----------------------------------------------------------------------===//
99// MergedLoadStoreMotion Pass
100//===----------------------------------------------------------------------===//
Davide Italianob49aa5c2016-06-17 19:10:09 +0000101class MergedLoadStoreMotion {
102 MemoryDependenceResults *MD = nullptr;
103 AliasAnalysis *AA = nullptr;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000104
Davide Italianob49aa5c2016-06-17 19:10:09 +0000105 // The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
106 // where Size0 and Size1 are the #instructions on the two sides of
107 // the diamond. The constant chosen here is arbitrary. Compiler Time
108 // Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl.
109 const int MagicCompileTimeControl = 250;
Davide Italiano41315f72016-06-16 17:40:53 +0000110
111public:
Davide Italianob49aa5c2016-06-17 19:10:09 +0000112 bool run(Function &F, MemoryDependenceResults *MD, AliasAnalysis &AA);
Davide Italiano41315f72016-06-16 17:40:53 +0000113
114private:
Davide Italiano41315f72016-06-16 17:40:53 +0000115 ///
116 /// \brief Remove instruction from parent and update memory dependence
117 /// analysis.
118 ///
119 void removeInstruction(Instruction *Inst);
120 BasicBlock *getDiamondTail(BasicBlock *BB);
121 bool isDiamondHead(BasicBlock *BB);
Davide Italiano41315f72016-06-16 17:40:53 +0000122 // Routines for sinking stores
123 StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI);
124 PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1);
125 bool isStoreSinkBarrierInRange(const Instruction &Start,
126 const Instruction &End, MemoryLocation Loc);
127 bool sinkStore(BasicBlock *BB, StoreInst *SinkCand, StoreInst *ElseInst);
128 bool mergeStores(BasicBlock *BB);
Davide Italiano41315f72016-06-16 17:40:53 +0000129};
Benjamin Kramer4d098922016-07-10 11:28:51 +0000130} // end anonymous namespace
Davide Italiano41315f72016-06-16 17:40:53 +0000131
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000132///
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000133/// \brief Remove instruction from parent and update memory dependence analysis.
134///
Davide Italiano41315f72016-06-16 17:40:53 +0000135void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000136 // Notify the memory dependence analysis.
137 if (MD) {
138 MD->removeInstruction(Inst);
David Majnemer8cce3332016-05-26 05:43:12 +0000139 if (auto *LI = dyn_cast<LoadInst>(Inst))
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000140 MD->invalidateCachedPointerInfo(LI->getPointerOperand());
David Majnemer8cce3332016-05-26 05:43:12 +0000141 if (Inst->getType()->isPtrOrPtrVectorTy()) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000142 MD->invalidateCachedPointerInfo(Inst);
143 }
144 }
145 Inst->eraseFromParent();
146}
147
148///
Davide Italiano41315f72016-06-16 17:40:53 +0000149/// \brief Return tail block of a diamond.
150///
151BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
152 assert(isDiamondHead(BB) && "Basic block is not head of a diamond");
153 return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor();
154}
155
156///
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000157/// \brief True when BB is the head of a diamond (hammock)
158///
Davide Italiano41315f72016-06-16 17:40:53 +0000159bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000160 if (!BB)
161 return false;
David Majnemer8cce3332016-05-26 05:43:12 +0000162 auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
163 if (!BI || !BI->isConditional())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000164 return false;
165
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000166 BasicBlock *Succ0 = BI->getSuccessor(0);
167 BasicBlock *Succ1 = BI->getSuccessor(1);
168
David Majnemer8cce3332016-05-26 05:43:12 +0000169 if (!Succ0->getSinglePredecessor())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000170 return false;
David Majnemer8cce3332016-05-26 05:43:12 +0000171 if (!Succ1->getSinglePredecessor())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000172 return false;
173
David Majnemer8cce3332016-05-26 05:43:12 +0000174 BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
175 BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000176 // Ignore triangles.
David Majnemer8cce3332016-05-26 05:43:12 +0000177 if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000178 return false;
179 return true;
180}
181
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000182
183///
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000184/// \brief True when instruction is a sink barrier for a store
185/// located in Loc
186///
187/// Whenever an instruction could possibly read or modify the
188/// value being stored or protect against the store from
189/// happening it is considered a sink barrier.
190///
Davide Italiano41315f72016-06-16 17:40:53 +0000191bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
192 const Instruction &End,
193 MemoryLocation Loc) {
David Majnemer47451252016-05-26 07:11:09 +0000194 for (const Instruction &Inst :
195 make_range(Start.getIterator(), End.getIterator()))
196 if (Inst.mayThrow())
197 return true;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000198 return AA->canInstructionRangeModRef(Start, End, Loc, MRI_ModRef);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000199}
200
201///
202/// \brief Check if \p BB contains a store to the same address as \p SI
203///
204/// \return The store in \p when it is safe to sink. Otherwise return Null.
205///
Davide Italiano41315f72016-06-16 17:40:53 +0000206StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
207 StoreInst *Store0) {
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000208 DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n");
Elena Demikhovskyef035bb2015-02-17 13:10:05 +0000209 BasicBlock *BB0 = Store0->getParent();
David Majnemerd7708772016-06-24 04:05:21 +0000210 for (Instruction &Inst : reverse(*BB1)) {
211 auto *Store1 = dyn_cast<StoreInst>(&Inst);
David Majnemer47451252016-05-26 07:11:09 +0000212 if (!Store1)
213 continue;
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000214
Chandler Carruthac80dc72015-06-17 07:18:54 +0000215 MemoryLocation Loc0 = MemoryLocation::get(Store0);
216 MemoryLocation Loc1 = MemoryLocation::get(Store1);
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000217 if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
Davide Italiano41315f72016-06-16 17:40:53 +0000218 !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
219 !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000220 return Store1;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000221 }
222 }
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000223 return nullptr;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000224}
225
226///
227/// \brief Create a PHI node in BB for the operands of S0 and S1
228///
Davide Italiano41315f72016-06-16 17:40:53 +0000229PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
230 StoreInst *S1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000231 // Create a phi if the values mismatch.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000232 Value *Opd1 = S0->getValueOperand();
233 Value *Opd2 = S1->getValueOperand();
David Majnemer8cce3332016-05-26 05:43:12 +0000234 if (Opd1 == Opd2)
235 return nullptr;
236
237 auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink",
238 &BB->front());
239 NewPN->addIncoming(Opd1, S0->getParent());
240 NewPN->addIncoming(Opd2, S1->getParent());
241 if (MD && NewPN->getType()->getScalarType()->isPointerTy())
242 MD->invalidateCachedPointerInfo(NewPN);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000243 return NewPN;
244}
245
246///
247/// \brief Merge two stores to same address and sink into \p BB
248///
249/// Also sinks GEP instruction computing the store address
250///
Davide Italiano41315f72016-06-16 17:40:53 +0000251bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
252 StoreInst *S1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000253 // Only one definition?
David Majnemer8cce3332016-05-26 05:43:12 +0000254 auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
255 auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000256 if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
257 (A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
258 (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) {
259 DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump();
260 dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n";
261 dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n");
262 // Hoist the instruction.
263 BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
264 // Intersect optional metadata.
Peter Collingbourne8f1dd5c2016-09-07 23:39:04 +0000265 S0->andIRFlags(S1);
Adrian Prantlcbdfdb72015-08-20 22:00:30 +0000266 S0->dropUnknownNonDebugMetadata();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000267
268 // Create the new store to be inserted at the join point.
David Majnemer8cce3332016-05-26 05:43:12 +0000269 StoreInst *SNew = cast<StoreInst>(S0->clone());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000270 Instruction *ANew = A0->clone();
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000271 SNew->insertBefore(&*InsertPt);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000272 ANew->insertBefore(SNew);
273
274 assert(S0->getParent() == A0->getParent());
275 assert(S1->getParent() == A1->getParent());
276
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000277 // New PHI operand? Use it.
Davide Italiano41315f72016-06-16 17:40:53 +0000278 if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000279 SNew->setOperand(0, NewPN);
Davide Italiano41315f72016-06-16 17:40:53 +0000280 removeInstruction(S0);
281 removeInstruction(S1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000282 A0->replaceAllUsesWith(ANew);
Davide Italiano41315f72016-06-16 17:40:53 +0000283 removeInstruction(A0);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000284 A1->replaceAllUsesWith(ANew);
Davide Italiano41315f72016-06-16 17:40:53 +0000285 removeInstruction(A1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000286 return true;
287 }
288 return false;
289}
290
291///
292/// \brief True when two stores are equivalent and can sink into the footer
293///
294/// Starting from a diamond tail block, iterate over the instructions in one
295/// predecessor block and try to match a store in the second predecessor.
296///
Davide Italiano41315f72016-06-16 17:40:53 +0000297bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000298
299 bool MergedStores = false;
300 assert(T && "Footer of a diamond cannot be empty");
301
302 pred_iterator PI = pred_begin(T), E = pred_end(T);
303 assert(PI != E);
304 BasicBlock *Pred0 = *PI;
305 ++PI;
306 BasicBlock *Pred1 = *PI;
307 ++PI;
308 // tail block of a diamond/hammock?
309 if (Pred0 == Pred1)
310 return false; // No.
311 if (PI != E)
312 return false; // No. More than 2 predecessors.
313
314 // #Instructions in Succ1 for Compile Time Control
315 int Size1 = Pred1->size();
316 int NStores = 0;
317
318 for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
319 RBI != RBE;) {
320
321 Instruction *I = &*RBI;
322 ++RBI;
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000323
David Majnemer8cce3332016-05-26 05:43:12 +0000324 // Don't sink non-simple (atomic, volatile) stores.
325 auto *S0 = dyn_cast<StoreInst>(I);
326 if (!S0 || !S0->isSimple())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000327 continue;
328
329 ++NStores;
330 if (NStores * Size1 >= MagicCompileTimeControl)
331 break;
Davide Italiano41315f72016-06-16 17:40:53 +0000332 if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
333 bool Res = sinkStore(T, S0, S1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000334 MergedStores |= Res;
335 // Don't attempt to sink below stores that had to stick around
336 // But after removal of a store and some of its feeding
337 // instruction search again from the beginning since the iterator
338 // is likely stale at this point.
339 if (!Res)
340 break;
David Majnemer8cce3332016-05-26 05:43:12 +0000341 RBI = Pred0->rbegin();
342 RBE = Pred0->rend();
343 DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000344 }
345 }
346 return MergedStores;
347}
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000348
Davide Italianob49aa5c2016-06-17 19:10:09 +0000349bool MergedLoadStoreMotion::run(Function &F, MemoryDependenceResults *MD,
350 AliasAnalysis &AA) {
351 this->MD = MD;
352 this->AA = &AA;
Davide Italiano41315f72016-06-16 17:40:53 +0000353
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000354 bool Changed = false;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000355 DEBUG(dbgs() << "Instruction Merger\n");
356
357 // Merge unconditional branches, allowing PRE to catch more
358 // optimization opportunities.
359 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000360 BasicBlock *BB = &*FI++;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000361
362 // Hoist equivalent loads and sink stores
363 // outside diamonds when possible
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000364 if (isDiamondHead(BB)) {
Davide Italiano41315f72016-06-16 17:40:53 +0000365 Changed |= mergeStores(getDiamondTail(BB));
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000366 }
367 }
368 return Changed;
369}
Davide Italianob49aa5c2016-06-17 19:10:09 +0000370
371namespace {
372class MergedLoadStoreMotionLegacyPass : public FunctionPass {
373public:
374 static char ID; // Pass identification, replacement for typeid
375 MergedLoadStoreMotionLegacyPass() : FunctionPass(ID) {
376 initializeMergedLoadStoreMotionLegacyPassPass(
377 *PassRegistry::getPassRegistry());
378 }
379
380 ///
381 /// \brief Run the transformation for each function
382 ///
383 bool runOnFunction(Function &F) override {
384 if (skipFunction(F))
385 return false;
386 MergedLoadStoreMotion Impl;
387 auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
388 return Impl.run(F, MDWP ? &MDWP->getMemDep() : nullptr,
389 getAnalysis<AAResultsWrapperPass>().getAAResults());
390 }
391
392private:
Davide Italianob49aa5c2016-06-17 19:10:09 +0000393 void getAnalysisUsage(AnalysisUsage &AU) const override {
394 AU.setPreservesCFG();
395 AU.addRequired<AAResultsWrapperPass>();
396 AU.addPreserved<GlobalsAAWrapperPass>();
397 AU.addPreserved<MemoryDependenceWrapperPass>();
398 }
399};
400
401char MergedLoadStoreMotionLegacyPass::ID = 0;
402} // anonymous namespace
403
404///
405/// \brief createMergedLoadStoreMotionPass - The public interface to this file.
406///
407FunctionPass *llvm::createMergedLoadStoreMotionPass() {
408 return new MergedLoadStoreMotionLegacyPass();
409}
410
411INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
412 "MergedLoadStoreMotion", false, false)
413INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
414INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
415INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
416 "MergedLoadStoreMotion", false, false)
417
418PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000419MergedLoadStoreMotionPass::run(Function &F, FunctionAnalysisManager &AM) {
Davide Italianob49aa5c2016-06-17 19:10:09 +0000420 MergedLoadStoreMotion Impl;
421 auto *MD = AM.getCachedResult<MemoryDependenceAnalysis>(F);
422 auto &AA = AM.getResult<AAManager>(F);
423 if (!Impl.run(F, MD, AA))
424 return PreservedAnalyses::all();
425
Davide Italianob49aa5c2016-06-17 19:10:09 +0000426 PreservedAnalyses PA;
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000427 PA.preserveSet<CFGAnalyses>();
Davide Italianob49aa5c2016-06-17 19:10:09 +0000428 PA.preserve<GlobalsAA>();
429 PA.preserve<MemoryDependenceAnalysis>();
430 return PA;
431}