blob: 77a39ea44e9901b5108140b40e2dfbee1bc77c86 [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 " +
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000153 Target.getName() + " target", OS);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000154
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000155 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
156 OS << "\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000157 OS << "namespace llvm {\n";
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000158 OS << "\n";
159 OS << "namespace " << InstNS.substr(0, InstNS.size() - 2) << " {\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000160 OS << "\n";
161
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000162 typedef std::map<MVT::SimpleValueType, InstructionMemo> TypeMap;
163 typedef std::map<std::string, TypeMap> OpcodeTypeMap;
164 typedef std::map<OperandsSignature, OpcodeTypeMap> OperandsOpcodeTypeMap;
165 OperandsOpcodeTypeMap SimplePatterns;
166
167 // Create the supported type signatures.
168 OperandsSignature KnownOperands;
169 SimplePatterns[KnownOperands] = OpcodeTypeMap();
170 KnownOperands.Operands.push_back("r");
171 SimplePatterns[KnownOperands] = OpcodeTypeMap();
172 KnownOperands.Operands.push_back("r");
173 SimplePatterns[KnownOperands] = OpcodeTypeMap();
174
175 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
176 E = CGP.ptm_end(); I != E; ++I) {
177 const PatternToMatch &Pattern = *I;
178
179 // For now, just look at Instructions, so that we don't have to worry
180 // about emitting multiple instructions for a pattern.
181 TreePatternNode *Dst = Pattern.getDstPattern();
182 if (Dst->isLeaf()) continue;
183 Record *Op = Dst->getOperator();
184 if (!Op->isSubClassOf("Instruction"))
185 continue;
186 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
187 if (II.OperandList.empty())
188 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000189
190 // For now, ignore instructions where the first operand is not an
191 // output register.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000192 Record *Op0Rec = II.OperandList[0].Rec;
193 if (!Op0Rec->isSubClassOf("RegisterClass"))
194 continue;
195 const CodeGenRegisterClass *DstRC = &Target.getRegisterClass(Op0Rec);
196 if (!DstRC)
197 continue;
198
199 // Inspect the pattern.
200 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
201 if (!InstPatNode) continue;
202 if (InstPatNode->isLeaf()) continue;
203
204 Record *InstPatOp = InstPatNode->getOperator();
205 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
206 MVT::SimpleValueType VT = InstPatNode->getTypeNum(0);
207
208 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000209 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000210 if (InstPatOp->isSubClassOf("Operand"))
211 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000212 if (InstPatOp->getName() == "imm" ||
213 InstPatOp->getName() == "fpimm")
214 continue;
215
216 // For now, filter out any instructions with predicates.
217 if (!InstPatNode->getPredicateFn().empty())
218 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000219
Dan Gohman379cad42008-08-19 20:36:33 +0000220 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000221 OperandsSignature Operands;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000222 if (!Operands.initialize(InstPatNode, Target, VT, DstRC))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000223 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000224
225 // If it's not a known signature, ignore it.
226 if (!SimplePatterns.count(Operands))
227 continue;
228
229 // Ok, we found a pattern that we can handle. Remember it.
230 {
Dan Gohman56726342008-08-19 18:07:49 +0000231 InstructionMemo Memo = {
232 Pattern.getDstPattern()->getOperator()->getName(),
233 DstRC
234 };
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000235 SimplePatterns[Operands][OpcodeName][VT] = Memo;
236 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000237 }
238
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000239 // Declare the target FastISel class.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000240 OS << "class FastISel : public llvm::FastISel {\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000241 for (OperandsOpcodeTypeMap::const_iterator OI = SimplePatterns.begin(),
242 OE = SimplePatterns.end(); OI != OE; ++OI) {
243 const OperandsSignature &Operands = OI->first;
244 const OpcodeTypeMap &OTM = OI->second;
245
246 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
247 I != E; ++I) {
248 const std::string &Opcode = I->first;
249 const TypeMap &TM = I->second;
250
251 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
252 TI != TE; ++TI) {
253 MVT::SimpleValueType VT = TI->first;
254
255 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
256 << "_" << getLegalCName(getName(VT)) << "(";
257 Operands.PrintParameters(OS);
258 OS << ");\n";
259 }
260
261 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
262 << "(MVT::SimpleValueType VT";
263 if (!Operands.empty())
264 OS << ", ";
265 Operands.PrintParameters(OS);
266 OS << ");\n";
267 }
268
Dan Gohman56e0f872008-08-19 20:31:38 +0000269 OS << " unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000270 Operands.PrintManglingSuffix(OS);
271 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
272 if (!Operands.empty())
273 OS << ", ";
274 Operands.PrintParameters(OS);
275 OS << ");\n";
276 }
277 OS << "public:\n";
Dan Gohmanbb466332008-08-20 21:05:57 +0000278 OS << " explicit FastISel(MachineFunction &mf) : llvm::FastISel(mf) {}\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000279 OS << "};\n";
280 OS << "\n";
281
282 // Define the target FastISel creation function.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000283 OS << "llvm::FastISel *createFastISel(MachineFunction &mf) {\n";
284 OS << " return new FastISel(mf);\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000285 OS << "}\n";
286 OS << "\n";
287
288 // Now emit code for all the patterns that we collected.
289 for (OperandsOpcodeTypeMap::const_iterator OI = SimplePatterns.begin(),
290 OE = SimplePatterns.end(); OI != OE; ++OI) {
291 const OperandsSignature &Operands = OI->first;
292 const OpcodeTypeMap &OTM = OI->second;
293
294 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
295 I != E; ++I) {
296 const std::string &Opcode = I->first;
297 const TypeMap &TM = I->second;
298
299 OS << "// FastEmit functions for " << Opcode << ".\n";
300 OS << "\n";
301
302 // Emit one function for each opcode,type pair.
303 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
304 TI != TE; ++TI) {
305 MVT::SimpleValueType VT = TI->first;
306 const InstructionMemo &Memo = TI->second;
307
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000308 OS << "unsigned FastISel::FastEmit_"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000309 << getLegalCName(Opcode)
310 << "_" << getLegalCName(getName(VT)) << "(";
311 Operands.PrintParameters(OS);
312 OS << ") {\n";
313 OS << " return FastEmitInst_";
314 Operands.PrintManglingSuffix(OS);
315 OS << "(" << InstNS << Memo.Name << ", ";
316 OS << InstNS << Memo.RC->getName() << "RegisterClass";
317 if (!Operands.empty())
318 OS << ", ";
319 Operands.PrintArguments(OS);
320 OS << ");\n";
321 OS << "}\n";
322 OS << "\n";
323 }
324
325 // Emit one function for the opcode that demultiplexes based on the type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000326 OS << "unsigned FastISel::FastEmit_"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000327 << getLegalCName(Opcode) << "(MVT::SimpleValueType VT";
328 if (!Operands.empty())
329 OS << ", ";
330 Operands.PrintParameters(OS);
331 OS << ") {\n";
332 OS << " switch (VT) {\n";
333 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
334 TI != TE; ++TI) {
335 MVT::SimpleValueType VT = TI->first;
336 std::string TypeName = getName(VT);
337 OS << " case " << TypeName << ": return FastEmit_"
338 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "(";
339 Operands.PrintArguments(OS);
340 OS << ");\n";
341 }
342 OS << " default: return 0;\n";
343 OS << " }\n";
344 OS << "}\n";
345 OS << "\n";
346 }
347
348 // Emit one function for the operand signature that demultiplexes based
349 // on opcode and type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000350 OS << "unsigned FastISel::FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000351 Operands.PrintManglingSuffix(OS);
352 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
353 if (!Operands.empty())
354 OS << ", ";
355 Operands.PrintParameters(OS);
356 OS << ") {\n";
357 OS << " switch (Opcode) {\n";
358 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
359 I != E; ++I) {
360 const std::string &Opcode = I->first;
361
362 OS << " case " << Opcode << ": return FastEmit_"
363 << getLegalCName(Opcode) << "(VT";
364 if (!Operands.empty())
365 OS << ", ";
366 Operands.PrintArguments(OS);
367 OS << ");\n";
368 }
369 OS << " default: return 0;\n";
370 OS << " }\n";
371 OS << "}\n";
372 OS << "\n";
373 }
374
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000375 OS << "} // namespace X86\n";
376 OS << "\n";
377 OS << "} // namespace llvm\n";
378}
379
380FastISelEmitter::FastISelEmitter(RecordKeeper &R)
381 : Records(R),
382 CGP(R),
383 Target(CGP.getTargetInfo()),
384 InstNS(Target.getInstNamespace() + "::") {
385
386 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000387}