RISCVMCInstLower.cpp 8.2 KB

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