From patchwork Thu Jul 11 17:43:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1130937 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-504943-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="VMQuzOOi"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="dKPthcn5"; 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 45l3M33SBJz9sNk for ; Fri, 12 Jul 2019 03:43:41 +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:from :subject:to:message-id:date:mime-version:content-type; q=dns; s= default; b=dI3OBO5toFl9ev1tCxmSZpsMyWqi7RUy1ppYs+5oGt4oxJZn6Zp1T 54lpTOeY5S0uopIUJzansrYqH42R3PwcORVzE7P78dOK+cNHUh+PZLJPq/3P8XiR 5e+u43exXMJspiLHVWK22htfCeqI1tsfS+tvwutFNIpqfIjYifl2qg= 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:from :subject:to:message-id:date:mime-version:content-type; s= default; bh=HPvE5N7koXLnXTe3HR2wKvDAyXQ=; b=VMQuzOOiQ0X2hLpSre45 lhfo8AwuYBW/YYrc89C4+2C7SuIqXkXbSIpN9FoD9uXmGihLY923DtYL0I4q+8mM R8x+gcrA7idwhbXDyMeqlbup2XozI7d06ukpyEp5Y+rhk5r+mAjpQfBQHI08BTqP TQK42Ai6saqWgD6hUskwrEM= Received: (qmail 92413 invoked by alias); 11 Jul 2019 17:43:32 -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 92405 invoked by uid 89); 11 Jul 2019 17:43:32 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=sk:Warray, sk:Warray-, sk:warray, sk:warray- X-HELO: mail-qk1-f176.google.com Received: from mail-qk1-f176.google.com (HELO mail-qk1-f176.google.com) (209.85.222.176) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 11 Jul 2019 17:43:22 +0000 Received: by mail-qk1-f176.google.com with SMTP id d79so4260256qke.11 for ; Thu, 11 Jul 2019 10:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:message-id:date:user-agent:mime-version :content-language; bh=DQBtn6juekozbplQ8mVag8HkYiSUAE0SIfMNfpmK03w=; b=dKPthcn5lnoAY2zsm4wm+1VKk2/1rigXiRnxLOoC42Afi8k8doJmz6TziFBKxBeDHs anxOR6f4HQV5wnKvXjtbs65M8CbFFLBxc8AQ91WVEHUqSN55uNH8aR1weJJw0mp2yXAz 9bfA5HUv9zPnvxYyBb7144snCwPk/LEYPi6z14E7NLFIhJ3xhgd8CR6mBlBldp/Obsmf 9punJr8CJA51MuqqJuGS3+5iJftPRg8C6fsdp156t0AsZMOpRU0B7OUbMKcyYA0S3HTr ZN1N4P2x5NRzHrh1KzS4wJ/KWtbnVep2iTsVbUqNExfISRcEE1YP6+poOFKHDrT3EUCO pz9g== Received: from [192.168.0.41] (174-16-116-9.hlrn.qwest.net. [174.16.116.9]) by smtp.gmail.com with ESMTPSA id f20sm2102889qkh.15.2019.07.11.10.43.19 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jul 2019 10:43:19 -0700 (PDT) From: Martin Sebor Subject: [PATCH] add --param ssa-name-def-chain-limit To: gcc-patches , Jeff Law , Richard Biener Message-ID: <976aadb5-b2d0-9799-4a42-c64b945d54c2@gmail.com> Date: Thu, 11 Jul 2019 11:43:18 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 X-IsSubscribed: yes Attached is a patch that adds a new parameter to limit the number of SSA_NAME assignments for GCC to follow in iterative or recursive algorithms. Purely as a proof of concept the patch introduces the parameter into -Warray-bounds where the warning follows POINTER_PLUS (and ASSERT_EXPR) assignments to get at the DECL the final pointer points to. With this "infrastructure" in place the parameter can start to be introduced wherever else it might be necessary. I don't know of any pathological cases where it actually is necessary (i.e., one the 512 default keeps from going off the rails) so the test I have put together for it is artificial. A better test case involving one of the known recursive algorithms would be helpful. Martin gcc/ChangeLog: * doc/invoke.texi (ssa-name-def-chain-limit): Document new --param. * params.def (PARAM_SSA_NAME_DEF_CHAIN_LIMIT): Add new --param. * tree-vrp.c (vrp_prop::check_mem_ref): Use PARAM_SSA_NAME_DEF_CHAIN_LIMIT. gcc/testsuite/ChangeLog: * gcc.dg/Warray-bounds-43.c: New test. Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 273357) +++ gcc/doc/invoke.texi (working copy) @@ -12225,6 +12225,13 @@ before the loop versioning pass considers it too b discounting any instructions in inner loops that directly benefit from versioning. +@item ssa-name-def-chain-limit +The maximum number of SSA_NAME assignments to follow in determining +a property of a variable such as its value. This limits the number +of iterations or recursive calls GCC performs when optimizing certain +statements or when determining their validity prior to issuing +diagnostics. + @end table @end table Index: gcc/params.def =================================================================== --- gcc/params.def (revision 273357) +++ gcc/params.def (working copy) @@ -1437,6 +1437,12 @@ DEFPARAM(PARAM_HASH_TABLE_VERIFICATION_LIMIT, "each searched element.", 10, 0, 0) +DEFPARAM(PARAM_SSA_NAME_DEF_CHAIN_LIMIT, + "ssa-name-def-chain-limit", + "The maximum number of SSA_NAME assignments to follow in determining " + "a value.", + 512, 0, 0) + /* Local variables: Index: gcc/tree-vrp.c =================================================================== --- gcc/tree-vrp.c (revision 273357) +++ gcc/tree-vrp.c (working copy) @@ -4492,7 +4492,8 @@ vrp_prop::check_mem_ref (location_t location, tree The loop computes the range of the final offset for expressions such as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs in some range. */ - while (TREE_CODE (arg) == SSA_NAME) + const unsigned limit = PARAM_VALUE (PARAM_SSA_NAME_DEF_CHAIN_LIMIT); + for (unsigned n = 0; TREE_CODE (arg) == SSA_NAME && n < limit; ++n) { gimple *def = SSA_NAME_DEF_STMT (arg); if (!is_gimple_assign (def)) Index: gcc/testsuite/gcc.dg/Warray-bounds-43.c =================================================================== --- gcc/testsuite/gcc.dg/Warray-bounds-43.c (nonexistent) +++ gcc/testsuite/gcc.dg/Warray-bounds-43.c (working copy) @@ -0,0 +1,133 @@ +/* Test to verify that --param ssa_name_def_chain_limit can be used to + limit the maximum number of SSA_NAME assignments the warning follows. + { dg-do compile } + { dg-options "-O2 -Wall --param ssa-name-def-chain-limit=4" } */ + +#define NOIPA __attribute__ ((noipa)) + +const char a0[] = ""; +const char a1[] = "1"; +const char a2[] = "12"; +const char a3[] = "123"; +const char a4[] = "1234"; +const char a5[] = "12345"; +const char a6[] = "123456"; +const char a7[] = "1234567"; +const char a8[] = "12345678"; +const char a9[] = "123456789"; + +void f (const char*, ...); + +int i0, i1, i2, i3, i4, i5, i6, i7, i8; + +NOIPA int g2 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + + f (p0, p1, p2); + + return p2[8]; // { dg-warning "\\\[-Warray-bounds]" } +} + +NOIPA int g3 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + const char *p3 = p2 + i; + + f (p0, p1, p2, p3); + + return p3[7]; // { dg-warning "\\\[-Warray-bounds]" } +} + +NOIPA int g4 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + const char *p3 = p2 + i; + const char *p4 = p3 + i; + + f (p0, p1, p2, p3, p4); + + return p4[6]; // { dg-warning "\\\[-Warray-bounds]" } +} + +NOIPA int g5 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + const char *p3 = p2 + i; + const char *p4 = p3 + i; + const char *p5 = p4 + i; + + f (p0, p1, p2, p3, p4, p5); + + return p5[5]; +} + +NOIPA int g6 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + const char *p3 = p2 + i; + const char *p4 = p3 + i; + const char *p5 = p4 + i; + const char *p6 = p5 + i; + + f (p0, p1, p2, p3, p4, p5, p6); + + return p6[4]; +} + +NOIPA int g7 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + const char *p3 = p2 + i; + const char *p4 = p3 + i; + const char *p5 = p4 + i; + const char *p6 = p5 + i; + const char *p7 = p6 + i; + + f (p0, p1, p2, p3, p4, p5, p6, p7); + + return p7[3]; +} + +NOIPA int g8 (int i) +{ + if (i < 1) i = 1; + + const char *p0 = a9; + const char *p1 = p0 + i; + const char *p2 = p1 + i; + const char *p3 = p2 + i; + const char *p4 = p3 + i; + const char *p5 = p4 + i; + const char *p6 = p5 + i; + const char *p7 = p6 + i; + const char *p8 = p7 + i; + + f (p0, p1, p2, p3, p4, p5, p6, p7, p8); + + return p8[2]; +}