1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \file ir_function_can_inline.cpp
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Determines if we can inline a function call using ir_function_inlining.cpp.
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The primary restriction is that we can't return from the function
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * other than as the last instruction.  We could potentially work
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * around this for some constructs by flattening control flow and
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * moving the return to the end, or by using breaks from a do {} while
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * (0) loop surrounding the function body.
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass ir_function_can_inline_visitor : public ir_hierarchical_visitor {
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_function_can_inline_visitor()
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->num_returns = 0;
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ir_visitor_status visit_enter(ir_return *);
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int num_returns;
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_function_can_inline_visitor::visit_enter(ir_return *ir)
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   (void) ir;
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->num_returns++;
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return visit_continue;
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgcan_inline(ir_call *call)
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_function_can_inline_visitor v;
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const ir_function_signature *callee = call->callee;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!callee->is_defined)
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return false;
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   v.run((exec_list *) &callee->body);
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* If the function is empty (no last instruction) or does not end with a
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * return statement, we need to count the implicit return.
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_instruction *last = (ir_instruction *)callee->body.get_tail();
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (last == NULL || !last->as_return())
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      v.num_returns++;
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return v.num_returns == 1;
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
77