aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-10-22 01:13:02 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-11-08 02:13:04 +0100
commit2680e82101a685cca986bf619bb1dd21e0573af8 (patch)
treeb6781de3fa05341044feea75e3810a73fd65244f
parent4146aecb9917aaad05c560280089e2557f113126 (diff)
downloadsparse-2680e82101a685cca986bf619bb1dd21e0573af8.tar.gz
cmp: canonicalize unsigned compare with UMAX or UMAX-1
Unsigned compares with UMAX (or UMAX-1) are equivalent to equality tests. These are preferable since it's easier to reason about them in other simplifications. So canonicalize these compares to equality tests. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--simplify.c8
-rw-r--r--validation/optim/set-uimm2.c1
2 files changed, 8 insertions, 1 deletions
diff --git a/simplify.c b/simplify.c
index 160a6ab1..7921300f 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1090,6 +1090,8 @@ static int simplify_compare_constant(struct instruction *insn, long long value)
return replace_with_pseudo(insn, value_pseudo(0));
if (value == 1) // (x < 1) --> (x == 0)
return replace_binop_value(insn, OP_SET_EQ, 0);
+ else if (value == bits) // (x < ~0) --> (x != ~0)
+ return replace_binop_value(insn, OP_SET_NE, value);
else // (x < y) --> (x <= (y-1))
changed |= replace_binop_value(insn, OP_SET_BE, value - 1);
break;
@@ -1098,6 +1100,8 @@ static int simplify_compare_constant(struct instruction *insn, long long value)
return replace_with_pseudo(insn, value_pseudo(1));
if (value == 1) // (x >= 1) --> (x != 0)
return replace_binop_value(insn, OP_SET_NE, 0);
+ else if (value == bits) // (x >= ~0) --> (x == ~0)
+ return replace_binop_value(insn, OP_SET_EQ, value);
else // (x >= y) --> (x > (y-1)
changed |= replace_binop_value(insn, OP_SET_A, value - 1);
break;
@@ -1106,12 +1110,16 @@ static int simplify_compare_constant(struct instruction *insn, long long value)
return replace_opcode(insn, OP_SET_EQ);
if (value == bits) // (x <= ~0) --> 1
return replace_with_pseudo(insn, value_pseudo(1));
+ if (value == (bits - 1)) // (x <= ~1) --> (x != ~0)
+ return replace_binop_value(insn, OP_SET_NE, bits);
break;
case OP_SET_A:
if (!value) // (x > 0) --> (x != 0)
return replace_opcode(insn, OP_SET_NE);
if (value == bits) // (x > ~0) --> 0
return replace_with_pseudo(insn, value_pseudo(0));
+ if (value == (bits - 1)) // (x > ~1) --> (x == ~0)
+ return replace_binop_value(insn, OP_SET_EQ, bits);
break;
}
return changed;
diff --git a/validation/optim/set-uimm2.c b/validation/optim/set-uimm2.c
index efa326f5..9138ae72 100644
--- a/validation/optim/set-uimm2.c
+++ b/validation/optim/set-uimm2.c
@@ -6,7 +6,6 @@ static _Bool setgt_umax(unsigned int a) { return (a > ~1) == (a == ~0); }
/*
* check-name: set-uimm2
* check-command: test-linearize $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-returns: 1