Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 1 | //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 8adcd9f | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits information about intrinsic functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 14 | #include "CodeGenIntrinsics.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 15 | #include "CodeGenTarget.h" |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 16 | #include "SequenceToOffsetTable.h" |
Richard Smith | 6bc9df3 | 2014-04-20 20:26:39 +0000 | [diff] [blame] | 17 | #include "TableGenBackends.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 19 | #include "llvm/TableGen/Error.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 20 | #include "llvm/TableGen/Record.h" |
Douglas Gregor | 12c1cd3 | 2012-05-02 17:32:48 +0000 | [diff] [blame] | 21 | #include "llvm/TableGen/StringMatcher.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 22 | #include "llvm/TableGen/TableGenBackend.h" |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 23 | #include "llvm/TableGen/StringToOffsetTable.h" |
Jeff Cohen | c4e2468 | 2006-03-15 02:51:05 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | class IntrinsicEmitter { |
| 29 | RecordKeeper &Records; |
| 30 | bool TargetOnly; |
| 31 | std::string TargetPrefix; |
| 32 | |
| 33 | public: |
| 34 | IntrinsicEmitter(RecordKeeper &R, bool T) |
| 35 | : Records(R), TargetOnly(T) {} |
| 36 | |
| 37 | void run(raw_ostream &OS); |
| 38 | |
| 39 | void EmitPrefix(raw_ostream &OS); |
| 40 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 41 | void EmitEnumInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); |
| 42 | void EmitTargetInfo(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); |
| 43 | void EmitIntrinsicToNameTable(const CodeGenIntrinsicTable &Ints, |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 44 | raw_ostream &OS); |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 45 | void EmitIntrinsicToOverloadTable(const CodeGenIntrinsicTable &Ints, |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 46 | raw_ostream &OS); |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 47 | void EmitGenerator(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); |
| 48 | void EmitAttributes(const CodeGenIntrinsicTable &Ints, raw_ostream &OS); |
| 49 | void EmitIntrinsicToBuiltinMap(const CodeGenIntrinsicTable &Ints, bool IsGCC, |
| 50 | raw_ostream &OS); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 51 | void EmitSuffix(raw_ostream &OS); |
| 52 | }; |
| 53 | } // End anonymous namespace |
| 54 | |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 56 | // IntrinsicEmitter Implementation |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 59 | void IntrinsicEmitter::run(raw_ostream &OS) { |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 60 | emitSourceFileHeader("Intrinsic Function Source Fragment", OS); |
| 61 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 62 | CodeGenIntrinsicTable Ints(Records, TargetOnly); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 63 | |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 64 | if (TargetOnly && !Ints.empty()) |
| 65 | TargetPrefix = Ints[0].TargetPrefix; |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 6739a89 | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 67 | EmitPrefix(OS); |
| 68 | |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 69 | // Emit the enum information. |
| 70 | EmitEnumInfo(Ints, OS); |
Chris Lattner | da1a4cc | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 71 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 72 | // Emit the target metadata. |
| 73 | EmitTargetInfo(Ints, OS); |
| 74 | |
Chris Lattner | da1a4cc | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 75 | // Emit the intrinsic ID -> name table. |
| 76 | EmitIntrinsicToNameTable(Ints, OS); |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 77 | |
| 78 | // Emit the intrinsic ID -> overload table. |
| 79 | EmitIntrinsicToOverloadTable(Ints, OS); |
| 80 | |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 81 | // Emit the intrinsic declaration generator. |
| 82 | EmitGenerator(Ints, OS); |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 83 | |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 84 | // Emit the intrinsic parameter attributes. |
| 85 | EmitAttributes(Ints, OS); |
Chris Lattner | fea17a9 | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 86 | |
Aditya Nandakumar | 8a76f91 | 2017-04-19 19:14:20 +0000 | [diff] [blame] | 87 | // Emit code to translate GCC builtins into LLVM intrinsics. |
| 88 | EmitIntrinsicToBuiltinMap(Ints, true, OS); |
Douglas Gregor | 6739a89 | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 89 | |
Aditya Nandakumar | 8a76f91 | 2017-04-19 19:14:20 +0000 | [diff] [blame] | 90 | // Emit code to translate MS builtins into LLVM intrinsics. |
| 91 | EmitIntrinsicToBuiltinMap(Ints, false, OS); |
Saleem Abdulrasool | 4e63fc4 | 2014-07-04 18:42:25 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 6739a89 | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 93 | EmitSuffix(OS); |
| 94 | } |
| 95 | |
| 96 | void IntrinsicEmitter::EmitPrefix(raw_ostream &OS) { |
| 97 | OS << "// VisualStudio defines setjmp as _setjmp\n" |
Michael J. Spencer | ded5f66 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 98 | "#if defined(_MSC_VER) && defined(setjmp) && \\\n" |
| 99 | " !defined(setjmp_undefined_for_msvc)\n" |
Michael J. Spencer | 511dce0 | 2010-09-14 04:27:38 +0000 | [diff] [blame] | 100 | "# pragma push_macro(\"setjmp\")\n" |
| 101 | "# undef setjmp\n" |
Michael J. Spencer | ded5f66 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 102 | "# define setjmp_undefined_for_msvc\n" |
Douglas Gregor | 6739a89 | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 103 | "#endif\n\n"; |
| 104 | } |
| 105 | |
| 106 | void IntrinsicEmitter::EmitSuffix(raw_ostream &OS) { |
Michael J. Spencer | ded5f66 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 107 | OS << "#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)\n" |
Douglas Gregor | 6739a89 | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 108 | "// let's return it to _setjmp state\n" |
Michael J. Spencer | 511dce0 | 2010-09-14 04:27:38 +0000 | [diff] [blame] | 109 | "# pragma pop_macro(\"setjmp\")\n" |
Michael J. Spencer | ded5f66 | 2010-09-24 19:48:47 +0000 | [diff] [blame] | 110 | "# undef setjmp_undefined_for_msvc\n" |
Douglas Gregor | 6739a89 | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 111 | "#endif\n\n"; |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 114 | void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints, |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 115 | raw_ostream &OS) { |
Chris Lattner | 6d8104e | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 116 | OS << "// Enum values for Intrinsics.h\n"; |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 117 | OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; |
| 118 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 119 | OS << " " << Ints[i].EnumName; |
| 120 | OS << ((i != e-1) ? ", " : " "); |
Justin Holewinski | 8a5bf7f | 2014-07-17 11:23:29 +0000 | [diff] [blame] | 121 | if (Ints[i].EnumName.size() < 40) |
| 122 | OS << std::string(40-Ints[i].EnumName.size(), ' '); |
| 123 | OS << " // " << Ints[i].Name << "\n"; |
Chris Lattner | c313d0b | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 124 | } |
| 125 | OS << "#endif\n\n"; |
| 126 | } |
Chris Lattner | 6d8104e | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 127 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 128 | void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints, |
| 129 | raw_ostream &OS) { |
| 130 | OS << "// Target mapping\n"; |
| 131 | OS << "#ifdef GET_INTRINSIC_TARGET_DATA\n"; |
| 132 | OS << "struct IntrinsicTargetInfo {\n" |
Saleem Abdulrasool | 6f5f001 | 2017-01-31 00:45:01 +0000 | [diff] [blame] | 133 | << " llvm::StringLiteral Name;\n" |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 134 | << " size_t Offset;\n" |
| 135 | << " size_t Count;\n" |
| 136 | << "};\n"; |
Benjamin Kramer | 7432113 | 2017-01-30 18:49:24 +0000 | [diff] [blame] | 137 | OS << "static constexpr IntrinsicTargetInfo TargetInfos[] = {\n"; |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 138 | for (auto Target : Ints.Targets) |
Saleem Abdulrasool | 6f5f001 | 2017-01-31 00:45:01 +0000 | [diff] [blame] | 139 | OS << " {llvm::StringLiteral(\"" << Target.Name << "\"), " << Target.Offset |
Benjamin Kramer | a9df941 | 2017-01-30 19:05:09 +0000 | [diff] [blame] | 140 | << ", " << Target.Count << "},\n"; |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 141 | OS << "};\n"; |
| 142 | OS << "#endif\n\n"; |
| 143 | } |
| 144 | |
| 145 | void IntrinsicEmitter::EmitIntrinsicToNameTable( |
| 146 | const CodeGenIntrinsicTable &Ints, raw_ostream &OS) { |
Chris Lattner | da1a4cc | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 147 | OS << "// Intrinsic ID to name table\n"; |
| 148 | OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; |
| 149 | OS << " // Note that entry #0 is the invalid intrinsic!\n"; |
Evan Cheng | c2c8b58 | 2006-03-28 22:25:56 +0000 | [diff] [blame] | 150 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 151 | OS << " \"" << Ints[i].Name << "\",\n"; |
Chris Lattner | da1a4cc | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 152 | OS << "#endif\n\n"; |
| 153 | } |
| 154 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 155 | void IntrinsicEmitter::EmitIntrinsicToOverloadTable( |
| 156 | const CodeGenIntrinsicTable &Ints, raw_ostream &OS) { |
Benjamin Kramer | acd78d5 | 2012-03-01 02:16:57 +0000 | [diff] [blame] | 157 | OS << "// Intrinsic ID to overload bitset\n"; |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 158 | OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n"; |
Benjamin Kramer | acd78d5 | 2012-03-01 02:16:57 +0000 | [diff] [blame] | 159 | OS << "static const uint8_t OTable[] = {\n"; |
| 160 | OS << " 0"; |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 161 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
Benjamin Kramer | acd78d5 | 2012-03-01 02:16:57 +0000 | [diff] [blame] | 162 | // Add one to the index so we emit a null bit for the invalid #0 intrinsic. |
| 163 | if ((i+1)%8 == 0) |
| 164 | OS << ",\n 0"; |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 165 | if (Ints[i].isOverloaded) |
Benjamin Kramer | acd78d5 | 2012-03-01 02:16:57 +0000 | [diff] [blame] | 166 | OS << " | (1<<" << (i+1)%8 << ')'; |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 167 | } |
Benjamin Kramer | acd78d5 | 2012-03-01 02:16:57 +0000 | [diff] [blame] | 168 | OS << "\n};\n\n"; |
| 169 | // OTable contains a true bit at the position if the intrinsic is overloaded. |
| 170 | OS << "return (OTable[id/8] & (1 << (id%8))) != 0;\n"; |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 171 | OS << "#endif\n\n"; |
| 172 | } |
| 173 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 174 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 175 | // NOTE: This must be kept in synch with the copy in lib/VMCore/Function.cpp! |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 176 | enum IIT_Info { |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 177 | // Common values should be encoded with 0-15. |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 178 | IIT_Done = 0, |
| 179 | IIT_I1 = 1, |
| 180 | IIT_I8 = 2, |
| 181 | IIT_I16 = 3, |
| 182 | IIT_I32 = 4, |
| 183 | IIT_I64 = 5, |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 184 | IIT_F16 = 6, |
| 185 | IIT_F32 = 7, |
| 186 | IIT_F64 = 8, |
| 187 | IIT_V2 = 9, |
| 188 | IIT_V4 = 10, |
| 189 | IIT_V8 = 11, |
| 190 | IIT_V16 = 12, |
| 191 | IIT_V32 = 13, |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 192 | IIT_PTR = 14, |
| 193 | IIT_ARG = 15, |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 194 | |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 195 | // Values from 16+ are only encodable with the inefficient encoding. |
| 196 | IIT_V64 = 16, |
Robert Khasanov | b25e562 | 2014-09-30 11:32:22 +0000 | [diff] [blame] | 197 | IIT_MMX = 17, |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 198 | IIT_TOKEN = 18, |
| 199 | IIT_METADATA = 19, |
| 200 | IIT_EMPTYSTRUCT = 20, |
| 201 | IIT_STRUCT2 = 21, |
| 202 | IIT_STRUCT3 = 22, |
| 203 | IIT_STRUCT4 = 23, |
| 204 | IIT_STRUCT5 = 24, |
| 205 | IIT_EXTEND_ARG = 25, |
| 206 | IIT_TRUNC_ARG = 26, |
| 207 | IIT_ANYPTR = 27, |
| 208 | IIT_V1 = 28, |
| 209 | IIT_VARARG = 29, |
| 210 | IIT_HALF_VEC_ARG = 30, |
| 211 | IIT_SAME_VEC_WIDTH_ARG = 31, |
| 212 | IIT_PTR_TO_ARG = 32, |
Elena Demikhovsky | caaceef | 2016-11-03 03:23:55 +0000 | [diff] [blame] | 213 | IIT_PTR_TO_ELT = 33, |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 214 | IIT_VEC_OF_ANYPTRS_TO_ELT = 34, |
Elena Demikhovsky | caaceef | 2016-11-03 03:23:55 +0000 | [diff] [blame] | 215 | IIT_I128 = 35, |
| 216 | IIT_V512 = 36, |
Artem Belevich | 786ca6a | 2017-10-12 17:40:00 +0000 | [diff] [blame] | 217 | IIT_V1024 = 37, |
| 218 | IIT_STRUCT6 = 38, |
| 219 | IIT_STRUCT7 = 39, |
| 220 | IIT_STRUCT8 = 40 |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | static void EncodeFixedValueType(MVT::SimpleValueType VT, |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 224 | std::vector<unsigned char> &Sig) { |
Craig Topper | 8561de9 | 2014-01-24 20:50:47 +0000 | [diff] [blame] | 225 | if (MVT(VT).isInteger()) { |
| 226 | unsigned BitWidth = MVT(VT).getSizeInBits(); |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 227 | switch (BitWidth) { |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 228 | default: PrintFatalError("unhandled integer type width in intrinsic!"); |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 229 | case 1: return Sig.push_back(IIT_I1); |
| 230 | case 8: return Sig.push_back(IIT_I8); |
| 231 | case 16: return Sig.push_back(IIT_I16); |
| 232 | case 32: return Sig.push_back(IIT_I32); |
| 233 | case 64: return Sig.push_back(IIT_I64); |
Kit Barton | 6646033 | 2015-05-25 15:49:26 +0000 | [diff] [blame] | 234 | case 128: return Sig.push_back(IIT_I128); |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 238 | switch (VT) { |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 239 | default: PrintFatalError("unhandled MVT in intrinsic!"); |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 240 | case MVT::f16: return Sig.push_back(IIT_F16); |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 241 | case MVT::f32: return Sig.push_back(IIT_F32); |
| 242 | case MVT::f64: return Sig.push_back(IIT_F64); |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 243 | case MVT::token: return Sig.push_back(IIT_TOKEN); |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 244 | case MVT::Metadata: return Sig.push_back(IIT_METADATA); |
| 245 | case MVT::x86mmx: return Sig.push_back(IIT_MMX); |
| 246 | // MVT::OtherVT is used to mean the empty struct type here. |
| 247 | case MVT::Other: return Sig.push_back(IIT_EMPTYSTRUCT); |
Andrew Trick | a2efd99 | 2013-10-31 17:18:11 +0000 | [diff] [blame] | 248 | // MVT::isVoid is used to represent varargs here. |
| 249 | case MVT::isVoid: return Sig.push_back(IIT_VARARG); |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Nico Weber | 262a133 | 2016-10-25 17:46:29 +0000 | [diff] [blame] | 253 | #if defined(_MSC_VER) && !defined(__clang__) |
| 254 | #pragma optimize("",off) // MSVC 2015 optimizer can't deal with this function. |
| 255 | #endif |
| 256 | |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 257 | static void EncodeFixedType(Record *R, std::vector<unsigned char> &ArgCodes, |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 258 | std::vector<unsigned char> &Sig) { |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 260 | if (R->isSubClassOf("LLVMMatchType")) { |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 261 | unsigned Number = R->getValueAsInt("Number"); |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 262 | assert(Number < ArgCodes.size() && "Invalid matching number!"); |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 263 | if (R->isSubClassOf("LLVMExtendedType")) |
| 264 | Sig.push_back(IIT_EXTEND_ARG); |
| 265 | else if (R->isSubClassOf("LLVMTruncatedType")) |
| 266 | Sig.push_back(IIT_TRUNC_ARG); |
Tim Northover | 4516de3 | 2014-03-29 07:04:54 +0000 | [diff] [blame] | 267 | else if (R->isSubClassOf("LLVMHalfElementsVectorType")) |
| 268 | Sig.push_back(IIT_HALF_VEC_ARG); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 269 | else if (R->isSubClassOf("LLVMVectorSameWidth")) { |
| 270 | Sig.push_back(IIT_SAME_VEC_WIDTH_ARG); |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 271 | Sig.push_back((Number << 3) | ArgCodes[Number]); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 272 | MVT::SimpleValueType VT = getValueType(R->getValueAsDef("ElTy")); |
| 273 | EncodeFixedValueType(VT, Sig); |
| 274 | return; |
| 275 | } |
Elena Demikhovsky | 23a485a | 2015-02-08 08:27:19 +0000 | [diff] [blame] | 276 | else if (R->isSubClassOf("LLVMPointerTo")) |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 277 | Sig.push_back(IIT_PTR_TO_ARG); |
Elad Cohen | ef5798a | 2017-05-03 12:28:54 +0000 | [diff] [blame] | 278 | else if (R->isSubClassOf("LLVMVectorOfAnyPointersToElt")) { |
| 279 | Sig.push_back(IIT_VEC_OF_ANYPTRS_TO_ELT); |
| 280 | unsigned ArgNo = ArgCodes.size(); |
| 281 | ArgCodes.push_back(3 /*vAny*/); |
| 282 | // Encode overloaded ArgNo |
| 283 | Sig.push_back(ArgNo); |
| 284 | // Encode LLVMMatchType<Number> ArgNo |
| 285 | Sig.push_back(Number); |
| 286 | return; |
| 287 | } else if (R->isSubClassOf("LLVMPointerToElt")) |
Elena Demikhovsky | caaceef | 2016-11-03 03:23:55 +0000 | [diff] [blame] | 288 | Sig.push_back(IIT_PTR_TO_ELT); |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 289 | else |
| 290 | Sig.push_back(IIT_ARG); |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 291 | return Sig.push_back((Number << 3) | ArgCodes[Number]); |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 292 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 294 | MVT::SimpleValueType VT = getValueType(R->getValueAsDef("VT")); |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 295 | |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 296 | unsigned Tmp = 0; |
Chris Lattner | c5a825b | 2012-05-26 23:03:52 +0000 | [diff] [blame] | 297 | switch (VT) { |
| 298 | default: break; |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 299 | case MVT::iPTRAny: ++Tmp; LLVM_FALLTHROUGH; |
| 300 | case MVT::vAny: ++Tmp; LLVM_FALLTHROUGH; |
| 301 | case MVT::fAny: ++Tmp; LLVM_FALLTHROUGH; |
| 302 | case MVT::iAny: ++Tmp; LLVM_FALLTHROUGH; |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 303 | case MVT::Any: { |
Chris Lattner | c5a825b | 2012-05-26 23:03:52 +0000 | [diff] [blame] | 304 | // If this is an "any" valuetype, then the type is the type of the next |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 305 | // type in the list specified to getIntrinsic(). |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 306 | Sig.push_back(IIT_ARG); |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 307 | |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 308 | // Figure out what arg # this is consuming, and remember what kind it was. |
| 309 | unsigned ArgNo = ArgCodes.size(); |
| 310 | ArgCodes.push_back(Tmp); |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 311 | |
Ramkumar Ramachandra | 75a4f35 | 2015-01-22 20:14:38 +0000 | [diff] [blame] | 312 | // Encode what sort of argument it must be in the low 3 bits of the ArgNo. |
| 313 | return Sig.push_back((ArgNo << 3) | Tmp); |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 314 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 315 | |
Chris Lattner | c5a825b | 2012-05-26 23:03:52 +0000 | [diff] [blame] | 316 | case MVT::iPTR: { |
| 317 | unsigned AddrSpace = 0; |
| 318 | if (R->isSubClassOf("LLVMQualPointerType")) { |
| 319 | AddrSpace = R->getValueAsInt("AddrSpace"); |
| 320 | assert(AddrSpace < 256 && "Address space exceeds 255"); |
| 321 | } |
| 322 | if (AddrSpace) { |
| 323 | Sig.push_back(IIT_ANYPTR); |
| 324 | Sig.push_back(AddrSpace); |
| 325 | } else { |
| 326 | Sig.push_back(IIT_PTR); |
| 327 | } |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 328 | return EncodeFixedType(R->getValueAsDef("ElTy"), ArgCodes, Sig); |
Chris Lattner | c5a825b | 2012-05-26 23:03:52 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 330 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 331 | |
Craig Topper | 8561de9 | 2014-01-24 20:50:47 +0000 | [diff] [blame] | 332 | if (MVT(VT).isVector()) { |
| 333 | MVT VVT = VT; |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 334 | switch (VVT.getVectorNumElements()) { |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 335 | default: PrintFatalError("unhandled vector type width in intrinsic!"); |
Jiangning Liu | 63dc840 | 2013-09-24 02:47:27 +0000 | [diff] [blame] | 336 | case 1: Sig.push_back(IIT_V1); break; |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 337 | case 2: Sig.push_back(IIT_V2); break; |
| 338 | case 4: Sig.push_back(IIT_V4); break; |
| 339 | case 8: Sig.push_back(IIT_V8); break; |
| 340 | case 16: Sig.push_back(IIT_V16); break; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 341 | case 32: Sig.push_back(IIT_V32); break; |
Robert Khasanov | b25e562 | 2014-09-30 11:32:22 +0000 | [diff] [blame] | 342 | case 64: Sig.push_back(IIT_V64); break; |
Krzysztof Parzyszek | b8bb90b | 2015-11-24 16:28:14 +0000 | [diff] [blame] | 343 | case 512: Sig.push_back(IIT_V512); break; |
| 344 | case 1024: Sig.push_back(IIT_V1024); break; |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 345 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 346 | |
Craig Topper | 8561de9 | 2014-01-24 20:50:47 +0000 | [diff] [blame] | 347 | return EncodeFixedValueType(VVT.getVectorElementType().SimpleTy, Sig); |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 348 | } |
Chris Lattner | c5a825b | 2012-05-26 23:03:52 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 350 | EncodeFixedValueType(VT, Sig); |
| 351 | } |
Francois Pichet | 9522bfc | 2012-05-17 04:00:03 +0000 | [diff] [blame] | 352 | |
Nico Weber | 262a133 | 2016-10-25 17:46:29 +0000 | [diff] [blame] | 353 | #if defined(_MSC_VER) && !defined(__clang__) |
| 354 | #pragma optimize("",on) |
| 355 | #endif |
| 356 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 357 | /// ComputeFixedEncoding - If we can encode the type signature for this |
| 358 | /// intrinsic into 32 bits, return it. If not, return ~0U. |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 359 | static void ComputeFixedEncoding(const CodeGenIntrinsic &Int, |
| 360 | std::vector<unsigned char> &TypeSig) { |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 361 | std::vector<unsigned char> ArgCodes; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 363 | if (Int.IS.RetVTs.empty()) |
| 364 | TypeSig.push_back(IIT_Done); |
| 365 | else if (Int.IS.RetVTs.size() == 1 && |
| 366 | Int.IS.RetVTs[0] == MVT::isVoid) |
| 367 | TypeSig.push_back(IIT_Done); |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 368 | else { |
| 369 | switch (Int.IS.RetVTs.size()) { |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 370 | case 1: break; |
| 371 | case 2: TypeSig.push_back(IIT_STRUCT2); break; |
| 372 | case 3: TypeSig.push_back(IIT_STRUCT3); break; |
| 373 | case 4: TypeSig.push_back(IIT_STRUCT4); break; |
| 374 | case 5: TypeSig.push_back(IIT_STRUCT5); break; |
Artem Belevich | 786ca6a | 2017-10-12 17:40:00 +0000 | [diff] [blame] | 375 | case 6: TypeSig.push_back(IIT_STRUCT6); break; |
| 376 | case 7: TypeSig.push_back(IIT_STRUCT7); break; |
| 377 | case 8: TypeSig.push_back(IIT_STRUCT8); break; |
Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 378 | default: llvm_unreachable("Unhandled case in struct"); |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 379 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 381 | for (unsigned i = 0, e = Int.IS.RetVTs.size(); i != e; ++i) |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 382 | EncodeFixedType(Int.IS.RetTypeDefs[i], ArgCodes, TypeSig); |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 383 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 385 | for (unsigned i = 0, e = Int.IS.ParamTypeDefs.size(); i != e; ++i) |
Chris Lattner | c464416 | 2012-05-27 16:39:08 +0000 | [diff] [blame] | 386 | EncodeFixedType(Int.IS.ParamTypeDefs[i], ArgCodes, TypeSig); |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 389 | static void printIITEntry(raw_ostream &OS, unsigned char X) { |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 390 | OS << (unsigned)X; |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 393 | void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints, |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 394 | raw_ostream &OS) { |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 395 | // If we can compute a 32-bit fixed encoding for this intrinsic, do so and |
| 396 | // capture it in this vector, otherwise store a ~0U. |
| 397 | std::vector<unsigned> FixedEncodings; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 398 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 399 | SequenceToOffsetTable<std::vector<unsigned char> > LongEncodingTable; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 400 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 401 | std::vector<unsigned char> TypeSig; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 402 | |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 403 | // Compute the unique argument type info. |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 404 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 405 | // Get the signature for the intrinsic. |
| 406 | TypeSig.clear(); |
| 407 | ComputeFixedEncoding(Ints[i], TypeSig); |
| 408 | |
| 409 | // Check to see if we can encode it into a 32-bit word. We can only encode |
| 410 | // 8 nibbles into a 32-bit word. |
| 411 | if (TypeSig.size() <= 8) { |
| 412 | bool Failed = false; |
| 413 | unsigned Result = 0; |
| 414 | for (unsigned i = 0, e = TypeSig.size(); i != e; ++i) { |
| 415 | // If we had an unencodable argument, bail out. |
| 416 | if (TypeSig[i] > 15) { |
| 417 | Failed = true; |
| 418 | break; |
| 419 | } |
| 420 | Result = (Result << 4) | TypeSig[e-i-1]; |
| 421 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 422 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 423 | // If this could be encoded into a 31-bit word, return it. |
| 424 | if (!Failed && (Result >> 31) == 0) { |
| 425 | FixedEncodings.push_back(Result); |
| 426 | continue; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Otherwise, we're going to unique the sequence into the |
| 431 | // LongEncodingTable, and use its offset in the 32-bit table instead. |
| 432 | LongEncodingTable.add(TypeSig); |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 433 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 434 | // This is a placehold that we'll replace after the table is laid out. |
| 435 | FixedEncodings.push_back(~0U); |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 436 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 437 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 438 | LongEncodingTable.layout(); |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 439 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 440 | OS << "// Global intrinsic function declaration type table.\n"; |
| 441 | OS << "#ifdef GET_INTRINSIC_GENERATOR_GLOBAL\n"; |
| 442 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 443 | OS << "static const unsigned IIT_Table[] = {\n "; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 445 | for (unsigned i = 0, e = FixedEncodings.size(); i != e; ++i) { |
| 446 | if ((i & 7) == 7) |
| 447 | OS << "\n "; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 448 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 449 | // If the entry fit in the table, just emit it. |
| 450 | if (FixedEncodings[i] != ~0U) { |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 451 | OS << "0x" << Twine::utohexstr(FixedEncodings[i]) << ", "; |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 452 | continue; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 453 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 454 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 455 | TypeSig.clear(); |
| 456 | ComputeFixedEncoding(Ints[i], TypeSig); |
Bill Wendling | 9182147 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 457 | |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 458 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 459 | // Otherwise, emit the offset into the long encoding table. We emit it this |
| 460 | // way so that it is easier to read the offset in the .def file. |
| 461 | OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", "; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 462 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 463 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 464 | OS << "0\n};\n\n"; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 465 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 466 | // Emit the shared table of register lists. |
| 467 | OS << "static const unsigned char IIT_LongEncodingTable[] = {\n"; |
| 468 | if (!LongEncodingTable.empty()) |
| 469 | LongEncodingTable.emit(OS, printIITEntry); |
| 470 | OS << " 255\n};\n\n"; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 471 | |
Patrik Hägglund | ca210d8 | 2012-05-23 12:34:56 +0000 | [diff] [blame] | 472 | OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Richard Smith | 6bc9df3 | 2014-04-20 20:26:39 +0000 | [diff] [blame] | 475 | namespace { |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 476 | struct AttributeComparator { |
| 477 | bool operator()(const CodeGenIntrinsic *L, const CodeGenIntrinsic *R) const { |
| 478 | // Sort throwing intrinsics after non-throwing intrinsics. |
| 479 | if (L->canThrow != R->canThrow) |
| 480 | return R->canThrow; |
| 481 | |
Eli Bendersky | 2281ef9 | 2014-03-18 23:51:07 +0000 | [diff] [blame] | 482 | if (L->isNoDuplicate != R->isNoDuplicate) |
| 483 | return R->isNoDuplicate; |
| 484 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 485 | if (L->isNoReturn != R->isNoReturn) |
| 486 | return R->isNoReturn; |
| 487 | |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 488 | if (L->isConvergent != R->isConvergent) |
| 489 | return R->isConvergent; |
| 490 | |
Matt Arsenault | b19b57e | 2017-04-28 20:25:27 +0000 | [diff] [blame] | 491 | if (L->isSpeculatable != R->isSpeculatable) |
| 492 | return R->isSpeculatable; |
| 493 | |
Matt Arsenault | 868af92 | 2017-04-28 21:01:46 +0000 | [diff] [blame] | 494 | if (L->hasSideEffects != R->hasSideEffects) |
| 495 | return R->hasSideEffects; |
| 496 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 497 | // Try to order by readonly/readnone attribute. |
Nicolai Haehnle | b48275f | 2016-04-19 21:58:33 +0000 | [diff] [blame] | 498 | CodeGenIntrinsic::ModRefBehavior LK = L->ModRef; |
| 499 | CodeGenIntrinsic::ModRefBehavior RK = R->ModRef; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 500 | if (LK != RK) return (LK > RK); |
| 501 | |
| 502 | // Order by argument attributes. |
| 503 | // This is reliable because each side is already sorted internally. |
| 504 | return (L->ArgumentAttributes < R->ArgumentAttributes); |
| 505 | } |
| 506 | }; |
| 507 | } // End anonymous namespace |
| 508 | |
Chris Lattner | 49b7ee1 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 509 | /// EmitAttributes - This emits the Intrinsic::getAttributes method. |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 510 | void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints, |
| 511 | raw_ostream &OS) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 512 | OS << "// Add parameter attributes that are not common to all intrinsics.\n"; |
| 513 | OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 514 | if (TargetOnly) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 515 | OS << "static AttributeList getAttributes(LLVMContext &C, " << TargetPrefix |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 516 | << "Intrinsic::ID id) {\n"; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 517 | else |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 518 | OS << "AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id) {\n"; |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 519 | |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 520 | // Compute the maximum number of attribute arguments and the map |
| 521 | typedef std::map<const CodeGenIntrinsic*, unsigned, |
| 522 | AttributeComparator> UniqAttrMapTy; |
| 523 | UniqAttrMapTy UniqAttributes; |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 524 | unsigned maxArgAttrs = 0; |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 525 | unsigned AttrNum = 0; |
Chris Lattner | 97b0d99 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 527 | const CodeGenIntrinsic &intrinsic = Ints[i]; |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 528 | maxArgAttrs = |
| 529 | std::max(maxArgAttrs, unsigned(intrinsic.ArgumentAttributes.size())); |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 530 | unsigned &N = UniqAttributes[&intrinsic]; |
| 531 | if (N) continue; |
| 532 | assert(AttrNum < 256 && "Too many unique attributes for table!"); |
| 533 | N = ++AttrNum; |
Chris Lattner | 97b0d99 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 534 | } |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 535 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 536 | // Emit an array of AttributeList. Most intrinsics will have at least one |
Bill Wendling | 4d3491c | 2013-01-27 03:25:05 +0000 | [diff] [blame] | 537 | // entry, for the function itself (index ~1), which is usually nounwind. |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 538 | OS << " static const uint8_t IntrinsicsToAttributesMap[] = {\n"; |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 539 | |
| 540 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 541 | const CodeGenIntrinsic &intrinsic = Ints[i]; |
| 542 | |
| 543 | OS << " " << UniqAttributes[&intrinsic] << ", // " |
| 544 | << intrinsic.Name << "\n"; |
| 545 | } |
| 546 | OS << " };\n\n"; |
| 547 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 548 | OS << " AttributeList AS[" << maxArgAttrs + 1 << "];\n"; |
Chris Lattner | 2089cd0 | 2009-01-12 02:41:37 +0000 | [diff] [blame] | 549 | OS << " unsigned NumAttrs = 0;\n"; |
Craig Topper | 374f19c | 2012-04-13 06:14:57 +0000 | [diff] [blame] | 550 | OS << " if (id != 0) {\n"; |
| 551 | OS << " switch(IntrinsicsToAttributesMap[id - "; |
| 552 | if (TargetOnly) |
| 553 | OS << "Intrinsic::num_intrinsics"; |
| 554 | else |
| 555 | OS << "1"; |
| 556 | OS << "]) {\n"; |
| 557 | OS << " default: llvm_unreachable(\"Invalid attribute number\");\n"; |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 558 | for (UniqAttrMapTy::const_iterator I = UniqAttributes.begin(), |
| 559 | E = UniqAttributes.end(); I != E; ++I) { |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 560 | OS << " case " << I->second << ": {\n"; |
Chris Lattner | 9d0a877 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 561 | |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 562 | const CodeGenIntrinsic &intrinsic = *(I->first); |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 563 | |
| 564 | // Keep track of the number of attributes we're writing out. |
| 565 | unsigned numAttrs = 0; |
| 566 | |
| 567 | // The argument attributes are alreadys sorted by argument index. |
Bill Wendling | ed42e79 | 2012-10-10 06:13:42 +0000 | [diff] [blame] | 568 | unsigned ai = 0, ae = intrinsic.ArgumentAttributes.size(); |
| 569 | if (ae) { |
| 570 | while (ai != ae) { |
| 571 | unsigned argNo = intrinsic.ArgumentAttributes[ai].first; |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 572 | unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex |
Craig Topper | ccd651c | 2012-02-28 06:32:00 +0000 | [diff] [blame] | 573 | |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 574 | OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {"; |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 575 | bool addComma = false; |
Chris Lattner | 2089cd0 | 2009-01-12 02:41:37 +0000 | [diff] [blame] | 576 | |
Bill Wendling | ed42e79 | 2012-10-10 06:13:42 +0000 | [diff] [blame] | 577 | do { |
| 578 | switch (intrinsic.ArgumentAttributes[ai].second) { |
| 579 | case CodeGenIntrinsic::NoCapture: |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 580 | if (addComma) |
| 581 | OS << ","; |
| 582 | OS << "Attribute::NoCapture"; |
| 583 | addComma = true; |
Bill Wendling | ed42e79 | 2012-10-10 06:13:42 +0000 | [diff] [blame] | 584 | break; |
Hal Finkel | 47646c0 | 2016-07-11 01:28:42 +0000 | [diff] [blame] | 585 | case CodeGenIntrinsic::Returned: |
| 586 | if (addComma) |
| 587 | OS << ","; |
| 588 | OS << "Attribute::Returned"; |
| 589 | addComma = true; |
| 590 | break; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 591 | case CodeGenIntrinsic::ReadOnly: |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 592 | if (addComma) |
| 593 | OS << ","; |
| 594 | OS << "Attribute::ReadOnly"; |
| 595 | addComma = true; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 596 | break; |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 597 | case CodeGenIntrinsic::WriteOnly: |
| 598 | if (addComma) |
| 599 | OS << ","; |
| 600 | OS << "Attribute::WriteOnly"; |
| 601 | addComma = true; |
| 602 | break; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 603 | case CodeGenIntrinsic::ReadNone: |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 604 | if (addComma) |
| 605 | OS << ","; |
Eric Christopher | 10f5d60 | 2015-07-30 21:16:34 +0000 | [diff] [blame] | 606 | OS << "Attribute::ReadNone"; |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 607 | addComma = true; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 608 | break; |
Bill Wendling | ed42e79 | 2012-10-10 06:13:42 +0000 | [diff] [blame] | 609 | } |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 610 | |
Bill Wendling | ed42e79 | 2012-10-10 06:13:42 +0000 | [diff] [blame] | 611 | ++ai; |
| 612 | } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo); |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 613 | OS << "};\n"; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 614 | OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " |
Reid Kleckner | a0b45f4 | 2017-05-03 18:17:31 +0000 | [diff] [blame] | 615 | << attrIdx << ", AttrParam" << attrIdx << ");\n"; |
Bill Wendling | ed42e79 | 2012-10-10 06:13:42 +0000 | [diff] [blame] | 616 | } |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Igor Laevsky | 30143ae | 2015-08-13 17:40:04 +0000 | [diff] [blame] | 619 | if (!intrinsic.canThrow || |
| 620 | intrinsic.ModRef != CodeGenIntrinsic::ReadWriteMem || |
| 621 | intrinsic.isNoReturn || intrinsic.isNoDuplicate || |
Matt Arsenault | b19b57e | 2017-04-28 20:25:27 +0000 | [diff] [blame] | 622 | intrinsic.isConvergent || intrinsic.isSpeculatable) { |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 623 | OS << " const Attribute::AttrKind Atts[] = {"; |
| 624 | bool addComma = false; |
| 625 | if (!intrinsic.canThrow) { |
| 626 | OS << "Attribute::NoUnwind"; |
| 627 | addComma = true; |
| 628 | } |
| 629 | if (intrinsic.isNoReturn) { |
| 630 | if (addComma) |
| 631 | OS << ","; |
| 632 | OS << "Attribute::NoReturn"; |
| 633 | addComma = true; |
| 634 | } |
Eli Bendersky | 2281ef9 | 2014-03-18 23:51:07 +0000 | [diff] [blame] | 635 | if (intrinsic.isNoDuplicate) { |
| 636 | if (addComma) |
| 637 | OS << ","; |
| 638 | OS << "Attribute::NoDuplicate"; |
| 639 | addComma = true; |
| 640 | } |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 641 | if (intrinsic.isConvergent) { |
| 642 | if (addComma) |
| 643 | OS << ","; |
| 644 | OS << "Attribute::Convergent"; |
| 645 | addComma = true; |
| 646 | } |
Matt Arsenault | b19b57e | 2017-04-28 20:25:27 +0000 | [diff] [blame] | 647 | if (intrinsic.isSpeculatable) { |
| 648 | if (addComma) |
| 649 | OS << ","; |
| 650 | OS << "Attribute::Speculatable"; |
| 651 | addComma = true; |
| 652 | } |
Chris Lattner | ff9e08b | 2012-05-27 23:20:41 +0000 | [diff] [blame] | 653 | |
Igor Laevsky | 30143ae | 2015-08-13 17:40:04 +0000 | [diff] [blame] | 654 | switch (intrinsic.ModRef) { |
| 655 | case CodeGenIntrinsic::NoMem: |
| 656 | if (addComma) |
| 657 | OS << ","; |
| 658 | OS << "Attribute::ReadNone"; |
| 659 | break; |
| 660 | case CodeGenIntrinsic::ReadArgMem: |
| 661 | if (addComma) |
| 662 | OS << ","; |
| 663 | OS << "Attribute::ReadOnly,"; |
| 664 | OS << "Attribute::ArgMemOnly"; |
| 665 | break; |
| 666 | case CodeGenIntrinsic::ReadMem: |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 667 | if (addComma) |
| 668 | OS << ","; |
| 669 | OS << "Attribute::ReadOnly"; |
Chris Lattner | ff9e08b | 2012-05-27 23:20:41 +0000 | [diff] [blame] | 670 | break; |
Andrew Kaylor | 57d35bf | 2016-11-22 19:16:04 +0000 | [diff] [blame] | 671 | case CodeGenIntrinsic::ReadInaccessibleMem: |
| 672 | if (addComma) |
| 673 | OS << ","; |
| 674 | OS << "Attribute::ReadOnly,"; |
| 675 | OS << "Attribute::InaccessibleMemOnly"; |
| 676 | break; |
| 677 | case CodeGenIntrinsic::ReadInaccessibleMemOrArgMem: |
| 678 | if (addComma) |
| 679 | OS << ","; |
| 680 | OS << "Attribute::ReadOnly,"; |
| 681 | OS << "Attribute::InaccessibleMemOrArgMemOnly"; |
| 682 | break; |
Nicolai Haehnle | b48275f | 2016-04-19 21:58:33 +0000 | [diff] [blame] | 683 | case CodeGenIntrinsic::WriteArgMem: |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 684 | if (addComma) |
| 685 | OS << ","; |
| 686 | OS << "Attribute::WriteOnly,"; |
| 687 | OS << "Attribute::ArgMemOnly"; |
| 688 | break; |
| 689 | case CodeGenIntrinsic::WriteMem: |
| 690 | if (addComma) |
| 691 | OS << ","; |
| 692 | OS << "Attribute::WriteOnly"; |
| 693 | break; |
Andrew Kaylor | 57d35bf | 2016-11-22 19:16:04 +0000 | [diff] [blame] | 694 | case CodeGenIntrinsic::WriteInaccessibleMem: |
| 695 | if (addComma) |
| 696 | OS << ","; |
| 697 | OS << "Attribute::WriteOnly,"; |
| 698 | OS << "Attribute::InaccessibleMemOnly"; |
| 699 | break; |
| 700 | case CodeGenIntrinsic::WriteInaccessibleMemOrArgMem: |
| 701 | if (addComma) |
| 702 | OS << ","; |
| 703 | OS << "Attribute::WriteOnly,"; |
Matt Arsenault | 2e8be9d | 2017-12-03 00:03:01 +0000 | [diff] [blame] | 704 | OS << "Attribute::InaccessibleMemOrArgMemOnly"; |
Andrew Kaylor | 57d35bf | 2016-11-22 19:16:04 +0000 | [diff] [blame] | 705 | break; |
Igor Laevsky | 30143ae | 2015-08-13 17:40:04 +0000 | [diff] [blame] | 706 | case CodeGenIntrinsic::ReadWriteArgMem: |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 707 | if (addComma) |
| 708 | OS << ","; |
Igor Laevsky | 30143ae | 2015-08-13 17:40:04 +0000 | [diff] [blame] | 709 | OS << "Attribute::ArgMemOnly"; |
| 710 | break; |
Andrew Kaylor | 57d35bf | 2016-11-22 19:16:04 +0000 | [diff] [blame] | 711 | case CodeGenIntrinsic::ReadWriteInaccessibleMem: |
| 712 | if (addComma) |
| 713 | OS << ","; |
| 714 | OS << "Attribute::InaccessibleMemOnly"; |
| 715 | break; |
| 716 | case CodeGenIntrinsic::ReadWriteInaccessibleMemOrArgMem: |
| 717 | if (addComma) |
| 718 | OS << ","; |
| 719 | OS << "Attribute::InaccessibleMemOrArgMemOnly"; |
Adrian Prantl | 0e6694d | 2017-12-19 22:05:25 +0000 | [diff] [blame] | 720 | break; |
Igor Laevsky | 30143ae | 2015-08-13 17:40:04 +0000 | [diff] [blame] | 721 | case CodeGenIntrinsic::ReadWriteMem: |
Chris Lattner | ff9e08b | 2012-05-27 23:20:41 +0000 | [diff] [blame] | 722 | break; |
Chris Lattner | 2089cd0 | 2009-01-12 02:41:37 +0000 | [diff] [blame] | 723 | } |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 724 | OS << "};\n"; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 725 | OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, " |
| 726 | << "AttributeList::FunctionIndex, Atts);\n"; |
Chris Lattner | 2089cd0 | 2009-01-12 02:41:37 +0000 | [diff] [blame] | 727 | } |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 728 | |
| 729 | if (numAttrs) { |
Craig Topper | 374f19c | 2012-04-13 06:14:57 +0000 | [diff] [blame] | 730 | OS << " NumAttrs = " << numAttrs << ";\n"; |
| 731 | OS << " break;\n"; |
Owen Anderson | b88cc2f | 2013-11-16 00:20:01 +0000 | [diff] [blame] | 732 | OS << " }\n"; |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 733 | } else { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 734 | OS << " return AttributeList();\n"; |
Filip Pizlo | c95bd8d | 2014-02-20 23:57:31 +0000 | [diff] [blame] | 735 | OS << " }\n"; |
John McCall | 375dcc9 | 2011-05-28 06:31:34 +0000 | [diff] [blame] | 736 | } |
Chris Lattner | 9d0a877 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 737 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 738 | |
Craig Topper | 374f19c | 2012-04-13 06:14:57 +0000 | [diff] [blame] | 739 | OS << " }\n"; |
Chris Lattner | 9d0a877 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 740 | OS << " }\n"; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 741 | OS << " return AttributeList::get(C, makeArrayRef(AS, NumAttrs));\n"; |
Chris Lattner | 49b7ee1 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 742 | OS << "}\n"; |
Chris Lattner | 2089cd0 | 2009-01-12 02:41:37 +0000 | [diff] [blame] | 743 | OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; |
Chris Lattner | e3c2db3 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 744 | } |
Chris Lattner | fea17a9 | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 745 | |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 746 | void IntrinsicEmitter::EmitIntrinsicToBuiltinMap( |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 747 | const CodeGenIntrinsicTable &Ints, bool IsGCC, raw_ostream &OS) { |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 748 | StringRef CompilerName = (IsGCC ? "GCC" : "MS"); |
| 749 | typedef std::map<std::string, std::map<std::string, std::string>> BIMTy; |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 750 | BIMTy BuiltinMap; |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 751 | StringToOffsetTable Table; |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 752 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 753 | const std::string &BuiltinName = |
| 754 | IsGCC ? Ints[i].GCCBuiltinName : Ints[i].MSBuiltinName; |
| 755 | if (!BuiltinName.empty()) { |
Chris Lattner | 91678fc | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 756 | // Get the map for this target prefix. |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 757 | std::map<std::string, std::string> &BIM = |
| 758 | BuiltinMap[Ints[i].TargetPrefix]; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 759 | |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 760 | if (!BIM.insert(std::make_pair(BuiltinName, Ints[i].EnumName)).second) |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 761 | PrintFatalError("Intrinsic '" + Ints[i].TheDef->getName() + |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 762 | "': duplicate " + CompilerName + " builtin name!"); |
| 763 | Table.GetOrAddStringOffset(BuiltinName); |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 766 | |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 767 | OS << "// Get the LLVM intrinsic that corresponds to a builtin.\n"; |
| 768 | OS << "// This is used by the C front-end. The builtin name is passed\n"; |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 769 | OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; |
| 770 | OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 771 | OS << "#ifdef GET_LLVM_INTRINSIC_FOR_" << CompilerName << "_BUILTIN\n"; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 772 | |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 773 | if (TargetOnly) { |
| 774 | OS << "static " << TargetPrefix << "Intrinsic::ID " |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 775 | << "getIntrinsicFor" << CompilerName << "Builtin(const char " |
Mehdi Amini | f9ff04c | 2016-10-10 19:31:09 +0000 | [diff] [blame] | 776 | << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 777 | } else { |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 778 | OS << "Intrinsic::ID Intrinsic::getIntrinsicFor" << CompilerName |
| 779 | << "Builtin(const char " |
Mehdi Amini | f9ff04c | 2016-10-10 19:31:09 +0000 | [diff] [blame] | 780 | << "*TargetPrefixStr, StringRef BuiltinNameStr) {\n"; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 781 | } |
Aditya Nandakumar | 8a76f91 | 2017-04-19 19:14:20 +0000 | [diff] [blame] | 782 | |
| 783 | if (Table.Empty()) { |
| 784 | OS << " return "; |
| 785 | if (!TargetPrefix.empty()) |
| 786 | OS << "(" << TargetPrefix << "Intrinsic::ID)"; |
| 787 | OS << "Intrinsic::not_intrinsic;\n"; |
| 788 | OS << "}\n"; |
| 789 | OS << "#endif\n\n"; |
| 790 | return; |
| 791 | } |
| 792 | |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 793 | OS << " static const char BuiltinNames[] = {\n"; |
| 794 | Table.EmitCharArray(OS); |
| 795 | OS << " };\n\n"; |
| 796 | |
| 797 | OS << " struct BuiltinEntry {\n"; |
| 798 | OS << " Intrinsic::ID IntrinID;\n"; |
| 799 | OS << " unsigned StrTabOffset;\n"; |
| 800 | OS << " const char *getName() const {\n"; |
| 801 | OS << " return &BuiltinNames[StrTabOffset];\n"; |
| 802 | OS << " }\n"; |
Mehdi Amini | f9ff04c | 2016-10-10 19:31:09 +0000 | [diff] [blame] | 803 | OS << " bool operator<(StringRef RHS) const {\n"; |
| 804 | OS << " return strncmp(getName(), RHS.data(), RHS.size()) < 0;\n"; |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 805 | OS << " }\n"; |
| 806 | OS << " };\n"; |
| 807 | |
Chris Lattner | 497d13e | 2010-09-06 03:14:45 +0000 | [diff] [blame] | 808 | OS << " StringRef TargetPrefix(TargetPrefixStr);\n\n"; |
Andrew Trick | d4d1d9c | 2013-10-31 17:18:07 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 810 | // Note: this could emit significantly better code if we cared. |
| 811 | for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ |
Chris Lattner | 91678fc | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 812 | OS << " "; |
| 813 | if (!I->first.empty()) |
Chris Lattner | 497d13e | 2010-09-06 03:14:45 +0000 | [diff] [blame] | 814 | OS << "if (TargetPrefix == \"" << I->first << "\") "; |
Chris Lattner | 91678fc | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 815 | else |
| 816 | OS << "/* Target Independent Builtins */ "; |
| 817 | OS << "{\n"; |
| 818 | |
Chris Lattner | 91678fc | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 819 | // Emit the comparisons for this target prefix. |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 820 | OS << " static const BuiltinEntry " << I->first << "Names[] = {\n"; |
| 821 | for (const auto &P : I->second) { |
| 822 | OS << " {Intrinsic::" << P.second << ", " |
| 823 | << Table.GetOrAddStringOffset(P.first) << "}, // " << P.first << "\n"; |
| 824 | } |
| 825 | OS << " };\n"; |
| 826 | OS << " auto I = std::lower_bound(std::begin(" << I->first << "Names),\n"; |
| 827 | OS << " std::end(" << I->first << "Names),\n"; |
| 828 | OS << " BuiltinNameStr);\n"; |
| 829 | OS << " if (I != std::end(" << I->first << "Names) &&\n"; |
Mehdi Amini | f9ff04c | 2016-10-10 19:31:09 +0000 | [diff] [blame] | 830 | OS << " I->getName() == BuiltinNameStr)\n"; |
Reid Kleckner | 5b46371 | 2016-01-27 01:43:12 +0000 | [diff] [blame] | 831 | OS << " return I->IntrinID;\n"; |
Chris Lattner | 91678fc | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 832 | OS << " }\n"; |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 833 | } |
Chris Lattner | 497d13e | 2010-09-06 03:14:45 +0000 | [diff] [blame] | 834 | OS << " return "; |
| 835 | if (!TargetPrefix.empty()) |
| 836 | OS << "(" << TargetPrefix << "Intrinsic::ID)"; |
| 837 | OS << "Intrinsic::not_intrinsic;\n"; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 838 | OS << "}\n"; |
Chris Lattner | 402a573 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 839 | OS << "#endif\n\n"; |
| 840 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 841 | |
Richard Smith | 6bc9df3 | 2014-04-20 20:26:39 +0000 | [diff] [blame] | 842 | void llvm::EmitIntrinsics(RecordKeeper &RK, raw_ostream &OS, bool TargetOnly) { |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 843 | IntrinsicEmitter(RK, TargetOnly).run(OS); |
| 844 | } |