diff mbox series

[11/22] sub: simplify C - (y + D) --> eval(C-D) - y

Message ID 20201020211021.82394-12-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series essential OP_ADD & OP_SUB simplifications | expand

Commit Message

Luc Van Oostenryck Oct. 20, 2020, 9:10 p.m. UTC
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                               | 18 ++++++++++++++++++
 validation/optim/simplify-cte-sub-addr.c |  1 -
 2 files changed, 18 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/simplify.c b/simplify.c
index aa0357d9e285..b97354bc40cc 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1157,6 +1157,22 @@  static int simplify_constant_rightside(struct instruction *insn)
 	return 0;
 }
 
+static int simplify_const_leftsub(struct instruction *insn, struct instruction *def)
+{
+	unsigned size = insn->size;
+	pseudo_t src1 = insn->src1;
+
+	switch (def->opcode) {
+	case OP_ADD:
+		if (constant(def->src2)) { // C - (y + D) --> eval(C-D) - y
+			insn->src1 = eval_op(OP_SUB, size, src1, def->src2);
+			return replace_pseudo(insn, &insn->src2, def->src1);
+		}
+		break;
+	}
+	return 0;
+}
+
 static int simplify_constant_leftside(struct instruction *insn)
 {
 	long long value = insn->src1->value;
@@ -1177,6 +1193,8 @@  static int simplify_constant_leftside(struct instruction *insn)
 	case OP_SUB:
 		if (!value)			// (0 - x) --> -x
 			return replace_with_unop(insn, OP_NEG, insn->src2);
+		if (insn->src2->type == PSEUDO_REG)
+			return simplify_const_leftsub(insn, insn->src2->def);
 		break;
 	}
 	return 0;
diff --git a/validation/optim/simplify-cte-sub-addr.c b/validation/optim/simplify-cte-sub-addr.c
index 81f5b34551c6..bf36c8a0a30a 100644
--- a/validation/optim/simplify-cte-sub-addr.c
+++ b/validation/optim/simplify-cte-sub-addr.c
@@ -3,7 +3,6 @@  int cte_sub_addr(int x) { return 2 - (x + 1); }
 /*
  * check-name: simplify-cte-sub-addr
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: sub\\..*\\$1, %arg1