blob: 8f137f83d34bf05e6c2df4294ca676cffa1512d5 [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) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000271 if (EVT(VT).isInteger()) {
272 unsigned BitWidth = EVT(VT).getSizeInBits();
273 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
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000353 if (EVT(VT).isVector()) {
354 EVT VVT = VT;
355 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
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000365 return EncodeFixedValueType(VVT.getVectorElementType().
366 getSimpleVT().SimpleTy, Sig);
367 }
Chris Lattnerc5a825b2012-05-26 23:03:52 +0000368
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000369 EncodeFixedValueType(VT, Sig);
370}
Francois Pichet9522bfc2012-05-17 04:00:03 +0000371
372#ifdef _MSC_VER
Francois Pichetb273b742012-05-17 03:38:19 +0000373#pragma optimize("",on)
Francois Pichet9522bfc2012-05-17 04:00:03 +0000374#endif
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000375
376/// ComputeFixedEncoding - If we can encode the type signature for this
377/// intrinsic into 32 bits, return it. If not, return ~0U.
Chris Lattnera3b0f522012-05-17 15:55:41 +0000378static void ComputeFixedEncoding(const CodeGenIntrinsic &Int,
379 std::vector<unsigned char> &TypeSig) {
Chris Lattnerc4644162012-05-27 16:39:08 +0000380 std::vector<unsigned char> ArgCodes;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000381
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000382 if (Int.IS.RetVTs.empty())
383 TypeSig.push_back(IIT_Done);
384 else if (Int.IS.RetVTs.size() == 1 &&
385 Int.IS.RetVTs[0] == MVT::isVoid)
386 TypeSig.push_back(IIT_Done);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000387 else {
388 switch (Int.IS.RetVTs.size()) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000389 case 1: break;
390 case 2: TypeSig.push_back(IIT_STRUCT2); break;
391 case 3: TypeSig.push_back(IIT_STRUCT3); break;
392 case 4: TypeSig.push_back(IIT_STRUCT4); break;
393 case 5: TypeSig.push_back(IIT_STRUCT5); break;
394 default: assert(0 && "Unhandled case in struct");
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000395 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000396
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000397 for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000398 EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig);
Chris Lattner3e34a7b2012-05-17 05:03:24 +0000399 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000400
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000401 for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i)
Chris Lattnerc4644162012-05-27 16:39:08 +0000402 EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig);
Chris Lattnera3b0f522012-05-17 15:55:41 +0000403}
404
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000405static void printIITEntry(raw_ostream &OS, unsigned char X) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000406 OS << (unsigned)X;
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000407}
408
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000409void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000410 raw_ostream &OS) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000411 // If we can compute a 32-bit fixed encoding for this intrinsic, do so and
412 // capture it in this vector, otherwise store a ~0U.
413 std::vector<unsigned> FixedEncodings;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000414
Chris Lattnera3b0f522012-05-17 15:55:41 +0000415 SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000416
Chris Lattnera3b0f522012-05-17 15:55:41 +0000417 std::vector<unsigned char> TypeSig;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000418
Jim Laskey2682ea62007-02-07 20:38:26 +0000419 // Compute the unique argument type info.
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000420 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Chris Lattnera3b0f522012-05-17 15:55:41 +0000421 // Get the signature for the intrinsic.
422 TypeSig.clear();
423 ComputeFixedEncoding(Ints[i], TypeSig);
424
425 // Check to see if we can encode it into a 32-bit word. We can only encode
426 // 8 nibbles into a 32-bit word.
427 if (TypeSig.size() <= 8) {
428 bool Failed = false;
429 unsigned Result = 0;
430 for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) {
431 // If we had an unencodable argument, bail out.
432 if (TypeSig[i] > 15) {
433 Failed = true;
434 break;
435 }
436 Result = (Result << 4) | TypeSig[e-i-1];
437 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000438
Chris Lattnera3b0f522012-05-17 15:55:41 +0000439 // If this could be encoded into a 31-bit word, return it.
440 if (!Failed && (Result >> 31) == 0) {
441 FixedEncodings.push_back(Result);
442 continue;
443 }
444 }
445
446 // Otherwise, we're going to unique the sequence into the
447 // LongEncodingTable, and use its offset in the 32-bit table instead.
448 LongEncodingTable.add(TypeSig);
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000449
Chris Lattnera3b0f522012-05-17 15:55:41 +0000450 // This is a placehold that we'll replace after the table is laid out.
451 FixedEncodings.push_back(~0U);
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000452 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000453
Chris Lattnera3b0f522012-05-17 15:55:41 +0000454 LongEncodingTable.layout();
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000455
Chris Lattnerf39c2782012-05-27 18:28:35 +0000456 OS << "// Global intrinsic function declaration type table.\n";
457 OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n";
458
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000459 OS << "static const unsigned IIT_Table[] = {\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000460
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000461 for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) {
462 if ((i & 7) == 7)
463 OS << "\n ";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000464
Chris Lattnera3b0f522012-05-17 15:55:41 +0000465 // If the entry fit in the table, just emit it.
466 if (FixedEncodings[i] != ~0U) {
Chris Lattner7f0e7ba2012-05-16 06:34:44 +0000467 OS << "0x" << utohexstr(FixedEncodings[i]) << ", ";
Chris Lattnera3b0f522012-05-17 15:55:41 +0000468 continue;
Jim Laskey2682ea62007-02-07 20:38:26 +0000469 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000470
Chris Lattnera3b0f522012-05-17 15:55:41 +0000471 TypeSig.clear();
472 ComputeFixedEncoding(Ints[i], TypeSig);
Bill Wendling91821472008-11-13 09:08:33 +0000473
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000474
Chris Lattnera3b0f522012-05-17 15:55:41 +0000475 // Otherwise, emit the offset into the long encoding table. We emit it this
476 // way so that it is easier to read the offset in the .def file.
477 OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
Jim Laskey2682ea62007-02-07 20:38:26 +0000478 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000479
Chris Lattnera3b0f522012-05-17 15:55:41 +0000480 OS << "0\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000481
Chris Lattnera3b0f522012-05-17 15:55:41 +0000482 // Emit the shared table of register lists.
483 OS << "static const unsigned char IIT_LongEncodingTable[] = {\n";
484 if (!LongEncodingTable.empty())
485 LongEncodingTable.emit(OS, printIITEntry);
486 OS << " 255\n};\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000487
Patrik Hägglundca210d82012-05-23 12:34:56 +0000488 OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
Jim Laskey2682ea62007-02-07 20:38:26 +0000489}
490
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000491enum ModRefKind {
492 MRK_none,
493 MRK_readonly,
494 MRK_readnone
495};
John McCall375dcc92011-05-28 06:31:34 +0000496
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000497static ModRefKind getModRefKind(const CodeGenIntrinsic &intrinsic) {
498 switch (intrinsic.ModRef) {
499 case CodeGenIntrinsic::NoMem:
500 return MRK_readnone;
501 case CodeGenIntrinsic::ReadArgMem:
502 case CodeGenIntrinsic::ReadMem:
503 return MRK_readonly;
504 case CodeGenIntrinsic::ReadWriteArgMem:
505 case CodeGenIntrinsic::ReadWriteMem:
506 return MRK_none;
John McCall375dcc92011-05-28 06:31:34 +0000507 }
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000508 llvm_unreachable("bad mod-ref kind");
John McCall375dcc92011-05-28 06:31:34 +0000509}
510
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000511namespace {
512struct AttributeComparator {
513 bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const {
514 // Sort throwing intrinsics after non-throwing intrinsics.
515 if (L->canThrow != R->canThrow)
516 return R->canThrow;
517
518 if (L->isNoReturn != R->isNoReturn)
519 return R->isNoReturn;
520
521 // Try to order by readonly/readnone attribute.
522 ModRefKind LK = getModRefKind(*L);
523 ModRefKind RK = getModRefKind(*R);
524 if (LK != RK) return (LK > RK);
525
526 // Order by argument attributes.
527 // This is reliable because each side is already sorted internally.
528 return (L->ArgumentAttributes < R->ArgumentAttributes);
529 }
530};
531} // End anonymous namespace
532
Chris Lattner49b7ee12009-01-12 01:18:58 +0000533/// EmitAttributes - This emits the Intrinsic::getAttributes method.
Chris Lattnere3c2db32006-03-09 22:37:52 +0000534void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000535EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000536 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
537 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000538 if (TargetOnly)
Bill Wendlinge94d8432012-12-07 23:16:57 +0000539 OS << "static AttributeSet getAttributes(LLVMContext &C, " << TargetPrefix
John McCall375dcc92011-05-28 06:31:34 +0000540 << "Intrinsic::ID id) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000541 else
Bill Wendlinge94d8432012-12-07 23:16:57 +0000542 OS << "AttributeSet Intrinsic::getAttributes(LLVMContext &C, ID id) {\n";
John McCall375dcc92011-05-28 06:31:34 +0000543
Craig Topperccd651c2012-02-28 06:32:00 +0000544 // Compute the maximum number of attribute arguments and the map
545 typedef std::map<const CodeGenIntrinsic*, unsigned,
546 AttributeComparator> UniqAttrMapTy;
547 UniqAttrMapTy UniqAttributes;
John McCall375dcc92011-05-28 06:31:34 +0000548 unsigned maxArgAttrs = 0;
Craig Topperccd651c2012-02-28 06:32:00 +0000549 unsigned AttrNum = 0;
Chris Lattner97b0d992006-03-24 01:13:55 +0000550 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
John McCall375dcc92011-05-28 06:31:34 +0000551 const CodeGenIntrinsic &intrinsic = Ints[i];
John McCall375dcc92011-05-28 06:31:34 +0000552 maxArgAttrs =
553 std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size()));
Craig Topperccd651c2012-02-28 06:32:00 +0000554 unsigned &N = UniqAttributes[&intrinsic];
555 if (N) continue;
556 assert(AttrNum < 256 && "Too many unique attributes for table!");
557 N = ++AttrNum;
Chris Lattner97b0d992006-03-24 01:13:55 +0000558 }
John McCall375dcc92011-05-28 06:31:34 +0000559
Bill Wendling4d3491c2013-01-27 03:25:05 +0000560 // Emit an array of AttributeSet. Most intrinsics will have at least one
561 // entry, for the function itself (index ~1), which is usually nounwind.
Craig Topperccd651c2012-02-28 06:32:00 +0000562 OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000563
564 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
565 const CodeGenIntrinsic &intrinsic = Ints[i];
566
567 OS << " " << UniqAttributes[&intrinsic] << ", // "
568 << intrinsic.Name << "\n";
569 }
570 OS << " };\n\n";
571
Bill Wendling4d3491c2013-01-27 03:25:05 +0000572 OS << " AttributeSet AS[" << maxArgAttrs+1 << "];\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000573 OS << " unsigned NumAttrs = 0;\n";
Craig Topper374f19c2012-04-13 06:14:57 +0000574 OS << " if (id != 0) {\n";
575 OS << " switch(IntrinsicsToAttributesMap[id - ";
576 if (TargetOnly)
577 OS << "Intrinsic::num_intrinsics";
578 else
579 OS << "1";
580 OS << "]) {\n";
581 OS << " default: llvm_unreachable(\"Invalid attribute number\");\n";
Craig Topperccd651c2012-02-28 06:32:00 +0000582 for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(),
583 E = UniqAttributes.end(); I != E; ++I) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000584 OS << " case " << I->second << ": {\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000585
Craig Topperccd651c2012-02-28 06:32:00 +0000586 const CodeGenIntrinsic &intrinsic = *(I->first);
John McCall375dcc92011-05-28 06:31:34 +0000587
588 // Keep track of the number of attributes we're writing out.
589 unsigned numAttrs = 0;
590
591 // The argument attributes are alreadys sorted by argument index.
Bill Wendlinged42e792012-10-10 06:13:42 +0000592 unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size();
593 if (ae) {
594 while (ai != ae) {
595 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
Craig Topperccd651c2012-02-28 06:32:00 +0000596
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000597 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
598 bool addComma = false;
Chris Lattner2089cd02009-01-12 02:41:37 +0000599
Bill Wendlinged42e792012-10-10 06:13:42 +0000600 do {
601 switch (intrinsic.ArgumentAttributes[ai].second) {
602 case CodeGenIntrinsic::NoCapture:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000603 if (addComma)
604 OS << ",";
605 OS << "Attribute::NoCapture";
606 addComma = true;
Bill Wendlinged42e792012-10-10 06:13:42 +0000607 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000608 case CodeGenIntrinsic::ReadOnly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000609 if (addComma)
610 OS << ",";
611 OS << "Attribute::ReadOnly";
612 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000613 break;
614 case CodeGenIntrinsic::ReadNone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000615 if (addComma)
616 OS << ",";
617 OS << "Attributes::ReadNone";
618 addComma = true;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000619 break;
Bill Wendlinged42e792012-10-10 06:13:42 +0000620 }
John McCall375dcc92011-05-28 06:31:34 +0000621
Bill Wendlinged42e792012-10-10 06:13:42 +0000622 ++ai;
623 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000624 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000625 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000626 << argNo+1 << ", AttrParam" << argNo +1 << ");\n";
Bill Wendlinged42e792012-10-10 06:13:42 +0000627 }
John McCall375dcc92011-05-28 06:31:34 +0000628 }
629
630 ModRefKind modRef = getModRefKind(intrinsic);
631
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000632 if (!intrinsic.canThrow || modRef || intrinsic.isNoReturn) {
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000633 OS << " const Attribute::AttrKind Atts[] = {";
634 bool addComma = false;
635 if (!intrinsic.canThrow) {
636 OS << "Attribute::NoUnwind";
637 addComma = true;
638 }
639 if (intrinsic.isNoReturn) {
640 if (addComma)
641 OS << ",";
642 OS << "Attribute::NoReturn";
643 addComma = true;
644 }
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000645
John McCall375dcc92011-05-28 06:31:34 +0000646 switch (modRef) {
647 case MRK_none: break;
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000648 case MRK_readonly:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000649 if (addComma)
650 OS << ",";
651 OS << "Attribute::ReadOnly";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000652 break;
653 case MRK_readnone:
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000654 if (addComma)
655 OS << ",";
656 OS << "Attribute::ReadNone";
Chris Lattnerff9e08b2012-05-27 23:20:41 +0000657 break;
Chris Lattner2089cd02009-01-12 02:41:37 +0000658 }
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000659 OS << "};\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000660 OS << " AS[" << numAttrs++ << "] = AttributeSet::get(C, "
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000661 << "AttributeSet::FunctionIndex, Atts);\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000662 }
John McCall375dcc92011-05-28 06:31:34 +0000663
664 if (numAttrs) {
Craig Topper374f19c2012-04-13 06:14:57 +0000665 OS << " NumAttrs = " << numAttrs << ";\n";
666 OS << " break;\n";
Owen Andersonb88cc2f2013-11-16 00:20:01 +0000667 OS << " }\n";
John McCall375dcc92011-05-28 06:31:34 +0000668 } else {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000669 OS << " return AttributeSet();\n";
John McCall375dcc92011-05-28 06:31:34 +0000670 }
Chris Lattner9d0a8772009-01-12 01:27:55 +0000671 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000672
Craig Topper374f19c2012-04-13 06:14:57 +0000673 OS << " }\n";
Chris Lattner9d0a8772009-01-12 01:27:55 +0000674 OS << " }\n";
Bill Wendling4d3491c2013-01-27 03:25:05 +0000675 OS << " return AttributeSet::get(C, ArrayRef<AttributeSet>(AS, "
Chris Lattner3cb6f832012-05-28 01:47:44 +0000676 "NumAttrs));\n";
Chris Lattner49b7ee12009-01-12 01:18:58 +0000677 OS << "}\n";
Chris Lattner2089cd02009-01-12 02:41:37 +0000678 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";
Chris Lattnere3c2db32006-03-09 22:37:52 +0000679}
Chris Lattnerfea17a92006-03-13 23:08:44 +0000680
Duncan Sands73247d22009-02-14 10:56:35 +0000681/// EmitModRefBehavior - Determine intrinsic alias analysis mod/ref behavior.
682void IntrinsicEmitter::
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000683EmitModRefBehavior(const std::vector<CodeGenIntrinsic> &Ints, raw_ostream &OS){
Benjamin Krameraba35032012-03-01 01:18:32 +0000684 OS << "// Determine intrinsic alias analysis mod/ref behavior.\n"
685 << "#ifdef GET_INTRINSIC_MODREF_BEHAVIOR\n"
686 << "assert(iid <= Intrinsic::" << Ints.back().EnumName << " && "
687 << "\"Unknown intrinsic.\");\n\n";
688
689 OS << "static const uint8_t IntrinsicModRefBehavior[] = {\n"
690 << " /* invalid */ UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000691 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
Benjamin Krameraba35032012-03-01 01:18:32 +0000692 OS << " /* " << TargetPrefix << Ints[i].EnumName << " */ ";
Duncan Sands73247d22009-02-14 10:56:35 +0000693 switch (Ints[i].ModRef) {
Duncan Sands73247d22009-02-14 10:56:35 +0000694 case CodeGenIntrinsic::NoMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000695 OS << "DoesNotAccessMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000696 break;
697 case CodeGenIntrinsic::ReadArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000698 OS << "OnlyReadsArgumentPointees,\n";
Dan Gohman88d5f7f2010-11-09 20:07:20 +0000699 break;
Duncan Sands73247d22009-02-14 10:56:35 +0000700 case CodeGenIntrinsic::ReadMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000701 OS << "OnlyReadsMemory,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000702 break;
Dan Gohmanddb2d652010-08-05 23:36:21 +0000703 case CodeGenIntrinsic::ReadWriteArgMem:
Benjamin Krameraba35032012-03-01 01:18:32 +0000704 OS << "OnlyAccessesArgumentPointees,\n";
705 break;
706 case CodeGenIntrinsic::ReadWriteMem:
707 OS << "UnknownModRefBehavior,\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000708 break;
709 }
710 }
Benjamin Krameraba35032012-03-01 01:18:32 +0000711 OS << "};\n\n"
712 << "return static_cast<ModRefBehavior>(IntrinsicModRefBehavior[iid]);\n"
713 << "#endif // GET_INTRINSIC_MODREF_BEHAVIOR\n\n";
Duncan Sands73247d22009-02-14 10:56:35 +0000714}
715
Chris Lattner69ea0142008-01-04 04:38:35 +0000716/// EmitTargetBuiltins - All of the builtins in the specified map are for the
717/// same target, and we already checked it.
718static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
Dale Johannesenb842d522009-02-05 01:49:45 +0000719 const std::string &TargetPrefix,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000720 raw_ostream &OS) {
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000721
Chris Lattner497d13e2010-09-06 03:14:45 +0000722 std::vector<StringMatcher::StringPair> Results;
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000723
Chris Lattner497d13e2010-09-06 03:14:45 +0000724 for (std::map<std::string, std::string>::const_iterator I = BIM.begin(),
725 E = BIM.end(); I != E; ++I) {
726 std::string ResultCode =
727 "return " + TargetPrefix + "Intrinsic::" + I->second + ";";
728 Results.push_back(StringMatcher::StringPair(I->first, ResultCode));
Chris Lattner69ea0142008-01-04 04:38:35 +0000729 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000730
731 StringMatcher("BuiltinName", Results, OS).Emit();
Chris Lattner69ea0142008-01-04 04:38:35 +0000732}
733
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000734
Chris Lattner402a5732006-03-15 01:33:26 +0000735void IntrinsicEmitter::
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000736EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000737 raw_ostream &OS) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000738 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner402a5732006-03-15 01:33:26 +0000739 BIMTy BuiltinMap;
740 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
741 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattner91678fc2008-01-02 21:24:22 +0000742 // Get the map for this target prefix.
743 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000744
Chris Lattner91678fc2008-01-02 21:24:22 +0000745 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
746 Ints[i].EnumName)).second)
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000747 PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() +
748 "': duplicate GCC builtin name!");
Chris Lattner402a5732006-03-15 01:33:26 +0000749 }
750 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000751
Chris Lattner402a5732006-03-15 01:33:26 +0000752 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
753 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
754 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
755 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
756 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000757
Dale Johannesenb842d522009-02-05 01:49:45 +0000758 if (TargetOnly) {
759 OS << "static " << TargetPrefix << "Intrinsic::ID "
760 << "getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000761 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000762 } else {
763 OS << "Intrinsic::ID Intrinsic::getIntrinsicForGCCBuiltin(const char "
Chris Lattner497d13e2010-09-06 03:14:45 +0000764 << "*TargetPrefixStr, const char *BuiltinNameStr) {\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000765 }
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000766
Chris Lattner497d13e2010-09-06 03:14:45 +0000767 OS << " StringRef BuiltinName(BuiltinNameStr);\n";
768 OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n";
Andrew Trickd4d1d9c2013-10-31 17:18:07 +0000769
Chris Lattner402a5732006-03-15 01:33:26 +0000770 // Note: this could emit significantly better code if we cared.
771 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattner91678fc2008-01-02 21:24:22 +0000772 OS << " ";
773 if (!I->first.empty())
Chris Lattner497d13e2010-09-06 03:14:45 +0000774 OS << "if (TargetPrefix == \"" << I->first << "\") ";
Chris Lattner91678fc2008-01-02 21:24:22 +0000775 else
776 OS << "/* Target Independent Builtins */ ";
777 OS << "{\n";
778
Chris Lattner91678fc2008-01-02 21:24:22 +0000779 // Emit the comparisons for this target prefix.
Dale Johannesenb842d522009-02-05 01:49:45 +0000780 EmitTargetBuiltins(I->second, TargetPrefix, OS);
Chris Lattner91678fc2008-01-02 21:24:22 +0000781 OS << " }\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000782 }
Chris Lattner497d13e2010-09-06 03:14:45 +0000783 OS << " return ";
784 if (!TargetPrefix.empty())
785 OS << "(" << TargetPrefix << "Intrinsic::ID)";
786 OS << "Intrinsic::not_intrinsic;\n";
Dale Johannesenb842d522009-02-05 01:49:45 +0000787 OS << "}\n";
Chris Lattner402a5732006-03-15 01:33:26 +0000788 OS << "#endif\n\n";
789}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000790
791namespace llvm {
792
793void EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly = false) {
794 IntrinsicEmitter(RK, TargetOnly).run(OS);
795}
796
797} // End llvm namespace