re PR tree-optimization/63747 (icf mis-compares switch gimple)
[gcc.git] / gcc / match.pd
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.
4
5 Copyright (C) 2014 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9 This file is part of GCC.
10
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
14 version.
15
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
19 for more details.
20
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/>. */
24
25
26 /* Generic tree predicates we inherit. */
27 (define_predicates
28 integer_onep integer_zerop integer_all_onesp
29 real_zerop real_onep
30 CONSTANT_CLASS_P
31 tree_expr_nonnegative_p)
32
33
34 /* Simplifications of operations with one constant operand and
35 simplifications to constants or single values. */
36
37 (for op (plus pointer_plus minus bit_ior bit_xor)
38 (simplify
39 (op @0 integer_zerop)
40 (non_lvalue @0)))
41
42 /* 0 +p index -> (type)index */
43 (simplify
44 (pointer_plus integer_zerop @1)
45 (non_lvalue (convert @1)))
46
47 /* Simplify x - x.
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
51 is volatile. */
52 (simplify
53 (minus @0 @0)
54 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
55 { build_zero_cst (type); }))
56
57 (simplify
58 (mult @0 integer_zerop@1)
59 @1)
60
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)
64 (simplify
65 (op @0 integer_onep)
66 (non_lvalue @0)))
67
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. */
72 (simplify
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))
76 @0))
77 /* X % 1 is always zero. */
78 (simplify
79 (op @0 integer_onep)
80 { build_zero_cst (type); }))
81
82 /* x | ~0 -> ~0 */
83 (simplify
84 (bit_ior @0 integer_all_onesp@1)
85 @1)
86
87 /* x & 0 -> 0 */
88 (simplify
89 (bit_and @0 integer_zerop@1)
90 @1)
91
92 /* x ^ x -> 0 */
93 (simplify
94 (bit_xor @0 @0)
95 { build_zero_cst (type); })
96
97 /* Canonicalize X ^ ~0 to ~X. */
98 (simplify
99 (bit_xor @0 integer_all_onesp@1)
100 (bit_not @0))
101
102 /* x & ~0 -> x */
103 (simplify
104 (bit_and @0 integer_all_onesp)
105 (non_lvalue @0))
106
107 /* x & x -> x, x | x -> x */
108 (for bitop (bit_and bit_ior)
109 (simplify
110 (bitop @0 @0)
111 (non_lvalue @0)))
112
113 (simplify
114 (abs (negate @0))
115 (abs @0))
116 (simplify
117 (abs tree_expr_nonnegative_p@0)
118 @0)
119
120
121 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
122 when profitable.
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)
128 (simplify
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
145 mode. */
146 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
147 /* Or if the precision of TO is not the same as the precision
148 of its mode. */
149 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
150 (convert (bitop @0 (convert @1))))))
151
152 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
153 (for bitop (bit_and bit_ior bit_xor)
154 (simplify
155 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
156 (bit_and (bitop @0 @2) @1)))
157
158 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
159 (simplify
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)))
162
163 /* Combine successive equal operations with constants. */
164 (for bitop (bit_and bit_ior bit_xor)
165 (simplify
166 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
167 (bitop @0 (bitop @1 @2))))
168
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
172 @0
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
176 (op @0 @1)))
177 (match truth_valued_p
178 (truth_not @0))
179
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))
190
191 /* X & !X -> 0. */
192 (simplify
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)
197 (simplify
198 (op:c truth_valued_p@0 (logical_inverted_value @0))
199 { build_one_cst (type); }))
200
201 (for bitop (bit_and bit_ior)
202 rbitop (bit_ior bit_and)
203 /* (x | y) & x -> x */
204 /* (x & y) | x -> x */
205 (simplify
206 (bitop:c (rbitop:c @0 @1) @0)
207 @0)
208 /* (~x | y) & x -> x & y */
209 /* (~x & y) | x -> x | y */
210 (simplify
211 (bitop:c (rbitop:c (bit_not @0) @1) @0)
212 (bitop @0 @1)))
213
214 /* If arg1 and arg2 are booleans (or any single bit type)
215 then try to simplify:
216
217 (~X & Y) -> X < Y
218 (X & ~Y) -> Y < X
219 (~X | Y) -> X <= Y
220 (X | ~Y) -> Y <= X
221
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 */
226 (simplify
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)
230 (lt @0 @1)))
231 (simplify
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)
235 (le @0 @1)))
236
237 /* ~~x -> x */
238 (simplify
239 (bit_not (bit_not @0))
240 @0)
241
242 (simplify
243 (negate (negate @0))
244 @0)
245
246
247 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
248 (simplify
249 (pointer_plus (pointer_plus @0 @1) @3)
250 (pointer_plus @0 (plus @1 @3)))
251
252 /* Pattern match
253 tem1 = (long) ptr1;
254 tem2 = (long) ptr2;
255 tem3 = tem2 - tem1;
256 tem4 = (unsigned long) tem3;
257 tem5 = ptr1 + tem4;
258 and produce
259 tem5 = ptr2; */
260 (simplify
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))))
266 @1))
267
268 /* Pattern match
269 tem = (sizetype) ptr;
270 tem = tem & algn;
271 tem = -tem;
272 ... = ptr p+ tem;
273 and produce the simpler and easier to analyze with respect to alignment
274 ... = ptr & ~algn; */
275 (simplify
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; })))
279
280
281 /* Simplifications of conversions. */
282
283 /* Basic strip-useless-type-conversions / strip_nops. */
284 (for cvt (convert view_convert float fix_trunc)
285 (simplify
286 (cvt @0)
287 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
288 || (GENERIC && type == TREE_TYPE (@0)))
289 @0)))
290
291 /* Contract view-conversions. */
292 (simplify
293 (view_convert (view_convert @0))
294 (view_convert @0))
295
296 /* For integral conversions with the same precision or pointer
297 conversions use a NOP_EXPR instead. */
298 (simplify
299 (view_convert @0)
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)))
303 (convert @0)))
304
305 /* Strip inner integral conversions that do not change precision or size. */
306 (simplify
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))))
312 (view_convert @1)))
313
314 /* Re-association barriers around constants and other re-association
315 barriers can be removed. */
316 (simplify
317 (paren CONSTANT_CLASS_P@0)
318 @0)
319 (simplify
320 (paren (paren@1 @0))
321 @1)