Add more graph traits specializations for dominator tree nodes
[oota-llvm.git] / include / llvm / Analysis / AliasAnalysis.h
1 //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
2 //
3 // This file defines the generic AliasAnalysis interface, which is used as the
4 // common interface used by all clients of alias analysis information, and
5 // implemented by all alias analysis implementations.  Mod/Ref information is
6 // also captured by this interface.
7 //
8 // Implementations of this interface must implement the various virtual methods,
9 // which automatically provides functionality for the entire suite of client
10 // APIs.
11 //
12 // This API represents memory as a (Pointer, Size) pair.  The Pointer component
13 // specifies the base memory address of the region, the Size specifies how large
14 // of an area is being queried.  If Size is 0, two pointers only alias if they
15 // are exactly equal.  If size is greater than zero, but small, the two pointers
16 // alias if the areas pointed to overlap.  If the size is very large (ie, ~0U),
17 // then the two pointers alias if they may be pointing to components of the same
18 // memory object.  Pointers that point to two completely different objects in
19 // memory never alias, regardless of the value of the Size component.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
24 #define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
25
26 #include "llvm/Support/CallSite.h"
27 class LoadInst;
28 class StoreInst;
29 class TargetData;
30 class AnalysisUsage;
31 class Pass;
32
33 class AliasAnalysis {
34   const TargetData *TD;
35 protected:
36   /// InitializeAliasAnalysis - Subclasses must call this method to initialize
37   /// the AliasAnalysis interface before any other methods are called.  This is
38   /// typically called by the run* methods of these subclasses.  This may be
39   /// called multiple times.
40   ///
41   void InitializeAliasAnalysis(Pass *P);
42   
43   // getAnalysisUsage - All alias analysis implementations should invoke this
44   // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
45   // TargetData is required by the pass.
46   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
47
48 public:
49   AliasAnalysis() : TD(0) {}
50   virtual ~AliasAnalysis();  // We want to be subclassed
51
52   /// getTargetData - Every alias analysis implementation depends on the size of
53   /// data items in the current Target.  This provides a uniform way to handle
54   /// it.
55   const TargetData &getTargetData() const { return *TD; }
56
57   //===--------------------------------------------------------------------===//
58   /// Alias Queries...
59   ///
60
61   /// Alias analysis result - Either we know for sure that it does not alias, we
62   /// know for sure it must alias, or we don't know anything: The two pointers
63   /// _might_ alias.  This enum is designed so you can do things like:
64   ///     if (AA.alias(P1, P2)) { ... }
65   /// to check to see if two pointers might alias.
66   ///
67   enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
68
69   /// alias - The main low level interface to the alias analysis implementation.
70   /// Returns a Result indicating whether the two pointers are aliased to each
71   /// other.  This is the interface that must be implemented by specific alias
72   /// analysis implementations.
73   ///
74   virtual AliasResult alias(const Value *V1, unsigned V1Size,
75                             const Value *V2, unsigned V2Size) {
76     return MayAlias;
77   }
78
79   //===--------------------------------------------------------------------===//
80   /// Simple mod/ref information...
81   ///
82
83   /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
84   /// bits which may be or'd together.
85   ///
86   enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
87
88   /// getModRefInfo - Return information about whether or not an instruction may
89   /// read or write memory specified by the pointer operand.  An instruction
90   /// that doesn't read or write memory may be trivially LICM'd for example.
91
92   /// getModRefInfo (for call sites) - Return whether information about whether
93   /// a particular call site modifies or reads the memory specified by the
94   /// pointer.
95   ///
96   virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
97     return ModRef;
98   }
99
100   /// getModRefInfo - Return information about whether two call sites may refer
101   /// to the same set of memory locations.  This function returns NoModRef if
102   /// the two calls refer to disjoint memory locations, Ref if they both read
103   /// some of the same memory, Mod if they both write to some of the same
104   /// memory, and ModRef if they read and write to the same memory.
105   ///
106   virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
107     return ModRef;
108   }
109
110   /// Convenience functions...
111   ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
112   ModRefResult getModRefInfo(StoreInst*S, Value *P, unsigned Size);
113   ModRefResult getModRefInfo(CallInst  *C, Value *P, unsigned Size) {
114     return getModRefInfo(CallSite(C), P, Size);
115   }
116   ModRefResult getModRefInfo(InvokeInst*I, Value *P, unsigned Size) {
117     return getModRefInfo(CallSite(I), P, Size);
118   }
119   ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
120     switch (I->getOpcode()) {
121     case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
122     case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
123     case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
124     case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
125     default:                  return NoModRef;
126     }
127   }
128
129   /// canBasicBlockModify - Return true if it is possible for execution of the
130   /// specified basic block to modify the value pointed to by Ptr.
131   ///
132   bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
133
134   /// canInstructionRangeModify - Return true if it is possible for the
135   /// execution of the specified instructions to modify the value pointed to by
136   /// Ptr.  The instructions to consider are all of the instructions in the
137   /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
138   ///
139   bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
140                                  const Value *Ptr, unsigned Size);
141 };
142
143 #endif