2012-04-10 12:01:50 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2012 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* 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:
|
|
|
|
|
* Eric Anholt <eric@anholt.net>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2024-12-06 14:25:29 -08:00
|
|
|
#pragma once
|
2014-02-19 14:47:57 -08:00
|
|
|
|
2024-02-20 20:46:11 -08:00
|
|
|
struct bblock_t;
|
|
|
|
|
|
2016-03-09 23:31:05 -08:00
|
|
|
#ifdef __cplusplus
|
2024-02-20 20:46:11 -08:00
|
|
|
|
2024-12-06 13:05:43 -08:00
|
|
|
#include "brw_inst.h"
|
2013-11-30 16:39:43 -08:00
|
|
|
|
2019-08-05 17:36:40 -07:00
|
|
|
/**
|
|
|
|
|
* CFG edge types.
|
|
|
|
|
*
|
|
|
|
|
* A logical edge represents a potential control flow path of the original
|
|
|
|
|
* scalar program, while a physical edge represents a control flow path that
|
|
|
|
|
* may not have existed in the original program but was introduced during
|
|
|
|
|
* vectorization in order to implement divergent control flow of different
|
|
|
|
|
* shader invocations within the same SIMD thread.
|
|
|
|
|
*
|
|
|
|
|
* All logical edges in the CFG are considered to be physical edges but not
|
|
|
|
|
* the other way around -- I.e. the logical CFG is a subset of the physical
|
|
|
|
|
* one.
|
|
|
|
|
*/
|
|
|
|
|
enum bblock_link_kind {
|
|
|
|
|
bblock_link_logical = 0,
|
|
|
|
|
bblock_link_physical
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-12 14:40:40 -07:00
|
|
|
struct bblock_link {
|
2014-05-12 09:54:15 -07:00
|
|
|
DECLARE_RALLOC_CXX_OPERATORS(bblock_link)
|
2012-04-10 12:01:50 -07:00
|
|
|
|
2019-08-05 17:36:40 -07:00
|
|
|
bblock_link(bblock_t *block, enum bblock_link_kind kind)
|
|
|
|
|
: block(block), kind(kind)
|
2012-04-10 12:01:50 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 14:40:40 -07:00
|
|
|
struct exec_node link;
|
2014-05-12 09:54:15 -07:00
|
|
|
struct bblock_t *block;
|
2019-08-05 17:36:40 -07:00
|
|
|
|
|
|
|
|
/* Type of this CFG edge. Because bblock_link_logical also implies
|
|
|
|
|
* bblock_link_physical, the proper way to test for membership of edge 'l'
|
|
|
|
|
* in CFG kind 'k' is 'l.kind <= k'.
|
|
|
|
|
*/
|
|
|
|
|
enum bblock_link_kind kind;
|
2012-04-10 12:01:50 -07:00
|
|
|
};
|
|
|
|
|
|
2024-12-07 10:25:45 -08:00
|
|
|
struct brw_shader;
|
2016-03-09 15:38:55 -08:00
|
|
|
struct cfg_t;
|
2014-05-12 09:54:15 -07:00
|
|
|
|
|
|
|
|
struct bblock_t {
|
2013-09-20 16:27:42 -07:00
|
|
|
DECLARE_RALLOC_CXX_OPERATORS(bblock_t)
|
2012-04-10 12:01:50 -07:00
|
|
|
|
2014-07-13 22:17:50 -07:00
|
|
|
explicit bblock_t(cfg_t *cfg);
|
2012-04-10 12:01:50 -07:00
|
|
|
|
2019-08-05 17:36:40 -07:00
|
|
|
void add_successor(void *mem_ctx, bblock_t *successor,
|
|
|
|
|
enum bblock_link_kind kind);
|
2023-11-03 22:40:45 -07:00
|
|
|
void dump(FILE *file = stderr) const;
|
2014-09-01 15:01:23 -07:00
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
brw_inst *start();
|
|
|
|
|
const brw_inst *start() const;
|
|
|
|
|
brw_inst *end();
|
|
|
|
|
const brw_inst *end() const;
|
2014-09-02 21:07:51 -07:00
|
|
|
|
|
|
|
|
bblock_t *next();
|
|
|
|
|
const bblock_t *next() const;
|
|
|
|
|
bblock_t *prev();
|
|
|
|
|
const bblock_t *prev() const;
|
2014-10-27 14:36:48 -07:00
|
|
|
|
|
|
|
|
bool ends_with_control_flow() const;
|
|
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
brw_inst *last_non_control_flow_inst();
|
2023-09-13 10:44:31 -07:00
|
|
|
|
2014-07-11 16:17:47 -07:00
|
|
|
struct exec_node link;
|
2014-07-13 22:17:50 -07:00
|
|
|
struct cfg_t *cfg;
|
2014-07-11 16:17:47 -07:00
|
|
|
|
2012-04-10 12:01:50 -07:00
|
|
|
int start_ip;
|
|
|
|
|
int end_ip;
|
|
|
|
|
|
2021-06-28 19:02:11 -07:00
|
|
|
/**
|
|
|
|
|
* Change in end_ip since the last time IPs of later blocks were updated.
|
|
|
|
|
*/
|
|
|
|
|
int end_ip_delta;
|
|
|
|
|
|
2014-09-01 15:01:23 -07:00
|
|
|
struct exec_list instructions;
|
2014-05-12 09:54:15 -07:00
|
|
|
struct exec_list parents;
|
|
|
|
|
struct exec_list children;
|
2014-07-11 22:31:39 -07:00
|
|
|
int num;
|
2012-04-10 12:01:50 -07:00
|
|
|
};
|
|
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
inline brw_inst *
|
2014-09-01 15:01:23 -07:00
|
|
|
bblock_t::start()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (brw_inst *)exec_list_get_head(&instructions);
|
2014-09-01 15:01:23 -07:00
|
|
|
}
|
|
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
inline const brw_inst *
|
2014-09-01 15:01:23 -07:00
|
|
|
bblock_t::start() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (const brw_inst *)exec_list_get_head_const(&instructions);
|
2014-09-01 15:01:23 -07:00
|
|
|
}
|
|
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
inline brw_inst *
|
2014-09-01 15:01:23 -07:00
|
|
|
bblock_t::end()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (brw_inst *)exec_list_get_tail(&instructions);
|
2014-09-01 15:01:23 -07:00
|
|
|
}
|
|
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
inline const brw_inst *
|
2014-09-01 15:01:23 -07:00
|
|
|
bblock_t::end() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (const brw_inst *)exec_list_get_tail_const(&instructions);
|
2014-09-01 15:01:23 -07:00
|
|
|
}
|
2014-09-02 21:07:51 -07:00
|
|
|
|
|
|
|
|
inline bblock_t *
|
|
|
|
|
bblock_t::next()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
if (exec_node_is_tail_sentinel(link.next))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return (struct bblock_t *)link.next;
|
2014-09-02 21:07:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const bblock_t *
|
|
|
|
|
bblock_t::next() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
if (exec_node_is_tail_sentinel(link.next))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return (const struct bblock_t *)link.next;
|
2014-09-02 21:07:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bblock_t *
|
|
|
|
|
bblock_t::prev()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
if (exec_node_is_head_sentinel(link.prev))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return (struct bblock_t *)link.prev;
|
2014-09-02 21:07:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const bblock_t *
|
|
|
|
|
bblock_t::prev() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
if (exec_node_is_head_sentinel(link.prev))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return (const struct bblock_t *)link.prev;
|
2014-09-02 21:07:51 -07:00
|
|
|
}
|
2014-10-27 14:36:48 -07:00
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
|
bblock_t::ends_with_control_flow() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
enum opcode op = end()->opcode;
|
|
|
|
|
return op == BRW_OPCODE_IF ||
|
|
|
|
|
op == BRW_OPCODE_ELSE ||
|
|
|
|
|
op == BRW_OPCODE_WHILE ||
|
|
|
|
|
op == BRW_OPCODE_BREAK ||
|
|
|
|
|
op == BRW_OPCODE_CONTINUE ||
|
|
|
|
|
op == SHADER_OPCODE_FLOW;
|
2014-10-27 14:36:48 -07:00
|
|
|
}
|
|
|
|
|
|
2024-12-07 00:23:07 -08:00
|
|
|
inline brw_inst *
|
2014-10-27 14:36:48 -07:00
|
|
|
bblock_t::last_non_control_flow_inst()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
brw_inst *inst = end();
|
|
|
|
|
if (ends_with_control_flow())
|
|
|
|
|
inst = (brw_inst *)inst->prev;
|
|
|
|
|
return inst;
|
2014-10-27 14:36:48 -07:00
|
|
|
}
|
2014-09-01 15:01:23 -07:00
|
|
|
|
2014-06-29 18:18:53 -07:00
|
|
|
struct cfg_t {
|
2016-11-28 13:24:04 -08:00
|
|
|
DECLARE_RALLOC_CXX_OPERATORS(cfg_t)
|
2012-04-10 12:01:50 -07:00
|
|
|
|
2025-02-27 22:04:03 -08:00
|
|
|
cfg_t(brw_shader *s, exec_list *instructions);
|
2012-10-03 13:16:09 -07:00
|
|
|
~cfg_t();
|
2012-11-20 17:30:46 -08:00
|
|
|
|
2014-07-13 22:17:50 -07:00
|
|
|
void remove_block(bblock_t *block);
|
|
|
|
|
|
2020-04-03 21:25:40 -05:00
|
|
|
bblock_t *first_block();
|
|
|
|
|
const bblock_t *first_block() const;
|
|
|
|
|
bblock_t *last_block();
|
|
|
|
|
const bblock_t *last_block() const;
|
|
|
|
|
|
2012-10-03 13:16:09 -07:00
|
|
|
bblock_t *new_block();
|
2013-11-30 20:38:48 -08:00
|
|
|
void set_next_block(bblock_t **cur, bblock_t *block, int ip);
|
2012-04-10 12:01:50 -07:00
|
|
|
void make_block_array();
|
|
|
|
|
|
2023-11-03 22:40:45 -07:00
|
|
|
void dump(FILE *file = stderr);
|
2014-02-26 16:07:52 -08:00
|
|
|
void dump_cfg();
|
2021-07-01 10:24:01 -07:00
|
|
|
|
2023-09-11 09:10:47 -07:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
void validate(UNUSED const char *stage_abbrev) { }
|
|
|
|
|
#else
|
|
|
|
|
void validate(const char *stage_abbrev);
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-07-01 10:24:01 -07:00
|
|
|
/**
|
|
|
|
|
* Propagate bblock_t::end_ip_delta data through the CFG.
|
|
|
|
|
*/
|
|
|
|
|
inline void adjust_block_ips();
|
|
|
|
|
|
2025-02-27 22:04:03 -08:00
|
|
|
struct brw_shader *s;
|
2012-04-10 12:01:50 -07:00
|
|
|
void *mem_ctx;
|
|
|
|
|
|
|
|
|
|
/** Ordered list (by ip) of basic blocks */
|
2014-06-29 18:18:53 -07:00
|
|
|
struct exec_list block_list;
|
|
|
|
|
struct bblock_t **blocks;
|
2012-04-10 12:01:50 -07:00
|
|
|
int num_blocks;
|
|
|
|
|
};
|
2014-02-19 14:47:57 -08:00
|
|
|
|
2020-04-03 21:25:40 -05:00
|
|
|
inline bblock_t *
|
|
|
|
|
cfg_t::first_block()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (struct bblock_t *)exec_list_get_head(&block_list);
|
2020-04-03 21:25:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inline bblock_t *
|
|
|
|
|
cfg_t::first_block() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (const struct bblock_t *)exec_list_get_head_const(&block_list);
|
2020-04-03 21:25:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bblock_t *
|
|
|
|
|
cfg_t::last_block()
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (struct bblock_t *)exec_list_get_tail(&block_list);
|
2020-04-03 21:25:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inline bblock_t *
|
|
|
|
|
cfg_t::last_block() const
|
|
|
|
|
{
|
2025-03-07 19:47:01 -08:00
|
|
|
return (const struct bblock_t *)exec_list_get_tail_const(&block_list);
|
2020-04-03 21:25:40 -05:00
|
|
|
}
|
|
|
|
|
|
2014-09-02 17:30:29 -07:00
|
|
|
/* Note that this is implemented with a double for loop -- break will
|
|
|
|
|
* break from the inner loop only!
|
|
|
|
|
*/
|
2014-07-11 21:17:01 -07:00
|
|
|
#define foreach_block_and_inst(__block, __type, __inst, __cfg) \
|
|
|
|
|
foreach_block (__block, __cfg) \
|
|
|
|
|
foreach_inst_in_block (__type, __inst, __block)
|
|
|
|
|
|
2014-09-02 17:30:29 -07:00
|
|
|
/* Note that this is implemented with a double for loop -- break will
|
|
|
|
|
* break from the inner loop only!
|
|
|
|
|
*/
|
2014-07-14 19:48:15 -07:00
|
|
|
#define foreach_block_and_inst_safe(__block, __type, __inst, __cfg) \
|
|
|
|
|
foreach_block_safe (__block, __cfg) \
|
|
|
|
|
foreach_inst_in_block_safe (__type, __inst, __block)
|
|
|
|
|
|
2014-07-11 22:31:39 -07:00
|
|
|
#define foreach_block(__block, __cfg) \
|
|
|
|
|
foreach_list_typed (bblock_t, __block, link, &(__cfg)->block_list)
|
|
|
|
|
|
2014-12-30 19:54:50 -08:00
|
|
|
#define foreach_block_reverse(__block, __cfg) \
|
|
|
|
|
foreach_list_typed_reverse (bblock_t, __block, link, &(__cfg)->block_list)
|
|
|
|
|
|
2014-07-14 11:15:51 -07:00
|
|
|
#define foreach_block_safe(__block, __cfg) \
|
|
|
|
|
foreach_list_typed_safe (bblock_t, __block, link, &(__cfg)->block_list)
|
|
|
|
|
|
2015-11-30 09:25:19 -08:00
|
|
|
#define foreach_block_reverse_safe(__block, __cfg) \
|
|
|
|
|
foreach_list_typed_reverse_safe (bblock_t, __block, link, &(__cfg)->block_list)
|
|
|
|
|
|
2014-06-24 12:42:00 -07:00
|
|
|
#define foreach_inst_in_block(__type, __inst, __block) \
|
2014-09-01 15:01:23 -07:00
|
|
|
foreach_in_list(__type, __inst, &(__block)->instructions)
|
2014-06-24 12:42:00 -07:00
|
|
|
|
2014-07-10 17:30:40 -07:00
|
|
|
#define foreach_inst_in_block_safe(__type, __inst, __block) \
|
2016-06-27 14:42:57 -07:00
|
|
|
for (__type *__inst = (__type *)__block->instructions.head_sentinel.next, \
|
2016-05-26 12:09:33 -07:00
|
|
|
*__next = (__type *)__inst->next; \
|
|
|
|
|
__next != NULL; \
|
2014-07-10 17:30:40 -07:00
|
|
|
__inst = __next, \
|
|
|
|
|
__next = (__type *)__next->next)
|
|
|
|
|
|
2014-06-24 12:42:00 -07:00
|
|
|
#define foreach_inst_in_block_reverse(__type, __inst, __block) \
|
2014-09-01 15:01:23 -07:00
|
|
|
foreach_in_list_reverse(__type, __inst, &(__block)->instructions)
|
2014-06-24 12:42:00 -07:00
|
|
|
|
2014-12-30 16:14:43 -08:00
|
|
|
#define foreach_inst_in_block_reverse_safe(__type, __inst, __block) \
|
|
|
|
|
foreach_in_list_reverse_safe(__type, __inst, &(__block)->instructions)
|
|
|
|
|
|
2015-10-20 11:16:00 +02:00
|
|
|
#define foreach_inst_in_block_starting_from(__type, __scan_inst, __inst) \
|
2014-09-01 20:01:50 -07:00
|
|
|
for (__type *__scan_inst = (__type *)__inst->next; \
|
2014-09-01 15:01:23 -07:00
|
|
|
!__scan_inst->is_tail_sentinel(); \
|
2014-09-01 20:01:50 -07:00
|
|
|
__scan_inst = (__type *)__scan_inst->next)
|
|
|
|
|
|
2015-10-20 11:16:00 +02:00
|
|
|
#define foreach_inst_in_block_reverse_starting_from(__type, __scan_inst, __inst) \
|
2014-09-01 20:01:50 -07:00
|
|
|
for (__type *__scan_inst = (__type *)__inst->prev; \
|
2014-09-01 15:01:23 -07:00
|
|
|
!__scan_inst->is_head_sentinel(); \
|
2014-09-01 20:01:50 -07:00
|
|
|
__scan_inst = (__type *)__scan_inst->prev)
|
|
|
|
|
|
2021-07-01 10:24:01 -07:00
|
|
|
inline void
|
|
|
|
|
cfg_t::adjust_block_ips()
|
|
|
|
|
{
|
|
|
|
|
int delta = 0;
|
|
|
|
|
|
|
|
|
|
foreach_block(block, this) {
|
|
|
|
|
block->start_ip += delta;
|
|
|
|
|
block->end_ip += delta;
|
|
|
|
|
|
|
|
|
|
delta += block->end_ip_delta;
|
|
|
|
|
|
|
|
|
|
block->end_ip_delta = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 23:31:05 -08:00
|
|
|
#endif
|