1//===- RISCVMatInt.cpp - Immediate materialisation -------------*- C++ -*--===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "RISCVMatInt.h"
10#include "MCTargetDesc/RISCVMCTargetDesc.h"
11#include "llvm/ADT/APInt.h"
12#include "llvm/MC/MCInstBuilder.h"
13#include "llvm/Support/MathExtras.h"
14using namespace llvm;
15
16static int getInstSeqCost(RISCVMatInt::InstSeq &Res, bool HasRVC) {
17 if (!HasRVC)
18 return Res.size();
19
20 int Cost = 0;
21 for (auto Instr : Res) {
22 // Assume instructions that aren't listed aren't compressible.
23 bool Compressed = false;
24 switch (Instr.getOpcode()) {
25 case RISCV::QC_E_LI:
26 // One 48-bit instruction takes the space of 1.5 regular instructions.
27 Cost += 150;
28 continue;
29 case RISCV::SLLI:
30 case RISCV::SRLI:
31 Compressed = true;
32 break;
33 case RISCV::ADDI:
34 case RISCV::ADDIW:
35 case RISCV::LUI:
36 Compressed = isInt<6>(x: Instr.getImm());
37 break;
38 }
39 // Two RVC instructions take the same space as one RVI instruction, but
40 // can take longer to execute than the single RVI instruction. Thus, we
41 // consider that two RVC instruction are slightly more costly than one
42 // RVI instruction. For longer sequences of RVC instructions the space
43 // savings can be worth it, though. The costs below try to model that.
44 if (!Compressed)
45 Cost += 100; // Baseline cost of one RVI instruction: 100%.
46 else
47 Cost += 70; // 70% cost of baseline.
48 }
49 return Cost;
50}
51
52// Recursively generate a sequence for materializing an integer.
53static void generateInstSeqImpl(int64_t Val, const MCSubtargetInfo &STI,
54 RISCVMatInt::InstSeq &Res) {
55 bool IsRV64 = STI.hasFeature(Feature: RISCV::Feature64Bit);
56
57 // Use BSETI for a single bit that can't be expressed by a single LUI or ADDI.
58 if (STI.hasFeature(Feature: RISCV::FeatureStdExtZbs) && isPowerOf2_64(Value: Val) &&
59 (!isInt<32>(x: Val) || Val == 0x800)) {
60 Res.emplace_back(Args: RISCV::BSETI, Args: Log2_64(Value: Val));
61 return;
62 }
63
64 if (!IsRV64 && STI.hasFeature(Feature: RISCV::FeatureVendorXqcili)) {
65 bool FitsOneStandardInst = ((Val & 0xFFF) == 0) || isInt<12>(x: Val);
66
67 // 20-bit signed immediates that don't fit into `ADDI` or `LUI` should use
68 // `QC.LI` (a single 32-bit instruction).
69 if (!FitsOneStandardInst && isInt<20>(x: Val)) {
70 Res.emplace_back(Args: RISCV::QC_LI, Args&: Val);
71 return;
72 }
73
74 // 32-bit signed immediates that don't fit into `ADDI`, `LUI` or `QC.LI`
75 // should use `QC.E.LI` (a single 48-bit instruction).
76 if (!FitsOneStandardInst && isInt<32>(x: Val)) {
77 Res.emplace_back(Args: RISCV::QC_E_LI, Args&: Val);
78 return;
79 }
80 }
81
82 // Try PLI/PLUI if available, but prefer LI/LUI.
83 if (STI.hasFeature(Feature: RISCV::FeatureStdExtP) && !isInt<12>(x: Val) &&
84 !isShiftedInt<20, 12>(x: Val)) {
85 unsigned Width = 64;
86
87 int32_t Bit31To0 = Val;
88 if (!IsRV64 || int32_t(Val >> 32) == Bit31To0)
89 Width = 32;
90
91 int16_t Bit15To0 = Bit31To0;
92 if (Width == 32 && int16_t(Bit31To0 >> 16) == Bit15To0)
93 Width = 16;
94
95 int8_t Bit7To0 = Bit15To0;
96 if (Width == 16 && int8_t(Bit15To0 >> 8) == Bit7To0) {
97 Res.emplace_back(Args: RISCV::PLI_B, Args&: Bit7To0);
98 return;
99 }
100
101 if (Width == 16 && isInt<10>(x: Bit15To0)) {
102 Res.emplace_back(Args: RISCV::PLI_H, Args&: Bit15To0);
103 return;
104 }
105 if (Width == 16 && isShiftedInt<10, 6>(x: Bit15To0)) {
106 Res.emplace_back(Args: RISCV::PLUI_H, Args: Bit15To0 >> 6);
107 return;
108 }
109
110 if (Width == 32 && isInt<10>(x: Bit31To0)) {
111 Res.emplace_back(Args: RISCV::PLI_W, Args&: Bit31To0);
112 return;
113 }
114 if (Width == 32 && isShiftedInt<10, 22>(x: Bit31To0)) {
115 Res.emplace_back(Args: RISCV::PLUI_W, Args: Bit31To0 >> 22);
116 return;
117 }
118 }
119
120 if (isInt<32>(x: Val)) {
121 // Depending on the active bits in the immediate Value v, the following
122 // instruction sequences are emitted:
123 //
124 // v == 0 : ADDI
125 // v[0,12) != 0 && v[12,32) == 0 : ADDI
126 // v[0,12) == 0 && v[12,32) != 0 : LUI
127 // v[0,32) != 0 : LUI+ADDI(W)
128 int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF;
129 int64_t Lo12 = SignExtend64<12>(x: Val);
130
131 if (Hi20)
132 Res.emplace_back(Args: RISCV::LUI, Args&: Hi20);
133
134 if (Lo12 || Hi20 == 0) {
135 unsigned AddiOpc = RISCV::ADDI;
136 if (IsRV64 && Hi20) {
137 // Use ADDIW rather than ADDI only when necessary for correctness. As
138 // noted in RISCVOptWInstrs, this helps reduce test differences vs
139 // RV32 without being a pessimization.
140 int64_t LuiRes = SignExtend64<32>(x: Hi20 << 12);
141 if (!isInt<32>(x: LuiRes + Lo12))
142 AddiOpc = RISCV::ADDIW;
143 }
144 Res.emplace_back(Args&: AddiOpc, Args&: Lo12);
145 }
146 return;
147 }
148
149 assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target");
150
151 // In the worst case, for a full 64-bit constant, a sequence of 8 instructions
152 // (i.e., LUI+ADDI+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emitted. Note
153 // that the first two instructions (LUI+ADDI) can contribute up to 32 bits
154 // while the following ADDI instructions contribute up to 12 bits each.
155 //
156 // On the first glance, implementing this seems to be possible by simply
157 // emitting the most significant 32 bits (LUI+ADDI(W)) followed by as many
158 // left shift (SLLI) and immediate additions (ADDI) as needed. However, due to
159 // the fact that ADDI performs a sign extended addition, doing it like that
160 // would only be possible when at most 11 bits of the ADDI instructions are
161 // used. Using all 12 bits of the ADDI instructions, like done by GAS,
162 // actually requires that the constant is processed starting with the least
163 // significant bit.
164 //
165 // In the following, constants are processed from LSB to MSB but instruction
166 // emission is performed from MSB to LSB by recursively calling
167 // generateInstSeq. In each recursion, first the lowest 12 bits are removed
168 // from the constant and the optimal shift amount, which can be greater than
169 // 12 bits if the constant is sparse, is determined. Then, the shifted
170 // remaining constant is processed recursively and gets emitted as soon as it
171 // fits into 32 bits. The emission of the shifts and additions is subsequently
172 // performed when the recursion returns.
173
174 int64_t Lo12 = SignExtend64<12>(x: Val);
175 Val = (uint64_t)Val - (uint64_t)Lo12;
176
177 unsigned ShiftAmount = 0;
178 unsigned ShiftOpc = RISCV::SLLI;
179
180 // Val might now be valid for LUI without needing a shift.
181 if (!isInt<32>(x: Val)) {
182 ShiftAmount = llvm::countr_zero(Val: (uint64_t)Val);
183 Val >>= ShiftAmount;
184
185 // If the remaining bits don't fit in 12 bits, we might be able to reduce
186 // the shift amount in order to use LUI which will zero the lower 12
187 // bits.
188 if (ShiftAmount > 12 && !isInt<12>(x: Val)) {
189 if (isInt<32>(x: (uint64_t)Val << 12)) {
190 // Reduce the shift amount and add zeros to the LSBs so it will match
191 // LUI.
192 ShiftAmount -= 12;
193 Val = (uint64_t)Val << 12;
194 } else if (isUInt<32>(x: (uint64_t)Val << 12) &&
195 STI.hasFeature(Feature: RISCV::FeatureStdExtZba)) {
196 // Reduce the shift amount and add zeros to the LSBs so it will match
197 // LUI, then shift left with SLLI.UW to clear the upper 32 set bits.
198 ShiftAmount -= 12;
199 Val = SignExtend64<32>(x: (uint64_t)Val << 12);
200 ShiftOpc = RISCV::SLLI_UW;
201 }
202 }
203
204 // Try to use SLLI_UW for Val when it is uint32 but not int32.
205 if (isUInt<32>(x: Val) && !isInt<32>(x: Val) &&
206 STI.hasFeature(Feature: RISCV::FeatureStdExtZba)) {
207 // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with
208 // SLLI_UW.
209 Val = SignExtend64<32>(x: (uint64_t)Val);
210 ShiftOpc = RISCV::SLLI_UW;
211 }
212 }
213
214 generateInstSeqImpl(Val, STI, Res);
215
216 // Skip shift if we were able to use LUI directly.
217 if (ShiftAmount) {
218 Res.emplace_back(Args&: ShiftOpc, Args&: ShiftAmount);
219 }
220
221 if (Lo12)
222 Res.emplace_back(Args: RISCV::ADDI, Args&: Lo12);
223}
224
225// Returns the amount by which Val rotated left is either a simm12 (for ADDI) or
226// a sign-extended 32-bit value with zero low 12 bits (for LUI), or 0 if there
227// is none.
228static unsigned extractRotateInfo(int64_t Val) {
229 // for case: 0b111..1..xxxxxx1..1..
230 unsigned LeadingOnes = llvm::countl_one(Value: (uint64_t)Val);
231 unsigned TrailingOnes = llvm::countr_one(Value: (uint64_t)Val);
232 if (TrailingOnes > 0 && TrailingOnes < 64 &&
233 (LeadingOnes + TrailingOnes) > (64 - 12))
234 return 64 - TrailingOnes;
235
236 // for case: 0bxxx1..1..1...xxx
237 unsigned UpperTrailingOnes = llvm::countr_one(Value: Hi_32(Value: Val));
238 unsigned LowerLeadingOnes = llvm::countl_one(Value: Lo_32(Value: Val));
239 if (UpperTrailingOnes < 32 &&
240 (UpperTrailingOnes + LowerLeadingOnes) > (64 - 12))
241 return 32 - UpperTrailingOnes;
242
243 // Try values LUI can load rotated, for example masks that wrap around bit 63
244 // and the boxed constants of JIT compilers. Rotated left by the result, Val
245 // is 12 zero bits, 20 bits of payload and 32 copies of the payload's top bit.
246 // Going upwards circularly, Val therefore contains either a run of at least
247 // 33 ones ending at bit 63 - Rotate or a run of at least 45 zeros ending at
248 // bit 11 - Rotate. A run that long contains bit 0 or bit 32, so its end is
249 // found by counting the trailing ones from one of those bits. Try both for
250 // each sign of the payload.
251 for (bool Negative : {true, false}) {
252 uint64_t Run = Negative ? Val : ~Val;
253 unsigned RunTop = Negative ? 64 : 64 + 12;
254 for (unsigned RunEnd :
255 {llvm::countr_one(Value: Run), 32 + llvm::countr_one(Value: Hi_32(Value: Run))}) {
256 unsigned Rotate = (RunTop - RunEnd) % 64;
257 if (Rotate != 0 &&
258 isShiftedInt<20, 12>(x: (int64_t)llvm::rotl<uint64_t>(V: Val, R: Rotate)))
259 return Rotate;
260 }
261 }
262
263 return 0;
264}
265
266static void generateInstSeqLeadingZeros(int64_t Val, const MCSubtargetInfo &STI,
267 RISCVMatInt::InstSeq &Res) {
268 assert(Val > 0 && "Expected positive val");
269
270 unsigned LeadingZeros = llvm::countl_zero(Val: (uint64_t)Val);
271 uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros;
272 // Fill in the bits that will be shifted out with 1s. An example where this
273 // helps is trailing one masks with 32 or more ones. This will generate
274 // ADDI -1 and an SRLI.
275 ShiftedVal |= maskTrailingOnes<uint64_t>(N: LeadingZeros);
276
277 RISCVMatInt::InstSeq TmpSeq;
278 generateInstSeqImpl(Val: ShiftedVal, STI, Res&: TmpSeq);
279
280 // Keep the new sequence if it is an improvement or the original is empty.
281 if ((TmpSeq.size() + 1) < Res.size() ||
282 (Res.empty() && TmpSeq.size() < 8)) {
283 TmpSeq.emplace_back(Args: RISCV::SRLI, Args&: LeadingZeros);
284 Res = TmpSeq;
285 }
286
287 // Some cases can benefit from filling the lower bits with zeros instead.
288 ShiftedVal &= maskTrailingZeros<uint64_t>(N: LeadingZeros);
289 TmpSeq.clear();
290 generateInstSeqImpl(Val: ShiftedVal, STI, Res&: TmpSeq);
291
292 // Keep the new sequence if it is an improvement or the original is empty.
293 if ((TmpSeq.size() + 1) < Res.size() ||
294 (Res.empty() && TmpSeq.size() < 8)) {
295 TmpSeq.emplace_back(Args: RISCV::SRLI, Args&: LeadingZeros);
296 Res = TmpSeq;
297 }
298
299 // If we have exactly 32 leading zeros and Zba, we can try using zext.w at
300 // the end of the sequence.
301 if (LeadingZeros == 32 && STI.hasFeature(Feature: RISCV::FeatureStdExtZba)) {
302 // Bit 31 is set, so sign extend to fill the upper bits with 1s.
303 uint64_t LeadingOnesVal = SignExtend64<32>(x: Val);
304 TmpSeq.clear();
305 generateInstSeqImpl(Val: LeadingOnesVal, STI, Res&: TmpSeq);
306
307 // Keep the new sequence if it is an improvement.
308 if ((TmpSeq.size() + 1) < Res.size() ||
309 (Res.empty() && TmpSeq.size() < 8)) {
310 TmpSeq.emplace_back(Args: RISCV::ADD_UW, Args: 0);
311 Res = TmpSeq;
312 }
313 }
314}
315
316namespace llvm::RISCVMatInt {
317InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI) {
318 RISCVMatInt::InstSeq Res;
319 generateInstSeqImpl(Val, STI, Res);
320
321 // If the low 12 bits are non-zero, the first expansion may end with an ADDI
322 // or ADDIW. If there are trailing zeros, try generating a sign extended
323 // constant with no trailing zeros and use a final SLLI to restore them.
324 if ((Val & 0xfff) != 0 && (Val & 1) == 0 && Res.size() >= 2) {
325 unsigned TrailingZeros = llvm::countr_zero(Val: (uint64_t)Val);
326 int64_t ShiftedVal = Val >> TrailingZeros;
327 // If we can use C.LI+C.SLLI instead of LUI+ADDI(W) prefer that since
328 // its more compressible. But only if LUI+ADDI(W) isn't fusable.
329 // NOTE: We don't check for C extension to minimize differences in generated
330 // code.
331 bool IsShiftedCompressible =
332 isInt<6>(x: ShiftedVal) && !STI.hasFeature(Feature: RISCV::TuneLUIADDIFusion);
333 RISCVMatInt::InstSeq TmpSeq;
334 generateInstSeqImpl(Val: ShiftedVal, STI, Res&: TmpSeq);
335
336 // Keep the new sequence if it is an improvement.
337 if ((TmpSeq.size() + 1) < Res.size() || IsShiftedCompressible) {
338 TmpSeq.emplace_back(Args: RISCV::SLLI, Args&: TrailingZeros);
339 Res = TmpSeq;
340 }
341 }
342
343 // If we have a 1 or 2 instruction sequence this is the best we can do. This
344 // will always be true for RV32 and will often be true for RV64.
345 if (Res.size() <= 2)
346 return Res;
347
348 assert(STI.hasFeature(RISCV::Feature64Bit) &&
349 "Expected RV32 to only need 2 instructions");
350
351 // If the lower 13 bits are something like 0x17ff, try to add 1 to change the
352 // lower 13 bits to 0x1800. We can restore this with an ADDI of -1 at the end
353 // of the sequence. Call generateInstSeqImpl on the new constant which may
354 // subtract 0xfffffffffffff800 to create another ADDI. This will leave a
355 // constant with more than 12 trailing zeros for the next recursive step.
356 if ((Val & 0xfff) != 0 && (Val & 0x1800) == 0x1000) {
357 int64_t Imm12 = -(0x800 - (Val & 0xfff));
358 int64_t AdjustedVal = Val - Imm12;
359 RISCVMatInt::InstSeq TmpSeq;
360 generateInstSeqImpl(Val: AdjustedVal, STI, Res&: TmpSeq);
361
362 // Keep the new sequence if it is an improvement.
363 if ((TmpSeq.size() + 1) < Res.size()) {
364 TmpSeq.emplace_back(Args: RISCV::ADDI, Args&: Imm12);
365 Res = TmpSeq;
366 }
367 }
368
369 // If the constant is positive we might be able to generate a shifted constant
370 // with no leading zeros and use a final SRLI to restore them.
371 if (Val > 0 && Res.size() > 2) {
372 generateInstSeqLeadingZeros(Val, STI, Res);
373 }
374
375 // If the constant is negative, trying inverting and using our trailing zero
376 // optimizations. Use an xori to invert the final value.
377 if (Val < 0 && Res.size() > 3) {
378 uint64_t InvertedVal = ~(uint64_t)Val;
379 RISCVMatInt::InstSeq TmpSeq;
380 generateInstSeqLeadingZeros(Val: InvertedVal, STI, Res&: TmpSeq);
381
382 // Keep it if we found a sequence that is smaller after inverting.
383 if (!TmpSeq.empty() && (TmpSeq.size() + 1) < Res.size()) {
384 TmpSeq.emplace_back(Args: RISCV::XORI, Args: -1);
385 Res = TmpSeq;
386 }
387 }
388
389 // If the Low and High halves are the same, use pack. The pack instruction
390 // packs the XLEN/2-bit lower halves of rs1 and rs2 into rd, with rs1 in the
391 // lower half and rs2 in the upper half.
392 if (Res.size() > 2 && (STI.hasFeature(Feature: RISCV::FeatureStdExtZbkb) ||
393 STI.hasFeature(Feature: RISCV::FeatureStdExtP))) {
394 int64_t LoVal = SignExtend64<32>(x: Val);
395 int64_t HiVal = SignExtend64<32>(x: Val >> 32);
396 if (LoVal == HiVal) {
397 RISCVMatInt::InstSeq TmpSeq;
398 generateInstSeqImpl(Val: LoVal, STI, Res&: TmpSeq);
399 if ((TmpSeq.size() + 1) < Res.size()) {
400 TmpSeq.emplace_back(Args: RISCV::PACK, Args: 0);
401 Res = TmpSeq;
402 }
403 }
404 }
405
406 // Perform optimization with BSETI in the Zbs extension.
407 if (Res.size() > 2 && STI.hasFeature(Feature: RISCV::FeatureStdExtZbs)) {
408 // Create a simm32 value for LUI+ADDI(W) by forcing the upper 33 bits to
409 // zero. Xor that with original value to get which bits should be set by
410 // BSETI.
411 uint64_t Lo = Val & 0x7fffffff;
412 uint64_t Hi = Val ^ Lo;
413 assert(Hi != 0);
414 RISCVMatInt::InstSeq TmpSeq;
415
416 if (Lo != 0)
417 generateInstSeqImpl(Val: Lo, STI, Res&: TmpSeq);
418
419 if (TmpSeq.size() + llvm::popcount(Value: Hi) < Res.size()) {
420 do {
421 TmpSeq.emplace_back(Args: RISCV::BSETI, Args: llvm::countr_zero(Val: Hi));
422 Hi &= (Hi - 1); // Clear lowest set bit.
423 } while (Hi != 0);
424 Res = TmpSeq;
425 }
426
427 // Fold LI 1 + SLLI into BSETI.
428 if (Res[0].getOpcode() == RISCV::ADDI && Res[0].getImm() == 1 &&
429 Res[1].getOpcode() == RISCV::SLLI) {
430 Res.erase(CI: Res.begin()); // Remove ADDI.
431 Res.front() = Inst(RISCV::BSETI, Res.front().getImm()); // Patch SLLI.
432 }
433 }
434
435 // Perform optimization with BCLRI in the Zbs extension.
436 if (Res.size() > 2 && STI.hasFeature(Feature: RISCV::FeatureStdExtZbs)) {
437 // Create a simm32 value for LUI+ADDI(W) by forcing the upper 33 bits to
438 // one. Xor that with original value to get which bits should be cleared by
439 // BCLRI.
440 uint64_t Lo = Val | 0xffffffff80000000;
441 uint64_t Hi = Val ^ Lo;
442 assert(Hi != 0);
443
444 RISCVMatInt::InstSeq TmpSeq;
445 generateInstSeqImpl(Val: Lo, STI, Res&: TmpSeq);
446
447 if (TmpSeq.size() + llvm::popcount(Value: Hi) < Res.size()) {
448 do {
449 TmpSeq.emplace_back(Args: RISCV::BCLRI, Args: llvm::countr_zero(Val: Hi));
450 Hi &= (Hi - 1); // Clear lowest set bit.
451 } while (Hi != 0);
452 Res = TmpSeq;
453 }
454 }
455
456 // Perform optimization with SH*ADD in the Zba extension.
457 if (Res.size() > 2 && STI.hasFeature(Feature: RISCV::FeatureStdExtZba)) {
458 int64_t Div = 0;
459 unsigned Opc = 0;
460 RISCVMatInt::InstSeq TmpSeq;
461 // Select the opcode and divisor.
462 if ((Val % 3) == 0 && isInt<32>(x: Val / 3)) {
463 Div = 3;
464 Opc = RISCV::SH1ADD;
465 } else if ((Val % 5) == 0 && isInt<32>(x: Val / 5)) {
466 Div = 5;
467 Opc = RISCV::SH2ADD;
468 } else if ((Val % 9) == 0 && isInt<32>(x: Val / 9)) {
469 Div = 9;
470 Opc = RISCV::SH3ADD;
471 }
472 // Build the new instruction sequence.
473 if (Div > 0) {
474 generateInstSeqImpl(Val: Val / Div, STI, Res&: TmpSeq);
475 if ((TmpSeq.size() + 1) < Res.size()) {
476 TmpSeq.emplace_back(Args&: Opc, Args: 0);
477 Res = TmpSeq;
478 }
479 } else {
480 // Try to use LUI+SH*ADD+ADDI.
481 int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull;
482 int64_t Lo12 = SignExtend64<12>(x: Val);
483 Div = 0;
484 if (isInt<32>(x: Hi52 / 3) && (Hi52 % 3) == 0) {
485 Div = 3;
486 Opc = RISCV::SH1ADD;
487 } else if (isInt<32>(x: Hi52 / 5) && (Hi52 % 5) == 0) {
488 Div = 5;
489 Opc = RISCV::SH2ADD;
490 } else if (isInt<32>(x: Hi52 / 9) && (Hi52 % 9) == 0) {
491 Div = 9;
492 Opc = RISCV::SH3ADD;
493 }
494 // Build the new instruction sequence.
495 if (Div > 0) {
496 // For Val that has zero Lo12 (implies Val equals to Hi52) should has
497 // already been processed to LUI+SH*ADD by previous optimization.
498 assert(Lo12 != 0 &&
499 "unexpected instruction sequence for immediate materialisation");
500 assert(TmpSeq.empty() && "Expected empty TmpSeq");
501 generateInstSeqImpl(Val: Hi52 / Div, STI, Res&: TmpSeq);
502 if ((TmpSeq.size() + 2) < Res.size()) {
503 TmpSeq.emplace_back(Args&: Opc, Args: 0);
504 TmpSeq.emplace_back(Args: RISCV::ADDI, Args&: Lo12);
505 Res = TmpSeq;
506 }
507 }
508 }
509 }
510
511 // Perform optimization with rori in the Zbb and th.srri in the XTheadBb
512 // extension.
513 if (Res.size() > 2 && (STI.hasFeature(Feature: RISCV::FeatureStdExtZbb) ||
514 STI.hasFeature(Feature: RISCV::FeatureVendorXTHeadBb))) {
515 unsigned RotateOpc =
516 STI.hasFeature(Feature: RISCV::FeatureStdExtZbb) ? RISCV::RORI : RISCV::TH_SRRI;
517 if (unsigned Rotate = extractRotateInfo(Val)) {
518 RISCVMatInt::InstSeq TmpSeq;
519 int64_t Rotated = llvm::rotl<uint64_t>(V: Val, R: Rotate);
520 if (isInt<12>(x: Rotated)) {
521 TmpSeq.emplace_back(Args: RISCV::ADDI, Args&: Rotated);
522 } else {
523 assert((isShiftedInt<20, 12>(Rotated)) &&
524 "Expected a value LUI can load");
525 TmpSeq.emplace_back(Args: RISCV::LUI, Args: (Rotated >> 12) & 0xFFFFF);
526 }
527 TmpSeq.emplace_back(Args&: RotateOpc, Args&: Rotate);
528 Res = TmpSeq;
529 }
530 }
531 return Res;
532}
533
534void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI,
535 MCRegister DestReg, SmallVectorImpl<MCInst> &Insts) {
536 RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Val, STI);
537
538 MCRegister SrcReg = RISCV::X0;
539 for (RISCVMatInt::Inst &Inst : Seq) {
540 switch (Inst.getOpndKind()) {
541 case RISCVMatInt::Imm:
542 Insts.push_back(Elt: MCInstBuilder(Inst.getOpcode())
543 .addReg(Reg: DestReg)
544 .addImm(Val: Inst.getImm()));
545 break;
546 case RISCVMatInt::RegX0:
547 Insts.push_back(Elt: MCInstBuilder(Inst.getOpcode())
548 .addReg(Reg: DestReg)
549 .addReg(Reg: SrcReg)
550 .addReg(Reg: RISCV::X0));
551 break;
552 case RISCVMatInt::RegReg:
553 Insts.push_back(Elt: MCInstBuilder(Inst.getOpcode())
554 .addReg(Reg: DestReg)
555 .addReg(Reg: SrcReg)
556 .addReg(Reg: SrcReg));
557 break;
558 case RISCVMatInt::RegImm:
559 Insts.push_back(Elt: MCInstBuilder(Inst.getOpcode())
560 .addReg(Reg: DestReg)
561 .addReg(Reg: SrcReg)
562 .addImm(Val: Inst.getImm()));
563 break;
564 }
565
566 // Only the first instruction has X0 as its source.
567 SrcReg = DestReg;
568 }
569}
570
571InstSeq generateTwoRegInstSeq(int64_t Val, const MCSubtargetInfo &STI,
572 unsigned &ShiftAmt, unsigned &AddOpc) {
573 int64_t LoVal = SignExtend64<32>(x: Val);
574 if (LoVal == 0)
575 return RISCVMatInt::InstSeq();
576
577 // Subtract the LoVal to emulate the effect of the final ADD.
578 uint64_t Tmp = (uint64_t)Val - (uint64_t)LoVal;
579 assert(Tmp != 0);
580
581 // Use trailing zero counts to figure how far we need to shift LoVal to line
582 // up with the remaining constant.
583 // TODO: This algorithm assumes all non-zero bits in the low 32 bits of the
584 // final constant come from LoVal.
585 unsigned TzLo = llvm::countr_zero(Val: (uint64_t)LoVal);
586 unsigned TzHi = llvm::countr_zero(Val: Tmp);
587 assert(TzLo < 32 && TzHi >= 32);
588 ShiftAmt = TzHi - TzLo;
589 AddOpc = RISCV::ADD;
590
591 if (Tmp == ((uint64_t)LoVal << ShiftAmt))
592 return RISCVMatInt::generateInstSeq(Val: LoVal, STI);
593
594 // If we have Zba, we can use (ADD_UW X, (SLLI X, 32)).
595 if (STI.hasFeature(Feature: RISCV::FeatureStdExtZba) && Lo_32(Value: Val) == Hi_32(Value: Val)) {
596 ShiftAmt = 32;
597 AddOpc = RISCV::ADD_UW;
598 return RISCVMatInt::generateInstSeq(Val: LoVal, STI);
599 }
600
601 return RISCVMatInt::InstSeq();
602}
603
604int getIntMatCost(const APInt &Val, unsigned Size, const MCSubtargetInfo &STI,
605 bool CompressionCost, bool FreeZeroes) {
606 bool IsRV64 = STI.hasFeature(Feature: RISCV::Feature64Bit);
607 bool HasRVC = CompressionCost && STI.hasFeature(Feature: RISCV::FeatureStdExtZca);
608 int PlatRegSize = IsRV64 ? 64 : 32;
609
610 // Split the constant into platform register sized chunks, and calculate cost
611 // of each chunk.
612 int Cost = 0;
613 for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) {
614 APInt Chunk = Val.ashr(ShiftAmt: ShiftVal).sextOrTrunc(width: PlatRegSize);
615 if (FreeZeroes && Chunk.getSExtValue() == 0)
616 continue;
617 InstSeq MatSeq = generateInstSeq(Val: Chunk.getSExtValue(), STI);
618 Cost += getInstSeqCost(Res&: MatSeq, HasRVC);
619 }
620 return std::max(a: FreeZeroes ? 0 : 1, b: Cost);
621}
622
623OpndKind Inst::getOpndKind() const {
624 switch (Opc) {
625 default:
626 llvm_unreachable("Unexpected opcode!");
627 case RISCV::LUI:
628 case RISCV::QC_LI:
629 case RISCV::QC_E_LI:
630 case RISCV::PLI_B:
631 case RISCV::PLI_H:
632 case RISCV::PLI_W:
633 case RISCV::PLUI_H:
634 case RISCV::PLUI_W:
635 return RISCVMatInt::Imm;
636 case RISCV::ADD_UW:
637 return RISCVMatInt::RegX0;
638 case RISCV::SH1ADD:
639 case RISCV::SH2ADD:
640 case RISCV::SH3ADD:
641 case RISCV::PACK:
642 return RISCVMatInt::RegReg;
643 case RISCV::ADDI:
644 case RISCV::ADDIW:
645 case RISCV::XORI:
646 case RISCV::SLLI:
647 case RISCV::SRLI:
648 case RISCV::SLLI_UW:
649 case RISCV::RORI:
650 case RISCV::BSETI:
651 case RISCV::BCLRI:
652 case RISCV::TH_SRRI:
653 return RISCVMatInt::RegImm;
654 }
655}
656
657} // namespace llvm::RISCVMatInt
658