1*e8d8bef9SDimitry Andric //===- RISCVMatInt.cpp - Immediate materialisation -------------*- C++ -*--===// 2*e8d8bef9SDimitry Andric // 3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*e8d8bef9SDimitry Andric // 7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8*e8d8bef9SDimitry Andric 9*e8d8bef9SDimitry Andric #include "RISCVMatInt.h" 10*e8d8bef9SDimitry Andric #include "MCTargetDesc/RISCVMCTargetDesc.h" 11*e8d8bef9SDimitry Andric #include "llvm/ADT/APInt.h" 12*e8d8bef9SDimitry Andric #include "llvm/Support/MathExtras.h" 13*e8d8bef9SDimitry Andric 14*e8d8bef9SDimitry Andric namespace llvm { 15*e8d8bef9SDimitry Andric 16*e8d8bef9SDimitry Andric namespace RISCVMatInt { 17*e8d8bef9SDimitry Andric void generateInstSeq(int64_t Val, bool IsRV64, InstSeq &Res) { 18*e8d8bef9SDimitry Andric if (isInt<32>(Val)) { 19*e8d8bef9SDimitry Andric // Depending on the active bits in the immediate Value v, the following 20*e8d8bef9SDimitry Andric // instruction sequences are emitted: 21*e8d8bef9SDimitry Andric // 22*e8d8bef9SDimitry Andric // v == 0 : ADDI 23*e8d8bef9SDimitry Andric // v[0,12) != 0 && v[12,32) == 0 : ADDI 24*e8d8bef9SDimitry Andric // v[0,12) == 0 && v[12,32) != 0 : LUI 25*e8d8bef9SDimitry Andric // v[0,32) != 0 : LUI+ADDI(W) 26*e8d8bef9SDimitry Andric int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF; 27*e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 28*e8d8bef9SDimitry Andric 29*e8d8bef9SDimitry Andric if (Hi20) 30*e8d8bef9SDimitry Andric Res.push_back(Inst(RISCV::LUI, Hi20)); 31*e8d8bef9SDimitry Andric 32*e8d8bef9SDimitry Andric if (Lo12 || Hi20 == 0) { 33*e8d8bef9SDimitry Andric unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI; 34*e8d8bef9SDimitry Andric Res.push_back(Inst(AddiOpc, Lo12)); 35*e8d8bef9SDimitry Andric } 36*e8d8bef9SDimitry Andric return; 37*e8d8bef9SDimitry Andric } 38*e8d8bef9SDimitry Andric 39*e8d8bef9SDimitry Andric assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target"); 40*e8d8bef9SDimitry Andric 41*e8d8bef9SDimitry Andric // In the worst case, for a full 64-bit constant, a sequence of 8 instructions 42*e8d8bef9SDimitry Andric // (i.e., LUI+ADDIW+SLLI+ADDI+SLLI+ADDI+SLLI+ADDI) has to be emmitted. Note 43*e8d8bef9SDimitry Andric // that the first two instructions (LUI+ADDIW) can contribute up to 32 bits 44*e8d8bef9SDimitry Andric // while the following ADDI instructions contribute up to 12 bits each. 45*e8d8bef9SDimitry Andric // 46*e8d8bef9SDimitry Andric // On the first glance, implementing this seems to be possible by simply 47*e8d8bef9SDimitry Andric // emitting the most significant 32 bits (LUI+ADDIW) followed by as many left 48*e8d8bef9SDimitry Andric // shift (SLLI) and immediate additions (ADDI) as needed. However, due to the 49*e8d8bef9SDimitry Andric // fact that ADDI performs a sign extended addition, doing it like that would 50*e8d8bef9SDimitry Andric // only be possible when at most 11 bits of the ADDI instructions are used. 51*e8d8bef9SDimitry Andric // Using all 12 bits of the ADDI instructions, like done by GAS, actually 52*e8d8bef9SDimitry Andric // requires that the constant is processed starting with the least significant 53*e8d8bef9SDimitry Andric // bit. 54*e8d8bef9SDimitry Andric // 55*e8d8bef9SDimitry Andric // In the following, constants are processed from LSB to MSB but instruction 56*e8d8bef9SDimitry Andric // emission is performed from MSB to LSB by recursively calling 57*e8d8bef9SDimitry Andric // generateInstSeq. In each recursion, first the lowest 12 bits are removed 58*e8d8bef9SDimitry Andric // from the constant and the optimal shift amount, which can be greater than 59*e8d8bef9SDimitry Andric // 12 bits if the constant is sparse, is determined. Then, the shifted 60*e8d8bef9SDimitry Andric // remaining constant is processed recursively and gets emitted as soon as it 61*e8d8bef9SDimitry Andric // fits into 32 bits. The emission of the shifts and additions is subsequently 62*e8d8bef9SDimitry Andric // performed when the recursion returns. 63*e8d8bef9SDimitry Andric 64*e8d8bef9SDimitry Andric int64_t Lo12 = SignExtend64<12>(Val); 65*e8d8bef9SDimitry Andric int64_t Hi52 = ((uint64_t)Val + 0x800ull) >> 12; 66*e8d8bef9SDimitry Andric int ShiftAmount = 12 + findFirstSet((uint64_t)Hi52); 67*e8d8bef9SDimitry Andric Hi52 = SignExtend64(Hi52 >> (ShiftAmount - 12), 64 - ShiftAmount); 68*e8d8bef9SDimitry Andric 69*e8d8bef9SDimitry Andric generateInstSeq(Hi52, IsRV64, Res); 70*e8d8bef9SDimitry Andric 71*e8d8bef9SDimitry Andric Res.push_back(Inst(RISCV::SLLI, ShiftAmount)); 72*e8d8bef9SDimitry Andric if (Lo12) 73*e8d8bef9SDimitry Andric Res.push_back(Inst(RISCV::ADDI, Lo12)); 74*e8d8bef9SDimitry Andric } 75*e8d8bef9SDimitry Andric 76*e8d8bef9SDimitry Andric int getIntMatCost(const APInt &Val, unsigned Size, bool IsRV64) { 77*e8d8bef9SDimitry Andric int PlatRegSize = IsRV64 ? 64 : 32; 78*e8d8bef9SDimitry Andric 79*e8d8bef9SDimitry Andric // Split the constant into platform register sized chunks, and calculate cost 80*e8d8bef9SDimitry Andric // of each chunk. 81*e8d8bef9SDimitry Andric int Cost = 0; 82*e8d8bef9SDimitry Andric for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) { 83*e8d8bef9SDimitry Andric APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize); 84*e8d8bef9SDimitry Andric InstSeq MatSeq; 85*e8d8bef9SDimitry Andric generateInstSeq(Chunk.getSExtValue(), IsRV64, MatSeq); 86*e8d8bef9SDimitry Andric Cost += MatSeq.size(); 87*e8d8bef9SDimitry Andric } 88*e8d8bef9SDimitry Andric return std::max(1, Cost); 89*e8d8bef9SDimitry Andric } 90*e8d8bef9SDimitry Andric } // namespace RISCVMatInt 91*e8d8bef9SDimitry Andric } // namespace llvm 92