Skip to content

Commit

Permalink
[RISCV] Optimize (and (shl GPR:, uimm5:), 0xffffffff) to use 2 shifts…
Browse files Browse the repository at this point in the history
… instead of 3.

The and would normally become SLLI+SRLI, giving us 2 SLLI+SRLI. We
can detect this and combine the 2 SLLIs into 1.
  • Loading branch information
topperc committed Mar 26, 2021
1 parent 5a18c57 commit 8f62a80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 11 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ def ImmSub32 : SDNodeXForm<imm, [{
N->getValueType(0));
}]>;

// Return an immediate value plus 32.
def ImmPlus32 : SDNodeXForm<imm, [{
return CurDAG->getTargetConstant(N->getSExtValue() + 32, SDLoc(N),
N->getValueType(0));
}]>;

// Return an immediate subtracted from XLen.
def ImmSubFromXLen : SDNodeXForm<imm, [{
uint64_t XLen = Subtarget->getXLen();
Expand Down Expand Up @@ -1141,6 +1147,11 @@ def : Pat<(i64 (shl (and GPR:$rs1, 0xffffffff), uimm5:$shamt)),
// shl/and can appear in the other order too.
def : Pat<(i64 (SLLIUWPat GPR:$rs1, uimm5:$shamt)),
(SRLI (SLLI GPR:$rs1, 32), (ImmSubFrom32 uimm5:$shamt))>;

// If we're shifting a value left by 0-31 bits, and then masking to 32-bits,
// use 2 shifts instead of 3.
def : Pat<(i64 (and (shl GPR:$rs1, uimm5:$shamt), 0xffffffff)),
(SRLI (SLLI GPR:$rs1, (ImmPlus32 uimm5:$shamt)), 32)>;
}

let Predicates = [IsRV64] in {
Expand Down
9 changes: 3 additions & 6 deletions llvm/test/CodeGen/RISCV/rv64i-exhaustive-w-insts.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1478,8 +1478,7 @@ define signext i32 @sext_slliw_zext(i32 zeroext %a) nounwind {
define zeroext i32 @zext_slliw_aext(i32 %a) nounwind {
; RV64I-LABEL: zext_slliw_aext:
; RV64I: # %bb.0:
; RV64I-NEXT: slli a0, a0, 7
; RV64I-NEXT: slli a0, a0, 32
; RV64I-NEXT: slli a0, a0, 39
; RV64I-NEXT: srli a0, a0, 32
; RV64I-NEXT: ret
%1 = shl i32 %a, 7
Expand All @@ -1489,8 +1488,7 @@ define zeroext i32 @zext_slliw_aext(i32 %a) nounwind {
define zeroext i32 @zext_slliw_sext(i32 signext %a) nounwind {
; RV64I-LABEL: zext_slliw_sext:
; RV64I: # %bb.0:
; RV64I-NEXT: slli a0, a0, 8
; RV64I-NEXT: slli a0, a0, 32
; RV64I-NEXT: slli a0, a0, 40
; RV64I-NEXT: srli a0, a0, 32
; RV64I-NEXT: ret
%1 = shl i32 %a, 8
Expand All @@ -1500,8 +1498,7 @@ define zeroext i32 @zext_slliw_sext(i32 signext %a) nounwind {
define zeroext i32 @zext_slliw_zext(i32 zeroext %a) nounwind {
; RV64I-LABEL: zext_slliw_zext:
; RV64I: # %bb.0:
; RV64I-NEXT: slli a0, a0, 9
; RV64I-NEXT: slli a0, a0, 32
; RV64I-NEXT: slli a0, a0, 41
; RV64I-NEXT: srli a0, a0, 32
; RV64I-NEXT: ret
%1 = shl i32 %a, 9
Expand Down

0 comments on commit 8f62a80

Please sign in to comment.