1218887Sdim//===- LibCallSemantics.h - Describe library semantics --------------------===// 2218887Sdim// 3218887Sdim// The LLVM Compiler Infrastructure 4218887Sdim// 5218887Sdim// This file is distributed under the University of Illinois Open Source 6218887Sdim// License. See LICENSE.TXT for details. 7218887Sdim// 8218887Sdim//===----------------------------------------------------------------------===// 9218887Sdim// 10218887Sdim// This file defines interfaces that can be used to describe language specific 11218887Sdim// runtime library interfaces (e.g. libc, libm, etc) to LLVM optimizers. 12218887Sdim// 13218887Sdim//===----------------------------------------------------------------------===// 14218887Sdim 15234353Sdim#ifndef LLVM_ANALYSIS_LIBCALLSEMANTICS_H 16234353Sdim#define LLVM_ANALYSIS_LIBCALLSEMANTICS_H 17226633Sdim 18226633Sdim#include "llvm/Analysis/AliasAnalysis.h" 19239462Sdim 20226633Sdimnamespace llvm { 21296417Sdim 22226633Sdim /// LibCallLocationInfo - This struct describes a set of memory locations that 23280031Sdim /// are accessed by libcalls. Identification of a location is doing with a 24280031Sdim /// simple callback function. 25243830Sdim /// 26243830Sdim /// For example, the LibCallInfo may be set up to model the behavior of 27243830Sdim /// standard libm functions. The location that they may be interested in is 28243830Sdim /// an abstract location that represents errno for the current target. In 29249423Sdim /// this case, a location for errno is anything such that the predicate 30280031Sdim /// returns true. On Mac OS/X, this predicate would return true if the 31280031Sdim /// pointer is the result of a call to "__error()". 32280031Sdim /// 33243830Sdim /// Locations can also be defined in a constant-sensitive way. For example, 34243830Sdim /// it is possible to define a location that returns true iff it is passed 35243830Sdim /// into the call as a specific argument. This is useful for modeling things 36239462Sdim /// like "printf", which can store to memory, but only through pointers passed 37226633Sdim /// with a '%n' constraint. 38243830Sdim /// 39243830Sdim struct LibCallLocationInfo { 40226633Sdim // TODO: Flags: isContextSensitive etc. 41226633Sdim 42226633Sdim /// isLocation - Return a LocResult if the specified pointer refers to this 43239462Sdim /// location for the specified call site. This returns "Yes" if we can tell 44239462Sdim /// that the pointer *does definitely* refer to the location, "No" if we can 45239462Sdim /// tell that the location *definitely does not* refer to the location, and 46239462Sdim /// returns "Unknown" if we cannot tell for certain. 47239462Sdim enum LocResult { 48239462Sdim Yes, No, Unknown 49226633Sdim }; 50239462Sdim LocResult (*isLocation)(ImmutableCallSite CS, 51239462Sdim const AliasAnalysis::Location &Loc); 52239462Sdim }; 53239462Sdim 54239462Sdim /// LibCallFunctionInfo - Each record in the array of FunctionInfo structs 55239462Sdim /// records the behavior of one libcall that is known by the optimizer. This 56239462Sdim /// captures things like the side effects of the call. Side effects are 57239462Sdim /// modeled both universally (in the readnone/readonly) sense, but also 58239462Sdim /// potentially against a set of abstract locations defined by the optimizer. 59 /// This allows an optimizer to define that some libcall (e.g. sqrt) is 60 /// side-effect free except that it might modify errno (thus, the call is 61 /// *not* universally readonly). Or it might say that the side effects 62 /// are unknown other than to say that errno is not modified. 63 /// 64 struct LibCallFunctionInfo { 65 /// Name - This is the name of the libcall this describes. 66 const char *Name; 67 68 /// TODO: Constant folding function: Constant* vector -> Constant*. 69 70 /// UniversalBehavior - This captures the absolute mod/ref behavior without 71 /// any specific context knowledge. For example, if the function is known 72 /// to be readonly, this would be set to 'ref'. If known to be readnone, 73 /// this is set to NoModRef. 74 AliasAnalysis::ModRefResult UniversalBehavior; 75 76 /// LocationMRInfo - This pair captures info about whether a specific 77 /// location is modified or referenced by a libcall. 78 struct LocationMRInfo { 79 /// LocationID - ID # of the accessed location or ~0U for array end. 80 unsigned LocationID; 81 /// MRInfo - Mod/Ref info for this location. 82 AliasAnalysis::ModRefResult MRInfo; 83 }; 84 85 /// DetailsType - Indicate the sense of the LocationDetails array. This 86 /// controls how the LocationDetails array is interpreted. 87 enum { 88 /// DoesOnly - If DetailsType is set to DoesOnly, then we know that the 89 /// *only* mod/ref behavior of this function is captured by the 90 /// LocationDetails array. If we are trying to say that 'sqrt' can only 91 /// modify errno, we'd have the {errnoloc,mod} in the LocationDetails 92 /// array and have DetailsType set to DoesOnly. 93 DoesOnly, 94 95 /// DoesNot - If DetailsType is set to DoesNot, then the sense of the 96 /// LocationDetails array is completely inverted. This means that we *do 97 /// not* know everything about the side effects of this libcall, but we do 98 /// know things that the libcall cannot do. This is useful for complex 99 /// functions like 'ctime' which have crazy mod/ref behavior, but are 100 /// known to never read or write errno. In this case, we'd have 101 /// {errnoloc,modref} in the LocationDetails array and DetailsType would 102 /// be set to DoesNot, indicating that ctime does not read or write the 103 /// errno location. 104 DoesNot 105 } DetailsType; 106 107 /// LocationDetails - This is a pointer to an array of LocationMRInfo 108 /// structs which indicates the behavior of the libcall w.r.t. specific 109 /// locations. For example, if this libcall is known to only modify 110 /// 'errno', it would have a LocationDetails array with the errno ID and 111 /// 'mod' in it. See the DetailsType field for how this is interpreted. 112 /// 113 /// In the "DoesOnly" case, this information is 'may' information for: there 114 /// is no guarantee that the specified side effect actually does happen, 115 /// just that it could. In the "DoesNot" case, this is 'must not' info. 116 /// 117 /// If this pointer is null, no details are known. 118 /// 119 const LocationMRInfo *LocationDetails; 120 }; 121 122 123 /// LibCallInfo - Abstract interface to query about library call information. 124 /// Instances of this class return known information about some set of 125 /// libcalls. 126 /// 127 class LibCallInfo { 128 // Implementation details of this object, private. 129 mutable void *Impl; 130 mutable const LibCallLocationInfo *Locations; 131 mutable unsigned NumLocations; 132 public: 133 LibCallInfo() : Impl(0), Locations(0), NumLocations(0) {} 134 virtual ~LibCallInfo(); 135 136 //===------------------------------------------------------------------===// 137 // Accessor Methods: Efficient access to contained data. 138 //===------------------------------------------------------------------===// 139 140 /// getLocationInfo - Return information about the specified LocationID. 141 const LibCallLocationInfo &getLocationInfo(unsigned LocID) const; 142 143 144 /// getFunctionInfo - Return the LibCallFunctionInfo object corresponding to 145 /// the specified function if we have it. If not, return null. 146 const LibCallFunctionInfo *getFunctionInfo(const Function *F) const; 147 148 149 //===------------------------------------------------------------------===// 150 // Implementation Methods: Subclasses should implement these. 151 //===------------------------------------------------------------------===// 152 153 /// getLocationInfo - Return descriptors for the locations referenced by 154 /// this set of libcalls. 155 virtual unsigned getLocationInfo(const LibCallLocationInfo *&Array) const { 156 return 0; 157 } 158 159 /// getFunctionInfoArray - Return an array of descriptors that describe the 160 /// set of libcalls represented by this LibCallInfo object. This array is 161 /// terminated by an entry with a NULL name. 162 virtual const LibCallFunctionInfo *getFunctionInfoArray() const = 0; 163 }; 164 165} // end namespace llvm 166 167#endif 168