blob: faa750daee220c6c5eba140d63393615ce47322d [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.
77 std::map<std::string, std::string> IntMapping;
78 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
79 IntMapping[Ints[i].Name] = Ints[i].EnumName;
80
81 OS << "// Function name -> enum value recognizer code.\n";
82 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
83 OS << " switch (Name[5]) {\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +000084 OS << " default: break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000085 // Emit the intrinsics in sorted order.
86 char LastChar = 0;
87 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
88 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +000089 if (I->first[5] != LastChar) {
90 LastChar = I->first[5];
91 OS << " case '" << LastChar << "':\n";
92 }
93
94 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
95 << I->second << ";\n";
96 }
97 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +000098 OS << " // The 'llvm.' namespace is reserved!\n";
99 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
100 OS << "#endif\n\n";
101}
102
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000103void IntrinsicEmitter::
104EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
105 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000106 OS << "// Intrinsic ID to name table\n";
107 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
108 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000109 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
110 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000111 OS << "#endif\n\n";
112}
113
Jim Laskey95d97b92007-02-06 18:30:58 +0000114static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) {
115 if (ArgType->getValueAsString("TypeVal") == "...") return true;
Jim Laskeyba4cc092007-02-06 18:02:54 +0000116
Chris Lattnerf124b462006-03-31 04:48:26 +0000117 OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000118 // If this is an integer type, check the width is correct.
119 if (ArgType->isSubClassOf("LLVMIntegerType"))
120 OS << ArgType->getValueAsInt("Width") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000121
Reid Spencerac9dcb92007-02-15 03:39:18 +0000122 // If this is a vector type, check that the subtype and size are correct.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000123 else if (ArgType->isSubClassOf("LLVMVectorType")) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000124 EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
125 OS << ArgType->getValueAsInt("NumElts") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000126 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000127
128 return false;
Chris Lattner18faf5d2006-03-13 22:38:57 +0000129}
130
Jim Laskey95af5922007-02-07 20:38:26 +0000131static void EmitTypeGenerate(std::ostream &OS, Record *ArgType) {
132 if (ArgType->isSubClassOf("LLVMIntegerType")) {
133 OS << "IntegerType::get(" << ArgType->getValueAsInt("Width") << ")";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000134 } else if (ArgType->isSubClassOf("LLVMVectorType")) {
135 OS << "VectorType::get(";
Jim Laskey95af5922007-02-07 20:38:26 +0000136 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"));
137 OS << ", " << ArgType->getValueAsInt("NumElts") << ")";
138 } else if (ArgType->isSubClassOf("LLVMPointerType")) {
139 OS << "PointerType::get(";
140 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"));
141 OS << ")";
142 } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) {
143 OS << "StructType::get(std::vector<const Type *>())";
144 } else {
145 OS << "Type::getPrimitiveType(";
146 OS << ArgType->getValueAsString("TypeVal") << ")";
147 }
148}
149
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000150/// RecordListComparator - Provide a determinstic comparator for lists of
151/// records.
152namespace {
153 struct RecordListComparator {
154 bool operator()(const std::vector<Record*> &LHS,
155 const std::vector<Record*> &RHS) const {
156 unsigned i = 0;
157 do {
158 if (i == RHS.size()) return false; // RHS is shorter than LHS.
159 if (LHS[i] != RHS[i])
160 return LHS[i]->getName() < RHS[i]->getName();
161 } while (++i != LHS.size());
162
163 return i != RHS.size();
164 }
165 };
166}
167
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000168void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
169 std::ostream &OS) {
170 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
171 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
172 OS << " switch (ID) {\n";
173 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000174
175 // This checking can emit a lot of very common code. To reduce the amount of
176 // code that we emit, batch up cases that have identical types. This avoids
177 // problems where GCC can run out of memory compiling Verifier.cpp.
178 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
179 RecordListComparator> MapTy;
180 MapTy UniqueArgInfos;
181
182 // Compute the unique argument type info.
183 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
184 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
185
186 // Loop through the array, emitting one comparison for each batch.
187 for (MapTy::iterator I = UniqueArgInfos.begin(),
188 E = UniqueArgInfos.end(); I != E; ++I) {
189 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
190 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
191 << Ints[I->second[i]].Name << "\n";
192 }
Chris Lattnerf124b462006-03-31 04:48:26 +0000193
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000194 const std::vector<Record*> &ArgTypes = I->first;
Chris Lattnerf124b462006-03-31 04:48:26 +0000195 OS << " VerifyIntrinsicPrototype(IF, ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000196 bool VarArg = false;
197 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
198 VarArg = EmitTypeVerify(OS, ArgTypes[j]);
199 if (VarArg) {
200 if ((j+1) != ArgTypes.size())
201 throw "Var arg type not last argument";
202 break;
203 }
204 }
205
206 OS << (VarArg ? "-2);\n" : "-1);\n");
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000207 OS << " break;\n";
208 }
209 OS << " }\n";
210 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000211}
212
Jim Laskey95af5922007-02-07 20:38:26 +0000213void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
214 std::ostream &OS) {
215 OS << "// Code for generating Intrinsic function declarations.\n";
216 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
217 OS << " switch (id) {\n";
218 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
219
220 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
221 // types.
222 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
223 RecordListComparator> MapTy;
224 MapTy UniqueArgInfos;
225
226 // Compute the unique argument type info.
227 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
228 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
229
230 // Loop through the array, emitting one generator for each batch.
231 for (MapTy::iterator I = UniqueArgInfos.begin(),
232 E = UniqueArgInfos.end(); I != E; ++I) {
233 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
234 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
235 << Ints[I->second[i]].Name << "\n";
236 }
237
238 const std::vector<Record*> &ArgTypes = I->first;
239 unsigned N = ArgTypes.size();
240
241 if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") {
242 OS << " IsVarArg = true;\n";
243 --N;
244 }
245
246 OS << " ResultTy = ";
247 EmitTypeGenerate(OS, ArgTypes[0]);
248 OS << ";\n";
249
250 for (unsigned j = 1; j != N; ++j) {
251 OS << " ArgTys.push_back(";
252 EmitTypeGenerate(OS, ArgTypes[j]);
253 OS << ");\n";
254 }
255
256 OS << " break;\n";
257 }
258 OS << " }\n";
259 OS << "#endif\n\n";
260}
261
Chris Lattner6448ee42006-03-09 22:30:49 +0000262void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
263 std::ostream &OS) {
264 OS << "// BasicAliasAnalysis code.\n";
265 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
266 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
267 switch (Ints[i].ModRef) {
268 default: break;
269 case CodeGenIntrinsic::NoMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000270 OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000271 break;
272 case CodeGenIntrinsic::ReadArgMem:
273 case CodeGenIntrinsic::ReadMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000274 OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000275 break;
276 }
277 }
278 OS << "#endif\n\n";
279}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000280
281void IntrinsicEmitter::
Chris Lattner7056de32006-03-24 01:13:55 +0000282EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
283 OS << "// SelectionDAGIsel code.\n";
284 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
285 OS << " switch (IntrinsicID) {\n";
286 OS << " default: break;\n";
287 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
288 switch (Ints[i].ModRef) {
289 default: break;
290 case CodeGenIntrinsic::NoMem:
291 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
292 break;
293 }
294 }
295 OS << " return true; // These intrinsics have no side effects.\n";
296 OS << " }\n";
297 OS << "#endif\n\n";
298}
299
300void IntrinsicEmitter::
Chris Lattner4e5f3592006-03-09 22:37:52 +0000301EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
Chris Lattner5348e392006-04-02 03:35:30 +0000302 OS << "// Return true if doesn't access or only reads memory.\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000303 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
Chris Lattner5348e392006-04-02 03:35:30 +0000304 OS << " switch (IntrinsicID) {\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000305 OS << " default: break;\n";
306 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
307 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000308 default: break;
309 case CodeGenIntrinsic::NoMem:
310 case CodeGenIntrinsic::ReadArgMem:
311 case CodeGenIntrinsic::ReadMem:
312 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
313 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000314 }
315 }
316 OS << " return true; // These intrinsics have no side effects.\n";
317 OS << " }\n";
318 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000319}
Chris Lattner022f64f2006-03-13 23:08:44 +0000320
321void IntrinsicEmitter::
322EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
323 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
324 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
325 OS << " switch (F->getIntrinsicID()) {\n";
326 OS << " default: BuiltinName = \"\"; break;\n";
327 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
328 if (!Ints[i].GCCBuiltinName.empty()) {
329 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
330 << Ints[i].GCCBuiltinName << "\"; break;\n";
331 }
332 }
333 OS << " }\n";
334 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000335}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000336
337void IntrinsicEmitter::
338EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
339 std::ostream &OS) {
340 typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
341 BIMTy BuiltinMap;
342 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
343 if (!Ints[i].GCCBuiltinName.empty()) {
344 std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
345 Ints[i].TargetPrefix);
346 if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
347 throw "Intrinsic '" + Ints[i].TheDef->getName() +
348 "': duplicate GCC builtin name!";
349 }
350 }
351
352 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
353 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
354 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
355 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
356 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
357 OS << " if (0);\n";
358 // Note: this could emit significantly better code if we cared.
359 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
360 OS << " else if (";
361 if (!I->first.second.empty()) {
362 // Emit this as a strcmp, so it can be constant folded by the FE.
363 OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
364 << " ";
365 }
366 OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
Chris Lattnerad45b002006-03-15 02:05:38 +0000367 OS << " IntrinsicID = Intrinsic::" << I->second << ";\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000368 }
369 OS << " else\n";
370 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
371 OS << "#endif\n\n";
372}