37class X86DynAllocaExpander {
39 bool run(MachineFunction &MF);
43 enum Lowering { TouchAndSub, Sub, Probe };
46 typedef MapVector<MachineInstr*, Lowering> LoweringMap;
49 void computeLowerings(MachineFunction &MF, LoweringMap& Lowerings);
52 Lowering getLowering(int64_t CurrentOffset, int64_t AllocaAmount);
55 void lower(MachineInstr*
MI, Lowering L);
57 MachineRegisterInfo *MRI =
nullptr;
58 const X86Subtarget *STI =
nullptr;
59 const TargetInstrInfo *TII =
nullptr;
60 const X86RegisterInfo *TRI =
nullptr;
62 unsigned SlotSize = 0;
63 int64_t StackProbeSize = 0;
64 bool NoStackArgProbe =
false;
69 X86DynAllocaExpanderLegacy() : MachineFunctionPass(ID) {}
71 bool runOnMachineFunction(MachineFunction &MF)
override;
73 void getAnalysisUsage(AnalysisUsage &AU)
const override {
79 StringRef getPassName()
const override {
return "X86 DynAlloca Expander"; }
85char X86DynAllocaExpanderLegacy::ID = 0;
90 "X86 DynAlloca Expander",
false,
false)
93 return new X86DynAllocaExpanderLegacy();
98 assert(
MI->getOpcode() == X86::DYN_ALLOCA_32 ||
99 MI->getOpcode() == X86::DYN_ALLOCA_64);
102 Register AmountReg =
MI->getOperand(0).getReg();
106 (Def->getOpcode() != X86::MOV32ri && Def->getOpcode() != X86::MOV64ri) ||
107 !Def->getOperand(1).isImm())
110 return Def->getOperand(1).getImm();
113X86DynAllocaExpander::Lowering
114X86DynAllocaExpander::getLowering(int64_t CurrentOffset,
115 int64_t AllocaAmount) {
117 if (AllocaAmount < 0 || AllocaAmount > StackProbeSize)
121 if (CurrentOffset + AllocaAmount <= StackProbeSize)
129 switch (
MI.getOpcode()) {
146void X86DynAllocaExpander::computeLowerings(MachineFunction &MF,
147 LoweringMap &Lowerings) {
153 DenseMap<MachineBasicBlock *, int64_t> OutOffset;
154 for (MachineBasicBlock &
MBB : MF)
155 OutOffset[&
MBB] = INT32_MAX;
162 ReversePostOrderTraversal<MachineFunction*> RPO(&MF);
164 for (MachineBasicBlock *
MBB : RPO) {
170 for (MachineInstr &
MI : *
MBB) {
171 if (
MI.getOpcode() == X86::DYN_ALLOCA_32 ||
172 MI.getOpcode() == X86::DYN_ALLOCA_64) {
191 }
else if (
MI.getOpcode() == X86::ADJCALLSTACKUP32 ||
192 MI.getOpcode() == X86::ADJCALLSTACKUP64) {
193 Offset -=
MI.getOperand(0).getImm();
194 }
else if (
MI.getOpcode() == X86::ADJCALLSTACKDOWN32 ||
195 MI.getOpcode() == X86::ADJCALLSTACKDOWN64) {
196 Offset +=
MI.getOperand(0).getImm();
197 }
else if (
MI.modifiesRegister(StackPtr,
TRI)) {
209 return X86::SUB64ri32;
213void X86DynAllocaExpander::lower(MachineInstr *
MI,
Lowering L) {
215 MachineBasicBlock *
MBB =
MI->getParent();
220 MI->eraseFromParent();
226 bool Is64Bit = STI->is64Bit();
227 bool Is64BitAlloca =
MI->getOpcode() == X86::DYN_ALLOCA_64;
228 assert(SlotSize == 4 || SlotSize == 8);
230 std::optional<MachineFunction::DebugInstrOperandPair> InstrNum;
231 if (
unsigned Num =
MI->peekDebugInstrNum()) {
238 assert(Amount >= SlotSize);
241 unsigned RegA = Is64Bit ? X86::RAX : X86::EAX;
243 .
addReg(RegA, RegState::Undef);
253 if (Amount == SlotSize) {
255 unsigned RegA = Is64Bit ? X86::RAX : X86::EAX;
257 .
addReg(RegA, RegState::Undef);
266 if (!NoStackArgProbe) {
268 unsigned RegA = Is64BitAlloca ? X86::RAX : X86::EAX;
270 .
addReg(
MI->getOperand(0).getReg());
278 TII->get(Is64BitAlloca ? X86::SUB64rr : X86::SUB32rr), StackPtr)
280 .
addReg(
MI->getOperand(0).getReg());
285 Register AmountReg =
MI->getOperand(0).getReg();
286 MI->eraseFromParent();
291 AmountDef->eraseFromParent();
294bool X86DynAllocaExpander::run(MachineFunction &MF) {
295 if (!MF.
getInfo<X86MachineFunctionInfo>()->hasDynAlloca())
302 StackPtr =
TRI->getStackRegister();
303 SlotSize =
TRI->getSlotSize();
309 LoweringMap Lowerings;
310 computeLowerings(MF, Lowerings);
311 for (
auto &
P : Lowerings)
312 lower(
P.first,
P.second);
317bool X86DynAllocaExpanderLegacy::runOnMachineFunction(MachineFunction &MF) {
318 return X86DynAllocaExpander().run(MF);
324 bool Changed = X86DynAllocaExpander().run(MF);
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)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represents analyses that only rely on functions' control flow.
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...
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.
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 & 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.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool use_empty(Register RegNo) const
use_empty - Return true if there are no instructions using the specified register.
LLVM_ABI MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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
Pass manager infrastructure for declaring and invalidating analyses.
DXILDebugInfoMap run(Module &M)
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.
FunctionPass * createX86DynAllocaExpanderLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
@ Sub
Subtraction of integers.