From patchwork Sun Apr 18 15:32:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12210193 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40D76C433ED for ; Sun, 18 Apr 2021 15:32:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 13B14610C7 for ; Sun, 18 Apr 2021 15:32:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231578AbhDRPdO (ORCPT ); Sun, 18 Apr 2021 11:33:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229998AbhDRPdO (ORCPT ); Sun, 18 Apr 2021 11:33:14 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE0BDC061760 for ; Sun, 18 Apr 2021 08:32:45 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id w3so49152530ejc.4 for ; Sun, 18 Apr 2021 08:32:45 -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 :mime-version:content-transfer-encoding; bh=7mTDPMx1aYnlOGqFSax2OjMQRk5DqmfeLh7StN5YDao=; b=AYyXUBCcAkNlhSYgaQQpX4jt4zH+4iPY23zkCGcYj5onmv7abAlfJ+Tu1ob2Lu/nb4 QsDH0rs3Cm1DNlyRFGuQKG59A+RadmKJ43v7IiLcaiuVqt1m+q+tLQ1/hL5B1ywOkO4D g8sJkMevzUS5j3/s9NBzgxTL4dLyFF/K9AMtTq2NUho0oPgi3+3wDbwz5LEVGb1WVabK 2scuJpLTnkmVW/GW04JD0RqLK0fjJEjhrAStOnS6y8R8L7dP5ut+B44oAyoAWI8emmg8 ZskDRrJNJNhxUzmsBq+N8cnuIn28fUrwDsDlBTge9x63KDbnr2saHbb+8XU/WYyWoUl7 CPGw== 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:mime-version:content-transfer-encoding; bh=7mTDPMx1aYnlOGqFSax2OjMQRk5DqmfeLh7StN5YDao=; b=qt1dCHgeMDu2bcyjewNpbptN+KveKjLUoguRbImaizZ5gC5gjvulzL3sndvOxbsm/1 nmqk3d7dOYANPaOgSa+wE19UYL7Lat4h/8P1ZPZXIHnjNpvVKWQ4ZGOrFSSJSlx85PYG 1pePMm0g6M3wH5E/PmdSECUbWSqZv/O0yTdEJtVsj9UEtoaXrpuppiz8hptxTI+nBvwN NmfR3jmezsPzUaSvo6aVSc02tdqBdObFvv5O2gw7YJpBNkvXBeNYSnusTn/g8c8lzujB 0aAe2QS7FN7loK5ezYYjpSfYL9DE0BVx4Lft9ReME2XXokF4I9cesUhwvPmTh+6vRZhD pmiQ== X-Gm-Message-State: AOAM530BOGL+9PJdgct3fJVkXfbQ6esAaub0YqlrYMD2IDGqWGS2YAmC +uSMxV3GdiYHpKzoVa4IcaUkuEoR14Q= X-Google-Smtp-Source: ABdhPJwmW/etaUp8vOnoZZNXQ31OuEQ6Bq4V4FiecS+Z2zeIyIVDzGTyHSH8RU7SL+ZGX+7P/a64AA== X-Received: by 2002:a17:906:b01:: with SMTP id u1mr18192508ejg.310.1618759964502; Sun, 18 Apr 2021 08:32:44 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:959c:c85:d486:38cf]) by smtp.gmail.com with ESMTPSA id k9sm8632888eje.102.2021.04.18.08.32.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Apr 2021 08:32:44 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [SPARSE 1/4] canonicalize constant signed compares toward zero Date: Sun, 18 Apr 2021 17:32:30 +0200 Message-Id: <20210418153233.45234-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> References: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Currently, signed compares against a constant are canonicalized toward the smallest possible constant. So, the following canonicalization are done: x < 256 --> x <= 255 x < -2047 --> x <= -2048 This has two advantages: 1) it maximalizes the number of constants possible for a given bit size. 2) it allows to remove all < and all >= But it has also a serious disadvantages: a simple comparison against zero, like: x >= 0 is canonicalized into: x > -1 Which can be more costly for some architectures if translated as such , is also less readable than the version using 0 and is also sometimes quite more complicated to match in some simplification patterns. So, canonicalize it using 'towards 0' / using the smallest constant in absolute value. Signed-off-by: Luc Van Oostenryck --- simplify.c | 34 +++++++++--- validation/optim/canonical-cmp-zero.c | 74 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 validation/optim/canonical-cmp-zero.c diff --git a/simplify.c b/simplify.c index 9e3514d838a9..e0e4f9ebcba9 100644 --- a/simplify.c +++ b/simplify.c @@ -1178,38 +1178,52 @@ static int simplify_compare_constant(struct instruction *insn, long long value) switch (insn->opcode) { case OP_SET_LT: + if (!value) + break; if (value == sign_bit(size)) // (x < SMIN) --> 0 return replace_with_pseudo(insn, value_pseudo(0)); if (value == sign_mask(size)) // (x < SMAX) --> (x != SMAX) return replace_opcode(insn, OP_SET_NE); if (value == sign_bit(size) + 1)// (x < SMIN + 1) --> (x == SMIN) return replace_binop_value(insn, OP_SET_EQ, sign_bit(size)); - changed |= replace_binop_value(insn, OP_SET_LE, (value - 1) & bits); + if (!(value & sign_bit(size))) + changed |= replace_binop_value(insn, OP_SET_LE, (value - 1) & bits); break; case OP_SET_LE: + if (!value) + break; if (value == sign_mask(size)) // (x <= SMAX) --> 1 return replace_with_pseudo(insn, value_pseudo(1)); if (value == sign_bit(size)) // (x <= SMIN) --> (x == SMIN) return replace_opcode(insn, OP_SET_EQ); if (value == sign_mask(size) - 1) // (x <= SMAX - 1) --> (x != SMAX) return replace_binop_value(insn, OP_SET_NE, sign_mask(size)); + if (value & sign_bit(size)) + changed |= replace_binop_value(insn, OP_SET_LT, (value + 1) & bits); break; case OP_SET_GE: + if (!value) + break; if (value == sign_bit(size)) // (x >= SMIN) --> 1 return replace_with_pseudo(insn, value_pseudo(1)); if (value == sign_mask(size)) // (x >= SMAX) --> (x == SMAX) return replace_opcode(insn, OP_SET_EQ); if (value == sign_bit(size) + 1)// (x >= SMIN + 1) --> (x != SMIN) return replace_binop_value(insn, OP_SET_NE, sign_bit(size)); - changed |= replace_binop_value(insn, OP_SET_GT, (value - 1) & bits); + if (!(value & sign_bit(size))) + changed |= replace_binop_value(insn, OP_SET_GT, (value - 1) & bits); break; case OP_SET_GT: + if (!value) + break; if (value == sign_mask(size)) // (x > SMAX) --> 0 return replace_with_pseudo(insn, value_pseudo(0)); if (value == sign_bit(size)) // (x > SMIN) --> (x != SMIN) return replace_opcode(insn, OP_SET_NE); if (value == sign_mask(size) - 1) // (x > SMAX - 1) --> (x == SMAX) return replace_binop_value(insn, OP_SET_EQ, sign_mask(size)); + if (value & sign_bit(size)) + changed |= replace_binop_value(insn, OP_SET_GE, (value + 1) & bits); break; case OP_SET_B: @@ -1271,8 +1285,10 @@ static int simplify_compare_constant(struct instruction *insn, long long value) if ((value & bits) != value) return replace_with_value(insn, 1); break; - case OP_SET_LE: + case OP_SET_LE: case OP_SET_LT: value = sign_extend(value, def->size); + if (insn->opcode == OP_SET_LT) + value -= 1; if (bits & sign_bit(def->size)) break; if (value < 0) @@ -1282,8 +1298,10 @@ static int simplify_compare_constant(struct instruction *insn, long long value) if (value == 0) return replace_opcode(insn, OP_SET_EQ); break; - case OP_SET_GT: + case OP_SET_GT: case OP_SET_GE: value = sign_extend(value, def->size); + if (insn->opcode == OP_SET_GE) + value -= 1; if (bits & sign_bit(def->size)) break; if (value < 0) @@ -1340,16 +1358,20 @@ static int simplify_compare_constant(struct instruction *insn, long long value) if (bits >= value) return replace_with_value(insn, 1); break; + case OP_SET_LT: + value -= 1; case OP_SET_LE: - value = sign_extend(value, def->size); if (bits & sign_bit(def->size)) { + value = sign_extend(value, def->size); if (value >= -1) return replace_with_value(insn, 1); } break; + case OP_SET_GE: + value -= 1; case OP_SET_GT: - value = sign_extend(value, def->size); if (bits & sign_bit(def->size)) { + value = sign_extend(value, def->size); if (value >= -1) return replace_with_value(insn, 0); } diff --git a/validation/optim/canonical-cmp-zero.c b/validation/optim/canonical-cmp-zero.c new file mode 100644 index 000000000000..e01a00e637b6 --- /dev/null +++ b/validation/optim/canonical-cmp-zero.c @@ -0,0 +1,74 @@ +int f00(int x) { return x >= 0; } +int f01(int x) { return x > -1; } +int f02(int x) { return x < 1; } +int f03(int x) { return x <= 0; } + +int f10(int x) { return x < 16; } +int f11(int x) { return x <= 15; } + +int f20(int x) { return x > -9; } +int f21(int x) { return x >= -8; } + +/* + * check-name: canonical-cmp-zero + * check-command: test-linearize -Wno-decl $file + * + * check-output-start +f00: +.L0: + + setge.32 %r2 <- %arg1, $0 + ret.32 %r2 + + +f01: +.L2: + + setge.32 %r5 <- %arg1, $0 + ret.32 %r5 + + +f02: +.L4: + + setle.32 %r8 <- %arg1, $0 + ret.32 %r8 + + +f03: +.L6: + + setle.32 %r11 <- %arg1, $0 + ret.32 %r11 + + +f10: +.L8: + + setle.32 %r14 <- %arg1, $15 + ret.32 %r14 + + +f11: +.L10: + + setle.32 %r17 <- %arg1, $15 + ret.32 %r17 + + +f20: +.L12: + + setge.32 %r20 <- %arg1, $0xfffffff8 + ret.32 %r20 + + +f21: +.L14: + + setge.32 %r23 <- %arg1, $0xfffffff8 + ret.32 %r23 + + + * check-output-end + */ From patchwork Sun Apr 18 15:32:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12210197 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 585FAC433B4 for ; Sun, 18 Apr 2021 15:32:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 35FE3610C7 for ; Sun, 18 Apr 2021 15:32:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231552AbhDRPdT (ORCPT ); Sun, 18 Apr 2021 11:33:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231600AbhDRPdQ (ORCPT ); Sun, 18 Apr 2021 11:33:16 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C2ABC061760 for ; Sun, 18 Apr 2021 08:32:46 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id x4so37768052edd.2 for ; Sun, 18 Apr 2021 08:32:46 -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 :mime-version:content-transfer-encoding; bh=hEy7q63scp3B3My+WUJqQUYS5JRHUfYszfzjUaKEF7Q=; b=lw0k0M32g1fiIYOvuR1rTw6xYtlLHjPfSOpnJHj/VNCCumXJFi1o7KgAMY056bJIuq uQ0ZOXR8b7iCyqsLzvpDTB+gR4DjM6bGusvn+wgg8gGwfhhlD52/e4Dy9+ezMYGB3wof FI5hBCaet52QlSBLe7ypXaJ6iatSghlMvXpiCsMxD+JUFESSHV8Ykk+nzEP4Y3cY1eW5 b5PmRxFLtkNDrO3BuG36IJ8c9xWtfNfqBq5Oyx+V57u9MiFwcSZ0pg/74lKvVoPzWgNX /F5uwlg1SY6ifXd1B0lkKfkxFyyTZ0xYCBbzTI8ZKwioXGafE1tHgM4Xy8f4BhkXCp7q VQig== 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:mime-version:content-transfer-encoding; bh=hEy7q63scp3B3My+WUJqQUYS5JRHUfYszfzjUaKEF7Q=; b=Pk7zujdgglt3BSHLcG30HgYakijL3RSy5o/kvi8Nc2jSwI6VNqFgEf/d6G9EpJRPST CleL4LU6JLmuDQzINLBWkYo9a+zY3yNh0tUqp0WgQC0EUQj06Ca9xs7HyHW/whGBcVrs OFEttJYMbTZFOspzFAuIPdRZMN3cgnp50vo6LtV83etJ3Dxl24/Sdm4qBV00zQ+lU1H8 3qebWcnkGrrNVpZF6qIWgUMOZT0N4gcPqU9x6p+tTBV5bqbpNiyF7FNljpqy6j+5eWJ8 uCDR0Z3FH5rGqBhQq2/AgmGsjhiTqqFd4P86eD9QLZzjDGRPcSKwAgnfpoLfqVokjvV+ /dJw== X-Gm-Message-State: AOAM530XjHBIfef5fb3FhY/cpU1oPFzEpjlWey3wLkCe6pQO2DLsHJgv X9ag60zFEifUIHLOBI5yMARebhNvD2c= X-Google-Smtp-Source: ABdhPJyVnz8pw5+1QQHMySf0CrNJ4UoGBDABUYNTu/K47kbNv6LaWxJLG+2sDQ9nv4vsWmuaocZxRw== X-Received: by 2002:aa7:d543:: with SMTP id u3mr8401571edr.26.1618759965291; Sun, 18 Apr 2021 08:32:45 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:959c:c85:d486:38cf]) by smtp.gmail.com with ESMTPSA id k9sm8632888eje.102.2021.04.18.08.32.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Apr 2021 08:32:44 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [SPARSE 2/4] add testcases for AND(x > 0, x <= C) --> x u<= C Date: Sun, 18 Apr 2021 17:32:31 +0200 Message-Id: <20210418153233.45234-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> References: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Signed-off-by: Luc Van Oostenryck --- validation/optim/range-check1.c | 17 +++++++++++++++++ validation/optim/range-check2.c | 15 +++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 validation/optim/range-check1.c create mode 100644 validation/optim/range-check2.c diff --git a/validation/optim/range-check1.c b/validation/optim/range-check1.c new file mode 100644 index 000000000000..82b939917da4 --- /dev/null +++ b/validation/optim/range-check1.c @@ -0,0 +1,17 @@ +#define N 1024 + +_Bool check_ok(long i) +{ + return i >= 0 && i < N; +} + +/* + * check-name: range-check1 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-contains: setbe\\..*0x3ff + * check-output-excludes: set[lga][te]\\. + * check-output-excludes: set[ab]\\. + */ diff --git a/validation/optim/range-check2.c b/validation/optim/range-check2.c new file mode 100644 index 000000000000..f565b84ea9db --- /dev/null +++ b/validation/optim/range-check2.c @@ -0,0 +1,15 @@ +#define N 1024 + +_Bool check_ok(int i) +{ + return (i >= 0 && i < N) == (((unsigned int)i) < N); +} + +/* + * check-name: range-check2 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ From patchwork Sun Apr 18 15:32:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12210195 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6771C433ED for ; Sun, 18 Apr 2021 15:32:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B188C610CB for ; Sun, 18 Apr 2021 15:32:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231727AbhDRPdT (ORCPT ); Sun, 18 Apr 2021 11:33:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231552AbhDRPdQ (ORCPT ); Sun, 18 Apr 2021 11:33:16 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BA37C061761 for ; Sun, 18 Apr 2021 08:32:47 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id u21so49127300ejo.13 for ; Sun, 18 Apr 2021 08:32:47 -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 :mime-version:content-transfer-encoding; bh=YwX/4OayGePcRu0I3LIpEbeLTjaI1qAIASJxYZHJl8k=; b=S48vTDJ4X2fgZ8a3QKdDMy2rzIYWEgMWocZTgmaUgz6MeA3bD00OIMj5zD3JdRDFnP 5h3gamPb9gbeypkNhdXCAGnEpiTLQmzwOEDMWb2I6xpe6MOv0KvfSujudcsB3jqP0TB9 cq+4wS09n7FBapwHz1adnZThmoPtp37T08cMx04k06GCOmBDiu5817inSzBhTFmX/QRv YZsM/7ZnoISrAd6soy2OGBpKt8u9KZ5eVfEavothNyHztNWH9wq1a0SGQpWpuHQ7QDHK 2gwaLeG/g/bUYmtj2IlHujqBNvGFkXaW+LG5KaBqwTzoxGVkx2xuhCYPdEEkRQNd/gV7 Zkww== 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:mime-version:content-transfer-encoding; bh=YwX/4OayGePcRu0I3LIpEbeLTjaI1qAIASJxYZHJl8k=; b=cGwG9aCGqqnt2imKuWMN/nqtPmveuwrvZEF2B/r3rn9ZO6nYpLZAZyjleTAfHLt2Ca oIuOJqfZcfDM8g/udnR02xQU7Dck4pFgnflnF8Ri2p9uMZtBKX44HBU5sHp7ctXnAxso OMFYX/0HCux8rOClIT7/08uwrjds/F5mWlcgvLdVFIs2UqILRFQkLuTpO7oKf/Mh//Ah Jd5lCw6+2DneH0g/ySeYo254WMpEg2NG/LAXxiMWKARvn6w0Jl1OCZKA/+LNA7I/CaqW qM8IiEn50UV8bX+/3MKOsz2Xgc6Z5tLL5AQp258utiBshX0yoRMeQnowdduErZ5dgwry IPOQ== X-Gm-Message-State: AOAM531+CzTrD/N5LA2zglGPGQOCo0apND2QCMoEdcINDUhRWDQ4k8Ew onWhr0NCozI1Ieq9QnkIG3Ub2Vz7FMY= X-Google-Smtp-Source: ABdhPJwGSZ/CkDHq4xcpc6IDgKEuHx4g0AXnDLSS9ZZc/jvT7hh83IH80dO+RNOGoahrWEwDFoaCvw== X-Received: by 2002:a17:907:20f2:: with SMTP id rh18mr17805260ejb.466.1618759966067; Sun, 18 Apr 2021 08:32:46 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:959c:c85:d486:38cf]) by smtp.gmail.com with ESMTPSA id k9sm8632888eje.102.2021.04.18.08.32.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Apr 2021 08:32:45 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [SPARSE 3/4] add helper is_positive() Date: Sun, 18 Apr 2021 17:32:32 +0200 Message-Id: <20210418153233.45234-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> References: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Add a small helper to test if a pseudo is a positive (= non-negative) constant (for a given bitsize). It's meant to make some conditions more readable. Signed-off-by: Luc Van Oostenryck --- linearize.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/linearize.h b/linearize.h index 7909b01f29c5..65f54c28a7d6 100644 --- a/linearize.h +++ b/linearize.h @@ -58,6 +58,11 @@ static inline bool is_nonzero(pseudo_t pseudo) return pseudo->type == PSEUDO_VAL && pseudo->value != 0; } +static inline bool is_positive(pseudo_t pseudo, unsigned size) +{ + return pseudo->type == PSEUDO_VAL && !(pseudo->value & sign_bit(size)); +} + struct multijmp { struct basic_block *target; From patchwork Sun Apr 18 15:32:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 12210199 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9275C43461 for ; Sun, 18 Apr 2021 15:32:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B288B610CB for ; Sun, 18 Apr 2021 15:32:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231600AbhDRPdT (ORCPT ); Sun, 18 Apr 2021 11:33:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231691AbhDRPdQ (ORCPT ); Sun, 18 Apr 2021 11:33:16 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 312D7C06174A for ; Sun, 18 Apr 2021 08:32:48 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id e14so49074673ejz.11 for ; Sun, 18 Apr 2021 08:32:48 -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 :mime-version:content-transfer-encoding; bh=HlUpvbNldbTvl7Ip9LFRTzGxZeFNlUyYCGeSWyyELMQ=; b=YWVl5rlKaElZ9r9QZEe1O8ziezkMqMPlqSMA4+rZKyoBB3qKSzhyg6meiXlZPO3iUc UzldeW0RqnbdqeFICNM6i9YRVLZwsxNhIaoyoedxy937ERxu0uhY6HSKL1Cr1UaPP7Fq bHoDtlO8cbu3LbagL5F2uKYJddbQRVew3iL7DjCl2CP28MWCqZUIoynk3vSbaIkfDrhf NkPIpjp2gIVIfQ6QYPuC5NkDuVb8j0KOKCy1NhhsuL4dtKOiowz7v60WwQcpfGhDSAon IwMZ6TCgbSDirHkBuCu6xwxbmGy9r8KvFSZq3Nz8X3OqVMziopq5kOTv5cKho2s6bj9N Wy9A== 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:mime-version:content-transfer-encoding; bh=HlUpvbNldbTvl7Ip9LFRTzGxZeFNlUyYCGeSWyyELMQ=; b=Hwn5LdX9N/NDRbUToqBAKy4vYWz0eZLbAQXYwZNj23DXUKDoJmp8lu/lD1UmW4CtRV UATsp/9Ve+JiAVefEU4khQDn1ma17xyEi8Dt1YV0yTOeVJemzXOIotIdin/LexCulOAe o1NzIXEpS7r52/Is6LM16h+fA9KwneHojdWfoZ/MHPz889S0GSerWU1mANjLsx1XLhhE haoZK5VDEYRfsh0OrHGlZTRZblC8uACK5XXVJ3/Jev5PW5a/3oqrupSXQVb7J9LGgEtB GqgBTpFm5gENKn6s0EyZTVHw2Xtqp9VwIFC9LytN0/+/CILTITtK1tX4+Z/yoZrUBaDN Vdvg== X-Gm-Message-State: AOAM531OZxJFjqFpgoXg0YxWknufQH+R0FM5yT3GXHkp2qmAPKRD2tb2 BdPcD6nU5WLsTHjPwcQzCTan+CG67c0= X-Google-Smtp-Source: ABdhPJyoDI7MvFh28UruUXkawW7QjWgaomKXBDOrjDCbCZt4v0Ftvmj8DdGB7UmgDMldtXjXB3UHiQ== X-Received: by 2002:a17:906:1313:: with SMTP id w19mr17773606ejb.64.1618759966999; Sun, 18 Apr 2021 08:32:46 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:959c:c85:d486:38cf]) by smtp.gmail.com with ESMTPSA id k9sm8632888eje.102.2021.04.18.08.32.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Apr 2021 08:32:46 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Linus Torvalds , Luc Van Oostenryck Subject: [SPARSE 4/4] simplify AND(x >= 0, x < C) --> (unsigned)x < C Date: Sun, 18 Apr 2021 17:32:33 +0200 Message-Id: <20210418153233.45234-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> References: <20210418153233.45234-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Such compares with a signed value are relatively common and can be easily be simplified into a single unsigned compare. So, do it. Note: This simplification triggers only 27 times in a x86-64 defconfig kernel. I expected more but I suppose it's because most checks aren't done against a constant or are done with unsigned values. Signed-off-by: Luc Van Oostenryck --- simplify.c | 11 +++++++++++ validation/optim/range-check1.c | 1 - validation/optim/range-check2.c | 1 - 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/simplify.c b/simplify.c index e0e4f9ebcba9..0a29db189b55 100644 --- a/simplify.c +++ b/simplify.c @@ -1920,6 +1920,17 @@ static int simplify_and_one_side(struct instruction *insn, pseudo_t *p1, pseudo_ if (def->src1 == defr->src1 && def->src2 == defr->src2) return replace_with_value(insn, 0); } + if (def->opcode == OP_SET_GE && is_zero(def->src2)) { + switch (DEF_OPCODE(defr, *p2)) { + case OP_SET_LE: + if (!is_positive(defr->src2, defr->itype->bit_size)) + break; + // (x >= 0) && (x <= C) --> (x u<= C) + insn->itype = defr->itype; + replace_binop(insn, OP_SET_BE, &insn->src1, defr->src1, &insn->src2, defr->src2); + return REPEAT_CSE; + } + } break; case OP_OR: if (DEF_OPCODE(defr, *p2) == OP_OR) { diff --git a/validation/optim/range-check1.c b/validation/optim/range-check1.c index 82b939917da4..358da045c456 100644 --- a/validation/optim/range-check1.c +++ b/validation/optim/range-check1.c @@ -8,7 +8,6 @@ _Bool check_ok(long i) /* * check-name: range-check1 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-contains: setbe\\..*0x3ff diff --git a/validation/optim/range-check2.c b/validation/optim/range-check2.c index f565b84ea9db..69c01b9d36d5 100644 --- a/validation/optim/range-check2.c +++ b/validation/optim/range-check2.c @@ -8,7 +8,6 @@ _Bool check_ok(int i) /* * check-name: range-check2 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1