From patchwork Thu Dec 10 12:38:33 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 555125 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.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A0524140D7B for ; Thu, 10 Dec 2015 23:38:47 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=oUixh03l; dkim-atps=neutral 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:to:subject:mime-version:content-type; q= dns; s=default; b=yEYGiK6uWLIeoF4Ou52m74GO4HZKdHxZpYZbcpy+DseIq1 OrS/f22Rm71fEM99tXgtw4NCfZDfzyH8Nx5vLUrK8Yfl+ncIQvxIKDuscMN+3gLN CLNQW3omdGuFyQ0JENEkBSfoT7tKblBhOgITchNtINsRVlDnmpNyoVXmjER/0= 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:to:subject:mime-version:content-type; s= default; bh=ci7Z/1PK+mW7u0Im8boM9k3iRv8=; b=oUixh03lyvvJVw2Rzpgx w4gHwkv7TDov63pxiGIqjhOQeLYD+hHWSDhgZoK1ymxwtDRVdAo71FSDCCCg0F/S 1tx6a7dqn68F50piHqJWIf0b6IpYGgih1/7V63uN5iMt5sI+YsqWAtkpl7CqKd4m jTEJslYfb/zW1CtCAU2MiVY= Received: (qmail 104705 invoked by alias); 10 Dec 2015 12:38:37 -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 104670 invoked by uid 89); 10 Dec 2015 12:38:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: prv-mh.provo.novell.com Received: from prv-mh.provo.novell.com (HELO prv-mh.provo.novell.com) (137.65.248.74) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 10 Dec 2015 12:38:34 +0000 Received: from INET-PRV-MTA by prv-mh.provo.novell.com with Novell_GroupWise; Thu, 10 Dec 2015 05:38:32 -0700 Message-Id: <5669805902000078000BE192@prv-mh.provo.novell.com> Date: Thu, 10 Dec 2015 05:38:33 -0700 From: "Jan Beulich" To: Subject: [v3] avoid alignment of static variables affecting stack's Mime-Version: 1.0 Function (or more narrow) scope static variables (as well as others not placed on the stack) should also not have any effect on the stack alignment. I noticed the issue first with Linux'es dynamic_pr_debug() construct using an 8-byte aligned sub-file-scope local variable. According to my checking bad behavior started with 4.6.x (4.5.3 was still okay), but generated code got quite a bit worse as of 4.9.0. [v3: Re-base to current trunk.] [v2: Drop inclusion of hard register variables, as requested by Jakub and Richard.] gcc/ 2015-12-10 Jan Beulich * cfgexpand.c (expand_one_var): Exclude static and external variables when adjusting stack alignment related state. gcc/testsuite/ 2015-12-10 Jan Beulich * gcc.c-torture/execute/stkalign.c: New. avoid alignment of static variables affecting stack's Function (or more narrow) scope static variables (as well as others not placed on the stack) should also not have any effect on the stack alignment. I noticed the issue first with Linux'es dynamic_pr_debug() construct using an 8-byte aligned sub-file-scope local variable. According to my checking bad behavior started with 4.6.x (4.5.3 was still okay), but generated code got quite a bit worse as of 4.9.0. [v3: Re-base to current trunk.] [v2: Drop inclusion of hard register variables, as requested by Jakub and Richard.] gcc/ 2015-12-10 Jan Beulich * cfgexpand.c (expand_one_var): Exclude static and external variables when adjusting stack alignment related state. gcc/testsuite/ 2015-12-10 Jan Beulich * gcc.c-torture/execute/stkalign.c: New. --- 2015-12-09/gcc/cfgexpand.c +++ 2015-12-09/gcc/cfgexpand.c @@ -1544,12 +1544,15 @@ static HOST_WIDE_INT expand_one_var (tree var, bool toplevel, bool really_expand) { unsigned int align = BITS_PER_UNIT; + bool stack = true; tree origvar = var; var = SSAVAR (var); if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL) { + stack = !TREE_STATIC (var) && !DECL_EXTERNAL (var); + /* Because we don't know if VAR will be in register or on stack, we conservatively assume it will be on stack even if VAR is eventually put into register after RA pass. For non-automatic @@ -1578,7 +1581,8 @@ expand_one_var (tree var, bool toplevel, align = POINTER_SIZE; } - record_alignment_for_reg_var (align); + if (stack) + record_alignment_for_reg_var (align); if (TREE_CODE (origvar) == SSA_NAME) { --- 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c +++ 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c @@ -0,0 +1,26 @@ +/* { dg-options "-fno-inline" } */ + +#include + +#define ALIGNMENT 64 + +unsigned test(unsigned n, unsigned p) +{ + static struct { char __attribute__((__aligned__(ALIGNMENT))) c; } s; + unsigned x; + + assert(__alignof__(s) == ALIGNMENT); + asm ("" : "=g" (x), "+m" (s) : "0" (&x)); + + return n ? test(n - 1, x) : (x ^ p); +} + +int main (int argc, char *argv[] __attribute__((unused))) +{ + unsigned int x = test(argc, 0); + + x |= test(argc + 1, 0); + x |= test(argc + 2, 0); + + return !(x & (ALIGNMENT - 1)); +} --- 2015-12-09/gcc/cfgexpand.c +++ 2015-12-09/gcc/cfgexpand.c @@ -1544,12 +1544,15 @@ static HOST_WIDE_INT expand_one_var (tree var, bool toplevel, bool really_expand) { unsigned int align = BITS_PER_UNIT; + bool stack = true; tree origvar = var; var = SSAVAR (var); if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL) { + stack = !TREE_STATIC (var) && !DECL_EXTERNAL (var); + /* Because we don't know if VAR will be in register or on stack, we conservatively assume it will be on stack even if VAR is eventually put into register after RA pass. For non-automatic @@ -1578,7 +1581,8 @@ expand_one_var (tree var, bool toplevel, align = POINTER_SIZE; } - record_alignment_for_reg_var (align); + if (stack) + record_alignment_for_reg_var (align); if (TREE_CODE (origvar) == SSA_NAME) { --- 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c +++ 2015-12-09/gcc/testsuite/gcc.c-torture/execute/stkalign.c @@ -0,0 +1,26 @@ +/* { dg-options "-fno-inline" } */ + +#include + +#define ALIGNMENT 64 + +unsigned test(unsigned n, unsigned p) +{ + static struct { char __attribute__((__aligned__(ALIGNMENT))) c; } s; + unsigned x; + + assert(__alignof__(s) == ALIGNMENT); + asm ("" : "=g" (x), "+m" (s) : "0" (&x)); + + return n ? test(n - 1, x) : (x ^ p); +} + +int main (int argc, char *argv[] __attribute__((unused))) +{ + unsigned int x = test(argc, 0); + + x |= test(argc + 1, 0); + x |= test(argc + 2, 0); + + return !(x & (ALIGNMENT - 1)); +}