34#define AARCH64_LOWER_HOMOGENEOUS_PROLOG_EPILOG_NAME \
35 "AArch64 homogeneous prolog/epilog lowering pass"
39 cl::desc(
"The minimum number of instructions that are outlined in a frame "
40 "helper (default = 2)"));
44class AArch64LowerHomogeneousPE {
74class AArch64LowerHomogeneousPrologEpilog :
public ModulePass {
97char AArch64LowerHomogeneousPrologEpilog::ID = 0;
100 "aarch64-lower-homogeneous-prolog-epilog",
103bool AArch64LowerHomogeneousPrologEpilog::runOnModule(
Module &M) {
108 &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
109 return AArch64LowerHomogeneousPE(&M, MMI).run();
112bool AArch64LowerHomogeneousPE::run() {
113 bool Changed =
false;
121 Changed |= runOnMachineFunction(*MF);
133 std::ostringstream RegStream;
136 RegStream <<
"OUTLINED_FUNCTION_PROLOG_";
139 RegStream <<
"OUTLINED_FUNCTION_PROLOG_FRAME" << FpOffset <<
"_";
142 RegStream <<
"OUTLINED_FUNCTION_EPILOG_";
145 RegStream <<
"OUTLINED_FUNCTION_EPILOG_TAIL_";
149 for (
auto Reg : Regs)
152 return RegStream.str();
162 assert(
F ==
nullptr &&
"Function has been created before");
164 Function::ExternalLinkage,
Name, M);
165 assert(
F &&
"Function was null!");
169 F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
173 F->addFnAttr(Attribute::OptimizeNone);
174 F->addFnAttr(Attribute::NoInline);
175 F->addFnAttr(Attribute::MinSize);
176 F->addFnAttr(Attribute::Naked);
201 int Offset,
bool IsPreDec) {
202 bool IsFloat = AArch64::FPR64RegClass.contains(Reg1);
206 Opc = IsFloat ? AArch64::STPDpre : AArch64::STPXpre;
208 Opc = IsFloat ? AArch64::STPDi : AArch64::STPXi;
224 int Offset,
bool IsPostDec) {
225 bool IsFloat = AArch64::FPR64RegClass.contains(Reg1);
229 Opc = IsFloat ? AArch64::LDPDpost : AArch64::LDPXpost;
231 Opc = IsFloat ? AArch64::LDPDi : AArch64::LDPXi;
276 unsigned FpOffset = 0) {
279 auto *
F = M->getFunction(
Name);
293 auto LRIdx = std::distance(Regs.
begin(),
llvm::find(Regs, AArch64::LR));
297 if (LRIdx !=
Size - 2) {
300 LRIdx -
Size + 2,
true);
304 for (
int I =
Size - 3;
I >= 0;
I -= 2) {
306 if (Regs[
I - 1] == AArch64::LR)
333 for (
int I = 0;
I <
Size - 2;
I += 2)
345 return M->getFunction(
Name);
360 auto RegCount = Regs.
size();
361 assert(RegCount > 0 && (RegCount % 2 == 0));
363 int InstCount = RegCount / 2;
381 for (
auto NextMI = NextMBBI; NextMI !=
MBB.
end(); NextMI++) {
382 if (NextMI->readsRegister(AArch64::W16,
TRI))
387 if (SuccMBB->isLiveIn(AArch64::W16) || SuccMBB->isLiveIn(AArch64::X16))
394 if (NextMBBI ==
MBB.
end())
396 if (NextMBBI->getOpcode() != AArch64::RET_ReallyLR)
428bool AArch64LowerHomogeneousPE::lowerEpilog(
436 for (
auto &MO :
MI.operands())
444 assert(
MI.getOpcode() == AArch64::HOM_Epilog);
449 auto *EpilogTailHelper =
457 NextMBBI = std::next(Return);
458 Return->removeFromParent();
470 for (
int I = 0;
I <
Size - 2;
I += 2)
502bool AArch64LowerHomogeneousPE::lowerProlog(
511 std::optional<int> FpOffset;
512 for (
auto &MO :
MI.operands()) {
514 if (MO.getReg() == AArch64::LR)
517 }
else if (MO.isImm()) {
518 FpOffset = MO.getImm();
526 assert(
MI.getOpcode() == AArch64::HOM_Prolog);
553 for (
int I =
Size - 3;
I >= 0;
I -= 2)
578 unsigned Opcode =
MI.getOpcode();
582 case AArch64::HOM_Prolog:
583 return lowerProlog(
MBB,
MBBI, NextMBBI);
584 case AArch64::HOM_Epilog:
585 return lowerEpilog(
MBB,
MBBI, NextMBBI);
603bool AArch64LowerHomogeneousPE::runOnMachineFunction(
MachineFunction &MF) {
613 return new AArch64LowerHomogeneousPrologEpilog();
static Function * getOrCreateFrameHelper(Module *M, MachineModuleInfo *MMI, SmallVectorImpl< unsigned > &Regs, FrameHelperType Type, unsigned FpOffset=0)
Return a unique function if a helper can be formed with the given Regs and frame type.
static bool shouldUseFrameHelper(MachineBasicBlock &MBB, MachineBasicBlock::iterator &NextMBBI, SmallVectorImpl< unsigned > &Regs, FrameHelperType Type)
This function checks if a frame helper should be used for HOM_Prolog/HOM_Epilog pseudo instruction ex...
static void emitLoad(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator Pos, const TargetInstrInfo &TII, unsigned Reg1, unsigned Reg2, int Offset, bool IsPostDec)
Emit a load-pair instruction for frame-destroy.
#define AARCH64_LOWER_HOMOGENEOUS_PROLOG_EPILOG_NAME
cl::opt< int > FrameHelperSizeThreshold("frame-helper-size-threshold", cl::init(2), cl::Hidden, cl::desc("The minimum number of instructions that are outlined in a frame " "helper (default = 2)"))
static std::string getFrameHelperName(SmallVectorImpl< unsigned > &Regs, FrameHelperType Type, unsigned FpOffset)
Return a frame helper name with the given CSRs and the helper type.
static void emitStore(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator Pos, const TargetInstrInfo &TII, unsigned Reg1, unsigned Reg2, int Offset, bool IsPreDec)
Emit a store-pair instruction for frame-setup.
static MachineFunction & createFrameHelperMachineFunction(Module *M, MachineModuleInfo *MMI, StringRef Name)
Create a Function for the unique frame helper with the given name.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
static const char * getRegisterName(MCRegister Reg, unsigned AltIdx=AArch64::NoRegAltName)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This is an important class for using LLVM in a threaded context.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
MachineBasicBlock * removeFromParent()
This method unlinks 'this' from the containing function, and returns it, but does not delete it.
MachineFunctionProperties & set(Property P)
MachineFunctionProperties & reset(Property P)
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineFunctionProperties & getProperties() const
Get the function properties.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const
Copy all the implicit operands from OtherMI onto this one.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
This class contains meta information specific to a module.
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
void freezeReservedRegs(const MachineFunction &)
freezeReservedRegs - Called by the register allocator to freeze the set of reserved registers before ...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
TargetInstrInfo - Interface to description of machine instruction set.
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
The instances of the Type class are immutable: once they are created, they are never changed.
static Type * getVoidTy(LLVMContext &C)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Define
Register definition.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void initializeAArch64LowerHomogeneousPrologEpilogPass(PassRegistry &)
unsigned getDefRegState(bool B)
ModulePass * createAArch64LowerHomogeneousPrologEpilogPass()
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.