1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp
31 tree_expr_nonnegative_p)
34 /* Simplifications of operations with one constant operand and
35 simplifications to constants or single values. */
37 (for op (plus pointer_plus minus bit_ior bit_xor)
42 /* 0 +p index -> (type)index */
44 (pointer_plus integer_zerop @1)
45 (non_lvalue (convert @1)))
48 This is unsafe for certain floats even in non-IEEE formats.
49 In IEEE, it is unsafe because it does wrong for NaNs.
50 Also note that operand_equal_p is always false if an operand
54 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
55 { build_zero_cst (type); }))
58 (mult @0 integer_zerop@1)
61 /* Make sure to preserve divisions by zero. This is the reason why
62 we don't simplify x / x to 1 or 0 / x to 0. */
63 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
68 /* Same applies to modulo operations, but fold is inconsistent here
69 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
70 (for op (ceil_mod floor_mod round_mod trunc_mod)
71 /* 0 % X is always zero. */
73 (op integer_zerop@0 @1)
74 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
75 (if (!integer_zerop (@1))
77 /* X % 1 is always zero. */
80 { build_zero_cst (type); }))
84 (bit_ior @0 integer_all_onesp@1)
89 (bit_and @0 integer_zerop@1)
95 { build_zero_cst (type); })
97 /* Canonicalize X ^ ~0 to ~X. */
99 (bit_xor @0 integer_all_onesp@1)
104 (bit_and @0 integer_all_onesp)
107 /* x & x -> x, x | x -> x */
108 (for bitop (bit_and bit_ior)
117 (abs tree_expr_nonnegative_p@0)
121 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
123 For bitwise binary operations apply operand conversions to the
124 binary operation result instead of to the operands. This allows
125 to combine successive conversions and bitwise binary operations.
126 We combine the above two cases by using a conditional convert. */
127 (for bitop (bit_and bit_ior bit_xor)
129 (bitop (convert @0) (convert? @1))
130 (if (((TREE_CODE (@1) == INTEGER_CST
131 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
132 && int_fits_type_p (@1, TREE_TYPE (@0)))
133 || (GIMPLE && types_compatible_p (TREE_TYPE (@0), TREE_TYPE (@1)))
134 || (GENERIC && TREE_TYPE (@0) == TREE_TYPE (@1)))
135 /* ??? This transform conflicts with fold-const.c doing
136 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
137 constants (if x has signed type, the sign bit cannot be set
138 in c). This folds extension into the BIT_AND_EXPR.
139 Restrict it to GIMPLE to avoid endless recursions. */
140 && (bitop != BIT_AND_EXPR || GIMPLE)
141 && (/* That's a good idea if the conversion widens the operand, thus
142 after hoisting the conversion the operation will be narrower. */
143 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
144 /* It's also a good idea if the conversion is to a non-integer
146 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
147 /* Or if the precision of TO is not the same as the precision
149 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
150 (convert (bitop @0 (convert @1))))))
152 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
153 (for bitop (bit_and bit_ior bit_xor)
155 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
156 (bit_and (bitop @0 @2) @1)))
158 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
160 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
161 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
163 /* Combine successive equal operations with constants. */
164 (for bitop (bit_and bit_ior bit_xor)
166 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
167 (bitop @0 (bitop @1 @2))))
169 /* Try simple folding for X op !X, and X op X with the help
170 of the truth_valued_p and logical_inverted_value predicates. */
171 (match truth_valued_p
173 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
174 (for op (lt le eq ne ge gt truth_and truth_andif truth_or truth_orif truth_xor)
175 (match truth_valued_p
177 (match truth_valued_p
180 (match (logical_inverted_value @0)
181 (bit_not truth_valued_p@0))
182 (match (logical_inverted_value @0)
183 (eq @0 integer_zerop)
184 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))))
185 (match (logical_inverted_value @0)
186 (ne truth_valued_p@0 integer_onep)
187 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))))
188 (match (logical_inverted_value @0)
189 (bit_xor truth_valued_p@0 integer_onep))
193 (bit_and:c @0 (logical_inverted_value @0))
194 { build_zero_cst (type); })
195 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
196 (for op (bit_ior bit_xor)
198 (op:c truth_valued_p@0 (logical_inverted_value @0))
199 { build_one_cst (type); }))
201 (for bitop (bit_and bit_ior)
202 rbitop (bit_ior bit_and)
203 /* (x | y) & x -> x */
204 /* (x & y) | x -> x */
206 (bitop:c (rbitop:c @0 @1) @0)
208 /* (~x | y) & x -> x & y */
209 /* (~x & y) | x -> x | y */
211 (bitop:c (rbitop:c (bit_not @0) @1) @0)
214 /* If arg1 and arg2 are booleans (or any single bit type)
215 then try to simplify:
222 But only do this if our result feeds into a comparison as
223 this transformation is not always a win, particularly on
224 targets with and-not instructions.
225 -> simplify_bitwise_binary_boolean */
227 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
228 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
229 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
232 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
233 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
234 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
239 (bit_not (bit_not @0))
247 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
249 (pointer_plus (pointer_plus @0 @1) @3)
250 (pointer_plus @0 (plus @1 @3)))
256 tem4 = (unsigned long) tem3;
261 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
262 /* Conditionally look through a sign-changing conversion. */
263 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
264 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
265 || (GENERIC && type == TREE_TYPE (@1))))
269 tem = (sizetype) ptr;
273 and produce the simpler and easier to analyze with respect to alignment
274 ... = ptr & ~algn; */
276 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
277 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
278 (bit_and @0 { algn; })))
281 /* Simplifications of conversions. */
283 /* Basic strip-useless-type-conversions / strip_nops. */
284 (for cvt (convert view_convert float fix_trunc)
287 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
288 || (GENERIC && type == TREE_TYPE (@0)))
291 /* Contract view-conversions. */
293 (view_convert (view_convert @0))
296 /* For integral conversions with the same precision or pointer
297 conversions use a NOP_EXPR instead. */
300 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
301 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
302 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
305 /* Strip inner integral conversions that do not change precision or size. */
307 (view_convert (convert@0 @1))
308 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
309 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
310 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
311 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
314 /* Re-association barriers around constants and other re-association
315 barriers can be removed. */
317 (paren CONSTANT_CLASS_P@0)