From patchwork Mon Feb 12 10:13:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 871970 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-473062-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="bghCKHQQ"; 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 3zg1hz3Mpzz9t2l for ; Mon, 12 Feb 2018 21:13:38 +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:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=qaWq035qU2wVrrCnPz8PnRdKYlZjOn+WhxlCQKmOfKBX9Id5Qvb0c vwoEA8+FHVXqat2KidBPcmLUPMmWCBN2IdJPVJM3QUf5wGgGQRcpCEiiIvmE2cUn +7qs3n+jfzo8EhoVc6xeUBkbhm+XfEAUUgc+dV/61dMO6pkXlLwfEY= 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:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=nc1jfqv/8ie+ozBN67FF1tYVW8Q=; b=bghCKHQQ5teg1bu0phmm hPJzRv6J+aXsdKeolngn27kGKFjsMjsB3InlaIJAeUCRfEfvkEEYG0Ss1qw4pDRL 6P1PtoJ/9p6iIfYDpOVKd1NhsZYsNJwI6mMlYUpkp6xg3/t44XrTggSlLPgmUzOD 2misrtcD/iqV4mbaOQafn28= Received: (qmail 12251 invoked by alias); 12 Feb 2018 10:13:31 -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 12240 invoked by uid 89); 12 Feb 2018 10:13:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wm0-f41.google.com Received: from mail-wm0-f41.google.com (HELO mail-wm0-f41.google.com) (74.125.82.41) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 12 Feb 2018 10:13:28 +0000 Received: by mail-wm0-f41.google.com with SMTP id i186so8527809wmi.4 for ; Mon, 12 Feb 2018 02:13:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:mail-followup-to:subject:date:message-id :user-agent:mime-version; bh=rb4urdcFOqBs0cM2LNZ3EbHhqQLhfpnljFp3KVPd6uM=; b=L50qRu41U8DYm4eOb4mCOPuk5C/b1FnUsvUoDBcj8kBz6mVni4kxh2VNb7NaGiOA6W HMl73Jpx8h2vrOI1PHBppaOUQHUJE/eIiAvsdPtWTilLt/L5F2uYHY+cAF8sqmBGBP8B ahmmwoUj4RTtobTQ76hdg07fcIud0cqkkFOKvhcKLikE6SiZzFRBXkTIRARLgaHMNBKa u6GE7pXZEjXc+FVoK4Bc8I4AMk3VuSoViQAUR3qZYgHfmuBp+wtrUUuKu9xJLvobMhyM Vj9y7VJpUpD+BZC/WG1ypEUvXV/+8CPUqlQawTV9Gu097tXWeTwAH+nHVt32wZIHCe/Q NDpQ== X-Gm-Message-State: APf1xPDUbYyjcUGdJzeVyti6Vb35T6HcIlLGfPbCK2o4xORXNCNESwbx zxnY8lsPTHD+L1IK+uZCFQ5dyOXrrhk= X-Google-Smtp-Source: AH8x224b3tx4B8u4244/C+DpnZZZTsT5mvV6+sn3KoxOKnXx7ocbx5ftiDLcXclTrGPXHo3Ze4Z/uw== X-Received: by 10.28.23.14 with SMTP id 14mr3257769wmx.5.1518430405780; Mon, 12 Feb 2018 02:13:25 -0800 (PST) Received: from localhost ([95.144.14.158]) by smtp.gmail.com with ESMTPSA id k20sm11955394wmh.20.2018.02.12.02.13.24 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 12 Feb 2018 02:13:25 -0800 (PST) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@linaro.org Subject: Add a DECL_EXPR for VLA pointer casts (PR 84305) Date: Mon, 12 Feb 2018 10:13:23 +0000 Message-ID: <87wozia530.fsf@linaro.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 This PR was about a case in which we ended up with a MULT_EXPR that was shared between an ungimplified VLA type and a pointer calculation. The SSA names used in the pointer calculation were later freed, but they were still there in the VLA, and caused an ICE when remapping the types during inlinling. The fix is to add a DECL_EXPR that forces the VLA type sizes to be gimplified too, but the tricky part is deciding where. As the comment in grokdeclarator says, we can't just add it to the statement list, since the size might only be conditionally evaluated. It might also end up being evaluated out of sequence. The patch gets around that by putting the DECL_EXPR in a BIND_EXPR and adding the BIND_EXPR to the list of things that need to be evaluated for the declarator. This means that the TYPE_NAME is used outside of its BIND_EXPR though. Is that a problem? Tested on aarch64-linux-gnu and x86_64-linux-gnu. OK to install? Richard 2018-02-11 Richard Sandiford gcc/c/ PR c/84305 * c-decl.c (grokdeclarator): Create an anonymous TYPE_DECL in PARM and TYPENAME contexts too, but attach it to a BIND_EXPR and include the BIND_EXPR in the list of things that need to be pre-evaluated. gcc/testsuite/ PR c/84305 * gcc.c-torture/compile/pr84305.c: New test. Index: gcc/c/c-decl.c =================================================================== --- gcc/c/c-decl.c 2018-02-12 10:10:10.802846717 +0000 +++ gcc/c/c-decl.c 2018-02-12 10:10:10.955833751 +0000 @@ -6479,28 +6479,53 @@ grokdeclarator (const struct c_declarato type has a name/declaration of it's own, but special attention is required if the type is anonymous. - We handle the NORMAL and FIELD contexts here by attaching an - artificial TYPE_DECL to such pointed-to type. This forces the - sizes evaluation at a safe point and ensures it is not deferred - until e.g. within a deeper conditional context. + We attach an artificial TYPE_DECL to such pointed-to type + and arrange for it to be included in a DECL_EXPR. This + forces the sizes evaluation at a safe point and ensures it + is not deferred until e.g. within a deeper conditional context. - We expect nothing to be needed here for PARM or TYPENAME. - Pushing a TYPE_DECL at this point for TYPENAME would actually - be incorrect, as we might be in the middle of an expression - with side effects on the pointed-to type size "arguments" prior - to the pointer declaration point and the fake TYPE_DECL in the - enclosing context would force the size evaluation prior to the - side effects. */ + PARM contexts have no enclosing statement list that + can hold the DECL_EXPR, so we need to use a BIND_EXPR + instead, and add it to the list of expressions that + need to be evaluated. + TYPENAME contexts do have an enclosing statement list, + but it would be incorrect to use it, as the size should + only be evaluated if the containing expression is + evaluated. We might also be in the middle of an + expression with side effects on the pointed-to type size + "arguments" prior to the pointer declaration point and + the fake TYPE_DECL in the enclosing context would force + the size evaluation prior to the side effects. We therefore + use BIND_EXPRs in TYPENAME contexts too. */ if (!TYPE_NAME (type) - && (decl_context == NORMAL || decl_context == FIELD) && variably_modified_type_p (type, NULL_TREE)) { + tree bind = NULL_TREE; + if (decl_context == TYPENAME || decl_context == PARM) + { + bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, + NULL_TREE, NULL_TREE); + TREE_SIDE_EFFECTS (bind) = 1; + BIND_EXPR_BODY (bind) = push_stmt_list (); + push_scope (); + } tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type); DECL_ARTIFICIAL (decl) = 1; pushdecl (decl); finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE); TYPE_NAME (type) = decl; + if (bind) + { + pop_scope (); + BIND_EXPR_BODY (bind) + = pop_stmt_list (BIND_EXPR_BODY (bind)); + if (*expr) + *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, + bind); + else + *expr = bind; + } } type = c_build_pointer_type (type); Index: gcc/testsuite/gcc.c-torture/compile/pr84305.c =================================================================== --- /dev/null 2018-02-10 09:05:46.714416790 +0000 +++ gcc/testsuite/gcc.c-torture/compile/pr84305.c 2018-02-12 10:10:10.958833497 +0000 @@ -0,0 +1,4 @@ +int res, a, b; +void *foo; +static void f2 (int arg) { res = ((int (*)[arg][b]) foo)[0][0][0]; } +void f1 (void) { f2 (a); }