Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / opt_constant_propagation.cpp
index 18f5da689071b801809792bae0b2ab3c5142fa59..5221417aca0e61586bf06aaa2d2e65c28966502e 100644 (file)
@@ -110,6 +110,8 @@ public:
    virtual ir_visitor_status visit_enter(class ir_if *);
 
    void add_constant(ir_assignment *ir);
+   void constant_folding(ir_rvalue **rvalue);
+   void constant_propagation(ir_rvalue **rvalue);
    void kill(ir_variable *ir, unsigned write_mask);
    void handle_if_block(exec_list *instructions);
    void handle_rvalue(ir_rvalue **rvalue);
@@ -132,8 +134,38 @@ public:
 
 
 void
-ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
-{
+ir_constant_propagation_visitor::constant_folding(ir_rvalue **rvalue) {
+
+   if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
+      return;
+
+   /* Note that we visit rvalues one leaving.  So if an expression has a
+    * non-constant operand, no need to go looking down it to find if it's
+    * constant.  This cuts the time of this pass down drastically.
+    */
+   ir_expression *expr = (*rvalue)->as_expression();
+   if (expr) {
+      for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
+        if (!expr->operands[i]->as_constant())
+           return;
+      }
+   }
+
+   /* Ditto for swizzles. */
+   ir_swizzle *swiz = (*rvalue)->as_swizzle();
+   if (swiz && !swiz->val->as_constant())
+      return;
+
+   ir_constant *constant = (*rvalue)->constant_expression_value();
+   if (constant) {
+      *rvalue = constant;
+      this->progress = true;
+   }
+}
+
+void
+ir_constant_propagation_visitor::constant_propagation(ir_rvalue **rvalue) {
+
    if (this->in_assignee || !*rvalue)
       return;
 
@@ -172,8 +204,7 @@ ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
         channel = i;
       }
 
-      foreach_list(n, this->acp) {
-        acp_entry *entry = (acp_entry *) n;
+      foreach_in_list(acp_entry, entry, this->acp) {
         if (entry->var == deref->var && entry->write_mask & (1 << channel)) {
            found = entry;
            break;
@@ -195,6 +226,9 @@ ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
       case GLSL_TYPE_FLOAT:
         data.f[i] = found->constant->value.f[rhs_channel];
         break;
+      case GLSL_TYPE_DOUBLE:
+        data.d[i] = found->constant->value.d[rhs_channel];
+        break;
       case GLSL_TYPE_INT:
         data.i[i] = found->constant->value.i[rhs_channel];
         break;
@@ -214,6 +248,13 @@ ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
    this->progress = true;
 }
 
+void
+ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
+{
+   constant_propagation(rvalue);
+   constant_folding(rvalue);
+}
+
 ir_visitor_status
 ir_constant_propagation_visitor::visit_enter(ir_function_signature *ir)
 {
@@ -241,6 +282,8 @@ ir_constant_propagation_visitor::visit_enter(ir_function_signature *ir)
 ir_visitor_status
 ir_constant_propagation_visitor::visit_leave(ir_assignment *ir)
 {
+  constant_folding(&ir->rhs);
+
    if (this->in_assignee)
       return visit_continue;
 
@@ -317,8 +360,7 @@ ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
    this->killed_all = false;
 
    /* Populate the initial acp with a constant of the original */
-   foreach_list(n, orig_acp) {
-      acp_entry *a = (acp_entry *) n;
+   foreach_in_list(acp_entry, a, orig_acp) {
       this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
    }
 
@@ -333,8 +375,7 @@ ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
    this->acp = orig_acp;
    this->killed_all = this->killed_all || orig_killed_all;
 
-   foreach_list(n, new_kills) {
-      kill_entry *k = (kill_entry *) n;
+   foreach_in_list(kill_entry, k, new_kills) {
       kill(k->var, k->write_mask);
    }
 }
@@ -378,8 +419,7 @@ ir_constant_propagation_visitor::visit_enter(ir_loop *ir)
    this->acp = orig_acp;
    this->killed_all = this->killed_all || orig_killed_all;
 
-   foreach_list(n, new_kills) {
-      kill_entry *k = (kill_entry *) n;
+   foreach_in_list(kill_entry, k, new_kills) {
       kill(k->var, k->write_mask);
    }
 
@@ -397,9 +437,7 @@ ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
       return;
 
    /* Remove any entries currently in the ACP for this kill. */
-   foreach_list_safe(n, this->acp) {
-      acp_entry *entry = (acp_entry *) n;
-
+   foreach_in_list_safe(acp_entry, entry, this->acp) {
       if (entry->var == var) {
         entry->write_mask &= ~write_mask;
         if (entry->write_mask == 0)
@@ -410,9 +448,7 @@ ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
    /* Add this writemask of the variable to the list of killed
     * variables in this block.
     */
-   foreach_list(n, this->kills) {
-      kill_entry *entry = (kill_entry *) n;
-
+   foreach_in_list(kill_entry, entry, this->kills) {
       if (entry->var == var) {
         entry->write_mask |= write_mask;
         return;
@@ -449,6 +485,14 @@ ir_constant_propagation_visitor::add_constant(ir_assignment *ir)
    if (!deref->var->type->is_vector() && !deref->var->type->is_scalar())
       return;
 
+   /* We can't do copy propagation on buffer variables, since the underlying
+    * memory storage is shared across multiple threads we can't be sure that
+    * the variable value isn't modified between this assignment and the next
+    * instruction where its value is read.
+    */
+   if (deref->var->data.mode == ir_var_shader_storage)
+      return;
+
    entry = new(this->mem_ctx) acp_entry(deref->var, ir->write_mask, constant);
    this->acp->push_tail(entry);
 }