diff options
Diffstat (limited to 'llvm/lib/Target/X86/X86FixupBWInsts.cpp')
-rw-r--r-- | llvm/lib/Target/X86/X86FixupBWInsts.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/llvm/lib/Target/X86/X86FixupBWInsts.cpp b/llvm/lib/Target/X86/X86FixupBWInsts.cpp index 045a4ed8dca..24d78b0b776 100644 --- a/llvm/lib/Target/X86/X86FixupBWInsts.cpp +++ b/llvm/lib/Target/X86/X86FixupBWInsts.cpp @@ -93,6 +93,11 @@ class FixupBWInstPass : public MachineFunctionPass { /// OK, otherwise return nullptr. MachineInstr *tryReplaceLoad(unsigned New32BitOpcode, MachineInstr *MI) const; + /// Change the MachineInstr \p MI into the equivalent 32-bit copy if it is + /// safe to do so. Return the replacement instruction if OK, otherwise return + /// nullptr. + MachineInstr *tryReplaceCopy(MachineInstr *MI) const; + public: FixupBWInstPass() : MachineFunctionPass(ID) {} @@ -219,6 +224,39 @@ MachineInstr *FixupBWInstPass::tryReplaceLoad(unsigned New32BitOpcode, return MIB; } +MachineInstr *FixupBWInstPass::tryReplaceCopy(MachineInstr *MI) const { + assert(MI->getNumExplicitOperands() == 2); + auto &OldDest = MI->getOperand(0); + auto &OldSrc = MI->getOperand(1); + + unsigned NewDestReg; + if (!getSuperRegDestIfDead(MI, NewDestReg)) + return nullptr; + + unsigned NewSrcReg = getX86SubSuperRegister(OldSrc.getReg(), 32); + + // This is only correct if we access the same subregister index: otherwise, + // we could try to replace "movb %ah, %al" with "movl %eax, %eax". + auto *TRI = &TII->getRegisterInfo(); + if (TRI->getSubRegIndex(NewSrcReg, OldSrc.getReg()) != + TRI->getSubRegIndex(NewDestReg, OldDest.getReg())) + return nullptr; + + // Safe to change the instruction. + // Don't set src flags, as we don't know if we're also killing the superreg. + MachineInstrBuilder MIB = + BuildMI(*MF, MI->getDebugLoc(), TII->get(X86::MOV32rr), NewDestReg) + .addReg(NewSrcReg); + +#ifndef NDEBUG + // Make sure we didn't drop any other implicit operand. + for (auto &Op : MI->implicit_operands()) + assert(Op.getReg() == NewDestReg && "Copy imp-defs unrelated reg?"); +#endif + + return MIB; +} + void FixupBWInstPass::processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB) { @@ -265,6 +303,15 @@ void FixupBWInstPass::processBasicBlock(MachineFunction &MF, NewMI = tryReplaceLoad(X86::MOVZX32rm16, MI); break; + case X86::MOV8rr: + case X86::MOV16rr: + // Always try to replace 8/16 bit copies with a 32 bit copy. + // Code size is either less (16) or equal (8), and there is sometimes a + // perf advantage from eliminating a false dependence on the upper portion + // of the register. + NewMI = tryReplaceCopy(MI); + break; + default: // nothing to do here. break; |