blob: 578b3aa2439da54ad5d92c6476f23d2efb2a47f4 [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) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000163 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000164 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000165 OS << "static const uint8_t OTable[] = {\n";
166 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000167 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000168 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
169 if ((i+1)%8 == 0)
170 OS << ",\n 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000171 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000172 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000173 }
Benjamin Kramer36a21382012-03-01 02:16:57 +0000174 OS << "\n};\n\n";
175 // OTable contains a true bit at the position if the intrinsic is overloaded.
176 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000177 OS << "#endif\n\n";
178}
179
Owen Anderson825b72b2009-08-11 20:47:22 +0000180static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +0000181 if (EVT(VT).isInteger()) {
182 unsigned BitWidth = EVT(VT).getSizeInBits();
Owen Anderson1d0be152009-08-13 21:58:54 +0000183 OS << "IntegerType::get(Context, " << BitWidth << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 } else if (VT == MVT::Other) {
185 // MVT::OtherVT is used to mean the empty struct type here.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000186 OS << "StructType::get(Context)";
Dan Gohmane3376ec2011-12-20 00:02:33 +0000187 } else if (VT == MVT::f16) {
188 OS << "Type::getHalfTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 } else if (VT == MVT::f32) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000190 OS << "Type::getFloatTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 } else if (VT == MVT::f64) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000192 OS << "Type::getDoubleTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 } else if (VT == MVT::f80) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000194 OS << "Type::getX86_FP80Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 } else if (VT == MVT::f128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000196 OS << "Type::getFP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 } else if (VT == MVT::ppcf128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000198 OS << "Type::getPPC_FP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 } else if (VT == MVT::isVoid) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000200 OS << "Type::getVoidTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 } else if (VT == MVT::Metadata) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000202 OS << "Type::getMetadataTy(Context)";
Dale Johannesen8be45252010-09-11 00:16:46 +0000203 } else if (VT == MVT::x86mmx) {
204 OS << "Type::getX86_MMXTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000205 } else {
206 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000207 }
208}
209
Daniel Dunbar1a551802009-07-03 00:10:29 +0000210static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000211 unsigned &ArgNo);
212
Daniel Dunbar1a551802009-07-03 00:10:29 +0000213static void EmitTypeGenerate(raw_ostream &OS,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000214 const std::vector<Record*> &ArgTypes,
215 unsigned &ArgNo) {
Chris Lattner93dc92e2010-03-22 20:56:36 +0000216 if (ArgTypes.empty())
217 return EmitTypeForValueType(OS, MVT::isVoid);
218
219 if (ArgTypes.size() == 1)
220 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000221
Chris Lattnerb2318662011-06-18 22:48:56 +0000222 OS << "StructType::get(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000223
224 for (std::vector<Record*>::const_iterator
Bill Wendling20072af2008-11-13 10:18:35 +0000225 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000226 EmitTypeGenerate(OS, *I, ArgNo);
Bill Wendling20072af2008-11-13 10:18:35 +0000227 OS << ", ";
228 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000229
Bill Wendling20072af2008-11-13 10:18:35 +0000230 OS << " NULL)";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000231}
232
Daniel Dunbar1a551802009-07-03 00:10:29 +0000233static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000234 unsigned &ArgNo) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000236
237 if (ArgType->isSubClassOf("LLVMMatchType")) {
238 unsigned Number = ArgType->getValueAsInt("Number");
239 assert(Number < ArgNo && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000240 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
241 OS << "VectorType::getExtendedElementVectorType"
242 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
243 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
244 OS << "VectorType::getTruncatedElementVectorType"
245 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
246 else
247 OS << "Tys[" << Number << "]";
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000249 // NOTE: The ArgNo variable here is not the absolute argument number, it is
250 // the index of the "arbitrary" type in the Tys array passed to the
251 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000252 // increment it when we actually hit an overloaded type. Getting this wrong
253 // leads to very subtle bugs!
254 OS << "Tys[" << ArgNo++ << "]";
Owen Andersone50ed302009-08-10 22:56:29 +0000255 } else if (EVT(VT).isVector()) {
256 EVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000257 OS << "VectorType::get(";
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000259 OS << ", " << VVT.getVectorNumElements() << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000261 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000262 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000263 OS << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 } else if (VT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +0000265 // Make sure the user has passed us an argument type to overload. If not,
266 // treat it as an ordinary (not overloaded) intrinsic.
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000267 OS << "(" << ArgNo << " < Tys.size()) ? Tys[" << ArgNo
Mon P Wange3b3a722008-07-30 04:36:53 +0000268 << "] : PointerType::getUnqual(";
269 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
270 OS << ")";
271 ++ArgNo;
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 } else if (VT == MVT::isVoid) {
Chandler Carruth69940402007-08-04 01:51:18 +0000273 if (ArgNo == 0)
Owen Anderson1d0be152009-08-13 21:58:54 +0000274 OS << "Type::getVoidTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000275 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 // MVT::isVoid is used to mean varargs here.
Chandler Carruth69940402007-08-04 01:51:18 +0000277 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000278 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000279 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000280 }
281}
282
Jim Grosbachda4231f2009-03-26 16:17:51 +0000283/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000284/// records.
285namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000286 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000287 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000288 bool operator()(const RecPair &LHS,
289 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000290 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000291 const std::vector<Record*> *LHSVec = &LHS.first;
292 const std::vector<Record*> *RHSVec = &RHS.first;
293 unsigned RHSSize = RHSVec->size();
294 unsigned LHSSize = LHSVec->size();
295
Chris Lattner93dc92e2010-03-22 20:56:36 +0000296 for (; i != LHSSize; ++i) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000297 if (i == RHSSize) return false; // RHS is shorter than LHS.
298 if ((*LHSVec)[i] != (*RHSVec)[i])
299 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
Chris Lattner93dc92e2010-03-22 20:56:36 +0000300 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000301
Bill Wendling023422a2008-11-13 12:03:00 +0000302 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000303
304 i = 0;
305 LHSVec = &LHS.second;
306 RHSVec = &RHS.second;
307 RHSSize = RHSVec->size();
308 LHSSize = LHSVec->size();
309
310 for (i = 0; i != LHSSize; ++i) {
311 if (i == RHSSize) return false; // RHS is shorter than LHS.
312 if ((*LHSVec)[i] != (*RHSVec)[i])
313 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
314 }
315
316 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000317 }
318 };
319}
320
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000321void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000322 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000323 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
324 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
325 OS << " switch (ID) {\n";
Craig Topper655b8de2012-02-05 07:21:30 +0000326 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000327
328 // This checking can emit a lot of very common code. To reduce the amount of
329 // code that we emit, batch up cases that have identical types. This avoids
330 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000331 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000332 MapTy UniqueArgInfos;
333
334 // Compute the unique argument type info.
335 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000336 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
337 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000338
339 // Loop through the array, emitting one comparison for each batch.
340 for (MapTy::iterator I = UniqueArgInfos.begin(),
341 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000342 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000343 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
344 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000345
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000346 const RecPair &ArgTypes = I->first;
347 const std::vector<Record*> &RetTys = ArgTypes.first;
348 const std::vector<Record*> &ParamTys = ArgTypes.second;
Bob Wilson09b13662009-07-29 16:35:59 +0000349 std::vector<unsigned> OverloadedTypeIndices;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000350
351 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
352 << ParamTys.size();
353
354 // Emit return types.
355 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
356 Record *ArgType = RetTys[j];
357 OS << ", ";
358
Chandler Carruth69940402007-08-04 01:51:18 +0000359 if (ArgType->isSubClassOf("LLVMMatchType")) {
360 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000361 assert(Number < OverloadedTypeIndices.size() &&
362 "Invalid matching number!");
363 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000364 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
365 OS << "~(ExtendedElementVectorType | " << Number << ")";
366 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
367 OS << "~(TruncatedElementVectorType | " << Number << ")";
368 else
369 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000370 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000372 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000373
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000374 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000375 OverloadedTypeIndices.push_back(j);
376
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000378 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000379 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000380 }
381
382 // Emit the parameter types.
383 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
384 Record *ArgType = ParamTys[j];
385 OS << ", ";
386
387 if (ArgType->isSubClassOf("LLVMMatchType")) {
388 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000389 assert(Number < OverloadedTypeIndices.size() &&
390 "Invalid matching number!");
391 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000392 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
393 OS << "~(ExtendedElementVectorType | " << Number << ")";
394 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
395 OS << "~(TruncatedElementVectorType | " << Number << ")";
396 else
397 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000398 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000400 OS << getEnumName(VT);
401
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000402 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000403 OverloadedTypeIndices.push_back(j + RetTys.size());
404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000406 throw "Var arg type not last argument";
407 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000408 }
409
Chandler Carruth69940402007-08-04 01:51:18 +0000410 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000411 OS << " break;\n";
412 }
413 OS << " }\n";
414 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000415}
416
Jim Laskey95af5922007-02-07 20:38:26 +0000417void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000418 raw_ostream &OS) {
Jim Laskey95af5922007-02-07 20:38:26 +0000419 OS << "// Code for generating Intrinsic function declarations.\n";
420 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
421 OS << " switch (id) {\n";
Craig Topper655b8de2012-02-05 07:21:30 +0000422 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000423
424 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
425 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000426 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000427 MapTy UniqueArgInfos;
428
429 // Compute the unique argument type info.
430 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000431 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
432 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000433
434 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000435 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
436
Jim Laskey95af5922007-02-07 20:38:26 +0000437 for (MapTy::iterator I = UniqueArgInfos.begin(),
438 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000439 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000440 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
441 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000442
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000443 const RecPair &ArgTypes = I->first;
444 const std::vector<Record*> &RetTys = ArgTypes.first;
445 const std::vector<Record*> &ParamTys = ArgTypes.second;
446
447 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000448
Chandler Carruth69940402007-08-04 01:51:18 +0000449 if (N > 1 &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000451 OS << " IsVarArg = true;\n";
452 --N;
453 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000454
Reid Spencer84c614d2007-05-22 19:30:31 +0000455 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000456 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000457 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000458 OS << ";\n";
459
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000460 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000461 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000462 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000463 OS << ");\n";
464 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000465
Jim Laskey95af5922007-02-07 20:38:26 +0000466 OS << " break;\n";
467 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000468
Jim Laskey95af5922007-02-07 20:38:26 +0000469 OS << " }\n";
470 OS << "#endif\n\n";
471}
472
John McCallbd0fa4c2011-05-28 06:31:34 +0000473namespace {
474 enum ModRefKind {
475 MRK_none,
476 MRK_readonly,
477 MRK_readnone
478 };
479
480 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
481 switch (intrinsic.ModRef) {
482 case CodeGenIntrinsic::NoMem:
483 return MRK_readnone;
484 case CodeGenIntrinsic::ReadArgMem:
485 case CodeGenIntrinsic::ReadMem:
486 return MRK_readonly;
487 case CodeGenIntrinsic::ReadWriteArgMem:
488 case CodeGenIntrinsic::ReadWriteMem:
489 return MRK_none;
490 }
Craig Topper655b8de2012-02-05 07:21:30 +0000491 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000492 }
493
494 struct AttributeComparator {
495 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
496 // Sort throwing intrinsics after non-throwing intrinsics.
497 if (L->canThrow != R->canThrow)
498 return R->canThrow;
499
500 // Try to order by readonly/readnone attribute.
501 ModRefKind LK = getModRefKind(*L);
502 ModRefKind RK = getModRefKind(*R);
503 if (LK != RK) return (LK > RK);
504
505 // Order by argument attributes.
506 // This is reliable because each side is already sorted internally.
507 return (L->ArgumentAttributes < R->ArgumentAttributes);
508 }
509 };
510}
511
Chris Lattner048ffb22009-01-12 01:18:58 +0000512/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000513void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000514EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000515 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
516 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000517 if (TargetOnly)
518 OS << "static AttrListPtr getAttributes(" << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000519 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000520 else
John McCallbd0fa4c2011-05-28 06:31:34 +0000521 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
522
Craig Topper1f595232012-02-28 06:32:00 +0000523 // Compute the maximum number of attribute arguments and the map
524 typedef std::map<const CodeGenIntrinsic*, unsigned,
525 AttributeComparator> UniqAttrMapTy;
526 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000527 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000528 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000529 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000530 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000531 maxArgAttrs =
532 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000533 unsigned &N = UniqAttributes[&intrinsic];
534 if (N) continue;
535 assert(AttrNum < 256 && "Too many unique attributes for table!");
536 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000537 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000538
539 // Emit an array of AttributeWithIndex. Most intrinsics will have
540 // at least one entry, for the function itself (index ~1), which is
541 // usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000542 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
543 OS << " 255, // Invalid intrinsic\n";
544
545 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
546 const CodeGenIntrinsic &intrinsic = Ints[i];
547
548 OS << " " << UniqAttributes[&intrinsic] << ", // "
549 << intrinsic.Name << "\n";
550 }
551 OS << " };\n\n";
552
John McCallbd0fa4c2011-05-28 06:31:34 +0000553 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000554 OS << " unsigned NumAttrs = 0;\n";
Craig Topper1f595232012-02-28 06:32:00 +0000555 OS << " switch(IntrinsicsToAttributesMap[id]) {\n";
556 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
557 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
558 E = UniqAttributes.end(); I != E; ++I) {
559 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000560
Craig Topper1f595232012-02-28 06:32:00 +0000561 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000562
563 // Keep track of the number of attributes we're writing out.
564 unsigned numAttrs = 0;
565
566 // The argument attributes are alreadys sorted by argument index.
567 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
568 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000569
John McCallbd0fa4c2011-05-28 06:31:34 +0000570 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
571 << argNo+1 << ", ";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000572
John McCallbd0fa4c2011-05-28 06:31:34 +0000573 bool moreThanOne = false;
574
575 do {
576 if (moreThanOne) OS << '|';
577
578 switch (intrinsic.ArgumentAttributes[ai].second) {
Chris Lattnerd4a27002009-01-12 02:41:37 +0000579 case CodeGenIntrinsic::NoCapture:
John McCallbd0fa4c2011-05-28 06:31:34 +0000580 OS << "Attribute::NoCapture";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000581 break;
582 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000583
584 ++ai;
585 moreThanOne = true;
586 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
587
588 OS << ");\n";
589 }
590
591 ModRefKind modRef = getModRefKind(intrinsic);
592
593 if (!intrinsic.canThrow || modRef) {
594 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
595 if (!intrinsic.canThrow) {
596 OS << "Attribute::NoUnwind";
597 if (modRef) OS << '|';
598 }
599 switch (modRef) {
600 case MRK_none: break;
601 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
602 case MRK_readnone: OS << "Attribute::ReadNone"; break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000603 }
604 OS << ");\n";
605 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000606
607 if (numAttrs) {
608 OS << " NumAttrs = " << numAttrs << ";\n";
609 OS << " break;\n";
610 } else {
611 OS << " return AttrListPtr();\n";
612 }
Chris Lattner10dae942009-01-12 01:27:55 +0000613 }
614
615 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000616 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000617 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000618 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000619}
Chris Lattner022f64f2006-03-13 23:08:44 +0000620
Duncan Sandsd869b382009-02-14 10:56:35 +0000621/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
622void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000623EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000624 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
625 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
626 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
627 << "\"Unknown intrinsic.\");\n\n";
628
629 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
630 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000631 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000632 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sandsd869b382009-02-14 10:56:35 +0000633 switch (Ints[i].ModRef) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000634 case CodeGenIntrinsic::NoMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000635 OS << "DoesNotAccessMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000636 break;
637 case CodeGenIntrinsic::ReadArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000638 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000639 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000640 case CodeGenIntrinsic::ReadMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000641 OS << "OnlyReadsMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000642 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000643 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000644 OS << "OnlyAccessesArgumentPointees,\n";
645 break;
646 case CodeGenIntrinsic::ReadWriteMem:
647 OS << "UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000648 break;
649 }
650 }
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000651 OS << "};\n\n"
652 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
653 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000654}
655
Chris Lattner022f64f2006-03-13 23:08:44 +0000656void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000657EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Chris Lattner022f64f2006-03-13 23:08:44 +0000658 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
659 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
660 OS << " switch (F->getIntrinsicID()) {\n";
661 OS << " default: BuiltinName = \"\"; break;\n";
662 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
663 if (!Ints[i].GCCBuiltinName.empty()) {
664 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
665 << Ints[i].GCCBuiltinName << "\"; break;\n";
666 }
667 }
668 OS << " }\n";
669 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000670}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000671
Chris Lattner331bf922008-01-04 04:38:35 +0000672/// EmitTargetBuiltins - All of the builtins in the specified map are for the
673/// same target, and we already checked it.
674static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000675 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000676 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000677
Chris Lattner298b1762010-09-06 03:14:45 +0000678 std::vector<StringMatcher::StringPair> Results;
Chris Lattner331bf922008-01-04 04:38:35 +0000679
Chris Lattner298b1762010-09-06 03:14:45 +0000680 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
681 E = BIM.end(); I != E; ++I) {
682 std::string ResultCode =
683 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
684 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000685 }
Chris Lattner298b1762010-09-06 03:14:45 +0000686
687 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000688}
689
690
Chris Lattner3f8b8912006-03-15 01:33:26 +0000691void IntrinsicEmitter::
692EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000693 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000694 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000695 BIMTy BuiltinMap;
696 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
697 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000698 // Get the map for this target prefix.
699 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
700
701 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
702 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000703 throw "Intrinsic '" + Ints[i].TheDef->getName() +
704 "': duplicate GCC builtin name!";
705 }
706 }
707
708 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
709 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
710 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
711 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
712 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000713
714 if (TargetOnly) {
715 OS << "static " << TargetPrefix << "Intrinsic::ID "
716 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000717 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000718 } else {
719 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000720 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000721 }
722
Chris Lattner298b1762010-09-06 03:14:45 +0000723 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
724 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000725
Chris Lattner3f8b8912006-03-15 01:33:26 +0000726 // Note: this could emit significantly better code if we cared.
727 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000728 OS << " ";
729 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000730 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000731 else
732 OS << "/* Target Independent Builtins */ ";
733 OS << "{\n";
734
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000735 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000736 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000737 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000738 }
Chris Lattner298b1762010-09-06 03:14:45 +0000739 OS << " return ";
740 if (!TargetPrefix.empty())
741 OS << "(" << TargetPrefix << "Intrinsic::ID)";
742 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000743 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000744 OS << "#endif\n\n";
745}