AliasAnalysis.h revision cb74993bdc37681ddbb80fa361575107afae1350
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; 46cb74993bdc37681ddbb80fa361575107afae1350Dan Gohman 47cb74993bdc37681ddbb80fa361575107afae1350Dan Gohmanprivate: 48ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner AliasAnalysis *AA; // Previous Alias Analysis to chain to. 49ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 50cb74993bdc37681ddbb80fa361575107afae1350Dan Gohmanprotected: 511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// InitializeAliasAnalysis - Subclasses must call this method to initialize 521c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// the AliasAnalysis interface before any other methods are called. This is 531c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// typically called by the run* methods of these subclasses. This may be 541c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// called multiple times. 551c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 561c56b730a6313886076d7b293a126ae5576f5288Chris Lattner void InitializeAliasAnalysis(Pass *P); 579769ab22265b313171d201b5928688524a01bd87Misha Brukman 5864d237cfae6bcb11157d525c79b5a5335e30370bDan Gohman /// getAnalysisUsage - All alias analysis implementations should invoke this 59fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// directly (using AliasAnalysis::getAnalysisUsage(AU)). 601c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const; 611c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 621c56b730a6313886076d7b293a126ae5576f5288Chris Lattnerpublic: 631997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel static char ID; // Class identification, replacement for typeinfo 64ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner AliasAnalysis() : TD(0), AA(0) {} 651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual ~AliasAnalysis(); // We want to be subclassed 661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 67ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// UnknownSize - This is a special value which can be used with the 68ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// size arguments in alias queries to indicate that the caller does not 69ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman /// know the sizes of the potential memory references. 70ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman static unsigned const UnknownSize = ~0u; 71ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman 72fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// getTargetData - Return a pointer to the current TargetData object, or 73fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// null if no TargetData object is available. 74dd298c8c6eb036baf35bf5a559c59d2afd2c7944Misha Brukman /// 75fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman const TargetData *getTargetData() const { return TD; } 76fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman 77fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// getTypeStoreSize - Return the TargetData store size for the given type, 78fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// if known, or a conservative value otherwise. 79fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman /// 80fc2a3ed0c9e32cf7edaf5030fa0972b916cc5f0bDan Gohman unsigned getTypeStoreSize(const Type *Ty); 814df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 821c56b730a6313886076d7b293a126ae5576f5288Chris Lattner //===--------------------------------------------------------------------===// 831c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// Alias Queries... 841c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 854df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 86f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Alias analysis result - Either we know for sure that it does not alias, we 87f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// know for sure it must alias, or we don't know anything: The two pointers 88f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// _might_ alias. This enum is designed so you can do things like: 89f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// if (AA.alias(P1, P2)) { ... } 90f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// to check to see if two pointers might alias. 91f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 92e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// See docs/AliasAnalysis.html for more information on the specific meanings 93e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// of these values. 94e53e3772f3c8b4cba69b7d77c8b2148a080fe9aeDan Gohman /// 951c56b730a6313886076d7b293a126ae5576f5288Chris Lattner enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 }; 964df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 97f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// alias - The main low level interface to the alias analysis implementation. 98f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Returns a Result indicating whether the two pointers are aliased to each 99f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// other. This is the interface that must be implemented by specific alias 100f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// analysis implementations. 101f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 1021c56b730a6313886076d7b293a126ae5576f5288Chris Lattner virtual AliasResult alias(const Value *V1, unsigned V1Size, 103ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner const Value *V2, unsigned V2Size); 1044df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 105847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman /// alias - A convenience wrapper for the case where the sizes are unknown. 106847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman AliasResult alias(const Value *V1, const Value *V2) { 107ef1cfac9e50def9097cd3e3ab3c5cad7f4c758ccDan Gohman return alias(V1, UnknownSize, V2, UnknownSize); 108847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman } 109847a84efd23a2c7d90429b82f6e0f19d1f913d9aDan Gohman 110c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner /// isNoAlias - A trivial helper function to check to see if the specified 111c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner /// pointers are no-alias. 112c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner bool isNoAlias(const Value *V1, unsigned V1Size, 113c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner const Value *V2, unsigned V2Size) { 114c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner return alias(V1, V1Size, V2, V2Size) == NoAlias; 115c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner } 116c46530b6a32a53edf0d6d32bcd09ea5d76940472Chris Lattner 117762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// pointsToConstantMemory - If the specified pointer is known to point into 118762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// constant global memory, return true. This allows disambiguation of store 119762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// instructions from constant pointers. 120762e8e846f0ad50ffea56216c5ea20db1c95b756Chris Lattner /// 121ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual bool pointsToConstantMemory(const Value *P); 12262b5c167de0ca5883a7ad5eb0900eb0c15ad3707Chris Lattner 123248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner //===--------------------------------------------------------------------===// 124248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// Simple mod/ref information... 125248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// 126248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner 127248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 128248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// bits which may be or'd together. 129248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// 130248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 1319769ab22265b313171d201b5928688524a01bd87Misha Brukman 1329769ab22265b313171d201b5928688524a01bd87Misha Brukman 133248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// ModRefBehavior - Summary of how a function affects memory in the program. 134248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// Loads from constant globals are not considered memory accesses for this 135248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// interface. Also, functions may freely modify stack space local to their 136248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner /// invocation without having to report it through these interfaces. 137248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner enum ModRefBehavior { 138248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // DoesNotAccessMemory - This function does not perform any non-local loads 139248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // or stores to memory. 140248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // 141248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // This property corresponds to the GCC 'const' attribute. 142248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner DoesNotAccessMemory, 1439769ab22265b313171d201b5928688524a01bd87Misha Brukman 144070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands // AccessesArguments - This function accesses function arguments in well 145070fbe69d36b24616de4e484ecbea221a2c7e8c2Duncan Sands // known (possibly volatile) ways, but does not access any other memory. 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 AccessesArgumentsAndGlobals, 1529769ab22265b313171d201b5928688524a01bd87Misha Brukman 153248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // OnlyReadsMemory - This function does not perform any non-local stores or 154248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // volatile loads, but may read from any memory location. 155248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // 156248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // This property corresponds to the GCC 'pure' attribute. 157248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner OnlyReadsMemory, 1589769ab22265b313171d201b5928688524a01bd87Misha Brukman 159248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // UnknownModRefBehavior - This indicates that the function could not be 160248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner // classified into one of the behaviors above. 161248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner UnknownModRefBehavior 162248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner }; 1639769ab22265b313171d201b5928688524a01bd87Misha Brukman 164dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// getModRefBehavior - Return the behavior when calling the given call site. 165a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 166dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 167dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// getModRefBehavior - Return the behavior when calling the given function. 168dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// For use when the call site is not known. 169a41af344c2964573318a77e2b43eafd4d3804003Dan Gohman virtual ModRefBehavior getModRefBehavior(const Function *F); 170dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 17179fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic 17279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman /// with the given id. 17379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid); 1747c422ac216fe39fc9c402a704cf296cca9dc5b22Duncan Sands 175dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// doesNotAccessMemory - If the specified call is known to never read or 176dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// write memory, return true. If the call only reads from known-constant 177dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// memory, it is also legal to return true. Calls that unwind the stack 178dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// are legal for this predicate. 1793e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 180dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// Many optimizations (such as CSE and LICM) can be performed on such calls 181dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// without worrying about aliasing properties, and many calls have this 182dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// property (e.g. calls to 'sin' and 'cos'). 1833e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 1843e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property corresponds to the GCC 'const' attribute. 1853e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 18679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool doesNotAccessMemory(ImmutableCallSite CS) { 187652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng return getModRefBehavior(CS) == DoesNotAccessMemory; 188dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands } 189dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 190dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// doesNotAccessMemory - If the specified function is known to never read or 191dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// write memory, return true. For use when the call site is not known. 192dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// 19379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool doesNotAccessMemory(const Function *F) { 194dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands return getModRefBehavior(F) == DoesNotAccessMemory; 195248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner } 1963e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner 197dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// onlyReadsMemory - If the specified call is known to only read from 198dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// non-volatile memory (or not access memory at all), return true. Calls 199dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// that unwind the stack are legal for this predicate. 2003e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 2013e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property allows many common optimizations to be performed in the 2023e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// absence of interfering store instructions, such as CSE of strlen calls. 2033e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 2043e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// This property corresponds to the GCC 'pure' attribute. 2053e295b1b59e47916c8ae5d42eb27a23bd580cabeChris Lattner /// 20679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool onlyReadsMemory(ImmutableCallSite CS) { 207652f7ea955bb433d6b7a4d33685dca9485fd7b8bEvan Cheng ModRefBehavior MRB = getModRefBehavior(CS); 208dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 209dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands } 210dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands 211dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// onlyReadsMemory - If the specified function is known to only read from 212dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// non-volatile memory (or not access memory at all), return true. For use 213dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// when the call site is not known. 214dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands /// 21579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman bool onlyReadsMemory(const Function *F) { 216dff6710717b159f089c76a07eda074eb6347eb92Duncan Sands ModRefBehavior MRB = getModRefBehavior(F); 2178eec644862251e14add0d2707d655fcce91e8f70Chris Lattner return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 218248e8ebeff834db9b78917b1531eeee7035eb113Chris Lattner } 2194df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 2201c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 2211c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo - Return information about whether or not an instruction may 2221c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// read or write memory specified by the pointer operand. An instruction 2231c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// that doesn't read or write memory may be trivially LICM'd for example. 2241c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 2251c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo (for call sites) - Return whether information about whether 2261c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// a particular call site modifies or reads the memory specified by the 2271c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// pointer. 2281c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 22979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 23079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value *P, unsigned Size); 2311c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 2321c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// getModRefInfo - Return information about whether two call sites may refer 2331c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// to the same set of memory locations. This function returns NoModRef if 234414c36769aff6ec688c49f493122529394357d05Chris Lattner /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory 235414c36769aff6ec688c49f493122529394357d05Chris Lattner /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or 236414c36769aff6ec688c49f493122529394357d05Chris Lattner /// ModRef if CS1 might read or write memory accessed by CS2. 2371c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// 23879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 23979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ImmutableCallSite CS2); 2401c56b730a6313886076d7b293a126ae5576f5288Chris Lattner 241dff6710717b159f089c76a07eda074eb6347eb92Duncan Sandspublic: 2421c56b730a6313886076d7b293a126ae5576f5288Chris Lattner /// Convenience functions... 24379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size); 24479fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size); 24579fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) { 24679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman return getModRefInfo(ImmutableCallSite(C), P, Size); 2471c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 24879fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const InvokeInst *I, 24979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value *P, unsigned Size) { 25079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman return getModRefInfo(ImmutableCallSite(I), P, Size); 2511c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 25279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const VAArgInst* I, 25379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value* P, unsigned Size) { 2541ef14f6e768eac76fc272320e79bdbd90747ef47Owen Anderson return AliasAnalysis::ModRef; 25514f1703ae33e13dbcadd701603fd4d7a6f7010b9Andrew Lenharth } 25679fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman ModRefResult getModRefInfo(const Instruction *I, 25779fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman const Value *P, unsigned Size) { 2581c56b730a6313886076d7b293a126ae5576f5288Chris Lattner switch (I->getOpcode()) { 25979fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, P,Size); 26079fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Load: return getModRefInfo((const LoadInst*)I, P, Size); 26179fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Store: return getModRefInfo((const StoreInst*)I, P,Size); 26279fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Call: return getModRefInfo((const CallInst*)I, P, Size); 26379fca6fea87be7221843031870bbf2c9ae1fc555Dan Gohman case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,P,Size); 2641c56b730a6313886076d7b293a126ae5576f5288Chris Lattner default: return NoModRef; 2651c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 2661c56b730a6313886076d7b293a126ae5576f5288Chris Lattner } 2674df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 268ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner //===--------------------------------------------------------------------===// 269ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// Higher level methods for querying mod/ref information. 270ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 271ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 272f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// canBasicBlockModify - Return true if it is possible for execution of the 273f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// specified basic block to modify the value pointed to by Ptr. 274f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 2751c56b730a6313886076d7b293a126ae5576f5288Chris Lattner bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size); 2764df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 277f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// canInstructionRangeModify - Return true if it is possible for the 278f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// execution of the specified instructions to modify the value pointed to by 279f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// Ptr. The instructions to consider are all of the instructions in the 280f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 281f12c2c28bd72091f2d7fff5718265c5ad52e7af8Chris Lattner /// 2824df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 2831c56b730a6313886076d7b293a126ae5576f5288Chris Lattner const Value *Ptr, unsigned Size); 284ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 285ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner //===--------------------------------------------------------------------===// 286ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// Methods that clients should call when they transform the program to allow 287ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// alias analyses to update their internal data structures. Note that these 288ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// methods may be called on any instruction, regardless of whether or not 289ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// they have pointer-analysis implications. 290ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 291ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 292ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// deleteValue - This method should be called whenever an LLVM Value is 293ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// deleted from the program, for example when an instruction is found to be 294ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// redundant and is eliminated. 295ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 296ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual void deleteValue(Value *V); 297ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 298ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// copyValue - This method should be used whenever a preexisting value in the 299ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// program is copied or cloned, introducing a new value. Note that analysis 300ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// implementations should tolerate clients that use this method to introduce 301ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// the same value multiple times: if the analysis already knows about a 302ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// value, it should ignore the request. 303ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 304ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner virtual void copyValue(Value *From, Value *To); 305ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner 306ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// replaceWithNewValue - This method is the obvious combination of the two 307ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// above, and it provided as a helper to simplify client code. 308ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner /// 309ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner void replaceWithNewValue(Value *Old, Value *New) { 310ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner copyValue(Old, New); 311ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner deleteValue(Old); 312ab8c565768ff7485f40cbb65e2914f9046e743d4Chris Lattner } 3134df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner}; 3144df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner 315a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// isNoAliasCall - Return true if this pointer is returned by a noalias 316a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// function. 317a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohmanbool isNoAliasCall(const Value *V); 318a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman 3193311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// isIdentifiedObject - Return true if this pointer refers to a distinct and 320a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// identifiable object. This returns true for: 3215753a4a0033da4add45f2e9930a4e1159d92a869Dan Gohman/// Global Variables and Functions (but not Global Aliases) 322a5f81bba4ab18d6129774d4d67495f14b6f64375Dan Gohman/// Allocas and Mallocs 3239e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman/// ByVal and NoAlias Arguments 3249e86f4364b912ae743490ba01d6989acfd12c046Dan Gohman/// NoAlias returns 3253311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman/// 3269e86f4364b912ae743490ba01d6989acfd12c046Dan Gohmanbool isIdentifiedObject(const Value *V); 3273311a1f8f0d8a2c6d940802bbb95eba0b801a615Dan Gohman 3284f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer} // End llvm namespace 3294f1bd9e9963239c119db70070db1d68286b3de7eReid Spencer 330fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// Because of the way .a files work, we must force the BasicAA implementation to 331fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// be pulled in if the AliasAnalysis header is included. Otherwise we run 332fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// the risk of AliasAnalysis being used, but the default implementation not 333fc188b95b81332c12043173c7f517341c6ad27a9Brian Gaeke// being linked into the tool that uses it. 3344f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis) 3354f1bd9e9963239c119db70070db1d68286b3de7eReid SpencerFORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis) 336d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 3374df22c0100fe27f19e6f4874f24eedd0742b9cf4Chris Lattner#endif 338