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 verifier.
50 EmitVerifier(Ints, OS);
52 // Emit the intrinsic declaration generator.
53 EmitGenerator(Ints, OS);
55 // Emit the intrinsic parameter attributes.
56 EmitAttributes(Ints, OS);
58 // Emit intrinsic alias analysis mod/ref behavior.
59 EmitModRefBehavior(Ints, OS);
61 // Emit code to translate GCC builtins into LLVM intrinsics.
62 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
67 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
68 OS << "// VisualStudio defines setjmp as _setjmp\n"
69 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
70 " !defined(setjmp_undefined_for_msvc)\n"
71 "# pragma push_macro(\"setjmp\")\n"
73 "# define setjmp_undefined_for_msvc\n"
77 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
78 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
79 "// let's return it to _setjmp state\n"
80 "# pragma pop_macro(\"setjmp\")\n"
81 "# undef setjmp_undefined_for_msvc\n"
85 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
87 OS << "// Enum values for Intrinsics.h\n";
88 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
89 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
90 OS << " " << Ints[i].EnumName;
91 OS << ((i != e-1) ? ", " : " ");
92 OS << std::string(40-Ints[i].EnumName.size(), ' ')
93 << "// " << Ints[i].Name << "\n";
98 void IntrinsicEmitter::
99 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
101 // Build a 'first character of function name' -> intrinsic # mapping.
102 std::map<char, std::vector<unsigned> > IntMapping;
103 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
104 IntMapping[Ints[i].Name[5]].push_back(i);
106 OS << "// Function name -> enum value recognizer code.\n";
107 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
108 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
109 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
110 OS << " default: break;\n";
111 // Emit the intrinsic matching stuff by first letter.
112 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
113 E = IntMapping.end(); I != E; ++I) {
114 OS << " case '" << I->first << "':\n";
115 std::vector<unsigned> &IntList = I->second;
117 // Emit all the overloaded intrinsics first, build a table of the
118 // non-overloaded ones.
119 std::vector<StringMatcher::StringPair> MatchTable;
121 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
122 unsigned IntNo = IntList[i];
123 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
124 Ints[IntNo].EnumName + ";";
126 if (!Ints[IntNo].isOverloaded) {
127 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
131 // For overloaded intrinsics, only the prefix needs to match
132 std::string TheStr = Ints[IntNo].Name.substr(6);
133 TheStr += '.'; // Require "bswap." instead of bswap.
134 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
138 // Emit the matcher logic for the fixed length strings.
139 StringMatcher("NameR", MatchTable, OS).Emit(1);
140 OS << " break; // end of '" << I->first << "' case.\n";
147 void IntrinsicEmitter::
148 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
150 OS << "// Intrinsic ID to name table\n";
151 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
152 OS << " // Note that entry #0 is the invalid intrinsic!\n";
153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
154 OS << " \"" << Ints[i].Name << "\",\n";
158 void IntrinsicEmitter::
159 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
161 OS << "// Intrinsic ID to overload bitset\n";
162 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
163 OS << "static const uint8_t OTable[] = {\n";
165 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
166 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
169 if (Ints[i].isOverloaded)
170 OS << " | (1<<" << (i+1)%8 << ')';
173 // OTable contains a true bit at the position if the intrinsic is overloaded.
174 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
178 /// RecordListComparator - Provide a deterministic comparator for lists of
181 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
182 struct RecordListComparator {
183 bool operator()(const RecPair &LHS,
184 const RecPair &RHS) const {
186 const std::vector<Record*> *LHSVec = &LHS.first;
187 const std::vector<Record*> *RHSVec = &RHS.first;
188 unsigned RHSSize = RHSVec->size();
189 unsigned LHSSize = LHSVec->size();
191 for (; i != LHSSize; ++i) {
192 if (i == RHSSize) return false; // RHS is shorter than LHS.
193 if ((*LHSVec)[i] != (*RHSVec)[i])
194 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
197 if (i != RHSSize) return true;
200 LHSVec = &LHS.second;
201 RHSVec = &RHS.second;
202 RHSSize = RHSVec->size();
203 LHSSize = LHSVec->size();
205 for (i = 0; i != LHSSize; ++i) {
206 if (i == RHSSize) return false; // RHS is shorter than LHS.
207 if ((*LHSVec)[i] != (*RHSVec)[i])
208 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
216 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
218 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
219 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
220 OS << " switch (ID) {\n";
221 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
223 // This checking can emit a lot of very common code. To reduce the amount of
224 // code that we emit, batch up cases that have identical types. This avoids
225 // problems where GCC can run out of memory compiling Verifier.cpp.
226 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
227 MapTy UniqueArgInfos;
229 // Compute the unique argument type info.
230 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
231 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
232 Ints[i].IS.ParamTypeDefs)].push_back(i);
234 // Loop through the array, emitting one comparison for each batch.
235 for (MapTy::iterator I = UniqueArgInfos.begin(),
236 E = UniqueArgInfos.end(); I != E; ++I) {
237 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
238 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
239 << Ints[I->second[i]].Name << "\n";
241 const RecPair &ArgTypes = I->first;
242 const std::vector<Record*> &RetTys = ArgTypes.first;
243 const std::vector<Record*> &ParamTys = ArgTypes.second;
244 std::vector<unsigned> OverloadedTypeIndices;
246 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
249 // Emit return types.
250 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
251 Record *ArgType = RetTys[j];
254 if (ArgType->isSubClassOf("LLVMMatchType")) {
255 unsigned Number = ArgType->getValueAsInt("Number");
256 assert(Number < OverloadedTypeIndices.size() &&
257 "Invalid matching number!");
258 Number = OverloadedTypeIndices[Number];
259 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
260 OS << "~(ExtendedElementVectorType | " << Number << ")";
261 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
262 OS << "~(TruncatedElementVectorType | " << Number << ")";
266 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
267 OS << getEnumName(VT);
269 if (EVT(VT).isOverloaded())
270 OverloadedTypeIndices.push_back(j);
272 if (VT == MVT::isVoid && j != 0 && j != je - 1)
273 throw "Var arg type not last argument";
277 // Emit the parameter types.
278 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
279 Record *ArgType = ParamTys[j];
282 if (ArgType->isSubClassOf("LLVMMatchType")) {
283 unsigned Number = ArgType->getValueAsInt("Number");
284 assert(Number < OverloadedTypeIndices.size() &&
285 "Invalid matching number!");
286 Number = OverloadedTypeIndices[Number];
287 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
288 OS << "~(ExtendedElementVectorType | " << Number << ")";
289 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
290 OS << "~(TruncatedElementVectorType | " << Number << ")";
294 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
295 OS << getEnumName(VT);
297 if (EVT(VT).isOverloaded())
298 OverloadedTypeIndices.push_back(j + RetTys.size());
300 if (VT == MVT::isVoid && j != 0 && j != je - 1)
301 throw "Var arg type not last argument";
313 // NOTE: This must be kept in synch with the version emitted to the .gen file!
315 // Common values should be encoded with 0-15.
333 // Values from 16+ are only encodable with the inefficient encoding.
335 IIT_EMPTYSTRUCT = 17,
340 IIT_EXTEND_VEC_ARG = 22,
341 IIT_TRUNC_VEC_ARG = 23,
346 static void EncodeFixedValueType(MVT::SimpleValueType VT,
347 std::vector<unsigned char> &Sig) {
348 if (EVT(VT).isInteger()) {
349 unsigned BitWidth = EVT(VT).getSizeInBits();
351 default: throw "unhandled integer type width in intrinsic!";
352 case 1: return Sig.push_back(IIT_I1);
353 case 8: return Sig.push_back(IIT_I8);
354 case 16: return Sig.push_back(IIT_I16);
355 case 32: return Sig.push_back(IIT_I32);
356 case 64: return Sig.push_back(IIT_I64);
361 default: throw "unhandled MVT in intrinsic!";
362 case MVT::f32: return Sig.push_back(IIT_F32);
363 case MVT::f64: return Sig.push_back(IIT_F64);
364 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
365 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
366 // MVT::OtherVT is used to mean the empty struct type here.
367 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
372 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
375 static void EncodeFixedType(Record *R, unsigned &NextArgNo,
376 std::vector<unsigned char> &Sig) {
378 if (R->isSubClassOf("LLVMMatchType")) {
379 unsigned Number = R->getValueAsInt("Number");
380 assert(Number < NextArgNo && "Invalid matching number!");
381 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
382 Sig.push_back(IIT_EXTEND_VEC_ARG);
383 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
384 Sig.push_back(IIT_TRUNC_VEC_ARG);
386 Sig.push_back(IIT_ARG);
387 return Sig.push_back(Number);
390 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
398 // If this is an "any" valuetype, then the type is the type of the next
399 // type in the list specified to getIntrinsic().
400 Sig.push_back(IIT_ARG);
401 return Sig.push_back(NextArgNo++);
404 unsigned AddrSpace = 0;
405 if (R->isSubClassOf("LLVMQualPointerType")) {
406 AddrSpace = R->getValueAsInt("AddrSpace");
407 assert(AddrSpace < 256 && "Address space exceeds 255");
410 Sig.push_back(IIT_ANYPTR);
411 Sig.push_back(AddrSpace);
413 Sig.push_back(IIT_PTR);
415 return EncodeFixedType(R->getValueAsDef("ElTy"), NextArgNo, Sig);
419 if (EVT(VT).isVector()) {
421 switch (VVT.getVectorNumElements()) {
422 default: throw "unhandled vector type width in intrinsic!";
423 case 2: Sig.push_back(IIT_V2); break;
424 case 4: Sig.push_back(IIT_V4); break;
425 case 8: Sig.push_back(IIT_V8); break;
426 case 16: Sig.push_back(IIT_V16); break;
427 case 32: Sig.push_back(IIT_V32); break;
430 return EncodeFixedValueType(VVT.getVectorElementType().
431 getSimpleVT().SimpleTy, Sig);
434 EncodeFixedValueType(VT, Sig);
438 #pragma optimize("",on)
441 /// ComputeFixedEncoding - If we can encode the type signature for this
442 /// intrinsic into 32 bits, return it. If not, return ~0U.
443 static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
444 std::vector<unsigned char> &TypeSig) {
445 unsigned NextArgNo = 0;
447 if (Int.IS.RetVTs.empty())
448 TypeSig.push_back(IIT_Done);
449 else if (Int.IS.RetVTs.size() == 1 &&
450 Int.IS.RetVTs[0] == MVT::isVoid)
451 TypeSig.push_back(IIT_Done);
453 switch (Int.IS.RetVTs.size()) {
455 case 2: TypeSig.push_back(IIT_STRUCT2); break;
456 case 3: TypeSig.push_back(IIT_STRUCT3); break;
457 case 4: TypeSig.push_back(IIT_STRUCT4); break;
458 case 5: TypeSig.push_back(IIT_STRUCT5); break;
459 default: assert(0 && "Unhandled case in struct");
462 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
463 EncodeFixedType(Int.IS.RetTypeDefs[i], NextArgNo, TypeSig);
466 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
467 EncodeFixedType(Int.IS.ParamTypeDefs[i], NextArgNo, TypeSig);
470 void printIITEntry(raw_ostream &OS, unsigned char X) {
474 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
476 OS << "// Global intrinsic function declaration type table.\n";
477 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
478 // NOTE: These enums must be kept in sync with the ones above!
479 OS << "enum IIT_Info {\n";
480 OS << " IIT_Done = 0,\n";
481 OS << " IIT_I1 = 1,\n";
482 OS << " IIT_I8 = 2,\n";
483 OS << " IIT_I16 = 3,\n";
484 OS << " IIT_I32 = 4,\n";
485 OS << " IIT_I64 = 5,\n";
486 OS << " IIT_F32 = 6,\n";
487 OS << " IIT_F64 = 7,\n";
488 OS << " IIT_V2 = 8,\n";
489 OS << " IIT_V4 = 9,\n";
490 OS << " IIT_V8 = 10,\n";
491 OS << " IIT_V16 = 11,\n";
492 OS << " IIT_V32 = 12,\n";
493 OS << " IIT_MMX = 13,\n";
494 OS << " IIT_PTR = 14,\n";
495 OS << " IIT_ARG = 15,\n";
496 OS << " IIT_METADATA = 16,\n";
497 OS << " IIT_EMPTYSTRUCT = 17,\n";
498 OS << " IIT_STRUCT2 = 18,\n";
499 OS << " IIT_STRUCT3 = 19,\n";
500 OS << " IIT_STRUCT4 = 20,\n";
501 OS << " IIT_STRUCT5 = 21,\n";
502 OS << " IIT_EXTEND_VEC_ARG = 22,\n";
503 OS << " IIT_TRUNC_VEC_ARG = 23,\n";
504 OS << " IIT_ANYPTR = 24\n";
508 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
509 // capture it in this vector, otherwise store a ~0U.
510 std::vector<unsigned> FixedEncodings;
512 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
514 std::vector<unsigned char> TypeSig;
516 // Compute the unique argument type info.
517 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
518 // Get the signature for the intrinsic.
520 ComputeFixedEncoding(Ints[i], TypeSig);
522 // Check to see if we can encode it into a 32-bit word. We can only encode
523 // 8 nibbles into a 32-bit word.
524 if (TypeSig.size() <= 8) {
527 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
528 // If we had an unencodable argument, bail out.
529 if (TypeSig[i] > 15) {
533 Result = (Result << 4) | TypeSig[e-i-1];
536 // If this could be encoded into a 31-bit word, return it.
537 if (!Failed && (Result >> 31) == 0) {
538 FixedEncodings.push_back(Result);
543 // Otherwise, we're going to unique the sequence into the
544 // LongEncodingTable, and use its offset in the 32-bit table instead.
545 LongEncodingTable.add(TypeSig);
547 // This is a placehold that we'll replace after the table is laid out.
548 FixedEncodings.push_back(~0U);
551 LongEncodingTable.layout();
553 OS << "static const unsigned IIT_Table[] = {\n ";
555 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
559 // If the entry fit in the table, just emit it.
560 if (FixedEncodings[i] != ~0U) {
561 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
566 ComputeFixedEncoding(Ints[i], TypeSig);
569 // Otherwise, emit the offset into the long encoding table. We emit it this
570 // way so that it is easier to read the offset in the .def file.
571 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
576 // Emit the shared table of register lists.
577 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
578 if (!LongEncodingTable.empty())
579 LongEncodingTable.emit(OS, printIITEntry);
580 OS << " 255\n};\n\n";
582 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
592 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
593 switch (intrinsic.ModRef) {
594 case CodeGenIntrinsic::NoMem:
596 case CodeGenIntrinsic::ReadArgMem:
597 case CodeGenIntrinsic::ReadMem:
599 case CodeGenIntrinsic::ReadWriteArgMem:
600 case CodeGenIntrinsic::ReadWriteMem:
603 llvm_unreachable("bad mod-ref kind");
606 struct AttributeComparator {
607 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
608 // Sort throwing intrinsics after non-throwing intrinsics.
609 if (L->canThrow != R->canThrow)
612 // Try to order by readonly/readnone attribute.
613 ModRefKind LK = getModRefKind(*L);
614 ModRefKind RK = getModRefKind(*R);
615 if (LK != RK) return (LK > RK);
617 // Order by argument attributes.
618 // This is reliable because each side is already sorted internally.
619 return (L->ArgumentAttributes < R->ArgumentAttributes);
624 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
625 void IntrinsicEmitter::
626 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
627 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
628 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
630 OS << "static AttrListPtr getAttributes(" << TargetPrefix
631 << "Intrinsic::ID id) {\n";
633 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
635 // Compute the maximum number of attribute arguments and the map
636 typedef std::map<const CodeGenIntrinsic*, unsigned,
637 AttributeComparator> UniqAttrMapTy;
638 UniqAttrMapTy UniqAttributes;
639 unsigned maxArgAttrs = 0;
640 unsigned AttrNum = 0;
641 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
642 const CodeGenIntrinsic &intrinsic = Ints[i];
644 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
645 unsigned &N = UniqAttributes[&intrinsic];
647 assert(AttrNum < 256 && "Too many unique attributes for table!");
651 // Emit an array of AttributeWithIndex. Most intrinsics will have
652 // at least one entry, for the function itself (index ~1), which is
654 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
656 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
657 const CodeGenIntrinsic &intrinsic = Ints[i];
659 OS << " " << UniqAttributes[&intrinsic] << ", // "
660 << intrinsic.Name << "\n";
664 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
665 OS << " unsigned NumAttrs = 0;\n";
666 OS << " if (id != 0) {\n";
667 OS << " switch(IntrinsicsToAttributesMap[id - ";
669 OS << "Intrinsic::num_intrinsics";
673 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
674 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
675 E = UniqAttributes.end(); I != E; ++I) {
676 OS << " case " << I->second << ":\n";
678 const CodeGenIntrinsic &intrinsic = *(I->first);
680 // Keep track of the number of attributes we're writing out.
681 unsigned numAttrs = 0;
683 // The argument attributes are alreadys sorted by argument index.
684 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
685 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
687 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
690 bool moreThanOne = false;
693 if (moreThanOne) OS << '|';
695 switch (intrinsic.ArgumentAttributes[ai].second) {
696 case CodeGenIntrinsic::NoCapture:
697 OS << "Attribute::NoCapture";
703 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
708 ModRefKind modRef = getModRefKind(intrinsic);
710 if (!intrinsic.canThrow || modRef) {
711 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
712 if (!intrinsic.canThrow) {
713 OS << "Attribute::NoUnwind";
714 if (modRef) OS << '|';
717 case MRK_none: break;
718 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
719 case MRK_readnone: OS << "Attribute::ReadNone"; break;
725 OS << " NumAttrs = " << numAttrs << ";\n";
728 OS << " return AttrListPtr();\n";
734 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
736 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
739 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
740 void IntrinsicEmitter::
741 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
742 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
743 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
744 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
745 << "\"Unknown intrinsic.\");\n\n";
747 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
748 << " /* invalid */ UnknownModRefBehavior,\n";
749 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
750 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
751 switch (Ints[i].ModRef) {
752 case CodeGenIntrinsic::NoMem:
753 OS << "DoesNotAccessMemory,\n";
755 case CodeGenIntrinsic::ReadArgMem:
756 OS << "OnlyReadsArgumentPointees,\n";
758 case CodeGenIntrinsic::ReadMem:
759 OS << "OnlyReadsMemory,\n";
761 case CodeGenIntrinsic::ReadWriteArgMem:
762 OS << "OnlyAccessesArgumentPointees,\n";
764 case CodeGenIntrinsic::ReadWriteMem:
765 OS << "UnknownModRefBehavior,\n";
770 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
771 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
774 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
775 /// same target, and we already checked it.
776 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
777 const std::string &TargetPrefix,
780 std::vector<StringMatcher::StringPair> Results;
782 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
783 E = BIM.end(); I != E; ++I) {
784 std::string ResultCode =
785 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
786 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
789 StringMatcher("BuiltinName", Results, OS).Emit();
793 void IntrinsicEmitter::
794 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
796 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
798 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
799 if (!Ints[i].GCCBuiltinName.empty()) {
800 // Get the map for this target prefix.
801 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
803 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
804 Ints[i].EnumName)).second)
805 throw "Intrinsic '" + Ints[i].TheDef->getName() +
806 "': duplicate GCC builtin name!";
810 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
811 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
812 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
813 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
814 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
817 OS << "static " << TargetPrefix << "Intrinsic::ID "
818 << "getIntrinsicForGCCBuiltin(const char "
819 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
821 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
822 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
825 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
826 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
828 // Note: this could emit significantly better code if we cared.
829 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
831 if (!I->first.empty())
832 OS << "if (TargetPrefix == \"" << I->first << "\") ";
834 OS << "/* Target Independent Builtins */ ";
837 // Emit the comparisons for this target prefix.
838 EmitTargetBuiltins(I->second, TargetPrefix, OS);
842 if (!TargetPrefix.empty())
843 OS << "(" << TargetPrefix << "Intrinsic::ID)";
844 OS << "Intrinsic::not_intrinsic;\n";