[llvm] 4edcf8a - [LoongArch] Moved expansion of PseudoCALL to LoongArchPreRAExpandPseudo pass

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 11 02:13:04 PST 2022


Author: wanglei
Date: 2022-11-11T18:04:56+08:00
New Revision: 4edcf8ab9f107c8d29c2e7a923c24a3a51e01435

URL: https://github.com/llvm/llvm-project/commit/4edcf8ab9f107c8d29c2e7a923c24a3a51e01435
DIFF: https://github.com/llvm/llvm-project/commit/4edcf8ab9f107c8d29c2e7a923c24a3a51e01435.diff

LOG: [LoongArch] Moved expansion of PseudoCALL to LoongArchPreRAExpandPseudo pass

This patch moves the expansion of the `PseudoCALL` insturction to
`LoongArchPreRAExpandPseudo` pass. This helps to expand into different
instruction sequences according to different CodeModels.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D137393

Added: 
    llvm/test/CodeGen/LoongArch/expand-call.ll

Modified: 
    llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
    llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
    llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp b/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
index e49c162cdb781..86c5e0713f4e4 100644
--- a/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
@@ -20,6 +20,7 @@
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/MC/MCContext.h"
+#include "llvm/Support/CodeGen.h"
 
 using namespace llvm;
 
@@ -74,6 +75,9 @@ class LoongArchPreRAExpandPseudo : public MachineFunctionPass {
   bool expandLoadAddressTLSGD(MachineBasicBlock &MBB,
                               MachineBasicBlock::iterator MBBI,
                               MachineBasicBlock::iterator &NextMBBI);
+  bool expandFunctionCALL(MachineBasicBlock &MBB,
+                          MachineBasicBlock::iterator MBBI,
+                          MachineBasicBlock::iterator &NextMBBI);
 };
 
 char LoongArchPreRAExpandPseudo::ID = 0;
@@ -116,6 +120,8 @@ bool LoongArchPreRAExpandPseudo::expandMI(
     return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI);
   case LoongArch::PseudoLA_TLS_GD:
     return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI);
+  case LoongArch::PseudoCALL:
+    return expandFunctionCALL(MBB, MBBI, NextMBBI);
   }
   return false;
 }
@@ -239,6 +245,32 @@ bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSGD(
                                  SecondOpcode, LoongArchII::MO_GOT_PC_LO);
 }
 
+bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
+    MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
+    MachineBasicBlock::iterator &NextMBBI) {
+  MachineFunction *MF = MBB.getParent();
+  MachineInstr &MI = *MBBI;
+  DebugLoc DL = MI.getDebugLoc();
+  const MachineOperand &Func = MI.getOperand(0);
+  MachineInstrBuilder CALL;
+
+  // TODO: CodeModel::Medium
+  switch (MF->getTarget().getCodeModel()) {
+  default:
+    report_fatal_error("Unsupported code model");
+    break;
+  case CodeModel::Small: // Default CodeModel.
+    CALL = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::BL)).add(Func);
+    break;
+  }
+
+  // Transfer implicit operands.
+  CALL.copyImplicitOps(MI);
+
+  MI.eraseFromParent();
+  return true;
+}
+
 } // end namespace
 
 INITIALIZE_PASS(LoongArchPreRAExpandPseudo, "loongarch-prera-expand-pseudo",

diff  --git a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
index 8874f7849a90e..cfd726f76ec5f 100644
--- a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
@@ -934,9 +934,7 @@ def : Pat<(brind (add GPR:$rj, simm16_lsl2:$imm16)),
           (PseudoBRIND GPR:$rj, simm16_lsl2:$imm16)>;
 
 let isCall = 1, Defs = [R1] in
-def PseudoCALL : Pseudo<(outs), (ins simm26_bl:$func)> {
-  let AsmString = "bl\t$func";
-}
+def PseudoCALL : Pseudo<(outs), (ins simm26_bl:$func)>;
 
 def : Pat<(loongarch_call tglobaladdr:$func), (PseudoCALL tglobaladdr:$func)>;
 def : Pat<(loongarch_call texternalsym:$func), (PseudoCALL texternalsym:$func)>;

diff  --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
index 5f3c1053537fc..4587d59087f32 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
@@ -43,10 +43,6 @@ class LoongArchMCCodeEmitter : public MCCodeEmitter {
                          SmallVectorImpl<MCFixup> &Fixups,
                          const MCSubtargetInfo &STI) const override;
 
-  void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
-                          SmallVectorImpl<MCFixup> &Fixups,
-                          const MCSubtargetInfo &STI) const;
-
   /// TableGen'erated function for getting the binary encoding for an
   /// instruction.
   uint64_t getBinaryCodeForInstr(const MCInst &MI,
@@ -277,17 +273,6 @@ LoongArchMCCodeEmitter::getExprOpValue(const MCInst &MI, const MCOperand &MO,
   return 0;
 }
 
-void LoongArchMCCodeEmitter::expandFunctionCall(
-    const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
-    const MCSubtargetInfo &STI) const {
-  MCOperand Func = MI.getOperand(0);
-  MCInst TmpInst = Func.isExpr()
-                       ? MCInstBuilder(LoongArch::BL).addExpr(Func.getExpr())
-                       : MCInstBuilder(LoongArch::BL).addImm(Func.getImm());
-  uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
-  support::endian::write(OS, Binary, support::little);
-}
-
 void LoongArchMCCodeEmitter::encodeInstruction(
     const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
     const MCSubtargetInfo &STI) const {
@@ -295,9 +280,6 @@ void LoongArchMCCodeEmitter::encodeInstruction(
   // Get byte count of instruction.
   unsigned Size = Desc.getSize();
 
-  if (MI.getOpcode() == LoongArch::PseudoCALL)
-    return expandFunctionCall(MI, OS, Fixups, STI);
-
   switch (Size) {
   default:
     llvm_unreachable("Unhandled encodeInstruction length!");

diff  --git a/llvm/test/CodeGen/LoongArch/expand-call.ll b/llvm/test/CodeGen/LoongArch/expand-call.ll
new file mode 100644
index 0000000000000..86bf4292665b7
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/expand-call.ll
@@ -0,0 +1,16 @@
+; RUN: llc --mtriple=loongarch64 --stop-before loongarch-prera-expand-pseudo \
+; RUN:     --verify-machineinstrs < %s | FileCheck %s --check-prefix=NOEXPAND
+; RUN: llc --mtriple=loongarch64 --stop-after loongarch-prera-expand-pseudo \
+; RUN:     --verify-machineinstrs < %s | FileCheck %s --check-prefix=EXPAND
+
+declare void @callee()
+
+define void @caller() nounwind {
+; NOEXPAND-LABEL: name: caller
+; NOEXPAND: PseudoCALL target-flags{{.*}}callee
+;
+; EXPAND-LABEL: name: caller
+; EXPAND: BL target-flags{{.*}}callee
+  call void @callee()
+  ret void
+}


        


More information about the llvm-commits mailing list