Skip to content

Commit

Permalink
[RISCV]Preserve (and X, 0xffff) in targetShrinkDemandedConstant
Browse files Browse the repository at this point in the history
shrinkdemandedconstant does some optimizations, but is not very friendly to riscv, targetShrinkDemandedConstant to limit the damage.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D134155
  • Loading branch information
ChunyuLiao committed Sep 19, 2022
1 parent 0820c6e commit 2e74157
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 5 additions & 6 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Expand Up @@ -10041,12 +10041,11 @@ bool RISCVTargetLowering::targetShrinkDemandedConstant(

// And has a few special cases for zext.
if (Opcode == ISD::AND) {
// Preserve (and X, 0xffff) when zext.h is supported.
if (Subtarget.hasStdExtZbb() || Subtarget.hasStdExtZbp()) {
APInt NewMask = APInt(Mask.getBitWidth(), 0xffff);
if (IsLegalMask(NewMask))
return UseMask(NewMask);
}
// Preserve (and X, 0xffff), if zext.h exists use zext.h,
// otherwise use SLLI + SRLI.
APInt NewMask = APInt(Mask.getBitWidth(), 0xffff);
if (IsLegalMask(NewMask))
return UseMask(NewMask);

// Try to preserve (and X, 0xffffffff), the (zext_inreg X, i32) pattern.
if (VT == MVT::i64) {
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/CodeGen/RISCV/rv64i-demanded-bits.ll
Expand Up @@ -140,3 +140,16 @@ define signext i32 @andi_srliw(i32 signext %0, ptr %1, i32 signext %2) {
%6 = add i32 %4, %2
ret i32 %6
}

define i32 @and_or(i32 signext %x) {
; CHECK-LABEL: and_or:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: ori a0, a0, 255
; CHECK-NEXT: slli a0, a0, 48
; CHECK-NEXT: srli a0, a0, 48
; CHECK-NEXT: ret
entry:
%and = and i32 %x, 65280
%or = or i32 %and, 255
ret i32 %or
}

0 comments on commit 2e74157

Please sign in to comment.