blob: ba30d97eaa35b4103447a666d0a943fd81b7ee36 [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)
Reid Spencerc4de3de2007-04-01 07:20:02 +0000102 IntMapping[Ints[i].Name] = 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";
106 OS << " switch (Name[5]) {\n";
Chris Lattner3b515802007-02-15 19:17:16 +0000107 OS << " default:\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000108 // Emit the intrinsics in sorted order.
109 char LastChar = 0;
Reid Spencerc4de3de2007-04-01 07:20:02 +0000110 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000111 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +0000112 if (I->first[5] != LastChar) {
113 LastChar = I->first[5];
Chris Lattner3b515802007-02-15 19:17:16 +0000114 OS << " break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000115 OS << " case '" << LastChar << "':\n";
116 }
117
Reid Spencerc4de3de2007-04-01 07:20:02 +0000118 // For overloaded intrinsics, only the prefix needs to match
119 if (Ints[I->second].isOverloaded)
Chandler Carruth69940402007-08-04 01:51:18 +0000120 OS << " if (Len > " << I->first.size()
121 << " && !memcmp(Name, \"" << I->first << ".\", "
Dale Johannesen49de9822009-02-05 01:49:45 +0000122 << (I->first.size() + 1) << ")) return " << TargetPrefix << "Intrinsic::"
Chandler Carruth69940402007-08-04 01:51:18 +0000123 << Ints[I->second].EnumName << ";\n";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000124 else
125 OS << " if (Len == " << I->first.size()
Chandler Carruth69940402007-08-04 01:51:18 +0000126 << " && !memcmp(Name, \"" << I->first << "\", "
Dale Johannesen49de9822009-02-05 01:49:45 +0000127 << I->first.size() << ")) return " << TargetPrefix << "Intrinsic::"
Reid Spencerc4de3de2007-04-01 07:20:02 +0000128 << Ints[I->second].EnumName << ";\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000129 }
130 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000131 OS << "#endif\n\n";
132}
133
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000134void IntrinsicEmitter::
135EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000136 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000137 OS << "// Intrinsic ID to name table\n";
138 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
139 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000140 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
141 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000142 OS << "#endif\n\n";
143}
144
Mon P Wang0d52ff12009-02-24 23:17:49 +0000145void IntrinsicEmitter::
146EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000147 raw_ostream &OS) {
Mon P Wang0d52ff12009-02-24 23:17:49 +0000148 OS << "// Intrinsic ID to overload table\n";
149 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
150 OS << " // Note that entry #0 is the invalid intrinsic!\n";
151 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
152 OS << " ";
153 if (Ints[i].isOverloaded)
154 OS << "true";
155 else
156 OS << "false";
157 OS << ",\n";
158 }
159 OS << "#endif\n\n";
160}
161
Owen Anderson825b72b2009-08-11 20:47:22 +0000162static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
Owen Andersone50ed302009-08-10 22:56:29 +0000163 if (EVT(VT).isInteger()) {
164 unsigned BitWidth = EVT(VT).getSizeInBits();
Owen Anderson1d0be152009-08-13 21:58:54 +0000165 OS << "IntegerType::get(Context, " << BitWidth << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 } else if (VT == MVT::Other) {
167 // MVT::OtherVT is used to mean the empty struct type here.
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000168 OS << "StructType::get(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 } else if (VT == MVT::f32) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000170 OS << "Type::getFloatTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 } else if (VT == MVT::f64) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000172 OS << "Type::getDoubleTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 } else if (VT == MVT::f80) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000174 OS << "Type::getX86_FP80Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 } else if (VT == MVT::f128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000176 OS << "Type::getFP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 } else if (VT == MVT::ppcf128) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000178 OS << "Type::getPPC_FP128Ty(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 } else if (VT == MVT::isVoid) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000180 OS << "Type::getVoidTy(Context)";
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 } else if (VT == MVT::Metadata) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000182 OS << "Type::getMetadataTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000183 } else {
184 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000185 }
186}
187
Daniel Dunbar1a551802009-07-03 00:10:29 +0000188static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000189 unsigned &ArgNo);
190
Daniel Dunbar1a551802009-07-03 00:10:29 +0000191static void EmitTypeGenerate(raw_ostream &OS,
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000192 const std::vector<Record*> &ArgTypes,
193 unsigned &ArgNo) {
Chris Lattner93dc92e2010-03-22 20:56:36 +0000194 if (ArgTypes.empty())
195 return EmitTypeForValueType(OS, MVT::isVoid);
196
197 if (ArgTypes.size() == 1)
198 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000199
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000200 OS << "StructType::get(Context, ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000201
202 for (std::vector<Record*>::const_iterator
Bill Wendling20072af2008-11-13 10:18:35 +0000203 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000204 EmitTypeGenerate(OS, *I, ArgNo);
Bill Wendling20072af2008-11-13 10:18:35 +0000205 OS << ", ";
206 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000207
Bill Wendling20072af2008-11-13 10:18:35 +0000208 OS << " NULL)";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000209}
210
Daniel Dunbar1a551802009-07-03 00:10:29 +0000211static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000212 unsigned &ArgNo) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000214
215 if (ArgType->isSubClassOf("LLVMMatchType")) {
216 unsigned Number = ArgType->getValueAsInt("Number");
217 assert(Number < ArgNo && "Invalid matching number!");
Bob Wilsonbc039792009-01-07 00:09:01 +0000218 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
219 OS << "VectorType::getExtendedElementVectorType"
220 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
221 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
222 OS << "VectorType::getTruncatedElementVectorType"
223 << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
224 else
225 OS << "Tys[" << Number << "]";
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000227 // NOTE: The ArgNo variable here is not the absolute argument number, it is
228 // the index of the "arbitrary" type in the Tys array passed to the
229 // Intrinsic::getDeclaration function. Consequently, we only want to
Chandler Carruth69940402007-08-04 01:51:18 +0000230 // increment it when we actually hit an overloaded type. Getting this wrong
231 // leads to very subtle bugs!
232 OS << "Tys[" << ArgNo++ << "]";
Owen Andersone50ed302009-08-10 22:56:29 +0000233 } else if (EVT(VT).isVector()) {
234 EVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000235 OS << "VectorType::get(";
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000237 OS << ", " << VVT.getVectorNumElements() << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000239 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000240 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000241 OS << ")";
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 } else if (VT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +0000243 // Make sure the user has passed us an argument type to overload. If not,
244 // treat it as an ordinary (not overloaded) intrinsic.
245 OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
246 << "] : PointerType::getUnqual(";
247 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
248 OS << ")";
249 ++ArgNo;
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 } else if (VT == MVT::isVoid) {
Chandler Carruth69940402007-08-04 01:51:18 +0000251 if (ArgNo == 0)
Owen Anderson1d0be152009-08-13 21:58:54 +0000252 OS << "Type::getVoidTy(Context)";
Chandler Carruth69940402007-08-04 01:51:18 +0000253 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 // MVT::isVoid is used to mean varargs here.
Chandler Carruth69940402007-08-04 01:51:18 +0000255 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000256 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000257 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000258 }
259}
260
Jim Grosbachda4231f2009-03-26 16:17:51 +0000261/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000262/// records.
263namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000264 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000265 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000266 bool operator()(const RecPair &LHS,
267 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000268 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000269 const std::vector<Record*> *LHSVec = &LHS.first;
270 const std::vector<Record*> *RHSVec = &RHS.first;
271 unsigned RHSSize = RHSVec->size();
272 unsigned LHSSize = LHSVec->size();
273
Chris Lattner93dc92e2010-03-22 20:56:36 +0000274 for (; i != LHSSize; ++i) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000275 if (i == RHSSize) return false; // RHS is shorter than LHS.
276 if ((*LHSVec)[i] != (*RHSVec)[i])
277 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
Chris Lattner93dc92e2010-03-22 20:56:36 +0000278 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000279
Bill Wendling023422a2008-11-13 12:03:00 +0000280 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000281
282 i = 0;
283 LHSVec = &LHS.second;
284 RHSVec = &RHS.second;
285 RHSSize = RHSVec->size();
286 LHSSize = LHSVec->size();
287
288 for (i = 0; i != LHSSize; ++i) {
289 if (i == RHSSize) return false; // RHS is shorter than LHS.
290 if ((*LHSVec)[i] != (*RHSVec)[i])
291 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
292 }
293
294 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000295 }
296 };
297}
298
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000299void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000300 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000301 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
302 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
303 OS << " switch (ID) {\n";
304 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000305
306 // This checking can emit a lot of very common code. To reduce the amount of
307 // code that we emit, batch up cases that have identical types. This avoids
308 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000309 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000310 MapTy UniqueArgInfos;
311
312 // Compute the unique argument type info.
313 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000314 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
315 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000316
317 // Loop through the array, emitting one comparison for each batch.
318 for (MapTy::iterator I = UniqueArgInfos.begin(),
319 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000320 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000321 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
322 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000323
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000324 const RecPair &ArgTypes = I->first;
325 const std::vector<Record*> &RetTys = ArgTypes.first;
326 const std::vector<Record*> &ParamTys = ArgTypes.second;
Bob Wilson09b13662009-07-29 16:35:59 +0000327 std::vector<unsigned> OverloadedTypeIndices;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000328
329 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
330 << ParamTys.size();
331
332 // Emit return types.
333 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
334 Record *ArgType = RetTys[j];
335 OS << ", ";
336
Chandler Carruth69940402007-08-04 01:51:18 +0000337 if (ArgType->isSubClassOf("LLVMMatchType")) {
338 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000339 assert(Number < OverloadedTypeIndices.size() &&
340 "Invalid matching number!");
341 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000342 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
343 OS << "~(ExtendedElementVectorType | " << Number << ")";
344 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
345 OS << "~(TruncatedElementVectorType | " << Number << ")";
346 else
347 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000348 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000350 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000351
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000352 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000353 OverloadedTypeIndices.push_back(j);
354
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000356 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000357 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000358 }
359
360 // Emit the parameter types.
361 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
362 Record *ArgType = ParamTys[j];
363 OS << ", ";
364
365 if (ArgType->isSubClassOf("LLVMMatchType")) {
366 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000367 assert(Number < OverloadedTypeIndices.size() &&
368 "Invalid matching number!");
369 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000370 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
371 OS << "~(ExtendedElementVectorType | " << Number << ")";
372 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
373 OS << "~(TruncatedElementVectorType | " << Number << ")";
374 else
375 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000376 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000378 OS << getEnumName(VT);
379
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000380 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000381 OverloadedTypeIndices.push_back(j + RetTys.size());
382
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000384 throw "Var arg type not last argument";
385 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000386 }
387
Chandler Carruth69940402007-08-04 01:51:18 +0000388 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000389 OS << " break;\n";
390 }
391 OS << " }\n";
392 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000393}
394
Jim Laskey95af5922007-02-07 20:38:26 +0000395void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000396 raw_ostream &OS) {
Jim Laskey95af5922007-02-07 20:38:26 +0000397 OS << "// Code for generating Intrinsic function declarations.\n";
398 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
399 OS << " switch (id) {\n";
400 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
401
402 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
403 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000404 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000405 MapTy UniqueArgInfos;
406
407 // Compute the unique argument type info.
408 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000409 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
410 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000411
412 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000413 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
414
Jim Laskey95af5922007-02-07 20:38:26 +0000415 for (MapTy::iterator I = UniqueArgInfos.begin(),
416 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000417 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000418 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
419 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000420
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000421 const RecPair &ArgTypes = I->first;
422 const std::vector<Record*> &RetTys = ArgTypes.first;
423 const std::vector<Record*> &ParamTys = ArgTypes.second;
424
425 unsigned N = ParamTys.size();
Jim Laskey95af5922007-02-07 20:38:26 +0000426
Chandler Carruth69940402007-08-04 01:51:18 +0000427 if (N > 1 &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000429 OS << " IsVarArg = true;\n";
430 --N;
431 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000432
Reid Spencer84c614d2007-05-22 19:30:31 +0000433 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000434 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000435 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000436 OS << ";\n";
437
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000438 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000439 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000440 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000441 OS << ");\n";
442 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000443
Jim Laskey95af5922007-02-07 20:38:26 +0000444 OS << " break;\n";
445 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000446
Jim Laskey95af5922007-02-07 20:38:26 +0000447 OS << " }\n";
448 OS << "#endif\n\n";
449}
450
Chris Lattner048ffb22009-01-12 01:18:58 +0000451/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000452void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000453EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000454 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
455 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000456 if (TargetOnly)
457 OS << "static AttrListPtr getAttributes(" << TargetPrefix
458 << "Intrinsic::ID id) {";
459 else
460 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {";
Chris Lattner048ffb22009-01-12 01:18:58 +0000461 OS << " // No intrinsic can throw exceptions.\n";
462 OS << " Attributes Attr = Attribute::NoUnwind;\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000463 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000464 OS << " default: break;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000465 unsigned MaxArgAttrs = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000466 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner10dae942009-01-12 01:27:55 +0000467 MaxArgAttrs =
468 std::max(MaxArgAttrs, unsigned(Ints[i].ArgumentAttributes.size()));
Chris Lattner7056de32006-03-24 01:13:55 +0000469 switch (Ints[i].ModRef) {
470 default: break;
471 case CodeGenIntrinsic::NoMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000472 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
473 << ":\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000474 break;
475 }
476 }
Devang Patel05988662008-09-25 21:00:45 +0000477 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000478 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000479 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
480 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000481 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000482 case CodeGenIntrinsic::ReadArgMem:
483 case CodeGenIntrinsic::ReadMem:
Dale Johannesen49de9822009-02-05 01:49:45 +0000484 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
485 << ":\n";
Chris Lattner022f64f2006-03-13 23:08:44 +0000486 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000487 }
488 }
Devang Patel05988662008-09-25 21:00:45 +0000489 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000490 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000491 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000492 OS << " AttributeWithIndex AWI[" << MaxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000493 OS << " unsigned NumAttrs = 0;\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000494 OS << " switch (id) {\n";
495 OS << " default: break;\n";
496
497 // Add argument attributes for any intrinsics that have them.
498 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
499 if (Ints[i].ArgumentAttributes.empty()) continue;
500
Dale Johannesen49de9822009-02-05 01:49:45 +0000501 OS << " case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
502 << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000503
504 std::vector<std::pair<unsigned, CodeGenIntrinsic::ArgAttribute> > ArgAttrs =
505 Ints[i].ArgumentAttributes;
506 // Sort by argument index.
507 std::sort(ArgAttrs.begin(), ArgAttrs.end());
508
509 unsigned NumArgsWithAttrs = 0;
510
Chris Lattnerd4a27002009-01-12 02:41:37 +0000511 while (!ArgAttrs.empty()) {
512 unsigned ArgNo = ArgAttrs[0].first;
513
514 OS << " AWI[" << NumArgsWithAttrs++ << "] = AttributeWithIndex::get("
515 << ArgNo+1 << ", 0";
516
517 while (!ArgAttrs.empty() && ArgAttrs[0].first == ArgNo) {
518 switch (ArgAttrs[0].second) {
519 default: assert(0 && "Unknown arg attribute");
520 case CodeGenIntrinsic::NoCapture:
521 OS << "|Attribute::NoCapture";
522 break;
523 }
524 ArgAttrs.erase(ArgAttrs.begin());
525 }
526 OS << ");\n";
527 }
Chris Lattner10dae942009-01-12 01:27:55 +0000528
Chris Lattnerd4a27002009-01-12 02:41:37 +0000529 OS << " NumAttrs = " << NumArgsWithAttrs << ";\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000530 OS << " break;\n";
531 }
532
533 OS << " }\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000534 OS << " AWI[NumAttrs] = AttributeWithIndex::get(~0, Attr);\n";
535 OS << " return AttrListPtr::get(AWI, NumAttrs+1);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000536 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000537 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000538}
Chris Lattner022f64f2006-03-13 23:08:44 +0000539
Duncan Sandsd869b382009-02-14 10:56:35 +0000540/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
541void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000542EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Duncan Sandsd869b382009-02-14 10:56:35 +0000543 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n";
544 OS << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n";
Duncan Sands7c422ac2010-01-06 08:45:52 +0000545 OS << "switch (iid) {\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000546 OS << "default:\n return UnknownModRefBehavior;\n";
547 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Dan Gohman7365c092010-08-05 23:36:21 +0000548 if (Ints[i].ModRef == CodeGenIntrinsic::ReadWriteMem)
Duncan Sandsd869b382009-02-14 10:56:35 +0000549 continue;
550 OS << "case " << TargetPrefix << "Intrinsic::" << Ints[i].EnumName
551 << ":\n";
552 switch (Ints[i].ModRef) {
553 default:
554 assert(false && "Unknown Mod/Ref type!");
555 case CodeGenIntrinsic::NoMem:
556 OS << " return DoesNotAccessMemory;\n";
557 break;
558 case CodeGenIntrinsic::ReadArgMem:
559 case CodeGenIntrinsic::ReadMem:
560 OS << " return OnlyReadsMemory;\n";
561 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000562 case CodeGenIntrinsic::ReadWriteArgMem:
Duncan Sandsd869b382009-02-14 10:56:35 +0000563 OS << " return AccessesArguments;\n";
564 break;
565 }
566 }
567 OS << "}\n";
568 OS << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
569}
570
Chris Lattner022f64f2006-03-13 23:08:44 +0000571void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000572EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Chris Lattner022f64f2006-03-13 23:08:44 +0000573 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
574 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
575 OS << " switch (F->getIntrinsicID()) {\n";
576 OS << " default: BuiltinName = \"\"; break;\n";
577 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
578 if (!Ints[i].GCCBuiltinName.empty()) {
579 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
580 << Ints[i].GCCBuiltinName << "\"; break;\n";
581 }
582 }
583 OS << " }\n";
584 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000585}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000586
Chris Lattner331bf922008-01-04 04:38:35 +0000587/// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
588/// sorted range of builtin names is equal to the current builtin. This breaks
589/// it down into a simple tree.
590///
591/// At this point, we know that all the builtins in the range have the same name
592/// for the first 'CharStart' characters. Only the end of the name needs to be
593/// discriminated.
594typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
595static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
596 unsigned CharStart, unsigned Indent,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000597 std::string TargetPrefix, raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000598 if (Start == End) return; // empty range.
599
600 // Determine what, if anything, is the same about all these strings.
601 std::string CommonString = Start->first;
602 unsigned NumInRange = 0;
603 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
604 // Find the first character that doesn't match.
605 const std::string &ThisStr = I->first;
606 unsigned NonMatchChar = CharStart;
607 while (NonMatchChar < CommonString.size() &&
608 NonMatchChar < ThisStr.size() &&
609 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
610 ++NonMatchChar;
611 // Truncate off pieces that don't match.
612 CommonString.resize(NonMatchChar);
613 }
614
615 // Just compare the rest of the string.
616 if (NumInRange == 1) {
617 if (CharStart != CommonString.size()) {
618 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
619 if (CharStart) OS << "+" << CharStart;
620 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
621 OS << CommonString.size() - CharStart << "))\n";
622 ++Indent;
623 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000624 OS << std::string(Indent*2, ' ') << "IntrinsicID = " << TargetPrefix
625 << "Intrinsic::";
Chris Lattner331bf922008-01-04 04:38:35 +0000626 OS << Start->second << ";\n";
627 return;
628 }
629
630 // At this point, we potentially have a common prefix for these builtins, emit
631 // a check for this common prefix.
632 if (CommonString.size() != CharStart) {
633 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
634 if (CharStart) OS << "+" << CharStart;
635 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
636 OS << CommonString.size()-CharStart << ")) {\n";
637
Dale Johannesen49de9822009-02-05 01:49:45 +0000638 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1,
639 TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000640 OS << std::string(Indent*2, ' ') << "}\n";
641 return;
642 }
643
644 // Output a switch on the character that differs across the set.
645 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
646 << "]) {";
647 if (CharStart)
648 OS << " // \"" << std::string(Start->first.begin(),
649 Start->first.begin()+CharStart) << "\"";
650 OS << "\n";
651
652 for (StrMapIterator I = Start; I != End; ) {
653 char ThisChar = I->first[CharStart];
654 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
655 // Figure out the range that has this common character.
656 StrMapIterator NextChar = I;
657 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
658 ++NextChar)
659 /*empty*/;
Dale Johannesen49de9822009-02-05 01:49:45 +0000660 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, TargetPrefix,OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000661 OS << std::string(Indent*2, ' ') << " break;\n";
662 I = NextChar;
663 }
664 OS << std::string(Indent*2, ' ') << "}\n";
665}
666
667/// 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 // Rearrange the builtins by length.
673 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
674 BuiltinsByLen.reserve(100);
675
676 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
677 if (I->first.size() >= BuiltinsByLen.size())
678 BuiltinsByLen.resize(I->first.size()+1);
679 BuiltinsByLen[I->first.size()].insert(*I);
680 }
681
682 // Now that we have all the builtins by their length, emit a switch stmt.
683 OS << " switch (strlen(BuiltinName)) {\n";
684 OS << " default: break;\n";
685 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
686 if (BuiltinsByLen[i].empty()) continue;
687 OS << " case " << i << ":\n";
688 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
Dale Johannesen49de9822009-02-05 01:49:45 +0000689 0, 3, TargetPrefix, OS);
Chris Lattner331bf922008-01-04 04:38:35 +0000690 OS << " break;\n";
691 }
692 OS << " }\n";
693}
694
695
Chris Lattner3f8b8912006-03-15 01:33:26 +0000696void IntrinsicEmitter::
697EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000698 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000699 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000700 BIMTy BuiltinMap;
701 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
702 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000703 // Get the map for this target prefix.
704 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
705
706 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
707 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000708 throw "Intrinsic '" + Ints[i].TheDef->getName() +
709 "': duplicate GCC builtin name!";
710 }
711 }
712
713 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
714 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
715 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
716 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
717 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000718
719 if (TargetOnly) {
720 OS << "static " << TargetPrefix << "Intrinsic::ID "
721 << "getIntrinsicForGCCBuiltin(const char "
722 << "*TargetPrefix, const char *BuiltinName) {\n";
723 OS << " " << TargetPrefix << "Intrinsic::ID IntrinsicID = ";
724 } else {
725 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
726 << "*TargetPrefix, const char *BuiltinName) {\n";
727 OS << " Intrinsic::ID IntrinsicID = ";
728 }
729
730 if (TargetOnly)
731 OS << "(" << TargetPrefix<< "Intrinsic::ID)";
732
733 OS << "Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000734
Chris Lattner3f8b8912006-03-15 01:33:26 +0000735 // Note: this could emit significantly better code if we cared.
736 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000737 OS << " ";
738 if (!I->first.empty())
739 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
740 else
741 OS << "/* Target Independent Builtins */ ";
742 OS << "{\n";
743
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000744 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000745 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000746 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000747 }
Dale Johannesen49de9822009-02-05 01:49:45 +0000748 OS << " return IntrinsicID;\n";
749 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000750 OS << "#endif\n\n";
751}