From patchwork Wed Sep 11 11:16:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1160907 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-508831-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="tVOF6Ycm"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46SzqW2FZyz9sCJ for ; Wed, 11 Sep 2019 21:16:21 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=JpK91pE7zWJqpGfJwqyRdNn76fVm55En9p0Xa9BQeCPCNIKdqdAX3 1FvtB3a8Iqaz9PFWHMQMRfZd4swcgQDVzSkDjN2fKKHG/qQjCnYGaGG9jIDoKzQB cIrD0nBnl3A5eCHkoLY4SwgIOIgoJtB9IYxCuDCY41v+wuSD7vobys= 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:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=1BTYvrYAxbJhLQcWBBCgt//jXDw=; b=tVOF6YcmdKEFtr4CmK6R DF9Ct8v+8KhJgg2AoRvRVWS0jBURru9Lt79MC5e1qEb2GCtjTo0u6Wy07skxHgzz M4Mr/uLbCGzJlb/C4USB9WoFtMnP+5QtuIv3kTmopBonp9l2R7jCSgKXBmLmD7zo UId6hmC3DVo4IYgOSeeDfbE= Received: (qmail 3589 invoked by alias); 11 Sep 2019 11:16:13 -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 3177 invoked by uid 89); 11 Sep 2019 11:16:13 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_NUMSUBJECT, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.suse.de Received: from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 11 Sep 2019 11:16:11 +0000 Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 9AF55ADDA for ; Wed, 11 Sep 2019 11:16:09 +0000 (UTC) Date: Wed, 11 Sep 2019 13:16:09 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR90387 Message-ID: User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 The following gets rid of __builtin_constant_p earlier to avoid array-bound diagnostics on dead code. There's been enough optimization on the fully inlined bodies so we can do that at the time of array-bound diagnostics. Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk. Richard. 2019-09-11 Richard Biener PR tree-optimization/90387 * vr-values.c (vr_values::extract_range_basic): After inlining simplify non-constant __builtin_constant_p to false. * gcc.dg/Warray-bounds-44.c: New testcase. Index: gcc/vr-values.c =================================================================== --- gcc/vr-values.c (revision 275638) +++ gcc/vr-values.c (working copy) @@ -1124,15 +1124,8 @@ vr_values::extract_range_basic (value_ra switch (cfn) { case CFN_BUILT_IN_CONSTANT_P: - /* If the call is __builtin_constant_p and the argument is a - function parameter resolve it to false. This avoids bogus - array bound warnings. - ??? We could do this as early as inlining is finished. */ - arg = gimple_call_arg (stmt, 0); - if (TREE_CODE (arg) == SSA_NAME - && SSA_NAME_IS_DEFAULT_DEF (arg) - && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL - && cfun->after_inlining) + /* Resolve calls to __builtin_constant_p after inlining. */ + if (cfun->after_inlining) { vr->set_zero (type); vr->equiv_clear (); Index: gcc/testsuite/gcc.dg/Warray-bounds-44.c =================================================================== --- gcc/testsuite/gcc.dg/Warray-bounds-44.c (nonexistent) +++ gcc/testsuite/gcc.dg/Warray-bounds-44.c (working copy) @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Warray-bounds" } */ + +int foo(unsigned int state, unsigned char * p, unsigned int p_len) +{ + static char const pattern[] = "abcd"; + static unsigned const pattern_length = sizeof(pattern) - 1; + + if (p_len == 1) { + return state; + } + + if (state < pattern_length && + p_len == (pattern_length - state) && + (!__builtin_constant_p(p_len) ? + __builtin_memcmp(p, pattern + state, p_len) : + ((unsigned char*)p)[6] == ((unsigned char*)pattern + state)[6] /* { dg-bogus "array bounds" } */ + )) { + + return 4; + } + return 1; +}