2015-02-03 06:49:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2015 Connor Abbott
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Connor Abbott (cwabbott0@gmail.com)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nir.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a pass for removing phi nodes that look like:
|
|
|
|
* a = phi(b, b, b, ...)
|
|
|
|
*
|
|
|
|
* Note that we can't ignore undef sources here, or else we may create a
|
|
|
|
* situation where the definition of b isn't dominated by its uses. We're
|
|
|
|
* allowed to do this since the definition of b must dominate all of the
|
|
|
|
* phi node's predecessors, which means it must dominate the phi node as well
|
|
|
|
* as all of the phi node's uses. In essence, the phi node acts as a copy
|
|
|
|
* instruction. b can't be another phi node in the same block, since the only
|
|
|
|
* time when phi nodes can source other phi nodes defined in the same block is
|
|
|
|
* at the loop header, and in that case one of the sources of the phi has to
|
|
|
|
* be from before the loop and that source can't be b.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2016-04-12 19:49:09 +01:00
|
|
|
remove_phis_block(nir_block *block)
|
2015-02-03 06:49:44 +00:00
|
|
|
{
|
2016-04-12 19:49:09 +01:00
|
|
|
bool progress = false;
|
2015-02-03 06:49:44 +00:00
|
|
|
|
|
|
|
nir_foreach_instr_safe(block, instr) {
|
|
|
|
if (instr->type != nir_instr_type_phi)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nir_phi_instr *phi = nir_instr_as_phi(instr);
|
|
|
|
|
|
|
|
nir_ssa_def *def = NULL;
|
|
|
|
bool srcs_same = true;
|
|
|
|
|
|
|
|
nir_foreach_phi_src(phi, src) {
|
|
|
|
assert(src->src.is_ssa);
|
nir/remove_phis: handle trivial back-edges
Some loops may have phi nodes that look like:
foo = ...
loop {
bar = phi(foo, bar)
...
}
in which case we can remove the phi node and replace all uses of 'bar'
with 'foo'. In particular, there are some L4D2 vertex shaders with loops
that, after optimization, look like:
/* succs: block_1 */
loop {
block block_1:
/* preds: block_0 block_4 */
vec1 ssa_2195 = phi block_0: ssa_2136, block_4: ssa_994
vec1 ssa_7321 = phi block_0: ssa_8195, block_4: ssa_7321
vec1 ssa_7324 = phi block_0: ssa_8198, block_4: ssa_7324
vec1 ssa_7327 = phi block_0: ssa_8174, block_4: ssa_7327
vec1 ssa_8139 = intrinsic load_uniform () () (232)
vec1 ssa_588 = ige ssa_2195, ssa_8139
/* succs: block_2 block_3 */
if ssa_588 {
block block_2:
/* preds: block_1 */
break
/* succs: block_5 */
} else {
block block_3:
/* preds: block_1 */
/* succs: block_4 */
}
block block_4:
/* preds: block_3 */
vec1 ssa_994 = iadd ssa_2195, ssa_2150
/* succs: block_1 */
}
where after removing the second, third, and fourth phi nodes, the loop becomes
entirely dead, and this patch will cause the loop to be deleted entirely.
No piglit regressions.
Shader-db results on bdw:
instructions in affected programs: 5824 -> 5664 (-2.75%)
total loops in shared programs: 2234 -> 2202 (-1.43%)
helped: 32
Reviewed-by: Matt Turner <mattst88@gmail.com>
Signed-off-by: Connor Abbott <cwabbott0@gmail.com>
2015-05-22 00:26:01 +01:00
|
|
|
|
|
|
|
/* For phi nodes at the beginning of loops, we may encounter some
|
|
|
|
* sources from backedges that point back to the destination of the
|
|
|
|
* same phi, i.e. something like:
|
|
|
|
*
|
|
|
|
* a = phi(a, b, ...)
|
|
|
|
*
|
|
|
|
* We can safely ignore these sources, since if all of the normal
|
|
|
|
* sources point to the same definition, then that definition must
|
|
|
|
* still dominate the phi node, and the phi will still always take
|
|
|
|
* the value of that definition.
|
|
|
|
*/
|
|
|
|
if (src->src.ssa == &phi->dest.ssa)
|
|
|
|
continue;
|
2015-02-03 06:49:44 +00:00
|
|
|
|
|
|
|
if (def == NULL) {
|
|
|
|
def = src->src.ssa;
|
|
|
|
} else {
|
|
|
|
if (src->src.ssa != def) {
|
|
|
|
srcs_same = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!srcs_same)
|
|
|
|
continue;
|
|
|
|
|
nir/remove_phis: handle trivial back-edges
Some loops may have phi nodes that look like:
foo = ...
loop {
bar = phi(foo, bar)
...
}
in which case we can remove the phi node and replace all uses of 'bar'
with 'foo'. In particular, there are some L4D2 vertex shaders with loops
that, after optimization, look like:
/* succs: block_1 */
loop {
block block_1:
/* preds: block_0 block_4 */
vec1 ssa_2195 = phi block_0: ssa_2136, block_4: ssa_994
vec1 ssa_7321 = phi block_0: ssa_8195, block_4: ssa_7321
vec1 ssa_7324 = phi block_0: ssa_8198, block_4: ssa_7324
vec1 ssa_7327 = phi block_0: ssa_8174, block_4: ssa_7327
vec1 ssa_8139 = intrinsic load_uniform () () (232)
vec1 ssa_588 = ige ssa_2195, ssa_8139
/* succs: block_2 block_3 */
if ssa_588 {
block block_2:
/* preds: block_1 */
break
/* succs: block_5 */
} else {
block block_3:
/* preds: block_1 */
/* succs: block_4 */
}
block block_4:
/* preds: block_3 */
vec1 ssa_994 = iadd ssa_2195, ssa_2150
/* succs: block_1 */
}
where after removing the second, third, and fourth phi nodes, the loop becomes
entirely dead, and this patch will cause the loop to be deleted entirely.
No piglit regressions.
Shader-db results on bdw:
instructions in affected programs: 5824 -> 5664 (-2.75%)
total loops in shared programs: 2234 -> 2202 (-1.43%)
helped: 32
Reviewed-by: Matt Turner <mattst88@gmail.com>
Signed-off-by: Connor Abbott <cwabbott0@gmail.com>
2015-05-22 00:26:01 +01:00
|
|
|
/* We must have found at least one definition, since there must be at
|
|
|
|
* least one forward edge.
|
|
|
|
*/
|
|
|
|
assert(def != NULL);
|
|
|
|
|
2015-02-03 06:49:44 +00:00
|
|
|
assert(phi->dest.is_ssa);
|
2015-09-09 21:24:35 +01:00
|
|
|
nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_src_for_ssa(def));
|
2015-02-03 06:49:44 +00:00
|
|
|
nir_instr_remove(instr);
|
|
|
|
|
2016-04-12 19:49:09 +01:00
|
|
|
progress = true;
|
2015-02-03 06:49:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:49:09 +01:00
|
|
|
return progress;
|
2015-02-03 06:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
remove_phis_impl(nir_function_impl *impl)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
|
2016-04-12 19:49:09 +01:00
|
|
|
nir_foreach_block(block, impl) {
|
|
|
|
progress |= remove_phis_block(block);
|
|
|
|
}
|
2015-02-03 06:49:44 +00:00
|
|
|
|
2015-11-03 05:43:40 +00:00
|
|
|
if (progress) {
|
|
|
|
nir_metadata_preserve(impl, nir_metadata_block_index |
|
|
|
|
nir_metadata_dominance);
|
|
|
|
}
|
|
|
|
|
2015-02-03 06:49:44 +00:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nir_opt_remove_phis(nir_shader *shader)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
|
2015-12-26 18:00:47 +00:00
|
|
|
nir_foreach_function(shader, function)
|
|
|
|
if (function->impl)
|
|
|
|
progress = remove_phis_impl(function->impl) || progress;
|
2015-02-03 06:49:44 +00:00
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|