From: Ian Romanick <ian.d.roman...@intel.com> For example (x * 43) and (x * -43) should be considered common.
v2: Fix a problem where the x in -x would get replaced with -x producing -(-x). Oops. Shader-db results: GM45 (0x2A42): total instructions in shared programs: 3544158 -> 3544133 (-0.00%) instructions in affected programs: 2151 -> 2126 (-1.16%) helped: 19 Iron Lake (0x0046): total instructions in shared programs: 4973887 -> 4973849 (-0.00%) instructions in affected programs: 3020 -> 2982 (-1.26%) helped: 26 Sandy Bridge (0x0116): total instructions in shared programs: 6799717 -> 6799682 (-0.00%) instructions in affected programs: 2658 -> 2623 (-1.32%) helped: 22 HURT: 6 Sandy Bridge (0x0116) NIR: total instructions in shared programs: 6787220 -> 6787227 (0.00%) instructions in affected programs: 1670 -> 1677 (0.42%) helped: 11 HURT: 12 Ivy Bridge (0x0166): total instructions in shared programs: 6274097 -> 6274041 (-0.00%) instructions in affected programs: 3227 -> 3171 (-1.74%) helped: 25 HURT: 5 Ivy Bridge (0x0166) NIR: total instructions in shared programs: 6298649 -> 6298635 (-0.00%) instructions in affected programs: 2302 -> 2288 (-0.61%) helped: 14 HURT: 11 Haswell (0x0426): total instructions in shared programs: 5760202 -> 5760189 (-0.00%) instructions in affected programs: 2070 -> 2057 (-0.63%) helped: 13 HURT: 10 Haswell (0x0426) NIR: total instructions in shared programs: 5767149 -> 5767160 (0.00%) instructions in affected programs: 3124 -> 3135 (0.35%) helped: 13 HURT: 22 Broadwell (0x162E): total instructions in shared programs: 6808264 -> 6808224 (-0.00%) instructions in affected programs: 2931 -> 2891 (-1.36%) helped: 32 HURT: 10 Broadwell (0x162E) NIR: total instructions in shared programs: 6986287 -> 6986297 (0.00%) instructions in affected programs: 2861 -> 2871 (0.35%) helped: 8 HURT: 24 Signed-off-by: Ian Romanick <ian.d.roman...@intel.com> Cc: Jason Ekstrand <jason.ekstr...@intel.com> --- src/glsl/opt_cse.cpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/glsl/opt_cse.cpp b/src/glsl/opt_cse.cpp index b0b67f4..41053fb 100644 --- a/src/glsl/opt_cse.cpp +++ b/src/glsl/opt_cse.cpp @@ -266,9 +266,28 @@ cse_visitor::try_cse(ir_rvalue *rvalue) printf("\n"); } - if (!rvalue->equals(*entry->val)) + const bool same = rvalue->equals(*entry->val); + const bool negative_same = rvalue->negative_equals(*entry->val); + + if (!same && !negative_same) continue; + if (negative_same) { + /* There are two special cases to catch or an infinite optimization + * loop can occur. Either entry->val is the negation of rvalue + * (i.e., rvalue is an node in the entry->val tree) or vice-versa. + */ + ir_expression *const val_expr = (*entry->val)->as_expression(); + if (val_expr && val_expr->operation == ir_unop_neg + && val_expr->operands[0] == rvalue) + continue; + + ir_expression *const rv_expr = rvalue->as_expression(); + if (rv_expr && rv_expr->operation == ir_unop_neg + && rv_expr->operands[0] == *entry->val) + continue; + } + if (debug) { printf("CSE: Replacing: "); (*entry->val)->print(); @@ -316,7 +335,16 @@ cse_visitor::try_cse(ir_rvalue *rvalue) } /* Replace the expression in our current tree with the variable. */ - return new(rvalue) ir_dereference_variable(entry->var); + if (same) + return new(rvalue) ir_dereference_variable(entry->var); + else { + assert(negative_same); + + ir_rvalue *const deref = + new(rvalue) ir_dereference_variable(entry->var); + + return new(rvalue) ir_expression(ir_unop_neg, deref); + } } return NULL; -- 2.1.0 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev