blob: 049282e5ebfeed8d44beeb3e888703a83c24f18a [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Lattner9e493cf2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000014#include "CodeGenIntrinsics.h"
Chandler Carruth69940402007-08-04 01:51:18 +000015#include "CodeGenTarget.h"
Chris Lattner387c9dc2012-05-17 15:55:41 +000016#include "SequenceToOffsetTable.h"
Richard Smith8f590212014-04-20 20:26:39 +000017#include "TableGenBackends.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000018#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000019#include "llvm/TableGen/Error.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000020#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +000021#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000022#include "llvm/TableGen/TableGenBackend.h"
Reid Klecknerfa217412016-01-27 01:43:12 +000023#include "llvm/TableGen/StringToOffsetTable.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000024#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000025using namespace llvm;
26
Jakob Stoklund Olesen6f36fa92012-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
Reid Kleckneraf7c4452018-06-23 02:02:38 +000037 void run(raw_ostream &OS, bool Enums);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000038
39 void EmitPrefix(raw_ostream &OS);
40
Justin Bognera3d02c72016-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 Olesen6f36fa92012-06-11 15:37:55 +000044 raw_ostream &OS);
Justin Bognera3d02c72016-07-15 16:31:37 +000045 void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints,
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000046 raw_ostream &OS);
Justin Bognera3d02c72016-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 Olesen6f36fa92012-06-11 15:37:55 +000051 void EmitSuffix(raw_ostream &OS);
52};
53} // End anonymous namespace
54
Chris Lattner9e493cf2006-03-03 02:32:46 +000055//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000056// IntrinsicEmitter Implementation
57//===----------------------------------------------------------------------===//
58
Reid Kleckneraf7c4452018-06-23 02:02:38 +000059void IntrinsicEmitter::run(raw_ostream &OS, bool Enums) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000060 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
61
Justin Bognera3d02c72016-07-15 16:31:37 +000062 CodeGenIntrinsicTable Ints(Records, TargetOnly);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000063
Dale Johannesen49de9822009-02-05 01:49:45 +000064 if (TargetOnly && !Ints.empty())
65 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattner9e493cf2006-03-03 02:32:46 +000066
Douglas Gregor7d9663c2010-05-11 06:17:44 +000067 EmitPrefix(OS);
68
Reid Kleckneraf7c4452018-06-23 02:02:38 +000069 if (Enums) {
70 // Emit the enum information.
71 EmitEnumInfo(Ints, OS);
72 } else {
73 // Emit the target metadata.
74 EmitTargetInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000075
Reid Kleckneraf7c4452018-06-23 02:02:38 +000076 // Emit the intrinsic ID -> name table.
77 EmitIntrinsicToNameTable(Ints, OS);
Justin Bognera3d02c72016-07-15 16:31:37 +000078
Reid Kleckneraf7c4452018-06-23 02:02:38 +000079 // Emit the intrinsic ID -> overload table.
80 EmitIntrinsicToOverloadTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000081
Reid Kleckneraf7c4452018-06-23 02:02:38 +000082 // Emit the intrinsic declaration generator.
83 EmitGenerator(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000084
Reid Kleckneraf7c4452018-06-23 02:02:38 +000085 // Emit the intrinsic parameter attributes.
86 EmitAttributes(Ints, OS);
Andrew Trickcf940ce2013-10-31 17:18:07 +000087
Reid Kleckneraf7c4452018-06-23 02:02:38 +000088 // Emit code to translate GCC builtins into LLVM intrinsics.
89 EmitIntrinsicToBuiltinMap(Ints, true, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000090
Reid Kleckneraf7c4452018-06-23 02:02:38 +000091 // Emit code to translate MS builtins into LLVM intrinsics.
92 EmitIntrinsicToBuiltinMap(Ints, false, OS);
93 }
Saleem Abdulrasoolbb5f6222014-07-04 18:42:25 +000094
Douglas Gregor7d9663c2010-05-11 06:17:44 +000095 EmitSuffix(OS);
96}
97
98void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
99 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +0000100 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
101 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +0000102 "# pragma push_macro(\"setjmp\")\n"
103 "# undef setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +0000104 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000105 "#endif\n\n";
106}
107
108void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencer1f409602010-09-24 19:48:47 +0000109 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000110 "// let's return it to _setjmp state\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +0000111 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +0000112 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000113 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000114}
115
Justin Bognera3d02c72016-07-15 16:31:37 +0000116void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000117 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +0000118 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000119 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
120 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
121 OS << " " << Ints[i].EnumName;
122 OS << ((i != e-1) ? ", " : " ");
Justin Holewinskidf50f412014-07-17 11:23:29 +0000123 if (Ints[i].EnumName.size() < 40)
124 OS << std::string(40-Ints[i].EnumName.size(), ' ');
125 OS << " // " << Ints[i].Name << "\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000126 }
127 OS << "#endif\n\n";
128}
Chris Lattner9b843b22006-03-09 20:34:19 +0000129
Justin Bognera3d02c72016-07-15 16:31:37 +0000130void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
131 raw_ostream &OS) {
132 OS << "// Target mapping\n";
133 OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n";
134 OS << "struct IntrinsicTargetInfo {\n"
Saleem Abdulrasool91f734b2017-01-31 00:45:01 +0000135 << " llvm::StringLiteral Name;\n"
Justin Bognera3d02c72016-07-15 16:31:37 +0000136 << " size_t Offset;\n"
137 << " size_t Count;\n"
138 << "};\n";
Benjamin Kramer794b7572017-01-30 18:49:24 +0000139 OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n";
Justin Bognera3d02c72016-07-15 16:31:37 +0000140 for (auto Target : Ints.Targets)
Saleem Abdulrasool91f734b2017-01-31 00:45:01 +0000141 OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset
Benjamin Kramer4337d892017-01-30 19:05:09 +0000142 << ", " << Target.Count << "},\n";
Justin Bognera3d02c72016-07-15 16:31:37 +0000143 OS << "};\n";
144 OS << "#endif\n\n";
145}
146
147void IntrinsicEmitter::EmitIntrinsicToNameTable(
148 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000149 OS << "// Intrinsic ID to name table\n";
150 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
151 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000152 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
153 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000154 OS << "#endif\n\n";
155}
156
Justin Bognera3d02c72016-07-15 16:31:37 +0000157void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
158 const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000159 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000160 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000161 OS << "static const uint8_t OTable[] = {\n";
162 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000163 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000164 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
165 if ((i+1)%8 == 0)
166 OS << ",\n 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000167 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000168 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000169 }
Benjamin Kramer36a21382012-03-01 02:16:57 +0000170 OS << "\n};\n\n";
171 // OTable contains a true bit at the position if the intrinsic is overloaded.
172 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000173 OS << "#endif\n\n";
174}
175
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000176
Gabor Buella7c84ee22018-04-30 10:18:11 +0000177// NOTE: This must be kept in synch with the copy in lib/IR/Function.cpp!
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000178enum IIT_Info {
Robert Khasanov10646db2014-10-20 19:25:05 +0000179 // Common values should be encoded with 0-15.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000180 IIT_Done = 0,
181 IIT_I1 = 1,
182 IIT_I8 = 2,
183 IIT_I16 = 3,
184 IIT_I32 = 4,
185 IIT_I64 = 5,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000186 IIT_F16 = 6,
187 IIT_F32 = 7,
188 IIT_F64 = 8,
189 IIT_V2 = 9,
190 IIT_V4 = 10,
191 IIT_V8 = 11,
192 IIT_V16 = 12,
193 IIT_V32 = 13,
Robert Khasanov10646db2014-10-20 19:25:05 +0000194 IIT_PTR = 14,
195 IIT_ARG = 15,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000196
Robert Khasanov10646db2014-10-20 19:25:05 +0000197 // Values from 16+ are only encodable with the inefficient encoding.
198 IIT_V64 = 16,
Robert Khasanovcfa57242014-09-30 11:32:22 +0000199 IIT_MMX = 17,
Joseph Tremoulet16c64792015-09-02 13:36:25 +0000200 IIT_TOKEN = 18,
201 IIT_METADATA = 19,
202 IIT_EMPTYSTRUCT = 20,
203 IIT_STRUCT2 = 21,
204 IIT_STRUCT3 = 22,
205 IIT_STRUCT4 = 23,
206 IIT_STRUCT5 = 24,
207 IIT_EXTEND_ARG = 25,
208 IIT_TRUNC_ARG = 26,
209 IIT_ANYPTR = 27,
210 IIT_V1 = 28,
211 IIT_VARARG = 29,
212 IIT_HALF_VEC_ARG = 30,
213 IIT_SAME_VEC_WIDTH_ARG = 31,
214 IIT_PTR_TO_ARG = 32,
Elena Demikhovsky872445f2016-11-03 03:23:55 +0000215 IIT_PTR_TO_ELT = 33,
Elad Cohenea59a242017-05-03 12:28:54 +0000216 IIT_VEC_OF_ANYPTRS_TO_ELT = 34,
Elena Demikhovsky872445f2016-11-03 03:23:55 +0000217 IIT_I128 = 35,
218 IIT_V512 = 36,
Artem Belevich3df0d5d2017-10-12 17:40:00 +0000219 IIT_V1024 = 37,
220 IIT_STRUCT6 = 38,
221 IIT_STRUCT7 = 39,
Stefan Pintilie3ea14f72018-07-09 18:50:06 +0000222 IIT_STRUCT8 = 40,
223 IIT_F128 = 41
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000224};
225
226static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000227 std::vector<unsigned char> &Sig) {
Craig Topperf3474542014-01-24 20:50:47 +0000228 if (MVT(VT).isInteger()) {
229 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000230 switch (BitWidth) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000231 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000232 case 1: return Sig.push_back(IIT_I1);
233 case 8: return Sig.push_back(IIT_I8);
234 case 16: return Sig.push_back(IIT_I16);
235 case 32: return Sig.push_back(IIT_I32);
236 case 64: return Sig.push_back(IIT_I64);
Kit Barton948ecae22015-05-25 15:49:26 +0000237 case 128: return Sig.push_back(IIT_I128);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000238 }
239 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000240
Chris Lattner46aaf692012-05-17 04:30:58 +0000241 switch (VT) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000242 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000243 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner46aaf692012-05-17 04:30:58 +0000244 case MVT::f32: return Sig.push_back(IIT_F32);
245 case MVT::f64: return Sig.push_back(IIT_F64);
Stefan Pintilie3ea14f72018-07-09 18:50:06 +0000246 case MVT::f128: return Sig.push_back(IIT_F128);
Joseph Tremoulet16c64792015-09-02 13:36:25 +0000247 case MVT::token: return Sig.push_back(IIT_TOKEN);
Chris Lattner46aaf692012-05-17 04:30:58 +0000248 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
249 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
250 // MVT::OtherVT is used to mean the empty struct type here.
251 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Trick2e50b8a2013-10-31 17:18:11 +0000252 // MVT::isVoid is used to represent varargs here.
253 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner46aaf692012-05-17 04:30:58 +0000254 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000255}
256
Nico Weberc31e4b52016-10-25 17:46:29 +0000257#if defined(_MSC_VER) && !defined(__clang__)
258#pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function.
259#endif
260
Chris Lattnerb4654c12012-05-27 16:39:08 +0000261static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000262 std::vector<unsigned char> &Sig) {
Andrew Trickcf940ce2013-10-31 17:18:07 +0000263
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000264 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000265 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerb4654c12012-05-27 16:39:08 +0000266 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoverb7de4282014-03-28 12:31:39 +0000267 if (R->isSubClassOf("LLVMExtendedType"))
268 Sig.push_back(IIT_EXTEND_ARG);
269 else if (R->isSubClassOf("LLVMTruncatedType"))
270 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover7c3e0572014-03-29 07:04:54 +0000271 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
272 Sig.push_back(IIT_HALF_VEC_ARG);
Elena Demikhovsky73ae1df2014-12-04 09:40:44 +0000273 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
274 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
Ramkumar Ramachandra230796b2015-01-22 20:14:38 +0000275 Sig.push_back((Number << 3) | ArgCodes[Number]);
Elena Demikhovsky73ae1df2014-12-04 09:40:44 +0000276 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
277 EncodeFixedValueType(VT, Sig);
278 return;
279 }
Elena Demikhovsky8be39c82015-02-08 08:27:19 +0000280 else if (R->isSubClassOf("LLVMPointerTo"))
Elena Demikhovskyb3132232014-12-25 07:49:20 +0000281 Sig.push_back(IIT_PTR_TO_ARG);
Elad Cohenea59a242017-05-03 12:28:54 +0000282 else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) {
283 Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT);
284 unsigned ArgNo = ArgCodes.size();
285 ArgCodes.push_back(3 /*vAny*/);
286 // Encode overloaded ArgNo
287 Sig.push_back(ArgNo);
288 // Encode LLVMMatchType<Number> ArgNo
289 Sig.push_back(Number);
290 return;
291 } else if (R->isSubClassOf("LLVMPointerToElt"))
Elena Demikhovsky872445f2016-11-03 03:23:55 +0000292 Sig.push_back(IIT_PTR_TO_ELT);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000293 else
294 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra230796b2015-01-22 20:14:38 +0000295 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000296 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000297
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000298 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner46aaf692012-05-17 04:30:58 +0000299
Chris Lattnerb4654c12012-05-27 16:39:08 +0000300 unsigned Tmp = 0;
Chris Lattnere82d5982012-05-26 23:03:52 +0000301 switch (VT) {
302 default: break;
Justin Bogner7d7a23e2016-08-17 20:30:52 +0000303 case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH;
304 case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH;
305 case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH;
306 case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH;
Ramkumar Ramachandra230796b2015-01-22 20:14:38 +0000307 case MVT::Any: {
Chris Lattnere82d5982012-05-26 23:03:52 +0000308 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickcf940ce2013-10-31 17:18:07 +0000309 // type in the list specified to getIntrinsic().
Chris Lattner46aaf692012-05-17 04:30:58 +0000310 Sig.push_back(IIT_ARG);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000311
Chris Lattnerb4654c12012-05-27 16:39:08 +0000312 // Figure out what arg # this is consuming, and remember what kind it was.
313 unsigned ArgNo = ArgCodes.size();
314 ArgCodes.push_back(Tmp);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000315
Ramkumar Ramachandra230796b2015-01-22 20:14:38 +0000316 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
317 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerb4654c12012-05-27 16:39:08 +0000318 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000319
Chris Lattnere82d5982012-05-26 23:03:52 +0000320 case MVT::iPTR: {
321 unsigned AddrSpace = 0;
322 if (R->isSubClassOf("LLVMQualPointerType")) {
323 AddrSpace = R->getValueAsInt("AddrSpace");
324 assert(AddrSpace < 256 && "Address space exceeds 255");
325 }
326 if (AddrSpace) {
327 Sig.push_back(IIT_ANYPTR);
328 Sig.push_back(AddrSpace);
329 } else {
330 Sig.push_back(IIT_PTR);
331 }
Chris Lattnerb4654c12012-05-27 16:39:08 +0000332 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnere82d5982012-05-26 23:03:52 +0000333 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000334 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000335
Craig Topperf3474542014-01-24 20:50:47 +0000336 if (MVT(VT).isVector()) {
337 MVT VVT = VT;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000338 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000339 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu477fc622013-09-24 02:47:27 +0000340 case 1: Sig.push_back(IIT_V1); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000341 case 2: Sig.push_back(IIT_V2); break;
342 case 4: Sig.push_back(IIT_V4); break;
343 case 8: Sig.push_back(IIT_V8); break;
344 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner46aaf692012-05-17 04:30:58 +0000345 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovcfa57242014-09-30 11:32:22 +0000346 case 64: Sig.push_back(IIT_V64); break;
Krzysztof Parzyszek05ee0ec2015-11-24 16:28:14 +0000347 case 512: Sig.push_back(IIT_V512); break;
348 case 1024: Sig.push_back(IIT_V1024); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000349 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000350
Craig Topperf3474542014-01-24 20:50:47 +0000351 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000352 }
Chris Lattnere82d5982012-05-26 23:03:52 +0000353
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000354 EncodeFixedValueType(VT, Sig);
355}
Francois Pichete4807c12012-05-17 04:00:03 +0000356
Nico Weberc31e4b52016-10-25 17:46:29 +0000357#if defined(_MSC_VER) && !defined(__clang__)
358#pragma optimize("",on)
359#endif
360
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000361/// ComputeFixedEncoding - If we can encode the type signature for this
362/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattner387c9dc2012-05-17 15:55:41 +0000363static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
364 std::vector<unsigned char> &TypeSig) {
Chris Lattnerb4654c12012-05-27 16:39:08 +0000365 std::vector<unsigned char> ArgCodes;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000366
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000367 if (Int.IS.RetVTs.empty())
368 TypeSig.push_back(IIT_Done);
369 else if (Int.IS.RetVTs.size() == 1 &&
370 Int.IS.RetVTs[0] == MVT::isVoid)
371 TypeSig.push_back(IIT_Done);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000372 else {
373 switch (Int.IS.RetVTs.size()) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000374 case 1: break;
375 case 2: TypeSig.push_back(IIT_STRUCT2); break;
376 case 3: TypeSig.push_back(IIT_STRUCT3); break;
377 case 4: TypeSig.push_back(IIT_STRUCT4); break;
378 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Artem Belevich3df0d5d2017-10-12 17:40:00 +0000379 case 6: TypeSig.push_back(IIT_STRUCT6); break;
380 case 7: TypeSig.push_back(IIT_STRUCT7); break;
381 case 8: TypeSig.push_back(IIT_STRUCT8); break;
Craig Topper10d664f2014-06-18 05:05:13 +0000382 default: llvm_unreachable("Unhandled case in struct");
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000383 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000384
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000385 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000386 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000387 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000388
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000389 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000390 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner387c9dc2012-05-17 15:55:41 +0000391}
392
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000393static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000394 OS << (unsigned)X;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000395}
396
Justin Bognera3d02c72016-07-15 16:31:37 +0000397void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000398 raw_ostream &OS) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000399 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
400 // capture it in this vector, otherwise store a ~0U.
401 std::vector<unsigned> FixedEncodings;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000402
Chris Lattner387c9dc2012-05-17 15:55:41 +0000403 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000404
Chris Lattner387c9dc2012-05-17 15:55:41 +0000405 std::vector<unsigned char> TypeSig;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000406
Jim Laskey95af5922007-02-07 20:38:26 +0000407 // Compute the unique argument type info.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000408 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000409 // Get the signature for the intrinsic.
410 TypeSig.clear();
411 ComputeFixedEncoding(Ints[i], TypeSig);
412
413 // Check to see if we can encode it into a 32-bit word. We can only encode
414 // 8 nibbles into a 32-bit word.
415 if (TypeSig.size() <= 8) {
416 bool Failed = false;
417 unsigned Result = 0;
418 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
419 // If we had an unencodable argument, bail out.
420 if (TypeSig[i] > 15) {
421 Failed = true;
422 break;
423 }
424 Result = (Result << 4) | TypeSig[e-i-1];
425 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000426
Chris Lattner387c9dc2012-05-17 15:55:41 +0000427 // If this could be encoded into a 31-bit word, return it.
428 if (!Failed && (Result >> 31) == 0) {
429 FixedEncodings.push_back(Result);
430 continue;
431 }
432 }
433
434 // Otherwise, we're going to unique the sequence into the
435 // LongEncodingTable, and use its offset in the 32-bit table instead.
436 LongEncodingTable.add(TypeSig);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000437
Chris Lattner387c9dc2012-05-17 15:55:41 +0000438 // This is a placehold that we'll replace after the table is laid out.
439 FixedEncodings.push_back(~0U);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000440 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000441
Chris Lattner387c9dc2012-05-17 15:55:41 +0000442 LongEncodingTable.layout();
Andrew Trickcf940ce2013-10-31 17:18:07 +0000443
Chris Lattner908a8312012-05-27 18:28:35 +0000444 OS << "// Global intrinsic function declaration type table.\n";
445 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
446
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000447 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000448
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000449 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
450 if ((i & 7) == 7)
451 OS << "\n ";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000452
Chris Lattner387c9dc2012-05-17 15:55:41 +0000453 // If the entry fit in the table, just emit it.
454 if (FixedEncodings[i] != ~0U) {
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000455 OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000456 continue;
Jim Laskey95af5922007-02-07 20:38:26 +0000457 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000458
Chris Lattner387c9dc2012-05-17 15:55:41 +0000459 TypeSig.clear();
460 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000461
Andrew Trickcf940ce2013-10-31 17:18:07 +0000462
Chris Lattner387c9dc2012-05-17 15:55:41 +0000463 // Otherwise, emit the offset into the long encoding table. We emit it this
464 // way so that it is easier to read the offset in the .def file.
465 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey95af5922007-02-07 20:38:26 +0000466 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000467
Chris Lattner387c9dc2012-05-17 15:55:41 +0000468 OS << "0\n};\n\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000469
Chris Lattner387c9dc2012-05-17 15:55:41 +0000470 // Emit the shared table of register lists.
471 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
472 if (!LongEncodingTable.empty())
473 LongEncodingTable.emit(OS, printIITEntry);
474 OS << " 255\n};\n\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000475
Patrik Hägglund9ce6f6f2012-05-23 12:34:56 +0000476 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey95af5922007-02-07 20:38:26 +0000477}
478
Richard Smith8f590212014-04-20 20:26:39 +0000479namespace {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000480struct AttributeComparator {
481 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
482 // Sort throwing intrinsics after non-throwing intrinsics.
483 if (L->canThrow != R->canThrow)
484 return R->canThrow;
485
Eli Bendersky21354ec2014-03-18 23:51:07 +0000486 if (L->isNoDuplicate != R->isNoDuplicate)
487 return R->isNoDuplicate;
488
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000489 if (L->isNoReturn != R->isNoReturn)
490 return R->isNoReturn;
491
Vedant Kumar3d60ff72018-11-14 19:53:41 +0000492 if (L->isCold != R->isCold)
493 return R->isCold;
494
Owen Anderson13146c72015-05-26 23:48:40 +0000495 if (L->isConvergent != R->isConvergent)
496 return R->isConvergent;
497
Matt Arsenaultea376da2017-04-28 20:25:27 +0000498 if (L->isSpeculatable != R->isSpeculatable)
499 return R->isSpeculatable;
500
Matt Arsenault8c9ed242017-04-28 21:01:46 +0000501 if (L->hasSideEffects != R->hasSideEffects)
502 return R->hasSideEffects;
503
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000504 // Try to order by readonly/readnone attribute.
Nicolai Haehnle318d6a22016-04-19 21:58:33 +0000505 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
506 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000507 if (LK != RK) return (LK > RK);
508
509 // Order by argument attributes.
510 // This is reliable because each side is already sorted internally.
511 return (L->ArgumentAttributes < R->ArgumentAttributes);
512 }
513};
514} // End anonymous namespace
515
Chris Lattner048ffb22009-01-12 01:18:58 +0000516/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Justin Bognera3d02c72016-07-15 16:31:37 +0000517void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
518 raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000519 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
520 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000521 if (TargetOnly)
Reid Kleckner67077702017-03-21 16:57:19 +0000522 OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000523 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000524 else
Reid Kleckner67077702017-03-21 16:57:19 +0000525 OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000526
Craig Topper1f595232012-02-28 06:32:00 +0000527 // Compute the maximum number of attribute arguments and the map
528 typedef std::map<const CodeGenIntrinsic*, unsigned,
529 AttributeComparator> UniqAttrMapTy;
530 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000531 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000532 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000533 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000534 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000535 maxArgAttrs =
536 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000537 unsigned &N = UniqAttributes[&intrinsic];
538 if (N) continue;
539 assert(AttrNum < 256 && "Too many unique attributes for table!");
540 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000541 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000542
Reid Kleckner67077702017-03-21 16:57:19 +0000543 // Emit an array of AttributeList. Most intrinsics will have at least one
Bill Wendlinge3617242013-01-27 03:25:05 +0000544 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000545 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topper1f595232012-02-28 06:32:00 +0000546
547 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
548 const CodeGenIntrinsic &intrinsic = Ints[i];
549
550 OS << " " << UniqAttributes[&intrinsic] << ", // "
551 << intrinsic.Name << "\n";
552 }
553 OS << " };\n\n";
554
Reid Kleckner67077702017-03-21 16:57:19 +0000555 OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000556 OS << " unsigned NumAttrs = 0;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000557 OS << " if (id != 0) {\n";
558 OS << " switch(IntrinsicsToAttributesMap[id - ";
559 if (TargetOnly)
560 OS << "Intrinsic::num_intrinsics";
561 else
562 OS << "1";
563 OS << "]) {\n";
564 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topper1f595232012-02-28 06:32:00 +0000565 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
566 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersone553fea2013-11-16 00:20:01 +0000567 OS << " case " << I->second << ": {\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000568
Craig Topper1f595232012-02-28 06:32:00 +0000569 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000570
571 // Keep track of the number of attributes we're writing out.
572 unsigned numAttrs = 0;
573
574 // The argument attributes are alreadys sorted by argument index.
Bill Wendling11d00422012-10-10 06:13:42 +0000575 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
576 if (ae) {
577 while (ai != ae) {
578 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Reid Klecknera82b3762017-05-03 18:17:31 +0000579 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
Craig Topper1f595232012-02-28 06:32:00 +0000580
Reid Klecknera82b3762017-05-03 18:17:31 +0000581 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
Owen Andersone553fea2013-11-16 00:20:01 +0000582 bool addComma = false;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000583
Bill Wendling11d00422012-10-10 06:13:42 +0000584 do {
585 switch (intrinsic.ArgumentAttributes[ai].second) {
586 case CodeGenIntrinsic::NoCapture:
Owen Andersone553fea2013-11-16 00:20:01 +0000587 if (addComma)
588 OS << ",";
589 OS << "Attribute::NoCapture";
590 addComma = true;
Bill Wendling11d00422012-10-10 06:13:42 +0000591 break;
Hal Finkelb7a19e92016-07-11 01:28:42 +0000592 case CodeGenIntrinsic::Returned:
593 if (addComma)
594 OS << ",";
595 OS << "Attribute::Returned";
596 addComma = true;
597 break;
Nick Lewyckydc897372013-07-06 00:29:58 +0000598 case CodeGenIntrinsic::ReadOnly:
Owen Andersone553fea2013-11-16 00:20:01 +0000599 if (addComma)
600 OS << ",";
601 OS << "Attribute::ReadOnly";
602 addComma = true;
Nick Lewyckydc897372013-07-06 00:29:58 +0000603 break;
Nicolai Haehnleb07f5402016-07-04 08:01:29 +0000604 case CodeGenIntrinsic::WriteOnly:
605 if (addComma)
606 OS << ",";
607 OS << "Attribute::WriteOnly";
608 addComma = true;
609 break;
Nick Lewyckydc897372013-07-06 00:29:58 +0000610 case CodeGenIntrinsic::ReadNone:
Owen Andersone553fea2013-11-16 00:20:01 +0000611 if (addComma)
612 OS << ",";
Eric Christopherf4745ec2015-07-30 21:16:34 +0000613 OS << "Attribute::ReadNone";
Owen Andersone553fea2013-11-16 00:20:01 +0000614 addComma = true;
Nick Lewyckydc897372013-07-06 00:29:58 +0000615 break;
Bill Wendling11d00422012-10-10 06:13:42 +0000616 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000617
Bill Wendling11d00422012-10-10 06:13:42 +0000618 ++ai;
619 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersone553fea2013-11-16 00:20:01 +0000620 OS << "};\n";
Reid Kleckner67077702017-03-21 16:57:19 +0000621 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
Reid Klecknera82b3762017-05-03 18:17:31 +0000622 << attrIdx << ", AttrParam" << attrIdx << ");\n";
Bill Wendling11d00422012-10-10 06:13:42 +0000623 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000624 }
625
Igor Laevskyea56ef72015-08-13 17:40:04 +0000626 if (!intrinsic.canThrow ||
627 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
Vedant Kumar3d60ff72018-11-14 19:53:41 +0000628 intrinsic.isNoReturn || intrinsic.isCold || intrinsic.isNoDuplicate ||
Matt Arsenaultea376da2017-04-28 20:25:27 +0000629 intrinsic.isConvergent || intrinsic.isSpeculatable) {
Owen Andersone553fea2013-11-16 00:20:01 +0000630 OS << " const Attribute::AttrKind Atts[] = {";
631 bool addComma = false;
632 if (!intrinsic.canThrow) {
633 OS << "Attribute::NoUnwind";
634 addComma = true;
635 }
636 if (intrinsic.isNoReturn) {
637 if (addComma)
638 OS << ",";
639 OS << "Attribute::NoReturn";
640 addComma = true;
641 }
Vedant Kumar3d60ff72018-11-14 19:53:41 +0000642 if (intrinsic.isCold) {
643 if (addComma)
644 OS << ",";
645 OS << "Attribute::Cold";
646 addComma = true;
647 }
Eli Bendersky21354ec2014-03-18 23:51:07 +0000648 if (intrinsic.isNoDuplicate) {
649 if (addComma)
650 OS << ",";
651 OS << "Attribute::NoDuplicate";
652 addComma = true;
653 }
Owen Anderson13146c72015-05-26 23:48:40 +0000654 if (intrinsic.isConvergent) {
655 if (addComma)
656 OS << ",";
657 OS << "Attribute::Convergent";
658 addComma = true;
659 }
Matt Arsenaultea376da2017-04-28 20:25:27 +0000660 if (intrinsic.isSpeculatable) {
661 if (addComma)
662 OS << ",";
663 OS << "Attribute::Speculatable";
664 addComma = true;
665 }
Chris Lattner86208902012-05-27 23:20:41 +0000666
Igor Laevskyea56ef72015-08-13 17:40:04 +0000667 switch (intrinsic.ModRef) {
668 case CodeGenIntrinsic::NoMem:
669 if (addComma)
670 OS << ",";
671 OS << "Attribute::ReadNone";
672 break;
673 case CodeGenIntrinsic::ReadArgMem:
674 if (addComma)
675 OS << ",";
676 OS << "Attribute::ReadOnly,";
677 OS << "Attribute::ArgMemOnly";
678 break;
679 case CodeGenIntrinsic::ReadMem:
Owen Andersone553fea2013-11-16 00:20:01 +0000680 if (addComma)
681 OS << ",";
682 OS << "Attribute::ReadOnly";
Chris Lattner86208902012-05-27 23:20:41 +0000683 break;
Andrew Kaylora5156e52016-11-22 19:16:04 +0000684 case CodeGenIntrinsic::ReadInaccessibleMem:
685 if (addComma)
686 OS << ",";
687 OS << "Attribute::ReadOnly,";
688 OS << "Attribute::InaccessibleMemOnly";
689 break;
690 case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem:
691 if (addComma)
692 OS << ",";
693 OS << "Attribute::ReadOnly,";
694 OS << "Attribute::InaccessibleMemOrArgMemOnly";
695 break;
Nicolai Haehnle318d6a22016-04-19 21:58:33 +0000696 case CodeGenIntrinsic::WriteArgMem:
Nicolai Haehnleb07f5402016-07-04 08:01:29 +0000697 if (addComma)
698 OS << ",";
699 OS << "Attribute::WriteOnly,";
700 OS << "Attribute::ArgMemOnly";
701 break;
702 case CodeGenIntrinsic::WriteMem:
703 if (addComma)
704 OS << ",";
705 OS << "Attribute::WriteOnly";
706 break;
Andrew Kaylora5156e52016-11-22 19:16:04 +0000707 case CodeGenIntrinsic::WriteInaccessibleMem:
708 if (addComma)
709 OS << ",";
710 OS << "Attribute::WriteOnly,";
711 OS << "Attribute::InaccessibleMemOnly";
712 break;
713 case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem:
714 if (addComma)
715 OS << ",";
716 OS << "Attribute::WriteOnly,";
Matt Arsenaulte87334b2017-12-03 00:03:01 +0000717 OS << "Attribute::InaccessibleMemOrArgMemOnly";
Andrew Kaylora5156e52016-11-22 19:16:04 +0000718 break;
Igor Laevskyea56ef72015-08-13 17:40:04 +0000719 case CodeGenIntrinsic::ReadWriteArgMem:
Owen Andersone553fea2013-11-16 00:20:01 +0000720 if (addComma)
721 OS << ",";
Igor Laevskyea56ef72015-08-13 17:40:04 +0000722 OS << "Attribute::ArgMemOnly";
723 break;
Andrew Kaylora5156e52016-11-22 19:16:04 +0000724 case CodeGenIntrinsic::ReadWriteInaccessibleMem:
725 if (addComma)
726 OS << ",";
727 OS << "Attribute::InaccessibleMemOnly";
728 break;
729 case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem:
730 if (addComma)
731 OS << ",";
732 OS << "Attribute::InaccessibleMemOrArgMemOnly";
Adrian Prantlc0ade052017-12-19 22:05:25 +0000733 break;
Igor Laevskyea56ef72015-08-13 17:40:04 +0000734 case CodeGenIntrinsic::ReadWriteMem:
Chris Lattner86208902012-05-27 23:20:41 +0000735 break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000736 }
Owen Andersone553fea2013-11-16 00:20:01 +0000737 OS << "};\n";
Reid Kleckner67077702017-03-21 16:57:19 +0000738 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
739 << "AttributeList::FunctionIndex, Atts);\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000740 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000741
742 if (numAttrs) {
Craig Topperb57b1702012-04-13 06:14:57 +0000743 OS << " NumAttrs = " << numAttrs << ";\n";
744 OS << " break;\n";
Owen Andersone553fea2013-11-16 00:20:01 +0000745 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000746 } else {
Reid Kleckner67077702017-03-21 16:57:19 +0000747 OS << " return AttributeList();\n";
Filip Pizlo23ffb3e2014-02-20 23:57:31 +0000748 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000749 }
Chris Lattner10dae942009-01-12 01:27:55 +0000750 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000751
Craig Topperb57b1702012-04-13 06:14:57 +0000752 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000753 OS << " }\n";
Reid Kleckner67077702017-03-21 16:57:19 +0000754 OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000755 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000756 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000757}
Chris Lattner022f64f2006-03-13 23:08:44 +0000758
Reid Klecknerfa217412016-01-27 01:43:12 +0000759void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
Justin Bognera3d02c72016-07-15 16:31:37 +0000760 const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) {
Reid Klecknerfa217412016-01-27 01:43:12 +0000761 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
762 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000763 BIMTy BuiltinMap;
Reid Klecknerfa217412016-01-27 01:43:12 +0000764 StringToOffsetTable Table;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000765 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Reid Klecknerfa217412016-01-27 01:43:12 +0000766 const std::string &BuiltinName =
767 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
768 if (!BuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000769 // Get the map for this target prefix.
Reid Klecknerfa217412016-01-27 01:43:12 +0000770 std::map<std::string, std::string> &BIM =
771 BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickcf940ce2013-10-31 17:18:07 +0000772
Reid Klecknerfa217412016-01-27 01:43:12 +0000773 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000774 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
Reid Klecknerfa217412016-01-27 01:43:12 +0000775 "': duplicate " + CompilerName + " builtin name!");
776 Table.GetOrAddStringOffset(BuiltinName);
Chris Lattner3f8b8912006-03-15 01:33:26 +0000777 }
778 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000779
Reid Klecknerfa217412016-01-27 01:43:12 +0000780 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
781 OS << "// This is used by the C front-end. The builtin name is passed\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000782 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
783 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
Reid Klecknerfa217412016-01-27 01:43:12 +0000784 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000785
Dale Johannesen49de9822009-02-05 01:49:45 +0000786 if (TargetOnly) {
787 OS << "static " << TargetPrefix << "Intrinsic::ID "
Reid Klecknerfa217412016-01-27 01:43:12 +0000788 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
Mehdi Amini15796d22016-10-10 19:31:09 +0000789 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000790 } else {
Reid Klecknerfa217412016-01-27 01:43:12 +0000791 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
792 << "Builtin(const char "
Mehdi Amini15796d22016-10-10 19:31:09 +0000793 << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000794 }
Aditya Nandakumar48c8dfd2017-04-19 19:14:20 +0000795
796 if (Table.Empty()) {
797 OS << " return ";
798 if (!TargetPrefix.empty())
799 OS << "(" << TargetPrefix << "Intrinsic::ID)";
800 OS << "Intrinsic::not_intrinsic;\n";
801 OS << "}\n";
802 OS << "#endif\n\n";
803 return;
804 }
805
Reid Klecknerfa217412016-01-27 01:43:12 +0000806 OS << " static const char BuiltinNames[] = {\n";
807 Table.EmitCharArray(OS);
808 OS << " };\n\n";
809
810 OS << " struct BuiltinEntry {\n";
811 OS << " Intrinsic::ID IntrinID;\n";
812 OS << " unsigned StrTabOffset;\n";
813 OS << " const char *getName() const {\n";
814 OS << " return &BuiltinNames[StrTabOffset];\n";
815 OS << " }\n";
Mehdi Amini15796d22016-10-10 19:31:09 +0000816 OS << " bool operator<(StringRef RHS) const {\n";
817 OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n";
Reid Klecknerfa217412016-01-27 01:43:12 +0000818 OS << " }\n";
819 OS << " };\n";
820
Chris Lattner298b1762010-09-06 03:14:45 +0000821 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000822
Chris Lattner3f8b8912006-03-15 01:33:26 +0000823 // Note: this could emit significantly better code if we cared.
824 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000825 OS << " ";
826 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000827 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000828 else
829 OS << "/* Target Independent Builtins */ ";
830 OS << "{\n";
831
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000832 // Emit the comparisons for this target prefix.
Reid Klecknerfa217412016-01-27 01:43:12 +0000833 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
834 for (const auto &P : I->second) {
835 OS << " {Intrinsic::" << P.second << ", "
836 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
837 }
838 OS << " };\n";
839 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
840 OS << " std::end(" << I->first << "Names),\n";
841 OS << " BuiltinNameStr);\n";
842 OS << " if (I != std::end(" << I->first << "Names) &&\n";
Mehdi Amini15796d22016-10-10 19:31:09 +0000843 OS << " I->getName() == BuiltinNameStr)\n";
Reid Klecknerfa217412016-01-27 01:43:12 +0000844 OS << " return I->IntrinID;\n";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000845 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000846 }
Chris Lattner298b1762010-09-06 03:14:45 +0000847 OS << " return ";
848 if (!TargetPrefix.empty())
849 OS << "(" << TargetPrefix << "Intrinsic::ID)";
850 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000851 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000852 OS << "#endif\n\n";
853}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000854
Reid Kleckneraf7c4452018-06-23 02:02:38 +0000855void llvm::EmitIntrinsicEnums(RecordKeeper &RK, raw_ostream &OS,
856 bool TargetOnly) {
857 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/true);
858}
859
860void llvm::EmitIntrinsicImpl(RecordKeeper &RK, raw_ostream &OS,
861 bool TargetOnly) {
862 IntrinsicEmitter(RK, TargetOnly).run(OS, /*Enums=*/false);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000863}