nir: move to compiler/
[mesa.git] / src / glsl / opt_flatten_nested_if_blocks.cpp
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
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:
10 *
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.
14 *
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.
22 */
23
24 /**
25 * \file opt_flatten_nested_if_blocks.cpp
26 *
27 * Flattens nested if blocks such as:
28 *
29 * if (x) {
30 * if (y) {
31 * ...
32 * }
33 * }
34 *
35 * into a single if block with a combined condition:
36 *
37 * if (x && y) {
38 * ...
39 * }
40 */
41
42 #include "ir.h"
43 #include "ir_builder.h"
44
45 using namespace ir_builder;
46
47 namespace {
48
49 class nested_if_flattener : public ir_hierarchical_visitor {
50 public:
51 nested_if_flattener()
52 {
53 progress = false;
54 }
55
56 ir_visitor_status visit_leave(ir_if *);
57 ir_visitor_status visit_enter(ir_assignment *);
58
59 bool progress;
60 };
61
62 } /* unnamed namespace */
63
64 /* We only care about the top level "if" instructions, so don't
65 * descend into expressions.
66 */
67 ir_visitor_status
68 nested_if_flattener::visit_enter(ir_assignment *ir)
69 {
70 (void) ir;
71 return visit_continue_with_parent;
72 }
73
74 bool
75 opt_flatten_nested_if_blocks(exec_list *instructions)
76 {
77 nested_if_flattener v;
78
79 v.run(instructions);
80 return v.progress;
81 }
82
83
84 ir_visitor_status
85 nested_if_flattener::visit_leave(ir_if *ir)
86 {
87 /* Only handle a single ir_if within the then clause of an ir_if. No extra
88 * instructions, no else clauses, nothing.
89 */
90 if (ir->then_instructions.is_empty() || !ir->else_instructions.is_empty())
91 return visit_continue;
92
93 ir_if *inner = ((ir_instruction *) ir->then_instructions.head)->as_if();
94 if (!inner || !inner->next->is_tail_sentinel() ||
95 !inner->else_instructions.is_empty())
96 return visit_continue;
97
98 ir->condition = logic_and(ir->condition, inner->condition);
99 inner->then_instructions.move_nodes_to(&ir->then_instructions);
100
101 progress = true;
102 return visit_continue;
103 }