diff options
| author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-08-08 01:04:26 +0200 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-08-22 23:28:07 +0200 |
| commit | 24e2b3800cc5451a509b53e8120f0e009da6f582 (patch) | |
| tree | e39c718afdf4962f893d45786508dde21277a541 | |
| parent | a4ae84a892b6e5a8b563f0bb125fcc5a3448b383 (diff) | |
| download | sparse-dev-24e2b3800cc5451a509b53e8120f0e009da6f582.tar.gz | |
simplify TRUNC((x & M') | y, N)
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@gmail.com>
| -rw-r--r-- | simplify.c | 8 | ||||
| -rw-r--r-- | validation/optim/and-or-trunc0.c | 1 | ||||
| -rw-r--r-- | validation/optim/and-or-trunc1.c | 1 | ||||
| -rw-r--r-- | validation/optim/and-or-trunc2.c | 1 | ||||
| -rw-r--r-- | validation/optim/and-or-truncx.c | 1 |
5 files changed, 8 insertions, 4 deletions
@@ -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 873cb2d5..3d326b6a 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 84c20317..6bbe8a8c 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 04cb57e7..e66c1f14 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 47d80dae..ef8249a1 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\\. |
