opt_if_simplification.cpp revision 5ba94206083fcd678febd6cac0231f35c0f1b77a
15ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt/*
25ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * Copyright © 2010 Intel Corporation
35ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt *
45ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
55ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * copy of this software and associated documentation files (the "Software"),
65ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * to deal in the Software without restriction, including without limitation
75ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
85ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * and/or sell copies of the Software, and to permit persons to whom the
95ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * Software is furnished to do so, subject to the following conditions:
105ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt *
115ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * The above copyright notice and this permission notice (including the next
125ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * paragraph) shall be included in all copies or substantial portions of the
135ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * Software.
145ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt *
155ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
165ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
175ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
185ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
195ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
205ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
215ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * DEALINGS IN THE SOFTWARE.
225ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt */
235ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
245ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt/**
255ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * \file ir_function_inlining.cpp
265ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt *
275ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * Moves constant branches of if statements out to the surrounding
285ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt * instruction stream.
295ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt */
305ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
315ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt#define NULL 0
325ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt#include "ir.h"
335ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt#include "ir_visitor.h"
345ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt#include "ir_function_inlining.h"
355ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt#include "glsl_types.h"
365ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
375ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtclass ir_if_simplification_visitor : public ir_visitor {
385ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtpublic:
395ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir_if_simplification_visitor()
405ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   {
415ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      /* empty */
425ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
435ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
445ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual ~ir_if_simplification_visitor()
455ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   {
465ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      /* empty */
475ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
485ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
495ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   /**
505ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt    * \name Visit methods
515ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt    *
525ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt    * As typical for the visitor pattern, there must be one \c visit method for
535ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt    * each concrete subclass of \c ir_instruction.  Virtual base classes within
545ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt    * the hierarchy should not have \c visit methods.
555ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt    */
565ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   /*@{*/
575ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_variable *);
585ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_label *);
595ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_loop *);
605ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_loop_jump *);
615ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_function_signature *);
625ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_function *);
635ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_expression *);
645ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_swizzle *);
655ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_dereference *);
665ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_assignment *);
675ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_constant *);
685ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_call *);
695ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_return *);
705ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   virtual void visit(ir_if *);
715ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   /*@}*/
725ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt};
735ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
745ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtbool
755ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtdo_if_simplification(exec_list *instructions)
765ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
775ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   bool progress = false;
785ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
795ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   foreach_iter(exec_list_iterator, iter, *instructions) {
805ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      ir_instruction *ir = (ir_instruction *)iter.get();
815ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      ir_if *conditional = ir->as_if();
825ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
835ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      if (conditional) {
845ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 ir_constant *condition_constant;
855ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
865ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 condition_constant =
875ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    conditional->condition->constant_expression_value();
885ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 if (condition_constant) {
895ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    /* Move the contents of the one branch of the conditional
905ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	     * that matters out.
915ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	     */
925ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    if (condition_constant->value.b[0]) {
935ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	       foreach_iter(exec_list_iterator, then_iter,
945ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt			    conditional->then_instructions) {
955ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt		  ir_instruction *then_ir = (ir_instruction *)then_iter.get();
965ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt		  ir->insert_before(then_ir);
975ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	       }
985ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    } else {
995ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	       foreach_iter(exec_list_iterator, else_iter,
1005ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt			    conditional->else_instructions) {
1015ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt		  ir_instruction *else_ir = (ir_instruction *)else_iter.get();
1025ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt		  ir->insert_before(else_ir);
1035ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	       }
1045ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    }
1055ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    ir->remove();
1065ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    progress = true;
1075ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    /* It would be nice to move the iterator back up to the point
1085ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	     * that we just spliced in contents.
1095ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	     */
1105ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 } else {
1115ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    ir_if_simplification_visitor v;
1125ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	    ir->accept(&v);
1135ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 }
1145ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      } else {
1155ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 ir_if_simplification_visitor v;
1165ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 ir->accept(&v);
1175ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      }
1185ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
1195ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1205ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   return progress;
1215ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1225ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1235ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtclass variable_remap : public exec_node {
1245ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtpublic:
1255ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   variable_remap(const ir_variable *old_var, ir_variable *new_var)
1265ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      : old_var(old_var), new_var(new_var)
1275ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   {
1285ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      /* empty */
1295ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
1305ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   const ir_variable *old_var;
1315ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir_variable *new_var;
1325ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt};
1335ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1345ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1355ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_variable *ir)
1365ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1375ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   (void) ir;
1385ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1395ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1405ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1415ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1425ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_label *ir)
1435ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1445ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir->signature->accept(this);
1455ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1465ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1475ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1485ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_loop *ir)
1495ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1505ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   do_if_simplification(&ir->body_instructions);
1515ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1525ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1535ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1545ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_loop_jump *ir)
1555ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1565ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   (void) ir;
1575ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1585ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1595ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1605ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1615ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_function_signature *ir)
1625ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1635ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   do_if_simplification(&ir->body);
1645ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1655ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1665ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1675ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1685ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_function *ir)
1695ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1705ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   (void) ir;
1715ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1725ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1735ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1745ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_expression *ir)
1755ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1765ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   unsigned int operand;
1775ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1785ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   for (operand = 0; operand < ir->get_num_operands(); operand++) {
1795ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      ir->operands[operand]->accept(this);
1805ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
1815ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1825ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1835ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1845ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1855ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_swizzle *ir)
1865ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1875ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir->val->accept(this);
1885ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1895ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1905ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
1915ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
1925ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_dereference *ir)
1935ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
1945ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   if (ir->mode == ir_dereference::ir_reference_array) {
1955ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      ir->selector.array_index->accept(this);
1965ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
1975ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir->var->accept(this);
1985ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
1995ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2005ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
2015ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_assignment *ir)
2025ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
2035ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir->rhs->accept(this);
2045ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
2055ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2065ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2075ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
2085ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_constant *ir)
2095ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
2105ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   (void) ir;
2115ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
2125ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2135ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2145ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
2155ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_call *ir)
2165ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
2175ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   (void) ir;
2185ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
2195ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2205ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2215ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
2225ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_return *ir)
2235ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
2245ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   (void) ir;
2255ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
2265ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2275ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2285ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtvoid
2295ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtir_if_simplification_visitor::visit(ir_if *ir)
2305ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
2315ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir->condition->accept(this);
2325ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
2335ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   do_if_simplification(&ir->then_instructions);
2345ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   do_if_simplification(&ir->else_instructions);
2355ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
236