1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2010 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21b8e80941Smrg * DEALINGS IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg/** 25b8e80941Smrg * \file ir_basic_block.cpp 26b8e80941Smrg * 27b8e80941Smrg * Basic block analysis of instruction streams. 28b8e80941Smrg */ 29b8e80941Smrg 30b8e80941Smrg#include "ir.h" 31b8e80941Smrg#include "ir_basic_block.h" 32b8e80941Smrg 33b8e80941Smrg/** 34b8e80941Smrg * Calls a user function for every basic block in the instruction stream. 35b8e80941Smrg * 36b8e80941Smrg * Basic block analysis is pretty easy in our IR thanks to the lack of 37b8e80941Smrg * unstructured control flow. We've got: 38b8e80941Smrg * 39b8e80941Smrg * ir_loop (for () {}, while () {}, do {} while ()) 40b8e80941Smrg * ir_loop_jump ( 41b8e80941Smrg * ir_if () {} 42b8e80941Smrg * ir_return 43b8e80941Smrg * ir_call() 44b8e80941Smrg * 45b8e80941Smrg * Note that the basic blocks returned by this don't encompass all 46b8e80941Smrg * operations performed by the program -- for example, if conditions 47b8e80941Smrg * don't get returned, nor do the assignments that will be generated 48b8e80941Smrg * for ir_call parameters. 49b8e80941Smrg */ 50b8e80941Smrgvoid call_for_basic_blocks(exec_list *instructions, 51b8e80941Smrg void (*callback)(ir_instruction *first, 52b8e80941Smrg ir_instruction *last, 53b8e80941Smrg void *data), 54b8e80941Smrg void *data) 55b8e80941Smrg{ 56b8e80941Smrg ir_instruction *leader = NULL; 57b8e80941Smrg ir_instruction *last = NULL; 58b8e80941Smrg 59b8e80941Smrg foreach_in_list(ir_instruction, ir, instructions) { 60b8e80941Smrg ir_if *ir_if; 61b8e80941Smrg ir_loop *ir_loop; 62b8e80941Smrg ir_function *ir_function; 63b8e80941Smrg 64b8e80941Smrg if (!leader) 65b8e80941Smrg leader = ir; 66b8e80941Smrg 67b8e80941Smrg if ((ir_if = ir->as_if())) { 68b8e80941Smrg callback(leader, ir, data); 69b8e80941Smrg leader = NULL; 70b8e80941Smrg 71b8e80941Smrg call_for_basic_blocks(&ir_if->then_instructions, callback, data); 72b8e80941Smrg call_for_basic_blocks(&ir_if->else_instructions, callback, data); 73b8e80941Smrg } else if ((ir_loop = ir->as_loop())) { 74b8e80941Smrg callback(leader, ir, data); 75b8e80941Smrg leader = NULL; 76b8e80941Smrg call_for_basic_blocks(&ir_loop->body_instructions, callback, data); 77b8e80941Smrg } else if (ir->as_jump() || ir->as_call()) { 78b8e80941Smrg callback(leader, ir, data); 79b8e80941Smrg leader = NULL; 80b8e80941Smrg } else if ((ir_function = ir->as_function())) { 81b8e80941Smrg /* A function definition doesn't interrupt our basic block 82b8e80941Smrg * since execution doesn't go into it. We should process the 83b8e80941Smrg * bodies of its signatures for BBs, though. 84b8e80941Smrg * 85b8e80941Smrg * Note that we miss an opportunity for producing more 86b8e80941Smrg * maximal BBs between the instructions that precede main() 87b8e80941Smrg * and the body of main(). Perhaps those instructions ought 88b8e80941Smrg * to live inside of main(). 89b8e80941Smrg */ 90b8e80941Smrg foreach_in_list(ir_function_signature, ir_sig, &ir_function->signatures) { 91b8e80941Smrg call_for_basic_blocks(&ir_sig->body, callback, data); 92b8e80941Smrg } 93b8e80941Smrg } 94b8e80941Smrg last = ir; 95b8e80941Smrg } 96b8e80941Smrg if (leader) { 97b8e80941Smrg callback(leader, last, data); 98b8e80941Smrg } 99b8e80941Smrg} 100