blob: 78644315e9c79933ed260cc18a3bbafb929154a4 [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) {
Owen Anderson6d0c25e2008-08-25 20:20:32 +000067 if (!InstPatNode->isLeaf() &&
68 InstPatNode->getOperator()->getName() == "imm") {
69 Operands.push_back("i");
70 return true;
71 }
72
Dan Gohmand1d2ee82008-08-19 20:56:30 +000073 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
74 TreePatternNode *Op = InstPatNode->getChild(i);
Dan Gohmand1d2ee82008-08-19 20:56:30 +000075 // For now, filter out any operand with a predicate.
76 if (!Op->getPredicateFn().empty())
77 return false;
Dan Gohmand5fe57d2008-08-21 01:41:07 +000078 // For now, filter out any operand with multiple values.
79 if (Op->getExtTypes().size() != 1)
80 return false;
81 // For now, all the operands must have the same type.
82 if (Op->getTypeNum(0) != VT)
83 return false;
84 if (!Op->isLeaf()) {
85 if (Op->getOperator()->getName() == "imm") {
86 Operands.push_back("i");
87 return true;
88 }
89 // For now, ignore fpimm and other non-leaf nodes.
90 return false;
91 }
Dan Gohmand1d2ee82008-08-19 20:56:30 +000092 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
93 if (!OpDI)
94 return false;
95 Record *OpLeafRec = OpDI->getDef();
Dan Gohmand5fe57d2008-08-21 01:41:07 +000096 // TODO: handle instructions which have physreg operands.
97 if (OpLeafRec->isSubClassOf("Register"))
98 return false;
99 // For now, the only other thing we accept is register operands.
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000100 if (!OpLeafRec->isSubClassOf("RegisterClass"))
101 return false;
102 // For now, require the register operands' register classes to all
103 // be the same.
104 const CodeGenRegisterClass *RC = &Target.getRegisterClass(OpLeafRec);
105 if (!RC)
106 return false;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000107 // For now, all the operands must have the same register class.
108 if (DstRC != RC)
109 return false;
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000110 Operands.push_back("r");
111 }
112 return true;
113 }
114
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000115 void PrintParameters(std::ostream &OS) const {
116 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
117 if (Operands[i] == "r") {
118 OS << "unsigned Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000119 } else if (Operands[i] == "i") {
120 OS << "uint64_t imm" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000121 } else {
122 assert("Unknown operand kind!");
123 abort();
124 }
125 if (i + 1 != e)
126 OS << ", ";
127 }
128 }
129
130 void PrintArguments(std::ostream &OS) const {
131 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
132 if (Operands[i] == "r") {
133 OS << "Op" << i;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000134 } else if (Operands[i] == "i") {
135 OS << "imm" << i;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000136 } else {
137 assert("Unknown operand kind!");
138 abort();
139 }
140 if (i + 1 != e)
141 OS << ", ";
142 }
143 }
144
145 void PrintManglingSuffix(std::ostream &OS) const {
146 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
147 OS << Operands[i];
148 }
149 }
150};
151
Dan Gohman04b7dfb2008-08-19 18:06:12 +0000152/// InstructionMemo - This class holds additional information about an
153/// instruction needed to emit code for it.
154///
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000155struct InstructionMemo {
156 std::string Name;
157 const CodeGenRegisterClass *RC;
158};
159
160}
161
162static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
163 return CGP.getSDNodeInfo(Op).getEnumName();
164}
165
166static std::string getLegalCName(std::string OpName) {
167 std::string::size_type pos = OpName.find("::");
168 if (pos != std::string::npos)
169 OpName.replace(pos, 2, "_");
170 return OpName;
171}
172
173void FastISelEmitter::run(std::ostream &OS) {
174 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000175 Target.getName() + " target", OS);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000176
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000177 OS << "#include \"llvm/CodeGen/FastISel.h\"\n";
178 OS << "\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000179 OS << "namespace llvm {\n";
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000180 OS << "\n";
181 OS << "namespace " << InstNS.substr(0, InstNS.size() - 2) << " {\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000182 OS << "\n";
183
Dan Gohman22bb3112008-08-22 00:20:26 +0000184 typedef std::map<std::string, InstructionMemo> PredMap;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000185 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
186 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
187 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
188 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> OperandsOpcodeTypeRetPredMap;
189 OperandsOpcodeTypeRetPredMap SimplePatterns;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000190
Dan Gohman0bfb7522008-08-22 00:28:15 +0000191 // Scan through all the patterns and record the simple ones.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000192 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
193 E = CGP.ptm_end(); I != E; ++I) {
194 const PatternToMatch &Pattern = *I;
195
196 // For now, just look at Instructions, so that we don't have to worry
197 // about emitting multiple instructions for a pattern.
198 TreePatternNode *Dst = Pattern.getDstPattern();
199 if (Dst->isLeaf()) continue;
200 Record *Op = Dst->getOperator();
201 if (!Op->isSubClassOf("Instruction"))
202 continue;
203 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
204 if (II.OperandList.empty())
205 continue;
Dan Gohman379cad42008-08-19 20:36:33 +0000206
207 // For now, ignore instructions where the first operand is not an
208 // output register.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000209 Record *Op0Rec = II.OperandList[0].Rec;
210 if (!Op0Rec->isSubClassOf("RegisterClass"))
211 continue;
212 const CodeGenRegisterClass *DstRC = &Target.getRegisterClass(Op0Rec);
213 if (!DstRC)
214 continue;
215
216 // Inspect the pattern.
217 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
218 if (!InstPatNode) continue;
219 if (InstPatNode->isLeaf()) continue;
220
221 Record *InstPatOp = InstPatNode->getOperator();
222 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
223 MVT::SimpleValueType VT = InstPatNode->getTypeNum(0);
224
225 // For now, filter out instructions which just set a register to
Dan Gohmanf4137b52008-08-19 20:30:54 +0000226 // an Operand or an immediate, like MOV32ri.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000227 if (InstPatOp->isSubClassOf("Operand"))
228 continue;
Dan Gohmanf4137b52008-08-19 20:30:54 +0000229
230 // For now, filter out any instructions with predicates.
231 if (!InstPatNode->getPredicateFn().empty())
232 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000233
Dan Gohman379cad42008-08-19 20:36:33 +0000234 // Check all the operands.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000235 OperandsSignature Operands;
Dan Gohmancf711aa2008-08-19 20:58:14 +0000236 if (!Operands.initialize(InstPatNode, Target, VT, DstRC))
Dan Gohmand1d2ee82008-08-19 20:56:30 +0000237 continue;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000238
Dan Gohman22bb3112008-08-22 00:20:26 +0000239 // Get the predicate that guards this pattern.
240 std::string PredicateCheck = Pattern.getPredicateCheck();
241
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000242 // Ok, we found a pattern that we can handle. Remember it.
Dan Gohman520b50c2008-08-21 00:35:26 +0000243 InstructionMemo Memo = {
244 Pattern.getDstPattern()->getOperator()->getName(),
245 DstRC
246 };
Owen Anderson7b2e5792008-08-25 23:43:09 +0000247 assert(!SimplePatterns[Operands][OpcodeName][VT][VT].count(PredicateCheck) &&
Dan Gohman22bb3112008-08-22 00:20:26 +0000248 "Duplicate pattern!");
Owen Anderson7b2e5792008-08-25 23:43:09 +0000249 SimplePatterns[Operands][OpcodeName][VT][VT][PredicateCheck] = Memo;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000250 }
251
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000252 // Declare the target FastISel class.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000253 OS << "class FastISel : public llvm::FastISel {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000254 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000255 OE = SimplePatterns.end(); OI != OE; ++OI) {
256 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000257 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000258
Owen Anderson7b2e5792008-08-25 23:43:09 +0000259 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000260 I != E; ++I) {
261 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000262 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000263
Owen Anderson7b2e5792008-08-25 23:43:09 +0000264 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000265 TI != TE; ++TI) {
266 MVT::SimpleValueType VT = TI->first;
Owen Anderson71669e52008-08-26 00:42:26 +0000267 const RetPredMap &RM = TI->second;
268
269 if (RM.size() != 1)
270 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
271 RI != RE; ++RI) {
272 MVT::SimpleValueType RetVT = RI->first;
273 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
274 << "_" << getLegalCName(getName(VT)) << "_"
275 << getLegalCName(getName(RetVT)) << "_";
276 Operands.PrintManglingSuffix(OS);
277 OS << "(";
278 Operands.PrintParameters(OS);
279 OS << ");\n";
280 }
281
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000282 OS << " unsigned FastEmit_" << getLegalCName(Opcode)
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000283 << "_" << getLegalCName(getName(VT)) << "_";
284 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000285 OS << "(MVT::SimpleValueType RetVT";
286 if (!Operands.empty())
287 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000288 Operands.PrintParameters(OS);
289 OS << ");\n";
290 }
291
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000292 OS << " unsigned FastEmit_" << getLegalCName(Opcode) << "_";
293 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000294 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000295 if (!Operands.empty())
296 OS << ", ";
297 Operands.PrintParameters(OS);
298 OS << ");\n";
299 }
300
Dan Gohman56e0f872008-08-19 20:31:38 +0000301 OS << " unsigned FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000302 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000303 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000304 if (!Operands.empty())
305 OS << ", ";
306 Operands.PrintParameters(OS);
307 OS << ");\n";
308 }
Dan Gohman22bb3112008-08-22 00:20:26 +0000309 OS << "\n";
310
311 // Declare the Subtarget member, which is used for predicate checks.
312 OS << " const " << InstNS.substr(0, InstNS.size() - 2)
313 << "Subtarget *Subtarget;\n";
314 OS << "\n";
315
316 // Declare the constructor.
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000317 OS << "public:\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000318 OS << " explicit FastISel(MachineFunction &mf)\n";
319 OS << " : llvm::FastISel(mf),\n";
320 OS << " Subtarget(&TM.getSubtarget<" << InstNS.substr(0, InstNS.size() - 2)
321 << "Subtarget>()) {}\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000322 OS << "};\n";
323 OS << "\n";
324
325 // Define the target FastISel creation function.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000326 OS << "llvm::FastISel *createFastISel(MachineFunction &mf) {\n";
327 OS << " return new FastISel(mf);\n";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000328 OS << "}\n";
329 OS << "\n";
330
331 // Now emit code for all the patterns that we collected.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000332 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000333 OE = SimplePatterns.end(); OI != OE; ++OI) {
334 const OperandsSignature &Operands = OI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000335 const OpcodeTypeRetPredMap &OTM = OI->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000336
Owen Anderson7b2e5792008-08-25 23:43:09 +0000337 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000338 I != E; ++I) {
339 const std::string &Opcode = I->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000340 const TypeRetPredMap &TM = I->second;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000341
342 OS << "// FastEmit functions for " << Opcode << ".\n";
343 OS << "\n";
344
345 // Emit one function for each opcode,type pair.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000346 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000347 TI != TE; ++TI) {
348 MVT::SimpleValueType VT = TI->first;
Owen Anderson7b2e5792008-08-25 23:43:09 +0000349 const RetPredMap &RM = TI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000350 if (RM.size() != 1) {
351 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
352 RI != RE; ++RI) {
353 MVT::SimpleValueType RetVT = RI->first;
354 const PredMap &PM = RI->second;
355 bool HasPred = false;
Dan Gohman22bb3112008-08-22 00:20:26 +0000356
Owen Anderson71669e52008-08-26 00:42:26 +0000357 OS << "unsigned FastISel::FastEmit_"
358 << getLegalCName(Opcode)
359 << "_" << getLegalCName(getName(VT))
360 << "_" << getLegalCName(getName(RetVT)) << "_";
361 Operands.PrintManglingSuffix(OS);
362 OS << "(";
363 Operands.PrintParameters(OS);
364 OS << ") {\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000365
Owen Anderson71669e52008-08-26 00:42:26 +0000366 // Emit code for each possible instruction. There may be
367 // multiple if there are subtarget concerns.
368 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
369 PI != PE; ++PI) {
370 std::string PredicateCheck = PI->first;
371 const InstructionMemo &Memo = PI->second;
372
373 if (PredicateCheck.empty()) {
374 assert(!HasPred &&
375 "Multiple instructions match, at least one has "
376 "a predicate and at least one doesn't!");
377 } else {
378 OS << " if (" + PredicateCheck + ")\n";
379 OS << " ";
380 HasPred = true;
381 }
382 OS << " return FastEmitInst_";
383 Operands.PrintManglingSuffix(OS);
384 OS << "(" << InstNS << Memo.Name << ", ";
385 OS << InstNS << Memo.RC->getName() << "RegisterClass";
386 if (!Operands.empty())
387 OS << ", ";
388 Operands.PrintArguments(OS);
389 OS << ");\n";
390 }
391 // Return 0 if none of the predicates were satisfied.
392 if (HasPred)
393 OS << " return 0;\n";
394 OS << "}\n";
395 OS << "\n";
396 }
397
398 // Emit one function for the type that demultiplexes on return type.
Owen Anderson7b2e5792008-08-25 23:43:09 +0000399 OS << "unsigned FastISel::FastEmit_"
Owen Anderson71669e52008-08-26 00:42:26 +0000400 << getLegalCName(Opcode) << "_"
401 << getLegalCName(getName(VT));
402 Operands.PrintManglingSuffix(OS);
403 OS << "(MVT::SimpleValueType RetVT";
404 if (!Operands.empty())
405 OS << ", ";
406 Operands.PrintParameters(OS);
407 OS << ") {\nswitch (RetVT) {\n";
408 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
409 RI != RE; ++RI) {
410 MVT::SimpleValueType RetVT = RI->first;
411 OS << " case " << getName(RetVT) << ": return FastEmit_"
412 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
413 << "_" << getLegalCName(getName(RetVT)) << "_";
414 Operands.PrintManglingSuffix(OS);
415 OS << "(";
416 Operands.PrintArguments(OS);
417 OS << ");\n";
418 }
419 OS << " default: return 0;\n}\n}\n\n";
420
421 } else {
422 // Non-variadic return type.
423 OS << "unsigned FastISel::FastEmit_"
424 << getLegalCName(Opcode) << "_"
425 << getLegalCName(getName(VT)) << "_";
Dan Gohman22bb3112008-08-22 00:20:26 +0000426 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000427 OS << "(MVT::SimpleValueType RetVT";
428 if (!Operands.empty())
429 OS << ", ";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000430 Operands.PrintParameters(OS);
431 OS << ") {\n";
Owen Anderson71669e52008-08-26 00:42:26 +0000432
433 const PredMap &PM = RM.begin()->second;
434 bool HasPred = false;
435
Owen Anderson7b2e5792008-08-25 23:43:09 +0000436 // Emit code for each possible instruction. There may be
437 // multiple if there are subtarget concerns.
Owen Anderson71669e52008-08-26 00:42:26 +0000438 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; ++PI) {
Owen Anderson7b2e5792008-08-25 23:43:09 +0000439 std::string PredicateCheck = PI->first;
440 const InstructionMemo &Memo = PI->second;
Owen Anderson71669e52008-08-26 00:42:26 +0000441
Owen Anderson7b2e5792008-08-25 23:43:09 +0000442 if (PredicateCheck.empty()) {
443 assert(!HasPred &&
444 "Multiple instructions match, at least one has "
445 "a predicate and at least one doesn't!");
446 } else {
447 OS << " if (" + PredicateCheck + ")\n";
448 OS << " ";
449 HasPred = true;
450 }
451 OS << " return FastEmitInst_";
452 Operands.PrintManglingSuffix(OS);
453 OS << "(" << InstNS << Memo.Name << ", ";
454 OS << InstNS << Memo.RC->getName() << "RegisterClass";
455 if (!Operands.empty())
456 OS << ", ";
457 Operands.PrintArguments(OS);
458 OS << ");\n";
459 }
Owen Anderson71669e52008-08-26 00:42:26 +0000460
Owen Anderson7b2e5792008-08-25 23:43:09 +0000461 // Return 0 if none of the predicates were satisfied.
462 if (HasPred)
463 OS << " return 0;\n";
464 OS << "}\n";
465 OS << "\n";
Dan Gohman22bb3112008-08-22 00:20:26 +0000466 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000467 }
468
469 // Emit one function for the opcode that demultiplexes based on the type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000470 OS << "unsigned FastISel::FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000471 << getLegalCName(Opcode) << "_";
472 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000473 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000474 if (!Operands.empty())
475 OS << ", ";
476 Operands.PrintParameters(OS);
477 OS << ") {\n";
478 OS << " switch (VT) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000479 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000480 TI != TE; ++TI) {
481 MVT::SimpleValueType VT = TI->first;
482 std::string TypeName = getName(VT);
483 OS << " case " << TypeName << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000484 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
485 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000486 OS << "(RetVT";
487 if (!Operands.empty())
488 OS << ", ";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000489 Operands.PrintArguments(OS);
490 OS << ");\n";
491 }
492 OS << " default: return 0;\n";
493 OS << " }\n";
494 OS << "}\n";
495 OS << "\n";
496 }
497
Dan Gohman0bfb7522008-08-22 00:28:15 +0000498 OS << "// Top-level FastEmit function.\n";
499 OS << "\n";
500
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000501 // Emit one function for the operand signature that demultiplexes based
502 // on opcode and type.
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000503 OS << "unsigned FastISel::FastEmit_";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000504 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000505 OS << "(MVT::SimpleValueType VT, MVT::SimpleValueType RetVT, ISD::NodeType Opcode";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000506 if (!Operands.empty())
507 OS << ", ";
508 Operands.PrintParameters(OS);
509 OS << ") {\n";
510 OS << " switch (Opcode) {\n";
Owen Anderson7b2e5792008-08-25 23:43:09 +0000511 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000512 I != E; ++I) {
513 const std::string &Opcode = I->first;
514
515 OS << " case " << Opcode << ": return FastEmit_"
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000516 << getLegalCName(Opcode) << "_";
517 Operands.PrintManglingSuffix(OS);
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000518 OS << "(VT, RetVT";
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000519 if (!Operands.empty())
520 OS << ", ";
521 Operands.PrintArguments(OS);
522 OS << ");\n";
523 }
524 OS << " default: return 0;\n";
525 OS << " }\n";
526 OS << "}\n";
527 OS << "\n";
528 }
529
Dan Gohmanc7f72de2008-08-21 00:19:05 +0000530 OS << "} // namespace X86\n";
531 OS << "\n";
532 OS << "} // namespace llvm\n";
533}
534
535FastISelEmitter::FastISelEmitter(RecordKeeper &R)
536 : Records(R),
537 CGP(R),
538 Target(CGP.getTargetInfo()),
539 InstNS(Target.getInstNamespace() + "::") {
540
541 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000542}