glcpp: Accept #elif without an expression if the expression doesn't matter.
[mesa.git] / ir_basic_block.cpp
1 /*
2 * Copyright © 2010 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 ir_basic_block.cpp
26 *
27 * Basic block analysis of instruction streams.
28 */
29
30 #include <stdio.h>
31 #include "ir.h"
32 #include "ir_visitor.h"
33 #include "ir_basic_block.h"
34 #include "glsl_types.h"
35
36 class ir_has_call_visitor : public ir_hierarchical_visitor {
37 public:
38 ir_has_call_visitor()
39 {
40 has_call = false;
41 }
42
43 virtual ir_visitor_status visit_enter(ir_call *ir)
44 {
45 (void) ir;
46 has_call = true;
47 return visit_stop;
48 }
49
50 bool has_call;
51 };
52
53 /**
54 * Calls a user function for every basic block in the instruction stream.
55 *
56 * Basic block analysis is pretty easy in our IR thanks to the lack of
57 * unstructured control flow. We've got:
58 *
59 * ir_loop (for () {}, while () {}, do {} while ())
60 * ir_loop_jump (
61 * ir_if () {}
62 * ir_return
63 * ir_call()
64 *
65 * Note that the basic blocks returned by this don't encompass all
66 * operations performed by the program -- for example, if conditions
67 * don't get returned, nor do the assignments that will be generated
68 * for ir_call parameters.
69 */
70 void call_for_basic_blocks(exec_list *instructions,
71 void (*callback)(ir_instruction *first,
72 ir_instruction *last,
73 void *data),
74 void *data)
75 {
76 ir_instruction *leader = NULL;
77 ir_instruction *last = NULL;
78
79 foreach_iter(exec_list_iterator, iter, *instructions) {
80 ir_instruction *ir = (ir_instruction *)iter.get();
81 ir_if *ir_if;
82 ir_loop *ir_loop;
83 ir_function *ir_function;
84
85 if (!leader)
86 leader = ir;
87
88 if ((ir_if = ir->as_if())) {
89 callback(leader, ir, data);
90 leader = NULL;
91
92 call_for_basic_blocks(&ir_if->then_instructions, callback, data);
93 call_for_basic_blocks(&ir_if->else_instructions, callback, data);
94 } else if ((ir_loop = ir->as_loop())) {
95 callback(leader, ir, data);
96 leader = NULL;
97 call_for_basic_blocks(&ir_loop->body_instructions, callback, data);
98 } else if (ir->as_return() || ir->as_call()) {
99 callback(leader, ir, data);
100 leader = NULL;
101 } else if ((ir_function = ir->as_function())) {
102 /* A function definition doesn't interrupt our basic block
103 * since execution doesn't go into it. We should process the
104 * bodies of its signatures for BBs, though.
105 *
106 * Note that we miss an opportunity for producing more
107 * maximal BBs between the instructions that precede main()
108 * and the body of main(). Perhaps those instructions ought
109 * to live inside of main().
110 */
111 foreach_iter(exec_list_iterator, fun_iter, *ir_function) {
112 ir_function_signature *ir_sig;
113
114 ir_sig = (ir_function_signature *)fun_iter.get();
115
116 call_for_basic_blocks(&ir_sig->body, callback, data);
117 }
118 } else if (ir->as_assignment()) {
119 ir_has_call_visitor v;
120
121 /* If there's a call in the expression tree being assigned,
122 * then that ends the BB too.
123 *
124 * The assumption is that any consumer of the basic block
125 * walker is fine with the fact that the call is somewhere in
126 * the tree even if portions of the tree may be evaluated
127 * after the call.
128 *
129 * A consumer that has an issue with this could not process
130 * the last instruction of the basic block. If doing so,
131 * expression flattener may be useful before using the basic
132 * block finder to get more maximal basic blocks out.
133 */
134 ir->accept(&v);
135 if (v.has_call) {
136 callback(leader, ir, data);
137 leader = NULL;
138 }
139 }
140 last = ir;
141 }
142 if (leader) {
143 callback(leader, last, data);
144 }
145 }