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"
47 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
50 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
51 /// the AliasAnalysis interface before any other methods are called. This is
52 /// typically called by the run* methods of these subclasses. This may be
53 /// called multiple times.
55 void InitializeAliasAnalysis(Pass *P);
57 /// getAnalysisUsage - All alias analysis implementations should invoke this
58 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
62 static char ID; // Class identification, replacement for typeinfo
63 AliasAnalysis() : TD(0), AA(0) {}
64 virtual ~AliasAnalysis(); // We want to be subclassed
66 /// UnknownSize - This is a special value which can be used with the
67 /// size arguments in alias queries to indicate that the caller does not
68 /// know the sizes of the potential memory references.
69 static uint64_t const UnknownSize = ~UINT64_C(0);
71 /// getTargetData - Return a pointer to the current TargetData object, or
72 /// null if no TargetData object is available.
74 const TargetData *getTargetData() const { return TD; }
76 /// getTypeStoreSize - Return the TargetData store size for the given type,
77 /// if known, or a conservative value otherwise.
79 uint64_t getTypeStoreSize(const Type *Ty);
81 //===--------------------------------------------------------------------===//
85 /// Location - A description of a memory location.
87 /// Ptr - The address of the start of the location.
89 /// Size - The maximum size of the location, or UnknownSize if the size is
90 /// not known. Note that an unknown size does not mean the pointer aliases
91 /// the entire virtual address space, because there are restrictions on
92 /// stepping out of one object and into another.
93 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
95 /// TBAATag - The metadata node which describes the TBAA type of
96 /// the location, or null if there is no known unique tag.
97 const MDNode *TBAATag;
99 explicit Location(const Value *P = 0,
100 uint64_t S = UnknownSize,
102 : Ptr(P), Size(S), TBAATag(N) {}
104 Location getWithNewPtr(const Value *NewPtr) const {
105 Location Copy(*this);
110 Location getWithoutTBAATag() const {
111 Location Copy(*this);
117 /// Alias analysis result - Either we know for sure that it does not alias, we
118 /// know for sure it must alias, or we don't know anything: The two pointers
119 /// _might_ alias. This enum is designed so you can do things like:
120 /// if (AA.alias(P1, P2)) { ... }
121 /// to check to see if two pointers might alias.
123 /// See docs/AliasAnalysis.html for more information on the specific meanings
126 enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
128 /// alias - The main low level interface to the alias analysis implementation.
129 /// Returns an AliasResult indicating whether the two pointers are aliased to
130 /// each other. This is the interface that must be implemented by specific
131 /// alias analysis implementations.
132 virtual AliasResult alias(const Location &LocA, const Location &LocB);
134 /// alias - A convenience wrapper.
135 AliasResult alias(const Value *V1, uint64_t V1Size,
136 const Value *V2, uint64_t V2Size) {
137 return alias(Location(V1, V1Size), Location(V2, V2Size));
140 /// alias - A convenience wrapper.
141 AliasResult alias(const Value *V1, const Value *V2) {
142 return alias(V1, UnknownSize, V2, UnknownSize);
145 /// isNoAlias - A trivial helper function to check to see if the specified
146 /// pointers are no-alias.
147 bool isNoAlias(const Location &LocA, const Location &LocB) {
148 return alias(LocA, LocB) == NoAlias;
151 /// isNoAlias - A convenience wrapper.
152 bool isNoAlias(const Value *V1, uint64_t V1Size,
153 const Value *V2, uint64_t V2Size) {
154 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
157 /// pointsToConstantMemory - If the specified memory location is
158 /// known to be constant, return true. If OrLocal is true and the
159 /// specified memory location is known to be "local" (derived from
160 /// an alloca), return true. Otherwise return false.
161 virtual bool pointsToConstantMemory(const Location &Loc,
162 bool OrLocal = false);
164 /// pointsToConstantMemory - A convenient wrapper.
165 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
166 return pointsToConstantMemory(Location(P), OrLocal);
169 //===--------------------------------------------------------------------===//
170 /// Simple mod/ref information...
173 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
174 /// bits which may be or'd together.
176 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
179 /// ModRefBehavior - Summary of how a function affects memory in the program.
180 /// Loads from constant globals are not considered memory accesses for this
181 /// interface. Also, functions may freely modify stack space local to their
182 /// invocation without having to report it through these interfaces.
183 enum ModRefBehavior {
184 // DoesNotAccessMemory - This function does not perform any non-local loads
185 // or stores to memory.
187 // This property corresponds to the GCC 'const' attribute.
188 // This property corresponds to the LLVM IR 'readnone' attribute.
189 // This property corresponds to the IntrNoMem LLVM intrinsic flag.
192 // AccessesArguments - This function accesses function arguments in well
193 // known (possibly volatile) ways, but does not access any other memory.
195 // This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
198 // OnlyReadsMemory - This function does not perform any non-local stores or
199 // volatile loads, but may read from any memory location.
201 // This property corresponds to the GCC 'pure' attribute.
202 // This property corresponds to the LLVM IR 'readonly' attribute.
203 // This property corresponds to the IntrReadMem LLVM intrinsic flag.
206 // UnknownModRefBehavior - This indicates that the function could not be
207 // classified into one of the behaviors above.
208 UnknownModRefBehavior
211 /// getModRefBehavior - Return the behavior when calling the given call site.
212 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
214 /// getModRefBehavior - Return the behavior when calling the given function.
215 /// For use when the call site is not known.
216 virtual ModRefBehavior getModRefBehavior(const Function *F);
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 return onlyReadsMemory(getModRefBehavior(CS));
253 /// onlyReadsMemory - If the specified function is known to only read from
254 /// non-volatile memory (or not access memory at all), return true. For use
255 /// when the call site is not known.
257 bool onlyReadsMemory(const Function *F) {
258 return onlyReadsMemory(getModRefBehavior(F));
261 /// onlyReadsMemory - If the functions with the specified behavior are known
262 /// to only read from non-volatile memory (or not access memory at all), return
263 /// true. For use when the call site is not known.
265 static bool onlyReadsMemory(ModRefBehavior MRB) {
266 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
270 /// getModRefInfo - Return information about whether or not an instruction may
271 /// read or write the specified memory location. An instruction
272 /// that doesn't read or write memory may be trivially LICM'd for example.
273 ModRefResult getModRefInfo(const Instruction *I,
274 const Location &Loc) {
275 switch (I->getOpcode()) {
276 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
277 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
278 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
279 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
280 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
281 default: return NoModRef;
285 /// getModRefInfo - A convenience wrapper.
286 ModRefResult getModRefInfo(const Instruction *I,
287 const Value *P, uint64_t Size) {
288 return getModRefInfo(I, Location(P, Size));
291 /// getModRefInfo (for call sites) - Return whether information about whether
292 /// a particular call site modifies or reads the specified memory location.
293 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
294 const Location &Loc);
296 /// getModRefInfo (for call sites) - A convenience wrapper.
297 ModRefResult getModRefInfo(ImmutableCallSite CS,
298 const Value *P, uint64_t Size) {
299 return getModRefInfo(CS, Location(P, Size));
302 /// getModRefInfo (for calls) - Return whether information about whether
303 /// a particular call modifies or reads the specified memory location.
304 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
305 return getModRefInfo(ImmutableCallSite(C), Loc);
308 /// getModRefInfo (for calls) - A convenience wrapper.
309 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
310 return getModRefInfo(C, Location(P, Size));
313 /// getModRefInfo (for invokes) - Return whether information about whether
314 /// a particular invoke modifies or reads the specified memory location.
315 ModRefResult getModRefInfo(const InvokeInst *I,
316 const Location &Loc) {
317 return getModRefInfo(ImmutableCallSite(I), Loc);
320 /// getModRefInfo (for invokes) - A convenience wrapper.
321 ModRefResult getModRefInfo(const InvokeInst *I,
322 const Value *P, uint64_t Size) {
323 return getModRefInfo(I, Location(P, Size));
326 /// getModRefInfo (for loads) - Return whether information about whether
327 /// a particular load modifies or reads the specified memory location.
328 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
330 /// getModRefInfo (for loads) - A convenience wrapper.
331 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
332 return getModRefInfo(L, Location(P, Size));
335 /// getModRefInfo (for stores) - Return whether information about whether
336 /// a particular store modifies or reads the specified memory location.
337 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
339 /// getModRefInfo (for stores) - A convenience wrapper.
340 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
341 return getModRefInfo(S, Location(P, Size));
344 /// getModRefInfo (for va_args) - Return whether information about whether
345 /// a particular va_arg modifies or reads the specified memory location.
346 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
348 /// getModRefInfo (for va_args) - A convenience wrapper.
349 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) {
350 return getModRefInfo(I, Location(P, Size));
353 /// getModRefInfo - Return information about whether two call sites may refer
354 /// to the same set of memory locations. See
355 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
357 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
358 ImmutableCallSite CS2);
360 //===--------------------------------------------------------------------===//
361 /// Higher level methods for querying mod/ref information.
364 /// canBasicBlockModify - Return true if it is possible for execution of the
365 /// specified basic block to modify the value pointed to by Ptr.
366 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
368 /// canBasicBlockModify - A convenience wrapper.
369 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
370 return canBasicBlockModify(BB, Location(P, Size));
373 /// canInstructionRangeModify - Return true if it is possible for the
374 /// execution of the specified instructions to modify the value pointed to by
375 /// Ptr. The instructions to consider are all of the instructions in the
376 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
377 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
378 const Location &Loc);
380 /// canInstructionRangeModify - A convenience wrapper.
381 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
382 const Value *Ptr, uint64_t Size) {
383 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
386 //===--------------------------------------------------------------------===//
387 /// Methods that clients should call when they transform the program to allow
388 /// alias analyses to update their internal data structures. Note that these
389 /// methods may be called on any instruction, regardless of whether or not
390 /// they have pointer-analysis implications.
393 /// deleteValue - This method should be called whenever an LLVM Value is
394 /// deleted from the program, for example when an instruction is found to be
395 /// redundant and is eliminated.
397 virtual void deleteValue(Value *V);
399 /// copyValue - This method should be used whenever a preexisting value in the
400 /// program is copied or cloned, introducing a new value. Note that analysis
401 /// implementations should tolerate clients that use this method to introduce
402 /// the same value multiple times: if the analysis already knows about a
403 /// value, it should ignore the request.
405 virtual void copyValue(Value *From, Value *To);
407 /// replaceWithNewValue - This method is the obvious combination of the two
408 /// above, and it provided as a helper to simplify client code.
410 void replaceWithNewValue(Value *Old, Value *New) {
416 /// isNoAliasCall - Return true if this pointer is returned by a noalias
418 bool isNoAliasCall(const Value *V);
420 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
421 /// identifiable object. This returns true for:
422 /// Global Variables and Functions (but not Global Aliases)
423 /// Allocas and Mallocs
424 /// ByVal and NoAlias Arguments
427 bool isIdentifiedObject(const Value *V);
429 } // End llvm namespace