LLVM  10.0.0svn
MergedLoadStoreMotion.cpp
Go to the documentation of this file.
1 //===- MergedLoadStoreMotion.cpp - merge and hoist/sink load/stores -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //! \file
10 //! This pass performs merges of loads and stores on both sides of a
11 // diamond (hammock). It hoists the loads and sinks the stores.
12 //
13 // The algorithm iteratively hoists two loads to the same address out of a
14 // diamond (hammock) and merges them into a single load in the header. Similar
15 // it sinks and merges two stores to the tail block (footer). The algorithm
16 // iterates over the instructions of one side of the diamond and attempts to
17 // find a matching load/store on the other side. It hoists / sinks when it
18 // thinks it safe to do so. This optimization helps with eg. hiding load
19 // latencies, triggering if-conversion, and reducing static code size.
20 //
21 // NOTE: This code no longer performs load hoisting, it is subsumed by GVNHoist.
22 //
23 //===----------------------------------------------------------------------===//
24 //
25 //
26 // Example:
27 // Diamond shaped code before merge:
28 //
29 // header:
30 // br %cond, label %if.then, label %if.else
31 // + +
32 // + +
33 // + +
34 // if.then: if.else:
35 // %lt = load %addr_l %le = load %addr_l
36 // <use %lt> <use %le>
37 // <...> <...>
38 // store %st, %addr_s store %se, %addr_s
39 // br label %if.end br label %if.end
40 // + +
41 // + +
42 // + +
43 // if.end ("footer"):
44 // <...>
45 //
46 // Diamond shaped code after merge:
47 //
48 // header:
49 // %l = load %addr_l
50 // br %cond, label %if.then, label %if.else
51 // + +
52 // + +
53 // + +
54 // if.then: if.else:
55 // <use %l> <use %l>
56 // <...> <...>
57 // br label %if.end br label %if.end
58 // + +
59 // + +
60 // + +
61 // if.end ("footer"):
62 // %s.sink = phi [%st, if.then], [%se, if.else]
63 // <...>
64 // store %s.sink, %addr_s
65 // <...>
66 //
67 //
68 //===----------------------- TODO -----------------------------------------===//
69 //
70 // 1) Generalize to regions other than diamonds
71 // 2) Be more aggressive merging memory operations
72 // Note that both changes require register pressure control
73 //
74 //===----------------------------------------------------------------------===//
75 
77 #include "llvm/ADT/Statistic.h"
79 #include "llvm/Analysis/CFG.h"
81 #include "llvm/Analysis/Loads.h"
83 #include "llvm/IR/Metadata.h"
84 #include "llvm/Support/Debug.h"
86 #include "llvm/Transforms/Scalar.h"
88 
89 using namespace llvm;
90 
91 #define DEBUG_TYPE "mldst-motion"
92 
93 namespace {
94 //===----------------------------------------------------------------------===//
95 // MergedLoadStoreMotion Pass
96 //===----------------------------------------------------------------------===//
98  AliasAnalysis *AA = nullptr;
99 
100  // The mergeLoad/Store algorithms could have Size0 * Size1 complexity,
101  // where Size0 and Size1 are the #instructions on the two sides of
102  // the diamond. The constant chosen here is arbitrary. Compiler Time
103  // Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl.
104  const int MagicCompileTimeControl = 250;
105 
106 public:
107  bool run(Function &F, AliasAnalysis &AA);
108 
109 private:
110  BasicBlock *getDiamondTail(BasicBlock *BB);
111  bool isDiamondHead(BasicBlock *BB);
112  // Routines for sinking stores
113  StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI);
114  PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1);
115  bool isStoreSinkBarrierInRange(const Instruction &Start,
116  const Instruction &End, MemoryLocation Loc);
117  bool sinkStore(BasicBlock *BB, StoreInst *SinkCand, StoreInst *ElseInst);
118  bool mergeStores(BasicBlock *BB);
119 };
120 } // end anonymous namespace
121 
122 ///
123 /// Return tail block of a diamond.
124 ///
125 BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
126  assert(isDiamondHead(BB) && "Basic block is not head of a diamond");
127  return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor();
128 }
129 
130 ///
131 /// True when BB is the head of a diamond (hammock)
132 ///
133 bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
134  if (!BB)
135  return false;
136  auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
137  if (!BI || !BI->isConditional())
138  return false;
139 
140  BasicBlock *Succ0 = BI->getSuccessor(0);
141  BasicBlock *Succ1 = BI->getSuccessor(1);
142 
143  if (!Succ0->getSinglePredecessor())
144  return false;
145  if (!Succ1->getSinglePredecessor())
146  return false;
147 
148  BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
149  BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
150  // Ignore triangles.
151  if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
152  return false;
153  return true;
154 }
155 
156 
157 ///
158 /// True when instruction is a sink barrier for a store
159 /// located in Loc
160 ///
161 /// Whenever an instruction could possibly read or modify the
162 /// value being stored or protect against the store from
163 /// happening it is considered a sink barrier.
164 ///
165 bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
166  const Instruction &End,
167  MemoryLocation Loc) {
168  for (const Instruction &Inst :
169  make_range(Start.getIterator(), End.getIterator()))
170  if (Inst.mayThrow())
171  return true;
172  return AA->canInstructionRangeModRef(Start, End, Loc, ModRefInfo::ModRef);
173 }
174 
175 ///
176 /// Check if \p BB contains a store to the same address as \p SI
177 ///
178 /// \return The store in \p when it is safe to sink. Otherwise return Null.
179 ///
180 StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
181  StoreInst *Store0) {
182  LLVM_DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n");
183  BasicBlock *BB0 = Store0->getParent();
184  for (Instruction &Inst : reverse(*BB1)) {
185  auto *Store1 = dyn_cast<StoreInst>(&Inst);
186  if (!Store1)
187  continue;
188 
189  MemoryLocation Loc0 = MemoryLocation::get(Store0);
190  MemoryLocation Loc1 = MemoryLocation::get(Store1);
191  if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
192  !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
193  !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
194  return Store1;
195  }
196  }
197  return nullptr;
198 }
199 
200 ///
201 /// Create a PHI node in BB for the operands of S0 and S1
202 ///
203 PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
204  StoreInst *S1) {
205  // Create a phi if the values mismatch.
206  Value *Opd1 = S0->getValueOperand();
207  Value *Opd2 = S1->getValueOperand();
208  if (Opd1 == Opd2)
209  return nullptr;
210 
211  auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink",
212  &BB->front());
213  NewPN->applyMergedLocation(S0->getDebugLoc(), S1->getDebugLoc());
214  NewPN->addIncoming(Opd1, S0->getParent());
215  NewPN->addIncoming(Opd2, S1->getParent());
216  return NewPN;
217 }
218 
219 ///
220 /// Merge two stores to same address and sink into \p BB
221 ///
222 /// Also sinks GEP instruction computing the store address
223 ///
224 bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
225  StoreInst *S1) {
226  // Only one definition?
227  auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
228  auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
229  if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
230  (A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
231  (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) {
232  LLVM_DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump();
233  dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n";
234  dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n");
235  // Hoist the instruction.
236  BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
237  // Intersect optional metadata.
238  S0->andIRFlags(S1);
240 
241  // Create the new store to be inserted at the join point.
242  StoreInst *SNew = cast<StoreInst>(S0->clone());
243  Instruction *ANew = A0->clone();
244  SNew->insertBefore(&*InsertPt);
245  ANew->insertBefore(SNew);
246 
247  assert(S0->getParent() == A0->getParent());
248  assert(S1->getParent() == A1->getParent());
249 
250  // New PHI operand? Use it.
251  if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
252  SNew->setOperand(0, NewPN);
253  S0->eraseFromParent();
254  S1->eraseFromParent();
255  A0->replaceAllUsesWith(ANew);
256  A0->eraseFromParent();
257  A1->replaceAllUsesWith(ANew);
258  A1->eraseFromParent();
259  return true;
260  }
261  return false;
262 }
263 
264 ///
265 /// True when two stores are equivalent and can sink into the footer
266 ///
267 /// Starting from a diamond tail block, iterate over the instructions in one
268 /// predecessor block and try to match a store in the second predecessor.
269 ///
270 bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
271 
272  bool MergedStores = false;
273  assert(T && "Footer of a diamond cannot be empty");
274 
275  pred_iterator PI = pred_begin(T), E = pred_end(T);
276  assert(PI != E);
277  BasicBlock *Pred0 = *PI;
278  ++PI;
279  BasicBlock *Pred1 = *PI;
280  ++PI;
281  // tail block of a diamond/hammock?
282  if (Pred0 == Pred1)
283  return false; // No.
284  if (PI != E)
285  return false; // No. More than 2 predecessors.
286 
287  // #Instructions in Succ1 for Compile Time Control
288  auto InstsNoDbg = Pred1->instructionsWithoutDebug();
289  int Size1 = std::distance(InstsNoDbg.begin(), InstsNoDbg.end());
290  int NStores = 0;
291 
292  for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
293  RBI != RBE;) {
294 
295  Instruction *I = &*RBI;
296  ++RBI;
297 
298  // Don't sink non-simple (atomic, volatile) stores.
299  auto *S0 = dyn_cast<StoreInst>(I);
300  if (!S0 || !S0->isSimple())
301  continue;
302 
303  ++NStores;
304  if (NStores * Size1 >= MagicCompileTimeControl)
305  break;
306  if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
307  bool Res = sinkStore(T, S0, S1);
308  MergedStores |= Res;
309  // Don't attempt to sink below stores that had to stick around
310  // But after removal of a store and some of its feeding
311  // instruction search again from the beginning since the iterator
312  // is likely stale at this point.
313  if (!Res)
314  break;
315  RBI = Pred0->rbegin();
316  RBE = Pred0->rend();
317  LLVM_DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump());
318  }
319  }
320  return MergedStores;
321 }
322 
323 bool MergedLoadStoreMotion::run(Function &F, AliasAnalysis &AA) {
324  this->AA = &AA;
325 
326  bool Changed = false;
327  LLVM_DEBUG(dbgs() << "Instruction Merger\n");
328 
329  // Merge unconditional branches, allowing PRE to catch more
330  // optimization opportunities.
331  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
332  BasicBlock *BB = &*FI++;
333 
334  // Hoist equivalent loads and sink stores
335  // outside diamonds when possible
336  if (isDiamondHead(BB)) {
337  Changed |= mergeStores(getDiamondTail(BB));
338  }
339  }
340  return Changed;
341 }
342 
343 namespace {
344 class MergedLoadStoreMotionLegacyPass : public FunctionPass {
345 public:
346  static char ID; // Pass identification, replacement for typeid
347  MergedLoadStoreMotionLegacyPass() : FunctionPass(ID) {
350  }
351 
352  ///
353  /// Run the transformation for each function
354  ///
355  bool runOnFunction(Function &F) override {
356  if (skipFunction(F))
357  return false;
359  return Impl.run(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
360  }
361 
362 private:
363  void getAnalysisUsage(AnalysisUsage &AU) const override {
364  AU.setPreservesCFG();
367  }
368 };
369 
371 } // anonymous namespace
372 
373 ///
374 /// createMergedLoadStoreMotionPass - The public interface to this file.
375 ///
377  return new MergedLoadStoreMotionLegacyPass();
378 }
379 
380 INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
381  "MergedLoadStoreMotion", false, false)
383 INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
385 
389  auto &AA = AM.getResult<AAManager>(F);
390  if (!Impl.run(F, AA))
391  return PreservedAnalyses::all();
392 
394  PA.preserveSet<CFGAnalyses>();
395  PA.preserve<GlobalsAA>();
396  return PA;
397 }
Legacy wrapper pass to provide the GlobalsAAResult object.
The access may reference and may modify the value stored in memory.
Value * getValueOperand()
Definition: Instructions.h:409
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This is the interface for a simple mod/ref and alias analysis over globals.
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
Definition: Metadata.cpp:1195
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
iterator end()
Definition: Function.h:682
INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion", "MergedLoadStoreMotion", false, false) INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass
This file contains the declarations for metadata subclasses.
F(f)
reverse_iterator rend()
Definition: BasicBlock.h:275
reverse_iterator rbegin()
Definition: BasicBlock.h:273
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:137
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4428
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
mldst motion
mldst MergedLoadStoreMotion
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:273
Instruction * clone() const
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
void andIRFlags(const Value *V)
Logical &#39;and&#39; of any supported wrapping, exact, and fast-math flags of V and this instruction...
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:268
An instruction for storing to memory.
Definition: Instructions.h:320
iterator begin()
Definition: Function.h:680
static bool runOnFunction(Function &F, bool PostInlining)
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:216
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:233
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Definition: Instruction.cpp:73
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
Conditional or Unconditional Branch instruction.
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug() const
Return a const iterator range over the instructions in the block, skipping any debug instructions...
Definition: BasicBlock.cpp:94
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const Instruction & front() const
Definition: BasicBlock.h:280
A manager for alias analyses.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
Represent the analysis usage information of a pass.
const Instruction & back() const
Definition: BasicBlock.h:282
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
self_iterator getIterator()
Definition: ilist_node.h:81
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Representation for a specific memory location.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Iterator for intrusive lists based on ilist_node.
void applyMergedLocation(const DILocation *LocA, const DILocation *LocB)
Merge 2 debug locations and apply it to the Instruction.
Definition: DebugInfo.cpp:689
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
FunctionPass * createMergedLoadStoreMotionPass()
createMergedLoadStoreMotionPass - The public interface to this file.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:114
This pass performs merges of loads and stores on both sides of a.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:321
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:189
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:174
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
bool isSimple() const
Definition: Instructions.h:401
#define LLVM_DEBUG(X)
Definition: Debug.h:122
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry &)
Value * getPointerOperand()
Definition: Instructions.h:412
const BasicBlock * getParent() const
Definition: Instruction.h:66