blob: 6b0d0202d9bb9a3e4ed9638338d62cb380494f84 [file] [log] [blame]
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +00001//===- MergedLoadStoreMotion.cpp - merge and hoist/sink load/stores -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +00006//
7//===----------------------------------------------------------------------===//
8//
9//! \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010//! This pass performs merges of loads and stores on both sides of a
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000011// 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
Denis Bakhvalov58f172f2019-09-05 17:00:32 +000017// find a matching load/store on the other side. New tail/footer block may be
18// insterted if the tail/footer block has more predecessors (not only the two
19// predecessors that are forming the diamond). It hoists / sinks when it thinks
20// it safe to do so. This optimization helps with eg. hiding load latencies,
21// triggering if-conversion, and reducing static code size.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000022//
Daniel Berlin390dfde2017-01-24 19:55:36 +000023// NOTE: This code no longer performs load hoisting, it is subsumed by GVNHoist.
24//
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000025//===----------------------------------------------------------------------===//
26//
27//
28// Example:
29// Diamond shaped code before merge:
30//
31// header:
32// br %cond, label %if.then, label %if.else
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000033// + +
34// + +
35// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000036// if.then: if.else:
37// %lt = load %addr_l %le = load %addr_l
38// <use %lt> <use %le>
39// <...> <...>
40// store %st, %addr_s store %se, %addr_s
41// br label %if.end br label %if.end
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000042// + +
43// + +
44// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000045// if.end ("footer"):
46// <...>
47//
48// Diamond shaped code after merge:
49//
50// header:
51// %l = load %addr_l
52// br %cond, label %if.then, label %if.else
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000053// + +
54// + +
55// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000056// if.then: if.else:
57// <use %l> <use %l>
58// <...> <...>
59// br label %if.end br label %if.end
Gerolf Hoflehnerea96a3d2014-08-07 23:19:55 +000060// + +
61// + +
62// + +
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000063// if.end ("footer"):
64// %s.sink = phi [%st, if.then], [%se, if.else]
65// <...>
66// store %s.sink, %addr_s
67// <...>
68//
69//
70//===----------------------- TODO -----------------------------------------===//
71//
72// 1) Generalize to regions other than diamonds
73// 2) Be more aggressive merging memory operations
74// Note that both changes require register pressure control
75//
76//===----------------------------------------------------------------------===//
77
Davide Italianob49aa5c2016-06-17 19:10:09 +000078#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000079#include "llvm/ADT/Statistic.h"
80#include "llvm/Analysis/AliasAnalysis.h"
81#include "llvm/Analysis/CFG.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000082#include "llvm/Analysis/GlobalsModRef.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000083#include "llvm/Analysis/Loads.h"
Eli Friedman9f8031c2016-06-12 02:11:20 +000084#include "llvm/Analysis/ValueTracking.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000085#include "llvm/IR/Metadata.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080086#include "llvm/InitializePasses.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000087#include "llvm/Support/Debug.h"
Benjamin Kramerb85d3752015-03-23 18:45:56 +000088#include "llvm/Support/raw_ostream.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000089#include "llvm/Transforms/Scalar.h"
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000090#include "llvm/Transforms/Utils/BasicBlockUtils.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
Benjamin Kramer4d098922016-07-10 11:28:51 +000096namespace {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +000097//===----------------------------------------------------------------------===//
98// MergedLoadStoreMotion Pass
99//===----------------------------------------------------------------------===//
Davide Italianob49aa5c2016-06-17 19:10:09 +0000100class MergedLoadStoreMotion {
Davide Italianob49aa5c2016-06-17 19:10:09 +0000101 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
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000109 const bool SplitFooterBB;
Davide Italiano41315f72016-06-16 17:40:53 +0000110public:
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000111 MergedLoadStoreMotion(bool SplitFooterBB) : SplitFooterBB(SplitFooterBB) {}
Bjorn Steinbrink983d6c32018-02-23 10:41:57 +0000112 bool run(Function &F, AliasAnalysis &AA);
Davide Italiano41315f72016-06-16 17:40:53 +0000113
114private:
Davide Italiano41315f72016-06-16 17:40:53 +0000115 BasicBlock *getDiamondTail(BasicBlock *BB);
116 bool isDiamondHead(BasicBlock *BB);
Davide Italiano41315f72016-06-16 17:40:53 +0000117 // Routines for sinking stores
118 StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI);
119 PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1);
120 bool isStoreSinkBarrierInRange(const Instruction &Start,
121 const Instruction &End, MemoryLocation Loc);
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000122 bool canSinkStoresAndGEPs(StoreInst *S0, StoreInst *S1) const;
123 void sinkStoresAndGEPs(BasicBlock *BB, StoreInst *SinkCand,
124 StoreInst *ElseInst);
Davide Italiano41315f72016-06-16 17:40:53 +0000125 bool mergeStores(BasicBlock *BB);
Davide Italiano41315f72016-06-16 17:40:53 +0000126};
Benjamin Kramer4d098922016-07-10 11:28:51 +0000127} // end anonymous namespace
Davide Italiano41315f72016-06-16 17:40:53 +0000128
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000129///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000130/// Return tail block of a diamond.
Davide Italiano41315f72016-06-16 17:40:53 +0000131///
132BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
133 assert(isDiamondHead(BB) && "Basic block is not head of a diamond");
134 return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor();
135}
136
137///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000138/// True when BB is the head of a diamond (hammock)
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000139///
Davide Italiano41315f72016-06-16 17:40:53 +0000140bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000141 if (!BB)
142 return false;
David Majnemer8cce3332016-05-26 05:43:12 +0000143 auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
144 if (!BI || !BI->isConditional())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000145 return false;
146
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000147 BasicBlock *Succ0 = BI->getSuccessor(0);
148 BasicBlock *Succ1 = BI->getSuccessor(1);
149
David Majnemer8cce3332016-05-26 05:43:12 +0000150 if (!Succ0->getSinglePredecessor())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000151 return false;
David Majnemer8cce3332016-05-26 05:43:12 +0000152 if (!Succ1->getSinglePredecessor())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000153 return false;
154
David Majnemer8cce3332016-05-26 05:43:12 +0000155 BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
156 BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000157 // Ignore triangles.
David Majnemer8cce3332016-05-26 05:43:12 +0000158 if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000159 return false;
160 return true;
161}
162
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000163
164///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000165/// True when instruction is a sink barrier for a store
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000166/// located in Loc
167///
168/// Whenever an instruction could possibly read or modify the
169/// value being stored or protect against the store from
170/// happening it is considered a sink barrier.
171///
Davide Italiano41315f72016-06-16 17:40:53 +0000172bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start,
173 const Instruction &End,
174 MemoryLocation Loc) {
David Majnemer47451252016-05-26 07:11:09 +0000175 for (const Instruction &Inst :
176 make_range(Start.getIterator(), End.getIterator()))
177 if (Inst.mayThrow())
178 return true;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000179 return AA->canInstructionRangeModRef(Start, End, Loc, ModRefInfo::ModRef);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000180}
181
182///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000183/// Check if \p BB contains a store to the same address as \p SI
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000184///
185/// \return The store in \p when it is safe to sink. Otherwise return Null.
186///
Davide Italiano41315f72016-06-16 17:40:53 +0000187StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
188 StoreInst *Store0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000189 LLVM_DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n");
Elena Demikhovskyef035bb2015-02-17 13:10:05 +0000190 BasicBlock *BB0 = Store0->getParent();
David Majnemerd7708772016-06-24 04:05:21 +0000191 for (Instruction &Inst : reverse(*BB1)) {
192 auto *Store1 = dyn_cast<StoreInst>(&Inst);
David Majnemer47451252016-05-26 07:11:09 +0000193 if (!Store1)
194 continue;
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000195
Chandler Carruthac80dc72015-06-17 07:18:54 +0000196 MemoryLocation Loc0 = MemoryLocation::get(Store0);
197 MemoryLocation Loc1 = MemoryLocation::get(Store1);
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000198 if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) &&
Davide Italiano41315f72016-06-16 17:40:53 +0000199 !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) &&
200 !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) {
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000201 return Store1;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000202 }
203 }
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000204 return nullptr;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000205}
206
207///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000208/// Create a PHI node in BB for the operands of S0 and S1
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000209///
Davide Italiano41315f72016-06-16 17:40:53 +0000210PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
211 StoreInst *S1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000212 // Create a phi if the values mismatch.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000213 Value *Opd1 = S0->getValueOperand();
214 Value *Opd2 = S1->getValueOperand();
David Majnemer8cce3332016-05-26 05:43:12 +0000215 if (Opd1 == Opd2)
216 return nullptr;
217
218 auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink",
219 &BB->front());
Jordan Rupprecht80e7e862018-11-02 18:25:41 +0000220 NewPN->applyMergedLocation(S0->getDebugLoc(), S1->getDebugLoc());
David Majnemer8cce3332016-05-26 05:43:12 +0000221 NewPN->addIncoming(Opd1, S0->getParent());
222 NewPN->addIncoming(Opd2, S1->getParent());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000223 return NewPN;
224}
225
226///
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000227/// Check if 2 stores can be sunk together with corresponding GEPs
228///
229bool MergedLoadStoreMotion::canSinkStoresAndGEPs(StoreInst *S0,
230 StoreInst *S1) const {
231 auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
232 auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
233 return A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
234 (A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
235 (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0);
236}
237
238///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000239/// Merge two stores to same address and sink into \p BB
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000240///
241/// Also sinks GEP instruction computing the store address
242///
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000243void MergedLoadStoreMotion::sinkStoresAndGEPs(BasicBlock *BB, StoreInst *S0,
244 StoreInst *S1) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000245 // Only one definition?
David Majnemer8cce3332016-05-26 05:43:12 +0000246 auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand());
247 auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand());
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000248 LLVM_DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump();
249 dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n";
250 dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n");
251 // Hoist the instruction.
252 BasicBlock::iterator InsertPt = BB->getFirstInsertionPt();
253 // Intersect optional metadata.
254 S0->andIRFlags(S1);
255 S0->dropUnknownNonDebugMetadata();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000256
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000257 // Create the new store to be inserted at the join point.
258 StoreInst *SNew = cast<StoreInst>(S0->clone());
259 Instruction *ANew = A0->clone();
260 SNew->insertBefore(&*InsertPt);
261 ANew->insertBefore(SNew);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000262
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000263 assert(S0->getParent() == A0->getParent());
264 assert(S1->getParent() == A1->getParent());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000265
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000266 // New PHI operand? Use it.
267 if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
268 SNew->setOperand(0, NewPN);
269 S0->eraseFromParent();
270 S1->eraseFromParent();
271 A0->replaceAllUsesWith(ANew);
272 A0->eraseFromParent();
273 A1->replaceAllUsesWith(ANew);
274 A1->eraseFromParent();
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000275}
276
277///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000278/// True when two stores are equivalent and can sink into the footer
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000279///
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000280/// Starting from a diamond head block, iterate over the instructions in one
281/// successor block and try to match a store in the second successor.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000282///
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000283bool MergedLoadStoreMotion::mergeStores(BasicBlock *HeadBB) {
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000284
285 bool MergedStores = false;
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000286 BasicBlock *TailBB = getDiamondTail(HeadBB);
287 BasicBlock *SinkBB = TailBB;
288 assert(SinkBB && "Footer of a diamond cannot be empty");
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000289
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000290 succ_iterator SI = succ_begin(HeadBB);
291 assert(SI != succ_end(HeadBB) && "Diamond head cannot have zero successors");
292 BasicBlock *Pred0 = *SI;
293 ++SI;
294 assert(SI != succ_end(HeadBB) && "Diamond head cannot have single successor");
295 BasicBlock *Pred1 = *SI;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000296 // tail block of a diamond/hammock?
297 if (Pred0 == Pred1)
298 return false; // No.
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000299 // bail out early if we can not merge into the footer BB
300 if (!SplitFooterBB && TailBB->hasNPredecessorsOrMore(3))
301 return false;
302 // #Instructions in Pred1 for Compile Time Control
Vedant Kumar5a0872c2018-05-16 23:20:42 +0000303 auto InstsNoDbg = Pred1->instructionsWithoutDebug();
304 int Size1 = std::distance(InstsNoDbg.begin(), InstsNoDbg.end());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000305 int NStores = 0;
306
307 for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
308 RBI != RBE;) {
309
310 Instruction *I = &*RBI;
311 ++RBI;
Elena Demikhovskya5599bf2014-12-15 14:09:53 +0000312
David Majnemer8cce3332016-05-26 05:43:12 +0000313 // Don't sink non-simple (atomic, volatile) stores.
314 auto *S0 = dyn_cast<StoreInst>(I);
315 if (!S0 || !S0->isSimple())
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000316 continue;
317
318 ++NStores;
319 if (NStores * Size1 >= MagicCompileTimeControl)
320 break;
Davide Italiano41315f72016-06-16 17:40:53 +0000321 if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) {
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000322 if (!canSinkStoresAndGEPs(S0, S1))
323 // Don't attempt to sink below stores that had to stick around
324 // But after removal of a store and some of its feeding
325 // instruction search again from the beginning since the iterator
326 // is likely stale at this point.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000327 break;
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000328
329 if (SinkBB == TailBB && TailBB->hasNPredecessorsOrMore(3)) {
330 // We have more than 2 predecessors. Insert a new block
331 // postdominating 2 predecessors we're going to sink from.
332 SinkBB = SplitBlockPredecessors(TailBB, {Pred0, Pred1}, ".sink.split");
333 if (!SinkBB)
334 break;
335 }
336
337 MergedStores = true;
338 sinkStoresAndGEPs(SinkBB, S0, S1);
David Majnemer8cce3332016-05-26 05:43:12 +0000339 RBI = Pred0->rbegin();
340 RBE = Pred0->rend();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000341 LLVM_DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump());
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000342 }
343 }
344 return MergedStores;
345}
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000346
Bjorn Steinbrink983d6c32018-02-23 10:41:57 +0000347bool MergedLoadStoreMotion::run(Function &F, AliasAnalysis &AA) {
Davide Italianob49aa5c2016-06-17 19:10:09 +0000348 this->AA = &AA;
Davide Italiano41315f72016-06-16 17:40:53 +0000349
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000350 bool Changed = false;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000351 LLVM_DEBUG(dbgs() << "Instruction Merger\n");
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000352
353 // Merge unconditional branches, allowing PRE to catch more
354 // optimization opportunities.
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000355 // This loop doesn't care about newly inserted/split blocks
356 // since they never will be diamond heads.
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000357 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000358 BasicBlock *BB = &*FI++;
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000359
360 // Hoist equivalent loads and sink stores
361 // outside diamonds when possible
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000362 if (isDiamondHead(BB)) {
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000363 Changed |= mergeStores(BB);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000364 }
365 }
366 return Changed;
367}
Davide Italianob49aa5c2016-06-17 19:10:09 +0000368
369namespace {
370class MergedLoadStoreMotionLegacyPass : public FunctionPass {
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000371 const bool SplitFooterBB;
Davide Italianob49aa5c2016-06-17 19:10:09 +0000372public:
373 static char ID; // Pass identification, replacement for typeid
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000374 MergedLoadStoreMotionLegacyPass(bool SplitFooterBB = false)
375 : FunctionPass(ID), SplitFooterBB(SplitFooterBB) {
Davide Italianob49aa5c2016-06-17 19:10:09 +0000376 initializeMergedLoadStoreMotionLegacyPassPass(
377 *PassRegistry::getPassRegistry());
378 }
379
380 ///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000381 /// Run the transformation for each function
Davide Italianob49aa5c2016-06-17 19:10:09 +0000382 ///
383 bool runOnFunction(Function &F) override {
384 if (skipFunction(F))
385 return false;
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000386 MergedLoadStoreMotion Impl(SplitFooterBB);
Bjorn Steinbrink983d6c32018-02-23 10:41:57 +0000387 return Impl.run(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
Davide Italianob49aa5c2016-06-17 19:10:09 +0000388 }
389
390private:
Davide Italianob49aa5c2016-06-17 19:10:09 +0000391 void getAnalysisUsage(AnalysisUsage &AU) const override {
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000392 if (!SplitFooterBB)
393 AU.setPreservesCFG();
Davide Italianob49aa5c2016-06-17 19:10:09 +0000394 AU.addRequired<AAResultsWrapperPass>();
395 AU.addPreserved<GlobalsAAWrapperPass>();
Davide Italianob49aa5c2016-06-17 19:10:09 +0000396 }
397};
398
399char MergedLoadStoreMotionLegacyPass::ID = 0;
400} // anonymous namespace
401
402///
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000403/// createMergedLoadStoreMotionPass - The public interface to this file.
Davide Italianob49aa5c2016-06-17 19:10:09 +0000404///
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000405FunctionPass *llvm::createMergedLoadStoreMotionPass(bool SplitFooterBB) {
406 return new MergedLoadStoreMotionLegacyPass(SplitFooterBB);
Davide Italianob49aa5c2016-06-17 19:10:09 +0000407}
408
409INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion",
410 "MergedLoadStoreMotion", false, false)
Davide Italianob49aa5c2016-06-17 19:10:09 +0000411INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
412INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion",
413 "MergedLoadStoreMotion", false, false)
414
415PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +0000416MergedLoadStoreMotionPass::run(Function &F, FunctionAnalysisManager &AM) {
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000417 MergedLoadStoreMotion Impl(Options.SplitFooterBB);
Davide Italianob49aa5c2016-06-17 19:10:09 +0000418 auto &AA = AM.getResult<AAManager>(F);
Bjorn Steinbrink983d6c32018-02-23 10:41:57 +0000419 if (!Impl.run(F, AA))
Davide Italianob49aa5c2016-06-17 19:10:09 +0000420 return PreservedAnalyses::all();
421
Davide Italianob49aa5c2016-06-17 19:10:09 +0000422 PreservedAnalyses PA;
Denis Bakhvalov58f172f2019-09-05 17:00:32 +0000423 if (!Options.SplitFooterBB)
424 PA.preserveSet<CFGAnalyses>();
Davide Italianob49aa5c2016-06-17 19:10:09 +0000425 PA.preserve<GlobalsAA>();
Davide Italianob49aa5c2016-06-17 19:10:09 +0000426 return PA;
427}