From patchwork Fri Sep 25 15:04:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Greenhalgh X-Patchwork-Id: 54169 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by patches.linaro.org (Postfix) with ESMTPS id 56609218DB for ; Fri, 25 Sep 2015 15:06:37 +0000 (UTC) Received: by wicmn1 with SMTP id mn1sf9483617wic.1 for ; Fri, 25 Sep 2015 08:06:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mailing-list:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:sender :delivered-to:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-type:x-original-sender :x-original-authentication-results; bh=VNjYskDjMhN8K3dSkPcaSl2sBiAF/Kk00c9R8Gn88FA=; b=ElHGSqUb2eCILDRF+HSA/0pf4fX5tOQ48q9Jwgjt61vf5vlWj4CN5npvTQ1iQ34wht oe8GGD2Pfw7SOC1ho9fwTg+85uCcesDd4a1+pY8Lq1btRqqZPjs/vX1/YrykmJAETzzJ 8iGPCmJ0klHy7zpTTvLvKedho9end0f8MA3sCm5PmLwrtM2f6SxB5Db1Fdb12Tv8bkbm brbH4RbGPiRuXegZ3s1wFcuXQF3TRz3N3Gr+im1vkluHmsWqr4Yt/+s5xSkXpxTnbH6k Y9VNFba3VZ3Plx4/GRYQBYhhUAsWaudEQfT8STmLxavOeocJRe1GWdO6Geu9/phhHFSA GAXA== X-Gm-Message-State: ALoCoQkCxiNOh8Wlc/PG/+7U5qAH9cOKYTqvzHuFcFB7h3OHQUnhLUbtKrX4yRX22p0ysY86ZMwL X-Received: by 10.112.158.202 with SMTP id ww10mr1010090lbb.13.1443193596619; Fri, 25 Sep 2015 08:06:36 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.20.65 with SMTP id k62ls213736lfi.16.gmail; Fri, 25 Sep 2015 08:06:36 -0700 (PDT) X-Received: by 10.152.7.106 with SMTP id i10mr1774617laa.114.1443193596482; Fri, 25 Sep 2015 08:06:36 -0700 (PDT) Received: from mail-la0-x22a.google.com (mail-la0-x22a.google.com. [2a00:1450:4010:c03::22a]) by mx.google.com with ESMTPS id e9si1897273laa.157.2015.09.25.08.06.36 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Sep 2015 08:06:36 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::22a as permitted sender) client-ip=2a00:1450:4010:c03::22a; Received: by lacdq2 with SMTP id dq2so46288394lac.1 for ; Fri, 25 Sep 2015 08:06:36 -0700 (PDT) X-Received: by 10.25.19.73 with SMTP id j70mr1192998lfi.29.1443193596163; Fri, 25 Sep 2015 08:06:36 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp95571lbq; Fri, 25 Sep 2015 08:06:34 -0700 (PDT) X-Received: by 10.107.17.234 with SMTP id 103mr6983900ior.21.1443193594490; Fri, 25 Sep 2015 08:06:34 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id p140si3210445iop.59.2015.09.25.08.06.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Sep 2015 08:06:34 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-408354-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 17941 invoked by alias); 25 Sep 2015 15:06:18 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list 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 17919 invoked by uid 89); 25 Sep 2015 15:06:18 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=unavailable version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (207.82.80.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 25 Sep 2015 15:06:17 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-18-pciF817lRxS2boVWRJJbkg-1; Fri, 25 Sep 2015 16:05:08 +0100 Received: from e107456-lin.cambridge.arm.com ([10.1.2.79]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Fri, 25 Sep 2015 16:05:07 +0100 From: James Greenhalgh To: gcc-patches@gcc.gnu.org Cc: ramana.radhakrishnan@arm.com, bernds_cb1@t-online.de, law@redhat.com, ebotcazou@libertysurf.fr, steven@gcc.gnu.org Subject: [Patch Prototype AArch64 ifcvt 4/3] Wire up the new if-convert costs hook for AArch64 Date: Fri, 25 Sep 2015 16:04:39 +0100 Message-Id: <1443193479-10830-5-git-send-email-james.greenhalgh@arm.com> In-Reply-To: <1443193479-10830-1-git-send-email-james.greenhalgh@arm.com> References: <20150911095608.GA2448@arm.com> <1443193479-10830-1-git-send-email-james.greenhalgh@arm.com> MIME-Version: 1.0 X-MC-Unique: pciF817lRxS2boVWRJJbkg-1 X-IsSubscribed: yes X-Original-Sender: james.greenhalgh@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::22a as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@gcc.gnu.org X-Google-Group-Id: 836684582541 Hi, This patch is a simple prototype showing how a target might choose to implement TARGET_COSTS_IFCVT_NOCE_IS_PROFITABLE_P. It has not been tuned, tested or looked at in any meaningful way. While the patch is in need of more detailed analysis it is sufficient to serve as an indication of what direction I was aiming for with this patch set. Clearly this is not OK for trunk without further work, but I thought I'd include it as an afterthought for the costs rework. Thanks, James --- 2015-09-26 James Greenhalgh * config/aarch64/aarch64.c (aarch64_additional_branch_cost_for_probability): New. (aarch64_ifcvt_noce_profitable_p): Likewise. (TARGET_COSTS_IFCVT_NOCE_PROFITABLE_P): Likewise. diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c index 4fa6a4e..6a753e9 100644 --- a/gcc/config/aarch64/aarch64.c +++ b/gcc/config/aarch64/aarch64.c @@ -76,6 +76,8 @@ #include "sched-int.h" #include "cortex-a57-fma-steering.h" #include "target-globals.h" +#include "ifcvt.h" +#include "math.h" /* This file should be included last. */ #include "target-def.h" @@ -13362,6 +13364,7 @@ aarch64_unspec_may_trap_p (const_rtx x, unsigned flags) return default_unspec_may_trap_p (x, flags); } + /* Implement TARGET_PROMOTED_TYPE to promote __fp16 to float. */ static tree aarch64_promoted_type (const_tree t) @@ -13370,6 +13373,117 @@ aarch64_promoted_type (const_tree t) return float_type_node; return NULL_TREE; } + +/* Get an additional cost roughly mapping to the cycles cost of a + mispredicted branch, scaled by how predictable we think that branch + is. Note that in the AArch64 back-end we try to map COSTS_N_INSNS (1) + to the latency of the cheapest instruction, with other instructions + scaled from that. + + PREDICTABILITY is derived from the computed branch taken probability + and will be in the range [0..1], where 0.0 is a branch which should be + considered unpredictable, and 1.0 is a branch which is predictable. + + Branches which are more predictable are less profitable to if-convert and + should return smaller values. Branches which are more predictable are more + profitable to if-convert and should return higher values. + + FORNOW: use math.h's log2 and take and return floats. + TODO: Design a function which gives more meaningful results. */ + +static float +aarch64_additional_branch_cost_for_probability (float predictability) +{ + /* Scale to be in range [1..2], then take log2 of it. Use this to + pick a point between two -mcpu (well, one day) dependent values. */ + unsigned int lower_bound = COSTS_N_INSNS (2); + unsigned int upper_bound = COSTS_N_INSNS (6); + + return log2 (predictability + 1.0f) + * (upper_bound - lower_bound) + lower_bound; +} + +/* Prototype only, return TRUE if SEQ is a profitable transformation of + the basic block structure defined in IF_INFO. + + TODO: Design and analyze how this function should actually behave. + This is just guesswork. */ + +static bool +aarch64_ifcvt_noce_profitable_p (rtx_insn *seq, + struct noce_if_info *if_info) +{ + bool speed_p + = optimize_bb_for_speed_p (if_info->test_bb); + + float sequence_cost (seq_cost (seq, speed_p)); + + /* We know that when expanding an if-convert sequence for AArch64 we + will generate a number of redundant comparisons. Account for that + by slightly modifying the numbers. */ + sequence_cost /= 1.5f; + + float then_cost (if_info->then_cost); + float else_cost = 0.0; + + if (if_info->else_bb) + else_cost = if_info->else_cost; + + /* Get the weighting factors. */ + edge te = EDGE_SUCC (if_info->test_bb, 0); + float taken_probability = ((float) te->probability) / REG_BR_PROB_BASE; + + /* We have to reverse the branch taken probability if we have a + then_else_reversed branch structure. We want to get this correct + so we scale the cost of the correct branch. */ + if (if_info->then_else_reversed) + taken_probability = 1.0f - taken_probability; + + /* For branch_probability, we don't care which branch we are + considering, we just need a value in the range [0..0.5]. */ + float branch_probability = taken_probability; + if (branch_probability > 0.5f) + branch_probability = 1.0f - branch_probability; + + /* Taken_probability is in range [0.0..0.5]. Scale to be in + range [0.0..1.0], and subtract from 1.0 so a taken_probability of 0.5 + gives a predictability of 0.0. */ + float predictability = 1.0 - (2.0 * branch_probability); + + float weighted_average_cost = ((taken_probability + * (then_cost - else_cost)) + then_cost); + + if (!if_info->else_bb) + weighted_average_cost /= 2.0f; + float branch_cost + = aarch64_additional_branch_cost_for_probability (predictability); + + float estimate_unconverted_cost = weighted_average_cost + branch_cost; + + bool judgement = sequence_cost <= estimate_unconverted_cost; + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "\n AArch64 cost calculations for if-conversion:\n" + " taken probability %f\n" + " then block cost %f\n" + " else block cost %f\n" + " weighted average cost %f\n" + " additional branch cost %f\n" + " total unconverted cost %f\n" + " new sequence cost %f\n" + "\n Judgement: %s\n", + taken_probability, then_cost, else_cost, + weighted_average_cost, branch_cost, + estimate_unconverted_cost, sequence_cost, + judgement + ? "Convert" + : "Do not convert"); + } + + return judgement; +} + #undef TARGET_ADDRESS_COST #define TARGET_ADDRESS_COST aarch64_address_cost @@ -13674,6 +13788,10 @@ aarch64_promoted_type (const_tree t) #undef TARGET_USE_PSEUDO_PIC_REG #define TARGET_USE_PSEUDO_PIC_REG aarch64_use_pseudo_pic_reg +#undef TARGET_COSTS_IFCVT_NOCE_PROFITABLE_P +#define TARGET_COSTS_IFCVT_NOCE_PROFITABLE_P \ + aarch64_ifcvt_noce_profitable_p + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-aarch64.h"