From patchwork Fri Mar 15 13:33:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1057018 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-497959-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="bFmiZE3p"; 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 44LRPf63cQz9s3q for ; Sat, 16 Mar 2019 00:34:13 +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:mime-version:content-type; q=dns; s=default; b=vfFjNeEq3Py2wYOuSWZ+u8zQYDioEP/9BloQmtTBewBgROiknS tmKxq4hAriGkLpP4mIcHq6z1g9LeIU4WtFC2lQPHmTE2xwqSaEMVO9GC/FCRZPFj V/kLOp2XOoUdFq4XGup2z2P1SgkubZ0KmbMjwnr2SWV0Xs3MAL3h8n4H8= 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:mime-version:content-type; s= default; bh=6NV9qVVJRHT/pQ1CkGd//QrWFrY=; b=bFmiZE3pcFSRsFt3toaJ OSU8XqkyPGBW7GYYj8RiSMAl7m38HkxO0H1ywpWOI6HEdHcHvjRCvW1pvDDwUifV N3XRkv9+byNtpNB1xASahDjiW1ukUV/OyIIEtoDFnjvwiZy14PrP0PhUMDoTQfa1 Eu5FidFVJFnwE7XY3GaEirI= Received: (qmail 32485 invoked by alias); 15 Mar 2019 13:34:05 -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 32468 invoked by uid 89); 15 Mar 2019 13:34:04 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, SPF_PASS autolearn=ham version=3.3.1 spammy=c-common.c, ccommonc, UD:c-common.c X-HELO: mx1.suse.de Received: from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Mar 2019 13:34:02 +0000 Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 2A944AC5A; Fri, 15 Mar 2019 13:34:00 +0000 (UTC) Date: Fri, 15 Mar 2019 14:33:59 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: jason@redhat.com, "Joseph S. Myers" Subject: [PATCH] Fix PR71598, aliasing between enums and compatible types Message-ID: User-Agent: Alpine 2.20 (LSU 67 2015-01-07) MIME-Version: 1.0 The following is an attempt to fix PR71598 where C (and C++?) have an implementation-defined compatible integer type for each enum and the TBAA rules mandate that accesses using a compatible type are allowed. The fix is applied to all C family frontends and the LTO frontend but not Fortran, Ada or other languages. Bootstrap & regtest running on x86_64-unknown-linux-gnu. I've tried to cover most cases even those with -fshort-enums. OK for trunk? It's probably a regression to some ancient GCC that didn't perform TBAA and given it's wrong-code a backport is probably mandated - do you agree? (after a while with no reported issues, of course) Thanks, Richard. 2019-03-15 Richard Biener PR c/71598 * gimple.c: Include langhooks.h. (gimple_get_alias_set): Treat enumeral types as the underlying integer type. c-family/ * c-common.c (c_common_get_alias_set): Treat enumeral types as the underlying integer type. * c-c++-common/torture/pr71598-1.c: New testcase. * c-c++-common/torture/pr71598-2.c: Likewise. Index: gcc/gimple.c =================================================================== --- gcc/gimple.c (revision 269704) +++ gcc/gimple.c (working copy) @@ -44,6 +44,7 @@ along with GCC; see the file COPYING3. #include "stringpool.h" #include "attribs.h" #include "asan.h" +#include "langhooks.h" /* All the tuples have their operand vector (if present) at the very bottom @@ -2587,6 +2588,16 @@ gimple_get_alias_set (tree t) return get_alias_set (t1); } + /* Allow aliasing between enumeral types and the underlying + integer type. This is required for C since those are + compatible types. */ + else if (TREE_CODE (t) == ENUMERAL_TYPE) + { + tree t1 = lang_hooks.types.type_for_size (tree_to_uhwi (TYPE_SIZE (t)), + false /* short-cut above */); + return get_alias_set (t1); + } + return -1; } Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 269704) +++ gcc/c-family/c-common.c (working copy) @@ -3681,6 +3681,15 @@ c_common_get_alias_set (tree t) return get_alias_set (t1); } + /* Allow aliasing between enumeral types and the underlying + integer type. This is required since those are compatible types. */ + else if (TREE_CODE (t) == ENUMERAL_TYPE) + { + tree t1 = c_common_type_for_size (tree_to_uhwi (TYPE_SIZE (t)), + false /* short-cut above */); + return get_alias_set (t1); + } + return -1; } Index: gcc/testsuite/c-c++-common/torture/pr71598-1.c =================================================================== --- gcc/testsuite/c-c++-common/torture/pr71598-1.c (nonexistent) +++ gcc/testsuite/c-c++-common/torture/pr71598-1.c (working copy) @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fno-short-enums" } */ + +enum e1 { c1 }; + +__attribute__((noinline,noclone)) +int f(enum e1 *p, unsigned *q) +{ + *p = c1; + *q = 2; + return *p; +} + +int main() +{ + unsigned x; + + if (f((enum e1 *)&x, &x) != 2) + __builtin_abort(); + return 0; +} Index: gcc/testsuite/c-c++-common/torture/pr71598-2.c =================================================================== --- gcc/testsuite/c-c++-common/torture/pr71598-2.c (nonexistent) +++ gcc/testsuite/c-c++-common/torture/pr71598-2.c (working copy) @@ -0,0 +1,47 @@ +/* { dg-do run } */ +/* { dg-additional-options "-fshort-enums" } */ + +enum e1 { c1 = -__INT_MAX__ }; + +__attribute__((noinline,noclone)) +int f(enum e1 *p, signed int *q) +{ + *p = c1; + *q = 2; + return *p; +} + +enum e2 { c2 = __SHRT_MAX__ + 1}; + +__attribute__((noinline,noclone)) +int g(enum e2 *p, unsigned short *q) +{ + *p = c2; + *q = 2; + return *p; +} + +enum e3 { c3 = __SCHAR_MAX__ }; + +__attribute__((noinline,noclone)) +int h(enum e3 *p, unsigned char *q) +{ + *p = c3; + *q = 2; + return *p; +} + +int main() +{ + signed x; + unsigned short y; + unsigned char z; + + if (f((enum e1 *)&x, &x) != 2) + __builtin_abort(); + if (g((enum e2 *)&y, &y) != 2) + __builtin_abort(); + if (h((enum e3 *)&z, &z) != 2) + __builtin_abort(); + return 0; +}