1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits information about intrinsic functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "CodeGenIntrinsics.h"
15 #include "CodeGenTarget.h"
16 #include "SequenceToOffsetTable.h"
17 #include "TableGenBackends.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 #include "llvm/TableGen/StringMatcher.h"
22 #include "llvm/TableGen/TableGenBackend.h"
23 #include "llvm/TableGen/StringToOffsetTable.h"
24 #include <algorithm>
25 using namespace llvm;
26
27 namespace {
28 class IntrinsicEmitter {
29 RecordKeeper &Records;
30 bool TargetOnly;
31 std::string TargetPrefix;
32
33 public:
IntrinsicEmitter(RecordKeeper & R,bool T)34 IntrinsicEmitter(RecordKeeper &R, bool T)
35 : Records(R), TargetOnly(T) {}
36
37 void run(raw_ostream &OS);
38
39 void EmitPrefix(raw_ostream &OS);
40
41 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
42 raw_ostream &OS);
43
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 EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
49 raw_ostream &OS);
50 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
51 raw_ostream &OS);
52 void EmitIntrinsicToBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
53 bool IsGCC, raw_ostream &OS);
54 void EmitSuffix(raw_ostream &OS);
55 };
56 } // End anonymous namespace
57
58 //===----------------------------------------------------------------------===//
59 // IntrinsicEmitter Implementation
60 //===----------------------------------------------------------------------===//
61
run(raw_ostream & OS)62 void IntrinsicEmitter::run(raw_ostream &OS) {
63 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
64
65 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
66
67 if (TargetOnly && !Ints.empty())
68 TargetPrefix = Ints[0].TargetPrefix;
69
70 EmitPrefix(OS);
71
72 // Emit the enum information.
73 EmitEnumInfo(Ints, OS);
74
75 // Emit the intrinsic ID -> name table.
76 EmitIntrinsicToNameTable(Ints, OS);
77
78 // Emit the intrinsic ID -> overload table.
79 EmitIntrinsicToOverloadTable(Ints, OS);
80
81 // Emit the intrinsic declaration generator.
82 EmitGenerator(Ints, OS);
83
84 // Emit the intrinsic parameter attributes.
85 EmitAttributes(Ints, OS);
86
87 // Individual targets don't need GCC builtin name mappings.
88 if (!TargetOnly) {
89 // Emit code to translate GCC builtins into LLVM intrinsics.
90 EmitIntrinsicToBuiltinMap(Ints, true, OS);
91
92 // Emit code to translate MS builtins into LLVM intrinsics.
93 EmitIntrinsicToBuiltinMap(Ints, false, OS);
94 }
95
96 EmitSuffix(OS);
97 }
98
EmitPrefix(raw_ostream & OS)99 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
100 OS << "// VisualStudio defines setjmp as _setjmp\n"
101 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
102 " !defined(setjmp_undefined_for_msvc)\n"
103 "# pragma push_macro(\"setjmp\")\n"
104 "# undef setjmp\n"
105 "# define setjmp_undefined_for_msvc\n"
106 "#endif\n\n";
107 }
108
EmitSuffix(raw_ostream & OS)109 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
110 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
111 "// let's return it to _setjmp state\n"
112 "# pragma pop_macro(\"setjmp\")\n"
113 "# undef setjmp_undefined_for_msvc\n"
114 "#endif\n\n";
115 }
116
EmitEnumInfo(const std::vector<CodeGenIntrinsic> & Ints,raw_ostream & OS)117 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
118 raw_ostream &OS) {
119 OS << "// Enum values for Intrinsics.h\n";
120 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
121 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
122 OS << " " << Ints[i].EnumName;
123 OS << ((i != e-1) ? ", " : " ");
124 if (Ints[i].EnumName.size() < 40)
125 OS << std::string(40-Ints[i].EnumName.size(), ' ');
126 OS << " // " << Ints[i].Name << "\n";
127 }
128 OS << "#endif\n\n";
129 }
130
131 void IntrinsicEmitter::
EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> & Ints,raw_ostream & OS)132 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
133 raw_ostream &OS) {
134 OS << "// Intrinsic ID to name table\n";
135 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
136 OS << " // Note that entry #0 is the invalid intrinsic!\n";
137 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
138 OS << " \"" << Ints[i].Name << "\",\n";
139 OS << "#endif\n\n";
140 }
141
142 void IntrinsicEmitter::
EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> & Ints,raw_ostream & OS)143 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
144 raw_ostream &OS) {
145 OS << "// Intrinsic ID to overload bitset\n";
146 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
147 OS << "static const uint8_t OTable[] = {\n";
148 OS << " 0";
149 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
150 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
151 if ((i+1)%8 == 0)
152 OS << ",\n 0";
153 if (Ints[i].isOverloaded)
154 OS << " | (1<<" << (i+1)%8 << ')';
155 }
156 OS << "\n};\n\n";
157 // OTable contains a true bit at the position if the intrinsic is overloaded.
158 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
159 OS << "#endif\n\n";
160 }
161
162
163 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
164 enum IIT_Info {
165 // Common values should be encoded with 0-15.
166 IIT_Done = 0,
167 IIT_I1 = 1,
168 IIT_I8 = 2,
169 IIT_I16 = 3,
170 IIT_I32 = 4,
171 IIT_I64 = 5,
172 IIT_F16 = 6,
173 IIT_F32 = 7,
174 IIT_F64 = 8,
175 IIT_V2 = 9,
176 IIT_V4 = 10,
177 IIT_V8 = 11,
178 IIT_V16 = 12,
179 IIT_V32 = 13,
180 IIT_PTR = 14,
181 IIT_ARG = 15,
182
183 // Values from 16+ are only encodable with the inefficient encoding.
184 IIT_V64 = 16,
185 IIT_MMX = 17,
186 IIT_TOKEN = 18,
187 IIT_METADATA = 19,
188 IIT_EMPTYSTRUCT = 20,
189 IIT_STRUCT2 = 21,
190 IIT_STRUCT3 = 22,
191 IIT_STRUCT4 = 23,
192 IIT_STRUCT5 = 24,
193 IIT_EXTEND_ARG = 25,
194 IIT_TRUNC_ARG = 26,
195 IIT_ANYPTR = 27,
196 IIT_V1 = 28,
197 IIT_VARARG = 29,
198 IIT_HALF_VEC_ARG = 30,
199 IIT_SAME_VEC_WIDTH_ARG = 31,
200 IIT_PTR_TO_ARG = 32,
201 IIT_VEC_OF_PTRS_TO_ELT = 33,
202 IIT_I128 = 34,
203 IIT_V512 = 35,
204 IIT_V1024 = 36
205 };
206
207
EncodeFixedValueType(MVT::SimpleValueType VT,std::vector<unsigned char> & Sig)208 static void EncodeFixedValueType(MVT::SimpleValueType VT,
209 std::vector<unsigned char> &Sig) {
210 if (MVT(VT).isInteger()) {
211 unsigned BitWidth = MVT(VT).getSizeInBits();
212 switch (BitWidth) {
213 default: PrintFatalError("unhandled integer type width in intrinsic!");
214 case 1: return Sig.push_back(IIT_I1);
215 case 8: return Sig.push_back(IIT_I8);
216 case 16: return Sig.push_back(IIT_I16);
217 case 32: return Sig.push_back(IIT_I32);
218 case 64: return Sig.push_back(IIT_I64);
219 case 128: return Sig.push_back(IIT_I128);
220 }
221 }
222
223 switch (VT) {
224 default: PrintFatalError("unhandled MVT in intrinsic!");
225 case MVT::f16: return Sig.push_back(IIT_F16);
226 case MVT::f32: return Sig.push_back(IIT_F32);
227 case MVT::f64: return Sig.push_back(IIT_F64);
228 case MVT::token: return Sig.push_back(IIT_TOKEN);
229 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
230 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
231 // MVT::OtherVT is used to mean the empty struct type here.
232 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
233 // MVT::isVoid is used to represent varargs here.
234 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
235 }
236 }
237
238 #if defined(_MSC_VER) && !defined(__clang__)
239 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
240 #endif
241
EncodeFixedType(Record * R,std::vector<unsigned char> & ArgCodes,std::vector<unsigned char> & Sig)242 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
243 std::vector<unsigned char> &Sig) {
244
245 if (R->isSubClassOf("LLVMMatchType")) {
246 unsigned Number = R->getValueAsInt("Number");
247 assert(Number < ArgCodes.size() && "Invalid matching number!");
248 if (R->isSubClassOf("LLVMExtendedType"))
249 Sig.push_back(IIT_EXTEND_ARG);
250 else if (R->isSubClassOf("LLVMTruncatedType"))
251 Sig.push_back(IIT_TRUNC_ARG);
252 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
253 Sig.push_back(IIT_HALF_VEC_ARG);
254 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
255 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
256 Sig.push_back((Number << 3) | ArgCodes[Number]);
257 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
258 EncodeFixedValueType(VT, Sig);
259 return;
260 }
261 else if (R->isSubClassOf("LLVMPointerTo"))
262 Sig.push_back(IIT_PTR_TO_ARG);
263 else if (R->isSubClassOf("LLVMVectorOfPointersToElt"))
264 Sig.push_back(IIT_VEC_OF_PTRS_TO_ELT);
265 else
266 Sig.push_back(IIT_ARG);
267 return Sig.push_back((Number << 3) | ArgCodes[Number]);
268 }
269
270 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
271
272 unsigned Tmp = 0;
273 switch (VT) {
274 default: break;
275 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
276 case MVT::vAny: ++Tmp; // FALL THROUGH.
277 case MVT::fAny: ++Tmp; // FALL THROUGH.
278 case MVT::iAny: ++Tmp; // FALL THROUGH.
279 case MVT::Any: {
280 // If this is an "any" valuetype, then the type is the type of the next
281 // type in the list specified to getIntrinsic().
282 Sig.push_back(IIT_ARG);
283
284 // Figure out what arg # this is consuming, and remember what kind it was.
285 unsigned ArgNo = ArgCodes.size();
286 ArgCodes.push_back(Tmp);
287
288 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
289 return Sig.push_back((ArgNo << 3) | Tmp);
290 }
291
292 case MVT::iPTR: {
293 unsigned AddrSpace = 0;
294 if (R->isSubClassOf("LLVMQualPointerType")) {
295 AddrSpace = R->getValueAsInt("AddrSpace");
296 assert(AddrSpace < 256 && "Address space exceeds 255");
297 }
298 if (AddrSpace) {
299 Sig.push_back(IIT_ANYPTR);
300 Sig.push_back(AddrSpace);
301 } else {
302 Sig.push_back(IIT_PTR);
303 }
304 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
305 }
306 }
307
308 if (MVT(VT).isVector()) {
309 MVT VVT = VT;
310 switch (VVT.getVectorNumElements()) {
311 default: PrintFatalError("unhandled vector type width in intrinsic!");
312 case 1: Sig.push_back(IIT_V1); break;
313 case 2: Sig.push_back(IIT_V2); break;
314 case 4: Sig.push_back(IIT_V4); break;
315 case 8: Sig.push_back(IIT_V8); break;
316 case 16: Sig.push_back(IIT_V16); break;
317 case 32: Sig.push_back(IIT_V32); break;
318 case 64: Sig.push_back(IIT_V64); break;
319 case 512: Sig.push_back(IIT_V512); break;
320 case 1024: Sig.push_back(IIT_V1024); break;
321 }
322
323 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
324 }
325
326 EncodeFixedValueType(VT, Sig);
327 }
328
329 #if defined(_MSC_VER) && !defined(__clang__)
330 #pragma optimize("",on)
331 #endif
332
333 /// ComputeFixedEncoding - If we can encode the type signature for this
334 /// intrinsic into 32 bits, return it. If not, return ~0U.
ComputeFixedEncoding(const CodeGenIntrinsic & Int,std::vector<unsigned char> & TypeSig)335 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
336 std::vector<unsigned char> &TypeSig) {
337 std::vector<unsigned char> ArgCodes;
338
339 if (Int.IS.RetVTs.empty())
340 TypeSig.push_back(IIT_Done);
341 else if (Int.IS.RetVTs.size() == 1 &&
342 Int.IS.RetVTs[0] == MVT::isVoid)
343 TypeSig.push_back(IIT_Done);
344 else {
345 switch (Int.IS.RetVTs.size()) {
346 case 1: break;
347 case 2: TypeSig.push_back(IIT_STRUCT2); break;
348 case 3: TypeSig.push_back(IIT_STRUCT3); break;
349 case 4: TypeSig.push_back(IIT_STRUCT4); break;
350 case 5: TypeSig.push_back(IIT_STRUCT5); break;
351 default: llvm_unreachable("Unhandled case in struct");
352 }
353
354 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
355 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
356 }
357
358 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
359 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
360 }
361
printIITEntry(raw_ostream & OS,unsigned char X)362 static void printIITEntry(raw_ostream &OS, unsigned char X) {
363 OS << (unsigned)X;
364 }
365
EmitGenerator(const std::vector<CodeGenIntrinsic> & Ints,raw_ostream & OS)366 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
367 raw_ostream &OS) {
368 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
369 // capture it in this vector, otherwise store a ~0U.
370 std::vector<unsigned> FixedEncodings;
371
372 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
373
374 std::vector<unsigned char> TypeSig;
375
376 // Compute the unique argument type info.
377 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
378 // Get the signature for the intrinsic.
379 TypeSig.clear();
380 ComputeFixedEncoding(Ints[i], TypeSig);
381
382 // Check to see if we can encode it into a 32-bit word. We can only encode
383 // 8 nibbles into a 32-bit word.
384 if (TypeSig.size() <= 8) {
385 bool Failed = false;
386 unsigned Result = 0;
387 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
388 // If we had an unencodable argument, bail out.
389 if (TypeSig[i] > 15) {
390 Failed = true;
391 break;
392 }
393 Result = (Result << 4) | TypeSig[e-i-1];
394 }
395
396 // If this could be encoded into a 31-bit word, return it.
397 if (!Failed && (Result >> 31) == 0) {
398 FixedEncodings.push_back(Result);
399 continue;
400 }
401 }
402
403 // Otherwise, we're going to unique the sequence into the
404 // LongEncodingTable, and use its offset in the 32-bit table instead.
405 LongEncodingTable.add(TypeSig);
406
407 // This is a placehold that we'll replace after the table is laid out.
408 FixedEncodings.push_back(~0U);
409 }
410
411 LongEncodingTable.layout();
412
413 OS << "// Global intrinsic function declaration type table.\n";
414 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
415
416 OS << "static const unsigned IIT_Table[] = {\n ";
417
418 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
419 if ((i & 7) == 7)
420 OS << "\n ";
421
422 // If the entry fit in the table, just emit it.
423 if (FixedEncodings[i] != ~0U) {
424 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
425 continue;
426 }
427
428 TypeSig.clear();
429 ComputeFixedEncoding(Ints[i], TypeSig);
430
431
432 // Otherwise, emit the offset into the long encoding table. We emit it this
433 // way so that it is easier to read the offset in the .def file.
434 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
435 }
436
437 OS << "0\n};\n\n";
438
439 // Emit the shared table of register lists.
440 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
441 if (!LongEncodingTable.empty())
442 LongEncodingTable.emit(OS, printIITEntry);
443 OS << " 255\n};\n\n";
444
445 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
446 }
447
448 namespace {
449 struct AttributeComparator {
operator ()__anon4d8cfaca0211::AttributeComparator450 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
451 // Sort throwing intrinsics after non-throwing intrinsics.
452 if (L->canThrow != R->canThrow)
453 return R->canThrow;
454
455 if (L->isNoDuplicate != R->isNoDuplicate)
456 return R->isNoDuplicate;
457
458 if (L->isNoReturn != R->isNoReturn)
459 return R->isNoReturn;
460
461 if (L->isConvergent != R->isConvergent)
462 return R->isConvergent;
463
464 // Try to order by readonly/readnone attribute.
465 CodeGenIntrinsic::ModRefBehavior LK = L->ModRef;
466 CodeGenIntrinsic::ModRefBehavior RK = R->ModRef;
467 if (LK != RK) return (LK > RK);
468
469 // Order by argument attributes.
470 // This is reliable because each side is already sorted internally.
471 return (L->ArgumentAttributes < R->ArgumentAttributes);
472 }
473 };
474 } // End anonymous namespace
475
476 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
477 void IntrinsicEmitter::
EmitAttributes(const std::vector<CodeGenIntrinsic> & Ints,raw_ostream & OS)478 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
479 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
480 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
481 if (TargetOnly)
482 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
483 << "Intrinsic::ID id) {\n";
484 else
485 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
486
487 // Compute the maximum number of attribute arguments and the map
488 typedef std::map<const CodeGenIntrinsic*, unsigned,
489 AttributeComparator> UniqAttrMapTy;
490 UniqAttrMapTy UniqAttributes;
491 unsigned maxArgAttrs = 0;
492 unsigned AttrNum = 0;
493 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
494 const CodeGenIntrinsic &intrinsic = Ints[i];
495 maxArgAttrs =
496 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
497 unsigned &N = UniqAttributes[&intrinsic];
498 if (N) continue;
499 assert(AttrNum < 256 && "Too many unique attributes for table!");
500 N = ++AttrNum;
501 }
502
503 // Emit an array of AttributeSet. Most intrinsics will have at least one
504 // entry, for the function itself (index ~1), which is usually nounwind.
505 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
506
507 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
508 const CodeGenIntrinsic &intrinsic = Ints[i];
509
510 OS << " " << UniqAttributes[&intrinsic] << ", // "
511 << intrinsic.Name << "\n";
512 }
513 OS << " };\n\n";
514
515 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
516 OS << " unsigned NumAttrs = 0;\n";
517 OS << " if (id != 0) {\n";
518 OS << " switch(IntrinsicsToAttributesMap[id - ";
519 if (TargetOnly)
520 OS << "Intrinsic::num_intrinsics";
521 else
522 OS << "1";
523 OS << "]) {\n";
524 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
525 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
526 E = UniqAttributes.end(); I != E; ++I) {
527 OS << " case " << I->second << ": {\n";
528
529 const CodeGenIntrinsic &intrinsic = *(I->first);
530
531 // Keep track of the number of attributes we're writing out.
532 unsigned numAttrs = 0;
533
534 // The argument attributes are alreadys sorted by argument index.
535 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
536 if (ae) {
537 while (ai != ae) {
538 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
539
540 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
541 bool addComma = false;
542
543 do {
544 switch (intrinsic.ArgumentAttributes[ai].second) {
545 case CodeGenIntrinsic::NoCapture:
546 if (addComma)
547 OS << ",";
548 OS << "Attribute::NoCapture";
549 addComma = true;
550 break;
551 case CodeGenIntrinsic::Returned:
552 if (addComma)
553 OS << ",";
554 OS << "Attribute::Returned";
555 addComma = true;
556 break;
557 case CodeGenIntrinsic::ReadOnly:
558 if (addComma)
559 OS << ",";
560 OS << "Attribute::ReadOnly";
561 addComma = true;
562 break;
563 case CodeGenIntrinsic::WriteOnly:
564 if (addComma)
565 OS << ",";
566 OS << "Attribute::WriteOnly";
567 addComma = true;
568 break;
569 case CodeGenIntrinsic::ReadNone:
570 if (addComma)
571 OS << ",";
572 OS << "Attribute::ReadNone";
573 addComma = true;
574 break;
575 }
576
577 ++ai;
578 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
579 OS << "};\n";
580 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
581 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
582 }
583 }
584
585 if (!intrinsic.canThrow ||
586 intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem ||
587 intrinsic.isNoReturn || intrinsic.isNoDuplicate ||
588 intrinsic.isConvergent) {
589 OS << " const Attribute::AttrKind Atts[] = {";
590 bool addComma = false;
591 if (!intrinsic.canThrow) {
592 OS << "Attribute::NoUnwind";
593 addComma = true;
594 }
595 if (intrinsic.isNoReturn) {
596 if (addComma)
597 OS << ",";
598 OS << "Attribute::NoReturn";
599 addComma = true;
600 }
601 if (intrinsic.isNoDuplicate) {
602 if (addComma)
603 OS << ",";
604 OS << "Attribute::NoDuplicate";
605 addComma = true;
606 }
607 if (intrinsic.isConvergent) {
608 if (addComma)
609 OS << ",";
610 OS << "Attribute::Convergent";
611 addComma = true;
612 }
613
614 switch (intrinsic.ModRef) {
615 case CodeGenIntrinsic::NoMem:
616 if (addComma)
617 OS << ",";
618 OS << "Attribute::ReadNone";
619 break;
620 case CodeGenIntrinsic::ReadArgMem:
621 if (addComma)
622 OS << ",";
623 OS << "Attribute::ReadOnly,";
624 OS << "Attribute::ArgMemOnly";
625 break;
626 case CodeGenIntrinsic::ReadMem:
627 if (addComma)
628 OS << ",";
629 OS << "Attribute::ReadOnly";
630 break;
631 case CodeGenIntrinsic::WriteArgMem:
632 if (addComma)
633 OS << ",";
634 OS << "Attribute::WriteOnly,";
635 OS << "Attribute::ArgMemOnly";
636 break;
637 case CodeGenIntrinsic::WriteMem:
638 if (addComma)
639 OS << ",";
640 OS << "Attribute::WriteOnly";
641 break;
642 case CodeGenIntrinsic::ReadWriteArgMem:
643 if (addComma)
644 OS << ",";
645 OS << "Attribute::ArgMemOnly";
646 break;
647 case CodeGenIntrinsic::ReadWriteMem:
648 break;
649 }
650 OS << "};\n";
651 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
652 << "AttributeSet::FunctionIndex, Atts);\n";
653 }
654
655 if (numAttrs) {
656 OS << " NumAttrs = " << numAttrs << ";\n";
657 OS << " break;\n";
658 OS << " }\n";
659 } else {
660 OS << " return AttributeSet();\n";
661 OS << " }\n";
662 }
663 }
664
665 OS << " }\n";
666 OS << " }\n";
667 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
668 OS << "}\n";
669 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
670 }
671
EmitIntrinsicToBuiltinMap(const std::vector<CodeGenIntrinsic> & Ints,bool IsGCC,raw_ostream & OS)672 void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
673 const std::vector<CodeGenIntrinsic> &Ints, bool IsGCC, raw_ostream &OS) {
674 StringRef CompilerName = (IsGCC ? "GCC" : "MS");
675 typedef std::map<std::string, std::map<std::string, std::string>> BIMTy;
676 BIMTy BuiltinMap;
677 StringToOffsetTable Table;
678 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
679 const std::string &BuiltinName =
680 IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName;
681 if (!BuiltinName.empty()) {
682 // Get the map for this target prefix.
683 std::map<std::string, std::string> &BIM =
684 BuiltinMap[Ints[i].TargetPrefix];
685
686 if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second)
687 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
688 "': duplicate " + CompilerName + " builtin name!");
689 Table.GetOrAddStringOffset(BuiltinName);
690 }
691 }
692
693 OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n";
694 OS << "// This is used by the C front-end. The builtin name is passed\n";
695 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
696 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
697 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n";
698
699 if (TargetOnly) {
700 OS << "static " << TargetPrefix << "Intrinsic::ID "
701 << "getIntrinsicFor" << CompilerName << "Builtin(const char "
702 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
703 } else {
704 OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName
705 << "Builtin(const char "
706 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
707 }
708 OS << " static const char BuiltinNames[] = {\n";
709 Table.EmitCharArray(OS);
710 OS << " };\n\n";
711
712 OS << " struct BuiltinEntry {\n";
713 OS << " Intrinsic::ID IntrinID;\n";
714 OS << " unsigned StrTabOffset;\n";
715 OS << " const char *getName() const {\n";
716 OS << " return &BuiltinNames[StrTabOffset];\n";
717 OS << " }\n";
718 OS << " bool operator<(const char *RHS) const {\n";
719 OS << " return strcmp(getName(), RHS) < 0;\n";
720 OS << " }\n";
721 OS << " };\n";
722
723
724 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
725 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
726
727 // Note: this could emit significantly better code if we cared.
728 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
729 OS << " ";
730 if (!I->first.empty())
731 OS << "if (TargetPrefix == \"" << I->first << "\") ";
732 else
733 OS << "/* Target Independent Builtins */ ";
734 OS << "{\n";
735
736 // Emit the comparisons for this target prefix.
737 OS << " static const BuiltinEntry " << I->first << "Names[] = {\n";
738 for (const auto &P : I->second) {
739 OS << " {Intrinsic::" << P.second << ", "
740 << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n";
741 }
742 OS << " };\n";
743 OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n";
744 OS << " std::end(" << I->first << "Names),\n";
745 OS << " BuiltinNameStr);\n";
746 OS << " if (I != std::end(" << I->first << "Names) &&\n";
747 OS << " strcmp(I->getName(), BuiltinNameStr) == 0)\n";
748 OS << " return I->IntrinID;\n";
749 OS << " }\n";
750 }
751 OS << " return ";
752 if (!TargetPrefix.empty())
753 OS << "(" << TargetPrefix << "Intrinsic::ID)";
754 OS << "Intrinsic::not_intrinsic;\n";
755 OS << "}\n";
756 OS << "#endif\n\n";
757 }
758
EmitIntrinsics(RecordKeeper & RK,raw_ostream & OS,bool TargetOnly)759 void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
760 IntrinsicEmitter(RK, TargetOnly).run(OS);
761 }
762