In an expression like OP((x | C), K), if the effective mask (M) corresponding to OP(_, K) is equal to the combined mask (C & M), then the OR operation is unneeded and can be replaced by M itself, giving: OP(M, K). In mathematical terms: 0) ((x | C) & M) = ((x & M) | (C & M)) 1) (C & M) = M 2) ((x & M) | (C & M)) = ((x & M) | M) = M and so OP((x | C), K) -> OP(M, K). For example, code like: unsigned int foo(int x) { return (x | 7) & 2; } is now simplified into: foo: ret.32 $2 which previously was not optimized foo: or.32 %r2 <- %arg1, $7 and.32 %r3 <- %r2, $2 ret.32 %r3 Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- simplify.c | 4 ++++ validation/optim/and-or-constant1.c | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 6c4546fcb..a36083789 100644 --- a/simplify.c +++ b/simplify.c @@ -622,6 +622,10 @@ static int simplify_mask_or(struct instruction *insn, unsigned long long mask, s // if (C & M) == 0: OR(x, C) -> x return replace_pseudo(insn, &insn->src1, src1); } + if (nval == mask) { + // if (C & M) == M: OR(x, C) -> M + return replace_pseudo(insn, &insn->src1, value_pseudo(mask)); + } } return 0; } diff --git a/validation/optim/and-or-constant1.c b/validation/optim/and-or-constant1.c index 3f1c90528..49823d5ca 100644 --- a/validation/optim/and-or-constant1.c +++ b/validation/optim/and-or-constant1.c @@ -6,7 +6,6 @@ int foo(int x) /* * check-name: or-and-constant1 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-contains: ret\\..*\\$0xfff -- 2.18.0