1 //===- FastISelEmitter.cpp - Generate an instruction selector -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits a "fast" instruction selector.
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.
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.
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.
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.
36 //===----------------------------------------------------------------------===//
38 #include "FastISelEmitter.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Streams.h"
42 #include "llvm/ADT/VectorExtras.h"
47 /// OperandsSignature - This class holds a description of a list of operand
48 /// types. It has utility methods for emitting text based on the operands.
50 struct OperandsSignature {
51 std::vector<std::string> Operands;
53 bool operator<(const OperandsSignature &O) const {
54 return Operands < O.Operands;
57 bool empty() const { return Operands.empty(); }
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;
64 assert("Unknown operand kind!");
72 void PrintArguments(std::ostream &OS) const {
73 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
74 if (Operands[i] == "r") {
77 assert("Unknown operand kind!");
85 void PrintManglingSuffix(std::ostream &OS) const {
86 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
92 /// InstructionMemo - This class holds additional information about an
93 /// instruction needed to emit code for it.
95 struct InstructionMemo {
97 const CodeGenRegisterClass *RC;
102 static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
103 return CGP.getSDNodeInfo(Op).getEnumName();
106 static 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, "_");
113 void FastISelEmitter::run(std::ostream &OS) {
114 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
115 CGP.getTargetInfo().getName() + " target", OS);
117 const CodeGenTarget &Target = CGP.getTargetInfo();
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.
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")
130 OS << "namespace llvm {\n";
131 OS << "namespace " << InstNS << " {\n";
132 OS << "class FastISel;\n";
137 if (!InstNS.empty()) InstNS += "::";
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;
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();
152 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
153 E = CGP.ptm_end(); I != E; ++I) {
154 const PatternToMatch &Pattern = *I;
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"))
163 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
164 if (II.OperandList.empty())
166 Record *Op0Rec = II.OperandList[0].Rec;
167 if (!Op0Rec->isSubClassOf("RegisterClass"))
169 const CodeGenRegisterClass *DstRC = &Target.getRegisterClass(Op0Rec);
173 // Inspect the pattern.
174 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
175 if (!InstPatNode) continue;
176 if (InstPatNode->isLeaf()) continue;
178 Record *InstPatOp = InstPatNode->getOperator();
179 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
180 MVT::SimpleValueType VT = InstPatNode->getTypeNum(0);
182 // For now, filter out instructions which just set a register to
183 // an Operand, like MOV32ri.
184 if (InstPatOp->isSubClassOf("Operand"))
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);
193 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
196 Record *OpLeafRec = OpDI->getDef();
197 if (!OpLeafRec->isSubClassOf("RegisterClass"))
199 const CodeGenRegisterClass *RC = &Target.getRegisterClass(OpLeafRec);
202 if (Op->getTypeNum(0) != VT)
204 Operands.Operands.push_back("r");
207 // If it's not a known signature, ignore it.
208 if (!SimplePatterns.count(Operands))
211 // Ok, we found a pattern that we can handle. Remember it.
213 InstructionMemo Memo = {
214 Pattern.getDstPattern()->getOperator()->getName(),
217 SimplePatterns[Operands][OpcodeName][VT] = Memo;
223 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
225 OS << "namespace llvm {\n";
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;
235 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
237 const std::string &Opcode = I->first;
238 const TypeMap &TM = I->second;
240 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
242 MVT::SimpleValueType VT = TI->first;
244 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
245 << "_" << getLegalCName(getName(VT)) << "(";
246 Operands.PrintParameters(OS);
250 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
251 << "(MVT::SimpleValueType VT";
252 if (!Operands.empty())
254 Operands.PrintParameters(OS);
258 OS << "unsigned FastEmit_";
259 Operands.PrintManglingSuffix(OS);
260 OS << "(MVT::SimpleValueType VT, ISD::NodeType Opcode";
261 if (!Operands.empty())
263 Operands.PrintParameters(OS);
267 OS << " FastISel(MachineBasicBlock *mbb, MachineFunction *mf, ";
268 OS << "const TargetInstrInfo *tii) : llvm::FastISel(mbb, mf, tii) {}\n";
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";
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;
286 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
288 const std::string &Opcode = I->first;
289 const TypeMap &TM = I->second;
291 OS << "// FastEmit functions for " << Opcode << ".\n";
294 // Emit one function for each opcode,type pair.
295 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
297 MVT::SimpleValueType VT = TI->first;
298 const InstructionMemo &Memo = TI->second;
300 OS << "unsigned " << InstNS << "FastISel::FastEmit_"
301 << getLegalCName(Opcode)
302 << "_" << getLegalCName(getName(VT)) << "(";
303 Operands.PrintParameters(OS);
305 OS << " return FastEmitInst_";
306 Operands.PrintManglingSuffix(OS);
307 OS << "(" << InstNS << Memo.Name << ", ";
308 OS << InstNS << Memo.RC->getName() << "RegisterClass";
309 if (!Operands.empty())
311 Operands.PrintArguments(OS);
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())
322 Operands.PrintParameters(OS);
324 OS << " switch (VT) {\n";
325 for (TypeMap::const_iterator TI = TM.begin(), TE = TM.end();
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);
334 OS << " default: return 0;\n";
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())
347 Operands.PrintParameters(OS);
349 OS << " switch (Opcode) {\n";
350 for (OpcodeTypeMap::const_iterator I = OTM.begin(), E = OTM.end();
352 const std::string &Opcode = I->first;
354 OS << " case " << Opcode << ": return FastEmit_"
355 << getLegalCName(Opcode) << "(VT";
356 if (!Operands.empty())
358 Operands.PrintArguments(OS);
361 OS << " default: return 0;\n";
370 // todo: really filter out Constants