blob: df4d847a4d7f5ed2ae517a961c27fa80bfb4558e [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9e493cf2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000014#include "CodeGenIntrinsics.h"
Chandler Carruth69940402007-08-04 01:51:18 +000015#include "CodeGenTarget.h"
Chris Lattner387c9dc2012-05-17 15:55:41 +000016#include "SequenceToOffsetTable.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000017#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000018#include "llvm/TableGen/Error.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000019#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +000020#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000021#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000022#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000023using namespace llvm;
24
Jakob Stoklund Olesen6f36fa92012-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 Lattner9e493cf2006-03-03 02:32:46 +000062//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000063// IntrinsicEmitter Implementation
64//===----------------------------------------------------------------------===//
65
Daniel Dunbar1a551802009-07-03 00:10:29 +000066void IntrinsicEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000067 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
68
Dale Johannesen49de9822009-02-05 01:49:45 +000069 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000070
Dale Johannesen49de9822009-02-05 01:49:45 +000071 if (TargetOnly && !Ints.empty())
72 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattner9e493cf2006-03-03 02:32:46 +000073
Douglas Gregor7d9663c2010-05-11 06:17:44 +000074 EmitPrefix(OS);
75
Chris Lattner9e493cf2006-03-03 02:32:46 +000076 // Emit the enum information.
77 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000078
79 // Emit the intrinsic ID -> name table.
80 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000081
82 // Emit the intrinsic ID -> overload table.
83 EmitIntrinsicToOverloadTable(Ints, OS);
84
Chris Lattner9b843b22006-03-09 20:34:19 +000085 // Emit the function name recognizer.
86 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000087
Jim Laskey95af5922007-02-07 20:38:26 +000088 // Emit the intrinsic declaration generator.
89 EmitGenerator(Ints, OS);
90
Duncan Sandsa3355ff2007-12-03 20:06:50 +000091 // Emit the intrinsic parameter attributes.
92 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000093
Duncan Sandsd869b382009-02-14 10:56:35 +000094 // Emit intrinsic alias analysis mod/ref behavior.
95 EmitModRefBehavior(Ints, OS);
96
Chris Lattner3f8b8912006-03-15 01:33:26 +000097 // Emit code to translate GCC builtins into LLVM intrinsics.
98 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor7d9663c2010-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. Spencer1f409602010-09-24 19:48:47 +0000105 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
106 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +0000107 "# pragma push_macro(\"setjmp\")\n"
108 "# undef setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +0000109 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000110 "#endif\n\n";
111}
112
113void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencer1f409602010-09-24 19:48:47 +0000114 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000115 "// let's return it to _setjmp state\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +0000116 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +0000117 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +0000118 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000119}
120
121void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000122 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +0000123 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-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 Lattner9b843b22006-03-09 20:34:19 +0000133
134void IntrinsicEmitter::
135EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000136 raw_ostream &OS) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000137 // Build a 'first character of function name' -> intrinsic # mapping.
138 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000139 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattnercc67c752010-09-06 03:58:45 +0000140 IntMapping[Ints[i].Name[5]].push_back(i);
141
Chris Lattner9b843b22006-03-09 20:34:19 +0000142 OS << "// Function name -> enum value recognizer code.\n";
143 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattnercc67c752010-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 Lattner9b843b22006-03-09 20:34:19 +0000149 E = IntMapping.end(); I != E; ++I) {
Chris Lattnercc67c752010-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 Lattner9b843b22006-03-09 20:34:19 +0000172 }
173
Chris Lattnercc67c752010-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 Lattner9b843b22006-03-09 20:34:19 +0000177 }
Chris Lattnercc67c752010-09-06 03:58:45 +0000178
Chris Lattner9b843b22006-03-09 20:34:19 +0000179 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000180 OS << "#endif\n\n";
181}
182
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000183void IntrinsicEmitter::
184EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000185 raw_ostream &OS) {
Chris Lattnerfda6aff2006-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 Chengf065a6f2006-03-28 22:25:56 +0000189 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
190 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000191 OS << "#endif\n\n";
192}
193
Mon P Wang0d52ff12009-02-24 23:17:49 +0000194void IntrinsicEmitter::
195EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000196 raw_ostream &OS) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000197 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000198 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000199 OS << "static const uint8_t OTable[] = {\n";
200 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000201 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-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 Wang0d52ff12009-02-24 23:17:49 +0000205 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000206 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000207 }
Benjamin Kramer36a21382012-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 Wang0d52ff12009-02-24 23:17:49 +0000211 OS << "#endif\n\n";
212}
213
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000214
Chris Lattner908a8312012-05-27 18:28:35 +0000215// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000216enum IIT_Info {
Chris Lattner46aaf692012-05-17 04:30:58 +0000217 // Common values should be encoded with 0-15.
Chris Lattnera98aa6a2012-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,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000224 IIT_F16 = 6,
225 IIT_F32 = 7,
226 IIT_F64 = 8,
227 IIT_V2 = 9,
228 IIT_V4 = 10,
229 IIT_V8 = 11,
230 IIT_V16 = 12,
231 IIT_V32 = 13,
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000232 IIT_PTR = 14,
233 IIT_ARG = 15,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000234
Chris Lattner46aaf692012-05-17 04:30:58 +0000235 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000236 IIT_MMX = 16,
237 IIT_METADATA = 17,
238 IIT_EMPTYSTRUCT = 18,
239 IIT_STRUCT2 = 19,
240 IIT_STRUCT3 = 20,
241 IIT_STRUCT4 = 21,
242 IIT_STRUCT5 = 22,
243 IIT_EXTEND_VEC_ARG = 23,
244 IIT_TRUNC_VEC_ARG = 24,
245 IIT_ANYPTR = 25
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000246};
247
Chris Lattner46aaf692012-05-17 04:30:58 +0000248
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000249static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000250 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000251 if (EVT(VT).isInteger()) {
252 unsigned BitWidth = EVT(VT).getSizeInBits();
253 switch (BitWidth) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000254 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000255 case 1: return Sig.push_back(IIT_I1);
256 case 8: return Sig.push_back(IIT_I8);
257 case 16: return Sig.push_back(IIT_I16);
258 case 32: return Sig.push_back(IIT_I32);
259 case 64: return Sig.push_back(IIT_I64);
260 }
261 }
262
Chris Lattner46aaf692012-05-17 04:30:58 +0000263 switch (VT) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000264 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000265 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner46aaf692012-05-17 04:30:58 +0000266 case MVT::f32: return Sig.push_back(IIT_F32);
267 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner46aaf692012-05-17 04:30:58 +0000268 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
269 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
270 // MVT::OtherVT is used to mean the empty struct type here.
271 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
272 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000273}
274
Francois Pichete4807c12012-05-17 04:00:03 +0000275#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000276#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Francois Pichete4807c12012-05-17 04:00:03 +0000277#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000278
Chris Lattnerb4654c12012-05-27 16:39:08 +0000279static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000280 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000281
282 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000283 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerb4654c12012-05-27 16:39:08 +0000284 assert(Number < ArgCodes.size() && "Invalid matching number!");
Chris Lattner46aaf692012-05-17 04:30:58 +0000285 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000286 Sig.push_back(IIT_EXTEND_VEC_ARG);
287 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
288 Sig.push_back(IIT_TRUNC_VEC_ARG);
289 else
290 Sig.push_back(IIT_ARG);
Chris Lattnerb4654c12012-05-27 16:39:08 +0000291 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000292 }
293
294 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner46aaf692012-05-17 04:30:58 +0000295
Chris Lattnerb4654c12012-05-27 16:39:08 +0000296 unsigned Tmp = 0;
Chris Lattnere82d5982012-05-26 23:03:52 +0000297 switch (VT) {
298 default: break;
Chris Lattnerb4654c12012-05-27 16:39:08 +0000299 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
300 case MVT::vAny: ++Tmp; // FALL THROUGH.
301 case MVT::fAny: ++Tmp; // FALL THROUGH.
302 case MVT::iAny: {
Chris Lattnere82d5982012-05-26 23:03:52 +0000303 // If this is an "any" valuetype, then the type is the type of the next
304 // type in the list specified to getIntrinsic().
Chris Lattner46aaf692012-05-17 04:30:58 +0000305 Sig.push_back(IIT_ARG);
Chris Lattnerb4654c12012-05-27 16:39:08 +0000306
307 // Figure out what arg # this is consuming, and remember what kind it was.
308 unsigned ArgNo = ArgCodes.size();
309 ArgCodes.push_back(Tmp);
310
311 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
312 return Sig.push_back((ArgNo << 2) | Tmp);
313 }
Chris Lattnere82d5982012-05-26 23:03:52 +0000314
315 case MVT::iPTR: {
316 unsigned AddrSpace = 0;
317 if (R->isSubClassOf("LLVMQualPointerType")) {
318 AddrSpace = R->getValueAsInt("AddrSpace");
319 assert(AddrSpace < 256 && "Address space exceeds 255");
320 }
321 if (AddrSpace) {
322 Sig.push_back(IIT_ANYPTR);
323 Sig.push_back(AddrSpace);
324 } else {
325 Sig.push_back(IIT_PTR);
326 }
Chris Lattnerb4654c12012-05-27 16:39:08 +0000327 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnere82d5982012-05-26 23:03:52 +0000328 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000329 }
330
331 if (EVT(VT).isVector()) {
332 EVT VVT = VT;
333 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000334 default: PrintFatalError("unhandled vector type width in intrinsic!");
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000335 case 2: Sig.push_back(IIT_V2); break;
336 case 4: Sig.push_back(IIT_V4); break;
337 case 8: Sig.push_back(IIT_V8); break;
338 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner46aaf692012-05-17 04:30:58 +0000339 case 32: Sig.push_back(IIT_V32); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000340 }
341
342 return EncodeFixedValueType(VVT.getVectorElementType().
343 getSimpleVT().SimpleTy, Sig);
344 }
Chris Lattnere82d5982012-05-26 23:03:52 +0000345
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000346 EncodeFixedValueType(VT, Sig);
347}
Francois Pichete4807c12012-05-17 04:00:03 +0000348
349#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000350#pragma optimize("",on)
Francois Pichete4807c12012-05-17 04:00:03 +0000351#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000352
353/// ComputeFixedEncoding - If we can encode the type signature for this
354/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattner387c9dc2012-05-17 15:55:41 +0000355static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
356 std::vector<unsigned char> &TypeSig) {
Chris Lattnerb4654c12012-05-27 16:39:08 +0000357 std::vector<unsigned char> ArgCodes;
Chris Lattner46aaf692012-05-17 04:30:58 +0000358
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000359 if (Int.IS.RetVTs.empty())
360 TypeSig.push_back(IIT_Done);
361 else if (Int.IS.RetVTs.size() == 1 &&
362 Int.IS.RetVTs[0] == MVT::isVoid)
363 TypeSig.push_back(IIT_Done);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000364 else {
365 switch (Int.IS.RetVTs.size()) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000366 case 1: break;
367 case 2: TypeSig.push_back(IIT_STRUCT2); break;
368 case 3: TypeSig.push_back(IIT_STRUCT3); break;
369 case 4: TypeSig.push_back(IIT_STRUCT4); break;
370 case 5: TypeSig.push_back(IIT_STRUCT5); break;
371 default: assert(0 && "Unhandled case in struct");
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000372 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000373
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000374 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000375 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000376 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000377
378 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000379 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner387c9dc2012-05-17 15:55:41 +0000380}
381
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000382static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000383 OS << (unsigned)X;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000384}
385
Jim Laskey95af5922007-02-07 20:38:26 +0000386void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000387 raw_ostream &OS) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000388 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
389 // capture it in this vector, otherwise store a ~0U.
390 std::vector<unsigned> FixedEncodings;
Jim Laskey95af5922007-02-07 20:38:26 +0000391
Chris Lattner387c9dc2012-05-17 15:55:41 +0000392 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
393
394 std::vector<unsigned char> TypeSig;
395
Jim Laskey95af5922007-02-07 20:38:26 +0000396 // Compute the unique argument type info.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000397 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000398 // Get the signature for the intrinsic.
399 TypeSig.clear();
400 ComputeFixedEncoding(Ints[i], TypeSig);
401
402 // Check to see if we can encode it into a 32-bit word. We can only encode
403 // 8 nibbles into a 32-bit word.
404 if (TypeSig.size() <= 8) {
405 bool Failed = false;
406 unsigned Result = 0;
407 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
408 // If we had an unencodable argument, bail out.
409 if (TypeSig[i] > 15) {
410 Failed = true;
411 break;
412 }
413 Result = (Result << 4) | TypeSig[e-i-1];
414 }
415
416 // If this could be encoded into a 31-bit word, return it.
417 if (!Failed && (Result >> 31) == 0) {
418 FixedEncodings.push_back(Result);
419 continue;
420 }
421 }
422
423 // Otherwise, we're going to unique the sequence into the
424 // LongEncodingTable, and use its offset in the 32-bit table instead.
425 LongEncodingTable.add(TypeSig);
426
427 // This is a placehold that we'll replace after the table is laid out.
428 FixedEncodings.push_back(~0U);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000429 }
430
Chris Lattner387c9dc2012-05-17 15:55:41 +0000431 LongEncodingTable.layout();
432
Chris Lattner908a8312012-05-27 18:28:35 +0000433 OS << "// Global intrinsic function declaration type table.\n";
434 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
435
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000436 OS << "static const unsigned IIT_Table[] = {\n ";
437
438 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
439 if ((i & 7) == 7)
440 OS << "\n ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000441
442 // If the entry fit in the table, just emit it.
443 if (FixedEncodings[i] != ~0U) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000444 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000445 continue;
Jim Laskey95af5922007-02-07 20:38:26 +0000446 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000447
448 TypeSig.clear();
449 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000450
Chris Lattner387c9dc2012-05-17 15:55:41 +0000451
452 // Otherwise, emit the offset into the long encoding table. We emit it this
453 // way so that it is easier to read the offset in the .def file.
454 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey95af5922007-02-07 20:38:26 +0000455 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000456
457 OS << "0\n};\n\n";
458
459 // Emit the shared table of register lists.
460 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
461 if (!LongEncodingTable.empty())
462 LongEncodingTable.emit(OS, printIITEntry);
463 OS << " 255\n};\n\n";
464
Patrik Hägglund9ce6f6f2012-05-23 12:34:56 +0000465 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey95af5922007-02-07 20:38:26 +0000466}
467
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000468enum ModRefKind {
469 MRK_none,
470 MRK_readonly,
471 MRK_readnone
472};
John McCallbd0fa4c2011-05-28 06:31:34 +0000473
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000474static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
475 switch (intrinsic.ModRef) {
476 case CodeGenIntrinsic::NoMem:
477 return MRK_readnone;
478 case CodeGenIntrinsic::ReadArgMem:
479 case CodeGenIntrinsic::ReadMem:
480 return MRK_readonly;
481 case CodeGenIntrinsic::ReadWriteArgMem:
482 case CodeGenIntrinsic::ReadWriteMem:
483 return MRK_none;
John McCallbd0fa4c2011-05-28 06:31:34 +0000484 }
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000485 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000486}
487
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000488namespace {
489struct AttributeComparator {
490 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
491 // Sort throwing intrinsics after non-throwing intrinsics.
492 if (L->canThrow != R->canThrow)
493 return R->canThrow;
494
495 if (L->isNoReturn != R->isNoReturn)
496 return R->isNoReturn;
497
498 // Try to order by readonly/readnone attribute.
499 ModRefKind LK = getModRefKind(*L);
500 ModRefKind RK = getModRefKind(*R);
501 if (LK != RK) return (LK > RK);
502
503 // Order by argument attributes.
504 // This is reliable because each side is already sorted internally.
505 return (L->ArgumentAttributes < R->ArgumentAttributes);
506 }
507};
508} // End anonymous namespace
509
Chris Lattner048ffb22009-01-12 01:18:58 +0000510/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000511void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000512EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000513 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
514 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000515 if (TargetOnly)
Bill Wendling99faa3b2012-12-07 23:16:57 +0000516 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000517 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000518 else
Bill Wendling99faa3b2012-12-07 23:16:57 +0000519 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000520
Craig Topper1f595232012-02-28 06:32:00 +0000521 // Compute the maximum number of attribute arguments and the map
522 typedef std::map<const CodeGenIntrinsic*, unsigned,
523 AttributeComparator> UniqAttrMapTy;
524 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000525 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000526 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000527 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000528 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000529 maxArgAttrs =
530 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000531 unsigned &N = UniqAttributes[&intrinsic];
532 if (N) continue;
533 assert(AttrNum < 256 && "Too many unique attributes for table!");
534 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000535 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000536
Bill Wendlinge3617242013-01-27 03:25:05 +0000537 // Emit an array of AttributeSet. Most intrinsics will have at least one
538 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000539 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topper1f595232012-02-28 06:32:00 +0000540
541 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
542 const CodeGenIntrinsic &intrinsic = Ints[i];
543
544 OS << " " << UniqAttributes[&intrinsic] << ", // "
545 << intrinsic.Name << "\n";
546 }
547 OS << " };\n\n";
548
Bill Wendlinge3617242013-01-27 03:25:05 +0000549 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000550 OS << " unsigned NumAttrs = 0;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000551 OS << " if (id != 0) {\n";
Bill Wendling629fb822012-12-22 00:37:52 +0000552 OS << " SmallVector<Attribute::AttrKind, 8> AttrVec;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000553 OS << " switch(IntrinsicsToAttributesMap[id - ";
554 if (TargetOnly)
555 OS << "Intrinsic::num_intrinsics";
556 else
557 OS << "1";
558 OS << "]) {\n";
559 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topper1f595232012-02-28 06:32:00 +0000560 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
561 E = UniqAttributes.end(); I != E; ++I) {
Craig Topperb57b1702012-04-13 06:14:57 +0000562 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000563
Craig Topper1f595232012-02-28 06:32:00 +0000564 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000565
566 // Keep track of the number of attributes we're writing out.
567 unsigned numAttrs = 0;
568
569 // The argument attributes are alreadys sorted by argument index.
Bill Wendling11d00422012-10-10 06:13:42 +0000570 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
571 if (ae) {
572 while (ai != ae) {
573 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000574
Bill Wendling11d00422012-10-10 06:13:42 +0000575 OS << " AttrVec.clear();\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000576
Bill Wendling11d00422012-10-10 06:13:42 +0000577 do {
578 switch (intrinsic.ArgumentAttributes[ai].second) {
579 case CodeGenIntrinsic::NoCapture:
Bill Wendling034b94b2012-12-19 07:18:57 +0000580 OS << " AttrVec.push_back(Attribute::NoCapture);\n";
Bill Wendling11d00422012-10-10 06:13:42 +0000581 break;
582 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000583
Bill Wendling11d00422012-10-10 06:13:42 +0000584 ++ai;
585 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
John McCallbd0fa4c2011-05-28 06:31:34 +0000586
Bill Wendlinge3617242013-01-27 03:25:05 +0000587 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Bill Wendling11d00422012-10-10 06:13:42 +0000588 << argNo+1 << ", AttrVec);\n";
589 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000590 }
591
592 ModRefKind modRef = getModRefKind(intrinsic);
593
Chris Lattner86208902012-05-27 23:20:41 +0000594 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Bill Wendling11d00422012-10-10 06:13:42 +0000595 OS << " AttrVec.clear();\n";
596
597 if (!intrinsic.canThrow)
Bill Wendling034b94b2012-12-19 07:18:57 +0000598 OS << " AttrVec.push_back(Attribute::NoUnwind);\n";
Bill Wendling11d00422012-10-10 06:13:42 +0000599 if (intrinsic.isNoReturn)
Bill Wendling034b94b2012-12-19 07:18:57 +0000600 OS << " AttrVec.push_back(Attribute::NoReturn);\n";
Chris Lattner86208902012-05-27 23:20:41 +0000601
John McCallbd0fa4c2011-05-28 06:31:34 +0000602 switch (modRef) {
603 case MRK_none: break;
Chris Lattner86208902012-05-27 23:20:41 +0000604 case MRK_readonly:
Bill Wendling034b94b2012-12-19 07:18:57 +0000605 OS << " AttrVec.push_back(Attribute::ReadOnly);\n";
Chris Lattner86208902012-05-27 23:20:41 +0000606 break;
607 case MRK_readnone:
Bill Wendling034b94b2012-12-19 07:18:57 +0000608 OS << " AttrVec.push_back(Attribute::ReadNone);\n";
Chris Lattner86208902012-05-27 23:20:41 +0000609 break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000610 }
Bill Wendlinge3617242013-01-27 03:25:05 +0000611 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Bill Wendling99faa3b2012-12-07 23:16:57 +0000612 << "AttributeSet::FunctionIndex, AttrVec);\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000613 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000614
615 if (numAttrs) {
Craig Topperb57b1702012-04-13 06:14:57 +0000616 OS << " NumAttrs = " << numAttrs << ";\n";
617 OS << " break;\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000618 } else {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000619 OS << " return AttributeSet();\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000620 }
Chris Lattner10dae942009-01-12 01:27:55 +0000621 }
622
Craig Topperb57b1702012-04-13 06:14:57 +0000623 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000624 OS << " }\n";
Bill Wendlinge3617242013-01-27 03:25:05 +0000625 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000626 "NumAttrs));\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000627 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000628 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000629}
Chris Lattner022f64f2006-03-13 23:08:44 +0000630
Duncan Sandsd869b382009-02-14 10:56:35 +0000631/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
632void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000633EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000634 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
635 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
636 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
637 << "\"Unknown intrinsic.\");\n\n";
638
639 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
640 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000641 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000642 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sandsd869b382009-02-14 10:56:35 +0000643 switch (Ints[i].ModRef) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000644 case CodeGenIntrinsic::NoMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000645 OS << "DoesNotAccessMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000646 break;
647 case CodeGenIntrinsic::ReadArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000648 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000649 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000650 case CodeGenIntrinsic::ReadMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000651 OS << "OnlyReadsMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000652 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000653 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000654 OS << "OnlyAccessesArgumentPointees,\n";
655 break;
656 case CodeGenIntrinsic::ReadWriteMem:
657 OS << "UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000658 break;
659 }
660 }
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000661 OS << "};\n\n"
662 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
663 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000664}
665
Chris Lattner331bf922008-01-04 04:38:35 +0000666/// EmitTargetBuiltins - All of the builtins in the specified map are for the
667/// same target, and we already checked it.
668static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000669 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000670 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000671
Chris Lattner298b1762010-09-06 03:14:45 +0000672 std::vector<StringMatcher::StringPair> Results;
Chris Lattner331bf922008-01-04 04:38:35 +0000673
Chris Lattner298b1762010-09-06 03:14:45 +0000674 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
675 E = BIM.end(); I != E; ++I) {
676 std::string ResultCode =
677 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
678 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000679 }
Chris Lattner298b1762010-09-06 03:14:45 +0000680
681 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000682}
683
684
Chris Lattner3f8b8912006-03-15 01:33:26 +0000685void IntrinsicEmitter::
686EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000687 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000688 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000689 BIMTy BuiltinMap;
690 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
691 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000692 // Get the map for this target prefix.
693 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
694
695 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
696 Ints[i].EnumName)).second)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000697 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
698 "': duplicate GCC builtin name!");
Chris Lattner3f8b8912006-03-15 01:33:26 +0000699 }
700 }
701
702 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
703 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
704 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
705 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
706 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000707
708 if (TargetOnly) {
709 OS << "static " << TargetPrefix << "Intrinsic::ID "
710 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000711 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000712 } else {
713 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000714 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000715 }
716
Chris Lattner298b1762010-09-06 03:14:45 +0000717 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
718 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000719
Chris Lattner3f8b8912006-03-15 01:33:26 +0000720 // Note: this could emit significantly better code if we cared.
721 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000722 OS << " ";
723 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000724 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000725 else
726 OS << "/* Target Independent Builtins */ ";
727 OS << "{\n";
728
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000729 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000730 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000731 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000732 }
Chris Lattner298b1762010-09-06 03:14:45 +0000733 OS << " return ";
734 if (!TargetPrefix.empty())
735 OS << "(" << TargetPrefix << "Intrinsic::ID)";
736 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000737 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000738 OS << "#endif\n\n";
739}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000740
741namespace llvm {
742
743void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
744 IntrinsicEmitter(RK, TargetOnly).run(OS);
745}
746
747} // End llvm namespace