LoongArchExpandPseudoInsts.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //===-- LoongArchExpandPseudoInsts.cpp - Expand pseudo instructions -------===//
  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 a pass that expands pseudo instructions into target
  10. // instructions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "LoongArch.h"
  14. #include "LoongArchInstrInfo.h"
  15. #include "LoongArchTargetMachine.h"
  16. #include "MCTargetDesc/LoongArchBaseInfo.h"
  17. #include "MCTargetDesc/LoongArchMCTargetDesc.h"
  18. #include "llvm/CodeGen/LivePhysRegs.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/Support/CodeGen.h"
  23. using namespace llvm;
  24. #define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME \
  25. "LoongArch Pre-RA pseudo instruction expansion pass"
  26. namespace {
  27. class LoongArchPreRAExpandPseudo : public MachineFunctionPass {
  28. public:
  29. const LoongArchInstrInfo *TII;
  30. static char ID;
  31. LoongArchPreRAExpandPseudo() : MachineFunctionPass(ID) {
  32. initializeLoongArchPreRAExpandPseudoPass(*PassRegistry::getPassRegistry());
  33. }
  34. bool runOnMachineFunction(MachineFunction &MF) override;
  35. void getAnalysisUsage(AnalysisUsage &AU) const override {
  36. AU.setPreservesCFG();
  37. MachineFunctionPass::getAnalysisUsage(AU);
  38. }
  39. StringRef getPassName() const override {
  40. return LOONGARCH_PRERA_EXPAND_PSEUDO_NAME;
  41. }
  42. private:
  43. bool expandMBB(MachineBasicBlock &MBB);
  44. bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  45. MachineBasicBlock::iterator &NextMBBI);
  46. bool expandPcalau12iInstPair(MachineBasicBlock &MBB,
  47. MachineBasicBlock::iterator MBBI,
  48. MachineBasicBlock::iterator &NextMBBI,
  49. unsigned FlagsHi, unsigned SecondOpcode,
  50. unsigned FlagsLo);
  51. bool expandLoadAddressPcrel(MachineBasicBlock &MBB,
  52. MachineBasicBlock::iterator MBBI,
  53. MachineBasicBlock::iterator &NextMBBI);
  54. bool expandLoadAddressGot(MachineBasicBlock &MBB,
  55. MachineBasicBlock::iterator MBBI,
  56. MachineBasicBlock::iterator &NextMBBI);
  57. bool expandLoadAddressTLSLE(MachineBasicBlock &MBB,
  58. MachineBasicBlock::iterator MBBI,
  59. MachineBasicBlock::iterator &NextMBBI);
  60. bool expandLoadAddressTLSIE(MachineBasicBlock &MBB,
  61. MachineBasicBlock::iterator MBBI,
  62. MachineBasicBlock::iterator &NextMBBI);
  63. bool expandLoadAddressTLSLD(MachineBasicBlock &MBB,
  64. MachineBasicBlock::iterator MBBI,
  65. MachineBasicBlock::iterator &NextMBBI);
  66. bool expandLoadAddressTLSGD(MachineBasicBlock &MBB,
  67. MachineBasicBlock::iterator MBBI,
  68. MachineBasicBlock::iterator &NextMBBI);
  69. bool expandFunctionCALL(MachineBasicBlock &MBB,
  70. MachineBasicBlock::iterator MBBI,
  71. MachineBasicBlock::iterator &NextMBBI,
  72. bool IsTailCall);
  73. };
  74. char LoongArchPreRAExpandPseudo::ID = 0;
  75. bool LoongArchPreRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
  76. TII =
  77. static_cast<const LoongArchInstrInfo *>(MF.getSubtarget().getInstrInfo());
  78. bool Modified = false;
  79. for (auto &MBB : MF)
  80. Modified |= expandMBB(MBB);
  81. return Modified;
  82. }
  83. bool LoongArchPreRAExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
  84. bool Modified = false;
  85. MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
  86. while (MBBI != E) {
  87. MachineBasicBlock::iterator NMBBI = std::next(MBBI);
  88. Modified |= expandMI(MBB, MBBI, NMBBI);
  89. MBBI = NMBBI;
  90. }
  91. return Modified;
  92. }
  93. bool LoongArchPreRAExpandPseudo::expandMI(
  94. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  95. MachineBasicBlock::iterator &NextMBBI) {
  96. switch (MBBI->getOpcode()) {
  97. case LoongArch::PseudoLA_PCREL:
  98. return expandLoadAddressPcrel(MBB, MBBI, NextMBBI);
  99. case LoongArch::PseudoLA_GOT:
  100. return expandLoadAddressGot(MBB, MBBI, NextMBBI);
  101. case LoongArch::PseudoLA_TLS_LE:
  102. return expandLoadAddressTLSLE(MBB, MBBI, NextMBBI);
  103. case LoongArch::PseudoLA_TLS_IE:
  104. return expandLoadAddressTLSIE(MBB, MBBI, NextMBBI);
  105. case LoongArch::PseudoLA_TLS_LD:
  106. return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI);
  107. case LoongArch::PseudoLA_TLS_GD:
  108. return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI);
  109. case LoongArch::PseudoCALL:
  110. return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/false);
  111. case LoongArch::PseudoTAIL:
  112. return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/true);
  113. }
  114. return false;
  115. }
  116. bool LoongArchPreRAExpandPseudo::expandPcalau12iInstPair(
  117. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  118. MachineBasicBlock::iterator &NextMBBI, unsigned FlagsHi,
  119. unsigned SecondOpcode, unsigned FlagsLo) {
  120. MachineFunction *MF = MBB.getParent();
  121. MachineInstr &MI = *MBBI;
  122. DebugLoc DL = MI.getDebugLoc();
  123. Register DestReg = MI.getOperand(0).getReg();
  124. Register ScratchReg =
  125. MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
  126. MachineOperand &Symbol = MI.getOperand(1);
  127. BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCALAU12I), ScratchReg)
  128. .addDisp(Symbol, 0, FlagsHi);
  129. MachineInstr *SecondMI =
  130. BuildMI(MBB, MBBI, DL, TII->get(SecondOpcode), DestReg)
  131. .addReg(ScratchReg)
  132. .addDisp(Symbol, 0, FlagsLo);
  133. if (MI.hasOneMemOperand())
  134. SecondMI->addMemOperand(*MF, *MI.memoperands_begin());
  135. MI.eraseFromParent();
  136. return true;
  137. }
  138. bool LoongArchPreRAExpandPseudo::expandLoadAddressPcrel(
  139. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  140. MachineBasicBlock::iterator &NextMBBI) {
  141. // Code Sequence:
  142. // pcalau12i $rd, %pc_hi20(sym)
  143. // addi.w/d $rd, $rd, %pc_lo12(sym)
  144. MachineFunction *MF = MBB.getParent();
  145. const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
  146. unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
  147. return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_PCREL_HI,
  148. SecondOpcode, LoongArchII::MO_PCREL_LO);
  149. }
  150. bool LoongArchPreRAExpandPseudo::expandLoadAddressGot(
  151. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  152. MachineBasicBlock::iterator &NextMBBI) {
  153. // Code Sequence:
  154. // pcalau12i $rd, %got_pc_hi20(sym)
  155. // ld.w/d $rd, $rd, %got_pc_lo12(sym)
  156. MachineFunction *MF = MBB.getParent();
  157. const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
  158. unsigned SecondOpcode = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
  159. return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_GOT_PC_HI,
  160. SecondOpcode, LoongArchII::MO_GOT_PC_LO);
  161. }
  162. bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSLE(
  163. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  164. MachineBasicBlock::iterator &NextMBBI) {
  165. // Code Sequence:
  166. // lu12i.w $rd, %le_hi20(sym)
  167. // ori $rd, $rd, %le_lo12(sym)
  168. MachineFunction *MF = MBB.getParent();
  169. MachineInstr &MI = *MBBI;
  170. DebugLoc DL = MI.getDebugLoc();
  171. Register DestReg = MI.getOperand(0).getReg();
  172. Register ScratchReg =
  173. MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
  174. MachineOperand &Symbol = MI.getOperand(1);
  175. BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU12I_W), ScratchReg)
  176. .addDisp(Symbol, 0, LoongArchII::MO_LE_HI);
  177. BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ORI), DestReg)
  178. .addReg(ScratchReg)
  179. .addDisp(Symbol, 0, LoongArchII::MO_LE_LO);
  180. MI.eraseFromParent();
  181. return true;
  182. }
  183. bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSIE(
  184. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  185. MachineBasicBlock::iterator &NextMBBI) {
  186. // Code Sequence:
  187. // pcalau12i $rd, %ie_pc_hi20(sym)
  188. // ld.w/d $rd, $rd, %ie_pc_lo12(sym)
  189. MachineFunction *MF = MBB.getParent();
  190. const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
  191. unsigned SecondOpcode = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
  192. return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_IE_PC_HI,
  193. SecondOpcode, LoongArchII::MO_IE_PC_LO);
  194. }
  195. bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSLD(
  196. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  197. MachineBasicBlock::iterator &NextMBBI) {
  198. // Code Sequence:
  199. // pcalau12i $rd, %ld_pc_hi20(sym)
  200. // addi.w/d $rd, $rd, %got_pc_lo12(sym)
  201. MachineFunction *MF = MBB.getParent();
  202. const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
  203. unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
  204. return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_LD_PC_HI,
  205. SecondOpcode, LoongArchII::MO_GOT_PC_LO);
  206. }
  207. bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSGD(
  208. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  209. MachineBasicBlock::iterator &NextMBBI) {
  210. // Code Sequence:
  211. // pcalau12i $rd, %gd_pc_hi20(sym)
  212. // addi.w/d $rd, $rd, %got_pc_lo12(sym)
  213. MachineFunction *MF = MBB.getParent();
  214. const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
  215. unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
  216. return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_GD_PC_HI,
  217. SecondOpcode, LoongArchII::MO_GOT_PC_LO);
  218. }
  219. bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
  220. MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
  221. MachineBasicBlock::iterator &NextMBBI, bool IsTailCall) {
  222. MachineFunction *MF = MBB.getParent();
  223. MachineInstr &MI = *MBBI;
  224. DebugLoc DL = MI.getDebugLoc();
  225. const MachineOperand &Func = MI.getOperand(0);
  226. MachineInstrBuilder CALL;
  227. unsigned Opcode;
  228. switch (MF->getTarget().getCodeModel()) {
  229. default:
  230. report_fatal_error("Unsupported code model");
  231. break;
  232. case CodeModel::Small: {
  233. // CALL:
  234. // bl func
  235. // TAIL:
  236. // b func
  237. Opcode = IsTailCall ? LoongArch::PseudoB_TAIL : LoongArch::BL;
  238. CALL = BuildMI(MBB, MBBI, DL, TII->get(Opcode)).add(Func);
  239. break;
  240. }
  241. case CodeModel::Medium: {
  242. // CALL:
  243. // pcalau12i $ra, %pc_hi20(func)
  244. // jirl $ra, $ra, %pc_lo12(func)
  245. // TAIL:
  246. // pcalau12i $scratch, %pc_hi20(func)
  247. // jirl $r0, $scratch, %pc_lo12(func)
  248. Opcode =
  249. IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
  250. Register ScratchReg =
  251. IsTailCall
  252. ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
  253. : LoongArch::R1;
  254. MachineInstrBuilder MIB =
  255. BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCALAU12I), ScratchReg);
  256. CALL = BuildMI(MBB, MBBI, DL, TII->get(Opcode)).addReg(ScratchReg);
  257. if (Func.isSymbol()) {
  258. const char *FnName = Func.getSymbolName();
  259. MIB.addExternalSymbol(FnName, LoongArchII::MO_PCREL_HI);
  260. CALL.addExternalSymbol(FnName, LoongArchII::MO_PCREL_LO);
  261. break;
  262. }
  263. assert(Func.isGlobal() && "Expected a GlobalValue at this time");
  264. const GlobalValue *GV = Func.getGlobal();
  265. MIB.addGlobalAddress(GV, 0, LoongArchII::MO_PCREL_HI);
  266. CALL.addGlobalAddress(GV, 0, LoongArchII::MO_PCREL_LO);
  267. break;
  268. }
  269. }
  270. // Transfer implicit operands.
  271. CALL.copyImplicitOps(MI);
  272. // Transfer MI flags.
  273. CALL.setMIFlags(MI.getFlags());
  274. MI.eraseFromParent();
  275. return true;
  276. }
  277. } // end namespace
  278. INITIALIZE_PASS(LoongArchPreRAExpandPseudo, "loongarch-prera-expand-pseudo",
  279. LOONGARCH_PRERA_EXPAND_PSEUDO_NAME, false, false)
  280. namespace llvm {
  281. FunctionPass *createLoongArchPreRAExpandPseudoPass() {
  282. return new LoongArchPreRAExpandPseudo();
  283. }
  284. } // end namespace llvm