aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-11-25 23:01:48 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-11-27 22:19:41 +0100
commitcafabc769e77f14e47ab44866b304e51af42c44c (patch)
tree4d8a3870d76d1ae0f0e304ecaab8cae22b180aac
parent2b77484ed79f2d2edb032b5922236575e57dfb5c (diff)
downloadsparse-cafabc769e77f14e47ab44866b304e51af42c44c.tar.gz
convert SEL(x & BIT1, BIT2, 0) into SHIFT(x & BIT1, S)
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>
-rw-r--r--simplify.c15
-rw-r--r--validation/optim/select-and-shift.c1
2 files changed, 15 insertions, 1 deletions
diff --git a/simplify.c b/simplify.c
index 0d9391e2..fc64e5b7 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 fbe044c7..5313fe4b 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