blob: 17f5d9b9f0ad0cc2d83ec3639fa6445e1daeec8a [file] [log] [blame]
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001//===- MemorySSA.cpp - Memory SSA Builder ---------------------------------===//
George Burgess IVe1100f52016-02-02 22:46:49 +00002//
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
George Burgess IVe1100f52016-02-02 22:46:49 +00006//
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00007//===----------------------------------------------------------------------===//
George Burgess IVe1100f52016-02-02 22:46:49 +00008//
9// This file implements the MemorySSA class.
10//
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000011//===----------------------------------------------------------------------===//
12
Daniel Berlin554dcd82017-04-11 20:06:36 +000013#include "llvm/Analysis/MemorySSA.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000014#include "llvm/ADT/DenseMap.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000015#include "llvm/ADT/DenseMapInfo.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000016#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/DepthFirstIterator.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000018#include "llvm/ADT/Hashing.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/Optional.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000021#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000023#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/iterator.h"
25#include "llvm/ADT/iterator_range.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000026#include "llvm/Analysis/AliasAnalysis.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000027#include "llvm/Analysis/IteratedDominanceFrontier.h"
28#include "llvm/Analysis/MemoryLocation.h"
Nico Weber432a3882018-04-30 14:59:11 +000029#include "llvm/Config/llvm-config.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000030#include "llvm/IR/AssemblyAnnotationWriter.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000031#include "llvm/IR/BasicBlock.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000032#include "llvm/IR/Dominators.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000033#include "llvm/IR/Function.h"
34#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Instructions.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000036#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000037#include "llvm/IR/Intrinsics.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000038#include "llvm/IR/LLVMContext.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000039#include "llvm/IR/PassManager.h"
40#include "llvm/IR/Use.h"
41#include "llvm/Pass.h"
42#include "llvm/Support/AtomicOrdering.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/CommandLine.h"
45#include "llvm/Support/Compiler.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000046#include "llvm/Support/Debug.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000047#include "llvm/Support/ErrorHandling.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000048#include "llvm/Support/FormattedStream.h"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000049#include "llvm/Support/raw_ostream.h"
George Burgess IVe1100f52016-02-02 22:46:49 +000050#include <algorithm>
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000051#include <cassert>
52#include <iterator>
53#include <memory>
54#include <utility>
55
56using namespace llvm;
George Burgess IVe1100f52016-02-02 22:46:49 +000057
58#define DEBUG_TYPE "memoryssa"
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000059
Geoff Berryefb0dd12016-06-14 21:19:40 +000060INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
Geoff Berryb96d3b22016-06-01 21:30:40 +000061 true)
George Burgess IVe1100f52016-02-02 22:46:49 +000062INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
63INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Geoff Berryefb0dd12016-06-14 21:19:40 +000064INITIALIZE_PASS_END(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false,
65 true)
George Burgess IVe1100f52016-02-02 22:46:49 +000066
Chad Rosier232e29e2016-07-06 21:20:47 +000067INITIALIZE_PASS_BEGIN(MemorySSAPrinterLegacyPass, "print-memoryssa",
68 "Memory SSA Printer", false, false)
69INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
70INITIALIZE_PASS_END(MemorySSAPrinterLegacyPass, "print-memoryssa",
71 "Memory SSA Printer", false, false)
72
Daniel Berlinc43aa5a2016-08-02 16:24:03 +000073static cl::opt<unsigned> MaxCheckLimit(
74 "memssa-check-limit", cl::Hidden, cl::init(100),
75 cl::desc("The maximum number of stores/phis MemorySSA"
76 "will consider trying to walk past (default = 100)"));
77
Alina Sbirleacc2e8cc2018-08-15 17:34:55 +000078// Always verify MemorySSA if expensive checking is enabled.
79#ifdef EXPENSIVE_CHECKS
80bool llvm::VerifyMemorySSA = true;
81#else
82bool llvm::VerifyMemorySSA = false;
83#endif
Alina Sbirlea4fd1f262019-04-23 20:59:44 +000084/// Enables memory ssa as a dependency for loop passes in legacy pass manager.
85cl::opt<bool> llvm::EnableMSSALoopDependency(
86 "enable-mssa-loop-dependency", cl::Hidden, cl::init(false),
87 cl::desc("Enable MemorySSA dependency for loop pass manager"));
88
Alina Sbirleacc2e8cc2018-08-15 17:34:55 +000089static cl::opt<bool, true>
90 VerifyMemorySSAX("verify-memoryssa", cl::location(VerifyMemorySSA),
91 cl::Hidden, cl::desc("Enable verification of MemorySSA."));
Chad Rosier232e29e2016-07-06 21:20:47 +000092
George Burgess IVe1100f52016-02-02 22:46:49 +000093namespace llvm {
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000094
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000095/// An assembly annotator class to print Memory SSA information in
George Burgess IVe1100f52016-02-02 22:46:49 +000096/// comments.
97class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter {
98 friend class MemorySSA;
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +000099
George Burgess IVe1100f52016-02-02 22:46:49 +0000100 const MemorySSA *MSSA;
101
102public:
103 MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {}
104
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000105 void emitBasicBlockStartAnnot(const BasicBlock *BB,
106 formatted_raw_ostream &OS) override {
George Burgess IVe1100f52016-02-02 22:46:49 +0000107 if (MemoryAccess *MA = MSSA->getMemoryAccess(BB))
108 OS << "; " << *MA << "\n";
109 }
110
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000111 void emitInstructionAnnot(const Instruction *I,
112 formatted_raw_ostream &OS) override {
George Burgess IVe1100f52016-02-02 22:46:49 +0000113 if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
114 OS << "; " << *MA << "\n";
115 }
116};
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000117
118} // end namespace llvm
George Burgess IVfd1f2f82016-06-24 21:02:12 +0000119
George Burgess IV5f308972016-07-19 01:29:15 +0000120namespace {
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000121
Daniel Berlindff31de2016-08-02 21:57:52 +0000122/// Our current alias analysis API differentiates heavily between calls and
123/// non-calls, and functions called on one usually assert on the other.
124/// This class encapsulates the distinction to simplify other code that wants
125/// "Memory affecting instructions and related data" to use as a key.
126/// For example, this class is used as a densemap key in the use optimizer.
127class MemoryLocOrCall {
128public:
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000129 bool IsCall = false;
130
Daniel Berlindff31de2016-08-02 21:57:52 +0000131 MemoryLocOrCall(MemoryUseOrDef *MUD)
132 : MemoryLocOrCall(MUD->getMemoryInst()) {}
Sebastian Pop5068d7a2016-10-13 03:23:33 +0000133 MemoryLocOrCall(const MemoryUseOrDef *MUD)
134 : MemoryLocOrCall(MUD->getMemoryInst()) {}
Daniel Berlindff31de2016-08-02 21:57:52 +0000135
136 MemoryLocOrCall(Instruction *Inst) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000137 if (auto *C = dyn_cast<CallBase>(Inst)) {
Daniel Berlindff31de2016-08-02 21:57:52 +0000138 IsCall = true;
Chandler Carruth363ac682019-01-07 05:42:51 +0000139 Call = C;
Daniel Berlindff31de2016-08-02 21:57:52 +0000140 } else {
141 IsCall = false;
142 // There is no such thing as a memorylocation for a fence inst, and it is
143 // unique in that regard.
144 if (!isa<FenceInst>(Inst))
145 Loc = MemoryLocation::get(Inst);
146 }
147 }
148
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000149 explicit MemoryLocOrCall(const MemoryLocation &Loc) : Loc(Loc) {}
Daniel Berlindff31de2016-08-02 21:57:52 +0000150
Chandler Carruth363ac682019-01-07 05:42:51 +0000151 const CallBase *getCall() const {
Daniel Berlindff31de2016-08-02 21:57:52 +0000152 assert(IsCall);
Chandler Carruth363ac682019-01-07 05:42:51 +0000153 return Call;
Daniel Berlindff31de2016-08-02 21:57:52 +0000154 }
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000155
Daniel Berlindff31de2016-08-02 21:57:52 +0000156 MemoryLocation getLoc() const {
157 assert(!IsCall);
158 return Loc;
159 }
160
161 bool operator==(const MemoryLocOrCall &Other) const {
162 if (IsCall != Other.IsCall)
163 return false;
164
George Burgess IV3588fd42018-03-29 00:54:39 +0000165 if (!IsCall)
166 return Loc == Other.Loc;
167
Chandler Carruth363ac682019-01-07 05:42:51 +0000168 if (Call->getCalledValue() != Other.Call->getCalledValue())
George Burgess IV3588fd42018-03-29 00:54:39 +0000169 return false;
170
Chandler Carruth363ac682019-01-07 05:42:51 +0000171 return Call->arg_size() == Other.Call->arg_size() &&
172 std::equal(Call->arg_begin(), Call->arg_end(),
173 Other.Call->arg_begin());
Daniel Berlindff31de2016-08-02 21:57:52 +0000174 }
175
176private:
Daniel Berlinf5361132016-10-22 04:15:41 +0000177 union {
Chandler Carruth363ac682019-01-07 05:42:51 +0000178 const CallBase *Call;
Daniel Berlind602e042017-01-25 20:56:19 +0000179 MemoryLocation Loc;
Daniel Berlinf5361132016-10-22 04:15:41 +0000180 };
Daniel Berlindff31de2016-08-02 21:57:52 +0000181};
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000182
183} // end anonymous namespace
Daniel Berlindff31de2016-08-02 21:57:52 +0000184
185namespace llvm {
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000186
Daniel Berlindff31de2016-08-02 21:57:52 +0000187template <> struct DenseMapInfo<MemoryLocOrCall> {
188 static inline MemoryLocOrCall getEmptyKey() {
189 return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getEmptyKey());
190 }
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000191
Daniel Berlindff31de2016-08-02 21:57:52 +0000192 static inline MemoryLocOrCall getTombstoneKey() {
193 return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getTombstoneKey());
194 }
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000195
Daniel Berlindff31de2016-08-02 21:57:52 +0000196 static unsigned getHashValue(const MemoryLocOrCall &MLOC) {
George Burgess IV3588fd42018-03-29 00:54:39 +0000197 if (!MLOC.IsCall)
198 return hash_combine(
199 MLOC.IsCall,
200 DenseMapInfo<MemoryLocation>::getHashValue(MLOC.getLoc()));
201
202 hash_code hash =
203 hash_combine(MLOC.IsCall, DenseMapInfo<const Value *>::getHashValue(
Chandler Carruth363ac682019-01-07 05:42:51 +0000204 MLOC.getCall()->getCalledValue()));
George Burgess IV3588fd42018-03-29 00:54:39 +0000205
Chandler Carruth363ac682019-01-07 05:42:51 +0000206 for (const Value *Arg : MLOC.getCall()->args())
George Burgess IV3588fd42018-03-29 00:54:39 +0000207 hash = hash_combine(hash, DenseMapInfo<const Value *>::getHashValue(Arg));
208 return hash;
Daniel Berlindff31de2016-08-02 21:57:52 +0000209 }
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000210
Daniel Berlindff31de2016-08-02 21:57:52 +0000211 static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS) {
212 return LHS == RHS;
213 }
214};
Daniel Berlindf101192016-08-03 00:01:46 +0000215
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000216} // end namespace llvm
217
George Burgess IV82e355c2016-08-03 19:39:54 +0000218/// This does one-way checks to see if Use could theoretically be hoisted above
219/// MayClobber. This will not check the other way around.
220///
221/// This assumes that, for the purposes of MemorySSA, Use comes directly after
222/// MayClobber, with no potentially clobbering operations in between them.
223/// (Where potentially clobbering ops are memory barriers, aliased stores, etc.)
Alina Sbirleaca741a82017-12-22 19:54:03 +0000224static bool areLoadsReorderable(const LoadInst *Use,
225 const LoadInst *MayClobber) {
George Burgess IV82e355c2016-08-03 19:39:54 +0000226 bool VolatileUse = Use->isVolatile();
227 bool VolatileClobber = MayClobber->isVolatile();
228 // Volatile operations may never be reordered with other volatile operations.
229 if (VolatileUse && VolatileClobber)
Alina Sbirleaca741a82017-12-22 19:54:03 +0000230 return false;
231 // Otherwise, volatile doesn't matter here. From the language reference:
232 // 'optimizers may change the order of volatile operations relative to
233 // non-volatile operations.'"
George Burgess IV82e355c2016-08-03 19:39:54 +0000234
235 // If a load is seq_cst, it cannot be moved above other loads. If its ordering
236 // is weaker, it can be moved above other loads. We just need to be sure that
237 // MayClobber isn't an acquire load, because loads can't be moved above
238 // acquire loads.
239 //
240 // Note that this explicitly *does* allow the free reordering of monotonic (or
241 // weaker) loads of the same address.
242 bool SeqCstUse = Use->getOrdering() == AtomicOrdering::SequentiallyConsistent;
243 bool MayClobberIsAcquire = isAtLeastOrStrongerThan(MayClobber->getOrdering(),
244 AtomicOrdering::Acquire);
Alina Sbirleaca741a82017-12-22 19:54:03 +0000245 return !(SeqCstUse || MayClobberIsAcquire);
George Burgess IV82e355c2016-08-03 19:39:54 +0000246}
247
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000248namespace {
249
250struct ClobberAlias {
251 bool IsClobber;
252 Optional<AliasResult> AR;
253};
254
255} // end anonymous namespace
256
257// Return a pair of {IsClobber (bool), AR (AliasResult)}. It relies on AR being
258// ignored if IsClobber = false.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000259template <typename AliasAnalysisType>
260static ClobberAlias
261instructionClobbersQuery(const MemoryDef *MD, const MemoryLocation &UseLoc,
262 const Instruction *UseInst, AliasAnalysisType &AA) {
Daniel Berlinc43aa5a2016-08-02 16:24:03 +0000263 Instruction *DefInst = MD->getMemoryInst();
264 assert(DefInst && "Defining instruction not actually an instruction");
Chandler Carruth363ac682019-01-07 05:42:51 +0000265 const auto *UseCall = dyn_cast<CallBase>(UseInst);
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000266 Optional<AliasResult> AR;
George Burgess IV5f308972016-07-19 01:29:15 +0000267
Daniel Berlindf101192016-08-03 00:01:46 +0000268 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) {
269 // These intrinsics will show up as affecting memory, but they are just
George Burgess IVff08c802018-08-10 05:14:43 +0000270 // markers, mostly.
271 //
272 // FIXME: We probably don't actually want MemorySSA to model these at all
273 // (including creating MemoryAccesses for them): we just end up inventing
274 // clobbers where they don't really exist at all. Please see D43269 for
275 // context.
Daniel Berlindf101192016-08-03 00:01:46 +0000276 switch (II->getIntrinsicID()) {
277 case Intrinsic::lifetime_start:
Chandler Carruth363ac682019-01-07 05:42:51 +0000278 if (UseCall)
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000279 return {false, NoAlias};
280 AR = AA.alias(MemoryLocation(II->getArgOperand(1)), UseLoc);
George Burgess IVff08c802018-08-10 05:14:43 +0000281 return {AR != NoAlias, AR};
Daniel Berlindf101192016-08-03 00:01:46 +0000282 case Intrinsic::lifetime_end:
283 case Intrinsic::invariant_start:
284 case Intrinsic::invariant_end:
285 case Intrinsic::assume:
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000286 return {false, NoAlias};
Daniel Berlindf101192016-08-03 00:01:46 +0000287 default:
288 break;
289 }
290 }
291
Chandler Carruth363ac682019-01-07 05:42:51 +0000292 if (UseCall) {
293 ModRefInfo I = AA.getModRefInfo(DefInst, UseCall);
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000294 AR = isMustSet(I) ? MustAlias : MayAlias;
295 return {isModOrRefSet(I), AR};
Hans Wennborg70e22d12017-11-21 18:00:01 +0000296 }
George Burgess IV82e355c2016-08-03 19:39:54 +0000297
Alina Sbirleaca741a82017-12-22 19:54:03 +0000298 if (auto *DefLoad = dyn_cast<LoadInst>(DefInst))
299 if (auto *UseLoad = dyn_cast<LoadInst>(UseInst))
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000300 return {!areLoadsReorderable(UseLoad, DefLoad), MayAlias};
George Burgess IV82e355c2016-08-03 19:39:54 +0000301
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000302 ModRefInfo I = AA.getModRefInfo(DefInst, UseLoc);
303 AR = isMustSet(I) ? MustAlias : MayAlias;
304 return {isModSet(I), AR};
Daniel Berlindff31de2016-08-02 21:57:52 +0000305}
306
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000307template <typename AliasAnalysisType>
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000308static ClobberAlias instructionClobbersQuery(MemoryDef *MD,
309 const MemoryUseOrDef *MU,
310 const MemoryLocOrCall &UseMLOC,
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000311 AliasAnalysisType &AA) {
Sebastian Pop5068d7a2016-10-13 03:23:33 +0000312 // FIXME: This is a temporary hack to allow a single instructionClobbersQuery
313 // to exist while MemoryLocOrCall is pushed through places.
314 if (UseMLOC.IsCall)
315 return instructionClobbersQuery(MD, MemoryLocation(), MU->getMemoryInst(),
316 AA);
317 return instructionClobbersQuery(MD, UseMLOC.getLoc(), MU->getMemoryInst(),
318 AA);
319}
320
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000321// Return true when MD may alias MU, return false otherwise.
Daniel Berlindcb004f2017-03-02 23:06:46 +0000322bool MemorySSAUtil::defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU,
323 AliasAnalysis &AA) {
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000324 return instructionClobbersQuery(MD, MU, MemoryLocOrCall(MU), AA).IsClobber;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000325}
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000326
327namespace {
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000328
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000329struct UpwardsMemoryQuery {
330 // True if our original query started off as a call
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000331 bool IsCall = false;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000332 // The pointer location we started the query with. This will be empty if
333 // IsCall is true.
334 MemoryLocation StartingLoc;
335 // This is the instruction we were querying about.
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000336 const Instruction *Inst = nullptr;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000337 // The MemoryAccess we actually got called with, used to test local domination
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000338 const MemoryAccess *OriginalAccess = nullptr;
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000339 Optional<AliasResult> AR = MayAlias;
Alina Sbirleaf7230202019-01-07 18:40:27 +0000340 bool SkipSelfAccess = false;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000341
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000342 UpwardsMemoryQuery() = default;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000343
344 UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access)
Chandler Carruth363ac682019-01-07 05:42:51 +0000345 : IsCall(isa<CallBase>(Inst)), Inst(Inst), OriginalAccess(Access) {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000346 if (!IsCall)
347 StartingLoc = MemoryLocation::get(Inst);
348 }
349};
350
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000351} // end anonymous namespace
352
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000353static bool lifetimeEndsAt(MemoryDef *MD, const MemoryLocation &Loc,
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000354 BatchAAResults &AA) {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000355 Instruction *Inst = MD->getMemoryInst();
356 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
357 switch (II->getIntrinsicID()) {
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000358 case Intrinsic::lifetime_end:
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000359 return AA.alias(MemoryLocation(II->getArgOperand(1)), Loc) == MustAlias;
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000360 default:
361 return false;
362 }
363 }
364 return false;
365}
366
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000367template <typename AliasAnalysisType>
368static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysisType &AA,
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000369 const Instruction *I) {
370 // If the memory can't be changed, then loads of the memory can't be
371 // clobbered.
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000372 return isa<LoadInst>(I) && (I->getMetadata(LLVMContext::MD_invariant_load) ||
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000373 AA.pointsToConstantMemory(MemoryLocation(
374 cast<LoadInst>(I)->getPointerOperand())));
Sebastian Pop5ba9f242016-10-13 01:39:10 +0000375}
376
George Burgess IV5f308972016-07-19 01:29:15 +0000377/// Verifies that `Start` is clobbered by `ClobberAt`, and that nothing
378/// inbetween `Start` and `ClobberAt` can clobbers `Start`.
379///
380/// This is meant to be as simple and self-contained as possible. Because it
381/// uses no cache, etc., it can be relatively expensive.
382///
383/// \param Start The MemoryAccess that we want to walk from.
384/// \param ClobberAt A clobber for Start.
385/// \param StartLoc The MemoryLocation for Start.
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000386/// \param MSSA The MemorySSA instance that Start and ClobberAt belong to.
George Burgess IV5f308972016-07-19 01:29:15 +0000387/// \param Query The UpwardsMemoryQuery we used for our search.
388/// \param AA The AliasAnalysis we used for our search.
Alina Sbirlea65f385d2018-09-07 23:51:41 +0000389/// \param AllowImpreciseClobber Always false, unless we do relaxed verify.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000390
391template <typename AliasAnalysisType>
Alina Sbirlead77edc02019-02-11 19:51:21 +0000392LLVM_ATTRIBUTE_UNUSED static void
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000393checkClobberSanity(const MemoryAccess *Start, MemoryAccess *ClobberAt,
George Burgess IV5f308972016-07-19 01:29:15 +0000394 const MemoryLocation &StartLoc, const MemorySSA &MSSA,
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000395 const UpwardsMemoryQuery &Query, AliasAnalysisType &AA,
Alina Sbirlea65f385d2018-09-07 23:51:41 +0000396 bool AllowImpreciseClobber = false) {
George Burgess IV5f308972016-07-19 01:29:15 +0000397 assert(MSSA.dominates(ClobberAt, Start) && "Clobber doesn't dominate start?");
398
399 if (MSSA.isLiveOnEntryDef(Start)) {
400 assert(MSSA.isLiveOnEntryDef(ClobberAt) &&
401 "liveOnEntry must clobber itself");
402 return;
403 }
404
George Burgess IV5f308972016-07-19 01:29:15 +0000405 bool FoundClobber = false;
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000406 DenseSet<ConstMemoryAccessPair> VisitedPhis;
407 SmallVector<ConstMemoryAccessPair, 8> Worklist;
George Burgess IV5f308972016-07-19 01:29:15 +0000408 Worklist.emplace_back(Start, StartLoc);
409 // Walk all paths from Start to ClobberAt, while looking for clobbers. If one
410 // is found, complain.
411 while (!Worklist.empty()) {
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000412 auto MAP = Worklist.pop_back_val();
George Burgess IV5f308972016-07-19 01:29:15 +0000413 // All we care about is that nothing from Start to ClobberAt clobbers Start.
414 // We learn nothing from revisiting nodes.
415 if (!VisitedPhis.insert(MAP).second)
416 continue;
417
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000418 for (const auto *MA : def_chain(MAP.first)) {
George Burgess IV5f308972016-07-19 01:29:15 +0000419 if (MA == ClobberAt) {
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000420 if (const auto *MD = dyn_cast<MemoryDef>(MA)) {
George Burgess IV5f308972016-07-19 01:29:15 +0000421 // instructionClobbersQuery isn't essentially free, so don't use `|=`,
422 // since it won't let us short-circuit.
423 //
424 // Also, note that this can't be hoisted out of the `Worklist` loop,
425 // since MD may only act as a clobber for 1 of N MemoryLocations.
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000426 FoundClobber = FoundClobber || MSSA.isLiveOnEntryDef(MD);
427 if (!FoundClobber) {
428 ClobberAlias CA =
429 instructionClobbersQuery(MD, MAP.second, Query.Inst, AA);
430 if (CA.IsClobber) {
431 FoundClobber = true;
432 // Not used: CA.AR;
433 }
434 }
George Burgess IV5f308972016-07-19 01:29:15 +0000435 }
436 break;
437 }
438
439 // We should never hit liveOnEntry, unless it's the clobber.
440 assert(!MSSA.isLiveOnEntryDef(MA) && "Hit liveOnEntry before clobber?");
441
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000442 if (const auto *MD = dyn_cast<MemoryDef>(MA)) {
Alina Sbirlea5bce4d52018-08-29 22:38:51 +0000443 // If Start is a Def, skip self.
444 if (MD == Start)
445 continue;
446
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000447 assert(!instructionClobbersQuery(MD, MAP.second, Query.Inst, AA)
448 .IsClobber &&
George Burgess IV5f308972016-07-19 01:29:15 +0000449 "Found clobber before reaching ClobberAt!");
450 continue;
451 }
452
Alina Sbirlea5bce4d52018-08-29 22:38:51 +0000453 if (const auto *MU = dyn_cast<MemoryUse>(MA)) {
Alina Sbirlea6edcc9e2018-08-29 23:20:29 +0000454 (void)MU;
Alina Sbirlea5bce4d52018-08-29 22:38:51 +0000455 assert (MU == Start &&
456 "Can only find use in def chain if Start is a use");
457 continue;
458 }
459
George Burgess IV5f308972016-07-19 01:29:15 +0000460 assert(isa<MemoryPhi>(MA));
Alina Sbirleaf5403d82018-08-29 18:26:04 +0000461 Worklist.append(
462 upward_defs_begin({const_cast<MemoryAccess *>(MA), MAP.second}),
463 upward_defs_end());
George Burgess IV5f308972016-07-19 01:29:15 +0000464 }
465 }
466
Alina Sbirlea65f385d2018-09-07 23:51:41 +0000467 // If the verify is done following an optimization, it's possible that
468 // ClobberAt was a conservative clobbering, that we can now infer is not a
469 // true clobbering access. Don't fail the verify if that's the case.
470 // We do have accesses that claim they're optimized, but could be optimized
471 // further. Updating all these can be expensive, so allow it for now (FIXME).
472 if (AllowImpreciseClobber)
473 return;
474
George Burgess IV5f308972016-07-19 01:29:15 +0000475 // If ClobberAt is a MemoryPhi, we can assume something above it acted as a
476 // clobber. Otherwise, `ClobberAt` should've acted as a clobber at some point.
477 assert((isa<MemoryPhi>(ClobberAt) || FoundClobber) &&
478 "ClobberAt never acted as a clobber");
479}
480
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000481namespace {
482
George Burgess IV5f308972016-07-19 01:29:15 +0000483/// Our algorithm for walking (and trying to optimize) clobbers, all wrapped up
484/// in one class.
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000485template <class AliasAnalysisType> class ClobberWalker {
George Burgess IV5f308972016-07-19 01:29:15 +0000486 /// Save a few bytes by using unsigned instead of size_t.
487 using ListIndex = unsigned;
488
489 /// Represents a span of contiguous MemoryDefs, potentially ending in a
490 /// MemoryPhi.
491 struct DefPath {
492 MemoryLocation Loc;
493 // Note that, because we always walk in reverse, Last will always dominate
494 // First. Also note that First and Last are inclusive.
495 MemoryAccess *First;
496 MemoryAccess *Last;
George Burgess IV5f308972016-07-19 01:29:15 +0000497 Optional<ListIndex> Previous;
498
499 DefPath(const MemoryLocation &Loc, MemoryAccess *First, MemoryAccess *Last,
500 Optional<ListIndex> Previous)
501 : Loc(Loc), First(First), Last(Last), Previous(Previous) {}
502
503 DefPath(const MemoryLocation &Loc, MemoryAccess *Init,
504 Optional<ListIndex> Previous)
505 : DefPath(Loc, Init, Init, Previous) {}
506 };
507
508 const MemorySSA &MSSA;
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000509 AliasAnalysisType &AA;
George Burgess IV5f308972016-07-19 01:29:15 +0000510 DominatorTree &DT;
George Burgess IV5f308972016-07-19 01:29:15 +0000511 UpwardsMemoryQuery *Query;
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000512 unsigned *UpwardWalkLimit;
George Burgess IV5f308972016-07-19 01:29:15 +0000513
514 // Phi optimization bookkeeping
515 SmallVector<DefPath, 32> Paths;
516 DenseSet<ConstMemoryAccessPair> VisitedPhis;
George Burgess IV5f308972016-07-19 01:29:15 +0000517
George Burgess IV5f308972016-07-19 01:29:15 +0000518 /// Find the nearest def or phi that `From` can legally be optimized to.
Daniel Berlind0420312017-04-01 09:01:12 +0000519 const MemoryAccess *getWalkTarget(const MemoryPhi *From) const {
George Burgess IV5f308972016-07-19 01:29:15 +0000520 assert(From->getNumOperands() && "Phi with no operands?");
521
522 BasicBlock *BB = From->getBlock();
George Burgess IV5f308972016-07-19 01:29:15 +0000523 MemoryAccess *Result = MSSA.getLiveOnEntryDef();
524 DomTreeNode *Node = DT.getNode(BB);
525 while ((Node = Node->getIDom())) {
Daniel Berlin7500c562017-04-01 08:59:45 +0000526 auto *Defs = MSSA.getBlockDefs(Node->getBlock());
527 if (Defs)
Daniel Berlind0420312017-04-01 09:01:12 +0000528 return &*Defs->rbegin();
George Burgess IV5f308972016-07-19 01:29:15 +0000529 }
George Burgess IV5f308972016-07-19 01:29:15 +0000530 return Result;
531 }
532
533 /// Result of calling walkToPhiOrClobber.
534 struct UpwardsWalkResult {
535 /// The "Result" of the walk. Either a clobber, the last thing we walked, or
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000536 /// both. Include alias info when clobber found.
George Burgess IV5f308972016-07-19 01:29:15 +0000537 MemoryAccess *Result;
538 bool IsKnownClobber;
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000539 Optional<AliasResult> AR;
George Burgess IV5f308972016-07-19 01:29:15 +0000540 };
541
542 /// Walk to the next Phi or Clobber in the def chain starting at Desc.Last.
543 /// This will update Desc.Last as it walks. It will (optionally) also stop at
544 /// StopAt.
545 ///
546 /// This does not test for whether StopAt is a clobber
Daniel Berlind0420312017-04-01 09:01:12 +0000547 UpwardsWalkResult
Alina Sbirleaf7230202019-01-07 18:40:27 +0000548 walkToPhiOrClobber(DefPath &Desc, const MemoryAccess *StopAt = nullptr,
549 const MemoryAccess *SkipStopAt = nullptr) const {
George Burgess IV5f308972016-07-19 01:29:15 +0000550 assert(!isa<MemoryUse>(Desc.Last) && "Uses don't exist in my world");
Alina Sbirleac8d6e042019-03-29 22:55:59 +0000551 assert(UpwardWalkLimit && "Need a valid walk limit");
Alina Sbirlea57769382019-04-12 18:48:46 +0000552 bool LimitAlreadyReached = false;
553 // (*UpwardWalkLimit) may be 0 here, due to the loop in tryOptimizePhi. Set
554 // it to 1. This will not do any alias() calls. It either returns in the
555 // first iteration in the loop below, or is set back to 0 if all def chains
556 // are free of MemoryDefs.
557 if (!*UpwardWalkLimit) {
558 *UpwardWalkLimit = 1;
559 LimitAlreadyReached = true;
560 }
George Burgess IV5f308972016-07-19 01:29:15 +0000561
562 for (MemoryAccess *Current : def_chain(Desc.Last)) {
563 Desc.Last = Current;
Alina Sbirleaf7230202019-01-07 18:40:27 +0000564 if (Current == StopAt || Current == SkipStopAt)
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000565 return {Current, false, MayAlias};
George Burgess IV5f308972016-07-19 01:29:15 +0000566
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000567 if (auto *MD = dyn_cast<MemoryDef>(Current)) {
568 if (MSSA.isLiveOnEntryDef(MD))
569 return {MD, true, MustAlias};
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000570
571 if (!--*UpwardWalkLimit)
572 return {Current, true, MayAlias};
573
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000574 ClobberAlias CA =
575 instructionClobbersQuery(MD, Desc.Loc, Query->Inst, AA);
576 if (CA.IsClobber)
577 return {MD, true, CA.AR};
578 }
George Burgess IV5f308972016-07-19 01:29:15 +0000579 }
580
Alina Sbirlea57769382019-04-12 18:48:46 +0000581 if (LimitAlreadyReached)
582 *UpwardWalkLimit = 0;
583
George Burgess IV5f308972016-07-19 01:29:15 +0000584 assert(isa<MemoryPhi>(Desc.Last) &&
585 "Ended at a non-clobber that's not a phi?");
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000586 return {Desc.Last, false, MayAlias};
George Burgess IV5f308972016-07-19 01:29:15 +0000587 }
588
589 void addSearches(MemoryPhi *Phi, SmallVectorImpl<ListIndex> &PausedSearches,
590 ListIndex PriorNode) {
591 auto UpwardDefs = make_range(upward_defs_begin({Phi, Paths[PriorNode].Loc}),
592 upward_defs_end());
593 for (const MemoryAccessPair &P : UpwardDefs) {
594 PausedSearches.push_back(Paths.size());
595 Paths.emplace_back(P.second, P.first, PriorNode);
596 }
597 }
598
599 /// Represents a search that terminated after finding a clobber. This clobber
600 /// may or may not be present in the path of defs from LastNode..SearchStart,
601 /// since it may have been retrieved from cache.
602 struct TerminatedPath {
603 MemoryAccess *Clobber;
604 ListIndex LastNode;
605 };
606
607 /// Get an access that keeps us from optimizing to the given phi.
608 ///
609 /// PausedSearches is an array of indices into the Paths array. Its incoming
610 /// value is the indices of searches that stopped at the last phi optimization
611 /// target. It's left in an unspecified state.
612 ///
613 /// If this returns None, NewPaused is a vector of searches that terminated
614 /// at StopWhere. Otherwise, NewPaused is left in an unspecified state.
George Burgess IV14633b52016-08-03 01:22:19 +0000615 Optional<TerminatedPath>
Daniel Berlind0420312017-04-01 09:01:12 +0000616 getBlockingAccess(const MemoryAccess *StopWhere,
George Burgess IV5f308972016-07-19 01:29:15 +0000617 SmallVectorImpl<ListIndex> &PausedSearches,
618 SmallVectorImpl<ListIndex> &NewPaused,
619 SmallVectorImpl<TerminatedPath> &Terminated) {
620 assert(!PausedSearches.empty() && "No searches to continue?");
621
622 // BFS vs DFS really doesn't make a difference here, so just do a DFS with
623 // PausedSearches as our stack.
624 while (!PausedSearches.empty()) {
625 ListIndex PathIndex = PausedSearches.pop_back_val();
626 DefPath &Node = Paths[PathIndex];
627
628 // If we've already visited this path with this MemoryLocation, we don't
629 // need to do so again.
630 //
631 // NOTE: That we just drop these paths on the ground makes caching
632 // behavior sporadic. e.g. given a diamond:
633 // A
634 // B C
635 // D
636 //
637 // ...If we walk D, B, A, C, we'll only cache the result of phi
638 // optimization for A, B, and D; C will be skipped because it dies here.
639 // This arguably isn't the worst thing ever, since:
640 // - We generally query things in a top-down order, so if we got below D
641 // without needing cache entries for {C, MemLoc}, then chances are
642 // that those cache entries would end up ultimately unused.
643 // - We still cache things for A, so C only needs to walk up a bit.
644 // If this behavior becomes problematic, we can fix without a ton of extra
645 // work.
646 if (!VisitedPhis.insert({Node.Last, Node.Loc}).second)
647 continue;
648
Alina Sbirleaf7230202019-01-07 18:40:27 +0000649 const MemoryAccess *SkipStopWhere = nullptr;
650 if (Query->SkipSelfAccess && Node.Loc == Query->StartingLoc) {
651 assert(isa<MemoryDef>(Query->OriginalAccess));
652 SkipStopWhere = Query->OriginalAccess;
653 }
654
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000655 UpwardsWalkResult Res = walkToPhiOrClobber(Node,
656 /*StopAt=*/StopWhere,
Alina Sbirleaf7230202019-01-07 18:40:27 +0000657 /*SkipStopAt=*/SkipStopWhere);
George Burgess IV5f308972016-07-19 01:29:15 +0000658 if (Res.IsKnownClobber) {
Alina Sbirleaf7230202019-01-07 18:40:27 +0000659 assert(Res.Result != StopWhere && Res.Result != SkipStopWhere);
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000660
George Burgess IV5f308972016-07-19 01:29:15 +0000661 // If this wasn't a cache hit, we hit a clobber when walking. That's a
662 // failure.
George Burgess IV14633b52016-08-03 01:22:19 +0000663 TerminatedPath Term{Res.Result, PathIndex};
Daniel Berlind7a7ae02017-04-05 19:01:58 +0000664 if (!MSSA.dominates(Res.Result, StopWhere))
George Burgess IV14633b52016-08-03 01:22:19 +0000665 return Term;
George Burgess IV5f308972016-07-19 01:29:15 +0000666
667 // Otherwise, it's a valid thing to potentially optimize to.
George Burgess IV14633b52016-08-03 01:22:19 +0000668 Terminated.push_back(Term);
George Burgess IV5f308972016-07-19 01:29:15 +0000669 continue;
670 }
671
Alina Sbirleaf7230202019-01-07 18:40:27 +0000672 if (Res.Result == StopWhere || Res.Result == SkipStopWhere) {
George Burgess IV5f308972016-07-19 01:29:15 +0000673 // We've hit our target. Save this path off for if we want to continue
Alina Sbirleaf7230202019-01-07 18:40:27 +0000674 // walking. If we are in the mode of skipping the OriginalAccess, and
675 // we've reached back to the OriginalAccess, do not save path, we've
676 // just looped back to self.
677 if (Res.Result != SkipStopWhere)
678 NewPaused.push_back(PathIndex);
George Burgess IV5f308972016-07-19 01:29:15 +0000679 continue;
680 }
681
682 assert(!MSSA.isLiveOnEntryDef(Res.Result) && "liveOnEntry is a clobber");
683 addSearches(cast<MemoryPhi>(Res.Result), PausedSearches, PathIndex);
684 }
685
686 return None;
687 }
688
689 template <typename T, typename Walker>
690 struct generic_def_path_iterator
691 : public iterator_facade_base<generic_def_path_iterator<T, Walker>,
692 std::forward_iterator_tag, T *> {
Hans Wennborg5519cb22019-03-25 09:27:42 +0000693 generic_def_path_iterator() {}
George Burgess IV5f308972016-07-19 01:29:15 +0000694 generic_def_path_iterator(Walker *W, ListIndex N) : W(W), N(N) {}
695
696 T &operator*() const { return curNode(); }
697
698 generic_def_path_iterator &operator++() {
699 N = curNode().Previous;
700 return *this;
701 }
702
703 bool operator==(const generic_def_path_iterator &O) const {
704 if (N.hasValue() != O.N.hasValue())
705 return false;
706 return !N.hasValue() || *N == *O.N;
707 }
708
709 private:
710 T &curNode() const { return W->Paths[*N]; }
711
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000712 Walker *W = nullptr;
713 Optional<ListIndex> N = None;
George Burgess IV5f308972016-07-19 01:29:15 +0000714 };
715
716 using def_path_iterator = generic_def_path_iterator<DefPath, ClobberWalker>;
717 using const_def_path_iterator =
718 generic_def_path_iterator<const DefPath, const ClobberWalker>;
719
720 iterator_range<def_path_iterator> def_path(ListIndex From) {
721 return make_range(def_path_iterator(this, From), def_path_iterator());
722 }
723
724 iterator_range<const_def_path_iterator> const_def_path(ListIndex From) const {
725 return make_range(const_def_path_iterator(this, From),
726 const_def_path_iterator());
727 }
728
729 struct OptznResult {
730 /// The path that contains our result.
731 TerminatedPath PrimaryClobber;
732 /// The paths that we can legally cache back from, but that aren't
733 /// necessarily the result of the Phi optimization.
734 SmallVector<TerminatedPath, 4> OtherClobbers;
735 };
736
737 ListIndex defPathIndex(const DefPath &N) const {
738 // The assert looks nicer if we don't need to do &N
739 const DefPath *NP = &N;
740 assert(!Paths.empty() && NP >= &Paths.front() && NP <= &Paths.back() &&
741 "Out of bounds DefPath!");
742 return NP - &Paths.front();
743 }
744
745 /// Try to optimize a phi as best as we can. Returns a SmallVector of Paths
746 /// that act as legal clobbers. Note that this won't return *all* clobbers.
747 ///
748 /// Phi optimization algorithm tl;dr:
749 /// - Find the earliest def/phi, A, we can optimize to
750 /// - Find if all paths from the starting memory access ultimately reach A
751 /// - If not, optimization isn't possible.
752 /// - Otherwise, walk from A to another clobber or phi, A'.
753 /// - If A' is a def, we're done.
754 /// - If A' is a phi, try to optimize it.
755 ///
756 /// A path is a series of {MemoryAccess, MemoryLocation} pairs. A path
757 /// terminates when a MemoryAccess that clobbers said MemoryLocation is found.
758 OptznResult tryOptimizePhi(MemoryPhi *Phi, MemoryAccess *Start,
759 const MemoryLocation &Loc) {
760 assert(Paths.empty() && VisitedPhis.empty() &&
761 "Reset the optimization state.");
762
763 Paths.emplace_back(Loc, Start, Phi, None);
764 // Stores how many "valid" optimization nodes we had prior to calling
765 // addSearches/getBlockingAccess. Necessary for caching if we had a blocker.
766 auto PriorPathsSize = Paths.size();
767
768 SmallVector<ListIndex, 16> PausedSearches;
769 SmallVector<ListIndex, 8> NewPaused;
770 SmallVector<TerminatedPath, 4> TerminatedPaths;
771
772 addSearches(Phi, PausedSearches, 0);
773
774 // Moves the TerminatedPath with the "most dominated" Clobber to the end of
775 // Paths.
776 auto MoveDominatedPathToEnd = [&](SmallVectorImpl<TerminatedPath> &Paths) {
777 assert(!Paths.empty() && "Need a path to move");
George Burgess IV5f308972016-07-19 01:29:15 +0000778 auto Dom = Paths.begin();
779 for (auto I = std::next(Dom), E = Paths.end(); I != E; ++I)
780 if (!MSSA.dominates(I->Clobber, Dom->Clobber))
781 Dom = I;
782 auto Last = Paths.end() - 1;
783 if (Last != Dom)
784 std::iter_swap(Last, Dom);
785 };
786
787 MemoryPhi *Current = Phi;
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000788 while (true) {
George Burgess IV5f308972016-07-19 01:29:15 +0000789 assert(!MSSA.isLiveOnEntryDef(Current) &&
790 "liveOnEntry wasn't treated as a clobber?");
791
Daniel Berlind0420312017-04-01 09:01:12 +0000792 const auto *Target = getWalkTarget(Current);
George Burgess IV5f308972016-07-19 01:29:15 +0000793 // If a TerminatedPath doesn't dominate Target, then it wasn't a legal
794 // optimization for the prior phi.
795 assert(all_of(TerminatedPaths, [&](const TerminatedPath &P) {
796 return MSSA.dominates(P.Clobber, Target);
797 }));
798
799 // FIXME: This is broken, because the Blocker may be reported to be
800 // liveOnEntry, and we'll happily wait for that to disappear (read: never)
George Burgess IV7f414b92016-08-22 23:40:01 +0000801 // For the moment, this is fine, since we do nothing with blocker info.
George Burgess IV14633b52016-08-03 01:22:19 +0000802 if (Optional<TerminatedPath> Blocker = getBlockingAccess(
George Burgess IV5f308972016-07-19 01:29:15 +0000803 Target, PausedSearches, NewPaused, TerminatedPaths)) {
George Burgess IV5f308972016-07-19 01:29:15 +0000804
805 // Find the node we started at. We can't search based on N->Last, since
806 // we may have gone around a loop with a different MemoryLocation.
George Burgess IV14633b52016-08-03 01:22:19 +0000807 auto Iter = find_if(def_path(Blocker->LastNode), [&](const DefPath &N) {
George Burgess IV5f308972016-07-19 01:29:15 +0000808 return defPathIndex(N) < PriorPathsSize;
809 });
810 assert(Iter != def_path_iterator());
811
812 DefPath &CurNode = *Iter;
813 assert(CurNode.Last == Current);
George Burgess IV5f308972016-07-19 01:29:15 +0000814
815 // Two things:
816 // A. We can't reliably cache all of NewPaused back. Consider a case
817 // where we have two paths in NewPaused; one of which can't optimize
818 // above this phi, whereas the other can. If we cache the second path
819 // back, we'll end up with suboptimal cache entries. We can handle
820 // cases like this a bit better when we either try to find all
821 // clobbers that block phi optimization, or when our cache starts
822 // supporting unfinished searches.
823 // B. We can't reliably cache TerminatedPaths back here without doing
824 // extra checks; consider a case like:
825 // T
826 // / \
827 // D C
828 // \ /
829 // S
830 // Where T is our target, C is a node with a clobber on it, D is a
831 // diamond (with a clobber *only* on the left or right node, N), and
832 // S is our start. Say we walk to D, through the node opposite N
833 // (read: ignoring the clobber), and see a cache entry in the top
834 // node of D. That cache entry gets put into TerminatedPaths. We then
835 // walk up to C (N is later in our worklist), find the clobber, and
836 // quit. If we append TerminatedPaths to OtherClobbers, we'll cache
837 // the bottom part of D to the cached clobber, ignoring the clobber
838 // in N. Again, this problem goes away if we start tracking all
839 // blockers for a given phi optimization.
840 TerminatedPath Result{CurNode.Last, defPathIndex(CurNode)};
841 return {Result, {}};
842 }
843
844 // If there's nothing left to search, then all paths led to valid clobbers
845 // that we got from our cache; pick the nearest to the start, and allow
846 // the rest to be cached back.
847 if (NewPaused.empty()) {
848 MoveDominatedPathToEnd(TerminatedPaths);
849 TerminatedPath Result = TerminatedPaths.pop_back_val();
850 return {Result, std::move(TerminatedPaths)};
851 }
852
853 MemoryAccess *DefChainEnd = nullptr;
854 SmallVector<TerminatedPath, 4> Clobbers;
855 for (ListIndex Paused : NewPaused) {
856 UpwardsWalkResult WR = walkToPhiOrClobber(Paths[Paused]);
857 if (WR.IsKnownClobber)
858 Clobbers.push_back({WR.Result, Paused});
859 else
860 // Micro-opt: If we hit the end of the chain, save it.
861 DefChainEnd = WR.Result;
862 }
863
864 if (!TerminatedPaths.empty()) {
865 // If we couldn't find the dominating phi/liveOnEntry in the above loop,
866 // do it now.
867 if (!DefChainEnd)
Daniel Berlind0420312017-04-01 09:01:12 +0000868 for (auto *MA : def_chain(const_cast<MemoryAccess *>(Target)))
George Burgess IV5f308972016-07-19 01:29:15 +0000869 DefChainEnd = MA;
870
871 // If any of the terminated paths don't dominate the phi we'll try to
872 // optimize, we need to figure out what they are and quit.
873 const BasicBlock *ChainBB = DefChainEnd->getBlock();
874 for (const TerminatedPath &TP : TerminatedPaths) {
875 // Because we know that DefChainEnd is as "high" as we can go, we
876 // don't need local dominance checks; BB dominance is sufficient.
877 if (DT.dominates(ChainBB, TP.Clobber->getBlock()))
878 Clobbers.push_back(TP);
879 }
880 }
881
882 // If we have clobbers in the def chain, find the one closest to Current
883 // and quit.
884 if (!Clobbers.empty()) {
885 MoveDominatedPathToEnd(Clobbers);
886 TerminatedPath Result = Clobbers.pop_back_val();
887 return {Result, std::move(Clobbers)};
888 }
889
890 assert(all_of(NewPaused,
891 [&](ListIndex I) { return Paths[I].Last == DefChainEnd; }));
892
893 // Because liveOnEntry is a clobber, this must be a phi.
894 auto *DefChainPhi = cast<MemoryPhi>(DefChainEnd);
895
896 PriorPathsSize = Paths.size();
897 PausedSearches.clear();
898 for (ListIndex I : NewPaused)
899 addSearches(DefChainPhi, PausedSearches, I);
900 NewPaused.clear();
901
902 Current = DefChainPhi;
903 }
904 }
905
George Burgess IV5f308972016-07-19 01:29:15 +0000906 void verifyOptResult(const OptznResult &R) const {
907 assert(all_of(R.OtherClobbers, [&](const TerminatedPath &P) {
908 return MSSA.dominates(P.Clobber, R.PrimaryClobber.Clobber);
909 }));
910 }
911
912 void resetPhiOptznState() {
913 Paths.clear();
914 VisitedPhis.clear();
915 }
916
917public:
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000918 ClobberWalker(const MemorySSA &MSSA, AliasAnalysisType &AA, DominatorTree &DT)
Daniel Berlind7a7ae02017-04-05 19:01:58 +0000919 : MSSA(MSSA), AA(AA), DT(DT) {}
George Burgess IV5f308972016-07-19 01:29:15 +0000920
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000921 AliasAnalysisType *getAA() { return &AA; }
George Burgess IV5f308972016-07-19 01:29:15 +0000922 /// Finds the nearest clobber for the given query, optimizing phis if
923 /// possible.
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000924 MemoryAccess *findClobber(MemoryAccess *Start, UpwardsMemoryQuery &Q,
925 unsigned &UpWalkLimit) {
George Burgess IV5f308972016-07-19 01:29:15 +0000926 Query = &Q;
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000927 UpwardWalkLimit = &UpWalkLimit;
928 // Starting limit must be > 0.
929 if (!UpWalkLimit)
930 UpWalkLimit++;
George Burgess IV5f308972016-07-19 01:29:15 +0000931
932 MemoryAccess *Current = Start;
933 // This walker pretends uses don't exist. If we're handed one, silently grab
934 // its def. (This has the nice side-effect of ensuring we never cache uses)
935 if (auto *MU = dyn_cast<MemoryUse>(Start))
936 Current = MU->getDefiningAccess();
937
938 DefPath FirstDesc(Q.StartingLoc, Current, Current, None);
939 // Fast path for the overly-common case (no crazy phi optimization
940 // necessary)
941 UpwardsWalkResult WalkResult = walkToPhiOrClobber(FirstDesc);
George Burgess IV93ea19b2016-07-24 07:03:49 +0000942 MemoryAccess *Result;
George Burgess IV5f308972016-07-19 01:29:15 +0000943 if (WalkResult.IsKnownClobber) {
George Burgess IV93ea19b2016-07-24 07:03:49 +0000944 Result = WalkResult.Result;
Alina Sbirlead90c9f42018-03-08 18:03:14 +0000945 Q.AR = WalkResult.AR;
George Burgess IV93ea19b2016-07-24 07:03:49 +0000946 } else {
947 OptznResult OptRes = tryOptimizePhi(cast<MemoryPhi>(FirstDesc.Last),
948 Current, Q.StartingLoc);
949 verifyOptResult(OptRes);
George Burgess IV93ea19b2016-07-24 07:03:49 +0000950 resetPhiOptznState();
951 Result = OptRes.PrimaryClobber.Clobber;
George Burgess IV5f308972016-07-19 01:29:15 +0000952 }
953
George Burgess IV5f308972016-07-19 01:29:15 +0000954#ifdef EXPENSIVE_CHECKS
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000955 if (!Q.SkipSelfAccess && *UpwardWalkLimit > 0)
Alina Sbirleae41f4b32019-01-10 21:47:15 +0000956 checkClobberSanity(Current, Result, Q.StartingLoc, MSSA, Q, AA);
George Burgess IV5f308972016-07-19 01:29:15 +0000957#endif
George Burgess IV93ea19b2016-07-24 07:03:49 +0000958 return Result;
George Burgess IV5f308972016-07-19 01:29:15 +0000959 }
960};
961
962struct RenamePassData {
963 DomTreeNode *DTN;
964 DomTreeNode::const_iterator ChildIt;
965 MemoryAccess *IncomingVal;
966
967 RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It,
968 MemoryAccess *M)
969 : DTN(D), ChildIt(It), IncomingVal(M) {}
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000970
George Burgess IV5f308972016-07-19 01:29:15 +0000971 void swap(RenamePassData &RHS) {
972 std::swap(DTN, RHS.DTN);
973 std::swap(ChildIt, RHS.ChildIt);
974 std::swap(IncomingVal, RHS.IncomingVal);
975 }
976};
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000977
978} // end anonymous namespace
George Burgess IV5f308972016-07-19 01:29:15 +0000979
980namespace llvm {
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +0000981
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000982template <class AliasAnalysisType> class MemorySSA::ClobberWalkerBase {
983 ClobberWalker<AliasAnalysisType> Walker;
Alina Sbirleabc8aa242019-01-07 19:22:37 +0000984 MemorySSA *MSSA;
985
986public:
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000987 ClobberWalkerBase(MemorySSA *M, AliasAnalysisType *A, DominatorTree *D)
Alina Sbirleabc8aa242019-01-07 19:22:37 +0000988 : Walker(*M, *A, *D), MSSA(M) {}
989
990 MemoryAccess *getClobberingMemoryAccessBase(MemoryAccess *,
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000991 const MemoryLocation &,
992 unsigned &);
993 // Third argument (bool), defines whether the clobber search should skip the
Alina Sbirleabc8aa242019-01-07 19:22:37 +0000994 // original queried access. If true, there will be a follow-up query searching
995 // for a clobber access past "self". Note that the Optimized access is not
996 // updated if a new clobber is found by this SkipSelf search. If this
997 // additional query becomes heavily used we may decide to cache the result.
998 // Walker instantiations will decide how to set the SkipSelf bool.
Alina Sbirleaf085cc52019-03-29 21:56:09 +0000999 MemoryAccess *getClobberingMemoryAccessBase(MemoryAccess *, unsigned &, bool);
Alina Sbirleabc8aa242019-01-07 19:22:37 +00001000};
1001
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001002/// A MemorySSAWalker that does AA walks to disambiguate accesses. It no
George Burgess IV45f263d2018-05-26 02:28:55 +00001003/// longer does caching on its own, but the name has been retained for the
1004/// moment.
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001005template <class AliasAnalysisType>
George Burgess IVfd1f2f82016-06-24 21:02:12 +00001006class MemorySSA::CachingWalker final : public MemorySSAWalker {
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001007 ClobberWalkerBase<AliasAnalysisType> *Walker;
George Burgess IV5f308972016-07-19 01:29:15 +00001008
George Burgess IVfd1f2f82016-06-24 21:02:12 +00001009public:
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001010 CachingWalker(MemorySSA *M, ClobberWalkerBase<AliasAnalysisType> *W)
Alina Sbirleabc8aa242019-01-07 19:22:37 +00001011 : MemorySSAWalker(M), Walker(W) {}
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001012 ~CachingWalker() override = default;
George Burgess IVfd1f2f82016-06-24 21:02:12 +00001013
George Burgess IV400ae402016-07-20 19:51:34 +00001014 using MemorySSAWalker::getClobberingMemoryAccess;
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001015
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001016 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, unsigned &UWL) {
1017 return Walker->getClobberingMemoryAccessBase(MA, UWL, false);
1018 }
1019 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
1020 const MemoryLocation &Loc,
1021 unsigned &UWL) {
1022 return Walker->getClobberingMemoryAccessBase(MA, Loc, UWL);
1023 }
1024
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001025 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA) override {
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001026 unsigned UpwardWalkLimit = MaxCheckLimit;
1027 return getClobberingMemoryAccess(MA, UpwardWalkLimit);
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001028 }
Alina Sbirleabc8aa242019-01-07 19:22:37 +00001029 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001030 const MemoryLocation &Loc) override {
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001031 unsigned UpwardWalkLimit = MaxCheckLimit;
1032 return getClobberingMemoryAccess(MA, Loc, UpwardWalkLimit);
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001033 }
Alina Sbirleabc8aa242019-01-07 19:22:37 +00001034
1035 void invalidateInfo(MemoryAccess *MA) override {
1036 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1037 MUD->resetOptimized();
1038 }
George Burgess IVfd1f2f82016-06-24 21:02:12 +00001039};
George Burgess IVe1100f52016-02-02 22:46:49 +00001040
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001041template <class AliasAnalysisType>
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001042class MemorySSA::SkipSelfWalker final : public MemorySSAWalker {
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001043 ClobberWalkerBase<AliasAnalysisType> *Walker;
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001044
1045public:
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001046 SkipSelfWalker(MemorySSA *M, ClobberWalkerBase<AliasAnalysisType> *W)
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001047 : MemorySSAWalker(M), Walker(W) {}
1048 ~SkipSelfWalker() override = default;
1049
1050 using MemorySSAWalker::getClobberingMemoryAccess;
1051
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001052 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, unsigned &UWL) {
1053 return Walker->getClobberingMemoryAccessBase(MA, UWL, true);
1054 }
1055 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
1056 const MemoryLocation &Loc,
1057 unsigned &UWL) {
1058 return Walker->getClobberingMemoryAccessBase(MA, Loc, UWL);
1059 }
1060
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001061 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA) override {
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001062 unsigned UpwardWalkLimit = MaxCheckLimit;
1063 return getClobberingMemoryAccess(MA, UpwardWalkLimit);
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001064 }
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001065 MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA,
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001066 const MemoryLocation &Loc) override {
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001067 unsigned UpwardWalkLimit = MaxCheckLimit;
1068 return getClobberingMemoryAccess(MA, Loc, UpwardWalkLimit);
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001069 }
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001070
1071 void invalidateInfo(MemoryAccess *MA) override {
1072 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
1073 MUD->resetOptimized();
1074 }
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001075};
1076
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001077} // end namespace llvm
1078
Daniel Berlin78cbd282017-02-20 22:26:03 +00001079void MemorySSA::renameSuccessorPhis(BasicBlock *BB, MemoryAccess *IncomingVal,
1080 bool RenameAllUses) {
George Burgess IVe1100f52016-02-02 22:46:49 +00001081 // Pass through values to our successors
1082 for (const BasicBlock *S : successors(BB)) {
1083 auto It = PerBlockAccesses.find(S);
1084 // Rename the phi nodes in our successor block
1085 if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1086 continue;
Daniel Berlinada263d2016-06-20 20:21:33 +00001087 AccessList *Accesses = It->second.get();
George Burgess IVe1100f52016-02-02 22:46:49 +00001088 auto *Phi = cast<MemoryPhi>(&Accesses->front());
Daniel Berlin78cbd282017-02-20 22:26:03 +00001089 if (RenameAllUses) {
1090 int PhiIndex = Phi->getBasicBlockIndex(BB);
1091 assert(PhiIndex != -1 && "Incomplete phi during partial rename");
1092 Phi->setIncomingValue(PhiIndex, IncomingVal);
1093 } else
1094 Phi->addIncoming(IncomingVal, BB);
George Burgess IVe1100f52016-02-02 22:46:49 +00001095 }
Daniel Berlin78cbd282017-02-20 22:26:03 +00001096}
George Burgess IVe1100f52016-02-02 22:46:49 +00001097
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001098/// Rename a single basic block into MemorySSA form.
Daniel Berlin78cbd282017-02-20 22:26:03 +00001099/// Uses the standard SSA renaming algorithm.
1100/// \returns The new incoming value.
1101MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB, MemoryAccess *IncomingVal,
1102 bool RenameAllUses) {
1103 auto It = PerBlockAccesses.find(BB);
1104 // Skip most processing if the list is empty.
1105 if (It != PerBlockAccesses.end()) {
1106 AccessList *Accesses = It->second.get();
1107 for (MemoryAccess &L : *Accesses) {
1108 if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&L)) {
1109 if (MUD->getDefiningAccess() == nullptr || RenameAllUses)
1110 MUD->setDefiningAccess(IncomingVal);
1111 if (isa<MemoryDef>(&L))
1112 IncomingVal = &L;
1113 } else {
1114 IncomingVal = &L;
1115 }
1116 }
1117 }
George Burgess IVe1100f52016-02-02 22:46:49 +00001118 return IncomingVal;
1119}
1120
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001121/// This is the standard SSA renaming algorithm.
George Burgess IVe1100f52016-02-02 22:46:49 +00001122///
1123/// We walk the dominator tree in preorder, renaming accesses, and then filling
1124/// in phi nodes in our successors.
1125void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal,
Daniel Berlin78cbd282017-02-20 22:26:03 +00001126 SmallPtrSetImpl<BasicBlock *> &Visited,
1127 bool SkipVisited, bool RenameAllUses) {
Alina Sbirlea0363c3b2019-05-02 23:41:58 +00001128 assert(Root && "Trying to rename accesses in an unreachable block");
1129
George Burgess IVe1100f52016-02-02 22:46:49 +00001130 SmallVector<RenamePassData, 32> WorkStack;
Daniel Berlin78cbd282017-02-20 22:26:03 +00001131 // Skip everything if we already renamed this block and we are skipping.
1132 // Note: You can't sink this into the if, because we need it to occur
1133 // regardless of whether we skip blocks or not.
1134 bool AlreadyVisited = !Visited.insert(Root->getBlock()).second;
1135 if (SkipVisited && AlreadyVisited)
1136 return;
1137
1138 IncomingVal = renameBlock(Root->getBlock(), IncomingVal, RenameAllUses);
1139 renameSuccessorPhis(Root->getBlock(), IncomingVal, RenameAllUses);
George Burgess IVe1100f52016-02-02 22:46:49 +00001140 WorkStack.push_back({Root, Root->begin(), IncomingVal});
George Burgess IVe1100f52016-02-02 22:46:49 +00001141
1142 while (!WorkStack.empty()) {
1143 DomTreeNode *Node = WorkStack.back().DTN;
1144 DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt;
1145 IncomingVal = WorkStack.back().IncomingVal;
1146
1147 if (ChildIt == Node->end()) {
1148 WorkStack.pop_back();
1149 } else {
1150 DomTreeNode *Child = *ChildIt;
1151 ++WorkStack.back().ChildIt;
1152 BasicBlock *BB = Child->getBlock();
Daniel Berlin78cbd282017-02-20 22:26:03 +00001153 // Note: You can't sink this into the if, because we need it to occur
1154 // regardless of whether we skip blocks or not.
1155 AlreadyVisited = !Visited.insert(BB).second;
1156 if (SkipVisited && AlreadyVisited) {
1157 // We already visited this during our renaming, which can happen when
1158 // being asked to rename multiple blocks. Figure out the incoming val,
1159 // which is the last def.
1160 // Incoming value can only change if there is a block def, and in that
1161 // case, it's the last block def in the list.
1162 if (auto *BlockDefs = getWritableBlockDefs(BB))
1163 IncomingVal = &*BlockDefs->rbegin();
1164 } else
1165 IncomingVal = renameBlock(BB, IncomingVal, RenameAllUses);
1166 renameSuccessorPhis(BB, IncomingVal, RenameAllUses);
George Burgess IVe1100f52016-02-02 22:46:49 +00001167 WorkStack.push_back({Child, Child->begin(), IncomingVal});
1168 }
1169 }
1170}
1171
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001172/// This handles unreachable block accesses by deleting phi nodes in
George Burgess IVe1100f52016-02-02 22:46:49 +00001173/// unreachable blocks, and marking all other unreachable MemoryAccess's as
1174/// being uses of the live on entry definition.
1175void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) {
1176 assert(!DT->isReachableFromEntry(BB) &&
1177 "Reachable block found while handling unreachable blocks");
1178
Daniel Berlinfc7e6512016-07-06 05:32:05 +00001179 // Make sure phi nodes in our reachable successors end up with a
1180 // LiveOnEntryDef for our incoming edge, even though our block is forward
1181 // unreachable. We could just disconnect these blocks from the CFG fully,
1182 // but we do not right now.
1183 for (const BasicBlock *S : successors(BB)) {
1184 if (!DT->isReachableFromEntry(S))
1185 continue;
1186 auto It = PerBlockAccesses.find(S);
1187 // Rename the phi nodes in our successor block
1188 if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front()))
1189 continue;
1190 AccessList *Accesses = It->second.get();
1191 auto *Phi = cast<MemoryPhi>(&Accesses->front());
1192 Phi->addIncoming(LiveOnEntryDef.get(), BB);
1193 }
1194
George Burgess IVe1100f52016-02-02 22:46:49 +00001195 auto It = PerBlockAccesses.find(BB);
1196 if (It == PerBlockAccesses.end())
1197 return;
1198
1199 auto &Accesses = It->second;
1200 for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) {
1201 auto Next = std::next(AI);
1202 // If we have a phi, just remove it. We are going to replace all
1203 // users with live on entry.
1204 if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI))
1205 UseOrDef->setDefiningAccess(LiveOnEntryDef.get());
1206 else
1207 Accesses->erase(AI);
1208 AI = Next;
1209 }
1210}
1211
Geoff Berryb96d3b22016-06-01 21:30:40 +00001212MemorySSA::MemorySSA(Function &Func, AliasAnalysis *AA, DominatorTree *DT)
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001213 : AA(nullptr), DT(DT), F(Func), LiveOnEntryDef(nullptr), Walker(nullptr),
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001214 SkipWalker(nullptr), NextID(0) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001215 // Build MemorySSA using a batch alias analysis. This reuses the internal
1216 // state that AA collects during an alias()/getModRefInfo() call. This is
1217 // safe because there are no CFG changes while building MemorySSA and can
1218 // significantly reduce the time spent by the compiler in AA, because we will
1219 // make queries about all the instructions in the Function.
1220 BatchAAResults BatchAA(*AA);
1221 buildMemorySSA(BatchAA);
1222 // Intentionally leave AA to nullptr while building so we don't accidently
1223 // use non-batch AliasAnalysis.
1224 this->AA = AA;
1225 // Also create the walker here.
1226 getWalker();
Geoff Berryb96d3b22016-06-01 21:30:40 +00001227}
1228
George Burgess IVe1100f52016-02-02 22:46:49 +00001229MemorySSA::~MemorySSA() {
1230 // Drop all our references
1231 for (const auto &Pair : PerBlockAccesses)
1232 for (MemoryAccess &MA : *Pair.second)
1233 MA.dropAllReferences();
1234}
1235
Daniel Berlin14300262016-06-21 18:39:20 +00001236MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) {
George Burgess IVe1100f52016-02-02 22:46:49 +00001237 auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr));
1238
1239 if (Res.second)
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001240 Res.first->second = llvm::make_unique<AccessList>();
George Burgess IVe1100f52016-02-02 22:46:49 +00001241 return Res.first->second.get();
1242}
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001243
Daniel Berlind602e042017-01-25 20:56:19 +00001244MemorySSA::DefsList *MemorySSA::getOrCreateDefsList(const BasicBlock *BB) {
1245 auto Res = PerBlockDefs.insert(std::make_pair(BB, nullptr));
1246
1247 if (Res.second)
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001248 Res.first->second = llvm::make_unique<DefsList>();
Daniel Berlind602e042017-01-25 20:56:19 +00001249 return Res.first->second.get();
1250}
George Burgess IVe1100f52016-02-02 22:46:49 +00001251
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001252namespace llvm {
1253
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001254/// This class is a batch walker of all MemoryUse's in the program, and points
1255/// their defining access at the thing that actually clobbers them. Because it
1256/// is a batch walker that touches everything, it does not operate like the
1257/// other walkers. This walker is basically performing a top-down SSA renaming
1258/// pass, where the version stack is used as the cache. This enables it to be
1259/// significantly more time and memory efficient than using the regular walker,
1260/// which is walking bottom-up.
1261class MemorySSA::OptimizeUses {
1262public:
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001263 OptimizeUses(MemorySSA *MSSA, CachingWalker<BatchAAResults> *Walker,
1264 BatchAAResults *BAA, DominatorTree *DT)
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001265 : MSSA(MSSA), Walker(Walker), AA(BAA), DT(DT) {}
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001266
1267 void optimizeUses();
1268
1269private:
1270 /// This represents where a given memorylocation is in the stack.
1271 struct MemlocStackInfo {
1272 // This essentially is keeping track of versions of the stack. Whenever
1273 // the stack changes due to pushes or pops, these versions increase.
1274 unsigned long StackEpoch;
1275 unsigned long PopEpoch;
1276 // This is the lower bound of places on the stack to check. It is equal to
1277 // the place the last stack walk ended.
1278 // Note: Correctness depends on this being initialized to 0, which densemap
1279 // does
1280 unsigned long LowerBound;
Daniel Berlin4b4c7222016-08-08 04:44:53 +00001281 const BasicBlock *LowerBoundBlock;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001282 // This is where the last walk for this memory location ended.
1283 unsigned long LastKill;
1284 bool LastKillValid;
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001285 Optional<AliasResult> AR;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001286 };
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001287
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001288 void optimizeUsesInBlock(const BasicBlock *, unsigned long &, unsigned long &,
1289 SmallVectorImpl<MemoryAccess *> &,
1290 DenseMap<MemoryLocOrCall, MemlocStackInfo> &);
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001291
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001292 MemorySSA *MSSA;
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001293 CachingWalker<BatchAAResults> *Walker;
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001294 BatchAAResults *AA;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001295 DominatorTree *DT;
1296};
1297
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001298} // end namespace llvm
1299
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001300/// Optimize the uses in a given block This is basically the SSA renaming
1301/// algorithm, with one caveat: We are able to use a single stack for all
1302/// MemoryUses. This is because the set of *possible* reaching MemoryDefs is
1303/// the same for every MemoryUse. The *actual* clobbering MemoryDef is just
1304/// going to be some position in that stack of possible ones.
1305///
1306/// We track the stack positions that each MemoryLocation needs
1307/// to check, and last ended at. This is because we only want to check the
1308/// things that changed since last time. The same MemoryLocation should
1309/// get clobbered by the same store (getModRefInfo does not use invariantness or
1310/// things like this, and if they start, we can modify MemoryLocOrCall to
1311/// include relevant data)
1312void MemorySSA::OptimizeUses::optimizeUsesInBlock(
1313 const BasicBlock *BB, unsigned long &StackEpoch, unsigned long &PopEpoch,
1314 SmallVectorImpl<MemoryAccess *> &VersionStack,
1315 DenseMap<MemoryLocOrCall, MemlocStackInfo> &LocStackInfo) {
1316
1317 /// If no accesses, nothing to do.
1318 MemorySSA::AccessList *Accesses = MSSA->getWritableBlockAccesses(BB);
1319 if (Accesses == nullptr)
1320 return;
1321
1322 // Pop everything that doesn't dominate the current block off the stack,
1323 // increment the PopEpoch to account for this.
Piotr Padlewskicc5868c12017-02-18 20:34:36 +00001324 while (true) {
1325 assert(
1326 !VersionStack.empty() &&
1327 "Version stack should have liveOnEntry sentinel dominating everything");
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001328 BasicBlock *BackBlock = VersionStack.back()->getBlock();
1329 if (DT->dominates(BackBlock, BB))
1330 break;
1331 while (VersionStack.back()->getBlock() == BackBlock)
1332 VersionStack.pop_back();
1333 ++PopEpoch;
1334 }
Piotr Padlewskicc5868c12017-02-18 20:34:36 +00001335
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001336 for (MemoryAccess &MA : *Accesses) {
1337 auto *MU = dyn_cast<MemoryUse>(&MA);
1338 if (!MU) {
1339 VersionStack.push_back(&MA);
1340 ++StackEpoch;
1341 continue;
1342 }
1343
George Burgess IV024f3d22016-08-03 19:57:02 +00001344 if (isUseTriviallyOptimizableToLiveOnEntry(*AA, MU->getMemoryInst())) {
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001345 MU->setDefiningAccess(MSSA->getLiveOnEntryDef(), true, None);
George Burgess IV024f3d22016-08-03 19:57:02 +00001346 continue;
1347 }
1348
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001349 MemoryLocOrCall UseMLOC(MU);
1350 auto &LocInfo = LocStackInfo[UseMLOC];
Daniel Berlin26fcea92016-08-02 20:02:21 +00001351 // If the pop epoch changed, it means we've removed stuff from top of
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001352 // stack due to changing blocks. We may have to reset the lower bound or
1353 // last kill info.
1354 if (LocInfo.PopEpoch != PopEpoch) {
1355 LocInfo.PopEpoch = PopEpoch;
1356 LocInfo.StackEpoch = StackEpoch;
Daniel Berlin4b4c7222016-08-08 04:44:53 +00001357 // If the lower bound was in something that no longer dominates us, we
1358 // have to reset it.
1359 // We can't simply track stack size, because the stack may have had
1360 // pushes/pops in the meantime.
1361 // XXX: This is non-optimal, but only is slower cases with heavily
1362 // branching dominator trees. To get the optimal number of queries would
1363 // be to make lowerbound and lastkill a per-loc stack, and pop it until
1364 // the top of that stack dominates us. This does not seem worth it ATM.
1365 // A much cheaper optimization would be to always explore the deepest
1366 // branch of the dominator tree first. This will guarantee this resets on
1367 // the smallest set of blocks.
1368 if (LocInfo.LowerBoundBlock && LocInfo.LowerBoundBlock != BB &&
Daniel Berlin1e98c042016-09-26 17:22:54 +00001369 !DT->dominates(LocInfo.LowerBoundBlock, BB)) {
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001370 // Reset the lower bound of things to check.
1371 // TODO: Some day we should be able to reset to last kill, rather than
1372 // 0.
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001373 LocInfo.LowerBound = 0;
Daniel Berlin4b4c7222016-08-08 04:44:53 +00001374 LocInfo.LowerBoundBlock = VersionStack[0]->getBlock();
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001375 LocInfo.LastKillValid = false;
1376 }
1377 } else if (LocInfo.StackEpoch != StackEpoch) {
1378 // If all that has changed is the StackEpoch, we only have to check the
1379 // new things on the stack, because we've checked everything before. In
1380 // this case, the lower bound of things to check remains the same.
1381 LocInfo.PopEpoch = PopEpoch;
1382 LocInfo.StackEpoch = StackEpoch;
1383 }
1384 if (!LocInfo.LastKillValid) {
1385 LocInfo.LastKill = VersionStack.size() - 1;
1386 LocInfo.LastKillValid = true;
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001387 LocInfo.AR = MayAlias;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001388 }
1389
1390 // At this point, we should have corrected last kill and LowerBound to be
1391 // in bounds.
1392 assert(LocInfo.LowerBound < VersionStack.size() &&
1393 "Lower bound out of range");
1394 assert(LocInfo.LastKill < VersionStack.size() &&
1395 "Last kill info out of range");
1396 // In any case, the new upper bound is the top of the stack.
1397 unsigned long UpperBound = VersionStack.size() - 1;
1398
1399 if (UpperBound - LocInfo.LowerBound > MaxCheckLimit) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001400 LLVM_DEBUG(dbgs() << "MemorySSA skipping optimization of " << *MU << " ("
1401 << *(MU->getMemoryInst()) << ")"
1402 << " because there are "
1403 << UpperBound - LocInfo.LowerBound
1404 << " stores to disambiguate\n");
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001405 // Because we did not walk, LastKill is no longer valid, as this may
1406 // have been a kill.
1407 LocInfo.LastKillValid = false;
1408 continue;
1409 }
1410 bool FoundClobberResult = false;
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001411 unsigned UpwardWalkLimit = MaxCheckLimit;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001412 while (UpperBound > LocInfo.LowerBound) {
1413 if (isa<MemoryPhi>(VersionStack[UpperBound])) {
1414 // For phis, use the walker, see where we ended up, go there
Alina Sbirleaf085cc52019-03-29 21:56:09 +00001415 MemoryAccess *Result =
1416 Walker->getClobberingMemoryAccess(MU, UpwardWalkLimit);
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001417 // We are guaranteed to find it or something is wrong
1418 while (VersionStack[UpperBound] != Result) {
1419 assert(UpperBound != 0);
1420 --UpperBound;
1421 }
1422 FoundClobberResult = true;
1423 break;
1424 }
1425
1426 MemoryDef *MD = cast<MemoryDef>(VersionStack[UpperBound]);
Daniel Berlindf101192016-08-03 00:01:46 +00001427 // If the lifetime of the pointer ends at this instruction, it's live on
1428 // entry.
1429 if (!UseMLOC.IsCall && lifetimeEndsAt(MD, UseMLOC.getLoc(), *AA)) {
1430 // Reset UpperBound to liveOnEntryDef's place in the stack
1431 UpperBound = 0;
1432 FoundClobberResult = true;
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001433 LocInfo.AR = MustAlias;
Daniel Berlindf101192016-08-03 00:01:46 +00001434 break;
1435 }
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001436 ClobberAlias CA = instructionClobbersQuery(MD, MU, UseMLOC, *AA);
1437 if (CA.IsClobber) {
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001438 FoundClobberResult = true;
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001439 LocInfo.AR = CA.AR;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001440 break;
1441 }
1442 --UpperBound;
1443 }
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001444
1445 // Note: Phis always have AliasResult AR set to MayAlias ATM.
1446
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001447 // At the end of this loop, UpperBound is either a clobber, or lower bound
1448 // PHI walking may cause it to be < LowerBound, and in fact, < LastKill.
1449 if (FoundClobberResult || UpperBound < LocInfo.LastKill) {
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001450 // We were last killed now by where we got to
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001451 if (MSSA->isLiveOnEntryDef(VersionStack[UpperBound]))
1452 LocInfo.AR = None;
1453 MU->setDefiningAccess(VersionStack[UpperBound], true, LocInfo.AR);
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001454 LocInfo.LastKill = UpperBound;
1455 } else {
1456 // Otherwise, we checked all the new ones, and now we know we can get to
1457 // LastKill.
Alina Sbirlead90c9f42018-03-08 18:03:14 +00001458 MU->setDefiningAccess(VersionStack[LocInfo.LastKill], true, LocInfo.AR);
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001459 }
1460 LocInfo.LowerBound = VersionStack.size() - 1;
Daniel Berlin4b4c7222016-08-08 04:44:53 +00001461 LocInfo.LowerBoundBlock = BB;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001462 }
1463}
1464
1465/// Optimize uses to point to their actual clobbering definitions.
1466void MemorySSA::OptimizeUses::optimizeUses() {
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001467 SmallVector<MemoryAccess *, 16> VersionStack;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001468 DenseMap<MemoryLocOrCall, MemlocStackInfo> LocStackInfo;
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001469 VersionStack.push_back(MSSA->getLiveOnEntryDef());
1470
1471 unsigned long StackEpoch = 1;
1472 unsigned long PopEpoch = 1;
Piotr Padlewskicc5868c12017-02-18 20:34:36 +00001473 // We perform a non-recursive top-down dominator tree walk.
Daniel Berlin7ac3d742016-08-05 22:09:14 +00001474 for (const auto *DomNode : depth_first(DT->getRootNode()))
1475 optimizeUsesInBlock(DomNode->getBlock(), StackEpoch, PopEpoch, VersionStack,
1476 LocStackInfo);
Daniel Berlinc43aa5a2016-08-02 16:24:03 +00001477}
1478
Daniel Berlin3d512a22016-08-22 19:14:30 +00001479void MemorySSA::placePHINodes(
Michael Zolotukhin67cfbaa2018-05-15 18:40:29 +00001480 const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks) {
Daniel Berlin3d512a22016-08-22 19:14:30 +00001481 // Determine where our MemoryPhi's should go
1482 ForwardIDFCalculator IDFs(*DT);
1483 IDFs.setDefiningBlocks(DefiningBlocks);
Daniel Berlin3d512a22016-08-22 19:14:30 +00001484 SmallVector<BasicBlock *, 32> IDFBlocks;
1485 IDFs.calculate(IDFBlocks);
1486
1487 // Now place MemoryPhi nodes.
Daniel Berlind602e042017-01-25 20:56:19 +00001488 for (auto &BB : IDFBlocks)
1489 createMemoryPhi(BB);
Daniel Berlin3d512a22016-08-22 19:14:30 +00001490}
1491
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001492void MemorySSA::buildMemorySSA(BatchAAResults &BAA) {
George Burgess IVe1100f52016-02-02 22:46:49 +00001493 // We create an access to represent "live on entry", for things like
1494 // arguments or users of globals, where the memory they use is defined before
1495 // the beginning of the function. We do not actually insert it into the IR.
1496 // We do not define a live on exit for the immediate uses, and thus our
1497 // semantics do *not* imply that something with no immediate uses can simply
1498 // be removed.
1499 BasicBlock &StartingPoint = F.getEntryBlock();
George Burgess IV612cf212018-02-27 06:43:19 +00001500 LiveOnEntryDef.reset(new MemoryDef(F.getContext(), nullptr, nullptr,
1501 &StartingPoint, NextID++));
George Burgess IVe1100f52016-02-02 22:46:49 +00001502
1503 // We maintain lists of memory accesses per-block, trading memory for time. We
1504 // could just look up the memory access for every possible instruction in the
1505 // stream.
1506 SmallPtrSet<BasicBlock *, 32> DefiningBlocks;
George Burgess IVe1100f52016-02-02 22:46:49 +00001507 // Go through each block, figure out where defs occur, and chain together all
1508 // the accesses.
1509 for (BasicBlock &B : F) {
Daniel Berlin7898ca62016-02-07 01:52:15 +00001510 bool InsertIntoDef = false;
Daniel Berlinada263d2016-06-20 20:21:33 +00001511 AccessList *Accesses = nullptr;
Daniel Berlind602e042017-01-25 20:56:19 +00001512 DefsList *Defs = nullptr;
George Burgess IVe1100f52016-02-02 22:46:49 +00001513 for (Instruction &I : B) {
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001514 MemoryUseOrDef *MUD = createNewAccess(&I, &BAA);
George Burgess IVb42b7622016-03-11 19:34:03 +00001515 if (!MUD)
George Burgess IVe1100f52016-02-02 22:46:49 +00001516 continue;
Daniel Berlin1b51a292016-02-07 01:52:19 +00001517
George Burgess IVe1100f52016-02-02 22:46:49 +00001518 if (!Accesses)
1519 Accesses = getOrCreateAccessList(&B);
George Burgess IVb42b7622016-03-11 19:34:03 +00001520 Accesses->push_back(MUD);
Daniel Berlind602e042017-01-25 20:56:19 +00001521 if (isa<MemoryDef>(MUD)) {
1522 InsertIntoDef = true;
1523 if (!Defs)
1524 Defs = getOrCreateDefsList(&B);
1525 Defs->push_back(*MUD);
1526 }
George Burgess IVe1100f52016-02-02 22:46:49 +00001527 }
Daniel Berlin7898ca62016-02-07 01:52:15 +00001528 if (InsertIntoDef)
1529 DefiningBlocks.insert(&B);
Daniel Berlin1b51a292016-02-07 01:52:19 +00001530 }
Michael Zolotukhin67cfbaa2018-05-15 18:40:29 +00001531 placePHINodes(DefiningBlocks);
George Burgess IVe1100f52016-02-02 22:46:49 +00001532
1533 // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get
1534 // filled in with all blocks.
1535 SmallPtrSet<BasicBlock *, 16> Visited;
1536 renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited);
1537
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001538 ClobberWalkerBase<BatchAAResults> WalkerBase(this, &BAA, DT);
1539 CachingWalker<BatchAAResults> WalkerLocal(this, &WalkerBase);
1540 OptimizeUses(this, &WalkerLocal, &BAA, DT).optimizeUses();
George Burgess IV5f308972016-07-19 01:29:15 +00001541
George Burgess IVe1100f52016-02-02 22:46:49 +00001542 // Mark the uses in unreachable blocks as live on entry, so that they go
1543 // somewhere.
1544 for (auto &BB : F)
1545 if (!Visited.count(&BB))
1546 markUnreachableAsLiveOnEntry(&BB);
Daniel Berlin16ed57c2016-06-27 18:22:27 +00001547}
George Burgess IVe1100f52016-02-02 22:46:49 +00001548
George Burgess IV5f308972016-07-19 01:29:15 +00001549MemorySSAWalker *MemorySSA::getWalker() { return getWalkerImpl(); }
1550
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001551MemorySSA::CachingWalker<AliasAnalysis> *MemorySSA::getWalkerImpl() {
Daniel Berlin16ed57c2016-06-27 18:22:27 +00001552 if (Walker)
1553 return Walker.get();
1554
Alina Sbirleabc8aa242019-01-07 19:22:37 +00001555 if (!WalkerBase)
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001556 WalkerBase =
1557 llvm::make_unique<ClobberWalkerBase<AliasAnalysis>>(this, AA, DT);
Alina Sbirleabc8aa242019-01-07 19:22:37 +00001558
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001559 Walker =
1560 llvm::make_unique<CachingWalker<AliasAnalysis>>(this, WalkerBase.get());
Geoff Berryb96d3b22016-06-01 21:30:40 +00001561 return Walker.get();
George Burgess IVe1100f52016-02-02 22:46:49 +00001562}
1563
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001564MemorySSAWalker *MemorySSA::getSkipSelfWalker() {
1565 if (SkipWalker)
1566 return SkipWalker.get();
1567
1568 if (!WalkerBase)
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001569 WalkerBase =
1570 llvm::make_unique<ClobberWalkerBase<AliasAnalysis>>(this, AA, DT);
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001571
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001572 SkipWalker =
1573 llvm::make_unique<SkipSelfWalker<AliasAnalysis>>(this, WalkerBase.get());
Alina Sbirlea12bbb4f2019-01-07 19:38:47 +00001574 return SkipWalker.get();
1575 }
1576
1577
Daniel Berlind602e042017-01-25 20:56:19 +00001578// This is a helper function used by the creation routines. It places NewAccess
1579// into the access and defs lists for a given basic block, at the given
1580// insertion point.
1581void MemorySSA::insertIntoListsForBlock(MemoryAccess *NewAccess,
1582 const BasicBlock *BB,
1583 InsertionPlace Point) {
1584 auto *Accesses = getOrCreateAccessList(BB);
1585 if (Point == Beginning) {
1586 // If it's a phi node, it goes first, otherwise, it goes after any phi
1587 // nodes.
1588 if (isa<MemoryPhi>(NewAccess)) {
1589 Accesses->push_front(NewAccess);
1590 auto *Defs = getOrCreateDefsList(BB);
1591 Defs->push_front(*NewAccess);
1592 } else {
1593 auto AI = find_if_not(
1594 *Accesses, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1595 Accesses->insert(AI, NewAccess);
1596 if (!isa<MemoryUse>(NewAccess)) {
1597 auto *Defs = getOrCreateDefsList(BB);
1598 auto DI = find_if_not(
1599 *Defs, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); });
1600 Defs->insert(DI, *NewAccess);
1601 }
1602 }
1603 } else {
1604 Accesses->push_back(NewAccess);
1605 if (!isa<MemoryUse>(NewAccess)) {
1606 auto *Defs = getOrCreateDefsList(BB);
1607 Defs->push_back(*NewAccess);
1608 }
1609 }
Daniel Berlin9d8a3352017-01-30 11:35:39 +00001610 BlockNumberingValid.erase(BB);
Daniel Berlind602e042017-01-25 20:56:19 +00001611}
1612
1613void MemorySSA::insertIntoListsBefore(MemoryAccess *What, const BasicBlock *BB,
1614 AccessList::iterator InsertPt) {
1615 auto *Accesses = getWritableBlockAccesses(BB);
1616 bool WasEnd = InsertPt == Accesses->end();
1617 Accesses->insert(AccessList::iterator(InsertPt), What);
1618 if (!isa<MemoryUse>(What)) {
1619 auto *Defs = getOrCreateDefsList(BB);
1620 // If we got asked to insert at the end, we have an easy job, just shove it
1621 // at the end. If we got asked to insert before an existing def, we also get
Zhaoshi Zhenga5531f22018-04-04 21:08:11 +00001622 // an iterator. If we got asked to insert before a use, we have to hunt for
Daniel Berlind602e042017-01-25 20:56:19 +00001623 // the next def.
1624 if (WasEnd) {
1625 Defs->push_back(*What);
1626 } else if (isa<MemoryDef>(InsertPt)) {
1627 Defs->insert(InsertPt->getDefsIterator(), *What);
1628 } else {
1629 while (InsertPt != Accesses->end() && !isa<MemoryDef>(InsertPt))
1630 ++InsertPt;
1631 // Either we found a def, or we are inserting at the end
1632 if (InsertPt == Accesses->end())
1633 Defs->push_back(*What);
1634 else
1635 Defs->insert(InsertPt->getDefsIterator(), *What);
1636 }
1637 }
Daniel Berlin9d8a3352017-01-30 11:35:39 +00001638 BlockNumberingValid.erase(BB);
Daniel Berlind602e042017-01-25 20:56:19 +00001639}
1640
George Burgess IV5676a5d2018-08-22 22:34:38 +00001641void MemorySSA::prepareForMoveTo(MemoryAccess *What, BasicBlock *BB) {
1642 // Keep it in the lookup tables, remove from the lists
1643 removeFromLists(What, false);
1644
1645 // Note that moving should implicitly invalidate the optimized state of a
1646 // MemoryUse (and Phis can't be optimized). However, it doesn't do so for a
1647 // MemoryDef.
1648 if (auto *MD = dyn_cast<MemoryDef>(What))
1649 MD->resetOptimized();
1650 What->setBlock(BB);
1651}
1652
Zhaoshi Zhenga5531f22018-04-04 21:08:11 +00001653// Move What before Where in the IR. The end result is that What will belong to
Daniel Berlin60ead052017-01-28 01:23:13 +00001654// the right lists and have the right Block set, but will not otherwise be
1655// correct. It will not have the right defining access, and if it is a def,
1656// things below it will not properly be updated.
1657void MemorySSA::moveTo(MemoryUseOrDef *What, BasicBlock *BB,
1658 AccessList::iterator Where) {
George Burgess IV5676a5d2018-08-22 22:34:38 +00001659 prepareForMoveTo(What, BB);
Daniel Berlin60ead052017-01-28 01:23:13 +00001660 insertIntoListsBefore(What, BB, Where);
1661}
1662
Alina Sbirlea0f533552018-07-11 22:11:46 +00001663void MemorySSA::moveTo(MemoryAccess *What, BasicBlock *BB,
Daniel Berlin9d8a3352017-01-30 11:35:39 +00001664 InsertionPlace Point) {
Alina Sbirlea0f533552018-07-11 22:11:46 +00001665 if (isa<MemoryPhi>(What)) {
1666 assert(Point == Beginning &&
1667 "Can only move a Phi at the beginning of the block");
1668 // Update lookup table entry
1669 ValueToMemoryAccess.erase(What->getBlock());
1670 bool Inserted = ValueToMemoryAccess.insert({BB, What}).second;
1671 (void)Inserted;
1672 assert(Inserted && "Cannot move a Phi to a block that already has one");
1673 }
1674
George Burgess IV5676a5d2018-08-22 22:34:38 +00001675 prepareForMoveTo(What, BB);
Daniel Berlin9d8a3352017-01-30 11:35:39 +00001676 insertIntoListsForBlock(What, BB, Point);
1677}
1678
Daniel Berlin14300262016-06-21 18:39:20 +00001679MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) {
1680 assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB");
Daniel Berlin14300262016-06-21 18:39:20 +00001681 MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++);
Daniel Berlin9d8a3352017-01-30 11:35:39 +00001682 // Phi's always are placed at the front of the block.
Daniel Berlind602e042017-01-25 20:56:19 +00001683 insertIntoListsForBlock(Phi, BB, Beginning);
Daniel Berlin5130cc82016-07-31 21:08:20 +00001684 ValueToMemoryAccess[BB] = Phi;
Daniel Berlin14300262016-06-21 18:39:20 +00001685 return Phi;
1686}
1687
1688MemoryUseOrDef *MemorySSA::createDefinedAccess(Instruction *I,
Alina Sbirlea79800992018-09-10 20:13:01 +00001689 MemoryAccess *Definition,
1690 const MemoryUseOrDef *Template) {
Daniel Berlin14300262016-06-21 18:39:20 +00001691 assert(!isa<PHINode>(I) && "Cannot create a defined access for a PHI");
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001692 MemoryUseOrDef *NewAccess = createNewAccess(I, AA, Template);
Daniel Berlin14300262016-06-21 18:39:20 +00001693 assert(
1694 NewAccess != nullptr &&
1695 "Tried to create a memory access for a non-memory touching instruction");
1696 NewAccess->setDefiningAccess(Definition);
1697 return NewAccess;
1698}
1699
Daniel Berlind952cea2017-04-07 01:28:36 +00001700// Return true if the instruction has ordering constraints.
1701// Note specifically that this only considers stores and loads
1702// because others are still considered ModRef by getModRefInfo.
1703static inline bool isOrdered(const Instruction *I) {
1704 if (auto *SI = dyn_cast<StoreInst>(I)) {
1705 if (!SI->isUnordered())
1706 return true;
1707 } else if (auto *LI = dyn_cast<LoadInst>(I)) {
1708 if (!LI->isUnordered())
1709 return true;
1710 }
1711 return false;
1712}
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00001713
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001714/// Helper function to create new memory accesses
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001715template <typename AliasAnalysisType>
Alina Sbirlea79800992018-09-10 20:13:01 +00001716MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I,
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001717 AliasAnalysisType *AAP,
Alina Sbirlea79800992018-09-10 20:13:01 +00001718 const MemoryUseOrDef *Template) {
Peter Collingbourneb9aa1f42016-05-26 04:58:46 +00001719 // The assume intrinsic has a control dependency which we model by claiming
1720 // that it writes arbitrarily. Ignore that fake memory dependency here.
1721 // FIXME: Replace this special casing with a more accurate modelling of
1722 // assume's control dependency.
1723 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
1724 if (II->getIntrinsicID() == Intrinsic::assume)
1725 return nullptr;
1726
Alina Sbirlea79800992018-09-10 20:13:01 +00001727 bool Def, Use;
1728 if (Template) {
1729 Def = dyn_cast_or_null<MemoryDef>(Template) != nullptr;
1730 Use = dyn_cast_or_null<MemoryUse>(Template) != nullptr;
1731#if !defined(NDEBUG)
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001732 ModRefInfo ModRef = AAP->getModRefInfo(I, None);
Alina Sbirlea79800992018-09-10 20:13:01 +00001733 bool DefCheck, UseCheck;
1734 DefCheck = isModSet(ModRef) || isOrdered(I);
1735 UseCheck = isRefSet(ModRef);
1736 assert(Def == DefCheck && (Def || Use == UseCheck) && "Invalid template");
1737#endif
1738 } else {
1739 // Find out what affect this instruction has on memory.
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001740 ModRefInfo ModRef = AAP->getModRefInfo(I, None);
Alina Sbirlea79800992018-09-10 20:13:01 +00001741 // The isOrdered check is used to ensure that volatiles end up as defs
1742 // (atomics end up as ModRef right now anyway). Until we separate the
1743 // ordering chain from the memory chain, this enables people to see at least
1744 // some relative ordering to volatiles. Note that getClobberingMemoryAccess
1745 // will still give an answer that bypasses other volatile loads. TODO:
1746 // Separate memory aliasing and ordering into two different chains so that
1747 // we can precisely represent both "what memory will this read/write/is
1748 // clobbered by" and "what instructions can I move this past".
1749 Def = isModSet(ModRef) || isOrdered(I);
1750 Use = isRefSet(ModRef);
1751 }
George Burgess IVe1100f52016-02-02 22:46:49 +00001752
1753 // It's possible for an instruction to not modify memory at all. During
1754 // construction, we ignore them.
Peter Collingbourneffecb142016-05-26 01:19:17 +00001755 if (!Def && !Use)
George Burgess IVe1100f52016-02-02 22:46:49 +00001756 return nullptr;
1757
George Burgess IVb42b7622016-03-11 19:34:03 +00001758 MemoryUseOrDef *MUD;
George Burgess IVe1100f52016-02-02 22:46:49 +00001759 if (Def)
George Burgess IVb42b7622016-03-11 19:34:03 +00001760 MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++);
George Burgess IVe1100f52016-02-02 22:46:49 +00001761 else
George Burgess IVb42b7622016-03-11 19:34:03 +00001762 MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent());
Daniel Berlin5130cc82016-07-31 21:08:20 +00001763 ValueToMemoryAccess[I] = MUD;
George Burgess IVb42b7622016-03-11 19:34:03 +00001764 return MUD;
George Burgess IVe1100f52016-02-02 22:46:49 +00001765}
1766
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001767/// Returns true if \p Replacer dominates \p Replacee .
George Burgess IVe1100f52016-02-02 22:46:49 +00001768bool MemorySSA::dominatesUse(const MemoryAccess *Replacer,
1769 const MemoryAccess *Replacee) const {
1770 if (isa<MemoryUseOrDef>(Replacee))
1771 return DT->dominates(Replacer->getBlock(), Replacee->getBlock());
1772 const auto *MP = cast<MemoryPhi>(Replacee);
1773 // For a phi node, the use occurs in the predecessor block of the phi node.
1774 // Since we may occur multiple times in the phi node, we have to check each
1775 // operand to ensure Replacer dominates each operand where Replacee occurs.
1776 for (const Use &Arg : MP->operands()) {
George Burgess IVb5a229f2016-02-02 23:15:26 +00001777 if (Arg.get() != Replacee &&
George Burgess IVe1100f52016-02-02 22:46:49 +00001778 !DT->dominates(Replacer->getBlock(), MP->getIncomingBlock(Arg)))
1779 return false;
1780 }
1781 return true;
1782}
1783
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001784/// Properly remove \p MA from all of MemorySSA's lookup tables.
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001785void MemorySSA::removeFromLookups(MemoryAccess *MA) {
1786 assert(MA->use_empty() &&
1787 "Trying to remove memory access that still has uses");
Daniel Berlin5c46b942016-07-19 22:49:43 +00001788 BlockNumbering.erase(MA);
George Burgess IV2cbf9732018-06-22 22:34:07 +00001789 if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001790 MUD->setDefiningAccess(nullptr);
1791 // Invalidate our walker's cache if necessary
1792 if (!isa<MemoryUse>(MA))
Alina Sbirleabfc779e2019-03-22 17:22:19 +00001793 getWalker()->invalidateInfo(MA);
George Burgess IV2cbf9732018-06-22 22:34:07 +00001794
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001795 Value *MemoryInst;
George Burgess IV2cbf9732018-06-22 22:34:07 +00001796 if (const auto *MUD = dyn_cast<MemoryUseOrDef>(MA))
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001797 MemoryInst = MUD->getMemoryInst();
George Burgess IV2cbf9732018-06-22 22:34:07 +00001798 else
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001799 MemoryInst = MA->getBlock();
George Burgess IV2cbf9732018-06-22 22:34:07 +00001800
Daniel Berlin5130cc82016-07-31 21:08:20 +00001801 auto VMA = ValueToMemoryAccess.find(MemoryInst);
1802 if (VMA->second == MA)
1803 ValueToMemoryAccess.erase(VMA);
Daniel Berlin60ead052017-01-28 01:23:13 +00001804}
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001805
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001806/// Properly remove \p MA from all of MemorySSA's lists.
Daniel Berlin60ead052017-01-28 01:23:13 +00001807///
1808/// Because of the way the intrusive list and use lists work, it is important to
1809/// do removal in the right order.
1810/// ShouldDelete defaults to true, and will cause the memory access to also be
1811/// deleted, not just removed.
1812void MemorySSA::removeFromLists(MemoryAccess *MA, bool ShouldDelete) {
Alina Sbirleada1e80f2018-06-29 20:46:16 +00001813 BasicBlock *BB = MA->getBlock();
Daniel Berlind602e042017-01-25 20:56:19 +00001814 // The access list owns the reference, so we erase it from the non-owning list
1815 // first.
1816 if (!isa<MemoryUse>(MA)) {
Alina Sbirleada1e80f2018-06-29 20:46:16 +00001817 auto DefsIt = PerBlockDefs.find(BB);
Daniel Berlind602e042017-01-25 20:56:19 +00001818 std::unique_ptr<DefsList> &Defs = DefsIt->second;
1819 Defs->remove(*MA);
1820 if (Defs->empty())
1821 PerBlockDefs.erase(DefsIt);
1822 }
1823
Daniel Berlin60ead052017-01-28 01:23:13 +00001824 // The erase call here will delete it. If we don't want it deleted, we call
1825 // remove instead.
Alina Sbirleada1e80f2018-06-29 20:46:16 +00001826 auto AccessIt = PerBlockAccesses.find(BB);
Daniel Berlinada263d2016-06-20 20:21:33 +00001827 std::unique_ptr<AccessList> &Accesses = AccessIt->second;
Daniel Berlin60ead052017-01-28 01:23:13 +00001828 if (ShouldDelete)
1829 Accesses->erase(MA);
1830 else
1831 Accesses->remove(MA);
1832
Alina Sbirleada1e80f2018-06-29 20:46:16 +00001833 if (Accesses->empty()) {
George Burgess IVe0e6e482016-03-02 02:35:04 +00001834 PerBlockAccesses.erase(AccessIt);
Alina Sbirleada1e80f2018-06-29 20:46:16 +00001835 BlockNumberingValid.erase(BB);
1836 }
Daniel Berlin83fc77b2016-03-01 18:46:54 +00001837}
1838
George Burgess IVe1100f52016-02-02 22:46:49 +00001839void MemorySSA::print(raw_ostream &OS) const {
1840 MemorySSAAnnotatedWriter Writer(this);
1841 F.print(OS, &Writer);
1842}
1843
Aaron Ballman615eb472017-10-15 14:32:27 +00001844#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Berlin78cbd282017-02-20 22:26:03 +00001845LLVM_DUMP_METHOD void MemorySSA::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +00001846#endif
George Burgess IVe1100f52016-02-02 22:46:49 +00001847
Daniel Berlin932b4cb2016-02-10 17:39:43 +00001848void MemorySSA::verifyMemorySSA() const {
1849 verifyDefUses(F);
1850 verifyDomination(F);
Daniel Berlin14300262016-06-21 18:39:20 +00001851 verifyOrdering(F);
George Burgess IV97ec6242018-06-25 05:30:36 +00001852 verifyDominationNumbers(F);
Alina Sbirlead77edc02019-02-11 19:51:21 +00001853 // Previously, the verification used to also verify that the clobberingAccess
1854 // cached by MemorySSA is the same as the clobberingAccess found at a later
1855 // query to AA. This does not hold true in general due to the current fragility
1856 // of BasicAA which has arbitrary caps on the things it analyzes before giving
1857 // up. As a result, transformations that are correct, will lead to BasicAA
1858 // returning different Alias answers before and after that transformation.
1859 // Invalidating MemorySSA is not an option, as the results in BasicAA can be so
1860 // random, in the worst case we'd need to rebuild MemorySSA from scratch after
1861 // every transformation, which defeats the purpose of using it. For such an
1862 // example, see test4 added in D51960.
Daniel Berlin14300262016-06-21 18:39:20 +00001863}
1864
George Burgess IV97ec6242018-06-25 05:30:36 +00001865/// Verify that all of the blocks we believe to have valid domination numbers
1866/// actually have valid domination numbers.
1867void MemorySSA::verifyDominationNumbers(const Function &F) const {
1868#ifndef NDEBUG
1869 if (BlockNumberingValid.empty())
1870 return;
1871
1872 SmallPtrSet<const BasicBlock *, 16> ValidBlocks = BlockNumberingValid;
1873 for (const BasicBlock &BB : F) {
1874 if (!ValidBlocks.count(&BB))
1875 continue;
1876
1877 ValidBlocks.erase(&BB);
1878
1879 const AccessList *Accesses = getBlockAccesses(&BB);
1880 // It's correct to say an empty block has valid numbering.
1881 if (!Accesses)
1882 continue;
1883
1884 // Block numbering starts at 1.
1885 unsigned long LastNumber = 0;
1886 for (const MemoryAccess &MA : *Accesses) {
1887 auto ThisNumberIter = BlockNumbering.find(&MA);
1888 assert(ThisNumberIter != BlockNumbering.end() &&
1889 "MemoryAccess has no domination number in a valid block!");
1890
1891 unsigned long ThisNumber = ThisNumberIter->second;
1892 assert(ThisNumber > LastNumber &&
1893 "Domination numbers should be strictly increasing!");
1894 LastNumber = ThisNumber;
1895 }
1896 }
1897
1898 assert(ValidBlocks.empty() &&
1899 "All valid BasicBlocks should exist in F -- dangling pointers?");
1900#endif
1901}
1902
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001903/// Verify that the order and existence of MemoryAccesses matches the
Daniel Berlin14300262016-06-21 18:39:20 +00001904/// order and existence of memory affecting instructions.
1905void MemorySSA::verifyOrdering(Function &F) const {
George Burgess IV6a9aa022018-08-28 00:32:32 +00001906#ifndef NDEBUG
Daniel Berlin14300262016-06-21 18:39:20 +00001907 // Walk all the blocks, comparing what the lookups think and what the access
1908 // lists think, as well as the order in the blocks vs the order in the access
1909 // lists.
1910 SmallVector<MemoryAccess *, 32> ActualAccesses;
Daniel Berlind602e042017-01-25 20:56:19 +00001911 SmallVector<MemoryAccess *, 32> ActualDefs;
Daniel Berlin14300262016-06-21 18:39:20 +00001912 for (BasicBlock &B : F) {
1913 const AccessList *AL = getBlockAccesses(&B);
Daniel Berlind602e042017-01-25 20:56:19 +00001914 const auto *DL = getBlockDefs(&B);
Daniel Berlin14300262016-06-21 18:39:20 +00001915 MemoryAccess *Phi = getMemoryAccess(&B);
Daniel Berlind602e042017-01-25 20:56:19 +00001916 if (Phi) {
Daniel Berlin14300262016-06-21 18:39:20 +00001917 ActualAccesses.push_back(Phi);
Daniel Berlind602e042017-01-25 20:56:19 +00001918 ActualDefs.push_back(Phi);
1919 }
1920
Daniel Berlin14300262016-06-21 18:39:20 +00001921 for (Instruction &I : B) {
1922 MemoryAccess *MA = getMemoryAccess(&I);
Daniel Berlind602e042017-01-25 20:56:19 +00001923 assert((!MA || (AL && (isa<MemoryUse>(MA) || DL))) &&
1924 "We have memory affecting instructions "
1925 "in this block but they are not in the "
1926 "access list or defs list");
1927 if (MA) {
Daniel Berlin14300262016-06-21 18:39:20 +00001928 ActualAccesses.push_back(MA);
Daniel Berlind602e042017-01-25 20:56:19 +00001929 if (isa<MemoryDef>(MA))
1930 ActualDefs.push_back(MA);
1931 }
Daniel Berlin14300262016-06-21 18:39:20 +00001932 }
1933 // Either we hit the assert, really have no accesses, or we have both
Daniel Berlind602e042017-01-25 20:56:19 +00001934 // accesses and an access list.
1935 // Same with defs.
1936 if (!AL && !DL)
Daniel Berlin14300262016-06-21 18:39:20 +00001937 continue;
1938 assert(AL->size() == ActualAccesses.size() &&
1939 "We don't have the same number of accesses in the block as on the "
1940 "access list");
Davide Italiano6c77de02017-01-30 03:16:43 +00001941 assert((DL || ActualDefs.size() == 0) &&
1942 "Either we should have a defs list, or we should have no defs");
Daniel Berlind602e042017-01-25 20:56:19 +00001943 assert((!DL || DL->size() == ActualDefs.size()) &&
1944 "We don't have the same number of defs in the block as on the "
1945 "def list");
Daniel Berlin14300262016-06-21 18:39:20 +00001946 auto ALI = AL->begin();
1947 auto AAI = ActualAccesses.begin();
1948 while (ALI != AL->end() && AAI != ActualAccesses.end()) {
1949 assert(&*ALI == *AAI && "Not the same accesses in the same order");
1950 ++ALI;
1951 ++AAI;
1952 }
1953 ActualAccesses.clear();
Daniel Berlind602e042017-01-25 20:56:19 +00001954 if (DL) {
1955 auto DLI = DL->begin();
1956 auto ADI = ActualDefs.begin();
1957 while (DLI != DL->end() && ADI != ActualDefs.end()) {
1958 assert(&*DLI == *ADI && "Not the same defs in the same order");
1959 ++DLI;
1960 ++ADI;
1961 }
1962 }
1963 ActualDefs.clear();
Daniel Berlin14300262016-06-21 18:39:20 +00001964 }
George Burgess IV6a9aa022018-08-28 00:32:32 +00001965#endif
Daniel Berlin932b4cb2016-02-10 17:39:43 +00001966}
1967
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001968/// Verify the domination properties of MemorySSA by checking that each
George Burgess IVe1100f52016-02-02 22:46:49 +00001969/// definition dominates all of its uses.
Daniel Berlin932b4cb2016-02-10 17:39:43 +00001970void MemorySSA::verifyDomination(Function &F) const {
Daniel Berlin7af95872016-08-05 21:47:20 +00001971#ifndef NDEBUG
George Burgess IVe1100f52016-02-02 22:46:49 +00001972 for (BasicBlock &B : F) {
1973 // Phi nodes are attached to basic blocks
Daniel Berlin2919b1c2016-08-05 21:46:52 +00001974 if (MemoryPhi *MP = getMemoryAccess(&B))
1975 for (const Use &U : MP->uses())
1976 assert(dominates(MP, U) && "Memory PHI does not dominate it's uses");
Daniel Berlin7af95872016-08-05 21:47:20 +00001977
George Burgess IVe1100f52016-02-02 22:46:49 +00001978 for (Instruction &I : B) {
1979 MemoryAccess *MD = dyn_cast_or_null<MemoryDef>(getMemoryAccess(&I));
1980 if (!MD)
1981 continue;
1982
Daniel Berlin2919b1c2016-08-05 21:46:52 +00001983 for (const Use &U : MD->uses())
1984 assert(dominates(MD, U) && "Memory Def does not dominate it's uses");
George Burgess IVe1100f52016-02-02 22:46:49 +00001985 }
1986 }
Daniel Berlin7af95872016-08-05 21:47:20 +00001987#endif
George Burgess IVe1100f52016-02-02 22:46:49 +00001988}
1989
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001990/// Verify the def-use lists in MemorySSA, by verifying that \p Use
George Burgess IVe1100f52016-02-02 22:46:49 +00001991/// appears in the use list of \p Def.
Daniel Berlin932b4cb2016-02-10 17:39:43 +00001992void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const {
Daniel Berlin7af95872016-08-05 21:47:20 +00001993#ifndef NDEBUG
George Burgess IVe1100f52016-02-02 22:46:49 +00001994 // The live on entry use may cause us to get a NULL def here
Daniel Berlin7af95872016-08-05 21:47:20 +00001995 if (!Def)
1996 assert(isLiveOnEntryDef(Use) &&
1997 "Null def but use not point to live on entry def");
1998 else
Daniel Berlinda2f38e2016-08-11 21:26:50 +00001999 assert(is_contained(Def->users(), Use) &&
Daniel Berlin7af95872016-08-05 21:47:20 +00002000 "Did not find use in def's use list");
2001#endif
George Burgess IVe1100f52016-02-02 22:46:49 +00002002}
2003
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002004/// Verify the immediate use information, by walking all the memory
George Burgess IVe1100f52016-02-02 22:46:49 +00002005/// accesses and verifying that, for each use, it appears in the
2006/// appropriate def's use list
Daniel Berlin932b4cb2016-02-10 17:39:43 +00002007void MemorySSA::verifyDefUses(Function &F) const {
George Burgess IV6a9aa022018-08-28 00:32:32 +00002008#ifndef NDEBUG
George Burgess IVe1100f52016-02-02 22:46:49 +00002009 for (BasicBlock &B : F) {
2010 // Phi nodes are attached to basic blocks
Daniel Berlin14300262016-06-21 18:39:20 +00002011 if (MemoryPhi *Phi = getMemoryAccess(&B)) {
David Majnemer580e7542016-06-25 00:04:06 +00002012 assert(Phi->getNumOperands() == static_cast<unsigned>(std::distance(
2013 pred_begin(&B), pred_end(&B))) &&
Daniel Berlin14300262016-06-21 18:39:20 +00002014 "Incomplete MemoryPhi Node");
Alina Sbirlea201d02c2018-06-20 21:06:13 +00002015 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
George Burgess IVe1100f52016-02-02 22:46:49 +00002016 verifyUseInDefs(Phi->getIncomingValue(I), Phi);
Alina Sbirlea201d02c2018-06-20 21:06:13 +00002017 assert(find(predecessors(&B), Phi->getIncomingBlock(I)) !=
2018 pred_end(&B) &&
2019 "Incoming phi block not a block predecessor");
2020 }
Daniel Berlin14300262016-06-21 18:39:20 +00002021 }
George Burgess IVe1100f52016-02-02 22:46:49 +00002022
2023 for (Instruction &I : B) {
George Burgess IV66837ab2016-11-01 21:17:46 +00002024 if (MemoryUseOrDef *MA = getMemoryAccess(&I)) {
2025 verifyUseInDefs(MA->getDefiningAccess(), MA);
George Burgess IVe1100f52016-02-02 22:46:49 +00002026 }
2027 }
2028 }
George Burgess IV6a9aa022018-08-28 00:32:32 +00002029#endif
George Burgess IVe1100f52016-02-02 22:46:49 +00002030}
2031
Daniel Berlin5c46b942016-07-19 22:49:43 +00002032/// Perform a local numbering on blocks so that instruction ordering can be
2033/// determined in constant time.
2034/// TODO: We currently just number in order. If we numbered by N, we could
2035/// allow at least N-1 sequences of insertBefore or insertAfter (and at least
2036/// log2(N) sequences of mixed before and after) without needing to invalidate
2037/// the numbering.
2038void MemorySSA::renumberBlock(const BasicBlock *B) const {
2039 // The pre-increment ensures the numbers really start at 1.
2040 unsigned long CurrentNumber = 0;
2041 const AccessList *AL = getBlockAccesses(B);
2042 assert(AL != nullptr && "Asking to renumber an empty block");
2043 for (const auto &I : *AL)
2044 BlockNumbering[&I] = ++CurrentNumber;
2045 BlockNumberingValid.insert(B);
2046}
2047
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002048/// Determine, for two memory accesses in the same block,
George Burgess IVe1100f52016-02-02 22:46:49 +00002049/// whether \p Dominator dominates \p Dominatee.
2050/// \returns True if \p Dominator dominates \p Dominatee.
2051bool MemorySSA::locallyDominates(const MemoryAccess *Dominator,
2052 const MemoryAccess *Dominatee) const {
Daniel Berlin5c46b942016-07-19 22:49:43 +00002053 const BasicBlock *DominatorBlock = Dominator->getBlock();
Daniel Berlin5c46b942016-07-19 22:49:43 +00002054
Daniel Berlin19860302016-07-19 23:08:08 +00002055 assert((DominatorBlock == Dominatee->getBlock()) &&
Daniel Berlin5c46b942016-07-19 22:49:43 +00002056 "Asking for local domination when accesses are in different blocks!");
Sebastian Pope1f60b12016-06-10 21:36:41 +00002057 // A node dominates itself.
2058 if (Dominatee == Dominator)
2059 return true;
2060
2061 // When Dominatee is defined on function entry, it is not dominated by another
2062 // memory access.
2063 if (isLiveOnEntryDef(Dominatee))
2064 return false;
2065
2066 // When Dominator is defined on function entry, it dominates the other memory
2067 // access.
2068 if (isLiveOnEntryDef(Dominator))
2069 return true;
2070
Daniel Berlin5c46b942016-07-19 22:49:43 +00002071 if (!BlockNumberingValid.count(DominatorBlock))
2072 renumberBlock(DominatorBlock);
George Burgess IVe1100f52016-02-02 22:46:49 +00002073
Daniel Berlin5c46b942016-07-19 22:49:43 +00002074 unsigned long DominatorNum = BlockNumbering.lookup(Dominator);
2075 // All numbers start with 1
2076 assert(DominatorNum != 0 && "Block was not numbered properly");
2077 unsigned long DominateeNum = BlockNumbering.lookup(Dominatee);
2078 assert(DominateeNum != 0 && "Block was not numbered properly");
2079 return DominatorNum < DominateeNum;
George Burgess IVe1100f52016-02-02 22:46:49 +00002080}
2081
George Burgess IV5f308972016-07-19 01:29:15 +00002082bool MemorySSA::dominates(const MemoryAccess *Dominator,
2083 const MemoryAccess *Dominatee) const {
2084 if (Dominator == Dominatee)
2085 return true;
2086
2087 if (isLiveOnEntryDef(Dominatee))
2088 return false;
2089
2090 if (Dominator->getBlock() != Dominatee->getBlock())
2091 return DT->dominates(Dominator->getBlock(), Dominatee->getBlock());
2092 return locallyDominates(Dominator, Dominatee);
2093}
2094
Daniel Berlin2919b1c2016-08-05 21:46:52 +00002095bool MemorySSA::dominates(const MemoryAccess *Dominator,
2096 const Use &Dominatee) const {
2097 if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Dominatee.getUser())) {
2098 BasicBlock *UseBB = MP->getIncomingBlock(Dominatee);
2099 // The def must dominate the incoming block of the phi.
2100 if (UseBB != Dominator->getBlock())
2101 return DT->dominates(Dominator->getBlock(), UseBB);
2102 // If the UseBB and the DefBB are the same, compare locally.
2103 return locallyDominates(Dominator, cast<MemoryAccess>(Dominatee));
2104 }
2105 // If it's not a PHI node use, the normal dominates can already handle it.
2106 return dominates(Dominator, cast<MemoryAccess>(Dominatee.getUser()));
2107}
2108
George Burgess IVe1100f52016-02-02 22:46:49 +00002109const static char LiveOnEntryStr[] = "liveOnEntry";
2110
Reid Kleckner96ab8722017-05-18 17:24:10 +00002111void MemoryAccess::print(raw_ostream &OS) const {
2112 switch (getValueID()) {
2113 case MemoryPhiVal: return static_cast<const MemoryPhi *>(this)->print(OS);
2114 case MemoryDefVal: return static_cast<const MemoryDef *>(this)->print(OS);
2115 case MemoryUseVal: return static_cast<const MemoryUse *>(this)->print(OS);
2116 }
2117 llvm_unreachable("invalid value id");
2118}
2119
George Burgess IVe1100f52016-02-02 22:46:49 +00002120void MemoryDef::print(raw_ostream &OS) const {
2121 MemoryAccess *UO = getDefiningAccess();
2122
George Burgess IVaa283d82018-06-14 19:55:53 +00002123 auto printID = [&OS](MemoryAccess *A) {
2124 if (A && A->getID())
2125 OS << A->getID();
2126 else
2127 OS << LiveOnEntryStr;
2128 };
2129
George Burgess IVe1100f52016-02-02 22:46:49 +00002130 OS << getID() << " = MemoryDef(";
George Burgess IVaa283d82018-06-14 19:55:53 +00002131 printID(UO);
2132 OS << ")";
2133
2134 if (isOptimized()) {
2135 OS << "->";
2136 printID(getOptimized());
2137
2138 if (Optional<AliasResult> AR = getOptimizedAccessType())
2139 OS << " " << *AR;
2140 }
George Burgess IVe1100f52016-02-02 22:46:49 +00002141}
2142
2143void MemoryPhi::print(raw_ostream &OS) const {
2144 bool First = true;
2145 OS << getID() << " = MemoryPhi(";
2146 for (const auto &Op : operands()) {
2147 BasicBlock *BB = getIncomingBlock(Op);
2148 MemoryAccess *MA = cast<MemoryAccess>(Op);
2149 if (!First)
2150 OS << ',';
2151 else
2152 First = false;
2153
2154 OS << '{';
2155 if (BB->hasName())
2156 OS << BB->getName();
2157 else
2158 BB->printAsOperand(OS, false);
2159 OS << ',';
2160 if (unsigned ID = MA->getID())
2161 OS << ID;
2162 else
2163 OS << LiveOnEntryStr;
2164 OS << '}';
2165 }
2166 OS << ')';
2167}
2168
George Burgess IVe1100f52016-02-02 22:46:49 +00002169void MemoryUse::print(raw_ostream &OS) const {
2170 MemoryAccess *UO = getDefiningAccess();
2171 OS << "MemoryUse(";
2172 if (UO && UO->getID())
2173 OS << UO->getID();
2174 else
2175 OS << LiveOnEntryStr;
2176 OS << ')';
George Burgess IVaa283d82018-06-14 19:55:53 +00002177
2178 if (Optional<AliasResult> AR = getOptimizedAccessType())
2179 OS << " " << *AR;
George Burgess IVe1100f52016-02-02 22:46:49 +00002180}
2181
2182void MemoryAccess::dump() const {
Daniel Berlin78cbd282017-02-20 22:26:03 +00002183// Cannot completely remove virtual function even in release mode.
Aaron Ballman615eb472017-10-15 14:32:27 +00002184#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
George Burgess IVe1100f52016-02-02 22:46:49 +00002185 print(dbgs());
2186 dbgs() << "\n";
Matthias Braun8c209aa2017-01-28 02:02:38 +00002187#endif
George Burgess IVe1100f52016-02-02 22:46:49 +00002188}
2189
Chad Rosier232e29e2016-07-06 21:20:47 +00002190char MemorySSAPrinterLegacyPass::ID = 0;
2191
2192MemorySSAPrinterLegacyPass::MemorySSAPrinterLegacyPass() : FunctionPass(ID) {
2193 initializeMemorySSAPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
2194}
2195
2196void MemorySSAPrinterLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
2197 AU.setPreservesAll();
2198 AU.addRequired<MemorySSAWrapperPass>();
Chad Rosier232e29e2016-07-06 21:20:47 +00002199}
2200
2201bool MemorySSAPrinterLegacyPass::runOnFunction(Function &F) {
2202 auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
2203 MSSA.print(dbgs());
2204 if (VerifyMemorySSA)
2205 MSSA.verifyMemorySSA();
2206 return false;
2207}
2208
Chandler Carruthdab4eae2016-11-23 17:53:26 +00002209AnalysisKey MemorySSAAnalysis::Key;
George Burgess IVe1100f52016-02-02 22:46:49 +00002210
Daniel Berlin1e98c042016-09-26 17:22:54 +00002211MemorySSAAnalysis::Result MemorySSAAnalysis::run(Function &F,
2212 FunctionAnalysisManager &AM) {
Geoff Berryb96d3b22016-06-01 21:30:40 +00002213 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
2214 auto &AA = AM.getResult<AAManager>(F);
Eugene Zelenkobb1b2d02017-08-16 22:07:40 +00002215 return MemorySSAAnalysis::Result(llvm::make_unique<MemorySSA>(F, &AA, &DT));
George Burgess IVe1100f52016-02-02 22:46:49 +00002216}
2217
Alina Sbirleab4683202019-04-30 22:43:55 +00002218bool MemorySSAAnalysis::Result::invalidate(
2219 Function &F, const PreservedAnalyses &PA,
2220 FunctionAnalysisManager::Invalidator &Inv) {
2221 auto PAC = PA.getChecker<MemorySSAAnalysis>();
2222 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
2223 Inv.invalidate<AAManager>(F, PA) ||
2224 Inv.invalidate<DominatorTreeAnalysis>(F, PA);
2225}
2226
Geoff Berryb96d3b22016-06-01 21:30:40 +00002227PreservedAnalyses MemorySSAPrinterPass::run(Function &F,
2228 FunctionAnalysisManager &AM) {
2229 OS << "MemorySSA for function: " << F.getName() << "\n";
Geoff Berry290a13e2016-08-08 18:27:22 +00002230 AM.getResult<MemorySSAAnalysis>(F).getMSSA().print(OS);
Geoff Berryb96d3b22016-06-01 21:30:40 +00002231
2232 return PreservedAnalyses::all();
George Burgess IVe1100f52016-02-02 22:46:49 +00002233}
2234
Geoff Berryb96d3b22016-06-01 21:30:40 +00002235PreservedAnalyses MemorySSAVerifierPass::run(Function &F,
2236 FunctionAnalysisManager &AM) {
Geoff Berry290a13e2016-08-08 18:27:22 +00002237 AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA();
Geoff Berryb96d3b22016-06-01 21:30:40 +00002238
2239 return PreservedAnalyses::all();
2240}
2241
2242char MemorySSAWrapperPass::ID = 0;
2243
2244MemorySSAWrapperPass::MemorySSAWrapperPass() : FunctionPass(ID) {
2245 initializeMemorySSAWrapperPassPass(*PassRegistry::getPassRegistry());
2246}
2247
2248void MemorySSAWrapperPass::releaseMemory() { MSSA.reset(); }
2249
2250void MemorySSAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
George Burgess IVe1100f52016-02-02 22:46:49 +00002251 AU.setPreservesAll();
Geoff Berryb96d3b22016-06-01 21:30:40 +00002252 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
2253 AU.addRequiredTransitive<AAResultsWrapperPass>();
George Burgess IVe1100f52016-02-02 22:46:49 +00002254}
2255
Geoff Berryb96d3b22016-06-01 21:30:40 +00002256bool MemorySSAWrapperPass::runOnFunction(Function &F) {
2257 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2258 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
2259 MSSA.reset(new MemorySSA(F, &AA, &DT));
George Burgess IVe1100f52016-02-02 22:46:49 +00002260 return false;
2261}
2262
Geoff Berryb96d3b22016-06-01 21:30:40 +00002263void MemorySSAWrapperPass::verifyAnalysis() const { MSSA->verifyMemorySSA(); }
George Burgess IVe1100f52016-02-02 22:46:49 +00002264
Geoff Berryb96d3b22016-06-01 21:30:40 +00002265void MemorySSAWrapperPass::print(raw_ostream &OS, const Module *M) const {
George Burgess IVe1100f52016-02-02 22:46:49 +00002266 MSSA->print(OS);
2267}
2268
George Burgess IVe1100f52016-02-02 22:46:49 +00002269MemorySSAWalker::MemorySSAWalker(MemorySSA *M) : MSSA(M) {}
2270
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002271/// Walk the use-def chains starting at \p StartingAccess and find
George Burgess IVe1100f52016-02-02 22:46:49 +00002272/// the MemoryAccess that actually clobbers Loc.
2273///
2274/// \returns our clobbering memory access
Alina Sbirleabfc779e2019-03-22 17:22:19 +00002275template <typename AliasAnalysisType>
2276MemoryAccess *
2277MemorySSA::ClobberWalkerBase<AliasAnalysisType>::getClobberingMemoryAccessBase(
Alina Sbirleaf085cc52019-03-29 21:56:09 +00002278 MemoryAccess *StartingAccess, const MemoryLocation &Loc,
2279 unsigned &UpwardWalkLimit) {
George Burgess IVe1100f52016-02-02 22:46:49 +00002280 if (isa<MemoryPhi>(StartingAccess))
2281 return StartingAccess;
2282
2283 auto *StartingUseOrDef = cast<MemoryUseOrDef>(StartingAccess);
2284 if (MSSA->isLiveOnEntryDef(StartingUseOrDef))
2285 return StartingUseOrDef;
2286
2287 Instruction *I = StartingUseOrDef->getMemoryInst();
2288
2289 // Conservatively, fences are always clobbers, so don't perform the walk if we
2290 // hit a fence.
Chandler Carruth363ac682019-01-07 05:42:51 +00002291 if (!isa<CallBase>(I) && I->isFenceLike())
George Burgess IVe1100f52016-02-02 22:46:49 +00002292 return StartingUseOrDef;
2293
2294 UpwardsMemoryQuery Q;
2295 Q.OriginalAccess = StartingUseOrDef;
2296 Q.StartingLoc = Loc;
George Burgess IV5f308972016-07-19 01:29:15 +00002297 Q.Inst = I;
George Burgess IVe1100f52016-02-02 22:46:49 +00002298 Q.IsCall = false;
George Burgess IVe1100f52016-02-02 22:46:49 +00002299
George Burgess IVe1100f52016-02-02 22:46:49 +00002300 // Unlike the other function, do not walk to the def of a def, because we are
2301 // handed something we already believe is the clobbering access.
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002302 // We never set SkipSelf to true in Q in this method.
George Burgess IVe1100f52016-02-02 22:46:49 +00002303 MemoryAccess *DefiningAccess = isa<MemoryUse>(StartingUseOrDef)
2304 ? StartingUseOrDef->getDefiningAccess()
2305 : StartingUseOrDef;
2306
Alina Sbirleaf085cc52019-03-29 21:56:09 +00002307 MemoryAccess *Clobber =
2308 Walker.findClobber(DefiningAccess, Q, UpwardWalkLimit);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002309 LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
2310 LLVM_DEBUG(dbgs() << *StartingUseOrDef << "\n");
2311 LLVM_DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is ");
2312 LLVM_DEBUG(dbgs() << *Clobber << "\n");
George Burgess IVe1100f52016-02-02 22:46:49 +00002313 return Clobber;
2314}
2315
Alina Sbirleabfc779e2019-03-22 17:22:19 +00002316template <typename AliasAnalysisType>
George Burgess IVe1100f52016-02-02 22:46:49 +00002317MemoryAccess *
Alina Sbirleabfc779e2019-03-22 17:22:19 +00002318MemorySSA::ClobberWalkerBase<AliasAnalysisType>::getClobberingMemoryAccessBase(
Alina Sbirleaf085cc52019-03-29 21:56:09 +00002319 MemoryAccess *MA, unsigned &UpwardWalkLimit, bool SkipSelf) {
George Burgess IV400ae402016-07-20 19:51:34 +00002320 auto *StartingAccess = dyn_cast<MemoryUseOrDef>(MA);
2321 // If this is a MemoryPhi, we can't do anything.
2322 if (!StartingAccess)
2323 return MA;
George Burgess IVe1100f52016-02-02 22:46:49 +00002324
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002325 bool IsOptimized = false;
2326
Daniel Berlincd2deac2016-10-20 20:13:45 +00002327 // If this is an already optimized use or def, return the optimized result.
Alina Sbirlead90c9f42018-03-08 18:03:14 +00002328 // Note: Currently, we store the optimized def result in a separate field,
2329 // since we can't use the defining access.
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002330 if (StartingAccess->isOptimized()) {
2331 if (!SkipSelf || !isa<MemoryDef>(StartingAccess))
2332 return StartingAccess->getOptimized();
2333 IsOptimized = true;
2334 }
Daniel Berlincd2deac2016-10-20 20:13:45 +00002335
George Burgess IV400ae402016-07-20 19:51:34 +00002336 const Instruction *I = StartingAccess->getMemoryInst();
George Burgess IV44477c62018-03-11 04:16:12 +00002337 // We can't sanely do anything with a fence, since they conservatively clobber
2338 // all memory, and have no locations to get pointers from to try to
2339 // disambiguate.
Chandler Carruth363ac682019-01-07 05:42:51 +00002340 if (!isa<CallBase>(I) && I->isFenceLike())
George Burgess IVe1100f52016-02-02 22:46:49 +00002341 return StartingAccess;
2342
Alina Sbirleab4d088d2018-11-13 21:12:49 +00002343 UpwardsMemoryQuery Q(I, StartingAccess);
2344
Alina Sbirleabfc779e2019-03-22 17:22:19 +00002345 if (isUseTriviallyOptimizableToLiveOnEntry(*Walker.getAA(), I)) {
George Burgess IV024f3d22016-08-03 19:57:02 +00002346 MemoryAccess *LiveOnEntry = MSSA->getLiveOnEntryDef();
George Burgess IV44477c62018-03-11 04:16:12 +00002347 StartingAccess->setOptimized(LiveOnEntry);
2348 StartingAccess->setOptimizedAccessType(None);
George Burgess IV024f3d22016-08-03 19:57:02 +00002349 return LiveOnEntry;
2350 }
2351
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002352 MemoryAccess *OptimizedAccess;
2353 if (!IsOptimized) {
2354 // Start with the thing we already think clobbers this location
2355 MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess();
George Burgess IVe1100f52016-02-02 22:46:49 +00002356
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002357 // At this point, DefiningAccess may be the live on entry def.
2358 // If it is, we will not get a better result.
2359 if (MSSA->isLiveOnEntryDef(DefiningAccess)) {
2360 StartingAccess->setOptimized(DefiningAccess);
2361 StartingAccess->setOptimizedAccessType(None);
2362 return DefiningAccess;
2363 }
George Burgess IVe1100f52016-02-02 22:46:49 +00002364
Alina Sbirleaf085cc52019-03-29 21:56:09 +00002365 OptimizedAccess = Walker.findClobber(DefiningAccess, Q, UpwardWalkLimit);
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002366 StartingAccess->setOptimized(OptimizedAccess);
2367 if (MSSA->isLiveOnEntryDef(OptimizedAccess))
2368 StartingAccess->setOptimizedAccessType(None);
2369 else if (Q.AR == MustAlias)
2370 StartingAccess->setOptimizedAccessType(MustAlias);
2371 } else
2372 OptimizedAccess = StartingAccess->getOptimized();
2373
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002374 LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is ");
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002375 LLVM_DEBUG(dbgs() << *StartingAccess << "\n");
2376 LLVM_DEBUG(dbgs() << "Optimized Memory SSA clobber for " << *I << " is ");
2377 LLVM_DEBUG(dbgs() << *OptimizedAccess << "\n");
Alina Sbirlead90c9f42018-03-08 18:03:14 +00002378
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002379 MemoryAccess *Result;
2380 if (SkipSelf && isa<MemoryPhi>(OptimizedAccess) &&
Alina Sbirleaf085cc52019-03-29 21:56:09 +00002381 isa<MemoryDef>(StartingAccess) && UpwardWalkLimit) {
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002382 assert(isa<MemoryDef>(Q.OriginalAccess));
2383 Q.SkipSelfAccess = true;
Alina Sbirleaf085cc52019-03-29 21:56:09 +00002384 Result = Walker.findClobber(OptimizedAccess, Q, UpwardWalkLimit);
Alina Sbirleabc8aa242019-01-07 19:22:37 +00002385 } else
2386 Result = OptimizedAccess;
2387
2388 LLVM_DEBUG(dbgs() << "Result Memory SSA clobber [SkipSelf = " << SkipSelf);
2389 LLVM_DEBUG(dbgs() << "] for " << *I << " is " << *Result << "\n");
George Burgess IVe1100f52016-02-02 22:46:49 +00002390
2391 return Result;
2392}
2393
George Burgess IVe1100f52016-02-02 22:46:49 +00002394MemoryAccess *
George Burgess IV400ae402016-07-20 19:51:34 +00002395DoNothingMemorySSAWalker::getClobberingMemoryAccess(MemoryAccess *MA) {
George Burgess IVe1100f52016-02-02 22:46:49 +00002396 if (auto *Use = dyn_cast<MemoryUseOrDef>(MA))
2397 return Use->getDefiningAccess();
2398 return MA;
2399}
2400
2401MemoryAccess *DoNothingMemorySSAWalker::getClobberingMemoryAccess(
George Burgess IV013fd732016-10-28 19:22:46 +00002402 MemoryAccess *StartingAccess, const MemoryLocation &) {
George Burgess IVe1100f52016-02-02 22:46:49 +00002403 if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess))
2404 return Use->getDefiningAccess();
2405 return StartingAccess;
2406}
Reid Kleckner96ab8722017-05-18 17:24:10 +00002407
2408void MemoryPhi::deleteMe(DerivedUser *Self) {
2409 delete static_cast<MemoryPhi *>(Self);
2410}
2411
2412void MemoryDef::deleteMe(DerivedUser *Self) {
2413 delete static_cast<MemoryDef *>(Self);
2414}
2415
2416void MemoryUse::deleteMe(DerivedUser *Self) {
2417 delete static_cast<MemoryUse *>(Self);
2418}