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 code for use by the "fast" instruction
11 // selection algorithm. See the comments at the top of
12 // lib/CodeGen/SelectionDAG/FastISel.cpp for background.
14 // This file scans through the target's tablegen instruction-info files
15 // and extracts instructions with obvious-looking patterns, and it emits
16 // code to look up these instructions by type and operator.
18 //===----------------------------------------------------------------------===//
20 #include "CodeGenDAGPatterns.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
26 #include "llvm/TableGen/TableGenBackend.h"
30 /// InstructionMemo - This class holds additional information about an
31 /// instruction needed to emit code for it.
34 struct InstructionMemo {
36 const CodeGenRegisterClass *RC;
38 std::vector<std::string>* PhysRegs;
40 } // End anonymous namespace
42 /// ImmPredicateSet - This uniques predicates (represented as a string) and
43 /// gives them unique (small) integer ID's that start at 0.
45 class ImmPredicateSet {
46 DenseMap<TreePattern *, unsigned> ImmIDs;
47 std::vector<TreePredicateFn> PredsByName;
50 unsigned getIDFor(TreePredicateFn Pred) {
51 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
53 PredsByName.push_back(Pred);
54 Entry = PredsByName.size();
59 const TreePredicateFn &getPredicate(unsigned i) {
60 assert(i < PredsByName.size());
61 return PredsByName[i];
64 typedef std::vector<TreePredicateFn>::const_iterator iterator;
65 iterator begin() const { return PredsByName.begin(); }
66 iterator end() const { return PredsByName.end(); }
69 } // End anonymous namespace
71 /// OperandsSignature - This class holds a description of a list of operand
72 /// types. It has utility methods for emitting text based on the operands.
75 struct OperandsSignature {
77 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
81 OpKind() : Repr(OK_Invalid) {}
83 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
84 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
86 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
87 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
88 static OpKind getImm(unsigned V) {
89 assert((unsigned)OK_Imm+V < 128 &&
90 "Too many integer predicates for the 'Repr' char");
91 OpKind K; K.Repr = OK_Imm+V; return K;
94 bool isReg() const { return Repr == OK_Reg; }
95 bool isFP() const { return Repr == OK_FP; }
96 bool isImm() const { return Repr >= OK_Imm; }
98 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
100 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
101 bool StripImmCodes) const {
109 if (unsigned Code = getImmCode())
110 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
116 SmallVector<OpKind, 3> Operands;
118 bool operator<(const OperandsSignature &O) const {
119 return Operands < O.Operands;
121 bool operator==(const OperandsSignature &O) const {
122 return Operands == O.Operands;
125 bool empty() const { return Operands.empty(); }
127 bool hasAnyImmediateCodes() const {
128 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
129 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
134 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
136 OperandsSignature getWithoutImmCodes() const {
137 OperandsSignature Result;
138 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
139 if (!Operands[i].isImm())
140 Result.Operands.push_back(Operands[i]);
142 Result.Operands.push_back(OpKind::getImm(0));
146 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
147 bool EmittedAnything = false;
148 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
149 if (!Operands[i].isImm()) continue;
151 unsigned Code = Operands[i].getImmCode();
152 if (Code == 0) continue;
157 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
159 // Emit the type check.
161 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
165 OS << PredFn.getFnName() << "(imm" << i <<')';
166 EmittedAnything = true;
170 /// initialize - Examine the given pattern and initialize the contents
171 /// of the Operands array accordingly. Return true if all the operands
172 /// are supported, false otherwise.
174 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
175 MVT::SimpleValueType VT,
176 ImmPredicateSet &ImmediatePredicates) {
177 if (InstPatNode->isLeaf())
180 if (InstPatNode->getOperator()->getName() == "imm") {
181 Operands.push_back(OpKind::getImm(0));
185 if (InstPatNode->getOperator()->getName() == "fpimm") {
186 Operands.push_back(OpKind::getFP());
190 const CodeGenRegisterClass *DstRC = 0;
192 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
193 TreePatternNode *Op = InstPatNode->getChild(i);
195 // Handle imm operands specially.
196 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
198 if (!Op->getPredicateFns().empty()) {
199 TreePredicateFn PredFn = Op->getPredicateFns()[0];
200 // If there is more than one predicate weighing in on this operand
201 // then we don't handle it. This doesn't typically happen for
202 // immediates anyway.
203 if (Op->getPredicateFns().size() > 1 ||
204 !PredFn.isImmediatePattern())
206 // Ignore any instruction with 'FastIselShouldIgnore', these are
207 // not needed and just bloat the fast instruction selector. For
208 // example, X86 doesn't need to generate code to match ADD16ri8 since
209 // ADD16ri will do just fine.
210 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
211 if (Rec->getValueAsBit("FastIselShouldIgnore"))
214 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
217 // Handle unmatched immediate sizes here.
218 //if (Op->getType(0) != VT)
221 Operands.push_back(OpKind::getImm(PredNo));
226 // For now, filter out any operand with a predicate.
227 // For now, filter out any operand with multiple values.
228 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
232 if (Op->getOperator()->getName() == "fpimm") {
233 Operands.push_back(OpKind::getFP());
236 // For now, ignore other non-leaf nodes.
240 assert(Op->hasTypeSet(0) && "Type infererence not done?");
242 // For now, all the operands must have the same type (if they aren't
243 // immediates). Note that this causes us to reject variable sized shifts
245 if (Op->getType(0) != VT)
248 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
251 Record *OpLeafRec = OpDI->getDef();
253 // For now, the only other thing we accept is register operands.
254 const CodeGenRegisterClass *RC = 0;
255 if (OpLeafRec->isSubClassOf("RegisterOperand"))
256 OpLeafRec = OpLeafRec->getValueAsDef("RegClass");
257 if (OpLeafRec->isSubClassOf("RegisterClass"))
258 RC = &Target.getRegisterClass(OpLeafRec);
259 else if (OpLeafRec->isSubClassOf("Register"))
260 RC = Target.getRegBank().getRegClassForRegister(OpLeafRec);
264 // For now, this needs to be a register class of some sort.
268 // For now, all the operands must have the same register class or be
269 // a strict subclass of the destination.
271 if (DstRC != RC && !DstRC->hasSubClass(RC))
275 Operands.push_back(OpKind::getReg());
280 void PrintParameters(raw_ostream &OS) const {
281 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
282 if (Operands[i].isReg()) {
283 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
284 } else if (Operands[i].isImm()) {
285 OS << "uint64_t imm" << i;
286 } else if (Operands[i].isFP()) {
287 OS << "const ConstantFP *f" << i;
289 llvm_unreachable("Unknown operand kind!");
296 void PrintArguments(raw_ostream &OS,
297 const std::vector<std::string> &PR) const {
298 assert(PR.size() == Operands.size());
299 bool PrintedArg = false;
300 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
302 // Implicit physical register operand.
307 if (Operands[i].isReg()) {
308 OS << "Op" << i << ", Op" << i << "IsKill";
310 } else if (Operands[i].isImm()) {
313 } else if (Operands[i].isFP()) {
317 llvm_unreachable("Unknown operand kind!");
322 void PrintArguments(raw_ostream &OS) const {
323 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
324 if (Operands[i].isReg()) {
325 OS << "Op" << i << ", Op" << i << "IsKill";
326 } else if (Operands[i].isImm()) {
328 } else if (Operands[i].isFP()) {
331 llvm_unreachable("Unknown operand kind!");
339 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
340 ImmPredicateSet &ImmPredicates,
341 bool StripImmCodes = false) const {
342 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
344 // Implicit physical register operand. e.g. Instruction::Mul expect to
345 // select to a binary op. On x86, mul may take a single operand with
346 // the other operand being implicit. We must emit something that looks
347 // like a binary instruction except for the very inner FastEmitInst_*
350 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
354 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
355 bool StripImmCodes = false) const {
356 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
357 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
360 } // End anonymous namespace
364 typedef std::map<std::string, InstructionMemo> PredMap;
365 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
366 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
367 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
368 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
369 OperandsOpcodeTypeRetPredMap;
371 OperandsOpcodeTypeRetPredMap SimplePatterns;
373 std::map<OperandsSignature, std::vector<OperandsSignature> >
374 SignaturesWithConstantForms;
377 ImmPredicateSet ImmediatePredicates;
379 explicit FastISelMap(std::string InstNS);
381 void collectPatterns(CodeGenDAGPatterns &CGP);
382 void printImmediatePredicates(raw_ostream &OS);
383 void printFunctionDefinitions(raw_ostream &OS);
385 } // End anonymous namespace
387 static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
388 return CGP.getSDNodeInfo(Op).getEnumName();
391 static std::string getLegalCName(std::string OpName) {
392 std::string::size_type pos = OpName.find("::");
393 if (pos != std::string::npos)
394 OpName.replace(pos, 2, "_");
398 FastISelMap::FastISelMap(std::string instns)
402 static std::string PhyRegForNode(TreePatternNode *Op,
403 const CodeGenTarget &Target) {
409 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
410 Record *OpLeafRec = OpDI->getDef();
411 if (!OpLeafRec->isSubClassOf("Register"))
414 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
415 "Namespace")->getValue())->getValue();
417 PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName();
421 void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) {
422 const CodeGenTarget &Target = CGP.getTargetInfo();
424 // Determine the target's namespace name.
425 InstNS = Target.getInstNamespace() + "::";
426 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
428 // Scan through all the patterns and record the simple ones.
429 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
430 E = CGP.ptm_end(); I != E; ++I) {
431 const PatternToMatch &Pattern = *I;
433 // For now, just look at Instructions, so that we don't have to worry
434 // about emitting multiple instructions for a pattern.
435 TreePatternNode *Dst = Pattern.getDstPattern();
436 if (Dst->isLeaf()) continue;
437 Record *Op = Dst->getOperator();
438 if (!Op->isSubClassOf("Instruction"))
440 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
441 if (II.Operands.empty())
444 // For now, ignore multi-instruction patterns.
445 bool MultiInsts = false;
446 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
447 TreePatternNode *ChildOp = Dst->getChild(i);
448 if (ChildOp->isLeaf())
450 if (ChildOp->getOperator()->isSubClassOf("Instruction")) {
458 // For now, ignore instructions where the first operand is not an
460 const CodeGenRegisterClass *DstRC = 0;
461 std::string SubRegNo;
462 if (Op->getName() != "EXTRACT_SUBREG") {
463 Record *Op0Rec = II.Operands[0].Rec;
464 if (Op0Rec->isSubClassOf("RegisterOperand"))
465 Op0Rec = Op0Rec->getValueAsDef("RegClass");
466 if (!Op0Rec->isSubClassOf("RegisterClass"))
468 DstRC = &Target.getRegisterClass(Op0Rec);
472 // If this isn't a leaf, then continue since the register classes are
473 // a bit too complicated for now.
474 if (!Dst->getChild(1)->isLeaf()) continue;
476 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
478 SubRegNo = getQualifiedName(SR->getDef());
480 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
483 // Inspect the pattern.
484 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
485 if (!InstPatNode) continue;
486 if (InstPatNode->isLeaf()) continue;
488 // Ignore multiple result nodes for now.
489 if (InstPatNode->getNumTypes() > 1) continue;
491 Record *InstPatOp = InstPatNode->getOperator();
492 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
493 MVT::SimpleValueType RetVT = MVT::isVoid;
494 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
495 MVT::SimpleValueType VT = RetVT;
496 if (InstPatNode->getNumChildren()) {
497 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
498 VT = InstPatNode->getChild(0)->getType(0);
501 // For now, filter out any instructions with predicates.
502 if (!InstPatNode->getPredicateFns().empty())
505 // Check all the operands.
506 OperandsSignature Operands;
507 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
510 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
511 if (InstPatNode->getOperator()->getName() == "imm" ||
512 InstPatNode->getOperator()->getName() == "fpimm")
513 PhysRegInputs->push_back("");
515 // Compute the PhysRegs used by the given pattern, and check that
516 // the mapping from the src to dst patterns is simple.
517 bool FoundNonSimplePattern = false;
518 unsigned DstIndex = 0;
519 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
520 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
521 if (PhysReg.empty()) {
522 if (DstIndex >= Dst->getNumChildren() ||
523 Dst->getChild(DstIndex)->getName() !=
524 InstPatNode->getChild(i)->getName()) {
525 FoundNonSimplePattern = true;
531 PhysRegInputs->push_back(PhysReg);
534 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
535 FoundNonSimplePattern = true;
537 if (FoundNonSimplePattern)
541 // Get the predicate that guards this pattern.
542 std::string PredicateCheck = Pattern.getPredicateCheck();
544 // Ok, we found a pattern that we can handle. Remember it.
545 InstructionMemo Memo = {
546 Pattern.getDstPattern()->getOperator()->getName(),
552 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
553 throw TGError(Pattern.getSrcRecord()->getLoc(),
554 "Duplicate record in FastISel table!");
556 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
558 // If any of the operands were immediates with predicates on them, strip
559 // them down to a signature that doesn't have predicates so that we can
560 // associate them with the stripped predicate version.
561 if (Operands.hasAnyImmediateCodes()) {
562 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
563 .push_back(Operands);
568 void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
569 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
572 OS << "\n// FastEmit Immediate Predicate functions.\n";
573 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
574 E = ImmediatePredicates.end(); I != E; ++I) {
575 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
576 OS << I->getImmediatePredicateCode() << "\n}\n";
583 void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
584 // Now emit code for all the patterns that we collected.
585 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
586 OE = SimplePatterns.end(); OI != OE; ++OI) {
587 const OperandsSignature &Operands = OI->first;
588 const OpcodeTypeRetPredMap &OTM = OI->second;
590 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
592 const std::string &Opcode = I->first;
593 const TypeRetPredMap &TM = I->second;
595 OS << "// FastEmit functions for " << Opcode << ".\n";
598 // Emit one function for each opcode,type pair.
599 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
601 MVT::SimpleValueType VT = TI->first;
602 const RetPredMap &RM = TI->second;
603 if (RM.size() != 1) {
604 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
606 MVT::SimpleValueType RetVT = RI->first;
607 const PredMap &PM = RI->second;
608 bool HasPred = false;
610 OS << "unsigned FastEmit_"
611 << getLegalCName(Opcode)
612 << "_" << getLegalCName(getName(VT))
613 << "_" << getLegalCName(getName(RetVT)) << "_";
614 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
616 Operands.PrintParameters(OS);
619 // Emit code for each possible instruction. There may be
620 // multiple if there are subtarget concerns.
621 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
623 std::string PredicateCheck = PI->first;
624 const InstructionMemo &Memo = PI->second;
626 if (PredicateCheck.empty()) {
628 "Multiple instructions match, at least one has "
629 "a predicate and at least one doesn't!");
631 OS << " if (" + PredicateCheck + ") {\n";
636 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
637 if ((*Memo.PhysRegs)[i] != "")
638 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
639 << "TII.get(TargetOpcode::COPY), "
640 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
643 OS << " return FastEmitInst_";
644 if (Memo.SubRegNo.empty()) {
645 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
646 ImmediatePredicates, true);
647 OS << "(" << InstNS << Memo.Name << ", ";
648 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
649 if (!Operands.empty())
651 Operands.PrintArguments(OS, *Memo.PhysRegs);
654 OS << "extractsubreg(" << getName(RetVT);
655 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
662 // Return 0 if none of the predicates were satisfied.
664 OS << " return 0;\n";
669 // Emit one function for the type that demultiplexes on return type.
670 OS << "unsigned FastEmit_"
671 << getLegalCName(Opcode) << "_"
672 << getLegalCName(getName(VT)) << "_";
673 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
675 if (!Operands.empty())
677 Operands.PrintParameters(OS);
678 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
679 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
681 MVT::SimpleValueType RetVT = RI->first;
682 OS << " case " << getName(RetVT) << ": return FastEmit_"
683 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
684 << "_" << getLegalCName(getName(RetVT)) << "_";
685 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
687 Operands.PrintArguments(OS);
690 OS << " default: return 0;\n}\n}\n\n";
693 // Non-variadic return type.
694 OS << "unsigned FastEmit_"
695 << getLegalCName(Opcode) << "_"
696 << getLegalCName(getName(VT)) << "_";
697 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
699 if (!Operands.empty())
701 Operands.PrintParameters(OS);
704 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
705 << ")\n return 0;\n";
707 const PredMap &PM = RM.begin()->second;
708 bool HasPred = false;
710 // Emit code for each possible instruction. There may be
711 // multiple if there are subtarget concerns.
712 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
714 std::string PredicateCheck = PI->first;
715 const InstructionMemo &Memo = PI->second;
717 if (PredicateCheck.empty()) {
719 "Multiple instructions match, at least one has "
720 "a predicate and at least one doesn't!");
722 OS << " if (" + PredicateCheck + ") {\n";
727 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
728 if ((*Memo.PhysRegs)[i] != "")
729 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
730 << "TII.get(TargetOpcode::COPY), "
731 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
734 OS << " return FastEmitInst_";
736 if (Memo.SubRegNo.empty()) {
737 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
738 ImmediatePredicates, true);
739 OS << "(" << InstNS << Memo.Name << ", ";
740 OS << "&" << InstNS << Memo.RC->getName() << "RegClass";
741 if (!Operands.empty())
743 Operands.PrintArguments(OS, *Memo.PhysRegs);
746 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
755 // Return 0 if none of the predicates were satisfied.
757 OS << " return 0;\n";
763 // Emit one function for the opcode that demultiplexes based on the type.
764 OS << "unsigned FastEmit_"
765 << getLegalCName(Opcode) << "_";
766 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
767 OS << "(MVT VT, MVT RetVT";
768 if (!Operands.empty())
770 Operands.PrintParameters(OS);
772 OS << " switch (VT.SimpleTy) {\n";
773 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
775 MVT::SimpleValueType VT = TI->first;
776 std::string TypeName = getName(VT);
777 OS << " case " << TypeName << ": return FastEmit_"
778 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
779 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
781 if (!Operands.empty())
783 Operands.PrintArguments(OS);
786 OS << " default: return 0;\n";
792 OS << "// Top-level FastEmit function.\n";
795 // Emit one function for the operand signature that demultiplexes based
796 // on opcode and type.
797 OS << "unsigned FastEmit_";
798 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
799 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
800 if (!Operands.empty())
802 Operands.PrintParameters(OS);
805 // If there are any forms of this signature available that operand on
806 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
807 // 64-bit operand), check them first.
809 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
810 = SignaturesWithConstantForms.find(Operands);
811 if (MI != SignaturesWithConstantForms.end()) {
812 // Unique any duplicates out of the list.
813 std::sort(MI->second.begin(), MI->second.end());
814 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
817 // Check each in order it was seen. It would be nice to have a good
818 // relative ordering between them, but we're not going for optimality
820 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
822 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
823 OS << ")\n if (unsigned Reg = FastEmit_";
824 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
825 OS << "(VT, RetVT, Opcode";
826 if (!MI->second[i].empty())
828 MI->second[i].PrintArguments(OS);
829 OS << "))\n return Reg;\n\n";
832 // Done with this, remove it.
833 SignaturesWithConstantForms.erase(MI);
836 OS << " switch (Opcode) {\n";
837 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
839 const std::string &Opcode = I->first;
841 OS << " case " << Opcode << ": return FastEmit_"
842 << getLegalCName(Opcode) << "_";
843 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
845 if (!Operands.empty())
847 Operands.PrintArguments(OS);
850 OS << " default: return 0;\n";
856 // TODO: SignaturesWithConstantForms should be empty here.
861 void EmitFastISel(RecordKeeper &RK, raw_ostream &OS) {
862 CodeGenDAGPatterns CGP(RK);
863 const CodeGenTarget &Target = CGP.getTargetInfo();
864 emitSourceFileHeader("\"Fast\" Instruction Selector for the " +
865 Target.getName() + " target", OS);
867 // Determine the target's namespace name.
868 std::string InstNS = Target.getInstNamespace() + "::";
869 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
871 FastISelMap F(InstNS);
872 F.collectPatterns(CGP);
873 F.printImmediatePredicates(OS);
874 F.printFunctionDefinitions(OS);
877 } // End llvm namespace