From patchwork Sun Dec 11 21:13:28 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: 9469871 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 7C986607D3 for ; Sun, 11 Dec 2016 21:16:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 709A4280FC for ; Sun, 11 Dec 2016 21:16:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 657542835E; Sun, 11 Dec 2016 21:16:06 +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 10CE428358 for ; Sun, 11 Dec 2016 21:16:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753790AbcLKVQF (ORCPT ); Sun, 11 Dec 2016 16:16:05 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34935 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753844AbcLKVQE (ORCPT ); Sun, 11 Dec 2016 16:16:04 -0500 Received: by mail-wm0-f66.google.com with SMTP id a20so7108372wme.2 for ; Sun, 11 Dec 2016 13:16:03 -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=tcHzMegLxBru67Y0XyHqJKctiGZAZZbm5zr/F+7Idm8=; b=LyDIz6/V5pp6+txcArIXMJ74Sjt1QMkwi6TDPYOhduj/k0DrdnnE+IySZYgmC38QIn YmeHLmpYZwGit43ht7FgQzrp4yuGo5XSr3DnsZL1jnEDUNcAfK/Wu5EuBL+DK6JzeEqi HVoPd8s+79l47yia5OQPPQePAVlbD7lLxeN2lkUYjXlHTSI3o/RE4eZ3oK7FBfa28pPY qssCjOfOfA54i9TQWS5gueRt1mJ6njQtVWG0W3DRTKzprTJkqXxorpXBBmdF9sK4vToJ B6sdRg8F9oQYiLy4ipY6CSbV7IKvE0XCCUeBUK0ZxVJs3wFdd73fNf+zqPddIwQMf8Rq i9UQ== 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=tcHzMegLxBru67Y0XyHqJKctiGZAZZbm5zr/F+7Idm8=; b=OCw3NGOLCWKZJnBT8TcSb00tV6vK/ZDQ0pzZT1CT8UdYBhL2Cg8iqhPm3PUNnmG532 jSRdCepDul9Yyd2Ohm+f3nHVGaShhg0AbWCqK91oNXaieZqueiw+iTB43l+2y1mv9DAO D6IjofyROtccLNhNp2VeX3urR64nlf+yWlq5z2bPIf8b2m1nM9OL0iJiT/bFlongcNKJ nQxI/5y5biFe54e4DXtZM4fC/Hstc1P3X42x+kDTjvG6wTfqp0OloEQLjTkpFIdQSMcu oycFkevff2qur7Hk4YqGgKRO7QQzn8NGQlyNDRXBQjDZuJBrXMSUeLbmz2TIXDWrNlaI cPtw== X-Gm-Message-State: AKaTC02YFCFf+iIttp7iw/9B2h9yRFSpqV8VG1Q6KMHopBo7IKxQVts9qis90fHBRodT/A== X-Received: by 10.28.209.7 with SMTP id i7mr7117421wmg.62.1481490962813; Sun, 11 Dec 2016 13:16:02 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:854:6400:64f0:fb64:8c04:9ee9]) by smtp.gmail.com with ESMTPSA id f126sm32087951wme.22.2016.12.11.13.16.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Dec 2016 13:16:02 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH 3/3] simplify '(x || x)' and '(x && x)' Date: Sun, 11 Dec 2016 22:13:28 +0100 Message-Id: <20161211211328.26735-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161211211328.26735-1-luc.vanoostenryck@gmail.com> References: <20161211211328.26735-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 The operators '||' and '&&' being idempotent, the expressions '(x || x)' and '(x && x)' can be simplified to a test against zero. Note: they could even be replaced by 'x' itself but only if 'x' is already a boolean expression/has already been tested against zero. If it is the case, the redundant test this will be optimized away in further steps. For example, test-linearize on the following code: int ior(int a) { return a || a; } emitted the following instructions: or-bool.32 %r3 <- %arg1, %arg1 after the patch, it now emits: setne.32 %r3 <- %arg1, $0 which is easier to combine with others simplifications. Signed-off-by: Luc Van Oostenryck --- simplify.c | 8 ++++---- validation/optim/bool-same-args.c | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 validation/optim/bool-same-args.c diff --git a/simplify.c b/simplify.c index d076ebb5..169561fd 100644 --- a/simplify.c +++ b/simplify.c @@ -578,10 +578,10 @@ static int simplify_binop_same_args(struct instruction *insn, pseudo_t arg) case OP_AND_BOOL: case OP_OR_BOOL: - // simplification is correct only if the operands - // have already been compared against zero which - // is not enforced. - break; + remove_usage(arg, &insn->src2); + insn->src2 = value_pseudo(0); + insn->opcode = OP_SET_NE; + return REPEAT_CSE; default: break; diff --git a/validation/optim/bool-same-args.c b/validation/optim/bool-same-args.c new file mode 100644 index 00000000..0ae4684e --- /dev/null +++ b/validation/optim/bool-same-args.c @@ -0,0 +1,12 @@ +static int ior(int a) { return a || a; } +static int and(int a) { return a && a; } + +/* + * check-name: bool-same-args + * check-command: test-linearize $file + * check-output-ignore + * + * check-output-excludes: or-bool\\. + * check-output-excludes: and-bool\\. + * check-output-contains: setne\\. + */