AliasAnalysis.h revision a41af344c2964573318a77e2b43eafd4d3804003
14df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===// 29769ab22265b313171d201b5928688524a01bd87Misha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 79769ab22265b313171d201b5928688524a01bd87Misha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 94df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// 104df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// This file defines the generic AliasAnalysis interface, which is used as the 114df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// common interface used by all clients of alias analysis information, and 121c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// implemented by all alias analysis implementations. Mod/Ref information is 131c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// also captured by this interface. 144df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// 154df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// Implementations of this interface must implement the various virtual methods, 164df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// which automatically provides functionality for the entire suite of client 174df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// APIs. 184df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner// 191c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// This API represents memory as a (Pointer, Size) pair. The Pointer component 201c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// specifies the base memory address of the region, the Size specifies how large 21e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman// of an area is being queried, or UnknownSize if the size is not known. 22e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman// Pointers that point to two completely different objects in memory never 23e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman// alias, regardless of the value of the Size component. 241c56b730a6313886076d7b293a126ae5576f5288Chris Lattner// 254df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner//===----------------------------------------------------------------------===// 264df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 274df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H 284df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H 294df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 301c56b730a6313886076d7b293a126ae5576f5288Chris Lattner#include "llvm/Support/CallSite.h" 317107c3badfe78ec89dcab6c02cf1b1bcaccc42a8Reid Spencer#include "llvm/System/IncludeFile.h" 3236f78c8935da34074ccd06d5674e45b9cd45da8bChris Lattner#include <vector> 33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 35d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 361c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass LoadInst; 371c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass StoreInst; 3814f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharthclass VAArgInst; 391c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass TargetData; 406df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass Pass; 416df60a9effe4d20a48cfd9d105c0ab3c5dc3e690Reid Spencerclass AnalysisUsage; 421c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 431c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerclass AliasAnalysis { 441c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerprotected: 45ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner const TargetData *TD; 46ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner AliasAnalysis *AA; // Previous Alias Analysis to chain to. 47ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 481c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// InitializeAliasAnalysis - Subclasses must call this method to initialize 491c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// the AliasAnalysis interface before any other methods are called. This is 501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// typically called by the run* methods of these subclasses. This may be 511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// called multiple times. 521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner void InitializeAliasAnalysis(Pass *P); 549769ab22265b313171d201b5928688524a01bd87Misha Brukman 5564d237cfae6bcb11157d525c79b5a5335e30370bDan Gohman /// getAnalysisUsage - All alias analysis implementations should invoke this 56fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// directly (using AliasAnalysis::getAnalysisUsage(AU)). 571c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const; 581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 591c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic: 601997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel static char ID; // Class identification, replacement for typeinfo 61ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner AliasAnalysis() : TD(0), AA(0) {} 621c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual ~AliasAnalysis(); // We want to be subclassed 631c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 64ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// UnknownSize - This is a special value which can be used with the 65ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// size arguments in alias queries to indicate that the caller does not 66ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// know the sizes of the potential memory references. 67ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman static unsigned const UnknownSize = ~0u; 68ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman 69fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// getTargetData - Return a pointer to the current TargetData object, or 70fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// null if no TargetData object is available. 71dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman /// 72fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman const TargetData *getTargetData() const { return TD; } 73fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman 74fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// getTypeStoreSize - Return the TargetData store size for the given type, 75fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// if known, or a conservative value otherwise. 76fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// 77fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman unsigned getTypeStoreSize(const Type *Ty); 784df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 791c56b730a6313886076d7b293a126ae5576f5288Chris Lattner //===--------------------------------------------------------------------===// 801c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// Alias Queries... 811c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 824df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 83f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Alias analysis result - Either we know for sure that it does not alias, we 84f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// know for sure it must alias, or we don't know anything: The two pointers 85f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// _might_ alias. This enum is designed so you can do things like: 86f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// if (AA.alias(P1, P2)) { ... } 87f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// to check to see if two pointers might alias. 88f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 89e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// See docs/AliasAnalysis.html for more information on the specific meanings 90e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// of these values. 91e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// 921c56b730a6313886076d7b293a126ae5576f5288Chris Lattner enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 }; 934df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 94f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// alias - The main low level interface to the alias analysis implementation. 95f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Returns a Result indicating whether the two pointers are aliased to each 96f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// other. This is the interface that must be implemented by specific alias 97f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// analysis implementations. 98f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 991c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual AliasResult alias(const Value *V1, unsigned V1Size, 100ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner const Value *V2, unsigned V2Size); 1014df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 102847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman /// alias - A convenience wrapper for the case where the sizes are unknown. 103847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman AliasResult alias(const Value *V1, const Value *V2) { 104ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman return alias(V1, UnknownSize, V2, UnknownSize); 105847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman } 106847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman 107c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner /// isNoAlias - A trivial helper function to check to see if the specified 108c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner /// pointers are no-alias. 109c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner bool isNoAlias(const Value *V1, unsigned V1Size, 110c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner const Value *V2, unsigned V2Size) { 111c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner return alias(V1, V1Size, V2, V2Size) == NoAlias; 112c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner } 113c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner 114762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// pointsToConstantMemory - If the specified pointer is known to point into 115762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// constant global memory, return true. This allows disambiguation of store 116762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// instructions from constant pointers. 117762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// 118ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual bool pointsToConstantMemory(const Value *P); 11962b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner 120248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner //===--------------------------------------------------------------------===// 121248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// Simple mod/ref information... 122248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// 123248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner 124248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 125248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// bits which may be or'd together. 126248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// 127248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 1289769ab22265b313171d201b5928688524a01bd87Misha Brukman 1299769ab22265b313171d201b5928688524a01bd87Misha Brukman 130248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// ModRefBehavior - Summary of how a function affects memory in the program. 131248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// Loads from constant globals are not considered memory accesses for this 132248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// interface. Also, functions may freely modify stack space local to their 133248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// invocation without having to report it through these interfaces. 134248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner enum ModRefBehavior { 135248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // DoesNotAccessMemory - This function does not perform any non-local loads 136248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // or stores to memory. 137248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // 138248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // This property corresponds to the GCC 'const' attribute. 139248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner DoesNotAccessMemory, 1409769ab22265b313171d201b5928688524a01bd87Misha Brukman 141070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands // AccessesArguments - This function accesses function arguments in well 142070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands // known (possibly volatile) ways, but does not access any other memory. 143248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // 1444c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman // Clients may use the Info parameter of getModRefBehavior to get specific 1454c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman // information about how pointer arguments are used. 146248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner AccessesArguments, 1479769ab22265b313171d201b5928688524a01bd87Misha Brukman 148248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // AccessesArgumentsAndGlobals - This function has accesses function 149070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands // arguments and global variables well known (possibly volatile) ways, but 150248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // does not access any other memory. 151248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // 1524c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman // Clients may use the Info parameter of getModRefBehavior to get specific 1534c0d95178010c8788129b392ab6a1c62484f1620Dan Gohman // information about how pointer arguments are used. 154248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner AccessesArgumentsAndGlobals, 1559769ab22265b313171d201b5928688524a01bd87Misha Brukman 156248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // OnlyReadsMemory - This function does not perform any non-local stores or 157248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // volatile loads, but may read from any memory location. 158248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // 159248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // This property corresponds to the GCC 'pure' attribute. 160248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner OnlyReadsMemory, 1619769ab22265b313171d201b5928688524a01bd87Misha Brukman 162248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // UnknownModRefBehavior - This indicates that the function could not be 163248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // classified into one of the behaviors above. 164248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner UnknownModRefBehavior 165248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner }; 1669769ab22265b313171d201b5928688524a01bd87Misha Brukman 167dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// getModRefBehavior - Return the behavior when calling the given call site. 168a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 169dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 170dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// getModRefBehavior - Return the behavior when calling the given function. 171dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// For use when the call site is not known. 172a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman virtual ModRefBehavior getModRefBehavior(const Function *F); 173dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 17479fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic 17579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman /// with the given id. 17679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid); 1777c422ac216fe39fc9c402a704cf296cca9dc5b22Duncan Sands 178dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// doesNotAccessMemory - If the specified call is known to never read or 179dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// write memory, return true. If the call only reads from known-constant 180dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// memory, it is also legal to return true. Calls that unwind the stack 181dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// are legal for this predicate. 1823e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 183dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// Many optimizations (such as CSE and LICM) can be performed on such calls 184dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// without worrying about aliasing properties, and many calls have this 185dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// property (e.g. calls to 'sin' and 'cos'). 1863e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 1873e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property corresponds to the GCC 'const' attribute. 1883e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 18979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool doesNotAccessMemory(ImmutableCallSite CS) { 190652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng return getModRefBehavior(CS) == DoesNotAccessMemory; 191dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands } 192dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 193dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// doesNotAccessMemory - If the specified function is known to never read or 194dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// write memory, return true. For use when the call site is not known. 195dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// 19679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool doesNotAccessMemory(const Function *F) { 197dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands return getModRefBehavior(F) == DoesNotAccessMemory; 198248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner } 1993e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner 200dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// onlyReadsMemory - If the specified call is known to only read from 201dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// non-volatile memory (or not access memory at all), return true. Calls 202dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// that unwind the stack are legal for this predicate. 2033e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 2043e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property allows many common optimizations to be performed in the 2053e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// absence of interfering store instructions, such as CSE of strlen calls. 2063e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 2073e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property corresponds to the GCC 'pure' attribute. 2083e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 20979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool onlyReadsMemory(ImmutableCallSite CS) { 210652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng ModRefBehavior MRB = getModRefBehavior(CS); 211dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 212dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands } 213dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 214dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// onlyReadsMemory - If the specified function is known to only read from 215dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// non-volatile memory (or not access memory at all), return true. For use 216dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// when the call site is not known. 217dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// 21879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool onlyReadsMemory(const Function *F) { 219dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands ModRefBehavior MRB = getModRefBehavior(F); 2208eec644862251e14add0d2707d655fcce91e8f70Chris Lattner return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 221248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner } 2224df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 2231c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 2241c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo - Return information about whether or not an instruction may 2251c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// read or write memory specified by the pointer operand. An instruction 2261c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// that doesn't read or write memory may be trivially LICM'd for example. 2271c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 2281c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo (for call sites) - Return whether information about whether 2291c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// a particular call site modifies or reads the memory specified by the 2301c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// pointer. 2311c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 23279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 23379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value *P, unsigned Size); 2341c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 2351c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo - Return information about whether two call sites may refer 2361c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// to the same set of memory locations. This function returns NoModRef if 237414c36769aff6ec688c49f493122529394357d05Chris Lattner /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory 238414c36769aff6ec688c49f493122529394357d05Chris Lattner /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or 239414c36769aff6ec688c49f493122529394357d05Chris Lattner /// ModRef if CS1 might read or write memory accessed by CS2. 2401c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 24179fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 24279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ImmutableCallSite CS2); 2431c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 244dff6710717b159f089c76a07eda074eb6347eb92Duncan Sandspublic: 2451c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// Convenience functions... 24679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size); 24779fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size); 24879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) { 24979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman return getModRefInfo(ImmutableCallSite(C), P, Size); 2501c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 25179fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const InvokeInst *I, 25279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value *P, unsigned Size) { 25379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman return getModRefInfo(ImmutableCallSite(I), P, Size); 2541c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 25579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const VAArgInst* I, 25679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value* P, unsigned Size) { 2571ef14f6e768eac76fc272320e79bdbd90747ef47Owen Anderson return AliasAnalysis::ModRef; 25814f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth } 25979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const Instruction *I, 26079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value *P, unsigned Size) { 2611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner switch (I->getOpcode()) { 26279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, P,Size); 26379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Load: return getModRefInfo((const LoadInst*)I, P, Size); 26479fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Store: return getModRefInfo((const StoreInst*)I, P,Size); 26579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Call: return getModRefInfo((const CallInst*)I, P, Size); 26679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,P,Size); 2671c56b730a6313886076d7b293a126ae5576f5288Chris Lattner default: return NoModRef; 2681c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 2691c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 2704df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 271ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner //===--------------------------------------------------------------------===// 272ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// Higher level methods for querying mod/ref information. 273ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 274ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 275f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// canBasicBlockModify - Return true if it is possible for execution of the 276f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// specified basic block to modify the value pointed to by Ptr. 277f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 2781c56b730a6313886076d7b293a126ae5576f5288Chris Lattner bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size); 2794df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 280f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// canInstructionRangeModify - Return true if it is possible for the 281f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// execution of the specified instructions to modify the value pointed to by 282f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Ptr. The instructions to consider are all of the instructions in the 283f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 284f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 2854df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 2861c56b730a6313886076d7b293a126ae5576f5288Chris Lattner const Value *Ptr, unsigned Size); 287ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 288ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner //===--------------------------------------------------------------------===// 289ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// Methods that clients should call when they transform the program to allow 290ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// alias analyses to update their internal data structures. Note that these 291ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// methods may be called on any instruction, regardless of whether or not 292ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// they have pointer-analysis implications. 293ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 294ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 295ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// deleteValue - This method should be called whenever an LLVM Value is 296ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// deleted from the program, for example when an instruction is found to be 297ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// redundant and is eliminated. 298ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 299ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual void deleteValue(Value *V); 300ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 301ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// copyValue - This method should be used whenever a preexisting value in the 302ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// program is copied or cloned, introducing a new value. Note that analysis 303ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// implementations should tolerate clients that use this method to introduce 304ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// the same value multiple times: if the analysis already knows about a 305ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// value, it should ignore the request. 306ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 307ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual void copyValue(Value *From, Value *To); 308ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 309ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// replaceWithNewValue - This method is the obvious combination of the two 310ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// above, and it provided as a helper to simplify client code. 311ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 312ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner void replaceWithNewValue(Value *Old, Value *New) { 313ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner copyValue(Old, New); 314ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner deleteValue(Old); 315ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner } 3164df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner}; 3174df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 318a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// isNoAliasCall - Return true if this pointer is returned by a noalias 319a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// function. 320a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohmanbool isNoAliasCall(const Value *V); 321a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman 3223311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// isIdentifiedObject - Return true if this pointer refers to a distinct and 323a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// identifiable object. This returns true for: 3245753a4a0033da4add45f2e9930a4e1159d92a869Dan Gohman/// Global Variables and Functions (but not Global Aliases) 325a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// Allocas and Mallocs 3269e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman/// ByVal and NoAlias Arguments 3279e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman/// NoAlias returns 3283311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// 3299e86f4364b912ae743490ba01d6989acfd12c046Dan Gohmanbool isIdentifiedObject(const Value *V); 3303311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman 3314f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace 3324f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer 333fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// Because of the way .a files work, we must force the BasicAA implementation to 334fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// be pulled in if the AliasAnalysis header is included. Otherwise we run 335fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// the risk of AliasAnalysis being used, but the default implementation not 336fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// being linked into the tool that uses it. 3374f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis) 3384f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis) 339d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 3404df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif 341