blob: 33256ccba46c2f1747ab5e390bd558e2d89aa27c [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,
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000216 IIT_PTR_TO_ELT = 33,
217 IIT_VEC_OF_PTRS_TO_ELT = 34,
218 IIT_I128 = 35,
219 IIT_V512 = 36,
220 IIT_V1024 = 37
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000221};
222
Chris Lattner827b2532012-05-17 04:30:58 +0000223
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000224static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000225 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000226 if (MVT(VT).isInteger()) {
227 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000228 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000229 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000230 case 1: return Sig.push_back(IIT_I1);
231 case 8: return Sig.push_back(IIT_I8);
232 case 16: return Sig.push_back(IIT_I16);
233 case 32: return Sig.push_back(IIT_I32);
234 case 64: return Sig.push_back(IIT_I64);
Kit Barton66460332015-05-25 15:49:26 +0000235 case 128: return Sig.push_back(IIT_I128);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000236 }
237 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000238
Chris Lattner827b2532012-05-17 04:30:58 +0000239 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000240 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000241 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000242 case MVT::f32: return Sig.push_back(IIT_F32);
243 case MVT::f64: return Sig.push_back(IIT_F64);
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000244 case MVT::token: return Sig.push_back(IIT_TOKEN);
Chris Lattner827b2532012-05-17 04:30:58 +0000245 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
246 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
247 // MVT::OtherVT is used to mean the empty struct type here.
248 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000249 // MVT::isVoid is used to represent varargs here.
250 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000251 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000252}
253
Nico Weber262a1332016-10-25 17:46:29 +0000254#if defined(_MSC_VER) && !defined(__clang__)
255#pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
256#endif
257
Chris Lattnerc4644162012-05-27 16:39:08 +0000258static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000259 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000260
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000261 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000262 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000263 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000264 if (R->isSubClassOf("LLVMExtendedType"))
265 Sig.push_back(IIT_EXTEND_ARG);
266 else if (R->isSubClassOf("LLVMTruncatedType"))
267 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000268 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
269 Sig.push_back(IIT_HALF_VEC_ARG);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000270 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
271 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000272 Sig.push_back((Number << 3) | ArgCodes[Number]);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000273 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
274 EncodeFixedValueType(VT, Sig);
275 return;
276 }
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000277 else if (R->isSubClassOf("LLVMPointerTo"))
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000278 Sig.push_back(IIT_PTR_TO_ARG);
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000279 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
280 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
Elena Demikhovskycaaceef2016-11-03 03:23:55 +0000281 else if (R->isSubClassOf("LLVMPointerToElt"))
282 Sig.push_back(IIT_PTR_TO_ELT);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000283 else
284 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000285 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000286 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000287
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000288 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000289
Chris Lattnerc4644162012-05-27 16:39:08 +0000290 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000291 switch (VT) {
292 default: break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000293 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
294 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
295 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
296 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000297 case MVT::Any: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000298 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000299 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000300 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000301
Chris Lattnerc4644162012-05-27 16:39:08 +0000302 // Figure out what arg # this is consuming, and remember what kind it was.
303 unsigned ArgNo = ArgCodes.size();
304 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000305
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000306 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
307 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerc4644162012-05-27 16:39:08 +0000308 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000309
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000310 case MVT::iPTR: {
311 unsigned AddrSpace = 0;
312 if (R->isSubClassOf("LLVMQualPointerType")) {
313 AddrSpace = R->getValueAsInt("AddrSpace");
314 assert(AddrSpace < 256 && "Address space exceeds 255");
315 }
316 if (AddrSpace) {
317 Sig.push_back(IIT_ANYPTR);
318 Sig.push_back(AddrSpace);
319 } else {
320 Sig.push_back(IIT_PTR);
321 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000322 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000323 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000324 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000325
Craig Topper8561de92014-01-24 20:50:47 +0000326 if (MVT(VT).isVector()) {
327 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000328 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000329 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000330 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000331 case 2: Sig.push_back(IIT_V2); break;
332 case 4: Sig.push_back(IIT_V4); break;
333 case 8: Sig.push_back(IIT_V8); break;
334 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000335 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000336 case 64: Sig.push_back(IIT_V64); break;
Krzysztof Parzyszekb8bb90b2015-11-24 16:28:14 +0000337 case 512: Sig.push_back(IIT_V512); break;
338 case 1024: Sig.push_back(IIT_V1024); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000339 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000340
Craig Topper8561de92014-01-24 20:50:47 +0000341 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000342 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000343
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000344 EncodeFixedValueType(VT, Sig);
345}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000346
Nico Weber262a1332016-10-25 17:46:29 +0000347#if defined(_MSC_VER) && !defined(__clang__)
348#pragma optimize("",on)
349#endif
350
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000351/// ComputeFixedEncoding - If we can encode the type signature for this
352/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000353static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
354 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000355 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000356
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000357 if (Int.IS.RetVTs.empty())
358 TypeSig.push_back(IIT_Done);
359 else if (Int.IS.RetVTs.size() == 1 &&
360 Int.IS.RetVTs[0] == MVT::isVoid)
361 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000362 else {
363 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000364 case 1: break;
365 case 2: TypeSig.push_back(IIT_STRUCT2); break;
366 case 3: TypeSig.push_back(IIT_STRUCT3); break;
367 case 4: TypeSig.push_back(IIT_STRUCT4); break;
368 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000369 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000370 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000371
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000372 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000373 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000374 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000375
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000376 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000377 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000378}
379
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000380static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000381 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000382}
383
Justin Bogner92a8c612016-07-15 16:31:37 +0000384void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000385 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000386 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
387 // capture it in this vector, otherwise store a ~0U.
388 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000389
Chris Lattnera3b0f522012-05-17 15:55:41 +0000390 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000391
Chris Lattnera3b0f522012-05-17 15:55:41 +0000392 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000393
Jim Laskey2682ea62007-02-07 20:38:26 +0000394 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000395 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000396 // Get the signature for the intrinsic.
397 TypeSig.clear();
398 ComputeFixedEncoding(Ints[i], TypeSig);
399
400 // Check to see if we can encode it into a 32-bit word. We can only encode
401 // 8 nibbles into a 32-bit word.
402 if (TypeSig.size() <= 8) {
403 bool Failed = false;
404 unsigned Result = 0;
405 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
406 // If we had an unencodable argument, bail out.
407 if (TypeSig[i] > 15) {
408 Failed = true;
409 break;
410 }
411 Result = (Result << 4) | TypeSig[e-i-1];
412 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000413
Chris Lattnera3b0f522012-05-17 15:55:41 +0000414 // If this could be encoded into a 31-bit word, return it.
415 if (!Failed && (Result >> 31) == 0) {
416 FixedEncodings.push_back(Result);
417 continue;
418 }
419 }
420
421 // Otherwise, we're going to unique the sequence into the
422 // LongEncodingTable, and use its offset in the 32-bit table instead.
423 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000424
Chris Lattnera3b0f522012-05-17 15:55:41 +0000425 // This is a placehold that we'll replace after the table is laid out.
426 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000427 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000428
Chris Lattnera3b0f522012-05-17 15:55:41 +0000429 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000430
Chris Lattnerf39c2782012-05-27 18:28:35 +0000431 OS << "// Global intrinsic function declaration type table.\n";
432 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
433
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000434 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000435
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000436 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
437 if ((i & 7) == 7)
438 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000439
Chris Lattnera3b0f522012-05-17 15:55:41 +0000440 // If the entry fit in the table, just emit it.
441 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000442 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000443 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000444 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000445
Chris Lattnera3b0f522012-05-17 15:55:41 +0000446 TypeSig.clear();
447 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000448
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000449
Chris Lattnera3b0f522012-05-17 15:55:41 +0000450 // Otherwise, emit the offset into the long encoding table. We emit it this
451 // way so that it is easier to read the offset in the .def file.
452 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000453 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000454
Chris Lattnera3b0f522012-05-17 15:55:41 +0000455 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000456
Chris Lattnera3b0f522012-05-17 15:55:41 +0000457 // Emit the shared table of register lists.
458 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
459 if (!LongEncodingTable.empty())
460 LongEncodingTable.emit(OS, printIITEntry);
461 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000462
Patrik Hägglundca210d82012-05-23 12:34:56 +0000463 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000464}
465
Richard Smith6bc9df32014-04-20 20:26:39 +0000466namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000467struct AttributeComparator {
468 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
469 // Sort throwing intrinsics after non-throwing intrinsics.
470 if (L->canThrow != R->canThrow)
471 return R->canThrow;
472
Eli Bendersky2281ef92014-03-18 23:51:07 +0000473 if (L->isNoDuplicate != R->isNoDuplicate)
474 return R->isNoDuplicate;
475
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000476 if (L->isNoReturn != R->isNoReturn)
477 return R->isNoReturn;
478
Owen Anderson85fa7d52015-05-26 23:48:40 +0000479 if (L->isConvergent != R->isConvergent)
480 return R->isConvergent;
481
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000482 // Try to order by readonly/readnone attribute.
Nicolai Haehnleb48275f2016-04-19 21:58:33 +0000483 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
484 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000485 if (LK != RK) return (LK > RK);
486
487 // Order by argument attributes.
488 // This is reliable because each side is already sorted internally.
489 return (L->ArgumentAttributes < R->ArgumentAttributes);
490 }
491};
492} // End anonymous namespace
493
Chris Lattner49b7ee12009-01-12 01:18:58 +0000494/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Justin Bogner92a8c612016-07-15 16:31:37 +0000495void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
496 raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000497 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
498 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000499 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000500 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000501 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000502 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000503 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000504
Craig Topperccd651c2012-02-28 06:32:00 +0000505 // Compute the maximum number of attribute arguments and the map
506 typedef std::map<const CodeGenIntrinsic*, unsigned,
507 AttributeComparator> UniqAttrMapTy;
508 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000509 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000510 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000511 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000512 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000513 maxArgAttrs =
514 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000515 unsigned &N = UniqAttributes[&intrinsic];
516 if (N) continue;
517 assert(AttrNum < 256 && "Too many unique attributes for table!");
518 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000519 }
John McCall375dcc92011-05-28 06:31:34 +0000520
Bill Wendling4d3491c2013-01-27 03:25:05 +0000521 // Emit an array of AttributeSet. Most intrinsics will have at least one
522 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000523 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000524
525 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
526 const CodeGenIntrinsic &intrinsic = Ints[i];
527
528 OS << " " << UniqAttributes[&intrinsic] << ", // "
529 << intrinsic.Name << "\n";
530 }
531 OS << " };\n\n";
532
Bill Wendling4d3491c2013-01-27 03:25:05 +0000533 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000534 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000535 OS << " if (id != 0) {\n";
536 OS << " switch(IntrinsicsToAttributesMap[id - ";
537 if (TargetOnly)
538 OS << "Intrinsic::num_intrinsics";
539 else
540 OS << "1";
541 OS << "]) {\n";
542 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000543 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
544 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000545 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000546
Craig Topperccd651c2012-02-28 06:32:00 +0000547 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000548
549 // Keep track of the number of attributes we're writing out.
550 unsigned numAttrs = 0;
551
552 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000553 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
554 if (ae) {
555 while (ai != ae) {
556 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000557
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000558 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
559 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000560
Bill Wendlinged42e792012-10-10 06:13:42 +0000561 do {
562 switch (intrinsic.ArgumentAttributes[ai].second) {
563 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000564 if (addComma)
565 OS << ",";
566 OS << "Attribute::NoCapture";
567 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000568 break;
Hal Finkel47646c02016-07-11 01:28:42 +0000569 case CodeGenIntrinsic::Returned:
570 if (addComma)
571 OS << ",";
572 OS << "Attribute::Returned";
573 addComma = true;
574 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000575 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000576 if (addComma)
577 OS << ",";
578 OS << "Attribute::ReadOnly";
579 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000580 break;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000581 case CodeGenIntrinsic::WriteOnly:
582 if (addComma)
583 OS << ",";
584 OS << "Attribute::WriteOnly";
585 addComma = true;
586 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000587 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000588 if (addComma)
589 OS << ",";
Eric Christopher10f5d602015-07-30 21:16:34 +0000590 OS << "Attribute::ReadNone";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000591 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000592 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000593 }
John McCall375dcc92011-05-28 06:31:34 +0000594
Bill Wendlinged42e792012-10-10 06:13:42 +0000595 ++ai;
596 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000597 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000598 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000599 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000600 }
John McCall375dcc92011-05-28 06:31:34 +0000601 }
602
Igor Laevsky30143ae2015-08-13 17:40:04 +0000603 if (!intrinsic.canThrow ||
604 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
605 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
606 intrinsic.isConvergent) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000607 OS << " const Attribute::AttrKind Atts[] = {";
608 bool addComma = false;
609 if (!intrinsic.canThrow) {
610 OS << "Attribute::NoUnwind";
611 addComma = true;
612 }
613 if (intrinsic.isNoReturn) {
614 if (addComma)
615 OS << ",";
616 OS << "Attribute::NoReturn";
617 addComma = true;
618 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000619 if (intrinsic.isNoDuplicate) {
620 if (addComma)
621 OS << ",";
622 OS << "Attribute::NoDuplicate";
623 addComma = true;
624 }
Owen Anderson85fa7d52015-05-26 23:48:40 +0000625 if (intrinsic.isConvergent) {
626 if (addComma)
627 OS << ",";
628 OS << "Attribute::Convergent";
629 addComma = true;
630 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000631
Igor Laevsky30143ae2015-08-13 17:40:04 +0000632 switch (intrinsic.ModRef) {
633 case CodeGenIntrinsic::NoMem:
634 if (addComma)
635 OS << ",";
636 OS << "Attribute::ReadNone";
637 break;
638 case CodeGenIntrinsic::ReadArgMem:
639 if (addComma)
640 OS << ",";
641 OS << "Attribute::ReadOnly,";
642 OS << "Attribute::ArgMemOnly";
643 break;
644 case CodeGenIntrinsic::ReadMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000645 if (addComma)
646 OS << ",";
647 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000648 break;
Andrew Kaylor57d35bf2016-11-22 19:16:04 +0000649 case CodeGenIntrinsic::ReadInaccessibleMem:
650 if (addComma)
651 OS << ",";
652 OS << "Attribute::ReadOnly,";
653 OS << "Attribute::InaccessibleMemOnly";
654 break;
655 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
656 if (addComma)
657 OS << ",";
658 OS << "Attribute::ReadOnly,";
659 OS << "Attribute::InaccessibleMemOrArgMemOnly";
660 break;
Nicolai Haehnleb48275f2016-04-19 21:58:33 +0000661 case CodeGenIntrinsic::WriteArgMem:
Nicolai Haehnle84c9f992016-07-04 08:01:29 +0000662 if (addComma)
663 OS << ",";
664 OS << "Attribute::WriteOnly,";
665 OS << "Attribute::ArgMemOnly";
666 break;
667 case CodeGenIntrinsic::WriteMem:
668 if (addComma)
669 OS << ",";
670 OS << "Attribute::WriteOnly";
671 break;
Andrew Kaylor57d35bf2016-11-22 19:16:04 +0000672 case CodeGenIntrinsic::WriteInaccessibleMem:
673 if (addComma)
674 OS << ",";
675 OS << "Attribute::WriteOnly,";
676 OS << "Attribute::InaccessibleMemOnly";
677 break;
678 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
679 if (addComma)
680 OS << ",";
681 OS << "Attribute::WriteOnly,";
682 OS << "Attribute::InaccessibleMemOrArgOnly";
683 break;
Igor Laevsky30143ae2015-08-13 17:40:04 +0000684 case CodeGenIntrinsic::ReadWriteArgMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000685 if (addComma)
686 OS << ",";
Igor Laevsky30143ae2015-08-13 17:40:04 +0000687 OS << "Attribute::ArgMemOnly";
688 break;
Andrew Kaylor57d35bf2016-11-22 19:16:04 +0000689 case CodeGenIntrinsic::ReadWriteInaccessibleMem:
690 if (addComma)
691 OS << ",";
692 OS << "Attribute::InaccessibleMemOnly";
693 break;
694 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
695 if (addComma)
696 OS << ",";
697 OS << "Attribute::InaccessibleMemOrArgMemOnly";
Igor Laevsky30143ae2015-08-13 17:40:04 +0000698 case CodeGenIntrinsic::ReadWriteMem:
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000699 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000700 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000701 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000702 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000703 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000704 }
John McCall375dcc92011-05-28 06:31:34 +0000705
706 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000707 OS << " NumAttrs = " << numAttrs << ";\n";
708 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000709 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000710 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000711 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000712 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000713 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000714 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000715
Craig Topper374f19c2012-04-13 06:14:57 +0000716 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000717 OS << " }\n";
Craig Toppere1d12942014-08-27 05:25:25 +0000718 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000719 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000720 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000721}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000722
Reid Kleckner5b463712016-01-27 01:43:12 +0000723void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
Justin Bogner92a8c612016-07-15 16:31:37 +0000724 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
Reid Kleckner5b463712016-01-27 01:43:12 +0000725 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
726 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000727 BIMTy BuiltinMap;
Reid Kleckner5b463712016-01-27 01:43:12 +0000728 StringToOffsetTable Table;
Chris Lattner402a5732006-03-15 01:33:26 +0000729 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Reid Kleckner5b463712016-01-27 01:43:12 +0000730 const std::string &BuiltinName =
731 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
732 if (!BuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000733 // Get the map for this target prefix.
Reid Kleckner5b463712016-01-27 01:43:12 +0000734 std::map<std::string, std::string> &BIM =
735 BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000736
Reid Kleckner5b463712016-01-27 01:43:12 +0000737 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000738 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
Reid Kleckner5b463712016-01-27 01:43:12 +0000739 "': duplicate " + CompilerName + " builtin name!");
740 Table.GetOrAddStringOffset(BuiltinName);
Chris Lattner402a5732006-03-15 01:33:26 +0000741 }
742 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000743
Reid Kleckner5b463712016-01-27 01:43:12 +0000744 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
745 OS << "// This is used by the C front-end. The builtin name is passed\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000746 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
747 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000748 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000749
Dale Johannesenb842d522009-02-05 01:49:45 +0000750 if (TargetOnly) {
751 OS << "static " << TargetPrefix << "Intrinsic::ID "
Reid Kleckner5b463712016-01-27 01:43:12 +0000752 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000753 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000754 } else {
Reid Kleckner5b463712016-01-27 01:43:12 +0000755 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
756 << "Builtin(const char "
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000757 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000758 }
Reid Kleckner5b463712016-01-27 01:43:12 +0000759 OS << " static const char BuiltinNames[] = {\n";
760 Table.EmitCharArray(OS);
761 OS << " };\n\n";
762
763 OS << " struct BuiltinEntry {\n";
764 OS << " Intrinsic::ID IntrinID;\n";
765 OS << " unsigned StrTabOffset;\n";
766 OS << " const char *getName() const {\n";
767 OS << " return &BuiltinNames[StrTabOffset];\n";
768 OS << " }\n";
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000769 OS << " bool operator<(StringRef RHS) const {\n";
770 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000771 OS << " }\n";
772 OS << " };\n";
773
Chris Lattner497d13e2010-09-06 03:14:45 +0000774 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000775
Chris Lattner402a5732006-03-15 01:33:26 +0000776 // Note: this could emit significantly better code if we cared.
777 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000778 OS << " ";
779 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000780 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000781 else
782 OS << "/* Target Independent Builtins */ ";
783 OS << "{\n";
784
Chris Lattner91678fc2008-01-02 21:24:22 +0000785 // Emit the comparisons for this target prefix.
Reid Kleckner5b463712016-01-27 01:43:12 +0000786 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
787 for (const auto &P : I->second) {
788 OS << " {Intrinsic::" << P.second << ", "
789 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
790 }
791 OS << " };\n";
792 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
793 OS << " std::end(" << I->first << "Names),\n";
794 OS << " BuiltinNameStr);\n";
795 OS << " if (I != std::end(" << I->first << "Names) &&\n";
Mehdi Aminif9ff04c2016-10-10 19:31:09 +0000796 OS << " I->getName() == BuiltinNameStr)\n";
Reid Kleckner5b463712016-01-27 01:43:12 +0000797 OS << " return I->IntrinID;\n";
Chris Lattner91678fc2008-01-02 21:24:22 +0000798 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000799 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000800 OS << " return ";
801 if (!TargetPrefix.empty())
802 OS << "(" << TargetPrefix << "Intrinsic::ID)";
803 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000804 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000805 OS << "#endif\n\n";
806}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000807
Richard Smith6bc9df32014-04-20 20:26:39 +0000808void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000809 IntrinsicEmitter(RK, TargetOnly).run(OS);
810}