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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits information about intrinsic functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "IntrinsicEmitter.h" |
| 15 | #include "Record.h" |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Jeff Cohen | 71c3bc3 | 2006-03-15 02:51:05 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 21 | // IntrinsicEmitter Implementation |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | void IntrinsicEmitter::run(std::ostream &OS) { |
| 25 | EmitSourceFileHeader("Intrinsic Function Source Fragment", OS); |
| 26 | |
| 27 | std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records); |
| 28 | |
| 29 | // Emit the enum information. |
| 30 | EmitEnumInfo(Ints, OS); |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 31 | |
| 32 | // Emit the intrinsic ID -> name table. |
| 33 | EmitIntrinsicToNameTable(Ints, OS); |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 34 | |
| 35 | // Emit the function name recognizer. |
| 36 | EmitFnNameRecognizer(Ints, OS); |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 37 | |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 38 | // Emit the intrinsic verifier. |
| 39 | EmitVerifier(Ints, OS); |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 40 | |
| 41 | // Emit mod/ref info for each function. |
| 42 | EmitModRefInfo(Ints, OS); |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 44 | // Emit table of non-memory accessing intrinsics. |
| 45 | EmitNoMemoryInfo(Ints, OS); |
| 46 | |
| 47 | // Emit side effect info for each intrinsic. |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 48 | EmitSideEffectInfo(Ints, OS); |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 49 | |
| 50 | // Emit a list of intrinsics with corresponding GCC builtins. |
| 51 | EmitGCCBuiltinList(Ints, OS); |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 52 | |
| 53 | // Emit code to translate GCC builtins into LLVM intrinsics. |
| 54 | EmitIntrinsicToGCCBuiltinMap(Ints, OS); |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints, |
| 58 | std::ostream &OS) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 59 | OS << "// Enum values for Intrinsics.h\n"; |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 60 | OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; |
| 61 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 62 | OS << " " << Ints[i].EnumName; |
| 63 | OS << ((i != e-1) ? ", " : " "); |
| 64 | OS << std::string(40-Ints[i].EnumName.size(), ' ') |
| 65 | << "// " << Ints[i].Name << "\n"; |
| 66 | } |
| 67 | OS << "#endif\n\n"; |
| 68 | } |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 69 | |
| 70 | void IntrinsicEmitter:: |
| 71 | EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, |
| 72 | std::ostream &OS) { |
| 73 | // Build a function name -> intrinsic name mapping. |
| 74 | std::map<std::string, std::string> IntMapping; |
| 75 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 76 | IntMapping[Ints[i].Name] = Ints[i].EnumName; |
| 77 | |
| 78 | OS << "// Function name -> enum value recognizer code.\n"; |
| 79 | OS << "#ifdef GET_FUNCTION_RECOGNIZER\n"; |
| 80 | OS << " switch (Name[5]) {\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 81 | OS << " default: break;\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 82 | // Emit the intrinsics in sorted order. |
| 83 | char LastChar = 0; |
| 84 | for (std::map<std::string, std::string>::iterator I = IntMapping.begin(), |
| 85 | E = IntMapping.end(); I != E; ++I) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 86 | if (I->first[5] != LastChar) { |
| 87 | LastChar = I->first[5]; |
| 88 | OS << " case '" << LastChar << "':\n"; |
| 89 | } |
| 90 | |
| 91 | OS << " if (Name == \"" << I->first << "\") return Intrinsic::" |
| 92 | << I->second << ";\n"; |
| 93 | } |
| 94 | OS << " }\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 95 | OS << " // The 'llvm.' namespace is reserved!\n"; |
| 96 | OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n"; |
| 97 | OS << "#endif\n\n"; |
| 98 | } |
| 99 | |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 100 | void IntrinsicEmitter:: |
| 101 | EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints, |
| 102 | std::ostream &OS) { |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 103 | OS << "// Intrinsic ID to name table\n"; |
| 104 | OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; |
| 105 | OS << " // Note that entry #0 is the invalid intrinsic!\n"; |
Evan Cheng | f065a6f | 2006-03-28 22:25:56 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 107 | OS << " \"" << Ints[i].Name << "\",\n"; |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 108 | OS << "#endif\n\n"; |
| 109 | } |
| 110 | |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 111 | static void EmitTypeVerify(std::ostream &OS, Record *ArgType) { |
Jim Laskey | ba4cc09 | 2007-02-06 18:02:54 +0000 | [diff] [blame^] | 112 | if (ArgType->getValueAsString("TypeVal") == "...") { |
| 113 | OS << "-2, "; |
| 114 | return; |
| 115 | } |
| 116 | |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 117 | OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", "; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 118 | // If this is an integer type, check the width is correct. |
| 119 | if (ArgType->isSubClassOf("LLVMIntegerType")) |
| 120 | OS << ArgType->getValueAsInt("Width") << ", "; |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 121 | |
| 122 | // If this is a packed type, check that the subtype and size are correct. |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 123 | else if (ArgType->isSubClassOf("LLVMPackedType")) { |
| 124 | EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy")); |
| 125 | OS << ArgType->getValueAsInt("NumElts") << ", "; |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 129 | /// RecordListComparator - Provide a determinstic comparator for lists of |
| 130 | /// records. |
| 131 | namespace { |
| 132 | struct RecordListComparator { |
| 133 | bool operator()(const std::vector<Record*> &LHS, |
| 134 | const std::vector<Record*> &RHS) const { |
| 135 | unsigned i = 0; |
| 136 | do { |
| 137 | if (i == RHS.size()) return false; // RHS is shorter than LHS. |
| 138 | if (LHS[i] != RHS[i]) |
| 139 | return LHS[i]->getName() < RHS[i]->getName(); |
| 140 | } while (++i != LHS.size()); |
| 141 | |
| 142 | return i != RHS.size(); |
| 143 | } |
| 144 | }; |
| 145 | } |
| 146 | |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 147 | void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, |
| 148 | std::ostream &OS) { |
| 149 | OS << "// Verifier::visitIntrinsicFunctionCall code.\n"; |
| 150 | OS << "#ifdef GET_INTRINSIC_VERIFIER\n"; |
| 151 | OS << " switch (ID) {\n"; |
| 152 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 153 | |
| 154 | // This checking can emit a lot of very common code. To reduce the amount of |
| 155 | // code that we emit, batch up cases that have identical types. This avoids |
| 156 | // problems where GCC can run out of memory compiling Verifier.cpp. |
| 157 | typedef std::map<std::vector<Record*>, std::vector<unsigned>, |
| 158 | RecordListComparator> MapTy; |
| 159 | MapTy UniqueArgInfos; |
| 160 | |
| 161 | // Compute the unique argument type info. |
| 162 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 163 | UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i); |
| 164 | |
| 165 | // Loop through the array, emitting one comparison for each batch. |
| 166 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 167 | E = UniqueArgInfos.end(); I != E; ++I) { |
| 168 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 169 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 170 | << Ints[I->second[i]].Name << "\n"; |
| 171 | } |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 172 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 173 | const std::vector<Record*> &ArgTypes = I->first; |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 174 | OS << " VerifyIntrinsicPrototype(IF, "; |
| 175 | for (unsigned j = 0; j != ArgTypes.size(); ++j) |
| 176 | EmitTypeVerify(OS, ArgTypes[j]); |
| 177 | OS << "-1);\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 178 | OS << " break;\n"; |
| 179 | } |
| 180 | OS << " }\n"; |
| 181 | OS << "#endif\n\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 184 | void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints, |
| 185 | std::ostream &OS) { |
| 186 | OS << "// BasicAliasAnalysis code.\n"; |
| 187 | OS << "#ifdef GET_MODREF_BEHAVIOR\n"; |
| 188 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 189 | switch (Ints[i].ModRef) { |
| 190 | default: break; |
| 191 | case CodeGenIntrinsic::NoMem: |
Chris Lattner | 90aa839 | 2006-10-04 21:52:35 +0000 | [diff] [blame] | 192 | OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n"; |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 193 | break; |
| 194 | case CodeGenIntrinsic::ReadArgMem: |
| 195 | case CodeGenIntrinsic::ReadMem: |
Chris Lattner | 90aa839 | 2006-10-04 21:52:35 +0000 | [diff] [blame] | 196 | OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n"; |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | } |
| 200 | OS << "#endif\n\n"; |
| 201 | } |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 202 | |
| 203 | void IntrinsicEmitter:: |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 204 | EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) { |
| 205 | OS << "// SelectionDAGIsel code.\n"; |
| 206 | OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n"; |
| 207 | OS << " switch (IntrinsicID) {\n"; |
| 208 | OS << " default: break;\n"; |
| 209 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 210 | switch (Ints[i].ModRef) { |
| 211 | default: break; |
| 212 | case CodeGenIntrinsic::NoMem: |
| 213 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | OS << " return true; // These intrinsics have no side effects.\n"; |
| 218 | OS << " }\n"; |
| 219 | OS << "#endif\n\n"; |
| 220 | } |
| 221 | |
| 222 | void IntrinsicEmitter:: |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 223 | EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){ |
Chris Lattner | 5348e39 | 2006-04-02 03:35:30 +0000 | [diff] [blame] | 224 | OS << "// Return true if doesn't access or only reads memory.\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 225 | OS << "#ifdef GET_SIDE_EFFECT_INFO\n"; |
Chris Lattner | 5348e39 | 2006-04-02 03:35:30 +0000 | [diff] [blame] | 226 | OS << " switch (IntrinsicID) {\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 227 | OS << " default: break;\n"; |
| 228 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 229 | switch (Ints[i].ModRef) { |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 230 | default: break; |
| 231 | case CodeGenIntrinsic::NoMem: |
| 232 | case CodeGenIntrinsic::ReadArgMem: |
| 233 | case CodeGenIntrinsic::ReadMem: |
| 234 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 235 | break; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | OS << " return true; // These intrinsics have no side effects.\n"; |
| 239 | OS << " }\n"; |
| 240 | OS << "#endif\n\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 241 | } |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 242 | |
| 243 | void IntrinsicEmitter:: |
| 244 | EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){ |
| 245 | OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n"; |
| 246 | OS << "#ifdef GET_GCC_BUILTIN_NAME\n"; |
| 247 | OS << " switch (F->getIntrinsicID()) {\n"; |
| 248 | OS << " default: BuiltinName = \"\"; break;\n"; |
| 249 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 250 | if (!Ints[i].GCCBuiltinName.empty()) { |
| 251 | OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \"" |
| 252 | << Ints[i].GCCBuiltinName << "\"; break;\n"; |
| 253 | } |
| 254 | } |
| 255 | OS << " }\n"; |
| 256 | OS << "#endif\n\n"; |
Reid Spencer | 767a25b | 2006-03-14 05:59:52 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 258 | |
| 259 | void IntrinsicEmitter:: |
| 260 | EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, |
| 261 | std::ostream &OS) { |
| 262 | typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy; |
| 263 | BIMTy BuiltinMap; |
| 264 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 265 | if (!Ints[i].GCCBuiltinName.empty()) { |
| 266 | std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName, |
| 267 | Ints[i].TargetPrefix); |
| 268 | if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second) |
| 269 | throw "Intrinsic '" + Ints[i].TheDef->getName() + |
| 270 | "': duplicate GCC builtin name!"; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n"; |
| 275 | OS << "// This is used by the C front-end. The GCC builtin name is passed\n"; |
| 276 | OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; |
| 277 | OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; |
| 278 | OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n"; |
| 279 | OS << " if (0);\n"; |
| 280 | // Note: this could emit significantly better code if we cared. |
| 281 | for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ |
| 282 | OS << " else if ("; |
| 283 | if (!I->first.second.empty()) { |
| 284 | // Emit this as a strcmp, so it can be constant folded by the FE. |
| 285 | OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n" |
| 286 | << " "; |
| 287 | } |
| 288 | OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n"; |
Chris Lattner | ad45b00 | 2006-03-15 02:05:38 +0000 | [diff] [blame] | 289 | OS << " IntrinsicID = Intrinsic::" << I->second << ";\n"; |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 290 | } |
| 291 | OS << " else\n"; |
| 292 | OS << " IntrinsicID = Intrinsic::not_intrinsic;\n"; |
| 293 | OS << "#endif\n\n"; |
| 294 | } |