blob: b7c05149438c37d0f521638b701f3fbfca8a793c [file] [log] [blame]
Ian Romanick9434a072010-08-26 15:58:33 -07001/* -*- 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#pragma once
26#ifndef LOOP_ANALYSIS_H
27#define LOOP_ANALYSIS_H
28
29#include "ir.h"
30#include "program/hash_table.h"
31
32/**
33 * Analyze and classify all variables used in all loops in the instruction list
34 */
35extern class loop_state *
36analyze_loop_variables(exec_list *instructions);
37
38
39/**
Ian Romanickbfe3fbb2010-08-26 16:43:57 -070040 * Fill in loop control fields
41 *
42 * Based on analysis of loop variables, this function tries to remove sequences
43 * in the loop of the form
44 *
45 * (if (expression bool ...) (break))
46 *
47 * and fill in the \c ir_loop::from, \c ir_loop::to, and \c ir_loop::counter
48 * fields of the \c ir_loop.
49 *
50 * In this process, some conditional break-statements may be eliminated
51 * altogether. For example, if it is provable that one loop exit condition will
52 * always be satisfied before another, the unnecessary exit condition will be
53 * removed.
54 */
55extern bool
56set_loop_controls(exec_list *instructions, loop_state *ls);
57
58
59/**
Ian Romanick9434a072010-08-26 15:58:33 -070060 * Tracking for all variables used in a loop
61 */
62class loop_variable_state : public exec_node {
63public:
64 class loop_variable *get(const ir_variable *);
65 class loop_variable *insert(ir_variable *);
66 class loop_terminator *insert(ir_if *);
67
68
69 /**
70 * Loop whose variable state is being tracked by this structure
71 */
72 ir_loop *loop;
73
74 /**
75 * Variables that have not yet been classified
76 */
77 exec_list variables;
78
79 /**
80 * Variables whose values are constant within the body of the loop
81 *
82 * This list contains \c loop_variable objects.
83 */
84 exec_list constants;
85
86 /**
87 * Induction variables for this loop
88 *
89 * This list contains \c loop_variable objects.
90 */
91 exec_list induction_variables;
92
93 /**
94 * Simple if-statements that lead to the termination of the loop
95 *
96 * This list contains \c loop_terminator objects.
97 *
98 * \sa is_loop_terminator
99 */
100 exec_list terminators;
101
102 /**
103 * Hash table containing all variables accessed in this loop
104 */
105 hash_table *var_hash;
106
107 loop_variable_state()
108 {
109 this->var_hash = hash_table_ctor(0, hash_table_pointer_hash,
110 hash_table_pointer_compare);
111 }
112
113 ~loop_variable_state()
114 {
115 hash_table_dtor(this->var_hash);
116 }
117};
118
119
120class loop_variable : public exec_node {
121public:
122 /** The variable in question. */
123 ir_variable *var;
124
125 /** Is the variable read in the loop before it is written? */
126 bool read_before_write;
127
128 /** Are all variables in the RHS of the assignment loop constants? */
129 bool rhs_clean;
130
131 /** Is there an assignment to the variable that is conditional? */
132 bool conditional_assignment;
133
134 /** Reference to the first assignment to the variable in the loop body. */
135 ir_assignment *first_assignment;
136
137 /** Number of assignments to the variable in the loop body. */
138 unsigned num_assignments;
139
140 /**
141 * Increment values for loop induction variables
142 *
143 * Loop induction variables have a single increment of the form
144 * \c b * \c biv + \c c, where \c b and \c c are loop constants and \c i
145 * is a basic loop induction variable.
146 *
147 * If \c iv_scale is \c NULL, 1 is used. If \c biv is the same as \c var,
148 * then \c var is a basic loop induction variable.
149 */
150 /*@{*/
151 ir_rvalue *iv_scale;
152 ir_variable *biv;
153 ir_rvalue *increment;
154 /*@}*/
155
156
157 inline bool is_loop_constant() const
158 {
159 const bool is_const = (this->num_assignments == 0)
160 || ((this->num_assignments == 1)
161 && !this->conditional_assignment
162 && !this->read_before_write
163 && this->rhs_clean);
164
165 /* If the RHS of *the* assignment is clean, then there must be exactly
166 * one assignment of the variable.
167 */
168 assert((this->rhs_clean && (this->num_assignments == 1))
169 || !this->rhs_clean);
170
171 /* Variables that are marked read-only *MUST* be loop constant.
172 */
173 assert(!this->var->read_only || (this->var->read_only && is_const));
174
175 return is_const;
176 }
177};
178
179
180class loop_terminator : public exec_node {
181public:
182 ir_if *ir;
183};
184
185
186class loop_state {
187public:
188 ~loop_state();
189
190 /**
191 * Get the loop variable state data for a particular loop
192 */
193 loop_variable_state *get(const ir_loop *);
194
195 loop_variable_state *insert(ir_loop *ir);
196
197private:
198 loop_state();
199
200 /**
201 * Hash table containing all loops that have been analyzed.
202 */
203 hash_table *ht;
204
205 void *mem_ctx;
206
207 friend class loop_analysis;
208};
209
210#endif /* LOOP_ANALYSIS_H */