From patchwork Tue Oct 8 02:42:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 1173071 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-510439-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="x1JDoR/E"; 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 46nM986LVSz9sNF for ; Tue, 8 Oct 2019 13:43:21 +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 :content-transfer-encoding; q=dns; s=default; b=elHHF9XpFSFU7+YQ Edf14OU8fy4i/c3K96NtUeXADDZCdpZpXbWVWa9Dfqtvd6C6mzJuQvBcGF6k4I7D NwSjDJERCUI4PDYUwJPJXekDEvY9xEjHjEfomu9/GWqNI95qlPbXrJKORAj5qlXh t6Csyi6S4zlKs82gSEgPWjvdINI= 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 :content-transfer-encoding; s=default; bh=qxWVQz4el8WsPw9lupGzS+ Vpjdk=; b=x1JDoR/EzZuZmkZxBEHbQNYsOnEcAzV5Qot23yLmsmS3ayX9ew/1Lv fL7xOqYSHSL8L2kNvkMEMsgQ3gLm+6EAb0Yrwhzsk/DSip0dbyJdZQYCuRqbvWB2 ynTjOmGzW/ahKO1JeXl4+jvnSVpu4MOkn8hUG496h/F4Lg5GPyzIo= Received: (qmail 97906 invoked by alias); 8 Oct 2019 02:43:09 -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 97839 invoked by uid 89); 8 Oct 2019 02:43:08 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=WIDTH, POP, H*RU:4000, HX-Spam-Relays-External:4000 X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 08 Oct 2019 02:43:07 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 7B87A116213; Mon, 7 Oct 2019 22:43:05 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id PvKdFfSdCNnl; Mon, 7 Oct 2019 22:43:05 -0400 (EDT) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id 4030711620B; Mon, 7 Oct 2019 22:43:05 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id x982goSY415097 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 7 Oct 2019 23:42:53 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Subject: use call-clobbered reg to disalign the stack Date: Mon, 07 Oct 2019 23:42:50 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 Some x86 tests of stack realignment, that disaligned the stack with pushes and pops, failed when the compiler was configured to tune for a target that preferred to accumulate outgoing arguments: the stack space is reserved before the asm push, the call sequence overwrites the saved register, and then the asm pop restores the overwritten value. Since that's a call-preserved register in 32-bit mode, it should be preserved unchanged, but isn't. Merely changing the register to a call-clobbered one would be enough, but the tests would remain fragile and prone to failure due to other optimizations, so I arranged for the compiler to be made aware of the register used for the push and the pop, so it won't use it for something else, and forced the function to use a frame pointer, so that it won't use stack pointer offsets for local variables: the offsets would likely be wrong between the asm push and pop. Tested on x86_64-linux-gnu with -m64 and -m32. Ok to install? for gcc/testsuite/ChangeLog * gcc.target/i386/20060512-1.c (sse2_test): Use a call-clobbered register variable for stack-disaligning push and pop. Require a frame pointer. * gcc.target/i386/20060512-3.c (sse2_test): Likewise. --- gcc/testsuite/gcc.target/i386/20060512-1.c | 16 +++++++++------- gcc/testsuite/gcc.target/i386/20060512-3.c | 7 ++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/gcc/testsuite/gcc.target/i386/20060512-1.c b/gcc/testsuite/gcc.target/i386/20060512-1.c index ec163a9bc51e9..fe95f6d52fa98 100644 --- a/gcc/testsuite/gcc.target/i386/20060512-1.c +++ b/gcc/testsuite/gcc.target/i386/20060512-1.c @@ -7,11 +7,11 @@ #include #ifdef __x86_64__ -# define PUSH "pushq %rsi" -# define POP "popq %rsi" +# define REG "rcx" +# define WIDTH "q" #else -# define PUSH "pushl %esi" -# define POP "popl %esi" +# define REG "ecx" +# define WIDTH "l" #endif __m128i __attribute__ ((__noinline__)) @@ -30,13 +30,15 @@ self_aligning_function (int x, int y) int g_1 = 20; int g_2 = 22; -static void +static void __attribute__ ((__optimize__ ("-fno-omit-frame-pointer"))) sse2_test (void) { int result; - asm (PUSH); /* Misalign runtime stack. */ + register int __attribute__ ((__mode__ (__word__))) reg asm (REG); + asm volatile ("push" WIDTH "\t%0" /* Disalign runtime stack. */ + : : "r" (reg) : "memory"); result = self_aligning_function (g_1, g_2); if (result != 42) abort (); - asm (POP); + asm volatile ("pop" WIDTH "\t%0" : "=r" (reg)); } diff --git a/gcc/testsuite/gcc.target/i386/20060512-3.c b/gcc/testsuite/gcc.target/i386/20060512-3.c index 3370b9ec25afb..0cebb47f6e9bc 100644 --- a/gcc/testsuite/gcc.target/i386/20060512-3.c +++ b/gcc/testsuite/gcc.target/i386/20060512-3.c @@ -23,13 +23,14 @@ self_aligning_function (int x, int y) int g_1 = 20; int g_2 = 22; -static void +static void __attribute__ ((__optimize__ ("-fno-omit-frame-pointer"))) sse2_test (void) { int result; - asm ("pushl %esi"); /* Disalign runtime stack. */ + register int reg asm ("ecx"); + asm ("pushl\t%0": : "r" (reg) : "memory"); /* Disalign runtime stack. */ result = self_aligning_function (g_1, g_2); if (result != 42) abort (); - asm ("popl %esi"); + asm ("popl\t%0" : "=r" (reg)); }