AliasAnalysis.h revision dff6710717b159f089c76a07eda074eb6347eb92
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"
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
581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  // getAnalysisUsage - All alias analysis implementations should invoke this
591c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
601c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  // TargetData is required by the pass.
611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
621c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
631c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic:
641997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel  static char ID; // Class identification, replacement for typeinfo
65ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis() : TD(0), AA(0) {}
661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual ~AliasAnalysis();  // We want to be subclassed
671c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
681c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getTargetData - Every alias analysis implementation depends on the size of
691c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// data items in the current Target.  This provides a uniform way to handle
701c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// it.
71dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman  ///
721c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  const TargetData &getTargetData() const { return *TD; }
734df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
741c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  //===--------------------------------------------------------------------===//
751c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Alias Queries...
761c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
774df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
78f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Alias analysis result - Either we know for sure that it does not alias, we
79f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// know for sure it must alias, or we don't know anything: The two pointers
80f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// _might_ alias.  This enum is designed so you can do things like:
81f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///     if (AA.alias(P1, P2)) { ... }
82f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// to check to see if two pointers might alias.
83f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
841c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
854df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
86f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// alias - The main low level interface to the alias analysis implementation.
87f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Returns a Result indicating whether the two pointers are aliased to each
88f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// other.  This is the interface that must be implemented by specific alias
89f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// analysis implementations.
90f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
911c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual AliasResult alias(const Value *V1, unsigned V1Size,
92ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner                            const Value *V2, unsigned V2Size);
934df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
9462b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// getMustAliases - If there are any pointers known that must alias this
9562b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// pointer, return them now.  This allows alias-set based alias analyses to
9662b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// perform a form a value numbering (which is exposed by load-vn).  If an
9762b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// alias analysis supports this, it should ADD any must aliased pointers to
9862b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  /// the specified vector.
9962b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner  ///
100ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals);
10162b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
102762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// pointsToConstantMemory - If the specified pointer is known to point into
103762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// constant global memory, return true.  This allows disambiguation of store
104762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  /// instructions from constant pointers.
105762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner  ///
106ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual bool pointsToConstantMemory(const Value *P);
10762b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
108248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  //===--------------------------------------------------------------------===//
109248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Simple mod/ref information...
110248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
111248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner
112248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
113248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// bits which may be or'd together.
114248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
115248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
1169769ab22265b313171d201b5928688524a01bd87Misha Brukman
1179769ab22265b313171d201b5928688524a01bd87Misha Brukman
118248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefBehavior - Summary of how a function affects memory in the program.
119248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Loads from constant globals are not considered memory accesses for this
120248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// interface.  Also, functions may freely modify stack space local to their
121248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// invocation without having to report it through these interfaces.
122248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefBehavior {
123248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // DoesNotAccessMemory - This function does not perform any non-local loads
124248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // or stores to memory.
125248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
126248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'const' attribute.
127248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    DoesNotAccessMemory,
1289769ab22265b313171d201b5928688524a01bd87Misha Brukman
129248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // AccessesArguments - This function accesses function arguments in
130248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // non-volatile and well known ways, but does not access any other memory.
131248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
132248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // Clients may call getArgumentAccesses to get specific information about
133248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // how pointer arguments are used.
134248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArguments,
1359769ab22265b313171d201b5928688524a01bd87Misha Brukman
136248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // AccessesArgumentsAndGlobals - This function has accesses function
137248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // arguments and global variables in non-volatile and well-known ways, but
138248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // does not access any other memory.
139248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
140248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // Clients may call getArgumentAccesses to get specific information about
141248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // how pointer arguments and globals are used.
142248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    AccessesArgumentsAndGlobals,
1439769ab22265b313171d201b5928688524a01bd87Misha Brukman
144248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // OnlyReadsMemory - This function does not perform any non-local stores or
145248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // volatile loads, but may read from any memory location.
146248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    //
147248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // This property corresponds to the GCC 'pure' attribute.
148248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    OnlyReadsMemory,
1499769ab22265b313171d201b5928688524a01bd87Misha Brukman
150248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // UnknownModRefBehavior - This indicates that the function could not be
151248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    // classified into one of the behaviors above.
152248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    UnknownModRefBehavior
153248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  };
1549769ab22265b313171d201b5928688524a01bd87Misha Brukman
155248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// PointerAccessInfo - This struct is used to return results for pointers,
156248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// globals, and the return value of a function.
157248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  struct PointerAccessInfo {
158248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// V - The value this record corresponds to.  This may be an Argument for
159248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// the function, a GlobalVariable, or null, corresponding to the return
160248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// value for the function.
161248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    Value *V;
1629769ab22265b313171d201b5928688524a01bd87Misha Brukman
163248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// ModRefInfo - Whether the pointer is loaded or stored to/from.
164248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ///
165248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    ModRefResult ModRefInfo;
1669769ab22265b313171d201b5928688524a01bd87Misha Brukman
167248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// AccessType - Specific fine-grained access information for the argument.
168248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// If none of these classifications is general enough, the
169248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// getModRefBehavior method should not return AccessesArguments*.  If a
170248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// record is not returned for a particular argument, the argument is never
171248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    /// dead and never dereferenced.
172248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    enum AccessType {
173248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ScalarAccess - The pointer is dereferenced.
174248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ///
175248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ScalarAccess,
1769769ab22265b313171d201b5928688524a01bd87Misha Brukman
177248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ArrayAccess - The pointer is indexed through as an array of elements.
178248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ///
179248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      ArrayAccess,
1809769ab22265b313171d201b5928688524a01bd87Misha Brukman
181248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// ElementAccess ?? P->F only?
1829769ab22265b313171d201b5928688524a01bd87Misha Brukman
183248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// CallsThrough - Indirect calls are made through the specified function
184248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner      /// pointer.
185410354fe0c052141dadeca939395743f8dd58e38Chris Lattner      CallsThrough
186248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner    };
1879769ab22265b313171d201b5928688524a01bd87Misha Brukman  };
1889769ab22265b313171d201b5928688524a01bd87Misha Brukman
189dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given call site.
190dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ModRefBehavior getModRefBehavior(CallSite CS,
191dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                   std::vector<PointerAccessInfo> *Info = 0);
192dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
193dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given function.
194dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// For use when the call site is not known.
195dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ModRefBehavior getModRefBehavior(Function *F,
196dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                   std::vector<PointerAccessInfo> *Info = 0);
197dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
198dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified call is known to never read or
199dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  If the call only reads from known-constant
200dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// memory, it is also legal to return true.  Calls that unwind the stack
201dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// are legal for this predicate.
2023e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
203dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// Many optimizations (such as CSE and LICM) can be performed on such calls
204dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// without worrying about aliasing properties, and many calls have this
205dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// property (e.g. calls to 'sin' and 'cos').
2063e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2073e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'const' attribute.
2083e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
209dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  bool doesNotAccessMemory(CallSite CS) {
210dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return getModRefBehavior(CS) == DoesNotAccessMemory;
211dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
212dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
213dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified function is known to never read or
214dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  For use when the call site is not known.
215dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
216248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool doesNotAccessMemory(Function *F) {
217dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return getModRefBehavior(F) == DoesNotAccessMemory;
218248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2193e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner
220dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified call is known to only read from
221dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  Calls
222dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// that unwind the stack are legal for this predicate.
2233e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2243e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property allows many common optimizations to be performed in the
2253e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// absence of interfering store instructions, such as CSE of strlen calls.
2263e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2273e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'pure' attribute.
2283e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
229dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  bool onlyReadsMemory(CallSite CS) {
230dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    ModRefBehavior MRB = getModRefBehavior(CS);
231dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
232dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
233dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
234dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified function is known to only read from
235dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  For use
236dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// when the call site is not known.
237dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
238248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  bool onlyReadsMemory(Function *F) {
239dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    ModRefBehavior MRB = getModRefBehavior(F);
2408eec644862251e14add0d2707d655fcce91e8f70Chris Lattner    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
241248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
2424df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
2431c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2441c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether or not an instruction may
2451c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// read or write memory specified by the pointer operand.  An instruction
2461c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// that doesn't read or write memory may be trivially LICM'd for example.
2471c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2481c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo (for call sites) - Return whether information about whether
2491c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// a particular call site modifies or reads the memory specified by the
2501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// pointer.
2511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
2525cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
2531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
2541c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether two call sites may refer
2551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// to the same set of memory locations.  This function returns NoModRef if
256414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
257414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
258414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// ModRef if CS1 might read or write memory accessed by CS2.
2591c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
2605cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
2611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
262e40bb915bae2aecdd1578ea356d5e4c8ac31061cChris Lattner  /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref
263414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// information for pairs of function calls (other than "pure" and "const"
264414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// functions).  This can be used by clients to avoid many pointless queries.
265414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// Remember that if you override this and chain to another analysis, you must
266414c36769aff6ec688c49f493122529394357d05Chris Lattner  /// make sure that it doesn't have mod/ref info either.
267e40bb915bae2aecdd1578ea356d5e4c8ac31061cChris Lattner  ///
268ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual bool hasNoModRefInfoForCalls() const;
269e40bb915bae2aecdd1578ea356d5e4c8ac31061cChris Lattner
270dff6710717b159f089c76a07eda074eb6347eb92Duncan Sandsprotected:
271dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior of the specified function if
272dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// called from the specified call site.  The call site may be null in which
273dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// case the most generic behavior of this function should be returned.
274dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
275dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands                                     std::vector<PointerAccessInfo> *Info = 0);
276dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
277dff6710717b159f089c76a07eda074eb6347eb92Duncan Sandspublic:
2781c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Convenience functions...
2791c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
2805cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
2815cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) {
2821c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(C), P, Size);
2831c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
2845cb66e24d42b40d087989199297c369b3f3b2766Chris Lattner  ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) {
2851c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    return getModRefInfo(CallSite(I), P, Size);
2861c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
28714f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) {
2881ef14f6e768eac76fc272320e79bdbd90747ef47Owen Anderson    return AliasAnalysis::ModRef;
28914f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth  }
2901c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
2911c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    switch (I->getOpcode()) {
29214f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth    case Instruction::VAArg:  return getModRefInfo((VAArgInst*)I, P, Size);
2931c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
2941c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
2951c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
2961c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
2971c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    default:                  return NoModRef;
2981c56b730a6313886076d7b293a126ae5576f5288Chris Lattner    }
2991c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
3004df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
301ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
302ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Higher level methods for querying mod/ref information.
303ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
304ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
305f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canBasicBlockModify - Return true if it is possible for execution of the
306f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// specified basic block to modify the value pointed to by Ptr.
307f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
3081c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
3094df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
310f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canInstructionRangeModify - Return true if it is possible for the
311f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// execution of the specified instructions to modify the value pointed to by
312f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Ptr.  The instructions to consider are all of the instructions in the
313f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
314f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
3154df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
3161c56b730a6313886076d7b293a126ae5576f5288Chris Lattner                                 const Value *Ptr, unsigned Size);
317ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
318ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
319ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Methods that clients should call when they transform the program to allow
320ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// alias analyses to update their internal data structures.  Note that these
321ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// methods may be called on any instruction, regardless of whether or not
322ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// they have pointer-analysis implications.
323ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
324ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
325ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleteValue - This method should be called whenever an LLVM Value is
326ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleted from the program, for example when an instruction is found to be
327ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// redundant and is eliminated.
328ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
329ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void deleteValue(Value *V);
330ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
331ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// copyValue - This method should be used whenever a preexisting value in the
332ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// program is copied or cloned, introducing a new value.  Note that analysis
333ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// implementations should tolerate clients that use this method to introduce
334ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// the same value multiple times: if the analysis already knows about a
335ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// value, it should ignore the request.
336ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
337ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void copyValue(Value *From, Value *To);
338ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
339ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// replaceWithNewValue - This method is the obvious combination of the two
340ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// above, and it provided as a helper to simplify client code.
341ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
342ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  void replaceWithNewValue(Value *Old, Value *New) {
343ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    copyValue(Old, New);
344ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    deleteValue(Old);
345ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  }
3464df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner};
3474df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
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