diff mbox series

[6/6] convert SEL(x & BIT1, BIT2, 0) into SHIFT(x & BIT1, S)

Message ID 20201127222516.44915-7-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series 'bits translation' simplification | expand

Commit Message

Luc Van Oostenryck Nov. 27, 2020, 10:25 p.m. UTC
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 <torvalds@linux-foundation.org>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                          | 15 +++++++++++++++
 validation/optim/select-and-shift.c |  1 -
 2 files changed, 15 insertions(+), 1 deletion(-)
diff mbox series

Patch

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