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@xxxxxxxxxxxxxxxxxxxx> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- simplify.c | 15 +++++++++++++++ validation/optim/select-and-shift.c | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 0d9391e21a56..fc64e5b77adf 100644 --- a/simplify.c +++ b/simplify.c @@ -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; } diff --git a/validation/optim/select-and-shift.c b/validation/optim/select-and-shift.c index fbe044c7cb44..5313fe4b1d86 100644 --- a/validation/optim/select-and-shift.c +++ b/validation/optim/select-and-shift.c @@ -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 -- 2.29.2