1 //===-- llvm/Instrinsics.h - LLVM Intrinsic Function Handling ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_INTRINSICS_H
17 #define LLVM_IR_INTRINSICS_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
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().
38 not_intrinsic = 0, // Must be zero
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
47 /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
48 /// Note, this version is for intrinsics with no overloads. Use the other
49 /// version of getName if overloads are required.
50 StringRef getName(ID id);
52 /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
53 /// Note, this version of getName supports overloads, but is less efficient
54 /// than the StringRef version of this function. If no overloads are
55 /// requried, it is safe to use this version, but better to use the StringRef
57 std::string getName(ID id, ArrayRef<Type*> Tys);
59 /// Return the function type for an intrinsic.
60 FunctionType *getType(LLVMContext &Context, ID id,
61 ArrayRef<Type*> Tys = None);
63 /// Returns true if the intrinsic can be overloaded.
64 bool isOverloaded(ID id);
66 /// Returns true if the intrinsic is a leaf, i.e. it does not make any calls
67 /// itself. Most intrinsics are leafs, the exceptions being the patchpoint
68 /// and statepoint intrinsics. These call (or invoke) their "target" argument.
71 /// Return the attributes for an intrinsic.
72 AttributeSet getAttributes(LLVMContext &C, ID id);
74 /// Create or insert an LLVM Function declaration for an intrinsic, and return
77 /// The Tys parameter is for intrinsics with overloaded types (e.g., those
78 /// using iAny, fAny, vAny, or iPTRAny). For a declaration of an overloaded
79 /// intrinsic, Tys must provide exactly one type for each overloaded type in
81 Function *getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys = None);
83 /// Looks up Name in NameTable via binary search. NameTable must be sorted
84 /// and all entries must start with "llvm.". If NameTable contains an exact
85 /// match for Name or a prefix of Name followed by a dot, its index in
86 /// NameTable is returned. Otherwise, -1 is returned.
87 int lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
90 /// Map a GCC builtin name to an intrinsic ID.
91 ID getIntrinsicForGCCBuiltin(const char *Prefix, StringRef BuiltinName);
93 /// Map a MS builtin name to an intrinsic ID.
94 ID getIntrinsicForMSBuiltin(const char *Prefix, StringRef BuiltinName);
96 /// This is a type descriptor which explains the type requirements of an
97 /// intrinsic. This is returned by getIntrinsicInfoTableEntries.
98 struct IITDescriptor {
99 enum IITDescriptorKind {
100 Void, VarArg, MMX, Token, Metadata, Half, Float, Double,
101 Integer, Vector, Pointer, Struct,
102 Argument, ExtendArgument, TruncArgument, HalfVecArgument,
103 SameVecWidthArgument, PtrToArgument, PtrToElt, VecOfPtrsToElt
107 unsigned Integer_Width;
108 unsigned Float_Width;
109 unsigned Vector_Width;
110 unsigned Pointer_AddressSpace;
111 unsigned Struct_NumElements;
112 unsigned Argument_Info;
122 unsigned getArgumentNumber() const {
123 assert(Kind == Argument || Kind == ExtendArgument ||
124 Kind == TruncArgument || Kind == HalfVecArgument ||
125 Kind == SameVecWidthArgument || Kind == PtrToArgument ||
126 Kind == PtrToElt || Kind == VecOfPtrsToElt);
127 return Argument_Info >> 3;
129 ArgKind getArgumentKind() const {
130 assert(Kind == Argument || Kind == ExtendArgument ||
131 Kind == TruncArgument || Kind == HalfVecArgument ||
132 Kind == SameVecWidthArgument || Kind == PtrToArgument ||
133 Kind == VecOfPtrsToElt);
134 return (ArgKind)(Argument_Info & 7);
137 static IITDescriptor get(IITDescriptorKind K, unsigned Field) {
138 IITDescriptor Result = { K, { Field } };
143 /// Return the IIT table descriptor for the specified intrinsic into an array
144 /// of IITDescriptors.
145 void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl<IITDescriptor> &T);
147 /// Match the specified type (which comes from an intrinsic argument or return
148 /// value) with the type constraints specified by the .td file. If the given
149 /// type is an overloaded type it is pushed to the ArgTys vector.
151 /// Returns false if the given type matches with the constraints, true
153 bool matchIntrinsicType(Type *Ty, ArrayRef<IITDescriptor> &Infos,
154 SmallVectorImpl<Type*> &ArgTys);
156 /// Verify if the intrinsic has variable arguments. This method is intended to
157 /// be called after all the fixed arguments have been matched first.
159 /// This method returns true on error.
160 bool matchIntrinsicVarArg(bool isVarArg, ArrayRef<IITDescriptor> &Infos);
162 // Checks if the intrinsic name matches with its signature and if not
163 // returns the declaration with the same signature and remangled name.
164 llvm::Optional<Function*> remangleIntrinsicFunction(Function *F);
166 } // End Intrinsic namespace
168 } // End llvm namespace