blob: 9247a85d22eede73f687081f6d063c74f7dd8082 [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
Duncan Sands83ec4b62008-06-06 12:08:01 +0000117static void EmitTypeForValueType(std::ostream &OS, MVT::SimpleValueType VT) {
118 if (MVT(VT).isInteger()) {
119 unsigned BitWidth = MVT(VT).getSizeInBits();
Chandler Carruth69940402007-08-04 01:51:18 +0000120 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
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000141static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
142 unsigned &ArgNo);
143
144static void EmitTypeGenerate(std::ostream &OS,
145 const std::vector<Record*> &ArgTypes,
146 unsigned &ArgNo) {
147 if (ArgTypes.size() == 1) {
148 EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
149 return;
150 }
151
152 OS << "StructType::get(";
153
154 for (std::vector<Record*>::const_iterator
155 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I)
156 EmitTypeGenerate(OS, *I, ArgNo);
157
158 OS << ", NULL)";
159}
160
161static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000162 unsigned &ArgNo) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000163 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000164
165 if (ArgType->isSubClassOf("LLVMMatchType")) {
166 unsigned Number = ArgType->getValueAsInt("Number");
167 assert(Number < ArgNo && "Invalid matching number!");
168 OS << "Tys[" << Number << "]";
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000169 } else if (VT == MVT::iAny || VT == MVT::fAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000170 // NOTE: The ArgNo variable here is not the absolute argument number, it is
171 // the index of the "arbitrary" type in the Tys array passed to the
172 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000173 // increment it when we actually hit an overloaded type. Getting this wrong
174 // leads to very subtle bugs!
175 OS << "Tys[" << ArgNo++ << "]";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000176 } else if (MVT(VT).isVector()) {
177 MVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000178 OS << "VectorType::get(";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000179 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT());
180 OS << ", " << VVT.getVectorNumElements() << ")";
Chandler Carruth69940402007-08-04 01:51:18 +0000181 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000182 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000183 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000184 OS << ")";
Mon P Wange3b3a722008-07-30 04:36:53 +0000185 } else if (VT == MVT::iPTRAny) {
186 // Make sure the user has passed us an argument type to overload. If not,
187 // treat it as an ordinary (not overloaded) intrinsic.
188 OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
189 << "] : PointerType::getUnqual(";
190 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
191 OS << ")";
192 ++ArgNo;
Chandler Carruth69940402007-08-04 01:51:18 +0000193 } else if (VT == MVT::isVoid) {
194 if (ArgNo == 0)
195 OS << "Type::VoidTy";
196 else
197 // MVT::isVoid is used to mean varargs here.
198 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000199 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000200 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000201 }
202}
203
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000204/// RecordListComparator - Provide a determinstic comparator for lists of
205/// records.
206namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000207 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000208 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000209 bool operator()(const RecPair &LHS,
210 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000211 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000212 const std::vector<Record*> *LHSVec = &LHS.first;
213 const std::vector<Record*> *RHSVec = &RHS.first;
214 unsigned RHSSize = RHSVec->size();
215 unsigned LHSSize = LHSVec->size();
216
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000217 do {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000218 if (i == RHSSize) return false; // RHS is shorter than LHS.
219 if ((*LHSVec)[i] != (*RHSVec)[i])
220 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
221 } while (++i != LHSSize);
222
223 if (i != RHSSize) return false;
224
225 i = 0;
226 LHSVec = &LHS.second;
227 RHSVec = &RHS.second;
228 RHSSize = RHSVec->size();
229 LHSSize = LHSVec->size();
230
231 for (i = 0; i != LHSSize; ++i) {
232 if (i == RHSSize) return false; // RHS is shorter than LHS.
233 if ((*LHSVec)[i] != (*RHSVec)[i])
234 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
235 }
236
237 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000238 }
239 };
240}
241
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000242void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
243 std::ostream &OS) {
244 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
245 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
246 OS << " switch (ID) {\n";
247 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000248
249 // This checking can emit a lot of very common code. To reduce the amount of
250 // code that we emit, batch up cases that have identical types. This avoids
251 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000252 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000253 MapTy UniqueArgInfos;
254
255 // Compute the unique argument type info.
256 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000257 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
258 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000259
260 // Loop through the array, emitting one comparison for each batch.
261 for (MapTy::iterator I = UniqueArgInfos.begin(),
262 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000263 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000264 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
265 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000266
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000267 const RecPair &ArgTypes = I->first;
268 const std::vector<Record*> &RetTys = ArgTypes.first;
269 const std::vector<Record*> &ParamTys = ArgTypes.second;
270
271 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
272 << ParamTys.size();
273
274 // Emit return types.
275 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
276 Record *ArgType = RetTys[j];
277 OS << ", ";
278
Chandler Carruth69940402007-08-04 01:51:18 +0000279 if (ArgType->isSubClassOf("LLVMMatchType")) {
280 unsigned Number = ArgType->getValueAsInt("Number");
281 assert(Number < j && "Invalid matching number!");
282 OS << "~" << Number;
283 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000284 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000285 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000286
287 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000288 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000289 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000290 }
291
292 // Emit the parameter types.
293 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
294 Record *ArgType = ParamTys[j];
295 OS << ", ";
296
297 if (ArgType->isSubClassOf("LLVMMatchType")) {
298 unsigned Number = ArgType->getValueAsInt("Number");
299 assert(Number < j + RetTys.size() && "Invalid matching number!");
300 OS << "~" << Number;
301 } else {
302 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
303 OS << getEnumName(VT);
304
305 if (VT == MVT::isVoid && j != 0 && j != je - 1)
306 throw "Var arg type not last argument";
307 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000308 }
309
Chandler Carruth69940402007-08-04 01:51:18 +0000310 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000311 OS << " break;\n";
312 }
313 OS << " }\n";
314 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000315}
316
Jim Laskey95af5922007-02-07 20:38:26 +0000317void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
318 std::ostream &OS) {
319 OS << "// Code for generating Intrinsic function declarations.\n";
320 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
321 OS << " switch (id) {\n";
322 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
323
324 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
325 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000326 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000327 MapTy UniqueArgInfos;
328
329 // Compute the unique argument type info.
330 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000331 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
332 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000333
334 // Loop through the array, emitting one generator for each batch.
335 for (MapTy::iterator I = UniqueArgInfos.begin(),
336 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000337 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Jim Laskey95af5922007-02-07 20:38:26 +0000338 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
339 << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000340
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000341 const RecPair &ArgTypes = I->first;
342 const std::vector<Record*> &RetTys = ArgTypes.first;
343 const std::vector<Record*> &ParamTys = ArgTypes.second;
344
345 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000346
Chandler Carruth69940402007-08-04 01:51:18 +0000347 if (N > 1 &&
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000348 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000349 OS << " IsVarArg = true;\n";
350 --N;
351 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000352
Reid Spencer84c614d2007-05-22 19:30:31 +0000353 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000354 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000355 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000356 OS << ";\n";
357
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000358 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000359 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000360 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000361 OS << ");\n";
362 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000363
Jim Laskey95af5922007-02-07 20:38:26 +0000364 OS << " break;\n";
365 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000366
Jim Laskey95af5922007-02-07 20:38:26 +0000367 OS << " }\n";
368 OS << "#endif\n\n";
369}
370
Chris Lattner4e5f3592006-03-09 22:37:52 +0000371void IntrinsicEmitter::
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000372EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
373 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
374 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
375 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000376 OS << " default: break;\n";
377 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
378 switch (Ints[i].ModRef) {
379 default: break;
380 case CodeGenIntrinsic::NoMem:
381 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
382 break;
383 }
384 }
Devang Patel05988662008-09-25 21:00:45 +0000385 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000386 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000387 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
388 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000389 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000390 case CodeGenIntrinsic::ReadArgMem:
391 case CodeGenIntrinsic::ReadMem:
392 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
393 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000394 }
395 }
Devang Patel05988662008-09-25 21:00:45 +0000396 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000397 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000398 OS << " }\n";
399 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000400}
Chris Lattner022f64f2006-03-13 23:08:44 +0000401
402void IntrinsicEmitter::
403EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
404 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
405 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
406 OS << " switch (F->getIntrinsicID()) {\n";
407 OS << " default: BuiltinName = \"\"; break;\n";
408 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
409 if (!Ints[i].GCCBuiltinName.empty()) {
410 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
411 << Ints[i].GCCBuiltinName << "\"; break;\n";
412 }
413 }
414 OS << " }\n";
415 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000416}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000417
Chris Lattner331bf922008-01-04 04:38:35 +0000418/// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
419/// sorted range of builtin names is equal to the current builtin. This breaks
420/// it down into a simple tree.
421///
422/// At this point, we know that all the builtins in the range have the same name
423/// for the first 'CharStart' characters. Only the end of the name needs to be
424/// discriminated.
425typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
426static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
427 unsigned CharStart, unsigned Indent,
428 std::ostream &OS) {
429 if (Start == End) return; // empty range.
430
431 // Determine what, if anything, is the same about all these strings.
432 std::string CommonString = Start->first;
433 unsigned NumInRange = 0;
434 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
435 // Find the first character that doesn't match.
436 const std::string &ThisStr = I->first;
437 unsigned NonMatchChar = CharStart;
438 while (NonMatchChar < CommonString.size() &&
439 NonMatchChar < ThisStr.size() &&
440 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
441 ++NonMatchChar;
442 // Truncate off pieces that don't match.
443 CommonString.resize(NonMatchChar);
444 }
445
446 // Just compare the rest of the string.
447 if (NumInRange == 1) {
448 if (CharStart != CommonString.size()) {
449 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
450 if (CharStart) OS << "+" << CharStart;
451 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
452 OS << CommonString.size() - CharStart << "))\n";
453 ++Indent;
454 }
455 OS << std::string(Indent*2, ' ') << "IntrinsicID = Intrinsic::";
456 OS << Start->second << ";\n";
457 return;
458 }
459
460 // At this point, we potentially have a common prefix for these builtins, emit
461 // a check for this common prefix.
462 if (CommonString.size() != CharStart) {
463 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
464 if (CharStart) OS << "+" << CharStart;
465 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
466 OS << CommonString.size()-CharStart << ")) {\n";
467
468 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1, OS);
469 OS << std::string(Indent*2, ' ') << "}\n";
470 return;
471 }
472
473 // Output a switch on the character that differs across the set.
474 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
475 << "]) {";
476 if (CharStart)
477 OS << " // \"" << std::string(Start->first.begin(),
478 Start->first.begin()+CharStart) << "\"";
479 OS << "\n";
480
481 for (StrMapIterator I = Start; I != End; ) {
482 char ThisChar = I->first[CharStart];
483 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
484 // Figure out the range that has this common character.
485 StrMapIterator NextChar = I;
486 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
487 ++NextChar)
488 /*empty*/;
489 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, OS);
490 OS << std::string(Indent*2, ' ') << " break;\n";
491 I = NextChar;
492 }
493 OS << std::string(Indent*2, ' ') << "}\n";
494}
495
496/// EmitTargetBuiltins - All of the builtins in the specified map are for the
497/// same target, and we already checked it.
498static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
499 std::ostream &OS) {
500 // Rearrange the builtins by length.
501 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
502 BuiltinsByLen.reserve(100);
503
504 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
505 if (I->first.size() >= BuiltinsByLen.size())
506 BuiltinsByLen.resize(I->first.size()+1);
507 BuiltinsByLen[I->first.size()].insert(*I);
508 }
509
510 // Now that we have all the builtins by their length, emit a switch stmt.
511 OS << " switch (strlen(BuiltinName)) {\n";
512 OS << " default: break;\n";
513 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
514 if (BuiltinsByLen[i].empty()) continue;
515 OS << " case " << i << ":\n";
516 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
517 0, 3, OS);
518 OS << " break;\n";
519 }
520 OS << " }\n";
521}
522
523
Chris Lattner3f8b8912006-03-15 01:33:26 +0000524void IntrinsicEmitter::
525EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
526 std::ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000527 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000528 BIMTy BuiltinMap;
529 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
530 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000531 // Get the map for this target prefix.
532 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
533
534 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
535 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000536 throw "Intrinsic '" + Ints[i].TheDef->getName() +
537 "': duplicate GCC builtin name!";
538 }
539 }
540
541 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
542 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
543 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
544 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
545 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Chris Lattner81442c02008-01-04 03:32:52 +0000546 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000547
Chris Lattner3f8b8912006-03-15 01:33:26 +0000548 // Note: this could emit significantly better code if we cared.
549 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000550 OS << " ";
551 if (!I->first.empty())
552 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
553 else
554 OS << "/* Target Independent Builtins */ ";
555 OS << "{\n";
556
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000557 // Emit the comparisons for this target prefix.
Chris Lattner331bf922008-01-04 04:38:35 +0000558 EmitTargetBuiltins(I->second, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000559 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000560 }
Chris Lattner3f8b8912006-03-15 01:33:26 +0000561 OS << "#endif\n\n";
562}