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"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/VectorExtras.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
31 /// InstructionMemo - This class holds additional information about an
32 /// instruction needed to emit code for it.
34 struct InstructionMemo
{
36 const CodeGenRegisterClass
*RC
;
38 std::vector
<std::string
>* PhysRegs
;
41 /// ImmPredicateSet - This uniques predicates (represented as a string) and
42 /// gives them unique (small) integer ID's that start at 0.
43 class ImmPredicateSet
{
44 DenseMap
<TreePattern
*, unsigned> ImmIDs
;
45 std::vector
<TreePredicateFn
> PredsByName
;
48 unsigned getIDFor(TreePredicateFn Pred
) {
49 unsigned &Entry
= ImmIDs
[Pred
.getOrigPatFragRecord()];
51 PredsByName
.push_back(Pred
);
52 Entry
= PredsByName
.size();
57 const TreePredicateFn
&getPredicate(unsigned i
) {
58 assert(i
< PredsByName
.size());
59 return PredsByName
[i
];
62 typedef std::vector
<TreePredicateFn
>::const_iterator iterator
;
63 iterator
begin() const { return PredsByName
.begin(); }
64 iterator
end() const { return PredsByName
.end(); }
68 /// OperandsSignature - This class holds a description of a list of operand
69 /// types. It has utility methods for emitting text based on the operands.
71 struct OperandsSignature
{
73 enum { OK_Reg
, OK_FP
, OK_Imm
, OK_Invalid
= -1 };
77 OpKind() : Repr(OK_Invalid
) {}
79 bool operator<(OpKind RHS
) const { return Repr
< RHS
.Repr
; }
80 bool operator==(OpKind RHS
) const { return Repr
== RHS
.Repr
; }
82 static OpKind
getReg() { OpKind K
; K
.Repr
= OK_Reg
; return K
; }
83 static OpKind
getFP() { OpKind K
; K
.Repr
= OK_FP
; return K
; }
84 static OpKind
getImm(unsigned V
) {
85 assert((unsigned)OK_Imm
+V
< 128 &&
86 "Too many integer predicates for the 'Repr' char");
87 OpKind K
; K
.Repr
= OK_Imm
+V
; return K
;
90 bool isReg() const { return Repr
== OK_Reg
; }
91 bool isFP() const { return Repr
== OK_FP
; }
92 bool isImm() const { return Repr
>= OK_Imm
; }
94 unsigned getImmCode() const { assert(isImm()); return Repr
-OK_Imm
; }
96 void printManglingSuffix(raw_ostream
&OS
, ImmPredicateSet
&ImmPredicates
,
97 bool StripImmCodes
) const {
105 if (unsigned Code
= getImmCode())
106 OS
<< "_" << ImmPredicates
.getPredicate(Code
-1).getFnName();
112 SmallVector
<OpKind
, 3> Operands
;
114 bool operator<(const OperandsSignature
&O
) const {
115 return Operands
< O
.Operands
;
117 bool operator==(const OperandsSignature
&O
) const {
118 return Operands
== O
.Operands
;
121 bool empty() const { return Operands
.empty(); }
123 bool hasAnyImmediateCodes() const {
124 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
)
125 if (Operands
[i
].isImm() && Operands
[i
].getImmCode() != 0)
130 /// getWithoutImmCodes - Return a copy of this with any immediate codes forced
132 OperandsSignature
getWithoutImmCodes() const {
133 OperandsSignature Result
;
134 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
)
135 if (!Operands
[i
].isImm())
136 Result
.Operands
.push_back(Operands
[i
]);
138 Result
.Operands
.push_back(OpKind::getImm(0));
142 void emitImmediatePredicate(raw_ostream
&OS
, ImmPredicateSet
&ImmPredicates
) {
143 bool EmittedAnything
= false;
144 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
) {
145 if (!Operands
[i
].isImm()) continue;
147 unsigned Code
= Operands
[i
].getImmCode();
148 if (Code
== 0) continue;
153 TreePredicateFn PredFn
= ImmPredicates
.getPredicate(Code
-1);
155 // Emit the type check.
157 << getEnumName(PredFn
.getOrigPatFragRecord()->getTree(0)->getType(0))
161 OS
<< PredFn
.getFnName() << "(imm" << i
<<')';
162 EmittedAnything
= true;
166 /// initialize - Examine the given pattern and initialize the contents
167 /// of the Operands array accordingly. Return true if all the operands
168 /// are supported, false otherwise.
170 bool initialize(TreePatternNode
*InstPatNode
, const CodeGenTarget
&Target
,
171 MVT::SimpleValueType VT
,
172 ImmPredicateSet
&ImmediatePredicates
) {
173 if (InstPatNode
->isLeaf())
176 if (InstPatNode
->getOperator()->getName() == "imm") {
177 Operands
.push_back(OpKind::getImm(0));
181 if (InstPatNode
->getOperator()->getName() == "fpimm") {
182 Operands
.push_back(OpKind::getFP());
186 const CodeGenRegisterClass
*DstRC
= 0;
188 for (unsigned i
= 0, e
= InstPatNode
->getNumChildren(); i
!= e
; ++i
) {
189 TreePatternNode
*Op
= InstPatNode
->getChild(i
);
191 // Handle imm operands specially.
192 if (!Op
->isLeaf() && Op
->getOperator()->getName() == "imm") {
194 if (!Op
->getPredicateFns().empty()) {
195 TreePredicateFn PredFn
= Op
->getPredicateFns()[0];
196 // If there is more than one predicate weighing in on this operand
197 // then we don't handle it. This doesn't typically happen for
198 // immediates anyway.
199 if (Op
->getPredicateFns().size() > 1 ||
200 !PredFn
.isImmediatePattern())
202 // Ignore any instruction with 'FastIselShouldIgnore', these are
203 // not needed and just bloat the fast instruction selector. For
204 // example, X86 doesn't need to generate code to match ADD16ri8 since
205 // ADD16ri will do just fine.
206 Record
*Rec
= PredFn
.getOrigPatFragRecord()->getRecord();
207 if (Rec
->getValueAsBit("FastIselShouldIgnore"))
210 PredNo
= ImmediatePredicates
.getIDFor(PredFn
)+1;
213 // Handle unmatched immediate sizes here.
214 //if (Op->getType(0) != VT)
217 Operands
.push_back(OpKind::getImm(PredNo
));
222 // For now, filter out any operand with a predicate.
223 // For now, filter out any operand with multiple values.
224 if (!Op
->getPredicateFns().empty() || Op
->getNumTypes() != 1)
228 if (Op
->getOperator()->getName() == "fpimm") {
229 Operands
.push_back(OpKind::getFP());
232 // For now, ignore other non-leaf nodes.
236 assert(Op
->hasTypeSet(0) && "Type infererence not done?");
238 // For now, all the operands must have the same type (if they aren't
239 // immediates). Note that this causes us to reject variable sized shifts
241 if (Op
->getType(0) != VT
)
244 DefInit
*OpDI
= dynamic_cast<DefInit
*>(Op
->getLeafValue());
247 Record
*OpLeafRec
= OpDI
->getDef();
249 // For now, the only other thing we accept is register operands.
250 const CodeGenRegisterClass
*RC
= 0;
251 if (OpLeafRec
->isSubClassOf("RegisterOperand"))
252 OpLeafRec
= OpLeafRec
->getValueAsDef("RegClass");
253 if (OpLeafRec
->isSubClassOf("RegisterClass"))
254 RC
= &Target
.getRegisterClass(OpLeafRec
);
255 else if (OpLeafRec
->isSubClassOf("Register"))
256 RC
= Target
.getRegBank().getRegClassForRegister(OpLeafRec
);
260 // For now, this needs to be a register class of some sort.
264 // For now, all the operands must have the same register class or be
265 // a strict subclass of the destination.
267 if (DstRC
!= RC
&& !DstRC
->hasSubClass(RC
))
271 Operands
.push_back(OpKind::getReg());
276 void PrintParameters(raw_ostream
&OS
) const {
277 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
) {
278 if (Operands
[i
].isReg()) {
279 OS
<< "unsigned Op" << i
<< ", bool Op" << i
<< "IsKill";
280 } else if (Operands
[i
].isImm()) {
281 OS
<< "uint64_t imm" << i
;
282 } else if (Operands
[i
].isFP()) {
283 OS
<< "ConstantFP *f" << i
;
285 llvm_unreachable("Unknown operand kind!");
292 void PrintArguments(raw_ostream
&OS
,
293 const std::vector
<std::string
> &PR
) const {
294 assert(PR
.size() == Operands
.size());
295 bool PrintedArg
= false;
296 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
) {
298 // Implicit physical register operand.
303 if (Operands
[i
].isReg()) {
304 OS
<< "Op" << i
<< ", Op" << i
<< "IsKill";
306 } else if (Operands
[i
].isImm()) {
309 } else if (Operands
[i
].isFP()) {
313 llvm_unreachable("Unknown operand kind!");
318 void PrintArguments(raw_ostream
&OS
) const {
319 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
) {
320 if (Operands
[i
].isReg()) {
321 OS
<< "Op" << i
<< ", Op" << i
<< "IsKill";
322 } else if (Operands
[i
].isImm()) {
324 } else if (Operands
[i
].isFP()) {
327 llvm_unreachable("Unknown operand kind!");
335 void PrintManglingSuffix(raw_ostream
&OS
, const std::vector
<std::string
> &PR
,
336 ImmPredicateSet
&ImmPredicates
,
337 bool StripImmCodes
= false) const {
338 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
) {
340 // Implicit physical register operand. e.g. Instruction::Mul expect to
341 // select to a binary op. On x86, mul may take a single operand with
342 // the other operand being implicit. We must emit something that looks
343 // like a binary instruction except for the very inner FastEmitInst_*
346 Operands
[i
].printManglingSuffix(OS
, ImmPredicates
, StripImmCodes
);
350 void PrintManglingSuffix(raw_ostream
&OS
, ImmPredicateSet
&ImmPredicates
,
351 bool StripImmCodes
= false) const {
352 for (unsigned i
= 0, e
= Operands
.size(); i
!= e
; ++i
)
353 Operands
[i
].printManglingSuffix(OS
, ImmPredicates
, StripImmCodes
);
358 typedef std::map
<std::string
, InstructionMemo
> PredMap
;
359 typedef std::map
<MVT::SimpleValueType
, PredMap
> RetPredMap
;
360 typedef std::map
<MVT::SimpleValueType
, RetPredMap
> TypeRetPredMap
;
361 typedef std::map
<std::string
, TypeRetPredMap
> OpcodeTypeRetPredMap
;
362 typedef std::map
<OperandsSignature
, OpcodeTypeRetPredMap
>
363 OperandsOpcodeTypeRetPredMap
;
365 OperandsOpcodeTypeRetPredMap SimplePatterns
;
367 std::map
<OperandsSignature
, std::vector
<OperandsSignature
> >
368 SignaturesWithConstantForms
;
371 ImmPredicateSet ImmediatePredicates
;
373 explicit FastISelMap(std::string InstNS
);
375 void collectPatterns(CodeGenDAGPatterns
&CGP
);
376 void printImmediatePredicates(raw_ostream
&OS
);
377 void printFunctionDefinitions(raw_ostream
&OS
);
382 static std::string
getOpcodeName(Record
*Op
, CodeGenDAGPatterns
&CGP
) {
383 return CGP
.getSDNodeInfo(Op
).getEnumName();
386 static std::string
getLegalCName(std::string OpName
) {
387 std::string::size_type pos
= OpName
.find("::");
388 if (pos
!= std::string::npos
)
389 OpName
.replace(pos
, 2, "_");
393 FastISelMap::FastISelMap(std::string instns
)
397 static std::string
PhyRegForNode(TreePatternNode
*Op
,
398 const CodeGenTarget
&Target
) {
404 DefInit
*OpDI
= dynamic_cast<DefInit
*>(Op
->getLeafValue());
405 Record
*OpLeafRec
= OpDI
->getDef();
406 if (!OpLeafRec
->isSubClassOf("Register"))
409 PhysReg
+= static_cast<StringInit
*>(OpLeafRec
->getValue( \
410 "Namespace")->getValue())->getValue();
412 PhysReg
+= Target
.getRegBank().getReg(OpLeafRec
)->getName();
416 void FastISelMap::collectPatterns(CodeGenDAGPatterns
&CGP
) {
417 const CodeGenTarget
&Target
= CGP
.getTargetInfo();
419 // Determine the target's namespace name.
420 InstNS
= Target
.getInstNamespace() + "::";
421 assert(InstNS
.size() > 2 && "Can't determine target-specific namespace!");
423 // Scan through all the patterns and record the simple ones.
424 for (CodeGenDAGPatterns::ptm_iterator I
= CGP
.ptm_begin(),
425 E
= CGP
.ptm_end(); I
!= E
; ++I
) {
426 const PatternToMatch
&Pattern
= *I
;
428 // For now, just look at Instructions, so that we don't have to worry
429 // about emitting multiple instructions for a pattern.
430 TreePatternNode
*Dst
= Pattern
.getDstPattern();
431 if (Dst
->isLeaf()) continue;
432 Record
*Op
= Dst
->getOperator();
433 if (!Op
->isSubClassOf("Instruction"))
435 CodeGenInstruction
&II
= CGP
.getTargetInfo().getInstruction(Op
);
436 if (II
.Operands
.empty())
439 // For now, ignore multi-instruction patterns.
440 bool MultiInsts
= false;
441 for (unsigned i
= 0, e
= Dst
->getNumChildren(); i
!= e
; ++i
) {
442 TreePatternNode
*ChildOp
= Dst
->getChild(i
);
443 if (ChildOp
->isLeaf())
445 if (ChildOp
->getOperator()->isSubClassOf("Instruction")) {
453 // For now, ignore instructions where the first operand is not an
455 const CodeGenRegisterClass
*DstRC
= 0;
456 std::string SubRegNo
;
457 if (Op
->getName() != "EXTRACT_SUBREG") {
458 Record
*Op0Rec
= II
.Operands
[0].Rec
;
459 if (Op0Rec
->isSubClassOf("RegisterOperand"))
460 Op0Rec
= Op0Rec
->getValueAsDef("RegClass");
461 if (!Op0Rec
->isSubClassOf("RegisterClass"))
463 DstRC
= &Target
.getRegisterClass(Op0Rec
);
467 // If this isn't a leaf, then continue since the register classes are
468 // a bit too complicated for now.
469 if (!Dst
->getChild(1)->isLeaf()) continue;
471 DefInit
*SR
= dynamic_cast<DefInit
*>(Dst
->getChild(1)->getLeafValue());
473 SubRegNo
= getQualifiedName(SR
->getDef());
475 SubRegNo
= Dst
->getChild(1)->getLeafValue()->getAsString();
478 // Inspect the pattern.
479 TreePatternNode
*InstPatNode
= Pattern
.getSrcPattern();
480 if (!InstPatNode
) continue;
481 if (InstPatNode
->isLeaf()) continue;
483 // Ignore multiple result nodes for now.
484 if (InstPatNode
->getNumTypes() > 1) continue;
486 Record
*InstPatOp
= InstPatNode
->getOperator();
487 std::string OpcodeName
= getOpcodeName(InstPatOp
, CGP
);
488 MVT::SimpleValueType RetVT
= MVT::isVoid
;
489 if (InstPatNode
->getNumTypes()) RetVT
= InstPatNode
->getType(0);
490 MVT::SimpleValueType VT
= RetVT
;
491 if (InstPatNode
->getNumChildren()) {
492 assert(InstPatNode
->getChild(0)->getNumTypes() == 1);
493 VT
= InstPatNode
->getChild(0)->getType(0);
496 // For now, filter out any instructions with predicates.
497 if (!InstPatNode
->getPredicateFns().empty())
500 // Check all the operands.
501 OperandsSignature Operands
;
502 if (!Operands
.initialize(InstPatNode
, Target
, VT
, ImmediatePredicates
))
505 std::vector
<std::string
>* PhysRegInputs
= new std::vector
<std::string
>();
506 if (InstPatNode
->getOperator()->getName() == "imm" ||
507 InstPatNode
->getOperator()->getName() == "fpimmm")
508 PhysRegInputs
->push_back("");
510 // Compute the PhysRegs used by the given pattern, and check that
511 // the mapping from the src to dst patterns is simple.
512 bool FoundNonSimplePattern
= false;
513 unsigned DstIndex
= 0;
514 for (unsigned i
= 0, e
= InstPatNode
->getNumChildren(); i
!= e
; ++i
) {
515 std::string PhysReg
= PhyRegForNode(InstPatNode
->getChild(i
), Target
);
516 if (PhysReg
.empty()) {
517 if (DstIndex
>= Dst
->getNumChildren() ||
518 Dst
->getChild(DstIndex
)->getName() !=
519 InstPatNode
->getChild(i
)->getName()) {
520 FoundNonSimplePattern
= true;
526 PhysRegInputs
->push_back(PhysReg
);
529 if (Op
->getName() != "EXTRACT_SUBREG" && DstIndex
< Dst
->getNumChildren())
530 FoundNonSimplePattern
= true;
532 if (FoundNonSimplePattern
)
536 // Get the predicate that guards this pattern.
537 std::string PredicateCheck
= Pattern
.getPredicateCheck();
539 // Ok, we found a pattern that we can handle. Remember it.
540 InstructionMemo Memo
= {
541 Pattern
.getDstPattern()->getOperator()->getName(),
547 if (SimplePatterns
[Operands
][OpcodeName
][VT
][RetVT
].count(PredicateCheck
))
548 throw TGError(Pattern
.getSrcRecord()->getLoc(),
549 "Duplicate record in FastISel table!");
551 SimplePatterns
[Operands
][OpcodeName
][VT
][RetVT
][PredicateCheck
] = Memo
;
553 // If any of the operands were immediates with predicates on them, strip
554 // them down to a signature that doesn't have predicates so that we can
555 // associate them with the stripped predicate version.
556 if (Operands
.hasAnyImmediateCodes()) {
557 SignaturesWithConstantForms
[Operands
.getWithoutImmCodes()]
558 .push_back(Operands
);
563 void FastISelMap::printImmediatePredicates(raw_ostream
&OS
) {
564 if (ImmediatePredicates
.begin() == ImmediatePredicates
.end())
567 OS
<< "\n// FastEmit Immediate Predicate functions.\n";
568 for (ImmPredicateSet::iterator I
= ImmediatePredicates
.begin(),
569 E
= ImmediatePredicates
.end(); I
!= E
; ++I
) {
570 OS
<< "static bool " << I
->getFnName() << "(int64_t Imm) {\n";
571 OS
<< I
->getImmediatePredicateCode() << "\n}\n";
578 void FastISelMap::printFunctionDefinitions(raw_ostream
&OS
) {
579 // Now emit code for all the patterns that we collected.
580 for (OperandsOpcodeTypeRetPredMap::const_iterator OI
= SimplePatterns
.begin(),
581 OE
= SimplePatterns
.end(); OI
!= OE
; ++OI
) {
582 const OperandsSignature
&Operands
= OI
->first
;
583 const OpcodeTypeRetPredMap
&OTM
= OI
->second
;
585 for (OpcodeTypeRetPredMap::const_iterator I
= OTM
.begin(), E
= OTM
.end();
587 const std::string
&Opcode
= I
->first
;
588 const TypeRetPredMap
&TM
= I
->second
;
590 OS
<< "// FastEmit functions for " << Opcode
<< ".\n";
593 // Emit one function for each opcode,type pair.
594 for (TypeRetPredMap::const_iterator TI
= TM
.begin(), TE
= TM
.end();
596 MVT::SimpleValueType VT
= TI
->first
;
597 const RetPredMap
&RM
= TI
->second
;
598 if (RM
.size() != 1) {
599 for (RetPredMap::const_iterator RI
= RM
.begin(), RE
= RM
.end();
601 MVT::SimpleValueType RetVT
= RI
->first
;
602 const PredMap
&PM
= RI
->second
;
603 bool HasPred
= false;
605 OS
<< "unsigned FastEmit_"
606 << getLegalCName(Opcode
)
607 << "_" << getLegalCName(getName(VT
))
608 << "_" << getLegalCName(getName(RetVT
)) << "_";
609 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
611 Operands
.PrintParameters(OS
);
614 // Emit code for each possible instruction. There may be
615 // multiple if there are subtarget concerns.
616 for (PredMap::const_iterator PI
= PM
.begin(), PE
= PM
.end();
618 std::string PredicateCheck
= PI
->first
;
619 const InstructionMemo
&Memo
= PI
->second
;
621 if (PredicateCheck
.empty()) {
623 "Multiple instructions match, at least one has "
624 "a predicate and at least one doesn't!");
626 OS
<< " if (" + PredicateCheck
+ ") {\n";
631 for (unsigned i
= 0; i
< Memo
.PhysRegs
->size(); ++i
) {
632 if ((*Memo
.PhysRegs
)[i
] != "")
633 OS
<< " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
634 << "TII.get(TargetOpcode::COPY), "
635 << (*Memo
.PhysRegs
)[i
] << ").addReg(Op" << i
<< ");\n";
638 OS
<< " return FastEmitInst_";
639 if (Memo
.SubRegNo
.empty()) {
640 Operands
.PrintManglingSuffix(OS
, *Memo
.PhysRegs
,
641 ImmediatePredicates
, true);
642 OS
<< "(" << InstNS
<< Memo
.Name
<< ", ";
643 OS
<< InstNS
<< Memo
.RC
->getName() << "RegisterClass";
644 if (!Operands
.empty())
646 Operands
.PrintArguments(OS
, *Memo
.PhysRegs
);
649 OS
<< "extractsubreg(" << getName(RetVT
);
650 OS
<< ", Op0, Op0IsKill, " << Memo
.SubRegNo
<< ");\n";
657 // Return 0 if none of the predicates were satisfied.
659 OS
<< " return 0;\n";
664 // Emit one function for the type that demultiplexes on return type.
665 OS
<< "unsigned FastEmit_"
666 << getLegalCName(Opcode
) << "_"
667 << getLegalCName(getName(VT
)) << "_";
668 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
670 if (!Operands
.empty())
672 Operands
.PrintParameters(OS
);
673 OS
<< ") {\nswitch (RetVT.SimpleTy) {\n";
674 for (RetPredMap::const_iterator RI
= RM
.begin(), RE
= RM
.end();
676 MVT::SimpleValueType RetVT
= RI
->first
;
677 OS
<< " case " << getName(RetVT
) << ": return FastEmit_"
678 << getLegalCName(Opcode
) << "_" << getLegalCName(getName(VT
))
679 << "_" << getLegalCName(getName(RetVT
)) << "_";
680 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
682 Operands
.PrintArguments(OS
);
685 OS
<< " default: return 0;\n}\n}\n\n";
688 // Non-variadic return type.
689 OS
<< "unsigned FastEmit_"
690 << getLegalCName(Opcode
) << "_"
691 << getLegalCName(getName(VT
)) << "_";
692 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
694 if (!Operands
.empty())
696 Operands
.PrintParameters(OS
);
699 OS
<< " if (RetVT.SimpleTy != " << getName(RM
.begin()->first
)
700 << ")\n return 0;\n";
702 const PredMap
&PM
= RM
.begin()->second
;
703 bool HasPred
= false;
705 // Emit code for each possible instruction. There may be
706 // multiple if there are subtarget concerns.
707 for (PredMap::const_iterator PI
= PM
.begin(), PE
= PM
.end(); PI
!= PE
;
709 std::string PredicateCheck
= PI
->first
;
710 const InstructionMemo
&Memo
= PI
->second
;
712 if (PredicateCheck
.empty()) {
714 "Multiple instructions match, at least one has "
715 "a predicate and at least one doesn't!");
717 OS
<< " if (" + PredicateCheck
+ ") {\n";
722 for (unsigned i
= 0; i
< Memo
.PhysRegs
->size(); ++i
) {
723 if ((*Memo
.PhysRegs
)[i
] != "")
724 OS
<< " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, "
725 << "TII.get(TargetOpcode::COPY), "
726 << (*Memo
.PhysRegs
)[i
] << ").addReg(Op" << i
<< ");\n";
729 OS
<< " return FastEmitInst_";
731 if (Memo
.SubRegNo
.empty()) {
732 Operands
.PrintManglingSuffix(OS
, *Memo
.PhysRegs
,
733 ImmediatePredicates
, true);
734 OS
<< "(" << InstNS
<< Memo
.Name
<< ", ";
735 OS
<< InstNS
<< Memo
.RC
->getName() << "RegisterClass";
736 if (!Operands
.empty())
738 Operands
.PrintArguments(OS
, *Memo
.PhysRegs
);
741 OS
<< "extractsubreg(RetVT, Op0, Op0IsKill, ";
750 // Return 0 if none of the predicates were satisfied.
752 OS
<< " return 0;\n";
758 // Emit one function for the opcode that demultiplexes based on the type.
759 OS
<< "unsigned FastEmit_"
760 << getLegalCName(Opcode
) << "_";
761 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
762 OS
<< "(MVT VT, MVT RetVT";
763 if (!Operands
.empty())
765 Operands
.PrintParameters(OS
);
767 OS
<< " switch (VT.SimpleTy) {\n";
768 for (TypeRetPredMap::const_iterator TI
= TM
.begin(), TE
= TM
.end();
770 MVT::SimpleValueType VT
= TI
->first
;
771 std::string TypeName
= getName(VT
);
772 OS
<< " case " << TypeName
<< ": return FastEmit_"
773 << getLegalCName(Opcode
) << "_" << getLegalCName(TypeName
) << "_";
774 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
776 if (!Operands
.empty())
778 Operands
.PrintArguments(OS
);
781 OS
<< " default: return 0;\n";
787 OS
<< "// Top-level FastEmit function.\n";
790 // Emit one function for the operand signature that demultiplexes based
791 // on opcode and type.
792 OS
<< "unsigned FastEmit_";
793 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
794 OS
<< "(MVT VT, MVT RetVT, unsigned Opcode";
795 if (!Operands
.empty())
797 Operands
.PrintParameters(OS
);
800 // If there are any forms of this signature available that operand on
801 // constrained forms of the immediate (e.g. 32-bit sext immediate in a
802 // 64-bit operand), check them first.
804 std::map
<OperandsSignature
, std::vector
<OperandsSignature
> >::iterator MI
805 = SignaturesWithConstantForms
.find(Operands
);
806 if (MI
!= SignaturesWithConstantForms
.end()) {
807 // Unique any duplicates out of the list.
808 std::sort(MI
->second
.begin(), MI
->second
.end());
809 MI
->second
.erase(std::unique(MI
->second
.begin(), MI
->second
.end()),
812 // Check each in order it was seen. It would be nice to have a good
813 // relative ordering between them, but we're not going for optimality
815 for (unsigned i
= 0, e
= MI
->second
.size(); i
!= e
; ++i
) {
817 MI
->second
[i
].emitImmediatePredicate(OS
, ImmediatePredicates
);
818 OS
<< ")\n if (unsigned Reg = FastEmit_";
819 MI
->second
[i
].PrintManglingSuffix(OS
, ImmediatePredicates
);
820 OS
<< "(VT, RetVT, Opcode";
821 if (!MI
->second
[i
].empty())
823 MI
->second
[i
].PrintArguments(OS
);
824 OS
<< "))\n return Reg;\n\n";
827 // Done with this, remove it.
828 SignaturesWithConstantForms
.erase(MI
);
831 OS
<< " switch (Opcode) {\n";
832 for (OpcodeTypeRetPredMap::const_iterator I
= OTM
.begin(), E
= OTM
.end();
834 const std::string
&Opcode
= I
->first
;
836 OS
<< " case " << Opcode
<< ": return FastEmit_"
837 << getLegalCName(Opcode
) << "_";
838 Operands
.PrintManglingSuffix(OS
, ImmediatePredicates
);
840 if (!Operands
.empty())
842 Operands
.PrintArguments(OS
);
845 OS
<< " default: return 0;\n";
851 // TODO: SignaturesWithConstantForms should be empty here.
854 void FastISelEmitter::run(raw_ostream
&OS
) {
855 const CodeGenTarget
&Target
= CGP
.getTargetInfo();
857 // Determine the target's namespace name.
858 std::string InstNS
= Target
.getInstNamespace() + "::";
859 assert(InstNS
.size() > 2 && "Can't determine target-specific namespace!");
861 EmitSourceFileHeader("\"Fast\" Instruction Selector for the " +
862 Target
.getName() + " target", OS
);
864 FastISelMap
F(InstNS
);
865 F
.collectPatterns(CGP
);
866 F
.printImmediatePredicates(OS
);
867 F
.printFunctionDefinitions(OS
);
870 FastISelEmitter::FastISelEmitter(RecordKeeper
&R
)
871 : Records(R
), CGP(R
) {