blob: 082e7345d4fbbc0d19c62e01ccd3ade58d66347c [file] [log] [blame]
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001//===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===//
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//
10/// \file
11///
12/// The AMDGPUAsmPrinter is used to print both assembly string and also binary
13/// code. When passed an MCAsmStreamer it prints assembly and when passed
14/// an MCObjectStreamer it outputs binary code.
15//
16//===----------------------------------------------------------------------===//
17//
18
19
20#include "AMDGPUAsmPrinter.h"
21#include "AMDGPU.h"
22#include "SIMachineFunctionInfo.h"
23#include "SIRegisterInfo.h"
24#include "llvm/MC/MCStreamer.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000025#include "llvm/Support/TargetRegistry.h"
Chandler Carruth58a2cbe2013-01-02 10:22:59 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000027
28using namespace llvm;
29
30
31static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm,
32 MCStreamer &Streamer) {
33 return new AMDGPUAsmPrinter(tm, Streamer);
34}
35
36extern "C" void LLVMInitializeR600AsmPrinter() {
37 TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
38}
39
40/// We need to override this function so we can avoid
41/// the call to EmitFunctionHeader(), which the MCPureStreamer can't handle.
42bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
43 const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
44 if (STM.dumpCode()) {
45#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
46 MF.dump();
47#endif
48 }
49 SetupMachineFunction(MF);
Tom Stellard3ce2ec82013-02-05 17:09:11 +000050 if (OutStreamer.hasRawTextSupport()) {
51 OutStreamer.EmitRawText("@" + MF.getName() + ":");
52 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +000053 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
54 if (STM.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
55 EmitProgramInfo(MF);
56 }
57 EmitFunctionBody();
58 return false;
59}
60
61void AMDGPUAsmPrinter::EmitProgramInfo(MachineFunction &MF) {
62 unsigned MaxSGPR = 0;
63 unsigned MaxVGPR = 0;
64 bool VCCUsed = false;
65 const SIRegisterInfo * RI =
66 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
67
68 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
69 BB != BB_E; ++BB) {
70 MachineBasicBlock &MBB = *BB;
71 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
72 I != E; ++I) {
73 MachineInstr &MI = *I;
74
75 unsigned numOperands = MI.getNumOperands();
76 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
77 MachineOperand & MO = MI.getOperand(op_idx);
78 unsigned maxUsed;
79 unsigned width = 0;
80 bool isSGPR = false;
81 unsigned reg;
82 unsigned hwReg;
83 if (!MO.isReg()) {
84 continue;
85 }
86 reg = MO.getReg();
87 if (reg == AMDGPU::VCC) {
88 VCCUsed = true;
89 continue;
90 }
91 switch (reg) {
92 default: break;
93 case AMDGPU::EXEC:
94 case AMDGPU::SI_LITERAL_CONSTANT:
95 case AMDGPU::SREG_LIT_0:
96 case AMDGPU::M0:
97 continue;
98 }
99
100 if (AMDGPU::SReg_32RegClass.contains(reg)) {
101 isSGPR = true;
102 width = 1;
103 } else if (AMDGPU::VReg_32RegClass.contains(reg)) {
104 isSGPR = false;
105 width = 1;
106 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
107 isSGPR = true;
108 width = 2;
109 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
110 isSGPR = false;
111 width = 2;
112 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
113 isSGPR = true;
114 width = 4;
115 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
116 isSGPR = false;
117 width = 4;
118 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
119 isSGPR = true;
120 width = 8;
Tom Stellard36ba9092013-02-07 17:02:09 +0000121 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
122 isSGPR = false;
123 width = 8;
124 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
125 isSGPR = false;
126 width = 16;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000127 } else {
128 assert(!"Unknown register class");
129 }
130 hwReg = RI->getEncodingValue(reg);
131 maxUsed = hwReg + width - 1;
132 if (isSGPR) {
133 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
134 } else {
135 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
136 }
137 }
138 }
139 }
140 if (VCCUsed) {
141 MaxSGPR += 2;
142 }
143 SIMachineFunctionInfo * MFI = MF.getInfo<SIMachineFunctionInfo>();
144 OutStreamer.EmitIntValue(MaxSGPR + 1, 4);
145 OutStreamer.EmitIntValue(MaxVGPR + 1, 4);
146 OutStreamer.EmitIntValue(MFI->SPIPSInputAddr, 4);
147}