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 "FastISelEmitter.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/VectorExtras.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
30 /// InstructionMemo - This class holds additional information about an
31 /// instruction needed to emit code for it.
33 struct InstructionMemo {
35 const CodeGenRegisterClass *RC;
37 std::vector<std::string>* PhysRegs;
40 /// ImmPredicateSet - This uniques predicates (represented as a string) and
41 /// gives them unique (small) integer ID's that start at 0.
42 class ImmPredicateSet {
43 DenseMap<TreePattern *, unsigned> ImmIDs;
44 std::vector<TreePredicateFn> PredsByName;
47 unsigned getIDFor(TreePredicateFn Pred) {
48 unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()];
50 PredsByName.push_back(Pred);
51 Entry = PredsByName.size();
56 const TreePredicateFn &getPredicate(unsigned i) {
57 assert(i < PredsByName.size());
58 return PredsByName[i];
61 typedef std::vector<TreePredicateFn>::const_iterator iterator;
62 iterator begin() const { return PredsByName.begin(); }
63 iterator end() const { return PredsByName.end(); }
67 /// OperandsSignature - This class holds a description of a list of operand
68 /// types. It has utility methods for emitting text based on the operands.
70 struct OperandsSignature {
72 enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 };
76 OpKind() : Repr(OK_Invalid) {}
78 bool operator<(OpKind RHS) const { return Repr < RHS.Repr; }
79 bool operator==(OpKind RHS) const { return Repr == RHS.Repr; }
81 static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; }
82 static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; }
83 static OpKind getImm(unsigned V) {
84 assert((unsigned)OK_Imm+V < 128 &&
85 "Too many integer predicates for the 'Repr' char");
86 OpKind K; K.Repr = OK_Imm+V; return K;
89 bool isReg() const { return Repr == OK_Reg; }
90 bool isFP() const { return Repr == OK_FP; }
91 bool isImm() const { return Repr >= OK_Imm; }
93 unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; }
95 void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
96 bool StripImmCodes) const {
104 if (unsigned Code = getImmCode())
105 OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName();
111 SmallVector<OpKind, 3> Operands;
113 bool operator<(const OperandsSignature &O) const {
114 return Operands < O.Operands;
116 bool operator==(const OperandsSignature &O) const {
117 return Operands == O.Operands;
120 bool empty() const { return Operands.empty(); }
122 bool hasAnyImmediateCodes() const {
123 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
124 if (Operands[i].isImm() && Operands[i].getImmCode() != 0)
129 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
131 OperandsSignature getWithoutImmCodes() const {
132 OperandsSignature Result;
133 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
134 if (!Operands[i].isImm())
135 Result.Operands.push_back(Operands[i]);
137 Result.Operands.push_back(OpKind::getImm(0));
141 void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) {
142 bool EmittedAnything = false;
143 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
144 if (!Operands[i].isImm()) continue;
146 unsigned Code = Operands[i].getImmCode();
147 if (Code == 0) continue;
152 TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1);
154 // Emit the type check.
156 << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0))
160 OS << PredFn.getFnName() << "(imm" << i <<')';
161 EmittedAnything = true;
165 /// initialize - Examine the given pattern and initialize the contents
166 /// of the Operands array accordingly. Return true if all the operands
167 /// are supported, false otherwise.
169 bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target,
170 MVT::SimpleValueType VT,
171 ImmPredicateSet &ImmediatePredicates) {
172 if (InstPatNode->isLeaf())
175 if (InstPatNode->getOperator()->getName() == "imm") {
176 Operands.push_back(OpKind::getImm(0));
180 if (InstPatNode->getOperator()->getName() == "fpimm") {
181 Operands.push_back(OpKind::getFP());
185 const CodeGenRegisterClass *DstRC = 0;
187 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
188 TreePatternNode *Op = InstPatNode->getChild(i);
190 // Handle imm operands specially.
191 if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") {
193 if (!Op->getPredicateFns().empty()) {
194 TreePredicateFn PredFn = Op->getPredicateFns()[0];
195 // If there is more than one predicate weighing in on this operand
196 // then we don't handle it. This doesn't typically happen for
197 // immediates anyway.
198 if (Op->getPredicateFns().size() > 1 ||
199 !PredFn.isImmediatePattern())
201 // Ignore any instruction with 'FastIselShouldIgnore', these are
202 // not needed and just bloat the fast instruction selector. For
203 // example, X86 doesn't need to generate code to match ADD16ri8 since
204 // ADD16ri will do just fine.
205 Record *Rec = PredFn.getOrigPatFragRecord()->getRecord();
206 if (Rec->getValueAsBit("FastIselShouldIgnore"))
209 PredNo = ImmediatePredicates.getIDFor(PredFn)+1;
212 // Handle unmatched immediate sizes here.
213 //if (Op->getType(0) != VT)
216 Operands.push_back(OpKind::getImm(PredNo));
221 // For now, filter out any operand with a predicate.
222 // For now, filter out any operand with multiple values.
223 if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1)
227 if (Op->getOperator()->getName() == "fpimm") {
228 Operands.push_back(OpKind::getFP());
231 // For now, ignore other non-leaf nodes.
235 assert(Op->hasTypeSet(0) && "Type infererence not done?");
237 // For now, all the operands must have the same type (if they aren't
238 // immediates). Note that this causes us to reject variable sized shifts
240 if (Op->getType(0) != VT)
243 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
246 Record *OpLeafRec = OpDI->getDef();
248 // For now, the only other thing we accept is register operands.
249 const CodeGenRegisterClass *RC = 0;
250 if (OpLeafRec->isSubClassOf("RegisterClass"))
251 RC = &Target.getRegisterClass(OpLeafRec);
252 else if (OpLeafRec->isSubClassOf("Register"))
253 RC = Target.getRegisterClassForRegister(OpLeafRec);
257 // For now, this needs to be a register class of some sort.
261 // For now, all the operands must have the same register class or be
262 // a strict subclass of the destination.
264 if (DstRC != RC && !DstRC->hasSubClass(RC))
268 Operands.push_back(OpKind::getReg());
273 void PrintParameters(raw_ostream &OS) const {
274 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
275 if (Operands[i].isReg()) {
276 OS << "unsigned Op" << i << ", bool Op" << i << "IsKill";
277 } else if (Operands[i].isImm()) {
278 OS << "uint64_t imm" << i;
279 } else if (Operands[i].isFP()) {
280 OS << "ConstantFP *f" << i;
282 llvm_unreachable("Unknown operand kind!");
289 void PrintArguments(raw_ostream &OS,
290 const std::vector<std::string> &PR) const {
291 assert(PR.size() == Operands.size());
292 bool PrintedArg = false;
293 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
295 // Implicit physical register operand.
300 if (Operands[i].isReg()) {
301 OS << "Op" << i << ", Op" << i << "IsKill";
303 } else if (Operands[i].isImm()) {
306 } else if (Operands[i].isFP()) {
310 llvm_unreachable("Unknown operand kind!");
315 void PrintArguments(raw_ostream &OS) const {
316 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
317 if (Operands[i].isReg()) {
318 OS << "Op" << i << ", Op" << i << "IsKill";
319 } else if (Operands[i].isImm()) {
321 } else if (Operands[i].isFP()) {
324 llvm_unreachable("Unknown operand kind!");
332 void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR,
333 ImmPredicateSet &ImmPredicates,
334 bool StripImmCodes = false) const {
335 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
337 // Implicit physical register operand. e.g. Instruction::Mul expect to
338 // select to a binary op. On x86, mul may take a single operand with
339 // the other operand being implicit. We must emit something that looks
340 // like a binary instruction except for the very inner FastEmitInst_*
343 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
347 void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates,
348 bool StripImmCodes = false) const {
349 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
350 Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes);
355 typedef std::map<std::string, InstructionMemo> PredMap;
356 typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap;
357 typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap;
358 typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap;
359 typedef std::map<OperandsSignature, OpcodeTypeRetPredMap>
360 OperandsOpcodeTypeRetPredMap;
362 OperandsOpcodeTypeRetPredMap SimplePatterns;
364 std::map<OperandsSignature, std::vector<OperandsSignature> >
365 SignaturesWithConstantForms;
368 ImmPredicateSet ImmediatePredicates;
370 explicit FastISelMap(std::string InstNS);
372 void collectPatterns(CodeGenDAGPatterns &CGP);
373 void printImmediatePredicates(raw_ostream &OS);
374 void printFunctionDefinitions(raw_ostream &OS);
379 static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
380 return CGP.getSDNodeInfo(Op).getEnumName();
383 static std::string getLegalCName(std::string OpName) {
384 std::string::size_type pos = OpName.find("::");
385 if (pos != std::string::npos)
386 OpName.replace(pos, 2, "_");
390 FastISelMap::FastISelMap(std::string instns)
394 static std::string PhyRegForNode(TreePatternNode *Op,
395 const CodeGenTarget &Target) {
401 DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue());
402 Record *OpLeafRec = OpDI->getDef();
403 if (!OpLeafRec->isSubClassOf("Register"))
406 PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \
407 "Namespace")->getValue())->getValue();
410 std::vector<CodeGenRegister> Regs = Target.getRegisters();
411 for (unsigned i = 0; i < Regs.size(); ++i) {
412 if (Regs[i].TheDef == OpLeafRec) {
413 PhysReg += Regs[i].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("RegisterClass"))
466 DstRC = &Target.getRegisterClass(Op0Rec);
470 // If this isn't a leaf, then continue since the register classes are
471 // a bit too complicated for now.
472 if (!Dst->getChild(1)->isLeaf()) continue;
474 DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue());
476 SubRegNo = getQualifiedName(SR->getDef());
478 SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString();
481 // Inspect the pattern.
482 TreePatternNode *InstPatNode = Pattern.getSrcPattern();
483 if (!InstPatNode) continue;
484 if (InstPatNode->isLeaf()) continue;
486 // Ignore multiple result nodes for now.
487 if (InstPatNode->getNumTypes() > 1) continue;
489 Record *InstPatOp = InstPatNode->getOperator();
490 std::string OpcodeName = getOpcodeName(InstPatOp, CGP);
491 MVT::SimpleValueType RetVT = MVT::isVoid;
492 if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0);
493 MVT::SimpleValueType VT = RetVT;
494 if (InstPatNode->getNumChildren()) {
495 assert(InstPatNode->getChild(0)->getNumTypes() == 1);
496 VT = InstPatNode->getChild(0)->getType(0);
499 // For now, filter out any instructions with predicates.
500 if (!InstPatNode->getPredicateFns().empty())
503 // Check all the operands.
504 OperandsSignature Operands;
505 if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates))
508 std::vector<std::string>* PhysRegInputs = new std::vector<std::string>();
509 if (InstPatNode->getOperator()->getName() == "imm" ||
510 InstPatNode->getOperator()->getName() == "fpimmm")
511 PhysRegInputs->push_back("");
513 // Compute the PhysRegs used by the given pattern, and check that
514 // the mapping from the src to dst patterns is simple.
515 bool FoundNonSimplePattern = false;
516 unsigned DstIndex = 0;
517 for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) {
518 std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target);
519 if (PhysReg.empty()) {
520 if (DstIndex >= Dst->getNumChildren() ||
521 Dst->getChild(DstIndex)->getName() !=
522 InstPatNode->getChild(i)->getName()) {
523 FoundNonSimplePattern = true;
529 PhysRegInputs->push_back(PhysReg);
532 if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren())
533 FoundNonSimplePattern = true;
535 if (FoundNonSimplePattern)
539 // Get the predicate that guards this pattern.
540 std::string PredicateCheck = Pattern.getPredicateCheck();
542 // Ok, we found a pattern that we can handle. Remember it.
543 InstructionMemo Memo = {
544 Pattern.getDstPattern()->getOperator()->getName(),
550 if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck))
551 throw TGError(Pattern.getSrcRecord()->getLoc(),
552 "Duplicate record in FastISel table!");
554 SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo;
556 // If any of the operands were immediates with predicates on them, strip
557 // them down to a signature that doesn't have predicates so that we can
558 // associate them with the stripped predicate version.
559 if (Operands.hasAnyImmediateCodes()) {
560 SignaturesWithConstantForms[Operands.getWithoutImmCodes()]
561 .push_back(Operands);
566 void FastISelMap::printImmediatePredicates(raw_ostream &OS) {
567 if (ImmediatePredicates.begin() == ImmediatePredicates.end())
570 OS << "\n// FastEmit Immediate Predicate functions.\n";
571 for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(),
572 E = ImmediatePredicates.end(); I != E; ++I) {
573 OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n";
574 OS << I->getImmediatePredicateCode() << "\n}\n";
581 void FastISelMap::printFunctionDefinitions(raw_ostream &OS) {
582 // Now emit code for all the patterns that we collected.
583 for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(),
584 OE = SimplePatterns.end(); OI != OE; ++OI) {
585 const OperandsSignature &Operands = OI->first;
586 const OpcodeTypeRetPredMap &OTM = OI->second;
588 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
590 const std::string &Opcode = I->first;
591 const TypeRetPredMap &TM = I->second;
593 OS << "// FastEmit functions for " << Opcode << ".\n";
596 // Emit one function for each opcode,type pair.
597 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
599 MVT::SimpleValueType VT = TI->first;
600 const RetPredMap &RM = TI->second;
601 if (RM.size() != 1) {
602 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
604 MVT::SimpleValueType RetVT = RI->first;
605 const PredMap &PM = RI->second;
606 bool HasPred = false;
608 OS << "unsigned FastEmit_"
609 << getLegalCName(Opcode)
610 << "_" << getLegalCName(getName(VT))
611 << "_" << getLegalCName(getName(RetVT)) << "_";
612 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
614 Operands.PrintParameters(OS);
617 // Emit code for each possible instruction. There may be
618 // multiple if there are subtarget concerns.
619 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end();
621 std::string PredicateCheck = PI->first;
622 const InstructionMemo &Memo = PI->second;
624 if (PredicateCheck.empty()) {
626 "Multiple instructions match, at least one has "
627 "a predicate and at least one doesn't!");
629 OS << " if (" + PredicateCheck + ") {\n";
634 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
635 if ((*Memo.PhysRegs)[i] != "")
636 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
637 << "TII.get(TargetOpcode::COPY), "
638 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
641 OS << " return FastEmitInst_";
642 if (Memo.SubRegNo.empty()) {
643 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
644 ImmediatePredicates, true);
645 OS << "(" << InstNS << Memo.Name << ", ";
646 OS << InstNS << Memo.RC->getName() << "RegisterClass";
647 if (!Operands.empty())
649 Operands.PrintArguments(OS, *Memo.PhysRegs);
652 OS << "extractsubreg(" << getName(RetVT);
653 OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n";
660 // Return 0 if none of the predicates were satisfied.
662 OS << " return 0;\n";
667 // Emit one function for the type that demultiplexes on return type.
668 OS << "unsigned FastEmit_"
669 << getLegalCName(Opcode) << "_"
670 << getLegalCName(getName(VT)) << "_";
671 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
673 if (!Operands.empty())
675 Operands.PrintParameters(OS);
676 OS << ") {\nswitch (RetVT.SimpleTy) {\n";
677 for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end();
679 MVT::SimpleValueType RetVT = RI->first;
680 OS << " case " << getName(RetVT) << ": return FastEmit_"
681 << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT))
682 << "_" << getLegalCName(getName(RetVT)) << "_";
683 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
685 Operands.PrintArguments(OS);
688 OS << " default: return 0;\n}\n}\n\n";
691 // Non-variadic return type.
692 OS << "unsigned FastEmit_"
693 << getLegalCName(Opcode) << "_"
694 << getLegalCName(getName(VT)) << "_";
695 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
697 if (!Operands.empty())
699 Operands.PrintParameters(OS);
702 OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first)
703 << ")\n return 0;\n";
705 const PredMap &PM = RM.begin()->second;
706 bool HasPred = false;
708 // Emit code for each possible instruction. There may be
709 // multiple if there are subtarget concerns.
710 for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE;
712 std::string PredicateCheck = PI->first;
713 const InstructionMemo &Memo = PI->second;
715 if (PredicateCheck.empty()) {
717 "Multiple instructions match, at least one has "
718 "a predicate and at least one doesn't!");
720 OS << " if (" + PredicateCheck + ") {\n";
725 for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) {
726 if ((*Memo.PhysRegs)[i] != "")
727 OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
728 << "TII.get(TargetOpcode::COPY), "
729 << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n";
732 OS << " return FastEmitInst_";
734 if (Memo.SubRegNo.empty()) {
735 Operands.PrintManglingSuffix(OS, *Memo.PhysRegs,
736 ImmediatePredicates, true);
737 OS << "(" << InstNS << Memo.Name << ", ";
738 OS << InstNS << Memo.RC->getName() << "RegisterClass";
739 if (!Operands.empty())
741 Operands.PrintArguments(OS, *Memo.PhysRegs);
744 OS << "extractsubreg(RetVT, Op0, Op0IsKill, ";
753 // Return 0 if none of the predicates were satisfied.
755 OS << " return 0;\n";
761 // Emit one function for the opcode that demultiplexes based on the type.
762 OS << "unsigned FastEmit_"
763 << getLegalCName(Opcode) << "_";
764 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
765 OS << "(MVT VT, MVT RetVT";
766 if (!Operands.empty())
768 Operands.PrintParameters(OS);
770 OS << " switch (VT.SimpleTy) {\n";
771 for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end();
773 MVT::SimpleValueType VT = TI->first;
774 std::string TypeName = getName(VT);
775 OS << " case " << TypeName << ": return FastEmit_"
776 << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_";
777 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
779 if (!Operands.empty())
781 Operands.PrintArguments(OS);
784 OS << " default: return 0;\n";
790 OS << "// Top-level FastEmit function.\n";
793 // Emit one function for the operand signature that demultiplexes based
794 // on opcode and type.
795 OS << "unsigned FastEmit_";
796 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
797 OS << "(MVT VT, MVT RetVT, unsigned Opcode";
798 if (!Operands.empty())
800 Operands.PrintParameters(OS);
803 // If there are any forms of this signature available that operand on
804 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
805 // 64-bit operand), check them first.
807 std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI
808 = SignaturesWithConstantForms.find(Operands);
809 if (MI != SignaturesWithConstantForms.end()) {
810 // Unique any duplicates out of the list.
811 std::sort(MI->second.begin(), MI->second.end());
812 MI->second.erase(std::unique(MI->second.begin(), MI->second.end()),
815 // Check each in order it was seen. It would be nice to have a good
816 // relative ordering between them, but we're not going for optimality
818 for (unsigned i = 0, e = MI->second.size(); i != e; ++i) {
820 MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates);
821 OS << ")\n if (unsigned Reg = FastEmit_";
822 MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates);
823 OS << "(VT, RetVT, Opcode";
824 if (!MI->second[i].empty())
826 MI->second[i].PrintArguments(OS);
827 OS << "))\n return Reg;\n\n";
830 // Done with this, remove it.
831 SignaturesWithConstantForms.erase(MI);
834 OS << " switch (Opcode) {\n";
835 for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end();
837 const std::string &Opcode = I->first;
839 OS << " case " << Opcode << ": return FastEmit_"
840 << getLegalCName(Opcode) << "_";
841 Operands.PrintManglingSuffix(OS, ImmediatePredicates);
843 if (!Operands.empty())
845 Operands.PrintArguments(OS);
848 OS << " default: return 0;\n";
854 // TODO: SignaturesWithConstantForms should be empty here.
857 void FastISelEmitter::run(raw_ostream &OS) {
858 const CodeGenTarget &Target = CGP.getTargetInfo();
860 // Determine the target's namespace name.
861 std::string InstNS = Target.getInstNamespace() + "::";
862 assert(InstNS.size() > 2 && "Can't determine target-specific namespace!");
864 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
865 Target.getName() + " target", OS);
867 FastISelMap F(InstNS);
868 F.collectPatterns(CGP);
869 F.printImmediatePredicates(OS);
870 F.printFunctionDefinitions(OS);
873 FastISelEmitter::FastISelEmitter(RecordKeeper &R)
874 : Records(R), CGP(R) {