blob: 173e50653274154483a2be8299d617ad89cbb11d [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
Justin Holewinskib3d630c2013-07-25 12:32:00 +0000134struct IntrinsicNameSorter {
135 IntrinsicNameSorter(const std::vector<CodeGenIntrinsic> &I)
136 : Ints(I) {}
137
138 // Sort in reverse order of intrinsic name so "abc.def" appears after
139 // "abd.def.ghi" in the overridden name matcher
140 bool operator()(unsigned i, unsigned j) {
141 return Ints[i].Name > Ints[j].Name;
142 }
143
144private:
145 const std::vector<CodeGenIntrinsic> &Ints;
146};
147
Chris Lattner6d8104e2006-03-09 20:34:19 +0000148void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000149EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000150 raw_ostream &OS) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000151 // Build a 'first character of function name' -> intrinsic # mapping.
152 std::map<char, std::vector<unsigned> > IntMapping;
Chris Lattner6d8104e2006-03-09 20:34:19 +0000153 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Chris Lattner07b332f2010-09-06 03:58:45 +0000154 IntMapping[Ints[i].Name[5]].push_back(i);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000155
Chris Lattner6d8104e2006-03-09 20:34:19 +0000156 OS << "// Function name -> enum value recognizer code.\n";
157 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
Chris Lattner07b332f2010-09-06 03:58:45 +0000158 OS << " StringRef NameR(Name+6, Len-6); // Skip over 'llvm.'\n";
159 OS << " switch (Name[5]) { // Dispatch on first letter.\n";
160 OS << " default: break;\n";
Justin Holewinskib3d630c2013-07-25 12:32:00 +0000161 IntrinsicNameSorter Sorter(Ints);
Chris Lattner07b332f2010-09-06 03:58:45 +0000162 // Emit the intrinsic matching stuff by first letter.
163 for (std::map<char, std::vector<unsigned> >::iterator I = IntMapping.begin(),
Chris Lattner6d8104e2006-03-09 20:34:19 +0000164 E = IntMapping.end(); I != E; ++I) {
Chris Lattner07b332f2010-09-06 03:58:45 +0000165 OS << " case '" << I->first << "':\n";
166 std::vector<unsigned> &IntList = I->second;
167
Justin Holewinskib3d630c2013-07-25 12:32:00 +0000168 // Sort intrinsics in reverse order of their names
169 std::sort(IntList.begin(), IntList.end(), Sorter);
170
Chris Lattner07b332f2010-09-06 03:58:45 +0000171 // Emit all the overloaded intrinsics first, build a table of the
172 // non-overloaded ones.
173 std::vector<StringMatcher::StringPair> MatchTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000174
Chris Lattner07b332f2010-09-06 03:58:45 +0000175 for (unsigned i = 0, e = IntList.size(); i != e; ++i) {
176 unsigned IntNo = IntList[i];
177 std::string Result = "return " + TargetPrefix + "Intrinsic::" +
178 Ints[IntNo].EnumName + ";";
179
180 if (!Ints[IntNo].isOverloaded) {
181 MatchTable.push_back(std::make_pair(Ints[IntNo].Name.substr(6),Result));
182 continue;
183 }
184
185 // For overloaded intrinsics, only the prefix needs to match
186 std::string TheStr = Ints[IntNo].Name.substr(6);
187 TheStr += '.'; // Require "bswap." instead of bswap.
188 OS << " if (NameR.startswith(\"" << TheStr << "\")) "
189 << Result << '\n';
Chris Lattner6d8104e2006-03-09 20:34:19 +0000190 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000191
Chris Lattner07b332f2010-09-06 03:58:45 +0000192 // Emit the matcher logic for the fixed length strings.
193 StringMatcher("NameR", MatchTable, OS).Emit(1);
194 OS << " break; // end of '" << I->first << "' case.\n";
Chris Lattner6d8104e2006-03-09 20:34:19 +0000195 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000196
Chris Lattner6d8104e2006-03-09 20:34:19 +0000197 OS << " }\n";
Chris Lattner6efe8632006-03-09 22:05:04 +0000198 OS << "#endif\n\n";
199}
200
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000201void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000202EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000203 raw_ostream &OS) {
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000204 OS << "// Intrinsic ID to name table\n";
205 OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
206 OS << " // Note that entry #0 is the invalid intrinsic!\n";
Evan Chengc2c8b582006-03-28 22:25:56 +0000207 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
208 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerda1a4cc2006-03-15 01:55:21 +0000209 OS << "#endif\n\n";
210}
211
Mon P Wangb4024932009-02-24 23:17:49 +0000212void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000213EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000214 raw_ostream &OS) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000215 OS << "// Intrinsic ID to overload bitset\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000216 OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
Benjamin Krameracd78d52012-03-01 02:16:57 +0000217 OS << "static const uint8_t OTable[] = {\n";
218 OS << " 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000219 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameracd78d52012-03-01 02:16:57 +0000220 // Add one to the index so we emit a null bit for the invalid #0 intrinsic.
221 if ((i+1)%8 == 0)
222 OS << ",\n 0";
Mon P Wangb4024932009-02-24 23:17:49 +0000223 if (Ints[i].isOverloaded)
Benjamin Krameracd78d52012-03-01 02:16:57 +0000224 OS << " | (1<<" << (i+1)%8 << ')';
Mon P Wangb4024932009-02-24 23:17:49 +0000225 }
Benjamin Krameracd78d52012-03-01 02:16:57 +0000226 OS << "\n};\n\n";
227 // OTable contains a true bit at the position if the intrinsic is overloaded.
228 OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n";
Mon P Wangb4024932009-02-24 23:17:49 +0000229 OS << "#endif\n\n";
230}
231
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000232
Chris Lattnerf39c2782012-05-27 18:28:35 +0000233// NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp!
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000234enum IIT_Info {
Chris Lattner827b2532012-05-17 04:30:58 +0000235 // Common values should be encoded with 0-15.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000236 IIT_Done = 0,
237 IIT_I1 = 1,
238 IIT_I8 = 2,
239 IIT_I16 = 3,
240 IIT_I32 = 4,
241 IIT_I64 = 5,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000242 IIT_F16 = 6,
243 IIT_F32 = 7,
244 IIT_F64 = 8,
245 IIT_V2 = 9,
246 IIT_V4 = 10,
247 IIT_V8 = 11,
248 IIT_V16 = 12,
249 IIT_V32 = 13,
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000250 IIT_PTR = 14,
251 IIT_ARG = 15,
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000252
Chris Lattner827b2532012-05-17 04:30:58 +0000253 // Values from 16+ are only encodable with the inefficient encoding.
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000254 IIT_MMX = 16,
255 IIT_METADATA = 17,
256 IIT_EMPTYSTRUCT = 18,
257 IIT_STRUCT2 = 19,
258 IIT_STRUCT3 = 20,
259 IIT_STRUCT4 = 21,
260 IIT_STRUCT5 = 22,
261 IIT_EXTEND_VEC_ARG = 23,
262 IIT_TRUNC_VEC_ARG = 24,
Jiangning Liu63dc8402013-09-24 02:47:27 +0000263 IIT_ANYPTR = 25,
Andrew Tricka2efd992013-10-31 17:18:11 +0000264 IIT_V1 = 26,
265 IIT_VARARG = 27
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000266};
267
Chris Lattner827b2532012-05-17 04:30:58 +0000268
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000269static void EncodeFixedValueType(MVT::SimpleValueType VT,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000270 std::vector<unsigned char> &Sig) {
Craig Topper8561de92014-01-24 20:50:47 +0000271 if (MVT(VT).isInteger()) {
272 unsigned BitWidth = MVT(VT).getSizeInBits();
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000273 switch (BitWidth) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000274 default: PrintFatalError("unhandled integer type width in intrinsic!");
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000275 case 1: return Sig.push_back(IIT_I1);
276 case 8: return Sig.push_back(IIT_I8);
277 case 16: return Sig.push_back(IIT_I16);
278 case 32: return Sig.push_back(IIT_I32);
279 case 64: return Sig.push_back(IIT_I64);
280 }
281 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000282
Chris Lattner827b2532012-05-17 04:30:58 +0000283 switch (VT) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000284 default: PrintFatalError("unhandled MVT in intrinsic!");
Michael Ilseman6c6d7152013-01-11 01:45:05 +0000285 case MVT::f16: return Sig.push_back(IIT_F16);
Chris Lattner827b2532012-05-17 04:30:58 +0000286 case MVT::f32: return Sig.push_back(IIT_F32);
287 case MVT::f64: return Sig.push_back(IIT_F64);
Chris Lattner827b2532012-05-17 04:30:58 +0000288 case MVT::Metadata: return Sig.push_back(IIT_METADATA);
289 case MVT::x86mmx: return Sig.push_back(IIT_MMX);
290 // MVT::OtherVT is used to mean the empty struct type here.
291 case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT);
Andrew Tricka2efd992013-10-31 17:18:11 +0000292 // MVT::isVoid is used to represent varargs here.
293 case MVT::isVoid: return Sig.push_back(IIT_VARARG);
Chris Lattner827b2532012-05-17 04:30:58 +0000294 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000295}
296
Francois Pichet9522bfc2012-05-17 04:00:03 +0000297#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000298#pragma optimize("",off) // MSVC 2010 optimizer can't deal with this function.
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000299#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000300
Chris Lattnerc4644162012-05-27 16:39:08 +0000301static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes,
Chris Lattnera3b0f522012-05-17 15:55:41 +0000302 std::vector<unsigned char> &Sig) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000303
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000304 if (R->isSubClassOf("LLVMMatchType")) {
Chris Lattner827b2532012-05-17 04:30:58 +0000305 unsigned Number = R->getValueAsInt("Number");
Chris Lattnerc4644162012-05-27 16:39:08 +0000306 assert(Number < ArgCodes.size() && "Invalid matching number!");
Chris Lattner827b2532012-05-17 04:30:58 +0000307 if (R->isSubClassOf("LLVMExtendedElementVectorType"))
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000308 Sig.push_back(IIT_EXTEND_VEC_ARG);
309 else if (R->isSubClassOf("LLVMTruncatedElementVectorType"))
310 Sig.push_back(IIT_TRUNC_VEC_ARG);
311 else
312 Sig.push_back(IIT_ARG);
Chris Lattnerc4644162012-05-27 16:39:08 +0000313 return Sig.push_back((Number << 2) | ArgCodes[Number]);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000314 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000315
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000316 MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT"));
Chris Lattner827b2532012-05-17 04:30:58 +0000317
Chris Lattnerc4644162012-05-27 16:39:08 +0000318 unsigned Tmp = 0;
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000319 switch (VT) {
320 default: break;
Chris Lattnerc4644162012-05-27 16:39:08 +0000321 case MVT::iPTRAny: ++Tmp; // FALL THROUGH.
322 case MVT::vAny: ++Tmp; // FALL THROUGH.
323 case MVT::fAny: ++Tmp; // FALL THROUGH.
324 case MVT::iAny: {
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000325 // If this is an "any" valuetype, then the type is the type of the next
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000326 // type in the list specified to getIntrinsic().
Chris Lattner827b2532012-05-17 04:30:58 +0000327 Sig.push_back(IIT_ARG);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000328
Chris Lattnerc4644162012-05-27 16:39:08 +0000329 // Figure out what arg # this is consuming, and remember what kind it was.
330 unsigned ArgNo = ArgCodes.size();
331 ArgCodes.push_back(Tmp);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000332
Chris Lattnerc4644162012-05-27 16:39:08 +0000333 // Encode what sort of argument it must be in the low 2 bits of the ArgNo.
334 return Sig.push_back((ArgNo << 2) | Tmp);
335 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000336
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000337 case MVT::iPTR: {
338 unsigned AddrSpace = 0;
339 if (R->isSubClassOf("LLVMQualPointerType")) {
340 AddrSpace = R->getValueAsInt("AddrSpace");
341 assert(AddrSpace < 256 && "Address space exceeds 255");
342 }
343 if (AddrSpace) {
344 Sig.push_back(IIT_ANYPTR);
345 Sig.push_back(AddrSpace);
346 } else {
347 Sig.push_back(IIT_PTR);
348 }
Chris Lattnerc4644162012-05-27 16:39:08 +0000349 return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig);
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000350 }
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000351 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000352
Craig Topper8561de92014-01-24 20:50:47 +0000353 if (MVT(VT).isVector()) {
354 MVT VVT = VT;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000355 switch (VVT.getVectorNumElements()) {
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000356 default: PrintFatalError("unhandled vector type width in intrinsic!");
Jiangning Liu63dc8402013-09-24 02:47:27 +0000357 case 1: Sig.push_back(IIT_V1); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000358 case 2: Sig.push_back(IIT_V2); break;
359 case 4: Sig.push_back(IIT_V4); break;
360 case 8: Sig.push_back(IIT_V8); break;
361 case 16: Sig.push_back(IIT_V16); break;
Chris Lattner827b2532012-05-17 04:30:58 +0000362 case 32: Sig.push_back(IIT_V32); break;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000363 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000364
Craig Topper8561de92014-01-24 20:50:47 +0000365 return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000366 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000367
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000368 EncodeFixedValueType(VT, Sig);
369}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000370
371#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000372#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000373#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000374
375/// ComputeFixedEncoding - If we can encode the type signature for this
376/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000377static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
378 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000379 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000380
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000381 if (Int.IS.RetVTs.empty())
382 TypeSig.push_back(IIT_Done);
383 else if (Int.IS.RetVTs.size() == 1 &&
384 Int.IS.RetVTs[0] == MVT::isVoid)
385 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000386 else {
387 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000388 case 1: break;
389 case 2: TypeSig.push_back(IIT_STRUCT2); break;
390 case 3: TypeSig.push_back(IIT_STRUCT3); break;
391 case 4: TypeSig.push_back(IIT_STRUCT4); break;
392 case 5: TypeSig.push_back(IIT_STRUCT5); break;
393 default: assert(0 && "Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000394 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000395
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000396 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000397 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000398 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000399
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000400 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000401 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000402}
403
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000404static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000405 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000406}
407
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000408void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000409 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000410 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
411 // capture it in this vector, otherwise store a ~0U.
412 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000413
Chris Lattnera3b0f522012-05-17 15:55:41 +0000414 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000415
Chris Lattnera3b0f522012-05-17 15:55:41 +0000416 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000417
Jim Laskey2682ea62007-02-07 20:38:26 +0000418 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000419 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000420 // Get the signature for the intrinsic.
421 TypeSig.clear();
422 ComputeFixedEncoding(Ints[i], TypeSig);
423
424 // Check to see if we can encode it into a 32-bit word. We can only encode
425 // 8 nibbles into a 32-bit word.
426 if (TypeSig.size() <= 8) {
427 bool Failed = false;
428 unsigned Result = 0;
429 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
430 // If we had an unencodable argument, bail out.
431 if (TypeSig[i] > 15) {
432 Failed = true;
433 break;
434 }
435 Result = (Result << 4) | TypeSig[e-i-1];
436 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000437
Chris Lattnera3b0f522012-05-17 15:55:41 +0000438 // If this could be encoded into a 31-bit word, return it.
439 if (!Failed && (Result >> 31) == 0) {
440 FixedEncodings.push_back(Result);
441 continue;
442 }
443 }
444
445 // Otherwise, we're going to unique the sequence into the
446 // LongEncodingTable, and use its offset in the 32-bit table instead.
447 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000448
Chris Lattnera3b0f522012-05-17 15:55:41 +0000449 // This is a placehold that we'll replace after the table is laid out.
450 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000451 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000452
Chris Lattnera3b0f522012-05-17 15:55:41 +0000453 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000454
Chris Lattnerf39c2782012-05-27 18:28:35 +0000455 OS << "// Global intrinsic function declaration type table.\n";
456 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
457
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000458 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000459
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000460 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
461 if ((i & 7) == 7)
462 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000463
Chris Lattnera3b0f522012-05-17 15:55:41 +0000464 // If the entry fit in the table, just emit it.
465 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000466 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000467 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000468 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000469
Chris Lattnera3b0f522012-05-17 15:55:41 +0000470 TypeSig.clear();
471 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000472
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000473
Chris Lattnera3b0f522012-05-17 15:55:41 +0000474 // Otherwise, emit the offset into the long encoding table. We emit it this
475 // way so that it is easier to read the offset in the .def file.
476 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000477 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000478
Chris Lattnera3b0f522012-05-17 15:55:41 +0000479 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000480
Chris Lattnera3b0f522012-05-17 15:55:41 +0000481 // Emit the shared table of register lists.
482 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
483 if (!LongEncodingTable.empty())
484 LongEncodingTable.emit(OS, printIITEntry);
485 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000486
Patrik Hägglundca210d82012-05-23 12:34:56 +0000487 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000488}
489
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000490enum ModRefKind {
491 MRK_none,
492 MRK_readonly,
493 MRK_readnone
494};
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
517 if (L->isNoReturn != R->isNoReturn)
518 return R->isNoReturn;
519
520 // Try to order by readonly/readnone attribute.
521 ModRefKind LK = getModRefKind(*L);
522 ModRefKind RK = getModRefKind(*R);
523 if (LK != RK) return (LK > RK);
524
525 // Order by argument attributes.
526 // This is reliable because each side is already sorted internally.
527 return (L->ArgumentAttributes < R->ArgumentAttributes);
528 }
529};
530} // End anonymous namespace
531
Chris Lattner49b7ee12009-01-12 01:18:58 +0000532/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000533void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000534EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000535 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
536 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000537 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000538 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000539 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000540 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000541 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000542
Craig Topperccd651c2012-02-28 06:32:00 +0000543 // Compute the maximum number of attribute arguments and the map
544 typedef std::map<const CodeGenIntrinsic*, unsigned,
545 AttributeComparator> UniqAttrMapTy;
546 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000547 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000548 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000549 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000550 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000551 maxArgAttrs =
552 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000553 unsigned &N = UniqAttributes[&intrinsic];
554 if (N) continue;
555 assert(AttrNum < 256 && "Too many unique attributes for table!");
556 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000557 }
John McCall375dcc92011-05-28 06:31:34 +0000558
Bill Wendling4d3491c2013-01-27 03:25:05 +0000559 // Emit an array of AttributeSet. Most intrinsics will have at least one
560 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000561 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000562
563 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
564 const CodeGenIntrinsic &intrinsic = Ints[i];
565
566 OS << " " << UniqAttributes[&intrinsic] << ", // "
567 << intrinsic.Name << "\n";
568 }
569 OS << " };\n\n";
570
Bill Wendling4d3491c2013-01-27 03:25:05 +0000571 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000572 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000573 OS << " if (id != 0) {\n";
574 OS << " switch(IntrinsicsToAttributesMap[id - ";
575 if (TargetOnly)
576 OS << "Intrinsic::num_intrinsics";
577 else
578 OS << "1";
579 OS << "]) {\n";
580 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000581 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
582 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000583 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000584
Craig Topperccd651c2012-02-28 06:32:00 +0000585 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000586
587 // Keep track of the number of attributes we're writing out.
588 unsigned numAttrs = 0;
589
590 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000591 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
592 if (ae) {
593 while (ai != ae) {
594 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000595
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000596 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
597 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000598
Bill Wendlinged42e792012-10-10 06:13:42 +0000599 do {
600 switch (intrinsic.ArgumentAttributes[ai].second) {
601 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000602 if (addComma)
603 OS << ",";
604 OS << "Attribute::NoCapture";
605 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000606 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000607 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000608 if (addComma)
609 OS << ",";
610 OS << "Attribute::ReadOnly";
611 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000612 break;
613 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000614 if (addComma)
615 OS << ",";
616 OS << "Attributes::ReadNone";
617 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000618 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000619 }
John McCall375dcc92011-05-28 06:31:34 +0000620
Bill Wendlinged42e792012-10-10 06:13:42 +0000621 ++ai;
622 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000623 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000624 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000625 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000626 }
John McCall375dcc92011-05-28 06:31:34 +0000627 }
628
629 ModRefKind modRef = getModRefKind(intrinsic);
630
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000631 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000632 OS << " const Attribute::AttrKind Atts[] = {";
633 bool addComma = false;
634 if (!intrinsic.canThrow) {
635 OS << "Attribute::NoUnwind";
636 addComma = true;
637 }
638 if (intrinsic.isNoReturn) {
639 if (addComma)
640 OS << ",";
641 OS << "Attribute::NoReturn";
642 addComma = true;
643 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000644
John McCall375dcc92011-05-28 06:31:34 +0000645 switch (modRef) {
646 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000647 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000648 if (addComma)
649 OS << ",";
650 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000651 break;
652 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000653 if (addComma)
654 OS << ",";
655 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000656 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000657 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000658 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000659 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000660 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000661 }
John McCall375dcc92011-05-28 06:31:34 +0000662
663 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000664 OS << " NumAttrs = " << numAttrs << ";\n";
665 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000666 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000667 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000668 OS << " return AttributeSet();\n";
John McCall375dcc92011-05-28 06:31:34 +0000669 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000670 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000671
Craig Topper374f19c2012-04-13 06:14:57 +0000672 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000673 OS << " }\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000674 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
Chris Lattner3cb6f832012-05-28 01:47:44 +0000675 "NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000676 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000677 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000678}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000679
Duncan Sands73247d22009-02-14 10:56:35 +0000680/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
681void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000682EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000683 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
684 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
685 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
686 << "\"Unknown intrinsic.\");\n\n";
687
688 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
689 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000690 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000691 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000692 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000693 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000694 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000695 break;
696 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000697 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000698 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000699 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000700 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000701 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000702 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000703 OS << "OnlyAccessesArgumentPointees,\n";
704 break;
705 case CodeGenIntrinsic::ReadWriteMem:
706 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000707 break;
708 }
709 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000710 OS << "};\n\n"
711 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
712 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000713}
714
Chris Lattner69ea0142008-01-04 04:38:35 +0000715/// EmitTargetBuiltins - All of the builtins in the specified map are for the
716/// same target, and we already checked it.
717static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000718 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000719 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000720
Chris Lattner497d13e2010-09-06 03:14:45 +0000721 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000722
Chris Lattner497d13e2010-09-06 03:14:45 +0000723 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
724 E = BIM.end(); I != E; ++I) {
725 std::string ResultCode =
726 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
727 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000728 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000729
730 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000731}
732
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000733
Chris Lattner402a5732006-03-15 01:33:26 +0000734void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000735EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000736 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000737 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000738 BIMTy BuiltinMap;
739 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
740 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000741 // Get the map for this target prefix.
742 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000743
Chris Lattner91678fc2008-01-02 21:24:22 +0000744 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
745 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000746 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
747 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000748 }
749 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000750
Chris Lattner402a5732006-03-15 01:33:26 +0000751 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
752 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
753 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
754 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
755 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000756
Dale Johannesenb842d522009-02-05 01:49:45 +0000757 if (TargetOnly) {
758 OS << "static " << TargetPrefix << "Intrinsic::ID "
759 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000760 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000761 } else {
762 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000763 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000764 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000765
Chris Lattner497d13e2010-09-06 03:14:45 +0000766 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
767 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000768
Chris Lattner402a5732006-03-15 01:33:26 +0000769 // Note: this could emit significantly better code if we cared.
770 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000771 OS << " ";
772 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000773 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000774 else
775 OS << "/* Target Independent Builtins */ ";
776 OS << "{\n";
777
Chris Lattner91678fc2008-01-02 21:24:22 +0000778 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000779 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000780 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000781 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000782 OS << " return ";
783 if (!TargetPrefix.empty())
784 OS << "(" << TargetPrefix << "Intrinsic::ID)";
785 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000786 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000787 OS << "#endif\n\n";
788}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000789
790namespace llvm {
791
792void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
793 IntrinsicEmitter(RK, TargetOnly).run(OS);
794}
795
796} // End llvm namespace