Skip to content

Commit e711a0c

Browse files
authored
[MIPS] Fix missing ANDI optimization (#97689)
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. Fixes #42826
1 parent 1ea0865 commit e711a0c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

llvm/lib/Target/Mips/Mips64InstrInfo.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,12 @@ def : MipsPat<(sra GPR64:$rt, (i32 (trunc GPR64:$rs))),
830830
def : MipsPat<(rotr GPR64:$rt, (i32 (trunc GPR64:$rs))),
831831
(DROTRV GPR64:$rt, (EXTRACT_SUBREG GPR64:$rs, sub_32))>,
832832
ISA_MIPS3, GPR_64;
833+
def : MipsPat<(and (srl (i32 (trunc GPR64:$src)), immZExt5:$imm5), immZExt16:$value),
834+
(ANDi (EXTRACT_SUBREG (DSRL GPR64:$src, immZExt5:$imm5), sub_32), immZExt16:$value)>,
835+
ISA_MIPS3, GPR_64;
836+
def : MipsPat<(MipsExt (i32 (trunc GPR64:$src)), immZExt5:$pos, immZExt5:$size),
837+
(EXTRACT_SUBREG (DEXT GPR64:$src, immZExt5:$pos, immZExt5:$size), sub_32)>,
838+
ISA_MIPS3, GPR_64;
833839

834840
// 32-to-64-bit extension
835841
def : MipsPat<(i64 (anyext GPR32:$src)),

llvm/test/CodeGen/Mips/llvm-ir/and-srl.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
define i64 @foo(i64 noundef %a) {
77
; MIPS4-LABEL: foo:
88
; MIPS4: # %bb.0: # %entry
9-
; MIPS4-NEXT: sll $1, $4, 0
10-
; MIPS4-NEXT: srl $1, $1, 2
9+
; MIPS4-NEXT: dsrl $1, $4, 2
1110
; MIPS4-NEXT: andi $1, $1, 7
1211
; MIPS4-NEXT: daddiu $2, $zero, 1
1312
; MIPS4-NEXT: jr $ra
1413
; MIPS4-NEXT: dsllv $2, $2, $1
1514
;
1615
; MIPS64R2-LABEL: foo:
1716
; MIPS64R2: # %bb.0: # %entry
18-
; MIPS64R2-NEXT: sll $1, $4, 0
19-
; MIPS64R2-NEXT: ext $1, $1, 2, 3
17+
; MIPS64R2-NEXT: dext $1, $4, 2, 3
2018
; MIPS64R2-NEXT: daddiu $2, $zero, 1
2119
; MIPS64R2-NEXT: jr $ra
2220
; MIPS64R2-NEXT: dsllv $2, $2, $1

0 commit comments

Comments
 (0)