1 //===----------------------- AMDGPUFrameLowering.cpp ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //==-----------------------------------------------------------------------===//
10 // Interface to describe a layout of a stack frame on a AMDGPU target machine.
12 //===----------------------------------------------------------------------===//
14 #include "AMDGPUFrameLowering.h"
15 #include "AMDGPURegisterInfo.h"
16 #include "AMDGPUSubtarget.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/Support/MathExtras.h"
22 AMDGPUFrameLowering::AMDGPUFrameLowering(StackDirection D, unsigned StackAl,
23 int LAO, unsigned TransAl)
24 : TargetFrameLowering(D, StackAl, LAO, TransAl) { }
26 AMDGPUFrameLowering::~AMDGPUFrameLowering() = default;
28 unsigned AMDGPUFrameLowering::getStackWidth(const MachineFunction &MF) const {
29 // XXX: Hardcoding to 1 for now.
31 // I think the StackWidth should stored as metadata associated with the
32 // MachineFunction. This metadata can either be added by a frontend, or
33 // calculated by a R600 specific LLVM IR pass.
35 // The StackWidth determines how stack objects are laid out in memory.
36 // For a vector stack variable, like: int4 stack[2], the data will be stored
37 // in the following ways depending on the StackWidth.
73 /// \returns The number of registers allocated for \p FI.
74 int AMDGPUFrameLowering::getFrameIndexReference(const MachineFunction &MF,
76 unsigned &FrameReg) const {
77 const MachineFrameInfo &MFI = MF.getFrameInfo();
78 const AMDGPURegisterInfo *RI
79 = MF.getSubtarget<AMDGPUSubtarget>().getRegisterInfo();
81 // Fill in FrameReg output argument.
82 FrameReg = RI->getFrameRegister(MF);
84 // Start the offset at 2 so we don't overwrite work group information.
85 // XXX: We should only do this when the shader actually uses this
87 unsigned OffsetBytes = 2 * (getStackWidth(MF) * 4);
88 int UpperBound = FI == -1 ? MFI.getNumObjects() : FI;
90 for (int i = MFI.getObjectIndexBegin(); i < UpperBound; ++i) {
91 OffsetBytes = alignTo(OffsetBytes, MFI.getObjectAlignment(i));
92 OffsetBytes += MFI.getObjectSize(i);
93 // Each register holds 4 bytes, so we must always align the offset to at
94 // least 4 bytes, so that 2 frame objects won't share the same register.
95 OffsetBytes = alignTo(OffsetBytes, 4);
99 OffsetBytes = alignTo(OffsetBytes, MFI.getObjectAlignment(FI));
101 return OffsetBytes / (getStackWidth(MF) * 4);