}
bblock_t::bblock_t(cfg_t *cfg) :
- cfg(cfg), start_ip(0), end_ip(0), num(0),
- if_block(NULL), else_block(NULL)
+ cfg(cfg), idom(NULL), start_ip(0), end_ip(0), num(0)
{
- start = NULL;
- end = NULL;
-
+ instructions.make_empty();
parents.make_empty();
children.make_empty();
}
if ((const bblock_t *)this->link.next != that)
return false;
- if (ends_block(this->end) ||
- starts_block(that->start))
+ if (ends_block(this->end()) ||
+ starts_block(that->start()))
return false;
return true;
}
this->end_ip = that->end_ip;
- this->end = that->end;
- this->else_block = that->else_block;
+ this->instructions.append_list(&that->instructions);
this->cfg->remove_block(that);
}
void
-bblock_t::dump(backend_visitor *v)
+bblock_t::dump(backend_shader *s) const
{
int ip = this->start_ip;
- for (backend_instruction *inst = (backend_instruction *)this->start;
- inst != this->end->next;
- inst = (backend_instruction *) inst->next) {
+ foreach_inst_in_block(backend_instruction, inst, this) {
fprintf(stderr, "%5d: ", ip);
- v->dump_instruction(inst);
+ s->dump_instruction(inst);
ip++;
}
}
block_list.make_empty();
blocks = NULL;
num_blocks = 0;
+ idom_dirty = true;
bblock_t *cur = NULL;
int ip = 0;
set_next_block(&cur, entry, ip);
- entry->start = (backend_instruction *) instructions->get_head();
-
- foreach_in_list(backend_instruction, inst, instructions) {
- cur->end = inst;
-
+ foreach_in_list_safe(backend_instruction, inst, instructions) {
/* set_next_block wants the post-incremented ip */
ip++;
+ inst->exec_node::remove();
+
switch (inst->opcode) {
case BRW_OPCODE_IF:
+ cur->instructions.push_tail(inst);
+
/* Push our information onto a stack so we can recover from
* nested ifs.
*/
* instructions.
*/
next = new_block();
- next->start = (backend_instruction *)inst->next;
cur_if->add_successor(mem_ctx, next);
set_next_block(&cur, next, ip);
break;
case BRW_OPCODE_ELSE:
+ cur->instructions.push_tail(inst);
+
cur_else = cur;
next = new_block();
- next->start = (backend_instruction *)inst->next;
+ assert(cur_if != NULL);
cur_if->add_successor(mem_ctx, next);
set_next_block(&cur, next, ip);
break;
case BRW_OPCODE_ENDIF: {
- if (cur->start == inst) {
+ if (cur->instructions.is_empty()) {
/* New block was just created; use it. */
cur_endif = cur;
} else {
cur_endif = new_block();
- cur_endif->start = inst;
- cur->end = (backend_instruction *)inst->prev;
cur->add_successor(mem_ctx, cur_endif);
set_next_block(&cur, cur_endif, ip - 1);
}
+ cur->instructions.push_tail(inst);
+
if (cur_else) {
cur_else->add_successor(mem_ctx, cur_endif);
} else {
+ assert(cur_if != NULL);
cur_if->add_successor(mem_ctx, cur_endif);
}
- assert(cur_if->end->opcode == BRW_OPCODE_IF);
- assert(!cur_else || cur_else->end->opcode == BRW_OPCODE_ELSE);
-
- cur_if->if_block = cur_if;
- cur_if->else_block = cur_else;
-
- if (cur_else) {
- cur_else->if_block = cur_if;
- cur_else->else_block = cur_else;
- }
-
- cur->if_block = cur_if;
- cur->else_block = cur_else;
+ assert(cur_if->end()->opcode == BRW_OPCODE_IF);
+ assert(!cur_else || cur_else->end()->opcode == BRW_OPCODE_ELSE);
/* Pop the stack so we're in the previous if/else/endif */
cur_if = pop_stack(&if_stack);
*/
cur_while = new_block();
- if (cur->start == inst) {
+ if (cur->instructions.is_empty()) {
/* New block was just created; use it. */
cur_do = cur;
} else {
cur_do = new_block();
- cur_do->start = inst;
- cur->end = (backend_instruction *)inst->prev;
cur->add_successor(mem_ctx, cur_do);
set_next_block(&cur, cur_do, ip - 1);
}
+
+ cur->instructions.push_tail(inst);
break;
case BRW_OPCODE_CONTINUE:
+ cur->instructions.push_tail(inst);
+
+ assert(cur_do != NULL);
cur->add_successor(mem_ctx, cur_do);
next = new_block();
- next->start = (backend_instruction *)inst->next;
if (inst->predicate)
cur->add_successor(mem_ctx, next);
break;
case BRW_OPCODE_BREAK:
+ cur->instructions.push_tail(inst);
+
+ assert(cur_while != NULL);
cur->add_successor(mem_ctx, cur_while);
next = new_block();
- next->start = (backend_instruction *)inst->next;
if (inst->predicate)
cur->add_successor(mem_ctx, next);
break;
case BRW_OPCODE_WHILE:
- cur_while->start = (backend_instruction *)inst->next;
+ cur->instructions.push_tail(inst);
+ assert(cur_do != NULL && cur_while != NULL);
cur->add_successor(mem_ctx, cur_do);
+
+ if (inst->predicate)
+ cur->add_successor(mem_ctx, cur_while);
+
set_next_block(&cur, cur_while, ip);
/* Pop the stack so we're in the previous loop */
break;
default:
+ cur->instructions.push_tail(inst);
break;
}
}
- assert(cur->end);
-
- cur->end_ip = ip;
+ cur->end_ip = ip - 1;
make_block_array();
}
this->blocks[this->num_blocks - 1]->num = this->num_blocks - 2;
this->num_blocks--;
+ idom_dirty = true;
}
bblock_t *
cfg_t::set_next_block(bblock_t **cur, bblock_t *block, int ip)
{
if (*cur) {
- assert((*cur)->end->next == block->start);
(*cur)->end_ip = ip - 1;
}
}
void
-cfg_t::dump(backend_visitor *v)
+cfg_t::dump(backend_shader *s)
{
+ if (idom_dirty)
+ calculate_idom();
+
foreach_block (block, this) {
- fprintf(stderr, "START B%d", block->num);
+ if (block->idom)
+ fprintf(stderr, "START B%d IDOM(B%d)", block->num, block->idom->num);
+ else
+ fprintf(stderr, "START B%d IDOM(none)", block->num);
+
foreach_list_typed(bblock_link, link, link, &block->parents) {
fprintf(stderr, " <-B%d",
link->block->num);
}
fprintf(stderr, "\n");
- block->dump(v);
+ if (s != NULL)
+ block->dump(s);
fprintf(stderr, "END B%d", block->num);
foreach_list_typed(bblock_link, link, link, &block->children) {
fprintf(stderr, " ->B%d",
fprintf(stderr, "\n");
}
}
+
+/* Calculates the immediate dominator of each block, according to "A Simple,
+ * Fast Dominance Algorithm" by Keith D. Cooper, Timothy J. Harvey, and Ken
+ * Kennedy.
+ *
+ * The authors claim that for control flow graphs of sizes normally encountered
+ * (less than 1000 nodes) that this algorithm is significantly faster than
+ * others like Lengauer-Tarjan.
+ */
+void
+cfg_t::calculate_idom()
+{
+ foreach_block(block, this) {
+ block->idom = NULL;
+ }
+ blocks[0]->idom = blocks[0];
+
+ bool changed;
+ do {
+ changed = false;
+
+ foreach_block(block, this) {
+ if (block->num == 0)
+ continue;
+
+ bblock_t *new_idom = NULL;
+ foreach_list_typed(bblock_link, parent, link, &block->parents) {
+ if (parent->block->idom) {
+ if (new_idom == NULL) {
+ new_idom = parent->block;
+ } else if (parent->block->idom != NULL) {
+ new_idom = intersect(parent->block, new_idom);
+ }
+ }
+ }
+
+ if (block->idom != new_idom) {
+ block->idom = new_idom;
+ changed = true;
+ }
+ }
+ } while (changed);
+
+ idom_dirty = false;
+}
+
+bblock_t *
+cfg_t::intersect(bblock_t *b1, bblock_t *b2)
+{
+ /* Note, the comparisons here are the opposite of what the paper says
+ * because we index blocks from beginning -> end (i.e. reverse post-order)
+ * instead of post-order like they assume.
+ */
+ while (b1->num != b2->num) {
+ while (b1->num > b2->num)
+ b1 = b1->idom;
+ while (b2->num > b1->num)
+ b2 = b2->idom;
+ }
+ assert(b1);
+ return b1;
+}
+
+void
+cfg_t::dump_cfg()
+{
+ printf("digraph CFG {\n");
+ for (int b = 0; b < num_blocks; b++) {
+ bblock_t *block = this->blocks[b];
+
+ foreach_list_typed_safe (bblock_link, child, link, &block->children) {
+ printf("\t%d -> %d\n", b, child->block->num);
+ }
+ }
+ printf("}\n");
+}
+
+void
+cfg_t::dump_domtree()
+{
+ printf("digraph DominanceTree {\n");
+ foreach_block(block, this) {
+ if (block->idom) {
+ printf("\t%d -> %d\n", block->idom->num, block->num);
+ }
+ }
+ printf("}\n");
+}