From patchwork Fri Oct 2 16:42:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 525647 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 B75E51402F0 for ; Sat, 3 Oct 2015 02:42:17 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=HsD7noFX; 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 :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=b/lMO2Mo3QQowbGAiJHMk7iVhbY79+uuaDUjWEY0te2lKO 5mlcIOmU2HhtDWAe6dzxFZzFhbD0p+ahnqiAwy+IQ/i3+xyBG/NbAHhToEh7q79m KFN4cuuQ3BJ5LsFEAkZUnFZp9UO2rPvTJK6ICIm4veDA/oR/KI13v4OOICzuw= 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 :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=xQNJFBg8oMYNxS+fDI/P24D9obg=; b=HsD7noFXEi/iyl/zdeVd 5+Zlb4nl5UTGofCxyvGVzL74KbD8SKF9OpyQCtceo2QGrTfdgKZqE3nZrMx1Nf26 OWIjbxpwOK1BSl6Tf+OSTxiOrG5NMmvaaTQxGZxjYs+Hveqw6bc8qzGExrG63mT1 W2kev/rvB3xT0y+nAzSMiUQ= Received: (qmail 30471 invoked by alias); 2 Oct 2015 16:42:10 -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 30462 invoked by uid 89); 2 Oct 2015 16:42:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL, BAYES_40, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 X-HELO: mail-vk0-f50.google.com Received: from mail-vk0-f50.google.com (HELO mail-vk0-f50.google.com) (209.85.213.50) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 02 Oct 2015 16:42:08 +0000 Received: by vkfp126 with SMTP id p126so63356629vkf.3 for ; Fri, 02 Oct 2015 09:42:05 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.31.34.7 with SMTP id i7mr9933543vki.60.1443804125797; Fri, 02 Oct 2015 09:42:05 -0700 (PDT) Received: by 10.103.40.68 with HTTP; Fri, 2 Oct 2015 09:42:05 -0700 (PDT) Date: Fri, 2 Oct 2015 18:42:05 +0200 Message-ID: Subject: [PATCH] Introduce ROUND_UP and ROUND_DOWN macros From: Uros Bizjak To: "gcc-patches@gcc.gnu.org" Hello! Attached patch introduces ROUND_UP and ROUND_DOWN global macros. These come handy to round and align various values, as shown by the usage in config/i386/ files. In addition to target independent code, many targets could benefit from these universal macros (e.g. arm and aarch64 can immediately replace their equivalent local definitions), so I propose to put these definitions to be available globally throughout the source. From a quick look, there are a number of places these macro can be used. And, as witnessed in attached patch, it is soooo easy to forget those "-1"s: - offset = (offset + stack_alignment_needed) & -stack_alignment_needed; + offset = ROUND_UP (offset, stack_alignment_needed); [...] - offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed; + offset = ROUND_UP (offset, stack_alignment_needed); 2015-10-02 Uros Bizjak * system.h (ROUND_UP): New macro definition. (ROUND_DOWN): Ditto. * ggc-page.c (ROUND_UP): Remove local macro definition. (PAGE_ALIGN): Implement using ROUND_UP macro. * config/i386/i386.h (PUSH_ROUNDING): Implement using ROUND_UP macro. * config/i386/i386.c (function_arg_advance_64): Use ROUND_UP macro to align values. (ix86_compute_frame_layout): Ditto. (ix86_expand_prologue): Ditto. (ix86_adjust_stack_and_probe): Use ROUND_DOWN macro to round down values. (expand_set_or_movmem_via_rep): Ditto. Patch was bootstrapped and regression tested on x86_64-linux-gnu {,-m32}. OK for mainline? Uros. Index: ggc-page.c =================================================================== --- ggc-page.c (revision 228380) +++ ggc-page.c (working copy) @@ -216,13 +216,9 @@ static const size_t extra_order_size_table[] = { #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f)) -/* Compute the smallest multiple of F that is >= X. */ - -#define ROUND_UP(x, f) (CEIL (x, f) * (f)) - /* Round X to next multiple of the page size */ -#define PAGE_ALIGN(x) (((x) + G.pagesize - 1) & ~(G.pagesize - 1)) +#define PAGE_ALIGN(x) ROUND_UP ((x), G.pagesize) /* The Ith entry is the number of objects on a page or order I. */ Index: system.h =================================================================== --- system.h (revision 228380) +++ system.h (working copy) @@ -369,6 +369,12 @@ extern int errno; /* Returns the least number N such that N * Y >= X. */ #define CEIL(x,y) (((x) + (y) - 1) / (y)) +/* This macro rounds x up to the y boundary. */ +#define ROUND_UP(x,y) (((x) + (y) - 1) & ~((y) - 1)) + +/* This macro rounds x down to the y boundary. */ +#define ROUND_DOWN(x,y) ((x) & ~((y) - 1)) + #ifdef HAVE_SYS_WAIT_H #include #endif Index: config/i386/i386.h =================================================================== --- config/i386/i386.h (revision 228380) +++ config/i386/i386.h (working copy) @@ -1596,8 +1596,7 @@ enum reg_class and -8 for 64bit targets, we need to make sure all stack pointer adjustments are in multiple of 4 for 32bit targets and 8 for 64bit targets. */ -#define PUSH_ROUNDING(BYTES) \ - (((BYTES) + UNITS_PER_WORD - 1) & -UNITS_PER_WORD) +#define PUSH_ROUNDING(BYTES) ROUND_UP (BYTES, UNITS_PER_WORD) /* If defined, the maximum amount of space required for outgoing arguments will be computed and placed into the variable `crtl->outgoing_args_size'. Index: config/i386/i386.c =================================================================== --- config/i386/i386.c (revision 228380) +++ config/i386/i386.c (working copy) @@ -8651,7 +8651,7 @@ function_arg_advance_64 (CUMULATIVE_ARGS *cum, mac else { int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD; - cum->words = (cum->words + align - 1) & ~(align - 1); + cum->words = ROUND_UP (cum->words, align); cum->words += words; return 0; } @@ -11285,7 +11285,7 @@ ix86_compute_frame_layout (struct ix86_frame *fram 16-byte aligned default stack, and thus we don't need to be within the re-aligned local stack frame to save them. */ gcc_assert (INCOMING_STACK_BOUNDARY >= 128); - offset = (offset + 16 - 1) & -16; + offset = ROUND_UP (offset, 16); offset += frame->nsseregs * 16; } frame->sse_reg_save_offset = offset; @@ -11295,7 +11295,7 @@ ix86_compute_frame_layout (struct ix86_frame *fram sure that no value happens to be the same before and after, force the alignment computation below to add a non-zero value. */ if (stack_realign_fp) - offset = (offset + stack_alignment_needed) & -stack_alignment_needed; + offset = ROUND_UP (offset, stack_alignment_needed); /* Va-arg area */ frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size; @@ -11308,7 +11308,7 @@ ix86_compute_frame_layout (struct ix86_frame *fram || !crtl->is_leaf || cfun->calls_alloca || ix86_current_function_calls_tls_descriptor) - offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed; + offset = ROUND_UP (offset, stack_alignment_needed); /* Frame pointer points here. */ frame->frame_pointer_offset = offset; @@ -11334,7 +11334,7 @@ ix86_compute_frame_layout (struct ix86_frame *fram or using alloca. */ if (!crtl->is_leaf || cfun->calls_alloca || ix86_current_function_calls_tls_descriptor) - offset = (offset + preferred_alignment - 1) & -preferred_alignment; + offset = ROUND_UP (offset, preferred_alignment); /* We've reached end of stack frame. */ frame->stack_pointer_offset = offset; @@ -12050,7 +12050,7 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT s /* Step 1: round SIZE to the previous multiple of the interval. */ - rounded_size = size & -PROBE_INTERVAL; + rounded_size = ROUND_DOWN (size, PROBE_INTERVAL); /* Step 2: compute initial and final value of the loop counter. */ @@ -12204,7 +12204,7 @@ ix86_emit_probe_stack_range (HOST_WIDE_INT first, /* Step 1: round SIZE to the previous multiple of the interval. */ - rounded_size = size & -PROBE_INTERVAL; + rounded_size = ROUND_DOWN (size, PROBE_INTERVAL); /* Step 2: compute initial and final value of the loop counter. */ @@ -12663,7 +12663,7 @@ ix86_expand_prologue (void) pointer is no longer valid. As for the value of sp_offset, see ix86_compute_frame_layout, which we need to match in order to pass verification of stack_pointer_offset at the end. */ - m->fs.sp_offset = (m->fs.sp_offset + align_bytes) & -align_bytes; + m->fs.sp_offset = ROUND_UP (m->fs.sp_offset, align_bytes); m->fs.sp_valid = false; } @@ -24692,8 +24692,8 @@ expand_set_or_movmem_via_rep (rtx destmem, rtx src destexp = gen_rtx_PLUS (Pmode, destptr, countreg); if ((!issetmem || orig_value == const0_rtx) && CONST_INT_P (count)) { - rounded_count = (INTVAL (count) - & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1)); + rounded_count + = ROUND_DOWN (INTVAL (count), (HOST_WIDE_INT) GET_MODE_SIZE (mode)); destmem = shallow_copy_rtx (destmem); set_mem_size (destmem, rounded_count); } @@ -24719,8 +24719,8 @@ expand_set_or_movmem_via_rep (rtx destmem, rtx src srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg); if (CONST_INT_P (count)) { - rounded_count = (INTVAL (count) - & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1)); + rounded_count + = ROUND_DOWN (INTVAL (count), (HOST_WIDE_INT) GET_MODE_SIZE (mode)); srcmem = shallow_copy_rtx (srcmem); set_mem_size (srcmem, rounded_count); }