Skip to content

Commit c4806db

Browse files
authored
[RISCV] Fold LI 1 / SLLI into BSETI during i64 materialization (#142348)
My first approach was to avoid emitting LI 1 / SLLI in the first place. Unfortunately, that favors BSETI C / ADDI -1 over LI -1 / SRLI 64-C even though the latter has both instructions compressible. This is because the code assumes in several places that a two-instruction sequence (here: BSETI / ADDI) cannot be improved. Another possible approach would be to keep LI 1 / SLLI if it is to be later replaced with SRLI. This would be harder to grasp than eventually patching LI 1 / SLLI with BSETI.
1 parent 9f7f4ac commit c4806db

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI) {
353353
} while (Hi != 0);
354354
Res = TmpSeq;
355355
}
356+
357+
// Fold LI 1 + SLLI into BSETI.
358+
if (Res[0].getOpcode() == RISCV::ADDI && Res[0].getImm() == 1 &&
359+
Res[1].getOpcode() == RISCV::SLLI) {
360+
Res.erase(Res.begin()); // Remove ADDI.
361+
Res.front() = Inst(RISCV::BSETI, Res.front().getImm()); // Patch SLLI.
362+
}
356363
}
357364

358365
// Perform optimization with BCLRI in the Zbs extension.

llvm/test/CodeGen/RISCV/imm.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4674,8 +4674,7 @@ define i64 @imm64_0xFFFFFFFF0() {
46744674
;
46754675
; RV64IZBS-LABEL: imm64_0xFFFFFFFF0:
46764676
; RV64IZBS: # %bb.0:
4677-
; RV64IZBS-NEXT: li a0, 1
4678-
; RV64IZBS-NEXT: slli a0, a0, 36
4677+
; RV64IZBS-NEXT: bseti a0, zero, 36
46794678
; RV64IZBS-NEXT: addi a0, a0, -16
46804679
; RV64IZBS-NEXT: ret
46814680
;
@@ -4737,8 +4736,7 @@ define i64 @imm64_0x1FFFFFF08() {
47374736
;
47384737
; RV64IZBS-LABEL: imm64_0x1FFFFFF08:
47394738
; RV64IZBS: # %bb.0:
4740-
; RV64IZBS-NEXT: li a0, 1
4741-
; RV64IZBS-NEXT: slli a0, a0, 33
4739+
; RV64IZBS-NEXT: bseti a0, zero, 33
47424740
; RV64IZBS-NEXT: addi a0, a0, -248
47434741
; RV64IZBS-NEXT: ret
47444742
;

llvm/test/CodeGen/RISCV/zbb-logic-neg-imm.ll

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -397,18 +397,27 @@ define i64 @and_or_or(i64 %x, i64 %y) {
397397
; RV32-NEXT: and a1, a1, a3
398398
; RV32-NEXT: ret
399399
;
400-
; RV64-LABEL: and_or_or:
401-
; RV64: # %bb.0:
402-
; RV64-NEXT: li a2, -1
403-
; RV64-NEXT: slli a2, a2, 33
404-
; RV64-NEXT: addi a2, a2, 1
405-
; RV64-NEXT: or a0, a0, a2
406-
; RV64-NEXT: li a2, 1
407-
; RV64-NEXT: slli a2, a2, 33
408-
; RV64-NEXT: addi a2, a2, -2
409-
; RV64-NEXT: or a1, a1, a2
410-
; RV64-NEXT: and a0, a0, a1
411-
; RV64-NEXT: ret
400+
; NOZBS64-LABEL: and_or_or:
401+
; NOZBS64: # %bb.0:
402+
; NOZBS64-NEXT: li a2, -1
403+
; NOZBS64-NEXT: slli a2, a2, 33
404+
; NOZBS64-NEXT: addi a2, a2, 1
405+
; NOZBS64-NEXT: or a0, a0, a2
406+
; NOZBS64-NEXT: li a2, 1
407+
; NOZBS64-NEXT: slli a2, a2, 33
408+
; NOZBS64-NEXT: addi a2, a2, -2
409+
; NOZBS64-NEXT: or a1, a1, a2
410+
; NOZBS64-NEXT: and a0, a0, a1
411+
; NOZBS64-NEXT: ret
412+
;
413+
; ZBS64-LABEL: and_or_or:
414+
; ZBS64: # %bb.0:
415+
; ZBS64-NEXT: bseti a2, zero, 33
416+
; ZBS64-NEXT: addi a2, a2, -2
417+
; ZBS64-NEXT: orn a0, a0, a2
418+
; ZBS64-NEXT: or a1, a1, a2
419+
; ZBS64-NEXT: and a0, a0, a1
420+
; ZBS64-NEXT: ret
412421
%a = or i64 %x, -8589934591
413422
%b = or i64 %y, 8589934590
414423
%c = and i64 %a, %b

0 commit comments

Comments
 (0)