blob: e4af9916880e3e470c63f22c42ce076dee7e3c2a [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//
22//===----------------------------------------------------------------------===//
23//
24//
25// Example:
26// Diamond shaped code before merge:
27//
28// header:
29// br %cond, label %if.then, label %if.else
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000030// + +
31// + +
32// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000033// 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
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000039// + +
40// + +
41// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000042// 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
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000050// + +
51// + +
52// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000053// if.then: if.else:
54// <use %l> <use %l>
55// <...> <...>
56// br label %if.end br label %if.end
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000057// + +
58// + +
59// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000060// 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
Davide Italianob49aa5c2016-06-17 19:10:09 +000075#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000076#include "llvm/ADT/Statistic.h"
77#include "llvm/Analysis/AliasAnalysis.h"
78#include "llvm/Analysis/CFG.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000079#include "llvm/Analysis/GlobalsModRef.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000080#include "llvm/Analysis/Loads.h"
81#include "llvm/Analysis/MemoryBuiltins.h"
82#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Eli Friedman9f8031c2016-06-12 02:11:20 +000083#include "llvm/Analysis/ValueTracking.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000084#include "llvm/IR/Metadata.h"
85#include "llvm/IR/PatternMatch.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000086#include "llvm/Support/Debug.h"
Benjamin Kramerb85d3752015-03-23 18:45:56 +000087#include "llvm/Support/raw_ostream.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000088#include "llvm/Transforms/Scalar.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000089#include "llvm/Transforms/Utils/BasicBlockUtils.h"
90#include "llvm/Transforms/Utils/SSAUpdater.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000091
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000092using namespace llvm;
93
94#define DEBUG_TYPE "mldst-motion"
95
96//===----------------------------------------------------------------------===//
97// MergedLoadStoreMotion Pass
98//===----------------------------------------------------------------------===//
Davide Italianob49aa5c2016-06-17 19:10:09 +000099class MergedLoadStoreMotion {
100 MemoryDependenceResults *MD = nullptr;
101 AliasAnalysis *AA = nullptr;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000102
Davide Italianob49aa5c2016-06-17 19:10:09 +0000103 // The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
104 // where Size0 and Size1 are the #instructions on the two sides of
105 // the diamond. The constant chosen here is arbitrary. Compiler Time
106 // Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl.
107 const int MagicCompileTimeControl = 250;
Davide Italiano41315f72016-06-16 17:40:53 +0000108
109public:
Davide Italianob49aa5c2016-06-17 19:10:09 +0000110 bool run(Function &F, MemoryDependenceResults *MD, AliasAnalysis &AA);
Davide Italiano41315f72016-06-16 17:40:53 +0000111
112private:
Davide Italiano41315f72016-06-16 17:40:53 +0000113 ///
114 /// \brief Remove instruction from parent and update memory dependence
115 /// analysis.
116 ///
117 void removeInstruction(Instruction *Inst);
118 BasicBlock *getDiamondTail(BasicBlock *BB);
119 bool isDiamondHead(BasicBlock *BB);
120 // Routines for hoisting loads
121 bool isLoadHoistBarrierInRange(const Instruction &Start,
122 const Instruction &End, LoadInst *LI,
123 bool SafeToLoadUnconditionally);
124 LoadInst *canHoistFromBlock(BasicBlock *BB, LoadInst *LI);
125 void hoistInstruction(BasicBlock *BB, Instruction *HoistCand,
126 Instruction *ElseInst);
127 bool isSafeToHoist(Instruction *I) const;
128 bool hoistLoad(BasicBlock *BB, LoadInst *HoistCand, LoadInst *ElseInst);
129 bool mergeLoads(BasicBlock *BB);
130 // Routines for sinking stores
131 StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI);
132 PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1);
133 bool isStoreSinkBarrierInRange(const Instruction &Start,
134 const Instruction &End, MemoryLocation Loc);
135 bool sinkStore(BasicBlock *BB, StoreInst *SinkCand, StoreInst *ElseInst);
136 bool mergeStores(BasicBlock *BB);
Davide Italiano41315f72016-06-16 17:40:53 +0000137};
138
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000139///
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000140/// \brief Remove instruction from parent and update memory dependence analysis.
141///
Davide Italiano41315f72016-06-16 17:40:53 +0000142void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000143 // Notify the memory dependence analysis.
144 if (MD) {
145 MD->removeInstruction(Inst);
David Majnemer8cce3332016-05-26 05:43:12 +0000146 if (auto *LI = dyn_cast<LoadInst>(Inst))
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000147 MD->invalidateCachedPointerInfo(LI->getPointerOperand());
David Majnemer8cce3332016-05-26 05:43:12 +0000148 if (Inst->getType()->isPtrOrPtrVectorTy()) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000149 MD->invalidateCachedPointerInfo(Inst);
150 }
151 }
152 Inst->eraseFromParent();
153}
154
155///
Davide Italiano41315f72016-06-16 17:40:53 +0000156/// \brief Return tail block of a diamond.
157///
158BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
159 assert(isDiamondHead(BB) && "Basic block is not head of a diamond");
160 return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor();
161}
162
163///
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000164/// \brief True when BB is the head of a diamond (hammock)
165///
Davide Italiano41315f72016-06-16 17:40:53 +0000166bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000167 if (!BB)
168 return false;
David Majnemer8cce3332016-05-26 05:43:12 +0000169 auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
170 if (!BI || !BI->isConditional())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000171 return false;
172
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000173 BasicBlock *Succ0 = BI->getSuccessor(0);
174 BasicBlock *Succ1 = BI->getSuccessor(1);
175
David Majnemer8cce3332016-05-26 05:43:12 +0000176 if (!Succ0->getSinglePredecessor())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000177 return false;
David Majnemer8cce3332016-05-26 05:43:12 +0000178 if (!Succ1->getSinglePredecessor())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000179 return false;
180
David Majnemer8cce3332016-05-26 05:43:12 +0000181 BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
182 BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000183 // Ignore triangles.
David Majnemer8cce3332016-05-26 05:43:12 +0000184 if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000185 return false;
186 return true;
187}
188
189///
190/// \brief True when instruction is a hoist barrier for a load
191///
192/// Whenever an instruction could possibly modify the value
193/// being loaded or protect against the load from happening
194/// it is considered a hoist barrier.
195///
Davide Italiano41315f72016-06-16 17:40:53 +0000196bool MergedLoadStoreMotion::isLoadHoistBarrierInRange(
197 const Instruction &Start, const Instruction &End, LoadInst *LI,
198 bool SafeToLoadUnconditionally) {
David Majnemer47451252016-05-26 07:11:09 +0000199 if (!SafeToLoadUnconditionally)
200 for (const Instruction &Inst :
201 make_range(Start.getIterator(), End.getIterator()))
Eli Friedman9f8031c2016-06-12 02:11:20 +0000202 if (!isGuaranteedToTransferExecutionToSuccessor(&Inst))
David Majnemer47451252016-05-26 07:11:09 +0000203 return true;
Chandler Carruthac80dc72015-06-17 07:18:54 +0000204 MemoryLocation Loc = MemoryLocation::get(LI);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000205 return AA->canInstructionRangeModRef(Start, End, Loc, MRI_Mod);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000206}
207
208///
209/// \brief Decide if a load can be hoisted
210///
211/// When there is a load in \p BB to the same address as \p LI
212/// and it can be hoisted from \p BB, return that load.
213/// Otherwise return Null.
214///
Davide Italiano41315f72016-06-16 17:40:53 +0000215LoadInst *MergedLoadStoreMotion::canHoistFromBlock(BasicBlock *BB1,
216 LoadInst *Load0) {
David Majnemer47451252016-05-26 07:11:09 +0000217 BasicBlock *BB0 = Load0->getParent();
218 BasicBlock *Head = BB0->getSinglePredecessor();
219 bool SafeToLoadUnconditionally = isSafeToLoadUnconditionally(
220 Load0->getPointerOperand(), Load0->getAlignment(),
221 Load0->getModule()->getDataLayout(),
222 /*ScanFrom=*/Head->getTerminator());
Elena Demikhovsky27152ae2014-11-02 08:03:05 +0000223 for (BasicBlock::iterator BBI = BB1->begin(), BBE = BB1->end(); BBI != BBE;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000224 ++BBI) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000225 Instruction *Inst = &*BBI;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000226
227 // Only merge and hoist loads when their result in used only in BB
David Majnemer47451252016-05-26 07:11:09 +0000228 auto *Load1 = dyn_cast<LoadInst>(Inst);
229 if (!Load1 || Inst->isUsedOutsideOfBlock(BB1))
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000230 continue;
231
Chandler Carruthac80dc72015-06-17 07:18:54 +0000232 MemoryLocation Loc0 = MemoryLocation::get(Load0);
233 MemoryLocation Loc1 = MemoryLocation::get(Load1);
Chad Rosier66a9d072016-06-14 12:47:18 +0000234 if (Load0->isSameOperationAs(Load1) && AA->isMustAlias(Loc0, Loc1) &&
David Majnemer47451252016-05-26 07:11:09 +0000235 !isLoadHoistBarrierInRange(BB1->front(), *Load1, Load1,
Davide Italiano41315f72016-06-16 17:40:53 +0000236 SafeToLoadUnconditionally) &&
David Majnemer47451252016-05-26 07:11:09 +0000237 !isLoadHoistBarrierInRange(BB0->front(), *Load0, Load0,
Davide Italiano41315f72016-06-16 17:40:53 +0000238 SafeToLoadUnconditionally)) {
Elena Demikhovsky27152ae2014-11-02 08:03:05 +0000239 return Load1;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000240 }
241 }
Elena Demikhovsky27152ae2014-11-02 08:03:05 +0000242 return nullptr;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000243}
244
245///
246/// \brief Merge two equivalent instructions \p HoistCand and \p ElseInst into
247/// \p BB
248///
249/// BB is the head of a diamond
250///
Davide Italiano41315f72016-06-16 17:40:53 +0000251void MergedLoadStoreMotion::hoistInstruction(BasicBlock *BB,
252 Instruction *HoistCand,
253 Instruction *ElseInst) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000254 DEBUG(dbgs() << " Hoist Instruction into BB \n"; BB->dump();
255 dbgs() << "Instruction Left\n"; HoistCand->dump(); dbgs() << "\n";
256 dbgs() << "Instruction Right\n"; ElseInst->dump(); dbgs() << "\n");
257 // Hoist the instruction.
258 assert(HoistCand->getParent() != BB);
259
260 // Intersect optional metadata.
261 HoistCand->intersectOptionalDataWith(ElseInst);
Adrian Prantlcbdfdb72015-08-20 22:00:30 +0000262 HoistCand->dropUnknownNonDebugMetadata();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000263
264 // Prepend point for instruction insert
265 Instruction *HoistPt = BB->getTerminator();
266
267 // Merged instruction
268 Instruction *HoistedInst = HoistCand->clone();
269
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000270 // Hoist instruction.
271 HoistedInst->insertBefore(HoistPt);
272
273 HoistCand->replaceAllUsesWith(HoistedInst);
Davide Italiano41315f72016-06-16 17:40:53 +0000274 removeInstruction(HoistCand);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000275 // Replace the else block instruction.
276 ElseInst->replaceAllUsesWith(HoistedInst);
Davide Italiano41315f72016-06-16 17:40:53 +0000277 removeInstruction(ElseInst);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000278}
279
280///
281/// \brief Return true if no operand of \p I is defined in I's parent block
282///
Davide Italiano41315f72016-06-16 17:40:53 +0000283bool MergedLoadStoreMotion::isSafeToHoist(Instruction *I) const {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000284 BasicBlock *Parent = I->getParent();
David Majnemer8cce3332016-05-26 05:43:12 +0000285 for (Use &U : I->operands())
286 if (auto *Instr = dyn_cast<Instruction>(&U))
287 if (Instr->getParent() == Parent)
288 return false;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000289 return true;
290}
291
292///
293/// \brief Merge two equivalent loads and GEPs and hoist into diamond head
294///
Davide Italiano41315f72016-06-16 17:40:53 +0000295bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
296 LoadInst *L1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000297 // Only one definition?
David Majnemer8cce3332016-05-26 05:43:12 +0000298 auto *A0 = dyn_cast<Instruction>(L0->getPointerOperand());
299 auto *A1 = dyn_cast<Instruction>(L1->getPointerOperand());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000300 if (A0 && A1 && A0->isIdenticalTo(A1) && isSafeToHoist(A0) &&
301 A0->hasOneUse() && (A0->getParent() == L0->getParent()) &&
302 A1->hasOneUse() && (A1->getParent() == L1->getParent()) &&
303 isa<GetElementPtrInst>(A0)) {
304 DEBUG(dbgs() << "Hoist Instruction into BB \n"; BB->dump();
305 dbgs() << "Instruction Left\n"; L0->dump(); dbgs() << "\n";
306 dbgs() << "Instruction Right\n"; L1->dump(); dbgs() << "\n");
Davide Italiano41315f72016-06-16 17:40:53 +0000307 hoistInstruction(BB, A0, A1);
308 hoistInstruction(BB, L0, L1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000309 return true;
David Majnemer8cce3332016-05-26 05:43:12 +0000310 }
311 return false;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000312}
313
314///
315/// \brief Try to hoist two loads to same address into diamond header
316///
317/// Starting from a diamond head block, iterate over the instructions in one
318/// successor block and try to match a load in the second successor.
319///
Davide Italiano41315f72016-06-16 17:40:53 +0000320bool MergedLoadStoreMotion::mergeLoads(BasicBlock *BB) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000321 bool MergedLoads = false;
322 assert(isDiamondHead(BB));
David Majnemer8cce3332016-05-26 05:43:12 +0000323 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000324 BasicBlock *Succ0 = BI->getSuccessor(0);
325 BasicBlock *Succ1 = BI->getSuccessor(1);
326 // #Instructions in Succ1 for Compile Time Control
327 int Size1 = Succ1->size();
328 int NLoads = 0;
329 for (BasicBlock::iterator BBI = Succ0->begin(), BBE = Succ0->end();
330 BBI != BBE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000331 Instruction *I = &*BBI;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000332 ++BBI;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000333
David Majnemer8cce3332016-05-26 05:43:12 +0000334 // Don't move non-simple (atomic, volatile) loads.
335 auto *L0 = dyn_cast<LoadInst>(I);
Elena Demikhovsky27152ae2014-11-02 08:03:05 +0000336 if (!L0 || !L0->isSimple() || L0->isUsedOutsideOfBlock(Succ0))
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000337 continue;
338
339 ++NLoads;
340 if (NLoads * Size1 >= MagicCompileTimeControl)
341 break;
Davide Italiano41315f72016-06-16 17:40:53 +0000342 if (LoadInst *L1 = canHoistFromBlock(Succ1, L0)) {
343 bool Res = hoistLoad(BB, L0, L1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000344 MergedLoads |= Res;
345 // Don't attempt to hoist above loads that had not been hoisted.
346 if (!Res)
347 break;
348 }
349 }
350 return MergedLoads;
351}
352
353///
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000354/// \brief True when instruction is a sink barrier for a store
355/// located in Loc
356///
357/// Whenever an instruction could possibly read or modify the
358/// value being stored or protect against the store from
359/// happening it is considered a sink barrier.
360///
Davide Italiano41315f72016-06-16 17:40:53 +0000361bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
362 const Instruction &End,
363 MemoryLocation Loc) {
David Majnemer47451252016-05-26 07:11:09 +0000364 for (const Instruction &Inst :
365 make_range(Start.getIterator(), End.getIterator()))
366 if (Inst.mayThrow())
367 return true;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000368 return AA->canInstructionRangeModRef(Start, End, Loc, MRI_ModRef);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000369}
370
371///
372/// \brief Check if \p BB contains a store to the same address as \p SI
373///
374/// \return The store in \p when it is safe to sink. Otherwise return Null.
375///
Davide Italiano41315f72016-06-16 17:40:53 +0000376StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
377 StoreInst *Store0) {
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000378 DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n");
Elena Demikhovskyef035bb2015-02-17 13:10:05 +0000379 BasicBlock *BB0 = Store0->getParent();
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000380 for (BasicBlock::reverse_iterator RBI = BB1->rbegin(), RBE = BB1->rend();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000381 RBI != RBE; ++RBI) {
382 Instruction *Inst = &*RBI;
383
David Majnemer47451252016-05-26 07:11:09 +0000384 auto *Store1 = dyn_cast<StoreInst>(Inst);
385 if (!Store1)
386 continue;
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000387
Chandler Carruthac80dc72015-06-17 07:18:54 +0000388 MemoryLocation Loc0 = MemoryLocation::get(Store0);
389 MemoryLocation Loc1 = MemoryLocation::get(Store1);
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000390 if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
Davide Italiano41315f72016-06-16 17:40:53 +0000391 !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
392 !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000393 return Store1;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000394 }
395 }
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000396 return nullptr;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000397}
398
399///
400/// \brief Create a PHI node in BB for the operands of S0 and S1
401///
Davide Italiano41315f72016-06-16 17:40:53 +0000402PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
403 StoreInst *S1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000404 // Create a phi if the values mismatch.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000405 Value *Opd1 = S0->getValueOperand();
406 Value *Opd2 = S1->getValueOperand();
David Majnemer8cce3332016-05-26 05:43:12 +0000407 if (Opd1 == Opd2)
408 return nullptr;
409
410 auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink",
411 &BB->front());
412 NewPN->addIncoming(Opd1, S0->getParent());
413 NewPN->addIncoming(Opd2, S1->getParent());
414 if (MD && NewPN->getType()->getScalarType()->isPointerTy())
415 MD->invalidateCachedPointerInfo(NewPN);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000416 return NewPN;
417}
418
419///
420/// \brief Merge two stores to same address and sink into \p BB
421///
422/// Also sinks GEP instruction computing the store address
423///
Davide Italiano41315f72016-06-16 17:40:53 +0000424bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
425 StoreInst *S1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000426 // Only one definition?
David Majnemer8cce3332016-05-26 05:43:12 +0000427 auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
428 auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000429 if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
430 (A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
431 (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) {
432 DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump();
433 dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n";
434 dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n");
435 // Hoist the instruction.
436 BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
437 // Intersect optional metadata.
438 S0->intersectOptionalDataWith(S1);
Adrian Prantlcbdfdb72015-08-20 22:00:30 +0000439 S0->dropUnknownNonDebugMetadata();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000440
441 // Create the new store to be inserted at the join point.
David Majnemer8cce3332016-05-26 05:43:12 +0000442 StoreInst *SNew = cast<StoreInst>(S0->clone());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000443 Instruction *ANew = A0->clone();
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000444 SNew->insertBefore(&*InsertPt);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000445 ANew->insertBefore(SNew);
446
447 assert(S0->getParent() == A0->getParent());
448 assert(S1->getParent() == A1->getParent());
449
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000450 // New PHI operand? Use it.
Davide Italiano41315f72016-06-16 17:40:53 +0000451 if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000452 SNew->setOperand(0, NewPN);
Davide Italiano41315f72016-06-16 17:40:53 +0000453 removeInstruction(S0);
454 removeInstruction(S1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000455 A0->replaceAllUsesWith(ANew);
Davide Italiano41315f72016-06-16 17:40:53 +0000456 removeInstruction(A0);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000457 A1->replaceAllUsesWith(ANew);
Davide Italiano41315f72016-06-16 17:40:53 +0000458 removeInstruction(A1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000459 return true;
460 }
461 return false;
462}
463
464///
465/// \brief True when two stores are equivalent and can sink into the footer
466///
467/// Starting from a diamond tail block, iterate over the instructions in one
468/// predecessor block and try to match a store in the second predecessor.
469///
Davide Italiano41315f72016-06-16 17:40:53 +0000470bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000471
472 bool MergedStores = false;
473 assert(T && "Footer of a diamond cannot be empty");
474
475 pred_iterator PI = pred_begin(T), E = pred_end(T);
476 assert(PI != E);
477 BasicBlock *Pred0 = *PI;
478 ++PI;
479 BasicBlock *Pred1 = *PI;
480 ++PI;
481 // tail block of a diamond/hammock?
482 if (Pred0 == Pred1)
483 return false; // No.
484 if (PI != E)
485 return false; // No. More than 2 predecessors.
486
487 // #Instructions in Succ1 for Compile Time Control
488 int Size1 = Pred1->size();
489 int NStores = 0;
490
491 for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
492 RBI != RBE;) {
493
494 Instruction *I = &*RBI;
495 ++RBI;
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000496
David Majnemer8cce3332016-05-26 05:43:12 +0000497 // Don't sink non-simple (atomic, volatile) stores.
498 auto *S0 = dyn_cast<StoreInst>(I);
499 if (!S0 || !S0->isSimple())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000500 continue;
501
502 ++NStores;
503 if (NStores * Size1 >= MagicCompileTimeControl)
504 break;
Davide Italiano41315f72016-06-16 17:40:53 +0000505 if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
506 bool Res = sinkStore(T, S0, S1);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000507 MergedStores |= Res;
508 // Don't attempt to sink below stores that had to stick around
509 // But after removal of a store and some of its feeding
510 // instruction search again from the beginning since the iterator
511 // is likely stale at this point.
512 if (!Res)
513 break;
David Majnemer8cce3332016-05-26 05:43:12 +0000514 RBI = Pred0->rbegin();
515 RBE = Pred0->rend();
516 DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000517 }
518 }
519 return MergedStores;
520}
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000521
Davide Italianob49aa5c2016-06-17 19:10:09 +0000522bool MergedLoadStoreMotion::run(Function &F, MemoryDependenceResults *MD,
523 AliasAnalysis &AA) {
524 this->MD = MD;
525 this->AA = &AA;
Davide Italiano41315f72016-06-16 17:40:53 +0000526
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000527 bool Changed = false;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000528 DEBUG(dbgs() << "Instruction Merger\n");
529
530 // Merge unconditional branches, allowing PRE to catch more
531 // optimization opportunities.
532 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000533 BasicBlock *BB = &*FI++;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000534
535 // Hoist equivalent loads and sink stores
536 // outside diamonds when possible
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000537 if (isDiamondHead(BB)) {
Davide Italiano41315f72016-06-16 17:40:53 +0000538 Changed |= mergeLoads(BB);
539 Changed |= mergeStores(getDiamondTail(BB));
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000540 }
541 }
542 return Changed;
543}
Davide Italianob49aa5c2016-06-17 19:10:09 +0000544
545namespace {
546class MergedLoadStoreMotionLegacyPass : public FunctionPass {
547public:
548 static char ID; // Pass identification, replacement for typeid
549 MergedLoadStoreMotionLegacyPass() : FunctionPass(ID) {
550 initializeMergedLoadStoreMotionLegacyPassPass(
551 *PassRegistry::getPassRegistry());
552 }
553
554 ///
555 /// \brief Run the transformation for each function
556 ///
557 bool runOnFunction(Function &F) override {
558 if (skipFunction(F))
559 return false;
560 MergedLoadStoreMotion Impl;
561 auto *MDWP = getAnalysisIfAvailable<MemoryDependenceWrapperPass>();
562 return Impl.run(F, MDWP ? &MDWP->getMemDep() : nullptr,
563 getAnalysis<AAResultsWrapperPass>().getAAResults());
564 }
565
566private:
567 // This transformation requires dominator postdominator info
568 void getAnalysisUsage(AnalysisUsage &AU) const override {
569 AU.setPreservesCFG();
570 AU.addRequired<AAResultsWrapperPass>();
571 AU.addPreserved<GlobalsAAWrapperPass>();
572 AU.addPreserved<MemoryDependenceWrapperPass>();
573 }
574};
575
576char MergedLoadStoreMotionLegacyPass::ID = 0;
577} // anonymous namespace
578
579///
580/// \brief createMergedLoadStoreMotionPass - The public interface to this file.
581///
582FunctionPass *llvm::createMergedLoadStoreMotionPass() {
583 return new MergedLoadStoreMotionLegacyPass();
584}
585
586INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
587 "MergedLoadStoreMotion", false, false)
588INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
589INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
590INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
591 "MergedLoadStoreMotion", false, false)
592
593PreservedAnalyses
594MergedLoadStoreMotionPass::run(Function &F, AnalysisManager<Function> &AM) {
595 MergedLoadStoreMotion Impl;
596 auto *MD = AM.getCachedResult<MemoryDependenceAnalysis>(F);
597 auto &AA = AM.getResult<AAManager>(F);
598 if (!Impl.run(F, MD, AA))
599 return PreservedAnalyses::all();
600
601 // FIXME: This pass should also 'preserve the CFG'.
602 // The new pass manager has currently no way to do it.
603 PreservedAnalyses PA;
604 PA.preserve<GlobalsAA>();
605 PA.preserve<MemoryDependenceAnalysis>();
606 return PA;
607}