blob: 8aa29c7c16d14b82c3410f0daca495f610d847f2 [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IntrinsicEmitter.h"
15#include "Record.h"
Chris Lattner18faf5d2006-03-13 22:38:57 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000017using namespace llvm;
18
19//===----------------------------------------------------------------------===//
20// CodeGenIntrinsic Implementation
21//===----------------------------------------------------------------------===//
22
23std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) {
24 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic");
25 return std::vector<CodeGenIntrinsic>(I.begin(), I.end());
26}
27
28CodeGenIntrinsic::CodeGenIntrinsic(Record *R) {
29 std::string DefName = R->getName();
Chris Lattner6448ee42006-03-09 22:30:49 +000030 ModRef = WriteMem;
Chris Lattner9e493cf2006-03-03 02:32:46 +000031
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());
Chris Lattner022f64f2006-03-13 23:08:44 +000036 GCCBuiltinName = R->getValueAsString("GCCBuiltinName");
Chris Lattner9e493cf2006-03-03 02:32:46 +000037
38 Name = R->getValueAsString("LLVMName");
39 if (Name == "") {
40 // If an explicit name isn't specified, derive one from the DefName.
41 Name = "llvm.";
42 for (unsigned i = 0, e = EnumName.size(); i != e; ++i)
43 if (EnumName[i] == '_')
44 Name += '.';
45 else
46 Name += EnumName[i];
47 }
Chris Lattnerf97a00e2006-03-09 22:05:04 +000048
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"));
Chris Lattner18faf5d2006-03-13 22:38:57 +000057 ArgTypeDefs.push_back(TyEl);
Chris Lattnerf97a00e2006-03-09 22:05:04 +000058 }
59 if (ArgTypes.size() == 0)
60 throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!";
Chris Lattner6448ee42006-03-09 22:30:49 +000061
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!");
70
71 if (Property->getName() == "InstrNoMem")
72 ModRef = NoMem;
73 else if (Property->getName() == "InstrReadArgMem")
74 ModRef = ReadArgMem;
75 else if (Property->getName() == "IntrReadMem")
76 ModRef = ReadMem;
77 else if (Property->getName() == "InstrWriteArgMem")
78 ModRef = WriteArgMem;
79 else if (Property->getName() == "IntrWriteMem")
80 ModRef = WriteMem;
81 else
82 assert(0 && "Unknown property!");
83 }
Chris Lattner9e493cf2006-03-03 02:32:46 +000084}
85
86//===----------------------------------------------------------------------===//
87// IntrinsicEmitter Implementation
88//===----------------------------------------------------------------------===//
89
90void IntrinsicEmitter::run(std::ostream &OS) {
91 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
92
93 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
94
95 // Emit the enum information.
96 EmitEnumInfo(Ints, OS);
Chris Lattner9b843b22006-03-09 20:34:19 +000097
98 // Emit the function name recognizer.
99 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000100
101 // Emit the intrinsic verifier.
102 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +0000103
104 // Emit mod/ref info for each function.
105 EmitModRefInfo(Ints, OS);
Chris Lattner4e5f3592006-03-09 22:37:52 +0000106
107 // Emit side effect info for each function.
108 EmitSideEffectInfo(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +0000109
110 // Emit a list of intrinsics with corresponding GCC builtins.
111 EmitGCCBuiltinList(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +0000112}
113
114void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
115 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +0000116 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +0000117 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";
123 }
124 OS << "#endif\n\n";
125}
Chris Lattner9b843b22006-03-09 20:34:19 +0000126
127void IntrinsicEmitter::
128EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
129 std::ostream &OS) {
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;
134
135 OS << "// Function name -> enum value recognizer code.\n";
136 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
137 OS << " switch (Name[5]) {\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000138 OS << " default: break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000139 // Emit the intrinsics in sorted order.
140 char LastChar = 0;
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";
149 }
150
151 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
152 << I->second << ";\n";
153 }
154 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000155 OS << " // The 'llvm.' namespace is reserved!\n";
156 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
157 OS << "#endif\n\n";
158}
159
Chris Lattner18faf5d2006-03-13 22:38:57 +0000160static void EmitTypeVerify(std::ostream &OS, const std::string &Val,
161 Record *ArgType) {
162 OS << " Assert1(" << Val << "->getTypeID() == "
163 << ArgType->getValueAsString("TypeVal") << ",\n"
164 << " \"Illegal intrinsic type!\", IF);\n";
165
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";
176 }
177}
178
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000179void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
180 std::ostream &OS) {
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
189 << ",\n"
190 << " \"Illegal # arguments for intrinsic function!\", IF);\n";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000191 EmitTypeVerify(OS, "FTy->getReturnType()", Ints[i].ArgTypeDefs[0]);
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000192 for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j)
Chris Lattner18faf5d2006-03-13 22:38:57 +0000193 EmitTypeVerify(OS, "FTy->getParamType(" + utostr(j-1) + ")",
194 Ints[i].ArgTypeDefs[j]);
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000195 OS << " break;\n";
196 }
197 OS << " }\n";
198 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000199}
200
Chris Lattner6448ee42006-03-09 22:30:49 +0000201void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
202 std::ostream &OS) {
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) {
207 default: break;
208 case CodeGenIntrinsic::NoMem:
209 OS << " NoMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
210 break;
211 case CodeGenIntrinsic::ReadArgMem:
212 case CodeGenIntrinsic::ReadMem:
213 OS << " OnlyReadsMemoryTable.push_back(\"" << Ints[i].Name << "\");\n";
214 break;
215 }
216 }
217 OS << "#endif\n\n";
218}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000219
220void IntrinsicEmitter::
221EmitSideEffectInfo(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) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000228 default: break;
229 case CodeGenIntrinsic::NoMem:
230 case CodeGenIntrinsic::ReadArgMem:
231 case CodeGenIntrinsic::ReadMem:
232 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
233 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000234 }
235 }
236 OS << " return true; // These intrinsics have no side effects.\n";
237 OS << " }\n";
238 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000239}
Chris Lattner022f64f2006-03-13 23:08:44 +0000240
241void IntrinsicEmitter::
242EmitGCCBuiltinList(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";
251 }
252 }
253 OS << " }\n";
254 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000255}