PPCMCInstLower.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //===-- PPCMCInstLower.cpp - Convert PPC 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 PPC MachineInstrs to their corresponding
  10. // MCInst records.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "MCTargetDesc/PPCMCExpr.h"
  14. #include "PPC.h"
  15. #include "PPCSubtarget.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/CodeGen/AsmPrinter.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineModuleInfoImpls.h"
  21. #include "llvm/CodeGen/TargetLowering.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/GlobalValue.h"
  24. #include "llvm/IR/Mangler.h"
  25. #include "llvm/MC/MCAsmInfo.h"
  26. #include "llvm/MC/MCExpr.h"
  27. #include "llvm/MC/MCInst.h"
  28. #include "llvm/Target/TargetLoweringObjectFile.h"
  29. using namespace llvm;
  30. static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO,
  31. AsmPrinter &AP) {
  32. const TargetMachine &TM = AP.TM;
  33. Mangler &Mang = TM.getObjFileLowering()->getMangler();
  34. const DataLayout &DL = AP.getDataLayout();
  35. MCContext &Ctx = AP.OutContext;
  36. SmallString<128> Name;
  37. if (!MO.isGlobal()) {
  38. assert(MO.isSymbol() && "Isn't a symbol reference");
  39. Mangler::getNameWithPrefix(Name, MO.getSymbolName(), DL);
  40. } else {
  41. const GlobalValue *GV = MO.getGlobal();
  42. TM.getNameWithPrefix(Name, GV, Mang);
  43. }
  44. MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
  45. return Sym;
  46. }
  47. static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
  48. AsmPrinter &Printer) {
  49. MCContext &Ctx = Printer.OutContext;
  50. MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
  51. unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
  52. switch (access) {
  53. case PPCII::MO_TPREL_LO:
  54. RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
  55. break;
  56. case PPCII::MO_TPREL_HA:
  57. RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
  58. break;
  59. case PPCII::MO_DTPREL_LO:
  60. RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
  61. break;
  62. case PPCII::MO_TLSLD_LO:
  63. RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
  64. break;
  65. case PPCII::MO_TOC_LO:
  66. RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
  67. break;
  68. case PPCII::MO_TLS:
  69. bool IsPCRel = (MO.getTargetFlags() & ~access) == PPCII::MO_PCREL_FLAG;
  70. RefKind = IsPCRel ? MCSymbolRefExpr::VK_PPC_TLS_PCREL
  71. : MCSymbolRefExpr::VK_PPC_TLS;
  72. break;
  73. }
  74. if (MO.getTargetFlags() == PPCII::MO_PLT)
  75. RefKind = MCSymbolRefExpr::VK_PLT;
  76. else if (MO.getTargetFlags() == PPCII::MO_PCREL_FLAG)
  77. RefKind = MCSymbolRefExpr::VK_PCREL;
  78. else if (MO.getTargetFlags() == (PPCII::MO_PCREL_FLAG | PPCII::MO_GOT_FLAG))
  79. RefKind = MCSymbolRefExpr::VK_PPC_GOT_PCREL;
  80. else if (MO.getTargetFlags() == (PPCII::MO_PCREL_FLAG | PPCII::MO_TPREL_FLAG))
  81. RefKind = MCSymbolRefExpr::VK_TPREL;
  82. else if (MO.getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG)
  83. RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSGD_PCREL;
  84. else if (MO.getTargetFlags() == PPCII::MO_GOT_TLSLD_PCREL_FLAG)
  85. RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_PCREL;
  86. else if (MO.getTargetFlags() == PPCII::MO_GOT_TPREL_PCREL_FLAG)
  87. RefKind = MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL;
  88. const MachineInstr *MI = MO.getParent();
  89. const MachineFunction *MF = MI->getMF();
  90. const Module *M = MF->getFunction().getParent();
  91. const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
  92. const TargetMachine &TM = Printer.TM;
  93. unsigned MIOpcode = MI->getOpcode();
  94. assert((Subtarget->isUsingPCRelativeCalls() || MIOpcode != PPC::BL8_NOTOC) &&
  95. "BL8_NOTOC is only valid when using PC Relative Calls.");
  96. if (Subtarget->isUsingPCRelativeCalls()) {
  97. if (MIOpcode == PPC::TAILB || MIOpcode == PPC::TAILB8 ||
  98. MIOpcode == PPC::TCRETURNdi || MIOpcode == PPC::TCRETURNdi8 ||
  99. MIOpcode == PPC::BL8_NOTOC) {
  100. RefKind = MCSymbolRefExpr::VK_PPC_NOTOC;
  101. }
  102. if (MO.getTargetFlags() == PPCII::MO_PCREL_OPT_FLAG)
  103. RefKind = MCSymbolRefExpr::VK_PPC_PCREL_OPT;
  104. }
  105. const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
  106. // If -msecure-plt -fPIC, add 32768 to symbol.
  107. if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
  108. M->getPICLevel() == PICLevel::BigPIC &&
  109. MO.getTargetFlags() == PPCII::MO_PLT)
  110. Expr =
  111. MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
  112. if (!MO.isJTI() && MO.getOffset())
  113. Expr = MCBinaryExpr::createAdd(Expr,
  114. MCConstantExpr::create(MO.getOffset(), Ctx),
  115. Ctx);
  116. // Subtract off the PIC base if required.
  117. if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
  118. const MachineFunction *MF = MO.getParent()->getParent()->getParent();
  119. const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
  120. Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
  121. }
  122. // Add ha16() / lo16() markers if required.
  123. switch (access) {
  124. case PPCII::MO_LO:
  125. Expr = PPCMCExpr::createLo(Expr, Ctx);
  126. break;
  127. case PPCII::MO_HA:
  128. Expr = PPCMCExpr::createHa(Expr, Ctx);
  129. break;
  130. }
  131. return MCOperand::createExpr(Expr);
  132. }
  133. void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
  134. AsmPrinter &AP) {
  135. OutMI.setOpcode(MI->getOpcode());
  136. for (const MachineOperand &MO : MI->operands()) {
  137. MCOperand MCOp;
  138. if (LowerPPCMachineOperandToMCOperand(MO, MCOp, AP))
  139. OutMI.addOperand(MCOp);
  140. }
  141. }
  142. bool llvm::LowerPPCMachineOperandToMCOperand(const MachineOperand &MO,
  143. MCOperand &OutMO, AsmPrinter &AP) {
  144. switch (MO.getType()) {
  145. default:
  146. llvm_unreachable("unknown operand type");
  147. case MachineOperand::MO_Register:
  148. assert(!MO.getSubReg() && "Subregs should be eliminated!");
  149. assert(MO.getReg() > PPC::NoRegister &&
  150. MO.getReg() < PPC::NUM_TARGET_REGS &&
  151. "Invalid register for this target!");
  152. // Ignore all implicit register operands.
  153. if (MO.isImplicit())
  154. return false;
  155. OutMO = MCOperand::createReg(MO.getReg());
  156. return true;
  157. case MachineOperand::MO_Immediate:
  158. OutMO = MCOperand::createImm(MO.getImm());
  159. return true;
  160. case MachineOperand::MO_MachineBasicBlock:
  161. OutMO = MCOperand::createExpr(
  162. MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), AP.OutContext));
  163. return true;
  164. case MachineOperand::MO_GlobalAddress:
  165. case MachineOperand::MO_ExternalSymbol:
  166. OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP);
  167. return true;
  168. case MachineOperand::MO_JumpTableIndex:
  169. OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
  170. return true;
  171. case MachineOperand::MO_ConstantPoolIndex:
  172. OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
  173. return true;
  174. case MachineOperand::MO_BlockAddress:
  175. OutMO =
  176. GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
  177. return true;
  178. case MachineOperand::MO_MCSymbol:
  179. OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP);
  180. return true;
  181. case MachineOperand::MO_RegisterMask:
  182. return false;
  183. }
  184. }