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 "IntrinsicEmitter.h"
16 #include "llvm/ADT/StringExtras.h"
19 //===----------------------------------------------------------------------===//
20 // CodeGenIntrinsic Implementation
21 //===----------------------------------------------------------------------===//
23 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
24 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
25 return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
28 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
29 std::string DefName = R->getName();
32 if (DefName.size() <= 4 ||
33 std::string(DefName.begin(), DefName.begin()+4) != "int_")
34 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
35 EnumName = std::string(DefName.begin()+4, DefName.end());
36 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
38 Name = R->getValueAsString("LLVMName");
40 // If an explicit name isn't specified, derive one from the DefName.
42 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
43 if (EnumName[i] == '_')
49 // Parse the list of argument types.
50 ListInit *TypeList = R->getValueAsListInit("Types");
51 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
52 DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
53 assert(DI && "Invalid list type!");
54 Record *TyEl = DI->getDef();
55 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
56 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
57 ArgTypeDefs.push_back(TyEl);
59 if (ArgTypes.size() == 0)
60 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
62 // Parse the intrinsic properties.
63 ListInit *PropList = R->getValueAsListInit("Properties");
64 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
65 DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
66 assert(DI && "Invalid list type!");
67 Record *Property = DI->getDef();
68 assert(Property->isSubClassOf("IntrinsicProperty") &&
69 "Expected a property!");
71 if (Property->getName() == "InstrNoMem")
73 else if (Property->getName() == "InstrReadArgMem")
75 else if (Property->getName() == "IntrReadMem")
77 else if (Property->getName() == "InstrWriteArgMem")
79 else if (Property->getName() == "IntrWriteMem")
82 assert(0 && "Unknown property!");
86 //===----------------------------------------------------------------------===//
87 // IntrinsicEmitter Implementation
88 //===----------------------------------------------------------------------===//
90 void IntrinsicEmitter::run(std::ostream &OS) {
91 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
93 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
95 // Emit the enum information.
96 EmitEnumInfo(Ints, OS);
98 // Emit the function name recognizer.
99 EmitFnNameRecognizer(Ints, OS);
101 // Emit the intrinsic verifier.
102 EmitVerifier(Ints, OS);
104 // Emit mod/ref info for each function.
105 EmitModRefInfo(Ints, OS);
107 // Emit side effect info for each function.
108 EmitSideEffectInfo(Ints, OS);
110 // Emit a list of intrinsics with corresponding GCC builtins.
111 EmitGCCBuiltinList(Ints, OS);
114 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
116 OS << "// Enum values for Intrinsics.h\n";
117 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
118 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
119 OS << " " << Ints[i].EnumName;
120 OS << ((i != e-1) ? ", " : " ");
121 OS << std::string(40-Ints[i].EnumName.size(), ' ')
122 << "// " << Ints[i].Name << "\n";
127 void IntrinsicEmitter::
128 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
130 // Build a function name -> intrinsic name mapping.
131 std::map<std::string, std::string> IntMapping;
132 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
133 IntMapping[Ints[i].Name] = Ints[i].EnumName;
135 OS << "// Function name -> enum value recognizer code.\n";
136 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
137 OS << " switch (Name[5]) {\n";
138 OS << " default: break;\n";
139 // Emit the intrinsics in sorted order.
141 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
142 E = IntMapping.end(); I != E; ++I) {
143 assert(I->first.size() > 5 && std::string(I->first.begin(),
144 I->first.begin()+5) == "llvm." &&
145 "Invalid intrinsic name!");
146 if (I->first[5] != LastChar) {
147 LastChar = I->first[5];
148 OS << " case '" << LastChar << "':\n";
151 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
152 << I->second << ";\n";
155 OS << " // The 'llvm.' namespace is reserved!\n";
156 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
160 static void EmitTypeVerify(std::ostream &OS, const std::string &Val,
162 OS << " Assert1(" << Val << "->getTypeID() == "
163 << ArgType->getValueAsString("TypeVal") << ",\n"
164 << " \"Illegal intrinsic type!\", IF);\n";
166 // If this is a packed type, check that the subtype and size are correct.
167 if (ArgType->isSubClassOf("LLVMPackedType")) {
168 Record *SubType = ArgType->getValueAsDef("ElTy");
169 OS << " Assert1(cast<PackedType>(" << Val
170 << ")->getElementType()->getTypeID() == "
171 << SubType->getValueAsString("TypeVal") << ",\n"
172 << " \"Illegal intrinsic type!\", IF);\n";
173 OS << " Assert1(cast<PackedType>(" << Val << ")->getNumElements() == "
174 << ArgType->getValueAsInt("NumElts") << ",\n"
175 << " \"Illegal intrinsic type!\", IF);\n";
179 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
181 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
182 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
183 OS << " switch (ID) {\n";
184 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
185 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
186 OS << " case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
187 << Ints[i].Name << "\n";
188 OS << " Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
190 << " \"Illegal # arguments for intrinsic function!\", IF);\n";
191 EmitTypeVerify(OS, "FTy->getReturnType()", Ints[i].ArgTypeDefs[0]);
192 for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
193 EmitTypeVerify(OS, "FTy->getParamType(" + utostr(j-1) + ")",
194 Ints[i].ArgTypeDefs[j]);
201 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
203 OS << "// BasicAliasAnalysis code.\n";
204 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
205 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
206 switch (Ints[i].ModRef) {
208 case CodeGenIntrinsic::NoMem:
209 OS << " NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
211 case CodeGenIntrinsic::ReadArgMem:
212 case CodeGenIntrinsic::ReadMem:
213 OS << " OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
220 void IntrinsicEmitter::
221 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
222 OS << "// isInstructionTriviallyDead code.\n";
223 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
224 OS << " switch (F->getIntrinsicID()) {\n";
225 OS << " default: break;\n";
226 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
227 switch (Ints[i].ModRef) {
229 case CodeGenIntrinsic::NoMem:
230 case CodeGenIntrinsic::ReadArgMem:
231 case CodeGenIntrinsic::ReadMem:
232 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
236 OS << " return true; // These intrinsics have no side effects.\n";
241 void IntrinsicEmitter::
242 EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
243 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
244 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
245 OS << " switch (F->getIntrinsicID()) {\n";
246 OS << " default: BuiltinName = \"\"; break;\n";
247 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
248 if (!Ints[i].GCCBuiltinName.empty()) {
249 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
250 << Ints[i].GCCBuiltinName << "\"; break;\n";