diff --git a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp index b5409330a8c5..482f5b4e97dd 100644 --- a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp +++ b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp @@ -1149,35 +1149,37 @@ static bool tryConstantFoldOp(MachineRegisterInfo &MRI, static bool tryFoldCndMask(const SIInstrInfo *TII, MachineInstr *MI) { unsigned Opc = MI->getOpcode(); + if (Opc != AMDGPU::V_CNDMASK_B32_e32 && Opc != AMDGPU::V_CNDMASK_B32_e64 && + Opc != AMDGPU::V_CNDMASK_B64_PSEUDO) + return false; - if (Opc == AMDGPU::V_CNDMASK_B32_e32 || - Opc == AMDGPU::V_CNDMASK_B32_e64 || - Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) { - const MachineOperand *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); - const MachineOperand *Src1 = TII->getNamedOperand(*MI, AMDGPU::OpName::src1); - int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1_modifiers); - int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0_modifiers); - if (Src1->isIdenticalTo(*Src0) && - (Src1ModIdx == -1 || !MI->getOperand(Src1ModIdx).getImm()) && - (Src0ModIdx == -1 || !MI->getOperand(Src0ModIdx).getImm())) { - LLVM_DEBUG(dbgs() << "Folded " << *MI << " into "); - auto &NewDesc = - TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY : getMovOpc(false)); - int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); - if (Src2Idx != -1) - MI->RemoveOperand(Src2Idx); - MI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1)); - if (Src1ModIdx != -1) - MI->RemoveOperand(Src1ModIdx); - if (Src0ModIdx != -1) - MI->RemoveOperand(Src0ModIdx); - mutateCopyOp(*MI, NewDesc); - LLVM_DEBUG(dbgs() << *MI); - return true; - } - } + MachineOperand *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0); + MachineOperand *Src1 = TII->getNamedOperand(*MI, AMDGPU::OpName::src1); + if (!Src1->isIdenticalTo(*Src0)) + return false; - return false; + int Src1ModIdx = + AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1_modifiers); + int Src0ModIdx = + AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0_modifiers); + if ((Src1ModIdx != -1 && MI->getOperand(Src1ModIdx).getImm() != 0) || + (Src0ModIdx != -1 && MI->getOperand(Src0ModIdx).getImm() != 0)) + return false; + + LLVM_DEBUG(dbgs() << "Folded " << *MI << " into "); + auto &NewDesc = + TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY : getMovOpc(false)); + int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); + if (Src2Idx != -1) + MI->RemoveOperand(Src2Idx); + MI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1)); + if (Src1ModIdx != -1) + MI->RemoveOperand(Src1ModIdx); + if (Src0ModIdx != -1) + MI->RemoveOperand(Src0ModIdx); + mutateCopyOp(*MI, NewDesc); + LLVM_DEBUG(dbgs() << *MI); + return true; } void SIFoldOperands::foldInstOperand(MachineInstr &MI,