blob: cf6934cb169e233ad80a2358c334835077ef8932 [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"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000017#include "llvm/ADT/StringExtras.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000018#include "llvm/TableGen/Error.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000019#include "llvm/TableGen/Record.h"
Douglas Gregor12c1cd32012-05-02 17:32:48 +000020#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/TableGen/TableGenBackend.h"
Jeff Cohenc4e24682006-03-15 02:51:05 +000022#include <algorithm>
Chris Lattnerc313d0b2006-03-03 02:32:46 +000023using namespace llvm;
24
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025namespace {
26class IntrinsicEmitter {
27 RecordKeeper &Records;
28 bool TargetOnly;
29 std::string TargetPrefix;
30
31public:
32 IntrinsicEmitter(RecordKeeper &R, bool T)
33 : Records(R), TargetOnly(T) {}
34
35 void run(raw_ostream &OS);
36
37 void EmitPrefix(raw_ostream &OS);
38
39 void EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
40 raw_ostream &OS);
41
42 void EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
43 raw_ostream &OS);
44 void EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
45 raw_ostream &OS);
46 void EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
47 raw_ostream &OS);
48 void EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
49 raw_ostream &OS);
50 void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
51 raw_ostream &OS);
52 void EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints,
53 raw_ostream &OS);
54 void EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints,
55 raw_ostream &OS);
56 void EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
57 raw_ostream &OS);
58 void EmitSuffix(raw_ostream &OS);
59};
60} // End anonymous namespace
61
Chris Lattnerc313d0b2006-03-03 02:32:46 +000062//===----------------------------------------------------------------------===//
Chris Lattnerc313d0b2006-03-03 02:32:46 +000063// IntrinsicEmitter Implementation
64//===----------------------------------------------------------------------===//
65
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000066void IntrinsicEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000067 emitSourceFileHeader("Intrinsic Function Source Fragment", OS);
68
Dale Johannesenb842d522009-02-05 01:49:45 +000069 std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records, TargetOnly);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000070
Dale Johannesenb842d522009-02-05 01:49:45 +000071 if (TargetOnly && !Ints.empty())
72 TargetPrefix = Ints[0].TargetPrefix;
Chris Lattnerc313d0b2006-03-03 02:32:46 +000073
Douglas Gregor6739a892010-05-11 06:17:44 +000074 EmitPrefix(OS);
75
Chris Lattnerc313d0b2006-03-03 02:32:46 +000076 // Emit the enum information.
77 EmitEnumInfo(Ints, OS);
Chris Lattnerda1a4cc2006-03-15 01:55:21 +000078
79 // Emit the intrinsic ID -> name table.
80 EmitIntrinsicToNameTable(Ints, OS);
Mon P Wangb4024932009-02-24 23:17:49 +000081
82 // Emit the intrinsic ID -> overload table.
83 EmitIntrinsicToOverloadTable(Ints, OS);
84
Chris Lattner6d8104e2006-03-09 20:34:19 +000085 // Emit the function name recognizer.
86 EmitFnNameRecognizer(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000087
Jim Laskey2682ea62007-02-07 20:38:26 +000088 // Emit the intrinsic declaration generator.
89 EmitGenerator(Ints, OS);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +000090
Duncan Sands38ef3a82007-12-03 20:06:50 +000091 // Emit the intrinsic parameter attributes.
92 EmitAttributes(Ints, OS);
Chris Lattnerfea17a92006-03-13 23:08:44 +000093
Duncan Sands73247d22009-02-14 10:56:35 +000094 // Emit intrinsic alias analysis mod/ref behavior.
95 EmitModRefBehavior(Ints, OS);
96
Chris Lattner402a5732006-03-15 01:33:26 +000097 // Emit code to translate GCC builtins into LLVM intrinsics.
98 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Douglas Gregor6739a892010-05-11 06:17:44 +000099
100 EmitSuffix(OS);
101}
102
103void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) {
104 OS << "// VisualStudio defines setjmp as _setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000105 "#if defined(_MSC_VER) && defined(setjmp) && \\\n"
106 " !defined(setjmp_undefined_for_msvc)\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000107 "# pragma push_macro(\"setjmp\")\n"
108 "# undef setjmp\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000109 "# define setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000110 "#endif\n\n";
111}
112
113void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) {
Michael J. Spencerded5f662010-09-24 19:48:47 +0000114 OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000115 "// let's return it to _setjmp state\n"
Michael J. Spencer511dce02010-09-14 04:27:38 +0000116 "# pragma pop_macro(\"setjmp\")\n"
Michael J. Spencerded5f662010-09-24 19:48:47 +0000117 "# undef setjmp_undefined_for_msvc\n"
Douglas Gregor6739a892010-05-11 06:17:44 +0000118 "#endif\n\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000119}
120
121void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000122 raw_ostream &OS) {
Chris Lattner6d8104e2006-03-09 20:34:19 +0000123 OS << "// Enum values for Intrinsics.h\n";
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000124 OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
125 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
126 OS << " " << Ints[i].EnumName;
127 OS << ((i != e-1) ? ", " : " ");
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000128 OS << std::string(40-Ints[i].EnumName.size(), ' ')
Chris Lattnerc313d0b2006-03-03 02:32:46 +0000129 << "// " << Ints[i].Name << "\n";
130 }
131 OS << "#endif\n\n";
132}
Chris Lattner6d8104e2006-03-09 20:34:19 +0000133
134void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000135EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000136 raw_ostream &OS) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000137 // Build a 'first character of function name' -> intrinsic # mapping.
138 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner6d8104e2006-03-09 20:34:19 +0000139 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattner07b332f2010-09-06 03:58:45 +0000140 IntMapping[Ints[i].Name[5]].push_back(i);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000141
Chris Lattner6d8104e2006-03-09 20:34:19 +0000142 OS << "// Function name -> enum value recognizer code.\n";
143 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattner07b332f2010-09-06 03:58:45 +0000144 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
145 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
146 OS << " default: break;\n";
147 // Emit the intrinsic matching stuff by first letter.
148 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner6d8104e2006-03-09 20:34:19 +0000149 E = IntMapping.end(); I != E; ++I) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000150 OS << " case '" << I->first << "':\n";
151 std::vector<unsigned> &IntList = I->second;
152
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000153 // Sort in reverse order of intrinsic name so "abc.def" appears after
154 // "abd.def.ghi" in the overridden name matcher
155 std::sort(IntList.begin(), IntList.end(), [&](unsigned i, unsigned j) {
156 return Ints[i].Name > Ints[j].Name;
157 });
Justin Holewinskib3d630c2013-07-25 12:32:00 +0000158
Chris Lattner07b332f2010-09-06 03:58:45 +0000159 // Emit all the overloaded intrinsics first, build a table of the
160 // non-overloaded ones.
161 std::vector<StringMatcher::StringPair> MatchTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000162
Chris Lattner07b332f2010-09-06 03:58:45 +0000163 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
164 unsigned IntNo = IntList[i];
165 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
166 Ints[IntNo].EnumName + ";";
167
168 if (!Ints[IntNo].isOverloaded) {
169 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
170 continue;
171 }
172
173 // For overloaded intrinsics, only the prefix needs to match
174 std::string TheStr = Ints[IntNo].Name.substr(6);
175 TheStr += '.'; // Require "bswap." instead of bswap.
176 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
177 << Result << '\n';
Chris Lattner6d8104e2006-03-09 20:34:19 +0000178 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000179
Chris Lattner07b332f2010-09-06 03:58:45 +0000180 // Emit the matcher logic for the fixed length strings.
181 StringMatcher("NameR", MatchTable, OS).Emit(1);
182 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner6d8104e2006-03-09 20:34:19 +0000183 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000184
Chris Lattner6d8104e2006-03-09 20:34:19 +0000185 OS << " }\n";
Chris Lattner6efe8632006-03-09 22:05:04 +0000186 OS << "#endif\n\n";
187}
188
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000189void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000190EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000191 raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000192 OS << "// Intrinsic ID to name table\n";
193 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
194 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000195 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
196 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000197 OS << "#endif\n\n";
198}
199
Mon P Wangb4024932009-02-24 23:17:49 +0000200void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000201EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000202 raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000203 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000204 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000205 OS << "static const uint8_t OTable[] = {\n";
206 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000207 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000208 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
209 if ((i+1)%8 == 0)
210 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000211 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000212 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000213 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000214 OS << "\n};\n\n";
215 // OTable contains a true bit at the position if the intrinsic is overloaded.
216 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000217 OS << "#endif\n\n";
218}
219
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000220
Chris Lattnerf39c2782012-05-27 18:28:35 +0000221// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000222enum IIT_Info {
Chris Lattner827b2532012-05-17 04:30:58 +0000223 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000224 IIT_Done = 0,
225 IIT_I1 = 1,
226 IIT_I8 = 2,
227 IIT_I16 = 3,
228 IIT_I32 = 4,
229 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000230 IIT_F16 = 6,
231 IIT_F32 = 7,
232 IIT_F64 = 8,
233 IIT_V2 = 9,
234 IIT_V4 = 10,
235 IIT_V8 = 11,
236 IIT_V16 = 12,
237 IIT_V32 = 13,
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000238 IIT_PTR = 14,
239 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000240
Chris Lattner827b2532012-05-17 04:30:58 +0000241 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000242 IIT_MMX = 16,
243 IIT_METADATA = 17,
244 IIT_EMPTYSTRUCT = 18,
245 IIT_STRUCT2 = 19,
246 IIT_STRUCT3 = 20,
247 IIT_STRUCT4 = 21,
248 IIT_STRUCT5 = 22,
249 IIT_EXTEND_VEC_ARG = 23,
250 IIT_TRUNC_VEC_ARG = 24,
Jiangning Liu63dc8402013-09-24 02:47:27 +0000251 IIT_ANYPTR = 25,
Andrew Tricka2efd992013-10-31 17:18:11 +0000252 IIT_V1 = 26,
253 IIT_VARARG = 27
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000254};
255
Chris Lattner827b2532012-05-17 04:30:58 +0000256
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000257static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000258 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000259 if (MVT(VT).isInteger()) {
260 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000261 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000262 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000263 case 1: return Sig.push_back(IIT_I1);
264 case 8: return Sig.push_back(IIT_I8);
265 case 16: return Sig.push_back(IIT_I16);
266 case 32: return Sig.push_back(IIT_I32);
267 case 64: return Sig.push_back(IIT_I64);
268 }
269 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000270
Chris Lattner827b2532012-05-17 04:30:58 +0000271 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000272 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000273 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000274 case MVT::f32: return Sig.push_back(IIT_F32);
275 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000276 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
277 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
278 // MVT::OtherVT is used to mean the empty struct type here.
279 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000280 // MVT::isVoid is used to represent varargs here.
281 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000282 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000283}
284
Francois Pichet9522bfc2012-05-17 04:00:03 +0000285#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000286#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000287#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000288
Chris Lattnerc4644162012-05-27 16:39:08 +0000289static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000290 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000291
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000292 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000293 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000294 assert(Number < ArgCodes.size() && "Invalid matching number!");
Chris Lattner827b2532012-05-17 04:30:58 +0000295 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000296 Sig.push_back(IIT_EXTEND_VEC_ARG);
297 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
298 Sig.push_back(IIT_TRUNC_VEC_ARG);
299 else
300 Sig.push_back(IIT_ARG);
Chris Lattnerc4644162012-05-27 16:39:08 +0000301 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000302 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000303
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000304 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000305
Chris Lattnerc4644162012-05-27 16:39:08 +0000306 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000307 switch (VT) {
308 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000309 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
310 case MVT::vAny: ++Tmp; // FALL THROUGH.
311 case MVT::fAny: ++Tmp; // FALL THROUGH.
312 case MVT::iAny: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000313 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000314 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000315 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000316
Chris Lattnerc4644162012-05-27 16:39:08 +0000317 // Figure out what arg # this is consuming, and remember what kind it was.
318 unsigned ArgNo = ArgCodes.size();
319 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000320
Chris Lattnerc4644162012-05-27 16:39:08 +0000321 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
322 return Sig.push_back((ArgNo << 2) | Tmp);
323 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000324
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000325 case MVT::iPTR: {
326 unsigned AddrSpace = 0;
327 if (R->isSubClassOf("LLVMQualPointerType")) {
328 AddrSpace = R->getValueAsInt("AddrSpace");
329 assert(AddrSpace < 256 && "Address space exceeds 255");
330 }
331 if (AddrSpace) {
332 Sig.push_back(IIT_ANYPTR);
333 Sig.push_back(AddrSpace);
334 } else {
335 Sig.push_back(IIT_PTR);
336 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000337 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000338 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000339 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000340
Craig Topper8561de92014-01-24 20:50:47 +0000341 if (MVT(VT).isVector()) {
342 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000343 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000344 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000345 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000346 case 2: Sig.push_back(IIT_V2); break;
347 case 4: Sig.push_back(IIT_V4); break;
348 case 8: Sig.push_back(IIT_V8); break;
349 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000350 case 32: Sig.push_back(IIT_V32); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000351 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000352
Craig Topper8561de92014-01-24 20:50:47 +0000353 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000354 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000355
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000356 EncodeFixedValueType(VT, Sig);
357}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000358
359#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000360#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000361#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000362
363/// ComputeFixedEncoding - If we can encode the type signature for this
364/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000365static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
366 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000367 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000368
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000369 if (Int.IS.RetVTs.empty())
370 TypeSig.push_back(IIT_Done);
371 else if (Int.IS.RetVTs.size() == 1 &&
372 Int.IS.RetVTs[0] == MVT::isVoid)
373 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000374 else {
375 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000376 case 1: break;
377 case 2: TypeSig.push_back(IIT_STRUCT2); break;
378 case 3: TypeSig.push_back(IIT_STRUCT3); break;
379 case 4: TypeSig.push_back(IIT_STRUCT4); break;
380 case 5: TypeSig.push_back(IIT_STRUCT5); break;
381 default: assert(0 && "Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000382 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000383
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000384 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000385 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000386 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000387
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000388 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000389 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000390}
391
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000392static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000393 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000394}
395
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000396void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000397 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000398 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
399 // capture it in this vector, otherwise store a ~0U.
400 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000401
Chris Lattnera3b0f522012-05-17 15:55:41 +0000402 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000403
Chris Lattnera3b0f522012-05-17 15:55:41 +0000404 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000405
Jim Laskey2682ea62007-02-07 20:38:26 +0000406 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000407 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000408 // Get the signature for the intrinsic.
409 TypeSig.clear();
410 ComputeFixedEncoding(Ints[i], TypeSig);
411
412 // Check to see if we can encode it into a 32-bit word. We can only encode
413 // 8 nibbles into a 32-bit word.
414 if (TypeSig.size() <= 8) {
415 bool Failed = false;
416 unsigned Result = 0;
417 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
418 // If we had an unencodable argument, bail out.
419 if (TypeSig[i] > 15) {
420 Failed = true;
421 break;
422 }
423 Result = (Result << 4) | TypeSig[e-i-1];
424 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000425
Chris Lattnera3b0f522012-05-17 15:55:41 +0000426 // If this could be encoded into a 31-bit word, return it.
427 if (!Failed && (Result >> 31) == 0) {
428 FixedEncodings.push_back(Result);
429 continue;
430 }
431 }
432
433 // Otherwise, we're going to unique the sequence into the
434 // LongEncodingTable, and use its offset in the 32-bit table instead.
435 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000436
Chris Lattnera3b0f522012-05-17 15:55:41 +0000437 // This is a placehold that we'll replace after the table is laid out.
438 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000439 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000440
Chris Lattnera3b0f522012-05-17 15:55:41 +0000441 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000442
Chris Lattnerf39c2782012-05-27 18:28:35 +0000443 OS << "// Global intrinsic function declaration type table.\n";
444 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
445
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000446 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000447
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000448 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
449 if ((i & 7) == 7)
450 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000451
Chris Lattnera3b0f522012-05-17 15:55:41 +0000452 // If the entry fit in the table, just emit it.
453 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000454 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000455 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000456 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000457
Chris Lattnera3b0f522012-05-17 15:55:41 +0000458 TypeSig.clear();
459 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000460
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000461
Chris Lattnera3b0f522012-05-17 15:55:41 +0000462 // Otherwise, emit the offset into the long encoding table. We emit it this
463 // way so that it is easier to read the offset in the .def file.
464 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000465 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000466
Chris Lattnera3b0f522012-05-17 15:55:41 +0000467 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000468
Chris Lattnera3b0f522012-05-17 15:55:41 +0000469 // Emit the shared table of register lists.
470 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
471 if (!LongEncodingTable.empty())
472 LongEncodingTable.emit(OS, printIITEntry);
473 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000474
Patrik Hägglundca210d82012-05-23 12:34:56 +0000475 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000476}
477
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000478enum ModRefKind {
479 MRK_none,
480 MRK_readonly,
481 MRK_readnone
482};
John McCall375dcc92011-05-28 06:31:34 +0000483
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000484static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
485 switch (intrinsic.ModRef) {
486 case CodeGenIntrinsic::NoMem:
487 return MRK_readnone;
488 case CodeGenIntrinsic::ReadArgMem:
489 case CodeGenIntrinsic::ReadMem:
490 return MRK_readonly;
491 case CodeGenIntrinsic::ReadWriteArgMem:
492 case CodeGenIntrinsic::ReadWriteMem:
493 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000494 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000495 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000496}
497
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000498namespace {
499struct AttributeComparator {
500 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
501 // Sort throwing intrinsics after non-throwing intrinsics.
502 if (L->canThrow != R->canThrow)
503 return R->canThrow;
504
505 if (L->isNoReturn != R->isNoReturn)
506 return R->isNoReturn;
507
508 // Try to order by readonly/readnone attribute.
509 ModRefKind LK = getModRefKind(*L);
510 ModRefKind RK = getModRefKind(*R);
511 if (LK != RK) return (LK > RK);
512
513 // Order by argument attributes.
514 // This is reliable because each side is already sorted internally.
515 return (L->ArgumentAttributes < R->ArgumentAttributes);
516 }
517};
518} // End anonymous namespace
519
Chris Lattner49b7ee12009-01-12 01:18:58 +0000520/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000521void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000522EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000523 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
524 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000525 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000526 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000527 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000528 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000529 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000530
Craig Topperccd651c2012-02-28 06:32:00 +0000531 // Compute the maximum number of attribute arguments and the map
532 typedef std::map<const CodeGenIntrinsic*, unsigned,
533 AttributeComparator> UniqAttrMapTy;
534 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000535 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000536 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000537 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000538 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000539 maxArgAttrs =
540 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000541 unsigned &N = UniqAttributes[&intrinsic];
542 if (N) continue;
543 assert(AttrNum < 256 && "Too many unique attributes for table!");
544 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000545 }
John McCall375dcc92011-05-28 06:31:34 +0000546
Bill Wendling4d3491c2013-01-27 03:25:05 +0000547 // Emit an array of AttributeSet. Most intrinsics will have at least one
548 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000549 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000550
551 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
552 const CodeGenIntrinsic &intrinsic = Ints[i];
553
554 OS << " " << UniqAttributes[&intrinsic] << ", // "
555 << intrinsic.Name << "\n";
556 }
557 OS << " };\n\n";
558
Bill Wendling4d3491c2013-01-27 03:25:05 +0000559 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000560 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000561 OS << " if (id != 0) {\n";
562 OS << " switch(IntrinsicsToAttributesMap[id - ";
563 if (TargetOnly)
564 OS << "Intrinsic::num_intrinsics";
565 else
566 OS << "1";
567 OS << "]) {\n";
568 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000569 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
570 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000571 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000572
Craig Topperccd651c2012-02-28 06:32:00 +0000573 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000574
575 // Keep track of the number of attributes we're writing out.
576 unsigned numAttrs = 0;
577
578 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000579 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
580 if (ae) {
581 while (ai != ae) {
582 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000583
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000584 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
585 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000586
Bill Wendlinged42e792012-10-10 06:13:42 +0000587 do {
588 switch (intrinsic.ArgumentAttributes[ai].second) {
589 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000590 if (addComma)
591 OS << ",";
592 OS << "Attribute::NoCapture";
593 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000594 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000595 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000596 if (addComma)
597 OS << ",";
598 OS << "Attribute::ReadOnly";
599 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000600 break;
601 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000602 if (addComma)
603 OS << ",";
604 OS << "Attributes::ReadNone";
605 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000606 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000607 }
John McCall375dcc92011-05-28 06:31:34 +0000608
Bill Wendlinged42e792012-10-10 06:13:42 +0000609 ++ai;
610 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000611 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000612 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000613 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000614 }
John McCall375dcc92011-05-28 06:31:34 +0000615 }
616
617 ModRefKind modRef = getModRefKind(intrinsic);
618
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000619 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000620 OS << " const Attribute::AttrKind Atts[] = {";
621 bool addComma = false;
622 if (!intrinsic.canThrow) {
623 OS << "Attribute::NoUnwind";
624 addComma = true;
625 }
626 if (intrinsic.isNoReturn) {
627 if (addComma)
628 OS << ",";
629 OS << "Attribute::NoReturn";
630 addComma = true;
631 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000632
John McCall375dcc92011-05-28 06:31:34 +0000633 switch (modRef) {
634 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000635 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000636 if (addComma)
637 OS << ",";
638 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000639 break;
640 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000641 if (addComma)
642 OS << ",";
643 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000644 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000645 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000646 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000647 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000648 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000649 }
John McCall375dcc92011-05-28 06:31:34 +0000650
651 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000652 OS << " NumAttrs = " << numAttrs << ";\n";
653 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000654 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000655 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000656 OS << " return AttributeSet();\n";
Filip Pizloc95bd8d2014-02-20 23:57:31 +0000657 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000658 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000659 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000660
Craig Topper374f19c2012-04-13 06:14:57 +0000661 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000662 OS << " }\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000663 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
Chris Lattner3cb6f832012-05-28 01:47:44 +0000664 "NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000665 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000666 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000667}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000668
Duncan Sands73247d22009-02-14 10:56:35 +0000669/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
670void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000671EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000672 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
673 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
674 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
675 << "\"Unknown intrinsic.\");\n\n";
676
677 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
678 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000679 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000680 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000681 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000682 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000683 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000684 break;
685 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000686 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000687 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000688 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000689 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000690 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000691 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000692 OS << "OnlyAccessesArgumentPointees,\n";
693 break;
694 case CodeGenIntrinsic::ReadWriteMem:
695 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000696 break;
697 }
698 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000699 OS << "};\n\n"
700 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
701 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000702}
703
Chris Lattner69ea0142008-01-04 04:38:35 +0000704/// EmitTargetBuiltins - All of the builtins in the specified map are for the
705/// same target, and we already checked it.
706static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000707 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000708 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000709
Chris Lattner497d13e2010-09-06 03:14:45 +0000710 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000711
Chris Lattner497d13e2010-09-06 03:14:45 +0000712 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
713 E = BIM.end(); I != E; ++I) {
714 std::string ResultCode =
715 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
716 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000717 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000718
719 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000720}
721
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000722
Chris Lattner402a5732006-03-15 01:33:26 +0000723void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000724EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000725 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000726 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000727 BIMTy BuiltinMap;
728 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
729 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000730 // Get the map for this target prefix.
731 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000732
Chris Lattner91678fc2008-01-02 21:24:22 +0000733 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
734 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000735 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
736 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000737 }
738 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000739
Chris Lattner402a5732006-03-15 01:33:26 +0000740 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
741 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
742 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
743 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
744 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000745
Dale Johannesenb842d522009-02-05 01:49:45 +0000746 if (TargetOnly) {
747 OS << "static " << TargetPrefix << "Intrinsic::ID "
748 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000749 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000750 } else {
751 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000752 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000753 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000754
Chris Lattner497d13e2010-09-06 03:14:45 +0000755 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
756 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000757
Chris Lattner402a5732006-03-15 01:33:26 +0000758 // Note: this could emit significantly better code if we cared.
759 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000760 OS << " ";
761 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000762 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000763 else
764 OS << "/* Target Independent Builtins */ ";
765 OS << "{\n";
766
Chris Lattner91678fc2008-01-02 21:24:22 +0000767 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000768 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000769 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000770 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000771 OS << " return ";
772 if (!TargetPrefix.empty())
773 OS << "(" << TargetPrefix << "Intrinsic::ID)";
774 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000775 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000776 OS << "#endif\n\n";
777}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000778
779namespace llvm {
780
781void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
782 IntrinsicEmitter(RK, TargetOnly).run(OS);
783}
784
785} // End llvm namespace