Skip to content

Commit fd65453

Browse files
wangleiatSixWeining
authored andcommitted
[LoongArch] Refactor insertDivByZeroTrap
Ensure non-terminators don't follow terminators. This patch fixes the `sdiv-udiv-srem-urem.ll` test failure with expensive check. Differential Revision: https://reviews.llvm.org/D130247
1 parent 487fa6f commit fd65453

File tree

2 files changed

+117
-41
lines changed

2 files changed

+117
-41
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -738,32 +738,52 @@ SDValue LoongArchTargetLowering::PerformDAGCombine(SDNode *N,
738738
}
739739

740740
static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
741-
MachineBasicBlock &MBB,
742-
const TargetInstrInfo &TII) {
741+
MachineBasicBlock *MBB) {
743742
if (!ZeroDivCheck)
744-
return &MBB;
743+
return MBB;
745744

746745
// Build instructions:
746+
// MBB:
747747
// div(or mod) $dst, $dividend, $divisor
748-
// bnez $divisor, 8
749-
// break 7
748+
// bnez $divisor, SinkMBB
749+
// BreakMBB:
750+
// break 7 // BRK_DIVZERO
751+
// SinkMBB:
750752
// fallthrough
753+
const BasicBlock *LLVM_BB = MBB->getBasicBlock();
754+
MachineFunction::iterator It = ++MBB->getIterator();
755+
MachineFunction *MF = MBB->getParent();
756+
auto BreakMBB = MF->CreateMachineBasicBlock(LLVM_BB);
757+
auto SinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
758+
MF->insert(It, BreakMBB);
759+
MF->insert(It, SinkMBB);
760+
761+
// Transfer the remainder of MBB and its successor edges to SinkMBB.
762+
SinkMBB->splice(SinkMBB->end(), MBB, std::next(MI.getIterator()), MBB->end());
763+
SinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
764+
765+
const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
766+
DebugLoc DL = MI.getDebugLoc();
751767
MachineOperand &Divisor = MI.getOperand(2);
752-
auto FallThrough = std::next(MI.getIterator());
768+
Register DivisorReg = Divisor.getReg();
753769

754-
BuildMI(MBB, FallThrough, MI.getDebugLoc(), TII.get(LoongArch::BNEZ))
755-
.addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
756-
.addImm(8);
770+
// MBB:
771+
BuildMI(MBB, DL, TII.get(LoongArch::BNEZ))
772+
.addReg(DivisorReg, getKillRegState(Divisor.isKill()))
773+
.addMBB(SinkMBB);
774+
MBB->addSuccessor(BreakMBB);
775+
MBB->addSuccessor(SinkMBB);
757776

777+
// BreakMBB:
758778
// See linux header file arch/loongarch/include/uapi/asm/break.h for the
759779
// definition of BRK_DIVZERO.
760-
BuildMI(MBB, FallThrough, MI.getDebugLoc(), TII.get(LoongArch::BREAK))
761-
.addImm(7/*BRK_DIVZERO*/);
780+
BuildMI(BreakMBB, DL, TII.get(LoongArch::BREAK)).addImm(7 /*BRK_DIVZERO*/);
781+
BreakMBB->addSuccessor(SinkMBB);
762782

763783
// Clear Divisor's kill flag.
764784
Divisor.setIsKill(false);
765785

766-
return &MBB;
786+
return SinkMBB;
767787
}
768788

769789
MachineBasicBlock *LoongArchTargetLowering::EmitInstrWithCustomInserter(
@@ -780,7 +800,7 @@ MachineBasicBlock *LoongArchTargetLowering::EmitInstrWithCustomInserter(
780800
case LoongArch::DIV_DU:
781801
case LoongArch::MOD_D:
782802
case LoongArch::MOD_DU:
783-
return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo());
803+
return insertDivByZeroTrap(MI, BB);
784804
break;
785805
}
786806
}

0 commit comments

Comments
 (0)