vc4: Add copy propagation between temps.
[mesa.git] / src / gallium / drivers / vc4 / vc4_qir.h
1 /*
2 * Copyright © 2014 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef VC4_QIR_H
25 #define VC4_QIR_H
26
27 #include <stdbool.h>
28 #include <stdint.h>
29
30 #include "util/u_simple_list.h"
31
32 enum qfile {
33 QFILE_NULL,
34 QFILE_TEMP,
35 QFILE_VARY,
36 QFILE_UNIF,
37 };
38
39 struct qreg {
40 enum qfile file;
41 uint32_t index;
42 };
43
44 enum qop {
45 QOP_UNDEF,
46 QOP_MOV,
47 QOP_FADD,
48 QOP_FSUB,
49 QOP_FMUL,
50 QOP_FMIN,
51 QOP_FMAX,
52 QOP_FMINABS,
53 QOP_FMAXABS,
54
55 QOP_SEQ,
56 QOP_SNE,
57 QOP_SGE,
58 QOP_SLT,
59 QOP_CMP,
60
61 QOP_FTOI,
62 QOP_RCP,
63 QOP_RSQ,
64 QOP_EXP2,
65 QOP_LOG2,
66 QOP_VW_SETUP,
67 QOP_VR_SETUP,
68 QOP_PACK_SCALED,
69 QOP_PACK_COLORS,
70 QOP_VPM_WRITE,
71 QOP_VPM_READ,
72 QOP_TLB_COLOR_WRITE,
73 QOP_VARY_ADD_C,
74 };
75
76 struct simple_node {
77 struct simple_node *next;
78 struct simple_node *prev;
79 };
80
81 struct qinst {
82 struct simple_node link;
83
84 enum qop op;
85 struct qreg dst;
86 struct qreg *src;
87 };
88
89 enum qstage {
90 /**
91 * Coordinate shader, runs during binning, before the VS, and just
92 * outputs position.
93 */
94 QSTAGE_COORD,
95 QSTAGE_VERT,
96 QSTAGE_FRAG,
97 };
98
99 enum quniform_contents {
100 /**
101 * Indicates that a constant 32-bit value is copied from the program's
102 * uniform contents.
103 */
104 QUNIFORM_CONSTANT,
105 /**
106 * Indicates that the program's uniform contents are used as an index
107 * into the GL uniform storage.
108 */
109 QUNIFORM_UNIFORM,
110
111 /** @{
112 * Scaling factors from clip coordinates to relative to the viewport
113 * center.
114 *
115 * This is used by the coordinate and vertex shaders to produce the
116 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
117 * point offsets from the viewport ccenter.
118 */
119 QUNIFORM_VIEWPORT_X_SCALE,
120 QUNIFORM_VIEWPORT_Y_SCALE,
121 /** @} */
122 };
123
124 struct qcompile {
125 struct qreg undef;
126 enum qstage stage;
127 uint32_t num_temps;
128 struct simple_node instructions;
129 uint32_t immediates[1024];
130
131 struct simple_node qpu_inst_list;
132 uint64_t *qpu_insts;
133 uint32_t qpu_inst_count;
134 uint32_t qpu_inst_size;
135 };
136
137 struct qcompile *qir_compile_init(void);
138 void qir_compile_destroy(struct qcompile *c);
139 struct qinst *qir_inst(enum qop op, struct qreg dst,
140 struct qreg src0, struct qreg src1);
141 struct qinst *qir_inst4(enum qop op, struct qreg dst,
142 struct qreg a,
143 struct qreg b,
144 struct qreg c,
145 struct qreg d);
146 void qir_emit(struct qcompile *c, struct qinst *inst);
147 struct qreg qir_get_temp(struct qcompile *c);
148 int qir_get_op_nsrc(enum qop qop);
149 bool qir_reg_equals(struct qreg a, struct qreg b);
150 bool qir_has_side_effects(struct qinst *inst);
151
152 void qir_dump(struct qcompile *c);
153 void qir_dump_inst(struct qinst *inst);
154 const char *qir_get_stage_name(enum qstage stage);
155
156 void qir_optimize(struct qcompile *c);
157 bool qir_opt_algebraic(struct qcompile *c);
158 bool qir_opt_copy_propagation(struct qcompile *c);
159 bool qir_opt_dead_code(struct qcompile *c);
160
161 #define QIR_ALU1(name) \
162 static inline struct qreg \
163 qir_##name(struct qcompile *c, struct qreg a) \
164 { \
165 struct qreg t = qir_get_temp(c); \
166 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
167 return t; \
168 }
169
170 #define QIR_ALU2(name) \
171 static inline struct qreg \
172 qir_##name(struct qcompile *c, struct qreg a, struct qreg b) \
173 { \
174 struct qreg t = qir_get_temp(c); \
175 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
176 return t; \
177 }
178
179 QIR_ALU1(MOV)
180 QIR_ALU2(FADD)
181 QIR_ALU2(FSUB)
182 QIR_ALU2(FMUL)
183 QIR_ALU2(FMIN)
184 QIR_ALU2(FMAX)
185 QIR_ALU2(FMINABS)
186 QIR_ALU2(FMAXABS)
187 QIR_ALU1(FTOI)
188 QIR_ALU1(RCP)
189 QIR_ALU1(RSQ)
190 QIR_ALU1(EXP2)
191 QIR_ALU1(LOG2)
192 QIR_ALU2(PACK_SCALED)
193 QIR_ALU1(VARY_ADD_C)
194
195 static inline void
196 qir_VPM_WRITE(struct qcompile *c, struct qreg a)
197 {
198 qir_emit(c, qir_inst(QOP_VPM_WRITE, c->undef, a, c->undef));
199 }
200
201 #endif /* VC4_QIR_H */