11591693c7b415e9869157c711fe11263c95d74eDavid Li/* 21591693c7b415e9869157c711fe11263c95d74eDavid Li * Copyright © 2010 Intel Corporation 31591693c7b415e9869157c711fe11263c95d74eDavid Li * 41591693c7b415e9869157c711fe11263c95d74eDavid Li * Permission is hereby granted, free of charge, to any person obtaining a 51591693c7b415e9869157c711fe11263c95d74eDavid Li * copy of this software and associated documentation files (the "Software"), 61591693c7b415e9869157c711fe11263c95d74eDavid Li * to deal in the Software without restriction, including without limitation 71591693c7b415e9869157c711fe11263c95d74eDavid Li * the rights to use, copy, modify, merge, publish, distribute, sublicense, 81591693c7b415e9869157c711fe11263c95d74eDavid Li * and/or sell copies of the Software, and to permit persons to whom the 91591693c7b415e9869157c711fe11263c95d74eDavid Li * Software is furnished to do so, subject to the following conditions: 101591693c7b415e9869157c711fe11263c95d74eDavid Li * 111591693c7b415e9869157c711fe11263c95d74eDavid Li * The above copyright notice and this permission notice (including the next 121591693c7b415e9869157c711fe11263c95d74eDavid Li * paragraph) shall be included in all copies or substantial portions of the 131591693c7b415e9869157c711fe11263c95d74eDavid Li * Software. 141591693c7b415e9869157c711fe11263c95d74eDavid Li * 151591693c7b415e9869157c711fe11263c95d74eDavid Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 161591693c7b415e9869157c711fe11263c95d74eDavid Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 171591693c7b415e9869157c711fe11263c95d74eDavid Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 181591693c7b415e9869157c711fe11263c95d74eDavid Li * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 191591693c7b415e9869157c711fe11263c95d74eDavid Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 201591693c7b415e9869157c711fe11263c95d74eDavid Li * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 211591693c7b415e9869157c711fe11263c95d74eDavid Li * DEALINGS IN THE SOFTWARE. 221591693c7b415e9869157c711fe11263c95d74eDavid Li */ 231591693c7b415e9869157c711fe11263c95d74eDavid Li 241591693c7b415e9869157c711fe11263c95d74eDavid Li/** 251591693c7b415e9869157c711fe11263c95d74eDavid Li * \file opt_if_simplification.cpp 261591693c7b415e9869157c711fe11263c95d74eDavid Li * 271591693c7b415e9869157c711fe11263c95d74eDavid Li * Moves constant branches of if statements out to the surrounding 281591693c7b415e9869157c711fe11263c95d74eDavid Li * instruction stream. 291591693c7b415e9869157c711fe11263c95d74eDavid Li */ 301591693c7b415e9869157c711fe11263c95d74eDavid Li 311591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ir.h" 321591693c7b415e9869157c711fe11263c95d74eDavid Li 331591693c7b415e9869157c711fe11263c95d74eDavid Liclass ir_if_simplification_visitor : public ir_hierarchical_visitor { 341591693c7b415e9869157c711fe11263c95d74eDavid Lipublic: 351591693c7b415e9869157c711fe11263c95d74eDavid Li ir_if_simplification_visitor() 361591693c7b415e9869157c711fe11263c95d74eDavid Li { 371591693c7b415e9869157c711fe11263c95d74eDavid Li this->made_progress = false; 381591693c7b415e9869157c711fe11263c95d74eDavid Li } 391591693c7b415e9869157c711fe11263c95d74eDavid Li 401591693c7b415e9869157c711fe11263c95d74eDavid Li ir_visitor_status visit_leave(ir_if *); 411591693c7b415e9869157c711fe11263c95d74eDavid Li 421591693c7b415e9869157c711fe11263c95d74eDavid Li bool made_progress; 431591693c7b415e9869157c711fe11263c95d74eDavid Li}; 441591693c7b415e9869157c711fe11263c95d74eDavid Li 451591693c7b415e9869157c711fe11263c95d74eDavid Libool 461591693c7b415e9869157c711fe11263c95d74eDavid Lido_if_simplification(exec_list *instructions) 471591693c7b415e9869157c711fe11263c95d74eDavid Li{ 481591693c7b415e9869157c711fe11263c95d74eDavid Li ir_if_simplification_visitor v; 491591693c7b415e9869157c711fe11263c95d74eDavid Li 501591693c7b415e9869157c711fe11263c95d74eDavid Li v.run(instructions); 511591693c7b415e9869157c711fe11263c95d74eDavid Li return v.made_progress; 521591693c7b415e9869157c711fe11263c95d74eDavid Li} 531591693c7b415e9869157c711fe11263c95d74eDavid Li 541591693c7b415e9869157c711fe11263c95d74eDavid Li 551591693c7b415e9869157c711fe11263c95d74eDavid Liir_visitor_status 561591693c7b415e9869157c711fe11263c95d74eDavid Liir_if_simplification_visitor::visit_leave(ir_if *ir) 571591693c7b415e9869157c711fe11263c95d74eDavid Li{ 581591693c7b415e9869157c711fe11263c95d74eDavid Li /* FINISHME: Ideally there would be a way to note that the condition results 591591693c7b415e9869157c711fe11263c95d74eDavid Li * FINISHME: in a constant before processing both of the other subtrees. 601591693c7b415e9869157c711fe11263c95d74eDavid Li * FINISHME: This can probably be done with some flags, but it would take 611591693c7b415e9869157c711fe11263c95d74eDavid Li * FINISHME: some work to get right. 621591693c7b415e9869157c711fe11263c95d74eDavid Li */ 631591693c7b415e9869157c711fe11263c95d74eDavid Li ir_constant *condition_constant = ir->condition->constant_expression_value(); 641591693c7b415e9869157c711fe11263c95d74eDavid Li if (condition_constant) { 651591693c7b415e9869157c711fe11263c95d74eDavid Li /* Move the contents of the one branch of the conditional 661591693c7b415e9869157c711fe11263c95d74eDavid Li * that matters out. 671591693c7b415e9869157c711fe11263c95d74eDavid Li */ 681591693c7b415e9869157c711fe11263c95d74eDavid Li if (condition_constant->value.b[0]) { 691591693c7b415e9869157c711fe11263c95d74eDavid Li foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) { 701591693c7b415e9869157c711fe11263c95d74eDavid Li ir_instruction *then_ir = (ir_instruction *)then_iter.get(); 711591693c7b415e9869157c711fe11263c95d74eDavid Li ir->insert_before(then_ir); 721591693c7b415e9869157c711fe11263c95d74eDavid Li } 731591693c7b415e9869157c711fe11263c95d74eDavid Li } else { 741591693c7b415e9869157c711fe11263c95d74eDavid Li foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) { 751591693c7b415e9869157c711fe11263c95d74eDavid Li ir_instruction *else_ir = (ir_instruction *)else_iter.get(); 761591693c7b415e9869157c711fe11263c95d74eDavid Li ir->insert_before(else_ir); 771591693c7b415e9869157c711fe11263c95d74eDavid Li } 781591693c7b415e9869157c711fe11263c95d74eDavid Li } 791591693c7b415e9869157c711fe11263c95d74eDavid Li ir->remove(); 801591693c7b415e9869157c711fe11263c95d74eDavid Li this->made_progress = true; 811591693c7b415e9869157c711fe11263c95d74eDavid Li } 821591693c7b415e9869157c711fe11263c95d74eDavid Li 831591693c7b415e9869157c711fe11263c95d74eDavid Li return visit_continue; 841591693c7b415e9869157c711fe11263c95d74eDavid Li} 85