AliasAnalysis.h revision 6fbcc26f1460eaee4e0eb8b426fc1ff0c7af11be
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"
34class LoadInst;
35class StoreInst;
36class TargetData;
37class AnalysisUsage;
38class Pass;
39
40class AliasAnalysis {
41  const TargetData *TD;
42protected:
43  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
44  /// the AliasAnalysis interface before any other methods are called.  This is
45  /// typically called by the run* methods of these subclasses.  This may be
46  /// called multiple times.
47  ///
48  void InitializeAliasAnalysis(Pass *P);
49
50  // getAnalysisUsage - All alias analysis implementations should invoke this
51  // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
52  // TargetData is required by the pass.
53  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
54
55public:
56  AliasAnalysis() : TD(0) {}
57  virtual ~AliasAnalysis();  // We want to be subclassed
58
59  /// getTargetData - Every alias analysis implementation depends on the size of
60  /// data items in the current Target.  This provides a uniform way to handle
61  /// it.
62  const TargetData &getTargetData() const { return *TD; }
63
64  //===--------------------------------------------------------------------===//
65  /// Alias Queries...
66  ///
67
68  /// Alias analysis result - Either we know for sure that it does not alias, we
69  /// know for sure it must alias, or we don't know anything: The two pointers
70  /// _might_ alias.  This enum is designed so you can do things like:
71  ///     if (AA.alias(P1, P2)) { ... }
72  /// to check to see if two pointers might alias.
73  ///
74  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
75
76  /// alias - The main low level interface to the alias analysis implementation.
77  /// Returns a Result indicating whether the two pointers are aliased to each
78  /// other.  This is the interface that must be implemented by specific alias
79  /// analysis implementations.
80  ///
81  virtual AliasResult alias(const Value *V1, unsigned V1Size,
82                            const Value *V2, unsigned V2Size) {
83    return MayAlias;
84  }
85
86  /// getMustAliases - If there are any pointers known that must alias this
87  /// pointer, return them now.  This allows alias-set based alias analyses to
88  /// perform a form a value numbering (which is exposed by load-vn).  If an
89  /// alias analysis supports this, it should ADD any must aliased pointers to
90  /// the specified vector.
91  ///
92  virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals) {}
93
94
95  //===--------------------------------------------------------------------===//
96  /// Simple mod/ref information...
97  ///
98
99  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
100  /// bits which may be or'd together.
101  ///
102  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
103
104  /// getModRefInfo - Return information about whether or not an instruction may
105  /// read or write memory specified by the pointer operand.  An instruction
106  /// that doesn't read or write memory may be trivially LICM'd for example.
107
108  /// getModRefInfo (for call sites) - Return whether information about whether
109  /// a particular call site modifies or reads the memory specified by the
110  /// pointer.
111  ///
112  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
113    return ModRef;
114  }
115
116  /// getModRefInfo - Return information about whether two call sites may refer
117  /// to the same set of memory locations.  This function returns NoModRef if
118  /// the two calls refer to disjoint memory locations, Ref if they both read
119  /// some of the same memory, Mod if they both write to some of the same
120  /// memory, and ModRef if they read and write to the same memory.
121  ///
122  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
123    return ModRef;
124  }
125
126  /// Convenience functions...
127  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
128  ModRefResult getModRefInfo(StoreInst*S, Value *P, unsigned Size);
129  ModRefResult getModRefInfo(CallInst  *C, Value *P, unsigned Size) {
130    return getModRefInfo(CallSite(C), P, Size);
131  }
132  ModRefResult getModRefInfo(InvokeInst*I, Value *P, unsigned Size) {
133    return getModRefInfo(CallSite(I), P, Size);
134  }
135  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
136    switch (I->getOpcode()) {
137    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
138    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
139    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
140    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
141    default:                  return NoModRef;
142    }
143  }
144
145  /// canBasicBlockModify - Return true if it is possible for execution of the
146  /// specified basic block to modify the value pointed to by Ptr.
147  ///
148  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
149
150  /// canInstructionRangeModify - Return true if it is possible for the
151  /// execution of the specified instructions to modify the value pointed to by
152  /// Ptr.  The instructions to consider are all of the instructions in the
153  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
154  ///
155  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
156                                 const Value *Ptr, unsigned Size);
157};
158
159#endif
160