34#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME \
35 "LoongArch Pre-RA pseudo instruction expansion pass"
36#define LOONGARCH_EXPAND_PSEUDO_NAME \
37 "LoongArch pseudo instruction expansion pass"
58char LoongArchPreRAExpandPseudoLegacy::ID = 0;
63 unsigned OpcodeHi,
unsigned OpcodeLo,
64 unsigned FlagsHi,
unsigned FlagsLo,
71 bool EnableRelax = STI.hasFeature(LoongArch::FeatureRelax);
85 if (OpcodeHi == LoongArch::PCALAU12I) {
94 .
addSym(PCAddSymbol, FlagsLo);
97 if (
MI.hasOneMemOperand())
100 MI.eraseFromParent();
107 unsigned LastOpcode,
unsigned IdentifyingMO,
109 Register DestReg,
bool EraseFromParent,
119 unsigned MO0, MO1, MO2, MO3;
120 switch (IdentifyingMO) {
151 "Large code model requires LA64");
181 const char *SymName = Symbol.getSymbolName();
182 Part0.addExternalSymbol(SymName, MO0);
183 Part1.addExternalSymbol(SymName, MO1);
184 Part2.addExternalSymbol(SymName, MO2);
185 Part3.addExternalSymbol(SymName, MO3);
187 Part0.addDisp(Symbol, 0, MO0);
188 Part1.addDisp(Symbol, 0, MO1);
189 Part2.addDisp(Symbol, 0, MO2);
190 Part3.addDisp(Symbol, 0, MO3);
194 MI.eraseFromParent();
202 unsigned LastOpcode,
unsigned IdentifyingMO,
205 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LastOpcode, IdentifyingMO,
206 MI.getOperand(2),
MI.getOperand(0).getReg(),
214 bool Large =
false) {
218 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::ADD_D,
233 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
234 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
235 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
240 return expandPcaxxu12iInstPair(
MBB,
MBBI, NextMBBI, OpcodeHi, OpcodeLo,
241 FlagsHi, FlagsLo,
TII);
248 bool Large =
false) {
252 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::LDX_D,
267 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
268 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
269 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
274 return expandPcaxxu12iInstPair(
MBB,
MBBI, NextMBBI, OpcodeHi, OpcodeLo,
275 FlagsHi, FlagsLo,
TII);
309 unsigned AddOp = STI.
is64Bit() ? LoongArch::PseudoAddTPRel_D
310 : LoongArch::PseudoAddTPRel_W;
316 unsigned AddiOp = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
340 MI.eraseFromParent();
348 bool Large =
false) {
352 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::LDX_D,
367 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
368 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
369 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
374 return expandPcaxxu12iInstPair(
MBB,
MBBI, NextMBBI, OpcodeHi, OpcodeLo,
375 FlagsHi, FlagsLo,
TII);
382 bool Large =
false) {
386 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::ADD_D,
401 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
402 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
403 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
408 return expandPcaxxu12iInstPair(
MBB,
MBBI, NextMBBI, OpcodeHi, OpcodeLo,
409 FlagsHi, FlagsLo,
TII);
416 bool Large =
false) {
420 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::ADD_D,
435 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
436 unsigned OpcodeHi = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
437 unsigned OpcodeLo = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
442 return expandPcaxxu12iInstPair(
MBB,
MBBI, NextMBBI, OpcodeHi, OpcodeLo,
443 FlagsHi, FlagsLo,
TII);
450 bool Large =
false) {
456 bool Has32S = STI.hasFeature(LoongArch::Feature32S);
457 bool EnableRelax = STI.hasFeature(LoongArch::FeatureRelax);
458 unsigned PCA = Has32S ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
459 unsigned ADD = STI.is64Bit() ? LoongArch::ADD_D : LoongArch::ADD_W;
460 unsigned ADDI = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
461 unsigned LD = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
487 "Large code model requires LA64");
543 EnableRelax && !Large));
548 EnableRelax && !Large))
554 MI.eraseFromParent();
575 unsigned Opcode = IsTailCall ? LoongArch::PseudoB_TAIL : LoongArch::BL;
576 MI.setDesc(
TII->get(Opcode));
585 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
593 unsigned LAOpcode = UseGOT ? LoongArch::LDX_D : LoongArch::ADD_D;
594 expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LAOpcode, MO, Func, AddrReg,
599 MI.setDesc(
TII->get(Opcode));
600 MI.getOperand(0).ChangeToRegister(AddrReg,
false);
613 bool IsFound =
false;
619 for (
auto &MO : MInst->
all_uses()) {
623 if (!
Reg.isVirtual())
628 for (
unsigned Idx = 0; Idx <
DefMI->getNumOperands(); ++Idx) {
631 MBBI->setPreInstrSymbol(
639 FindJTIMI(
DefMI, --FindDepth);
644 FindJTIMI(&*
MBBI, 4);
660 MI.getOperand(0).getReg())
662 MI.eraseFromParent();
670 switch (
MBBI->getOpcode()) {
671 case LoongArch::PseudoLA_PCREL:
672 return expandLoadAddressPcrel(
MBB,
MBBI, NextMBBI,
TII);
673 case LoongArch::PseudoLA_PCREL_LARGE:
674 return expandLoadAddressPcrel(
MBB,
MBBI, NextMBBI,
TII,
true);
675 case LoongArch::PseudoLA_GOT:
676 return expandLoadAddressGot(
MBB,
MBBI, NextMBBI,
TII);
677 case LoongArch::PseudoLA_GOT_LARGE:
678 return expandLoadAddressGot(
MBB,
MBBI, NextMBBI,
TII,
true);
679 case LoongArch::PseudoLA_TLS_LE:
680 return expandLoadAddressTLSLE(
MBB,
MBBI, NextMBBI,
TII);
681 case LoongArch::PseudoLA_TLS_IE:
682 return expandLoadAddressTLSIE(
MBB,
MBBI, NextMBBI,
TII);
683 case LoongArch::PseudoLA_TLS_IE_LARGE:
684 return expandLoadAddressTLSIE(
MBB,
MBBI, NextMBBI,
TII,
true);
685 case LoongArch::PseudoLA_TLS_LD:
686 return expandLoadAddressTLSLD(
MBB,
MBBI, NextMBBI,
TII);
687 case LoongArch::PseudoLA_TLS_LD_LARGE:
688 return expandLoadAddressTLSLD(
MBB,
MBBI, NextMBBI,
TII,
true);
689 case LoongArch::PseudoLA_TLS_GD:
690 return expandLoadAddressTLSGD(
MBB,
MBBI, NextMBBI,
TII);
691 case LoongArch::PseudoLA_TLS_GD_LARGE:
692 return expandLoadAddressTLSGD(
MBB,
MBBI, NextMBBI,
TII,
true);
693 case LoongArch::PseudoLA_TLS_DESC:
694 return expandLoadAddressTLSDesc(
MBB,
MBBI, NextMBBI,
TII);
695 case LoongArch::PseudoLA_TLS_DESC_LARGE:
696 return expandLoadAddressTLSDesc(
MBB,
MBBI, NextMBBI,
TII,
true);
697 case LoongArch::PseudoCALL_SMALL:
698 case LoongArch::PseudoCALL_LARGE:
699 return expandFunctionCALLPreRA(
MBB,
MBBI, NextMBBI,
false,
701 case LoongArch::PseudoTAIL_SMALL:
702 case LoongArch::PseudoTAIL_LARGE:
703 return expandFunctionCALLPreRA(
MBB,
MBBI, NextMBBI,
true,
705 case LoongArch::PseudoMOVCF2GRViaFR:
706 return expandMOVCF2GRViaFR(
MBB,
MBBI, NextMBBI,
TII);
707 case LoongArch::PseudoBRIND:
740bool LoongArchPreRAExpandPseudoLegacy::runOnMachineFunction(
742 return expandPseudosPreRAInMF(MF);
760char LoongArchExpandPseudoLegacy::ID = 0;
783 MF->
insert(++
MBB.getIterator(), FalseBB);
784 MF->
insert(++FalseBB->getIterator(), SinkBB);
795 BuildMI(FalseBB,
DL,
TII->get(LoongArch::SET_CFR_TRUE), DestReg);
797 FalseBB->addSuccessor(SinkBB);
799 SinkBB->splice(SinkBB->end(), &
MBB,
MI,
MBB.end());
800 SinkBB->transferSuccessors(&
MBB);
802 MBB.addSuccessor(FalseBB);
803 MBB.addSuccessor(SinkBB);
805 NextMBBI =
MBB.end();
806 MI.eraseFromParent();
846 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
847 Register ScratchReg = IsTailCall ? LoongArch::R20 : LoongArch::R1;
849 unsigned PC = Is64Bit ? LoongArch::PCADDU18I : LoongArch::PCADDU12I;
860 MI.setDesc(
TII->get(Opcode));
861 MI.getOperand(0).ChangeToRegister(ScratchReg,
false);
877 bool DstIsDead =
MBBI->getOperand(0).isDead();
880 int64_t
Hi =
MBBI->getOperand(2).getImm();
881 bool IsLA64 = STI.is64Bit();
893 MBBI->eraseFromParent();
900 switch (
MBBI->getOpcode()) {
901 case LoongArch::PseudoCopyCFR:
903 case LoongArch::PseudoCALL_MEDIUM:
905 case LoongArch::PseudoTAIL_MEDIUM:
907 case LoongArch::PseudoAddUpperImm:
938bool LoongArchExpandPseudoLegacy::runOnMachineFunction(
MachineFunction &MF) {
958 "loongarch-pre-ra-expand-pseudo",
967 return new LoongArchPreRAExpandPseudoLegacy();
970 return new LoongArchExpandPseudoLegacy();
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
cl::opt< bool > LArchAnnotateTableJump("loongarch-annotate-tablejump", cl::Hidden, cl::desc("Annotate table jump instruction to correlate it with the jump table."), cl::init(false))
static bool expandMBB(MachineBasicBlock &MBB, const LoongArchInstrInfo *TII)
static bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const LoongArchInstrInfo *TII)
static bool expandPseudo(MachineFunction &MF)
#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME
#define LOONGARCH_EXPAND_PSEUDO_NAME
static bool expandAddUpperImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const LoongArchInstrInfo *TII)
static bool expandFunctionCALL(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, bool IsTailCall, const LoongArchInstrInfo *TII)
static bool expandCopyCFR(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const LoongArchInstrInfo *TII)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
static bool is64Bit(const char *name)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM Basic Block Representation.
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LoongArchMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private Lo...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addDisp(const MachineOperand &Disp, int64_t off, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
LLVM_ABI void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
filtered_mop_range all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
LLVM_ABI void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
static MachineOperand CreateImm(int64_t Val)
@ MO_ExternalSymbol
Name of external global symbol.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Represent a constant reference to a string, i.e.
CodeModel::Model getCodeModel() const
Returns the code model.
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned encodeFlags(unsigned Flags, bool Relax)
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createLoongArchExpandPseudoLegacyPass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
@ Define
Register definition.
@ Renamable
Register that may be renamed.
FunctionPass * createLoongArchPreRAExpandPseudoLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
constexpr RegState getDeadRegState(bool B)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr RegState getRenamableRegState(bool B)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().