1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \file lower_instructions.cpp
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Many GPUs lack native instructions for certain expression operations, and
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * must replace them with some other expression tree.  This pass lowers some
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the most common cases, allowing the lowering code to be implemented once
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * rather than in each driver backend.
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Currently supported transformations:
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - SUB_TO_ADD_NEG
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - DIV_TO_MUL_RCP
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - INT_DIV_TO_MUL_RCP
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - EXP_TO_EXP2
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - POW_TO_EXP2
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - LOG_TO_LOG2
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * - MOD_TO_FRACT
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SUB_TO_ADD_NEG:
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ---------------
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Breaks an ir_binop_sub expression down to add(op0, neg(op1))
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * This simplifies expression reassociation, and for many backends
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * there is no subtract operation separate from adding the negation.
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * For backends with native subtract operations, they will probably
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * want to recognize add(op0, neg(op1)) or the other way around to
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * produce a subtract anyway.
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DIV_TO_MUL_RCP and INT_DIV_TO_MUL_RCP:
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * --------------------------------------
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Breaks an ir_binop_div expression down to op0 * (rcp(op1)).
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Many GPUs don't have a divide instruction (945 and 965 included),
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * but they do have an RCP instruction to compute an approximate
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * reciprocal.  By breaking the operation down, constant reciprocals
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * can get constant folded.
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DIV_TO_MUL_RCP only lowers floating point division; INT_DIV_TO_MUL_RCP
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * handles the integer case, converting to and from floating point so that
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * RCP is possible.
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * EXP_TO_EXP2 and LOG_TO_LOG2:
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ----------------------------
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Many GPUs don't have a base e log or exponent instruction, but they
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * do have base 2 versions, so this pass converts exp and log to exp2
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and log2 operations.
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * POW_TO_EXP2:
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * -----------
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Many older GPUs don't have an x**y instruction.  For these GPUs, convert
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * x**y to 2**(y * log2(x)).
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MOD_TO_FRACT:
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * -------------
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Breaks an ir_binop_mod expression down to (op1 * fract(op0 / op1))
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Many GPUs don't have a MOD instruction (945 and 965 included), and
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * if we have to break it down like this anyway, it gives an
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * opportunity to do things like constant fold the (1.0 / op1) easily.
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/core.h" /* for M_LOG2E */
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl_types.h"
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h"
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_optimization.h"
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass lower_instructions_visitor : public ir_hierarchical_visitor {
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   lower_instructions_visitor(unsigned lower)
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      : progress(false), lower(lower) { }
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_visitor_status visit_leave(ir_expression *);
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool progress;
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprivate:
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned lower; /** Bitfield of which operations to lower */
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void sub_to_add_neg(ir_expression *);
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void div_to_mul_rcp(ir_expression *);
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void int_div_to_mul_rcp(ir_expression *);
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void mod_to_fract(ir_expression *);
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void exp_to_exp2(ir_expression *);
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void pow_to_exp2(ir_expression *);
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void log_to_log2(ir_expression *);
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Determine if a particular type of lowering should occur
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define lowering(x) (this->lower & x)
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions(exec_list *instructions, unsigned what_to_lower)
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   lower_instructions_visitor v(what_to_lower);
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   visit_list_elements(&v, instructions);
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return v.progress;
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::sub_to_add_neg(ir_expression *ir)
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operation = ir_binop_add;
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[1] = new(ir) ir_expression(ir_unop_neg, ir->operands[1]->type,
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   ir->operands[1], NULL);
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::div_to_mul_rcp(ir_expression *ir)
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(ir->operands[1]->type->is_float());
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* New expression for the 1.0 / op1 */
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_rvalue *expr;
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   expr = new(ir) ir_expression(ir_unop_rcp,
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				ir->operands[1]->type,
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				ir->operands[1]);
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* op0 / op1 -> op0 * (1.0 / op1) */
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operation = ir_binop_mul;
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[1] = expr;
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::int_div_to_mul_rcp(ir_expression *ir)
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(ir->operands[1]->type->is_integer());
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Be careful with integer division -- we need to do it as a
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * float and re-truncate, since rcp(n > 1) of an integer would
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * just be 0.
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_rvalue *op0, *op1;
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const struct glsl_type *vec_type;
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      ir->operands[1]->type->vector_elements,
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      ir->operands[1]->type->matrix_columns);
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ir->operands[1]->type->base_type == GLSL_TYPE_INT)
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      op1 = new(ir) ir_expression(ir_unop_i2f, vec_type, ir->operands[1], NULL);
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      op1 = new(ir) ir_expression(ir_unop_u2f, vec_type, ir->operands[1], NULL);
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   op1 = new(ir) ir_expression(ir_unop_rcp, op1->type, op1, NULL);
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      ir->operands[0]->type->vector_elements,
176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      ir->operands[0]->type->matrix_columns);
177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ir->operands[0]->type->base_type == GLSL_TYPE_INT)
179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      op0 = new(ir) ir_expression(ir_unop_i2f, vec_type, ir->operands[0], NULL);
180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else
181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      op0 = new(ir) ir_expression(ir_unop_u2f, vec_type, ir->operands[0], NULL);
182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      ir->type->vector_elements,
185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				      ir->type->matrix_columns);
186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   op0 = new(ir) ir_expression(ir_binop_mul, vec_type, op0, op1);
188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ir->operands[1]->type->base_type == GLSL_TYPE_INT) {
190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir->operation = ir_unop_f2i;
191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir->operands[0] = op0;
192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   } else {
193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir->operation = ir_unop_i2u;
194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir->operands[0] = new(ir) ir_expression(ir_unop_f2i, op0);
195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[1] = NULL;
197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::exp_to_exp2(ir_expression *ir)
203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_constant *log2_e = new(ir) ir_constant(float(M_LOG2E));
205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operation = ir_unop_exp2;
207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[0] = new(ir) ir_expression(ir_binop_mul, ir->operands[0]->type,
208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   ir->operands[0], log2_e);
209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::pow_to_exp2(ir_expression *ir)
214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_expression *const log2_x =
216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      new(ir) ir_expression(ir_unop_log2, ir->operands[0]->type,
217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			    ir->operands[0]);
218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operation = ir_unop_exp2;
220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[0] = new(ir) ir_expression(ir_binop_mul, ir->operands[1]->type,
221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   ir->operands[1], log2_x);
222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[1] = NULL;
223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::log_to_log2(ir_expression *ir)
228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operation = ir_binop_mul;
230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[0] = new(ir) ir_expression(ir_unop_log2, ir->operands[0]->type,
231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   ir->operands[0], NULL);
232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[1] = new(ir) ir_constant(float(1.0 / M_LOG2E));
233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::mod_to_fract(ir_expression *ir)
238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_variable *temp = new(ir) ir_variable(ir->operands[1]->type, "mod_b",
240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   ir_var_temporary);
241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->base_ir->insert_before(temp);
242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_assignment *const assign =
244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      new(ir) ir_assignment(new(ir) ir_dereference_variable(temp),
245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			    ir->operands[1], NULL);
246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->base_ir->insert_before(assign);
248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_expression *const div_expr =
250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      new(ir) ir_expression(ir_binop_div, ir->operands[0]->type,
251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			    ir->operands[0],
252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			    new(ir) ir_dereference_variable(temp));
253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Don't generate new IR that would need to be lowered in an additional
255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * pass.
256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (lowering(DIV_TO_MUL_RCP))
258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      div_to_mul_rcp(div_expr);
259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_rvalue *expr = new(ir) ir_expression(ir_unop_fract,
261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   ir->operands[0]->type,
262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   div_expr,
263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org					   NULL);
264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operation = ir_binop_mul;
266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[0] = new(ir) ir_dereference_variable(temp);
267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir->operands[1] = expr;
268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->progress = true;
269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status
272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orglower_instructions_visitor::visit_leave(ir_expression *ir)
273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   switch (ir->operation) {
275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case ir_binop_sub:
276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (lowering(SUB_TO_ADD_NEG))
277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 sub_to_add_neg(ir);
278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case ir_binop_div:
281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (ir->operands[1]->type->is_integer() && lowering(INT_DIV_TO_MUL_RCP))
282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 int_div_to_mul_rcp(ir);
283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else if (ir->operands[1]->type->is_float() && lowering(DIV_TO_MUL_RCP))
284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 div_to_mul_rcp(ir);
285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case ir_unop_exp:
288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (lowering(EXP_TO_EXP2))
289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 exp_to_exp2(ir);
290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case ir_unop_log:
293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (lowering(LOG_TO_LOG2))
294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 log_to_log2(ir);
295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case ir_binop_mod:
298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (lowering(MOD_TO_FRACT) && ir->type->is_float())
299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 mod_to_fract(ir);
300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case ir_binop_pow:
303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (lowering(POW_TO_EXP2))
304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 pow_to_exp2(ir);
305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   default:
308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return visit_continue;
309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return visit_continue;
312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
313