@@ -1266,10 +1266,14 @@ static int simplify_compare_constant(struct instruction *insn, long long value)
case OP_SET_EQ:
if ((value & bits) != value)
return replace_with_value(insn, 0);
+ if (value == bits && is_power_of_2(bits))
+ return replace_binop_value(insn, OP_SET_NE, 0);
break;
case OP_SET_NE:
if ((value & bits) != value)
return replace_with_value(insn, 1);
+ if (value == bits && is_power_of_2(bits))
+ return replace_binop_value(insn, OP_SET_EQ, 0);
break;
case OP_SET_LE:
value = sign_extend(value, def->size);
new file mode 100644
@@ -0,0 +1,12 @@
+#define M 32
+
+_Bool eq(int a) { return ((a & M) != M) == ((a & M) == 0); }
+_Bool ne(int a) { return ((a & M) == M) == ((a & M) != 0); }
+
+/*
+ * check-name: cmp-and-pow2
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-returns: 1
+ */
and same for its dual: ((x & M) != M) --> ((x & M) == 0) Beside the canonicalization itself, these simplifications are useful because the compare against 0 can often be further simplified (for example when it is used by OP_CBR or OP_SELECT). Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- simplify.c | 4 ++++ validation/optim/cmp-and-pow2.c | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 validation/optim/cmp-and-pow2.c