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/**
25df883eb1575a740bf91e01cbe2eaa4dbc1f9f154Chad Versace * \file opt_if_simplification.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#include "ir.h"
325ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
33337d9c955b070224f7278524af54ddacd8bb0f17Eric Anholtnamespace {
34337d9c955b070224f7278524af54ddacd8bb0f17Eric Anholt
35a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickclass ir_if_simplification_visitor : public ir_hierarchical_visitor {
365ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtpublic:
375ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir_if_simplification_visitor()
385ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   {
39a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      this->made_progress = false;
405ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
415ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
42a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_visitor_status visit_leave(ir_if *);
43991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt   ir_visitor_status visit_enter(ir_assignment *);
445ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
45a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   bool made_progress;
465ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt};
475ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
48337d9c955b070224f7278524af54ddacd8bb0f17Eric Anholt} /* unnamed namespace */
49337d9c955b070224f7278524af54ddacd8bb0f17Eric Anholt
50991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt/* We only care about the top level "if" instructions, so don't
51991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt * descend into expressions.
52991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt */
53991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholtir_visitor_status
54991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholtir_if_simplification_visitor::visit_enter(ir_assignment *ir)
55991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt{
569a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   (void) ir;
57991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt   return visit_continue_with_parent;
58991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt}
59991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt
605ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtbool
615ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtdo_if_simplification(exec_list *instructions)
625ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
63a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_if_simplification_visitor v;
645ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
65a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   v.run(instructions);
66a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   return v.made_progress;
67a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick}
685ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
695ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
70a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_visitor_status
71a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_if_simplification_visitor::visit_leave(ir_if *ir)
72a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick{
733f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt   /* If the if statement has nothing on either side, remove it. */
743f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt   if (ir->then_instructions.is_empty() &&
753f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt       ir->else_instructions.is_empty()) {
763f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt      ir->remove();
773f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt      this->made_progress = true;
783f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt      return visit_continue;
793f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt   }
803f349d4e18d8c114a34fc3c36e1dc55345c1cc31Eric Anholt
81a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   /* FINISHME: Ideally there would be a way to note that the condition results
82a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: in a constant before processing both of the other subtrees.
83a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: This can probably be done with some flags, but it would take
84a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: some work to get right.
85a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    */
86a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_constant *condition_constant = ir->condition->constant_expression_value();
87a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   if (condition_constant) {
88a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      /* Move the contents of the one branch of the conditional
89a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick       * that matters out.
90a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick       */
91a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      if (condition_constant->value.b[0]) {
92a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
93a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir_instruction *then_ir = (ir_instruction *)then_iter.get();
94a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir->insert_before(then_ir);
955ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 }
965ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      } else {
97a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
98a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir_instruction *else_ir = (ir_instruction *)else_iter.get();
99a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir->insert_before(else_ir);
100a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 }
1015ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      }
102a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      ir->remove();
103a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      this->made_progress = true;
1045ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
1055ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
106a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   return visit_continue;
1075ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
108