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