Path: blob/21.2-virgl/src/compiler/glsl/ir_basic_block.cpp
4545 views
/*1* Copyright © 2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223/**24* \file ir_basic_block.cpp25*26* Basic block analysis of instruction streams.27*/2829#include "ir.h"30#include "ir_basic_block.h"3132/**33* Calls a user function for every basic block in the instruction stream.34*35* Basic block analysis is pretty easy in our IR thanks to the lack of36* unstructured control flow. We've got:37*38* ir_loop (for () {}, while () {}, do {} while ())39* ir_loop_jump (40* ir_if () {}41* ir_return42* ir_call()43*44* Note that the basic blocks returned by this don't encompass all45* operations performed by the program -- for example, if conditions46* don't get returned, nor do the assignments that will be generated47* for ir_call parameters.48*/49void call_for_basic_blocks(exec_list *instructions,50void (*callback)(ir_instruction *first,51ir_instruction *last,52void *data),53void *data)54{55ir_instruction *leader = NULL;56ir_instruction *last = NULL;5758foreach_in_list(ir_instruction, ir, instructions) {59ir_if *ir_if;60ir_loop *ir_loop;61ir_function *ir_function;6263if (!leader)64leader = ir;6566if ((ir_if = ir->as_if())) {67callback(leader, ir, data);68leader = NULL;6970call_for_basic_blocks(&ir_if->then_instructions, callback, data);71call_for_basic_blocks(&ir_if->else_instructions, callback, data);72} else if ((ir_loop = ir->as_loop())) {73callback(leader, ir, data);74leader = NULL;75call_for_basic_blocks(&ir_loop->body_instructions, callback, data);76} else if (ir->as_jump() || ir->as_call()) {77callback(leader, ir, data);78leader = NULL;79} else if ((ir_function = ir->as_function())) {80/* A function definition doesn't interrupt our basic block81* since execution doesn't go into it. We should process the82* bodies of its signatures for BBs, though.83*84* Note that we miss an opportunity for producing more85* maximal BBs between the instructions that precede main()86* and the body of main(). Perhaps those instructions ought87* to live inside of main().88*/89foreach_in_list(ir_function_signature, ir_sig, &ir_function->signatures) {90call_for_basic_blocks(&ir_sig->body, callback, data);91}92}93last = ir;94}95if (leader) {96callback(leader, last, data);97}98}99100101