Skip to content

Commit

Permalink
Merge branch 'uniq-phinode'
Browse files Browse the repository at this point in the history
* phi-sources can only have a single user (or none)c
  • Loading branch information
lucvoo committed Mar 8, 2021
2 parents 1749410 + 3caeefd commit 5485c7b
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Documentation/IR.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ Others
* .phi_src: operand (type must be compatible with .target, alias .src)
* .target: the "result" PSEUDO_PHI
* .type: type of .target
* .phi_users: list of phi instructions using the target pseudo
* .phi_node: the unique phi instruction using the target pseudo

.. op:: OP_CALL
Function call.
Expand Down
21 changes: 1 addition & 20 deletions flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,6 @@ static int rewrite_branch(struct basic_block *bb,
return 1;
}

///
// returns the phi-node corresponding to a phi-source
static struct instruction *get_phinode(struct instruction *phisrc)
{
struct pseudo_user *pu;

FOR_EACH_PTR(phisrc->target->users, pu) {
struct instruction *user;

if (!pu)
continue;
user = pu->insn;
assert(user->opcode == OP_PHI);
return user;
} END_FOR_EACH_PTR(pu);
assert(0);
}


/*
* Return the known truth value of a pseudo, or -1 if
* it's not known.
Expand Down Expand Up @@ -852,7 +833,7 @@ static int retarget_parents(struct basic_block *bb, struct basic_block *target)

static void remove_merging_phisrc(struct basic_block *top, struct instruction *insn)
{
struct instruction *user = get_phinode(insn);
struct instruction *user = insn->phi_node;
pseudo_t phi;

FOR_EACH_PTR(user->phi_list, phi) {
Expand Down
14 changes: 5 additions & 9 deletions linearize.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,7 @@ const char *show_instruction(struct instruction *insn)
break;

case OP_PHISOURCE: {
struct instruction *phi;
buf += sprintf(buf, "%s <- %s ", show_pseudo(insn->target), show_pseudo(insn->phi_src));
FOR_EACH_PTR(insn->phi_users, phi) {
buf += sprintf(buf, " (%s)", show_pseudo(phi->target));
} END_FOR_EACH_PTR(phi);
break;
}

Expand Down Expand Up @@ -1683,8 +1679,8 @@ static pseudo_t add_join_conditional(struct entrypoint *ep, struct expression *e
return (phi1 == VOID) ? phi1 : phi1->def->src;

phi_node = alloc_typed_instruction(OP_PHI, expr->ctype);
use_pseudo(phi_node, phi1, add_pseudo(&phi_node->phi_list, phi1));
use_pseudo(phi_node, phi2, add_pseudo(&phi_node->phi_list, phi2));
link_phi(phi_node, phi1);
link_phi(phi_node, phi2);
phi_node->target = target = alloc_pseudo(phi_node);
add_one_insn(ep, phi_node);
return target;
Expand Down Expand Up @@ -1756,7 +1752,7 @@ static void insert_phis(struct basic_block *bb, pseudo_t src, struct symbol *cty
struct instruction *br = delete_last_instruction(&parent->insns);
pseudo_t phi = alloc_phi(parent, src, ctype);
add_instruction(&parent->insns, br);
use_pseudo(node, phi, add_pseudo(&node->phi_list, phi));
link_phi(node, phi);
} END_FOR_EACH_PTR(parent);
}

Expand Down Expand Up @@ -1789,7 +1785,7 @@ static pseudo_t linearize_logical(struct entrypoint *ep, struct expression *expr
src2 = linearize_expression_to_bool(ep, expr->right);
src2 = cast_pseudo(ep, src2, &bool_ctype, ctype);
phi2 = alloc_phi(ep->active, src2, ctype);
use_pseudo(node, phi2, add_pseudo(&node->phi_list, phi2));
link_phi(node, phi2);

// join
set_activeblock(ep, merge);
Expand Down Expand Up @@ -2049,7 +2045,7 @@ static void add_return(struct entrypoint *ep, struct basic_block *bb, struct sym
}
phi = alloc_phi(ep->active, src, ctype);
phi->ident = &return_ident;
use_pseudo(phi_node, phi, add_pseudo(&phi_node->phi_list, phi));
link_phi(phi_node, phi);
}

static pseudo_t linearize_fn_statement(struct entrypoint *ep, struct statement *stmt)
Expand Down
8 changes: 7 additions & 1 deletion linearize.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct instruction {
};
struct /* phi source */ {
pseudo_t phi_src;
struct instruction_list *phi_users;
struct instruction *phi_node;
};
struct /* unops */ {
pseudo_t src;
Expand Down Expand Up @@ -291,6 +291,12 @@ static inline void use_pseudo(struct instruction *insn, pseudo_t p, pseudo_t *pp
add_pseudo_user_ptr(alloc_pseudo_user(insn, pp), &p->users);
}

static inline void link_phi(struct instruction *node, pseudo_t phi)
{
use_pseudo(node, phi, add_pseudo(&node->phi_list, phi));
phi->def->phi_node = node;
}

static inline void remove_bb_from_list(struct basic_block_list **list, struct basic_block *entry, int count)
{
delete_ptr_list_entry((struct ptr_list **)list, entry, count);
Expand Down
1 change: 0 additions & 1 deletion liveness.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ static void track_phi_uses(struct instruction *insn)
continue;
def = phi->def;
assert(def->opcode == OP_PHISOURCE);
add_ptr_list(&def->phi_users, insn);
} END_FOR_EACH_PTR(phi);
}

