[PATCH] simplify TRUNC((x & M') | y, N)

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

 



A N-bit truncate is not much different than ANDing with
a N-bit mask and so some simplifications done for AND can
also be done for TRUNC. For example for code like this:
	char foo(int x, int y) { return (x & 0xffff) | y; }

the mask is unneeded and the function should be equivalent to:
	char foo(int x, int y) { return x | y; }

The simplification in this patch does exactly this, giving:
	foo:
		or.32       %r4 <- %arg1, %arg2
		trunc.8     %r5 <- (32) %r4
		ret.8       %r5

while previously the mask was not optimized away:
	foo:
		and.32      %r2 <- %arg1, $0xffff
		or.32       %r4 <- %r2, %arg2
		trunc.8     %r5 <- (32) %r4
		ret.8       %r5

This simplification is especially important for signed bitfields
because the TRUNC+ZEXT of unsigned bitfields is simplified into
an OP_AND but this is, of course, not the case for the TRUNC+SEXT
of signed bitfields.

Do the simplification by calling simplify_mask_or(), initialy used
for OP_AND, but with the effective mask corresponding to TRUNC(x, N):
$mask(N).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 simplify.c                       | 8 ++++++++
 validation/optim/and-or-trunc0.c | 1 -
 validation/optim/and-or-trunc1.c | 1 -
 validation/optim/and-or-trunc2.c | 1 -
 validation/optim/and-or-truncx.c | 1 -
 5 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/simplify.c b/simplify.c
index 737da6501..529ffd7c6 100644
--- a/simplify.c
+++ b/simplify.c
@@ -632,6 +632,7 @@ static int simplify_mask_or_and(struct instruction *insn, unsigned long long mas
 //	* if OP(x, K) == AND(x, M), @mask M is K
 //	* if OP(x, K) == LSR(x, S), @mask M is (-1 << S)
 //	* if OP(x, K) == SHL(x, S), @mask M is (-1 >> S)
+//	* if OP(x, K) == TRUNC(x, N), @mask M is $mask(N)
 static int simplify_mask_or(struct instruction *insn, unsigned long long mask, struct instruction *or)
 {
 	pseudo_t src1 = or->src1;
@@ -1399,6 +1400,13 @@ static int simplify_cast(struct instruction *insn)
 			return replace_pseudo(insn, &insn->src1, def->src1);
 		}
 		break;
+	case OP_OR:
+		switch (insn->opcode) {
+		case OP_TRUNC:
+			mask = bits_mask(insn->size);
+			return simplify_mask_or(insn, mask, def);
+		}
+		break;
 	case OP_TRUNC:
 		switch (insn->opcode) {
 		case OP_TRUNC:
diff --git a/validation/optim/and-or-trunc0.c b/validation/optim/and-or-trunc0.c
index 873cb2d5e..3d326b6af 100644
--- a/validation/optim/and-or-trunc0.c
+++ b/validation/optim/and-or-trunc0.c
@@ -6,7 +6,6 @@ char foo(int x, int y)
 /*
  * check-name: and-or-trunc0
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-excludes: and\\.
diff --git a/validation/optim/and-or-trunc1.c b/validation/optim/and-or-trunc1.c
index 84c20317d..6bbe8a8cf 100644
--- a/validation/optim/and-or-trunc1.c
+++ b/validation/optim/and-or-trunc1.c
@@ -6,7 +6,6 @@ char foo(int x, int y)
 /*
  * check-name: and-or-trunc1
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-excludes: and\\.
diff --git a/validation/optim/and-or-trunc2.c b/validation/optim/and-or-trunc2.c
index 04cb57e79..e66c1f142 100644
--- a/validation/optim/and-or-trunc2.c
+++ b/validation/optim/and-or-trunc2.c
@@ -6,7 +6,6 @@ char foo(int x, int y)
 /*
  * check-name: and-or-trunc2
  * check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-pattern(1): and\\.
diff --git a/validation/optim/and-or-truncx.c b/validation/optim/and-or-truncx.c
index 47d80daec..ef8249a10 100644
--- a/validation/optim/and-or-truncx.c
+++ b/validation/optim/and-or-truncx.c
@@ -6,7 +6,6 @@ char foo(int x, int y, int b)
 /*
  * check-name: and-or-truncx
  * 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