blob: d9e0d25142f0980bebb89bb8071a78d5c252d56e [file] [log] [blame]
Chris Lattnerc313d0b2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc313d0b2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000014#include "CodeGenIntrinsics.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000015#include "CodeGenTarget.h"
Chris Lattnera3b0f522012-05-17 15:55:41 +000016#include "SequenceToOffsetTable.h"
Richard Smith6bc9df32014-04-20 20:26:39 +000017#include "TableGenBackends.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000018#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000019#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000020#include "llvm/TableGen/Record.h"
Douglas Gregor12c1cd32012-05-02 17:32:48 +000021#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000022#include "llvm/TableGen/TableGenBackend.h"
Reid Kleckner5b463712016-01-27 01:43:12 +000023#include "llvm/TableGen/StringToOffsetTable.h"
Jeff Cohenc4e24682006-03-15 02:51:05 +000024#include <algorithm>
Chris Lattnerc313d0b2006-03-03 02:32:46 +000025using namespace llvm;
26
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000027namespace {
28class IntrinsicEmitter {
29 RecordKeeper &Records;
30 bool TargetOnly;
31 std::string TargetPrefix;
32
33public:
34 IntrinsicEmitter(RecordKeeper &R, bool T)
35 : Records(R), TargetOnly(T) {}
36
37 void run(raw_ostream &OS);
38
39 void EmitPrefix(raw_ostream &OS);
40
Justin Bogner92a8c612016-07-15 16:31:37 +000041 void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
42 void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
43 void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000044 raw_ostream &OS);
Justin Bogner92a8c612016-07-15 16:31:37 +000045 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000046 raw_ostream &OS);
Justin Bogner92a8c612016-07-15 16:31:37 +000047 void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
48 void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS);
49 void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC,
50 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000051 void EmitSuffix(raw_ostream &OS);
52};
53} // End anonymous namespace
54
Chris Lattnerc313d0b2006-03-03 02:32:46 +000055//===----------------------------------------------------------------------===//
Chris Lattnerc313d0b2006-03-03 02:32:46 +000056// IntrinsicEmitter Implementation
57//===----------------------------------------------------------------------===//
58
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000059void IntrinsicEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000060 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
61
Justin Bogner92a8c612016-07-15 16:31:37 +000062 CodeGenIntrinsicTable Ints(Records, TargetOnly);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000063
Dale Johannesenb842d522009-02-05 01:49:45 +000064 if (TargetOnly && !Ints.empty())
65 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattnerc313d0b2006-03-03 02:32:46 +000066
Douglas Gregor6739a892010-05-11 06:17:44 +000067 EmitPrefix(OS);
68
Chris Lattnerc313d0b2006-03-03 02:32:46 +000069 // Emit the enum information.
70 EmitEnumInfo(Ints, OS);
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000071
Justin Bogner92a8c612016-07-15 16:31:37 +000072 // Emit the target metadata.
73 EmitTargetInfo(Ints, OS);
74
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000075 // Emit the intrinsic ID -> name table.
76 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wangb4024932009-02-24 23:17:49 +000077
78 // Emit the intrinsic ID -> overload table.
79 EmitIntrinsicToOverloadTable(Ints, OS);
80
Jim Laskey2682ea62007-02-07 20:38:26 +000081 // Emit the intrinsic declaration generator.
82 EmitGenerator(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000083
Duncan Sands38ef3a82007-12-03 20:06:50 +000084 // Emit the intrinsic parameter attributes.
85 EmitAttributes(Ints, OS);
Chris Lattnerfea17a92006-03-13 23:08:44 +000086
Aditya Nandakumar8a76f912017-04-19 19:14:20 +000087 // Emit code to translate GCC builtins into LLVM intrinsics.
88 EmitIntrinsicToBuiltinMap(Ints, true, OS);
Douglas Gregor6739a892010-05-11 06:17:44 +000089
Aditya Nandakumar8a76f912017-04-19 19:14:20 +000090 // Emit code to translate MS builtins into LLVM intrinsics.
91 EmitIntrinsicToBuiltinMap(Ints, false, OS);
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +000092
Douglas Gregor6739a892010-05-11 06:17:44 +000093 EmitSuffix(OS);
94}
95
96void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
97 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +000098 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
99 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000100 "# pragma push_macro(\"setjmp\")\n"
101 "# undef setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000102 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000103 "#endif\n\n";
104}
105
106void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencerded5f662010-09-24 19:48:47 +0000107 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000108 "// let's return it to _setjmp state\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000109 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000110 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000111 "#endif\n\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000112}
113
Justin Bogner92a8c612016-07-15 16:31:37 +0000114void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000115 raw_ostream &OS) {
Chris Lattner6d8104e2006-03-09 20:34:19 +0000116 OS << "// Enum values for Intrinsics.h\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000117 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
118 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
119 OS << " " << Ints[i].EnumName;
120 OS << ((i != e-1) ? ", " : " ");
Justin Holewinski8a5bf7f2014-07-17 11:23:29 +0000121 if (Ints[i].EnumName.size() < 40)
122 OS << std::string(40-Ints[i].EnumName.size(), ' ');
123 OS << " // " << Ints[i].Name << "\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000124 }
125 OS << "#endif\n\n";
126}
Chris Lattner6d8104e2006-03-09 20:34:19 +0000127
Justin Bogner92a8c612016-07-15 16:31:37 +0000128void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
129 raw_ostream &OS) {
130 OS << "// Target mapping\n";
131 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
132 OS << "struct IntrinsicTargetInfo {\n"
Saleem Abdulrasool6f5f0012017-01-31 00:45:01 +0000133 << " llvm::StringLiteral Name;\n"
Justin Bogner92a8c612016-07-15 16:31:37 +0000134 << " size_t Offset;\n"
135 << " size_t Count;\n"
136 << "};\n";
Benjamin Kramer74321132017-01-30 18:49:24 +0000137 OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
Justin Bogner92a8c612016-07-15 16:31:37 +0000138 for (auto Target : Ints.Targets)
Saleem Abdulrasool6f5f0012017-01-31 00:45:01 +0000139 OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
Benjamin Kramera9df9412017-01-30 19:05:09 +0000140 << ", " << Target.Count << "},\n";
Justin Bogner92a8c612016-07-15 16:31:37 +0000141 OS << "};\n";
142 OS << "#endif\n\n";
143}
144
145void IntrinsicEmitter::EmitIntrinsicToNameTable(
146 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000147 OS << "// Intrinsic ID to name table\n";
148 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
149 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000150 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
151 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000152 OS << "#endif\n\n";
153}
154
Justin Bogner92a8c612016-07-15 16:31:37 +0000155void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
156 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000157 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000158 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000159 OS << "static const uint8_t OTable[] = {\n";
160 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000161 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000162 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
163 if ((i+1)%8 == 0)
164 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000165 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000166 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000167 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000168 OS << "\n};\n\n";
169 // OTable contains a true bit at the position if the intrinsic is overloaded.
170 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000171 OS << "#endif\n\n";
172}
173
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000174
Chris Lattnerf39c2782012-05-27 18:28:35 +0000175// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000176enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000177 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000178 IIT_Done = 0,
179 IIT_I1 = 1,
180 IIT_I8 = 2,
181 IIT_I16 = 3,
182 IIT_I32 = 4,
183 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000184 IIT_F16 = 6,
185 IIT_F32 = 7,
186 IIT_F64 = 8,
187 IIT_V2 = 9,
188 IIT_V4 = 10,
189 IIT_V8 = 11,
190 IIT_V16 = 12,
191 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000192 IIT_PTR = 14,
193 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000194
Robert Khasanov65c27562014-10-20 19:25:05 +0000195 // Values from 16+ are only encodable with the inefficient encoding.
196 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000197 IIT_MMX = 17,
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000198 IIT_TOKEN = 18,
199 IIT_METADATA = 19,
200 IIT_EMPTYSTRUCT = 20,
201 IIT_STRUCT2 = 21,
202 IIT_STRUCT3 = 22,
203 IIT_STRUCT4 = 23,
204 IIT_STRUCT5 = 24,
205 IIT_EXTEND_ARG = 25,
206 IIT_TRUNC_ARG = 26,
207 IIT_ANYPTR = 27,
208 IIT_V1 = 28,
209 IIT_VARARG = 29,
210 IIT_HALF_VEC_ARG = 30,
211 IIT_SAME_VEC_WIDTH_ARG = 31,
212 IIT_PTR_TO_ARG = 32,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000213 IIT_PTR_TO_ELT = 33,
Elad Cohenef5798a2017-05-03 12:28:54 +0000214 IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000215 IIT_I128 = 35,
216 IIT_V512 = 36,
Artem Belevich786ca6a2017-10-12 17:40:00 +0000217 IIT_V1024 = 37,
218 IIT_STRUCT6 = 38,
219 IIT_STRUCT7 = 39,
220 IIT_STRUCT8 = 40
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000221};
222
223static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000224 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000225 if (MVT(VT).isInteger()) {
226 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000227 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000228 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000229 case 1: return Sig.push_back(IIT_I1);
230 case 8: return Sig.push_back(IIT_I8);
231 case 16: return Sig.push_back(IIT_I16);
232 case 32: return Sig.push_back(IIT_I32);
233 case 64: return Sig.push_back(IIT_I64);
Kit Barton66460332015-05-25 15:49:26 +0000234 case 128: return Sig.push_back(IIT_I128);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000235 }
236 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000237
Chris Lattner827b2532012-05-17 04:30:58 +0000238 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000239 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000240 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000241 case MVT::f32: return Sig.push_back(IIT_F32);
242 case MVT::f64: return Sig.push_back(IIT_F64);
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000243 case MVT::token: return Sig.push_back(IIT_TOKEN);
Chris Lattner827b2532012-05-17 04:30:58 +0000244 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
245 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
246 // MVT::OtherVT is used to mean the empty struct type here.
247 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000248 // MVT::isVoid is used to represent varargs here.
249 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000250 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000251}
252
Nico Weber262a1332016-10-25 17:46:29 +0000253#if defined(_MSC_VER) && !defined(__clang__)
254#pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
255#endif
256
Chris Lattnerc4644162012-05-27 16:39:08 +0000257static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000258 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000259
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000260 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000261 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000262 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000263 if (R->isSubClassOf("LLVMExtendedType"))
264 Sig.push_back(IIT_EXTEND_ARG);
265 else if (R->isSubClassOf("LLVMTruncatedType"))
266 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000267 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
268 Sig.push_back(IIT_HALF_VEC_ARG);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000269 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
270 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000271 Sig.push_back((Number << 3) | ArgCodes[Number]);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000272 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
273 EncodeFixedValueType(VT, Sig);
274 return;
275 }
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000276 else if (R->isSubClassOf("LLVMPointerTo"))
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000277 Sig.push_back(IIT_PTR_TO_ARG);
Elad Cohenef5798a2017-05-03 12:28:54 +0000278 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
279 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
280 unsigned ArgNo = ArgCodes.size();
281 ArgCodes.push_back(3 /*vAny*/);
282 // Encode overloaded ArgNo
283 Sig.push_back(ArgNo);
284 // Encode LLVMMatchType<Number> ArgNo
285 Sig.push_back(Number);
286 return;
287 } else if (R->isSubClassOf("LLVMPointerToElt"))
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000288 Sig.push_back(IIT_PTR_TO_ELT);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000289 else
290 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000291 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000292 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000293
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000294 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000295
Chris Lattnerc4644162012-05-27 16:39:08 +0000296 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000297 switch (VT) {
298 default: break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000299 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
300 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
301 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
302 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000303 case MVT::Any: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000304 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000305 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000306 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000307
Chris Lattnerc4644162012-05-27 16:39:08 +0000308 // Figure out what arg # this is consuming, and remember what kind it was.
309 unsigned ArgNo = ArgCodes.size();
310 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000311
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000312 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
313 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerc4644162012-05-27 16:39:08 +0000314 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000315
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000316 case MVT::iPTR: {
317 unsigned AddrSpace = 0;
318 if (R->isSubClassOf("LLVMQualPointerType")) {
319 AddrSpace = R->getValueAsInt("AddrSpace");
320 assert(AddrSpace < 256 && "Address space exceeds 255");
321 }
322 if (AddrSpace) {
323 Sig.push_back(IIT_ANYPTR);
324 Sig.push_back(AddrSpace);
325 } else {
326 Sig.push_back(IIT_PTR);
327 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000328 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000329 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000330 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000331
Craig Topper8561de92014-01-24 20:50:47 +0000332 if (MVT(VT).isVector()) {
333 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000334 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000335 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000336 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000337 case 2: Sig.push_back(IIT_V2); break;
338 case 4: Sig.push_back(IIT_V4); break;
339 case 8: Sig.push_back(IIT_V8); break;
340 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000341 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000342 case 64: Sig.push_back(IIT_V64); break;
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000343 case 512: Sig.push_back(IIT_V512); break;
344 case 1024: Sig.push_back(IIT_V1024); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000345 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000346
Craig Topper8561de92014-01-24 20:50:47 +0000347 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000348 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000349
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000350 EncodeFixedValueType(VT, Sig);
351}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000352
Nico Weber262a1332016-10-25 17:46:29 +0000353#if defined(_MSC_VER) && !defined(__clang__)
354#pragma optimize("",on)
355#endif
356
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000357/// ComputeFixedEncoding - If we can encode the type signature for this
358/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000359static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
360 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000361 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000362
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000363 if (Int.IS.RetVTs.empty())
364 TypeSig.push_back(IIT_Done);
365 else if (Int.IS.RetVTs.size() == 1 &&
366 Int.IS.RetVTs[0] == MVT::isVoid)
367 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000368 else {
369 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000370 case 1: break;
371 case 2: TypeSig.push_back(IIT_STRUCT2); break;
372 case 3: TypeSig.push_back(IIT_STRUCT3); break;
373 case 4: TypeSig.push_back(IIT_STRUCT4); break;
374 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Artem Belevich786ca6a2017-10-12 17:40:00 +0000375 case 6: TypeSig.push_back(IIT_STRUCT6); break;
376 case 7: TypeSig.push_back(IIT_STRUCT7); break;
377 case 8: TypeSig.push_back(IIT_STRUCT8); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000378 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000379 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000380
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000381 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000382 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000383 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000384
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000385 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000386 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000387}
388
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000389static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000390 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000391}
392
Justin Bogner92a8c612016-07-15 16:31:37 +0000393void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000394 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000395 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
396 // capture it in this vector, otherwise store a ~0U.
397 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000398
Chris Lattnera3b0f522012-05-17 15:55:41 +0000399 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000400
Chris Lattnera3b0f522012-05-17 15:55:41 +0000401 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000402
Jim Laskey2682ea62007-02-07 20:38:26 +0000403 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000404 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000405 // Get the signature for the intrinsic.
406 TypeSig.clear();
407 ComputeFixedEncoding(Ints[i], TypeSig);
408
409 // Check to see if we can encode it into a 32-bit word. We can only encode
410 // 8 nibbles into a 32-bit word.
411 if (TypeSig.size() <= 8) {
412 bool Failed = false;
413 unsigned Result = 0;
414 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
415 // If we had an unencodable argument, bail out.
416 if (TypeSig[i] > 15) {
417 Failed = true;
418 break;
419 }
420 Result = (Result << 4) | TypeSig[e-i-1];
421 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000422
Chris Lattnera3b0f522012-05-17 15:55:41 +0000423 // If this could be encoded into a 31-bit word, return it.
424 if (!Failed && (Result >> 31) == 0) {
425 FixedEncodings.push_back(Result);
426 continue;
427 }
428 }
429
430 // Otherwise, we're going to unique the sequence into the
431 // LongEncodingTable, and use its offset in the 32-bit table instead.
432 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000433
Chris Lattnera3b0f522012-05-17 15:55:41 +0000434 // This is a placehold that we'll replace after the table is laid out.
435 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000436 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000437
Chris Lattnera3b0f522012-05-17 15:55:41 +0000438 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000439
Chris Lattnerf39c2782012-05-27 18:28:35 +0000440 OS << "// Global intrinsic function declaration type table.\n";
441 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
442
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000443 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000444
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000445 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
446 if ((i & 7) == 7)
447 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000448
Chris Lattnera3b0f522012-05-17 15:55:41 +0000449 // If the entry fit in the table, just emit it.
450 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000451 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000452 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000453 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000454
Chris Lattnera3b0f522012-05-17 15:55:41 +0000455 TypeSig.clear();
456 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000457
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000458
Chris Lattnera3b0f522012-05-17 15:55:41 +0000459 // Otherwise, emit the offset into the long encoding table. We emit it this
460 // way so that it is easier to read the offset in the .def file.
461 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000462 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000463
Chris Lattnera3b0f522012-05-17 15:55:41 +0000464 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000465
Chris Lattnera3b0f522012-05-17 15:55:41 +0000466 // Emit the shared table of register lists.
467 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
468 if (!LongEncodingTable.empty())
469 LongEncodingTable.emit(OS, printIITEntry);
470 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000471
Patrik Hägglundca210d82012-05-23 12:34:56 +0000472 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000473}
474
Richard Smith6bc9df32014-04-20 20:26:39 +0000475namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000476struct AttributeComparator {
477 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
478 // Sort throwing intrinsics after non-throwing intrinsics.
479 if (L->canThrow != R->canThrow)
480 return R->canThrow;
481
Eli Bendersky2281ef92014-03-18 23:51:07 +0000482 if (L->isNoDuplicate != R->isNoDuplicate)
483 return R->isNoDuplicate;
484
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000485 if (L->isNoReturn != R->isNoReturn)
486 return R->isNoReturn;
487
Owen Anderson85fa7d52015-05-26 23:48:40 +0000488 if (L->isConvergent != R->isConvergent)
489 return R->isConvergent;
490
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000491 if (L->isSpeculatable != R->isSpeculatable)
492 return R->isSpeculatable;
493
Matt Arsenault868af922017-04-28 21:01:46 +0000494 if (L->hasSideEffects != R->hasSideEffects)
495 return R->hasSideEffects;
496
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000497 // Try to order by readonly/readnone attribute.
Nicolai Haehnleb48275f2016-04-19 21:58:33 +0000498 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
499 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000500 if (LK != RK) return (LK > RK);
501
502 // Order by argument attributes.
503 // This is reliable because each side is already sorted internally.
504 return (L->ArgumentAttributes < R->ArgumentAttributes);
505 }
506};
507} // End anonymous namespace
508
Chris Lattner49b7ee12009-01-12 01:18:58 +0000509/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Justin Bogner92a8c612016-07-15 16:31:37 +0000510void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
511 raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000512 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
513 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000514 if (TargetOnly)
Reid Klecknerb5180542017-03-21 16:57:19 +0000515 OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000516 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000517 else
Reid Klecknerb5180542017-03-21 16:57:19 +0000518 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000519
Craig Topperccd651c2012-02-28 06:32:00 +0000520 // Compute the maximum number of attribute arguments and the map
521 typedef std::map<const CodeGenIntrinsic*, unsigned,
522 AttributeComparator> UniqAttrMapTy;
523 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000524 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000525 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000526 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000527 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000528 maxArgAttrs =
529 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000530 unsigned &N = UniqAttributes[&intrinsic];
531 if (N) continue;
532 assert(AttrNum < 256 && "Too many unique attributes for table!");
533 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000534 }
John McCall375dcc92011-05-28 06:31:34 +0000535
Reid Klecknerb5180542017-03-21 16:57:19 +0000536 // Emit an array of AttributeList. Most intrinsics will have at least one
Bill Wendling4d3491c2013-01-27 03:25:05 +0000537 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000538 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000539
540 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
541 const CodeGenIntrinsic &intrinsic = Ints[i];
542
543 OS << " " << UniqAttributes[&intrinsic] << ", // "
544 << intrinsic.Name << "\n";
545 }
546 OS << " };\n\n";
547
Reid Klecknerb5180542017-03-21 16:57:19 +0000548 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000549 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000550 OS << " if (id != 0) {\n";
551 OS << " switch(IntrinsicsToAttributesMap[id - ";
552 if (TargetOnly)
553 OS << "Intrinsic::num_intrinsics";
554 else
555 OS << "1";
556 OS << "]) {\n";
557 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000558 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
559 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000560 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000561
Craig Topperccd651c2012-02-28 06:32:00 +0000562 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000563
564 // Keep track of the number of attributes we're writing out.
565 unsigned numAttrs = 0;
566
567 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000568 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
569 if (ae) {
570 while (ai != ae) {
571 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Reid Klecknera0b45f42017-05-03 18:17:31 +0000572 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
Craig Topperccd651c2012-02-28 06:32:00 +0000573
Reid Klecknera0b45f42017-05-03 18:17:31 +0000574 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000575 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000576
Bill Wendlinged42e792012-10-10 06:13:42 +0000577 do {
578 switch (intrinsic.ArgumentAttributes[ai].second) {
579 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000580 if (addComma)
581 OS << ",";
582 OS << "Attribute::NoCapture";
583 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000584 break;
Hal Finkel47646c02016-07-11 01:28:42 +0000585 case CodeGenIntrinsic::Returned:
586 if (addComma)
587 OS << ",";
588 OS << "Attribute::Returned";
589 addComma = true;
590 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000591 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000592 if (addComma)
593 OS << ",";
594 OS << "Attribute::ReadOnly";
595 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000596 break;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000597 case CodeGenIntrinsic::WriteOnly:
598 if (addComma)
599 OS << ",";
600 OS << "Attribute::WriteOnly";
601 addComma = true;
602 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000603 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000604 if (addComma)
605 OS << ",";
Eric Christopher10f5d602015-07-30 21:16:34 +0000606 OS << "Attribute::ReadNone";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000607 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000608 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000609 }
John McCall375dcc92011-05-28 06:31:34 +0000610
Bill Wendlinged42e792012-10-10 06:13:42 +0000611 ++ai;
612 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000613 OS << "};\n";
Reid Klecknerb5180542017-03-21 16:57:19 +0000614 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
Reid Klecknera0b45f42017-05-03 18:17:31 +0000615 << attrIdx << ", AttrParam" << attrIdx << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000616 }
John McCall375dcc92011-05-28 06:31:34 +0000617 }
618
Igor Laevsky30143ae2015-08-13 17:40:04 +0000619 if (!intrinsic.canThrow ||
620 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
621 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000622 intrinsic.isConvergent || intrinsic.isSpeculatable) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000623 OS << " const Attribute::AttrKind Atts[] = {";
624 bool addComma = false;
625 if (!intrinsic.canThrow) {
626 OS << "Attribute::NoUnwind";
627 addComma = true;
628 }
629 if (intrinsic.isNoReturn) {
630 if (addComma)
631 OS << ",";
632 OS << "Attribute::NoReturn";
633 addComma = true;
634 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000635 if (intrinsic.isNoDuplicate) {
636 if (addComma)
637 OS << ",";
638 OS << "Attribute::NoDuplicate";
639 addComma = true;
640 }
Owen Anderson85fa7d52015-05-26 23:48:40 +0000641 if (intrinsic.isConvergent) {
642 if (addComma)
643 OS << ",";
644 OS << "Attribute::Convergent";
645 addComma = true;
646 }
Matt Arsenaultb19b57e2017-04-28 20:25:27 +0000647 if (intrinsic.isSpeculatable) {
648 if (addComma)
649 OS << ",";
650 OS << "Attribute::Speculatable";
651 addComma = true;
652 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000653
Igor Laevsky30143ae2015-08-13 17:40:04 +0000654 switch (intrinsic.ModRef) {
655 case CodeGenIntrinsic::NoMem:
656 if (addComma)
657 OS << ",";
658 OS << "Attribute::ReadNone";
659 break;
660 case CodeGenIntrinsic::ReadArgMem:
661 if (addComma)
662 OS << ",";
663 OS << "Attribute::ReadOnly,";
664 OS << "Attribute::ArgMemOnly";
665 break;
666 case CodeGenIntrinsic::ReadMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000667 if (addComma)
668 OS << ",";
669 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000670 break;
Andrew Kaylor57d35bf2016-11-22 19:16:04 +0000671 case CodeGenIntrinsic::ReadInaccessibleMem:
672 if (addComma)
673 OS << ",";
674 OS << "Attribute::ReadOnly,";
675 OS << "Attribute::InaccessibleMemOnly";
676 break;
677 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
678 if (addComma)
679 OS << ",";
680 OS << "Attribute::ReadOnly,";
681 OS << "Attribute::InaccessibleMemOrArgMemOnly";
682 break;
Nicolai Haehnleb48275f2016-04-19 21:58:33 +0000683 case CodeGenIntrinsic::WriteArgMem:
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000684 if (addComma)
685 OS << ",";
686 OS << "Attribute::WriteOnly,";
687 OS << "Attribute::ArgMemOnly";
688 break;
689 case CodeGenIntrinsic::WriteMem:
690 if (addComma)
691 OS << ",";
692 OS << "Attribute::WriteOnly";
693 break;
Andrew Kaylor57d35bf2016-11-22 19:16:04 +0000694 case CodeGenIntrinsic::WriteInaccessibleMem:
695 if (addComma)
696 OS << ",";
697 OS << "Attribute::WriteOnly,";
698 OS << "Attribute::InaccessibleMemOnly";
699 break;
700 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
701 if (addComma)
702 OS << ",";
703 OS << "Attribute::WriteOnly,";
704 OS << "Attribute::InaccessibleMemOrArgOnly";
705 break;
Igor Laevsky30143ae2015-08-13 17:40:04 +0000706 case CodeGenIntrinsic::ReadWriteArgMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000707 if (addComma)
708 OS << ",";
Igor Laevsky30143ae2015-08-13 17:40:04 +0000709 OS << "Attribute::ArgMemOnly";
710 break;
Andrew Kaylor57d35bf2016-11-22 19:16:04 +0000711 case CodeGenIntrinsic::ReadWriteInaccessibleMem:
712 if (addComma)
713 OS << ",";
714 OS << "Attribute::InaccessibleMemOnly";
715 break;
716 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
717 if (addComma)
718 OS << ",";
719 OS << "Attribute::InaccessibleMemOrArgMemOnly";
Igor Laevsky30143ae2015-08-13 17:40:04 +0000720 case CodeGenIntrinsic::ReadWriteMem:
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000721 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000722 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000723 OS << "};\n";
Reid Klecknerb5180542017-03-21 16:57:19 +0000724 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
725 << "AttributeList::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000726 }
John McCall375dcc92011-05-28 06:31:34 +0000727
728 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000729 OS << " NumAttrs = " << numAttrs << ";\n";
730 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000731 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000732 } else {
Reid Klecknerb5180542017-03-21 16:57:19 +0000733 OS << " return AttributeList();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000734 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000735 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000736 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000737
Craig Topper374f19c2012-04-13 06:14:57 +0000738 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000739 OS << " }\n";
Reid Klecknerb5180542017-03-21 16:57:19 +0000740 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000741 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000742 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000743}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000744
Reid Kleckner5b463712016-01-27 01:43:12 +0000745void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
Justin Bogner92a8c612016-07-15 16:31:37 +0000746 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
Reid Kleckner5b463712016-01-27 01:43:12 +0000747 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
748 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000749 BIMTy BuiltinMap;
Reid Kleckner5b463712016-01-27 01:43:12 +0000750 StringToOffsetTable Table;
Chris Lattner402a5732006-03-15 01:33:26 +0000751 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Reid Kleckner5b463712016-01-27 01:43:12 +0000752 const std::string &BuiltinName =
753 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
754 if (!BuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000755 // Get the map for this target prefix.
Reid Kleckner5b463712016-01-27 01:43:12 +0000756 std::map<std::string, std::string> &BIM =
757 BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000758
Reid Kleckner5b463712016-01-27 01:43:12 +0000759 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000760 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
Reid Kleckner5b463712016-01-27 01:43:12 +0000761 "': duplicate " + CompilerName + " builtin name!");
762 Table.GetOrAddStringOffset(BuiltinName);
Chris Lattner402a5732006-03-15 01:33:26 +0000763 }
764 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000765
Reid Kleckner5b463712016-01-27 01:43:12 +0000766 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
767 OS << "// This is used by the C front-end. The builtin name is passed\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000768 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
769 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000770 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000771
Dale Johannesenb842d522009-02-05 01:49:45 +0000772 if (TargetOnly) {
773 OS << "static " << TargetPrefix << "Intrinsic::ID "
Reid Kleckner5b463712016-01-27 01:43:12 +0000774 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000775 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000776 } else {
Reid Kleckner5b463712016-01-27 01:43:12 +0000777 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
778 << "Builtin(const char "
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000779 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000780 }
Aditya Nandakumar8a76f912017-04-19 19:14:20 +0000781
782 if (Table.Empty()) {
783 OS << " return ";
784 if (!TargetPrefix.empty())
785 OS << "(" << TargetPrefix << "Intrinsic::ID)";
786 OS << "Intrinsic::not_intrinsic;\n";
787 OS << "}\n";
788 OS << "#endif\n\n";
789 return;
790 }
791
Reid Kleckner5b463712016-01-27 01:43:12 +0000792 OS << " static const char BuiltinNames[] = {\n";
793 Table.EmitCharArray(OS);
794 OS << " };\n\n";
795
796 OS << " struct BuiltinEntry {\n";
797 OS << " Intrinsic::ID IntrinID;\n";
798 OS << " unsigned StrTabOffset;\n";
799 OS << " const char *getName() const {\n";
800 OS << " return &BuiltinNames[StrTabOffset];\n";
801 OS << " }\n";
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000802 OS << " bool operator<(StringRef RHS) const {\n";
803 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000804 OS << " }\n";
805 OS << " };\n";
806
Chris Lattner497d13e2010-09-06 03:14:45 +0000807 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000808
Chris Lattner402a5732006-03-15 01:33:26 +0000809 // Note: this could emit significantly better code if we cared.
810 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000811 OS << " ";
812 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000813 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000814 else
815 OS << "/* Target Independent Builtins */ ";
816 OS << "{\n";
817
Chris Lattner91678fc2008-01-02 21:24:22 +0000818 // Emit the comparisons for this target prefix.
Reid Kleckner5b463712016-01-27 01:43:12 +0000819 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
820 for (const auto &P : I->second) {
821 OS << " {Intrinsic::" << P.second << ", "
822 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
823 }
824 OS << " };\n";
825 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
826 OS << " std::end(" << I->first << "Names),\n";
827 OS << " BuiltinNameStr);\n";
828 OS << " if (I != std::end(" << I->first << "Names) &&\n";
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000829 OS << " I->getName() == BuiltinNameStr)\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000830 OS << " return I->IntrinID;\n";
Chris Lattner91678fc2008-01-02 21:24:22 +0000831 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000832 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000833 OS << " return ";
834 if (!TargetPrefix.empty())
835 OS << "(" << TargetPrefix << "Intrinsic::ID)";
836 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000837 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000838 OS << "#endif\n\n";
839}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000840
Richard Smith6bc9df32014-04-20 20:26:39 +0000841void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000842 IntrinsicEmitter(RK, TargetOnly).run(OS);
843}