[PATCH 2/2] canonicalize unsigned compares against 0 or 1

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Some unsigned compares against 0 or 1 are equivalent to testing
equality with 0 (x <= 0, x > 0, x < 1, x >= 1).

Canonicalize them to this later, more common form.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 simplify.c                   | 20 ++++++++++++++++++++
 validation/optim/set-uimm0.c |  6 +++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/simplify.c b/simplify.c
index 4441b27c7546..96cd73a0a61a 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1176,11 +1176,31 @@ static int simplify_constant_rightside(struct instruction *insn)
 	case OP_SET_B:
 		if (!value) {			// (x < 0) --> 0
 			return replace_with_pseudo(insn, value_pseudo(0));
+		} else if (value == 1) {	// (x < 1) --> (x == 0)
+			insn->src2 = value_pseudo(0);
+			insn->opcode = OP_SET_EQ;
+			return REPEAT_CSE;
 		}
 		break;
 	case OP_SET_AE:
 		if (!value) {			// (x >= 0) --> 1
 			return replace_with_pseudo(insn, value_pseudo(1));
+		} else if (value == 1) {	// (x >= 1) --> (x != 0)
+			insn->src2 = value_pseudo(0);
+			insn->opcode = OP_SET_NE;
+			return REPEAT_CSE;
+		}
+		break;
+	case OP_SET_BE:
+		if (!value) {			// (x <= 0) --> (x == 0)
+			insn->opcode = OP_SET_EQ;
+			return REPEAT_CSE;
+		}
+		break;
+	case OP_SET_A:
+		if (!value) {			// (x > 0) --> (x != 0)
+			insn->opcode = OP_SET_NE;
+			return REPEAT_CSE;
 		}
 		break;
 	}
diff --git a/validation/optim/set-uimm0.c b/validation/optim/set-uimm0.c
index 1f62358ff0fb..ded8fc827223 100644
--- a/validation/optim/set-uimm0.c
+++ b/validation/optim/set-uimm0.c
@@ -1,10 +1,14 @@
 static _Bool setlt0(unsigned int a) { return (a <   0u) == 0; }
 static _Bool setge0(unsigned int a) { return (a >=  0u) == 1; }
+static _Bool setle0(unsigned int a) { return (a <=  0u) == (a == 0); }
+static _Bool setgt0(unsigned int a) { return (a >   0u) == (a != 0); }
+static _Bool setlt1(unsigned int a) { return (a <   1u) == (a == 0); }
+static _Bool setge1(unsigned int a) { return (a >=  1u) == (a != 0); }
 
 /*
  * check-name: set-uimm0
  * check-command: test-linearize $file
  *
  * check-output-ignore
- * check-output-pattern(2): ret\\.1 *\\$1
+ * check-output-pattern(6): ret\\.1 *\\$1
  */
-- 
2.28.0




[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux