From patchwork Thu Jun 23 14:43:43 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Stubbs X-Patchwork-Id: 101641 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]) by ozlabs.org (Postfix) with SMTP id 23B51B6F86 for ; Fri, 24 Jun 2011 00:44:05 +1000 (EST) Received: (qmail 2408 invoked by alias); 23 Jun 2011 14:44:04 -0000 Received: (qmail 2398 invoked by uid 22791); 23 Jun 2011 14:44:02 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, TW_TM X-Spam-Check-By: sourceware.org Received: from mail-wy0-f175.google.com (HELO mail-wy0-f175.google.com) (74.125.82.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 23 Jun 2011 14:43:48 +0000 Received: by wyb34 with SMTP id 34so1560536wyb.20 for ; Thu, 23 Jun 2011 07:43:47 -0700 (PDT) Received: by 10.227.11.143 with SMTP id t15mr2062036wbt.56.1308840226973; Thu, 23 Jun 2011 07:43:46 -0700 (PDT) Received: from [192.168.0.100] (cpc2-hawk4-0-0-cust828.aztw.cable.virginmedia.com [82.32.123.61]) by mx.google.com with ESMTPS id ge4sm1285790wbb.30.2011.06.23.07.43.45 (version=SSLv3 cipher=OTHER); Thu, 23 Jun 2011 07:43:46 -0700 (PDT) Message-ID: <4E03511F.9040507@codesourcery.com> Date: Thu, 23 Jun 2011 15:43:43 +0100 From: Andrew Stubbs User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110516 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: gcc-patches@gcc.gnu.org CC: patches@linaro.org Subject: [PATCH (7/7)] Mixed-sign multiplies using narrowest mode References: <4E034EF2.3070503@codesourcery.com> In-Reply-To: <4E034EF2.3070503@codesourcery.com> 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 Patch 4 introduced support for using signed multiplies to code unsigned multiplies in a narrower mode. Patch 5 then introduced support for mis-matched input modes. These two combined mean that there is case where only the smaller of two inputs is unsigned, and yet it still tries to user a mode wider than the larger, signed input. This is bad because it means unnecessary extends and because the wider operation might not exist. This patch catches that case, and ensures that the smaller, unsigned input, is zero-extended to match the mode of the larger, signed input. Of course, both inputs may still have to be extended to fit the nearest available instruction, so it doesn't make a difference every time. OK? Andrew 2011-06-23 Andrew Stubbs gcc/ * tree-ssa-math-opts.c (convert_mult_to_widen): Better handle unsigned inputs of different modes. (convert_plusminus_to_widen): Likewise. gcc/testsuite/ * gcc.target/arm/smlalbb-3.c: New file. --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/smlalbb-3.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=armv7-a" } */ + +long long +foo (long long a, short *b, char *c) +{ + return a + *b * *c; +} + +/* { dg-final { scan-assembler "smlalbb" } } */ --- a/gcc/tree-ssa-math-opts.c +++ b/gcc/tree-ssa-math-opts.c @@ -2103,9 +2103,17 @@ convert_mult_to_widen (gimple stmt, gimple_stmt_iterator *gsi) { if (op != smul_widen_optab) { - from_mode = GET_MODE_WIDER_MODE (from_mode); - if (GET_MODE_SIZE (to_mode) <= GET_MODE_SIZE (from_mode)) - return false; + /* We can use a signed multiply with unsigned types as long as + there is a wider mode to use, or it is the smaller of the two + types that is unsigned. Note that type1 >= type2, always. */ + if (TYPE_UNSIGNED (type1) + || (TYPE_UNSIGNED (type2) + && TYPE_MODE (type2) == from_mode)) + { + from_mode = GET_MODE_WIDER_MODE (from_mode); + if (GET_MODE_SIZE (to_mode) <= GET_MODE_SIZE (from_mode)) + return false; + } op = smul_widen_optab; handler = find_widening_optab_handler_and_mode (op, to_mode, @@ -2244,14 +2252,21 @@ convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple stmt, if (TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2)) { enum machine_mode mode = TYPE_MODE (type1); - mode = GET_MODE_WIDER_MODE (mode); - if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (TYPE_MODE (type))) + + /* We can use a signed multiply with unsigned types as long as + there is a wider mode to use, or it is the smaller of the two + types that is unsigned. Note that type1 >= type2, always. */ + if (TYPE_UNSIGNED (type1) + || (TYPE_UNSIGNED (type2) + && TYPE_MODE (type2) == mode)) { - type1 = type2 = lang_hooks.types.type_for_mode (mode, 0); - cast1 = cast2 = true; + mode = GET_MODE_WIDER_MODE (mode); + if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (TYPE_MODE (type))) + return false; } - else - return false; + + type1 = type2 = lang_hooks.types.type_for_mode (mode, 0); + cast1 = cast2 = true; } if (TYPE_MODE (type2) != TYPE_MODE (type1))