summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Target/X86/X86InstructionSelector.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Target/X86/X86InstructionSelector.cpp')
-rw-r--r--llvm/lib/Target/X86/X86InstructionSelector.cpp84
1 files changed, 84 insertions, 0 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,
OpenPOWER on IntegriCloud