2017-04-27 07:36:58 +08:00
|
|
|
//===-- MachineFrameInfo.cpp ---------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-04-27 07:36:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file Implements MachineFrameInfo that manages the stack frame.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-04-27 07:36:58 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "codegen"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-09-27 20:54:21 +08:00
|
|
|
void MachineFrameInfo::ensureMaxAlignment(Align Alignment) {
|
2017-04-27 07:36:58 +08:00
|
|
|
if (!StackRealignable)
|
2019-09-27 20:54:21 +08:00
|
|
|
assert(Alignment <= StackAlignment &&
|
|
|
|
"For targets without stack realignment, Alignment is out of limit!");
|
|
|
|
if (MaxAlignment < Alignment)
|
|
|
|
MaxAlignment = Alignment;
|
2017-04-27 07:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clamp the alignment if requested and emit a warning.
|
2019-09-27 20:54:21 +08:00
|
|
|
static inline Align clampStackAlignment(bool ShouldClamp, Align Alignment,
|
|
|
|
Align StackAlignment) {
|
|
|
|
if (!ShouldClamp || Alignment <= StackAlignment)
|
|
|
|
return Alignment;
|
2020-04-06 18:08:22 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Warning: requested alignment " << DebugStr(Alignment)
|
|
|
|
<< " exceeds the stack alignment "
|
|
|
|
<< DebugStr(StackAlignment)
|
2018-05-14 20:53:11 +08:00
|
|
|
<< " when stack realignment is off" << '\n');
|
2019-09-27 20:54:21 +08:00
|
|
|
return StackAlignment;
|
2017-04-27 07:36:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-27 20:54:21 +08:00
|
|
|
int MachineFrameInfo::CreateStackObject(uint64_t Size, Align Alignment,
|
2017-12-05 09:18:15 +08:00
|
|
|
bool IsSpillSlot,
|
|
|
|
const AllocaInst *Alloca,
|
|
|
|
uint8_t StackID) {
|
2017-04-27 07:36:58 +08:00
|
|
|
assert(Size != 0 && "Cannot allocate zero size stack objects!");
|
|
|
|
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
2017-12-05 09:18:15 +08:00
|
|
|
Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
|
|
|
|
!IsSpillSlot, StackID));
|
2017-04-27 07:36:58 +08:00
|
|
|
int Index = (int)Objects.size() - NumFixedObjects - 1;
|
|
|
|
assert(Index >= 0 && "Bad frame index!");
|
2019-04-02 17:46:52 +08:00
|
|
|
if (StackID == 0)
|
|
|
|
ensureMaxAlignment(Alignment);
|
2017-04-27 07:36:58 +08:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2019-09-27 20:54:21 +08:00
|
|
|
int MachineFrameInfo::CreateSpillStackObject(uint64_t Size, Align Alignment) {
|
2017-04-27 07:36:58 +08:00
|
|
|
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
|
|
CreateStackObject(Size, Alignment, true);
|
|
|
|
int Index = (int)Objects.size() - NumFixedObjects - 1;
|
|
|
|
ensureMaxAlignment(Alignment);
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2019-09-27 20:54:21 +08:00
|
|
|
int MachineFrameInfo::CreateVariableSizedObject(Align Alignment,
|
2017-04-27 07:36:58 +08:00
|
|
|
const AllocaInst *Alloca) {
|
|
|
|
HasVarSizedObjects = true;
|
|
|
|
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
|
|
Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
|
|
|
|
ensureMaxAlignment(Alignment);
|
|
|
|
return (int)Objects.size()-NumFixedObjects-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
|
2017-12-05 09:18:15 +08:00
|
|
|
bool IsImmutable, bool IsAliased) {
|
2017-04-27 07:36:58 +08:00
|
|
|
assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
|
|
|
|
// The alignment of the frame index can be determined from its offset from
|
|
|
|
// the incoming frame position. If the frame object is at offset 32 and
|
|
|
|
// the stack is guaranteed to be 16-byte aligned, then we know that the
|
|
|
|
// object is 16-byte aligned. Note that unlike the non-fixed case, if the
|
|
|
|
// stack needs realignment, we can't assume that the stack will in fact be
|
|
|
|
// aligned.
|
2019-09-27 20:54:21 +08:00
|
|
|
Align Alignment =
|
[Alignment][NFC] Deprecate Align::None()
Summary:
This is a follow up on https://reviews.llvm.org/D71473#inline-647262.
There's a caveat here that `Align(1)` relies on the compiler understanding of `Log2_64` implementation to produce good code. One could use `Align()` as a replacement but I believe it is less clear that the alignment is one in that case.
Reviewers: xbolva00, courbet, bollu
Subscribers: arsenm, dylanmckay, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, jsji, Jim, kerbowa, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D73099
2020-01-21 22:00:04 +08:00
|
|
|
commonAlignment(ForcedRealign ? Align(1) : StackAlignment, SPOffset);
|
2017-12-05 09:18:15 +08:00
|
|
|
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
|
|
Objects.insert(Objects.begin(),
|
|
|
|
StackObject(Size, Alignment, SPOffset, IsImmutable,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*IsSpillSlot=*/false, /*Alloca=*/nullptr,
|
2017-12-05 09:18:15 +08:00
|
|
|
IsAliased));
|
2017-04-27 07:36:58 +08:00
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
|
|
|
|
int64_t SPOffset,
|
2017-12-05 09:18:15 +08:00
|
|
|
bool IsImmutable) {
|
2019-09-27 20:54:21 +08:00
|
|
|
Align Alignment =
|
[Alignment][NFC] Deprecate Align::None()
Summary:
This is a follow up on https://reviews.llvm.org/D71473#inline-647262.
There's a caveat here that `Align(1)` relies on the compiler understanding of `Log2_64` implementation to produce good code. One could use `Align()` as a replacement but I believe it is less clear that the alignment is one in that case.
Reviewers: xbolva00, courbet, bollu
Subscribers: arsenm, dylanmckay, sdardis, nemanjai, jvesely, nhaehnle, hiraditya, kbarton, jrtc27, atanasyan, jsji, Jim, kerbowa, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D73099
2020-01-21 22:00:04 +08:00
|
|
|
commonAlignment(ForcedRealign ? Align(1) : StackAlignment, SPOffset);
|
2017-12-05 09:18:15 +08:00
|
|
|
Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
|
|
|
|
Objects.insert(Objects.begin(),
|
|
|
|
StackObject(Size, Alignment, SPOffset, IsImmutable,
|
|
|
|
/*IsSpillSlot=*/true, /*Alloca=*/nullptr,
|
|
|
|
/*IsAliased=*/false));
|
2017-04-27 07:36:58 +08:00
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
|
|
|
|
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
BitVector BV(TRI->getNumRegs());
|
|
|
|
|
|
|
|
// Before CSI is calculated, no registers are considered pristine. They can be
|
|
|
|
// freely used and PEI will make sure they are saved.
|
|
|
|
if (!isCalleeSavedInfoValid())
|
|
|
|
return BV;
|
|
|
|
|
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
|
|
|
|
++CSR)
|
|
|
|
BV.set(*CSR);
|
|
|
|
|
|
|
|
// Saved CSRs are not pristine.
|
|
|
|
for (auto &I : getCalleeSavedInfo())
|
|
|
|
for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
|
|
|
|
BV.reset(*S);
|
|
|
|
|
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
|
[AArch64] Fix issues with large arrays on stack
Summary:
This patch fixes a few issues when large arrays are allocated on the
stack. Currently, clang has inconsistent behaviour, for debug builds
there is an assertion failure when the array size on stack is around 2GB
but there is no assertion when the stack is around 8GB. For release
builds there is no assertion, the compilation succeeds but generates
incorrect code. The incorrect code generated is due to using
int/unsigned int instead of their 64-bit counterparts. This patch,
1) Removes the assertion in frame legality check.
2) Converts int/unsigned int in some places to the 64-bit variants. This
helps in generating correct code and removes the inconsistent behaviour.
3) Adds a test which runs without optimisations.
Reviewers: sdesmalen, efriedma, fhahn, aemerson
Reviewed By: efriedma
Subscribers: eli.friedman, fpetrogalli, kristof.beyls, hiraditya,
llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70496
2019-11-20 20:45:26 +08:00
|
|
|
uint64_t MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
|
2017-04-27 07:36:58 +08:00
|
|
|
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
2020-03-18 01:27:59 +08:00
|
|
|
Align MaxAlign = getMaxAlign();
|
[AArch64] Fix issues with large arrays on stack
Summary:
This patch fixes a few issues when large arrays are allocated on the
stack. Currently, clang has inconsistent behaviour, for debug builds
there is an assertion failure when the array size on stack is around 2GB
but there is no assertion when the stack is around 8GB. For release
builds there is no assertion, the compilation succeeds but generates
incorrect code. The incorrect code generated is due to using
int/unsigned int instead of their 64-bit counterparts. This patch,
1) Removes the assertion in frame legality check.
2) Converts int/unsigned int in some places to the 64-bit variants. This
helps in generating correct code and removes the inconsistent behaviour.
3) Adds a test which runs without optimisations.
Reviewers: sdesmalen, efriedma, fhahn, aemerson
Reviewed By: efriedma
Subscribers: eli.friedman, fpetrogalli, kristof.beyls, hiraditya,
llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70496
2019-11-20 20:45:26 +08:00
|
|
|
int64_t Offset = 0;
|
2017-04-27 07:36:58 +08:00
|
|
|
|
|
|
|
// This code is very, very similar to PEI::calculateFrameObjectOffsets().
|
|
|
|
// It really should be refactored to share code. Until then, changes
|
|
|
|
// should keep in mind that there's tight coupling between the two.
|
|
|
|
|
|
|
|
for (int i = getObjectIndexBegin(); i != 0; ++i) {
|
2019-04-02 17:46:52 +08:00
|
|
|
// Only estimate stack size of default stack.
|
2019-06-17 17:13:29 +08:00
|
|
|
if (getStackID(i) != TargetStackID::Default)
|
2019-04-02 17:46:52 +08:00
|
|
|
continue;
|
[AArch64] Fix issues with large arrays on stack
Summary:
This patch fixes a few issues when large arrays are allocated on the
stack. Currently, clang has inconsistent behaviour, for debug builds
there is an assertion failure when the array size on stack is around 2GB
but there is no assertion when the stack is around 8GB. For release
builds there is no assertion, the compilation succeeds but generates
incorrect code. The incorrect code generated is due to using
int/unsigned int instead of their 64-bit counterparts. This patch,
1) Removes the assertion in frame legality check.
2) Converts int/unsigned int in some places to the 64-bit variants. This
helps in generating correct code and removes the inconsistent behaviour.
3) Adds a test which runs without optimisations.
Reviewers: sdesmalen, efriedma, fhahn, aemerson
Reviewed By: efriedma
Subscribers: eli.friedman, fpetrogalli, kristof.beyls, hiraditya,
llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70496
2019-11-20 20:45:26 +08:00
|
|
|
int64_t FixedOff = -getObjectOffset(i);
|
2017-04-27 07:36:58 +08:00
|
|
|
if (FixedOff > Offset) Offset = FixedOff;
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
|
2019-04-02 17:46:52 +08:00
|
|
|
// Only estimate stack size of live objects on default stack.
|
2019-06-17 17:13:29 +08:00
|
|
|
if (isDeadObjectIndex(i) || getStackID(i) != TargetStackID::Default)
|
2017-04-27 07:36:58 +08:00
|
|
|
continue;
|
|
|
|
Offset += getObjectSize(i);
|
2020-03-18 01:27:59 +08:00
|
|
|
Align Alignment = getObjectAlign(i);
|
2017-04-27 07:36:58 +08:00
|
|
|
// Adjust to alignment boundary
|
2020-03-18 01:27:59 +08:00
|
|
|
Offset = alignTo(Offset, Alignment);
|
2017-04-27 07:36:58 +08:00
|
|
|
|
2020-03-18 01:27:59 +08:00
|
|
|
MaxAlign = std::max(Alignment, MaxAlign);
|
2017-04-27 07:36:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (adjustsStack() && TFI->hasReservedCallFrame(MF))
|
|
|
|
Offset += getMaxCallFrameSize();
|
|
|
|
|
|
|
|
// Round up the size to a multiple of the alignment. If the function has
|
|
|
|
// any calls or alloca's, align to the target's StackAlignment value to
|
|
|
|
// ensure that the callee's frame or the alloca data is suitably aligned;
|
|
|
|
// otherwise, for leaf functions, align to the TransientStackAlignment
|
|
|
|
// value.
|
2020-03-18 01:27:59 +08:00
|
|
|
Align StackAlign;
|
2017-04-27 07:36:58 +08:00
|
|
|
if (adjustsStack() || hasVarSizedObjects() ||
|
|
|
|
(RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
|
2020-03-18 01:27:59 +08:00
|
|
|
StackAlign = TFI->getStackAlign();
|
2017-04-27 07:36:58 +08:00
|
|
|
else
|
2020-03-18 01:27:59 +08:00
|
|
|
StackAlign = TFI->getTransientStackAlign();
|
2017-04-27 07:36:58 +08:00
|
|
|
|
|
|
|
// If the frame pointer is eliminated, all frame offsets will be relative to
|
|
|
|
// SP not FP. Align to MaxAlign so this works.
|
|
|
|
StackAlign = std::max(StackAlign, MaxAlign);
|
2020-03-18 01:27:59 +08:00
|
|
|
return alignTo(Offset, StackAlign);
|
2017-04-27 07:36:58 +08:00
|
|
|
}
|
|
|
|
|
2017-05-06 06:04:05 +08:00
|
|
|
void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
|
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
|
|
|
unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
|
|
|
|
unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
|
|
|
|
assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
|
|
|
|
"Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
|
|
|
|
|
|
|
|
MaxCallFrameSize = 0;
|
|
|
|
for (const MachineBasicBlock &MBB : MF) {
|
|
|
|
for (const MachineInstr &MI : MBB) {
|
|
|
|
unsigned Opcode = MI.getOpcode();
|
|
|
|
if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
|
|
|
|
unsigned Size = TII.getFrameSize(MI);
|
|
|
|
MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
|
|
|
|
AdjustsStack = true;
|
|
|
|
} else if (MI.isInlineAsm()) {
|
|
|
|
// Some inline asm's need a stack frame, as indicated by operand 1.
|
|
|
|
unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
|
|
|
|
if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
|
|
|
|
AdjustsStack = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 07:36:58 +08:00
|
|
|
void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
|
2020-06-28 19:52:33 +08:00
|
|
|
if (Objects.empty()) return;
|
|
|
|
|
2017-04-27 07:36:58 +08:00
|
|
|
const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
|
|
|
|
int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
|
|
|
|
|
|
|
|
OS << "Frame Objects:\n";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
|
|
const StackObject &SO = Objects[i];
|
|
|
|
OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
|
2017-07-21 05:03:45 +08:00
|
|
|
|
|
|
|
if (SO.StackID != 0)
|
2020-06-28 19:52:33 +08:00
|
|
|
OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
|
2017-07-21 05:03:45 +08:00
|
|
|
|
2017-04-27 07:36:58 +08:00
|
|
|
if (SO.Size == ~0ULL) {
|
|
|
|
OS << "dead\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (SO.Size == 0)
|
|
|
|
OS << "variable sized";
|
|
|
|
else
|
|
|
|
OS << "size=" << SO.Size;
|
2019-08-21 22:29:30 +08:00
|
|
|
OS << ", align=" << SO.Alignment.value();
|
2017-04-27 07:36:58 +08:00
|
|
|
|
|
|
|
if (i < NumFixedObjects)
|
2020-06-28 19:52:33 +08:00
|
|
|
OS << ", fixed";
|
2017-04-27 07:36:58 +08:00
|
|
|
if (i < NumFixedObjects || SO.SPOffset != -1) {
|
|
|
|
int64_t Off = SO.SPOffset - ValOffset;
|
|
|
|
OS << ", at location [SP";
|
|
|
|
if (Off > 0)
|
|
|
|
OS << "+" << Off;
|
|
|
|
else if (Off < 0)
|
|
|
|
OS << Off;
|
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-04-27 07:36:58 +08:00
|
|
|
LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
|
|
|
print(MF, dbgs());
|
|
|
|
}
|
|
|
|
#endif
|