From c7b1046a9fa6da916f11fb9e43d61fd772470183 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Wed, 19 May 2010 13:20:12 +0200 Subject: [PATCH] Refactor ir_dereference support for ir_visitor Move the accept method for visitors from ir_dereference to the derived classes. --- ir.h | 20 +++++++++--- ir_constant_expression.cpp | 30 ++++++++++++----- ir_constant_folding.cpp | 37 +++++++++++++++------ ir_copy_propagation.cpp | 42 +++++++++++++----------- ir_expression_flattening.cpp | 22 ++++++++++--- ir_function_inlining.cpp | 62 ++++++++++++++++++++++------------- ir_print_visitor.cpp | 63 ++++++++++++++++-------------------- ir_print_visitor.h | 4 ++- ir_visitor.h | 4 ++- 9 files changed, 178 insertions(+), 106 deletions(-) diff --git a/ir.h b/ir.h index f785ddb6373..2d641766b2e 100644 --- a/ir.h +++ b/ir.h @@ -793,11 +793,6 @@ public: return this; } - virtual void accept(ir_visitor *v) - { - v->visit(this); - } - bool is_lvalue(); /** @@ -844,6 +839,11 @@ public: return (ir_variable *) this->var; } + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + virtual ir_visitor_status accept(ir_hierarchical_visitor *); }; @@ -862,6 +862,11 @@ public: return ((ir_rvalue *) this->var)->variable_referenced(); } + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + virtual ir_visitor_status accept(ir_hierarchical_visitor *); private: @@ -883,6 +888,11 @@ public: return ((ir_rvalue *) this->var)->variable_referenced(); } + virtual void accept(ir_visitor *v) + { + v->visit(this); + } + virtual ir_visitor_status accept(ir_hierarchical_visitor *); }; diff --git a/ir_constant_expression.cpp b/ir_constant_expression.cpp index e5626c53117..076fdfda75f 100644 --- a/ir_constant_expression.cpp +++ b/ir_constant_expression.cpp @@ -67,7 +67,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); @@ -412,16 +414,28 @@ ir_constant_visitor::visit(ir_swizzle *ir) void -ir_constant_visitor::visit(ir_dereference *ir) +ir_constant_visitor::visit(ir_dereference_variable *ir) { value = NULL; - if (ir->mode == ir_dereference::ir_reference_variable) { - ir_variable *var = ir->var->as_variable(); - if (var && var->constant_value) { - value = new ir_constant(ir->type, &var->constant_value->value); - } - } + ir_variable *var = ir->variable_referenced(); + if (var && var->constant_value) + value = new ir_constant(ir->type, &var->constant_value->value); +} + + +void +ir_constant_visitor::visit(ir_dereference_array *ir) +{ + value = NULL; + /* FINISHME: Other dereference modes. */ +} + + +void +ir_constant_visitor::visit(ir_dereference_record *ir) +{ + value = NULL; /* FINISHME: Other dereference modes. */ } diff --git a/ir_constant_folding.cpp b/ir_constant_folding.cpp index 913d42f0d9a..9a2a7f3f2e7 100644 --- a/ir_constant_folding.cpp +++ b/ir_constant_folding.cpp @@ -61,7 +61,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); @@ -117,15 +119,30 @@ ir_constant_folding_visitor::visit(ir_swizzle *ir) void -ir_constant_folding_visitor::visit(ir_dereference *ir) -{ - if (ir->mode == ir_dereference::ir_reference_array) { - ir_constant *const_val = ir->selector.array_index->constant_expression_value(); - if (const_val) - ir->selector.array_index = const_val; - else - ir->selector.array_index->accept(this); - } +ir_constant_folding_visitor::visit(ir_dereference_variable *ir) +{ + (void) ir; +} + + +void +ir_constant_folding_visitor::visit(ir_dereference_array *ir) +{ + ir_constant *const_val = + ir->selector.array_index->constant_expression_value(); + + if (const_val) + ir->selector.array_index = const_val; + else + ir->selector.array_index->accept(this); + + ir->var->accept(this); +} + + +void +ir_constant_folding_visitor::visit(ir_dereference_record *ir) +{ ir->var->accept(this); } diff --git a/ir_copy_propagation.cpp b/ir_copy_propagation.cpp index aac12b3da38..47f9390710c 100644 --- a/ir_copy_propagation.cpp +++ b/ir_copy_propagation.cpp @@ -78,7 +78,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); @@ -149,30 +151,34 @@ ir_copy_propagation_visitor::visit(ir_swizzle *ir) * must not be shared by multiple IR operations! */ void -ir_copy_propagation_visitor::visit(ir_dereference *ir) +ir_copy_propagation_visitor::visit(ir_dereference_variable *ir) { - ir_variable *var; + ir_variable *var = ir->variable_referenced(); - if (ir->mode == ir_dereference::ir_reference_array) { - ir->selector.array_index->accept(this); - } - - var = ir->var->as_variable(); - if (var) { - foreach_iter(exec_list_iterator, iter, *this->acp) { - acp_entry *entry = (acp_entry *)iter.get(); + foreach_iter(exec_list_iterator, iter, *this->acp) { + acp_entry *entry = (acp_entry *)iter.get(); - if (var == entry->lhs) { - ir->var = entry->rhs; - this->progress = true; - break; - } + if (var == entry->lhs) { + ir->var = entry->rhs; + this->progress = true; + break; } - } else { - ir->var->accept(this); } } +void +ir_copy_propagation_visitor::visit(ir_dereference_array *ir) +{ + ir->var->accept(this); + ir->selector.array_index->accept(this); +} + +void +ir_copy_propagation_visitor::visit(ir_dereference_record *ir) +{ + ir->var->accept(this); +} + void ir_copy_propagation_visitor::visit(ir_assignment *ir) { diff --git a/ir_expression_flattening.cpp b/ir_expression_flattening.cpp index 9494786d4e4..1e0244988a2 100644 --- a/ir_expression_flattening.cpp +++ b/ir_expression_flattening.cpp @@ -70,7 +70,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); @@ -162,11 +164,21 @@ ir_expression_flattening_visitor::visit(ir_swizzle *ir) void -ir_expression_flattening_visitor::visit(ir_dereference *ir) +ir_expression_flattening_visitor::visit(ir_dereference_variable *ir) +{ + ir->var->accept(this); +} + +void +ir_expression_flattening_visitor::visit(ir_dereference_array *ir) +{ + ir->selector.array_index->accept(this); + ir->var->accept(this); +} + +void +ir_expression_flattening_visitor::visit(ir_dereference_record *ir) { - if (ir->mode == ir_dereference::ir_reference_array) { - ir->selector.array_index->accept(this); - } ir->var->accept(this); } diff --git a/ir_function_inlining.cpp b/ir_function_inlining.cpp index 499ce5f1dc2..38572a2057a 100644 --- a/ir_function_inlining.cpp +++ b/ir_function_inlining.cpp @@ -61,7 +61,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); @@ -134,7 +136,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); @@ -218,30 +222,34 @@ ir_function_cloning_visitor::visit(ir_swizzle *ir) } void -ir_function_cloning_visitor::visit(ir_dereference *ir) +ir_function_cloning_visitor::visit(ir_dereference_variable *ir) { - if (ir->mode == ir_dereference::ir_reference_variable) { - ir_variable *var = this->get_remapped_variable(ir->variable_referenced()); - this->result = new ir_dereference_variable(var); - } else if (ir->mode == ir_dereference::ir_reference_array) { - ir->var->accept(this); + ir_variable *var = this->get_remapped_variable(ir->variable_referenced()); + this->result = new ir_dereference_variable(var); +} - ir_rvalue *var = this->result->as_rvalue(); +void +ir_function_cloning_visitor::visit(ir_dereference_array *ir) +{ + ir->var->accept(this); - ir->selector.array_index->accept(this); + ir_rvalue *var = this->result->as_rvalue(); - ir_rvalue *index = this->result->as_rvalue(); + ir->selector.array_index->accept(this); - this->result = new ir_dereference_array(var, index); - } else { - assert(ir->mode == ir_dereference::ir_reference_record); + ir_rvalue *index = this->result->as_rvalue(); - ir->var->accept(this); + this->result = new ir_dereference_array(var, index); +} - ir_rvalue *var = this->result->as_rvalue(); +void +ir_function_cloning_visitor::visit(ir_dereference_record *ir) +{ + ir->var->accept(this); - this->result = new ir_dereference_record(var, strdup(ir->selector.field)); - } + ir_rvalue *var = this->result->as_rvalue(); + + this->result = new ir_dereference_record(var, strdup(ir->selector.field)); } void @@ -509,11 +517,21 @@ ir_function_inlining_visitor::visit(ir_swizzle *ir) void -ir_function_inlining_visitor::visit(ir_dereference *ir) +ir_function_inlining_visitor::visit(ir_dereference_variable *ir) +{ + ir->var->accept(this); +} + +void +ir_function_inlining_visitor::visit(ir_dereference_array *ir) +{ + ir->selector.array_index->accept(this); + ir->var->accept(this); +} + +void +ir_function_inlining_visitor::visit(ir_dereference_record *ir) { - if (ir->mode == ir_dereference::ir_reference_array) { - ir->selector.array_index->accept(this); - } ir->var->accept(this); } diff --git a/ir_print_visitor.cpp b/ir_print_visitor.cpp index ee7aa311758..7cb5eeba015 100644 --- a/ir_print_visitor.cpp +++ b/ir_print_visitor.cpp @@ -74,22 +74,18 @@ print_type(const glsl_type *t) void ir_print_visitor::visit(ir_variable *ir) { - if (deref_depth) { - printf("%s", ir->name); - } else { - printf("(declare "); + printf("(declare "); - const char *const cent = (ir->centroid) ? "centroid " : ""; - const char *const inv = (ir->invariant) ? "invariant " : ""; - const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " }; - const char *const interp[] = { "", "flat", "noperspective" }; + const char *const cent = (ir->centroid) ? "centroid " : ""; + const char *const inv = (ir->invariant) ? "invariant " : ""; + const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " }; + const char *const interp[] = { "", "flat", "noperspective" }; - printf("(%s%s%s%s) ", - cent, inv, mode[ir->mode], interp[ir->interpolation]); + printf("(%s%s%s%s) ", + cent, inv, mode[ir->mode], interp[ir->interpolation]); - print_type(ir->type); - printf(" %s)", ir->name); - } + print_type(ir->type); + printf(" %s)", ir->name); } @@ -166,31 +162,26 @@ void ir_print_visitor::visit(ir_swizzle *ir) } -void ir_print_visitor::visit(ir_dereference *ir) +void ir_print_visitor::visit(ir_dereference_variable *ir) { - deref_depth++; - - switch (ir->mode) { - case ir_dereference::ir_reference_variable: { - printf("(var_ref "); - ir->var->accept(this); - printf(") "); - break; - } - case ir_dereference::ir_reference_array: - printf("(array_ref "); - ir->var->accept(this); - ir->selector.array_index->accept(this); - printf(") "); - break; - case ir_dereference::ir_reference_record: - printf("(record_ref "); - ir->var->accept(this); - printf("(%s)) ", ir->selector.field); - break; - } + printf("(var_ref %s) ", ir->variable_referenced()->name); +} + - deref_depth--; +void ir_print_visitor::visit(ir_dereference_array *ir) +{ + printf("(array_ref "); + ir->var->accept(this); + ir->selector.array_index->accept(this); + printf(") "); +} + + +void ir_print_visitor::visit(ir_dereference_record *ir) +{ + printf("(record_ref "); + ir->var->accept(this); + printf("(%s)) ", ir->selector.field); } diff --git a/ir_print_visitor.h b/ir_print_visitor.h index aeee538df29..4af508794bd 100644 --- a/ir_print_visitor.h +++ b/ir_print_visitor.h @@ -61,7 +61,9 @@ public: virtual void visit(ir_function *); virtual void visit(ir_expression *); virtual void visit(ir_swizzle *); - virtual void visit(ir_dereference *); + virtual void visit(ir_dereference_variable *); + virtual void visit(ir_dereference_array *); + virtual void visit(ir_dereference_record *); virtual void visit(ir_assignment *); virtual void visit(ir_constant *); virtual void visit(ir_call *); diff --git a/ir_visitor.h b/ir_visitor.h index 579eee701d7..ba30858fe62 100644 --- a/ir_visitor.h +++ b/ir_visitor.h @@ -49,7 +49,9 @@ public: virtual void visit(class ir_function *) = 0; virtual void visit(class ir_expression *) = 0; virtual void visit(class ir_swizzle *) = 0; - virtual void visit(class ir_dereference *) = 0; + virtual void visit(class ir_dereference_variable *) = 0; + virtual void visit(class ir_dereference_array *) = 0; + virtual void visit(class ir_dereference_record *) = 0; virtual void visit(class ir_assignment *) = 0; virtual void visit(class ir_constant *) = 0; virtual void visit(class ir_call *) = 0; -- 2.30.2