blob: bfa02e0e1f8de3a4b53f13d4bdab95a5a853ee60 [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner53ad0ed2002-08-22 18:25:32 +00009//
10// This file implements the generic AliasAnalysis interface which is used as the
11// common interface used by all clients and implementations of alias analysis.
12//
13// This file also implements the default version of the AliasAnalysis interface
14// that is to be used when no other implementation is specified. This does some
15// simple tests that detect obvious cases: two different global pointers cannot
16// alias, a global cannot alias a malloc, two different mallocs cannot alias,
17// etc.
18//
19// This alias analysis implementation really isn't very good for anything, but
20// it is very fast, and makes a nice clean default implementation. Because it
21// handles lots of little corner cases, other, more complex, alias analysis
22// implementations may choose to rely on this pass to resolve these simple and
23// easy cases.
24//
25//===----------------------------------------------------------------------===//
26
Chris Lattnerd501c132003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Reid Spencer6df60a92006-06-07 20:00:19 +000028#include "llvm/Pass.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029#include "llvm/BasicBlock.h"
Duncan Sandsdff67102007-12-01 07:51:45 +000030#include "llvm/Function.h"
Owen Andersoncd895252009-02-03 06:27:22 +000031#include "llvm/IntrinsicInst.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000032#include "llvm/Instructions.h"
Dan Gohmanb2143b62010-09-14 21:25:10 +000033#include "llvm/LLVMContext.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000034#include "llvm/Type.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000035#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner53ad0ed2002-08-22 18:25:32 +000038// Register the AliasAnalysis interface, providing a nice name to refer to.
Owen Anderson081c34b2010-10-19 17:21:58 +000039INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
Devang Patel19974732007-05-03 01:11:54 +000040char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000041
Chris Lattner5a24d702004-05-23 21:15:48 +000042//===----------------------------------------------------------------------===//
43// Default chaining methods
44//===----------------------------------------------------------------------===//
45
46AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +000047AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
Chris Lattner5a24d702004-05-23 21:15:48 +000048 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmanb2143b62010-09-14 21:25:10 +000049 return AA->alias(LocA, LocB);
Chris Lattner5a24d702004-05-23 21:15:48 +000050}
51
Dan Gohmana25e5db2010-11-08 16:45:26 +000052bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
53 bool OrLocal) {
Chris Lattner5a24d702004-05-23 21:15:48 +000054 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmana25e5db2010-11-08 16:45:26 +000055 return AA->pointsToConstantMemory(Loc, OrLocal);
Chris Lattner5a24d702004-05-23 21:15:48 +000056}
57
Chris Lattner5a24d702004-05-23 21:15:48 +000058void AliasAnalysis::deleteValue(Value *V) {
59 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60 AA->deleteValue(V);
61}
62
63void AliasAnalysis::copyValue(Value *From, Value *To) {
64 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
65 AA->copyValue(From, To);
66}
67
Owen Andersonab6acc62011-01-03 21:38:41 +000068void AliasAnalysis::addEscapingUse(Use &U) {
69 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
70 AA->addEscapingUse(U);
71}
72
73
Chris Lattner5a24d702004-05-23 21:15:48 +000074AliasAnalysis::ModRefResult
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000075AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Dan Gohmanb2143b62010-09-14 21:25:10 +000076 const Location &Loc) {
Dan Gohman852dda42010-10-25 16:28:57 +000077 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000078
79 ModRefBehavior MRB = getModRefBehavior(CS);
80 if (MRB == DoesNotAccessMemory)
81 return NoModRef;
82
83 ModRefResult Mask = ModRef;
Dan Gohmanc07661c2010-11-09 20:06:55 +000084 if (onlyReadsMemory(MRB))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000085 Mask = Ref;
Dan Gohmanc07661c2010-11-09 20:06:55 +000086
Dan Gohman68a60562010-11-10 18:17:28 +000087 if (onlyAccessesArgPointees(MRB)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000088 bool doesAlias = false;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +000089 if (doesAccessArgPointees(MRB)) {
90 MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +000091 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Dan Gohmanbddc1ca2011-04-27 18:39:03 +000092 AI != AE; ++AI) {
93 const Value *Arg = *AI;
94 if (!Arg->getType()->isPointerTy())
95 continue;
96 Location CSLoc(Arg, UnknownSize, CSTag);
97 if (!isNoAlias(CSLoc, Loc)) {
Dan Gohman68a60562010-11-10 18:17:28 +000098 doesAlias = true;
99 break;
100 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000101 }
102 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000103 if (!doesAlias)
104 return NoModRef;
105 }
106
Dan Gohmanb2143b62010-09-14 21:25:10 +0000107 // If Loc is a constant memory location, the call definitely could not
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000108 // modify the memory location.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000109 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000110 Mask = ModRefResult(Mask & ~Mod);
111
Dan Gohmane46a3882010-10-25 16:29:52 +0000112 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000113 if (!AA) return Mask;
114
115 // Otherwise, fall back to the next AA in the chain. But we can merge
116 // in any mask we've managed to compute.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000117 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000118}
119
120AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000121AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman852dda42010-10-25 16:28:57 +0000122 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000123
124 // If CS1 or CS2 are readnone, they don't interact.
125 ModRefBehavior CS1B = getModRefBehavior(CS1);
126 if (CS1B == DoesNotAccessMemory) return NoModRef;
127
128 ModRefBehavior CS2B = getModRefBehavior(CS2);
129 if (CS2B == DoesNotAccessMemory) return NoModRef;
130
131 // If they both only read from memory, there is no dependence.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000132 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000133 return NoModRef;
134
135 AliasAnalysis::ModRefResult Mask = ModRef;
136
137 // If CS1 only reads memory, the only dependence on CS2 can be
138 // from CS1 reading memory written by CS2.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000139 if (onlyReadsMemory(CS1B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000140 Mask = ModRefResult(Mask & Ref);
141
142 // If CS2 only access memory through arguments, accumulate the mod/ref
143 // information from CS1's references to the memory referenced by
144 // CS2's arguments.
Dan Gohman68a60562010-11-10 18:17:28 +0000145 if (onlyAccessesArgPointees(CS2B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000146 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000147 if (doesAccessArgPointees(CS2B)) {
148 MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +0000149 for (ImmutableCallSite::arg_iterator
150 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000151 const Value *Arg = *I;
152 if (!Arg->getType()->isPointerTy())
153 continue;
154 Location CS2Loc(Arg, UnknownSize, CS2Tag);
155 R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask);
Dan Gohman68a60562010-11-10 18:17:28 +0000156 if (R == Mask)
157 break;
158 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000159 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000160 return R;
161 }
162
163 // If CS1 only accesses memory through arguments, check if CS2 references
164 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
Dan Gohman68a60562010-11-10 18:17:28 +0000165 if (onlyAccessesArgPointees(CS1B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000166 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000167 if (doesAccessArgPointees(CS1B)) {
168 MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +0000169 for (ImmutableCallSite::arg_iterator
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000170 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
171 const Value *Arg = *I;
172 if (!Arg->getType()->isPointerTy())
173 continue;
174 Location CS1Loc(Arg, UnknownSize, CS1Tag);
175 if (getModRefInfo(CS2, CS1Loc) != NoModRef) {
Dan Gohman68a60562010-11-10 18:17:28 +0000176 R = Mask;
177 break;
178 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000179 }
180 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000181 if (R == NoModRef)
182 return R;
183 }
184
Dan Gohmane46a3882010-10-25 16:29:52 +0000185 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000186 if (!AA) return Mask;
187
188 // Otherwise, fall back to the next AA in the chain. But we can merge
189 // in any mask we've managed to compute.
190 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
191}
192
193AliasAnalysis::ModRefBehavior
194AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman852dda42010-10-25 16:28:57 +0000195 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000196
197 ModRefBehavior Min = UnknownModRefBehavior;
198
199 // Call back into the alias analysis with the other form of getModRefBehavior
200 // to see if it can give a better response.
201 if (const Function *F = CS.getCalledFunction())
202 Min = getModRefBehavior(F);
203
Dan Gohmane46a3882010-10-25 16:29:52 +0000204 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000205 if (!AA) return Min;
206
207 // Otherwise, fall back to the next AA in the chain. But we can merge
208 // in any result we've managed to compute.
Dan Gohman42c31a72010-11-10 01:02:18 +0000209 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000210}
211
212AliasAnalysis::ModRefBehavior
213AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000214 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000215 return AA->getModRefBehavior(F);
Chris Lattner5a24d702004-05-23 21:15:48 +0000216}
217
Chris Lattner5a24d702004-05-23 21:15:48 +0000218//===----------------------------------------------------------------------===//
219// AliasAnalysis non-virtual helper method implementation
220//===----------------------------------------------------------------------===//
221
Dan Gohman6d8eb152010-11-11 21:50:19 +0000222AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
223 return Location(LI->getPointerOperand(),
224 getTypeStoreSize(LI->getType()),
225 LI->getMetadata(LLVMContext::MD_tbaa));
226}
227
228AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
229 return Location(SI->getPointerOperand(),
230 getTypeStoreSize(SI->getValueOperand()->getType()),
231 SI->getMetadata(LLVMContext::MD_tbaa));
232}
233
234AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
235 return Location(VI->getPointerOperand(),
236 UnknownSize,
237 VI->getMetadata(LLVMContext::MD_tbaa));
238}
239
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000240
241AliasAnalysis::Location
242AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
243 uint64_t Size = UnknownSize;
244 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
245 Size = C->getValue().getZExtValue();
246
Dan Gohman387f28a2010-12-16 02:51:19 +0000247 // memcpy/memmove can have TBAA tags. For memcpy, they apply
248 // to both the source and the destination.
249 MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
250
251 return Location(MTI->getRawSource(), Size, TBAATag);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000252}
253
254AliasAnalysis::Location
Chris Lattner9dc9e812010-11-30 01:48:20 +0000255AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000256 uint64_t Size = UnknownSize;
257 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
258 Size = C->getValue().getZExtValue();
Dan Gohman387f28a2010-12-16 02:51:19 +0000259
260 // memcpy/memmove can have TBAA tags. For memcpy, they apply
261 // to both the source and the destination.
262 MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000263
Dan Gohman387f28a2010-12-16 02:51:19 +0000264 return Location(MTI->getRawDest(), Size, TBAATag);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000265}
266
267
268
Chris Lattner14ac8772003-02-26 19:26:51 +0000269AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000270AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000271 // Be conservative in the face of volatile.
272 if (L->isVolatile())
273 return ModRef;
274
Dan Gohman14a498a2010-08-03 17:27:43 +0000275 // If the load address doesn't alias the given address, it doesn't read
276 // or write the specified memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000277 if (!alias(getLocation(L), Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000278 return NoModRef;
279
Dan Gohman14a498a2010-08-03 17:27:43 +0000280 // Otherwise, a load just reads.
281 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000282}
283
Chris Lattner14ac8772003-02-26 19:26:51 +0000284AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000285AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000286 // Be conservative in the face of volatile.
287 if (S->isVolatile())
288 return ModRef;
289
Dan Gohman9b8639c2010-08-06 18:10:45 +0000290 // If the store address cannot alias the pointer in question, then the
291 // specified memory cannot be modified by the store.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000292 if (!alias(getLocation(S), Loc))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000293 return NoModRef;
294
295 // If the pointer is a pointer to constant memory, then it could not have been
296 // modified by this store.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000297 if (pointsToConstantMemory(Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000298 return NoModRef;
299
300 // Otherwise, a store just writes.
301 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000302}
303
Dan Gohmane26a7b52010-08-06 18:24:38 +0000304AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000305AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane26a7b52010-08-06 18:24:38 +0000306 // If the va_arg address cannot alias the pointer in question, then the
307 // specified memory cannot be accessed by the va_arg.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000308 if (!alias(getLocation(V), Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000309 return NoModRef;
310
311 // If the pointer is a pointer to constant memory, then it could not have been
312 // modified by this va_arg.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000313 if (pointsToConstantMemory(Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000314 return NoModRef;
315
316 // Otherwise, a va_arg reads and writes.
317 return ModRef;
318}
319
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000320// AliasAnalysis destructor: DO NOT move this to the header file for
321// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
322// the AliasAnalysis.o file in the current .a file, causing alias analysis
323// support to not be included in the tool correctly!
324//
325AliasAnalysis::~AliasAnalysis() {}
326
Dan Gohman5a56bf62008-05-30 00:02:02 +0000327/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000328/// AliasAnalysis interface before any other methods are called.
329///
330void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000331 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000332 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000333}
334
335// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000336// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000337void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000338 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000339}
340
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000341/// getTypeStoreSize - Return the TargetData store size for the given type,
342/// if known, or a conservative value otherwise.
343///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000344uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
Dan Gohmanf3a925d2010-10-19 17:06:23 +0000345 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000346}
347
Chris Lattnerf9355f62002-08-22 22:46:39 +0000348/// canBasicBlockModify - Return true if it is possible for execution of the
349/// specified basic block to modify the value pointed to by Ptr.
350///
Chris Lattner14ac8772003-02-26 19:26:51 +0000351bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000352 const Location &Loc) {
353 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000354}
355
Chris Lattnerf9355f62002-08-22 22:46:39 +0000356/// canInstructionRangeModify - Return true if it is possible for the execution
357/// of the specified instructions to modify the value pointed to by Ptr. The
358/// instructions to consider are all of the instructions in the range of [I1,I2]
359/// INCLUSIVE. I1 and I2 must be in the same basic block.
360///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000361bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
362 const Instruction &I2,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000363 const Location &Loc) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000364 assert(I1.getParent() == I2.getParent() &&
365 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000366 BasicBlock::const_iterator I = &I1;
367 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000368 ++E; // Convert from inclusive to exclusive range.
369
Chris Lattner14ac8772003-02-26 19:26:51 +0000370 for (; I != E; ++I) // Check every instruction in range
Dan Gohmanb2143b62010-09-14 21:25:10 +0000371 if (getModRefInfo(I, Loc) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000372 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000373 return false;
374}
375
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000376/// isNoAliasCall - Return true if this pointer is returned by a noalias
377/// function.
378bool llvm::isNoAliasCall(const Value *V) {
379 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000380 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000381 .paramHasAttr(0, Attribute::NoAlias);
382 return false;
383}
384
385/// isIdentifiedObject - Return true if this pointer refers to a distinct and
386/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000387/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000388/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000389/// ByVal and NoAlias Arguments
390/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000391///
Dan Gohman9e86f432010-07-07 14:27:09 +0000392bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000393 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000394 return true;
395 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000396 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000397 if (isNoAliasCall(V))
398 return true;
399 if (const Argument *A = dyn_cast<Argument>(V))
400 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000401 return false;
402}