llvm.org GIT mirror llvm / b2143b6
Remove the experimental AliasAnalysis::getDependency interface, which isn't a good level of abstraction for memdep. Instead, generalize AliasAnalysis::alias and related interfaces with a new Location class for describing a memory location. For now, this is the same Pointer and Size as before, plus an additional field for a TBAA tag. Also, introduce a fixed MD_tbaa metadata tag kind. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@113858 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
13 changed file(s) with 264 addition(s) and 518 deletion(s). Raw diff Collapse all Expand all
8282 /// Alias Queries...
8383 ///
8484
85 /// Location - A description of a memory location.
86 struct Location {
87 /// Ptr - The address of the start of the location.
88 const Value *Ptr;
89 /// Size - The size of the location.
90 unsigned Size;
91 /// TBAATag - The metadata node which describes the TBAA type of
92 /// the location, or null if there is no (unique) tag.
93 const MDNode *TBAATag;
94
95 explicit Location(const Value *P = 0,
96 unsigned S = UnknownSize,
97 const MDNode *N = 0)
98 : Ptr(P), Size(S), TBAATag(N) {}
99 };
100
85101 /// Alias analysis result - Either we know for sure that it does not alias, we
86102 /// know for sure it must alias, or we don't know anything: The two pointers
87103 /// _might_ alias. This enum is designed so you can do things like:
97113 /// Returns a Result indicating whether the two pointers are aliased to each
98114 /// other. This is the interface that must be implemented by specific alias
99115 /// analysis implementations.
100 ///
101 virtual AliasResult alias(const Value *V1, unsigned V1Size,
102 const Value *V2, unsigned V2Size);
103
104 /// alias - A convenience wrapper for the case where the sizes are unknown.
116 virtual AliasResult alias(const Location &LocA, const Location &LocB);
117
118 /// alias - A convenience wrapper.
119 AliasResult alias(const Value *V1, unsigned V1Size,
120 const Value *V2, unsigned V2Size) {
121 return alias(Location(V1, V1Size), Location(V2, V2Size));
122 }
123
124 /// alias - A convenience wrapper.
105125 AliasResult alias(const Value *V1, const Value *V2) {
106126 return alias(V1, UnknownSize, V2, UnknownSize);
107127 }
108128
109129 /// isNoAlias - A trivial helper function to check to see if the specified
110130 /// pointers are no-alias.
131 bool isNoAlias(const Location &LocA, const Location &LocB) {
132 return alias(LocA, LocB) == NoAlias;
133 }
134
135 /// isNoAlias - A convenience wrapper.
111136 bool isNoAlias(const Value *V1, unsigned V1Size,
112137 const Value *V2, unsigned V2Size) {
113 return alias(V1, V1Size, V2, V2Size) == NoAlias;
114 }
115
116 /// pointsToConstantMemory - If the specified pointer is known to point into
117 /// constant global memory, return true. This allows disambiguation of store
138 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
139 }
140
141 /// pointsToConstantMemory - If the specified memory location is known to be
142 /// constant, return true. This allows disambiguation of store
118143 /// instructions from constant pointers.
119144 ///
120 virtual bool pointsToConstantMemory(const Value *P);
145 virtual bool pointsToConstantMemory(const Location &Loc);
146
147 /// pointsToConstantMemory - A convenient wrapper.
148 bool pointsToConstantMemory(const Value *P) {
149 return pointsToConstantMemory(Location(P));
150 }
121151
122152 //===--------------------------------------------------------------------===//
123153 /// Simple mod/ref information...
219249
220250
221251 /// getModRefInfo - Return information about whether or not an instruction may
222 /// read or write memory specified by the pointer operand. An instruction
252 /// read or write the specified memory location. An instruction
223253 /// that doesn't read or write memory may be trivially LICM'd for example.
224254 ModRefResult getModRefInfo(const Instruction *I,
225 const Value *P, unsigned Size) {
255 const Location &Loc) {
226256 switch (I->getOpcode()) {
227 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, P,Size);
228 case Instruction::Load: return getModRefInfo((const LoadInst*)I, P, Size);
229 case Instruction::Store: return getModRefInfo((const StoreInst*)I, P,Size);
230 case Instruction::Call: return getModRefInfo((const CallInst*)I, P, Size);
231 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,P,Size);
257 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
258 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
259 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
260 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
261 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
232262 default: return NoModRef;
233263 }
234264 }
235265
266 /// getModRefInfo - A convenience wrapper.
267 ModRefResult getModRefInfo(const Instruction *I,
268 const Value *P, unsigned Size) {
269 return getModRefInfo(I, Location(P, Size));
270 }
271
236272 /// getModRefInfo (for call sites) - Return whether information about whether
237 /// a particular call site modifies or reads the memory specified by the
238 /// pointer.
273 /// a particular call site modifies or reads the specified memory location.
239274 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
240 const Value *P, unsigned Size);
275 const Location &Loc);
276
277 /// getModRefInfo (for call sites) - A convenience wrapper.
278 ModRefResult getModRefInfo(ImmutableCallSite CS,
279 const Value *P, unsigned Size) {
280 return getModRefInfo(CS, Location(P, Size));
281 }
241282
242283 /// getModRefInfo (for calls) - Return whether information about whether
243 /// a particular call modifies or reads the memory specified by the
244 /// pointer.
284 /// a particular call modifies or reads the specified memory location.
285 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
286 return getModRefInfo(ImmutableCallSite(C), Loc);
287 }
288
289 /// getModRefInfo (for calls) - A convenience wrapper.
245290 ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) {
246 return getModRefInfo(ImmutableCallSite(C), P, Size);
291 return getModRefInfo(C, Location(P, Size));
247292 }
248293
249294 /// getModRefInfo (for invokes) - Return whether information about whether
250 /// a particular invoke modifies or reads the memory specified by the
251 /// pointer.
295 /// a particular invoke modifies or reads the specified memory location.
296 ModRefResult getModRefInfo(const InvokeInst *I,
297 const Location &Loc) {
298 return getModRefInfo(ImmutableCallSite(I), Loc);
299 }
300
301 /// getModRefInfo (for invokes) - A convenience wrapper.
252302 ModRefResult getModRefInfo(const InvokeInst *I,
253303 const Value *P, unsigned Size) {
254 return getModRefInfo(ImmutableCallSite(I), P, Size);
304 return getModRefInfo(I, Location(P, Size));
255305 }
256306
257307 /// getModRefInfo (for loads) - Return whether information about whether
258 /// a particular load modifies or reads the memory specified by the
259 /// pointer.
260 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size);
308 /// a particular load modifies or reads the specified memory location.
309 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
310
311 /// getModRefInfo (for loads) - A convenience wrapper.
312 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
313 return getModRefInfo(L, Location(P, Size));
314 }
261315
262316 /// getModRefInfo (for stores) - Return whether information about whether
263 /// a particular store modifies or reads the memory specified by the
264 /// pointer.
265 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size);
317 /// a particular store modifies or reads the specified memory location.
318 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
319
320 /// getModRefInfo (for stores) - A convenience wrapper.
321 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
322 return getModRefInfo(S, Location(P, Size));
323 }
266324
267325 /// getModRefInfo (for va_args) - Return whether information about whether
268 /// a particular va_arg modifies or reads the memory specified by the
269 /// pointer.
270 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, unsigned Size);
326 /// a particular va_arg modifies or reads the specified memory location.
327 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
328
329 /// getModRefInfo (for va_args) - A convenience wrapper.
330 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, unsigned Size) {
331 return getModRefInfo(I, Location(P, Size));
332 }
271333
272334 /// getModRefInfo - Return information about whether two call sites may refer
273335 /// to the same set of memory locations. See
277339 ImmutableCallSite CS2);
278340
279341 //===--------------------------------------------------------------------===//
280 /// Dependence queries.
281 ///
282
283 /// DependenceResult - These are the return values for getDependence queries.
284 /// They are defined in terms of "memory", but they are also used to model
285 /// other side effects, such as I/O and volatility.
286 enum DependenceResult {
287 /// ReadThenRead - The instructions are ReadThenReadSome and the second
288 /// instruction reads from exactly the same memory read from by the first.
289 ReadThenRead,
290
291 /// ReadThenReadSome - The instructions are Independent, both are read-only,
292 /// and the second instruction reads from a subset of the memory read from
293 /// by the first.
294 ReadThenReadSome,
295
296 /// Independent - Neither instruction reads from or writes to memory written
297 /// to by the other. All enum values lower than this one are special cases
298 /// of Indepenent.
299 Independent,
300
301 /// WriteThenRead - The instructions are WriteThenReadSome and the second
302 /// instruction reads from exactly the same memory written by the first.
303 WriteThenRead,
304
305 /// WriteThenReadSome - The first instruction is write-only, the second
306 /// instruction is read-only, and the second only reads from memory
307 /// written to by the first.
308 WriteThenReadSome,
309
310 /// ReadThenWrite - The first instruction is read-only, the second
311 /// instruction is write-only, and the second wrotes to exactly the
312 /// same memory read from by the first.
313 ReadThenWrite,
314
315 /// WriteThenWrite - The instructions are WriteThenWriteSome, and the
316 /// second instruction writes to exactly the same memory written to by
317 /// the first.
318 WriteThenWrite,
319
320 /// WriteSomeThenWrite - Both instructions are write-only, and the second
321 /// instruction writes to a superset of the memory written to by the first.
322 WriteSomeThenWrite,
323
324 /// Unknown - The relationship between the instructions cannot be
325 /// determined or does not fit into any of the cases defined here.
326 Unknown
327 };
328
329 /// DependenceQueryFlags - Flags for refining dependence queries.
330 enum DependenceQueryFlags {
331 Default = 0,
332 IgnoreLoads = 1,
333 IgnoreStores = 2
334 };
335
336 /// getDependence - Determine the dependence relationship between the
337 /// instructions. This does not include "register" dependencies; it just
338 /// considers memory references and other side effects.
339 /// WARNING: This is an experimental interface.
340 DependenceResult getDependence(const Instruction *First,
341 const Instruction *Second) {
342 return getDependence(First, 0, Default, Second, 0, Default);
343 }
344
345 /// getDependence - Determine the dependence relationship between the
346 /// instructions. This does not include "register" dependencies; it just
347 /// considers memory references and other side effects. This overload
348 /// has additional parameters to allow phi-translated addresses to be
349 /// specified, and additional flags to refine the query.
350 /// WARNING: This is an experimental interface.
351 virtual DependenceResult getDependence(const Instruction *First,
352 const Value *FirstPHITranslatedAddr,
353 DependenceQueryFlags FirstFlags,
354 const Instruction *Second,
355 const Value *SecondPHITranslatedAddr,
356 DependenceQueryFlags SecondFlags);
357
358 //===--------------------------------------------------------------------===//
359342 /// Higher level methods for querying mod/ref information.
360343 ///
361344
362345 /// canBasicBlockModify - Return true if it is possible for execution of the
363346 /// specified basic block to modify the value pointed to by Ptr.
364 ///
365 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
347 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
348
349 /// canBasicBlockModify - A convenience wrapper.
350 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size){
351 return canBasicBlockModify(BB, Location(P, Size));
352 }
366353
367354 /// canInstructionRangeModify - Return true if it is possible for the
368355 /// execution of the specified instructions to modify the value pointed to by
369356 /// Ptr. The instructions to consider are all of the instructions in the
370357 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
371 ///
372358 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
373 const Value *Ptr, unsigned Size);
359 const Location &Loc);
360
361 /// canInstructionRangeModify - A convenience wrapper.
362 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
363 const Value *Ptr, unsigned Size) {
364 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
365 }
374366
375367 //===--------------------------------------------------------------------===//
376368 /// Methods that clients should call when they transform the program to allow
400392 copyValue(Old, New);
401393 deleteValue(Old);
402394 }
403
404 protected:
405 /// getDependenceViaModRefInfo - Helper function for implementing getDependence
406 /// in implementations which already have getModRefInfo implementations.
407 DependenceResult getDependenceViaModRefInfo(const Instruction *First,
408 const Value *FirstPHITranslatedAddr,
409 DependenceQueryFlags FirstFlags,
410 const Instruction *Second,
411 const Value *SecondPHITranslatedAddr,
412 DependenceQueryFlags SecondFlags);
413
414395 };
415396
416397 /// isNoAliasCall - Return true if this pointer is returned by a noalias
3535 ~LibCallAliasAnalysis();
3636
3737 ModRefResult getModRefInfo(ImmutableCallSite CS,
38 const Value *P, unsigned Size);
38 const Location &Loc);
3939
4040 ModRefResult getModRefInfo(ImmutableCallSite CS1,
4141 ImmutableCallSite CS2) {
6363 private:
6464 ModRefResult AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
6565 ImmutableCallSite CS,
66 const Value *P, unsigned Size);
66 const Location &Loc);
6767 };
6868 } // End of llvm namespace
6969
4747 Yes, No, Unknown
4848 };
4949 LocResult (*isLocation)(ImmutableCallSite CS,
50 const Value *Ptr, unsigned Size);
50 const AliasAnalysis::Location &Loc);
5151 };
5252
5353 /// LibCallFunctionInfo - Each record in the array of FunctionInfo structs
3636 // Pinned metadata names, which always have the same value. This is a
3737 // compile-time performance optimization, not a correctness optimization.
3838 enum {
39 MD_dbg = 0 // "dbg"
39 MD_dbg = 0, // "dbg"
40 MD_tbaa = 1 // "tbaa"
4041 };
4142
4243 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
2929 #include "llvm/Function.h"
3030 #include "llvm/IntrinsicInst.h"
3131 #include "llvm/Instructions.h"
32 #include "llvm/LLVMContext.h"
3233 #include "llvm/Type.h"
3334 #include "llvm/Target/TargetData.h"
3435 using namespace llvm;
4243 //===----------------------------------------------------------------------===//
4344
4445 AliasAnalysis::AliasResult
45 AliasAnalysis::alias(const Value *V1, unsigned V1Size,
46 const Value *V2, unsigned V2Size) {
47 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
48 return AA->alias(V1, V1Size, V2, V2Size);
49 }
50
51 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
52 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
53 return AA->pointsToConstantMemory(P);
46 AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
47 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
48 return AA->alias(LocA, LocB);
49 }
50
51 bool AliasAnalysis::pointsToConstantMemory(const Location &Loc) {
52 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
53 return AA->pointsToConstantMemory(Loc);
5454 }
5555
5656 void AliasAnalysis::deleteValue(Value *V) {
6565
6666 AliasAnalysis::ModRefResult
6767 AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
68 const Value *P, unsigned Size) {
68 const Location &Loc) {
6969 // Don't assert AA because BasicAA calls us in order to make use of the
7070 // logic here.
7171
8080 bool doesAlias = false;
8181 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
8282 AI != AE; ++AI)
83 if (!isNoAlias(*AI, ~0U, P, Size)) {
83 if (!isNoAlias(Location(*AI), Loc)) {
8484 doesAlias = true;
8585 break;
8686 }
8989 return NoModRef;
9090 }
9191
92 // If P points to a constant memory location, the call definitely could not
92 // If Loc is a constant memory location, the call definitely could not
9393 // modify the memory location.
94 if ((Mask & Mod) && pointsToConstantMemory(P))
94 if ((Mask & Mod) && pointsToConstantMemory(Loc))
9595 Mask = ModRefResult(Mask & ~Mod);
9696
9797 // If this is BasicAA, don't forward.
9999
100100 // Otherwise, fall back to the next AA in the chain. But we can merge
101101 // in any mask we've managed to compute.
102 return ModRefResult(AA->getModRefInfo(CS, P, Size) & Mask);
102 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
103103 }
104104
105105 AliasAnalysis::ModRefResult
187187 return AA->getModRefBehavior(F);
188188 }
189189
190 AliasAnalysis::DependenceResult
191 AliasAnalysis::getDependence(const Instruction *First,
192 const Value *FirstPHITranslatedAddr,
193 DependenceQueryFlags FirstFlags,
194 const Instruction *Second,
195 const Value *SecondPHITranslatedAddr,
196 DependenceQueryFlags SecondFlags) {
197 assert(AA && "AA didn't call InitializeAliasAnalyais in its run method!");
198 return AA->getDependence(First, FirstPHITranslatedAddr, FirstFlags,
199 Second, SecondPHITranslatedAddr, SecondFlags);
200 }
201
202190 //===----------------------------------------------------------------------===//
203191 // AliasAnalysis non-virtual helper method implementation
204192 //===----------------------------------------------------------------------===//
205193
206194 AliasAnalysis::ModRefResult
207 AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
195 AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
208196 // Be conservative in the face of volatile.
209197 if (L->isVolatile())
210198 return ModRef;
211199
212200 // If the load address doesn't alias the given address, it doesn't read
213201 // or write the specified memory.
214 if (!alias(L->getOperand(0), getTypeStoreSize(L->getType()), P, Size))
202 if (!alias(Location(L->getOperand(0),
203 getTypeStoreSize(L->getType()),
204 L->getMetadata(LLVMContext::MD_tbaa)),
205 Loc))
215206 return NoModRef;
216207
217208 // Otherwise, a load just reads.
219210 }
220211
221212 AliasAnalysis::ModRefResult
222 AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
213 AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
223214 // Be conservative in the face of volatile.
224215 if (S->isVolatile())
225216 return ModRef;
226217
227218 // If the store address cannot alias the pointer in question, then the
228219 // specified memory cannot be modified by the store.
229 if (!alias(S->getOperand(1),
230 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
220 if (!alias(Location(S->getOperand(1),
221 getTypeStoreSize(S->getOperand(0)->getType()),
222 S->getMetadata(LLVMContext::MD_tbaa)),
223 Loc))
231224 return NoModRef;
232225
233226 // If the pointer is a pointer to constant memory, then it could not have been
234227 // modified by this store.
235 if (pointsToConstantMemory(P))
228 if (pointsToConstantMemory(Loc))
236229 return NoModRef;
237230
238231 // Otherwise, a store just writes.
240233 }
241234
242235 AliasAnalysis::ModRefResult
243 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Value *P, unsigned Size) {
236 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
244237 // If the va_arg address cannot alias the pointer in question, then the
245238 // specified memory cannot be accessed by the va_arg.
246 if (!alias(V->getOperand(0), UnknownSize, P, Size))
239 if (!alias(Location(V->getOperand(0),
240 UnknownSize,
241 V->getMetadata(LLVMContext::MD_tbaa)),
242 Loc))
247243 return NoModRef;
248244
249245 // If the pointer is a pointer to constant memory, then it could not have been
250246 // modified by this va_arg.
251 if (pointsToConstantMemory(P))
247 if (pointsToConstantMemory(Loc))
252248 return NoModRef;
253249
254250 // Otherwise, a va_arg reads and writes.
255251 return ModRef;
256 }
257
258 AliasAnalysis::DependenceResult
259 AliasAnalysis::getDependenceViaModRefInfo(const Instruction *First,
260 const Value *FirstPHITranslatedAddr,
261 DependenceQueryFlags FirstFlags,
262 const Instruction *Second,
263 const Value *SecondPHITranslatedAddr,
264 DependenceQueryFlags SecondFlags) {
265 if (const LoadInst *L = dyn_cast(First)) {
266 // Be over-conservative with volatile for now.
267 if (L->isVolatile())
268 return Unknown;
269
270 // If we don't have a phi-translated address, use the actual one.
271 if (!FirstPHITranslatedAddr)
272 FirstPHITranslatedAddr = L->getPointerOperand();
273
274 // Forward this query to getModRefInfo.
275 switch (getModRefInfo(Second,
276 FirstPHITranslatedAddr,
277 getTypeStoreSize(L->getType()))) {
278 case NoModRef:
279 // Second doesn't reference First's memory, so they're independent.
280 return Independent;
281
282 case Ref:
283 // Second only reads from the memory read from by First. If it
284 // also writes to any other memory, be conservative.
285 if (Second->mayWriteToMemory())
286 return Unknown;
287
288 // If it's loading the same size from the same address, we can
289 // give a more precise result.
290 if (const LoadInst *SecondL = dyn_cast(Second)) {
291 // If we don't have a phi-translated address, use the actual one.
292 if (!SecondPHITranslatedAddr)
293 SecondPHITranslatedAddr = SecondL->getPointerOperand();
294
295 unsigned LSize = getTypeStoreSize(L->getType());
296 unsigned SecondLSize = getTypeStoreSize(SecondL->getType());
297 if (alias(FirstPHITranslatedAddr, LSize,
298 SecondPHITranslatedAddr, SecondLSize) ==
299 MustAlias) {
300 // If the loads are the same size, it's ReadThenRead.
301 if (LSize == SecondLSize)
302 return ReadThenRead;
303
304 // If the second load is smaller, it's only ReadThenReadSome.
305 if (LSize > SecondLSize)
306 return ReadThenReadSome;
307 }
308 }
309
310 // Otherwise it's just two loads.
311 return Independent;
312
313 case Mod:
314 // Second only writes to the memory read from by First. If it
315 // also reads from any other memory, be conservative.
316 if (Second->mayReadFromMemory())
317 return Unknown;
318
319 // If it's storing the same size to the same address, we can
320 // give a more precise result.
321 if (const StoreInst *SecondS = dyn_cast(Second)) {
322 // If we don't have a phi-translated address, use the actual one.
323 if (!SecondPHITranslatedAddr)
324 SecondPHITranslatedAddr = SecondS->getPointerOperand();
325
326 unsigned LSize = getTypeStoreSize(L->getType());
327 unsigned SecondSSize = getTypeStoreSize(SecondS->getType());
328 if (alias(FirstPHITranslatedAddr, LSize,
329 SecondPHITranslatedAddr, SecondSSize) ==
330 MustAlias) {
331 // If the load and the store are the same size, it's ReadThenWrite.
332 if (LSize == SecondSSize)
333 return ReadThenWrite;
334 }
335 }
336
337 // Otherwise we don't know if it could be writing to other memory.
338 return Unknown;
339
340 case ModRef:
341 // Second reads and writes to the memory read from by First.
342 // We don't have a way to express that.
343 return Unknown;
344 }
345
346 } else if (const StoreInst *S = dyn_cast(First)) {
347 // Be over-conservative with volatile for now.
348 if (S->isVolatile())
349 return Unknown;
350
351 // If we don't have a phi-translated address, use the actual one.
352 if (!FirstPHITranslatedAddr)
353 FirstPHITranslatedAddr = S->getPointerOperand();
354
355 // Forward this query to getModRefInfo.
356 switch (getModRefInfo(Second,
357 FirstPHITranslatedAddr,
358 getTypeStoreSize(S->getValueOperand()->getType()))) {
359 case NoModRef:
360 // Second doesn't reference First's memory, so they're independent.
361 return Independent;
362
363 case Ref:
364 // Second only reads from the memory written to by First. If it
365 // also writes to any other memory, be conservative.
366 if (Second->mayWriteToMemory())
367 return Unknown;
368
369 // If it's loading the same size from the same address, we can
370 // give a more precise result.
371 if (const LoadInst *SecondL = dyn_cast(Second)) {
372 // If we don't have a phi-translated address, use the actual one.
373 if (!SecondPHITranslatedAddr)
374 SecondPHITranslatedAddr = SecondL->getPointerOperand();
375
376 unsigned SSize = getTypeStoreSize(S->getValueOperand()->getType());
377 unsigned SecondLSize = getTypeStoreSize(SecondL->getType());
378 if (alias(FirstPHITranslatedAddr, SSize,
379 SecondPHITranslatedAddr, SecondLSize) ==
380 MustAlias) {
381 // If the store and the load are the same size, it's WriteThenRead.
382 if (SSize == SecondLSize)
383 return WriteThenRead;
384
385 // If the load is smaller, it's only WriteThenReadSome.
386 if (SSize > SecondLSize)
387 return WriteThenReadSome;
388 }
389 }
390
391 // Otherwise we don't know if it could be reading from other memory.
392 return Unknown;
393
394 case Mod:
395 // Second only writes to the memory written to by First. If it
396 // also reads from any other memory, be conservative.
397 if (Second->mayReadFromMemory())
398 return Unknown;
399
400 // If it's storing the same size to the same address, we can
401 // give a more precise result.
402 if (const StoreInst *SecondS = dyn_cast(Second)) {
403 // If we don't have a phi-translated address, use the actual one.
404 if (!SecondPHITranslatedAddr)
405 SecondPHITranslatedAddr = SecondS->getPointerOperand();
406
407 unsigned SSize = getTypeStoreSize(S->getValueOperand()->getType());
408 unsigned SecondSSize = getTypeStoreSize(SecondS->getType());
409 if (alias(FirstPHITranslatedAddr, SSize,
410 SecondPHITranslatedAddr, SecondSSize) ==
411 MustAlias) {
412 // If the stores are the same size, it's WriteThenWrite.
413 if (SSize == SecondSSize)
414 return WriteThenWrite;
415
416 // If the second store is larger, it's only WriteSomeThenWrite.
417 if (SSize < SecondSSize)
418 return WriteSomeThenWrite;
419 }
420 }
421
422 // Otherwise we don't know if it could be writing to other memory.
423 return Unknown;
424
425 case ModRef:
426 // Second reads and writes to the memory written to by First.
427 // We don't have a way to express that.
428 return Unknown;
429 }
430
431 } else if (const VAArgInst *V = dyn_cast(First)) {
432 // If we don't have a phi-translated address, use the actual one.
433 if (!FirstPHITranslatedAddr)
434 FirstPHITranslatedAddr = V->getPointerOperand();
435
436 // Forward this query to getModRefInfo.
437 if (getModRefInfo(Second, FirstPHITranslatedAddr, UnknownSize) == NoModRef)
438 // Second doesn't reference First's memory, so they're independent.
439 return Independent;
440
441 } else if (ImmutableCallSite FirstCS = cast(First)) {
442 assert(!FirstPHITranslatedAddr &&
443 !SecondPHITranslatedAddr &&
444 "PHI translation with calls not supported yet!");
445
446 // If both instructions are calls/invokes we can use the two-callsite
447 // form of getModRefInfo.
448 if (ImmutableCallSite SecondCS = cast(Second))
449 // getModRefInfo's arguments are backwards from intuition.
450 switch (getModRefInfo(SecondCS, FirstCS)) {
451 case NoModRef:
452 // Second doesn't reference First's memory, so they're independent.
453 return Independent;
454
455 case Ref:
456 // If they're both read-only, there's no dependence.
457 if (FirstCS.onlyReadsMemory() && SecondCS.onlyReadsMemory())
458 return Independent;
459
460 // Otherwise it's not obvious what we can do here.
461 return Unknown;
462
463 case Mod:
464 // It's not obvious what we can do here.
465 return Unknown;
466
467 case ModRef:
468 // I know, right?
469 return Unknown;
470 }
471 }
472
473 // For anything else, be conservative.
474 return Unknown;
475252 }
476253
477254 AliasAnalysis::ModRefBehavior
513290 /// specified basic block to modify the value pointed to by Ptr.
514291 ///
515292 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
516 const Value *Ptr, unsigned Size) {
517 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
293 const Location &Loc) {
294 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
518295 }
519296
520297 /// canInstructionRangeModify - Return true if it is possible for the execution
524301 ///
525302 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
526303 const Instruction &I2,
527 const Value *Ptr, unsigned Size) {
304 const Location &Loc) {
528305 assert(I1.getParent() == I2.getParent() &&
529306 "Instructions not in same basic block!");
530307 BasicBlock::const_iterator I = &I1;
532309 ++E; // Convert from inclusive to exclusive range.
533310
534311 for (; I != E; ++I) // Check every instruction in range
535 if (getModRefInfo(I, Ptr, Size) & Mod)
312 if (getModRefInfo(I, Loc) & Mod)
536313 return true;
537314 return false;
538315 }
9393 }
9494
9595 // FIXME: We could count these too...
96 bool pointsToConstantMemory(const Value *P) {
97 return getAnalysis().pointsToConstantMemory(P);
96 bool pointsToConstantMemory(const Location &Loc) {
97 return getAnalysis().pointsToConstantMemory(Loc);
9898 }
9999
100100 // Forwarding functions: just delegate to a real AA implementation, counting
101101 // the number of responses...
102 AliasResult alias(const Value *V1, unsigned V1Size,
103 const Value *V2, unsigned V2Size);
102 AliasResult alias(const Location &LocA, const Location &LocB);
104103
105104 ModRefResult getModRefInfo(ImmutableCallSite CS,
106 const Value *P, unsigned Size);
105 const Location &Loc);
107106 ModRefResult getModRefInfo(ImmutableCallSite CS1,
108107 ImmutableCallSite CS2) {
109108 return AliasAnalysis::getModRefInfo(CS1,CS2);
120119 }
121120
122121 AliasAnalysis::AliasResult
123 AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
124 const Value *V2, unsigned V2Size) {
125 AliasResult R = getAnalysis().alias(V1, V1Size, V2, V2Size);
122 AliasAnalysisCounter::alias(const Location &LocA, const Location &LocB) {
123 AliasResult R = getAnalysis().alias(LocA, LocB);
126124
127125 const char *AliasString;
128126 switch (R) {
134132
135133 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
136134 errs() << AliasString << ":\t";
137 errs() << "[" << V1Size << "B] ";
138 WriteAsOperand(errs(), V1, true, M);
135 errs() << "[" << LocA.Size << "B] ";
136 WriteAsOperand(errs(), LocA.Ptr, true, M);
139137 errs() << ", ";
140 errs() << "[" << V2Size << "B] ";
141 WriteAsOperand(errs(), V2, true, M);
138 errs() << "[" << LocB.Size << "B] ";
139 WriteAsOperand(errs(), LocB.Ptr, true, M);
142140 errs() << "\n";
143141 }
144142
147145
148146 AliasAnalysis::ModRefResult
149147 AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
150 const Value *P, unsigned Size) {
151 ModRefResult R = getAnalysis().getModRefInfo(CS, P, Size);
148 const Location &Loc) {
149 ModRefResult R = getAnalysis().getModRefInfo(CS, Loc);
152150
153151 const char *MRString;
154152 switch (R) {
161159
162160 if (PrintAll || (PrintAllFailures && R == ModRef)) {
163161 errs() << MRString << ": Ptr: ";
164 errs() << "[" << Size << "B] ";
165 WriteAsOperand(errs(), P, true, M);
162 errs() << "[" << Loc.Size << "B] ";
163 WriteAsOperand(errs(), Loc.Ptr, true, M);
166164 errs() << "\t<->" << *CS.getInstruction() << '\n';
167165 }
168166 return R;
9191 //------------------------------------------------
9292 // Implement the AliasAnalysis API
9393 //
94 AliasResult alias(const Value *V1, unsigned V1Size,
95 const Value *V2, unsigned V2Size) {
96 assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before");
97 assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before");
98 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
94 AliasResult alias(const Location &LocA, const Location &LocB) {
95 assert(Vals.find(LocA.Ptr) != Vals.end() &&
96 "Never seen value in AA before");
97 assert(Vals.find(LocB.Ptr) != Vals.end() &&
98 "Never seen value in AA before");
99 return AliasAnalysis::alias(LocA, LocB);
99100 }
100101
101102 ModRefResult getModRefInfo(ImmutableCallSite CS,
102 const Value *P, unsigned Size) {
103 assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
104 return AliasAnalysis::getModRefInfo(CS, P, Size);
103 const Location &Loc) {
104 assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
105 return AliasAnalysis::getModRefInfo(CS, Loc);
105106 }
106107
107108 ModRefResult getModRefInfo(ImmutableCallSite CS1,
109110 return AliasAnalysis::getModRefInfo(CS1,CS2);
110111 }
111112
112 bool pointsToConstantMemory(const Value *P) {
113 assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
114 return AliasAnalysis::pointsToConstantMemory(P);
113 bool pointsToConstantMemory(const Location &Loc) {
114 assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
115 return AliasAnalysis::pointsToConstantMemory(Loc);
115116 }
116117
117118 virtual void deleteValue(Value *V) {
2121 #include "llvm/GlobalVariable.h"
2222 #include "llvm/Instructions.h"
2323 #include "llvm/IntrinsicInst.h"
24 #include "llvm/LLVMContext.h"
2425 #include "llvm/Operator.h"
2526 #include "llvm/Pass.h"
2627 #include "llvm/Analysis/CaptureTracking.h"
148149 TD = getAnalysisIfAvailable();
149150 }
150151
151 virtual AliasResult alias(const Value *V1, unsigned V1Size,
152 const Value *V2, unsigned V2Size) {
152 virtual AliasResult alias(const Location &LocA, const Location &LocB) {
153153 return MayAlias;
154154 }
155155
160160 return UnknownModRefBehavior;
161161 }
162162
163 virtual bool pointsToConstantMemory(const Value *P) { return false; }
163 virtual bool pointsToConstantMemory(const Location &Loc) { return false; }
164164 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
165 const Value *P, unsigned Size) {
165 const Location &Loc) {
166166 return ModRef;
167167 }
168168 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
169169 ImmutableCallSite CS2) {
170170 return ModRef;
171 }
172
173 virtual DependenceResult getDependence(const Instruction *First,
174 const Value *FirstPHITranslatedAddr,
175 DependenceQueryFlags FirstFlags,
176 const Instruction *Second,
177 const Value *SecondPHITranslatedAddr,
178 DependenceQueryFlags SecondFlags) {
179 return Unknown;
180171 }
181172
182173 virtual void deleteValue(Value *V) {}
500491 static char ID; // Class identification, replacement for typeinfo
501492 BasicAliasAnalysis() : NoAA(ID) {}
502493
503 virtual AliasResult alias(const Value *V1, unsigned V1Size,
504 const Value *V2, unsigned V2Size) {
494 virtual AliasResult alias(const Location &LocA,
495 const Location &LocB) {
505496 assert(Visited.empty() && "Visited must be cleared after use!");
506 assert(notDifferentParent(V1, V2) &&
497 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
507498 "BasicAliasAnalysis doesn't support interprocedural queries.");
508 AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size);
499 AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocB.Ptr, LocB.Size);
509500 Visited.clear();
510501 return Alias;
511502 }
512503
513504 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
514 const Value *P, unsigned Size);
505 const Location &Loc);
515506
516507 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
517508 ImmutableCallSite CS2) {
521512
522513 /// pointsToConstantMemory - Chase pointers until we find a (constant
523514 /// global) or not.
524 virtual bool pointsToConstantMemory(const Value *P);
515 virtual bool pointsToConstantMemory(const Location &Loc);
525516
526517 /// getModRefBehavior - Return the behavior when calling the given
527518 /// call site.
530521 /// getModRefBehavior - Return the behavior when calling the given function.
531522 /// For use when the call site is not known.
532523 virtual ModRefBehavior getModRefBehavior(const Function *F);
533
534 virtual DependenceResult getDependence(const Instruction *First,
535 const Value *FirstPHITranslatedAddr,
536 DependenceQueryFlags FirstFlags,
537 const Instruction *Second,
538 const Value *SecondPHITranslatedAddr,
539 DependenceQueryFlags SecondFlags);
540524
541525 /// getAdjustedAnalysisPointer - This method is used when a pass implements
542526 /// an analysis interface through multiple inheritance. If needed, it
585569
586570 /// pointsToConstantMemory - Chase pointers until we find a (constant
587571 /// global) or not.
588 bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
572 bool BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
589573 if (const GlobalVariable *GV =
590 dyn_cast(P->getUnderlyingObject()))
574 dyn_cast(Loc.Ptr->getUnderlyingObject()))
591575 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
592576 // global to be marked constant in some modules and non-constant in others.
593577 // GV may even be a declaration, not a definition.
594578 return GV->isConstant();
595579
596 return NoAA::pointsToConstantMemory(P);
580 return NoAA::pointsToConstantMemory(Loc);
597581 }
598582
599583 /// getModRefBehavior - Return the behavior when calling the given call site.
635619 /// simple "address taken" analysis on local objects.
636620 AliasAnalysis::ModRefResult
637621 BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
638 const Value *P, unsigned Size) {
639 assert(notDifferentParent(CS.getInstruction(), P) &&
622 const Location &Loc) {
623 assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) &&
640624 "AliasAnalysis query involving multiple functions!");
641625
642 const Value *Object = P->getUnderlyingObject();
643
644 // If this is a tail call and P points to a stack location, we know that
626 const Value *Object = Loc.Ptr->getUnderlyingObject();
627
628 // If this is a tail call and Loc.Ptr points to a stack location, we know that
645629 // the tail call cannot access or modify the local stack.
646630 // We cannot exclude byval arguments here; these belong to the caller of
647631 // the current function not to the current function, and a tail callee
665649 !CS.paramHasAttr(ArgNo+1, Attribute::NoCapture))
666650 continue;
667651
668 // If this is a no-capture pointer argument, see if we can tell that it
652 // If this is a no-capture pointer argument, see if we can tell that it
669653 // is impossible to alias the pointer we're checking. If not, we have to
670654 // assume that the call could touch the pointer, even though it doesn't
671655 // escape.
672 if (!isNoAlias(cast(CI), UnknownSize, P, UnknownSize)) {
656 if (!isNoAlias(Location(cast(CI)), Loc)) {
673657 PassedAsArg = true;
674658 break;
675659 }
691675 Len = LenCI->getZExtValue();
692676 Value *Dest = II->getArgOperand(0);
693677 Value *Src = II->getArgOperand(1);
694 if (isNoAlias(Dest, Len, P, Size)) {
695 if (isNoAlias(Src, Len, P, Size))
678 if (isNoAlias(Location(Dest, Len), Loc)) {
679 if (isNoAlias(Location(Src, Len), Loc))
696680 return NoModRef;
697681 return Ref;
698682 }
704688 if (ConstantInt *LenCI = dyn_cast(II->getArgOperand(2))) {
705689 unsigned Len = LenCI->getZExtValue();
706690 Value *Dest = II->getArgOperand(0);
707 if (isNoAlias(Dest, Len, P, Size))
691 if (isNoAlias(Location(Dest, Len), Loc))
708692 return NoModRef;
709693 }
710694 break;
723707 if (TD) {
724708 Value *Op1 = II->getArgOperand(0);
725709 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType());
726 if (isNoAlias(Op1, Op1Size, P, Size))
710 MDNode *Tag = II->getMetadata(LLVMContext::MD_tbaa);
711 if (isNoAlias(Location(Op1, Op1Size, Tag), Loc))
727712 return NoModRef;
728713 }
729714 break;
732717 case Intrinsic::invariant_start: {
733718 unsigned PtrSize =
734719 cast(II->getArgOperand(0))->getZExtValue();
735 if (isNoAlias(II->getArgOperand(1), PtrSize, P, Size))
720 if (isNoAlias(Location(II->getArgOperand(1),
721 PtrSize,
722 II->getMetadata(LLVMContext::MD_tbaa)),
723 Loc))
736724 return NoModRef;
737725 break;
738726 }
739727 case Intrinsic::invariant_end: {
740728 unsigned PtrSize =
741729 cast(II->getArgOperand(1))->getZExtValue();
742 if (isNoAlias(II->getArgOperand(2), PtrSize, P, Size))
730 if (isNoAlias(Location(II->getArgOperand(2),
731 PtrSize,
732 II->getMetadata(LLVMContext::MD_tbaa)),
733 Loc))
743734 return NoModRef;
744735 break;
745736 }
746737 }
747738
748739 // The AliasAnalysis base class has some smarts, lets use them.
749 return AliasAnalysis::getModRefInfo(CS, P, Size);
750 }
751
752 AliasAnalysis::DependenceResult
753 BasicAliasAnalysis::getDependence(const Instruction *First,
754 const Value *FirstPHITranslatedAddr,
755 DependenceQueryFlags FirstFlags,
756 const Instruction *Second,
757 const Value *SecondPHITranslatedAddr,
758 DependenceQueryFlags SecondFlags) {
759 // We don't have anything special to say yet.
760 return getDependenceViaModRefInfo(First, FirstPHITranslatedAddr, FirstFlags,
761 Second, SecondPHITranslatedAddr, SecondFlags);
740 return AliasAnalysis::getModRefInfo(CS, Loc);
762741 }
763742
764743 /// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
11021081 if (const SelectInst *S1 = dyn_cast(V1))
11031082 return aliasSelect(S1, V1Size, V2, V2Size);
11041083
1105 return NoAA::alias(V1, V1Size, V2, V2Size);
1084 return NoAA::alias(Location(V1, V1Size), Location(V2, V2Size));
11061085 }
11071086
11081087 // Make sure that anything that uses AliasAnalysis pulls in this file.
105105 //------------------------------------------------
106106 // Implement the AliasAnalysis API
107107 //
108 AliasResult alias(const Value *V1, unsigned V1Size,
109 const Value *V2, unsigned V2Size);
108 AliasResult alias(const Location &LocA, const Location &LocB);
110109 ModRefResult getModRefInfo(ImmutableCallSite CS,
111 const Value *P, unsigned Size);
110 const Location &Loc);
112111 ModRefResult getModRefInfo(ImmutableCallSite CS1,
113112 ImmutableCallSite CS2) {
114113 return AliasAnalysis::getModRefInfo(CS1, CS2);
475474 /// other is some random pointer, we know there cannot be an alias, because the
476475 /// address of the global isn't taken.
477476 AliasAnalysis::AliasResult
478 GlobalsModRef::alias(const Value *V1, unsigned V1Size,
479 const Value *V2, unsigned V2Size) {
477 GlobalsModRef::alias(const Location &LocA,
478 const Location &LocB) {
480479 // Get the base object these pointers point to.
481 const Value *UV1 = V1->getUnderlyingObject();
482 const Value *UV2 = V2->getUnderlyingObject();
480 const Value *UV1 = LocA.Ptr->getUnderlyingObject();
481 const Value *UV2 = LocB.Ptr->getUnderlyingObject();
483482
484483 // If either of the underlying values is a global, they may be non-addr-taken
485484 // globals, which we can answer queries about.
527526 if ((GV1 || GV2) && GV1 != GV2)
528527 return NoAlias;
529528
530 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
529 return AliasAnalysis::alias(LocA, LocB);
531530 }
532531
533532 AliasAnalysis::ModRefResult
534533 GlobalsModRef::getModRefInfo(ImmutableCallSite CS,
535 const Value *P, unsigned Size) {
534 const Location &Loc) {
536535 unsigned Known = ModRef;
537536
538537 // If we are asking for mod/ref info of a direct call with a pointer to a
539538 // global we are tracking, return information if we have it.
540 if (const GlobalValue *GV = dyn_cast(P->getUnderlyingObject()))
539 if (const GlobalValue *GV =
540 dyn_cast(Loc.Ptr->getUnderlyingObject()))
541541 if (GV->hasLocalLinkage())
542542 if (const Function *F = CS.getCalledFunction())
543543 if (NonAddressTakenGlobals.count(GV))
546546
547547 if (Known == NoModRef)
548548 return NoModRef; // No need to query other mod/ref analyses
549 return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size));
549 return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc));
550550 }
551551
552552
4242 /// vs the specified pointer/size.
4343 AliasAnalysis::ModRefResult
4444 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
45 ImmutableCallSite CS, const Value *P,
46 unsigned Size) {
45 ImmutableCallSite CS,
46 const Location &Loc) {
4747 // If we have a function, check to see what kind of mod/ref effects it
4848 // has. Start by including any info globally known about the function.
4949 AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
6363 if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
6464 // Find out if the pointer refers to a known location.
6565 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
66 const LibCallLocationInfo &Loc =
66 const LibCallLocationInfo &LocInfo =
6767 LCI->getLocationInfo(Details[i].LocationID);
68 LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
68 LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
6969 if (Res != LibCallLocationInfo::Yes) continue;
7070
7171 // If we find a match against a location that we 'do not' interact with,
8484 // Find out if the pointer refers to a known location.
8585 bool NoneMatch = true;
8686 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
87 const LibCallLocationInfo &Loc =
87 const LibCallLocationInfo &LocInfo =
8888 LCI->getLocationInfo(Details[i].LocationID);
89 LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
89 LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
9090 if (Res == LibCallLocationInfo::No) continue;
9191
9292 // If we don't know if this pointer points to the location, then we have to
117117 //
118118 AliasAnalysis::ModRefResult
119119 LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
120 const Value *P, unsigned Size) {
120 const Location &Loc) {
121121 ModRefResult MRInfo = ModRef;
122122
123123 // If this is a direct call to a function that LCI knows about, get the
125125 if (LCI) {
126126 if (const Function *F = CS.getCalledFunction()) {
127127 if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
128 MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, P, Size));
128 MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
129129 if (MRInfo == NoModRef) return NoModRef;
130130 }
131131 }
132132 }
133133
134134 // The AliasAnalysis base class has some smarts, lets use them.
135 return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, P, Size));
135 return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
136136 }
4848 private:
4949 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
5050 virtual bool runOnFunction(Function &F);
51 virtual AliasResult alias(const Value *V1, unsigned V1Size,
52 const Value *V2, unsigned V2Size);
51 virtual AliasResult alias(const Location &LocA, const Location &LocB);
5352
5453 Value *GetBaseValue(const SCEV *S);
5554 };
10099 }
101100
102101 AliasAnalysis::AliasResult
103 ScalarEvolutionAliasAnalysis::alias(const Value *A, unsigned ASize,
104 const Value *B, unsigned BSize) {
102 ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
103 const Location &LocB) {
105104 // If either of the memory references is empty, it doesn't matter what the
106105 // pointer values are. This allows the code below to ignore this special
107106 // case.
108 if (ASize == 0 || BSize == 0)
107 if (LocA.Size == 0 || LocB.Size == 0)
109108 return NoAlias;
110109
111110 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
112 const SCEV *AS = SE->getSCEV(const_cast(A));
113 const SCEV *BS = SE->getSCEV(const_cast(B));
111 const SCEV *AS = SE->getSCEV(const_cast(LocA.Ptr));
112 const SCEV *BS = SE->getSCEV(const_cast(LocB.Ptr));
114113
115114 // If they evaluate to the same expression, it's a MustAlias.
116115 if (AS == BS) return MustAlias;
120119 if (SE->getEffectiveSCEVType(AS->getType()) ==
121120 SE->getEffectiveSCEVType(BS->getType())) {
122121 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
123 APInt ASizeInt(BitWidth, ASize);
124 APInt BSizeInt(BitWidth, BSize);
122 APInt ASizeInt(BitWidth, LocA.Size);
123 APInt BSizeInt(BitWidth, LocB.Size);
125124
126125 // Compute the difference between the two pointers.
127126 const SCEV *BA = SE->getMinusSCEV(BS, AS);
153152 // inttoptr and ptrtoint operators.
154153 Value *AO = GetBaseValue(AS);
155154 Value *BO = GetBaseValue(BS);
156 if ((AO && AO != A) || (BO && BO != B))
157 if (alias(AO ? AO : A, AO ? UnknownSize : ASize,
158 BO ? BO : B, BO ? UnknownSize : BSize) == NoAlias)
155 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
156 if (alias(Location(AO ? AO : LocA.Ptr,
157 AO ? +UnknownSize : LocA.Size,
158 AO ? 0 : LocA.TBAATag),
159 Location(BO ? BO : LocB.Ptr,
160 BO ? +UnknownSize : LocB.Size,
161 BO ? 0 : LocB.TBAATag)) == NoAlias)
159162 return NoAlias;
160163
161164 // Forward the query to the next analysis.
162 return AliasAnalysis::alias(A, ASize, B, BSize);
165 return AliasAnalysis::alias(LocA, LocB);
163166 }
9595
9696 private:
9797 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
98 virtual AliasResult alias(const Value *V1, unsigned V1Size,
99 const Value *V2, unsigned V2Size);
100 virtual bool pointsToConstantMemory(const Value *P);
98 virtual AliasResult alias(const Location &LocA, const Location &LocB);
99 virtual bool pointsToConstantMemory(const Location &Loc);
101100 };
102101 } // End of anonymous namespace
103102
117116 }
118117
119118 AliasAnalysis::AliasResult
120 TypeBasedAliasAnalysis::alias(const Value *A, unsigned ASize,
121 const Value *B, unsigned BSize) {
119 TypeBasedAliasAnalysis::alias(const Location &LocA,
120 const Location &LocB) {
122121 // Currently, metadata can only be attached to Instructions.
123 const Instruction *AI = dyn_cast(A);
122 const Instruction *AI = dyn_cast(LocA.Ptr);
124123 if (!AI) return MayAlias;
125 const Instruction *BI = dyn_cast(B);
124 const Instruction *BI = dyn_cast(LocB.Ptr);
126125 if (!BI) return MayAlias;
127126
128127 // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
174173 return MayAlias;
175174 }
176175
177 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Value *P) {
176 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
178177 // Currently, metadata can only be attached to Instructions.
179 const Instruction *I = dyn_cast(P);
178 const Instruction *I = dyn_cast(Loc.Ptr);
180179 if (!I) return false;
181180
182181 MDNode *M =
2727 }
2828
2929 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
30 // Create the first metadata kind, which is always 'dbg'.
30 // Create the fixed metadata kinds. This is done in the same order as the
31 // MD_* enum values so that they correspond.
32
33 // Create the 'dbg' metadata kind.
3134 unsigned DbgID = getMDKindID("dbg");
3235 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
36
37 // Create the 'tbaa' metadata kind.
38 unsigned TBAAID = getMDKindID("tbaa");
39 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
3340 }
3441 LLVMContext::~LLVMContext() { delete pImpl; }
3542