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) { 21*81ad6265SDimitry Andric // Assume instructions that aren't listed aren't compressible. 22*81ad6265SDimitry Andric bool Compressed = false; 23fe6060f1SDimitry Andric switch (Instr.Opc) { 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: 31fe6060f1SDimitry Andric Compressed = isInt<6>(Instr.Imm); 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 53e8d8bef9SDimitry Andric if (isInt<32>(Val)) { 54e8d8bef9SDimitry Andric // Depending on the active bits in the immediate Value v, the following 55e8d8bef9SDimitry Andric // instruction sequences are emitted: 56e8d8bef9SDimitry Andric // 57e8d8bef9SDimitry Andric // v == 0 : ADDI 58e8d8bef9SDimitry Andric // v[0,12) != 0 && v[12,32) == 0 : ADDI 59e8d8bef9SDimitry Andric // v[0,12) == 0 && v[12,32) != 0 : LUI 60e8d8bef9SDimitry Andric // v[0,32) != 0 : LUI+ADDI(W) 61e8d8bef9SDimitry Andric int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF; 62e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 63e8d8bef9SDimitry Andric 64e8d8bef9SDimitry Andric if (Hi20) 65fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::LUI, Hi20)); 66e8d8bef9SDimitry Andric 67e8d8bef9SDimitry Andric if (Lo12 || Hi20 == 0) { 68e8d8bef9SDimitry Andric unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI; 69fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(AddiOpc, Lo12)); 70e8d8bef9SDimitry Andric } 71e8d8bef9SDimitry Andric return; 72e8d8bef9SDimitry Andric } 73e8d8bef9SDimitry Andric 74e8d8bef9SDimitry Andric assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target"); 75e8d8bef9SDimitry Andric 76*81ad6265SDimitry Andric // Use BSETI for a single bit. 77*81ad6265SDimitry Andric if (ActiveFeatures[RISCV::FeatureStdExtZbs] && isPowerOf2_64(Val)) { 78*81ad6265SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::BSETI, Log2_64(Val))); 79*81ad6265SDimitry Andric return; 80*81ad6265SDimitry Andric } 81*81ad6265SDimitry Andric 82e8d8bef9SDimitry Andric // In the worst case, for a full 64-bit constant, a sequence of 8 instructions 83349cc55cSDimitry Andric // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emitted. Note 84e8d8bef9SDimitry Andric // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits 85e8d8bef9SDimitry Andric // while the following ADDI instructions contribute up to 12 bits each. 86e8d8bef9SDimitry Andric // 87e8d8bef9SDimitry Andric // On the first glance, implementing this seems to be possible by simply 88e8d8bef9SDimitry Andric // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left 89e8d8bef9SDimitry Andric // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the 90e8d8bef9SDimitry Andric // fact that ADDI performs a sign extended addition, doing it like that would 91e8d8bef9SDimitry Andric // only be possible when at most 11 bits of the ADDI instructions are used. 92e8d8bef9SDimitry Andric // Using all 12 bits of the ADDI instructions, like done by GAS, actually 93e8d8bef9SDimitry Andric // requires that the constant is processed starting with the least significant 94e8d8bef9SDimitry Andric // bit. 95e8d8bef9SDimitry Andric // 96e8d8bef9SDimitry Andric // In the following, constants are processed from LSB to MSB but instruction 97e8d8bef9SDimitry Andric // emission is performed from MSB to LSB by recursively calling 98e8d8bef9SDimitry Andric // generateInstSeq. In each recursion, first the lowest 12 bits are removed 99e8d8bef9SDimitry Andric // from the constant and the optimal shift amount, which can be greater than 100e8d8bef9SDimitry Andric // 12 bits if the constant is sparse, is determined. Then, the shifted 101e8d8bef9SDimitry Andric // remaining constant is processed recursively and gets emitted as soon as it 102e8d8bef9SDimitry Andric // fits into 32 bits. The emission of the shifts and additions is subsequently 103e8d8bef9SDimitry Andric // performed when the recursion returns. 104e8d8bef9SDimitry Andric 105e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 106*81ad6265SDimitry Andric Val = (uint64_t)Val - (uint64_t)Lo12; 107*81ad6265SDimitry Andric 108*81ad6265SDimitry Andric int ShiftAmount = 0; 109*81ad6265SDimitry Andric bool Unsigned = false; 110*81ad6265SDimitry Andric 111*81ad6265SDimitry Andric // Val might now be valid for LUI without needing a shift. 112*81ad6265SDimitry Andric if (!isInt<32>(Val)) { 113*81ad6265SDimitry Andric ShiftAmount = findFirstSet((uint64_t)Val); 114*81ad6265SDimitry Andric Val >>= ShiftAmount; 115e8d8bef9SDimitry Andric 116fe6060f1SDimitry Andric // If the remaining bits don't fit in 12 bits, we might be able to reduce the 117fe6060f1SDimitry Andric // shift amount in order to use LUI which will zero the lower 12 bits. 118*81ad6265SDimitry Andric if (ShiftAmount > 12 && !isInt<12>(Val)) { 119*81ad6265SDimitry Andric if (isInt<32>((uint64_t)Val << 12)) { 120fe6060f1SDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match LUI. 121fe6060f1SDimitry Andric ShiftAmount -= 12; 122*81ad6265SDimitry Andric Val = (uint64_t)Val << 12; 123*81ad6265SDimitry Andric } else if (isUInt<32>((uint64_t)Val << 12) && 124349cc55cSDimitry Andric ActiveFeatures[RISCV::FeatureStdExtZba]) { 125349cc55cSDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match 126349cc55cSDimitry Andric // LUI, then shift left with SLLI.UW to clear the upper 32 set bits. 127349cc55cSDimitry Andric ShiftAmount -= 12; 128*81ad6265SDimitry Andric Val = ((uint64_t)Val << 12) | (0xffffffffull << 32); 129349cc55cSDimitry Andric Unsigned = true; 130349cc55cSDimitry Andric } 131349cc55cSDimitry Andric } 132349cc55cSDimitry Andric 133*81ad6265SDimitry Andric // Try to use SLLI_UW for Val when it is uint32 but not int32. 134*81ad6265SDimitry Andric if (isUInt<32>((uint64_t)Val) && !isInt<32>((uint64_t)Val) && 135349cc55cSDimitry Andric ActiveFeatures[RISCV::FeatureStdExtZba]) { 1361fd87a68SDimitry Andric // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with 1371fd87a68SDimitry Andric // SLLI_UW. 138*81ad6265SDimitry Andric Val = ((uint64_t)Val) | (0xffffffffull << 32); 139349cc55cSDimitry Andric Unsigned = true; 140e8d8bef9SDimitry Andric } 141*81ad6265SDimitry Andric } 142e8d8bef9SDimitry Andric 143*81ad6265SDimitry Andric generateInstSeqImpl(Val, ActiveFeatures, Res); 144fe6060f1SDimitry Andric 145*81ad6265SDimitry Andric // Skip shift if we were able to use LUI directly. 146*81ad6265SDimitry Andric if (ShiftAmount) { 147349cc55cSDimitry Andric if (Unsigned) 1481fd87a68SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLI_UW, ShiftAmount)); 149349cc55cSDimitry Andric else 150fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLI, ShiftAmount)); 151*81ad6265SDimitry Andric } 152*81ad6265SDimitry Andric 153fe6060f1SDimitry Andric if (Lo12) 154fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12)); 155fe6060f1SDimitry Andric } 156fe6060f1SDimitry Andric 15704eeddc0SDimitry Andric static unsigned extractRotateInfo(int64_t Val) { 15804eeddc0SDimitry Andric // for case: 0b111..1..xxxxxx1..1.. 15904eeddc0SDimitry Andric unsigned LeadingOnes = countLeadingOnes((uint64_t)Val); 16004eeddc0SDimitry Andric unsigned TrailingOnes = countTrailingOnes((uint64_t)Val); 16104eeddc0SDimitry Andric if (TrailingOnes > 0 && TrailingOnes < 64 && 16204eeddc0SDimitry Andric (LeadingOnes + TrailingOnes) > (64 - 12)) 16304eeddc0SDimitry Andric return 64 - TrailingOnes; 16404eeddc0SDimitry Andric 16504eeddc0SDimitry Andric // for case: 0bxxx1..1..1...xxx 16604eeddc0SDimitry Andric unsigned UpperTrailingOnes = countTrailingOnes(Hi_32(Val)); 16704eeddc0SDimitry Andric unsigned LowerLeadingOnes = countLeadingOnes(Lo_32(Val)); 16804eeddc0SDimitry Andric if (UpperTrailingOnes < 32 && 16904eeddc0SDimitry Andric (UpperTrailingOnes + LowerLeadingOnes) > (64 - 12)) 17004eeddc0SDimitry Andric return 32 - UpperTrailingOnes; 17104eeddc0SDimitry Andric 17204eeddc0SDimitry Andric return 0; 17304eeddc0SDimitry Andric } 17404eeddc0SDimitry Andric 175fe6060f1SDimitry Andric namespace llvm { 176fe6060f1SDimitry Andric namespace RISCVMatInt { 177fe6060f1SDimitry Andric InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) { 178fe6060f1SDimitry Andric RISCVMatInt::InstSeq Res; 179fe6060f1SDimitry Andric generateInstSeqImpl(Val, ActiveFeatures, Res); 180fe6060f1SDimitry Andric 181*81ad6265SDimitry Andric // If there are trailing zeros, try generating a sign extended constant with 182*81ad6265SDimitry Andric // no trailing zeros and use a final SLLI to restore them. 183*81ad6265SDimitry Andric if ((Val & 1) == 0 && Res.size() > 2) { 184*81ad6265SDimitry Andric unsigned TrailingZeros = countTrailingZeros((uint64_t)Val); 185*81ad6265SDimitry Andric int64_t ShiftedVal = Val >> TrailingZeros; 186*81ad6265SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 187*81ad6265SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 188*81ad6265SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SLLI, TrailingZeros)); 189*81ad6265SDimitry Andric 190*81ad6265SDimitry Andric // Keep the new sequence if it is an improvement. 191*81ad6265SDimitry Andric if (TmpSeq.size() < Res.size()) { 192*81ad6265SDimitry Andric Res = TmpSeq; 193*81ad6265SDimitry Andric // A 2 instruction sequence is the best we can do. 194*81ad6265SDimitry Andric if (Res.size() <= 2) 195*81ad6265SDimitry Andric return Res; 196*81ad6265SDimitry Andric } 197*81ad6265SDimitry Andric } 198*81ad6265SDimitry Andric 199fe6060f1SDimitry Andric // If the constant is positive we might be able to generate a shifted constant 200fe6060f1SDimitry Andric // with no leading zeros and use a final SRLI to restore them. 201fe6060f1SDimitry Andric if (Val > 0 && Res.size() > 2) { 202fe6060f1SDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 203fe6060f1SDimitry Andric "Expected RV32 to only need 2 instructions"); 204fe6060f1SDimitry Andric unsigned LeadingZeros = countLeadingZeros((uint64_t)Val); 205fe6060f1SDimitry Andric uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros; 206fe6060f1SDimitry Andric // Fill in the bits that will be shifted out with 1s. An example where this 207fe6060f1SDimitry Andric // helps is trailing one masks with 32 or more ones. This will generate 208fe6060f1SDimitry Andric // ADDI -1 and an SRLI. 209fe6060f1SDimitry Andric ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros); 210fe6060f1SDimitry Andric 211fe6060f1SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 212fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 213fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros)); 214fe6060f1SDimitry Andric 215fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 216fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 217fe6060f1SDimitry Andric Res = TmpSeq; 218fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 219fe6060f1SDimitry Andric if (Res.size() <= 2) 220fe6060f1SDimitry Andric return Res; 221fe6060f1SDimitry Andric } 222fe6060f1SDimitry Andric 223fe6060f1SDimitry Andric // Some cases can benefit from filling the lower bits with zeros instead. 224fe6060f1SDimitry Andric ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros); 225fe6060f1SDimitry Andric TmpSeq.clear(); 226fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 227fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros)); 228fe6060f1SDimitry Andric 229fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 230fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 231fe6060f1SDimitry Andric Res = TmpSeq; 232fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 233fe6060f1SDimitry Andric if (Res.size() <= 2) 234fe6060f1SDimitry Andric return Res; 235fe6060f1SDimitry Andric } 236fe6060f1SDimitry Andric 237fe6060f1SDimitry Andric // If we have exactly 32 leading zeros and Zba, we can try using zext.w at 238fe6060f1SDimitry Andric // the end of the sequence. 239349cc55cSDimitry Andric if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 240fe6060f1SDimitry Andric // Try replacing upper bits with 1. 241fe6060f1SDimitry Andric uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros); 242fe6060f1SDimitry Andric TmpSeq.clear(); 243fe6060f1SDimitry Andric generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq); 2441fd87a68SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADD_UW, 0)); 245fe6060f1SDimitry Andric 246fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 247fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 248fe6060f1SDimitry Andric Res = TmpSeq; 249fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 250fe6060f1SDimitry Andric if (Res.size() <= 2) 251fe6060f1SDimitry Andric return Res; 252fe6060f1SDimitry Andric } 253fe6060f1SDimitry Andric } 254fe6060f1SDimitry Andric } 255fe6060f1SDimitry Andric 256349cc55cSDimitry Andric // Perform optimization with BCLRI/BSETI in the Zbs extension. 257349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) { 258349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 259349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 260349cc55cSDimitry Andric 261349cc55cSDimitry Andric // 1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000, 262349cc55cSDimitry Andric // call generateInstSeqImpl with Val|0x80000000 (which is expected be 263349cc55cSDimitry Andric // an int32), then emit (BCLRI r, 31). 264349cc55cSDimitry Andric // 2. For values in range 0x80000000 ~ 0xffffffff, call generateInstSeqImpl 265349cc55cSDimitry Andric // with Val&~0x80000000 (which is expected to be an int32), then 266349cc55cSDimitry Andric // emit (BSETI r, 31). 267349cc55cSDimitry Andric int64_t NewVal; 268349cc55cSDimitry Andric unsigned Opc; 269349cc55cSDimitry Andric if (Val < 0) { 270349cc55cSDimitry Andric Opc = RISCV::BCLRI; 271349cc55cSDimitry Andric NewVal = Val | 0x80000000ll; 272349cc55cSDimitry Andric } else { 273349cc55cSDimitry Andric Opc = RISCV::BSETI; 274349cc55cSDimitry Andric NewVal = Val & ~0x80000000ll; 275349cc55cSDimitry Andric } 276349cc55cSDimitry Andric if (isInt<32>(NewVal)) { 277349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 278349cc55cSDimitry Andric generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq); 279349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 31)); 280349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 281349cc55cSDimitry Andric Res = TmpSeq; 282349cc55cSDimitry Andric } 283349cc55cSDimitry Andric 284349cc55cSDimitry Andric // Try to use BCLRI for upper 32 bits if the original lower 32 bits are 285349cc55cSDimitry Andric // negative int32, or use BSETI for upper 32 bits if the original lower 286349cc55cSDimitry Andric // 32 bits are positive int32. 287349cc55cSDimitry Andric int32_t Lo = Val; 288349cc55cSDimitry Andric uint32_t Hi = Val >> 32; 289349cc55cSDimitry Andric Opc = 0; 290349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 291349cc55cSDimitry Andric generateInstSeqImpl(Lo, ActiveFeatures, TmpSeq); 292349cc55cSDimitry Andric // Check if it is profitable to use BCLRI/BSETI. 293349cc55cSDimitry Andric if (Lo > 0 && TmpSeq.size() + countPopulation(Hi) < Res.size()) { 294349cc55cSDimitry Andric Opc = RISCV::BSETI; 295349cc55cSDimitry Andric } else if (Lo < 0 && TmpSeq.size() + countPopulation(~Hi) < Res.size()) { 296349cc55cSDimitry Andric Opc = RISCV::BCLRI; 297349cc55cSDimitry Andric Hi = ~Hi; 298349cc55cSDimitry Andric } 299349cc55cSDimitry Andric // Search for each bit and build corresponding BCLRI/BSETI. 300349cc55cSDimitry Andric if (Opc > 0) { 301349cc55cSDimitry Andric while (Hi != 0) { 302349cc55cSDimitry Andric unsigned Bit = countTrailingZeros(Hi); 303349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, Bit + 32)); 304349cc55cSDimitry Andric Hi &= ~(1 << Bit); 305349cc55cSDimitry Andric } 306349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 307349cc55cSDimitry Andric Res = TmpSeq; 308349cc55cSDimitry Andric } 309349cc55cSDimitry Andric } 310349cc55cSDimitry Andric 311349cc55cSDimitry Andric // Perform optimization with SH*ADD in the Zba extension. 312349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 313349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 314349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 315349cc55cSDimitry Andric int64_t Div = 0; 316349cc55cSDimitry Andric unsigned Opc = 0; 317349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 318349cc55cSDimitry Andric // Select the opcode and divisor. 319349cc55cSDimitry Andric if ((Val % 3) == 0 && isInt<32>(Val / 3)) { 320349cc55cSDimitry Andric Div = 3; 321349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 322349cc55cSDimitry Andric } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) { 323349cc55cSDimitry Andric Div = 5; 324349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 325349cc55cSDimitry Andric } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) { 326349cc55cSDimitry Andric Div = 9; 327349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 328349cc55cSDimitry Andric } 329349cc55cSDimitry Andric // Build the new instruction sequence. 330349cc55cSDimitry Andric if (Div > 0) { 331349cc55cSDimitry Andric generateInstSeqImpl(Val / Div, ActiveFeatures, TmpSeq); 332349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0)); 333349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 334349cc55cSDimitry Andric Res = TmpSeq; 3353a9a9c0cSDimitry Andric } else { 336349cc55cSDimitry Andric // Try to use LUI+SH*ADD+ADDI. 337349cc55cSDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull; 338349cc55cSDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 339349cc55cSDimitry Andric Div = 0; 340349cc55cSDimitry Andric if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) { 341349cc55cSDimitry Andric Div = 3; 342349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 343349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) { 344349cc55cSDimitry Andric Div = 5; 345349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 346349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) { 347349cc55cSDimitry Andric Div = 9; 348349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 349349cc55cSDimitry Andric } 350349cc55cSDimitry Andric // Build the new instruction sequence. 351349cc55cSDimitry Andric if (Div > 0) { 352349cc55cSDimitry Andric // For Val that has zero Lo12 (implies Val equals to Hi52) should has 353349cc55cSDimitry Andric // already been processed to LUI+SH*ADD by previous optimization. 354349cc55cSDimitry Andric assert(Lo12 != 0 && 355349cc55cSDimitry Andric "unexpected instruction sequence for immediate materialisation"); 3563a9a9c0cSDimitry Andric assert(TmpSeq.empty() && "Expected empty TmpSeq"); 357349cc55cSDimitry Andric generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq); 358349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0)); 359349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12)); 360349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 361349cc55cSDimitry Andric Res = TmpSeq; 362349cc55cSDimitry Andric } 363349cc55cSDimitry Andric } 3643a9a9c0cSDimitry Andric } 365349cc55cSDimitry Andric 36604eeddc0SDimitry Andric // Perform optimization with rori in the Zbb extension. 36704eeddc0SDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbb]) { 36804eeddc0SDimitry Andric if (unsigned Rotate = extractRotateInfo(Val)) { 36904eeddc0SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 37004eeddc0SDimitry Andric uint64_t NegImm12 = 37104eeddc0SDimitry Andric ((uint64_t)Val >> (64 - Rotate)) | ((uint64_t)Val << Rotate); 37204eeddc0SDimitry Andric assert(isInt<12>(NegImm12)); 37304eeddc0SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, NegImm12)); 37404eeddc0SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::RORI, Rotate)); 37504eeddc0SDimitry Andric Res = TmpSeq; 37604eeddc0SDimitry Andric } 37704eeddc0SDimitry Andric } 378fe6060f1SDimitry Andric return Res; 379fe6060f1SDimitry Andric } 380fe6060f1SDimitry Andric 381fe6060f1SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, 382349cc55cSDimitry Andric const FeatureBitset &ActiveFeatures, bool CompressionCost) { 383fe6060f1SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit]; 384fe6060f1SDimitry Andric bool HasRVC = CompressionCost && ActiveFeatures[RISCV::FeatureStdExtC]; 385e8d8bef9SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32; 386e8d8bef9SDimitry Andric 387e8d8bef9SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost 388e8d8bef9SDimitry Andric // of each chunk. 389e8d8bef9SDimitry Andric int Cost = 0; 390e8d8bef9SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) { 391e8d8bef9SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize); 392fe6060f1SDimitry Andric InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures); 393fe6060f1SDimitry Andric Cost += getInstSeqCost(MatSeq, HasRVC); 394e8d8bef9SDimitry Andric } 395e8d8bef9SDimitry Andric return std::max(1, Cost); 396e8d8bef9SDimitry Andric } 397*81ad6265SDimitry Andric 398*81ad6265SDimitry Andric OpndKind Inst::getOpndKind() const { 399*81ad6265SDimitry Andric switch (Opc) { 400*81ad6265SDimitry Andric default: 401*81ad6265SDimitry Andric llvm_unreachable("Unexpected opcode!"); 402*81ad6265SDimitry Andric case RISCV::LUI: 403*81ad6265SDimitry Andric return RISCVMatInt::Imm; 404*81ad6265SDimitry Andric case RISCV::ADD_UW: 405*81ad6265SDimitry Andric return RISCVMatInt::RegX0; 406*81ad6265SDimitry Andric case RISCV::SH1ADD: 407*81ad6265SDimitry Andric case RISCV::SH2ADD: 408*81ad6265SDimitry Andric case RISCV::SH3ADD: 409*81ad6265SDimitry Andric return RISCVMatInt::RegReg; 410*81ad6265SDimitry Andric case RISCV::ADDI: 411*81ad6265SDimitry Andric case RISCV::ADDIW: 412*81ad6265SDimitry Andric case RISCV::SLLI: 413*81ad6265SDimitry Andric case RISCV::SRLI: 414*81ad6265SDimitry Andric case RISCV::SLLI_UW: 415*81ad6265SDimitry Andric case RISCV::RORI: 416*81ad6265SDimitry Andric case RISCV::BSETI: 417*81ad6265SDimitry Andric case RISCV::BCLRI: 418*81ad6265SDimitry Andric return RISCVMatInt::RegImm; 419*81ad6265SDimitry Andric } 420*81ad6265SDimitry Andric } 421*81ad6265SDimitry Andric 422e8d8bef9SDimitry Andric } // namespace RISCVMatInt 423e8d8bef9SDimitry Andric } // namespace llvm 424