From patchwork Mon May 23 12:00:53 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 96936 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]) by ozlabs.org (Postfix) with SMTP id 9199DB6FAB for ; Mon, 23 May 2011 22:01:13 +1000 (EST) Received: (qmail 10786 invoked by alias); 23 May 2011 12:01:12 -0000 Received: (qmail 10758 invoked by uid 22791); 23 May 2011 12:01:09 -0000 X-SWARE-Spam-Status: No, hits=-3.3 required=5.0 tests=AWL, BAYES_00, TW_CP, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from cantor2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 23 May 2011 12:00:55 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.221.2]) by mx2.suse.de (Postfix) with ESMTP id 78E498FE69 for ; Mon, 23 May 2011 14:00:53 +0200 (CEST) Date: Mon, 23 May 2011 14:00:53 +0200 (CEST) From: Richard Guenther To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR15419 Message-ID: User-Agent: Alpine 2.00 (LNX 1167 2008-08-23) MIME-Version: 1.0 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 We're confused when void * pointers enter memcpy folding. The following fixes it. Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk. Richard. 2011-05-23 Richard Guenther PR middle-end/15419 * builtins.c (fold_builtin_memory_op): Be less restrictive about what pointer types we accept for folding. * gcc.dg/memcpy-3.c: New testcase. Index: gcc/builtins.c =================================================================== --- gcc/builtins.c (revision 174058) +++ gcc/builtins.c (working copy) @@ -8509,6 +8509,9 @@ fold_builtin_memory_op (location_t loc, Perhaps we ought to inherit type from non-VOID argument here? */ STRIP_NOPS (src); STRIP_NOPS (dest); + if (!POINTER_TYPE_P (TREE_TYPE (src)) + || !POINTER_TYPE_P (TREE_TYPE (dest))) + return NULL_TREE; /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */ if (TREE_CODE (src) == POINTER_PLUS_EXPR) { @@ -8525,8 +8528,7 @@ fold_builtin_memory_op (location_t loc, dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest); } srctype = TREE_TYPE (TREE_TYPE (src)); - if (srctype - && TREE_CODE (srctype) == ARRAY_TYPE + if (TREE_CODE (srctype) == ARRAY_TYPE && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) { srctype = TREE_TYPE (srctype); @@ -8534,21 +8536,15 @@ fold_builtin_memory_op (location_t loc, src = build1 (NOP_EXPR, build_pointer_type (srctype), src); } desttype = TREE_TYPE (TREE_TYPE (dest)); - if (desttype - && TREE_CODE (desttype) == ARRAY_TYPE + if (TREE_CODE (desttype) == ARRAY_TYPE && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) { desttype = TREE_TYPE (desttype); STRIP_NOPS (dest); dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest); } - if (!srctype || !desttype - || TREE_ADDRESSABLE (srctype) - || TREE_ADDRESSABLE (desttype) - || !TYPE_SIZE_UNIT (srctype) - || !TYPE_SIZE_UNIT (desttype) - || TREE_CODE (TYPE_SIZE_UNIT (srctype)) != INTEGER_CST - || TREE_CODE (TYPE_SIZE_UNIT (desttype)) != INTEGER_CST) + if (TREE_ADDRESSABLE (srctype) + || TREE_ADDRESSABLE (desttype)) return NULL_TREE; src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT); Index: gcc/testsuite/gcc.dg/memcpy-3.c =================================================================== --- gcc/testsuite/gcc.dg/memcpy-3.c (revision 0) +++ gcc/testsuite/gcc.dg/memcpy-3.c (revision 0) @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized" } */ + +int get_int(const void *p) +{ + int w; + __builtin_memcpy(&w, p, sizeof (int)); + return w; +} + +/* { dg-final { scan-tree-dump-not "memcpy" "optimized" } } */ +/* { dg-final { scan-tree-dump-times "MEM" 1 "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */