blob: 201a2cc5fef5c1ffef930fea5d8db059367858e7 [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001//===- 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 Gohman5ec9efd2008-09-30 20:48:29 +000010// 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 Gohmanb0cf29c2008-08-13 20:19:35 +000013//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000014// 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 Gohmanb0cf29c2008-08-13 20:19:35 +000017//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000018//===----------------------------------------------------------------------===//
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"
25using namespace llvm;
26
27namespace {
28
Owen Anderson667d8f72008-08-29 17:45:56 +000029/// InstructionMemo - This class holds additional information about an
30/// instruction needed to emit code for it.
31///
32struct InstructionMemo {
33 std::string Name;
34 const CodeGenRegisterClass *RC;
35 unsigned char SubRegNo;
36 std::vector<std::string>* PhysRegs;
37};
38
Dan Gohman04b7dfb2008-08-19 18:06:12 +000039/// 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 Gohmanb0cf29c2008-08-13 20:19:35 +000042struct 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 Gohmand1d2ee82008-08-19 20:56:30 +000051 /// 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 Andersonabb1f162008-08-26 01:22:59 +000057 MVT::SimpleValueType VT) {
Owen Anderson6d0c25e2008-08-25 20:20:32 +000058 if (!InstPatNode->isLeaf() &&
59 InstPatNode->getOperator()->getName() == "imm") {
60 Operands.push_back("i");
61 return true;
62 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000063 if (!InstPatNode->isLeaf() &&
64 InstPatNode->getOperator()->getName() == "fpimm") {
65 Operands.push_back("f");
66 return true;
67 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000068
Owen Andersonabb1f162008-08-26 01:22:59 +000069 const CodeGenRegisterClass *DstRC = 0;
70
Dan Gohmand1d2ee82008-08-19 20:56:30 +000071 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
72 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000073 // For now, filter out any operand with a predicate.
Dan Gohman0540e172008-10-15 06:17:21 +000074 if (!Op->getPredicateFns().empty())
Dan Gohmand1d2ee82008-08-19 20:56:30 +000075 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000076 // 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 Gohman10df0fa2008-08-27 01:09:54 +000087 if (Op->getOperator()->getName() == "fpimm") {
88 Operands.push_back("f");
89 return true;
90 }
Dan Gohman833ddf82008-08-27 16:18:22 +000091 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000092 return false;
93 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000094 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
95 if (!OpDI)
96 return false;
97 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +000098 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +000099
Owen Anderson667d8f72008-08-29 17:45:56 +0000100 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 Gohmand1d2ee82008-08-19 20:56:30 +0000106 return false;
107 // For now, require the register operands' register classes to all
108 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000109 if (!RC)
110 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000111 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000112 if (DstRC) {
113 if (DstRC != RC)
114 return false;
115 } else
116 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000117 Operands.push_back("r");
118 }
119 return true;
120 }
121
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000122 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 Gohmand5fe57d2008-08-21 01:41:07 +0000126 } else if (Operands[i] == "i") {
127 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000128 } else if (Operands[i] == "f") {
129 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000130 } else {
131 assert("Unknown operand kind!");
132 abort();
133 }
134 if (i + 1 != e)
135 OS << ", ";
136 }
137 }
138
Owen Anderson667d8f72008-08-29 17:45:56 +0000139 void PrintArguments(std::ostream &OS,
140 const std::vector<std::string>& PR) const {
141 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000142 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000143 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000144 if (PR[i] != "")
145 // Implicit physical register operand.
146 continue;
147
148 if (PrintedArg)
149 OS << ", ";
150 if (Operands[i] == "r") {
Owen Anderson667d8f72008-08-29 17:45:56 +0000151 OS << "Op" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000152 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000153 } else if (Operands[i] == "i") {
154 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000155 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000156 } else if (Operands[i] == "f") {
157 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000158 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000159 } else {
160 assert("Unknown operand kind!");
161 abort();
162 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000163 }
164 }
165
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000166 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 Gohmand5fe57d2008-08-21 01:41:07 +0000170 } else if (Operands[i] == "i") {
171 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000172 } else if (Operands[i] == "f") {
173 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000174 } else {
175 assert("Unknown operand kind!");
176 abort();
177 }
178 if (i + 1 != e)
179 OS << ", ";
180 }
181 }
182
Owen Anderson667d8f72008-08-29 17:45:56 +0000183
Evan Cheng98d2d072008-09-08 08:39:33 +0000184 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 Gohmanb0cf29c2008-08-13 20:19:35 +0000198 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 Gohman72d63af2008-08-26 21:21:20 +0000205class 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
216public:
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 Gohmanb0cf29c2008-08-13 20:19:35 +0000224}
225
226static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
227 return CGP.getSDNodeInfo(Op).getEnumName();
228}
229
230static 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 Gohman72d63af2008-08-26 21:21:20 +0000237FastISelMap::FastISelMap(std::string instns)
238 : InstNS(instns) {
239}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000240
Dan Gohman72d63af2008-08-26 21:21:20 +0000241void 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 Gohmanb0cf29c2008-08-13 20:19:35 +0000247
Dan Gohman0bfb7522008-08-22 00:28:15 +0000248 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000249 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 Gohman379cad42008-08-19 20:36:33 +0000263
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000264 // 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 Gohman379cad42008-08-19 20:36:33 +0000278 // For now, ignore instructions where the first operand is not an
279 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000280 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 Gohmanb0cf29c2008-08-13 20:19:35 +0000293
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 Andersonabb1f162008-08-26 01:22:59 +0000301 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
302 MVT::SimpleValueType VT = RetVT;
303 if (InstPatNode->getNumChildren())
304 VT = InstPatNode->getChild(0)->getTypeNum(0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000305
306 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000307 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000308 if (InstPatOp->isSubClassOf("Operand"))
309 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000310
311 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000312 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000313 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000314
Dan Gohman379cad42008-08-19 20:36:33 +0000315 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000316 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000317 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000318 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000319
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 Gohmanb0cf29c2008-08-13 20:19:35 +0000354
Dan Gohman22bb3112008-08-22 00:20:26 +0000355 // Get the predicate that guards this pattern.
356 std::string PredicateCheck = Pattern.getPredicateCheck();
357
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000358 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000359 InstructionMemo Memo = {
360 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000361 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000362 SubRegNo,
363 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000364 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000365 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000366 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000367 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000368 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000369}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000370
Dan Gohman72d63af2008-08-26 21:21:20 +0000371void FastISelMap::PrintFunctionDefinitions(std::ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000372 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000373 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000374 OE = SimplePatterns.end(); OI != OE; ++OI) {
375 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000376 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000377
Owen Anderson7b2e5792008-08-25 23:43:09 +0000378 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000379 I != E; ++I) {
380 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000381 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000382
383 OS << "// FastEmit functions for " << Opcode << ".\n";
384 OS << "\n";
385
386 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000387 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000388 TI != TE; ++TI) {
389 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000390 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000391 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 Gohman22bb3112008-08-22 00:20:26 +0000397
Evan Chengc3f44b02008-09-03 00:03:49 +0000398 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000399 << getLegalCName(Opcode)
400 << "_" << getLegalCName(getName(VT))
401 << "_" << getLegalCName(getName(RetVT)) << "_";
402 Operands.PrintManglingSuffix(OS);
403 OS << "(";
404 Operands.PrintParameters(OS);
405 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000406
Owen Anderson71669e52008-08-26 00:42:26 +0000407 // 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 Anderson667d8f72008-08-29 17:45:56 +0000419 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000420 OS << " ";
421 HasPred = true;
422 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000423
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 Anderson71669e52008-08-26 00:42:26 +0000433 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000434 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000435 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000436 OS << "(" << InstNS << Memo.Name << ", ";
437 OS << InstNS << Memo.RC->getName() << "RegisterClass";
438 if (!Operands.empty())
439 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000440 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000441 OS << ");\n";
442 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000443 OS << "extractsubreg(" << getName(RetVT);
444 OS << ", Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000445 OS << (unsigned)Memo.SubRegNo;
446 OS << ");\n";
447 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000448
449 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000450 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000451
Owen Anderson71669e52008-08-26 00:42:26 +0000452 }
453 // Return 0 if none of the predicates were satisfied.
454 if (HasPred)
455 OS << " return 0;\n";
456 OS << "}\n";
457 OS << "\n";
458 }
459
460 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000461 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000462 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000463 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000464 Operands.PrintManglingSuffix(OS);
465 OS << "(MVT::SimpleValueType RetVT";
466 if (!Operands.empty())
467 OS << ", ";
468 Operands.PrintParameters(OS);
469 OS << ") {\nswitch (RetVT) {\n";
470 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
471 RI != RE; ++RI) {
472 MVT::SimpleValueType RetVT = RI->first;
473 OS << " case " << getName(RetVT) << ": return FastEmit_"
474 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
475 << "_" << getLegalCName(getName(RetVT)) << "_";
476 Operands.PrintManglingSuffix(OS);
477 OS << "(";
478 Operands.PrintArguments(OS);
479 OS << ");\n";
480 }
481 OS << " default: return 0;\n}\n}\n\n";
482
483 } else {
484 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000485 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000486 << getLegalCName(Opcode) << "_"
487 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000488 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000489 OS << "(MVT::SimpleValueType RetVT";
490 if (!Operands.empty())
491 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000492 Operands.PrintParameters(OS);
493 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000494
Owen Anderson70647e82008-08-26 18:50:00 +0000495 OS << " if (RetVT != " << getName(RM.begin()->first)
496 << ")\n return 0;\n";
497
Owen Anderson71669e52008-08-26 00:42:26 +0000498 const PredMap &PM = RM.begin()->second;
499 bool HasPred = false;
500
Owen Anderson7b2e5792008-08-25 23:43:09 +0000501 // Emit code for each possible instruction. There may be
502 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000503 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
504 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000505 std::string PredicateCheck = PI->first;
506 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000507
Owen Anderson7b2e5792008-08-25 23:43:09 +0000508 if (PredicateCheck.empty()) {
509 assert(!HasPred &&
510 "Multiple instructions match, at least one has "
511 "a predicate and at least one doesn't!");
512 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000513 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000514 OS << " ";
515 HasPred = true;
516 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000517
518 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
519 if ((*Memo.PhysRegs)[i] != "")
520 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
521 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
522 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
523 << (*Memo.PhysRegs)[i] << "), "
524 << "MRI.getRegClass(Op" << i << "));\n";
525 }
526
Owen Anderson7b2e5792008-08-25 23:43:09 +0000527 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000528
529 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000530 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000531 OS << "(" << InstNS << Memo.Name << ", ";
532 OS << InstNS << Memo.RC->getName() << "RegisterClass";
533 if (!Operands.empty())
534 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000535 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000536 OS << ");\n";
537 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000538 OS << "extractsubreg(RetVT, Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000539 OS << (unsigned)Memo.SubRegNo;
540 OS << ");\n";
541 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000542
543 if (HasPred)
544 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000545 }
Owen Anderson71669e52008-08-26 00:42:26 +0000546
Owen Anderson7b2e5792008-08-25 23:43:09 +0000547 // Return 0 if none of the predicates were satisfied.
548 if (HasPred)
549 OS << " return 0;\n";
550 OS << "}\n";
551 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000552 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000553 }
554
555 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000556 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000557 << getLegalCName(Opcode) << "_";
558 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000559 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560 if (!Operands.empty())
561 OS << ", ";
562 Operands.PrintParameters(OS);
563 OS << ") {\n";
564 OS << " switch (VT) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000565 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000566 TI != TE; ++TI) {
567 MVT::SimpleValueType VT = TI->first;
568 std::string TypeName = getName(VT);
569 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000570 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
571 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000572 OS << "(RetVT";
573 if (!Operands.empty())
574 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000575 Operands.PrintArguments(OS);
576 OS << ");\n";
577 }
578 OS << " default: return 0;\n";
579 OS << " }\n";
580 OS << "}\n";
581 OS << "\n";
582 }
583
Dan Gohman0bfb7522008-08-22 00:28:15 +0000584 OS << "// Top-level FastEmit function.\n";
585 OS << "\n";
586
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000587 // Emit one function for the operand signature that demultiplexes based
588 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000589 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000591 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592 if (!Operands.empty())
593 OS << ", ";
594 Operands.PrintParameters(OS);
595 OS << ") {\n";
596 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000597 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000598 I != E; ++I) {
599 const std::string &Opcode = I->first;
600
601 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000602 << getLegalCName(Opcode) << "_";
603 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000604 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000605 if (!Operands.empty())
606 OS << ", ";
607 Operands.PrintArguments(OS);
608 OS << ");\n";
609 }
610 OS << " default: return 0;\n";
611 OS << " }\n";
612 OS << "}\n";
613 OS << "\n";
614 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000615}
616
617void FastISelEmitter::run(std::ostream &OS) {
618 const CodeGenTarget &Target = CGP.getTargetInfo();
619
620 // Determine the target's namespace name.
621 std::string InstNS = Target.getInstNamespace() + "::";
622 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
623
624 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
625 Target.getName() + " target", OS);
626
Dan Gohman72d63af2008-08-26 21:21:20 +0000627 FastISelMap F(InstNS);
628 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000629 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000630}
631
632FastISelEmitter::FastISelEmitter(RecordKeeper &R)
633 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000634 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000635}
Dan Gohman72d63af2008-08-26 21:21:20 +0000636