1 //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the generic AliasAnalysis interface, which is used as the
11 // common interface used by all clients of alias analysis information, and
12 // implemented by all alias analysis implementations. Mod/Ref information is
13 // also captured by this interface.
15 // Implementations of this interface must implement the various virtual methods,
16 // which automatically provides functionality for the entire suite of client
19 // This API represents memory as a (Pointer, Size) pair. The Pointer component
20 // specifies the base memory address of the region, the Size specifies how large
21 // of an area is being queried, or UnknownSize if the size is not known.
22 // Pointers that point to two completely different objects in memory never
23 // alias, regardless of the value of the Size component.
25 //===----------------------------------------------------------------------===//
27 #ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
28 #define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
30 #include "llvm/Support/CallSite.h"
31 #include "llvm/System/IncludeFile.h"
48 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
51 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
52 /// the AliasAnalysis interface before any other methods are called. This is
53 /// typically called by the run* methods of these subclasses. This may be
54 /// called multiple times.
56 void InitializeAliasAnalysis(Pass *P);
58 /// getAnalysisUsage - All alias analysis implementations should invoke this
59 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
63 static char ID; // Class identification, replacement for typeinfo
64 AliasAnalysis() : TD(0), AA(0) {}
65 virtual ~AliasAnalysis(); // We want to be subclassed
67 /// UnknownSize - This is a special value which can be used with the
68 /// size arguments in alias queries to indicate that the caller does not
69 /// know the sizes of the potential memory references.
70 static unsigned const UnknownSize = ~0u;
72 /// getTargetData - Return a pointer to the current TargetData object, or
73 /// null if no TargetData object is available.
75 const TargetData *getTargetData() const { return TD; }
77 /// getTypeStoreSize - Return the TargetData store size for the given type,
78 /// if known, or a conservative value otherwise.
80 unsigned getTypeStoreSize(const Type *Ty);
82 //===--------------------------------------------------------------------===//
86 /// Location - A description of a memory location.
88 /// Ptr - The address of the start of the location.
90 /// Size - The size of the location.
92 /// TBAATag - The metadata node which describes the TBAA type of
93 /// the location, or null if there is no (unique) tag.
94 const MDNode *TBAATag;
96 explicit Location(const Value *P = 0,
97 unsigned S = UnknownSize,
99 : Ptr(P), Size(S), TBAATag(N) {}
101 Location getWithNewPtr(const Value *NewPtr) const {
102 Location Copy(*this);
107 Location getWithoutTBAATag() const {
108 Location Copy(*this);
114 /// Alias analysis result - Either we know for sure that it does not alias, we
115 /// know for sure it must alias, or we don't know anything: The two pointers
116 /// _might_ alias. This enum is designed so you can do things like:
117 /// if (AA.alias(P1, P2)) { ... }
118 /// to check to see if two pointers might alias.
120 /// See docs/AliasAnalysis.html for more information on the specific meanings
123 enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
125 /// alias - The main low level interface to the alias analysis implementation.
126 /// Returns a Result indicating whether the two pointers are aliased to each
127 /// other. This is the interface that must be implemented by specific alias
128 /// analysis implementations.
129 virtual AliasResult alias(const Location &LocA, const Location &LocB);
131 /// alias - A convenience wrapper.
132 AliasResult alias(const Value *V1, unsigned V1Size,
133 const Value *V2, unsigned V2Size) {
134 return alias(Location(V1, V1Size), Location(V2, V2Size));
137 /// alias - A convenience wrapper.
138 AliasResult alias(const Value *V1, const Value *V2) {
139 return alias(V1, UnknownSize, V2, UnknownSize);
142 /// isNoAlias - A trivial helper function to check to see if the specified
143 /// pointers are no-alias.
144 bool isNoAlias(const Location &LocA, const Location &LocB) {
145 return alias(LocA, LocB) == NoAlias;
148 /// isNoAlias - A convenience wrapper.
149 bool isNoAlias(const Value *V1, unsigned V1Size,
150 const Value *V2, unsigned V2Size) {
151 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
154 /// pointsToConstantMemory - If the specified memory location is known to be
155 /// constant, return true. This allows disambiguation of store
156 /// instructions from constant pointers.
158 virtual bool pointsToConstantMemory(const Location &Loc);
160 /// pointsToConstantMemory - A convenient wrapper.
161 bool pointsToConstantMemory(const Value *P) {
162 return pointsToConstantMemory(Location(P));
165 //===--------------------------------------------------------------------===//
166 /// Simple mod/ref information...
169 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
170 /// bits which may be or'd together.
172 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
175 /// ModRefBehavior - Summary of how a function affects memory in the program.
176 /// Loads from constant globals are not considered memory accesses for this
177 /// interface. Also, functions may freely modify stack space local to their
178 /// invocation without having to report it through these interfaces.
179 enum ModRefBehavior {
180 // DoesNotAccessMemory - This function does not perform any non-local loads
181 // or stores to memory.
183 // This property corresponds to the GCC 'const' attribute.
186 // AccessesArguments - This function accesses function arguments in well
187 // known (possibly volatile) ways, but does not access any other memory.
190 // AccessesArgumentsAndGlobals - This function has accesses function
191 // arguments and global variables well known (possibly volatile) ways, but
192 // does not access any other memory.
193 AccessesArgumentsAndGlobals,
195 // OnlyReadsMemory - This function does not perform any non-local stores or
196 // volatile loads, but may read from any memory location.
198 // This property corresponds to the GCC 'pure' attribute.
201 // UnknownModRefBehavior - This indicates that the function could not be
202 // classified into one of the behaviors above.
203 UnknownModRefBehavior
206 /// getModRefBehavior - Return the behavior when calling the given call site.
207 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
209 /// getModRefBehavior - Return the behavior when calling the given function.
210 /// For use when the call site is not known.
211 virtual ModRefBehavior getModRefBehavior(const Function *F);
213 /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic
214 /// with the given id. Most clients won't need this, because the regular
215 /// getModRefBehavior incorporates this information.
216 static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid);
218 /// doesNotAccessMemory - If the specified call is known to never read or
219 /// write memory, return true. If the call only reads from known-constant
220 /// memory, it is also legal to return true. Calls that unwind the stack
221 /// are legal for this predicate.
223 /// Many optimizations (such as CSE and LICM) can be performed on such calls
224 /// without worrying about aliasing properties, and many calls have this
225 /// property (e.g. calls to 'sin' and 'cos').
227 /// This property corresponds to the GCC 'const' attribute.
229 bool doesNotAccessMemory(ImmutableCallSite CS) {
230 return getModRefBehavior(CS) == DoesNotAccessMemory;
233 /// doesNotAccessMemory - If the specified function is known to never read or
234 /// write memory, return true. For use when the call site is not known.
236 bool doesNotAccessMemory(const Function *F) {
237 return getModRefBehavior(F) == DoesNotAccessMemory;
240 /// onlyReadsMemory - If the specified call is known to only read from
241 /// non-volatile memory (or not access memory at all), return true. Calls
242 /// that unwind the stack are legal for this predicate.
244 /// This property allows many common optimizations to be performed in the
245 /// absence of interfering store instructions, such as CSE of strlen calls.
247 /// This property corresponds to the GCC 'pure' attribute.
249 bool onlyReadsMemory(ImmutableCallSite CS) {
250 ModRefBehavior MRB = getModRefBehavior(CS);
251 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
254 /// onlyReadsMemory - If the specified function is known to only read from
255 /// non-volatile memory (or not access memory at all), return true. For use
256 /// when the call site is not known.
258 bool onlyReadsMemory(const Function *F) {
259 ModRefBehavior MRB = getModRefBehavior(F);
260 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
264 /// getModRefInfo - Return information about whether or not an instruction may
265 /// read or write the specified memory location. An instruction
266 /// that doesn't read or write memory may be trivially LICM'd for example.
267 ModRefResult getModRefInfo(const Instruction *I,
268 const Location &Loc) {
269 switch (I->getOpcode()) {
270 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
271 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
272 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
273 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
274 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
275 default: return NoModRef;
279 /// getModRefInfo - A convenience wrapper.
280 ModRefResult getModRefInfo(const Instruction *I,
281 const Value *P, unsigned Size) {
282 return getModRefInfo(I, Location(P, Size));
285 /// getModRefInfo (for call sites) - Return whether information about whether
286 /// a particular call site modifies or reads the specified memory location.
287 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
288 const Location &Loc);
290 /// getModRefInfo (for call sites) - A convenience wrapper.
291 ModRefResult getModRefInfo(ImmutableCallSite CS,
292 const Value *P, unsigned Size) {
293 return getModRefInfo(CS, Location(P, Size));
296 /// getModRefInfo (for calls) - Return whether information about whether
297 /// a particular call modifies or reads the specified memory location.
298 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
299 return getModRefInfo(ImmutableCallSite(C), Loc);
302 /// getModRefInfo (for calls) - A convenience wrapper.
303 ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) {
304 return getModRefInfo(C, Location(P, Size));
307 /// getModRefInfo (for invokes) - Return whether information about whether
308 /// a particular invoke modifies or reads the specified memory location.
309 ModRefResult getModRefInfo(const InvokeInst *I,
310 const Location &Loc) {
311 return getModRefInfo(ImmutableCallSite(I), Loc);
314 /// getModRefInfo (for invokes) - A convenience wrapper.
315 ModRefResult getModRefInfo(const InvokeInst *I,
316 const Value *P, unsigned Size) {
317 return getModRefInfo(I, Location(P, Size));
320 /// getModRefInfo (for loads) - Return whether information about whether
321 /// a particular load modifies or reads the specified memory location.
322 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
324 /// getModRefInfo (for loads) - A convenience wrapper.
325 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
326 return getModRefInfo(L, Location(P, Size));
329 /// getModRefInfo (for stores) - Return whether information about whether
330 /// a particular store modifies or reads the specified memory location.
331 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
333 /// getModRefInfo (for stores) - A convenience wrapper.
334 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
335 return getModRefInfo(S, Location(P, Size));
338 /// getModRefInfo (for va_args) - Return whether information about whether
339 /// a particular va_arg modifies or reads the specified memory location.
340 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
342 /// getModRefInfo (for va_args) - A convenience wrapper.
343 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, unsigned Size) {
344 return getModRefInfo(I, Location(P, Size));
347 /// getModRefInfo - Return information about whether two call sites may refer
348 /// to the same set of memory locations. See
349 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
351 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
352 ImmutableCallSite CS2);
354 //===--------------------------------------------------------------------===//
355 /// Higher level methods for querying mod/ref information.
358 /// canBasicBlockModify - Return true if it is possible for execution of the
359 /// specified basic block to modify the value pointed to by Ptr.
360 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
362 /// canBasicBlockModify - A convenience wrapper.
363 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size){
364 return canBasicBlockModify(BB, Location(P, Size));
367 /// canInstructionRangeModify - Return true if it is possible for the
368 /// execution of the specified instructions to modify the value pointed to by
369 /// Ptr. The instructions to consider are all of the instructions in the
370 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
371 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
372 const Location &Loc);
374 /// canInstructionRangeModify - A convenience wrapper.
375 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
376 const Value *Ptr, unsigned Size) {
377 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
380 //===--------------------------------------------------------------------===//
381 /// Methods that clients should call when they transform the program to allow
382 /// alias analyses to update their internal data structures. Note that these
383 /// methods may be called on any instruction, regardless of whether or not
384 /// they have pointer-analysis implications.
387 /// deleteValue - This method should be called whenever an LLVM Value is
388 /// deleted from the program, for example when an instruction is found to be
389 /// redundant and is eliminated.
391 virtual void deleteValue(Value *V);
393 /// copyValue - This method should be used whenever a preexisting value in the
394 /// program is copied or cloned, introducing a new value. Note that analysis
395 /// implementations should tolerate clients that use this method to introduce
396 /// the same value multiple times: if the analysis already knows about a
397 /// value, it should ignore the request.
399 virtual void copyValue(Value *From, Value *To);
401 /// replaceWithNewValue - This method is the obvious combination of the two
402 /// above, and it provided as a helper to simplify client code.
404 void replaceWithNewValue(Value *Old, Value *New) {
410 /// isNoAliasCall - Return true if this pointer is returned by a noalias
412 bool isNoAliasCall(const Value *V);
414 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
415 /// identifiable object. This returns true for:
416 /// Global Variables and Functions (but not Global Aliases)
417 /// Allocas and Mallocs
418 /// ByVal and NoAlias Arguments
421 bool isIdentifiedObject(const Value *V);
423 } // End llvm namespace
425 // Because of the way .a files work, we must force the BasicAA implementation to
426 // be pulled in if the AliasAnalysis header is included. Otherwise we run
427 // the risk of AliasAnalysis being used, but the default implementation not
428 // being linked into the tool that uses it.
429 FORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
430 FORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)