radeon/llvm: Move util functions into AMDGPU namespace
[mesa.git] / src / gallium / drivers / radeon / AMDGPUUtil.cpp
1 //===-- AMDGPUUtil.cpp - AMDGPU Utility functions -------------------------===//
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 // Common utility functions used by hw codegen targets
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "AMDGPUUtil.h"
15 #include "AMDGPURegisterInfo.h"
16 #include "AMDIL.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23
24 using namespace llvm;
25
26 // Some instructions act as place holders to emulate operations that the GPU
27 // hardware does automatically. This function can be used to check if
28 // an opcode falls into this category.
29 bool AMDGPU::isPlaceHolderOpcode(unsigned opcode)
30 {
31 switch (opcode) {
32 default: return false;
33 case AMDIL::RETURN:
34 case AMDIL::LOAD_INPUT:
35 case AMDIL::LAST:
36 case AMDIL::MASK_WRITE:
37 case AMDIL::RESERVE_REG:
38 return true;
39 }
40 }
41
42 bool AMDGPU::isTransOp(unsigned opcode)
43 {
44 switch(opcode) {
45 default: return false;
46
47 case AMDIL::COS_f32:
48 case AMDIL::COS_r600:
49 case AMDIL::COS_eg:
50 case AMDIL::RSQ_f32:
51 case AMDIL::FTOI:
52 case AMDIL::ITOF:
53 case AMDIL::MULLIT:
54 case AMDIL::MUL_LIT_r600:
55 case AMDIL::MUL_LIT_eg:
56 case AMDIL::SHR_i32:
57 case AMDIL::SIN_f32:
58 case AMDIL::EXP_f32:
59 case AMDIL::EXP_IEEE_r600:
60 case AMDIL::EXP_IEEE_eg:
61 case AMDIL::LOG_CLAMPED_r600:
62 case AMDIL::LOG_IEEE_r600:
63 case AMDIL::LOG_CLAMPED_eg:
64 case AMDIL::LOG_IEEE_eg:
65 case AMDIL::LOG_f32:
66 return true;
67 }
68 }
69
70 bool AMDGPU::isTexOp(unsigned opcode)
71 {
72 switch(opcode) {
73 default: return false;
74 case AMDIL::TEX_LD:
75 case AMDIL::TEX_GET_TEXTURE_RESINFO:
76 case AMDIL::TEX_SAMPLE:
77 case AMDIL::TEX_SAMPLE_C:
78 case AMDIL::TEX_SAMPLE_L:
79 case AMDIL::TEX_SAMPLE_C_L:
80 case AMDIL::TEX_SAMPLE_LB:
81 case AMDIL::TEX_SAMPLE_C_LB:
82 case AMDIL::TEX_SAMPLE_G:
83 case AMDIL::TEX_SAMPLE_C_G:
84 case AMDIL::TEX_GET_GRADIENTS_H:
85 case AMDIL::TEX_GET_GRADIENTS_V:
86 return true;
87 }
88 }
89
90 bool AMDGPU::isReductionOp(unsigned opcode)
91 {
92 switch(opcode) {
93 default: return false;
94 case AMDIL::DOT4_r600:
95 case AMDIL::DOT4_eg:
96 return true;
97 }
98 }
99
100 bool AMDGPU::isCubeOp(unsigned opcode)
101 {
102 switch(opcode) {
103 default: return false;
104 case AMDIL::CUBE_r600:
105 case AMDIL::CUBE_eg:
106 return true;
107 }
108 }
109
110
111 bool AMDGPU::isFCOp(unsigned opcode)
112 {
113 switch(opcode) {
114 default: return false;
115 case AMDIL::BREAK_LOGICALZ_f32:
116 case AMDIL::BREAK_LOGICALNZ_i32:
117 case AMDIL::BREAK_LOGICALZ_i32:
118 case AMDIL::BREAK_LOGICALNZ_f32:
119 case AMDIL::CONTINUE_LOGICALNZ_f32:
120 case AMDIL::IF_LOGICALNZ_i32:
121 case AMDIL::IF_LOGICALZ_f32:
122 case AMDIL::ELSE:
123 case AMDIL::ENDIF:
124 case AMDIL::ENDLOOP:
125 case AMDIL::IF_LOGICALNZ_f32:
126 case AMDIL::WHILELOOP:
127 return true;
128 }
129 }
130
131 void AMDGPU::utilAddLiveIn(llvm::MachineFunction * MF,
132 llvm::MachineRegisterInfo & MRI,
133 const struct llvm::TargetInstrInfo * TII,
134 unsigned physReg, unsigned virtReg)
135 {
136 if (!MRI.isLiveIn(physReg)) {
137 MRI.addLiveIn(physReg, virtReg);
138 MF->front().addLiveIn(physReg);
139 BuildMI(MF->front(), MF->front().begin(), DebugLoc(),
140 TII->get(TargetOpcode::COPY), virtReg)
141 .addReg(physReg);
142 } else {
143 MRI.replaceRegWith(virtReg, MRI.getLiveInVirtReg(physReg));
144 }
145 }