glsl: Ignore loop-too-large heuristic if there's bad variable indexing.
glsl: Ignore loop-too-large heuristic if there's bad variable indexing.

Many shaders use a pattern such as:

for (int i = 0; i < NUM_LIGHTS; i++) {
   ...access a uniform array, or shader input/output array...
}

where NUM_LIGHTS is a small constant (such as 2, 4, or 8).

The expectation is that the compiler will unroll those loops, turning
the array access into constant indexing, which is more efficient, and
which may enable array splitting and other optimizations.

In many cases, our heuristic fails - either there's another tiny nested
loop inside, or the estimated number of instructions is just barely
beyond the threshold.  So, we fail to unroll the loop, leaving the
variable indexing in place.

Drivers which don't support the particular flavor of variable indexing
will call lower_variable_index_to_cond_assign(), which generates piles
and piles of immensely inefficient code.  We'd like to avoid generating
that.

This patch detects unsupported forms of variable-indexing in loops, where
the array index is a loop induction variable.  In that case, it bypasses
the loop-too-large heuristic and forces unrolling.

Improves performance in various microbenchmarks: Gl32PSBump8 by 47%,
Gl32ShMapVsm by 80%, and Gl32ShMapPcf by 27%.  No changes in shader-db.

v2: Check ir->array for being an array or matrix, rather than the
    ir_dereference_array itself.
v3: Fix and expand statistics in commit message.

Signed-off-by: Kenneth Graunke <bd2562f754ec92342f93f61c25d731e290a2ffa8@whitecape.org>
Reviewed-by: Eric Anholt <96f164ad4d9b2b0dacf8ebee2bb1eeb3aa69adf1@anholt.net>

djreep81/glsl-optimizer,dellis1972/glsl-optimizer,benaadams/glsl-optimizer,wolf96/glsl-optimizer,benaadams/glsl-optimizer,tokyovigilante/glsl-optimizer,zz85/glsl-optimizer,djreep81/glsl-optimizer,zz85/glsl-optimizer,mcanthony/glsl-optimizer,metora/MesaGLSLCompiler,jbarczak/glsl-optimizer,bkaradzic/glsl-optimizer,zeux/glsl-optimizer,jbarczak/glsl-optimizer,mcanthony/glsl-optimizer,mcanthony/glsl-optimizer,tokyovigilante/glsl-optimizer,zeux/glsl-optimizer,wolf96/glsl-optimizer,zeux/glsl-optimizer,zeux/glsl-optimizer,djreep81/glsl-optimizer,benaadams/glsl-optimizer,zz85/glsl-optimizer,benaadams/glsl-optimizer,bkaradzic/glsl-optimizer,jbarczak/glsl-optimizer,bkaradzic/glsl-optimizer,wolf96/glsl-optimizer,dellis1972/glsl-optimizer,mcanthony/glsl-optimizer,jbarczak/glsl-optimizer,djreep81/glsl-optimizer,benaadams/glsl-optimizer,wolf96/glsl-optimizer,mcanthony/glsl-optimizer,dellis1972/glsl-optimizer,bkaradzic/glsl-optimizer,zz85/glsl-optimizer,wolf96/glsl-optimizer,dellis1972/glsl-optimizer,tokyovigilante/glsl-optimizer,zz85/glsl-optimizer,djreep81/glsl-optimizer,tokyovigilante/glsl-optimizer,dellis1972/glsl-optimizer,bkaradzic/glsl-optimizer,zz85/glsl-optimizer,metora/MesaGLSLCompiler,benaadams/glsl-optimizer,zeux/glsl-optimizer,metora/MesaGLSLCompiler,tokyovigilante/glsl-optimizer,jbarczak/glsl-optimizer