AliasAnalysis.h revision 2b38c0d94bb54659c8748eca75b4ca64a0f01b78
1//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15// Implementations of this interface must implement the various virtual methods,
16// which automatically provides functionality for the entire suite of client
17// APIs.
18//
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.  If Size is 0, two pointers only alias if they
22// are exactly equal.  If size is greater than zero, but small, the two pointers
23// alias if the areas pointed to overlap.  If the size is very large (ie, ~0U),
24// then the two pointers alias if they may be pointing to components of the same
25// memory object.  Pointers that point to two completely different objects in
26// memory never alias, regardless of the value of the Size component.
27//
28//===----------------------------------------------------------------------===//
29
30#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
31#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
32
33#include "llvm/Support/CallSite.h"
34#include "llvm/Pass.h"    // Need this for IncludeFile
35
36namespace llvm {
37
38class LoadInst;
39class StoreInst;
40class TargetData;
41
42class AliasAnalysis {
43  const TargetData *TD;
44protected:
45  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
46  /// the AliasAnalysis interface before any other methods are called.  This is
47  /// typically called by the run* methods of these subclasses.  This may be
48  /// called multiple times.
49  ///
50  void InitializeAliasAnalysis(Pass *P);
51
52  // getAnalysisUsage - All alias analysis implementations should invoke this
53  // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
54  // TargetData is required by the pass.
55  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
56
57public:
58  AliasAnalysis() : TD(0) {}
59  virtual ~AliasAnalysis();  // We want to be subclassed
60
61  /// getTargetData - Every alias analysis implementation depends on the size of
62  /// data items in the current Target.  This provides a uniform way to handle
63  /// it.
64  const TargetData &getTargetData() const { return *TD; }
65
66  //===--------------------------------------------------------------------===//
67  /// Alias Queries...
68  ///
69
70  /// Alias analysis result - Either we know for sure that it does not alias, we
71  /// know for sure it must alias, or we don't know anything: The two pointers
72  /// _might_ alias.  This enum is designed so you can do things like:
73  ///     if (AA.alias(P1, P2)) { ... }
74  /// to check to see if two pointers might alias.
75  ///
76  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
77
78  /// alias - The main low level interface to the alias analysis implementation.
79  /// Returns a Result indicating whether the two pointers are aliased to each
80  /// other.  This is the interface that must be implemented by specific alias
81  /// analysis implementations.
82  ///
83  virtual AliasResult alias(const Value *V1, unsigned V1Size,
84                            const Value *V2, unsigned V2Size) {
85    return MayAlias;
86  }
87
88  /// getMustAliases - If there are any pointers known that must alias this
89  /// pointer, return them now.  This allows alias-set based alias analyses to
90  /// perform a form a value numbering (which is exposed by load-vn).  If an
91  /// alias analysis supports this, it should ADD any must aliased pointers to
92  /// the specified vector.
93  ///
94  virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals) {}
95
96  /// pointsToConstantMemory - If the specified pointer is known to point into
97  /// constant global memory, return true.  This allows disambiguation of store
98  /// instructions from constant pointers.
99  ///
100  virtual bool pointsToConstantMemory(const Value *P) { return false; }
101
102  //===--------------------------------------------------------------------===//
103  /// Simple mod/ref information...
104  ///
105
106  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
107  /// bits which may be or'd together.
108  ///
109  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
110
111  /// getModRefInfo - Return information about whether or not an instruction may
112  /// read or write memory specified by the pointer operand.  An instruction
113  /// that doesn't read or write memory may be trivially LICM'd for example.
114
115  /// getModRefInfo (for call sites) - Return whether information about whether
116  /// a particular call site modifies or reads the memory specified by the
117  /// pointer.
118  ///
119  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
120    // If P points to a constant memory location, the call definitely could not
121    // modify the memory location.
122    return pointsToConstantMemory(P) ? Ref : ModRef;
123  }
124
125  /// getModRefInfo - Return information about whether two call sites may refer
126  /// to the same set of memory locations.  This function returns NoModRef if
127  /// the two calls refer to disjoint memory locations, Ref if they both read
128  /// some of the same memory, Mod if they both write to some of the same
129  /// memory, and ModRef if they read and write to the same memory.
130  ///
131  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
132    return ModRef;
133  }
134
135  /// Convenience functions...
136  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
137  ModRefResult getModRefInfo(StoreInst*S, Value *P, unsigned Size);
138  ModRefResult getModRefInfo(CallInst  *C, Value *P, unsigned Size) {
139    return getModRefInfo(CallSite(C), P, Size);
140  }
141  ModRefResult getModRefInfo(InvokeInst*I, Value *P, unsigned Size) {
142    return getModRefInfo(CallSite(I), P, Size);
143  }
144  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
145    switch (I->getOpcode()) {
146    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
147    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
148    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
149    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
150    default:                  return NoModRef;
151    }
152  }
153
154  /// canBasicBlockModify - Return true if it is possible for execution of the
155  /// specified basic block to modify the value pointed to by Ptr.
156  ///
157  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
158
159  /// canInstructionRangeModify - Return true if it is possible for the
160  /// execution of the specified instructions to modify the value pointed to by
161  /// Ptr.  The instructions to consider are all of the instructions in the
162  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
163  ///
164  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
165                                 const Value *Ptr, unsigned Size);
166};
167
168// Because of the way .a files work, we must force the BasicAA implementation to
169// be pulled in if the AliasAnalysis header is included.  Otherwise we run
170// the risk of AliasAnalysis being used, but the default implementation not
171// being linked into the tool that uses it.
172//
173extern void BasicAAStub();
174static IncludeFile HDR_INCLUDE_BASICAA_CPP((void*)&BasicAAStub);
175
176} // End llvm namespace
177
178#endif
179