blob: 34358c41a742cb234820439ba05f8d1203de81d1 [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 {
Chris Lattner827b2532012-05-17 04:30:58 +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,
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000243 IIT_PTR = 14,
244 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000245
Chris Lattner827b2532012-05-17 04:30:58 +0000246 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000247 IIT_MMX = 16,
248 IIT_METADATA = 17,
249 IIT_EMPTYSTRUCT = 18,
250 IIT_STRUCT2 = 19,
251 IIT_STRUCT3 = 20,
252 IIT_STRUCT4 = 21,
253 IIT_STRUCT5 = 22,
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000254 IIT_EXTEND_ARG = 23,
255 IIT_TRUNC_ARG = 24,
Jiangning Liu63dc8402013-09-24 02:47:27 +0000256 IIT_ANYPTR = 25,
Andrew Tricka2efd992013-10-31 17:18:11 +0000257 IIT_V1 = 26,
Tim Northover4516de32014-03-29 07:04:54 +0000258 IIT_VARARG = 27,
259 IIT_HALF_VEC_ARG = 28
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000260};
261
Chris Lattner827b2532012-05-17 04:30:58 +0000262
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000263static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000264 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000265 if (MVT(VT).isInteger()) {
266 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000267 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000268 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000269 case 1: return Sig.push_back(IIT_I1);
270 case 8: return Sig.push_back(IIT_I8);
271 case 16: return Sig.push_back(IIT_I16);
272 case 32: return Sig.push_back(IIT_I32);
273 case 64: return Sig.push_back(IIT_I64);
274 }
275 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000276
Chris Lattner827b2532012-05-17 04:30:58 +0000277 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000278 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000279 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000280 case MVT::f32: return Sig.push_back(IIT_F32);
281 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000282 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
283 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
284 // MVT::OtherVT is used to mean the empty struct type here.
285 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000286 // MVT::isVoid is used to represent varargs here.
287 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000288 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000289}
290
Francois Pichet9522bfc2012-05-17 04:00:03 +0000291#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000292#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000293#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000294
Chris Lattnerc4644162012-05-27 16:39:08 +0000295static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000296 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000297
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000298 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000299 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000300 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000301 if (R->isSubClassOf("LLVMExtendedType"))
302 Sig.push_back(IIT_EXTEND_ARG);
303 else if (R->isSubClassOf("LLVMTruncatedType"))
304 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000305 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
306 Sig.push_back(IIT_HALF_VEC_ARG);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000307 else
308 Sig.push_back(IIT_ARG);
Chris Lattnerc4644162012-05-27 16:39:08 +0000309 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000310 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000311
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000312 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000313
Chris Lattnerc4644162012-05-27 16:39:08 +0000314 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000315 switch (VT) {
316 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000317 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
318 case MVT::vAny: ++Tmp; // FALL THROUGH.
319 case MVT::fAny: ++Tmp; // FALL THROUGH.
320 case MVT::iAny: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000321 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000322 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000323 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000324
Chris Lattnerc4644162012-05-27 16:39:08 +0000325 // Figure out what arg # this is consuming, and remember what kind it was.
326 unsigned ArgNo = ArgCodes.size();
327 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000328
Chris Lattnerc4644162012-05-27 16:39:08 +0000329 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
330 return Sig.push_back((ArgNo << 2) | Tmp);
331 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000332
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000333 case MVT::iPTR: {
334 unsigned AddrSpace = 0;
335 if (R->isSubClassOf("LLVMQualPointerType")) {
336 AddrSpace = R->getValueAsInt("AddrSpace");
337 assert(AddrSpace < 256 && "Address space exceeds 255");
338 }
339 if (AddrSpace) {
340 Sig.push_back(IIT_ANYPTR);
341 Sig.push_back(AddrSpace);
342 } else {
343 Sig.push_back(IIT_PTR);
344 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000345 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000346 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000347 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000348
Craig Topper8561de92014-01-24 20:50:47 +0000349 if (MVT(VT).isVector()) {
350 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000351 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000352 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000353 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000354 case 2: Sig.push_back(IIT_V2); break;
355 case 4: Sig.push_back(IIT_V4); break;
356 case 8: Sig.push_back(IIT_V8); break;
357 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000358 case 32: Sig.push_back(IIT_V32); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000359 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000360
Craig Topper8561de92014-01-24 20:50:47 +0000361 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000362 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000363
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000364 EncodeFixedValueType(VT, Sig);
365}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000366
367#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000368#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000369#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000370
371/// ComputeFixedEncoding - If we can encode the type signature for this
372/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000373static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
374 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000375 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000376
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000377 if (Int.IS.RetVTs.empty())
378 TypeSig.push_back(IIT_Done);
379 else if (Int.IS.RetVTs.size() == 1 &&
380 Int.IS.RetVTs[0] == MVT::isVoid)
381 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000382 else {
383 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000384 case 1: break;
385 case 2: TypeSig.push_back(IIT_STRUCT2); break;
386 case 3: TypeSig.push_back(IIT_STRUCT3); break;
387 case 4: TypeSig.push_back(IIT_STRUCT4); break;
388 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000389 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000390 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000391
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000392 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000393 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000394 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000395
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000396 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000397 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000398}
399
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000400static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000401 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000402}
403
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000404void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000405 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000406 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
407 // capture it in this vector, otherwise store a ~0U.
408 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000409
Chris Lattnera3b0f522012-05-17 15:55:41 +0000410 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000411
Chris Lattnera3b0f522012-05-17 15:55:41 +0000412 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000413
Jim Laskey2682ea62007-02-07 20:38:26 +0000414 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000415 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000416 // Get the signature for the intrinsic.
417 TypeSig.clear();
418 ComputeFixedEncoding(Ints[i], TypeSig);
419
420 // Check to see if we can encode it into a 32-bit word. We can only encode
421 // 8 nibbles into a 32-bit word.
422 if (TypeSig.size() <= 8) {
423 bool Failed = false;
424 unsigned Result = 0;
425 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
426 // If we had an unencodable argument, bail out.
427 if (TypeSig[i] > 15) {
428 Failed = true;
429 break;
430 }
431 Result = (Result << 4) | TypeSig[e-i-1];
432 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000433
Chris Lattnera3b0f522012-05-17 15:55:41 +0000434 // If this could be encoded into a 31-bit word, return it.
435 if (!Failed && (Result >> 31) == 0) {
436 FixedEncodings.push_back(Result);
437 continue;
438 }
439 }
440
441 // Otherwise, we're going to unique the sequence into the
442 // LongEncodingTable, and use its offset in the 32-bit table instead.
443 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000444
Chris Lattnera3b0f522012-05-17 15:55:41 +0000445 // This is a placehold that we'll replace after the table is laid out.
446 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000447 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000448
Chris Lattnera3b0f522012-05-17 15:55:41 +0000449 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000450
Chris Lattnerf39c2782012-05-27 18:28:35 +0000451 OS << "// Global intrinsic function declaration type table.\n";
452 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
453
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000454 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000455
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000456 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
457 if ((i & 7) == 7)
458 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000459
Chris Lattnera3b0f522012-05-17 15:55:41 +0000460 // If the entry fit in the table, just emit it.
461 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000462 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000463 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000464 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000465
Chris Lattnera3b0f522012-05-17 15:55:41 +0000466 TypeSig.clear();
467 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000468
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000469
Chris Lattnera3b0f522012-05-17 15:55:41 +0000470 // Otherwise, emit the offset into the long encoding table. We emit it this
471 // way so that it is easier to read the offset in the .def file.
472 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000473 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000474
Chris Lattnera3b0f522012-05-17 15:55:41 +0000475 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000476
Chris Lattnera3b0f522012-05-17 15:55:41 +0000477 // Emit the shared table of register lists.
478 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
479 if (!LongEncodingTable.empty())
480 LongEncodingTable.emit(OS, printIITEntry);
481 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000482
Patrik Hägglundca210d82012-05-23 12:34:56 +0000483 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000484}
485
Richard Smith6bc9df32014-04-20 20:26:39 +0000486namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000487enum ModRefKind {
488 MRK_none,
489 MRK_readonly,
490 MRK_readnone
491};
Richard Smith6bc9df32014-04-20 20:26:39 +0000492}
John McCall375dcc92011-05-28 06:31:34 +0000493
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000494static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
495 switch (intrinsic.ModRef) {
496 case CodeGenIntrinsic::NoMem:
497 return MRK_readnone;
498 case CodeGenIntrinsic::ReadArgMem:
499 case CodeGenIntrinsic::ReadMem:
500 return MRK_readonly;
501 case CodeGenIntrinsic::ReadWriteArgMem:
502 case CodeGenIntrinsic::ReadWriteMem:
503 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000504 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000505 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000506}
507
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000508namespace {
509struct AttributeComparator {
510 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
511 // Sort throwing intrinsics after non-throwing intrinsics.
512 if (L->canThrow != R->canThrow)
513 return R->canThrow;
514
Eli Bendersky2281ef92014-03-18 23:51:07 +0000515 if (L->isNoDuplicate != R->isNoDuplicate)
516 return R->isNoDuplicate;
517
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000518 if (L->isNoReturn != R->isNoReturn)
519 return R->isNoReturn;
520
521 // Try to order by readonly/readnone attribute.
522 ModRefKind LK = getModRefKind(*L);
523 ModRefKind RK = getModRefKind(*R);
524 if (LK != RK) return (LK > RK);
525
526 // Order by argument attributes.
527 // This is reliable because each side is already sorted internally.
528 return (L->ArgumentAttributes < R->ArgumentAttributes);
529 }
530};
531} // End anonymous namespace
532
Chris Lattner49b7ee12009-01-12 01:18:58 +0000533/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000534void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000535EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000536 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
537 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000538 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000539 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000540 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000541 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000542 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000543
Craig Topperccd651c2012-02-28 06:32:00 +0000544 // Compute the maximum number of attribute arguments and the map
545 typedef std::map<const CodeGenIntrinsic*, unsigned,
546 AttributeComparator> UniqAttrMapTy;
547 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000548 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000549 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000550 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000551 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000552 maxArgAttrs =
553 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000554 unsigned &N = UniqAttributes[&intrinsic];
555 if (N) continue;
556 assert(AttrNum < 256 && "Too many unique attributes for table!");
557 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000558 }
John McCall375dcc92011-05-28 06:31:34 +0000559
Bill Wendling4d3491c2013-01-27 03:25:05 +0000560 // Emit an array of AttributeSet. Most intrinsics will have at least one
561 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000562 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000563
564 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
565 const CodeGenIntrinsic &intrinsic = Ints[i];
566
567 OS << " " << UniqAttributes[&intrinsic] << ", // "
568 << intrinsic.Name << "\n";
569 }
570 OS << " };\n\n";
571
Bill Wendling4d3491c2013-01-27 03:25:05 +0000572 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000573 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000574 OS << " if (id != 0) {\n";
575 OS << " switch(IntrinsicsToAttributesMap[id - ";
576 if (TargetOnly)
577 OS << "Intrinsic::num_intrinsics";
578 else
579 OS << "1";
580 OS << "]) {\n";
581 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000582 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
583 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000584 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000585
Craig Topperccd651c2012-02-28 06:32:00 +0000586 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000587
588 // Keep track of the number of attributes we're writing out.
589 unsigned numAttrs = 0;
590
591 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000592 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
593 if (ae) {
594 while (ai != ae) {
595 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000596
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000597 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
598 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000599
Bill Wendlinged42e792012-10-10 06:13:42 +0000600 do {
601 switch (intrinsic.ArgumentAttributes[ai].second) {
602 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000603 if (addComma)
604 OS << ",";
605 OS << "Attribute::NoCapture";
606 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000607 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000608 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000609 if (addComma)
610 OS << ",";
611 OS << "Attribute::ReadOnly";
612 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000613 break;
614 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000615 if (addComma)
616 OS << ",";
617 OS << "Attributes::ReadNone";
618 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000619 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000620 }
John McCall375dcc92011-05-28 06:31:34 +0000621
Bill Wendlinged42e792012-10-10 06:13:42 +0000622 ++ai;
623 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000624 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000625 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000626 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000627 }
John McCall375dcc92011-05-28 06:31:34 +0000628 }
629
630 ModRefKind modRef = getModRefKind(intrinsic);
631
Eli Bendersky2281ef92014-03-18 23:51:07 +0000632 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
633 intrinsic.isNoDuplicate) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000634 OS << " const Attribute::AttrKind Atts[] = {";
635 bool addComma = false;
636 if (!intrinsic.canThrow) {
637 OS << "Attribute::NoUnwind";
638 addComma = true;
639 }
640 if (intrinsic.isNoReturn) {
641 if (addComma)
642 OS << ",";
643 OS << "Attribute::NoReturn";
644 addComma = true;
645 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000646 if (intrinsic.isNoDuplicate) {
647 if (addComma)
648 OS << ",";
649 OS << "Attribute::NoDuplicate";
650 addComma = true;
651 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000652
John McCall375dcc92011-05-28 06:31:34 +0000653 switch (modRef) {
654 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000655 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000656 if (addComma)
657 OS << ",";
658 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000659 break;
660 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000661 if (addComma)
662 OS << ",";
663 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000664 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000665 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000666 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000667 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000668 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000669 }
John McCall375dcc92011-05-28 06:31:34 +0000670
671 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000672 OS << " NumAttrs = " << numAttrs << ";\n";
673 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000674 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000675 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000676 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000677 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000678 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000679 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000680
Craig Topper374f19c2012-04-13 06:14:57 +0000681 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000682 OS << " }\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000683 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
Chris Lattner3cb6f832012-05-28 01:47:44 +0000684 "NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000685 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000686 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000687}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000688
Duncan Sands73247d22009-02-14 10:56:35 +0000689/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
690void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000691EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000692 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
693 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
694 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
695 << "\"Unknown intrinsic.\");\n\n";
696
697 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
698 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000699 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000700 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000701 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000702 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000703 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000704 break;
705 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000706 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000707 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000708 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000709 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000710 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000711 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000712 OS << "OnlyAccessesArgumentPointees,\n";
713 break;
714 case CodeGenIntrinsic::ReadWriteMem:
715 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000716 break;
717 }
718 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000719 OS << "};\n\n"
720 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
721 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000722}
723
Chris Lattner69ea0142008-01-04 04:38:35 +0000724/// EmitTargetBuiltins - All of the builtins in the specified map are for the
725/// same target, and we already checked it.
726static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000727 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000728 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000729
Chris Lattner497d13e2010-09-06 03:14:45 +0000730 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000731
Chris Lattner497d13e2010-09-06 03:14:45 +0000732 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
733 E = BIM.end(); I != E; ++I) {
734 std::string ResultCode =
735 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
736 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000737 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000738
739 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000740}
741
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000742
Chris Lattner402a5732006-03-15 01:33:26 +0000743void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000744EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000745 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000746 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000747 BIMTy BuiltinMap;
748 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
749 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000750 // Get the map for this target prefix.
751 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000752
Chris Lattner91678fc2008-01-02 21:24:22 +0000753 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
754 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000755 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
756 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000757 }
758 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000759
Chris Lattner402a5732006-03-15 01:33:26 +0000760 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
761 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
762 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
763 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
764 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000765
Dale Johannesenb842d522009-02-05 01:49:45 +0000766 if (TargetOnly) {
767 OS << "static " << TargetPrefix << "Intrinsic::ID "
768 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000769 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000770 } else {
771 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000772 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000773 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000774
Chris Lattner497d13e2010-09-06 03:14:45 +0000775 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
776 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000777
Chris Lattner402a5732006-03-15 01:33:26 +0000778 // Note: this could emit significantly better code if we cared.
779 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000780 OS << " ";
781 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000782 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000783 else
784 OS << "/* Target Independent Builtins */ ";
785 OS << "{\n";
786
Chris Lattner91678fc2008-01-02 21:24:22 +0000787 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000788 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000789 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000790 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000791 OS << " return ";
792 if (!TargetPrefix.empty())
793 OS << "(" << TargetPrefix << "Intrinsic::ID)";
794 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000795 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000796 OS << "#endif\n\n";
797}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000798
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000799void IntrinsicEmitter::
800EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
801 raw_ostream &OS) {
802 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
803
804 for (const auto &Intrinsic : Ints) {
805 if (Intrinsic.MSBuiltinName.empty())
806 continue;
807
808 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
809 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
810 Intrinsic.EnumName)).second)
811 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
812 "duplicate MS builtin name!");
813 }
814
815 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
816 "// This is used by the C front-end. The MS builtin name is passed\n"
817 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
818 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
819 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
820
821 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
822 << (TargetOnly ? "" : "Intrinsic::")
823 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
824 OS << " StringRef BuiltinName(BN);\n"
825 " StringRef TargetPrefix(TP);\n"
826 "\n";
827
828 for (const auto &Builtins : TargetBuiltins) {
829 OS << " ";
830 if (Builtins.first.empty())
831 OS << "/* Target Independent Builtins */ ";
832 else
833 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
834 OS << "{\n";
835 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
836 OS << "}";
837 }
838
839 OS << " return ";
840 if (!TargetPrefix.empty())
841 OS << "(" << TargetPrefix << "Intrinsic::ID)";
842 OS << "Intrinsic::not_intrinsic;\n";
843 OS << "}\n";
844
845 OS << "#endif\n\n";
846}
847
Richard Smith6bc9df32014-04-20 20:26:39 +0000848void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000849 IntrinsicEmitter(RK, TargetOnly).run(OS);
850}