Skip to content

Commit dd5663d

Browse files
committed
Reduce the number of copies emitted as machine instructions by
generating results in vregs that will need them. In the case of something like this: CopyToReg((add X, Y), reg1024), we no longer emit code like this: reg1025 = add X, Y reg1024 = reg 1025 Instead, we emit: reg1024 = add X, Y Whoa! :) llvm-svn: 24111
1 parent 5c7d731 commit dd5663d

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

llvm/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,28 @@ void SimpleSched::EmitNode(NodeInfo *NI) {
10061006

10071007
// Add result register values for things that are defined by this
10081008
// instruction.
1009-
if (NumResults) VRBase = CreateVirtualRegisters(MI, NumResults, II);
1009+
1010+
// If the node is only used by a CopyToReg and the dest reg is a vreg, use
1011+
// the CopyToReg'd destination register instead of creating a new vreg.
1012+
if (NumResults == 1) {
1013+
for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1014+
UI != E; ++UI) {
1015+
SDNode *Use = *UI;
1016+
if (Use->getOpcode() == ISD::CopyToReg &&
1017+
Use->getOperand(2).Val == Node) {
1018+
unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
1019+
if (MRegisterInfo::isVirtualRegister(Reg)) {
1020+
VRBase = Reg;
1021+
MI->addRegOperand(Reg, MachineOperand::Def);
1022+
break;
1023+
}
1024+
}
1025+
}
1026+
}
1027+
1028+
// Otherwise, create new virtual registers.
1029+
if (NumResults && VRBase == 0)
1030+
VRBase = CreateVirtualRegisters(MI, NumResults, II);
10101031

10111032
// Emit all of the actual operands of this instruction, adding them to the
10121033
// instruction as appropriate.
@@ -1084,10 +1105,11 @@ void SimpleSched::EmitNode(NodeInfo *NI) {
10841105
case ISD::TokenFactor:
10851106
break;
10861107
case ISD::CopyToReg: {
1087-
unsigned Val = getVR(Node->getOperand(2));
1088-
MRI.copyRegToReg(*BB, BB->end(),
1089-
cast<RegisterSDNode>(Node->getOperand(1))->getReg(), Val,
1090-
RegMap->getRegClass(Val));
1108+
unsigned InReg = getVR(Node->getOperand(2));
1109+
unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1110+
if (InReg != DestReg) // Coallesced away the copy?
1111+
MRI.copyRegToReg(*BB, BB->end(), DestReg, InReg,
1112+
RegMap->getRegClass(InReg));
10911113
break;
10921114
}
10931115
case ISD::CopyFromReg: {
@@ -1097,21 +1119,40 @@ void SimpleSched::EmitNode(NodeInfo *NI) {
10971119
break;
10981120
}
10991121

1122+
// If the node is only used by a CopyToReg and the dest reg is a vreg, use
1123+
// the CopyToReg'd destination register instead of creating a new vreg.
1124+
for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1125+
UI != E; ++UI) {
1126+
SDNode *Use = *UI;
1127+
if (Use->getOpcode() == ISD::CopyToReg &&
1128+
Use->getOperand(2).Val == Node) {
1129+
unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
1130+
if (MRegisterInfo::isVirtualRegister(DestReg)) {
1131+
VRBase = DestReg;
1132+
break;
1133+
}
1134+
}
1135+
}
1136+
11001137
// Figure out the register class to create for the destreg.
11011138
const TargetRegisterClass *TRC = 0;
1139+
if (VRBase) {
1140+
TRC = RegMap->getRegClass(VRBase);
1141+
} else {
11021142

1103-
// Pick the register class of the right type that contains this physreg.
1104-
for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
1105-
E = MRI.regclass_end(); I != E; ++I)
1106-
if ((*I)->getType() == Node->getValueType(0) &&
1107-
(*I)->contains(SrcReg)) {
1108-
TRC = *I;
1109-
break;
1110-
}
1111-
assert(TRC && "Couldn't find register class for reg copy!");
1143+
// Pick the register class of the right type that contains this physreg.
1144+
for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
1145+
E = MRI.regclass_end(); I != E; ++I)
1146+
if ((*I)->getType() == Node->getValueType(0) &&
1147+
(*I)->contains(SrcReg)) {
1148+
TRC = *I;
1149+
break;
1150+
}
1151+
assert(TRC && "Couldn't find register class for reg copy!");
11121152

1113-
// Create the reg, emit the copy.
1114-
VRBase = RegMap->createVirtualRegister(TRC);
1153+
// Create the reg, emit the copy.
1154+
VRBase = RegMap->createVirtualRegister(TRC);
1155+
}
11151156
MRI.copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
11161157
break;
11171158
}

0 commit comments

Comments
 (0)