opt_if_simplification.cpp revision a0b4f3d631cefa6ee3f341461e4754ef6462f89b
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
34a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickclass ir_if_simplification_visitor : public ir_hierarchical_visitor {
355ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtpublic:
365ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir_if_simplification_visitor()
375ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   {
38a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      this->made_progress = false;
395ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
405ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
41a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_visitor_status visit_leave(ir_if *);
425ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
43a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   bool made_progress;
445ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt};
455ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
465ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtbool
475ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtdo_if_simplification(exec_list *instructions)
485ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
49a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_if_simplification_visitor v;
505ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
51a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   v.run(instructions);
52a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   return v.made_progress;
53a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick}
545ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
555ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
56a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_visitor_status
57a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_if_simplification_visitor::visit_leave(ir_if *ir)
58a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick{
59a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   /* FINISHME: Ideally there would be a way to note that the condition results
60a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: in a constant before processing both of the other subtrees.
61a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: This can probably be done with some flags, but it would take
62a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: some work to get right.
63a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    */
64a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_constant *condition_constant = ir->condition->constant_expression_value();
65a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   if (condition_constant) {
66a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      /* Move the contents of the one branch of the conditional
67a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick       * that matters out.
68a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick       */
69a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      if (condition_constant->value.b[0]) {
70a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
71a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir_instruction *then_ir = (ir_instruction *)then_iter.get();
72a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir->insert_before(then_ir);
735ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 }
745ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      } else {
75a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
76a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir_instruction *else_ir = (ir_instruction *)else_iter.get();
77a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir->insert_before(else_ir);
78a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 }
795ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      }
80a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      ir->remove();
81a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      this->made_progress = true;
825ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
835ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
84a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   return visit_continue;
855ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
86