blob: cd4f0e62d727dcc006933814762c633af3142813 [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);
Andrew Trickcf940ce2013-10-31 17:18:07 +000087
Jim Laskey95af5922007-02-07 20:38:26 +000088 // Emit the intrinsic declaration generator.
89 EmitGenerator(Ints, OS);
Andrew Trickcf940ce2013-10-31 17:18:07 +000090
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) ? ", " : " ");
Andrew Trickcf940ce2013-10-31 17:18:07 +0000128 OS << std::string(40-Ints[i].EnumName.size(), ' ')
Chris Lattner9e493cf2006-03-03 02:32:46 +0000129 << "// " << Ints[i].Name << "\n";
130 }
131 OS << "#endif\n\n";
132}
Chris Lattner9b843b22006-03-09 20:34:19 +0000133
Justin Holewinski563a9cf2013-07-25 12:32:00 +0000134struct IntrinsicNameSorter {
135 IntrinsicNameSorter(const std::vector<CodeGenIntrinsic> &I)
136 : Ints(I) {}
137
138 // Sort in reverse order of intrinsic name so "abc.def" appears after
139 // "abd.def.ghi" in the overridden name matcher
140 bool operator()(unsigned i, unsigned j) {
141 return Ints[i].Name > Ints[j].Name;
142 }
143
144private:
145 const std::vector<CodeGenIntrinsic> &Ints;
146};
147
Chris Lattner9b843b22006-03-09 20:34:19 +0000148void IntrinsicEmitter::
Andrew Trickcf940ce2013-10-31 17:18:07 +0000149EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000150 raw_ostream &OS) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000151 // Build a 'first character of function name' -> intrinsic # mapping.
152 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattnercc67c752010-09-06 03:58:45 +0000154 IntMapping[Ints[i].Name[5]].push_back(i);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000155
Chris Lattner9b843b22006-03-09 20:34:19 +0000156 OS << "// Function name -> enum value recognizer code.\n";
157 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattnercc67c752010-09-06 03:58:45 +0000158 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
159 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
160 OS << " default: break;\n";
Justin Holewinski563a9cf2013-07-25 12:32:00 +0000161 IntrinsicNameSorter Sorter(Ints);
Chris Lattnercc67c752010-09-06 03:58:45 +0000162 // Emit the intrinsic matching stuff by first letter.
163 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000164 E = IntMapping.end(); I != E; ++I) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000165 OS << " case '" << I->first << "':\n";
166 std::vector<unsigned> &IntList = I->second;
167
Justin Holewinski563a9cf2013-07-25 12:32:00 +0000168 // Sort intrinsics in reverse order of their names
169 std::sort(IntList.begin(), IntList.end(), Sorter);
170
Chris Lattnercc67c752010-09-06 03:58:45 +0000171 // Emit all the overloaded intrinsics first, build a table of the
172 // non-overloaded ones.
173 std::vector<StringMatcher::StringPair> MatchTable;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000174
Chris Lattnercc67c752010-09-06 03:58:45 +0000175 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
176 unsigned IntNo = IntList[i];
177 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
178 Ints[IntNo].EnumName + ";";
179
180 if (!Ints[IntNo].isOverloaded) {
181 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
182 continue;
183 }
184
185 // For overloaded intrinsics, only the prefix needs to match
186 std::string TheStr = Ints[IntNo].Name.substr(6);
187 TheStr += '.'; // Require "bswap." instead of bswap.
188 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
189 << Result << '\n';
Chris Lattner9b843b22006-03-09 20:34:19 +0000190 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000191
Chris Lattnercc67c752010-09-06 03:58:45 +0000192 // Emit the matcher logic for the fixed length strings.
193 StringMatcher("NameR", MatchTable, OS).Emit(1);
194 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000195 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000196
Chris Lattner9b843b22006-03-09 20:34:19 +0000197 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000198 OS << "#endif\n\n";
199}
200
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000201void IntrinsicEmitter::
Andrew Trickcf940ce2013-10-31 17:18:07 +0000202EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000203 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000204 OS << "// Intrinsic ID to name table\n";
205 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
206 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000207 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
208 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000209 OS << "#endif\n\n";
210}
211
Mon P Wang0d52ff12009-02-24 23:17:49 +0000212void IntrinsicEmitter::
Andrew Trickcf940ce2013-10-31 17:18:07 +0000213EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000214 raw_ostream &OS) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000215 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000216 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000217 OS << "static const uint8_t OTable[] = {\n";
218 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000219 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000220 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
221 if ((i+1)%8 == 0)
222 OS << ",\n 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000223 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000224 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000225 }
Benjamin Kramer36a21382012-03-01 02:16:57 +0000226 OS << "\n};\n\n";
227 // OTable contains a true bit at the position if the intrinsic is overloaded.
228 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000229 OS << "#endif\n\n";
230}
231
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000232
Chris Lattner908a8312012-05-27 18:28:35 +0000233// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000234enum IIT_Info {
Chris Lattner46aaf692012-05-17 04:30:58 +0000235 // Common values should be encoded with 0-15.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000236 IIT_Done = 0,
237 IIT_I1 = 1,
238 IIT_I8 = 2,
239 IIT_I16 = 3,
240 IIT_I32 = 4,
241 IIT_I64 = 5,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000242 IIT_F16 = 6,
243 IIT_F32 = 7,
244 IIT_F64 = 8,
245 IIT_V2 = 9,
246 IIT_V4 = 10,
247 IIT_V8 = 11,
248 IIT_V16 = 12,
249 IIT_V32 = 13,
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000250 IIT_PTR = 14,
251 IIT_ARG = 15,
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000252
Chris Lattner46aaf692012-05-17 04:30:58 +0000253 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000254 IIT_MMX = 16,
255 IIT_METADATA = 17,
256 IIT_EMPTYSTRUCT = 18,
257 IIT_STRUCT2 = 19,
258 IIT_STRUCT3 = 20,
259 IIT_STRUCT4 = 21,
260 IIT_STRUCT5 = 22,
261 IIT_EXTEND_VEC_ARG = 23,
262 IIT_TRUNC_VEC_ARG = 24,
Jiangning Liu477fc622013-09-24 02:47:27 +0000263 IIT_ANYPTR = 25,
264 IIT_V1 = 26
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000265};
266
Chris Lattner46aaf692012-05-17 04:30:58 +0000267
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000268static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000269 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000270 if (EVT(VT).isInteger()) {
271 unsigned BitWidth = EVT(VT).getSizeInBits();
272 switch (BitWidth) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000273 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000274 case 1: return Sig.push_back(IIT_I1);
275 case 8: return Sig.push_back(IIT_I8);
276 case 16: return Sig.push_back(IIT_I16);
277 case 32: return Sig.push_back(IIT_I32);
278 case 64: return Sig.push_back(IIT_I64);
279 }
280 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000281
Chris Lattner46aaf692012-05-17 04:30:58 +0000282 switch (VT) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000283 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman4d0b4a42013-01-11 01:45:05 +0000284 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner46aaf692012-05-17 04:30:58 +0000285 case MVT::f32: return Sig.push_back(IIT_F32);
286 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner46aaf692012-05-17 04:30:58 +0000287 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
288 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
289 // MVT::OtherVT is used to mean the empty struct type here.
290 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
291 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000292}
293
Francois Pichete4807c12012-05-17 04:00:03 +0000294#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000295#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Andrew Trickcf940ce2013-10-31 17:18:07 +0000296#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000297
Chris Lattnerb4654c12012-05-27 16:39:08 +0000298static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000299 std::vector<unsigned char> &Sig) {
Andrew Trickcf940ce2013-10-31 17:18:07 +0000300
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000301 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000302 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerb4654c12012-05-27 16:39:08 +0000303 assert(Number < ArgCodes.size() && "Invalid matching number!");
Chris Lattner46aaf692012-05-17 04:30:58 +0000304 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000305 Sig.push_back(IIT_EXTEND_VEC_ARG);
306 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
307 Sig.push_back(IIT_TRUNC_VEC_ARG);
308 else
309 Sig.push_back(IIT_ARG);
Chris Lattnerb4654c12012-05-27 16:39:08 +0000310 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000311 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000312
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000313 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner46aaf692012-05-17 04:30:58 +0000314
Chris Lattnerb4654c12012-05-27 16:39:08 +0000315 unsigned Tmp = 0;
Chris Lattnere82d5982012-05-26 23:03:52 +0000316 switch (VT) {
317 default: break;
Chris Lattnerb4654c12012-05-27 16:39:08 +0000318 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
319 case MVT::vAny: ++Tmp; // FALL THROUGH.
320 case MVT::fAny: ++Tmp; // FALL THROUGH.
321 case MVT::iAny: {
Chris Lattnere82d5982012-05-26 23:03:52 +0000322 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickcf940ce2013-10-31 17:18:07 +0000323 // type in the list specified to getIntrinsic().
Chris Lattner46aaf692012-05-17 04:30:58 +0000324 Sig.push_back(IIT_ARG);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000325
Chris Lattnerb4654c12012-05-27 16:39:08 +0000326 // Figure out what arg # this is consuming, and remember what kind it was.
327 unsigned ArgNo = ArgCodes.size();
328 ArgCodes.push_back(Tmp);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000329
Chris Lattnerb4654c12012-05-27 16:39:08 +0000330 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
331 return Sig.push_back((ArgNo << 2) | Tmp);
332 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000333
Chris Lattnere82d5982012-05-26 23:03:52 +0000334 case MVT::iPTR: {
335 unsigned AddrSpace = 0;
336 if (R->isSubClassOf("LLVMQualPointerType")) {
337 AddrSpace = R->getValueAsInt("AddrSpace");
338 assert(AddrSpace < 256 && "Address space exceeds 255");
339 }
340 if (AddrSpace) {
341 Sig.push_back(IIT_ANYPTR);
342 Sig.push_back(AddrSpace);
343 } else {
344 Sig.push_back(IIT_PTR);
345 }
Chris Lattnerb4654c12012-05-27 16:39:08 +0000346 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnere82d5982012-05-26 23:03:52 +0000347 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000348 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000349
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000350 if (EVT(VT).isVector()) {
351 EVT VVT = VT;
352 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000353 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu477fc622013-09-24 02:47:27 +0000354 case 1: Sig.push_back(IIT_V1); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000355 case 2: Sig.push_back(IIT_V2); break;
356 case 4: Sig.push_back(IIT_V4); break;
357 case 8: Sig.push_back(IIT_V8); break;
358 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner46aaf692012-05-17 04:30:58 +0000359 case 32: Sig.push_back(IIT_V32); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000360 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000361
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000362 return EncodeFixedValueType(VVT.getVectorElementType().
363 getSimpleVT().SimpleTy, Sig);
364 }
Chris Lattnere82d5982012-05-26 23:03:52 +0000365
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000366 EncodeFixedValueType(VT, Sig);
367}
Francois Pichete4807c12012-05-17 04:00:03 +0000368
369#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000370#pragma optimize("",on)
Francois Pichete4807c12012-05-17 04:00:03 +0000371#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000372
373/// ComputeFixedEncoding - If we can encode the type signature for this
374/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattner387c9dc2012-05-17 15:55:41 +0000375static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
376 std::vector<unsigned char> &TypeSig) {
Chris Lattnerb4654c12012-05-27 16:39:08 +0000377 std::vector<unsigned char> ArgCodes;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000378
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000379 if (Int.IS.RetVTs.empty())
380 TypeSig.push_back(IIT_Done);
381 else if (Int.IS.RetVTs.size() == 1 &&
382 Int.IS.RetVTs[0] == MVT::isVoid)
383 TypeSig.push_back(IIT_Done);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000384 else {
385 switch (Int.IS.RetVTs.size()) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000386 case 1: break;
387 case 2: TypeSig.push_back(IIT_STRUCT2); break;
388 case 3: TypeSig.push_back(IIT_STRUCT3); break;
389 case 4: TypeSig.push_back(IIT_STRUCT4); break;
390 case 5: TypeSig.push_back(IIT_STRUCT5); break;
391 default: assert(0 && "Unhandled case in struct");
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000392 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000393
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000394 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000395 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000396 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000397
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000398 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000399 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner387c9dc2012-05-17 15:55:41 +0000400}
401
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000402static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000403 OS << (unsigned)X;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000404}
405
Andrew Trickcf940ce2013-10-31 17:18:07 +0000406void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000407 raw_ostream &OS) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000408 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
409 // capture it in this vector, otherwise store a ~0U.
410 std::vector<unsigned> FixedEncodings;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000411
Chris Lattner387c9dc2012-05-17 15:55:41 +0000412 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000413
Chris Lattner387c9dc2012-05-17 15:55:41 +0000414 std::vector<unsigned char> TypeSig;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000415
Jim Laskey95af5922007-02-07 20:38:26 +0000416 // Compute the unique argument type info.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000417 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000418 // Get the signature for the intrinsic.
419 TypeSig.clear();
420 ComputeFixedEncoding(Ints[i], TypeSig);
421
422 // Check to see if we can encode it into a 32-bit word. We can only encode
423 // 8 nibbles into a 32-bit word.
424 if (TypeSig.size() <= 8) {
425 bool Failed = false;
426 unsigned Result = 0;
427 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
428 // If we had an unencodable argument, bail out.
429 if (TypeSig[i] > 15) {
430 Failed = true;
431 break;
432 }
433 Result = (Result << 4) | TypeSig[e-i-1];
434 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000435
Chris Lattner387c9dc2012-05-17 15:55:41 +0000436 // If this could be encoded into a 31-bit word, return it.
437 if (!Failed && (Result >> 31) == 0) {
438 FixedEncodings.push_back(Result);
439 continue;
440 }
441 }
442
443 // Otherwise, we're going to unique the sequence into the
444 // LongEncodingTable, and use its offset in the 32-bit table instead.
445 LongEncodingTable.add(TypeSig);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000446
Chris Lattner387c9dc2012-05-17 15:55:41 +0000447 // This is a placehold that we'll replace after the table is laid out.
448 FixedEncodings.push_back(~0U);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000449 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000450
Chris Lattner387c9dc2012-05-17 15:55:41 +0000451 LongEncodingTable.layout();
Andrew Trickcf940ce2013-10-31 17:18:07 +0000452
Chris Lattner908a8312012-05-27 18:28:35 +0000453 OS << "// Global intrinsic function declaration type table.\n";
454 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
455
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000456 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000457
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000458 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
459 if ((i & 7) == 7)
460 OS << "\n ";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000461
Chris Lattner387c9dc2012-05-17 15:55:41 +0000462 // If the entry fit in the table, just emit it.
463 if (FixedEncodings[i] != ~0U) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000464 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000465 continue;
Jim Laskey95af5922007-02-07 20:38:26 +0000466 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000467
Chris Lattner387c9dc2012-05-17 15:55:41 +0000468 TypeSig.clear();
469 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000470
Andrew Trickcf940ce2013-10-31 17:18:07 +0000471
Chris Lattner387c9dc2012-05-17 15:55:41 +0000472 // Otherwise, emit the offset into the long encoding table. We emit it this
473 // way so that it is easier to read the offset in the .def file.
474 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey95af5922007-02-07 20:38:26 +0000475 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000476
Chris Lattner387c9dc2012-05-17 15:55:41 +0000477 OS << "0\n};\n\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000478
Chris Lattner387c9dc2012-05-17 15:55:41 +0000479 // Emit the shared table of register lists.
480 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
481 if (!LongEncodingTable.empty())
482 LongEncodingTable.emit(OS, printIITEntry);
483 OS << " 255\n};\n\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000484
Patrik Hägglund9ce6f6f2012-05-23 12:34:56 +0000485 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey95af5922007-02-07 20:38:26 +0000486}
487
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000488enum ModRefKind {
489 MRK_none,
490 MRK_readonly,
491 MRK_readnone
492};
John McCallbd0fa4c2011-05-28 06:31:34 +0000493
Jakob Stoklund Olesen6f36fa92012-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 McCallbd0fa4c2011-05-28 06:31:34 +0000504 }
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000505 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000506}
507
Jakob Stoklund Olesen6f36fa92012-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
515 if (L->isNoReturn != R->isNoReturn)
516 return R->isNoReturn;
517
518 // Try to order by readonly/readnone attribute.
519 ModRefKind LK = getModRefKind(*L);
520 ModRefKind RK = getModRefKind(*R);
521 if (LK != RK) return (LK > RK);
522
523 // Order by argument attributes.
524 // This is reliable because each side is already sorted internally.
525 return (L->ArgumentAttributes < R->ArgumentAttributes);
526 }
527};
528} // End anonymous namespace
529
Chris Lattner048ffb22009-01-12 01:18:58 +0000530/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000531void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000532EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000533 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
534 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000535 if (TargetOnly)
Bill Wendling99faa3b2012-12-07 23:16:57 +0000536 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000537 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000538 else
Bill Wendling99faa3b2012-12-07 23:16:57 +0000539 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000540
Craig Topper1f595232012-02-28 06:32:00 +0000541 // Compute the maximum number of attribute arguments and the map
542 typedef std::map<const CodeGenIntrinsic*, unsigned,
543 AttributeComparator> UniqAttrMapTy;
544 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000545 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000546 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000547 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000548 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000549 maxArgAttrs =
550 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000551 unsigned &N = UniqAttributes[&intrinsic];
552 if (N) continue;
553 assert(AttrNum < 256 && "Too many unique attributes for table!");
554 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000555 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000556
Bill Wendlinge3617242013-01-27 03:25:05 +0000557 // Emit an array of AttributeSet. Most intrinsics will have at least one
558 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000559 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topper1f595232012-02-28 06:32:00 +0000560
561 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
562 const CodeGenIntrinsic &intrinsic = Ints[i];
563
564 OS << " " << UniqAttributes[&intrinsic] << ", // "
565 << intrinsic.Name << "\n";
566 }
567 OS << " };\n\n";
568
Bill Wendlinge3617242013-01-27 03:25:05 +0000569 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000570 OS << " unsigned NumAttrs = 0;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000571 OS << " if (id != 0) {\n";
Bill Wendling629fb822012-12-22 00:37:52 +0000572 OS << " SmallVector<Attribute::AttrKind, 8> AttrVec;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000573 OS << " switch(IntrinsicsToAttributesMap[id - ";
574 if (TargetOnly)
575 OS << "Intrinsic::num_intrinsics";
576 else
577 OS << "1";
578 OS << "]) {\n";
579 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topper1f595232012-02-28 06:32:00 +0000580 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
581 E = UniqAttributes.end(); I != E; ++I) {
Craig Topperb57b1702012-04-13 06:14:57 +0000582 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000583
Craig Topper1f595232012-02-28 06:32:00 +0000584 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000585
586 // Keep track of the number of attributes we're writing out.
587 unsigned numAttrs = 0;
588
589 // The argument attributes are alreadys sorted by argument index.
Bill Wendling11d00422012-10-10 06:13:42 +0000590 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
591 if (ae) {
592 while (ai != ae) {
593 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000594
Bill Wendling11d00422012-10-10 06:13:42 +0000595 OS << " AttrVec.clear();\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000596
Bill Wendling11d00422012-10-10 06:13:42 +0000597 do {
598 switch (intrinsic.ArgumentAttributes[ai].second) {
599 case CodeGenIntrinsic::NoCapture:
Bill Wendling034b94b2012-12-19 07:18:57 +0000600 OS << " AttrVec.push_back(Attribute::NoCapture);\n";
Bill Wendling11d00422012-10-10 06:13:42 +0000601 break;
Nick Lewyckydc897372013-07-06 00:29:58 +0000602 case CodeGenIntrinsic::ReadOnly:
603 OS << " AttrVec.push_back(Attribute::ReadOnly);\n";
604 break;
605 case CodeGenIntrinsic::ReadNone:
606 OS << " AttrVec.push_back(Attribute::ReadNone);\n";
607 break;
Bill Wendling11d00422012-10-10 06:13:42 +0000608 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000609
Bill Wendling11d00422012-10-10 06:13:42 +0000610 ++ai;
611 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
John McCallbd0fa4c2011-05-28 06:31:34 +0000612
Bill Wendlinge3617242013-01-27 03:25:05 +0000613 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Bill Wendling11d00422012-10-10 06:13:42 +0000614 << argNo+1 << ", AttrVec);\n";
615 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000616 }
617
618 ModRefKind modRef = getModRefKind(intrinsic);
619
Chris Lattner86208902012-05-27 23:20:41 +0000620 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Bill Wendling11d00422012-10-10 06:13:42 +0000621 OS << " AttrVec.clear();\n";
622
623 if (!intrinsic.canThrow)
Bill Wendling034b94b2012-12-19 07:18:57 +0000624 OS << " AttrVec.push_back(Attribute::NoUnwind);\n";
Bill Wendling11d00422012-10-10 06:13:42 +0000625 if (intrinsic.isNoReturn)
Bill Wendling034b94b2012-12-19 07:18:57 +0000626 OS << " AttrVec.push_back(Attribute::NoReturn);\n";
Chris Lattner86208902012-05-27 23:20:41 +0000627
John McCallbd0fa4c2011-05-28 06:31:34 +0000628 switch (modRef) {
629 case MRK_none: break;
Chris Lattner86208902012-05-27 23:20:41 +0000630 case MRK_readonly:
Bill Wendling034b94b2012-12-19 07:18:57 +0000631 OS << " AttrVec.push_back(Attribute::ReadOnly);\n";
Chris Lattner86208902012-05-27 23:20:41 +0000632 break;
633 case MRK_readnone:
Andrew Trickcf940ce2013-10-31 17:18:07 +0000634 OS << " AttrVec.push_back(Attribute::ReadNone);\n";
Chris Lattner86208902012-05-27 23:20:41 +0000635 break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000636 }
Bill Wendlinge3617242013-01-27 03:25:05 +0000637 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Bill Wendling99faa3b2012-12-07 23:16:57 +0000638 << "AttributeSet::FunctionIndex, AttrVec);\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000639 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000640
641 if (numAttrs) {
Craig Topperb57b1702012-04-13 06:14:57 +0000642 OS << " NumAttrs = " << numAttrs << ";\n";
643 OS << " break;\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000644 } else {
Bill Wendling99faa3b2012-12-07 23:16:57 +0000645 OS << " return AttributeSet();\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000646 }
Chris Lattner10dae942009-01-12 01:27:55 +0000647 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000648
Craig Topperb57b1702012-04-13 06:14:57 +0000649 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000650 OS << " }\n";
Bill Wendlinge3617242013-01-27 03:25:05 +0000651 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000652 "NumAttrs));\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000653 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000654 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000655}
Chris Lattner022f64f2006-03-13 23:08:44 +0000656
Duncan Sandsd869b382009-02-14 10:56:35 +0000657/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
658void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000659EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000660 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
661 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
662 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
663 << "\"Unknown intrinsic.\");\n\n";
664
665 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
666 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000667 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000668 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sandsd869b382009-02-14 10:56:35 +0000669 switch (Ints[i].ModRef) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000670 case CodeGenIntrinsic::NoMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000671 OS << "DoesNotAccessMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000672 break;
673 case CodeGenIntrinsic::ReadArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000674 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000675 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000676 case CodeGenIntrinsic::ReadMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000677 OS << "OnlyReadsMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000678 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000679 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000680 OS << "OnlyAccessesArgumentPointees,\n";
681 break;
682 case CodeGenIntrinsic::ReadWriteMem:
683 OS << "UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000684 break;
685 }
686 }
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000687 OS << "};\n\n"
688 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
689 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000690}
691
Chris Lattner331bf922008-01-04 04:38:35 +0000692/// EmitTargetBuiltins - All of the builtins in the specified map are for the
693/// same target, and we already checked it.
694static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000695 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000696 raw_ostream &OS) {
Andrew Trickcf940ce2013-10-31 17:18:07 +0000697
Chris Lattner298b1762010-09-06 03:14:45 +0000698 std::vector<StringMatcher::StringPair> Results;
Andrew Trickcf940ce2013-10-31 17:18:07 +0000699
Chris Lattner298b1762010-09-06 03:14:45 +0000700 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
701 E = BIM.end(); I != E; ++I) {
702 std::string ResultCode =
703 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
704 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000705 }
Chris Lattner298b1762010-09-06 03:14:45 +0000706
707 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000708}
709
Andrew Trickcf940ce2013-10-31 17:18:07 +0000710
Chris Lattner3f8b8912006-03-15 01:33:26 +0000711void IntrinsicEmitter::
Andrew Trickcf940ce2013-10-31 17:18:07 +0000712EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000713 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000714 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000715 BIMTy BuiltinMap;
716 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
717 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000718 // Get the map for this target prefix.
719 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickcf940ce2013-10-31 17:18:07 +0000720
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000721 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
722 Ints[i].EnumName)).second)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000723 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
724 "': duplicate GCC builtin name!");
Chris Lattner3f8b8912006-03-15 01:33:26 +0000725 }
726 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000727
Chris Lattner3f8b8912006-03-15 01:33:26 +0000728 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
729 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
730 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
731 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
732 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000733
Dale Johannesen49de9822009-02-05 01:49:45 +0000734 if (TargetOnly) {
735 OS << "static " << TargetPrefix << "Intrinsic::ID "
736 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000737 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000738 } else {
739 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000740 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000741 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000742
Chris Lattner298b1762010-09-06 03:14:45 +0000743 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
744 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickcf940ce2013-10-31 17:18:07 +0000745
Chris Lattner3f8b8912006-03-15 01:33:26 +0000746 // Note: this could emit significantly better code if we cared.
747 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000748 OS << " ";
749 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000750 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000751 else
752 OS << "/* Target Independent Builtins */ ";
753 OS << "{\n";
754
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000755 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000756 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000757 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000758 }
Chris Lattner298b1762010-09-06 03:14:45 +0000759 OS << " return ";
760 if (!TargetPrefix.empty())
761 OS << "(" << TargetPrefix << "Intrinsic::ID)";
762 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000763 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000764 OS << "#endif\n\n";
765}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000766
767namespace llvm {
768
769void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
770 IntrinsicEmitter(RK, TargetOnly).run(OS);
771}
772
773} // End llvm namespace