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" 12*0fca6ea1SDimitry Andric #include "llvm/MC/MCInstBuilder.h" 13e8d8bef9SDimitry Andric #include "llvm/Support/MathExtras.h" 14fe6060f1SDimitry Andric using namespace llvm; 15e8d8bef9SDimitry Andric 16fe6060f1SDimitry Andric static int getInstSeqCost(RISCVMatInt::InstSeq &Res, bool HasRVC) { 17fe6060f1SDimitry Andric if (!HasRVC) 18fe6060f1SDimitry Andric return Res.size(); 19e8d8bef9SDimitry Andric 20fe6060f1SDimitry Andric int Cost = 0; 21fe6060f1SDimitry Andric for (auto Instr : Res) { 2281ad6265SDimitry Andric // Assume instructions that aren't listed aren't compressible. 2381ad6265SDimitry Andric bool Compressed = false; 24bdd1243dSDimitry Andric switch (Instr.getOpcode()) { 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: 32bdd1243dSDimitry Andric Compressed = isInt<6>(Instr.getImm()); 33fe6060f1SDimitry Andric break; 34fe6060f1SDimitry Andric } 35fe6060f1SDimitry Andric // Two RVC instructions take the same space as one RVI instruction, but 36fe6060f1SDimitry Andric // can take longer to execute than the single RVI instruction. Thus, we 37fe6060f1SDimitry Andric // consider that two RVC instruction are slightly more costly than one 38fe6060f1SDimitry Andric // RVI instruction. For longer sequences of RVC instructions the space 39fe6060f1SDimitry Andric // savings can be worth it, though. The costs below try to model that. 40fe6060f1SDimitry Andric if (!Compressed) 41fe6060f1SDimitry Andric Cost += 100; // Baseline cost of one RVI instruction: 100%. 42fe6060f1SDimitry Andric else 43fe6060f1SDimitry Andric Cost += 70; // 70% cost of baseline. 44fe6060f1SDimitry Andric } 45fe6060f1SDimitry Andric return Cost; 46fe6060f1SDimitry Andric } 47fe6060f1SDimitry Andric 48fe6060f1SDimitry Andric // Recursively generate a sequence for materializing an integer. 495f757f3fSDimitry Andric static void generateInstSeqImpl(int64_t Val, const MCSubtargetInfo &STI, 50fe6060f1SDimitry Andric RISCVMatInt::InstSeq &Res) { 515f757f3fSDimitry Andric bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit); 52fe6060f1SDimitry Andric 53bdd1243dSDimitry Andric // Use BSETI for a single bit that can't be expressed by a single LUI or ADDI. 545f757f3fSDimitry Andric if (STI.hasFeature(RISCV::FeatureStdExtZbs) && isPowerOf2_64(Val) && 55bdd1243dSDimitry Andric (!isInt<32>(Val) || Val == 0x800)) { 56bdd1243dSDimitry Andric Res.emplace_back(RISCV::BSETI, Log2_64(Val)); 57bdd1243dSDimitry Andric return; 58bdd1243dSDimitry Andric } 59bdd1243dSDimitry Andric 60e8d8bef9SDimitry Andric if (isInt<32>(Val)) { 61e8d8bef9SDimitry Andric // Depending on the active bits in the immediate Value v, the following 62e8d8bef9SDimitry Andric // instruction sequences are emitted: 63e8d8bef9SDimitry Andric // 64e8d8bef9SDimitry Andric // v == 0 : ADDI 65e8d8bef9SDimitry Andric // v[0,12) != 0 && v[12,32) == 0 : ADDI 66e8d8bef9SDimitry Andric // v[0,12) == 0 && v[12,32) != 0 : LUI 67e8d8bef9SDimitry Andric // v[0,32) != 0 : LUI+ADDI(W) 68e8d8bef9SDimitry Andric int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF; 69e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 70e8d8bef9SDimitry Andric 71e8d8bef9SDimitry Andric if (Hi20) 72bdd1243dSDimitry Andric Res.emplace_back(RISCV::LUI, Hi20); 73e8d8bef9SDimitry Andric 74e8d8bef9SDimitry Andric if (Lo12 || Hi20 == 0) { 75e8d8bef9SDimitry Andric unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI; 76bdd1243dSDimitry Andric Res.emplace_back(AddiOpc, Lo12); 77e8d8bef9SDimitry Andric } 78e8d8bef9SDimitry Andric return; 79e8d8bef9SDimitry Andric } 80e8d8bef9SDimitry Andric 81e8d8bef9SDimitry Andric assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target"); 82e8d8bef9SDimitry Andric 83e8d8bef9SDimitry Andric // In the worst case, for a full 64-bit constant, a sequence of 8 instructions 84349cc55cSDimitry Andric // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emitted. Note 85e8d8bef9SDimitry Andric // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits 86e8d8bef9SDimitry Andric // while the following ADDI instructions contribute up to 12 bits each. 87e8d8bef9SDimitry Andric // 88e8d8bef9SDimitry Andric // On the first glance, implementing this seems to be possible by simply 89e8d8bef9SDimitry Andric // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left 90e8d8bef9SDimitry Andric // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the 91e8d8bef9SDimitry Andric // fact that ADDI performs a sign extended addition, doing it like that would 92e8d8bef9SDimitry Andric // only be possible when at most 11 bits of the ADDI instructions are used. 93e8d8bef9SDimitry Andric // Using all 12 bits of the ADDI instructions, like done by GAS, actually 94e8d8bef9SDimitry Andric // requires that the constant is processed starting with the least significant 95e8d8bef9SDimitry Andric // bit. 96e8d8bef9SDimitry Andric // 97e8d8bef9SDimitry Andric // In the following, constants are processed from LSB to MSB but instruction 98e8d8bef9SDimitry Andric // emission is performed from MSB to LSB by recursively calling 99e8d8bef9SDimitry Andric // generateInstSeq. In each recursion, first the lowest 12 bits are removed 100e8d8bef9SDimitry Andric // from the constant and the optimal shift amount, which can be greater than 101e8d8bef9SDimitry Andric // 12 bits if the constant is sparse, is determined. Then, the shifted 102e8d8bef9SDimitry Andric // remaining constant is processed recursively and gets emitted as soon as it 103e8d8bef9SDimitry Andric // fits into 32 bits. The emission of the shifts and additions is subsequently 104e8d8bef9SDimitry Andric // performed when the recursion returns. 105e8d8bef9SDimitry Andric 106e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 10781ad6265SDimitry Andric Val = (uint64_t)Val - (uint64_t)Lo12; 10881ad6265SDimitry Andric 10981ad6265SDimitry Andric int ShiftAmount = 0; 11081ad6265SDimitry Andric bool Unsigned = false; 11181ad6265SDimitry Andric 11281ad6265SDimitry Andric // Val might now be valid for LUI without needing a shift. 11381ad6265SDimitry Andric if (!isInt<32>(Val)) { 114bdd1243dSDimitry Andric ShiftAmount = llvm::countr_zero((uint64_t)Val); 11581ad6265SDimitry Andric Val >>= ShiftAmount; 116e8d8bef9SDimitry Andric 117*0fca6ea1SDimitry Andric // If the remaining bits don't fit in 12 bits, we might be able to reduce 118*0fca6ea1SDimitry Andric // the // shift amount in order to use LUI which will zero the lower 12 119*0fca6ea1SDimitry Andric // bits. 12081ad6265SDimitry Andric if (ShiftAmount > 12 && !isInt<12>(Val)) { 12181ad6265SDimitry Andric if (isInt<32>((uint64_t)Val << 12)) { 122*0fca6ea1SDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match 123*0fca6ea1SDimitry Andric // LUI. 124fe6060f1SDimitry Andric ShiftAmount -= 12; 12581ad6265SDimitry Andric Val = (uint64_t)Val << 12; 12681ad6265SDimitry Andric } else if (isUInt<32>((uint64_t)Val << 12) && 1275f757f3fSDimitry Andric STI.hasFeature(RISCV::FeatureStdExtZba)) { 128349cc55cSDimitry Andric // Reduce the shift amount and add zeros to the LSBs so it will match 129349cc55cSDimitry Andric // LUI, then shift left with SLLI.UW to clear the upper 32 set bits. 130349cc55cSDimitry Andric ShiftAmount -= 12; 13181ad6265SDimitry Andric Val = ((uint64_t)Val << 12) | (0xffffffffull << 32); 132349cc55cSDimitry Andric Unsigned = true; 133349cc55cSDimitry Andric } 134349cc55cSDimitry Andric } 135349cc55cSDimitry Andric 13681ad6265SDimitry Andric // Try to use SLLI_UW for Val when it is uint32 but not int32. 13781ad6265SDimitry Andric if (isUInt<32>((uint64_t)Val) && !isInt<32>((uint64_t)Val) && 1385f757f3fSDimitry Andric STI.hasFeature(RISCV::FeatureStdExtZba)) { 1391fd87a68SDimitry Andric // Use LUI+ADDI or LUI to compose, then clear the upper 32 bits with 1401fd87a68SDimitry Andric // SLLI_UW. 14181ad6265SDimitry Andric Val = ((uint64_t)Val) | (0xffffffffull << 32); 142349cc55cSDimitry Andric Unsigned = true; 143e8d8bef9SDimitry Andric } 14481ad6265SDimitry Andric } 145e8d8bef9SDimitry Andric 1465f757f3fSDimitry Andric generateInstSeqImpl(Val, STI, Res); 147fe6060f1SDimitry Andric 14881ad6265SDimitry Andric // Skip shift if we were able to use LUI directly. 14981ad6265SDimitry Andric if (ShiftAmount) { 150bdd1243dSDimitry Andric unsigned Opc = Unsigned ? RISCV::SLLI_UW : RISCV::SLLI; 151bdd1243dSDimitry Andric Res.emplace_back(Opc, ShiftAmount); 15281ad6265SDimitry Andric } 15381ad6265SDimitry Andric 154fe6060f1SDimitry Andric if (Lo12) 155bdd1243dSDimitry Andric Res.emplace_back(RISCV::ADDI, Lo12); 156fe6060f1SDimitry Andric } 157fe6060f1SDimitry Andric 15804eeddc0SDimitry Andric static unsigned extractRotateInfo(int64_t Val) { 15904eeddc0SDimitry Andric // for case: 0b111..1..xxxxxx1..1.. 16006c3fb27SDimitry Andric unsigned LeadingOnes = llvm::countl_one((uint64_t)Val); 16106c3fb27SDimitry Andric unsigned TrailingOnes = llvm::countr_one((uint64_t)Val); 16204eeddc0SDimitry Andric if (TrailingOnes > 0 && TrailingOnes < 64 && 16304eeddc0SDimitry Andric (LeadingOnes + TrailingOnes) > (64 - 12)) 16404eeddc0SDimitry Andric return 64 - TrailingOnes; 16504eeddc0SDimitry Andric 16604eeddc0SDimitry Andric // for case: 0bxxx1..1..1...xxx 16706c3fb27SDimitry Andric unsigned UpperTrailingOnes = llvm::countr_one(Hi_32(Val)); 16806c3fb27SDimitry Andric unsigned LowerLeadingOnes = llvm::countl_one(Lo_32(Val)); 16904eeddc0SDimitry Andric if (UpperTrailingOnes < 32 && 17004eeddc0SDimitry Andric (UpperTrailingOnes + LowerLeadingOnes) > (64 - 12)) 17104eeddc0SDimitry Andric return 32 - UpperTrailingOnes; 17204eeddc0SDimitry Andric 17304eeddc0SDimitry Andric return 0; 17404eeddc0SDimitry Andric } 17504eeddc0SDimitry Andric 1765f757f3fSDimitry Andric static void generateInstSeqLeadingZeros(int64_t Val, const MCSubtargetInfo &STI, 1775f757f3fSDimitry Andric RISCVMatInt::InstSeq &Res) { 1785f757f3fSDimitry Andric assert(Val > 0 && "Expected postive val"); 1795f757f3fSDimitry Andric 1805f757f3fSDimitry Andric unsigned LeadingZeros = llvm::countl_zero((uint64_t)Val); 1815f757f3fSDimitry Andric uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros; 1825f757f3fSDimitry Andric // Fill in the bits that will be shifted out with 1s. An example where this 1835f757f3fSDimitry Andric // helps is trailing one masks with 32 or more ones. This will generate 1845f757f3fSDimitry Andric // ADDI -1 and an SRLI. 1855f757f3fSDimitry Andric ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros); 1865f757f3fSDimitry Andric 1875f757f3fSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 1885f757f3fSDimitry Andric generateInstSeqImpl(ShiftedVal, STI, TmpSeq); 1895f757f3fSDimitry Andric 1905f757f3fSDimitry Andric // Keep the new sequence if it is an improvement or the original is empty. 1915f757f3fSDimitry Andric if ((TmpSeq.size() + 1) < Res.size() || 1925f757f3fSDimitry Andric (Res.empty() && TmpSeq.size() < 8)) { 1935f757f3fSDimitry Andric TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros); 1945f757f3fSDimitry Andric Res = TmpSeq; 1955f757f3fSDimitry Andric } 1965f757f3fSDimitry Andric 1975f757f3fSDimitry Andric // Some cases can benefit from filling the lower bits with zeros instead. 1985f757f3fSDimitry Andric ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros); 1995f757f3fSDimitry Andric TmpSeq.clear(); 2005f757f3fSDimitry Andric generateInstSeqImpl(ShiftedVal, STI, TmpSeq); 2015f757f3fSDimitry Andric 2025f757f3fSDimitry Andric // Keep the new sequence if it is an improvement or the original is empty. 2035f757f3fSDimitry Andric if ((TmpSeq.size() + 1) < Res.size() || 2045f757f3fSDimitry Andric (Res.empty() && TmpSeq.size() < 8)) { 2055f757f3fSDimitry Andric TmpSeq.emplace_back(RISCV::SRLI, LeadingZeros); 2065f757f3fSDimitry Andric Res = TmpSeq; 2075f757f3fSDimitry Andric } 2085f757f3fSDimitry Andric 2095f757f3fSDimitry Andric // If we have exactly 32 leading zeros and Zba, we can try using zext.w at 2105f757f3fSDimitry Andric // the end of the sequence. 2115f757f3fSDimitry Andric if (LeadingZeros == 32 && STI.hasFeature(RISCV::FeatureStdExtZba)) { 2125f757f3fSDimitry Andric // Try replacing upper bits with 1. 2135f757f3fSDimitry Andric uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros); 2145f757f3fSDimitry Andric TmpSeq.clear(); 2155f757f3fSDimitry Andric generateInstSeqImpl(LeadingOnesVal, STI, TmpSeq); 2165f757f3fSDimitry Andric 2175f757f3fSDimitry Andric // Keep the new sequence if it is an improvement. 2185f757f3fSDimitry Andric if ((TmpSeq.size() + 1) < Res.size() || 2195f757f3fSDimitry Andric (Res.empty() && TmpSeq.size() < 8)) { 2205f757f3fSDimitry Andric TmpSeq.emplace_back(RISCV::ADD_UW, 0); 2215f757f3fSDimitry Andric Res = TmpSeq; 2225f757f3fSDimitry Andric } 2235f757f3fSDimitry Andric } 2245f757f3fSDimitry Andric } 2255f757f3fSDimitry Andric 226bdd1243dSDimitry Andric namespace llvm::RISCVMatInt { 2275f757f3fSDimitry Andric InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI) { 228fe6060f1SDimitry Andric RISCVMatInt::InstSeq Res; 2295f757f3fSDimitry Andric generateInstSeqImpl(Val, STI, Res); 230fe6060f1SDimitry Andric 231bdd1243dSDimitry Andric // If the low 12 bits are non-zero, the first expansion may end with an ADDI 232bdd1243dSDimitry Andric // or ADDIW. If there are trailing zeros, try generating a sign extended 233bdd1243dSDimitry Andric // constant with no trailing zeros and use a final SLLI to restore them. 234bdd1243dSDimitry Andric if ((Val & 0xfff) != 0 && (Val & 1) == 0 && Res.size() >= 2) { 23506c3fb27SDimitry Andric unsigned TrailingZeros = llvm::countr_zero((uint64_t)Val); 23681ad6265SDimitry Andric int64_t ShiftedVal = Val >> TrailingZeros; 237bdd1243dSDimitry Andric // If we can use C.LI+C.SLLI instead of LUI+ADDI(W) prefer that since 238bdd1243dSDimitry Andric // its more compressible. But only if LUI+ADDI(W) isn't fusable. 239bdd1243dSDimitry Andric // NOTE: We don't check for C extension to minimize differences in generated 240bdd1243dSDimitry Andric // code. 241bdd1243dSDimitry Andric bool IsShiftedCompressible = 2425f757f3fSDimitry Andric isInt<6>(ShiftedVal) && !STI.hasFeature(RISCV::TuneLUIADDIFusion); 24381ad6265SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 2445f757f3fSDimitry Andric generateInstSeqImpl(ShiftedVal, STI, TmpSeq); 24581ad6265SDimitry Andric 24681ad6265SDimitry Andric // Keep the new sequence if it is an improvement. 24706c3fb27SDimitry Andric if ((TmpSeq.size() + 1) < Res.size() || IsShiftedCompressible) { 24806c3fb27SDimitry Andric TmpSeq.emplace_back(RISCV::SLLI, TrailingZeros); 24981ad6265SDimitry Andric Res = TmpSeq; 25081ad6265SDimitry Andric } 25106c3fb27SDimitry Andric } 25206c3fb27SDimitry Andric 25306c3fb27SDimitry Andric // If we have a 1 or 2 instruction sequence this is the best we can do. This 25406c3fb27SDimitry Andric // will always be true for RV32 and will often be true for RV64. 25506c3fb27SDimitry Andric if (Res.size() <= 2) 25606c3fb27SDimitry Andric return Res; 25706c3fb27SDimitry Andric 2585f757f3fSDimitry Andric assert(STI.hasFeature(RISCV::Feature64Bit) && 25906c3fb27SDimitry Andric "Expected RV32 to only need 2 instructions"); 26081ad6265SDimitry Andric 2615f757f3fSDimitry Andric // If the lower 13 bits are something like 0x17ff, try to add 1 to change the 2625f757f3fSDimitry Andric // lower 13 bits to 0x1800. We can restore this with an ADDI of -1 at the end 2635f757f3fSDimitry Andric // of the sequence. Call generateInstSeqImpl on the new constant which may 2645f757f3fSDimitry Andric // subtract 0xfffffffffffff800 to create another ADDI. This will leave a 2655f757f3fSDimitry Andric // constant with more than 12 trailing zeros for the next recursive step. 2665f757f3fSDimitry Andric if ((Val & 0xfff) != 0 && (Val & 0x1800) == 0x1000) { 2675f757f3fSDimitry Andric int64_t Imm12 = -(0x800 - (Val & 0xfff)); 2685f757f3fSDimitry Andric int64_t AdjustedVal = Val - Imm12; 2695f757f3fSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 2705f757f3fSDimitry Andric generateInstSeqImpl(AdjustedVal, STI, TmpSeq); 2715f757f3fSDimitry Andric 2725f757f3fSDimitry Andric // Keep the new sequence if it is an improvement. 2735f757f3fSDimitry Andric if ((TmpSeq.size() + 1) < Res.size()) { 2745f757f3fSDimitry Andric TmpSeq.emplace_back(RISCV::ADDI, Imm12); 2755f757f3fSDimitry Andric Res = TmpSeq; 2765f757f3fSDimitry Andric } 2775f757f3fSDimitry Andric } 2785f757f3fSDimitry Andric 279fe6060f1SDimitry Andric // If the constant is positive we might be able to generate a shifted constant 280fe6060f1SDimitry Andric // with no leading zeros and use a final SRLI to restore them. 2815f757f3fSDimitry Andric if (Val > 0 && Res.size() > 2) { 2825f757f3fSDimitry Andric generateInstSeqLeadingZeros(Val, STI, Res); 2835f757f3fSDimitry Andric } 284fe6060f1SDimitry Andric 2855f757f3fSDimitry Andric // If the constant is negative, trying inverting and using our trailing zero 2865f757f3fSDimitry Andric // optimizations. Use an xori to invert the final value. 2875f757f3fSDimitry Andric if (Val < 0 && Res.size() > 3) { 2885f757f3fSDimitry Andric uint64_t InvertedVal = ~(uint64_t)Val; 289fe6060f1SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 2905f757f3fSDimitry Andric generateInstSeqLeadingZeros(InvertedVal, STI, TmpSeq); 291fe6060f1SDimitry Andric 2925f757f3fSDimitry Andric // Keep it if we found a sequence that is smaller after inverting. 2935f757f3fSDimitry Andric if (!TmpSeq.empty() && (TmpSeq.size() + 1) < Res.size()) { 2945f757f3fSDimitry Andric TmpSeq.emplace_back(RISCV::XORI, -1); 295fe6060f1SDimitry Andric Res = TmpSeq; 29606c3fb27SDimitry Andric } 29706c3fb27SDimitry Andric } 29806c3fb27SDimitry Andric 29906c3fb27SDimitry Andric // If the Low and High halves are the same, use pack. The pack instruction 30006c3fb27SDimitry Andric // packs the XLEN/2-bit lower halves of rs1 and rs2 into rd, with rs1 in the 30106c3fb27SDimitry Andric // lower half and rs2 in the upper half. 3025f757f3fSDimitry Andric if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZbkb)) { 30306c3fb27SDimitry Andric int64_t LoVal = SignExtend64<32>(Val); 30406c3fb27SDimitry Andric int64_t HiVal = SignExtend64<32>(Val >> 32); 30506c3fb27SDimitry Andric if (LoVal == HiVal) { 30606c3fb27SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 3075f757f3fSDimitry Andric generateInstSeqImpl(LoVal, STI, TmpSeq); 30806c3fb27SDimitry Andric if ((TmpSeq.size() + 1) < Res.size()) { 30906c3fb27SDimitry Andric TmpSeq.emplace_back(RISCV::PACK, 0); 31006c3fb27SDimitry Andric Res = TmpSeq; 31106c3fb27SDimitry Andric } 31206c3fb27SDimitry Andric } 31306c3fb27SDimitry Andric } 314fe6060f1SDimitry Andric 315*0fca6ea1SDimitry Andric // Perform optimization with BSETI in the Zbs extension. 3165f757f3fSDimitry Andric if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZbs)) { 317*0fca6ea1SDimitry Andric // Create a simm32 value for LUI+ADDIW by forcing the upper 33 bits to zero. 318*0fca6ea1SDimitry Andric // Xor that with original value to get which bits should be set by BSETI. 319*0fca6ea1SDimitry Andric uint64_t Lo = Val & 0x7fffffff; 320*0fca6ea1SDimitry Andric uint64_t Hi = Val ^ Lo; 321*0fca6ea1SDimitry Andric assert(Hi != 0); 322349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 323*0fca6ea1SDimitry Andric 324*0fca6ea1SDimitry Andric if (Lo != 0) 325*0fca6ea1SDimitry Andric generateInstSeqImpl(Lo, STI, TmpSeq); 326*0fca6ea1SDimitry Andric 327*0fca6ea1SDimitry Andric if (TmpSeq.size() + llvm::popcount(Hi) < Res.size()) { 328*0fca6ea1SDimitry Andric do { 329*0fca6ea1SDimitry Andric TmpSeq.emplace_back(RISCV::BSETI, llvm::countr_zero(Hi)); 330*0fca6ea1SDimitry Andric Hi &= (Hi - 1); // Clear lowest set bit. 331*0fca6ea1SDimitry Andric } while (Hi != 0); 332349cc55cSDimitry Andric Res = TmpSeq; 333349cc55cSDimitry Andric } 33406c3fb27SDimitry Andric } 335349cc55cSDimitry Andric 336*0fca6ea1SDimitry Andric // Perform optimization with BCLRI in the Zbs extension. 337*0fca6ea1SDimitry Andric if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZbs)) { 338*0fca6ea1SDimitry Andric // Create a simm32 value for LUI+ADDIW by forcing the upper 33 bits to one. 339*0fca6ea1SDimitry Andric // Xor that with original value to get which bits should be cleared by 340*0fca6ea1SDimitry Andric // BCLRI. 341*0fca6ea1SDimitry Andric uint64_t Lo = Val | 0xffffffff80000000; 342*0fca6ea1SDimitry Andric uint64_t Hi = Val ^ Lo; 343*0fca6ea1SDimitry Andric assert(Hi != 0); 344*0fca6ea1SDimitry Andric 345349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 3465f757f3fSDimitry Andric generateInstSeqImpl(Lo, STI, TmpSeq); 347*0fca6ea1SDimitry Andric 348*0fca6ea1SDimitry Andric if (TmpSeq.size() + llvm::popcount(Hi) < Res.size()) { 349*0fca6ea1SDimitry Andric do { 350*0fca6ea1SDimitry Andric TmpSeq.emplace_back(RISCV::BCLRI, llvm::countr_zero(Hi)); 351bdd1243dSDimitry Andric Hi &= (Hi - 1); // Clear lowest set bit. 352*0fca6ea1SDimitry Andric } while (Hi != 0); 353349cc55cSDimitry Andric Res = TmpSeq; 354349cc55cSDimitry Andric } 355349cc55cSDimitry Andric } 356349cc55cSDimitry Andric 357349cc55cSDimitry Andric // Perform optimization with SH*ADD in the Zba extension. 3585f757f3fSDimitry Andric if (Res.size() > 2 && STI.hasFeature(RISCV::FeatureStdExtZba)) { 359349cc55cSDimitry Andric int64_t Div = 0; 360349cc55cSDimitry Andric unsigned Opc = 0; 361349cc55cSDimitry Andric RISCVMatInt::InstSeq TmpSeq; 362349cc55cSDimitry Andric // Select the opcode and divisor. 363349cc55cSDimitry Andric if ((Val % 3) == 0 && isInt<32>(Val / 3)) { 364349cc55cSDimitry Andric Div = 3; 365349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 366349cc55cSDimitry Andric } else if ((Val % 5) == 0 && isInt<32>(Val / 5)) { 367349cc55cSDimitry Andric Div = 5; 368349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 369349cc55cSDimitry Andric } else if ((Val % 9) == 0 && isInt<32>(Val / 9)) { 370349cc55cSDimitry Andric Div = 9; 371349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 372349cc55cSDimitry Andric } 373349cc55cSDimitry Andric // Build the new instruction sequence. 374349cc55cSDimitry Andric if (Div > 0) { 3755f757f3fSDimitry Andric generateInstSeqImpl(Val / Div, STI, TmpSeq); 37606c3fb27SDimitry Andric if ((TmpSeq.size() + 1) < Res.size()) { 377bdd1243dSDimitry Andric TmpSeq.emplace_back(Opc, 0); 378349cc55cSDimitry Andric Res = TmpSeq; 37906c3fb27SDimitry Andric } 3803a9a9c0cSDimitry Andric } else { 381349cc55cSDimitry Andric // Try to use LUI+SH*ADD+ADDI. 382349cc55cSDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull; 383349cc55cSDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 384349cc55cSDimitry Andric Div = 0; 385349cc55cSDimitry Andric if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) { 386349cc55cSDimitry Andric Div = 3; 387349cc55cSDimitry Andric Opc = RISCV::SH1ADD; 388349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) { 389349cc55cSDimitry Andric Div = 5; 390349cc55cSDimitry Andric Opc = RISCV::SH2ADD; 391349cc55cSDimitry Andric } else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) { 392349cc55cSDimitry Andric Div = 9; 393349cc55cSDimitry Andric Opc = RISCV::SH3ADD; 394349cc55cSDimitry Andric } 395349cc55cSDimitry Andric // Build the new instruction sequence. 396349cc55cSDimitry Andric if (Div > 0) { 397349cc55cSDimitry Andric // For Val that has zero Lo12 (implies Val equals to Hi52) should has 398349cc55cSDimitry Andric // already been processed to LUI+SH*ADD by previous optimization. 399349cc55cSDimitry Andric assert(Lo12 != 0 && 400349cc55cSDimitry Andric "unexpected instruction sequence for immediate materialisation"); 4013a9a9c0cSDimitry Andric assert(TmpSeq.empty() && "Expected empty TmpSeq"); 4025f757f3fSDimitry Andric generateInstSeqImpl(Hi52 / Div, STI, TmpSeq); 40306c3fb27SDimitry Andric if ((TmpSeq.size() + 2) < Res.size()) { 404bdd1243dSDimitry Andric TmpSeq.emplace_back(Opc, 0); 405bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::ADDI, Lo12); 406349cc55cSDimitry Andric Res = TmpSeq; 407349cc55cSDimitry Andric } 408349cc55cSDimitry Andric } 4093a9a9c0cSDimitry Andric } 41006c3fb27SDimitry Andric } 411349cc55cSDimitry Andric 41206c3fb27SDimitry Andric // Perform optimization with rori in the Zbb and th.srri in the XTheadBb 41306c3fb27SDimitry Andric // extension. 4145f757f3fSDimitry Andric if (Res.size() > 2 && (STI.hasFeature(RISCV::FeatureStdExtZbb) || 4155f757f3fSDimitry Andric STI.hasFeature(RISCV::FeatureVendorXTHeadBb))) { 41604eeddc0SDimitry Andric if (unsigned Rotate = extractRotateInfo(Val)) { 41704eeddc0SDimitry Andric RISCVMatInt::InstSeq TmpSeq; 41806c3fb27SDimitry Andric uint64_t NegImm12 = llvm::rotl<uint64_t>(Val, Rotate); 41904eeddc0SDimitry Andric assert(isInt<12>(NegImm12)); 420bdd1243dSDimitry Andric TmpSeq.emplace_back(RISCV::ADDI, NegImm12); 4215f757f3fSDimitry Andric TmpSeq.emplace_back(STI.hasFeature(RISCV::FeatureStdExtZbb) 42206c3fb27SDimitry Andric ? RISCV::RORI 42306c3fb27SDimitry Andric : RISCV::TH_SRRI, 42406c3fb27SDimitry Andric Rotate); 42504eeddc0SDimitry Andric Res = TmpSeq; 42604eeddc0SDimitry Andric } 42704eeddc0SDimitry Andric } 428fe6060f1SDimitry Andric return Res; 429fe6060f1SDimitry Andric } 430fe6060f1SDimitry Andric 431*0fca6ea1SDimitry Andric void generateMCInstSeq(int64_t Val, const MCSubtargetInfo &STI, 432*0fca6ea1SDimitry Andric MCRegister DestReg, SmallVectorImpl<MCInst> &Insts) { 433*0fca6ea1SDimitry Andric RISCVMatInt::InstSeq Seq = RISCVMatInt::generateInstSeq(Val, STI); 434*0fca6ea1SDimitry Andric 435*0fca6ea1SDimitry Andric MCRegister SrcReg = RISCV::X0; 436*0fca6ea1SDimitry Andric for (RISCVMatInt::Inst &Inst : Seq) { 437*0fca6ea1SDimitry Andric switch (Inst.getOpndKind()) { 438*0fca6ea1SDimitry Andric case RISCVMatInt::Imm: 439*0fca6ea1SDimitry Andric Insts.push_back(MCInstBuilder(Inst.getOpcode()) 440*0fca6ea1SDimitry Andric .addReg(DestReg) 441*0fca6ea1SDimitry Andric .addImm(Inst.getImm())); 442*0fca6ea1SDimitry Andric break; 443*0fca6ea1SDimitry Andric case RISCVMatInt::RegX0: 444*0fca6ea1SDimitry Andric Insts.push_back(MCInstBuilder(Inst.getOpcode()) 445*0fca6ea1SDimitry Andric .addReg(DestReg) 446*0fca6ea1SDimitry Andric .addReg(SrcReg) 447*0fca6ea1SDimitry Andric .addReg(RISCV::X0)); 448*0fca6ea1SDimitry Andric break; 449*0fca6ea1SDimitry Andric case RISCVMatInt::RegReg: 450*0fca6ea1SDimitry Andric Insts.push_back(MCInstBuilder(Inst.getOpcode()) 451*0fca6ea1SDimitry Andric .addReg(DestReg) 452*0fca6ea1SDimitry Andric .addReg(SrcReg) 453*0fca6ea1SDimitry Andric .addReg(SrcReg)); 454*0fca6ea1SDimitry Andric break; 455*0fca6ea1SDimitry Andric case RISCVMatInt::RegImm: 456*0fca6ea1SDimitry Andric Insts.push_back(MCInstBuilder(Inst.getOpcode()) 457*0fca6ea1SDimitry Andric .addReg(DestReg) 458*0fca6ea1SDimitry Andric .addReg(SrcReg) 459*0fca6ea1SDimitry Andric .addImm(Inst.getImm())); 460*0fca6ea1SDimitry Andric break; 461*0fca6ea1SDimitry Andric } 462*0fca6ea1SDimitry Andric 463*0fca6ea1SDimitry Andric // Only the first instruction has X0 as its source. 464*0fca6ea1SDimitry Andric SrcReg = DestReg; 465*0fca6ea1SDimitry Andric } 466*0fca6ea1SDimitry Andric } 467*0fca6ea1SDimitry Andric 4685f757f3fSDimitry Andric InstSeq generateTwoRegInstSeq(int64_t Val, const MCSubtargetInfo &STI, 4695f757f3fSDimitry Andric unsigned &ShiftAmt, unsigned &AddOpc) { 4705f757f3fSDimitry Andric int64_t LoVal = SignExtend64<32>(Val); 4715f757f3fSDimitry Andric if (LoVal == 0) 4725f757f3fSDimitry Andric return RISCVMatInt::InstSeq(); 4735f757f3fSDimitry Andric 4745f757f3fSDimitry Andric // Subtract the LoVal to emulate the effect of the final ADD. 4755f757f3fSDimitry Andric uint64_t Tmp = (uint64_t)Val - (uint64_t)LoVal; 4765f757f3fSDimitry Andric assert(Tmp != 0); 4775f757f3fSDimitry Andric 4785f757f3fSDimitry Andric // Use trailing zero counts to figure how far we need to shift LoVal to line 4795f757f3fSDimitry Andric // up with the remaining constant. 4805f757f3fSDimitry Andric // TODO: This algorithm assumes all non-zero bits in the low 32 bits of the 4815f757f3fSDimitry Andric // final constant come from LoVal. 4825f757f3fSDimitry Andric unsigned TzLo = llvm::countr_zero((uint64_t)LoVal); 4835f757f3fSDimitry Andric unsigned TzHi = llvm::countr_zero(Tmp); 4845f757f3fSDimitry Andric assert(TzLo < 32 && TzHi >= 32); 4855f757f3fSDimitry Andric ShiftAmt = TzHi - TzLo; 4865f757f3fSDimitry Andric AddOpc = RISCV::ADD; 4875f757f3fSDimitry Andric 4885f757f3fSDimitry Andric if (Tmp == ((uint64_t)LoVal << ShiftAmt)) 4895f757f3fSDimitry Andric return RISCVMatInt::generateInstSeq(LoVal, STI); 4905f757f3fSDimitry Andric 4915f757f3fSDimitry Andric // If we have Zba, we can use (ADD_UW X, (SLLI X, 32)). 4925f757f3fSDimitry Andric if (STI.hasFeature(RISCV::FeatureStdExtZba) && Lo_32(Val) == Hi_32(Val)) { 4935f757f3fSDimitry Andric ShiftAmt = 32; 4945f757f3fSDimitry Andric AddOpc = RISCV::ADD_UW; 4955f757f3fSDimitry Andric return RISCVMatInt::generateInstSeq(LoVal, STI); 4965f757f3fSDimitry Andric } 4975f757f3fSDimitry Andric 4985f757f3fSDimitry Andric return RISCVMatInt::InstSeq(); 4995f757f3fSDimitry Andric } 5005f757f3fSDimitry Andric 5015f757f3fSDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, const MCSubtargetInfo &STI, 502*0fca6ea1SDimitry Andric bool CompressionCost, bool FreeZeroes) { 5035f757f3fSDimitry Andric bool IsRV64 = STI.hasFeature(RISCV::Feature64Bit); 5045f757f3fSDimitry Andric bool HasRVC = CompressionCost && (STI.hasFeature(RISCV::FeatureStdExtC) || 5055f757f3fSDimitry Andric STI.hasFeature(RISCV::FeatureStdExtZca)); 506e8d8bef9SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32; 507e8d8bef9SDimitry Andric 508e8d8bef9SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost 509e8d8bef9SDimitry Andric // of each chunk. 510e8d8bef9SDimitry Andric int Cost = 0; 511e8d8bef9SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) { 512e8d8bef9SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize); 513*0fca6ea1SDimitry Andric if (FreeZeroes && Chunk.getSExtValue() == 0) 514*0fca6ea1SDimitry Andric continue; 5155f757f3fSDimitry Andric InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), STI); 516fe6060f1SDimitry Andric Cost += getInstSeqCost(MatSeq, HasRVC); 517e8d8bef9SDimitry Andric } 518*0fca6ea1SDimitry Andric return std::max(FreeZeroes ? 0 : 1, Cost); 519e8d8bef9SDimitry Andric } 52081ad6265SDimitry Andric 52181ad6265SDimitry Andric OpndKind Inst::getOpndKind() const { 52281ad6265SDimitry Andric switch (Opc) { 52381ad6265SDimitry Andric default: 52481ad6265SDimitry Andric llvm_unreachable("Unexpected opcode!"); 52581ad6265SDimitry Andric case RISCV::LUI: 52681ad6265SDimitry Andric return RISCVMatInt::Imm; 52781ad6265SDimitry Andric case RISCV::ADD_UW: 52881ad6265SDimitry Andric return RISCVMatInt::RegX0; 52981ad6265SDimitry Andric case RISCV::SH1ADD: 53081ad6265SDimitry Andric case RISCV::SH2ADD: 53181ad6265SDimitry Andric case RISCV::SH3ADD: 53206c3fb27SDimitry Andric case RISCV::PACK: 53381ad6265SDimitry Andric return RISCVMatInt::RegReg; 53481ad6265SDimitry Andric case RISCV::ADDI: 53581ad6265SDimitry Andric case RISCV::ADDIW: 5365f757f3fSDimitry Andric case RISCV::XORI: 53781ad6265SDimitry Andric case RISCV::SLLI: 53881ad6265SDimitry Andric case RISCV::SRLI: 53981ad6265SDimitry Andric case RISCV::SLLI_UW: 54081ad6265SDimitry Andric case RISCV::RORI: 54181ad6265SDimitry Andric case RISCV::BSETI: 54281ad6265SDimitry Andric case RISCV::BCLRI: 54306c3fb27SDimitry Andric case RISCV::TH_SRRI: 54481ad6265SDimitry Andric return RISCVMatInt::RegImm; 54581ad6265SDimitry Andric } 54681ad6265SDimitry Andric } 54781ad6265SDimitry Andric 548bdd1243dSDimitry Andric } // namespace llvm::RISCVMatInt 549