blob: b434c15d8c2de39f2fe356b6b8fc6cb57520cb92 [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);
69 if (!Op->isLeaf())
70 return false;
71 // For now, filter out any operand with a predicate.
72 if (!Op->getPredicateFn().empty())
73 return false;
74 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
75 if (!OpDI)
76 return false;
77 Record *OpLeafRec = OpDI->getDef();
78 // For now, only accept register operands.
79 if (!OpLeafRec->isSubClassOf("RegisterClass"))
80 return false;
81 // For now, require the register operands' register classes to all
82 // be the same.
83 const CodeGenRegisterClass *RC = &Target.getRegisterClass(OpLeafRec);
84 if (!RC)
85 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +000086 // For now, all the operands must have the same register class.
87 if (DstRC != RC)
88 return false;
Dan Gohmand1d2ee82008-08-19 20:56:30 +000089 // For now, all the operands must have the same type.
90 if (Op->getTypeNum(0) != VT)
91 return false;
92 Operands.push_back("r");
93 }
94 return true;
95 }
96
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000097 void PrintParameters(std::ostream &OS) const {
98 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
99 if (Operands[i] == "r") {
100 OS << "unsigned Op" << i;
101 } else {
102 assert("Unknown operand kind!");
103 abort();
104 }
105 if (i + 1 != e)
106 OS << ", ";
107 }
108 }
109
110 void PrintArguments(std::ostream &OS) const {
111 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
112 if (Operands[i] == "r") {
113 OS << "Op" << i;
114 } else {
115 assert("Unknown operand kind!");
116 abort();
117 }
118 if (i + 1 != e)
119 OS << ", ";
120 }
121 }
122
123 void PrintManglingSuffix(std::ostream &OS) const {
124 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
125 OS << Operands[i];
126 }
127 }
128};
129
Dan Gohman04b7dfb2008-08-19 18:06:12 +0000130/// InstructionMemo - This class holds additional information about an
131/// instruction needed to emit code for it.
132///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000133struct InstructionMemo {
134 std::string Name;
135 const CodeGenRegisterClass *RC;
136};
137
138}
139
140static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
141 return CGP.getSDNodeInfo(Op).getEnumName();
142}
143
144static std::string getLegalCName(std::string OpName) {
145 std::string::size_type pos = OpName.find("::");
146 if (pos != std::string::npos)
147 OpName.replace(pos, 2, "_");
148 return OpName;
149}
150
151void FastISelEmitter::run(std::ostream &OS) {
152 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
153 CGP.getTargetInfo().getName() + " target", OS);
154
155 const CodeGenTarget &Target = CGP.getTargetInfo();
156
157 // Get the namespace to insert instructions into. Make sure not to pick up
158 // "TargetInstrInfo" by accidentally getting the namespace off the PHI
159 // instruction or something.
160 std::string InstNS;
161 for (CodeGenTarget::inst_iterator i = Target.inst_begin(),
162 e = Target.inst_end(); i != e; ++i) {
163 InstNS = i->second.Namespace;
164 if (InstNS != "TargetInstrInfo")
165 break;
166 }
167
168 OS << "namespace llvm {\n";
169 OS << "namespace " << InstNS << " {\n";
170 OS << "class FastISel;\n";
171 OS << "}\n";
172 OS << "}\n";
173 OS << "\n";
174
175 if (!InstNS.empty()) InstNS += "::";
176
177 typedef std::map<MVT::SimpleValueType, InstructionMemo> TypeMap;
178 typedef std::map<std::string, TypeMap> OpcodeTypeMap;
179 typedef std::map<OperandsSignature, OpcodeTypeMap> OperandsOpcodeTypeMap;
180 OperandsOpcodeTypeMap SimplePatterns;
181
182 // Create the supported type signatures.
183 OperandsSignature KnownOperands;
184 SimplePatterns[KnownOperands] = OpcodeTypeMap();
185 KnownOperands.Operands.push_back("r");
186 SimplePatterns[KnownOperands] = OpcodeTypeMap();
187 KnownOperands.Operands.push_back("r");
188 SimplePatterns[KnownOperands] = OpcodeTypeMap();
189
190 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
191 E = CGP.ptm_end(); I != E; ++I) {
192 const PatternToMatch &Pattern = *I;
193
194 // For now, just look at Instructions, so that we don't have to worry
195 // about emitting multiple instructions for a pattern.
196 TreePatternNode *Dst = Pattern.getDstPattern();
197 if (Dst->isLeaf()) continue;
198 Record *Op = Dst->getOperator();
199 if (!Op->isSubClassOf("Instruction"))
200 continue;
201 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
202 if (II.OperandList.empty())
203 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000204
205 // For now, ignore instructions where the first operand is not an
206 // output register.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000207 Record *Op0Rec = II.OperandList[0].Rec;
208 if (!Op0Rec->isSubClassOf("RegisterClass"))
209 continue;
210 const CodeGenRegisterClass *DstRC = &Target.getRegisterClass(Op0Rec);
211 if (!DstRC)
212 continue;
213
214 // Inspect the pattern.
215 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
216 if (!InstPatNode) continue;
217 if (InstPatNode->isLeaf()) continue;
218
219 Record *InstPatOp = InstPatNode->getOperator();
220 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
221 MVT::SimpleValueType VT = InstPatNode->getTypeNum(0);
222
223 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000224 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000225 if (InstPatOp->isSubClassOf("Operand"))
226 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000227 if (InstPatOp->getName() == "imm" ||
228 InstPatOp->getName() == "fpimm")
229 continue;
230
231 // For now, filter out any instructions with predicates.
232 if (!InstPatNode->getPredicateFn().empty())
233 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000234
Dan Gohman379cad42008-08-19 20:36:33 +0000235 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000236 OperandsSignature Operands;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000237 if (!Operands.initialize(InstPatNode, Target, VT, DstRC))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000238 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000239
240 // If it's not a known signature, ignore it.
241 if (!SimplePatterns.count(Operands))
242 continue;
243
244 // Ok, we found a pattern that we can handle. Remember it.
245 {
Dan Gohman56726342008-08-19 18:07:49 +0000246 InstructionMemo Memo = {
247 Pattern.getDstPattern()->getOperator()->getName(),
248 DstRC
249 };
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000250 SimplePatterns[Operands][OpcodeName][VT] = Memo;
251 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000252 }
253
254 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
255 OS << "\n";
256 OS << "namespace llvm {\n";
257 OS << "\n";
258
259 // Declare the target FastISel class.
260 OS << "class " << InstNS << "FastISel : public llvm::FastISel {\n";
261 for (OperandsOpcodeTypeMap::const_iterator OI = SimplePatterns.begin(),
262 OE = SimplePatterns.end(); OI != OE; ++OI) {
263 const OperandsSignature &Operands = OI->first;
264 const OpcodeTypeMap &OTM = OI->second;
265
266 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
267 I != E; ++I) {
268 const std::string &Opcode = I->first;
269 const TypeMap &TM = I->second;
270
271 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
272 TI != TE; ++TI) {
273 MVT::SimpleValueType VT = TI->first;
274
275 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
276 << "_" << getLegalCName(getName(VT)) << "(";
277 Operands.PrintParameters(OS);
278 OS << ");\n";
279 }
280
281 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
282 << "(MVT::SimpleValueType VT";
283 if (!Operands.empty())
284 OS << ", ";
285 Operands.PrintParameters(OS);
286 OS << ");\n";
287 }
288
Dan Gohman56e0f872008-08-19 20:31:38 +0000289 OS << " unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000290 Operands.PrintManglingSuffix(OS);
291 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
292 if (!Operands.empty())
293 OS << ", ";
294 Operands.PrintParameters(OS);
295 OS << ");\n";
296 }
297 OS << "public:\n";
298 OS << " FastISel(MachineBasicBlock *mbb, MachineFunction *mf, ";
299 OS << "const TargetInstrInfo *tii) : llvm::FastISel(mbb, mf, tii) {}\n";
300 OS << "};\n";
301 OS << "\n";
302
303 // Define the target FastISel creation function.
304 OS << "llvm::FastISel *" << InstNS
305 << "createFastISel(MachineBasicBlock *mbb, MachineFunction *mf, ";
306 OS << "const TargetInstrInfo *tii) {\n";
307 OS << " return new " << InstNS << "FastISel(mbb, mf, tii);\n";
308 OS << "}\n";
309 OS << "\n";
310
311 // Now emit code for all the patterns that we collected.
312 for (OperandsOpcodeTypeMap::const_iterator OI = SimplePatterns.begin(),
313 OE = SimplePatterns.end(); OI != OE; ++OI) {
314 const OperandsSignature &Operands = OI->first;
315 const OpcodeTypeMap &OTM = OI->second;
316
317 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
318 I != E; ++I) {
319 const std::string &Opcode = I->first;
320 const TypeMap &TM = I->second;
321
322 OS << "// FastEmit functions for " << Opcode << ".\n";
323 OS << "\n";
324
325 // Emit one function for each opcode,type pair.
326 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
327 TI != TE; ++TI) {
328 MVT::SimpleValueType VT = TI->first;
329 const InstructionMemo &Memo = TI->second;
330
331 OS << "unsigned " << InstNS << "FastISel::FastEmit_"
332 << getLegalCName(Opcode)
333 << "_" << getLegalCName(getName(VT)) << "(";
334 Operands.PrintParameters(OS);
335 OS << ") {\n";
336 OS << " return FastEmitInst_";
337 Operands.PrintManglingSuffix(OS);
338 OS << "(" << InstNS << Memo.Name << ", ";
339 OS << InstNS << Memo.RC->getName() << "RegisterClass";
340 if (!Operands.empty())
341 OS << ", ";
342 Operands.PrintArguments(OS);
343 OS << ");\n";
344 OS << "}\n";
345 OS << "\n";
346 }
347
348 // Emit one function for the opcode that demultiplexes based on the type.
349 OS << "unsigned " << InstNS << "FastISel::FastEmit_"
350 << getLegalCName(Opcode) << "(MVT::SimpleValueType VT";
351 if (!Operands.empty())
352 OS << ", ";
353 Operands.PrintParameters(OS);
354 OS << ") {\n";
355 OS << " switch (VT) {\n";
356 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
357 TI != TE; ++TI) {
358 MVT::SimpleValueType VT = TI->first;
359 std::string TypeName = getName(VT);
360 OS << " case " << TypeName << ": return FastEmit_"
361 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "(";
362 Operands.PrintArguments(OS);
363 OS << ");\n";
364 }
365 OS << " default: return 0;\n";
366 OS << " }\n";
367 OS << "}\n";
368 OS << "\n";
369 }
370
371 // Emit one function for the operand signature that demultiplexes based
372 // on opcode and type.
373 OS << "unsigned " << InstNS << "FastISel::FastEmit_";
374 Operands.PrintManglingSuffix(OS);
375 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
376 if (!Operands.empty())
377 OS << ", ";
378 Operands.PrintParameters(OS);
379 OS << ") {\n";
380 OS << " switch (Opcode) {\n";
381 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
382 I != E; ++I) {
383 const std::string &Opcode = I->first;
384
385 OS << " case " << Opcode << ": return FastEmit_"
386 << getLegalCName(Opcode) << "(VT";
387 if (!Operands.empty())
388 OS << ", ";
389 Operands.PrintArguments(OS);
390 OS << ");\n";
391 }
392 OS << " default: return 0;\n";
393 OS << " }\n";
394 OS << "}\n";
395 OS << "\n";
396 }
397
398 OS << "}\n";
399}
400
401// todo: really filter out Constants