diff options
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Target/X86/X86InstructionSelector.cpp | 84 | ||||
-rw-r--r-- | llvm/lib/Target/X86/X86LegalizerInfo.cpp | 8 |
2 files changed, 88 insertions, 4 deletions
diff --git a/llvm/lib/Target/X86/X86InstructionSelector.cpp b/llvm/lib/Target/X86/X86InstructionSelector.cpp index 0750f272799..4996288a193 100644 --- a/llvm/lib/Target/X86/X86InstructionSelector.cpp +++ b/llvm/lib/Target/X86/X86InstructionSelector.cpp @@ -114,6 +114,8 @@ private: bool selectImplicitDefOrPHI(MachineInstr &I, MachineRegisterInfo &MRI) const; bool selectShift(MachineInstr &I, MachineRegisterInfo &MRI, MachineFunction &MF) const; + bool selectSDiv(MachineInstr &I, MachineRegisterInfo &MRI, + MachineFunction &MF) const; // emit insert subreg instruction and insert it before MachineInstr &I bool emitInsertSubreg(unsigned DstReg, unsigned SrcReg, MachineInstr &I, @@ -379,6 +381,8 @@ bool X86InstructionSelector::select(MachineInstr &I, case TargetOpcode::G_ASHR: case TargetOpcode::G_LSHR: return selectShift(I, MRI, MF); + case TargetOpcode::G_SDIV: + return selectSDiv(I, MRI, MF); } return false; @@ -1481,6 +1485,86 @@ bool X86InstructionSelector::selectShift(MachineInstr &I, return true; } +bool X86InstructionSelector::selectSDiv(MachineInstr &I, + MachineRegisterInfo &MRI, + MachineFunction &MF) const { + + assert(I.getOpcode() == TargetOpcode::G_SDIV && "unexpected instruction"); + + const unsigned DstReg = I.getOperand(0).getReg(); + const unsigned DividentReg = I.getOperand(1).getReg(); + const unsigned DiviserReg = I.getOperand(2).getReg(); + + const LLT RegTy = MRI.getType(DstReg); + assert(RegTy == MRI.getType(DividentReg) && + RegTy == MRI.getType(DiviserReg) && + "Arguments and return value types must match"); + + const RegisterBank &RegRB = *RBI.getRegBank(DstReg, MRI, TRI); + + // For the X86 IDIV instruction, in most cases the dividend + // (numerator) must be in a specific register pair highreg:lowreg, + // producing the quotient in lowreg and the remainder in highreg. + // For most data types, to set up the instruction, the dividend is + // copied into lowreg, and lowreg is sign-extended into highreg. The + // exception is i8, where the dividend is defined as a single register rather + // than a register pair, and we therefore directly sign-extend the dividend + // into lowreg, instead of copying, and ignore the highreg. + const static struct SDivEntry { + unsigned SizeInBits; + unsigned QuotientReg; + unsigned DividentRegUpper; + unsigned DividentRegLower; + unsigned OpSignExtend; + unsigned OpCopy; + unsigned OpDiv; + } OpTable[] = { + {8, X86::AL, X86::NoRegister, X86::AX, 0, X86::MOVSX16rr8, + X86::IDIV8r}, // i8 + {16, X86::AX, X86::DX, X86::AX, X86::CWD, TargetOpcode::COPY, + X86::IDIV16r}, // i16 + {32, X86::EAX, X86::EDX, X86::EAX, X86::CDQ, TargetOpcode::COPY, + X86::IDIV32r}, // i32 + {64, X86::RAX, X86::RDX, X86::RAX, X86::CQO, TargetOpcode::COPY, + X86::IDIV64r} // i64 + }; + + if (RegRB.getID() != X86::GPRRegBankID) + return false; + + auto SDivEntryIt = std::find_if( + std::begin(OpTable), std::end(OpTable), [RegTy](const SDivEntry &El) { + return El.SizeInBits == RegTy.getSizeInBits(); + }); + + if (SDivEntryIt == std::end(OpTable)) + return false; + + const TargetRegisterClass *RegRC = getRegClass(RegTy, RegRB); + if (!RBI.constrainGenericRegister(DividentReg, *RegRC, MRI) || + !RBI.constrainGenericRegister(DiviserReg, *RegRC, MRI) || + !RBI.constrainGenericRegister(DstReg, *RegRC, MRI)) { + DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) + << " operand\n"); + return false; + } + + BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SDivEntryIt->OpCopy), + SDivEntryIt->DividentRegLower) + .addReg(DividentReg); + if (SDivEntryIt->DividentRegUpper != X86::NoRegister) + BuildMI(*I.getParent(), I, I.getDebugLoc(), + TII.get(SDivEntryIt->OpSignExtend)); + BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SDivEntryIt->OpDiv)) + .addReg(DiviserReg); + BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY), + DstReg) + .addReg(SDivEntryIt->QuotientReg); + + I.eraseFromParent(); + return true; +} + InstructionSelector * llvm::createX86InstructionSelector(const X86TargetMachine &TM, X86Subtarget &Subtarget, diff --git a/llvm/lib/Target/X86/X86LegalizerInfo.cpp b/llvm/lib/Target/X86/X86LegalizerInfo.cpp index 94dece2d150..c5e37d56d0e 100644 --- a/llvm/lib/Target/X86/X86LegalizerInfo.cpp +++ b/llvm/lib/Target/X86/X86LegalizerInfo.cpp @@ -131,8 +131,8 @@ void X86LegalizerInfo::setLegalizerInfo32bit() { .widenScalarToNextPow2(0, /*Min*/ 8); getActionDefinitionsBuilder(G_INTTOPTR).legalFor({s32, p0}); - // Shifts - getActionDefinitionsBuilder({G_SHL, G_LSHR, G_ASHR}) + // Shifts and SDIV + getActionDefinitionsBuilder({G_SHL, G_LSHR, G_ASHR, G_SDIV}) .legalFor({s8, s16, s32}) .clampScalar(0, s8, s32); } @@ -214,8 +214,8 @@ void X86LegalizerInfo::setLegalizerInfo64bit() { // Comparison setAction({G_ICMP, 1, s64}, Legal); - // Shifts - getActionDefinitionsBuilder({G_SHL, G_LSHR, G_ASHR}) + // Shifts and SDIV + getActionDefinitionsBuilder({G_SHL, G_LSHR, G_ASHR, G_SDIV}) .legalFor({s8, s16, s32, s64}) .clampScalar(0, s8, s64); |