linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Simplify booleans
@ 2017-04-12 14:17 Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 1/3] simplify 'x | ~0' and 'x & ~0' Luc Van Oostenryck
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2017-04-12 14:17 UTC (permalink / raw)
  To: linux-sparse; +Cc: Christopher Li, Luc Van Oostenryck

The goal of this series is to add some simplification
concerning booleans and is a preparatory steps to give
a proper boolean context when required.


This series is available at:
	git://github.com/lucvoo/sparse.git simplify-bool-mask
based on commit:
	d440a16358aefd718029963bb2261f1deccfddab (fpos)
up to commit:
	d4208cd1ef7df00f7d79dba1ccb365136d382914

Luc Van Oostenryck (3):
  simplify 'x | ~0' and 'x & ~0'
  simplify 'x ^ ~0' to '~x'
  simplify casts bool -> int -> bool

 simplify.c                       | 35 +++++++++++++++++++++++++++++++++--
 validation/optim/bool-int-bool.c | 12 ++++++++++++
 validation/optim/bool-not-zero.c | 30 ++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 2 deletions(-)
 create mode 100644 validation/optim/bool-int-bool.c
 create mode 100644 validation/optim/bool-not-zero.c

-- 
2.12.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] simplify 'x | ~0' and 'x & ~0'
  2017-04-12 14:17 [PATCH 0/3] Simplify booleans Luc Van Oostenryck
@ 2017-04-12 14:18 ` Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 2/3] simplify 'x ^ ~0' to '~x' Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 3/3] simplify casts bool -> int -> bool Luc Van Oostenryck
  2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2017-04-12 14:18 UTC (permalink / raw)
  To: linux-sparse; +Cc: Christopher Li, Luc Van Oostenryck

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                       | 11 ++++++++++-
 validation/optim/bool-not-zero.c | 22 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 validation/optim/bool-not-zero.c

diff --git a/simplify.c b/simplify.c
index 2286440e0..e4ccb6c5f 100644
--- a/simplify.c
+++ b/simplify.c
@@ -453,6 +453,8 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
 static int simplify_constant_rightside(struct instruction *insn)
 {
 	long long value = insn->src2->value;
+	long long sbit = 1ULL << (insn->size - 1);
+	long long bits = sbit | (sbit - 1);
 
 	switch (insn->opcode) {
 	case OP_OR_BOOL:
@@ -460,6 +462,11 @@ static int simplify_constant_rightside(struct instruction *insn)
 			return replace_with_pseudo(insn, insn->src2);
 		goto case_neutral_zero;
 
+	case OP_OR:
+		if ((value & bits) == bits)
+			return replace_with_pseudo(insn, insn->src2);
+		goto case_neutral_zero;
+
 	case OP_SUB:
 		if (value) {
 			insn->opcode = OP_ADD;
@@ -468,7 +475,7 @@ static int simplify_constant_rightside(struct instruction *insn)
 		}
 	/* Fall through */
 	case OP_ADD:
-	case OP_OR: case OP_XOR:
+	case OP_XOR:
 	case OP_SHL:
 	case OP_LSR:
 	case_neutral_zero:
@@ -492,6 +499,8 @@ static int simplify_constant_rightside(struct instruction *insn)
 			return replace_with_pseudo(insn, insn->src1);
 	/* Fall through */
 	case OP_AND:
+		if ((value & bits) == bits)
+			return replace_with_pseudo(insn, insn->src1);
 		if (!value)
 			return replace_with_pseudo(insn, insn->src2);
 		return 0;
diff --git a/validation/optim/bool-not-zero.c b/validation/optim/bool-not-zero.c
new file mode 100644
index 000000000..ce74705e8
--- /dev/null
+++ b/validation/optim/bool-not-zero.c
@@ -0,0 +1,22 @@
+int  or_not0(int a) { return a | ~0; }
+int and_not0(int a) { return a & ~0; }
+
+/*
+ * check-name: bool-not-zero
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-start
+or_not0:
+.L0:
+	<entry-point>
+	ret.32      $0xffffffff
+
+
+and_not0:
+.L2:
+	<entry-point>
+	ret.32      %arg1
+
+
+ * check-output-end
+ */
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] simplify 'x ^ ~0' to '~x'
  2017-04-12 14:17 [PATCH 0/3] Simplify booleans Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 1/3] simplify 'x | ~0' and 'x & ~0' Luc Van Oostenryck
@ 2017-04-12 14:18 ` Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 3/3] simplify casts bool -> int -> bool Luc Van Oostenryck
  2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2017-04-12 14:18 UTC (permalink / raw)
  To: linux-sparse; +Cc: Christopher Li, Luc Van Oostenryck

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                       | 8 +++++++-
 validation/optim/bool-not-zero.c | 8 ++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/simplify.c b/simplify.c
