blob: 0642f59ace5e7fc816a75cc67d2ab0b182da2e73 [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"
Peter Collingbourne7c788882011-10-01 16:41:13 +000016#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +000017#include "llvm/TableGen/StringMatcher.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 Lattner3f8b8912006-03-15 01:33:26 +000060 // Emit code to translate GCC builtins into LLVM intrinsics.
61 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor7d9663c2010-05-11 06:17:44 +000062
63 EmitSuffix(OS);
64}
65
66void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
67 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000068 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
69 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000070 "# pragma push_macro(\"setjmp\")\n"
71 "# undef setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000072 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000073 "#endif\n\n";
74}
75
76void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencer1f409602010-09-24 19:48:47 +000077 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000078 "// let's return it to _setjmp state\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000079 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000080 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000081 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000082}
83
84void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000085 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000086 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000087 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
88 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
89 OS << " " << Ints[i].EnumName;
90 OS << ((i != e-1) ? ", " : " ");
91 OS << std::string(40-Ints[i].EnumName.size(), ' ')
92 << "// " << Ints[i].Name << "\n";
93 }
94 OS << "#endif\n\n";
95}
Chris Lattner9b843b22006-03-09 20:34:19 +000096
97void IntrinsicEmitter::
98EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000099 raw_ostream &OS) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000100 // Build a 'first character of function name' -> intrinsic # mapping.
101 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000102 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattnercc67c752010-09-06 03:58:45 +0000103 IntMapping[Ints[i].Name[5]].push_back(i);
104
Chris Lattner9b843b22006-03-09 20:34:19 +0000105 OS << "// Function name -> enum value recognizer code.\n";
106 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattnercc67c752010-09-06 03:58:45 +0000107 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
108 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
109 OS << " default: break;\n";
110 // Emit the intrinsic matching stuff by first letter.
111 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000112 E = IntMapping.end(); I != E; ++I) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000113 OS << " case '" << I->first << "':\n";
114 std::vector<unsigned> &IntList = I->second;
115
116 // Emit all the overloaded intrinsics first, build a table of the
117 // non-overloaded ones.
118 std::vector<StringMatcher::StringPair> MatchTable;
119
120 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
121 unsigned IntNo = IntList[i];
122 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
123 Ints[IntNo].EnumName + ";";
124
125 if (!Ints[IntNo].isOverloaded) {
126 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
127 continue;
128 }
129
130 // For overloaded intrinsics, only the prefix needs to match
131 std::string TheStr = Ints[IntNo].Name.substr(6);
132 TheStr += '.'; // Require "bswap." instead of bswap.
133 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
134 << Result << '\n';
Chris Lattner9b843b22006-03-09 20:34:19 +0000135 }
136
Chris Lattnercc67c752010-09-06 03:58:45 +0000137 // Emit the matcher logic for the fixed length strings.
138 StringMatcher("NameR", MatchTable, OS).Emit(1);
139 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000140 }
Chris Lattnercc67c752010-09-06 03:58:45 +0000141
Chris Lattner9b843b22006-03-09 20:34:19 +0000142 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000143 OS << "#endif\n\n";
144}
145
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000146void IntrinsicEmitter::
147EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000148 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000149 OS << "// Intrinsic ID to name table\n";
150 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
151 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000152 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
153 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000154 OS << "#endif\n\n";
155}
156
Mon P Wang0d52ff12009-02-24 23:17:49 +0000157void IntrinsicEmitter::
158EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000159 raw_ostream &OS) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000160 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000161 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000162 OS << "static const uint8_t OTable[] = {\n";
163 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000164 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000165 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
166 if ((i+1)%8 == 0)
167 OS << ",\n 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000168 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000169 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000170 }
Benjamin Kramer36a21382012-03-01 02:16:57 +0000171 OS << "\n};\n\n";
172 // OTable contains a true bit at the position if the intrinsic is overloaded.
173 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000174 OS << "#endif\n\n";
175}
176
Jim Grosbachda4231f2009-03-26 16:17:51 +0000177/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000178/// records.
179namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000180 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000181 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000182 bool operator()(const RecPair &LHS,
183 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000184 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000185 const std::vector<Record*> *LHSVec = &LHS.first;
186 const std::vector<Record*> *RHSVec = &RHS.first;
187 unsigned RHSSize = RHSVec->size();
188 unsigned LHSSize = LHSVec->size();
189
Chris Lattner93dc92e2010-03-22 20:56:36 +0000190 for (; i != LHSSize; ++i) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000191 if (i == RHSSize) return false; // RHS is shorter than LHS.
192 if ((*LHSVec)[i] != (*RHSVec)[i])
193 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
Chris Lattner93dc92e2010-03-22 20:56:36 +0000194 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000195
Bill Wendling023422a2008-11-13 12:03:00 +0000196 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000197
198 i = 0;
199 LHSVec = &LHS.second;
200 RHSVec = &RHS.second;
201 RHSSize = RHSVec->size();
202 LHSSize = LHSVec->size();
203
204 for (i = 0; i != LHSSize; ++i) {
205 if (i == RHSSize) return false; // RHS is shorter than LHS.
206 if ((*LHSVec)[i] != (*RHSVec)[i])
207 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
208 }
209
210 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000211 }
212 };
213}
214
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000215void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000216 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000217 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
218 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
219 OS << " switch (ID) {\n";
Craig Topper655b8de2012-02-05 07:21:30 +0000220 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000221
222 // This checking can emit a lot of very common code. To reduce the amount of
223 // code that we emit, batch up cases that have identical types. This avoids
224 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000225 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000226 MapTy UniqueArgInfos;
227
228 // Compute the unique argument type info.
229 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000230 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
231 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000232
233 // Loop through the array, emitting one comparison for each batch.
234 for (MapTy::iterator I = UniqueArgInfos.begin(),
235 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000236 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000237 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
238 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000239
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000240 const RecPair &ArgTypes = I->first;
241 const std::vector<Record*> &RetTys = ArgTypes.first;
242 const std::vector<Record*> &ParamTys = ArgTypes.second;
Bob Wilson09b13662009-07-29 16:35:59 +0000243 std::vector<unsigned> OverloadedTypeIndices;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000244
245 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
246 << ParamTys.size();
247
248 // Emit return types.
249 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
250 Record *ArgType = RetTys[j];
251 OS << ", ";
252
Chandler Carruth69940402007-08-04 01:51:18 +0000253 if (ArgType->isSubClassOf("LLVMMatchType")) {
254 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000255 assert(Number < OverloadedTypeIndices.size() &&
256 "Invalid matching number!");
257 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000258 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
259 OS << "~(ExtendedElementVectorType | " << Number << ")";
260 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
261 OS << "~(TruncatedElementVectorType | " << Number << ")";
262 else
263 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000264 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000266 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000267
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000268 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000269 OverloadedTypeIndices.push_back(j);
270
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000272 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000273 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000274 }
275
276 // Emit the parameter types.
277 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
278 Record *ArgType = ParamTys[j];
279 OS << ", ";
280
281 if (ArgType->isSubClassOf("LLVMMatchType")) {
282 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000283 assert(Number < OverloadedTypeIndices.size() &&
284 "Invalid matching number!");
285 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000286 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
287 OS << "~(ExtendedElementVectorType | " << Number << ")";
288 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
289 OS << "~(TruncatedElementVectorType | " << Number << ")";
290 else
291 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000292 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000294 OS << getEnumName(VT);
295
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000296 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000297 OverloadedTypeIndices.push_back(j + RetTys.size());
298
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000300 throw "Var arg type not last argument";
301 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000302 }
303
Chandler Carruth69940402007-08-04 01:51:18 +0000304 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000305 OS << " break;\n";
306 }
307 OS << " }\n";
308 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000309}
310
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000311static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
312 if (EVT(VT).isInteger()) {
313 unsigned BitWidth = EVT(VT).getSizeInBits();
314 OS << "IntegerType::get(Context, " << BitWidth << ")";
315 } else if (VT == MVT::Other) {
316 // MVT::OtherVT is used to mean the empty struct type here.
317 OS << "StructType::get(Context)";
318 } else if (VT == MVT::f16) {
319 OS << "Type::getHalfTy(Context)";
320 } else if (VT == MVT::f32) {
321 OS << "Type::getFloatTy(Context)";
322 } else if (VT == MVT::f64) {
323 OS << "Type::getDoubleTy(Context)";
324 } else if (VT == MVT::f80) {
325 OS << "Type::getX86_FP80Ty(Context)";
326 } else if (VT == MVT::f128) {
327 OS << "Type::getFP128Ty(Context)";
328 } else if (VT == MVT::ppcf128) {
329 OS << "Type::getPPC_FP128Ty(Context)";
330 } else if (VT == MVT::isVoid) {
331 OS << "Type::getVoidTy(Context)";
332 } else if (VT == MVT::Metadata) {
333 OS << "Type::getMetadataTy(Context)";
334 } else if (VT == MVT::x86mmx) {
335 OS << "Type::getX86_MMXTy(Context)";
336 } else {
337 assert(false && "Unsupported ValueType!");
338 }
339}
340
341static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
342 unsigned &ArgNo);
343
344static void EmitTypeGenerate(raw_ostream &OS,
345 const std::vector<Record*> &ArgTypes,
346 unsigned &ArgNo) {
347 if (ArgTypes.empty())
348 return EmitTypeForValueType(OS, MVT::isVoid);
349
350 if (ArgTypes.size() == 1)
351 return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
352
353 OS << "StructType::get(";
354
355 for (std::vector<Record*>::const_iterator
356 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
357 EmitTypeGenerate(OS, *I, ArgNo);
358 OS << ", ";
359 }
360
361 OS << " NULL)";
362}
363
364static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
365 unsigned &ArgNo) {
366 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
367
368 if (ArgType->isSubClassOf("LLVMMatchType")) {
369 unsigned Number = ArgType->getValueAsInt("Number");
370 assert(Number < ArgNo && "Invalid matching number!");
371 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
372 OS << "VectorType::getExtendedElementVectorType"
373 << "(cast<VectorType>(Tys[" << Number << "]))";
374 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
375 OS << "VectorType::getTruncatedElementVectorType"
376 << "(cast<VectorType>(Tys[" << Number << "]))";
377 else
378 OS << "Tys[" << Number << "]";
379 } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
380 // NOTE: The ArgNo variable here is not the absolute argument number, it is
381 // the index of the "arbitrary" type in the Tys array passed to the
382 // Intrinsic::getDeclaration function. Consequently, we only want to
383 // increment it when we actually hit an overloaded type. Getting this wrong
384 // leads to very subtle bugs!
385 OS << "Tys[" << ArgNo++ << "]";
386 } else if (EVT(VT).isVector()) {
387 EVT VVT = VT;
388 OS << "VectorType::get(";
389 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
390 OS << ", " << VVT.getVectorNumElements() << ")";
391 } else if (VT == MVT::iPTR) {
392 OS << "PointerType::getUnqual(";
393 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
394 OS << ")";
395 } else if (VT == MVT::iPTRAny) {
396 // Make sure the user has passed us an argument type to overload. If not,
397 // treat it as an ordinary (not overloaded) intrinsic.
398 OS << "(" << ArgNo << " < Tys.size()) ? Tys[" << ArgNo
399 << "] : PointerType::getUnqual(";
400 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
401 OS << ")";
402 ++ArgNo;
403 } else if (VT == MVT::isVoid) {
404 assert(ArgNo == 0);
405 OS << "Type::getVoidTy(Context)";
406 } else {
407 EmitTypeForValueType(OS, VT);
408 }
409}
410
411
412// NOTE: This must be kept in synch with the version emitted to the .gen file!
413enum IIT_Info {
414 IIT_Done = 0,
415 IIT_I1 = 1,
416 IIT_I8 = 2,
417 IIT_I16 = 3,
418 IIT_I32 = 4,
419 IIT_I64 = 5,
420 IIT_F32 = 6,
421 IIT_F64 = 7,
422 IIT_V2 = 8,
423 IIT_V4 = 9,
424 IIT_V8 = 10,
425 IIT_V16 = 11,
426 IIT_MMX = 12,
427 IIT_PTR = 13,
428 IIT_ARG = 14
429};
430
431static void EncodeFixedValueType(MVT::SimpleValueType VT,
432 SmallVectorImpl<unsigned> &Sig) {
433 if (EVT(VT).isInteger()) {
434 unsigned BitWidth = EVT(VT).getSizeInBits();
435 switch (BitWidth) {
436 default: return Sig.push_back(~0U);
437 case 1: return Sig.push_back(IIT_I1);
438 case 8: return Sig.push_back(IIT_I8);
439 case 16: return Sig.push_back(IIT_I16);
440 case 32: return Sig.push_back(IIT_I32);
441 case 64: return Sig.push_back(IIT_I64);
442 }
443 }
444
445/* } else if (VT == MVT::Other) {
446 // MVT::OtherVT is used to mean the empty struct type here.
447 OS << "StructType::get(Context)";
448 } else if (VT == MVT::f16) {
449 OS << "Type::getHalfTy(Context)";*/
450 if (VT == MVT::f32)
451 return Sig.push_back(IIT_F32);
452 if (VT == MVT::f64)
453 return Sig.push_back(IIT_F64);
454 //if (VT == MVT::f80) {
455 // OS << "Type::getX86_FP80Ty(Context)";
456 //if (VT == MVT::f128) {
457 // OS << "Type::getFP128Ty(Context)";
458 // if (VT == MVT::ppcf128) {
459 // OS << "Type::getPPC_FP128Ty(Context)";
460 //if (VT == MVT::Metadata) {
461 // OS << "Type::getMetadataTy(Context)";
462 if (VT == MVT::x86mmx)
463 return Sig.push_back(IIT_MMX);
464
465 assert(VT != MVT::isVoid);
466 Sig.push_back(~0U);
467}
468
Francois Pichet3aca8792012-05-17 03:38:19 +0000469#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000470
471static void EncodeFixedType(Record *R, SmallVectorImpl<unsigned> &Sig) {
472
473 if (R->isSubClassOf("LLVMMatchType")) {
474 return Sig.push_back(~0U);
475/*
476 unsigned Number = ArgType->getValueAsInt("Number");
477 assert(Number < ArgNo && "Invalid matching number!");
478 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
479 OS << "VectorType::getExtendedElementVectorType"
480 << "(cast<VectorType>(Tys[" << Number << "]))";
481 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
482 OS << "VectorType::getTruncatedElementVectorType"
483 << "(cast<VectorType>(Tys[" << Number << "]))";
484 else
485 OS << "Tys[" << Number << "]";
486 */
487 }
488
489 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
490
491 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
492 return Sig.push_back(~0U);
493 /*
494 // NOTE: The ArgNo variable here is not the absolute argument number, it is
495 // the index of the "arbitrary" type in the Tys array passed to the
496 // Intrinsic::getDeclaration function. Consequently, we only want to
497 // increment it when we actually hit an overloaded type. Getting this wrong
498 // leads to very subtle bugs!
499 OS << "Tys[" << ArgNo++ << "]";
500 */
501 }
502
503 if (EVT(VT).isVector()) {
504 EVT VVT = VT;
505 switch (VVT.getVectorNumElements()) {
506 default: Sig.push_back(~0U); return;
507 case 2: Sig.push_back(IIT_V2); break;
508 case 4: Sig.push_back(IIT_V4); break;
509 case 8: Sig.push_back(IIT_V8); break;
510 case 16: Sig.push_back(IIT_V16); break;
511 }
512
513 return EncodeFixedValueType(VVT.getVectorElementType().
514 getSimpleVT().SimpleTy, Sig);
515 }
516
517 if (VT == MVT::iPTR) {
518 Sig.push_back(IIT_PTR);
519 return EncodeFixedType(R->getValueAsDef("ElTy"), Sig);
520 }
521
522 /*if (VT == MVT::iPTRAny) {
523 // Make sure the user has passed us an argument type to overload. If not,
524 // treat it as an ordinary (not overloaded) intrinsic.
525 OS << "(" << ArgNo << " < Tys.size()) ? Tys[" << ArgNo
526 << "] : PointerType::getUnqual(";
527 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
528 OS << ")";
529 ++ArgNo;
530 }*/
531
532 assert(VT != MVT::isVoid);
533 EncodeFixedValueType(VT, Sig);
534}
Francois Pichet3aca8792012-05-17 03:38:19 +0000535#pragma optimize("",on)
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000536
537/// ComputeFixedEncoding - If we can encode the type signature for this
538/// intrinsic into 32 bits, return it. If not, return ~0U.
539static unsigned ComputeFixedEncoding(const CodeGenIntrinsic &Int) {
540 if (Int.IS.RetVTs.size() >= 2) return ~0U;
541
542 SmallVector<unsigned, 8> TypeSig;
543 if (Int.IS.RetVTs.empty())
544 TypeSig.push_back(IIT_Done);
545 else if (Int.IS.RetVTs.size() == 1 &&
546 Int.IS.RetVTs[0] == MVT::isVoid)
547 TypeSig.push_back(IIT_Done);
548 else
549 EncodeFixedType(Int.IS.RetTypeDefs[0], TypeSig);
550
551 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
552 EncodeFixedType(Int.IS.ParamTypeDefs[i], TypeSig);
553
554 // Can only encode 8 nibbles into a 32-bit word.
555 if (TypeSig.size() > 8) return ~0U;
556
557 unsigned Result = 0;
558 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
559 // If we had an unencodable argument, bail out.
560 if (TypeSig[i] == ~0U)
561 return ~0U;
562 Result = (Result << 4) | TypeSig[e-i-1];
563 }
564
565 return Result;
566}
567
Jim Laskey95af5922007-02-07 20:38:26 +0000568void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000569 raw_ostream &OS) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000570 OS << "// Global intrinsic function declaration type table.\n";
571 OS << "#ifdef GET_INTRINSTIC_GENERATOR_GLOBAL\n";
572 // NOTE: These enums must be kept in sync with the ones above!
573 OS << "enum IIT_Info {\n";
574 OS << " IIT_Done = 0,\n";
575 OS << " IIT_I1 = 1,\n";
576 OS << " IIT_I8 = 2,\n";
577 OS << " IIT_I16 = 3,\n";
578 OS << " IIT_I32 = 4,\n";
579 OS << " IIT_I64 = 5,\n";
580 OS << " IIT_F32 = 6,\n";
581 OS << " IIT_F64 = 7,\n";
582 OS << " IIT_V2 = 8,\n";
583 OS << " IIT_V4 = 9,\n";
584 OS << " IIT_V8 = 10,\n";
585 OS << " IIT_V16 = 11,\n";
586 OS << " IIT_MMX = 12,\n";
587 OS << " IIT_PTR = 13,\n";
588 OS << " IIT_ARG = 14\n";
589 // 15 is unassigned so far.
590 OS << "};\n\n";
591
Jim Laskey95af5922007-02-07 20:38:26 +0000592
593 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
594 // types.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000595 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000596 MapTy UniqueArgInfos;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000597
598 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
599 // capture it in this vector, otherwise store a ~0U.
600 std::vector<unsigned> FixedEncodings;
Jim Laskey95af5922007-02-07 20:38:26 +0000601
602 // Compute the unique argument type info.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000603 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
604 FixedEncodings.push_back(ComputeFixedEncoding(Ints[i]));
605
606 // If we didn't compute a compact encoding, emit a long-form variant.
607 if (FixedEncodings.back() == ~0U)
608 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
609 Ints[i].IS.ParamTypeDefs)].push_back(i);
610 }
611
612 OS << "static const unsigned IIT_Table[] = {\n ";
613
614 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
615 if ((i & 7) == 7)
616 OS << "\n ";
617 if (FixedEncodings[i] == ~0U)
618 OS << "~0U, ";
619 else
620 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
621 }
622
623 OS << "0\n};\n\n#endif\n\n"; // End of GET_INTRINSTIC_GENERATOR_GLOBAL
624
625 OS << "// Code for generating Intrinsic function declarations.\n";
626 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
627 OS << " switch (id) {\n";
628 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000629
630 // Loop through the array, emitting one generator for each batch.
Dale Johannesen49de9822009-02-05 01:49:45 +0000631 std::string IntrinsicStr = TargetPrefix + "Intrinsic::";
632
Jim Laskey95af5922007-02-07 20:38:26 +0000633 for (MapTy::iterator I = UniqueArgInfos.begin(),
634 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000635 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dale Johannesen49de9822009-02-05 01:49:45 +0000636 OS << " case " << IntrinsicStr << Ints[I->second[i]].EnumName
637 << ":\t\t// " << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000638
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000639 const RecPair &ArgTypes = I->first;
640 const std::vector<Record*> &RetTys = ArgTypes.first;
641 const std::vector<Record*> &ParamTys = ArgTypes.second;
642
643 unsigned N = ParamTys.size();
Reid Spencer84c614d2007-05-22 19:30:31 +0000644 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000645 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000646 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000647 OS << ";\n";
648
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000649 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000650 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000651 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000652 OS << ");\n";
653 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000654
Jim Laskey95af5922007-02-07 20:38:26 +0000655 OS << " break;\n";
656 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000657
Jim Laskey95af5922007-02-07 20:38:26 +0000658 OS << " }\n";
659 OS << "#endif\n\n";
660}
661
John McCallbd0fa4c2011-05-28 06:31:34 +0000662namespace {
663 enum ModRefKind {
664 MRK_none,
665 MRK_readonly,
666 MRK_readnone
667 };
668
669 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
670 switch (intrinsic.ModRef) {
671 case CodeGenIntrinsic::NoMem:
672 return MRK_readnone;
673 case CodeGenIntrinsic::ReadArgMem:
674 case CodeGenIntrinsic::ReadMem:
675 return MRK_readonly;
676 case CodeGenIntrinsic::ReadWriteArgMem:
677 case CodeGenIntrinsic::ReadWriteMem:
678 return MRK_none;
679 }
Craig Topper655b8de2012-02-05 07:21:30 +0000680 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000681 }
682
683 struct AttributeComparator {
684 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
685 // Sort throwing intrinsics after non-throwing intrinsics.
686 if (L->canThrow != R->canThrow)
687 return R->canThrow;
688
689 // Try to order by readonly/readnone attribute.
690 ModRefKind LK = getModRefKind(*L);
691 ModRefKind RK = getModRefKind(*R);
692 if (LK != RK) return (LK > RK);
693
694 // Order by argument attributes.
695 // This is reliable because each side is already sorted internally.
696 return (L->ArgumentAttributes < R->ArgumentAttributes);
697 }
698 };
699}
700
Chris Lattner048ffb22009-01-12 01:18:58 +0000701/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000702void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000703EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000704 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
705 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000706 if (TargetOnly)
707 OS << "static AttrListPtr getAttributes(" << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000708 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000709 else
John McCallbd0fa4c2011-05-28 06:31:34 +0000710 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
711
Craig Topper1f595232012-02-28 06:32:00 +0000712 // Compute the maximum number of attribute arguments and the map
713 typedef std::map<const CodeGenIntrinsic*, unsigned,
714 AttributeComparator> UniqAttrMapTy;
715 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000716 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000717 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000718 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000719 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000720 maxArgAttrs =
721 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000722 unsigned &N = UniqAttributes[&intrinsic];
723 if (N) continue;
724 assert(AttrNum < 256 && "Too many unique attributes for table!");
725 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000726 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000727
728 // Emit an array of AttributeWithIndex. Most intrinsics will have
729 // at least one entry, for the function itself (index ~1), which is
730 // usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000731 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topper1f595232012-02-28 06:32:00 +0000732
733 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
734 const CodeGenIntrinsic &intrinsic = Ints[i];
735
736 OS << " " << UniqAttributes[&intrinsic] << ", // "
737 << intrinsic.Name << "\n";
738 }
739 OS << " };\n\n";
740
John McCallbd0fa4c2011-05-28 06:31:34 +0000741 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000742 OS << " unsigned NumAttrs = 0;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000743 OS << " if (id != 0) {\n";
744 OS << " switch(IntrinsicsToAttributesMap[id - ";
745 if (TargetOnly)
746 OS << "Intrinsic::num_intrinsics";
747 else
748 OS << "1";
749 OS << "]) {\n";
750 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topper1f595232012-02-28 06:32:00 +0000751 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
752 E = UniqAttributes.end(); I != E; ++I) {
Craig Topperb57b1702012-04-13 06:14:57 +0000753 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000754
Craig Topper1f595232012-02-28 06:32:00 +0000755 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000756
757 // Keep track of the number of attributes we're writing out.
758 unsigned numAttrs = 0;
759
760 // The argument attributes are alreadys sorted by argument index.
761 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
762 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000763
Craig Topperb57b1702012-04-13 06:14:57 +0000764 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
John McCallbd0fa4c2011-05-28 06:31:34 +0000765 << argNo+1 << ", ";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000766
John McCallbd0fa4c2011-05-28 06:31:34 +0000767 bool moreThanOne = false;
768
769 do {
770 if (moreThanOne) OS << '|';
771
772 switch (intrinsic.ArgumentAttributes[ai].second) {
Chris Lattnerd4a27002009-01-12 02:41:37 +0000773 case CodeGenIntrinsic::NoCapture:
John McCallbd0fa4c2011-05-28 06:31:34 +0000774 OS << "Attribute::NoCapture";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000775 break;
776 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000777
778 ++ai;
779 moreThanOne = true;
780 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
781
782 OS << ");\n";
783 }
784
785 ModRefKind modRef = getModRefKind(intrinsic);
786
787 if (!intrinsic.canThrow || modRef) {
Craig Topperb57b1702012-04-13 06:14:57 +0000788 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
John McCallbd0fa4c2011-05-28 06:31:34 +0000789 if (!intrinsic.canThrow) {
790 OS << "Attribute::NoUnwind";
791 if (modRef) OS << '|';
792 }
793 switch (modRef) {
794 case MRK_none: break;
795 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
796 case MRK_readnone: OS << "Attribute::ReadNone"; break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000797 }
798 OS << ");\n";
799 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000800
801 if (numAttrs) {
Craig Topperb57b1702012-04-13 06:14:57 +0000802 OS << " NumAttrs = " << numAttrs << ";\n";
803 OS << " break;\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000804 } else {
Craig Topperb57b1702012-04-13 06:14:57 +0000805 OS << " return AttrListPtr();\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000806 }
Chris Lattner10dae942009-01-12 01:27:55 +0000807 }
808
Craig Topperb57b1702012-04-13 06:14:57 +0000809 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000810 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000811 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000812 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000813 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000814}
Chris Lattner022f64f2006-03-13 23:08:44 +0000815
Duncan Sandsd869b382009-02-14 10:56:35 +0000816/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
817void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000818EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000819 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
820 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
821 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
822 << "\"Unknown intrinsic.\");\n\n";
823
824 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
825 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000826 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000827 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sandsd869b382009-02-14 10:56:35 +0000828 switch (Ints[i].ModRef) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000829 case CodeGenIntrinsic::NoMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000830 OS << "DoesNotAccessMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000831 break;
832 case CodeGenIntrinsic::ReadArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000833 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000834 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000835 case CodeGenIntrinsic::ReadMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000836 OS << "OnlyReadsMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000837 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000838 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000839 OS << "OnlyAccessesArgumentPointees,\n";
840 break;
841 case CodeGenIntrinsic::ReadWriteMem:
842 OS << "UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000843 break;
844 }
845 }
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000846 OS << "};\n\n"
847 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
848 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000849}
850
Chris Lattner331bf922008-01-04 04:38:35 +0000851/// EmitTargetBuiltins - All of the builtins in the specified map are for the
852/// same target, and we already checked it.
853static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000854 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000855 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000856
Chris Lattner298b1762010-09-06 03:14:45 +0000857 std::vector<StringMatcher::StringPair> Results;
Chris Lattner331bf922008-01-04 04:38:35 +0000858
Chris Lattner298b1762010-09-06 03:14:45 +0000859 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
860 E = BIM.end(); I != E; ++I) {
861 std::string ResultCode =
862 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
863 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000864 }
Chris Lattner298b1762010-09-06 03:14:45 +0000865
866 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000867}
868
869
Chris Lattner3f8b8912006-03-15 01:33:26 +0000870void IntrinsicEmitter::
871EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000872 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000873 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000874 BIMTy BuiltinMap;
875 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
876 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000877 // Get the map for this target prefix.
878 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
879
880 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
881 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000882 throw "Intrinsic '" + Ints[i].TheDef->getName() +
883 "': duplicate GCC builtin name!";
884 }
885 }
886
887 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
888 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
889 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
890 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
891 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000892
893 if (TargetOnly) {
894 OS << "static " << TargetPrefix << "Intrinsic::ID "
895 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000896 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000897 } else {
898 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000899 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000900 }
901
Chris Lattner298b1762010-09-06 03:14:45 +0000902 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
903 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000904
Chris Lattner3f8b8912006-03-15 01:33:26 +0000905 // Note: this could emit significantly better code if we cared.
906 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000907 OS << " ";
908 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000909 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000910 else
911 OS << "/* Target Independent Builtins */ ";
912 OS << "{\n";
913
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000914 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000915 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000916 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000917 }
Chris Lattner298b1762010-09-06 03:14:45 +0000918 OS << " return ";
919 if (!TargetPrefix.empty())
920 OS << "(" << TargetPrefix << "Intrinsic::ID)";
921 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000922 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000923 OS << "#endif\n\n";
924}