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"
17 #include "llvm/ADT/StringExtras.h"
21 //===----------------------------------------------------------------------===//
22 // IntrinsicEmitter Implementation
23 //===----------------------------------------------------------------------===//
25 void IntrinsicEmitter::run(std::ostream &OS) {
26 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
28 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
30 // Emit the enum information.
31 EmitEnumInfo(Ints, OS);
33 // Emit the intrinsic ID -> name table.
34 EmitIntrinsicToNameTable(Ints, OS);
36 // Emit the function name recognizer.
37 EmitFnNameRecognizer(Ints, OS);
39 // Emit the intrinsic verifier.
40 EmitVerifier(Ints, OS);
42 // Emit the intrinsic declaration generator.
43 EmitGenerator(Ints, OS);
45 // Emit the intrinsic parameter attributes.
46 EmitAttributes(Ints, OS);
48 // Emit a list of intrinsics with corresponding GCC builtins.
49 EmitGCCBuiltinList(Ints, OS);
51 // Emit code to translate GCC builtins into LLVM intrinsics.
52 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
55 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
57 OS << "// Enum values for Intrinsics.h\n";
58 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
59 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
60 OS << " " << Ints[i].EnumName;
61 OS << ((i != e-1) ? ", " : " ");
62 OS << std::string(40-Ints[i].EnumName.size(), ' ')
63 << "// " << Ints[i].Name << "\n";
68 void IntrinsicEmitter::
69 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
71 // Build a function name -> intrinsic name mapping.
72 std::map<std::string, unsigned> IntMapping;
73 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
74 IntMapping[Ints[i].Name] = i;
76 OS << "// Function name -> enum value recognizer code.\n";
77 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
78 OS << " switch (Name[5]) {\n";
80 // Emit the intrinsics in sorted order.
82 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
83 E = IntMapping.end(); I != E; ++I) {
84 if (I->first[5] != LastChar) {
85 LastChar = I->first[5];
87 OS << " case '" << LastChar << "':\n";
90 // For overloaded intrinsics, only the prefix needs to match
91 if (Ints[I->second].isOverloaded)
92 OS << " if (Len > " << I->first.size()
93 << " && !memcmp(Name, \"" << I->first << ".\", "
94 << (I->first.size() + 1) << ")) return Intrinsic::"
95 << Ints[I->second].EnumName << ";\n";
97 OS << " if (Len == " << I->first.size()
98 << " && !memcmp(Name, \"" << I->first << "\", "
99 << I->first.size() << ")) return Intrinsic::"
100 << Ints[I->second].EnumName << ";\n";
106 void IntrinsicEmitter::
107 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
109 OS << "// Intrinsic ID to name table\n";
110 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
111 OS << " // Note that entry #0 is the invalid intrinsic!\n";
112 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
113 OS << " \"" << Ints[i].Name << "\",\n";
117 static void EmitTypeForValueType(std::ostream &OS, MVT::SimpleValueType VT) {
118 if (MVT(VT).isInteger()) {
119 unsigned BitWidth = MVT(VT).getSizeInBits();
120 OS << "IntegerType::get(" << BitWidth << ")";
121 } else if (VT == MVT::Other) {
122 // MVT::OtherVT is used to mean the empty struct type here.
123 OS << "StructType::get(std::vector<const Type *>())";
124 } else if (VT == MVT::f32) {
125 OS << "Type::FloatTy";
126 } else if (VT == MVT::f64) {
127 OS << "Type::DoubleTy";
128 } else if (VT == MVT::f80) {
129 OS << "Type::X86_FP80Ty";
130 } else if (VT == MVT::f128) {
131 OS << "Type::FP128Ty";
132 } else if (VT == MVT::ppcf128) {
133 OS << "Type::PPC_FP128Ty";
134 } else if (VT == MVT::isVoid) {
135 OS << "Type::VoidTy";
137 assert(false && "Unsupported ValueType!");
141 static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
144 static void EmitTypeGenerate(std::ostream &OS,
145 const std::vector<Record*> &ArgTypes,
147 if (ArgTypes.size() == 1) {
148 EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
152 OS << "StructType::get(";
154 for (std::vector<Record*>::const_iterator
155 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
156 EmitTypeGenerate(OS, *I, ArgNo);
163 static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
165 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
167 if (ArgType->isSubClassOf("LLVMMatchType")) {
168 unsigned Number = ArgType->getValueAsInt("Number");
169 assert(Number < ArgNo && "Invalid matching number!");
170 OS << "Tys[" << Number << "]";
171 } else if (VT == MVT::iAny || VT == MVT::fAny) {
172 // NOTE: The ArgNo variable here is not the absolute argument number, it is
173 // the index of the "arbitrary" type in the Tys array passed to the
174 // Intrinsic::getDeclaration function. Consequently, we only want to
175 // increment it when we actually hit an overloaded type. Getting this wrong
176 // leads to very subtle bugs!
177 OS << "Tys[" << ArgNo++ << "]";
178 } else if (MVT(VT).isVector()) {
180 OS << "VectorType::get(";
181 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT());
182 OS << ", " << VVT.getVectorNumElements() << ")";
183 } else if (VT == MVT::iPTR) {
184 OS << "PointerType::getUnqual(";
185 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
187 } else if (VT == MVT::iPTRAny) {
188 // Make sure the user has passed us an argument type to overload. If not,
189 // treat it as an ordinary (not overloaded) intrinsic.
190 OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
191 << "] : PointerType::getUnqual(";
192 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
195 } else if (VT == MVT::isVoid) {
197 OS << "Type::VoidTy";
199 // MVT::isVoid is used to mean varargs here.
202 EmitTypeForValueType(OS, VT);
206 /// RecordListComparator - Provide a determinstic comparator for lists of
209 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
210 struct RecordListComparator {
211 bool operator()(const RecPair &LHS,
212 const RecPair &RHS) const {
214 const std::vector<Record*> *LHSVec = &LHS.first;
215 const std::vector<Record*> *RHSVec = &RHS.first;
216 unsigned RHSSize = RHSVec->size();
217 unsigned LHSSize = LHSVec->size();
220 if (i == RHSSize) return false; // RHS is shorter than LHS.
221 if ((*LHSVec)[i] != (*RHSVec)[i])
222 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
223 } while (++i != LHSSize);
225 if (i != RHSSize) return true;
228 LHSVec = &LHS.second;
229 RHSVec = &RHS.second;
230 RHSSize = RHSVec->size();
231 LHSSize = LHSVec->size();
233 for (i = 0; i != LHSSize; ++i) {
234 if (i == RHSSize) return false; // RHS is shorter than LHS.
235 if ((*LHSVec)[i] != (*RHSVec)[i])
236 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
244 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
246 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
247 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
248 OS << " switch (ID) {\n";
249 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
251 // This checking can emit a lot of very common code. To reduce the amount of
252 // code that we emit, batch up cases that have identical types. This avoids
253 // problems where GCC can run out of memory compiling Verifier.cpp.
254 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
255 MapTy UniqueArgInfos;
257 // Compute the unique argument type info.
258 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
259 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
260 Ints[i].IS.ParamTypeDefs)].push_back(i);
262 // Loop through the array, emitting one comparison for each batch.
263 for (MapTy::iterator I = UniqueArgInfos.begin(),
264 E = UniqueArgInfos.end(); I != E; ++I) {
265 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
266 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
267 << Ints[I->second[i]].Name << "\n";
269 const RecPair &ArgTypes = I->first;
270 const std::vector<Record*> &RetTys = ArgTypes.first;
271 const std::vector<Record*> &ParamTys = ArgTypes.second;
273 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
276 // Emit return types.
277 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
278 Record *ArgType = RetTys[j];
281 if (ArgType->isSubClassOf("LLVMMatchType")) {
282 unsigned Number = ArgType->getValueAsInt("Number");
283 assert(Number < j && "Invalid matching number!");
286 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
287 OS << getEnumName(VT);
289 if (VT == MVT::isVoid && j != 0 && j != je - 1)
290 throw "Var arg type not last argument";
294 // Emit the parameter types.
295 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
296 Record *ArgType = ParamTys[j];
299 if (ArgType->isSubClassOf("LLVMMatchType")) {
300 unsigned Number = ArgType->getValueAsInt("Number");
301 assert(Number < j + RetTys.size() && "Invalid matching number!");
304 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
305 OS << getEnumName(VT);
307 if (VT == MVT::isVoid && j != 0 && j != je - 1)
308 throw "Var arg type not last argument";
319 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
321 OS << "// Code for generating Intrinsic function declarations.\n";
322 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
323 OS << " switch (id) {\n";
324 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
326 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
328 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
329 MapTy UniqueArgInfos;
331 // Compute the unique argument type info.
332 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
333 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
334 Ints[i].IS.ParamTypeDefs)].push_back(i);
336 // Loop through the array, emitting one generator for each batch.
337 for (MapTy::iterator I = UniqueArgInfos.begin(),
338 E = UniqueArgInfos.end(); I != E; ++I) {
339 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
340 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
341 << Ints[I->second[i]].Name << "\n";
343 const RecPair &ArgTypes = I->first;
344 const std::vector<Record*> &RetTys = ArgTypes.first;
345 const std::vector<Record*> &ParamTys = ArgTypes.second;
347 unsigned N = ParamTys.size();
350 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
351 OS << " IsVarArg = true;\n";
356 OS << " ResultTy = ";
357 EmitTypeGenerate(OS, RetTys, ArgNo);
360 for (unsigned j = 0; j != N; ++j) {
361 OS << " ArgTys.push_back(";
362 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
373 void IntrinsicEmitter::
374 EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
375 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
376 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
377 OS << " switch (id) {\n";
378 OS << " default: break;\n";
379 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
380 switch (Ints[i].ModRef) {
382 case CodeGenIntrinsic::NoMem:
383 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
387 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
389 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
390 switch (Ints[i].ModRef) {
392 case CodeGenIntrinsic::ReadArgMem:
393 case CodeGenIntrinsic::ReadMem:
394 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
398 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
404 void IntrinsicEmitter::
405 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
406 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
407 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
408 OS << " switch (F->getIntrinsicID()) {\n";
409 OS << " default: BuiltinName = \"\"; break;\n";
410 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
411 if (!Ints[i].GCCBuiltinName.empty()) {
412 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
413 << Ints[i].GCCBuiltinName << "\"; break;\n";
420 /// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
421 /// sorted range of builtin names is equal to the current builtin. This breaks
422 /// it down into a simple tree.
424 /// At this point, we know that all the builtins in the range have the same name
425 /// for the first 'CharStart' characters. Only the end of the name needs to be
427 typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
428 static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
429 unsigned CharStart, unsigned Indent,
431 if (Start == End) return; // empty range.
433 // Determine what, if anything, is the same about all these strings.
434 std::string CommonString = Start->first;
435 unsigned NumInRange = 0;
436 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
437 // Find the first character that doesn't match.
438 const std::string &ThisStr = I->first;
439 unsigned NonMatchChar = CharStart;
440 while (NonMatchChar < CommonString.size() &&
441 NonMatchChar < ThisStr.size() &&
442 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
444 // Truncate off pieces that don't match.
445 CommonString.resize(NonMatchChar);
448 // Just compare the rest of the string.
449 if (NumInRange == 1) {
450 if (CharStart != CommonString.size()) {
451 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
452 if (CharStart) OS << "+" << CharStart;
453 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
454 OS << CommonString.size() - CharStart << "))\n";
457 OS << std::string(Indent*2, ' ') << "IntrinsicID = Intrinsic::";
458 OS << Start->second << ";\n";
462 // At this point, we potentially have a common prefix for these builtins, emit
463 // a check for this common prefix.
464 if (CommonString.size() != CharStart) {
465 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
466 if (CharStart) OS << "+" << CharStart;
467 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
468 OS << CommonString.size()-CharStart << ")) {\n";
470 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1, OS);
471 OS << std::string(Indent*2, ' ') << "}\n";
475 // Output a switch on the character that differs across the set.
476 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
479 OS << " // \"" << std::string(Start->first.begin(),
480 Start->first.begin()+CharStart) << "\"";
483 for (StrMapIterator I = Start; I != End; ) {
484 char ThisChar = I->first[CharStart];
485 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
486 // Figure out the range that has this common character.
487 StrMapIterator NextChar = I;
488 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
491 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, OS);
492 OS << std::string(Indent*2, ' ') << " break;\n";
495 OS << std::string(Indent*2, ' ') << "}\n";
498 /// EmitTargetBuiltins - All of the builtins in the specified map are for the
499 /// same target, and we already checked it.
500 static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
502 // Rearrange the builtins by length.
503 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
504 BuiltinsByLen.reserve(100);
506 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
507 if (I->first.size() >= BuiltinsByLen.size())
508 BuiltinsByLen.resize(I->first.size()+1);
509 BuiltinsByLen[I->first.size()].insert(*I);
512 // Now that we have all the builtins by their length, emit a switch stmt.
513 OS << " switch (strlen(BuiltinName)) {\n";
514 OS << " default: break;\n";
515 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
516 if (BuiltinsByLen[i].empty()) continue;
517 OS << " case " << i << ":\n";
518 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
526 void IntrinsicEmitter::
527 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
529 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
531 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
532 if (!Ints[i].GCCBuiltinName.empty()) {
533 // Get the map for this target prefix.
534 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
536 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
537 Ints[i].EnumName)).second)
538 throw "Intrinsic '" + Ints[i].TheDef->getName() +
539 "': duplicate GCC builtin name!";
543 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
544 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
545 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
546 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
547 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
548 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
550 // Note: this could emit significantly better code if we cared.
551 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
553 if (!I->first.empty())
554 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
556 OS << "/* Target Independent Builtins */ ";
559 // Emit the comparisons for this target prefix.
560 EmitTargetBuiltins(I->second, OS);