From patchwork Wed Nov 27 00:01:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1201253 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-514661-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="GftUSmHs"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="MoyvHzrm"; 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 47N1Cb1GvVz9sSW for ; Wed, 27 Nov 2019 11:01:46 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=d9hZfmQaBNWrgW5sKo+stNlFMxl7kCe4Q1iXgQ8qDq426aJzyr dNZ6ptjq3ejt6dzup5DKzBinp+cL7Y/0kQMwMIDH4RShVVY/l1n0vVSxi3TDReSu fg41GaNDVQxJWJTUSrwerYIZflkVchAW6Xi8uKyhnPgE97hJxjcYbWdP4= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=j4ahGDheNQudbETvRZFJSULHfP4=; b=GftUSmHsspo9yShsW0x3 A2guHow/Hr99FPDCiDXrNqiei0xRHADD4KneMUe74SPnv2csSAw9leB+Qs87oSjI n1Bx+adIBBhzme3dkj0zq/fJsrCB0i2sk79sdsgaMBG20bDTX3K9HI1DV9C6sDKr V+uQoLFW+mghsXtqGhGPAQk= Received: (qmail 75064 invoked by alias); 27 Nov 2019 00:01:37 -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 74964 invoked by uid 89); 27 Nov 2019 00:01:36 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.8 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=HX-HELO:sk:mail-pj, forwprop1, failure_on_line, HTo:U*law X-HELO: mail-pj1-f51.google.com Received: from mail-pj1-f51.google.com (HELO mail-pj1-f51.google.com) (209.85.216.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 27 Nov 2019 00:01:34 +0000 Received: by mail-pj1-f51.google.com with SMTP id ep1so9065630pjb.7 for ; Tue, 26 Nov 2019 16:01:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=5tahnZi6jDAyi5gXTiE+s0rc7mBWaxgRoVUkHuyT0Mg=; b=MoyvHzrmjkPzY+UKS98RKHNCQrAG1cgqta1B/v0EEfzmuhKQGwXe0lPsZl5clciwHP kNNOFij6ItDD3tE3STNK0wKrwE2vEPSv72MHUoFLKXyoNwY6vVM/sewgXabJHOZvyvTD WL0HehQaC8OBZeEHi1pjBZmejNnm9pl0G3i4sr0Oz2yTASjejSzr0ERs1LuHObtMv2j4 IZW2Pm+j626DxvqHNwZlPmsEIgd6hJuuVIXZiahGnCmFbFiq6UdLvNHrexwQKLDPOJXZ +GHUuWgNh0g2a2liFQ/4sWCmkbTMF6/bmZ6Sl/iLXFGJEy5Qz0rIi9imElyhDDmyARb4 nVPw== Received: from [192.168.0.41] (75-166-111-174.hlrn.qwest.net. [75.166.111.174]) by smtp.gmail.com with ESMTPSA id w8sm173014pfn.186.2019.11.26.16.01.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Nov 2019 16:01:30 -0800 (PST) To: gcc-patches , Jeff Law From: Martin Sebor Subject: [committed] restore a test inadvertently removed in r278621 (PR 92683)) Message-ID: <7c39fa6f-078c-e5fe-4de6-1c770fc20b58@gmail.com> Date: Tue, 26 Nov 2019 17:01:23 -0700 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 In r278621 I accidentally removed a test that prevented an optimization from taking place when it wasn't supposed to, causing test failures in Jeff's mass rebuilds of downstream packages (e.g., snapd). In r278742 I committed the attached patch, restoring the test. Martin PR tree-optimization/92683 - strncmp incorrect result with equal substrings and non-const bound gcc/testsuite/ChangeLog: PR tree-optimization/92683 * gcc.dg/strcmpopt_8.c: New test. * gcc.dg/strcmpopt_9.c: New test. gcc/ChangeLog: PR tree-optimization/92683 * gimple-fold.c (gimple_fold_builtin_string_compare): Restore a test inadvertently removed in a previous change. Rename local variable for clarity. Index: gcc/gimple-fold.c =================================================================== --- gcc/gimple-fold.c (revision 278733) +++ gcc/gimple-fold.c (working copy) @@ -2346,15 +2346,16 @@ gimple_fold_builtin_string_compare (gimple_stmt_it tree str1 = gimple_call_arg (stmt, 0); tree str2 = gimple_call_arg (stmt, 1); tree lhs = gimple_call_lhs (stmt); - tree len = NULL_TREE; + + tree bound_node = NULL_TREE; unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U; /* Handle strncmp and strncasecmp functions. */ if (gimple_call_num_args (stmt) == 3) { - len = gimple_call_arg (stmt, 2); - if (tree_fits_uhwi_p (len)) - bound = tree_to_uhwi (len); + bound_node = gimple_call_arg (stmt, 2); + if (tree_fits_uhwi_p (bound_node)) + bound = tree_to_uhwi (bound_node); } /* If the LEN parameter is zero, return zero. */ @@ -2418,6 +2419,9 @@ gimple_fold_builtin_string_compare (gimple_stmt_it case BUILT_IN_STRNCMP: case BUILT_IN_STRNCMP_EQ: { + if (bound == HOST_WIDE_INT_M1U) + break; + /* Reduce the bound to be no more than the length of the shorter of the two strings, or the sizes of the unterminated arrays. */ Index: gcc/testsuite/gcc.dg/strcmpopt_8.c =================================================================== --- gcc/testsuite/gcc.dg/strcmpopt_8.c (nonexistent) +++ gcc/testsuite/gcc.dg/strcmpopt_8.c (working copy) @@ -0,0 +1,177 @@ +/* PR tree-optimization/92683 - strncmp incorrect result with equal substrings + and nonconst bound + { dg-do compile } + { dg-options "-O1 -Wall -fdump-tree-forwprop1" } */ + +#define SIZE_MAX __SIZE_MAX__ + +#define S123 "123" +#define S1234 "1234" + +typedef __SIZE_TYPE__ size_t; + +#ifndef ident +# define ident(n) n +#endif + +extern void failure_on_line (int); + +/* Verify that the test in 'if (EQL strncmp (S, T, N))' is folded. */ +#define T(eql, s, t, n) do { \ + max = ident (n); \ + if (!(eql __builtin_strncmp (s, t, max))) \ + failure_on_line (__LINE__); \ + } while (0) + +void test_literal (void) +{ + size_t max; + + T (0 ==, S123, S1234, 0); + T (0 ==, S123, S1234, 1); + T (0 ==, S123, S1234, 2); + T (0 ==, S123, S1234, 3); + T (0 >, S123, S1234, 4); + T (0 >, S123, S1234, 5); + T (0 >, S123, S1234, SIZE_MAX - 2); + T (0 >, S123, S1234, SIZE_MAX - 1); + T (0 >, S123, S1234, SIZE_MAX); + + T (0 ==, S123 + 1, S1234, 0); + T (0 <, S123 + 1, S1234, 1); + T (0 <, S123 + 1, S1234, 2); + T (0 <, S123 + 1, S1234, 3); + T (0 <, S123 + 1, S1234, 4); + T (0 <, S123 + 1, S1234, SIZE_MAX - 2); + T (0 <, S123 + 1, S1234, SIZE_MAX - 1); + T (0 <, S123 + 1, S1234, SIZE_MAX); + + T (0 ==, S123 + 1, S1234 + 1, 0); + T (0 ==, S123 + 1, S1234 + 1, 1); + T (0 ==, S123 + 1, S1234 + 1, 2); + T (0 >, S123 + 1, S1234 + 1, 3); + T (0 >, S123 + 1, S1234 + 1, SIZE_MAX - 1); + T (0 >, S123 + 1, S1234 + 1, SIZE_MAX); + + T (0 ==, S123 + 3, S1234 + 1, 0); + T (0 >, S123 + 3, S1234 + 1, 1); + T (0 >, S123 + 3, S1234 + 1, 2); + T (0 >, S123 + 3, S1234 + 1, 3); + T (0 >, S123 + 3, S1234 + 1, SIZE_MAX - 1); + T (0 >, S123 + 3, S1234 + 1, SIZE_MAX); + + int zero = 0; + + T (zero ==, S123, S1234, 0); + T (zero ==, S123, S1234, 1); + T (zero ==, S123, S1234, 2); + T (zero ==, S123, S1234, 3); + T (zero >, S123, S1234, 4); + T (zero >, S123, S1234, 5); + T (zero >, S123, S1234, SIZE_MAX - 2); + T (zero >, S123, S1234, SIZE_MAX - 1); + T (zero >, S123, S1234, SIZE_MAX); + + T (zero ==, S123 + 1, S1234, 0); + T (zero <, S123 + 1, S1234, 1); + T (zero <, S123 + 1, S1234, 2); + T (zero <, S123 + 1, S1234, 3); + T (zero <, S123 + 1, S1234, 4); + T (zero <, S123 + 1, S1234, SIZE_MAX - 2); + T (zero <, S123 + 1, S1234, SIZE_MAX - 1); + T (zero <, S123 + 1, S1234, SIZE_MAX); + + T (zero ==, S123 + 1, S1234 + 1, 0); + T (zero ==, S123 + 1, S1234 + 1, 1); + T (zero ==, S123 + 1, S1234 + 1, 2); + T (zero >, S123 + 1, S1234 + 1, 3); + T (zero >, S123 + 1, S1234 + 1, SIZE_MAX - 1); + T (zero >, S123 + 1, S1234 + 1, SIZE_MAX); + + T (zero ==, S123 + 3, S1234 + 1, 0); + T (zero >, S123 + 3, S1234 + 1, 1); + T (zero >, S123 + 3, S1234 + 1, 2); + T (zero >, S123 + 3, S1234 + 1, 3); + T (zero >, S123 + 3, S1234 + 1, SIZE_MAX - 1); + T (zero >, S123 + 3, S1234 + 1, SIZE_MAX); +} + +const char s123[] = S123; +const char s1234[] = S1234; + +void test_cst_array (void) +{ + size_t max; + + T (0 ==, s123, s1234, 0); + T (0 ==, s123, s1234, 1); + T (0 ==, s123, s1234, 2); + T (0 ==, s123, s1234, 3); + T (0 >, s123, s1234, 4); + T (0 >, s123, s1234, 5); + T (0 >, s123, s1234, SIZE_MAX - 2); + T (0 >, s123, s1234, SIZE_MAX - 1); + T (0 >, s123, s1234, SIZE_MAX); + + T (0 ==, s123 + 1, s1234, 0); + T (0 <, s123 + 1, s1234, 1); + T (0 <, s123 + 1, s1234, 2); + T (0 <, s123 + 1, s1234, 3); + T (0 <, s123 + 1, s1234, 4); + T (0 <, s123 + 1, s1234, SIZE_MAX - 2); + T (0 <, s123 + 1, s1234, SIZE_MAX - 1); + T (0 <, s123 + 1, s1234, SIZE_MAX); + + T (0 ==, s123 + 1, s1234 + 1, 0); + T (0 ==, s123 + 1, s1234 + 1, 1); + T (0 ==, s123 + 1, s1234 + 1, 2); + T (0 >, s123 + 1, s1234 + 1, 3); + T (0 >, s123 + 1, s1234 + 1, SIZE_MAX - 1); + T (0 >, s123 + 1, s1234 + 1, SIZE_MAX); + + T (0 ==, s123 + 3, s1234 + 1, 0); + T (0 >, s123 + 3, s1234 + 1, 1); + T (0 >, s123 + 3, s1234 + 1, 2); + T (0 >, s123 + 3, s1234 + 1, 3); + T (0 >, s123 + 3, s1234 + 1, SIZE_MAX - 1); + T (0 >, s123 + 3, s1234 + 1, SIZE_MAX); + + int zero = 0; + + T (zero ==, s123, s1234, 0); + T (zero ==, s123, s1234, 1); + T (zero ==, s123, s1234, 2); + T (zero ==, s123, s1234, 3); + T (zero >, s123, s1234, 4); + T (zero >, s123, s1234, 5); + T (zero >, s123, s1234, SIZE_MAX - 2); + T (zero >, s123, s1234, SIZE_MAX - 1); + T (zero >, s123, s1234, SIZE_MAX); + + T (zero ==, s123 + 1, s1234, 0); + T (zero <, s123 + 1, s1234, 1); + T (zero <, s123 + 1, s1234, 2); + T (zero <, s123 + 1, s1234, 3); + T (zero <, s123 + 1, s1234, 4); + T (zero <, s123 + 1, s1234, SIZE_MAX - 2); + T (zero <, s123 + 1, s1234, SIZE_MAX - 1); + T (zero <, s123 + 1, s1234, SIZE_MAX); + + T (zero ==, s123 + 1, s1234 + 1, 0); + T (zero ==, s123 + 1, s1234 + 1, 1); + T (zero ==, s123 + 1, s1234 + 1, 2); + T (zero >, s123 + 1, s1234 + 1, 3); + T (zero >, s123 + 1, s1234 + 1, SIZE_MAX - 1); + T (zero >, s123 + 1, s1234 + 1, SIZE_MAX); + + T (zero ==, s123 + 3, s1234 + 1, 0); + T (zero >, s123 + 3, s1234 + 1, 1); + T (zero >, s123 + 3, s1234 + 1, 2); + T (zero >, s123 + 3, s1234 + 1, 3); + T (zero >, s123 + 3, s1234 + 1, SIZE_MAX - 1); + T (zero >, s123 + 3, s1234 + 1, SIZE_MAX); +} + +/* { dg-final { scan-tree-dump-not "strcmp" "forwprop1" } } + { dg-final { scan-tree-dump-not "strncmp" "forwprop1" } } + { dg-final { scan-tree-dump-not "failure_on_line_" "forwprop1" } } */ Index: gcc/testsuite/gcc.dg/strcmpopt_9.c =================================================================== --- gcc/testsuite/gcc.dg/strcmpopt_9.c (nonexistent) +++ gcc/testsuite/gcc.dg/strcmpopt_9.c (working copy) @@ -0,0 +1,34 @@ +/* PR tree-optimization/92683 - strncmp incorrect result with equal substrings + and nonconst bound + { dg-do run } + { dg-options "-O2 -Wall" } */ + +#include "strlenopt.h" + +#define ident(n) ident (n) + +__attribute__ ((noclone, noinline, noipa)) size_t +ident (size_t x) +{ + return x; +} + +int nfails; + +__attribute__ ((noclone, noinline, noipa)) void +failure_on_line (int line) +{ + __builtin_printf ("failure on line %i\n", line); + ++nfails; +} + +#include "strcmpopt_8.c" + +int main (void) +{ + test_literal (); + test_cst_array (); + + if (nfails) + __builtin_abort (); +}