Skip to content

Commit f994611

Browse files
toppercmemfrob
authored andcommitted
[RISCV] Improve 64-bit integer constant materialization for more cases.
For positive constants we try shifting left to remove leading zeros and fill the bottom bits with 1s. We then materialize that constant shift it right. This patch adds a new strategy to try filling the bottom bits with zeros instead. This catches some additional cases.
1 parent 91b4e6c commit f994611

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ InstSeq generateInstSeq(int64_t Val, bool IsRV64) {
9494
generateInstSeqImpl(Val, IsRV64, TmpSeq);
9595
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, ShiftAmount));
9696

97+
// Keep the new sequence if it is an improvement.
98+
if (TmpSeq.size() < Res.size())
99+
Res = TmpSeq;
100+
101+
// Some cases can benefit from filling the lower bits with zeros instead.
102+
Val &= maskTrailingZeros<uint64_t>(ShiftAmount);
103+
TmpSeq.clear();
104+
generateInstSeqImpl(Val, IsRV64, TmpSeq);
105+
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, ShiftAmount));
106+
97107
// Keep the new sequence if it is an improvement.
98108
if (TmpSeq.size() < Res.size())
99109
Res = TmpSeq;

llvm/test/CodeGen/RISCV/calling-conv-half.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,9 @@ define i32 @caller_half_on_stack() nounwind {
347347
; RV64IF: # %bb.0:
348348
; RV64IF-NEXT: addi sp, sp, -16
349349
; RV64IF-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
350-
; RV64IF-NEXT: lui a0, 256
351-
; RV64IF-NEXT: addiw a0, a0, -11
352-
; RV64IF-NEXT: slli a0, a0, 12
353-
; RV64IF-NEXT: addi t0, a0, -1792
350+
; RV64IF-NEXT: addi a0, zero, -183
351+
; RV64IF-NEXT: slli a0, a0, 40
352+
; RV64IF-NEXT: srli t0, a0, 32
354353
; RV64IF-NEXT: addi a0, zero, 1
355354
; RV64IF-NEXT: addi a1, zero, 2
356355
; RV64IF-NEXT: addi a2, zero, 3

llvm/test/CodeGen/RISCV/imm.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ define i64 @imm_right_shifted_lui_1() nounwind {
375375
;
376376
; RV64I-LABEL: imm_right_shifted_lui_1:
377377
; RV64I: # %bb.0:
378-
; RV64I-NEXT: lui a0, 983072
379-
; RV64I-NEXT: addiw a0, a0, -1
378+
; RV64I-NEXT: lui a0, 983056
380379
; RV64I-NEXT: srli a0, a0, 16
381380
; RV64I-NEXT: ret
382381
ret i64 281474976706561 ; 0xFFFF_FFFF_F001

llvm/test/MC/RISCV/rv64i-aliases-valid.s

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ li x5, 0x100004000
118118
# CHECK-EXPAND-NEXT: addiw t1, t1, 1
119119
# CHECK-EXPAND-NEXT: slli t1, t1, 32
120120
li x6, 0x100100000000
121-
# CHECK-EXPAND: lui t2, 983072
122-
# CHECK-EXPAND-NEXT: addiw t2, t2, -1
121+
# CHECK-EXPAND: lui t2, 983056
123122
# CHECK-EXPAND-NEXT: srli t2, t2, 16
124123
li x7, 0xFFFFFFFFF001
125124
# CHECK-EXPAND: lui s0, 65536

0 commit comments

Comments
 (0)