linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
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	[thread overview]
Message-ID: <20201127222516.44915-3-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20201127222516.44915-1-luc.vanoostenryck@gmail.com>

'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 <luc.vanoostenryck@gmail.com>
---
 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
-- 
2.29.2


  parent reply	other threads:[~2020-11-27 22:33 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-22 15:27 [PATCH 0/7] simplify logical negation Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 1/7] not: add testcases for canonicalization & simplification of negations Luc Van Oostenryck
2020-11-22 19:00   ` Linus Torvalds
2020-11-22 19:57     ` Luc Van Oostenryck
2020-11-22 20:13       ` Linus Torvalds
2020-11-22 20:38         ` Luc Van Oostenryck
2020-11-22 22:26         ` Luc Van Oostenryck
2020-11-27 22:25         ` [PATCH 0/6] 'bits translation' simplification Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 1/6] add testscases for 'bits translation' optimization Luc Van Oostenryck
2020-11-27 22:25           ` Luc Van Oostenryck [this message]
2020-11-27 22:25           ` [PATCH 3/6] add helper is_power_of_2() Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 4/6] add helper is_pow2() Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 5/6] add log base 2 function: log2_exact() Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 6/6] convert SEL(x & BIT1, BIT2, 0) into SHIFT(x & BIT1, S) Luc Van Oostenryck
2020-11-27 22:25           ` [PATCH 7/7] move up instructions blocking if-conversion Luc Van Oostenryck
2020-11-27 22:44           ` [PATCH 0/6] 'bits translation' simplification Linus Torvalds
2020-11-22 15:27 ` [PATCH 2/7] canon: put PSEUDO_ARGs in canonical order too Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 3/7] canon: put PSEUDO_REGs " Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 4/7] canon: simplify calculation of canonical order Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 5/7] opcode: add helpers opcode_negate() & opcode_swap() Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 6/7] not: simplify (~x {&,|,^} x) --> {0,~0,~0} Luc Van Oostenryck
2020-11-22 15:27 ` [PATCH 7/7] not: simplify ((x cmp y) {&,|,^} (x !cmp y)) --> {0,1,1} Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201127222516.44915-3-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).