i965/fs: Optimize IF/MOV/ELSE/MOV/ENDIF to SEL when possible.
authorKenneth Graunke <kenneth@whitecape.org>
Sun, 4 Aug 2013 09:05:43 +0000 (02:05 -0700)
committerKenneth Graunke <kenneth@whitecape.org>
Mon, 12 Aug 2013 20:13:01 +0000 (13:13 -0700)
commit80e1c2f35f76562a6a62cfa630edaf97e6d6f227
tree7542b90826f24d9fd99be64104a74e62e0327b16
parent2c32c3985ca6232a81d21feb9ac6443145b42d0e
i965/fs: Optimize IF/MOV/ELSE/MOV/ENDIF to SEL when possible.

Many GLSL shaders contain code of the form:

   x = condition ? foo : bar

The compiler emits an ir_if tree for this, since each subexpression
might be a complex tree that could have side-effects and short-circuit
logic operations.

However, the common case is to simply pick one of two constants or
variable's values---which is exactly what SEL is for.  Replacing IF/ELSE
with SEL also simplifies the control flow graph, making optimization
passes which work on basic blocks more effective.

The shader-db statistics:

   total instructions in shared programs: 1655247 -> 1503234 (-9.18%)
   instructions in affected programs:     949188 -> 797175 (-16.02%)

   2,970 shaders were helped, none hurt.  Gained 181 SIMD16 programs.

This helps Valve's Source Engine games (max -41.33%), The Cave
(max -33.33%), Serious Sam 3 (max -18.64%), Yo Frankie! (max -30.19%),
Zen Bound (max -22.22%), GStreamer (max -6.12%), and GLBenchmark 2.7
(max -1.94%).

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
src/mesa/drivers/dri/i965/brw_fs.h
src/mesa/drivers/dri/i965/brw_fs_visitor.cpp