+/* Simplify an integral conversion from an SSA name in STMT. */
+
+static bool
+simplify_conversion_using_ranges (gimple stmt)
+{
+ tree innerop, middleop, finaltype;
+ gimple def_stmt;
+ value_range_t *innervr;
+ bool inner_unsigned_p, middle_unsigned_p, final_unsigned_p;
+ unsigned inner_prec, middle_prec, final_prec;
+ double_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
+
+ finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
+ if (!INTEGRAL_TYPE_P (finaltype))
+ return false;
+ middleop = gimple_assign_rhs1 (stmt);
+ def_stmt = SSA_NAME_DEF_STMT (middleop);
+ if (!is_gimple_assign (def_stmt)
+ || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
+ return false;
+ innerop = gimple_assign_rhs1 (def_stmt);
+ if (TREE_CODE (innerop) != SSA_NAME)
+ return false;
+
+ /* Get the value-range of the inner operand. */
+ innervr = get_value_range (innerop);
+ if (innervr->type != VR_RANGE
+ || TREE_CODE (innervr->min) != INTEGER_CST
+ || TREE_CODE (innervr->max) != INTEGER_CST)
+ return false;
+
+ /* Simulate the conversion chain to check if the result is equal if
+ the middle conversion is removed. */
+ innermin = tree_to_double_int (innervr->min);
+ innermax = tree_to_double_int (innervr->max);
+
+ inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
+ middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
+ final_prec = TYPE_PRECISION (finaltype);
+
+ /* If the first conversion is not injective, the second must not
+ be widening. */
+ if (double_int_cmp (double_int_sub (innermax, innermin),
+ double_int_mask (middle_prec), true) > 0
+ && middle_prec < final_prec)
+ return false;
+ /* We also want a medium value so that we can track the effect that
+ narrowing conversions with sign change have. */
+ inner_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (innerop));
+ if (inner_unsigned_p)
+ innermed = double_int_rshift (double_int_mask (inner_prec),
+ 1, inner_prec, false);
+ else
+ innermed = double_int_zero;
+ if (double_int_cmp (innermin, innermed, inner_unsigned_p) >= 0
+ || double_int_cmp (innermed, innermax, inner_unsigned_p) >= 0)
+ innermed = innermin;
+
+ middle_unsigned_p = TYPE_UNSIGNED (TREE_TYPE (middleop));
+ middlemin = double_int_ext (innermin, middle_prec, middle_unsigned_p);
+ middlemed = double_int_ext (innermed, middle_prec, middle_unsigned_p);
+ middlemax = double_int_ext (innermax, middle_prec, middle_unsigned_p);
+
+ /* Require that the final conversion applied to both the original
+ and the intermediate range produces the same result. */
+ final_unsigned_p = TYPE_UNSIGNED (finaltype);
+ if (!double_int_equal_p (double_int_ext (middlemin,
+ final_prec, final_unsigned_p),
+ double_int_ext (innermin,
+ final_prec, final_unsigned_p))
+ || !double_int_equal_p (double_int_ext (middlemed,
+ final_prec, final_unsigned_p),
+ double_int_ext (innermed,
+ final_prec, final_unsigned_p))
+ || !double_int_equal_p (double_int_ext (middlemax,
+ final_prec, final_unsigned_p),
+ double_int_ext (innermax,
+ final_prec, final_unsigned_p)))
+ return false;
+
+ gimple_assign_set_rhs1 (stmt, innerop);
+ update_stmt (stmt);
+ return true;
+}
+
+/* Return whether the value range *VR fits in an integer type specified
+ by PRECISION and UNSIGNED_P. */
+
+static bool
+range_fits_type_p (value_range_t *vr, unsigned precision, bool unsigned_p)
+{
+ tree src_type;
+ unsigned src_precision;
+ double_int tem;
+
+ /* We can only handle integral and pointer types. */
+ src_type = TREE_TYPE (vr->min);
+ if (!INTEGRAL_TYPE_P (src_type)
+ && !POINTER_TYPE_P (src_type))
+ return false;
+
+ /* An extension is always fine, so is an identity transform. */
+ src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
+ if (src_precision < precision
+ || (src_precision == precision
+ && TYPE_UNSIGNED (src_type) == unsigned_p))
+ return true;
+
+ /* Now we can only handle ranges with constant bounds. */
+ if (vr->type != VR_RANGE
+ || TREE_CODE (vr->min) != INTEGER_CST
+ || TREE_CODE (vr->max) != INTEGER_CST)
+ return false;
+
+ /* For precision-preserving sign-changes the MSB of the double-int
+ has to be clear. */
+ if (src_precision == precision
+ && (TREE_INT_CST_HIGH (vr->min) | TREE_INT_CST_HIGH (vr->max)) < 0)
+ return false;
+
+ /* Then we can perform the conversion on both ends and compare
+ the result for equality. */
+ tem = double_int_ext (tree_to_double_int (vr->min), precision, unsigned_p);
+ if (!double_int_equal_p (tree_to_double_int (vr->min), tem))
+ return false;
+ tem = double_int_ext (tree_to_double_int (vr->max), precision, unsigned_p);
+ if (!double_int_equal_p (tree_to_double_int (vr->max), tem))
+ return false;
+
+ return true;
+}
+
+/* Simplify a conversion from integral SSA name to float in STMT. */
+
+static bool
+simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
+{
+ tree rhs1 = gimple_assign_rhs1 (stmt);
+ value_range_t *vr = get_value_range (rhs1);
+ enum machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
+ enum machine_mode mode;
+ tree tem;
+ gimple conv;
+
+ /* We can only handle constant ranges. */
+ if (vr->type != VR_RANGE
+ || TREE_CODE (vr->min) != INTEGER_CST
+ || TREE_CODE (vr->max) != INTEGER_CST)
+ return false;
+
+ /* First check if we can use a signed type in place of an unsigned. */
+ if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
+ && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
+ != CODE_FOR_nothing)
+ && range_fits_type_p (vr, GET_MODE_PRECISION
+ (TYPE_MODE (TREE_TYPE (rhs1))), 0))
+ mode = TYPE_MODE (TREE_TYPE (rhs1));
+ /* If we can do the conversion in the current input mode do nothing. */
+ else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
+ TYPE_UNSIGNED (TREE_TYPE (rhs1))))
+ return false;
+ /* Otherwise search for a mode we can use, starting from the narrowest
+ integer mode available. */
+ else
+ {
+ mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
+ do
+ {
+ /* If we cannot do a signed conversion to float from mode
+ or if the value-range does not fit in the signed type
+ try with a wider mode. */
+ if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
+ && range_fits_type_p (vr, GET_MODE_PRECISION (mode), 0))
+ break;
+
+ mode = GET_MODE_WIDER_MODE (mode);
+ /* But do not widen the input. Instead leave that to the
+ optabs expansion code. */
+ if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
+ return false;
+ }
+ while (mode != VOIDmode);
+ if (mode == VOIDmode)
+ return false;
+ }
+
+ /* It works, insert a truncation or sign-change before the
+ float conversion. */
+ tem = create_tmp_var (build_nonstandard_integer_type
+ (GET_MODE_PRECISION (mode), 0), NULL);
+ conv = gimple_build_assign_with_ops (NOP_EXPR, tem, rhs1, NULL_TREE);
+ tem = make_ssa_name (tem, conv);
+ gimple_assign_set_lhs (conv, tem);
+ gsi_insert_before (gsi, conv, GSI_SAME_STMT);
+ gimple_assign_set_rhs1 (stmt, tem);
+ update_stmt (stmt);
+
+ return true;
+}
+