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) { 21fe6060f1SDimitry Andric bool Compressed; 22fe6060f1SDimitry Andric switch (Instr.Opc) { 23*349cc55cSDimitry Andric default: 24*349cc55cSDimitry Andric llvm_unreachable("Unexpected opcode"); 25fe6060f1SDimitry Andric case RISCV::SLLI: 26fe6060f1SDimitry Andric case RISCV::SRLI: 27fe6060f1SDimitry Andric Compressed = true; 28fe6060f1SDimitry Andric break; 29fe6060f1SDimitry Andric case RISCV::ADDI: 30fe6060f1SDimitry Andric case RISCV::ADDIW: 31fe6060f1SDimitry Andric case RISCV::LUI: 32fe6060f1SDimitry Andric Compressed = isInt<6>(Instr.Imm); 33fe6060f1SDimitry Andric break; 34fe6060f1SDimitry Andric case RISCV::ADDUW: 35fe6060f1SDimitry Andric Compressed = false; 36fe6060f1SDimitry Andric break; 37fe6060f1SDimitry Andric } 38fe6060f1SDimitry Andric // Two RVC instructions take the same space as one RVI instruction, but 39fe6060f1SDimitry Andric // can take longer to execute than the single RVI instruction. Thus, we 40fe6060f1SDimitry Andric // consider that two RVC instruction are slightly more costly than one 41fe6060f1SDimitry Andric // RVI instruction. For longer sequences of RVC instructions the space 42fe6060f1SDimitry Andric // savings can be worth it, though. The costs below try to model that. 43fe6060f1SDimitry Andric if (!Compressed) 44fe6060f1SDimitry Andric Cost += 100; // Baseline cost of one RVI instruction: 100%. 45fe6060f1SDimitry Andric else 46fe6060f1SDimitry Andric Cost += 70; // 70% cost of baseline. 47fe6060f1SDimitry Andric } 48fe6060f1SDimitry Andric return Cost; 49fe6060f1SDimitry Andric } 50fe6060f1SDimitry Andric 51fe6060f1SDimitry Andric // Recursively generate a sequence for materializing an integer. 52fe6060f1SDimitry Andric static void generateInstSeqImpl(int64_t Val, 53fe6060f1SDimitry Andric const FeatureBitset &ActiveFeatures, 54fe6060f1SDimitry Andric RISCVMatInt::InstSeq &Res) { 55fe6060f1SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit]; 56fe6060f1SDimitry Andric 57e8d8bef9SDimitry Andric if (isInt<32>(Val)) { 58e8d8bef9SDimitry Andric // Depending on the active bits in the immediate Value v, the following 59e8d8bef9SDimitry Andric // instruction sequences are emitted: 60e8d8bef9SDimitry Andric // 61e8d8bef9SDimitry Andric // v == 0 : ADDI 62e8d8bef9SDimitry Andric // v[0,12) != 0 && v[12,32) == 0 : ADDI 63e8d8bef9SDimitry Andric // v[0,12) == 0 && v[12,32) != 0 : LUI 64e8d8bef9SDimitry Andric // v[0,32) != 0 : LUI+ADDI(W) 65e8d8bef9SDimitry Andric int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF; 66e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 67e8d8bef9SDimitry Andric 68e8d8bef9SDimitry Andric if (Hi20) 69fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::LUI, Hi20)); 70e8d8bef9SDimitry Andric 71e8d8bef9SDimitry Andric if (Lo12 || Hi20 == 0) { 72e8d8bef9SDimitry Andric unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI; 73fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(AddiOpc, Lo12)); 74e8d8bef9SDimitry Andric } 75e8d8bef9SDimitry Andric return; 76e8d8bef9SDimitry Andric } 77e8d8bef9SDimitry Andric 78e8d8bef9SDimitry Andric assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target"); 79e8d8bef9SDimitry Andric 80e8d8bef9SDimitry Andric // In the worst case, for a full 64-bit constant, a sequence of 8 instructions 81*349cc55cSDimitry Andric // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emitted. Note 82e8d8bef9SDimitry Andric // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits 83e8d8bef9SDimitry Andric // while the following ADDI instructions contribute up to 12 bits each. 84e8d8bef9SDimitry Andric // 85e8d8bef9SDimitry Andric // On the first glance, implementing this seems to be possible by simply 86e8d8bef9SDimitry Andric // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left 87e8d8bef9SDimitry Andric // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the 88e8d8bef9SDimitry Andric // fact that ADDI performs a sign extended addition, doing it like that would 89e8d8bef9SDimitry Andric // only be possible when at most 11 bits of the ADDI instructions are used. 90e8d8bef9SDimitry Andric // Using all 12 bits of the ADDI instructions, like done by GAS, actually 91e8d8bef9SDimitry Andric // requires that the constant is processed starting with the least significant 92e8d8bef9SDimitry Andric // bit. 93e8d8bef9SDimitry Andric // 94e8d8bef9SDimitry Andric // In the following, constants are processed from LSB to MSB but instruction 95e8d8bef9SDimitry Andric // emission is performed from MSB to LSB by recursively calling 96e8d8bef9SDimitry Andric // generateInstSeq. In each recursion, first the lowest 12 bits are removed 97e8d8bef9SDimitry Andric // from the constant and the optimal shift amount, which can be greater than 98e8d8bef9SDimitry Andric // 12 bits if the constant is sparse, is determined. Then, the shifted 99e8d8bef9SDimitry Andric // remaining constant is processed recursively and gets emitted as soon as it 100e8d8bef9SDimitry Andric // fits into 32 bits. The emission of the shifts and additions is subsequently 101e8d8bef9SDimitry Andric // performed when the recursion returns. 102e8d8bef9SDimitry Andric 103e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 104e8d8bef9SDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) >> 12; 105e8d8bef9SDimitry Andric int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52); 106e8d8bef9SDimitry Andric Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount); 107e8d8bef9SDimitry Andric 108fe6060f1SDimitry Andric // If the remaining bits don't fit in 12 bits, we might be able to reduce the 109fe6060f1SDimitry Andric // shift amount in order to use LUI which will zero the lower 12 bits. 110*349cc55cSDimitry Andric bool Unsigned = false; 111*349cc55cSDimitry Andric if (ShiftAmount > 12 && !isInt<12>(Hi52)) { 112*349cc55cSDimitry Andric if (isInt<32>((uint64_t)Hi52 << 12)) { 113fe6060f1SDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match LUI. 114fe6060f1SDimitry Andric ShiftAmount -= 12; 115fe6060f1SDimitry Andric Hi52 = (uint64_t)Hi52 << 12; 116*349cc55cSDimitry Andric } else if (isUInt<32>((uint64_t)Hi52 << 12) && 117*349cc55cSDimitry Andric ActiveFeatures[RISCV::FeatureStdExtZba]) { 118*349cc55cSDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match 119*349cc55cSDimitry Andric // LUI, then shift left with SLLI.UW to clear the upper 32 set bits. 120*349cc55cSDimitry Andric ShiftAmount -= 12; 121*349cc55cSDimitry Andric Hi52 = ((uint64_t)Hi52 << 12) | (0xffffffffull << 32); 122*349cc55cSDimitry Andric Unsigned = true; 123*349cc55cSDimitry Andric } 124*349cc55cSDimitry Andric } 125*349cc55cSDimitry Andric 126*349cc55cSDimitry Andric // Try to use SLLIUW for Hi52 when it is uint32 but not int32. 127*349cc55cSDimitry Andric if (isUInt<32>((uint64_t)Hi52) && !isInt<32>((uint64_t)Hi52) && 128*349cc55cSDimitry Andric ActiveFeatures[RISCV::FeatureStdExtZba]) { 129*349cc55cSDimitry Andric // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with SLLIUW. 130*349cc55cSDimitry Andric Hi52 = ((uint64_t)Hi52) | (0xffffffffull << 32); 131*349cc55cSDimitry Andric Unsigned = true; 132e8d8bef9SDimitry Andric } 133e8d8bef9SDimitry Andric 134fe6060f1SDimitry Andric generateInstSeqImpl(Hi52, ActiveFeatures, Res); 135fe6060f1SDimitry Andric 136*349cc55cSDimitry Andric if (Unsigned) 137*349cc55cSDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLIUW, ShiftAmount)); 138*349cc55cSDimitry Andric else 139fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLI, ShiftAmount)); 140fe6060f1SDimitry Andric if (Lo12) 141fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12)); 142fe6060f1SDimitry Andric } 143fe6060f1SDimitry Andric 144fe6060f1SDimitry Andric namespace llvm { 145fe6060f1SDimitry Andric namespace RISCVMatInt { 146fe6060f1SDimitry Andric InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) { 147fe6060f1SDimitry Andric RISCVMatInt::InstSeq Res; 148fe6060f1SDimitry Andric generateInstSeqImpl(Val, ActiveFeatures, Res); 149fe6060f1SDimitry Andric 150fe6060f1SDimitry Andric // If the constant is positive we might be able to generate a shifted constant 151fe6060f1SDimitry Andric // with no leading zeros and use a final SRLI to restore them. 152fe6060f1SDimitry Andric if (Val > 0 && Res.size() > 2) { 153fe6060f1SDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 154fe6060f1SDimitry Andric "Expected RV32 to only need 2 instructions"); 155fe6060f1SDimitry Andric unsigned LeadingZeros = countLeadingZeros((uint64_t)Val); 156fe6060f1SDimitry Andric uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros; 157fe6060f1SDimitry Andric // Fill in the bits that will be shifted out with 1s. An example where this 158fe6060f1SDimitry Andric // helps is trailing one masks with 32 or more ones. This will generate 159fe6060f1SDimitry Andric // ADDI -1 and an SRLI. 160fe6060f1SDimitry Andric ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros); 161fe6060f1SDimitry Andric 162fe6060f1SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 163fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 164fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros)); 165fe6060f1SDimitry Andric 166fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 167fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 168fe6060f1SDimitry Andric Res = TmpSeq; 169fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 170fe6060f1SDimitry Andric if (Res.size() <= 2) 171fe6060f1SDimitry Andric return Res; 172fe6060f1SDimitry Andric } 173fe6060f1SDimitry Andric 174fe6060f1SDimitry Andric // Some cases can benefit from filling the lower bits with zeros instead. 175fe6060f1SDimitry Andric ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros); 176fe6060f1SDimitry Andric TmpSeq.clear(); 177fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 178fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros)); 179fe6060f1SDimitry Andric 180fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 181fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 182fe6060f1SDimitry Andric Res = TmpSeq; 183fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 184fe6060f1SDimitry Andric if (Res.size() <= 2) 185fe6060f1SDimitry Andric return Res; 186fe6060f1SDimitry Andric } 187fe6060f1SDimitry Andric 188fe6060f1SDimitry Andric // If we have exactly 32 leading zeros and Zba, we can try using zext.w at 189fe6060f1SDimitry Andric // the end of the sequence. 190*349cc55cSDimitry Andric if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 191fe6060f1SDimitry Andric // Try replacing upper bits with 1. 192fe6060f1SDimitry Andric uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros); 193fe6060f1SDimitry Andric TmpSeq.clear(); 194fe6060f1SDimitry Andric generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq); 195fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDUW, 0)); 196fe6060f1SDimitry Andric 197fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 198fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 199fe6060f1SDimitry Andric Res = TmpSeq; 200fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 201fe6060f1SDimitry Andric if (Res.size() <= 2) 202fe6060f1SDimitry Andric return Res; 203fe6060f1SDimitry Andric } 204fe6060f1SDimitry Andric } 205fe6060f1SDimitry Andric } 206fe6060f1SDimitry Andric 207*349cc55cSDimitry Andric // Perform optimization with BCLRI/BSETI in the Zbs extension. 208*349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) { 209*349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 210*349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 211*349cc55cSDimitry Andric 212*349cc55cSDimitry Andric // 1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000, 213*349cc55cSDimitry Andric // call generateInstSeqImpl with Val|0x80000000 (which is expected be 214*349cc55cSDimitry Andric // an int32), then emit (BCLRI r, 31). 215*349cc55cSDimitry Andric // 2. For values in range 0x80000000 ~ 0xffffffff, call generateInstSeqImpl 216*349cc55cSDimitry Andric // with Val&~0x80000000 (which is expected to be an int32), then 217*349cc55cSDimitry Andric // emit (BSETI r, 31). 218*349cc55cSDimitry Andric int64_t NewVal; 219*349cc55cSDimitry Andric unsigned Opc; 220*349cc55cSDimitry Andric if (Val < 0) { 221*349cc55cSDimitry Andric Opc = RISCV::BCLRI; 222*349cc55cSDimitry Andric NewVal = Val | 0x80000000ll; 223*349cc55cSDimitry Andric } else { 224*349cc55cSDimitry Andric Opc = RISCV::BSETI; 225*349cc55cSDimitry Andric NewVal = Val & ~0x80000000ll; 226*349cc55cSDimitry Andric } 227*349cc55cSDimitry Andric if (isInt<32>(NewVal)) { 228*349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 229*349cc55cSDimitry Andric generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq); 230*349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 31)); 231*349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 232*349cc55cSDimitry Andric Res = TmpSeq; 233*349cc55cSDimitry Andric } 234*349cc55cSDimitry Andric 235*349cc55cSDimitry Andric // Try to use BCLRI for upper 32 bits if the original lower 32 bits are 236*349cc55cSDimitry Andric // negative int32, or use BSETI for upper 32 bits if the original lower 237*349cc55cSDimitry Andric // 32 bits are positive int32. 238*349cc55cSDimitry Andric int32_t Lo = Val; 239*349cc55cSDimitry Andric uint32_t Hi = Val >> 32; 240*349cc55cSDimitry Andric Opc = 0; 241*349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 242*349cc55cSDimitry Andric generateInstSeqImpl(Lo, ActiveFeatures, TmpSeq); 243*349cc55cSDimitry Andric // Check if it is profitable to use BCLRI/BSETI. 244*349cc55cSDimitry Andric if (Lo > 0 && TmpSeq.size() + countPopulation(Hi) < Res.size()) { 245*349cc55cSDimitry Andric Opc = RISCV::BSETI; 246*349cc55cSDimitry Andric } else if (Lo < 0 && TmpSeq.size() + countPopulation(~Hi) < Res.size()) { 247*349cc55cSDimitry Andric Opc = RISCV::BCLRI; 248*349cc55cSDimitry Andric Hi = ~Hi; 249*349cc55cSDimitry Andric } 250*349cc55cSDimitry Andric // Search for each bit and build corresponding BCLRI/BSETI. 251*349cc55cSDimitry Andric if (Opc > 0) { 252*349cc55cSDimitry Andric while (Hi != 0) { 253*349cc55cSDimitry Andric unsigned Bit = countTrailingZeros(Hi); 254*349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, Bit + 32)); 255*349cc55cSDimitry Andric Hi &= ~(1 << Bit); 256*349cc55cSDimitry Andric } 257*349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 258*349cc55cSDimitry Andric Res = TmpSeq; 259*349cc55cSDimitry Andric } 260*349cc55cSDimitry Andric } 261*349cc55cSDimitry Andric 262*349cc55cSDimitry Andric // Perform optimization with SH*ADD in the Zba extension. 263*349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 264*349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 265*349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 266*349cc55cSDimitry Andric int64_t Div = 0; 267*349cc55cSDimitry Andric unsigned Opc = 0; 268*349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 269*349cc55cSDimitry Andric // Select the opcode and divisor. 270*349cc55cSDimitry Andric if ((Val % 3) == 0 && isInt<32>(Val / 3)) { 271*349cc55cSDimitry Andric Div = 3; 272*349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 273*349cc55cSDimitry Andric } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) { 274*349cc55cSDimitry Andric Div = 5; 275*349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 276*349cc55cSDimitry Andric } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) { 277*349cc55cSDimitry Andric Div = 9; 278*349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 279*349cc55cSDimitry Andric } 280*349cc55cSDimitry Andric // Build the new instruction sequence. 281*349cc55cSDimitry Andric if (Div > 0) { 282*349cc55cSDimitry Andric generateInstSeqImpl(Val / Div, ActiveFeatures, TmpSeq); 283*349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0)); 284*349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 285*349cc55cSDimitry Andric Res = TmpSeq; 286*349cc55cSDimitry Andric } 287*349cc55cSDimitry Andric // Try to use LUI+SH*ADD+ADDI. 288*349cc55cSDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull; 289*349cc55cSDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 290*349cc55cSDimitry Andric Div = 0; 291*349cc55cSDimitry Andric if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) { 292*349cc55cSDimitry Andric Div = 3; 293*349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 294*349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) { 295*349cc55cSDimitry Andric Div = 5; 296*349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 297*349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) { 298*349cc55cSDimitry Andric Div = 9; 299*349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 300*349cc55cSDimitry Andric } 301*349cc55cSDimitry Andric // Build the new instruction sequence. 302*349cc55cSDimitry Andric if (Div > 0) { 303*349cc55cSDimitry Andric // For Val that has zero Lo12 (implies Val equals to Hi52) should has 304*349cc55cSDimitry Andric // already been processed to LUI+SH*ADD by previous optimization. 305*349cc55cSDimitry Andric assert(Lo12 != 0 && 306*349cc55cSDimitry Andric "unexpected instruction sequence for immediate materialisation"); 307*349cc55cSDimitry Andric generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq); 308*349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0)); 309*349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12)); 310*349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 311*349cc55cSDimitry Andric Res = TmpSeq; 312*349cc55cSDimitry Andric } 313*349cc55cSDimitry Andric } 314*349cc55cSDimitry Andric 315fe6060f1SDimitry Andric return Res; 316fe6060f1SDimitry Andric } 317fe6060f1SDimitry Andric 318fe6060f1SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, 319*349cc55cSDimitry Andric const FeatureBitset &ActiveFeatures, bool CompressionCost) { 320fe6060f1SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit]; 321fe6060f1SDimitry Andric bool HasRVC = CompressionCost && ActiveFeatures[RISCV::FeatureStdExtC]; 322e8d8bef9SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32; 323e8d8bef9SDimitry Andric 324e8d8bef9SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost 325e8d8bef9SDimitry Andric // of each chunk. 326e8d8bef9SDimitry Andric int Cost = 0; 327e8d8bef9SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) { 328e8d8bef9SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize); 329fe6060f1SDimitry Andric InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures); 330fe6060f1SDimitry Andric Cost += getInstSeqCost(MatSeq, HasRVC); 331e8d8bef9SDimitry Andric } 332e8d8bef9SDimitry Andric return std::max(1, Cost); 333e8d8bef9SDimitry Andric } 334e8d8bef9SDimitry Andric } // namespace RISCVMatInt 335e8d8bef9SDimitry Andric } // namespace llvm 336