[PATCH v3 10/15] simplify OP(((x & M') | y), K) when (M' & M) == M

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

 



In an expression like this, if the inner mask (M') 'covers' all the
bits of the effective mask (M) corresponding to OP(_, K), IOW if
(M' & M) == M, then the inner mask (M') is unneeded and can be
optimized away, giving: OP((x | y), K).

For example, with this simplification, code like:
	int foo(int x, int y)
	{
		return (((x & 0x0fffffff) | y) & 0xfff);
	}

is now optimized into the minimal:
	foo:
		or.32       %r4 <- %arg1, %arg2
		and.32      %r5 <- %r4, $0xfff
		ret.32      %r5

while previously, the inner mask was not optimized away:
	foo:
		and.32      %r2 <- %arg1, $0xfffffff
		or.32       %r4 <- %r2, %arg2
		and.32      %r5 <- %r4, $0xfff
		ret.32      %r5

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 simplify.c                      | 8 ++++++++
 validation/optim/and-or-bf1.c   | 1 -
 validation/optim/and-or-lsr1.c  | 1 -
 validation/optim/and-or-mask1.c | 1 -
 4 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/simplify.c b/simplify.c
index 455860833..dda7c739a 100644
--- a/simplify.c
+++ b/simplify.c
@@ -574,6 +574,14 @@ static int simplify_mask_or_and(struct instruction *insn, unsigned long long mas
 		// if (M' & M) == 0: ((a & M') | b) -> b
 		return replace_pseudo(insn, &insn->src1, orb);
 	}
+	if (multi_users(insn->src1))
+		return 0;	// can't modify anything inside the OR
+	if (nmask == mask) {
+		struct instruction *or = insn->src1->def;
+		pseudo_t *arg = (ora == or->src1) ? &or->src1 : &or->src2;
+		// if (M' & M) == M: ((a & M') | b) -> (a | b)
+		return replace_pseudo(or, arg, and->src1);
+	}
 	return 0;
 }
 
diff --git a/validation/optim/and-or-bf1.c b/validation/optim/and-or-bf1.c
index d20f1cb7c..23477ff34 100644
--- a/validation/optim/and-or-bf1.c
+++ b/validation/optim/and-or-bf1.c
@@ -11,7 +11,6 @@ void foo(struct s *d, const struct s *s, int a)
 /*
  * check-name: and-or-bf1
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-pattern(2): and\\.
diff --git a/validation/optim/and-or-lsr1.c b/validation/optim/and-or-lsr1.c
index 4806d7bca..bd1dbc8aa 100644
--- a/validation/optim/and-or-lsr1.c
+++ b/validation/optim/and-or-lsr1.c
@@ -6,7 +6,6 @@ int foo(int a, int b)
 /*
  * check-name: and-or-lsr1
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-pattern(0): and\\.
diff --git a/validation/optim/and-or-mask1.c b/validation/optim/and-or-mask1.c
index f86e05659..bff3a89f2 100644
--- a/validation/optim/and-or-mask1.c
+++ b/validation/optim/and-or-mask1.c
@@ -6,7 +6,6 @@ int foo(int a, int b)
 /*
  * check-name: and-or-mask1
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-pattern(1): and\\.
-- 
2.18.0




[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