From patchwork Tue Jan 6 09:28:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Bruel X-Patchwork-Id: 425582 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 E9CB51400A0 for ; Tue, 6 Jan 2015 20:29:09 +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 :message-id:date:from:mime-version:to:cc:subject:content-type; q=dns; s=default; b=GQ9JzGfulfqNQtRIGKs8kiSYkGggdwh2PrHU63xyx+Q ncc//+J48cig7sO19xtAwlLOUW0bDLNu2CInbk4NIZ9ofFcR2qyX6YU8Qsly9PjW bfqBTIODKf1kIiOc/rya0wab4M8oZPekhCMQvid4wb/Q20Zci7BhkV5ACzqsDstU = 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 :message-id:date:from:mime-version:to:cc:subject:content-type; s=default; bh=5jEXBgVcEhOcI5QtPV9jwbFxlec=; b=ZPWdMD9ZMoMJYNqmG DEXuMS16EFhHqAdp3DqjQ2xQBOGs5upDg1frJgGdLV1C73SUFxTBUa0mlilcZP5v M55lW04+JEQRvXXfTn3OvR5J2emzsl8zvXvTg6udsHRwAzhijgunacCjRrHrA1r4 qUhJiRWHqoQf8H8/QZpR1Gi62s= Received: (qmail 4514 invoked by alias); 6 Jan 2015 09:29:02 -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 4500 invoked by uid 89); 6 Jan 2015 09:29:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mx07-00178001.pphosted.com Received: from mx07-00178001.pphosted.com (HELO mx07-00178001.pphosted.com) (62.209.51.94) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Tue, 06 Jan 2015 09:29:00 +0000 Received: from pps.filterd (m0046670.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.14.5/8.14.5) with SMTP id t069PtiL015945; Tue, 6 Jan 2015 10:28:54 +0100 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 1rqrkx59dr-1 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 06 Jan 2015 10:28:54 +0100 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 53A3C3A; Tue, 6 Jan 2015 09:28:10 +0000 (GMT) Received: from Webmail-eu.st.com (safex1hubcas6.st.com [10.75.90.73]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id AB8E02C465; Tue, 6 Jan 2015 09:28:52 +0000 (GMT) Received: from [164.129.122.197] (164.129.122.197) by webmail-eu.st.com (10.75.90.13) with Microsoft SMTP Server (TLS) id 8.3.342.0; Tue, 6 Jan 2015 10:28:52 +0100 Message-ID: <54ABAAD0.4020302@st.com> Date: Tue, 6 Jan 2015 10:28:48 +0100 From: Christian Bruel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Kaz Kojima , Oleg Endo Cc: "gcc-patches@gcc.gnu.org" Subject: [PATCH][SH] Check for 0 length with inlined strnlen builtin X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.13.68, 1.0.33, 0.0.0000 definitions=2015-01-06_04:2015-01-05, 2015-01-06, 1970-01-01 signatures=0 X-IsSubscribed: yes Hello, We should not enter the first iteration when length is 0. Testcase attached. Difficult to reduce because register allocation generated accidentally the correct return value. testsuite OK OK for 4.9 and trunk ? Christian 2015-01-08 Christian Bruel PR target/64507 * config/sh/sh-mem.cc (sh_expand_cmpnstr): Check 0 length. 2015-01-08 Christian Bruel PR target/64507 * gcc.target/sh/pr64507.c: New test. Index: gcc/config/sh/sh-mem.cc =================================================================== --- gcc/config/sh/sh-mem.cc (revision 219182) +++ gcc/config/sh/sh-mem.cc (working copy) @@ -1,5 +1,5 @@ /* Helper routines for memory move and comparison insns. - Copyright (C) 2013-2014 Free Software Foundation, Inc. + Copyright (C) 2013-2015 Free Software Foundation, Inc. This file is part of GCC. @@ -421,6 +421,7 @@ /* end loop. Reached max iterations. */ if (sbytes == 0) { + emit_insn (gen_subsi3 (operands[0], tmp1, tmp2)); jump = emit_jump_insn (gen_jump_compact (L_return)); emit_barrier_after (jump); } @@ -496,6 +497,13 @@ jump = emit_jump_insn (gen_jump_compact( L_end_loop_byte)); emit_barrier_after (jump); } + else + { + emit_insn (gen_tstsi_t (len, len)); + emit_move_insn (operands[0], const0_rtx); + jump = emit_jump_insn (gen_branch_true (L_return)); + add_int_reg_note (jump, REG_BR_PROB, prob_unlikely); + } addr1 = adjust_automodify_address (addr1, QImode, s1_addr, 0); addr2 = adjust_automodify_address (addr2, QImode, s2_addr, 0); @@ -536,10 +544,10 @@ emit_insn (gen_zero_extendqisi2 (tmp2, gen_lowpart (QImode, tmp2))); emit_insn (gen_zero_extendqisi2 (tmp1, gen_lowpart (QImode, tmp1))); - emit_label (L_return); - emit_insn (gen_subsi3 (operands[0], tmp1, tmp2)); + emit_label (L_return); + return true; } Index: gcc/testsuite/gcc.target/sh/pr64507.c =================================================================== --- gcc/testsuite/gcc.target/sh/pr64507.c (revision 0) +++ gcc/testsuite/gcc.target/sh/pr64507.c (working copy) @@ -0,0 +1,25 @@ +/* Check that the __builtin_strnlen returns 0 with with + non-constant 0 length. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +extern int snprintf(char *, int, const char *, ...); +extern void abort (void); + +int main() + { + int i; + int cmp = 0; + char buffer[1024]; + const char* s = "the string"; + + snprintf(buffer, 4, "%s", s); + + for (i = 1; i < 4; i++) + cmp += __builtin_strncmp(buffer, s, i - 1); + + if (cmp) + abort(); + + return 0; +}