diff options
Diffstat (limited to 'llvm/lib/Target/X86')
-rw-r--r-- | llvm/lib/Target/X86/X86FixupBWInsts.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/lib/Target/X86/X86FixupBWInsts.cpp b/llvm/lib/Target/X86/X86FixupBWInsts.cpp index ad152824c18..0d1a14b67d2 100644 --- a/llvm/lib/Target/X86/X86FixupBWInsts.cpp +++ b/llvm/lib/Target/X86/X86FixupBWInsts.cpp @@ -90,6 +90,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: static char ID; @@ -226,6 +231,42 @@ 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. + // However, the superregister might not be defined; make it explicit that + // we don't care about the higher bits by reading it as Undef, and adding + // an imp-use on the original subregister. + MachineInstrBuilder MIB = + BuildMI(*MF, MI->getDebugLoc(), TII->get(X86::MOV32rr), NewDestReg) + .addReg(NewSrcReg, RegState::Undef) + .addReg(OldSrc.getReg(), RegState::Implicit); + + // Drop imp-defs/uses that would be redundant with the new def/use. + for (auto &Op : MI->implicit_operands()) + if (Op.getReg() != (Op.isDef() ? NewDestReg : NewSrcReg)) + MIB.addOperand(Op); + + return MIB; +} + void FixupBWInstPass::processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB) { @@ -272,6 +313,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; |