opt_if_simplification.cpp revision 991fa4d3d07a3ebda2a1398d346b18b3afbaa736
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
33a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickclass ir_if_simplification_visitor : public ir_hierarchical_visitor {
345ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtpublic:
355ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   ir_if_simplification_visitor()
365ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   {
37a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      this->made_progress = false;
385ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
395ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
40a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_visitor_status visit_leave(ir_if *);
41991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt   ir_visitor_status visit_enter(ir_assignment *);
425ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
43a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   bool made_progress;
445ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt};
455ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
46991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt/* We only care about the top level "if" instructions, so don't
47991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt * descend into expressions.
48991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt */
49991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholtir_visitor_status
50991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholtir_if_simplification_visitor::visit_enter(ir_assignment *ir)
51991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt{
52991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt   return visit_continue_with_parent;
53991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt}
54991fa4d3d07a3ebda2a1398d346b18b3afbaa736Eric Anholt
555ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtbool
565ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtdo_if_simplification(exec_list *instructions)
575ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{
58a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_if_simplification_visitor v;
595ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
60a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   v.run(instructions);
61a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   return v.made_progress;
62a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick}
635ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
645ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
65a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_visitor_status
66a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_if_simplification_visitor::visit_leave(ir_if *ir)
67a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick{
68a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   /* FINISHME: Ideally there would be a way to note that the condition results
69a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: in a constant before processing both of the other subtrees.
70a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: This can probably be done with some flags, but it would take
71a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    * FINISHME: some work to get right.
72a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick    */
73a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   ir_constant *condition_constant = ir->condition->constant_expression_value();
74a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   if (condition_constant) {
75a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      /* Move the contents of the one branch of the conditional
76a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick       * that matters out.
77a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick       */
78a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      if (condition_constant->value.b[0]) {
79a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
80a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir_instruction *then_ir = (ir_instruction *)then_iter.get();
81a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir->insert_before(then_ir);
825ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt	 }
835ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      } else {
84a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
85a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir_instruction *else_ir = (ir_instruction *)else_iter.get();
86a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	    ir->insert_before(else_ir);
87a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick	 }
885ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt      }
89a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      ir->remove();
90a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick      this->made_progress = true;
915ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt   }
925ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt
93a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick   return visit_continue;
945ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}
95