Skip to content

[RISCV][GISel] Custom promote s32 G_SHL/ASHR/LSHR on RV64. #115559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 12, 2024
Merged
43 changes: 38 additions & 5 deletions llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,11 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
.lower();

getActionDefinitionsBuilder({G_SHL, G_ASHR, G_LSHR})
.legalFor({{s32, s32}, {sXLen, sXLen}})
.legalFor({{sXLen, sXLen}})
.customFor(ST.is64Bit(), {{s32, s32}})
.widenScalarToNextPow2(0)
.clampScalar(1, s32, sXLen)
.clampScalar(0, s32, sXLen)
.minScalarSameAs(1, 0)
.maxScalarSameAs(1, 0);
.clampScalar(1, sXLen, sXLen)
.clampScalar(0, sXLen, sXLen);

auto &ExtActions =
getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})
Expand Down Expand Up @@ -1166,6 +1165,12 @@ static unsigned getRISCVWOpcode(unsigned Opcode) {
switch (Opcode) {
default:
llvm_unreachable("Unexpected opcode");
case TargetOpcode::G_ASHR:
return RISCV::G_SRAW;
case TargetOpcode::G_LSHR:
return RISCV::G_SRLW;
case TargetOpcode::G_SHL:
return RISCV::G_SLLW;
case TargetOpcode::G_SDIV:
return RISCV::G_DIVW;
case TargetOpcode::G_UDIV:
Expand Down Expand Up @@ -1223,6 +1228,34 @@ bool RISCVLegalizerInfo::legalizeCustom(
return Helper.lower(MI, 0, /* Unused hint type */ LLT()) ==
LegalizerHelper::Legalized;
}
case TargetOpcode::G_ASHR:
case TargetOpcode::G_LSHR:
case TargetOpcode::G_SHL: {
if (getIConstantVRegValWithLookThrough(MI.getOperand(2).getReg(), MRI)) {
// We don't need a custom node for shift by constant. Just widen the
// source and the shift amount.
unsigned ExtOpc = TargetOpcode::G_ANYEXT;
if (MI.getOpcode() == TargetOpcode::G_ASHR)
ExtOpc = TargetOpcode::G_SEXT;
else if (MI.getOpcode() == TargetOpcode::G_LSHR)
ExtOpc = TargetOpcode::G_ZEXT;

Helper.Observer.changingInstr(MI);
Helper.widenScalarSrc(MI, sXLen, 1, ExtOpc);
Helper.widenScalarSrc(MI, sXLen, 2, TargetOpcode::G_ZEXT);
Helper.widenScalarDst(MI, sXLen);
Helper.Observer.changedInstr(MI);
return true;
}

Helper.Observer.changingInstr(MI);
Helper.widenScalarSrc(MI, sXLen, 1, TargetOpcode::G_ANYEXT);
Helper.widenScalarSrc(MI, sXLen, 2, TargetOpcode::G_ANYEXT);
Helper.widenScalarDst(MI, sXLen);
MI.setDesc(MIRBuilder.getTII().get(getRISCVWOpcode(MI.getOpcode())));
Helper.Observer.changedInstr(MI);
return true;
}
case TargetOpcode::G_SDIV:
case TargetOpcode::G_UDIV:
case TargetOpcode::G_UREM:
Expand Down
17 changes: 6 additions & 11 deletions llvm/lib/Target/RISCV/RISCVGISel.td
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def : LdPat<load, LD, PtrVT>;
def : StPat<store, SD, GPR, PtrVT>;
}

let Predicates = [IsRV64] in {
// FIXME: Temporary until i32->i64 zext is no longer legal.
def : Pat <(srl (zext GPR:$rs1), uimm5:$shamt),
(SRLIW GPR:$rs1, uimm5:$shamt)>;
}

//===----------------------------------------------------------------------===//
// RV64 i32 patterns not used by SelectionDAG
//===----------------------------------------------------------------------===//
Expand All @@ -195,17 +201,6 @@ def : Pat<(anyext GPR:$src), (COPY GPR:$src)>;
def : Pat<(sext GPR:$src), (ADDIW GPR:$src, 0)>;
def : Pat<(trunc GPR:$src), (COPY GPR:$src)>;

def : PatGprGpr<shl, SLLW, i32, i32>;
def : PatGprGpr<srl, SRLW, i32, i32>;
def : PatGprGpr<sra, SRAW, i32, i32>;

def : Pat<(i32 (shl GPR:$rs1, uimm5i32:$imm)),
(SLLIW GPR:$rs1, (i64 (as_i64imm $imm)))>;
def : Pat<(i32 (srl GPR:$rs1, uimm5i32:$imm)),
(SRLIW GPR:$rs1, (i64 (as_i64imm $imm)))>;
def : Pat<(i32 (sra GPR:$rs1, uimm5i32:$imm)),
(SRAIW GPR:$rs1, (i64 (as_i64imm $imm)))>;

// Use sext if the sign bit of the input is 0.
def : Pat<(zext_is_sext GPR:$src), (ADDIW GPR:$src, 0)>;
}
Expand Down
24 changes: 24 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrGISel.td
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ class RISCVGenericInstruction : GenericInstruction {
let Namespace = "RISCV";
}

