Skip to contentSkip to conversation

simplify and canonicalize unsigned compares

PATCH In review Luc Van Oostenryck 2 participants 5 messages Oct 23, 2020 Sparse checker

lore
Share & tools
lore
#0 lore raw

cover simplify and canonicalize unsigned compares

Some unsigned compares are always true or false. Some others, testing if the value is greater or smaller than 0 or 1 are equivalent to testing if this value is equal or not to 0.

These 2 patches simplify the first ones and canonicalize the others to the equivalent equality test.

Luc Van Oostenryck (2):
simplify unsigned compares against 0
canonicalize unsigned compares against 0 or 1

 simplify.c                   | 30 ++++++++++++++++++++++++++++++
 validation/optim/set-uimm0.c | 14 ++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 validation/optim/set-uimm0.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/2 simplify unsigned compares against 0

Some unsigned compares against 0 are always true or always false (x < 0 or x >= 0). Simplify them.

---
 simplify.c                   | 10 ++++++++++
 validation/optim/set-uimm0.c | 10 ++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 validation/optim/set-uimm0.c
simplify.c +10 -0
index 6caf6cbcf918..4441b27c7546 100644
@@ -1173,6 +1173,16 @@ static int simplify_constant_rightside(struct instruction *insn)
 	case OP_SET_NE:
 	case OP_SET_EQ:
 		return simplify_seteq_setne(insn, value);
+	case OP_SET_B:
+		if (!value) {			// (x < 0) --> 0
+			return replace_with_pseudo(insn, value_pseudo(0));
+		}
+		break;
+	case OP_SET_AE:
+		if (!value) {			// (x >= 0) --> 1
+			return replace_with_pseudo(insn, value_pseudo(1));
+		}
+		break;
 	}
 	return 0;
 }
validation/optim/set-uimm0.c new file +10 -0
new file mode 100644
index 000000000000..1f62358ff0fb
@@ -0,0 +1,10 @@
+static _Bool setlt0(unsigned int a) { return (a <   0u) == 0; }
+static _Bool setge0(unsigned int a) { return (a >=  0u) == 1; }
+
+/*
+ * check-name: set-uimm0
+ * check-command: test-linearize $file
+ *
+ * check-output-ignore
+ * check-output-pattern(2): ret\\.1 *\\$1
+ */
Signature · 1 line
2.28.0
#2 lore raw

Re: [PATCH 1/2] simplify unsigned compares against 0

Quoted 3 lines · Luc Van Oostenryck

Some unsigned compares against 0 are always true or always false (x < 0 or x >= 0). Simplify them.

Fair enough, but if you're simplifying compares, one of the more important simplifications is to make the compare unsigned in the first place.

IOW, simplifying

    (signed >= 0 && signed < X)

into

(unsigned < X)

if you can show that X is positive (most trivially a constant). Example:

    int cmp(int i) { return i >= 0 && i < 4; }

which currently generates

    setge.1     %r3 <- %arg1, $0
    setlt.1     %r6 <- %arg1, $4
    and.1       %r7 <- %r3, %r6
    zext.32     %r8 <- (1) %r7
    ret.32      %r8

which is obviously not great.

Another comparison simplification often worth doing is to do cast simplification, ie

((cast) X cmpop Y)

where 'Y' already fits in the original type of 'X', and the cast is unnecessary. Test-case:

    int cmp(int i) { return i < sizeof(i); }

and notice how sparse generates

    sext.64     %r2 <- (32) %arg1
    setb.32     %r3 <- %r2, $4
    ret.32      %r3

with that entirely unnecessary sign extension that doesn't really help..

The related simplification is then

    (signed >= 0 && unsigned < X)

and just removing the "signed > 0" case. These happen when you have code that checks for sizes:

    int cmp(int i) { return i >= 0 && i < sizeof(i); }

where that "i < sizeof(i)" ended up being not just extended to 64-bit, but an unsigned compare due to the 'sizeof()' being unsigned.

Linus

#3 lore raw

Re: [PATCH 1/2] simplify unsigned compares against 0

Quoted 10 lines · Linus Torvalds
Quoted 3 lines · Luc Van Oostenryck

Some unsigned compares against 0 are always true or always false (x < 0 or x >= 0). Simplify them.

Fair enough, but if you're simplifying compares, one of the more important simplifications is to make the compare unsigned in the first place.

Quoted 6 lines

...

Another comparison simplification often worth doing is to do cast simplification, ie

((cast) X cmpop Y)

Yes, these seem quite easy with a nice return rate. I've a lot of pending, uncompleted topic branches (which I'm currently trying to polish and upstream, hence the apparent lack of a direction line) but I don't think I've anything for these. I'll look at them soon.

-- Luc

#4 lore raw

PATCH 2/2 canonicalize unsigned compares against 0 or 1

In reply to Luc Van Oostenryck

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.

---
 simplify.c                   | 20 ++++++++++++++++++++
 validation/optim/set-uimm0.c |  6 +++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
simplify.c +20 -0
index 4441b27c7546..96cd73a0a61a 100644
@@ -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;
 	}
validation/optim/set-uimm0.c +5 -1
index 1f62358ff0fb..ded8fc827223 100644
@@ -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
  */
Signature · 1 line
2.28.0