blob: 277640d79968acd541526485dec0b2dad2ff1c3d [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"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000023#include "llvm/ADT/VectorExtras.h"
24using namespace llvm;
25
26namespace {
27
Owen Anderson667d8f72008-08-29 17:45:56 +000028/// InstructionMemo - This class holds additional information about an
29/// instruction needed to emit code for it.
30///
31struct InstructionMemo {
32 std::string Name;
33 const CodeGenRegisterClass *RC;
34 unsigned char SubRegNo;
35 std::vector<std::string>* PhysRegs;
36};
37
Dan Gohman04b7dfb2008-08-19 18:06:12 +000038/// OperandsSignature - This class holds a description of a list of operand
39/// types. It has utility methods for emitting text based on the operands.
40///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000041struct OperandsSignature {
42 std::vector<std::string> Operands;
43
44 bool operator<(const OperandsSignature &O) const {
45 return Operands < O.Operands;
46 }
47
48 bool empty() const { return Operands.empty(); }
49
Dan Gohmand1d2ee82008-08-19 20:56:30 +000050 /// initialize - Examine the given pattern and initialize the contents
51 /// of the Operands array accordingly. Return true if all the operands
52 /// are supported, false otherwise.
53 ///
54 bool initialize(TreePatternNode *InstPatNode,
55 const CodeGenTarget &Target,
Owen Anderson825b72b2009-08-11 20:47:22 +000056 MVT::SimpleValueType VT) {
Owen Anderson6d0c25e2008-08-25 20:20:32 +000057 if (!InstPatNode->isLeaf() &&
58 InstPatNode->getOperator()->getName() == "imm") {
59 Operands.push_back("i");
60 return true;
61 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000062 if (!InstPatNode->isLeaf() &&
63 InstPatNode->getOperator()->getName() == "fpimm") {
64 Operands.push_back("f");
65 return true;
66 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000067
Owen Andersonabb1f162008-08-26 01:22:59 +000068 const CodeGenRegisterClass *DstRC = 0;
69
Dan Gohmand1d2ee82008-08-19 20:56:30 +000070 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
71 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000072 // For now, filter out any operand with a predicate.
Dan Gohman0540e172008-10-15 06:17:21 +000073 if (!Op->getPredicateFns().empty())
Dan Gohmand1d2ee82008-08-19 20:56:30 +000074 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000075 // For now, filter out any operand with multiple values.
76 if (Op->getExtTypes().size() != 1)
77 return false;
78 // For now, all the operands must have the same type.
79 if (Op->getTypeNum(0) != VT)
80 return false;
81 if (!Op->isLeaf()) {
82 if (Op->getOperator()->getName() == "imm") {
83 Operands.push_back("i");
Dale Johannesenedc87742009-05-21 22:25:49 +000084 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000085 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000086 if (Op->getOperator()->getName() == "fpimm") {
87 Operands.push_back("f");
Dale Johannesenedc87742009-05-21 22:25:49 +000088 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +000089 }
Dan Gohman833ddf82008-08-27 16:18:22 +000090 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000091 return false;
92 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000093 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
94 if (!OpDI)
95 return false;
96 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +000097 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +000098
Owen Anderson667d8f72008-08-29 17:45:56 +000099 const CodeGenRegisterClass *RC = 0;
100 if (OpLeafRec->isSubClassOf("RegisterClass"))
101 RC = &Target.getRegisterClass(OpLeafRec);
102 else if (OpLeafRec->isSubClassOf("Register"))
103 RC = Target.getRegisterClassForRegister(OpLeafRec);
104 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000105 return false;
106 // For now, require the register operands' register classes to all
107 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000108 if (!RC)
109 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000110 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000111 if (DstRC) {
112 if (DstRC != RC)
113 return false;
114 } else
115 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000116 Operands.push_back("r");
117 }
118 return true;
119 }
120
Daniel Dunbar1a551802009-07-03 00:10:29 +0000121 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000122 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
123 if (Operands[i] == "r") {
124 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000125 } else if (Operands[i] == "i") {
126 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000127 } else if (Operands[i] == "f") {
128 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000129 } else {
130 assert("Unknown operand kind!");
131 abort();
132 }
133 if (i + 1 != e)
134 OS << ", ";
135 }
136 }
137
Daniel Dunbar1a551802009-07-03 00:10:29 +0000138 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000139 const std::vector<std::string>& PR) const {
140 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000141 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000142 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000143 if (PR[i] != "")
144 // Implicit physical register operand.
145 continue;
146
147 if (PrintedArg)
148 OS << ", ";
149 if (Operands[i] == "r") {
Owen Anderson667d8f72008-08-29 17:45:56 +0000150 OS << "Op" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000151 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000152 } else if (Operands[i] == "i") {
153 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000154 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000155 } else if (Operands[i] == "f") {
156 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000157 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000158 } else {
159 assert("Unknown operand kind!");
160 abort();
161 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000162 }
163 }
164
Daniel Dunbar1a551802009-07-03 00:10:29 +0000165 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000166 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
167 if (Operands[i] == "r") {
168 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000169 } else if (Operands[i] == "i") {
170 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000171 } else if (Operands[i] == "f") {
172 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000173 } else {
174 assert("Unknown operand kind!");
175 abort();
176 }
177 if (i + 1 != e)
178 OS << ", ";
179 }
180 }
181
Owen Anderson667d8f72008-08-29 17:45:56 +0000182
Daniel Dunbar1a551802009-07-03 00:10:29 +0000183 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000184 const std::vector<std::string>& PR) const {
185 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
186 if (PR[i] != "")
187 // Implicit physical register operand. e.g. Instruction::Mul expect to
188 // select to a binary op. On x86, mul may take a single operand with
189 // the other operand being implicit. We must emit something that looks
190 // like a binary instruction except for the very inner FastEmitInst_*
191 // call.
192 continue;
193 OS << Operands[i];
194 }
195 }
196
Daniel Dunbar1a551802009-07-03 00:10:29 +0000197 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000198 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
199 OS << Operands[i];
200 }
201 }
202};
203
Dan Gohman72d63af2008-08-26 21:21:20 +0000204class FastISelMap {
205 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
207 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000208 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
209 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
210
211 OperandsOpcodeTypeRetPredMap SimplePatterns;
212
213 std::string InstNS;
214
215public:
216 explicit FastISelMap(std::string InstNS);
217
218 void CollectPatterns(CodeGenDAGPatterns &CGP);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000219 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000220};
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 Anderson825b72b2009-08-11 20:47:22 +0000299 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
300 MVT::SimpleValueType VT = RetVT;
Owen Andersonabb1f162008-08-26 01:22:59 +0000301 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.
Dan Gohman0540e172008-10-15 06:17:21 +0000310 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000311 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
Daniel Dunbar1a551802009-07-03 00:10:29 +0000369void FastISelMap::PrintFunctionDefinitions(raw_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) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 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) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000393 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) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000433 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000434 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 {
Evan Cheng536ab132009-01-22 09:10:11 +0000441 OS << "extractsubreg(" << getName(RetVT);
442 OS << ", Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000443 OS << (unsigned)Memo.SubRegNo;
444 OS << ");\n";
445 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000446
447 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000448 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000449
Owen Anderson71669e52008-08-26 00:42:26 +0000450 }
451 // Return 0 if none of the predicates were satisfied.
452 if (HasPred)
453 OS << " return 0;\n";
454 OS << "}\n";
455 OS << "\n";
456 }
457
458 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000459 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000460 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000461 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000462 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000464 if (!Operands.empty())
465 OS << ", ";
466 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000468 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
469 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000471 OS << " case " << getName(RetVT) << ": return FastEmit_"
472 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
473 << "_" << getLegalCName(getName(RetVT)) << "_";
474 Operands.PrintManglingSuffix(OS);
475 OS << "(";
476 Operands.PrintArguments(OS);
477 OS << ");\n";
478 }
479 OS << " default: return 0;\n}\n}\n\n";
480
481 } else {
482 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000483 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000484 << getLegalCName(Opcode) << "_"
485 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000486 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000488 if (!Operands.empty())
489 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000490 Operands.PrintParameters(OS);
491 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000492
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000494 << ")\n return 0;\n";
495
Owen Anderson71669e52008-08-26 00:42:26 +0000496 const PredMap &PM = RM.begin()->second;
497 bool HasPred = false;
498
Owen Anderson7b2e5792008-08-25 23:43:09 +0000499 // Emit code for each possible instruction. There may be
500 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000501 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
502 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000503 std::string PredicateCheck = PI->first;
504 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000505
Owen Anderson7b2e5792008-08-25 23:43:09 +0000506 if (PredicateCheck.empty()) {
507 assert(!HasPred &&
508 "Multiple instructions match, at least one has "
509 "a predicate and at least one doesn't!");
510 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000511 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000512 OS << " ";
513 HasPred = true;
514 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000515
516 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
517 if ((*Memo.PhysRegs)[i] != "")
518 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
519 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
520 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
521 << (*Memo.PhysRegs)[i] << "), "
522 << "MRI.getRegClass(Op" << i << "));\n";
523 }
524
Owen Anderson7b2e5792008-08-25 23:43:09 +0000525 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000526
527 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000528 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000529 OS << "(" << InstNS << Memo.Name << ", ";
530 OS << InstNS << Memo.RC->getName() << "RegisterClass";
531 if (!Operands.empty())
532 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000533 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000534 OS << ");\n";
535 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000536 OS << "extractsubreg(RetVT, Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000537 OS << (unsigned)Memo.SubRegNo;
538 OS << ");\n";
539 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000540
541 if (HasPred)
542 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000543 }
Owen Anderson71669e52008-08-26 00:42:26 +0000544
Owen Anderson7b2e5792008-08-25 23:43:09 +0000545 // Return 0 if none of the predicates were satisfied.
546 if (HasPred)
547 OS << " return 0;\n";
548 OS << "}\n";
549 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000550 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000551 }
552
553 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000554 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000555 << getLegalCName(Opcode) << "_";
556 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000558 if (!Operands.empty())
559 OS << ", ";
560 Operands.PrintParameters(OS);
561 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000563 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000564 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000566 std::string TypeName = getName(VT);
567 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000568 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
569 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000570 OS << "(RetVT";
571 if (!Operands.empty())
572 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000573 Operands.PrintArguments(OS);
574 OS << ");\n";
575 }
576 OS << " default: return 0;\n";
577 OS << " }\n";
578 OS << "}\n";
579 OS << "\n";
580 }
581
Dan Gohman0bfb7522008-08-22 00:28:15 +0000582 OS << "// Top-level FastEmit function.\n";
583 OS << "\n";
584
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000585 // Emit one function for the operand signature that demultiplexes based
586 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000587 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000588 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 OS << "(MVT VT, MVT RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 if (!Operands.empty())
591 OS << ", ";
592 Operands.PrintParameters(OS);
593 OS << ") {\n";
594 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000595 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000596 I != E; ++I) {
597 const std::string &Opcode = I->first;
598
599 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000600 << getLegalCName(Opcode) << "_";
601 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000602 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000603 if (!Operands.empty())
604 OS << ", ";
605 Operands.PrintArguments(OS);
606 OS << ");\n";
607 }
608 OS << " default: return 0;\n";
609 OS << " }\n";
610 OS << "}\n";
611 OS << "\n";
612 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000613}
614
Daniel Dunbar1a551802009-07-03 00:10:29 +0000615void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000616 const CodeGenTarget &Target = CGP.getTargetInfo();
617
618 // Determine the target's namespace name.
619 std::string InstNS = Target.getInstNamespace() + "::";
620 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
621
622 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
623 Target.getName() + " target", OS);
624
Dan Gohman72d63af2008-08-26 21:21:20 +0000625 FastISelMap F(InstNS);
626 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000627 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000628}
629
630FastISelEmitter::FastISelEmitter(RecordKeeper &R)
631 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000632 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000633}
Dan Gohman72d63af2008-08-26 21:21:20 +0000634