Skip to content

Commit 9a64523

Browse files
committed
[RISCV] Add more Zbs patterns for -riscv-experimental-rv64-legal-i32.
1 parent 1fad78b commit 9a64523

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,11 @@ def : Pat<(i32 (and (shiftop<srl> GPR:$rs1, (XLenVT GPR:$rs2)), 1)),
899899
def : Pat<(i64 (and (anyext (i32 (shiftop<srl> GPR:$rs1, (XLenVT GPR:$rs2)))), 1)),
900900
(BEXT GPR:$rs1, GPR:$rs2)>;
901901

902+
def : Pat<(i32 (shiftop<shl> 1, (XLenVT GPR:$rs2))),
903+
(BSET (XLenVT X0), GPR:$rs2)>;
904+
def : Pat<(i32 (not (shiftop<shl> -1, (XLenVT GPR:$rs2)))),
905+
(ADDI (BSET (XLenVT X0), GPR:$rs2), -1)>;
906+
902907
def : Pat<(i32 (and (srl GPR:$rs1, uimm5:$shamt), (i32 1))),
903908
(BEXTI GPR:$rs1, uimm5:$shamt)>;
904909

llvm/test/CodeGen/RISCV/rv64-legal-i32/rv64zbs.ll

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,17 @@ define signext i32 @bset_i32_load(ptr %p, i32 signext %b) nounwind {
169169

170170
; We can use bsetw for 1 << x by setting the first source to zero.
171171
define signext i32 @bset_i32_zero(i32 signext %a) nounwind {
172-
; CHECK-LABEL: bset_i32_zero:
173-
; CHECK: # %bb.0:
174-
; CHECK-NEXT: li a1, 1
175-
; CHECK-NEXT: sllw a0, a1, a0
176-
; CHECK-NEXT: ret
172+
; RV64I-LABEL: bset_i32_zero:
173+
; RV64I: # %bb.0:
174+
; RV64I-NEXT: li a1, 1
175+
; RV64I-NEXT: sllw a0, a1, a0
176+
; RV64I-NEXT: ret
177+
;
178+
; RV64ZBS-LABEL: bset_i32_zero:
179+
; RV64ZBS: # %bb.0:
180+
; RV64ZBS-NEXT: bset a0, zero, a0
181+
; RV64ZBS-NEXT: sext.w a0, a0
182+
; RV64ZBS-NEXT: ret
177183
%shl = shl i32 1, %a
178184
ret i32 %shl
179185
}
@@ -1076,3 +1082,78 @@ define i64 @or_i64_66901(i64 %a) nounwind {
10761082
%or = or i64 %a, 66901
10771083
ret i64 %or
10781084
}
1085+
1086+
define signext i32 @bset_trailing_ones_i32_mask(i32 signext %a) nounwind {
1087+
; RV64I-LABEL: bset_trailing_ones_i32_mask:
1088+
; RV64I: # %bb.0:
1089+
; RV64I-NEXT: li a1, -1
1090+
; RV64I-NEXT: sllw a0, a1, a0
1091+
; RV64I-NEXT: not a0, a0
1092+
; RV64I-NEXT: ret
1093+
;
1094+
; RV64ZBS-LABEL: bset_trailing_ones_i32_mask:
1095+
; RV64ZBS: # %bb.0:
1096+
; RV64ZBS-NEXT: andi a0, a0, 31
1097+
; RV64ZBS-NEXT: bset a0, zero, a0
1098+
; RV64ZBS-NEXT: addiw a0, a0, -1
1099+
; RV64ZBS-NEXT: ret
1100+
%and = and i32 %a, 31
1101+
%shift = shl nsw i32 -1, %and
1102+
%not = xor i32 %shift, -1
1103+
ret i32 %not
1104+
}
1105+
1106+
define signext i32 @bset_trailing_ones_i32_no_mask(i32 signext %a) nounwind {
1107+
; RV64I-LABEL: bset_trailing_ones_i32_no_mask:
1108+
; RV64I: # %bb.0:
1109+
; RV64I-NEXT: li a1, -1
1110+
; RV64I-NEXT: sllw a0, a1, a0
1111+
; RV64I-NEXT: not a0, a0
1112+
; RV64I-NEXT: ret
1113+
;
1114+
; RV64ZBS-LABEL: bset_trailing_ones_i32_no_mask:
1115+
; RV64ZBS: # %bb.0:
1116+
; RV64ZBS-NEXT: bset a0, zero, a0
1117+
; RV64ZBS-NEXT: addiw a0, a0, -1
1118+
; RV64ZBS-NEXT: ret
1119+
%shift = shl nsw i32 -1, %a
1120+
%not = xor i32 %shift, -1
1121+
ret i32 %not
1122+
}
1123+
1124+
define signext i64 @bset_trailing_ones_i64_mask(i64 signext %a) nounwind {
1125+
; RV64I-LABEL: bset_trailing_ones_i64_mask:
1126+
; RV64I: # %bb.0:
1127+
; RV64I-NEXT: li a1, -1
1128+
; RV64I-NEXT: sll a0, a1, a0
1129+
; RV64I-NEXT: not a0, a0
1130+
; RV64I-NEXT: ret
1131+
;
1132+
; RV64ZBS-LABEL: bset_trailing_ones_i64_mask:
1133+
; RV64ZBS: # %bb.0:
1134+
; RV64ZBS-NEXT: bset a0, zero, a0
1135+
; RV64ZBS-NEXT: addi a0, a0, -1
1136+
; RV64ZBS-NEXT: ret
1137+
%and = and i64 %a, 63
1138+
%shift = shl nsw i64 -1, %and
1139+
%not = xor i64 %shift, -1
1140+
ret i64 %not
1141+
}
1142+
1143+
define signext i64 @bset_trailing_ones_i64_no_mask(i64 signext %a) nounwind {
1144+
; RV64I-LABEL: bset_trailing_ones_i64_no_mask:
1145+
; RV64I: # %bb.0:
1146+
; RV64I-NEXT: li a1, -1
1147+
; RV64I-NEXT: sll a0, a1, a0
1148+
; RV64I-NEXT: not a0, a0
1149+
; RV64I-NEXT: ret
1150+
;
1151+
; RV64ZBS-LABEL: bset_trailing_ones_i64_no_mask:
1152+
; RV64ZBS: # %bb.0:
1153+
; RV64ZBS-NEXT: bset a0, zero, a0
1154+
; RV64ZBS-NEXT: addi a0, a0, -1
1155+
; RV64ZBS-NEXT: ret
1156+
%shift = shl nsw i64 -1, %a
1157+
%not = xor i64 %shift, -1
1158+
ret i64 %not
1159+
}

0 commit comments

Comments
 (0)