Patches for building llvm on Solaris x86. Contributed by Nathan Keynes.
[oota-llvm.git] / include / llvm / Analysis / LibCallSemantics.h
1 //===- LibCallSemantics.h - Describe library semantics --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines interfaces that can be used to describe language specific
11 // runtime library interfaces (e.g. libc, libm, etc) to LLVM optimizers.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_LIBCALLSEMANTICS_H
16 #define LLVM_ANALYSIS_LIBCALLSEMANTICS_H
17
18 #include "llvm/Analysis/AliasAnalysis.h"
19
20 namespace llvm {
21
22   /// LibCallLocationInfo - This struct describes a set of memory locations that
23   /// are accessed by libcalls.  Identification of a location is doing with a
24   /// simple callback function.
25   ///
26   /// For example, the LibCallInfo may be set up to model the behavior of
27   /// standard libm functions.  The location that they may be interested in is
28   /// an abstract location that represents errno for the current target.  In
29   /// this case, a location for errno is anything such that the predicate
30   /// returns true.  On Mac OS/X, this predicate would return true if the
31   /// pointer is the result of a call to "__error()".
32   ///
33   /// Locations can also be defined in a constant-sensitive way.  For example,
34   /// it is possible to define a location that returns true iff it is passed
35   /// into the call as a specific argument.  This is useful for modeling things
36   /// like "printf", which can store to memory, but only through pointers passed
37   /// with a '%n' constraint.
38   ///
39   struct LibCallLocationInfo {
40     // TODO: Flags: isContextSensitive etc.
41     
42     /// isLocation - Return a LocResult if the specified pointer refers to this
43     /// location for the specified call site.  This returns "Yes" if we can tell
44     /// that the pointer *does definitely* refer to the location, "No" if we can
45     /// tell that the location *definitely does not* refer to the location, and
46     /// returns "Unknown" if we cannot tell for certain.
47     enum LocResult {
48       Yes, No, Unknown
49     };
50     LocResult (*isLocation)(CallSite cs, const Value *Ptr, unsigned Size);
51   };
52   
53   /// LibCallFunctionInfo - Each record in the array of FunctionInfo structs
54   /// records the behavior of one libcall that is known by the optimizer.  This
55   /// captures things like the side effects of the call.  Side effects are
56   /// modeled both universally (in the readnone/readonly) sense, but also
57   /// potentially against a set of abstract locations defined by the optimizer.
58   /// This allows an optimizer to define that some libcall (e.g. sqrt) is
59   /// side-effect free except that it might modify errno (thus, the call is
60   /// *not* universally readonly).  Or it might say that the side effects
61   /// are unknown other than to say that errno is not modified.
62   ///
63   struct LibCallFunctionInfo {
64     /// Name - This is the name of the libcall this describes.
65     const char *Name;
66     
67     /// TODO: Constant folding function: Constant* vector -> Constant*.
68     
69     /// UniversalBehavior - This captures the absolute mod/ref behavior without
70     /// any specific context knowledge.  For example, if the function is known
71     /// to be readonly, this would be set to 'ref'.  If known to be readnone,
72     /// this is set to NoModRef.
73     AliasAnalysis::ModRefResult UniversalBehavior;
74     
75     /// LocationMRInfo - This pair captures info about whether a specific
76     /// location is modified or referenced by a libcall.
77     struct LocationMRInfo {
78       /// LocationID - ID # of the accessed location or ~0U for array end.
79       unsigned LocationID;
80       /// MRInfo - Mod/Ref info for this location.
81       AliasAnalysis::ModRefResult MRInfo;
82     };
83     
84     /// DetailsType - Indicate the sense of the LocationDetails array.  This
85     /// controls how the LocationDetails array is interpreted.
86     enum {
87       /// DoesOnly - If DetailsType is set to DoesOnly, then we know that the
88       /// *only* mod/ref behavior of this function is captured by the
89       /// LocationDetails array.  If we are trying to say that 'sqrt' can only
90       /// modify errno, we'd have the {errnoloc,mod} in the LocationDetails
91       /// array and have DetailsType set to DoesOnly.
92       DoesOnly,
93       
94       /// DoesNot - If DetailsType is set to DoesNot, then the sense of the
95       /// LocationDetails array is completely inverted.  This means that we *do
96       /// not* know everything about the side effects of this libcall, but we do
97       /// know things that the libcall cannot do.  This is useful for complex
98       /// functions like 'ctime' which have crazy mod/ref behavior, but are
99       /// known to never read or write errno.  In this case, we'd have
100       /// {errnoloc,modref} in the LocationDetails array and DetailsType would
101       /// be set to DoesNot, indicating that ctime does not read or write the
102       /// errno location.
103       DoesNot
104     } DetailsType;
105     
106     /// LocationDetails - This is a pointer to an array of LocationMRInfo
107     /// structs which indicates the behavior of the libcall w.r.t. specific
108     /// locations.  For example, if this libcall is known to only modify
109     /// 'errno', it would have a LocationDetails array with the errno ID and
110     /// 'mod' in it.  See the DetailsType field for how this is interpreted.
111     ///
112     /// In the "DoesOnly" case, this information is 'may' information for: there
113     /// is no guarantee that the specified side effect actually does happen,
114     /// just that it could.  In the "DoesNot" case, this is 'must not' info.
115     ///
116     /// If this pointer is null, no details are known.
117     ///
118     const LocationMRInfo *LocationDetails;
119   };
120   
121   
122   /// LibCallInfo - Abstract interface to query about library call information.
123   /// Instances of this class return known information about some set of
124   /// libcalls.
125   /// 
126   class LibCallInfo {
127     // Implementation details of this object, private.
128     mutable void *Impl;
129     mutable const LibCallLocationInfo *Locations;
130     mutable unsigned NumLocations;
131   public:
132     LibCallInfo() : Impl(0), Locations(0), NumLocations(0) {}
133     virtual ~LibCallInfo();
134     
135     //===------------------------------------------------------------------===//
136     //  Accessor Methods: Efficient access to contained data.
137     //===------------------------------------------------------------------===//
138     
139     /// getLocationInfo - Return information about the specified LocationID.
140     const LibCallLocationInfo &getLocationInfo(unsigned LocID) const;
141     
142     
143     /// getFunctionInfo - Return the LibCallFunctionInfo object corresponding to
144     /// the specified function if we have it.  If not, return null.
145     const LibCallFunctionInfo *getFunctionInfo(Function *F) const;
146     
147     
148     //===------------------------------------------------------------------===//
149     //  Implementation Methods: Subclasses should implement these.
150     //===------------------------------------------------------------------===//
151     
152     /// getLocationInfo - Return descriptors for the locations referenced by
153     /// this set of libcalls.
154     virtual unsigned getLocationInfo(const LibCallLocationInfo *&Array) const {
155       return 0;
156     }
157     
158     /// getFunctionInfoArray - Return an array of descriptors that describe the
159     /// set of libcalls represented by this LibCallInfo object.  This array is
160     /// terminated by an entry with a NULL name.
161     virtual const LibCallFunctionInfo *getFunctionInfoArray() const = 0;
162   };
163
164 } // end namespace llvm
165
166 #endif