• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- c++ -*- */
2 /*
3  * Copyright © 2010 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef LOOP_ANALYSIS_H
26 #define LOOP_ANALYSIS_H
27 
28 #include "ir.h"
29 #include "util/hash_table.h"
30 
31 /**
32  * Analyze and classify all variables used in all loops in the instruction list
33  */
34 extern class loop_state *
35 analyze_loop_variables(exec_list *instructions);
36 
37 static inline bool
is_break(ir_instruction * ir)38 is_break(ir_instruction *ir)
39 {
40    return ir != NULL && ir->ir_type == ir_type_loop_jump &&
41       ((ir_loop_jump *) ir)->is_break();
42 }
43 
44 
45 extern bool
46 unroll_loops(exec_list *instructions, loop_state *ls,
47              const struct gl_shader_compiler_options *options);
48 
49 
50 /**
51  * Tracking for all variables used in a loop
52  */
53 class loop_variable_state : public exec_node {
54 public:
55    class loop_variable *get(const ir_variable *);
56    class loop_variable *insert(ir_variable *);
57    class loop_variable *get_or_insert(ir_variable *, bool in_assignee);
58    class loop_terminator *insert(ir_if *, bool continue_from_then);
59 
60 
61    /**
62     * Variables that have not yet been classified
63     */
64    exec_list variables;
65 
66    /**
67     * Variables whose values are constant within the body of the loop
68     *
69     * This list contains \c loop_variable objects.
70     */
71    exec_list constants;
72 
73    /**
74     * Induction variables for this loop
75     *
76     * This list contains \c loop_variable objects.
77     */
78    exec_list induction_variables;
79 
80    /**
81     * Simple if-statements that lead to the termination of the loop
82     *
83     * This list contains \c loop_terminator objects.
84     *
85     * \sa is_loop_terminator
86     */
87    exec_list terminators;
88 
89    /**
90     * If any of the terminators in \c terminators leads to termination of the
91     * loop after a constant number of iterations, this is the terminator that
92     * leads to termination after the smallest number of iterations.  Otherwise
93     * NULL.
94     */
95    loop_terminator *limiting_terminator;
96 
97    /**
98     * Hash table containing all variables accessed in this loop
99     */
100    hash_table *var_hash;
101 
102    /**
103     * Number of ir_loop_jump instructions that operate on this loop
104     */
105    unsigned num_loop_jumps;
106 
107    /**
108     * Whether this loop contains any function calls.
109     */
110    bool contains_calls;
111 
loop_variable_state()112    loop_variable_state()
113    {
114       this->num_loop_jumps = 0;
115       this->contains_calls = false;
116       this->var_hash = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
117                                                _mesa_key_pointer_equal);
118       this->limiting_terminator = NULL;
119    }
120 
~loop_variable_state()121    ~loop_variable_state()
122    {
123       _mesa_hash_table_destroy(this->var_hash, NULL);
124    }
125 
126    DECLARE_RALLOC_CXX_OPERATORS(loop_variable_state)
127 };
128 
129 
130 class loop_variable : public exec_node {
131 public:
132    /** The variable in question. */
133    ir_variable *var;
134 
135    /** Is the variable read in the loop before it is written? */
136    bool read_before_write;
137 
138    /** Are all variables in the RHS of the assignment loop constants? */
139    bool rhs_clean;
140 
141    /**
142     * Is there an assignment to the variable that is conditional, or inside a
143     * nested loop?
144     */
145    bool conditional_or_nested_assignment;
146 
147    /** Reference to the first assignment to the variable in the loop body. */
148    ir_assignment *first_assignment;
149 
150    /** Number of assignments to the variable in the loop body. */
151    unsigned num_assignments;
152 
153    /**
154     * Increment value for a loop induction variable
155     *
156     * If this is a loop induction variable, the amount by which the variable
157     * is incremented on each iteration through the loop.
158     *
159     * If this is not a loop induction variable, NULL.
160     */
161    ir_rvalue *increment;
162 
163 
is_induction_var()164    inline bool is_induction_var() const
165    {
166       /* Induction variables always have a non-null increment, and vice
167        * versa.
168        */
169       return this->increment != NULL;
170    }
171 
172 
is_loop_constant()173    inline bool is_loop_constant() const
174    {
175       const bool is_const = (this->num_assignments == 0)
176          || (((this->num_assignments == 1)
177 	     && !this->conditional_or_nested_assignment
178 	     && !this->read_before_write
179              && this->rhs_clean) || this->var->data.read_only);
180 
181       /* If the RHS of *the* assignment is clean, then there must be exactly
182        * one assignment of the variable.
183        */
184       assert((this->rhs_clean && (this->num_assignments == 1))
185 	     || !this->rhs_clean);
186 
187       return is_const;
188    }
189 
190    void record_reference(bool in_assignee,
191                          bool in_conditional_code_or_nested_loop,
192                          ir_assignment *current_assignment);
193 };
194 
195 
196 class loop_terminator : public exec_node {
197 public:
loop_terminator()198    loop_terminator()
199       : ir(NULL), iterations(-1)
200    {
201    }
202 
203    /**
204     * Statement which terminates the loop.
205     */
206    ir_if *ir;
207 
208    /**
209     * The number of iterations after which the terminator is known to
210     * terminate the loop (if that is a fixed value).  Otherwise -1.
211     */
212    int iterations;
213 
214    /* Does the if continue from the then branch or the else branch */
215    bool continue_from_then;
216 };
217 
218 
219 class loop_state {
220 public:
221    ~loop_state();
222 
223    /**
224     * Get the loop variable state data for a particular loop
225     */
226    loop_variable_state *get(const ir_loop *);
227 
228    loop_variable_state *insert(ir_loop *ir);
229 
230    bool loop_found;
231 
232 private:
233    loop_state();
234 
235    /**
236     * Hash table containing all loops that have been analyzed.
237     */
238    hash_table *ht;
239 
240    void *mem_ctx;
241 
242    friend loop_state *analyze_loop_variables(exec_list *instructions);
243 };
244 
245 #endif /* LOOP_ANALYSIS_H */
246