From 942f0e331f7a92ac46cee8793aac07af74e541e6 Mon Sep 17 00:00:00 2001
From: Kugan Vivekanandarajah <kugan.vivekanandarajah@linaro.org>
Date: Thu, 18 Aug 2016 20:29:38 +1000
Subject: [PATCH 7/7] Add warn_type_limits to ccp
---
gcc/tree-ssa-ccp.c | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
@@ -142,6 +142,8 @@ along with GCC; see the file COPYING3. If not see
#include "cfgloop.h"
#include "stor-layout.h"
#include "optabs-query.h"
+#include "diagnostic-core.h"
+#include "intl.h"
/* Possible lattice values. */
@@ -2147,7 +2149,11 @@ ccp_fold_stmt (gimple_stmt_iterator *gsi)
case GIMPLE_COND:
{
gcond *cond_stmt = as_a <gcond *> (stmt);
+ tree lhs = gimple_cond_lhs (stmt);
+ tree rhs = gimple_cond_rhs (stmt);
ccp_prop_value_t val;
+ wide_int min, max, rhs_val;
+ bool warn_limit = false;
/* Statement evaluation will handle type mismatches in constants
more gracefully than the final propagation. This allows us to
fold more conditionals here. */
@@ -2165,6 +2171,41 @@ ccp_fold_stmt (gimple_stmt_iterator *gsi)
fprintf (dump_file, "\n");
}
+ /* If the comparison is being folded and the operand on the LHS
+ is being compared against a constant value that is outside of
+ the natural range of LHSs type, then the predicate will
+ always fold regardless of the value of LHS. If -Wtype-limits
+ was specified, emit a warning. */
+ if (warn_type_limits
+ && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
+ && (rhs = get_constant_value (rhs))
+ && (TREE_CODE (rhs) == INTEGER_CST))
+ {
+ rhs_val = rhs;
+ min = TYPE_MIN_VALUE (TREE_TYPE (lhs));
+ max = TYPE_MAX_VALUE (TREE_TYPE (lhs));
+ warn_limit = true;
+ }
+
+ if (warn_limit
+ && (wi::cmp (rhs_val, min, TYPE_SIGN (TREE_TYPE (lhs))) == -1
+ || wi::cmp (max, rhs_val, TYPE_SIGN (TREE_TYPE (lhs))) == -1))
+ {
+ location_t location;
+
+ if (!gimple_has_location (stmt))
+ location = input_location;
+ else
+ location = gimple_location (stmt);
+
+ warning_at (location, OPT_Wtype_limits,
+ integer_zerop (val.value)
+ ? G_("comparison always false "
+ "due to limited range of data type")
+ : G_("comparison always true "
+ "due to limited range of data type"));
+ }
+
if (integer_zerop (val.value))
gimple_cond_make_false (cond_stmt);
else
--
2.7.4