Skip to content

Commit

Permalink
[LoongArch] Moved expansion of PseudoCALL to LoongArchPreRAExpandPseu…
Browse files Browse the repository at this point in the history
…do 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
  • Loading branch information
wangleiat committed Nov 11, 2022
1 parent 8a8983b commit 4edcf8a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
32 changes: 32 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
Expand Up @@ -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)>;
Expand Down
18 changes: 0 additions & 18 deletions llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
Expand Up @@ -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,
Expand Down Expand Up @@ -277,27 +273,13 @@ 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 {
const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
// 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!");
Expand Down
16 changes: 16 additions & 0 deletions 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
}

0 comments on commit 4edcf8a

Please sign in to comment.