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
41 * - BITFIELD_INSERT_TO_BFM_BFI
47 * Breaks an ir_binop_sub expression down to add(op0, neg(op1))
49 * This simplifies expression reassociation, and for many backends
50 * there is no subtract operation separate from adding the negation.
51 * For backends with native subtract operations, they will probably
52 * want to recognize add(op0, neg(op1)) or the other way around to
53 * produce a subtract anyway.
55 * DIV_TO_MUL_RCP and INT_DIV_TO_MUL_RCP:
56 * --------------------------------------
57 * Breaks an ir_binop_div expression down to op0 * (rcp(op1)).
59 * Many GPUs don't have a divide instruction (945 and 965 included),
60 * but they do have an RCP instruction to compute an approximate
61 * reciprocal. By breaking the operation down, constant reciprocals
62 * can get constant folded.
64 * DIV_TO_MUL_RCP only lowers floating point division; INT_DIV_TO_MUL_RCP
65 * handles the integer case, converting to and from floating point so that
68 * EXP_TO_EXP2 and LOG_TO_LOG2:
69 * ----------------------------
70 * Many GPUs don't have a base e log or exponent instruction, but they
71 * do have base 2 versions, so this pass converts exp and log to exp2
72 * and log2 operations.
76 * Many older GPUs don't have an x**y instruction. For these GPUs, convert
77 * x**y to 2**(y * log2(x)).
81 * Breaks an ir_binop_mod expression down to (op1 * fract(op0 / op1))
83 * Many GPUs don't have a MOD instruction (945 and 965 included), and
84 * if we have to break it down like this anyway, it gives an
85 * opportunity to do things like constant fold the (1.0 / op1) easily.
89 * Converts ir_binop_ldexp to arithmetic and bit operations.
91 * BITFIELD_INSERT_TO_BFM_BFI:
92 * ---------------------------
93 * Breaks ir_quadop_bitfield_insert into ir_binop_bfm (bitfield mask) and
94 * ir_triop_bfi (bitfield insert).
96 * Many GPUs implement the bitfieldInsert() built-in from ARB_gpu_shader_5
97 * with a pair of instructions.
101 * Converts ir_carry into (x + y) < x.
105 * Converts ir_borrow into (x < y).
109 #include "main/core.h" /* for M_LOG2E */
110 #include "glsl_types.h"
112 #include "ir_builder.h"
113 #include "ir_optimization.h"
115 using namespace ir_builder
;
119 class lower_instructions_visitor
: public ir_hierarchical_visitor
{
121 lower_instructions_visitor(unsigned lower
)
122 : progress(false), lower(lower
) { }
124 ir_visitor_status
visit_leave(ir_expression
*);
129 unsigned lower
; /** Bitfield of which operations to lower */
131 void sub_to_add_neg(ir_expression
*);
132 void div_to_mul_rcp(ir_expression
*);
133 void int_div_to_mul_rcp(ir_expression
*);
134 void mod_to_fract(ir_expression
*);
135 void exp_to_exp2(ir_expression
*);
136 void pow_to_exp2(ir_expression
*);
137 void log_to_log2(ir_expression
*);
138 void bitfield_insert_to_bfm_bfi(ir_expression
*);
139 void ldexp_to_arith(ir_expression
*);
140 void carry_to_arith(ir_expression
*);
141 void borrow_to_arith(ir_expression
*);
144 } /* anonymous namespace */
147 * Determine if a particular type of lowering should occur
149 #define lowering(x) (this->lower & x)
152 lower_instructions(exec_list
*instructions
, unsigned what_to_lower
)
154 lower_instructions_visitor
v(what_to_lower
);
156 visit_list_elements(&v
, instructions
);
161 lower_instructions_visitor::sub_to_add_neg(ir_expression
*ir
)
163 ir
->operation
= ir_binop_add
;
164 ir
->operands
[1] = new(ir
) ir_expression(ir_unop_neg
, ir
->operands
[1]->type
,
165 ir
->operands
[1], NULL
);
166 this->progress
= true;
170 lower_instructions_visitor::div_to_mul_rcp(ir_expression
*ir
)
172 assert(ir
->operands
[1]->type
->is_float());
174 /* New expression for the 1.0 / op1 */
176 expr
= new(ir
) ir_expression(ir_unop_rcp
,
177 ir
->operands
[1]->type
,
180 /* op0 / op1 -> op0 * (1.0 / op1) */
181 ir
->operation
= ir_binop_mul
;
182 ir
->operands
[1] = expr
;
184 this->progress
= true;
188 lower_instructions_visitor::int_div_to_mul_rcp(ir_expression
*ir
)
190 assert(ir
->operands
[1]->type
->is_integer());
192 /* Be careful with integer division -- we need to do it as a
193 * float and re-truncate, since rcp(n > 1) of an integer would
196 ir_rvalue
*op0
, *op1
;
197 const struct glsl_type
*vec_type
;
199 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
200 ir
->operands
[1]->type
->vector_elements
,
201 ir
->operands
[1]->type
->matrix_columns
);
203 if (ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
)
204 op1
= new(ir
) ir_expression(ir_unop_i2f
, vec_type
, ir
->operands
[1], NULL
);
206 op1
= new(ir
) ir_expression(ir_unop_u2f
, vec_type
, ir
->operands
[1], NULL
);
208 op1
= new(ir
) ir_expression(ir_unop_rcp
, op1
->type
, op1
, NULL
);
210 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
211 ir
->operands
[0]->type
->vector_elements
,
212 ir
->operands
[0]->type
->matrix_columns
);
214 if (ir
->operands
[0]->type
->base_type
== GLSL_TYPE_INT
)
215 op0
= new(ir
) ir_expression(ir_unop_i2f
, vec_type
, ir
->operands
[0], NULL
);
217 op0
= new(ir
) ir_expression(ir_unop_u2f
, vec_type
, ir
->operands
[0], NULL
);
219 vec_type
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
220 ir
->type
->vector_elements
,
221 ir
->type
->matrix_columns
);
223 op0
= new(ir
) ir_expression(ir_binop_mul
, vec_type
, op0
, op1
);
225 if (ir
->operands
[1]->type
->base_type
== GLSL_TYPE_INT
) {
226 ir
->operation
= ir_unop_f2i
;
227 ir
->operands
[0] = op0
;
229 ir
->operation
= ir_unop_i2u
;
230 ir
->operands
[0] = new(ir
) ir_expression(ir_unop_f2i
, op0
);
232 ir
->operands
[1] = NULL
;
234 this->progress
= true;
238 lower_instructions_visitor::exp_to_exp2(ir_expression
*ir
)
240 ir_constant
*log2_e
= new(ir
) ir_constant(float(M_LOG2E
));
242 ir
->operation
= ir_unop_exp2
;
243 ir
->operands
[0] = new(ir
) ir_expression(ir_binop_mul
, ir
->operands
[0]->type
,
244 ir
->operands
[0], log2_e
);
245 this->progress
= true;
249 lower_instructions_visitor::pow_to_exp2(ir_expression
*ir
)
251 ir_expression
*const log2_x
=
252 new(ir
) ir_expression(ir_unop_log2
, ir
->operands
[0]->type
,
255 ir
->operation
= ir_unop_exp2
;
256 ir
->operands
[0] = new(ir
) ir_expression(ir_binop_mul
, ir
->operands
[1]->type
,
257 ir
->operands
[1], log2_x
);
258 ir
->operands
[1] = NULL
;
259 this->progress
= true;
263 lower_instructions_visitor::log_to_log2(ir_expression
*ir
)
265 ir
->operation
= ir_binop_mul
;
266 ir
->operands
[0] = new(ir
) ir_expression(ir_unop_log2
, ir
->operands
[0]->type
,
267 ir
->operands
[0], NULL
);
268 ir
->operands
[1] = new(ir
) ir_constant(float(1.0 / M_LOG2E
));
269 this->progress
= true;
273 lower_instructions_visitor::mod_to_fract(ir_expression
*ir
)
275 ir_variable
*temp
= new(ir
) ir_variable(ir
->operands
[1]->type
, "mod_b",
277 this->base_ir
->insert_before(temp
);
279 ir_assignment
*const assign
=
280 new(ir
) ir_assignment(new(ir
) ir_dereference_variable(temp
),
281 ir
->operands
[1], NULL
);
283 this->base_ir
->insert_before(assign
);
285 ir_expression
*const div_expr
=
286 new(ir
) ir_expression(ir_binop_div
, ir
->operands
[0]->type
,
288 new(ir
) ir_dereference_variable(temp
));
290 /* Don't generate new IR that would need to be lowered in an additional
293 if (lowering(DIV_TO_MUL_RCP
))
294 div_to_mul_rcp(div_expr
);
296 ir_rvalue
*expr
= new(ir
) ir_expression(ir_unop_fract
,
297 ir
->operands
[0]->type
,
301 ir
->operation
= ir_binop_mul
;
302 ir
->operands
[0] = new(ir
) ir_dereference_variable(temp
);
303 ir
->operands
[1] = expr
;
304 this->progress
= true;
308 lower_instructions_visitor::bitfield_insert_to_bfm_bfi(ir_expression
*ir
)
311 * ir_quadop_bitfield_insert base insert offset bits
313 * ir_triop_bfi (ir_binop_bfm bits offset) insert base
316 ir_rvalue
*base_expr
= ir
->operands
[0];
318 ir
->operation
= ir_triop_bfi
;
319 ir
->operands
[0] = new(ir
) ir_expression(ir_binop_bfm
,
320 ir
->type
->get_base_type(),
323 /* ir->operands[1] is still the value to insert. */
324 ir
->operands
[2] = base_expr
;
325 ir
->operands
[3] = NULL
;
327 this->progress
= true;
331 lower_instructions_visitor::ldexp_to_arith(ir_expression
*ir
)
334 * ir_binop_ldexp x exp
337 * extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
338 * resulting_biased_exp = extracted_biased_exp + exp;
340 * if (resulting_biased_exp < 1) {
341 * return copysign(0.0, x);
344 * return bitcast_u2f((bitcast_f2u(x) & sign_mantissa_mask) |
345 * lshift(i2u(resulting_biased_exp), exp_shift));
347 * which we can't actually implement as such, since the GLSL IR doesn't
348 * have vectorized if-statements. We actually implement it without branches
349 * using conditional-select:
351 * extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
352 * resulting_biased_exp = extracted_biased_exp + exp;
354 * is_not_zero_or_underflow = gequal(resulting_biased_exp, 1);
355 * x = csel(is_not_zero_or_underflow, x, copysign(0.0f, x));
356 * resulting_biased_exp = csel(is_not_zero_or_underflow,
357 * resulting_biased_exp, 0);
359 * return bitcast_u2f((bitcast_f2u(x) & sign_mantissa_mask) |
360 * lshift(i2u(resulting_biased_exp), exp_shift));
363 const unsigned vec_elem
= ir
->type
->vector_elements
;
366 const glsl_type
*ivec
= glsl_type::get_instance(GLSL_TYPE_INT
, vec_elem
, 1);
367 const glsl_type
*bvec
= glsl_type::get_instance(GLSL_TYPE_BOOL
, vec_elem
, 1);
370 ir_constant
*zeroi
= ir_constant::zero(ir
, ivec
);
372 ir_constant
*sign_mask
= new(ir
) ir_constant(0x80000000u
, vec_elem
);
374 ir_constant
*exp_shift
= new(ir
) ir_constant(23);
375 ir_constant
*exp_width
= new(ir
) ir_constant(8);
377 /* Temporary variables */
378 ir_variable
*x
= new(ir
) ir_variable(ir
->type
, "x", ir_var_temporary
);
379 ir_variable
*exp
= new(ir
) ir_variable(ivec
, "exp", ir_var_temporary
);
381 ir_variable
*zero_sign_x
= new(ir
) ir_variable(ir
->type
, "zero_sign_x",
384 ir_variable
*extracted_biased_exp
=
385 new(ir
) ir_variable(ivec
, "extracted_biased_exp", ir_var_temporary
);
386 ir_variable
*resulting_biased_exp
=
387 new(ir
) ir_variable(ivec
, "resulting_biased_exp", ir_var_temporary
);
389 ir_variable
*is_not_zero_or_underflow
=
390 new(ir
) ir_variable(bvec
, "is_not_zero_or_underflow", ir_var_temporary
);
392 ir_instruction
&i
= *base_ir
;
394 /* Copy <x> and <exp> arguments. */
396 i
.insert_before(assign(x
, ir
->operands
[0]));
397 i
.insert_before(exp
);
398 i
.insert_before(assign(exp
, ir
->operands
[1]));
400 /* Extract the biased exponent from <x>. */
401 i
.insert_before(extracted_biased_exp
);
402 i
.insert_before(assign(extracted_biased_exp
,
403 rshift(bitcast_f2i(abs(x
)), exp_shift
)));
405 i
.insert_before(resulting_biased_exp
);
406 i
.insert_before(assign(resulting_biased_exp
,
407 add(extracted_biased_exp
, exp
)));
409 /* Test if result is ±0.0, subnormal, or underflow by checking if the
410 * resulting biased exponent would be less than 0x1. If so, the result is
411 * 0.0 with the sign of x. (Actually, invert the conditions so that
412 * immediate values are the second arguments, which is better for i965)
414 i
.insert_before(zero_sign_x
);
415 i
.insert_before(assign(zero_sign_x
,
416 bitcast_u2f(bit_and(bitcast_f2u(x
), sign_mask
))));
418 i
.insert_before(is_not_zero_or_underflow
);
419 i
.insert_before(assign(is_not_zero_or_underflow
,
420 gequal(resulting_biased_exp
,
421 new(ir
) ir_constant(0x1, vec_elem
))));
422 i
.insert_before(assign(x
, csel(is_not_zero_or_underflow
,
424 i
.insert_before(assign(resulting_biased_exp
,
425 csel(is_not_zero_or_underflow
,
426 resulting_biased_exp
, zeroi
)));
428 /* We could test for overflows by checking if the resulting biased exponent
429 * would be greater than 0xFE. Turns out we don't need to because the GLSL
432 * "If this product is too large to be represented in the
433 * floating-point type, the result is undefined."
436 ir_constant
*exp_shift_clone
= exp_shift
->clone(ir
, NULL
);
437 ir
->operation
= ir_unop_bitcast_i2f
;
438 ir
->operands
[0] = bitfield_insert(bitcast_f2i(x
), resulting_biased_exp
,
439 exp_shift_clone
, exp_width
);
440 ir
->operands
[1] = NULL
;
442 /* Don't generate new IR that would need to be lowered in an additional
445 if (lowering(BITFIELD_INSERT_TO_BFM_BFI
))
446 bitfield_insert_to_bfm_bfi(ir
->operands
[0]->as_expression());
448 this->progress
= true;
452 lower_instructions_visitor::carry_to_arith(ir_expression
*ir
)
457 * sum = ir_binop_add x y
458 * bcarry = ir_binop_less sum x
459 * carry = ir_unop_b2i bcarry
462 ir_rvalue
*x_clone
= ir
->operands
[0]->clone(ir
, NULL
);
463 ir
->operation
= ir_unop_i2u
;
464 ir
->operands
[0] = b2i(less(add(ir
->operands
[0], ir
->operands
[1]), x_clone
));
465 ir
->operands
[1] = NULL
;
467 this->progress
= true;
471 lower_instructions_visitor::borrow_to_arith(ir_expression
*ir
)
474 * ir_binop_borrow x y
476 * bcarry = ir_binop_less x y
477 * carry = ir_unop_b2i bcarry
480 ir
->operation
= ir_unop_i2u
;
481 ir
->operands
[0] = b2i(less(ir
->operands
[0], ir
->operands
[1]));
482 ir
->operands
[1] = NULL
;
484 this->progress
= true;
488 lower_instructions_visitor::visit_leave(ir_expression
*ir
)
490 switch (ir
->operation
) {
492 if (lowering(SUB_TO_ADD_NEG
))
497 if (ir
->operands
[1]->type
->is_integer() && lowering(INT_DIV_TO_MUL_RCP
))
498 int_div_to_mul_rcp(ir
);
499 else if (ir
->operands
[1]->type
->is_float() && lowering(DIV_TO_MUL_RCP
))
504 if (lowering(EXP_TO_EXP2
))
509 if (lowering(LOG_TO_LOG2
))
514 if (lowering(MOD_TO_FRACT
) && ir
->type
->is_float())
519 if (lowering(POW_TO_EXP2
))
523 case ir_quadop_bitfield_insert
:
524 if (lowering(BITFIELD_INSERT_TO_BFM_BFI
))
525 bitfield_insert_to_bfm_bfi(ir
);
529 if (lowering(LDEXP_TO_ARITH
))
534 if (lowering(CARRY_TO_ARITH
))
538 case ir_binop_borrow
:
539 if (lowering(BORROW_TO_ARITH
))
544 return visit_continue
;
547 return visit_continue
;