2 * Copyright © 2010 Intel Corporation
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:
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
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
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_instructions.cpp
27 * Many GPUs lack native instructions for certain expression operations, and
28 * must replace them with some other expression tree. This pass lowers some
29 * of the most common cases, allowing the lowering code to be implemented once
30 * rather than in each driver backend.
32 * Currently supported transformations:
35 * - INT_DIV_TO_MUL_RCP
42 * - BITFIELD_INSERT_TO_BFM_BFI
46 * Breaks an ir_binop_sub expression down to add(op0, neg(op1))
48 * This simplifies expression reassociation, and for many backends
49 * there is no subtract operation separate from adding the negation.
50 * For backends with native subtract operations, they will probably
51 * want to recognize add(op0, neg(op1)) or the other way around to
52 * produce a subtract anyway.
54 * DIV_TO_MUL_RCP and INT_DIV_TO_MUL_RCP:
55 * --------------------------------------
56 * Breaks an ir_binop_div expression down to op0 * (rcp(op1)).
58 * Many GPUs don't have a divide instruction (945 and 965 included),
59 * but they do have an RCP instruction to compute an approximate
60 * reciprocal. By breaking the operation down, constant reciprocals
61 * can get constant folded.
63 * DIV_TO_MUL_RCP only lowers floating point division; INT_DIV_TO_MUL_RCP
64 * handles the integer case, converting to and from floating point so that
67 * EXP_TO_EXP2 and LOG_TO_LOG2:
68 * ----------------------------
69 * Many GPUs don't have a base e log or exponent instruction, but they
70 * do have base 2 versions, so this pass converts exp and log to exp2
71 * and log2 operations.
75 * Many older GPUs don't have an x**y instruction. For these GPUs, convert
76 * x**y to 2**(y * log2(x)).
80 * Breaks an ir_binop_mod expression down to (op1 * fract(op0 / op1))
82 * Many GPUs don't have a MOD instruction (945 and 965 included), and
83 * if we have to break it down like this anyway, it gives an
84 * opportunity to do things like constant fold the (1.0 / op1) easily.
88 * Converts ir_binop_ldexp to arithmetic and bit operations.
92 * Converts ir_triop_lrp to (op0 * (1.0f - op2)) + (op1 * op2).
94 * BITFIELD_INSERT_TO_BFM_BFI:
95 * ---------------------------
96 * Breaks ir_quadop_bitfield_insert into ir_binop_bfm (bitfield mask) and
97 * ir_triop_bfi (bitfield insert).
99 * Many GPUs implement the bitfieldInsert() built-in from ARB_gpu_shader_5
100 * with a pair of instructions.
104 #include "main/core.h" /* for M_LOG2E */
105 #include "glsl_types.h"
107 #include "ir_builder.h"
108 #include "ir_optimization.h"
110 using namespace ir_builder
;
114 class lower_instructions_visitor
: public ir_hierarchical_visitor
{
116 lower_instructions_visitor(unsigned lower
)
117 : progress(false), lower(lower
) { }
119 ir_visitor_status
visit_leave(ir_expression
*);
124 unsigned lower
; /** Bitfield of which operations to lower */
126 void sub_to_add_neg(ir_expression
*);
127 void div_to_mul_rcp(ir_expression
*);
128 void int_div_to_mul_rcp(ir_expression
*);
129 void mod_to_fract(ir_expression
*);
130 void exp_to_exp2(ir_expression
*);
131 void pow_to_exp2(ir_expression
*);
132 void log_to_log2(ir_expression
*);
133 void lrp_to_arith(ir_expression
*);
134 void bitfield_insert_to_bfm_bfi(ir_expression
*);
135 void ldexp_to_arith(ir_expression
*);
138 } /* anonymous namespace */
141 * Determine if a particular type of lowering should occur
143 #define lowering(x) (this->lower & x)
146 lower_instructions(exec_list
*instructions
, unsigned what_to_lower
)
148 lower_instructions_visitor
v(what_to_lower
);
150 visit_list_elements(&v
, instructions
);
155 lower_instructions_visitor::sub_to_add_neg(ir_expression
*ir
)
157 ir
->operation
= ir_binop_add
;
158 ir
->operands
[1] = new(ir
) ir_expression(ir_unop_neg
, ir
->operands
[1]->type
,
159 ir
->operands
[1], NULL
);
160 this->progress
= true;
164 lower_instructions_visitor::div_to_mul_rcp(ir_expression
*ir
)
166 assert(ir
->operands
[1]->type
->is_float());
168 /* New expression for the 1.0 / op1 */
170 expr
= new(ir
) ir_expression(ir_unop_rcp
,
171 ir
->operands
[1]->type
,
174 /* op0 / op1 -> op0 * (1.0 / op1) */
175 ir
->operation
= ir_binop_mul
;
176 ir
->operands
[1] = expr
;
178 this->progress
= true;
182 lower_instructions_visitor::int_div_to_mul_rcp(ir_expression
*ir
)
184 assert(ir
->operands
[1]->type
->is_integer());
186 /* Be careful with integer division -- we need to do it as a
187 * float and re-truncate, since rcp(n > 1) of an integer would
190 ir_rvalue
*op0
, *op1
;
191 const struct glsl_type
*vec_type
;
193 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
194 ir
->operands
[1]->type
->vector_elements
,
195 ir
->operands
[1]->type
->matrix_columns
);
197 if (ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
)
198 op1
= new(ir
) ir_expression(ir_unop_i2f
, vec_type
, ir
->operands
[1], NULL
);
200 op1
= new(ir
) ir_expression(ir_unop_u2f
, vec_type
, ir
->operands
[1], NULL
);
202 op1
= new(ir
) ir_expression(ir_unop_rcp
, op1
->type
, op1
, NULL
);
204 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
205 ir
->operands
[0]->type
->vector_elements
,
206 ir
->operands
[0]->type
->matrix_columns
);
208 if (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
)
209 op0
= new(ir
) ir_expression(ir_unop_i2f
, vec_type
, ir
->operands
[0], NULL
);
211 op0
= new(ir
) ir_expression(ir_unop_u2f
, vec_type
, ir
->operands
[0], NULL
);
213 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
214 ir
->type
->vector_elements
,
215 ir
->type
->matrix_columns
);
217 op0
= new(ir
) ir_expression(ir_binop_mul
, vec_type
, op0
, op1
);
219 if (ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
) {
220 ir
->operation
= ir_unop_f2i
;
221 ir
->operands
[0] = op0
;
223 ir
->operation
= ir_unop_i2u
;
224 ir
->operands
[0] = new(ir
) ir_expression(ir_unop_f2i
, op0
);
226 ir
->operands
[1] = NULL
;
228 this->progress
= true;
232 lower_instructions_visitor::exp_to_exp2(ir_expression
*ir
)
234 ir_constant
*log2_e
= new(ir
) ir_constant(float(M_LOG2E
));
236 ir
->operation
= ir_unop_exp2
;
237 ir
->operands
[0] = new(ir
) ir_expression(ir_binop_mul
, ir
->operands
[0]->type
,
238 ir
->operands
[0], log2_e
);
239 this->progress
= true;
243 lower_instructions_visitor::pow_to_exp2(ir_expression
*ir
)
245 ir_expression
*const log2_x
=
246 new(ir
) ir_expression(ir_unop_log2
, ir
->operands
[0]->type
,
249 ir
->operation
= ir_unop_exp2
;
250 ir
->operands
[0] = new(ir
) ir_expression(ir_binop_mul
, ir
->operands
[1]->type
,
251 ir
->operands
[1], log2_x
);
252 ir
->operands
[1] = NULL
;
253 this->progress
= true;
257 lower_instructions_visitor::log_to_log2(ir_expression
*ir
)
259 ir
->operation
= ir_binop_mul
;
260 ir
->operands
[0] = new(ir
) ir_expression(ir_unop_log2
, ir
->operands
[0]->type
,
261 ir
->operands
[0], NULL
);
262 ir
->operands
[1] = new(ir
) ir_constant(float(1.0 / M_LOG2E
));
263 this->progress
= true;
267 lower_instructions_visitor::mod_to_fract(ir_expression
*ir
)
269 ir_variable
*temp
= new(ir
) ir_variable(ir
->operands
[1]->type
, "mod_b",
271 this->base_ir
->insert_before(temp
);
273 ir_assignment
*const assign
=
274 new(ir
) ir_assignment(new(ir
) ir_dereference_variable(temp
),
275 ir
->operands
[1], NULL
);
277 this->base_ir
->insert_before(assign
);
279 ir_expression
*const div_expr
=
280 new(ir
) ir_expression(ir_binop_div
, ir
->operands
[0]->type
,
282 new(ir
) ir_dereference_variable(temp
));
284 /* Don't generate new IR that would need to be lowered in an additional
287 if (lowering(DIV_TO_MUL_RCP
))
288 div_to_mul_rcp(div_expr
);
290 ir_rvalue
*expr
= new(ir
) ir_expression(ir_unop_fract
,
291 ir
->operands
[0]->type
,
295 ir
->operation
= ir_binop_mul
;
296 ir
->operands
[0] = new(ir
) ir_dereference_variable(temp
);
297 ir
->operands
[1] = expr
;
298 this->progress
= true;
302 lower_instructions_visitor::lrp_to_arith(ir_expression
*ir
)
304 /* (lrp x y a) -> x*(1-a) + y*a */
307 ir_variable
*temp
= new(ir
) ir_variable(ir
->operands
[2]->type
, "lrp_factor",
309 this->base_ir
->insert_before(temp
);
310 this->base_ir
->insert_before(assign(temp
, ir
->operands
[2]));
312 ir_constant
*one
= new(ir
) ir_constant(1.0f
);
314 ir
->operation
= ir_binop_add
;
315 ir
->operands
[0] = mul(ir
->operands
[0], sub(one
, temp
));
316 ir
->operands
[1] = mul(ir
->operands
[1], temp
);
317 ir
->operands
[2] = NULL
;
319 this->progress
= true;
323 lower_instructions_visitor::bitfield_insert_to_bfm_bfi(ir_expression
*ir
)
326 * ir_quadop_bitfield_insert base insert offset bits
328 * ir_triop_bfi (ir_binop_bfm bits offset) insert base
331 ir_rvalue
*base_expr
= ir
->operands
[0];
333 ir
->operation
= ir_triop_bfi
;
334 ir
->operands
[0] = new(ir
) ir_expression(ir_binop_bfm
,
335 ir
->type
->get_base_type(),
338 /* ir->operands[1] is still the value to insert. */
339 ir
->operands
[2] = base_expr
;
340 ir
->operands
[3] = NULL
;
342 this->progress
= true;
346 lower_instructions_visitor::ldexp_to_arith(ir_expression
*ir
)
349 * ir_binop_ldexp x exp
352 * extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
353 * resulting_biased_exp = extracted_biased_exp + exp;
355 * if (resulting_biased_exp < 1) {
356 * return copysign(0.0, x);
359 * return bitcast_u2f((bitcast_f2u(x) & sign_mantissa_mask) |
360 * lshift(i2u(resulting_biased_exp), exp_shift));
362 * which we can't actually implement as such, since the GLSL IR doesn't
363 * have vectorized if-statements. We actually implement it without branches
364 * using conditional-select:
366 * extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
367 * resulting_biased_exp = extracted_biased_exp + exp;
369 * is_not_zero_or_underflow = gequal(resulting_biased_exp, 1);
370 * x = csel(is_not_zero_or_underflow, x, copysign(0.0f, x));
371 * resulting_biased_exp = csel(is_not_zero_or_underflow,
372 * resulting_biased_exp, 0);
374 * return bitcast_u2f((bitcast_f2u(x) & sign_mantissa_mask) |
375 * lshift(i2u(resulting_biased_exp), exp_shift));
378 const unsigned vec_elem
= ir
->type
->vector_elements
;
381 const glsl_type
*ivec
= glsl_type::get_instance(GLSL_TYPE_INT
, vec_elem
, 1);
382 const glsl_type
*bvec
= glsl_type::get_instance(GLSL_TYPE_BOOL
, vec_elem
, 1);
385 ir_constant
*zeroi
= ir_constant::zero(ir
, ivec
);
387 ir_constant
*sign_mask
= new(ir
) ir_constant(0x80000000u
, vec_elem
);
389 ir_constant
*exp_shift
= new(ir
) ir_constant(23u, vec_elem
);
390 ir_constant
*exp_width
= new(ir
) ir_constant(8u, vec_elem
);
392 /* Temporary variables */
393 ir_variable
*x
= new(ir
) ir_variable(ir
->type
, "x", ir_var_temporary
);
394 ir_variable
*exp
= new(ir
) ir_variable(ivec
, "exp", ir_var_temporary
);
396 ir_variable
*zero_sign_x
= new(ir
) ir_variable(ir
->type
, "zero_sign_x",
399 ir_variable
*extracted_biased_exp
=
400 new(ir
) ir_variable(ivec
, "extracted_biased_exp", ir_var_temporary
);
401 ir_variable
*resulting_biased_exp
=
402 new(ir
) ir_variable(ivec
, "resulting_biased_exp", ir_var_temporary
);
404 ir_variable
*is_not_zero_or_underflow
=
405 new(ir
) ir_variable(bvec
, "is_not_zero_or_underflow", ir_var_temporary
);
407 ir_instruction
&i
= *base_ir
;
409 /* Copy <x> and <exp> arguments. */
411 i
.insert_before(assign(x
, ir
->operands
[0]));
412 i
.insert_before(exp
);
413 i
.insert_before(assign(exp
, ir
->operands
[1]));
415 /* Extract the biased exponent from <x>. */
416 i
.insert_before(extracted_biased_exp
);
417 i
.insert_before(assign(extracted_biased_exp
,
418 rshift(bitcast_f2i(abs(x
)), exp_shift
)));
420 i
.insert_before(resulting_biased_exp
);
421 i
.insert_before(assign(resulting_biased_exp
,
422 add(extracted_biased_exp
, exp
)));
424 /* Test if result is ±0.0, subnormal, or underflow by checking if the
425 * resulting biased exponent would be less than 0x1. If so, the result is
426 * 0.0 with the sign of x. (Actually, invert the conditions so that
427 * immediate values are the second arguments, which is better for i965)
429 i
.insert_before(zero_sign_x
);
430 i
.insert_before(assign(zero_sign_x
,
431 bitcast_u2f(bit_and(bitcast_f2u(x
), sign_mask
))));
433 i
.insert_before(is_not_zero_or_underflow
);
434 i
.insert_before(assign(is_not_zero_or_underflow
,
435 gequal(resulting_biased_exp
,
436 new(ir
) ir_constant(0x1, vec_elem
))));
437 i
.insert_before(assign(x
, csel(is_not_zero_or_underflow
,
439 i
.insert_before(assign(resulting_biased_exp
,
440 csel(is_not_zero_or_underflow
,
441 resulting_biased_exp
, zeroi
)));
443 /* We could test for overflows by checking if the resulting biased exponent
444 * would be greater than 0xFE. Turns out we don't need to because the GLSL
447 * "If this product is too large to be represented in the
448 * floating-point type, the result is undefined."
451 ir_constant
*exp_shift_clone
= exp_shift
->clone(ir
, NULL
);
452 ir
->operation
= ir_unop_bitcast_i2f
;
453 ir
->operands
[0] = bitfield_insert(bitcast_f2i(x
), resulting_biased_exp
,
454 exp_shift_clone
, exp_width
);
455 ir
->operands
[1] = NULL
;
457 /* Don't generate new IR that would need to be lowered in an additional
460 if (lowering(BITFIELD_INSERT_TO_BFM_BFI
))
461 bitfield_insert_to_bfm_bfi(ir
->operands
[0]->as_expression());
463 this->progress
= true;
467 lower_instructions_visitor::visit_leave(ir_expression
*ir
)
469 switch (ir
->operation
) {
471 if (lowering(SUB_TO_ADD_NEG
))
476 if (ir
->operands
[1]->type
->is_integer() && lowering(INT_DIV_TO_MUL_RCP
))
477 int_div_to_mul_rcp(ir
);
478 else if (ir
->operands
[1]->type
->is_float() && lowering(DIV_TO_MUL_RCP
))
483 if (lowering(EXP_TO_EXP2
))
488 if (lowering(LOG_TO_LOG2
))
493 if (lowering(MOD_TO_FRACT
) && ir
->type
->is_float())
498 if (lowering(POW_TO_EXP2
))
503 if (lowering(LRP_TO_ARITH
))
507 case ir_quadop_bitfield_insert
:
508 if (lowering(BITFIELD_INSERT_TO_BFM_BFI
))
509 bitfield_insert_to_bfm_bfi(ir
);
513 if (lowering(LDEXP_TO_ARITH
))
518 return visit_continue
;
521 return visit_continue
;