opt_if_simplification.cpp revision df883eb1575a740bf91e01cbe2eaa4dbc1f9f154
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 *); 415ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt 42a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick bool made_progress; 435ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt}; 445ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt 455ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtbool 465ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholtdo_if_simplification(exec_list *instructions) 475ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt{ 48a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir_if_simplification_visitor v; 495ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt 50a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick v.run(instructions); 51a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick return v.made_progress; 52a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick} 535ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt 545ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt 55a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_visitor_status 56a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanickir_if_simplification_visitor::visit_leave(ir_if *ir) 57a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick{ 58a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick /* FINISHME: Ideally there would be a way to note that the condition results 59a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick * FINISHME: in a constant before processing both of the other subtrees. 60a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick * FINISHME: This can probably be done with some flags, but it would take 61a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick * FINISHME: some work to get right. 62a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick */ 63a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir_constant *condition_constant = ir->condition->constant_expression_value(); 64a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick if (condition_constant) { 65a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick /* Move the contents of the one branch of the conditional 66a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick * that matters out. 67a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick */ 68a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick if (condition_constant->value.b[0]) { 69a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) { 70a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir_instruction *then_ir = (ir_instruction *)then_iter.get(); 71a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir->insert_before(then_ir); 725ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt } 735ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt } else { 74a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) { 75a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir_instruction *else_ir = (ir_instruction *)else_iter.get(); 76a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir->insert_before(else_ir); 77a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick } 785ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt } 79a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick ir->remove(); 80a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick this->made_progress = true; 815ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt } 825ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt 83a0b4f3d631cefa6ee3f341461e4754ef6462f89bIan Romanick return visit_continue; 845ba94206083fcd678febd6cac0231f35c0f1b77aEric Anholt} 85