Skip to content

Commit

Permalink
[MIPS] Fix missing ANDI optimization
Browse files Browse the repository at this point in the history
1. Add MipsPat to optimize (andi (srl (truncate i64 $1), x), y) to
   (andi (truncate (dsrl i64 $1, x)), y).
2. Add MipsPat to optimize (ext (truncate i64 $1), x, y) to
   (truncate (dext i64 $1, x, y)).

The assembly result is the same as gcc.

Fix #42826
  • Loading branch information
yingopq committed Jul 4, 2024
1 parent 173514d commit 62488f1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions llvm/lib/Target/Mips/Mips64InstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,12 @@ def : MipsPat<(sra GPR64:$rt, (i32 (trunc GPR64:$rs))),
def : MipsPat<(rotr GPR64:$rt, (i32 (trunc GPR64:$rs))),
(DROTRV GPR64:$rt, (EXTRACT_SUBREG GPR64:$rs, sub_32))>,
ISA_MIPS3, GPR_64;
def : MipsPat<(and (srl (i32 (trunc GPR64:$src)), immZExt5:$imm5), immZExt5:$value),
(ANDi (EXTRACT_SUBREG (DSRL GPR64:$src, immZExt5:$imm5), sub_32), immZExt5:$value)>,
ISA_MIPS3, GPR_64;
def : MipsPat<(MipsExt (i32 (trunc GPR64:$src)), immZExt5:$pos, immZExt5:$size),
(EXTRACT_SUBREG (DEXT GPR64:$src, immZExt5:$pos, immZExt5:$size), sub_32)>,
ISA_MIPS3, GPR_64;

// 32-to-64-bit extension
def : MipsPat<(i64 (anyext GPR32:$src)),
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/CodeGen/Mips/llvm-ir/and-srl.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: llc < %s -mtriple=mips64-unknown-linux-gnu -mcpu=mips64 | FileCheck %s \
; RUN: -check-prefix=MIPS4
; RUN: llc < %s -mtriple=mips64-unknown-linux-gnu -mcpu=mips64r2 | FileCheck %s \
; RUN: -check-prefix=MIPS64R2

define i64 @foo(i64 noundef %a) {
; MIPS4-LABEL: foo:
; MIPS4: # %bb.0: # %entry
; MIPS4-NEXT: dsrl $1, $4, 2
; MIPS4-NEXT: andi $1, $1, 7
; MIPS4-NEXT: daddiu $2, $zero, 1
; MIPS4-NEXT: jr $ra
; MIPS4-NEXT: dsllv $2, $2, $1
;
; MIPS64R2-LABEL: foo:
; MIPS64R2: # %bb.0: # %entry
; MIPS64R2-NEXT: dext $1, $4, 2, 3
; MIPS64R2-NEXT: daddiu $2, $zero, 1
; MIPS64R2-NEXT: jr $ra
; MIPS64R2-NEXT: dsllv $2, $2, $1
entry:
%div1 = lshr i64 %a, 2
%and = and i64 %div1, 7
%shl = shl nuw nsw i64 1, %and
ret i64 %shl
}

0 comments on commit 62488f1

Please sign in to comment.