32#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME \
33 "LoongArch Pre-RA pseudo instruction expansion pass"
34#define LOONGARCH_EXPAND_PSEUDO_NAME \
35 "LoongArch pseudo instruction expansion pass"
65 unsigned FlagsHi,
unsigned SecondOpcode,
70 unsigned LastOpcode,
unsigned IdentifyingMO);
74 unsigned LastOpcode,
unsigned IdentifyingMO,
76 bool EraseFromParent);
112char LoongArchPreRAExpandPseudo::ID = 0;
114bool LoongArchPreRAExpandPseudo::runOnMachineFunction(
MachineFunction &MF) {
136bool LoongArchPreRAExpandPseudo::expandMI(
139 switch (
MBBI->getOpcode()) {
140 case LoongArch::PseudoLA_PCREL:
141 return expandLoadAddressPcrel(
MBB,
MBBI, NextMBBI);
142 case LoongArch::PseudoLA_PCREL_LARGE:
143 return expandLoadAddressPcrel(
MBB,
MBBI, NextMBBI,
true);
144 case LoongArch::PseudoLA_GOT:
145 return expandLoadAddressGot(
MBB,
MBBI, NextMBBI);
146 case LoongArch::PseudoLA_GOT_LARGE:
147 return expandLoadAddressGot(
MBB,
MBBI, NextMBBI,
true);
148 case LoongArch::PseudoLA_TLS_LE:
149 return expandLoadAddressTLSLE(
MBB,
MBBI, NextMBBI);
150 case LoongArch::PseudoLA_TLS_IE:
151 return expandLoadAddressTLSIE(
MBB,
MBBI, NextMBBI);
152 case LoongArch::PseudoLA_TLS_IE_LARGE:
153 return expandLoadAddressTLSIE(
MBB,
MBBI, NextMBBI,
true);
154 case LoongArch::PseudoLA_TLS_LD:
155 return expandLoadAddressTLSLD(
MBB,
MBBI, NextMBBI);
156 case LoongArch::PseudoLA_TLS_LD_LARGE:
157 return expandLoadAddressTLSLD(
MBB,
MBBI, NextMBBI,
true);
158 case LoongArch::PseudoLA_TLS_GD:
159 return expandLoadAddressTLSGD(
MBB,
MBBI, NextMBBI);
160 case LoongArch::PseudoLA_TLS_GD_LARGE:
161 return expandLoadAddressTLSGD(
MBB,
MBBI, NextMBBI,
true);
162 case LoongArch::PseudoLA_TLS_DESC:
163 return expandLoadAddressTLSDesc(
MBB,
MBBI, NextMBBI);
164 case LoongArch::PseudoLA_TLS_DESC_LARGE:
165 return expandLoadAddressTLSDesc(
MBB,
MBBI, NextMBBI,
true);
166 case LoongArch::PseudoCALL:
167 case LoongArch::PseudoCALL_LARGE:
168 return expandFunctionCALL(
MBB,
MBBI, NextMBBI,
false);
169 case LoongArch::PseudoTAIL:
170 case LoongArch::PseudoTAIL_LARGE:
171 return expandFunctionCALL(
MBB,
MBBI, NextMBBI,
true);
172 case LoongArch::PseudoBRIND:
182bool LoongArchPreRAExpandPseudo::expandPcalau12iInstPair(
185 unsigned SecondOpcode,
unsigned FlagsLo) {
203 if (
MI.hasOneMemOperand())
206 MI.eraseFromParent();
210bool LoongArchPreRAExpandPseudo::expandLargeAddressLoad(
213 unsigned IdentifyingMO) {
215 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LastOpcode, IdentifyingMO,
216 MI.getOperand(2),
MI.getOperand(0).getReg(),
220bool LoongArchPreRAExpandPseudo::expandLargeAddressLoad(
224 bool EraseFromParent) {
233 unsigned MO0, MO1, MO2, MO3;
234 switch (IdentifyingMO) {
265 "Large code model requires LA64");
295 const char *SymName = Symbol.getSymbolName();
296 Part0.addExternalSymbol(SymName, MO0);
297 Part1.addExternalSymbol(SymName, MO1);
298 Part2.addExternalSymbol(SymName, MO2);
299 Part3.addExternalSymbol(SymName, MO3);
301 Part0.addDisp(Symbol, 0, MO0);
302 Part1.addDisp(Symbol, 0, MO1);
303 Part2.addDisp(Symbol, 0, MO2);
304 Part3.addDisp(Symbol, 0, MO3);
308 MI.eraseFromParent();
313bool LoongArchPreRAExpandPseudo::expandLoadAddressPcrel(
319 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::ADD_D,
327 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
332bool LoongArchPreRAExpandPseudo::expandLoadAddressGot(
338 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::LDX_D,
346 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
351bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSLE(
395 MI.eraseFromParent();
399bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSIE(
405 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::LDX_D,
413 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
418bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSLD(
424 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::ADD_D,
432 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
437bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSGD(
443 return expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LoongArch::ADD_D,
451 unsigned SecondOpcode = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
456bool LoongArchPreRAExpandPseudo::expandLoadAddressTLSDesc(
464 unsigned ADD = STI.is64Bit() ? LoongArch::ADD_D : LoongArch::ADD_W;
465 unsigned ADDI = STI.is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
466 unsigned LD = STI.is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
488 "Large code model requires LA64");
529 MI.eraseFromParent();
533bool LoongArchPreRAExpandPseudo::expandFunctionCALL(
552 Opcode = IsTailCall ? LoongArch::PseudoB_TAIL : LoongArch::BL;
561 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
569 unsigned LAOpcode = UseGOT ? LoongArch::LDX_D : LoongArch::ADD_D;
570 expandLargeAddressLoad(
MBB,
MBBI, NextMBBI, LAOpcode, MO, Func, AddrReg,
578 CALL.copyImplicitOps(
MI);
581 CALL.setMIFlags(
MI.getFlags());
583 MI.eraseFromParent();
587void LoongArchPreRAExpandPseudo::annotateTableJump(
592 bool IsFound =
false;
598 for (
auto &MO : MInst->
all_uses()) {
602 if (!Reg.isVirtual())
610 MBBI->setPreInstrSymbol(
617 FindJTIMI(
DefMI, --FindDepth);
622 FindJTIMI(&*
MBBI, 3);
652char LoongArchExpandPseudo::ID = 0;
681 switch (
MBBI->getOpcode()) {
682 case LoongArch::PseudoCopyCFR:
683 return expandCopyCFR(
MBB,
MBBI, NextMBBI);
684 case LoongArch::PseudoCALL_MEDIUM:
685 return expandFunctionCALL(
MBB,
MBBI, NextMBBI,
false);
686 case LoongArch::PseudoTAIL_MEDIUM:
687 return expandFunctionCALL(
MBB,
MBBI, NextMBBI,
true);
693bool LoongArchExpandPseudo::expandCopyCFR(
714 MF->
insert(++FalseBB->getIterator(), SinkBB);
725 BuildMI(FalseBB,
DL,
TII->get(LoongArch::SET_CFR_TRUE), DestReg);
727 FalseBB->addSuccessor(SinkBB);
729 SinkBB->splice(SinkBB->end(), &
MBB,
MI,
MBB.
end());
730 SinkBB->transferSuccessors(&
MBB);
736 MI.eraseFromParent();
746bool LoongArchExpandPseudo::expandFunctionCALL(
768 IsTailCall ? LoongArch::PseudoJIRL_TAIL : LoongArch::PseudoJIRL_CALL;
769 Register ScratchReg = IsTailCall ? LoongArch::R20 : LoongArch::R1;
785 CALL.copyImplicitOps(
MI);
788 CALL.setMIFlags(
MI.getFlags());
790 MI.eraseFromParent();
805 return new LoongArchPreRAExpandPseudo();
808 return new LoongArchExpandPseudo();
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static Expected< BitVector > expand(StringRef S, StringRef Original)
const HexagonInstrInfo * TII
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define LOONGARCH_PRERA_EXPAND_PSEUDO_NAME
cl::opt< bool > LArchAnnotateTableJump
#define LOONGARCH_EXPAND_PSEUDO_NAME
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM Basic Block Representation.
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...
LoongArchMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private Lo...
MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
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 & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addDisp(const MachineOperand &Disp, int64_t off, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
iterator_range< filtered_mop_iterator > all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
unsigned getNumOperands() const
Retuns the total number of operands.
const MachineOperand & getOperand(unsigned i) const
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.
@ MO_ExternalSymbol
Name of external global symbol.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
StringRef - Represent a constant reference to a string, i.e.
CodeModel::Model getCodeModel() const
Returns the code model.
virtual const TargetInstrInfo * getInstrInfo() const
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ Kill
The last use of a register.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void initializeLoongArchPreRAExpandPseudoPass(PassRegistry &)
void initializeLoongArchExpandPseudoPass(PassRegistry &)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
FunctionPass * createLoongArchPreRAExpandPseudoPass()
FunctionPass * createLoongArchExpandPseudoPass()
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().