summaryrefslogtreecommitdiff
path: root/src/glsl/ir_copy_propagation.cpp
blob: f502f5e0b0693d72a44bd3fad0808a584ff54111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * 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.
 */

/**
 * \file ir_copy_propagation.cpp
 *
 * Moves usage of recently-copied variables to the previous copy of
 * the variable within basic blocks.
 *
 * This should reduce the number of MOV instructions in the generated
 * programs unless copy propagation is also done on the LIR, and may
 * help anyway by triggering other optimizations that live in the HIR.
 */

#include "ir.h"
#include "ir_visitor.h"
#include "ir_basic_block.h"
#include "ir_optimization.h"
#include "glsl_types.h"

class acp_entry : public exec_node
{
public:
   acp_entry(ir_variable *lhs, ir_variable *rhs)
   {
      assert(lhs);
      assert(rhs);
      this->lhs = lhs;
      this->rhs = rhs;
   }

   ir_variable *lhs;
   ir_variable *rhs;
};

class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
public:
   ir_copy_propagation_visitor(exec_list *acp)
   {
      progress = false;
      in_lhs = false;
      this->acp = acp;
   }

   virtual ir_visitor_status visit(class ir_dereference_variable *);
   virtual ir_visitor_status visit_enter(class ir_loop *);
   virtual ir_visitor_status visit_enter(class ir_function_signature *);
   virtual ir_visitor_status visit_enter(class ir_function *);
   virtual ir_visitor_status visit_enter(class ir_assignment *);
   virtual ir_visitor_status visit_enter(class ir_call *);
   virtual ir_visitor_status visit_enter(class ir_if *);

   /** List of acp_entry */
   exec_list *acp;
   bool progress;

   /** Currently in the LHS of an assignment? */
   bool in_lhs;
};


ir_visitor_status
ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
{
   (void)ir;
   return visit_continue_with_parent;
}

ir_visitor_status
ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
{
   (void)ir;
   return visit_continue_with_parent;
}

ir_visitor_status
ir_copy_propagation_visitor::visit_enter(ir_assignment *ir)
{
   (void) ir;
   this->in_lhs = true;
   return visit_continue;
}

ir_visitor_status
ir_copy_propagation_visitor::visit_enter(ir_function *ir)
{
   (void) ir;
   return visit_continue_with_parent;
}

/**
 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
 *
 * This is where the actual copy propagation occurs.  Note that the
 * rewriting of ir_dereference means that the ir_dereference instance
 * must not be shared by multiple IR operations!
 */
ir_visitor_status
ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
{
   /* Ignores the LHS.  Don't want to rewrite the LHS to point at some
    * other storage!
    */
   if (this->in_lhs) {
      this->in_lhs = false;
      return visit_continue;
   }

   ir_variable *var = ir->variable_referenced();

   foreach_iter(exec_list_iterator, iter, *this->acp) {
      acp_entry *entry = (acp_entry *)iter.get();

      if (var == entry->lhs) {
	 ir->var = entry->rhs;
	 this->progress = true;
	 break;
      }
   }

   return visit_continue;
}


ir_visitor_status
ir_copy_propagation_visitor::visit_enter(ir_call *ir)
{
   (void)ir;

   /* Note, if we were to do copy propagation to parameters of calls, we'd
    * have to be careful about out params.
    */
   return visit_continue_with_parent;
}


ir_visitor_status
ir_copy_propagation_visitor::visit_enter(ir_if *ir)
{
   ir->condition->accept(this);

   /* Do not traverse into the body of the if-statement since that is a
    * different basic block.
    */
   return visit_continue_with_parent;
}

static bool
propagate_copies(ir_instruction *ir, exec_list *acp)
{
   ir_copy_propagation_visitor v(acp);

   ir->accept(&v);

   return v.progress;
}

static void
kill_invalidated_copies(ir_assignment *ir, exec_list *acp)
{
   ir_variable *var = ir->lhs->variable_referenced();
   assert(var != NULL);

   foreach_iter(exec_list_iterator, iter, *acp) {
      acp_entry *entry = (acp_entry *)iter.get();

      if (entry->lhs == var || entry->rhs == var) {
	 entry->remove();
      }
   }
}

/**
 * Adds an entry to the available copy list if it's a plain assignment
 * of a variable to a variable.
 */
static void
add_copy(void *ctx, ir_assignment *ir, exec_list *acp)
{
   acp_entry *entry;

   if (ir->condition) {
      ir_constant *condition = ir->condition->as_constant();
      if (!condition || !condition->value.b[0])
	 return;
   }

   ir_variable *lhs_var = ir->lhs->whole_variable_referenced();
   ir_variable *rhs_var = ir->rhs->whole_variable_referenced();

   if ((lhs_var != NULL) && (rhs_var != NULL)) {
      entry = new(ctx) acp_entry(lhs_var, rhs_var);
      acp->push_tail(entry);
   }
}

static void
copy_propagation_basic_block(ir_instruction *first,
			     ir_instruction *last,
			     void *data)
{
   ir_instruction *ir;
   /* List of avaialble_copy */
   exec_list acp;
   bool *out_progress = (bool *)data;
   bool progress = false;

   void *ctx = talloc_new(NULL);
   for (ir = first;; ir = (ir_instruction *)ir->next) {
      ir_assignment *ir_assign = ir->as_assignment();

      progress = propagate_copies(ir, &acp) || progress;

      if (ir_assign) {
	 kill_invalidated_copies(ir_assign, &acp);

	 add_copy(ctx, ir_assign, &acp);
      }
      if (ir == last)
	 break;
   }
   *out_progress = progress;
   talloc_free(ctx);
}

/**
 * Does a copy propagation pass on the code present in the instruction stream.
 */
bool
do_copy_propagation(exec_list *instructions)
{
   bool progress = false;

   call_for_basic_blocks(instructions, copy_propagation_basic_block, &progress);

   return progress;
}