break;
biased_p |= for_biased;
- if (TYPE_UNSIGNED (type) || biased_p)
+
+ /* The type should be an unsigned type if the original type is unsigned
+ or if the lower bound is constant and non-negative or if the type is
+ biased, see E_Signed_Integer_Subtype case of gnat_to_gnu_entity. */
+ if (TYPE_UNSIGNED (type)
+ || (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
+ && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
+ || biased_p)
new_type = make_unsigned_type (size);
else
new_type = make_signed_type (size);
/* If the result is an integral type whose precision is not equal to its
size, sign- or zero-extend the result. We need not do this if the input
is an integral type of the same precision and signedness or if the output
- is a biased type or if both the input and output are unsigned. */
+ is a biased type or if both the input and output are unsigned, or if the
+ lower bound is constant and non-negative, see E_Signed_Integer_Subtype
+ case of gnat_to_gnu_entity. */
if (!notrunc_p
&& INTEGRAL_TYPE_P (type)
&& TYPE_RM_SIZE (type)
? TYPE_RM_SIZE (etype)
: TYPE_SIZE (etype)) == 0)
&& !(code == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type))
- && !(TYPE_UNSIGNED (type) && TYPE_UNSIGNED (etype)))
+ && !((TYPE_UNSIGNED (type)
+ || (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
+ && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0))
+ && TYPE_UNSIGNED (etype)))
{
tree base_type
= gnat_type_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
val2 = fold_convert (TREE_TYPE (val1), val2);
STRIP_USELESS_TYPE_CONVERSION (val2);
- if ((TREE_CODE (val1) == SSA_NAME
- || (TREE_CODE (val1) == NEGATE_EXPR
- && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
- || TREE_CODE (val1) == PLUS_EXPR
- || TREE_CODE (val1) == MINUS_EXPR)
- && (TREE_CODE (val2) == SSA_NAME
- || (TREE_CODE (val2) == NEGATE_EXPR
- && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
- || TREE_CODE (val2) == PLUS_EXPR
- || TREE_CODE (val2) == MINUS_EXPR))
- {
- tree n1, c1, n2, c2;
- enum tree_code code1, code2;
-
- /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
- return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
- same name, return -2. */
- if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
- {
- code1 = SSA_NAME;
- n1 = val1;
- c1 = NULL_TREE;
- }
- else
- {
- code1 = TREE_CODE (val1);
- n1 = TREE_OPERAND (val1, 0);
- c1 = TREE_OPERAND (val1, 1);
- if (tree_int_cst_sgn (c1) == -1)
- {
- if (is_negative_overflow_infinity (c1))
- return -2;
- c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
- if (!c1)
- return -2;
- code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
- }
- }
-
- if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
- {
- code2 = SSA_NAME;
- n2 = val2;
- c2 = NULL_TREE;
- }
- else
- {
- code2 = TREE_CODE (val2);
- n2 = TREE_OPERAND (val2, 0);
- c2 = TREE_OPERAND (val2, 1);
- if (tree_int_cst_sgn (c2) == -1)
- {
- if (is_negative_overflow_infinity (c2))
- return -2;
- c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
- if (!c2)
- return -2;
- code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
- }
- }
-
- /* Both values must use the same name. */
- if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
- {
- n1 = TREE_OPERAND (n1, 0);
- n2 = TREE_OPERAND (n2, 0);
- }
- if (n1 != n2)
+ const bool overflow_undefined
+ = INTEGRAL_TYPE_P (TREE_TYPE (val1))
+ && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
+ tree inv1, inv2;
+ bool neg1, neg2;
+ tree sym1 = get_single_symbol (val1, &neg1, &inv1);
+ tree sym2 = get_single_symbol (val2, &neg2, &inv2);
+
+ /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
+ accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
+ if (sym1 && sym2)
+ {
+ /* Both values must use the same name with the same sign. */
+ if (sym1 != sym2 || neg1 != neg2)
return -2;
- if (code1 == SSA_NAME && code2 == SSA_NAME)
- /* NAME == NAME */
+ /* [-]NAME + CST == [-]NAME + CST. */
+ if (inv1 == inv2)
return 0;
/* If overflow is defined we cannot simplify more. */
- if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
+ if (!overflow_undefined)
return -2;
if (strict_overflow_p != NULL
- && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
- && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
+ && (!inv1 || !TREE_NO_WARNING (val1))
+ && (!inv2 || !TREE_NO_WARNING (val2)))
*strict_overflow_p = true;
- if (code1 == SSA_NAME)
- {
- if (code2 == PLUS_EXPR)
- /* NAME < NAME + CST */
- return -1;
- else if (code2 == MINUS_EXPR)
- /* NAME > NAME - CST */
- return 1;
- }
- else if (code1 == PLUS_EXPR)
- {
- if (code2 == SSA_NAME)
- /* NAME + CST > NAME */
- return 1;
- else if (code2 == PLUS_EXPR)
- /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
- return compare_values_warnv (c1, c2, strict_overflow_p);
- else if (code2 == MINUS_EXPR)
- /* NAME + CST1 > NAME - CST2 */
- return 1;
- }
- else if (code1 == MINUS_EXPR)
+ if (!inv1)
+ inv1 = build_int_cst (TREE_TYPE (val1), 0);
+ if (!inv2)
+ inv2 = build_int_cst (TREE_TYPE (val2), 0);
+
+ return compare_values_warnv (inv1, inv2, strict_overflow_p);
+ }
+
+ const bool cst1 = is_gimple_min_invariant (val1);
+ const bool cst2 = is_gimple_min_invariant (val2);
+
+ /* If one is of the form '[-]NAME + CST' and the other is constant, then
+ it might be possible to say something depending on the constants. */
+ if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
+ {
+ if (!overflow_undefined)
+ return -2;
+
+ if (strict_overflow_p != NULL
+ && (!sym1 || !TREE_NO_WARNING (val1))
+ && (!sym2 || !TREE_NO_WARNING (val2)))
+ *strict_overflow_p = true;
+
+ const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
+ tree cst = cst1 ? val1 : val2;
+ tree inv = cst1 ? inv2 : inv1;
+
+ /* Compute the difference between the constants. If it overflows or
+ underflows, this means that we can trivially compare the NAME with
+ it and, consequently, the two values with each other. */
+ wide_int diff = wi::sub (cst, inv);
+ if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
{
- if (code2 == SSA_NAME)
- /* NAME - CST < NAME */
- return -1;
- else if (code2 == PLUS_EXPR)
- /* NAME - CST1 < NAME + CST2 */
- return -1;
- else if (code2 == MINUS_EXPR)
- /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
- C1 and C2 are swapped in the call to compare_values. */
- return compare_values_warnv (c2, c1, strict_overflow_p);
+ const int res = wi::cmp (cst, inv, sgn);
+ return cst1 ? res : -res;
}
- gcc_unreachable ();
+ return -2;
}
- /* We cannot compare non-constants. */
- if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
+ /* We cannot say anything more for non-constants. */
+ if (!cst1 || !cst2)
return -2;
if (!POINTER_TYPE_P (TREE_TYPE (val1)))