AliasAnalysis.h revision 3574eca1b02600bac4e625297f4ecf745f4c4f32
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//
198e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman// This API identifies memory regions with the Location class. The pointer
208e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman// component specifies the base memory address of the region. The Size specifies
218e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman// the maximum size (in address units) of the memory region, or UnknownSize if
228e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman// the size is not known. The TBAA tag identifies the "type" of the memory
238e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman// reference; see the TypeBasedAliasAnalysis class for details.
248e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//
258e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman// Some non-obvious details include:
268e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//  - Pointers that point to two completely different objects in memory never
278e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//    alias, regardless of the value of the Size component.
288e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//  - NoAlias doesn't imply inequal pointers. The most obvious example of this
298e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//    is two pointers to constant memory. Even if they are equal, constant
308e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//    memory is never stored to, so there will never be any dependencies.
318e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//    In this and other situations, the pointers may be both NoAlias and
328e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//    MustAlias at the same time. The current API can only return one result,
338e78cc4e130a8773cc8a2be2a94c4a97317ac383Dan Gohman//    though this is rarely a problem in practice.
341c56b730a6313886076d7b293a126ae5576f5288Chris Lattner//
354df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===----------------------------------------------------------------------===//
364df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
374df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
384df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
394df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
401c56b730a6313886076d7b293a126ae5576f5288Chris Lattner#include "llvm/Support/CallSite.h"
411fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman#include "llvm/ADT/DenseMap.h"
42d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
43d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
44d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
451c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass LoadInst;
461c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass StoreInst;
4714f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharthclass VAArgInst;
483574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmowclass DataLayout;
498e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramerclass TargetLibraryInfo;
506df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass Pass;
516df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass AnalysisUsage;
52e90c5cb747631b315350e7ee7424048c7778bdf9Chris Lattnerclass MemTransferInst;
539dc9e81aa7412b329bbaf51a589a81475214802bChris Lattnerclass MemIntrinsic;
543a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosierclass DominatorTree;
551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
561c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass AliasAnalysis {
571c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerprotected:
583574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  const DataLayout *TD;
598e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer  const TargetLibraryInfo *TLI;
60cb74993bdc37681ddbb80fa361575107afae1350Dan Gohman
61cb74993bdc37681ddbb80fa361575107afae1350Dan Gohmanprivate:
62ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
63ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
64cb74993bdc37681ddbb80fa361575107afae1350Dan Gohmanprotected:
651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// the AliasAnalysis interface before any other methods are called.  This is
671c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// typically called by the run* methods of these subclasses.  This may be
681c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// called multiple times.
691c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
701c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  void InitializeAliasAnalysis(Pass *P);
719769ab22265b313171d201b5928688524a01bd87Misha Brukman
7264d237cfae6bcb11157d525c79b5a5335e30370bDan Gohman  /// getAnalysisUsage - All alias analysis implementations should invoke this
73fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
741c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
751c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
761c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic:
771997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel  static char ID; // Class identification, replacement for typeinfo
788e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer  AliasAnalysis() : TD(0), TLI(0), AA(0) {}
791c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  virtual ~AliasAnalysis();  // We want to be subclassed
801c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
81ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  /// UnknownSize - This is a special value which can be used with the
82ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  /// size arguments in alias queries to indicate that the caller does not
83ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman  /// know the sizes of the potential memory references.
843da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman  static uint64_t const UnknownSize = ~UINT64_C(0);
85ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman
863574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  /// getDataLayout - Return a pointer to the current DataLayout object, or
873574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  /// null if no DataLayout object is available.
88dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman  ///
893574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  const DataLayout *getDataLayout() const { return TD; }
90fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman
918e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer  /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
928e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer  /// object, or null if no TargetLibraryInfo object is available.
938e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer  ///
948e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer  const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
958e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer
963574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  /// getTypeStoreSize - Return the DataLayout store size for the given type,
97fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  /// if known, or a conservative value otherwise.
98fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman  ///
99db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  uint64_t getTypeStoreSize(Type *Ty);
1004df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
1011c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  //===--------------------------------------------------------------------===//
1021c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// Alias Queries...
1031c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  ///
1044df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
105b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// Location - A description of a memory location.
106b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  struct Location {
107b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    /// Ptr - The address of the start of the location.
108b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    const Value *Ptr;
109a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman    /// Size - The maximum size of the location, in address-units, or
110a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman    /// UnknownSize if the size is not known.  Note that an unknown size does
111a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman    /// not mean the pointer aliases the entire virtual address space, because
112a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman    /// there are restrictions on stepping out of one object and into another.
1136c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman    /// See http://llvm.org/docs/LangRef.html#pointeraliasing
1143da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman    uint64_t Size;
115b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    /// TBAATag - The metadata node which describes the TBAA type of
1166c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman    /// the location, or null if there is no known unique tag.
117b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    const MDNode *TBAATag;
118b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
119c413330c99a573ef3ffe80a46400b1d3eca2398dChris Lattner    explicit Location(const Value *P = 0, uint64_t S = UnknownSize,
120b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                      const MDNode *N = 0)
121b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman      : Ptr(P), Size(S), TBAATag(N) {}
1229f27074f42f03af640977006dc7c05005fb77991Dan Gohman
1239f27074f42f03af640977006dc7c05005fb77991Dan Gohman    Location getWithNewPtr(const Value *NewPtr) const {
1249f27074f42f03af640977006dc7c05005fb77991Dan Gohman      Location Copy(*this);
1259f27074f42f03af640977006dc7c05005fb77991Dan Gohman      Copy.Ptr = NewPtr;
1269f27074f42f03af640977006dc7c05005fb77991Dan Gohman      return Copy;
1279f27074f42f03af640977006dc7c05005fb77991Dan Gohman    }
1289f27074f42f03af640977006dc7c05005fb77991Dan Gohman
129075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman    Location getWithNewSize(uint64_t NewSize) const {
130075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman      Location Copy(*this);
131075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman      Copy.Size = NewSize;
132075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman      return Copy;
133075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman    }
134075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman
1359f27074f42f03af640977006dc7c05005fb77991Dan Gohman    Location getWithoutTBAATag() const {
1369f27074f42f03af640977006dc7c05005fb77991Dan Gohman      Location Copy(*this);
1379f27074f42f03af640977006dc7c05005fb77991Dan Gohman      Copy.TBAATag = 0;
1389f27074f42f03af640977006dc7c05005fb77991Dan Gohman      return Copy;
1399f27074f42f03af640977006dc7c05005fb77991Dan Gohman    }
140b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  };
141b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
1426d8eb156e6be727570b300bac7712f745a318c7dDan Gohman  /// getLocation - Fill in Loc with information about the memory reference by
1436d8eb156e6be727570b300bac7712f745a318c7dDan Gohman  /// the given instruction.
1446d8eb156e6be727570b300bac7712f745a318c7dDan Gohman  Location getLocation(const LoadInst *LI);
1456d8eb156e6be727570b300bac7712f745a318c7dDan Gohman  Location getLocation(const StoreInst *SI);
1466d8eb156e6be727570b300bac7712f745a318c7dDan Gohman  Location getLocation(const VAArgInst *VI);
14746cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman  Location getLocation(const AtomicCmpXchgInst *CXI);
14846cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman  Location getLocation(const AtomicRMWInst *RMWI);
14916f7993e0df4bd0727f8535bd47a2776bec10545Chris Lattner  static Location getLocationForSource(const MemTransferInst *MTI);
1509dc9e81aa7412b329bbaf51a589a81475214802bChris Lattner  static Location getLocationForDest(const MemIntrinsic *MI);
1516d8eb156e6be727570b300bac7712f745a318c7dDan Gohman
152f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Alias analysis result - Either we know for sure that it does not alias, we
153f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// know for sure it must alias, or we don't know anything: The two pointers
154f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// _might_ alias.  This enum is designed so you can do things like:
155f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///     if (AA.alias(P1, P2)) { ... }
156f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// to check to see if two pointers might alias.
157f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  ///
158e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman  /// See docs/AliasAnalysis.html for more information on the specific meanings
159e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman  /// of these values.
160e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman  ///
16117e8078ae14ed71f657d59c77efa3bedf171161aDan Gohman  enum AliasResult {
16217e8078ae14ed71f657d59c77efa3bedf171161aDan Gohman    NoAlias = 0,        ///< No dependencies.
1634a34cbd2b9ba9efcdab4c4e656df2d7bd22e2604Dan Gohman    MayAlias,           ///< Anything goes.
1644a34cbd2b9ba9efcdab4c4e656df2d7bd22e2604Dan Gohman    PartialAlias,       ///< Pointers differ, but pointees overlap.
1654a34cbd2b9ba9efcdab4c4e656df2d7bd22e2604Dan Gohman    MustAlias           ///< Pointers are equal.
16617e8078ae14ed71f657d59c77efa3bedf171161aDan Gohman  };
1674df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
168f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// alias - The main low level interface to the alias analysis implementation.
1696c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman  /// Returns an AliasResult indicating whether the two pointers are aliased to
1706c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman  /// each other.  This is the interface that must be implemented by specific
1716c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman  /// alias analysis implementations.
172b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  virtual AliasResult alias(const Location &LocA, const Location &LocB);
1734df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
174b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// alias - A convenience wrapper.
1753da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman  AliasResult alias(const Value *V1, uint64_t V1Size,
1763da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman                    const Value *V2, uint64_t V2Size) {
177b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return alias(Location(V1, V1Size), Location(V2, V2Size));
178b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
179b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
180b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// alias - A convenience wrapper.
181847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman  AliasResult alias(const Value *V1, const Value *V2) {
182ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman    return alias(V1, UnknownSize, V2, UnknownSize);
183847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman  }
184847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman
185c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  /// isNoAlias - A trivial helper function to check to see if the specified
186c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  /// pointers are no-alias.
187b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  bool isNoAlias(const Location &LocA, const Location &LocB) {
188b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return alias(LocA, LocB) == NoAlias;
189b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
190b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
191b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// isNoAlias - A convenience wrapper.
1923da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman  bool isNoAlias(const Value *V1, uint64_t V1Size,
1933da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman                 const Value *V2, uint64_t V2Size) {
194b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
195c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner  }
19672c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner
197230768bd1316a012e88ac62689589fe5e2f10456Dan Gohman  /// isNoAlias - A convenience wrapper.
198230768bd1316a012e88ac62689589fe5e2f10456Dan Gohman  bool isNoAlias(const Value *V1, const Value *V2) {
199230768bd1316a012e88ac62689589fe5e2f10456Dan Gohman    return isNoAlias(Location(V1), Location(V2));
200230768bd1316a012e88ac62689589fe5e2f10456Dan Gohman  }
201230768bd1316a012e88ac62689589fe5e2f10456Dan Gohman
20272c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner  /// isMustAlias - A convenience wrapper.
20372c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner  bool isMustAlias(const Location &LocA, const Location &LocB) {
20472c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner    return alias(LocA, LocB) == MustAlias;
20572c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner  }
206c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner
207cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner  /// isMustAlias - A convenience wrapper.
208cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner  bool isMustAlias(const Value *V1, const Value *V2) {
209cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner    return alias(V1, 1, V2, 1) == MustAlias;
210cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner  }
211cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner
212a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman  /// pointsToConstantMemory - If the specified memory location is
213a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman  /// known to be constant, return true. If OrLocal is true and the
214a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman  /// specified memory location is known to be "local" (derived from
215a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman  /// an alloca), return true. Otherwise return false.
216a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman  virtual bool pointsToConstantMemory(const Location &Loc,
217a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman                                      bool OrLocal = false);
218b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
219b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// pointsToConstantMemory - A convenient wrapper.
220a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman  bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
221a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman    return pointsToConstantMemory(Location(P), OrLocal);
222b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
22362b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner
224248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  //===--------------------------------------------------------------------===//
225248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Simple mod/ref information...
226248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
227248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner
228248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
229248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// bits which may be or'd together.
230248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  ///
231248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
2329769ab22265b313171d201b5928688524a01bd87Misha Brukman
23342c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman  /// These values define additional bits used to define the
23442c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman  /// ModRefBehavior values.
23542c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman  enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
2369769ab22265b313171d201b5928688524a01bd87Misha Brukman
237248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// ModRefBehavior - Summary of how a function affects memory in the program.
238248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// Loads from constant globals are not considered memory accesses for this
239248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// interface.  Also, functions may freely modify stack space local to their
240248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  /// invocation without having to report it through these interfaces.
241248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  enum ModRefBehavior {
24250a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// DoesNotAccessMemory - This function does not perform any non-local loads
24350a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// or stores to memory.
24450a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    ///
24550a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the GCC 'const' attribute.
24650a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the LLVM IR 'readnone' attribute.
24750a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
24842c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman    DoesNotAccessMemory = Nowhere | NoModRef,
2499769ab22265b313171d201b5928688524a01bd87Misha Brukman
250e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    /// OnlyReadsArgumentPointees - The only memory references in this function
251e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    /// (if it has any) are non-volatile loads from objects pointed to by its
252e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    /// pointer-typed arguments, with arbitrary offsets.
25350a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    ///
25450a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
255e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    OnlyReadsArgumentPointees = ArgumentPointees | Ref,
256db78c4873ea8c4b3dce90d761a1ad59d5bcdd6e7Dan Gohman
257e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    /// OnlyAccessesArgumentPointees - The only memory references in this
258e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    /// function (if it has any) are non-volatile loads and stores from objects
259e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    /// pointed to by its pointer-typed arguments, with arbitrary offsets.
26050a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    ///
26150a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
262e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman    OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
2639769ab22265b313171d201b5928688524a01bd87Misha Brukman
26450a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// OnlyReadsMemory - This function does not perform any non-local stores or
26550a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// volatile loads, but may read from any memory location.
26650a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    ///
26750a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the GCC 'pure' attribute.
26850a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the LLVM IR 'readonly' attribute.
26950a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
27042c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman    OnlyReadsMemory = Anywhere | Ref,
2719769ab22265b313171d201b5928688524a01bd87Misha Brukman
27250a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// UnknownModRefBehavior - This indicates that the function could not be
27350a04d067f8803d52cababdabe5c188844c5c210Dan Gohman    /// classified into one of the behaviors above.
27442c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman    UnknownModRefBehavior = Anywhere | ModRef
275248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  };
2769769ab22265b313171d201b5928688524a01bd87Misha Brukman
277dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given call site.
278a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman  virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
279dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
280dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// getModRefBehavior - Return the behavior when calling the given function.
281dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// For use when the call site is not known.
282a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman  virtual ModRefBehavior getModRefBehavior(const Function *F);
283dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
284dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified call is known to never read or
285dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  If the call only reads from known-constant
286dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// memory, it is also legal to return true.  Calls that unwind the stack
287dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// are legal for this predicate.
2883e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
289dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// Many optimizations (such as CSE and LICM) can be performed on such calls
290dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// without worrying about aliasing properties, and many calls have this
291dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// property (e.g. calls to 'sin' and 'cos').
2923e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
2933e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'const' attribute.
2943e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
29579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  bool doesNotAccessMemory(ImmutableCallSite CS) {
296652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng    return getModRefBehavior(CS) == DoesNotAccessMemory;
297dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
298dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
299dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// doesNotAccessMemory - If the specified function is known to never read or
300dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// write memory, return true.  For use when the call site is not known.
301dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
30279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  bool doesNotAccessMemory(const Function *F) {
303dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands    return getModRefBehavior(F) == DoesNotAccessMemory;
304248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
3053e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner
306dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified call is known to only read from
307dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  Calls
308dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// that unwind the stack are legal for this predicate.
3093e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
3103e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property allows many common optimizations to be performed in the
3113e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// absence of interfering store instructions, such as CSE of strlen calls.
3123e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
3133e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  /// This property corresponds to the GCC 'pure' attribute.
3143e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner  ///
31579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  bool onlyReadsMemory(ImmutableCallSite CS) {
316467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman    return onlyReadsMemory(getModRefBehavior(CS));
317dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  }
318dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands
319dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// onlyReadsMemory - If the specified function is known to only read from
320dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// non-volatile memory (or not access memory at all), return true.  For use
321dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  /// when the call site is not known.
322dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands  ///
32379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  bool onlyReadsMemory(const Function *F) {
324467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman    return onlyReadsMemory(getModRefBehavior(F));
325467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman  }
326467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman
327432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman  /// onlyReadsMemory - Return true if functions with the specified behavior are
328432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman  /// known to only read from non-volatile memory (or not access memory at all).
329467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman  ///
330467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman  static bool onlyReadsMemory(ModRefBehavior MRB) {
33142c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman    return !(MRB & Mod);
332248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner  }
3334df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
334432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman  /// onlyAccessesArgPointees - Return true if functions with the specified
335b395e4a0262c36b17b18aa33514b2daf2a85e9a3Dan Gohman  /// behavior are known to read and write at most from objects pointed to by
336b395e4a0262c36b17b18aa33514b2daf2a85e9a3Dan Gohman  /// their pointer-typed arguments (with arbitrary offsets).
337432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman  ///
338432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman  static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
339432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman    return !(MRB & Anywhere & ~ArgumentPointees);
340432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman  }
341432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman
34268a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman  /// doesAccessArgPointees - Return true if functions with the specified
34356169787dbfb6d0b422ef114889b481e977b8996Nick Lewycky  /// behavior are known to potentially read or write from objects pointed
34468a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman  /// to be their pointer-typed arguments (with arbitrary offsets).
34568a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman  ///
34668a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman  static bool doesAccessArgPointees(ModRefBehavior MRB) {
34768a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman    return (MRB & ModRef) && (MRB & ArgumentPointees);
34868a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman  }
34968a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman
3501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo - Return information about whether or not an instruction may
351b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// read or write the specified memory location.  An instruction
3521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// that doesn't read or write memory may be trivially LICM'd for example.
353fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  ModRefResult getModRefInfo(const Instruction *I,
354b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                             const Location &Loc) {
355fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman    switch (I->getOpcode()) {
356b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    case Instruction::VAArg:  return getModRefInfo((const VAArgInst*)I, Loc);
357b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    case Instruction::Load:   return getModRefInfo((const LoadInst*)I,  Loc);
358b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    case Instruction::Store:  return getModRefInfo((const StoreInst*)I, Loc);
3598a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman    case Instruction::Fence:  return getModRefInfo((const FenceInst*)I, Loc);
36055ba816883842e793cdeb32fcb805c4e011b527fEli Friedman    case Instruction::AtomicCmpXchg:
36155ba816883842e793cdeb32fcb805c4e011b527fEli Friedman      return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
36255ba816883842e793cdeb32fcb805c4e011b527fEli Friedman    case Instruction::AtomicRMW:
36355ba816883842e793cdeb32fcb805c4e011b527fEli Friedman      return getModRefInfo((const AtomicRMWInst*)I, Loc);
364b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    case Instruction::Call:   return getModRefInfo((const CallInst*)I,  Loc);
365b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
366fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman    default:                  return NoModRef;
367fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman    }
368fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  }
3691c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
370b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo - A convenience wrapper.
371b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(const Instruction *I,
3723da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman                             const Value *P, uint64_t Size) {
373b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(I, Location(P, Size));
374b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
375b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
3761c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  /// getModRefInfo (for call sites) - Return whether information about whether
377b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// a particular call site modifies or reads the specified memory location.
37879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
379b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                                     const Location &Loc);
380b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
381b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo (for call sites) - A convenience wrapper.
382b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(ImmutableCallSite CS,
3833da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman                             const Value *P, uint64_t Size) {
384b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(CS, Location(P, Size));
385b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
3861c56b730a6313886076d7b293a126ae5576f5288Chris Lattner
387fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// getModRefInfo (for calls) - Return whether information about whether
388b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// a particular call modifies or reads the specified memory location.
389b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
390b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(ImmutableCallSite(C), Loc);
391b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
392b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
393b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo (for calls) - A convenience wrapper.
3943da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman  ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
395b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(C, Location(P, Size));
3961c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
397fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman
398fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// getModRefInfo (for invokes) - Return whether information about whether
399b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// a particular invoke modifies or reads the specified memory location.
400b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(const InvokeInst *I,
401b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                             const Location &Loc) {
402b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(ImmutableCallSite(I), Loc);
403b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
404b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
405b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo (for invokes) - A convenience wrapper.
40679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman  ModRefResult getModRefInfo(const InvokeInst *I,
4073da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman                             const Value *P, uint64_t Size) {
408b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(I, Location(P, Size));
4091c56b730a6313886076d7b293a126ae5576f5288Chris Lattner  }
410fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman
411fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// getModRefInfo (for loads) - Return whether information about whether
412b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// a particular load modifies or reads the specified memory location.
413b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
414b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
415b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo (for loads) - A convenience wrapper.
4163da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman  ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
417b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(L, Location(P, Size));
418b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
419fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman
420fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// getModRefInfo (for stores) - Return whether information about whether
421b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// a particular store modifies or reads the specified memory location.
422b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
423b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
424b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo (for stores) - A convenience wrapper.
4252feee6454d4c9cbb3a104b0f593d31a91a14d15aChris Lattner  ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
426b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(S, Location(P, Size));
427b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
428fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman
4298a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  /// getModRefInfo (for fences) - Return whether information about whether
4308a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  /// a particular store modifies or reads the specified memory location.
4318a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
4328a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman    // Conservatively correct.  (We could possibly be a bit smarter if
4338a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman    // Loc is a alloca that doesn't escape.)
4348a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman    return ModRef;
4358a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  }
4368a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman
4378a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  /// getModRefInfo (for fences) - A convenience wrapper.
4388a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
4398a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman    return getModRefInfo(S, Location(P, Size));
4408a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman  }
4418a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman
44255ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  /// getModRefInfo (for cmpxchges) - Return whether information about whether
44355ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  /// a particular cmpxchg modifies or reads the specified memory location.
44446cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman  ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
44555ba816883842e793cdeb32fcb805c4e011b527fEli Friedman
44655ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  /// getModRefInfo (for cmpxchges) - A convenience wrapper.
44755ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
44855ba816883842e793cdeb32fcb805c4e011b527fEli Friedman                             const Value *P, unsigned Size) {
44955ba816883842e793cdeb32fcb805c4e011b527fEli Friedman    return getModRefInfo(CX, Location(P, Size));
45055ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  }
45155ba816883842e793cdeb32fcb805c4e011b527fEli Friedman
45255ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  /// getModRefInfo (for atomicrmws) - Return whether information about whether
45355ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  /// a particular atomicrmw modifies or reads the specified memory location.
45446cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman  ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
45555ba816883842e793cdeb32fcb805c4e011b527fEli Friedman
45655ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  /// getModRefInfo (for atomicrmws) - A convenience wrapper.
45755ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
45855ba816883842e793cdeb32fcb805c4e011b527fEli Friedman                             const Value *P, unsigned Size) {
45955ba816883842e793cdeb32fcb805c4e011b527fEli Friedman    return getModRefInfo(RMW, Location(P, Size));
46055ba816883842e793cdeb32fcb805c4e011b527fEli Friedman  }
46155ba816883842e793cdeb32fcb805c4e011b527fEli Friedman
462fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// getModRefInfo (for va_args) - Return whether information about whether
463b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// a particular va_arg modifies or reads the specified memory location.
464b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
465b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
466b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// getModRefInfo (for va_args) - A convenience wrapper.
4672feee6454d4c9cbb3a104b0f593d31a91a14d15aChris Lattner  ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
468b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return getModRefInfo(I, Location(P, Size));
469b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
470fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman
471fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// getModRefInfo - Return information about whether two call sites may refer
472fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// to the same set of memory locations.  See
473fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  ///   http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
474fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  /// for details.
475fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman  virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
476fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman                                     ImmutableCallSite CS2);
4774df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
4783a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier  /// callCapturesBefore - Return information about whether a particular call
4793a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier  /// site modifies or reads the specified memory location.
4803a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier  ModRefResult callCapturesBefore(const Instruction *I,
4813a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier                                  const AliasAnalysis::Location &MemLoc,
4823a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier                                  DominatorTree *DT);
4833a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier
4843a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier  /// callCapturesBefore - A convenience wrapper.
4853a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier  ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
4863a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier                                  uint64_t Size, DominatorTree *DT) {
4873a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier    return callCapturesBefore(I, Location(P, Size), DT);
4883a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier  }
4893a884f5c17ac32e34e7e62b4602a0d73eeda1ce8Chad Rosier
490ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
491ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Higher level methods for querying mod/ref information.
492ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
493ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
494f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canBasicBlockModify - Return true if it is possible for execution of the
495f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// specified basic block to modify the value pointed to by Ptr.
496b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
497b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
498b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// canBasicBlockModify - A convenience wrapper.
4993da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
500b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return canBasicBlockModify(BB, Location(P, Size));
501b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
5024df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
503f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// canInstructionRangeModify - Return true if it is possible for the
504f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// execution of the specified instructions to modify the value pointed to by
505f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// Ptr.  The instructions to consider are all of the instructions in the
506f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
5074df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
508b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman                                 const Location &Loc);
509b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman
510b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  /// canInstructionRangeModify - A convenience wrapper.
511b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
5123da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman                                 const Value *Ptr, uint64_t Size) {
513b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
514b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman  }
515ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
516ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  //===--------------------------------------------------------------------===//
517ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// Methods that clients should call when they transform the program to allow
518ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// alias analyses to update their internal data structures.  Note that these
519ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// methods may be called on any instruction, regardless of whether or not
520ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// they have pointer-analysis implications.
521ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
522ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
523ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleteValue - This method should be called whenever an LLVM Value is
524ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// deleted from the program, for example when an instruction is found to be
525ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// redundant and is eliminated.
526ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
527ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void deleteValue(Value *V);
528ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
529ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// copyValue - This method should be used whenever a preexisting value in the
530ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// program is copied or cloned, introducing a new value.  Note that analysis
531ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// implementations should tolerate clients that use this method to introduce
532ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// the same value multiple times: if the analysis already knows about a
533ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// value, it should ignore the request.
534ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
535ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  virtual void copyValue(Value *From, Value *To);
536ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner
537ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  /// addEscapingUse - This method should be used whenever an escaping use is
538ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  /// added to a pointer value.  Analysis implementations may either return
539ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  /// conservative responses for that value in the future, or may recompute
540ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  /// some or all internal state to continue providing precise responses.
541ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  ///
542ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  /// Escaping uses are considered by anything _except_ the following:
543ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  ///  - GEPs or bitcasts of the pointer
544ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  ///  - Loads through the pointer
545ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  ///  - Stores through (but not of) the pointer
546ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson  virtual void addEscapingUse(Use &U);
547ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson
548ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// replaceWithNewValue - This method is the obvious combination of the two
549ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  /// above, and it provided as a helper to simplify client code.
550ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  ///
551ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  void replaceWithNewValue(Value *Old, Value *New) {
552ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    copyValue(Old, New);
553ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner    deleteValue(Old);
554ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner  }
5554df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner};
5564df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner
5571fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman// Specialize DenseMapInfo for Location.
5581fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohmantemplate<>
5591fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohmanstruct DenseMapInfo<AliasAnalysis::Location> {
5601fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  static inline AliasAnalysis::Location getEmptyKey() {
5611fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman    return
5621fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman      AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(),
5631fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman                              0, 0);
5641fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  }
5651fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  static inline AliasAnalysis::Location getTombstoneKey() {
5661fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman    return
5671fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman      AliasAnalysis::Location(DenseMapInfo<const Value *>::getTombstoneKey(),
5681fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman                              0, 0);
5691fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  }
5701fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  static unsigned getHashValue(const AliasAnalysis::Location &Val) {
5711fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman    return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
5721fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman           DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
5731fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman           DenseMapInfo<const MDNode *>::getHashValue(Val.TBAATag);
5741fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  }
5751fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  static bool isEqual(const AliasAnalysis::Location &LHS,
5761fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman                      const AliasAnalysis::Location &RHS) {
5771fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman    return LHS.Ptr == RHS.Ptr &&
5781fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman           LHS.Size == RHS.Size &&
5791fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman           LHS.TBAATag == RHS.TBAATag;
5801fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman  }
5811fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman};
5821fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman
583a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// isNoAliasCall - Return true if this pointer is returned by a noalias
584a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// function.
585a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohmanbool isNoAliasCall(const Value *V);
586a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman
5873311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// isIdentifiedObject - Return true if this pointer refers to a distinct and
588a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// identifiable object.  This returns true for:
5895753a4a0033da4add45f2e9930a4e1159d92a869Dan Gohman///    Global Variables and Functions (but not Global Aliases)
590a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman///    Allocas and Mallocs
5919e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman///    ByVal and NoAlias Arguments
5929e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman///    NoAlias returns
5933311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman///
5949e86f4364b912ae743490ba01d6989acfd12c046Dan Gohmanbool isIdentifiedObject(const Value *V);
5953311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman
59655f4ab84e567ac101d70f052771d1bc67a7560e3Nick Lewycky/// isKnownNonNull - Return true if this pointer couldn't possibly be null by
59755f4ab84e567ac101d70f052771d1bc67a7560e3Nick Lewycky/// its definition.  This returns true for allocas, non-extern-weak globals and
59855f4ab84e567ac101d70f052771d1bc67a7560e3Nick Lewycky/// byval arguments.
59955f4ab84e567ac101d70f052771d1bc67a7560e3Nick Lewyckybool isKnownNonNull(const Value *V);
60055f4ab84e567ac101d70f052771d1bc67a7560e3Nick Lewycky
6014f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace
6024f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer
6034df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif
604