LLVM API Documentation
00001 //===- LibCallSemantics.h - Describe library semantics --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines interfaces that can be used to describe language specific 00011 // runtime library interfaces (e.g. libc, libm, etc) to LLVM optimizers. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_ANALYSIS_LIBCALLSEMANTICS_H 00016 #define LLVM_ANALYSIS_LIBCALLSEMANTICS_H 00017 00018 #include "llvm/Analysis/AliasAnalysis.h" 00019 00020 namespace llvm { 00021 00022 /// LibCallLocationInfo - This struct describes a set of memory locations that 00023 /// are accessed by libcalls. Identification of a location is doing with a 00024 /// simple callback function. 00025 /// 00026 /// For example, the LibCallInfo may be set up to model the behavior of 00027 /// standard libm functions. The location that they may be interested in is 00028 /// an abstract location that represents errno for the current target. In 00029 /// this case, a location for errno is anything such that the predicate 00030 /// returns true. On Mac OS/X, this predicate would return true if the 00031 /// pointer is the result of a call to "__error()". 00032 /// 00033 /// Locations can also be defined in a constant-sensitive way. For example, 00034 /// it is possible to define a location that returns true iff it is passed 00035 /// into the call as a specific argument. This is useful for modeling things 00036 /// like "printf", which can store to memory, but only through pointers passed 00037 /// with a '%n' constraint. 00038 /// 00039 struct LibCallLocationInfo { 00040 // TODO: Flags: isContextSensitive etc. 00041 00042 /// isLocation - Return a LocResult if the specified pointer refers to this 00043 /// location for the specified call site. This returns "Yes" if we can tell 00044 /// that the pointer *does definitely* refer to the location, "No" if we can 00045 /// tell that the location *definitely does not* refer to the location, and 00046 /// returns "Unknown" if we cannot tell for certain. 00047 enum LocResult { 00048 Yes, No, Unknown 00049 }; 00050 LocResult (*isLocation)(CallSite CS, const Value *Ptr, unsigned Size); 00051 }; 00052 00053 /// LibCallFunctionInfo - Each record in the array of FunctionInfo structs 00054 /// records the behavior of one libcall that is known by the optimizer. This 00055 /// captures things like the side effects of the call. Side effects are 00056 /// modeled both universally (in the readnone/readonly) sense, but also 00057 /// potentially against a set of abstract locations defined by the optimizer. 00058 /// This allows an optimizer to define that some libcall (e.g. sqrt) is 00059 /// side-effect free except that it might modify errno (thus, the call is 00060 /// *not* universally readonly). Or it might say that the side effects 00061 /// are unknown other than to say that errno is not modified. 00062 /// 00063 struct LibCallFunctionInfo { 00064 /// Name - This is the name of the libcall this describes. 00065 const char *Name; 00066 00067 /// TODO: Constant folding function: Constant* vector -> Constant*. 00068 00069 /// UniversalBehavior - This captures the absolute mod/ref behavior without 00070 /// any specific context knowledge. For example, if the function is known 00071 /// to be readonly, this would be set to 'ref'. If known to be readnone, 00072 /// this is set to NoModRef. 00073 AliasAnalysis::ModRefResult UniversalBehavior; 00074 00075 /// LocationMRInfo - This pair captures info about whether a specific 00076 /// location is modified or referenced by a libcall. 00077 struct LocationMRInfo { 00078 /// LocationID - ID # of the accessed location or ~0U for array end. 00079 unsigned LocationID; 00080 /// MRInfo - Mod/Ref info for this location. 00081 AliasAnalysis::ModRefResult MRInfo; 00082 }; 00083 00084 /// DetailsType - Indicate the sense of the LocationDetails array. This 00085 /// controls how the LocationDetails array is interpreted. 00086 enum { 00087 /// DoesOnly - If DetailsType is set to DoesOnly, then we know that the 00088 /// *only* mod/ref behavior of this function is captured by the 00089 /// LocationDetails array. If we are trying to say that 'sqrt' can only 00090 /// modify errno, we'd have the {errnoloc,mod} in the LocationDetails 00091 /// array and have DetailsType set to DoesOnly. 00092 DoesOnly, 00093 00094 /// DoesNot - If DetailsType is set to DoesNot, then the sense of the 00095 /// LocationDetails array is completely inverted. This means that we *do 00096 /// not* know everything about the side effects of this libcall, but we do 00097 /// know things that the libcall cannot do. This is useful for complex 00098 /// functions like 'ctime' which have crazy mod/ref behavior, but are 00099 /// known to never read or write errno. In this case, we'd have 00100 /// {errnoloc,modref} in the LocationDetails array and DetailsType would 00101 /// be set to DoesNot, indicating that ctime does not read or write the 00102 /// errno location. 00103 DoesNot 00104 } DetailsType; 00105 00106 /// LocationDetails - This is a pointer to an array of LocationMRInfo 00107 /// structs which indicates the behavior of the libcall w.r.t. specific 00108 /// locations. For example, if this libcall is known to only modify 00109 /// 'errno', it would have a LocationDetails array with the errno ID and 00110 /// 'mod' in it. See the DetailsType field for how this is interpreted. 00111 /// 00112 /// In the "DoesOnly" case, this information is 'may' information for: there 00113 /// is no guarantee that the specified side effect actually does happen, 00114 /// just that it could. In the "DoesNot" case, this is 'must not' info. 00115 /// 00116 /// If this pointer is null, no details are known. 00117 /// 00118 const LocationMRInfo *LocationDetails; 00119 }; 00120 00121 00122 /// LibCallInfo - Abstract interface to query about library call information. 00123 /// Instances of this class return known information about some set of 00124 /// libcalls. 00125 /// 00126 class LibCallInfo { 00127 // Implementation details of this object, private. 00128 mutable void *Impl; 00129 mutable const LibCallLocationInfo *Locations; 00130 mutable unsigned NumLocations; 00131 public: 00132 LibCallInfo() : Impl(0), Locations(0), NumLocations(0) {} 00133 virtual ~LibCallInfo(); 00134 00135 //===------------------------------------------------------------------===// 00136 // Accessor Methods: Efficient access to contained data. 00137 //===------------------------------------------------------------------===// 00138 00139 /// getLocationInfo - Return information about the specified LocationID. 00140 const LibCallLocationInfo &getLocationInfo(unsigned LocID) const; 00141 00142 00143 /// getFunctionInfo - Return the LibCallFunctionInfo object corresponding to 00144 /// the specified function if we have it. If not, return null. 00145 const LibCallFunctionInfo *getFunctionInfo(Function *F) const; 00146 00147 00148 //===------------------------------------------------------------------===// 00149 // Implementation Methods: Subclasses should implement these. 00150 //===------------------------------------------------------------------===// 00151 00152 /// getLocationInfo - Return descriptors for the locations referenced by 00153 /// this set of libcalls. 00154 virtual unsigned getLocationInfo(const LibCallLocationInfo *&Array) const { 00155 return 0; 00156 } 00157 00158 /// getFunctionInfoArray - Return an array of descriptors that describe the 00159 /// set of libcalls represented by this LibCallInfo object. This array is 00160 /// terminated by an entry with a NULL name. 00161 virtual const LibCallFunctionInfo *getFunctionInfoArray() const = 0; 00162 }; 00163 00164 } // end namespace llvm 00165 00166 #endif