blob: 37f6de057dadc80f01e333c3605e7552be097442 [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,
260 IIT_HALF_VEC_ARG = 29
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000261};
262
Chris Lattner827b2532012-05-17 04:30:58 +0000263
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000264static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000265 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000266 if (MVT(VT).isInteger()) {
267 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000268 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000269 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000270 case 1: return Sig.push_back(IIT_I1);
271 case 8: return Sig.push_back(IIT_I8);
272 case 16: return Sig.push_back(IIT_I16);
273 case 32: return Sig.push_back(IIT_I32);
274 case 64: return Sig.push_back(IIT_I64);
275 }
276 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000277
Chris Lattner827b2532012-05-17 04:30:58 +0000278 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000279 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000280 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000281 case MVT::f32: return Sig.push_back(IIT_F32);
282 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000283 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
284 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
285 // MVT::OtherVT is used to mean the empty struct type here.
286 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000287 // MVT::isVoid is used to represent varargs here.
288 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000289 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000290}
291
Francois Pichet9522bfc2012-05-17 04:00:03 +0000292#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000293#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000294#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000295
Chris Lattnerc4644162012-05-27 16:39:08 +0000296static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000297 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000298
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000299 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000300 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000301 assert(Number < ArgCodes.size() && "Invalid matching number!");
Tim Northoveraa3cf1e2014-03-28 12:31:39 +0000302 if (R->isSubClassOf("LLVMExtendedType"))
303 Sig.push_back(IIT_EXTEND_ARG);
304 else if (R->isSubClassOf("LLVMTruncatedType"))
305 Sig.push_back(IIT_TRUNC_ARG);
Tim Northover4516de32014-03-29 07:04:54 +0000306 else if (R->isSubClassOf("LLVMHalfElementsVectorType"))
307 Sig.push_back(IIT_HALF_VEC_ARG);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000308 else
309 Sig.push_back(IIT_ARG);
Chris Lattnerc4644162012-05-27 16:39:08 +0000310 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000311 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000312
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000313 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000314
Chris Lattnerc4644162012-05-27 16:39:08 +0000315 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000316 switch (VT) {
317 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000318 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
319 case MVT::vAny: ++Tmp; // FALL THROUGH.
320 case MVT::fAny: ++Tmp; // FALL THROUGH.
321 case MVT::iAny: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000322 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000323 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000324 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000325
Chris Lattnerc4644162012-05-27 16:39:08 +0000326 // Figure out what arg # this is consuming, and remember what kind it was.
327 unsigned ArgNo = ArgCodes.size();
328 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000329
Chris Lattnerc4644162012-05-27 16:39:08 +0000330 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
331 return Sig.push_back((ArgNo << 2) | Tmp);
332 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000333
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000334 case MVT::iPTR: {
335 unsigned AddrSpace = 0;
336 if (R->isSubClassOf("LLVMQualPointerType")) {
337 AddrSpace = R->getValueAsInt("AddrSpace");
338 assert(AddrSpace < 256 && "Address space exceeds 255");
339 }
340 if (AddrSpace) {
341 Sig.push_back(IIT_ANYPTR);
342 Sig.push_back(AddrSpace);
343 } else {
344 Sig.push_back(IIT_PTR);
345 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000346 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000347 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000348 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000349
Craig Topper8561de92014-01-24 20:50:47 +0000350 if (MVT(VT).isVector()) {
351 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000352 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000353 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000354 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000355 case 2: Sig.push_back(IIT_V2); break;
356 case 4: Sig.push_back(IIT_V4); break;
357 case 8: Sig.push_back(IIT_V8); break;
358 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000359 case 32: Sig.push_back(IIT_V32); break;
Robert Khasanovb25e5622014-09-30 11:32:22 +0000360 case 64: Sig.push_back(IIT_V64); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000361 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000362
Craig Topper8561de92014-01-24 20:50:47 +0000363 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000364 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000365
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000366 EncodeFixedValueType(VT, Sig);
367}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000368
369#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000370#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000371#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000372
373/// ComputeFixedEncoding - If we can encode the type signature for this
374/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000375static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
376 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000377 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000378
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000379 if (Int.IS.RetVTs.empty())
380 TypeSig.push_back(IIT_Done);
381 else if (Int.IS.RetVTs.size() == 1 &&
382 Int.IS.RetVTs[0] == MVT::isVoid)
383 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000384 else {
385 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000386 case 1: break;
387 case 2: TypeSig.push_back(IIT_STRUCT2); break;
388 case 3: TypeSig.push_back(IIT_STRUCT3); break;
389 case 4: TypeSig.push_back(IIT_STRUCT4); break;
390 case 5: TypeSig.push_back(IIT_STRUCT5); break;
Craig Topper2a30d782014-06-18 05:05:13 +0000391 default: llvm_unreachable("Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000392 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000393
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000394 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000395 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000396 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000397
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000398 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000399 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000400}
401
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000402static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000403 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000404}
405
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000406void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000407 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000408 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
409 // capture it in this vector, otherwise store a ~0U.
410 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000411
Chris Lattnera3b0f522012-05-17 15:55:41 +0000412 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000413
Chris Lattnera3b0f522012-05-17 15:55:41 +0000414 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000415
Jim Laskey2682ea62007-02-07 20:38:26 +0000416 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000417 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000418 // Get the signature for the intrinsic.
419 TypeSig.clear();
420 ComputeFixedEncoding(Ints[i], TypeSig);
421
422 // Check to see if we can encode it into a 32-bit word. We can only encode
423 // 8 nibbles into a 32-bit word.
424 if (TypeSig.size() <= 8) {
425 bool Failed = false;
426 unsigned Result = 0;
427 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
428 // If we had an unencodable argument, bail out.
429 if (TypeSig[i] > 15) {
430 Failed = true;
431 break;
432 }
433 Result = (Result << 4) | TypeSig[e-i-1];
434 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000435
Chris Lattnera3b0f522012-05-17 15:55:41 +0000436 // If this could be encoded into a 31-bit word, return it.
437 if (!Failed && (Result >> 31) == 0) {
438 FixedEncodings.push_back(Result);
439 continue;
440 }
441 }
442
443 // Otherwise, we're going to unique the sequence into the
444 // LongEncodingTable, and use its offset in the 32-bit table instead.
445 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000446
Chris Lattnera3b0f522012-05-17 15:55:41 +0000447 // This is a placehold that we'll replace after the table is laid out.
448 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000449 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000450
Chris Lattnera3b0f522012-05-17 15:55:41 +0000451 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000452
Chris Lattnerf39c2782012-05-27 18:28:35 +0000453 OS << "// Global intrinsic function declaration type table.\n";
454 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
455
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000456 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000457
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000458 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
459 if ((i & 7) == 7)
460 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000461
Chris Lattnera3b0f522012-05-17 15:55:41 +0000462 // If the entry fit in the table, just emit it.
463 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000464 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000465 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000466 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000467
Chris Lattnera3b0f522012-05-17 15:55:41 +0000468 TypeSig.clear();
469 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000470
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000471
Chris Lattnera3b0f522012-05-17 15:55:41 +0000472 // Otherwise, emit the offset into the long encoding table. We emit it this
473 // way so that it is easier to read the offset in the .def file.
474 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000475 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000476
Chris Lattnera3b0f522012-05-17 15:55:41 +0000477 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000478
Chris Lattnera3b0f522012-05-17 15:55:41 +0000479 // Emit the shared table of register lists.
480 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
481 if (!LongEncodingTable.empty())
482 LongEncodingTable.emit(OS, printIITEntry);
483 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000484
Patrik Hägglundca210d82012-05-23 12:34:56 +0000485 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000486}
487
Richard Smith6bc9df32014-04-20 20:26:39 +0000488namespace {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000489enum ModRefKind {
490 MRK_none,
491 MRK_readonly,
492 MRK_readnone
493};
Richard Smith6bc9df32014-04-20 20:26:39 +0000494}
John McCall375dcc92011-05-28 06:31:34 +0000495
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000496static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
497 switch (intrinsic.ModRef) {
498 case CodeGenIntrinsic::NoMem:
499 return MRK_readnone;
500 case CodeGenIntrinsic::ReadArgMem:
501 case CodeGenIntrinsic::ReadMem:
502 return MRK_readonly;
503 case CodeGenIntrinsic::ReadWriteArgMem:
504 case CodeGenIntrinsic::ReadWriteMem:
505 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000506 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000507 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000508}
509
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000510namespace {
511struct AttributeComparator {
512 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
513 // Sort throwing intrinsics after non-throwing intrinsics.
514 if (L->canThrow != R->canThrow)
515 return R->canThrow;
516
Eli Bendersky2281ef92014-03-18 23:51:07 +0000517 if (L->isNoDuplicate != R->isNoDuplicate)
518 return R->isNoDuplicate;
519
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000520 if (L->isNoReturn != R->isNoReturn)
521 return R->isNoReturn;
522
523 // Try to order by readonly/readnone attribute.
524 ModRefKind LK = getModRefKind(*L);
525 ModRefKind RK = getModRefKind(*R);
526 if (LK != RK) return (LK > RK);
527
528 // Order by argument attributes.
529 // This is reliable because each side is already sorted internally.
530 return (L->ArgumentAttributes < R->ArgumentAttributes);
531 }
532};
533} // End anonymous namespace
534
Chris Lattner49b7ee12009-01-12 01:18:58 +0000535/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000536void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000537EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000538 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
539 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000540 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000541 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000542 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000543 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000544 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000545
Craig Topperccd651c2012-02-28 06:32:00 +0000546 // Compute the maximum number of attribute arguments and the map
547 typedef std::map<const CodeGenIntrinsic*, unsigned,
548 AttributeComparator> UniqAttrMapTy;
549 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000550 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000551 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000552 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000553 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000554 maxArgAttrs =
555 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000556 unsigned &N = UniqAttributes[&intrinsic];
557 if (N) continue;
558 assert(AttrNum < 256 && "Too many unique attributes for table!");
559 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000560 }
John McCall375dcc92011-05-28 06:31:34 +0000561
Bill Wendling4d3491c2013-01-27 03:25:05 +0000562 // Emit an array of AttributeSet. Most intrinsics will have at least one
563 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000564 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000565
566 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
567 const CodeGenIntrinsic &intrinsic = Ints[i];
568
569 OS << " " << UniqAttributes[&intrinsic] << ", // "
570 << intrinsic.Name << "\n";
571 }
572 OS << " };\n\n";
573
Bill Wendling4d3491c2013-01-27 03:25:05 +0000574 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000575 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000576 OS << " if (id != 0) {\n";
577 OS << " switch(IntrinsicsToAttributesMap[id - ";
578 if (TargetOnly)
579 OS << "Intrinsic::num_intrinsics";
580 else
581 OS << "1";
582 OS << "]) {\n";
583 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000584 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
585 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000586 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000587
Craig Topperccd651c2012-02-28 06:32:00 +0000588 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000589
590 // Keep track of the number of attributes we're writing out.
591 unsigned numAttrs = 0;
592
593 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000594 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
595 if (ae) {
596 while (ai != ae) {
597 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000598
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000599 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
600 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000601
Bill Wendlinged42e792012-10-10 06:13:42 +0000602 do {
603 switch (intrinsic.ArgumentAttributes[ai].second) {
604 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000605 if (addComma)
606 OS << ",";
607 OS << "Attribute::NoCapture";
608 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000609 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000610 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000611 if (addComma)
612 OS << ",";
613 OS << "Attribute::ReadOnly";
614 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000615 break;
616 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000617 if (addComma)
618 OS << ",";
619 OS << "Attributes::ReadNone";
620 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000621 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000622 }
John McCall375dcc92011-05-28 06:31:34 +0000623
Bill Wendlinged42e792012-10-10 06:13:42 +0000624 ++ai;
625 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000626 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000627 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000628 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000629 }
John McCall375dcc92011-05-28 06:31:34 +0000630 }
631
632 ModRefKind modRef = getModRefKind(intrinsic);
633
Eli Bendersky2281ef92014-03-18 23:51:07 +0000634 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn ||
635 intrinsic.isNoDuplicate) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000636 OS << " const Attribute::AttrKind Atts[] = {";
637 bool addComma = false;
638 if (!intrinsic.canThrow) {
639 OS << "Attribute::NoUnwind";
640 addComma = true;
641 }
642 if (intrinsic.isNoReturn) {
643 if (addComma)
644 OS << ",";
645 OS << "Attribute::NoReturn";
646 addComma = true;
647 }
Eli Bendersky2281ef92014-03-18 23:51:07 +0000648 if (intrinsic.isNoDuplicate) {
649 if (addComma)
650 OS << ",";
651 OS << "Attribute::NoDuplicate";
652 addComma = true;
653 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000654
John McCall375dcc92011-05-28 06:31:34 +0000655 switch (modRef) {
656 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000657 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000658 if (addComma)
659 OS << ",";
660 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000661 break;
662 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000663 if (addComma)
664 OS << ",";
665 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000666 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000667 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000668 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000669 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000670 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000671 }
John McCall375dcc92011-05-28 06:31:34 +0000672
673 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000674 OS << " NumAttrs = " << numAttrs << ";\n";
675 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000676 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000677 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000678 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000679 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000680 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000681 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000682
Craig Topper374f19c2012-04-13 06:14:57 +0000683 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000684 OS << " }\n";
Craig Toppere1d12942014-08-27 05:25:25 +0000685 OS << " return AttributeSet::get(C, makeArrayRef(AS, NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000686 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000687 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000688}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000689
Duncan Sands73247d22009-02-14 10:56:35 +0000690/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
691void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000692EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000693 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
694 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
695 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
696 << "\"Unknown intrinsic.\");\n\n";
697
698 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
699 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000700 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000701 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000702 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000703 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000704 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000705 break;
706 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000707 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000708 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000709 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000710 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000711 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000712 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000713 OS << "OnlyAccessesArgumentPointees,\n";
714 break;
715 case CodeGenIntrinsic::ReadWriteMem:
716 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000717 break;
718 }
719 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000720 OS << "};\n\n"
721 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
722 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000723}
724
Chris Lattner69ea0142008-01-04 04:38:35 +0000725/// EmitTargetBuiltins - All of the builtins in the specified map are for the
726/// same target, and we already checked it.
727static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000728 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000729 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000730
Chris Lattner497d13e2010-09-06 03:14:45 +0000731 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000732
Chris Lattner497d13e2010-09-06 03:14:45 +0000733 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
734 E = BIM.end(); I != E; ++I) {
735 std::string ResultCode =
736 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
737 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000738 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000739
740 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000741}
742
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000743
Chris Lattner402a5732006-03-15 01:33:26 +0000744void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000745EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000746 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000747 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000748 BIMTy BuiltinMap;
749 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
750 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000751 // Get the map for this target prefix.
752 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000753
Chris Lattner91678fc2008-01-02 21:24:22 +0000754 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
755 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000756 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
757 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000758 }
759 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000760
Chris Lattner402a5732006-03-15 01:33:26 +0000761 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
762 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
763 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
764 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
765 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000766
Dale Johannesenb842d522009-02-05 01:49:45 +0000767 if (TargetOnly) {
768 OS << "static " << TargetPrefix << "Intrinsic::ID "
769 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000770 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000771 } else {
772 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000773 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000774 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000775
Chris Lattner497d13e2010-09-06 03:14:45 +0000776 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
777 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000778
Chris Lattner402a5732006-03-15 01:33:26 +0000779 // Note: this could emit significantly better code if we cared.
780 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000781 OS << " ";
782 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000783 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000784 else
785 OS << "/* Target Independent Builtins */ ";
786 OS << "{\n";
787
Chris Lattner91678fc2008-01-02 21:24:22 +0000788 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000789 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000790 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000791 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000792 OS << " return ";
793 if (!TargetPrefix.empty())
794 OS << "(" << TargetPrefix << "Intrinsic::ID)";
795 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000796 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000797 OS << "#endif\n\n";
798}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000799
Saleem Abdulrasool4e63fc42014-07-04 18:42:25 +0000800void IntrinsicEmitter::
801EmitIntrinsicToMSBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
802 raw_ostream &OS) {
803 std::map<std::string, std::map<std::string, std::string>> TargetBuiltins;
804
805 for (const auto &Intrinsic : Ints) {
806 if (Intrinsic.MSBuiltinName.empty())
807 continue;
808
809 auto &Builtins = TargetBuiltins[Intrinsic.TargetPrefix];
810 if (!Builtins.insert(std::make_pair(Intrinsic.MSBuiltinName,
811 Intrinsic.EnumName)).second)
812 PrintFatalError("Intrinsic '" + Intrinsic.TheDef->getName() + "': "
813 "duplicate MS builtin name!");
814 }
815
816 OS << "// Get the LLVM intrinsic that corresponds to a MS builtin.\n"
817 "// This is used by the C front-end. The MS builtin name is passed\n"
818 "// in as a BuiltinName, and a target prefix (e.g. 'arm') is passed\n"
819 "// in as a TargetPrefix. The result is assigned to 'IntrinsicID'.\n"
820 "#ifdef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN\n";
821
822 OS << (TargetOnly ? "static " + TargetPrefix : "") << "Intrinsic::ID "
823 << (TargetOnly ? "" : "Intrinsic::")
824 << "getIntrinsicForMSBuiltin(const char *TP, const char *BN) {\n";
825 OS << " StringRef BuiltinName(BN);\n"
826 " StringRef TargetPrefix(TP);\n"
827 "\n";
828
829 for (const auto &Builtins : TargetBuiltins) {
830 OS << " ";
831 if (Builtins.first.empty())
832 OS << "/* Target Independent Builtins */ ";
833 else
834 OS << "if (TargetPrefix == \"" << Builtins.first << "\") ";
835 OS << "{\n";
836 EmitTargetBuiltins(Builtins.second, TargetPrefix, OS);
837 OS << "}";
838 }
839
840 OS << " return ";
841 if (!TargetPrefix.empty())
842 OS << "(" << TargetPrefix << "Intrinsic::ID)";
843 OS << "Intrinsic::not_intrinsic;\n";
844 OS << "}\n";
845
846 OS << "#endif\n\n";
847}
848
Richard Smith6bc9df32014-04-20 20:26:39 +0000849void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000850 IntrinsicEmitter(RK, TargetOnly).run(OS);
851}