aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-01-29 11:48:04 +0100
committerChristopher Li <sparse@chrisli.org>2017-02-13 09:34:45 +0800
commita903d3a56a9e0d51f2603815e5d2394b1c94c57a (patch)
tree3a77fef5fa1c956c6f064221e352325aa27f5fab
parente8cb94f5ecd7ecc5eae7e03c62cbcfe3886f96fb (diff)
downloadsparse-dev-a903d3a56a9e0d51f2603815e5d2394b1c94c57a.tar.gz
fix killing OP_CAST & friends
Currently kill_instruction() doesn't do anything with the operands of casts instructions. But when theses instructions are removed the operands 'usage' must be adjusted and this is not done and as result the instructions producing the operands of these casts are not optimized away as expected. This patch fixes that by killing these casts the same way as others unary instructions (OP_NOT & OP_NEG). To illustrate the situation, the output of test-linearize on the following code: extern void __abort(void); struct s { int elem:3; }; void foo(struct s *x); void foo(struct s *x) { if (x->elem == 0) { if (x->elem != 0 && x->elem != 1) __abort(); } } gives this output without the patch: foo: load.32 %r2 <- 0[%arg1] cast.32 %r3 <- (3) %r2 br .L1 .L1: ret Since x->elem can't be at the same time == 0 & != 0, the inner if is never true and the whole code should have been optimized away. The 'cast' instruction is obviously not needed but nevertheless present. With the patch, the output is much closer to what's expected: foo: load.32 %r2 <- 0[%arg1] br .L1 .L1: ret Note 1) The 'load' instruction is also dead but it's a separate problem. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> Signed-off-by: Christopher Li <sparse@chrisli.org>
-rw-r--r--simplify.c4
-rw-r--r--validation/kill-casts.c22
2 files changed, 26 insertions, 0 deletions
diff --git a/simplify.c b/simplify.c
index 90998021..fc6bae79 100644
--- a/simplify.c
+++ b/simplify.c
@@ -195,6 +195,10 @@ void kill_instruction(struct instruction *insn)
repeat_phase |= REPEAT_CSE;
return;
+ case OP_CAST:
+ case OP_SCAST:
+ case OP_FPCAST:
+ case OP_PTRCAST:
case OP_NOT: case OP_NEG:
insn->bb = NULL;
kill_use(&insn->src1);
diff --git a/validation/kill-casts.c b/validation/kill-casts.c
new file mode 100644
index 00000000..cf52f246
--- /dev/null
+++ b/validation/kill-casts.c
@@ -0,0 +1,22 @@
+extern void __abort(void);
+
+struct s {
+ int elem:3;
+};
+
+void foo(struct s *x);
+void foo(struct s *x)
+{
+ if (x->elem == 0) {
+ if (x->elem != 0 && x->elem != 1)
+ __abort();
+ }
+}
+
+/*
+ * check-name: kill-casts
+ * check-command: test-linearize $file
+ *
+ * check-output-ignore
+ * check-output-excludes: cast\\.
+ */