blob: 6f47c633e164beaedc0d60c3052962e0151cfef4 [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.
Owen Anderson667d8f72008-08-29 17:45:56 +0000117
118 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());
160 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
161 if (PR[i] != "") {
162 OS << PR[i];
163 } else if (Operands[i] == "r") {
164 OS << "Op" << i;
165 } else if (Operands[i] == "i") {
166 OS << "imm" << i;
167 } else if (Operands[i] == "f") {
168 OS << "f" << i;
169 } else {
170 assert("Unknown operand kind!");
171 abort();
172 }
173 if (i + 1 != e)
174 OS << ", ";
175 }
176 }
177
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000178 void PrintArguments(std::ostream &OS) const {
179 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
180 if (Operands[i] == "r") {
181 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000182 } else if (Operands[i] == "i") {
183 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000184 } else if (Operands[i] == "f") {
185 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000186 } else {
187 assert("Unknown operand kind!");
188 abort();
189 }
190 if (i + 1 != e)
191 OS << ", ";
192 }
193 }
194
Owen Anderson667d8f72008-08-29 17:45:56 +0000195
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000196 void PrintManglingSuffix(std::ostream &OS) const {
197 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
198 OS << Operands[i];
199 }
200 }
201};
202
Dan Gohman72d63af2008-08-26 21:21:20 +0000203class FastISelMap {
204 typedef std::map<std::string, InstructionMemo> PredMap;
205 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
206 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
207 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
208 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
209
210 OperandsOpcodeTypeRetPredMap SimplePatterns;
211
212 std::string InstNS;
213
214public:
215 explicit FastISelMap(std::string InstNS);
216
217 void CollectPatterns(CodeGenDAGPatterns &CGP);
218 void PrintClass(std::ostream &OS);
219 void PrintFunctionDefinitions(std::ostream &OS);
220};
221
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000222}
223
224static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
225 return CGP.getSDNodeInfo(Op).getEnumName();
226}
227
228static std::string getLegalCName(std::string OpName) {
229 std::string::size_type pos = OpName.find("::");
230 if (pos != std::string::npos)
231 OpName.replace(pos, 2, "_");
232 return OpName;
233}
234
Dan Gohman72d63af2008-08-26 21:21:20 +0000235FastISelMap::FastISelMap(std::string instns)
236 : InstNS(instns) {
237}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000238
Dan Gohman72d63af2008-08-26 21:21:20 +0000239void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
240 const CodeGenTarget &Target = CGP.getTargetInfo();
241
242 // Determine the target's namespace name.
243 InstNS = Target.getInstNamespace() + "::";
244 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000245
Dan Gohman0bfb7522008-08-22 00:28:15 +0000246 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000247 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
248 E = CGP.ptm_end(); I != E; ++I) {
249 const PatternToMatch &Pattern = *I;
250
251 // For now, just look at Instructions, so that we don't have to worry
252 // about emitting multiple instructions for a pattern.
253 TreePatternNode *Dst = Pattern.getDstPattern();
254 if (Dst->isLeaf()) continue;
255 Record *Op = Dst->getOperator();
256 if (!Op->isSubClassOf("Instruction"))
257 continue;
258 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
259 if (II.OperandList.empty())
260 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000261
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000262 // For now, ignore multi-instruction patterns.
263 bool MultiInsts = false;
264 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
265 TreePatternNode *ChildOp = Dst->getChild(i);
266 if (ChildOp->isLeaf())
267 continue;
268 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
269 MultiInsts = true;
270 break;
271 }
272 }
273 if (MultiInsts)
274 continue;
275
Dan Gohman379cad42008-08-19 20:36:33 +0000276 // For now, ignore instructions where the first operand is not an
277 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000278 const CodeGenRegisterClass *DstRC = 0;
279 unsigned SubRegNo = ~0;
280 if (Op->getName() != "EXTRACT_SUBREG") {
281 Record *Op0Rec = II.OperandList[0].Rec;
282 if (!Op0Rec->isSubClassOf("RegisterClass"))
283 continue;
284 DstRC = &Target.getRegisterClass(Op0Rec);
285 if (!DstRC)
286 continue;
287 } else {
288 SubRegNo = static_cast<IntInit*>(
289 Dst->getChild(1)->getLeafValue())->getValue();
290 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000291
292 // Inspect the pattern.
293 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
294 if (!InstPatNode) continue;
295 if (InstPatNode->isLeaf()) continue;
296
297 Record *InstPatOp = InstPatNode->getOperator();
298 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Owen Andersonabb1f162008-08-26 01:22:59 +0000299 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
300 MVT::SimpleValueType VT = RetVT;
301 if (InstPatNode->getNumChildren())
302 VT = InstPatNode->getChild(0)->getTypeNum(0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000303
304 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000305 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000306 if (InstPatOp->isSubClassOf("Operand"))
307 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000308
309 // For now, filter out any instructions with predicates.
310 if (!InstPatNode->getPredicateFn().empty())
311 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000312
Dan Gohman379cad42008-08-19 20:36:33 +0000313 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000314 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000315 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000316 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000317
318 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
319 if (!InstPatNode->isLeaf() &&
320 (InstPatNode->getOperator()->getName() == "imm" ||
321 InstPatNode->getOperator()->getName() == "fpimmm"))
322 PhysRegInputs->push_back("");
323 else if (!InstPatNode->isLeaf()) {
324 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
325 TreePatternNode *Op = InstPatNode->getChild(i);
326 if (!Op->isLeaf()) {
327 PhysRegInputs->push_back("");
328 continue;
329 }
330
331 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
332 Record *OpLeafRec = OpDI->getDef();
333 std::string PhysReg;
334 if (OpLeafRec->isSubClassOf("Register")) {
335 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
336 "Namespace")->getValue())->getValue();
337 PhysReg += "::";
338
339 std::vector<CodeGenRegister> Regs = Target.getRegisters();
340 for (unsigned i = 0; i < Regs.size(); ++i) {
341 if (Regs[i].TheDef == OpLeafRec) {
342 PhysReg += Regs[i].getName();
343 break;
344 }
345 }
346 }
347
348 PhysRegInputs->push_back(PhysReg);
349 }
350 } else
351 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000352
Dan Gohman22bb3112008-08-22 00:20:26 +0000353 // Get the predicate that guards this pattern.
354 std::string PredicateCheck = Pattern.getPredicateCheck();
355
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000356 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000357 InstructionMemo Memo = {
358 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000359 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000360 SubRegNo,
361 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000362 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000363 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000364 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000365 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000366 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000367}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000368
Dan Gohman72d63af2008-08-26 21:21:20 +0000369void FastISelMap::PrintFunctionDefinitions(std::ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000370 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000371 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000372 OE = SimplePatterns.end(); OI != OE; ++OI) {
373 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000374 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000375
Owen Anderson7b2e5792008-08-25 23:43:09 +0000376 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000377 I != E; ++I) {
378 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000379 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000380
381 OS << "// FastEmit functions for " << Opcode << ".\n";
382 OS << "\n";
383
384 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000385 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000386 TI != TE; ++TI) {
387 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000388 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000389 if (RM.size() != 1) {
390 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
391 RI != RE; ++RI) {
392 MVT::SimpleValueType RetVT = RI->first;
393 const PredMap &PM = RI->second;
394 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000395
Evan Chengc3f44b02008-09-03 00:03:49 +0000396 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000397 << getLegalCName(Opcode)
398 << "_" << getLegalCName(getName(VT))
399 << "_" << getLegalCName(getName(RetVT)) << "_";
400 Operands.PrintManglingSuffix(OS);
401 OS << "(";
402 Operands.PrintParameters(OS);
403 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000404
Owen Anderson71669e52008-08-26 00:42:26 +0000405 // Emit code for each possible instruction. There may be
406 // multiple if there are subtarget concerns.
407 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
408 PI != PE; ++PI) {
409 std::string PredicateCheck = PI->first;
410 const InstructionMemo &Memo = PI->second;
411
412 if (PredicateCheck.empty()) {
413 assert(!HasPred &&
414 "Multiple instructions match, at least one has "
415 "a predicate and at least one doesn't!");
416 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000417 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000418 OS << " ";
419 HasPred = true;
420 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000421
422 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
423 if ((*Memo.PhysRegs)[i] != "")
424 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
425 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
426 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
427 << (*Memo.PhysRegs)[i] << "), "
428 << "MRI.getRegClass(Op" << i << "));\n";
429 }
430
Owen Anderson71669e52008-08-26 00:42:26 +0000431 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000432 if (Memo.SubRegNo == (unsigned char)~0) {
433 Operands.PrintManglingSuffix(OS);
434 OS << "(" << InstNS << Memo.Name << ", ";
435 OS << InstNS << Memo.RC->getName() << "RegisterClass";
436 if (!Operands.empty())
437 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000438 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000439 OS << ");\n";
440 } else {
441 OS << "extractsubreg(Op0, ";
442 OS << (unsigned)Memo.SubRegNo;
443 OS << ");\n";
444 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000445
446 if (HasPred)
447 OS << "}\n";
448
Owen Anderson71669e52008-08-26 00:42:26 +0000449 }
450 // Return 0 if none of the predicates were satisfied.
451 if (HasPred)
452 OS << " return 0;\n";
453 OS << "}\n";
454 OS << "\n";
455 }
456
457 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000458 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000459 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000460 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000461 Operands.PrintManglingSuffix(OS);
462 OS << "(MVT::SimpleValueType RetVT";
463 if (!Operands.empty())
464 OS << ", ";
465 Operands.PrintParameters(OS);
466 OS << ") {\nswitch (RetVT) {\n";
467 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
468 RI != RE; ++RI) {
469 MVT::SimpleValueType RetVT = RI->first;
470 OS << " case " << getName(RetVT) << ": return FastEmit_"
471 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
472 << "_" << getLegalCName(getName(RetVT)) << "_";
473 Operands.PrintManglingSuffix(OS);
474 OS << "(";
475 Operands.PrintArguments(OS);
476 OS << ");\n";
477 }
478 OS << " default: return 0;\n}\n}\n\n";
479
480 } else {
481 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000482 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000483 << getLegalCName(Opcode) << "_"
484 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000485 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000486 OS << "(MVT::SimpleValueType RetVT";
487 if (!Operands.empty())
488 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000489 Operands.PrintParameters(OS);
490 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000491
Owen Anderson70647e82008-08-26 18:50:00 +0000492 OS << " if (RetVT != " << getName(RM.begin()->first)
493 << ")\n return 0;\n";
494
Owen Anderson71669e52008-08-26 00:42:26 +0000495 const PredMap &PM = RM.begin()->second;
496 bool HasPred = false;
497
Owen Anderson7b2e5792008-08-25 23:43:09 +0000498 // Emit code for each possible instruction. There may be
499 // multiple if there are subtarget concerns.
Owen Anderson71669e52008-08-26 00:42:26 +0000500 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000501 std::string PredicateCheck = PI->first;
502 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000503
Owen Anderson7b2e5792008-08-25 23:43:09 +0000504 if (PredicateCheck.empty()) {
505 assert(!HasPred &&
506 "Multiple instructions match, at least one has "
507 "a predicate and at least one doesn't!");
508 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000509 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000510 OS << " ";
511 HasPred = true;
512 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000513
514 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
515 if ((*Memo.PhysRegs)[i] != "")
516 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
517 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
518 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
519 << (*Memo.PhysRegs)[i] << "), "
520 << "MRI.getRegClass(Op" << i << "));\n";
521 }
522
Owen Anderson7b2e5792008-08-25 23:43:09 +0000523 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000524
525 if (Memo.SubRegNo == (unsigned char)~0) {
526 Operands.PrintManglingSuffix(OS);
527 OS << "(" << InstNS << Memo.Name << ", ";
528 OS << InstNS << Memo.RC->getName() << "RegisterClass";
529 if (!Operands.empty())
530 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000531 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000532 OS << ");\n";
533 } else {
534 OS << "extractsubreg(Op0, ";
535 OS << (unsigned)Memo.SubRegNo;
536 OS << ");\n";
537 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000538
539 if (HasPred)
540 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000541 }
Owen Anderson71669e52008-08-26 00:42:26 +0000542
Owen Anderson7b2e5792008-08-25 23:43:09 +0000543 // Return 0 if none of the predicates were satisfied.
544 if (HasPred)
545 OS << " return 0;\n";
546 OS << "}\n";
547 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000548 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000549 }
550
551 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000552 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000553 << getLegalCName(Opcode) << "_";
554 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000555 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000556 if (!Operands.empty())
557 OS << ", ";
558 Operands.PrintParameters(OS);
559 OS << ") {\n";
560 OS << " switch (VT) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000561 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000562 TI != TE; ++TI) {
563 MVT::SimpleValueType VT = TI->first;
564 std::string TypeName = getName(VT);
565 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000566 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
567 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000568 OS << "(RetVT";
569 if (!Operands.empty())
570 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000571 Operands.PrintArguments(OS);
572 OS << ");\n";
573 }
574 OS << " default: return 0;\n";
575 OS << " }\n";
576 OS << "}\n";
577 OS << "\n";
578 }
579
Dan Gohman0bfb7522008-08-22 00:28:15 +0000580 OS << "// Top-level FastEmit function.\n";
581 OS << "\n";
582
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583 // Emit one function for the operand signature that demultiplexes based
584 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000585 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000586 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000587 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000588 if (!Operands.empty())
589 OS << ", ";
590 Operands.PrintParameters(OS);
591 OS << ") {\n";
592 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000593 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000594 I != E; ++I) {
595 const std::string &Opcode = I->first;
596
597 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000598 << getLegalCName(Opcode) << "_";
599 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000600 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000601 if (!Operands.empty())
602 OS << ", ";
603 Operands.PrintArguments(OS);
604 OS << ");\n";
605 }
606 OS << " default: return 0;\n";
607 OS << " }\n";
608 OS << "}\n";
609 OS << "\n";
610 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000611}
612
613void FastISelEmitter::run(std::ostream &OS) {
614 const CodeGenTarget &Target = CGP.getTargetInfo();
615
616 // Determine the target's namespace name.
617 std::string InstNS = Target.getInstNamespace() + "::";
618 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
619
620 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
621 Target.getName() + " target", OS);
622
Dan Gohman72d63af2008-08-26 21:21:20 +0000623 FastISelMap F(InstNS);
624 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000625 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000626}
627
628FastISelEmitter::FastISelEmitter(RecordKeeper &R)
629 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000630 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000631}
Dan Gohman72d63af2008-08-26 21:21:20 +0000632