1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2010 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * constant 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, constant, 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 constantright 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 CONSTANTRIGHT 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 opt_constant_propagation.cpp 26b8e80941Smrg * 27b8e80941Smrg * Tracks assignments of constants to channels of variables, and 28b8e80941Smrg * usage of those constant channels with direct usage of the constants. 29b8e80941Smrg * 30b8e80941Smrg * This can lead to constant folding and algebraic optimizations in 31b8e80941Smrg * those later expressions, while causing no increase in instruction 32b8e80941Smrg * count (due to constants being generally free to load from a 33b8e80941Smrg * constant push buffer or as instruction immediate values) and 34b8e80941Smrg * possibly reducing register pressure. 35b8e80941Smrg */ 36b8e80941Smrg 37b8e80941Smrg#include "ir.h" 38b8e80941Smrg#include "ir_visitor.h" 39b8e80941Smrg#include "ir_rvalue_visitor.h" 40b8e80941Smrg#include "ir_basic_block.h" 41b8e80941Smrg#include "ir_optimization.h" 42b8e80941Smrg#include "compiler/glsl_types.h" 43b8e80941Smrg#include "util/hash_table.h" 44b8e80941Smrg 45b8e80941Smrgnamespace { 46b8e80941Smrg 47b8e80941Smrgclass acp_entry : public exec_node 48b8e80941Smrg{ 49b8e80941Smrgpublic: 50b8e80941Smrg /* override operator new from exec_node */ 51b8e80941Smrg DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(acp_entry) 52b8e80941Smrg 53b8e80941Smrg acp_entry(ir_variable *var, unsigned write_mask, ir_constant *constant) 54b8e80941Smrg { 55b8e80941Smrg assert(var); 56b8e80941Smrg assert(constant); 57b8e80941Smrg this->var = var; 58b8e80941Smrg this->write_mask = write_mask; 59b8e80941Smrg this->constant = constant; 60b8e80941Smrg this->initial_values = write_mask; 61b8e80941Smrg } 62b8e80941Smrg 63b8e80941Smrg acp_entry(const acp_entry *src) 64b8e80941Smrg { 65b8e80941Smrg this->var = src->var; 66b8e80941Smrg this->write_mask = src->write_mask; 67b8e80941Smrg this->constant = src->constant; 68b8e80941Smrg this->initial_values = src->initial_values; 69b8e80941Smrg } 70b8e80941Smrg 71b8e80941Smrg ir_variable *var; 72b8e80941Smrg ir_constant *constant; 73b8e80941Smrg unsigned write_mask; 74b8e80941Smrg 75b8e80941Smrg /** Mask of values initially available in the constant. */ 76b8e80941Smrg unsigned initial_values; 77b8e80941Smrg}; 78b8e80941Smrg 79b8e80941Smrg 80b8e80941Smrgclass ir_constant_propagation_visitor : public ir_rvalue_visitor { 81b8e80941Smrgpublic: 82b8e80941Smrg ir_constant_propagation_visitor() 83b8e80941Smrg { 84b8e80941Smrg progress = false; 85b8e80941Smrg killed_all = false; 86b8e80941Smrg mem_ctx = ralloc_context(0); 87b8e80941Smrg this->lin_ctx = linear_alloc_parent(this->mem_ctx, 0); 88b8e80941Smrg this->acp = new(mem_ctx) exec_list; 89b8e80941Smrg this->kills = _mesa_pointer_hash_table_create(mem_ctx); 90b8e80941Smrg } 91b8e80941Smrg ~ir_constant_propagation_visitor() 92b8e80941Smrg { 93b8e80941Smrg ralloc_free(mem_ctx); 94b8e80941Smrg } 95b8e80941Smrg 96b8e80941Smrg virtual ir_visitor_status visit_enter(class ir_loop *); 97b8e80941Smrg virtual ir_visitor_status visit_enter(class ir_function_signature *); 98b8e80941Smrg virtual ir_visitor_status visit_enter(class ir_function *); 99b8e80941Smrg virtual ir_visitor_status visit_leave(class ir_assignment *); 100b8e80941Smrg virtual ir_visitor_status visit_enter(class ir_call *); 101b8e80941Smrg virtual ir_visitor_status visit_enter(class ir_if *); 102b8e80941Smrg 103b8e80941Smrg void add_constant(ir_assignment *ir); 104b8e80941Smrg void constant_folding(ir_rvalue **rvalue); 105b8e80941Smrg void constant_propagation(ir_rvalue **rvalue); 106b8e80941Smrg void kill(ir_variable *ir, unsigned write_mask); 107b8e80941Smrg void handle_if_block(exec_list *instructions, hash_table *kills, bool *killed_all); 108b8e80941Smrg void handle_loop(class ir_loop *, bool keep_acp); 109b8e80941Smrg void handle_rvalue(ir_rvalue **rvalue); 110b8e80941Smrg 111b8e80941Smrg /** List of acp_entry: The available constants to propagate */ 112b8e80941Smrg exec_list *acp; 113b8e80941Smrg 114b8e80941Smrg /** 115b8e80941Smrg * Hash table of killed entries: maps variables to the mask of killed channels. 116b8e80941Smrg */ 117b8e80941Smrg hash_table *kills; 118b8e80941Smrg 119b8e80941Smrg bool progress; 120b8e80941Smrg 121b8e80941Smrg bool killed_all; 122b8e80941Smrg 123b8e80941Smrg void *mem_ctx; 124b8e80941Smrg void *lin_ctx; 125b8e80941Smrg}; 126b8e80941Smrg 127b8e80941Smrg 128b8e80941Smrgvoid 129b8e80941Smrgir_constant_propagation_visitor::constant_folding(ir_rvalue **rvalue) 130b8e80941Smrg{ 131b8e80941Smrg if (this->in_assignee || *rvalue == NULL) 132b8e80941Smrg return; 133b8e80941Smrg 134b8e80941Smrg if (ir_constant_fold(rvalue)) 135b8e80941Smrg this->progress = true; 136b8e80941Smrg 137b8e80941Smrg ir_dereference_variable *var_ref = (*rvalue)->as_dereference_variable(); 138b8e80941Smrg if (var_ref && !var_ref->type->is_array()) { 139b8e80941Smrg ir_constant *constant = 140b8e80941Smrg var_ref->constant_expression_value(ralloc_parent(var_ref)); 141b8e80941Smrg if (constant) { 142b8e80941Smrg *rvalue = constant; 143b8e80941Smrg this->progress = true; 144b8e80941Smrg } 145b8e80941Smrg } 146b8e80941Smrg} 147b8e80941Smrg 148b8e80941Smrgvoid 149b8e80941Smrgir_constant_propagation_visitor::constant_propagation(ir_rvalue **rvalue) { 150b8e80941Smrg 151b8e80941Smrg if (this->in_assignee || !*rvalue) 152b8e80941Smrg return; 153b8e80941Smrg 154b8e80941Smrg const glsl_type *type = (*rvalue)->type; 155b8e80941Smrg if (!type->is_scalar() && !type->is_vector()) 156b8e80941Smrg return; 157b8e80941Smrg 158b8e80941Smrg ir_swizzle *swiz = NULL; 159b8e80941Smrg ir_dereference_variable *deref = (*rvalue)->as_dereference_variable(); 160b8e80941Smrg if (!deref) { 161b8e80941Smrg swiz = (*rvalue)->as_swizzle(); 162b8e80941Smrg if (!swiz) 163b8e80941Smrg return; 164b8e80941Smrg 165b8e80941Smrg deref = swiz->val->as_dereference_variable(); 166b8e80941Smrg if (!deref) 167b8e80941Smrg return; 168b8e80941Smrg } 169b8e80941Smrg 170b8e80941Smrg ir_constant_data data; 171b8e80941Smrg memset(&data, 0, sizeof(data)); 172b8e80941Smrg 173b8e80941Smrg for (unsigned int i = 0; i < type->components(); i++) { 174b8e80941Smrg int channel; 175b8e80941Smrg acp_entry *found = NULL; 176b8e80941Smrg 177b8e80941Smrg if (swiz) { 178b8e80941Smrg switch (i) { 179b8e80941Smrg case 0: channel = swiz->mask.x; break; 180b8e80941Smrg case 1: channel = swiz->mask.y; break; 181b8e80941Smrg case 2: channel = swiz->mask.z; break; 182b8e80941Smrg case 3: channel = swiz->mask.w; break; 183b8e80941Smrg default: assert(!"shouldn't be reached"); channel = 0; break; 184b8e80941Smrg } 185b8e80941Smrg } else { 186b8e80941Smrg channel = i; 187b8e80941Smrg } 188b8e80941Smrg 189b8e80941Smrg foreach_in_list(acp_entry, entry, this->acp) { 190b8e80941Smrg if (entry->var == deref->var && entry->write_mask & (1 << channel)) { 191b8e80941Smrg found = entry; 192b8e80941Smrg break; 193b8e80941Smrg } 194b8e80941Smrg } 195b8e80941Smrg 196b8e80941Smrg if (!found) 197b8e80941Smrg return; 198b8e80941Smrg 199b8e80941Smrg int rhs_channel = 0; 200b8e80941Smrg for (int j = 0; j < 4; j++) { 201b8e80941Smrg if (j == channel) 202b8e80941Smrg break; 203b8e80941Smrg if (found->initial_values & (1 << j)) 204b8e80941Smrg rhs_channel++; 205b8e80941Smrg } 206b8e80941Smrg 207b8e80941Smrg switch (type->base_type) { 208b8e80941Smrg case GLSL_TYPE_FLOAT: 209b8e80941Smrg data.f[i] = found->constant->value.f[rhs_channel]; 210b8e80941Smrg break; 211b8e80941Smrg case GLSL_TYPE_DOUBLE: 212b8e80941Smrg data.d[i] = found->constant->value.d[rhs_channel]; 213b8e80941Smrg break; 214b8e80941Smrg case GLSL_TYPE_INT: 215b8e80941Smrg data.i[i] = found->constant->value.i[rhs_channel]; 216b8e80941Smrg break; 217b8e80941Smrg case GLSL_TYPE_UINT: 218b8e80941Smrg data.u[i] = found->constant->value.u[rhs_channel]; 219b8e80941Smrg break; 220b8e80941Smrg case GLSL_TYPE_BOOL: 221b8e80941Smrg data.b[i] = found->constant->value.b[rhs_channel]; 222b8e80941Smrg break; 223b8e80941Smrg case GLSL_TYPE_UINT64: 224b8e80941Smrg data.u64[i] = found->constant->value.u64[rhs_channel]; 225b8e80941Smrg break; 226b8e80941Smrg case GLSL_TYPE_INT64: 227b8e80941Smrg data.i64[i] = found->constant->value.i64[rhs_channel]; 228b8e80941Smrg break; 229b8e80941Smrg default: 230b8e80941Smrg assert(!"not reached"); 231b8e80941Smrg break; 232b8e80941Smrg } 233b8e80941Smrg } 234b8e80941Smrg 235b8e80941Smrg *rvalue = new(ralloc_parent(deref)) ir_constant(type, &data); 236b8e80941Smrg this->progress = true; 237b8e80941Smrg} 238b8e80941Smrg 239b8e80941Smrgvoid 240b8e80941Smrgir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue) 241b8e80941Smrg{ 242b8e80941Smrg constant_propagation(rvalue); 243b8e80941Smrg constant_folding(rvalue); 244b8e80941Smrg} 245b8e80941Smrg 246b8e80941Smrgir_visitor_status 247b8e80941Smrgir_constant_propagation_visitor::visit_enter(ir_function_signature *ir) 248b8e80941Smrg{ 249b8e80941Smrg /* Treat entry into a function signature as a completely separate 250b8e80941Smrg * block. Any instructions at global scope will be shuffled into 251b8e80941Smrg * main() at link time, so they're irrelevant to us. 252b8e80941Smrg */ 253b8e80941Smrg exec_list *orig_acp = this->acp; 254b8e80941Smrg hash_table *orig_kills = this->kills; 255b8e80941Smrg bool orig_killed_all = this->killed_all; 256b8e80941Smrg 257b8e80941Smrg this->acp = new(mem_ctx) exec_list; 258b8e80941Smrg this->kills = _mesa_pointer_hash_table_create(mem_ctx); 259b8e80941Smrg this->killed_all = false; 260b8e80941Smrg 261b8e80941Smrg visit_list_elements(this, &ir->body); 262b8e80941Smrg 263b8e80941Smrg this->kills = orig_kills; 264b8e80941Smrg this->acp = orig_acp; 265b8e80941Smrg this->killed_all = orig_killed_all; 266b8e80941Smrg 267b8e80941Smrg return visit_continue_with_parent; 268b8e80941Smrg} 269b8e80941Smrg 270b8e80941Smrgir_visitor_status 271b8e80941Smrgir_constant_propagation_visitor::visit_leave(ir_assignment *ir) 272b8e80941Smrg{ 273b8e80941Smrg constant_folding(&ir->rhs); 274b8e80941Smrg 275b8e80941Smrg if (this->in_assignee) 276b8e80941Smrg return visit_continue; 277b8e80941Smrg 278b8e80941Smrg unsigned kill_mask = ir->write_mask; 279b8e80941Smrg if (ir->lhs->as_dereference_array()) { 280b8e80941Smrg /* The LHS of the assignment uses an array indexing operator (e.g. v[i] 281b8e80941Smrg * = ...;). Since we only try to constant propagate vectors and 282b8e80941Smrg * scalars, this means that either (a) array indexing is being used to 283b8e80941Smrg * select a vector component, or (b) the variable in question is neither 284b8e80941Smrg * a scalar or a vector, so we don't care about it. In the former case, 285b8e80941Smrg * we want to kill the whole vector, since in general we can't predict 286b8e80941Smrg * which vector component will be selected by array indexing. In the 287b8e80941Smrg * latter case, it doesn't matter what we do, so go ahead and kill the 288b8e80941Smrg * whole variable anyway. 289b8e80941Smrg * 290b8e80941Smrg * Note that if the array index is constant (e.g. v[2] = ...;), we could 291b8e80941Smrg * in principle be smarter, but we don't need to, because a future 292b8e80941Smrg * optimization pass will convert it to a simple assignment with the 293b8e80941Smrg * correct mask. 294b8e80941Smrg */ 295b8e80941Smrg kill_mask = ~0; 296b8e80941Smrg } 297b8e80941Smrg kill(ir->lhs->variable_referenced(), kill_mask); 298b8e80941Smrg 299b8e80941Smrg add_constant(ir); 300b8e80941Smrg 301b8e80941Smrg return visit_continue; 302b8e80941Smrg} 303b8e80941Smrg 304b8e80941Smrgir_visitor_status 305b8e80941Smrgir_constant_propagation_visitor::visit_enter(ir_function *ir) 306b8e80941Smrg{ 307b8e80941Smrg (void) ir; 308b8e80941Smrg return visit_continue; 309b8e80941Smrg} 310b8e80941Smrg 311b8e80941Smrgir_visitor_status 312b8e80941Smrgir_constant_propagation_visitor::visit_enter(ir_call *ir) 313b8e80941Smrg{ 314b8e80941Smrg /* Do constant propagation on call parameters, but skip any out params */ 315b8e80941Smrg foreach_two_lists(formal_node, &ir->callee->parameters, 316b8e80941Smrg actual_node, &ir->actual_parameters) { 317b8e80941Smrg ir_variable *sig_param = (ir_variable *) formal_node; 318b8e80941Smrg ir_rvalue *param = (ir_rvalue *) actual_node; 319b8e80941Smrg if (sig_param->data.mode != ir_var_function_out 320b8e80941Smrg && sig_param->data.mode != ir_var_function_inout) { 321b8e80941Smrg ir_rvalue *new_param = param; 322b8e80941Smrg handle_rvalue(&new_param); 323b8e80941Smrg if (new_param != param) 324b8e80941Smrg param->replace_with(new_param); 325b8e80941Smrg else 326b8e80941Smrg param->accept(this); 327b8e80941Smrg } 328b8e80941Smrg } 329b8e80941Smrg 330b8e80941Smrg /* Since we're unlinked, we don't (necssarily) know the side effects of 331b8e80941Smrg * this call. So kill all copies. 332b8e80941Smrg */ 333b8e80941Smrg acp->make_empty(); 334b8e80941Smrg this->killed_all = true; 335b8e80941Smrg 336b8e80941Smrg return visit_continue_with_parent; 337b8e80941Smrg} 338b8e80941Smrg 339b8e80941Smrgvoid 340b8e80941Smrgir_constant_propagation_visitor::handle_if_block(exec_list *instructions, hash_table *kills, bool *killed_all) 341b8e80941Smrg{ 342b8e80941Smrg exec_list *orig_acp = this->acp; 343b8e80941Smrg hash_table *orig_kills = this->kills; 344b8e80941Smrg bool orig_killed_all = this->killed_all; 345b8e80941Smrg 346b8e80941Smrg this->acp = new(mem_ctx) exec_list; 347b8e80941Smrg this->kills = kills; 348b8e80941Smrg this->killed_all = false; 349b8e80941Smrg 350b8e80941Smrg /* Populate the initial acp with a constant of the original */ 351b8e80941Smrg foreach_in_list(acp_entry, a, orig_acp) { 352b8e80941Smrg this->acp->push_tail(new(this->lin_ctx) acp_entry(a)); 353b8e80941Smrg } 354b8e80941Smrg 355b8e80941Smrg visit_list_elements(this, instructions); 356b8e80941Smrg 357b8e80941Smrg *killed_all = this->killed_all; 358b8e80941Smrg this->kills = orig_kills; 359b8e80941Smrg this->acp = orig_acp; 360b8e80941Smrg this->killed_all = orig_killed_all; 361b8e80941Smrg} 362b8e80941Smrg 363b8e80941Smrgir_visitor_status 364b8e80941Smrgir_constant_propagation_visitor::visit_enter(ir_if *ir) 365b8e80941Smrg{ 366b8e80941Smrg ir->condition->accept(this); 367b8e80941Smrg handle_rvalue(&ir->condition); 368b8e80941Smrg 369b8e80941Smrg hash_table *new_kills = _mesa_pointer_hash_table_create(mem_ctx); 370b8e80941Smrg bool then_killed_all = false; 371b8e80941Smrg bool else_killed_all = false; 372b8e80941Smrg 373b8e80941Smrg handle_if_block(&ir->then_instructions, new_kills, &then_killed_all); 374b8e80941Smrg handle_if_block(&ir->else_instructions, new_kills, &else_killed_all); 375b8e80941Smrg 376b8e80941Smrg if (then_killed_all || else_killed_all) { 377b8e80941Smrg acp->make_empty(); 378b8e80941Smrg killed_all = true; 379b8e80941Smrg } else { 380b8e80941Smrg hash_table_foreach(new_kills, htk) 381b8e80941Smrg kill((ir_variable *) htk->key, (uintptr_t) htk->data); 382b8e80941Smrg } 383b8e80941Smrg 384b8e80941Smrg _mesa_hash_table_destroy(new_kills, NULL); 385b8e80941Smrg 386b8e80941Smrg /* handle_if_block() already descended into the children. */ 387b8e80941Smrg return visit_continue_with_parent; 388b8e80941Smrg} 389b8e80941Smrg 390b8e80941Smrgvoid 391b8e80941Smrgir_constant_propagation_visitor::handle_loop(ir_loop *ir, bool keep_acp) 392b8e80941Smrg{ 393b8e80941Smrg exec_list *orig_acp = this->acp; 394b8e80941Smrg hash_table *orig_kills = this->kills; 395b8e80941Smrg bool orig_killed_all = this->killed_all; 396b8e80941Smrg 397b8e80941Smrg this->acp = new(mem_ctx) exec_list; 398b8e80941Smrg this->kills = _mesa_pointer_hash_table_create(mem_ctx); 399b8e80941Smrg this->killed_all = false; 400b8e80941Smrg 401b8e80941Smrg if (keep_acp) { 402b8e80941Smrg foreach_in_list(acp_entry, a, orig_acp) { 403b8e80941Smrg this->acp->push_tail(new(this->lin_ctx) acp_entry(a)); 404b8e80941Smrg } 405b8e80941Smrg } 406b8e80941Smrg 407b8e80941Smrg visit_list_elements(this, &ir->body_instructions); 408b8e80941Smrg 409b8e80941Smrg if (this->killed_all) { 410b8e80941Smrg orig_acp->make_empty(); 411b8e80941Smrg } 412b8e80941Smrg 413b8e80941Smrg hash_table *new_kills = this->kills; 414b8e80941Smrg this->kills = orig_kills; 415b8e80941Smrg this->acp = orig_acp; 416b8e80941Smrg this->killed_all = this->killed_all || orig_killed_all; 417b8e80941Smrg 418b8e80941Smrg hash_table_foreach(new_kills, htk) { 419b8e80941Smrg kill((ir_variable *) htk->key, (uintptr_t) htk->data); 420b8e80941Smrg } 421b8e80941Smrg} 422b8e80941Smrg 423b8e80941Smrgir_visitor_status 424b8e80941Smrgir_constant_propagation_visitor::visit_enter(ir_loop *ir) 425b8e80941Smrg{ 426b8e80941Smrg /* Make a conservative first pass over the loop with an empty ACP set. 427b8e80941Smrg * This also removes any killed entries from the original ACP set. 428b8e80941Smrg */ 429b8e80941Smrg handle_loop(ir, false); 430b8e80941Smrg 431b8e80941Smrg /* Then, run it again with the real ACP set, minus any killed entries. 432b8e80941Smrg * This takes care of propagating values from before the loop into it. 433b8e80941Smrg */ 434b8e80941Smrg handle_loop(ir, true); 435b8e80941Smrg 436b8e80941Smrg /* already descended into the children. */ 437b8e80941Smrg return visit_continue_with_parent; 438b8e80941Smrg} 439b8e80941Smrg 440b8e80941Smrgvoid 441b8e80941Smrgir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask) 442b8e80941Smrg{ 443b8e80941Smrg assert(var != NULL); 444b8e80941Smrg 445b8e80941Smrg /* We don't track non-vectors. */ 446b8e80941Smrg if (!var->type->is_vector() && !var->type->is_scalar()) 447b8e80941Smrg return; 448b8e80941Smrg 449b8e80941Smrg /* Remove any entries currently in the ACP for this kill. */ 450b8e80941Smrg foreach_in_list_safe(acp_entry, entry, this->acp) { 451b8e80941Smrg if (entry->var == var) { 452b8e80941Smrg entry->write_mask &= ~write_mask; 453b8e80941Smrg if (entry->write_mask == 0) 454b8e80941Smrg entry->remove(); 455b8e80941Smrg } 456b8e80941Smrg } 457b8e80941Smrg 458b8e80941Smrg /* Add this writemask of the variable to the hash table of killed 459b8e80941Smrg * variables in this block. 460b8e80941Smrg */ 461b8e80941Smrg hash_entry *kill_hash_entry = _mesa_hash_table_search(this->kills, var); 462b8e80941Smrg if (kill_hash_entry) { 463b8e80941Smrg uintptr_t new_write_mask = ((uintptr_t) kill_hash_entry->data) | write_mask; 464b8e80941Smrg kill_hash_entry->data = (void *) new_write_mask; 465b8e80941Smrg return; 466b8e80941Smrg } 467b8e80941Smrg /* Not already in the hash table. Make new entry. */ 468b8e80941Smrg _mesa_hash_table_insert(this->kills, var, (void *) uintptr_t(write_mask)); 469b8e80941Smrg} 470b8e80941Smrg 471b8e80941Smrg/** 472b8e80941Smrg * Adds an entry to the available constant list if it's a plain assignment 473b8e80941Smrg * of a variable to a variable. 474b8e80941Smrg */ 475b8e80941Smrgvoid 476b8e80941Smrgir_constant_propagation_visitor::add_constant(ir_assignment *ir) 477b8e80941Smrg{ 478b8e80941Smrg acp_entry *entry; 479b8e80941Smrg 480b8e80941Smrg if (ir->condition) 481b8e80941Smrg return; 482b8e80941Smrg 483b8e80941Smrg if (!ir->write_mask) 484b8e80941Smrg return; 485b8e80941Smrg 486b8e80941Smrg ir_dereference_variable *deref = ir->lhs->as_dereference_variable(); 487b8e80941Smrg ir_constant *constant = ir->rhs->as_constant(); 488b8e80941Smrg 489b8e80941Smrg if (!deref || !constant) 490b8e80941Smrg return; 491b8e80941Smrg 492b8e80941Smrg /* Only do constant propagation on vectors. Constant matrices, 493b8e80941Smrg * arrays, or structures would require more work elsewhere. 494b8e80941Smrg */ 495b8e80941Smrg if (!deref->var->type->is_vector() && !deref->var->type->is_scalar()) 496b8e80941Smrg return; 497b8e80941Smrg 498b8e80941Smrg /* We can't do copy propagation on buffer variables, since the underlying 499b8e80941Smrg * memory storage is shared across multiple threads we can't be sure that 500b8e80941Smrg * the variable value isn't modified between this assignment and the next 501b8e80941Smrg * instruction where its value is read. 502b8e80941Smrg */ 503b8e80941Smrg if (deref->var->data.mode == ir_var_shader_storage || 504b8e80941Smrg deref->var->data.mode == ir_var_shader_shared) 505b8e80941Smrg return; 506b8e80941Smrg 507b8e80941Smrg entry = new(this->lin_ctx) acp_entry(deref->var, ir->write_mask, constant); 508b8e80941Smrg this->acp->push_tail(entry); 509b8e80941Smrg} 510b8e80941Smrg 511b8e80941Smrg} /* unnamed namespace */ 512b8e80941Smrg 513b8e80941Smrg/** 514b8e80941Smrg * Does a constant propagation pass on the code present in the instruction stream. 515b8e80941Smrg */ 516b8e80941Smrgbool 517b8e80941Smrgdo_constant_propagation(exec_list *instructions) 518b8e80941Smrg{ 519b8e80941Smrg ir_constant_propagation_visitor v; 520b8e80941Smrg 521b8e80941Smrg visit_list_elements(&v, instructions); 522b8e80941Smrg 523b8e80941Smrg return v.progress; 524b8e80941Smrg} 525