blob: 9ec39340e3f62cb4e007c43ba2b680ce2accffaa [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"
Jeff Cohenc4e24682006-03-15 02:51:05 +000023#include <algorithm>
Chris Lattnerc313d0b2006-03-03 02:32:46 +000024using namespace llvm;
25
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000026namespace {
27class IntrinsicEmitter {
28 RecordKeeper &Records;
29 bool TargetOnly;
30 std::string TargetPrefix;
31
32public:
33 IntrinsicEmitter(RecordKeeper &R, bool T)
34 : Records(R), TargetOnly(T) {}
35
36 void run(raw_ostream &OS);
37
38 void EmitPrefix(raw_ostream &OS);
39
40 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
41 raw_ostream &OS);
42
43 void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
44 raw_ostream &OS);
45 void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
46 raw_ostream &OS);
47 void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
48 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000049 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
50 raw_ostream &OS);
51 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
52 raw_ostream &OS);
53 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
54 raw_ostream &OS);
55 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
56 raw_ostream &OS);
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +000057 void EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
58 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000059 void EmitSuffix(raw_ostream &OS);
60};
61} // End anonymous namespace
62
Chris Lattnerc313d0b2006-03-03 02:32:46 +000063//===----------------------------------------------------------------------===//
Chris Lattnerc313d0b2006-03-03 02:32:46 +000064// IntrinsicEmitter Implementation
65//===----------------------------------------------------------------------===//
66
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000067void IntrinsicEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000068 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
69
Dale Johannesenb842d522009-02-05 01:49:45 +000070 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000071
Dale Johannesenb842d522009-02-05 01:49:45 +000072 if (TargetOnly && !Ints.empty())
73 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattnerc313d0b2006-03-03 02:32:46 +000074
Douglas Gregor6739a892010-05-11 06:17:44 +000075 EmitPrefix(OS);
76
Chris Lattnerc313d0b2006-03-03 02:32:46 +000077 // Emit the enum information.
78 EmitEnumInfo(Ints, OS);
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000079
80 // Emit the intrinsic ID -> name table.
81 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wangb4024932009-02-24 23:17:49 +000082
83 // Emit the intrinsic ID -> overload table.
84 EmitIntrinsicToOverloadTable(Ints, OS);
85
Chris Lattner6d8104e2006-03-09 20:34:19 +000086 // Emit the function name recognizer.
87 EmitFnNameRecognizer(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000088
Jim Laskey2682ea62007-02-07 20:38:26 +000089 // Emit the intrinsic declaration generator.
90 EmitGenerator(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000091
Duncan Sands38ef3a82007-12-03 20:06:50 +000092 // Emit the intrinsic parameter attributes.
93 EmitAttributes(Ints, OS);
Chris Lattnerfea17a92006-03-13 23:08:44 +000094
Duncan Sands73247d22009-02-14 10:56:35 +000095 // Emit intrinsic alias analysis mod/ref behavior.
96 EmitModRefBehavior(Ints, OS);
97
Chris Lattner402a5732006-03-15 01:33:26 +000098 // Emit code to translate GCC builtins into LLVM intrinsics.
99 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor6739a892010-05-11 06:17:44 +0000100
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000101 // Emit code to translate MS builtins into LLVM intrinsics.
102 EmitIntrinsicToMSBuiltinMap(Ints, OS);
103
Douglas Gregor6739a892010-05-11 06:17:44 +0000104 EmitSuffix(OS);
105}
106
107void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
108 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000109 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
110 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000111 "# pragma push_macro(\"setjmp\")\n"
112 "# undef setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000113 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000114 "#endif\n\n";
115}
116
117void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencerded5f662010-09-24 19:48:47 +0000118 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000119 "// let's return it to _setjmp state\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000120 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000121 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000122 "#endif\n\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000123}
124
125void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000126 raw_ostream &OS) {
Chris Lattner6d8104e2006-03-09 20:34:19 +0000127 OS << "// Enum values for Intrinsics.h\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000128 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
129 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
130 OS << " " << Ints[i].EnumName;
131 OS << ((i != e-1) ? ", " : " ");
Justin Holewinski8a5bf7f2014-07-17 11:23:29 +0000132 if (Ints[i].EnumName.size() < 40)
133 OS << std::string(40-Ints[i].EnumName.size(), ' ');
134 OS << " // " << Ints[i].Name << "\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000135 }
136 OS << "#endif\n\n";
137}
Chris Lattner6d8104e2006-03-09 20:34:19 +0000138
139void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000140EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000141 raw_ostream &OS) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000142 // Build a 'first character of function name' -> intrinsic # mapping.
143 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner6d8104e2006-03-09 20:34:19 +0000144 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattner07b332f2010-09-06 03:58:45 +0000145 IntMapping[Ints[i].Name[5]].push_back(i);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000146
Chris Lattner6d8104e2006-03-09 20:34:19 +0000147 OS << "// Function name -> enum value recognizer code.\n";
148 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattner07b332f2010-09-06 03:58:45 +0000149 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
150 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
151 OS << " default: break;\n";
152 // Emit the intrinsic matching stuff by first letter.
153 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner6d8104e2006-03-09 20:34:19 +0000154 E = IntMapping.end(); I != E; ++I) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000155 OS << " case '" << I->first << "':\n";
156 std::vector<unsigned> &IntList = I->second;
157
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000158 // Sort in reverse order of intrinsic name so "abc.def" appears after
159 // "abd.def.ghi" in the overridden name matcher
160 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
161 return Ints[i].Name > Ints[j].Name;
162 });
Justin Holewinskib3d630c2013-07-25 12:32:00 +0000163
Chris Lattner07b332f2010-09-06 03:58:45 +0000164 // Emit all the overloaded intrinsics first, build a table of the
165 // non-overloaded ones.
166 std::vector<StringMatcher::StringPair> MatchTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000167
Chris Lattner07b332f2010-09-06 03:58:45 +0000168 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
169 unsigned IntNo = IntList[i];
170 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
171 Ints[IntNo].EnumName + ";";
172
173 if (!Ints[IntNo].isOverloaded) {
174 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
175 continue;
176 }
177
178 // For overloaded intrinsics, only the prefix needs to match
179 std::string TheStr = Ints[IntNo].Name.substr(6);
180 TheStr += '.'; // Require "bswap." instead of bswap.
181 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
182 << Result << '\n';
Chris Lattner6d8104e2006-03-09 20:34:19 +0000183 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000184
Chris Lattner07b332f2010-09-06 03:58:45 +0000185 // Emit the matcher logic for the fixed length strings.
186 StringMatcher("NameR", MatchTable, OS).Emit(1);
187 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner6d8104e2006-03-09 20:34:19 +0000188 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000189
Chris Lattner6d8104e2006-03-09 20:34:19 +0000190 OS << " }\n";
Chris Lattner6efe8632006-03-09 22:05:04 +0000191 OS << "#endif\n\n";
192}
193
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000194void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000195EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000196 raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000197 OS << "// Intrinsic ID to name table\n";
198 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
199 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000200 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
201 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000202 OS << "#endif\n\n";
203}
204
Mon P Wangb4024932009-02-24 23:17:49 +0000205void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000206EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000207 raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000208 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000209 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000210 OS << "static const uint8_t OTable[] = {\n";
211 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000212 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000213 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
214 if ((i+1)%8 == 0)
215 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000216 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000217 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000218 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000219 OS << "\n};\n\n";
220 // OTable contains a true bit at the position if the intrinsic is overloaded.
221 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000222 OS << "#endif\n\n";
223}
224
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000225
Chris Lattnerf39c2782012-05-27 18:28:35 +0000226// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000227enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000228 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000229 IIT_Done = 0,
230 IIT_I1 = 1,
231 IIT_I8 = 2,
232 IIT_I16 = 3,
233 IIT_I32 = 4,
234 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000235 IIT_F16 = 6,
236 IIT_F32 = 7,
237 IIT_F64 = 8,
238 IIT_V2 = 9,
239 IIT_V4 = 10,
240 IIT_V8 = 11,
241 IIT_V16 = 12,
242 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000243 IIT_PTR = 14,
244 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000245
Robert Khasanov65c27562014-10-20 19:25:05 +0000246 // Values from 16+ are only encodable with the inefficient encoding.
247 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000248 IIT_MMX = 17,
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000249 IIT_TOKEN = 18,
250 IIT_METADATA = 19,
251 IIT_EMPTYSTRUCT = 20,
252 IIT_STRUCT2 = 21,
253 IIT_STRUCT3 = 22,
254 IIT_STRUCT4 = 23,
255 IIT_STRUCT5 = 24,
256 IIT_EXTEND_ARG = 25,
257 IIT_TRUNC_ARG = 26,
258 IIT_ANYPTR = 27,
259 IIT_V1 = 28,
260 IIT_VARARG = 29,
261 IIT_HALF_VEC_ARG = 30,
262 IIT_SAME_VEC_WIDTH_ARG = 31,
263 IIT_PTR_TO_ARG = 32,
264 IIT_VEC_OF_PTRS_TO_ELT = 33,
265 IIT_I128 = 34
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000266};
267
Chris Lattner827b2532012-05-17 04:30:58 +0000268
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000269static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000270 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000271 if (MVT(VT).isInteger()) {
272 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000273 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000274 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000275 case 1: return Sig.push_back(IIT_I1);
276 case 8: return Sig.push_back(IIT_I8);
277 case 16: return Sig.push_back(IIT_I16);
278 case 32: return Sig.push_back(IIT_I32);
279 case 64: return Sig.push_back(IIT_I64);
Kit Barton66460332015-05-25 15:49:26 +0000280 case 128: return Sig.push_back(IIT_I128);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000281 }
282 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000283
Chris Lattner827b2532012-05-17 04:30:58 +0000284 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000285 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000286 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000287 case MVT::f32: return Sig.push_back(IIT_F32);
288 case MVT::f64: return Sig.push_back(IIT_F64);
Joseph Tremoulet917c7382015-09-02 13:36:25 +0000289 case MVT::token: return Sig.push_back(IIT_TOKEN);
Chris Lattner827b2532012-05-17 04:30:58 +0000290 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
291 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
292 // MVT::OtherVT is used to mean the empty struct type here.
293 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000294 // MVT::isVoid is used to represent varargs here.
295 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000296 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000297}
298
Reid Klecknerf3c82092015-02-26 21:34:11 +0000299#if defined(_MSC_VER) && !defined(__clang__)
300#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
301#endif
302
Chris Lattnerc4644162012-05-27 16:39:08 +0000303static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000304 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000305
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000306 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000307 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000308 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000309 if (R->isSubClassOf("LLVMExtendedType"))
310 Sig.push_back(IIT_EXTEND_ARG);
311 else if (R->isSubClassOf("LLVMTruncatedType"))
312 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000313 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
314 Sig.push_back(IIT_HALF_VEC_ARG);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000315 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
316 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000317 Sig.push_back((Number << 3) | ArgCodes[Number]);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000318 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
319 EncodeFixedValueType(VT, Sig);
320 return;
321 }
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000322 else if (R->isSubClassOf("LLVMPointerTo"))
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000323 Sig.push_back(IIT_PTR_TO_ARG);
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000324 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
325 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000326 else
327 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000328 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000329 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000330
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000331 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000332
Chris Lattnerc4644162012-05-27 16:39:08 +0000333 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000334 switch (VT) {
335 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000336 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
337 case MVT::vAny: ++Tmp; // FALL THROUGH.
338 case MVT::fAny: ++Tmp; // FALL THROUGH.
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000339 case MVT::iAny: ++Tmp; // FALL THROUGH.
340 case MVT::Any: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000341 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000342 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000343 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000344
Chris Lattnerc4644162012-05-27 16:39:08 +0000345 // Figure out what arg # this is consuming, and remember what kind it was.
346 unsigned ArgNo = ArgCodes.size();
347 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000348
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000349 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
350 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerc4644162012-05-27 16:39:08 +0000351 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000352
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000353 case MVT::iPTR: {
354 unsigned AddrSpace = 0;
355 if (R->isSubClassOf("LLVMQualPointerType")) {
356 AddrSpace = R->getValueAsInt("AddrSpace");
357 assert(AddrSpace < 256 && "Address space exceeds 255");
358 }
359 if (AddrSpace) {
360 Sig.push_back(IIT_ANYPTR);
361 Sig.push_back(AddrSpace);
362 } else {
363 Sig.push_back(IIT_PTR);
364 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000365 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000366 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000367 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000368
Craig Topper8561de92014-01-24 20:50:47 +0000369 if (MVT(VT).isVector()) {
370 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000371 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000372 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000373 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000374 case 2: Sig.push_back(IIT_V2); break;
375 case 4: Sig.push_back(IIT_V4); break;
376 case 8: Sig.push_back(IIT_V8); break;
377 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000378 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000379 case 64: Sig.push_back(IIT_V64); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000380 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000381
Craig Topper8561de92014-01-24 20:50:47 +0000382 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000383 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000384
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000385 EncodeFixedValueType(VT, Sig);
386}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000387
Reid Klecknerf3c82092015-02-26 21:34:11 +0000388#if defined(_MSC_VER) && !defined(__clang__)
389#pragma optimize("",on)
390#endif
391
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000392/// ComputeFixedEncoding - If we can encode the type signature for this
393/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000394static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
395 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000396 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000397
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000398 if (Int.IS.RetVTs.empty())
399 TypeSig.push_back(IIT_Done);
400 else if (Int.IS.RetVTs.size() == 1 &&
401 Int.IS.RetVTs[0] == MVT::isVoid)
402 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000403 else {
404 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000405 case 1: break;
406 case 2: TypeSig.push_back(IIT_STRUCT2); break;
407 case 3: TypeSig.push_back(IIT_STRUCT3); break;
408 case 4: TypeSig.push_back(IIT_STRUCT4); break;
409 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000410 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000411 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000412
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000413 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000414 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000415 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000416
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000417 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000418 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000419}
420
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000421static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000422 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000423}
424
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000425void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000426 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000427 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
428 // capture it in this vector, otherwise store a ~0U.
429 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000430
Chris Lattnera3b0f522012-05-17 15:55:41 +0000431 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000432
Chris Lattnera3b0f522012-05-17 15:55:41 +0000433 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000434
Jim Laskey2682ea62007-02-07 20:38:26 +0000435 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000436 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000437 // Get the signature for the intrinsic.
438 TypeSig.clear();
439 ComputeFixedEncoding(Ints[i], TypeSig);
440
441 // Check to see if we can encode it into a 32-bit word. We can only encode
442 // 8 nibbles into a 32-bit word.
443 if (TypeSig.size() <= 8) {
444 bool Failed = false;
445 unsigned Result = 0;
446 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
447 // If we had an unencodable argument, bail out.
448 if (TypeSig[i] > 15) {
449 Failed = true;
450 break;
451 }
452 Result = (Result << 4) | TypeSig[e-i-1];
453 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000454
Chris Lattnera3b0f522012-05-17 15:55:41 +0000455 // If this could be encoded into a 31-bit word, return it.
456 if (!Failed && (Result >> 31) == 0) {
457 FixedEncodings.push_back(Result);
458 continue;
459 }
460 }
461
462 // Otherwise, we're going to unique the sequence into the
463 // LongEncodingTable, and use its offset in the 32-bit table instead.
464 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000465
Chris Lattnera3b0f522012-05-17 15:55:41 +0000466 // This is a placehold that we'll replace after the table is laid out.
467 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000468 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000469
Chris Lattnera3b0f522012-05-17 15:55:41 +0000470 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000471
Chris Lattnerf39c2782012-05-27 18:28:35 +0000472 OS << "// Global intrinsic function declaration type table.\n";
473 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
474
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000475 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000476
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000477 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
478 if ((i & 7) == 7)
479 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000480
Chris Lattnera3b0f522012-05-17 15:55:41 +0000481 // If the entry fit in the table, just emit it.
482 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000483 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000484 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000485 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000486
Chris Lattnera3b0f522012-05-17 15:55:41 +0000487 TypeSig.clear();
488 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000489
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000490
Chris Lattnera3b0f522012-05-17 15:55:41 +0000491 // Otherwise, emit the offset into the long encoding table. We emit it this
492 // way so that it is easier to read the offset in the .def file.
493 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000494 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000495
Chris Lattnera3b0f522012-05-17 15:55:41 +0000496 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000497
Chris Lattnera3b0f522012-05-17 15:55:41 +0000498 // Emit the shared table of register lists.
499 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
500 if (!LongEncodingTable.empty())
501 LongEncodingTable.emit(OS, printIITEntry);
502 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000503
Patrik Hägglundca210d82012-05-23 12:34:56 +0000504 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000505}
506
Richard Smith6bc9df32014-04-20 20:26:39 +0000507namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000508struct AttributeComparator {
509 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
510 // Sort throwing intrinsics after non-throwing intrinsics.
511 if (L->canThrow != R->canThrow)
512 return R->canThrow;
513
Eli Bendersky2281ef92014-03-18 23:51:07 +0000514 if (L->isNoDuplicate != R->isNoDuplicate)
515 return R->isNoDuplicate;
516
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000517 if (L->isNoReturn != R->isNoReturn)
518 return R->isNoReturn;
519
Owen Anderson85fa7d52015-05-26 23:48:40 +0000520 if (L->isConvergent != R->isConvergent)
521 return R->isConvergent;
522
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000523 // Try to order by readonly/readnone attribute.
Igor Laevsky30143ae2015-08-13 17:40:04 +0000524 CodeGenIntrinsic::ModRefKind LK = L->ModRef;
525 CodeGenIntrinsic::ModRefKind RK = R->ModRef;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000526 if (LK != RK) return (LK > RK);
527
528 // Order by argument attributes.
529 // This is reliable because each side is already sorted internally.
530 return (L->ArgumentAttributes < R->ArgumentAttributes);
531 }
532};
533} // End anonymous namespace
534
Chris Lattner49b7ee12009-01-12 01:18:58 +0000535/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000536void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000537EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000538 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
539 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000540 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000541 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000542 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000543 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000544 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000545
Craig Topperccd651c2012-02-28 06:32:00 +0000546 // Compute the maximum number of attribute arguments and the map
547 typedef std::map<const CodeGenIntrinsic*, unsigned,
548 AttributeComparator> UniqAttrMapTy;
549 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000550 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000551 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000552 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000553 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000554 maxArgAttrs =
555 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000556 unsigned &N = UniqAttributes[&intrinsic];
557 if (N) continue;
558 assert(AttrNum < 256 && "Too many unique attributes for table!");
559 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000560 }
John McCall375dcc92011-05-28 06:31:34 +0000561
Bill Wendling4d3491c2013-01-27 03:25:05 +0000562 // Emit an array of AttributeSet. Most intrinsics will have at least one
563 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000564 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000565
566 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
567 const CodeGenIntrinsic &intrinsic = Ints[i];
568
569 OS << " " << UniqAttributes[&intrinsic] << ", // "
570 << intrinsic.Name << "\n";
571 }
572 OS << " };\n\n";
573
Bill Wendling4d3491c2013-01-27 03:25:05 +0000574 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000575 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000576 OS << " if (id != 0) {\n";
577 OS << " switch(IntrinsicsToAttributesMap[id - ";
578 if (TargetOnly)
579 OS << "Intrinsic::num_intrinsics";
580 else
581 OS << "1";
582 OS << "]) {\n";
583 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000584 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
585 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000586 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000587
Craig Topperccd651c2012-02-28 06:32:00 +0000588 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000589
590 // Keep track of the number of attributes we're writing out.
591 unsigned numAttrs = 0;
592
593 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000594 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
595 if (ae) {
596 while (ai != ae) {
597 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000598
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000599 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
600 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000601
Bill Wendlinged42e792012-10-10 06:13:42 +0000602 do {
603 switch (intrinsic.ArgumentAttributes[ai].second) {
604 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000605 if (addComma)
606 OS << ",";
607 OS << "Attribute::NoCapture";
608 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000609 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000610 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000611 if (addComma)
612 OS << ",";
613 OS << "Attribute::ReadOnly";
614 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000615 break;
616 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000617 if (addComma)
618 OS << ",";
Eric Christopher10f5d602015-07-30 21:16:34 +0000619 OS << "Attribute::ReadNone";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000620 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000621 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000622 }
John McCall375dcc92011-05-28 06:31:34 +0000623
Bill Wendlinged42e792012-10-10 06:13:42 +0000624 ++ai;
625 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000626 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000627 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000628 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000629 }
John McCall375dcc92011-05-28 06:31:34 +0000630 }
631
Igor Laevsky30143ae2015-08-13 17:40:04 +0000632 if (!intrinsic.canThrow ||
633 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
634 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
635 intrinsic.isConvergent) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000636 OS << " const Attribute::AttrKind Atts[] = {";
637 bool addComma = false;
638 if (!intrinsic.canThrow) {
639 OS << "Attribute::NoUnwind";
640 addComma = true;
641 }
642 if (intrinsic.isNoReturn) {
643 if (addComma)
644 OS << ",";
645 OS << "Attribute::NoReturn";
646 addComma = true;
647 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000648 if (intrinsic.isNoDuplicate) {
649 if (addComma)
650 OS << ",";
651 OS << "Attribute::NoDuplicate";
652 addComma = true;
653 }
Owen Anderson85fa7d52015-05-26 23:48:40 +0000654 if (intrinsic.isConvergent) {
655 if (addComma)
656 OS << ",";
657 OS << "Attribute::Convergent";
658 addComma = true;
659 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000660
Igor Laevsky30143ae2015-08-13 17:40:04 +0000661 switch (intrinsic.ModRef) {
662 case CodeGenIntrinsic::NoMem:
663 if (addComma)
664 OS << ",";
665 OS << "Attribute::ReadNone";
666 break;
667 case CodeGenIntrinsic::ReadArgMem:
668 if (addComma)
669 OS << ",";
670 OS << "Attribute::ReadOnly,";
671 OS << "Attribute::ArgMemOnly";
672 break;
673 case CodeGenIntrinsic::ReadMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000674 if (addComma)
675 OS << ",";
676 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000677 break;
Igor Laevsky30143ae2015-08-13 17:40:04 +0000678 case CodeGenIntrinsic::ReadWriteArgMem:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000679 if (addComma)
680 OS << ",";
Igor Laevsky30143ae2015-08-13 17:40:04 +0000681 OS << "Attribute::ArgMemOnly";
682 break;
683 case CodeGenIntrinsic::ReadWriteMem:
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000684 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000685 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000686 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000687 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000688 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000689 }
John McCall375dcc92011-05-28 06:31:34 +0000690
691 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000692 OS << " NumAttrs = " << numAttrs << ";\n";
693 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000694 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000695 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000696 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000697 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000698 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000699 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000700
Craig Topper374f19c2012-04-13 06:14:57 +0000701 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000702 OS << " }\n";
Craig Toppere1d12942014-08-27 05:25:25 +0000703 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000704 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000705 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000706}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000707
Duncan Sands73247d22009-02-14 10:56:35 +0000708/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
709void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000710EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000711 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
712 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
713 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
714 << "\"Unknown intrinsic.\");\n\n";
715
716 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
Chandler Carruth194f59c2015-07-22 23:15:57 +0000717 << " /* invalid */ FMRB_UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000718 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000719 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000720 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000721 case CodeGenIntrinsic::NoMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000722 OS << "FMRB_DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000723 break;
724 case CodeGenIntrinsic::ReadArgMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000725 OS << "FMRB_OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000726 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000727 case CodeGenIntrinsic::ReadMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000728 OS << "FMRB_OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000729 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000730 case CodeGenIntrinsic::ReadWriteArgMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000731 OS << "FMRB_OnlyAccessesArgumentPointees,\n";
Benjamin Krameraba35032012-03-01 01:18:32 +0000732 break;
733 case CodeGenIntrinsic::ReadWriteMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000734 OS << "FMRB_UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000735 break;
736 }
737 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000738 OS << "};\n\n"
Chandler Carruth194f59c2015-07-22 23:15:57 +0000739 << "return "
740 "static_cast<FunctionModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
Benjamin Krameraba35032012-03-01 01:18:32 +0000741 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000742}
743
Chris Lattner69ea0142008-01-04 04:38:35 +0000744/// EmitTargetBuiltins - All of the builtins in the specified map are for the
745/// same target, and we already checked it.
746static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000747 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000748 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000749
Chris Lattner497d13e2010-09-06 03:14:45 +0000750 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000751
Chris Lattner497d13e2010-09-06 03:14:45 +0000752 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
753 E = BIM.end(); I != E; ++I) {
754 std::string ResultCode =
755 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000756 Results.emplace_back(I->first, ResultCode);
Chris Lattner69ea0142008-01-04 04:38:35 +0000757 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000758
759 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000760}
761
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000762
Chris Lattner402a5732006-03-15 01:33:26 +0000763void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000764EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000765 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000766 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000767 BIMTy BuiltinMap;
768 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
769 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000770 // Get the map for this target prefix.
771 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000772
Chris Lattner91678fc2008-01-02 21:24:22 +0000773 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
774 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000775 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
776 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000777 }
778 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000779
Chris Lattner402a5732006-03-15 01:33:26 +0000780 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
781 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
782 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";
784 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000785
Dale Johannesenb842d522009-02-05 01:49:45 +0000786 if (TargetOnly) {
787 OS << "static " << TargetPrefix << "Intrinsic::ID "
788 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000789 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000790 } else {
791 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000792 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000793 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000794
Chris Lattner497d13e2010-09-06 03:14:45 +0000795 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
796 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000797
Chris Lattner402a5732006-03-15 01:33:26 +0000798 // Note: this could emit significantly better code if we cared.
799 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000800 OS << " ";
801 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000802 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000803 else
804 OS << "/* Target Independent Builtins */ ";
805 OS << "{\n";
806
Chris Lattner91678fc2008-01-02 21:24:22 +0000807 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000808 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000809 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000810 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000811 OS << " return ";
812 if (!TargetPrefix.empty())
813 OS << "(" << TargetPrefix << "Intrinsic::ID)";
814 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000815 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000816 OS << "#endif\n\n";
817}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000818
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000819void IntrinsicEmitter::
820EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
821 raw_ostream &OS) {
822 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
823
824 for (const auto &Intrinsic : Ints) {
825 if (Intrinsic.MSBuiltinName.empty())
826 continue;
827
828 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
829 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
830 Intrinsic.EnumName)).second)
831 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
832 "duplicate MS builtin name!");
833 }
834
835 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
836 "// This is used by the C front-end. The MS builtin name is passed\n"
837 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
838 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
839 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
840
841 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
842 << (TargetOnly ? "" : "Intrinsic::")
843 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
844 OS << " StringRef BuiltinName(BN);\n"
845 " StringRef TargetPrefix(TP);\n"
846 "\n";
847
848 for (const auto &Builtins : TargetBuiltins) {
849 OS << " ";
850 if (Builtins.first.empty())
851 OS << "/* Target Independent Builtins */ ";
852 else
853 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
854 OS << "{\n";
855 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
856 OS << "}";
857 }
858
859 OS << " return ";
860 if (!TargetPrefix.empty())
861 OS << "(" << TargetPrefix << "Intrinsic::ID)";
862 OS << "Intrinsic::not_intrinsic;\n";
863 OS << "}\n";
864
865 OS << "#endif\n\n";
866}
867
Richard Smith6bc9df32014-04-20 20:26:39 +0000868void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000869 IntrinsicEmitter(RK, TargetOnly).run(OS);
870}