// Pseudo equivalent to a RISCVISD::SRAW.
def G_SRAW : RISCVGenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1, type0:$src2);
let hasSideEffects = false;
}
def : GINodeEquiv<G_SRAW, riscv_sraw>;

// Pseudo equivalent to a RISCVISD::SRLW.
def G_SRLW : RISCVGenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1, type0:$src2);
let hasSideEffects = false;
}
def : GINodeEquiv<G_SRLW, riscv_srlw>;

// Pseudo equivalent to a RISCVISD::SLLW.
def G_SLLW : RISCVGenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1, type0:$src2);
let hasSideEffects = false;
}
def : GINodeEquiv<G_SLLW, riscv_sllw>;

// Pseudo equivalent to a RISCVISD::DIVW.
def G_DIVW : RISCVGenericInstruction {
let OutOperandList = (outs type0:$dst);
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/RISCV/GlobalISel/alu-roundtrip.ll
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ define i32 @slli_i32(i32 %a) {
;
; RV64IM-LABEL: slli_i32:
; RV64IM: # %bb.0: # %entry
; RV64IM-NEXT: slliw a0, a0, 11
; RV64IM-NEXT: slli a0, a0, 11
; RV64IM-NEXT: ret
entry:
%0 = shl i32 %a, 11
Expand Down
93 changes: 43 additions & 50 deletions llvm/test/CodeGen/RISCV/GlobalISel/bitmanip.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
; RUN: llc -mtriple=riscv32 -global-isel -global-isel-abort=1 < %s 2>&1 | FileCheck %s --check-prefixes=RV32
; RUN: llc -mtriple=riscv64 -global-isel -global-isel-abort=1 < %s 2>&1 | FileCheck %s --check-prefixes=RV64

; FIXME: andi a0, a0, 1 is unneeded
define i2 @bitreverse_i2(i2 %x) {
; RV32-LABEL: bitreverse_i2:
; RV32: # %bb.0:
Expand All @@ -18,15 +17,13 @@ define i2 @bitreverse_i2(i2 %x) {
; RV64-NEXT: slli a1, a0, 1
; RV64-NEXT: andi a1, a1, 2
; RV64-NEXT: andi a0, a0, 3
; RV64-NEXT: srliw a0, a0, 1
; RV64-NEXT: andi a0, a0, 1
; RV64-NEXT: srli a0, a0, 1
; RV64-NEXT: or a0, a1, a0
; RV64-NEXT: ret
%rev = call i2 @llvm.bitreverse.i2(i2 %x)
ret i2 %rev
}

; FIXME: andi a0, a0, 1 is unneeded
define i3 @bitreverse_i3(i3 %x) {
; RV32-LABEL: bitreverse_i3:
; RV32: # %bb.0:
Expand All @@ -46,15 +43,13 @@ define i3 @bitreverse_i3(i3 %x) {
; RV64-NEXT: andi a0, a0, 7
; RV64-NEXT: andi a2, a0, 2
; RV64-NEXT: or a1, a1, a2
; RV64-NEXT: srliw a0, a0, 2
; RV64-NEXT: andi a0, a0, 1
; RV64-NEXT: srli a0, a0, 2
; RV64-NEXT: or a0, a1, a0
; RV64-NEXT: ret
%rev = call i3 @llvm.bitreverse.i3(i3 %x)
ret i3 %rev
}

; FIXME: andi a0, a0, 1 is unneeded
define i4 @bitreverse_i4(i4 %x) {
; RV32-LABEL: bitreverse_i4:
; RV32: # %bb.0:
Expand All @@ -79,18 +74,16 @@ define i4 @bitreverse_i4(i4 %x) {
; RV64-NEXT: andi a2, a2, 4
; RV64-NEXT: or a1, a1, a2
; RV64-NEXT: andi a0, a0, 15
; RV64-NEXT: srliw a2, a0, 1
; RV64-NEXT: srli a2, a0, 1
; RV64-NEXT: andi a2, a2, 2
; RV64-NEXT: or a1, a1, a2
; RV64-NEXT: srliw a0, a0, 3
; RV64-NEXT: andi a0, a0, 1
; RV64-NEXT: srli a0, a0, 3
; RV64-NEXT: or a0, a1, a0
; RV64-NEXT: ret
%rev = call i4 @llvm.bitreverse.i4(i4 %x)
ret i4 %rev
}

; FIXME: andi a0, a0, 1 is unneeded
define i7 @bitreverse_i7(i7 %x) {
; RV32-LABEL: bitreverse_i7:
; RV32: # %bb.0:
Expand Down Expand Up @@ -122,20 +115,20 @@ define i7 @bitreverse_i7(i7 %x) {
; RV64-NEXT: slli a2, a0, 4
; RV64-NEXT: andi a2, a2, 32
; RV64-NEXT: or a1, a1, a2
; RV64-NEXT: slli a2, a0, 2
; RV64-NEXT: andi a2, a2, 16
; RV64-NEXT: li a2, 2
; RV64-NEXT: slli a3, a0, 2
; RV64-NEXT: andi a3, a3, 16
; RV64-NEXT: andi a0, a0, 127
; RV64-NEXT: andi a3, a0, 8
; RV64-NEXT: or a2, a2, a3
; RV64-NEXT: andi a4, a0, 8
; RV64-NEXT: or a3, a3, a4
; RV64-NEXT: or a1, a1, a3
; RV64-NEXT: srli a3, a0, 2
; RV64-NEXT: andi a3, a3, 4
; RV64-NEXT: srli a4, a0, 4
; RV64-NEXT: and a2, a4, a2
; RV64-NEXT: or a2, a3, a2
; RV64-NEXT: or a1, a1, a2
; RV64-NEXT: srliw a2, a0, 2
; RV64-NEXT: andi a2, a2, 4
; RV64-NEXT: srliw a3, a0, 4
; RV64-NEXT: andi a3, a3, 2
; RV64-NEXT: or a2, a2, a3
; RV64-NEXT: or a1, a1, a2
; RV64-NEXT: srliw a0, a0, 6
; RV64-NEXT: andi a0, a0, 1
; RV64-NEXT: srli a0, a0, 6
; RV64-NEXT: or a0, a1, a0
; RV64-NEXT: ret
%rev = call i7 @llvm.bitreverse.i7(i7 %x)
Expand Down Expand Up @@ -179,39 +172,39 @@ define i24 @bitreverse_i24(i24 %x) {
;
; RV64-LABEL: bitreverse_i24:
; RV64: # %bb.0:
; RV64-NEXT: slli a1, a0, 16
; RV64-NEXT: lui a2, 4096
; RV64-NEXT: addi a2, a2, -1
; RV64-NEXT: and a0, a0, a2
; RV64-NEXT: srliw a0, a0, 16
; RV64-NEXT: or a0, a0, a1
; RV64-NEXT: lui a1, 65521
; RV64-NEXT: addi a1, a1, -241
; RV64-NEXT: slli a1, a1, 4
; RV64-NEXT: and a3, a1, a2
; RV64-NEXT: lui a1, 4096
; RV64-NEXT: addiw a1, a1, -1
; RV64-NEXT: slli a2, a0, 16
; RV64-NEXT: and a0, a0, a1
; RV64-NEXT: srli a0, a0, 16
; RV64-NEXT: or a0, a0, a2
; RV64-NEXT: lui a2, 65521
; RV64-NEXT: addiw a2, a2, -241
; RV64-NEXT: slli a2, a2, 4
; RV64-NEXT: and a3, a2, a1
; RV64-NEXT: and a3, a0, a3
; RV64-NEXT: srliw a3, a3, 4
; RV64-NEXT: srli a3, a3, 4
; RV64-NEXT: slli a0, a0, 4
; RV64-NEXT: and a0, a0, a1
; RV64-NEXT: and a0, a0, a2
; RV64-NEXT: or a0, a3, a0
; RV64-NEXT: lui a1, 261939
; RV64-NEXT: addi a1, a1, 819
; RV64-NEXT: slli a1, a1, 2
; RV64-NEXT: and a3, a1, a2
; RV64-NEXT: lui a2, 261939
; RV64-NEXT: addiw a2, a2, 819
; RV64-NEXT: slli a2, a2, 2
; RV64-NEXT: and a3, a2, a1
; RV64-NEXT: and a3, a0, a3
; RV64-NEXT: srliw a3, a3, 2
; RV64-NEXT: srli a3, a3, 2
; RV64-NEXT: slli a0, a0, 2
; RV64-NEXT: and a0, a0, a1
; RV64-NEXT: and a0, a0, a2
; RV64-NEXT: or a0, a3, a0
; RV64-NEXT: lui a1, 523605
; RV64-NEXT: addiw a1, a1, 1365
; RV64-NEXT: slli a1, a1, 1
; RV64-NEXT: and a2, a1, a2
; RV64-NEXT: and a2, a0, a2
; RV64-NEXT: srliw a2, a2, 1
; RV64-NEXT: slliw a0, a0, 1
; RV64-NEXT: and a0, a0, a1
; RV64-NEXT: or a0, a2, a0
; RV64-NEXT: lui a2, 523605
; RV64-NEXT: addiw a2, a2, 1365
; RV64-NEXT: slli a2, a2, 1
; RV64-NEXT: and a1, a2, a1
; RV64-NEXT: and a1, a0, a1
; RV64-NEXT: srli a1, a1, 1
; RV64-NEXT: slli a0, a0, 1
; RV64-NEXT: and a0, a0, a2
; RV64-NEXT: or a0, a1, a0
; RV64-NEXT: ret
%rev = call i24 @llvm.bitreverse.i24(i24 %x)
ret i24 %rev
Expand Down
16 changes: 12 additions & 4 deletions llvm/test/CodeGen/RISCV/GlobalISel/combine.ll
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ define i32 @mul_to_shift(i32 %x) {
; RV32-NEXT: slli a0, a0, 2
; RV32-NEXT: ret
;
; RV64-LABEL: mul_to_shift:
; RV64: # %bb.0:
; RV64-NEXT: slliw a0, a0, 2
; RV64-NEXT: ret
; RV64-O0-LABEL: mul_to_shift:
; RV64-O0: # %bb.0:
; RV64-O0-NEXT: li a1, 2
; RV64-O0-NEXT: sll a0, a0, a1
; RV64-O0-NEXT: ret
;
; RV64-OPT-LABEL: mul_to_shift:
; RV64-OPT: # %bb.0:
; RV64-OPT-NEXT: slli a0, a0, 2
; RV64-OPT-NEXT: ret
%a = mul i32 %x, 4
ret i32 %a
}
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; RV64: {{.*}}
8 changes: 4 additions & 4 deletions llvm/test/CodeGen/RISCV/GlobalISel/iabs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ define i8 @abs8(i8 %x) {
;
; RV64I-LABEL: abs8:
; RV64I: # %bb.0:
; RV64I-NEXT: slli a1, a0, 24
; RV64I-NEXT: sraiw a1, a1, 31
; RV64I-NEXT: slli a1, a0, 56
; RV64I-NEXT: srai a1, a1, 63
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: xor a0, a0, a1
; RV64I-NEXT: ret
Expand Down Expand Up @@ -65,8 +65,8 @@ define i16 @abs16(i16 %x) {
;
; RV64I-LABEL: abs16:
; RV64I: # %bb.0:
; RV64I-NEXT: slli a1, a0, 16
; RV64I-NEXT: sraiw a1, a1, 31
; RV64I-NEXT: slli a1, a0, 48
; RV64I-NEXT: srai a1, a1, 63
; RV64I-NEXT: add a0, a0, a1
; RV64I-NEXT: xor a0, a0, a1
; RV64I-NEXT: ret
Expand Down
Loading