2010-08-26 15:58:33 -07:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LOOP_ANALYSIS_H
|
|
|
|
#define LOOP_ANALYSIS_H
|
|
|
|
|
|
|
|
#include "ir.h"
|
2016-08-16 22:10:34 +02:00
|
|
|
#include "util/hash_table.h"
|
2010-08-26 15:58:33 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Analyze and classify all variables used in all loops in the instruction list
|
|
|
|
*/
|
|
|
|
extern class loop_state *
|
|
|
|
analyze_loop_variables(exec_list *instructions);
|
|
|
|
|
2017-09-05 15:59:07 +10:00
|
|
|
static inline bool
|
|
|
|
is_break(ir_instruction *ir)
|
|
|
|
{
|
|
|
|
return ir != NULL && ir->ir_type == ir_type_loop_jump &&
|
|
|
|
((ir_loop_jump *) ir)->is_break();
|
|
|
|
}
|
|
|
|
|
2010-08-26 15:58:33 -07:00
|
|
|
|
2010-08-27 13:59:49 -07:00
|
|
|
extern bool
|
2014-04-08 19:58:36 -07:00
|
|
|
unroll_loops(exec_list *instructions, loop_state *ls,
|
|
|
|
const struct gl_shader_compiler_options *options);
|
2010-08-27 13:59:49 -07:00
|
|
|
|
|
|
|
|
2010-08-26 15:58:33 -07:00
|
|
|
/**
|
|
|
|
* Tracking for all variables used in a loop
|
|
|
|
*/
|
|
|
|
class loop_variable_state : public exec_node {
|
|
|
|
public:
|
|
|
|
class loop_variable *get(const ir_variable *);
|
|
|
|
class loop_variable *insert(ir_variable *);
|
2013-11-28 10:42:01 -08:00
|
|
|
class loop_variable *get_or_insert(ir_variable *, bool in_assignee);
|
2017-09-04 13:11:49 +10:00
|
|
|
class loop_terminator *insert(ir_if *, bool continue_from_then);
|
2010-08-26 15:58:33 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variables that have not yet been classified
|
|
|
|
*/
|
|
|
|
exec_list variables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variables whose values are constant within the body of the loop
|
|
|
|
*
|
|
|
|
* This list contains \c loop_variable objects.
|
|
|
|
*/
|
|
|
|
exec_list constants;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Induction variables for this loop
|
|
|
|
*
|
|
|
|
* This list contains \c loop_variable objects.
|
|
|
|
*/
|
|
|
|
exec_list induction_variables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple if-statements that lead to the termination of the loop
|
|
|
|
*
|
|
|
|
* This list contains \c loop_terminator objects.
|
|
|
|
*
|
|
|
|
* \sa is_loop_terminator
|
|
|
|
*/
|
|
|
|
exec_list terminators;
|
|
|
|
|
2013-11-28 14:40:19 -08:00
|
|
|
/**
|
|
|
|
* If any of the terminators in \c terminators leads to termination of the
|
|
|
|
* loop after a constant number of iterations, this is the terminator that
|
|
|
|
* leads to termination after the smallest number of iterations. Otherwise
|
|
|
|
* NULL.
|
|
|
|
*/
|
|
|
|
loop_terminator *limiting_terminator;
|
|
|
|
|
2010-08-26 15:58:33 -07:00
|
|
|
/**
|
|
|
|
* Hash table containing all variables accessed in this loop
|
|
|
|
*/
|
|
|
|
hash_table *var_hash;
|
|
|
|
|
2010-08-27 15:41:20 -07:00
|
|
|
/**
|
|
|
|
* Number of ir_loop_jump instructions that operate on this loop
|
|
|
|
*/
|
|
|
|
unsigned num_loop_jumps;
|
|
|
|
|
2012-03-28 14:00:42 -07:00
|
|
|
/**
|
|
|
|
* Whether this loop contains any function calls.
|
|
|
|
*/
|
|
|
|
bool contains_calls;
|
|
|
|
|
2010-08-26 15:58:33 -07:00
|
|
|
loop_variable_state()
|
|
|
|
{
|
2010-08-27 15:41:20 -07:00
|
|
|
this->num_loop_jumps = 0;
|
2012-03-28 14:00:42 -07:00
|
|
|
this->contains_calls = false;
|
2016-08-16 22:10:34 +02:00
|
|
|
this->var_hash = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
|
|
|
|
_mesa_key_pointer_equal);
|
2013-11-28 14:40:19 -08:00
|
|
|
this->limiting_terminator = NULL;
|
2010-08-26 15:58:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~loop_variable_state()
|
|
|
|
{
|
2016-08-16 22:10:34 +02:00
|
|
|
_mesa_hash_table_destroy(this->var_hash, NULL);
|
2010-08-26 15:58:33 -07:00
|
|
|
}
|
2012-06-05 15:58:41 -07:00
|
|
|
|
2014-09-30 00:12:40 -04:00
|
|
|
DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state)
|
2010-08-26 15:58:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class loop_variable : public exec_node {
|
|
|
|
public:
|
|
|
|
/** The variable in question. */
|
|
|
|
ir_variable *var;
|
|
|
|
|
|
|
|
/** Is the variable read in the loop before it is written? */
|
|
|
|
bool read_before_write;
|
|
|
|
|
|
|
|
/** Are all variables in the RHS of the assignment loop constants? */
|
|
|
|
bool rhs_clean;
|
|
|
|
|
2013-11-28 10:48:37 -08:00
|
|
|
/**
|
|
|
|
* Is there an assignment to the variable that is conditional, or inside a
|
|
|
|
* nested loop?
|
|
|
|
*/
|
|
|
|
bool conditional_or_nested_assignment;
|
2010-08-26 15:58:33 -07:00
|
|
|
|
|
|
|
/** Reference to the first assignment to the variable in the loop body. */
|
|
|
|
ir_assignment *first_assignment;
|
|
|
|
|
|
|
|
/** Number of assignments to the variable in the loop body. */
|
|
|
|
unsigned num_assignments;
|
|
|
|
|
|
|
|
/**
|
2013-11-28 12:17:54 -08:00
|
|
|
* Increment value for a loop induction variable
|
2010-08-26 15:58:33 -07:00
|
|
|
*
|
2013-11-28 12:17:54 -08:00
|
|
|
* If this is a loop induction variable, the amount by which the variable
|
|
|
|
* is incremented on each iteration through the loop.
|
2010-08-26 15:58:33 -07:00
|
|
|
*
|
2013-11-28 12:17:54 -08:00
|
|
|
* If this is not a loop induction variable, NULL.
|
2010-08-26 15:58:33 -07:00
|
|
|
*/
|
|
|
|
ir_rvalue *increment;
|
|
|
|
|
|
|
|
|
2013-11-28 12:44:53 -08:00
|
|
|
inline bool is_induction_var() const
|
|
|
|
{
|
|
|
|
/* Induction variables always have a non-null increment, and vice
|
|
|
|
* versa.
|
|
|
|
*/
|
|
|
|
return this->increment != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-26 15:58:33 -07:00
|
|
|
inline bool is_loop_constant() const
|
|
|
|
{
|
|
|
|
const bool is_const = (this->num_assignments == 0)
|
2014-09-09 14:56:06 +03:00
|
|
|
|| (((this->num_assignments == 1)
|
2013-11-28 10:48:37 -08:00
|
|
|
&& !this->conditional_or_nested_assignment
|
2010-08-26 15:58:33 -07:00
|
|
|
&& !this->read_before_write
|
2014-09-09 14:56:06 +03:00
|
|
|
&& this->rhs_clean) || this->var->data.read_only);
|
2010-08-26 15:58:33 -07:00
|
|
|
|
|
|
|
/* If the RHS of *the* assignment is clean, then there must be exactly
|
|
|
|
* one assignment of the variable.
|
|
|
|
*/
|
|
|
|
assert((this->rhs_clean && (this->num_assignments == 1))
|
|
|
|
|| !this->rhs_clean);
|
|
|
|
|
|
|
|
return is_const;
|
|
|
|
}
|
2013-11-28 10:42:01 -08:00
|
|
|
|
2013-11-28 10:48:37 -08:00
|
|
|
void record_reference(bool in_assignee,
|
|
|
|
bool in_conditional_code_or_nested_loop,
|
2013-11-28 10:42:01 -08:00
|
|
|
ir_assignment *current_assignment);
|
2010-08-26 15:58:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class loop_terminator : public exec_node {
|
|
|
|
public:
|
2013-11-28 14:40:19 -08:00
|
|
|
loop_terminator()
|
|
|
|
: ir(NULL), iterations(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Statement which terminates the loop.
|
|
|
|
*/
|
2010-08-26 15:58:33 -07:00
|
|
|
ir_if *ir;
|
2013-11-28 14:40:19 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of iterations after which the terminator is known to
|
|
|
|
* terminate the loop (if that is a fixed value). Otherwise -1.
|
|
|
|
*/
|
|
|
|
int iterations;
|
2017-09-04 13:11:49 +10:00
|
|
|
|
|
|
|
/* Does the if continue from the then branch or the else branch */
|
|
|
|
bool continue_from_then;
|
2010-08-26 15:58:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class loop_state {
|
|
|
|
public:
|
|
|
|
~loop_state();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the loop variable state data for a particular loop
|
|
|
|
*/
|
|
|
|
loop_variable_state *get(const ir_loop *);
|
|
|
|
|
|
|
|
loop_variable_state *insert(ir_loop *ir);
|
|
|
|
|
2011-01-17 22:07:55 -08:00
|
|
|
bool loop_found;
|
|
|
|
|
2010-08-26 15:58:33 -07:00
|
|
|
private:
|
|
|
|
loop_state();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash table containing all loops that have been analyzed.
|
|
|
|
*/
|
|
|
|
hash_table *ht;
|
|
|
|
|
|
|
|
void *mem_ctx;
|
|
|
|
|
2012-07-10 16:26:33 -07:00
|
|
|
friend loop_state *analyze_loop_variables(exec_list *instructions);
|
2010-08-26 15:58:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* LOOP_ANALYSIS_H */
|