blob: ab527521dc74d1cf29d6e712985c267fa6371d42 [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
41 // Emit mod/ref info for each function.
42 EmitModRefInfo(Ints, OS);
Chris Lattner4e5f3592006-03-09 22:37:52 +000043
Chris Lattner7056de32006-03-24 01:13:55 +000044 // Emit table of non-memory accessing intrinsics.
45 EmitNoMemoryInfo(Ints, OS);
46
47 // Emit side effect info for each intrinsic.
Chris Lattner4e5f3592006-03-09 22:37:52 +000048 EmitSideEffectInfo(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000049
50 // Emit a list of intrinsics with corresponding GCC builtins.
51 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000052
53 // Emit code to translate GCC builtins into LLVM intrinsics.
54 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +000055}
56
57void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
58 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000059 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000060 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
61 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
62 OS << " " << Ints[i].EnumName;
63 OS << ((i != e-1) ? ", " : " ");
64 OS << std::string(40-Ints[i].EnumName.size(), ' ')
65 << "// " << Ints[i].Name << "\n";
66 }
67 OS << "#endif\n\n";
68}
Chris Lattner9b843b22006-03-09 20:34:19 +000069
70void IntrinsicEmitter::
71EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
72 std::ostream &OS) {
73 // Build a function name -> intrinsic name mapping.
74 std::map<std::string, std::string> IntMapping;
75 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
76 IntMapping[Ints[i].Name] = Ints[i].EnumName;
77
78 OS << "// Function name -> enum value recognizer code.\n";
79 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
80 OS << " switch (Name[5]) {\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +000081 OS << " default: break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000082 // Emit the intrinsics in sorted order.
83 char LastChar = 0;
84 for (std::map<std::string, std::string>::iterator I = IntMapping.begin(),
85 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +000086 if (I->first[5] != LastChar) {
87 LastChar = I->first[5];
88 OS << " case '" << LastChar << "':\n";
89 }
90
91 OS << " if (Name == \"" << I->first << "\") return Intrinsic::"
92 << I->second << ";\n";
93 }
94 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +000095 OS << " // The 'llvm.' namespace is reserved!\n";
96 OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n";
97 OS << "#endif\n\n";
98}
99
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000100void IntrinsicEmitter::
101EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
102 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000103 OS << "// Intrinsic ID to name table\n";
104 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
105 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000106 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
107 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000108 OS << "#endif\n\n";
109}
110
Jim Laskey95d97b92007-02-06 18:30:58 +0000111static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) {
112 if (ArgType->getValueAsString("TypeVal") == "...") return true;
Jim Laskeyba4cc092007-02-06 18:02:54 +0000113
Chris Lattnerf124b462006-03-31 04:48:26 +0000114 OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", ";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000115 // If this is an integer type, check the width is correct.
116 if (ArgType->isSubClassOf("LLVMIntegerType"))
117 OS << ArgType->getValueAsInt("Width") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000118
119 // If this is a packed type, check that the subtype and size are correct.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000120 else if (ArgType->isSubClassOf("LLVMPackedType")) {
121 EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy"));
122 OS << ArgType->getValueAsInt("NumElts") << ", ";
Chris Lattner18faf5d2006-03-13 22:38:57 +0000123 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000124
125 return false;
Chris Lattner18faf5d2006-03-13 22:38:57 +0000126}
127
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000128/// RecordListComparator - Provide a determinstic comparator for lists of
129/// records.
130namespace {
131 struct RecordListComparator {
132 bool operator()(const std::vector<Record*> &LHS,
133 const std::vector<Record*> &RHS) const {
134 unsigned i = 0;
135 do {
136 if (i == RHS.size()) return false; // RHS is shorter than LHS.
137 if (LHS[i] != RHS[i])
138 return LHS[i]->getName() < RHS[i]->getName();
139 } while (++i != LHS.size());
140
141 return i != RHS.size();
142 }
143 };
144}
145
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000146void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
147 std::ostream &OS) {
148 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
149 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
150 OS << " switch (ID) {\n";
151 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000152
153 // This checking can emit a lot of very common code. To reduce the amount of
154 // code that we emit, batch up cases that have identical types. This avoids
155 // problems where GCC can run out of memory compiling Verifier.cpp.
156 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
157 RecordListComparator> MapTy;
158 MapTy UniqueArgInfos;
159
160 // Compute the unique argument type info.
161 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
162 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
163
164 // Loop through the array, emitting one comparison for each batch.
165 for (MapTy::iterator I = UniqueArgInfos.begin(),
166 E = UniqueArgInfos.end(); I != E; ++I) {
167 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
168 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
169 << Ints[I->second[i]].Name << "\n";
170 }
Chris Lattnerf124b462006-03-31 04:48:26 +0000171
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000172 const std::vector<Record*> &ArgTypes = I->first;
Chris Lattnerf124b462006-03-31 04:48:26 +0000173 OS << " VerifyIntrinsicPrototype(IF, ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000174 bool VarArg = false;
175 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
176 VarArg = EmitTypeVerify(OS, ArgTypes[j]);
177 if (VarArg) {
178 if ((j+1) != ArgTypes.size())
179 throw "Var arg type not last argument";
180 break;
181 }
182 }
183
184 OS << (VarArg ? "-2);\n" : "-1);\n");
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000185 OS << " break;\n";
186 }
187 OS << " }\n";
188 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000189}
190
Chris Lattner6448ee42006-03-09 22:30:49 +0000191void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
192 std::ostream &OS) {
193 OS << "// BasicAliasAnalysis code.\n";
194 OS << "#ifdef GET_MODREF_BEHAVIOR\n";
195 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
196 switch (Ints[i].ModRef) {
197 default: break;
198 case CodeGenIntrinsic::NoMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000199 OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000200 break;
201 case CodeGenIntrinsic::ReadArgMem:
202 case CodeGenIntrinsic::ReadMem:
Chris Lattner90aa8392006-10-04 21:52:35 +0000203 OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n";
Chris Lattner6448ee42006-03-09 22:30:49 +0000204 break;
205 }
206 }
207 OS << "#endif\n\n";
208}
Chris Lattner4e5f3592006-03-09 22:37:52 +0000209
210void IntrinsicEmitter::
Chris Lattner7056de32006-03-24 01:13:55 +0000211EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
212 OS << "// SelectionDAGIsel code.\n";
213 OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n";
214 OS << " switch (IntrinsicID) {\n";
215 OS << " default: break;\n";
216 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
217 switch (Ints[i].ModRef) {
218 default: break;
219 case CodeGenIntrinsic::NoMem:
220 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
221 break;
222 }
223 }
224 OS << " return true; // These intrinsics have no side effects.\n";
225 OS << " }\n";
226 OS << "#endif\n\n";
227}
228
229void IntrinsicEmitter::
Chris Lattner4e5f3592006-03-09 22:37:52 +0000230EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
Chris Lattner5348e392006-04-02 03:35:30 +0000231 OS << "// Return true if doesn't access or only reads memory.\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000232 OS << "#ifdef GET_SIDE_EFFECT_INFO\n";
Chris Lattner5348e392006-04-02 03:35:30 +0000233 OS << " switch (IntrinsicID) {\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000234 OS << " default: break;\n";
235 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
236 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000237 default: break;
238 case CodeGenIntrinsic::NoMem:
239 case CodeGenIntrinsic::ReadArgMem:
240 case CodeGenIntrinsic::ReadMem:
241 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
242 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000243 }
244 }
245 OS << " return true; // These intrinsics have no side effects.\n";
246 OS << " }\n";
247 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000248}
Chris Lattner022f64f2006-03-13 23:08:44 +0000249
250void IntrinsicEmitter::
251EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
252 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
253 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
254 OS << " switch (F->getIntrinsicID()) {\n";
255 OS << " default: BuiltinName = \"\"; break;\n";
256 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
257 if (!Ints[i].GCCBuiltinName.empty()) {
258 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
259 << Ints[i].GCCBuiltinName << "\"; break;\n";
260 }
261 }
262 OS << " }\n";
263 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000264}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000265
266void IntrinsicEmitter::
267EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
268 std::ostream &OS) {
269 typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy;
270 BIMTy BuiltinMap;
271 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
272 if (!Ints[i].GCCBuiltinName.empty()) {
273 std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName,
274 Ints[i].TargetPrefix);
275 if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second)
276 throw "Intrinsic '" + Ints[i].TheDef->getName() +
277 "': duplicate GCC builtin name!";
278 }
279 }
280
281 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
282 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
283 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
284 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
285 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
286 OS << " if (0);\n";
287 // Note: this could emit significantly better code if we cared.
288 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
289 OS << " else if (";
290 if (!I->first.second.empty()) {
291 // Emit this as a strcmp, so it can be constant folded by the FE.
292 OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n"
293 << " ";
294 }
295 OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n";
Chris Lattnerad45b002006-03-15 02:05:38 +0000296 OS << " IntrinsicID = Intrinsic::" << I->second << ";\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000297 }
298 OS << " else\n";
299 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
300 OS << "#endif\n\n";
301}