blob: 455398e499c39a8c4ec95f15a84a8d06d14f7d6e [file] [log] [blame]
Eric Anholt5c89f0e2010-05-04 13:04:40 -07001/*
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_visit_tree.h"
34#include "ir_basic_block.h"
35#include "glsl_types.h"
36
37static void
38has_call_callback(ir_instruction *ir, void *data)
39{
40 bool *has_call = (bool *)data;
41
42 *has_call = *has_call || ir->as_call();
43}
44
45/**
46 * Calls a user function for every basic block in the instruction stream.
47 *
48 * Basic block analysis is pretty easy in our IR thanks to the lack of
49 * unstructured control flow. We've got:
50 *
51 * ir_loop (for () {}, while () {}, do {} while ())
52 * ir_loop_jump (
53 * ir_if () {}
54 * ir_return
55 * ir_call()
56 *
57 * Note that the basic blocks returned by this don't encompass all
58 * operations performed by the program -- for example, if conditions
59 * don't get returned, nor do the assignments that will be generated
60 * for ir_call parameters.
61 */
62void call_for_basic_blocks(exec_list *instructions,
63 void (*callback)(ir_instruction *first,
Eric Anholt8e75de32010-05-05 09:31:53 -070064 ir_instruction *last,
65 void *data),
66 void *data)
Eric Anholt5c89f0e2010-05-04 13:04:40 -070067{
68 ir_instruction *leader = NULL;
69 ir_instruction *last = NULL;
70
71 foreach_iter(exec_list_iterator, iter, *instructions) {
72 ir_instruction *ir = (ir_instruction *)iter.get();
73 ir_if *ir_if;
74 ir_loop *ir_loop;
75 ir_function *ir_function;
76
77 if (!leader)
78 leader = ir;
79
80 if ((ir_if = ir->as_if())) {
Eric Anholt8e75de32010-05-05 09:31:53 -070081 callback(leader, ir, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070082 leader = NULL;
83
Eric Anholt8e75de32010-05-05 09:31:53 -070084 call_for_basic_blocks(&ir_if->then_instructions, callback, data);
85 call_for_basic_blocks(&ir_if->else_instructions, callback, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070086 } else if ((ir_loop = ir->as_loop())) {
Eric Anholt8e75de32010-05-05 09:31:53 -070087 callback(leader, ir, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070088 leader = NULL;
Eric Anholt8e75de32010-05-05 09:31:53 -070089 call_for_basic_blocks(&ir_loop->body_instructions, callback, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070090 } else if (ir->as_return() || ir->as_call()) {
Eric Anholt8e75de32010-05-05 09:31:53 -070091 callback(leader, ir, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070092 leader = NULL;
93 } else if ((ir_function = ir->as_function())) {
94 /* A function definition doesn't interrupt our basic block
95 * since execution doesn't go into it. We should process the
96 * bodies of its signatures for BBs, though.
97 *
98 * Note that we miss an opportunity for producing more
99 * maximal BBs between the instructions that precede main()
100 * and the body of main(). Perhaps those instructions ought
101 * to live inside of main().
102 */
103 foreach_iter(exec_list_iterator, fun_iter, *ir_function) {
104 ir_function_signature *ir_sig;
105
106 ir_sig = (ir_function_signature *)fun_iter.get();
107
Eric Anholt8e75de32010-05-05 09:31:53 -0700108 call_for_basic_blocks(&ir_sig->body, callback, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700109 }
110 } else if (ir->as_assignment()) {
111 bool has_call = false;
112
113 /* If there's a call in the expression tree being assigned,
114 * then that ends the BB too.
115 *
116 * The assumption is that any consumer of the basic block
117 * walker is fine with the fact that the call is somewhere in
118 * the tree even if portions of the tree may be evaluated
119 * after the call.
120 *
121 * A consumer that has an issue with this could not process
122 * the last instruction of the basic block. If doing so,
123 * expression flattener may be useful before using the basic
124 * block finder to get more maximal basic blocks out.
125 */
126 ir_visit_tree(ir, has_call_callback, &has_call);
127
128 if (has_call) {
Eric Anholt8e75de32010-05-05 09:31:53 -0700129 callback(leader, ir, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700130 leader = NULL;
131 }
132 }
133 last = ir;
134 }
135 if (leader) {
Eric Anholt8e75de32010-05-05 09:31:53 -0700136 callback(leader, last, data);
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700137 }
138}