blob: a595b1edd76ce4c5d4976175383a006f1724b8f7 [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9e493cf2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth69940402007-08-04 01:51:18 +000014#include "CodeGenTarget.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000015#include "IntrinsicEmitter.h"
Chris Lattner387c9dc2012-05-17 15:55:41 +000016#include "SequenceToOffsetTable.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000017#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +000018#include "llvm/TableGen/StringMatcher.h"
Chris Lattner18faf5d2006-03-13 22:38:57 +000019#include "llvm/ADT/StringExtras.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000020#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000024// IntrinsicEmitter Implementation
25//===----------------------------------------------------------------------===//
26
Daniel Dunbar1a551802009-07-03 00:10:29 +000027void IntrinsicEmitter::run(raw_ostream &OS) {
Chris Lattner9e493cf2006-03-03 02:32:46 +000028 EmitSourceFileHeader("Intrinsic Function Source Fragment", OS);
29
Dale Johannesen49de9822009-02-05 01:49:45 +000030 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
31
32 if (TargetOnly && !Ints.empty())
33 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattner9e493cf2006-03-03 02:32:46 +000034
Douglas Gregor7d9663c2010-05-11 06:17:44 +000035 EmitPrefix(OS);
36
Chris Lattner9e493cf2006-03-03 02:32:46 +000037 // Emit the enum information.
38 EmitEnumInfo(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000039
40 // Emit the intrinsic ID -> name table.
41 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wang0d52ff12009-02-24 23:17:49 +000042
43 // Emit the intrinsic ID -> overload table.
44 EmitIntrinsicToOverloadTable(Ints, OS);
45
Chris Lattner9b843b22006-03-09 20:34:19 +000046 // Emit the function name recognizer.
47 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000048
Chris Lattnerf97a00e2006-03-09 22:05:04 +000049 // Emit the intrinsic verifier.
50 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000051
Jim Laskey95af5922007-02-07 20:38:26 +000052 // Emit the intrinsic declaration generator.
53 EmitGenerator(Ints, OS);
54
Duncan Sandsa3355ff2007-12-03 20:06:50 +000055 // Emit the intrinsic parameter attributes.
56 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000057
Duncan Sandsd869b382009-02-14 10:56:35 +000058 // Emit intrinsic alias analysis mod/ref behavior.
59 EmitModRefBehavior(Ints, OS);
60
Chris Lattner3f8b8912006-03-15 01:33:26 +000061 // Emit code to translate GCC builtins into LLVM intrinsics.
62 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor7d9663c2010-05-11 06:17:44 +000063
64 EmitSuffix(OS);
65}
66
67void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
68 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000069 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
70 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000071 "# pragma push_macro(\"setjmp\")\n"
72 "# undef setjmp\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000073 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000074 "#endif\n\n";
75}
76
77void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencer1f409602010-09-24 19:48:47 +000078 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000079 "// let's return it to _setjmp state\n"
Michael J. Spencer08047f62010-09-14 04:27:38 +000080 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencer1f409602010-09-24 19:48:47 +000081 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor7d9663c2010-05-11 06:17:44 +000082 "#endif\n\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000083}
84
85void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +000086 raw_ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000087 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000088 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
89 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
90 OS << " " << Ints[i].EnumName;
91 OS << ((i != e-1) ? ", " : " ");
92 OS << std::string(40-Ints[i].EnumName.size(), ' ')
93 << "// " << Ints[i].Name << "\n";
94 }
95 OS << "#endif\n\n";
96}
Chris Lattner9b843b22006-03-09 20:34:19 +000097
98void IntrinsicEmitter::
99EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000100 raw_ostream &OS) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000101 // Build a 'first character of function name' -> intrinsic # mapping.
102 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +0000103 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattnercc67c752010-09-06 03:58:45 +0000104 IntMapping[Ints[i].Name[5]].push_back(i);
105
Chris Lattner9b843b22006-03-09 20:34:19 +0000106 OS << "// Function name -> enum value recognizer code.\n";
107 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattnercc67c752010-09-06 03:58:45 +0000108 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
109 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
110 OS << " default: break;\n";
111 // Emit the intrinsic matching stuff by first letter.
112 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +0000113 E = IntMapping.end(); I != E; ++I) {
Chris Lattnercc67c752010-09-06 03:58:45 +0000114 OS << " case '" << I->first << "':\n";
115 std::vector<unsigned> &IntList = I->second;
116
117 // Emit all the overloaded intrinsics first, build a table of the
118 // non-overloaded ones.
119 std::vector<StringMatcher::StringPair> MatchTable;
120
121 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
122 unsigned IntNo = IntList[i];
123 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
124 Ints[IntNo].EnumName + ";";
125
126 if (!Ints[IntNo].isOverloaded) {
127 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
128 continue;
129 }
130
131 // For overloaded intrinsics, only the prefix needs to match
132 std::string TheStr = Ints[IntNo].Name.substr(6);
133 TheStr += '.'; // Require "bswap." instead of bswap.
134 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
135 << Result << '\n';
Chris Lattner9b843b22006-03-09 20:34:19 +0000136 }
137
Chris Lattnercc67c752010-09-06 03:58:45 +0000138 // Emit the matcher logic for the fixed length strings.
139 StringMatcher("NameR", MatchTable, OS).Emit(1);
140 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000141 }
Chris Lattnercc67c752010-09-06 03:58:45 +0000142
Chris Lattner9b843b22006-03-09 20:34:19 +0000143 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000144 OS << "#endif\n\n";
145}
146
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000147void IntrinsicEmitter::
148EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000149 raw_ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000150 OS << "// Intrinsic ID to name table\n";
151 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
152 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengf065a6f2006-03-28 22:25:56 +0000153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
154 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000155 OS << "#endif\n\n";
156}
157
Mon P Wang0d52ff12009-02-24 23:17:49 +0000158void IntrinsicEmitter::
159EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000160 raw_ostream &OS) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000161 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000162 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Kramer36a21382012-03-01 02:16:57 +0000163 OS << "static const uint8_t OTable[] = {\n";
164 OS << " 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000165 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramer36a21382012-03-01 02:16:57 +0000166 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
167 if ((i+1)%8 == 0)
168 OS << ",\n 0";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000169 if (Ints[i].isOverloaded)
Benjamin Kramer36a21382012-03-01 02:16:57 +0000170 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wang0d52ff12009-02-24 23:17:49 +0000171 }
Benjamin Kramer36a21382012-03-01 02:16:57 +0000172 OS << "\n};\n\n";
173 // OTable contains a true bit at the position if the intrinsic is overloaded.
174 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wang0d52ff12009-02-24 23:17:49 +0000175 OS << "#endif\n\n";
176}
177
Jim Grosbachda4231f2009-03-26 16:17:51 +0000178/// RecordListComparator - Provide a deterministic comparator for lists of
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000179/// records.
180namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000181 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000182 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000183 bool operator()(const RecPair &LHS,
184 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000185 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000186 const std::vector<Record*> *LHSVec = &LHS.first;
187 const std::vector<Record*> *RHSVec = &RHS.first;
188 unsigned RHSSize = RHSVec->size();
189 unsigned LHSSize = LHSVec->size();
190
Chris Lattner93dc92e2010-03-22 20:56:36 +0000191 for (; i != LHSSize; ++i) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000192 if (i == RHSSize) return false; // RHS is shorter than LHS.
193 if ((*LHSVec)[i] != (*RHSVec)[i])
194 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
Chris Lattner93dc92e2010-03-22 20:56:36 +0000195 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000196
Bill Wendling023422a2008-11-13 12:03:00 +0000197 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000198
199 i = 0;
200 LHSVec = &LHS.second;
201 RHSVec = &RHS.second;
202 RHSSize = RHSVec->size();
203 LHSSize = LHSVec->size();
204
205 for (i = 0; i != LHSSize; ++i) {
206 if (i == RHSSize) return false; // RHS is shorter than LHS.
207 if ((*LHSVec)[i] != (*RHSVec)[i])
208 return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
209 }
210
211 return i != RHSSize;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000212 }
213 };
214}
215
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000216void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000217 raw_ostream &OS) {
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000218 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
219 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
220 OS << " switch (ID) {\n";
Craig Topper655b8de2012-02-05 07:21:30 +0000221 OS << " default: llvm_unreachable(\"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000222
223 // This checking can emit a lot of very common code. To reduce the amount of
224 // code that we emit, batch up cases that have identical types. This avoids
225 // problems where GCC can run out of memory compiling Verifier.cpp.
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000226 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000227 MapTy UniqueArgInfos;
228
229 // Compute the unique argument type info.
230 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000231 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
232 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000233
234 // Loop through the array, emitting one comparison for each batch.
235 for (MapTy::iterator I = UniqueArgInfos.begin(),
236 E = UniqueArgInfos.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000237 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000238 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
239 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000240
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000241 const RecPair &ArgTypes = I->first;
242 const std::vector<Record*> &RetTys = ArgTypes.first;
243 const std::vector<Record*> &ParamTys = ArgTypes.second;
Bob Wilson09b13662009-07-29 16:35:59 +0000244 std::vector<unsigned> OverloadedTypeIndices;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000245
246 OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", "
247 << ParamTys.size();
248
249 // Emit return types.
250 for (unsigned j = 0, je = RetTys.size(); j != je; ++j) {
251 Record *ArgType = RetTys[j];
252 OS << ", ";
253
Chandler Carruth69940402007-08-04 01:51:18 +0000254 if (ArgType->isSubClassOf("LLVMMatchType")) {
255 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000256 assert(Number < OverloadedTypeIndices.size() &&
257 "Invalid matching number!");
258 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000259 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
260 OS << "~(ExtendedElementVectorType | " << Number << ")";
261 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
262 OS << "~(TruncatedElementVectorType | " << Number << ")";
263 else
264 OS << "~" << Number;
Chandler Carruth69940402007-08-04 01:51:18 +0000265 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000267 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000268
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000269 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000270 OverloadedTypeIndices.push_back(j);
271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000273 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000274 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000275 }
276
277 // Emit the parameter types.
278 for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) {
279 Record *ArgType = ParamTys[j];
280 OS << ", ";
281
282 if (ArgType->isSubClassOf("LLVMMatchType")) {
283 unsigned Number = ArgType->getValueAsInt("Number");
Bob Wilson09b13662009-07-29 16:35:59 +0000284 assert(Number < OverloadedTypeIndices.size() &&
285 "Invalid matching number!");
286 Number = OverloadedTypeIndices[Number];
Bob Wilsonbc039792009-01-07 00:09:01 +0000287 if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
288 OS << "~(ExtendedElementVectorType | " << Number << ")";
289 else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
290 OS << "~(TruncatedElementVectorType | " << Number << ")";
291 else
292 OS << "~" << Number;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000293 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000295 OS << getEnumName(VT);
296
Bob Wilson61fc4cf2009-08-11 01:14:02 +0000297 if (EVT(VT).isOverloaded())
Bob Wilson09b13662009-07-29 16:35:59 +0000298 OverloadedTypeIndices.push_back(j + RetTys.size());
299
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000301 throw "Var arg type not last argument";
302 }
Jim Laskey95d97b92007-02-06 18:30:58 +0000303 }
304
Chandler Carruth69940402007-08-04 01:51:18 +0000305 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000306 OS << " break;\n";
307 }
308 OS << " }\n";
309 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000310}
311
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000312
313// NOTE: This must be kept in synch with the version emitted to the .gen file!
314enum IIT_Info {
Chris Lattner46aaf692012-05-17 04:30:58 +0000315 // Common values should be encoded with 0-15.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000316 IIT_Done = 0,
317 IIT_I1 = 1,
318 IIT_I8 = 2,
319 IIT_I16 = 3,
320 IIT_I32 = 4,
321 IIT_I64 = 5,
322 IIT_F32 = 6,
323 IIT_F64 = 7,
324 IIT_V2 = 8,
325 IIT_V4 = 9,
326 IIT_V8 = 10,
327 IIT_V16 = 11,
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000328 IIT_V32 = 12,
329 IIT_MMX = 13,
330 IIT_PTR = 14,
331 IIT_ARG = 15,
Chris Lattner46aaf692012-05-17 04:30:58 +0000332
333 // Values from 16+ are only encodable with the inefficient encoding.
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000334 IIT_METADATA = 16,
335 IIT_EMPTYSTRUCT = 17,
336 IIT_STRUCT2 = 18,
337 IIT_STRUCT3 = 19,
338 IIT_STRUCT4 = 20,
339 IIT_STRUCT5 = 21,
340 IIT_EXTEND_VEC_ARG = 22,
341 IIT_TRUNC_VEC_ARG = 23
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000342};
343
Chris Lattner46aaf692012-05-17 04:30:58 +0000344
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000345static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000346 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000347 if (EVT(VT).isInteger()) {
348 unsigned BitWidth = EVT(VT).getSizeInBits();
349 switch (BitWidth) {
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000350 default: throw "unhandled integer type width in intrinsic!";
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000351 case 1: return Sig.push_back(IIT_I1);
352 case 8: return Sig.push_back(IIT_I8);
353 case 16: return Sig.push_back(IIT_I16);
354 case 32: return Sig.push_back(IIT_I32);
355 case 64: return Sig.push_back(IIT_I64);
356 }
357 }
358
Chris Lattner46aaf692012-05-17 04:30:58 +0000359 switch (VT) {
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000360 default: throw "unhandled MVT in intrinsic!";
Chris Lattner46aaf692012-05-17 04:30:58 +0000361 case MVT::f32: return Sig.push_back(IIT_F32);
362 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner46aaf692012-05-17 04:30:58 +0000363 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
364 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
365 // MVT::OtherVT is used to mean the empty struct type here.
366 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
367 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000368}
369
Francois Pichete4807c12012-05-17 04:00:03 +0000370#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000371#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Francois Pichete4807c12012-05-17 04:00:03 +0000372#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000373
Chris Lattner46aaf692012-05-17 04:30:58 +0000374static void EncodeFixedType(Record *R, unsigned &NextArgNo,
Chris Lattner387c9dc2012-05-17 15:55:41 +0000375 std::vector<unsigned char> &Sig) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000376
377 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000378 unsigned Number = R->getValueAsInt("Number");
379 assert(Number < NextArgNo && "Invalid matching number!");
380 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000381 Sig.push_back(IIT_EXTEND_VEC_ARG);
382 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
383 Sig.push_back(IIT_TRUNC_VEC_ARG);
384 else
385 Sig.push_back(IIT_ARG);
Chris Lattner46aaf692012-05-17 04:30:58 +0000386 return Sig.push_back(Number);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000387 }
388
389 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner46aaf692012-05-17 04:30:58 +0000390
391 // If this is an "any" valuetype, then the type is the type of the next
392 // type in the list specified to getIntrinsic().
393 if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny ||
Chris Lattner15706cb2012-05-17 04:07:48 +0000394 VT == MVT::iPTRAny) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000395 Sig.push_back(IIT_ARG);
396 return Sig.push_back(NextArgNo++);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000397 }
398
399 if (EVT(VT).isVector()) {
400 EVT VVT = VT;
401 switch (VVT.getVectorNumElements()) {
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000402 default: throw "unhandled vector type width in intrinsic!";
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000403 case 2: Sig.push_back(IIT_V2); break;
404 case 4: Sig.push_back(IIT_V4); break;
405 case 8: Sig.push_back(IIT_V8); break;
406 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner46aaf692012-05-17 04:30:58 +0000407 case 32: Sig.push_back(IIT_V32); break;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000408 }
409
410 return EncodeFixedValueType(VVT.getVectorElementType().
411 getSimpleVT().SimpleTy, Sig);
412 }
413
414 if (VT == MVT::iPTR) {
415 Sig.push_back(IIT_PTR);
Pete Cooperb4285112012-05-21 23:21:28 +0000416 unsigned AddrSpace = 0;
417 if (R->isSubClassOf("LLVMQualPointerType")) {
418 AddrSpace = R->getValueAsInt("AddrSpace");
419 assert(AddrSpace < 256 && "Address space exceeds 255");
420 }
421 Sig.push_back(AddrSpace);
Chris Lattner46aaf692012-05-17 04:30:58 +0000422 return EncodeFixedType(R->getValueAsDef("ElTy"), NextArgNo, Sig);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000423 }
424
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000425 EncodeFixedValueType(VT, Sig);
426}
Francois Pichete4807c12012-05-17 04:00:03 +0000427
428#ifdef _MSC_VER
Francois Pichet3aca8792012-05-17 03:38:19 +0000429#pragma optimize("",on)
Francois Pichete4807c12012-05-17 04:00:03 +0000430#endif
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000431
432/// ComputeFixedEncoding - If we can encode the type signature for this
433/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattner387c9dc2012-05-17 15:55:41 +0000434static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
435 std::vector<unsigned char> &TypeSig) {
Chris Lattner46aaf692012-05-17 04:30:58 +0000436 unsigned NextArgNo = 0;
437
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000438 if (Int.IS.RetVTs.empty())
439 TypeSig.push_back(IIT_Done);
440 else if (Int.IS.RetVTs.size() == 1 &&
441 Int.IS.RetVTs[0] == MVT::isVoid)
442 TypeSig.push_back(IIT_Done);
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000443 else {
444 switch (Int.IS.RetVTs.size()) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000445 case 1: break;
446 case 2: TypeSig.push_back(IIT_STRUCT2); break;
447 case 3: TypeSig.push_back(IIT_STRUCT3); break;
448 case 4: TypeSig.push_back(IIT_STRUCT4); break;
449 case 5: TypeSig.push_back(IIT_STRUCT5); break;
450 default: assert(0 && "Unhandled case in struct");
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000451 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000452
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000453 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
454 EncodeFixedType(Int.IS.RetTypeDefs[i], NextArgNo, TypeSig);
455 }
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000456
457 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattner46aaf692012-05-17 04:30:58 +0000458 EncodeFixedType(Int.IS.ParamTypeDefs[i], NextArgNo, TypeSig);
Chris Lattner387c9dc2012-05-17 15:55:41 +0000459}
460
461void printIITEntry(raw_ostream &OS, unsigned char X) {
462 OS << (unsigned)X;
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000463}
464
Jim Laskey95af5922007-02-07 20:38:26 +0000465void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000466 raw_ostream &OS) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000467 OS << "// Global intrinsic function declaration type table.\n";
468 OS << "#ifdef GET_INTRINSTIC_GENERATOR_GLOBAL\n";
469 // NOTE: These enums must be kept in sync with the ones above!
470 OS << "enum IIT_Info {\n";
471 OS << " IIT_Done = 0,\n";
472 OS << " IIT_I1 = 1,\n";
473 OS << " IIT_I8 = 2,\n";
474 OS << " IIT_I16 = 3,\n";
475 OS << " IIT_I32 = 4,\n";
476 OS << " IIT_I64 = 5,\n";
477 OS << " IIT_F32 = 6,\n";
478 OS << " IIT_F64 = 7,\n";
479 OS << " IIT_V2 = 8,\n";
480 OS << " IIT_V4 = 9,\n";
481 OS << " IIT_V8 = 10,\n";
482 OS << " IIT_V16 = 11,\n";
Chris Lattnerd7cf5eb2012-05-17 05:03:24 +0000483 OS << " IIT_V32 = 12,\n";
484 OS << " IIT_MMX = 13,\n";
485 OS << " IIT_PTR = 14,\n";
486 OS << " IIT_ARG = 15,\n";
487 OS << " IIT_METADATA = 16,\n";
488 OS << " IIT_EMPTYSTRUCT = 17,\n";
489 OS << " IIT_STRUCT2 = 18,\n";
490 OS << " IIT_STRUCT3 = 19,\n";
491 OS << " IIT_STRUCT4 = 20,\n";
492 OS << " IIT_STRUCT5 = 21,\n";
493 OS << " IIT_EXTEND_VEC_ARG = 22,\n";
494 OS << " IIT_TRUNC_VEC_ARG = 23\n";
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000495 OS << "};\n\n";
496
Jim Laskey95af5922007-02-07 20:38:26 +0000497
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000498 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
499 // capture it in this vector, otherwise store a ~0U.
500 std::vector<unsigned> FixedEncodings;
Jim Laskey95af5922007-02-07 20:38:26 +0000501
Chris Lattner387c9dc2012-05-17 15:55:41 +0000502 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
503
504 std::vector<unsigned char> TypeSig;
505
Jim Laskey95af5922007-02-07 20:38:26 +0000506 // Compute the unique argument type info.
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000507 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattner387c9dc2012-05-17 15:55:41 +0000508 // Get the signature for the intrinsic.
509 TypeSig.clear();
510 ComputeFixedEncoding(Ints[i], TypeSig);
511
512 // Check to see if we can encode it into a 32-bit word. We can only encode
513 // 8 nibbles into a 32-bit word.
514 if (TypeSig.size() <= 8) {
515 bool Failed = false;
516 unsigned Result = 0;
517 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
518 // If we had an unencodable argument, bail out.
519 if (TypeSig[i] > 15) {
520 Failed = true;
521 break;
522 }
523 Result = (Result << 4) | TypeSig[e-i-1];
524 }
525
526 // If this could be encoded into a 31-bit word, return it.
527 if (!Failed && (Result >> 31) == 0) {
528 FixedEncodings.push_back(Result);
529 continue;
530 }
531 }
532
533 // Otherwise, we're going to unique the sequence into the
534 // LongEncodingTable, and use its offset in the 32-bit table instead.
535 LongEncodingTable.add(TypeSig);
536
537 // This is a placehold that we'll replace after the table is laid out.
538 FixedEncodings.push_back(~0U);
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000539 }
540
Chris Lattner387c9dc2012-05-17 15:55:41 +0000541 LongEncodingTable.layout();
542
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000543 OS << "static const unsigned IIT_Table[] = {\n ";
544
545 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
546 if ((i & 7) == 7)
547 OS << "\n ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000548
549 // If the entry fit in the table, just emit it.
550 if (FixedEncodings[i] != ~0U) {
Chris Lattnera98aa6a2012-05-16 06:34:44 +0000551 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattner387c9dc2012-05-17 15:55:41 +0000552 continue;
Jim Laskey95af5922007-02-07 20:38:26 +0000553 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000554
555 TypeSig.clear();
556 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000557
Chris Lattner387c9dc2012-05-17 15:55:41 +0000558
559 // Otherwise, emit the offset into the long encoding table. We emit it this
560 // way so that it is easier to read the offset in the .def file.
561 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey95af5922007-02-07 20:38:26 +0000562 }
Chris Lattner387c9dc2012-05-17 15:55:41 +0000563
564 OS << "0\n};\n\n";
565
566 // Emit the shared table of register lists.
567 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
568 if (!LongEncodingTable.empty())
569 LongEncodingTable.emit(OS, printIITEntry);
570 OS << " 255\n};\n\n";
571
572 OS << "#endif\n\n"; // End of GET_INTRINSTIC_GENERATOR_GLOBAL
Jim Laskey95af5922007-02-07 20:38:26 +0000573}
574
John McCallbd0fa4c2011-05-28 06:31:34 +0000575namespace {
576 enum ModRefKind {
577 MRK_none,
578 MRK_readonly,
579 MRK_readnone
580 };
581
582 ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
583 switch (intrinsic.ModRef) {
584 case CodeGenIntrinsic::NoMem:
585 return MRK_readnone;
586 case CodeGenIntrinsic::ReadArgMem:
587 case CodeGenIntrinsic::ReadMem:
588 return MRK_readonly;
589 case CodeGenIntrinsic::ReadWriteArgMem:
590 case CodeGenIntrinsic::ReadWriteMem:
591 return MRK_none;
592 }
Craig Topper655b8de2012-02-05 07:21:30 +0000593 llvm_unreachable("bad mod-ref kind");
John McCallbd0fa4c2011-05-28 06:31:34 +0000594 }
595
596 struct AttributeComparator {
597 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
598 // Sort throwing intrinsics after non-throwing intrinsics.
599 if (L->canThrow != R->canThrow)
600 return R->canThrow;
601
602 // Try to order by readonly/readnone attribute.
603 ModRefKind LK = getModRefKind(*L);
604 ModRefKind RK = getModRefKind(*R);
605 if (LK != RK) return (LK > RK);
606
607 // Order by argument attributes.
608 // This is reliable because each side is already sorted internally.
609 return (L->ArgumentAttributes < R->ArgumentAttributes);
610 }
611 };
612}
613
Chris Lattner048ffb22009-01-12 01:18:58 +0000614/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattner4e5f3592006-03-09 22:37:52 +0000615void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000616EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000617 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
618 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000619 if (TargetOnly)
620 OS << "static AttrListPtr getAttributes(" << TargetPrefix
John McCallbd0fa4c2011-05-28 06:31:34 +0000621 << "Intrinsic::ID id) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000622 else
John McCallbd0fa4c2011-05-28 06:31:34 +0000623 OS << "AttrListPtr Intrinsic::getAttributes(ID id) {\n";
624
Craig Topper1f595232012-02-28 06:32:00 +0000625 // Compute the maximum number of attribute arguments and the map
626 typedef std::map<const CodeGenIntrinsic*, unsigned,
627 AttributeComparator> UniqAttrMapTy;
628 UniqAttrMapTy UniqAttributes;
John McCallbd0fa4c2011-05-28 06:31:34 +0000629 unsigned maxArgAttrs = 0;
Craig Topper1f595232012-02-28 06:32:00 +0000630 unsigned AttrNum = 0;
Chris Lattner7056de32006-03-24 01:13:55 +0000631 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCallbd0fa4c2011-05-28 06:31:34 +0000632 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCallbd0fa4c2011-05-28 06:31:34 +0000633 maxArgAttrs =
634 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topper1f595232012-02-28 06:32:00 +0000635 unsigned &N = UniqAttributes[&intrinsic];
636 if (N) continue;
637 assert(AttrNum < 256 && "Too many unique attributes for table!");
638 N = ++AttrNum;
Chris Lattner7056de32006-03-24 01:13:55 +0000639 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000640
641 // Emit an array of AttributeWithIndex. Most intrinsics will have
642 // at least one entry, for the function itself (index ~1), which is
643 // usually nounwind.
Craig Topper1f595232012-02-28 06:32:00 +0000644 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topper1f595232012-02-28 06:32:00 +0000645
646 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
647 const CodeGenIntrinsic &intrinsic = Ints[i];
648
649 OS << " " << UniqAttributes[&intrinsic] << ", // "
650 << intrinsic.Name << "\n";
651 }
652 OS << " };\n\n";
653
John McCallbd0fa4c2011-05-28 06:31:34 +0000654 OS << " AttributeWithIndex AWI[" << maxArgAttrs+1 << "];\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000655 OS << " unsigned NumAttrs = 0;\n";
Craig Topperb57b1702012-04-13 06:14:57 +0000656 OS << " if (id != 0) {\n";
657 OS << " switch(IntrinsicsToAttributesMap[id - ";
658 if (TargetOnly)
659 OS << "Intrinsic::num_intrinsics";
660 else
661 OS << "1";
662 OS << "]) {\n";
663 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topper1f595232012-02-28 06:32:00 +0000664 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
665 E = UniqAttributes.end(); I != E; ++I) {
Craig Topperb57b1702012-04-13 06:14:57 +0000666 OS << " case " << I->second << ":\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000667
Craig Topper1f595232012-02-28 06:32:00 +0000668 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCallbd0fa4c2011-05-28 06:31:34 +0000669
670 // Keep track of the number of attributes we're writing out.
671 unsigned numAttrs = 0;
672
673 // The argument attributes are alreadys sorted by argument index.
674 for (unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); ai != ae;) {
675 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topper1f595232012-02-28 06:32:00 +0000676
Craig Topperb57b1702012-04-13 06:14:57 +0000677 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get("
John McCallbd0fa4c2011-05-28 06:31:34 +0000678 << argNo+1 << ", ";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000679
John McCallbd0fa4c2011-05-28 06:31:34 +0000680 bool moreThanOne = false;
681
682 do {
683 if (moreThanOne) OS << '|';
684
685 switch (intrinsic.ArgumentAttributes[ai].second) {
Chris Lattnerd4a27002009-01-12 02:41:37 +0000686 case CodeGenIntrinsic::NoCapture:
John McCallbd0fa4c2011-05-28 06:31:34 +0000687 OS << "Attribute::NoCapture";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000688 break;
689 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000690
691 ++ai;
692 moreThanOne = true;
693 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
694
695 OS << ");\n";
696 }
697
698 ModRefKind modRef = getModRefKind(intrinsic);
699
700 if (!intrinsic.canThrow || modRef) {
Craig Topperb57b1702012-04-13 06:14:57 +0000701 OS << " AWI[" << numAttrs++ << "] = AttributeWithIndex::get(~0, ";
John McCallbd0fa4c2011-05-28 06:31:34 +0000702 if (!intrinsic.canThrow) {
703 OS << "Attribute::NoUnwind";
704 if (modRef) OS << '|';
705 }
706 switch (modRef) {
707 case MRK_none: break;
708 case MRK_readonly: OS << "Attribute::ReadOnly"; break;
709 case MRK_readnone: OS << "Attribute::ReadNone"; break;
Chris Lattnerd4a27002009-01-12 02:41:37 +0000710 }
711 OS << ");\n";
712 }
John McCallbd0fa4c2011-05-28 06:31:34 +0000713
714 if (numAttrs) {
Craig Topperb57b1702012-04-13 06:14:57 +0000715 OS << " NumAttrs = " << numAttrs << ";\n";
716 OS << " break;\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000717 } else {
Craig Topperb57b1702012-04-13 06:14:57 +0000718 OS << " return AttrListPtr();\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000719 }
Chris Lattner10dae942009-01-12 01:27:55 +0000720 }
721
Craig Topperb57b1702012-04-13 06:14:57 +0000722 OS << " }\n";
Chris Lattner10dae942009-01-12 01:27:55 +0000723 OS << " }\n";
John McCallbd0fa4c2011-05-28 06:31:34 +0000724 OS << " return AttrListPtr::get(AWI, NumAttrs);\n";
Chris Lattner048ffb22009-01-12 01:18:58 +0000725 OS << "}\n";
Chris Lattnerd4a27002009-01-12 02:41:37 +0000726 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000727}
Chris Lattner022f64f2006-03-13 23:08:44 +0000728
Duncan Sandsd869b382009-02-14 10:56:35 +0000729/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
730void IntrinsicEmitter::
Daniel Dunbar1a551802009-07-03 00:10:29 +0000731EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000732 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
733 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
734 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
735 << "\"Unknown intrinsic.\");\n\n";
736
737 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
738 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000739 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000740 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sandsd869b382009-02-14 10:56:35 +0000741 switch (Ints[i].ModRef) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000742 case CodeGenIntrinsic::NoMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000743 OS << "DoesNotAccessMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000744 break;
745 case CodeGenIntrinsic::ReadArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000746 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman9423f632010-11-09 20:07:20 +0000747 break;
Duncan Sandsd869b382009-02-14 10:56:35 +0000748 case CodeGenIntrinsic::ReadMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000749 OS << "OnlyReadsMemory,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000750 break;
Dan Gohman7365c092010-08-05 23:36:21 +0000751 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000752 OS << "OnlyAccessesArgumentPointees,\n";
753 break;
754 case CodeGenIntrinsic::ReadWriteMem:
755 OS << "UnknownModRefBehavior,\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000756 break;
757 }
758 }
Benjamin Kramerb519a0f2012-03-01 01:18:32 +0000759 OS << "};\n\n"
760 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
761 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sandsd869b382009-02-14 10:56:35 +0000762}
763
Chris Lattner331bf922008-01-04 04:38:35 +0000764/// EmitTargetBuiltins - All of the builtins in the specified map are for the
765/// same target, and we already checked it.
766static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesen49de9822009-02-05 01:49:45 +0000767 const std::string &TargetPrefix,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000768 raw_ostream &OS) {
Chris Lattner331bf922008-01-04 04:38:35 +0000769
Chris Lattner298b1762010-09-06 03:14:45 +0000770 std::vector<StringMatcher::StringPair> Results;
Chris Lattner331bf922008-01-04 04:38:35 +0000771
Chris Lattner298b1762010-09-06 03:14:45 +0000772 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
773 E = BIM.end(); I != E; ++I) {
774 std::string ResultCode =
775 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
776 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner331bf922008-01-04 04:38:35 +0000777 }
Chris Lattner298b1762010-09-06 03:14:45 +0000778
779 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner331bf922008-01-04 04:38:35 +0000780}
781
782
Chris Lattner3f8b8912006-03-15 01:33:26 +0000783void IntrinsicEmitter::
784EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar1a551802009-07-03 00:10:29 +0000785 raw_ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000786 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000787 BIMTy BuiltinMap;
788 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
789 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000790 // Get the map for this target prefix.
791 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
792
793 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
794 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000795 throw "Intrinsic '" + Ints[i].TheDef->getName() +
796 "': duplicate GCC builtin name!";
797 }
798 }
799
800 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
801 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
802 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
803 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
804 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000805
806 if (TargetOnly) {
807 OS << "static " << TargetPrefix << "Intrinsic::ID "
808 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000809 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000810 } else {
811 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner298b1762010-09-06 03:14:45 +0000812 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000813 }
814
Chris Lattner298b1762010-09-06 03:14:45 +0000815 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
816 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000817
Chris Lattner3f8b8912006-03-15 01:33:26 +0000818 // Note: this could emit significantly better code if we cared.
819 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000820 OS << " ";
821 if (!I->first.empty())
Chris Lattner298b1762010-09-06 03:14:45 +0000822 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000823 else
824 OS << "/* Target Independent Builtins */ ";
825 OS << "{\n";
826
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000827 // Emit the comparisons for this target prefix.
Dale Johannesen49de9822009-02-05 01:49:45 +0000828 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000829 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000830 }
Chris Lattner298b1762010-09-06 03:14:45 +0000831 OS << " return ";
832 if (!TargetPrefix.empty())
833 OS << "(" << TargetPrefix << "Intrinsic::ID)";
834 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesen49de9822009-02-05 01:49:45 +0000835 OS << "}\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000836 OS << "#endif\n\n";
837}