[PATCH 7/7] simplify (x & M) >> S to (x >> S) & (M >> S)

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

 



This is especially usefull when simplifying code
accessing bitfields.

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

diff --git a/simplify.c b/simplify.c
index acb834439..d27deedf1 100644
--- a/simplify.c
+++ b/simplify.c
@@ -590,6 +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;
 	unsigned long long nval;
 	unsigned int size;
 	pseudo_t src2;
@@ -643,6 +644,19 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
 		if (value >= size)
 			goto zero;
 		switch(DEF_OPCODE(def, pseudo)) {
+		case OP_AND:
+			// replace (A & M) >> S
+			// by      (A >> S) & (M >> S)
+			if (!constant(def->src2))
+				break;
+			if (nbr_users(pseudo) > 1)
+				break;
+			mask = def->src2->value;
+			def->opcode = OP_LSR;
+			def->src2 = insn->src2;
+			insn->opcode = OP_AND;
+			insn->src2 = value_pseudo(mask >> value);
+			return REPEAT_CSE;
 		case OP_LSR:
 			goto case_shift_shift;
 		case OP_OR:
diff --git a/validation/optim/and-lsr.c b/validation/optim/and-lsr.c
index df6b72f36..439eb8227 100644
--- a/validation/optim/and-lsr.c
+++ b/validation/optim/and-lsr.c
@@ -8,7 +8,6 @@ unsigned int foo(unsigned int x)
 /*
  * check-name: and-lsr
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: and\\..*\\$15
-- 
2.18.0

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[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