1*349cc55cSDimitry Andric //===----- X86DynAllocaExpander.cpp - Expand DynAlloca pseudo instruction -===// 2*349cc55cSDimitry Andric // 3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*349cc55cSDimitry Andric // 7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8*349cc55cSDimitry Andric // 9*349cc55cSDimitry Andric // This file defines a pass that expands DynAlloca pseudo-instructions. 10*349cc55cSDimitry Andric // 11*349cc55cSDimitry Andric // It performs a conservative analysis to determine whether each allocation 12*349cc55cSDimitry Andric // falls within a region of the stack that is safe to use, or whether stack 13*349cc55cSDimitry Andric // probes must be emitted. 14*349cc55cSDimitry Andric // 15*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 16*349cc55cSDimitry Andric 17*349cc55cSDimitry Andric #include "X86.h" 18*349cc55cSDimitry Andric #include "X86InstrBuilder.h" 19*349cc55cSDimitry Andric #include "X86InstrInfo.h" 20*349cc55cSDimitry Andric #include "X86MachineFunctionInfo.h" 21*349cc55cSDimitry Andric #include "X86Subtarget.h" 22*349cc55cSDimitry Andric #include "llvm/ADT/MapVector.h" 23*349cc55cSDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 24*349cc55cSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 25*349cc55cSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 26*349cc55cSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 27*349cc55cSDimitry Andric #include "llvm/CodeGen/Passes.h" 28*349cc55cSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 29*349cc55cSDimitry Andric #include "llvm/IR/Function.h" 30*349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h" 31*349cc55cSDimitry Andric 32*349cc55cSDimitry Andric using namespace llvm; 33*349cc55cSDimitry Andric 34*349cc55cSDimitry Andric namespace { 35*349cc55cSDimitry Andric 36*349cc55cSDimitry Andric class X86DynAllocaExpander : public MachineFunctionPass { 37*349cc55cSDimitry Andric public: 38*349cc55cSDimitry Andric X86DynAllocaExpander() : MachineFunctionPass(ID) {} 39*349cc55cSDimitry Andric 40*349cc55cSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 41*349cc55cSDimitry Andric 42*349cc55cSDimitry Andric private: 43*349cc55cSDimitry Andric /// Strategies for lowering a DynAlloca. 44*349cc55cSDimitry Andric enum Lowering { TouchAndSub, Sub, Probe }; 45*349cc55cSDimitry Andric 46*349cc55cSDimitry Andric /// Deterministic-order map from DynAlloca instruction to desired lowering. 47*349cc55cSDimitry Andric typedef MapVector<MachineInstr*, Lowering> LoweringMap; 48*349cc55cSDimitry Andric 49*349cc55cSDimitry Andric /// Compute which lowering to use for each DynAlloca instruction. 50*349cc55cSDimitry Andric void computeLowerings(MachineFunction &MF, LoweringMap& Lowerings); 51*349cc55cSDimitry Andric 52*349cc55cSDimitry Andric /// Get the appropriate lowering based on current offset and amount. 53*349cc55cSDimitry Andric Lowering getLowering(int64_t CurrentOffset, int64_t AllocaAmount); 54*349cc55cSDimitry Andric 55*349cc55cSDimitry Andric /// Lower a DynAlloca instruction. 56*349cc55cSDimitry Andric void lower(MachineInstr* MI, Lowering L); 57*349cc55cSDimitry Andric 58*349cc55cSDimitry Andric MachineRegisterInfo *MRI = nullptr; 59*349cc55cSDimitry Andric const X86Subtarget *STI = nullptr; 60*349cc55cSDimitry Andric const TargetInstrInfo *TII = nullptr; 61*349cc55cSDimitry Andric const X86RegisterInfo *TRI = nullptr; 62*349cc55cSDimitry Andric unsigned StackPtr = 0; 63*349cc55cSDimitry Andric unsigned SlotSize = 0; 64*349cc55cSDimitry Andric int64_t StackProbeSize = 0; 65*349cc55cSDimitry Andric bool NoStackArgProbe = false; 66*349cc55cSDimitry Andric 67*349cc55cSDimitry Andric StringRef getPassName() const override { return "X86 DynAlloca Expander"; } 68*349cc55cSDimitry Andric static char ID; 69*349cc55cSDimitry Andric }; 70*349cc55cSDimitry Andric 71*349cc55cSDimitry Andric char X86DynAllocaExpander::ID = 0; 72*349cc55cSDimitry Andric 73*349cc55cSDimitry Andric } // end anonymous namespace 74*349cc55cSDimitry Andric 75*349cc55cSDimitry Andric FunctionPass *llvm::createX86DynAllocaExpander() { 76*349cc55cSDimitry Andric return new X86DynAllocaExpander(); 77*349cc55cSDimitry Andric } 78*349cc55cSDimitry Andric 79*349cc55cSDimitry Andric /// Return the allocation amount for a DynAlloca instruction, or -1 if unknown. 80*349cc55cSDimitry Andric static int64_t getDynAllocaAmount(MachineInstr *MI, MachineRegisterInfo *MRI) { 81*349cc55cSDimitry Andric assert(MI->getOpcode() == X86::DYN_ALLOCA_32 || 82*349cc55cSDimitry Andric MI->getOpcode() == X86::DYN_ALLOCA_64); 83*349cc55cSDimitry Andric assert(MI->getOperand(0).isReg()); 84*349cc55cSDimitry Andric 85*349cc55cSDimitry Andric Register AmountReg = MI->getOperand(0).getReg(); 86*349cc55cSDimitry Andric MachineInstr *Def = MRI->getUniqueVRegDef(AmountReg); 87*349cc55cSDimitry Andric 88*349cc55cSDimitry Andric if (!Def || 89*349cc55cSDimitry Andric (Def->getOpcode() != X86::MOV32ri && Def->getOpcode() != X86::MOV64ri) || 90*349cc55cSDimitry Andric !Def->getOperand(1).isImm()) 91*349cc55cSDimitry Andric return -1; 92*349cc55cSDimitry Andric 93*349cc55cSDimitry Andric return Def->getOperand(1).getImm(); 94*349cc55cSDimitry Andric } 95*349cc55cSDimitry Andric 96*349cc55cSDimitry Andric X86DynAllocaExpander::Lowering 97*349cc55cSDimitry Andric X86DynAllocaExpander::getLowering(int64_t CurrentOffset, 98*349cc55cSDimitry Andric int64_t AllocaAmount) { 99*349cc55cSDimitry Andric // For a non-constant amount or a large amount, we have to probe. 100*349cc55cSDimitry Andric if (AllocaAmount < 0 || AllocaAmount > StackProbeSize) 101*349cc55cSDimitry Andric return Probe; 102*349cc55cSDimitry Andric 103*349cc55cSDimitry Andric // If it fits within the safe region of the stack, just subtract. 104*349cc55cSDimitry Andric if (CurrentOffset + AllocaAmount <= StackProbeSize) 105*349cc55cSDimitry Andric return Sub; 106*349cc55cSDimitry Andric 107*349cc55cSDimitry Andric // Otherwise, touch the current tip of the stack, then subtract. 108*349cc55cSDimitry Andric return TouchAndSub; 109*349cc55cSDimitry Andric } 110*349cc55cSDimitry Andric 111*349cc55cSDimitry Andric static bool isPushPop(const MachineInstr &MI) { 112*349cc55cSDimitry Andric switch (MI.getOpcode()) { 113*349cc55cSDimitry Andric case X86::PUSH32i8: 114*349cc55cSDimitry Andric case X86::PUSH32r: 115*349cc55cSDimitry Andric case X86::PUSH32rmm: 116*349cc55cSDimitry Andric case X86::PUSH32rmr: 117*349cc55cSDimitry Andric case X86::PUSHi32: 118*349cc55cSDimitry Andric case X86::PUSH64i8: 119*349cc55cSDimitry Andric case X86::PUSH64r: 120*349cc55cSDimitry Andric case X86::PUSH64rmm: 121*349cc55cSDimitry Andric case X86::PUSH64rmr: 122*349cc55cSDimitry Andric case X86::PUSH64i32: 123*349cc55cSDimitry Andric case X86::POP32r: 124*349cc55cSDimitry Andric case X86::POP64r: 125*349cc55cSDimitry Andric return true; 126*349cc55cSDimitry Andric default: 127*349cc55cSDimitry Andric return false; 128*349cc55cSDimitry Andric } 129*349cc55cSDimitry Andric } 130*349cc55cSDimitry Andric 131*349cc55cSDimitry Andric void X86DynAllocaExpander::computeLowerings(MachineFunction &MF, 132*349cc55cSDimitry Andric LoweringMap &Lowerings) { 133*349cc55cSDimitry Andric // Do a one-pass reverse post-order walk of the CFG to conservatively estimate 134*349cc55cSDimitry Andric // the offset between the stack pointer and the lowest touched part of the 135*349cc55cSDimitry Andric // stack, and use that to decide how to lower each DynAlloca instruction. 136*349cc55cSDimitry Andric 137*349cc55cSDimitry Andric // Initialize OutOffset[B], the stack offset at exit from B, to something big. 138*349cc55cSDimitry Andric DenseMap<MachineBasicBlock *, int64_t> OutOffset; 139*349cc55cSDimitry Andric for (MachineBasicBlock &MBB : MF) 140*349cc55cSDimitry Andric OutOffset[&MBB] = INT32_MAX; 141*349cc55cSDimitry Andric 142*349cc55cSDimitry Andric // Note: we don't know the offset at the start of the entry block since the 143*349cc55cSDimitry Andric // prologue hasn't been inserted yet, and how much that will adjust the stack 144*349cc55cSDimitry Andric // pointer depends on register spills, which have not been computed yet. 145*349cc55cSDimitry Andric 146*349cc55cSDimitry Andric // Compute the reverse post-order. 147*349cc55cSDimitry Andric ReversePostOrderTraversal<MachineFunction*> RPO(&MF); 148*349cc55cSDimitry Andric 149*349cc55cSDimitry Andric for (MachineBasicBlock *MBB : RPO) { 150*349cc55cSDimitry Andric int64_t Offset = -1; 151*349cc55cSDimitry Andric for (MachineBasicBlock *Pred : MBB->predecessors()) 152*349cc55cSDimitry Andric Offset = std::max(Offset, OutOffset[Pred]); 153*349cc55cSDimitry Andric if (Offset == -1) Offset = INT32_MAX; 154*349cc55cSDimitry Andric 155*349cc55cSDimitry Andric for (MachineInstr &MI : *MBB) { 156*349cc55cSDimitry Andric if (MI.getOpcode() == X86::DYN_ALLOCA_32 || 157*349cc55cSDimitry Andric MI.getOpcode() == X86::DYN_ALLOCA_64) { 158*349cc55cSDimitry Andric // A DynAlloca moves StackPtr, and potentially touches it. 159*349cc55cSDimitry Andric int64_t Amount = getDynAllocaAmount(&MI, MRI); 160*349cc55cSDimitry Andric Lowering L = getLowering(Offset, Amount); 161*349cc55cSDimitry Andric Lowerings[&MI] = L; 162*349cc55cSDimitry Andric switch (L) { 163*349cc55cSDimitry Andric case Sub: 164*349cc55cSDimitry Andric Offset += Amount; 165*349cc55cSDimitry Andric break; 166*349cc55cSDimitry Andric case TouchAndSub: 167*349cc55cSDimitry Andric Offset = Amount; 168*349cc55cSDimitry Andric break; 169*349cc55cSDimitry Andric case Probe: 170*349cc55cSDimitry Andric Offset = 0; 171*349cc55cSDimitry Andric break; 172*349cc55cSDimitry Andric } 173*349cc55cSDimitry Andric } else if (MI.isCall() || isPushPop(MI)) { 174*349cc55cSDimitry Andric // Calls, pushes and pops touch the tip of the stack. 175*349cc55cSDimitry Andric Offset = 0; 176*349cc55cSDimitry Andric } else if (MI.getOpcode() == X86::ADJCALLSTACKUP32 || 177*349cc55cSDimitry Andric MI.getOpcode() == X86::ADJCALLSTACKUP64) { 178*349cc55cSDimitry Andric Offset -= MI.getOperand(0).getImm(); 179*349cc55cSDimitry Andric } else if (MI.getOpcode() == X86::ADJCALLSTACKDOWN32 || 180*349cc55cSDimitry Andric MI.getOpcode() == X86::ADJCALLSTACKDOWN64) { 181*349cc55cSDimitry Andric Offset += MI.getOperand(0).getImm(); 182*349cc55cSDimitry Andric } else if (MI.modifiesRegister(StackPtr, TRI)) { 183*349cc55cSDimitry Andric // Any other modification of SP means we've lost track of it. 184*349cc55cSDimitry Andric Offset = INT32_MAX; 185*349cc55cSDimitry Andric } 186*349cc55cSDimitry Andric } 187*349cc55cSDimitry Andric 188*349cc55cSDimitry Andric OutOffset[MBB] = Offset; 189*349cc55cSDimitry Andric } 190*349cc55cSDimitry Andric } 191*349cc55cSDimitry Andric 192*349cc55cSDimitry Andric static unsigned getSubOpcode(bool Is64Bit, int64_t Amount) { 193*349cc55cSDimitry Andric if (Is64Bit) 194*349cc55cSDimitry Andric return isInt<8>(Amount) ? X86::SUB64ri8 : X86::SUB64ri32; 195*349cc55cSDimitry Andric return isInt<8>(Amount) ? X86::SUB32ri8 : X86::SUB32ri; 196*349cc55cSDimitry Andric } 197*349cc55cSDimitry Andric 198*349cc55cSDimitry Andric void X86DynAllocaExpander::lower(MachineInstr *MI, Lowering L) { 199*349cc55cSDimitry Andric const DebugLoc &DL = MI->getDebugLoc(); 200*349cc55cSDimitry Andric MachineBasicBlock *MBB = MI->getParent(); 201*349cc55cSDimitry Andric MachineBasicBlock::iterator I = *MI; 202*349cc55cSDimitry Andric 203*349cc55cSDimitry Andric int64_t Amount = getDynAllocaAmount(MI, MRI); 204*349cc55cSDimitry Andric if (Amount == 0) { 205*349cc55cSDimitry Andric MI->eraseFromParent(); 206*349cc55cSDimitry Andric return; 207*349cc55cSDimitry Andric } 208*349cc55cSDimitry Andric 209*349cc55cSDimitry Andric // These two variables differ on x32, which is a 64-bit target with a 210*349cc55cSDimitry Andric // 32-bit alloca. 211*349cc55cSDimitry Andric bool Is64Bit = STI->is64Bit(); 212*349cc55cSDimitry Andric bool Is64BitAlloca = MI->getOpcode() == X86::DYN_ALLOCA_64; 213*349cc55cSDimitry Andric assert(SlotSize == 4 || SlotSize == 8); 214*349cc55cSDimitry Andric 215*349cc55cSDimitry Andric switch (L) { 216*349cc55cSDimitry Andric case TouchAndSub: { 217*349cc55cSDimitry Andric assert(Amount >= SlotSize); 218*349cc55cSDimitry Andric 219*349cc55cSDimitry Andric // Use a push to touch the top of the stack. 220*349cc55cSDimitry Andric unsigned RegA = Is64Bit ? X86::RAX : X86::EAX; 221*349cc55cSDimitry Andric BuildMI(*MBB, I, DL, TII->get(Is64Bit ? X86::PUSH64r : X86::PUSH32r)) 222*349cc55cSDimitry Andric .addReg(RegA, RegState::Undef); 223*349cc55cSDimitry Andric Amount -= SlotSize; 224*349cc55cSDimitry Andric if (!Amount) 225*349cc55cSDimitry Andric break; 226*349cc55cSDimitry Andric 227*349cc55cSDimitry Andric // Fall through to make any remaining adjustment. 228*349cc55cSDimitry Andric LLVM_FALLTHROUGH; 229*349cc55cSDimitry Andric } 230*349cc55cSDimitry Andric case Sub: 231*349cc55cSDimitry Andric assert(Amount > 0); 232*349cc55cSDimitry Andric if (Amount == SlotSize) { 233*349cc55cSDimitry Andric // Use push to save size. 234*349cc55cSDimitry Andric unsigned RegA = Is64Bit ? X86::RAX : X86::EAX; 235*349cc55cSDimitry Andric BuildMI(*MBB, I, DL, TII->get(Is64Bit ? X86::PUSH64r : X86::PUSH32r)) 236*349cc55cSDimitry Andric .addReg(RegA, RegState::Undef); 237*349cc55cSDimitry Andric } else { 238*349cc55cSDimitry Andric // Sub. 239*349cc55cSDimitry Andric BuildMI(*MBB, I, DL, 240*349cc55cSDimitry Andric TII->get(getSubOpcode(Is64BitAlloca, Amount)), StackPtr) 241*349cc55cSDimitry Andric .addReg(StackPtr) 242*349cc55cSDimitry Andric .addImm(Amount); 243*349cc55cSDimitry Andric } 244*349cc55cSDimitry Andric break; 245*349cc55cSDimitry Andric case Probe: 246*349cc55cSDimitry Andric if (!NoStackArgProbe) { 247*349cc55cSDimitry Andric // The probe lowering expects the amount in RAX/EAX. 248*349cc55cSDimitry Andric unsigned RegA = Is64BitAlloca ? X86::RAX : X86::EAX; 249*349cc55cSDimitry Andric BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::COPY), RegA) 250*349cc55cSDimitry Andric .addReg(MI->getOperand(0).getReg()); 251*349cc55cSDimitry Andric 252*349cc55cSDimitry Andric // Do the probe. 253*349cc55cSDimitry Andric STI->getFrameLowering()->emitStackProbe(*MBB->getParent(), *MBB, MI, DL, 254*349cc55cSDimitry Andric /*InProlog=*/false); 255*349cc55cSDimitry Andric } else { 256*349cc55cSDimitry Andric // Sub 257*349cc55cSDimitry Andric BuildMI(*MBB, I, DL, 258*349cc55cSDimitry Andric TII->get(Is64BitAlloca ? X86::SUB64rr : X86::SUB32rr), StackPtr) 259*349cc55cSDimitry Andric .addReg(StackPtr) 260*349cc55cSDimitry Andric .addReg(MI->getOperand(0).getReg()); 261*349cc55cSDimitry Andric } 262*349cc55cSDimitry Andric break; 263*349cc55cSDimitry Andric } 264*349cc55cSDimitry Andric 265*349cc55cSDimitry Andric Register AmountReg = MI->getOperand(0).getReg(); 266*349cc55cSDimitry Andric MI->eraseFromParent(); 267*349cc55cSDimitry Andric 268*349cc55cSDimitry Andric // Delete the definition of AmountReg. 269*349cc55cSDimitry Andric if (MRI->use_empty(AmountReg)) 270*349cc55cSDimitry Andric if (MachineInstr *AmountDef = MRI->getUniqueVRegDef(AmountReg)) 271*349cc55cSDimitry Andric AmountDef->eraseFromParent(); 272*349cc55cSDimitry Andric } 273*349cc55cSDimitry Andric 274*349cc55cSDimitry Andric bool X86DynAllocaExpander::runOnMachineFunction(MachineFunction &MF) { 275*349cc55cSDimitry Andric if (!MF.getInfo<X86MachineFunctionInfo>()->hasDynAlloca()) 276*349cc55cSDimitry Andric return false; 277*349cc55cSDimitry Andric 278*349cc55cSDimitry Andric MRI = &MF.getRegInfo(); 279*349cc55cSDimitry Andric STI = &MF.getSubtarget<X86Subtarget>(); 280*349cc55cSDimitry Andric TII = STI->getInstrInfo(); 281*349cc55cSDimitry Andric TRI = STI->getRegisterInfo(); 282*349cc55cSDimitry Andric StackPtr = TRI->getStackRegister(); 283*349cc55cSDimitry Andric SlotSize = TRI->getSlotSize(); 284*349cc55cSDimitry Andric 285*349cc55cSDimitry Andric StackProbeSize = 4096; 286*349cc55cSDimitry Andric if (MF.getFunction().hasFnAttribute("stack-probe-size")) { 287*349cc55cSDimitry Andric MF.getFunction() 288*349cc55cSDimitry Andric .getFnAttribute("stack-probe-size") 289*349cc55cSDimitry Andric .getValueAsString() 290*349cc55cSDimitry Andric .getAsInteger(0, StackProbeSize); 291*349cc55cSDimitry Andric } 292*349cc55cSDimitry Andric NoStackArgProbe = MF.getFunction().hasFnAttribute("no-stack-arg-probe"); 293*349cc55cSDimitry Andric if (NoStackArgProbe) 294*349cc55cSDimitry Andric StackProbeSize = INT64_MAX; 295*349cc55cSDimitry Andric 296*349cc55cSDimitry Andric LoweringMap Lowerings; 297*349cc55cSDimitry Andric computeLowerings(MF, Lowerings); 298*349cc55cSDimitry Andric for (auto &P : Lowerings) 299*349cc55cSDimitry Andric lower(P.first, P.second); 300*349cc55cSDimitry Andric 301*349cc55cSDimitry Andric return true; 302*349cc55cSDimitry Andric } 303