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 "llvm/TableGen/Record.h"
17 #include "llvm/TableGen/StringMatcher.h"
18 #include "llvm/ADT/StringExtras.h"
22 //===----------------------------------------------------------------------===//
23 // IntrinsicEmitter Implementation
24 //===----------------------------------------------------------------------===//
26 void IntrinsicEmitter::run(raw_ostream &OS) {
27 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
29 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
31 if (TargetOnly && !Ints.empty())
32 TargetPrefix = Ints[0].TargetPrefix;
36 // Emit the enum information.
37 EmitEnumInfo(Ints, OS);
39 // Emit the intrinsic ID -> name table.
40 EmitIntrinsicToNameTable(Ints, OS);
42 // Emit the intrinsic ID -> overload table.
43 EmitIntrinsicToOverloadTable(Ints, OS);
45 // Emit the function name recognizer.
46 EmitFnNameRecognizer(Ints, OS);
48 // Emit the intrinsic verifier.
49 EmitVerifier(Ints, OS);
51 // Emit the intrinsic declaration generator.
52 EmitGenerator(Ints, OS);
54 // Emit the intrinsic parameter attributes.
55 EmitAttributes(Ints, OS);
57 // Emit intrinsic alias analysis mod/ref behavior.
58 EmitModRefBehavior(Ints, OS);
60 // Emit code to translate GCC builtins into LLVM intrinsics.
61 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
66 void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
67 OS << "// VisualStudio defines setjmp as _setjmp\n"
68 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
69 " !defined(setjmp_undefined_for_msvc)\n"
70 "# pragma push_macro(\"setjmp\")\n"
72 "# define setjmp_undefined_for_msvc\n"
76 void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
77 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
78 "// let's return it to _setjmp state\n"
79 "# pragma pop_macro(\"setjmp\")\n"
80 "# undef setjmp_undefined_for_msvc\n"
84 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
86 OS << "// Enum values for Intrinsics.h\n";
87 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
88 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
89 OS << " " << Ints[i].EnumName;
90 OS << ((i != e-1) ? ", " : " ");
91 OS << std::string(40-Ints[i].EnumName.size(), ' ')
92 << "// " << Ints[i].Name << "\n";
97 void IntrinsicEmitter::
98 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
100 // Build a 'first character of function name' -> intrinsic # mapping.
101 std::map<char, std::vector<unsigned> > IntMapping;
102 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
103 IntMapping[Ints[i].Name[5]].push_back(i);
105 OS << "// Function name -> enum value recognizer code.\n";
106 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
107 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
108 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
109 OS << " default: break;\n";
110 // Emit the intrinsic matching stuff by first letter.
111 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
112 E = IntMapping.end(); I != E; ++I) {
113 OS << " case '" << I->first << "':\n";
114 std::vector<unsigned> &IntList = I->second;
116 // Emit all the overloaded intrinsics first, build a table of the
117 // non-overloaded ones.
118 std::vector<StringMatcher::StringPair> MatchTable;
120 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
121 unsigned IntNo = IntList[i];
122 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
123 Ints[IntNo].EnumName + ";";
125 if (!Ints[IntNo].isOverloaded) {
126 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
130 // For overloaded intrinsics, only the prefix needs to match
131 std::string TheStr = Ints[IntNo].Name.substr(6);
132 TheStr += '.'; // Require "bswap." instead of bswap.
133 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
137 // Emit the matcher logic for the fixed length strings.
138 StringMatcher("NameR", MatchTable, OS).Emit(1);
139 OS << " break; // end of '" << I->first << "' case.\n";
146 void IntrinsicEmitter::
147 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
149 OS << "// Intrinsic ID to name table\n";
150 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
151 OS << " // Note that entry #0 is the invalid intrinsic!\n";
152 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
153 OS << " \"" << Ints[i].Name << "\",\n";
157 void IntrinsicEmitter::
158 EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
160 OS << "// Intrinsic ID to overload bitset\n";
161 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
162 OS << "static const uint8_t OTable[] = {\n";
164 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
165 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
168 if (Ints[i].isOverloaded)
169 OS << " | (1<<" << (i+1)%8 << ')';
172 // OTable contains a true bit at the position if the intrinsic is overloaded.
173 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
177 /// RecordListComparator - Provide a deterministic comparator for lists of
180 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
181 struct RecordListComparator {
182 bool operator()(const RecPair &LHS,
183 const RecPair &RHS) const {
185 const std::vector<Record*> *LHSVec = &LHS.first;
186 const std::vector<Record*> *RHSVec = &RHS.first;
187 unsigned RHSSize = RHSVec->size();
188 unsigned LHSSize = LHSVec->size();
190 for (; i != LHSSize; ++i) {
191 if (i == RHSSize) return false; // RHS is shorter than LHS.
192 if ((*LHSVec)[i] != (*RHSVec)[i])
193 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
196 if (i != RHSSize) return true;
199 LHSVec = &LHS.second;
200 RHSVec = &RHS.second;
201 RHSSize = RHSVec->size();
202 LHSSize = LHSVec->size();
204 for (i = 0; i != LHSSize; ++i) {
205 if (i == RHSSize) return false; // RHS is shorter than LHS.
206 if ((*LHSVec)[i] != (*RHSVec)[i])
207 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
215 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
217 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
218 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
219 OS << " switch (ID) {\n";
220 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
222 // This checking can emit a lot of very common code. To reduce the amount of
223 // code that we emit, batch up cases that have identical types. This avoids
224 // problems where GCC can run out of memory compiling Verifier.cpp.
225 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
226 MapTy UniqueArgInfos;
228 // Compute the unique argument type info.
229 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
230 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
231 Ints[i].IS.ParamTypeDefs)].push_back(i);
233 // Loop through the array, emitting one comparison for each batch.
234 for (MapTy::iterator I = UniqueArgInfos.begin(),
235 E = UniqueArgInfos.end(); I != E; ++I) {
236 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
237 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
238 << Ints[I->second[i]].Name << "\n";
240 const RecPair &ArgTypes = I->first;
241 const std::vector<Record*> &RetTys = ArgTypes.first;
242 const std::vector<Record*> &ParamTys = ArgTypes.second;
243 std::vector<unsigned> OverloadedTypeIndices;
245 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
248 // Emit return types.
249 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
250 Record *ArgType = RetTys[j];
253 if (ArgType->isSubClassOf("LLVMMatchType")) {
254 unsigned Number = ArgType->getValueAsInt("Number");
255 assert(Number < OverloadedTypeIndices.size() &&
256 "Invalid matching number!");
257 Number = OverloadedTypeIndices[Number];
258 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
259 OS << "~(ExtendedElementVectorType | " << Number << ")";
260 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
261 OS << "~(TruncatedElementVectorType | " << Number << ")";
265 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
266 OS << getEnumName(VT);
268 if (EVT(VT).isOverloaded())
269 OverloadedTypeIndices.push_back(j);
271 if (VT == MVT::isVoid && j != 0 && j != je - 1)
272 throw "Var arg type not last argument";
276 // Emit the parameter types.
277 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
278 Record *ArgType = ParamTys[j];
281 if (ArgType->isSubClassOf("LLVMMatchType")) {
282 unsigned Number = ArgType->getValueAsInt("Number");
283 assert(Number < OverloadedTypeIndices.size() &&
284 "Invalid matching number!");
285 Number = OverloadedTypeIndices[Number];
286 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
287 OS << "~(ExtendedElementVectorType | " << Number << ")";
288 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
289 OS << "~(TruncatedElementVectorType | " << Number << ")";
293 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
294 OS << getEnumName(VT);
296 if (EVT(VT).isOverloaded())
297 OverloadedTypeIndices.push_back(j + RetTys.size());
299 if (VT == MVT::isVoid && j != 0 && j != je - 1)
300 throw "Var arg type not last argument";
311 static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
312 if (EVT(VT).isInteger()) {
313 unsigned BitWidth = EVT(VT).getSizeInBits();
314 OS << "IntegerType::get(Context, " << BitWidth << ")";
315 } else if (VT == MVT::Other) {
316 // MVT::OtherVT is used to mean the empty struct type here.
317 OS << "StructType::get(Context)";
318 } else if (VT == MVT::f16) {
319 OS << "Type::getHalfTy(Context)";
320 } else if (VT == MVT::f32) {
321 OS << "Type::getFloatTy(Context)";
322 } else if (VT == MVT::f64) {
323 OS << "Type::getDoubleTy(Context)";
324 } else if (VT == MVT::f128) {
325 OS << "Type::getFP128Ty(Context)";
326 } else if (VT == MVT::ppcf128) {
327 OS << "Type::getPPC_FP128Ty(Context)";
328 } else if (VT == MVT::isVoid) {
329 OS << "Type::getVoidTy(Context)";
330 } else if (VT == MVT::Metadata) {
331 OS << "Type::getMetadataTy(Context)";
332 } else if (VT == MVT::x86mmx) {
333 OS << "Type::getX86_MMXTy(Context)";
335 assert(false && "Unsupported ValueType!");
339 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
342 static void EmitTypeGenerate(raw_ostream &OS,
343 const std::vector<Record*> &ArgTypes,
345 if (ArgTypes.empty())
346 return EmitTypeForValueType(OS, MVT::isVoid);
348 if (ArgTypes.size() == 1)
349 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
351 OS << "StructType::get(";
353 for (std::vector<Record*>::const_iterator
354 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
355 EmitTypeGenerate(OS, *I, ArgNo);
362 static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
364 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
366 if (ArgType->isSubClassOf("LLVMMatchType")) {
367 unsigned Number = ArgType->getValueAsInt("Number");
368 assert(Number < ArgNo && "Invalid matching number!");
369 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
370 OS << "VectorType::getExtendedElementVectorType"
371 << "(cast<VectorType>(Tys[" << Number << "]))";
372 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
373 OS << "VectorType::getTruncatedElementVectorType"
374 << "(cast<VectorType>(Tys[" << Number << "]))";
376 OS << "Tys[" << Number << "]";
377 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny ||
378 VT == MVT::iPTRAny) {
379 // NOTE: The ArgNo variable here is not the absolute argument number, it is
380 // the index of the "arbitrary" type in the Tys array passed to the
381 // Intrinsic::getDeclaration function. Consequently, we only want to
382 // increment it when we actually hit an overloaded type. Getting this wrong
383 // leads to very subtle bugs!
384 OS << "Tys[" << ArgNo++ << "]";
385 } else if (EVT(VT).isVector()) {
387 OS << "VectorType::get(";
388 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
389 OS << ", " << VVT.getVectorNumElements() << ")";
390 } else if (VT == MVT::iPTR) {
391 OS << "PointerType::getUnqual(";
392 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
394 } else if (VT == MVT::isVoid) {
396 OS << "Type::getVoidTy(Context)";
398 EmitTypeForValueType(OS, VT);
403 // NOTE: This must be kept in synch with the version emitted to the .gen file!
405 // Common values should be encoded with 0-15.
423 // Values from 16+ are only encodable with the inefficient encoding.
425 IIT_EMPTYSTRUCT = 17,
430 IIT_EXTEND_VEC_ARG = 22,
431 IIT_TRUNC_VEC_ARG = 23
435 static void EncodeFixedValueType(MVT::SimpleValueType VT,
436 SmallVectorImpl<unsigned> &Sig) {
437 if (EVT(VT).isInteger()) {
438 unsigned BitWidth = EVT(VT).getSizeInBits();
440 default: throw "unhandled integer type width in intrinsic!";
441 case 1: return Sig.push_back(IIT_I1);
442 case 8: return Sig.push_back(IIT_I8);
443 case 16: return Sig.push_back(IIT_I16);
444 case 32: return Sig.push_back(IIT_I32);
445 case 64: return Sig.push_back(IIT_I64);
450 default: throw "unhandled MVT in intrinsic!";
451 case MVT::f32: return Sig.push_back(IIT_F32);
452 case MVT::f64: return Sig.push_back(IIT_F64);
453 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
454 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
455 // MVT::OtherVT is used to mean the empty struct type here.
456 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
461 #pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
464 static void EncodeFixedType(Record *R, unsigned &NextArgNo,
465 SmallVectorImpl<unsigned> &Sig) {
467 if (R->isSubClassOf("LLVMMatchType")) {
468 unsigned Number = R->getValueAsInt("Number");
469 assert(Number < NextArgNo && "Invalid matching number!");
470 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
471 Sig.push_back(IIT_EXTEND_VEC_ARG);
472 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
473 Sig.push_back(IIT_TRUNC_VEC_ARG);
475 Sig.push_back(IIT_ARG);
476 return Sig.push_back(Number);
479 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
481 // If this is an "any" valuetype, then the type is the type of the next
482 // type in the list specified to getIntrinsic().
483 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny ||
484 VT == MVT::iPTRAny) {
485 Sig.push_back(IIT_ARG);
486 return Sig.push_back(NextArgNo++);
489 if (EVT(VT).isVector()) {
491 switch (VVT.getVectorNumElements()) {
492 default: throw "unhandled vector type width in intrinsic!";
493 case 2: Sig.push_back(IIT_V2); break;
494 case 4: Sig.push_back(IIT_V4); break;
495 case 8: Sig.push_back(IIT_V8); break;
496 case 16: Sig.push_back(IIT_V16); break;
497 case 32: Sig.push_back(IIT_V32); break;
500 return EncodeFixedValueType(VVT.getVectorElementType().
501 getSimpleVT().SimpleTy, Sig);
504 if (VT == MVT::iPTR) {
505 Sig.push_back(IIT_PTR);
506 return EncodeFixedType(R->getValueAsDef("ElTy"), NextArgNo, Sig);
509 EncodeFixedValueType(VT, Sig);
513 #pragma optimize("",on)
516 /// ComputeFixedEncoding - If we can encode the type signature for this
517 /// intrinsic into 32 bits, return it. If not, return ~0U.
518 static unsigned ComputeFixedEncoding(const CodeGenIntrinsic &Int) {
519 unsigned NextArgNo = 0;
521 SmallVector<unsigned, 8> TypeSig;
522 if (Int.IS.RetVTs.empty())
523 TypeSig.push_back(IIT_Done);
524 else if (Int.IS.RetVTs.size() == 1 &&
525 Int.IS.RetVTs[0] == MVT::isVoid)
526 TypeSig.push_back(IIT_Done);
528 switch (Int.IS.RetVTs.size()) {
530 case 2: TypeSig.push_back(IIT_STRUCT2); break;
531 case 3: TypeSig.push_back(IIT_STRUCT3); break;
532 case 4: TypeSig.push_back(IIT_STRUCT4); break;
533 case 5: TypeSig.push_back(IIT_STRUCT5); break;
534 default: assert(0 && "Unhandled case in struct");
537 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
538 EncodeFixedType(Int.IS.RetTypeDefs[i], NextArgNo, TypeSig);
541 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
542 EncodeFixedType(Int.IS.ParamTypeDefs[i], NextArgNo, TypeSig);
544 // Can only encode 8 nibbles into a 32-bit word.
545 if (TypeSig.size() > 8) return ~0U;
548 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
549 // If we had an unencodable argument, bail out.
552 Result = (Result << 4) | TypeSig[e-i-1];
558 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
560 OS << "// Global intrinsic function declaration type table.\n";
561 OS << "#ifdef GET_INTRINSTIC_GENERATOR_GLOBAL\n";
562 // NOTE: These enums must be kept in sync with the ones above!
563 OS << "enum IIT_Info {\n";
564 OS << " IIT_Done = 0,\n";
565 OS << " IIT_I1 = 1,\n";
566 OS << " IIT_I8 = 2,\n";
567 OS << " IIT_I16 = 3,\n";
568 OS << " IIT_I32 = 4,\n";
569 OS << " IIT_I64 = 5,\n";
570 OS << " IIT_F32 = 6,\n";
571 OS << " IIT_F64 = 7,\n";
572 OS << " IIT_V2 = 8,\n";
573 OS << " IIT_V4 = 9,\n";
574 OS << " IIT_V8 = 10,\n";
575 OS << " IIT_V16 = 11,\n";
576 OS << " IIT_V32 = 12,\n";
577 OS << " IIT_MMX = 13,\n";
578 OS << " IIT_PTR = 14,\n";
579 OS << " IIT_ARG = 15,\n";
580 OS << " IIT_METADATA = 16,\n";
581 OS << " IIT_EMPTYSTRUCT = 17,\n";
582 OS << " IIT_STRUCT2 = 18,\n";
583 OS << " IIT_STRUCT3 = 19,\n";
584 OS << " IIT_STRUCT4 = 20,\n";
585 OS << " IIT_STRUCT5 = 21,\n";
586 OS << " IIT_EXTEND_VEC_ARG = 22,\n";
587 OS << " IIT_TRUNC_VEC_ARG = 23\n";
591 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
593 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
594 MapTy UniqueArgInfos;
596 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
597 // capture it in this vector, otherwise store a ~0U.
598 std::vector<unsigned> FixedEncodings;
600 // Compute the unique argument type info.
601 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
602 FixedEncodings.push_back(ComputeFixedEncoding(Ints[i]));
604 // If we didn't compute a compact encoding, emit a long-form variant.
605 if (FixedEncodings.back() == ~0U)
606 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
607 Ints[i].IS.ParamTypeDefs)].push_back(i);
610 OS << "static const unsigned IIT_Table[] = {\n ";
612 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
615 if (FixedEncodings[i] == ~0U)
618 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
621 OS << "0\n};\n\n#endif\n\n"; // End of GET_INTRINSTIC_GENERATOR_GLOBAL
623 OS << "// Code for generating Intrinsic function declarations.\n";
624 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
625 OS << " switch (id) {\n";
626 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
628 // Loop through the array, emitting one generator for each batch.
629 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
631 for (MapTy::iterator I = UniqueArgInfos.begin(),
632 E = UniqueArgInfos.end(); I != E; ++I) {
633 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
634 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
635 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
637 const RecPair &ArgTypes = I->first;
638 const std::vector<Record*> &RetTys = ArgTypes.first;
639 const std::vector<Record*> &ParamTys = ArgTypes.second;
641 unsigned N = ParamTys.size();
643 OS << " ResultTy = ";
644 EmitTypeGenerate(OS, RetTys, ArgNo);
647 for (unsigned j = 0; j != N; ++j) {
648 OS << " ArgTys.push_back(";
649 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
667 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
668 switch (intrinsic.ModRef) {
669 case CodeGenIntrinsic::NoMem:
671 case CodeGenIntrinsic::ReadArgMem:
672 case CodeGenIntrinsic::ReadMem:
674 case CodeGenIntrinsic::ReadWriteArgMem:
675 case CodeGenIntrinsic::ReadWriteMem:
678 llvm_unreachable("bad mod-ref kind");
681 struct AttributeComparator {
682 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
683 // Sort throwing intrinsics after non-throwing intrinsics.
684 if (L->canThrow != R->canThrow)
687 // Try to order by readonly/readnone attribute.
688 ModRefKind LK = getModRefKind(*L);
689 ModRefKind RK = getModRefKind(*R);
690 if (LK != RK) return (LK > RK);
692 // Order by argument attributes.
693 // This is reliable because each side is already sorted internally.
694 return (L->ArgumentAttributes < R->ArgumentAttributes);
699 /// EmitAttributes - This emits the Intrinsic::getAttributes method.
700 void IntrinsicEmitter::
701 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
702 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
703 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
705 OS << "static AttrListPtr getAttributes(" << TargetPrefix
706 << "Intrinsic::ID id) {\n";
708 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
710 // Compute the maximum number of attribute arguments and the map
711 typedef std::map<const CodeGenIntrinsic*, unsigned,
712 AttributeComparator> UniqAttrMapTy;
713 UniqAttrMapTy UniqAttributes;
714 unsigned maxArgAttrs = 0;
715 unsigned AttrNum = 0;
716 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
717 const CodeGenIntrinsic &intrinsic = Ints[i];
719 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
720 unsigned &N = UniqAttributes[&intrinsic];
722 assert(AttrNum < 256 && "Too many unique attributes for table!");
726 // Emit an array of AttributeWithIndex. Most intrinsics will have
727 // at least one entry, for the function itself (index ~1), which is
729 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
731 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
732 const CodeGenIntrinsic &intrinsic = Ints[i];
734 OS << " " << UniqAttributes[&intrinsic] << ", // "
735 << intrinsic.Name << "\n";
739 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
740 OS << " unsigned NumAttrs = 0;\n";
741 OS << " if (id != 0) {\n";
742 OS << " switch(IntrinsicsToAttributesMap[id - ";
744 OS << "Intrinsic::num_intrinsics";
748 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
749 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
750 E = UniqAttributes.end(); I != E; ++I) {
751 OS << " case " << I->second << ":\n";
753 const CodeGenIntrinsic &intrinsic = *(I->first);
755 // Keep track of the number of attributes we're writing out.
756 unsigned numAttrs = 0;
758 // The argument attributes are alreadys sorted by argument index.
759 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
760 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
762 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
765 bool moreThanOne = false;
768 if (moreThanOne) OS << '|';
770 switch (intrinsic.ArgumentAttributes[ai].second) {
771 case CodeGenIntrinsic::NoCapture:
772 OS << "Attribute::NoCapture";
778 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
783 ModRefKind modRef = getModRefKind(intrinsic);
785 if (!intrinsic.canThrow || modRef) {
786 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
787 if (!intrinsic.canThrow) {
788 OS << "Attribute::NoUnwind";
789 if (modRef) OS << '|';
792 case MRK_none: break;
793 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
794 case MRK_readnone: OS << "Attribute::ReadNone"; break;
800 OS << " NumAttrs = " << numAttrs << ";\n";
803 OS << " return AttrListPtr();\n";
809 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
811 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
814 /// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
815 void IntrinsicEmitter::
816 EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
817 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
818 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
819 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
820 << "\"Unknown intrinsic.\");\n\n";
822 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
823 << " /* invalid */ UnknownModRefBehavior,\n";
824 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
825 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
826 switch (Ints[i].ModRef) {
827 case CodeGenIntrinsic::NoMem:
828 OS << "DoesNotAccessMemory,\n";
830 case CodeGenIntrinsic::ReadArgMem:
831 OS << "OnlyReadsArgumentPointees,\n";
833 case CodeGenIntrinsic::ReadMem:
834 OS << "OnlyReadsMemory,\n";
836 case CodeGenIntrinsic::ReadWriteArgMem:
837 OS << "OnlyAccessesArgumentPointees,\n";
839 case CodeGenIntrinsic::ReadWriteMem:
840 OS << "UnknownModRefBehavior,\n";
845 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
846 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
849 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
850 /// same target, and we already checked it.
851 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
852 const std::string &TargetPrefix,
855 std::vector<StringMatcher::StringPair> Results;
857 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
858 E = BIM.end(); I != E; ++I) {
859 std::string ResultCode =
860 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
861 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
864 StringMatcher("BuiltinName", Results, OS).Emit();
868 void IntrinsicEmitter::
869 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
871 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
873 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
874 if (!Ints[i].GCCBuiltinName.empty()) {
875 // Get the map for this target prefix.
876 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
878 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
879 Ints[i].EnumName)).second)
880 throw "Intrinsic '" + Ints[i].TheDef->getName() +
881 "': duplicate GCC builtin name!";
885 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
886 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
887 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
888 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
889 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
892 OS << "static " << TargetPrefix << "Intrinsic::ID "
893 << "getIntrinsicForGCCBuiltin(const char "
894 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
896 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
897 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
900 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
901 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
903 // Note: this could emit significantly better code if we cared.
904 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
906 if (!I->first.empty())
907 OS << "if (TargetPrefix == \"" << I->first << "\") ";
909 OS << "/* Target Independent Builtins */ ";
912 // Emit the comparisons for this target prefix.
913 EmitTargetBuiltins(I->second, TargetPrefix, OS);
917 if (!TargetPrefix.empty())
918 OS << "(" << TargetPrefix << "Intrinsic::ID)";
919 OS << "Intrinsic::not_intrinsic;\n";