Problem in TwoAddressInstructionPass::runOnMachineFunction regarding subRegs


It seems to me that the TwoAddressInstructionPass::runOnMachineFunction method has some problems when the tied destination register has a subReg.

The two changes below improves the situation for me but I'm all new to this so I'm not sure how it's supposed to work. I'm running on 2.9.

Any comments?

@@ -1172,12 +1172,20 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
              unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg();
              TII->reMaterialize(*mbbi, mi, regA, regASubIdx, DefMI, *TRI);
            } else {
- BuildMI(*mbbi, mi, mi->getDebugLoc(), TII->get(TargetOpcode::COPY),
- regA).addReg(regB);
+ unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg();
+ if (regASubIdx) {
+ BuildMI(*mbbi, mi, mi->getDebugLoc(),
+ TII->get(TargetOpcode::COPY)).
+ addReg(regA, RegState::Define, regASubIdx).addReg(regB);
+ }
+ else {
+ BuildMI(*mbbi, mi, mi->getDebugLoc(),
+ TII->get(TargetOpcode::COPY), regA).addReg(regB);
+ }

            MachineBasicBlock::iterator prevMI = prior(mi);
            // Update DistanceMap.
            DistanceMap.insert(std::make_pair(prevMI, Dist));
@@ -1190,11 +1198,13 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
                   "inconsistent operand info for 2-reg pass");
            if (MO.isKill()) {
              RemovedKillFlag = true;
+ unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg();
+ MO.setSubReg(regASubIdx);

/Mikael Holmén

Normally, sub-register defs don't exist before TwoAddressInstructionPass. Everything is created using INSERT_SUBREG and REG_SEQUENCE.

What does your code look like before TwoAddressInstructionPass? Is it in SSA form, or do you have multiple defs on a virtual register?

The machine code verifier (-verify-machineinstrs) will check SSA form, but that feature is not in 2.9.


I have another babyism question about TwoAddressInstruction ?

It seems a opt pass, but I disable it, there is too mang failed cases, so does it a requisite pass ?