1 //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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 mod/ref info for each function.
46 EmitModRefInfo(Ints, OS);
48 // Emit table of non-memory accessing intrinsics.
49 EmitNoMemoryInfo(Ints, OS);
51 // Emit side effect info for each intrinsic.
52 EmitSideEffectInfo(Ints, OS);
54 // Emit a list of intrinsics with corresponding GCC builtins.
55 EmitGCCBuiltinList(Ints, OS);
57 // Emit code to translate GCC builtins into LLVM intrinsics.
58 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
61 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
63 OS << "// Enum values for Intrinsics.h\n";
64 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
65 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
66 OS << " " << Ints[i].EnumName;
67 OS << ((i != e-1) ? ", " : " ");
68 OS << std::string(40-Ints[i].EnumName.size(), ' ')
69 << "// " << Ints[i].Name << "\n";
74 void IntrinsicEmitter::
75 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
77 // Build a function name -> intrinsic name mapping.
78 std::map<std::string, unsigned> IntMapping;
79 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
80 IntMapping[Ints[i].Name] = i;
82 OS << "// Function name -> enum value recognizer code.\n";
83 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
84 OS << " switch (Name[5]) {\n";
86 // Emit the intrinsics in sorted order.
88 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
89 E = IntMapping.end(); I != E; ++I) {
90 if (I->first[5] != LastChar) {
91 LastChar = I->first[5];
93 OS << " case '" << LastChar << "':\n";
96 // For overloaded intrinsics, only the prefix needs to match
97 if (Ints[I->second].isOverloaded)
98 OS << " if (Len > " << I->first.size()
99 << " && !memcmp(Name, \"" << I->first << ".\", "
100 << (I->first.size() + 1) << ")) return Intrinsic::"
101 << Ints[I->second].EnumName << ";\n";
103 OS << " if (Len == " << I->first.size()
104 << " && !memcmp(Name, \"" << I->first << "\", "
105 << I->first.size() << ")) return Intrinsic::"
106 << Ints[I->second].EnumName << ";\n";
112 void IntrinsicEmitter::
113 EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
115 OS << "// Intrinsic ID to name table\n";
116 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
117 OS << " // Note that entry #0 is the invalid intrinsic!\n";
118 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
119 OS << " \"" << Ints[i].Name << "\",\n";
123 static void EmitTypeForValueType(std::ostream &OS, MVT::ValueType VT) {
124 if (MVT::isInteger(VT)) {
125 unsigned BitWidth = MVT::getSizeInBits(VT);
126 OS << "IntegerType::get(" << BitWidth << ")";
127 } else if (VT == MVT::Other) {
128 // MVT::OtherVT is used to mean the empty struct type here.
129 OS << "StructType::get(std::vector<const Type *>())";
130 } else if (VT == MVT::f32) {
131 OS << "Type::FloatTy";
132 } else if (VT == MVT::f64) {
133 OS << "Type::DoubleTy";
134 } else if (VT == MVT::isVoid) {
135 OS << "Type::VoidTy";
137 assert(false && "Unsupported ValueType!");
141 static void EmitTypeGenerate(std::ostream &OS, Record *ArgType,
143 MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT"));
145 if (ArgType->isSubClassOf("LLVMMatchType")) {
146 unsigned Number = ArgType->getValueAsInt("Number");
147 assert(Number < ArgNo && "Invalid matching number!");
148 OS << "Tys[" << Number << "]";
149 } else if (VT == MVT::iAny || VT == MVT::fAny) {
150 // NOTE: The ArgNo variable here is not the absolute argument number, it is
151 // the index of the "arbitrary" type in the Tys array passed to the
152 // Intrinsic::getDeclaration function. Consequently, we only want to
153 // increment it when we actually hit an overloaded type. Getting this wrong
154 // leads to very subtle bugs!
155 OS << "Tys[" << ArgNo++ << "]";
156 } else if (MVT::isVector(VT)) {
157 OS << "VectorType::get(";
158 EmitTypeForValueType(OS, MVT::getVectorElementType(VT));
159 OS << ", " << MVT::getVectorNumElements(VT) << ")";
160 } else if (VT == MVT::iPTR) {
161 OS << "PointerType::get(";
162 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
164 } else if (VT == MVT::isVoid) {
166 OS << "Type::VoidTy";
168 // MVT::isVoid is used to mean varargs here.
171 EmitTypeForValueType(OS, VT);
175 /// RecordListComparator - Provide a determinstic comparator for lists of
178 struct RecordListComparator {
179 bool operator()(const std::vector<Record*> &LHS,
180 const std::vector<Record*> &RHS) const {
183 if (i == RHS.size()) return false; // RHS is shorter than LHS.
184 if (LHS[i] != RHS[i])
185 return LHS[i]->getName() < RHS[i]->getName();
186 } while (++i != LHS.size());
188 return i != RHS.size();
193 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
195 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
196 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
197 OS << " switch (ID) {\n";
198 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
200 // This checking can emit a lot of very common code. To reduce the amount of
201 // code that we emit, batch up cases that have identical types. This avoids
202 // problems where GCC can run out of memory compiling Verifier.cpp.
203 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
204 RecordListComparator> MapTy;
205 MapTy UniqueArgInfos;
207 // Compute the unique argument type info.
208 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
209 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
211 // Loop through the array, emitting one comparison for each batch.
212 for (MapTy::iterator I = UniqueArgInfos.begin(),
213 E = UniqueArgInfos.end(); I != E; ++I) {
214 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
215 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
216 << Ints[I->second[i]].Name << "\n";
219 const std::vector<Record*> &ArgTypes = I->first;
220 OS << " VerifyIntrinsicPrototype(ID, IF, " << ArgTypes.size() << ", ";
221 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
222 Record *ArgType = ArgTypes[j];
223 if (ArgType->isSubClassOf("LLVMMatchType")) {
224 unsigned Number = ArgType->getValueAsInt("Number");
225 assert(Number < j && "Invalid matching number!");
228 MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT"));
229 OS << getEnumName(VT);
230 if (VT == MVT::isVoid && j != 0 && j != ArgTypes.size()-1)
231 throw "Var arg type not last argument";
233 if (j != ArgTypes.size()-1)
244 void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
246 OS << "// Code for generating Intrinsic function declarations.\n";
247 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
248 OS << " switch (id) {\n";
249 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
251 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
253 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
254 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[Ints[i].ArgTypeDefs].push_back(i);
261 // Loop through the array, emitting one generator for each batch.
262 for (MapTy::iterator I = UniqueArgInfos.begin(),
263 E = UniqueArgInfos.end(); I != E; ++I) {
264 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
265 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
266 << Ints[I->second[i]].Name << "\n";
269 const std::vector<Record*> &ArgTypes = I->first;
270 unsigned N = ArgTypes.size();
273 getValueType(ArgTypes[N-1]->getValueAsDef("VT")) == MVT::isVoid) {
274 OS << " IsVarArg = true;\n";
279 OS << " ResultTy = ";
280 EmitTypeGenerate(OS, ArgTypes[0], ArgNo);
283 for (unsigned j = 1; j != N; ++j) {
284 OS << " ArgTys.push_back(";
285 EmitTypeGenerate(OS, ArgTypes[j], ArgNo);
294 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
296 OS << "// BasicAliasAnalysis code.\n";
297 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
298 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
299 switch (Ints[i].ModRef) {
301 case CodeGenIntrinsic::NoMem:
302 OS << " NoMemoryIntrinsics->set(Intrinsic::" << Ints[i].EnumName << ");\n";
304 case CodeGenIntrinsic::ReadArgMem:
305 case CodeGenIntrinsic::ReadMem:
306 OS << " OnlyReadsMemoryIntrinsics->set(Intrinsic::" << Ints[i].EnumName << ");\n";
313 void IntrinsicEmitter::
314 EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
315 OS << "// SelectionDAGIsel code.\n";
316 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
317 OS << " switch (IntrinsicID) {\n";
318 OS << " default: break;\n";
319 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
320 switch (Ints[i].ModRef) {
322 case CodeGenIntrinsic::NoMem:
323 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
327 OS << " return true; // These intrinsics do not reference memory.\n";
332 void IntrinsicEmitter::
333 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
334 OS << "// Return true if doesn't access or only reads memory.\n";
335 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
336 OS << " switch (IntrinsicID) {\n";
337 OS << " default: break;\n";
338 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
339 switch (Ints[i].ModRef) {
341 case CodeGenIntrinsic::NoMem:
342 case CodeGenIntrinsic::ReadArgMem:
343 case CodeGenIntrinsic::ReadMem:
344 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
348 OS << " return true; // These intrinsics have no side effects.\n";
353 void IntrinsicEmitter::
354 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
355 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
356 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
357 OS << " switch (F->getIntrinsicID()) {\n";
358 OS << " default: BuiltinName = \"\"; break;\n";
359 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
360 if (!Ints[i].GCCBuiltinName.empty()) {
361 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
362 << Ints[i].GCCBuiltinName << "\"; break;\n";
369 void IntrinsicEmitter::
370 EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
372 typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
374 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
375 if (!Ints[i].GCCBuiltinName.empty()) {
376 std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
377 Ints[i].TargetPrefix);
378 if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
379 throw "Intrinsic '" + Ints[i].TheDef->getName() +
380 "': duplicate GCC builtin name!";
384 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
385 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
386 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
387 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
388 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
390 // Note: this could emit significantly better code if we cared.
391 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
393 if (!I->first.second.empty()) {
394 // Emit this as a strcmp, so it can be constant folded by the FE.
395 OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
398 OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
399 OS << " IntrinsicID = Intrinsic::" << I->second << ";\n";
402 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";