blob: b17212bf1197656f3a1ea779b31711c7dad9e807 [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"
Chris Lattner298b1762010-09-06 03:14:45 +000016#include "StringMatcher.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Chris Lattner18faf5d2006-03-13 22:38:57 +000018#include "llvm/ADT/StringExtras.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000019#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000023// IntrinsicEmitter Implementation
24//===----------------------------------------------------------------------===//
25
Daniel Dunbar1a551802009-07-03 00:10:29 +000026void IntrinsicEmitter::run(raw_ostream &OS) {
Chris Lattner9e493cf2006-03-03 02:32:46 +000027 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
28
Dale Johannesen49de9822009-02-05 01:49:45 +000029 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
30
31 if (TargetOnly && !Ints.empty())
32 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattner9e493cf2006-03-03 02:32:46 +000033
Douglas Gregor7d9663c2010-05-11 06:17:44 +000034 EmitPrefix(OS);
35
Chris Lattner9e493cf2006-03-03 02:32:46 +000036 // Emit the enum information.
37 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000038
39 // Emit the intrinsic ID -> name table.
40 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000041
42 // Emit the intrinsic ID -> overload table.
43 EmitIntrinsicToOverloadTable(Ints, OS);
44
Chris Lattner9b843b22006-03-09 20:34:19 +000045 // Emit the function name recognizer.
46 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000047
Chris Lattnerf97a00e2006-03-09 22:05:04 +000048 // Emit the intrinsic verifier.
49 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000050
Jim Laskey95af5922007-02-07 20:38:26 +000051 // Emit the intrinsic declaration generator.
52 EmitGenerator(Ints, OS);
53
Duncan Sandsa3355ff2007-12-03 20:06:50 +000054 // Emit the intrinsic parameter attributes.
55 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000056
Duncan Sandsd869b382009-02-14 10:56:35 +000057 // Emit intrinsic alias analysis mod/ref behavior.
58 EmitModRefBehavior(Ints, OS);
59
Chris Lattner022f64f2006-03-13 23:08:44 +000060 // Emit a list of intrinsics with corresponding GCC builtins.
61 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000062
63 // Emit code to translate GCC builtins into LLVM intrinsics.
64 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor7d9663c2010-05-11 06:17:44 +000065
66 EmitSuffix(OS);
67}
68
69void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
70 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000071 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
72 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000073 "# pragma push_macro(\"setjmp\")\n"
74 "# undef setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000075 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000076 "#endif\n\n";
77}
78
79void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencer1f409602010-09-24 19:48:47 +000080 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000081 "// let's return it to _setjmp state\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000082 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000083 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000084 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000085}
86
87void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000088 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000089 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000090 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
91 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
92 OS << " " << Ints[i].EnumName;
93 OS << ((i != e-1) ? ", " : " ");
94 OS << std::string(40-Ints[i].EnumName.size(), ' ')
95 << "// " << Ints[i].Name << "\n";
96 }
97 OS << "#endif\n\n";
98}
Chris Lattner9b843b22006-03-09 20:34:19 +000099
100void IntrinsicEmitter::
101EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000102 raw_ostream &OS) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000103 // Build a 'first character of function name' -> intrinsic # mapping.
104 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000105 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattnercc67c752010-09-06 03:58:45 +0000106 IntMapping[Ints[i].Name[5]].push_back(i);
107
Chris Lattner9b843b22006-03-09 20:34:19 +0000108 OS << "// Function name -> enum value recognizer code.\n";
109 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattnercc67c752010-09-06 03:58:45 +0000110 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
111 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
112 OS << " default: break;\n";
113 // Emit the intrinsic matching stuff by first letter.
114 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000115 E = IntMapping.end(); I != E; ++I) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000116 OS << " case '" << I->first << "':\n";
117 std::vector<unsigned> &IntList = I->second;
118
119 // Emit all the overloaded intrinsics first, build a table of the
120 // non-overloaded ones.
121 std::vector<StringMatcher::StringPair> MatchTable;
122
123 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
124 unsigned IntNo = IntList[i];
125 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
126 Ints[IntNo].EnumName + ";";
127
128 if (!Ints[IntNo].isOverloaded) {
129 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
130 continue;
131 }
132
133 // For overloaded intrinsics, only the prefix needs to match
134 std::string TheStr = Ints[IntNo].Name.substr(6);
135 TheStr += '.'; // Require "bswap." instead of bswap.
136 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
137 << Result << '\n';
Chris Lattner9b843b22006-03-09 20:34:19 +0000138 }
139
Chris Lattnercc67c752010-09-06 03:58:45 +0000140 // Emit the matcher logic for the fixed length strings.
141 StringMatcher("NameR", MatchTable, OS).Emit(1);
142 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000143 }
Chris Lattnercc67c752010-09-06 03:58:45 +0000144
Chris Lattner9b843b22006-03-09 20:34:19 +0000145 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000146 OS << "#endif\n\n";
147}
148
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000149void IntrinsicEmitter::
150EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000151 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000152 OS << "// Intrinsic ID to name table\n";
153 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
154 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000155 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
156 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000157 OS << "#endif\n\n";
158}
159
Mon P Wang0d52ff12009-02-24 23:17:49 +0000160void IntrinsicEmitter::
161EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000162 raw_ostream &OS) {
Mon P Wang0d52ff12009-02-24 23:17:49 +0000163 OS << "// Intrinsic ID to overload table\n";
164 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
165 OS << " // Note that entry #0 is the invalid intrinsic!\n";
166 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
167 OS << " ";
168 if (Ints[i].isOverloaded)
169 OS << "true";
170 else
171 OS << "false";
172 OS << ",\n";
173 }
174 OS << "#endif\n\n";
175}
176
Owen Anderson825b72b2009-08-11 20:47:22 +0000177static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +0000178 if (EVT(VT).isInteger()) {
179 unsigned BitWidth = EVT(VT).getSizeInBits();
Owen Anderson1d0be152009-08-13 21:58:54 +0000180 OS << "IntegerType::get(Context, " << BitWidth << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 } else if (VT == MVT::Other) {
182 // MVT::OtherVT is used to mean the empty struct type here.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000183 OS << "StructType::get(Context)";
Dan Gohmane3376ec2011-12-20 00:02:33 +0000184 } else if (VT == MVT::f16) {
185 OS << "Type::getHalfTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 } else if (VT == MVT::f32) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000187 OS << "Type::getFloatTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 } else if (VT == MVT::f64) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000189 OS << "Type::getDoubleTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 } else if (VT == MVT::f80) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000191 OS << "Type::getX86_FP80Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 } else if (VT == MVT::f128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000193 OS << "Type::getFP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 } else if (VT == MVT::ppcf128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000195 OS << "Type::getPPC_FP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 } else if (VT == MVT::isVoid) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000197 OS << "Type::getVoidTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 } else if (VT == MVT::Metadata) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000199 OS << "Type::getMetadataTy(Context)";
Dale Johannesen8be45252010-09-11 00:16:46 +0000200 } else if (VT == MVT::x86mmx) {
201 OS << "Type::getX86_MMXTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000202 } else {
203 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000204 }
205}
206
Daniel Dunbar1a551802009-07-03 00:10:29 +0000207static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000208 unsigned &ArgNo);
209
Daniel Dunbar1a551802009-07-03 00:10:29 +0000210static void EmitTypeGenerate(raw_ostream &OS,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000211 const std::vector<Record*> &ArgTypes,
212 unsigned &ArgNo) {
Chris Lattner93dc92e2010-03-22 20:56:36 +0000213 if (ArgTypes.empty())
214 return EmitTypeForValueType(OS, MVT::isVoid);
215
216 if (ArgTypes.size() == 1)
217 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000218
Chris Lattnerb2318662011-06-18 22:48:56 +0000219 OS << "StructType::get(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000220
221 for (std::vector<Record*>::const_iterator
Bill Wendling20072af2008-11-13 10:18:35 +0000222 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000223 EmitTypeGenerate(OS, *I, ArgNo);
Bill Wendling20072af2008-11-13 10:18:35 +0000224 OS << ", ";
225 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000226
Bill Wendling20072af2008-11-13 10:18:35 +0000227 OS << " NULL)";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000228}
229
Daniel Dunbar1a551802009-07-03 00:10:29 +0000230static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000231 unsigned &ArgNo) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000233
234 if (ArgType->isSubClassOf("LLVMMatchType")) {
235 unsigned Number = ArgType->getValueAsInt("Number");
236 assert(Number < ArgNo && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000237 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
238 OS << "VectorType::getExtendedElementVectorType"
239 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
240 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
241 OS << "VectorType::getTruncatedElementVectorType"
242 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
243 else
244 OS << "Tys[" << Number << "]";
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000246 // NOTE: The ArgNo variable here is not the absolute argument number, it is
247 // the index of the "arbitrary" type in the Tys array passed to the
248 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000249 // increment it when we actually hit an overloaded type. Getting this wrong
250 // leads to very subtle bugs!
251 OS << "Tys[" << ArgNo++ << "]";
Owen Andersone50ed302009-08-10 22:56:29 +0000252 } else if (EVT(VT).isVector()) {
253 EVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000254 OS << "VectorType::get(";
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000256 OS << ", " << VVT.getVectorNumElements() << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000258 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000259 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000260 OS << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 } else if (VT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +0000262 // Make sure the user has passed us an argument type to overload. If not,
263 // treat it as an ordinary (not overloaded) intrinsic.
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000264 OS << "(" << ArgNo << " < Tys.size()) ? Tys[" << ArgNo
Mon P Wange3b3a722008-07-30 04:36:53 +0000265 << "] : PointerType::getUnqual(";
266 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
267 OS << ")";
268 ++ArgNo;
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 } else if (VT == MVT::isVoid) {
Chandler Carruth69940402007-08-04 01:51:18 +0000270 if (ArgNo == 0)
Owen Anderson1d0be152009-08-13 21:58:54 +0000271 OS << "Type::getVoidTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000272 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 // MVT::isVoid is used to mean varargs here.
Chandler Carruth69940402007-08-04 01:51:18 +0000274 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000275 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000276 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000277 }
278}
279
Jim Grosbachda4231f2009-03-26 16:17:51 +0000280/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000281/// records.
282namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000283 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000284 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000285 bool operator()(const RecPair &LHS,
286 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000287 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000288 const std::vector<Record*> *LHSVec = &LHS.first;
289 const std::vector<Record*> *RHSVec = &RHS.first;
290 unsigned RHSSize = RHSVec->size();
291 unsigned LHSSize = LHSVec->size();
292
Chris Lattner93dc92e2010-03-22 20:56:36 +0000293 for (; i != LHSSize; ++i) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000294 if (i == RHSSize) return false; // RHS is shorter than LHS.
295 if ((*LHSVec)[i] != (*RHSVec)[i])
296 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
Chris Lattner93dc92e2010-03-22 20:56:36 +0000297 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000298
Bill Wendling023422a2008-11-13 12:03:00 +0000299 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000300
301 i = 0;
302 LHSVec = &LHS.second;
303 RHSVec = &RHS.second;
304 RHSSize = RHSVec->size();
305 LHSSize = LHSVec->size();
306
307 for (i = 0; i != LHSSize; ++i) {
308 if (i == RHSSize) return false; // RHS is shorter than LHS.
309 if ((*LHSVec)[i] != (*RHSVec)[i])
310 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
311 }
312
313 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000314 }
315 };
316}
317
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000318void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000319 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000320 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
321 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
322 OS << " switch (ID) {\n";
Craig Topper655b8de2012-02-05 07:21:30 +0000323 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000324
325 // This checking can emit a lot of very common code. To reduce the amount of
326 // code that we emit, batch up cases that have identical types. This avoids
327 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000328 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000329 MapTy UniqueArgInfos;
330
331 // Compute the unique argument type info.
332 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000333 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
334 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000335
336 // Loop through the array, emitting one comparison for each batch.
337 for (MapTy::iterator I = UniqueArgInfos.begin(),
338 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000339 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000340 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
341 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000342
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000343 const RecPair &ArgTypes = I->first;
344 const std::vector<Record*> &RetTys = ArgTypes.first;
345 const std::vector<Record*> &ParamTys = ArgTypes.second;
Bob Wilson09b13662009-07-29 16:35:59 +0000346 std::vector<unsigned> OverloadedTypeIndices;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000347
348 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
349 << ParamTys.size();
350
351 // Emit return types.
352 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
353 Record *ArgType = RetTys[j];
354 OS << ", ";
355
Chandler Carruth69940402007-08-04 01:51:18 +0000356 if (ArgType->isSubClassOf("LLVMMatchType")) {
357 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000358 assert(Number < OverloadedTypeIndices.size() &&
359 "Invalid matching number!");
360 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000361 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
362 OS << "~(ExtendedElementVectorType | " << Number << ")";
363 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
364 OS << "~(TruncatedElementVectorType | " << Number << ")";
365 else
366 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000367 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000369 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000370
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000371 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000372 OverloadedTypeIndices.push_back(j);
373
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000375 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000376 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000377 }
378
379 // Emit the parameter types.
380 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
381 Record *ArgType = ParamTys[j];
382 OS << ", ";
383
384 if (ArgType->isSubClassOf("LLVMMatchType")) {
385 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000386 assert(Number < OverloadedTypeIndices.size() &&
387 "Invalid matching number!");
388 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000389 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
390 OS << "~(ExtendedElementVectorType | " << Number << ")";
391 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
392 OS << "~(TruncatedElementVectorType | " << Number << ")";
393 else
394 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000395 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000397 OS << getEnumName(VT);
398
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000399 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000400 OverloadedTypeIndices.push_back(j + RetTys.size());
401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000403 throw "Var arg type not last argument";
404 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000405 }
406
Chandler Carruth69940402007-08-04 01:51:18 +0000407 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000408 OS << " break;\n";
409 }
410 OS << " }\n";
411 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000412}
413
Jim Laskey95af5922007-02-07 20:38:26 +0000414void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000415 raw_ostream &OS) {
Jim Laskey95af5922007-02-07 20:38:26 +0000416 OS << "// Code for generating Intrinsic function declarations.\n";
417 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
418 OS << " switch (id) {\n";
Craig Topper655b8de2012-02-05 07:21:30 +0000419 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000420
421 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
422 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000423 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000424 MapTy UniqueArgInfos;
425
426 // Compute the unique argument type info.
427 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000428 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
429 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000430
431 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000432 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
433
Jim Laskey95af5922007-02-07 20:38:26 +0000434 for (MapTy::iterator I = UniqueArgInfos.begin(),
435 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000436 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000437 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
438 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000439
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000440 const RecPair &ArgTypes = I->first;
441 const std::vector<Record*> &RetTys = ArgTypes.first;
442 const std::vector<Record*> &ParamTys = ArgTypes.second;
443
444 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000445
Chandler Carruth69940402007-08-04 01:51:18 +0000446 if (N > 1 &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000448 OS << " IsVarArg = true;\n";
449 --N;
450 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000451
Reid Spencer84c614d2007-05-22 19:30:31 +0000452 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000453 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000454 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000455 OS << ";\n";
456
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000457 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000458 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000459 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000460 OS << ");\n";
461 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000462
Jim Laskey95af5922007-02-07 20:38:26 +0000463 OS << " break;\n";
464 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000465
Jim Laskey95af5922007-02-07 20:38:26 +0000466 OS << " }\n";
467 OS << "#endif\n\n";
468}
469
John McCallbd0fa4c2011-05-28 06:31:34 +0000470namespace {
471 enum ModRefKind {
472 MRK_none,
473 MRK_readonly,
474 MRK_readnone
475 };
476
477 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
478 switch (intrinsic.ModRef) {
479 case CodeGenIntrinsic::NoMem:
480 return MRK_readnone;
481 case CodeGenIntrinsic::ReadArgMem:
482 case CodeGenIntrinsic::ReadMem:
483 return MRK_readonly;
484 case CodeGenIntrinsic::ReadWriteArgMem:
485 case CodeGenIntrinsic::ReadWriteMem:
486 return MRK_none;
487 }
Craig Topper655b8de2012-02-05 07:21:30 +0000488 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000489 }
490
491 struct AttributeComparator {
492 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
493 // Sort throwing intrinsics after non-throwing intrinsics.
494 if (L->canThrow != R->canThrow)
495 return R->canThrow;
496
497 // Try to order by readonly/readnone attribute.
498 ModRefKind LK = getModRefKind(*L);
499 ModRefKind RK = getModRefKind(*R);
500 if (LK != RK) return (LK > RK);
501
502 // Order by argument attributes.
503 // This is reliable because each side is already sorted internally.
504 return (L->ArgumentAttributes < R->ArgumentAttributes);
505 }
506 };
507}
508
Chris Lattner048ffb22009-01-12 01:18:58 +0000509/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000510void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000511EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000512 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
513 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000514 if (TargetOnly)
515 OS << "static AttrListPtr getAttributes(" << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000516 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000517 else
John McCallbd0fa4c2011-05-28 06:31:34 +0000518 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
519
Craig Topper1f595232012-02-28 06:32:00 +0000520 // Compute the maximum number of attribute arguments and the map
521 typedef std::map<const CodeGenIntrinsic*, unsigned,
522 AttributeComparator> UniqAttrMapTy;
523 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000524 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000525 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000526 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000527 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000528 maxArgAttrs =
529 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000530 unsigned &N = UniqAttributes[&intrinsic];
531 if (N) continue;
532 assert(AttrNum < 256 && "Too many unique attributes for table!");
533 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000534 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000535
536 // Emit an array of AttributeWithIndex. Most intrinsics will have
537 // at least one entry, for the function itself (index ~1), which is
538 // usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000539 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
540 OS << " 255, // Invalid intrinsic\n";
541
542 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
543 const CodeGenIntrinsic &intrinsic = Ints[i];
544
545 OS << " " << UniqAttributes[&intrinsic] << ", // "
546 << intrinsic.Name << "\n";
547 }
548 OS << " };\n\n";
549
John McCallbd0fa4c2011-05-28 06:31:34 +0000550 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000551 OS << " unsigned NumAttrs = 0;\n";
Craig Topper1f595232012-02-28 06:32:00 +0000552 OS << " switch(IntrinsicsToAttributesMap[id]) {\n";
553 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
554 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
555 E = UniqAttributes.end(); I != E; ++I) {
556 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000557
Craig Topper1f595232012-02-28 06:32:00 +0000558 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000559
560 // Keep track of the number of attributes we're writing out.
561 unsigned numAttrs = 0;
562
563 // The argument attributes are alreadys sorted by argument index.
564 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
565 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000566
John McCallbd0fa4c2011-05-28 06:31:34 +0000567 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
568 << argNo+1 << ", ";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000569
John McCallbd0fa4c2011-05-28 06:31:34 +0000570 bool moreThanOne = false;
571
572 do {
573 if (moreThanOne) OS << '|';
574
575 switch (intrinsic.ArgumentAttributes[ai].second) {
Chris Lattnerd4a27002009-01-12 02:41:37 +0000576 case CodeGenIntrinsic::NoCapture:
John McCallbd0fa4c2011-05-28 06:31:34 +0000577 OS << "Attribute::NoCapture";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000578 break;
579 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000580
581 ++ai;
582 moreThanOne = true;
583 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
584
585 OS << ");\n";
586 }
587
588 ModRefKind modRef = getModRefKind(intrinsic);
589
590 if (!intrinsic.canThrow || modRef) {
591 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
592 if (!intrinsic.canThrow) {
593 OS << "Attribute::NoUnwind";
594 if (modRef) OS << '|';
595 }
596 switch (modRef) {
597 case MRK_none: break;
598 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
599 case MRK_readnone: OS << "Attribute::ReadNone"; break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000600 }
601 OS << ");\n";
602 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000603
604 if (numAttrs) {
605 OS << " NumAttrs = " << numAttrs << ";\n";
606 OS << " break;\n";
607 } else {
608 OS << " return AttrListPtr();\n";
609 }
Chris Lattner10dae942009-01-12 01:27:55 +0000610 }
611
612 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000613 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000614 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000615 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000616}
Chris Lattner022f64f2006-03-13 23:08:44 +0000617
Duncan Sandsd869b382009-02-14 10:56:35 +0000618/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
619void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000620EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Duncan Sandsd869b382009-02-14 10:56:35 +0000621 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n";
622 OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n";
Duncan Sands7c422ac2010-01-06 08:45:52 +0000623 OS << "switch (iid) {\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000624 OS << "default:\n return UnknownModRefBehavior;\n";
625 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Dan Gohman7365c092010-08-05 23:36:21 +0000626 if (Ints[i].ModRef == CodeGenIntrinsic::ReadWriteMem)
Duncan Sandsd869b382009-02-14 10:56:35 +0000627 continue;
628 OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
629 << ":\n";
630 switch (Ints[i].ModRef) {
631 default:
632 assert(false && "Unknown Mod/Ref type!");
633 case CodeGenIntrinsic::NoMem:
634 OS << " return DoesNotAccessMemory;\n";
635 break;
636 case CodeGenIntrinsic::ReadArgMem:
Dan Gohmane88ccb52010-11-10 18:30:00 +0000637 OS << " return OnlyReadsArgumentPointees;\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000638 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000639 case CodeGenIntrinsic::ReadMem:
640 OS << " return OnlyReadsMemory;\n";
641 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000642 case CodeGenIntrinsic::ReadWriteArgMem:
Dan Gohmane88ccb52010-11-10 18:30:00 +0000643 OS << " return OnlyAccessesArgumentPointees;\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000644 break;
645 }
646 }
647 OS << "}\n";
648 OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
649}
650
Chris Lattner022f64f2006-03-13 23:08:44 +0000651void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000652EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Chris Lattner022f64f2006-03-13 23:08:44 +0000653 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
654 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
655 OS << " switch (F->getIntrinsicID()) {\n";
656 OS << " default: BuiltinName = \"\"; break;\n";
657 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
658 if (!Ints[i].GCCBuiltinName.empty()) {
659 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
660 << Ints[i].GCCBuiltinName << "\"; break;\n";
661 }
662 }
663 OS << " }\n";
664 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000665}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000666
Chris Lattner331bf922008-01-04 04:38:35 +0000667/// EmitTargetBuiltins - All of the builtins in the specified map are for the
668/// same target, and we already checked it.
669static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000670 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000671 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000672
Chris Lattner298b1762010-09-06 03:14:45 +0000673 std::vector<StringMatcher::StringPair> Results;
Chris Lattner331bf922008-01-04 04:38:35 +0000674
Chris Lattner298b1762010-09-06 03:14:45 +0000675 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
676 E = BIM.end(); I != E; ++I) {
677 std::string ResultCode =
678 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
679 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000680 }
Chris Lattner298b1762010-09-06 03:14:45 +0000681
682 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000683}
684
685
Chris Lattner3f8b8912006-03-15 01:33:26 +0000686void IntrinsicEmitter::
687EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000688 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000689 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000690 BIMTy BuiltinMap;
691 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
692 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000693 // Get the map for this target prefix.
694 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
695
696 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
697 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000698 throw "Intrinsic '" + Ints[i].TheDef->getName() +
699 "': duplicate GCC builtin name!";
700 }
701 }
702
703 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
704 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
705 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
706 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
707 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000708
709 if (TargetOnly) {
710 OS << "static " << TargetPrefix << "Intrinsic::ID "
711 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000712 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000713 } else {
714 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000715 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000716 }
717
Chris Lattner298b1762010-09-06 03:14:45 +0000718 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
719 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000720
Chris Lattner3f8b8912006-03-15 01:33:26 +0000721 // Note: this could emit significantly better code if we cared.
722 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000723 OS << " ";
724 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000725 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000726 else
727 OS << "/* Target Independent Builtins */ ";
728 OS << "{\n";
729
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000730 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000731 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000732 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000733 }
Chris Lattner298b1762010-09-06 03:14:45 +0000734 OS << " return ";
735 if (!TargetPrefix.empty())
736 OS << "(" << TargetPrefix << "Intrinsic::ID)";
737 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000738 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000739 OS << "#endif\n\n";
740}