101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2010 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg/**
2501e04c3fSmrg * \file ir_optimization.h
2601e04c3fSmrg *
2701e04c3fSmrg * Prototypes for optimization passes to be called by the compiler and drivers.
2801e04c3fSmrg */
2901e04c3fSmrg
3001e04c3fSmrg#ifndef GLSL_IR_OPTIMIZATION_H
3101e04c3fSmrg#define GLSL_IR_OPTIMIZATION_H
3201e04c3fSmrg
3301e04c3fSmrgstruct gl_linked_shader;
3401e04c3fSmrgstruct gl_shader_program;
3501e04c3fSmrg
3601e04c3fSmrg/* Operations for lower_instructions() */
3701e04c3fSmrg#define SUB_TO_ADD_NEG     0x01
3801e04c3fSmrg#define FDIV_TO_MUL_RCP    0x02
3901e04c3fSmrg#define EXP_TO_EXP2        0x04
4001e04c3fSmrg#define POW_TO_EXP2        0x08
4101e04c3fSmrg#define LOG_TO_LOG2        0x10
4201e04c3fSmrg#define MOD_TO_FLOOR       0x20
4301e04c3fSmrg#define INT_DIV_TO_MUL_RCP 0x40
4401e04c3fSmrg#define LDEXP_TO_ARITH     0x80
4501e04c3fSmrg#define CARRY_TO_ARITH     0x100
4601e04c3fSmrg#define BORROW_TO_ARITH    0x200
4701e04c3fSmrg#define SAT_TO_CLAMP       0x400
4801e04c3fSmrg#define DOPS_TO_DFRAC      0x800
4901e04c3fSmrg#define DFREXP_DLDEXP_TO_ARITH    0x1000
5001e04c3fSmrg#define BIT_COUNT_TO_MATH         0x02000
5101e04c3fSmrg#define EXTRACT_TO_SHIFTS         0x04000
5201e04c3fSmrg#define INSERT_TO_SHIFTS          0x08000
5301e04c3fSmrg#define REVERSE_TO_SHIFTS         0x10000
5401e04c3fSmrg#define FIND_LSB_TO_FLOAT_CAST    0x20000
5501e04c3fSmrg#define FIND_MSB_TO_FLOAT_CAST    0x40000
5601e04c3fSmrg#define IMUL_HIGH_TO_MUL          0x80000
5701e04c3fSmrg#define DDIV_TO_MUL_RCP           0x100000
5801e04c3fSmrg#define DIV_TO_MUL_RCP            (FDIV_TO_MUL_RCP | DDIV_TO_MUL_RCP)
5901e04c3fSmrg#define SQRT_TO_ABS_SQRT          0x200000
607e102996Smaya#define MUL64_TO_MUL_AND_MUL_HIGH 0x400000
6101e04c3fSmrg
6201e04c3fSmrg/* Opertaions for lower_64bit_integer_instructions() */
6301e04c3fSmrg#define MUL64                     (1U << 0)
6401e04c3fSmrg#define SIGN64                    (1U << 1)
6501e04c3fSmrg#define DIV64                     (1U << 2)
6601e04c3fSmrg#define MOD64                     (1U << 3)
6701e04c3fSmrg
6801e04c3fSmrg/**
6901e04c3fSmrg * \see class lower_packing_builtins_visitor
7001e04c3fSmrg */
7101e04c3fSmrgenum lower_packing_builtins_op {
7201e04c3fSmrg   LOWER_PACK_UNPACK_NONE               = 0x0000,
7301e04c3fSmrg
7401e04c3fSmrg   LOWER_PACK_SNORM_2x16                = 0x0001,
7501e04c3fSmrg   LOWER_UNPACK_SNORM_2x16              = 0x0002,
7601e04c3fSmrg
7701e04c3fSmrg   LOWER_PACK_UNORM_2x16                = 0x0004,
7801e04c3fSmrg   LOWER_UNPACK_UNORM_2x16              = 0x0008,
7901e04c3fSmrg
8001e04c3fSmrg   LOWER_PACK_HALF_2x16                 = 0x0010,
8101e04c3fSmrg   LOWER_UNPACK_HALF_2x16               = 0x0020,
8201e04c3fSmrg
8301e04c3fSmrg   LOWER_PACK_SNORM_4x8                 = 0x0040,
8401e04c3fSmrg   LOWER_UNPACK_SNORM_4x8               = 0x0080,
8501e04c3fSmrg
8601e04c3fSmrg   LOWER_PACK_UNORM_4x8                 = 0x0100,
8701e04c3fSmrg   LOWER_UNPACK_UNORM_4x8               = 0x0200,
8801e04c3fSmrg
8901e04c3fSmrg   LOWER_PACK_USE_BFI                   = 0x0400,
9001e04c3fSmrg   LOWER_PACK_USE_BFE                   = 0x0800,
9101e04c3fSmrg};
9201e04c3fSmrg
9301e04c3fSmrgbool do_common_optimization(exec_list *ir, bool linked,
9401e04c3fSmrg			    bool uniform_locations_assigned,
9501e04c3fSmrg                            const struct gl_shader_compiler_options *options,
9601e04c3fSmrg                            bool native_integers);
9701e04c3fSmrg
9801e04c3fSmrgbool ir_constant_fold(ir_rvalue **rvalue);
9901e04c3fSmrg
10001e04c3fSmrgbool do_rebalance_tree(exec_list *instructions);
10101e04c3fSmrgbool do_algebraic(exec_list *instructions, bool native_integers,
10201e04c3fSmrg                  const struct gl_shader_compiler_options *options);
10301e04c3fSmrgbool opt_conditional_discard(exec_list *instructions);
10401e04c3fSmrgbool do_constant_folding(exec_list *instructions);
10501e04c3fSmrgbool do_constant_variable(exec_list *instructions);
10601e04c3fSmrgbool do_constant_variable_unlinked(exec_list *instructions);
10701e04c3fSmrgbool do_copy_propagation_elements(exec_list *instructions);
10801e04c3fSmrgbool do_constant_propagation(exec_list *instructions);
10901e04c3fSmrgvoid do_dead_builtin_varyings(struct gl_context *ctx,
11001e04c3fSmrg                              gl_linked_shader *producer,
11101e04c3fSmrg                              gl_linked_shader *consumer,
11201e04c3fSmrg                              unsigned num_tfeedback_decls,
11301e04c3fSmrg                              class tfeedback_decl *tfeedback_decls);
11401e04c3fSmrgbool do_dead_code(exec_list *instructions, bool uniform_locations_assigned);
11501e04c3fSmrgbool do_dead_code_local(exec_list *instructions);
11601e04c3fSmrgbool do_dead_code_unlinked(exec_list *instructions);
11701e04c3fSmrgbool do_dead_functions(exec_list *instructions);
11801e04c3fSmrgbool opt_flip_matrices(exec_list *instructions);
11901e04c3fSmrgbool do_function_inlining(exec_list *instructions);
12001e04c3fSmrgbool do_lower_jumps(exec_list *instructions, bool pull_out_jumps = true, bool lower_sub_return = true, bool lower_main_return = false, bool lower_continue = false, bool lower_break = false);
12101e04c3fSmrgbool do_if_simplification(exec_list *instructions);
12201e04c3fSmrgbool opt_flatten_nested_if_blocks(exec_list *instructions);
12301e04c3fSmrgbool do_discard_simplification(exec_list *instructions);
12401e04c3fSmrgbool lower_if_to_cond_assign(gl_shader_stage stage, exec_list *instructions,
12501e04c3fSmrg                             unsigned max_depth = 0, unsigned min_branch_cost = 0);
12601e04c3fSmrgbool do_mat_op_to_vec(exec_list *instructions);
12701e04c3fSmrgbool do_minmax_prune(exec_list *instructions);
12801e04c3fSmrgbool do_structure_splitting(exec_list *instructions);
12901e04c3fSmrgbool optimize_swizzles(exec_list *instructions);
13001e04c3fSmrgbool do_vectorize(exec_list *instructions);
13101e04c3fSmrgbool do_tree_grafting(exec_list *instructions);
13201e04c3fSmrgbool do_vec_index_to_cond_assign(exec_list *instructions);
13301e04c3fSmrgbool do_vec_index_to_swizzle(exec_list *instructions);
13401e04c3fSmrgbool lower_discard(exec_list *instructions);
13501e04c3fSmrgvoid lower_discard_flow(exec_list *instructions);
13601e04c3fSmrgbool lower_instructions(exec_list *instructions, unsigned what_to_lower);
13701e04c3fSmrgbool lower_variable_index_to_cond_assign(gl_shader_stage stage,
13801e04c3fSmrg    exec_list *instructions, bool lower_input, bool lower_output,
13901e04c3fSmrg    bool lower_temp, bool lower_uniform);
14001e04c3fSmrgbool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz);
1417ec681f3Smrgbool lower_const_arrays_to_uniforms(exec_list *instructions, unsigned stage, unsigned max_uniform_components);
14201e04c3fSmrgbool lower_clip_cull_distance(struct gl_shader_program *prog,
14301e04c3fSmrg                              gl_linked_shader *shader);
1447ec681f3Smrgir_variable * lower_xfb_varying(void *mem_ctx,
1457ec681f3Smrg                                gl_linked_shader *shader,
1467ec681f3Smrg                                const char *old_var_name);
14701e04c3fSmrgvoid lower_output_reads(unsigned stage, exec_list *instructions);
14801e04c3fSmrgbool lower_packing_builtins(exec_list *instructions, int op_mask);
14901e04c3fSmrgvoid lower_shared_reference(struct gl_context *ctx,
15001e04c3fSmrg                            struct gl_shader_program *prog,
15101e04c3fSmrg                            struct gl_linked_shader *shader);
15201e04c3fSmrgvoid lower_ubo_reference(struct gl_linked_shader *shader,
15301e04c3fSmrg                         bool clamp_block_indices, bool use_std430_as_default);
15401e04c3fSmrgvoid lower_packed_varyings(void *mem_ctx,
15501e04c3fSmrg                           unsigned locations_used,
15601e04c3fSmrg                           const uint8_t *components,
15701e04c3fSmrg                           ir_variable_mode mode,
15801e04c3fSmrg                           unsigned gs_input_vertices,
15901e04c3fSmrg                           gl_linked_shader *shader,
1607ec681f3Smrg                           bool disable_varying_packing,
1617ec681f3Smrg                           bool disable_xfb_packing,
1627ec681f3Smrg                           bool xfb_enabled);
16301e04c3fSmrgbool lower_vector_insert(exec_list *instructions, bool lower_nonconstant_index);
16401e04c3fSmrgbool lower_vector_derefs(gl_linked_shader *shader);
16501e04c3fSmrgvoid lower_named_interface_blocks(void *mem_ctx, gl_linked_shader *shader);
16601e04c3fSmrgbool optimize_redundant_jumps(exec_list *instructions);
16701e04c3fSmrgbool optimize_split_arrays(exec_list *instructions, bool linked);
16801e04c3fSmrgbool lower_offset_arrays(exec_list *instructions);
16901e04c3fSmrgvoid optimize_dead_builtin_variables(exec_list *instructions,
17001e04c3fSmrg                                     enum ir_variable_mode other);
17101e04c3fSmrgbool lower_tess_level(gl_linked_shader *shader);
17201e04c3fSmrg
17301e04c3fSmrgbool lower_vertex_id(gl_linked_shader *shader);
17401e04c3fSmrgbool lower_cs_derived(gl_linked_shader *shader);
17501e04c3fSmrgbool lower_blend_equation_advanced(gl_linked_shader *shader, bool coherent);
17601e04c3fSmrg
1777ec681f3Smrgbool lower_builtins(exec_list *instructions);
17801e04c3fSmrgbool lower_subroutine(exec_list *instructions, struct _mesa_glsl_parse_state *state);
1797ec681f3Smrgbool propagate_invariance(exec_list *instructions);
18001e04c3fSmrg
18101e04c3fSmrgnamespace ir_builder { class ir_factory; };
18201e04c3fSmrg
18301e04c3fSmrgir_variable *compare_index_block(ir_builder::ir_factory &body,
18401e04c3fSmrg                                 ir_variable *index,
18501e04c3fSmrg                                 unsigned base, unsigned components);
18601e04c3fSmrg
18701e04c3fSmrgbool lower_64bit_integer_instructions(exec_list *instructions,
18801e04c3fSmrg                                      unsigned what_to_lower);
18901e04c3fSmrg
1907ec681f3Smrgvoid lower_precision(const struct gl_shader_compiler_options *options,
1917ec681f3Smrg                     exec_list *instructions);
1927ec681f3Smrg
19301e04c3fSmrg#endif /* GLSL_IR_OPTIMIZATION_H */
194