@@ -102,7 +102,7 @@ namespace {
102
102
MachineInstr *FindLastUseInMBB (unsigned Reg, MachineBasicBlock *MBB,
103
103
unsigned Dist);
104
104
105
- bool isProfitableToCommute (unsigned regB, unsigned regC,
105
+ bool isProfitableToCommute (unsigned regA, unsigned regB, unsigned regC,
106
106
MachineInstr *MI, MachineBasicBlock *MBB,
107
107
unsigned Dist);
108
108
@@ -567,7 +567,8 @@ regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
567
567
// / isProfitableToReMat - Return true if it's potentially profitable to commute
568
568
// / the two-address instruction that's being processed.
569
569
bool
570
- TwoAddressInstructionPass::isProfitableToCommute (unsigned regB, unsigned regC,
570
+ TwoAddressInstructionPass::isProfitableToCommute (unsigned regA, unsigned regB,
571
+ unsigned regC,
571
572
MachineInstr *MI, MachineBasicBlock *MBB,
572
573
unsigned Dist) {
573
574
if (OptLevel == CodeGenOpt::None)
@@ -604,15 +605,15 @@ TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
604
605
// %reg1026<def> = ADD %reg1024, %reg1025
605
606
// r0 = MOV %reg1026
606
607
// Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
607
- unsigned FromRegB = getMappedReg (regB, SrcRegMap );
608
- unsigned FromRegC = getMappedReg (regC, SrcRegMap);
609
- unsigned ToRegB = getMappedReg (regB, DstRegMap );
610
- unsigned ToRegC = getMappedReg (regC, DstRegMap );
611
- if ((FromRegB && ToRegB && ! regsAreCompatible (FromRegB, ToRegB , TRI)) &&
612
- (( !FromRegC && !ToRegC) ||
613
- regsAreCompatible (FromRegB, ToRegC, TRI) ||
614
- regsAreCompatible (FromRegC, ToRegB, TRI)))
615
- return true ;
608
+ unsigned ToRegA = getMappedReg (regA, DstRegMap );
609
+ if (ToRegA) {
610
+ unsigned FromRegB = getMappedReg (regB, SrcRegMap );
611
+ unsigned FromRegC = getMappedReg (regC, SrcRegMap );
612
+ bool BComp = !FromRegB || regsAreCompatible (FromRegB, ToRegA , TRI);
613
+ bool CComp = !FromRegC || regsAreCompatible (FromRegC, ToRegA, TRI);
614
+ if (BComp != CComp)
615
+ return !BComp && CComp;
616
+ }
616
617
617
618
// If there is a use of regC between its last def (could be livein) and this
618
619
// instruction, then bail.
@@ -1211,6 +1212,9 @@ TryInstructionTransform(MachineBasicBlock::iterator &mi,
1211
1212
return true ; // Done with this instruction.
1212
1213
}
1213
1214
1215
+ if (TargetRegisterInfo::isVirtualRegister (regA))
1216
+ ScanUses (regA, &*mbbi, Processed);
1217
+
1214
1218
// Check if it is profitable to commute the operands.
1215
1219
unsigned SrcOp1, SrcOp2;
1216
1220
unsigned regC = 0 ;
@@ -1230,7 +1234,7 @@ TryInstructionTransform(MachineBasicBlock::iterator &mi,
1230
1234
// If C dies but B does not, swap the B and C operands.
1231
1235
// This makes the live ranges of A and C joinable.
1232
1236
TryCommute = true ;
1233
- else if (isProfitableToCommute (regB, regC, &MI, mbbi, Dist)) {
1237
+ else if (isProfitableToCommute (regA, regB, regC, &MI, mbbi, Dist)) {
1234
1238
TryCommute = true ;
1235
1239
AggressiveCommute = true ;
1236
1240
}
@@ -1252,9 +1256,6 @@ TryInstructionTransform(MachineBasicBlock::iterator &mi,
1252
1256
return true ;
1253
1257
}
1254
1258
1255
- if (TargetRegisterInfo::isVirtualRegister (regA))
1256
- ScanUses (regA, &*mbbi, Processed);
1257
-
1258
1259
if (MI.isConvertibleTo3Addr ()) {
1259
1260
// This instruction is potentially convertible to a true
1260
1261
// three-address instruction. Check if it is profitable.
0 commit comments