blob: 87f9c90dc4a759a24564b41f9cc373d289fc8756 [file] [log] [blame]
Chris Lattnerc313d0b2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-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 Lattnerc313d0b2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000014#include "CodeGenIntrinsics.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000015#include "CodeGenTarget.h"
Chris Lattnera3b0f522012-05-17 15:55:41 +000016#include "SequenceToOffsetTable.h"
Richard Smith6bc9df32014-04-20 20:26:39 +000017#include "TableGenBackends.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000018#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000019#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000020#include "llvm/TableGen/Record.h"
Douglas Gregor12c1cd32012-05-02 17:32:48 +000021#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000022#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenc4e24682006-03-15 02:51:05 +000023#include <algorithm>
Chris Lattnerc313d0b2006-03-03 02:32:46 +000024using namespace llvm;
25
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000026namespace {
27class IntrinsicEmitter {
28 RecordKeeper &Records;
29 bool TargetOnly;
30 std::string TargetPrefix;
31
32public:
33 IntrinsicEmitter(RecordKeeper &R, bool T)
34 : Records(R), TargetOnly(T) {}
35
36 void run(raw_ostream &OS);
37
38 void EmitPrefix(raw_ostream &OS);
39
40 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
41 raw_ostream &OS);
42
43 void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
44 raw_ostream &OS);
45 void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
46 raw_ostream &OS);
47 void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
48 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000049 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
50 raw_ostream &OS);
51 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
52 raw_ostream &OS);
53 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
54 raw_ostream &OS);
55 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
56 raw_ostream &OS);
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +000057 void EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
58 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000059 void EmitSuffix(raw_ostream &OS);
60};
61} // End anonymous namespace
62
Chris Lattnerc313d0b2006-03-03 02:32:46 +000063//===----------------------------------------------------------------------===//
Chris Lattnerc313d0b2006-03-03 02:32:46 +000064// IntrinsicEmitter Implementation
65//===----------------------------------------------------------------------===//
66
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000067void IntrinsicEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000068 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
69
Dale Johannesenb842d522009-02-05 01:49:45 +000070 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000071
Dale Johannesenb842d522009-02-05 01:49:45 +000072 if (TargetOnly && !Ints.empty())
73 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattnerc313d0b2006-03-03 02:32:46 +000074
Douglas Gregor6739a892010-05-11 06:17:44 +000075 EmitPrefix(OS);
76
Chris Lattnerc313d0b2006-03-03 02:32:46 +000077 // Emit the enum information.
78 EmitEnumInfo(Ints, OS);
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000079
80 // Emit the intrinsic ID -> name table.
81 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wangb4024932009-02-24 23:17:49 +000082
83 // Emit the intrinsic ID -> overload table.
84 EmitIntrinsicToOverloadTable(Ints, OS);
85
Chris Lattner6d8104e2006-03-09 20:34:19 +000086 // Emit the function name recognizer.
87 EmitFnNameRecognizer(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000088
Jim Laskey2682ea62007-02-07 20:38:26 +000089 // Emit the intrinsic declaration generator.
90 EmitGenerator(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000091
Duncan Sands38ef3a82007-12-03 20:06:50 +000092 // Emit the intrinsic parameter attributes.
93 EmitAttributes(Ints, OS);
Chris Lattnerfea17a92006-03-13 23:08:44 +000094
Duncan Sands73247d22009-02-14 10:56:35 +000095 // Emit intrinsic alias analysis mod/ref behavior.
96 EmitModRefBehavior(Ints, OS);
97
Chris Lattner402a5732006-03-15 01:33:26 +000098 // Emit code to translate GCC builtins into LLVM intrinsics.
99 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor6739a892010-05-11 06:17:44 +0000100
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000101 // Emit code to translate MS builtins into LLVM intrinsics.
102 EmitIntrinsicToMSBuiltinMap(Ints, OS);
103
Douglas Gregor6739a892010-05-11 06:17:44 +0000104 EmitSuffix(OS);
105}
106
107void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
108 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000109 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
110 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000111 "# pragma push_macro(\"setjmp\")\n"
112 "# undef setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000113 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000114 "#endif\n\n";
115}
116
117void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencerded5f662010-09-24 19:48:47 +0000118 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000119 "// let's return it to _setjmp state\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000120 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000121 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000122 "#endif\n\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000123}
124
125void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000126 raw_ostream &OS) {
Chris Lattner6d8104e2006-03-09 20:34:19 +0000127 OS << "// Enum values for Intrinsics.h\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000128 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
129 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
130 OS << " " << Ints[i].EnumName;
131 OS << ((i != e-1) ? ", " : " ");
Justin Holewinski8a5bf7f2014-07-17 11:23:29 +0000132 if (Ints[i].EnumName.size() < 40)
133 OS << std::string(40-Ints[i].EnumName.size(), ' ');
134 OS << " // " << Ints[i].Name << "\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000135 }
136 OS << "#endif\n\n";
137}
Chris Lattner6d8104e2006-03-09 20:34:19 +0000138
139void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000140EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000141 raw_ostream &OS) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000142 // Build a 'first character of function name' -> intrinsic # mapping.
143 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner6d8104e2006-03-09 20:34:19 +0000144 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattner07b332f2010-09-06 03:58:45 +0000145 IntMapping[Ints[i].Name[5]].push_back(i);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000146
Chris Lattner6d8104e2006-03-09 20:34:19 +0000147 OS << "// Function name -> enum value recognizer code.\n";
148 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattner07b332f2010-09-06 03:58:45 +0000149 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
150 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
151 OS << " default: break;\n";
152 // Emit the intrinsic matching stuff by first letter.
153 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner6d8104e2006-03-09 20:34:19 +0000154 E = IntMapping.end(); I != E; ++I) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000155 OS << " case '" << I->first << "':\n";
156 std::vector<unsigned> &IntList = I->second;
157
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000158 // Sort in reverse order of intrinsic name so "abc.def" appears after
159 // "abd.def.ghi" in the overridden name matcher
160 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
161 return Ints[i].Name > Ints[j].Name;
162 });
Justin Holewinskib3d630c2013-07-25 12:32:00 +0000163
Chris Lattner07b332f2010-09-06 03:58:45 +0000164 // Emit all the overloaded intrinsics first, build a table of the
165 // non-overloaded ones.
166 std::vector<StringMatcher::StringPair> MatchTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000167
Chris Lattner07b332f2010-09-06 03:58:45 +0000168 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
169 unsigned IntNo = IntList[i];
170 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
171 Ints[IntNo].EnumName + ";";
172
173 if (!Ints[IntNo].isOverloaded) {
174 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
175 continue;
176 }
177
178 // For overloaded intrinsics, only the prefix needs to match
179 std::string TheStr = Ints[IntNo].Name.substr(6);
180 TheStr += '.'; // Require "bswap." instead of bswap.
181 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
182 << Result << '\n';
Chris Lattner6d8104e2006-03-09 20:34:19 +0000183 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000184
Chris Lattner07b332f2010-09-06 03:58:45 +0000185 // Emit the matcher logic for the fixed length strings.
186 StringMatcher("NameR", MatchTable, OS).Emit(1);
187 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner6d8104e2006-03-09 20:34:19 +0000188 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000189
Chris Lattner6d8104e2006-03-09 20:34:19 +0000190 OS << " }\n";
Chris Lattner6efe8632006-03-09 22:05:04 +0000191 OS << "#endif\n\n";
192}
193
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000194void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000195EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000196 raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000197 OS << "// Intrinsic ID to name table\n";
198 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
199 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000200 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
201 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000202 OS << "#endif\n\n";
203}
204
Mon P Wangb4024932009-02-24 23:17:49 +0000205void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000206EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000207 raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000208 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000209 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000210 OS << "static const uint8_t OTable[] = {\n";
211 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000212 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000213 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
214 if ((i+1)%8 == 0)
215 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000216 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000217 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000218 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000219 OS << "\n};\n\n";
220 // OTable contains a true bit at the position if the intrinsic is overloaded.
221 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000222 OS << "#endif\n\n";
223}
224
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000225
Chris Lattnerf39c2782012-05-27 18:28:35 +0000226// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000227enum IIT_Info {
Robert Khasanov65c27562014-10-20 19:25:05 +0000228 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000229 IIT_Done = 0,
230 IIT_I1 = 1,
231 IIT_I8 = 2,
232 IIT_I16 = 3,
233 IIT_I32 = 4,
234 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000235 IIT_F16 = 6,
236 IIT_F32 = 7,
237 IIT_F64 = 8,
238 IIT_V2 = 9,
239 IIT_V4 = 10,
240 IIT_V8 = 11,
241 IIT_V16 = 12,
242 IIT_V32 = 13,
Robert Khasanov65c27562014-10-20 19:25:05 +0000243 IIT_PTR = 14,
244 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000245
Robert Khasanov65c27562014-10-20 19:25:05 +0000246 // Values from 16+ are only encodable with the inefficient encoding.
247 IIT_V64 = 16,
Robert Khasanovb25e5622014-09-30 11:32:22 +0000248 IIT_MMX = 17,
249 IIT_METADATA = 18,
250 IIT_EMPTYSTRUCT = 19,
251 IIT_STRUCT2 = 20,
252 IIT_STRUCT3 = 21,
253 IIT_STRUCT4 = 22,
254 IIT_STRUCT5 = 23,
255 IIT_EXTEND_ARG = 24,
256 IIT_TRUNC_ARG = 25,
257 IIT_ANYPTR = 26,
258 IIT_V1 = 27,
259 IIT_VARARG = 28,
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000260 IIT_HALF_VEC_ARG = 29,
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000261 IIT_SAME_VEC_WIDTH_ARG = 30,
262 IIT_PTR_TO_ARG = 31
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000263};
264
Chris Lattner827b2532012-05-17 04:30:58 +0000265
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000266static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000267 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000268 if (MVT(VT).isInteger()) {
269 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000270 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000271 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000272 case 1: return Sig.push_back(IIT_I1);
273 case 8: return Sig.push_back(IIT_I8);
274 case 16: return Sig.push_back(IIT_I16);
275 case 32: return Sig.push_back(IIT_I32);
276 case 64: return Sig.push_back(IIT_I64);
277 }
278 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000279
Chris Lattner827b2532012-05-17 04:30:58 +0000280 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000281 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000282 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000283 case MVT::f32: return Sig.push_back(IIT_F32);
284 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000285 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
286 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
287 // MVT::OtherVT is used to mean the empty struct type here.
288 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000289 // MVT::isVoid is used to represent varargs here.
290 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000291 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000292}
293
Francois Pichet9522bfc2012-05-17 04:00:03 +0000294#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000295#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000296#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000297
Chris Lattnerc4644162012-05-27 16:39:08 +0000298static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000299 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000300
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000301 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000302 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000303 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000304 if (R->isSubClassOf("LLVMExtendedType"))
305 Sig.push_back(IIT_EXTEND_ARG);
306 else if (R->isSubClassOf("LLVMTruncatedType"))
307 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000308 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
309 Sig.push_back(IIT_HALF_VEC_ARG);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000310 else if (R->isSubClassOf("LLVMVectorSameWidth")) {
311 Sig.push_back(IIT_SAME_VEC_WIDTH_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000312 Sig.push_back((Number << 3) | ArgCodes[Number]);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +0000313 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy"));
314 EncodeFixedValueType(VT, Sig);
315 return;
316 }
Elena Demikhovskyfb81b932014-12-25 07:49:20 +0000317 else if (R->isSubClassOf("LLVMPointerTo")) {
318 Sig.push_back(IIT_PTR_TO_ARG);
319 }
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000320 else
321 Sig.push_back(IIT_ARG);
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000322 return Sig.push_back((Number << 3) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000323 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000324
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000325 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000326
Chris Lattnerc4644162012-05-27 16:39:08 +0000327 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000328 switch (VT) {
329 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000330 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
331 case MVT::vAny: ++Tmp; // FALL THROUGH.
332 case MVT::fAny: ++Tmp; // FALL THROUGH.
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000333 case MVT::iAny: ++Tmp; // FALL THROUGH.
334 case MVT::Any: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000335 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000336 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000337 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000338
Chris Lattnerc4644162012-05-27 16:39:08 +0000339 // Figure out what arg # this is consuming, and remember what kind it was.
340 unsigned ArgNo = ArgCodes.size();
341 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000342
Ramkumar Ramachandra75a4f352015-01-22 20:14:38 +0000343 // Encode what sort of argument it must be in the low 3 bits of the ArgNo.
344 return Sig.push_back((ArgNo << 3) | Tmp);
Chris Lattnerc4644162012-05-27 16:39:08 +0000345 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000346
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000347 case MVT::iPTR: {
348 unsigned AddrSpace = 0;
349 if (R->isSubClassOf("LLVMQualPointerType")) {
350 AddrSpace = R->getValueAsInt("AddrSpace");
351 assert(AddrSpace < 256 && "Address space exceeds 255");
352 }
353 if (AddrSpace) {
354 Sig.push_back(IIT_ANYPTR);
355 Sig.push_back(AddrSpace);
356 } else {
357 Sig.push_back(IIT_PTR);
358 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000359 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000360 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000361 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000362
Craig Topper8561de92014-01-24 20:50:47 +0000363 if (MVT(VT).isVector()) {
364 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000365 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000366 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000367 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000368 case 2: Sig.push_back(IIT_V2); break;
369 case 4: Sig.push_back(IIT_V4); break;
370 case 8: Sig.push_back(IIT_V8); break;
371 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000372 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000373 case 64: Sig.push_back(IIT_V64); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000374 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000375
Craig Topper8561de92014-01-24 20:50:47 +0000376 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000377 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000378
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000379 EncodeFixedValueType(VT, Sig);
380}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000381
382#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000383#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000384#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000385
386/// ComputeFixedEncoding - If we can encode the type signature for this
387/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000388static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
389 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000390 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000391
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000392 if (Int.IS.RetVTs.empty())
393 TypeSig.push_back(IIT_Done);
394 else if (Int.IS.RetVTs.size() == 1 &&
395 Int.IS.RetVTs[0] == MVT::isVoid)
396 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000397 else {
398 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000399 case 1: break;
400 case 2: TypeSig.push_back(IIT_STRUCT2); break;
401 case 3: TypeSig.push_back(IIT_STRUCT3); break;
402 case 4: TypeSig.push_back(IIT_STRUCT4); break;
403 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000404 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000405 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000406
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000407 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000408 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000409 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000410
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000411 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000412 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000413}
414
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000415static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000416 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000417}
418
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000419void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000420 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000421 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
422 // capture it in this vector, otherwise store a ~0U.
423 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000424
Chris Lattnera3b0f522012-05-17 15:55:41 +0000425 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000426
Chris Lattnera3b0f522012-05-17 15:55:41 +0000427 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000428
Jim Laskey2682ea62007-02-07 20:38:26 +0000429 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000430 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000431 // Get the signature for the intrinsic.
432 TypeSig.clear();
433 ComputeFixedEncoding(Ints[i], TypeSig);
434
435 // Check to see if we can encode it into a 32-bit word. We can only encode
436 // 8 nibbles into a 32-bit word.
437 if (TypeSig.size() <= 8) {
438 bool Failed = false;
439 unsigned Result = 0;
440 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
441 // If we had an unencodable argument, bail out.
442 if (TypeSig[i] > 15) {
443 Failed = true;
444 break;
445 }
446 Result = (Result << 4) | TypeSig[e-i-1];
447 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000448
Chris Lattnera3b0f522012-05-17 15:55:41 +0000449 // If this could be encoded into a 31-bit word, return it.
450 if (!Failed && (Result >> 31) == 0) {
451 FixedEncodings.push_back(Result);
452 continue;
453 }
454 }
455
456 // Otherwise, we're going to unique the sequence into the
457 // LongEncodingTable, and use its offset in the 32-bit table instead.
458 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000459
Chris Lattnera3b0f522012-05-17 15:55:41 +0000460 // This is a placehold that we'll replace after the table is laid out.
461 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000462 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000463
Chris Lattnera3b0f522012-05-17 15:55:41 +0000464 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000465
Chris Lattnerf39c2782012-05-27 18:28:35 +0000466 OS << "// Global intrinsic function declaration type table.\n";
467 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
468
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000469 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000470
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000471 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
472 if ((i & 7) == 7)
473 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000474
Chris Lattnera3b0f522012-05-17 15:55:41 +0000475 // If the entry fit in the table, just emit it.
476 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000477 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000478 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000479 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000480
Chris Lattnera3b0f522012-05-17 15:55:41 +0000481 TypeSig.clear();
482 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000483
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000484
Chris Lattnera3b0f522012-05-17 15:55:41 +0000485 // Otherwise, emit the offset into the long encoding table. We emit it this
486 // way so that it is easier to read the offset in the .def file.
487 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000488 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000489
Chris Lattnera3b0f522012-05-17 15:55:41 +0000490 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000491
Chris Lattnera3b0f522012-05-17 15:55:41 +0000492 // Emit the shared table of register lists.
493 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
494 if (!LongEncodingTable.empty())
495 LongEncodingTable.emit(OS, printIITEntry);
496 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000497
Patrik Hägglundca210d82012-05-23 12:34:56 +0000498 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000499}
500
Richard Smith6bc9df32014-04-20 20:26:39 +0000501namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000502enum ModRefKind {
503 MRK_none,
504 MRK_readonly,
505 MRK_readnone
506};
Richard Smith6bc9df32014-04-20 20:26:39 +0000507}
John McCall375dcc92011-05-28 06:31:34 +0000508
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000509static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
510 switch (intrinsic.ModRef) {
511 case CodeGenIntrinsic::NoMem:
512 return MRK_readnone;
513 case CodeGenIntrinsic::ReadArgMem:
514 case CodeGenIntrinsic::ReadMem:
515 return MRK_readonly;
516 case CodeGenIntrinsic::ReadWriteArgMem:
517 case CodeGenIntrinsic::ReadWriteMem:
518 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000519 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000520 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000521}
522
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000523namespace {
524struct AttributeComparator {
525 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
526 // Sort throwing intrinsics after non-throwing intrinsics.
527 if (L->canThrow != R->canThrow)
528 return R->canThrow;
529
Eli Bendersky2281ef92014-03-18 23:51:07 +0000530 if (L->isNoDuplicate != R->isNoDuplicate)
531 return R->isNoDuplicate;
532
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000533 if (L->isNoReturn != R->isNoReturn)
534 return R->isNoReturn;
535
536 // Try to order by readonly/readnone attribute.
537 ModRefKind LK = getModRefKind(*L);
538 ModRefKind RK = getModRefKind(*R);
539 if (LK != RK) return (LK > RK);
540
541 // Order by argument attributes.
542 // This is reliable because each side is already sorted internally.
543 return (L->ArgumentAttributes < R->ArgumentAttributes);
544 }
545};
546} // End anonymous namespace
547
Chris Lattner49b7ee12009-01-12 01:18:58 +0000548/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000549void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000550EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000551 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
552 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000553 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000554 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000555 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000556 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000557 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000558
Craig Topperccd651c2012-02-28 06:32:00 +0000559 // Compute the maximum number of attribute arguments and the map
560 typedef std::map<const CodeGenIntrinsic*, unsigned,
561 AttributeComparator> UniqAttrMapTy;
562 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000563 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000564 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000565 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000566 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000567 maxArgAttrs =
568 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000569 unsigned &N = UniqAttributes[&intrinsic];
570 if (N) continue;
571 assert(AttrNum < 256 && "Too many unique attributes for table!");
572 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000573 }
John McCall375dcc92011-05-28 06:31:34 +0000574
Bill Wendling4d3491c2013-01-27 03:25:05 +0000575 // Emit an array of AttributeSet. Most intrinsics will have at least one
576 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000577 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000578
579 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
580 const CodeGenIntrinsic &intrinsic = Ints[i];
581
582 OS << " " << UniqAttributes[&intrinsic] << ", // "
583 << intrinsic.Name << "\n";
584 }
585 OS << " };\n\n";
586
Bill Wendling4d3491c2013-01-27 03:25:05 +0000587 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000588 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000589 OS << " if (id != 0) {\n";
590 OS << " switch(IntrinsicsToAttributesMap[id - ";
591 if (TargetOnly)
592 OS << "Intrinsic::num_intrinsics";
593 else
594 OS << "1";
595 OS << "]) {\n";
596 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000597 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
598 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000599 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000600
Craig Topperccd651c2012-02-28 06:32:00 +0000601 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000602
603 // Keep track of the number of attributes we're writing out.
604 unsigned numAttrs = 0;
605
606 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000607 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
608 if (ae) {
609 while (ai != ae) {
610 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000611
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000612 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
613 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000614
Bill Wendlinged42e792012-10-10 06:13:42 +0000615 do {
616 switch (intrinsic.ArgumentAttributes[ai].second) {
617 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000618 if (addComma)
619 OS << ",";
620 OS << "Attribute::NoCapture";
621 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000622 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000623 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000624 if (addComma)
625 OS << ",";
626 OS << "Attribute::ReadOnly";
627 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000628 break;
629 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000630 if (addComma)
631 OS << ",";
632 OS << "Attributes::ReadNone";
633 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000634 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000635 }
John McCall375dcc92011-05-28 06:31:34 +0000636
Bill Wendlinged42e792012-10-10 06:13:42 +0000637 ++ai;
638 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000639 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000640 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000641 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000642 }
John McCall375dcc92011-05-28 06:31:34 +0000643 }
644
645 ModRefKind modRef = getModRefKind(intrinsic);
646
Eli Bendersky2281ef92014-03-18 23:51:07 +0000647 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
648 intrinsic.isNoDuplicate) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000649 OS << " const Attribute::AttrKind Atts[] = {";
650 bool addComma = false;
651 if (!intrinsic.canThrow) {
652 OS << "Attribute::NoUnwind";
653 addComma = true;
654 }
655 if (intrinsic.isNoReturn) {
656 if (addComma)
657 OS << ",";
658 OS << "Attribute::NoReturn";
659 addComma = true;
660 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000661 if (intrinsic.isNoDuplicate) {
662 if (addComma)
663 OS << ",";
664 OS << "Attribute::NoDuplicate";
665 addComma = true;
666 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000667
John McCall375dcc92011-05-28 06:31:34 +0000668 switch (modRef) {
669 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000670 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000671 if (addComma)
672 OS << ",";
673 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000674 break;
675 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000676 if (addComma)
677 OS << ",";
678 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000679 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000680 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000681 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000682 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000683 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000684 }
John McCall375dcc92011-05-28 06:31:34 +0000685
686 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000687 OS << " NumAttrs = " << numAttrs << ";\n";
688 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000689 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000690 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000691 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000692 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000693 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000694 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000695
Craig Topper374f19c2012-04-13 06:14:57 +0000696 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000697 OS << " }\n";
Craig Toppere1d12942014-08-27 05:25:25 +0000698 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000699 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000700 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000701}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000702
Duncan Sands73247d22009-02-14 10:56:35 +0000703/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
704void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000705EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000706 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
707 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
708 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
709 << "\"Unknown intrinsic.\");\n\n";
710
711 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
712 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000713 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000714 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000715 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000716 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000717 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000718 break;
719 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000720 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000721 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000722 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000723 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000724 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000725 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000726 OS << "OnlyAccessesArgumentPointees,\n";
727 break;
728 case CodeGenIntrinsic::ReadWriteMem:
729 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000730 break;
731 }
732 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000733 OS << "};\n\n"
734 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
735 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000736}
737
Chris Lattner69ea0142008-01-04 04:38:35 +0000738/// EmitTargetBuiltins - All of the builtins in the specified map are for the
739/// same target, and we already checked it.
740static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000741 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000742 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000743
Chris Lattner497d13e2010-09-06 03:14:45 +0000744 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000745
Chris Lattner497d13e2010-09-06 03:14:45 +0000746 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
747 E = BIM.end(); I != E; ++I) {
748 std::string ResultCode =
749 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
750 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000751 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000752
753 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000754}
755
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000756
Chris Lattner402a5732006-03-15 01:33:26 +0000757void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000758EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000759 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000760 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000761 BIMTy BuiltinMap;
762 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
763 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000764 // Get the map for this target prefix.
765 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000766
Chris Lattner91678fc2008-01-02 21:24:22 +0000767 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
768 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000769 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
770 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000771 }
772 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000773
Chris Lattner402a5732006-03-15 01:33:26 +0000774 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
775 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
776 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
777 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
778 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000779
Dale Johannesenb842d522009-02-05 01:49:45 +0000780 if (TargetOnly) {
781 OS << "static " << TargetPrefix << "Intrinsic::ID "
782 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000783 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000784 } else {
785 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000786 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000787 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000788
Chris Lattner497d13e2010-09-06 03:14:45 +0000789 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
790 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000791
Chris Lattner402a5732006-03-15 01:33:26 +0000792 // Note: this could emit significantly better code if we cared.
793 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000794 OS << " ";
795 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000796 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000797 else
798 OS << "/* Target Independent Builtins */ ";
799 OS << "{\n";
800
Chris Lattner91678fc2008-01-02 21:24:22 +0000801 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000802 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000803 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000804 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000805 OS << " return ";
806 if (!TargetPrefix.empty())
807 OS << "(" << TargetPrefix << "Intrinsic::ID)";
808 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000809 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000810 OS << "#endif\n\n";
811}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000812
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000813void IntrinsicEmitter::
814EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
815 raw_ostream &OS) {
816 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
817
818 for (const auto &Intrinsic : Ints) {
819 if (Intrinsic.MSBuiltinName.empty())
820 continue;
821
822 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
823 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
824 Intrinsic.EnumName)).second)
825 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
826 "duplicate MS builtin name!");
827 }
828
829 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
830 "// This is used by the C front-end. The MS builtin name is passed\n"
831 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
832 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
833 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
834
835 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
836 << (TargetOnly ? "" : "Intrinsic::")
837 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
838 OS << " StringRef BuiltinName(BN);\n"
839 " StringRef TargetPrefix(TP);\n"
840 "\n";
841
842 for (const auto &Builtins : TargetBuiltins) {
843 OS << " ";
844 if (Builtins.first.empty())
845 OS << "/* Target Independent Builtins */ ";
846 else
847 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
848 OS << "{\n";
849 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
850 OS << "}";
851 }
852
853 OS << " return ";
854 if (!TargetPrefix.empty())
855 OS << "(" << TargetPrefix << "Intrinsic::ID)";
856 OS << "Intrinsic::not_intrinsic;\n";
857 OS << "}\n";
858
859 OS << "#endif\n\n";
860}
861
Richard Smith6bc9df32014-04-20 20:26:39 +0000862void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000863 IntrinsicEmitter(RK, TargetOnly).run(OS);
864}