17330f729Sjoerg //===-- MachineFrameInfo.cpp ---------------------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg /// \file Implements MachineFrameInfo that manages the stack frame.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "llvm/CodeGen/MachineFrameInfo.h"
147330f729Sjoerg
157330f729Sjoerg #include "llvm/ADT/BitVector.h"
167330f729Sjoerg #include "llvm/CodeGen/MachineFunction.h"
177330f729Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
187330f729Sjoerg #include "llvm/CodeGen/TargetFrameLowering.h"
197330f729Sjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
207330f729Sjoerg #include "llvm/CodeGen/TargetRegisterInfo.h"
217330f729Sjoerg #include "llvm/CodeGen/TargetSubtargetInfo.h"
227330f729Sjoerg #include "llvm/Config/llvm-config.h"
237330f729Sjoerg #include "llvm/Support/Debug.h"
247330f729Sjoerg #include "llvm/Support/raw_ostream.h"
257330f729Sjoerg #include <cassert>
267330f729Sjoerg
277330f729Sjoerg #define DEBUG_TYPE "codegen"
287330f729Sjoerg
297330f729Sjoerg using namespace llvm;
307330f729Sjoerg
ensureMaxAlignment(Align Alignment)317330f729Sjoerg void MachineFrameInfo::ensureMaxAlignment(Align Alignment) {
327330f729Sjoerg if (!StackRealignable)
337330f729Sjoerg assert(Alignment <= StackAlignment &&
347330f729Sjoerg "For targets without stack realignment, Alignment is out of limit!");
357330f729Sjoerg if (MaxAlignment < Alignment)
367330f729Sjoerg MaxAlignment = Alignment;
377330f729Sjoerg }
387330f729Sjoerg
397330f729Sjoerg /// Clamp the alignment if requested and emit a warning.
clampStackAlignment(bool ShouldClamp,Align Alignment,Align StackAlignment)407330f729Sjoerg static inline Align clampStackAlignment(bool ShouldClamp, Align Alignment,
417330f729Sjoerg Align StackAlignment) {
427330f729Sjoerg if (!ShouldClamp || Alignment <= StackAlignment)
437330f729Sjoerg return Alignment;
44*82d56013Sjoerg LLVM_DEBUG(dbgs() << "Warning: requested alignment " << DebugStr(Alignment)
45*82d56013Sjoerg << " exceeds the stack alignment "
46*82d56013Sjoerg << DebugStr(StackAlignment)
477330f729Sjoerg << " when stack realignment is off" << '\n');
487330f729Sjoerg return StackAlignment;
497330f729Sjoerg }
507330f729Sjoerg
CreateStackObject(uint64_t Size,Align Alignment,bool IsSpillSlot,const AllocaInst * Alloca,uint8_t StackID)517330f729Sjoerg int MachineFrameInfo::CreateStackObject(uint64_t Size, Align Alignment,
527330f729Sjoerg bool IsSpillSlot,
537330f729Sjoerg const AllocaInst *Alloca,
547330f729Sjoerg uint8_t StackID) {
557330f729Sjoerg assert(Size != 0 && "Cannot allocate zero size stack objects!");
567330f729Sjoerg Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
577330f729Sjoerg Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
587330f729Sjoerg !IsSpillSlot, StackID));
597330f729Sjoerg int Index = (int)Objects.size() - NumFixedObjects - 1;
607330f729Sjoerg assert(Index >= 0 && "Bad frame index!");
617330f729Sjoerg if (StackID == 0)
627330f729Sjoerg ensureMaxAlignment(Alignment);
637330f729Sjoerg return Index;
647330f729Sjoerg }
657330f729Sjoerg
CreateSpillStackObject(uint64_t Size,Align Alignment)667330f729Sjoerg int MachineFrameInfo::CreateSpillStackObject(uint64_t Size, Align Alignment) {
677330f729Sjoerg Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
687330f729Sjoerg CreateStackObject(Size, Alignment, true);
697330f729Sjoerg int Index = (int)Objects.size() - NumFixedObjects - 1;
707330f729Sjoerg ensureMaxAlignment(Alignment);
717330f729Sjoerg return Index;
727330f729Sjoerg }
737330f729Sjoerg
CreateVariableSizedObject(Align Alignment,const AllocaInst * Alloca)747330f729Sjoerg int MachineFrameInfo::CreateVariableSizedObject(Align Alignment,
757330f729Sjoerg const AllocaInst *Alloca) {
767330f729Sjoerg HasVarSizedObjects = true;
777330f729Sjoerg Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
787330f729Sjoerg Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
797330f729Sjoerg ensureMaxAlignment(Alignment);
807330f729Sjoerg return (int)Objects.size()-NumFixedObjects-1;
817330f729Sjoerg }
827330f729Sjoerg
CreateFixedObject(uint64_t Size,int64_t SPOffset,bool IsImmutable,bool IsAliased)837330f729Sjoerg int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
847330f729Sjoerg bool IsImmutable, bool IsAliased) {
857330f729Sjoerg assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
867330f729Sjoerg // The alignment of the frame index can be determined from its offset from
877330f729Sjoerg // the incoming frame position. If the frame object is at offset 32 and
887330f729Sjoerg // the stack is guaranteed to be 16-byte aligned, then we know that the
897330f729Sjoerg // object is 16-byte aligned. Note that unlike the non-fixed case, if the
907330f729Sjoerg // stack needs realignment, we can't assume that the stack will in fact be
917330f729Sjoerg // aligned.
927330f729Sjoerg Align Alignment =
93*82d56013Sjoerg commonAlignment(ForcedRealign ? Align(1) : StackAlignment, SPOffset);
947330f729Sjoerg Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
957330f729Sjoerg Objects.insert(Objects.begin(),
967330f729Sjoerg StackObject(Size, Alignment, SPOffset, IsImmutable,
977330f729Sjoerg /*IsSpillSlot=*/false, /*Alloca=*/nullptr,
987330f729Sjoerg IsAliased));
997330f729Sjoerg return -++NumFixedObjects;
1007330f729Sjoerg }
1017330f729Sjoerg
CreateFixedSpillStackObject(uint64_t Size,int64_t SPOffset,bool IsImmutable)1027330f729Sjoerg int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
1037330f729Sjoerg int64_t SPOffset,
1047330f729Sjoerg bool IsImmutable) {
1057330f729Sjoerg Align Alignment =
106*82d56013Sjoerg commonAlignment(ForcedRealign ? Align(1) : StackAlignment, SPOffset);
1077330f729Sjoerg Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
1087330f729Sjoerg Objects.insert(Objects.begin(),
1097330f729Sjoerg StackObject(Size, Alignment, SPOffset, IsImmutable,
1107330f729Sjoerg /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
1117330f729Sjoerg /*IsAliased=*/false));
1127330f729Sjoerg return -++NumFixedObjects;
1137330f729Sjoerg }
1147330f729Sjoerg
getPristineRegs(const MachineFunction & MF) const1157330f729Sjoerg BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
1167330f729Sjoerg const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1177330f729Sjoerg BitVector BV(TRI->getNumRegs());
1187330f729Sjoerg
1197330f729Sjoerg // Before CSI is calculated, no registers are considered pristine. They can be
1207330f729Sjoerg // freely used and PEI will make sure they are saved.
1217330f729Sjoerg if (!isCalleeSavedInfoValid())
1227330f729Sjoerg return BV;
1237330f729Sjoerg
1247330f729Sjoerg const MachineRegisterInfo &MRI = MF.getRegInfo();
1257330f729Sjoerg for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
1267330f729Sjoerg ++CSR)
1277330f729Sjoerg BV.set(*CSR);
1287330f729Sjoerg
1297330f729Sjoerg // Saved CSRs are not pristine.
1307330f729Sjoerg for (auto &I : getCalleeSavedInfo())
1317330f729Sjoerg for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
1327330f729Sjoerg BV.reset(*S);
1337330f729Sjoerg
1347330f729Sjoerg return BV;
1357330f729Sjoerg }
1367330f729Sjoerg
estimateStackSize(const MachineFunction & MF) const137*82d56013Sjoerg uint64_t MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
1387330f729Sjoerg const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
1397330f729Sjoerg const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
140*82d56013Sjoerg Align MaxAlign = getMaxAlign();
141*82d56013Sjoerg int64_t Offset = 0;
1427330f729Sjoerg
1437330f729Sjoerg // This code is very, very similar to PEI::calculateFrameObjectOffsets().
1447330f729Sjoerg // It really should be refactored to share code. Until then, changes
1457330f729Sjoerg // should keep in mind that there's tight coupling between the two.
1467330f729Sjoerg
1477330f729Sjoerg for (int i = getObjectIndexBegin(); i != 0; ++i) {
1487330f729Sjoerg // Only estimate stack size of default stack.
1497330f729Sjoerg if (getStackID(i) != TargetStackID::Default)
1507330f729Sjoerg continue;
151*82d56013Sjoerg int64_t FixedOff = -getObjectOffset(i);
1527330f729Sjoerg if (FixedOff > Offset) Offset = FixedOff;
1537330f729Sjoerg }
1547330f729Sjoerg for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
1557330f729Sjoerg // Only estimate stack size of live objects on default stack.
1567330f729Sjoerg if (isDeadObjectIndex(i) || getStackID(i) != TargetStackID::Default)
1577330f729Sjoerg continue;
1587330f729Sjoerg Offset += getObjectSize(i);
159*82d56013Sjoerg Align Alignment = getObjectAlign(i);
1607330f729Sjoerg // Adjust to alignment boundary
161*82d56013Sjoerg Offset = alignTo(Offset, Alignment);
1627330f729Sjoerg
163*82d56013Sjoerg MaxAlign = std::max(Alignment, MaxAlign);
1647330f729Sjoerg }
1657330f729Sjoerg
1667330f729Sjoerg if (adjustsStack() && TFI->hasReservedCallFrame(MF))
1677330f729Sjoerg Offset += getMaxCallFrameSize();
1687330f729Sjoerg
1697330f729Sjoerg // Round up the size to a multiple of the alignment. If the function has
1707330f729Sjoerg // any calls or alloca's, align to the target's StackAlignment value to
1717330f729Sjoerg // ensure that the callee's frame or the alloca data is suitably aligned;
1727330f729Sjoerg // otherwise, for leaf functions, align to the TransientStackAlignment
1737330f729Sjoerg // value.
174*82d56013Sjoerg Align StackAlign;
1757330f729Sjoerg if (adjustsStack() || hasVarSizedObjects() ||
176*82d56013Sjoerg (RegInfo->hasStackRealignment(MF) && getObjectIndexEnd() != 0))
177*82d56013Sjoerg StackAlign = TFI->getStackAlign();
1787330f729Sjoerg else
179*82d56013Sjoerg StackAlign = TFI->getTransientStackAlign();
1807330f729Sjoerg
1817330f729Sjoerg // If the frame pointer is eliminated, all frame offsets will be relative to
1827330f729Sjoerg // SP not FP. Align to MaxAlign so this works.
1837330f729Sjoerg StackAlign = std::max(StackAlign, MaxAlign);
184*82d56013Sjoerg return alignTo(Offset, StackAlign);
1857330f729Sjoerg }
1867330f729Sjoerg
computeMaxCallFrameSize(const MachineFunction & MF)1877330f729Sjoerg void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
1887330f729Sjoerg const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1897330f729Sjoerg unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
1907330f729Sjoerg unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
1917330f729Sjoerg assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
1927330f729Sjoerg "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
1937330f729Sjoerg
1947330f729Sjoerg MaxCallFrameSize = 0;
1957330f729Sjoerg for (const MachineBasicBlock &MBB : MF) {
1967330f729Sjoerg for (const MachineInstr &MI : MBB) {
1977330f729Sjoerg unsigned Opcode = MI.getOpcode();
1987330f729Sjoerg if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
1997330f729Sjoerg unsigned Size = TII.getFrameSize(MI);
2007330f729Sjoerg MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
2017330f729Sjoerg AdjustsStack = true;
2027330f729Sjoerg } else if (MI.isInlineAsm()) {
2037330f729Sjoerg // Some inline asm's need a stack frame, as indicated by operand 1.
2047330f729Sjoerg unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
2057330f729Sjoerg if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
2067330f729Sjoerg AdjustsStack = true;
2077330f729Sjoerg }
2087330f729Sjoerg }
2097330f729Sjoerg }
2107330f729Sjoerg }
2117330f729Sjoerg
print(const MachineFunction & MF,raw_ostream & OS) const2127330f729Sjoerg void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
2137330f729Sjoerg if (Objects.empty()) return;
2147330f729Sjoerg
2157330f729Sjoerg const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
2167330f729Sjoerg int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
2177330f729Sjoerg
2187330f729Sjoerg OS << "Frame Objects:\n";
2197330f729Sjoerg
2207330f729Sjoerg for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
2217330f729Sjoerg const StackObject &SO = Objects[i];
2227330f729Sjoerg OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
2237330f729Sjoerg
2247330f729Sjoerg if (SO.StackID != 0)
2257330f729Sjoerg OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
2267330f729Sjoerg
2277330f729Sjoerg if (SO.Size == ~0ULL) {
2287330f729Sjoerg OS << "dead\n";
2297330f729Sjoerg continue;
2307330f729Sjoerg }
2317330f729Sjoerg if (SO.Size == 0)
2327330f729Sjoerg OS << "variable sized";
2337330f729Sjoerg else
2347330f729Sjoerg OS << "size=" << SO.Size;
2357330f729Sjoerg OS << ", align=" << SO.Alignment.value();
2367330f729Sjoerg
2377330f729Sjoerg if (i < NumFixedObjects)
2387330f729Sjoerg OS << ", fixed";
2397330f729Sjoerg if (i < NumFixedObjects || SO.SPOffset != -1) {
2407330f729Sjoerg int64_t Off = SO.SPOffset - ValOffset;
2417330f729Sjoerg OS << ", at location [SP";
2427330f729Sjoerg if (Off > 0)
2437330f729Sjoerg OS << "+" << Off;
2447330f729Sjoerg else if (Off < 0)
2457330f729Sjoerg OS << Off;
2467330f729Sjoerg OS << "]";
2477330f729Sjoerg }
2487330f729Sjoerg OS << "\n";
2497330f729Sjoerg }
2507330f729Sjoerg }
2517330f729Sjoerg
2527330f729Sjoerg #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(const MachineFunction & MF) const2537330f729Sjoerg LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
2547330f729Sjoerg print(MF, dbgs());
2557330f729Sjoerg }
2567330f729Sjoerg #endif
257