- /* skip unnecessary nodes. */
- switch (get_irn_opcode (n)) {
- case iro_Block:
- {
- /* The Block constructor does not call optimize, but mature_block
- calls the optimization. */
- assert(get_Block_matured(n));
-
- /* Straightening: a single entry Block following a single exit Block
- can be merged, if it is not the Start block. */
- /* !!! Beware, all Phi-nodes of n must have been optimized away.
- This should be true, as the block is matured before optimize is called.
- But what about Phi-cycles with the Phi0/Id that could not be resolved?
- Remaining Phi nodes are just Ids. */
- if ((get_Block_n_cfgpreds(n) == 1) &&
- (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp) &&
- (get_opt_control_flow())) {
- n = get_nodes_Block(get_Block_cfgpred(n, 0)); DBG_OPT_STG;
-
- } else if ((get_Block_n_cfgpreds(n) == 2) &&
- (get_opt_control_flow())) {
- /* Test whether Cond jumps twice to this block
- @@@ we could do this also with two loops finding two preds from several ones. */
- a = get_Block_cfgpred(n, 0);
- b = get_Block_cfgpred(n, 1);
- if ((get_irn_op(a) == op_Proj) &&
- (get_irn_op(b) == op_Proj) &&
- (get_Proj_pred(a) == get_Proj_pred(b)) &&
- (get_irn_op(get_Proj_pred(a)) == op_Cond)) {
- /* Also a single entry Block following a single exit Block. Phis have
- twice the same operand and will be optimized away. */
- n = get_nodes_Block(a); DBG_OPT_IFSIM;
- }
- } else if (get_opt_unreachable_code() &&
- (n != current_ir_graph->start_block) &&
- (n != current_ir_graph->end_block) ) {
- int i;
- /* If all inputs are dead, this block is dead too, except if it is
- the start or end block. This is a step of unreachable code
- elimination */
- for (i = 0; i < get_Block_n_cfgpreds(n); i++) {
- if (!is_Bad(get_Block_cfgpred(n, i))) break;
+ return n;
+}
+
+/**
+ * optimize operations that are commutative and have neutral 0.
+ */
+static ir_node *equivalent_node_neutral_zero(ir_node *n)
+{
+ ir_node *oldn = n;
+
+ ir_node *a = get_binop_left(n);
+ ir_node *b = get_binop_right(n);
+
+ tarval *tv;
+ ir_node *on;
+
+ /* After running compute_node there is only one constant predecessor.
+ Find this predecessors value and remember the other node: */
+ if ((tv = computed_value (a)) != tarval_bad) {
+ on = b;
+ } else if ((tv = computed_value (b)) != tarval_bad) {
+ on = a;
+ } else
+ return n;
+
+ /* If this predecessors constant value is zero, the operation is
+ unnecessary. Remove it: */
+ if (tarval_classify (tv) == TV_CLASSIFY_NULL) {
+ n = on; DBG_OPT_ALGSIM1;
+ }
+
+ return n;
+}
+
+static ir_node *equivalent_node_Add(ir_node *n)
+{
+ return equivalent_node_neutral_zero(n);
+}
+
+static ir_node *equivalent_node_Eor(ir_node *n)
+{
+ return equivalent_node_neutral_zero(n);
+}
+
+/**
+ * optimize operations that are not commutative but have neutral 0 on left.
+ * Test only one predecessor.
+ */
+static ir_node *equivalent_node_left_zero(ir_node *n)
+{
+ ir_node *oldn = n;
+
+ ir_node *a = get_binop_left(n);
+ ir_node *b = get_binop_right(n);
+
+ if (tarval_classify (computed_value (b)) == TV_CLASSIFY_NULL) {
+ n = a; DBG_OPT_ALGSIM1;
+ }
+
+ return n;
+}
+
+static ir_node *equivalent_node_Sub(ir_node *n)
+{
+ return equivalent_node_left_zero(n);
+}
+
+static ir_node *equivalent_node_Shl(ir_node *n)
+{
+ return equivalent_node_left_zero(n);
+}
+
+static ir_node *equivalent_node_Shr(ir_node *n)
+{
+ return equivalent_node_left_zero(n);
+}
+
+static ir_node *equivalent_node_Shrs(ir_node *n)
+{
+ return equivalent_node_left_zero(n);
+}
+
+static ir_node *equivalent_node_Rot(ir_node *n)
+{
+ return equivalent_node_left_zero(n);
+}
+
+static ir_node *equivalent_node_symmetric_unop(ir_node *n)
+{
+ ir_node *oldn = n;
+
+ /* optimize symmetric unop */
+ if (get_irn_op(get_unop_op(n)) == get_irn_op(n)) {
+ n = get_unop_op(get_unop_op(n)); DBG_OPT_ALGSIM2;
+ }
+ return n;
+}
+
+static ir_node *equivalent_node_Not(ir_node *n)
+{
+ /* NotNot x == x */
+ return equivalent_node_symmetric_unop(n);
+}
+
+static ir_node *equivalent_node_Minus(ir_node *n)
+{
+ /* --x == x */ /* ??? Is this possible or can --x raise an
+ out of bounds exception if min =! max? */
+ return equivalent_node_symmetric_unop(n);
+}
+
+static ir_node *equivalent_node_Mul(ir_node *n)
+{
+ ir_node *oldn = n;
+
+ ir_node *a = get_Mul_left(n);
+ ir_node *b = get_Mul_right(n);
+
+ /* Mul is commutative and has again an other neutral element. */
+ if (tarval_classify (computed_value (a)) == TV_CLASSIFY_ONE) {
+ n = b; DBG_OPT_ALGSIM1;
+ } else if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE) {
+ n = a; DBG_OPT_ALGSIM1;
+ }
+ return n;
+}
+
+static ir_node *equivalent_node_Div(ir_node *n)
+{
+ ir_node *a = get_Div_left(n);
+ ir_node *b = get_Div_right(n);
+
+ /* Div is not commutative. */
+ if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
+ /* Turn Div into a tuple (mem, bad, a) */
+ ir_node *mem = get_Div_mem(n);
+ turn_into_tuple(n, 3);
+ set_Tuple_pred(n, pn_Div_M, mem);
+ set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
+ set_Tuple_pred(n, pn_Div_res, a);
+ }
+ return n;
+}
+
+static ir_node *equivalent_node_And(ir_node *n)
+{
+ ir_node *oldn = n;
+
+ ir_node *a = get_And_left(n);
+ ir_node *b = get_And_right(n);
+
+ if (a == b) {
+ n = a; /* And has it's own neutral element */
+ } else if (tarval_classify (computed_value (a)) == TV_CLASSIFY_ALL_ONE) {
+ n = b;
+ } else if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ALL_ONE) {
+ n = a;
+ }
+ if (n != oldn) DBG_OPT_ALGSIM1;
+ return n;
+}
+
+static ir_node *equivalent_node_Conv(ir_node *n)
+{
+ ir_node *oldn = n;
+ ir_node *a = get_Conv_op(n);
+ ir_node *b;
+
+ ir_mode *n_mode = get_irn_mode(n);
+ ir_mode *a_mode = get_irn_mode(a);
+
+ if (n_mode == a_mode) { /* No Conv necessary */
+ n = a; DBG_OPT_ALGSIM3;
+ } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
+ ir_mode *b_mode;
+
+ b = get_Conv_op(a);
+ n_mode = get_irn_mode(n);
+ b_mode = get_irn_mode(b);
+
+ if (n_mode == b_mode) {
+ if (n_mode == mode_b) {
+ n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */ DBG_OPT_ALGSIM1;
+ }
+ else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
+ if (smaller_mode(b_mode, a_mode)){
+ n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */ DBG_OPT_ALGSIM1;