blob: 0aa9dfc49d4a3204efc6eb9cb083188840b21b57 [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//
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"
43using namespace llvm;
44
45namespace {
46
Owen Anderson667d8f72008-08-29 17:45:56 +000047/// InstructionMemo - This class holds additional information about an
48/// instruction needed to emit code for it.
49///
50struct InstructionMemo {
51 std::string Name;
52 const CodeGenRegisterClass *RC;
53 unsigned char SubRegNo;
54 std::vector<std::string>* PhysRegs;
55};
56
Dan Gohman04b7dfb2008-08-19 18:06:12 +000057/// OperandsSignature - This class holds a description of a list of operand
58/// types. It has utility methods for emitting text based on the operands.
59///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000060struct OperandsSignature {
61 std::vector<std::string> Operands;
62
63 bool operator<(const OperandsSignature &O) const {
64 return Operands < O.Operands;
65 }
66
67 bool empty() const { return Operands.empty(); }
68
Dan Gohmand1d2ee82008-08-19 20:56:30 +000069 /// initialize - Examine the given pattern and initialize the contents
70 /// of the Operands array accordingly. Return true if all the operands
71 /// are supported, false otherwise.
72 ///
73 bool initialize(TreePatternNode *InstPatNode,
74 const CodeGenTarget &Target,
Owen Andersonabb1f162008-08-26 01:22:59 +000075 MVT::SimpleValueType VT) {
Owen Anderson6d0c25e2008-08-25 20:20:32 +000076 if (!InstPatNode->isLeaf() &&
77 InstPatNode->getOperator()->getName() == "imm") {
78 Operands.push_back("i");
79 return true;
80 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000081 if (!InstPatNode->isLeaf() &&
82 InstPatNode->getOperator()->getName() == "fpimm") {
83 Operands.push_back("f");
84 return true;
85 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000086
Owen Andersonabb1f162008-08-26 01:22:59 +000087 const CodeGenRegisterClass *DstRC = 0;
88
Dan Gohmand1d2ee82008-08-19 20:56:30 +000089 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
90 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000091 // For now, filter out any operand with a predicate.
92 if (!Op->getPredicateFn().empty())
93 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000094 // For now, filter out any operand with multiple values.
95 if (Op->getExtTypes().size() != 1)
96 return false;
97 // For now, all the operands must have the same type.
98 if (Op->getTypeNum(0) != VT)
99 return false;
100 if (!Op->isLeaf()) {
101 if (Op->getOperator()->getName() == "imm") {
102 Operands.push_back("i");
103 return true;
104 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000105 if (Op->getOperator()->getName() == "fpimm") {
106 Operands.push_back("f");
107 return true;
108 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000109 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000110 return false;
111 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000112 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
113 if (!OpDI)
114 return false;
115 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000116 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +0000117
Owen Anderson667d8f72008-08-29 17:45:56 +0000118 const CodeGenRegisterClass *RC = 0;
119 if (OpLeafRec->isSubClassOf("RegisterClass"))
120 RC = &Target.getRegisterClass(OpLeafRec);
121 else if (OpLeafRec->isSubClassOf("Register"))
122 RC = Target.getRegisterClassForRegister(OpLeafRec);
123 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000124 return false;
125 // For now, require the register operands' register classes to all
126 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000127 if (!RC)
128 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000129 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000130 if (DstRC) {
131 if (DstRC != RC)
132 return false;
133 } else
134 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000135 Operands.push_back("r");
136 }
137 return true;
138 }
139
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000140 void PrintParameters(std::ostream &OS) const {
141 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
142 if (Operands[i] == "r") {
143 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000144 } else if (Operands[i] == "i") {
145 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000146 } else if (Operands[i] == "f") {
147 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000148 } else {
149 assert("Unknown operand kind!");
150 abort();
151 }
152 if (i + 1 != e)
153 OS << ", ";
154 }
155 }
156
Owen Anderson667d8f72008-08-29 17:45:56 +0000157 void PrintArguments(std::ostream &OS,
158 const std::vector<std::string>& PR) const {
159 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000160 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000161 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000162 if (PR[i] != "")
163 // Implicit physical register operand.
164 continue;
165
166 if (PrintedArg)
167 OS << ", ";
168 if (Operands[i] == "r") {
Owen Anderson667d8f72008-08-29 17:45:56 +0000169 OS << "Op" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000170 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000171 } else if (Operands[i] == "i") {
172 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000173 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000174 } else if (Operands[i] == "f") {
175 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000176 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000177 } else {
178 assert("Unknown operand kind!");
179 abort();
180 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000181 }
182 }
183
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000184 void PrintArguments(std::ostream &OS) const {
185 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
186 if (Operands[i] == "r") {
187 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000188 } else if (Operands[i] == "i") {
189 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000190 } else if (Operands[i] == "f") {
191 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000192 } else {
193 assert("Unknown operand kind!");
194 abort();
195 }
196 if (i + 1 != e)
197 OS << ", ";
198 }
199 }
200
Owen Anderson667d8f72008-08-29 17:45:56 +0000201
Evan Cheng98d2d072008-09-08 08:39:33 +0000202 void PrintManglingSuffix(std::ostream &OS,
203 const std::vector<std::string>& PR) const {
204 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
205 if (PR[i] != "")
206 // Implicit physical register operand. e.g. Instruction::Mul expect to
207 // select to a binary op. On x86, mul may take a single operand with
208 // the other operand being implicit. We must emit something that looks
209 // like a binary instruction except for the very inner FastEmitInst_*
210 // call.
211 continue;
212 OS << Operands[i];
213 }
214 }
215
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000216 void PrintManglingSuffix(std::ostream &OS) const {
217 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
218 OS << Operands[i];
219 }
220 }
221};
222
Dan Gohman72d63af2008-08-26 21:21:20 +0000223class FastISelMap {
224 typedef std::map<std::string, InstructionMemo> PredMap;
225 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
226 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
227 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
228 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
229
230 OperandsOpcodeTypeRetPredMap SimplePatterns;
231
232 std::string InstNS;
233
234public:
235 explicit FastISelMap(std::string InstNS);
236
237 void CollectPatterns(CodeGenDAGPatterns &CGP);
238 void PrintClass(std::ostream &OS);
239 void PrintFunctionDefinitions(std::ostream &OS);
240};
241
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000242}
243
244static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
245 return CGP.getSDNodeInfo(Op).getEnumName();
246}
247
248static std::string getLegalCName(std::string OpName) {
249 std::string::size_type pos = OpName.find("::");
250 if (pos != std::string::npos)
251 OpName.replace(pos, 2, "_");
252 return OpName;
253}
254
Dan Gohman72d63af2008-08-26 21:21:20 +0000255FastISelMap::FastISelMap(std::string instns)
256 : InstNS(instns) {
257}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000258
Dan Gohman72d63af2008-08-26 21:21:20 +0000259void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
260 const CodeGenTarget &Target = CGP.getTargetInfo();
261
262 // Determine the target's namespace name.
263 InstNS = Target.getInstNamespace() + "::";
264 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000265
Dan Gohman0bfb7522008-08-22 00:28:15 +0000266 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000267 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
268 E = CGP.ptm_end(); I != E; ++I) {
269 const PatternToMatch &Pattern = *I;
270
271 // For now, just look at Instructions, so that we don't have to worry
272 // about emitting multiple instructions for a pattern.
273 TreePatternNode *Dst = Pattern.getDstPattern();
274 if (Dst->isLeaf()) continue;
275 Record *Op = Dst->getOperator();
276 if (!Op->isSubClassOf("Instruction"))
277 continue;
278 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
279 if (II.OperandList.empty())
280 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000281
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000282 // For now, ignore multi-instruction patterns.
283 bool MultiInsts = false;
284 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
285 TreePatternNode *ChildOp = Dst->getChild(i);
286 if (ChildOp->isLeaf())
287 continue;
288 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
289 MultiInsts = true;
290 break;
291 }
292 }
293 if (MultiInsts)
294 continue;
295
Dan Gohman379cad42008-08-19 20:36:33 +0000296 // For now, ignore instructions where the first operand is not an
297 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000298 const CodeGenRegisterClass *DstRC = 0;
299 unsigned SubRegNo = ~0;
300 if (Op->getName() != "EXTRACT_SUBREG") {
301 Record *Op0Rec = II.OperandList[0].Rec;
302 if (!Op0Rec->isSubClassOf("RegisterClass"))
303 continue;
304 DstRC = &Target.getRegisterClass(Op0Rec);
305 if (!DstRC)
306 continue;
307 } else {
308 SubRegNo = static_cast<IntInit*>(
309 Dst->getChild(1)->getLeafValue())->getValue();
310 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000311
312 // Inspect the pattern.
313 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
314 if (!InstPatNode) continue;
315 if (InstPatNode->isLeaf()) continue;
316
317 Record *InstPatOp = InstPatNode->getOperator();
318 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Owen Andersonabb1f162008-08-26 01:22:59 +0000319 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
320 MVT::SimpleValueType VT = RetVT;
321 if (InstPatNode->getNumChildren())
322 VT = InstPatNode->getChild(0)->getTypeNum(0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000323
324 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000325 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000326 if (InstPatOp->isSubClassOf("Operand"))
327 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000328
329 // For now, filter out any instructions with predicates.
330 if (!InstPatNode->getPredicateFn().empty())
331 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000332
Dan Gohman379cad42008-08-19 20:36:33 +0000333 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000334 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000335 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000336 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000337
338 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
339 if (!InstPatNode->isLeaf() &&
340 (InstPatNode->getOperator()->getName() == "imm" ||
341 InstPatNode->getOperator()->getName() == "fpimmm"))
342 PhysRegInputs->push_back("");
343 else if (!InstPatNode->isLeaf()) {
344 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
345 TreePatternNode *Op = InstPatNode->getChild(i);
346 if (!Op->isLeaf()) {
347 PhysRegInputs->push_back("");
348 continue;
349 }
350
351 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
352 Record *OpLeafRec = OpDI->getDef();
353 std::string PhysReg;
354 if (OpLeafRec->isSubClassOf("Register")) {
355 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
356 "Namespace")->getValue())->getValue();
357 PhysReg += "::";
358
359 std::vector<CodeGenRegister> Regs = Target.getRegisters();
360 for (unsigned i = 0; i < Regs.size(); ++i) {
361 if (Regs[i].TheDef == OpLeafRec) {
362 PhysReg += Regs[i].getName();
363 break;
364 }
365 }
366 }
367
368 PhysRegInputs->push_back(PhysReg);
369 }
370 } else
371 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000372
Dan Gohman22bb3112008-08-22 00:20:26 +0000373 // Get the predicate that guards this pattern.
374 std::string PredicateCheck = Pattern.getPredicateCheck();
375
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000376 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000377 InstructionMemo Memo = {
378 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000379 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000380 SubRegNo,
381 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000382 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000383 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000384 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000385 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000386 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000387}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000388
Dan Gohman72d63af2008-08-26 21:21:20 +0000389void FastISelMap::PrintFunctionDefinitions(std::ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000390 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000391 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000392 OE = SimplePatterns.end(); OI != OE; ++OI) {
393 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000394 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000395
Owen Anderson7b2e5792008-08-25 23:43:09 +0000396 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000397 I != E; ++I) {
398 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000399 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000400
401 OS << "// FastEmit functions for " << Opcode << ".\n";
402 OS << "\n";
403
404 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000405 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000406 TI != TE; ++TI) {
407 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000408 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000409 if (RM.size() != 1) {
410 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
411 RI != RE; ++RI) {
412 MVT::SimpleValueType RetVT = RI->first;
413 const PredMap &PM = RI->second;
414 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000415
Evan Chengc3f44b02008-09-03 00:03:49 +0000416 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000417 << getLegalCName(Opcode)
418 << "_" << getLegalCName(getName(VT))
419 << "_" << getLegalCName(getName(RetVT)) << "_";
420 Operands.PrintManglingSuffix(OS);
421 OS << "(";
422 Operands.PrintParameters(OS);
423 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000424
Owen Anderson71669e52008-08-26 00:42:26 +0000425 // Emit code for each possible instruction. There may be
426 // multiple if there are subtarget concerns.
427 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
428 PI != PE; ++PI) {
429 std::string PredicateCheck = PI->first;
430 const InstructionMemo &Memo = PI->second;
431
432 if (PredicateCheck.empty()) {
433 assert(!HasPred &&
434 "Multiple instructions match, at least one has "
435 "a predicate and at least one doesn't!");
436 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000437 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000438 OS << " ";
439 HasPred = true;
440 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000441
442 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
443 if ((*Memo.PhysRegs)[i] != "")
444 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
445 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
446 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
447 << (*Memo.PhysRegs)[i] << "), "
448 << "MRI.getRegClass(Op" << i << "));\n";
449 }
450
Owen Anderson71669e52008-08-26 00:42:26 +0000451 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000452 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000453 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000454 OS << "(" << InstNS << Memo.Name << ", ";
455 OS << InstNS << Memo.RC->getName() << "RegisterClass";
456 if (!Operands.empty())
457 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000458 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000459 OS << ");\n";
460 } else {
461 OS << "extractsubreg(Op0, ";
462 OS << (unsigned)Memo.SubRegNo;
463 OS << ");\n";
464 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000465
466 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000467 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000468
Owen Anderson71669e52008-08-26 00:42:26 +0000469 }
470 // Return 0 if none of the predicates were satisfied.
471 if (HasPred)
472 OS << " return 0;\n";
473 OS << "}\n";
474 OS << "\n";
475 }
476
477 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000478 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000479 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000480 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000481 Operands.PrintManglingSuffix(OS);
482 OS << "(MVT::SimpleValueType RetVT";
483 if (!Operands.empty())
484 OS << ", ";
485 Operands.PrintParameters(OS);
486 OS << ") {\nswitch (RetVT) {\n";
487 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
488 RI != RE; ++RI) {
489 MVT::SimpleValueType RetVT = RI->first;
490 OS << " case " << getName(RetVT) << ": return FastEmit_"
491 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
492 << "_" << getLegalCName(getName(RetVT)) << "_";
493 Operands.PrintManglingSuffix(OS);
494 OS << "(";
495 Operands.PrintArguments(OS);
496 OS << ");\n";
497 }
498 OS << " default: return 0;\n}\n}\n\n";
499
500 } else {
501 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000502 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000503 << getLegalCName(Opcode) << "_"
504 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000505 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000506 OS << "(MVT::SimpleValueType RetVT";
507 if (!Operands.empty())
508 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000509 Operands.PrintParameters(OS);
510 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000511
Owen Anderson70647e82008-08-26 18:50:00 +0000512 OS << " if (RetVT != " << getName(RM.begin()->first)
513 << ")\n return 0;\n";
514
Owen Anderson71669e52008-08-26 00:42:26 +0000515 const PredMap &PM = RM.begin()->second;
516 bool HasPred = false;
517
Owen Anderson7b2e5792008-08-25 23:43:09 +0000518 // Emit code for each possible instruction. There may be
519 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000520 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
521 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000522 std::string PredicateCheck = PI->first;
523 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000524
Owen Anderson7b2e5792008-08-25 23:43:09 +0000525 if (PredicateCheck.empty()) {
526 assert(!HasPred &&
527 "Multiple instructions match, at least one has "
528 "a predicate and at least one doesn't!");
529 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000530 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000531 OS << " ";
532 HasPred = true;
533 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000534
535 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
536 if ((*Memo.PhysRegs)[i] != "")
537 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
538 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
539 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
540 << (*Memo.PhysRegs)[i] << "), "
541 << "MRI.getRegClass(Op" << i << "));\n";
542 }
543
Owen Anderson7b2e5792008-08-25 23:43:09 +0000544 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000545
546 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000547 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000548 OS << "(" << InstNS << Memo.Name << ", ";
549 OS << InstNS << Memo.RC->getName() << "RegisterClass";
550 if (!Operands.empty())
551 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000552 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000553 OS << ");\n";
554 } else {
555 OS << "extractsubreg(Op0, ";
556 OS << (unsigned)Memo.SubRegNo;
557 OS << ");\n";
558 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000559
560 if (HasPred)
561 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000562 }
Owen Anderson71669e52008-08-26 00:42:26 +0000563
Owen Anderson7b2e5792008-08-25 23:43:09 +0000564 // Return 0 if none of the predicates were satisfied.
565 if (HasPred)
566 OS << " return 0;\n";
567 OS << "}\n";
568 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000569 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570 }
571
572 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000573 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000574 << getLegalCName(Opcode) << "_";
575 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000576 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000577 if (!Operands.empty())
578 OS << ", ";
579 Operands.PrintParameters(OS);
580 OS << ") {\n";
581 OS << " switch (VT) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000582 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583 TI != TE; ++TI) {
584 MVT::SimpleValueType VT = TI->first;
585 std::string TypeName = getName(VT);
586 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000587 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
588 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000589 OS << "(RetVT";
590 if (!Operands.empty())
591 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592 Operands.PrintArguments(OS);
593 OS << ");\n";
594 }
595 OS << " default: return 0;\n";
596 OS << " }\n";
597 OS << "}\n";
598 OS << "\n";
599 }
600
Dan Gohman0bfb7522008-08-22 00:28:15 +0000601 OS << "// Top-level FastEmit function.\n";
602 OS << "\n";
603
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000604 // Emit one function for the operand signature that demultiplexes based
605 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000606 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000607 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000608 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000609 if (!Operands.empty())
610 OS << ", ";
611 Operands.PrintParameters(OS);
612 OS << ") {\n";
613 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000614 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000615 I != E; ++I) {
616 const std::string &Opcode = I->first;
617
618 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000619 << getLegalCName(Opcode) << "_";
620 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000621 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000622 if (!Operands.empty())
623 OS << ", ";
624 Operands.PrintArguments(OS);
625 OS << ");\n";
626 }
627 OS << " default: return 0;\n";
628 OS << " }\n";
629 OS << "}\n";
630 OS << "\n";
631 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000632}
633
634void FastISelEmitter::run(std::ostream &OS) {
635 const CodeGenTarget &Target = CGP.getTargetInfo();
636
637 // Determine the target's namespace name.
638 std::string InstNS = Target.getInstNamespace() + "::";
639 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
640
641 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
642 Target.getName() + " target", OS);
643
Dan Gohman72d63af2008-08-26 21:21:20 +0000644 FastISelMap F(InstNS);
645 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000646 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000647}
648
649FastISelEmitter::FastISelEmitter(RecordKeeper &R)
650 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000651 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000652}
Dan Gohman72d63af2008-08-26 21:21:20 +0000653