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"
18 //===----------------------------------------------------------------------===//
19 // CodeGenIntrinsic Implementation
20 //===----------------------------------------------------------------------===//
22 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
23 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
24 return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
27 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
28 std::string DefName = R->getName();
31 if (DefName.size() <= 4 ||
32 std::string(DefName.begin(), DefName.begin()+4) != "int_")
33 throw "Intrinsic '" + DefName + "' does not start with 'int_'!";
34 EnumName = std::string(DefName.begin()+4, DefName.end());
36 Name = R->getValueAsString("LLVMName");
38 // If an explicit name isn't specified, derive one from the DefName.
40 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
41 if (EnumName[i] == '_')
47 // Parse the list of argument types.
48 ListInit *TypeList = R->getValueAsListInit("Types");
49 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) {
50 DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i));
51 assert(DI && "Invalid list type!");
52 Record *TyEl = DI->getDef();
53 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!");
54 ArgTypes.push_back(TyEl->getValueAsString("TypeVal"));
56 if (ArgTypes.size() == 0)
57 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
59 // Parse the intrinsic properties.
60 ListInit *PropList = R->getValueAsListInit("Properties");
61 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) {
62 DefInit *DI = dynamic_cast<DefInit*>(PropList->getElement(i));
63 assert(DI && "Invalid list type!");
64 Record *Property = DI->getDef();
65 assert(Property->isSubClassOf("IntrinsicProperty") &&
66 "Expected a property!");
68 if (Property->getName() == "InstrNoMem")
70 else if (Property->getName() == "InstrReadArgMem")
72 else if (Property->getName() == "IntrReadMem")
74 else if (Property->getName() == "InstrWriteArgMem")
76 else if (Property->getName() == "IntrWriteMem")
79 assert(0 && "Unknown property!");
83 //===----------------------------------------------------------------------===//
84 // IntrinsicEmitter Implementation
85 //===----------------------------------------------------------------------===//
87 void IntrinsicEmitter::run(std::ostream &OS) {
88 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
90 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
92 // Emit the enum information.
93 EmitEnumInfo(Ints, OS);
95 // Emit the function name recognizer.
96 EmitFnNameRecognizer(Ints, OS);
98 // Emit the intrinsic verifier.
99 EmitVerifier(Ints, OS);
101 // Emit mod/ref info for each function.
102 EmitModRefInfo(Ints, OS);
104 // Emit side effect info for each function.
105 EmitSideEffectInfo(Ints, OS);
108 void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
110 OS << "// Enum values for Intrinsics.h\n";
111 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
112 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
113 OS << " " << Ints[i].EnumName;
114 OS << ((i != e-1) ? ", " : " ");
115 OS << std::string(40-Ints[i].EnumName.size(), ' ')
116 << "// " << Ints[i].Name << "\n";
121 void IntrinsicEmitter::
122 EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
124 // Build a function name -> intrinsic name mapping.
125 std::map<std::string, std::string> IntMapping;
126 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
127 IntMapping[Ints[i].Name] = Ints[i].EnumName;
129 OS << "// Function name -> enum value recognizer code.\n";
130 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
131 OS << " switch (Name[5]) {\n";
132 OS << " default: break;\n";
133 // Emit the intrinsics in sorted order.
135 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
136 E = IntMapping.end(); I != E; ++I) {
137 assert(I->first.size() > 5 && std::string(I->first.begin(),
138 I->first.begin()+5) == "llvm." &&
139 "Invalid intrinsic name!");
140 if (I->first[5] != LastChar) {
141 LastChar = I->first[5];
142 OS << " case '" << LastChar << "':\n";
145 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
146 << I->second << ";\n";
149 OS << " // The 'llvm.' namespace is reserved!\n";
150 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
154 void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
156 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
157 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
158 OS << " switch (ID) {\n";
159 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
160 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
161 OS << " case Intrinsic::" << Ints[i].EnumName << ":\t\t// "
162 << Ints[i].Name << "\n";
163 OS << " Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1
165 << " \"Illegal # arguments for intrinsic function!\", IF);\n";
166 OS << " Assert1(FTy->getReturnType()->getTypeID() == "
167 << Ints[i].ArgTypes[0] << ",\n"
168 << " \"Illegal result type!\", IF);\n";
169 for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
170 OS << " Assert1(FTy->getParamType(" << j-1 << ")->getTypeID() == "
171 << Ints[i].ArgTypes[j] << ",\n"
172 << " \"Illegal argument type!\", IF);\n";
179 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
181 OS << "// BasicAliasAnalysis code.\n";
182 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
183 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
184 switch (Ints[i].ModRef) {
186 case CodeGenIntrinsic::NoMem:
187 OS << " NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
189 case CodeGenIntrinsic::ReadArgMem:
190 case CodeGenIntrinsic::ReadMem:
191 OS << " OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
198 void IntrinsicEmitter::
199 EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
200 OS << "// isInstructionTriviallyDead code.\n";
201 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
202 OS << " switch (F->getIntrinsicID()) {\n";
203 OS << " default: break;\n";
204 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
205 switch (Ints[i].ModRef) {
207 case CodeGenIntrinsic::NoMem:
208 case CodeGenIntrinsic::ReadArgMem:
209 case CodeGenIntrinsic::ReadMem:
210 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
214 OS << " return true; // These intrinsics have no side effects.\n";