This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Fix the implicit operands in PPCInstrInfo::PredicateInstruction()
ClosedPublic

Authored by ZhangKang on Jun 23 2020, 8:59 AM.

Details

Summary

In the file PPCInstrInfo.cpp:

1449 bool PPCInstrInfo::PredicateInstruction(MachineInstr &MI,
1450                                         ArrayRef<MachineOperand> Pred) const {
1451   unsigned OpC = MI.getOpcode();
1452   if (OpC == PPC::BLR || OpC == PPC::BLR8) {
1453     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1454       bool isPPC64 = Subtarget.isPPC64();
1455       MI.setDesc(get(Pred[0].getImm() ? (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR)
1456                                       : (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
1457     } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
              ...
          }

Above code only modify the opcode, but doesn't consider the implicit register for the new opcode.
For example, if MI is BLR implicit $lr, implicit $rm, in the line 1455. We use MI.setDesc() to convert the BLR to BDZLR.
We will get:

BDZLR implicit $lr, implicit $rm

But we don't realize BLR and BDZLR have different implicit register. In fact, the right instruction should be below:

BDZLR implicit-def $ctr, implicit $ctr, implicit $lr, implicit $rm

Diff Detail

Event Timeline

ZhangKang created this revision.Jun 23 2020, 8:59 AM

I don't think it makes sense to try to fix implicit operands generically. If we're changing the opcode in a way that affects the operands, the code making that change should be aware of what the change needs to be. That code can add or remove operands as necessary.

"Automatically" fixing the operands later might produce the same result, but it's harder to understand what's happening.

jsji edited reviewers, added: Restricted Project; removed: ppc-slack.Jun 26 2020, 8:55 AM
jsji requested changes to this revision.Jul 17 2020, 12:14 PM

Agree with Eli in general. Although this is the code making that change , current condition code `if (MI.getOpcode() != OpC) ` to fix the reg is too general .
We should only fix it when there is real needs.

So maybe set a flag where the implicit operands needs update, and only call the routine when the flag is on.

This revision now requires changes to proceed.Jul 17 2020, 12:14 PM
ZhangKang updated this revision to Diff 279165.Jul 20 2020, 3:11 AM

Add IsNeedFixImplicitOps to call the fixupImplicitDefUseOperands when needed.

jsji accepted this revision as: jsji.Jul 20 2020, 7:09 AM

LGTM. Please wait a few days in case @efriedma has other comments.

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
1679

IsNeedFixImplicitOps -> needImplicitOpsFix

jsji accepted this revision.Jul 20 2020, 7:09 AM
This revision is now accepted and ready to land.Jul 20 2020, 7:09 AM

I was thinking more along the lines of just writing something like MIB.addReg(Pred[1].getReg(), RegState::ImplicitDefine).addReg(Pred[1].getReg(), RegState::Implicit);.

ZhangKang updated this revision to Diff 279525.Jul 21 2020, 8:08 AM

Don't call fixupImplicitDefUseOperands ().

This revision was automatically updated to reflect the committed changes.