1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2010 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21b8e80941Smrg * DEALINGS IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg/** 25b8e80941Smrg * \file lower_discard.cpp 26b8e80941Smrg * 27b8e80941Smrg * This pass moves discards out of if-statements. 28b8e80941Smrg * 29b8e80941Smrg * Case 1: The "then" branch contains a conditional discard: 30b8e80941Smrg * --------------------------------------------------------- 31b8e80941Smrg * 32b8e80941Smrg * if (cond1) { 33b8e80941Smrg * s1; 34b8e80941Smrg * discard cond2; 35b8e80941Smrg * s2; 36b8e80941Smrg * } else { 37b8e80941Smrg * s3; 38b8e80941Smrg * } 39b8e80941Smrg * 40b8e80941Smrg * becomes: 41b8e80941Smrg * 42b8e80941Smrg * temp = false; 43b8e80941Smrg * if (cond1) { 44b8e80941Smrg * s1; 45b8e80941Smrg * temp = cond2; 46b8e80941Smrg * s2; 47b8e80941Smrg * } else { 48b8e80941Smrg * s3; 49b8e80941Smrg * } 50b8e80941Smrg * discard temp; 51b8e80941Smrg * 52b8e80941Smrg * Case 2: The "else" branch contains a conditional discard: 53b8e80941Smrg * --------------------------------------------------------- 54b8e80941Smrg * 55b8e80941Smrg * if (cond1) { 56b8e80941Smrg * s1; 57b8e80941Smrg * } else { 58b8e80941Smrg * s2; 59b8e80941Smrg * discard cond2; 60b8e80941Smrg * s3; 61b8e80941Smrg * } 62b8e80941Smrg * 63b8e80941Smrg * becomes: 64b8e80941Smrg * 65b8e80941Smrg * temp = false; 66b8e80941Smrg * if (cond1) { 67b8e80941Smrg * s1; 68b8e80941Smrg * } else { 69b8e80941Smrg * s2; 70b8e80941Smrg * temp = cond2; 71b8e80941Smrg * s3; 72b8e80941Smrg * } 73b8e80941Smrg * discard temp; 74b8e80941Smrg * 75b8e80941Smrg * Case 3: Both branches contain a conditional discard: 76b8e80941Smrg * ---------------------------------------------------- 77b8e80941Smrg * 78b8e80941Smrg * if (cond1) { 79b8e80941Smrg * s1; 80b8e80941Smrg * discard cond2; 81b8e80941Smrg * s2; 82b8e80941Smrg * } else { 83b8e80941Smrg * s3; 84b8e80941Smrg * discard cond3; 85b8e80941Smrg * s4; 86b8e80941Smrg * } 87b8e80941Smrg * 88b8e80941Smrg * becomes: 89b8e80941Smrg * 90b8e80941Smrg * temp = false; 91b8e80941Smrg * if (cond1) { 92b8e80941Smrg * s1; 93b8e80941Smrg * temp = cond2; 94b8e80941Smrg * s2; 95b8e80941Smrg * } else { 96b8e80941Smrg * s3; 97b8e80941Smrg * temp = cond3; 98b8e80941Smrg * s4; 99b8e80941Smrg * } 100b8e80941Smrg * discard temp; 101b8e80941Smrg * 102b8e80941Smrg * If there are multiple conditional discards, we need only deal with one of 103b8e80941Smrg * them. Repeatedly applying this pass will take care of the others. 104b8e80941Smrg * 105b8e80941Smrg * Unconditional discards are treated as having a condition of "true". 106b8e80941Smrg */ 107b8e80941Smrg 108b8e80941Smrg#include "compiler/glsl_types.h" 109b8e80941Smrg#include "ir.h" 110b8e80941Smrg 111b8e80941Smrgnamespace { 112b8e80941Smrg 113b8e80941Smrgclass lower_discard_visitor : public ir_hierarchical_visitor { 114b8e80941Smrgpublic: 115b8e80941Smrg lower_discard_visitor() 116b8e80941Smrg { 117b8e80941Smrg this->progress = false; 118b8e80941Smrg } 119b8e80941Smrg 120b8e80941Smrg ir_visitor_status visit_leave(ir_if *); 121b8e80941Smrg 122b8e80941Smrg bool progress; 123b8e80941Smrg}; 124b8e80941Smrg 125b8e80941Smrg} /* anonymous namespace */ 126b8e80941Smrg 127b8e80941Smrgbool 128b8e80941Smrglower_discard(exec_list *instructions) 129b8e80941Smrg{ 130b8e80941Smrg lower_discard_visitor v; 131b8e80941Smrg 132b8e80941Smrg visit_list_elements(&v, instructions); 133b8e80941Smrg 134b8e80941Smrg return v.progress; 135b8e80941Smrg} 136b8e80941Smrg 137b8e80941Smrg 138b8e80941Smrgstatic ir_discard * 139b8e80941Smrgfind_discard(exec_list &instructions) 140b8e80941Smrg{ 141b8e80941Smrg foreach_in_list(ir_instruction, node, &instructions) { 142b8e80941Smrg ir_discard *ir = node->as_discard(); 143b8e80941Smrg if (ir != NULL) 144b8e80941Smrg return ir; 145b8e80941Smrg } 146b8e80941Smrg return NULL; 147b8e80941Smrg} 148b8e80941Smrg 149b8e80941Smrg 150b8e80941Smrgstatic void 151b8e80941Smrgreplace_discard(void *mem_ctx, ir_variable *var, ir_discard *ir) 152b8e80941Smrg{ 153b8e80941Smrg ir_rvalue *condition = ir->condition; 154b8e80941Smrg 155b8e80941Smrg /* For unconditional discards, use "true" as the condition. */ 156b8e80941Smrg if (condition == NULL) 157b8e80941Smrg condition = new(mem_ctx) ir_constant(true); 158b8e80941Smrg 159b8e80941Smrg ir_assignment *assignment = 160b8e80941Smrg new(mem_ctx) ir_assignment(new(mem_ctx) ir_dereference_variable(var), 161b8e80941Smrg condition); 162b8e80941Smrg 163b8e80941Smrg ir->replace_with(assignment); 164b8e80941Smrg} 165b8e80941Smrg 166b8e80941Smrg 167b8e80941Smrgir_visitor_status 168b8e80941Smrglower_discard_visitor::visit_leave(ir_if *ir) 169b8e80941Smrg{ 170b8e80941Smrg ir_discard *then_discard = find_discard(ir->then_instructions); 171b8e80941Smrg ir_discard *else_discard = find_discard(ir->else_instructions); 172b8e80941Smrg 173b8e80941Smrg if (then_discard == NULL && else_discard == NULL) 174b8e80941Smrg return visit_continue; 175b8e80941Smrg 176b8e80941Smrg void *mem_ctx = ralloc_parent(ir); 177b8e80941Smrg 178b8e80941Smrg ir_variable *temp = new(mem_ctx) ir_variable(glsl_type::bool_type, 179b8e80941Smrg "discard_cond_temp", 180b8e80941Smrg ir_var_temporary); 181b8e80941Smrg ir_assignment *temp_initializer = 182b8e80941Smrg new(mem_ctx) ir_assignment(new(mem_ctx) ir_dereference_variable(temp), 183b8e80941Smrg new(mem_ctx) ir_constant(false)); 184b8e80941Smrg 185b8e80941Smrg ir->insert_before(temp); 186b8e80941Smrg ir->insert_before(temp_initializer); 187b8e80941Smrg 188b8e80941Smrg if (then_discard != NULL) 189b8e80941Smrg replace_discard(mem_ctx, temp, then_discard); 190b8e80941Smrg 191b8e80941Smrg if (else_discard != NULL) 192b8e80941Smrg replace_discard(mem_ctx, temp, else_discard); 193b8e80941Smrg 194b8e80941Smrg ir_discard *discard = then_discard != NULL ? then_discard : else_discard; 195b8e80941Smrg discard->condition = new(mem_ctx) ir_dereference_variable(temp); 196b8e80941Smrg ir->insert_after(discard); 197b8e80941Smrg 198b8e80941Smrg this->progress = true; 199b8e80941Smrg 200b8e80941Smrg return visit_continue; 201b8e80941Smrg} 202