From 4edcf8ab9f107c8d29c2e7a923c24a3a51e01435 Mon Sep 17 00:00:00 2001 From: wanglei Date: Fri, 11 Nov 2022 09:52:26 +0800 Subject: [PATCH] [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 --- .../LoongArch/LoongArchExpandPseudoInsts.cpp | 32 +++++++++++++++++++ .../Target/LoongArch/LoongArchInstrInfo.td | 4 +-- .../MCTargetDesc/LoongArchMCCodeEmitter.cpp | 18 ----------- llvm/test/CodeGen/LoongArch/expand-call.ll | 16 ++++++++++ 4 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 llvm/test/CodeGen/LoongArch/expand-call.ll 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 &Fixups, const MCSubtargetInfo &STI) const override; - void expandFunctionCall(const MCInst &MI, raw_ostream &OS, - SmallVectorImpl &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 &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 &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 +}