aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-07-18 16:06:28 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-07-25 18:47:09 +0200
commit081b35e3a3d2ea50132b68816816966eceb6377f (patch)
tree71dfc77fc808f2cf84a5fee15182a36f82ade389
parent52579227fdc1978bb12be250ab063deaeccbde0f (diff)
downloadsparse-dev-081b35e3a3d2ea50132b68816816966eceb6377f.tar.gz
shift: simplify ASR(ZEXT(X, N), C)
After an OP_ZEXT, it is guaranteed that the sign bit is zero. So, an arithmetic right-shift following an OP_ZEXT gives the same result as an logical right-shift which is simpler to handle and further optimize. So, when preceded by an OP_ZEXT, replace OP_ASR by OP_LSR. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--simplify.c10
-rw-r--r--validation/optim/zext-asr.c13
2 files changed, 23 insertions, 0 deletions
diff --git a/simplify.c b/simplify.c
index f360c2e9..31d1a9ba 100644
--- a/simplify.c
+++ b/simplify.c
@@ -606,6 +606,16 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
else if (value >= size)
value = size - 1;
goto new_value;
+
+ case OP_ZEXT:
+ // transform:
+ // zext.N %t <- (O) %a
+ // asr.N %r <- %t, C
+ // into
+ // zext.N %t <- (O) %a
+ // lsr.N %r <- %t, C
+ insn->opcode = OP_LSR;
+ return REPEAT_CSE;
}
break;
case OP_LSR:
diff --git a/validation/optim/zext-asr.c b/validation/optim/zext-asr.c
new file mode 100644
index 00000000..5f235ad8
--- /dev/null
+++ b/validation/optim/zext-asr.c
@@ -0,0 +1,13 @@
+unsigned short foo(unsigned short a)
+{
+ return a >> 16;
+}
+
+/*
+ * check-name: zext-asr
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-contains: ret\\..*\\$0
+ * check-output-excludes: asr\\.
+ */