index e4ccb6c5f..775c1e2dd 100644
--- a/simplify.c
+++ b/simplify.c
@@ -467,6 +467,13 @@ static int simplify_constant_rightside(struct instruction *insn)
 			return replace_with_pseudo(insn, insn->src2);
 		goto case_neutral_zero;
 
+	case OP_XOR:
+		if ((value & bits) == bits) {
+			insn->opcode = OP_NOT;
+			return REPEAT_CSE;
+		}
+		goto case_neutral_zero;
+
 	case OP_SUB:
 		if (value) {
 			insn->opcode = OP_ADD;
@@ -475,7 +482,6 @@ static int simplify_constant_rightside(struct instruction *insn)
 		}
 	/* Fall through */
 	case OP_ADD:
-	case OP_XOR:
 	case OP_SHL:
 	case OP_LSR:
 	case_neutral_zero:
diff --git a/validation/optim/bool-not-zero.c b/validation/optim/bool-not-zero.c
index ce74705e8..189fe3311 100644
--- a/validation/optim/bool-not-zero.c
+++ b/validation/optim/bool-not-zero.c
@@ -1,5 +1,6 @@
 int  or_not0(int a) { return a | ~0; }
 int and_not0(int a) { return a & ~0; }
+int xor_not0(int a) { return a ^ ~0; }
 
 /*
  * check-name: bool-not-zero
@@ -18,5 +19,12 @@ and_not0:
 	ret.32      %arg1
 
 
+xor_not0:
+.L4:
+	<entry-point>
+	not.32      %r8 <- %arg1
+	ret.32      %r8
+
+
  * check-output-end
  */
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] simplify casts bool -> int -> bool
  2017-04-12 14:17 [PATCH 0/3] Simplify booleans Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 1/3] simplify 'x | ~0' and 'x & ~0' Luc Van Oostenryck
  2017-04-12 14:18 ` [PATCH 2/3] simplify 'x ^ ~0' to '~x' Luc Van Oostenryck
@ 2017-04-12 14:18 ` Luc Van Oostenryck
  2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2017-04-12 14:18 UTC (permalink / raw)
  To: linux-sparse; +Cc: Christopher Li, Luc Van Oostenryck

Because of C's integer promotion, in code like 'a == 0',
the operand 'a' must be promoted to int, which result in
following linearization if the type of 'a' was _Bool:
	cast.32  %t <- (1) %a
	setne.32 %r <- %t, $0
While required by the standard, this promotion is unneeded
in the given situation.

Change this by simplifying away such casts.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                       | 18 +++++++++++++++++-
 validation/optim/bool-int-bool.c | 12 ++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 validation/optim/bool-int-bool.c

diff --git a/simplify.c b/simplify.c
index 775c1e2dd..96448a666 100644
--- a/simplify.c
+++ b/simplify.c
@@ -445,9 +445,25 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
 		remove_usage(old, &insn->src1);
 		return REPEAT_CSE;
 
+	case OP_CAST:
+		if (def->orig_type->bit_size != 1)
+			break;
+
+		// Convert:
+		//	cast.n	%t <- (1) %a
+		//	setne.m %r <- %t, $0
+		// into:
+		//	...
+		//	setne.m %r <- %a, $0
+		// and similar for setne/eq ... 0/1
+		use_pseudo(insn, def->src1, &insn->src1);
+		remove_usage(old, &insn->src1);
+		return REPEAT_CSE;
+
 	default:
-		return 0;
+		break;
 	}
+	return 0;
 }
 
 static int simplify_constant_rightside(struct instruction *insn)
diff --git a/validation/optim/bool-int-bool.c b/validation/optim/bool-int-bool.c
new file mode 100644
index 000000000..de34a68bb
--- /dev/null
+++ b/validation/optim/bool-int-bool.c
@@ -0,0 +1,12 @@
+_Bool beq0(_Bool a) { return (a == 0); }
+_Bool beq1(_Bool a) { return (a == 1); }
+_Bool bne0(_Bool a) { return (a != 0); }
+_Bool bne1(_Bool a) { return (a != 1); }
+
+/*
+ * check-name: bool - int - bool constants
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: cast\\.
+ */
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-04-12 14:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-12 14:17 [PATCH 0/3] Simplify booleans Luc Van Oostenryck
2017-04-12 14:18 ` [PATCH 1/3] simplify 'x | ~0' and 'x & ~0' Luc Van Oostenryck
2017-04-12 14:18 ` [PATCH 2/3] simplify 'x ^ ~0' to '~x' Luc Van Oostenryck
2017-04-12 14:18 ` [PATCH 3/3] simplify casts bool -> int -> bool Luc Van Oostenryck

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).