#include "RISCVMatInt.h"
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "llvm/ADT/APInt.h"
#include "llvm/Support/MathExtras.h"
using namespace llvm;
static int getInstSeqCost(RISCVMatInt::InstSeq &Res, bool HasRVC) {
if (!HasRVC)
return Res.size();
int Cost = 0;
for (auto Instr : Res) {
bool Compressed = false;
switch (Instr.Opc) {
case RISCV::SLLI:
case RISCV::SRLI:
Compressed = true;
break;
case RISCV::ADDI:
case RISCV::ADDIW:
case RISCV::LUI:
Compressed = isInt<6>(Instr.Imm);
break;
}
if (!Compressed)
Cost += 100; else
Cost += 70; }
return Cost;
}
static void generateInstSeqImpl(int64_t Val,
const FeatureBitset &ActiveFeatures,
RISCVMatInt::InstSeq &Res) {
bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit];
if (isInt<32>(Val)) {
int64_t Hi20 = ((Val + 0x800) >> 12) & 0xFFFFF;
int64_t Lo12 = SignExtend64<12>(Val);
if (Hi20)
Res.push_back(RISCVMatInt::Inst(RISCV::LUI, Hi20));
if (Lo12 || Hi20 == 0) {
unsigned AddiOpc = (IsRV64 && Hi20) ? RISCV::ADDIW : RISCV::ADDI;
Res.push_back(RISCVMatInt::Inst(AddiOpc, Lo12));
}
return;
}
assert(IsRV64 && "Can't emit >32-bit imm for non-RV64 target");
if (ActiveFeatures[RISCV::FeatureStdExtZbs] && isPowerOf2_64(Val)) {
Res.push_back(RISCVMatInt::Inst(RISCV::BSETI, Log2_64(Val)));
return;
}
int64_t Lo12 = SignExtend64<12>(Val);
Val = (uint64_t)Val - (uint64_t)Lo12;
int ShiftAmount = 0;
bool Unsigned = false;
if (!isInt<32>(Val)) {
ShiftAmount = findFirstSet((uint64_t)Val);
Val >>= ShiftAmount;
if (ShiftAmount > 12 && !isInt<12>(Val)) {
if (isInt<32>((uint64_t)Val << 12)) {
ShiftAmount -= 12;
Val = (uint64_t)Val << 12;
} else if (isUInt<32>((uint64_t)Val << 12) &&
ActiveFeatures[RISCV::FeatureStdExtZba]) {
ShiftAmount -= 12;
Val = ((uint64_t)Val << 12) | (0xffffffffull << 32);
Unsigned = true;
}
}
if (isUInt<32>((uint64_t)Val) && !isInt<32>((uint64_t)Val) &&
ActiveFeatures[RISCV::FeatureStdExtZba]) {
Val = ((uint64_t)Val) | (0xffffffffull << 32);
Unsigned = true;
}
}
generateInstSeqImpl(Val, ActiveFeatures, Res);
if (ShiftAmount) {
if (Unsigned)
Res.push_back(RISCVMatInt::Inst(RISCV::SLLI_UW, ShiftAmount));
else
Res.push_back(RISCVMatInt::Inst(RISCV::SLLI, ShiftAmount));
}
if (Lo12)
Res.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12));
}
static unsigned extractRotateInfo(int64_t Val) {
unsigned LeadingOnes = countLeadingOnes((uint64_t)Val);
unsigned TrailingOnes = countTrailingOnes((uint64_t)Val);
if (TrailingOnes > 0 && TrailingOnes < 64 &&
(LeadingOnes + TrailingOnes) > (64 - 12))
return 64 - TrailingOnes;
unsigned UpperTrailingOnes = countTrailingOnes(Hi_32(Val));
unsigned LowerLeadingOnes = countLeadingOnes(Lo_32(Val));
if (UpperTrailingOnes < 32 &&
(UpperTrailingOnes + LowerLeadingOnes) > (64 - 12))
return 32 - UpperTrailingOnes;
return 0;
}
namespace llvm {
namespace RISCVMatInt {
InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) {
RISCVMatInt::InstSeq Res;
generateInstSeqImpl(Val, ActiveFeatures, Res);
if ((Val & 1) == 0 && Res.size() > 2) {
unsigned TrailingZeros = countTrailingZeros((uint64_t)Val);
int64_t ShiftedVal = Val >> TrailingZeros;
RISCVMatInt::InstSeq TmpSeq;
generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SLLI, TrailingZeros));
if (TmpSeq.size() < Res.size()) {
Res = TmpSeq;
if (Res.size() <= 2)
return Res;
}
}
if (Val > 0 && Res.size() > 2) {
assert(ActiveFeatures[RISCV::Feature64Bit] &&
"Expected RV32 to only need 2 instructions");
unsigned LeadingZeros = countLeadingZeros((uint64_t)Val);
uint64_t ShiftedVal = (uint64_t)Val << LeadingZeros;
ShiftedVal |= maskTrailingOnes<uint64_t>(LeadingZeros);
RISCVMatInt::InstSeq TmpSeq;
generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros));
if (TmpSeq.size() < Res.size()) {
Res = TmpSeq;
if (Res.size() <= 2)
return Res;
}
ShiftedVal &= maskTrailingZeros<uint64_t>(LeadingZeros);
TmpSeq.clear();
generateInstSeqImpl(ShiftedVal, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::SRLI, LeadingZeros));
if (TmpSeq.size() < Res.size()) {
Res = TmpSeq;
if (Res.size() <= 2)
return Res;
}
if (LeadingZeros == 32 && ActiveFeatures[RISCV::FeatureStdExtZba]) {
uint64_t LeadingOnesVal = Val | maskLeadingOnes<uint64_t>(LeadingZeros);
TmpSeq.clear();
generateInstSeqImpl(LeadingOnesVal, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADD_UW, 0));
if (TmpSeq.size() < Res.size()) {
Res = TmpSeq;
if (Res.size() <= 2)
return Res;
}
}
}
if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) {
assert(ActiveFeatures[RISCV::Feature64Bit] &&
"Expected RV32 to only need 2 instructions");
int64_t NewVal;
unsigned Opc;
if (Val < 0) {
Opc = RISCV::BCLRI;
NewVal = Val | 0x80000000ll;
} else {
Opc = RISCV::BSETI;
NewVal = Val & ~0x80000000ll;
}
if (isInt<32>(NewVal)) {
RISCVMatInt::InstSeq TmpSeq;
generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(Opc, 31));
if (TmpSeq.size() < Res.size())
Res = TmpSeq;
}
int32_t Lo = Val;
uint32_t Hi = Val >> 32;
Opc = 0;
RISCVMatInt::InstSeq TmpSeq;
generateInstSeqImpl(Lo, ActiveFeatures, TmpSeq);
if (Lo > 0 && TmpSeq.size() + countPopulation(Hi) < Res.size()) {
Opc = RISCV::BSETI;
} else if (Lo < 0 && TmpSeq.size() + countPopulation(~Hi) < Res.size()) {
Opc = RISCV::BCLRI;
Hi = ~Hi;
}
if (Opc > 0) {
while (Hi != 0) {
unsigned Bit = countTrailingZeros(Hi);
TmpSeq.push_back(RISCVMatInt::Inst(Opc, Bit + 32));
Hi &= ~(1 << Bit);
}
if (TmpSeq.size() < Res.size())
Res = TmpSeq;
}
}
if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZba]) {
assert(ActiveFeatures[RISCV::Feature64Bit] &&
"Expected RV32 to only need 2 instructions");
int64_t Div = 0;
unsigned Opc = 0;
RISCVMatInt::InstSeq TmpSeq;
if ((Val % 3) == 0 && isInt<32>(Val / 3)) {
Div = 3;
Opc = RISCV::SH1ADD;
} else if ((Val % 5) == 0 && isInt<32>(Val / 5)) {
Div = 5;
Opc = RISCV::SH2ADD;
} else if ((Val % 9) == 0 && isInt<32>(Val / 9)) {
Div = 9;
Opc = RISCV::SH3ADD;
}
if (Div > 0) {
generateInstSeqImpl(Val / Div, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0));
if (TmpSeq.size() < Res.size())
Res = TmpSeq;
} else {
int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull;
int64_t Lo12 = SignExtend64<12>(Val);
Div = 0;
if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) {
Div = 3;
Opc = RISCV::SH1ADD;
} else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) {
Div = 5;
Opc = RISCV::SH2ADD;
} else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) {
Div = 9;
Opc = RISCV::SH3ADD;
}
if (Div > 0) {
assert(Lo12 != 0 &&
"unexpected instruction sequence for immediate materialisation");
assert(TmpSeq.empty() && "Expected empty TmpSeq");
generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq);
TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0));
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12));
if (TmpSeq.size() < Res.size())
Res = TmpSeq;
}
}
}
if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbb]) {
if (unsigned Rotate = extractRotateInfo(Val)) {
RISCVMatInt::InstSeq TmpSeq;
uint64_t NegImm12 =
((uint64_t)Val >> (64 - Rotate)) | ((uint64_t)Val << Rotate);
assert(isInt<12>(NegImm12));
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, NegImm12));
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::RORI, Rotate));
Res = TmpSeq;
}
}
return Res;
}
int getIntMatCost(const APInt &Val, unsigned Size,
const FeatureBitset &ActiveFeatures, bool CompressionCost) {
bool IsRV64 = ActiveFeatures[RISCV::Feature64Bit];
bool HasRVC = CompressionCost && ActiveFeatures[RISCV::FeatureStdExtC];
int PlatRegSize = IsRV64 ? 64 : 32;
int Cost = 0;
for (unsigned ShiftVal = 0; ShiftVal < Size; ShiftVal += PlatRegSize) {
APInt Chunk = Val.ashr(ShiftVal).sextOrTrunc(PlatRegSize);
InstSeq MatSeq = generateInstSeq(Chunk.getSExtValue(), ActiveFeatures);
Cost += getInstSeqCost(MatSeq, HasRVC);
}
return std::max(1, Cost);
}
OpndKind Inst::getOpndKind() const {
switch (Opc) {
default:
llvm_unreachable("Unexpected opcode!");
case RISCV::LUI:
return RISCVMatInt::Imm;
case RISCV::ADD_UW:
return RISCVMatInt::RegX0;
case RISCV::SH1ADD:
case RISCV::SH2ADD:
case RISCV::SH3ADD:
return RISCVMatInt::RegReg;
case RISCV::ADDI:
case RISCV::ADDIW:
case RISCV::SLLI:
case RISCV::SRLI:
case RISCV::SLLI_UW:
case RISCV::RORI:
case RISCV::BSETI:
case RISCV::BCLRI:
return RISCVMatInt::RegImm;
}
}
} }