From patchwork Thu Oct 17 08:35:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1178363 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-511191-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Sj6uDgEL"; 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 46v2Yl1lnSz9sP3 for ; Thu, 17 Oct 2019 19:35:52 +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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=ha+euXhU1U/p4fE2YhSxj3OgQ7LFN 1sSlE7Kd+t1DkyYE3siF5HjAiX8ro93NufZKDS6tWljlcy6676XlRuqdwB5+5fLZ eEpCYboPmzSpWeicrkM6r78TnZeSkTVLofv9EsUykfKqz7NDeBFXGm6kEoNOHWp5 gE6N2sjSKxifK4= 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:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=z4RAZe2Z4AiXwr1G//zXFgvOKa0=; b=Sj6 uDgEL+OIP+HGBtOXUQnMERn9ZLxI2g0xKtC+hqGQo9CNn52Ira/Sf5hCnGvO/If0 7LwXZn262qMqrHY4ezFX4ysoDhUmIqsZwonA9sTGZHkymlS1y7KqRp2srAcm7TeQ SQLy62cOqogR/NRPit99NEoIdJnJ4GLPDc5/LelM= Received: (qmail 116777 invoked by alias); 17 Oct 2019 08:35:45 -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 116768 invoked by uid 89); 17 Oct 2019 08:35:45 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=ssa_names, SSA_NAMEs X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 17 Oct 2019 08:35:44 +0000 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ADFC8C05E740; Thu, 17 Oct 2019 08:35:42 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.36.118.135]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 20430101E81E; Thu, 17 Oct 2019 08:35:41 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id x9H8Zdwt008355; Thu, 17 Oct 2019 10:35:39 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id x9H8ZaDa008354; Thu, 17 Oct 2019 10:35:36 +0200 Date: Thu, 17 Oct 2019 10:35:36 +0200 From: Jakub Jelinek To: Richard Biener , Jeff Law , Martin Sebor , Qing Zhao Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix an omission in the recent strlen optimization (PR tree-optimization/92056) Message-ID: <20191017083536.GZ2116@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.11.3 (2019-02-01) X-IsSubscribed: yes Hi! objsz computation has two modes. One is a cheap one that doesn't handle SSA_NAMEs and is used in say random builtin folding. The other is where compute_builtin_object_size is called in between init_object_sizes () and fini_object_sizes () calls, where those set up data structures and the call then handles SSA_NAMEs and caches results for them. This second mode is what the objsz pass uses, and in some cases the strlen pass too, but in other cases it doesn't. While fini_object_sizes (); is called unconditionally at the end of strlen pass, init_object_sizes () is only called when the strlen pass calls handle_printf_call which calls get_destination_size; after that, any strcmp etc. takes advantage of that, but if no *printf is encountered, it will not. Note, init_object_sizes () can be called multiple times and does nothing the second and following time, unless fini_object_sizes () has been called. And fini_object_sizes () can be called multiple times and doesn't do anything if since the last fini_object_sizes () no init_object_sizes () has been called. So, on the following testcase without the patch, we set the value range of the first strcmp call to ~[0, 0], because we determine the buffer holding the first operand is at most 7 bytes long, but the second operand is a string literal with 7 characters + terminating NUL, but on the second call we don't, because no sprintf has been called in the function (and processed before the call). Fixed thusly, ok for trunk if it passes bootstrap/regtest? 2019-10-17 Jakub Jelinek PR tree-optimization/92056 * tree-ssa-strlen.c (determine_min_objsize): Call init_object_sizes before calling compute_builtin_object_size. * gcc.dg/tree-ssa/pr92056.c: New test. Jakub --- gcc/tree-ssa-strlen.c.jj 2019-10-17 00:18:09.851648007 +0200 +++ gcc/tree-ssa-strlen.c 2019-10-17 10:19:19.546086865 +0200 @@ -3462,6 +3462,8 @@ determine_min_objsize (tree dest) { unsigned HOST_WIDE_INT size = 0; + init_object_sizes (); + if (compute_builtin_object_size (dest, 2, &size)) return size; --- gcc/testsuite/gcc.dg/tree-ssa/pr92056.c.jj 2019-10-17 10:18:25.819907087 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/pr92056.c 2019-10-17 10:17:56.201359262 +0200 @@ -0,0 +1,36 @@ +/* PR tree-optimization/92056 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "strcmp \\(" "optimized" } } */ + +void bar (int, char *); + +int +foo (int x, char *y, const char *z) +{ + char *a; + __builtin_sprintf (y, z); + if (x == 3) + a = __builtin_malloc (5); + else if (x == 7) + a = __builtin_malloc (6); + else + a = __builtin_malloc (7); + bar (x, a); + return __builtin_strcmp (a, "abcdefg") != 0; +} + +int +baz (int x) +{ + char *a; + if (x == 3) + a = __builtin_malloc (5); + else if (x == 7) + a = __builtin_malloc (6); + else + a = __builtin_malloc (7); + bar (x, a); + return __builtin_strcmp (a, "abcdefg") != 0; +}