AliasAnalysis.h revision 4f1bd9e9963239c119db70070db1d68286b3de7e
14df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
56fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// This file was developed by the LLVM research group and is distributed under
66fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// the University of Illinois Open Source 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"
346df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencer#include "llvm/Support/IncludeFile.h"
35d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
36d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
37d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
381c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass LoadInst;
391c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass StoreInst;
4014f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharthclass VAArgInst;
411c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass TargetData;
426df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass Pass;
436df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass AnalysisUsage;
441c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
451c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass AliasAnalysis {
461c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerprotected:
47ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  const TargetData *TD;
48ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
49ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// the AliasAnalysis interface before any other methods are called.  This is
521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// typically called by the run* methods of these subclasses.  This may be
531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// called multiple times.
541c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  void InitializeAliasAnalysis(Pass *P);
569769ab22265b313171d201b5928688524a01bd87Misha Brukman
571c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  // getAnalysisUsage - All alias analysis implementations should invoke this
581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
591c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  // TargetData is required by the pass.
601c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
621c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic:
63ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis() : TD(0), AA(0) {}
641c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual ~AliasAnalysis();  // We want to be subclassed
651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getTargetData - Every alias analysis implementation depends on the size of
671c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// data items in the current Target.  This provides a uniform way to handle
681c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// it.
69dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman  ///
701c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  const TargetData &getTargetData() const { return *TD; }
714df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
721c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  //===--------------------------------------------------------------------===//
731c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Alias Queries...
741c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
754df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
76f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Alias analysis result - Either we know for sure that it does not alias, we
77f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// know for sure it must alias, or we don't know anything: The two pointers
78f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// _might_ alias.  This enum is designed so you can do things like:
79f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///     if (AA.alias(P1, P2)) { ... }
80f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// to check to see if two pointers might alias.
81f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
821c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
834df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
84f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// alias - The main low level interface to the alias analysis implementation.
85f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Returns a Result indicating whether the two pointers are aliased to each
86f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// other.  This is the interface that must be implemented by specific alias
87f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// analysis implementations.
88f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
891c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual AliasResult alias(const Value *V1, unsigned V1Size,
90ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner                            const Value *V2, unsigned V2Size);
914df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
9262b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// getMustAliases - If there are any pointers known that must alias this
9362b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// pointer, return them now.  This allows alias-set based alias analyses to
9462b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// perform a form a value numbering (which is exposed by load-vn).  If an
9562b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// alias analysis supports this, it should ADD any must aliased pointers to
9662b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// the specified vector.
9762b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  ///
98ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals);
9962b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
100762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// pointsToConstantMemory - If the specified pointer is known to point into
101762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// constant global memory, return true.  This allows disambiguation of store
102762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// instructions from constant pointers.
103762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  ///
104ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual bool pointsToConstantMemory(const Value *P);
10562b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
106248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  //===--------------------------------------------------------------------===//
107248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Simple mod/ref information...
108248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
109248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner
110248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
111248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// bits which may be or'd together.
112248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
113248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
1149769ab22265b313171d201b5928688524a01bd87Misha Brukman
1159769ab22265b313171d201b5928688524a01bd87Misha Brukman
116248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefBehavior - Summary of how a function affects memory in the program.
117248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Loads from constant globals are not considered memory accesses for this
118248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// interface.  Also, functions may freely modify stack space local to their
119248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// invocation without having to report it through these interfaces.
120248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefBehavior {
121248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // DoesNotAccessMemory - This function does not perform any non-local loads
122248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // or stores to memory.
123248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
124248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'const' attribute.
125248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    DoesNotAccessMemory,
1269769ab22265b313171d201b5928688524a01bd87Misha Brukman
127248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // AccessesArguments - This function accesses function arguments in
128248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // non-volatile and well known ways, but does not access any other memory.
129248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
130248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // Clients may call getArgumentAccesses to get specific information about
131248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // how pointer arguments are used.
132248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArguments,
1339769ab22265b313171d201b5928688524a01bd87Misha Brukman
134248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // AccessesArgumentsAndGlobals - This function has accesses function
135248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // arguments and global variables in non-volatile and well-known ways, but
136248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // does not access any other memory.
137248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
138248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // Clients may call getArgumentAccesses to get specific information about
139248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // how pointer arguments and globals are used.
140248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArgumentsAndGlobals,
1419769ab22265b313171d201b5928688524a01bd87Misha Brukman
142248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // OnlyReadsMemory - This function does not perform any non-local stores or
143248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // volatile loads, but may read from any memory location.
144248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
145248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'pure' attribute.
146248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    OnlyReadsMemory,
1479769ab22265b313171d201b5928688524a01bd87Misha Brukman
148248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // UnknownModRefBehavior - This indicates that the function could not be
149248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // classified into one of the behaviors above.
150248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    UnknownModRefBehavior
151248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  };
1529769ab22265b313171d201b5928688524a01bd87Misha Brukman
153248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// PointerAccessInfo - This struct is used to return results for pointers,
154248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// globals, and the return value of a function.
155248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  struct PointerAccessInfo {
156248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// V - The value this record corresponds to.  This may be an Argument for
157248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// the function, a GlobalVariable, or null, corresponding to the return
158248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// value for the function.
159248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    Value *V;
1609769ab22265b313171d201b5928688524a01bd87Misha Brukman
161248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// ModRefInfo - Whether the pointer is loaded or stored to/from.
162248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ///
163248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ModRefResult ModRefInfo;
1649769ab22265b313171d201b5928688524a01bd87Misha Brukman
165248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// AccessType - Specific fine-grained access information for the argument.
166248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// If none of these classifications is general enough, the
167248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// getModRefBehavior method should not return AccessesArguments*.  If a
168248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// record is not returned for a particular argument, the argument is never
169248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// dead and never dereferenced.
170248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    enum AccessType {
171248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ScalarAccess - The pointer is dereferenced.
172248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ///
173248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ScalarAccess,
1749769ab22265b313171d201b5928688524a01bd87Misha Brukman
175248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ArrayAccess - The pointer is indexed through as an array of elements.
176248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ///
177248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ArrayAccess,
1789769ab22265b313171d201b5928688524a01bd87Misha Brukman
179248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ElementAccess ?? P->F only?
1809769ab22265b313171d201b5928688524a01bd87Misha Brukman
181248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// CallsThrough - Indirect calls are made through the specified function
182248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// pointer.
183410354fe0c052141dadeca939395743f8dd58e38Chris Lattner      CallsThrough
184248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    };
1859769ab22265b313171d201b5928688524a01bd87Misha Brukman  };
1869769ab22265b313171d201b5928688524a01bd87Misha Brukman
187248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// getModRefBehavior - Return the behavior of the specified function if
188248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// called from the specified call site.  The call site may be null in which
189248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// case the most generic behavior of this function should be returned.
190248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
191fb752ba02ada9349353b256f81405dd6866c1364Chris Lattner                                     std::vector<PointerAccessInfo> *Info = 0);
1929769ab22265b313171d201b5928688524a01bd87Misha Brukman
1933e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// doesNotAccessMemory - If the specified function is known to never read or
1945cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  /// write memory, return true.  If the function only reads from known-constant
19594c420da4a10498c1955d837ed11e66ae3c21dcaChris Lattner  /// memory, it is also legal to return true.  Functions that unwind the stack
19694c420da4a10498c1955d837ed11e66ae3c21dcaChris Lattner  /// are not legal for this predicate.
1973e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
1983e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// Many optimizations (such as CSE and LICM) can be performed on calls to it,
1993e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// without worrying about aliasing properties, and many functions have this
2003e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// property (e.g. 'sin' and 'cos').
2013e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2023e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'const' attribute.
2033e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
204248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool doesNotAccessMemory(Function *F) {
205248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    return getModRefBehavior(F, CallSite()) == DoesNotAccessMemory;
206248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2073e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner
2083e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// onlyReadsMemory - If the specified function is known to only read from
20994c420da4a10498c1955d837ed11e66ae3c21dcaChris Lattner  /// non-volatile memory (or not access memory at all), return true.  Functions
21094c420da4a10498c1955d837ed11e66ae3c21dcaChris Lattner  /// that unwind the stack are not legal for this predicate.
2113e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2123e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property allows many common optimizations to be performed in the
2133e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// absence of interfering store instructions, such as CSE of strlen calls.
2143e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2153e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'pure' attribute.
2163e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
217248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool onlyReadsMemory(Function *F) {
218fb752ba02ada9349353b256f81405dd6866c1364Chris Lattner    /// FIXME: If the analysis returns more precise info, we can reduce it to
219fb752ba02ada9349353b256f81405dd6866c1364Chris Lattner    /// this.
2208eec644862251e14add0d2707d655fcce91e8f70Chris Lattner    ModRefBehavior MRB = getModRefBehavior(F, CallSite());
2218eec644862251e14add0d2707d655fcce91e8f70Chris Lattner    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
222248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2234df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
2241c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2251c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether or not an instruction may
2261c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// read or write memory specified by the pointer operand.  An instruction
2271c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// that doesn't read or write memory may be trivially LICM'd for example.
2281c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2291c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo (for call sites) - Return whether information about whether
2301c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// a particular call site modifies or reads the memory specified by the
2311c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// pointer.
2321c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
2335cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
2341c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2351c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether two call sites may refer
2361c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// to the same set of memory locations.  This function returns NoModRef if
237414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
238414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
239414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// ModRef if CS1 might read or write memory accessed by CS2.
2401c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
2415cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
2421c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
243e40bb915bae2aecdd1578ea356d5e4c8ac31061cChris Lattner  /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref
244414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// information for pairs of function calls (other than "pure" and "const"
245414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// functions).  This can be used by clients to avoid many pointless queries.
246414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// Remember that if you override this and chain to another analysis, you must
247414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// make sure that it doesn't have mod/ref info either.
248e40bb915bae2aecdd1578ea356d5e4c8ac31061cChris Lattner  ///
249ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual bool hasNoModRefInfoForCalls() const;
250e40bb915bae2aecdd1578ea356d5e4c8ac31061cChris Lattner
2511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Convenience functions...
2521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
2535cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
2545cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) {
2551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(C), P, Size);
2561c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2575cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) {
2581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(I), P, Size);
2591c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
26014f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) {
26114f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth    return AliasAnalysis::Mod;
26214f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  }
2631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
2641c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    switch (I->getOpcode()) {
26514f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth    case Instruction::VAArg:  return getModRefInfo((VAArgInst*)I, P, Size);
2661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
2671c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
2681c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
2691c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
2701c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    default:                  return NoModRef;
2711c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    }
2721c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2734df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
274ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
275ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Higher level methods for querying mod/ref information.
276ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
277ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
278f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canBasicBlockModify - Return true if it is possible for execution of the
279f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// specified basic block to modify the value pointed to by Ptr.
280f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
2811c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
2824df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
283f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canInstructionRangeModify - Return true if it is possible for the
284f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// execution of the specified instructions to modify the value pointed to by
285f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Ptr.  The instructions to consider are all of the instructions in the
286f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
287f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
2884df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
2891c56b730a6313886076d7b293a126ae5576f5288Chris Lattner                                 const Value *Ptr, unsigned Size);
290ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
291ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
292ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Methods that clients should call when they transform the program to allow
293ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// alias analyses to update their internal data structures.  Note that these
294ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// methods may be called on any instruction, regardless of whether or not
295ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// they have pointer-analysis implications.
296ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
297ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
298ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleteValue - This method should be called whenever an LLVM Value is
299ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleted from the program, for example when an instruction is found to be
300ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// redundant and is eliminated.
301ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
302ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void deleteValue(Value *V);
303ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
304ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// copyValue - This method should be used whenever a preexisting value in the
305ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// program is copied or cloned, introducing a new value.  Note that analysis
306ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// implementations should tolerate clients that use this method to introduce
307ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// the same value multiple times: if the analysis already knows about a
308ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// value, it should ignore the request.
309ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
310ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void copyValue(Value *From, Value *To);
311ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
312ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// replaceWithNewValue - This method is the obvious combination of the two
313ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// above, and it provided as a helper to simplify client code.
314ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
315ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  void replaceWithNewValue(Value *Old, Value *New) {
316ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    copyValue(Old, New);
317ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    deleteValue(Old);
318ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  }
3194df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner};
3204df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
3214f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace
3224f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer
323fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// Because of the way .a files work, we must force the BasicAA implementation to
324fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// be pulled in if the AliasAnalysis header is included.  Otherwise we run
325fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// the risk of AliasAnalysis being used, but the default implementation not
326fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// being linked into the tool that uses it.
3274f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
3284f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)
329d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3304df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif
331