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"
|
2020-12-01 11:48:24 +08:00
|
|
|
#include "RISCVSubtarget.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;
|
2020-11-25 22:01:19 +08:00
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
MCOp = lowerSymbolOperand(MO, AP.GetJTISymbol(MO.getIndex()), AP);
|
|
|
|
break;
|
2017-11-08 21:24:21 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:48:24 +08:00
|
|
|
static bool lowerRISCVVMachineInstrToMCInst(const MachineInstr *MI,
|
|
|
|
MCInst &OutMI) {
|
|
|
|
const RISCVVPseudosTable::PseudoInfo *RVV =
|
|
|
|
RISCVVPseudosTable::getPseudoInfo(MI->getOpcode());
|
|
|
|
if (!RVV)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
OutMI.setOpcode(RVV->BaseInstr);
|
|
|
|
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
assert(MBB && "MI expected to be in a basic block");
|
|
|
|
const MachineFunction *MF = MBB->getParent();
|
|
|
|
assert(MF && "MBB expected to be in a machine function");
|
|
|
|
|
|
|
|
const TargetRegisterInfo *TRI =
|
|
|
|
MF->getSubtarget<RISCVSubtarget>().getRegisterInfo();
|
|
|
|
assert(TRI && "TargetRegisterInfo expected");
|
|
|
|
|
2021-01-11 10:01:23 +08:00
|
|
|
uint64_t TSFlags = MI->getDesc().TSFlags;
|
|
|
|
int NumOps = MI->getNumExplicitOperands();
|
|
|
|
|
2020-12-01 11:48:24 +08:00
|
|
|
for (const MachineOperand &MO : MI->explicit_operands()) {
|
|
|
|
int OpNo = (int)MI->getOperandNo(&MO);
|
|
|
|
assert(OpNo >= 0 && "Operand number doesn't fit in an 'int' type");
|
|
|
|
|
2021-01-11 10:01:23 +08:00
|
|
|
// Skip VL and SEW operands which are the last two operands if present.
|
|
|
|
if ((TSFlags & RISCVII::HasVLOpMask) && OpNo == (NumOps - 2))
|
|
|
|
continue;
|
|
|
|
if ((TSFlags & RISCVII::HasSEWOpMask) && OpNo == (NumOps - 1))
|
2020-12-01 11:48:24 +08:00
|
|
|
continue;
|
|
|
|
|
2021-01-11 10:01:23 +08:00
|
|
|
// Skip merge op. It should be the first operand after the result.
|
|
|
|
if ((TSFlags & RISCVII::HasMergeOpMask) && OpNo == 1) {
|
|
|
|
assert(MI->getNumExplicitDefs() == 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:48:24 +08:00
|
|
|
MCOperand MCOp;
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown operand type");
|
|
|
|
case MachineOperand::MO_Register: {
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
|
2020-12-11 05:37:42 +08:00
|
|
|
if (RISCV::VRM2RegClass.contains(Reg) ||
|
|
|
|
RISCV::VRM4RegClass.contains(Reg) ||
|
|
|
|
RISCV::VRM8RegClass.contains(Reg)) {
|
[RISCV] Implement vlseg intrinsics.
For Zvlsseg, we need continuous vector registers for the values. We need
to define new register classes for the different combinations of (number
of fields and LMUL). For example,
when the number of fields(NF) = 3, LMUL = 2, the values will be assigned
to (V0M2, V2M2, V4M2), (V2M2, V4M2, V6M2), (V4M2, V6M2, V8M2), ...
We define the vlseg intrinsics with multiple outputs. There is no way to
describe the codegen patterns with multiple outputs in the tablegen
files. We do the codegen in RISCVISelDAGToDAG and use EXTRACT_SUBREG to
extract the values of output.
The multiple scalable vector values will be put into a struct. This
patch is depended on the support for scalable vector struct.
Differential Revision: https://reviews.llvm.org/D94229
2020-12-31 17:14:15 +08:00
|
|
|
Reg = TRI->getSubReg(Reg, RISCV::sub_vrm1_0);
|
2020-12-01 11:48:24 +08:00
|
|
|
assert(Reg && "Subregister does not exist");
|
2021-01-22 22:38:11 +08:00
|
|
|
} else if (RISCV::FPR16RegClass.contains(Reg)) {
|
|
|
|
Reg = TRI->getMatchingSuperReg(Reg, RISCV::sub_16, &RISCV::FPR32RegClass);
|
|
|
|
assert(Reg && "Subregister does not exist");
|
|
|
|
} else if (RISCV::FPR64RegClass.contains(Reg)) {
|
|
|
|
Reg = TRI->getSubReg(Reg, RISCV::sub_32);
|
|
|
|
assert(Reg && "Superregister does not exist");
|
2020-12-01 11:48:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MCOp = MCOperand::createReg(Reg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
MCOp = MCOperand::createImm(MO.getImm());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
OutMI.addOperand(MCOp);
|
|
|
|
}
|
2020-12-10 17:06:22 +08:00
|
|
|
|
2020-12-11 15:16:08 +08:00
|
|
|
// Unmasked pseudo instructions need to append dummy mask operand to
|
|
|
|
// V instructions. All V instructions are modeled as the masked version.
|
2021-01-11 10:01:23 +08:00
|
|
|
if (TSFlags & RISCVII::HasDummyMaskOpMask)
|
2020-12-10 17:06:22 +08:00
|
|
|
OutMI.addOperand(MCOperand::createReg(RISCV::NoRegister));
|
|
|
|
|
2020-12-01 11:48:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-08 21:24:21 +08:00
|
|
|
void llvm::LowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
|
|
|
|
const AsmPrinter &AP) {
|
2020-12-01 11:48:24 +08:00
|
|
|
if (lowerRISCVVMachineInstrToMCInst(MI, OutMI))
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
}
|
2021-01-14 09:14:45 +08:00
|
|
|
|
|
|
|
if (OutMI.getOpcode() == RISCV::PseudoReadVLENB) {
|
|
|
|
OutMI.setOpcode(RISCV::CSRRS);
|
|
|
|
OutMI.addOperand(MCOperand::createImm(
|
|
|
|
RISCVSysReg::lookupSysRegByName("VLENB")->Encoding));
|
|
|
|
OutMI.addOperand(MCOperand::createReg(RISCV::X0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-22 09:08:41 +08:00
|
|
|
if (OutMI.getOpcode() == RISCV::PseudoReadVL) {
|
|
|
|
OutMI.setOpcode(RISCV::CSRRS);
|
|
|
|
OutMI.addOperand(MCOperand::createImm(
|
|
|
|
RISCVSysReg::lookupSysRegByName("VL")->Encoding));
|
|
|
|
OutMI.addOperand(MCOperand::createReg(RISCV::X0));
|
|
|
|
return;
|
|
|
|
}
|
2017-10-20 05:37:38 +08:00
|
|
|
}
|