1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"), 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions: 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software. 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE. 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \file ir_expression_flattening.cpp 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Takes the leaves of expression trees and makes them dereferences of 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * assignments of the leaves to temporaries, according to a predicate. 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * This is used for breaking down matrix operations, where it's easier to 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * create a temporary and work on each of its vector components individually. 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h" 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_visitor.h" 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_rvalue_visitor.h" 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_expression_flattening.h" 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl_types.h" 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass ir_expression_flattening_visitor : public ir_rvalue_visitor { 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic: 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_expression_flattening_visitor(bool (*predicate)(ir_instruction *ir)) 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org { 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this->predicate = predicate; 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual ~ir_expression_flattening_visitor() 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org { 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /* empty */ 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void handle_rvalue(ir_rvalue **rvalue); 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool (*predicate)(ir_instruction *ir); 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdo_expression_flattening(exec_list *instructions, 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool (*predicate)(ir_instruction *ir)) 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_expression_flattening_visitor v(predicate); 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org foreach_iter(exec_list_iterator, iter, *instructions) { 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_instruction *ir = (ir_instruction *)iter.get(); 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir->accept(&v); 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_expression_flattening_visitor::handle_rvalue(ir_rvalue **rvalue) 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable *var; 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_assignment *assign; 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_rvalue *ir = *rvalue; 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!ir || !this->predicate(ir)) 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void *ctx = ralloc_parent(ir); 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org var = new(ctx) ir_variable(ir->type, "flattening_tmp", ir_var_temporary); 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org base_ir->insert_before(var); 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir, 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NULL); 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org base_ir->insert_before(assign); 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *rvalue = new(ctx) ir_dereference_variable(var); 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 91