Skip to content

Commit 4edcf8a

Browse files
committed
[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
1 parent 8a8983b commit 4edcf8a

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/CodeGen/MachineFunctionPass.h"
2121
#include "llvm/CodeGen/MachineInstrBuilder.h"
2222
#include "llvm/MC/MCContext.h"
23+
#include "llvm/Support/CodeGen.h"
2324

2425
using namespace llvm;
2526

@@ -74,6 +75,9 @@ class LoongArchPreRAExpandPseudo : public MachineFunctionPass {
7475
bool expandLoadAddressTLSGD(MachineBasicBlock &MBB,
7576
MachineBasicBlock::iterator MBBI,
7677
MachineBasicBlock::iterator &NextMBBI);
78+
bool expandFunctionCALL(MachineBasicBlock &MBB,
79+
MachineBasicBlock::iterator MBBI,
80+
MachineBasicBlock::iterator &NextMBBI);
7781
};
7882

7983
char LoongArchPreRAExpandPseudo::ID = 0;
@@ -116,6 +120,8 @@ bool LoongArchPreRAExpandPseudo::expandMI(
116120
return expandLoadAddressTLSLD(MBB, MBBI, NextMBBI);
117121
case LoongArch::PseudoLA_TLS_GD:
118122
return expandLoadAddressTLSGD(MBB, MBBI, NextMBBI);
123+
case LoongArch::PseudoCALL:
124+
return expandFunctionCALL(MBB, MBBI, NextMBBI);
119125
}
120126
return false;
121127
}
@@ -239,6 +245,32 @@ bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSGD(
239245
SecondOpcode, LoongArchII::MO_GOT_PC_LO);
240246
}
241247

248+
bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
249+
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
250+
MachineBasicBlock::iterator &NextMBBI) {
251+
MachineFunction *MF = MBB.getParent();
252+
MachineInstr &MI = *MBBI;
253+
DebugLoc DL = MI.getDebugLoc();
254+
const MachineOperand &Func = MI.getOperand(0);
255+
MachineInstrBuilder CALL;
256+
257+
// TODO: CodeModel::Medium
258+
switch (MF->getTarget().getCodeModel()) {
259+
default:
260+
report_fatal_error("Unsupported code model");
261+
break;
262+
case CodeModel::Small: // Default CodeModel.
263+
CALL = BuildMI(MBB, MBBI, DL, TII->get(LoongArch::BL)).add(Func);
264+
break;
265+
}
266+
267+
// Transfer implicit operands.
268+
CALL.copyImplicitOps(MI);
269+
270+
MI.eraseFromParent();
271+
return true;
272+
}
273+
242274
} // end namespace
243275

244276
INITIALIZE_PASS(LoongArchPreRAExpandPseudo, "loongarch-prera-expand-pseudo",

llvm/lib/Target/LoongArch/LoongArchInstrInfo.td

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,7 @@ def : Pat<(brind (add GPR:$rj, simm16_lsl2:$imm16)),
934934
(PseudoBRIND GPR:$rj, simm16_lsl2:$imm16)>;
935935

936936
let isCall = 1, Defs = [R1] in
937-
def PseudoCALL : Pseudo<(outs), (ins simm26_bl:$func)> {
938-
let AsmString = "bl\t$func";
939-
}
937+
def PseudoCALL : Pseudo<(outs), (ins simm26_bl:$func)>;
940938

941939
def : Pat<(loongarch_call tglobaladdr:$func), (PseudoCALL tglobaladdr:$func)>;
942940
def : Pat<(loongarch_call texternalsym:$func), (PseudoCALL texternalsym:$func)>;

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ class LoongArchMCCodeEmitter : public MCCodeEmitter {
4343
SmallVectorImpl<MCFixup> &Fixups,
4444
const MCSubtargetInfo &STI) const override;
4545

46-
void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
47-
SmallVectorImpl<MCFixup> &Fixups,
48-
const MCSubtargetInfo &STI) const;
49-
5046
/// TableGen'erated function for getting the binary encoding for an
5147
/// instruction.
5248
uint64_t getBinaryCodeForInstr(const MCInst &MI,
@@ -277,27 +273,13 @@ LoongArchMCCodeEmitter::getExprOpValue(const MCInst &MI, const MCOperand &MO,
277273
return 0;
278274
}
279275

280-
void LoongArchMCCodeEmitter::expandFunctionCall(
281-
const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
282-
const MCSubtargetInfo &STI) const {
283-
MCOperand Func = MI.getOperand(0);
284-
MCInst TmpInst = Func.isExpr()
285-
? MCInstBuilder(LoongArch::BL).addExpr(Func.getExpr())
286-
: MCInstBuilder(LoongArch::BL).addImm(Func.getImm());
287-
uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
288-
support::endian::write(OS, Binary, support::little);
289-
}
290-
291276
void LoongArchMCCodeEmitter::encodeInstruction(
292277
const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
293278
const MCSubtargetInfo &STI) const {
294279
const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
295280
// Get byte count of instruction.
296281
unsigned Size = Desc.getSize();
297282

298-
if (MI.getOpcode() == LoongArch::PseudoCALL)
299-
return expandFunctionCall(MI, OS, Fixups, STI);
300-
301283
switch (Size) {
302284
default:
303285
llvm_unreachable("Unhandled encodeInstruction length!");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: llc --mtriple=loongarch64 --stop-before loongarch-prera-expand-pseudo \
2+
; RUN: --verify-machineinstrs < %s | FileCheck %s --check-prefix=NOEXPAND
3+
; RUN: llc --mtriple=loongarch64 --stop-after loongarch-prera-expand-pseudo \
4+
; RUN: --verify-machineinstrs < %s | FileCheck %s --check-prefix=EXPAND
5+
6+
declare void @callee()
7+
8+
define void @caller() nounwind {
9+
; NOEXPAND-LABEL: name: caller
10+
; NOEXPAND: PseudoCALL target-flags{{.*}}callee
11+
;
12+
; EXPAND-LABEL: name: caller
13+
; EXPAND: BL target-flags{{.*}}callee
14+
call void @callee()
15+
ret void
16+
}

0 commit comments

Comments
 (0)