blob: 805fb7102a353dfaeafdbd6d97fa44cf2fd2b153 [file] [log] [blame]
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001//===----------------------- AMDGPUFrameLowering.cpp ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//==-----------------------------------------------------------------------===//
9//
Matt Arsenault7836f892016-01-20 21:22:21 +000010// Interface to describe a layout of a stack frame on a AMDGPU target machine.
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000011//
12//===----------------------------------------------------------------------===//
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000013
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000014#include "AMDGPUFrameLowering.h"
15#include "AMDGPURegisterInfo.h"
Matt Arsenault43e92fe2016-06-24 06:30:11 +000016#include "AMDGPUSubtarget.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000017#include "llvm/CodeGen/MachineFunction.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000019#include "llvm/Support/MathExtras.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000020
21using namespace llvm;
22AMDGPUFrameLowering::AMDGPUFrameLowering(StackDirection D, unsigned StackAl,
23 int LAO, unsigned TransAl)
24 : TargetFrameLowering(D, StackAl, LAO, TransAl) { }
25
Eugene Zelenko2bc2f332016-12-09 22:06:55 +000026AMDGPUFrameLowering::~AMDGPUFrameLowering() = default;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000027
28unsigned AMDGPUFrameLowering::getStackWidth(const MachineFunction &MF) const {
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000029 // XXX: Hardcoding to 1 for now.
30 //
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.
34 //
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.
38 //
39 // StackWidth = 1:
40 //
41 // T0.X = stack[0].x
42 // T1.X = stack[0].y
43 // T2.X = stack[0].z
44 // T3.X = stack[0].w
45 // T4.X = stack[1].x
46 // T5.X = stack[1].y
47 // T6.X = stack[1].z
48 // T7.X = stack[1].w
49 //
50 // StackWidth = 2:
51 //
52 // T0.X = stack[0].x
53 // T0.Y = stack[0].y
54 // T1.X = stack[0].z
55 // T1.Y = stack[0].w
56 // T2.X = stack[1].x
57 // T2.Y = stack[1].y
58 // T3.X = stack[1].z
59 // T3.Y = stack[1].w
Matt Arsenault7836f892016-01-20 21:22:21 +000060 //
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000061 // StackWidth = 4:
62 // T0.X = stack[0].x
63 // T0.Y = stack[0].y
64 // T0.Z = stack[0].z
65 // T0.W = stack[0].w
66 // T1.X = stack[1].x
67 // T1.Y = stack[1].y
68 // T1.Z = stack[1].z
69 // T1.W = stack[1].w
70 return 1;
71}
72
73/// \returns The number of registers allocated for \p FI.
James Y Knight5567baf2015-08-15 02:32:35 +000074int AMDGPUFrameLowering::getFrameIndexReference(const MachineFunction &MF,
75 int FI,
76 unsigned &FrameReg) const {
Matthias Braun941a7052016-07-28 18:40:00 +000077 const MachineFrameInfo &MFI = MF.getFrameInfo();
Matt Arsenault43e92fe2016-06-24 06:30:11 +000078 const AMDGPURegisterInfo *RI
79 = MF.getSubtarget<AMDGPUSubtarget>().getRegisterInfo();
James Y Knight5567baf2015-08-15 02:32:35 +000080
81 // Fill in FrameReg output argument.
82 FrameReg = RI->getFrameRegister(MF);
83
Tom Stellard27982b12014-01-22 19:24:19 +000084 // 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
86 // information.
Tom Stellard598f3942014-01-22 19:24:23 +000087 unsigned OffsetBytes = 2 * (getStackWidth(MF) * 4);
Matthias Braun941a7052016-07-28 18:40:00 +000088 int UpperBound = FI == -1 ? MFI.getNumObjects() : FI;
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000089
Matthias Braun941a7052016-07-28 18:40:00 +000090 for (int i = MFI.getObjectIndexBegin(); i < UpperBound; ++i) {
91 OffsetBytes = alignTo(OffsetBytes, MFI.getObjectAlignment(i));
92 OffsetBytes += MFI.getObjectSize(i);
Matt Arsenaultb2e87442014-06-14 04:26:07 +000093 // Each register holds 4 bytes, so we must always align the offset to at
Tom Stellard598f3942014-01-22 19:24:23 +000094 // least 4 bytes, so that 2 frame objects won't share the same register.
Rui Ueyamada00f2f2016-01-14 21:06:47 +000095 OffsetBytes = alignTo(OffsetBytes, 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000096 }
Tom Stellard598f3942014-01-22 19:24:23 +000097
98 if (FI != -1)
Matthias Braun941a7052016-07-28 18:40:00 +000099 OffsetBytes = alignTo(OffsetBytes, MFI.getObjectAlignment(FI));
Tom Stellard598f3942014-01-22 19:24:23 +0000100
101 return OffsetBytes / (getStackWidth(MF) * 4);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000102}