+ * Check if a given Const node is greater or equal a given size.
+ *
+ * @return no_alias if the Const is greater, may_alias else
+ */
+static ir_alias_relation check_const(ir_node *cns, int size) {
+ tarval *tv = get_Const_tarval(cns);
+ tarval *tv_size;
+
+ if (size == 0)
+ return tarval_is_null(tv) ? may_alias : no_alias;
+ tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
+ return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
+} /* check_const */
+
+/**
+ * Treat idx1 and idx2 as integer indexes and check if they differ always more than size.
+ *
+ * @return sure_alias iff idx1 == idx2
+ * no_alias iff they ALWAYS differ more than size
+ * may_alias else
+ */
+static ir_alias_relation different_index(ir_node *idx1, ir_node *idx2, int size) {
+ if (idx1 == idx2)
+ return sure_alias;
+ if (is_Const(idx1) && is_Const(idx2)) {
+ /* both are const, we can compare them */
+ tarval *tv1 = get_Const_tarval(idx1);
+ tarval *tv2 = get_Const_tarval(idx2);
+ tarval *tv, *tv_size;
+ ir_mode *m1, *m2;
+
+ if (size == 0)
+ return tv1 == tv2 ? sure_alias : no_alias;
+
+ /* arg, modes may be different */
+ m1 = get_tarval_mode(tv1);
+ m2 = get_tarval_mode(tv2);
+ if (m1 != m2) {
+ int size = get_mode_size_bits(m1) - get_mode_size_bits(m2);
+
+ if (size < 0) {
+ /* m1 is a small mode, cast up */
+ m1 = mode_is_signed(m1) ? find_signed_mode(m2) : find_unsigned_mode(m2);
+ if (m1 == NULL) {
+ /* should NOT happen, but if it does we give up here */
+ return may_alias;
+ }
+ tv1 = tarval_convert_to(tv1, m1);
+ } else if (size > 0) {
+ /* m2 is a small mode, cast up */
+ m2 = mode_is_signed(m2) ? find_signed_mode(m1) : find_unsigned_mode(m1);
+ if (m2 == NULL) {
+ /* should NOT happen, but if it does we give up here */
+ return may_alias;
+ }
+ tv2 = tarval_convert_to(tv2, m2);
+ }
+ /* here the size should be identical, check for signed */
+ if (get_mode_sign(m1) != get_mode_sign(m2)) {
+ /* find the signed */
+ if (mode_is_signed(m2)) {
+ tarval *t = tv1;
+ ir_mode *tm = m1;
+ tv1 = tv2; m1 = m2;
+ tv2 = t; m2 = tm;
+ }
+
+ /* m1 is now the signed one */
+ if (tarval_cmp(tv1, get_tarval_null(m1)) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
+ /* tv1 is signed, but >= 0, simply cast into unsigned */
+ tv1 = tarval_convert_to(tv1, m2);
+ } else {
+ tv_size = new_tarval_from_long(size, m2);
+
+ if (tarval_cmp(tv2, tv_size) & (pn_Cmp_Eq|pn_Cmp_Gt)) {
+ /* tv1 is negative and tv2 >= tv_size, so the difference is bigger than size */
+ return no_alias;
+ }
+ /* tv_size > tv2, so we can subtract without overflow */
+ tv2 = tarval_sub(tv_size, tv2);
+
+ /* tv1 is < 0, so we can negate it */
+ tv1 = tarval_neg(tv1);
+
+ /* cast it into unsigned. for two-complement it does the right thing for MIN_INT */
+ tv1 = tarval_convert_to(tv1, m2);
+
+ /* now we can compare without overflow */
+ return tarval_cmp(tv1, tv2) & (pn_Cmp_Eq|pn_Cmp_Gt) ? no_alias : may_alias;
+ }
+ }
+ }
+ if (tarval_cmp(tv1, tv2) == pn_Cmp_Gt) {
+ tarval *t = tv1;
+ tv1 = tv2;
+ tv2 = t;
+ }
+ /* tv1 is now the "smaller" one */
+ tv = tarval_sub(tv2, tv1);
+ tv_size = new_tarval_from_long(size, get_tarval_mode(tv));
+ return tarval_cmp(tv_size, tv) & (pn_Cmp_Eq|pn_Cmp_Lt) ? no_alias : may_alias;
+ }
+
+ /* Note: we rely here on the fact that normalization puts constants on the RIGHT side */
+ if (is_Add(idx1)) {
+ ir_node *l1 = get_Add_left(idx1);
+ ir_node *r1 = get_Add_right(idx1);
+
+ if (l1 == idx2) {
+ /* x + c == y */
+ if (is_Const(r1))
+ return check_const(r1, size);
+ }
+ if (is_Add(idx2)) {
+ /* both are Adds, check if they are of x + a == x + b kind */
+ ir_node *l2 = get_Add_left(idx2);
+ ir_node *r2 = get_Add_right(idx2);
+
+ if (l1 == l2)
+ return different_index(r1, r2, size);
+ else if (l1 == r2)
+ return different_index(r1, l2, size);
+ else if (r1 == r2)
+ return different_index(l1, l2, size);
+ else if (r1 == l2)
+ return different_index(l1, r2, size);
+ }
+ }
+ if (is_Add(idx2)) {
+ ir_node *l2 = get_Add_left(idx2);
+ ir_node *r2 = get_Add_right(idx2);
+
+ if (l2 == idx1) {
+ /* x + c == y */
+ if (is_Const(r2))
+ return check_const(r2, size);
+ }
+ }
+
+ if (is_Sub(idx1)) {
+ ir_node *l1 = get_Sub_left(idx1);
+ ir_node *r1 = get_Sub_right(idx1);
+
+ if (l1 == idx2) {
+ /* x - c == y */
+ if (is_Const(r1))
+ return check_const(r1, size);
+ }
+
+ if (is_Sub(idx2)) {
+ /* both are Subs, check if they are of x - a == x - b kind */
+ ir_node *l2 = get_Sub_left(idx2);
+
+ if (l1 == l2) {
+ ir_node *r2 = get_Sub_right(idx2);
+ return different_index(r1, r2, size);
+ }
+ }
+ }
+ if (is_Sub(idx2)) {
+ ir_node *l2 = get_Sub_left(idx2);
+ ir_node *r2 = get_Sub_right(idx2);
+
+ if (l2 == idx1) {
+ /* x - c == y */
+ if (is_Const(r2))
+ return check_const(r2, size);
+ }
+
+ }
+ return may_alias;
+} /* different_index */
+
+/**
+ * Two Sel addresses have the same base address, check if there offsets are different.