@@ -1006,7 +1006,28 @@ void SimpleSched::EmitNode(NodeInfo *NI) {
1006
1006
1007
1007
// Add result register values for things that are defined by this
1008
1008
// 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);
1010
1031
1011
1032
// Emit all of the actual operands of this instruction, adding them to the
1012
1033
// instruction as appropriate.
@@ -1084,10 +1105,11 @@ void SimpleSched::EmitNode(NodeInfo *NI) {
1084
1105
case ISD::TokenFactor:
1085
1106
break ;
1086
1107
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));
1091
1113
break ;
1092
1114
}
1093
1115
case ISD::CopyFromReg: {
@@ -1097,21 +1119,40 @@ void SimpleSched::EmitNode(NodeInfo *NI) {
1097
1119
break ;
1098
1120
}
1099
1121
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
+
1100
1137
// Figure out the register class to create for the destreg.
1101
1138
const TargetRegisterClass *TRC = 0 ;
1139
+ if (VRBase) {
1140
+ TRC = RegMap->getRegClass (VRBase);
1141
+ } else {
1102
1142
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!" );
1112
1152
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
+ }
1115
1156
MRI.copyRegToReg (*BB, BB->end (), VRBase, SrcReg, TRC);
1116
1157
break ;
1117
1158
}
0 commit comments