LLVM  4.0.0
AMDGPUIntrinsicInfo.cpp
Go to the documentation of this file.
1 //===- AMDGPUIntrinsicInfo.cpp - AMDGPU Intrinsic Information ---*- 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 /// \file
11 /// \brief AMDGPU Implementation of the IntrinsicInfo class.
12 //
13 //===-----------------------------------------------------------------------===//
14 
15 #include "AMDGPUIntrinsicInfo.h"
16 #include "AMDGPUSubtarget.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/Module.h"
20 
21 using namespace llvm;
22 
24  : TargetIntrinsicInfo() {}
25 
26 static const char *const IntrinsicNameTable[] = {
27 #define GET_INTRINSIC_NAME_TABLE
28 #include "AMDGPUGenIntrinsics.inc"
29 #undef GET_INTRINSIC_NAME_TABLE
30 };
31 
32 namespace {
33 #define GET_INTRINSIC_ATTRIBUTES
34 #include "AMDGPUGenIntrinsics.inc"
35 #undef GET_INTRINSIC_ATTRIBUTES
36 }
37 
39  ArrayRef<Type *> Tys) const {
40  if (IntrID < Intrinsic::num_intrinsics)
41  return StringRef();
42 
44  "Invalid intrinsic ID");
45 
47 }
48 
49 std::string AMDGPUIntrinsicInfo::getName(unsigned IntrID, Type **Tys,
50  unsigned NumTys) const {
51  return getName(IntrID, makeArrayRef(Tys, NumTys)).str();
52 }
53 
55  ArrayRef<Type*> Tys) const {
56  // FIXME: Re-use Intrinsic::getType machinery
57  switch (ID) {
58  case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
59  Type *F32Ty = Type::getFloatTy(Context);
60  return FunctionType::get(F32Ty, { F32Ty, F32Ty }, false);
61  }
62  default:
63  llvm_unreachable("unhandled intrinsic");
64  }
65 }
66 
67 unsigned AMDGPUIntrinsicInfo::lookupName(const char *NameData,
68  unsigned Len) const {
69  StringRef Name(NameData, Len);
70  if (!Name.startswith("llvm."))
71  return 0; // All intrinsics start with 'llvm.'
72 
73  // Look for a name match in our table. If the intrinsic is not overloaded,
74  // require an exact match. If it is overloaded, require a prefix match. The
75  // AMDGPU enum enum starts at Intrinsic::num_intrinsics.
77  if (Idx >= 0) {
78  bool IsPrefixMatch = Name.size() > strlen(IntrinsicNameTable[Idx]);
79  return IsPrefixMatch == isOverloaded(Idx + 1)
81  : 0;
82  }
83 
84  return 0;
85 }
86 
87 bool AMDGPUIntrinsicInfo::isOverloaded(unsigned id) const {
88 // Overload Table
89 #define GET_INTRINSIC_OVERLOAD_TABLE
90 #include "AMDGPUGenIntrinsics.inc"
91 #undef GET_INTRINSIC_OVERLOAD_TABLE
92 }
93 
95  ArrayRef<Type *> Tys) const {
96  FunctionType *FTy = getType(M->getContext(), IntrID, Tys);
97  Function *F
98  = cast<Function>(M->getOrInsertFunction(getName(IntrID, Tys), FTy));
99 
101  static_cast<AMDGPUIntrinsic::ID>(IntrID));
102  F->setAttributes(AS);
103  return F;
104 }
105 
107  Type **Tys,
108  unsigned NumTys) const {
109  return getDeclaration(M, IntrID, makeArrayRef(Tys, NumTys));
110 }
LLVMContext & Context
AMDGPU specific subclass of TargetSubtarget.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
unsigned lookupName(const char *Name, unsigned Len) const override
Look up target intrinsic by name.
bool isOverloaded(unsigned IID) const override
Returns true if the intrinsic can be overloaded.
StringRef getName(unsigned IntrId, ArrayRef< Type * > Tys=None) const
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:157
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:440
Class to represent function types.
Definition: DerivedTypes.h:102
#define F(x, y, z)
Definition: MD5.cpp:51
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:264
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:291
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeSet AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:123
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
FunctionType * getType(LLVMContext &Context, unsigned ID, ArrayRef< Type * > Tys=None) const
Function * getDeclaration(Module *M, unsigned ID, Type **Tys=nullptr, unsigned NumTys=0) const override
Create or insert an LLVM Function declaration for an intrinsic, and return it.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:225
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetIntrinsicInfo - Interface to description of machine instruction set.
static const char *const IntrinsicNameTable[]
Module.h This file contains the declarations for the Module class.
Interface for the AMDGPU Implementation of the Intrinsic Info class.
AttributeSet getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
int lookupLLVMIntrinsicByName(ArrayRef< const char * > NameTable, StringRef Name)
Looks up Name in NameTable via binary search.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:222