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