From patchwork Fri Nov 3 16:32:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 833990 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-465885-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="ROLX/Moc"; 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 3yT6v60Whpz9sNV for ; Sat, 4 Nov 2017 03:32:48 +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=FfaHJVmE9wEJPZYnjCXd+BRlD59fUDmf7P7cXLalKEzYL1MCknGQn KYbrjOkSAmdMSUwa8Age3PoHzaHAsyVGAz5o+JzeyM0+Yja1rFNaahRMhs1POvPH gXnwPyx5KvtbaDU+g6r7fy4zHJy0aZ+3+d2iR8raQNwLW0GcDUTl8M= 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=z0y4IJwsqSE3e+Q5b1jjlkzOHCc=; b=ROLX/Moc3NPKu9WYRv8G kV27KAyjdyBtoaxWbXLDchlTudCswRJEIRKUvNbzF9J/Np+rjHU0vNoUddPxaaYW BVNuFgnYTxryr3ozN2BiZw2hUcvZTpjyrt6GRyet6rXVoHa85b6+Wt0vXoH7kapg vyPYdpPEZWlYSF/GqkBVnQU= Received: (qmail 43006 invoked by alias); 3 Nov 2017 16:32:36 -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 42902 invoked by uid 89); 3 Nov 2017 16:32:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-15.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=PARTS, STEP, stopping X-HELO: mail-wm0-f45.google.com Received: from mail-wm0-f45.google.com (HELO mail-wm0-f45.google.com) (74.125.82.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 03 Nov 2017 16:32:34 +0000 Received: by mail-wm0-f45.google.com with SMTP id t139so2747054wmt.1 for ; Fri, 03 Nov 2017 09:32:33 -0700 (PDT) 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=rTzUfIVFG7YNolfP3xtU99YwCVwGgvI2jDw5zdiHciE=; b=cxPdjA4IAZGs3F3vaGFKh2mAHDJdBfsb1p20O/VpTfPl9ZdP+s+5RVGNqDmsFClHVt fwgQuBbp2uoIPvec7P6YfnzfbBhYWh3w59Vg64y/Ce28n4AKLFhuMbWx53aqoiXQBeru lfrcJGceajNLlkUp2NXb80Z/+QJp3tNTzcAjbpkovHtHEkwlOgbjHzNi80m2/2svHdqE SFzoLi67IXM79BvMpJt+MQdhhcd+i66Y5N8WZsP7RW91SY/eu6qX/G/dxWGgd0T8qub9 MtlcELKE53ijrNBU5kj84xPScoN28itpJZEqe0ajP5bFU6VdsT249C4NOV8hq+NK2wWT OSVQ== X-Gm-Message-State: AJaThX6WTorAj0ljiWF+ULf3jaIEpwPcrltZT1FYBxMyi60XTap+R+gC jSTB/E3zRvsesAMIqxFDe4pyVCiQQQ8= X-Google-Smtp-Source: ABhQp+T/cndegWEAlmdNIFQAGWVl4NSJn4xDbDJRUERf1utvI4KvMLHox9zw2ILJ8xBLmF6KhGGe8A== X-Received: by 10.28.18.144 with SMTP id 138mr247355wms.135.1509726751476; Fri, 03 Nov 2017 09:32:31 -0700 (PDT) Received: from localhost (188.29.164.162.threembb.co.uk. [188.29.164.162]) by smtp.gmail.com with ESMTPSA id 25sm4186230wrv.8.2017.11.03.09.32.30 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 03 Nov 2017 09:32:30 -0700 (PDT) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@linaro.org Subject: Improve canonicalisation of TARGET_MEM_REFs Date: Fri, 03 Nov 2017 16:32:29 +0000 Message-ID: <87tvybnx0i.fsf@linaro.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 A general TARGET_MEM_REF is: BASE + STEP * INDEX + INDEX2 + OFFSET After classifying the address in this way, the code that builds TARGET_MEM_REFs tries to simplify the address until it's valid for the current target and for the mode of memory being addressed. It does this in a fixed order: (1) add SYMBOL to BASE (2) add INDEX * STEP to the base, if STEP != 1 (3) add OFFSET to INDEX or BASE (reverted if unsuccessful) (4) add INDEX to BASE (5) add OFFSET to BASE So suppose we had an address: &symbol + offset + index * 8 (e.g. "a[i + 1]" for a global "a") on a target that only allows an index or an offset, not both. Following the steps above, we'd first create: tmp = symbol tmp2 = tmp + index * 8 Then if the given offset value was valid for the mode being addressed, we'd create: MEM[base:tmp2, offset:offset] while if it was invalid we'd create: tmp3 = tmp2 + offset MEM[base:tmp3, offset:0] The problem is that this could happen if ivopts had decided to use a scaled index for an address that happens to have a constant base. The old procedure failed to give an indexed TARGET_MEM_REF in that case, and adding the offset last prevented later passes from being able to fold the index back in. The patch avoids this by skipping (2) if BASE + INDEX * STEP is a legitimate address and if OFFSET is stopping the address being valid. Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64-linux-gnu. OK to install? Richard 2017-10-31 Richard Sandiford Alan Hayward David Sherwood gcc/ * tree-ssa-address.c (keep_index_p): New function. (create_mem_ref): Use it. Only split out the INDEX * STEP component if that is invalid even with the symbol and offset removed. Index: gcc/tree-ssa-address.c =================================================================== --- gcc/tree-ssa-address.c 2017-11-03 12:15:44.097060121 +0000 +++ gcc/tree-ssa-address.c 2017-11-03 12:21:18.060359821 +0000 @@ -746,6 +746,20 @@ gimplify_mem_ref_parts (gimple_stmt_iter true, GSI_SAME_STMT); } +/* Return true if the STEP in PARTS gives a valid BASE + INDEX * STEP + address for type TYPE and if the offset is making it appear invalid. */ + +static bool +keep_index_p (tree type, mem_address parts) +{ + if (!parts.base) + return false; + + gcc_assert (!parts.symbol); + parts.offset = NULL_TREE; + return valid_mem_ref_p (TYPE_MODE (type), TYPE_ADDR_SPACE (type), &parts); +} + /* Creates and returns a TARGET_MEM_REF for address ADDR. If necessary computations are emitted in front of GSI. TYPE is the mode of created memory reference. IV_CAND is the selected iv candidate in ADDR, @@ -809,7 +823,8 @@ create_mem_ref (gimple_stmt_iterator *gs into: index' = index << step; [... + index' + ,,,]. */ - if (parts.step && !integer_onep (parts.step)) + bool scaled_p = (parts.step && !integer_onep (parts.step)); + if (scaled_p && !keep_index_p (type, parts)) { gcc_assert (parts.index); parts.index = force_gimple_operand_gsi (gsi, @@ -821,6 +836,7 @@ create_mem_ref (gimple_stmt_iterator *gs mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true); if (mem_ref) return mem_ref; + scaled_p = false; } /* Add offset to invariant part by transforming address expression: @@ -832,7 +848,9 @@ create_mem_ref (gimple_stmt_iterator *gs index' = index + offset; [base + index'] depending on which one is invariant. */ - if (parts.offset && !integer_zerop (parts.offset)) + if (parts.offset + && !integer_zerop (parts.offset) + && (!var_in_base || !scaled_p)) { tree old_base = unshare_expr (parts.base); tree old_index = unshare_expr (parts.index); @@ -882,7 +900,7 @@ create_mem_ref (gimple_stmt_iterator *gs /* Transform [base + index + ...] into: base' = base + index; [base' + ...]. */ - if (parts.index) + if (parts.index && !scaled_p) { tmp = parts.index; parts.index = NULL_TREE;