[PATCH 3/6] simplify ((x & M) >> S) when (M >> S) == 0

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

 



The instructions LSR(AND(x, M), S) are already simplified into
AND(LSR(x, S), (M >> 12)) but only if AND(x, M) has a single user.

However, if (M >> S) == 0, they can always directly be simplified to 0.
For example code like:
	unsigned foo(unsigned x)
	{
		unsigned t = (x & 0x00000fff);
		return (t >> 12) & t;
	}

is now simplified into:
	foo:
		ret.32      $0

while previously it was:
	foo:
		and.32      %r2 <- %arg1, $0xfff
		lsr.32      %r4 <- %r2, $12
		and.32      %r6 <- %r4, %r2
		ret.32      %r6

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 simplify.c                  | 6 +++++-
 validation/optim/lsr-and0.c | 1 -
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/simplify.c b/simplify.c
index c18965bfa..fe264417e 100644
--- a/simplify.c
+++ b/simplify.c
@@ -590,7 +590,7 @@ static int simplify_or_lsr(struct instruction *insn, pseudo_t src, pseudo_t othe
 static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long value)
 {
 	struct instruction *def;
-	unsigned long long mask, omask;
+	unsigned long long mask, omask, nmask;
 	unsigned long long nval;
 	unsigned int size;
 	pseudo_t src2;
@@ -649,7 +649,11 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
 			// by      (A >> S) & (M >> S)
 			if (!constant(def->src2))
 				break;
+			mask = bits_mask(insn->size - value) << value;
 			omask = def->src2->value;
+			nmask = omask & mask;
+			if (nmask == 0)
+				return replace_with_pseudo(insn, value_pseudo(0));
 			if (nbr_users(pseudo) > 1)
 				break;
 			def->opcode = OP_LSR;
diff --git a/validation/optim/lsr-and0.c b/validation/optim/lsr-and0.c
index 292c0332f..94310ba8a 100644
--- a/validation/optim/lsr-and0.c
+++ b/validation/optim/lsr-and0.c
@@ -7,7 +7,6 @@ unsigned lsr_and0(unsigned x)
 /*
  * check-name: lsr-and0
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: ret\\..*\\$0$
-- 
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