[PATCH] simplify SEL(x == y, x, y) and friends

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

 



If the condition of a select instruction is a equality test of the
select's operands, then the result of the select is always the same as
its second operand. Same for the first operand with an inequality test.

Simplify away these selects.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 simplify.c                     | 16 ++++++++++++++++
 validation/optim/eqne-select.c | 12 ++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 validation/optim/eqne-select.c

diff --git a/simplify.c b/simplify.c
index f2aaa52de84b..2d165cfe143c 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1747,6 +1747,7 @@ static int simplify_cast(struct instruction *insn)
 
 static int simplify_select(struct instruction *insn)
 {
+	struct instruction *def;
 	pseudo_t cond, src1, src2;
 
 	cond = insn->src1;
@@ -1782,6 +1783,21 @@ static int simplify_select(struct instruction *insn)
 		kill_use(&insn->src3);
 		return replace_with_value(insn, 0);
 	}
+
+	switch (DEF_OPCODE(def, cond)) {
+	case OP_SET_EQ:
+		if (src1 == def->src1 && src2 == def->src2)
+			return replace_with_pseudo(insn, src2); // SEL(x==y,x,y) --> y
+		if (src2 == def->src1 && src1 == def->src2)
+			return replace_with_pseudo(insn, src2); // SEL(y==x,x,y) --> y
+		break;
+	case OP_SET_NE:
+		if (src1 == def->src1 && src2 == def->src2)
+			return replace_with_pseudo(insn, src1); // SEL(x!=y,x,y) --> x
+		if (src2 == def->src1 && src1 == def->src2)
+			return replace_with_pseudo(insn, src1); // SEL(y!=x,x,y) --> x
+		break;
+	}
 	return 0;
 }
 
diff --git a/validation/optim/eqne-select.c b/validation/optim/eqne-select.c
new file mode 100644
index 000000000000..9dfd88b5eb4d
--- /dev/null
+++ b/validation/optim/eqne-select.c
@@ -0,0 +1,12 @@
+int sel_eq01(int a, int b) { return ((a == b) ? a : b) == b; }
+int sel_eq10(int a, int b) { return ((a == b) ? b : a) == a; }
+int sel_ne01(int a, int b) { return ((a != b) ? a : b) == a; }
+int sel_ne10(int a, int b) { return ((a != b) ? b : a) == b; }
+
+/*
+ * check-name: eqne-select
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-returns: 1
+ */
-- 
2.29.2




[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