blob: 8eff650a84886bcbdeeef7b2b3d12ae0272a3245 [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
Reid Kleckner5b463712016-01-27 01:43:12 +000087 // Individual targets don't need GCC builtin name mappings.
88 if (!TargetOnly) {
89 // Emit code to translate GCC builtins into LLVM intrinsics.
90 EmitIntrinsicToBuiltinMap(Ints, true, OS);
Douglas Gregor6739a892010-05-11 06:17:44 +000091
Reid Kleckner5b463712016-01-27 01:43:12 +000092 // Emit code to translate MS builtins into LLVM intrinsics.
93 EmitIntrinsicToBuiltinMap(Ints, false, OS);
94 }
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +000095
Douglas Gregor6739a892010-05-11 06:17:44 +000096 EmitSuffix(OS);
97}
98
99void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
100 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000101 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
102 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000103 "# pragma push_macro(\"setjmp\")\n"
104 "# undef setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000105 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000106 "#endif\n\n";
107}
108
109void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencerded5f662010-09-24 19:48:47 +0000110 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000111 "// let's return it to _setjmp state\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000112 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000113 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000114 "#endif\n\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000115}
116
Justin Bogner92a8c612016-07-15 16:31:37 +0000117void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000118 raw_ostream &OS) {
Chris Lattner6d8104e2006-03-09 20:34:19 +0000119 OS << "// Enum values for Intrinsics.h\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000120 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
121 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
122 OS << " " << Ints[i].EnumName;
123 OS << ((i != e-1) ? ", " : " ");
Justin Holewinski8a5bf7f2014-07-17 11:23:29 +0000124 if (Ints[i].EnumName.size() < 40)
125 OS << std::string(40-Ints[i].EnumName.size(), ' ');
126 OS << " // " << Ints[i].Name << "\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000127 }
128 OS << "#endif\n\n";
129}
Chris Lattner6d8104e2006-03-09 20:34:19 +0000130
Justin Bogner92a8c612016-07-15 16:31:37 +0000131void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
132 raw_ostream &OS) {
133 OS << "// Target mapping\n";
134 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
135 OS << "struct IntrinsicTargetInfo {\n"
136 << " StringRef Name;\n"
137 << " size_t Offset;\n"
138 << " size_t Count;\n"
139 << "};\n";
140 OS << "static const IntrinsicTargetInfo TargetInfos[] = {\n";
141 for (auto Target : Ints.Targets)
142 OS << " {\"" << Target.Name << "\", " << Target.Offset << ", "
143 << Target.Count << "},\n";
144 OS << "};\n";
145 OS << "#endif\n\n";
146}
147
148void IntrinsicEmitter::EmitIntrinsicToNameTable(
149 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000150 OS << "// Intrinsic ID to name table\n";
151 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
152 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
154 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000155 OS << "#endif\n\n";
156}
157
Justin Bogner92a8c612016-07-15 16:31:37 +0000158void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
159 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000160 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000161 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000162 OS << "static const uint8_t OTable[] = {\n";
163 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000164 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000165 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
166 if ((i+1)%8 == 0)
167 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000168 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000169 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000170 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000171 OS << "\n};\n\n";
172 // OTable contains a true bit at the position if the intrinsic is overloaded.
173 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000174 OS << "#endif\n\n";
175}
176
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000177
Chris Lattnerf39c2782012-05-27 18:28:35 +0000178// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000179enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000180 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000181 IIT_Done = 0,
182 IIT_I1 = 1,
183 IIT_I8 = 2,
184 IIT_I16 = 3,
185 IIT_I32 = 4,
186 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000187 IIT_F16 = 6,
188 IIT_F32 = 7,
189 IIT_F64 = 8,
190 IIT_V2 = 9,
191 IIT_V4 = 10,
192 IIT_V8 = 11,
193 IIT_V16 = 12,
194 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000195 IIT_PTR = 14,
196 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000197
Robert Khasanov65c27562014-10-20 19:25:05 +0000198 // Values from 16+ are only encodable with the inefficient encoding.
199 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000200 IIT_MMX = 17,
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000201 IIT_TOKEN = 18,
202 IIT_METADATA = 19,
203 IIT_EMPTYSTRUCT = 20,
204 IIT_STRUCT2 = 21,
205 IIT_STRUCT3 = 22,
206 IIT_STRUCT4 = 23,
207 IIT_STRUCT5 = 24,
208 IIT_EXTEND_ARG = 25,
209 IIT_TRUNC_ARG = 26,
210 IIT_ANYPTR = 27,
211 IIT_V1 = 28,
212 IIT_VARARG = 29,
213 IIT_HALF_VEC_ARG = 30,
214 IIT_SAME_VEC_WIDTH_ARG = 31,
215 IIT_PTR_TO_ARG = 32,
216 IIT_VEC_OF_PTRS_TO_ELT = 33,
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000217 IIT_I128 = 34,
218 IIT_V512 = 35,
219 IIT_V1024 = 36
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000220};
221
Chris Lattner827b2532012-05-17 04:30:58 +0000222
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000223static 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
Reid Klecknerf3c82092015-02-26 21:34:11 +0000253#if defined(_MSC_VER) && !defined(__clang__)
254#pragma optimize("",off) // MSVC 2010 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);
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000278 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
279 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000280 else
281 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000282 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000283 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000284
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000285 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000286
Chris Lattnerc4644162012-05-27 16:39:08 +0000287 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000288 switch (VT) {
289 default: break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000290 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
291 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
292 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
293 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000294 case MVT::Any: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000295 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000296 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000297 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000298
Chris Lattnerc4644162012-05-27 16:39:08 +0000299 // Figure out what arg # this is consuming, and remember what kind it was.
300 unsigned ArgNo = ArgCodes.size();
301 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000302
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000303 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
304 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerc4644162012-05-27 16:39:08 +0000305 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000306
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000307 case MVT::iPTR: {
308 unsigned AddrSpace = 0;
309 if (R->isSubClassOf("LLVMQualPointerType")) {
310 AddrSpace = R->getValueAsInt("AddrSpace");
311 assert(AddrSpace < 256 && "Address space exceeds 255");
312 }
313 if (AddrSpace) {
314 Sig.push_back(IIT_ANYPTR);
315 Sig.push_back(AddrSpace);
316 } else {
317 Sig.push_back(IIT_PTR);
318 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000319 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000320 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000321 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000322
Craig Topper8561de92014-01-24 20:50:47 +0000323 if (MVT(VT).isVector()) {
324 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000325 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000326 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000327 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000328 case 2: Sig.push_back(IIT_V2); break;
329 case 4: Sig.push_back(IIT_V4); break;
330 case 8: Sig.push_back(IIT_V8); break;
331 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000332 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000333 case 64: Sig.push_back(IIT_V64); break;
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000334 case 512: Sig.push_back(IIT_V512); break;
335 case 1024: Sig.push_back(IIT_V1024); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000336 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000337
Craig Topper8561de92014-01-24 20:50:47 +0000338 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000339 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000340
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000341 EncodeFixedValueType(VT, Sig);
342}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000343
Reid Klecknerf3c82092015-02-26 21:34:11 +0000344#if defined(_MSC_VER) && !defined(__clang__)
345#pragma optimize("",on)
346#endif
347
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000348/// ComputeFixedEncoding - If we can encode the type signature for this
349/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000350static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
351 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000352 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000353
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000354 if (Int.IS.RetVTs.empty())
355 TypeSig.push_back(IIT_Done);
356 else if (Int.IS.RetVTs.size() == 1 &&
357 Int.IS.RetVTs[0] == MVT::isVoid)
358 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000359 else {
360 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000361 case 1: break;
362 case 2: TypeSig.push_back(IIT_STRUCT2); break;
363 case 3: TypeSig.push_back(IIT_STRUCT3); break;
364 case 4: TypeSig.push_back(IIT_STRUCT4); break;
365 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000366 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000367 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000368
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000369 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000370 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000371 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000372
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000373 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000374 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000375}
376
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000377static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000378 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000379}
380
Justin Bogner92a8c612016-07-15 16:31:37 +0000381void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000382 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000383 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
384 // capture it in this vector, otherwise store a ~0U.
385 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000386
Chris Lattnera3b0f522012-05-17 15:55:41 +0000387 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000388
Chris Lattnera3b0f522012-05-17 15:55:41 +0000389 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000390
Jim Laskey2682ea62007-02-07 20:38:26 +0000391 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000392 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000393 // Get the signature for the intrinsic.
394 TypeSig.clear();
395 ComputeFixedEncoding(Ints[i], TypeSig);
396
397 // Check to see if we can encode it into a 32-bit word. We can only encode
398 // 8 nibbles into a 32-bit word.
399 if (TypeSig.size() <= 8) {
400 bool Failed = false;
401 unsigned Result = 0;
402 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
403 // If we had an unencodable argument, bail out.
404 if (TypeSig[i] > 15) {
405 Failed = true;
406 break;
407 }
408 Result = (Result << 4) | TypeSig[e-i-1];
409 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000410
Chris Lattnera3b0f522012-05-17 15:55:41 +0000411 // If this could be encoded into a 31-bit word, return it.
412 if (!Failed && (Result >> 31) == 0) {
413 FixedEncodings.push_back(Result);
414 continue;
415 }
416 }
417
418 // Otherwise, we're going to unique the sequence into the
419 // LongEncodingTable, and use its offset in the 32-bit table instead.
420 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000421
Chris Lattnera3b0f522012-05-17 15:55:41 +0000422 // This is a placehold that we'll replace after the table is laid out.
423 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000424 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000425
Chris Lattnera3b0f522012-05-17 15:55:41 +0000426 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000427
Chris Lattnerf39c2782012-05-27 18:28:35 +0000428 OS << "// Global intrinsic function declaration type table.\n";
429 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
430
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000431 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000432
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000433 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
434 if ((i & 7) == 7)
435 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000436
Chris Lattnera3b0f522012-05-17 15:55:41 +0000437 // If the entry fit in the table, just emit it.
438 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000439 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000440 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000441 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000442
Chris Lattnera3b0f522012-05-17 15:55:41 +0000443 TypeSig.clear();
444 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000445
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000446
Chris Lattnera3b0f522012-05-17 15:55:41 +0000447 // Otherwise, emit the offset into the long encoding table. We emit it this
448 // way so that it is easier to read the offset in the .def file.
449 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000450 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000451
Chris Lattnera3b0f522012-05-17 15:55:41 +0000452 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000453
Chris Lattnera3b0f522012-05-17 15:55:41 +0000454 // Emit the shared table of register lists.
455 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
456 if (!LongEncodingTable.empty())
457 LongEncodingTable.emit(OS, printIITEntry);
458 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000459
Patrik Hägglundca210d82012-05-23 12:34:56 +0000460 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000461}
462
Richard Smith6bc9df32014-04-20 20:26:39 +0000463namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000464struct AttributeComparator {
465 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
466 // Sort throwing intrinsics after non-throwing intrinsics.
467 if (L->canThrow != R->canThrow)
468 return R->canThrow;
469
Eli Bendersky2281ef92014-03-18 23:51:07 +0000470 if (L->isNoDuplicate != R->isNoDuplicate)
471 return R->isNoDuplicate;
472
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000473 if (L->isNoReturn != R->isNoReturn)
474 return R->isNoReturn;
475
Owen Anderson85fa7d52015-05-26 23:48:40 +0000476 if (L->isConvergent != R->isConvergent)
477 return R->isConvergent;
478
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000479 // Try to order by readonly/readnone attribute.
Nicolai Haehnleb48275f2016-04-19 21:58:33 +0000480 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
481 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000482 if (LK != RK) return (LK > RK);
483
484 // Order by argument attributes.
485 // This is reliable because each side is already sorted internally.
486 return (L->ArgumentAttributes < R->ArgumentAttributes);
487 }
488};
489} // End anonymous namespace
490
Chris Lattner49b7ee12009-01-12 01:18:58 +0000491/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Justin Bogner92a8c612016-07-15 16:31:37 +0000492void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
493 raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000494 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
495 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000496 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000497 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000498 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000499 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000500 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000501
Craig Topperccd651c2012-02-28 06:32:00 +0000502 // Compute the maximum number of attribute arguments and the map
503 typedef std::map<const CodeGenIntrinsic*, unsigned,
504 AttributeComparator> UniqAttrMapTy;
505 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000506 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000507 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000508 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000509 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000510 maxArgAttrs =
511 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000512 unsigned &N = UniqAttributes[&intrinsic];
513 if (N) continue;
514 assert(AttrNum < 256 && "Too many unique attributes for table!");
515 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000516 }
John McCall375dcc92011-05-28 06:31:34 +0000517
Bill Wendling4d3491c2013-01-27 03:25:05 +0000518 // Emit an array of AttributeSet. Most intrinsics will have at least one
519 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000520 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000521
522 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
523 const CodeGenIntrinsic &intrinsic = Ints[i];
524
525 OS << " " << UniqAttributes[&intrinsic] << ", // "
526 << intrinsic.Name << "\n";
527 }
528 OS << " };\n\n";
529
Bill Wendling4d3491c2013-01-27 03:25:05 +0000530 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000531 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000532 OS << " if (id != 0) {\n";
533 OS << " switch(IntrinsicsToAttributesMap[id - ";
534 if (TargetOnly)
535 OS << "Intrinsic::num_intrinsics";
536 else
537 OS << "1";
538 OS << "]) {\n";
539 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000540 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
541 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000542 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000543
Craig Topperccd651c2012-02-28 06:32:00 +0000544 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000545
546 // Keep track of the number of attributes we're writing out.
547 unsigned numAttrs = 0;
548
549 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000550 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
551 if (ae) {
552 while (ai != ae) {
553 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000554
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000555 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
556 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000557
Bill Wendlinged42e792012-10-10 06:13:42 +0000558 do {
559 switch (intrinsic.ArgumentAttributes[ai].second) {
560 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000561 if (addComma)
562 OS << ",";
563 OS << "Attribute::NoCapture";
564 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000565 break;
Hal Finkel47646c02016-07-11 01:28:42 +0000566 case CodeGenIntrinsic::Returned:
567 if (addComma)
568 OS << ",";
569 OS << "Attribute::Returned";
570 addComma = true;
571 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000572 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000573 if (addComma)
574 OS << ",";
575 OS << "Attribute::ReadOnly";
576 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000577 break;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000578 case CodeGenIntrinsic::WriteOnly:
579 if (addComma)
580 OS << ",";
581 OS << "Attribute::WriteOnly";
582 addComma = true;
583 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000584 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000585 if (addComma)
586 OS << ",";
Eric Christopher10f5d602015-07-30 21:16:34 +0000587 OS << "Attribute::ReadNone";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000588 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000589 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000590 }
John McCall375dcc92011-05-28 06:31:34 +0000591
Bill Wendlinged42e792012-10-10 06:13:42 +0000592 ++ai;
593 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000594 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000595 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000596 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000597 }
John McCall375dcc92011-05-28 06:31:34 +0000598 }
599
Igor Laevsky30143ae2015-08-13 17:40:04 +0000600 if (!intrinsic.canThrow ||
601 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
602 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
603 intrinsic.isConvergent) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000604 OS << " const Attribute::AttrKind Atts[] = {";
605 bool addComma = false;
606 if (!intrinsic.canThrow) {
607 OS << "Attribute::NoUnwind";
608 addComma = true;
609 }
610 if (intrinsic.isNoReturn) {
611 if (addComma)
612 OS << ",";
613 OS << "Attribute::NoReturn";
614 addComma = true;
615 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000616 if (intrinsic.isNoDuplicate) {
617 if (addComma)
618 OS << ",";
619 OS << "Attribute::NoDuplicate";
620 addComma = true;
621 }
Owen Anderson85fa7d52015-05-26 23:48:40 +0000622 if (intrinsic.isConvergent) {
623 if (addComma)
624 OS << ",";
625 OS << "Attribute::Convergent";
626 addComma = true;
627 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000628
Igor Laevsky30143ae2015-08-13 17:40:04 +0000629 switch (intrinsic.ModRef) {
630 case CodeGenIntrinsic::NoMem:
631 if (addComma)
632 OS << ",";
633 OS << "Attribute::ReadNone";
634 break;
635 case CodeGenIntrinsic::ReadArgMem:
636 if (addComma)
637 OS << ",";
638 OS << "Attribute::ReadOnly,";
639 OS << "Attribute::ArgMemOnly";
640 break;
641 case CodeGenIntrinsic::ReadMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000642 if (addComma)
643 OS << ",";
644 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000645 break;
Nicolai Haehnleb48275f2016-04-19 21:58:33 +0000646 case CodeGenIntrinsic::WriteArgMem:
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000647 if (addComma)
648 OS << ",";
649 OS << "Attribute::WriteOnly,";
650 OS << "Attribute::ArgMemOnly";
651 break;
652 case CodeGenIntrinsic::WriteMem:
653 if (addComma)
654 OS << ",";
655 OS << "Attribute::WriteOnly";
656 break;
Igor Laevsky30143ae2015-08-13 17:40:04 +0000657 case CodeGenIntrinsic::ReadWriteArgMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000658 if (addComma)
659 OS << ",";
Igor Laevsky30143ae2015-08-13 17:40:04 +0000660 OS << "Attribute::ArgMemOnly";
661 break;
662 case CodeGenIntrinsic::ReadWriteMem:
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000663 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000664 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000665 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000666 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000667 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000668 }
John McCall375dcc92011-05-28 06:31:34 +0000669
670 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000671 OS << " NumAttrs = " << numAttrs << ";\n";
672 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000673 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000674 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000675 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000676 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000677 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000678 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000679
Craig Topper374f19c2012-04-13 06:14:57 +0000680 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000681 OS << " }\n";
Craig Toppere1d12942014-08-27 05:25:25 +0000682 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000683 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000684 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000685}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000686
Reid Kleckner5b463712016-01-27 01:43:12 +0000687void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
Justin Bogner92a8c612016-07-15 16:31:37 +0000688 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
Reid Kleckner5b463712016-01-27 01:43:12 +0000689 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
690 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000691 BIMTy BuiltinMap;
Reid Kleckner5b463712016-01-27 01:43:12 +0000692 StringToOffsetTable Table;
Chris Lattner402a5732006-03-15 01:33:26 +0000693 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Reid Kleckner5b463712016-01-27 01:43:12 +0000694 const std::string &BuiltinName =
695 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
696 if (!BuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000697 // Get the map for this target prefix.
Reid Kleckner5b463712016-01-27 01:43:12 +0000698 std::map<std::string, std::string> &BIM =
699 BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000700
Reid Kleckner5b463712016-01-27 01:43:12 +0000701 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000702 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
Reid Kleckner5b463712016-01-27 01:43:12 +0000703 "': duplicate " + CompilerName + " builtin name!");
704 Table.GetOrAddStringOffset(BuiltinName);
Chris Lattner402a5732006-03-15 01:33:26 +0000705 }
706 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000707
Reid Kleckner5b463712016-01-27 01:43:12 +0000708 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
709 OS << "// This is used by the C front-end. The builtin name is passed\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000710 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
711 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000712 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000713
Dale Johannesenb842d522009-02-05 01:49:45 +0000714 if (TargetOnly) {
715 OS << "static " << TargetPrefix << "Intrinsic::ID "
Reid Kleckner5b463712016-01-27 01:43:12 +0000716 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000717 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000718 } else {
Reid Kleckner5b463712016-01-27 01:43:12 +0000719 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
720 << "Builtin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000721 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000722 }
Reid Kleckner5b463712016-01-27 01:43:12 +0000723 OS << " static const char BuiltinNames[] = {\n";
724 Table.EmitCharArray(OS);
725 OS << " };\n\n";
726
727 OS << " struct BuiltinEntry {\n";
728 OS << " Intrinsic::ID IntrinID;\n";
729 OS << " unsigned StrTabOffset;\n";
730 OS << " const char *getName() const {\n";
731 OS << " return &BuiltinNames[StrTabOffset];\n";
732 OS << " }\n";
733 OS << " bool operator<(const char *RHS) const {\n";
734 OS << " return strcmp(getName(), RHS) < 0;\n";
735 OS << " }\n";
736 OS << " };\n";
737
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000738
Chris Lattner497d13e2010-09-06 03:14:45 +0000739 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
740 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000741
Chris Lattner402a5732006-03-15 01:33:26 +0000742 // Note: this could emit significantly better code if we cared.
743 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000744 OS << " ";
745 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000746 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000747 else
748 OS << "/* Target Independent Builtins */ ";
749 OS << "{\n";
750
Chris Lattner91678fc2008-01-02 21:24:22 +0000751 // Emit the comparisons for this target prefix.
Reid Kleckner5b463712016-01-27 01:43:12 +0000752 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
753 for (const auto &P : I->second) {
754 OS << " {Intrinsic::" << P.second << ", "
755 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
756 }
757 OS << " };\n";
758 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
759 OS << " std::end(" << I->first << "Names),\n";
760 OS << " BuiltinNameStr);\n";
761 OS << " if (I != std::end(" << I->first << "Names) &&\n";
762 OS << " strcmp(I->getName(), BuiltinNameStr) == 0)\n";
763 OS << " return I->IntrinID;\n";
Chris Lattner91678fc2008-01-02 21:24:22 +0000764 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000765 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000766 OS << " return ";
767 if (!TargetPrefix.empty())
768 OS << "(" << TargetPrefix << "Intrinsic::ID)";
769 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000770 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000771 OS << "#endif\n\n";
772}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000773
Richard Smith6bc9df32014-04-20 20:26:39 +0000774void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000775 IntrinsicEmitter(RK, TargetOnly).run(OS);
776}