Expand Down
1 change: 1 addition & 0 deletions memops.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static int find_dominating_parents(pseudo_t pseudo, struct instruction *insn,
phi->ident = phi->ident ? : one->target->ident;
add_instruction(&parent->insns, br);
use_pseudo(insn, phi, add_pseudo(dominators, phi));
phi->def->phi_node = insn;
} END_FOR_EACH_PTR(parent);
return 1;
}
Expand Down
2 changes: 0 additions & 2 deletions sparse-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,8 +1342,6 @@ int main(int argc, char **argv)

compile(module, symlist);

/* need ->phi_users */
dbg_dead = 1;
FOR_EACH_PTR(filelist, file) {
symlist = sparse(file);
if (die_if_error)
Expand Down
2 changes: 1 addition & 1 deletion ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ static void ssa_rename_phi(struct instruction *insn)
pseudo_t phi = alloc_phi(par, val, var);
phi->ident = var->ident;
add_instruction(&par->insns, term);
use_pseudo(insn, phi, add_pseudo(&insn->phi_list, phi));
link_phi(insn, phi);
mark_phi_used(val);
} END_FOR_EACH_PTR(par);
}
Expand Down
30 changes: 0 additions & 30 deletions storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,35 +261,6 @@ static void set_up_argument_storage(struct entrypoint *ep, struct basic_block *b
} END_FOR_EACH_PTR(arg);
}

/*
* One phi-source may feed multiple phi nodes. If so, combine
* the storage output for this bb into one entry to reduce
* storage pressure.
*/
static void combine_phi_storage(struct basic_block *bb)
{
struct instruction *insn;
FOR_EACH_PTR(bb->insns, insn) {
struct instruction *phi;
struct storage *last;

if (!insn->bb || insn->opcode != OP_PHISOURCE)
continue;
last = NULL;
FOR_EACH_PTR(insn->phi_users, phi) {
struct storage *storage = lookup_storage(bb, phi->target, STOR_OUT);
if (!storage) {
DELETE_CURRENT_PTR(phi);
continue;
}
if (last && storage != last)
storage = combine_storage(storage, last);
last = storage;
} END_FOR_EACH_PTR(phi);
PACK_PTR_LIST(&insn->phi_users);
} END_FOR_EACH_PTR(insn);
}

void set_up_storage(struct entrypoint *ep)
{
struct basic_block *bb;
Expand All @@ -300,7 +271,6 @@ void set_up_storage(struct entrypoint *ep)
/* Then do a list of all the inter-bb storage */
FOR_EACH_PTR(ep->bbs, bb) {
set_up_bb_storage(bb);
combine_phi_storage(bb);
} END_FOR_EACH_PTR(bb);

name_storage();
Expand Down

0 comments on commit 5485c7b

Please sign in to comment.