blob: bf92a9a0237be938b60f5b355832767dc24ec32e [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 PrintClass(raw_ostream &OS);
220 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000221};
222
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000223}
224
225static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
226 return CGP.getSDNodeInfo(Op).getEnumName();
227}
228
229static std::string getLegalCName(std::string OpName) {
230 std::string::size_type pos = OpName.find("::");
231 if (pos != std::string::npos)
232 OpName.replace(pos, 2, "_");
233 return OpName;
234}
235
Dan Gohman72d63af2008-08-26 21:21:20 +0000236FastISelMap::FastISelMap(std::string instns)
237 : InstNS(instns) {
238}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000239
Dan Gohman72d63af2008-08-26 21:21:20 +0000240void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
241 const CodeGenTarget &Target = CGP.getTargetInfo();
242
243 // Determine the target's namespace name.
244 InstNS = Target.getInstNamespace() + "::";
245 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000246
Dan Gohman0bfb7522008-08-22 00:28:15 +0000247 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000248 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
249 E = CGP.ptm_end(); I != E; ++I) {
250 const PatternToMatch &Pattern = *I;
251
252 // For now, just look at Instructions, so that we don't have to worry
253 // about emitting multiple instructions for a pattern.
254 TreePatternNode *Dst = Pattern.getDstPattern();
255 if (Dst->isLeaf()) continue;
256 Record *Op = Dst->getOperator();
257 if (!Op->isSubClassOf("Instruction"))
258 continue;
259 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
260 if (II.OperandList.empty())
261 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000262
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000263 // For now, ignore multi-instruction patterns.
264 bool MultiInsts = false;
265 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
266 TreePatternNode *ChildOp = Dst->getChild(i);
267 if (ChildOp->isLeaf())
268 continue;
269 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
270 MultiInsts = true;
271 break;
272 }
273 }
274 if (MultiInsts)
275 continue;
276
Dan Gohman379cad42008-08-19 20:36:33 +0000277 // For now, ignore instructions where the first operand is not an
278 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000279 const CodeGenRegisterClass *DstRC = 0;
280 unsigned SubRegNo = ~0;
281 if (Op->getName() != "EXTRACT_SUBREG") {
282 Record *Op0Rec = II.OperandList[0].Rec;
283 if (!Op0Rec->isSubClassOf("RegisterClass"))
284 continue;
285 DstRC = &Target.getRegisterClass(Op0Rec);
286 if (!DstRC)
287 continue;
288 } else {
289 SubRegNo = static_cast<IntInit*>(
290 Dst->getChild(1)->getLeafValue())->getValue();
291 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000292
293 // Inspect the pattern.
294 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
295 if (!InstPatNode) continue;
296 if (InstPatNode->isLeaf()) continue;
297
298 Record *InstPatOp = InstPatNode->getOperator();
299 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
301 MVT::SimpleValueType VT = RetVT;
Owen Andersonabb1f162008-08-26 01:22:59 +0000302 if (InstPatNode->getNumChildren())
303 VT = InstPatNode->getChild(0)->getTypeNum(0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000304
305 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000306 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000307 if (InstPatOp->isSubClassOf("Operand"))
308 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000309
310 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000311 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000312 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000313
Dan Gohman379cad42008-08-19 20:36:33 +0000314 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000315 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000316 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000317 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000318
319 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
320 if (!InstPatNode->isLeaf() &&
321 (InstPatNode->getOperator()->getName() == "imm" ||
322 InstPatNode->getOperator()->getName() == "fpimmm"))
323 PhysRegInputs->push_back("");
324 else if (!InstPatNode->isLeaf()) {
325 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
326 TreePatternNode *Op = InstPatNode->getChild(i);
327 if (!Op->isLeaf()) {
328 PhysRegInputs->push_back("");
329 continue;
330 }
331
332 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
333 Record *OpLeafRec = OpDI->getDef();
334 std::string PhysReg;
335 if (OpLeafRec->isSubClassOf("Register")) {
336 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
337 "Namespace")->getValue())->getValue();
338 PhysReg += "::";
339
340 std::vector<CodeGenRegister> Regs = Target.getRegisters();
341 for (unsigned i = 0; i < Regs.size(); ++i) {
342 if (Regs[i].TheDef == OpLeafRec) {
343 PhysReg += Regs[i].getName();
344 break;
345 }
346 }
347 }
348
349 PhysRegInputs->push_back(PhysReg);
350 }
351 } else
352 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000353
Dan Gohman22bb3112008-08-22 00:20:26 +0000354 // Get the predicate that guards this pattern.
355 std::string PredicateCheck = Pattern.getPredicateCheck();
356
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000357 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000358 InstructionMemo Memo = {
359 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000360 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000361 SubRegNo,
362 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000363 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000364 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000365 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000366 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000367 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000368}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000369
Daniel Dunbar1a551802009-07-03 00:10:29 +0000370void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000371 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000372 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000373 OE = SimplePatterns.end(); OI != OE; ++OI) {
374 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000375 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000376
Owen Anderson7b2e5792008-08-25 23:43:09 +0000377 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000378 I != E; ++I) {
379 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000380 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000381
382 OS << "// FastEmit functions for " << Opcode << ".\n";
383 OS << "\n";
384
385 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000386 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000387 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000389 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000390 if (RM.size() != 1) {
391 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
392 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000394 const PredMap &PM = RI->second;
395 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000396
Evan Chengc3f44b02008-09-03 00:03:49 +0000397 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000398 << getLegalCName(Opcode)
399 << "_" << getLegalCName(getName(VT))
400 << "_" << getLegalCName(getName(RetVT)) << "_";
401 Operands.PrintManglingSuffix(OS);
402 OS << "(";
403 Operands.PrintParameters(OS);
404 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000405
Owen Anderson71669e52008-08-26 00:42:26 +0000406 // Emit code for each possible instruction. There may be
407 // multiple if there are subtarget concerns.
408 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
409 PI != PE; ++PI) {
410 std::string PredicateCheck = PI->first;
411 const InstructionMemo &Memo = PI->second;
412
413 if (PredicateCheck.empty()) {
414 assert(!HasPred &&
415 "Multiple instructions match, at least one has "
416 "a predicate and at least one doesn't!");
417 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000418 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000419 OS << " ";
420 HasPred = true;
421 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000422
423 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
424 if ((*Memo.PhysRegs)[i] != "")
425 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
426 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
427 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
428 << (*Memo.PhysRegs)[i] << "), "
429 << "MRI.getRegClass(Op" << i << "));\n";
430 }
431
Owen Anderson71669e52008-08-26 00:42:26 +0000432 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000433 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000434 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000435 OS << "(" << InstNS << Memo.Name << ", ";
436 OS << InstNS << Memo.RC->getName() << "RegisterClass";
437 if (!Operands.empty())
438 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000439 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000440 OS << ");\n";
441 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000442 OS << "extractsubreg(" << getName(RetVT);
443 OS << ", Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000444 OS << (unsigned)Memo.SubRegNo;
445 OS << ");\n";
446 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000447
448 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000449 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000450
Owen Anderson71669e52008-08-26 00:42:26 +0000451 }
452 // Return 0 if none of the predicates were satisfied.
453 if (HasPred)
454 OS << " return 0;\n";
455 OS << "}\n";
456 OS << "\n";
457 }
458
459 // Emit one function for the type that demultiplexes on return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000460 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000461 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000462 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000463 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000465 if (!Operands.empty())
466 OS << ", ";
467 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000469 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
470 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000472 OS << " case " << getName(RetVT) << ": return FastEmit_"
473 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
474 << "_" << getLegalCName(getName(RetVT)) << "_";
475 Operands.PrintManglingSuffix(OS);
476 OS << "(";
477 Operands.PrintArguments(OS);
478 OS << ");\n";
479 }
480 OS << " default: return 0;\n}\n}\n\n";
481
482 } else {
483 // Non-variadic return type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000484 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000485 << getLegalCName(Opcode) << "_"
486 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000487 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000489 if (!Operands.empty())
490 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000491 Operands.PrintParameters(OS);
492 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000493
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000495 << ")\n return 0;\n";
496
Owen Anderson71669e52008-08-26 00:42:26 +0000497 const PredMap &PM = RM.begin()->second;
498 bool HasPred = false;
499
Owen Anderson7b2e5792008-08-25 23:43:09 +0000500 // Emit code for each possible instruction. There may be
501 // multiple if there are subtarget concerns.
Evan Cheng98d2d072008-09-08 08:39:33 +0000502 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
503 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000504 std::string PredicateCheck = PI->first;
505 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000506
Owen Anderson7b2e5792008-08-25 23:43:09 +0000507 if (PredicateCheck.empty()) {
508 assert(!HasPred &&
509 "Multiple instructions match, at least one has "
510 "a predicate and at least one doesn't!");
511 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000512 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000513 OS << " ";
514 HasPred = true;
515 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000516
517 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
518 if ((*Memo.PhysRegs)[i] != "")
519 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
520 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
521 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
522 << (*Memo.PhysRegs)[i] << "), "
523 << "MRI.getRegClass(Op" << i << "));\n";
524 }
525
Owen Anderson7b2e5792008-08-25 23:43:09 +0000526 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000527
528 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000529 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000530 OS << "(" << InstNS << Memo.Name << ", ";
531 OS << InstNS << Memo.RC->getName() << "RegisterClass";
532 if (!Operands.empty())
533 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000534 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000535 OS << ");\n";
536 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000537 OS << "extractsubreg(RetVT, Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000538 OS << (unsigned)Memo.SubRegNo;
539 OS << ");\n";
540 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000541
542 if (HasPred)
543 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000544 }
Owen Anderson71669e52008-08-26 00:42:26 +0000545
Owen Anderson7b2e5792008-08-25 23:43:09 +0000546 // Return 0 if none of the predicates were satisfied.
547 if (HasPred)
548 OS << " return 0;\n";
549 OS << "}\n";
550 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000551 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000552 }
553
554 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000555 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000556 << getLegalCName(Opcode) << "_";
557 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000559 if (!Operands.empty())
560 OS << ", ";
561 Operands.PrintParameters(OS);
562 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000564 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000565 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000567 std::string TypeName = getName(VT);
568 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000569 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
570 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000571 OS << "(RetVT";
572 if (!Operands.empty())
573 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000574 Operands.PrintArguments(OS);
575 OS << ");\n";
576 }
577 OS << " default: return 0;\n";
578 OS << " }\n";
579 OS << "}\n";
580 OS << "\n";
581 }
582
Dan Gohman0bfb7522008-08-22 00:28:15 +0000583 OS << "// Top-level FastEmit function.\n";
584 OS << "\n";
585
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000586 // Emit one function for the operand signature that demultiplexes based
587 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000588 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 OS << "(MVT VT, MVT RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000591 if (!Operands.empty())
592 OS << ", ";
593 Operands.PrintParameters(OS);
594 OS << ") {\n";
595 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000596 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000597 I != E; ++I) {
598 const std::string &Opcode = I->first;
599
600 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000601 << getLegalCName(Opcode) << "_";
602 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000603 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000604 if (!Operands.empty())
605 OS << ", ";
606 Operands.PrintArguments(OS);
607 OS << ");\n";
608 }
609 OS << " default: return 0;\n";
610 OS << " }\n";
611 OS << "}\n";
612 OS << "\n";
613 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000614}
615
Daniel Dunbar1a551802009-07-03 00:10:29 +0000616void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000617 const CodeGenTarget &Target = CGP.getTargetInfo();
618
619 // Determine the target's namespace name.
620 std::string InstNS = Target.getInstNamespace() + "::";
621 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
622
623 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
624 Target.getName() + " target", OS);
625
Dan Gohman72d63af2008-08-26 21:21:20 +0000626 FastISelMap F(InstNS);
627 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000628 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000629}
630
631FastISelEmitter::FastISelEmitter(RecordKeeper &R)
632 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000633 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000634}
Dan Gohman72d63af2008-08-26 21:21:20 +0000635