2017-10-20 05:37:38 +08:00
|
|
|
//===-- RISCVMCInstLower.cpp - Convert RISCV MachineInstr to an MCInst ------=//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-10-20 05:37:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains code to lower RISCV MachineInstrs to their corresponding
|
|
|
|
// MCInst records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RISCV.h"
|
2017-11-08 21:24:21 +08:00
|
|
|
#include "MCTargetDesc/RISCVMCExpr.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2017-10-20 05:37:38 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-11-08 21:24:21 +08:00
|
|
|
static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
|
|
|
|
const AsmPrinter &AP) {
|
|
|
|
MCContext &Ctx = AP.OutContext;
|
|
|
|
RISCVMCExpr::VariantKind Kind;
|
|
|
|
|
|
|
|
switch (MO.getTargetFlags()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown target flag on GV operand");
|
|
|
|
case RISCVII::MO_None:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_None;
|
|
|
|
break;
|
2019-04-01 22:53:17 +08:00
|
|
|
case RISCVII::MO_CALL:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_CALL;
|
|
|
|
break;
|
2019-06-18 22:29:45 +08:00
|
|
|
case RISCVII::MO_PLT:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
|
|
|
|
break;
|
2017-11-08 21:24:21 +08:00
|
|
|
case RISCVII::MO_LO:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_LO;
|
|
|
|
break;
|
|
|
|
case RISCVII::MO_HI:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_HI;
|
|
|
|
break;
|
2019-04-01 22:42:56 +08:00
|
|
|
case RISCVII::MO_PCREL_LO:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_PCREL_LO;
|
|
|
|
break;
|
|
|
|
case RISCVII::MO_PCREL_HI:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_PCREL_HI;
|
|
|
|
break;
|
2019-06-11 20:57:47 +08:00
|
|
|
case RISCVII::MO_GOT_HI:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_GOT_HI;
|
|
|
|
break;
|
2019-06-19 16:40:59 +08:00
|
|
|
case RISCVII::MO_TPREL_LO:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_TPREL_LO;
|
|
|
|
break;
|
|
|
|
case RISCVII::MO_TPREL_HI:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_TPREL_HI;
|
|
|
|
break;
|
|
|
|
case RISCVII::MO_TPREL_ADD:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_TPREL_ADD;
|
|
|
|
break;
|
|
|
|
case RISCVII::MO_TLS_GOT_HI:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_TLS_GOT_HI;
|
|
|
|
break;
|
|
|
|
case RISCVII::MO_TLS_GD_HI:
|
|
|
|
Kind = RISCVMCExpr::VK_RISCV_TLS_GD_HI;
|
|
|
|
break;
|
2017-11-08 21:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const MCExpr *ME =
|
|
|
|
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
|
|
|
|
|
2018-01-11 05:05:07 +08:00
|
|
|
if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
|
2017-11-08 21:24:21 +08:00
|
|
|
ME = MCBinaryExpr::createAdd(
|
|
|
|
ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
|
|
|
|
|
2018-01-11 05:05:07 +08:00
|
|
|
if (Kind != RISCVMCExpr::VK_RISCV_None)
|
|
|
|
ME = RISCVMCExpr::create(ME, Kind, Ctx);
|
2017-11-08 21:24:21 +08:00
|
|
|
return MCOperand::createExpr(ME);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool llvm::LowerRISCVMachineOperandToMCOperand(const MachineOperand &MO,
|
|
|
|
MCOperand &MCOp,
|
|
|
|
const AsmPrinter &AP) {
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
|
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
// Ignore all implicit register operands.
|
|
|
|
if (MO.isImplicit())
|
|
|
|
return false;
|
|
|
|
MCOp = MCOperand::createReg(MO.getReg());
|
|
|
|
break;
|
2017-11-08 21:41:21 +08:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
// Regmasks are like implicit defs.
|
|
|
|
return false;
|
2017-11-08 21:24:21 +08:00
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
MCOp = MCOperand::createImm(MO.getImm());
|
|
|
|
break;
|
2017-11-08 21:31:40 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2018-01-11 05:05:07 +08:00
|
|
|
MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
|
2017-11-08 21:31:40 +08:00
|
|
|
break;
|
2017-11-08 21:24:21 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
|
|
|
|
break;
|
[RISCV] Support and tests for a variety of additional LLVM IR constructs
Previous patches primarily ensured that codegen was possible for the standard
RISC-V instructions. However, there are a number of IR inputs that wouldn't be
appropriately lowered. This patch both adds test cases and supports lowering
for a number of these cases:
* Improved sext/zext/trunc support
* Support for setcc variants that don't map directly to RISC-V instructions
* Lowering mul, and hence support for external symbols
* addc, adde, subc, sube
* mulhs, srem, mulhu, urem, udiv, sdiv
* {srl,sra,shl}_parts
* brind
* br_jt
* bswap, ctlz, cttz, ctpop
* rotl, rotr
* BlockAddress operands
Differential Revision: https://reviews.llvm.org/D29938
llvm-svn: 318737
2017-11-21 16:11:03 +08:00
|
|
|
case MachineOperand::MO_BlockAddress:
|
|
|
|
MCOp = lowerSymbolOperand(
|
|
|
|
MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
MCOp = lowerSymbolOperand(
|
|
|
|
MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
|
|
|
|
break;
|
2018-03-20 21:26:12 +08:00
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
|
|
|
|
break;
|
2017-11-08 21:24:21 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::LowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
|
|
|
|
const AsmPrinter &AP) {
|
2017-10-20 05:37:38 +08:00
|
|
|
OutMI.setOpcode(MI->getOpcode());
|
|
|
|
|
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
MCOperand MCOp;
|
2017-11-08 21:24:21 +08:00
|
|
|
if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
|
|
|
|
OutMI.addOperand(MCOp);
|
2017-10-20 05:37:38 +08:00
|
|
|
}
|
|
|
|
}
|