The instructions SHL(AND(x, M), S) can be simplified to 0 if (M << S) == 0. For example code like: unsigned foo(unsigned x) { return (x & 0xfff00000) << 12; } is now simplified into: foo: ret.32 $0 Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- simplify.c | 11 +++++++++++ validation/optim/shl-and0.c | 1 - 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 8ace8a5da..5c1c98088 100644 --- a/simplify.c +++ b/simplify.c @@ -689,6 +689,17 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v if (value >= size) goto zero; switch(DEF_OPCODE(def, pseudo)) { + case OP_AND: + // simplify (A & M) << S + if (!constant(def->src2)) + break; + mask = bits_mask(insn->size) >> value; + omask = def->src2->value; + nmask = omask & mask; + if (nmask == 0) + return replace_with_pseudo(insn, value_pseudo(0)); + // do not simplify into ((A << S) & (M << S)) + break; case OP_LSR: // replace ((x >> S) << S) // by (x & (-1 << S)) diff --git a/validation/optim/shl-and0.c b/validation/optim/shl-and0.c index 63b20501c..6cbe8e58f 100644 --- a/validation/optim/shl-and0.c +++ b/validation/optim/shl-and0.c @@ -7,7 +7,6 @@ unsigned foo(unsigned x) /* * check-name: shl-and0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-contains: ret\\..*\\$0$ -- 2.18.0