Skip to content

Commit c5abad3

Browse files
committed
[RISCV] Codegen support for materializing constants
Differential Revision: https://reviews.llvm.org/D39101 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317684 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 96342eb commit c5abad3

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ def simm21_lsb0 : Operand<XLenVT> {
8686
let DecoderMethod = "decodeSImmOperandAndLsl1<21>";
8787
}
8888

89+
// Standalone (codegen-only) immleaf patterns.
90+
def simm32 : ImmLeaf<XLenVT, [{return isInt<32>(Imm);}]>;
91+
92+
// Extract least significant 12 bits from an immediate value and sign extend
93+
// them.
94+
def LO12Sext : SDNodeXForm<imm, [{
95+
return CurDAG->getTargetConstant(SignExtend64<12>(N->getZExtValue()),
96+
SDLoc(N), N->getValueType(0));
97+
}]>;
98+
99+
// Extract the most significant 20 bits from an immediate value. Add 1 if bit
100+
// 11 is 1, to compensate for the low 12 bits in the matching immediate addi
101+
// or ld/st being negative.
102+
def HI20 : SDNodeXForm<imm, [{
103+
return CurDAG->getTargetConstant(((N->getZExtValue()+0x800) >> 12) & 0xfffff,
104+
SDLoc(N), N->getValueType(0));
105+
}]>;
106+
89107
//===----------------------------------------------------------------------===//
90108
// Instruction Class Templates
91109
//===----------------------------------------------------------------------===//
@@ -257,6 +275,12 @@ class PatGprUimm5<SDPatternOperator OpNode, RVInstIShift Inst>
257275
: Pat<(OpNode GPR:$rs1, uimm5:$shamt),
258276
(Inst GPR:$rs1, uimm5:$shamt)>;
259277

278+
/// Immediates
279+
280+
def : Pat<(simm12:$imm), (ADDI X0, simm12:$imm)>;
281+
// TODO: Add a pattern for immediates with all zeroes in the lower 12 bits.
282+
def : Pat<(simm32:$imm), (ADDI (LUI (HI20 imm:$imm)), (LO12Sext imm:$imm))>;
283+
260284
/// Simple arithmetic operations
261285

262286
def : PatGprGpr<add, ADD>;

test/CodeGen/RISCV/alu32.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ define i32 @addi(i32 %a) nounwind {
77
; RV32I-LABEL: addi:
88
; RV32I: addi a0, a0, 1
99
; RV32I: jalr zero, ra, 0
10-
; TODO: check support for materialising larger constants
1110
%1 = add i32 %a, 1
1211
ret i32 %1
1312
}

test/CodeGen/RISCV/imm.ll

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s -check-prefix=RV32I
4+
5+
; Materializing constants
6+
7+
define i32 @zero() nounwind {
8+
; RV32I-LABEL: zero:
9+
; RV32I: # BB#0:
10+
; RV32I-NEXT: addi a0, zero, 0
11+
; RV32I-NEXT: jalr zero, ra, 0
12+
ret i32 0
13+
}
14+
15+
define i32 @pos_small() nounwind {
16+
; RV32I-LABEL: pos_small:
17+
; RV32I: # BB#0:
18+
; RV32I-NEXT: addi a0, zero, 2047
19+
; RV32I-NEXT: jalr zero, ra, 0
20+
ret i32 2047
21+
}
22+
23+
define i32 @neg_small() nounwind {
24+
; RV32I-LABEL: neg_small:
25+
; RV32I: # BB#0:
26+
; RV32I-NEXT: addi a0, zero, -2048
27+
; RV32I-NEXT: jalr zero, ra, 0
28+
ret i32 -2048
29+
}
30+
31+
define i32 @pos_i32() nounwind {
32+
; RV32I-LABEL: pos_i32:
33+
; RV32I: # BB#0:
34+
; RV32I-NEXT: lui a0, 423811
35+
; RV32I-NEXT: addi a0, a0, -1297
36+
; RV32I-NEXT: jalr zero, ra, 0
37+
ret i32 1735928559
38+
}
39+
40+
define i32 @neg_i32() nounwind {
41+
; RV32I-LABEL: neg_i32:
42+
; RV32I: # BB#0:
43+
; RV32I-NEXT: lui a0, 912092
44+
; RV32I-NEXT: addi a0, a0, -273
45+
; RV32I-NEXT: jalr zero, ra, 0
46+
ret i32 -559038737
47+
}

0 commit comments

Comments
 (0)