blob: d042844aa1387667bf663fe6dda99c6bde0df8f3 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIMachineFunctionInfo.cpp - SI Machine Function Info -------===//
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/// \file
9//===----------------------------------------------------------------------===//
10
11
12#include "SIMachineFunctionInfo.h"
Tom Stellard96468902014-09-24 01:33:17 +000013#include "AMDGPUSubtarget.h"
Tom Stellardeba61072014-05-02 15:41:42 +000014#include "SIInstrInfo.h"
Tom Stellard96468902014-09-24 01:33:17 +000015#include "llvm/CodeGen/MachineInstrBuilder.h"
Tom Stellardc5cf2f02014-08-21 20:40:54 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Tom Stellardc149dc02013-11-27 21:23:35 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellardeba61072014-05-02 15:41:42 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/LLVMContext.h"
Tom Stellardc149dc02013-11-27 21:23:35 +000020
21#define MAX_LANES 64
Tom Stellard75aadc22012-12-11 21:25:42 +000022
23using namespace llvm;
24
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000025
26// Pin the vtable to this file.
27void SIMachineFunctionInfo::anchor() {}
28
Tom Stellard75aadc22012-12-11 21:25:42 +000029SIMachineFunctionInfo::SIMachineFunctionInfo(const MachineFunction &MF)
Vincent Lejeuneace6f732013-04-01 21:47:53 +000030 : AMDGPUMachineFunction(MF),
Tom Stellard96468902014-09-24 01:33:17 +000031 TIDReg(AMDGPU::NoRegister),
Matt Arsenault49affb82015-11-25 20:55:12 +000032 ScratchRSrcReg(AMDGPU::NoRegister),
33 LDSWaveSpillSize(0),
Tom Stellardc149dc02013-11-27 21:23:35 +000034 PSInputAddr(0),
Tom Stellard96468902014-09-24 01:33:17 +000035 NumUserSGPRs(0),
Matt Arsenault49affb82015-11-25 20:55:12 +000036 HasSpilledSGPRs(false),
37 HasSpilledVGPRs(false),
38 DispatchPtr(false),
39 QueuePtr(false),
40 DispatchID(false),
41 KernargSegmentPtr(true),
42 FlatScratchInit(false),
43 GridWorkgroupCountX(false),
44 GridWorkgroupCountY(false),
45 GridWorkgroupCountZ(false),
46 WorkGroupIDX(true),
47 WorkGroupIDY(false),
48 WorkGroupIDZ(false),
49 WorkGroupInfo(false),
50 WorkItemIDX(true),
51 WorkItemIDY(false),
52 WorkItemIDZ(false) {
53 const Function *F = MF.getFunction();
54
55 if (F->hasFnAttribute("amdgpu-dispatch-ptr"))
56 DispatchPtr = true;
57
58 if (F->hasFnAttribute("amdgpu-work-group-id-y"))
59 WorkGroupIDY = true;
60
61 if (F->hasFnAttribute("amdgpu-work-group-id-z"))
62 WorkGroupIDZ = true;
63
64 if (F->hasFnAttribute("amdgpu-work-item-id-y"))
65 WorkItemIDY = true;
66
67 if (F->hasFnAttribute("amdgpu-work-item-id-z"))
68 WorkItemIDZ = true;
69}
Tom Stellardc5cf2f02014-08-21 20:40:54 +000070
Matt Arsenault0e3d3892015-11-30 21:15:53 +000071void SIMachineFunctionInfo::setScratchRSrcReg(const SIRegisterInfo *TRI) {
72 // We need to round up to next multiple of 4.
73 unsigned NextSReg128 = RoundUpToAlignment(NumUserSGPRs + 5, 4);
74 unsigned RegSub0 = AMDGPU::SReg_32RegClass.getRegister(NextSReg128);
75 ScratchRSrcReg = TRI->getMatchingSuperReg(RegSub0, AMDGPU::sub0,
76 &AMDGPU::SReg_128RegClass);
77}
78
Tom Stellardc5cf2f02014-08-21 20:40:54 +000079SIMachineFunctionInfo::SpilledReg SIMachineFunctionInfo::getSpilledReg(
80 MachineFunction *MF,
81 unsigned FrameIndex,
82 unsigned SubIdx) {
83 const MachineFrameInfo *FrameInfo = MF->getFrameInfo();
Eric Christopher0795a2e2015-02-19 01:10:55 +000084 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(
85 MF->getSubtarget<AMDGPUSubtarget>().getRegisterInfo());
Tom Stellardc5cf2f02014-08-21 20:40:54 +000086 MachineRegisterInfo &MRI = MF->getRegInfo();
87 int64_t Offset = FrameInfo->getObjectOffset(FrameIndex);
88 Offset += SubIdx * 4;
89
90 unsigned LaneVGPRIdx = Offset / (64 * 4);
91 unsigned Lane = (Offset / 4) % 64;
92
93 struct SpilledReg Spill;
94
95 if (!LaneVGPRs.count(LaneVGPRIdx)) {
Tom Stellard42fb60e2015-01-14 15:42:31 +000096 unsigned LaneVGPR = TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass);
Tom Stellardc5cf2f02014-08-21 20:40:54 +000097 LaneVGPRs[LaneVGPRIdx] = LaneVGPR;
Tom Stellardc5cf2f02014-08-21 20:40:54 +000098
99 // Add this register as live-in to all blocks to avoid machine verifer
100 // complaining about use of an undefined physical register.
101 for (MachineFunction::iterator BI = MF->begin(), BE = MF->end();
102 BI != BE; ++BI) {
103 BI->addLiveIn(LaneVGPR);
104 }
105 }
106
107 Spill.VGPR = LaneVGPRs[LaneVGPRIdx];
108 Spill.Lane = Lane;
109 return Spill;
Tom Stellardc149dc02013-11-27 21:23:35 +0000110}
Tom Stellard96468902014-09-24 01:33:17 +0000111
112unsigned SIMachineFunctionInfo::getMaximumWorkGroupSize(
113 const MachineFunction &MF) const {
Eric Christopher0795a2e2015-02-19 01:10:55 +0000114 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Tom Stellard96468902014-09-24 01:33:17 +0000115 // FIXME: We should get this information from kernel attributes if it
116 // is available.
117 return getShaderType() == ShaderType::COMPUTE ? 256 : ST.getWavefrontSize();
118}