LLVM  14.0.0git
RISCVMCInstLower.cpp
Go to the documentation of this file.
1 //===-- RISCVMCInstLower.cpp - Convert RISCV MachineInstr to an MCInst ------=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains code to lower RISCV MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "RISCV.h"
15 #include "RISCVSubtarget.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
26 
27 using namespace llvm;
28 
30  const AsmPrinter &AP) {
31  MCContext &Ctx = AP.OutContext;
33 
34  switch (MO.getTargetFlags()) {
35  default:
36  llvm_unreachable("Unknown target flag on GV operand");
37  case RISCVII::MO_None:
39  break;
40  case RISCVII::MO_CALL:
42  break;
43  case RISCVII::MO_PLT:
45  break;
46  case RISCVII::MO_LO:
48  break;
49  case RISCVII::MO_HI:
51  break;
54  break;
57  break;
58  case RISCVII::MO_GOT_HI:
60  break;
63  break;
66  break;
69  break;
72  break;
75  break;
76  }
77 
78  const MCExpr *ME =
80 
81  if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
83  ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
84 
86  ME = RISCVMCExpr::create(ME, Kind, Ctx);
87  return MCOperand::createExpr(ME);
88 }
89 
91  MCOperand &MCOp,
92  const AsmPrinter &AP) {
93  switch (MO.getType()) {
94  default:
95  report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
97  // Ignore all implicit register operands.
98  if (MO.isImplicit())
99  return false;
100  MCOp = MCOperand::createReg(MO.getReg());
101  break;
103  // Regmasks are like implicit defs.
104  return false;
106  MCOp = MCOperand::createImm(MO.getImm());
107  break;
109  MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
110  break;
112  MCOp = lowerSymbolOperand(MO, AP.getSymbolPreferLocal(*MO.getGlobal()), AP);
113  break;
115  MCOp = lowerSymbolOperand(
116  MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
117  break;
119  MCOp = lowerSymbolOperand(
120  MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
121  break;
123  MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
124  break;
126  MCOp = lowerSymbolOperand(MO, AP.GetJTISymbol(MO.getIndex()), AP);
127  break;
128  }
129  return true;
130 }
131 
133  MCInst &OutMI) {
134  const RISCVVPseudosTable::PseudoInfo *RVV =
135  RISCVVPseudosTable::getPseudoInfo(MI->getOpcode());
136  if (!RVV)
137  return false;
138 
139  OutMI.setOpcode(RVV->BaseInstr);
140 
141  const MachineBasicBlock *MBB = MI->getParent();
142  assert(MBB && "MI expected to be in a basic block");
143  const MachineFunction *MF = MBB->getParent();
144  assert(MF && "MBB expected to be in a machine function");
145 
146  const TargetRegisterInfo *TRI =
147  MF->getSubtarget<RISCVSubtarget>().getRegisterInfo();
148  assert(TRI && "TargetRegisterInfo expected");
149 
150  uint64_t TSFlags = MI->getDesc().TSFlags;
151  unsigned NumOps = MI->getNumExplicitOperands();
152 
153  // Skip policy, VL and SEW operands which are the last operands if present.
154  if (RISCVII::hasVecPolicyOp(TSFlags))
155  --NumOps;
156  if (RISCVII::hasVLOp(TSFlags))
157  --NumOps;
158  if (RISCVII::hasSEWOp(TSFlags))
159  --NumOps;
160 
161  for (unsigned OpNo = 0; OpNo != NumOps; ++OpNo) {
162  const MachineOperand &MO = MI->getOperand(OpNo);
163 
164  // Skip merge op. It should be the first operand after the result.
165  if (RISCVII::hasMergeOp(TSFlags) && OpNo == 1) {
166  assert(MI->getNumExplicitDefs() == 1);
167  continue;
168  }
169 
170  MCOperand MCOp;
171  switch (MO.getType()) {
172  default:
173  llvm_unreachable("Unknown operand type");
175  unsigned Reg = MO.getReg();
176 
177  if (RISCV::VRM2RegClass.contains(Reg) ||
178  RISCV::VRM4RegClass.contains(Reg) ||
179  RISCV::VRM8RegClass.contains(Reg)) {
180  Reg = TRI->getSubReg(Reg, RISCV::sub_vrm1_0);
181  assert(Reg && "Subregister does not exist");
182  } else if (RISCV::FPR16RegClass.contains(Reg)) {
183  Reg = TRI->getMatchingSuperReg(Reg, RISCV::sub_16, &RISCV::FPR32RegClass);
184  assert(Reg && "Subregister does not exist");
185  } else if (RISCV::FPR64RegClass.contains(Reg)) {
186  Reg = TRI->getSubReg(Reg, RISCV::sub_32);
187  assert(Reg && "Superregister does not exist");
188  }
189 
190  MCOp = MCOperand::createReg(Reg);
191  break;
192  }
194  MCOp = MCOperand::createImm(MO.getImm());
195  break;
196  }
197  OutMI.addOperand(MCOp);
198  }
199 
200  // Unmasked pseudo instructions need to append dummy mask operand to
201  // V instructions. All V instructions are modeled as the masked version.
202  if (RISCVII::hasDummyMaskOp(TSFlags))
203  OutMI.addOperand(MCOperand::createReg(RISCV::NoRegister));
204 
205  return true;
206 }
207 
209  AsmPrinter &AP) {
211  return false;
212 
213  OutMI.setOpcode(MI->getOpcode());
214 
215  for (const MachineOperand &MO : MI->operands()) {
216  MCOperand MCOp;
217  if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
218  OutMI.addOperand(MCOp);
219  }
220 
221  switch (OutMI.getOpcode()) {
222  case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
223  const Function &F = MI->getParent()->getParent()->getFunction();
224  if (F.hasFnAttribute("patchable-function-entry")) {
225  unsigned Num;
226  if (F.getFnAttribute("patchable-function-entry")
227  .getValueAsString()
228  .getAsInteger(10, Num))
229  return false;
230  AP.emitNops(Num);
231  return true;
232  }
233  break;
234  }
235  case RISCV::PseudoReadVLENB:
236  OutMI.setOpcode(RISCV::CSRRS);
238  RISCVSysReg::lookupSysRegByName("VLENB")->Encoding));
239  OutMI.addOperand(MCOperand::createReg(RISCV::X0));
240  break;
241  case RISCV::PseudoReadVL:
242  OutMI.setOpcode(RISCV::CSRRS);
243  OutMI.addOperand(
245  OutMI.addOperand(MCOperand::createReg(RISCV::X0));
246  break;
247  }
248  return false;
249 }
AsmPrinter.h
llvm::MachineOperand::MO_BlockAddress
@ MO_BlockAddress
Address of a basic block.
Definition: MachineOperand.h:63
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
MachineInstr.h
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:53
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCOperand::createExpr
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:563
llvm::RISCVII::MO_TLS_GOT_HI
@ MO_TLS_GOT_HI
Definition: RISCVBaseInfo.h:163
llvm::MCOperand::createImm
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:141
llvm::RISCVMCExpr::VK_RISCV_TLS_GD_HI
@ VK_RISCV_TLS_GD_HI
Definition: RISCVMCExpr.h:36
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::Function
Definition: Function.h:62
llvm::RISCVMCExpr::VK_RISCV_TPREL_ADD
@ VK_RISCV_TPREL_ADD
Definition: RISCVMCExpr.h:34
llvm::AArch64SysReg::lookupSysRegByName
const SysReg * lookupSysRegByName(StringRef)
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
contains
return AArch64::GPR64RegClass contains(Reg)
llvm::MachineOperand::getBlockAddress
const BlockAddress * getBlockAddress() const
Definition: MachineOperand.h:568
ErrorHandling.h
MachineBasicBlock.h
llvm::RISCVII::MO_GOT_HI
@ MO_GOT_HI
Definition: RISCVBaseInfo.h:159
llvm::RISCVII::MO_None
@ MO_None
Definition: RISCVBaseInfo.h:152
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
llvm::RISCVII::hasSEWOp
static bool hasSEWOp(uint64_t TSFlags)
Definition: RISCVBaseInfo.h:138
llvm::MachineOperand::isJTI
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
Definition: MachineOperand.h:337
llvm::RISCVII::MO_HI
@ MO_HI
Definition: RISCVBaseInfo.h:156
RISCVMCExpr.h
llvm::RISCVMCExpr::VK_RISCV_TPREL_LO
@ VK_RISCV_TPREL_LO
Definition: RISCVMCExpr.h:32
llvm::RISCVVPseudosTable::PseudoInfo
Definition: RISCVInstrInfo.h:190
llvm::RISCVMCExpr::VK_RISCV_HI
@ VK_RISCV_HI
Definition: RISCVMCExpr.h:28
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::RISCVMCExpr::VK_RISCV_TPREL_HI
@ VK_RISCV_TPREL_HI
Definition: RISCVMCExpr.h:33
llvm::RISCVII::MO_LO
@ MO_LO
Definition: RISCVBaseInfo.h:155
llvm::RISCVII::hasVecPolicyOp
static bool hasVecPolicyOp(uint64_t TSFlags)
Definition: RISCVBaseInfo.h:146
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:52
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::RISCVMCExpr::VK_RISCV_TLS_GOT_HI
@ VK_RISCV_TLS_GOT_HI
Definition: RISCVMCExpr.h:35
llvm::MCInst::setOpcode
void setOpcode(unsigned Op)
Definition: MCInst.h:197
llvm::RISCVMCExpr::VariantKind
VariantKind
Definition: RISCVMCExpr.h:25
llvm::RISCVVPseudosTable::PseudoInfo::BaseInstr
uint16_t BaseInstr
Definition: RISCVInstrInfo.h:192
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:60
llvm::MachineOperand::isImplicit
bool isImplicit() const
Definition: MachineOperand.h:380
llvm::RISCVMCExpr::VK_RISCV_LO
@ VK_RISCV_LO
Definition: RISCVMCExpr.h:27
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:62
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:537
llvm::RISCVII::MO_PCREL_LO
@ MO_PCREL_LO
Definition: RISCVBaseInfo.h:157
MCContext.h
llvm::lowerRISCVMachineInstrToMCInst
bool lowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
Definition: RISCVMCInstLower.cpp:208
llvm::RISCVII::MO_PLT
@ MO_PLT
Definition: RISCVBaseInfo.h:154
MCInst.h
llvm::MachineOperand::isMBB
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
Definition: MachineOperand.h:329
llvm::LowerRISCVMachineOperandToMCOperand
bool LowerRISCVMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &MCOp, const AsmPrinter &AP)
Definition: RISCVMCInstLower.cpp:90
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::RISCVII::hasVLOp
static bool hasVLOp(uint64_t TSFlags)
Definition: RISCVBaseInfo.h:142
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MCInst::addOperand
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
llvm::RISCVMCExpr::VK_RISCV_None
@ VK_RISCV_None
Definition: RISCVMCExpr.h:26
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:630
llvm::RISCVMCExpr::create
static const RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: RISCVMCExpr.cpp:30
llvm::RISCVII::MO_TPREL_ADD
@ MO_TPREL_ADD
Definition: RISCVBaseInfo.h:162
llvm::MachineOperand::getTargetFlags
unsigned getTargetFlags() const
Definition: MachineOperand.h:221
llvm::AsmPrinter::GetCPISymbol
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
Definition: AsmPrinter.cpp:3125
llvm::RISCVMCExpr::VK_RISCV_PCREL_HI
@ VK_RISCV_PCREL_HI
Definition: RISCVMCExpr.h:30
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
uint64_t
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3116
llvm::TargetRegisterInfo::getMatchingSuperReg
MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx, const TargetRegisterClass *RC) const
Return a super-register of the specified register Reg so its sub-register of index SubIdx is Reg.
Definition: TargetRegisterInfo.h:577
llvm::AsmPrinter::getSymbolPreferLocal
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
Definition: AsmPrinter.cpp:490
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:219
llvm::RISCVMCExpr::VK_RISCV_PCREL_LO
@ VK_RISCV_PCREL_LO
Definition: RISCVMCExpr.h:29
llvm::RISCVII::MO_TPREL_LO
@ MO_TPREL_LO
Definition: RISCVBaseInfo.h:160
llvm::RISCVSubtarget
Definition: RISCVSubtarget.h:35
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
llvm::RISCVMCExpr::VK_RISCV_GOT_HI
@ VK_RISCV_GOT_HI
Definition: RISCVMCExpr.h:31
llvm::AsmPrinter::OutContext
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:92
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:360
RISCV.h
llvm::RISCVII::hasMergeOp
static bool hasMergeOp(uint64_t TSFlags)
Definition: RISCVBaseInfo.h:134
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::RISCVII::MO_PCREL_HI
@ MO_PCREL_HI
Definition: RISCVBaseInfo.h:158
llvm::MachineOperand::MO_JumpTableIndex
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
Definition: MachineOperand.h:60
llvm::MCBinaryExpr::createAdd
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:524
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3172
llvm::MachineOperand::getMBB
MachineBasicBlock * getMBB() const
Definition: MachineOperand.h:552
MCAsmInfo.h
llvm::RISCVII::MO_TPREL_HI
@ MO_TPREL_HI
Definition: RISCVBaseInfo.h:161
llvm::AsmPrinter::GetJTISymbol
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Definition: AsmPrinter.cpp:3153
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::MCOperand::createReg
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:134
llvm::MachineOperand::MO_MachineBasicBlock
@ MO_MachineBasicBlock
MachineBasicBlock reference.
Definition: MachineOperand.h:56
lowerSymbolOperand
static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym, const AsmPrinter &AP)
Definition: RISCVMCInstLower.cpp:29
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::AsmPrinter::emitNops
void emitNops(unsigned N)
Emit N NOP instructions.
Definition: AsmPrinter.cpp:3102
llvm::MachineOperand::MO_ExternalSymbol
@ MO_ExternalSymbol
Name of external global symbol.
Definition: MachineOperand.h:61
llvm::MachineOperand::getIndex
int getIndex() const
Definition: MachineOperand.h:557
llvm::RISCVII::MO_CALL
@ MO_CALL
Definition: RISCVBaseInfo.h:153
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:198
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:385
lowerRISCVVMachineInstrToMCInst
static bool lowerRISCVVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI)
Definition: RISCVMCInstLower.cpp:132
RISCVSubtarget.h
llvm::RISCVMCExpr::VK_RISCV_CALL
@ VK_RISCV_CALL
Definition: RISCVMCExpr.h:37
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:608
llvm::TargetRegisterInfo::getSubReg
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
Definition: TargetRegisterInfo.h:1094
llvm::RISCVII::MO_TLS_GD_HI
@ MO_TLS_GD_HI
Definition: RISCVBaseInfo.h:164
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::RISCVII::hasDummyMaskOp
static bool hasDummyMaskOp(uint64_t TSFlags)
Definition: RISCVBaseInfo.h:126
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::MachineOperand::MO_RegisterMask
@ MO_RegisterMask
Mask of preserved registers.
Definition: MachineOperand.h:64
llvm::RISCVMCExpr::VK_RISCV_CALL_PLT
@ VK_RISCV_CALL_PLT
Definition: RISCVMCExpr.h:38
raw_ostream.h
MCExpr.h
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MachineOperand::MO_ConstantPoolIndex
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
Definition: MachineOperand.h:58