LLVM 23.0.0git
RISCVMatInt.cpp
Go to the documentation of this file.
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"
11#include "llvm/ADT/APInt.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>(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,
55 bool IsRV64 = STI.hasFeature(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(RISCV::FeatureStdExtZbs) && isPowerOf2_64(Val) &&
59 (!isInt<32>(Val) || Val == 0x800)) {
60 Res.emplace_back(RISCV::BSETI, Log2_64(Val));
61 return;
62 }
63
64 if (!IsRV64 && STI.hasFeature(RISCV::FeatureVendorXqcili)) {
65 bool FitsOneStandardInst = ((Val & 0xFFF) == 0) || isInt<12>(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>(Val)) {
70 Res.emplace_back(RISCV::QC_LI, 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>(Val)) {
77 Res.emplace_back(RISCV::QC_E_LI, Val);
78 return;
79 }
80 }
81
82 // Try PLI/PLUI if available, but prefer LI/LUI.
83 if (STI.hasFeature(RISCV::FeatureStdExtP) && !isInt<12>(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(RISCV::PLI_B, Bit7To0);
98 return;
99 }
100
101 if (Width == 16 && isInt<10>(Bit15To0)) {
102 Res.emplace_back(RISCV::PLI_H, Bit15To0);
103 return;
104 }
105 if (Width == 16 && isShiftedInt<10, 6>(Bit15To0)) {
106 Res.emplace_back(RISCV::PLUI_H, Bit15To0 >> 6);
107 return;
108 }
109
110 if (Width == 32 && isInt<10>(Bit31To0)) {
111 Res.emplace_back(RISCV::PLI_W, Bit31To0);
112 return;
113 }
114 if (Width == 32 && isShiftedInt<10, 22>(Bit31To0)) {
115 Res.emplace_back(RISCV::PLUI_W, Bit31To0 >> 22);
116 return;
117 }
118 }
119
120 if (isInt<32>(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>(Val);
130
131 if (Hi20)
132 Res.emplace_back(RISCV::LUI, 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>(Hi20 << 12);
141 if (!isInt<32>(LuiRes + Lo12))
142 AddiOpc = RISCV::ADDIW;
143 }
144 Res.emplace_back(AddiOpc, 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>(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>(Val)) {
182 ShiftAmount = llvm::countr_zero((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>(Val)) {
189 if (isInt<32>((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>((uint64_t)Val << 12) &&
195 STI.hasFeature(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>((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>(Val) && !isInt<32>(Val) &&
206 STI.hasFeature(RISCV::FeatureStdExtZba)) {
207 // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with
208 // SLLI_UW.
209 Val = SignExtend64<32>((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(ShiftOpc, ShiftAmount);
219 }
220
221 if (Lo12)
222 Res.emplace_back(RISCV::ADDI, Lo12);
223}
224
225static unsigned extractRotateInfo(int64_t Val) {
226 // for case: 0b111..1..xxxxxx1..1..
227 unsigned LeadingOnes = llvm::countl_one((uint64_t)Val);
228 unsigned TrailingOnes = llvm::countr_one((uint64_t)Val);
229 if (TrailingOnes > 0 && TrailingOnes < 64 &&
230 (LeadingOnes + TrailingOnes) > (64 - 12))
231 return 64 - TrailingOnes;
232
233 // for case: 0bxxx1..1..1...xxx
234 unsigned UpperTrailingOnes = llvm::countr_one(Hi_32(Val));
235 unsigned LowerLeadingOnes = llvm::countl_one(Lo_32(Val));
236 if (UpperTrailingOnes < 32 &&
237 (UpperTrailingOnes + LowerLeadingOnes) > (64 - 12))
238 return 32 - UpperTrailingOnes;
239
240 return 0;
241}
242
243static void generateInstSeqLeadingZeros(int64_t Val, const MCSubtargetInfo &STI,
245 assert(Val > 0 && "Expected positive val");
246
247 unsigned LeadingZeros = llvm::countl_zero((uint64_t)Val);
248 uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros;
249 // Fill in the bits that will be shifted out with 1s. An example where this
250 // helps is trailing one masks with 32 or more ones. This will generate
251 // ADDI -1 and an SRLI.
252 ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros);
253
255 generateInstSeqImpl(ShiftedVal, STI, TmpSeq);
256
257 // Keep the new sequence if it is an improvement or the original is empty.
258 if ((TmpSeq.size() + 1) < Res.size() ||
259 (Res.empty() && TmpSeq.size() < 8)) {
260 TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros);
261 Res = TmpSeq;
262 }
263
264 // Some cases can benefit from filling the lower bits with zeros instead.
265 ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros);
266 TmpSeq.clear();
267 generateInstSeqImpl(ShiftedVal, STI, TmpSeq);
268
269 // Keep the new sequence if it is an improvement or the original is empty.
270 if ((TmpSeq.size() + 1) < Res.size() ||
271 (Res.empty() && TmpSeq.size() < 8)) {
272 TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros);
273 Res = TmpSeq;
274 }
275
276 // If we have exactly 32 leading zeros and Zba, we can try using zext.w at
277 // the end of the sequence.
278 if (LeadingZeros == 32 && STI.hasFeature(RISCV::FeatureStdExtZba)) {
279 // Bit 31 is set, so sign extend to fill the upper bits with 1s.
280 uint64_t LeadingOnesVal = SignExtend64<32>(Val);
281 TmpSeq.clear();
282 generateInstSeqImpl(LeadingOnesVal, STI, TmpSeq);
283
284 // Keep the new sequence if it is an improvement.
285 if ((TmpSeq.size() + 1) < Res.size() ||
286 (Res.empty() && TmpSeq.size() < 8)) {
287 TmpSeq.emplace_back(RISCV::ADD_UW, 0);
288 Res = TmpSeq;
289 }
290 }
291}
292
294InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI) {
296 generateInstSeqImpl(Val, STI, Res);
297
298 // If the low 12 bits are non-zero, the first expansion may end with an ADDI
299 // or ADDIW. If there are trailing zeros, try generating a sign extended
300 // constant with no trailing zeros and use a final SLLI to restore them.
301 if ((Val & 0xfff) != 0 && (Val & 1) == 0 && Res.size() >= 2) {
302 unsigned TrailingZeros = llvm::countr_zero((uint64_t)Val);
303 int64_t ShiftedVal = Val >> TrailingZeros;
304 // If we can use C.LI+C.SLLI instead of LUI+ADDI(W) prefer that since
305 // its more compressible. But only if LUI+ADDI(W) isn't fusable.
306 // NOTE: We don't check for C extension to minimize differences in generated
307 // code.
308 bool IsShiftedCompressible =
309 isInt<6>(ShiftedVal) && !STI.hasFeature(RISCV::TuneLUIADDIFusion);
311 generateInstSeqImpl(ShiftedVal, STI, TmpSeq);
312
313 // Keep the new sequence if it is an improvement.
314 if ((TmpSeq.size() + 1) < Res.size() || IsShiftedCompressible) {
315 TmpSeq.emplace_back(RISCV::SLLI, TrailingZeros);
316 Res = TmpSeq;
317 }
318 }
319
320 // If we have a 1 or 2 instruction sequence this is the best we can do. This
321 // will always be true for RV32 and will often be true for RV64.
322 if (Res.size() <= 2)
323 return Res;
324
325 assert(STI.hasFeature(RISCV::Feature64Bit) &&
326 "Expected RV32 to only need 2 instructions");
327
328 // If the lower 13 bits are something like 0x17ff, try to add 1 to change the
329 // lower 13 bits to 0x1800. We can restore this with an ADDI of -1 at the end
330 // of the sequence. Call generateInstSeqImpl on the new constant which may
331 // subtract 0xfffffffffffff800 to create another ADDI. This will leave a
332 // constant with more than 12 trailing zeros for the next recursive step.
333 if ((Val & 0xfff) != 0 && (Val & 0x1800) == 0x1000) {
334 int64_t Imm12 = -(0x800 - (Val & 0xfff));
335 int64_t AdjustedVal = Val - Imm12;
337 generateInstSeqImpl(AdjustedVal, STI, TmpSeq);
338
339 // Keep the new sequence if it is an improvement.
340 if ((TmpSeq.size() + 1) < Res.size()) {
341 TmpSeq.emplace_back(RISCV::ADDI, Imm12);
342 Res = TmpSeq;
343 }
344 }
345
346 // If the constant is positive we might be able to generate a shifted constant
347 // with no leading zeros and use a final SRLI to restore them.
348 if (Val > 0 && Res.size() > 2) {
349 generateInstSeqLeadingZeros(Val, STI, Res);
350 }
351
352 // If the constant is negative, trying inverting and using our trailing zero
353 // optimizations. Use an xori to invert the final value.
354 if (Val < 0 && Res.size() > 3) {
355 uint64_t InvertedVal = ~(uint64_t)Val;
357 generateInstSeqLeadingZeros(InvertedVal, STI, TmpSeq);
358
359 // Keep it if we found a sequence that is smaller after inverting.
360 if (!TmpSeq.empty() && (TmpSeq.size() + 1) < Res.size()) {
361 TmpSeq.emplace_back(RISCV::XORI, -1);
362 Res = TmpSeq;
363 }
364 }
365
366 // If the Low and High halves are the same, use pack. The pack instruction
367 // packs the XLEN/2-bit lower halves of rs1 and rs2 into rd, with rs1 in the
368 // lower half and rs2 in the upper half.
369 if (Res.size() > 2 && (STI.hasFeature(RISCV::FeatureStdExtZbkb) ||
370 STI.hasFeature(RISCV::FeatureStdExtP))) {
371 int64_t LoVal = SignExtend64<32>(Val);
372 int64_t HiVal = SignExtend64<32>(Val >> 32);
373 if (LoVal == HiVal) {
375 generateInstSeqImpl(LoVal, STI, TmpSeq);
376 if ((TmpSeq.size() + 1) < Res.size()) {
377 TmpSeq.emplace_back(RISCV::PACK, 0);
378 Res = TmpSeq;
379 }
380 }
381 }
382
383 // Perform optimization with BSETI in the Zbs extension.
384 if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZbs)) {
385 // Create a simm32 value for LUI+ADDI(W) by forcing the upper 33 bits to
386 // zero. Xor that with original value to get which bits should be set by
387 // BSETI.
388 uint64_t Lo = Val & 0x7fffffff;
389 uint64_t Hi = Val ^ Lo;
390 assert(Hi != 0);
392
393 if (Lo != 0)
394 generateInstSeqImpl(Lo, STI, TmpSeq);
395
396 if (TmpSeq.size() + llvm::popcount(Hi) < Res.size()) {
397 do {
398 TmpSeq.emplace_back(RISCV::BSETI, llvm::countr_zero(Hi));
399 Hi &= (Hi - 1); // Clear lowest set bit.
400 } while (Hi != 0);
401 Res = TmpSeq;
402 }
403
404 // Fold LI 1 + SLLI into BSETI.
405 if (Res[0].getOpcode() == RISCV::ADDI && Res[0].getImm() == 1 &&
406 Res[1].getOpcode() == RISCV::SLLI) {
407 Res.erase(Res.begin()); // Remove ADDI.
408 Res.front() = Inst(RISCV::BSETI, Res.front().getImm()); // Patch SLLI.
409 }
410 }
411
412 // Perform optimization with BCLRI in the Zbs extension.
413 if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZbs)) {
414 // Create a simm32 value for LUI+ADDI(W) by forcing the upper 33 bits to
415 // one. Xor that with original value to get which bits should be cleared by
416 // BCLRI.
417 uint64_t Lo = Val | 0xffffffff80000000;
418 uint64_t Hi = Val ^ Lo;
419 assert(Hi != 0);
420
422 generateInstSeqImpl(Lo, STI, TmpSeq);
423
424 if (TmpSeq.size() + llvm::popcount(Hi) < Res.size()) {
425 do {
426 TmpSeq.emplace_back(RISCV::BCLRI, llvm::countr_zero(Hi));
427 Hi &= (Hi - 1); // Clear lowest set bit.
428 } while (Hi != 0);
429 Res = TmpSeq;
430 }
431 }
432
433 // Perform optimization with SH*ADD in the Zba extension.
434 if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZba)) {
435 int64_t Div = 0;
436 unsigned Opc = 0;
438 // Select the opcode and divisor.
439 if ((Val % 3) == 0 && isInt<32>(Val / 3)) {
440 Div = 3;
441 Opc = RISCV::SH1ADD;
442 } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) {
443 Div = 5;
444 Opc = RISCV::SH2ADD;
445 } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) {
446 Div = 9;
447 Opc = RISCV::SH3ADD;
448 }
449 // Build the new instruction sequence.
450 if (Div > 0) {
451 generateInstSeqImpl(Val / Div, STI, TmpSeq);
452 if ((TmpSeq.size() + 1) < Res.size()) {
453 TmpSeq.emplace_back(Opc, 0);
454 Res = TmpSeq;
455 }
456 } else {
457 // Try to use LUI+SH*ADD+ADDI.
458 int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull;
459 int64_t Lo12 = SignExtend64<12>(Val);
460 Div = 0;
461 if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) {
462 Div = 3;
463 Opc = RISCV::SH1ADD;
464 } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) {
465 Div = 5;
466 Opc = RISCV::SH2ADD;
467 } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) {
468 Div = 9;
469 Opc = RISCV::SH3ADD;
470 }
471 // Build the new instruction sequence.
472 if (Div > 0) {
473 // For Val that has zero Lo12 (implies Val equals to Hi52) should has
474 // already been processed to LUI+SH*ADD by previous optimization.
475 assert(Lo12 != 0 &&
476 "unexpected instruction sequence for immediate materialisation");
477 assert(TmpSeq.empty() && "Expected empty TmpSeq");
478 generateInstSeqImpl(Hi52 / Div, STI, TmpSeq);
479 if ((TmpSeq.size() + 2) < Res.size()) {
480 TmpSeq.emplace_back(Opc, 0);
481 TmpSeq.emplace_back(RISCV::ADDI, Lo12);
482 Res = TmpSeq;
483 }
484 }
485 }
486 }
487
488 // Perform optimization with rori in the Zbb and th.srri in the XTheadBb
489 // extension.
490 if (Res.size() > 2 && (STI.hasFeature(RISCV::FeatureStdExtZbb) ||
491 STI.hasFeature(RISCV::FeatureVendorXTHeadBb))) {
492 if (unsigned Rotate = extractRotateInfo(Val)) {
494 uint64_t NegImm12 = llvm::rotl<uint64_t>(Val, Rotate);
495 assert(isInt<12>(NegImm12));
496 TmpSeq.emplace_back(RISCV::ADDI, NegImm12);
497 TmpSeq.emplace_back(STI.hasFeature(RISCV::FeatureStdExtZbb)
498 ? RISCV::RORI
499 : RISCV::TH_SRRI,
500 Rotate);
501 Res = TmpSeq;
502 }
503 }
504 return Res;
505}
506
507void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI,
508 MCRegister DestReg, SmallVectorImpl<MCInst> &Insts) {
510
511 MCRegister SrcReg = RISCV::X0;
512 for (RISCVMatInt::Inst &Inst : Seq) {
513 switch (Inst.getOpndKind()) {
514 case RISCVMatInt::Imm:
516 .addReg(DestReg)
517 .addImm(Inst.getImm()));
518 break;
521 .addReg(DestReg)
522 .addReg(SrcReg)
523 .addReg(RISCV::X0));
524 break;
527 .addReg(DestReg)
528 .addReg(SrcReg)
529 .addReg(SrcReg));
530 break;
533 .addReg(DestReg)
534 .addReg(SrcReg)
535 .addImm(Inst.getImm()));
536 break;
537 }
538
539 // Only the first instruction has X0 as its source.
540 SrcReg = DestReg;
541 }
542}
543
545 unsigned &ShiftAmt, unsigned &AddOpc) {
546 int64_t LoVal = SignExtend64<32>(Val);
547 if (LoVal == 0)
548 return RISCVMatInt::InstSeq();
549
550 // Subtract the LoVal to emulate the effect of the final ADD.
551 uint64_t Tmp = (uint64_t)Val - (uint64_t)LoVal;
552 assert(Tmp != 0);
553
554 // Use trailing zero counts to figure how far we need to shift LoVal to line
555 // up with the remaining constant.
556 // TODO: This algorithm assumes all non-zero bits in the low 32 bits of the
557 // final constant come from LoVal.
558 unsigned TzLo = llvm::countr_zero((uint64_t)LoVal);
559 unsigned TzHi = llvm::countr_zero(Tmp);
560 assert(TzLo < 32 && TzHi >= 32);
561 ShiftAmt = TzHi - TzLo;
562 AddOpc = RISCV::ADD;
563
564 if (Tmp == ((uint64_t)LoVal << ShiftAmt))
565 return RISCVMatInt::generateInstSeq(LoVal, STI);
566
567 // If we have Zba, we can use (ADD_UW X, (SLLI X, 32)).
568 if (STI.hasFeature(RISCV::FeatureStdExtZba) && Lo_32(Val) == Hi_32(Val)) {
569 ShiftAmt = 32;
570 AddOpc = RISCV::ADD_UW;
571 return RISCVMatInt::generateInstSeq(LoVal, STI);
572 }
573
574 return RISCVMatInt::InstSeq();
575}
576
577int getIntMatCost(const APInt &Val, unsigned Size, const MCSubtargetInfo &STI,
578 bool CompressionCost, bool FreeZeroes) {
579 bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit);
580 bool HasRVC = CompressionCost && STI.hasFeature(RISCV::FeatureStdExtZca);
581 int PlatRegSize = IsRV64 ? 64 : 32;
582
583 // Split the constant into platform register sized chunks, and calculate cost
584 // of each chunk.
585 int Cost = 0;
586 for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) {
587 APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize);
588 if (FreeZeroes && Chunk.getSExtValue() == 0)
589 continue;
590 InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), STI);
591 Cost += getInstSeqCost(MatSeq, HasRVC);
592 }
593 return std::max(FreeZeroes ? 0 : 1, Cost);
594}
595
597 switch (Opc) {
598 default:
599 llvm_unreachable("Unexpected opcode!");
600 case RISCV::LUI:
601 case RISCV::QC_LI:
602 case RISCV::QC_E_LI:
603 case RISCV::PLI_B:
604 case RISCV::PLI_H:
605 case RISCV::PLI_W:
606 case RISCV::PLUI_H:
607 case RISCV::PLUI_W:
608 return RISCVMatInt::Imm;
609 case RISCV::ADD_UW:
610 return RISCVMatInt::RegX0;
611 case RISCV::SH1ADD:
612 case RISCV::SH2ADD:
613 case RISCV::SH3ADD:
614 case RISCV::PACK:
615 return RISCVMatInt::RegReg;
616 case RISCV::ADDI:
617 case RISCV::ADDIW:
618 case RISCV::XORI:
619 case RISCV::SLLI:
620 case RISCV::SRLI:
621 case RISCV::SLLI_UW:
622 case RISCV::RORI:
623 case RISCV::BSETI:
624 case RISCV::BCLRI:
625 case RISCV::TH_SRRI:
626 return RISCVMatInt::RegImm;
627 }
628}
629
630} // namespace llvm::RISCVMatInt
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
static void generateInstSeqLeadingZeros(int64_t Val, const MCSubtargetInfo &STI, RISCVMatInt::InstSeq &Res)
static void generateInstSeqImpl(int64_t Val, const MCSubtargetInfo &STI, RISCVMatInt::InstSeq &Res)
static unsigned extractRotateInfo(int64_t Val)
static int getInstSeqCost(RISCVMatInt::InstSeq &Res, bool HasRVC)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1083
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1585
MCInstBuilder & addReg(MCRegister Reg)
Add a new register operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
int64_t getImm() const
Definition RISCVMatInt.h:39
unsigned getOpcode() const
Definition RISCVMatInt.h:38
OpndKind getOpndKind() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI)
int getIntMatCost(const APInt &Val, unsigned Size, const MCSubtargetInfo &STI, bool CompressionCost, bool FreeZeroes)
InstSeq generateTwoRegInstSeq(int64_t Val, const MCSubtargetInfo &STI, unsigned &ShiftAmt, unsigned &AddOpc)
SmallVector< Inst, 8 > InstSeq
Definition RISCVMatInt.h:43
void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI, MCRegister DestReg, SmallVectorImpl< MCInst > &Insts)
This is an optimization pass for GlobalISel generic memory operations.
InstructionCost Cost
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition MathExtras.h:150
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
Definition bit.h:302
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition MathExtras.h:155
constexpr T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
Definition MathExtras.h:94
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:182
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:77
constexpr T rotl(T V, int R)
Definition bit.h:386