1 //===---- ScheduleDAGEmit.cpp - Emit routines for the ScheduleDAG class ---===//
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 implements the Emit routines for the ScheduleDAG class, which creates
11 // MachineInstrs according to the computed schedule.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "pre-RA-sched"
16 #include "ScheduleDAGSDNodes.h"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetInstrInfo.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MathExtras.h"
31 /// getInstrOperandRegClass - Return register class of the operand of an
32 /// instruction of the specified TargetInstrDesc.
33 static const TargetRegisterClass
*
34 getInstrOperandRegClass(const TargetRegisterInfo
*TRI
,
35 const TargetInstrDesc
&II
, unsigned Op
) {
36 if (Op
>= II
.getNumOperands()) {
37 assert(II
.isVariadic() && "Invalid operand # of instruction");
40 if (II
.OpInfo
[Op
].isLookupPtrRegClass())
41 return TRI
->getPointerRegClass();
42 return TRI
->getRegClass(II
.OpInfo
[Op
].RegClass
);
45 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
46 /// implicit physical register output.
47 void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode
*Node
, unsigned ResNo
,
48 bool IsClone
, bool IsCloned
,
50 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
52 if (TargetRegisterInfo::isVirtualRegister(SrcReg
)) {
53 // Just use the input register directly!
54 SDValue
Op(Node
, ResNo
);
57 bool isNew
= VRBaseMap
.insert(std::make_pair(Op
, SrcReg
)).second
;
58 isNew
= isNew
; // Silence compiler warning.
59 assert(isNew
&& "Node emitted out of order - early");
63 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
64 // the CopyToReg'd destination register instead of creating a new vreg.
66 const TargetRegisterClass
*UseRC
= NULL
;
67 if (!IsClone
&& !IsCloned
)
68 for (SDNode::use_iterator UI
= Node
->use_begin(), E
= Node
->use_end();
72 if (User
->getOpcode() == ISD::CopyToReg
&&
73 User
->getOperand(2).getNode() == Node
&&
74 User
->getOperand(2).getResNo() == ResNo
) {
75 unsigned DestReg
= cast
<RegisterSDNode
>(User
->getOperand(1))->getReg();
76 if (TargetRegisterInfo::isVirtualRegister(DestReg
)) {
79 } else if (DestReg
!= SrcReg
)
82 for (unsigned i
= 0, e
= User
->getNumOperands(); i
!= e
; ++i
) {
83 SDValue Op
= User
->getOperand(i
);
84 if (Op
.getNode() != Node
|| Op
.getResNo() != ResNo
)
86 MVT VT
= Node
->getValueType(Op
.getResNo());
87 if (VT
== MVT::Other
|| VT
== MVT::Flag
)
90 if (User
->isMachineOpcode()) {
91 const TargetInstrDesc
&II
= TII
->get(User
->getMachineOpcode());
92 const TargetRegisterClass
*RC
=
93 getInstrOperandRegClass(TRI
, II
, i
+II
.getNumDefs());
97 if (UseRC
->hasSuperClass(RC
))
100 assert((UseRC
== RC
|| RC
->hasSuperClass(UseRC
)) &&
101 "Multiple uses expecting different register classes!");
111 MVT VT
= Node
->getValueType(ResNo
);
112 const TargetRegisterClass
*SrcRC
= 0, *DstRC
= 0;
113 SrcRC
= TRI
->getPhysicalRegisterRegClass(SrcReg
, VT
);
115 // Figure out the register class to create for the destreg.
117 DstRC
= MRI
.getRegClass(VRBase
);
119 assert(UseRC
->hasType(VT
) && "Incompatible phys register def and uses!");
122 DstRC
= TLI
->getRegClassFor(VT
);
125 // If all uses are reading from the src physical register and copying the
126 // register is either impossible or very expensive, then don't create a copy.
127 if (MatchReg
&& SrcRC
->getCopyCost() < 0) {
130 // Create the reg, emit the copy.
131 VRBase
= MRI
.createVirtualRegister(DstRC
);
132 bool Emitted
= TII
->copyRegToReg(*BB
, InsertPos
, VRBase
, SrcReg
,
135 assert(Emitted
&& "Unable to issue a copy instruction!\n");
138 SDValue
Op(Node
, ResNo
);
141 bool isNew
= VRBaseMap
.insert(std::make_pair(Op
, VRBase
)).second
;
142 isNew
= isNew
; // Silence compiler warning.
143 assert(isNew
&& "Node emitted out of order - early");
146 /// getDstOfCopyToRegUse - If the only use of the specified result number of
147 /// node is a CopyToReg, return its destination register. Return 0 otherwise.
148 unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode
*Node
,
149 unsigned ResNo
) const {
150 if (!Node
->hasOneUse())
153 SDNode
*User
= *Node
->use_begin();
154 if (User
->getOpcode() == ISD::CopyToReg
&&
155 User
->getOperand(2).getNode() == Node
&&
156 User
->getOperand(2).getResNo() == ResNo
) {
157 unsigned Reg
= cast
<RegisterSDNode
>(User
->getOperand(1))->getReg();
158 if (TargetRegisterInfo::isVirtualRegister(Reg
))
164 void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode
*Node
, MachineInstr
*MI
,
165 const TargetInstrDesc
&II
,
166 bool IsClone
, bool IsCloned
,
167 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
168 assert(Node
->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF
&&
169 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
171 for (unsigned i
= 0; i
< II
.getNumDefs(); ++i
) {
172 // If the specific node value is only used by a CopyToReg and the dest reg
173 // is a vreg in the same register class, use the CopyToReg'd destination
174 // register instead of creating a new vreg.
176 const TargetRegisterClass
*RC
= getInstrOperandRegClass(TRI
, II
, i
);
178 if (!IsClone
&& !IsCloned
)
179 for (SDNode::use_iterator UI
= Node
->use_begin(), E
= Node
->use_end();
182 if (User
->getOpcode() == ISD::CopyToReg
&&
183 User
->getOperand(2).getNode() == Node
&&
184 User
->getOperand(2).getResNo() == i
) {
185 unsigned Reg
= cast
<RegisterSDNode
>(User
->getOperand(1))->getReg();
186 if (TargetRegisterInfo::isVirtualRegister(Reg
)) {
187 const TargetRegisterClass
*RegRC
= MRI
.getRegClass(Reg
);
190 MI
->addOperand(MachineOperand::CreateReg(Reg
, true));
197 // Create the result registers for this node and add the result regs to
198 // the machine instruction.
200 assert(RC
&& "Isn't a register operand!");
201 VRBase
= MRI
.createVirtualRegister(RC
);
202 MI
->addOperand(MachineOperand::CreateReg(VRBase
, true));
208 bool isNew
= VRBaseMap
.insert(std::make_pair(Op
, VRBase
)).second
;
209 isNew
= isNew
; // Silence compiler warning.
210 assert(isNew
&& "Node emitted out of order - early");
214 /// getVR - Return the virtual register corresponding to the specified result
215 /// of the specified node.
216 unsigned ScheduleDAGSDNodes::getVR(SDValue Op
,
217 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
218 if (Op
.isMachineOpcode() &&
219 Op
.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF
) {
220 // Add an IMPLICIT_DEF instruction before every use.
221 unsigned VReg
= getDstOfOnlyCopyToRegUse(Op
.getNode(), Op
.getResNo());
222 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
223 // does not include operand register class info.
225 const TargetRegisterClass
*RC
= TLI
->getRegClassFor(Op
.getValueType());
226 VReg
= MRI
.createVirtualRegister(RC
);
228 BuildMI(BB
, Op
.getDebugLoc(), TII
->get(TargetInstrInfo::IMPLICIT_DEF
),VReg
);
232 DenseMap
<SDValue
, unsigned>::iterator I
= VRBaseMap
.find(Op
);
233 assert(I
!= VRBaseMap
.end() && "Node emitted out of order - late");
238 /// AddRegisterOperand - Add the specified register as an operand to the
239 /// specified machine instr. Insert register copies if the register is
240 /// not in the required register class.
242 ScheduleDAGSDNodes::AddRegisterOperand(MachineInstr
*MI
, SDValue Op
,
244 const TargetInstrDesc
*II
,
245 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
246 assert(Op
.getValueType() != MVT::Other
&&
247 Op
.getValueType() != MVT::Flag
&&
248 "Chain and flag operands should occur at end of operand list!");
249 // Get/emit the operand.
250 unsigned VReg
= getVR(Op
, VRBaseMap
);
251 assert(TargetRegisterInfo::isVirtualRegister(VReg
) && "Not a vreg?");
253 const TargetInstrDesc
&TID
= MI
->getDesc();
254 bool isOptDef
= IIOpNum
< TID
.getNumOperands() &&
255 TID
.OpInfo
[IIOpNum
].isOptionalDef();
257 // If the instruction requires a register in a different class, create
258 // a new virtual register and copy the value into it.
260 const TargetRegisterClass
*SrcRC
=
261 MRI
.getRegClass(VReg
);
262 const TargetRegisterClass
*DstRC
=
263 getInstrOperandRegClass(TRI
, *II
, IIOpNum
);
264 assert((DstRC
|| (TID
.isVariadic() && IIOpNum
>= TID
.getNumOperands())) &&
265 "Don't have operand info for this instruction!");
266 if (DstRC
&& SrcRC
!= DstRC
&& !SrcRC
->hasSuperClass(DstRC
)) {
267 unsigned NewVReg
= MRI
.createVirtualRegister(DstRC
);
268 bool Emitted
= TII
->copyRegToReg(*BB
, InsertPos
, NewVReg
, VReg
,
270 assert(Emitted
&& "Unable to issue a copy instruction!\n");
275 MI
->addOperand(MachineOperand::CreateReg(VReg
, isOptDef
));
278 /// AddOperand - Add the specified operand to the specified machine instr. II
279 /// specifies the instruction information for the node, and IIOpNum is the
280 /// operand number (in the II) that we are adding. IIOpNum and II are used for
282 void ScheduleDAGSDNodes::AddOperand(MachineInstr
*MI
, SDValue Op
,
284 const TargetInstrDesc
*II
,
285 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
286 if (Op
.isMachineOpcode()) {
287 AddRegisterOperand(MI
, Op
, IIOpNum
, II
, VRBaseMap
);
288 } else if (ConstantSDNode
*C
= dyn_cast
<ConstantSDNode
>(Op
)) {
289 MI
->addOperand(MachineOperand::CreateImm(C
->getZExtValue()));
290 } else if (ConstantFPSDNode
*F
= dyn_cast
<ConstantFPSDNode
>(Op
)) {
291 const ConstantFP
*CFP
= F
->getConstantFPValue();
292 MI
->addOperand(MachineOperand::CreateFPImm(CFP
));
293 } else if (RegisterSDNode
*R
= dyn_cast
<RegisterSDNode
>(Op
)) {
294 MI
->addOperand(MachineOperand::CreateReg(R
->getReg(), false));
295 } else if (GlobalAddressSDNode
*TGA
= dyn_cast
<GlobalAddressSDNode
>(Op
)) {
296 MI
->addOperand(MachineOperand::CreateGA(TGA
->getGlobal(),TGA
->getOffset()));
297 } else if (BasicBlockSDNode
*BBNode
= dyn_cast
<BasicBlockSDNode
>(Op
)) {
298 MI
->addOperand(MachineOperand::CreateMBB(BBNode
->getBasicBlock()));
299 } else if (FrameIndexSDNode
*FI
= dyn_cast
<FrameIndexSDNode
>(Op
)) {
300 MI
->addOperand(MachineOperand::CreateFI(FI
->getIndex()));
301 } else if (JumpTableSDNode
*JT
= dyn_cast
<JumpTableSDNode
>(Op
)) {
302 MI
->addOperand(MachineOperand::CreateJTI(JT
->getIndex()));
303 } else if (ConstantPoolSDNode
*CP
= dyn_cast
<ConstantPoolSDNode
>(Op
)) {
304 int Offset
= CP
->getOffset();
305 unsigned Align
= CP
->getAlignment();
306 const Type
*Type
= CP
->getType();
307 // MachineConstantPool wants an explicit alignment.
309 Align
= TM
.getTargetData()->getPrefTypeAlignment(Type
);
311 // Alignment of vector types. FIXME!
312 Align
= TM
.getTargetData()->getTypePaddedSize(Type
);
317 if (CP
->isMachineConstantPoolEntry())
318 Idx
= ConstPool
->getConstantPoolIndex(CP
->getMachineCPVal(), Align
);
320 Idx
= ConstPool
->getConstantPoolIndex(CP
->getConstVal(), Align
);
321 MI
->addOperand(MachineOperand::CreateCPI(Idx
, Offset
));
322 } else if (ExternalSymbolSDNode
*ES
= dyn_cast
<ExternalSymbolSDNode
>(Op
)) {
323 MI
->addOperand(MachineOperand::CreateES(ES
->getSymbol()));
325 assert(Op
.getValueType() != MVT::Other
&&
326 Op
.getValueType() != MVT::Flag
&&
327 "Chain and flag operands should occur at end of operand list!");
328 AddRegisterOperand(MI
, Op
, IIOpNum
, II
, VRBaseMap
);
332 /// getSubRegisterRegClass - Returns the register class of specified register
333 /// class' "SubIdx"'th sub-register class.
334 static const TargetRegisterClass
*
335 getSubRegisterRegClass(const TargetRegisterClass
*TRC
, unsigned SubIdx
) {
336 // Pick the register class of the subregister
337 TargetRegisterInfo::regclass_iterator I
=
338 TRC
->subregclasses_begin() + SubIdx
-1;
339 assert(I
< TRC
->subregclasses_end() &&
340 "Invalid subregister index for register class");
344 /// getSuperRegisterRegClass - Returns the register class of a superreg A whose
345 /// "SubIdx"'th sub-register class is the specified register class and whose
346 /// type matches the specified type.
347 static const TargetRegisterClass
*
348 getSuperRegisterRegClass(const TargetRegisterClass
*TRC
,
349 unsigned SubIdx
, MVT VT
) {
350 // Pick the register class of the superegister for this type
351 for (TargetRegisterInfo::regclass_iterator I
= TRC
->superregclasses_begin(),
352 E
= TRC
->superregclasses_end(); I
!= E
; ++I
)
353 if ((*I
)->hasType(VT
) && getSubRegisterRegClass(*I
, SubIdx
) == TRC
)
355 assert(false && "Couldn't find the register class");
359 /// EmitSubregNode - Generate machine code for subreg nodes.
361 void ScheduleDAGSDNodes::EmitSubregNode(SDNode
*Node
,
362 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
364 unsigned Opc
= Node
->getMachineOpcode();
366 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
367 // the CopyToReg'd destination register instead of creating a new vreg.
368 for (SDNode::use_iterator UI
= Node
->use_begin(), E
= Node
->use_end();
371 if (User
->getOpcode() == ISD::CopyToReg
&&
372 User
->getOperand(2).getNode() == Node
) {
373 unsigned DestReg
= cast
<RegisterSDNode
>(User
->getOperand(1))->getReg();
374 if (TargetRegisterInfo::isVirtualRegister(DestReg
)) {
381 if (Opc
== TargetInstrInfo::EXTRACT_SUBREG
) {
382 unsigned SubIdx
= cast
<ConstantSDNode
>(Node
->getOperand(1))->getZExtValue();
384 // Create the extract_subreg machine instruction.
385 MachineInstr
*MI
= BuildMI(MF
, Node
->getDebugLoc(),
386 TII
->get(TargetInstrInfo::EXTRACT_SUBREG
));
388 // Figure out the register class to create for the destreg.
389 unsigned VReg
= getVR(Node
->getOperand(0), VRBaseMap
);
390 const TargetRegisterClass
*TRC
= MRI
.getRegClass(VReg
);
391 const TargetRegisterClass
*SRC
= getSubRegisterRegClass(TRC
, SubIdx
);
393 // Figure out the register class to create for the destreg.
394 // Note that if we're going to directly use an existing register,
395 // it must be precisely the required class, and not a subclass
397 if (VRBase
== 0 || SRC
!= MRI
.getRegClass(VRBase
)) {
399 assert(SRC
&& "Couldn't find source register class");
400 VRBase
= MRI
.createVirtualRegister(SRC
);
403 // Add def, source, and subreg index
404 MI
->addOperand(MachineOperand::CreateReg(VRBase
, true));
405 AddOperand(MI
, Node
->getOperand(0), 0, 0, VRBaseMap
);
406 MI
->addOperand(MachineOperand::CreateImm(SubIdx
));
407 BB
->insert(InsertPos
, MI
);
408 } else if (Opc
== TargetInstrInfo::INSERT_SUBREG
||
409 Opc
== TargetInstrInfo::SUBREG_TO_REG
) {
410 SDValue N0
= Node
->getOperand(0);
411 SDValue N1
= Node
->getOperand(1);
412 SDValue N2
= Node
->getOperand(2);
413 unsigned SubReg
= getVR(N1
, VRBaseMap
);
414 unsigned SubIdx
= cast
<ConstantSDNode
>(N2
)->getZExtValue();
415 const TargetRegisterClass
*TRC
= MRI
.getRegClass(SubReg
);
416 const TargetRegisterClass
*SRC
=
417 getSuperRegisterRegClass(TRC
, SubIdx
,
418 Node
->getValueType(0));
420 // Figure out the register class to create for the destreg.
421 // Note that if we're going to directly use an existing register,
422 // it must be precisely the required class, and not a subclass
424 if (VRBase
== 0 || SRC
!= MRI
.getRegClass(VRBase
)) {
426 assert(SRC
&& "Couldn't find source register class");
427 VRBase
= MRI
.createVirtualRegister(SRC
);
430 // Create the insert_subreg or subreg_to_reg machine instruction.
431 MachineInstr
*MI
= BuildMI(MF
, Node
->getDebugLoc(), TII
->get(Opc
));
432 MI
->addOperand(MachineOperand::CreateReg(VRBase
, true));
434 // If creating a subreg_to_reg, then the first input operand
435 // is an implicit value immediate, otherwise it's a register
436 if (Opc
== TargetInstrInfo::SUBREG_TO_REG
) {
437 const ConstantSDNode
*SD
= cast
<ConstantSDNode
>(N0
);
438 MI
->addOperand(MachineOperand::CreateImm(SD
->getZExtValue()));
440 AddOperand(MI
, N0
, 0, 0, VRBaseMap
);
441 // Add the subregster being inserted
442 AddOperand(MI
, N1
, 0, 0, VRBaseMap
);
443 MI
->addOperand(MachineOperand::CreateImm(SubIdx
));
444 BB
->insert(InsertPos
, MI
);
446 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
449 bool isNew
= VRBaseMap
.insert(std::make_pair(Op
, VRBase
)).second
;
450 isNew
= isNew
; // Silence compiler warning.
451 assert(isNew
&& "Node emitted out of order - early");
454 /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
455 /// COPY_TO_REGCLASS is just a normal copy, except that the destination
456 /// register is constrained to be in a particular register class.
459 ScheduleDAGSDNodes::EmitCopyToRegClassNode(SDNode
*Node
,
460 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
461 unsigned VReg
= getVR(Node
->getOperand(0), VRBaseMap
);
462 const TargetRegisterClass
*SrcRC
= MRI
.getRegClass(VReg
);
464 unsigned DstRCIdx
= cast
<ConstantSDNode
>(Node
->getOperand(1))->getZExtValue();
465 const TargetRegisterClass
*DstRC
= TRI
->getRegClass(DstRCIdx
);
467 // Create the new VReg in the destination class and emit a copy.
468 unsigned NewVReg
= MRI
.createVirtualRegister(DstRC
);
469 bool Emitted
= TII
->copyRegToReg(*BB
, InsertPos
, NewVReg
, VReg
,
472 "Unable to issue a copy instruction for a COPY_TO_REGCLASS node!\n");
475 bool isNew
= VRBaseMap
.insert(std::make_pair(Op
, NewVReg
)).second
;
476 isNew
= isNew
; // Silence compiler warning.
477 assert(isNew
&& "Node emitted out of order - early");
480 /// EmitNode - Generate machine code for an node and needed dependencies.
482 void ScheduleDAGSDNodes::EmitNode(SDNode
*Node
, bool IsClone
, bool IsCloned
,
483 DenseMap
<SDValue
, unsigned> &VRBaseMap
) {
484 // If machine instruction
485 if (Node
->isMachineOpcode()) {
486 unsigned Opc
= Node
->getMachineOpcode();
488 // Handle subreg insert/extract specially
489 if (Opc
== TargetInstrInfo::EXTRACT_SUBREG
||
490 Opc
== TargetInstrInfo::INSERT_SUBREG
||
491 Opc
== TargetInstrInfo::SUBREG_TO_REG
) {
492 EmitSubregNode(Node
, VRBaseMap
);
496 // Handle COPY_TO_REGCLASS specially.
497 if (Opc
== TargetInstrInfo::COPY_TO_REGCLASS
) {
498 EmitCopyToRegClassNode(Node
, VRBaseMap
);
502 if (Opc
== TargetInstrInfo::IMPLICIT_DEF
)
503 // We want a unique VR for each IMPLICIT_DEF use.
506 const TargetInstrDesc
&II
= TII
->get(Opc
);
507 unsigned NumResults
= CountResults(Node
);
508 unsigned NodeOperands
= CountOperands(Node
);
509 unsigned MemOperandsEnd
= ComputeMemOperandsEnd(Node
);
510 bool HasPhysRegOuts
= (NumResults
> II
.getNumDefs()) &&
511 II
.getImplicitDefs() != 0;
513 unsigned NumMIOperands
= NodeOperands
+ NumResults
;
514 assert((II
.getNumOperands() == NumMIOperands
||
515 HasPhysRegOuts
|| II
.isVariadic()) &&
516 "#operands for dag node doesn't match .td file!");
519 // Create the new machine instruction.
520 MachineInstr
*MI
= BuildMI(MF
, Node
->getDebugLoc(), II
);
522 // Add result register values for things that are defined by this
525 CreateVirtualRegisters(Node
, MI
, II
, IsClone
, IsCloned
, VRBaseMap
);
527 // Emit all of the actual operands of this instruction, adding them to the
528 // instruction as appropriate.
529 for (unsigned i
= 0; i
!= NodeOperands
; ++i
)
530 AddOperand(MI
, Node
->getOperand(i
), i
+II
.getNumDefs(), &II
, VRBaseMap
);
532 // Emit all of the memory operands of this instruction
533 for (unsigned i
= NodeOperands
; i
!= MemOperandsEnd
; ++i
)
534 AddMemOperand(MI
, cast
<MemOperandSDNode
>(Node
->getOperand(i
))->MO
);
536 if (II
.usesCustomDAGSchedInsertionHook()) {
537 // Insert this instruction into the basic block using a target
538 // specific inserter which may returns a new basic block.
539 BB
= TLI
->EmitInstrWithCustomInserter(MI
, BB
);
540 InsertPos
= BB
->end();
542 BB
->insert(InsertPos
, MI
);
545 // Additional results must be an physical register def.
546 if (HasPhysRegOuts
) {
547 for (unsigned i
= II
.getNumDefs(); i
< NumResults
; ++i
) {
548 unsigned Reg
= II
.getImplicitDefs()[i
- II
.getNumDefs()];
549 if (Node
->hasAnyUseOfValue(i
))
550 EmitCopyFromReg(Node
, i
, IsClone
, IsCloned
, Reg
, VRBaseMap
);
556 switch (Node
->getOpcode()) {
561 assert(0 && "This target-independent node should have been selected!");
563 case ISD::EntryToken
:
564 assert(0 && "EntryToken should have been excluded from the schedule!");
566 case ISD::TokenFactor
: // fall thru
568 case ISD::CopyToReg
: {
570 SDValue SrcVal
= Node
->getOperand(2);
571 if (RegisterSDNode
*R
= dyn_cast
<RegisterSDNode
>(SrcVal
))
572 SrcReg
= R
->getReg();
574 SrcReg
= getVR(SrcVal
, VRBaseMap
);
576 unsigned DestReg
= cast
<RegisterSDNode
>(Node
->getOperand(1))->getReg();
577 if (SrcReg
== DestReg
) // Coalesced away the copy? Ignore.
580 const TargetRegisterClass
*SrcTRC
= 0, *DstTRC
= 0;
581 // Get the register classes of the src/dst.
582 if (TargetRegisterInfo::isVirtualRegister(SrcReg
))
583 SrcTRC
= MRI
.getRegClass(SrcReg
);
585 SrcTRC
= TRI
->getPhysicalRegisterRegClass(SrcReg
,SrcVal
.getValueType());
587 if (TargetRegisterInfo::isVirtualRegister(DestReg
))
588 DstTRC
= MRI
.getRegClass(DestReg
);
590 DstTRC
= TRI
->getPhysicalRegisterRegClass(DestReg
,
591 Node
->getOperand(1).getValueType());
593 bool Emitted
= TII
->copyRegToReg(*BB
, InsertPos
, DestReg
, SrcReg
,
595 assert(Emitted
&& "Unable to issue a copy instruction!\n");
598 case ISD::CopyFromReg
: {
599 unsigned SrcReg
= cast
<RegisterSDNode
>(Node
->getOperand(1))->getReg();
600 EmitCopyFromReg(Node
, 0, IsClone
, IsCloned
, SrcReg
, VRBaseMap
);
603 case ISD::INLINEASM
: {
604 unsigned NumOps
= Node
->getNumOperands();
605 if (Node
->getOperand(NumOps
-1).getValueType() == MVT::Flag
)
606 --NumOps
; // Ignore the flag operand.
608 // Create the inline asm machine instruction.
609 MachineInstr
*MI
= BuildMI(MF
, Node
->getDebugLoc(),
610 TII
->get(TargetInstrInfo::INLINEASM
));
612 // Add the asm string as an external symbol operand.
614 cast
<ExternalSymbolSDNode
>(Node
->getOperand(1))->getSymbol();
615 MI
->addOperand(MachineOperand::CreateES(AsmStr
));
617 // Add all of the operand registers to the instruction.
618 for (unsigned i
= 2; i
!= NumOps
;) {
620 cast
<ConstantSDNode
>(Node
->getOperand(i
))->getZExtValue();
621 unsigned NumVals
= InlineAsm::getNumOperandRegisters(Flags
);
623 MI
->addOperand(MachineOperand::CreateImm(Flags
));
624 ++i
; // Skip the ID value.
627 default: assert(0 && "Bad flags!");
628 case 2: // Def of register.
629 for (; NumVals
; --NumVals
, ++i
) {
630 unsigned Reg
= cast
<RegisterSDNode
>(Node
->getOperand(i
))->getReg();
631 MI
->addOperand(MachineOperand::CreateReg(Reg
, true));
634 case 6: // Def of earlyclobber register.
635 for (; NumVals
; --NumVals
, ++i
) {
636 unsigned Reg
= cast
<RegisterSDNode
>(Node
->getOperand(i
))->getReg();
637 MI
->addOperand(MachineOperand::CreateReg(Reg
, true, false, false,
641 case 1: // Use of register.
642 case 3: // Immediate.
643 case 4: // Addressing mode.
644 // The addressing mode has been selected, just add all of the
645 // operands to the machine instruction.
646 for (; NumVals
; --NumVals
, ++i
)
647 AddOperand(MI
, Node
->getOperand(i
), 0, 0, VRBaseMap
);
651 BB
->insert(InsertPos
, MI
);
657 /// EmitSchedule - Emit the machine code in scheduled order.
658 MachineBasicBlock
*ScheduleDAGSDNodes::EmitSchedule() {
659 DenseMap
<SDValue
, unsigned> VRBaseMap
;
660 DenseMap
<SUnit
*, unsigned> CopyVRBaseMap
;
661 for (unsigned i
= 0, e
= Sequence
.size(); i
!= e
; i
++) {
662 SUnit
*SU
= Sequence
[i
];
664 // Null SUnit* is a noop.
669 // For pre-regalloc scheduling, create instructions corresponding to the
670 // SDNode and any flagged SDNodes and append them to the block.
671 if (!SU
->getNode()) {
673 EmitPhysRegCopy(SU
, CopyVRBaseMap
);
677 SmallVector
<SDNode
*, 4> FlaggedNodes
;
678 for (SDNode
*N
= SU
->getNode()->getFlaggedNode(); N
;
679 N
= N
->getFlaggedNode())
680 FlaggedNodes
.push_back(N
);
681 while (!FlaggedNodes
.empty()) {
682 EmitNode(FlaggedNodes
.back(), SU
->OrigNode
!= SU
, SU
->isCloned
,VRBaseMap
);
683 FlaggedNodes
.pop_back();
685 EmitNode(SU
->getNode(), SU
->OrigNode
!= SU
, SU
->isCloned
, VRBaseMap
);