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_variable_refcount.cpp 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Provides a visitor which produces a list of variables referenced, 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * how many times they were referenced and assigned, and whether they 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * were defined in the scope. 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h" 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_visitor.h" 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_variable_refcount.h" 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl_types.h" 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// constructor 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_entry::ir_variable_refcount_entry(ir_variable *var) 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this->var = var; 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assign = NULL; 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assigned_count = 0; 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org declaration = false; 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org referenced_count = 0; 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_entry * 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_visitor::get_variable_entry(ir_variable *var) 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assert(var); 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org foreach_iter(exec_list_iterator, iter, this->variable_list) { 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable_refcount_entry *entry = (ir_variable_refcount_entry *)iter.get(); 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (entry->var == var) 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return entry; 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable_refcount_entry *entry = new(mem_ctx) ir_variable_refcount_entry(var); 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org assert(entry->referenced_count == 0); 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this->variable_list.push_tail(entry); 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return entry; 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_visitor::visit(ir_variable *ir) 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable_refcount_entry *entry = this->get_variable_entry(ir); 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (entry) 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org entry->declaration = true; 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return visit_continue; 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_visitor::visit(ir_dereference_variable *ir) 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable *const var = ir->variable_referenced(); 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable_refcount_entry *entry = this->get_variable_entry(var); 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (entry) 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org entry->referenced_count++; 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return visit_continue; 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_visitor::visit_enter(ir_function_signature *ir) 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org /* We don't want to descend into the function parameters and 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * dead-code eliminate them, so just accept the body here. 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */ 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org visit_list_elements(this, &ir->body); 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return visit_continue_with_parent; 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_variable_refcount_visitor::visit_leave(ir_assignment *ir) 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{ 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ir_variable_refcount_entry *entry; 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org entry = this->get_variable_entry(ir->lhs->variable_referenced()); 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (entry) { 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org entry->assigned_count++; 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (entry->assign == NULL) 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org entry->assign = ir; 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return visit_continue; 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 114