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) { 23349cc55cSDimitry Andric default: 24349cc55cSDimitry 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; 341fd87a68SDimitry Andric case RISCV::ADD_UW: 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 81349cc55cSDimitry 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. 110349cc55cSDimitry Andric bool Unsigned = false; 111349cc55cSDimitry Andric if (ShiftAmount > 12 && !isInt<12>(Hi52)) { 112349cc55cSDimitry 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; 116349cc55cSDimitry Andric } else if (isUInt<32>((uint64_t)Hi52 << 12) && 117349cc55cSDimitry Andric ActiveFeatures[RISCV::FeatureStdExtZba]) { 118349cc55cSDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match 119349cc55cSDimitry Andric // LUI, then shift left with SLLI.UW to clear the upper 32 set bits. 120349cc55cSDimitry Andric ShiftAmount -= 12; 121349cc55cSDimitry Andric Hi52 = ((uint64_t)Hi52 << 12) | (0xffffffffull << 32); 122349cc55cSDimitry Andric Unsigned = true; 123349cc55cSDimitry Andric } 124349cc55cSDimitry Andric } 125349cc55cSDimitry Andric 1261fd87a68SDimitry Andric // Try to use SLLI_UW for Hi52 when it is uint32 but not int32. 127349cc55cSDimitry Andric if (isUInt<32>((uint64_t)Hi52) && !isInt<32>((uint64_t)Hi52) && 128349cc55cSDimitry Andric ActiveFeatures[RISCV::FeatureStdExtZba]) { 1291fd87a68SDimitry Andric // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with 1301fd87a68SDimitry Andric // SLLI_UW. 131349cc55cSDimitry Andric Hi52 = ((uint64_t)Hi52) | (0xffffffffull << 32); 132349cc55cSDimitry Andric Unsigned = true; 133e8d8bef9SDimitry Andric } 134e8d8bef9SDimitry Andric 135fe6060f1SDimitry Andric generateInstSeqImpl(Hi52, ActiveFeatures, Res); 136fe6060f1SDimitry Andric 137349cc55cSDimitry Andric if (Unsigned) 1381fd87a68SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLI_UW, ShiftAmount)); 139349cc55cSDimitry Andric else 140fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::SLLI, ShiftAmount)); 141fe6060f1SDimitry Andric if (Lo12) 142fe6060f1SDimitry Andric Res.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12)); 143fe6060f1SDimitry Andric } 144fe6060f1SDimitry Andric 14504eeddc0SDimitry Andric static unsigned extractRotateInfo(int64_t Val) { 14604eeddc0SDimitry Andric // for case: 0b111..1..xxxxxx1..1.. 14704eeddc0SDimitry Andric unsigned LeadingOnes = countLeadingOnes((uint64_t)Val); 14804eeddc0SDimitry Andric unsigned TrailingOnes = countTrailingOnes((uint64_t)Val); 14904eeddc0SDimitry Andric if (TrailingOnes > 0 && TrailingOnes < 64 && 15004eeddc0SDimitry Andric (LeadingOnes + TrailingOnes) > (64 - 12)) 15104eeddc0SDimitry Andric return 64 - TrailingOnes; 15204eeddc0SDimitry Andric 15304eeddc0SDimitry Andric // for case: 0bxxx1..1..1...xxx 15404eeddc0SDimitry Andric unsigned UpperTrailingOnes = countTrailingOnes(Hi_32(Val)); 15504eeddc0SDimitry Andric unsigned LowerLeadingOnes = countLeadingOnes(Lo_32(Val)); 15604eeddc0SDimitry Andric if (UpperTrailingOnes < 32 && 15704eeddc0SDimitry Andric (UpperTrailingOnes + LowerLeadingOnes) > (64 - 12)) 15804eeddc0SDimitry Andric return 32 - UpperTrailingOnes; 15904eeddc0SDimitry Andric 16004eeddc0SDimitry Andric return 0; 16104eeddc0SDimitry Andric } 16204eeddc0SDimitry Andric 163fe6060f1SDimitry Andric namespace llvm { 164fe6060f1SDimitry Andric namespace RISCVMatInt { 165fe6060f1SDimitry Andric InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) { 166fe6060f1SDimitry Andric RISCVMatInt::InstSeq Res; 167fe6060f1SDimitry Andric generateInstSeqImpl(Val, ActiveFeatures, Res); 168fe6060f1SDimitry Andric 169fe6060f1SDimitry Andric // If the constant is positive we might be able to generate a shifted constant 170fe6060f1SDimitry Andric // with no leading zeros and use a final SRLI to restore them. 171fe6060f1SDimitry Andric if (Val > 0 && Res.size() > 2) { 172fe6060f1SDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 173fe6060f1SDimitry Andric "Expected RV32 to only need 2 instructions"); 174fe6060f1SDimitry Andric unsigned LeadingZeros = countLeadingZeros((uint64_t)Val); 175fe6060f1SDimitry Andric uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros; 176fe6060f1SDimitry Andric // Fill in the bits that will be shifted out with 1s. An example where this 177fe6060f1SDimitry Andric // helps is trailing one masks with 32 or more ones. This will generate 178fe6060f1SDimitry Andric // ADDI -1 and an SRLI. 179fe6060f1SDimitry Andric ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros); 180fe6060f1SDimitry Andric 181fe6060f1SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 182fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 183fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros)); 184fe6060f1SDimitry Andric 185fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 186fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 187fe6060f1SDimitry Andric Res = TmpSeq; 188fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 189fe6060f1SDimitry Andric if (Res.size() <= 2) 190fe6060f1SDimitry Andric return Res; 191fe6060f1SDimitry Andric } 192fe6060f1SDimitry Andric 193fe6060f1SDimitry Andric // Some cases can benefit from filling the lower bits with zeros instead. 194fe6060f1SDimitry Andric ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros); 195fe6060f1SDimitry Andric TmpSeq.clear(); 196fe6060f1SDimitry Andric generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq); 197fe6060f1SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros)); 198fe6060f1SDimitry Andric 199fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 200fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 201fe6060f1SDimitry Andric Res = TmpSeq; 202fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 203fe6060f1SDimitry Andric if (Res.size() <= 2) 204fe6060f1SDimitry Andric return Res; 205fe6060f1SDimitry Andric } 206fe6060f1SDimitry Andric 207fe6060f1SDimitry Andric // If we have exactly 32 leading zeros and Zba, we can try using zext.w at 208fe6060f1SDimitry Andric // the end of the sequence. 209349cc55cSDimitry Andric if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 210fe6060f1SDimitry Andric // Try replacing upper bits with 1. 211fe6060f1SDimitry Andric uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros); 212fe6060f1SDimitry Andric TmpSeq.clear(); 213fe6060f1SDimitry Andric generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq); 2141fd87a68SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADD_UW, 0)); 215fe6060f1SDimitry Andric 216fe6060f1SDimitry Andric // Keep the new sequence if it is an improvement. 217fe6060f1SDimitry Andric if (TmpSeq.size() < Res.size()) { 218fe6060f1SDimitry Andric Res = TmpSeq; 219fe6060f1SDimitry Andric // A 2 instruction sequence is the best we can do. 220fe6060f1SDimitry Andric if (Res.size() <= 2) 221fe6060f1SDimitry Andric return Res; 222fe6060f1SDimitry Andric } 223fe6060f1SDimitry Andric } 224fe6060f1SDimitry Andric } 225fe6060f1SDimitry Andric 226349cc55cSDimitry Andric // Perform optimization with BCLRI/BSETI in the Zbs extension. 227349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) { 228349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 229349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 230349cc55cSDimitry Andric 231349cc55cSDimitry Andric // 1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000, 232349cc55cSDimitry Andric // call generateInstSeqImpl with Val|0x80000000 (which is expected be 233349cc55cSDimitry Andric // an int32), then emit (BCLRI r, 31). 234349cc55cSDimitry Andric // 2. For values in range 0x80000000 ~ 0xffffffff, call generateInstSeqImpl 235349cc55cSDimitry Andric // with Val&~0x80000000 (which is expected to be an int32), then 236349cc55cSDimitry Andric // emit (BSETI r, 31). 237349cc55cSDimitry Andric int64_t NewVal; 238349cc55cSDimitry Andric unsigned Opc; 239349cc55cSDimitry Andric if (Val < 0) { 240349cc55cSDimitry Andric Opc = RISCV::BCLRI; 241349cc55cSDimitry Andric NewVal = Val | 0x80000000ll; 242349cc55cSDimitry Andric } else { 243349cc55cSDimitry Andric Opc = RISCV::BSETI; 244349cc55cSDimitry Andric NewVal = Val & ~0x80000000ll; 245349cc55cSDimitry Andric } 246349cc55cSDimitry Andric if (isInt<32>(NewVal)) { 247349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 248349cc55cSDimitry Andric generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq); 249349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 31)); 250349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 251349cc55cSDimitry Andric Res = TmpSeq; 252349cc55cSDimitry Andric } 253349cc55cSDimitry Andric 254349cc55cSDimitry Andric // Try to use BCLRI for upper 32 bits if the original lower 32 bits are 255349cc55cSDimitry Andric // negative int32, or use BSETI for upper 32 bits if the original lower 256349cc55cSDimitry Andric // 32 bits are positive int32. 257349cc55cSDimitry Andric int32_t Lo = Val; 258349cc55cSDimitry Andric uint32_t Hi = Val >> 32; 259349cc55cSDimitry Andric Opc = 0; 260349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 261349cc55cSDimitry Andric generateInstSeqImpl(Lo, ActiveFeatures, TmpSeq); 262349cc55cSDimitry Andric // Check if it is profitable to use BCLRI/BSETI. 263349cc55cSDimitry Andric if (Lo > 0 && TmpSeq.size() + countPopulation(Hi) < Res.size()) { 264349cc55cSDimitry Andric Opc = RISCV::BSETI; 265349cc55cSDimitry Andric } else if (Lo < 0 && TmpSeq.size() + countPopulation(~Hi) < Res.size()) { 266349cc55cSDimitry Andric Opc = RISCV::BCLRI; 267349cc55cSDimitry Andric Hi = ~Hi; 268349cc55cSDimitry Andric } 269349cc55cSDimitry Andric // Search for each bit and build corresponding BCLRI/BSETI. 270349cc55cSDimitry Andric if (Opc > 0) { 271349cc55cSDimitry Andric while (Hi != 0) { 272349cc55cSDimitry Andric unsigned Bit = countTrailingZeros(Hi); 273349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, Bit + 32)); 274349cc55cSDimitry Andric Hi &= ~(1 << Bit); 275349cc55cSDimitry Andric } 276349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 277349cc55cSDimitry Andric Res = TmpSeq; 278349cc55cSDimitry Andric } 279349cc55cSDimitry Andric } 280349cc55cSDimitry Andric 281349cc55cSDimitry Andric // Perform optimization with SH*ADD in the Zba extension. 282349cc55cSDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZba]) { 283349cc55cSDimitry Andric assert(ActiveFeatures[RISCV::Feature64Bit] && 284349cc55cSDimitry Andric "Expected RV32 to only need 2 instructions"); 285349cc55cSDimitry Andric int64_t Div = 0; 286349cc55cSDimitry Andric unsigned Opc = 0; 287349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 288349cc55cSDimitry Andric // Select the opcode and divisor. 289349cc55cSDimitry Andric if ((Val % 3) == 0 && isInt<32>(Val / 3)) { 290349cc55cSDimitry Andric Div = 3; 291349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 292349cc55cSDimitry Andric } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) { 293349cc55cSDimitry Andric Div = 5; 294349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 295349cc55cSDimitry Andric } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) { 296349cc55cSDimitry Andric Div = 9; 297349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 298349cc55cSDimitry Andric } 299349cc55cSDimitry Andric // Build the new instruction sequence. 300349cc55cSDimitry Andric if (Div > 0) { 301349cc55cSDimitry Andric generateInstSeqImpl(Val / Div, ActiveFeatures, TmpSeq); 302349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0)); 303349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 304349cc55cSDimitry Andric Res = TmpSeq; 305*3a9a9c0cSDimitry Andric } else { 306349cc55cSDimitry Andric // Try to use LUI+SH*ADD+ADDI. 307349cc55cSDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull; 308349cc55cSDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 309349cc55cSDimitry Andric Div = 0; 310349cc55cSDimitry Andric if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) { 311349cc55cSDimitry Andric Div = 3; 312349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 313349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) { 314349cc55cSDimitry Andric Div = 5; 315349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 316349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) { 317349cc55cSDimitry Andric Div = 9; 318349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 319349cc55cSDimitry Andric } 320349cc55cSDimitry Andric // Build the new instruction sequence. 321349cc55cSDimitry Andric if (Div > 0) { 322349cc55cSDimitry Andric // For Val that has zero Lo12 (implies Val equals to Hi52) should has 323349cc55cSDimitry Andric // already been processed to LUI+SH*ADD by previous optimization. 324349cc55cSDimitry Andric assert(Lo12 != 0 && 325349cc55cSDimitry Andric "unexpected instruction sequence for immediate materialisation"); 326*3a9a9c0cSDimitry Andric assert(TmpSeq.empty() && "Expected empty TmpSeq"); 327349cc55cSDimitry Andric generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq); 328349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0)); 329349cc55cSDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12)); 330349cc55cSDimitry Andric if (TmpSeq.size() < Res.size()) 331349cc55cSDimitry Andric Res = TmpSeq; 332349cc55cSDimitry Andric } 333349cc55cSDimitry Andric } 334*3a9a9c0cSDimitry Andric } 335349cc55cSDimitry Andric 33604eeddc0SDimitry Andric // Perform optimization with rori in the Zbb extension. 33704eeddc0SDimitry Andric if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbb]) { 33804eeddc0SDimitry Andric if (unsigned Rotate = extractRotateInfo(Val)) { 33904eeddc0SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 34004eeddc0SDimitry Andric uint64_t NegImm12 = 34104eeddc0SDimitry Andric ((uint64_t)Val >> (64 - Rotate)) | ((uint64_t)Val << Rotate); 34204eeddc0SDimitry Andric assert(isInt<12>(NegImm12)); 34304eeddc0SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, NegImm12)); 34404eeddc0SDimitry Andric TmpSeq.push_back(RISCVMatInt::Inst(RISCV::RORI, Rotate)); 34504eeddc0SDimitry Andric Res = TmpSeq; 34604eeddc0SDimitry Andric } 34704eeddc0SDimitry Andric } 348fe6060f1SDimitry Andric return Res; 349fe6060f1SDimitry Andric } 350fe6060f1SDimitry Andric 351fe6060f1SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, 352349cc55cSDimitry Andric const FeatureBitset &ActiveFeatures, bool CompressionCost) { 353fe6060f1SDimitry Andric bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit]; 354fe6060f1SDimitry Andric bool HasRVC = CompressionCost && ActiveFeatures[RISCV::FeatureStdExtC]; 355e8d8bef9SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32; 356e8d8bef9SDimitry Andric 357e8d8bef9SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost 358e8d8bef9SDimitry Andric // of each chunk. 359e8d8bef9SDimitry Andric int Cost = 0; 360e8d8bef9SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) { 361e8d8bef9SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize); 362fe6060f1SDimitry Andric InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures); 363fe6060f1SDimitry Andric Cost += getInstSeqCost(MatSeq, HasRVC); 364e8d8bef9SDimitry Andric } 365e8d8bef9SDimitry Andric return std::max(1, Cost); 366e8d8bef9SDimitry Andric } 367e8d8bef9SDimitry Andric } // namespace RISCVMatInt 368e8d8bef9SDimitry Andric } // namespace llvm 369