[PATCH 4/7] simplify ((x & M') | y ) & M into (y & M) when (M' & M) == 0

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

 



This is a common pattern for bitfield simplification.

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

diff --git a/simplify.c b/simplify.c
index 911ab11a9..b3b5b5591 100644
--- a/simplify.c
+++ b/simplify.c
@@ -775,18 +775,41 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
 	return 0;
 }
 
+static int simplify_and_or_mask(struct instruction *insn, pseudo_t and, pseudo_t other, unsigned long long mask)
+{
+	struct instruction *def = and->def;
+
+	if (!constant(def->src2))
+		return 0;
+	if (def->src2->value & mask)
+		return 0;
+	return replace_pseudo(insn, &insn->src1, other);
+}
+
 static int simplify_constant_mask(struct instruction *insn, unsigned long long mask)
 {
 	pseudo_t old = insn->src1;
 	unsigned long long omask;
 	unsigned long long nmask;
 	struct instruction *def;
+	pseudo_t src1, src2;
 	int osize;
 
 	switch (DEF_OPCODE(def, old)) {
 	case OP_FPCMP ... OP_BINCMP_END:
 		osize = 1;
 		goto oldsize;
+	case OP_OR:
+		// Let's handle ((A & M') | B ) & M
+		// or           (B | (A & M')) & M
+		// when M' & M == 0
+		src1 = def->src1;
+		src2 = def->src2;
+		if (def_opcode(src1) == OP_AND)
+			return simplify_and_or_mask(insn, src1, src2, mask);
+		if (def_opcode(src2) == OP_AND)
+			return simplify_and_or_mask(insn, src2, src1, mask);
+		break;
 	case OP_ZEXT:
 		osize = def->orig_type->bit_size;
 		/* fall through */
diff --git a/validation/optim/mask-out.c b/validation/optim/mask-out.c
index bf116873e..ac85aec80 100644
--- a/validation/optim/mask-out.c
+++ b/validation/optim/mask-out.c
@@ -6,7 +6,6 @@ unsigned mask(unsigned a, unsigned b)
 /*
  * check-name: mask-out
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-excludes: %arg1
-- 
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