blob: cd992c8181cd8c99bbe12c1a05a0960f47bc0d73 [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
Douglas Gregor7d9663c2010-05-11 06:17:44 +000033 EmitPrefix(OS);
34
Chris Lattner9e493cf2006-03-03 02:32:46 +000035 // Emit the enum information.
36 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000037
38 // Emit the intrinsic ID -> name table.
39 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000040
41 // Emit the intrinsic ID -> overload table.
42 EmitIntrinsicToOverloadTable(Ints, OS);
43
Chris Lattner9b843b22006-03-09 20:34:19 +000044 // Emit the function name recognizer.
45 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000046
Chris Lattnerf97a00e2006-03-09 22:05:04 +000047 // Emit the intrinsic verifier.
48 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000049
Jim Laskey95af5922007-02-07 20:38:26 +000050 // Emit the intrinsic declaration generator.
51 EmitGenerator(Ints, OS);
52
Duncan Sandsa3355ff2007-12-03 20:06:50 +000053 // Emit the intrinsic parameter attributes.
54 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000055
Duncan Sandsd869b382009-02-14 10:56:35 +000056 // Emit intrinsic alias analysis mod/ref behavior.
57 EmitModRefBehavior(Ints, OS);
58
Chris Lattner022f64f2006-03-13 23:08:44 +000059 // Emit a list of intrinsics with corresponding GCC builtins.
60 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000061
62 // Emit code to translate GCC builtins into LLVM intrinsics.
63 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor7d9663c2010-05-11 06:17:44 +000064
65 EmitSuffix(OS);
66}
67
68void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
69 OS << "// VisualStudio defines setjmp as _setjmp\n"
70 "#if defined(_MSC_VER) && defined(setjmp)\n"
71 "#define setjmp_undefined_for_visual_studio\n"
72 "#undef setjmp\n"
73 "#endif\n\n";
74}
75
76void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
77 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_visual_studio)\n"
78 "// let's return it to _setjmp state\n"
79 "#define setjmp _setjmp\n"
80 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000081}
82
83void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000084 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000085 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000086 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
87 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
88 OS << " " << Ints[i].EnumName;
89 OS << ((i != e-1) ? ", " : " ");
90 OS << std::string(40-Ints[i].EnumName.size(), ' ')
91 << "// " << Ints[i].Name << "\n";
92 }
93 OS << "#endif\n\n";
94}
Chris Lattner9b843b22006-03-09 20:34:19 +000095
96void IntrinsicEmitter::
97EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000098 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000099 // Build a function name -> intrinsic name mapping.
Reid Spencerc4de3de2007-04-01 07:20:02 +0000100 std::map<std::string, unsigned> IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000101 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattner2738ff92010-09-06 01:44:44 +0000102 IntMapping[Ints[i].Name.substr(5)] = i;
Chris Lattner9b843b22006-03-09 20:34:19 +0000103
104 OS << "// Function name -> enum value recognizer code.\n";
105 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattner2738ff92010-09-06 01:44:44 +0000106 OS << " Name += 5; Len -= 5; // Skip over 'llvm.'\n";
107 OS << " switch (*Name) { // Dispatch on first letter.\n";
Chris Lattner3b515802007-02-15 19:17:16 +0000108 OS << " default:\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000109 // Emit the intrinsics in sorted order.
110 char LastChar = 0;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000111 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000112 E = IntMapping.end(); I != E; ++I) {
Chris Lattner2738ff92010-09-06 01:44:44 +0000113 if (I->first[0] != LastChar) {
114 LastChar = I->first[0];
Chris Lattner3b515802007-02-15 19:17:16 +0000115 OS << " break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000116 OS << " case '" << LastChar << "':\n";
117 }
118
Reid Spencerc4de3de2007-04-01 07:20:02 +0000119 // For overloaded intrinsics, only the prefix needs to match
Chris Lattner2738ff92010-09-06 01:44:44 +0000120 std::string TheStr = I->first;
121 if (Ints[I->second].isOverloaded) {
122 TheStr += '.'; // Require "bswap." instead of bswap.
123 OS << " if (Len > " << I->first.size();
124 } else {
125 OS << " if (Len == " << I->first.size();
126 }
127
128 OS << " && !memcmp(Name, \"" << TheStr << "\", "
129 << TheStr.size() << ")) return " << TargetPrefix << "Intrinsic::"
Chandler Carruth69940402007-08-04 01:51:18 +0000130 << Ints[I->second].EnumName << ";\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000131 }
132 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000133 OS << "#endif\n\n";
134}
135
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000136void IntrinsicEmitter::
137EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000138 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000139 OS << "// Intrinsic ID to name table\n";
140 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
141 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000142 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
143 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000144 OS << "#endif\n\n";
145}
146
Mon P Wang0d52ff12009-02-24 23:17:49 +0000147void IntrinsicEmitter::
148EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000149 raw_ostream &OS) {
Mon P Wang0d52ff12009-02-24 23:17:49 +0000150 OS << "// Intrinsic ID to overload table\n";
151 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
152 OS << " // Note that entry #0 is the invalid intrinsic!\n";
153 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
154 OS << " ";
155 if (Ints[i].isOverloaded)
156 OS << "true";
157 else
158 OS << "false";
159 OS << ",\n";
160 }
161 OS << "#endif\n\n";
162}
163
Owen Anderson825b72b2009-08-11 20:47:22 +0000164static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +0000165 if (EVT(VT).isInteger()) {
166 unsigned BitWidth = EVT(VT).getSizeInBits();
Owen Anderson1d0be152009-08-13 21:58:54 +0000167 OS << "IntegerType::get(Context, " << BitWidth << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 } else if (VT == MVT::Other) {
169 // MVT::OtherVT is used to mean the empty struct type here.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000170 OS << "StructType::get(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 } else if (VT == MVT::f32) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000172 OS << "Type::getFloatTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 } else if (VT == MVT::f64) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000174 OS << "Type::getDoubleTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 } else if (VT == MVT::f80) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000176 OS << "Type::getX86_FP80Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 } else if (VT == MVT::f128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000178 OS << "Type::getFP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 } else if (VT == MVT::ppcf128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000180 OS << "Type::getPPC_FP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 } else if (VT == MVT::isVoid) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000182 OS << "Type::getVoidTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 } else if (VT == MVT::Metadata) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000184 OS << "Type::getMetadataTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000185 } else {
186 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000187 }
188}
189
Daniel Dunbar1a551802009-07-03 00:10:29 +0000190static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000191 unsigned &ArgNo);
192
Daniel Dunbar1a551802009-07-03 00:10:29 +0000193static void EmitTypeGenerate(raw_ostream &OS,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000194 const std::vector<Record*> &ArgTypes,
195 unsigned &ArgNo) {
Chris Lattner93dc92e2010-03-22 20:56:36 +0000196 if (ArgTypes.empty())
197 return EmitTypeForValueType(OS, MVT::isVoid);
198
199 if (ArgTypes.size() == 1)
200 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000201
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000202 OS << "StructType::get(Context, ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000203
204 for (std::vector<Record*>::const_iterator
Bill Wendling20072af2008-11-13 10:18:35 +0000205 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000206 EmitTypeGenerate(OS, *I, ArgNo);
Bill Wendling20072af2008-11-13 10:18:35 +0000207 OS << ", ";
208 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000209
Bill Wendling20072af2008-11-13 10:18:35 +0000210 OS << " NULL)";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000211}
212
Daniel Dunbar1a551802009-07-03 00:10:29 +0000213static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000214 unsigned &ArgNo) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000216
217 if (ArgType->isSubClassOf("LLVMMatchType")) {
218 unsigned Number = ArgType->getValueAsInt("Number");
219 assert(Number < ArgNo && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000220 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
221 OS << "VectorType::getExtendedElementVectorType"
222 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
223 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
224 OS << "VectorType::getTruncatedElementVectorType"
225 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
226 else
227 OS << "Tys[" << Number << "]";
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000229 // NOTE: The ArgNo variable here is not the absolute argument number, it is
230 // the index of the "arbitrary" type in the Tys array passed to the
231 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000232 // increment it when we actually hit an overloaded type. Getting this wrong
233 // leads to very subtle bugs!
234 OS << "Tys[" << ArgNo++ << "]";
Owen Andersone50ed302009-08-10 22:56:29 +0000235 } else if (EVT(VT).isVector()) {
236 EVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000237 OS << "VectorType::get(";
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000239 OS << ", " << VVT.getVectorNumElements() << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000241 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000242 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000243 OS << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 } else if (VT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +0000245 // Make sure the user has passed us an argument type to overload. If not,
246 // treat it as an ordinary (not overloaded) intrinsic.
247 OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
248 << "] : PointerType::getUnqual(";
249 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
250 OS << ")";
251 ++ArgNo;
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 } else if (VT == MVT::isVoid) {
Chandler Carruth69940402007-08-04 01:51:18 +0000253 if (ArgNo == 0)
Owen Anderson1d0be152009-08-13 21:58:54 +0000254 OS << "Type::getVoidTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000255 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 // MVT::isVoid is used to mean varargs here.
Chandler Carruth69940402007-08-04 01:51:18 +0000257 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000258 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000259 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000260 }
261}
262
Jim Grosbachda4231f2009-03-26 16:17:51 +0000263/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000264/// records.
265namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000266 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000267 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000268 bool operator()(const RecPair &LHS,
269 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000270 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000271 const std::vector<Record*> *LHSVec = &LHS.first;
272 const std::vector<Record*> *RHSVec = &RHS.first;
273 unsigned RHSSize = RHSVec->size();
274 unsigned LHSSize = LHSVec->size();
275
Chris Lattner93dc92e2010-03-22 20:56:36 +0000276 for (; i != LHSSize; ++i) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000277 if (i == RHSSize) return false; // RHS is shorter than LHS.
278 if ((*LHSVec)[i] != (*RHSVec)[i])
279 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
Chris Lattner93dc92e2010-03-22 20:56:36 +0000280 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000281
Bill Wendling023422a2008-11-13 12:03:00 +0000282 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000283
284 i = 0;
285 LHSVec = &LHS.second;
286 RHSVec = &RHS.second;
287 RHSSize = RHSVec->size();
288 LHSSize = LHSVec->size();
289
290 for (i = 0; i != LHSSize; ++i) {
291 if (i == RHSSize) return false; // RHS is shorter than LHS.
292 if ((*LHSVec)[i] != (*RHSVec)[i])
293 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
294 }
295
296 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000297 }
298 };
299}
300
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000301void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000302 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000303 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
304 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
305 OS << " switch (ID) {\n";
306 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000307
308 // This checking can emit a lot of very common code. To reduce the amount of
309 // code that we emit, batch up cases that have identical types. This avoids
310 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000311 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000312 MapTy UniqueArgInfos;
313
314 // Compute the unique argument type info.
315 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000316 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
317 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000318
319 // Loop through the array, emitting one comparison for each batch.
320 for (MapTy::iterator I = UniqueArgInfos.begin(),
321 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000322 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000323 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
324 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000325
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000326 const RecPair &ArgTypes = I->first;
327 const std::vector<Record*> &RetTys = ArgTypes.first;
328 const std::vector<Record*> &ParamTys = ArgTypes.second;
Bob Wilson09b13662009-07-29 16:35:59 +0000329 std::vector<unsigned> OverloadedTypeIndices;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000330
331 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
332 << ParamTys.size();
333
334 // Emit return types.
335 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
336 Record *ArgType = RetTys[j];
337 OS << ", ";
338
Chandler Carruth69940402007-08-04 01:51:18 +0000339 if (ArgType->isSubClassOf("LLVMMatchType")) {
340 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000341 assert(Number < OverloadedTypeIndices.size() &&
342 "Invalid matching number!");
343 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000344 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
345 OS << "~(ExtendedElementVectorType | " << Number << ")";
346 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
347 OS << "~(TruncatedElementVectorType | " << Number << ")";
348 else
349 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000350 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000352 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000353
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000354 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000355 OverloadedTypeIndices.push_back(j);
356
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000358 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000359 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000360 }
361
362 // Emit the parameter types.
363 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
364 Record *ArgType = ParamTys[j];
365 OS << ", ";
366
367 if (ArgType->isSubClassOf("LLVMMatchType")) {
368 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000369 assert(Number < OverloadedTypeIndices.size() &&
370 "Invalid matching number!");
371 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000372 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
373 OS << "~(ExtendedElementVectorType | " << Number << ")";
374 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
375 OS << "~(TruncatedElementVectorType | " << Number << ")";
376 else
377 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000378 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000380 OS << getEnumName(VT);
381
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000382 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000383 OverloadedTypeIndices.push_back(j + RetTys.size());
384
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000386 throw "Var arg type not last argument";
387 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000388 }
389
Chandler Carruth69940402007-08-04 01:51:18 +0000390 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000391 OS << " break;\n";
392 }
393 OS << " }\n";
394 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000395}
396
Jim Laskey95af5922007-02-07 20:38:26 +0000397void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000398 raw_ostream &OS) {
Jim Laskey95af5922007-02-07 20:38:26 +0000399 OS << "// Code for generating Intrinsic function declarations.\n";
400 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
401 OS << " switch (id) {\n";
402 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
403
404 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
405 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000406 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000407 MapTy UniqueArgInfos;
408
409 // Compute the unique argument type info.
410 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000411 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
412 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000413
414 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000415 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
416
Jim Laskey95af5922007-02-07 20:38:26 +0000417 for (MapTy::iterator I = UniqueArgInfos.begin(),
418 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000419 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000420 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
421 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000422
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000423 const RecPair &ArgTypes = I->first;
424 const std::vector<Record*> &RetTys = ArgTypes.first;
425 const std::vector<Record*> &ParamTys = ArgTypes.second;
426
427 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000428
Chandler Carruth69940402007-08-04 01:51:18 +0000429 if (N > 1 &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000431 OS << " IsVarArg = true;\n";
432 --N;
433 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000434
Reid Spencer84c614d2007-05-22 19:30:31 +0000435 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000436 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000437 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000438 OS << ";\n";
439
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000440 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000441 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000442 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000443 OS << ");\n";
444 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000445
Jim Laskey95af5922007-02-07 20:38:26 +0000446 OS << " break;\n";
447 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000448
Jim Laskey95af5922007-02-07 20:38:26 +0000449 OS << " }\n";
450 OS << "#endif\n\n";
451}
452
Chris Lattner048ffb22009-01-12 01:18:58 +0000453/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000454void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000455EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000456 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
457 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000458 if (TargetOnly)
459 OS << "static AttrListPtr getAttributes(" << TargetPrefix
460 << "Intrinsic::ID id) {";
461 else
462 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {";
Chris Lattner048ffb22009-01-12 01:18:58 +0000463 OS << " // No intrinsic can throw exceptions.\n";
464 OS << " Attributes Attr = Attribute::NoUnwind;\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000465 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000466 OS << " default: break;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000467 unsigned MaxArgAttrs = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000468 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner10dae942009-01-12 01:27:55 +0000469 MaxArgAttrs =
470 std::max(MaxArgAttrs, unsigned(Ints[i].ArgumentAttributes.size()));
Chris Lattner7056de32006-03-24 01:13:55 +0000471 switch (Ints[i].ModRef) {
472 default: break;
473 case CodeGenIntrinsic::NoMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000474 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
475 << ":\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000476 break;
477 }
478 }
Devang Patel05988662008-09-25 21:00:45 +0000479 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000480 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000481 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
482 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000483 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000484 case CodeGenIntrinsic::ReadArgMem:
485 case CodeGenIntrinsic::ReadMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000486 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
487 << ":\n";
Chris Lattner022f64f2006-03-13 23:08:44 +0000488 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000489 }
490 }
Devang Patel05988662008-09-25 21:00:45 +0000491 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000492 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000493 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000494 OS << " AttributeWithIndex AWI[" << MaxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000495 OS << " unsigned NumAttrs = 0;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000496 OS << " switch (id) {\n";
497 OS << " default: break;\n";
498
499 // Add argument attributes for any intrinsics that have them.
500 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
501 if (Ints[i].ArgumentAttributes.empty()) continue;
502
Dale Johannesen49de9822009-02-05 01:49:45 +0000503 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
504 << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000505
506 std::vector<std::pair<unsigned, CodeGenIntrinsic::ArgAttribute> > ArgAttrs =
507 Ints[i].ArgumentAttributes;
508 // Sort by argument index.
509 std::sort(ArgAttrs.begin(), ArgAttrs.end());
510
511 unsigned NumArgsWithAttrs = 0;
512
Chris Lattnerd4a27002009-01-12 02:41:37 +0000513 while (!ArgAttrs.empty()) {
514 unsigned ArgNo = ArgAttrs[0].first;
515
516 OS << " AWI[" << NumArgsWithAttrs++ << "] = AttributeWithIndex::get("
517 << ArgNo+1 << ", 0";
518
519 while (!ArgAttrs.empty() && ArgAttrs[0].first == ArgNo) {
520 switch (ArgAttrs[0].second) {
521 default: assert(0 && "Unknown arg attribute");
522 case CodeGenIntrinsic::NoCapture:
523 OS << "|Attribute::NoCapture";
524 break;
525 }
526 ArgAttrs.erase(ArgAttrs.begin());
527 }
528 OS << ");\n";
529 }
Chris Lattner10dae942009-01-12 01:27:55 +0000530
Chris Lattnerd4a27002009-01-12 02:41:37 +0000531 OS << " NumAttrs = " << NumArgsWithAttrs << ";\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000532 OS << " break;\n";
533 }
534
535 OS << " }\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000536 OS << " AWI[NumAttrs] = AttributeWithIndex::get(~0, Attr);\n";
537 OS << " return AttrListPtr::get(AWI, NumAttrs+1);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000538 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000539 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000540}
Chris Lattner022f64f2006-03-13 23:08:44 +0000541
Duncan Sandsd869b382009-02-14 10:56:35 +0000542/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
543void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000544EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Duncan Sandsd869b382009-02-14 10:56:35 +0000545 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n";
546 OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n";
Duncan Sands7c422ac2010-01-06 08:45:52 +0000547 OS << "switch (iid) {\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000548 OS << "default:\n return UnknownModRefBehavior;\n";
549 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Dan Gohman7365c092010-08-05 23:36:21 +0000550 if (Ints[i].ModRef == CodeGenIntrinsic::ReadWriteMem)
Duncan Sandsd869b382009-02-14 10:56:35 +0000551 continue;
552 OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
553 << ":\n";
554 switch (Ints[i].ModRef) {
555 default:
556 assert(false && "Unknown Mod/Ref type!");
557 case CodeGenIntrinsic::NoMem:
558 OS << " return DoesNotAccessMemory;\n";
559 break;
560 case CodeGenIntrinsic::ReadArgMem:
561 case CodeGenIntrinsic::ReadMem:
562 OS << " return OnlyReadsMemory;\n";
563 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000564 case CodeGenIntrinsic::ReadWriteArgMem:
Duncan Sandsd869b382009-02-14 10:56:35 +0000565 OS << " return AccessesArguments;\n";
566 break;
567 }
568 }
569 OS << "}\n";
570 OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
571}
572
Chris Lattner022f64f2006-03-13 23:08:44 +0000573void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000574EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Chris Lattner022f64f2006-03-13 23:08:44 +0000575 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
576 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
577 OS << " switch (F->getIntrinsicID()) {\n";
578 OS << " default: BuiltinName = \"\"; break;\n";
579 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
580 if (!Ints[i].GCCBuiltinName.empty()) {
581 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
582 << Ints[i].GCCBuiltinName << "\"; break;\n";
583 }
584 }
585 OS << " }\n";
586 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000587}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000588
Chris Lattner331bf922008-01-04 04:38:35 +0000589/// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
590/// sorted range of builtin names is equal to the current builtin. This breaks
591/// it down into a simple tree.
592///
593/// At this point, we know that all the builtins in the range have the same name
594/// for the first 'CharStart' characters. Only the end of the name needs to be
595/// discriminated.
596typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
597static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
598 unsigned CharStart, unsigned Indent,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000599 std::string TargetPrefix, raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000600 if (Start == End) return; // empty range.
601
602 // Determine what, if anything, is the same about all these strings.
603 std::string CommonString = Start->first;
604 unsigned NumInRange = 0;
605 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
606 // Find the first character that doesn't match.
607 const std::string &ThisStr = I->first;
608 unsigned NonMatchChar = CharStart;
609 while (NonMatchChar < CommonString.size() &&
610 NonMatchChar < ThisStr.size() &&
611 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
612 ++NonMatchChar;
613 // Truncate off pieces that don't match.
614 CommonString.resize(NonMatchChar);
615 }
616
617 // Just compare the rest of the string.
618 if (NumInRange == 1) {
619 if (CharStart != CommonString.size()) {
620 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
621 if (CharStart) OS << "+" << CharStart;
622 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
623 OS << CommonString.size() - CharStart << "))\n";
624 ++Indent;
625 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000626 OS << std::string(Indent*2, ' ') << "IntrinsicID = " << TargetPrefix
627 << "Intrinsic::";
Chris Lattner331bf922008-01-04 04:38:35 +0000628 OS << Start->second << ";\n";
629 return;
630 }
631
632 // At this point, we potentially have a common prefix for these builtins, emit
633 // a check for this common prefix.
634 if (CommonString.size() != CharStart) {
635 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
636 if (CharStart) OS << "+" << CharStart;
637 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
638 OS << CommonString.size()-CharStart << ")) {\n";
639
Dale Johannesen49de9822009-02-05 01:49:45 +0000640 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1,
641 TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000642 OS << std::string(Indent*2, ' ') << "}\n";
643 return;
644 }
645
646 // Output a switch on the character that differs across the set.
647 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
648 << "]) {";
649 if (CharStart)
650 OS << " // \"" << std::string(Start->first.begin(),
651 Start->first.begin()+CharStart) << "\"";
652 OS << "\n";
653
654 for (StrMapIterator I = Start; I != End; ) {
655 char ThisChar = I->first[CharStart];
656 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
657 // Figure out the range that has this common character.
658 StrMapIterator NextChar = I;
659 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
660 ++NextChar)
661 /*empty*/;
Dale Johannesen49de9822009-02-05 01:49:45 +0000662 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, TargetPrefix,OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000663 OS << std::string(Indent*2, ' ') << " break;\n";
664 I = NextChar;
665 }
666 OS << std::string(Indent*2, ' ') << "}\n";
667}
668
669/// EmitTargetBuiltins - All of the builtins in the specified map are for the
670/// same target, and we already checked it.
671static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000672 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000673 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000674 // Rearrange the builtins by length.
675 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
676 BuiltinsByLen.reserve(100);
677
678 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
679 if (I->first.size() >= BuiltinsByLen.size())
680 BuiltinsByLen.resize(I->first.size()+1);
681 BuiltinsByLen[I->first.size()].insert(*I);
682 }
683
684 // Now that we have all the builtins by their length, emit a switch stmt.
685 OS << " switch (strlen(BuiltinName)) {\n";
686 OS << " default: break;\n";
687 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
688 if (BuiltinsByLen[i].empty()) continue;
689 OS << " case " << i << ":\n";
690 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
Dale Johannesen49de9822009-02-05 01:49:45 +0000691 0, 3, TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000692 OS << " break;\n";
693 }
694 OS << " }\n";
695}
696
697
Chris Lattner3f8b8912006-03-15 01:33:26 +0000698void IntrinsicEmitter::
699EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000700 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000701 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000702 BIMTy BuiltinMap;
703 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
704 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000705 // Get the map for this target prefix.
706 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
707
708 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
709 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000710 throw "Intrinsic '" + Ints[i].TheDef->getName() +
711 "': duplicate GCC builtin name!";
712 }
713 }
714
715 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
716 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
717 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
718 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
719 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000720
721 if (TargetOnly) {
722 OS << "static " << TargetPrefix << "Intrinsic::ID "
723 << "getIntrinsicForGCCBuiltin(const char "
724 << "*TargetPrefix, const char *BuiltinName) {\n";
725 OS << " " << TargetPrefix << "Intrinsic::ID IntrinsicID = ";
726 } else {
727 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
728 << "*TargetPrefix, const char *BuiltinName) {\n";
729 OS << " Intrinsic::ID IntrinsicID = ";
730 }
731
732 if (TargetOnly)
733 OS << "(" << TargetPrefix<< "Intrinsic::ID)";
734
735 OS << "Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000736
Chris Lattner3f8b8912006-03-15 01:33:26 +0000737 // Note: this could emit significantly better code if we cared.
738 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000739 OS << " ";
740 if (!I->first.empty())
741 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
742 else
743 OS << "/* Target Independent Builtins */ ";
744 OS << "{\n";
745
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000746 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000747 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000748 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000749 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000750 OS << " return IntrinsicID;\n";
751 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000752 OS << "#endif\n\n";
753}