[PATCH 6/7] simplify (x << S) >> S into x & (-1 >> S)

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

 



This transformation is especially usefull when simplifying code
accessing bitfields or for other masking manipulations.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 simplify.c                             | 11 +++++++++++
 validation/optim/shift-zext.c          |  1 -
 validation/optim/shl-lsr.c             |  1 -
 validation/optim/store-load-bitfield.c | 12 +++---------
 4 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/simplify.c b/simplify.c
index 7f02d4742..acb834439 100644
--- a/simplify.c
+++ b/simplify.c
@@ -656,6 +656,17 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
 			if (def_opcode(src) == OP_AND)
 				return simplify_or_lsr(insn, src, def->src1, value);
 			break;
+		case OP_SHL:
+			// replace (A << S) >> S
+			// by      A & (Mask(size) >> S)
+			if (!constant(def->src2))
+				break;
+			if (def->src2->value != value)
+				break;
+			size = insn->size - value;
+			insn->opcode = OP_AND;
+			insn->src2 = value_pseudo((1ULL << size) - 1);
+			return replace_pseudo(insn, &insn->src1, def->src1);
 		}
 		break;
 	case OP_SHL:
diff --git a/validation/optim/shift-zext.c b/validation/optim/shift-zext.c
index 070416f30..30409beca 100644
--- a/validation/optim/shift-zext.c
+++ b/validation/optim/shift-zext.c
@@ -6,7 +6,6 @@ unsigned int foo(unsigned int x)
 /*
  * check-name: shift-zext
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: and\\..*%arg1, \\$0xfff
diff --git a/validation/optim/shl-lsr.c b/validation/optim/shl-lsr.c
index 1ab2c48fc..4f4b7e66b 100644
--- a/validation/optim/shl-lsr.c
+++ b/validation/optim/shl-lsr.c
@@ -6,7 +6,6 @@ unsigned mask(unsigned x)
 /*
  * check-name: shl-lsr
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: and\\..*0x1ffff
diff --git a/validation/optim/store-load-bitfield.c b/validation/optim/store-load-bitfield.c
index 1d8ff2402..f68cb6000 100644
--- a/validation/optim/store-load-bitfield.c
+++ b/validation/optim/store-load-bitfield.c
@@ -28,21 +28,15 @@ int sfoo(int a)
 ufoo:
 .L0:
 	<entry-point>
-	and.32      %r4 <- %arg1, $7
-	shl.32      %r5 <- %r4, $2
-	lsr.32      %r9 <- %r5, $2
-	and.32      %r11 <- %r9, $7
+	and.32      %r11 <- %arg1, $7
 	ret.32      %r11
 
 
 sfoo:
 .L2:
 	<entry-point>
-	and.32      %r16 <- %arg1, $7
-	shl.32      %r17 <- %r16, $2
-	lsr.32      %r21 <- %r17, $2
-	trunc.3     %r22 <- (32) %r21
-	sext.32     %r23 <- (3) %r22
+	trunc.3     %r16 <- (32) %arg1
+	sext.32     %r23 <- (3) %r16
 	ret.32      %r23
 
 
-- 
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