blob: 9e2bb9d87f2402982b658c05edd70520fd6f2dbf [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
Chandler Carruth69940402007-08-04 01:51:18 +000014#include "CodeGenTarget.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000015#include "IntrinsicEmitter.h"
Chris Lattner387c9dc2012-05-17 15:55:41 +000016#include "SequenceToOffsetTable.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +000018#include "llvm/TableGen/StringMatcher.h"
Chris Lattner18faf5d2006-03-13 22:38:57 +000019#include "llvm/ADT/StringExtras.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000020#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000024// IntrinsicEmitter Implementation
25//===----------------------------------------------------------------------===//
26
Daniel Dunbar1a551802009-07-03 00:10:29 +000027void IntrinsicEmitter::run(raw_ostream &OS) {
Chris Lattner9e493cf2006-03-03 02:32:46 +000028 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
29
Dale Johannesen49de9822009-02-05 01:49:45 +000030 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
31
32 if (TargetOnly && !Ints.empty())
33 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattner9e493cf2006-03-03 02:32:46 +000034
Douglas Gregor7d9663c2010-05-11 06:17:44 +000035 EmitPrefix(OS);
36
Chris Lattner9e493cf2006-03-03 02:32:46 +000037 // Emit the enum information.
38 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000039
40 // Emit the intrinsic ID -> name table.
41 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000042
43 // Emit the intrinsic ID -> overload table.
44 EmitIntrinsicToOverloadTable(Ints, OS);
45
Chris Lattner9b843b22006-03-09 20:34:19 +000046 // Emit the function name recognizer.
47 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000048
Jim Laskey95af5922007-02-07 20:38:26 +000049 // Emit the intrinsic declaration generator.
50 EmitGenerator(Ints, OS);
51
Duncan Sandsa3355ff2007-12-03 20:06:50 +000052 // Emit the intrinsic parameter attributes.
53 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000054
Duncan Sandsd869b382009-02-14 10:56:35 +000055 // Emit intrinsic alias analysis mod/ref behavior.
56 EmitModRefBehavior(Ints, OS);
57
Chris Lattner3f8b8912006-03-15 01:33:26 +000058 // Emit code to translate GCC builtins into LLVM intrinsics.
59 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor7d9663c2010-05-11 06:17:44 +000060
61 EmitSuffix(OS);
62}
63
64void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
65 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000066 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
67 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000068 "# pragma push_macro(\"setjmp\")\n"
69 "# undef setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000070 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000071 "#endif\n\n";
72}
73
74void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencer1f409602010-09-24 19:48:47 +000075 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000076 "// let's return it to _setjmp state\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000077 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000078 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000079 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000080}
81
82void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000083 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000084 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000085 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
86 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
87 OS << " " << Ints[i].EnumName;
88 OS << ((i != e-1) ? ", " : " ");
89 OS << std::string(40-Ints[i].EnumName.size(), ' ')
90 << "// " << Ints[i].Name << "\n";
91 }
92 OS << "#endif\n\n";
93}
Chris Lattner9b843b22006-03-09 20:34:19 +000094
95void IntrinsicEmitter::
96EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000097 raw_ostream &OS) {
Chris Lattnercc67c752010-09-06 03:58:45 +000098 // Build a 'first character of function name' -> intrinsic # mapping.
99 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000100 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattnercc67c752010-09-06 03:58:45 +0000101 IntMapping[Ints[i].Name[5]].push_back(i);
102
Chris Lattner9b843b22006-03-09 20:34:19 +0000103 OS << "// Function name -> enum value recognizer code.\n";
104 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattnercc67c752010-09-06 03:58:45 +0000105 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
106 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
107 OS << " default: break;\n";
108 // Emit the intrinsic matching stuff by first letter.
109 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000110 E = IntMapping.end(); I != E; ++I) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000111 OS << " case '" << I->first << "':\n";
112 std::vector<unsigned> &IntList = I->second;
113
114 // Emit all the overloaded intrinsics first, build a table of the
115 // non-overloaded ones.
116 std::vector<StringMatcher::StringPair> MatchTable;
117
118 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
119 unsigned IntNo = IntList[i];
120 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
121 Ints[IntNo].EnumName + ";";
122
123 if (!Ints[IntNo].isOverloaded) {
124 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
125 continue;
126 }
127
128 // For overloaded intrinsics, only the prefix needs to match
129 std::string TheStr = Ints[IntNo].Name.substr(6);
130 TheStr += '.'; // Require "bswap." instead of bswap.
131 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
132 << Result << '\n';
Chris Lattner9b843b22006-03-09 20:34:19 +0000133 }
134
Chris Lattnercc67c752010-09-06 03:58:45 +0000135 // Emit the matcher logic for the fixed length strings.
136 StringMatcher("NameR", MatchTable, OS).Emit(1);
137 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000138 }
Chris Lattnercc67c752010-09-06 03:58:45 +0000139
Chris Lattner9b843b22006-03-09 20:34:19 +0000140 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000141 OS << "#endif\n\n";
142}
143
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000144void IntrinsicEmitter::
145EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000146 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000147 OS << "// Intrinsic ID to name table\n";
148 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
149 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000150 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
151 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000152 OS << "#endif\n\n";
153}
154
Mon P Wang0d52ff12009-02-24 23:17:49 +0000155void IntrinsicEmitter::
156EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000157 raw_ostream &OS) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000158 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000159 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000160 OS << "static const uint8_t OTable[] = {\n";
161 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000162 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000163 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
164 if ((i+1)%8 == 0)
165 OS << ",\n 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000166 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000167 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000168 }
Benjamin Kramer36a21382012-03-01 02:16:57 +0000169 OS << "\n};\n\n";
170 // OTable contains a true bit at the position if the intrinsic is overloaded.
171 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000172 OS << "#endif\n\n";
173}
174
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000175
Chris Lattner908a8312012-05-27 18:28:35 +0000176// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000177enum IIT_Info {
Chris Lattner46aaf692012-05-17 04:30:58 +0000178 // Common values should be encoded with 0-15.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000179 IIT_Done = 0,
180 IIT_I1 = 1,
181 IIT_I8 = 2,
182 IIT_I16 = 3,
183 IIT_I32 = 4,
184 IIT_I64 = 5,
185 IIT_F32 = 6,
186 IIT_F64 = 7,
187 IIT_V2 = 8,
188 IIT_V4 = 9,
189 IIT_V8 = 10,
190 IIT_V16 = 11,
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000191 IIT_V32 = 12,
192 IIT_MMX = 13,
193 IIT_PTR = 14,
194 IIT_ARG = 15,
Chris Lattner46aaf692012-05-17 04:30:58 +0000195
196 // Values from 16+ are only encodable with the inefficient encoding.
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000197 IIT_METADATA = 16,
198 IIT_EMPTYSTRUCT = 17,
199 IIT_STRUCT2 = 18,
200 IIT_STRUCT3 = 19,
201 IIT_STRUCT4 = 20,
202 IIT_STRUCT5 = 21,
203 IIT_EXTEND_VEC_ARG = 22,
Chris Lattnera48289a2012-05-23 05:19:18 +0000204 IIT_TRUNC_VEC_ARG = 23,
205 IIT_ANYPTR = 24
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000206};
207
Chris Lattner46aaf692012-05-17 04:30:58 +0000208
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000209static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000210 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000211 if (EVT(VT).isInteger()) {
212 unsigned BitWidth = EVT(VT).getSizeInBits();
213 switch (BitWidth) {
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000214 default: throw "unhandled integer type width in intrinsic!";
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000215 case 1: return Sig.push_back(IIT_I1);
216 case 8: return Sig.push_back(IIT_I8);
217 case 16: return Sig.push_back(IIT_I16);
218 case 32: return Sig.push_back(IIT_I32);
219 case 64: return Sig.push_back(IIT_I64);
220 }
221 }
222
Chris Lattner46aaf692012-05-17 04:30:58 +0000223 switch (VT) {
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000224 default: throw "unhandled MVT in intrinsic!";
Chris Lattner46aaf692012-05-17 04:30:58 +0000225 case MVT::f32: return Sig.push_back(IIT_F32);
226 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner46aaf692012-05-17 04:30:58 +0000227 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
228 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
229 // MVT::OtherVT is used to mean the empty struct type here.
230 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
231 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000232}
233
Francois Pichete4807c12012-05-17 04:00:03 +0000234#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000235#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Francois Pichete4807c12012-05-17 04:00:03 +0000236#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000237
Chris Lattnerb4654c12012-05-27 16:39:08 +0000238static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000239 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000240
241 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000242 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerb4654c12012-05-27 16:39:08 +0000243 assert(Number < ArgCodes.size() && "Invalid matching number!");
Chris Lattner46aaf692012-05-17 04:30:58 +0000244 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000245 Sig.push_back(IIT_EXTEND_VEC_ARG);
246 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
247 Sig.push_back(IIT_TRUNC_VEC_ARG);
248 else
249 Sig.push_back(IIT_ARG);
Chris Lattnerb4654c12012-05-27 16:39:08 +0000250 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000251 }
252
253 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner46aaf692012-05-17 04:30:58 +0000254
Chris Lattnerb4654c12012-05-27 16:39:08 +0000255 unsigned Tmp = 0;
Chris Lattnere82d5982012-05-26 23:03:52 +0000256 switch (VT) {
257 default: break;
Chris Lattnerb4654c12012-05-27 16:39:08 +0000258 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
259 case MVT::vAny: ++Tmp; // FALL THROUGH.
260 case MVT::fAny: ++Tmp; // FALL THROUGH.
261 case MVT::iAny: {
Chris Lattnere82d5982012-05-26 23:03:52 +0000262 // If this is an "any" valuetype, then the type is the type of the next
263 // type in the list specified to getIntrinsic().
Chris Lattner46aaf692012-05-17 04:30:58 +0000264 Sig.push_back(IIT_ARG);
Chris Lattnerb4654c12012-05-27 16:39:08 +0000265
266 // Figure out what arg # this is consuming, and remember what kind it was.
267 unsigned ArgNo = ArgCodes.size();
268 ArgCodes.push_back(Tmp);
269
270 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
271 return Sig.push_back((ArgNo << 2) | Tmp);
272 }
Chris Lattnere82d5982012-05-26 23:03:52 +0000273
274 case MVT::iPTR: {
275 unsigned AddrSpace = 0;
276 if (R->isSubClassOf("LLVMQualPointerType")) {
277 AddrSpace = R->getValueAsInt("AddrSpace");
278 assert(AddrSpace < 256 && "Address space exceeds 255");
279 }
280 if (AddrSpace) {
281 Sig.push_back(IIT_ANYPTR);
282 Sig.push_back(AddrSpace);
283 } else {
284 Sig.push_back(IIT_PTR);
285 }
Chris Lattnerb4654c12012-05-27 16:39:08 +0000286 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnere82d5982012-05-26 23:03:52 +0000287 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000288 }
289
290 if (EVT(VT).isVector()) {
291 EVT VVT = VT;
292 switch (VVT.getVectorNumElements()) {
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000293 default: throw "unhandled vector type width in intrinsic!";
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000294 case 2: Sig.push_back(IIT_V2); break;
295 case 4: Sig.push_back(IIT_V4); break;
296 case 8: Sig.push_back(IIT_V8); break;
297 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner46aaf692012-05-17 04:30:58 +0000298 case 32: Sig.push_back(IIT_V32); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000299 }
300
301 return EncodeFixedValueType(VVT.getVectorElementType().
302 getSimpleVT().SimpleTy, Sig);
303 }
Chris Lattnere82d5982012-05-26 23:03:52 +0000304
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000305 EncodeFixedValueType(VT, Sig);
306}
Francois Pichete4807c12012-05-17 04:00:03 +0000307
308#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000309#pragma optimize("",on)
Francois Pichete4807c12012-05-17 04:00:03 +0000310#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000311
312/// ComputeFixedEncoding - If we can encode the type signature for this
313/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattner387c9dc2012-05-17 15:55:41 +0000314static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
315 std::vector<unsigned char> &TypeSig) {
Chris Lattnerb4654c12012-05-27 16:39:08 +0000316 std::vector<unsigned char> ArgCodes;
Chris Lattner46aaf692012-05-17 04:30:58 +0000317
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000318 if (Int.IS.RetVTs.empty())
319 TypeSig.push_back(IIT_Done);
320 else if (Int.IS.RetVTs.size() == 1 &&
321 Int.IS.RetVTs[0] == MVT::isVoid)
322 TypeSig.push_back(IIT_Done);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000323 else {
324 switch (Int.IS.RetVTs.size()) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000325 case 1: break;
326 case 2: TypeSig.push_back(IIT_STRUCT2); break;
327 case 3: TypeSig.push_back(IIT_STRUCT3); break;
328 case 4: TypeSig.push_back(IIT_STRUCT4); break;
329 case 5: TypeSig.push_back(IIT_STRUCT5); break;
330 default: assert(0 && "Unhandled case in struct");
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000331 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000332
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000333 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000334 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000335 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000336
337 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerb4654c12012-05-27 16:39:08 +0000338 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner387c9dc2012-05-17 15:55:41 +0000339}
340
341void printIITEntry(raw_ostream &OS, unsigned char X) {
342 OS << (unsigned)X;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000343}
344
Jim Laskey95af5922007-02-07 20:38:26 +0000345void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000346 raw_ostream &OS) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000347 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
348 // capture it in this vector, otherwise store a ~0U.
349 std::vector<unsigned> FixedEncodings;
Jim Laskey95af5922007-02-07 20:38:26 +0000350
Chris Lattner387c9dc2012-05-17 15:55:41 +0000351 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
352
353 std::vector<unsigned char> TypeSig;
354
Jim Laskey95af5922007-02-07 20:38:26 +0000355 // Compute the unique argument type info.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000356 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000357 // Get the signature for the intrinsic.
358 TypeSig.clear();
359 ComputeFixedEncoding(Ints[i], TypeSig);
360
361 // Check to see if we can encode it into a 32-bit word. We can only encode
362 // 8 nibbles into a 32-bit word.
363 if (TypeSig.size() <= 8) {
364 bool Failed = false;
365 unsigned Result = 0;
366 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
367 // If we had an unencodable argument, bail out.
368 if (TypeSig[i] > 15) {
369 Failed = true;
370 break;
371 }
372 Result = (Result << 4) | TypeSig[e-i-1];
373 }
374
375 // If this could be encoded into a 31-bit word, return it.
376 if (!Failed && (Result >> 31) == 0) {
377 FixedEncodings.push_back(Result);
378 continue;
379 }
380 }
381
382 // Otherwise, we're going to unique the sequence into the
383 // LongEncodingTable, and use its offset in the 32-bit table instead.
384 LongEncodingTable.add(TypeSig);
385
386 // This is a placehold that we'll replace after the table is laid out.
387 FixedEncodings.push_back(~0U);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000388 }
389
Chris Lattner387c9dc2012-05-17 15:55:41 +0000390 LongEncodingTable.layout();
391
Chris Lattner908a8312012-05-27 18:28:35 +0000392 OS << "// Global intrinsic function declaration type table.\n";
393 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
394
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000395 OS << "static const unsigned IIT_Table[] = {\n ";
396
397 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
398 if ((i & 7) == 7)
399 OS << "\n ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000400
401 // If the entry fit in the table, just emit it.
402 if (FixedEncodings[i] != ~0U) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000403 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000404 continue;
Jim Laskey95af5922007-02-07 20:38:26 +0000405 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000406
407 TypeSig.clear();
408 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000409
Chris Lattner387c9dc2012-05-17 15:55:41 +0000410
411 // Otherwise, emit the offset into the long encoding table. We emit it this
412 // way so that it is easier to read the offset in the .def file.
413 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey95af5922007-02-07 20:38:26 +0000414 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000415
416 OS << "0\n};\n\n";
417
418 // Emit the shared table of register lists.
419 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
420 if (!LongEncodingTable.empty())
421 LongEncodingTable.emit(OS, printIITEntry);
422 OS << " 255\n};\n\n";
423
Patrik Hägglund9ce6f6f2012-05-23 12:34:56 +0000424 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey95af5922007-02-07 20:38:26 +0000425}
426
John McCallbd0fa4c2011-05-28 06:31:34 +0000427namespace {
428 enum ModRefKind {
429 MRK_none,
430 MRK_readonly,
431 MRK_readnone
432 };
433
434 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
435 switch (intrinsic.ModRef) {
436 case CodeGenIntrinsic::NoMem:
437 return MRK_readnone;
438 case CodeGenIntrinsic::ReadArgMem:
439 case CodeGenIntrinsic::ReadMem:
440 return MRK_readonly;
441 case CodeGenIntrinsic::ReadWriteArgMem:
442 case CodeGenIntrinsic::ReadWriteMem:
443 return MRK_none;
444 }
Craig Topper655b8de2012-02-05 07:21:30 +0000445 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000446 }
447
448 struct AttributeComparator {
449 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
450 // Sort throwing intrinsics after non-throwing intrinsics.
451 if (L->canThrow != R->canThrow)
452 return R->canThrow;
453
Chris Lattner86208902012-05-27 23:20:41 +0000454 if (L->isNoReturn != R->isNoReturn)
455 return R->isNoReturn;
456
John McCallbd0fa4c2011-05-28 06:31:34 +0000457 // Try to order by readonly/readnone attribute.
458 ModRefKind LK = getModRefKind(*L);
459 ModRefKind RK = getModRefKind(*R);
460 if (LK != RK) return (LK > RK);
461
462 // Order by argument attributes.
463 // This is reliable because each side is already sorted internally.
464 return (L->ArgumentAttributes < R->ArgumentAttributes);
465 }
466 };
467}
468
Chris Lattner048ffb22009-01-12 01:18:58 +0000469/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000470void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000471EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000472 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
473 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000474 if (TargetOnly)
475 OS << "static AttrListPtr getAttributes(" << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000476 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000477 else
John McCallbd0fa4c2011-05-28 06:31:34 +0000478 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
479
Craig Topper1f595232012-02-28 06:32:00 +0000480 // Compute the maximum number of attribute arguments and the map
481 typedef std::map<const CodeGenIntrinsic*, unsigned,
482 AttributeComparator> UniqAttrMapTy;
483 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000484 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000485 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000486 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000487 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000488 maxArgAttrs =
489 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000490 unsigned &N = UniqAttributes[&intrinsic];
491 if (N) continue;
492 assert(AttrNum < 256 && "Too many unique attributes for table!");
493 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000494 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000495
496 // Emit an array of AttributeWithIndex. Most intrinsics will have
497 // at least one entry, for the function itself (index ~1), which is
498 // usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000499 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topper1f595232012-02-28 06:32:00 +0000500
501 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
502 const CodeGenIntrinsic &intrinsic = Ints[i];
503
504 OS << " " << UniqAttributes[&intrinsic] << ", // "
505 << intrinsic.Name << "\n";
506 }
507 OS << " };\n\n";
508
John McCallbd0fa4c2011-05-28 06:31:34 +0000509 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000510 OS << " unsigned NumAttrs = 0;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000511 OS << " if (id != 0) {\n";
512 OS << " switch(IntrinsicsToAttributesMap[id - ";
513 if (TargetOnly)
514 OS << "Intrinsic::num_intrinsics";
515 else
516 OS << "1";
517 OS << "]) {\n";
518 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topper1f595232012-02-28 06:32:00 +0000519 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
520 E = UniqAttributes.end(); I != E; ++I) {
Craig Topperb57b1702012-04-13 06:14:57 +0000521 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000522
Craig Topper1f595232012-02-28 06:32:00 +0000523 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000524
525 // Keep track of the number of attributes we're writing out.
526 unsigned numAttrs = 0;
527
528 // The argument attributes are alreadys sorted by argument index.
529 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
530 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000531
Craig Topperb57b1702012-04-13 06:14:57 +0000532 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
John McCallbd0fa4c2011-05-28 06:31:34 +0000533 << argNo+1 << ", ";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000534
John McCallbd0fa4c2011-05-28 06:31:34 +0000535 bool moreThanOne = false;
536
537 do {
538 if (moreThanOne) OS << '|';
539
540 switch (intrinsic.ArgumentAttributes[ai].second) {
Chris Lattnerd4a27002009-01-12 02:41:37 +0000541 case CodeGenIntrinsic::NoCapture:
John McCallbd0fa4c2011-05-28 06:31:34 +0000542 OS << "Attribute::NoCapture";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000543 break;
544 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000545
546 ++ai;
547 moreThanOne = true;
548 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
549
550 OS << ");\n";
551 }
552
553 ModRefKind modRef = getModRefKind(intrinsic);
554
Chris Lattner86208902012-05-27 23:20:41 +0000555 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Craig Topperb57b1702012-04-13 06:14:57 +0000556 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
Chris Lattner86208902012-05-27 23:20:41 +0000557 bool Emitted = false;
John McCallbd0fa4c2011-05-28 06:31:34 +0000558 if (!intrinsic.canThrow) {
559 OS << "Attribute::NoUnwind";
Chris Lattner86208902012-05-27 23:20:41 +0000560 Emitted = true;
John McCallbd0fa4c2011-05-28 06:31:34 +0000561 }
Chris Lattner86208902012-05-27 23:20:41 +0000562
563 if (intrinsic.isNoReturn) {
564 if (Emitted) OS << '|';
565 OS << "Attribute::NoReturn";
566 Emitted = true;
567 }
568
John McCallbd0fa4c2011-05-28 06:31:34 +0000569 switch (modRef) {
570 case MRK_none: break;
Chris Lattner86208902012-05-27 23:20:41 +0000571 case MRK_readonly:
572 if (Emitted) OS << '|';
573 OS << "Attribute::ReadOnly";
574 break;
575 case MRK_readnone:
576 if (Emitted) OS << '|';
577 OS << "Attribute::ReadNone";
578 break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000579 }
580 OS << ");\n";
581 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000582
583 if (numAttrs) {
Craig Topperb57b1702012-04-13 06:14:57 +0000584 OS << " NumAttrs = " << numAttrs << ";\n";
585 OS << " break;\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000586 } else {
Craig Topperb57b1702012-04-13 06:14:57 +0000587 OS << " return AttrListPtr();\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000588 }
Chris Lattner10dae942009-01-12 01:27:55 +0000589 }
590
Craig Topperb57b1702012-04-13 06:14:57 +0000591 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000592 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000593 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000594 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000595 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000596}
Chris Lattner022f64f2006-03-13 23:08:44 +0000597
Duncan Sandsd869b382009-02-14 10:56:35 +0000598/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
599void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000600EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000601 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
602 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
603 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
604 << "\"Unknown intrinsic.\");\n\n";
605
606 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
607 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000608 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000609 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sandsd869b382009-02-14 10:56:35 +0000610 switch (Ints[i].ModRef) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000611 case CodeGenIntrinsic::NoMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000612 OS << "DoesNotAccessMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000613 break;
614 case CodeGenIntrinsic::ReadArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000615 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000616 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000617 case CodeGenIntrinsic::ReadMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000618 OS << "OnlyReadsMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000619 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000620 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000621 OS << "OnlyAccessesArgumentPointees,\n";
622 break;
623 case CodeGenIntrinsic::ReadWriteMem:
624 OS << "UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000625 break;
626 }
627 }
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000628 OS << "};\n\n"
629 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
630 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000631}
632
Chris Lattner331bf922008-01-04 04:38:35 +0000633/// EmitTargetBuiltins - All of the builtins in the specified map are for the
634/// same target, and we already checked it.
635static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000636 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000637 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000638
Chris Lattner298b1762010-09-06 03:14:45 +0000639 std::vector<StringMatcher::StringPair> Results;
Chris Lattner331bf922008-01-04 04:38:35 +0000640
Chris Lattner298b1762010-09-06 03:14:45 +0000641 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
642 E = BIM.end(); I != E; ++I) {
643 std::string ResultCode =
644 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
645 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000646 }
Chris Lattner298b1762010-09-06 03:14:45 +0000647
648 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000649}
650
651
Chris Lattner3f8b8912006-03-15 01:33:26 +0000652void IntrinsicEmitter::
653EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000654 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000655 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000656 BIMTy BuiltinMap;
657 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
658 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000659 // Get the map for this target prefix.
660 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
661
662 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
663 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000664 throw "Intrinsic '" + Ints[i].TheDef->getName() +
665 "': duplicate GCC builtin name!";
666 }
667 }
668
669 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
670 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
671 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
672 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
673 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000674
675 if (TargetOnly) {
676 OS << "static " << TargetPrefix << "Intrinsic::ID "
677 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000678 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000679 } else {
680 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000681 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000682 }
683
Chris Lattner298b1762010-09-06 03:14:45 +0000684 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
685 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000686
Chris Lattner3f8b8912006-03-15 01:33:26 +0000687 // Note: this could emit significantly better code if we cared.
688 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000689 OS << " ";
690 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000691 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000692 else
693 OS << "/* Target Independent Builtins */ ";
694 OS << "{\n";
695
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000696 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000697 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000698 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000699 }
Chris Lattner298b1762010-09-06 03:14:45 +0000700 OS << " return ";
701 if (!TargetPrefix.empty())
702 OS << "(" << TargetPrefix << "Intrinsic::ID)";
703 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000704 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000705 OS << "#endif\n\n";
706}