blob: 8800a778164d1d7c80a55a1713cc4f5c2ad7cc02 [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
Dale Johannesen49de9822009-02-05 01:49:45 +000028 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
29
30 if (TargetOnly && !Ints.empty())
31 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattner9e493cf2006-03-03 02:32:46 +000032
33 // Emit the enum information.
34 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000035
36 // Emit the intrinsic ID -> name table.
37 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000038
39 // Emit the intrinsic ID -> overload table.
40 EmitIntrinsicToOverloadTable(Ints, OS);
41
Chris Lattner9b843b22006-03-09 20:34:19 +000042 // Emit the function name recognizer.
43 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000044
Chris Lattnerf97a00e2006-03-09 22:05:04 +000045 // Emit the intrinsic verifier.
46 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000047
Jim Laskey95af5922007-02-07 20:38:26 +000048 // Emit the intrinsic declaration generator.
49 EmitGenerator(Ints, OS);
50
Duncan Sandsa3355ff2007-12-03 20:06:50 +000051 // Emit the intrinsic parameter attributes.
52 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000053
Duncan Sandsd869b382009-02-14 10:56:35 +000054 // Emit intrinsic alias analysis mod/ref behavior.
55 EmitModRefBehavior(Ints, OS);
56
Chris Lattner022f64f2006-03-13 23:08:44 +000057 // Emit a list of intrinsics with corresponding GCC builtins.
58 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000059
60 // Emit code to translate GCC builtins into LLVM intrinsics.
61 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +000062}
63
64void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
65 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000066 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000067 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
68 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
69 OS << " " << Ints[i].EnumName;
70 OS << ((i != e-1) ? ", " : " ");
71 OS << std::string(40-Ints[i].EnumName.size(), ' ')
72 << "// " << Ints[i].Name << "\n";
73 }
74 OS << "#endif\n\n";
75}
Chris Lattner9b843b22006-03-09 20:34:19 +000076
77void IntrinsicEmitter::
78EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
79 std::ostream &OS) {
80 // Build a function name -> intrinsic name mapping.
Reid Spencerc4de3de2007-04-01 07:20:02 +000081 std::map<std::string, unsigned> IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +000082 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Reid Spencerc4de3de2007-04-01 07:20:02 +000083 IntMapping[Ints[i].Name] = i;
Chris Lattner9b843b22006-03-09 20:34:19 +000084
85 OS << "// Function name -> enum value recognizer code.\n";
86 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
87 OS << " switch (Name[5]) {\n";
Chris Lattner3b515802007-02-15 19:17:16 +000088 OS << " default:\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000089 // Emit the intrinsics in sorted order.
90 char LastChar = 0;
Reid Spencerc4de3de2007-04-01 07:20:02 +000091 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +000092 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +000093 if (I->first[5] != LastChar) {
94 LastChar = I->first[5];
Chris Lattner3b515802007-02-15 19:17:16 +000095 OS << " break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000096 OS << " case '" << LastChar << "':\n";
97 }
98
Reid Spencerc4de3de2007-04-01 07:20:02 +000099 // For overloaded intrinsics, only the prefix needs to match
100 if (Ints[I->second].isOverloaded)
Chandler Carruth69940402007-08-04 01:51:18 +0000101 OS << " if (Len > " << I->first.size()
102 << " && !memcmp(Name, \"" << I->first << ".\", "
Dale Johannesen49de9822009-02-05 01:49:45 +0000103 << (I->first.size() + 1) << ")) return " << TargetPrefix << "Intrinsic::"
Chandler Carruth69940402007-08-04 01:51:18 +0000104 << Ints[I->second].EnumName << ";\n";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000105 else
106 OS << " if (Len == " << I->first.size()
Chandler Carruth69940402007-08-04 01:51:18 +0000107 << " && !memcmp(Name, \"" << I->first << "\", "
Dale Johannesen49de9822009-02-05 01:49:45 +0000108 << I->first.size() << ")) return " << TargetPrefix << "Intrinsic::"
Reid Spencerc4de3de2007-04-01 07:20:02 +0000109 << Ints[I->second].EnumName << ";\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000110 }
111 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000112 OS << "#endif\n\n";
113}
114
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000115void IntrinsicEmitter::
116EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
117 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000118 OS << "// Intrinsic ID to name table\n";
119 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
120 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000121 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
122 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000123 OS << "#endif\n\n";
124}
125
Mon P Wang0d52ff12009-02-24 23:17:49 +0000126void IntrinsicEmitter::
127EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
128 std::ostream &OS) {
129 OS << "// Intrinsic ID to overload table\n";
130 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
131 OS << " // Note that entry #0 is the invalid intrinsic!\n";
132 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
133 OS << " ";
134 if (Ints[i].isOverloaded)
135 OS << "true";
136 else
137 OS << "false";
138 OS << ",\n";
139 }
140 OS << "#endif\n\n";
141}
142
Duncan Sands83ec4b62008-06-06 12:08:01 +0000143static void EmitTypeForValueType(std::ostream &OS, MVT::SimpleValueType VT) {
144 if (MVT(VT).isInteger()) {
145 unsigned BitWidth = MVT(VT).getSizeInBits();
Chandler Carruth69940402007-08-04 01:51:18 +0000146 OS << "IntegerType::get(" << BitWidth << ")";
147 } else if (VT == MVT::Other) {
148 // MVT::OtherVT is used to mean the empty struct type here.
149 OS << "StructType::get(std::vector<const Type *>())";
150 } else if (VT == MVT::f32) {
151 OS << "Type::FloatTy";
152 } else if (VT == MVT::f64) {
153 OS << "Type::DoubleTy";
Dale Johannesen317096a2007-09-28 01:08:20 +0000154 } else if (VT == MVT::f80) {
155 OS << "Type::X86_FP80Ty";
156 } else if (VT == MVT::f128) {
157 OS << "Type::FP128Ty";
158 } else if (VT == MVT::ppcf128) {
159 OS << "Type::PPC_FP128Ty";
Chandler Carruth69940402007-08-04 01:51:18 +0000160 } else if (VT == MVT::isVoid) {
161 OS << "Type::VoidTy";
162 } else {
163 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000164 }
165}
166
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000167static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
168 unsigned &ArgNo);
169
170static void EmitTypeGenerate(std::ostream &OS,
171 const std::vector<Record*> &ArgTypes,
172 unsigned &ArgNo) {
173 if (ArgTypes.size() == 1) {
174 EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
175 return;
176 }
177
178 OS << "StructType::get(";
179
180 for (std::vector<Record*>::const_iterator
Bill Wendling20072af2008-11-13 10:18:35 +0000181 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000182 EmitTypeGenerate(OS, *I, ArgNo);
Bill Wendling20072af2008-11-13 10:18:35 +0000183 OS << ", ";
184 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000185
Bill Wendling20072af2008-11-13 10:18:35 +0000186 OS << " NULL)";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000187}
188
189static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000190 unsigned &ArgNo) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000191 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000192
193 if (ArgType->isSubClassOf("LLVMMatchType")) {
194 unsigned Number = ArgType->getValueAsInt("Number");
195 assert(Number < ArgNo && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000196 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
197 OS << "VectorType::getExtendedElementVectorType"
198 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
199 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
200 OS << "VectorType::getTruncatedElementVectorType"
201 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
202 else
203 OS << "Tys[" << Number << "]";
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000204 } else if (VT == MVT::iAny || VT == MVT::fAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000205 // NOTE: The ArgNo variable here is not the absolute argument number, it is
206 // the index of the "arbitrary" type in the Tys array passed to the
207 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000208 // increment it when we actually hit an overloaded type. Getting this wrong
209 // leads to very subtle bugs!
210 OS << "Tys[" << ArgNo++ << "]";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000211 } else if (MVT(VT).isVector()) {
212 MVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000213 OS << "VectorType::get(";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000214 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT());
215 OS << ", " << VVT.getVectorNumElements() << ")";
Chandler Carruth69940402007-08-04 01:51:18 +0000216 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000217 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000218 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000219 OS << ")";
Mon P Wange3b3a722008-07-30 04:36:53 +0000220 } else if (VT == MVT::iPTRAny) {
221 // Make sure the user has passed us an argument type to overload. If not,
222 // treat it as an ordinary (not overloaded) intrinsic.
223 OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
224 << "] : PointerType::getUnqual(";
225 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
226 OS << ")";
227 ++ArgNo;
Chandler Carruth69940402007-08-04 01:51:18 +0000228 } else if (VT == MVT::isVoid) {
229 if (ArgNo == 0)
230 OS << "Type::VoidTy";
231 else
232 // MVT::isVoid is used to mean varargs here.
233 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000234 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000235 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000236 }
237}
238
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000239/// RecordListComparator - Provide a determinstic comparator for lists of
240/// records.
241namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000242 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000243 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000244 bool operator()(const RecPair &LHS,
245 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000246 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000247 const std::vector<Record*> *LHSVec = &LHS.first;
248 const std::vector<Record*> *RHSVec = &RHS.first;
249 unsigned RHSSize = RHSVec->size();
250 unsigned LHSSize = LHSVec->size();
251
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000252 do {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000253 if (i == RHSSize) return false; // RHS is shorter than LHS.
254 if ((*LHSVec)[i] != (*RHSVec)[i])
255 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
256 } while (++i != LHSSize);
257
Bill Wendling023422a2008-11-13 12:03:00 +0000258 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000259
260 i = 0;
261 LHSVec = &LHS.second;
262 RHSVec = &RHS.second;
263 RHSSize = RHSVec->size();
264 LHSSize = LHSVec->size();
265
266 for (i = 0; i != LHSSize; ++i) {
267 if (i == RHSSize) return false; // RHS is shorter than LHS.
268 if ((*LHSVec)[i] != (*RHSVec)[i])
269 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
270 }
271
272 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000273 }
274 };
275}
276
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000277void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
278 std::ostream &OS) {
279 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
280 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
281 OS << " switch (ID) {\n";
282 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000283
284 // This checking can emit a lot of very common code. To reduce the amount of
285 // code that we emit, batch up cases that have identical types. This avoids
286 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000287 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000288 MapTy UniqueArgInfos;
289
290 // Compute the unique argument type info.
291 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000292 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
293 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000294
295 // Loop through the array, emitting one comparison for each batch.
296 for (MapTy::iterator I = UniqueArgInfos.begin(),
297 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000298 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000299 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
300 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000301
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000302 const RecPair &ArgTypes = I->first;
303 const std::vector<Record*> &RetTys = ArgTypes.first;
304 const std::vector<Record*> &ParamTys = ArgTypes.second;
305
306 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
307 << ParamTys.size();
308
309 // Emit return types.
310 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
311 Record *ArgType = RetTys[j];
312 OS << ", ";
313
Chandler Carruth69940402007-08-04 01:51:18 +0000314 if (ArgType->isSubClassOf("LLVMMatchType")) {
315 unsigned Number = ArgType->getValueAsInt("Number");
316 assert(Number < j && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000317 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
318 OS << "~(ExtendedElementVectorType | " << Number << ")";
319 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
320 OS << "~(TruncatedElementVectorType | " << Number << ")";
321 else
322 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000323 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000324 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000325 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000326
327 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000328 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000329 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000330 }
331
332 // Emit the parameter types.
333 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
334 Record *ArgType = ParamTys[j];
335 OS << ", ";
336
337 if (ArgType->isSubClassOf("LLVMMatchType")) {
338 unsigned Number = ArgType->getValueAsInt("Number");
339 assert(Number < j + RetTys.size() && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000340 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
341 OS << "~(ExtendedElementVectorType | " << Number << ")";
342 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
343 OS << "~(TruncatedElementVectorType | " << Number << ")";
344 else
345 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000346 } else {
347 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
348 OS << getEnumName(VT);
349
350 if (VT == MVT::isVoid && j != 0 && j != je - 1)
351 throw "Var arg type not last argument";
352 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000353 }
354
Chandler Carruth69940402007-08-04 01:51:18 +0000355 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000356 OS << " break;\n";
357 }
358 OS << " }\n";
359 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000360}
361
Jim Laskey95af5922007-02-07 20:38:26 +0000362void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
363 std::ostream &OS) {
364 OS << "// Code for generating Intrinsic function declarations.\n";
365 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
366 OS << " switch (id) {\n";
367 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
368
369 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
370 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000371 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000372 MapTy UniqueArgInfos;
373
374 // Compute the unique argument type info.
375 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000376 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
377 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000378
379 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000380 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
381
Jim Laskey95af5922007-02-07 20:38:26 +0000382 for (MapTy::iterator I = UniqueArgInfos.begin(),
383 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000384 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000385 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
386 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000387
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000388 const RecPair &ArgTypes = I->first;
389 const std::vector<Record*> &RetTys = ArgTypes.first;
390 const std::vector<Record*> &ParamTys = ArgTypes.second;
391
392 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000393
Chandler Carruth69940402007-08-04 01:51:18 +0000394 if (N > 1 &&
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000395 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000396 OS << " IsVarArg = true;\n";
397 --N;
398 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000399
Reid Spencer84c614d2007-05-22 19:30:31 +0000400 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000401 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000402 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000403 OS << ";\n";
404
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000405 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000406 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000407 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000408 OS << ");\n";
409 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000410
Jim Laskey95af5922007-02-07 20:38:26 +0000411 OS << " break;\n";
412 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000413
Jim Laskey95af5922007-02-07 20:38:26 +0000414 OS << " }\n";
415 OS << "#endif\n\n";
416}
417
Chris Lattner048ffb22009-01-12 01:18:58 +0000418/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000419void IntrinsicEmitter::
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000420EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
421 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
422 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000423 if (TargetOnly)
424 OS << "static AttrListPtr getAttributes(" << TargetPrefix
425 << "Intrinsic::ID id) {";
426 else
427 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {";
Chris Lattner048ffb22009-01-12 01:18:58 +0000428 OS << " // No intrinsic can throw exceptions.\n";
429 OS << " Attributes Attr = Attribute::NoUnwind;\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000430 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000431 OS << " default: break;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000432 unsigned MaxArgAttrs = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000433 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner10dae942009-01-12 01:27:55 +0000434 MaxArgAttrs =
435 std::max(MaxArgAttrs, unsigned(Ints[i].ArgumentAttributes.size()));
Chris Lattner7056de32006-03-24 01:13:55 +0000436 switch (Ints[i].ModRef) {
437 default: break;
438 case CodeGenIntrinsic::NoMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000439 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
440 << ":\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000441 break;
442 }
443 }
Devang Patel05988662008-09-25 21:00:45 +0000444 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000445 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000446 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
447 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000448 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000449 case CodeGenIntrinsic::ReadArgMem:
450 case CodeGenIntrinsic::ReadMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000451 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
452 << ":\n";
Chris Lattner022f64f2006-03-13 23:08:44 +0000453 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000454 }
455 }
Devang Patel05988662008-09-25 21:00:45 +0000456 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000457 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000458 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000459 OS << " AttributeWithIndex AWI[" << MaxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000460 OS << " unsigned NumAttrs = 0;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000461 OS << " switch (id) {\n";
462 OS << " default: break;\n";
463
464 // Add argument attributes for any intrinsics that have them.
465 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
466 if (Ints[i].ArgumentAttributes.empty()) continue;
467
Dale Johannesen49de9822009-02-05 01:49:45 +0000468 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
469 << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000470
471 std::vector<std::pair<unsigned, CodeGenIntrinsic::ArgAttribute> > ArgAttrs =
472 Ints[i].ArgumentAttributes;
473 // Sort by argument index.
474 std::sort(ArgAttrs.begin(), ArgAttrs.end());
475
476 unsigned NumArgsWithAttrs = 0;
477
Chris Lattnerd4a27002009-01-12 02:41:37 +0000478 while (!ArgAttrs.empty()) {
479 unsigned ArgNo = ArgAttrs[0].first;
480
481 OS << " AWI[" << NumArgsWithAttrs++ << "] = AttributeWithIndex::get("
482 << ArgNo+1 << ", 0";
483
484 while (!ArgAttrs.empty() && ArgAttrs[0].first == ArgNo) {
485 switch (ArgAttrs[0].second) {
486 default: assert(0 && "Unknown arg attribute");
487 case CodeGenIntrinsic::NoCapture:
488 OS << "|Attribute::NoCapture";
489 break;
490 }
491 ArgAttrs.erase(ArgAttrs.begin());
492 }
493 OS << ");\n";
494 }
Chris Lattner10dae942009-01-12 01:27:55 +0000495
Chris Lattnerd4a27002009-01-12 02:41:37 +0000496 OS << " NumAttrs = " << NumArgsWithAttrs << ";\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000497 OS << " break;\n";
498 }
499
500 OS << " }\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000501 OS << " AWI[NumAttrs] = AttributeWithIndex::get(~0, Attr);\n";
502 OS << " return AttrListPtr::get(AWI, NumAttrs+1);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000503 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000504 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000505}
Chris Lattner022f64f2006-03-13 23:08:44 +0000506
Duncan Sandsd869b382009-02-14 10:56:35 +0000507/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
508void IntrinsicEmitter::
509EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
510 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n";
511 OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n";
512 OS << "switch (id) {\n";
513 OS << "default:\n return UnknownModRefBehavior;\n";
514 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
515 if (Ints[i].ModRef == CodeGenIntrinsic::WriteMem)
516 continue;
517 OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
518 << ":\n";
519 switch (Ints[i].ModRef) {
520 default:
521 assert(false && "Unknown Mod/Ref type!");
522 case CodeGenIntrinsic::NoMem:
523 OS << " return DoesNotAccessMemory;\n";
524 break;
525 case CodeGenIntrinsic::ReadArgMem:
526 case CodeGenIntrinsic::ReadMem:
527 OS << " return OnlyReadsMemory;\n";
528 break;
529 case CodeGenIntrinsic::WriteArgMem:
530 OS << " return AccessesArguments;\n";
531 break;
532 }
533 }
534 OS << "}\n";
535 OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
536}
537
Chris Lattner022f64f2006-03-13 23:08:44 +0000538void IntrinsicEmitter::
539EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
540 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
541 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
542 OS << " switch (F->getIntrinsicID()) {\n";
543 OS << " default: BuiltinName = \"\"; break;\n";
544 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
545 if (!Ints[i].GCCBuiltinName.empty()) {
546 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
547 << Ints[i].GCCBuiltinName << "\"; break;\n";
548 }
549 }
550 OS << " }\n";
551 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000552}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000553
Chris Lattner331bf922008-01-04 04:38:35 +0000554/// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
555/// sorted range of builtin names is equal to the current builtin. This breaks
556/// it down into a simple tree.
557///
558/// At this point, we know that all the builtins in the range have the same name
559/// for the first 'CharStart' characters. Only the end of the name needs to be
560/// discriminated.
561typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
562static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
563 unsigned CharStart, unsigned Indent,
Dale Johannesen49de9822009-02-05 01:49:45 +0000564 std::string TargetPrefix, std::ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000565 if (Start == End) return; // empty range.
566
567 // Determine what, if anything, is the same about all these strings.
568 std::string CommonString = Start->first;
569 unsigned NumInRange = 0;
570 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
571 // Find the first character that doesn't match.
572 const std::string &ThisStr = I->first;
573 unsigned NonMatchChar = CharStart;
574 while (NonMatchChar < CommonString.size() &&
575 NonMatchChar < ThisStr.size() &&
576 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
577 ++NonMatchChar;
578 // Truncate off pieces that don't match.
579 CommonString.resize(NonMatchChar);
580 }
581
582 // Just compare the rest of the string.
583 if (NumInRange == 1) {
584 if (CharStart != CommonString.size()) {
585 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
586 if (CharStart) OS << "+" << CharStart;
587 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
588 OS << CommonString.size() - CharStart << "))\n";
589 ++Indent;
590 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000591 OS << std::string(Indent*2, ' ') << "IntrinsicID = " << TargetPrefix
592 << "Intrinsic::";
Chris Lattner331bf922008-01-04 04:38:35 +0000593 OS << Start->second << ";\n";
594 return;
595 }
596
597 // At this point, we potentially have a common prefix for these builtins, emit
598 // a check for this common prefix.
599 if (CommonString.size() != CharStart) {
600 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
601 if (CharStart) OS << "+" << CharStart;
602 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
603 OS << CommonString.size()-CharStart << ")) {\n";
604
Dale Johannesen49de9822009-02-05 01:49:45 +0000605 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1,
606 TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000607 OS << std::string(Indent*2, ' ') << "}\n";
608 return;
609 }
610
611 // Output a switch on the character that differs across the set.
612 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
613 << "]) {";
614 if (CharStart)
615 OS << " // \"" << std::string(Start->first.begin(),
616 Start->first.begin()+CharStart) << "\"";
617 OS << "\n";
618
619 for (StrMapIterator I = Start; I != End; ) {
620 char ThisChar = I->first[CharStart];
621 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
622 // Figure out the range that has this common character.
623 StrMapIterator NextChar = I;
624 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
625 ++NextChar)
626 /*empty*/;
Dale Johannesen49de9822009-02-05 01:49:45 +0000627 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, TargetPrefix,OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000628 OS << std::string(Indent*2, ' ') << " break;\n";
629 I = NextChar;
630 }
631 OS << std::string(Indent*2, ' ') << "}\n";
632}
633
634/// EmitTargetBuiltins - All of the builtins in the specified map are for the
635/// same target, and we already checked it.
636static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000637 const std::string &TargetPrefix,
Chris Lattner331bf922008-01-04 04:38:35 +0000638 std::ostream &OS) {
639 // Rearrange the builtins by length.
640 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
641 BuiltinsByLen.reserve(100);
642
643 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
644 if (I->first.size() >= BuiltinsByLen.size())
645 BuiltinsByLen.resize(I->first.size()+1);
646 BuiltinsByLen[I->first.size()].insert(*I);
647 }
648
649 // Now that we have all the builtins by their length, emit a switch stmt.
650 OS << " switch (strlen(BuiltinName)) {\n";
651 OS << " default: break;\n";
652 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
653 if (BuiltinsByLen[i].empty()) continue;
654 OS << " case " << i << ":\n";
655 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
Dale Johannesen49de9822009-02-05 01:49:45 +0000656 0, 3, TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000657 OS << " break;\n";
658 }
659 OS << " }\n";
660}
661
662
Chris Lattner3f8b8912006-03-15 01:33:26 +0000663void IntrinsicEmitter::
664EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
665 std::ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000666 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000667 BIMTy BuiltinMap;
668 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
669 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000670 // Get the map for this target prefix.
671 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
672
673 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
674 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000675 throw "Intrinsic '" + Ints[i].TheDef->getName() +
676 "': duplicate GCC builtin name!";
677 }
678 }
679
680 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
681 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
682 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
683 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
684 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000685
686 if (TargetOnly) {
687 OS << "static " << TargetPrefix << "Intrinsic::ID "
688 << "getIntrinsicForGCCBuiltin(const char "
689 << "*TargetPrefix, const char *BuiltinName) {\n";
690 OS << " " << TargetPrefix << "Intrinsic::ID IntrinsicID = ";
691 } else {
692 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
693 << "*TargetPrefix, const char *BuiltinName) {\n";
694 OS << " Intrinsic::ID IntrinsicID = ";
695 }
696
697 if (TargetOnly)
698 OS << "(" << TargetPrefix<< "Intrinsic::ID)";
699
700 OS << "Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000701
Chris Lattner3f8b8912006-03-15 01:33:26 +0000702 // Note: this could emit significantly better code if we cared.
703 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000704 OS << " ";
705 if (!I->first.empty())
706 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
707 else
708 OS << "/* Target Independent Builtins */ ";
709 OS << "{\n";
710
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000711 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000712 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000713 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000714 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000715 OS << " return IntrinsicID;\n";
716 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000717 OS << "#endif\n\n";
718}