| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -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_function_can_inline.cpp |
| 26 | * |
| 27 | * Determines if we can inline a function call using ir_function_inlining.cpp. |
| 28 | * |
| 29 | * The primary restriction is that we can't return from the function |
| 30 | * other than as the last instruction. We could potentially work |
| 31 | * around this for some constructs by flattening control flow and |
| 32 | * moving the return to the end, or by using breaks from a do {} while |
| 33 | * (0) loop surrounding the function body. |
| 34 | */ |
| 35 | |
| 36 | #define NULL 0 |
| 37 | #include "ir.h" |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 38 | |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 39 | class ir_function_can_inline_visitor : public ir_hierarchical_visitor { |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 40 | public: |
| 41 | ir_function_can_inline_visitor() |
| 42 | { |
| 43 | this->can_inline = true; |
| 44 | this->num_returns = 0; |
| 45 | } |
| 46 | |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 47 | virtual ir_visitor_status visit_enter(ir_loop *); |
| 48 | virtual ir_visitor_status visit_enter(ir_return *); |
| 49 | virtual ir_visitor_status visit_enter(ir_if *); |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 50 | |
| 51 | bool can_inline; |
| 52 | int num_returns; |
| 53 | }; |
| 54 | |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 55 | ir_visitor_status |
| 56 | ir_function_can_inline_visitor::visit_enter(ir_loop *ir) |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 57 | { |
| 58 | /* FINISHME: Implement loop cloning in ir_function_inlining.cpp */ |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 59 | (void) ir; |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 60 | this->can_inline = false; |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 61 | return visit_stop; |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 64 | |
| 65 | ir_visitor_status |
| 66 | ir_function_can_inline_visitor::visit_enter(ir_return *ir) |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 67 | { |
| 68 | (void) ir; |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 69 | this->num_returns++; |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 70 | return visit_continue; |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 74 | ir_visitor_status |
| 75 | ir_function_can_inline_visitor::visit_enter(ir_if *ir) |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 76 | { |
| 77 | /* FINISHME: Implement if cloning in ir_function_inlining.cpp. */ |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 78 | (void) ir; |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 79 | this->can_inline = false; |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 80 | return visit_stop; |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool |
| 84 | can_inline(ir_call *call) |
| 85 | { |
| 86 | ir_function_can_inline_visitor v; |
| 87 | const ir_function_signature *callee = call->get_callee(); |
| 88 | |
| Ian Romanick | 671e4f6 | 2010-05-14 12:53:33 -0700 | [diff] [blame] | 89 | v.run((exec_list *) &callee->body); |
| Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 90 | |
| 91 | ir_instruction *last = (ir_instruction *)callee->body.get_tail(); |
| 92 | if (last && !last->as_return()) |
| 93 | v.num_returns++; |
| 94 | |
| 95 | return v.can_inline && v.num_returns == 1; |
| 96 | } |