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 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 41 | // Emit the intrinsic declaration generator. |
| 42 | EmitGenerator(Ints, OS); |
| 43 | |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 44 | // Emit mod/ref info for each function. |
| 45 | EmitModRefInfo(Ints, OS); |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 47 | // Emit table of non-memory accessing intrinsics. |
| 48 | EmitNoMemoryInfo(Ints, OS); |
| 49 | |
| 50 | // Emit side effect info for each intrinsic. |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 51 | EmitSideEffectInfo(Ints, OS); |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 52 | |
| 53 | // Emit a list of intrinsics with corresponding GCC builtins. |
| 54 | EmitGCCBuiltinList(Ints, OS); |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 55 | |
| 56 | // Emit code to translate GCC builtins into LLVM intrinsics. |
| 57 | EmitIntrinsicToGCCBuiltinMap(Ints, OS); |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints, |
| 61 | std::ostream &OS) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 62 | OS << "// Enum values for Intrinsics.h\n"; |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 63 | OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; |
| 64 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 65 | OS << " " << Ints[i].EnumName; |
| 66 | OS << ((i != e-1) ? ", " : " "); |
| 67 | OS << std::string(40-Ints[i].EnumName.size(), ' ') |
| 68 | << "// " << Ints[i].Name << "\n"; |
| 69 | } |
| 70 | OS << "#endif\n\n"; |
| 71 | } |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 72 | |
| 73 | void IntrinsicEmitter:: |
| 74 | EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, |
| 75 | std::ostream &OS) { |
| 76 | // Build a function name -> intrinsic name mapping. |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 77 | std::map<std::string, unsigned> IntMapping; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 78 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 79 | IntMapping[Ints[i].Name] = i; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 80 | |
| 81 | OS << "// Function name -> enum value recognizer code.\n"; |
| 82 | OS << "#ifdef GET_FUNCTION_RECOGNIZER\n"; |
| 83 | OS << " switch (Name[5]) {\n"; |
Chris Lattner | 3b51580 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 84 | OS << " default:\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 85 | // Emit the intrinsics in sorted order. |
| 86 | char LastChar = 0; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 87 | for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(), |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 88 | E = IntMapping.end(); I != E; ++I) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 89 | if (I->first[5] != LastChar) { |
| 90 | LastChar = I->first[5]; |
Chris Lattner | 3b51580 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 91 | OS << " break;\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 92 | OS << " case '" << LastChar << "':\n"; |
| 93 | } |
| 94 | |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 95 | // For overloaded intrinsics, only the prefix needs to match |
| 96 | if (Ints[I->second].isOverloaded) |
| 97 | OS << " if (Len >= " << I->first.size() |
| 98 | << " && !memcmp(Name, \"" << I->first << "\", " << I->first.size() |
| 99 | << ")) return Intrinsic::" << Ints[I->second].EnumName << ";\n"; |
| 100 | else |
| 101 | OS << " if (Len == " << I->first.size() |
| 102 | << " && !memcmp(Name, \"" << I->first << "\", Len)) return Intrinsic::" |
| 103 | << Ints[I->second].EnumName << ";\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 104 | } |
| 105 | OS << " }\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 106 | OS << "#endif\n\n"; |
| 107 | } |
| 108 | |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 109 | void IntrinsicEmitter:: |
| 110 | EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints, |
| 111 | std::ostream &OS) { |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 112 | OS << "// Intrinsic ID to name table\n"; |
| 113 | OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; |
| 114 | OS << " // Note that entry #0 is the invalid intrinsic!\n"; |
Evan Cheng | f065a6f | 2006-03-28 22:25:56 +0000 | [diff] [blame] | 115 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 116 | OS << " \"" << Ints[i].Name << "\",\n"; |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 117 | OS << "#endif\n\n"; |
| 118 | } |
| 119 | |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 120 | static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) { |
| 121 | if (ArgType->getValueAsString("TypeVal") == "...") return true; |
Jim Laskey | ba4cc09 | 2007-02-06 18:02:54 +0000 | [diff] [blame] | 122 | |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 123 | OS << "(int)" << ArgType->getValueAsString("TypeVal") << ", "; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 124 | // If this is an integer type, check the width is correct. |
| 125 | if (ArgType->isSubClassOf("LLVMIntegerType")) |
| 126 | OS << ArgType->getValueAsInt("Width") << ", "; |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 127 | |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 128 | // If this is a vector type, check that the subtype and size are correct. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 129 | else if (ArgType->isSubClassOf("LLVMVectorType")) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 130 | EmitTypeVerify(OS, ArgType->getValueAsDef("ElTy")); |
| 131 | OS << ArgType->getValueAsInt("NumElts") << ", "; |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 132 | } |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 133 | |
| 134 | return false; |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 137 | static void EmitTypeGenerate(std::ostream &OS, Record *ArgType, unsigned ArgNo){ |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 138 | if (ArgType->isSubClassOf("LLVMIntegerType")) { |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 139 | unsigned BitWidth = ArgType->getValueAsInt("Width"); |
| 140 | if (BitWidth == 0) |
| 141 | OS << "Tys[" << ArgNo << "]"; |
| 142 | else |
| 143 | OS << "IntegerType::get(" << BitWidth << ")"; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 144 | } else if (ArgType->isSubClassOf("LLVMVectorType")) { |
| 145 | OS << "VectorType::get("; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 146 | EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 147 | OS << ", " << ArgType->getValueAsInt("NumElts") << ")"; |
| 148 | } else if (ArgType->isSubClassOf("LLVMPointerType")) { |
| 149 | OS << "PointerType::get("; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 150 | EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 151 | OS << ")"; |
| 152 | } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) { |
| 153 | OS << "StructType::get(std::vector<const Type *>())"; |
| 154 | } else { |
| 155 | OS << "Type::getPrimitiveType("; |
| 156 | OS << ArgType->getValueAsString("TypeVal") << ")"; |
| 157 | } |
| 158 | } |
| 159 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 160 | /// RecordListComparator - Provide a determinstic comparator for lists of |
| 161 | /// records. |
| 162 | namespace { |
| 163 | struct RecordListComparator { |
| 164 | bool operator()(const std::vector<Record*> &LHS, |
| 165 | const std::vector<Record*> &RHS) const { |
| 166 | unsigned i = 0; |
| 167 | do { |
| 168 | if (i == RHS.size()) return false; // RHS is shorter than LHS. |
| 169 | if (LHS[i] != RHS[i]) |
| 170 | return LHS[i]->getName() < RHS[i]->getName(); |
| 171 | } while (++i != LHS.size()); |
| 172 | |
| 173 | return i != RHS.size(); |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 178 | void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, |
| 179 | std::ostream &OS) { |
| 180 | OS << "// Verifier::visitIntrinsicFunctionCall code.\n"; |
| 181 | OS << "#ifdef GET_INTRINSIC_VERIFIER\n"; |
| 182 | OS << " switch (ID) {\n"; |
| 183 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 184 | |
| 185 | // This checking can emit a lot of very common code. To reduce the amount of |
| 186 | // code that we emit, batch up cases that have identical types. This avoids |
| 187 | // problems where GCC can run out of memory compiling Verifier.cpp. |
| 188 | typedef std::map<std::vector<Record*>, std::vector<unsigned>, |
| 189 | RecordListComparator> MapTy; |
| 190 | MapTy UniqueArgInfos; |
| 191 | |
| 192 | // Compute the unique argument type info. |
| 193 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 194 | UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i); |
| 195 | |
| 196 | // Loop through the array, emitting one comparison for each batch. |
| 197 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 198 | E = UniqueArgInfos.end(); I != E; ++I) { |
| 199 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 200 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 201 | << Ints[I->second[i]].Name << "\n"; |
| 202 | } |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 203 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 204 | const std::vector<Record*> &ArgTypes = I->first; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 205 | OS << " VerifyIntrinsicPrototype(ID, IF, "; |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 206 | bool VarArg = false; |
| 207 | for (unsigned j = 0; j != ArgTypes.size(); ++j) { |
| 208 | VarArg = EmitTypeVerify(OS, ArgTypes[j]); |
| 209 | if (VarArg) { |
| 210 | if ((j+1) != ArgTypes.size()) |
| 211 | throw "Var arg type not last argument"; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | OS << (VarArg ? "-2);\n" : "-1);\n"); |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 217 | OS << " break;\n"; |
| 218 | } |
| 219 | OS << " }\n"; |
| 220 | OS << "#endif\n\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 223 | void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, |
| 224 | std::ostream &OS) { |
| 225 | OS << "// Code for generating Intrinsic function declarations.\n"; |
| 226 | OS << "#ifdef GET_INTRINSIC_GENERATOR\n"; |
| 227 | OS << " switch (id) {\n"; |
| 228 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
| 229 | |
| 230 | // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical |
| 231 | // types. |
| 232 | typedef std::map<std::vector<Record*>, std::vector<unsigned>, |
| 233 | RecordListComparator> MapTy; |
| 234 | MapTy UniqueArgInfos; |
| 235 | |
| 236 | // Compute the unique argument type info. |
| 237 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 238 | UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i); |
| 239 | |
| 240 | // Loop through the array, emitting one generator for each batch. |
| 241 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 242 | E = UniqueArgInfos.end(); I != E; ++I) { |
| 243 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 244 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 245 | << Ints[I->second[i]].Name << "\n"; |
| 246 | } |
| 247 | |
| 248 | const std::vector<Record*> &ArgTypes = I->first; |
| 249 | unsigned N = ArgTypes.size(); |
| 250 | |
| 251 | if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") { |
| 252 | OS << " IsVarArg = true;\n"; |
| 253 | --N; |
| 254 | } |
| 255 | |
| 256 | OS << " ResultTy = "; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 257 | EmitTypeGenerate(OS, ArgTypes[0], 0); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 258 | OS << ";\n"; |
| 259 | |
| 260 | for (unsigned j = 1; j != N; ++j) { |
| 261 | OS << " ArgTys.push_back("; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 262 | EmitTypeGenerate(OS, ArgTypes[j], j); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 263 | OS << ");\n"; |
| 264 | } |
| 265 | |
| 266 | OS << " break;\n"; |
| 267 | } |
| 268 | OS << " }\n"; |
| 269 | OS << "#endif\n\n"; |
| 270 | } |
| 271 | |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 272 | void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints, |
| 273 | std::ostream &OS) { |
| 274 | OS << "// BasicAliasAnalysis code.\n"; |
| 275 | OS << "#ifdef GET_MODREF_BEHAVIOR\n"; |
| 276 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 277 | switch (Ints[i].ModRef) { |
| 278 | default: break; |
| 279 | case CodeGenIntrinsic::NoMem: |
Chris Lattner | 90aa839 | 2006-10-04 21:52:35 +0000 | [diff] [blame] | 280 | OS << " NoMemoryTable->push_back(\"" << Ints[i].Name << "\");\n"; |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 281 | break; |
| 282 | case CodeGenIntrinsic::ReadArgMem: |
| 283 | case CodeGenIntrinsic::ReadMem: |
Chris Lattner | 90aa839 | 2006-10-04 21:52:35 +0000 | [diff] [blame] | 284 | OS << " OnlyReadsMemoryTable->push_back(\"" << Ints[i].Name << "\");\n"; |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | } |
| 288 | OS << "#endif\n\n"; |
| 289 | } |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 290 | |
| 291 | void IntrinsicEmitter:: |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 292 | EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) { |
| 293 | OS << "// SelectionDAGIsel code.\n"; |
| 294 | OS << "#ifdef GET_NO_MEMORY_INTRINSICS\n"; |
| 295 | OS << " switch (IntrinsicID) {\n"; |
| 296 | OS << " default: break;\n"; |
| 297 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 298 | switch (Ints[i].ModRef) { |
| 299 | default: break; |
| 300 | case CodeGenIntrinsic::NoMem: |
| 301 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | OS << " return true; // These intrinsics have no side effects.\n"; |
| 306 | OS << " }\n"; |
| 307 | OS << "#endif\n\n"; |
| 308 | } |
| 309 | |
| 310 | void IntrinsicEmitter:: |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 311 | EmitSideEffectInfo(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){ |
Chris Lattner | 5348e39 | 2006-04-02 03:35:30 +0000 | [diff] [blame] | 312 | OS << "// Return true if doesn't access or only reads memory.\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 313 | OS << "#ifdef GET_SIDE_EFFECT_INFO\n"; |
Chris Lattner | 5348e39 | 2006-04-02 03:35:30 +0000 | [diff] [blame] | 314 | OS << " switch (IntrinsicID) {\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 315 | OS << " default: break;\n"; |
| 316 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 317 | switch (Ints[i].ModRef) { |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 318 | default: break; |
| 319 | case CodeGenIntrinsic::NoMem: |
| 320 | case CodeGenIntrinsic::ReadArgMem: |
| 321 | case CodeGenIntrinsic::ReadMem: |
| 322 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 323 | break; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | OS << " return true; // These intrinsics have no side effects.\n"; |
| 327 | OS << " }\n"; |
| 328 | OS << "#endif\n\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 330 | |
| 331 | void IntrinsicEmitter:: |
| 332 | EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){ |
| 333 | OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n"; |
| 334 | OS << "#ifdef GET_GCC_BUILTIN_NAME\n"; |
| 335 | OS << " switch (F->getIntrinsicID()) {\n"; |
| 336 | OS << " default: BuiltinName = \"\"; break;\n"; |
| 337 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 338 | if (!Ints[i].GCCBuiltinName.empty()) { |
| 339 | OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \"" |
| 340 | << Ints[i].GCCBuiltinName << "\"; break;\n"; |
| 341 | } |
| 342 | } |
| 343 | OS << " }\n"; |
| 344 | OS << "#endif\n\n"; |
Reid Spencer | 767a25b | 2006-03-14 05:59:52 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 346 | |
| 347 | void IntrinsicEmitter:: |
| 348 | EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, |
| 349 | std::ostream &OS) { |
| 350 | typedef std::map<std::pair<std::string, std::string>, std::string> BIMTy; |
| 351 | BIMTy BuiltinMap; |
| 352 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 353 | if (!Ints[i].GCCBuiltinName.empty()) { |
| 354 | std::pair<std::string, std::string> Key(Ints[i].GCCBuiltinName, |
| 355 | Ints[i].TargetPrefix); |
| 356 | if (!BuiltinMap.insert(std::make_pair(Key, Ints[i].EnumName)).second) |
| 357 | throw "Intrinsic '" + Ints[i].TheDef->getName() + |
| 358 | "': duplicate GCC builtin name!"; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n"; |
| 363 | OS << "// This is used by the C front-end. The GCC builtin name is passed\n"; |
| 364 | OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; |
| 365 | OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; |
| 366 | OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n"; |
| 367 | OS << " if (0);\n"; |
| 368 | // Note: this could emit significantly better code if we cared. |
| 369 | for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ |
| 370 | OS << " else if ("; |
| 371 | if (!I->first.second.empty()) { |
| 372 | // Emit this as a strcmp, so it can be constant folded by the FE. |
| 373 | OS << "!strcmp(TargetPrefix, \"" << I->first.second << "\") &&\n" |
| 374 | << " "; |
| 375 | } |
| 376 | OS << "!strcmp(BuiltinName, \"" << I->first.first << "\"))\n"; |
Chris Lattner | ad45b00 | 2006-03-15 02:05:38 +0000 | [diff] [blame] | 377 | OS << " IntrinsicID = Intrinsic::" << I->second << ";\n"; |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 378 | } |
| 379 | OS << " else\n"; |
| 380 | OS << " IntrinsicID = Intrinsic::not_intrinsic;\n"; |
| 381 | OS << "#endif\n\n"; |
| 382 | } |