From patchwork Wed Dec 7 15:46:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9464763 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 649006022E for ; Wed, 7 Dec 2016 15:47:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 69F1A28358 for ; Wed, 7 Dec 2016 15:47:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5ED4628519; Wed, 7 Dec 2016 15:47:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A78D28358 for ; Wed, 7 Dec 2016 15:47:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932346AbcLGPrI (ORCPT ); Wed, 7 Dec 2016 10:47:08 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:36652 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932177AbcLGPrI (ORCPT ); Wed, 7 Dec 2016 10:47:08 -0500 Received: by mail-wm0-f68.google.com with SMTP id m203so28578517wma.3 for ; Wed, 07 Dec 2016 07:47:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=RnJ0FyXVBzZzQnNUsQtFkuCqQndNzU9ICJIwds/ewEM=; b=w2jwM9QfDVyjDzLWcSumC5yVuyC20K5vlmffmnWsFCebqGXxeE4aQB4l27EmZALx1T 6XM+viKM+5MAmfV8bLtWgSw/NWgYCiL6vzmB3BaN61CNFrYScGbZdnnUML4j+E1XFANB IHQmUIIkXqm6kQOIkLhPbVFbRWEts/pnIHHPZ4dTFGbCGZ0Ecb2RDvImXCN2BX90h+It 3M01/UzjO0UWxilwi99yUmCi+wRQzVHiUK1Hqt34jJqehGovWVMgPTSF6/q6gTjgSk1w U9uBTzVjNzNe14hLDYIzjTdsDdZ6/LnmcdSG6OJIHcBPyziS9ej9jKlmHVQE3fGnxVcc qKyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RnJ0FyXVBzZzQnNUsQtFkuCqQndNzU9ICJIwds/ewEM=; b=AEbcnCuSOnMFk0rwmlxA0JlnaCeyMIE9V2BI3kFJlpQ1wDJ/e7tuaCrJvOA8WKYyie FmtNJiuYZOy5QhxLm7CqbpD0aPIp7QW3Yzh/2zdRdXKhjUzYwg4uiXz9fXkULYCT35zn aASxevMxhoFQxakcDRK1//AFVqByS8zQqfbaDyzoI5q6K397iw/U6EYRYNeFVEcpiK8H OCZE3HR8cE2HPVRw4ONuyvGAgGoIbVswNke7qvbf3uULtebdDDq1cHW8sRerCu+CI1Ly PcxvHMxqSMr/u7FIr+l1NgiX/6l2mIe4KFiC3NIFCRzjRXfKnkYPW0n+po1EsIkZkdfd q/hw== X-Gm-Message-State: AKaTC00PBQAtUEWyEeCOtX27L//6PRfS/LBRCg/PMohqWaVGuHd2N2elsgGKpPghcGwo1w== X-Received: by 10.28.185.78 with SMTP id j75mr759487wmf.14.1481125626657; Wed, 07 Dec 2016 07:47:06 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:816:ae00:7dc3:2e5b:38fe:befd]) by smtp.gmail.com with ESMTPSA id b7sm32020014wjm.39.2016.12.07.07.47.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Dec 2016 07:47:06 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH 3/5] simplify '(x * -1)' to '-x' Date: Wed, 7 Dec 2016 16:46:53 +0100 Message-Id: <20161207154655.98109-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161207154655.98109-1-luc.vanoostenryck@gmail.com> References: <20161207154655.98109-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently we simplify multiplication by 1 but nothing is done for multiplication by -1 which is equivalent to the negation of its first operand. This patch add this simplification. Also add small test cases showing the simplification. Signed-off-by: Luc Van Oostenryck --- simplify.c | 12 ++++++++++++ validation/optim/muldiv-minus-one.c | 13 +++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 validation/optim/muldiv-minus-one.c diff --git a/simplify.c b/simplify.c index 5541fc4c..6c7c79e9 100644 --- a/simplify.c +++ b/simplify.c @@ -312,6 +312,9 @@ static int simplify_asr(struct instruction *insn, pseudo_t pseudo, long long val static int simplify_mul_div(struct instruction *insn, long long value) { + unsigned long long sbit = 1ULL << (insn->size - 1); + unsigned long long bits = sbit | (sbit - 1); + if (value == 1) return replace_with_pseudo(insn, insn->src1); @@ -320,6 +323,15 @@ static int simplify_mul_div(struct instruction *insn, long long value) case OP_MULU: if (value == 0) return replace_with_pseudo(insn, insn->src2); + + if (!(value & sbit)) // positive + break; + + value |= ~bits; + if (value == -1) { + insn->opcode = OP_NEG; + return REPEAT_CSE; + } /* Fall through */ case OP_DIVS: case OP_DIVU: diff --git a/validation/optim/muldiv-minus-one.c b/validation/optim/muldiv-minus-one.c new file mode 100644 index 00000000..729b7344 --- /dev/null +++ b/validation/optim/muldiv-minus-one.c @@ -0,0 +1,13 @@ +typedef unsigned int u32; + +int smulm1(int a) { return a * -1; } +u32 umulm1(u32 a) { return a * (u32) -1; } + +/* + * check-name: muldiv-minus-one + * check-command: test-linearize -Wno-decl $file + * check-output-ignore + * + * check-output-excludes: mul[us]\\. + * check-output-contains: neg\\. + */