From patchwork Fri Aug 19 02:28:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 74212 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp90187qga; Thu, 18 Aug 2016 19:29:07 -0700 (PDT) X-Received: by 10.66.88.131 with SMTP id bg3mr9371973pab.43.1471573747332; Thu, 18 Aug 2016 19:29:07 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id t1si5515632pav.243.2016.08.18.19.29.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Aug 2016 19:29:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-434257-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-434257-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-434257-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; q=dns; s=default; b=EJM/1ort4B1zR6J7hM 9edBoTdz3DK28HWYM1eMBn3m+0vj/6NopbQww32IiQxtUuVRm054H2qA4uOAnNgn 3aKnGd94/Xwetfslgxv20d9uP465bs4pdtuIbP9NZ9vYTZJEt+tiIX1lRPR5YnBd JzEJ0BQKCyff82mQBIVryO3bw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; s=default; bh=+XnVZb3ilipPBogZa5hruLfv giE=; b=usv6ddhSc8Nzys1PpMFuoh/beVtJQUhra5aDWTNfLpPPKxQaPSd95+n3 VNdZXFSIKHmQta8xedsY42jZW+PPLdQnPttB1PWHGMVJy6BEaQ4yJnPLHNcM9ye4 JWfKgSC+Bk0friCUOAWX8+MaYioMs/qfl+tMg2lVi0spUkps9gc= Received: (qmail 123842 invoked by alias); 19 Aug 2016 02:28:51 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 123825 invoked by uid 89); 19 Aug 2016 02:28:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=Possible X-HELO: mail-qk0-f170.google.com Received: from mail-qk0-f170.google.com (HELO mail-qk0-f170.google.com) (209.85.220.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 19 Aug 2016 02:28:38 +0000 Received: by mail-qk0-f170.google.com with SMTP id t7so34657421qkh.1 for ; Thu, 18 Aug 2016 19:28:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=cxJtDC1DzZ826mVFX3mh4bH5LwWClzfs7/0vf9iQaOA=; b=ExSXCorfQMVw45/7/O1hz6Grzh0Gm107YL6lWijnC18Oz+LsQsyxh2noNXFzfF81Uw k3H+qMZXA7PQ3Wjwhw8czz0rMlutj+BGSvgCLhyQzdTF+wYExSsk0UplgXqpiWuimy/F JZPllUfZFlOEokm2HNQpJhSXFowdYOEpKoNJXi8uDGL14B2oRqh29J4VFtOqDv1DmXEf ZX6xkCuL0tfIfnWApzO5qHVoG558+h5mEqChl5B+J6GB7WG+877dHoDCjUjxp2r5ubx0 mZHM9XbfUruqabscn47jpIUaXVKUZKP462ySideVaHeA/kpwVVFvq9YUsCcx6WyUlaDg K5HA== X-Gm-Message-State: AE9vXwMbxBi/LOn9Ns9DwCFHtNjFw5ZNbb3rEn/2g7dckyXL/4+11941+6+9O56AsWbbRNTz9+8ODRWVTKkETtki X-Received: by 10.55.182.4 with SMTP id g4mr6306819qkf.120.1471573717033; Thu, 18 Aug 2016 19:28:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.200.53.49 with HTTP; Thu, 18 Aug 2016 19:28:36 -0700 (PDT) In-Reply-To: References: From: Kugan Vivekanandarajah Date: Fri, 19 Aug 2016 12:28:36 +1000 Message-ID: Subject: Re: [TREE-SSA-CCP] Issue warning when folding condition To: "gcc-patches@gcc.gnu.org" , Richard Biener , Jeff Law X-IsSubscribed: yes On 19 August 2016 at 12:09, Kugan Vivekanandarajah wrote: > The testcase pr33738.C for warning fails with early-vrp patch. The > reason is, with early-vrp ccp2 is folding the comparison that used to > be folded in simplify_stmt_for_jump_threading. Since early-vrp does > not perform jump-threading is not optimized there. > > Attached patch adds this warning to tree-ssa-ccp.c. We might also run > into some other similar issues in the future. Sorry, I attached the wrong patch (with typo). Here is the correct one. Thanks, Kugan > > Bootstrapped and regression tested on x86_64-linux-gnu with no new regressions. > > Is this OK for trunk? > > Thanks, > Kugan > > gcc/ChangeLog: > > 2016-08-18 Kugan Vivekanandarajah > > * tree-ssa-ccp.c (ccp_fold_stmt): If the comparison is being folded > and the operand on the LHS is being compared against a constant > value that is outside of type limit, issue warning. >From 942f0e331f7a92ac46cee8793aac07af74e541e6 Mon Sep 17 00:00:00 2001 From: Kugan Vivekanandarajah 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(+) diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c index 5d5386e..fce76ce 100644 --- a/gcc/tree-ssa-ccp.c +++ b/gcc/tree-ssa-ccp.c @@ -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 (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