blob: 4f0124310400bc225924bc73882d8409a9e0c271 [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001//===- FastISelEmitter.cpp - Generate an instruction selector -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a "fast" instruction selector.
11//
12// This instruction selection method is designed to emit very poor code
13// quickly. Also, it is not designed to do much lowering, so most illegal
14// types (e.g. i64 on 32-bit targets) and operations (e.g. calls) are not
15// supported and cannot easily be added. Blocks containing operations
16// that are not supported need to be handled by a more capable selector,
17// such as the SelectionDAG selector.
18//
19// The intended use for "fast" instruction selection is "-O0" mode
20// compilation, where the quality of the generated code is irrelevant when
21// weighed against the speed at which the code can be generated.
22//
23// If compile time is so important, you might wonder why we don't just
24// skip codegen all-together, emit LLVM bytecode files, and execute them
25// with an interpreter. The answer is that it would complicate linking and
26// debugging, and also because that isn't how a compiler is expected to
27// work in some circles.
28//
29// If you need better generated code or more lowering than what this
30// instruction selector provides, use the SelectionDAG (DAGISel) instruction
31// selector instead. If you're looking here because SelectionDAG isn't fast
32// enough, consider looking into improving the SelectionDAG infastructure
33// instead. At the time of this writing there remain several major
34// opportunities for improvement.
35//
36//===----------------------------------------------------------------------===//
37
38#include "FastISelEmitter.h"
39#include "Record.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/Streams.h"
42#include "llvm/ADT/VectorExtras.h"
43using namespace llvm;
44
45namespace {
46
Owen Anderson667d8f72008-08-29 17:45:56 +000047/// InstructionMemo - This class holds additional information about an
48/// instruction needed to emit code for it.
49///
50struct InstructionMemo {
51 std::string Name;
52 const CodeGenRegisterClass *RC;
53 unsigned char SubRegNo;
54 std::vector<std::string>* PhysRegs;
55};
56
Dan Gohman04b7dfb2008-08-19 18:06:12 +000057/// OperandsSignature - This class holds a description of a list of operand
58/// types. It has utility methods for emitting text based on the operands.
59///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000060struct OperandsSignature {
61 std::vector<std::string> Operands;
62
63 bool operator<(const OperandsSignature &O) const {
64 return Operands < O.Operands;
65 }
66
67 bool empty() const { return Operands.empty(); }
68
Dan Gohmand1d2ee82008-08-19 20:56:30 +000069 /// initialize - Examine the given pattern and initialize the contents
70 /// of the Operands array accordingly. Return true if all the operands
71 /// are supported, false otherwise.
72 ///
73 bool initialize(TreePatternNode *InstPatNode,
74 const CodeGenTarget &Target,
Owen Andersonabb1f162008-08-26 01:22:59 +000075 MVT::SimpleValueType VT) {
Owen Anderson6d0c25e2008-08-25 20:20:32 +000076 if (!InstPatNode->isLeaf() &&
77 InstPatNode->getOperator()->getName() == "imm") {
78 Operands.push_back("i");
79 return true;
80 }
Dan Gohman10df0fa2008-08-27 01:09:54 +000081 if (!InstPatNode->isLeaf() &&
82 InstPatNode->getOperator()->getName() == "fpimm") {
83 Operands.push_back("f");
84 return true;
85 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +000086
Owen Andersonabb1f162008-08-26 01:22:59 +000087 const CodeGenRegisterClass *DstRC = 0;
88
Dan Gohmand1d2ee82008-08-19 20:56:30 +000089 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
90 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000091 // For now, filter out any operand with a predicate.
92 if (!Op->getPredicateFn().empty())
93 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000094 // For now, filter out any operand with multiple values.
95 if (Op->getExtTypes().size() != 1)
96 return false;
97 // For now, all the operands must have the same type.
98 if (Op->getTypeNum(0) != VT)
99 return false;
100 if (!Op->isLeaf()) {
101 if (Op->getOperator()->getName() == "imm") {
102 Operands.push_back("i");
103 return true;
104 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000105 if (Op->getOperator()->getName() == "fpimm") {
106 Operands.push_back("f");
107 return true;
108 }
Dan Gohman833ddf82008-08-27 16:18:22 +0000109 // For now, ignore other non-leaf nodes.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000110 return false;
111 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000112 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
113 if (!OpDI)
114 return false;
115 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000116 // For now, the only other thing we accept is register operands.
Owen Anderson667d8f72008-08-29 17:45:56 +0000117
118 const CodeGenRegisterClass *RC = 0;
119 if (OpLeafRec->isSubClassOf("RegisterClass"))
120 RC = &Target.getRegisterClass(OpLeafRec);
121 else if (OpLeafRec->isSubClassOf("Register"))
122 RC = Target.getRegisterClassForRegister(OpLeafRec);
123 else
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000124 return false;
125 // For now, require the register operands' register classes to all
126 // be the same.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000127 if (!RC)
128 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000129 // For now, all the operands must have the same register class.
Owen Andersonabb1f162008-08-26 01:22:59 +0000130 if (DstRC) {
131 if (DstRC != RC)
132 return false;
133 } else
134 DstRC = RC;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000135 Operands.push_back("r");
136 }
137 return true;
138 }
139
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000140 void PrintParameters(std::ostream &OS) const {
141 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
142 if (Operands[i] == "r") {
143 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000144 } else if (Operands[i] == "i") {
145 OS << "uint64_t imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000146 } else if (Operands[i] == "f") {
147 OS << "ConstantFP *f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000148 } else {
149 assert("Unknown operand kind!");
150 abort();
151 }
152 if (i + 1 != e)
153 OS << ", ";
154 }
155 }
156
Owen Anderson667d8f72008-08-29 17:45:56 +0000157 void PrintArguments(std::ostream &OS,
158 const std::vector<std::string>& PR) const {
159 assert(PR.size() == Operands.size());
160 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
161 if (PR[i] != "") {
162 OS << PR[i];
163 } else if (Operands[i] == "r") {
164 OS << "Op" << i;
165 } else if (Operands[i] == "i") {
166 OS << "imm" << i;
167 } else if (Operands[i] == "f") {
168 OS << "f" << i;
169 } else {
170 assert("Unknown operand kind!");
171 abort();
172 }
173 if (i + 1 != e)
174 OS << ", ";
175 }
176 }
177
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000178 void PrintArguments(std::ostream &OS) const {
179 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
180 if (Operands[i] == "r") {
181 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000182 } else if (Operands[i] == "i") {
183 OS << "imm" << i;
Dan Gohman10df0fa2008-08-27 01:09:54 +0000184 } else if (Operands[i] == "f") {
185 OS << "f" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000186 } else {
187 assert("Unknown operand kind!");
188 abort();
189 }
190 if (i + 1 != e)
191 OS << ", ";
192 }
193 }
194
Owen Anderson667d8f72008-08-29 17:45:56 +0000195
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000196 void PrintManglingSuffix(std::ostream &OS) const {
197 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
198 OS << Operands[i];
199 }
200 }
201};
202
Dan Gohman72d63af2008-08-26 21:21:20 +0000203class FastISelMap {
204 typedef std::map<std::string, InstructionMemo> PredMap;
205 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
206 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
207 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
208 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
209
210 OperandsOpcodeTypeRetPredMap SimplePatterns;
211
212 std::string InstNS;
213
214public:
215 explicit FastISelMap(std::string InstNS);
216
217 void CollectPatterns(CodeGenDAGPatterns &CGP);
218 void PrintClass(std::ostream &OS);
219 void PrintFunctionDefinitions(std::ostream &OS);
220};
221
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000222}
223
224static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
225 return CGP.getSDNodeInfo(Op).getEnumName();
226}
227
228static std::string getLegalCName(std::string OpName) {
229 std::string::size_type pos = OpName.find("::");
230 if (pos != std::string::npos)
231 OpName.replace(pos, 2, "_");
232 return OpName;
233}
234
Dan Gohman72d63af2008-08-26 21:21:20 +0000235FastISelMap::FastISelMap(std::string instns)
236 : InstNS(instns) {
237}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000238
Dan Gohman72d63af2008-08-26 21:21:20 +0000239void FastISelMap::CollectPatterns(CodeGenDAGPatterns &CGP) {
240 const CodeGenTarget &Target = CGP.getTargetInfo();
241
242 // Determine the target's namespace name.
243 InstNS = Target.getInstNamespace() + "::";
244 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000245
Dan Gohman0bfb7522008-08-22 00:28:15 +0000246 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000247 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
248 E = CGP.ptm_end(); I != E; ++I) {
249 const PatternToMatch &Pattern = *I;
250
251 // For now, just look at Instructions, so that we don't have to worry
252 // about emitting multiple instructions for a pattern.
253 TreePatternNode *Dst = Pattern.getDstPattern();
254 if (Dst->isLeaf()) continue;
255 Record *Op = Dst->getOperator();
256 if (!Op->isSubClassOf("Instruction"))
257 continue;
258 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
259 if (II.OperandList.empty())
260 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000261
262 // For now, ignore instructions where the first operand is not an
263 // output register.
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000264 const CodeGenRegisterClass *DstRC = 0;
265 unsigned SubRegNo = ~0;
266 if (Op->getName() != "EXTRACT_SUBREG") {
267 Record *Op0Rec = II.OperandList[0].Rec;
268 if (!Op0Rec->isSubClassOf("RegisterClass"))
269 continue;
270 DstRC = &Target.getRegisterClass(Op0Rec);
271 if (!DstRC)
272 continue;
273 } else {
274 SubRegNo = static_cast<IntInit*>(
275 Dst->getChild(1)->getLeafValue())->getValue();
276 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000277
278 // Inspect the pattern.
279 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
280 if (!InstPatNode) continue;
281 if (InstPatNode->isLeaf()) continue;
282
283 Record *InstPatOp = InstPatNode->getOperator();
284 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
Owen Andersonabb1f162008-08-26 01:22:59 +0000285 MVT::SimpleValueType RetVT = InstPatNode->getTypeNum(0);
286 MVT::SimpleValueType VT = RetVT;
287 if (InstPatNode->getNumChildren())
288 VT = InstPatNode->getChild(0)->getTypeNum(0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000289
290 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000291 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000292 if (InstPatOp->isSubClassOf("Operand"))
293 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000294
295 // For now, filter out any instructions with predicates.
296 if (!InstPatNode->getPredicateFn().empty())
297 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000298
Dan Gohman379cad42008-08-19 20:36:33 +0000299 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000300 OperandsSignature Operands;
Owen Andersonabb1f162008-08-26 01:22:59 +0000301 if (!Operands.initialize(InstPatNode, Target, VT))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000302 continue;
Owen Anderson667d8f72008-08-29 17:45:56 +0000303
304 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
305 if (!InstPatNode->isLeaf() &&
306 (InstPatNode->getOperator()->getName() == "imm" ||
307 InstPatNode->getOperator()->getName() == "fpimmm"))
308 PhysRegInputs->push_back("");
309 else if (!InstPatNode->isLeaf()) {
310 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
311 TreePatternNode *Op = InstPatNode->getChild(i);
312 if (!Op->isLeaf()) {
313 PhysRegInputs->push_back("");
314 continue;
315 }
316
317 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
318 Record *OpLeafRec = OpDI->getDef();
319 std::string PhysReg;
320 if (OpLeafRec->isSubClassOf("Register")) {
321 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
322 "Namespace")->getValue())->getValue();
323 PhysReg += "::";
324
325 std::vector<CodeGenRegister> Regs = Target.getRegisters();
326 for (unsigned i = 0; i < Regs.size(); ++i) {
327 if (Regs[i].TheDef == OpLeafRec) {
328 PhysReg += Regs[i].getName();
329 break;
330 }
331 }
332 }
333
334 PhysRegInputs->push_back(PhysReg);
335 }
336 } else
337 PhysRegInputs->push_back("");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000338
Dan Gohman22bb3112008-08-22 00:20:26 +0000339 // Get the predicate that guards this pattern.
340 std::string PredicateCheck = Pattern.getPredicateCheck();
341
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000342 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000343 InstructionMemo Memo = {
344 Pattern.getDstPattern()->getOperator()->getName(),
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000345 DstRC,
Owen Anderson667d8f72008-08-29 17:45:56 +0000346 SubRegNo,
347 PhysRegInputs
Dan Gohman520b50c2008-08-21 00:35:26 +0000348 };
Owen Andersonabb1f162008-08-26 01:22:59 +0000349 assert(!SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000350 "Duplicate pattern!");
Owen Andersonabb1f162008-08-26 01:22:59 +0000351 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000352 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000353}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000354
Dan Gohman72d63af2008-08-26 21:21:20 +0000355void FastISelMap::PrintClass(std::ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000356 // Declare the target FastISel class.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000357 OS << "class FastISel : public llvm::FastISel {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000358 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000359 OE = SimplePatterns.end(); OI != OE; ++OI) {
360 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000361 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000362
Owen Anderson7b2e5792008-08-25 23:43:09 +0000363 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000364 I != E; ++I) {
365 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000366 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000367
Owen Anderson7b2e5792008-08-25 23:43:09 +0000368 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000369 TI != TE; ++TI) {
370 MVT::SimpleValueType VT = TI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000371 const RetPredMap &RM = TI->second;
372
373 if (RM.size() != 1)
374 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
375 RI != RE; ++RI) {
376 MVT::SimpleValueType RetVT = RI->first;
377 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
378 << "_" << getLegalCName(getName(VT)) << "_"
379 << getLegalCName(getName(RetVT)) << "_";
380 Operands.PrintManglingSuffix(OS);
381 OS << "(";
382 Operands.PrintParameters(OS);
383 OS << ");\n";
384 }
385
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000386 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000387 << "_" << getLegalCName(getName(VT)) << "_";
388 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000389 OS << "(MVT::SimpleValueType RetVT";
390 if (!Operands.empty())
391 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000392 Operands.PrintParameters(OS);
393 OS << ");\n";
394 }
395
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000396 OS << " unsigned FastEmit_" << getLegalCName(Opcode) << "_";
397 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000398 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000399 if (!Operands.empty())
400 OS << ", ";
401 Operands.PrintParameters(OS);
402 OS << ");\n";
403 }
404
Dan Gohman56e0f872008-08-19 20:31:38 +0000405 OS << " unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000406 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000407 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000408 if (!Operands.empty())
409 OS << ", ";
410 Operands.PrintParameters(OS);
411 OS << ");\n";
412 }
Dan Gohman22bb3112008-08-22 00:20:26 +0000413 OS << "\n";
414
Dan Gohman99b21822008-08-28 23:21:34 +0000415 OS << "bool TargetSelectInstruction(Instruction *I,\n";
416 OS << " "
417 "DenseMap<const Value *, unsigned> &ValueMap,\n";
418 OS << " "
419 "DenseMap<const BasicBlock *, MachineBasicBlock *> &MBBMap,\n";
420 OS << " "
421 "MachineBasicBlock *MBB);\n";
422
Dan Gohman22bb3112008-08-22 00:20:26 +0000423 // Declare the Subtarget member, which is used for predicate checks.
424 OS << " const " << InstNS.substr(0, InstNS.size() - 2)
425 << "Subtarget *Subtarget;\n";
426 OS << "\n";
427
428 // Declare the constructor.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000429 OS << "public:\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000430 OS << " explicit FastISel(MachineFunction &mf)\n";
431 OS << " : llvm::FastISel(mf),\n";
432 OS << " Subtarget(&TM.getSubtarget<" << InstNS.substr(0, InstNS.size() - 2)
433 << "Subtarget>()) {}\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000434 OS << "};\n";
435 OS << "\n";
Dan Gohman72d63af2008-08-26 21:21:20 +0000436}
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000437
Dan Gohman72d63af2008-08-26 21:21:20 +0000438void FastISelMap::PrintFunctionDefinitions(std::ostream &OS) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000439 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000440 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000441 OE = SimplePatterns.end(); OI != OE; ++OI) {
442 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000443 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000444
Owen Anderson7b2e5792008-08-25 23:43:09 +0000445 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000446 I != E; ++I) {
447 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000448 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000449
450 OS << "// FastEmit functions for " << Opcode << ".\n";
451 OS << "\n";
452
453 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000454 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000455 TI != TE; ++TI) {
456 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000457 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000458 if (RM.size() != 1) {
459 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
460 RI != RE; ++RI) {
461 MVT::SimpleValueType RetVT = RI->first;
462 const PredMap &PM = RI->second;
463 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000464
Owen Anderson71669e52008-08-26 00:42:26 +0000465 OS << "unsigned FastISel::FastEmit_"
466 << getLegalCName(Opcode)
467 << "_" << getLegalCName(getName(VT))
468 << "_" << getLegalCName(getName(RetVT)) << "_";
469 Operands.PrintManglingSuffix(OS);
470 OS << "(";
471 Operands.PrintParameters(OS);
472 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000473
Owen Anderson71669e52008-08-26 00:42:26 +0000474 // Emit code for each possible instruction. There may be
475 // multiple if there are subtarget concerns.
476 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
477 PI != PE; ++PI) {
478 std::string PredicateCheck = PI->first;
479 const InstructionMemo &Memo = PI->second;
480
481 if (PredicateCheck.empty()) {
482 assert(!HasPred &&
483 "Multiple instructions match, at least one has "
484 "a predicate and at least one doesn't!");
485 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000486 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000487 OS << " ";
488 HasPred = true;
489 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000490
491 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
492 if ((*Memo.PhysRegs)[i] != "")
493 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
494 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
495 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
496 << (*Memo.PhysRegs)[i] << "), "
497 << "MRI.getRegClass(Op" << i << "));\n";
498 }
499
Owen Anderson71669e52008-08-26 00:42:26 +0000500 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000501 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 << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000507 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000508 OS << ");\n";
509 } else {
510 OS << "extractsubreg(Op0, ";
511 OS << (unsigned)Memo.SubRegNo;
512 OS << ");\n";
513 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000514
515 if (HasPred)
516 OS << "}\n";
517
Owen Anderson71669e52008-08-26 00:42:26 +0000518 }
519 // Return 0 if none of the predicates were satisfied.
520 if (HasPred)
521 OS << " return 0;\n";
522 OS << "}\n";
523 OS << "\n";
524 }
525
526 // Emit one function for the type that demultiplexes on return type.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000527 OS << "unsigned FastISel::FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000528 << getLegalCName(Opcode) << "_"
Owen Andersonabb1f162008-08-26 01:22:59 +0000529 << getLegalCName(getName(VT)) << "_";
Owen Anderson71669e52008-08-26 00:42:26 +0000530 Operands.PrintManglingSuffix(OS);
531 OS << "(MVT::SimpleValueType RetVT";
532 if (!Operands.empty())
533 OS << ", ";
534 Operands.PrintParameters(OS);
535 OS << ") {\nswitch (RetVT) {\n";
536 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
537 RI != RE; ++RI) {
538 MVT::SimpleValueType RetVT = RI->first;
539 OS << " case " << getName(RetVT) << ": return FastEmit_"
540 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
541 << "_" << getLegalCName(getName(RetVT)) << "_";
542 Operands.PrintManglingSuffix(OS);
543 OS << "(";
544 Operands.PrintArguments(OS);
545 OS << ");\n";
546 }
547 OS << " default: return 0;\n}\n}\n\n";
548
549 } else {
550 // Non-variadic return type.
551 OS << "unsigned FastISel::FastEmit_"
552 << getLegalCName(Opcode) << "_"
553 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000554 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000555 OS << "(MVT::SimpleValueType RetVT";
556 if (!Operands.empty())
557 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000558 Operands.PrintParameters(OS);
559 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000560
Owen Anderson70647e82008-08-26 18:50:00 +0000561 OS << " if (RetVT != " << getName(RM.begin()->first)
562 << ")\n return 0;\n";
563
Owen Anderson71669e52008-08-26 00:42:26 +0000564 const PredMap &PM = RM.begin()->second;
565 bool HasPred = false;
566
Owen Anderson7b2e5792008-08-25 23:43:09 +0000567 // Emit code for each possible instruction. There may be
568 // multiple if there are subtarget concerns.
Owen Anderson71669e52008-08-26 00:42:26 +0000569 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000570 std::string PredicateCheck = PI->first;
571 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000572
Owen Anderson7b2e5792008-08-25 23:43:09 +0000573 if (PredicateCheck.empty()) {
574 assert(!HasPred &&
575 "Multiple instructions match, at least one has "
576 "a predicate and at least one doesn't!");
577 } else {
Owen Anderson667d8f72008-08-29 17:45:56 +0000578 OS << " if (" + PredicateCheck + ") {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000579 OS << " ";
580 HasPred = true;
581 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000582
583 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
584 if ((*Memo.PhysRegs)[i] != "")
585 OS << " TII.copyRegToReg(*MBB, MBB->end(), "
586 << (*Memo.PhysRegs)[i] << ", Op" << i << ", "
587 << "TM.getRegisterInfo()->getPhysicalRegisterRegClass("
588 << (*Memo.PhysRegs)[i] << "), "
589 << "MRI.getRegClass(Op" << i << "));\n";
590 }
591
Owen Anderson7b2e5792008-08-25 23:43:09 +0000592 OS << " return FastEmitInst_";
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000593
594 if (Memo.SubRegNo == (unsigned char)~0) {
595 Operands.PrintManglingSuffix(OS);
596 OS << "(" << InstNS << Memo.Name << ", ";
597 OS << InstNS << Memo.RC->getName() << "RegisterClass";
598 if (!Operands.empty())
599 OS << ", ";
Owen Anderson667d8f72008-08-29 17:45:56 +0000600 Operands.PrintArguments(OS, *Memo.PhysRegs);
Owen Andersonb5dbcb52008-08-28 18:06:12 +0000601 OS << ");\n";
602 } else {
603 OS << "extractsubreg(Op0, ";
604 OS << (unsigned)Memo.SubRegNo;
605 OS << ");\n";
606 }
Owen Anderson667d8f72008-08-29 17:45:56 +0000607
608 if (HasPred)
609 OS << " }\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000610 }
Owen Anderson71669e52008-08-26 00:42:26 +0000611
Owen Anderson7b2e5792008-08-25 23:43:09 +0000612 // Return 0 if none of the predicates were satisfied.
613 if (HasPred)
614 OS << " return 0;\n";
615 OS << "}\n";
616 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000617 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000618 }
619
620 // Emit one function for the opcode that demultiplexes based on the type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000621 OS << "unsigned FastISel::FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000622 << getLegalCName(Opcode) << "_";
623 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000624 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000625 if (!Operands.empty())
626 OS << ", ";
627 Operands.PrintParameters(OS);
628 OS << ") {\n";
629 OS << " switch (VT) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000630 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000631 TI != TE; ++TI) {
632 MVT::SimpleValueType VT = TI->first;
633 std::string TypeName = getName(VT);
634 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000635 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
636 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000637 OS << "(RetVT";
638 if (!Operands.empty())
639 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000640 Operands.PrintArguments(OS);
641 OS << ");\n";
642 }
643 OS << " default: return 0;\n";
644 OS << " }\n";
645 OS << "}\n";
646 OS << "\n";
647 }
648
Dan Gohman0bfb7522008-08-22 00:28:15 +0000649 OS << "// Top-level FastEmit function.\n";
650 OS << "\n";
651
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000652 // Emit one function for the operand signature that demultiplexes based
653 // on opcode and type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000654 OS << "unsigned FastISel::FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000655 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000656 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000657 if (!Operands.empty())
658 OS << ", ";
659 Operands.PrintParameters(OS);
660 OS << ") {\n";
661 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000662 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000663 I != E; ++I) {
664 const std::string &Opcode = I->first;
665
666 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000667 << getLegalCName(Opcode) << "_";
668 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000669 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000670 if (!Operands.empty())
671 OS << ", ";
672 Operands.PrintArguments(OS);
673 OS << ");\n";
674 }
675 OS << " default: return 0;\n";
676 OS << " }\n";
677 OS << "}\n";
678 OS << "\n";
679 }
Dan Gohman72d63af2008-08-26 21:21:20 +0000680}
681
682void FastISelEmitter::run(std::ostream &OS) {
683 const CodeGenTarget &Target = CGP.getTargetInfo();
684
685 // Determine the target's namespace name.
686 std::string InstNS = Target.getInstNamespace() + "::";
687 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
688
689 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
690 Target.getName() + " target", OS);
691
692 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
693 OS << "\n";
694 OS << "namespace llvm {\n";
695 OS << "\n";
696 OS << "namespace " << InstNS.substr(0, InstNS.size() - 2) << " {\n";
697 OS << "\n";
698
699 FastISelMap F(InstNS);
700 F.CollectPatterns(CGP);
701 F.PrintClass(OS);
702 F.PrintFunctionDefinitions(OS);
703
704 // Define the target FastISel creation function.
705 OS << "llvm::FastISel *createFastISel(MachineFunction &mf) {\n";
706 OS << " return new FastISel(mf);\n";
707 OS << "}\n";
708 OS << "\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000709
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000710 OS << "} // namespace X86\n";
711 OS << "\n";
712 OS << "} // namespace llvm\n";
713}
714
715FastISelEmitter::FastISelEmitter(RecordKeeper &R)
716 : Records(R),
Dan Gohman72d63af2008-08-26 21:21:20 +0000717 CGP(R) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000718}
Dan Gohman72d63af2008-08-26 21:21:20 +0000719