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: 127983 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp3059675ljc; Mon, 12 Feb 2018 02:13:42 -0800 (PST) X-Google-Smtp-Source: AH8x2254yiYRc9EFHAB8D3MZXEI+Wx2O9a3Y9YaUY8AfL4lvhoFzis+KxCV46Ey2jBbo0ggSlUpk X-Received: by 10.99.175.3 with SMTP id w3mr8955345pge.328.1518430422555; Mon, 12 Feb 2018 02:13:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518430422; cv=none; d=google.com; s=arc-20160816; b=xQonFYM+W8HX7HPGPi/5Vhg/7r0kq++3lqyAgm3KmhopeYIpP1krTa3S2EZvMrcmwX c2/tVYSSmY4ykiVh5pPk2n3yOEBOtDWrs5i+ge2W9+SO4gxkQlGQefKQ2mAZQEiUQUAb DhiA0JNl2501xpYJPq0TdpXYUFlPb/OAChB/NKOQwLWn6yQYTsByVg+OiXKpm16QYytu XYOYIGlT82ZahfBSs+26N1m0w069k0EbI+stkJLpPCLixyyP52kV6qVTFXkNWVbrXbVR PMSZYlHNawuOGSgW49ALSZ/JrPol21LgBTfM6s1QXgBy7OBo5x/eK8qlAnnBNZsATv1I 4Z4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:message-id:date:subject:mail-followup-to:to :from:delivered-to:sender:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=rb4urdcFOqBs0cM2LNZ3EbHhqQLhfpnljFp3KVPd6uM=; b=Ndmb2PSLl6phEQOLHThHgZdCh39WmXWy7tPwBTbxQQyjzmMPLqxlHBB2ZRtdQWc3KK aUSZQdUAHtlDUyyWiXQaK0b6VyiiCrnV1peR//NQUK7/UCBLpbKSGdrNCSy2hZ/aUwc5 zKDZsZhhc/JnR6lm9dt4H1ZzTv8GCTjfjH0UweY+4iUCN5KimgEppQNNZm+iCT4Aoqmm xxqyW3GgdS3NhKx5gL1N/dSPwfaYAz/oRQe/oVqGiee9yn6FSHKB0fPPqW/JMEdQJa2D v2cnMg3N25Lk84rlP7DaJ6ow0nJcjyZX4+o0+ILFfO4Stj7qyiSyJUBYI+d3dSeKQKSJ J05g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=srXt3b51; spf=pass (google.com: domain of gcc-patches-return-473062-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-473062-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id k10-v6si5781842pln.378.2018.02.12.02.13.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Feb 2018 02:13:42 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-473062-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=srXt3b51; spf=pass (google.com: domain of gcc-patches-return-473062-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-473062-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=lo51txVXLxNwWg+ZyaGOovDD6vizs2kRl9yTiODLQ6eNddO/sm/Nn +h5pgynB3XtHh0ZEHnqHbNhFA6yGgn1yEq4MtI2MIQgsP481HrV4Lme9p6rgEavM ZmIxzldv9PpGZTq/PMguEAa869lDTLsnsUH38+EOiSvrs6jxecysoY= 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=srXt3b51LcGJ7wP8DFS+ q/aTJIJXzIcbXVyxBg8iMFku1ZeBHnv++zSsBdL136CRinhj+OXGNiqqmhHUMJ5h vh2nWHwND0ukeOW7+51GuGLKl4I9eYTNrnm5u/JKx6LHAkzpOBoVTEuJKAJxUaag sIibJjkSlTRby03LCzRcm+A= 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-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); }