LLVM 24.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"
25#include "llvm/IR/Analysis.h"
26#include "llvm/MC/MCContext.h"
29
30using namespace llvm;
31
33
34#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME \
35 "LoongArch Pre-RA pseudo instruction expansion pass"
36#define LOONGARCH_EXPAND_PSEUDO_NAME \
37 "LoongArch pseudo instruction expansion pass"
38
39namespace {
40
41class LoongArchPreRAExpandPseudoLegacy : public MachineFunctionPass {
42public:
43 static char ID;
44
45 LoongArchPreRAExpandPseudoLegacy() : MachineFunctionPass(ID) {}
46
47 bool runOnMachineFunction(MachineFunction &MF) override;
48
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.setPreservesCFG();
52 }
53 StringRef getPassName() const override {
55 }
56};
57
58char LoongArchPreRAExpandPseudoLegacy::ID = 0;
59
60static bool expandPcaxxu12iInstPair(MachineBasicBlock &MBB,
63 unsigned OpcodeHi, unsigned OpcodeLo,
64 unsigned FlagsHi, unsigned FlagsLo,
65 const LoongArchInstrInfo *TII) {
66 MachineFunction *MF = MBB.getParent();
68 DebugLoc DL = MI.getDebugLoc();
69
70 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
71 bool EnableRelax = STI.hasFeature(LoongArch::FeatureRelax);
72
73 Register DestReg = MI.getOperand(0).getReg();
74 Register ScratchReg =
75 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
76 MachineOperand &Symbol = MI.getOperand(1);
77
78 MachineInstr *FirstMI =
79 BuildMI(MBB, MBBI, DL, TII->get(OpcodeHi), ScratchReg)
80 .addDisp(Symbol, 0, LoongArchII::encodeFlags(FlagsHi, EnableRelax));
81
82 MachineInstr *SecondMI = nullptr;
83 FlagsLo = LoongArchII::encodeFlags(FlagsLo, EnableRelax);
84
85 if (OpcodeHi == LoongArch::PCALAU12I) {
86 SecondMI = BuildMI(MBB, MBBI, DL, TII->get(OpcodeLo), DestReg)
87 .addReg(ScratchReg)
88 .addDisp(Symbol, 0, FlagsLo);
89 } else {
90 MCSymbol *PCAddSymbol = MF->getContext().createNamedTempSymbol("pcadd_hi");
91 FirstMI->setPreInstrSymbol(*MF, PCAddSymbol);
92 SecondMI = BuildMI(MBB, MBBI, DL, TII->get(OpcodeLo), DestReg)
93 .addReg(ScratchReg)
94 .addSym(PCAddSymbol, FlagsLo);
95 }
96
97 if (MI.hasOneMemOperand())
98 SecondMI->addMemOperand(*MF, *MI.memoperands_begin());
99
100 MI.eraseFromParent();
101 return true;
102}
103
104static bool expandLargeAddressLoad(MachineBasicBlock &MBB,
107 unsigned LastOpcode, unsigned IdentifyingMO,
108 const MachineOperand &Symbol,
109 Register DestReg, bool EraseFromParent,
110 const LoongArchInstrInfo *TII) {
111 // Code Sequence:
112 //
113 // Part1: pcalau12i $scratch, %MO1(sym)
114 // Part0: addi.d $dest, $zero, %MO0(sym)
115 // Part2: lu32i.d $dest, %MO2(sym)
116 // Part3: lu52i.d $dest, $dest, %MO3(sym)
117 // Fin: LastOpcode $dest, $dest, $scratch
118
119 unsigned MO0, MO1, MO2, MO3;
120 switch (IdentifyingMO) {
121 default:
122 llvm_unreachable("unsupported identifying MO");
124 MO0 = IdentifyingMO;
128 break;
132 // These cases relocate just like the GOT case, except for Part1.
134 MO1 = IdentifyingMO;
137 break;
139 MO0 = IdentifyingMO;
143 break;
144 }
145
146 MachineFunction *MF = MBB.getParent();
147 MachineInstr &MI = *MBBI;
148 DebugLoc DL = MI.getDebugLoc();
149
151 "Large code model requires LA64");
152
153 Register TmpPart1 =
154 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
155 Register TmpPart0 =
156 DestReg.isVirtual()
157 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
158 : DestReg;
159 Register TmpParts02 =
160 DestReg.isVirtual()
161 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
162 : DestReg;
163 Register TmpParts023 =
164 DestReg.isVirtual()
165 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
166 : DestReg;
167
168 auto Part1 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PCALAU12I), TmpPart1);
169 auto Part0 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ADDI_D), TmpPart0)
170 .addReg(LoongArch::R0);
171 auto Part2 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU32I_D), TmpParts02)
172 // "rj" is needed due to InstrInfo pattern requirement.
173 .addReg(TmpPart0);
174 auto Part3 = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU52I_D), TmpParts023)
175 .addReg(TmpParts02);
176 BuildMI(MBB, MBBI, DL, TII->get(LastOpcode), DestReg)
177 .addReg(TmpParts023)
178 .addReg(TmpPart1);
179
180 if (Symbol.getType() == MachineOperand::MO_ExternalSymbol) {
181 const char *SymName = Symbol.getSymbolName();
182 Part0.addExternalSymbol(SymName, MO0);
183 Part1.addExternalSymbol(SymName, MO1);
184 Part2.addExternalSymbol(SymName, MO2);
185 Part3.addExternalSymbol(SymName, MO3);
186 } else {
187 Part0.addDisp(Symbol, 0, MO0);
188 Part1.addDisp(Symbol, 0, MO1);
189 Part2.addDisp(Symbol, 0, MO2);
190 Part3.addDisp(Symbol, 0, MO3);
191 }
192
193 if (EraseFromParent)
194 MI.eraseFromParent();
195
196 return true;
197}
198
199static bool expandLargeAddressLoad(MachineBasicBlock &MBB,
202 unsigned LastOpcode, unsigned IdentifyingMO,
203 const LoongArchInstrInfo *TII) {
204 MachineInstr &MI = *MBBI;
205 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LastOpcode, IdentifyingMO,
206 MI.getOperand(2), MI.getOperand(0).getReg(),
207 true, TII);
208}
209
210static bool expandLoadAddressPcrel(MachineBasicBlock &MBB,
213 const LoongArchInstrInfo *TII,
214 bool Large = false) {
215 if (Large)
216 // Emit the 5-insn large address load sequence with the `%pc` family of
217 // relocs.
218 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::ADD_D,
220
221 // Code Sequence:
222 //
223 // for la32r expands to:
224 // .Lpcadd_hi:
225 // pcaddu12i $rd, %pcadd_hi20(sym)
226 // addi.w $rd, $rd, %pcadd_lo12(.Lpcadd_hi)
227 //
228 // for la32s and la64 expands to:
229 // pcalau12i $rd, %pc_hi20(sym)
230 // addi.w/d $rd, $rd, %pc_lo12(sym)
231 MachineFunction *MF = MBB.getParent();
232 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
233 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
234 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
235 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
236 unsigned FlagsHi =
238 unsigned FlagsLo =
240 return expandPcaxxu12iInstPair(MBB, MBBI, NextMBBI, OpcodeHi, OpcodeLo,
241 FlagsHi, FlagsLo, TII);
242}
243
244static bool expandLoadAddressGot(MachineBasicBlock &MBB,
247 const LoongArchInstrInfo *TII,
248 bool Large = false) {
249 if (Large)
250 // Emit the 5-insn large address load sequence with the `%got_pc` family
251 // of relocs, loading the result from GOT with `ldx.d` in the end.
252 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::LDX_D,
254
255 // Code Sequence:
256 //
257 // for la32r expands to:
258 // .Lpcadd_hi:
259 // pcaddu12i $rd, %got_pcadd_hi20(sym)
260 // ld.w $rd, $rd, %got_pcadd_lo12(.Lpcadd_hi)
261 //
262 // for la32s and la64 expands to:
263 // pcalau12i $rd, %got_pc_hi20(sym)
264 // ld.w/d $rd, $rd, %got_pc_lo12(sym)
265 MachineFunction *MF = MBB.getParent();
266 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
267 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
268 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
269 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
270 unsigned FlagsHi =
272 unsigned FlagsLo =
274 return expandPcaxxu12iInstPair(MBB, MBBI, NextMBBI, OpcodeHi, OpcodeLo,
275 FlagsHi, FlagsLo, TII);
276}
277
278static bool expandLoadAddressTLSLE(MachineBasicBlock &MBB,
281 const LoongArchInstrInfo *TII) {
282 // Code Sequence:
283 // lu12i.w $rd, %le_hi20_r(sym)
284 // add.w/d $rd, $rd, $tp, %le_add_r(sym)
285 // addi.w/d $rd, $rd, %le_lo12_r(sym)
286 //
287 // Code Sequence while using the large code model:
288 // lu12i.w $rd, %le_hi20(sym)
289 // ori $rd, $rd, %le_lo12(sym)
290 // lu32i.d $rd, %le64_lo20(sym)
291 // lu52i.d $rd, $rd, %le64_hi12(sym)
292 MachineFunction *MF = MBB.getParent();
293 MachineInstr &MI = *MBBI;
294 DebugLoc DL = MI.getDebugLoc();
295
296 bool Large = MF->getTarget().getCodeModel() == CodeModel::Large;
297 Register DestReg = MI.getOperand(0).getReg();
298 Register Parts01 =
299 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
300 Register Part1 =
301 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
302 MachineOperand &Symbol = MI.getOperand(1);
303
304 if (!Large) {
305 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU12I_W), Part1)
306 .addDisp(Symbol, 0, LoongArchII::MO_LE_HI_R);
307
308 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
309 unsigned AddOp = STI.is64Bit() ? LoongArch::PseudoAddTPRel_D
310 : LoongArch::PseudoAddTPRel_W;
311 BuildMI(MBB, MBBI, DL, TII->get(AddOp), Parts01)
312 .addReg(Part1)
313 .addReg(LoongArch::R2)
315
316 unsigned AddiOp = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
317 BuildMI(MBB, MBBI, DL, TII->get(AddiOp), DestReg)
318 .addReg(Parts01)
319 .addDisp(Symbol, 0, LoongArchII::MO_LE_LO_R);
320 } else {
321 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU12I_W), Part1)
322 .addDisp(Symbol, 0, LoongArchII::MO_LE_HI);
323
324 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ORI), Parts01)
325 .addReg(Part1)
326 .addDisp(Symbol, 0, LoongArchII::MO_LE_LO);
327
328 Register Parts012 =
329 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
330
331 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU32I_D), Parts012)
332 // "rj" is needed due to InstrInfo pattern requirement.
333 .addReg(Parts01)
334 .addDisp(Symbol, 0, LoongArchII::MO_LE64_LO);
335 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU52I_D), DestReg)
336 .addReg(Parts012)
337 .addDisp(Symbol, 0, LoongArchII::MO_LE64_HI);
338 }
339
340 MI.eraseFromParent();
341 return true;
342}
343
344static bool expandLoadAddressTLSIE(MachineBasicBlock &MBB,
347 const LoongArchInstrInfo *TII,
348 bool Large = false) {
349 if (Large)
350 // Emit the 5-insn large address load sequence with the `%ie_pc` family
351 // of relocs, loading the result with `ldx.d` in the end.
352 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::LDX_D,
354
355 // Code Sequence:
356 //
357 // for la32r expands to:
358 // .Lpcadd_hi:
359 // pcaddu12i $rd, %ie_pcadd_hi20(sym)
360 // ld.w $rd, $rd, %ie_pcadd_lo12(.Lpcadd_hi)
361 //
362 // for la32s and la64 expands to:
363 // pcalau12i $rd, %ie_pc_hi20(sym)
364 // ld.w/d $rd, $rd, %ie_pc_lo12(sym)
365 MachineFunction *MF = MBB.getParent();
366 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
367 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
368 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
369 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
370 unsigned FlagsHi =
372 unsigned FlagsLo =
374 return expandPcaxxu12iInstPair(MBB, MBBI, NextMBBI, OpcodeHi, OpcodeLo,
375 FlagsHi, FlagsLo, TII);
376}
377
378static bool expandLoadAddressTLSLD(MachineBasicBlock &MBB,
381 const LoongArchInstrInfo *TII,
382 bool Large = false) {
383 if (Large)
384 // Emit the 5-insn large address load sequence with the `%got_pc` family
385 // of relocs, with the `pcalau12i` insn relocated with `%ld_pc_hi20`.
386 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::ADD_D,
388
389 // Code Sequence:
390 //
391 // for la32r expands to:
392 // .Lpcadd_hi:
393 // pcaddu12i $rd, %ld_pcadd_hi20(sym)
394 // addi.w $rd, $rd, %ld_pcadd_lo12(.Lpcadd_hi)
395 //
396 // for la32s and la64 expands to:
397 // pcalau12i $rd, %ld_pc_hi20(sym)
398 // addi.w/d $rd, $rd, %got_pc_lo12(sym)
399 MachineFunction *MF = MBB.getParent();
400 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
401 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
402 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
403 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
404 unsigned FlagsHi =
406 unsigned FlagsLo =
408 return expandPcaxxu12iInstPair(MBB, MBBI, NextMBBI, OpcodeHi, OpcodeLo,
409 FlagsHi, FlagsLo, TII);
410}
411
412static bool expandLoadAddressTLSGD(MachineBasicBlock &MBB,
415 const LoongArchInstrInfo *TII,
416 bool Large = false) {
417 if (Large)
418 // Emit the 5-insn large address load sequence with the `%got_pc` family
419 // of relocs, with the `pcalau12i` insn relocated with `%gd_pc_hi20`.
420 return expandLargeAddressLoad(MBB, MBBI, NextMBBI, LoongArch::ADD_D,
422
423 // Code Sequence:
424 //
425 // for la32r expands to:
426 // .Lpcadd_hi:
427 // pcaddu12i $rd, %gd_pcadd_hi20(sym)
428 // addi.w $rd, $rd, %gd_pcadd_lo12(.Lpcadd_hi)
429 //
430 // for la32s and la64 expands to:
431 // pcalau12i $rd, %gd_pc_hi20(sym)
432 // addi.w/d $rd, $rd, %got_pc_lo12(sym)
433 MachineFunction *MF = MBB.getParent();
434 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
435 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
436 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
437 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
438 unsigned FlagsHi =
440 unsigned FlagsLo =
442 return expandPcaxxu12iInstPair(MBB, MBBI, NextMBBI, OpcodeHi, OpcodeLo,
443 FlagsHi, FlagsLo, TII);
444}
445
446static bool expandLoadAddressTLSDesc(MachineBasicBlock &MBB,
449 const LoongArchInstrInfo *TII,
450 bool Large = false) {
451 MachineFunction *MF = MBB.getParent();
452 MachineInstr &MI = *MBBI;
453 DebugLoc DL = MI.getDebugLoc();
454
455 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
456 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
457 bool EnableRelax = STI.hasFeature(LoongArch::FeatureRelax);
458 unsigned PCA = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
459 unsigned ADD = STI.is64Bit() ? LoongArch::ADD_D : LoongArch::ADD_W;
460 unsigned ADDI = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
461 unsigned LD = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
462 unsigned MO =
464
465 Register DestReg = MI.getOperand(0).getReg();
466 Register Tmp1Reg =
467 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
468 MachineOperand &Symbol = MI.getOperand(Large ? 2 : 1);
469
470 MachineInstr *PCAMI =
471 BuildMI(MBB, MBBI, DL, TII->get(PCA), Tmp1Reg)
472 .addDisp(Symbol, 0,
473 LoongArchII::encodeFlags(MO, EnableRelax && !Large));
474
475 if (Large) {
476 // Code Sequence:
477 //
478 // pcalau12i $a0, %desc_pc_hi20(sym)
479 // addi.d $a1, $zero, %desc_pc_lo12(sym)
480 // lu32i.d $a1, %desc64_pc_lo20(sym)
481 // lu52i.d $a1, $a1, %desc64_pc_hi12(sym)
482 // add.d $a0, $a0, $a1
483 // ld.d $ra, $a0, %desc_ld(sym)
484 // jirl $ra, $ra, %desc_call(sym)
485 // add.d $dst, $a0, $tp
486 assert(MBB.getParent()->getSubtarget<LoongArchSubtarget>().is64Bit() &&
487 "Large code model requires LA64");
488 Register Tmp2Reg =
489 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
490 Register Tmp3Reg =
491 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
492 Register Tmp4Reg =
493 MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass);
494 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ADDI_D), Tmp2Reg)
495 .addReg(LoongArch::R0)
497 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU32I_D), Tmp3Reg)
498 .addReg(Tmp2Reg)
500 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU52I_D), Tmp4Reg)
501 .addReg(Tmp3Reg)
503 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::ADD_D), LoongArch::R4)
504 .addReg(Tmp1Reg)
505 .addReg(Tmp4Reg);
506 } else {
507 // Code Sequence:
508 //
509 // for la32r expands to:
510 // .Lpcadd_hi:
511 // pcaddu12i $a0, %desc_pcadd_hi20(sym)
512 // addi.w $a0, $a0, %desc_pcadd_lo12(.Lpcadd_hi)
513 // ld.w $ra, $a0, %desc_ld(sym)
514 // jirl $ra, $ra, %desc_call(sym)
515 // add.w $dst, $a0, $tp
516 //
517 // for la32s and la64 expands to:
518 // pcalau12i $a0, %desc_pc_hi20(sym)
519 // addi.w/d $a0, $a0, %desc_pc_lo12(sym)
520 // ld.w/d $ra, $a0, %desc_ld(sym)
521 // jirl $ra, $ra, %desc_call(sym)
522 // add.w/d $dst, $a0, $tp
523 if (Has32S) {
524 BuildMI(MBB, MBBI, DL, TII->get(ADDI), LoongArch::R4)
525 .addReg(Tmp1Reg)
526 .addDisp(Symbol, 0,
528 EnableRelax));
529 } else {
530 MCSymbol *PCASymbol = MF->getContext().createNamedTempSymbol("pcadd_hi");
531 PCAMI->setPreInstrSymbol(*MF, PCASymbol);
532 BuildMI(MBB, MBBI, DL, TII->get(ADDI), LoongArch::R4)
533 .addReg(Tmp1Reg)
535 LoongArchII::MO_DESC_PCADD_LO, EnableRelax));
536 }
537 }
538
539 BuildMI(MBB, MBBI, DL, TII->get(LD), LoongArch::R1)
540 .addReg(LoongArch::R4)
541 .addDisp(Symbol, 0,
543 EnableRelax && !Large));
544 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::PseudoDESC_CALL), LoongArch::R1)
545 .addReg(LoongArch::R1)
546 .addDisp(Symbol, 0,
548 EnableRelax && !Large))
549 .setOperandDead(0); // $r1
550 BuildMI(MBB, MBBI, DL, TII->get(ADD), DestReg)
551 .addReg(LoongArch::R4)
552 .addReg(LoongArch::R2);
553
554 MI.eraseFromParent();
555 return true;
556}
557
558static bool expandFunctionCALLPreRA(MachineBasicBlock &MBB,
561 bool IsTailCall,
562 const LoongArchInstrInfo *TII) {
563 MachineFunction *MF = MBB.getParent();
564 MachineInstr &MI = *MBBI;
565
566 switch (MF->getTarget().getCodeModel()) {
567 default:
568 report_fatal_error("Unexpected code model");
569 break;
570 case CodeModel::Small: {
571 // CALL:
572 // bl func
573 // TAIL:
574 // b func
575 unsigned Opcode = IsTailCall ? LoongArch::PseudoB_TAIL : LoongArch::BL;
576 MI.setDesc(TII->get(Opcode));
577 return true;
578 }
579 case CodeModel::Large: {
580 // Emit the 5-insn large address load sequence, either directly or
581 // indirectly in case of going through the GOT, then JIRL_TAIL or
582 // JIRL_CALL to $addr.
583 const MachineOperand &Func = MI.getOperand(0);
584 unsigned Opcode =
585 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
586 Register AddrReg =
587 IsTailCall
588 ? MF->getRegInfo().createVirtualRegister(&LoongArch::GPRRegClass)
589 : LoongArch::R1;
590
591 bool UseGOT = Func.getTargetFlags() == LoongArchII::MO_CALL_PLT;
593 unsigned LAOpcode = UseGOT ? LoongArch::LDX_D : LoongArch::ADD_D;
594 expandLargeAddressLoad(MBB, MBBI, NextMBBI, LAOpcode, MO, Func, AddrReg,
595 false, TII);
596
597 // Mutate the pseudo into the JIRL in place, keeping its implicit operands.
598 // Operand 0 becomes the address register; operand 1 is the jump offset.
599 MI.setDesc(TII->get(Opcode));
600 MI.getOperand(0).ChangeToRegister(AddrReg, /*isDef=*/false);
601 MI.insert(std::next(MI.operands_begin()), MachineOperand::CreateImm(0));
602 return true;
603 }
604 }
605 llvm_unreachable("Unexpected code model");
606}
607
608static void annotateTableJump(MachineBasicBlock &MBB,
610 MachineFunction *MF = MBB.getParent();
611 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
612
613 bool IsFound = false;
614
615 std::function<void(MachineInstr *, int)> FindJTIMI = [&](MachineInstr *MInst,
616 int FindDepth) {
617 if (FindDepth < 0)
618 return;
619 for (auto &MO : MInst->all_uses()) {
620 if (IsFound)
621 return;
622 Register Reg = MO.getReg();
623 if (!Reg.isVirtual())
624 continue;
625 MachineInstr *DefMI = MRI.getVRegDef(Reg);
626 if (!DefMI)
627 continue;
628 for (unsigned Idx = 0; Idx < DefMI->getNumOperands(); ++Idx) {
629 MachineOperand &MO = DefMI->getOperand(Idx);
630 if (MO.isJTI()) {
631 MBBI->setPreInstrSymbol(
632 *MF, MF->getContext().createNamedTempSymbol("jrtb_"));
633 MF->getInfo<LoongArchMachineFunctionInfo>()->setJumpInfo(
634 &*MBBI, MO.getIndex());
635 IsFound = true;
636 return;
637 }
638 }
639 FindJTIMI(DefMI, --FindDepth);
640 }
641 };
642
643 // FindDepth = 4, probably sufficient.
644 FindJTIMI(&*MBBI, /*FindDepth=*/4);
645}
646
647static bool expandMOVCF2GRViaFR(MachineBasicBlock &MBB,
650 const LoongArchInstrInfo *TII) {
651 MachineFunction *MF = MBB.getParent();
652 MachineInstr &MI = *MBBI;
653 DebugLoc DL = MI.getDebugLoc();
654
655 Register TmpReg =
656 MF->getRegInfo().createVirtualRegister(&LoongArch::FPR32RegClass);
657 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::MOVCF2FR_xS), TmpReg)
658 .addReg(MI.getOperand(1).getReg());
659 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::MOVFR2GR_S),
660 MI.getOperand(0).getReg())
661 .addReg(TmpReg);
662 MI.eraseFromParent();
663 return true;
664}
665
666static bool expandMIPreRA(MachineBasicBlock &MBB,
669 const LoongArchInstrInfo *TII) {
670 switch (MBBI->getOpcode()) {
671 case LoongArch::PseudoLA_PCREL:
672 return expandLoadAddressPcrel(MBB, MBBI, NextMBBI, TII);
673 case LoongArch::PseudoLA_PCREL_LARGE:
674 return expandLoadAddressPcrel(MBB, MBBI, NextMBBI, TII, /*Large=*/true);
675 case LoongArch::PseudoLA_GOT:
676 return expandLoadAddressGot(MBB, MBBI, NextMBBI, TII);
677 case LoongArch::PseudoLA_GOT_LARGE:
678 return expandLoadAddressGot(MBB, MBBI, NextMBBI, TII, /*Large=*/true);
679 case LoongArch::PseudoLA_TLS_LE:
680 return expandLoadAddressTLSLE(MBB, MBBI, NextMBBI, TII);
681 case LoongArch::PseudoLA_TLS_IE:
682 return expandLoadAddressTLSIE(MBB, MBBI, NextMBBI, TII);
683 case LoongArch::PseudoLA_TLS_IE_LARGE:
684 return expandLoadAddressTLSIE(MBB, MBBI, NextMBBI, TII, /*Large=*/true);
685 case LoongArch::PseudoLA_TLS_LD:
686 return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI, TII);
687 case LoongArch::PseudoLA_TLS_LD_LARGE:
688 return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI, TII, /*Large=*/true);
689 case LoongArch::PseudoLA_TLS_GD:
690 return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI, TII);
691 case LoongArch::PseudoLA_TLS_GD_LARGE:
692 return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI, TII, /*Large=*/true);
693 case LoongArch::PseudoLA_TLS_DESC:
694 return expandLoadAddressTLSDesc(MBB, MBBI, NextMBBI, TII);
695 case LoongArch::PseudoLA_TLS_DESC_LARGE:
696 return expandLoadAddressTLSDesc(MBB, MBBI, NextMBBI, TII, /*Large=*/true);
697 case LoongArch::PseudoCALL_SMALL:
698 case LoongArch::PseudoCALL_LARGE:
699 return expandFunctionCALLPreRA(MBB, MBBI, NextMBBI, /*IsTailCall=*/false,
700 TII);
701 case LoongArch::PseudoTAIL_SMALL:
702 case LoongArch::PseudoTAIL_LARGE:
703 return expandFunctionCALLPreRA(MBB, MBBI, NextMBBI, /*IsTailCall=*/true,
704 TII);
705 case LoongArch::PseudoMOVCF2GRViaFR:
706 return expandMOVCF2GRViaFR(MBB, MBBI, NextMBBI, TII);
707 case LoongArch::PseudoBRIND:
708 // If the PseudoBRIND is used to table jump, then emit a label to annotate
709 // the `jr` instruction, and save the instructions.
711 annotateTableJump(MBB, MBBI);
712 break;
713 }
714 return false;
715}
716
717static bool expandMBBPreRA(MachineBasicBlock &MBB,
718 const LoongArchInstrInfo *TII) {
719 bool Modified = false;
720
721 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
722 while (MBBI != E) {
723 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
724 Modified |= expandMIPreRA(MBB, MBBI, NMBBI, TII);
725 MBBI = NMBBI;
726 }
727
728 return Modified;
729}
730
731static bool expandPseudosPreRAInMF(MachineFunction &MF) {
732 const auto *TII =
733 static_cast<const LoongArchInstrInfo *>(MF.getSubtarget().getInstrInfo());
734 bool Modified = false;
735 for (auto &MBB : MF)
736 Modified |= expandMBBPreRA(MBB, TII);
737 return Modified;
738}
739
740bool LoongArchPreRAExpandPseudoLegacy::runOnMachineFunction(
741 MachineFunction &MF) {
742 return expandPseudosPreRAInMF(MF);
743}
744
745class LoongArchExpandPseudoLegacy : public MachineFunctionPass {
746public:
747 static char ID;
748
749 LoongArchExpandPseudoLegacy() : MachineFunctionPass(ID) {}
750
751 bool runOnMachineFunction(MachineFunction &MF) override;
752
753 StringRef getPassName() const override {
755 }
756};
757
758} // end namespace
759
760char LoongArchExpandPseudoLegacy::ID = 0;
761
765 const LoongArchInstrInfo *TII) {
766 MachineFunction *MF = MBB.getParent();
767 MachineInstr &MI = *MBBI;
768 DebugLoc DL = MI.getDebugLoc();
769
770 // Expand:
771 // MBB:
772 // fcmp.caf.s $dst, $fa0, $fa0 # set $dst 0(false)
773 // bceqz $src, SinkBB
774 // FalseBB:
775 // fcmp.cueq.s $dst, $fa0, $fa0 # set $dst 1(true)
776 // SinkBB:
777 // fallthrough
778
779 const BasicBlock *LLVM_BB = MBB.getBasicBlock();
780 auto *FalseBB = MF->CreateMachineBasicBlock(LLVM_BB);
781 auto *SinkBB = MF->CreateMachineBasicBlock(LLVM_BB);
782
783 MF->insert(++MBB.getIterator(), FalseBB);
784 MF->insert(++FalseBB->getIterator(), SinkBB);
785
786 Register DestReg = MI.getOperand(0).getReg();
787 Register SrcReg = MI.getOperand(1).getReg();
788 // DestReg = 0
789 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::SET_CFR_FALSE), DestReg);
790 // Insert branch instruction.
791 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::BCEQZ))
792 .addReg(SrcReg)
793 .addMBB(SinkBB);
794 // DestReg = 1
795 BuildMI(FalseBB, DL, TII->get(LoongArch::SET_CFR_TRUE), DestReg);
796
797 FalseBB->addSuccessor(SinkBB);
798
799 SinkBB->splice(SinkBB->end(), &MBB, MI, MBB.end());
800 SinkBB->transferSuccessors(&MBB);
801
802 MBB.addSuccessor(FalseBB);
803 MBB.addSuccessor(SinkBB);
804
805 NextMBBI = MBB.end();
806 MI.eraseFromParent();
807
808 // Make sure live-ins are correctly attached to this new basic block.
812
813 return true;
814}
815
819 bool IsTailCall, const LoongArchInstrInfo *TII) {
820 MachineFunction *MF = MBB.getParent();
821 MachineInstr &MI = *MBBI;
822 DebugLoc DL = MI.getDebugLoc();
823 const MachineOperand &Func = MI.getOperand(0);
824
825 switch (MF->getTarget().getCodeModel()) {
826 default:
827 report_fatal_error("Unexpected code model");
828 break;
829 case CodeModel::Medium: {
830 // for la32 expands to:
831 // CALL:
832 // pcaddu12i $ra, %call30(func)
833 // jirl $ra, $ra, 0
834 // TAIL:
835 // pcaddu12i $t8, %call30(func)
836 // jirl $r0, $t8, 0
837 //
838 // for la64 expands to:
839 // CALL:
840 // pcaddu18i $ra, %call36(func)
841 // jirl $ra, $ra, 0
842 // TAIL:
843 // pcaddu18i $t8, %call36(func)
844 // jirl $r0, $t8, 0
845 unsigned Opcode =
846 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
847 Register ScratchReg = IsTailCall ? LoongArch::R20 : LoongArch::R1;
848 bool Is64Bit = MF->getSubtarget<LoongArchSubtarget>().is64Bit();
849 unsigned PC = Is64Bit ? LoongArch::PCADDU18I : LoongArch::PCADDU12I;
850 unsigned MO = Is64Bit ? LoongArchII::MO_CALL36 : LoongArchII::MO_CALL30;
851 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(PC), ScratchReg);
852
853 if (Func.isSymbol())
854 MIB.addExternalSymbol(Func.getSymbolName(), MO);
855 else
856 MIB.addDisp(Func, 0, MO);
857
858 // Mutate the pseudo into the JIRL in place, keeping its implicit operands.
859 // Operand 0 becomes the address register; operand 1 is the jump offset.
860 MI.setDesc(TII->get(Opcode));
861 MI.getOperand(0).ChangeToRegister(ScratchReg, /*isDef=*/false);
862 MI.insert(std::next(MI.operands_begin()), MachineOperand::CreateImm(0));
863 return true;
864 }
865 }
866 llvm_unreachable("Unexpected code model");
867}
868
871 const LoongArchInstrInfo *TII) {
872 DebugLoc DL = MBBI->getDebugLoc();
873 MachineFunction *MF = MBB.getParent();
874 const auto &STI = MF->getSubtarget<LoongArchSubtarget>();
875
876 Register DstReg = MBBI->getOperand(0).getReg();
877 bool DstIsDead = MBBI->getOperand(0).isDead();
878 bool Renamable = MBBI->getOperand(0).isRenamable();
879 Register BaseReg = MBBI->getOperand(1).getReg();
880 int64_t Hi = MBBI->getOperand(2).getImm();
881 bool IsLA64 = STI.is64Bit();
882
883 // Expand to LU12I_W + ADD_W/D: the immediate is upper 20-bit.
884 BuildMI(MBB, MBBI, DL, TII->get(LoongArch::LU12I_W))
886 .addImm(Hi);
887 BuildMI(MBB, MBBI, DL, TII->get(IsLA64 ? LoongArch::ADD_D : LoongArch::ADD_W))
888 .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead) |
890 .addReg(BaseReg)
892
893 MBBI->eraseFromParent();
894 return true;
895}
896
899 const LoongArchInstrInfo *TII) {
900 switch (MBBI->getOpcode()) {
901 case LoongArch::PseudoCopyCFR:
902 return expandCopyCFR(MBB, MBBI, NextMBBI, TII);
903 case LoongArch::PseudoCALL_MEDIUM:
904 return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/false, TII);
905 case LoongArch::PseudoTAIL_MEDIUM:
906 return expandFunctionCALL(MBB, MBBI, NextMBBI, /*IsTailCall=*/true, TII);
907 case LoongArch::PseudoAddUpperImm:
908 return expandAddUpperImm(MBB, MBBI, TII);
909 }
910
911 return false;
912}
913
915 bool Modified = false;
916
917 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
918 while (MBBI != E) {
919 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
920 Modified |= expandMI(MBB, MBBI, NMBBI, TII);
921 MBBI = NMBBI;
922 }
923
924 return Modified;
925}
926
928 const LoongArchInstrInfo *TII =
929 static_cast<const LoongArchInstrInfo *>(MF.getSubtarget().getInstrInfo());
930
931 bool Modified = false;
932 for (auto &MBB : MF)
934
935 return Modified;
936}
937
938bool LoongArchExpandPseudoLegacy::runOnMachineFunction(MachineFunction &MF) {
939 return expandPseudo(MF);
940}
941
942PreservedAnalyses
948
956
957INITIALIZE_PASS(LoongArchPreRAExpandPseudoLegacy,
958 "loongarch-pre-ra-expand-pseudo",
960
961INITIALIZE_PASS(LoongArchExpandPseudoLegacy, "loongarch-expand-pseudo",
963
964namespace llvm {
965
967 return new LoongArchPreRAExpandPseudoLegacy();
968}
970 return new LoongArchExpandPseudoLegacy();
971}
972
973} // end namespace llvm
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
cl::opt< bool > LArchAnnotateTableJump("loongarch-annotate-tablejump", cl::Hidden, cl::desc("Annotate table jump instruction to correlate it with the jump table."), cl::init(false))
static bool expandMBB(MachineBasicBlock &MBB, const LoongArchInstrInfo *TII)
static bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const LoongArchInstrInfo *TII)
static bool expandPseudo(MachineFunction &MF)
#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME
#define LOONGARCH_EXPAND_PSEUDO_NAME
static bool expandAddUpperImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const LoongArchInstrInfo *TII)
static bool expandFunctionCALL(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, bool IsTailCall, const LoongArchInstrInfo *TII)
static bool expandCopyCFR(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const LoongArchInstrInfo *TII)
Register Reg
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
MachineRegisterInfo & MRI
static bool is64Bit(const char *name)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
LLVM Basic Block Representation.
Definition BasicBlock.h:61
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
A set of physical registers with utility functions to track liveness when walking backward/forward th...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LoongArchMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private Lo...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
MachineInstrBundleIterator< MachineInstr > iterator
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.
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)
CreateMachineInstr - Allocate a new MachineInstr.
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 & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addDisp(const MachineOperand &Disp, int64_t off, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
LLVM_ABI void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
filtered_mop_range all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
LLVM_ABI 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.
static MachineOperand CreateImm(int64_t Val)
@ MO_ExternalSymbol
Name of external global symbol.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
CodeModel::Model getCodeModel() const
Returns the code model.
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetPassConfig.
static unsigned encodeFlags(unsigned Flags, bool Relax)
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createLoongArchExpandPseudoLegacyPass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
@ Define
Register definition.
@ Renamable
Register that may be renamed.
FunctionPass * createLoongArchPreRAExpandPseudoLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
constexpr RegState getDeadRegState(bool B)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr RegState getRenamableRegState(bool B)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().