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: 117949 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp3667399qgn; Fri, 3 Nov 2017 09:32:54 -0700 (PDT) X-Google-Smtp-Source: ABhQp+SDTb8R1z+9NORjfHq4gg8P2xa/IndERIoKkywcoJpOQs0GO0AAEvX++O5jc4x9TrxBdO5Z X-Received: by 10.99.97.149 with SMTP id v143mr7821020pgb.413.1509726774719; Fri, 03 Nov 2017 09:32:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509726774; cv=none; d=google.com; s=arc-20160816; b=K2sU2KnsiKp/7Kv9EzYnSS0b5Hf/tM4Yp/LwKQ+DznOcmXPFmoE5gvofkT5lTOY5v0 iHUCTfZmfmQQ/6uxdCQCB6IJTGmLE5Mzl6RZCm8h+8gTZoVsQnywYZUZJaw64HD/XKuL nnWs9s/sw1lIjPWGUmPGKIELOYykOGrC4NNcn8vMZglQh772ReDtihMNy9vzt8zMwU2P S3nPOU+kQa8jcOb3PkyH6HhM+wkYXTSOTpM2tc1e8rddDF2oQz3D+62+W6cZ4+UACy5d /YKlSB/9TrK/DbFojrpUAkzPk5MDG6GLk3EgS68OUDZiREf+szKw4gQsTT2hxSRWX7ki oymA== 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=rTzUfIVFG7YNolfP3xtU99YwCVwGgvI2jDw5zdiHciE=; b=Q8OsBicGdEIcZWK6QYCPB82G7MZklIt8vXK+N8x72mHCTnBlaq4xOe27cTGyBuGVnl v8UXFq61I6puy6PdnNvSfcrAgYHr9/0uP6jzYjfIVDxCrd6U9PjN7VVoD5rHvKFDRYHu 9BO3IZb8kEN0G74lJTBKxldDNeOBoFVCFX2pdVvgLSe4P35xPTnmvOf9kPQcHxZ6C+JD yl3TvXqURnWSlnUFT+jGZUqmMYPTqwTOaeP4oYUodjgVCike7cl0n33gzeWtqntP2WSu oH3iv/ba1MRsUFaWZ6zAddVhk1QjwE8uYaF+ikU0bzD8D8DVkxBV07frs6JLfb+kgMh8 i6lA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wV7Id54M; spf=pass (google.com: domain of gcc-patches-return-465885-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-465885-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 v17si6269736pgb.548.2017.11.03.09.32.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Nov 2017 09:32:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-465885-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=wV7Id54M; spf=pass (google.com: domain of gcc-patches-return-465885-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-465885-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=oOnIxzSbVKkWI4VQJW+AcINCmxhkuvBxufrcLl5ISW+jdl5HgFcse g6o3opz4ddTMT86x/BHr7/YcDbjr/KHcSLI+dhyCwy+SoPTtIFs7TM6I+fmBfLKv NUs5mb7NnEqTClD76SLgGJtKu5R/ttRYdNZ4xJL5jKMvUwx/3VuB1A= 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=wV7Id54MSHGHtLsrVNeR nVfK6Z5wcInbh43KlRURlGoKg+ugqvVNVawWVGmDrFXh91Kjmt0N0I0W90vQ83Gx VC+ESpD+/jCLFlz+8hns+/2hzxGF831y3UgcJ9+OX1v3x552ut/3rFL7Kbv47bsF Vj5AYkMG0PGRCPgns3CaHw8= 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-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;