36 X86DynAllocaExpander() : MachineFunctionPass(ID) {}
38 bool runOnMachineFunction(MachineFunction &MF)
override;
42 enum Lowering { TouchAndSub, Sub, Probe };
45 typedef MapVector<MachineInstr*, Lowering> LoweringMap;
48 void computeLowerings(MachineFunction &MF, LoweringMap& Lowerings);
51 Lowering getLowering(int64_t CurrentOffset, int64_t AllocaAmount);
54 void lower(MachineInstr*
MI, Lowering L);
56 MachineRegisterInfo *MRI =
nullptr;
57 const X86Subtarget *STI =
nullptr;
58 const TargetInstrInfo *TII =
nullptr;
59 const X86RegisterInfo *TRI =
nullptr;
61 unsigned SlotSize = 0;
62 int64_t StackProbeSize = 0;
63 bool NoStackArgProbe =
false;
65 StringRef getPassName()
const override {
return "X86 DynAlloca Expander"; }
71char X86DynAllocaExpander::ID = 0;
76 "X86 DynAlloca Expander",
false,
false)
79 return new X86DynAllocaExpander();
84 assert(
MI->getOpcode() == X86::DYN_ALLOCA_32 ||
85 MI->getOpcode() == X86::DYN_ALLOCA_64);
88 Register AmountReg =
MI->getOperand(0).getReg();
92 (Def->getOpcode() != X86::MOV32ri && Def->getOpcode() != X86::MOV64ri) ||
93 !Def->getOperand(1).isImm())
96 return Def->getOperand(1).getImm();
99X86DynAllocaExpander::Lowering
100X86DynAllocaExpander::getLowering(int64_t CurrentOffset,
101 int64_t AllocaAmount) {
103 if (AllocaAmount < 0 || AllocaAmount > StackProbeSize)
107 if (CurrentOffset + AllocaAmount <= StackProbeSize)
115 switch (
MI.getOpcode()) {
132void X86DynAllocaExpander::computeLowerings(MachineFunction &MF,
133 LoweringMap &Lowerings) {
139 DenseMap<MachineBasicBlock *, int64_t> OutOffset;
140 for (MachineBasicBlock &
MBB : MF)
141 OutOffset[&
MBB] = INT32_MAX;
148 ReversePostOrderTraversal<MachineFunction*> RPO(&MF);
150 for (MachineBasicBlock *
MBB : RPO) {
156 for (MachineInstr &
MI : *
MBB) {
157 if (
MI.getOpcode() == X86::DYN_ALLOCA_32 ||
158 MI.getOpcode() == X86::DYN_ALLOCA_64) {
177 }
else if (
MI.getOpcode() == X86::ADJCALLSTACKUP32 ||
178 MI.getOpcode() == X86::ADJCALLSTACKUP64) {
179 Offset -=
MI.getOperand(0).getImm();
180 }
else if (
MI.getOpcode() == X86::ADJCALLSTACKDOWN32 ||
181 MI.getOpcode() == X86::ADJCALLSTACKDOWN64) {
182 Offset +=
MI.getOperand(0).getImm();
183 }
else if (
MI.modifiesRegister(StackPtr,
TRI)) {
195 return X86::SUB64ri32;
199void X86DynAllocaExpander::lower(MachineInstr *
MI,
Lowering L) {
201 MachineBasicBlock *
MBB =
MI->getParent();
206 MI->eraseFromParent();
212 bool Is64Bit = STI->is64Bit();
213 bool Is64BitAlloca =
MI->getOpcode() == X86::DYN_ALLOCA_64;
214 assert(SlotSize == 4 || SlotSize == 8);
216 std::optional<MachineFunction::DebugInstrOperandPair> InstrNum;
217 if (
unsigned Num =
MI->peekDebugInstrNum()) {
224 assert(Amount >= SlotSize);
227 unsigned RegA = Is64Bit ? X86::RAX : X86::EAX;
239 if (Amount == SlotSize) {
241 unsigned RegA = Is64Bit ? X86::RAX : X86::EAX;
252 if (!NoStackArgProbe) {
254 unsigned RegA = Is64BitAlloca ? X86::RAX : X86::EAX;
256 .
addReg(
MI->getOperand(0).getReg());
264 TII->get(Is64BitAlloca ? X86::SUB64rr : X86::SUB32rr), StackPtr)
266 .
addReg(
MI->getOperand(0).getReg());
271 Register AmountReg =
MI->getOperand(0).getReg();
272 MI->eraseFromParent();
275 if (
MRI->use_empty(AmountReg))
276 if (MachineInstr *AmountDef =
MRI->getUniqueVRegDef(AmountReg))
277 AmountDef->eraseFromParent();
280bool X86DynAllocaExpander::runOnMachineFunction(MachineFunction &MF) {
281 if (!MF.
getInfo<X86MachineFunctionInfo>()->hasDynAlloca())
288 StackPtr =
TRI->getStackRegister();
289 SlotSize =
TRI->getSlotSize();
295 LoweringMap Lowerings;
296 computeLowerings(MF, Lowerings);
297 for (
auto &
P : Lowerings)
298 lower(
P.first,
P.second);
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
pre isel intrinsic Pre ISel Intrinsic Lowering
static bool isPushPop(const MachineInstr &MI)
static int64_t getDynAllocaAmount(MachineInstr *MI, MachineRegisterInfo *MRI)
Return the allocation amount for a DynAlloca instruction, or -1 if unknown.
static unsigned getSubOpcode(bool Is64Bit)
FunctionPass class - This class is used to implement most global optimizations.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog, std::optional< MachineFunction::DebugInstrOperandPair > InstrNum=std::nullopt) const
Emit target stack probe code.
const X86TargetLowering * getTargetLowering() const override
const X86InstrInfo * getInstrInfo() const override
const X86RegisterInfo * getRegisterInfo() const override
const X86FrameLowering * getFrameLowering() const override
unsigned getStackProbeSize(const MachineFunction &MF) const
@ Undef
Value of the register doesn't matter.
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.
@ Sub
Subtraction of integers.
FunctionPass * createX86DynAllocaExpander()
Return a pass that expands DynAlloca pseudo-instructions.