@@ -2271,6 +2271,21 @@ static int simplify_select(struct instruction *insn)
// both values must be non-zero
return replace_with_pseudo(insn, src1);
}
+ case OP_AND:
+ if (is_pow2(def->src2) && is_pow2(src1) && is_zero(src2) && insn->size == def->size && one_use(cond)) {
+ unsigned s1 = log2_exact(def->src2->value);
+ unsigned s2 = log2_exact(insn->src2->value);
+ unsigned shift;
+
+ if (s1 == s2)
+ return replace_with_pseudo(insn, cond);
+
+ // SEL(x & A, B, 0) --> SHIFT(x & A, S)
+ insn->opcode = (s1 < s2) ? OP_SHL : OP_LSR;
+ shift = (s1 < s2) ? (s2 - s1) : (s1 - s2);
+ insn->src2 = value_pseudo(shift);
+ return REPEAT_CSE;
+ }
break;
}
@@ -11,7 +11,6 @@ int bar(int p) { return ((p & B) ? A : 0) == ((((unsigned)p) & B) >> S); }
/*
* check-name: select-and-shift
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-returns: 1
Convert an expression like: (x & (1 << A)) ? (1 << B) : 0 into: (x & (1 << A)) << (B - A) or: (x & (1 << A)) >> (A - B) Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- simplify.c | 15 +++++++++++++++ validation/optim/select-and-shift.c | 1 - 2 files changed, 15 insertions(+), 1 deletion(-)