+/* Returns the invariant definition for USE, or NULL if USE is not
+ invariant. */
+
+static struct invariant *
+invariant_for_use (struct df_ref *use)
+{
+ struct df_link *defs;
+ struct df_ref *def;
+ basic_block bb = BLOCK_FOR_INSN (use->insn), def_bb;
+
+ if (use->flags & DF_REF_READ_WRITE)
+ return NULL;
+
+ defs = DF_REF_CHAIN (use);
+ if (!defs || defs->next)
+ return NULL;
+ def = defs->ref;
+ check_invariant_table_size ();
+ if (!invariant_table[DF_REF_ID(def)])
+ return NULL;
+
+ def_bb = DF_REF_BB (def);
+ if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
+ return NULL;
+ return invariant_table[DF_REF_ID(def)];
+}
+
+/* Computes hash value for invariant expression X in INSN. */
+
+static hashval_t
+hash_invariant_expr_1 (rtx insn, rtx x)
+{
+ enum rtx_code code = GET_CODE (x);
+ int i, j;
+ const char *fmt;
+ hashval_t val = code;
+ int do_not_record_p;
+ struct df_ref *use;
+ struct invariant *inv;
+
+ switch (code)
+ {
+ case CONST_INT:
+ case CONST_DOUBLE:
+ case CONST_FIXED:
+ case SYMBOL_REF:
+ case CONST:
+ case LABEL_REF:
+ return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
+
+ case REG:
+ use = df_find_use (insn, x);
+ if (!use)
+ return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
+ inv = invariant_for_use (use);
+ if (!inv)
+ return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
+
+ gcc_assert (inv->eqto != ~0u);
+ return inv->eqto;
+
+ default:
+ break;
+ }
+
+ fmt = GET_RTX_FORMAT (code);
+ for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
+ {
+ if (fmt[i] == 'e')
+ val ^= hash_invariant_expr_1 (insn, XEXP (x, i));
+ else if (fmt[i] == 'E')
+ {
+ for (j = 0; j < XVECLEN (x, i); j++)
+ val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j));
+ }
+ else if (fmt[i] == 'i' || fmt[i] == 'n')
+ val ^= XINT (x, i);
+ }
+
+ return val;
+}
+
+/* Returns true if the invariant expressions E1 and E2 used in insns INSN1
+ and INSN2 have always the same value. */
+
+static bool
+invariant_expr_equal_p (rtx insn1, rtx e1, rtx insn2, rtx e2)
+{
+ enum rtx_code code = GET_CODE (e1);
+ int i, j;
+ const char *fmt;
+ struct df_ref *use1, *use2;
+ struct invariant *inv1 = NULL, *inv2 = NULL;
+ rtx sub1, sub2;
+
+ /* If mode of only one of the operands is VOIDmode, it is not equivalent to
+ the other one. If both are VOIDmode, we rely on the caller of this
+ function to verify that their modes are the same. */
+ if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2))
+ return false;
+
+ switch (code)
+ {
+ case CONST_INT:
+ case CONST_DOUBLE:
+ case CONST_FIXED:
+ case SYMBOL_REF:
+ case CONST:
+ case LABEL_REF:
+ return rtx_equal_p (e1, e2);
+
+ case REG:
+ use1 = df_find_use (insn1, e1);
+ use2 = df_find_use (insn2, e2);
+ if (use1)
+ inv1 = invariant_for_use (use1);
+ if (use2)
+ inv2 = invariant_for_use (use2);
+
+ if (!inv1 && !inv2)
+ return rtx_equal_p (e1, e2);
+
+ if (!inv1 || !inv2)
+ return false;
+
+ gcc_assert (inv1->eqto != ~0u);
+ gcc_assert (inv2->eqto != ~0u);
+ return inv1->eqto == inv2->eqto;
+
+ default:
+ break;
+ }
+
+ fmt = GET_RTX_FORMAT (code);
+ for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
+ {
+ if (fmt[i] == 'e')
+ {
+ sub1 = XEXP (e1, i);
+ sub2 = XEXP (e2, i);
+
+ if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
+ return false;
+ }
+
+ else if (fmt[i] == 'E')
+ {
+ if (XVECLEN (e1, i) != XVECLEN (e2, i))
+ return false;
+
+ for (j = 0; j < XVECLEN (e1, i); j++)
+ {
+ sub1 = XVECEXP (e1, i, j);
+ sub2 = XVECEXP (e2, i, j);
+
+ if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
+ return false;
+ }
+ }
+ else if (fmt[i] == 'i' || fmt[i] == 'n')
+ {
+ if (XINT (e1, i) != XINT (e2, i))
+ return false;
+ }
+ /* Unhandled type of subexpression, we fail conservatively. */
+ else
+ return false;
+ }
+
+ return true;
+}
+
+/* Returns hash value for invariant expression entry E. */
+
+static hashval_t
+hash_invariant_expr (const void *e)
+{
+ const struct invariant_expr_entry *entry = e;
+
+ return entry->hash;
+}
+
+/* Compares invariant expression entries E1 and E2. */
+
+static int
+eq_invariant_expr (const void *e1, const void *e2)
+{
+ const struct invariant_expr_entry *entry1 = e1;
+ const struct invariant_expr_entry *entry2 = e2;
+
+ if (entry1->mode != entry2->mode)
+ return 0;
+
+ return invariant_expr_equal_p (entry1->inv->insn, entry1->expr,
+ entry2->inv->insn, entry2->expr);
+}
+
+/* Checks whether invariant with value EXPR in machine mode MODE is
+ recorded in EQ. If this is the case, return the invariant. Otherwise
+ insert INV to the table for this expression and return INV. */
+
+static struct invariant *
+find_or_insert_inv (htab_t eq, rtx expr, enum machine_mode mode,
+ struct invariant *inv)
+{
+ hashval_t hash = hash_invariant_expr_1 (inv->insn, expr);
+ struct invariant_expr_entry *entry;
+ struct invariant_expr_entry pentry;
+ PTR *slot;
+
+ pentry.expr = expr;
+ pentry.inv = inv;
+ pentry.mode = mode;
+ slot = htab_find_slot_with_hash (eq, &pentry, hash, INSERT);
+ entry = *slot;
+
+ if (entry)
+ return entry->inv;
+
+ entry = XNEW (struct invariant_expr_entry);
+ entry->inv = inv;
+ entry->expr = expr;
+ entry->mode = mode;
+ entry->hash = hash;
+ *slot = entry;
+
+ return inv;
+}
+
+/* Finds invariants identical to INV and records the equivalence. EQ is the
+ hash table of the invariants. */
+
+static void
+find_identical_invariants (htab_t eq, struct invariant *inv)
+{
+ unsigned depno;
+ bitmap_iterator bi;
+ struct invariant *dep;
+ rtx expr, set;
+ enum machine_mode mode;
+
+ if (inv->eqto != ~0u)
+ return;
+
+ EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
+ {
+ dep = VEC_index (invariant_p, invariants, depno);
+ find_identical_invariants (eq, dep);
+ }
+
+ set = single_set (inv->insn);
+ expr = SET_SRC (set);
+ mode = GET_MODE (expr);
+ if (mode == VOIDmode)
+ mode = GET_MODE (SET_DEST (set));
+ inv->eqto = find_or_insert_inv (eq, expr, mode, inv)->invno;
+
+ if (dump_file && inv->eqto != inv->invno)
+ fprintf (dump_file,
+ "Invariant %d is equivalent to invariant %d.\n",
+ inv->invno, inv->eqto);
+}
+
+/* Find invariants with the same value and record the equivalences. */
+
+static void
+merge_identical_invariants (void)
+{
+ unsigned i;
+ struct invariant *inv;
+ htab_t eq = htab_create (VEC_length (invariant_p, invariants),
+ hash_invariant_expr, eq_invariant_expr, free);
+
+ for (i = 0; VEC_iterate (invariant_p, invariants, i, inv); i++)
+ find_identical_invariants (eq, inv);
+
+ htab_delete (eq);
+}
+