Skip to content

Commit da0046a

Browse files
committed
[MIPS] Fix missing ANDI optimization
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
1 parent 173514d commit da0046a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
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)),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: llc < %s -mtriple=mips64-unknown-linux-gnu -mcpu=mips64 | FileCheck %s \
2+
; RUN: -check-prefix=MIPS4
3+
; RUN: llc < %s -mtriple=mips64-unknown-linux-gnu -mcpu=mips64r2 | FileCheck %s \
4+
; RUN: -check-prefix=MIPS64R2
5+
6+
define i64 @foo(i64 noundef %a) {
7+
; MIPS4-LABEL: foo:
8+
; MIPS4: # %bb.0: # %entry
9+
; MIPS4-NEXT: dsrl $1, $4, 2
10+
; MIPS4-NEXT: andi $1, $1, 7
11+
; MIPS4-NEXT: daddiu $2, $zero, 1
12+
; MIPS4-NEXT: jr $ra
13+
; MIPS4-NEXT: dsllv $2, $2, $1
14+
;
15+
; MIPS64R2-LABEL: foo:
16+
; MIPS64R2: # %bb.0: # %entry
17+
; MIPS64R2-NEXT: dext $1, $4, 2, 3
18+
; MIPS64R2-NEXT: daddiu $2, $zero, 1
19+
; MIPS64R2-NEXT: jr $ra
20+
; MIPS64R2-NEXT: dsllv $2, $2, $1
21+
entry:
22+
%div1 = lshr i64 %a, 2
23+
%and = and i64 %div1, 7
24+
%shl = shl nuw nsw i64 1, %and
25+
ret i64 %shl
26+
}

0 commit comments

Comments
 (0)