blob: faf347088008d2e944a24dfb32fa3b99fa67903c [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"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000017#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000018#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000019#include "llvm/TableGen/Record.h"
Douglas Gregor12c1cd32012-05-02 17:32:48 +000020#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenc4e24682006-03-15 02:51:05 +000022#include <algorithm>
Chris Lattnerc313d0b2006-03-03 02:32:46 +000023using namespace llvm;
24
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025namespace {
26class IntrinsicEmitter {
27 RecordKeeper &Records;
28 bool TargetOnly;
29 std::string TargetPrefix;
30
31public:
32 IntrinsicEmitter(RecordKeeper &R, bool T)
33 : Records(R), TargetOnly(T) {}
34
35 void run(raw_ostream &OS);
36
37 void EmitPrefix(raw_ostream &OS);
38
39 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
40 raw_ostream &OS);
41
42 void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
43 raw_ostream &OS);
44 void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
45 raw_ostream &OS);
46 void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
47 raw_ostream &OS);
48 void EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
49 raw_ostream &OS);
50 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
51 raw_ostream &OS);
52 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
53 raw_ostream &OS);
54 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
55 raw_ostream &OS);
56 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
57 raw_ostream &OS);
58 void EmitSuffix(raw_ostream &OS);
59};
60} // End anonymous namespace
61
Chris Lattnerc313d0b2006-03-03 02:32:46 +000062//===----------------------------------------------------------------------===//
Chris Lattnerc313d0b2006-03-03 02:32:46 +000063// IntrinsicEmitter Implementation
64//===----------------------------------------------------------------------===//
65
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000066void IntrinsicEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000067 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
68
Dale Johannesenb842d522009-02-05 01:49:45 +000069 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000070
Dale Johannesenb842d522009-02-05 01:49:45 +000071 if (TargetOnly && !Ints.empty())
72 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattnerc313d0b2006-03-03 02:32:46 +000073
Douglas Gregor6739a892010-05-11 06:17:44 +000074 EmitPrefix(OS);
75
Chris Lattnerc313d0b2006-03-03 02:32:46 +000076 // Emit the enum information.
77 EmitEnumInfo(Ints, OS);
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000078
79 // Emit the intrinsic ID -> name table.
80 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wangb4024932009-02-24 23:17:49 +000081
82 // Emit the intrinsic ID -> overload table.
83 EmitIntrinsicToOverloadTable(Ints, OS);
84
Chris Lattner6d8104e2006-03-09 20:34:19 +000085 // Emit the function name recognizer.
86 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000087
Jim Laskey2682ea62007-02-07 20:38:26 +000088 // Emit the intrinsic declaration generator.
89 EmitGenerator(Ints, OS);
90
Duncan Sands38ef3a82007-12-03 20:06:50 +000091 // Emit the intrinsic parameter attributes.
92 EmitAttributes(Ints, OS);
Chris Lattnerfea17a92006-03-13 23:08:44 +000093
Duncan Sands73247d22009-02-14 10:56:35 +000094 // Emit intrinsic alias analysis mod/ref behavior.
95 EmitModRefBehavior(Ints, OS);
96
Chris Lattner402a5732006-03-15 01:33:26 +000097 // Emit code to translate GCC builtins into LLVM intrinsics.
98 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor6739a892010-05-11 06:17:44 +000099
100 EmitSuffix(OS);
101}
102
103void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
104 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000105 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
106 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000107 "# pragma push_macro(\"setjmp\")\n"
108 "# undef setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000109 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000110 "#endif\n\n";
111}
112
113void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencerded5f662010-09-24 19:48:47 +0000114 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000115 "// let's return it to _setjmp state\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000116 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000117 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000118 "#endif\n\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000119}
120
121void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000122 raw_ostream &OS) {
Chris Lattner6d8104e2006-03-09 20:34:19 +0000123 OS << "// Enum values for Intrinsics.h\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000124 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
125 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
126 OS << " " << Ints[i].EnumName;
127 OS << ((i != e-1) ? ", " : " ");
128 OS << std::string(40-Ints[i].EnumName.size(), ' ')
129 << "// " << Ints[i].Name << "\n";
130 }
131 OS << "#endif\n\n";
132}
Chris Lattner6d8104e2006-03-09 20:34:19 +0000133
134void IntrinsicEmitter::
135EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000136 raw_ostream &OS) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000137 // Build a 'first character of function name' -> intrinsic # mapping.
138 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner6d8104e2006-03-09 20:34:19 +0000139 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattner07b332f2010-09-06 03:58:45 +0000140 IntMapping[Ints[i].Name[5]].push_back(i);
141
Chris Lattner6d8104e2006-03-09 20:34:19 +0000142 OS << "// Function name -> enum value recognizer code.\n";
143 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattner07b332f2010-09-06 03:58:45 +0000144 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
145 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
146 OS << " default: break;\n";
147 // Emit the intrinsic matching stuff by first letter.
148 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner6d8104e2006-03-09 20:34:19 +0000149 E = IntMapping.end(); I != E; ++I) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000150 OS << " case '" << I->first << "':\n";
151 std::vector<unsigned> &IntList = I->second;
152
153 // Emit all the overloaded intrinsics first, build a table of the
154 // non-overloaded ones.
155 std::vector<StringMatcher::StringPair> MatchTable;
156
157 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
158 unsigned IntNo = IntList[i];
159 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
160 Ints[IntNo].EnumName + ";";
161
162 if (!Ints[IntNo].isOverloaded) {
163 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
164 continue;
165 }
166
167 // For overloaded intrinsics, only the prefix needs to match
168 std::string TheStr = Ints[IntNo].Name.substr(6);
169 TheStr += '.'; // Require "bswap." instead of bswap.
170 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
171 << Result << '\n';
Chris Lattner6d8104e2006-03-09 20:34:19 +0000172 }
173
Chris Lattner07b332f2010-09-06 03:58:45 +0000174 // Emit the matcher logic for the fixed length strings.
175 StringMatcher("NameR", MatchTable, OS).Emit(1);
176 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner6d8104e2006-03-09 20:34:19 +0000177 }
Chris Lattner07b332f2010-09-06 03:58:45 +0000178
Chris Lattner6d8104e2006-03-09 20:34:19 +0000179 OS << " }\n";
Chris Lattner6efe8632006-03-09 22:05:04 +0000180 OS << "#endif\n\n";
181}
182
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000183void IntrinsicEmitter::
184EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000185 raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000186 OS << "// Intrinsic ID to name table\n";
187 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
188 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000189 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
190 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000191 OS << "#endif\n\n";
192}
193
Mon P Wangb4024932009-02-24 23:17:49 +0000194void IntrinsicEmitter::
195EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000196 raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000197 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000198 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000199 OS << "static const uint8_t OTable[] = {\n";
200 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000201 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000202 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
203 if ((i+1)%8 == 0)
204 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000205 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000206 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000207 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000208 OS << "\n};\n\n";
209 // OTable contains a true bit at the position if the intrinsic is overloaded.
210 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000211 OS << "#endif\n\n";
212}
213
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000214
Chris Lattnerf39c2782012-05-27 18:28:35 +0000215// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000216enum IIT_Info {
Chris Lattner827b2532012-05-17 04:30:58 +0000217 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000218 IIT_Done = 0,
219 IIT_I1 = 1,
220 IIT_I8 = 2,
221 IIT_I16 = 3,
222 IIT_I32 = 4,
223 IIT_I64 = 5,
224 IIT_F32 = 6,
225 IIT_F64 = 7,
226 IIT_V2 = 8,
227 IIT_V4 = 9,
228 IIT_V8 = 10,
229 IIT_V16 = 11,
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000230 IIT_V32 = 12,
231 IIT_MMX = 13,
232 IIT_PTR = 14,
233 IIT_ARG = 15,
Chris Lattner827b2532012-05-17 04:30:58 +0000234
235 // Values from 16+ are only encodable with the inefficient encoding.
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000236 IIT_METADATA = 16,
237 IIT_EMPTYSTRUCT = 17,
238 IIT_STRUCT2 = 18,
239 IIT_STRUCT3 = 19,
240 IIT_STRUCT4 = 20,
241 IIT_STRUCT5 = 21,
242 IIT_EXTEND_VEC_ARG = 22,
Chris Lattner4f18aa82012-05-23 05:19:18 +0000243 IIT_TRUNC_VEC_ARG = 23,
244 IIT_ANYPTR = 24
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000245};
246
Chris Lattner827b2532012-05-17 04:30:58 +0000247
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000248static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000249 std::vector<unsigned char> &Sig) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000250 if (EVT(VT).isInteger()) {
251 unsigned BitWidth = EVT(VT).getSizeInBits();
252 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000253 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000254 case 1: return Sig.push_back(IIT_I1);
255 case 8: return Sig.push_back(IIT_I8);
256 case 16: return Sig.push_back(IIT_I16);
257 case 32: return Sig.push_back(IIT_I32);
258 case 64: return Sig.push_back(IIT_I64);
259 }
260 }
261
Chris Lattner827b2532012-05-17 04:30:58 +0000262 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000263 default: PrintFatalError("unhandled MVT in intrinsic!");
Chris Lattner827b2532012-05-17 04:30:58 +0000264 case MVT::f32: return Sig.push_back(IIT_F32);
265 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000266 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
267 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
268 // MVT::OtherVT is used to mean the empty struct type here.
269 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
270 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000271}
272
Francois Pichet9522bfc2012-05-17 04:00:03 +0000273#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000274#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Francois Pichet9522bfc2012-05-17 04:00:03 +0000275#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000276
Chris Lattnerc4644162012-05-27 16:39:08 +0000277static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000278 std::vector<unsigned char> &Sig) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000279
280 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000281 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000282 assert(Number < ArgCodes.size() && "Invalid matching number!");
Chris Lattner827b2532012-05-17 04:30:58 +0000283 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000284 Sig.push_back(IIT_EXTEND_VEC_ARG);
285 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
286 Sig.push_back(IIT_TRUNC_VEC_ARG);
287 else
288 Sig.push_back(IIT_ARG);
Chris Lattnerc4644162012-05-27 16:39:08 +0000289 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000290 }
291
292 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000293
Chris Lattnerc4644162012-05-27 16:39:08 +0000294 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000295 switch (VT) {
296 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000297 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
298 case MVT::vAny: ++Tmp; // FALL THROUGH.
299 case MVT::fAny: ++Tmp; // FALL THROUGH.
300 case MVT::iAny: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000301 // If this is an "any" valuetype, then the type is the type of the next
302 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000303 Sig.push_back(IIT_ARG);
Chris Lattnerc4644162012-05-27 16:39:08 +0000304
305 // Figure out what arg # this is consuming, and remember what kind it was.
306 unsigned ArgNo = ArgCodes.size();
307 ArgCodes.push_back(Tmp);
308
309 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
310 return Sig.push_back((ArgNo << 2) | Tmp);
311 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000312
313 case MVT::iPTR: {
314 unsigned AddrSpace = 0;
315 if (R->isSubClassOf("LLVMQualPointerType")) {
316 AddrSpace = R->getValueAsInt("AddrSpace");
317 assert(AddrSpace < 256 && "Address space exceeds 255");
318 }
319 if (AddrSpace) {
320 Sig.push_back(IIT_ANYPTR);
321 Sig.push_back(AddrSpace);
322 } else {
323 Sig.push_back(IIT_PTR);
324 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000325 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000326 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000327 }
328
329 if (EVT(VT).isVector()) {
330 EVT VVT = VT;
331 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000332 default: PrintFatalError("unhandled vector type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000333 case 2: Sig.push_back(IIT_V2); break;
334 case 4: Sig.push_back(IIT_V4); break;
335 case 8: Sig.push_back(IIT_V8); break;
336 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000337 case 32: Sig.push_back(IIT_V32); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000338 }
339
340 return EncodeFixedValueType(VVT.getVectorElementType().
341 getSimpleVT().SimpleTy, Sig);
342 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000343
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000344 EncodeFixedValueType(VT, Sig);
345}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000346
347#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000348#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000349#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000350
351/// ComputeFixedEncoding - If we can encode the type signature for this
352/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000353static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
354 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000355 std::vector<unsigned char> ArgCodes;
Chris Lattner827b2532012-05-17 04:30:58 +0000356
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000357 if (Int.IS.RetVTs.empty())
358 TypeSig.push_back(IIT_Done);
359 else if (Int.IS.RetVTs.size() == 1 &&
360 Int.IS.RetVTs[0] == MVT::isVoid)
361 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000362 else {
363 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000364 case 1: break;
365 case 2: TypeSig.push_back(IIT_STRUCT2); break;
366 case 3: TypeSig.push_back(IIT_STRUCT3); break;
367 case 4: TypeSig.push_back(IIT_STRUCT4); break;
368 case 5: TypeSig.push_back(IIT_STRUCT5); break;
369 default: assert(0 && "Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000370 }
Chris Lattnera3b0f522012-05-17 15:55:41 +0000371
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000372 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000373 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000374 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000375
376 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000377 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000378}
379
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000380static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000381 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000382}
383
Jim Laskey2682ea62007-02-07 20:38:26 +0000384void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000385 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000386 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
387 // capture it in this vector, otherwise store a ~0U.
388 std::vector<unsigned> FixedEncodings;
Jim Laskey2682ea62007-02-07 20:38:26 +0000389
Chris Lattnera3b0f522012-05-17 15:55:41 +0000390 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
391
392 std::vector<unsigned char> TypeSig;
393
Jim Laskey2682ea62007-02-07 20:38:26 +0000394 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000395 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000396 // Get the signature for the intrinsic.
397 TypeSig.clear();
398 ComputeFixedEncoding(Ints[i], TypeSig);
399
400 // Check to see if we can encode it into a 32-bit word. We can only encode
401 // 8 nibbles into a 32-bit word.
402 if (TypeSig.size() <= 8) {
403 bool Failed = false;
404 unsigned Result = 0;
405 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
406 // If we had an unencodable argument, bail out.
407 if (TypeSig[i] > 15) {
408 Failed = true;
409 break;
410 }
411 Result = (Result << 4) | TypeSig[e-i-1];
412 }
413
414 // If this could be encoded into a 31-bit word, return it.
415 if (!Failed && (Result >> 31) == 0) {
416 FixedEncodings.push_back(Result);
417 continue;
418 }
419 }
420
421 // Otherwise, we're going to unique the sequence into the
422 // LongEncodingTable, and use its offset in the 32-bit table instead.
423 LongEncodingTable.add(TypeSig);
424
425 // This is a placehold that we'll replace after the table is laid out.
426 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000427 }
428
Chris Lattnera3b0f522012-05-17 15:55:41 +0000429 LongEncodingTable.layout();
430
Chris Lattnerf39c2782012-05-27 18:28:35 +0000431 OS << "// Global intrinsic function declaration type table.\n";
432 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
433
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000434 OS << "static const unsigned IIT_Table[] = {\n ";
435
436 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
437 if ((i & 7) == 7)
438 OS << "\n ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000439
440 // If the entry fit in the table, just emit it.
441 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000442 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000443 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000444 }
Chris Lattnera3b0f522012-05-17 15:55:41 +0000445
446 TypeSig.clear();
447 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000448
Chris Lattnera3b0f522012-05-17 15:55:41 +0000449
450 // Otherwise, emit the offset into the long encoding table. We emit it this
451 // way so that it is easier to read the offset in the .def file.
452 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000453 }
Chris Lattnera3b0f522012-05-17 15:55:41 +0000454
455 OS << "0\n};\n\n";
456
457 // Emit the shared table of register lists.
458 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
459 if (!LongEncodingTable.empty())
460 LongEncodingTable.emit(OS, printIITEntry);
461 OS << " 255\n};\n\n";
462
Patrik Hägglundca210d82012-05-23 12:34:56 +0000463 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000464}
465
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000466enum ModRefKind {
467 MRK_none,
468 MRK_readonly,
469 MRK_readnone
470};
John McCall375dcc92011-05-28 06:31:34 +0000471
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000472static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
473 switch (intrinsic.ModRef) {
474 case CodeGenIntrinsic::NoMem:
475 return MRK_readnone;
476 case CodeGenIntrinsic::ReadArgMem:
477 case CodeGenIntrinsic::ReadMem:
478 return MRK_readonly;
479 case CodeGenIntrinsic::ReadWriteArgMem:
480 case CodeGenIntrinsic::ReadWriteMem:
481 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000482 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000483 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000484}
485
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000486namespace {
487struct AttributeComparator {
488 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
489 // Sort throwing intrinsics after non-throwing intrinsics.
490 if (L->canThrow != R->canThrow)
491 return R->canThrow;
492
493 if (L->isNoReturn != R->isNoReturn)
494 return R->isNoReturn;
495
496 // Try to order by readonly/readnone attribute.
497 ModRefKind LK = getModRefKind(*L);
498 ModRefKind RK = getModRefKind(*R);
499 if (LK != RK) return (LK > RK);
500
501 // Order by argument attributes.
502 // This is reliable because each side is already sorted internally.
503 return (L->ArgumentAttributes < R->ArgumentAttributes);
504 }
505};
506} // End anonymous namespace
507
Chris Lattner49b7ee12009-01-12 01:18:58 +0000508/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000509void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000510EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000511 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
512 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000513 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000514 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000515 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000516 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000517 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000518
Craig Topperccd651c2012-02-28 06:32:00 +0000519 // Compute the maximum number of attribute arguments and the map
520 typedef std::map<const CodeGenIntrinsic*, unsigned,
521 AttributeComparator> UniqAttrMapTy;
522 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000523 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000524 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000525 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000526 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000527 maxArgAttrs =
528 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000529 unsigned &N = UniqAttributes[&intrinsic];
530 if (N) continue;
531 assert(AttrNum < 256 && "Too many unique attributes for table!");
532 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000533 }
John McCall375dcc92011-05-28 06:31:34 +0000534
535 // Emit an array of AttributeWithIndex. Most intrinsics will have
536 // at least one entry, for the function itself (index ~1), which is
537 // usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000538 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000539
540 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
541 const CodeGenIntrinsic &intrinsic = Ints[i];
542
543 OS << " " << UniqAttributes[&intrinsic] << ", // "
544 << intrinsic.Name << "\n";
545 }
546 OS << " };\n\n";
547
John McCall375dcc92011-05-28 06:31:34 +0000548 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000549 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000550 OS << " if (id != 0) {\n";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000551 OS << " SmallVector<Attribute::AttrVal, 8> AttrVec;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000552 OS << " switch(IntrinsicsToAttributesMap[id - ";
553 if (TargetOnly)
554 OS << "Intrinsic::num_intrinsics";
555 else
556 OS << "1";
557 OS << "]) {\n";
558 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000559 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
560 E = UniqAttributes.end(); I != E; ++I) {
Craig Topper374f19c2012-04-13 06:14:57 +0000561 OS << " case " << I->second << ":\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000562
Craig Topperccd651c2012-02-28 06:32:00 +0000563 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000564
565 // Keep track of the number of attributes we're writing out.
566 unsigned numAttrs = 0;
567
568 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000569 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
570 if (ae) {
571 while (ai != ae) {
572 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000573
Bill Wendlinged42e792012-10-10 06:13:42 +0000574 OS << " AttrVec.clear();\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000575
Bill Wendlinged42e792012-10-10 06:13:42 +0000576 do {
577 switch (intrinsic.ArgumentAttributes[ai].second) {
578 case CodeGenIntrinsic::NoCapture:
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000579 OS << " AttrVec.push_back(Attribute::NoCapture);\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000580 break;
581 }
John McCall375dcc92011-05-28 06:31:34 +0000582
Bill Wendlinged42e792012-10-10 06:13:42 +0000583 ++ai;
584 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
John McCall375dcc92011-05-28 06:31:34 +0000585
Bill Wendlingd079a442012-10-15 04:46:55 +0000586 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(C, "
Bill Wendlinged42e792012-10-10 06:13:42 +0000587 << argNo+1 << ", AttrVec);\n";
588 }
John McCall375dcc92011-05-28 06:31:34 +0000589 }
590
591 ModRefKind modRef = getModRefKind(intrinsic);
592
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000593 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Bill Wendlinged42e792012-10-10 06:13:42 +0000594 OS << " AttrVec.clear();\n";
595
596 if (!intrinsic.canThrow)
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000597 OS << " AttrVec.push_back(Attribute::NoUnwind);\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000598 if (intrinsic.isNoReturn)
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000599 OS << " AttrVec.push_back(Attribute::NoReturn);\n";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000600
John McCall375dcc92011-05-28 06:31:34 +0000601 switch (modRef) {
602 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000603 case MRK_readonly:
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000604 OS << " AttrVec.push_back(Attribute::ReadOnly);\n";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000605 break;
606 case MRK_readnone:
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000607 OS << " AttrVec.push_back(Attribute::ReadNone);\n";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000608 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000609 }
Bill Wendlingfbd38fe2012-10-15 07:29:08 +0000610 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(C, "
Bill Wendlinge94d8432012-12-07 23:16:57 +0000611 << "AttributeSet::FunctionIndex, AttrVec);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000612 }
John McCall375dcc92011-05-28 06:31:34 +0000613
614 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000615 OS << " NumAttrs = " << numAttrs << ";\n";
616 OS << " break;\n";
John McCall375dcc92011-05-28 06:31:34 +0000617 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000618 OS << " return AttributeSet();\n";
John McCall375dcc92011-05-28 06:31:34 +0000619 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000620 }
621
Craig Topper374f19c2012-04-13 06:14:57 +0000622 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000623 OS << " }\n";
Bill Wendlinge94d8432012-12-07 23:16:57 +0000624 OS << " return AttributeSet::get(C, ArrayRef<AttributeWithIndex>(AWI, "
Chris Lattner3cb6f832012-05-28 01:47:44 +0000625 "NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000626 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000627 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000628}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000629
Duncan Sands73247d22009-02-14 10:56:35 +0000630/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
631void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000632EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000633 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
634 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
635 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
636 << "\"Unknown intrinsic.\");\n\n";
637
638 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
639 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000640 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000641 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000642 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000643 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000644 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000645 break;
646 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000647 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000648 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000649 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000650 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000651 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000652 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000653 OS << "OnlyAccessesArgumentPointees,\n";
654 break;
655 case CodeGenIntrinsic::ReadWriteMem:
656 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000657 break;
658 }
659 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000660 OS << "};\n\n"
661 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
662 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000663}
664
Chris Lattner69ea0142008-01-04 04:38:35 +0000665/// EmitTargetBuiltins - All of the builtins in the specified map are for the
666/// same target, and we already checked it.
667static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000668 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000669 raw_ostream &OS) {
Chris Lattner69ea0142008-01-04 04:38:35 +0000670
Chris Lattner497d13e2010-09-06 03:14:45 +0000671 std::vector<StringMatcher::StringPair> Results;
Chris Lattner69ea0142008-01-04 04:38:35 +0000672
Chris Lattner497d13e2010-09-06 03:14:45 +0000673 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
674 E = BIM.end(); I != E; ++I) {
675 std::string ResultCode =
676 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
677 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000678 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000679
680 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000681}
682
683
Chris Lattner402a5732006-03-15 01:33:26 +0000684void IntrinsicEmitter::
685EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000686 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000687 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000688 BIMTy BuiltinMap;
689 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
690 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000691 // Get the map for this target prefix.
692 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
693
694 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
695 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000696 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
697 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000698 }
699 }
700
701 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
702 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
703 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
704 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
705 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000706
707 if (TargetOnly) {
708 OS << "static " << TargetPrefix << "Intrinsic::ID "
709 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000710 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000711 } else {
712 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000713 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000714 }
715
Chris Lattner497d13e2010-09-06 03:14:45 +0000716 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
717 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner69ea0142008-01-04 04:38:35 +0000718
Chris Lattner402a5732006-03-15 01:33:26 +0000719 // Note: this could emit significantly better code if we cared.
720 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000721 OS << " ";
722 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000723 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000724 else
725 OS << "/* Target Independent Builtins */ ";
726 OS << "{\n";
727
Chris Lattner91678fc2008-01-02 21:24:22 +0000728 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000729 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000730 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000731 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000732 OS << " return ";
733 if (!TargetPrefix.empty())
734 OS << "(" << TargetPrefix << "Intrinsic::ID)";
735 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000736 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000737 OS << "#endif\n\n";
738}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000739
740namespace llvm {
741
742void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
743 IntrinsicEmitter(RK, TargetOnly).run(OS);
744}
745
746} // End llvm namespace