10b57cec5SDimitry Andric //===- LocalStackSlotAllocation.cpp - Pre-allocate locals to stack slots --===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass assigns local frame indices to stack slots relative to one another 100b57cec5SDimitry Andric // and allocates additional base registers to access them when the target 110b57cec5SDimitry Andric // estimates they are likely to be out of range of stack pointer and frame 120b57cec5SDimitry Andric // pointer relative addressing. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 180b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 310b57cec5SDimitry Andric #include "llvm/Pass.h" 320b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 330b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 340b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 350b57cec5SDimitry Andric #include <algorithm> 360b57cec5SDimitry Andric #include <cassert> 370b57cec5SDimitry Andric #include <cstdint> 380b57cec5SDimitry Andric #include <tuple> 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric using namespace llvm; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric #define DEBUG_TYPE "localstackalloc" 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric STATISTIC(NumAllocations, "Number of frame indices allocated into local block"); 450b57cec5SDimitry Andric STATISTIC(NumBaseRegisters, "Number of virtual frame base registers allocated"); 460b57cec5SDimitry Andric STATISTIC(NumReplacements, "Number of frame indices references replaced"); 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric namespace { 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric class FrameRef { 510b57cec5SDimitry Andric MachineBasicBlock::iterator MI; // Instr referencing the frame 520b57cec5SDimitry Andric int64_t LocalOffset; // Local offset of the frame idx referenced 530b57cec5SDimitry Andric int FrameIdx; // The frame index 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Order reference instruction appears in program. Used to ensure 560b57cec5SDimitry Andric // deterministic order when multiple instructions may reference the same 570b57cec5SDimitry Andric // location. 580b57cec5SDimitry Andric unsigned Order; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric public: 610b57cec5SDimitry Andric FrameRef(MachineInstr *I, int64_t Offset, int Idx, unsigned Ord) : 620b57cec5SDimitry Andric MI(I), LocalOffset(Offset), FrameIdx(Idx), Order(Ord) {} 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric bool operator<(const FrameRef &RHS) const { 650b57cec5SDimitry Andric return std::tie(LocalOffset, FrameIdx, Order) < 660b57cec5SDimitry Andric std::tie(RHS.LocalOffset, RHS.FrameIdx, RHS.Order); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric MachineBasicBlock::iterator getMachineInstr() const { return MI; } 700b57cec5SDimitry Andric int64_t getLocalOffset() const { return LocalOffset; } 710b57cec5SDimitry Andric int getFrameIndex() const { return FrameIdx; } 720b57cec5SDimitry Andric }; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric class LocalStackSlotPass: public MachineFunctionPass { 750b57cec5SDimitry Andric SmallVector<int64_t, 16> LocalOffsets; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric /// StackObjSet - A set of stack object indexes 780b57cec5SDimitry Andric using StackObjSet = SmallSetVector<int, 8>; 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, int64_t &Offset, 810b57cec5SDimitry Andric bool StackGrowsDown, unsigned &MaxAlign); 820b57cec5SDimitry Andric void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, 830b57cec5SDimitry Andric SmallSet<int, 16> &ProtectedObjs, 840b57cec5SDimitry Andric MachineFrameInfo &MFI, bool StackGrowsDown, 850b57cec5SDimitry Andric int64_t &Offset, unsigned &MaxAlign); 860b57cec5SDimitry Andric void calculateFrameObjectOffsets(MachineFunction &Fn); 870b57cec5SDimitry Andric bool insertFrameReferenceRegisters(MachineFunction &Fn); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric public: 900b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric explicit LocalStackSlotPass() : MachineFunctionPass(ID) { 930b57cec5SDimitry Andric initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry()); 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 990b57cec5SDimitry Andric AU.setPreservesCFG(); 1000b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric }; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric } // end anonymous namespace 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric char LocalStackSlotPass::ID = 0; 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID; 1090b57cec5SDimitry Andric INITIALIZE_PASS(LocalStackSlotPass, DEBUG_TYPE, 1100b57cec5SDimitry Andric "Local Stack Slot Allocation", false, false) 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) { 1130b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 1140b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1150b57cec5SDimitry Andric unsigned LocalObjectCount = MFI.getObjectIndexEnd(); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric // If the target doesn't want/need this pass, or if there are no locals 1180b57cec5SDimitry Andric // to consider, early exit. 1190b57cec5SDimitry Andric if (!TRI->requiresVirtualBaseRegisters(MF) || LocalObjectCount == 0) 1200b57cec5SDimitry Andric return true; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric // Make sure we have enough space to store the local offsets. 1230b57cec5SDimitry Andric LocalOffsets.resize(MFI.getObjectIndexEnd()); 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric // Lay out the local blob. 1260b57cec5SDimitry Andric calculateFrameObjectOffsets(MF); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric // Insert virtual base registers to resolve frame index references. 1290b57cec5SDimitry Andric bool UsedBaseRegs = insertFrameReferenceRegisters(MF); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Tell MFI whether any base registers were allocated. PEI will only 1320b57cec5SDimitry Andric // want to use the local block allocations from this pass if there were any. 1330b57cec5SDimitry Andric // Otherwise, PEI can do a bit better job of getting the alignment right 1340b57cec5SDimitry Andric // without a hole at the start since it knows the alignment of the stack 1350b57cec5SDimitry Andric // at the start of local allocation, and this pass doesn't. 1360b57cec5SDimitry Andric MFI.setUseLocalStackAllocationBlock(UsedBaseRegs); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric return true; 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric /// AdjustStackOffset - Helper function used to adjust the stack frame offset. 1420b57cec5SDimitry Andric void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo &MFI, 1430b57cec5SDimitry Andric int FrameIdx, int64_t &Offset, 1440b57cec5SDimitry Andric bool StackGrowsDown, 1450b57cec5SDimitry Andric unsigned &MaxAlign) { 1460b57cec5SDimitry Andric // If the stack grows down, add the object size to find the lowest address. 1470b57cec5SDimitry Andric if (StackGrowsDown) 1480b57cec5SDimitry Andric Offset += MFI.getObjectSize(FrameIdx); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric unsigned Align = MFI.getObjectAlignment(FrameIdx); 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric // If the alignment of this object is greater than that of the stack, then 1530b57cec5SDimitry Andric // increase the stack alignment to match. 1540b57cec5SDimitry Andric MaxAlign = std::max(MaxAlign, Align); 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric // Adjust to alignment boundary. 1570b57cec5SDimitry Andric Offset = (Offset + Align - 1) / Align * Align; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric int64_t LocalOffset = StackGrowsDown ? -Offset : Offset; 1600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Allocate FI(" << FrameIdx << ") to local offset " 1610b57cec5SDimitry Andric << LocalOffset << "\n"); 1620b57cec5SDimitry Andric // Keep the offset available for base register allocation 1630b57cec5SDimitry Andric LocalOffsets[FrameIdx] = LocalOffset; 1640b57cec5SDimitry Andric // And tell MFI about it for PEI to use later 1650b57cec5SDimitry Andric MFI.mapLocalFrameObject(FrameIdx, LocalOffset); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric if (!StackGrowsDown) 1680b57cec5SDimitry Andric Offset += MFI.getObjectSize(FrameIdx); 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric ++NumAllocations; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e., 1740b57cec5SDimitry Andric /// those required to be close to the Stack Protector) to stack offsets. 1750b57cec5SDimitry Andric void LocalStackSlotPass::AssignProtectedObjSet(const StackObjSet &UnassignedObjs, 1760b57cec5SDimitry Andric SmallSet<int, 16> &ProtectedObjs, 1770b57cec5SDimitry Andric MachineFrameInfo &MFI, 1780b57cec5SDimitry Andric bool StackGrowsDown, int64_t &Offset, 1790b57cec5SDimitry Andric unsigned &MaxAlign) { 1800b57cec5SDimitry Andric for (StackObjSet::const_iterator I = UnassignedObjs.begin(), 1810b57cec5SDimitry Andric E = UnassignedObjs.end(); I != E; ++I) { 1820b57cec5SDimitry Andric int i = *I; 1830b57cec5SDimitry Andric AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign); 1840b57cec5SDimitry Andric ProtectedObjs.insert(i); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the 1890b57cec5SDimitry Andric /// abstract stack objects. 1900b57cec5SDimitry Andric void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) { 1910b57cec5SDimitry Andric // Loop over all of the stack objects, assigning sequential addresses... 1920b57cec5SDimitry Andric MachineFrameInfo &MFI = Fn.getFrameInfo(); 1930b57cec5SDimitry Andric const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); 1940b57cec5SDimitry Andric bool StackGrowsDown = 1950b57cec5SDimitry Andric TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 1960b57cec5SDimitry Andric int64_t Offset = 0; 1970b57cec5SDimitry Andric unsigned MaxAlign = 0; 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // Make sure that the stack protector comes before the local variables on the 2000b57cec5SDimitry Andric // stack. 2010b57cec5SDimitry Andric SmallSet<int, 16> ProtectedObjs; 2020b57cec5SDimitry Andric if (MFI.hasStackProtectorIndex()) { 2030b57cec5SDimitry Andric int StackProtectorFI = MFI.getStackProtectorIndex(); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // We need to make sure we didn't pre-allocate the stack protector when 2060b57cec5SDimitry Andric // doing this. 2070b57cec5SDimitry Andric // If we already have a stack protector, this will re-assign it to a slot 2080b57cec5SDimitry Andric // that is **not** covering the protected objects. 2090b57cec5SDimitry Andric assert(!MFI.isObjectPreAllocated(StackProtectorFI) && 2100b57cec5SDimitry Andric "Stack protector pre-allocated in LocalStackSlotAllocation"); 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric StackObjSet LargeArrayObjs; 2130b57cec5SDimitry Andric StackObjSet SmallArrayObjs; 2140b57cec5SDimitry Andric StackObjSet AddrOfObjs; 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric AdjustStackOffset(MFI, StackProtectorFI, Offset, StackGrowsDown, MaxAlign); 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric // Assign large stack objects first. 2190b57cec5SDimitry Andric for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { 2200b57cec5SDimitry Andric if (MFI.isDeadObjectIndex(i)) 2210b57cec5SDimitry Andric continue; 2220b57cec5SDimitry Andric if (StackProtectorFI == (int)i) 2230b57cec5SDimitry Andric continue; 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric switch (MFI.getObjectSSPLayout(i)) { 2260b57cec5SDimitry Andric case MachineFrameInfo::SSPLK_None: 2270b57cec5SDimitry Andric continue; 2280b57cec5SDimitry Andric case MachineFrameInfo::SSPLK_SmallArray: 2290b57cec5SDimitry Andric SmallArrayObjs.insert(i); 2300b57cec5SDimitry Andric continue; 2310b57cec5SDimitry Andric case MachineFrameInfo::SSPLK_AddrOf: 2320b57cec5SDimitry Andric AddrOfObjs.insert(i); 2330b57cec5SDimitry Andric continue; 2340b57cec5SDimitry Andric case MachineFrameInfo::SSPLK_LargeArray: 2350b57cec5SDimitry Andric LargeArrayObjs.insert(i); 2360b57cec5SDimitry Andric continue; 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric llvm_unreachable("Unexpected SSPLayoutKind."); 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown, 2420b57cec5SDimitry Andric Offset, MaxAlign); 2430b57cec5SDimitry Andric AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown, 2440b57cec5SDimitry Andric Offset, MaxAlign); 2450b57cec5SDimitry Andric AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown, 2460b57cec5SDimitry Andric Offset, MaxAlign); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric // Then assign frame offsets to stack objects that are not used to spill 2500b57cec5SDimitry Andric // callee saved registers. 2510b57cec5SDimitry Andric for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { 2520b57cec5SDimitry Andric if (MFI.isDeadObjectIndex(i)) 2530b57cec5SDimitry Andric continue; 2540b57cec5SDimitry Andric if (MFI.getStackProtectorIndex() == (int)i) 2550b57cec5SDimitry Andric continue; 2560b57cec5SDimitry Andric if (ProtectedObjs.count(i)) 2570b57cec5SDimitry Andric continue; 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric AdjustStackOffset(MFI, i, Offset, StackGrowsDown, MaxAlign); 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric // Remember how big this blob of stack space is 2630b57cec5SDimitry Andric MFI.setLocalFrameSize(Offset); 264*8bcb0991SDimitry Andric MFI.setLocalFrameMaxAlign(assumeAligned(MaxAlign)); 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric static inline bool 2680b57cec5SDimitry Andric lookupCandidateBaseReg(unsigned BaseReg, 2690b57cec5SDimitry Andric int64_t BaseOffset, 2700b57cec5SDimitry Andric int64_t FrameSizeAdjust, 2710b57cec5SDimitry Andric int64_t LocalFrameOffset, 2720b57cec5SDimitry Andric const MachineInstr &MI, 2730b57cec5SDimitry Andric const TargetRegisterInfo *TRI) { 2740b57cec5SDimitry Andric // Check if the relative offset from the where the base register references 2750b57cec5SDimitry Andric // to the target address is in range for the instruction. 2760b57cec5SDimitry Andric int64_t Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset; 2770b57cec5SDimitry Andric return TRI->isFrameOffsetLegal(&MI, BaseReg, Offset); 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { 2810b57cec5SDimitry Andric // Scan the function's instructions looking for frame index references. 2820b57cec5SDimitry Andric // For each, ask the target if it wants a virtual base register for it 2830b57cec5SDimitry Andric // based on what we can tell it about where the local will end up in the 2840b57cec5SDimitry Andric // stack frame. If it wants one, re-use a suitable one we've previously 2850b57cec5SDimitry Andric // allocated, or if there isn't one that fits the bill, allocate a new one 2860b57cec5SDimitry Andric // and ask the target to create a defining instruction for it. 2870b57cec5SDimitry Andric bool UsedBaseReg = false; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric MachineFrameInfo &MFI = Fn.getFrameInfo(); 2900b57cec5SDimitry Andric const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo(); 2910b57cec5SDimitry Andric const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); 2920b57cec5SDimitry Andric bool StackGrowsDown = 2930b57cec5SDimitry Andric TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric // Collect all of the instructions in the block that reference 2960b57cec5SDimitry Andric // a frame index. Also store the frame index referenced to ease later 2970b57cec5SDimitry Andric // lookup. (For any insn that has more than one FI reference, we arbitrarily 2980b57cec5SDimitry Andric // choose the first one). 2990b57cec5SDimitry Andric SmallVector<FrameRef, 64> FrameReferenceInsns; 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric unsigned Order = 0; 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric for (MachineBasicBlock &BB : Fn) { 3040b57cec5SDimitry Andric for (MachineInstr &MI : BB) { 3050b57cec5SDimitry Andric // Debug value, stackmap and patchpoint instructions can't be out of 3060b57cec5SDimitry Andric // range, so they don't need any updates. 3070b57cec5SDimitry Andric if (MI.isDebugInstr() || MI.getOpcode() == TargetOpcode::STATEPOINT || 3080b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::STACKMAP || 3090b57cec5SDimitry Andric MI.getOpcode() == TargetOpcode::PATCHPOINT) 3100b57cec5SDimitry Andric continue; 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric // For now, allocate the base register(s) within the basic block 3130b57cec5SDimitry Andric // where they're used, and don't try to keep them around outside 3140b57cec5SDimitry Andric // of that. It may be beneficial to try sharing them more broadly 3150b57cec5SDimitry Andric // than that, but the increased register pressure makes that a 3160b57cec5SDimitry Andric // tricky thing to balance. Investigate if re-materializing these 3170b57cec5SDimitry Andric // becomes an issue. 3180b57cec5SDimitry Andric for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 3190b57cec5SDimitry Andric // Consider replacing all frame index operands that reference 3200b57cec5SDimitry Andric // an object allocated in the local block. 3210b57cec5SDimitry Andric if (MI.getOperand(i).isFI()) { 3220b57cec5SDimitry Andric // Don't try this with values not in the local block. 3230b57cec5SDimitry Andric if (!MFI.isObjectPreAllocated(MI.getOperand(i).getIndex())) 3240b57cec5SDimitry Andric break; 3250b57cec5SDimitry Andric int Idx = MI.getOperand(i).getIndex(); 3260b57cec5SDimitry Andric int64_t LocalOffset = LocalOffsets[Idx]; 3270b57cec5SDimitry Andric if (!TRI->needsFrameBaseReg(&MI, LocalOffset)) 3280b57cec5SDimitry Andric break; 3290b57cec5SDimitry Andric FrameReferenceInsns.push_back(FrameRef(&MI, LocalOffset, Idx, Order++)); 3300b57cec5SDimitry Andric break; 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // Sort the frame references by local offset. 3370b57cec5SDimitry Andric // Use frame index as a tie-breaker in case MI's have the same offset. 3380b57cec5SDimitry Andric llvm::sort(FrameReferenceInsns); 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric MachineBasicBlock *Entry = &Fn.front(); 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric unsigned BaseReg = 0; 3430b57cec5SDimitry Andric int64_t BaseOffset = 0; 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric // Loop through the frame references and allocate for them as necessary. 3460b57cec5SDimitry Andric for (int ref = 0, e = FrameReferenceInsns.size(); ref < e ; ++ref) { 3470b57cec5SDimitry Andric FrameRef &FR = FrameReferenceInsns[ref]; 3480b57cec5SDimitry Andric MachineInstr &MI = *FR.getMachineInstr(); 3490b57cec5SDimitry Andric int64_t LocalOffset = FR.getLocalOffset(); 3500b57cec5SDimitry Andric int FrameIdx = FR.getFrameIndex(); 3510b57cec5SDimitry Andric assert(MFI.isObjectPreAllocated(FrameIdx) && 3520b57cec5SDimitry Andric "Only pre-allocated locals expected!"); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // We need to keep the references to the stack protector slot through frame 3550b57cec5SDimitry Andric // index operands so that it gets resolved by PEI rather than this pass. 3560b57cec5SDimitry Andric // This avoids accesses to the stack protector though virtual base 3570b57cec5SDimitry Andric // registers, and forces PEI to address it using fp/sp/bp. 3580b57cec5SDimitry Andric if (MFI.hasStackProtectorIndex() && 3590b57cec5SDimitry Andric FrameIdx == MFI.getStackProtectorIndex()) 3600b57cec5SDimitry Andric continue; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Considering: " << MI); 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric unsigned idx = 0; 3650b57cec5SDimitry Andric for (unsigned f = MI.getNumOperands(); idx != f; ++idx) { 3660b57cec5SDimitry Andric if (!MI.getOperand(idx).isFI()) 3670b57cec5SDimitry Andric continue; 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric if (FrameIdx == MI.getOperand(idx).getIndex()) 3700b57cec5SDimitry Andric break; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric assert(idx < MI.getNumOperands() && "Cannot find FI operand"); 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric int64_t Offset = 0; 3760b57cec5SDimitry Andric int64_t FrameSizeAdjust = StackGrowsDown ? MFI.getLocalFrameSize() : 0; 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Replacing FI in: " << MI); 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric // If we have a suitable base register available, use it; otherwise 3810b57cec5SDimitry Andric // create a new one. Note that any offset encoded in the 3820b57cec5SDimitry Andric // instruction itself will be taken into account by the target, 3830b57cec5SDimitry Andric // so we don't have to adjust for it here when reusing a base 3840b57cec5SDimitry Andric // register. 3850b57cec5SDimitry Andric if (UsedBaseReg && 3860b57cec5SDimitry Andric lookupCandidateBaseReg(BaseReg, BaseOffset, FrameSizeAdjust, 3870b57cec5SDimitry Andric LocalOffset, MI, TRI)) { 3880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Reusing base register " << BaseReg << "\n"); 3890b57cec5SDimitry Andric // We found a register to reuse. 3900b57cec5SDimitry Andric Offset = FrameSizeAdjust + LocalOffset - BaseOffset; 3910b57cec5SDimitry Andric } else { 3920b57cec5SDimitry Andric // No previously defined register was in range, so create a new one. 3930b57cec5SDimitry Andric int64_t InstrOffset = TRI->getFrameIndexInstrOffset(&MI, idx); 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric int64_t PrevBaseOffset = BaseOffset; 3960b57cec5SDimitry Andric BaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset; 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric // We'd like to avoid creating single-use virtual base registers. 3990b57cec5SDimitry Andric // Because the FrameRefs are in sorted order, and we've already 4000b57cec5SDimitry Andric // processed all FrameRefs before this one, just check whether or not 4010b57cec5SDimitry Andric // the next FrameRef will be able to reuse this new register. If not, 4020b57cec5SDimitry Andric // then don't bother creating it. 4030b57cec5SDimitry Andric if (ref + 1 >= e || 4040b57cec5SDimitry Andric !lookupCandidateBaseReg( 4050b57cec5SDimitry Andric BaseReg, BaseOffset, FrameSizeAdjust, 4060b57cec5SDimitry Andric FrameReferenceInsns[ref + 1].getLocalOffset(), 4070b57cec5SDimitry Andric *FrameReferenceInsns[ref + 1].getMachineInstr(), TRI)) { 4080b57cec5SDimitry Andric BaseOffset = PrevBaseOffset; 4090b57cec5SDimitry Andric continue; 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric const MachineFunction *MF = MI.getMF(); 4130b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getPointerRegClass(*MF); 4140b57cec5SDimitry Andric BaseReg = Fn.getRegInfo().createVirtualRegister(RC); 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Materializing base register " << BaseReg 4170b57cec5SDimitry Andric << " at frame local offset " 4180b57cec5SDimitry Andric << LocalOffset + InstrOffset << "\n"); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric // Tell the target to insert the instruction to initialize 4210b57cec5SDimitry Andric // the base register. 4220b57cec5SDimitry Andric // MachineBasicBlock::iterator InsertionPt = Entry->begin(); 4230b57cec5SDimitry Andric TRI->materializeFrameBaseRegister(Entry, BaseReg, FrameIdx, 4240b57cec5SDimitry Andric InstrOffset); 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric // The base register already includes any offset specified 4270b57cec5SDimitry Andric // by the instruction, so account for that so it doesn't get 4280b57cec5SDimitry Andric // applied twice. 4290b57cec5SDimitry Andric Offset = -InstrOffset; 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric ++NumBaseRegisters; 4320b57cec5SDimitry Andric UsedBaseReg = true; 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric assert(BaseReg != 0 && "Unable to allocate virtual base register!"); 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric // Modify the instruction to use the new base register rather 4370b57cec5SDimitry Andric // than the frame index operand. 4380b57cec5SDimitry Andric TRI->resolveFrameIndex(MI, BaseReg, Offset); 4390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Resolved: " << MI); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric ++NumReplacements; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric return UsedBaseReg; 4450b57cec5SDimitry Andric } 446