2016-09-22 21:19:29 +00:00
|
|
|
#include "mcg.h"
|
|
|
|
|
2016-09-26 21:03:04 +00:00
|
|
|
static MAPOF(struct basicblock, struct basicblock) graph;
|
2016-09-26 20:48:58 +00:00
|
|
|
static ARRAYOF(struct ir) pops;
|
|
|
|
static ARRAYOF(struct ir) pushes;
|
2016-09-23 21:59:15 +00:00
|
|
|
|
2016-09-22 21:19:29 +00:00
|
|
|
static struct ir* get_last_push(struct basicblock* bb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=bb->irs.count-1; i>=0; i--)
|
2016-09-22 21:19:29 +00:00
|
|
|
{
|
2016-09-26 20:48:58 +00:00
|
|
|
struct ir* ir = bb->irs.item[i];
|
2016-09-22 21:19:29 +00:00
|
|
|
|
|
|
|
if (ir->opcode == IR_PUSH)
|
|
|
|
return ir;
|
|
|
|
if (ir_find(ir, IR_POP))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ir* get_first_pop(struct basicblock* bb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=0; i<bb->irs.count; i++)
|
2016-09-22 21:19:29 +00:00
|
|
|
{
|
|
|
|
struct ir* irr;
|
2016-09-26 20:48:58 +00:00
|
|
|
struct ir* ir = bb->irs.item[i];
|
2016-09-22 21:19:29 +00:00
|
|
|
|
|
|
|
if (ir->opcode == IR_PUSH)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
irr = ir_find(ir, IR_POP);
|
|
|
|
if (irr)
|
|
|
|
return irr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:59:15 +00:00
|
|
|
static bool collect_outputs_cb(struct ir* ir, void* user)
|
|
|
|
{
|
|
|
|
struct basicblock* caller = user;
|
|
|
|
|
|
|
|
if (ir->opcode == IR_BLOCK)
|
2016-09-26 21:03:04 +00:00
|
|
|
map_addp(&graph, caller, ir->u.bvalue);
|
2016-09-23 21:59:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_bb_graph(struct procedure* proc)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2016-09-26 21:03:04 +00:00
|
|
|
graph.count = 0;
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=0; i<proc->blocks.count; i++)
|
2016-09-23 21:59:15 +00:00
|
|
|
{
|
2016-09-26 20:48:58 +00:00
|
|
|
struct basicblock* bb = proc->blocks.item[i];
|
|
|
|
for (j=0; j<bb->irs.count; j++)
|
|
|
|
ir_walk(bb->irs.item[j], collect_outputs_cb, bb);
|
2016-09-23 21:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_block(struct procedure* proc, struct basicblock* bb)
|
2016-09-22 21:19:29 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct ir* ir;
|
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
pushes.count = pops.count = 0;
|
2016-09-22 21:19:29 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
struct ir* lastpush = get_last_push(bb);
|
|
|
|
if (!lastpush)
|
|
|
|
return;
|
|
|
|
|
2016-09-23 21:59:15 +00:00
|
|
|
/* Abort unless *every* successor block of this one starts with a pop
|
2016-09-22 21:19:29 +00:00
|
|
|
* of the same size... */
|
|
|
|
|
2016-09-26 21:03:04 +00:00
|
|
|
for (i=0; i<graph.count; i++)
|
2016-09-22 21:19:29 +00:00
|
|
|
{
|
2016-09-26 21:03:04 +00:00
|
|
|
if (graph.item[i].left == bb)
|
2016-09-22 21:19:29 +00:00
|
|
|
{
|
2016-09-26 21:03:04 +00:00
|
|
|
struct basicblock* outbb = graph.item[i].right;
|
2016-09-22 21:19:29 +00:00
|
|
|
|
2016-09-23 21:59:15 +00:00
|
|
|
ir = get_first_pop(outbb);
|
2016-09-22 21:19:29 +00:00
|
|
|
if (!ir || (ir->size != lastpush->size))
|
|
|
|
return;
|
2016-09-26 20:48:58 +00:00
|
|
|
array_appendu(&pops, ir);
|
2016-09-23 21:59:15 +00:00
|
|
|
|
|
|
|
/* Also abort unless *every* predecessor block of the one we've
|
|
|
|
* just found *also* ends in a push of the same size. */
|
|
|
|
|
2016-09-26 21:03:04 +00:00
|
|
|
for (j=0; j<graph.count; j++)
|
2016-09-23 21:59:15 +00:00
|
|
|
{
|
2016-09-26 21:03:04 +00:00
|
|
|
if (graph.item[j].right == outbb)
|
2016-09-23 21:59:15 +00:00
|
|
|
{
|
2016-09-26 21:03:04 +00:00
|
|
|
struct basicblock* inbb = graph.item[j].left;
|
2016-09-23 21:59:15 +00:00
|
|
|
|
|
|
|
ir = get_last_push(inbb);
|
|
|
|
if (!ir || (ir->size != lastpush->size))
|
|
|
|
return;
|
2016-09-26 20:48:58 +00:00
|
|
|
array_appendu(&pushes, ir);
|
2016-09-23 21:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-22 21:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Okay, now we can wire them all up. */
|
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=0; i<pushes.count; i++)
|
2016-09-22 21:19:29 +00:00
|
|
|
{
|
2016-09-26 20:48:58 +00:00
|
|
|
struct ir* ir = pushes.item[i];
|
2016-09-23 21:59:15 +00:00
|
|
|
assert(ir->is_sequence);
|
|
|
|
*ir = *ir->left;
|
|
|
|
ir->is_sequence = true;
|
|
|
|
}
|
2016-09-22 21:19:29 +00:00
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=0; i<pops.count; i++)
|
2016-09-23 21:59:15 +00:00
|
|
|
{
|
2016-09-26 20:48:58 +00:00
|
|
|
struct ir* ir = pops.item[i];
|
|
|
|
struct ir* pushir = pushes.item[0];
|
2016-09-23 21:59:15 +00:00
|
|
|
struct ir* phi = new_ir1(IR_PHI, ir->size, pushir);
|
2016-09-22 21:19:29 +00:00
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (j=1; j<pushes.count; j++)
|
|
|
|
phi = new_ir2(IR_PHI, ir->size, phi, pushes.item[j]);
|
2016-09-22 21:19:29 +00:00
|
|
|
|
2016-09-23 21:59:15 +00:00
|
|
|
phi->is_sequence = ir->is_sequence;
|
|
|
|
*ir = *phi;
|
2016-09-22 21:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 19:07:16 +00:00
|
|
|
void pass_convert_stack_ops(struct procedure* proc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-09-23 21:59:15 +00:00
|
|
|
make_bb_graph(proc);
|
|
|
|
|
2016-09-26 20:48:58 +00:00
|
|
|
for (i=0; i<proc->blocks.count; i++)
|
|
|
|
convert_block(proc, proc->blocks.item[i]);
|
2016-09-23 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:19:29 +00:00
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|