Skip to content

Commit

Permalink
[AArch64] Remove superfluous sxtw in peephole opt
Browse files Browse the repository at this point in the history
Across a basic-block we might have in i32 extract from a value that only
operates on upper bits (for example a sxtw). We can replace the COPY with a
new version skipping the sxtw.
  • Loading branch information
davemgreen committed Jun 21, 2024
1 parent e887624 commit 2cf6269
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
31 changes: 31 additions & 0 deletions llvm/lib/Target/AArch64/AArch64MIPeepholeOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct AArch64MIPeepholeOpt : public MachineFunctionPass {
bool visitINSviGPR(MachineInstr &MI, unsigned Opc);
bool visitINSvi64lane(MachineInstr &MI);
bool visitFMOVDr(MachineInstr &MI);
bool visitCopy(MachineInstr &MI);
bool runOnMachineFunction(MachineFunction &MF) override;

StringRef getPassName() const override {
Expand Down Expand Up @@ -690,6 +691,33 @@ bool AArch64MIPeepholeOpt::visitFMOVDr(MachineInstr &MI) {
return true;
}

// Acrocss a basic-block we might have in i32 extract from a value that only
// operates on upper bits (for example a sxtw). We can replace the COPY with a
// new version skipping the sxtw.
bool AArch64MIPeepholeOpt::visitCopy(MachineInstr &MI) {
if (MI.getOperand(1).getSubReg() != AArch64::sub_32 ||
!MRI->hasOneNonDBGUse(MI.getOperand(1).getReg()))
return false;

MachineInstr *SrcMI = MRI->getUniqueVRegDef(MI.getOperand(1).getReg());
MachineInstr *CopyMI = SrcMI;
if (SrcMI && SrcMI->isFullCopy() &&
MRI->hasOneNonDBGUse(SrcMI->getOperand(1).getReg()))
SrcMI = MRI->getUniqueVRegDef(SrcMI->getOperand(1).getReg());

if (!SrcMI || SrcMI->getOpcode() != AArch64::SBFMXri ||
SrcMI->getOperand(2).getImm() != 0 || SrcMI->getOperand(3).getImm() != 31)
return false;

Register SrcReg = SrcMI->getOperand(1).getReg();
MRI->constrainRegClass(SrcReg, MRI->getRegClass(MI.getOperand(1).getReg()));
MI.getOperand(1).setReg(SrcReg);
if (CopyMI != SrcMI)
CopyMI->eraseFromParent();
SrcMI->eraseFromParent();
return true;
}

bool AArch64MIPeepholeOpt::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;
Expand Down Expand Up @@ -771,6 +799,9 @@ bool AArch64MIPeepholeOpt::runOnMachineFunction(MachineFunction &MF) {
case AArch64::FMOVDr:
Changed |= visitFMOVDr(MI);
break;
case AArch64::COPY:
Changed |= visitCopy(MI);
break;
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions llvm/test/CodeGen/AArch64/aarch64-mull-masks.ll
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ define i64 @smull_ldrsw_shift(ptr %x0, i64 %x1) {
; CHECK-LABEL: smull_ldrsw_shift:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: ldrsw x8, [x0]
; CHECK-NEXT: sxtw x9, w1
; CHECK-NEXT: smull x0, w8, w9
; CHECK-NEXT: smull x0, w8, w1
; CHECK-NEXT: ret
entry:
%ext64 = load i32, ptr %x0
Expand Down Expand Up @@ -490,8 +489,7 @@ define i64 @smaddl_ldrsw_shift(ptr %x0, i64 %x1, i64 %x2) {
; CHECK-LABEL: smaddl_ldrsw_shift:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: ldrsw x8, [x0]
; CHECK-NEXT: sxtw x9, w1
; CHECK-NEXT: smaddl x0, w8, w9, x2
; CHECK-NEXT: smaddl x0, w8, w1, x2
; CHECK-NEXT: ret
entry:
%ext64 = load i32, ptr %x0
Expand Down Expand Up @@ -654,8 +652,7 @@ define i64 @smnegl_ldrsw_shift(ptr %x0, i64 %x1) {
; CHECK-LABEL: smnegl_ldrsw_shift:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: ldrsw x8, [x0]
; CHECK-NEXT: sxtw x9, w1
; CHECK-NEXT: smnegl x0, w8, w9
; CHECK-NEXT: smnegl x0, w8, w1
; CHECK-NEXT: ret
entry:
%ext64 = load i32, ptr %x0
Expand Down Expand Up @@ -818,8 +815,7 @@ define i64 @smsubl_ldrsw_shift(ptr %x0, i64 %x1, i64 %x2) {
; CHECK-LABEL: smsubl_ldrsw_shift:
; CHECK: // %bb.0: // %entry
; CHECK-NEXT: ldrsw x8, [x0]
; CHECK-NEXT: sxtw x9, w1
; CHECK-NEXT: smsubl x0, w8, w9, x2
; CHECK-NEXT: smsubl x0, w8, w1, x2
; CHECK-NEXT: ret
entry:
%ext64 = load i32, ptr %x0
Expand Down

0 comments on commit 2cf6269

Please sign in to comment.