1//===-- llvm/Instrinsics.h - LLVM Intrinsic Function Handling ---*- C++ -*-===//
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 a set of enums which allow processing of intrinsic
11// functions.  Values of these enum types are returned by
12// Function::getIntrinsicID.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_INTRINSICS_H
17#define LLVM_IR_INTRINSICS_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/Optional.h"
22#include <string>
23
24namespace llvm {
25
26class Type;
27class FunctionType;
28class Function;
29class LLVMContext;
30class Module;
31class AttributeSet;
32
33/// This namespace contains an enum with a value for every intrinsic/builtin
34/// function known by LLVM. The enum values are returned by
35/// Function::getIntrinsicID().
36namespace Intrinsic {
37  enum ID : unsigned {
38    not_intrinsic = 0,   // Must be zero
39
40    // Get the intrinsic enums generated from Intrinsics.td
41#define GET_INTRINSIC_ENUM_VALUES
42#include "llvm/IR/Intrinsics.gen"
43#undef GET_INTRINSIC_ENUM_VALUES
44    , num_intrinsics
45  };
46
47  /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
48  std::string getName(ID id, ArrayRef<Type*> Tys = None);
49
50  /// Return the function type for an intrinsic.
51  FunctionType *getType(LLVMContext &Context, ID id,
52                        ArrayRef<Type*> Tys = None);
53
54  /// Returns true if the intrinsic can be overloaded.
55  bool isOverloaded(ID id);
56
57  /// Returns true if the intrinsic is a leaf, i.e. it does not make any calls
58  /// itself.  Most intrinsics are leafs, the exceptions being the patchpoint
59  /// and statepoint intrinsics. These call (or invoke) their "target" argument.
60  bool isLeaf(ID id);
61
62  /// Return the attributes for an intrinsic.
63  AttributeSet getAttributes(LLVMContext &C, ID id);
64
65  /// Create or insert an LLVM Function declaration for an intrinsic, and return
66  /// it.
67  ///
68  /// The Tys parameter is for intrinsics with overloaded types (e.g., those
69  /// using iAny, fAny, vAny, or iPTRAny).  For a declaration of an overloaded
70  /// intrinsic, Tys must provide exactly one type for each overloaded type in
71  /// the intrinsic.
72  Function *getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys = None);
73
74  /// Looks up Name in NameTable via binary search. NameTable must be sorted
75  /// and all entries must start with "llvm.".  If NameTable contains an exact
76  /// match for Name or a prefix of Name followed by a dot, its index in
77  /// NameTable is returned. Otherwise, -1 is returned.
78  int lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
79                                StringRef Name);
80
81  /// Map a GCC builtin name to an intrinsic ID.
82  ID getIntrinsicForGCCBuiltin(const char *Prefix, const char *BuiltinName);
83
84  /// Map a MS builtin name to an intrinsic ID.
85  ID getIntrinsicForMSBuiltin(const char *Prefix, const char *BuiltinName);
86
87  /// This is a type descriptor which explains the type requirements of an
88  /// intrinsic. This is returned by getIntrinsicInfoTableEntries.
89  struct IITDescriptor {
90    enum IITDescriptorKind {
91      Void, VarArg, MMX, Token, Metadata, Half, Float, Double,
92      Integer, Vector, Pointer, Struct,
93      Argument, ExtendArgument, TruncArgument, HalfVecArgument,
94      SameVecWidthArgument, PtrToArgument, VecOfPtrsToElt
95    } Kind;
96
97    union {
98      unsigned Integer_Width;
99      unsigned Float_Width;
100      unsigned Vector_Width;
101      unsigned Pointer_AddressSpace;
102      unsigned Struct_NumElements;
103      unsigned Argument_Info;
104    };
105
106    enum ArgKind {
107      AK_Any,
108      AK_AnyInteger,
109      AK_AnyFloat,
110      AK_AnyVector,
111      AK_AnyPointer
112    };
113    unsigned getArgumentNumber() const {
114      assert(Kind == Argument || Kind == ExtendArgument ||
115             Kind == TruncArgument || Kind == HalfVecArgument ||
116             Kind == SameVecWidthArgument || Kind == PtrToArgument ||
117             Kind == VecOfPtrsToElt);
118      return Argument_Info >> 3;
119    }
120    ArgKind getArgumentKind() const {
121      assert(Kind == Argument || Kind == ExtendArgument ||
122             Kind == TruncArgument || Kind == HalfVecArgument ||
123             Kind == SameVecWidthArgument || Kind == PtrToArgument ||
124             Kind == VecOfPtrsToElt);
125      return (ArgKind)(Argument_Info & 7);
126    }
127
128    static IITDescriptor get(IITDescriptorKind K, unsigned Field) {
129      IITDescriptor Result = { K, { Field } };
130      return Result;
131    }
132  };
133
134  /// Return the IIT table descriptor for the specified intrinsic into an array
135  /// of IITDescriptors.
136  void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl<IITDescriptor> &T);
137
138  /// Match the specified type (which comes from an intrinsic argument or return
139  /// value) with the type constraints specified by the .td file. If the given
140  /// type is an overloaded type it is pushed to the ArgTys vector.
141  ///
142  /// Returns false if the given type matches with the constraints, true
143  /// otherwise.
144  bool matchIntrinsicType(Type *Ty, ArrayRef<IITDescriptor> &Infos,
145                          SmallVectorImpl<Type*> &ArgTys);
146
147  /// Verify if the intrinsic has variable arguments. This method is intended to
148  /// be called after all the fixed arguments have been matched first.
149  ///
150  /// This method returns true on error.
151  bool matchIntrinsicVarArg(bool isVarArg, ArrayRef<IITDescriptor> &Infos);
152
153  // Checks if the intrinsic name matches with its signature and if not
154  // returns the declaration with the same signature and remangled name.
155  llvm::Optional<Function*> remangleIntrinsicFunction(Function *F);
156
157} // End Intrinsic namespace
158
159} // End llvm namespace
160
161#endif
162