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)(ImmutableCallSite CS, 00051 const AliasAnalysis::Location &Loc); 00052 }; 00053 00054 /// LibCallFunctionInfo - Each record in the array of FunctionInfo structs 00055 /// records the behavior of one libcall that is known by the optimizer. This 00056 /// captures things like the side effects of the call. Side effects are 00057 /// modeled both universally (in the readnone/readonly) sense, but also 00058 /// potentially against a set of abstract locations defined by the optimizer. 00059 /// This allows an optimizer to define that some libcall (e.g. sqrt) is 00060 /// side-effect free except that it might modify errno (thus, the call is 00061 /// *not* universally readonly). Or it might say that the side effects 00062 /// are unknown other than to say that errno is not modified. 00063 /// 00064 struct LibCallFunctionInfo { 00065 /// Name - This is the name of the libcall this describes. 00066 const char *Name; 00067 00068 /// TODO: Constant folding function: Constant* vector -> Constant*. 00069 00070 /// UniversalBehavior - This captures the absolute mod/ref behavior without 00071 /// any specific context knowledge. For example, if the function is known 00072 /// to be readonly, this would be set to 'ref'. If known to be readnone, 00073 /// this is set to NoModRef. 00074 AliasAnalysis::ModRefResult UniversalBehavior; 00075 00076 /// LocationMRInfo - This pair captures info about whether a specific 00077 /// location is modified or referenced by a libcall. 00078 struct LocationMRInfo { 00079 /// LocationID - ID # of the accessed location or ~0U for array end. 00080 unsigned LocationID; 00081 /// MRInfo - Mod/Ref info for this location. 00082 AliasAnalysis::ModRefResult MRInfo; 00083 }; 00084 00085 /// DetailsType - Indicate the sense of the LocationDetails array. This 00086 /// controls how the LocationDetails array is interpreted. 00087 enum { 00088 /// DoesOnly - If DetailsType is set to DoesOnly, then we know that the 00089 /// *only* mod/ref behavior of this function is captured by the 00090 /// LocationDetails array. If we are trying to say that 'sqrt' can only 00091 /// modify errno, we'd have the {errnoloc,mod} in the LocationDetails 00092 /// array and have DetailsType set to DoesOnly. 00093 DoesOnly, 00094 00095 /// DoesNot - If DetailsType is set to DoesNot, then the sense of the 00096 /// LocationDetails array is completely inverted. This means that we *do 00097 /// not* know everything about the side effects of this libcall, but we do 00098 /// know things that the libcall cannot do. This is useful for complex 00099 /// functions like 'ctime' which have crazy mod/ref behavior, but are 00100 /// known to never read or write errno. In this case, we'd have 00101 /// {errnoloc,modref} in the LocationDetails array and DetailsType would 00102 /// be set to DoesNot, indicating that ctime does not read or write the 00103 /// errno location. 00104 DoesNot 00105 } DetailsType; 00106 00107 /// LocationDetails - This is a pointer to an array of LocationMRInfo 00108 /// structs which indicates the behavior of the libcall w.r.t. specific 00109 /// locations. For example, if this libcall is known to only modify 00110 /// 'errno', it would have a LocationDetails array with the errno ID and 00111 /// 'mod' in it. See the DetailsType field for how this is interpreted. 00112 /// 00113 /// In the "DoesOnly" case, this information is 'may' information for: there 00114 /// is no guarantee that the specified side effect actually does happen, 00115 /// just that it could. In the "DoesNot" case, this is 'must not' info. 00116 /// 00117 /// If this pointer is null, no details are known. 00118 /// 00119 const LocationMRInfo *LocationDetails; 00120 }; 00121 00122 00123 /// LibCallInfo - Abstract interface to query about library call information. 00124 /// Instances of this class return known information about some set of 00125 /// libcalls. 00126 /// 00127 class LibCallInfo { 00128 // Implementation details of this object, private. 00129 mutable void *Impl; 00130 mutable const LibCallLocationInfo *Locations; 00131 mutable unsigned NumLocations; 00132 public: 00133 LibCallInfo() : Impl(nullptr), Locations(nullptr), NumLocations(0) {} 00134 virtual ~LibCallInfo(); 00135 00136 //===------------------------------------------------------------------===// 00137 // Accessor Methods: Efficient access to contained data. 00138 //===------------------------------------------------------------------===// 00139 00140 /// getLocationInfo - Return information about the specified LocationID. 00141 const LibCallLocationInfo &getLocationInfo(unsigned LocID) const; 00142 00143 00144 /// getFunctionInfo - Return the LibCallFunctionInfo object corresponding to 00145 /// the specified function if we have it. If not, return null. 00146 const LibCallFunctionInfo *getFunctionInfo(const Function *F) const; 00147 00148 00149 //===------------------------------------------------------------------===// 00150 // Implementation Methods: Subclasses should implement these. 00151 //===------------------------------------------------------------------===// 00152 00153 /// getLocationInfo - Return descriptors for the locations referenced by 00154 /// this set of libcalls. 00155 virtual unsigned getLocationInfo(const LibCallLocationInfo *&Array) const { 00156 return 0; 00157 } 00158 00159 /// getFunctionInfoArray - Return an array of descriptors that describe the 00160 /// set of libcalls represented by this LibCallInfo object. This array is 00161 /// terminated by an entry with a NULL name. 00162 virtual const LibCallFunctionInfo *getFunctionInfoArray() const = 0; 00163 }; 00164 00165 } // end namespace llvm 00166 00167 #endif