aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-06-29 18:51:03 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-10-23 17:44:52 +0200
commit5c5dc0a95fd65a7fbbb05ba9f018bc8dfebaa97d (patch)
tree3a0377160c9cfb8ea232b6d8370f65e600e08f9d
parenta80921db90cd8de52b2fa37bf61e6156dd3a6bcd (diff)
downloadsparse-5c5dc0a95fd65a7fbbb05ba9f018bc8dfebaa97d.tar.gz
canonicalize unsigned compares against 0 or 1
Some unsigned compares against 0 or 1 are equivalent to testing equality with 0 (x <= 0, x > 0, x < 1, x >= 1). Canonicalize them to this later, more common form. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--simplify.c20
-rw-r--r--validation/optim/set-uimm0.c6
2 files changed, 25 insertions, 1 deletions
diff --git a/simplify.c b/simplify.c
index 4441b27c..96cd73a0 100644
--- a/simplify.c
+++ b/simplify.c
@@ -1176,11 +1176,31 @@ static int simplify_constant_rightside(struct instruction *insn)
case OP_SET_B:
if (!value) { // (x < 0) --> 0
return replace_with_pseudo(insn, value_pseudo(0));
+ } else if (value == 1) { // (x < 1) --> (x == 0)
+ insn->src2 = value_pseudo(0);
+ insn->opcode = OP_SET_EQ;
+ return REPEAT_CSE;
}
break;
case OP_SET_AE:
if (!value) { // (x >= 0) --> 1
return replace_with_pseudo(insn, value_pseudo(1));
+ } else if (value == 1) { // (x >= 1) --> (x != 0)
+ insn->src2 = value_pseudo(0);
+ insn->opcode = OP_SET_NE;
+ return REPEAT_CSE;
+ }
+ break;
+ case OP_SET_BE:
+ if (!value) { // (x <= 0) --> (x == 0)
+ insn->opcode = OP_SET_EQ;
+ return REPEAT_CSE;
+ }
+ break;
+ case OP_SET_A:
+ if (!value) { // (x > 0) --> (x != 0)
+ insn->opcode = OP_SET_NE;
+ return REPEAT_CSE;
}
break;
}
diff --git a/validation/optim/set-uimm0.c b/validation/optim/set-uimm0.c
index 1f62358f..ded8fc82 100644
--- a/validation/optim/set-uimm0.c
+++ b/validation/optim/set-uimm0.c
@@ -1,10 +1,14 @@
static _Bool setlt0(unsigned int a) { return (a < 0u) == 0; }
static _Bool setge0(unsigned int a) { return (a >= 0u) == 1; }
+static _Bool setle0(unsigned int a) { return (a <= 0u) == (a == 0); }
+static _Bool setgt0(unsigned int a) { return (a > 0u) == (a != 0); }
+static _Bool setlt1(unsigned int a) { return (a < 1u) == (a == 0); }
+static _Bool setge1(unsigned int a) { return (a >= 1u) == (a != 0); }
/*
* check-name: set-uimm0
* check-command: test-linearize $file
*
* check-output-ignore
- * check-output-pattern(2): ret\\.1 *\\$1
+ * check-output-pattern(6): ret\\.1 *\\$1
*/