2016-10-04 21:42:00 +00:00
|
|
|
#include "mcg.h"
|
|
|
|
|
|
|
|
/* Insert empty nodes at certain places in the basic block graph so that when
|
|
|
|
* we convert out of SSA form, we have somewhere to insert copies. This is
|
|
|
|
* necessary for correctness in certain circumstances. The best explanation of
|
|
|
|
* why I've found is here, starting at the bottom of page 23.
|
|
|
|
*
|
|
|
|
* Briggs, Preston, et al.
|
|
|
|
* "Practical improvements to the construction and destruction of static single assignment form."
|
|
|
|
* Software-Practice and experience 28.8 (1998): 859-882.
|
|
|
|
*
|
|
|
|
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.25.749
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct procedure* current_proc;
|
|
|
|
|
|
|
|
struct rewrite_params
|
|
|
|
{
|
|
|
|
struct basicblock* find;
|
|
|
|
struct basicblock* replace;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool find_replace_cb(struct ir* ir, void* user)
|
|
|
|
{
|
|
|
|
struct rewrite_params* rwp = user;
|
|
|
|
|
|
|
|
if ((ir->opcode == IR_BLOCK) && (ir->u.bvalue == rwp->find))
|
|
|
|
ir->u.bvalue = rwp->replace;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void split_edge(struct basicblock* source, struct basicblock* sink)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct rewrite_params rwp;
|
|
|
|
struct basicblock* bb = bb_get(NULL);
|
|
|
|
|
2016-10-06 19:34:21 +00:00
|
|
|
struct ir* jump =
|
2016-10-04 21:42:00 +00:00
|
|
|
new_ir1(
|
|
|
|
IR_JUMP, 0,
|
|
|
|
new_bbir(sink)
|
2016-10-06 19:34:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
jump->root = jump->left->root = jump;
|
|
|
|
array_append(&bb->irs, jump);
|
2016-10-04 21:42:00 +00:00
|
|
|
|
|
|
|
rwp.find = sink;
|
|
|
|
rwp.replace = bb;
|
|
|
|
|
|
|
|
for (i=0; i<source->irs.count; i++)
|
|
|
|
ir_walk(source->irs.item[i], find_replace_cb, &rwp);
|
|
|
|
|
|
|
|
array_remove(&source->nexts, sink);
|
|
|
|
array_append(&source->nexts, bb);
|
|
|
|
|
|
|
|
array_append(&bb->prevs, source);
|
|
|
|
array_append(&bb->nexts, sink);
|
|
|
|
|
|
|
|
array_remove(&sink->prevs, source);
|
|
|
|
array_append(&sink->prevs, bb);
|
|
|
|
|
|
|
|
array_append(¤t_proc->blocks, bb);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:18:37 +00:00
|
|
|
static bool consider_edges_leading_from(struct basicblock* bb)
|
2016-10-04 21:42:00 +00:00
|
|
|
{
|
2016-10-10 21:18:37 +00:00
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
if (bb->nexts.count > 1)
|
2016-10-04 21:42:00 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-10-10 21:18:37 +00:00
|
|
|
for (i=0; i<bb->nexts.count; i++)
|
2016-10-04 21:42:00 +00:00
|
|
|
{
|
2016-10-10 21:18:37 +00:00
|
|
|
struct basicblock* next = bb->nexts.item[i];
|
|
|
|
if (next->prevs.count > 1)
|
|
|
|
{
|
|
|
|
split_edge(bb, next);
|
|
|
|
changed = true;
|
|
|
|
}
|
2016-10-04 21:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 21:18:37 +00:00
|
|
|
|
|
|
|
return changed;
|
2016-10-04 21:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pass_split_critical_edges(struct procedure* proc)
|
|
|
|
{
|
|
|
|
int i;
|
2016-10-10 21:18:37 +00:00
|
|
|
bool changed;
|
2016-10-04 21:42:00 +00:00
|
|
|
|
|
|
|
current_proc = proc;
|
|
|
|
|
2016-10-10 21:18:37 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
changed = false;
|
|
|
|
|
|
|
|
for (i=0; i<proc->blocks.count; i++)
|
|
|
|
changed |= consider_edges_leading_from(proc->blocks.item[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
while (changed);
|
2016-10-04 21:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|
|
|
|
|