blob: 50c512f847f286a60d63d525e086009c6a17c48f [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/**
40 * Tracking for all variables used in a loop
41 */
42class loop_variable_state : public exec_node {
43public:
44 class loop_variable *get(const ir_variable *);
45 class loop_variable *insert(ir_variable *);
46 class loop_terminator *insert(ir_if *);
47
48
49 /**
50 * Loop whose variable state is being tracked by this structure
51 */
52 ir_loop *loop;
53
54 /**
55 * Variables that have not yet been classified
56 */
57 exec_list variables;
58
59 /**
60 * Variables whose values are constant within the body of the loop
61 *
62 * This list contains \c loop_variable objects.
63 */
64 exec_list constants;
65
66 /**
67 * Induction variables for this loop
68 *
69 * This list contains \c loop_variable objects.
70 */
71 exec_list induction_variables;
72
73 /**
74 * Simple if-statements that lead to the termination of the loop
75 *
76 * This list contains \c loop_terminator objects.
77 *
78 * \sa is_loop_terminator
79 */
80 exec_list terminators;
81
82 /**
83 * Hash table containing all variables accessed in this loop
84 */
85 hash_table *var_hash;
86
87 loop_variable_state()
88 {
89 this->var_hash = hash_table_ctor(0, hash_table_pointer_hash,
90 hash_table_pointer_compare);
91 }
92
93 ~loop_variable_state()
94 {
95 hash_table_dtor(this->var_hash);
96 }
97};
98
99
100class loop_variable : public exec_node {
101public:
102 /** The variable in question. */
103 ir_variable *var;
104
105 /** Is the variable read in the loop before it is written? */
106 bool read_before_write;
107
108 /** Are all variables in the RHS of the assignment loop constants? */
109 bool rhs_clean;
110
111 /** Is there an assignment to the variable that is conditional? */
112 bool conditional_assignment;
113
114 /** Reference to the first assignment to the variable in the loop body. */
115 ir_assignment *first_assignment;
116
117 /** Number of assignments to the variable in the loop body. */
118 unsigned num_assignments;
119
120 /**
121 * Increment values for loop induction variables
122 *
123 * Loop induction variables have a single increment of the form
124 * \c b * \c biv + \c c, where \c b and \c c are loop constants and \c i
125 * is a basic loop induction variable.
126 *
127 * If \c iv_scale is \c NULL, 1 is used. If \c biv is the same as \c var,
128 * then \c var is a basic loop induction variable.
129 */
130 /*@{*/
131 ir_rvalue *iv_scale;
132 ir_variable *biv;
133 ir_rvalue *increment;
134 /*@}*/
135
136
137 inline bool is_loop_constant() const
138 {
139 const bool is_const = (this->num_assignments == 0)
140 || ((this->num_assignments == 1)
141 && !this->conditional_assignment
142 && !this->read_before_write
143 && this->rhs_clean);
144
145 /* If the RHS of *the* assignment is clean, then there must be exactly
146 * one assignment of the variable.
147 */
148 assert((this->rhs_clean && (this->num_assignments == 1))
149 || !this->rhs_clean);
150
151 /* Variables that are marked read-only *MUST* be loop constant.
152 */
153 assert(!this->var->read_only || (this->var->read_only && is_const));
154
155 return is_const;
156 }
157};
158
159
160class loop_terminator : public exec_node {
161public:
162 ir_if *ir;
163};
164
165
166class loop_state {
167public:
168 ~loop_state();
169
170 /**
171 * Get the loop variable state data for a particular loop
172 */
173 loop_variable_state *get(const ir_loop *);
174
175 loop_variable_state *insert(ir_loop *ir);
176
177private:
178 loop_state();
179
180 /**
181 * Hash table containing all loops that have been analyzed.
182 */
183 hash_table *ht;
184
185 void *mem_ctx;
186
187 friend class loop_analysis;
188};
189
190#endif /* LOOP_ANALYSIS_H */