mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-20 19:42:54 +02:00
9cc166efe6
Summary: This patch implements a tablegen-driven Instruction Compression mechanism for generating RISCV compressed instructions (C Extension) from the expanded instruction form. This tablegen backend processes CompressPat declarations in a td file and generates all the compile-time and runtime checks required to validate the declarations, validate the input operands and generate correct instructions. The checks include validating register operands, immediate operands, fixed register operands and fixed immediate operands. Example: class CompressPat<dag input, dag output> { dag Input = input; dag Output = output; list<Predicate> Predicates = []; } let Predicates = [HasStdExtC] in { def : CompressPat<(ADD GPRNoX0:$rs1, GPRNoX0:$rs1, GPRNoX0:$rs2), (C_ADD GPRNoX0:$rs1, GPRNoX0:$rs2)>; } The result is an auto-generated header file 'RISCVGenCompressEmitter.inc' which exports two functions for compressing/uncompressing MCInst instructions, plus some helper functions: bool compressInst(MCInst& OutInst, const MCInst &MI, const MCSubtargetInfo &STI, MCContext &Context); bool uncompressInst(MCInst& OutInst, const MCInst &MI, const MCRegisterInfo &MRI, const MCSubtargetInfo &STI); The clients that include this auto-generated header file and invoke these functions can compress an instruction before emitting it, in the target-specific ASM or ELF streamer, or can uncompress an instruction before printing it, when the expanded instruction format aliases is favored. The following clients were added to implement compression\uncompression for RISCV: 1) RISCVAsmParser::MatchAndEmitInstruction: Inserted a call to compressInst() to compresses instructions parsed by llvm-mc coming from an ASM input. 2) RISCVAsmPrinter::EmitInstruction: Inserted a call to compressInst() to compress instructions that were lowered from Machine Instructions (MachineInstr). 3) RVInstPrinter::printInst: Inserted a call to uncompressInst() to print the expanded version of the instruction instead of the compressed one (e.g, add s0, s0, a5 instead of c.add s0, a5) when -riscv-no-aliases is not passed. This patch squashes D45119, D42780 and D41932. It was reviewed in smaller patches by asb, efriedma, apazos and mgrang. Reviewers: asb, efriedma, apazos, llvm-commits, sabuasal Reviewed By: sabuasal Subscribers: mgorny, eraman, asb, rbar, johnrusso, simoncook, jordy.potman.lists, apazos, niosHD, kito-cheng, shiva0217, zzheng Differential Revision: https://reviews.llvm.org/D45385 llvm-svn: 329455
104 lines
3.0 KiB
C++
104 lines
3.0 KiB
C++
//===-- RISCVMCExpr.cpp - RISCV specific MC expression classes ------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains the implementation of the assembly expression modifiers
|
|
// accepted by the RISCV architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "RISCV.h"
|
|
#include "RISCVMCExpr.h"
|
|
#include "llvm/MC/MCAssembler.h"
|
|
#include "llvm/MC/MCContext.h"
|
|
#include "llvm/MC/MCStreamer.h"
|
|
#include "llvm/MC/MCSymbolELF.h"
|
|
#include "llvm/MC/MCValue.h"
|
|
#include "llvm/Object/ELF.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "riscvmcexpr"
|
|
|
|
const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, VariantKind Kind,
|
|
MCContext &Ctx) {
|
|
return new (Ctx) RISCVMCExpr(Expr, Kind);
|
|
}
|
|
|
|
void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
|
|
bool HasVariant = getKind() != VK_RISCV_None;
|
|
if (HasVariant)
|
|
OS << '%' << getVariantKindName(getKind()) << '(';
|
|
Expr->print(OS, MAI);
|
|
if (HasVariant)
|
|
OS << ')';
|
|
}
|
|
|
|
bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
|
|
const MCAsmLayout *Layout,
|
|
const MCFixup *Fixup) const {
|
|
return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
|
|
}
|
|
|
|
void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
|
|
Streamer.visitUsedExpr(*getSubExpr());
|
|
}
|
|
|
|
RISCVMCExpr::VariantKind RISCVMCExpr::getVariantKindForName(StringRef name) {
|
|
return StringSwitch<RISCVMCExpr::VariantKind>(name)
|
|
.Case("lo", VK_RISCV_LO)
|
|
.Case("hi", VK_RISCV_HI)
|
|
.Case("pcrel_lo", VK_RISCV_PCREL_LO)
|
|
.Case("pcrel_hi", VK_RISCV_PCREL_HI)
|
|
.Default(VK_RISCV_Invalid);
|
|
}
|
|
|
|
StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) {
|
|
switch (Kind) {
|
|
default:
|
|
llvm_unreachable("Invalid ELF symbol kind");
|
|
case VK_RISCV_LO:
|
|
return "lo";
|
|
case VK_RISCV_HI:
|
|
return "hi";
|
|
case VK_RISCV_PCREL_LO:
|
|
return "pcrel_lo";
|
|
case VK_RISCV_PCREL_HI:
|
|
return "pcrel_hi";
|
|
}
|
|
}
|
|
|
|
bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
|
|
MCValue Value;
|
|
|
|
if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO)
|
|
return false;
|
|
|
|
if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
|
|
return false;
|
|
|
|
if (!Value.isAbsolute())
|
|
return false;
|
|
|
|
Res = evaluateAsInt64(Value.getConstant());
|
|
return true;
|
|
}
|
|
|
|
int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
|
|
switch (Kind) {
|
|
default:
|
|
llvm_unreachable("Invalid kind");
|
|
case VK_RISCV_LO:
|
|
return SignExtend64<12>(Value);
|
|
case VK_RISCV_HI:
|
|
// Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
|
|
return ((Value + 0x800) >> 12) & 0xfffff;
|
|
}
|
|
}
|