Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 1 | //===- AMDGPUIntrinsicInfo.cpp - AMDGPU Intrinsic Information ---*- C++ -*-===// |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 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 | |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 15 | #include "AMDGPUIntrinsicInfo.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 16 | #include "AMDGPUSubtarget.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/IR/Intrinsics.h" |
| 19 | #include "llvm/IR/Module.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Eric Christopher | ac4b69e | 2014-07-25 22:22:39 +0000 | [diff] [blame] | 23 | AMDGPUIntrinsicInfo::AMDGPUIntrinsicInfo() |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 24 | : TargetIntrinsicInfo() {} |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 25 | |
Reid Kleckner | 1c93b4c | 2016-01-26 23:01:21 +0000 | [diff] [blame] | 26 | static const char *const IntrinsicNameTable[] = { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 27 | #define GET_INTRINSIC_NAME_TABLE |
| 28 | #include "AMDGPUGenIntrinsics.inc" |
| 29 | #undef GET_INTRINSIC_NAME_TABLE |
Reid Kleckner | 1c93b4c | 2016-01-26 23:01:21 +0000 | [diff] [blame] | 30 | }; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 31 | |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | #define GET_INTRINSIC_ATTRIBUTES |
| 34 | #include "AMDGPUGenIntrinsics.inc" |
| 35 | #undef GET_INTRINSIC_ATTRIBUTES |
| 36 | } |
| 37 | |
| 38 | StringRef AMDGPUIntrinsicInfo::getName(unsigned IntrID, |
| 39 | ArrayRef<Type *> Tys) const { |
| 40 | if (IntrID < Intrinsic::num_intrinsics) |
| 41 | return StringRef(); |
| 42 | |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 43 | assert(IntrID < AMDGPUIntrinsic::num_AMDGPU_intrinsics && |
| 44 | "Invalid intrinsic ID"); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 45 | |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 46 | return IntrinsicNameTable[IntrID - Intrinsic::num_intrinsics]; |
| 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 | |
| 54 | FunctionType *AMDGPUIntrinsicInfo::getType(LLVMContext &Context, unsigned ID, |
| 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 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Reid Kleckner | 1c93b4c | 2016-01-26 23:01:21 +0000 | [diff] [blame] | 67 | unsigned AMDGPUIntrinsicInfo::lookupName(const char *NameData, |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 68 | unsigned Len) const { |
Reid Kleckner | 1c93b4c | 2016-01-26 23:01:21 +0000 | [diff] [blame] | 69 | StringRef Name(NameData, Len); |
| 70 | if (!Name.startswith("llvm.")) |
Rafael Espindola | ebd8e38 | 2013-05-22 14:57:42 +0000 | [diff] [blame] | 71 | return 0; // All intrinsics start with 'llvm.' |
| 72 | |
Reid Kleckner | 1c93b4c | 2016-01-26 23:01:21 +0000 | [diff] [blame] | 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. |
| 76 | int Idx = Intrinsic::lookupLLVMIntrinsicByName(IntrinsicNameTable, Name); |
| 77 | if (Idx >= 0) { |
| 78 | bool IsPrefixMatch = Name.size() > strlen(IntrinsicNameTable[Idx]); |
| 79 | return IsPrefixMatch == isOverloaded(Idx + 1) |
| 80 | ? Intrinsic::num_intrinsics + Idx |
| 81 | : 0; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 82 | } |
Reid Kleckner | 1c93b4c | 2016-01-26 23:01:21 +0000 | [diff] [blame] | 83 | |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 84 | return 0; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 87 | bool AMDGPUIntrinsicInfo::isOverloaded(unsigned id) const { |
| 88 | // Overload Table |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 89 | #define GET_INTRINSIC_OVERLOAD_TABLE |
| 90 | #include "AMDGPUGenIntrinsics.inc" |
| 91 | #undef GET_INTRINSIC_OVERLOAD_TABLE |
| 92 | } |
| 93 | |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 94 | Function *AMDGPUIntrinsicInfo::getDeclaration(Module *M, unsigned IntrID, |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 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 | |
| 100 | AttributeSet AS = getAttributes(M->getContext(), |
| 101 | static_cast<AMDGPUIntrinsic::ID>(IntrID)); |
| 102 | F->setAttributes(AS); |
| 103 | return F; |
| 104 | } |
| 105 | |
| 106 | Function *AMDGPUIntrinsicInfo::getDeclaration(Module *M, unsigned IntrID, |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 107 | Type **Tys, |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 108 | unsigned NumTys) const { |
| 109 | return getDeclaration(M, IntrID, makeArrayRef(Tys, NumTys)); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 110 | } |