blob: 89e9621c172bab416061af24af4aa703f2cb2790 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29: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"
16#include "llvm/ADT/StringExtras.h"
17#include <algorithm>
18using namespace llvm;
19
20//===----------------------------------------------------------------------===//
21// IntrinsicEmitter Implementation
22//===----------------------------------------------------------------------===//
23
24void IntrinsicEmitter::run(std::ostream &OS) {
25 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
26
27 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
28
29 // Emit the enum information.
30 EmitEnumInfo(Ints, OS);
31
32 // Emit the intrinsic ID -> name table.
33 EmitIntrinsicToNameTable(Ints, OS);
34
35 // Emit the function name recognizer.
36 EmitFnNameRecognizer(Ints, OS);
37
38 // Emit the intrinsic verifier.
39 EmitVerifier(Ints, OS);
40
41 // Emit the intrinsic declaration generator.
42 EmitGenerator(Ints, OS);
43
44 // Emit mod/ref info for each function.
45 EmitModRefInfo(Ints, OS);
46
47 // Emit table of non-memory accessing intrinsics.
48 EmitNoMemoryInfo(Ints, OS);
49
50 // Emit side effect info for each intrinsic.
51 EmitSideEffectInfo(Ints, OS);
52
53 // Emit a list of intrinsics with corresponding GCC builtins.
54 EmitGCCBuiltinList(Ints, OS);
55
56 // Emit code to translate GCC builtins into LLVM intrinsics.
57 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
58}
59
60void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
61 std::ostream &OS) {
62 OS << "// Enum values for Intrinsics.h\n";
63 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
64 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
65 OS << " " << Ints[i].EnumName;
66 OS << ((i != e-1) ? ", " : " ");
67 OS << std::string(40-Ints[i].EnumName.size(), ' ')
68 << "// " << Ints[i].Name << "\n";
69 }
70 OS << "#endif\n\n";
71}
72
73void IntrinsicEmitter::
74EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
75 std::ostream &OS) {
76 // Build a function name -> intrinsic name mapping.
77 std::map<std::string, unsigned> IntMapping;
78 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
79 IntMapping[Ints[i].Name] = i;
80
81 OS << "// Function name -> enum value recognizer code.\n";
82 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
83 OS << " switch (Name[5]) {\n";
84 OS << " default:\n";
85 // Emit the intrinsics in sorted order.
86 char LastChar = 0;
87 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
88 E = IntMapping.end(); I != E; ++I) {
89 if (I->first[5] != LastChar) {
90 LastChar = I->first[5];
91 OS << " break;\n";
92 OS << " case '" << LastChar << "':\n";
93 }
94
95 // For overloaded intrinsics, only the prefix needs to match
96 if (Ints[I->second].isOverloaded)
97 OS << " if (Len >= " << I->first.size()
98 << " && !memcmp(Name, \"" << I->first << "\", " << I->first.size()
99 << ")) return Intrinsic::" << Ints[I->second].EnumName << ";\n";
100 else
101 OS << " if (Len == " << I->first.size()
102 << " && !memcmp(Name, \"" << I->first << "\", Len)) return Intrinsic::"
103 << Ints[I->second].EnumName << ";\n";
104 }
105 OS << " }\n";
106 OS << "#endif\n\n";
107}
108
109void IntrinsicEmitter::
110EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
111 std::ostream &OS) {
112 OS << "// Intrinsic ID to name table\n";
113 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
114 OS << " // Note that entry #0 is the invalid intrinsic!\n";
115 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
116 OS << " \"" << Ints[i].Name << "\",\n";
117 OS << "#endif\n\n";
118}
119
120static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) {
121 if (ArgType->getValueAsString("TypeVal") == "...") return true;
122
123 OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
124 // If this is an integer type, check the width is correct.
125 if (ArgType->isSubClassOf("LLVMIntegerType"))
126 OS << ArgType->getValueAsInt("Width") << ", ";
127
128 // If this is a vector type, check that the subtype and size are correct.
129 else if (ArgType->isSubClassOf("LLVMVectorType")) {
130 EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
131 OS << ArgType->getValueAsInt("NumElts") << ", ";
132 }
133
134 return false;
135}
136
137static void EmitTypeGenerate(std::ostream &OS, Record *ArgType,
138 unsigned &ArgNo) {
139 if (ArgType->isSubClassOf("LLVMIntegerType")) {
140 unsigned BitWidth = ArgType->getValueAsInt("Width");
141 // NOTE: The ArgNo variable here is not the absolute argument number, it is
142 // the index of the "arbitrary" type in the Tys array passed to the
143 // Intrinsic::getDeclaration function. Consequently, we only want to
144 // increment it when we actually hit an arbitrary integer type which is
145 // identified by BitWidth == 0. Getting this wrong leads to very subtle
146 // bugs!
147 if (BitWidth == 0)
148 OS << "Tys[" << ArgNo++ << "]";
149 else
150 OS << "IntegerType::get(" << BitWidth << ")";
151 } else if (ArgType->isSubClassOf("LLVMVectorType")) {
152 OS << "VectorType::get(";
153 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
154 OS << ", " << ArgType->getValueAsInt("NumElts") << ")";
155 } else if (ArgType->isSubClassOf("LLVMPointerType")) {
156 OS << "PointerType::get(";
157 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
158 OS << ")";
159 } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) {
160 OS << "StructType::get(std::vector<const Type *>())";
161 } else {
162 OS << "Type::getPrimitiveType(";
163 OS << ArgType->getValueAsString("TypeVal") << ")";
164 }
165}
166
167/// RecordListComparator - Provide a determinstic comparator for lists of
168/// records.
169namespace {
170 struct RecordListComparator {
171 bool operator()(const std::vector<Record*> &LHS,
172 const std::vector<Record*> &RHS) const {
173 unsigned i = 0;
174 do {
175 if (i == RHS.size()) return false; // RHS is shorter than LHS.
176 if (LHS[i] != RHS[i])
177 return LHS[i]->getName() < RHS[i]->getName();
178 } while (++i != LHS.size());
179
180 return i != RHS.size();
181 }
182 };
183}
184
185void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
186 std::ostream &OS) {
187 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
188 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
189 OS << " switch (ID) {\n";
190 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
191
192 // This checking can emit a lot of very common code. To reduce the amount of
193 // code that we emit, batch up cases that have identical types. This avoids
194 // problems where GCC can run out of memory compiling Verifier.cpp.
195 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
196 RecordListComparator> MapTy;
197 MapTy UniqueArgInfos;
198
199 // Compute the unique argument type info.
200 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
201 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
202
203 // Loop through the array, emitting one comparison for each batch.
204 for (MapTy::iterator I = UniqueArgInfos.begin(),
205 E = UniqueArgInfos.end(); I != E; ++I) {
206 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
207 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
208 << Ints[I->second[i]].Name << "\n";
209 }
210
211 const std::vector<Record*> &ArgTypes = I->first;
212 OS << " VerifyIntrinsicPrototype(ID, IF, ";
213 bool VarArg = false;
214 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
215 VarArg = EmitTypeVerify(OS, ArgTypes[j]);
216 if (VarArg) {
217 if ((j+1) != ArgTypes.size())
218 throw "Var arg type not last argument";
219 break;
220 }
221 }
222
223 OS << (VarArg ? "-2);\n" : "-1);\n");
224 OS << " break;\n";
225 }
226 OS << " }\n";
227 OS << "#endif\n\n";
228}
229
230void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
231 std::ostream &OS) {
232 OS << "// Code for generating Intrinsic function declarations.\n";
233 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
234 OS << " switch (id) {\n";
235 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
236
237 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
238 // types.
239 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
240 RecordListComparator> MapTy;
241 MapTy UniqueArgInfos;
242
243 // Compute the unique argument type info.
244 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
245 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
246
247 // Loop through the array, emitting one generator for each batch.
248 for (MapTy::iterator I = UniqueArgInfos.begin(),
249 E = UniqueArgInfos.end(); I != E; ++I) {
250 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
251 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
252 << Ints[I->second[i]].Name << "\n";
253 }
254
255 const std::vector<Record*> &ArgTypes = I->first;
256 unsigned N = ArgTypes.size();
257
258 if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") {
259 OS << " IsVarArg = true;\n";
260 --N;
261 }
262
263 unsigned ArgNo = 0;
264 OS << " ResultTy = ";
265 EmitTypeGenerate(OS, ArgTypes[0], ArgNo);
266 OS << ";\n";
267
268 for (unsigned j = 1; j != N; ++j) {
269 OS << " ArgTys.push_back(";
270 EmitTypeGenerate(OS, ArgTypes[j], ArgNo);
271 OS << ");\n";
272 }
273 OS << " break;\n";
274 }
275 OS << " }\n";
276 OS << "#endif\n\n";
277}
278
279void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
280 std::ostream &OS) {
281 OS << "// BasicAliasAnalysis code.\n";
282 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
283 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
284 switch (Ints[i].ModRef) {
285 default: break;
286 case CodeGenIntrinsic::NoMem:
287 OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
288 break;
289 case CodeGenIntrinsic::ReadArgMem:
290 case CodeGenIntrinsic::ReadMem:
291 OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
292 break;
293 }
294 }
295 OS << "#endif\n\n";
296}
297
298void IntrinsicEmitter::
299EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
300 OS << "// SelectionDAGIsel code.\n";
301 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
302 OS << " switch (IntrinsicID) {\n";
303 OS << " default: break;\n";
304 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
305 switch (Ints[i].ModRef) {
306 default: break;
307 case CodeGenIntrinsic::NoMem:
308 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
309 break;
310 }
311 }
312 OS << " return true; // These intrinsics have no side effects.\n";
313 OS << " }\n";
314 OS << "#endif\n\n";
315}
316
317void IntrinsicEmitter::
318EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
319 OS << "// Return true if doesn't access or only reads memory.\n";
320 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
321 OS << " switch (IntrinsicID) {\n";
322 OS << " default: break;\n";
323 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
324 switch (Ints[i].ModRef) {
325 default: break;
326 case CodeGenIntrinsic::NoMem:
327 case CodeGenIntrinsic::ReadArgMem:
328 case CodeGenIntrinsic::ReadMem:
329 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
330 break;
331 }
332 }
333 OS << " return true; // These intrinsics have no side effects.\n";
334 OS << " }\n";
335 OS << "#endif\n\n";
336}
337
338void IntrinsicEmitter::
339EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
340 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
341 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
342 OS << " switch (F->getIntrinsicID()) {\n";
343 OS << " default: BuiltinName = \"\"; break;\n";
344 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
345 if (!Ints[i].GCCBuiltinName.empty()) {
346 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
347 << Ints[i].GCCBuiltinName << "\"; break;\n";
348 }
349 }
350 OS << " }\n";
351 OS << "#endif\n\n";
352}
353
354void IntrinsicEmitter::
355EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
356 std::ostream &OS) {
357 typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
358 BIMTy BuiltinMap;
359 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
360 if (!Ints[i].GCCBuiltinName.empty()) {
361 std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
362 Ints[i].TargetPrefix);
363 if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
364 throw "Intrinsic '" + Ints[i].TheDef->getName() +
365 "': duplicate GCC builtin name!";
366 }
367 }
368
369 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
370 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
371 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
372 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
373 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
374 OS << " if (0);\n";
375 // Note: this could emit significantly better code if we cared.
376 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
377 OS << " else if (";
378 if (!I->first.second.empty()) {
379 // Emit this as a strcmp, so it can be constant folded by the FE.
380 OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
381 << " ";
382 }
383 OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
384 OS << " IntrinsicID = Intrinsic::" << I->second << ";\n";
385 }
386 OS << " else\n";
387 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
388 OS << "#endif\n\n";
389}