diff mbox series

[3/9] simplify (x & M) cmps C

Message ID 20210310214950.84192-4-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series simplify CMP(AND(x,M), C) and CMP(OR(x,M), C) | expand

Commit Message

Luc Van Oostenryck March 10, 2021, 9:49 p.m. UTC
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                   | 25 +++++++++++++++++++++++++
 validation/optim/cmps-and0.c |  1 -
 2 files changed, 25 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/simplify.c b/simplify.c
index 207af8edf28f..90b0c5ba0127 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1258,6 +1258,31 @@  static int simplify_compare_constant(struct instruction *insn, long long value)
 	src2 = insn->src2;
 	value = src2->value;
 	switch (DEF_OPCODE(def, src1)) {
+	case OP_AND:
+		if (!constant(def->src2))
+			break;
+		bits = def->src2->value;
+		switch (insn->opcode) {
+		case OP_SET_LE:
+			value = sign_extend(value, def->size);
+			if (bits & sign_bit(def->size))
+				break;
+			if (value < 0)
+				return replace_with_value(insn, 0);
+			if (value >= (long long)bits)
+				return replace_with_value(insn, 1);
+			break;
+		case OP_SET_GT:
+			value = sign_extend(value, def->size);
+			if (bits & sign_bit(def->size))
+				break;
+			if (value < 0)
+				return replace_with_value(insn, 1);
+			if (value >= (long long)bits)
+				return replace_with_value(insn, 0);
+			break;
+		}
+		break;
 	case OP_SEXT:				// sext(x) cmp C --> x cmp trunc(C)
 		osize = def->orig_type->bit_size;
 		if (is_signed_constant(value, osize, size)) {
diff --git a/validation/optim/cmps-and0.c b/validation/optim/cmps-and0.c
index 097ec2f9f16c..962fbd2d29f8 100644
--- a/validation/optim/cmps-and0.c
+++ b/validation/optim/cmps-and0.c
@@ -15,7 +15,6 @@  int cmps_and_ge_gtm(int a) { return ((a & MASK) >= (MASK + 1)) + 1; }
 /*
  * check-name: cmps-and0
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-returns: 1