1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "../Target.h"
#include "MCTargetDesc/RISCVBaseInfo.h"
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "MCTargetDesc/RISCVMatInt.h"
#include "RISCVInstrInfo.h"
// include computeAvailableFeatures and computeRequiredFeatures.
#define GET_AVAILABLE_OPCODE_CHECKER
#include "RISCVGenInstrInfo.inc"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include <vector>
namespace llvm {
namespace exegesis {
#include "RISCVGenExegesis.inc"
namespace {
// Stores constant value to a general-purpose (integer) register.
static std::vector<MCInst> loadIntReg(const MCSubtargetInfo &STI,
MCRegister Reg, const APInt &Value) {
SmallVector<MCInst, 8> MCInstSeq;
MCRegister DestReg = Reg;
RISCVMatInt::generateMCInstSeq(Value.getSExtValue(), STI, DestReg, MCInstSeq);
std::vector<MCInst> MatIntInstrs(MCInstSeq.begin(), MCInstSeq.end());
return MatIntInstrs;
}
const MCPhysReg ScratchIntReg = RISCV::X30; // t5
// Stores constant bits to a floating-point register.
static std::vector<MCInst> loadFPRegBits(const MCSubtargetInfo &STI,
MCRegister Reg, const APInt &Bits,
unsigned FmvOpcode) {
std::vector<MCInst> Instrs = loadIntReg(STI, ScratchIntReg, Bits);
Instrs.push_back(MCInstBuilder(FmvOpcode).addReg(Reg).addReg(ScratchIntReg));
return Instrs;
}
// main idea is:
// we support APInt only if (represented as double) it has zero fractional
// part: 1.0, 2.0, 3.0, etc... then we can do the trick: write int to tmp reg t5
// and then do FCVT this is only reliable thing in 32-bit mode, otherwise we
// need to use __floatsidf
static std::vector<MCInst> loadFP64RegBits32(const MCSubtargetInfo &STI,
MCRegister Reg,
const APInt &Bits) {
double D = Bits.bitsToDouble();
double IPart;
double FPart = std::modf(D, &IPart);
if (std::abs(FPart) > std::numeric_limits<double>::epsilon()) {
errs() << "loadFP64RegBits32 is not implemented for doubles like " << D
<< ", please remove fractional part\n";
return {};
}
std::vector<MCInst> Instrs = loadIntReg(STI, ScratchIntReg, Bits);
Instrs.push_back(
MCInstBuilder(RISCV::FCVT_D_W).addReg(Reg).addReg(ScratchIntReg));
return Instrs;
}
static MCInst nop() {
// ADDI X0, X0, 0
return MCInstBuilder(RISCV::ADDI)
.addReg(RISCV::X0)
.addReg(RISCV::X0)
.addImm(0);
}
static bool isVectorRegList(MCRegister Reg) {
return RISCV::VRM2RegClass.contains(Reg) ||
RISCV::VRM4RegClass.contains(Reg) ||
RISCV::VRM8RegClass.contains(Reg) ||
RISCV::VRN2M1RegClass.contains(Reg) ||
RISCV::VRN2M2RegClass.contains(Reg) ||
RISCV::VRN2M4RegClass.contains(Reg) ||
RISCV::VRN3M1RegClass.contains(Reg) ||
RISCV::VRN3M2RegClass.contains(Reg) ||
RISCV::VRN4M1RegClass.contains(Reg) ||
RISCV::VRN4M2RegClass.contains(Reg) ||
RISCV::VRN5M1RegClass.contains(Reg) ||
RISCV::VRN6M1RegClass.contains(Reg) ||
RISCV::VRN7M1RegClass.contains(Reg) ||
RISCV::VRN8M1RegClass.contains(Reg);
}
class ExegesisRISCVTarget : public ExegesisTarget {
public:
ExegesisRISCVTarget();
bool matchesArch(Triple::ArchType Arch) const override;
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, MCRegister Reg,
const APInt &Value) const override;
MCRegister getDefaultLoopCounterRegister(const Triple &) const override;
void decrementLoopCounterAndJump(MachineBasicBlock &MBB,
MachineBasicBlock &TargetMBB,
const MCInstrInfo &MII,
MCRegister LoopRegister) const override;
MCRegister getScratchMemoryRegister(const Triple &TT) const override;
void fillMemoryOperands(InstructionTemplate &IT, MCRegister Reg,
unsigned Offset) const override;
ArrayRef<MCPhysReg> getUnavailableRegisters() const override;
bool allowAsBackToBack(const Instruction &Instr) const override {
return !Instr.Description.isPseudo();
}
Error randomizeTargetMCOperand(const Instruction &Instr, const Variable &Var,
MCOperand &AssignedValue,
const BitVector &ForbiddenRegs) const override;
std::vector<InstructionTemplate>
generateInstructionVariants(const Instruction &Instr,
unsigned MaxConfigsPerOpcode) const override;
};
ExegesisRISCVTarget::ExegesisRISCVTarget()
: ExegesisTarget(RISCVCpuPfmCounters, RISCV_MC::isOpcodeAvailable) {}
bool ExegesisRISCVTarget::matchesArch(Triple::ArchType Arch) const {
return Arch == Triple::riscv32 || Arch == Triple::riscv64;
}
std::vector<MCInst> ExegesisRISCVTarget::setRegTo(const MCSubtargetInfo &STI,
MCRegister Reg,
const APInt &Value) const {
if (RISCV::GPRRegClass.contains(Reg))
return loadIntReg(STI, Reg, Value);
if (RISCV::FPR16RegClass.contains(Reg))
return loadFPRegBits(STI, Reg, Value, RISCV::FMV_H_X);
if (RISCV::FPR32RegClass.contains(Reg))
return loadFPRegBits(STI, Reg, Value, RISCV::FMV_W_X);
if (RISCV::FPR64RegClass.contains(Reg)) {
if (STI.hasFeature(RISCV::Feature64Bit))
return loadFPRegBits(STI, Reg, Value, RISCV::FMV_D_X);
return loadFP64RegBits32(STI, Reg, Value);
}
if (Reg == RISCV::FRM || Reg == RISCV::VL || Reg == RISCV::VLENB ||
Reg == RISCV::VTYPE || RISCV::GPRPairRegClass.contains(Reg) ||
RISCV::VRRegClass.contains(Reg) || isVectorRegList(Reg)) {
// Don't initialize:
// - FRM
// - VL, VLENB, VTYPE
// - vector registers (and vector register lists)
// - Zfinx registers
// Generate 'NOP' so that exegesis treats such registers as initialized
// (it tries to initialize them with '0' anyway).
return {nop()};
}
errs() << "setRegTo is not implemented for Reg " << Reg
<< ", results will be unreliable\n";
return {};
}
const MCPhysReg DefaultLoopCounterReg = RISCV::X31; // t6
const MCPhysReg ScratchMemoryReg = RISCV::X10; // a0
MCRegister
ExegesisRISCVTarget::getDefaultLoopCounterRegister(const Triple &) const {
return DefaultLoopCounterReg;
}
void ExegesisRISCVTarget::decrementLoopCounterAndJump(
MachineBasicBlock &MBB, MachineBasicBlock &TargetMBB,
const MCInstrInfo &MII, MCRegister LoopRegister) const {
BuildMI(&MBB, DebugLoc(), MII.get(RISCV::ADDI))
.addDef(LoopRegister)
.addUse(LoopRegister)
.addImm(-1);
BuildMI(&MBB, DebugLoc(), MII.get(RISCV::BNE))
.addUse(LoopRegister)
.addUse(RISCV::X0)
.addMBB(&TargetMBB);
}
MCRegister
ExegesisRISCVTarget::getScratchMemoryRegister(const Triple &TT) const {
return ScratchMemoryReg; // a0
}
void ExegesisRISCVTarget::fillMemoryOperands(InstructionTemplate &IT,
MCRegister Reg,
unsigned Offset) const {
// TODO: for now we ignore Offset because have no way
// to detect it in instruction.
auto &I = IT.getInstr();
auto MemOpIt =
find_if(I.Operands, [](const Operand &Op) { return Op.isMemory(); });
assert(MemOpIt != I.Operands.end() &&
"Instruction must have memory operands");
const Operand &MemOp = *MemOpIt;
assert(MemOp.isReg() && "Memory operand expected to be register");
IT.getValueFor(MemOp) = MCOperand::createReg(Reg);
}
const MCPhysReg UnavailableRegisters[4] = {RISCV::X0, DefaultLoopCounterReg,
ScratchIntReg, ScratchMemoryReg};
ArrayRef<MCPhysReg> ExegesisRISCVTarget::getUnavailableRegisters() const {
return UnavailableRegisters;
}
Error ExegesisRISCVTarget::randomizeTargetMCOperand(
const Instruction &Instr, const Variable &Var, MCOperand &AssignedValue,
const BitVector &ForbiddenRegs) const {
uint8_t OperandType =
Instr.getPrimaryOperand(Var).getExplicitOperandInfo().OperandType;
switch (OperandType) {
case RISCVOp::OPERAND_FRMARG:
AssignedValue = MCOperand::createImm(RISCVFPRndMode::DYN);
break;
case RISCVOp::OPERAND_SIMM10_LSB0000_NONZERO:
AssignedValue = MCOperand::createImm(0b1 << 4);
break;
case RISCVOp::OPERAND_SIMM6_NONZERO:
case RISCVOp::OPERAND_UIMMLOG2XLEN_NONZERO:
AssignedValue = MCOperand::createImm(1);
break;
default:
if (OperandType >= RISCVOp::OPERAND_FIRST_RISCV_IMM &&
OperandType <= RISCVOp::OPERAND_LAST_RISCV_IMM)
AssignedValue = MCOperand::createImm(0);
}
return Error::success();
}
std::vector<InstructionTemplate>
ExegesisRISCVTarget::generateInstructionVariants(
const Instruction &Instr, unsigned int MaxConfigsPerOpcode) const {
InstructionTemplate IT{&Instr};
for (const Operand &Op : Instr.Operands)
if (Op.isMemory()) {
IT.getValueFor(Op) = MCOperand::createReg(ScratchMemoryReg);
}
return {IT};
}
} // anonymous namespace
static ExegesisTarget *getTheRISCVExegesisTarget() {
static ExegesisRISCVTarget Target;
return &Target;
}
void InitializeRISCVExegesisTarget() {
ExegesisTarget::registerTarget(getTheRISCVExegesisTarget());
}
} // namespace exegesis
} // namespace llvm
|