@@ -52,9 +52,8 @@ class SIShrinkInstructions {
52
52
bool instModifiesReg (const MachineInstr *MI, unsigned Reg,
53
53
unsigned SubReg) const ;
54
54
Register trySwapCndOperands (MachineInstr &MI) const ;
55
- bool
56
- shouldSwapCndOperands (MachineInstr &MI,
57
- SmallVector<MachineOperand *, 4 > &UsesToProcess) const ;
55
+ bool shouldSwapCndOperands (Register Reg,
56
+ std::vector<MachineInstr *> &UsesToProcess) const ;
58
57
unsigned getInverseCompareOpcode (MachineInstr &MI) const ;
59
58
TargetInstrInfo::RegSubRegPair getSubRegForIndex (Register Reg, unsigned Sub,
60
59
unsigned I) const ;
@@ -954,31 +953,34 @@ unsigned SIShrinkInstructions::getInverseCompareOpcode(MachineInstr &MI) const {
954
953
}
955
954
956
955
bool SIShrinkInstructions::shouldSwapCndOperands (
957
- MachineInstr &MI, SmallVector<MachineOperand *, 4 > &UsesToProcess) const {
958
- auto AllUses = MRI->use_nodbg_operands (MI. getOperand ( 0 ). getReg () );
956
+ Register Reg, std::vector<MachineInstr * > &UsesToProcess) const {
957
+ auto AllUses = MRI->use_nodbg_instructions (Reg );
959
958
int InstsToSwap = 0 ;
960
959
961
- for (auto &Use : AllUses) {
962
- MachineInstr *UseInst = Use.getParent ();
963
- if (UseInst->getOpcode () != AMDGPU::V_CNDMASK_B32_e64)
960
+ for (auto &UseInst : AllUses) {
961
+ if (UseInst.getOpcode () != AMDGPU::V_CNDMASK_B32_e64)
964
962
return false ;
965
963
966
- UsesToProcess.push_back (&Use );
964
+ UsesToProcess.push_back (&UseInst );
967
965
968
- MachineOperand &Src0 = UseInst-> getOperand (2 );
969
- MachineOperand &Src1 = UseInst-> getOperand (4 );
966
+ MachineOperand &Src0 = UseInst. getOperand (2 );
967
+ MachineOperand &Src1 = UseInst. getOperand (4 );
970
968
971
- bool Src0Imm = Src0.isImm ();
972
- bool Src1Imm = Src1.isImm ();
969
+ // if instruction has source modifiers it cannot be converted to VOP2
970
+ if (UseInst.getOperand (1 ).getImm () != SISrcMods::NONE ||
971
+ UseInst.getOperand (3 ).getImm () != SISrcMods::NONE)
972
+ continue ;
973
+
974
+ bool Src0IsVGPR = Src0.isReg () && TRI->isVGPR (*MRI, Src0.getReg ());
975
+ bool Src1IsVGPR = Src1.isReg () && TRI->isVGPR (*MRI, Src1.getReg ());
973
976
974
- if (!Src1Imm && Src0Imm)
977
+ // Src1 always has to be VGPR in VOP2
978
+ if (!Src0IsVGPR && Src1IsVGPR)
975
979
InstsToSwap--;
976
- else if (Src1Imm && !Src0Imm &&
977
- UseInst->getOperand (1 ).getImm () == SISrcMods::NONE &&
978
- TRI->isVGPR (*MRI, Src0.getReg ()))
980
+ else if (Src0IsVGPR && !Src1IsVGPR)
979
981
InstsToSwap++;
980
982
}
981
- return ( InstsToSwap > 0 ) ;
983
+ return InstsToSwap > 0 ;
982
984
}
983
985
984
986
static void swapCndOperands (MachineInstr &MI) {
@@ -1013,9 +1015,9 @@ Register SIShrinkInstructions::trySwapCndOperands(MachineInstr &MI) const {
1013
1015
Register Reg = MI.getOperand (0 ).getReg ();
1014
1016
1015
1017
unsigned Opcode = getInverseCompareOpcode (MI);
1016
- SmallVector<MachineOperand *, 4 > UsesToProcess;
1018
+ std::vector<MachineInstr * > UsesToProcess;
1017
1019
if (!Opcode ||
1018
- !SIShrinkInstructions::shouldSwapCndOperands (MI , UsesToProcess))
1020
+ !SIShrinkInstructions::shouldSwapCndOperands (Reg , UsesToProcess))
1019
1021
return Reg;
1020
1022
1021
1023
auto DL = MI.getDebugLoc ();
@@ -1027,15 +1029,14 @@ Register SIShrinkInstructions::trySwapCndOperands(MachineInstr &MI) const {
1027
1029
1028
1030
unsigned OpNum = MI.getNumExplicitOperands ();
1029
1031
for (unsigned Idx = 1 ; Idx < OpNum; Idx++) {
1030
- MachineOperand Op = MI.getOperand (Idx);
1032
+ MachineOperand & Op = MI.getOperand (Idx);
1031
1033
InverseCompare.add (Op);
1032
1034
if (Op.isReg () && Op.isKill ())
1033
1035
InverseCompare->getOperand (Idx).setIsKill (false );
1034
1036
}
1035
1037
1036
- for (auto &Use : UsesToProcess) {
1037
- MachineInstr *Inst = Use->getParent ();
1038
- swapCndOperands (*Inst);
1038
+ for (auto Use : UsesToProcess) {
1039
+ swapCndOperands (*Use);
1039
1040
}
1040
1041
1041
1042
MRI->replaceRegWith (Reg, NewVCC);
0 commit comments