AliasAnalysis.h revision c46530b6a32a53edf0d6d32bcd09ea5d76940472
14df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
94df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//
104df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// This file defines the generic AliasAnalysis interface, which is used as the
114df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// common interface used by all clients of alias analysis information, and
121c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// implemented by all alias analysis implementations.  Mod/Ref information is
131c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// also captured by this interface.
144df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//
154df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// Implementations of this interface must implement the various virtual methods,
164df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// which automatically provides functionality for the entire suite of client
174df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// APIs.
184df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//
191c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// This API represents memory as a (Pointer, Size) pair.  The Pointer component
201c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// specifies the base memory address of the region, the Size specifies how large
211c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// of an area is being queried.  If Size is 0, two pointers only alias if they
221c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// are exactly equal.  If size is greater than zero, but small, the two pointers
231c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// alias if the areas pointed to overlap.  If the size is very large (ie, ~0U),
241c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// then the two pointers alias if they may be pointing to components of the same
251c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// memory object.  Pointers that point to two completely different objects in
261c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// memory never alias, regardless of the value of the Size component.
271c56b730a6313886076d7b293a126ae5576f5288Chris Lattner//
284df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===----------------------------------------------------------------------===//
294df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
304df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
314df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
324df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
331c56b730a6313886076d7b293a126ae5576f5288Chris Lattner#include "llvm/Support/CallSite.h"
347107c3badfe78ec89dcab6c02cf1b1bcaccc42a8Reid Spencer#include "llvm/System/IncludeFile.h"
3536f78c8935da34074ccd06d5674e45b9cd45da8bChris Lattner#include <vector>
36d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
37d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
38d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
391c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass LoadInst;
401c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass StoreInst;
4114f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharthclass VAArgInst;
421c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass TargetData;
436df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass Pass;
446df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass AnalysisUsage;
451c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
461c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass AliasAnalysis {
471c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerprotected:
48ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  const TargetData *TD;
49ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
50ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// the AliasAnalysis interface before any other methods are called.  This is
531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// typically called by the run* methods of these subclasses.  This may be
541c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// called multiple times.
551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
561c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  void InitializeAliasAnalysis(Pass *P);
579769ab22265b313171d201b5928688524a01bd87Misha Brukman
5864d237cfae6bcb11157d525c79b5a5335e30370bDan Gohman  /// getAnalysisUsage - All alias analysis implementations should invoke this
59fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
601c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
621c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic:
631997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel  static char ID; // Class identification, replacement for typeinfo
64ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis() : TD(0), AA(0) {}
651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual ~AliasAnalysis();  // We want to be subclassed
661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
67fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// getTargetData - Return a pointer to the current TargetData object, or
68fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// null if no TargetData object is available.
69dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman  ///
70fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  const TargetData *getTargetData() const { return TD; }
71fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman
72fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// getTypeStoreSize - Return the TargetData store size for the given type,
73fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// if known, or a conservative value otherwise.
74fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  ///
75fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  unsigned getTypeStoreSize(const Type *Ty);
764df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
771c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  //===--------------------------------------------------------------------===//
781c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Alias Queries...
791c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
804df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
81f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Alias analysis result - Either we know for sure that it does not alias, we
82f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// know for sure it must alias, or we don't know anything: The two pointers
83f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// _might_ alias.  This enum is designed so you can do things like:
84f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///     if (AA.alias(P1, P2)) { ... }
85f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// to check to see if two pointers might alias.
86f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
871c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
884df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
89f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// alias - The main low level interface to the alias analysis implementation.
90f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Returns a Result indicating whether the two pointers are aliased to each
91f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// other.  This is the interface that must be implemented by specific alias
92f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// analysis implementations.
93f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
941c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual AliasResult alias(const Value *V1, unsigned V1Size,
95ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner                            const Value *V2, unsigned V2Size);
964df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
97c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  /// isNoAlias - A trivial helper function to check to see if the specified
98c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  /// pointers are no-alias.
99c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  bool isNoAlias(const Value *V1, unsigned V1Size,
100c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner                 const Value *V2, unsigned V2Size) {
101c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner    return alias(V1, V1Size, V2, V2Size) == NoAlias;
102c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  }
103c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner
104762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// pointsToConstantMemory - If the specified pointer is known to point into
105762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// constant global memory, return true.  This allows disambiguation of store
106762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// instructions from constant pointers.
107762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  ///
108ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual bool pointsToConstantMemory(const Value *P);
10962b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
110248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  //===--------------------------------------------------------------------===//
111248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Simple mod/ref information...
112248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
113248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner
114248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
115248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// bits which may be or'd together.
116248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
117248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
1189769ab22265b313171d201b5928688524a01bd87Misha Brukman
1199769ab22265b313171d201b5928688524a01bd87Misha Brukman
120248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefBehavior - Summary of how a function affects memory in the program.
121248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Loads from constant globals are not considered memory accesses for this
122248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// interface.  Also, functions may freely modify stack space local to their
123248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// invocation without having to report it through these interfaces.
124248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefBehavior {
125248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // DoesNotAccessMemory - This function does not perform any non-local loads
126248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // or stores to memory.
127248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
128248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'const' attribute.
129248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    DoesNotAccessMemory,
1309769ab22265b313171d201b5928688524a01bd87Misha Brukman
131070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands    // AccessesArguments - This function accesses function arguments in well
132070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands    // known (possibly volatile) ways, but does not access any other memory.
133248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
1344c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // Clients may use the Info parameter of getModRefBehavior to get specific
1354c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // information about how pointer arguments are used.
136248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArguments,
1379769ab22265b313171d201b5928688524a01bd87Misha Brukman
138248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // AccessesArgumentsAndGlobals - This function has accesses function
139070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands    // arguments and global variables well known (possibly volatile) ways, but
140248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // does not access any other memory.
141248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
1424c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // Clients may use the Info parameter of getModRefBehavior to get specific
1434c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // information about how pointer arguments are used.
144248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArgumentsAndGlobals,
1459769ab22265b313171d201b5928688524a01bd87Misha Brukman
146248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // OnlyReadsMemory - This function does not perform any non-local stores or
147248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // volatile loads, but may read from any memory location.
148248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
149248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'pure' attribute.
150248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    OnlyReadsMemory,
1519769ab22265b313171d201b5928688524a01bd87Misha Brukman
152248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // UnknownModRefBehavior - This indicates that the function could not be
153248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // classified into one of the behaviors above.
154248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    UnknownModRefBehavior
155248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  };
1569769ab22265b313171d201b5928688524a01bd87Misha Brukman
157248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// PointerAccessInfo - This struct is used to return results for pointers,
158248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// globals, and the return value of a function.
159248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  struct PointerAccessInfo {
160248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// V - The value this record corresponds to.  This may be an Argument for
161248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// the function, a GlobalVariable, or null, corresponding to the return
162248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// value for the function.
163248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    Value *V;
1649769ab22265b313171d201b5928688524a01bd87Misha Brukman
165248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// ModRefInfo - Whether the pointer is loaded or stored to/from.
166248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ///
167248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ModRefResult ModRefInfo;
1689769ab22265b313171d201b5928688524a01bd87Misha Brukman
169248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// AccessType - Specific fine-grained access information for the argument.
170248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// If none of these classifications is general enough, the
171248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// getModRefBehavior method should not return AccessesArguments*.  If a
172248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// record is not returned for a particular argument, the argument is never
173248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// dead and never dereferenced.
174248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    enum AccessType {
175248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ScalarAccess - The pointer is dereferenced.
176248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ///
177248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ScalarAccess,
1789769ab22265b313171d201b5928688524a01bd87Misha Brukman
179248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ArrayAccess - The pointer is indexed through as an array of elements.
180248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ///
181248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ArrayAccess,
1829769ab22265b313171d201b5928688524a01bd87Misha Brukman
183248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ElementAccess ?? P->F only?
1849769ab22265b313171d201b5928688524a01bd87Misha Brukman
185248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// CallsThrough - Indirect calls are made through the specified function
186248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// pointer.
187410354fe0c052141dadeca939395743f8dd58e38Chris Lattner      CallsThrough
188248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    };
1899769ab22265b313171d201b5928688524a01bd87Misha Brukman  };
1909769ab22265b313171d201b5928688524a01bd87Misha Brukman
191dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given call site.
192e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson  virtual ModRefBehavior getModRefBehavior(CallSite CS,
193dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                   std::vector<PointerAccessInfo> *Info = 0);
194dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
195dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given function.
196dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// For use when the call site is not known.
197e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson  virtual ModRefBehavior getModRefBehavior(Function *F,
198dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                   std::vector<PointerAccessInfo> *Info = 0);
199dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
200dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified call is known to never read or
201dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  If the call only reads from known-constant
202dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// memory, it is also legal to return true.  Calls that unwind the stack
203dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// are legal for this predicate.
2043e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
205dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// Many optimizations (such as CSE and LICM) can be performed on such calls
206dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// without worrying about aliasing properties, and many calls have this
207dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// property (e.g. calls to 'sin' and 'cos').
2083e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2093e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'const' attribute.
2103e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
211652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  bool doesNotAccessMemory(CallSite CS) {
212652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng    return getModRefBehavior(CS) == DoesNotAccessMemory;
213dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
214dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
215dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified function is known to never read or
216dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  For use when the call site is not known.
217dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
218248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool doesNotAccessMemory(Function *F) {
219dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return getModRefBehavior(F) == DoesNotAccessMemory;
220248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2213e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner
222dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified call is known to only read from
223dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  Calls
224dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// that unwind the stack are legal for this predicate.
2253e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2263e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property allows many common optimizations to be performed in the
2273e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// absence of interfering store instructions, such as CSE of strlen calls.
2283e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2293e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'pure' attribute.
2303e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
231652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  bool onlyReadsMemory(CallSite CS) {
232652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng    ModRefBehavior MRB = getModRefBehavior(CS);
233dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
234dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
235dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
236dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified function is known to only read from
237dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  For use
238dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// when the call site is not known.
239dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
240248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool onlyReadsMemory(Function *F) {
241dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    ModRefBehavior MRB = getModRefBehavior(F);
2428eec644862251e14add0d2707d655fcce91e8f70Chris Lattner    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
243248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2444df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
2451c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2461c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether or not an instruction may
2471c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// read or write memory specified by the pointer operand.  An instruction
2481c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// that doesn't read or write memory may be trivially LICM'd for example.
2491c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo (for call sites) - Return whether information about whether
2511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// a particular call site modifies or reads the memory specified by the
2521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// pointer.
2531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
254652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
2551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2561c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether two call sites may refer
2571c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// to the same set of memory locations.  This function returns NoModRef if
258414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
259414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
260414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// ModRef if CS1 might read or write memory accessed by CS2.
2611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
262652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
2631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
264dff6710717b159f089c76a07eda074eb6347eb92Duncan Sandspublic:
2651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Convenience functions...
2661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
2675cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
2685cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) {
2691c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(C), P, Size);
2701c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2715cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) {
2721c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(I), P, Size);
2731c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
27414f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) {
2751ef14f6e768eac76fc272320e79bdbd90747ef47Owen Anderson    return AliasAnalysis::ModRef;
27614f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  }
2771c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
2781c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    switch (I->getOpcode()) {
27914f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth    case Instruction::VAArg:  return getModRefInfo((VAArgInst*)I, P, Size);
2801c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
2811c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
2821c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
2831c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
2841c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    default:                  return NoModRef;
2851c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    }
2861c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2874df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
288ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
289ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Higher level methods for querying mod/ref information.
290ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
291ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
292f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canBasicBlockModify - Return true if it is possible for execution of the
293f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// specified basic block to modify the value pointed to by Ptr.
294f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
2951c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
2964df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
297f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canInstructionRangeModify - Return true if it is possible for the
298f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// execution of the specified instructions to modify the value pointed to by
299f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Ptr.  The instructions to consider are all of the instructions in the
300f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
301f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
3024df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
3031c56b730a6313886076d7b293a126ae5576f5288Chris Lattner                                 const Value *Ptr, unsigned Size);
304ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
305ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
306ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Methods that clients should call when they transform the program to allow
307ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// alias analyses to update their internal data structures.  Note that these
308ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// methods may be called on any instruction, regardless of whether or not
309ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// they have pointer-analysis implications.
310ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
311ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
312ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleteValue - This method should be called whenever an LLVM Value is
313ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleted from the program, for example when an instruction is found to be
314ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// redundant and is eliminated.
315ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
316ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void deleteValue(Value *V);
317ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
318ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// copyValue - This method should be used whenever a preexisting value in the
319ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// program is copied or cloned, introducing a new value.  Note that analysis
320ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// implementations should tolerate clients that use this method to introduce
321ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// the same value multiple times: if the analysis already knows about a
322ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// value, it should ignore the request.
323ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
324ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void copyValue(Value *From, Value *To);
325ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
326ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// replaceWithNewValue - This method is the obvious combination of the two
327ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// above, and it provided as a helper to simplify client code.
328ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
329ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  void replaceWithNewValue(Value *Old, Value *New) {
330ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    copyValue(Old, New);
331ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    deleteValue(Old);
332ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  }
3334df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner};
3344df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
335a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// isNoAliasCall - Return true if this pointer is returned by a noalias
336a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// function.
337a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohmanbool isNoAliasCall(const Value *V);
338a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman
3393311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// isIdentifiedObject - Return true if this pointer refers to a distinct and
340a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// identifiable object.  This returns true for:
3415753a4a0033da4add45f2e9930a4e1159d92a869Dan Gohman///    Global Variables and Functions (but not Global Aliases)
342a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman///    Allocas and Mallocs
343a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman///    ByVal and NoAlias Arguments
344a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman///    NoAlias returns
3453311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman///
3463311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohmanbool isIdentifiedObject(const Value *V);
3473311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman
3484f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace
3494f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer
350fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// Because of the way .a files work, we must force the BasicAA implementation to
351fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// be pulled in if the AliasAnalysis header is included.  Otherwise we run
352fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// the risk of AliasAnalysis being used, but the default implementation not
353fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// being linked into the tool that uses it.
3544f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
3554f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)
356d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3574df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif
358