11591693c7b415e9869157c711fe11263c95d74eDavid Li/*
21591693c7b415e9869157c711fe11263c95d74eDavid Li * Copyright © 2010 Intel Corporation
31591693c7b415e9869157c711fe11263c95d74eDavid Li *
41591693c7b415e9869157c711fe11263c95d74eDavid Li * Permission is hereby granted, free of charge, to any person obtaining a
51591693c7b415e9869157c711fe11263c95d74eDavid Li * copy of this software and associated documentation files (the "Software"),
61591693c7b415e9869157c711fe11263c95d74eDavid Li * to deal in the Software without restriction, including without limitation
71591693c7b415e9869157c711fe11263c95d74eDavid Li * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81591693c7b415e9869157c711fe11263c95d74eDavid Li * and/or sell copies of the Software, and to permit persons to whom the
91591693c7b415e9869157c711fe11263c95d74eDavid Li * Software is furnished to do so, subject to the following conditions:
101591693c7b415e9869157c711fe11263c95d74eDavid Li *
111591693c7b415e9869157c711fe11263c95d74eDavid Li * The above copyright notice and this permission notice (including the next
121591693c7b415e9869157c711fe11263c95d74eDavid Li * paragraph) shall be included in all copies or substantial portions of the
131591693c7b415e9869157c711fe11263c95d74eDavid Li * Software.
141591693c7b415e9869157c711fe11263c95d74eDavid Li *
151591693c7b415e9869157c711fe11263c95d74eDavid Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161591693c7b415e9869157c711fe11263c95d74eDavid Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171591693c7b415e9869157c711fe11263c95d74eDavid Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181591693c7b415e9869157c711fe11263c95d74eDavid Li * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191591693c7b415e9869157c711fe11263c95d74eDavid Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201591693c7b415e9869157c711fe11263c95d74eDavid Li * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211591693c7b415e9869157c711fe11263c95d74eDavid Li * DEALINGS IN THE SOFTWARE.
221591693c7b415e9869157c711fe11263c95d74eDavid Li */
231591693c7b415e9869157c711fe11263c95d74eDavid Li
241591693c7b415e9869157c711fe11263c95d74eDavid Li/**
251591693c7b415e9869157c711fe11263c95d74eDavid Li * \file ir_function_can_inline.cpp
261591693c7b415e9869157c711fe11263c95d74eDavid Li *
271591693c7b415e9869157c711fe11263c95d74eDavid Li * Determines if we can inline a function call using ir_function_inlining.cpp.
281591693c7b415e9869157c711fe11263c95d74eDavid Li *
291591693c7b415e9869157c711fe11263c95d74eDavid Li * The primary restriction is that we can't return from the function
301591693c7b415e9869157c711fe11263c95d74eDavid Li * other than as the last instruction.  We could potentially work
311591693c7b415e9869157c711fe11263c95d74eDavid Li * around this for some constructs by flattening control flow and
321591693c7b415e9869157c711fe11263c95d74eDavid Li * moving the return to the end, or by using breaks from a do {} while
331591693c7b415e9869157c711fe11263c95d74eDavid Li * (0) loop surrounding the function body.
341591693c7b415e9869157c711fe11263c95d74eDavid Li */
351591693c7b415e9869157c711fe11263c95d74eDavid Li
361591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ir.h"
371591693c7b415e9869157c711fe11263c95d74eDavid Li
381591693c7b415e9869157c711fe11263c95d74eDavid Liclass ir_function_can_inline_visitor : public ir_hierarchical_visitor {
391591693c7b415e9869157c711fe11263c95d74eDavid Lipublic:
401591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_function_can_inline_visitor()
411591693c7b415e9869157c711fe11263c95d74eDavid Li   {
421591693c7b415e9869157c711fe11263c95d74eDavid Li      this->num_returns = 0;
431591693c7b415e9869157c711fe11263c95d74eDavid Li   }
441591693c7b415e9869157c711fe11263c95d74eDavid Li
451591693c7b415e9869157c711fe11263c95d74eDavid Li   virtual ir_visitor_status visit_enter(ir_return *);
461591693c7b415e9869157c711fe11263c95d74eDavid Li
471591693c7b415e9869157c711fe11263c95d74eDavid Li   int num_returns;
481591693c7b415e9869157c711fe11263c95d74eDavid Li};
491591693c7b415e9869157c711fe11263c95d74eDavid Li
501591693c7b415e9869157c711fe11263c95d74eDavid Liir_visitor_status
511591693c7b415e9869157c711fe11263c95d74eDavid Liir_function_can_inline_visitor::visit_enter(ir_return *ir)
521591693c7b415e9869157c711fe11263c95d74eDavid Li{
531591693c7b415e9869157c711fe11263c95d74eDavid Li   (void) ir;
541591693c7b415e9869157c711fe11263c95d74eDavid Li   this->num_returns++;
551591693c7b415e9869157c711fe11263c95d74eDavid Li   return visit_continue;
561591693c7b415e9869157c711fe11263c95d74eDavid Li}
571591693c7b415e9869157c711fe11263c95d74eDavid Li
581591693c7b415e9869157c711fe11263c95d74eDavid Libool
591591693c7b415e9869157c711fe11263c95d74eDavid Lican_inline(ir_call *call)
601591693c7b415e9869157c711fe11263c95d74eDavid Li{
611591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_function_can_inline_visitor v;
621591693c7b415e9869157c711fe11263c95d74eDavid Li   const ir_function_signature *callee = call->get_callee();
631591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!callee->is_defined)
641591693c7b415e9869157c711fe11263c95d74eDavid Li      return false;
651591693c7b415e9869157c711fe11263c95d74eDavid Li
661591693c7b415e9869157c711fe11263c95d74eDavid Li   v.run((exec_list *) &callee->body);
671591693c7b415e9869157c711fe11263c95d74eDavid Li
681591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If the function is empty (no last instruction) or does not end with a
691591693c7b415e9869157c711fe11263c95d74eDavid Li    * return statement, we need to count the implicit return.
701591693c7b415e9869157c711fe11263c95d74eDavid Li    */
711591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_instruction *last = (ir_instruction *)callee->body.get_tail();
721591693c7b415e9869157c711fe11263c95d74eDavid Li   if (last == NULL || !last->as_return())
731591693c7b415e9869157c711fe11263c95d74eDavid Li      v.num_returns++;
741591693c7b415e9869157c711fe11263c95d74eDavid Li
751591693c7b415e9869157c711fe11263c95d74eDavid Li   return v.num_returns == 1;
761591693c7b415e9869157c711fe11263c95d74eDavid Li}
77