blob: bf8ab8e0b4b14ae3dae2fb8d89db6b79d05858a6 [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9e493cf2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth69940402007-08-04 01:51:18 +000014#include "CodeGenTarget.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000015#include "IntrinsicEmitter.h"
16#include "Record.h"
Chris Lattner18faf5d2006-03-13 22:38:57 +000017#include "llvm/ADT/StringExtras.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000018#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000019using namespace llvm;
20
21//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000022// IntrinsicEmitter Implementation
23//===----------------------------------------------------------------------===//
24
25void IntrinsicEmitter::run(std::ostream &OS) {
26 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
27
28 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records);
29
30 // Emit the enum information.
31 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000032
33 // Emit the intrinsic ID -> name table.
34 EmitIntrinsicToNameTable(Ints, OS);
Chris Lattner9b843b22006-03-09 20:34:19 +000035
36 // Emit the function name recognizer.
37 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000038
Chris Lattnerf97a00e2006-03-09 22:05:04 +000039 // Emit the intrinsic verifier.
40 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000041
Jim Laskey95af5922007-02-07 20:38:26 +000042 // Emit the intrinsic declaration generator.
43 EmitGenerator(Ints, OS);
44
Duncan Sandsa3355ff2007-12-03 20:06:50 +000045 // Emit the intrinsic parameter attributes.
46 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000047
48 // Emit a list of intrinsics with corresponding GCC builtins.
49 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000050
51 // Emit code to translate GCC builtins into LLVM intrinsics.
52 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +000053}
54
55void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
56 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000057 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000058 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
59 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
60 OS << " " << Ints[i].EnumName;
61 OS << ((i != e-1) ? ", " : " ");
62 OS << std::string(40-Ints[i].EnumName.size(), ' ')
63 << "// " << Ints[i].Name << "\n";
64 }
65 OS << "#endif\n\n";
66}
Chris Lattner9b843b22006-03-09 20:34:19 +000067
68void IntrinsicEmitter::
69EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
70 std::ostream &OS) {
71 // Build a function name -> intrinsic name mapping.
Reid Spencerc4de3de2007-04-01 07:20:02 +000072 std::map<std::string, unsigned> IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +000073 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Reid Spencerc4de3de2007-04-01 07:20:02 +000074 IntMapping[Ints[i].Name] = i;
Chris Lattner9b843b22006-03-09 20:34:19 +000075
76 OS << "// Function name -> enum value recognizer code.\n";
77 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
78 OS << " switch (Name[5]) {\n";
Chris Lattner3b515802007-02-15 19:17:16 +000079 OS << " default:\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000080 // Emit the intrinsics in sorted order.
81 char LastChar = 0;
Reid Spencerc4de3de2007-04-01 07:20:02 +000082 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +000083 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +000084 if (I->first[5] != LastChar) {
85 LastChar = I->first[5];
Chris Lattner3b515802007-02-15 19:17:16 +000086 OS << " break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000087 OS << " case '" << LastChar << "':\n";
88 }
89
Reid Spencerc4de3de2007-04-01 07:20:02 +000090 // For overloaded intrinsics, only the prefix needs to match
91 if (Ints[I->second].isOverloaded)
Chandler Carruth69940402007-08-04 01:51:18 +000092 OS << " if (Len > " << I->first.size()
93 << " && !memcmp(Name, \"" << I->first << ".\", "
94 << (I->first.size() + 1) << ")) return Intrinsic::"
95 << Ints[I->second].EnumName << ";\n";
Reid Spencerc4de3de2007-04-01 07:20:02 +000096 else
97 OS << " if (Len == " << I->first.size()
Chandler Carruth69940402007-08-04 01:51:18 +000098 << " && !memcmp(Name, \"" << I->first << "\", "
99 << I->first.size() << ")) return Intrinsic::"
Reid Spencerc4de3de2007-04-01 07:20:02 +0000100 << Ints[I->second].EnumName << ";\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000101 }
102 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000103 OS << "#endif\n\n";
104}
105
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000106void IntrinsicEmitter::
107EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
108 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000109 OS << "// Intrinsic ID to name table\n";
110 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
111 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000112 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
113 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000114 OS << "#endif\n\n";
115}
116
Chandler Carruth69940402007-08-04 01:51:18 +0000117static void EmitTypeForValueType(std::ostream &OS, MVT::ValueType VT) {
118 if (MVT::isInteger(VT)) {
119 unsigned BitWidth = MVT::getSizeInBits(VT);
120 OS << "IntegerType::get(" << BitWidth << ")";
121 } else if (VT == MVT::Other) {
122 // MVT::OtherVT is used to mean the empty struct type here.
123 OS << "StructType::get(std::vector<const Type *>())";
124 } else if (VT == MVT::f32) {
125 OS << "Type::FloatTy";
126 } else if (VT == MVT::f64) {
127 OS << "Type::DoubleTy";
Dale Johannesen317096a2007-09-28 01:08:20 +0000128 } else if (VT == MVT::f80) {
129 OS << "Type::X86_FP80Ty";
130 } else if (VT == MVT::f128) {
131 OS << "Type::FP128Ty";
132 } else if (VT == MVT::ppcf128) {
133 OS << "Type::PPC_FP128Ty";
Chandler Carruth69940402007-08-04 01:51:18 +0000134 } else if (VT == MVT::isVoid) {
135 OS << "Type::VoidTy";
136 } else {
137 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000138 }
139}
140
Reid Spencer84c614d2007-05-22 19:30:31 +0000141static void EmitTypeGenerate(std::ostream &OS, Record *ArgType,
142 unsigned &ArgNo) {
Chandler Carruth69940402007-08-04 01:51:18 +0000143 MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT"));
144
145 if (ArgType->isSubClassOf("LLVMMatchType")) {
146 unsigned Number = ArgType->getValueAsInt("Number");
147 assert(Number < ArgNo && "Invalid matching number!");
148 OS << "Tys[" << Number << "]";
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000149 } else if (VT == MVT::iAny || VT == MVT::fAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000150 // NOTE: The ArgNo variable here is not the absolute argument number, it is
151 // the index of the "arbitrary" type in the Tys array passed to the
152 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000153 // increment it when we actually hit an overloaded type. Getting this wrong
154 // leads to very subtle bugs!
155 OS << "Tys[" << ArgNo++ << "]";
156 } else if (MVT::isVector(VT)) {
Reid Spencer9d6565a2007-02-15 02:26:10 +0000157 OS << "VectorType::get(";
Chandler Carruth69940402007-08-04 01:51:18 +0000158 EmitTypeForValueType(OS, MVT::getVectorElementType(VT));
159 OS << ", " << MVT::getVectorNumElements(VT) << ")";
160 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000161 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000162 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000163 OS << ")";
Chandler Carruth69940402007-08-04 01:51:18 +0000164 } else if (VT == MVT::isVoid) {
165 if (ArgNo == 0)
166 OS << "Type::VoidTy";
167 else
168 // MVT::isVoid is used to mean varargs here.
169 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000170 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000171 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000172 }
173}
174
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000175/// RecordListComparator - Provide a determinstic comparator for lists of
176/// records.
177namespace {
178 struct RecordListComparator {
179 bool operator()(const std::vector<Record*> &LHS,
180 const std::vector<Record*> &RHS) const {
181 unsigned i = 0;
182 do {
183 if (i == RHS.size()) return false; // RHS is shorter than LHS.
184 if (LHS[i] != RHS[i])
185 return LHS[i]->getName() < RHS[i]->getName();
186 } while (++i != LHS.size());
187
188 return i != RHS.size();
189 }
190 };
191}
192
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000193void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
194 std::ostream &OS) {
195 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
196 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
197 OS << " switch (ID) {\n";
198 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000199
200 // This checking can emit a lot of very common code. To reduce the amount of
201 // code that we emit, batch up cases that have identical types. This avoids
202 // problems where GCC can run out of memory compiling Verifier.cpp.
203 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
204 RecordListComparator> MapTy;
205 MapTy UniqueArgInfos;
206
207 // Compute the unique argument type info.
208 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
209 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
210
211 // Loop through the array, emitting one comparison for each batch.
212 for (MapTy::iterator I = UniqueArgInfos.begin(),
213 E = UniqueArgInfos.end(); I != E; ++I) {
214 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
215 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
216 << Ints[I->second[i]].Name << "\n";
217 }
Chris Lattnerf124b462006-03-31 04:48:26 +0000218
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000219 const std::vector<Record*> &ArgTypes = I->first;
Chandler Carruth69940402007-08-04 01:51:18 +0000220 OS << " VerifyIntrinsicPrototype(ID, IF, " << ArgTypes.size() << ", ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000221 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
Chandler Carruth69940402007-08-04 01:51:18 +0000222 Record *ArgType = ArgTypes[j];
223 if (ArgType->isSubClassOf("LLVMMatchType")) {
224 unsigned Number = ArgType->getValueAsInt("Number");
225 assert(Number < j && "Invalid matching number!");
226 OS << "~" << Number;
227 } else {
228 MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT"));
229 OS << getEnumName(VT);
230 if (VT == MVT::isVoid && j != 0 && j != ArgTypes.size()-1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000231 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000232 }
Chandler Carruth69940402007-08-04 01:51:18 +0000233 if (j != ArgTypes.size()-1)
234 OS << ", ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000235 }
236
Chandler Carruth69940402007-08-04 01:51:18 +0000237 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000238 OS << " break;\n";
239 }
240 OS << " }\n";
241 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000242}
243
Jim Laskey95af5922007-02-07 20:38:26 +0000244void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
245 std::ostream &OS) {
246 OS << "// Code for generating Intrinsic function declarations.\n";
247 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
248 OS << " switch (id) {\n";
249 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
250
251 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
252 // types.
253 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
254 RecordListComparator> MapTy;
255 MapTy UniqueArgInfos;
256
257 // Compute the unique argument type info.
258 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
259 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
260
261 // Loop through the array, emitting one generator for each batch.
262 for (MapTy::iterator I = UniqueArgInfos.begin(),
263 E = UniqueArgInfos.end(); I != E; ++I) {
264 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
265 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
266 << Ints[I->second[i]].Name << "\n";
267 }
268
269 const std::vector<Record*> &ArgTypes = I->first;
270 unsigned N = ArgTypes.size();
271
Chandler Carruth69940402007-08-04 01:51:18 +0000272 if (N > 1 &&
273 getValueType(ArgTypes[N-1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000274 OS << " IsVarArg = true;\n";
275 --N;
276 }
277
Reid Spencer84c614d2007-05-22 19:30:31 +0000278 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000279 OS << " ResultTy = ";
Reid Spencer84c614d2007-05-22 19:30:31 +0000280 EmitTypeGenerate(OS, ArgTypes[0], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000281 OS << ";\n";
282
283 for (unsigned j = 1; j != N; ++j) {
284 OS << " ArgTys.push_back(";
Reid Spencer84c614d2007-05-22 19:30:31 +0000285 EmitTypeGenerate(OS, ArgTypes[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000286 OS << ");\n";
287 }
Jim Laskey95af5922007-02-07 20:38:26 +0000288 OS << " break;\n";
289 }
290 OS << " }\n";
291 OS << "#endif\n\n";
292}
293
Chris Lattner4e5f3592006-03-09 22:37:52 +0000294void IntrinsicEmitter::
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000295EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
296 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
297 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
298 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000299 OS << " default: break;\n";
300 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
301 switch (Ints[i].ModRef) {
302 default: break;
303 case CodeGenIntrinsic::NoMem:
304 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
305 break;
306 }
307 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000308 OS << " Attr |= ParamAttr::ReadNone; // These do not access memory.\n";
309 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000310 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
311 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000312 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000313 case CodeGenIntrinsic::ReadArgMem:
314 case CodeGenIntrinsic::ReadMem:
315 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
316 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000317 }
318 }
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000319 OS << " Attr |= ParamAttr::ReadOnly; // These do not write memory.\n";
320 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000321 OS << " }\n";
322 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000323}
Chris Lattner022f64f2006-03-13 23:08:44 +0000324
325void IntrinsicEmitter::
326EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
327 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
328 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
329 OS << " switch (F->getIntrinsicID()) {\n";
330 OS << " default: BuiltinName = \"\"; break;\n";
331 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
332 if (!Ints[i].GCCBuiltinName.empty()) {
333 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
334 << Ints[i].GCCBuiltinName << "\"; break;\n";
335 }
336 }
337 OS << " }\n";
338 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000339}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000340
341void IntrinsicEmitter::
342EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
343 std::ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000344 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000345 BIMTy BuiltinMap;
346 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
347 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000348 // Get the map for this target prefix.
349 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
350
351 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
352 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000353 throw "Intrinsic '" + Ints[i].TheDef->getName() +
354 "': duplicate GCC builtin name!";
355 }
356 }
357
358 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
359 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
360 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
361 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
362 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000363
Chris Lattner3f8b8912006-03-15 01:33:26 +0000364 // Note: this could emit significantly better code if we cared.
365 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000366 OS << " ";
367 if (!I->first.empty())
368 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
369 else
370 OS << "/* Target Independent Builtins */ ";
371 OS << "{\n";
372
373 OS << " if (0);\n";
374
375 // Emit the comparisons for this target prefix.
376 std::map<std::string, std::string> &BIM = I->second;
377 for (std::map<std::string, std::string>::iterator J = BIM.begin(),
378 E = BIM.end(); J != E; ++J) {
379 OS << " else if (!strcmp(BuiltinName, \"" << J->first << "\"))\n";
380 OS << " IntrinsicID = Intrinsic::" << J->second << ";\n";
381 }
382 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000383 }
384 OS << " else\n";
385 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
386 OS << "#endif\n\n";
387}