Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1 | //===- FastISelEmitter.cpp - Generate an instruction selector -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 10 | // This tablegen backend emits code for use by the "fast" instruction |
| 11 | // selection algorithm. See the comments at the top of |
| 12 | // lib/CodeGen/SelectionDAG/FastISel.cpp for background. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 13 | // |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 14 | // This file scans through the target's tablegen instruction-info files |
| 15 | // and extracts instructions with obvious-looking patterns, and it emits |
| 16 | // code to look up these instructions by type and operator. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 17 | // |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "FastISelEmitter.h" |
| 21 | #include "Record.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/Streams.h" |
| 24 | #include "llvm/ADT/VectorExtras.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 29 | /// InstructionMemo - This class holds additional information about an |
| 30 | /// instruction needed to emit code for it. |
| 31 | /// |
| 32 | struct InstructionMemo { |
| 33 | std::string Name; |
| 34 | const CodeGenRegisterClass *RC; |
| 35 | unsigned char SubRegNo; |
| 36 | std::vector<std::string>* PhysRegs; |
| 37 | }; |
| 38 | |
Dan Gohman | 04b7dfb | 2008-08-19 18:06:12 +0000 | [diff] [blame] | 39 | /// OperandsSignature - This class holds a description of a list of operand |
| 40 | /// types. It has utility methods for emitting text based on the operands. |
| 41 | /// |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 42 | struct OperandsSignature { |
| 43 | std::vector<std::string> Operands; |
| 44 | |
| 45 | bool operator<(const OperandsSignature &O) const { |
| 46 | return Operands < O.Operands; |
| 47 | } |
| 48 | |
| 49 | bool empty() const { return Operands.empty(); } |
| 50 | |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 51 | /// initialize - Examine the given pattern and initialize the contents |
| 52 | /// of the Operands array accordingly. Return true if all the operands |
| 53 | /// are supported, false otherwise. |
| 54 | /// |
| 55 | bool initialize(TreePatternNode *InstPatNode, |
| 56 | const CodeGenTarget &Target, |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 57 | MVT::SimpleValueType VT) { |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 58 | if (!InstPatNode->isLeaf() && |
| 59 | InstPatNode->getOperator()->getName() == "imm") { |
| 60 | Operands.push_back("i"); |
| 61 | return true; |
| 62 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 63 | if (!InstPatNode->isLeaf() && |
| 64 | InstPatNode->getOperator()->getName() == "fpimm") { |
| 65 | Operands.push_back("f"); |
| 66 | return true; |
| 67 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 68 | |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 69 | const CodeGenRegisterClass *DstRC = 0; |
| 70 | |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 71 | for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { |
| 72 | TreePatternNode *Op = InstPatNode->getChild(i); |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 73 | // For now, filter out any operand with a predicate. |
| 74 | if (!Op->getPredicateFn().empty()) |
| 75 | return false; |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 76 | // For now, filter out any operand with multiple values. |
| 77 | if (Op->getExtTypes().size() != 1) |
| 78 | return false; |
| 79 | // For now, all the operands must have the same type. |
| 80 | if (Op->getTypeNum(0) != VT) |
| 81 | return false; |
| 82 | if (!Op->isLeaf()) { |
| 83 | if (Op->getOperator()->getName() == "imm") { |
| 84 | Operands.push_back("i"); |
| 85 | return true; |
| 86 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 87 | if (Op->getOperator()->getName() == "fpimm") { |
| 88 | Operands.push_back("f"); |
| 89 | return true; |
| 90 | } |
Dan Gohman | 833ddf8 | 2008-08-27 16:18:22 +0000 | [diff] [blame] | 91 | // For now, ignore other non-leaf nodes. |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 94 | DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); |
| 95 | if (!OpDI) |
| 96 | return false; |
| 97 | Record *OpLeafRec = OpDI->getDef(); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 98 | // For now, the only other thing we accept is register operands. |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 99 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 100 | const CodeGenRegisterClass *RC = 0; |
| 101 | if (OpLeafRec->isSubClassOf("RegisterClass")) |
| 102 | RC = &Target.getRegisterClass(OpLeafRec); |
| 103 | else if (OpLeafRec->isSubClassOf("Register")) |
| 104 | RC = Target.getRegisterClassForRegister(OpLeafRec); |
| 105 | else |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 106 | return false; |
| 107 | // For now, require the register operands' register classes to all |
| 108 | // be the same. |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 109 | if (!RC) |
| 110 | return false; |
Dan Gohman | cf711aa | 2008-08-19 20:58:14 +0000 | [diff] [blame] | 111 | // For now, all the operands must have the same register class. |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 112 | if (DstRC) { |
| 113 | if (DstRC != RC) |
| 114 | return false; |
| 115 | } else |
| 116 | DstRC = RC; |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 117 | Operands.push_back("r"); |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 122 | void PrintParameters(std::ostream &OS) const { |
| 123 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 124 | if (Operands[i] == "r") { |
| 125 | OS << "unsigned Op" << i; |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 126 | } else if (Operands[i] == "i") { |
| 127 | OS << "uint64_t imm" << i; |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 128 | } else if (Operands[i] == "f") { |
| 129 | OS << "ConstantFP *f" << i; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 130 | } else { |
| 131 | assert("Unknown operand kind!"); |
| 132 | abort(); |
| 133 | } |
| 134 | if (i + 1 != e) |
| 135 | OS << ", "; |
| 136 | } |
| 137 | } |
| 138 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 139 | void PrintArguments(std::ostream &OS, |
| 140 | const std::vector<std::string>& PR) const { |
| 141 | assert(PR.size() == Operands.size()); |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 142 | bool PrintedArg = false; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 143 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 144 | if (PR[i] != "") |
| 145 | // Implicit physical register operand. |
| 146 | continue; |
| 147 | |
| 148 | if (PrintedArg) |
| 149 | OS << ", "; |
| 150 | if (Operands[i] == "r") { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 151 | OS << "Op" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 152 | PrintedArg = true; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 153 | } else if (Operands[i] == "i") { |
| 154 | OS << "imm" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 155 | PrintedArg = true; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 156 | } else if (Operands[i] == "f") { |
| 157 | OS << "f" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 158 | PrintedArg = true; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 159 | } else { |
| 160 | assert("Unknown operand kind!"); |
| 161 | abort(); |
| 162 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 166 | void PrintArguments(std::ostream &OS) const { |
| 167 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 168 | if (Operands[i] == "r") { |
| 169 | OS << "Op" << i; |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 170 | } else if (Operands[i] == "i") { |
| 171 | OS << "imm" << i; |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 172 | } else if (Operands[i] == "f") { |
| 173 | OS << "f" << i; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 174 | } else { |
| 175 | assert("Unknown operand kind!"); |
| 176 | abort(); |
| 177 | } |
| 178 | if (i + 1 != e) |
| 179 | OS << ", "; |
| 180 | } |
| 181 | } |
| 182 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 183 | |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 184 | void PrintManglingSuffix(std::ostream &OS, |
| 185 | const std::vector<std::string>& PR) const { |
| 186 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 187 | if (PR[i] != "") |
| 188 | // Implicit physical register operand. e.g. Instruction::Mul expect to |
| 189 | // select to a binary op. On x86, mul may take a single operand with |
| 190 | // the other operand being implicit. We must emit something that looks |
| 191 | // like a binary instruction except for the very inner FastEmitInst_* |
| 192 | // call. |
| 193 | continue; |
| 194 | OS << Operands[i]; |
| 195 | } |
| 196 | } |
| 197 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 198 | void PrintManglingSuffix(std::ostream &OS) const { |
| 199 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 200 | OS << Operands[i]; |
| 201 | } |
| 202 | } |
| 203 | }; |
| 204 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 205 | class FastISelMap { |
| 206 | typedef std::map<std::string, InstructionMemo> PredMap; |
| 207 | typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap; |
| 208 | typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap; |
| 209 | typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap; |
| 210 | typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap; |
| 211 | |
| 212 | OperandsOpcodeTypeRetPredMap SimplePatterns; |
| 213 | |
| 214 | std::string InstNS; |
| 215 | |
| 216 | public: |
| 217 | explicit FastISelMap(std::string InstNS); |
| 218 | |
| 219 | void CollectPatterns(CodeGenDAGPatterns &CGP); |
| 220 | void PrintClass(std::ostream &OS); |
| 221 | void PrintFunctionDefinitions(std::ostream &OS); |
| 222 | }; |
| 223 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) { |
| 227 | return CGP.getSDNodeInfo(Op).getEnumName(); |
| 228 | } |
| 229 | |
| 230 | static std::string getLegalCName(std::string OpName) { |
| 231 | std::string::size_type pos = OpName.find("::"); |
| 232 | if (pos != std::string::npos) |
| 233 | OpName.replace(pos, 2, "_"); |
| 234 | return OpName; |
| 235 | } |
| 236 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 237 | FastISelMap::FastISelMap(std::string instns) |
| 238 | : InstNS(instns) { |
| 239 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 240 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 241 | void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) { |
| 242 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
| 243 | |
| 244 | // Determine the target's namespace name. |
| 245 | InstNS = Target.getInstNamespace() + "::"; |
| 246 | assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 247 | |
Dan Gohman | 0bfb752 | 2008-08-22 00:28:15 +0000 | [diff] [blame] | 248 | // Scan through all the patterns and record the simple ones. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 249 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), |
| 250 | E = CGP.ptm_end(); I != E; ++I) { |
| 251 | const PatternToMatch &Pattern = *I; |
| 252 | |
| 253 | // For now, just look at Instructions, so that we don't have to worry |
| 254 | // about emitting multiple instructions for a pattern. |
| 255 | TreePatternNode *Dst = Pattern.getDstPattern(); |
| 256 | if (Dst->isLeaf()) continue; |
| 257 | Record *Op = Dst->getOperator(); |
| 258 | if (!Op->isSubClassOf("Instruction")) |
| 259 | continue; |
| 260 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName()); |
| 261 | if (II.OperandList.empty()) |
| 262 | continue; |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 263 | |
Evan Cheng | 34fc6ce | 2008-09-07 08:19:51 +0000 | [diff] [blame] | 264 | // For now, ignore multi-instruction patterns. |
| 265 | bool MultiInsts = false; |
| 266 | for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) { |
| 267 | TreePatternNode *ChildOp = Dst->getChild(i); |
| 268 | if (ChildOp->isLeaf()) |
| 269 | continue; |
| 270 | if (ChildOp->getOperator()->isSubClassOf("Instruction")) { |
| 271 | MultiInsts = true; |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | if (MultiInsts) |
| 276 | continue; |
| 277 | |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 278 | // For now, ignore instructions where the first operand is not an |
| 279 | // output register. |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 280 | const CodeGenRegisterClass *DstRC = 0; |
| 281 | unsigned SubRegNo = ~0; |
| 282 | if (Op->getName() != "EXTRACT_SUBREG") { |
| 283 | Record *Op0Rec = II.OperandList[0].Rec; |
| 284 | if (!Op0Rec->isSubClassOf("RegisterClass")) |
| 285 | continue; |
| 286 | DstRC = &Target.getRegisterClass(Op0Rec); |
| 287 | if (!DstRC) |
| 288 | continue; |
| 289 | } else { |
| 290 | SubRegNo = static_cast<IntInit*>( |
| 291 | Dst->getChild(1)->getLeafValue())->getValue(); |
| 292 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 293 | |
| 294 | // Inspect the pattern. |
| 295 | TreePatternNode *InstPatNode = Pattern.getSrcPattern(); |
| 296 | if (!InstPatNode) continue; |
| 297 | if (InstPatNode->isLeaf()) continue; |
| 298 | |
| 299 | Record *InstPatOp = InstPatNode->getOperator(); |
| 300 | std::string OpcodeName = getOpcodeName(InstPatOp, CGP); |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 301 | MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0); |
| 302 | MVT::SimpleValueType VT = RetVT; |
| 303 | if (InstPatNode->getNumChildren()) |
| 304 | VT = InstPatNode->getChild(0)->getTypeNum(0); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 305 | |
| 306 | // For now, filter out instructions which just set a register to |
Dan Gohman | f4137b5 | 2008-08-19 20:30:54 +0000 | [diff] [blame] | 307 | // an Operand or an immediate, like MOV32ri. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 308 | if (InstPatOp->isSubClassOf("Operand")) |
| 309 | continue; |
Dan Gohman | f4137b5 | 2008-08-19 20:30:54 +0000 | [diff] [blame] | 310 | |
| 311 | // For now, filter out any instructions with predicates. |
| 312 | if (!InstPatNode->getPredicateFn().empty()) |
| 313 | continue; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 314 | |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 315 | // Check all the operands. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 316 | OperandsSignature Operands; |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 317 | if (!Operands.initialize(InstPatNode, Target, VT)) |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 318 | continue; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 319 | |
| 320 | std::vector<std::string>* PhysRegInputs = new std::vector<std::string>(); |
| 321 | if (!InstPatNode->isLeaf() && |
| 322 | (InstPatNode->getOperator()->getName() == "imm" || |
| 323 | InstPatNode->getOperator()->getName() == "fpimmm")) |
| 324 | PhysRegInputs->push_back(""); |
| 325 | else if (!InstPatNode->isLeaf()) { |
| 326 | for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { |
| 327 | TreePatternNode *Op = InstPatNode->getChild(i); |
| 328 | if (!Op->isLeaf()) { |
| 329 | PhysRegInputs->push_back(""); |
| 330 | continue; |
| 331 | } |
| 332 | |
| 333 | DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); |
| 334 | Record *OpLeafRec = OpDI->getDef(); |
| 335 | std::string PhysReg; |
| 336 | if (OpLeafRec->isSubClassOf("Register")) { |
| 337 | PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \ |
| 338 | "Namespace")->getValue())->getValue(); |
| 339 | PhysReg += "::"; |
| 340 | |
| 341 | std::vector<CodeGenRegister> Regs = Target.getRegisters(); |
| 342 | for (unsigned i = 0; i < Regs.size(); ++i) { |
| 343 | if (Regs[i].TheDef == OpLeafRec) { |
| 344 | PhysReg += Regs[i].getName(); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | PhysRegInputs->push_back(PhysReg); |
| 351 | } |
| 352 | } else |
| 353 | PhysRegInputs->push_back(""); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 354 | |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 355 | // Get the predicate that guards this pattern. |
| 356 | std::string PredicateCheck = Pattern.getPredicateCheck(); |
| 357 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 358 | // Ok, we found a pattern that we can handle. Remember it. |
Dan Gohman | 520b50c | 2008-08-21 00:35:26 +0000 | [diff] [blame] | 359 | InstructionMemo Memo = { |
| 360 | Pattern.getDstPattern()->getOperator()->getName(), |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 361 | DstRC, |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 362 | SubRegNo, |
| 363 | PhysRegInputs |
Dan Gohman | 520b50c | 2008-08-21 00:35:26 +0000 | [diff] [blame] | 364 | }; |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 365 | assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) && |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 366 | "Duplicate pattern!"); |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 367 | SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 368 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 369 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 370 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 371 | void FastISelMap::PrintFunctionDefinitions(std::ostream &OS) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 372 | // Now emit code for all the patterns that we collected. |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 373 | for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(), |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 374 | OE = SimplePatterns.end(); OI != OE; ++OI) { |
| 375 | const OperandsSignature &Operands = OI->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 376 | const OpcodeTypeRetPredMap &OTM = OI->second; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 377 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 378 | for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 379 | I != E; ++I) { |
| 380 | const std::string &Opcode = I->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 381 | const TypeRetPredMap &TM = I->second; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 382 | |
| 383 | OS << "// FastEmit functions for " << Opcode << ".\n"; |
| 384 | OS << "\n"; |
| 385 | |
| 386 | // Emit one function for each opcode,type pair. |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 387 | for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 388 | TI != TE; ++TI) { |
| 389 | MVT::SimpleValueType VT = TI->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 390 | const RetPredMap &RM = TI->second; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 391 | if (RM.size() != 1) { |
| 392 | for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); |
| 393 | RI != RE; ++RI) { |
| 394 | MVT::SimpleValueType RetVT = RI->first; |
| 395 | const PredMap &PM = RI->second; |
| 396 | bool HasPred = false; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 397 | |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 398 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 399 | << getLegalCName(Opcode) |
| 400 | << "_" << getLegalCName(getName(VT)) |
| 401 | << "_" << getLegalCName(getName(RetVT)) << "_"; |
| 402 | Operands.PrintManglingSuffix(OS); |
| 403 | OS << "("; |
| 404 | Operands.PrintParameters(OS); |
| 405 | OS << ") {\n"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 406 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 407 | // Emit code for each possible instruction. There may be |
| 408 | // multiple if there are subtarget concerns. |
| 409 | for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); |
| 410 | PI != PE; ++PI) { |
| 411 | std::string PredicateCheck = PI->first; |
| 412 | const InstructionMemo &Memo = PI->second; |
| 413 | |
| 414 | if (PredicateCheck.empty()) { |
| 415 | assert(!HasPred && |
| 416 | "Multiple instructions match, at least one has " |
| 417 | "a predicate and at least one doesn't!"); |
| 418 | } else { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 419 | OS << " if (" + PredicateCheck + ") {\n"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 420 | OS << " "; |
| 421 | HasPred = true; |
| 422 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 423 | |
| 424 | for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { |
| 425 | if ((*Memo.PhysRegs)[i] != "") |
| 426 | OS << " TII.copyRegToReg(*MBB, MBB->end(), " |
| 427 | << (*Memo.PhysRegs)[i] << ", Op" << i << ", " |
| 428 | << "TM.getRegisterInfo()->getPhysicalRegisterRegClass(" |
| 429 | << (*Memo.PhysRegs)[i] << "), " |
| 430 | << "MRI.getRegClass(Op" << i << "));\n"; |
| 431 | } |
| 432 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 433 | OS << " return FastEmitInst_"; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 434 | if (Memo.SubRegNo == (unsigned char)~0) { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 435 | Operands.PrintManglingSuffix(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 436 | OS << "(" << InstNS << Memo.Name << ", "; |
| 437 | OS << InstNS << Memo.RC->getName() << "RegisterClass"; |
| 438 | if (!Operands.empty()) |
| 439 | OS << ", "; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 440 | Operands.PrintArguments(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 441 | OS << ");\n"; |
| 442 | } else { |
| 443 | OS << "extractsubreg(Op0, "; |
| 444 | OS << (unsigned)Memo.SubRegNo; |
| 445 | OS << ");\n"; |
| 446 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 447 | |
| 448 | if (HasPred) |
Evan Cheng | d07b46e | 2008-09-07 08:23:06 +0000 | [diff] [blame] | 449 | OS << " }\n"; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 450 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 451 | } |
| 452 | // Return 0 if none of the predicates were satisfied. |
| 453 | if (HasPred) |
| 454 | OS << " return 0;\n"; |
| 455 | OS << "}\n"; |
| 456 | OS << "\n"; |
| 457 | } |
| 458 | |
| 459 | // Emit one function for the type that demultiplexes on return type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 460 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 461 | << getLegalCName(Opcode) << "_" |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 462 | << getLegalCName(getName(VT)) << "_"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 463 | Operands.PrintManglingSuffix(OS); |
| 464 | OS << "(MVT::SimpleValueType RetVT"; |
| 465 | if (!Operands.empty()) |
| 466 | OS << ", "; |
| 467 | Operands.PrintParameters(OS); |
| 468 | OS << ") {\nswitch (RetVT) {\n"; |
| 469 | for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); |
| 470 | RI != RE; ++RI) { |
| 471 | MVT::SimpleValueType RetVT = RI->first; |
| 472 | OS << " case " << getName(RetVT) << ": return FastEmit_" |
| 473 | << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT)) |
| 474 | << "_" << getLegalCName(getName(RetVT)) << "_"; |
| 475 | Operands.PrintManglingSuffix(OS); |
| 476 | OS << "("; |
| 477 | Operands.PrintArguments(OS); |
| 478 | OS << ");\n"; |
| 479 | } |
| 480 | OS << " default: return 0;\n}\n}\n\n"; |
| 481 | |
| 482 | } else { |
| 483 | // Non-variadic return type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 484 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 485 | << getLegalCName(Opcode) << "_" |
| 486 | << getLegalCName(getName(VT)) << "_"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 487 | Operands.PrintManglingSuffix(OS); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 488 | OS << "(MVT::SimpleValueType RetVT"; |
| 489 | if (!Operands.empty()) |
| 490 | OS << ", "; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 491 | Operands.PrintParameters(OS); |
| 492 | OS << ") {\n"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 493 | |
Owen Anderson | 70647e8 | 2008-08-26 18:50:00 +0000 | [diff] [blame] | 494 | OS << " if (RetVT != " << getName(RM.begin()->first) |
| 495 | << ")\n return 0;\n"; |
| 496 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 497 | const PredMap &PM = RM.begin()->second; |
| 498 | bool HasPred = false; |
| 499 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 500 | // Emit code for each possible instruction. There may be |
| 501 | // multiple if there are subtarget concerns. |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 502 | for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; |
| 503 | ++PI) { |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 504 | std::string PredicateCheck = PI->first; |
| 505 | const InstructionMemo &Memo = PI->second; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 506 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 507 | if (PredicateCheck.empty()) { |
| 508 | assert(!HasPred && |
| 509 | "Multiple instructions match, at least one has " |
| 510 | "a predicate and at least one doesn't!"); |
| 511 | } else { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 512 | OS << " if (" + PredicateCheck + ") {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 513 | OS << " "; |
| 514 | HasPred = true; |
| 515 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 516 | |
| 517 | for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { |
| 518 | if ((*Memo.PhysRegs)[i] != "") |
| 519 | OS << " TII.copyRegToReg(*MBB, MBB->end(), " |
| 520 | << (*Memo.PhysRegs)[i] << ", Op" << i << ", " |
| 521 | << "TM.getRegisterInfo()->getPhysicalRegisterRegClass(" |
| 522 | << (*Memo.PhysRegs)[i] << "), " |
| 523 | << "MRI.getRegClass(Op" << i << "));\n"; |
| 524 | } |
| 525 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 526 | OS << " return FastEmitInst_"; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 527 | |
| 528 | if (Memo.SubRegNo == (unsigned char)~0) { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 529 | Operands.PrintManglingSuffix(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 530 | OS << "(" << InstNS << Memo.Name << ", "; |
| 531 | OS << InstNS << Memo.RC->getName() << "RegisterClass"; |
| 532 | if (!Operands.empty()) |
| 533 | OS << ", "; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 534 | Operands.PrintArguments(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 535 | OS << ");\n"; |
| 536 | } else { |
| 537 | OS << "extractsubreg(Op0, "; |
| 538 | OS << (unsigned)Memo.SubRegNo; |
| 539 | OS << ");\n"; |
| 540 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 541 | |
| 542 | if (HasPred) |
| 543 | OS << " }\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 544 | } |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 545 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 546 | // Return 0 if none of the predicates were satisfied. |
| 547 | if (HasPred) |
| 548 | OS << " return 0;\n"; |
| 549 | OS << "}\n"; |
| 550 | OS << "\n"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 551 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // Emit one function for the opcode that demultiplexes based on the type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 555 | OS << "unsigned FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 556 | << getLegalCName(Opcode) << "_"; |
| 557 | Operands.PrintManglingSuffix(OS); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 558 | OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 559 | if (!Operands.empty()) |
| 560 | OS << ", "; |
| 561 | Operands.PrintParameters(OS); |
| 562 | OS << ") {\n"; |
| 563 | OS << " switch (VT) {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 564 | for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 565 | TI != TE; ++TI) { |
| 566 | MVT::SimpleValueType VT = TI->first; |
| 567 | std::string TypeName = getName(VT); |
| 568 | OS << " case " << TypeName << ": return FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 569 | << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_"; |
| 570 | Operands.PrintManglingSuffix(OS); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 571 | OS << "(RetVT"; |
| 572 | if (!Operands.empty()) |
| 573 | OS << ", "; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 574 | Operands.PrintArguments(OS); |
| 575 | OS << ");\n"; |
| 576 | } |
| 577 | OS << " default: return 0;\n"; |
| 578 | OS << " }\n"; |
| 579 | OS << "}\n"; |
| 580 | OS << "\n"; |
| 581 | } |
| 582 | |
Dan Gohman | 0bfb752 | 2008-08-22 00:28:15 +0000 | [diff] [blame] | 583 | OS << "// Top-level FastEmit function.\n"; |
| 584 | OS << "\n"; |
| 585 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 586 | // Emit one function for the operand signature that demultiplexes based |
| 587 | // on opcode and type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 588 | OS << "unsigned FastEmit_"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 589 | Operands.PrintManglingSuffix(OS); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 590 | OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 591 | if (!Operands.empty()) |
| 592 | OS << ", "; |
| 593 | Operands.PrintParameters(OS); |
| 594 | OS << ") {\n"; |
| 595 | OS << " switch (Opcode) {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 596 | for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 597 | I != E; ++I) { |
| 598 | const std::string &Opcode = I->first; |
| 599 | |
| 600 | OS << " case " << Opcode << ": return FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 601 | << getLegalCName(Opcode) << "_"; |
| 602 | Operands.PrintManglingSuffix(OS); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 603 | OS << "(VT, RetVT"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 604 | if (!Operands.empty()) |
| 605 | OS << ", "; |
| 606 | Operands.PrintArguments(OS); |
| 607 | OS << ");\n"; |
| 608 | } |
| 609 | OS << " default: return 0;\n"; |
| 610 | OS << " }\n"; |
| 611 | OS << "}\n"; |
| 612 | OS << "\n"; |
| 613 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void FastISelEmitter::run(std::ostream &OS) { |
| 617 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
| 618 | |
| 619 | // Determine the target's namespace name. |
| 620 | std::string InstNS = Target.getInstNamespace() + "::"; |
| 621 | assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); |
| 622 | |
| 623 | EmitSourceFileHeader("\"Fast\" Instruction Selector for the " + |
| 624 | Target.getName() + " target", OS); |
| 625 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 626 | FastISelMap F(InstNS); |
| 627 | F.CollectPatterns(CGP); |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 628 | F.PrintFunctionDefinitions(OS); |
Dan Gohman | c7f72de | 2008-08-21 00:19:05 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | FastISelEmitter::FastISelEmitter(RecordKeeper &R) |
| 632 | : Records(R), |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 633 | CGP(R) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 634 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 635 | |