i965/nir: Run DCE again before going out of SSA
[mesa.git] / src / mesa / drivers / dri / i965 / brw_shader.h
1 /*
2 * Copyright © 2010 Intel Corporation
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 #include <stdint.h>
25 #include "brw_reg.h"
26 #include "brw_defines.h"
27 #include "main/compiler.h"
28 #include "glsl/ir.h"
29
30 #ifdef __cplusplus
31 #include "brw_ir_allocator.h"
32 #endif
33
34 #pragma once
35
36 #define MAX_SAMPLER_MESSAGE_SIZE 11
37 #define MAX_VGRF_SIZE 16
38
39 enum PACKED register_file {
40 BAD_FILE,
41 GRF,
42 MRF,
43 IMM,
44 HW_REG, /* a struct brw_reg */
45 ATTR,
46 UNIFORM, /* prog_data->params[reg] */
47 };
48
49 struct backend_reg
50 {
51 #ifdef __cplusplus
52 bool is_zero() const;
53 bool is_one() const;
54 bool is_negative_one() const;
55 bool is_null() const;
56 bool is_accumulator() const;
57 bool in_range(const backend_reg &r, unsigned n) const;
58 #endif
59
60 enum register_file file; /**< Register file: GRF, MRF, IMM. */
61 enum brw_reg_type type; /**< Register type: BRW_REGISTER_TYPE_* */
62
63 /**
64 * Register number.
65 *
66 * For GRF, it's a virtual register number until register allocation.
67 *
68 * For MRF, it's the hardware register.
69 */
70 uint16_t reg;
71
72 /**
73 * Offset within the virtual register.
74 *
75 * In the scalar backend, this is in units of a float per pixel for pre-
76 * register allocation registers (i.e., one register in SIMD8 mode and two
77 * registers in SIMD16 mode).
78 *
79 * For uniforms, this is in units of 1 float.
80 */
81 uint16_t reg_offset;
82
83 struct brw_reg fixed_hw_reg;
84
85 bool negate;
86 bool abs;
87 };
88
89 struct cfg_t;
90 struct bblock_t;
91
92 #ifdef __cplusplus
93 struct backend_instruction : public exec_node {
94 bool is_3src() const;
95 bool is_tex() const;
96 bool is_math() const;
97 bool is_control_flow() const;
98 bool is_commutative() const;
99 bool can_do_source_mods() const;
100 bool can_do_saturate() const;
101 bool can_do_cmod() const;
102 bool reads_accumulator_implicitly() const;
103 bool writes_accumulator_implicitly(struct brw_context *brw) const;
104
105 void remove(bblock_t *block);
106 void insert_after(bblock_t *block, backend_instruction *inst);
107 void insert_before(bblock_t *block, backend_instruction *inst);
108 void insert_before(bblock_t *block, exec_list *list);
109
110 /**
111 * True if the instruction has side effects other than writing to
112 * its destination registers. You are expected not to reorder or
113 * optimize these out unless you know what you are doing.
114 */
115 bool has_side_effects() const;
116 #else
117 struct backend_instruction {
118 struct exec_node link;
119 #endif
120 /** @{
121 * Annotation for the generated IR. One of the two can be set.
122 */
123 const void *ir;
124 const char *annotation;
125 /** @} */
126
127 uint32_t offset; /**< spill/unspill offset or texture offset bitfield */
128 uint8_t mlen; /**< SEND message length */
129 int8_t base_mrf; /**< First MRF in the SEND message, if mlen is nonzero. */
130 uint8_t target; /**< MRT target. */
131 uint8_t regs_written; /**< Number of registers written by the instruction. */
132
133 enum opcode opcode; /* BRW_OPCODE_* or FS_OPCODE_* */
134 enum brw_conditional_mod conditional_mod; /**< BRW_CONDITIONAL_* */
135 enum brw_predicate predicate;
136 bool predicate_inverse:1;
137 bool writes_accumulator:1; /**< instruction implicitly writes accumulator */
138 bool force_writemask_all:1;
139 bool no_dd_clear:1;
140 bool no_dd_check:1;
141 bool saturate:1;
142 bool shadow_compare:1;
143 bool header_present:1;
144
145 /* Chooses which flag subregister (f0.0 or f0.1) is used for conditional
146 * mod and predication.
147 */
148 unsigned flag_subreg:1;
149 };
150
151 #ifdef __cplusplus
152
153 enum instruction_scheduler_mode {
154 SCHEDULE_PRE,
155 SCHEDULE_PRE_NON_LIFO,
156 SCHEDULE_PRE_LIFO,
157 SCHEDULE_POST,
158 };
159
160 class backend_visitor : public ir_visitor {
161 protected:
162
163 backend_visitor(struct brw_context *brw,
164 struct gl_shader_program *shader_prog,
165 struct gl_program *prog,
166 struct brw_stage_prog_data *stage_prog_data,
167 gl_shader_stage stage);
168
169 public:
170
171 struct brw_context * const brw;
172 struct gl_context * const ctx;
173 struct brw_shader * const shader;
174 struct gl_shader_program * const shader_prog;
175 struct gl_program * const prog;
176 struct brw_stage_prog_data * const stage_prog_data;
177
178 /** ralloc context for temporary data used during compile */
179 void *mem_ctx;
180
181 /**
182 * List of either fs_inst or vec4_instruction (inheriting from
183 * backend_instruction)
184 */
185 exec_list instructions;
186
187 cfg_t *cfg;
188
189 gl_shader_stage stage;
190 bool debug_enabled;
191 const char *stage_name;
192 const char *stage_abbrev;
193
194 brw::simple_allocator alloc;
195
196 virtual void dump_instruction(backend_instruction *inst) = 0;
197 virtual void dump_instruction(backend_instruction *inst, FILE *file) = 0;
198 virtual void dump_instructions();
199 virtual void dump_instructions(const char *name);
200
201 void calculate_cfg();
202 void invalidate_cfg();
203
204 void assign_common_binding_table_offsets(uint32_t next_binding_table_offset);
205
206 virtual void invalidate_live_intervals() = 0;
207 };
208
209 uint32_t brw_texture_offset(struct gl_context *ctx, int *offsets,
210 unsigned num_components);
211
212 #endif /* __cplusplus */
213
214 enum brw_reg_type brw_type_for_base_type(const struct glsl_type *type);
215 enum brw_conditional_mod brw_conditional_for_comparison(unsigned int op);
216 uint32_t brw_math_function(enum opcode op);
217 const char *brw_instruction_name(enum opcode op);
218 bool brw_saturate_immediate(enum brw_reg_type type, struct brw_reg *reg);
219 bool brw_negate_immediate(enum brw_reg_type type, struct brw_reg *reg);
220 bool brw_abs_immediate(enum brw_reg_type type, struct brw_reg *reg);
221
222 #ifdef __cplusplus
223 extern "C" {
224 #endif
225
226 bool brw_vs_precompile(struct gl_context *ctx,
227 struct gl_shader_program *shader_prog,
228 struct gl_program *prog);
229 bool brw_gs_precompile(struct gl_context *ctx,
230 struct gl_shader_program *shader_prog,
231 struct gl_program *prog);
232 bool brw_fs_precompile(struct gl_context *ctx,
233 struct gl_shader_program *shader_prog,
234 struct gl_program *prog);
235
236 #ifdef __cplusplus
237 }
238 #endif