156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//===- LibCallSemantics.h - Describe library semantics --------------------===//
256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//
356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//                     The LLVM Compiler Infrastructure
456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//
556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner// This file is distributed under the University of Illinois Open Source
656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner// License. See LICENSE.TXT for details.
756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//
856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//===----------------------------------------------------------------------===//
956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//
1056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner// This file defines interfaces that can be used to describe language specific
1156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner// runtime library interfaces (e.g. libc, libm, etc) to LLVM optimizers.
1256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//
1356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner//===----------------------------------------------------------------------===//
1456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
1556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner#ifndef LLVM_ANALYSIS_LIBCALLSEMANTICS_H
1656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner#define LLVM_ANALYSIS_LIBCALLSEMANTICS_H
1756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
1856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner#include "llvm/Analysis/AliasAnalysis.h"
1956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
2056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattnernamespace llvm {
2156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
2256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// LibCallLocationInfo - This struct describes a set of memory locations that
2356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// are accessed by libcalls.  Identification of a location is doing with a
2456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// simple callback function.
2556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  ///
2656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// For example, the LibCallInfo may be set up to model the behavior of
2756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// standard libm functions.  The location that they may be interested in is
2856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// an abstract location that represents errno for the current target.  In
2956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// this case, a location for errno is anything such that the predicate
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// returns true.  On Mac OS X, this predicate would return true if the
3156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// pointer is the result of a call to "__error()".
3256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  ///
3356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// Locations can also be defined in a constant-sensitive way.  For example,
3456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// it is possible to define a location that returns true iff it is passed
3556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// into the call as a specific argument.  This is useful for modeling things
3656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// like "printf", which can store to memory, but only through pointers passed
3756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// with a '%n' constraint.
3856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  ///
3956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  struct LibCallLocationInfo {
4056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    // TODO: Flags: isContextSensitive etc.
4156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
4256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// isLocation - Return a LocResult if the specified pointer refers to this
4356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// location for the specified call site.  This returns "Yes" if we can tell
4456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// that the pointer *does definitely* refer to the location, "No" if we can
4556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// tell that the location *definitely does not* refer to the location, and
4656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// returns "Unknown" if we cannot tell for certain.
4756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    enum LocResult {
4856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      Yes, No, Unknown
4956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    };
50503e204470d7d49eacf973cef28ec26ff6641914Dan Gohman    LocResult (*isLocation)(ImmutableCallSite CS,
51cfa06abaf7c5c3ef31e42e8b7e3a1592db4eaec4Dan Gohman                            const AliasAnalysis::Location &Loc);
5256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  };
5356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
5456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// LibCallFunctionInfo - Each record in the array of FunctionInfo structs
5556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// records the behavior of one libcall that is known by the optimizer.  This
5656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// captures things like the side effects of the call.  Side effects are
5756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// modeled both universally (in the readnone/readonly) sense, but also
5856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// potentially against a set of abstract locations defined by the optimizer.
5956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// This allows an optimizer to define that some libcall (e.g. sqrt) is
6056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// side-effect free except that it might modify errno (thus, the call is
6156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// *not* universally readonly).  Or it might say that the side effects
6256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// are unknown other than to say that errno is not modified.
6356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  ///
6456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  struct LibCallFunctionInfo {
6556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// Name - This is the name of the libcall this describes.
6656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    const char *Name;
6756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
6856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// TODO: Constant folding function: Constant* vector -> Constant*.
6956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
7056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// UniversalBehavior - This captures the absolute mod/ref behavior without
7156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// any specific context knowledge.  For example, if the function is known
7256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// to be readonly, this would be set to 'ref'.  If known to be readnone,
7356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// this is set to NoModRef.
7456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    AliasAnalysis::ModRefResult UniversalBehavior;
7556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
7656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// LocationMRInfo - This pair captures info about whether a specific
7756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// location is modified or referenced by a libcall.
7856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    struct LocationMRInfo {
7956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// LocationID - ID # of the accessed location or ~0U for array end.
8056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      unsigned LocationID;
8156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// MRInfo - Mod/Ref info for this location.
8256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      AliasAnalysis::ModRefResult MRInfo;
8356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    };
8456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
8556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// DetailsType - Indicate the sense of the LocationDetails array.  This
8656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// controls how the LocationDetails array is interpreted.
8756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    enum {
8856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// DoesOnly - If DetailsType is set to DoesOnly, then we know that the
8956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// *only* mod/ref behavior of this function is captured by the
9056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// LocationDetails array.  If we are trying to say that 'sqrt' can only
9156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// modify errno, we'd have the {errnoloc,mod} in the LocationDetails
9256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// array and have DetailsType set to DoesOnly.
9356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      DoesOnly,
9456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
9556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// DoesNot - If DetailsType is set to DoesNot, then the sense of the
9656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// LocationDetails array is completely inverted.  This means that we *do
9756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// not* know everything about the side effects of this libcall, but we do
9856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// know things that the libcall cannot do.  This is useful for complex
9956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// functions like 'ctime' which have crazy mod/ref behavior, but are
10056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// known to never read or write errno.  In this case, we'd have
10156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// {errnoloc,modref} in the LocationDetails array and DetailsType would
10256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// be set to DoesNot, indicating that ctime does not read or write the
10356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      /// errno location.
10456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      DoesNot
10556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    } DetailsType;
10656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
10756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// LocationDetails - This is a pointer to an array of LocationMRInfo
10856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// structs which indicates the behavior of the libcall w.r.t. specific
10956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// locations.  For example, if this libcall is known to only modify
11056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// 'errno', it would have a LocationDetails array with the errno ID and
11156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// 'mod' in it.  See the DetailsType field for how this is interpreted.
11256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    ///
11356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// In the "DoesOnly" case, this information is 'may' information for: there
11456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// is no guarantee that the specified side effect actually does happen,
11556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// just that it could.  In the "DoesNot" case, this is 'must not' info.
11656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    ///
11756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// If this pointer is null, no details are known.
11856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    ///
11956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    const LocationMRInfo *LocationDetails;
12056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  };
12156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
12256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
12356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// LibCallInfo - Abstract interface to query about library call information.
12456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// Instances of this class return known information about some set of
12556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  /// libcalls.
12656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  ///
12756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  class LibCallInfo {
12856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    // Implementation details of this object, private.
12956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    mutable void *Impl;
13056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    mutable const LibCallLocationInfo *Locations;
13156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    mutable unsigned NumLocations;
13256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  public:
133dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    LibCallInfo() : Impl(nullptr), Locations(nullptr), NumLocations(0) {}
13456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    virtual ~LibCallInfo();
13556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
13656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    //===------------------------------------------------------------------===//
13756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    //  Accessor Methods: Efficient access to contained data.
13856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    //===------------------------------------------------------------------===//
13956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
14056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// getLocationInfo - Return information about the specified LocationID.
14156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    const LibCallLocationInfo &getLocationInfo(unsigned LocID) const;
14256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
14356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
14456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// getFunctionInfo - Return the LibCallFunctionInfo object corresponding to
14556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// the specified function if we have it.  If not, return null.
146503e204470d7d49eacf973cef28ec26ff6641914Dan Gohman    const LibCallFunctionInfo *getFunctionInfo(const Function *F) const;
14756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
14856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
14956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    //===------------------------------------------------------------------===//
15056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    //  Implementation Methods: Subclasses should implement these.
15156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    //===------------------------------------------------------------------===//
15256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
15356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// getLocationInfo - Return descriptors for the locations referenced by
15456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// this set of libcalls.
15556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    virtual unsigned getLocationInfo(const LibCallLocationInfo *&Array) const {
15656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner      return 0;
15756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    }
15856eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
15956eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// getFunctionInfoArray - Return an array of descriptors that describe the
16056eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// set of libcalls represented by this LibCallInfo object.  This array is
16156eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    /// terminated by an entry with a NULL name.
16256eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner    virtual const LibCallFunctionInfo *getFunctionInfoArray() const = 0;
16356eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner  };
16456eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
16556eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner} // end namespace llvm
16656eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner
16756eb21d20a098073395889490b5fd2e2fbdbe0a4Chris Lattner#endif
168