@@ -165,11 +165,9 @@ bool LoongArchPreRAExpandPseudo::expandMI(
165165 case LoongArch::PseudoLA_TLS_DESC_LARGE:
166166 return expandLoadAddressTLSDesc (MBB, MBBI, NextMBBI, /* Large=*/ true );
167167 case LoongArch::PseudoCALL:
168- case LoongArch::PseudoCALL_MEDIUM:
169168 case LoongArch::PseudoCALL_LARGE:
170169 return expandFunctionCALL (MBB, MBBI, NextMBBI, /* IsTailCall=*/ false );
171170 case LoongArch::PseudoTAIL:
172- case LoongArch::PseudoTAIL_MEDIUM:
173171 case LoongArch::PseudoTAIL_LARGE:
174172 return expandFunctionCALL (MBB, MBBI, NextMBBI, /* IsTailCall=*/ true );
175173 case LoongArch::PseudoBRIND:
@@ -545,7 +543,7 @@ bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
545543
546544 switch (MF->getTarget ().getCodeModel ()) {
547545 default :
548- report_fatal_error (" Unsupported code model" );
546+ report_fatal_error (" Unexpected code model" );
549547 break ;
550548 case CodeModel::Small: {
551549 // CALL:
@@ -556,31 +554,6 @@ bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
556554 CALL = BuildMI (MBB, MBBI, DL, TII->get (Opcode)).add (Func);
557555 break ;
558556 }
559- case CodeModel::Medium: {
560- // CALL:
561- // pcaddu18i $ra, %call36(func)
562- // jirl $ra, $ra, 0
563- // TAIL:
564- // pcaddu18i $scratch, %call36(func)
565- // jirl $r0, $scratch, 0
566- Opcode =
567- IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
568- Register ScratchReg =
569- IsTailCall
570- ? MF->getRegInfo ().createVirtualRegister (&LoongArch::GPRRegClass)
571- : LoongArch::R1;
572- MachineInstrBuilder MIB =
573- BuildMI (MBB, MBBI, DL, TII->get (LoongArch::PCADDU18I), ScratchReg);
574-
575- CALL =
576- BuildMI (MBB, MBBI, DL, TII->get (Opcode)).addReg (ScratchReg).addImm (0 );
577-
578- if (Func.isSymbol ())
579- MIB.addExternalSymbol (Func.getSymbolName (), LoongArchII::MO_CALL36);
580- else
581- MIB.addDisp (Func, 0 , LoongArchII::MO_CALL36);
582- break ;
583- }
584557 case CodeModel::Large: {
585558 // Emit the 5-insn large address load sequence, either directly or
586559 // indirectly in case of going through the GOT, then JIRL_TAIL or
@@ -671,6 +644,10 @@ class LoongArchExpandPseudo : public MachineFunctionPass {
671644 MachineBasicBlock::iterator &NextMBBI);
672645 bool expandCopyCFR (MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
673646 MachineBasicBlock::iterator &NextMBBI);
647+ bool expandFunctionCALL (MachineBasicBlock &MBB,
648+ MachineBasicBlock::iterator MBBI,
649+ MachineBasicBlock::iterator &NextMBBI,
650+ bool IsTailCall);
674651};
675652
676653char LoongArchExpandPseudo::ID = 0 ;
@@ -705,6 +682,10 @@ bool LoongArchExpandPseudo::expandMI(MachineBasicBlock &MBB,
705682 switch (MBBI->getOpcode ()) {
706683 case LoongArch::PseudoCopyCFR:
707684 return expandCopyCFR (MBB, MBBI, NextMBBI);
685+ case LoongArch::PseudoCALL_MEDIUM:
686+ return expandFunctionCALL (MBB, MBBI, NextMBBI, /* IsTailCall=*/ false );
687+ case LoongArch::PseudoTAIL_MEDIUM:
688+ return expandFunctionCALL (MBB, MBBI, NextMBBI, /* IsTailCall=*/ true );
708689 }
709690
710691 return false ;
@@ -763,6 +744,54 @@ bool LoongArchExpandPseudo::expandCopyCFR(
763744 return true ;
764745}
765746
747+ bool LoongArchExpandPseudo::expandFunctionCALL (
748+ MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
749+ MachineBasicBlock::iterator &NextMBBI, bool IsTailCall) {
750+ MachineFunction *MF = MBB.getParent ();
751+ MachineInstr &MI = *MBBI;
752+ DebugLoc DL = MI.getDebugLoc ();
753+ const MachineOperand &Func = MI.getOperand (0 );
754+ MachineInstrBuilder CALL;
755+ unsigned Opcode;
756+
757+ switch (MF->getTarget ().getCodeModel ()) {
758+ default :
759+ report_fatal_error (" Unexpected code model" );
760+ break ;
761+ case CodeModel::Medium: {
762+ // CALL:
763+ // pcaddu18i $ra, %call36(func)
764+ // jirl $ra, $ra, 0
765+ // TAIL:
766+ // pcaddu18i $t8, %call36(func)
767+ // jirl $r0, $t8, 0
768+ Opcode =
769+ IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
770+ Register ScratchReg = IsTailCall ? LoongArch::R20 : LoongArch::R1;
771+ MachineInstrBuilder MIB =
772+ BuildMI (MBB, MBBI, DL, TII->get (LoongArch::PCADDU18I), ScratchReg);
773+
774+ CALL =
775+ BuildMI (MBB, MBBI, DL, TII->get (Opcode)).addReg (ScratchReg).addImm (0 );
776+
777+ if (Func.isSymbol ())
778+ MIB.addExternalSymbol (Func.getSymbolName (), LoongArchII::MO_CALL36);
779+ else
780+ MIB.addDisp (Func, 0 , LoongArchII::MO_CALL36);
781+ break ;
782+ }
783+ }
784+
785+ // Transfer implicit operands.
786+ CALL.copyImplicitOps (MI);
787+
788+ // Transfer MI flags.
789+ CALL.setMIFlags (MI.getFlags ());
790+
791+ MI.eraseFromParent ();
792+ return true ;
793+ }
794+
766795} // end namespace
767796
768797INITIALIZE_PASS (LoongArchPreRAExpandPseudo, " loongarch-prera-expand-pseudo" ,
0 commit comments