From patchwork Wed Apr 12 14:18:02 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: 9677569 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 7305860325 for ; Wed, 12 Apr 2017 14:20:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 65D932867E for ; Wed, 12 Apr 2017 14:20:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5AA502868A; Wed, 12 Apr 2017 14:20:26 +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=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, THIS_AD, 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 9A53D2867E for ; Wed, 12 Apr 2017 14:20:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753286AbdDLOUP (ORCPT ); Wed, 12 Apr 2017 10:20:15 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:35149 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752940AbdDLOUO (ORCPT ); Wed, 12 Apr 2017 10:20:14 -0400 Received: by mail-wr0-f194.google.com with SMTP id l44so4423334wrc.2 for ; Wed, 12 Apr 2017 07:20:13 -0700 (PDT) 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=GEM8FfkTDUN5sZvQtyL8QuOLipWxFoONWkQz8CmAl48=; b=g+YHP8yrXeudKur2/p4gDduks44a8v5F3b8klUUYlHAE5kvXpw2E7fncZ9es1PPpWJ JmAVx8vn29gnw9NHa45ntjgbXPfsx/lEJhPVB4Ie9EKy/6G94Y0oswlUaGkin0MX5p3C /Vw0khKnctcdNqLW9PDXxH/lfZ3oUz2Q7BVvP1VVyBfafKvchLHsbAkA44FxxO6AQiBS JGSZ5EYVsPqT3LQPlOYkhDewOjULxV662eXaSLNLODRfWB6rSQ+/VnIbPsXxLI+Us7lo 1uMKucw2TBo7+7BhYBqzIaNCiR4fI5sEtBJ5/dlAYY25rJAzWM2NU2yBD++D6js/q1H6 i4Qw== 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=GEM8FfkTDUN5sZvQtyL8QuOLipWxFoONWkQz8CmAl48=; b=TnEv8sK/+vDciXfwzZ6ARtykWx0szUB9cwOVDljv7sMigOzA5g7vKM7sM8qHDkKuSh 3M2mRc3jFBkDjW4VXueIfcatG9yqJHceJypAaq1FhMyzRNUBkFJoCaZp2fCle4yFJcRl huEOX2GHbN4178CkFMhw+DM2t8enjrcGaWyPHi6HiTxcI54jEdC2qNedfkPbnMMiYxmD BTgXffgW3Gv/L4ZmZLWoevw+PrDA6Bps2AW0/K12uYYiZrcjy/z6elunuCMSXEN2ZQkh cWehCmCaAHjU2tJxbsLsZNkFnrt7jQdjEqs8OyjXt8vG3KW/9SvEbph8+GdEIuoC3TqL pRqg== X-Gm-Message-State: AN3rC/58fmTL42k5cqSQFlt77NiHaPXMrLLRaYeHs3iFjq8b77kzeYlHgdrEk/btNuPbzw== X-Received: by 10.223.170.70 with SMTP id q6mr3661605wrd.162.1492006812880; Wed, 12 Apr 2017 07:20:12 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:8b2:dc00:6c54:829e:388a:2472]) by smtp.gmail.com with ESMTPSA id j2sm8402914wrd.28.2017.04.12.07.20.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 07:20:12 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH 3/3] simplify casts bool -> int -> bool Date: Wed, 12 Apr 2017 16:18:02 +0200 Message-Id: <20170412141802.81231-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170412141802.81231-1-luc.vanoostenryck@gmail.com> References: <20170412141802.81231-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 Because of C's integer promotion, in code like 'a == 0', the operand 'a' must be promoted to int, which result in following linearization if the type of 'a' was _Bool: cast.32 %t <- (1) %a setne.32 %r <- %t, $0 While required by the standard, this promotion is unneeded in the given situation. Change this by simplifying away such casts. Signed-off-by: Luc Van Oostenryck --- simplify.c | 18 +++++++++++++++++- validation/optim/bool-int-bool.c | 12 ++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 validation/optim/bool-int-bool.c diff --git a/simplify.c b/simplify.c index 775c1e2dd..96448a666 100644 --- a/simplify.c +++ b/simplify.c @@ -445,9 +445,25 @@ static int simplify_seteq_setne(struct instruction *insn, long long value) remove_usage(old, &insn->src1); return REPEAT_CSE; + case OP_CAST: + if (def->orig_type->bit_size != 1) + break; + + // Convert: + // cast.n %t <- (1) %a + // setne.m %r <- %t, $0 + // into: + // ... + // setne.m %r <- %a, $0 + // and similar for setne/eq ... 0/1 + use_pseudo(insn, def->src1, &insn->src1); + remove_usage(old, &insn->src1); + return REPEAT_CSE; + default: - return 0; + break; } + return 0; } static int simplify_constant_rightside(struct instruction *insn) diff --git a/validation/optim/bool-int-bool.c b/validation/optim/bool-int-bool.c new file mode 100644 index 000000000..de34a68bb --- /dev/null +++ b/validation/optim/bool-int-bool.c @@ -0,0 +1,12 @@ +_Bool beq0(_Bool a) { return (a == 0); } +_Bool beq1(_Bool a) { return (a == 1); } +_Bool bne0(_Bool a) { return (a != 0); } +_Bool bne1(_Bool a) { return (a != 1); } + +/* + * check-name: bool - int - bool constants + * check-command: test-linearize -Wno-decl $file + * + * check-output-ignore + * check-output-excludes: cast\\. + */