1*9880d681SAndroid Build Coastguard Worker //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements an analysis that determines, for a given memory
11*9880d681SAndroid Build Coastguard Worker // operation, what preceding memory operations it depends on. It builds on
12*9880d681SAndroid Build Coastguard Worker // alias analysis information, and tries to provide a lazy, caching interface to
13*9880d681SAndroid Build Coastguard Worker // a common kind of alias information query.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/MemoryDependenceAnalysis.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InstructionSimplify.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/MemoryBuiltins.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/PHITransAddr.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/OrderedBasicBlock.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/PredIteratorCache.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "memdep"
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
41*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
42*9880d681SAndroid Build Coastguard Worker STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCacheNonLocalPtr,
45*9880d681SAndroid Build Coastguard Worker "Number of fully cached non-local ptr responses");
46*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCacheDirtyNonLocalPtr,
47*9880d681SAndroid Build Coastguard Worker "Number of cached, but dirty, non-local ptr responses");
48*9880d681SAndroid Build Coastguard Worker STATISTIC(NumUncacheNonLocalPtr, "Number of uncached non-local ptr responses");
49*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCacheCompleteNonLocalPtr,
50*9880d681SAndroid Build Coastguard Worker "Number of block queries that were completely cached");
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker // Limit for the number of instructions to scan in a block.
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> BlockScanLimit(
55*9880d681SAndroid Build Coastguard Worker "memdep-block-scan-limit", cl::Hidden, cl::init(100),
56*9880d681SAndroid Build Coastguard Worker cl::desc("The number of instructions to scan in a block in memory "
57*9880d681SAndroid Build Coastguard Worker "dependency analysis (default = 100)"));
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
60*9880d681SAndroid Build Coastguard Worker BlockNumberLimit("memdep-block-number-limit", cl::Hidden, cl::init(1000),
61*9880d681SAndroid Build Coastguard Worker cl::desc("The number of blocks to scan during memory "
62*9880d681SAndroid Build Coastguard Worker "dependency analysis (default = 1000)"));
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker // Limit on the number of memdep results to process.
65*9880d681SAndroid Build Coastguard Worker static const unsigned int NumResultsLimit = 100;
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker /// This is a helper function that removes Val from 'Inst's set in ReverseMap.
68*9880d681SAndroid Build Coastguard Worker ///
69*9880d681SAndroid Build Coastguard Worker /// If the set becomes empty, remove Inst's entry.
70*9880d681SAndroid Build Coastguard Worker template <typename KeyTy>
71*9880d681SAndroid Build Coastguard Worker static void
RemoveFromReverseMap(DenseMap<Instruction *,SmallPtrSet<KeyTy,4>> & ReverseMap,Instruction * Inst,KeyTy Val)72*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(DenseMap<Instruction *, SmallPtrSet<KeyTy, 4>> &ReverseMap,
73*9880d681SAndroid Build Coastguard Worker Instruction *Inst, KeyTy Val) {
74*9880d681SAndroid Build Coastguard Worker typename DenseMap<Instruction *, SmallPtrSet<KeyTy, 4>>::iterator InstIt =
75*9880d681SAndroid Build Coastguard Worker ReverseMap.find(Inst);
76*9880d681SAndroid Build Coastguard Worker assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
77*9880d681SAndroid Build Coastguard Worker bool Found = InstIt->second.erase(Val);
78*9880d681SAndroid Build Coastguard Worker assert(Found && "Invalid reverse map!");
79*9880d681SAndroid Build Coastguard Worker (void)Found;
80*9880d681SAndroid Build Coastguard Worker if (InstIt->second.empty())
81*9880d681SAndroid Build Coastguard Worker ReverseMap.erase(InstIt);
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker /// If the given instruction references a specific memory location, fill in Loc
85*9880d681SAndroid Build Coastguard Worker /// with the details, otherwise set Loc.Ptr to null.
86*9880d681SAndroid Build Coastguard Worker ///
87*9880d681SAndroid Build Coastguard Worker /// Returns a ModRefInfo value describing the general behavior of the
88*9880d681SAndroid Build Coastguard Worker /// instruction.
GetLocation(const Instruction * Inst,MemoryLocation & Loc,const TargetLibraryInfo & TLI)89*9880d681SAndroid Build Coastguard Worker static ModRefInfo GetLocation(const Instruction *Inst, MemoryLocation &Loc,
90*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo &TLI) {
91*9880d681SAndroid Build Coastguard Worker if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
92*9880d681SAndroid Build Coastguard Worker if (LI->isUnordered()) {
93*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation::get(LI);
94*9880d681SAndroid Build Coastguard Worker return MRI_Ref;
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker if (LI->getOrdering() == AtomicOrdering::Monotonic) {
97*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation::get(LI);
98*9880d681SAndroid Build Coastguard Worker return MRI_ModRef;
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation();
101*9880d681SAndroid Build Coastguard Worker return MRI_ModRef;
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
105*9880d681SAndroid Build Coastguard Worker if (SI->isUnordered()) {
106*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation::get(SI);
107*9880d681SAndroid Build Coastguard Worker return MRI_Mod;
108*9880d681SAndroid Build Coastguard Worker }
109*9880d681SAndroid Build Coastguard Worker if (SI->getOrdering() == AtomicOrdering::Monotonic) {
110*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation::get(SI);
111*9880d681SAndroid Build Coastguard Worker return MRI_ModRef;
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation();
114*9880d681SAndroid Build Coastguard Worker return MRI_ModRef;
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
118*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation::get(V);
119*9880d681SAndroid Build Coastguard Worker return MRI_ModRef;
120*9880d681SAndroid Build Coastguard Worker }
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker if (const CallInst *CI = isFreeCall(Inst, &TLI)) {
123*9880d681SAndroid Build Coastguard Worker // calls to free() deallocate the entire structure
124*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation(CI->getArgOperand(0));
125*9880d681SAndroid Build Coastguard Worker return MRI_Mod;
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
129*9880d681SAndroid Build Coastguard Worker AAMDNodes AAInfo;
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker switch (II->getIntrinsicID()) {
132*9880d681SAndroid Build Coastguard Worker case Intrinsic::lifetime_start:
133*9880d681SAndroid Build Coastguard Worker case Intrinsic::lifetime_end:
134*9880d681SAndroid Build Coastguard Worker case Intrinsic::invariant_start:
135*9880d681SAndroid Build Coastguard Worker II->getAAMetadata(AAInfo);
136*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation(
137*9880d681SAndroid Build Coastguard Worker II->getArgOperand(1),
138*9880d681SAndroid Build Coastguard Worker cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AAInfo);
139*9880d681SAndroid Build Coastguard Worker // These intrinsics don't really modify the memory, but returning Mod
140*9880d681SAndroid Build Coastguard Worker // will allow them to be handled conservatively.
141*9880d681SAndroid Build Coastguard Worker return MRI_Mod;
142*9880d681SAndroid Build Coastguard Worker case Intrinsic::invariant_end:
143*9880d681SAndroid Build Coastguard Worker II->getAAMetadata(AAInfo);
144*9880d681SAndroid Build Coastguard Worker Loc = MemoryLocation(
145*9880d681SAndroid Build Coastguard Worker II->getArgOperand(2),
146*9880d681SAndroid Build Coastguard Worker cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AAInfo);
147*9880d681SAndroid Build Coastguard Worker // These intrinsics don't really modify the memory, but returning Mod
148*9880d681SAndroid Build Coastguard Worker // will allow them to be handled conservatively.
149*9880d681SAndroid Build Coastguard Worker return MRI_Mod;
150*9880d681SAndroid Build Coastguard Worker default:
151*9880d681SAndroid Build Coastguard Worker break;
152*9880d681SAndroid Build Coastguard Worker }
153*9880d681SAndroid Build Coastguard Worker }
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker // Otherwise, just do the coarse-grained thing that always works.
156*9880d681SAndroid Build Coastguard Worker if (Inst->mayWriteToMemory())
157*9880d681SAndroid Build Coastguard Worker return MRI_ModRef;
158*9880d681SAndroid Build Coastguard Worker if (Inst->mayReadFromMemory())
159*9880d681SAndroid Build Coastguard Worker return MRI_Ref;
160*9880d681SAndroid Build Coastguard Worker return MRI_NoModRef;
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker /// Private helper for finding the local dependencies of a call site.
getCallSiteDependencyFrom(CallSite CS,bool isReadOnlyCall,BasicBlock::iterator ScanIt,BasicBlock * BB)164*9880d681SAndroid Build Coastguard Worker MemDepResult MemoryDependenceResults::getCallSiteDependencyFrom(
165*9880d681SAndroid Build Coastguard Worker CallSite CS, bool isReadOnlyCall, BasicBlock::iterator ScanIt,
166*9880d681SAndroid Build Coastguard Worker BasicBlock *BB) {
167*9880d681SAndroid Build Coastguard Worker unsigned Limit = BlockScanLimit;
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker // Walk backwards through the block, looking for dependencies
170*9880d681SAndroid Build Coastguard Worker while (ScanIt != BB->begin()) {
171*9880d681SAndroid Build Coastguard Worker // Limit the amount of scanning we do so we don't end up with quadratic
172*9880d681SAndroid Build Coastguard Worker // running time on extreme testcases.
173*9880d681SAndroid Build Coastguard Worker --Limit;
174*9880d681SAndroid Build Coastguard Worker if (!Limit)
175*9880d681SAndroid Build Coastguard Worker return MemDepResult::getUnknown();
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker Instruction *Inst = &*--ScanIt;
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker // If this inst is a memory op, get the pointer it accessed
180*9880d681SAndroid Build Coastguard Worker MemoryLocation Loc;
181*9880d681SAndroid Build Coastguard Worker ModRefInfo MR = GetLocation(Inst, Loc, TLI);
182*9880d681SAndroid Build Coastguard Worker if (Loc.Ptr) {
183*9880d681SAndroid Build Coastguard Worker // A simple instruction.
184*9880d681SAndroid Build Coastguard Worker if (AA.getModRefInfo(CS, Loc) != MRI_NoModRef)
185*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
186*9880d681SAndroid Build Coastguard Worker continue;
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker if (auto InstCS = CallSite(Inst)) {
190*9880d681SAndroid Build Coastguard Worker // Debug intrinsics don't cause dependences.
191*9880d681SAndroid Build Coastguard Worker if (isa<DbgInfoIntrinsic>(Inst))
192*9880d681SAndroid Build Coastguard Worker continue;
193*9880d681SAndroid Build Coastguard Worker // If these two calls do not interfere, look past it.
194*9880d681SAndroid Build Coastguard Worker switch (AA.getModRefInfo(CS, InstCS)) {
195*9880d681SAndroid Build Coastguard Worker case MRI_NoModRef:
196*9880d681SAndroid Build Coastguard Worker // If the two calls are the same, return InstCS as a Def, so that
197*9880d681SAndroid Build Coastguard Worker // CS can be found redundant and eliminated.
198*9880d681SAndroid Build Coastguard Worker if (isReadOnlyCall && !(MR & MRI_Mod) &&
199*9880d681SAndroid Build Coastguard Worker CS.getInstruction()->isIdenticalToWhenDefined(Inst))
200*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(Inst);
201*9880d681SAndroid Build Coastguard Worker
202*9880d681SAndroid Build Coastguard Worker // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
203*9880d681SAndroid Build Coastguard Worker // keep scanning.
204*9880d681SAndroid Build Coastguard Worker continue;
205*9880d681SAndroid Build Coastguard Worker default:
206*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
207*9880d681SAndroid Build Coastguard Worker }
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker // If we could not obtain a pointer for the instruction and the instruction
211*9880d681SAndroid Build Coastguard Worker // touches memory then assume that this is a dependency.
212*9880d681SAndroid Build Coastguard Worker if (MR != MRI_NoModRef)
213*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
214*9880d681SAndroid Build Coastguard Worker }
215*9880d681SAndroid Build Coastguard Worker
216*9880d681SAndroid Build Coastguard Worker // No dependence found. If this is the entry block of the function, it is
217*9880d681SAndroid Build Coastguard Worker // unknown, otherwise it is non-local.
218*9880d681SAndroid Build Coastguard Worker if (BB != &BB->getParent()->getEntryBlock())
219*9880d681SAndroid Build Coastguard Worker return MemDepResult::getNonLocal();
220*9880d681SAndroid Build Coastguard Worker return MemDepResult::getNonFuncLocal();
221*9880d681SAndroid Build Coastguard Worker }
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker /// Return true if LI is a load that would fully overlap MemLoc if done as
224*9880d681SAndroid Build Coastguard Worker /// a wider legal integer load.
225*9880d681SAndroid Build Coastguard Worker ///
226*9880d681SAndroid Build Coastguard Worker /// MemLocBase, MemLocOffset are lazily computed here the first time the
227*9880d681SAndroid Build Coastguard Worker /// base/offs of memloc is needed.
isLoadLoadClobberIfExtendedToFullWidth(const MemoryLocation & MemLoc,const Value * & MemLocBase,int64_t & MemLocOffs,const LoadInst * LI)228*9880d681SAndroid Build Coastguard Worker static bool isLoadLoadClobberIfExtendedToFullWidth(const MemoryLocation &MemLoc,
229*9880d681SAndroid Build Coastguard Worker const Value *&MemLocBase,
230*9880d681SAndroid Build Coastguard Worker int64_t &MemLocOffs,
231*9880d681SAndroid Build Coastguard Worker const LoadInst *LI) {
232*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = LI->getModule()->getDataLayout();
233*9880d681SAndroid Build Coastguard Worker
234*9880d681SAndroid Build Coastguard Worker // If we haven't already computed the base/offset of MemLoc, do so now.
235*9880d681SAndroid Build Coastguard Worker if (!MemLocBase)
236*9880d681SAndroid Build Coastguard Worker MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, DL);
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker unsigned Size = MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
239*9880d681SAndroid Build Coastguard Worker MemLocBase, MemLocOffs, MemLoc.Size, LI);
240*9880d681SAndroid Build Coastguard Worker return Size != 0;
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker
getLoadLoadClobberFullWidthSize(const Value * MemLocBase,int64_t MemLocOffs,unsigned MemLocSize,const LoadInst * LI)243*9880d681SAndroid Build Coastguard Worker unsigned MemoryDependenceResults::getLoadLoadClobberFullWidthSize(
244*9880d681SAndroid Build Coastguard Worker const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize,
245*9880d681SAndroid Build Coastguard Worker const LoadInst *LI) {
246*9880d681SAndroid Build Coastguard Worker // We can only extend simple integer loads.
247*9880d681SAndroid Build Coastguard Worker if (!isa<IntegerType>(LI->getType()) || !LI->isSimple())
248*9880d681SAndroid Build Coastguard Worker return 0;
249*9880d681SAndroid Build Coastguard Worker
250*9880d681SAndroid Build Coastguard Worker // Load widening is hostile to ThreadSanitizer: it may cause false positives
251*9880d681SAndroid Build Coastguard Worker // or make the reports more cryptic (access sizes are wrong).
252*9880d681SAndroid Build Coastguard Worker if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread))
253*9880d681SAndroid Build Coastguard Worker return 0;
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = LI->getModule()->getDataLayout();
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker // Get the base of this load.
258*9880d681SAndroid Build Coastguard Worker int64_t LIOffs = 0;
259*9880d681SAndroid Build Coastguard Worker const Value *LIBase =
260*9880d681SAndroid Build Coastguard Worker GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, DL);
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker // If the two pointers are not based on the same pointer, we can't tell that
263*9880d681SAndroid Build Coastguard Worker // they are related.
264*9880d681SAndroid Build Coastguard Worker if (LIBase != MemLocBase)
265*9880d681SAndroid Build Coastguard Worker return 0;
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker // Okay, the two values are based on the same pointer, but returned as
268*9880d681SAndroid Build Coastguard Worker // no-alias. This happens when we have things like two byte loads at "P+1"
269*9880d681SAndroid Build Coastguard Worker // and "P+3". Check to see if increasing the size of the "LI" load up to its
270*9880d681SAndroid Build Coastguard Worker // alignment (or the largest native integer type) will allow us to load all
271*9880d681SAndroid Build Coastguard Worker // the bits required by MemLoc.
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker // If MemLoc is before LI, then no widening of LI will help us out.
274*9880d681SAndroid Build Coastguard Worker if (MemLocOffs < LIOffs)
275*9880d681SAndroid Build Coastguard Worker return 0;
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker // Get the alignment of the load in bytes. We assume that it is safe to load
278*9880d681SAndroid Build Coastguard Worker // any legal integer up to this size without a problem. For example, if we're
279*9880d681SAndroid Build Coastguard Worker // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can
280*9880d681SAndroid Build Coastguard Worker // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it
281*9880d681SAndroid Build Coastguard Worker // to i16.
282*9880d681SAndroid Build Coastguard Worker unsigned LoadAlign = LI->getAlignment();
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker int64_t MemLocEnd = MemLocOffs + MemLocSize;
285*9880d681SAndroid Build Coastguard Worker
286*9880d681SAndroid Build Coastguard Worker // If no amount of rounding up will let MemLoc fit into LI, then bail out.
287*9880d681SAndroid Build Coastguard Worker if (LIOffs + LoadAlign < MemLocEnd)
288*9880d681SAndroid Build Coastguard Worker return 0;
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker // This is the size of the load to try. Start with the next larger power of
291*9880d681SAndroid Build Coastguard Worker // two.
292*9880d681SAndroid Build Coastguard Worker unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits() / 8U;
293*9880d681SAndroid Build Coastguard Worker NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker while (1) {
296*9880d681SAndroid Build Coastguard Worker // If this load size is bigger than our known alignment or would not fit
297*9880d681SAndroid Build Coastguard Worker // into a native integer register, then we fail.
298*9880d681SAndroid Build Coastguard Worker if (NewLoadByteSize > LoadAlign ||
299*9880d681SAndroid Build Coastguard Worker !DL.fitsInLegalInteger(NewLoadByteSize * 8))
300*9880d681SAndroid Build Coastguard Worker return 0;
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard Worker if (LIOffs + NewLoadByteSize > MemLocEnd &&
303*9880d681SAndroid Build Coastguard Worker LI->getParent()->getParent()->hasFnAttribute(
304*9880d681SAndroid Build Coastguard Worker Attribute::SanitizeAddress))
305*9880d681SAndroid Build Coastguard Worker // We will be reading past the location accessed by the original program.
306*9880d681SAndroid Build Coastguard Worker // While this is safe in a regular build, Address Safety analysis tools
307*9880d681SAndroid Build Coastguard Worker // may start reporting false warnings. So, don't do widening.
308*9880d681SAndroid Build Coastguard Worker return 0;
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker // If a load of this width would include all of MemLoc, then we succeed.
311*9880d681SAndroid Build Coastguard Worker if (LIOffs + NewLoadByteSize >= MemLocEnd)
312*9880d681SAndroid Build Coastguard Worker return NewLoadByteSize;
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard Worker NewLoadByteSize <<= 1;
315*9880d681SAndroid Build Coastguard Worker }
316*9880d681SAndroid Build Coastguard Worker }
317*9880d681SAndroid Build Coastguard Worker
isVolatile(Instruction * Inst)318*9880d681SAndroid Build Coastguard Worker static bool isVolatile(Instruction *Inst) {
319*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
320*9880d681SAndroid Build Coastguard Worker return LI->isVolatile();
321*9880d681SAndroid Build Coastguard Worker else if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
322*9880d681SAndroid Build Coastguard Worker return SI->isVolatile();
323*9880d681SAndroid Build Coastguard Worker else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst))
324*9880d681SAndroid Build Coastguard Worker return AI->isVolatile();
325*9880d681SAndroid Build Coastguard Worker return false;
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker
getPointerDependencyFrom(const MemoryLocation & MemLoc,bool isLoad,BasicBlock::iterator ScanIt,BasicBlock * BB,Instruction * QueryInst)328*9880d681SAndroid Build Coastguard Worker MemDepResult MemoryDependenceResults::getPointerDependencyFrom(
329*9880d681SAndroid Build Coastguard Worker const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,
330*9880d681SAndroid Build Coastguard Worker BasicBlock *BB, Instruction *QueryInst) {
331*9880d681SAndroid Build Coastguard Worker
332*9880d681SAndroid Build Coastguard Worker if (QueryInst != nullptr) {
333*9880d681SAndroid Build Coastguard Worker if (auto *LI = dyn_cast<LoadInst>(QueryInst)) {
334*9880d681SAndroid Build Coastguard Worker MemDepResult invariantGroupDependency =
335*9880d681SAndroid Build Coastguard Worker getInvariantGroupPointerDependency(LI, BB);
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker if (invariantGroupDependency.isDef())
338*9880d681SAndroid Build Coastguard Worker return invariantGroupDependency;
339*9880d681SAndroid Build Coastguard Worker }
340*9880d681SAndroid Build Coastguard Worker }
341*9880d681SAndroid Build Coastguard Worker return getSimplePointerDependencyFrom(MemLoc, isLoad, ScanIt, BB, QueryInst);
342*9880d681SAndroid Build Coastguard Worker }
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard Worker MemDepResult
getInvariantGroupPointerDependency(LoadInst * LI,BasicBlock * BB)345*9880d681SAndroid Build Coastguard Worker MemoryDependenceResults::getInvariantGroupPointerDependency(LoadInst *LI,
346*9880d681SAndroid Build Coastguard Worker BasicBlock *BB) {
347*9880d681SAndroid Build Coastguard Worker Value *LoadOperand = LI->getPointerOperand();
348*9880d681SAndroid Build Coastguard Worker // It's is not safe to walk the use list of global value, because function
349*9880d681SAndroid Build Coastguard Worker // passes aren't allowed to look outside their functions.
350*9880d681SAndroid Build Coastguard Worker if (isa<GlobalValue>(LoadOperand))
351*9880d681SAndroid Build Coastguard Worker return MemDepResult::getUnknown();
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard Worker auto *InvariantGroupMD = LI->getMetadata(LLVMContext::MD_invariant_group);
354*9880d681SAndroid Build Coastguard Worker if (!InvariantGroupMD)
355*9880d681SAndroid Build Coastguard Worker return MemDepResult::getUnknown();
356*9880d681SAndroid Build Coastguard Worker
357*9880d681SAndroid Build Coastguard Worker MemDepResult Result = MemDepResult::getUnknown();
358*9880d681SAndroid Build Coastguard Worker llvm::SmallSet<Value *, 14> Seen;
359*9880d681SAndroid Build Coastguard Worker // Queue to process all pointers that are equivalent to load operand.
360*9880d681SAndroid Build Coastguard Worker llvm::SmallVector<Value *, 8> LoadOperandsQueue;
361*9880d681SAndroid Build Coastguard Worker LoadOperandsQueue.push_back(LoadOperand);
362*9880d681SAndroid Build Coastguard Worker while (!LoadOperandsQueue.empty()) {
363*9880d681SAndroid Build Coastguard Worker Value *Ptr = LoadOperandsQueue.pop_back_val();
364*9880d681SAndroid Build Coastguard Worker if (isa<GlobalValue>(Ptr))
365*9880d681SAndroid Build Coastguard Worker continue;
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker if (auto *BCI = dyn_cast<BitCastInst>(Ptr)) {
368*9880d681SAndroid Build Coastguard Worker if (Seen.insert(BCI->getOperand(0)).second) {
369*9880d681SAndroid Build Coastguard Worker LoadOperandsQueue.push_back(BCI->getOperand(0));
370*9880d681SAndroid Build Coastguard Worker }
371*9880d681SAndroid Build Coastguard Worker }
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker for (Use &Us : Ptr->uses()) {
374*9880d681SAndroid Build Coastguard Worker auto *U = dyn_cast<Instruction>(Us.getUser());
375*9880d681SAndroid Build Coastguard Worker if (!U || U == LI || !DT.dominates(U, LI))
376*9880d681SAndroid Build Coastguard Worker continue;
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker if (auto *BCI = dyn_cast<BitCastInst>(U)) {
379*9880d681SAndroid Build Coastguard Worker if (Seen.insert(BCI).second) {
380*9880d681SAndroid Build Coastguard Worker LoadOperandsQueue.push_back(BCI);
381*9880d681SAndroid Build Coastguard Worker }
382*9880d681SAndroid Build Coastguard Worker continue;
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker // If we hit load/store with the same invariant.group metadata (and the
385*9880d681SAndroid Build Coastguard Worker // same pointer operand) we can assume that value pointed by pointer
386*9880d681SAndroid Build Coastguard Worker // operand didn't change.
387*9880d681SAndroid Build Coastguard Worker if ((isa<LoadInst>(U) || isa<StoreInst>(U)) && U->getParent() == BB &&
388*9880d681SAndroid Build Coastguard Worker U->getMetadata(LLVMContext::MD_invariant_group) == InvariantGroupMD)
389*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(U);
390*9880d681SAndroid Build Coastguard Worker }
391*9880d681SAndroid Build Coastguard Worker }
392*9880d681SAndroid Build Coastguard Worker return Result;
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker
getSimplePointerDependencyFrom(const MemoryLocation & MemLoc,bool isLoad,BasicBlock::iterator ScanIt,BasicBlock * BB,Instruction * QueryInst)395*9880d681SAndroid Build Coastguard Worker MemDepResult MemoryDependenceResults::getSimplePointerDependencyFrom(
396*9880d681SAndroid Build Coastguard Worker const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt,
397*9880d681SAndroid Build Coastguard Worker BasicBlock *BB, Instruction *QueryInst) {
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker const Value *MemLocBase = nullptr;
400*9880d681SAndroid Build Coastguard Worker int64_t MemLocOffset = 0;
401*9880d681SAndroid Build Coastguard Worker unsigned Limit = BlockScanLimit;
402*9880d681SAndroid Build Coastguard Worker bool isInvariantLoad = false;
403*9880d681SAndroid Build Coastguard Worker
404*9880d681SAndroid Build Coastguard Worker // We must be careful with atomic accesses, as they may allow another thread
405*9880d681SAndroid Build Coastguard Worker // to touch this location, clobbering it. We are conservative: if the
406*9880d681SAndroid Build Coastguard Worker // QueryInst is not a simple (non-atomic) memory access, we automatically
407*9880d681SAndroid Build Coastguard Worker // return getClobber.
408*9880d681SAndroid Build Coastguard Worker // If it is simple, we know based on the results of
409*9880d681SAndroid Build Coastguard Worker // "Compiler testing via a theory of sound optimisations in the C11/C++11
410*9880d681SAndroid Build Coastguard Worker // memory model" in PLDI 2013, that a non-atomic location can only be
411*9880d681SAndroid Build Coastguard Worker // clobbered between a pair of a release and an acquire action, with no
412*9880d681SAndroid Build Coastguard Worker // access to the location in between.
413*9880d681SAndroid Build Coastguard Worker // Here is an example for giving the general intuition behind this rule.
414*9880d681SAndroid Build Coastguard Worker // In the following code:
415*9880d681SAndroid Build Coastguard Worker // store x 0;
416*9880d681SAndroid Build Coastguard Worker // release action; [1]
417*9880d681SAndroid Build Coastguard Worker // acquire action; [4]
418*9880d681SAndroid Build Coastguard Worker // %val = load x;
419*9880d681SAndroid Build Coastguard Worker // It is unsafe to replace %val by 0 because another thread may be running:
420*9880d681SAndroid Build Coastguard Worker // acquire action; [2]
421*9880d681SAndroid Build Coastguard Worker // store x 42;
422*9880d681SAndroid Build Coastguard Worker // release action; [3]
423*9880d681SAndroid Build Coastguard Worker // with synchronization from 1 to 2 and from 3 to 4, resulting in %val
424*9880d681SAndroid Build Coastguard Worker // being 42. A key property of this program however is that if either
425*9880d681SAndroid Build Coastguard Worker // 1 or 4 were missing, there would be a race between the store of 42
426*9880d681SAndroid Build Coastguard Worker // either the store of 0 or the load (making the whole program racy).
427*9880d681SAndroid Build Coastguard Worker // The paper mentioned above shows that the same property is respected
428*9880d681SAndroid Build Coastguard Worker // by every program that can detect any optimization of that kind: either
429*9880d681SAndroid Build Coastguard Worker // it is racy (undefined) or there is a release followed by an acquire
430*9880d681SAndroid Build Coastguard Worker // between the pair of accesses under consideration.
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker // If the load is invariant, we "know" that it doesn't alias *any* write. We
433*9880d681SAndroid Build Coastguard Worker // do want to respect mustalias results since defs are useful for value
434*9880d681SAndroid Build Coastguard Worker // forwarding, but any mayalias write can be assumed to be noalias.
435*9880d681SAndroid Build Coastguard Worker // Arguably, this logic should be pushed inside AliasAnalysis itself.
436*9880d681SAndroid Build Coastguard Worker if (isLoad && QueryInst) {
437*9880d681SAndroid Build Coastguard Worker LoadInst *LI = dyn_cast<LoadInst>(QueryInst);
438*9880d681SAndroid Build Coastguard Worker if (LI && LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr)
439*9880d681SAndroid Build Coastguard Worker isInvariantLoad = true;
440*9880d681SAndroid Build Coastguard Worker }
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = BB->getModule()->getDataLayout();
443*9880d681SAndroid Build Coastguard Worker
444*9880d681SAndroid Build Coastguard Worker // Create a numbered basic block to lazily compute and cache instruction
445*9880d681SAndroid Build Coastguard Worker // positions inside a BB. This is used to provide fast queries for relative
446*9880d681SAndroid Build Coastguard Worker // position between two instructions in a BB and can be used by
447*9880d681SAndroid Build Coastguard Worker // AliasAnalysis::callCapturesBefore.
448*9880d681SAndroid Build Coastguard Worker OrderedBasicBlock OBB(BB);
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker // Return "true" if and only if the instruction I is either a non-simple
451*9880d681SAndroid Build Coastguard Worker // load or a non-simple store.
452*9880d681SAndroid Build Coastguard Worker auto isNonSimpleLoadOrStore = [](Instruction *I) -> bool {
453*9880d681SAndroid Build Coastguard Worker if (auto *LI = dyn_cast<LoadInst>(I))
454*9880d681SAndroid Build Coastguard Worker return !LI->isSimple();
455*9880d681SAndroid Build Coastguard Worker if (auto *SI = dyn_cast<StoreInst>(I))
456*9880d681SAndroid Build Coastguard Worker return !SI->isSimple();
457*9880d681SAndroid Build Coastguard Worker return false;
458*9880d681SAndroid Build Coastguard Worker };
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker // Return "true" if I is not a load and not a store, but it does access
461*9880d681SAndroid Build Coastguard Worker // memory.
462*9880d681SAndroid Build Coastguard Worker auto isOtherMemAccess = [](Instruction *I) -> bool {
463*9880d681SAndroid Build Coastguard Worker return !isa<LoadInst>(I) && !isa<StoreInst>(I) && I->mayReadOrWriteMemory();
464*9880d681SAndroid Build Coastguard Worker };
465*9880d681SAndroid Build Coastguard Worker
466*9880d681SAndroid Build Coastguard Worker // Walk backwards through the basic block, looking for dependencies.
467*9880d681SAndroid Build Coastguard Worker while (ScanIt != BB->begin()) {
468*9880d681SAndroid Build Coastguard Worker Instruction *Inst = &*--ScanIt;
469*9880d681SAndroid Build Coastguard Worker
470*9880d681SAndroid Build Coastguard Worker if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
471*9880d681SAndroid Build Coastguard Worker // Debug intrinsics don't (and can't) cause dependencies.
472*9880d681SAndroid Build Coastguard Worker if (isa<DbgInfoIntrinsic>(II))
473*9880d681SAndroid Build Coastguard Worker continue;
474*9880d681SAndroid Build Coastguard Worker
475*9880d681SAndroid Build Coastguard Worker // Limit the amount of scanning we do so we don't end up with quadratic
476*9880d681SAndroid Build Coastguard Worker // running time on extreme testcases.
477*9880d681SAndroid Build Coastguard Worker --Limit;
478*9880d681SAndroid Build Coastguard Worker if (!Limit)
479*9880d681SAndroid Build Coastguard Worker return MemDepResult::getUnknown();
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard Worker if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
482*9880d681SAndroid Build Coastguard Worker // If we reach a lifetime begin or end marker, then the query ends here
483*9880d681SAndroid Build Coastguard Worker // because the value is undefined.
484*9880d681SAndroid Build Coastguard Worker if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
485*9880d681SAndroid Build Coastguard Worker // FIXME: This only considers queries directly on the invariant-tagged
486*9880d681SAndroid Build Coastguard Worker // pointer, not on query pointers that are indexed off of them. It'd
487*9880d681SAndroid Build Coastguard Worker // be nice to handle that at some point (the right approach is to use
488*9880d681SAndroid Build Coastguard Worker // GetPointerBaseWithConstantOffset).
489*9880d681SAndroid Build Coastguard Worker if (AA.isMustAlias(MemoryLocation(II->getArgOperand(1)), MemLoc))
490*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(II);
491*9880d681SAndroid Build Coastguard Worker continue;
492*9880d681SAndroid Build Coastguard Worker }
493*9880d681SAndroid Build Coastguard Worker }
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard Worker // Values depend on loads if the pointers are must aliased. This means
496*9880d681SAndroid Build Coastguard Worker // that a load depends on another must aliased load from the same value.
497*9880d681SAndroid Build Coastguard Worker // One exception is atomic loads: a value can depend on an atomic load that
498*9880d681SAndroid Build Coastguard Worker // it does not alias with when this atomic load indicates that another
499*9880d681SAndroid Build Coastguard Worker // thread may be accessing the location.
500*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker // While volatile access cannot be eliminated, they do not have to clobber
503*9880d681SAndroid Build Coastguard Worker // non-aliasing locations, as normal accesses, for example, can be safely
504*9880d681SAndroid Build Coastguard Worker // reordered with volatile accesses.
505*9880d681SAndroid Build Coastguard Worker if (LI->isVolatile()) {
506*9880d681SAndroid Build Coastguard Worker if (!QueryInst)
507*9880d681SAndroid Build Coastguard Worker // Original QueryInst *may* be volatile
508*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(LI);
509*9880d681SAndroid Build Coastguard Worker if (isVolatile(QueryInst))
510*9880d681SAndroid Build Coastguard Worker // Ordering required if QueryInst is itself volatile
511*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(LI);
512*9880d681SAndroid Build Coastguard Worker // Otherwise, volatile doesn't imply any special ordering
513*9880d681SAndroid Build Coastguard Worker }
514*9880d681SAndroid Build Coastguard Worker
515*9880d681SAndroid Build Coastguard Worker // Atomic loads have complications involved.
516*9880d681SAndroid Build Coastguard Worker // A Monotonic (or higher) load is OK if the query inst is itself not
517*9880d681SAndroid Build Coastguard Worker // atomic.
518*9880d681SAndroid Build Coastguard Worker // FIXME: This is overly conservative.
519*9880d681SAndroid Build Coastguard Worker if (LI->isAtomic() && isStrongerThanUnordered(LI->getOrdering())) {
520*9880d681SAndroid Build Coastguard Worker if (!QueryInst || isNonSimpleLoadOrStore(QueryInst) ||
521*9880d681SAndroid Build Coastguard Worker isOtherMemAccess(QueryInst))
522*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(LI);
523*9880d681SAndroid Build Coastguard Worker if (LI->getOrdering() != AtomicOrdering::Monotonic)
524*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(LI);
525*9880d681SAndroid Build Coastguard Worker }
526*9880d681SAndroid Build Coastguard Worker
527*9880d681SAndroid Build Coastguard Worker MemoryLocation LoadLoc = MemoryLocation::get(LI);
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker // If we found a pointer, check if it could be the same as our pointer.
530*9880d681SAndroid Build Coastguard Worker AliasResult R = AA.alias(LoadLoc, MemLoc);
531*9880d681SAndroid Build Coastguard Worker
532*9880d681SAndroid Build Coastguard Worker if (isLoad) {
533*9880d681SAndroid Build Coastguard Worker if (R == NoAlias) {
534*9880d681SAndroid Build Coastguard Worker // If this is an over-aligned integer load (for example,
535*9880d681SAndroid Build Coastguard Worker // "load i8* %P, align 4") see if it would obviously overlap with the
536*9880d681SAndroid Build Coastguard Worker // queried location if widened to a larger load (e.g. if the queried
537*9880d681SAndroid Build Coastguard Worker // location is 1 byte at P+1). If so, return it as a load/load
538*9880d681SAndroid Build Coastguard Worker // clobber result, allowing the client to decide to widen the load if
539*9880d681SAndroid Build Coastguard Worker // it wants to.
540*9880d681SAndroid Build Coastguard Worker if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) {
541*9880d681SAndroid Build Coastguard Worker if (LI->getAlignment() * 8 > ITy->getPrimitiveSizeInBits() &&
542*9880d681SAndroid Build Coastguard Worker isLoadLoadClobberIfExtendedToFullWidth(MemLoc, MemLocBase,
543*9880d681SAndroid Build Coastguard Worker MemLocOffset, LI))
544*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
545*9880d681SAndroid Build Coastguard Worker }
546*9880d681SAndroid Build Coastguard Worker continue;
547*9880d681SAndroid Build Coastguard Worker }
548*9880d681SAndroid Build Coastguard Worker
549*9880d681SAndroid Build Coastguard Worker // Must aliased loads are defs of each other.
550*9880d681SAndroid Build Coastguard Worker if (R == MustAlias)
551*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(Inst);
552*9880d681SAndroid Build Coastguard Worker
553*9880d681SAndroid Build Coastguard Worker #if 0 // FIXME: Temporarily disabled. GVN is cleverly rewriting loads
554*9880d681SAndroid Build Coastguard Worker // in terms of clobbering loads, but since it does this by looking
555*9880d681SAndroid Build Coastguard Worker // at the clobbering load directly, it doesn't know about any
556*9880d681SAndroid Build Coastguard Worker // phi translation that may have happened along the way.
557*9880d681SAndroid Build Coastguard Worker
558*9880d681SAndroid Build Coastguard Worker // If we have a partial alias, then return this as a clobber for the
559*9880d681SAndroid Build Coastguard Worker // client to handle.
560*9880d681SAndroid Build Coastguard Worker if (R == PartialAlias)
561*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
562*9880d681SAndroid Build Coastguard Worker #endif
563*9880d681SAndroid Build Coastguard Worker
564*9880d681SAndroid Build Coastguard Worker // Random may-alias loads don't depend on each other without a
565*9880d681SAndroid Build Coastguard Worker // dependence.
566*9880d681SAndroid Build Coastguard Worker continue;
567*9880d681SAndroid Build Coastguard Worker }
568*9880d681SAndroid Build Coastguard Worker
569*9880d681SAndroid Build Coastguard Worker // Stores don't depend on other no-aliased accesses.
570*9880d681SAndroid Build Coastguard Worker if (R == NoAlias)
571*9880d681SAndroid Build Coastguard Worker continue;
572*9880d681SAndroid Build Coastguard Worker
573*9880d681SAndroid Build Coastguard Worker // Stores don't alias loads from read-only memory.
574*9880d681SAndroid Build Coastguard Worker if (AA.pointsToConstantMemory(LoadLoc))
575*9880d681SAndroid Build Coastguard Worker continue;
576*9880d681SAndroid Build Coastguard Worker
577*9880d681SAndroid Build Coastguard Worker // Stores depend on may/must aliased loads.
578*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(Inst);
579*9880d681SAndroid Build Coastguard Worker }
580*9880d681SAndroid Build Coastguard Worker
581*9880d681SAndroid Build Coastguard Worker if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
582*9880d681SAndroid Build Coastguard Worker // Atomic stores have complications involved.
583*9880d681SAndroid Build Coastguard Worker // A Monotonic store is OK if the query inst is itself not atomic.
584*9880d681SAndroid Build Coastguard Worker // FIXME: This is overly conservative.
585*9880d681SAndroid Build Coastguard Worker if (!SI->isUnordered() && SI->isAtomic()) {
586*9880d681SAndroid Build Coastguard Worker if (!QueryInst || isNonSimpleLoadOrStore(QueryInst) ||
587*9880d681SAndroid Build Coastguard Worker isOtherMemAccess(QueryInst))
588*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(SI);
589*9880d681SAndroid Build Coastguard Worker if (SI->getOrdering() != AtomicOrdering::Monotonic)
590*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(SI);
591*9880d681SAndroid Build Coastguard Worker }
592*9880d681SAndroid Build Coastguard Worker
593*9880d681SAndroid Build Coastguard Worker // FIXME: this is overly conservative.
594*9880d681SAndroid Build Coastguard Worker // While volatile access cannot be eliminated, they do not have to clobber
595*9880d681SAndroid Build Coastguard Worker // non-aliasing locations, as normal accesses can for example be reordered
596*9880d681SAndroid Build Coastguard Worker // with volatile accesses.
597*9880d681SAndroid Build Coastguard Worker if (SI->isVolatile())
598*9880d681SAndroid Build Coastguard Worker if (!QueryInst || isNonSimpleLoadOrStore(QueryInst) ||
599*9880d681SAndroid Build Coastguard Worker isOtherMemAccess(QueryInst))
600*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(SI);
601*9880d681SAndroid Build Coastguard Worker
602*9880d681SAndroid Build Coastguard Worker // If alias analysis can tell that this store is guaranteed to not modify
603*9880d681SAndroid Build Coastguard Worker // the query pointer, ignore it. Use getModRefInfo to handle cases where
604*9880d681SAndroid Build Coastguard Worker // the query pointer points to constant memory etc.
605*9880d681SAndroid Build Coastguard Worker if (AA.getModRefInfo(SI, MemLoc) == MRI_NoModRef)
606*9880d681SAndroid Build Coastguard Worker continue;
607*9880d681SAndroid Build Coastguard Worker
608*9880d681SAndroid Build Coastguard Worker // Ok, this store might clobber the query pointer. Check to see if it is
609*9880d681SAndroid Build Coastguard Worker // a must alias: in this case, we want to return this as a def.
610*9880d681SAndroid Build Coastguard Worker MemoryLocation StoreLoc = MemoryLocation::get(SI);
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker // If we found a pointer, check if it could be the same as our pointer.
613*9880d681SAndroid Build Coastguard Worker AliasResult R = AA.alias(StoreLoc, MemLoc);
614*9880d681SAndroid Build Coastguard Worker
615*9880d681SAndroid Build Coastguard Worker if (R == NoAlias)
616*9880d681SAndroid Build Coastguard Worker continue;
617*9880d681SAndroid Build Coastguard Worker if (R == MustAlias)
618*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(Inst);
619*9880d681SAndroid Build Coastguard Worker if (isInvariantLoad)
620*9880d681SAndroid Build Coastguard Worker continue;
621*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
622*9880d681SAndroid Build Coastguard Worker }
623*9880d681SAndroid Build Coastguard Worker
624*9880d681SAndroid Build Coastguard Worker // If this is an allocation, and if we know that the accessed pointer is to
625*9880d681SAndroid Build Coastguard Worker // the allocation, return Def. This means that there is no dependence and
626*9880d681SAndroid Build Coastguard Worker // the access can be optimized based on that. For example, a load could
627*9880d681SAndroid Build Coastguard Worker // turn into undef. Note that we can bypass the allocation itself when
628*9880d681SAndroid Build Coastguard Worker // looking for a clobber in many cases; that's an alias property and is
629*9880d681SAndroid Build Coastguard Worker // handled by BasicAA.
630*9880d681SAndroid Build Coastguard Worker if (isa<AllocaInst>(Inst) || isNoAliasFn(Inst, &TLI)) {
631*9880d681SAndroid Build Coastguard Worker const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL);
632*9880d681SAndroid Build Coastguard Worker if (AccessPtr == Inst || AA.isMustAlias(Inst, AccessPtr))
633*9880d681SAndroid Build Coastguard Worker return MemDepResult::getDef(Inst);
634*9880d681SAndroid Build Coastguard Worker }
635*9880d681SAndroid Build Coastguard Worker
636*9880d681SAndroid Build Coastguard Worker if (isInvariantLoad)
637*9880d681SAndroid Build Coastguard Worker continue;
638*9880d681SAndroid Build Coastguard Worker
639*9880d681SAndroid Build Coastguard Worker // A release fence requires that all stores complete before it, but does
640*9880d681SAndroid Build Coastguard Worker // not prevent the reordering of following loads or stores 'before' the
641*9880d681SAndroid Build Coastguard Worker // fence. As a result, we look past it when finding a dependency for
642*9880d681SAndroid Build Coastguard Worker // loads. DSE uses this to find preceeding stores to delete and thus we
643*9880d681SAndroid Build Coastguard Worker // can't bypass the fence if the query instruction is a store.
644*9880d681SAndroid Build Coastguard Worker if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
645*9880d681SAndroid Build Coastguard Worker if (isLoad && FI->getOrdering() == AtomicOrdering::Release)
646*9880d681SAndroid Build Coastguard Worker continue;
647*9880d681SAndroid Build Coastguard Worker
648*9880d681SAndroid Build Coastguard Worker // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
649*9880d681SAndroid Build Coastguard Worker ModRefInfo MR = AA.getModRefInfo(Inst, MemLoc);
650*9880d681SAndroid Build Coastguard Worker // If necessary, perform additional analysis.
651*9880d681SAndroid Build Coastguard Worker if (MR == MRI_ModRef)
652*9880d681SAndroid Build Coastguard Worker MR = AA.callCapturesBefore(Inst, MemLoc, &DT, &OBB);
653*9880d681SAndroid Build Coastguard Worker switch (MR) {
654*9880d681SAndroid Build Coastguard Worker case MRI_NoModRef:
655*9880d681SAndroid Build Coastguard Worker // If the call has no effect on the queried pointer, just ignore it.
656*9880d681SAndroid Build Coastguard Worker continue;
657*9880d681SAndroid Build Coastguard Worker case MRI_Mod:
658*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
659*9880d681SAndroid Build Coastguard Worker case MRI_Ref:
660*9880d681SAndroid Build Coastguard Worker // If the call is known to never store to the pointer, and if this is a
661*9880d681SAndroid Build Coastguard Worker // load query, we can safely ignore it (scan past it).
662*9880d681SAndroid Build Coastguard Worker if (isLoad)
663*9880d681SAndroid Build Coastguard Worker continue;
664*9880d681SAndroid Build Coastguard Worker default:
665*9880d681SAndroid Build Coastguard Worker // Otherwise, there is a potential dependence. Return a clobber.
666*9880d681SAndroid Build Coastguard Worker return MemDepResult::getClobber(Inst);
667*9880d681SAndroid Build Coastguard Worker }
668*9880d681SAndroid Build Coastguard Worker }
669*9880d681SAndroid Build Coastguard Worker
670*9880d681SAndroid Build Coastguard Worker // No dependence found. If this is the entry block of the function, it is
671*9880d681SAndroid Build Coastguard Worker // unknown, otherwise it is non-local.
672*9880d681SAndroid Build Coastguard Worker if (BB != &BB->getParent()->getEntryBlock())
673*9880d681SAndroid Build Coastguard Worker return MemDepResult::getNonLocal();
674*9880d681SAndroid Build Coastguard Worker return MemDepResult::getNonFuncLocal();
675*9880d681SAndroid Build Coastguard Worker }
676*9880d681SAndroid Build Coastguard Worker
getDependency(Instruction * QueryInst)677*9880d681SAndroid Build Coastguard Worker MemDepResult MemoryDependenceResults::getDependency(Instruction *QueryInst) {
678*9880d681SAndroid Build Coastguard Worker Instruction *ScanPos = QueryInst;
679*9880d681SAndroid Build Coastguard Worker
680*9880d681SAndroid Build Coastguard Worker // Check for a cached result
681*9880d681SAndroid Build Coastguard Worker MemDepResult &LocalCache = LocalDeps[QueryInst];
682*9880d681SAndroid Build Coastguard Worker
683*9880d681SAndroid Build Coastguard Worker // If the cached entry is non-dirty, just return it. Note that this depends
684*9880d681SAndroid Build Coastguard Worker // on MemDepResult's default constructing to 'dirty'.
685*9880d681SAndroid Build Coastguard Worker if (!LocalCache.isDirty())
686*9880d681SAndroid Build Coastguard Worker return LocalCache;
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard Worker // Otherwise, if we have a dirty entry, we know we can start the scan at that
689*9880d681SAndroid Build Coastguard Worker // instruction, which may save us some work.
690*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = LocalCache.getInst()) {
691*9880d681SAndroid Build Coastguard Worker ScanPos = Inst;
692*9880d681SAndroid Build Coastguard Worker
693*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
694*9880d681SAndroid Build Coastguard Worker }
695*9880d681SAndroid Build Coastguard Worker
696*9880d681SAndroid Build Coastguard Worker BasicBlock *QueryParent = QueryInst->getParent();
697*9880d681SAndroid Build Coastguard Worker
698*9880d681SAndroid Build Coastguard Worker // Do the scan.
699*9880d681SAndroid Build Coastguard Worker if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
700*9880d681SAndroid Build Coastguard Worker // No dependence found. If this is the entry block of the function, it is
701*9880d681SAndroid Build Coastguard Worker // unknown, otherwise it is non-local.
702*9880d681SAndroid Build Coastguard Worker if (QueryParent != &QueryParent->getParent()->getEntryBlock())
703*9880d681SAndroid Build Coastguard Worker LocalCache = MemDepResult::getNonLocal();
704*9880d681SAndroid Build Coastguard Worker else
705*9880d681SAndroid Build Coastguard Worker LocalCache = MemDepResult::getNonFuncLocal();
706*9880d681SAndroid Build Coastguard Worker } else {
707*9880d681SAndroid Build Coastguard Worker MemoryLocation MemLoc;
708*9880d681SAndroid Build Coastguard Worker ModRefInfo MR = GetLocation(QueryInst, MemLoc, TLI);
709*9880d681SAndroid Build Coastguard Worker if (MemLoc.Ptr) {
710*9880d681SAndroid Build Coastguard Worker // If we can do a pointer scan, make it happen.
711*9880d681SAndroid Build Coastguard Worker bool isLoad = !(MR & MRI_Mod);
712*9880d681SAndroid Build Coastguard Worker if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst))
713*9880d681SAndroid Build Coastguard Worker isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start;
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Worker LocalCache = getPointerDependencyFrom(
716*9880d681SAndroid Build Coastguard Worker MemLoc, isLoad, ScanPos->getIterator(), QueryParent, QueryInst);
717*9880d681SAndroid Build Coastguard Worker } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
718*9880d681SAndroid Build Coastguard Worker CallSite QueryCS(QueryInst);
719*9880d681SAndroid Build Coastguard Worker bool isReadOnly = AA.onlyReadsMemory(QueryCS);
720*9880d681SAndroid Build Coastguard Worker LocalCache = getCallSiteDependencyFrom(
721*9880d681SAndroid Build Coastguard Worker QueryCS, isReadOnly, ScanPos->getIterator(), QueryParent);
722*9880d681SAndroid Build Coastguard Worker } else
723*9880d681SAndroid Build Coastguard Worker // Non-memory instruction.
724*9880d681SAndroid Build Coastguard Worker LocalCache = MemDepResult::getUnknown();
725*9880d681SAndroid Build Coastguard Worker }
726*9880d681SAndroid Build Coastguard Worker
727*9880d681SAndroid Build Coastguard Worker // Remember the result!
728*9880d681SAndroid Build Coastguard Worker if (Instruction *I = LocalCache.getInst())
729*9880d681SAndroid Build Coastguard Worker ReverseLocalDeps[I].insert(QueryInst);
730*9880d681SAndroid Build Coastguard Worker
731*9880d681SAndroid Build Coastguard Worker return LocalCache;
732*9880d681SAndroid Build Coastguard Worker }
733*9880d681SAndroid Build Coastguard Worker
734*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
735*9880d681SAndroid Build Coastguard Worker /// This method is used when -debug is specified to verify that cache arrays
736*9880d681SAndroid Build Coastguard Worker /// are properly kept sorted.
AssertSorted(MemoryDependenceResults::NonLocalDepInfo & Cache,int Count=-1)737*9880d681SAndroid Build Coastguard Worker static void AssertSorted(MemoryDependenceResults::NonLocalDepInfo &Cache,
738*9880d681SAndroid Build Coastguard Worker int Count = -1) {
739*9880d681SAndroid Build Coastguard Worker if (Count == -1)
740*9880d681SAndroid Build Coastguard Worker Count = Cache.size();
741*9880d681SAndroid Build Coastguard Worker assert(std::is_sorted(Cache.begin(), Cache.begin() + Count) &&
742*9880d681SAndroid Build Coastguard Worker "Cache isn't sorted!");
743*9880d681SAndroid Build Coastguard Worker }
744*9880d681SAndroid Build Coastguard Worker #endif
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker const MemoryDependenceResults::NonLocalDepInfo &
getNonLocalCallDependency(CallSite QueryCS)747*9880d681SAndroid Build Coastguard Worker MemoryDependenceResults::getNonLocalCallDependency(CallSite QueryCS) {
748*9880d681SAndroid Build Coastguard Worker assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
749*9880d681SAndroid Build Coastguard Worker "getNonLocalCallDependency should only be used on calls with "
750*9880d681SAndroid Build Coastguard Worker "non-local deps!");
751*9880d681SAndroid Build Coastguard Worker PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
752*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo &Cache = CacheP.first;
753*9880d681SAndroid Build Coastguard Worker
754*9880d681SAndroid Build Coastguard Worker // This is the set of blocks that need to be recomputed. In the cached case,
755*9880d681SAndroid Build Coastguard Worker // this can happen due to instructions being deleted etc. In the uncached
756*9880d681SAndroid Build Coastguard Worker // case, this starts out as the set of predecessors we care about.
757*9880d681SAndroid Build Coastguard Worker SmallVector<BasicBlock *, 32> DirtyBlocks;
758*9880d681SAndroid Build Coastguard Worker
759*9880d681SAndroid Build Coastguard Worker if (!Cache.empty()) {
760*9880d681SAndroid Build Coastguard Worker // Okay, we have a cache entry. If we know it is not dirty, just return it
761*9880d681SAndroid Build Coastguard Worker // with no computation.
762*9880d681SAndroid Build Coastguard Worker if (!CacheP.second) {
763*9880d681SAndroid Build Coastguard Worker ++NumCacheNonLocal;
764*9880d681SAndroid Build Coastguard Worker return Cache;
765*9880d681SAndroid Build Coastguard Worker }
766*9880d681SAndroid Build Coastguard Worker
767*9880d681SAndroid Build Coastguard Worker // If we already have a partially computed set of results, scan them to
768*9880d681SAndroid Build Coastguard Worker // determine what is dirty, seeding our initial DirtyBlocks worklist.
769*9880d681SAndroid Build Coastguard Worker for (auto &Entry : Cache)
770*9880d681SAndroid Build Coastguard Worker if (Entry.getResult().isDirty())
771*9880d681SAndroid Build Coastguard Worker DirtyBlocks.push_back(Entry.getBB());
772*9880d681SAndroid Build Coastguard Worker
773*9880d681SAndroid Build Coastguard Worker // Sort the cache so that we can do fast binary search lookups below.
774*9880d681SAndroid Build Coastguard Worker std::sort(Cache.begin(), Cache.end());
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard Worker ++NumCacheDirtyNonLocal;
777*9880d681SAndroid Build Coastguard Worker // cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
778*9880d681SAndroid Build Coastguard Worker // << Cache.size() << " cached: " << *QueryInst;
779*9880d681SAndroid Build Coastguard Worker } else {
780*9880d681SAndroid Build Coastguard Worker // Seed DirtyBlocks with each of the preds of QueryInst's block.
781*9880d681SAndroid Build Coastguard Worker BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
782*9880d681SAndroid Build Coastguard Worker for (BasicBlock *Pred : PredCache.get(QueryBB))
783*9880d681SAndroid Build Coastguard Worker DirtyBlocks.push_back(Pred);
784*9880d681SAndroid Build Coastguard Worker ++NumUncacheNonLocal;
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker
787*9880d681SAndroid Build Coastguard Worker // isReadonlyCall - If this is a read-only call, we can be more aggressive.
788*9880d681SAndroid Build Coastguard Worker bool isReadonlyCall = AA.onlyReadsMemory(QueryCS);
789*9880d681SAndroid Build Coastguard Worker
790*9880d681SAndroid Build Coastguard Worker SmallPtrSet<BasicBlock *, 32> Visited;
791*9880d681SAndroid Build Coastguard Worker
792*9880d681SAndroid Build Coastguard Worker unsigned NumSortedEntries = Cache.size();
793*9880d681SAndroid Build Coastguard Worker DEBUG(AssertSorted(Cache));
794*9880d681SAndroid Build Coastguard Worker
795*9880d681SAndroid Build Coastguard Worker // Iterate while we still have blocks to update.
796*9880d681SAndroid Build Coastguard Worker while (!DirtyBlocks.empty()) {
797*9880d681SAndroid Build Coastguard Worker BasicBlock *DirtyBB = DirtyBlocks.back();
798*9880d681SAndroid Build Coastguard Worker DirtyBlocks.pop_back();
799*9880d681SAndroid Build Coastguard Worker
800*9880d681SAndroid Build Coastguard Worker // Already processed this block?
801*9880d681SAndroid Build Coastguard Worker if (!Visited.insert(DirtyBB).second)
802*9880d681SAndroid Build Coastguard Worker continue;
803*9880d681SAndroid Build Coastguard Worker
804*9880d681SAndroid Build Coastguard Worker // Do a binary search to see if we already have an entry for this block in
805*9880d681SAndroid Build Coastguard Worker // the cache set. If so, find it.
806*9880d681SAndroid Build Coastguard Worker DEBUG(AssertSorted(Cache, NumSortedEntries));
807*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo::iterator Entry =
808*9880d681SAndroid Build Coastguard Worker std::upper_bound(Cache.begin(), Cache.begin() + NumSortedEntries,
809*9880d681SAndroid Build Coastguard Worker NonLocalDepEntry(DirtyBB));
810*9880d681SAndroid Build Coastguard Worker if (Entry != Cache.begin() && std::prev(Entry)->getBB() == DirtyBB)
811*9880d681SAndroid Build Coastguard Worker --Entry;
812*9880d681SAndroid Build Coastguard Worker
813*9880d681SAndroid Build Coastguard Worker NonLocalDepEntry *ExistingResult = nullptr;
814*9880d681SAndroid Build Coastguard Worker if (Entry != Cache.begin() + NumSortedEntries &&
815*9880d681SAndroid Build Coastguard Worker Entry->getBB() == DirtyBB) {
816*9880d681SAndroid Build Coastguard Worker // If we already have an entry, and if it isn't already dirty, the block
817*9880d681SAndroid Build Coastguard Worker // is done.
818*9880d681SAndroid Build Coastguard Worker if (!Entry->getResult().isDirty())
819*9880d681SAndroid Build Coastguard Worker continue;
820*9880d681SAndroid Build Coastguard Worker
821*9880d681SAndroid Build Coastguard Worker // Otherwise, remember this slot so we can update the value.
822*9880d681SAndroid Build Coastguard Worker ExistingResult = &*Entry;
823*9880d681SAndroid Build Coastguard Worker }
824*9880d681SAndroid Build Coastguard Worker
825*9880d681SAndroid Build Coastguard Worker // If the dirty entry has a pointer, start scanning from it so we don't have
826*9880d681SAndroid Build Coastguard Worker // to rescan the entire block.
827*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator ScanPos = DirtyBB->end();
828*9880d681SAndroid Build Coastguard Worker if (ExistingResult) {
829*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = ExistingResult->getResult().getInst()) {
830*9880d681SAndroid Build Coastguard Worker ScanPos = Inst->getIterator();
831*9880d681SAndroid Build Coastguard Worker // We're removing QueryInst's use of Inst.
832*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
833*9880d681SAndroid Build Coastguard Worker QueryCS.getInstruction());
834*9880d681SAndroid Build Coastguard Worker }
835*9880d681SAndroid Build Coastguard Worker }
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker // Find out if this block has a local dependency for QueryInst.
838*9880d681SAndroid Build Coastguard Worker MemDepResult Dep;
839*9880d681SAndroid Build Coastguard Worker
840*9880d681SAndroid Build Coastguard Worker if (ScanPos != DirtyBB->begin()) {
841*9880d681SAndroid Build Coastguard Worker Dep =
842*9880d681SAndroid Build Coastguard Worker getCallSiteDependencyFrom(QueryCS, isReadonlyCall, ScanPos, DirtyBB);
843*9880d681SAndroid Build Coastguard Worker } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
844*9880d681SAndroid Build Coastguard Worker // No dependence found. If this is the entry block of the function, it is
845*9880d681SAndroid Build Coastguard Worker // a clobber, otherwise it is unknown.
846*9880d681SAndroid Build Coastguard Worker Dep = MemDepResult::getNonLocal();
847*9880d681SAndroid Build Coastguard Worker } else {
848*9880d681SAndroid Build Coastguard Worker Dep = MemDepResult::getNonFuncLocal();
849*9880d681SAndroid Build Coastguard Worker }
850*9880d681SAndroid Build Coastguard Worker
851*9880d681SAndroid Build Coastguard Worker // If we had a dirty entry for the block, update it. Otherwise, just add
852*9880d681SAndroid Build Coastguard Worker // a new entry.
853*9880d681SAndroid Build Coastguard Worker if (ExistingResult)
854*9880d681SAndroid Build Coastguard Worker ExistingResult->setResult(Dep);
855*9880d681SAndroid Build Coastguard Worker else
856*9880d681SAndroid Build Coastguard Worker Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));
857*9880d681SAndroid Build Coastguard Worker
858*9880d681SAndroid Build Coastguard Worker // If the block has a dependency (i.e. it isn't completely transparent to
859*9880d681SAndroid Build Coastguard Worker // the value), remember the association!
860*9880d681SAndroid Build Coastguard Worker if (!Dep.isNonLocal()) {
861*9880d681SAndroid Build Coastguard Worker // Keep the ReverseNonLocalDeps map up to date so we can efficiently
862*9880d681SAndroid Build Coastguard Worker // update this when we remove instructions.
863*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = Dep.getInst())
864*9880d681SAndroid Build Coastguard Worker ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
865*9880d681SAndroid Build Coastguard Worker } else {
866*9880d681SAndroid Build Coastguard Worker
867*9880d681SAndroid Build Coastguard Worker // If the block *is* completely transparent to the load, we need to check
868*9880d681SAndroid Build Coastguard Worker // the predecessors of this block. Add them to our worklist.
869*9880d681SAndroid Build Coastguard Worker for (BasicBlock *Pred : PredCache.get(DirtyBB))
870*9880d681SAndroid Build Coastguard Worker DirtyBlocks.push_back(Pred);
871*9880d681SAndroid Build Coastguard Worker }
872*9880d681SAndroid Build Coastguard Worker }
873*9880d681SAndroid Build Coastguard Worker
874*9880d681SAndroid Build Coastguard Worker return Cache;
875*9880d681SAndroid Build Coastguard Worker }
876*9880d681SAndroid Build Coastguard Worker
getNonLocalPointerDependency(Instruction * QueryInst,SmallVectorImpl<NonLocalDepResult> & Result)877*9880d681SAndroid Build Coastguard Worker void MemoryDependenceResults::getNonLocalPointerDependency(
878*9880d681SAndroid Build Coastguard Worker Instruction *QueryInst, SmallVectorImpl<NonLocalDepResult> &Result) {
879*9880d681SAndroid Build Coastguard Worker const MemoryLocation Loc = MemoryLocation::get(QueryInst);
880*9880d681SAndroid Build Coastguard Worker bool isLoad = isa<LoadInst>(QueryInst);
881*9880d681SAndroid Build Coastguard Worker BasicBlock *FromBB = QueryInst->getParent();
882*9880d681SAndroid Build Coastguard Worker assert(FromBB);
883*9880d681SAndroid Build Coastguard Worker
884*9880d681SAndroid Build Coastguard Worker assert(Loc.Ptr->getType()->isPointerTy() &&
885*9880d681SAndroid Build Coastguard Worker "Can't get pointer deps of a non-pointer!");
886*9880d681SAndroid Build Coastguard Worker Result.clear();
887*9880d681SAndroid Build Coastguard Worker
888*9880d681SAndroid Build Coastguard Worker // This routine does not expect to deal with volatile instructions.
889*9880d681SAndroid Build Coastguard Worker // Doing so would require piping through the QueryInst all the way through.
890*9880d681SAndroid Build Coastguard Worker // TODO: volatiles can't be elided, but they can be reordered with other
891*9880d681SAndroid Build Coastguard Worker // non-volatile accesses.
892*9880d681SAndroid Build Coastguard Worker
893*9880d681SAndroid Build Coastguard Worker // We currently give up on any instruction which is ordered, but we do handle
894*9880d681SAndroid Build Coastguard Worker // atomic instructions which are unordered.
895*9880d681SAndroid Build Coastguard Worker // TODO: Handle ordered instructions
896*9880d681SAndroid Build Coastguard Worker auto isOrdered = [](Instruction *Inst) {
897*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
898*9880d681SAndroid Build Coastguard Worker return !LI->isUnordered();
899*9880d681SAndroid Build Coastguard Worker } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
900*9880d681SAndroid Build Coastguard Worker return !SI->isUnordered();
901*9880d681SAndroid Build Coastguard Worker }
902*9880d681SAndroid Build Coastguard Worker return false;
903*9880d681SAndroid Build Coastguard Worker };
904*9880d681SAndroid Build Coastguard Worker if (isVolatile(QueryInst) || isOrdered(QueryInst)) {
905*9880d681SAndroid Build Coastguard Worker Result.push_back(NonLocalDepResult(FromBB, MemDepResult::getUnknown(),
906*9880d681SAndroid Build Coastguard Worker const_cast<Value *>(Loc.Ptr)));
907*9880d681SAndroid Build Coastguard Worker return;
908*9880d681SAndroid Build Coastguard Worker }
909*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = FromBB->getModule()->getDataLayout();
910*9880d681SAndroid Build Coastguard Worker PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL, &AC);
911*9880d681SAndroid Build Coastguard Worker
912*9880d681SAndroid Build Coastguard Worker // This is the set of blocks we've inspected, and the pointer we consider in
913*9880d681SAndroid Build Coastguard Worker // each block. Because of critical edges, we currently bail out if querying
914*9880d681SAndroid Build Coastguard Worker // a block with multiple different pointers. This can happen during PHI
915*9880d681SAndroid Build Coastguard Worker // translation.
916*9880d681SAndroid Build Coastguard Worker DenseMap<BasicBlock *, Value *> Visited;
917*9880d681SAndroid Build Coastguard Worker if (getNonLocalPointerDepFromBB(QueryInst, Address, Loc, isLoad, FromBB,
918*9880d681SAndroid Build Coastguard Worker Result, Visited, true))
919*9880d681SAndroid Build Coastguard Worker return;
920*9880d681SAndroid Build Coastguard Worker Result.clear();
921*9880d681SAndroid Build Coastguard Worker Result.push_back(NonLocalDepResult(FromBB, MemDepResult::getUnknown(),
922*9880d681SAndroid Build Coastguard Worker const_cast<Value *>(Loc.Ptr)));
923*9880d681SAndroid Build Coastguard Worker }
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard Worker /// Compute the memdep value for BB with Pointer/PointeeSize using either
926*9880d681SAndroid Build Coastguard Worker /// cached information in Cache or by doing a lookup (which may use dirty cache
927*9880d681SAndroid Build Coastguard Worker /// info if available).
928*9880d681SAndroid Build Coastguard Worker ///
929*9880d681SAndroid Build Coastguard Worker /// If we do a lookup, add the result to the cache.
GetNonLocalInfoForBlock(Instruction * QueryInst,const MemoryLocation & Loc,bool isLoad,BasicBlock * BB,NonLocalDepInfo * Cache,unsigned NumSortedEntries)930*9880d681SAndroid Build Coastguard Worker MemDepResult MemoryDependenceResults::GetNonLocalInfoForBlock(
931*9880d681SAndroid Build Coastguard Worker Instruction *QueryInst, const MemoryLocation &Loc, bool isLoad,
932*9880d681SAndroid Build Coastguard Worker BasicBlock *BB, NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
933*9880d681SAndroid Build Coastguard Worker
934*9880d681SAndroid Build Coastguard Worker // Do a binary search to see if we already have an entry for this block in
935*9880d681SAndroid Build Coastguard Worker // the cache set. If so, find it.
936*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo::iterator Entry = std::upper_bound(
937*9880d681SAndroid Build Coastguard Worker Cache->begin(), Cache->begin() + NumSortedEntries, NonLocalDepEntry(BB));
938*9880d681SAndroid Build Coastguard Worker if (Entry != Cache->begin() && (Entry - 1)->getBB() == BB)
939*9880d681SAndroid Build Coastguard Worker --Entry;
940*9880d681SAndroid Build Coastguard Worker
941*9880d681SAndroid Build Coastguard Worker NonLocalDepEntry *ExistingResult = nullptr;
942*9880d681SAndroid Build Coastguard Worker if (Entry != Cache->begin() + NumSortedEntries && Entry->getBB() == BB)
943*9880d681SAndroid Build Coastguard Worker ExistingResult = &*Entry;
944*9880d681SAndroid Build Coastguard Worker
945*9880d681SAndroid Build Coastguard Worker // If we have a cached entry, and it is non-dirty, use it as the value for
946*9880d681SAndroid Build Coastguard Worker // this dependency.
947*9880d681SAndroid Build Coastguard Worker if (ExistingResult && !ExistingResult->getResult().isDirty()) {
948*9880d681SAndroid Build Coastguard Worker ++NumCacheNonLocalPtr;
949*9880d681SAndroid Build Coastguard Worker return ExistingResult->getResult();
950*9880d681SAndroid Build Coastguard Worker }
951*9880d681SAndroid Build Coastguard Worker
952*9880d681SAndroid Build Coastguard Worker // Otherwise, we have to scan for the value. If we have a dirty cache
953*9880d681SAndroid Build Coastguard Worker // entry, start scanning from its position, otherwise we scan from the end
954*9880d681SAndroid Build Coastguard Worker // of the block.
955*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator ScanPos = BB->end();
956*9880d681SAndroid Build Coastguard Worker if (ExistingResult && ExistingResult->getResult().getInst()) {
957*9880d681SAndroid Build Coastguard Worker assert(ExistingResult->getResult().getInst()->getParent() == BB &&
958*9880d681SAndroid Build Coastguard Worker "Instruction invalidated?");
959*9880d681SAndroid Build Coastguard Worker ++NumCacheDirtyNonLocalPtr;
960*9880d681SAndroid Build Coastguard Worker ScanPos = ExistingResult->getResult().getInst()->getIterator();
961*9880d681SAndroid Build Coastguard Worker
962*9880d681SAndroid Build Coastguard Worker // Eliminating the dirty entry from 'Cache', so update the reverse info.
963*9880d681SAndroid Build Coastguard Worker ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
964*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseNonLocalPtrDeps, &*ScanPos, CacheKey);
965*9880d681SAndroid Build Coastguard Worker } else {
966*9880d681SAndroid Build Coastguard Worker ++NumUncacheNonLocalPtr;
967*9880d681SAndroid Build Coastguard Worker }
968*9880d681SAndroid Build Coastguard Worker
969*9880d681SAndroid Build Coastguard Worker // Scan the block for the dependency.
970*9880d681SAndroid Build Coastguard Worker MemDepResult Dep =
971*9880d681SAndroid Build Coastguard Worker getPointerDependencyFrom(Loc, isLoad, ScanPos, BB, QueryInst);
972*9880d681SAndroid Build Coastguard Worker
973*9880d681SAndroid Build Coastguard Worker // If we had a dirty entry for the block, update it. Otherwise, just add
974*9880d681SAndroid Build Coastguard Worker // a new entry.
975*9880d681SAndroid Build Coastguard Worker if (ExistingResult)
976*9880d681SAndroid Build Coastguard Worker ExistingResult->setResult(Dep);
977*9880d681SAndroid Build Coastguard Worker else
978*9880d681SAndroid Build Coastguard Worker Cache->push_back(NonLocalDepEntry(BB, Dep));
979*9880d681SAndroid Build Coastguard Worker
980*9880d681SAndroid Build Coastguard Worker // If the block has a dependency (i.e. it isn't completely transparent to
981*9880d681SAndroid Build Coastguard Worker // the value), remember the reverse association because we just added it
982*9880d681SAndroid Build Coastguard Worker // to Cache!
983*9880d681SAndroid Build Coastguard Worker if (!Dep.isDef() && !Dep.isClobber())
984*9880d681SAndroid Build Coastguard Worker return Dep;
985*9880d681SAndroid Build Coastguard Worker
986*9880d681SAndroid Build Coastguard Worker // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
987*9880d681SAndroid Build Coastguard Worker // update MemDep when we remove instructions.
988*9880d681SAndroid Build Coastguard Worker Instruction *Inst = Dep.getInst();
989*9880d681SAndroid Build Coastguard Worker assert(Inst && "Didn't depend on anything?");
990*9880d681SAndroid Build Coastguard Worker ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
991*9880d681SAndroid Build Coastguard Worker ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
992*9880d681SAndroid Build Coastguard Worker return Dep;
993*9880d681SAndroid Build Coastguard Worker }
994*9880d681SAndroid Build Coastguard Worker
995*9880d681SAndroid Build Coastguard Worker /// Sort the NonLocalDepInfo cache, given a certain number of elements in the
996*9880d681SAndroid Build Coastguard Worker /// array that are already properly ordered.
997*9880d681SAndroid Build Coastguard Worker ///
998*9880d681SAndroid Build Coastguard Worker /// This is optimized for the case when only a few entries are added.
999*9880d681SAndroid Build Coastguard Worker static void
SortNonLocalDepInfoCache(MemoryDependenceResults::NonLocalDepInfo & Cache,unsigned NumSortedEntries)1000*9880d681SAndroid Build Coastguard Worker SortNonLocalDepInfoCache(MemoryDependenceResults::NonLocalDepInfo &Cache,
1001*9880d681SAndroid Build Coastguard Worker unsigned NumSortedEntries) {
1002*9880d681SAndroid Build Coastguard Worker switch (Cache.size() - NumSortedEntries) {
1003*9880d681SAndroid Build Coastguard Worker case 0:
1004*9880d681SAndroid Build Coastguard Worker // done, no new entries.
1005*9880d681SAndroid Build Coastguard Worker break;
1006*9880d681SAndroid Build Coastguard Worker case 2: {
1007*9880d681SAndroid Build Coastguard Worker // Two new entries, insert the last one into place.
1008*9880d681SAndroid Build Coastguard Worker NonLocalDepEntry Val = Cache.back();
1009*9880d681SAndroid Build Coastguard Worker Cache.pop_back();
1010*9880d681SAndroid Build Coastguard Worker MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
1011*9880d681SAndroid Build Coastguard Worker std::upper_bound(Cache.begin(), Cache.end() - 1, Val);
1012*9880d681SAndroid Build Coastguard Worker Cache.insert(Entry, Val);
1013*9880d681SAndroid Build Coastguard Worker // FALL THROUGH.
1014*9880d681SAndroid Build Coastguard Worker }
1015*9880d681SAndroid Build Coastguard Worker case 1:
1016*9880d681SAndroid Build Coastguard Worker // One new entry, Just insert the new value at the appropriate position.
1017*9880d681SAndroid Build Coastguard Worker if (Cache.size() != 1) {
1018*9880d681SAndroid Build Coastguard Worker NonLocalDepEntry Val = Cache.back();
1019*9880d681SAndroid Build Coastguard Worker Cache.pop_back();
1020*9880d681SAndroid Build Coastguard Worker MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
1021*9880d681SAndroid Build Coastguard Worker std::upper_bound(Cache.begin(), Cache.end(), Val);
1022*9880d681SAndroid Build Coastguard Worker Cache.insert(Entry, Val);
1023*9880d681SAndroid Build Coastguard Worker }
1024*9880d681SAndroid Build Coastguard Worker break;
1025*9880d681SAndroid Build Coastguard Worker default:
1026*9880d681SAndroid Build Coastguard Worker // Added many values, do a full scale sort.
1027*9880d681SAndroid Build Coastguard Worker std::sort(Cache.begin(), Cache.end());
1028*9880d681SAndroid Build Coastguard Worker break;
1029*9880d681SAndroid Build Coastguard Worker }
1030*9880d681SAndroid Build Coastguard Worker }
1031*9880d681SAndroid Build Coastguard Worker
1032*9880d681SAndroid Build Coastguard Worker /// Perform a dependency query based on pointer/pointeesize starting at the end
1033*9880d681SAndroid Build Coastguard Worker /// of StartBB.
1034*9880d681SAndroid Build Coastguard Worker ///
1035*9880d681SAndroid Build Coastguard Worker /// Add any clobber/def results to the results vector and keep track of which
1036*9880d681SAndroid Build Coastguard Worker /// blocks are visited in 'Visited'.
1037*9880d681SAndroid Build Coastguard Worker ///
1038*9880d681SAndroid Build Coastguard Worker /// This has special behavior for the first block queries (when SkipFirstBlock
1039*9880d681SAndroid Build Coastguard Worker /// is true). In this special case, it ignores the contents of the specified
1040*9880d681SAndroid Build Coastguard Worker /// block and starts returning dependence info for its predecessors.
1041*9880d681SAndroid Build Coastguard Worker ///
1042*9880d681SAndroid Build Coastguard Worker /// This function returns true on success, or false to indicate that it could
1043*9880d681SAndroid Build Coastguard Worker /// not compute dependence information for some reason. This should be treated
1044*9880d681SAndroid Build Coastguard Worker /// as a clobber dependence on the first instruction in the predecessor block.
getNonLocalPointerDepFromBB(Instruction * QueryInst,const PHITransAddr & Pointer,const MemoryLocation & Loc,bool isLoad,BasicBlock * StartBB,SmallVectorImpl<NonLocalDepResult> & Result,DenseMap<BasicBlock *,Value * > & Visited,bool SkipFirstBlock)1045*9880d681SAndroid Build Coastguard Worker bool MemoryDependenceResults::getNonLocalPointerDepFromBB(
1046*9880d681SAndroid Build Coastguard Worker Instruction *QueryInst, const PHITransAddr &Pointer,
1047*9880d681SAndroid Build Coastguard Worker const MemoryLocation &Loc, bool isLoad, BasicBlock *StartBB,
1048*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<NonLocalDepResult> &Result,
1049*9880d681SAndroid Build Coastguard Worker DenseMap<BasicBlock *, Value *> &Visited, bool SkipFirstBlock) {
1050*9880d681SAndroid Build Coastguard Worker // Look up the cached info for Pointer.
1051*9880d681SAndroid Build Coastguard Worker ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);
1052*9880d681SAndroid Build Coastguard Worker
1053*9880d681SAndroid Build Coastguard Worker // Set up a temporary NLPI value. If the map doesn't yet have an entry for
1054*9880d681SAndroid Build Coastguard Worker // CacheKey, this value will be inserted as the associated value. Otherwise,
1055*9880d681SAndroid Build Coastguard Worker // it'll be ignored, and we'll have to check to see if the cached size and
1056*9880d681SAndroid Build Coastguard Worker // aa tags are consistent with the current query.
1057*9880d681SAndroid Build Coastguard Worker NonLocalPointerInfo InitialNLPI;
1058*9880d681SAndroid Build Coastguard Worker InitialNLPI.Size = Loc.Size;
1059*9880d681SAndroid Build Coastguard Worker InitialNLPI.AATags = Loc.AATags;
1060*9880d681SAndroid Build Coastguard Worker
1061*9880d681SAndroid Build Coastguard Worker // Get the NLPI for CacheKey, inserting one into the map if it doesn't
1062*9880d681SAndroid Build Coastguard Worker // already have one.
1063*9880d681SAndroid Build Coastguard Worker std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =
1064*9880d681SAndroid Build Coastguard Worker NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));
1065*9880d681SAndroid Build Coastguard Worker NonLocalPointerInfo *CacheInfo = &Pair.first->second;
1066*9880d681SAndroid Build Coastguard Worker
1067*9880d681SAndroid Build Coastguard Worker // If we already have a cache entry for this CacheKey, we may need to do some
1068*9880d681SAndroid Build Coastguard Worker // work to reconcile the cache entry and the current query.
1069*9880d681SAndroid Build Coastguard Worker if (!Pair.second) {
1070*9880d681SAndroid Build Coastguard Worker if (CacheInfo->Size < Loc.Size) {
1071*9880d681SAndroid Build Coastguard Worker // The query's Size is greater than the cached one. Throw out the
1072*9880d681SAndroid Build Coastguard Worker // cached data and proceed with the query at the greater size.
1073*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair();
1074*9880d681SAndroid Build Coastguard Worker CacheInfo->Size = Loc.Size;
1075*9880d681SAndroid Build Coastguard Worker for (auto &Entry : CacheInfo->NonLocalDeps)
1076*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = Entry.getResult().getInst())
1077*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
1078*9880d681SAndroid Build Coastguard Worker CacheInfo->NonLocalDeps.clear();
1079*9880d681SAndroid Build Coastguard Worker } else if (CacheInfo->Size > Loc.Size) {
1080*9880d681SAndroid Build Coastguard Worker // This query's Size is less than the cached one. Conservatively restart
1081*9880d681SAndroid Build Coastguard Worker // the query using the greater size.
1082*9880d681SAndroid Build Coastguard Worker return getNonLocalPointerDepFromBB(
1083*9880d681SAndroid Build Coastguard Worker QueryInst, Pointer, Loc.getWithNewSize(CacheInfo->Size), isLoad,
1084*9880d681SAndroid Build Coastguard Worker StartBB, Result, Visited, SkipFirstBlock);
1085*9880d681SAndroid Build Coastguard Worker }
1086*9880d681SAndroid Build Coastguard Worker
1087*9880d681SAndroid Build Coastguard Worker // If the query's AATags are inconsistent with the cached one,
1088*9880d681SAndroid Build Coastguard Worker // conservatively throw out the cached data and restart the query with
1089*9880d681SAndroid Build Coastguard Worker // no tag if needed.
1090*9880d681SAndroid Build Coastguard Worker if (CacheInfo->AATags != Loc.AATags) {
1091*9880d681SAndroid Build Coastguard Worker if (CacheInfo->AATags) {
1092*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair();
1093*9880d681SAndroid Build Coastguard Worker CacheInfo->AATags = AAMDNodes();
1094*9880d681SAndroid Build Coastguard Worker for (auto &Entry : CacheInfo->NonLocalDeps)
1095*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = Entry.getResult().getInst())
1096*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
1097*9880d681SAndroid Build Coastguard Worker CacheInfo->NonLocalDeps.clear();
1098*9880d681SAndroid Build Coastguard Worker }
1099*9880d681SAndroid Build Coastguard Worker if (Loc.AATags)
1100*9880d681SAndroid Build Coastguard Worker return getNonLocalPointerDepFromBB(
1101*9880d681SAndroid Build Coastguard Worker QueryInst, Pointer, Loc.getWithoutAATags(), isLoad, StartBB, Result,
1102*9880d681SAndroid Build Coastguard Worker Visited, SkipFirstBlock);
1103*9880d681SAndroid Build Coastguard Worker }
1104*9880d681SAndroid Build Coastguard Worker }
1105*9880d681SAndroid Build Coastguard Worker
1106*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;
1107*9880d681SAndroid Build Coastguard Worker
1108*9880d681SAndroid Build Coastguard Worker // If we have valid cached information for exactly the block we are
1109*9880d681SAndroid Build Coastguard Worker // investigating, just return it with no recomputation.
1110*9880d681SAndroid Build Coastguard Worker if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
1111*9880d681SAndroid Build Coastguard Worker // We have a fully cached result for this query then we can just return the
1112*9880d681SAndroid Build Coastguard Worker // cached results and populate the visited set. However, we have to verify
1113*9880d681SAndroid Build Coastguard Worker // that we don't already have conflicting results for these blocks. Check
1114*9880d681SAndroid Build Coastguard Worker // to ensure that if a block in the results set is in the visited set that
1115*9880d681SAndroid Build Coastguard Worker // it was for the same pointer query.
1116*9880d681SAndroid Build Coastguard Worker if (!Visited.empty()) {
1117*9880d681SAndroid Build Coastguard Worker for (auto &Entry : *Cache) {
1118*9880d681SAndroid Build Coastguard Worker DenseMap<BasicBlock *, Value *>::iterator VI =
1119*9880d681SAndroid Build Coastguard Worker Visited.find(Entry.getBB());
1120*9880d681SAndroid Build Coastguard Worker if (VI == Visited.end() || VI->second == Pointer.getAddr())
1121*9880d681SAndroid Build Coastguard Worker continue;
1122*9880d681SAndroid Build Coastguard Worker
1123*9880d681SAndroid Build Coastguard Worker // We have a pointer mismatch in a block. Just return false, saying
1124*9880d681SAndroid Build Coastguard Worker // that something was clobbered in this result. We could also do a
1125*9880d681SAndroid Build Coastguard Worker // non-fully cached query, but there is little point in doing this.
1126*9880d681SAndroid Build Coastguard Worker return false;
1127*9880d681SAndroid Build Coastguard Worker }
1128*9880d681SAndroid Build Coastguard Worker }
1129*9880d681SAndroid Build Coastguard Worker
1130*9880d681SAndroid Build Coastguard Worker Value *Addr = Pointer.getAddr();
1131*9880d681SAndroid Build Coastguard Worker for (auto &Entry : *Cache) {
1132*9880d681SAndroid Build Coastguard Worker Visited.insert(std::make_pair(Entry.getBB(), Addr));
1133*9880d681SAndroid Build Coastguard Worker if (Entry.getResult().isNonLocal()) {
1134*9880d681SAndroid Build Coastguard Worker continue;
1135*9880d681SAndroid Build Coastguard Worker }
1136*9880d681SAndroid Build Coastguard Worker
1137*9880d681SAndroid Build Coastguard Worker if (DT.isReachableFromEntry(Entry.getBB())) {
1138*9880d681SAndroid Build Coastguard Worker Result.push_back(
1139*9880d681SAndroid Build Coastguard Worker NonLocalDepResult(Entry.getBB(), Entry.getResult(), Addr));
1140*9880d681SAndroid Build Coastguard Worker }
1141*9880d681SAndroid Build Coastguard Worker }
1142*9880d681SAndroid Build Coastguard Worker ++NumCacheCompleteNonLocalPtr;
1143*9880d681SAndroid Build Coastguard Worker return true;
1144*9880d681SAndroid Build Coastguard Worker }
1145*9880d681SAndroid Build Coastguard Worker
1146*9880d681SAndroid Build Coastguard Worker // Otherwise, either this is a new block, a block with an invalid cache
1147*9880d681SAndroid Build Coastguard Worker // pointer or one that we're about to invalidate by putting more info into it
1148*9880d681SAndroid Build Coastguard Worker // than its valid cache info. If empty, the result will be valid cache info,
1149*9880d681SAndroid Build Coastguard Worker // otherwise it isn't.
1150*9880d681SAndroid Build Coastguard Worker if (Cache->empty())
1151*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
1152*9880d681SAndroid Build Coastguard Worker else
1153*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair();
1154*9880d681SAndroid Build Coastguard Worker
1155*9880d681SAndroid Build Coastguard Worker SmallVector<BasicBlock *, 32> Worklist;
1156*9880d681SAndroid Build Coastguard Worker Worklist.push_back(StartBB);
1157*9880d681SAndroid Build Coastguard Worker
1158*9880d681SAndroid Build Coastguard Worker // PredList used inside loop.
1159*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<BasicBlock *, PHITransAddr>, 16> PredList;
1160*9880d681SAndroid Build Coastguard Worker
1161*9880d681SAndroid Build Coastguard Worker // Keep track of the entries that we know are sorted. Previously cached
1162*9880d681SAndroid Build Coastguard Worker // entries will all be sorted. The entries we add we only sort on demand (we
1163*9880d681SAndroid Build Coastguard Worker // don't insert every element into its sorted position). We know that we
1164*9880d681SAndroid Build Coastguard Worker // won't get any reuse from currently inserted values, because we don't
1165*9880d681SAndroid Build Coastguard Worker // revisit blocks after we insert info for them.
1166*9880d681SAndroid Build Coastguard Worker unsigned NumSortedEntries = Cache->size();
1167*9880d681SAndroid Build Coastguard Worker unsigned WorklistEntries = BlockNumberLimit;
1168*9880d681SAndroid Build Coastguard Worker bool GotWorklistLimit = false;
1169*9880d681SAndroid Build Coastguard Worker DEBUG(AssertSorted(*Cache));
1170*9880d681SAndroid Build Coastguard Worker
1171*9880d681SAndroid Build Coastguard Worker while (!Worklist.empty()) {
1172*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = Worklist.pop_back_val();
1173*9880d681SAndroid Build Coastguard Worker
1174*9880d681SAndroid Build Coastguard Worker // If we do process a large number of blocks it becomes very expensive and
1175*9880d681SAndroid Build Coastguard Worker // likely it isn't worth worrying about
1176*9880d681SAndroid Build Coastguard Worker if (Result.size() > NumResultsLimit) {
1177*9880d681SAndroid Build Coastguard Worker Worklist.clear();
1178*9880d681SAndroid Build Coastguard Worker // Sort it now (if needed) so that recursive invocations of
1179*9880d681SAndroid Build Coastguard Worker // getNonLocalPointerDepFromBB and other routines that could reuse the
1180*9880d681SAndroid Build Coastguard Worker // cache value will only see properly sorted cache arrays.
1181*9880d681SAndroid Build Coastguard Worker if (Cache && NumSortedEntries != Cache->size()) {
1182*9880d681SAndroid Build Coastguard Worker SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
1183*9880d681SAndroid Build Coastguard Worker }
1184*9880d681SAndroid Build Coastguard Worker // Since we bail out, the "Cache" set won't contain all of the
1185*9880d681SAndroid Build Coastguard Worker // results for the query. This is ok (we can still use it to accelerate
1186*9880d681SAndroid Build Coastguard Worker // specific block queries) but we can't do the fastpath "return all
1187*9880d681SAndroid Build Coastguard Worker // results from the set". Clear out the indicator for this.
1188*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair();
1189*9880d681SAndroid Build Coastguard Worker return false;
1190*9880d681SAndroid Build Coastguard Worker }
1191*9880d681SAndroid Build Coastguard Worker
1192*9880d681SAndroid Build Coastguard Worker // Skip the first block if we have it.
1193*9880d681SAndroid Build Coastguard Worker if (!SkipFirstBlock) {
1194*9880d681SAndroid Build Coastguard Worker // Analyze the dependency of *Pointer in FromBB. See if we already have
1195*9880d681SAndroid Build Coastguard Worker // been here.
1196*9880d681SAndroid Build Coastguard Worker assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
1197*9880d681SAndroid Build Coastguard Worker
1198*9880d681SAndroid Build Coastguard Worker // Get the dependency info for Pointer in BB. If we have cached
1199*9880d681SAndroid Build Coastguard Worker // information, we will use it, otherwise we compute it.
1200*9880d681SAndroid Build Coastguard Worker DEBUG(AssertSorted(*Cache, NumSortedEntries));
1201*9880d681SAndroid Build Coastguard Worker MemDepResult Dep = GetNonLocalInfoForBlock(QueryInst, Loc, isLoad, BB,
1202*9880d681SAndroid Build Coastguard Worker Cache, NumSortedEntries);
1203*9880d681SAndroid Build Coastguard Worker
1204*9880d681SAndroid Build Coastguard Worker // If we got a Def or Clobber, add this to the list of results.
1205*9880d681SAndroid Build Coastguard Worker if (!Dep.isNonLocal()) {
1206*9880d681SAndroid Build Coastguard Worker if (DT.isReachableFromEntry(BB)) {
1207*9880d681SAndroid Build Coastguard Worker Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));
1208*9880d681SAndroid Build Coastguard Worker continue;
1209*9880d681SAndroid Build Coastguard Worker }
1210*9880d681SAndroid Build Coastguard Worker }
1211*9880d681SAndroid Build Coastguard Worker }
1212*9880d681SAndroid Build Coastguard Worker
1213*9880d681SAndroid Build Coastguard Worker // If 'Pointer' is an instruction defined in this block, then we need to do
1214*9880d681SAndroid Build Coastguard Worker // phi translation to change it into a value live in the predecessor block.
1215*9880d681SAndroid Build Coastguard Worker // If not, we just add the predecessors to the worklist and scan them with
1216*9880d681SAndroid Build Coastguard Worker // the same Pointer.
1217*9880d681SAndroid Build Coastguard Worker if (!Pointer.NeedsPHITranslationFromBlock(BB)) {
1218*9880d681SAndroid Build Coastguard Worker SkipFirstBlock = false;
1219*9880d681SAndroid Build Coastguard Worker SmallVector<BasicBlock *, 16> NewBlocks;
1220*9880d681SAndroid Build Coastguard Worker for (BasicBlock *Pred : PredCache.get(BB)) {
1221*9880d681SAndroid Build Coastguard Worker // Verify that we haven't looked at this block yet.
1222*9880d681SAndroid Build Coastguard Worker std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> InsertRes =
1223*9880d681SAndroid Build Coastguard Worker Visited.insert(std::make_pair(Pred, Pointer.getAddr()));
1224*9880d681SAndroid Build Coastguard Worker if (InsertRes.second) {
1225*9880d681SAndroid Build Coastguard Worker // First time we've looked at *PI.
1226*9880d681SAndroid Build Coastguard Worker NewBlocks.push_back(Pred);
1227*9880d681SAndroid Build Coastguard Worker continue;
1228*9880d681SAndroid Build Coastguard Worker }
1229*9880d681SAndroid Build Coastguard Worker
1230*9880d681SAndroid Build Coastguard Worker // If we have seen this block before, but it was with a different
1231*9880d681SAndroid Build Coastguard Worker // pointer then we have a phi translation failure and we have to treat
1232*9880d681SAndroid Build Coastguard Worker // this as a clobber.
1233*9880d681SAndroid Build Coastguard Worker if (InsertRes.first->second != Pointer.getAddr()) {
1234*9880d681SAndroid Build Coastguard Worker // Make sure to clean up the Visited map before continuing on to
1235*9880d681SAndroid Build Coastguard Worker // PredTranslationFailure.
1236*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NewBlocks.size(); i++)
1237*9880d681SAndroid Build Coastguard Worker Visited.erase(NewBlocks[i]);
1238*9880d681SAndroid Build Coastguard Worker goto PredTranslationFailure;
1239*9880d681SAndroid Build Coastguard Worker }
1240*9880d681SAndroid Build Coastguard Worker }
1241*9880d681SAndroid Build Coastguard Worker if (NewBlocks.size() > WorklistEntries) {
1242*9880d681SAndroid Build Coastguard Worker // Make sure to clean up the Visited map before continuing on to
1243*9880d681SAndroid Build Coastguard Worker // PredTranslationFailure.
1244*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NewBlocks.size(); i++)
1245*9880d681SAndroid Build Coastguard Worker Visited.erase(NewBlocks[i]);
1246*9880d681SAndroid Build Coastguard Worker GotWorklistLimit = true;
1247*9880d681SAndroid Build Coastguard Worker goto PredTranslationFailure;
1248*9880d681SAndroid Build Coastguard Worker }
1249*9880d681SAndroid Build Coastguard Worker WorklistEntries -= NewBlocks.size();
1250*9880d681SAndroid Build Coastguard Worker Worklist.append(NewBlocks.begin(), NewBlocks.end());
1251*9880d681SAndroid Build Coastguard Worker continue;
1252*9880d681SAndroid Build Coastguard Worker }
1253*9880d681SAndroid Build Coastguard Worker
1254*9880d681SAndroid Build Coastguard Worker // We do need to do phi translation, if we know ahead of time we can't phi
1255*9880d681SAndroid Build Coastguard Worker // translate this value, don't even try.
1256*9880d681SAndroid Build Coastguard Worker if (!Pointer.IsPotentiallyPHITranslatable())
1257*9880d681SAndroid Build Coastguard Worker goto PredTranslationFailure;
1258*9880d681SAndroid Build Coastguard Worker
1259*9880d681SAndroid Build Coastguard Worker // We may have added values to the cache list before this PHI translation.
1260*9880d681SAndroid Build Coastguard Worker // If so, we haven't done anything to ensure that the cache remains sorted.
1261*9880d681SAndroid Build Coastguard Worker // Sort it now (if needed) so that recursive invocations of
1262*9880d681SAndroid Build Coastguard Worker // getNonLocalPointerDepFromBB and other routines that could reuse the cache
1263*9880d681SAndroid Build Coastguard Worker // value will only see properly sorted cache arrays.
1264*9880d681SAndroid Build Coastguard Worker if (Cache && NumSortedEntries != Cache->size()) {
1265*9880d681SAndroid Build Coastguard Worker SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
1266*9880d681SAndroid Build Coastguard Worker NumSortedEntries = Cache->size();
1267*9880d681SAndroid Build Coastguard Worker }
1268*9880d681SAndroid Build Coastguard Worker Cache = nullptr;
1269*9880d681SAndroid Build Coastguard Worker
1270*9880d681SAndroid Build Coastguard Worker PredList.clear();
1271*9880d681SAndroid Build Coastguard Worker for (BasicBlock *Pred : PredCache.get(BB)) {
1272*9880d681SAndroid Build Coastguard Worker PredList.push_back(std::make_pair(Pred, Pointer));
1273*9880d681SAndroid Build Coastguard Worker
1274*9880d681SAndroid Build Coastguard Worker // Get the PHI translated pointer in this predecessor. This can fail if
1275*9880d681SAndroid Build Coastguard Worker // not translatable, in which case the getAddr() returns null.
1276*9880d681SAndroid Build Coastguard Worker PHITransAddr &PredPointer = PredList.back().second;
1277*9880d681SAndroid Build Coastguard Worker PredPointer.PHITranslateValue(BB, Pred, &DT, /*MustDominate=*/false);
1278*9880d681SAndroid Build Coastguard Worker Value *PredPtrVal = PredPointer.getAddr();
1279*9880d681SAndroid Build Coastguard Worker
1280*9880d681SAndroid Build Coastguard Worker // Check to see if we have already visited this pred block with another
1281*9880d681SAndroid Build Coastguard Worker // pointer. If so, we can't do this lookup. This failure can occur
1282*9880d681SAndroid Build Coastguard Worker // with PHI translation when a critical edge exists and the PHI node in
1283*9880d681SAndroid Build Coastguard Worker // the successor translates to a pointer value different than the
1284*9880d681SAndroid Build Coastguard Worker // pointer the block was first analyzed with.
1285*9880d681SAndroid Build Coastguard Worker std::pair<DenseMap<BasicBlock *, Value *>::iterator, bool> InsertRes =
1286*9880d681SAndroid Build Coastguard Worker Visited.insert(std::make_pair(Pred, PredPtrVal));
1287*9880d681SAndroid Build Coastguard Worker
1288*9880d681SAndroid Build Coastguard Worker if (!InsertRes.second) {
1289*9880d681SAndroid Build Coastguard Worker // We found the pred; take it off the list of preds to visit.
1290*9880d681SAndroid Build Coastguard Worker PredList.pop_back();
1291*9880d681SAndroid Build Coastguard Worker
1292*9880d681SAndroid Build Coastguard Worker // If the predecessor was visited with PredPtr, then we already did
1293*9880d681SAndroid Build Coastguard Worker // the analysis and can ignore it.
1294*9880d681SAndroid Build Coastguard Worker if (InsertRes.first->second == PredPtrVal)
1295*9880d681SAndroid Build Coastguard Worker continue;
1296*9880d681SAndroid Build Coastguard Worker
1297*9880d681SAndroid Build Coastguard Worker // Otherwise, the block was previously analyzed with a different
1298*9880d681SAndroid Build Coastguard Worker // pointer. We can't represent the result of this case, so we just
1299*9880d681SAndroid Build Coastguard Worker // treat this as a phi translation failure.
1300*9880d681SAndroid Build Coastguard Worker
1301*9880d681SAndroid Build Coastguard Worker // Make sure to clean up the Visited map before continuing on to
1302*9880d681SAndroid Build Coastguard Worker // PredTranslationFailure.
1303*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = PredList.size(); i < n; ++i)
1304*9880d681SAndroid Build Coastguard Worker Visited.erase(PredList[i].first);
1305*9880d681SAndroid Build Coastguard Worker
1306*9880d681SAndroid Build Coastguard Worker goto PredTranslationFailure;
1307*9880d681SAndroid Build Coastguard Worker }
1308*9880d681SAndroid Build Coastguard Worker }
1309*9880d681SAndroid Build Coastguard Worker
1310*9880d681SAndroid Build Coastguard Worker // Actually process results here; this need to be a separate loop to avoid
1311*9880d681SAndroid Build Coastguard Worker // calling getNonLocalPointerDepFromBB for blocks we don't want to return
1312*9880d681SAndroid Build Coastguard Worker // any results for. (getNonLocalPointerDepFromBB will modify our
1313*9880d681SAndroid Build Coastguard Worker // datastructures in ways the code after the PredTranslationFailure label
1314*9880d681SAndroid Build Coastguard Worker // doesn't expect.)
1315*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, n = PredList.size(); i < n; ++i) {
1316*9880d681SAndroid Build Coastguard Worker BasicBlock *Pred = PredList[i].first;
1317*9880d681SAndroid Build Coastguard Worker PHITransAddr &PredPointer = PredList[i].second;
1318*9880d681SAndroid Build Coastguard Worker Value *PredPtrVal = PredPointer.getAddr();
1319*9880d681SAndroid Build Coastguard Worker
1320*9880d681SAndroid Build Coastguard Worker bool CanTranslate = true;
1321*9880d681SAndroid Build Coastguard Worker // If PHI translation was unable to find an available pointer in this
1322*9880d681SAndroid Build Coastguard Worker // predecessor, then we have to assume that the pointer is clobbered in
1323*9880d681SAndroid Build Coastguard Worker // that predecessor. We can still do PRE of the load, which would insert
1324*9880d681SAndroid Build Coastguard Worker // a computation of the pointer in this predecessor.
1325*9880d681SAndroid Build Coastguard Worker if (!PredPtrVal)
1326*9880d681SAndroid Build Coastguard Worker CanTranslate = false;
1327*9880d681SAndroid Build Coastguard Worker
1328*9880d681SAndroid Build Coastguard Worker // FIXME: it is entirely possible that PHI translating will end up with
1329*9880d681SAndroid Build Coastguard Worker // the same value. Consider PHI translating something like:
1330*9880d681SAndroid Build Coastguard Worker // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
1331*9880d681SAndroid Build Coastguard Worker // to recurse here, pedantically speaking.
1332*9880d681SAndroid Build Coastguard Worker
1333*9880d681SAndroid Build Coastguard Worker // If getNonLocalPointerDepFromBB fails here, that means the cached
1334*9880d681SAndroid Build Coastguard Worker // result conflicted with the Visited list; we have to conservatively
1335*9880d681SAndroid Build Coastguard Worker // assume it is unknown, but this also does not block PRE of the load.
1336*9880d681SAndroid Build Coastguard Worker if (!CanTranslate ||
1337*9880d681SAndroid Build Coastguard Worker !getNonLocalPointerDepFromBB(QueryInst, PredPointer,
1338*9880d681SAndroid Build Coastguard Worker Loc.getWithNewPtr(PredPtrVal), isLoad,
1339*9880d681SAndroid Build Coastguard Worker Pred, Result, Visited)) {
1340*9880d681SAndroid Build Coastguard Worker // Add the entry to the Result list.
1341*9880d681SAndroid Build Coastguard Worker NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal);
1342*9880d681SAndroid Build Coastguard Worker Result.push_back(Entry);
1343*9880d681SAndroid Build Coastguard Worker
1344*9880d681SAndroid Build Coastguard Worker // Since we had a phi translation failure, the cache for CacheKey won't
1345*9880d681SAndroid Build Coastguard Worker // include all of the entries that we need to immediately satisfy future
1346*9880d681SAndroid Build Coastguard Worker // queries. Mark this in NonLocalPointerDeps by setting the
1347*9880d681SAndroid Build Coastguard Worker // BBSkipFirstBlockPair pointer to null. This requires reuse of the
1348*9880d681SAndroid Build Coastguard Worker // cached value to do more work but not miss the phi trans failure.
1349*9880d681SAndroid Build Coastguard Worker NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];
1350*9880d681SAndroid Build Coastguard Worker NLPI.Pair = BBSkipFirstBlockPair();
1351*9880d681SAndroid Build Coastguard Worker continue;
1352*9880d681SAndroid Build Coastguard Worker }
1353*9880d681SAndroid Build Coastguard Worker }
1354*9880d681SAndroid Build Coastguard Worker
1355*9880d681SAndroid Build Coastguard Worker // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
1356*9880d681SAndroid Build Coastguard Worker CacheInfo = &NonLocalPointerDeps[CacheKey];
1357*9880d681SAndroid Build Coastguard Worker Cache = &CacheInfo->NonLocalDeps;
1358*9880d681SAndroid Build Coastguard Worker NumSortedEntries = Cache->size();
1359*9880d681SAndroid Build Coastguard Worker
1360*9880d681SAndroid Build Coastguard Worker // Since we did phi translation, the "Cache" set won't contain all of the
1361*9880d681SAndroid Build Coastguard Worker // results for the query. This is ok (we can still use it to accelerate
1362*9880d681SAndroid Build Coastguard Worker // specific block queries) but we can't do the fastpath "return all
1363*9880d681SAndroid Build Coastguard Worker // results from the set" Clear out the indicator for this.
1364*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair();
1365*9880d681SAndroid Build Coastguard Worker SkipFirstBlock = false;
1366*9880d681SAndroid Build Coastguard Worker continue;
1367*9880d681SAndroid Build Coastguard Worker
1368*9880d681SAndroid Build Coastguard Worker PredTranslationFailure:
1369*9880d681SAndroid Build Coastguard Worker // The following code is "failure"; we can't produce a sane translation
1370*9880d681SAndroid Build Coastguard Worker // for the given block. It assumes that we haven't modified any of
1371*9880d681SAndroid Build Coastguard Worker // our datastructures while processing the current block.
1372*9880d681SAndroid Build Coastguard Worker
1373*9880d681SAndroid Build Coastguard Worker if (!Cache) {
1374*9880d681SAndroid Build Coastguard Worker // Refresh the CacheInfo/Cache pointer if it got invalidated.
1375*9880d681SAndroid Build Coastguard Worker CacheInfo = &NonLocalPointerDeps[CacheKey];
1376*9880d681SAndroid Build Coastguard Worker Cache = &CacheInfo->NonLocalDeps;
1377*9880d681SAndroid Build Coastguard Worker NumSortedEntries = Cache->size();
1378*9880d681SAndroid Build Coastguard Worker }
1379*9880d681SAndroid Build Coastguard Worker
1380*9880d681SAndroid Build Coastguard Worker // Since we failed phi translation, the "Cache" set won't contain all of the
1381*9880d681SAndroid Build Coastguard Worker // results for the query. This is ok (we can still use it to accelerate
1382*9880d681SAndroid Build Coastguard Worker // specific block queries) but we can't do the fastpath "return all
1383*9880d681SAndroid Build Coastguard Worker // results from the set". Clear out the indicator for this.
1384*9880d681SAndroid Build Coastguard Worker CacheInfo->Pair = BBSkipFirstBlockPair();
1385*9880d681SAndroid Build Coastguard Worker
1386*9880d681SAndroid Build Coastguard Worker // If *nothing* works, mark the pointer as unknown.
1387*9880d681SAndroid Build Coastguard Worker //
1388*9880d681SAndroid Build Coastguard Worker // If this is the magic first block, return this as a clobber of the whole
1389*9880d681SAndroid Build Coastguard Worker // incoming value. Since we can't phi translate to one of the predecessors,
1390*9880d681SAndroid Build Coastguard Worker // we have to bail out.
1391*9880d681SAndroid Build Coastguard Worker if (SkipFirstBlock)
1392*9880d681SAndroid Build Coastguard Worker return false;
1393*9880d681SAndroid Build Coastguard Worker
1394*9880d681SAndroid Build Coastguard Worker bool foundBlock = false;
1395*9880d681SAndroid Build Coastguard Worker for (NonLocalDepEntry &I : llvm::reverse(*Cache)) {
1396*9880d681SAndroid Build Coastguard Worker if (I.getBB() != BB)
1397*9880d681SAndroid Build Coastguard Worker continue;
1398*9880d681SAndroid Build Coastguard Worker
1399*9880d681SAndroid Build Coastguard Worker assert((GotWorklistLimit || I.getResult().isNonLocal() ||
1400*9880d681SAndroid Build Coastguard Worker !DT.isReachableFromEntry(BB)) &&
1401*9880d681SAndroid Build Coastguard Worker "Should only be here with transparent block");
1402*9880d681SAndroid Build Coastguard Worker foundBlock = true;
1403*9880d681SAndroid Build Coastguard Worker I.setResult(MemDepResult::getUnknown());
1404*9880d681SAndroid Build Coastguard Worker Result.push_back(
1405*9880d681SAndroid Build Coastguard Worker NonLocalDepResult(I.getBB(), I.getResult(), Pointer.getAddr()));
1406*9880d681SAndroid Build Coastguard Worker break;
1407*9880d681SAndroid Build Coastguard Worker }
1408*9880d681SAndroid Build Coastguard Worker (void)foundBlock; (void)GotWorklistLimit;
1409*9880d681SAndroid Build Coastguard Worker assert((foundBlock || GotWorklistLimit) && "Current block not in cache?");
1410*9880d681SAndroid Build Coastguard Worker }
1411*9880d681SAndroid Build Coastguard Worker
1412*9880d681SAndroid Build Coastguard Worker // Okay, we're done now. If we added new values to the cache, re-sort it.
1413*9880d681SAndroid Build Coastguard Worker SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
1414*9880d681SAndroid Build Coastguard Worker DEBUG(AssertSorted(*Cache));
1415*9880d681SAndroid Build Coastguard Worker return true;
1416*9880d681SAndroid Build Coastguard Worker }
1417*9880d681SAndroid Build Coastguard Worker
1418*9880d681SAndroid Build Coastguard Worker /// If P exists in CachedNonLocalPointerInfo, remove it.
RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P)1419*9880d681SAndroid Build Coastguard Worker void MemoryDependenceResults::RemoveCachedNonLocalPointerDependencies(
1420*9880d681SAndroid Build Coastguard Worker ValueIsLoadPair P) {
1421*9880d681SAndroid Build Coastguard Worker CachedNonLocalPointerInfo::iterator It = NonLocalPointerDeps.find(P);
1422*9880d681SAndroid Build Coastguard Worker if (It == NonLocalPointerDeps.end())
1423*9880d681SAndroid Build Coastguard Worker return;
1424*9880d681SAndroid Build Coastguard Worker
1425*9880d681SAndroid Build Coastguard Worker // Remove all of the entries in the BB->val map. This involves removing
1426*9880d681SAndroid Build Coastguard Worker // instructions from the reverse map.
1427*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo &PInfo = It->second.NonLocalDeps;
1428*9880d681SAndroid Build Coastguard Worker
1429*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
1430*9880d681SAndroid Build Coastguard Worker Instruction *Target = PInfo[i].getResult().getInst();
1431*9880d681SAndroid Build Coastguard Worker if (!Target)
1432*9880d681SAndroid Build Coastguard Worker continue; // Ignore non-local dep results.
1433*9880d681SAndroid Build Coastguard Worker assert(Target->getParent() == PInfo[i].getBB());
1434*9880d681SAndroid Build Coastguard Worker
1435*9880d681SAndroid Build Coastguard Worker // Eliminating the dirty entry from 'Cache', so update the reverse info.
1436*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
1437*9880d681SAndroid Build Coastguard Worker }
1438*9880d681SAndroid Build Coastguard Worker
1439*9880d681SAndroid Build Coastguard Worker // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
1440*9880d681SAndroid Build Coastguard Worker NonLocalPointerDeps.erase(It);
1441*9880d681SAndroid Build Coastguard Worker }
1442*9880d681SAndroid Build Coastguard Worker
invalidateCachedPointerInfo(Value * Ptr)1443*9880d681SAndroid Build Coastguard Worker void MemoryDependenceResults::invalidateCachedPointerInfo(Value *Ptr) {
1444*9880d681SAndroid Build Coastguard Worker // If Ptr isn't really a pointer, just ignore it.
1445*9880d681SAndroid Build Coastguard Worker if (!Ptr->getType()->isPointerTy())
1446*9880d681SAndroid Build Coastguard Worker return;
1447*9880d681SAndroid Build Coastguard Worker // Flush store info for the pointer.
1448*9880d681SAndroid Build Coastguard Worker RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
1449*9880d681SAndroid Build Coastguard Worker // Flush load info for the pointer.
1450*9880d681SAndroid Build Coastguard Worker RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
1451*9880d681SAndroid Build Coastguard Worker }
1452*9880d681SAndroid Build Coastguard Worker
invalidateCachedPredecessors()1453*9880d681SAndroid Build Coastguard Worker void MemoryDependenceResults::invalidateCachedPredecessors() {
1454*9880d681SAndroid Build Coastguard Worker PredCache.clear();
1455*9880d681SAndroid Build Coastguard Worker }
1456*9880d681SAndroid Build Coastguard Worker
removeInstruction(Instruction * RemInst)1457*9880d681SAndroid Build Coastguard Worker void MemoryDependenceResults::removeInstruction(Instruction *RemInst) {
1458*9880d681SAndroid Build Coastguard Worker // Walk through the Non-local dependencies, removing this one as the value
1459*9880d681SAndroid Build Coastguard Worker // for any cached queries.
1460*9880d681SAndroid Build Coastguard Worker NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
1461*9880d681SAndroid Build Coastguard Worker if (NLDI != NonLocalDeps.end()) {
1462*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo &BlockMap = NLDI->second.first;
1463*9880d681SAndroid Build Coastguard Worker for (auto &Entry : BlockMap)
1464*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = Entry.getResult().getInst())
1465*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
1466*9880d681SAndroid Build Coastguard Worker NonLocalDeps.erase(NLDI);
1467*9880d681SAndroid Build Coastguard Worker }
1468*9880d681SAndroid Build Coastguard Worker
1469*9880d681SAndroid Build Coastguard Worker // If we have a cached local dependence query for this instruction, remove it.
1470*9880d681SAndroid Build Coastguard Worker //
1471*9880d681SAndroid Build Coastguard Worker LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
1472*9880d681SAndroid Build Coastguard Worker if (LocalDepEntry != LocalDeps.end()) {
1473*9880d681SAndroid Build Coastguard Worker // Remove us from DepInst's reverse set now that the local dep info is gone.
1474*9880d681SAndroid Build Coastguard Worker if (Instruction *Inst = LocalDepEntry->second.getInst())
1475*9880d681SAndroid Build Coastguard Worker RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
1476*9880d681SAndroid Build Coastguard Worker
1477*9880d681SAndroid Build Coastguard Worker // Remove this local dependency info.
1478*9880d681SAndroid Build Coastguard Worker LocalDeps.erase(LocalDepEntry);
1479*9880d681SAndroid Build Coastguard Worker }
1480*9880d681SAndroid Build Coastguard Worker
1481*9880d681SAndroid Build Coastguard Worker // If we have any cached pointer dependencies on this instruction, remove
1482*9880d681SAndroid Build Coastguard Worker // them. If the instruction has non-pointer type, then it can't be a pointer
1483*9880d681SAndroid Build Coastguard Worker // base.
1484*9880d681SAndroid Build Coastguard Worker
1485*9880d681SAndroid Build Coastguard Worker // Remove it from both the load info and the store info. The instruction
1486*9880d681SAndroid Build Coastguard Worker // can't be in either of these maps if it is non-pointer.
1487*9880d681SAndroid Build Coastguard Worker if (RemInst->getType()->isPointerTy()) {
1488*9880d681SAndroid Build Coastguard Worker RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
1489*9880d681SAndroid Build Coastguard Worker RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
1490*9880d681SAndroid Build Coastguard Worker }
1491*9880d681SAndroid Build Coastguard Worker
1492*9880d681SAndroid Build Coastguard Worker // Loop over all of the things that depend on the instruction we're removing.
1493*9880d681SAndroid Build Coastguard Worker //
1494*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<Instruction *, Instruction *>, 8> ReverseDepsToAdd;
1495*9880d681SAndroid Build Coastguard Worker
1496*9880d681SAndroid Build Coastguard Worker // If we find RemInst as a clobber or Def in any of the maps for other values,
1497*9880d681SAndroid Build Coastguard Worker // we need to replace its entry with a dirty version of the instruction after
1498*9880d681SAndroid Build Coastguard Worker // it. If RemInst is a terminator, we use a null dirty value.
1499*9880d681SAndroid Build Coastguard Worker //
1500*9880d681SAndroid Build Coastguard Worker // Using a dirty version of the instruction after RemInst saves having to scan
1501*9880d681SAndroid Build Coastguard Worker // the entire block to get to this point.
1502*9880d681SAndroid Build Coastguard Worker MemDepResult NewDirtyVal;
1503*9880d681SAndroid Build Coastguard Worker if (!RemInst->isTerminator())
1504*9880d681SAndroid Build Coastguard Worker NewDirtyVal = MemDepResult::getDirty(&*++RemInst->getIterator());
1505*9880d681SAndroid Build Coastguard Worker
1506*9880d681SAndroid Build Coastguard Worker ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
1507*9880d681SAndroid Build Coastguard Worker if (ReverseDepIt != ReverseLocalDeps.end()) {
1508*9880d681SAndroid Build Coastguard Worker // RemInst can't be the terminator if it has local stuff depending on it.
1509*9880d681SAndroid Build Coastguard Worker assert(!ReverseDepIt->second.empty() && !isa<TerminatorInst>(RemInst) &&
1510*9880d681SAndroid Build Coastguard Worker "Nothing can locally depend on a terminator");
1511*9880d681SAndroid Build Coastguard Worker
1512*9880d681SAndroid Build Coastguard Worker for (Instruction *InstDependingOnRemInst : ReverseDepIt->second) {
1513*9880d681SAndroid Build Coastguard Worker assert(InstDependingOnRemInst != RemInst &&
1514*9880d681SAndroid Build Coastguard Worker "Already removed our local dep info");
1515*9880d681SAndroid Build Coastguard Worker
1516*9880d681SAndroid Build Coastguard Worker LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
1517*9880d681SAndroid Build Coastguard Worker
1518*9880d681SAndroid Build Coastguard Worker // Make sure to remember that new things depend on NewDepInst.
1519*9880d681SAndroid Build Coastguard Worker assert(NewDirtyVal.getInst() &&
1520*9880d681SAndroid Build Coastguard Worker "There is no way something else can have "
1521*9880d681SAndroid Build Coastguard Worker "a local dep on this if it is a terminator!");
1522*9880d681SAndroid Build Coastguard Worker ReverseDepsToAdd.push_back(
1523*9880d681SAndroid Build Coastguard Worker std::make_pair(NewDirtyVal.getInst(), InstDependingOnRemInst));
1524*9880d681SAndroid Build Coastguard Worker }
1525*9880d681SAndroid Build Coastguard Worker
1526*9880d681SAndroid Build Coastguard Worker ReverseLocalDeps.erase(ReverseDepIt);
1527*9880d681SAndroid Build Coastguard Worker
1528*9880d681SAndroid Build Coastguard Worker // Add new reverse deps after scanning the set, to avoid invalidating the
1529*9880d681SAndroid Build Coastguard Worker // 'ReverseDeps' reference.
1530*9880d681SAndroid Build Coastguard Worker while (!ReverseDepsToAdd.empty()) {
1531*9880d681SAndroid Build Coastguard Worker ReverseLocalDeps[ReverseDepsToAdd.back().first].insert(
1532*9880d681SAndroid Build Coastguard Worker ReverseDepsToAdd.back().second);
1533*9880d681SAndroid Build Coastguard Worker ReverseDepsToAdd.pop_back();
1534*9880d681SAndroid Build Coastguard Worker }
1535*9880d681SAndroid Build Coastguard Worker }
1536*9880d681SAndroid Build Coastguard Worker
1537*9880d681SAndroid Build Coastguard Worker ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1538*9880d681SAndroid Build Coastguard Worker if (ReverseDepIt != ReverseNonLocalDeps.end()) {
1539*9880d681SAndroid Build Coastguard Worker for (Instruction *I : ReverseDepIt->second) {
1540*9880d681SAndroid Build Coastguard Worker assert(I != RemInst && "Already removed NonLocalDep info for RemInst");
1541*9880d681SAndroid Build Coastguard Worker
1542*9880d681SAndroid Build Coastguard Worker PerInstNLInfo &INLD = NonLocalDeps[I];
1543*9880d681SAndroid Build Coastguard Worker // The information is now dirty!
1544*9880d681SAndroid Build Coastguard Worker INLD.second = true;
1545*9880d681SAndroid Build Coastguard Worker
1546*9880d681SAndroid Build Coastguard Worker for (auto &Entry : INLD.first) {
1547*9880d681SAndroid Build Coastguard Worker if (Entry.getResult().getInst() != RemInst)
1548*9880d681SAndroid Build Coastguard Worker continue;
1549*9880d681SAndroid Build Coastguard Worker
1550*9880d681SAndroid Build Coastguard Worker // Convert to a dirty entry for the subsequent instruction.
1551*9880d681SAndroid Build Coastguard Worker Entry.setResult(NewDirtyVal);
1552*9880d681SAndroid Build Coastguard Worker
1553*9880d681SAndroid Build Coastguard Worker if (Instruction *NextI = NewDirtyVal.getInst())
1554*9880d681SAndroid Build Coastguard Worker ReverseDepsToAdd.push_back(std::make_pair(NextI, I));
1555*9880d681SAndroid Build Coastguard Worker }
1556*9880d681SAndroid Build Coastguard Worker }
1557*9880d681SAndroid Build Coastguard Worker
1558*9880d681SAndroid Build Coastguard Worker ReverseNonLocalDeps.erase(ReverseDepIt);
1559*9880d681SAndroid Build Coastguard Worker
1560*9880d681SAndroid Build Coastguard Worker // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1561*9880d681SAndroid Build Coastguard Worker while (!ReverseDepsToAdd.empty()) {
1562*9880d681SAndroid Build Coastguard Worker ReverseNonLocalDeps[ReverseDepsToAdd.back().first].insert(
1563*9880d681SAndroid Build Coastguard Worker ReverseDepsToAdd.back().second);
1564*9880d681SAndroid Build Coastguard Worker ReverseDepsToAdd.pop_back();
1565*9880d681SAndroid Build Coastguard Worker }
1566*9880d681SAndroid Build Coastguard Worker }
1567*9880d681SAndroid Build Coastguard Worker
1568*9880d681SAndroid Build Coastguard Worker // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1569*9880d681SAndroid Build Coastguard Worker // value in the NonLocalPointerDeps info.
1570*9880d681SAndroid Build Coastguard Worker ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1571*9880d681SAndroid Build Coastguard Worker ReverseNonLocalPtrDeps.find(RemInst);
1572*9880d681SAndroid Build Coastguard Worker if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
1573*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<Instruction *, ValueIsLoadPair>, 8>
1574*9880d681SAndroid Build Coastguard Worker ReversePtrDepsToAdd;
1575*9880d681SAndroid Build Coastguard Worker
1576*9880d681SAndroid Build Coastguard Worker for (ValueIsLoadPair P : ReversePtrDepIt->second) {
1577*9880d681SAndroid Build Coastguard Worker assert(P.getPointer() != RemInst &&
1578*9880d681SAndroid Build Coastguard Worker "Already removed NonLocalPointerDeps info for RemInst");
1579*9880d681SAndroid Build Coastguard Worker
1580*9880d681SAndroid Build Coastguard Worker NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;
1581*9880d681SAndroid Build Coastguard Worker
1582*9880d681SAndroid Build Coastguard Worker // The cache is not valid for any specific block anymore.
1583*9880d681SAndroid Build Coastguard Worker NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();
1584*9880d681SAndroid Build Coastguard Worker
1585*9880d681SAndroid Build Coastguard Worker // Update any entries for RemInst to use the instruction after it.
1586*9880d681SAndroid Build Coastguard Worker for (auto &Entry : NLPDI) {
1587*9880d681SAndroid Build Coastguard Worker if (Entry.getResult().getInst() != RemInst)
1588*9880d681SAndroid Build Coastguard Worker continue;
1589*9880d681SAndroid Build Coastguard Worker
1590*9880d681SAndroid Build Coastguard Worker // Convert to a dirty entry for the subsequent instruction.
1591*9880d681SAndroid Build Coastguard Worker Entry.setResult(NewDirtyVal);
1592*9880d681SAndroid Build Coastguard Worker
1593*9880d681SAndroid Build Coastguard Worker if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1594*9880d681SAndroid Build Coastguard Worker ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1595*9880d681SAndroid Build Coastguard Worker }
1596*9880d681SAndroid Build Coastguard Worker
1597*9880d681SAndroid Build Coastguard Worker // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1598*9880d681SAndroid Build Coastguard Worker // subsequent value may invalidate the sortedness.
1599*9880d681SAndroid Build Coastguard Worker std::sort(NLPDI.begin(), NLPDI.end());
1600*9880d681SAndroid Build Coastguard Worker }
1601*9880d681SAndroid Build Coastguard Worker
1602*9880d681SAndroid Build Coastguard Worker ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
1603*9880d681SAndroid Build Coastguard Worker
1604*9880d681SAndroid Build Coastguard Worker while (!ReversePtrDepsToAdd.empty()) {
1605*9880d681SAndroid Build Coastguard Worker ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first].insert(
1606*9880d681SAndroid Build Coastguard Worker ReversePtrDepsToAdd.back().second);
1607*9880d681SAndroid Build Coastguard Worker ReversePtrDepsToAdd.pop_back();
1608*9880d681SAndroid Build Coastguard Worker }
1609*9880d681SAndroid Build Coastguard Worker }
1610*9880d681SAndroid Build Coastguard Worker
1611*9880d681SAndroid Build Coastguard Worker assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
1612*9880d681SAndroid Build Coastguard Worker DEBUG(verifyRemoved(RemInst));
1613*9880d681SAndroid Build Coastguard Worker }
1614*9880d681SAndroid Build Coastguard Worker
1615*9880d681SAndroid Build Coastguard Worker /// Verify that the specified instruction does not occur in our internal data
1616*9880d681SAndroid Build Coastguard Worker /// structures.
1617*9880d681SAndroid Build Coastguard Worker ///
1618*9880d681SAndroid Build Coastguard Worker /// This function verifies by asserting in debug builds.
verifyRemoved(Instruction * D) const1619*9880d681SAndroid Build Coastguard Worker void MemoryDependenceResults::verifyRemoved(Instruction *D) const {
1620*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
1621*9880d681SAndroid Build Coastguard Worker for (const auto &DepKV : LocalDeps) {
1622*9880d681SAndroid Build Coastguard Worker assert(DepKV.first != D && "Inst occurs in data structures");
1623*9880d681SAndroid Build Coastguard Worker assert(DepKV.second.getInst() != D && "Inst occurs in data structures");
1624*9880d681SAndroid Build Coastguard Worker }
1625*9880d681SAndroid Build Coastguard Worker
1626*9880d681SAndroid Build Coastguard Worker for (const auto &DepKV : NonLocalPointerDeps) {
1627*9880d681SAndroid Build Coastguard Worker assert(DepKV.first.getPointer() != D && "Inst occurs in NLPD map key");
1628*9880d681SAndroid Build Coastguard Worker for (const auto &Entry : DepKV.second.NonLocalDeps)
1629*9880d681SAndroid Build Coastguard Worker assert(Entry.getResult().getInst() != D && "Inst occurs as NLPD value");
1630*9880d681SAndroid Build Coastguard Worker }
1631*9880d681SAndroid Build Coastguard Worker
1632*9880d681SAndroid Build Coastguard Worker for (const auto &DepKV : NonLocalDeps) {
1633*9880d681SAndroid Build Coastguard Worker assert(DepKV.first != D && "Inst occurs in data structures");
1634*9880d681SAndroid Build Coastguard Worker const PerInstNLInfo &INLD = DepKV.second;
1635*9880d681SAndroid Build Coastguard Worker for (const auto &Entry : INLD.first)
1636*9880d681SAndroid Build Coastguard Worker assert(Entry.getResult().getInst() != D &&
1637*9880d681SAndroid Build Coastguard Worker "Inst occurs in data structures");
1638*9880d681SAndroid Build Coastguard Worker }
1639*9880d681SAndroid Build Coastguard Worker
1640*9880d681SAndroid Build Coastguard Worker for (const auto &DepKV : ReverseLocalDeps) {
1641*9880d681SAndroid Build Coastguard Worker assert(DepKV.first != D && "Inst occurs in data structures");
1642*9880d681SAndroid Build Coastguard Worker for (Instruction *Inst : DepKV.second)
1643*9880d681SAndroid Build Coastguard Worker assert(Inst != D && "Inst occurs in data structures");
1644*9880d681SAndroid Build Coastguard Worker }
1645*9880d681SAndroid Build Coastguard Worker
1646*9880d681SAndroid Build Coastguard Worker for (const auto &DepKV : ReverseNonLocalDeps) {
1647*9880d681SAndroid Build Coastguard Worker assert(DepKV.first != D && "Inst occurs in data structures");
1648*9880d681SAndroid Build Coastguard Worker for (Instruction *Inst : DepKV.second)
1649*9880d681SAndroid Build Coastguard Worker assert(Inst != D && "Inst occurs in data structures");
1650*9880d681SAndroid Build Coastguard Worker }
1651*9880d681SAndroid Build Coastguard Worker
1652*9880d681SAndroid Build Coastguard Worker for (const auto &DepKV : ReverseNonLocalPtrDeps) {
1653*9880d681SAndroid Build Coastguard Worker assert(DepKV.first != D && "Inst occurs in rev NLPD map");
1654*9880d681SAndroid Build Coastguard Worker
1655*9880d681SAndroid Build Coastguard Worker for (ValueIsLoadPair P : DepKV.second)
1656*9880d681SAndroid Build Coastguard Worker assert(P != ValueIsLoadPair(D, false) && P != ValueIsLoadPair(D, true) &&
1657*9880d681SAndroid Build Coastguard Worker "Inst occurs in ReverseNonLocalPtrDeps map");
1658*9880d681SAndroid Build Coastguard Worker }
1659*9880d681SAndroid Build Coastguard Worker #endif
1660*9880d681SAndroid Build Coastguard Worker }
1661*9880d681SAndroid Build Coastguard Worker
1662*9880d681SAndroid Build Coastguard Worker char MemoryDependenceAnalysis::PassID;
1663*9880d681SAndroid Build Coastguard Worker
1664*9880d681SAndroid Build Coastguard Worker MemoryDependenceResults
run(Function & F,AnalysisManager<Function> & AM)1665*9880d681SAndroid Build Coastguard Worker MemoryDependenceAnalysis::run(Function &F, AnalysisManager<Function> &AM) {
1666*9880d681SAndroid Build Coastguard Worker auto &AA = AM.getResult<AAManager>(F);
1667*9880d681SAndroid Build Coastguard Worker auto &AC = AM.getResult<AssumptionAnalysis>(F);
1668*9880d681SAndroid Build Coastguard Worker auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1669*9880d681SAndroid Build Coastguard Worker auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1670*9880d681SAndroid Build Coastguard Worker return MemoryDependenceResults(AA, AC, TLI, DT);
1671*9880d681SAndroid Build Coastguard Worker }
1672*9880d681SAndroid Build Coastguard Worker
1673*9880d681SAndroid Build Coastguard Worker char MemoryDependenceWrapperPass::ID = 0;
1674*9880d681SAndroid Build Coastguard Worker
1675*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(MemoryDependenceWrapperPass, "memdep",
1676*9880d681SAndroid Build Coastguard Worker "Memory Dependence Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)1677*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1678*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1679*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1680*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1681*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(MemoryDependenceWrapperPass, "memdep",
1682*9880d681SAndroid Build Coastguard Worker "Memory Dependence Analysis", false, true)
1683*9880d681SAndroid Build Coastguard Worker
1684*9880d681SAndroid Build Coastguard Worker MemoryDependenceWrapperPass::MemoryDependenceWrapperPass() : FunctionPass(ID) {
1685*9880d681SAndroid Build Coastguard Worker initializeMemoryDependenceWrapperPassPass(*PassRegistry::getPassRegistry());
1686*9880d681SAndroid Build Coastguard Worker }
~MemoryDependenceWrapperPass()1687*9880d681SAndroid Build Coastguard Worker MemoryDependenceWrapperPass::~MemoryDependenceWrapperPass() {}
1688*9880d681SAndroid Build Coastguard Worker
releaseMemory()1689*9880d681SAndroid Build Coastguard Worker void MemoryDependenceWrapperPass::releaseMemory() {
1690*9880d681SAndroid Build Coastguard Worker MemDep.reset();
1691*9880d681SAndroid Build Coastguard Worker }
1692*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const1693*9880d681SAndroid Build Coastguard Worker void MemoryDependenceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1694*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
1695*9880d681SAndroid Build Coastguard Worker AU.addRequired<AssumptionCacheTracker>();
1696*9880d681SAndroid Build Coastguard Worker AU.addRequired<DominatorTreeWrapperPass>();
1697*9880d681SAndroid Build Coastguard Worker AU.addRequiredTransitive<AAResultsWrapperPass>();
1698*9880d681SAndroid Build Coastguard Worker AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
1699*9880d681SAndroid Build Coastguard Worker }
1700*9880d681SAndroid Build Coastguard Worker
runOnFunction(Function & F)1701*9880d681SAndroid Build Coastguard Worker bool MemoryDependenceWrapperPass::runOnFunction(Function &F) {
1702*9880d681SAndroid Build Coastguard Worker auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
1703*9880d681SAndroid Build Coastguard Worker auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1704*9880d681SAndroid Build Coastguard Worker auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1705*9880d681SAndroid Build Coastguard Worker auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1706*9880d681SAndroid Build Coastguard Worker MemDep.emplace(AA, AC, TLI, DT);
1707*9880d681SAndroid Build Coastguard Worker return false;
1708*9880d681SAndroid Build Coastguard Worker }
1709