blob: f884c424a71f3c9fd6208fad043ca64f0cb36079 [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"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000017#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000018using namespace llvm;
19
20//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000021// 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);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000031
32 // Emit the intrinsic ID -> name table.
33 EmitIntrinsicToNameTable(Ints, OS);
Chris Lattner9b843b22006-03-09 20:34:19 +000034
35 // Emit the function name recognizer.
36 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000037
Chris Lattnerf97a00e2006-03-09 22:05:04 +000038 // Emit the intrinsic verifier.
39 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000040
Jim Laskey95af5922007-02-07 20:38:26 +000041 // Emit the intrinsic declaration generator.
42 EmitGenerator(Ints, OS);
43
Chris Lattner6448ee42006-03-09 22:30:49 +000044 // Emit mod/ref info for each function.
45 EmitModRefInfo(Ints, OS);
Chris Lattner4e5f3592006-03-09 22:37:52 +000046
Chris Lattner7056de32006-03-24 01:13:55 +000047 // Emit table of non-memory accessing intrinsics.
48 EmitNoMemoryInfo(Ints, OS);
49
50 // Emit side effect info for each intrinsic.
Chris Lattner4e5f3592006-03-09 22:37:52 +000051 EmitSideEffectInfo(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000052
53 // Emit a list of intrinsics with corresponding GCC builtins.
54 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000055
56 // Emit code to translate GCC builtins into LLVM intrinsics.
57 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +000058}
59
60void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
61 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000062 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000063 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}
Chris Lattner9b843b22006-03-09 20:34:19 +000072
73void IntrinsicEmitter::
74EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
75 std::ostream &OS) {
76 // Build a function name -> intrinsic name mapping.
Reid Spencerc4de3de2007-04-01 07:20:02 +000077 std::map<std::string, unsigned> IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +000078 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Reid Spencerc4de3de2007-04-01 07:20:02 +000079 IntMapping[Ints[i].Name] = i;
Chris Lattner9b843b22006-03-09 20:34:19 +000080
81 OS << "// Function name -> enum value recognizer code.\n";
82 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
83 OS << " switch (Name[5]) {\n";
Chris Lattner3b515802007-02-15 19:17:16 +000084 OS << " default:\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000085 // Emit the intrinsics in sorted order.
86 char LastChar = 0;
Reid Spencerc4de3de2007-04-01 07:20:02 +000087 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +000088 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +000089 if (I->first[5] != LastChar) {
90 LastChar = I->first[5];
Chris Lattner3b515802007-02-15 19:17:16 +000091 OS << " break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000092 OS << " case '" << LastChar << "':\n";
93 }
94
Reid Spencerc4de3de2007-04-01 07:20:02 +000095 // 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";
Chris Lattner9b843b22006-03-09 20:34:19 +0000104 }
105 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000106 OS << " // The 'llvm.' namespace is reserved!\n";
107 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
108 OS << "#endif\n\n";
109}
110
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000111void IntrinsicEmitter::
112EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
113 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000114 OS << "// Intrinsic ID to name table\n";
115 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
116 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000117 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
118 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000119 OS << "#endif\n\n";
120}
121
Jim Laskey95d97b92007-02-06 18:30:58 +0000122static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) {
123 if (ArgType->getValueAsString("TypeVal") == "...") return true;
Jim Laskeyba4cc092007-02-06 18:02:54 +0000124
Chris Lattnerf124b462006-03-31 04:48:26 +0000125 OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000126 // If this is an integer type, check the width is correct.
127 if (ArgType->isSubClassOf("LLVMIntegerType"))
128 OS << ArgType->getValueAsInt("Width") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000129
Reid Spencerac9dcb92007-02-15 03:39:18 +0000130 // If this is a vector type, check that the subtype and size are correct.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000131 else if (ArgType->isSubClassOf("LLVMVectorType")) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000132 EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
133 OS << ArgType->getValueAsInt("NumElts") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000134 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000135
136 return false;
Chris Lattner18faf5d2006-03-13 22:38:57 +0000137}
138
Reid Spencerc4de3de2007-04-01 07:20:02 +0000139static void EmitTypeGenerate(std::ostream &OS, Record *ArgType, unsigned ArgNo){
Jim Laskey95af5922007-02-07 20:38:26 +0000140 if (ArgType->isSubClassOf("LLVMIntegerType")) {
Reid Spencerc4de3de2007-04-01 07:20:02 +0000141 unsigned BitWidth = ArgType->getValueAsInt("Width");
142 if (BitWidth == 0)
143 OS << "Tys[" << ArgNo << "]";
144 else
145 OS << "IntegerType::get(" << BitWidth << ")";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000146 } else if (ArgType->isSubClassOf("LLVMVectorType")) {
147 OS << "VectorType::get(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000148 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000149 OS << ", " << ArgType->getValueAsInt("NumElts") << ")";
150 } else if (ArgType->isSubClassOf("LLVMPointerType")) {
151 OS << "PointerType::get(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000152 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000153 OS << ")";
154 } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) {
155 OS << "StructType::get(std::vector<const Type *>())";
156 } else {
157 OS << "Type::getPrimitiveType(";
158 OS << ArgType->getValueAsString("TypeVal") << ")";
159 }
160}
161
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000162/// RecordListComparator - Provide a determinstic comparator for lists of
163/// records.
164namespace {
165 struct RecordListComparator {
166 bool operator()(const std::vector<Record*> &LHS,
167 const std::vector<Record*> &RHS) const {
168 unsigned i = 0;
169 do {
170 if (i == RHS.size()) return false; // RHS is shorter than LHS.
171 if (LHS[i] != RHS[i])
172 return LHS[i]->getName() < RHS[i]->getName();
173 } while (++i != LHS.size());
174
175 return i != RHS.size();
176 }
177 };
178}
179
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000180void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
181 std::ostream &OS) {
182 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
183 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
184 OS << " switch (ID) {\n";
185 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000186
187 // This checking can emit a lot of very common code. To reduce the amount of
188 // code that we emit, batch up cases that have identical types. This avoids
189 // problems where GCC can run out of memory compiling Verifier.cpp.
190 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
191 RecordListComparator> MapTy;
192 MapTy UniqueArgInfos;
193
194 // Compute the unique argument type info.
195 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
196 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
197
198 // Loop through the array, emitting one comparison for each batch.
199 for (MapTy::iterator I = UniqueArgInfos.begin(),
200 E = UniqueArgInfos.end(); I != E; ++I) {
201 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
202 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
203 << Ints[I->second[i]].Name << "\n";
204 }
Chris Lattnerf124b462006-03-31 04:48:26 +0000205
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000206 const std::vector<Record*> &ArgTypes = I->first;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000207 OS << " VerifyIntrinsicPrototype(ID, IF, ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000208 bool VarArg = false;
209 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
210 VarArg = EmitTypeVerify(OS, ArgTypes[j]);
211 if (VarArg) {
212 if ((j+1) != ArgTypes.size())
213 throw "Var arg type not last argument";
214 break;
215 }
216 }
217
218 OS << (VarArg ? "-2);\n" : "-1);\n");
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000219 OS << " break;\n";
220 }
221 OS << " }\n";
222 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000223}
224
Jim Laskey95af5922007-02-07 20:38:26 +0000225void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
226 std::ostream &OS) {
227 OS << "// Code for generating Intrinsic function declarations.\n";
228 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
229 OS << " switch (id) {\n";
230 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
231
232 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
233 // types.
234 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
235 RecordListComparator> MapTy;
236 MapTy UniqueArgInfos;
237
238 // Compute the unique argument type info.
239 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
240 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
241
242 // Loop through the array, emitting one generator for each batch.
243 for (MapTy::iterator I = UniqueArgInfos.begin(),
244 E = UniqueArgInfos.end(); I != E; ++I) {
245 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
246 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
247 << Ints[I->second[i]].Name << "\n";
248 }
249
250 const std::vector<Record*> &ArgTypes = I->first;
251 unsigned N = ArgTypes.size();
252
253 if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") {
254 OS << " IsVarArg = true;\n";
255 --N;
256 }
257
258 OS << " ResultTy = ";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000259 EmitTypeGenerate(OS, ArgTypes[0], 0);
Jim Laskey95af5922007-02-07 20:38:26 +0000260 OS << ";\n";
261
262 for (unsigned j = 1; j != N; ++j) {
263 OS << " ArgTys.push_back(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000264 EmitTypeGenerate(OS, ArgTypes[j], j);
Jim Laskey95af5922007-02-07 20:38:26 +0000265 OS << ");\n";
266 }
267
268 OS << " break;\n";
269 }
270 OS << " }\n";
271 OS << "#endif\n\n";
272}
273
Chris Lattner6448ee42006-03-09 22:30:49 +0000274void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
275 std::ostream &OS) {
276 OS << "// BasicAliasAnalysis code.\n";
277 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
278 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
279 switch (Ints[i].ModRef) {
280 default: break;
281 case CodeGenIntrinsic::NoMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000282 OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000283 break;
284 case CodeGenIntrinsic::ReadArgMem:
285 case CodeGenIntrinsic::ReadMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000286 OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000287 break;
288 }
289 }
290 OS << "#endif\n\n";
291}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000292
293void IntrinsicEmitter::
Chris Lattner7056de32006-03-24 01:13:55 +0000294EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
295 OS << "// SelectionDAGIsel code.\n";
296 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
297 OS << " switch (IntrinsicID) {\n";
298 OS << " default: break;\n";
299 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
300 switch (Ints[i].ModRef) {
301 default: break;
302 case CodeGenIntrinsic::NoMem:
303 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
304 break;
305 }
306 }
307 OS << " return true; // These intrinsics have no side effects.\n";
308 OS << " }\n";
309 OS << "#endif\n\n";
310}
311
312void IntrinsicEmitter::
Chris Lattner4e5f3592006-03-09 22:37:52 +0000313EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
Chris Lattner5348e392006-04-02 03:35:30 +0000314 OS << "// Return true if doesn't access or only reads memory.\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000315 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
Chris Lattner5348e392006-04-02 03:35:30 +0000316 OS << " switch (IntrinsicID) {\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000317 OS << " default: break;\n";
318 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
319 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000320 default: break;
321 case CodeGenIntrinsic::NoMem:
322 case CodeGenIntrinsic::ReadArgMem:
323 case CodeGenIntrinsic::ReadMem:
324 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
325 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000326 }
327 }
328 OS << " return true; // These intrinsics have no side effects.\n";
329 OS << " }\n";
330 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000331}
Chris Lattner022f64f2006-03-13 23:08:44 +0000332
333void IntrinsicEmitter::
334EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
335 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
336 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
337 OS << " switch (F->getIntrinsicID()) {\n";
338 OS << " default: BuiltinName = \"\"; break;\n";
339 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
340 if (!Ints[i].GCCBuiltinName.empty()) {
341 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
342 << Ints[i].GCCBuiltinName << "\"; break;\n";
343 }
344 }
345 OS << " }\n";
346 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000347}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000348
349void IntrinsicEmitter::
350EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
351 std::ostream &OS) {
352 typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
353 BIMTy BuiltinMap;
354 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
355 if (!Ints[i].GCCBuiltinName.empty()) {
356 std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
357 Ints[i].TargetPrefix);
358 if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
359 throw "Intrinsic '" + Ints[i].TheDef->getName() +
360 "': duplicate GCC builtin name!";
361 }
362 }
363
364 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
365 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
366 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
367 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
368 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
369 OS << " if (0);\n";
370 // Note: this could emit significantly better code if we cared.
371 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
372 OS << " else if (";
373 if (!I->first.second.empty()) {
374 // Emit this as a strcmp, so it can be constant folded by the FE.
375 OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
376 << " ";
377 }
378 OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
Chris Lattnerad45b002006-03-15 02:05:38 +0000379 OS << " IntrinsicID = Intrinsic::" << I->second << ";\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000380 }
381 OS << " else\n";
382 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
383 OS << "#endif\n\n";
384}