xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1e8d8bef9SDimitry Andric //===- RISCVMatInt.cpp - Immediate materialisation -------------*- C++ -*--===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric 
9e8d8bef9SDimitry Andric #include "RISCVMatInt.h"
10e8d8bef9SDimitry Andric #include "MCTargetDesc/RISCVMCTargetDesc.h"
11e8d8bef9SDimitry Andric #include "llvm/ADT/APInt.h"
12e8d8bef9SDimitry Andric #include "llvm/Support/MathExtras.h"
13fe6060f1SDimitry Andric using namespace llvm;
14e8d8bef9SDimitry Andric 
15fe6060f1SDimitry Andric static int getInstSeqCost(RISCVMatInt::InstSeq &Res, bool HasRVC) {
16fe6060f1SDimitry Andric   if (!HasRVC)
17fe6060f1SDimitry Andric     return Res.size();
18e8d8bef9SDimitry Andric 
19fe6060f1SDimitry Andric   int Cost = 0;
20fe6060f1SDimitry Andric   for (auto Instr : Res) {
2181ad6265SDimitry Andric     // Assume instructions that aren't listed aren't compressible.
2281ad6265SDimitry Andric     bool Compressed = false;
23bdd1243dSDimitry Andric     switch (Instr.getOpcode()) {
24fe6060f1SDimitry Andric     case RISCV::SLLI:
25fe6060f1SDimitry Andric     case RISCV::SRLI:
26fe6060f1SDimitry Andric       Compressed = true;
27fe6060f1SDimitry Andric       break;
28fe6060f1SDimitry Andric     case RISCV::ADDI:
29fe6060f1SDimitry Andric     case RISCV::ADDIW:
30fe6060f1SDimitry Andric     case RISCV::LUI:
31bdd1243dSDimitry Andric       Compressed = isInt<6>(Instr.getImm());
32fe6060f1SDimitry Andric       break;
33fe6060f1SDimitry Andric     }
34fe6060f1SDimitry Andric     // Two RVC instructions take the same space as one RVI instruction, but
35fe6060f1SDimitry Andric     // can take longer to execute than the single RVI instruction. Thus, we
36fe6060f1SDimitry Andric     // consider that two RVC instruction are slightly more costly than one
37fe6060f1SDimitry Andric     // RVI instruction. For longer sequences of RVC instructions the space
38fe6060f1SDimitry Andric     // savings can be worth it, though. The costs below try to model that.
39fe6060f1SDimitry Andric     if (!Compressed)
40fe6060f1SDimitry Andric       Cost += 100; // Baseline cost of one RVI instruction: 100%.
41fe6060f1SDimitry Andric     else
42fe6060f1SDimitry Andric       Cost += 70; // 70% cost of baseline.
43fe6060f1SDimitry Andric   }
44fe6060f1SDimitry Andric   return Cost;
45fe6060f1SDimitry Andric }
46fe6060f1SDimitry Andric 
47fe6060f1SDimitry Andric // Recursively generate a sequence for materializing an integer.
48fe6060f1SDimitry Andric static void generateInstSeqImpl(int64_t Val,
49fe6060f1SDimitry Andric                                 const FeatureBitset &ActiveFeatures,
50fe6060f1SDimitry Andric                                 RISCVMatInt::InstSeq &Res) {
51fe6060f1SDimitry Andric   bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit];
52fe6060f1SDimitry Andric 
53bdd1243dSDimitry Andric   // Use BSETI for a single bit that can't be expressed by a single LUI or ADDI.
54bdd1243dSDimitry Andric   if (ActiveFeatures[RISCV::FeatureStdExtZbs] && isPowerOf2_64(Val) &&
55bdd1243dSDimitry Andric       (!isInt<32>(Val) || Val == 0x800)) {
56bdd1243dSDimitry Andric     Res.emplace_back(RISCV::BSETI, Log2_64(Val));
57bdd1243dSDimitry Andric     return;
58bdd1243dSDimitry Andric   }
59bdd1243dSDimitry Andric 
60e8d8bef9SDimitry Andric   if (isInt<32>(Val)) {
61e8d8bef9SDimitry Andric     // Depending on the active bits in the immediate Value v, the following
62e8d8bef9SDimitry Andric     // instruction sequences are emitted:
63e8d8bef9SDimitry Andric     //
64e8d8bef9SDimitry Andric     // v == 0                        : ADDI
65e8d8bef9SDimitry Andric     // v[0,12) != 0 && v[12,32) == 0 : ADDI
66e8d8bef9SDimitry Andric     // v[0,12) == 0 && v[12,32) != 0 : LUI
67e8d8bef9SDimitry Andric     // v[0,32) != 0                  : LUI+ADDI(W)
68e8d8bef9SDimitry Andric     int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF;
69e8d8bef9SDimitry Andric     int64_t Lo12 = SignExtend64<12>(Val);
70e8d8bef9SDimitry Andric 
71e8d8bef9SDimitry Andric     if (Hi20)
72bdd1243dSDimitry Andric       Res.emplace_back(RISCV::LUI, Hi20);
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric     if (Lo12 || Hi20 == 0) {
75e8d8bef9SDimitry Andric       unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI;
76bdd1243dSDimitry Andric       Res.emplace_back(AddiOpc, Lo12);
77e8d8bef9SDimitry Andric     }
78e8d8bef9SDimitry Andric     return;
79e8d8bef9SDimitry Andric   }
80e8d8bef9SDimitry Andric 
81e8d8bef9SDimitry Andric   assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target");
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric   // In the worst case, for a full 64-bit constant, a sequence of 8 instructions
84349cc55cSDimitry Andric   // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emitted. Note
85e8d8bef9SDimitry Andric   // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits
86e8d8bef9SDimitry Andric   // while the following ADDI instructions contribute up to 12 bits each.
87e8d8bef9SDimitry Andric   //
88e8d8bef9SDimitry Andric   // On the first glance, implementing this seems to be possible by simply
89e8d8bef9SDimitry Andric   // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left
90e8d8bef9SDimitry Andric   // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the
91e8d8bef9SDimitry Andric   // fact that ADDI performs a sign extended addition, doing it like that would
92e8d8bef9SDimitry Andric   // only be possible when at most 11 bits of the ADDI instructions are used.
93e8d8bef9SDimitry Andric   // Using all 12 bits of the ADDI instructions, like done by GAS, actually
94e8d8bef9SDimitry Andric   // requires that the constant is processed starting with the least significant
95e8d8bef9SDimitry Andric   // bit.
96e8d8bef9SDimitry Andric   //
97e8d8bef9SDimitry Andric   // In the following, constants are processed from LSB to MSB but instruction
98e8d8bef9SDimitry Andric   // emission is performed from MSB to LSB by recursively calling
99e8d8bef9SDimitry Andric   // generateInstSeq. In each recursion, first the lowest 12 bits are removed
100e8d8bef9SDimitry Andric   // from the constant and the optimal shift amount, which can be greater than
101e8d8bef9SDimitry Andric   // 12 bits if the constant is sparse, is determined. Then, the shifted
102e8d8bef9SDimitry Andric   // remaining constant is processed recursively and gets emitted as soon as it
103e8d8bef9SDimitry Andric   // fits into 32 bits. The emission of the shifts and additions is subsequently
104e8d8bef9SDimitry Andric   // performed when the recursion returns.
105e8d8bef9SDimitry Andric 
106e8d8bef9SDimitry Andric   int64_t Lo12 = SignExtend64<12>(Val);
10781ad6265SDimitry Andric   Val = (uint64_t)Val - (uint64_t)Lo12;
10881ad6265SDimitry Andric 
10981ad6265SDimitry Andric   int ShiftAmount = 0;
11081ad6265SDimitry Andric   bool Unsigned = false;
11181ad6265SDimitry Andric 
11281ad6265SDimitry Andric   // Val might now be valid for LUI without needing a shift.
11381ad6265SDimitry Andric   if (!isInt<32>(Val)) {
114bdd1243dSDimitry Andric     ShiftAmount = llvm::countr_zero((uint64_t)Val);
11581ad6265SDimitry Andric     Val >>= ShiftAmount;
116e8d8bef9SDimitry Andric 
117fe6060f1SDimitry Andric     // If the remaining bits don't fit in 12 bits, we might be able to reduce the
118fe6060f1SDimitry Andric     // shift amount in order to use LUI which will zero the lower 12 bits.
11981ad6265SDimitry Andric     if (ShiftAmount > 12 && !isInt<12>(Val)) {
12081ad6265SDimitry Andric       if (isInt<32>((uint64_t)Val << 12)) {
121fe6060f1SDimitry Andric         // Reduce the shift amount and add zeros to the LSBs so it will match LUI.
122fe6060f1SDimitry Andric         ShiftAmount -= 12;
12381ad6265SDimitry Andric         Val = (uint64_t)Val << 12;
12481ad6265SDimitry Andric       } else if (isUInt<32>((uint64_t)Val << 12) &&
125349cc55cSDimitry Andric                  ActiveFeatures[RISCV::FeatureStdExtZba]) {
126349cc55cSDimitry Andric         // Reduce the shift amount and add zeros to the LSBs so it will match
127349cc55cSDimitry Andric         // LUI, then shift left with SLLI.UW to clear the upper 32 set bits.
128349cc55cSDimitry Andric         ShiftAmount -= 12;
12981ad6265SDimitry Andric         Val = ((uint64_t)Val << 12) | (0xffffffffull << 32);
130349cc55cSDimitry Andric         Unsigned = true;
131349cc55cSDimitry Andric       }
132349cc55cSDimitry Andric     }
133349cc55cSDimitry Andric 
13481ad6265SDimitry Andric     // Try to use SLLI_UW for Val when it is uint32 but not int32.
13581ad6265SDimitry Andric     if (isUInt<32>((uint64_t)Val) && !isInt<32>((uint64_t)Val) &&
136349cc55cSDimitry Andric         ActiveFeatures[RISCV::FeatureStdExtZba]) {
1371fd87a68SDimitry Andric       // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with
1381fd87a68SDimitry Andric       // SLLI_UW.
13981ad6265SDimitry Andric       Val = ((uint64_t)Val) | (0xffffffffull << 32);
140349cc55cSDimitry Andric       Unsigned = true;
141e8d8bef9SDimitry Andric     }
14281ad6265SDimitry Andric   }
143e8d8bef9SDimitry Andric 
14481ad6265SDimitry Andric   generateInstSeqImpl(Val, ActiveFeatures, Res);
145fe6060f1SDimitry Andric 
14681ad6265SDimitry Andric   // Skip shift if we were able to use LUI directly.
14781ad6265SDimitry Andric   if (ShiftAmount) {
148bdd1243dSDimitry Andric     unsigned Opc = Unsigned ? RISCV::SLLI_UW : RISCV::SLLI;
149bdd1243dSDimitry Andric     Res.emplace_back(Opc, ShiftAmount);
15081ad6265SDimitry Andric   }
15181ad6265SDimitry Andric 
152fe6060f1SDimitry Andric   if (Lo12)
153bdd1243dSDimitry Andric     Res.emplace_back(RISCV::ADDI, Lo12);
154fe6060f1SDimitry Andric }
155fe6060f1SDimitry Andric 
15604eeddc0SDimitry Andric static unsigned extractRotateInfo(int64_t Val) {
15704eeddc0SDimitry Andric   // for case: 0b111..1..xxxxxx1..1..
158*06c3fb27SDimitry Andric   unsigned LeadingOnes = llvm::countl_one((uint64_t)Val);
159*06c3fb27SDimitry Andric   unsigned TrailingOnes = llvm::countr_one((uint64_t)Val);
16004eeddc0SDimitry Andric   if (TrailingOnes > 0 && TrailingOnes < 64 &&
16104eeddc0SDimitry Andric       (LeadingOnes + TrailingOnes) > (64 - 12))
16204eeddc0SDimitry Andric     return 64 - TrailingOnes;
16304eeddc0SDimitry Andric 
16404eeddc0SDimitry Andric   // for case: 0bxxx1..1..1...xxx
165*06c3fb27SDimitry Andric   unsigned UpperTrailingOnes = llvm::countr_one(Hi_32(Val));
166*06c3fb27SDimitry Andric   unsigned LowerLeadingOnes = llvm::countl_one(Lo_32(Val));
16704eeddc0SDimitry Andric   if (UpperTrailingOnes < 32 &&
16804eeddc0SDimitry Andric       (UpperTrailingOnes + LowerLeadingOnes) > (64 - 12))
16904eeddc0SDimitry Andric     return 32 - UpperTrailingOnes;
17004eeddc0SDimitry Andric 
17104eeddc0SDimitry Andric   return 0;
17204eeddc0SDimitry Andric }
17304eeddc0SDimitry Andric 
174bdd1243dSDimitry Andric namespace llvm::RISCVMatInt {
175fe6060f1SDimitry Andric InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) {
176fe6060f1SDimitry Andric   RISCVMatInt::InstSeq Res;
177fe6060f1SDimitry Andric   generateInstSeqImpl(Val, ActiveFeatures, Res);
178fe6060f1SDimitry Andric 
179bdd1243dSDimitry Andric   // If the low 12 bits are non-zero, the first expansion may end with an ADDI
180bdd1243dSDimitry Andric   // or ADDIW. If there are trailing zeros, try generating a sign extended
181bdd1243dSDimitry Andric   // constant with no trailing zeros and use a final SLLI to restore them.
182bdd1243dSDimitry Andric   if ((Val & 0xfff) != 0 && (Val & 1) == 0 && Res.size() >= 2) {
183*06c3fb27SDimitry Andric     unsigned TrailingZeros = llvm::countr_zero((uint64_t)Val);
18481ad6265SDimitry Andric     int64_t ShiftedVal = Val >> TrailingZeros;
185bdd1243dSDimitry Andric     // If we can use C.LI+C.SLLI instead of LUI+ADDI(W) prefer that since
186bdd1243dSDimitry Andric     // its more compressible. But only if LUI+ADDI(W) isn't fusable.
187bdd1243dSDimitry Andric     // NOTE: We don't check for C extension to minimize differences in generated
188bdd1243dSDimitry Andric     // code.
189bdd1243dSDimitry Andric     bool IsShiftedCompressible =
190bdd1243dSDimitry Andric               isInt<6>(ShiftedVal) && !ActiveFeatures[RISCV::TuneLUIADDIFusion];
19181ad6265SDimitry Andric     RISCVMatInt::InstSeq TmpSeq;
19281ad6265SDimitry Andric     generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
19381ad6265SDimitry Andric 
19481ad6265SDimitry Andric     // Keep the new sequence if it is an improvement.
195*06c3fb27SDimitry Andric     if ((TmpSeq.size() + 1) < Res.size() || IsShiftedCompressible) {
196*06c3fb27SDimitry Andric       TmpSeq.emplace_back(RISCV::SLLI, TrailingZeros);
19781ad6265SDimitry Andric       Res = TmpSeq;
19881ad6265SDimitry Andric     }
199*06c3fb27SDimitry Andric   }
200*06c3fb27SDimitry Andric 
201*06c3fb27SDimitry Andric   // If we have a 1 or 2 instruction sequence this is the best we can do. This
202*06c3fb27SDimitry Andric   // will always be true for RV32 and will often be true for RV64.
203*06c3fb27SDimitry Andric   if (Res.size() <= 2)
204*06c3fb27SDimitry Andric     return Res;
205*06c3fb27SDimitry Andric 
206*06c3fb27SDimitry Andric   assert(ActiveFeatures[RISCV::Feature64Bit] &&
207*06c3fb27SDimitry Andric          "Expected RV32 to only need 2 instructions");
20881ad6265SDimitry Andric 
209fe6060f1SDimitry Andric   // If the constant is positive we might be able to generate a shifted constant
210fe6060f1SDimitry Andric   // with no leading zeros and use a final SRLI to restore them.
211*06c3fb27SDimitry Andric   if (Val > 0) {
212*06c3fb27SDimitry Andric     assert(Res.size() > 2 && "Expected longer sequence");
213*06c3fb27SDimitry Andric     unsigned LeadingZeros = llvm::countl_zero((uint64_t)Val);
214fe6060f1SDimitry Andric     uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros;
215fe6060f1SDimitry Andric     // Fill in the bits that will be shifted out with 1s. An example where this
216fe6060f1SDimitry Andric     // helps is trailing one masks with 32 or more ones. This will generate
217fe6060f1SDimitry Andric     // ADDI -1 and an SRLI.
218fe6060f1SDimitry Andric     ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros);
219fe6060f1SDimitry Andric 
220fe6060f1SDimitry Andric     RISCVMatInt::InstSeq TmpSeq;
221fe6060f1SDimitry Andric     generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
222fe6060f1SDimitry Andric 
223fe6060f1SDimitry Andric     // Keep the new sequence if it is an improvement.
224*06c3fb27SDimitry Andric     if ((TmpSeq.size() + 1) < Res.size()) {
225*06c3fb27SDimitry Andric       TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros);
226fe6060f1SDimitry Andric       Res = TmpSeq;
227*06c3fb27SDimitry Andric     }
228fe6060f1SDimitry Andric 
229fe6060f1SDimitry Andric     // Some cases can benefit from filling the lower bits with zeros instead.
230fe6060f1SDimitry Andric     ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros);
231fe6060f1SDimitry Andric     TmpSeq.clear();
232fe6060f1SDimitry Andric     generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
233fe6060f1SDimitry Andric 
234fe6060f1SDimitry Andric     // Keep the new sequence if it is an improvement.
235*06c3fb27SDimitry Andric     if ((TmpSeq.size() + 1) < Res.size()) {
236*06c3fb27SDimitry Andric       TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros);
237fe6060f1SDimitry Andric       Res = TmpSeq;
238*06c3fb27SDimitry Andric     }
239fe6060f1SDimitry Andric 
240fe6060f1SDimitry Andric     // If we have exactly 32 leading zeros and Zba, we can try using zext.w at
241fe6060f1SDimitry Andric     // the end of the sequence.
242349cc55cSDimitry Andric     if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureStdExtZba]) {
243fe6060f1SDimitry Andric       // Try replacing upper bits with 1.
244fe6060f1SDimitry Andric       uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros);
245fe6060f1SDimitry Andric       TmpSeq.clear();
246fe6060f1SDimitry Andric       generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq);
247fe6060f1SDimitry Andric 
248fe6060f1SDimitry Andric       // Keep the new sequence if it is an improvement.
249*06c3fb27SDimitry Andric       if ((TmpSeq.size() + 1) < Res.size()) {
250*06c3fb27SDimitry Andric         TmpSeq.emplace_back(RISCV::ADD_UW, 0);
251fe6060f1SDimitry Andric         Res = TmpSeq;
252fe6060f1SDimitry Andric       }
253fe6060f1SDimitry Andric     }
254*06c3fb27SDimitry Andric   }
255*06c3fb27SDimitry Andric 
256*06c3fb27SDimitry Andric   // If the Low and High halves are the same, use pack. The pack instruction
257*06c3fb27SDimitry Andric   // packs the XLEN/2-bit lower halves of rs1 and rs2 into rd, with rs1 in the
258*06c3fb27SDimitry Andric   // lower half and rs2 in the upper half.
259*06c3fb27SDimitry Andric   if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbkb]) {
260*06c3fb27SDimitry Andric     int64_t LoVal = SignExtend64<32>(Val);
261*06c3fb27SDimitry Andric     int64_t HiVal = SignExtend64<32>(Val >> 32);
262*06c3fb27SDimitry Andric     if (LoVal == HiVal) {
263*06c3fb27SDimitry Andric       RISCVMatInt::InstSeq TmpSeq;
264*06c3fb27SDimitry Andric       generateInstSeqImpl(LoVal, ActiveFeatures, TmpSeq);
265*06c3fb27SDimitry Andric       if ((TmpSeq.size() + 1) < Res.size()) {
266*06c3fb27SDimitry Andric         TmpSeq.emplace_back(RISCV::PACK, 0);
267*06c3fb27SDimitry Andric         Res = TmpSeq;
268*06c3fb27SDimitry Andric       }
269*06c3fb27SDimitry Andric     }
270*06c3fb27SDimitry Andric   }
271fe6060f1SDimitry Andric 
272349cc55cSDimitry Andric   // Perform optimization with BCLRI/BSETI in the Zbs extension.
273349cc55cSDimitry Andric   if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) {
274349cc55cSDimitry Andric     // 1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000,
275349cc55cSDimitry Andric     //    call generateInstSeqImpl with Val|0x80000000 (which is expected be
276349cc55cSDimitry Andric     //    an int32), then emit (BCLRI r, 31).
277349cc55cSDimitry Andric     // 2. For values in range 0x80000000 ~ 0xffffffff, call generateInstSeqImpl
278349cc55cSDimitry Andric     //    with Val&~0x80000000 (which is expected to be an int32), then
279349cc55cSDimitry Andric     //    emit (BSETI r, 31).
280349cc55cSDimitry Andric     int64_t NewVal;
281349cc55cSDimitry Andric     unsigned Opc;
282349cc55cSDimitry Andric     if (Val < 0) {
283349cc55cSDimitry Andric       Opc = RISCV::BCLRI;
284349cc55cSDimitry Andric       NewVal = Val | 0x80000000ll;
285349cc55cSDimitry Andric     } else {
286349cc55cSDimitry Andric       Opc = RISCV::BSETI;
287349cc55cSDimitry Andric       NewVal = Val & ~0x80000000ll;
288349cc55cSDimitry Andric     }
289349cc55cSDimitry Andric     if (isInt<32>(NewVal)) {
290349cc55cSDimitry Andric       RISCVMatInt::InstSeq TmpSeq;
291349cc55cSDimitry Andric       generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq);
292*06c3fb27SDimitry Andric       if ((TmpSeq.size() + 1) < Res.size()) {
293bdd1243dSDimitry Andric         TmpSeq.emplace_back(Opc, 31);
294349cc55cSDimitry Andric         Res = TmpSeq;
295349cc55cSDimitry Andric       }
296*06c3fb27SDimitry Andric     }
297349cc55cSDimitry Andric 
298349cc55cSDimitry Andric     // Try to use BCLRI for upper 32 bits if the original lower 32 bits are
299349cc55cSDimitry Andric     // negative int32, or use BSETI for upper 32 bits if the original lower
300349cc55cSDimitry Andric     // 32 bits are positive int32.
301bdd1243dSDimitry Andric     int32_t Lo = Lo_32(Val);
302bdd1243dSDimitry Andric     uint32_t Hi = Hi_32(Val);
303349cc55cSDimitry Andric     Opc = 0;
304349cc55cSDimitry Andric     RISCVMatInt::InstSeq TmpSeq;
305349cc55cSDimitry Andric     generateInstSeqImpl(Lo, ActiveFeatures, TmpSeq);
306349cc55cSDimitry Andric     // Check if it is profitable to use BCLRI/BSETI.
307bdd1243dSDimitry Andric     if (Lo > 0 && TmpSeq.size() + llvm::popcount(Hi) < Res.size()) {
308349cc55cSDimitry Andric       Opc = RISCV::BSETI;
309bdd1243dSDimitry Andric     } else if (Lo < 0 && TmpSeq.size() + llvm::popcount(~Hi) < Res.size()) {
310349cc55cSDimitry Andric       Opc = RISCV::BCLRI;
311349cc55cSDimitry Andric       Hi = ~Hi;
312349cc55cSDimitry Andric     }
313349cc55cSDimitry Andric     // Search for each bit and build corresponding BCLRI/BSETI.
314349cc55cSDimitry Andric     if (Opc > 0) {
315349cc55cSDimitry Andric       while (Hi != 0) {
316bdd1243dSDimitry Andric         unsigned Bit = llvm::countr_zero(Hi);
317bdd1243dSDimitry Andric         TmpSeq.emplace_back(Opc, Bit + 32);
318bdd1243dSDimitry Andric         Hi &= (Hi - 1); // Clear lowest set bit.
319349cc55cSDimitry Andric       }
320349cc55cSDimitry Andric       if (TmpSeq.size() < Res.size())
321349cc55cSDimitry Andric         Res = TmpSeq;
322349cc55cSDimitry Andric     }
323349cc55cSDimitry Andric   }
324349cc55cSDimitry Andric 
325349cc55cSDimitry Andric   // Perform optimization with SH*ADD in the Zba extension.
326349cc55cSDimitry Andric   if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZba]) {
327349cc55cSDimitry Andric     int64_t Div = 0;
328349cc55cSDimitry Andric     unsigned Opc = 0;
329349cc55cSDimitry Andric     RISCVMatInt::InstSeq TmpSeq;
330349cc55cSDimitry Andric     // Select the opcode and divisor.
331349cc55cSDimitry Andric     if ((Val % 3) == 0 && isInt<32>(Val / 3)) {
332349cc55cSDimitry Andric       Div = 3;
333349cc55cSDimitry Andric       Opc = RISCV::SH1ADD;
334349cc55cSDimitry Andric     } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) {
335349cc55cSDimitry Andric       Div = 5;
336349cc55cSDimitry Andric       Opc = RISCV::SH2ADD;
337349cc55cSDimitry Andric     } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) {
338349cc55cSDimitry Andric       Div = 9;
339349cc55cSDimitry Andric       Opc = RISCV::SH3ADD;
340349cc55cSDimitry Andric     }
341349cc55cSDimitry Andric     // Build the new instruction sequence.
342349cc55cSDimitry Andric     if (Div > 0) {
343349cc55cSDimitry Andric       generateInstSeqImpl(Val / Div, ActiveFeatures, TmpSeq);
344*06c3fb27SDimitry Andric       if ((TmpSeq.size() + 1) < Res.size()) {
345bdd1243dSDimitry Andric         TmpSeq.emplace_back(Opc, 0);
346349cc55cSDimitry Andric         Res = TmpSeq;
347*06c3fb27SDimitry Andric       }
3483a9a9c0cSDimitry Andric     } else {
349349cc55cSDimitry Andric       // Try to use LUI+SH*ADD+ADDI.
350349cc55cSDimitry Andric       int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull;
351349cc55cSDimitry Andric       int64_t Lo12 = SignExtend64<12>(Val);
352349cc55cSDimitry Andric       Div = 0;
353349cc55cSDimitry Andric       if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) {
354349cc55cSDimitry Andric         Div = 3;
355349cc55cSDimitry Andric         Opc = RISCV::SH1ADD;
356349cc55cSDimitry Andric       } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) {
357349cc55cSDimitry Andric         Div = 5;
358349cc55cSDimitry Andric         Opc = RISCV::SH2ADD;
359349cc55cSDimitry Andric       } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) {
360349cc55cSDimitry Andric         Div = 9;
361349cc55cSDimitry Andric         Opc = RISCV::SH3ADD;
362349cc55cSDimitry Andric       }
363349cc55cSDimitry Andric       // Build the new instruction sequence.
364349cc55cSDimitry Andric       if (Div > 0) {
365349cc55cSDimitry Andric         // For Val that has zero Lo12 (implies Val equals to Hi52) should has
366349cc55cSDimitry Andric         // already been processed to LUI+SH*ADD by previous optimization.
367349cc55cSDimitry Andric         assert(Lo12 != 0 &&
368349cc55cSDimitry Andric                "unexpected instruction sequence for immediate materialisation");
3693a9a9c0cSDimitry Andric         assert(TmpSeq.empty() && "Expected empty TmpSeq");
370349cc55cSDimitry Andric         generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq);
371*06c3fb27SDimitry Andric         if ((TmpSeq.size() + 2) < Res.size()) {
372bdd1243dSDimitry Andric           TmpSeq.emplace_back(Opc, 0);
373bdd1243dSDimitry Andric           TmpSeq.emplace_back(RISCV::ADDI, Lo12);
374349cc55cSDimitry Andric           Res = TmpSeq;
375349cc55cSDimitry Andric         }
376349cc55cSDimitry Andric       }
3773a9a9c0cSDimitry Andric     }
378*06c3fb27SDimitry Andric   }
379349cc55cSDimitry Andric 
380*06c3fb27SDimitry Andric   // Perform optimization with rori in the Zbb and th.srri in the XTheadBb
381*06c3fb27SDimitry Andric   // extension.
382*06c3fb27SDimitry Andric   if (Res.size() > 2 && (ActiveFeatures[RISCV::FeatureStdExtZbb] ||
383*06c3fb27SDimitry Andric                          ActiveFeatures[RISCV::FeatureVendorXTHeadBb])) {
38404eeddc0SDimitry Andric     if (unsigned Rotate = extractRotateInfo(Val)) {
38504eeddc0SDimitry Andric       RISCVMatInt::InstSeq TmpSeq;
386*06c3fb27SDimitry Andric       uint64_t NegImm12 = llvm::rotl<uint64_t>(Val, Rotate);
38704eeddc0SDimitry Andric       assert(isInt<12>(NegImm12));
388bdd1243dSDimitry Andric       TmpSeq.emplace_back(RISCV::ADDI, NegImm12);
389*06c3fb27SDimitry Andric       TmpSeq.emplace_back(ActiveFeatures[RISCV::FeatureStdExtZbb]
390*06c3fb27SDimitry Andric                               ? RISCV::RORI
391*06c3fb27SDimitry Andric                               : RISCV::TH_SRRI,
392*06c3fb27SDimitry Andric                           Rotate);
39304eeddc0SDimitry Andric       Res = TmpSeq;
39404eeddc0SDimitry Andric     }
39504eeddc0SDimitry Andric   }
396fe6060f1SDimitry Andric   return Res;
397fe6060f1SDimitry Andric }
398fe6060f1SDimitry Andric 
399fe6060f1SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size,
400349cc55cSDimitry Andric                   const FeatureBitset &ActiveFeatures, bool CompressionCost) {
401fe6060f1SDimitry Andric   bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit];
402bdd1243dSDimitry Andric   bool HasRVC = CompressionCost && (ActiveFeatures[RISCV::FeatureStdExtC] ||
403*06c3fb27SDimitry Andric                                     ActiveFeatures[RISCV::FeatureStdExtZca]);
404e8d8bef9SDimitry Andric   int PlatRegSize = IsRV64 ? 64 : 32;
405e8d8bef9SDimitry Andric 
406e8d8bef9SDimitry Andric   // Split the constant into platform register sized chunks, and calculate cost
407e8d8bef9SDimitry Andric   // of each chunk.
408e8d8bef9SDimitry Andric   int Cost = 0;
409e8d8bef9SDimitry Andric   for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) {
410e8d8bef9SDimitry Andric     APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize);
411fe6060f1SDimitry Andric     InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures);
412fe6060f1SDimitry Andric     Cost += getInstSeqCost(MatSeq, HasRVC);
413e8d8bef9SDimitry Andric   }
414e8d8bef9SDimitry Andric   return std::max(1, Cost);
415e8d8bef9SDimitry Andric }
41681ad6265SDimitry Andric 
41781ad6265SDimitry Andric OpndKind Inst::getOpndKind() const {
41881ad6265SDimitry Andric   switch (Opc) {
41981ad6265SDimitry Andric   default:
42081ad6265SDimitry Andric     llvm_unreachable("Unexpected opcode!");
42181ad6265SDimitry Andric   case RISCV::LUI:
42281ad6265SDimitry Andric     return RISCVMatInt::Imm;
42381ad6265SDimitry Andric   case RISCV::ADD_UW:
42481ad6265SDimitry Andric     return RISCVMatInt::RegX0;
42581ad6265SDimitry Andric   case RISCV::SH1ADD:
42681ad6265SDimitry Andric   case RISCV::SH2ADD:
42781ad6265SDimitry Andric   case RISCV::SH3ADD:
428*06c3fb27SDimitry Andric   case RISCV::PACK:
42981ad6265SDimitry Andric     return RISCVMatInt::RegReg;
43081ad6265SDimitry Andric   case RISCV::ADDI:
43181ad6265SDimitry Andric   case RISCV::ADDIW:
43281ad6265SDimitry Andric   case RISCV::SLLI:
43381ad6265SDimitry Andric   case RISCV::SRLI:
43481ad6265SDimitry Andric   case RISCV::SLLI_UW:
43581ad6265SDimitry Andric   case RISCV::RORI:
43681ad6265SDimitry Andric   case RISCV::BSETI:
43781ad6265SDimitry Andric   case RISCV::BCLRI:
438*06c3fb27SDimitry Andric   case RISCV::TH_SRRI:
43981ad6265SDimitry Andric     return RISCVMatInt::RegImm;
44081ad6265SDimitry Andric   }
44181ad6265SDimitry Andric }
44281ad6265SDimitry Andric 
443bdd1243dSDimitry Andric } // namespace llvm::RISCVMatInt
444