blob: c44ae5e4caa733f9add95ebd3eef2f6b62f858f6 [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 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;
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];
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
Chris Lattner3b515802007-02-15 19:17:16 +000095 OS << " if (Len == " << I->first.size()
96 << " && !strcmp(Name, \"" << I->first << "\")) return Intrinsic::"
Chris Lattner9b843b22006-03-09 20:34:19 +000097 << I->second << ";\n";
98 }
99 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000100 OS << " // The 'llvm.' namespace is reserved!\n";
101 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
102 OS << "#endif\n\n";
103}
104
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000105void IntrinsicEmitter::
106EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
107 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000108 OS << "// Intrinsic ID to name table\n";
109 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
110 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000111 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
112 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000113 OS << "#endif\n\n";
114}
115
Jim Laskey95d97b92007-02-06 18:30:58 +0000116static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) {
117 if (ArgType->getValueAsString("TypeVal") == "...") return true;
Jim Laskeyba4cc092007-02-06 18:02:54 +0000118
Chris Lattnerf124b462006-03-31 04:48:26 +0000119 OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000120 // If this is an integer type, check the width is correct.
121 if (ArgType->isSubClassOf("LLVMIntegerType"))
122 OS << ArgType->getValueAsInt("Width") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000123
Reid Spencerac9dcb92007-02-15 03:39:18 +0000124 // If this is a vector type, check that the subtype and size are correct.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000125 else if (ArgType->isSubClassOf("LLVMVectorType")) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000126 EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
127 OS << ArgType->getValueAsInt("NumElts") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000128 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000129
130 return false;
Chris Lattner18faf5d2006-03-13 22:38:57 +0000131}
132
Jim Laskey95af5922007-02-07 20:38:26 +0000133static void EmitTypeGenerate(std::ostream &OS, Record *ArgType) {
134 if (ArgType->isSubClassOf("LLVMIntegerType")) {
135 OS << "IntegerType::get(" << ArgType->getValueAsInt("Width") << ")";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000136 } else if (ArgType->isSubClassOf("LLVMVectorType")) {
137 OS << "VectorType::get(";
Jim Laskey95af5922007-02-07 20:38:26 +0000138 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"));
139 OS << ", " << ArgType->getValueAsInt("NumElts") << ")";
140 } else if (ArgType->isSubClassOf("LLVMPointerType")) {
141 OS << "PointerType::get(";
142 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"));
143 OS << ")";
144 } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) {
145 OS << "StructType::get(std::vector<const Type *>())";
146 } else {
147 OS << "Type::getPrimitiveType(";
148 OS << ArgType->getValueAsString("TypeVal") << ")";
149 }
150}
151
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000152/// RecordListComparator - Provide a determinstic comparator for lists of
153/// records.
154namespace {
155 struct RecordListComparator {
156 bool operator()(const std::vector<Record*> &LHS,
157 const std::vector<Record*> &RHS) const {
158 unsigned i = 0;
159 do {
160 if (i == RHS.size()) return false; // RHS is shorter than LHS.
161 if (LHS[i] != RHS[i])
162 return LHS[i]->getName() < RHS[i]->getName();
163 } while (++i != LHS.size());
164
165 return i != RHS.size();
166 }
167 };
168}
169
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000170void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
171 std::ostream &OS) {
172 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
173 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
174 OS << " switch (ID) {\n";
175 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000176
177 // This checking can emit a lot of very common code. To reduce the amount of
178 // code that we emit, batch up cases that have identical types. This avoids
179 // problems where GCC can run out of memory compiling Verifier.cpp.
180 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
181 RecordListComparator> MapTy;
182 MapTy UniqueArgInfos;
183
184 // Compute the unique argument type info.
185 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
186 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
187
188 // Loop through the array, emitting one comparison for each batch.
189 for (MapTy::iterator I = UniqueArgInfos.begin(),
190 E = UniqueArgInfos.end(); I != E; ++I) {
191 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
192 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
193 << Ints[I->second[i]].Name << "\n";
194 }
Chris Lattnerf124b462006-03-31 04:48:26 +0000195
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000196 const std::vector<Record*> &ArgTypes = I->first;
Chris Lattnerf124b462006-03-31 04:48:26 +0000197 OS << " VerifyIntrinsicPrototype(IF, ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000198 bool VarArg = false;
199 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
200 VarArg = EmitTypeVerify(OS, ArgTypes[j]);
201 if (VarArg) {
202 if ((j+1) != ArgTypes.size())
203 throw "Var arg type not last argument";
204 break;
205 }
206 }
207
208 OS << (VarArg ? "-2);\n" : "-1);\n");
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000209 OS << " break;\n";
210 }
211 OS << " }\n";
212 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000213}
214
Jim Laskey95af5922007-02-07 20:38:26 +0000215void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
216 std::ostream &OS) {
217 OS << "// Code for generating Intrinsic function declarations.\n";
218 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
219 OS << " switch (id) {\n";
220 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
221
222 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
223 // types.
224 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
225 RecordListComparator> MapTy;
226 MapTy UniqueArgInfos;
227
228 // Compute the unique argument type info.
229 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
230 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
231
232 // Loop through the array, emitting one generator for each batch.
233 for (MapTy::iterator I = UniqueArgInfos.begin(),
234 E = UniqueArgInfos.end(); I != E; ++I) {
235 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
236 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
237 << Ints[I->second[i]].Name << "\n";
238 }
239
240 const std::vector<Record*> &ArgTypes = I->first;
241 unsigned N = ArgTypes.size();
242
243 if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") {
244 OS << " IsVarArg = true;\n";
245 --N;
246 }
247
248 OS << " ResultTy = ";
249 EmitTypeGenerate(OS, ArgTypes[0]);
250 OS << ";\n";
251
252 for (unsigned j = 1; j != N; ++j) {
253 OS << " ArgTys.push_back(";
254 EmitTypeGenerate(OS, ArgTypes[j]);
255 OS << ");\n";
256 }
257
258 OS << " break;\n";
259 }
260 OS << " }\n";
261 OS << "#endif\n\n";
262}
263
Chris Lattner6448ee42006-03-09 22:30:49 +0000264void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
265 std::ostream &OS) {
266 OS << "// BasicAliasAnalysis code.\n";
267 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
268 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
269 switch (Ints[i].ModRef) {
270 default: break;
271 case CodeGenIntrinsic::NoMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000272 OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000273 break;
274 case CodeGenIntrinsic::ReadArgMem:
275 case CodeGenIntrinsic::ReadMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000276 OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000277 break;
278 }
279 }
280 OS << "#endif\n\n";
281}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000282
283void IntrinsicEmitter::
Chris Lattner7056de32006-03-24 01:13:55 +0000284EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
285 OS << "// SelectionDAGIsel code.\n";
286 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
287 OS << " switch (IntrinsicID) {\n";
288 OS << " default: break;\n";
289 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
290 switch (Ints[i].ModRef) {
291 default: break;
292 case CodeGenIntrinsic::NoMem:
293 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
294 break;
295 }
296 }
297 OS << " return true; // These intrinsics have no side effects.\n";
298 OS << " }\n";
299 OS << "#endif\n\n";
300}
301
302void IntrinsicEmitter::
Chris Lattner4e5f3592006-03-09 22:37:52 +0000303EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
Chris Lattner5348e392006-04-02 03:35:30 +0000304 OS << "// Return true if doesn't access or only reads memory.\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000305 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
Chris Lattner5348e392006-04-02 03:35:30 +0000306 OS << " switch (IntrinsicID) {\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000307 OS << " default: break;\n";
308 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
309 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000310 default: break;
311 case CodeGenIntrinsic::NoMem:
312 case CodeGenIntrinsic::ReadArgMem:
313 case CodeGenIntrinsic::ReadMem:
314 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
315 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000316 }
317 }
318 OS << " return true; // These intrinsics have no side effects.\n";
319 OS << " }\n";
320 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000321}
Chris Lattner022f64f2006-03-13 23:08:44 +0000322
323void IntrinsicEmitter::
324EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
325 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
326 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
327 OS << " switch (F->getIntrinsicID()) {\n";
328 OS << " default: BuiltinName = \"\"; break;\n";
329 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
330 if (!Ints[i].GCCBuiltinName.empty()) {
331 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
332 << Ints[i].GCCBuiltinName << "\"; break;\n";
333 }
334 }
335 OS << " }\n";
336 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000337}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000338
339void IntrinsicEmitter::
340EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
341 std::ostream &OS) {
342 typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
343 BIMTy BuiltinMap;
344 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
345 if (!Ints[i].GCCBuiltinName.empty()) {
346 std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
347 Ints[i].TargetPrefix);
348 if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
349 throw "Intrinsic '" + Ints[i].TheDef->getName() +
350 "': duplicate GCC builtin name!";
351 }
352 }
353
354 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
355 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
356 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
357 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
358 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
359 OS << " if (0);\n";
360 // Note: this could emit significantly better code if we cared.
361 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
362 OS << " else if (";
363 if (!I->first.second.empty()) {
364 // Emit this as a strcmp, so it can be constant folded by the FE.
365 OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
366 << " ";
367 }
368 OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
Chris Lattnerad45b002006-03-15 02:05:38 +0000369 OS << " IntrinsicID = Intrinsic::" << I->second << ";\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000370 }
371 OS << " else\n";
372 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
373 OS << "#endif\n\n";
374}