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"
16 #include "MCTargetDesc/RISCVMCExpr.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineInstr.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"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 using namespace llvm;
28 
lowerSymbolOperand(const MachineOperand & MO,MCSymbol * Sym,const AsmPrinter & AP)29 static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
30                                     const AsmPrinter &AP) {
31   MCContext &Ctx = AP.OutContext;
32   RISCVMCExpr::VariantKind Kind;
33 
34   switch (MO.getTargetFlags()) {
35   default:
36     llvm_unreachable("Unknown target flag on GV operand");
37   case RISCVII::MO_None:
38     Kind = RISCVMCExpr::VK_RISCV_None;
39     break;
40   case RISCVII::MO_CALL:
41     Kind = RISCVMCExpr::VK_RISCV_CALL;
42     break;
43   case RISCVII::MO_PLT:
44     Kind = RISCVMCExpr::VK_RISCV_CALL_PLT;
45     break;
46   case RISCVII::MO_LO:
47     Kind = RISCVMCExpr::VK_RISCV_LO;
48     break;
49   case RISCVII::MO_HI:
50     Kind = RISCVMCExpr::VK_RISCV_HI;
51     break;
52   case RISCVII::MO_PCREL_LO:
53     Kind = RISCVMCExpr::VK_RISCV_PCREL_LO;
54     break;
55   case RISCVII::MO_PCREL_HI:
56     Kind = RISCVMCExpr::VK_RISCV_PCREL_HI;
57     break;
58   case RISCVII::MO_GOT_HI:
59     Kind = RISCVMCExpr::VK_RISCV_GOT_HI;
60     break;
61   case RISCVII::MO_TPREL_LO:
62     Kind = RISCVMCExpr::VK_RISCV_TPREL_LO;
63     break;
64   case RISCVII::MO_TPREL_HI:
65     Kind = RISCVMCExpr::VK_RISCV_TPREL_HI;
66     break;
67   case RISCVII::MO_TPREL_ADD:
68     Kind = RISCVMCExpr::VK_RISCV_TPREL_ADD;
69     break;
70   case RISCVII::MO_TLS_GOT_HI:
71     Kind = RISCVMCExpr::VK_RISCV_TLS_GOT_HI;
72     break;
73   case RISCVII::MO_TLS_GD_HI:
74     Kind = RISCVMCExpr::VK_RISCV_TLS_GD_HI;
75     break;
76   }
77 
78   const MCExpr *ME =
79       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
80 
81   if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
82     ME = MCBinaryExpr::createAdd(
83         ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
84 
85   if (Kind != RISCVMCExpr::VK_RISCV_None)
86     ME = RISCVMCExpr::create(ME, Kind, Ctx);
87   return MCOperand::createExpr(ME);
88 }
89 
LowerRISCVMachineOperandToMCOperand(const MachineOperand & MO,MCOperand & MCOp,const AsmPrinter & AP)90 bool llvm::LowerRISCVMachineOperandToMCOperand(const MachineOperand &MO,
91                                                MCOperand &MCOp,
92                                                const AsmPrinter &AP) {
93   switch (MO.getType()) {
94   default:
95     report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
96   case MachineOperand::MO_Register:
97     // Ignore all implicit register operands.
98     if (MO.isImplicit())
99       return false;
100     MCOp = MCOperand::createReg(MO.getReg());
101     break;
102   case MachineOperand::MO_RegisterMask:
103     // Regmasks are like implicit defs.
104     return false;
105   case MachineOperand::MO_Immediate:
106     MCOp = MCOperand::createImm(MO.getImm());
107     break;
108   case MachineOperand::MO_MachineBasicBlock:
109     MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
110     break;
111   case MachineOperand::MO_GlobalAddress:
112     MCOp = lowerSymbolOperand(MO, AP.getSymbolPreferLocal(*MO.getGlobal()), AP);
113     break;
114   case MachineOperand::MO_BlockAddress:
115     MCOp = lowerSymbolOperand(
116         MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
117     break;
118   case MachineOperand::MO_ExternalSymbol:
119     MCOp = lowerSymbolOperand(
120         MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
121     break;
122   case MachineOperand::MO_ConstantPoolIndex:
123     MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
124     break;
125   case MachineOperand::MO_JumpTableIndex:
126     MCOp = lowerSymbolOperand(MO, AP.GetJTISymbol(MO.getIndex()), AP);
127     break;
128   }
129   return true;
130 }
131 
lowerRISCVVMachineInstrToMCInst(const MachineInstr * MI,MCInst & OutMI)132 static bool lowerRISCVVMachineInstrToMCInst(const MachineInstr *MI,
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   int NumOps = MI->getNumExplicitOperands();
152 
153   for (const MachineOperand &MO : MI->explicit_operands()) {
154     int OpNo = (int)MI->getOperandNo(&MO);
155     assert(OpNo >= 0 && "Operand number doesn't fit in an 'int' type");
156 
157     // Skip VL and SEW operands which are the last two operands if present.
158     if (RISCVII::hasVLOp(TSFlags) && OpNo == (NumOps - 2))
159       continue;
160     if (RISCVII::hasSEWOp(TSFlags) && OpNo == (NumOps - 1))
161       continue;
162 
163     // Skip merge op. It should be the first operand after the result.
164     if (RISCVII::hasMergeOp(TSFlags) && OpNo == 1) {
165       assert(MI->getNumExplicitDefs() == 1);
166       continue;
167     }
168 
169     MCOperand MCOp;
170     switch (MO.getType()) {
171     default:
172       llvm_unreachable("Unknown operand type");
173     case MachineOperand::MO_Register: {
174       unsigned Reg = MO.getReg();
175 
176       if (RISCV::VRM2RegClass.contains(Reg) ||
177           RISCV::VRM4RegClass.contains(Reg) ||
178           RISCV::VRM8RegClass.contains(Reg)) {
179         Reg = TRI->getSubReg(Reg, RISCV::sub_vrm1_0);
180         assert(Reg && "Subregister does not exist");
181       } else if (RISCV::FPR16RegClass.contains(Reg)) {
182         Reg = TRI->getMatchingSuperReg(Reg, RISCV::sub_16, &RISCV::FPR32RegClass);
183         assert(Reg && "Subregister does not exist");
184       } else if (RISCV::FPR64RegClass.contains(Reg)) {
185         Reg = TRI->getSubReg(Reg, RISCV::sub_32);
186         assert(Reg && "Superregister does not exist");
187       }
188 
189       MCOp = MCOperand::createReg(Reg);
190       break;
191     }
192     case MachineOperand::MO_Immediate:
193       MCOp = MCOperand::createImm(MO.getImm());
194       break;
195     }
196     OutMI.addOperand(MCOp);
197   }
198 
199   // Unmasked pseudo instructions need to append dummy mask operand to
200   // V instructions. All V instructions are modeled as the masked version.
201   if (RISCVII::hasDummyMaskOp(TSFlags))
202     OutMI.addOperand(MCOperand::createReg(RISCV::NoRegister));
203 
204   return true;
205 }
206 
lowerRISCVMachineInstrToMCInst(const MachineInstr * MI,MCInst & OutMI,AsmPrinter & AP)207 bool llvm::lowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
208                                           AsmPrinter &AP) {
209   if (lowerRISCVVMachineInstrToMCInst(MI, OutMI))
210     return false;
211 
212   OutMI.setOpcode(MI->getOpcode());
213 
214   for (const MachineOperand &MO : MI->operands()) {
215     MCOperand MCOp;
216     if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
217       OutMI.addOperand(MCOp);
218   }
219 
220   switch (OutMI.getOpcode()) {
221   case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
222     const Function &F = MI->getParent()->getParent()->getFunction();
223     if (F.hasFnAttribute("patchable-function-entry")) {
224       unsigned Num;
225       if (F.getFnAttribute("patchable-function-entry")
226               .getValueAsString()
227               .getAsInteger(10, Num))
228         return false;
229       AP.emitNops(Num);
230       return true;
231     }
232     break;
233   }
234   case RISCV::PseudoReadVLENB:
235     OutMI.setOpcode(RISCV::CSRRS);
236     OutMI.addOperand(MCOperand::createImm(
237         RISCVSysReg::lookupSysRegByName("VLENB")->Encoding));
238     OutMI.addOperand(MCOperand::createReg(RISCV::X0));
239     break;
240   case RISCV::PseudoReadVL:
241     OutMI.setOpcode(RISCV::CSRRS);
242     OutMI.addOperand(
243         MCOperand::createImm(RISCVSysReg::lookupSysRegByName("VL")->Encoding));
244     OutMI.addOperand(MCOperand::createReg(RISCV::X0));
245     break;
246   }
247   return false;
248 }
249