AliasAnalysis.h revision e53e3772f3c8b4cba69b7d77c8b2148a080fe9ae
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
21e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman// of an area is being queried, or UnknownSize if the size is not known.
22e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman// Pointers that point to two completely different objects in memory never
23e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman// alias, regardless of the value of the Size component.
241c56b730a6313886076d7b293a126ae5576f5288Chris Lattner//
254df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===----------------------------------------------------------------------===//
264df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
274df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
284df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
294df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
301c56b730a6313886076d7b293a126ae5576f5288Chris Lattner#include "llvm/Support/CallSite.h"
317107c3badfe78ec89dcab6c02cf1b1bcaccc42a8Reid Spencer#include "llvm/System/IncludeFile.h"
3236f78c8935da34074ccd06d5674e45b9cd45da8bChris Lattner#include <vector>
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
35d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
361c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass LoadInst;
371c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass StoreInst;
3814f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharthclass VAArgInst;
391c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass TargetData;
406df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass Pass;
416df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass AnalysisUsage;
421c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
431c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass AliasAnalysis {
441c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerprotected:
45ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  const TargetData *TD;
46ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
47ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
481c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
491c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// the AliasAnalysis interface before any other methods are called.  This is
501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// typically called by the run* methods of these subclasses.  This may be
511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// called multiple times.
521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  void InitializeAliasAnalysis(Pass *P);
549769ab22265b313171d201b5928688524a01bd87Misha Brukman
5564d237cfae6bcb11157d525c79b5a5335e30370bDan Gohman  /// getAnalysisUsage - All alias analysis implementations should invoke this
56fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
571c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
591c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic:
601997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel  static char ID; // Class identification, replacement for typeinfo
61ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis() : TD(0), AA(0) {}
621c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual ~AliasAnalysis();  // We want to be subclassed
631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
64ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  /// UnknownSize - This is a special value which can be used with the
65ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  /// size arguments in alias queries to indicate that the caller does not
66ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  /// know the sizes of the potential memory references.
67ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  static unsigned const UnknownSize = ~0u;
68ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman
69fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// getTargetData - Return a pointer to the current TargetData object, or
70fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// null if no TargetData object is available.
71dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman  ///
72fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  const TargetData *getTargetData() const { return TD; }
73fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman
74fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// getTypeStoreSize - Return the TargetData store size for the given type,
75fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// if known, or a conservative value otherwise.
76fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  ///
77fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  unsigned getTypeStoreSize(const Type *Ty);
784df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
791c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  //===--------------------------------------------------------------------===//
801c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Alias Queries...
811c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
824df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
83f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Alias analysis result - Either we know for sure that it does not alias, we
84f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// know for sure it must alias, or we don't know anything: The two pointers
85f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// _might_ alias.  This enum is designed so you can do things like:
86f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///     if (AA.alias(P1, P2)) { ... }
87f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// to check to see if two pointers might alias.
88f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
89e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman  /// See docs/AliasAnalysis.html for more information on the specific meanings
90e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman  /// of these values.
91e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman  ///
921c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
934df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
94f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// alias - The main low level interface to the alias analysis implementation.
95f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Returns a Result indicating whether the two pointers are aliased to each
96f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// other.  This is the interface that must be implemented by specific alias
97f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// analysis implementations.
98f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
991c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual AliasResult alias(const Value *V1, unsigned V1Size,
100ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner                            const Value *V2, unsigned V2Size);
1014df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
102847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman  /// alias - A convenience wrapper for the case where the sizes are unknown.
103847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman  AliasResult alias(const Value *V1, const Value *V2) {
104ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman    return alias(V1, UnknownSize, V2, UnknownSize);
105847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman  }
106847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman
107c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  /// isNoAlias - A trivial helper function to check to see if the specified
108c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  /// pointers are no-alias.
109c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  bool isNoAlias(const Value *V1, unsigned V1Size,
110c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner                 const Value *V2, unsigned V2Size) {
111c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner    return alias(V1, V1Size, V2, V2Size) == NoAlias;
112c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  }
113c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner
114762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// pointsToConstantMemory - If the specified pointer is known to point into
115762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// constant global memory, return true.  This allows disambiguation of store
116762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// instructions from constant pointers.
117762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  ///
118ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual bool pointsToConstantMemory(const Value *P);
11962b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
120248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  //===--------------------------------------------------------------------===//
121248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Simple mod/ref information...
122248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
123248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner
124248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
125248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// bits which may be or'd together.
126248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
127248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
1289769ab22265b313171d201b5928688524a01bd87Misha Brukman
1299769ab22265b313171d201b5928688524a01bd87Misha Brukman
130248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefBehavior - Summary of how a function affects memory in the program.
131248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Loads from constant globals are not considered memory accesses for this
132248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// interface.  Also, functions may freely modify stack space local to their
133248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// invocation without having to report it through these interfaces.
134248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefBehavior {
135248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // DoesNotAccessMemory - This function does not perform any non-local loads
136248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // or stores to memory.
137248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
138248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'const' attribute.
139248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    DoesNotAccessMemory,
1409769ab22265b313171d201b5928688524a01bd87Misha Brukman
141070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands    // AccessesArguments - This function accesses function arguments in well
142070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands    // known (possibly volatile) ways, but does not access any other memory.
143248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
1444c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // Clients may use the Info parameter of getModRefBehavior to get specific
1454c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // information about how pointer arguments are used.
146248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArguments,
1479769ab22265b313171d201b5928688524a01bd87Misha Brukman
148248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // AccessesArgumentsAndGlobals - This function has accesses function
149070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands    // arguments and global variables well known (possibly volatile) ways, but
150248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // does not access any other memory.
151248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
1524c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // Clients may use the Info parameter of getModRefBehavior to get specific
1534c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman    // information about how pointer arguments are used.
154248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArgumentsAndGlobals,
1559769ab22265b313171d201b5928688524a01bd87Misha Brukman
156248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // OnlyReadsMemory - This function does not perform any non-local stores or
157248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // volatile loads, but may read from any memory location.
158248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
159248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'pure' attribute.
160248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    OnlyReadsMemory,
1619769ab22265b313171d201b5928688524a01bd87Misha Brukman
162248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // UnknownModRefBehavior - This indicates that the function could not be
163248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // classified into one of the behaviors above.
164248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    UnknownModRefBehavior
165248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  };
1669769ab22265b313171d201b5928688524a01bd87Misha Brukman
167248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// PointerAccessInfo - This struct is used to return results for pointers,
168248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// globals, and the return value of a function.
169248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  struct PointerAccessInfo {
170248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// V - The value this record corresponds to.  This may be an Argument for
171248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// the function, a GlobalVariable, or null, corresponding to the return
172248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// value for the function.
173248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    Value *V;
1749769ab22265b313171d201b5928688524a01bd87Misha Brukman
175248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// ModRefInfo - Whether the pointer is loaded or stored to/from.
176248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ///
177248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ModRefResult ModRefInfo;
1789769ab22265b313171d201b5928688524a01bd87Misha Brukman  };
1799769ab22265b313171d201b5928688524a01bd87Misha Brukman
180dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given call site.
181e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson  virtual ModRefBehavior getModRefBehavior(CallSite CS,
182dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                   std::vector<PointerAccessInfo> *Info = 0);
183dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
184dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given function.
185dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// For use when the call site is not known.
186e79422096ea5319a365d160693d0957a2a4df75eOwen Anderson  virtual ModRefBehavior getModRefBehavior(Function *F,
187dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                   std::vector<PointerAccessInfo> *Info = 0);
188dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
1897c422ac216fe39fc9c402a704cf296cca9dc5b22Duncan Sands  /// getModRefBehavior - Return the modref behavior of the intrinsic with the
1907c422ac216fe39fc9c402a704cf296cca9dc5b22Duncan Sands  /// given id.
1917c422ac216fe39fc9c402a704cf296cca9dc5b22Duncan Sands  static ModRefBehavior getModRefBehavior(unsigned iid);
1927c422ac216fe39fc9c402a704cf296cca9dc5b22Duncan Sands
193dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified call is known to never read or
194dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  If the call only reads from known-constant
195dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// memory, it is also legal to return true.  Calls that unwind the stack
196dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// are legal for this predicate.
1973e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
198dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// Many optimizations (such as CSE and LICM) can be performed on such calls
199dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// without worrying about aliasing properties, and many calls have this
200dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// property (e.g. calls to 'sin' and 'cos').
2013e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2023e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'const' attribute.
2033e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
204652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  bool doesNotAccessMemory(CallSite CS) {
205652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng    return getModRefBehavior(CS) == DoesNotAccessMemory;
206dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
207dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
208dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified function is known to never read or
209dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  For use when the call site is not known.
210dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
211248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool doesNotAccessMemory(Function *F) {
212dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return getModRefBehavior(F) == DoesNotAccessMemory;
213248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2143e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner
215dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified call is known to only read from
216dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  Calls
217dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// that unwind the stack are legal for this predicate.
2183e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2193e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property allows many common optimizations to be performed in the
2203e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// absence of interfering store instructions, such as CSE of strlen calls.
2213e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2223e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'pure' attribute.
2233e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
224652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  bool onlyReadsMemory(CallSite CS) {
225652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng    ModRefBehavior MRB = getModRefBehavior(CS);
226dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
227dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
228dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
229dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified function is known to only read from
230dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  For use
231dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// when the call site is not known.
232dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
233248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool onlyReadsMemory(Function *F) {
234dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    ModRefBehavior MRB = getModRefBehavior(F);
2358eec644862251e14add0d2707d655fcce91e8f70Chris Lattner    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
236248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2374df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
2381c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2391c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether or not an instruction may
2401c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// read or write memory specified by the pointer operand.  An instruction
2411c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// that doesn't read or write memory may be trivially LICM'd for example.
2421c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2431c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo (for call sites) - Return whether information about whether
2441c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// a particular call site modifies or reads the memory specified by the
2451c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// pointer.
2461c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
247652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
2481c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2491c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether two call sites may refer
2501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// to the same set of memory locations.  This function returns NoModRef if
251414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
252414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
253414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// ModRef if CS1 might read or write memory accessed by CS2.
2541c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
255652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
2561c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
257dff6710717b159f089c76a07eda074eb6347eb92Duncan Sandspublic:
2581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Convenience functions...
2591c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
2605cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
2615cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) {
2621c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(C), P, Size);
2631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2645cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) {
2651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(I), P, Size);
2661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
26714f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) {
2681ef14f6e768eac76fc272320e79bdbd90747ef47Owen Anderson    return AliasAnalysis::ModRef;
26914f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  }
2701c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
2711c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    switch (I->getOpcode()) {
27214f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth    case Instruction::VAArg:  return getModRefInfo((VAArgInst*)I, P, Size);
2731c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
2741c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
2751c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
2761c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
2771c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    default:                  return NoModRef;
2781c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    }
2791c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2804df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
281ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
282ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Higher level methods for querying mod/ref information.
283ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
284ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
285f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canBasicBlockModify - Return true if it is possible for execution of the
286f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// specified basic block to modify the value pointed to by Ptr.
287f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
2881c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
2894df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
290f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canInstructionRangeModify - Return true if it is possible for the
291f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// execution of the specified instructions to modify the value pointed to by
292f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Ptr.  The instructions to consider are all of the instructions in the
293f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
294f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
2954df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
2961c56b730a6313886076d7b293a126ae5576f5288Chris Lattner                                 const Value *Ptr, unsigned Size);
297ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
298ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
299ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Methods that clients should call when they transform the program to allow
300ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// alias analyses to update their internal data structures.  Note that these
301ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// methods may be called on any instruction, regardless of whether or not
302ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// they have pointer-analysis implications.
303ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
304ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
305ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleteValue - This method should be called whenever an LLVM Value is
306ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleted from the program, for example when an instruction is found to be
307ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// redundant and is eliminated.
308ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
309ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void deleteValue(Value *V);
310ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
311ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// copyValue - This method should be used whenever a preexisting value in the
312ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// program is copied or cloned, introducing a new value.  Note that analysis
313ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// implementations should tolerate clients that use this method to introduce
314ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// the same value multiple times: if the analysis already knows about a
315ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// value, it should ignore the request.
316ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
317ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void copyValue(Value *From, Value *To);
318ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
319ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// replaceWithNewValue - This method is the obvious combination of the two
320ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// above, and it provided as a helper to simplify client code.
321ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
322ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  void replaceWithNewValue(Value *Old, Value *New) {
323ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    copyValue(Old, New);
324ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    deleteValue(Old);
325ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  }
3264df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner};
3274df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
328a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// isNoAliasCall - Return true if this pointer is returned by a noalias
329a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// function.
330a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohmanbool isNoAliasCall(const Value *V);
331a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman
3323311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// isIdentifiedObject - Return true if this pointer refers to a distinct and
333a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// identifiable object.  This returns true for:
3345753a4a0033da4add45f2e9930a4e1159d92a869Dan Gohman///    Global Variables and Functions (but not Global Aliases)
335a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman///    Allocas and Mallocs
3369e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman///    ByVal and NoAlias Arguments
3379e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman///    NoAlias returns
3383311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman///
3399e86f4364b912ae743490ba01d6989acfd12c046Dan Gohmanbool isIdentifiedObject(const Value *V);
3403311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman
3414f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace
3424f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer
343fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// Because of the way .a files work, we must force the BasicAA implementation to
344fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// be pulled in if the AliasAnalysis header is included.  Otherwise we run
345fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// the risk of AliasAnalysis being used, but the default implementation not
346fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// being linked into the tool that uses it.
3474f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
3484f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)
349d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3504df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif
351