g3dvl: Use sobel filter for chroma interpolation
[mesa/nouveau-pmpeg.git] / src / glsl / opt_discard_simplification.cpp
blobba4981bae53e5be185cbf178d7d915c214c1a255
1 /*
2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * \file opt_discard_simplification.cpp
27 * This pass simplifies if-statements and loops containing unconditional
28 * discards.
30 * Case 1: Both branches contain unconditional discards:
31 * -----------------------------------------------------
33 * if (cond) {
34 * s1;
35 * discard;
36 * s2;
37 * } else {
38 * s3;
39 * discard;
40 * s4;
41 * }
43 * becomes:
45 * discard
47 * Case 2: The "then" clause contains an unconditional discard:
48 * ------------------------------------------------------------
50 * if (cond) {
51 * s1;
52 * discard;
53 * s2;
54 * } else {
55 * s3;
56 * }
58 * becomes:
60 * if (cond) {
61 * discard;
62 * } else {
63 * s3;
64 * }
66 * Case 3: The "else" clause contains an unconditional discard:
67 * ------------------------------------------------------------
69 * if (cond) {
70 * s1;
71 * } else {
72 * s2;
73 * discard;
74 * s3;
75 * }
77 * becomes:
79 * if (cond) {
80 * s1;
81 * } else {
82 * discard;
83 * }
86 #include "glsl_types.h"
87 #include "ir.h"
89 class discard_simplifier : public ir_hierarchical_visitor {
90 public:
91 discard_simplifier()
93 this->progress = false;
96 ir_visitor_status visit_enter(ir_if *);
97 ir_visitor_status visit_enter(ir_loop *);
98 ir_visitor_status visit_enter(ir_assignment *);
100 bool progress;
103 static ir_discard *
104 find_unconditional_discard(exec_list &instructions)
106 foreach_list(n, &instructions) {
107 ir_instruction *ir = (ir_instruction *)n;
109 if (ir->ir_type == ir_type_return ||
110 ir->ir_type == ir_type_loop_jump)
111 return NULL;
113 /* So far, this code doesn't know how to look inside of flow
114 * control to see if a discard later on at this level is
115 * unconditional.
117 if (ir->ir_type == ir_type_if ||
118 ir->ir_type == ir_type_loop)
119 return NULL;
121 ir_discard *discard = ir->as_discard();
122 if (discard != NULL && discard->condition == NULL)
123 return discard;
125 return NULL;
128 static bool
129 is_only_instruction(ir_discard *discard)
131 return (discard->prev->is_head_sentinel() &&
132 discard->next->is_tail_sentinel());
135 /* We only care about the top level instructions, so don't descend
136 * into expressions.
138 ir_visitor_status
139 discard_simplifier::visit_enter(ir_assignment *ir)
141 (void) ir;
142 return visit_continue_with_parent;
145 ir_visitor_status
146 discard_simplifier::visit_enter(ir_if *ir)
148 ir_discard *then_discard = find_unconditional_discard(ir->then_instructions);
149 ir_discard *else_discard = find_unconditional_discard(ir->else_instructions);
151 if (then_discard == NULL && else_discard == NULL)
152 return visit_continue;
154 /* If both branches result in discard, replace whole if with discard. */
155 if (then_discard != NULL && else_discard != NULL) {
156 this->progress = true;
157 ir->replace_with(then_discard);
158 return visit_continue_with_parent;
161 /* Otherwise, one branch has a discard. */
162 if (then_discard != NULL && !is_only_instruction(then_discard)) {
163 this->progress = true;
164 ir->then_instructions.make_empty();
165 ir->then_instructions.push_tail(then_discard);
166 } else if (else_discard != NULL && !is_only_instruction(else_discard)) {
167 this->progress = true;
168 ir->else_instructions.make_empty();
169 ir->else_instructions.push_tail(else_discard);
172 visit_list_elements(this, &ir->then_instructions);
173 return visit_continue_with_parent;
176 ir_visitor_status
177 discard_simplifier::visit_enter(ir_loop *ir)
179 ir_discard *discard = find_unconditional_discard(ir->body_instructions);
181 if (discard) {
182 ir->replace_with(discard);
183 return visit_continue_with_parent;
186 return visit_continue;
189 bool
190 do_discard_simplification(exec_list *instructions)
192 /* Look for a top-level unconditional discard */
193 ir_discard *discard = find_unconditional_discard(*instructions);
194 if (discard != NULL) {
195 instructions->make_empty();
196 instructions->push_tail(discard);
197 return true;
200 discard_simplifier v;
202 visit_list_elements(&v, instructions);
204 return v.progress;