blob: 305158f471415495b3871e173cec7d05c60591d3 [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
Dan Gohman04b7dfb2008-08-19 18:06:12 +000047/// OperandsSignature - This class holds a description of a list of operand
48/// types. It has utility methods for emitting text based on the operands.
49///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000050struct OperandsSignature {
51 std::vector<std::string> Operands;
52
53 bool operator<(const OperandsSignature &O) const {
54 return Operands < O.Operands;
55 }
56
57 bool empty() const { return Operands.empty(); }
58
Dan Gohmand1d2ee82008-08-19 20:56:30 +000059 /// initialize - Examine the given pattern and initialize the contents
60 /// of the Operands array accordingly. Return true if all the operands
61 /// are supported, false otherwise.
62 ///
63 bool initialize(TreePatternNode *InstPatNode,
64 const CodeGenTarget &Target,
Owen Andersonabb1f162008-08-26 01:22:59 +000065 MVT::SimpleValueType VT) {
Owen Anderson6d0c25e2008-08-25 20:20:32 +000066 if (!InstPatNode->isLeaf() &&
67 InstPatNode->getOperator()->getName() == "imm") {
68 Operands.push_back("i");
69 return true;
70 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000071 if (!InstPatNode->isLeaf() &&
72 InstPatNode->getOperator()->getName() == "fpimm") {
73 Operands.push_back("f");
74 return true;
75 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000076
Owen Andersonabb1f162008-08-26 01:22:59 +000077 const CodeGenRegisterClass *DstRC = 0;
78
Dan Gohmand1d2ee82008-08-19 20:56:30 +000079 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
80 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000081 // For now, filter out any operand with a predicate.
82 if (!Op->getPredicateFn().empty())
83 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000084 // For now, filter out any operand with multiple values.
85 if (Op->getExtTypes().size() != 1)
86 return false;
87 // For now, all the operands must have the same type.
88 if (Op->getTypeNum(0) != VT)
89 return false;
90 if (!Op->isLeaf()) {
91 if (Op->getOperator()->getName() == "imm") {
92 Operands.push_back("i");
93 return true;
94 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000095 if (Op->getOperator()->getName() == "fpimm") {
96 Operands.push_back("f");
97 return true;
98 }
Dan Gohman833ddf82008-08-27 16:18:22 +000099 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000100 return false;
101 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000102 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
103 if (!OpDI)
104 return false;
105 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000106 // TODO: handle instructions which have physreg operands.
107 if (OpLeafRec->isSubClassOf("Register"))
108 return false;
109 // For now, the only other thing we accept is register operands.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000110 if (!OpLeafRec->isSubClassOf("RegisterClass"))
111 return false;
112 // For now, require the register operands' register classes to all
113 // be the same.
114 const CodeGenRegisterClass *RC = &Target.getRegisterClass(OpLeafRec);
115 if (!RC)
116 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000117 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000118 if (DstRC) {
119 if (DstRC != RC)
120 return false;
121 } else
122 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000123 Operands.push_back("r");
124 }
125 return true;
126 }
127
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000128 void PrintParameters(std::ostream &OS) const {
129 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
130 if (Operands[i] == "r") {
131 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000132 } else if (Operands[i] == "i") {
133 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000134 } else if (Operands[i] == "f") {
135 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000136 } else {
137 assert("Unknown operand kind!");
138 abort();
139 }
140 if (i + 1 != e)
141 OS << ", ";
142 }
143 }
144
145 void PrintArguments(std::ostream &OS) const {
146 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
147 if (Operands[i] == "r") {
148 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000149 } else if (Operands[i] == "i") {
150 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000151 } else if (Operands[i] == "f") {
152 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000153 } else {
154 assert("Unknown operand kind!");
155 abort();
156 }
157 if (i + 1 != e)
158 OS << ", ";
159 }
160 }
161
162 void PrintManglingSuffix(std::ostream &OS) const {
163 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
164 OS << Operands[i];
165 }
166 }
167};
168
Dan Gohman04b7dfb2008-08-19 18:06:12 +0000169/// InstructionMemo - This class holds additional information about an
170/// instruction needed to emit code for it.
171///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000172struct InstructionMemo {
173 std::string Name;
174 const CodeGenRegisterClass *RC;
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000175 unsigned char SubRegNo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000176};
177
Dan Gohman72d63af2008-08-26 21:21:20 +0000178class FastISelMap {
179 typedef std::map<std::string, InstructionMemo> PredMap;
180 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
181 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
182 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
183 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
184
185 OperandsOpcodeTypeRetPredMap SimplePatterns;
186
187 std::string InstNS;
188
189public:
190 explicit FastISelMap(std::string InstNS);
191
192 void CollectPatterns(CodeGenDAGPatterns &CGP);
193 void PrintClass(std::ostream &OS);
194 void PrintFunctionDefinitions(std::ostream &OS);
195};
196
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000197}
198
199static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
200 return CGP.getSDNodeInfo(Op).getEnumName();
201}
202
203static std::string getLegalCName(std::string OpName) {
204 std::string::size_type pos = OpName.find("::");
205 if (pos != std::string::npos)
206 OpName.replace(pos, 2, "_");
207 return OpName;
208}
209
Dan Gohman72d63af2008-08-26 21:21:20 +0000210FastISelMap::FastISelMap(std::string instns)
211 : InstNS(instns) {
212}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000213
Dan Gohman72d63af2008-08-26 21:21:20 +0000214void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
215 const CodeGenTarget &Target = CGP.getTargetInfo();
216
217 // Determine the target's namespace name.
218 InstNS = Target.getInstNamespace() + "::";
219 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000220
Dan Gohman0bfb7522008-08-22 00:28:15 +0000221 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000222 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
223 E = CGP.ptm_end(); I != E; ++I) {
224 const PatternToMatch &Pattern = *I;
225
226 // For now, just look at Instructions, so that we don't have to worry
227 // about emitting multiple instructions for a pattern.
228 TreePatternNode *Dst = Pattern.getDstPattern();
229 if (Dst->isLeaf()) continue;
230 Record *Op = Dst->getOperator();
231 if (!Op->isSubClassOf("Instruction"))
232 continue;
233 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
234 if (II.OperandList.empty())
235 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000236
237 // For now, ignore instructions where the first operand is not an
238 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000239 const CodeGenRegisterClass *DstRC = 0;
240 unsigned SubRegNo = ~0;
241 if (Op->getName() != "EXTRACT_SUBREG") {
242 Record *Op0Rec = II.OperandList[0].Rec;
243 if (!Op0Rec->isSubClassOf("RegisterClass"))
244 continue;
245 DstRC = &Target.getRegisterClass(Op0Rec);
246 if (!DstRC)
247 continue;
248 } else {
249 SubRegNo = static_cast<IntInit*>(
250 Dst->getChild(1)->getLeafValue())->getValue();
251 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000252
253 // Inspect the pattern.
254 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
255 if (!InstPatNode) continue;
256 if (InstPatNode->isLeaf()) continue;
257
258 Record *InstPatOp = InstPatNode->getOperator();
259 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Owen Andersonabb1f162008-08-26 01:22:59 +0000260 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
261 MVT::SimpleValueType VT = RetVT;
262 if (InstPatNode->getNumChildren())
263 VT = InstPatNode->getChild(0)->getTypeNum(0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000264
265 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000266 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000267 if (InstPatOp->isSubClassOf("Operand"))
268 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000269
270 // For now, filter out any instructions with predicates.
271 if (!InstPatNode->getPredicateFn().empty())
272 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000273
Dan Gohman379cad42008-08-19 20:36:33 +0000274 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000275 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000276 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000277 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000278
Dan Gohman22bb3112008-08-22 00:20:26 +0000279 // Get the predicate that guards this pattern.
280 std::string PredicateCheck = Pattern.getPredicateCheck();
281
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000282 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000283 InstructionMemo Memo = {
284 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000285 DstRC,
286 SubRegNo
Dan Gohman520b50c2008-08-21 00:35:26 +0000287 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000288 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000289 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000290 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000291 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000292}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000293
Dan Gohman72d63af2008-08-26 21:21:20 +0000294void FastISelMap::PrintClass(std::ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000295 // Declare the target FastISel class.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000296 OS << "class FastISel : public llvm::FastISel {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000297 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000298 OE = SimplePatterns.end(); OI != OE; ++OI) {
299 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000300 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000301
Owen Anderson7b2e5792008-08-25 23:43:09 +0000302 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000303 I != E; ++I) {
304 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000305 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000306
Owen Anderson7b2e5792008-08-25 23:43:09 +0000307 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000308 TI != TE; ++TI) {
309 MVT::SimpleValueType VT = TI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000310 const RetPredMap &RM = TI->second;
311
312 if (RM.size() != 1)
313 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
314 RI != RE; ++RI) {
315 MVT::SimpleValueType RetVT = RI->first;
316 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
317 << "_" << getLegalCName(getName(VT)) << "_"
318 << getLegalCName(getName(RetVT)) << "_";
319 Operands.PrintManglingSuffix(OS);
320 OS << "(";
321 Operands.PrintParameters(OS);
322 OS << ");\n";
323 }
324
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000325 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000326 << "_" << getLegalCName(getName(VT)) << "_";
327 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000328 OS << "(MVT::SimpleValueType RetVT";
329 if (!Operands.empty())
330 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000331 Operands.PrintParameters(OS);
332 OS << ");\n";
333 }
334
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000335 OS << " unsigned FastEmit_" << getLegalCName(Opcode) << "_";
336 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000337 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000338 if (!Operands.empty())
339 OS << ", ";
340 Operands.PrintParameters(OS);
341 OS << ");\n";
342 }
343
Dan Gohman56e0f872008-08-19 20:31:38 +0000344 OS << " unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000345 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000346 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000347 if (!Operands.empty())
348 OS << ", ";
349 Operands.PrintParameters(OS);
350 OS << ");\n";
351 }
Dan Gohman22bb3112008-08-22 00:20:26 +0000352 OS << "\n";
353
354 // Declare the Subtarget member, which is used for predicate checks.
355 OS << " const " << InstNS.substr(0, InstNS.size() - 2)
356 << "Subtarget *Subtarget;\n";
357 OS << "\n";
358
359 // Declare the constructor.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000360 OS << "public:\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000361 OS << " explicit FastISel(MachineFunction &mf)\n";
362 OS << " : llvm::FastISel(mf),\n";
363 OS << " Subtarget(&TM.getSubtarget<" << InstNS.substr(0, InstNS.size() - 2)
364 << "Subtarget>()) {}\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000365 OS << "};\n";
366 OS << "\n";
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
Owen Anderson71669e52008-08-26 00:42:26 +0000396 OS << "unsigned FastISel::FastEmit_"
397 << 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 {
417 OS << " if (" + PredicateCheck + ")\n";
418 OS << " ";
419 HasPred = true;
420 }
421 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000422 if (Memo.SubRegNo == (unsigned char)~0) {
423 Operands.PrintManglingSuffix(OS);
424 OS << "(" << InstNS << Memo.Name << ", ";
425 OS << InstNS << Memo.RC->getName() << "RegisterClass";
426 if (!Operands.empty())
427 OS << ", ";
428 Operands.PrintArguments(OS);
429 OS << ");\n";
430 } else {
431 OS << "extractsubreg(Op0, ";
432 OS << (unsigned)Memo.SubRegNo;
433 OS << ");\n";
434 }
Owen Anderson71669e52008-08-26 00:42:26 +0000435 }
436 // Return 0 if none of the predicates were satisfied.
437 if (HasPred)
438 OS << " return 0;\n";
439 OS << "}\n";
440 OS << "\n";
441 }
442
443 // Emit one function for the type that demultiplexes on return type.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000444 OS << "unsigned FastISel::FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000445 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000446 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000447 Operands.PrintManglingSuffix(OS);
448 OS << "(MVT::SimpleValueType RetVT";
449 if (!Operands.empty())
450 OS << ", ";
451 Operands.PrintParameters(OS);
452 OS << ") {\nswitch (RetVT) {\n";
453 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
454 RI != RE; ++RI) {
455 MVT::SimpleValueType RetVT = RI->first;
456 OS << " case " << getName(RetVT) << ": return FastEmit_"
457 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
458 << "_" << getLegalCName(getName(RetVT)) << "_";
459 Operands.PrintManglingSuffix(OS);
460 OS << "(";
461 Operands.PrintArguments(OS);
462 OS << ");\n";
463 }
464 OS << " default: return 0;\n}\n}\n\n";
465
466 } else {
467 // Non-variadic return type.
468 OS << "unsigned FastISel::FastEmit_"
469 << getLegalCName(Opcode) << "_"
470 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000471 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000472 OS << "(MVT::SimpleValueType RetVT";
473 if (!Operands.empty())
474 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000475 Operands.PrintParameters(OS);
476 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000477
Owen Anderson70647e82008-08-26 18:50:00 +0000478 OS << " if (RetVT != " << getName(RM.begin()->first)
479 << ")\n return 0;\n";
480
Owen Anderson71669e52008-08-26 00:42:26 +0000481 const PredMap &PM = RM.begin()->second;
482 bool HasPred = false;
483
Owen Anderson7b2e5792008-08-25 23:43:09 +0000484 // Emit code for each possible instruction. There may be
485 // multiple if there are subtarget concerns.
Owen Anderson71669e52008-08-26 00:42:26 +0000486 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000487 std::string PredicateCheck = PI->first;
488 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000489
Owen Anderson7b2e5792008-08-25 23:43:09 +0000490 if (PredicateCheck.empty()) {
491 assert(!HasPred &&
492 "Multiple instructions match, at least one has "
493 "a predicate and at least one doesn't!");
494 } else {
495 OS << " if (" + PredicateCheck + ")\n";
496 OS << " ";
497 HasPred = true;
498 }
499 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000500
501 if (Memo.SubRegNo == (unsigned char)~0) {
502 Operands.PrintManglingSuffix(OS);
503 OS << "(" << InstNS << Memo.Name << ", ";
504 OS << InstNS << Memo.RC->getName() << "RegisterClass";
505 if (!Operands.empty())
506 OS << ", ";
507 Operands.PrintArguments(OS);
508 OS << ");\n";
509 } else {
510 OS << "extractsubreg(Op0, ";
511 OS << (unsigned)Memo.SubRegNo;
512 OS << ");\n";
513 }
Owen Anderson7b2e5792008-08-25 23:43:09 +0000514 }
Owen Anderson71669e52008-08-26 00:42:26 +0000515
Owen Anderson7b2e5792008-08-25 23:43:09 +0000516 // Return 0 if none of the predicates were satisfied.
517 if (HasPred)
518 OS << " return 0;\n";
519 OS << "}\n";
520 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000521 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000522 }
523
524 // Emit one function for the opcode that demultiplexes based on the type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000525 OS << "unsigned FastISel::FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000526 << getLegalCName(Opcode) << "_";
527 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000528 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000529 if (!Operands.empty())
530 OS << ", ";
531 Operands.PrintParameters(OS);
532 OS << ") {\n";
533 OS << " switch (VT) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000534 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000535 TI != TE; ++TI) {
536 MVT::SimpleValueType VT = TI->first;
537 std::string TypeName = getName(VT);
538 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000539 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
540 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000541 OS << "(RetVT";
542 if (!Operands.empty())
543 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000544 Operands.PrintArguments(OS);
545 OS << ");\n";
546 }
547 OS << " default: return 0;\n";
548 OS << " }\n";
549 OS << "}\n";
550 OS << "\n";
551 }
552
Dan Gohman0bfb7522008-08-22 00:28:15 +0000553 OS << "// Top-level FastEmit function.\n";
554 OS << "\n";
555
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000556 // Emit one function for the operand signature that demultiplexes based
557 // on opcode and type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000558 OS << "unsigned FastISel::FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000559 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000560 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000561 if (!Operands.empty())
562 OS << ", ";
563 Operands.PrintParameters(OS);
564 OS << ") {\n";
565 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000566 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000567 I != E; ++I) {
568 const std::string &Opcode = I->first;
569
570 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000571 << getLegalCName(Opcode) << "_";
572 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000573 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000574 if (!Operands.empty())
575 OS << ", ";
576 Operands.PrintArguments(OS);
577 OS << ");\n";
578 }
579 OS << " default: return 0;\n";
580 OS << " }\n";
581 OS << "}\n";
582 OS << "\n";
583 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000584}
585
586void FastISelEmitter::run(std::ostream &OS) {
587 const CodeGenTarget &Target = CGP.getTargetInfo();
588
589 // Determine the target's namespace name.
590 std::string InstNS = Target.getInstNamespace() + "::";
591 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
592
593 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
594 Target.getName() + " target", OS);
595
596 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
597 OS << "\n";
598 OS << "namespace llvm {\n";
599 OS << "\n";
600 OS << "namespace " << InstNS.substr(0, InstNS.size() - 2) << " {\n";
601 OS << "\n";
602
603 FastISelMap F(InstNS);
604 F.CollectPatterns(CGP);
605 F.PrintClass(OS);
606 F.PrintFunctionDefinitions(OS);
607
608 // Define the target FastISel creation function.
609 OS << "llvm::FastISel *createFastISel(MachineFunction &mf) {\n";
610 OS << " return new FastISel(mf);\n";
611 OS << "}\n";
612 OS << "\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000613
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000614 OS << "} // namespace X86\n";
615 OS << "\n";
616 OS << "} // namespace llvm\n";
617}
618
619FastISelEmitter::FastISelEmitter(RecordKeeper &R)
620 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000621 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000622}
Dan Gohman72d63af2008-08-26 21:21:20 +0000623