aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-08-18 18:42:17 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-08-22 09:26:18 +0200
commitc68bfab0e487452d17534393efaa0c8da165aff0 (patch)
tree872e9ed273e53ff0604b184dd4697adf2f4e4a6b
parent8e5ef1b17efcef79a75747278d0e8116a85a0908 (diff)
downloadsparse-dev-c68bfab0e487452d17534393efaa0c8da165aff0.tar.gz
simplify ((x & M) >> S) when (M >> S) == 0
The instructions LSR(AND(x, M), S) are already simplified into AND(LSR(x, S), (M >> S)) but only if AND(x, M) has a single user. However, if (M >> S) == 0, they can always directly be simplified to 0. For example code like: unsigned foo(unsigned x) { unsigned t = (x & 0x00000fff); return (t >> 12) & t; } is now simplified into: foo: ret.32 $0 while previously it was: foo: and.32 %r2 <- %arg1, $0xfff lsr.32 %r4 <- %r2, $12 and.32 %r6 <- %r4, %r2 ret.32 %r6 Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--simplify.c6
-rw-r--r--validation/optim/lsr-and0.c1
2 files changed, 5 insertions, 2 deletions
diff --git a/simplify.c b/simplify.c
index 21d78603..0511aa67 100644
--- a/simplify.c
+++ b/simplify.c
@@ -621,7 +621,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, omask;
+ unsigned long long mask, omask, nmask;
unsigned long long nval;
unsigned int size;
pseudo_t src2;
@@ -680,7 +680,11 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
// by (A >> S) & (M >> S)
if (!constant(def->src2))
break;
+ mask = bits_mask(insn->size - value) << value;
omask = def->src2->value;
+ nmask = omask & mask;
+ if (nmask == 0)
+ return replace_with_pseudo(insn, value_pseudo(0));
if (nbr_users(pseudo) > 1)
break;
def->opcode = OP_LSR;
diff --git a/validation/optim/lsr-and0.c b/validation/optim/lsr-and0.c
index 292c0332..94310ba8 100644
--- a/validation/optim/lsr-and0.c
+++ b/validation/optim/lsr-and0.c
@@ -7,7 +7,6 @@ unsigned lsr_and0(unsigned x)
/*
* check-name: lsr-and0
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-contains: ret\\..*\\$0$