Eric Anholt | 5c89f0e | 2010-05-04 13:04:40 -0700 | [diff] [blame^] | 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_visit_tree.h" |
| 34 | #include "ir_basic_block.h" |
| 35 | #include "glsl_types.h" |
| 36 | |
| 37 | static void |
| 38 | has_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 | */ |
| 62 | void call_for_basic_blocks(exec_list *instructions, |
| 63 | void (*callback)(ir_instruction *first, |
| 64 | ir_instruction *last)) |
| 65 | { |
| 66 | ir_instruction *leader = NULL; |
| 67 | ir_instruction *last = NULL; |
| 68 | |
| 69 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 70 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 71 | ir_if *ir_if; |
| 72 | ir_loop *ir_loop; |
| 73 | ir_function *ir_function; |
| 74 | |
| 75 | if (!leader) |
| 76 | leader = ir; |
| 77 | |
| 78 | if ((ir_if = ir->as_if())) { |
| 79 | callback(leader, ir); |
| 80 | leader = NULL; |
| 81 | |
| 82 | call_for_basic_blocks(&ir_if->then_instructions, callback); |
| 83 | call_for_basic_blocks(&ir_if->else_instructions, callback); |
| 84 | } else if ((ir_loop = ir->as_loop())) { |
| 85 | callback(leader, ir); |
| 86 | leader = NULL; |
| 87 | call_for_basic_blocks(&ir_loop->body_instructions, callback); |
| 88 | } else if (ir->as_return() || ir->as_call()) { |
| 89 | callback(leader, ir); |
| 90 | leader = NULL; |
| 91 | } else if ((ir_function = ir->as_function())) { |
| 92 | /* A function definition doesn't interrupt our basic block |
| 93 | * since execution doesn't go into it. We should process the |
| 94 | * bodies of its signatures for BBs, though. |
| 95 | * |
| 96 | * Note that we miss an opportunity for producing more |
| 97 | * maximal BBs between the instructions that precede main() |
| 98 | * and the body of main(). Perhaps those instructions ought |
| 99 | * to live inside of main(). |
| 100 | */ |
| 101 | foreach_iter(exec_list_iterator, fun_iter, *ir_function) { |
| 102 | ir_function_signature *ir_sig; |
| 103 | |
| 104 | ir_sig = (ir_function_signature *)fun_iter.get(); |
| 105 | |
| 106 | call_for_basic_blocks(&ir_sig->body, callback); |
| 107 | } |
| 108 | } else if (ir->as_assignment()) { |
| 109 | bool has_call = false; |
| 110 | |
| 111 | /* If there's a call in the expression tree being assigned, |
| 112 | * then that ends the BB too. |
| 113 | * |
| 114 | * The assumption is that any consumer of the basic block |
| 115 | * walker is fine with the fact that the call is somewhere in |
| 116 | * the tree even if portions of the tree may be evaluated |
| 117 | * after the call. |
| 118 | * |
| 119 | * A consumer that has an issue with this could not process |
| 120 | * the last instruction of the basic block. If doing so, |
| 121 | * expression flattener may be useful before using the basic |
| 122 | * block finder to get more maximal basic blocks out. |
| 123 | */ |
| 124 | ir_visit_tree(ir, has_call_callback, &has_call); |
| 125 | |
| 126 | if (has_call) { |
| 127 | callback(leader, ir); |
| 128 | leader = NULL; |
| 129 | } |
| 130 | } |
| 131 | last = ir; |
| 132 | } |
| 133 | if (leader) { |
| 134 | callback(leader, last); |
| 135 | } |
| 136 | } |