AliasAnalysis.h revision 46cb5afdcd031c371c78201fb34291d9d48b2ee4
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; 481c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass TargetData; 496df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass Pass; 506df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass AnalysisUsage; 51e90c5cb747631b315350e7ee7424048c7778bdf9Chris Lattnerclass MemTransferInst; 529dc9e81aa7412b329bbaf51a589a81475214802bChris Lattnerclass MemIntrinsic; 531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 541c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass AliasAnalysis { 551c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerprotected: 56ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner const TargetData *TD; 57cb74993bdc37681ddbb80fa361575107afae1350Dan Gohman 58cb74993bdc37681ddbb80fa361575107afae1350Dan Gohmanprivate: 59ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner AliasAnalysis *AA; // Previous Alias Analysis to chain to. 60ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 61cb74993bdc37681ddbb80fa361575107afae1350Dan Gohmanprotected: 621c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// InitializeAliasAnalysis - Subclasses must call this method to initialize 631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// the AliasAnalysis interface before any other methods are called. This is 641c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// typically called by the run* methods of these subclasses. This may be 651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// called multiple times. 661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 671c56b730a6313886076d7b293a126ae5576f5288Chris Lattner void InitializeAliasAnalysis(Pass *P); 689769ab22265b313171d201b5928688524a01bd87Misha Brukman 6964d237cfae6bcb11157d525c79b5a5335e30370bDan Gohman /// getAnalysisUsage - All alias analysis implementations should invoke this 70fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// directly (using AliasAnalysis::getAnalysisUsage(AU)). 711c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const; 721c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 731c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic: 741997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel static char ID; // Class identification, replacement for typeinfo 75ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner AliasAnalysis() : TD(0), AA(0) {} 761c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual ~AliasAnalysis(); // We want to be subclassed 771c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 78ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// UnknownSize - This is a special value which can be used with the 79ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// size arguments in alias queries to indicate that the caller does not 80ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// know the sizes of the potential memory references. 813da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman static uint64_t const UnknownSize = ~UINT64_C(0); 82ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman 83fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// getTargetData - Return a pointer to the current TargetData object, or 84fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// null if no TargetData object is available. 85dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman /// 86fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman const TargetData *getTargetData() const { return TD; } 87fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman 88fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// getTypeStoreSize - Return the TargetData store size for the given type, 89fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// if known, or a conservative value otherwise. 90fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// 91db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner uint64_t getTypeStoreSize(Type *Ty); 924df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 931c56b730a6313886076d7b293a126ae5576f5288Chris Lattner //===--------------------------------------------------------------------===// 941c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// Alias Queries... 951c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 964df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 97b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// Location - A description of a memory location. 98b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman struct Location { 99b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// Ptr - The address of the start of the location. 100b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const Value *Ptr; 101a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman /// Size - The maximum size of the location, in address-units, or 102a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman /// UnknownSize if the size is not known. Note that an unknown size does 103a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman /// not mean the pointer aliases the entire virtual address space, because 104a6d60ddbbb0c33690f7e89cad2b5e60cee84b89eDan Gohman /// there are restrictions on stepping out of one object and into another. 1056c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman /// See http://llvm.org/docs/LangRef.html#pointeraliasing 1063da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman uint64_t Size; 107b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// TBAATag - The metadata node which describes the TBAA type of 1086c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman /// the location, or null if there is no known unique tag. 109b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const MDNode *TBAATag; 110b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 111c413330c99a573ef3ffe80a46400b1d3eca2398dChris Lattner explicit Location(const Value *P = 0, uint64_t S = UnknownSize, 112b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const MDNode *N = 0) 113b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman : Ptr(P), Size(S), TBAATag(N) {} 1149f27074f42f03af640977006dc7c05005fb77991Dan Gohman 1159f27074f42f03af640977006dc7c05005fb77991Dan Gohman Location getWithNewPtr(const Value *NewPtr) const { 1169f27074f42f03af640977006dc7c05005fb77991Dan Gohman Location Copy(*this); 1179f27074f42f03af640977006dc7c05005fb77991Dan Gohman Copy.Ptr = NewPtr; 1189f27074f42f03af640977006dc7c05005fb77991Dan Gohman return Copy; 1199f27074f42f03af640977006dc7c05005fb77991Dan Gohman } 1209f27074f42f03af640977006dc7c05005fb77991Dan Gohman 121075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman Location getWithNewSize(uint64_t NewSize) const { 122075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman Location Copy(*this); 123075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman Copy.Size = NewSize; 124075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman return Copy; 125075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman } 126075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman 1279f27074f42f03af640977006dc7c05005fb77991Dan Gohman Location getWithoutTBAATag() const { 1289f27074f42f03af640977006dc7c05005fb77991Dan Gohman Location Copy(*this); 1299f27074f42f03af640977006dc7c05005fb77991Dan Gohman Copy.TBAATag = 0; 1309f27074f42f03af640977006dc7c05005fb77991Dan Gohman return Copy; 1319f27074f42f03af640977006dc7c05005fb77991Dan Gohman } 132b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman }; 133b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 1346d8eb156e6be727570b300bac7712f745a318c7dDan Gohman /// getLocation - Fill in Loc with information about the memory reference by 1356d8eb156e6be727570b300bac7712f745a318c7dDan Gohman /// the given instruction. 1366d8eb156e6be727570b300bac7712f745a318c7dDan Gohman Location getLocation(const LoadInst *LI); 1376d8eb156e6be727570b300bac7712f745a318c7dDan Gohman Location getLocation(const StoreInst *SI); 1386d8eb156e6be727570b300bac7712f745a318c7dDan Gohman Location getLocation(const VAArgInst *VI); 13946cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman Location getLocation(const AtomicCmpXchgInst *CXI); 14046cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman Location getLocation(const AtomicRMWInst *RMWI); 14116f7993e0df4bd0727f8535bd47a2776bec10545Chris Lattner static Location getLocationForSource(const MemTransferInst *MTI); 1429dc9e81aa7412b329bbaf51a589a81475214802bChris Lattner static Location getLocationForDest(const MemIntrinsic *MI); 1436d8eb156e6be727570b300bac7712f745a318c7dDan Gohman 144f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Alias analysis result - Either we know for sure that it does not alias, we 145f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// know for sure it must alias, or we don't know anything: The two pointers 146f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// _might_ alias. This enum is designed so you can do things like: 147f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// if (AA.alias(P1, P2)) { ... } 148f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// to check to see if two pointers might alias. 149f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 150e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// See docs/AliasAnalysis.html for more information on the specific meanings 151e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// of these values. 152e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// 15317e8078ae14ed71f657d59c77efa3bedf171161aDan Gohman enum AliasResult { 15417e8078ae14ed71f657d59c77efa3bedf171161aDan Gohman NoAlias = 0, ///< No dependencies. 1554a34cbd2b9ba9efcdab4c4e656df2d7bd22e2604Dan Gohman MayAlias, ///< Anything goes. 1564a34cbd2b9ba9efcdab4c4e656df2d7bd22e2604Dan Gohman PartialAlias, ///< Pointers differ, but pointees overlap. 1574a34cbd2b9ba9efcdab4c4e656df2d7bd22e2604Dan Gohman MustAlias ///< Pointers are equal. 15817e8078ae14ed71f657d59c77efa3bedf171161aDan Gohman }; 1594df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 160f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// alias - The main low level interface to the alias analysis implementation. 1616c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman /// Returns an AliasResult indicating whether the two pointers are aliased to 1626c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman /// each other. This is the interface that must be implemented by specific 1636c25c920e66aa2d8f6b48454bc9770087ff7939aDan Gohman /// alias analysis implementations. 164b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman virtual AliasResult alias(const Location &LocA, const Location &LocB); 1654df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 166b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// alias - A convenience wrapper. 1673da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman AliasResult alias(const Value *V1, uint64_t V1Size, 1683da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman const Value *V2, uint64_t V2Size) { 169b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return alias(Location(V1, V1Size), Location(V2, V2Size)); 170b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 171b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 172b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// alias - A convenience wrapper. 173847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman AliasResult alias(const Value *V1, const Value *V2) { 174ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman return alias(V1, UnknownSize, V2, UnknownSize); 175847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman } 176847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman 177c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner /// isNoAlias - A trivial helper function to check to see if the specified 178c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner /// pointers are no-alias. 179b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman bool isNoAlias(const Location &LocA, const Location &LocB) { 180b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return alias(LocA, LocB) == NoAlias; 181b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 182b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 183b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// isNoAlias - A convenience wrapper. 1843da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman bool isNoAlias(const Value *V1, uint64_t V1Size, 1853da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman const Value *V2, uint64_t V2Size) { 186b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return isNoAlias(Location(V1, V1Size), Location(V2, V2Size)); 187c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner } 18872c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner 18972c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner /// isMustAlias - A convenience wrapper. 19072c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner bool isMustAlias(const Location &LocA, const Location &LocB) { 19172c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner return alias(LocA, LocB) == MustAlias; 19272c194a8be83d217360ebc6b1f3ad21c5ffa16a9Chris Lattner } 193c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner 194cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner /// isMustAlias - A convenience wrapper. 195cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner bool isMustAlias(const Value *V1, const Value *V2) { 196cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner return alias(V1, 1, V2, 1) == MustAlias; 197cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner } 198cc10244d7725f191bdc91cd62befff0c97257c7bChris Lattner 199a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman /// pointsToConstantMemory - If the specified memory location is 200a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman /// known to be constant, return true. If OrLocal is true and the 201a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman /// specified memory location is known to be "local" (derived from 202a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman /// an alloca), return true. Otherwise return false. 203a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman virtual bool pointsToConstantMemory(const Location &Loc, 204a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman bool OrLocal = false); 205b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 206b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// pointsToConstantMemory - A convenient wrapper. 207a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman bool pointsToConstantMemory(const Value *P, bool OrLocal = false) { 208a25e5dbcc2371352386a01e3c1b8e76dd890272bDan Gohman return pointsToConstantMemory(Location(P), OrLocal); 209b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 21062b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner 211248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner //===--------------------------------------------------------------------===// 212248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// Simple mod/ref information... 213248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// 214248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner 215248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 216248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// bits which may be or'd together. 217248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// 218248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 2199769ab22265b313171d201b5928688524a01bd87Misha Brukman 22042c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman /// These values define additional bits used to define the 22142c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman /// ModRefBehavior values. 22242c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees }; 2239769ab22265b313171d201b5928688524a01bd87Misha Brukman 224248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// ModRefBehavior - Summary of how a function affects memory in the program. 225248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// Loads from constant globals are not considered memory accesses for this 226248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// interface. Also, functions may freely modify stack space local to their 227248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// invocation without having to report it through these interfaces. 228248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner enum ModRefBehavior { 22950a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// DoesNotAccessMemory - This function does not perform any non-local loads 23050a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// or stores to memory. 23150a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// 23250a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the GCC 'const' attribute. 23350a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the LLVM IR 'readnone' attribute. 23450a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the IntrNoMem LLVM intrinsic flag. 23542c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman DoesNotAccessMemory = Nowhere | NoModRef, 2369769ab22265b313171d201b5928688524a01bd87Misha Brukman 237e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman /// OnlyReadsArgumentPointees - The only memory references in this function 238e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman /// (if it has any) are non-volatile loads from objects pointed to by its 239e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman /// pointer-typed arguments, with arbitrary offsets. 24050a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// 24150a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag. 242e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman OnlyReadsArgumentPointees = ArgumentPointees | Ref, 243db78c4873ea8c4b3dce90d761a1ad59d5bcdd6e7Dan Gohman 244e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman /// OnlyAccessesArgumentPointees - The only memory references in this 245e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman /// function (if it has any) are non-volatile loads and stores from objects 246e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman /// pointed to by its pointer-typed arguments, with arbitrary offsets. 24750a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// 24850a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag. 249e88ccb545d0e03fd4445d361100fc95f350c6663Dan Gohman OnlyAccessesArgumentPointees = ArgumentPointees | ModRef, 2509769ab22265b313171d201b5928688524a01bd87Misha Brukman 25150a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// OnlyReadsMemory - This function does not perform any non-local stores or 25250a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// volatile loads, but may read from any memory location. 25350a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// 25450a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the GCC 'pure' attribute. 25550a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the LLVM IR 'readonly' attribute. 25650a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// This property corresponds to the IntrReadMem LLVM intrinsic flag. 25742c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman OnlyReadsMemory = Anywhere | Ref, 2589769ab22265b313171d201b5928688524a01bd87Misha Brukman 25950a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// UnknownModRefBehavior - This indicates that the function could not be 26050a04d067f8803d52cababdabe5c188844c5c210Dan Gohman /// classified into one of the behaviors above. 26142c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman UnknownModRefBehavior = Anywhere | ModRef 262248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner }; 2639769ab22265b313171d201b5928688524a01bd87Misha Brukman 264dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// getModRefBehavior - Return the behavior when calling the given call site. 265a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 266dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 267dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// getModRefBehavior - Return the behavior when calling the given function. 268dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// For use when the call site is not known. 269a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman virtual ModRefBehavior getModRefBehavior(const Function *F); 270dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 271dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// doesNotAccessMemory - If the specified call is known to never read or 272dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// write memory, return true. If the call only reads from known-constant 273dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// memory, it is also legal to return true. Calls that unwind the stack 274dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// are legal for this predicate. 2753e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 276dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// Many optimizations (such as CSE and LICM) can be performed on such calls 277dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// without worrying about aliasing properties, and many calls have this 278dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// property (e.g. calls to 'sin' and 'cos'). 2793e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 2803e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property corresponds to the GCC 'const' attribute. 2813e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 28279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool doesNotAccessMemory(ImmutableCallSite CS) { 283652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng return getModRefBehavior(CS) == DoesNotAccessMemory; 284dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands } 285dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 286dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// doesNotAccessMemory - If the specified function is known to never read or 287dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// write memory, return true. For use when the call site is not known. 288dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// 28979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool doesNotAccessMemory(const Function *F) { 290dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands return getModRefBehavior(F) == DoesNotAccessMemory; 291248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner } 2923e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner 293dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// onlyReadsMemory - If the specified call is known to only read from 294dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// non-volatile memory (or not access memory at all), return true. Calls 295dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// that unwind the stack are legal for this predicate. 2963e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 2973e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property allows many common optimizations to be performed in the 2983e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// absence of interfering store instructions, such as CSE of strlen calls. 2993e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 3003e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property corresponds to the GCC 'pure' attribute. 3013e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 30279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool onlyReadsMemory(ImmutableCallSite CS) { 303467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman return onlyReadsMemory(getModRefBehavior(CS)); 304dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands } 305dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 306dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// onlyReadsMemory - If the specified function is known to only read from 307dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// non-volatile memory (or not access memory at all), return true. For use 308dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// when the call site is not known. 309dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// 31079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool onlyReadsMemory(const Function *F) { 311467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman return onlyReadsMemory(getModRefBehavior(F)); 312467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman } 313467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman 314432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman /// onlyReadsMemory - Return true if functions with the specified behavior are 315432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman /// known to only read from non-volatile memory (or not access memory at all). 316467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman /// 317467a0adfc8b6bd4b114e024c200c159497a4cd86Dan Gohman static bool onlyReadsMemory(ModRefBehavior MRB) { 31842c31a70735e55bf82e66a9315c97d1821c9a798Dan Gohman return !(MRB & Mod); 319248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner } 3204df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 321432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman /// onlyAccessesArgPointees - Return true if functions with the specified 322b395e4a0262c36b17b18aa33514b2daf2a85e9a3Dan Gohman /// behavior are known to read and write at most from objects pointed to by 323b395e4a0262c36b17b18aa33514b2daf2a85e9a3Dan Gohman /// their pointer-typed arguments (with arbitrary offsets). 324432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman /// 325432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman static bool onlyAccessesArgPointees(ModRefBehavior MRB) { 326432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman return !(MRB & Anywhere & ~ArgumentPointees); 327432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman } 328432d08cbdb9ceaa333f1d6eab4f8b542fdddf9dbDan Gohman 32968a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman /// doesAccessArgPointees - Return true if functions with the specified 33068a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman /// behavior are known to potentially read or write from objects pointed 33168a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman /// to be their pointer-typed arguments (with arbitrary offsets). 33268a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman /// 33368a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman static bool doesAccessArgPointees(ModRefBehavior MRB) { 33468a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman return (MRB & ModRef) && (MRB & ArgumentPointees); 33568a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman } 33668a6056dafd4913ce42606353ab1ff7208215ff2Dan Gohman 3371c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo - Return information about whether or not an instruction may 338b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// read or write the specified memory location. An instruction 3391c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// that doesn't read or write memory may be trivially LICM'd for example. 340fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman ModRefResult getModRefInfo(const Instruction *I, 341b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const Location &Loc) { 342fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman switch (I->getOpcode()) { 343b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc); 344b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc); 345b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc); 3468a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc); 34755ba816883842e793cdeb32fcb805c4e011b527fEli Friedman case Instruction::AtomicCmpXchg: 34855ba816883842e793cdeb32fcb805c4e011b527fEli Friedman return getModRefInfo((const AtomicCmpXchgInst*)I, Loc); 34955ba816883842e793cdeb32fcb805c4e011b527fEli Friedman case Instruction::AtomicRMW: 35055ba816883842e793cdeb32fcb805c4e011b527fEli Friedman return getModRefInfo((const AtomicRMWInst*)I, Loc); 351b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc); 352b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc); 353fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman default: return NoModRef; 354fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman } 355fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman } 3561c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 357b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo - A convenience wrapper. 358b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(const Instruction *I, 3593da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman const Value *P, uint64_t Size) { 360b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(I, Location(P, Size)); 361b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 362b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 3631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo (for call sites) - Return whether information about whether 364b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// a particular call site modifies or reads the specified memory location. 36579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 366b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const Location &Loc); 367b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 368b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo (for call sites) - A convenience wrapper. 369b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(ImmutableCallSite CS, 3703da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman const Value *P, uint64_t Size) { 371b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(CS, Location(P, Size)); 372b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 3731c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 374fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// getModRefInfo (for calls) - Return whether information about whether 375b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// a particular call modifies or reads the specified memory location. 376b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) { 377b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(ImmutableCallSite(C), Loc); 378b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 379b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 380b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo (for calls) - A convenience wrapper. 3813da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) { 382b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(C, Location(P, Size)); 3831c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 384fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman 385fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// getModRefInfo (for invokes) - Return whether information about whether 386b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// a particular invoke modifies or reads the specified memory location. 387b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(const InvokeInst *I, 388b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const Location &Loc) { 389b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(ImmutableCallSite(I), Loc); 390b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 391b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 392b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo (for invokes) - A convenience wrapper. 39379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const InvokeInst *I, 3943da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman const Value *P, uint64_t Size) { 395b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(I, Location(P, Size)); 3961c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 397fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman 398fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// getModRefInfo (for loads) - Return whether information about whether 399b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// a particular load modifies or reads the specified memory location. 400b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc); 401b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 402b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo (for loads) - A convenience wrapper. 4033da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) { 404b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(L, Location(P, Size)); 405b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 406fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman 407fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// getModRefInfo (for stores) - Return whether information about whether 408b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// a particular store modifies or reads the specified memory location. 409b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc); 410b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 411b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo (for stores) - A convenience wrapper. 4122feee6454d4c9cbb3a104b0f593d31a91a14d15aChris Lattner ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){ 413b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(S, Location(P, Size)); 414b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 415fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman 4168a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman /// getModRefInfo (for fences) - Return whether information about whether 4178a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman /// a particular store modifies or reads the specified memory location. 4188a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) { 4198a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman // Conservatively correct. (We could possibly be a bit smarter if 4208a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman // Loc is a alloca that doesn't escape.) 4218a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman return ModRef; 4228a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman } 4238a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman 4248a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman /// getModRefInfo (for fences) - A convenience wrapper. 4258a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){ 4268a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman return getModRefInfo(S, Location(P, Size)); 4278a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman } 4288a552bb85a5e9a6c250c0a899941fbd3ae7b5006Eli Friedman 42955ba816883842e793cdeb32fcb805c4e011b527fEli Friedman /// getModRefInfo (for cmpxchges) - Return whether information about whether 43055ba816883842e793cdeb32fcb805c4e011b527fEli Friedman /// a particular cmpxchg modifies or reads the specified memory location. 43146cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc); 43255ba816883842e793cdeb32fcb805c4e011b527fEli Friedman 43355ba816883842e793cdeb32fcb805c4e011b527fEli Friedman /// getModRefInfo (for cmpxchges) - A convenience wrapper. 43455ba816883842e793cdeb32fcb805c4e011b527fEli Friedman ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, 43555ba816883842e793cdeb32fcb805c4e011b527fEli Friedman const Value *P, unsigned Size) { 43655ba816883842e793cdeb32fcb805c4e011b527fEli Friedman return getModRefInfo(CX, Location(P, Size)); 43755ba816883842e793cdeb32fcb805c4e011b527fEli Friedman } 43855ba816883842e793cdeb32fcb805c4e011b527fEli Friedman 43955ba816883842e793cdeb32fcb805c4e011b527fEli Friedman /// getModRefInfo (for atomicrmws) - Return whether information about whether 44055ba816883842e793cdeb32fcb805c4e011b527fEli Friedman /// a particular atomicrmw modifies or reads the specified memory location. 44146cb5afdcd031c371c78201fb34291d9d48b2ee4Eli Friedman ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc); 44255ba816883842e793cdeb32fcb805c4e011b527fEli Friedman 44355ba816883842e793cdeb32fcb805c4e011b527fEli Friedman /// getModRefInfo (for atomicrmws) - A convenience wrapper. 44455ba816883842e793cdeb32fcb805c4e011b527fEli Friedman ModRefResult getModRefInfo(const AtomicRMWInst *RMW, 44555ba816883842e793cdeb32fcb805c4e011b527fEli Friedman const Value *P, unsigned Size) { 44655ba816883842e793cdeb32fcb805c4e011b527fEli Friedman return getModRefInfo(RMW, Location(P, Size)); 44755ba816883842e793cdeb32fcb805c4e011b527fEli Friedman } 44855ba816883842e793cdeb32fcb805c4e011b527fEli Friedman 449fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// getModRefInfo (for va_args) - Return whether information about whether 450b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// a particular va_arg modifies or reads the specified memory location. 451b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc); 452b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 453b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// getModRefInfo (for va_args) - A convenience wrapper. 4542feee6454d4c9cbb3a104b0f593d31a91a14d15aChris Lattner ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){ 455b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return getModRefInfo(I, Location(P, Size)); 456b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 457fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman 458fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// getModRefInfo - Return information about whether two call sites may refer 459fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// to the same set of memory locations. See 460fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 461fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman /// for details. 462fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 463fa6cb8d8211579f1370e80b7bac54ebe3fbe35c8Dan Gohman ImmutableCallSite CS2); 4644df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 465ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner //===--------------------------------------------------------------------===// 466ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// Higher level methods for querying mod/ref information. 467ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 468ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 469f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// canBasicBlockModify - Return true if it is possible for execution of the 470f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// specified basic block to modify the value pointed to by Ptr. 471b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc); 472b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 473b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// canBasicBlockModify - A convenience wrapper. 4743da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){ 475b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return canBasicBlockModify(BB, Location(P, Size)); 476b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 4774df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 478f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// canInstructionRangeModify - Return true if it is possible for the 479f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// execution of the specified instructions to modify the value pointed to by 480f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Ptr. The instructions to consider are all of the instructions in the 481f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 4824df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 483b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman const Location &Loc); 484b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman 485b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman /// canInstructionRangeModify - A convenience wrapper. 486b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 4873da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman const Value *Ptr, uint64_t Size) { 488b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman return canInstructionRangeModify(I1, I2, Location(Ptr, Size)); 489b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman } 490ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 491ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner //===--------------------------------------------------------------------===// 492ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// Methods that clients should call when they transform the program to allow 493ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// alias analyses to update their internal data structures. Note that these 494ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// methods may be called on any instruction, regardless of whether or not 495ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// they have pointer-analysis implications. 496ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 497ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 498ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// deleteValue - This method should be called whenever an LLVM Value is 499ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// deleted from the program, for example when an instruction is found to be 500ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// redundant and is eliminated. 501ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 502ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual void deleteValue(Value *V); 503ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 504ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// copyValue - This method should be used whenever a preexisting value in the 505ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// program is copied or cloned, introducing a new value. Note that analysis 506ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// implementations should tolerate clients that use this method to introduce 507ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// the same value multiple times: if the analysis already knows about a 508ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// value, it should ignore the request. 509ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 510ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual void copyValue(Value *From, Value *To); 511ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 512ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// addEscapingUse - This method should be used whenever an escaping use is 513ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// added to a pointer value. Analysis implementations may either return 514ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// conservative responses for that value in the future, or may recompute 515ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// some or all internal state to continue providing precise responses. 516ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// 517ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// Escaping uses are considered by anything _except_ the following: 518ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// - GEPs or bitcasts of the pointer 519ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// - Loads through the pointer 520ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson /// - Stores through (but not of) the pointer 521ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson virtual void addEscapingUse(Use &U); 522ab6acc6ecdc4585a55059e36d81481d1c26d3ff9Owen Anderson 523ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// replaceWithNewValue - This method is the obvious combination of the two 524ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// above, and it provided as a helper to simplify client code. 525ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 526ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner void replaceWithNewValue(Value *Old, Value *New) { 527ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner copyValue(Old, New); 528ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner deleteValue(Old); 529ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner } 5304df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner}; 5314df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 5321fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman// Specialize DenseMapInfo for Location. 5331fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohmantemplate<> 5341fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohmanstruct DenseMapInfo<AliasAnalysis::Location> { 5351fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman static inline AliasAnalysis::Location getEmptyKey() { 5361fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman return 5371fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(), 5381fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman 0, 0); 5391fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman } 5401fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman static inline AliasAnalysis::Location getTombstoneKey() { 5411fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman return 5421fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman AliasAnalysis::Location(DenseMapInfo<const Value *>::getTombstoneKey(), 5431fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman 0, 0); 5441fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman } 5451fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman static unsigned getHashValue(const AliasAnalysis::Location &Val) { 5461fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^ 5471fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^ 5481fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman DenseMapInfo<const MDNode *>::getHashValue(Val.TBAATag); 5491fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman } 5501fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman static bool isEqual(const AliasAnalysis::Location &LHS, 5511fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman const AliasAnalysis::Location &RHS) { 5521fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman return LHS.Ptr == RHS.Ptr && 5531fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman LHS.Size == RHS.Size && 5541fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman LHS.TBAATag == RHS.TBAATag; 5551fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman } 5561fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman}; 5571fc18d71deb0e23a9101c87bb7b1455098ce1c09Dan Gohman 558a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// isNoAliasCall - Return true if this pointer is returned by a noalias 559a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// function. 560a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohmanbool isNoAliasCall(const Value *V); 561a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman 5623311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// isIdentifiedObject - Return true if this pointer refers to a distinct and 563a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// identifiable object. This returns true for: 5645753a4a0033da4add45f2e9930a4e1159d92a869Dan Gohman/// Global Variables and Functions (but not Global Aliases) 565a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// Allocas and Mallocs 5669e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman/// ByVal and NoAlias Arguments 5679e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman/// NoAlias returns 5683311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// 5699e86f4364b912ae743490ba01d6989acfd12c046Dan Gohmanbool isIdentifiedObject(const Value *V); 5703311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman 5714f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace 5724f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer 5734df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif 574