blob: 18a8db6b5348150031fd9139107c748af716c5c1 [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,
249 IIT_METADATA = 18,
250 IIT_EMPTYSTRUCT = 19,
251 IIT_STRUCT2 = 20,
252 IIT_STRUCT3 = 21,
253 IIT_STRUCT4 = 22,
254 IIT_STRUCT5 = 23,
255 IIT_EXTEND_ARG = 24,
256 IIT_TRUNC_ARG = 25,
257 IIT_ANYPTR = 26,
258 IIT_V1 = 27,
259 IIT_VARARG = 28,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000260 IIT_HALF_VEC_ARG = 29,
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000261 IIT_SAME_VEC_WIDTH_ARG = 30,
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000262 IIT_PTR_TO_ARG = 31,
Kit Barton66460332015-05-25 15:49:26 +0000263 IIT_VEC_OF_PTRS_TO_ELT = 32,
264 IIT_I128 = 33
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000265};
266
Chris Lattner827b2532012-05-17 04:30:58 +0000267
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000268static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000269 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000270 if (MVT(VT).isInteger()) {
271 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000272 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000273 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000274 case 1: return Sig.push_back(IIT_I1);
275 case 8: return Sig.push_back(IIT_I8);
276 case 16: return Sig.push_back(IIT_I16);
277 case 32: return Sig.push_back(IIT_I32);
278 case 64: return Sig.push_back(IIT_I64);
Kit Barton66460332015-05-25 15:49:26 +0000279 case 128: return Sig.push_back(IIT_I128);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000280 }
281 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000282
Chris Lattner827b2532012-05-17 04:30:58 +0000283 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000284 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000285 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000286 case MVT::f32: return Sig.push_back(IIT_F32);
287 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000288 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
289 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
290 // MVT::OtherVT is used to mean the empty struct type here.
291 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000292 // MVT::isVoid is used to represent varargs here.
293 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000294 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000295}
296
Reid Klecknerf3c82092015-02-26 21:34:11 +0000297#if defined(_MSC_VER) && !defined(__clang__)
298#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
299#endif
300
Chris Lattnerc4644162012-05-27 16:39:08 +0000301static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000302 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000303
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000304 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000305 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000306 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000307 if (R->isSubClassOf("LLVMExtendedType"))
308 Sig.push_back(IIT_EXTEND_ARG);
309 else if (R->isSubClassOf("LLVMTruncatedType"))
310 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000311 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
312 Sig.push_back(IIT_HALF_VEC_ARG);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000313 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
314 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000315 Sig.push_back((Number << 3) | ArgCodes[Number]);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000316 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
317 EncodeFixedValueType(VT, Sig);
318 return;
319 }
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000320 else if (R->isSubClassOf("LLVMPointerTo"))
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000321 Sig.push_back(IIT_PTR_TO_ARG);
Elena Demikhovsky23a485a2015-02-08 08:27:19 +0000322 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
323 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000324 else
325 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000326 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000327 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000328
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000329 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000330
Chris Lattnerc4644162012-05-27 16:39:08 +0000331 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000332 switch (VT) {
333 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000334 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
335 case MVT::vAny: ++Tmp; // FALL THROUGH.
336 case MVT::fAny: ++Tmp; // FALL THROUGH.
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000337 case MVT::iAny: ++Tmp; // FALL THROUGH.
338 case MVT::Any: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000339 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000340 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000341 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000342
Chris Lattnerc4644162012-05-27 16:39:08 +0000343 // Figure out what arg # this is consuming, and remember what kind it was.
344 unsigned ArgNo = ArgCodes.size();
345 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000346
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000347 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
348 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerc4644162012-05-27 16:39:08 +0000349 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000350
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000351 case MVT::iPTR: {
352 unsigned AddrSpace = 0;
353 if (R->isSubClassOf("LLVMQualPointerType")) {
354 AddrSpace = R->getValueAsInt("AddrSpace");
355 assert(AddrSpace < 256 && "Address space exceeds 255");
356 }
357 if (AddrSpace) {
358 Sig.push_back(IIT_ANYPTR);
359 Sig.push_back(AddrSpace);
360 } else {
361 Sig.push_back(IIT_PTR);
362 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000363 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000364 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000365 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000366
Craig Topper8561de92014-01-24 20:50:47 +0000367 if (MVT(VT).isVector()) {
368 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000369 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000370 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000371 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000372 case 2: Sig.push_back(IIT_V2); break;
373 case 4: Sig.push_back(IIT_V4); break;
374 case 8: Sig.push_back(IIT_V8); break;
375 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000376 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000377 case 64: Sig.push_back(IIT_V64); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000378 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000379
Craig Topper8561de92014-01-24 20:50:47 +0000380 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000381 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000382
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000383 EncodeFixedValueType(VT, Sig);
384}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000385
Reid Klecknerf3c82092015-02-26 21:34:11 +0000386#if defined(_MSC_VER) && !defined(__clang__)
387#pragma optimize("",on)
388#endif
389
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000390/// ComputeFixedEncoding - If we can encode the type signature for this
391/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000392static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
393 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000394 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000395
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000396 if (Int.IS.RetVTs.empty())
397 TypeSig.push_back(IIT_Done);
398 else if (Int.IS.RetVTs.size() == 1 &&
399 Int.IS.RetVTs[0] == MVT::isVoid)
400 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000401 else {
402 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000403 case 1: break;
404 case 2: TypeSig.push_back(IIT_STRUCT2); break;
405 case 3: TypeSig.push_back(IIT_STRUCT3); break;
406 case 4: TypeSig.push_back(IIT_STRUCT4); break;
407 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000408 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000409 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000410
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000411 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000412 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000413 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000414
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000415 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000416 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000417}
418
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000419static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000420 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000421}
422
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000423void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000424 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000425 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
426 // capture it in this vector, otherwise store a ~0U.
427 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000428
Chris Lattnera3b0f522012-05-17 15:55:41 +0000429 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000430
Chris Lattnera3b0f522012-05-17 15:55:41 +0000431 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000432
Jim Laskey2682ea62007-02-07 20:38:26 +0000433 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000434 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000435 // Get the signature for the intrinsic.
436 TypeSig.clear();
437 ComputeFixedEncoding(Ints[i], TypeSig);
438
439 // Check to see if we can encode it into a 32-bit word. We can only encode
440 // 8 nibbles into a 32-bit word.
441 if (TypeSig.size() <= 8) {
442 bool Failed = false;
443 unsigned Result = 0;
444 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
445 // If we had an unencodable argument, bail out.
446 if (TypeSig[i] > 15) {
447 Failed = true;
448 break;
449 }
450 Result = (Result << 4) | TypeSig[e-i-1];
451 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000452
Chris Lattnera3b0f522012-05-17 15:55:41 +0000453 // If this could be encoded into a 31-bit word, return it.
454 if (!Failed && (Result >> 31) == 0) {
455 FixedEncodings.push_back(Result);
456 continue;
457 }
458 }
459
460 // Otherwise, we're going to unique the sequence into the
461 // LongEncodingTable, and use its offset in the 32-bit table instead.
462 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000463
Chris Lattnera3b0f522012-05-17 15:55:41 +0000464 // This is a placehold that we'll replace after the table is laid out.
465 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000466 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000467
Chris Lattnera3b0f522012-05-17 15:55:41 +0000468 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000469
Chris Lattnerf39c2782012-05-27 18:28:35 +0000470 OS << "// Global intrinsic function declaration type table.\n";
471 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
472
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000473 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000474
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000475 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
476 if ((i & 7) == 7)
477 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000478
Chris Lattnera3b0f522012-05-17 15:55:41 +0000479 // If the entry fit in the table, just emit it.
480 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000481 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000482 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000483 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000484
Chris Lattnera3b0f522012-05-17 15:55:41 +0000485 TypeSig.clear();
486 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000487
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000488
Chris Lattnera3b0f522012-05-17 15:55:41 +0000489 // Otherwise, emit the offset into the long encoding table. We emit it this
490 // way so that it is easier to read the offset in the .def file.
491 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000492 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000493
Chris Lattnera3b0f522012-05-17 15:55:41 +0000494 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000495
Chris Lattnera3b0f522012-05-17 15:55:41 +0000496 // Emit the shared table of register lists.
497 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
498 if (!LongEncodingTable.empty())
499 LongEncodingTable.emit(OS, printIITEntry);
500 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000501
Patrik Hägglundca210d82012-05-23 12:34:56 +0000502 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000503}
504
Richard Smith6bc9df32014-04-20 20:26:39 +0000505namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000506enum ModRefKind {
507 MRK_none,
508 MRK_readonly,
509 MRK_readnone
510};
Richard Smith6bc9df32014-04-20 20:26:39 +0000511}
John McCall375dcc92011-05-28 06:31:34 +0000512
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000513static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
514 switch (intrinsic.ModRef) {
515 case CodeGenIntrinsic::NoMem:
516 return MRK_readnone;
517 case CodeGenIntrinsic::ReadArgMem:
518 case CodeGenIntrinsic::ReadMem:
519 return MRK_readonly;
520 case CodeGenIntrinsic::ReadWriteArgMem:
521 case CodeGenIntrinsic::ReadWriteMem:
522 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000523 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000524 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000525}
526
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000527namespace {
528struct AttributeComparator {
529 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
530 // Sort throwing intrinsics after non-throwing intrinsics.
531 if (L->canThrow != R->canThrow)
532 return R->canThrow;
533
Eli Bendersky2281ef92014-03-18 23:51:07 +0000534 if (L->isNoDuplicate != R->isNoDuplicate)
535 return R->isNoDuplicate;
536
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000537 if (L->isNoReturn != R->isNoReturn)
538 return R->isNoReturn;
539
Owen Anderson85fa7d52015-05-26 23:48:40 +0000540 if (L->isConvergent != R->isConvergent)
541 return R->isConvergent;
542
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000543 // Try to order by readonly/readnone attribute.
544 ModRefKind LK = getModRefKind(*L);
545 ModRefKind RK = getModRefKind(*R);
546 if (LK != RK) return (LK > RK);
547
548 // Order by argument attributes.
549 // This is reliable because each side is already sorted internally.
550 return (L->ArgumentAttributes < R->ArgumentAttributes);
551 }
552};
553} // End anonymous namespace
554
Chris Lattner49b7ee12009-01-12 01:18:58 +0000555/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000556void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000557EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000558 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
559 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000560 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000561 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000562 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000563 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000564 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000565
Craig Topperccd651c2012-02-28 06:32:00 +0000566 // Compute the maximum number of attribute arguments and the map
567 typedef std::map<const CodeGenIntrinsic*, unsigned,
568 AttributeComparator> UniqAttrMapTy;
569 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000570 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000571 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000572 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000573 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000574 maxArgAttrs =
575 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000576 unsigned &N = UniqAttributes[&intrinsic];
577 if (N) continue;
578 assert(AttrNum < 256 && "Too many unique attributes for table!");
579 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000580 }
John McCall375dcc92011-05-28 06:31:34 +0000581
Bill Wendling4d3491c2013-01-27 03:25:05 +0000582 // Emit an array of AttributeSet. Most intrinsics will have at least one
583 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000584 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000585
586 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
587 const CodeGenIntrinsic &intrinsic = Ints[i];
588
589 OS << " " << UniqAttributes[&intrinsic] << ", // "
590 << intrinsic.Name << "\n";
591 }
592 OS << " };\n\n";
593
Bill Wendling4d3491c2013-01-27 03:25:05 +0000594 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000595 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000596 OS << " if (id != 0) {\n";
597 OS << " switch(IntrinsicsToAttributesMap[id - ";
598 if (TargetOnly)
599 OS << "Intrinsic::num_intrinsics";
600 else
601 OS << "1";
602 OS << "]) {\n";
603 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000604 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
605 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000606 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000607
Craig Topperccd651c2012-02-28 06:32:00 +0000608 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000609
610 // Keep track of the number of attributes we're writing out.
611 unsigned numAttrs = 0;
612
613 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000614 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
615 if (ae) {
616 while (ai != ae) {
617 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000618
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000619 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
620 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000621
Bill Wendlinged42e792012-10-10 06:13:42 +0000622 do {
623 switch (intrinsic.ArgumentAttributes[ai].second) {
624 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000625 if (addComma)
626 OS << ",";
627 OS << "Attribute::NoCapture";
628 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000629 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000630 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000631 if (addComma)
632 OS << ",";
633 OS << "Attribute::ReadOnly";
634 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000635 break;
636 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000637 if (addComma)
638 OS << ",";
639 OS << "Attributes::ReadNone";
640 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000641 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000642 }
John McCall375dcc92011-05-28 06:31:34 +0000643
Bill Wendlinged42e792012-10-10 06:13:42 +0000644 ++ai;
645 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000646 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000647 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000648 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000649 }
John McCall375dcc92011-05-28 06:31:34 +0000650 }
651
652 ModRefKind modRef = getModRefKind(intrinsic);
653
Eli Bendersky2281ef92014-03-18 23:51:07 +0000654 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
Owen Anderson85fa7d52015-05-26 23:48:40 +0000655 intrinsic.isNoDuplicate || intrinsic.isConvergent) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000656 OS << " const Attribute::AttrKind Atts[] = {";
657 bool addComma = false;
658 if (!intrinsic.canThrow) {
659 OS << "Attribute::NoUnwind";
660 addComma = true;
661 }
662 if (intrinsic.isNoReturn) {
663 if (addComma)
664 OS << ",";
665 OS << "Attribute::NoReturn";
666 addComma = true;
667 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000668 if (intrinsic.isNoDuplicate) {
669 if (addComma)
670 OS << ",";
671 OS << "Attribute::NoDuplicate";
672 addComma = true;
673 }
Owen Anderson85fa7d52015-05-26 23:48:40 +0000674 if (intrinsic.isConvergent) {
675 if (addComma)
676 OS << ",";
677 OS << "Attribute::Convergent";
678 addComma = true;
679 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000680
John McCall375dcc92011-05-28 06:31:34 +0000681 switch (modRef) {
682 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000683 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000684 if (addComma)
685 OS << ",";
686 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000687 break;
688 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000689 if (addComma)
690 OS << ",";
691 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000692 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000693 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000694 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000695 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000696 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000697 }
John McCall375dcc92011-05-28 06:31:34 +0000698
699 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000700 OS << " NumAttrs = " << numAttrs << ";\n";
701 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000702 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000703 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000704 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000705 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000706 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000707 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000708
Craig Topper374f19c2012-04-13 06:14:57 +0000709 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000710 OS << " }\n";
Craig Toppere1d12942014-08-27 05:25:25 +0000711 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000712 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000713 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000714}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000715
Duncan Sands73247d22009-02-14 10:56:35 +0000716/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
717void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000718EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000719 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
720 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
721 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
722 << "\"Unknown intrinsic.\");\n\n";
723
724 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
Chandler Carruth194f59c2015-07-22 23:15:57 +0000725 << " /* invalid */ FMRB_UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000726 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000727 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000728 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000729 case CodeGenIntrinsic::NoMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000730 OS << "FMRB_DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000731 break;
732 case CodeGenIntrinsic::ReadArgMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000733 OS << "FMRB_OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000734 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000735 case CodeGenIntrinsic::ReadMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000736 OS << "FMRB_OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000737 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000738 case CodeGenIntrinsic::ReadWriteArgMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000739 OS << "FMRB_OnlyAccessesArgumentPointees,\n";
Benjamin Krameraba35032012-03-01 01:18:32 +0000740 break;
741 case CodeGenIntrinsic::ReadWriteMem:
Chandler Carruth194f59c2015-07-22 23:15:57 +0000742 OS << "FMRB_UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000743 break;
744 }
745 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000746 OS << "};\n\n"
Chandler Carruth194f59c2015-07-22 23:15:57 +0000747 << "return "
748 "static_cast<FunctionModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
Benjamin Krameraba35032012-03-01 01:18:32 +0000749 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000750}
751
Chris Lattner69ea0142008-01-04 04:38:35 +0000752/// EmitTargetBuiltins - All of the builtins in the specified map are for the
753/// same target, and we already checked it.
754static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000755 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000756 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000757
Chris Lattner497d13e2010-09-06 03:14:45 +0000758 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000759
Chris Lattner497d13e2010-09-06 03:14:45 +0000760 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
761 E = BIM.end(); I != E; ++I) {
762 std::string ResultCode =
763 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000764 Results.emplace_back(I->first, ResultCode);
Chris Lattner69ea0142008-01-04 04:38:35 +0000765 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000766
767 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000768}
769
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000770
Chris Lattner402a5732006-03-15 01:33:26 +0000771void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000772EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000773 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000774 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000775 BIMTy BuiltinMap;
776 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
777 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000778 // Get the map for this target prefix.
779 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000780
Chris Lattner91678fc2008-01-02 21:24:22 +0000781 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
782 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000783 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
784 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000785 }
786 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000787
Chris Lattner402a5732006-03-15 01:33:26 +0000788 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
789 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
790 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
791 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
792 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000793
Dale Johannesenb842d522009-02-05 01:49:45 +0000794 if (TargetOnly) {
795 OS << "static " << TargetPrefix << "Intrinsic::ID "
796 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000797 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000798 } else {
799 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000800 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000801 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000802
Chris Lattner497d13e2010-09-06 03:14:45 +0000803 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
804 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000805
Chris Lattner402a5732006-03-15 01:33:26 +0000806 // Note: this could emit significantly better code if we cared.
807 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000808 OS << " ";
809 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000810 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000811 else
812 OS << "/* Target Independent Builtins */ ";
813 OS << "{\n";
814
Chris Lattner91678fc2008-01-02 21:24:22 +0000815 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000816 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000817 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000818 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000819 OS << " return ";
820 if (!TargetPrefix.empty())
821 OS << "(" << TargetPrefix << "Intrinsic::ID)";
822 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000823 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000824 OS << "#endif\n\n";
825}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000826
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000827void IntrinsicEmitter::
828EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
829 raw_ostream &OS) {
830 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
831
832 for (const auto &Intrinsic : Ints) {
833 if (Intrinsic.MSBuiltinName.empty())
834 continue;
835
836 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
837 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
838 Intrinsic.EnumName)).second)
839 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
840 "duplicate MS builtin name!");
841 }
842
843 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
844 "// This is used by the C front-end. The MS builtin name is passed\n"
845 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
846 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
847 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
848
849 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
850 << (TargetOnly ? "" : "Intrinsic::")
851 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
852 OS << " StringRef BuiltinName(BN);\n"
853 " StringRef TargetPrefix(TP);\n"
854 "\n";
855
856 for (const auto &Builtins : TargetBuiltins) {
857 OS << " ";
858 if (Builtins.first.empty())
859 OS << "/* Target Independent Builtins */ ";
860 else
861 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
862 OS << "{\n";
863 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
864 OS << "}";
865 }
866
867 OS << " return ";
868 if (!TargetPrefix.empty())
869 OS << "(" << TargetPrefix << "Intrinsic::ID)";
870 OS << "Intrinsic::not_intrinsic;\n";
871 OS << "}\n";
872
873 OS << "#endif\n\n";
874}
875
Richard Smith6bc9df32014-04-20 20:26:39 +0000876void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000877 IntrinsicEmitter(RK, TargetOnly).run(OS);
878}