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