blob: 52f9563ec7264f8d3c999d8609b7261831b973d6 [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.
Chris Lattner2cacec52010-03-15 06:00:16 +000076 assert(Op->hasTypeSet() && "Type infererence not done?");
Dan Gohmand5fe57d2008-08-21 01:41:07 +000077 // For now, all the operands must have the same type.
Chris Lattner2cacec52010-03-15 06:00:16 +000078 if (Op->getType() != VT)
Dan Gohmand5fe57d2008-08-21 01:41:07 +000079 return false;
80 if (!Op->isLeaf()) {
81 if (Op->getOperator()->getName() == "imm") {
82 Operands.push_back("i");
Dale Johannesenedc87742009-05-21 22:25:49 +000083 continue;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000084 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000085 if (Op->getOperator()->getName() == "fpimm") {
86 Operands.push_back("f");
Dale Johannesenedc87742009-05-21 22:25:49 +000087 continue;
Dan Gohman10df0fa2008-08-27 01:09:54 +000088 }
Dan Gohman833ddf82008-08-27 16:18:22 +000089 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +000090 return false;
91 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000092 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
93 if (!OpDI)
94 return false;
95 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +000096 // For now, the only other thing we accept is register operands.
Evan Cheng98d2d072008-09-08 08:39:33 +000097
Owen Anderson667d8f72008-08-29 17:45:56 +000098 const CodeGenRegisterClass *RC = 0;
99 if (OpLeafRec->isSubClassOf("RegisterClass"))
100 RC = &Target.getRegisterClass(OpLeafRec);
101 else if (OpLeafRec->isSubClassOf("Register"))
102 RC = Target.getRegisterClassForRegister(OpLeafRec);
103 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000104 return false;
105 // For now, require the register operands' register classes to all
106 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000107 if (!RC)
108 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000109 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000110 if (DstRC) {
111 if (DstRC != RC)
112 return false;
113 } else
114 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000115 Operands.push_back("r");
116 }
117 return true;
118 }
119
Daniel Dunbar1a551802009-07-03 00:10:29 +0000120 void PrintParameters(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000121 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
122 if (Operands[i] == "r") {
123 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000124 } else if (Operands[i] == "i") {
125 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000126 } else if (Operands[i] == "f") {
127 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000128 } else {
129 assert("Unknown operand kind!");
130 abort();
131 }
132 if (i + 1 != e)
133 OS << ", ";
134 }
135 }
136
Daniel Dunbar1a551802009-07-03 00:10:29 +0000137 void PrintArguments(raw_ostream &OS,
Owen Anderson667d8f72008-08-29 17:45:56 +0000138 const std::vector<std::string>& PR) const {
139 assert(PR.size() == Operands.size());
Evan Cheng98d2d072008-09-08 08:39:33 +0000140 bool PrintedArg = false;
Owen Anderson667d8f72008-08-29 17:45:56 +0000141 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000142 if (PR[i] != "")
143 // Implicit physical register operand.
144 continue;
145
146 if (PrintedArg)
147 OS << ", ";
148 if (Operands[i] == "r") {
Owen Anderson667d8f72008-08-29 17:45:56 +0000149 OS << "Op" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000150 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000151 } else if (Operands[i] == "i") {
152 OS << "imm" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000153 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000154 } else if (Operands[i] == "f") {
155 OS << "f" << i;
Evan Cheng98d2d072008-09-08 08:39:33 +0000156 PrintedArg = true;
Owen Anderson667d8f72008-08-29 17:45:56 +0000157 } else {
158 assert("Unknown operand kind!");
159 abort();
160 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000161 }
162 }
163
Daniel Dunbar1a551802009-07-03 00:10:29 +0000164 void PrintArguments(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000165 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
166 if (Operands[i] == "r") {
167 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000168 } else if (Operands[i] == "i") {
169 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000170 } else if (Operands[i] == "f") {
171 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000172 } else {
173 assert("Unknown operand kind!");
174 abort();
175 }
176 if (i + 1 != e)
177 OS << ", ";
178 }
179 }
180
Owen Anderson667d8f72008-08-29 17:45:56 +0000181
Daniel Dunbar1a551802009-07-03 00:10:29 +0000182 void PrintManglingSuffix(raw_ostream &OS,
Evan Cheng98d2d072008-09-08 08:39:33 +0000183 const std::vector<std::string>& PR) const {
184 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
185 if (PR[i] != "")
186 // Implicit physical register operand. e.g. Instruction::Mul expect to
187 // select to a binary op. On x86, mul may take a single operand with
188 // the other operand being implicit. We must emit something that looks
189 // like a binary instruction except for the very inner FastEmitInst_*
190 // call.
191 continue;
192 OS << Operands[i];
193 }
194 }
195
Daniel Dunbar1a551802009-07-03 00:10:29 +0000196 void PrintManglingSuffix(raw_ostream &OS) const {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000197 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;
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
206 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
Dan Gohman72d63af2008-08-26 21:21:20 +0000207 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);
Daniel Dunbar1a551802009-07-03 00:10:29 +0000218 void PrintFunctionDefinitions(raw_ostream &OS);
Dan Gohman72d63af2008-08-26 21:21:20 +0000219};
220
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000221}
222
223static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
224 return CGP.getSDNodeInfo(Op).getEnumName();
225}
226
227static std::string getLegalCName(std::string OpName) {
228 std::string::size_type pos = OpName.find("::");
229 if (pos != std::string::npos)
230 OpName.replace(pos, 2, "_");
231 return OpName;
232}
233
Dan Gohman72d63af2008-08-26 21:21:20 +0000234FastISelMap::FastISelMap(std::string instns)
235 : InstNS(instns) {
236}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000237
Dan Gohman72d63af2008-08-26 21:21:20 +0000238void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
239 const CodeGenTarget &Target = CGP.getTargetInfo();
240
241 // Determine the target's namespace name.
242 InstNS = Target.getInstNamespace() + "::";
243 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000244
Dan Gohman0bfb7522008-08-22 00:28:15 +0000245 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000246 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
247 E = CGP.ptm_end(); I != E; ++I) {
248 const PatternToMatch &Pattern = *I;
249
250 // For now, just look at Instructions, so that we don't have to worry
251 // about emitting multiple instructions for a pattern.
252 TreePatternNode *Dst = Pattern.getDstPattern();
253 if (Dst->isLeaf()) continue;
254 Record *Op = Dst->getOperator();
255 if (!Op->isSubClassOf("Instruction"))
256 continue;
Chris Lattnerf30187a2010-03-19 00:07:20 +0000257 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000258 if (II.OperandList.empty())
259 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000260
Evan Cheng34fc6ce2008-09-07 08:19:51 +0000261 // For now, ignore multi-instruction patterns.
262 bool MultiInsts = false;
263 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
264 TreePatternNode *ChildOp = Dst->getChild(i);
265 if (ChildOp->isLeaf())
266 continue;
267 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
268 MultiInsts = true;
269 break;
270 }
271 }
272 if (MultiInsts)
273 continue;
274
Dan Gohman379cad42008-08-19 20:36:33 +0000275 // For now, ignore instructions where the first operand is not an
276 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000277 const CodeGenRegisterClass *DstRC = 0;
278 unsigned SubRegNo = ~0;
279 if (Op->getName() != "EXTRACT_SUBREG") {
280 Record *Op0Rec = II.OperandList[0].Rec;
281 if (!Op0Rec->isSubClassOf("RegisterClass"))
282 continue;
283 DstRC = &Target.getRegisterClass(Op0Rec);
284 if (!DstRC)
285 continue;
286 } else {
287 SubRegNo = static_cast<IntInit*>(
288 Dst->getChild(1)->getLeafValue())->getValue();
289 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000290
291 // Inspect the pattern.
292 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
293 if (!InstPatNode) continue;
294 if (InstPatNode->isLeaf()) continue;
295
296 Record *InstPatOp = InstPatNode->getOperator();
297 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000298 MVT::SimpleValueType RetVT = InstPatNode->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 MVT::SimpleValueType VT = RetVT;
Owen Andersonabb1f162008-08-26 01:22:59 +0000300 if (InstPatNode->getNumChildren())
Chris Lattner2cacec52010-03-15 06:00:16 +0000301 VT = InstPatNode->getChild(0)->getType();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000302
303 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000304 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000305 if (InstPatOp->isSubClassOf("Operand"))
306 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000307
308 // For now, filter out any instructions with predicates.
Dan Gohman0540e172008-10-15 06:17:21 +0000309 if (!InstPatNode->getPredicateFns().empty())
Dan Gohmanf4137b52008-08-19 20:30:54 +0000310 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000311
Dan Gohman379cad42008-08-19 20:36:33 +0000312 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000313 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000314 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000315 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000316
317 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
318 if (!InstPatNode->isLeaf() &&
319 (InstPatNode->getOperator()->getName() == "imm" ||
320 InstPatNode->getOperator()->getName() == "fpimmm"))
321 PhysRegInputs->push_back("");
322 else if (!InstPatNode->isLeaf()) {
323 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
324 TreePatternNode *Op = InstPatNode->getChild(i);
325 if (!Op->isLeaf()) {
326 PhysRegInputs->push_back("");
327 continue;
328 }
329
330 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
331 Record *OpLeafRec = OpDI->getDef();
332 std::string PhysReg;
333 if (OpLeafRec->isSubClassOf("Register")) {
334 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
335 "Namespace")->getValue())->getValue();
336 PhysReg += "::";
337
338 std::vector<CodeGenRegister> Regs = Target.getRegisters();
339 for (unsigned i = 0; i < Regs.size(); ++i) {
340 if (Regs[i].TheDef == OpLeafRec) {
341 PhysReg += Regs[i].getName();
342 break;
343 }
344 }
345 }
346
347 PhysRegInputs->push_back(PhysReg);
348 }
349 } else
350 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000351
Dan Gohman22bb3112008-08-22 00:20:26 +0000352 // Get the predicate that guards this pattern.
353 std::string PredicateCheck = Pattern.getPredicateCheck();
354
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000355 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000356 InstructionMemo Memo = {
357 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000358 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000359 SubRegNo,
360 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000361 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000362 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000363 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000364 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000365 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000366}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000367
Daniel Dunbar1a551802009-07-03 00:10:29 +0000368void FastISelMap::PrintFunctionDefinitions(raw_ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000369 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000370 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000371 OE = SimplePatterns.end(); OI != OE; ++OI) {
372 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000373 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000374
Owen Anderson7b2e5792008-08-25 23:43:09 +0000375 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000376 I != E; ++I) {
377 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000378 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000379
380 OS << "// FastEmit functions for " << Opcode << ".\n";
381 OS << "\n";
382
383 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000384 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000385 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000387 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000388 if (RM.size() != 1) {
389 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
390 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000392 const PredMap &PM = RI->second;
393 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000394
Evan Chengc3f44b02008-09-03 00:03:49 +0000395 OS << "unsigned FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000396 << getLegalCName(Opcode)
397 << "_" << getLegalCName(getName(VT))
398 << "_" << getLegalCName(getName(RetVT)) << "_";
399 Operands.PrintManglingSuffix(OS);
400 OS << "(";
401 Operands.PrintParameters(OS);
402 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000403
Owen Anderson71669e52008-08-26 00:42:26 +0000404 // Emit code for each possible instruction. There may be
405 // multiple if there are subtarget concerns.
406 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
407 PI != PE; ++PI) {
408 std::string PredicateCheck = PI->first;
409 const InstructionMemo &Memo = PI->second;
410
411 if (PredicateCheck.empty()) {
412 assert(!HasPred &&
413 "Multiple instructions match, at least one has "
414 "a predicate and at least one doesn't!");
415 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000416 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000417 OS << " ";
418 HasPred = true;
419 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000420
421 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
422 if ((*Memo.PhysRegs)[i] != "")
423 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
424 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
425 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
426 << (*Memo.PhysRegs)[i] << "), "
427 << "MRI.getRegClass(Op" << i << "));\n";
428 }
429
Owen Anderson71669e52008-08-26 00:42:26 +0000430 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000431 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000432 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000433 OS << "(" << InstNS << Memo.Name << ", ";
434 OS << InstNS << Memo.RC->getName() << "RegisterClass";
435 if (!Operands.empty())
436 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000437 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000438 OS << ");\n";
439 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000440 OS << "extractsubreg(" << getName(RetVT);
441 OS << ", Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000442 OS << (unsigned)Memo.SubRegNo;
443 OS << ");\n";
444 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000445
446 if (HasPred)
Evan Chengd07b46e2008-09-07 08:23:06 +0000447 OS << " }\n";
Owen Anderson667d8f72008-08-29 17:45:56 +0000448
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);
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 OS << "(MVT RetVT";
Owen Anderson71669e52008-08-26 00:42:26 +0000463 if (!Operands.empty())
464 OS << ", ";
465 Operands.PrintParameters(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000467 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
468 RI != RE; ++RI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 MVT::SimpleValueType RetVT = RI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000470 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 Anderson825b72b2009-08-11 20:47:22 +0000486 OS << "(MVT RetVT";
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000487 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 Anderson825b72b2009-08-11 20:47:22 +0000492 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
Owen Anderson70647e82008-08-26 18:50:00 +0000493 << ")\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.
Evan Cheng98d2d072008-09-08 08:39:33 +0000500 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
501 ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000502 std::string PredicateCheck = PI->first;
503 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000504
Owen Anderson7b2e5792008-08-25 23:43:09 +0000505 if (PredicateCheck.empty()) {
506 assert(!HasPred &&
507 "Multiple instructions match, at least one has "
508 "a predicate and at least one doesn't!");
509 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000510 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000511 OS << " ";
512 HasPred = true;
513 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000514
515 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
516 if ((*Memo.PhysRegs)[i] != "")
517 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
518 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
519 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
520 << (*Memo.PhysRegs)[i] << "), "
521 << "MRI.getRegClass(Op" << i << "));\n";
522 }
523
Owen Anderson7b2e5792008-08-25 23:43:09 +0000524 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000525
526 if (Memo.SubRegNo == (unsigned char)~0) {
Evan Cheng98d2d072008-09-08 08:39:33 +0000527 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000528 OS << "(" << InstNS << Memo.Name << ", ";
529 OS << InstNS << Memo.RC->getName() << "RegisterClass";
530 if (!Operands.empty())
531 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000532 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000533 OS << ");\n";
534 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000535 OS << "extractsubreg(RetVT, Op0, ";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000536 OS << (unsigned)Memo.SubRegNo;
537 OS << ");\n";
538 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000539
540 if (HasPred)
541 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000542 }
Owen Anderson71669e52008-08-26 00:42:26 +0000543
Owen Anderson7b2e5792008-08-25 23:43:09 +0000544 // Return 0 if none of the predicates were satisfied.
545 if (HasPred)
546 OS << " return 0;\n";
547 OS << "}\n";
548 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000549 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000550 }
551
552 // Emit one function for the opcode that demultiplexes based on the type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000553 OS << "unsigned FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000554 << getLegalCName(Opcode) << "_";
555 Operands.PrintManglingSuffix(OS);
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 OS << "(MVT VT, MVT RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000557 if (!Operands.empty())
558 OS << ", ";
559 Operands.PrintParameters(OS);
560 OS << ") {\n";
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 OS << " switch (VT.SimpleTy) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000562 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000563 TI != TE; ++TI) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 MVT::SimpleValueType VT = TI->first;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000565 std::string TypeName = getName(VT);
566 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000567 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
568 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000569 OS << "(RetVT";
570 if (!Operands.empty())
571 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000572 Operands.PrintArguments(OS);
573 OS << ");\n";
574 }
575 OS << " default: return 0;\n";
576 OS << " }\n";
577 OS << "}\n";
578 OS << "\n";
579 }
580
Dan Gohman0bfb7522008-08-22 00:28:15 +0000581 OS << "// Top-level FastEmit function.\n";
582 OS << "\n";
583
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000584 // Emit one function for the operand signature that demultiplexes based
585 // on opcode and type.
Evan Chengc3f44b02008-09-03 00:03:49 +0000586 OS << "unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000587 Operands.PrintManglingSuffix(OS);
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000588 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000589 if (!Operands.empty())
590 OS << ", ";
591 Operands.PrintParameters(OS);
592 OS << ") {\n";
593 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000594 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000595 I != E; ++I) {
596 const std::string &Opcode = I->first;
597
598 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000599 << getLegalCName(Opcode) << "_";
600 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000601 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000602 if (!Operands.empty())
603 OS << ", ";
604 Operands.PrintArguments(OS);
605 OS << ");\n";
606 }
607 OS << " default: return 0;\n";
608 OS << " }\n";
609 OS << "}\n";
610 OS << "\n";
611 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000612}
613
Daniel Dunbar1a551802009-07-03 00:10:29 +0000614void FastISelEmitter::run(raw_ostream &OS) {
Dan Gohman72d63af2008-08-26 21:21:20 +0000615 const CodeGenTarget &Target = CGP.getTargetInfo();
616
617 // Determine the target's namespace name.
618 std::string InstNS = Target.getInstNamespace() + "::";
619 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
620
621 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
622 Target.getName() + " target", OS);
623
Dan Gohman72d63af2008-08-26 21:21:20 +0000624 FastISelMap F(InstNS);
625 F.CollectPatterns(CGP);
Dan Gohman72d63af2008-08-26 21:21:20 +0000626 F.PrintFunctionDefinitions(OS);
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000627}
628
629FastISelEmitter::FastISelEmitter(RecordKeeper &R)
630 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000631 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000632}
Dan Gohman72d63af2008-08-26 21:21:20 +0000633