From patchwork Tue Feb 7 20:50:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9561155 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 75F506047A for ; Tue, 7 Feb 2017 20:54:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67B9028443 for ; Tue, 7 Feb 2017 20:54:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5AEA828492; Tue, 7 Feb 2017 20:54:01 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, 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 0CE3328443 for ; Tue, 7 Feb 2017 20:54:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932376AbdBGUx4 (ORCPT ); Tue, 7 Feb 2017 15:53:56 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33153 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755096AbdBGUxS (ORCPT ); Tue, 7 Feb 2017 15:53:18 -0500 Received: by mail-wm0-f68.google.com with SMTP id v77so30450239wmv.0 for ; Tue, 07 Feb 2017 12:52:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2SV0qBHqasUfIPi0eyr4KuaD/9sIuqjHZVjKLVJELGo=; b=FSaHmZsHD3EMWFsqVqxQdkYD1TL+kWfP9yurtJhh9jGDrK/QqucWGQ/h10ilgmTJWD Gk74VHSFU8tzCkqxoo4VEJCLwf0ljg1kx8+BgCBGgoZXBf5bEsDljh19xGHPPrsmqjIN Cv2Q4CE1BWgTh9L/kAu2pe9tE1YlB8LzXV468J92FX5QVsrtt/rTbCsF7Qu18azm45WK /LotZkFOT0sci6Mnp1uL9jHX8/UIQIMVtkEAJCizW9y9SP+KBhha9mTLv1SDZrA8YilV cpErtljx23/1ybfTIVGwprtHLjXaLyllH5isXnTRPn6cp+FsrIAXL4QoIEaX2X2s4V1k eN3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2SV0qBHqasUfIPi0eyr4KuaD/9sIuqjHZVjKLVJELGo=; b=be29u0wh8vL+KA6GyygpwdKe0WS0o6OWUxxJaYbKgV4+fttARu2Z3fFnnKVZcMZOy/ yfjzYTYPLZSTfihfDWemsWItKEsqOf3+9m9p2upIQ5XZ+O0fVpB641QUBIWf92XBLkQf XzCCC7ujsAN66IedhB0pPzJB4UeEY4baXTlwyBwYs93ekoG4IXicioSHpzPS1yNqfqMQ BIcY6vhQ5JOJ89FE0IpA1eYs6IJxBYtzLVpaMSViR0WkeU50dDBq+txVmEaPAcMy64RS 7XCWz9hsnINILyKmG4Kwbz8bhAGDmbq9EKpGIfijtyEUYy22LJFTGLFxC9n7nOqlJczy jMyw== X-Gm-Message-State: AMke39ln68X0P0T2GO+CRvZk7XVvP4blMddJQ4T+4UadbY0DsLDq6tmGBR3uhveZdAB4iQ== X-Received: by 10.28.64.213 with SMTP id n204mr16047289wma.12.1486500756492; Tue, 07 Feb 2017 12:52:36 -0800 (PST) Received: from localhost.localdomain ([91.179.185.46]) by smtp.gmail.com with ESMTPSA id g5sm9320846wrd.0.2017.02.07.12.52.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Feb 2017 12:52:36 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Rasmus Villemoes , Luc Van Oostenryck Subject: [PATCH v3 3/5] simplify '(x * -1)' to '-x' Date: Tue, 7 Feb 2017 21:50:26 +0100 Message-Id: <20170207205028.35869-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170207205028.35869-1-luc.vanoostenryck@gmail.com> References: <20170207205028.35869-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 | 11 +++++++++++ validation/optim/muldiv-minus-one.c | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 validation/optim/muldiv-minus-one.c diff --git a/simplify.c b/simplify.c index 91021dbb1..363cc5ad7 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,14 @@ 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; + } } return 0; diff --git a/validation/optim/muldiv-minus-one.c b/validation/optim/muldiv-minus-one.c new file mode 100644 index 000000000..729b73443 --- /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\\. + */