LLVM 20.0.0git
LoongArchExpandPseudoInsts.cpp
Go to the documentation of this file.
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
14#include "LoongArch.h"
15#include "LoongArchInstrInfo.h"
24#include "llvm/MC/MCContext.h"
27
28using namespace llvm;
29
31
32#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME \
33 "LoongArch Pre-RA pseudo instruction expansion pass"
34#define LOONGARCH_EXPAND_PSEUDO_NAME \
35 "LoongArch pseudo instruction expansion pass"
36
37namespace {
38
39class LoongArchPreRAExpandPseudo : public MachineFunctionPass {
40public:
42 static char ID;
43
44 LoongArchPreRAExpandPseudo() : MachineFunctionPass(ID) {
46 }
47
48 bool runOnMachineFunction(MachineFunction &MF) override;
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesCFG();
53 }
54 StringRef getPassName() const override {
56 }
57
58private:
59 bool expandMBB(MachineBasicBlock &MBB);
62 bool expandPcalau12iInstPair(MachineBasicBlock &MBB,
65 unsigned FlagsHi, unsigned SecondOpcode,
66 unsigned FlagsLo);
67 bool expandLargeAddressLoad(MachineBasicBlock &MBB,
70 unsigned LastOpcode, unsigned IdentifyingMO);
71 bool expandLargeAddressLoad(MachineBasicBlock &MBB,
74 unsigned LastOpcode, unsigned IdentifyingMO,
75 const MachineOperand &Symbol, Register DestReg,
76 bool EraseFromParent);
77 bool expandLoadAddressPcrel(MachineBasicBlock &MBB,
80 bool Large = false);
81 bool expandLoadAddressGot(MachineBasicBlock &MBB,
84 bool Large = false);
85 bool expandLoadAddressTLSLE(MachineBasicBlock &MBB,
88 bool expandLoadAddressTLSIE(MachineBasicBlock &MBB,
91 bool Large = false);
92 bool expandLoadAddressTLSLD(MachineBasicBlock &MBB,
95 bool Large = false);
96 bool expandLoadAddressTLSGD(MachineBasicBlock &MBB,
99 bool Large = false);
100 bool expandLoadAddressTLSDesc(MachineBasicBlock &MBB,
103 bool Large = false);
104 bool expandFunctionCALL(MachineBasicBlock &MBB,
107 bool IsTailCall);
108 void annotateTableJump(MachineBasicBlock &MBB,
110};
111
112char LoongArchPreRAExpandPseudo::ID = 0;
113
114bool LoongArchPreRAExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
115 TII =
116 static_cast<const LoongArchInstrInfo *>(MF.getSubtarget().getInstrInfo());
117 bool Modified = false;
118 for (auto &MBB : MF)
119 Modified |= expandMBB(MBB);
120 return Modified;
121}
122
123bool LoongArchPreRAExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
124 bool Modified = false;
125
127 while (MBBI != E) {
128 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
129 Modified |= expandMI(MBB, MBBI, NMBBI);
130 MBBI = NMBBI;
131 }
132
133 return Modified;
134}
135
136bool LoongArchPreRAExpandPseudo::expandMI(
138 MachineBasicBlock::iterator &NextMBBI) {
139 switch (MBBI->getOpcode()) {
140 case LoongArch::PseudoLA_PCREL:
141 return expandLoadAddressPcrel(MBB, MBBI, NextMBBI);
142 case LoongArch::PseudoLA_PCREL_LARGE:
143 return expandLoadAddressPcrel(MBB, MBBI, NextMBBI, /*Large=*/true);
144 case LoongArch::PseudoLA_GOT:
145 return expandLoadAddressGot(MBB, MBBI, NextMBBI);
146 case LoongArch::PseudoLA_GOT_LARGE:
147 return expandLoadAddressGot(MBB, MBBI, NextMBBI, /*Large=*/true);
148 case LoongArch::PseudoLA_TLS_LE:
149 return expandLoadAddressTLSLE(MBB, MBBI, NextMBBI);
150 case LoongArch::PseudoLA_TLS_IE:
151 return expandLoadAddressTLSIE(MBB, MBBI, NextMBBI);
152 case LoongArch::PseudoLA_TLS_IE_LARGE:
153 return expandLoadAddressTLSIE(MBB, MBBI, NextMBBI, /*Large=*/true);
154 case LoongArch::PseudoLA_TLS_LD:
155 return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI);
156 case LoongArch::PseudoLA_TLS_LD_LARGE:
157 return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI, /*Large=*/true);
158 case LoongArch::PseudoLA_TLS_GD:
159 return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI);
160 case LoongArch::PseudoLA_TLS_GD_LARGE:
161 return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI, /*Large=*/true);
162 case LoongArch::PseudoLA_TLS_DESC:
163 return expandLoadAddressTLSDesc(MBB, MBBI, NextMBBI);
164 case LoongArch::PseudoLA_TLS_DESC_LARGE:
165 return expandLoadAddressTLSDesc(MBB, MBBI, NextMBBI, /*Large=*/true);
166 case LoongArch::PseudoCALL:
167 case LoongArch::PseudoCALL_LARGE:
168 return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/false);
169 case LoongArch::PseudoTAIL:
170 case LoongArch::PseudoTAIL_LARGE:
171 return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/true);
172 case LoongArch::PseudoBRIND:
173 // If the PseudoBRIND is used to table jump, then emit a label to annotate
174 // the `jr` instruction, and save the instructions.
176 annotateTableJump(MBB, MBBI);
177 break;
178 }
179 return false;
180}
181
182bool LoongArchPreRAExpandPseudo::expandPcalau12iInstPair(
184 MachineBasicBlock::iterator &NextMBBI, unsigned FlagsHi,
185 unsigned SecondOpcode, unsigned FlagsLo) {
187 MachineInstr &MI = *MBBI;
188 DebugLoc DL = MI.getDebugLoc();
189
190 Register DestReg = MI.getOperand(0).getReg();
191 Register ScratchReg =
192 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
193 MachineOperand &Symbol = MI.getOperand(1);
194
195 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCALAU12I), ScratchReg)
196 .addDisp(Symbol, 0, FlagsHi);
197
198 MachineInstr *SecondMI =
199 BuildMI(MBB, MBBI, DL, TII->get(SecondOpcode), DestReg)
200 .addReg(ScratchReg)
201 .addDisp(Symbol, 0, FlagsLo);
202
203 if (MI.hasOneMemOperand())
204 SecondMI->addMemOperand(*MF, *MI.memoperands_begin());
205
206 MI.eraseFromParent();
207 return true;
208}
209
210bool LoongArchPreRAExpandPseudo::expandLargeAddressLoad(
212 MachineBasicBlock::iterator &NextMBBI, unsigned LastOpcode,
213 unsigned IdentifyingMO) {
214 MachineInstr &MI = *MBBI;
215 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LastOpcode, IdentifyingMO,
216 MI.getOperand(2), MI.getOperand(0).getReg(),
217 true);
218}
219
220bool LoongArchPreRAExpandPseudo::expandLargeAddressLoad(
222 MachineBasicBlock::iterator &NextMBBI, unsigned LastOpcode,
223 unsigned IdentifyingMO, const MachineOperand &Symbol, Register DestReg,
224 bool EraseFromParent) {
225 // Code Sequence:
226 //
227 // Part1: pcalau12i $scratch, %MO1(sym)
228 // Part0: addi.d $dest, $zero, %MO0(sym)
229 // Part2: lu32i.d $dest, %MO2(sym)
230 // Part3: lu52i.d $dest, $dest, %MO3(sym)
231 // Fin: LastOpcode $dest, $dest, $scratch
232
233 unsigned MO0, MO1, MO2, MO3;
234 switch (IdentifyingMO) {
235 default:
236 llvm_unreachable("unsupported identifying MO");
238 MO0 = IdentifyingMO;
242 break;
246 // These cases relocate just like the GOT case, except for Part1.
248 MO1 = IdentifyingMO;
251 break;
253 MO0 = IdentifyingMO;
257 break;
258 }
259
261 MachineInstr &MI = *MBBI;
262 DebugLoc DL = MI.getDebugLoc();
263
265 "Large code model requires LA64");
266
267 Register TmpPart1 =
268 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
269 Register TmpPart0 =
270 DestReg.isVirtual()
271 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
272 : DestReg;
273 Register TmpParts02 =
274 DestReg.isVirtual()
275 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
276 : DestReg;
277 Register TmpParts023 =
278 DestReg.isVirtual()
279 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
280 : DestReg;
281
282 auto Part1 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCALAU12I), TmpPart1);
283 auto Part0 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ADDI_D), TmpPart0)
284 .addReg(LoongArch::R0);
285 auto Part2 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU32I_D), TmpParts02)
286 // "rj" is needed due to InstrInfo pattern requirement.
287 .addReg(TmpPart0, RegState::Kill);
288 auto Part3 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU52I_D), TmpParts023)
289 .addReg(TmpParts02, RegState::Kill);
290 BuildMI(MBB, MBBI, DL, TII->get(LastOpcode), DestReg)
291 .addReg(TmpParts023)
292 .addReg(TmpPart1, RegState::Kill);
293
294 if (Symbol.getType() == MachineOperand::MO_ExternalSymbol) {
295 const char *SymName = Symbol.getSymbolName();
296 Part0.addExternalSymbol(SymName, MO0);
297 Part1.addExternalSymbol(SymName, MO1);
298 Part2.addExternalSymbol(SymName, MO2);
299 Part3.addExternalSymbol(SymName, MO3);
300 } else {
301 Part0.addDisp(Symbol, 0, MO0);
302 Part1.addDisp(Symbol, 0, MO1);
303 Part2.addDisp(Symbol, 0, MO2);
304 Part3.addDisp(Symbol, 0, MO3);
305 }
306
307 if (EraseFromParent)
308 MI.eraseFromParent();
309
310 return true;
311}
312
313bool LoongArchPreRAExpandPseudo::expandLoadAddressPcrel(
315 MachineBasicBlock::iterator &NextMBBI, bool Large) {
316 if (Large)
317 // Emit the 5-insn large address load sequence with the `%pc` family of
318 // relocs.
319 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::ADD_D,
321
322 // Code Sequence:
323 // pcalau12i $rd, %pc_hi20(sym)
324 // addi.w/d $rd, $rd, %pc_lo12(sym)
326 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
327 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
328 return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_PCREL_HI,
329 SecondOpcode, LoongArchII::MO_PCREL_LO);
330}
331
332bool LoongArchPreRAExpandPseudo::expandLoadAddressGot(
334 MachineBasicBlock::iterator &NextMBBI, bool Large) {
335 if (Large)
336 // Emit the 5-insn large address load sequence with the `%got_pc` family
337 // of relocs, loading the result from GOT with `ldx.d` in the end.
338 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::LDX_D,
340
341 // Code Sequence:
342 // pcalau12i $rd, %got_pc_hi20(sym)
343 // ld.w/d $rd, $rd, %got_pc_lo12(sym)
345 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
346 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
347 return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_GOT_PC_HI,
348 SecondOpcode, LoongArchII::MO_GOT_PC_LO);
349}
350
351bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSLE(
353 MachineBasicBlock::iterator &NextMBBI) {
354 // Code Sequence:
355 // lu12i.w $rd, %le_hi20(sym)
356 // ori $rd, $rd, %le_lo12(sym)
357 //
358 // And additionally if generating code using the large code model:
359 //
360 // lu32i.d $rd, %le64_lo20(sym)
361 // lu52i.d $rd, $rd, %le64_hi12(sym)
363 MachineInstr &MI = *MBBI;
364 DebugLoc DL = MI.getDebugLoc();
365
366 bool Large = MF->getTarget().getCodeModel() == CodeModel::Large;
367 Register DestReg = MI.getOperand(0).getReg();
368 Register Parts01 =
369 Large ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
370 : DestReg;
371 Register Part1 =
372 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
373 MachineOperand &Symbol = MI.getOperand(1);
374
375 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU12I_W), Part1)
376 .addDisp(Symbol, 0, LoongArchII::MO_LE_HI);
377
378 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ORI), Parts01)
379 .addReg(Part1, RegState::Kill)
380 .addDisp(Symbol, 0, LoongArchII::MO_LE_LO);
381
382 if (Large) {
383 Register Parts012 =
384 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
385
386 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU32I_D), Parts012)
387 // "rj" is needed due to InstrInfo pattern requirement.
388 .addReg(Parts01, RegState::Kill)
389 .addDisp(Symbol, 0, LoongArchII::MO_LE64_LO);
390 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU52I_D), DestReg)
391 .addReg(Parts012, RegState::Kill)
392 .addDisp(Symbol, 0, LoongArchII::MO_LE64_HI);
393 }
394
395 MI.eraseFromParent();
396 return true;
397}
398
399bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSIE(
401 MachineBasicBlock::iterator &NextMBBI, bool Large) {
402 if (Large)
403 // Emit the 5-insn large address load sequence with the `%ie_pc` family
404 // of relocs, loading the result with `ldx.d` in the end.
405 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::LDX_D,
407
408 // Code Sequence:
409 // pcalau12i $rd, %ie_pc_hi20(sym)
410 // ld.w/d $rd, $rd, %ie_pc_lo12(sym)
412 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
413 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
414 return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_IE_PC_HI,
415 SecondOpcode, LoongArchII::MO_IE_PC_LO);
416}
417
418bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSLD(
420 MachineBasicBlock::iterator &NextMBBI, bool Large) {
421 if (Large)
422 // Emit the 5-insn large address load sequence with the `%got_pc` family
423 // of relocs, with the `pcalau12i` insn relocated with `%ld_pc_hi20`.
424 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::ADD_D,
426
427 // Code Sequence:
428 // pcalau12i $rd, %ld_pc_hi20(sym)
429 // addi.w/d $rd, $rd, %got_pc_lo12(sym)
431 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
432 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
433 return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_LD_PC_HI,
434 SecondOpcode, LoongArchII::MO_GOT_PC_LO);
435}
436
437bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSGD(
439 MachineBasicBlock::iterator &NextMBBI, bool Large) {
440 if (Large)
441 // Emit the 5-insn large address load sequence with the `%got_pc` family
442 // of relocs, with the `pcalau12i` insn relocated with `%gd_pc_hi20`.
443 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::ADD_D,
445
446 // Code Sequence:
447 // pcalau12i $rd, %gd_pc_hi20(sym)
448 // addi.w/d $rd, $rd, %got_pc_lo12(sym)
450 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
451 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
452 return expandPcalau12iInstPair(MBB, MBBI, NextMBBI, LoongArchII::MO_GD_PC_HI,
453 SecondOpcode, LoongArchII::MO_GOT_PC_LO);
454}
455
456bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSDesc(
458 MachineBasicBlock::iterator &NextMBBI, bool Large) {
460 MachineInstr &MI = *MBBI;
461 DebugLoc DL = MI.getDebugLoc();
462
463 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
464 unsigned ADD = STI.is64Bit() ? LoongArch::ADD_D : LoongArch::ADD_W;
465 unsigned ADDI = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
466 unsigned LD = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
467
468 Register DestReg = MI.getOperand(0).getReg();
469 Register Tmp1Reg =
470 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
471 MachineOperand &Symbol = MI.getOperand(Large ? 2 : 1);
472
473 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCALAU12I), Tmp1Reg)
475
476 if (Large) {
477 // Code Sequence:
478 //
479 // pcalau12i $a0, %desc_pc_hi20(sym)
480 // addi.d $a1, $zero, %desc_pc_lo12(sym)
481 // lu32i.d $a1, %desc64_pc_lo20(sym)
482 // lu52i.d $a1, $a1, %desc64_pc_hi12(sym)
483 // add.d $a0, $a0, $a1
484 // ld.d $ra, $a0, %desc_ld(sym)
485 // jirl $ra, $ra, %desc_call(sym)
486 // add.d $dst, $a0, $tp
488 "Large code model requires LA64");
489 Register Tmp2Reg =
490 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
491 Register Tmp3Reg =
492 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
493 Register Tmp4Reg =
494 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
495 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ADDI_D), Tmp2Reg)
496 .addReg(LoongArch::R0)
498 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU32I_D), Tmp3Reg)
499 .addReg(Tmp2Reg, RegState::Kill)
501 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU52I_D), Tmp4Reg)
502 .addReg(Tmp3Reg)
504 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ADD_D), LoongArch::R4)
505 .addReg(Tmp1Reg)
506 .addReg(Tmp4Reg);
507 } else {
508 // Code Sequence:
509 // pcalau12i $a0, %desc_pc_hi20(sym)
510 // addi.w/d $a0, $a0, %desc_pc_lo12(sym)
511 // ld.w/d $ra, $a0, %desc_ld(sym)
512 // jirl $ra, $ra, %desc_ld(sym)
513 // add.d $dst, $a0, $tp
514 BuildMI(MBB, MBBI, DL, TII->get(ADDI), LoongArch::R4)
515 .addReg(Tmp1Reg)
517 }
518
519 BuildMI(MBB, MBBI, DL, TII->get(LD), LoongArch::R1)
520 .addReg(LoongArch::R4)
521 .addDisp(Symbol, 0, LoongArchII::MO_DESC_LD);
522 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PseudoDESC_CALL), LoongArch::R1)
523 .addReg(LoongArch::R1)
525 BuildMI(MBB, MBBI, DL, TII->get(ADD), DestReg)
526 .addReg(LoongArch::R4)
527 .addReg(LoongArch::R2);
528
529 MI.eraseFromParent();
530 return true;
531}
532
533bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
535 MachineBasicBlock::iterator &NextMBBI, bool IsTailCall) {
537 MachineInstr &MI = *MBBI;
538 DebugLoc DL = MI.getDebugLoc();
539 const MachineOperand &Func = MI.getOperand(0);
541 unsigned Opcode;
542
543 switch (MF->getTarget().getCodeModel()) {
544 default:
545 report_fatal_error("Unexpected code model");
546 break;
547 case CodeModel::Small: {
548 // CALL:
549 // bl func
550 // TAIL:
551 // b func
552 Opcode = IsTailCall ? LoongArch::PseudoB_TAIL : LoongArch::BL;
553 CALL = BuildMI(MBB, MBBI, DL, TII->get(Opcode)).add(Func);
554 break;
555 }
556 case CodeModel::Large: {
557 // Emit the 5-insn large address load sequence, either directly or
558 // indirectly in case of going through the GOT, then JIRL_TAIL or
559 // JIRL_CALL to $addr.
560 Opcode =
561 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
562 Register AddrReg =
563 IsTailCall
564 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
565 : LoongArch::R1;
566
567 bool UseGOT = Func.getTargetFlags() == LoongArchII::MO_CALL_PLT;
569 unsigned LAOpcode = UseGOT ? LoongArch::LDX_D : LoongArch::ADD_D;
570 expandLargeAddressLoad(MBB, MBBI, NextMBBI, LAOpcode, MO, Func, AddrReg,
571 false);
572 CALL = BuildMI(MBB, MBBI, DL, TII->get(Opcode)).addReg(AddrReg).addImm(0);
573 break;
574 }
575 }
576
577 // Transfer implicit operands.
578 CALL.copyImplicitOps(MI);
579
580 // Transfer MI flags.
581 CALL.setMIFlags(MI.getFlags());
582
583 MI.eraseFromParent();
584 return true;
585}
586
587void LoongArchPreRAExpandPseudo::annotateTableJump(
591
592 bool IsFound = false;
593
594 std::function<void(MachineInstr *, int)> FindJTIMI = [&](MachineInstr *MInst,
595 int FindDepth) {
596 if (FindDepth < 0)
597 return;
598 for (auto &MO : MInst->all_uses()) {
599 if (IsFound)
600 return;
601 Register Reg = MO.getReg();
602 if (!Reg.isVirtual())
603 continue;
604 MachineInstr *DefMI = MRI.getVRegDef(Reg);
605 if (!DefMI)
606 continue;
607 for (unsigned Idx = 0; Idx < DefMI->getNumOperands(); ++Idx) {
609 if (MO.isJTI()) {
610 MBBI->setPreInstrSymbol(
611 *MF, MF->getContext().createNamedTempSymbol("jrtb_"));
612 MF->getInfo<LoongArchMachineFunctionInfo>()->setJumpInfo(&*MBBI, &MO);
613 IsFound = true;
614 return;
615 }
616 }
617 FindJTIMI(DefMI, --FindDepth);
618 }
619 };
620
621 // FindDepth = 3, probably sufficient.
622 FindJTIMI(&*MBBI, /*FindDepth=*/3);
623}
624
625class LoongArchExpandPseudo : public MachineFunctionPass {
626public:
627 const LoongArchInstrInfo *TII;
628 static char ID;
629
630 LoongArchExpandPseudo() : MachineFunctionPass(ID) {
632 }
633
634 bool runOnMachineFunction(MachineFunction &MF) override;
635
636 StringRef getPassName() const override {
638 }
639
640private:
641 bool expandMBB(MachineBasicBlock &MBB);
646 bool expandFunctionCALL(MachineBasicBlock &MBB,
649 bool IsTailCall);
650};
651
652char LoongArchExpandPseudo::ID = 0;
653
654bool LoongArchExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
655 TII =
656 static_cast<const LoongArchInstrInfo *>(MF.getSubtarget().getInstrInfo());
657
658 bool Modified = false;
659 for (auto &MBB : MF)
660 Modified |= expandMBB(MBB);
661
662 return Modified;
663}
664
665bool LoongArchExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
666 bool Modified = false;
667
669 while (MBBI != E) {
670 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
671 Modified |= expandMI(MBB, MBBI, NMBBI);
672 MBBI = NMBBI;
673 }
674
675 return Modified;
676}
677
678bool LoongArchExpandPseudo::expandMI(MachineBasicBlock &MBB,
680 MachineBasicBlock::iterator &NextMBBI) {
681 switch (MBBI->getOpcode()) {
682 case LoongArch::PseudoCopyCFR:
683 return expandCopyCFR(MBB, MBBI, NextMBBI);
684 case LoongArch::PseudoCALL_MEDIUM:
685 return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/false);
686 case LoongArch::PseudoTAIL_MEDIUM:
687 return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/true);
688 }
689
690 return false;
691}
692
693bool LoongArchExpandPseudo::expandCopyCFR(
695 MachineBasicBlock::iterator &NextMBBI) {
697 MachineInstr &MI = *MBBI;
698 DebugLoc DL = MI.getDebugLoc();
699
700 // Expand:
701 // MBB:
702 // fcmp.caf.s $dst, $fa0, $fa0 # set $dst 0(false)
703 // bceqz $src, SinkBB
704 // FalseBB:
705 // fcmp.cueq.s $dst, $fa0, $fa0 # set $dst 1(true)
706 // SinkBB:
707 // fallthrough
708
709 const BasicBlock *LLVM_BB = MBB.getBasicBlock();
710 auto *FalseBB = MF->CreateMachineBasicBlock(LLVM_BB);
711 auto *SinkBB = MF->CreateMachineBasicBlock(LLVM_BB);
712
713 MF->insert(++MBB.getIterator(), FalseBB);
714 MF->insert(++FalseBB->getIterator(), SinkBB);
715
716 Register DestReg = MI.getOperand(0).getReg();
717 Register SrcReg = MI.getOperand(1).getReg();
718 // DestReg = 0
719 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::SET_CFR_FALSE), DestReg);
720 // Insert branch instruction.
721 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::BCEQZ))
722 .addReg(SrcReg)
723 .addMBB(SinkBB);
724 // DestReg = 1
725 BuildMI(FalseBB, DL, TII->get(LoongArch::SET_CFR_TRUE), DestReg);
726
727 FalseBB->addSuccessor(SinkBB);
728
729 SinkBB->splice(SinkBB->end(), &MBB, MI, MBB.end());
730 SinkBB->transferSuccessors(&MBB);
731
732 MBB.addSuccessor(FalseBB);
733 MBB.addSuccessor(SinkBB);
734
735 NextMBBI = MBB.end();
736 MI.eraseFromParent();
737
738 // Make sure live-ins are correctly attached to this new basic block.
739 LivePhysRegs LiveRegs;
740 computeAndAddLiveIns(LiveRegs, *FalseBB);
741 computeAndAddLiveIns(LiveRegs, *SinkBB);
742
743 return true;
744}
745
746bool LoongArchExpandPseudo::expandFunctionCALL(
748 MachineBasicBlock::iterator &NextMBBI, bool IsTailCall) {
750 MachineInstr &MI = *MBBI;
751 DebugLoc DL = MI.getDebugLoc();
752 const MachineOperand &Func = MI.getOperand(0);
754 unsigned Opcode;
755
756 switch (MF->getTarget().getCodeModel()) {
757 default:
758 report_fatal_error("Unexpected code model");
759 break;
760 case CodeModel::Medium: {
761 // CALL:
762 // pcaddu18i $ra, %call36(func)
763 // jirl $ra, $ra, 0
764 // TAIL:
765 // pcaddu18i $t8, %call36(func)
766 // jirl $r0, $t8, 0
767 Opcode =
768 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
769 Register ScratchReg = IsTailCall ? LoongArch::R20 : LoongArch::R1;
771 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCADDU18I), ScratchReg);
772
773 CALL =
774 BuildMI(MBB, MBBI, DL, TII->get(Opcode)).addReg(ScratchReg).addImm(0);
775
776 if (Func.isSymbol())
777 MIB.addExternalSymbol(Func.getSymbolName(), LoongArchII::MO_CALL36);
778 else
779 MIB.addDisp(Func, 0, LoongArchII::MO_CALL36);
780 break;
781 }
782 }
783
784 // Transfer implicit operands.
785 CALL.copyImplicitOps(MI);
786
787 // Transfer MI flags.
788 CALL.setMIFlags(MI.getFlags());
789
790 MI.eraseFromParent();
791 return true;
792}
793
794} // end namespace
795
796INITIALIZE_PASS(LoongArchPreRAExpandPseudo, "loongarch-prera-expand-pseudo",
798
799INITIALIZE_PASS(LoongArchExpandPseudo, "loongarch-expand-pseudo",
801
802namespace llvm {
803
805 return new LoongArchPreRAExpandPseudo();
806}
808 return new LoongArchExpandPseudo();
809}
810
811} // end namespace llvm
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static Expected< BitVector > expand(StringRef S, StringRef Original)
Definition: GlobPattern.cpp:21
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
loongarch expand pseudo
#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME
cl::opt< bool > LArchAnnotateTableJump
#define LOONGARCH_EXPAND_PSEUDO_NAME
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:52
LoongArchMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private Lo...
MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
Definition: MCContext.cpp:347
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addDisp(const MachineOperand &Disp, int64_t off, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:69
iterator_range< filtered_mop_iterator > all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
Definition: MachineInstr.h:772
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:578
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
@ MO_ExternalSymbol
Name of external global symbol.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
CodeModel::Model getCodeModel() const
Returns the code model.
virtual const TargetInstrInfo * getInstrInfo() const
self_iterator getIterator()
Definition: ilist_node.h:132
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetPassConfig.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ Kill
The last use of a register.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void initializeLoongArchPreRAExpandPseudoPass(PassRegistry &)
void initializeLoongArchExpandPseudoPass(PassRegistry &)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
FunctionPass * createLoongArchPreRAExpandPseudoPass()
FunctionPass * createLoongArchExpandPseudoPass()
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().