blob: 36768316bc0e894c1bff5869b467ce7ab22622f3 [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
Daniel Dunbar1a551802009-07-03 00:10:29 +000025void IntrinsicEmitter::run(raw_ostream &OS) {
Chris Lattner9e493cf2006-03-03 02:32:46 +000026 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,
Daniel Dunbar1a551802009-07-03 00:10:29 +000065 raw_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,
Daniel Dunbar1a551802009-07-03 00:10:29 +000079 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000080 // 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,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000117 raw_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,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000128 raw_ostream &OS) {
Mon P Wang0d52ff12009-02-24 23:17:49 +0000129 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
Daniel Dunbar1a551802009-07-03 00:10:29 +0000143static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000144 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.
Chris Lattner0fd38062009-07-01 04:13:31 +0000149 OS << "StructType::get()";
Chandler Carruth69940402007-08-04 01:51:18 +0000150 } 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
Daniel Dunbar1a551802009-07-03 00:10:29 +0000167static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000168 unsigned &ArgNo);
169
Daniel Dunbar1a551802009-07-03 00:10:29 +0000170static void EmitTypeGenerate(raw_ostream &OS,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000171 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
Daniel Dunbar1a551802009-07-03 00:10:29 +0000189static void EmitTypeGenerate(raw_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
Jim Grosbachda4231f2009-03-26 16:17:51 +0000239/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000240/// 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,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000278 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000279 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");
Bob Wilsonbc039792009-01-07 00:09:01 +0000316 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
317 OS << "~(ExtendedElementVectorType | " << Number << ")";
318 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
319 OS << "~(TruncatedElementVectorType | " << Number << ")";
320 else
321 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000322 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000323 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000324 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000325
326 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000327 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000328 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000329 }
330
331 // Emit the parameter types.
332 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
333 Record *ArgType = ParamTys[j];
334 OS << ", ";
335
336 if (ArgType->isSubClassOf("LLVMMatchType")) {
337 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilsonbc039792009-01-07 00:09:01 +0000338 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
339 OS << "~(ExtendedElementVectorType | " << Number << ")";
340 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
341 OS << "~(TruncatedElementVectorType | " << Number << ")";
342 else
343 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000344 } else {
345 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
346 OS << getEnumName(VT);
347
348 if (VT == MVT::isVoid && j != 0 && j != je - 1)
349 throw "Var arg type not last argument";
350 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000351 }
352
Chandler Carruth69940402007-08-04 01:51:18 +0000353 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000354 OS << " break;\n";
355 }
356 OS << " }\n";
357 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000358}
359
Jim Laskey95af5922007-02-07 20:38:26 +0000360void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000361 raw_ostream &OS) {
Jim Laskey95af5922007-02-07 20:38:26 +0000362 OS << "// Code for generating Intrinsic function declarations.\n";
363 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
364 OS << " switch (id) {\n";
365 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
366
367 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
368 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000369 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000370 MapTy UniqueArgInfos;
371
372 // Compute the unique argument type info.
373 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000374 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
375 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000376
377 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000378 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
379
Jim Laskey95af5922007-02-07 20:38:26 +0000380 for (MapTy::iterator I = UniqueArgInfos.begin(),
381 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000382 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000383 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
384 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000385
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000386 const RecPair &ArgTypes = I->first;
387 const std::vector<Record*> &RetTys = ArgTypes.first;
388 const std::vector<Record*> &ParamTys = ArgTypes.second;
389
390 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000391
Chandler Carruth69940402007-08-04 01:51:18 +0000392 if (N > 1 &&
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000393 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000394 OS << " IsVarArg = true;\n";
395 --N;
396 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000397
Reid Spencer84c614d2007-05-22 19:30:31 +0000398 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000399 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000400 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000401 OS << ";\n";
402
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000403 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000404 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000405 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000406 OS << ");\n";
407 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000408
Jim Laskey95af5922007-02-07 20:38:26 +0000409 OS << " break;\n";
410 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000411
Jim Laskey95af5922007-02-07 20:38:26 +0000412 OS << " }\n";
413 OS << "#endif\n\n";
414}
415
Chris Lattner048ffb22009-01-12 01:18:58 +0000416/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000417void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000418EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000419 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
420 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000421 if (TargetOnly)
422 OS << "static AttrListPtr getAttributes(" << TargetPrefix
423 << "Intrinsic::ID id) {";
424 else
425 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {";
Chris Lattner048ffb22009-01-12 01:18:58 +0000426 OS << " // No intrinsic can throw exceptions.\n";
427 OS << " Attributes Attr = Attribute::NoUnwind;\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000428 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000429 OS << " default: break;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000430 unsigned MaxArgAttrs = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000431 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner10dae942009-01-12 01:27:55 +0000432 MaxArgAttrs =
433 std::max(MaxArgAttrs, unsigned(Ints[i].ArgumentAttributes.size()));
Chris Lattner7056de32006-03-24 01:13:55 +0000434 switch (Ints[i].ModRef) {
435 default: break;
436 case CodeGenIntrinsic::NoMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000437 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
438 << ":\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000439 break;
440 }
441 }
Devang Patel05988662008-09-25 21:00:45 +0000442 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000443 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000444 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
445 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000446 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000447 case CodeGenIntrinsic::ReadArgMem:
448 case CodeGenIntrinsic::ReadMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000449 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
450 << ":\n";
Chris Lattner022f64f2006-03-13 23:08:44 +0000451 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000452 }
453 }
Devang Patel05988662008-09-25 21:00:45 +0000454 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000455 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000456 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000457 OS << " AttributeWithIndex AWI[" << MaxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000458 OS << " unsigned NumAttrs = 0;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000459 OS << " switch (id) {\n";
460 OS << " default: break;\n";
461
462 // Add argument attributes for any intrinsics that have them.
463 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
464 if (Ints[i].ArgumentAttributes.empty()) continue;
465
Dale Johannesen49de9822009-02-05 01:49:45 +0000466 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
467 << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000468
469 std::vector<std::pair<unsigned, CodeGenIntrinsic::ArgAttribute> > ArgAttrs =
470 Ints[i].ArgumentAttributes;
471 // Sort by argument index.
472 std::sort(ArgAttrs.begin(), ArgAttrs.end());
473
474 unsigned NumArgsWithAttrs = 0;
475
Chris Lattnerd4a27002009-01-12 02:41:37 +0000476 while (!ArgAttrs.empty()) {
477 unsigned ArgNo = ArgAttrs[0].first;
478
479 OS << " AWI[" << NumArgsWithAttrs++ << "] = AttributeWithIndex::get("
480 << ArgNo+1 << ", 0";
481
482 while (!ArgAttrs.empty() && ArgAttrs[0].first == ArgNo) {
483 switch (ArgAttrs[0].second) {
484 default: assert(0 && "Unknown arg attribute");
485 case CodeGenIntrinsic::NoCapture:
486 OS << "|Attribute::NoCapture";
487 break;
488 }
489 ArgAttrs.erase(ArgAttrs.begin());
490 }
491 OS << ");\n";
492 }
Chris Lattner10dae942009-01-12 01:27:55 +0000493
Chris Lattnerd4a27002009-01-12 02:41:37 +0000494 OS << " NumAttrs = " << NumArgsWithAttrs << ";\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000495 OS << " break;\n";
496 }
497
498 OS << " }\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000499 OS << " AWI[NumAttrs] = AttributeWithIndex::get(~0, Attr);\n";
500 OS << " return AttrListPtr::get(AWI, NumAttrs+1);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000501 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000502 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000503}
Chris Lattner022f64f2006-03-13 23:08:44 +0000504
Duncan Sandsd869b382009-02-14 10:56:35 +0000505/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
506void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000507EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Duncan Sandsd869b382009-02-14 10:56:35 +0000508 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n";
509 OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n";
510 OS << "switch (id) {\n";
511 OS << "default:\n return UnknownModRefBehavior;\n";
512 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
513 if (Ints[i].ModRef == CodeGenIntrinsic::WriteMem)
514 continue;
515 OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
516 << ":\n";
517 switch (Ints[i].ModRef) {
518 default:
519 assert(false && "Unknown Mod/Ref type!");
520 case CodeGenIntrinsic::NoMem:
521 OS << " return DoesNotAccessMemory;\n";
522 break;
523 case CodeGenIntrinsic::ReadArgMem:
524 case CodeGenIntrinsic::ReadMem:
525 OS << " return OnlyReadsMemory;\n";
526 break;
527 case CodeGenIntrinsic::WriteArgMem:
528 OS << " return AccessesArguments;\n";
529 break;
530 }
531 }
532 OS << "}\n";
533 OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
534}
535
Chris Lattner022f64f2006-03-13 23:08:44 +0000536void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000537EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Chris Lattner022f64f2006-03-13 23:08:44 +0000538 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
539 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
540 OS << " switch (F->getIntrinsicID()) {\n";
541 OS << " default: BuiltinName = \"\"; break;\n";
542 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
543 if (!Ints[i].GCCBuiltinName.empty()) {
544 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
545 << Ints[i].GCCBuiltinName << "\"; break;\n";
546 }
547 }
548 OS << " }\n";
549 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000550}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000551
Chris Lattner331bf922008-01-04 04:38:35 +0000552/// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
553/// sorted range of builtin names is equal to the current builtin. This breaks
554/// it down into a simple tree.
555///
556/// At this point, we know that all the builtins in the range have the same name
557/// for the first 'CharStart' characters. Only the end of the name needs to be
558/// discriminated.
559typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
560static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
561 unsigned CharStart, unsigned Indent,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000562 std::string TargetPrefix, raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000563 if (Start == End) return; // empty range.
564
565 // Determine what, if anything, is the same about all these strings.
566 std::string CommonString = Start->first;
567 unsigned NumInRange = 0;
568 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
569 // Find the first character that doesn't match.
570 const std::string &ThisStr = I->first;
571 unsigned NonMatchChar = CharStart;
572 while (NonMatchChar < CommonString.size() &&
573 NonMatchChar < ThisStr.size() &&
574 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
575 ++NonMatchChar;
576 // Truncate off pieces that don't match.
577 CommonString.resize(NonMatchChar);
578 }
579
580 // Just compare the rest of the string.
581 if (NumInRange == 1) {
582 if (CharStart != CommonString.size()) {
583 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
584 if (CharStart) OS << "+" << CharStart;
585 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
586 OS << CommonString.size() - CharStart << "))\n";
587 ++Indent;
588 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000589 OS << std::string(Indent*2, ' ') << "IntrinsicID = " << TargetPrefix
590 << "Intrinsic::";
Chris Lattner331bf922008-01-04 04:38:35 +0000591 OS << Start->second << ";\n";
592 return;
593 }
594
595 // At this point, we potentially have a common prefix for these builtins, emit
596 // a check for this common prefix.
597 if (CommonString.size() != CharStart) {
598 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
599 if (CharStart) OS << "+" << CharStart;
600 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
601 OS << CommonString.size()-CharStart << ")) {\n";
602
Dale Johannesen49de9822009-02-05 01:49:45 +0000603 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1,
604 TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000605 OS << std::string(Indent*2, ' ') << "}\n";
606 return;
607 }
608
609 // Output a switch on the character that differs across the set.
610 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
611 << "]) {";
612 if (CharStart)
613 OS << " // \"" << std::string(Start->first.begin(),
614 Start->first.begin()+CharStart) << "\"";
615 OS << "\n";
616
617 for (StrMapIterator I = Start; I != End; ) {
618 char ThisChar = I->first[CharStart];
619 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
620 // Figure out the range that has this common character.
621 StrMapIterator NextChar = I;
622 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
623 ++NextChar)
624 /*empty*/;
Dale Johannesen49de9822009-02-05 01:49:45 +0000625 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, TargetPrefix,OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000626 OS << std::string(Indent*2, ' ') << " break;\n";
627 I = NextChar;
628 }
629 OS << std::string(Indent*2, ' ') << "}\n";
630}
631
632/// EmitTargetBuiltins - All of the builtins in the specified map are for the
633/// same target, and we already checked it.
634static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000635 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000636 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000637 // Rearrange the builtins by length.
638 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
639 BuiltinsByLen.reserve(100);
640
641 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
642 if (I->first.size() >= BuiltinsByLen.size())
643 BuiltinsByLen.resize(I->first.size()+1);
644 BuiltinsByLen[I->first.size()].insert(*I);
645 }
646
647 // Now that we have all the builtins by their length, emit a switch stmt.
648 OS << " switch (strlen(BuiltinName)) {\n";
649 OS << " default: break;\n";
650 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
651 if (BuiltinsByLen[i].empty()) continue;
652 OS << " case " << i << ":\n";
653 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
Dale Johannesen49de9822009-02-05 01:49:45 +0000654 0, 3, TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000655 OS << " break;\n";
656 }
657 OS << " }\n";
658}
659
660
Chris Lattner3f8b8912006-03-15 01:33:26 +0000661void IntrinsicEmitter::
662EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000663 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000664 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000665 BIMTy BuiltinMap;
666 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
667 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000668 // Get the map for this target prefix.
669 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
670
671 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
672 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000673 throw "Intrinsic '" + Ints[i].TheDef->getName() +
674 "': duplicate GCC builtin name!";
675 }
676 }
677
678 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
679 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
680 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
681 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
682 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000683
684 if (TargetOnly) {
685 OS << "static " << TargetPrefix << "Intrinsic::ID "
686 << "getIntrinsicForGCCBuiltin(const char "
687 << "*TargetPrefix, const char *BuiltinName) {\n";
688 OS << " " << TargetPrefix << "Intrinsic::ID IntrinsicID = ";
689 } else {
690 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
691 << "*TargetPrefix, const char *BuiltinName) {\n";
692 OS << " Intrinsic::ID IntrinsicID = ";
693 }
694
695 if (TargetOnly)
696 OS << "(" << TargetPrefix<< "Intrinsic::ID)";
697
698 OS << "Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000699
Chris Lattner3f8b8912006-03-15 01:33:26 +0000700 // Note: this could emit significantly better code if we cared.
701 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000702 OS << " ";
703 if (!I->first.empty())
704 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
705 else
706 OS << "/* Target Independent Builtins */ ";
707 OS << "{\n";
708
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000709 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000710 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000711 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000712 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000713 OS << " return IntrinsicID;\n";
714 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000715 OS << "#endif\n\n";
716}