1af732203SDimitry Andric //===- RISCVMatInt.cpp - Immediate materialisation -------------*- C++ -*--===//
2af732203SDimitry Andric //
3af732203SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4af732203SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5af732203SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6af732203SDimitry Andric //
7af732203SDimitry Andric //===----------------------------------------------------------------------===//
8af732203SDimitry Andric
9af732203SDimitry Andric #include "RISCVMatInt.h"
10af732203SDimitry Andric #include "MCTargetDesc/RISCVMCTargetDesc.h"
11af732203SDimitry Andric #include "llvm/ADT/APInt.h"
12af732203SDimitry Andric #include "llvm/Support/MathExtras.h"
13*5f7ddb14SDimitry Andric using namespace llvm;
14af732203SDimitry Andric
getInstSeqCost(RISCVMatInt::InstSeq & Res,bool HasRVC)15*5f7ddb14SDimitry Andric static int getInstSeqCost(RISCVMatInt::InstSeq &Res, bool HasRVC) {
16*5f7ddb14SDimitry Andric if (!HasRVC)
17*5f7ddb14SDimitry Andric return Res.size();
18af732203SDimitry Andric
19*5f7ddb14SDimitry Andric int Cost = 0;
20*5f7ddb14SDimitry Andric for (auto Instr : Res) {
21*5f7ddb14SDimitry Andric bool Compressed;
22*5f7ddb14SDimitry Andric switch (Instr.Opc) {
23*5f7ddb14SDimitry Andric default: llvm_unreachable("Unexpected opcode");
24*5f7ddb14SDimitry Andric case RISCV::SLLI:
25*5f7ddb14SDimitry Andric case RISCV::SRLI:
26*5f7ddb14SDimitry Andric Compressed = true;
27*5f7ddb14SDimitry Andric break;
28*5f7ddb14SDimitry Andric case RISCV::ADDI:
29*5f7ddb14SDimitry Andric case RISCV::ADDIW:
30*5f7ddb14SDimitry Andric case RISCV::LUI:
31*5f7ddb14SDimitry Andric Compressed = isInt<6>(Instr.Imm);
32*5f7ddb14SDimitry Andric break;
33*5f7ddb14SDimitry Andric case RISCV::ADDUW:
34*5f7ddb14SDimitry Andric Compressed = false;
35*5f7ddb14SDimitry Andric break;
36*5f7ddb14SDimitry Andric }
37*5f7ddb14SDimitry Andric // Two RVC instructions take the same space as one RVI instruction, but
38*5f7ddb14SDimitry Andric // can take longer to execute than the single RVI instruction. Thus, we
39*5f7ddb14SDimitry Andric // consider that two RVC instruction are slightly more costly than one
40*5f7ddb14SDimitry Andric // RVI instruction. For longer sequences of RVC instructions the space
41*5f7ddb14SDimitry Andric // savings can be worth it, though. The costs below try to model that.
42*5f7ddb14SDimitry Andric if (!Compressed)
43*5f7ddb14SDimitry Andric Cost += 100; // Baseline cost of one RVI instruction: 100%.
44*5f7ddb14SDimitry Andric else
45*5f7ddb14SDimitry Andric Cost += 70; // 70% cost of baseline.
46*5f7ddb14SDimitry Andric }
47*5f7ddb14SDimitry Andric return Cost;
48*5f7ddb14SDimitry Andric }
49*5f7ddb14SDimitry Andric
50*5f7ddb14SDimitry Andric // Recursively generate a sequence for materializing an integer.
generateInstSeqImpl(int64_t Val,const FeatureBitset & ActiveFeatures,RISCVMatInt::InstSeq & Res)51*5f7ddb14SDimitry Andric static void generateInstSeqImpl(int64_t Val,
52*5f7ddb14SDimitry Andric const FeatureBitset &ActiveFeatures,
53*5f7ddb14SDimitry Andric RISCVMatInt::InstSeq &Res) {
54*5f7ddb14SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit];
55*5f7ddb14SDimitry Andric
56af732203SDimitry Andric if (isInt<32>(Val)) {
57af732203SDimitry Andric // Depending on the active bits in the immediate Value v, the following
58af732203SDimitry Andric // instruction sequences are emitted:
59af732203SDimitry Andric //
60af732203SDimitry Andric // v == 0 : ADDI
61af732203SDimitry Andric // v[0,12) != 0 && v[12,32) == 0 : ADDI
62af732203SDimitry Andric // v[0,12) == 0 && v[12,32) != 0 : LUI
63af732203SDimitry Andric // v[0,32) != 0 : LUI+ADDI(W)
64af732203SDimitry Andric int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF;
65af732203SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val);
66af732203SDimitry Andric
67af732203SDimitry Andric if (Hi20)
68*5f7ddb14SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::LUI, Hi20));
69af732203SDimitry Andric
70af732203SDimitry Andric if (Lo12 || Hi20 == 0) {
71af732203SDimitry Andric unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI;
72*5f7ddb14SDimitry Andric Res.push_back(RISCVMatInt::Inst(AddiOpc, Lo12));
73af732203SDimitry Andric }
74af732203SDimitry Andric return;
75af732203SDimitry Andric }
76af732203SDimitry Andric
77af732203SDimitry Andric assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target");
78af732203SDimitry Andric
79af732203SDimitry Andric // In the worst case, for a full 64-bit constant, a sequence of 8 instructions
80af732203SDimitry Andric // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emmitted. Note
81af732203SDimitry Andric // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits
82af732203SDimitry Andric // while the following ADDI instructions contribute up to 12 bits each.
83af732203SDimitry Andric //
84af732203SDimitry Andric // On the first glance, implementing this seems to be possible by simply
85af732203SDimitry Andric // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left
86af732203SDimitry Andric // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the
87af732203SDimitry Andric // fact that ADDI performs a sign extended addition, doing it like that would
88af732203SDimitry Andric // only be possible when at most 11 bits of the ADDI instructions are used.
89af732203SDimitry Andric // Using all 12 bits of the ADDI instructions, like done by GAS, actually
90af732203SDimitry Andric // requires that the constant is processed starting with the least significant
91af732203SDimitry Andric // bit.
92af732203SDimitry Andric //
93af732203SDimitry Andric // In the following, constants are processed from LSB to MSB but instruction
94af732203SDimitry Andric // emission is performed from MSB to LSB by recursively calling
95af732203SDimitry Andric // generateInstSeq. In each recursion, first the lowest 12 bits are removed
96af732203SDimitry Andric // from the constant and the optimal shift amount, which can be greater than
97af732203SDimitry Andric // 12 bits if the constant is sparse, is determined. Then, the shifted
98af732203SDimitry Andric // remaining constant is processed recursively and gets emitted as soon as it
99af732203SDimitry Andric // fits into 32 bits. The emission of the shifts and additions is subsequently
100af732203SDimitry Andric // performed when the recursion returns.
101af732203SDimitry Andric
102af732203SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val);
103af732203SDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) >> 12;
104af732203SDimitry Andric int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52);
105af732203SDimitry Andric Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount);
106af732203SDimitry Andric
107*5f7ddb14SDimitry Andric // If the remaining bits don't fit in 12 bits, we might be able to reduce the
108*5f7ddb14SDimitry Andric // shift amount in order to use LUI which will zero the lower 12 bits.
109*5f7ddb14SDimitry Andric if (ShiftAmount > 12 && !isInt<12>(Hi52) && isInt<32>((uint64_t)Hi52 << 12)) {
110*5f7ddb14SDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match LUI.
111*5f7ddb14SDimitry Andric ShiftAmount -= 12;
112*5f7ddb14SDimitry Andric Hi52 = (uint64_t)Hi52 << 12;
113af732203SDimitry Andric }
114af732203SDimitry Andric
115*5f7ddb14SDimitry Andric generateInstSeqImpl(Hi52, ActiveFeatures, Res);
116*5f7ddb14SDimitry Andric
117*5f7ddb14SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLI, ShiftAmount));
118*5f7ddb14SDimitry Andric if (Lo12)
119*5f7ddb14SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12));
120*5f7ddb14SDimitry Andric }
121*5f7ddb14SDimitry Andric
122*5f7ddb14SDimitry Andric namespace llvm {
123*5f7ddb14SDimitry Andric namespace RISCVMatInt {
generateInstSeq(int64_t Val,const FeatureBitset & ActiveFeatures)124*5f7ddb14SDimitry Andric InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) {
125*5f7ddb14SDimitry Andric RISCVMatInt::InstSeq Res;
126*5f7ddb14SDimitry Andric generateInstSeqImpl(Val, ActiveFeatures, Res);
127*5f7ddb14SDimitry Andric
128*5f7ddb14SDimitry Andric // If the constant is positive we might be able to generate a shifted constant
129*5f7ddb14SDimitry Andric // with no leading zeros and use a final SRLI to restore them.
130*5f7ddb14SDimitry Andric if (Val > 0 && Res.size() > 2) {
131*5f7ddb14SDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] &&
132*5f7ddb14SDimitry Andric "Expected RV32 to only need 2 instructions");
133*5f7ddb14SDimitry Andric unsigned LeadingZeros = countLeadingZeros((uint64_t)Val);
134*5f7ddb14SDimitry Andric uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros;
135*5f7ddb14SDimitry Andric // Fill in the bits that will be shifted out with 1s. An example where this
136*5f7ddb14SDimitry Andric // helps is trailing one masks with 32 or more ones. This will generate
137*5f7ddb14SDimitry Andric // ADDI -1 and an SRLI.
138*5f7ddb14SDimitry Andric ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros);
139*5f7ddb14SDimitry Andric
140*5f7ddb14SDimitry Andric RISCVMatInt::InstSeq TmpSeq;
141*5f7ddb14SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
142*5f7ddb14SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros));
143*5f7ddb14SDimitry Andric
144*5f7ddb14SDimitry Andric // Keep the new sequence if it is an improvement.
145*5f7ddb14SDimitry Andric if (TmpSeq.size() < Res.size()) {
146*5f7ddb14SDimitry Andric Res = TmpSeq;
147*5f7ddb14SDimitry Andric // A 2 instruction sequence is the best we can do.
148*5f7ddb14SDimitry Andric if (Res.size() <= 2)
149*5f7ddb14SDimitry Andric return Res;
150*5f7ddb14SDimitry Andric }
151*5f7ddb14SDimitry Andric
152*5f7ddb14SDimitry Andric // Some cases can benefit from filling the lower bits with zeros instead.
153*5f7ddb14SDimitry Andric ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros);
154*5f7ddb14SDimitry Andric TmpSeq.clear();
155*5f7ddb14SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
156*5f7ddb14SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros));
157*5f7ddb14SDimitry Andric
158*5f7ddb14SDimitry Andric // Keep the new sequence if it is an improvement.
159*5f7ddb14SDimitry Andric if (TmpSeq.size() < Res.size()) {
160*5f7ddb14SDimitry Andric Res = TmpSeq;
161*5f7ddb14SDimitry Andric // A 2 instruction sequence is the best we can do.
162*5f7ddb14SDimitry Andric if (Res.size() <= 2)
163*5f7ddb14SDimitry Andric return Res;
164*5f7ddb14SDimitry Andric }
165*5f7ddb14SDimitry Andric
166*5f7ddb14SDimitry Andric // If we have exactly 32 leading zeros and Zba, we can try using zext.w at
167*5f7ddb14SDimitry Andric // the end of the sequence.
168*5f7ddb14SDimitry Andric if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureExtZba]) {
169*5f7ddb14SDimitry Andric // Try replacing upper bits with 1.
170*5f7ddb14SDimitry Andric uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros);
171*5f7ddb14SDimitry Andric TmpSeq.clear();
172*5f7ddb14SDimitry Andric generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq);
173*5f7ddb14SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDUW, 0));
174*5f7ddb14SDimitry Andric
175*5f7ddb14SDimitry Andric // Keep the new sequence if it is an improvement.
176*5f7ddb14SDimitry Andric if (TmpSeq.size() < Res.size()) {
177*5f7ddb14SDimitry Andric Res = TmpSeq;
178*5f7ddb14SDimitry Andric // A 2 instruction sequence is the best we can do.
179*5f7ddb14SDimitry Andric if (Res.size() <= 2)
180*5f7ddb14SDimitry Andric return Res;
181*5f7ddb14SDimitry Andric }
182*5f7ddb14SDimitry Andric }
183*5f7ddb14SDimitry Andric }
184*5f7ddb14SDimitry Andric
185*5f7ddb14SDimitry Andric return Res;
186*5f7ddb14SDimitry Andric }
187*5f7ddb14SDimitry Andric
getIntMatCost(const APInt & Val,unsigned Size,const FeatureBitset & ActiveFeatures,bool CompressionCost)188*5f7ddb14SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size,
189*5f7ddb14SDimitry Andric const FeatureBitset &ActiveFeatures,
190*5f7ddb14SDimitry Andric bool CompressionCost) {
191*5f7ddb14SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit];
192*5f7ddb14SDimitry Andric bool HasRVC = CompressionCost && ActiveFeatures[RISCV::FeatureStdExtC];
193af732203SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32;
194af732203SDimitry Andric
195af732203SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost
196af732203SDimitry Andric // of each chunk.
197af732203SDimitry Andric int Cost = 0;
198af732203SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) {
199af732203SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize);
200*5f7ddb14SDimitry Andric InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures);
201*5f7ddb14SDimitry Andric Cost += getInstSeqCost(MatSeq, HasRVC);
202af732203SDimitry Andric }
203af732203SDimitry Andric return std::max(1, Cost);
204af732203SDimitry Andric }
205af732203SDimitry Andric } // namespace RISCVMatInt
206af732203SDimitry Andric } // namespace llvm
207