From patchwork Fri Nov 27 22:25:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937289 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.7 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 5532CC3E8C5 for ; Fri, 27 Nov 2020 22:28:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0419221D46 for ; Fri, 27 Nov 2020 22:28:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oYAq0y3Z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730724AbgK0W1x (ORCPT ); Fri, 27 Nov 2020 17:27:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730732AbgK0W10 (ORCPT ); Fri, 27 Nov 2020 17:27:26 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91B09C0613D2 for ; Fri, 27 Nov 2020 14:27:25 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id g25so3228808wmh.1 for ; Fri, 27 Nov 2020 14:27:25 -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 :mime-version:content-transfer-encoding; bh=LN6gco1zIlqtyCTgrYmmvxgCDWzIy9ZAVhewoEZKPDE=; b=oYAq0y3ZdZmjvH8LYghSQyhEd9JrjjHH3l8VB7/vpMnSpfhBMZQhOoj8K0FegoJtxB 7cwuYGmrsTwdLkHEeSoJvN1HntPhagY0OcgUChcC7LQOhcXzm6BqJgq0p/z6RY79+CIR 7vGEIiUzR5Zuc2yj7en2HEWS51sDKRoguYkHcmJVqu5YQ3dJ/ekPRDLXFQWXeTdTpris pGTubV9YAPolueIkq8g0H7/eLJFme3lJ2O5JP9EQLhBS9NDs6WJnqP5LcdfkszrFXqqf ol6d8uZ6+eVTbZErU4OpW7Nz4GjYrz6D+L+JJrfcA69LOpLrLd2InYX+1KmJVYyrw4Z5 DZfQ== 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=LN6gco1zIlqtyCTgrYmmvxgCDWzIy9ZAVhewoEZKPDE=; b=G+VLw/qhP4jyAV+ngMrbAkpYkJypKOrzEprnD6lMc0X8fIbDVNxQl7Ut/59yZFLNDI YamubRDSGjbrPqJ8li6sM4CiQlF30pYfxDH0YvqpvKFswZMpq5TZB4CYRZsM4Epy/QBq 5vqVS0XOCcFdoH4CHZUWtYJ4P6IZKSsmyOlkdueWmTczvYwxkopK7N94w9RD1Qtay63c 6yGZhTTL2g6lyOJw8rx3PGaaF1sswWxXnMu+3aLoooc0u3HhTff0H/XO6PocM8WEULj1 eEx16PRb31Wai+a0MvVvgdARa739W3gzvNAqSEOTQRXzNCYPP5pCuqZNczbcLFQ6WXcS Lhhg== X-Gm-Message-State: AOAM532vU/Qy00eVMXQK+8G2TanqN8wU706jIKLOqu1m/DXrqiCk9rEp ZToRaERHUuqzX6R2oc6UDgZiXIHs6wg= X-Google-Smtp-Source: ABdhPJxUHNLO4hASpyBYJf6z6/EoOhsW2KcSR0wD4S4N63P/WRbAjnyF3dV5tFcpvX+9YD3Yrp7KQg== X-Received: by 2002:a1c:2605:: with SMTP id m5mr10118172wmm.111.1606516044063; Fri, 27 Nov 2020 14:27:24 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:23 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 1/6] add testscases for 'bits translation' optimization Date: Fri, 27 Nov 2020 23:25:10 +0100 Message-Id: <20201127222516.44915-2-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Add some testcase related to the simplification of expressions like: if (val1 & BIT1) val2 |= BIT2; into val2 |= (val1 & BIT1) {SHL/LSR} |BIT2-BIT1|; Signed-off-by: Luc Van Oostenryck --- validation/optim/fact-select01.c | 26 ++++++++++++++++++++++++++ validation/optim/select-and-shift.c | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 validation/optim/fact-select01.c create mode 100644 validation/optim/select-and-shift.c diff --git a/validation/optim/fact-select01.c b/validation/optim/fact-select01.c new file mode 100644 index 000000000000..ef4e5e89a7be --- /dev/null +++ b/validation/optim/fact-select01.c @@ -0,0 +1,26 @@ +int add_yx_y(int p, int x, int y) { return (p ? (y+x) : y) == ((p ? x : 0) + y); } +int add_xy_y(int p, int y, int x) { return (p ? (x+y) : y) == ((p ? x : 0) + y); } +int add_xy_x(int p, int x, int y) { return (p ? (x+y) : x) == ((p ? y : 0) + x); } +int add_yx_x(int p, int y, int x) { return (p ? (y+x) : x) == ((p ? y : 0) + x); } +int add_y_yx(int p, int x, int y) { return (p ? y : (y+x)) == ((p ? 0 : x) + y); } + +int ior_yx_y(int p, int x, int y) { return (p ? (y|x) : y) == ((p ? x : 0) | y); } +int ior_xy_y(int p, int y, int x) { return (p ? (x|y) : y) == ((p ? x : 0) | y); } +int ior_xy_x(int p, int x, int y) { return (p ? (x|y) : x) == ((p ? y : 0) | x); } +int ior_yx_x(int p, int y, int x) { return (p ? (y|x) : x) == ((p ? y : 0) | x); } +int ior_y_yx(int p, int x, int y) { return (p ? y : (y|x)) == ((p ? 0 : x) | y); } + +int xor_yx_y(int p, int x, int y) { return (p ? (y^x) : y) == ((p ? x : 0) ^ y); } +int xor_xy_y(int p, int y, int x) { return (p ? (x^y) : y) == ((p ? x : 0) ^ y); } +int xor_xy_x(int p, int x, int y) { return (p ? (x^y) : x) == ((p ? y : 0) ^ x); } +int xor_yx_x(int p, int y, int x) { return (p ? (y^x) : x) == ((p ? y : 0) ^ x); } +int xor_y_yx(int p, int x, int y) { return (p ? y : (y^x)) == ((p ? 0 : x) ^ y); } + +/* + * check-name: fact-select01 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/select-and-shift.c b/validation/optim/select-and-shift.c new file mode 100644 index 000000000000..fbe044c7cb44 --- /dev/null +++ b/validation/optim/select-and-shift.c @@ -0,0 +1,18 @@ +#define S1 2 +#define S2 5 +#define S (S2 - S1) + +#define A (1 << S1) +#define B (1 << S2) + +int foo(int p) { return ((p & A) ? B : 0) == ((((unsigned)p) & A) << S); } +int bar(int p) { return ((p & B) ? A : 0) == ((((unsigned)p) & B) >> S); } + +/* + * check-name: select-and-shift + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ From patchwork Fri Nov 27 22:25:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937305 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.7 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 1EE2FC3E8C5 for ; Fri, 27 Nov 2020 22:33:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE51C22228 for ; Fri, 27 Nov 2020 22:33:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Dg3U1hd7" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730724AbgK0Wcw (ORCPT ); Fri, 27 Nov 2020 17:32:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731032AbgK0W10 (ORCPT ); Fri, 27 Nov 2020 17:27:26 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52A77C0613D4 for ; Fri, 27 Nov 2020 14:27:26 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id h21so7727350wmb.2 for ; Fri, 27 Nov 2020 14:27:26 -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 :mime-version:content-transfer-encoding; bh=ZdFsuFEoV7kfT1dhDUS8+TraI8Fo/krZo/2ACS2fyhc=; b=Dg3U1hd7czbmNJT2U2KivQyl4WxrZPENotn+Qnel8iunEHlBonkTBUUGpTYoVNbUqJ 5DI4myvATwPPhDrvO2hyyauWncZ0aNafMzbQa9BzQnsGV2Cu+QCgFPvucogu6dtOemmp viRv4loT0SyI0cZBdviluJWgl47j9DA/ENJUt+Bcfo8N/p0aVedJY+eXf7D26qT+jxGD nuwedCpzN72K9hT19AlqMvQxrClBlvboIHt7qHKpujnlIzTY+ao8WR7/0ZJ7aI6odFfw p5i5eFPenHFKZJK4xiLkRQDEYhj5n+/7u3i2K6rkz/+R0TZ0yVdHLJJZZ1W1zU39MHwj NJEA== 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=ZdFsuFEoV7kfT1dhDUS8+TraI8Fo/krZo/2ACS2fyhc=; b=e/Kom/+UUz+LEq9hDjNdfX3bi6DbWkCqKEKusjGaxvt5fJDq2V5R+zQHWWOz7/+vZt KPzQ4TX7VrbjukhOdtK0aN2a6KE+T4r1Vs4E+CB7x7Pxhyp3m3GHc3SLmIt3awxE0JWa aIMKPXvyy4EIZS0uJ+B3Xhp+4AMqJNfbe8hv/JNHYfpNfwJTkprk8tBpFdN6dP6ly/1d B5YKHEZ3y3aPh2la6nWJ+xjEJfwokFEJvqY5cPSwi6MYUSNfFphWXeoMydUp3krTbMVu TAQ5K2QhUH5SwrAPziGSpEGqrFJYEh7tCsG91prOah2Ta9JpRJYQJcmoUboLZ31ea912 92yg== X-Gm-Message-State: AOAM532Wi61MBluszPS/jdjRnxfco3ciuzlzjCuiG6jdGwoaX3K9P3xc 1NbeB8JO1xg0bCB0F5Yci3KZQ59RBAM= X-Google-Smtp-Source: ABdhPJyL2bQUFDRvwrPAq7WE/yW8c1ghv6K6NDTpgTU5T28v8f3byrX/S3oicMJ/q4NYfBfC1wnyZQ== X-Received: by 2002:a1c:41c4:: with SMTP id o187mr11988422wma.48.1606516044812; Fri, 27 Nov 2020 14:27:24 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:24 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 2/6] factorize SEL(x, OP(y,z), y) into OP(SEL(x, z, 0), y) Date: Fri, 27 Nov 2020 23:25:11 +0100 Message-Id: <20201127222516.44915-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org 'Factorize' and expression like: x ? (y | z) : y; into (x ? z : 0) | y; and some positional variants as well as replacing '|' by '+' or '^'. Note: it's not very clear if this is really an improvement but it allows some very nice simplification of 'bits translations'. Note: the same can be done for others operations, for example it can be done for '&' if '0' (the neuter value for '|', '+' and '^') by '~0' (same with '*' and '1'). Signed-off-by: Luc Van Oostenryck --- simplify.c | 40 ++++++++++++++++++++++++++++++++ validation/optim/fact-select01.c | 1 - 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 89a064b93e85..d729b390ae76 100644 --- a/simplify.c +++ b/simplify.c @@ -554,6 +554,16 @@ static inline int swap_insn(struct instruction *out, struct instruction *in, pse return replace_insn_pair(out, in->opcode, in, out->opcode, a, b, c); } +/// +// create an instruction pair OUT(SELECT(a, b, c), d) +static int swap_select(struct instruction *out, struct instruction *in, pseudo_t a, pseudo_t b, pseudo_t c, pseudo_t d) +{ + use_pseudo(in, c, &in->src3); + swap_insn(out, in, a, b, d); + kill_use(&out->src3); + return REPEAT_CSE; +} + static inline int def_opcode(pseudo_t p) { if (p->type != PSEUDO_REG) @@ -2254,6 +2264,36 @@ static int simplify_select(struct instruction *insn) } break; } + + switch (DEF_OPCODE(def, src1)) { + case OP_ADD: case OP_OR: case OP_XOR: + if ((def->src1 == src2) && can_move_to(cond, def)) { + // SEL(x, OP(y,z), y) --> OP(SEL(x, z, 0), y) + swap_select(insn, def, cond, def->src2, value_pseudo(0), src2); + return REPEAT_CSE; + } + if ((def->src2 == src2) && can_move_to(cond, def)) { + // SEL(x, OP(z,y), y) --> OP(SEL(x, z, 0), y) + swap_select(insn, def, cond, def->src1, value_pseudo(0), src2); + return REPEAT_CSE; + } + break; + } + + switch (DEF_OPCODE(def, src2)) { + case OP_ADD: case OP_OR: case OP_XOR: + if ((def->src1 == src1) && can_move_to(cond, def)) { + // SEL(x, y, OP(y,z)) --> OP(SEL(x, 0, z), y) + swap_select(insn, def, cond, value_pseudo(0), def->src2, src1); + return REPEAT_CSE; + } + if ((def->src2 == src1) && can_move_to(cond, def)) { + // SEL(x, y, OP(z,y)) --> OP(SEL(x, 0, z), y) + swap_select(insn, def, cond, value_pseudo(0), def->src1, src1); + return REPEAT_CSE; + } + break; + } return 0; } diff --git a/validation/optim/fact-select01.c b/validation/optim/fact-select01.c index ef4e5e89a7be..9232fc908e34 100644 --- a/validation/optim/fact-select01.c +++ b/validation/optim/fact-select01.c @@ -19,7 +19,6 @@ int xor_y_yx(int p, int x, int y) { return (p ? y : (y^x)) == ((p ? 0 : x) ^ y); /* * check-name: fact-select01 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 From patchwork Fri Nov 27 22:25:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937303 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.7 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 3AFA7C3E8C5 for ; Fri, 27 Nov 2020 22:32:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DB92F22228 for ; Fri, 27 Nov 2020 22:32:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W1NdUbjO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726763AbgK0WcE (ORCPT ); Fri, 27 Nov 2020 17:32:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731378AbgK0W11 (ORCPT ); Fri, 27 Nov 2020 17:27:27 -0500 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29EAEC0617A7 for ; Fri, 27 Nov 2020 14:27:27 -0800 (PST) Received: by mail-wr1-x442.google.com with SMTP id 64so7002029wra.11 for ; Fri, 27 Nov 2020 14:27:27 -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 :mime-version:content-transfer-encoding; bh=VEVhyCx6rFVqcPZ2YTAJKtQ0As6CDeHLt7rTuGNimiI=; b=W1NdUbjO1rdF0S0SDDZFGYn85NoNBYz1HVml0RiXbjcuBVfPrrZ4Cx+3nRR+6w2nbW 5iNddFhUZY47CDFyy3ZRqqhreGP42SnXJMuz+2IBjRYW8181gGd7NoVyfN0Bt2dzXWcH zpQUl+XdnK0ETzwsvHf87aieU1YuFvS0XdbNHqIJzv3ak0fMaoSwG+8sRSaKrUMGIdWV NPglr+ad/Vx9bPkKjJJQz/qWD6AI8IWLE0IxJvtbpP4ME10xKyub0635OmY6wMjSfoSQ eerzl18DI0SZzshpxVNEo2WoIE93tmFpy68e9sxHupSqY4uQKPXXM3VZalW3XMjcYpph LyBw== 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=VEVhyCx6rFVqcPZ2YTAJKtQ0As6CDeHLt7rTuGNimiI=; b=ect3vPuySg7fchgxGsNLKLUZb6Kt5NymmOxvV5WQBqvRmtxuLJ+O0JAIoTYMGHujuq WbmJOLR1OHBrrFOw/bGB0xgy9VJtd077o2npt7EmgymMGynT6jyzBvdEOt75dbqIke5U sSSk4u8o1iRGxnz5DNRWllg5AuSV7H0vc6omkmUrel9FdF0mKI2b8LH7OlZuS0usp7HW qN/mPPdwI/qYlMBJFZO/Qgt5sQq8Op/f9VxTFBmel7Szhkm4hEesOMetLJuiDgPdMMg+ asS+EdpssGn6fYiEEbg9Xnep/41mri6P5XWiflFmaIqshyxjzxdfV2DmhsjhJ6BuFGt8 34gg== X-Gm-Message-State: AOAM5333Kzf2F0PH9pW0/KGMhgdIRhNKTM4dQ4TifGaIHk+uxu2hG0Cy OgpocLugmHQ5cAEzJFzftZ91kOOs5Dw= X-Google-Smtp-Source: ABdhPJxq30EnDCkiWrOVg6qez7UWcUV/13RG+pnwZilF1OgDcat9z7u0BOgITeNoVJn5Q9gSmWApnw== X-Received: by 2002:adf:de05:: with SMTP id b5mr13545841wrm.131.1606516045741; Fri, 27 Nov 2020 14:27:25 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:25 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 3/6] add helper is_power_of_2() Date: Fri, 27 Nov 2020 23:25:12 +0100 Message-Id: <20201127222516.44915-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Add is_power_of_2() to test if a value is a power of 2. Signed-off-by: Luc Van Oostenryck --- bits.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bits.h b/bits.h index c0dc952eaed9..63a663c248e4 100644 --- a/bits.h +++ b/bits.h @@ -58,4 +58,9 @@ static inline long long bits_extend(long long val, unsigned size, int is_signed) return val; } +static inline int is_power_of_2(long long val) +{ + return val && !(val & (val - 1)); +} + #endif From patchwork Fri Nov 27 22:25:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937299 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.7 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 8A4CCC3E8C5 for ; Fri, 27 Nov 2020 22:30:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4882021D46 for ; Fri, 27 Nov 2020 22:30:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G3cNNp5Y" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731355AbgK0WaU (ORCPT ); Fri, 27 Nov 2020 17:30:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731413AbgK0W12 (ORCPT ); Fri, 27 Nov 2020 17:27:28 -0500 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F36BC061A04 for ; Fri, 27 Nov 2020 14:27:28 -0800 (PST) Received: by mail-wr1-x442.google.com with SMTP id k14so7046807wrn.1 for ; Fri, 27 Nov 2020 14:27:28 -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 :mime-version:content-transfer-encoding; bh=fy1/8aHGLqmG6nr3Py2q0Ie8D12Oq5+K1Jwk8epTuy0=; b=G3cNNp5YbHrTWnHEkQORnLpB3gfhjfGdrLKBJ+s5PwfcI59k6sDrCeuXQVcwyLheBY YpOs2fIpTItnwDf2pptFS6KHmC1gNuriGshLa4H8QDmpA11nQNKPSmfF0ElvXKa6sRs8 livEdZJoAQIFh1LfsA/Fs3KyeGwYyAKqf9v2dIx98uIA1F/pJPl1zmPhiL1hXXpnqPW6 SOvKgJQM6SwoAonNrXGgNZWWUvKQpjyoxky2yhHJcpeaDHzW/MT1xiT7VGIVgi4S0OsD yyWsrwzvW05vsZMw8O7nkTCZyzjcbkZFUacVO3aVqUmyqE1k0ITw3nBKV5qxxm/EbrH3 xxBg== 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=fy1/8aHGLqmG6nr3Py2q0Ie8D12Oq5+K1Jwk8epTuy0=; b=PYAHyoBbHUrSls4OHnJTCJqd/jo2zu9TLaFsqK6UPrq1FxHsS/XD0Qeec3iKShshbU 2qHdVWBO578m4elT4yTwRK8D2nYn/W88Kh+Hz5mE8NPPwn/heAztE1AbEiKVdjc3ArcK y9PqNvDCAjEaKGpKY7+9jbAJDvo/wiBNmjNh05R6cvGx1+JbbNHHOUi8/i3VBKIQCRrZ ElG8zbE00xJsVYjEPKUKG2BUX2M7XK8MtVjCNUC0CEgSDvRiaT3YOy9vRcmAeAPSZqru +HZD3L2gDauqtdLTLxxg1qWol9OqnFT9MvAnirDQe+AO6OBX+yFJwopp2hBf+XZiZLU0 7HiQ== X-Gm-Message-State: AOAM530mSToIqAlEbYa8sK/xvPzt6D4DC0c+bW/zsEKTKbDFXo2K77QE x4KYvQzb6PFEGGSyMTgatAB/9YAoETU= X-Google-Smtp-Source: ABdhPJwTdwkC0cIQaBWjkzXJhiI0CQBrnGOF9Flo43IsYiCUTdgr5Wnmncnvvidoc2r9APQD/ypYKg== X-Received: by 2002:adf:ba47:: with SMTP id t7mr13209731wrg.47.1606516046749; Fri, 27 Nov 2020 14:27:26 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:26 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 4/6] add helper is_pow2() Date: Fri, 27 Nov 2020 23:25:13 +0100 Message-Id: <20201127222516.44915-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Add is_pow2() to test if a pseudo is a power of 2. Signed-off-by: Luc Van Oostenryck --- simplify.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/simplify.c b/simplify.c index d729b390ae76..0d9391e21a56 100644 --- a/simplify.c +++ b/simplify.c @@ -52,6 +52,15 @@ // Utilities // ^^^^^^^^^ +/// +// check if a pseudo is a power of 2 +static inline bool is_pow2(pseudo_t src) +{ + if (src->type != PSEUDO_VAL) + return false; + return is_power_of_2(src->value); +} + /// // find the trivial parent for a phi-source static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo) From patchwork Fri Nov 27 22:25:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937295 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.7 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 E6108C3E8C5 for ; Fri, 27 Nov 2020 22:30:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E44421D46 for ; Fri, 27 Nov 2020 22:30:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="uWtQQCcL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730213AbgK0W3J (ORCPT ); Fri, 27 Nov 2020 17:29:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726721AbgK0W1a (ORCPT ); Fri, 27 Nov 2020 17:27:30 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19F8FC061A47 for ; Fri, 27 Nov 2020 14:27:29 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id c198so6249258wmd.0 for ; Fri, 27 Nov 2020 14:27:29 -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 :mime-version:content-transfer-encoding; bh=s+w1yl8gKIlBvFF6/7fDEy9cWHTXXmi33iOvB8eGjSQ=; b=uWtQQCcLzT+dag22fk5n9+SCmJTE1CZ9V6QelaYdiKzlW1BHKo9FzCaclqSO3gtEUK l3GrcHk2FoJZdUcTgi750EbUDo3QzL/gL7UEM5oxYExOONspBXi/sfpVdI/m7UjQWcsz xl/czK9yp+onTV4RrKAhgjk2ABEK4UtIE+dqU7+NMr+Nq5vMerkmf009BmqV/rGx3cw3 izTmJIzxxereYAff9J0QTKet6G7C4m4spAbP670DM+2v51V/HpCDHNUd7UyMX2yGc8MD ws8QOhf13VymdYJ7kcRZXcN3F5TtS35S+NjzTJlaCIVNXnuk8FLt6nyynxd98d634MqD 1p4g== 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=s+w1yl8gKIlBvFF6/7fDEy9cWHTXXmi33iOvB8eGjSQ=; b=rRoOLe4Ic+JmEp5qonaMtc8Wn5P+3sbtdyA16AOGk8AZNkCetwjyqGdUYDXIw1fFhH gAm9NdyJAsU3eo+o2ImHk5wSxB66aL3VBtYToZ5UNB0ypvea54ON7le8opoWuwGs9dio sfy7tuSVkTATU9K+cZwNNRvnpIK0y5be8niKnifHVg9rL1wMCxFQMcXiNF0vVrMkhk5t yTLNTMdTcyalq1Bn7TPuR4TzWKo8ZzSxiZ/FpvQFqfD9/TfUs2HwVpqHX1fAicIP2KRG Uk3XRLMZVuVADJwcQpc2LrkZ6h6TtD9kxGKw2q5I1/pd4SsnLdfsaVNXnKhVKovb29tu 35/A== X-Gm-Message-State: AOAM531LXTOCbvvuBmutXCqojcFri8H6p+SjuZj8Ew+nwAdVSsB3Rid3 SAQbPC25C333I9Cjr8p586+MFClb54o= X-Google-Smtp-Source: ABdhPJy9HSGWwSLB3bnhdxeg0fiCVt6B42QMuia/tXl4zPI7sJkeyG8tK19sZxaPAcO7ah4MvK73zQ== X-Received: by 2002:a1c:5603:: with SMTP id k3mr11752187wmb.151.1606516047678; Fri, 27 Nov 2020 14:27:27 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:27 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 5/6] add log base 2 function: log2_exact() Date: Fri, 27 Nov 2020 23:25:14 +0100 Message-Id: <20201127222516.44915-6-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Add log2_exact() to get the base 2 logarithm of a value known to be a power of 2. Signed-off-by: Luc Van Oostenryck --- bits.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bits.h b/bits.h index 63a663c248e4..9908190d8c2f 100644 --- a/bits.h +++ b/bits.h @@ -63,4 +63,11 @@ static inline int is_power_of_2(long long val) return val && !(val & (val - 1)); } +/// +// log base 2 of an exact power-of-2 +static inline int log2_exact(unsigned long long val) +{ + return 8 * sizeof(val) - __builtin_clzl(val) - 1; +} + #endif From patchwork Fri Nov 27 22:25:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937297 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.7 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,URIBL_BLOCKED,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 4117EC3E8C5 for ; Fri, 27 Nov 2020 22:30:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 086EC21D46 for ; Fri, 27 Nov 2020 22:30:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J7e9dNBg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726721AbgK0W3a (ORCPT ); Fri, 27 Nov 2020 17:29:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731701AbgK0W1a (ORCPT ); Fri, 27 Nov 2020 17:27:30 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 257D1C061A48 for ; Fri, 27 Nov 2020 14:27:30 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id u10so2497029wmm.0 for ; Fri, 27 Nov 2020 14:27:30 -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 :mime-version:content-transfer-encoding; bh=ZPOeT52Yy2HDZEE21W3er4FPZopoUAHQvnz+6jw6apU=; b=J7e9dNBg9ic9Qae9At2VX68O+AL3Jo52W6j8o3TknS0zPvgFyotkX1/Qljt/b72OX6 eaz8XxetkgyEoMWxdHo5cFB2SyTGWdLD8C6EssyDQNUnMGIz6luoR2K2608Ds7ZSuqvR bNnCIG34kjE9W3SX0OmLQ1zvVdu5Q+Q+2Ps+X1sB5+ZCx/P0GMmjdKu84/mM52XcVzBo xC8Lmls5WOt4LJwoDmYxx+9MJOxSr1NE8rGLZ2zPQ03juCqCXew90VZOIabiQ9dg5PpM pXgeAEqis1Vg6Nf/XGMvRqofbFI/gkdSevfeWztm30zCMl5rO1wHMrXCakSKefA8+V2I RTkQ== 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=ZPOeT52Yy2HDZEE21W3er4FPZopoUAHQvnz+6jw6apU=; b=W5qGh6m1KQW9Idx5A7jvUG8WjTMgL7+7GtJPtdLFZ8CHg/Ob0urYaVbdIMJceJfyzT +T4WWVFt5xip874/lIdw+wDVc+ko/cezOMTefEBPICwygIqnMB5UQIlcyeFuYhKvFKnV iXDrpYiDslprkNN5DCB22l/tDqnSkfgZBhKuoDK85QJe3983g3vS4qd+xxfb2vnTqV2o LfzWMesrOxPsKTu5f5GKoGpsaVVSRgGS9TjtZuQVcaRteiq4o5UN4GaZ8ff45mFA56UD b/y41tjDtuzLUjOPyWABwfPNI1Xok7Xvgi5CtBdAJYv8XpNat/rkWioEZImc6aOkZhag PqbA== X-Gm-Message-State: AOAM533ZMg4uGNNebkynpon46gpsboTxSvydWR9ruublelPRwAJh+nvR DpJfNGOHmHgSMKXFeVUH758hnk2pBl4= X-Google-Smtp-Source: ABdhPJzjZ9bCy0D/GvkrTNC00Dvlo4yYzMrItxFy9LzCTLsQM7w83tGQJwZky6j/wLLeYrpR61CBYQ== X-Received: by 2002:a1c:3c04:: with SMTP id j4mr11411474wma.105.1606516048617; Fri, 27 Nov 2020 14:27:28 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:28 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck , Linus Torvalds Subject: [PATCH 6/6] convert SEL(x & BIT1, BIT2, 0) into SHIFT(x & BIT1, S) Date: Fri, 27 Nov 2020 23:25:15 +0100 Message-Id: <20201127222516.44915-7-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org Convert an expression like: (x & (1 << A)) ? (1 << B) : 0 into: (x & (1 << A)) << (B - A) or: (x & (1 << A)) >> (A - B) Suggested-by: Linus Torvalds Signed-off-by: Luc Van Oostenryck --- simplify.c | 15 +++++++++++++++ validation/optim/select-and-shift.c | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 0d9391e21a56..fc64e5b77adf 100644 --- a/simplify.c +++ b/simplify.c @@ -2271,6 +2271,21 @@ static int simplify_select(struct instruction *insn) // both values must be non-zero return replace_with_pseudo(insn, src1); } + case OP_AND: + if (is_pow2(def->src2) && is_pow2(src1) && is_zero(src2) && insn->size == def->size && one_use(cond)) { + unsigned s1 = log2_exact(def->src2->value); + unsigned s2 = log2_exact(insn->src2->value); + unsigned shift; + + if (s1 == s2) + return replace_with_pseudo(insn, cond); + + // SEL(x & A, B, 0) --> SHIFT(x & A, S) + insn->opcode = (s1 < s2) ? OP_SHL : OP_LSR; + shift = (s1 < s2) ? (s2 - s1) : (s1 - s2); + insn->src2 = value_pseudo(shift); + return REPEAT_CSE; + } break; } diff --git a/validation/optim/select-and-shift.c b/validation/optim/select-and-shift.c index fbe044c7cb44..5313fe4b1d86 100644 --- a/validation/optim/select-and-shift.c +++ b/validation/optim/select-and-shift.c @@ -11,7 +11,6 @@ int bar(int p) { return ((p & B) ? A : 0) == ((((unsigned)p) & B) >> S); } /* * check-name: select-and-shift * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 From patchwork Fri Nov 27 22:25:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 11937293 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=-10.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,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 DDA17C3E8C5 for ; Fri, 27 Nov 2020 22:29:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C95921D46 for ; Fri, 27 Nov 2020 22:29:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nJ+6Dpuq" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726763AbgK0W25 (ORCPT ); Fri, 27 Nov 2020 17:28:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731734AbgK0W1i (ORCPT ); Fri, 27 Nov 2020 17:27:38 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 024DFC0613D1 for ; Fri, 27 Nov 2020 14:27:31 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id a3so7715777wmb.5 for ; Fri, 27 Nov 2020 14:27:30 -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 :mime-version:content-transfer-encoding; bh=rcJFeupoq2I+Su3kf87HxC6Hjp7Hy0uwsRH4JXL6yj0=; b=nJ+6DpuqoAJ/yMy2vcrm3Y1dNcjN1CAsZg9p728c3U+JyGmAKFQPhrqujhBmLEwTeX dF9bcLvSiVOC62V8xhYwA4pIgeE0OObG2AccZifmShkgRQhMXFxoLpJx6dAgzfDrOtj/ DjefD6qiTruwRQhkqztImaR6QxefMKm4xnNZSQBcRb0vi7hGiFbsSMisJ3hmm2MzWyx7 kQZNOjC09yMyHwev9x9zxymT6B1FX/VbhaVOnepmd0cEXKTPKXw0z8U+iyfplY0y0TX2 F0RwAk/uwnljnrsowgUZIFhmeO5g5vab/qrHbbPATMjx0Z/UOqH8VMgqW1gvJGoUvQg3 +KnQ== 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=rcJFeupoq2I+Su3kf87HxC6Hjp7Hy0uwsRH4JXL6yj0=; b=dEL/JXWiqeQc2D7nurCbM8zprEcaQSF0EjiwWTuWp7cq0SEXfnaoak3VlOE58rojnI Y1C5Zg4JNc4ANyIVWiAb8IsVKZrdBYO+RrFRYb8TvLImsv3ya19bWXg9nEmTdH6g4PwG q5HmMgpa0ZN7/yf5e1zHkaDCiVbgqPmAimjNE1co8fejUerFoHgwWAOG1hhkrwD280ug Zk5FQLy5pFmsihOB/1mdrajpIQ8sLh4jzVkk33fyvB4wbXFDuLg/mJQqiSrbYzZDBUlu N5RKfsOJWX5pvipANdMkJRUY0VjXgT28BVjC6gI2FyEdziLJsplPt5FFqUdFOBI9rzK8 myVQ== X-Gm-Message-State: AOAM532TVA1XalrizdbBMHtUhb2/9NoaN3wnFxc7Tz6Am+90cCPEqSh+ vT4V/0uyaU2O+oKCcfp29rxEkZhXRPE= X-Google-Smtp-Source: ABdhPJzwZfI72OdihnxlfaOM82qBE0uR+N8K5S4GNTK28DxcEDz3ppiCsIKysIr2kfJzrRbbmDHOnQ== X-Received: by 2002:a7b:c950:: with SMTP id i16mr11613509wml.136.1606516049566; Fri, 27 Nov 2020 14:27:29 -0800 (PST) Received: from localhost.localdomain ([2a02:a03f:b7fe:f700:f469:b93b:e449:cc30]) by smtp.gmail.com with ESMTPSA id q16sm17347050wrn.13.2020.11.27.14.27.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Nov 2020 14:27:29 -0800 (PST) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Luc Van Oostenryck Subject: [PATCH 7/7] move up instructions blocking if-conversion Date: Fri, 27 Nov 2020 23:25:16 +0100 Message-Id: <20201127222516.44915-8-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> References: <20201127222516.44915-1-luc.vanoostenryck@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org --- linearize.h | 6 +++++ simplify.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/linearize.h b/linearize.h index 2c548d43526f..71a693960c9e 100644 --- a/linearize.h +++ b/linearize.h @@ -300,6 +300,12 @@ static inline void replace_bb_in_list(struct basic_block_list **list, replace_ptr_list_entry((struct ptr_list **)list, old, new, count); } +static inline void replace_insn(struct instruction *old, struct instruction *new) +{ + replace_ptr_list_entry((struct ptr_list **)&old->bb->insns, old, new, 1); +} + + struct entrypoint { struct symbol *name; struct symbol_list *syms; diff --git a/simplify.c b/simplify.c index fc64e5b77adf..14a5562c84a4 100644 --- a/simplify.c +++ b/simplify.c @@ -61,6 +61,63 @@ static inline bool is_pow2(pseudo_t src) return is_power_of_2(src->value); } +/// +// check if a pseudo is defined in a BB +// +// :note: this could also use the liveness information if available. +// +static inline bool is_defined_at(pseudo_t src, struct basic_block *bb) +{ + if (src->type != PSEUDO_REG) + return true; + return domtree_dominates(src->def->bb, bb); +} + +/// +// move an instruction at the end of the given BB +static void move_insn(struct instruction *insn, struct basic_block *bb) +{ + struct instruction *last = delete_last_instruction(&bb->insns); + insn->bb = bb; + add_instruction(&bb->insns, insn); + add_instruction(&bb->insns, last); +} + +/// +// try to move an instruction into a BB if all its arguments are defined there +static bool try_to_move_insn(struct instruction *insn, struct basic_block *bb) +{ + static struct instruction NOP; // will later be ignore because !ep + + if (!bb || bb == insn->bb) + return 0; + + switch (insn->opcode) { + case OP_SEL: + if (!is_defined_at(insn->src3, bb)) + return 0; + case OP_ADD: case OP_SUB: + case OP_ASR: case OP_LSR: case OP_SHL: + case OP_AND: case OP_XOR: case OP_OR: + case OP_BINCMP ... OP_BINCMP_END: + case OP_FPCMP ... OP_FPCMP_END: + case OP_FADD: case OP_FSUB: + // basically, all the binops but multiply and divide, just because + if (!is_defined_at(insn->src2, bb)) + return 0; + case OP_UNOP ... OP_UNOP_END: + if (!is_defined_at(insn->src1, bb)) + return 0; + break; + default: + return 0; + } + replace_insn(insn, &NOP); + move_insn(insn, bb); + repeat_phase |= REPEAT_CSE; + return 1; +} + /// // find the trivial parent for a phi-source static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo) @@ -68,8 +125,11 @@ static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseud /* Can't go upwards if the pseudo is defined in the bb it came from.. */ if (pseudo->type == PSEUDO_REG) { struct instruction *def = pseudo->def; - if (def->bb == source) - return source; + if (def->bb == source) { + // unless we can move the defining instruction up + if (!try_to_move_insn(def, source->idom)) + return source; + } } if (bb_list_size(source->children) != 1 || bb_list_size(source->parents) != 1) return source;