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; 23*bdd1243dSDimitry 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: 31*bdd1243dSDimitry 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 53*bdd1243dSDimitry Andric // Use BSETI for a single bit that can't be expressed by a single LUI or ADDI. 54*bdd1243dSDimitry Andric if (ActiveFeatures[RISCV::FeatureStdExtZbs] && isPowerOf2_64(Val) && 55*bdd1243dSDimitry Andric (!isInt<32>(Val) || Val == 0x800)) { 56*bdd1243dSDimitry Andric Res.emplace_back(RISCV::BSETI, Log2_64(Val)); 57*bdd1243dSDimitry Andric return; 58*bdd1243dSDimitry Andric } 59*bdd1243dSDimitry 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) 72*bdd1243dSDimitry 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; 76*bdd1243dSDimitry 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)) { 114*bdd1243dSDimitry 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) { 148*bdd1243dSDimitry Andric unsigned Opc = Unsigned ? RISCV::SLLI_UW : RISCV::SLLI; 149*bdd1243dSDimitry Andric Res.emplace_back(Opc, ShiftAmount); 15081ad6265SDimitry Andric } 15181ad6265SDimitry Andric 152fe6060f1SDimitry Andric if (Lo12) 153*bdd1243dSDimitry 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.. 15804eeddc0SDimitry Andric unsigned LeadingOnes = countLeadingOnes((uint64_t)Val); 15904eeddc0SDimitry Andric unsigned TrailingOnes = countTrailingOnes((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 16504eeddc0SDimitry Andric unsigned UpperTrailingOnes = countTrailingOnes(Hi_32(Val)); 16604eeddc0SDimitry Andric unsigned LowerLeadingOnes = countLeadingOnes(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 174*bdd1243dSDimitry 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 179*bdd1243dSDimitry Andric // If the low 12 bits are non-zero, the first expansion may end with an ADDI 180*bdd1243dSDimitry Andric // or ADDIW. If there are trailing zeros, try generating a sign extended 181*bdd1243dSDimitry Andric // constant with no trailing zeros and use a final SLLI to restore them. 182*bdd1243dSDimitry Andric if ((Val & 0xfff) != 0 && (Val & 1) == 0 && Res.size() >= 2) { 18381ad6265SDimitry Andric unsigned TrailingZeros = countTrailingZeros((uint64_t)Val); 18481ad6265SDimitry Andric int64_t ShiftedVal = Val >> TrailingZeros; 185*bdd1243dSDimitry Andric // If we can use C.LI+C.SLLI instead of LUI+ADDI(W) prefer that since 186*bdd1243dSDimitry Andric // its more compressible. But only if LUI+ADDI(W) isn't fusable. 187*bdd1243dSDimitry Andric // NOTE: We don't check for C extension to minimize differences in generated 188*bdd1243dSDimitry Andric // code. 189*bdd1243dSDimitry Andric bool IsShiftedCompressible = 190*bdd1243dSDimitry Andric isInt<6>(ShiftedVal) && !ActiveFeatures[RISCV::TuneLUIADDIFusion]; 19181ad6265SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 19281ad6265SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 193*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::SLLI, TrailingZeros); 19481ad6265SDimitry Andric 19581ad6265SDimitry Andric // Keep the new sequence if it is an improvement. 196*bdd1243dSDimitry Andric if (TmpSeq.size() < Res.size() || IsShiftedCompressible) 19781ad6265SDimitry Andric Res = TmpSeq; 19881ad6265SDimitry Andric } 19981ad6265SDimitry Andric 200fe6060f1SDimitry Andric // If the constant is positive we might be able to generate a shifted constant 201fe6060f1SDimitry Andric // with no leading zeros and use a final SRLI to restore them. 202fe6060f1SDimitry Andric if (Val > 0 && Res.size() > 2) { 203fe6060f1SDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 204fe6060f1SDimitry Andric "Expected RV32 to only need 2 instructions"); 205fe6060f1SDimitry Andric unsigned LeadingZeros = countLeadingZeros((uint64_t)Val); 206fe6060f1SDimitry Andric uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros; 207fe6060f1SDimitry Andric // Fill in the bits that will be shifted out with 1s. An example where this 208fe6060f1SDimitry Andric // helps is trailing one masks with 32 or more ones. This will generate 209fe6060f1SDimitry Andric // ADDI -1 and an SRLI. 210fe6060f1SDimitry Andric ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros); 211fe6060f1SDimitry Andric 212fe6060f1SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 213fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 214*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros); 215fe6060f1SDimitry Andric 216fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 217*bdd1243dSDimitry Andric if (TmpSeq.size() < Res.size()) 218fe6060f1SDimitry Andric Res = TmpSeq; 219fe6060f1SDimitry Andric 220fe6060f1SDimitry Andric // Some cases can benefit from filling the lower bits with zeros instead. 221fe6060f1SDimitry Andric ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros); 222fe6060f1SDimitry Andric TmpSeq.clear(); 223fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 224*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros); 225fe6060f1SDimitry Andric 226fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 227*bdd1243dSDimitry Andric if (TmpSeq.size() < Res.size()) 228fe6060f1SDimitry Andric Res = TmpSeq; 229fe6060f1SDimitry Andric 230fe6060f1SDimitry Andric // If we have exactly 32 leading zeros and Zba, we can try using zext.w at 231fe6060f1SDimitry Andric // the end of the sequence. 232349cc55cSDimitry Andric if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 233fe6060f1SDimitry Andric // Try replacing upper bits with 1. 234fe6060f1SDimitry Andric uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros); 235fe6060f1SDimitry Andric TmpSeq.clear(); 236fe6060f1SDimitry Andric generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq); 237*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::ADD_UW, 0); 238fe6060f1SDimitry Andric 239fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 240*bdd1243dSDimitry Andric if (TmpSeq.size() < Res.size()) 241fe6060f1SDimitry Andric Res = TmpSeq; 242fe6060f1SDimitry Andric } 243fe6060f1SDimitry Andric } 244fe6060f1SDimitry Andric 245349cc55cSDimitry Andric // Perform optimization with BCLRI/BSETI in the Zbs extension. 246349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) { 247349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 248349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 249349cc55cSDimitry Andric 250349cc55cSDimitry Andric // 1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000, 251349cc55cSDimitry Andric // call generateInstSeqImpl with Val|0x80000000 (which is expected be 252349cc55cSDimitry Andric // an int32), then emit (BCLRI r, 31). 253349cc55cSDimitry Andric // 2. For values in range 0x80000000 ~ 0xffffffff, call generateInstSeqImpl 254349cc55cSDimitry Andric // with Val&~0x80000000 (which is expected to be an int32), then 255349cc55cSDimitry Andric // emit (BSETI r, 31). 256349cc55cSDimitry Andric int64_t NewVal; 257349cc55cSDimitry Andric unsigned Opc; 258349cc55cSDimitry Andric if (Val < 0) { 259349cc55cSDimitry Andric Opc = RISCV::BCLRI; 260349cc55cSDimitry Andric NewVal = Val | 0x80000000ll; 261349cc55cSDimitry Andric } else { 262349cc55cSDimitry Andric Opc = RISCV::BSETI; 263349cc55cSDimitry Andric NewVal = Val & ~0x80000000ll; 264349cc55cSDimitry Andric } 265349cc55cSDimitry Andric if (isInt<32>(NewVal)) { 266349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 267349cc55cSDimitry Andric generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq); 268*bdd1243dSDimitry Andric TmpSeq.emplace_back(Opc, 31); 269349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 270349cc55cSDimitry Andric Res = TmpSeq; 271349cc55cSDimitry Andric } 272349cc55cSDimitry Andric 273349cc55cSDimitry Andric // Try to use BCLRI for upper 32 bits if the original lower 32 bits are 274349cc55cSDimitry Andric // negative int32, or use BSETI for upper 32 bits if the original lower 275349cc55cSDimitry Andric // 32 bits are positive int32. 276*bdd1243dSDimitry Andric int32_t Lo = Lo_32(Val); 277*bdd1243dSDimitry Andric uint32_t Hi = Hi_32(Val); 278349cc55cSDimitry Andric Opc = 0; 279349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 280349cc55cSDimitry Andric generateInstSeqImpl(Lo, ActiveFeatures, TmpSeq); 281349cc55cSDimitry Andric // Check if it is profitable to use BCLRI/BSETI. 282*bdd1243dSDimitry Andric if (Lo > 0 && TmpSeq.size() + llvm::popcount(Hi) < Res.size()) { 283349cc55cSDimitry Andric Opc = RISCV::BSETI; 284*bdd1243dSDimitry Andric } else if (Lo < 0 && TmpSeq.size() + llvm::popcount(~Hi) < Res.size()) { 285349cc55cSDimitry Andric Opc = RISCV::BCLRI; 286349cc55cSDimitry Andric Hi = ~Hi; 287349cc55cSDimitry Andric } 288349cc55cSDimitry Andric // Search for each bit and build corresponding BCLRI/BSETI. 289349cc55cSDimitry Andric if (Opc > 0) { 290349cc55cSDimitry Andric while (Hi != 0) { 291*bdd1243dSDimitry Andric unsigned Bit = llvm::countr_zero(Hi); 292*bdd1243dSDimitry Andric TmpSeq.emplace_back(Opc, Bit + 32); 293*bdd1243dSDimitry Andric Hi &= (Hi - 1); // Clear lowest set bit. 294349cc55cSDimitry Andric } 295349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 296349cc55cSDimitry Andric Res = TmpSeq; 297349cc55cSDimitry Andric } 298349cc55cSDimitry Andric } 299349cc55cSDimitry Andric 300349cc55cSDimitry Andric // Perform optimization with SH*ADD in the Zba extension. 301349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 302349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 303349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 304349cc55cSDimitry Andric int64_t Div = 0; 305349cc55cSDimitry Andric unsigned Opc = 0; 306349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 307349cc55cSDimitry Andric // Select the opcode and divisor. 308349cc55cSDimitry Andric if ((Val % 3) == 0 && isInt<32>(Val / 3)) { 309349cc55cSDimitry Andric Div = 3; 310349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 311349cc55cSDimitry Andric } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) { 312349cc55cSDimitry Andric Div = 5; 313349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 314349cc55cSDimitry Andric } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) { 315349cc55cSDimitry Andric Div = 9; 316349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 317349cc55cSDimitry Andric } 318349cc55cSDimitry Andric // Build the new instruction sequence. 319349cc55cSDimitry Andric if (Div > 0) { 320349cc55cSDimitry Andric generateInstSeqImpl(Val / Div, ActiveFeatures, TmpSeq); 321*bdd1243dSDimitry Andric TmpSeq.emplace_back(Opc, 0); 322349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 323349cc55cSDimitry Andric Res = TmpSeq; 3243a9a9c0cSDimitry Andric } else { 325349cc55cSDimitry Andric // Try to use LUI+SH*ADD+ADDI. 326349cc55cSDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull; 327349cc55cSDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 328349cc55cSDimitry Andric Div = 0; 329349cc55cSDimitry Andric if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) { 330349cc55cSDimitry Andric Div = 3; 331349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 332349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) { 333349cc55cSDimitry Andric Div = 5; 334349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 335349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) { 336349cc55cSDimitry Andric Div = 9; 337349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 338349cc55cSDimitry Andric } 339349cc55cSDimitry Andric // Build the new instruction sequence. 340349cc55cSDimitry Andric if (Div > 0) { 341349cc55cSDimitry Andric // For Val that has zero Lo12 (implies Val equals to Hi52) should has 342349cc55cSDimitry Andric // already been processed to LUI+SH*ADD by previous optimization. 343349cc55cSDimitry Andric assert(Lo12 != 0 && 344349cc55cSDimitry Andric "unexpected instruction sequence for immediate materialisation"); 3453a9a9c0cSDimitry Andric assert(TmpSeq.empty() && "Expected empty TmpSeq"); 346349cc55cSDimitry Andric generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq); 347*bdd1243dSDimitry Andric TmpSeq.emplace_back(Opc, 0); 348*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::ADDI, Lo12); 349349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 350349cc55cSDimitry Andric Res = TmpSeq; 351349cc55cSDimitry Andric } 352349cc55cSDimitry Andric } 3533a9a9c0cSDimitry Andric } 354349cc55cSDimitry Andric 35504eeddc0SDimitry Andric // Perform optimization with rori in the Zbb extension. 35604eeddc0SDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbb]) { 35704eeddc0SDimitry Andric if (unsigned Rotate = extractRotateInfo(Val)) { 35804eeddc0SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 35904eeddc0SDimitry Andric uint64_t NegImm12 = 36004eeddc0SDimitry Andric ((uint64_t)Val >> (64 - Rotate)) | ((uint64_t)Val << Rotate); 36104eeddc0SDimitry Andric assert(isInt<12>(NegImm12)); 362*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::ADDI, NegImm12); 363*bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::RORI, Rotate); 36404eeddc0SDimitry Andric Res = TmpSeq; 36504eeddc0SDimitry Andric } 36604eeddc0SDimitry Andric } 367fe6060f1SDimitry Andric return Res; 368fe6060f1SDimitry Andric } 369fe6060f1SDimitry Andric 370fe6060f1SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, 371349cc55cSDimitry Andric const FeatureBitset &ActiveFeatures, bool CompressionCost) { 372fe6060f1SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit]; 373*bdd1243dSDimitry Andric bool HasRVC = CompressionCost && (ActiveFeatures[RISCV::FeatureStdExtC] || 374*bdd1243dSDimitry Andric ActiveFeatures[RISCV::FeatureExtZca]); 375e8d8bef9SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32; 376e8d8bef9SDimitry Andric 377e8d8bef9SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost 378e8d8bef9SDimitry Andric // of each chunk. 379e8d8bef9SDimitry Andric int Cost = 0; 380e8d8bef9SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) { 381e8d8bef9SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize); 382fe6060f1SDimitry Andric InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures); 383fe6060f1SDimitry Andric Cost += getInstSeqCost(MatSeq, HasRVC); 384e8d8bef9SDimitry Andric } 385e8d8bef9SDimitry Andric return std::max(1, Cost); 386e8d8bef9SDimitry Andric } 38781ad6265SDimitry Andric 38881ad6265SDimitry Andric OpndKind Inst::getOpndKind() const { 38981ad6265SDimitry Andric switch (Opc) { 39081ad6265SDimitry Andric default: 39181ad6265SDimitry Andric llvm_unreachable("Unexpected opcode!"); 39281ad6265SDimitry Andric case RISCV::LUI: 39381ad6265SDimitry Andric return RISCVMatInt::Imm; 39481ad6265SDimitry Andric case RISCV::ADD_UW: 39581ad6265SDimitry Andric return RISCVMatInt::RegX0; 39681ad6265SDimitry Andric case RISCV::SH1ADD: 39781ad6265SDimitry Andric case RISCV::SH2ADD: 39881ad6265SDimitry Andric case RISCV::SH3ADD: 39981ad6265SDimitry Andric return RISCVMatInt::RegReg; 40081ad6265SDimitry Andric case RISCV::ADDI: 40181ad6265SDimitry Andric case RISCV::ADDIW: 40281ad6265SDimitry Andric case RISCV::SLLI: 40381ad6265SDimitry Andric case RISCV::SRLI: 40481ad6265SDimitry Andric case RISCV::SLLI_UW: 40581ad6265SDimitry Andric case RISCV::RORI: 40681ad6265SDimitry Andric case RISCV::BSETI: 40781ad6265SDimitry Andric case RISCV::BCLRI: 40881ad6265SDimitry Andric return RISCVMatInt::RegImm; 40981ad6265SDimitry Andric } 41081ad6265SDimitry Andric } 41181ad6265SDimitry Andric 412*bdd1243dSDimitry Andric } // namespace llvm::RISCVMatInt 413