From patchwork Mon Mar 25 17:00:51 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 230756 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 441682C00A5 for ; Tue, 26 Mar 2013 04:01:29 +1100 (EST) 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:subject:content-type; q= dns; s=default; b=P0xUPjOXvyNhRDC3exPfYZJK+r7ouYFA7OEqh1MbsphyZ/ xF5XX8a8bdn1L/eCDtxYF6/Kdg+WyUFZTlc8Z0o43jL+J4iAPk6BWAygQYTM7CYy 7KOQ0WpPIdO2Nc4Yn+Sg3GwJem/zTDOnYYMdxKNRvpWJ16CI4PNIw9te0be/E= 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:subject:content-type; s= default; bh=Na5k7JfcH1ygcI6gS5o7RLoddAI=; b=tprwkma807p6bg2KV3Hd PLsmz1zauvo2QLCgcq/6pg98E3R880t+8sgAN69n3f6NxfQTYuDSZcr46YmU51O/ QAqodcMNGjUZSmy8Bw5wqqJaAheXHSJXKmwkZW3ApDPB/l0RCYO4OGmkljjVC1zf pLMlWhkt/YYY89jKGAIVdgg= Received: (qmail 16858 invoked by alias); 25 Mar 2013 17:01:04 -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 16759 invoked by uid 89); 25 Mar 2013 17:00:56 -0000 Received: from cc-smtpout3.netcologne.de (HELO cc-smtpout3.netcologne.de) (89.1.8.213) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Mon, 25 Mar 2013 17:00:56 +0000 Received: from cc-smtpin3.netcologne.de (cc-smtpin3.netcologne.de [89.1.8.203]) by cc-smtpout3.netcologne.de (Postfix) with ESMTP id C2A1012574; Mon, 25 Mar 2013 18:00:53 +0100 (CET) Received: from [192.168.0.107] (xdsl-87-79-252-122.netcologne.de [87.79.252.122]) by cc-smtpin3.netcologne.de (Postfix) with ESMTPSA id 9BA4611DBD; Mon, 25 Mar 2013 18:00:52 +0100 (CET) Message-ID: <515082C3.9070602@netcologne.de> Date: Mon, 25 Mar 2013 18:00:51 +0100 From: Thomas Koenig User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130307 Thunderbird/17.0.4 MIME-Version: 1.0 To: "fortran@gcc.gnu.org" , gcc-patches Subject: [patch, fortran] Use memcmp() for string comparison for constant-length kind=1 strings X-Virus-Found: No Hello world, this patch uses memcpy() directly when comparing two kind=1 strings of equal and constant lengths. The test case modification depends on the previous patch at http://gcc.gnu.org/ml/gcc-patches/2013-03/msg00996.html for setting the string lengths for substrings. Regression-tested. No extra test case because the original test cases have to be modified to avoid failure, and test the new feature. OK for trunk after committing the patch above? 2013-03-25 Thomas Koenig * trans-expr.c (build_memcmp_call): New function. (gfc_build_compare_string): If the kind=1 strings to be compared have constant and equal lengths, use memcmp(). 2013-03-25 Thomas Koenig * gfortran.dg/character_comparison_3.f90: Adjust for use of memcmp for constant and equal string lengths. * gfortran.dg/character_comparison_5.f90: Likewise. Index: fortran/trans-expr.c =================================================================== --- fortran/trans-expr.c (Revision 196748) +++ fortran/trans-expr.c (Arbeitskopie) @@ -2655,6 +2665,32 @@ gfc_optimize_len_trim (tree len, tree str, int kin return -1; } +/* Helper to build a call to memcmp. */ + +static tree +build_memcmp_call (tree s1, tree s2, tree n) +{ + tree tmp; + + if (!POINTER_TYPE_P (TREE_TYPE (s1))) + s1 = gfc_build_addr_expr (pvoid_type_node, s1); + else + s1 = fold_convert (pvoid_type_node, s1); + + if (!POINTER_TYPE_P (TREE_TYPE (s2))) + s2 = gfc_build_addr_expr (pvoid_type_node, s2); + else + s2 = fold_convert (pvoid_type_node, s2); + + n = fold_convert (size_type_node, n); + + tmp = build_call_expr_loc (input_location, + builtin_decl_explicit (BUILT_IN_MEMCMP), + 3, s1, s2, n); + + return fold_convert (integer_type_node, tmp); +} + /* Compare two strings. If they are all single characters, the result is the subtraction of them. Otherwise, we build a library call. */ @@ -2698,7 +2734,13 @@ gfc_build_compare_string (tree len1, tree str1, tr /* Build a call for the comparison. */ if (kind == 1) - fndecl = gfor_fndecl_compare_string; + { + if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2) + && tree_int_cst_equal (len1, len2)) + return build_memcmp_call (str1, str2, len1); + else + fndecl = gfor_fndecl_compare_string; + } else if (kind == 4) fndecl = gfor_fndecl_compare_string_char4; else Index: testsuite/gfortran.dg/character_comparison_3.f90 =================================================================== --- testsuite/gfortran.dg/character_comparison_3.f90 (Revision 196748) +++ testsuite/gfortran.dg/character_comparison_3.f90 (Arbeitskopie) @@ -25,6 +25,7 @@ program main if (c(:k3) == c(:k44)) call abort end program main -! { dg-final { scan-tree-dump-times "gfortran_compare_string" 8 "original" } } +! { dg-final { scan-tree-dump-times "gfortran_compare_string" 6 "original" } } +! { dg-final { scan-tree-dump-times "__builtin_memcmp" 2 "original" } } ! { dg-final { cleanup-tree-dump "original" } } Index: testsuite/gfortran.dg/character_comparison_5.f90 =================================================================== --- testsuite/gfortran.dg/character_comparison_5.f90 (Revision 196748) +++ testsuite/gfortran.dg/character_comparison_5.f90 (Arbeitskopie) @@ -16,6 +16,6 @@ program main end program main ! { dg-final { scan-tree-dump-times "gfortran_concat_string" 0 "original" } } -! { dg-final { scan-tree-dump-times "gfortran_compare_string" 2 "original" } } +! { dg-final { scan-tree-dump-times "__builtin_memcmp" 2 "original" } } ! { dg-final { cleanup-tree-dump "original" } }