From patchwork Tue May 2 08:31:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 757461 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 3wHDzV0Fp3z9sD9 for ; Tue, 2 May 2017 18:31:49 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="d+FVSGZ0"; 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:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=w/HehMlVwgzTdRF+9whczVYFGcK+1NizRq4flvxYmXfwRO05Xu p89wZLFm61Mjz/uBlN5QdI2MMF8BQUMspGizkTs7hgAA1wX5xMOk0XaEKkJ27EgX D7zRbRKav1xzU90bqSW8jp6GHO6fNmHy9DdtdT/LstjsLf6tlzvFrHv7k= 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:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=xh2xRBcNuweXtLzBowkuqjYrnGw=; b=d+FVSGZ0Ks5JyD5f7Mjc dHRq6bWsjvxCwVEOObAMEBR1qPVp87sQAN9K09X04Kpum8enPFLV4R8QnGLFwIyc YIlZ1y+tQzL9kyk+CpLnEdvyl0ZPRaDiiA3wvyMDwAqJ/ZoVIPUVoGMItZWvoZ/w Q0AaehvWXOz15PKoLY1HM6w= Received: (qmail 49785 invoked by alias); 2 May 2017 08:31:37 -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 49242 invoked by uid 89); 2 May 2017 08:31:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=n1 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, 02 May 2017 08:31:33 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 317CB9EBE; Tue, 2 May 2017 04:31:34 -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 ayJdB1PrC3UC; Tue, 2 May 2017 04:31:34 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 214AE9EBF; Tue, 2 May 2017 04:31:34 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4192) id 200D84FF; Tue, 2 May 2017 04:31:34 -0400 (EDT) Date: Tue, 2 May 2017 04:31:34 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Optimization of fixed/fixed operations with compatible 'smalls. Message-ID: <20170502083134.GA30617@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) If the Small values of the fixed points involved in a division operation have common factors, it is possible to simplify the restulting expression, which will involve numerators and denominators of the corresponding Small values of the type, as those values are integer literals. The following must execute quietly: gcc -c -gnatDG ec.adb grep "30 / 30" ec.adb.dg with S; with Interfaces; use Interfaces; procedure Ec is Signal_Denominator : constant := 400; type Signal_Type is delta 1.0 / Signal_Denominator range -65_536.0 / Signal_Denominator .. 65_535.0 / Signal_Denominator; for Signal_Type'Small use 1.0 / Signal_Denominator; Delay_30 : constant := 30; type Filter_30_Input_Type is delta 1.0 / Signal_Denominator range 0.0 .. Signal_Type'Last; for Filter_30_Input_Type'Small use 1.0 / Signal_Denominator; type Filter_30_Output_Type is delta 1.0 / Signal_Denominator / Delay_30 range 0.0 .. Signal_Type'Last; for Filter_30_Output_Type'Small use 1.0 / Signal_Denominator / Delay_30; type Sum_30_Index_Type is mod Delay_30; package Sum_30_Filter is new S (Input_Type => Filter_30_Input_Type, Output_Type => Filter_30_Output_Type, Filter_Index_Type => Sum_30_Index_Type); begin null; end Ec; --- generic type Input_Type is delta <>; type Output_Type is delta <>; type Filter_Index_Type is mod <>; package S is type Filter_Type is private; function Process_Sample (This : in out Filter_Type; Sample : in Input_Type) return Output_Type; private type Sum_Filter_Array_Type is array (Filter_Index_Type) of Input_Type; type Filter_Type is record Samples : Sum_Filter_Array_Type; Index : Filter_Index_Type; Last_Output : Output_Type; end record; end S; --- package body S is function Process_Sample (This : in out Filter_Type; Sample : in Input_Type) return Output_Type is -- Initialize output as y(n-1). Output : Output_Type := This.Last_Output; type Filter_Delay_Type is delta 1.0 range 1.0 .. 65_535.0; D : constant Filter_Delay_Type := Filter_Delay_Type (Filter_Index_Type'Last - Filter_Index_Type'First) + 1.0; begin -- Compute y(n) = y(n-1) + (x(n) - x(n-D)) / D, where D = filter delay. Output := Output + (Sample - This.Samples (This.Index)) / D; return Output; end Process_Sample; end S; Tested on x86_64-pc-linux-gnu, committed on trunk 2017-05-02 Ed Schonberg * exp_fixd.adb (Expand_Divide_Fixed_By_Fixed_Giving_Fixed): Simplify the expression for a fixed-fixed division to remove divisions by constants whenever possible, as an optimization for restricted targets. Index: exp_fixd.adb =================================================================== --- exp_fixd.adb (revision 247461) +++ exp_fixd.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2015, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2017, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -2008,6 +2008,31 @@ else Do_Divide_Fixed_Fixed (N); + + -- A focused optimization: if after constant folding the + -- expression is of the form: T ((Exp * D) / D), where D is + -- a static constant, return T (Exp). This form will show up + -- when D is the denominator of the static expression for the + -- 'small of fixed-point types involved. This transformation + -- removes a division that may be expensive on some targets. + + if Nkind (N) = N_Type_Conversion + and then Nkind (Expression (N)) = N_Op_Divide + then + declare + Num : constant Node_Id := Left_Opnd (Expression (N)); + Den : constant Node_Id := Right_Opnd (Expression (N)); + + begin + if Nkind (Den) = N_Integer_Literal + and then Nkind (Num) = N_Op_Multiply + and then Nkind (Right_Opnd (Num)) = N_Integer_Literal + and then Intval (Den) = Intval (Right_Opnd (Num)) + then + Rewrite (Expression (N), Left_Opnd (Num)); + end if; + end; + end if; end if; end Expand_Divide_Fixed_By_Fixed_Giving_Fixed;