Index: llvm/lib/Target/PowerPC/PPCInstrInfo.td =================================================================== --- llvm/lib/Target/PowerPC/PPCInstrInfo.td +++ llvm/lib/Target/PowerPC/PPCInstrInfo.td @@ -3177,6 +3177,7 @@ // the function label. def UpdateGBR : PPCEmitTimePseudo<(outs gprc:$rD, gprc:$rT), (ins gprc:$rI), "#UpdateGBR", []>; +def UNENCODED_NOP: PPCEmitTimePseudo<(outs), (ins), "#UNENCODED_NOP", []>; // Standard shifts. These are represented separately from the real shifts above // so that we can distinguish between shifts that allow 5-bit and 6-bit shift Index: llvm/lib/Target/PowerPC/PPCPreEmitPeephole.cpp =================================================================== --- llvm/lib/Target/PowerPC/PPCPreEmitPeephole.cpp +++ llvm/lib/Target/PowerPC/PPCPreEmitPeephole.cpp @@ -170,6 +170,10 @@ Changed |= removeRedundantLIs(MBB, TRI); for (MachineInstr &MI : MBB) { unsigned Opc = MI.getOpcode(); + if (Opc == PPC::UNENCODED_NOP) { + InstrsToErase.push_back(&MI); + continue; + } // Detect self copies - these can result from running AADB. if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) { const MCInstrDesc &MCID = TII->get(Opc); Index: llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp =================================================================== --- llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp +++ llvm/lib/Target/PowerPC/PPCRegisterInfo.cpp @@ -747,12 +747,17 @@ Register SrcReg = MI.getOperand(0).getReg(); // Search up the BB to find the definition of the CR bit. - MachineBasicBlock::reverse_iterator Ins; + MachineBasicBlock::reverse_iterator Ins = MI; + MachineBasicBlock::reverse_iterator Rend = MBB.rend(); + ++Ins; unsigned CRBitSpillDistance = 0; - for (Ins = MI; Ins != MBB.rend(); Ins++) { + bool SeenUse = false; + for (; Ins != Rend; ++Ins) { // Definition found. if (Ins->modifiesRegister(SrcReg, TRI)) break; + if (Ins->readsRegister(SrcReg, TRI)) + SeenUse = true; // Unable to find CR bit definition within maximum search distance. if (CRBitSpillDistance == MaxCRBitSpillDist) { Ins = MI; @@ -767,15 +772,18 @@ if (Ins == MBB.rend()) Ins = MI; + bool SpillsKnownBit = false; // There is no need to extract the CR bit if its value is already known. switch (Ins->getOpcode()) { case PPC::CRUNSET: BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) .addImm(0); + SpillsKnownBit = true; break; case PPC::CRSET: BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) .addImm(-32768); + SpillsKnownBit = true; break; default: // We need to move the CR field that contains the CR bit we are spilling. @@ -803,8 +811,13 @@ .addReg(Reg, RegState::Kill), FrameIndex); + bool KillsCRBit = MI.killsRegister(SrcReg, TRI); // Discard the pseudo instruction. MBB.erase(II); + if (SpillsKnownBit && KillsCRBit && !SeenUse) { + Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); + Ins->RemoveOperand(0); + } } void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, Index: llvm/test/CodeGen/PowerPC/knowCRBitSpill.ll =================================================================== --- llvm/test/CodeGen/PowerPC/knowCRBitSpill.ll +++ llvm/test/CodeGen/PowerPC/knowCRBitSpill.ll @@ -21,7 +21,6 @@ ; CHECK-DAG: mfocrf [[REG2:.*]], [[CREG]] ; CHECK-DAG: rlwinm [[REG2]], [[REG2]] ; CHECK: .LBB0_3: -; CHECK-DAG: creqv [[CREG:.*]]*cr5+lt, [[CREG]]*cr5+lt, [[CREG]]*cr5+lt ; CHECK: lis [[REG1:.*]], -32768 ; CHECK: .LBB0_4: ; CHECK-NOT: mfocrf [[REG2:.*]], [[CREG]] @@ -81,7 +80,6 @@ define dso_local signext i32 @spillCRUNSET(%struct.p5rx* readonly %p1, i32 signext %p2, i32 signext %p3) { ; CHECK-LABEL: spillCRUNSET: ; CHECK: # %bb.0: # %entry -; CHECK-DAG: crxor [[CREG:.*]]*cr5+lt, [[CREG]]*cr5+lt, [[CREG]]*cr5+lt ; CHECK-DAG: li [[REG1:.*]], 0 ; CHECK-NOT: mfocrf [[REG2:.*]], [[CREG]] ; CHECK-NOT: rlwinm [[REG2]], [[REG2]]