diff mbox

[02/10] simplify 'x | ~0' to '~0'

Message ID 20180626060002.35753-3-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Luc Van Oostenryck June 26, 2018, 5:59 a.m. UTC
This is a simple identity with the potential to trigger
more simplifications.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                       |  9 ++++++++-
 validation/optim/bits-not-zero.c | 15 +++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 validation/optim/bits-not-zero.c
diff mbox

Patch

diff --git a/simplify.c b/simplify.c
index ad32fe08a..25e2a41c1 100644
--- a/simplify.c
+++ b/simplify.c
@@ -620,6 +620,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:
@@ -627,6 +629,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;
@@ -635,7 +642,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:
diff --git a/validation/optim/bits-not-zero.c b/validation/optim/bits-not-zero.c
new file mode 100644
index 000000000..9872794b1
--- /dev/null
+++ b/validation/optim/bits-not-zero.c
@@ -0,0 +1,15 @@ 
+int  or_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
+
+
+ * check-output-end
+ */