1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits information about intrinsic functions.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenTarget.h"
15 #include "IntrinsicEmitter.h"
16 #include "SequenceToOffsetTable.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/TableGen/StringMatcher.h"
19 #include "llvm/ADT/StringExtras.h"
23 //===----------------------------------------------------------------------===//
24 // IntrinsicEmitter Implementation
25 //===----------------------------------------------------------------------===//
27 void IntrinsicEmitter::run(raw_ostream &OS) {
28 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
30 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
32 if (TargetOnly && !Ints.empty())
33 TargetPrefix = Ints[0].TargetPrefix;
37 // Emit the enum information.
38 EmitEnumInfo(Ints, OS);
40 // Emit the intrinsic ID -> name table.
41 EmitIntrinsicToNameTable(Ints, OS);
43 // Emit the intrinsic ID -> overload table.
44 EmitIntrinsicToOverloadTable(Ints, OS);
46 // Emit the function name recognizer.
47 EmitFnNameRecognizer(Ints, OS);
49 // Emit the intrinsic declaration generator.
50 EmitGenerator(Ints, OS);
52 // Emit the intrinsic parameter attributes.
53 EmitAttributes(Ints, OS);
55 // Emit intrinsic alias analysis mod/ref behavior.
56 EmitModRefBehavior(Ints, OS);
58 // Emit code to translate GCC builtins into LLVM intrinsics.
59 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
64 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
65 OS << "// VisualStudio defines setjmp as _setjmp\n"
66 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
67 " !defined(setjmp_undefined_for_msvc)\n"
68 "# pragma push_macro(\"setjmp\")\n"
70 "# define setjmp_undefined_for_msvc\n"
74 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
75 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
76 "// let's return it to _setjmp state\n"
77 "# pragma pop_macro(\"setjmp\")\n"
78 "# undef setjmp_undefined_for_msvc\n"
82 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
84 OS << "// Enum values for Intrinsics.h\n";
85 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";
95 void IntrinsicEmitter::
96 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
98 // Build a 'first character of function name' -> intrinsic # mapping.
99 std::map<char, std::vector<unsigned> > IntMapping;
100 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
101 IntMapping[Ints[i].Name[5]].push_back(i);
103 OS << "// Function name -> enum value recognizer code.\n";
104 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
105 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(),
110 E = IntMapping.end(); I != E; ++I) {
111 OS << " case '" << I->first << "':\n";
112 std::vector<unsigned> &IntList = I->second;
114 // Emit all the overloaded intrinsics first, build a table of the
115 // non-overloaded ones.
116 std::vector<StringMatcher::StringPair> MatchTable;
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 + ";";
123 if (!Ints[IntNo].isOverloaded) {
124 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
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 << "\")) "
135 // 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";
144 void IntrinsicEmitter::
145 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
147 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";
150 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
151 OS << " \"" << Ints[i].Name << "\",\n";
155 void IntrinsicEmitter::
156 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
158 OS << "// Intrinsic ID to overload bitset\n";
159 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
160 OS << "static const uint8_t OTable[] = {\n";
162 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
163 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
166 if (Ints[i].isOverloaded)
167 OS << " | (1<<" << (i+1)%8 << ')';
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";
176 // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
178 // Common values should be encoded with 0-15.
196 // Values from 16+ are only encodable with the inefficient encoding.
198 IIT_EMPTYSTRUCT = 17,
203 IIT_EXTEND_VEC_ARG = 22,
204 IIT_TRUNC_VEC_ARG = 23,
209 static void EncodeFixedValueType(MVT::SimpleValueType VT,
210 std::vector<unsigned char> &Sig) {
211 if (EVT(VT).isInteger()) {
212 unsigned BitWidth = EVT(VT).getSizeInBits();
214 default: throw "unhandled integer type width in intrinsic!";
215 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);
224 default: throw "unhandled MVT in intrinsic!";
225 case MVT::f32: return Sig.push_back(IIT_F32);
226 case MVT::f64: return Sig.push_back(IIT_F64);
227 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);
235 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
238 static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
239 std::vector<unsigned char> &Sig) {
241 if (R->isSubClassOf("LLVMMatchType")) {
242 unsigned Number = R->getValueAsInt("Number");
243 assert(Number < ArgCodes.size() && "Invalid matching number!");
244 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
245 Sig.push_back(IIT_EXTEND_VEC_ARG);
246 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
247 Sig.push_back(IIT_TRUNC_VEC_ARG);
249 Sig.push_back(IIT_ARG);
250 return Sig.push_back((Number << 2) | ArgCodes[Number]);
253 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
258 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
259 case MVT::vAny: ++Tmp; // FALL THROUGH.
260 case MVT::fAny: ++Tmp; // FALL THROUGH.
262 // If this is an "any" valuetype, then the type is the type of the next
263 // type in the list specified to getIntrinsic().
264 Sig.push_back(IIT_ARG);
266 // Figure out what arg # this is consuming, and remember what kind it was.
267 unsigned ArgNo = ArgCodes.size();
268 ArgCodes.push_back(Tmp);
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);
275 unsigned AddrSpace = 0;
276 if (R->isSubClassOf("LLVMQualPointerType")) {
277 AddrSpace = R->getValueAsInt("AddrSpace");
278 assert(AddrSpace < 256 && "Address space exceeds 255");
281 Sig.push_back(IIT_ANYPTR);
282 Sig.push_back(AddrSpace);
284 Sig.push_back(IIT_PTR);
286 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
290 if (EVT(VT).isVector()) {
292 switch (VVT.getVectorNumElements()) {
293 default: throw "unhandled vector type width in intrinsic!";
294 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;
298 case 32: Sig.push_back(IIT_V32); break;
301 return EncodeFixedValueType(VVT.getVectorElementType().
302 getSimpleVT().SimpleTy, Sig);
305 EncodeFixedValueType(VT, Sig);
309 #pragma optimize("",on)
312 /// ComputeFixedEncoding - If we can encode the type signature for this
313 /// intrinsic into 32 bits, return it. If not, return ~0U.
314 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
315 std::vector<unsigned char> &TypeSig) {
316 std::vector<unsigned char> ArgCodes;
318 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);
324 switch (Int.IS.RetVTs.size()) {
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");
333 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
334 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
337 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
338 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
341 void printIITEntry(raw_ostream &OS, unsigned char X) {
345 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
347 // 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;
351 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
353 std::vector<unsigned char> TypeSig;
355 // Compute the unique argument type info.
356 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
357 // Get the signature for the intrinsic.
359 ComputeFixedEncoding(Ints[i], TypeSig);
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) {
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) {
372 Result = (Result << 4) | TypeSig[e-i-1];
375 // If this could be encoded into a 31-bit word, return it.
376 if (!Failed && (Result >> 31) == 0) {
377 FixedEncodings.push_back(Result);
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);
386 // This is a placehold that we'll replace after the table is laid out.
387 FixedEncodings.push_back(~0U);
390 LongEncodingTable.layout();
392 OS << "// Global intrinsic function declaration type table.\n";
393 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
395 OS << "static const unsigned IIT_Table[] = {\n ";
397 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
401 // If the entry fit in the table, just emit it.
402 if (FixedEncodings[i] != ~0U) {
403 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
408 ComputeFixedEncoding(Ints[i], TypeSig);
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) << ", ";
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";
424 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
434 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
435 switch (intrinsic.ModRef) {
436 case CodeGenIntrinsic::NoMem:
438 case CodeGenIntrinsic::ReadArgMem:
439 case CodeGenIntrinsic::ReadMem:
441 case CodeGenIntrinsic::ReadWriteArgMem:
442 case CodeGenIntrinsic::ReadWriteMem:
445 llvm_unreachable("bad mod-ref kind");
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)
454 // Try to order by readonly/readnone attribute.
455 ModRefKind LK = getModRefKind(*L);
456 ModRefKind RK = getModRefKind(*R);
457 if (LK != RK) return (LK > RK);
459 // Order by argument attributes.
460 // This is reliable because each side is already sorted internally.
461 return (L->ArgumentAttributes < R->ArgumentAttributes);
466 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
467 void IntrinsicEmitter::
468 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
469 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
470 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
472 OS << "static AttrListPtr getAttributes(" << TargetPrefix
473 << "Intrinsic::ID id) {\n";
475 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
477 // Compute the maximum number of attribute arguments and the map
478 typedef std::map<const CodeGenIntrinsic*, unsigned,
479 AttributeComparator> UniqAttrMapTy;
480 UniqAttrMapTy UniqAttributes;
481 unsigned maxArgAttrs = 0;
482 unsigned AttrNum = 0;
483 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
484 const CodeGenIntrinsic &intrinsic = Ints[i];
486 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
487 unsigned &N = UniqAttributes[&intrinsic];
489 assert(AttrNum < 256 && "Too many unique attributes for table!");
493 // Emit an array of AttributeWithIndex. Most intrinsics will have
494 // at least one entry, for the function itself (index ~1), which is
496 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
498 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
499 const CodeGenIntrinsic &intrinsic = Ints[i];
501 OS << " " << UniqAttributes[&intrinsic] << ", // "
502 << intrinsic.Name << "\n";
506 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
507 OS << " unsigned NumAttrs = 0;\n";
508 OS << " if (id != 0) {\n";
509 OS << " switch(IntrinsicsToAttributesMap[id - ";
511 OS << "Intrinsic::num_intrinsics";
515 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
516 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
517 E = UniqAttributes.end(); I != E; ++I) {
518 OS << " case " << I->second << ":\n";
520 const CodeGenIntrinsic &intrinsic = *(I->first);
522 // Keep track of the number of attributes we're writing out.
523 unsigned numAttrs = 0;
525 // The argument attributes are alreadys sorted by argument index.
526 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
527 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
529 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
532 bool moreThanOne = false;
535 if (moreThanOne) OS << '|';
537 switch (intrinsic.ArgumentAttributes[ai].second) {
538 case CodeGenIntrinsic::NoCapture:
539 OS << "Attribute::NoCapture";
545 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
550 ModRefKind modRef = getModRefKind(intrinsic);
552 if (!intrinsic.canThrow || modRef) {
553 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
554 if (!intrinsic.canThrow) {
555 OS << "Attribute::NoUnwind";
556 if (modRef) OS << '|';
559 case MRK_none: break;
560 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
561 case MRK_readnone: OS << "Attribute::ReadNone"; break;
567 OS << " NumAttrs = " << numAttrs << ";\n";
570 OS << " return AttrListPtr();\n";
576 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
578 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
581 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
582 void IntrinsicEmitter::
583 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
584 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
585 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
586 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
587 << "\"Unknown intrinsic.\");\n\n";
589 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
590 << " /* invalid */ UnknownModRefBehavior,\n";
591 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
592 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
593 switch (Ints[i].ModRef) {
594 case CodeGenIntrinsic::NoMem:
595 OS << "DoesNotAccessMemory,\n";
597 case CodeGenIntrinsic::ReadArgMem:
598 OS << "OnlyReadsArgumentPointees,\n";
600 case CodeGenIntrinsic::ReadMem:
601 OS << "OnlyReadsMemory,\n";
603 case CodeGenIntrinsic::ReadWriteArgMem:
604 OS << "OnlyAccessesArgumentPointees,\n";
606 case CodeGenIntrinsic::ReadWriteMem:
607 OS << "UnknownModRefBehavior,\n";
612 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
613 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
616 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
617 /// same target, and we already checked it.
618 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
619 const std::string &TargetPrefix,
622 std::vector<StringMatcher::StringPair> Results;
624 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
625 E = BIM.end(); I != E; ++I) {
626 std::string ResultCode =
627 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
628 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
631 StringMatcher("BuiltinName", Results, OS).Emit();
635 void IntrinsicEmitter::
636 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
638 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
640 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
641 if (!Ints[i].GCCBuiltinName.empty()) {
642 // Get the map for this target prefix.
643 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
645 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
646 Ints[i].EnumName)).second)
647 throw "Intrinsic '" + Ints[i].TheDef->getName() +
648 "': duplicate GCC builtin name!";
652 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
653 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
654 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
655 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
656 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
659 OS << "static " << TargetPrefix << "Intrinsic::ID "
660 << "getIntrinsicForGCCBuiltin(const char "
661 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
663 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
664 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
667 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
668 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
670 // Note: this could emit significantly better code if we cared.
671 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
673 if (!I->first.empty())
674 OS << "if (TargetPrefix == \"" << I->first << "\") ";
676 OS << "/* Target Independent Builtins */ ";
679 // Emit the comparisons for this target prefix.
680 EmitTargetBuiltins(I->second, TargetPrefix, OS);
684 if (!TargetPrefix.empty())
685 OS << "(" << TargetPrefix << "Intrinsic::ID)";
686 OS << "Intrinsic::not_intrinsic;\n";