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 other
30  * than as the last instruction.  In lower_jumps.cpp, we can lower return
31  * statements not at the end of the function to other control flow in order to
32  * deal with this restriction.
33  */
34 
35 #include "ir.h"
36 
37 class ir_function_can_inline_visitor : public ir_hierarchical_visitor {
38 public:
ir_function_can_inline_visitor()39    ir_function_can_inline_visitor()
40    {
41       this->num_returns = 0;
42    }
43 
44    virtual ir_visitor_status visit_enter(ir_return *);
45 
46    int num_returns;
47 };
48 
49 ir_visitor_status
visit_enter(ir_return * ir)50 ir_function_can_inline_visitor::visit_enter(ir_return *ir)
51 {
52    (void) ir;
53    this->num_returns++;
54    return visit_continue;
55 }
56 
57 bool
can_inline(ir_call * call)58 can_inline(ir_call *call)
59 {
60    ir_function_can_inline_visitor v;
61    const ir_function_signature *callee = call->callee;
62    if (!callee->is_defined)
63       return false;
64 
65    v.run((exec_list *) &callee->body);
66 
67    /* If the function is empty (no last instruction) or does not end with a
68     * return statement, we need to count the implicit return.
69     */
70    ir_instruction *last = (ir_instruction *)callee->body.get_tail();
71    if (last == NULL || !last->as_return())
72       v.num_returns++;
73 
74    return v.num_returns == 1;
75 }
76