#include <assert.h>
#include "iroptimize.h"
-#include "archop.h"
#include "irflag.h"
#include "ircons.h"
#include "list.h"
#include "irgraph_t.h"
#include "irnode_t.h"
#include "iropt_t.h"
+#include "irpass_t.h"
#include "irgwalk.h"
#include "irop.h"
#include "irouts.h"
union {
long proj; /**< For Proj nodes, its proj number */
ir_entity *ent; /**< For Sel Nodes, its entity */
+ int intVal; /**< For Conv/Div Nodes: strict/remainderless */
+ unsigned uintVal;/**< for Builtin: the kind */
+ ir_node *block; /**< for Block: itself */
+ void *ptr; /**< generic pointer for hash/cmp */
} u;
};
case iro_Sel:
key.u.ent = get_Sel_entity(irn);
break;
+ case iro_Conv:
+ key.u.intVal = get_Conv_strict(irn);
+ break;
+ case iro_Div:
+ key.u.intVal = get_Div_no_remainder(irn);
+ break;
+ case iro_Block:
+ key.u.block = irn;
+ break;
+ case iro_Load:
+ key.mode = get_Load_mode(irn);
+ break;
+ case iro_Builtin:
+ key.u.intVal = get_Builtin_kind(irn);
+ break;
default:
break;
}
first = 0;
} else {
- assert(key.code == get_irn_opcode(irn));
+ assert((unsigned)key.code == get_irn_opcode(irn));
assert(key.mode == get_irn_mode(irn));
assert(key.arity == get_irn_arity(irn));
case iro_Sel:
assert(key.u.ent == get_Sel_entity(irn));
break;
+ case iro_Conv:
+ assert(key.u.intVal == get_Conv_strict(irn));
+ break;
+ case iro_Div:
+ assert(key.u.intVal == get_Div_no_remainder(irn));
+ break;
+ case iro_Block:
+ assert(key.u.block == irn);
+ break;
+ case iro_Load:
+ assert(key.mode == get_Load_mode(irn));
+ break;
+ case iro_Builtin:
+ assert(key.u.intVal == (int) get_Builtin_kind(irn));
+ break;
default:
break;
}
assert(leader != node && leader->part == node->part);
}
}
+#else
+ (void) env;
#endif
}
* Check list.
*/
static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
- const node_t *e;
+#ifndef NDEBUG
+ const node_t *e;
#define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
for (e = list; e != NULL; e = NEXT(e)) {
assert(e->part == Z);
}
#undef NEXT
+#else
+ (void) list;
+ (void) ofs;
+ (void) Z;
+#endif
} /* ido_check_list */
/**
* @return a hash value for the given opcode map entry
*/
static unsigned opcode_hash(const opcode_key_t *entry) {
- return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
+ return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ptr) + entry->arity;
} /* opcode_hash */
/**
(void) size;
return o1->code != o2->code || o1->mode != o2->mode ||
o1->arity != o2->arity ||
- o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
+ o1->u.proj != o2->u.proj ||
+ o1->u.intVal != o2->u.intVal || /* this already checks uIntVal */
+ o1->u.ptr != o2->u.ptr;
} /* cmp_opcode */
/**
if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
return 0;
break;
- case iro_Min:
- case iro_Max:
- /* all inputs are followers */
- return 1;
default:
assert(!"opcode not implemented yet");
break;
case iro_Sel:
key.u.ent = get_Sel_entity(irn);
break;
+ case iro_Conv:
+ key.u.intVal = get_Conv_strict(irn);
+ break;
+ case iro_Div:
+ key.u.intVal = get_Div_no_remainder(irn);
+ break;
+ case iro_Block:
+ /*
+ * Some ugliness here: Two Blocks having the same
+ * IJmp predecessor would be congruent, which of course is wrong.
+ * We fix it by never letting blocks be congruent
+ * which cannot be detected by combo either.
+ */
+ key.u.block = irn;
+ break;
+ case iro_Load:
+ key.mode = get_Load_mode(irn);
+ break;
+ case iro_Builtin:
+ key.u.intVal = get_Builtin_kind(irn);
+ break;
default:
break;
}
pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
p = get_irn_node(pred);
-
return p->part;
} /* lambda_partition */
int i;
ir_node *block = node->node;
- if (block == get_irg_start_block(current_ir_graph)) {
- /* start block is always reachable */
+ if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
+ /* start block and labelled blocks are always reachable */
node->type.tv = tarval_reachable;
return;
}
* It would be safe to compute Top IF it can be assured, that only Cmp
* nodes are inputs to Conds. We check that first.
* This is the way Frontends typically build Firm, but some optimizations
- * (cond_eval for instance) might replace them by Phib's...
+ * (jump threading for instance) might replace them by Phib's...
*/
node->type.tv = tarval_UNKNOWN;
} /* compute_Unknown */
node->type.tv = tarval_reachable;
} else if (selector->type.tv == tarval_top) {
if (tarval_UNKNOWN == tarval_top &&
- pnc == get_Cond_defaultProj(cond)) {
+ pnc == get_Cond_default_proj(cond)) {
/* a switch based of Top is always "default" */
node->type.tv = tarval_reachable;
} else {
}
} else {
long value = get_tarval_long(selector->type.tv);
- if (pnc == get_Cond_defaultProj(cond)) {
+ if (pnc == get_Cond_default_proj(cond)) {
/* default switch, have to check ALL other cases */
int i;
node->type = pred->type;
} /* compute_Confirm */
-/**
- * (Re-)compute the type for a Max.
- *
- * @param node the node
- */
-static void compute_Max(node_t *node) {
- ir_node *op = node->node;
- node_t *l = get_irn_node(get_binop_left(op));
- node_t *r = get_irn_node(get_binop_right(op));
- lattice_elem_t a = l->type;
- lattice_elem_t b = r->type;
-
- if (a.tv == tarval_top || b.tv == tarval_top) {
- node->type.tv = tarval_top;
- } else if (is_con(a) && is_con(b)) {
- /* both nodes are constants, we can probably do something */
- if (a.tv == b.tv) {
- /* this case handles SymConsts as well */
- node->type = a;
- } else {
- ir_mode *mode = get_irn_mode(op);
- tarval *tv_min = get_mode_min(mode);
-
- if (a.tv == tv_min)
- node->type = b;
- else if (b.tv == tv_min)
- node->type = a;
- else if (is_tarval(a.tv) && is_tarval(b.tv)) {
- if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
- node->type.tv = a.tv;
- else
- node->type.tv = b.tv;
- } else {
- node->type.tv = tarval_bad;
- }
- }
- } else if (r->part == l->part) {
- /* both nodes congruent, we can probably do something */
- node->type = a;
- } else {
- node->type.tv = tarval_bottom;
- }
-} /* compute_Max */
-
-/**
- * (Re-)compute the type for a Min.
- *
- * @param node the node
- */
-static void compute_Min(node_t *node) {
- ir_node *op = node->node;
- node_t *l = get_irn_node(get_binop_left(op));
- node_t *r = get_irn_node(get_binop_right(op));
- lattice_elem_t a = l->type;
- lattice_elem_t b = r->type;
-
- if (a.tv == tarval_top || b.tv == tarval_top) {
- node->type.tv = tarval_top;
- } else if (is_con(a) && is_con(b)) {
- /* both nodes are constants, we can probably do something */
- if (a.tv == b.tv) {
- /* this case handles SymConsts as well */
- node->type = a;
- } else {
- ir_mode *mode = get_irn_mode(op);
- tarval *tv_max = get_mode_max(mode);
-
- if (a.tv == tv_max)
- node->type = b;
- else if (b.tv == tv_max)
- node->type = a;
- else if (is_tarval(a.tv) && is_tarval(b.tv)) {
- if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
- node->type.tv = a.tv;
- else
- node->type.tv = b.tv;
- } else {
- node->type.tv = tarval_bad;
- }
- }
- } else if (r->part == l->part) {
- /* both nodes congruent, we can probably do something */
- node->type = a;
- } else {
- node->type.tv = tarval_bottom;
- }
-} /* compute_Min */
-
/**
* (Re-)compute the type for a given node.
*
return node;
} /* identity_Mux */
-/**
- * Calculates the Identity for Min nodes.
- */
-static node_t *identity_Min(node_t *node) {
- ir_node *op = node->node;
- node_t *a = get_irn_node(get_binop_left(op));
- node_t *b = get_irn_node(get_binop_right(op));
- ir_mode *mode = get_irn_mode(op);
- tarval *tv_max;
-
- if (a->part == b->part) {
- /* leader of multiple predecessors */
- return a;
- }
-
- /* works even with NaN */
- tv_max = get_mode_max(mode);
- if (a->type.tv == tv_max)
- return b;
- if (b->type.tv == tv_max)
- return a;
- return node;
-} /* identity_Min */
-
-/**
- * Calculates the Identity for Max nodes.
- */
-static node_t *identity_Max(node_t *node) {
- ir_node *op = node->node;
- node_t *a = get_irn_node(get_binop_left(op));
- node_t *b = get_irn_node(get_binop_right(op));
- ir_mode *mode = get_irn_mode(op);
- tarval *tv_min;
-
- if (a->part == b->part) {
- /* leader of multiple predecessors */
- return a;
- }
-
- /* works even with NaN */
- tv_min = get_mode_min(mode);
- if (a->type.tv == tv_min)
- return b;
- if (b->type.tv == tv_min)
- return a;
- return node;
-} /* identity_Max */
-
/**
* Calculates the Identity for nodes.
*/
return identity_Confirm(node);
case iro_Mux:
return identity_Mux(node);
- case iro_Min:
- return identity_Min(node);
- case iro_Max:
- return identity_Max(node);
default:
return node;
}
* Return non-zero if the control flow predecessor node pred
* is the only reachable control flow exit of its block.
*
- * @param pred the control flow exit
+ * @param pred the control flow exit
+ * @param block the destination block
*/
-static int can_exchange(ir_node *pred) {
- if (is_Start(pred))
+static int can_exchange(ir_node *pred, ir_node *block) {
+ if (is_Start(pred) || has_Block_entity(block))
return 0;
else if (is_Jmp(pred))
return 1;
/* only one predecessor combine */
ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
- if (can_exchange(pred)) {
+ if (can_exchange(pred, block)) {
ir_node *new_block = get_nodes_block(pred);
DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
/* this Phi is replaced by a constant */
tarval *tv = node->type.tv;
- ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
+ ir_node *c = new_Const(tv);
set_irn_node(c, node);
node->node = c;
/* this Block has only one live predecessor */
ir_node *pred = skip_Proj(in_X[0]);
- if (can_exchange(pred)) {
+ if (can_exchange(pred, block)) {
ir_node *new_block = get_nodes_block(pred);
DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
exchange(block, new_block);
ir_node *block = get_nodes_block(leader);
dbg_info *dbg = get_irn_dbg_info(irn);
- leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode);
+ leader = new_rd_Conv(dbg, block, leader, mode);
}
exchange(irn, leader);
} /* exchange_leader */
if (is_Cond(cond)) {
if (only_one_reachable_proj(cond)) {
- ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
+ ir_node *jmp = new_r_Jmp(block->node);
set_irn_node(jmp, node);
node->node = jmp;
DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
*/
if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
/* can be replaced by a constant */
- ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
+ ir_node *c = new_Const(tv);
set_irn_node(c, node);
node->node = c;
DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
} else if (is_entity(node->type.sym.entity_p)) {
if (! is_SymConst(irn)) {
/* can be replaced by a SymConst */
- ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
+ ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
set_irn_node(symc, node);
node->node = symc;
SET(Return);
SET(End);
SET(Call);
-
- if (op_Max != NULL)
- SET(Max);
- if (op_Min != NULL)
- SET(Min);
} /* set_compute_functions */
/**
set_irg_extblk_inconsistent(irg);
set_irg_doms_inconsistent(irg);
set_irg_loopinfo_inconsistent(irg);
+ set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
}
ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
set_value_of_func(NULL);
current_ir_graph = rem;
} /* combo */
+
+/**
+ * Wrapper for running combo() as an ir_graph pass.
+ */
+static int pass_wrapper(ir_graph *irg, void *context) {
+ (void)context;
+ combo(irg);
+ /* combo is a fix-point iteration */
+ return 0;
+} /* pass_wrapper */
+
+/* Creates an ir_graph pass for combo. */
+ir_graph_pass_t *combo_pass(const char *name, int verify, int dump) {
+ struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
+
+ pass->kind = k_ir_prog_pass;
+ pass->run_on_irg = pass_wrapper;
+ pass->context = pass;
+ pass->name = name ? name : "combo";
+ pass->verify = verify != 0;
+ pass->dump = dump != 0;
+
+ INIT_LIST_HEAD(&pass->list);
+
+ return pass;
+} /* combo_pass */