blob: 5868a24e193f8a280da558d422ded7397b6cd2a1 [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,
Dan Gohmancf711aa2008-08-19 20:58:14 +000065 MVT::SimpleValueType VT,
66 const CodeGenRegisterClass *DstRC) {
Dan Gohmand1d2ee82008-08-19 20:56:30 +000067 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
68 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000069 // For now, filter out any operand with a predicate.
70 if (!Op->getPredicateFn().empty())
71 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000072 // For now, filter out any operand with multiple values.
73 if (Op->getExtTypes().size() != 1)
74 return false;
75 // For now, all the operands must have the same type.
76 if (Op->getTypeNum(0) != VT)
77 return false;
78 if (!Op->isLeaf()) {
79 if (Op->getOperator()->getName() == "imm") {
80 Operands.push_back("i");
81 return true;
82 }
83 // For now, ignore fpimm and other non-leaf nodes.
84 return false;
85 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000086 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
87 if (!OpDI)
88 return false;
89 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +000090 // TODO: handle instructions which have physreg operands.
91 if (OpLeafRec->isSubClassOf("Register"))
92 return false;
93 // For now, the only other thing we accept is register operands.
Dan Gohmand1d2ee82008-08-19 20:56:30 +000094 if (!OpLeafRec->isSubClassOf("RegisterClass"))
95 return false;
96 // For now, require the register operands' register classes to all
97 // be the same.
98 const CodeGenRegisterClass *RC = &Target.getRegisterClass(OpLeafRec);
99 if (!RC)
100 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000101 // For now, all the operands must have the same register class.
102 if (DstRC != RC)
103 return false;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000104 Operands.push_back("r");
105 }
106 return true;
107 }
108
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000109 void PrintParameters(std::ostream &OS) const {
110 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
111 if (Operands[i] == "r") {
112 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000113 } else if (Operands[i] == "i") {
114 OS << "uint64_t imm" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000115 } else {
116 assert("Unknown operand kind!");
117 abort();
118 }
119 if (i + 1 != e)
120 OS << ", ";
121 }
122 }
123
124 void PrintArguments(std::ostream &OS) const {
125 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
126 if (Operands[i] == "r") {
127 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000128 } else if (Operands[i] == "i") {
129 OS << "imm" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000130 } else {
131 assert("Unknown operand kind!");
132 abort();
133 }
134 if (i + 1 != e)
135 OS << ", ";
136 }
137 }
138
139 void PrintManglingSuffix(std::ostream &OS) const {
140 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
141 OS << Operands[i];
142 }
143 }
144};
145
Dan Gohman04b7dfb2008-08-19 18:06:12 +0000146/// InstructionMemo - This class holds additional information about an
147/// instruction needed to emit code for it.
148///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000149struct InstructionMemo {
150 std::string Name;
151 const CodeGenRegisterClass *RC;
152};
153
154}
155
156static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
157 return CGP.getSDNodeInfo(Op).getEnumName();
158}
159
160static std::string getLegalCName(std::string OpName) {
161 std::string::size_type pos = OpName.find("::");
162 if (pos != std::string::npos)
163 OpName.replace(pos, 2, "_");
164 return OpName;
165}
166
167void FastISelEmitter::run(std::ostream &OS) {
168 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000169 Target.getName() + " target", OS);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000170
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000171 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
172 OS << "\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000173 OS << "namespace llvm {\n";
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000174 OS << "\n";
175 OS << "namespace " << InstNS.substr(0, InstNS.size() - 2) << " {\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000176 OS << "\n";
177
Dan Gohman22bb3112008-08-22 00:20:26 +0000178 typedef std::map<std::string, InstructionMemo> PredMap;
179 typedef std::map<MVT::SimpleValueType, PredMap> TypePredMap;
180 typedef std::map<std::string, TypePredMap> OpcodeTypePredMap;
181 typedef std::map<OperandsSignature, OpcodeTypePredMap> OperandsOpcodeTypePredMap;
182 OperandsOpcodeTypePredMap SimplePatterns;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000183
Dan Gohman0bfb7522008-08-22 00:28:15 +0000184 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000185 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
186 E = CGP.ptm_end(); I != E; ++I) {
187 const PatternToMatch &Pattern = *I;
188
189 // For now, just look at Instructions, so that we don't have to worry
190 // about emitting multiple instructions for a pattern.
191 TreePatternNode *Dst = Pattern.getDstPattern();
192 if (Dst->isLeaf()) continue;
193 Record *Op = Dst->getOperator();
194 if (!Op->isSubClassOf("Instruction"))
195 continue;
196 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
197 if (II.OperandList.empty())
198 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000199
200 // For now, ignore instructions where the first operand is not an
201 // output register.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000202 Record *Op0Rec = II.OperandList[0].Rec;
203 if (!Op0Rec->isSubClassOf("RegisterClass"))
204 continue;
205 const CodeGenRegisterClass *DstRC = &Target.getRegisterClass(Op0Rec);
206 if (!DstRC)
207 continue;
208
209 // Inspect the pattern.
210 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
211 if (!InstPatNode) continue;
212 if (InstPatNode->isLeaf()) continue;
213
214 Record *InstPatOp = InstPatNode->getOperator();
215 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
216 MVT::SimpleValueType VT = InstPatNode->getTypeNum(0);
217
218 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000219 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000220 if (InstPatOp->isSubClassOf("Operand"))
221 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000222 if (InstPatOp->getName() == "imm" ||
223 InstPatOp->getName() == "fpimm")
224 continue;
225
226 // For now, filter out any instructions with predicates.
227 if (!InstPatNode->getPredicateFn().empty())
228 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000229
Dan Gohman379cad42008-08-19 20:36:33 +0000230 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000231 OperandsSignature Operands;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000232 if (!Operands.initialize(InstPatNode, Target, VT, DstRC))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000233 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000234
Dan Gohman22bb3112008-08-22 00:20:26 +0000235 // Get the predicate that guards this pattern.
236 std::string PredicateCheck = Pattern.getPredicateCheck();
237
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000238 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000239 InstructionMemo Memo = {
240 Pattern.getDstPattern()->getOperator()->getName(),
241 DstRC
242 };
Dan Gohman22bb3112008-08-22 00:20:26 +0000243 assert(!SimplePatterns[Operands][OpcodeName][VT].count(PredicateCheck) &&
244 "Duplicate pattern!");
245 SimplePatterns[Operands][OpcodeName][VT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000246 }
247
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000248 // Declare the target FastISel class.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000249 OS << "class FastISel : public llvm::FastISel {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000250 for (OperandsOpcodeTypePredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000251 OE = SimplePatterns.end(); OI != OE; ++OI) {
252 const OperandsSignature &Operands = OI->first;
Dan Gohman22bb3112008-08-22 00:20:26 +0000253 const OpcodeTypePredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000254
Dan Gohman22bb3112008-08-22 00:20:26 +0000255 for (OpcodeTypePredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000256 I != E; ++I) {
257 const std::string &Opcode = I->first;
Dan Gohman22bb3112008-08-22 00:20:26 +0000258 const TypePredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000259
Dan Gohman22bb3112008-08-22 00:20:26 +0000260 for (TypePredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000261 TI != TE; ++TI) {
262 MVT::SimpleValueType VT = TI->first;
263
264 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000265 << "_" << getLegalCName(getName(VT)) << "_";
266 Operands.PrintManglingSuffix(OS);
267 OS << "(";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000268 Operands.PrintParameters(OS);
269 OS << ");\n";
270 }
271
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000272 OS << " unsigned FastEmit_" << getLegalCName(Opcode) << "_";
273 Operands.PrintManglingSuffix(OS);
274 OS << "(MVT::SimpleValueType VT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000275 if (!Operands.empty())
276 OS << ", ";
277 Operands.PrintParameters(OS);
278 OS << ");\n";
279 }
280
Dan Gohman56e0f872008-08-19 20:31:38 +0000281 OS << " unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000282 Operands.PrintManglingSuffix(OS);
283 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
284 if (!Operands.empty())
285 OS << ", ";
286 Operands.PrintParameters(OS);
287 OS << ");\n";
288 }
Dan Gohman22bb3112008-08-22 00:20:26 +0000289 OS << "\n";
290
291 // Declare the Subtarget member, which is used for predicate checks.
292 OS << " const " << InstNS.substr(0, InstNS.size() - 2)
293 << "Subtarget *Subtarget;\n";
294 OS << "\n";
295
296 // Declare the constructor.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000297 OS << "public:\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000298 OS << " explicit FastISel(MachineFunction &mf)\n";
299 OS << " : llvm::FastISel(mf),\n";
300 OS << " Subtarget(&TM.getSubtarget<" << InstNS.substr(0, InstNS.size() - 2)
301 << "Subtarget>()) {}\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000302 OS << "};\n";
303 OS << "\n";
304
305 // Define the target FastISel creation function.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000306 OS << "llvm::FastISel *createFastISel(MachineFunction &mf) {\n";
307 OS << " return new FastISel(mf);\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000308 OS << "}\n";
309 OS << "\n";
310
311 // Now emit code for all the patterns that we collected.
Dan Gohman22bb3112008-08-22 00:20:26 +0000312 for (OperandsOpcodeTypePredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000313 OE = SimplePatterns.end(); OI != OE; ++OI) {
314 const OperandsSignature &Operands = OI->first;
Dan Gohman22bb3112008-08-22 00:20:26 +0000315 const OpcodeTypePredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000316
Dan Gohman22bb3112008-08-22 00:20:26 +0000317 for (OpcodeTypePredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000318 I != E; ++I) {
319 const std::string &Opcode = I->first;
Dan Gohman22bb3112008-08-22 00:20:26 +0000320 const TypePredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000321
322 OS << "// FastEmit functions for " << Opcode << ".\n";
323 OS << "\n";
324
325 // Emit one function for each opcode,type pair.
Dan Gohman22bb3112008-08-22 00:20:26 +0000326 for (TypePredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000327 TI != TE; ++TI) {
328 MVT::SimpleValueType VT = TI->first;
Dan Gohman22bb3112008-08-22 00:20:26 +0000329 const PredMap &PM = TI->second;
330 bool HasPred = false;
331
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000332 OS << "unsigned FastISel::FastEmit_"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000333 << getLegalCName(Opcode)
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000334 << "_" << getLegalCName(getName(VT)) << "_";
335 Operands.PrintManglingSuffix(OS);
336 OS << "(";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000337 Operands.PrintParameters(OS);
338 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000339
340 // Emit code for each possible instruction. There may be
341 // multiple if there are subtarget concerns.
342 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
343 PI != PE; ++PI) {
344 std::string PredicateCheck = PI->first;
345 const InstructionMemo &Memo = PI->second;
346
347 if (PredicateCheck.empty()) {
348 assert(!HasPred && "Multiple instructions match, at least one has "
349 "a predicate and at least one doesn't!");
350 } else {
351 OS << " if (" + PredicateCheck + ")\n";
352 OS << " ";
353 HasPred = true;
354 }
355 OS << " return FastEmitInst_";
356 Operands.PrintManglingSuffix(OS);
357 OS << "(" << InstNS << Memo.Name << ", ";
358 OS << InstNS << Memo.RC->getName() << "RegisterClass";
359 if (!Operands.empty())
360 OS << ", ";
361 Operands.PrintArguments(OS);
362 OS << ");\n";
363 }
364 // Return 0 if none of the predicates were satisfied.
365 if (HasPred)
366 OS << " return 0;\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000367 OS << "}\n";
368 OS << "\n";
369 }
370
371 // Emit one function for the opcode that demultiplexes based on the type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000372 OS << "unsigned FastISel::FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000373 << getLegalCName(Opcode) << "_";
374 Operands.PrintManglingSuffix(OS);
375 OS << "(MVT::SimpleValueType VT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000376 if (!Operands.empty())
377 OS << ", ";
378 Operands.PrintParameters(OS);
379 OS << ") {\n";
380 OS << " switch (VT) {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000381 for (TypePredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000382 TI != TE; ++TI) {
383 MVT::SimpleValueType VT = TI->first;
384 std::string TypeName = getName(VT);
385 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000386 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
387 Operands.PrintManglingSuffix(OS);
388 OS << "(";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000389 Operands.PrintArguments(OS);
390 OS << ");\n";
391 }
392 OS << " default: return 0;\n";
393 OS << " }\n";
394 OS << "}\n";
395 OS << "\n";
396 }
397
Dan Gohman0bfb7522008-08-22 00:28:15 +0000398 OS << "// Top-level FastEmit function.\n";
399 OS << "\n";
400
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000401 // Emit one function for the operand signature that demultiplexes based
402 // on opcode and type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000403 OS << "unsigned FastISel::FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000404 Operands.PrintManglingSuffix(OS);
405 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
406 if (!Operands.empty())
407 OS << ", ";
408 Operands.PrintParameters(OS);
409 OS << ") {\n";
410 OS << " switch (Opcode) {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000411 for (OpcodeTypePredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000412 I != E; ++I) {
413 const std::string &Opcode = I->first;
414
415 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000416 << getLegalCName(Opcode) << "_";
417 Operands.PrintManglingSuffix(OS);
418 OS << "(VT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000419 if (!Operands.empty())
420 OS << ", ";
421 Operands.PrintArguments(OS);
422 OS << ");\n";
423 }
424 OS << " default: return 0;\n";
425 OS << " }\n";
426 OS << "}\n";
427 OS << "\n";
428 }
429
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000430 OS << "} // namespace X86\n";
431 OS << "\n";
432 OS << "} // namespace llvm\n";
433}
434
435FastISelEmitter::FastISelEmitter(RecordKeeper &R)
436 : Records(R),
437 CGP(R),
438 Target(CGP.getTargetInfo()),
439 InstNS(Target.getInstNamespace() + "::") {
440
441 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000442}