Skip to content

Commit d61b40e

Browse files
committed
[RISCV] Improve 64-bit integer materialization for some cases.
This adds a new integer materialization strategy mainly targeted at 64-bit constants like 0xffffffff where there are 32 or more trailing ones with leading zeros. We can materialize these by using an addi -1 and srli to restore the leading zeros. This matches what gcc does. I haven't limited to just these cases though. The implementation here takes the constant, shifts out all the leading zeros and shifts ones into the LSBs, creates the new sequence, adds an srli, and checks if this is shorter than our original strategy. I've separated the recursive portion into a standalone function so I could append the new strategy outside of the recursion. Since external users are no longer using the recursive function, I've cleaned up the external interface to return the sequence instead of taking a vector by reference. Reviewed By: asb Differential Revision: https://reviews.llvm.org/D98821
1 parent 9783437 commit d61b40e

23 files changed

Lines changed: 250 additions & 248 deletions

‎llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,8 +2201,7 @@ void RISCVAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
22012201

22022202
void RISCVAsmParser::emitLoadImm(MCRegister DestReg, int64_t Value,
22032203
MCStreamer &Out) {
2204-
RISCVMatInt::InstSeq Seq;
2205-
RISCVMatInt::generateInstSeq(Value, isRV64(), Seq);
2204+
RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Value, isRV64());
22062205

22072206
MCRegister SrcReg = RISCV::X0;
22082207
for (RISCVMatInt::Inst &Inst : Seq) {

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include "MCTargetDesc/RISCVMCTargetDesc.h"
1111
#include "llvm/ADT/APInt.h"
1212
#include "llvm/Support/MathExtras.h"
13+
using namespace llvm;
1314

14-
namespace llvm {
15-
16-
namespace RISCVMatInt {
17-
void generateInstSeq(int64_t Val, bool IsRV64, InstSeq &Res) {
15+
// Recursively generate a sequence for materializing an integer.
16+
static void generateInstSeqImpl(int64_t Val, bool IsRV64,
17+
RISCVMatInt::InstSeq &Res) {
1818
if (isInt<32>(Val)) {
1919
// Depending on the active bits in the immediate Value v, the following
2020
// instruction sequences are emitted:
@@ -27,11 +27,11 @@ void generateInstSeq(int64_t Val, bool IsRV64, InstSeq &Res) {
2727
int64_t Lo12 = SignExtend64<12>(Val);
2828

2929
if (Hi20)
30-
Res.push_back(Inst(RISCV::LUI, Hi20));
30+
Res.push_back(RISCVMatInt::Inst(RISCV::LUI, Hi20));
3131

3232
if (Lo12 || Hi20 == 0) {
3333
unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI;
34-
Res.push_back(Inst(AddiOpc, Lo12));
34+
Res.push_back(RISCVMatInt::Inst(AddiOpc, Lo12));
3535
}
3636
return;
3737
}
@@ -66,11 +66,40 @@ void generateInstSeq(int64_t Val, bool IsRV64, InstSeq &Res) {
6666
int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52);
6767
Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount);
6868

69-
generateInstSeq(Hi52, IsRV64, Res);
69+
generateInstSeqImpl(Hi52, IsRV64, Res);
7070

71-
Res.push_back(Inst(RISCV::SLLI, ShiftAmount));
71+
Res.push_back(RISCVMatInt::Inst(RISCV::SLLI, ShiftAmount));
7272
if (Lo12)
73-
Res.push_back(Inst(RISCV::ADDI, Lo12));
73+
Res.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12));
74+
}
75+
76+
namespace llvm {
77+
namespace RISCVMatInt {
78+
InstSeq generateInstSeq(int64_t Val, bool IsRV64) {
79+
RISCVMatInt::InstSeq Res;
80+
generateInstSeqImpl(Val, IsRV64, Res);
81+
82+
// If the constant is positive we might be able to generate a shifted constant
83+
// with no leading zeros and use a final SRLI to restore them.
84+
if (Val > 0 && Res.size() > 2) {
85+
assert(IsRV64 && "Expected RV32 to only need 2 instructions");
86+
unsigned ShiftAmount = countLeadingZeros((uint64_t)Val);
87+
Val <<= ShiftAmount;
88+
// Fill in the bits that will be shifted out with 1s. An example where this
89+
// helps is trailing one masks with 32 or more ones. This will generate
90+
// ADDI -1 and an SRLI.
91+
Val |= maskTrailingOnes<uint64_t>(ShiftAmount);
92+
93+
RISCVMatInt::InstSeq TmpSeq;
94+
generateInstSeqImpl(Val, IsRV64, TmpSeq);
95+
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, ShiftAmount));
96+
97+
// Keep the new sequence if it is an improvement.
98+
if (TmpSeq.size() < Res.size())
99+
Res = TmpSeq;
100+
}
101+
102+
return Res;
74103
}
75104

76105
int getIntMatCost(const APInt &Val, unsigned Size, bool IsRV64) {
@@ -81,8 +110,7 @@ int getIntMatCost(const APInt &Val, unsigned Size, bool IsRV64) {
81110
int Cost = 0;
82111
for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) {
83112
APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize);
84-
InstSeq MatSeq;
85-
generateInstSeq(Chunk.getSExtValue(), IsRV64, MatSeq);
113+
InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), IsRV64);
86114
Cost += MatSeq.size();
87115
}
88116
return std::max(1, Cost);

