Hi,
We missed folding (convert)(X op const) -> (convert)X op (convert)const for
unsigned narrowing because of reason reported at
https://gcc.gnu.org/ml/gcc/2016-07/msg00126.html
This patch fixes the issue by adding new match&simplify pattern, it also adds a
test case. This is the prerequisite patch for next patch adding new
vectorization pattern.
Bootstrap and test along with the next patch on x86_64 and AArch64. Is it OK?
Thanks,
bin
2016-10-11 Bin Cheng <bin.ch...@arm.com>
* match.pd ((convert (X op C))): Fold type narrowing for unsigned
operation on constant.
gcc/testsuite/ChangeLog
2016-10-11 Bin Cheng <bin.ch...@arm.com>
* gcc.dg/fold-narrow-unsgn-opcst.c: New test.
diff --git a/gcc/match.pd b/gcc/match.pd
index 786cf4c..7875424 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1731,6 +1731,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>= inside_prec - !inside_unsignedp)
(convert @0)))))))
+/* (convert (X op C)) -> ((convert)X op (convert)C) if it is narrowing
+ conversion and both types wrap when overflow. */
+(for op (plus minus)
+ (simplify
+ (convert (op @0 @1))
+ (if (INTEGRAL_TYPE_P (type)
+ && TYPE_OVERFLOW_WRAPS (type)
+ && TREE_CODE (@1) == INTEGER_CST
+ && INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
+ && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
+ (op (convert @0) (convert @1)))))
+
/* If we have a narrowing conversion to an integral type that is fed by a
BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
masks off bits outside the final type (and nothing else). */
diff --git a/gcc/testsuite/gcc.dg/fold-narrow-unsgn-opcst.c
b/gcc/testsuite/gcc.dg/fold-narrow-unsgn-opcst.c
new file mode 100644
index 0000000..aff96a9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fold-narrow-unsgn-opcst.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-gimple" } */
+
+unsigned char foo (unsigned short s)
+{
+ return (unsigned char)(s + 65530);
+}
+/* { dg-final { scan-tree-dump-not " 65530" "gimple" } } */