2015-05-01 07:38:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 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"
|
|
|
|
#include "nir_control_flow.h"
|
|
|
|
|
|
|
|
/*
|
2015-05-08 19:42:14 +01:00
|
|
|
* This file implements an optimization that deletes statically
|
Remove wrongly repeated words in comments
Clean up misrepetitions ('if if', 'the the' etc) found throughout the
comments. This has been done manually, after grepping
case-insensitively for duplicate if, is, the, then, do, for, an,
plus a few other typos corrected in fly-by
v2:
* proper commit message and non-joke title;
* replace two 'as is' followed by 'is' to 'as-is'.
v3:
* 'a integer' => 'an integer' and similar (originally spotted by
Jason Ekstrand, I fixed a few other similar ones while at it)
Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
2016-06-23 18:20:18 +01:00
|
|
|
* unreachable/dead code. In NIR, one way this can happen is when an if
|
2015-05-08 19:42:14 +01:00
|
|
|
* statement has a constant condition:
|
2015-05-01 07:38:17 +01:00
|
|
|
*
|
|
|
|
* if (true) {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* We delete the if statement and paste the contents of the always-executed
|
|
|
|
* branch into the surrounding control flow, possibly removing more code if
|
|
|
|
* the branch had a jump at the end.
|
2015-05-08 06:44:24 +01:00
|
|
|
*
|
2015-05-08 19:42:14 +01:00
|
|
|
* Another way is that control flow can end in a jump so that code after it
|
2015-05-08 06:44:24 +01:00
|
|
|
* never gets executed. In particular, this can happen after optimizing
|
|
|
|
* something like:
|
|
|
|
*
|
|
|
|
* if (true) {
|
|
|
|
* ...
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* We also consider the case where both branches of an if end in a jump, e.g.:
|
|
|
|
*
|
|
|
|
* if (...) {
|
|
|
|
* break;
|
|
|
|
* } else {
|
|
|
|
* continue;
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
*
|
2018-03-19 23:34:17 +00:00
|
|
|
* Finally, we also handle removing useless loops and ifs, i.e. loops and ifs
|
|
|
|
* with no side effects and without any definitions that are used
|
|
|
|
* elsewhere. This case is a little different from the first two in that the
|
|
|
|
* code is actually run (it just never does anything), but there are similar
|
|
|
|
* issues with needing to be careful with restarting after deleting the
|
|
|
|
* cf_node (see dead_cf_list()) so this is a convenient place to remove them.
|
2015-05-01 07:38:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_after_cf_node(nir_cf_node *node)
|
|
|
|
{
|
|
|
|
nir_cf_node *end = node;
|
|
|
|
while (!nir_cf_node_is_last(end))
|
|
|
|
end = nir_cf_node_next(end);
|
|
|
|
|
|
|
|
nir_cf_list list;
|
|
|
|
nir_cf_extract(&list, nir_after_cf_node(node), nir_after_cf_node(end));
|
|
|
|
nir_cf_delete(&list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opt_constant_if(nir_if *if_stmt, bool condition)
|
|
|
|
{
|
|
|
|
/* First, we need to remove any phi nodes after the if by rewriting uses to
|
|
|
|
* point to the correct source.
|
|
|
|
*/
|
|
|
|
nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node));
|
2016-10-06 03:08:57 +01:00
|
|
|
nir_block *last_block = condition ? nir_if_last_then_block(if_stmt)
|
|
|
|
: nir_if_last_else_block(if_stmt);
|
2015-05-01 07:38:17 +01:00
|
|
|
|
2016-04-27 02:34:19 +01:00
|
|
|
nir_foreach_instr_safe(instr, after) {
|
2015-05-01 07:38:17 +01:00
|
|
|
if (instr->type != nir_instr_type_phi)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nir_phi_instr *phi = nir_instr_as_phi(instr);
|
|
|
|
nir_ssa_def *def = NULL;
|
2016-04-27 04:16:21 +01:00
|
|
|
nir_foreach_phi_src(phi_src, phi) {
|
2015-05-01 07:38:17 +01:00
|
|
|
if (phi_src->pred != last_block)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
assert(phi_src->src.is_ssa);
|
|
|
|
def = phi_src->src.ssa;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(def);
|
|
|
|
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-05-01 07:38:17 +01:00
|
|
|
nir_instr_remove(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The control flow list we're about to paste in may include a jump at the
|
|
|
|
* end, and in that case we have to delete the rest of the control flow
|
|
|
|
* list after the if since it's unreachable and the validator will balk if
|
|
|
|
* we don't.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!exec_list_is_empty(&last_block->instr_list)) {
|
|
|
|
nir_instr *last_instr = nir_block_last_instr(last_block);
|
|
|
|
if (last_instr->type == nir_instr_type_jump)
|
|
|
|
remove_after_cf_node(&if_stmt->cf_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, actually paste in the then or else branch and delete the if. */
|
|
|
|
struct exec_list *cf_list = condition ? &if_stmt->then_list
|
|
|
|
: &if_stmt->else_list;
|
|
|
|
|
|
|
|
nir_cf_list list;
|
2016-08-05 02:08:20 +01:00
|
|
|
nir_cf_list_extract(&list, cf_list);
|
2015-05-01 07:38:17 +01:00
|
|
|
nir_cf_reinsert(&list, nir_after_cf_node(&if_stmt->cf_node));
|
|
|
|
nir_cf_node_remove(&if_stmt->cf_node);
|
|
|
|
}
|
|
|
|
|
2015-05-08 19:42:14 +01:00
|
|
|
static bool
|
2019-02-14 03:42:39 +00:00
|
|
|
def_only_used_in_cf_node(nir_ssa_def *def, void *_node)
|
2015-05-08 19:42:14 +01:00
|
|
|
{
|
2019-02-14 03:42:39 +00:00
|
|
|
nir_cf_node *node = _node;
|
|
|
|
assert(node->type == nir_cf_node_loop || node->type == nir_cf_node_if);
|
|
|
|
|
|
|
|
nir_block *before = nir_cf_node_as_block(nir_cf_node_prev(node));
|
|
|
|
nir_block *after = nir_cf_node_as_block(nir_cf_node_next(node));
|
|
|
|
|
|
|
|
nir_foreach_use(use, def) {
|
|
|
|
/* Because NIR is structured, we can easily determine whether or not a
|
|
|
|
* value escapes a CF node by looking at the block indices of its uses
|
|
|
|
* to see if they lie outside the bounds of the CF node.
|
|
|
|
*
|
|
|
|
* Note: Normally, the uses of a phi instruction are considered to be
|
|
|
|
* used in the block that is the predecessor of the phi corresponding to
|
|
|
|
* that use. If we were computing liveness or something similar, that
|
|
|
|
* would mean a special case here for phis. However, we're trying here
|
|
|
|
* to determine if the SSA def ever escapes the loop. If it's used by a
|
|
|
|
* phi that lives outside the loop then it doesn't matter if the
|
|
|
|
* corresponding predecessor is inside the loop or not because the value
|
|
|
|
* can go through the phi into the outside world and escape the loop.
|
|
|
|
*/
|
|
|
|
if (use->parent_instr->block->index <= before->index ||
|
|
|
|
use->parent_instr->block->index >= after->index)
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-08 19:42:14 +01:00
|
|
|
|
2019-02-20 07:17:28 +00:00
|
|
|
/* Same check for if-condition uses */
|
|
|
|
nir_foreach_if_use(use, def) {
|
|
|
|
nir_block *use_block =
|
|
|
|
nir_cf_node_as_block(nir_cf_node_prev(&use->parent_if->cf_node));
|
|
|
|
|
|
|
|
if (use_block->index <= before->index ||
|
|
|
|
use_block->index >= after->index)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-14 03:42:39 +00:00
|
|
|
return true;
|
2015-05-08 19:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-14 01:27:25 +00:00
|
|
|
* Test if a loop node is dead. Such nodes are dead if:
|
2015-05-08 19:42:14 +01:00
|
|
|
*
|
|
|
|
* 1) It has no side effects (i.e. intrinsics which could possibly affect the
|
|
|
|
* state of the program aside from producing an SSA value, indicated by a lack
|
|
|
|
* of NIR_INTRINSIC_CAN_ELIMINATE).
|
|
|
|
*
|
2018-03-19 23:34:16 +00:00
|
|
|
* 2) It has no phi instructions after it, since those indicate values inside
|
|
|
|
* the node being used after the node.
|
2015-05-08 19:42:14 +01:00
|
|
|
*
|
2018-03-19 23:34:16 +00:00
|
|
|
* 3) None of the values defined inside the node is used outside the node,
|
|
|
|
* i.e. none of the definitions that dominate the node exit are used outside.
|
|
|
|
*
|
|
|
|
* If those conditions hold, then the node is dead and can be deleted.
|
2015-05-08 19:42:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2018-03-19 23:34:17 +00:00
|
|
|
node_is_dead(nir_cf_node *node)
|
2015-05-08 19:42:14 +01:00
|
|
|
{
|
2019-02-14 01:27:25 +00:00
|
|
|
assert(node->type == nir_cf_node_loop);
|
2018-03-19 23:34:17 +00:00
|
|
|
|
|
|
|
nir_block *after = nir_cf_node_as_block(nir_cf_node_next(node));
|
2015-05-08 19:42:14 +01:00
|
|
|
|
2019-02-14 03:42:39 +00:00
|
|
|
/* Quick check if there are any phis that follow this CF node. If there
|
|
|
|
* are, then we automatically know it isn't dead.
|
|
|
|
*/
|
2015-05-08 19:42:14 +01:00
|
|
|
if (!exec_list_is_empty(&after->instr_list) &&
|
|
|
|
nir_block_first_instr(after)->type == nir_instr_type_phi)
|
|
|
|
return false;
|
|
|
|
|
2019-02-14 03:42:39 +00:00
|
|
|
nir_function_impl *impl = nir_cf_node_get_function(node);
|
|
|
|
nir_metadata_require(impl, nir_metadata_block_index);
|
|
|
|
|
2019-02-14 03:28:07 +00:00
|
|
|
nir_foreach_block_in_cf_node(block, node) {
|
|
|
|
bool inside_loop = node->type == nir_cf_node_loop;
|
|
|
|
for (nir_cf_node *n = &block->cf_node;
|
|
|
|
!inside_loop && n != node; n = n->parent) {
|
|
|
|
if (n->type == nir_cf_node_loop)
|
|
|
|
inside_loop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nir_foreach_instr(instr, block) {
|
|
|
|
if (instr->type == nir_instr_type_call)
|
2019-05-23 04:13:15 +01:00
|
|
|
return false;
|
2019-02-14 03:28:07 +00:00
|
|
|
|
|
|
|
/* Return instructions can cause us to skip over other side-effecting
|
|
|
|
* instructions after the loop, so consider them to have side effects
|
|
|
|
* here.
|
|
|
|
*
|
|
|
|
* When the block is not inside a loop, break and continue might also
|
|
|
|
* cause a skip.
|
|
|
|
*/
|
|
|
|
if (instr->type == nir_instr_type_jump &&
|
|
|
|
(!inside_loop || nir_instr_as_jump(instr)->type == nir_jump_return))
|
|
|
|
return false;
|
|
|
|
|
2019-02-14 03:42:39 +00:00
|
|
|
if (instr->type == nir_instr_type_intrinsic) {
|
|
|
|
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
|
|
|
if (!(nir_intrinsic_infos[intrin->intrinsic].flags &
|
|
|
|
NIR_INTRINSIC_CAN_ELIMINATE))
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-19 12:40:07 +01:00
|
|
|
|
2019-02-14 03:42:39 +00:00
|
|
|
if (!nir_foreach_ssa_def(instr, def_only_used_in_cf_node, node))
|
2015-05-08 19:42:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 07:38:17 +01:00
|
|
|
static bool
|
2015-05-08 06:44:24 +01:00
|
|
|
dead_cf_block(nir_block *block)
|
2015-05-01 07:38:17 +01:00
|
|
|
{
|
|
|
|
nir_if *following_if = nir_block_get_following_if(block);
|
2015-05-08 19:42:14 +01:00
|
|
|
if (following_if) {
|
2018-10-20 15:10:02 +01:00
|
|
|
if (!nir_src_is_const(following_if->condition))
|
2016-04-29 19:31:59 +01:00
|
|
|
return false;
|
2015-05-01 07:38:17 +01:00
|
|
|
|
2018-10-20 15:10:02 +01:00
|
|
|
opt_constant_if(following_if, nir_src_as_bool(following_if->condition));
|
2015-05-08 19:42:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-05-01 07:38:17 +01:00
|
|
|
|
2015-05-08 19:42:14 +01:00
|
|
|
nir_loop *following_loop = nir_block_get_following_loop(block);
|
|
|
|
if (!following_loop)
|
|
|
|
return false;
|
2015-05-01 07:38:17 +01:00
|
|
|
|
2018-03-19 23:34:17 +00:00
|
|
|
if (!node_is_dead(&following_loop->cf_node))
|
2015-05-08 19:42:14 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
nir_cf_node_remove(&following_loop->cf_node);
|
2015-05-01 07:38:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-08 06:44:24 +01:00
|
|
|
static bool
|
|
|
|
dead_cf_list(struct exec_list *list, bool *list_ends_in_jump)
|
2015-05-01 07:38:17 +01:00
|
|
|
{
|
|
|
|
bool progress = false;
|
2015-05-08 06:44:24 +01:00
|
|
|
*list_ends_in_jump = false;
|
|
|
|
|
|
|
|
nir_cf_node *prev = NULL;
|
|
|
|
|
|
|
|
foreach_list_typed(nir_cf_node, cur, node, list) {
|
|
|
|
switch (cur->type) {
|
|
|
|
case nir_cf_node_block: {
|
|
|
|
nir_block *block = nir_cf_node_as_block(cur);
|
|
|
|
if (dead_cf_block(block)) {
|
2015-05-08 19:42:14 +01:00
|
|
|
/* We just deleted the if or loop after this block, so we may have
|
2015-05-08 06:44:24 +01:00
|
|
|
* deleted the block before or after it -- which one is an
|
|
|
|
* implementation detail. Therefore, to recover the place we were
|
|
|
|
* at, we have to use the previous cf_node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (prev) {
|
|
|
|
cur = nir_cf_node_next(prev);
|
|
|
|
} else {
|
|
|
|
cur = exec_node_data(nir_cf_node, exec_list_get_head(list),
|
|
|
|
node);
|
|
|
|
}
|
|
|
|
|
|
|
|
block = nir_cf_node_as_block(cur);
|
|
|
|
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:34:05 +01:00
|
|
|
if (nir_block_ends_in_jump(block)) {
|
2015-05-08 06:44:24 +01:00
|
|
|
*list_ends_in_jump = true;
|
|
|
|
|
|
|
|
if (!exec_node_is_tail_sentinel(cur->node.next)) {
|
|
|
|
remove_after_cf_node(cur);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case nir_cf_node_if: {
|
|
|
|
nir_if *if_stmt = nir_cf_node_as_if(cur);
|
|
|
|
bool then_ends_in_jump, else_ends_in_jump;
|
|
|
|
progress |= dead_cf_list(&if_stmt->then_list, &then_ends_in_jump);
|
|
|
|
progress |= dead_cf_list(&if_stmt->else_list, &else_ends_in_jump);
|
|
|
|
|
|
|
|
if (then_ends_in_jump && else_ends_in_jump) {
|
|
|
|
*list_ends_in_jump = true;
|
|
|
|
nir_block *next = nir_cf_node_as_block(nir_cf_node_next(cur));
|
|
|
|
if (!exec_list_is_empty(&next->instr_list) ||
|
|
|
|
!exec_node_is_tail_sentinel(next->cf_node.node.next)) {
|
|
|
|
remove_after_cf_node(cur);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case nir_cf_node_loop: {
|
|
|
|
nir_loop *loop = nir_cf_node_as_loop(cur);
|
|
|
|
bool dummy;
|
|
|
|
progress |= dead_cf_list(&loop->body, &dummy);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
unreachable("unknown cf node type");
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
opt_dead_cf_impl(nir_function_impl *impl)
|
|
|
|
{
|
|
|
|
bool dummy;
|
|
|
|
bool progress = dead_cf_list(&impl->body, &dummy);
|
2015-05-01 07:38:17 +01:00
|
|
|
|
2018-09-10 22:31:29 +01:00
|
|
|
if (progress) {
|
2015-05-01 07:38:17 +01:00
|
|
|
nir_metadata_preserve(impl, nir_metadata_none);
|
2019-08-30 17:35:26 +01:00
|
|
|
|
|
|
|
/* The CF manipulation code called by this pass is smart enough to keep
|
|
|
|
* from breaking any SSA use/def chains by replacing any uses of removed
|
|
|
|
* instructions with SSA undefs. However, it's not quite smart enough
|
|
|
|
* to always preserve the dominance properties. In particular, if you
|
|
|
|
* remove the one break from a loop, stuff in the loop may still be used
|
|
|
|
* outside the loop even though there's no path between the two. We can
|
|
|
|
* easily fix these issues by calling nir_repair_ssa which will ensure
|
|
|
|
* that the dominance properties hold.
|
|
|
|
*/
|
|
|
|
nir_repair_ssa_impl(impl);
|
|
|
|
} else {
|
2018-09-10 22:31:29 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
impl->valid_metadata &= ~nir_metadata_not_properly_reset;
|
|
|
|
#endif
|
2019-08-30 17:35:26 +01:00
|
|
|
}
|
2015-05-01 07:38:17 +01:00
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nir_opt_dead_cf(nir_shader *shader)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
|
2016-04-27 04:26:42 +01:00
|
|
|
nir_foreach_function(function, shader)
|
2015-12-26 18:00:47 +00:00
|
|
|
if (function->impl)
|
|
|
|
progress |= opt_dead_cf_impl(function->impl);
|
2015-05-01 07:38:17 +01:00
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|