‎llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ struct Inst {
2525
using InstSeq = SmallVector<Inst, 8>;
2626

2727
// Helper to generate an instruction sequence that will materialise the given
28-
// immediate value into a register. A sequence of instructions represented by
29-
// a simple struct produced rather than directly emitting the instructions in
28+
// immediate value into a register. A sequence of instructions represented by a
29+
// simple struct is produced rather than directly emitting the instructions in
3030
// order to allow this helper to be used from both the MC layer and during
3131
// instruction selection.
32-
void generateInstSeq(int64_t Val, bool IsRV64, InstSeq &Res);
32+
InstSeq generateInstSeq(int64_t Val, bool IsRV64);
3333

3434
// Helper to estimate the number of instructions required to materialise the
3535
// given immediate value into a register. This estimate does not account for

‎llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ void RISCVDAGToDAGISel::PostprocessISelDAG() {
4646

4747
static SDNode *selectImm(SelectionDAG *CurDAG, const SDLoc &DL, int64_t Imm,
4848
MVT XLenVT) {
49-
RISCVMatInt::InstSeq Seq;
50-
RISCVMatInt::generateInstSeq(Imm, XLenVT == MVT::i64, Seq);
49+
RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Imm, XLenVT == MVT::i64);
5150

5251
SDNode *Result = nullptr;
5352
SDValue SrcReg = CurDAG->getRegister(RISCV::X0, XLenVT);

‎llvm/lib/Target/RISCV/RISCVInstrInfo.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
354354
if (!IsRV64 && !isInt<32>(Val))
355355
report_fatal_error("Should only materialize 32-bit constants for RV32");
356356

357-
RISCVMatInt::InstSeq Seq;
358-
RISCVMatInt::generateInstSeq(Val, IsRV64, Seq);
357+
RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Val, IsRV64);
359358
assert(Seq.size() > 0);
360359

361360
for (RISCVMatInt::Inst &Inst : Seq) {

‎llvm/test/CodeGen/RISCV/copysign-casts.ll‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ define double @fold_promote_d_s(double %a, float %b) nounwind {
3939
; RV64I-LABEL: fold_promote_d_s:
4040
; RV64I: # %bb.0:
4141
; RV64I-NEXT: addi a2, zero, -1
42-
; RV64I-NEXT: slli a2, a2, 63
43-
; RV64I-NEXT: addi a2, a2, -1
42+
; RV64I-NEXT: srli a2, a2, 1
4443
; RV64I-NEXT: and a0, a0, a2
4544
; RV64I-NEXT: lui a2, 524288
4645
; RV64I-NEXT: and a1, a1, a2
@@ -111,8 +110,7 @@ define double @fold_promote_d_h(double %a, half %b) nounwind {
111110
; RV64I-LABEL: fold_promote_d_h:
112111
; RV64I: # %bb.0:
113112
; RV64I-NEXT: addi a2, zero, -1
114-
; RV64I-NEXT: slli a2, a2, 63
115-
; RV64I-NEXT: addi a2, a2, -1
113+
; RV64I-NEXT: srli a2, a2, 1
116114
; RV64I-NEXT: and a0, a0, a2
117115
; RV64I-NEXT: lui a2, 8
118116
; RV64I-NEXT: and a1, a1, a2

‎llvm/test/CodeGen/RISCV/double-bitmanip-dagcombines.ll‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ define double @fabs(double %a) nounwind {
6767
; RV64I-LABEL: fabs:
6868
; RV64I: # %bb.0:
6969
; RV64I-NEXT: addi a1, zero, -1
70-
; RV64I-NEXT: slli a1, a1, 63
71-
; RV64I-NEXT: addi a1, a1, -1
70+
; RV64I-NEXT: srli a1, a1, 1
7271
; RV64I-NEXT: and a0, a0, a1
7372
; RV64I-NEXT: ret
7473
;
7574
; RV64IFD-LABEL: fabs:
7675
; RV64IFD: # %bb.0:
7776
; RV64IFD-NEXT: addi a1, zero, -1
78-
; RV64IFD-NEXT: slli a1, a1, 63
79-
; RV64IFD-NEXT: addi a1, a1, -1
77+
; RV64IFD-NEXT: srli a1, a1, 1
8078
; RV64IFD-NEXT: and a0, a0, a1
8179
; RV64IFD-NEXT: ret
8280
%1 = call double @llvm.fabs.f64(double %a)
@@ -120,9 +118,9 @@ define double @fcopysign_fneg(double %a, double %b) nounwind {
120118
; RV64I: # %bb.0:
121119
; RV64I-NEXT: not a1, a1
122120
; RV64I-NEXT: addi a2, zero, -1
123-
; RV64I-NEXT: slli a2, a2, 63
124-
; RV64I-NEXT: and a1, a1, a2
125-
; RV64I-NEXT: addi a2, a2, -1
121+
; RV64I-NEXT: slli a3, a2, 63
122+
; RV64I-NEXT: and a1, a1, a3
123+
; RV64I-NEXT: srli a2, a2, 1
126124
; RV64I-NEXT: and a0, a0, a2
127125
; RV64I-NEXT: or a0, a0, a1
128126
; RV64I-NEXT: ret

‎llvm/test/CodeGen/RISCV/double-intrinsics.ll‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ define double @fabs_f64(double %a) nounwind {
385385
; RV64IFD-LABEL: fabs_f64:
386386
; RV64IFD: # %bb.0:
387387
; RV64IFD-NEXT: addi a1, zero, -1
388-
; RV64IFD-NEXT: slli a1, a1, 63
389-
; RV64IFD-NEXT: addi a1, a1, -1
388+
; RV64IFD-NEXT: srli a1, a1, 1
390389
; RV64IFD-NEXT: and a0, a0, a1
391390
; RV64IFD-NEXT: ret
392391
%1 = call double @llvm.fabs.f64(double %a)

‎llvm/test/CodeGen/RISCV/float-bit-preserving-dagcombines.ll‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ define double @bitcast_double_and(double %a1, double %a2) nounwind {
111111
; RV64F-NEXT: mv s0, a0
112112
; RV64F-NEXT: call __adddf3@plt
113113
; RV64F-NEXT: addi a1, zero, -1
114-
; RV64F-NEXT: slli a1, a1, 63
115-
; RV64F-NEXT: addi a1, a1, -1
114+
; RV64F-NEXT: srli a1, a1, 1
116115
; RV64F-NEXT: and a1, a0, a1
117116
; RV64F-NEXT: mv a0, s0
118117
; RV64F-NEXT: call __adddf3@plt

‎llvm/test/CodeGen/RISCV/imm.ll‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ define i64 @imm64_2() nounwind {
183183
;
184184
; RV64I-LABEL: imm64_2:
185185
; RV64I: # %bb.0:
186-
; RV64I-NEXT: addi a0, zero, 1
187-
; RV64I-NEXT: slli a0, a0, 32
188-
; RV64I-NEXT: addi a0, a0, -1
186+
; RV64I-NEXT: addi a0, zero, -1
187+
; RV64I-NEXT: srli a0, a0, 32
189188
; RV64I-NEXT: ret
190189
ret i64 4294967295 ; 0xFFFF_FFFF
191190
}
@@ -376,11 +375,9 @@ define i64 @imm_right_shifted_lui_1() nounwind {
376375
;
377376
; RV64I-LABEL: imm_right_shifted_lui_1:
378377
; RV64I: # %bb.0:
379-
; RV64I-NEXT: addi a0, zero, 1
380-
; RV64I-NEXT: slli a0, a0, 36
381-
; RV64I-NEXT: addi a0, a0, -1
382-
; RV64I-NEXT: slli a0, a0, 12
383-
; RV64I-NEXT: addi a0, a0, 1
378+
; RV64I-NEXT: lui a0, 983072
379+
; RV64I-NEXT: addiw a0, a0, -1
380+
; RV64I-NEXT: srli a0, a0, 16
384381
; RV64I-NEXT: ret
385382
ret i64 281474976706561 ; 0xFFFF_FFFF_F001
386383
}

0 commit comments

Comments
 (0)