]>
Commit | Line | Data |
---|---|---|
cbdd87d4 | 1 | /* Statement simplification on GIMPLE. |
23a5b65a | 2 | Copyright (C) 2010-2014 Free Software Foundation, Inc. |
cbdd87d4 RG |
3 | Split out from tree-ssa-ccp.c. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
9 | Free Software Foundation; either version 3, or (at your option) any | |
10 | later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
d8a2d370 DN |
26 | #include "stringpool.h" |
27 | #include "expr.h" | |
28 | #include "stmt.h" | |
29 | #include "stor-layout.h" | |
cbdd87d4 | 30 | #include "flags.h" |
cbdd87d4 | 31 | #include "function.h" |
7ee2468b | 32 | #include "dumpfile.h" |
442b4905 | 33 | #include "bitmap.h" |
2fb9a547 AM |
34 | #include "basic-block.h" |
35 | #include "tree-ssa-alias.h" | |
36 | #include "internal-fn.h" | |
37 | #include "gimple-fold.h" | |
38 | #include "gimple-expr.h" | |
39 | #include "is-a.h" | |
18f429e2 | 40 | #include "gimple.h" |
45b0be94 | 41 | #include "gimplify.h" |
5be5c238 | 42 | #include "gimple-iterator.h" |
442b4905 AM |
43 | #include "gimple-ssa.h" |
44 | #include "tree-ssanames.h" | |
45 | #include "tree-into-ssa.h" | |
46 | #include "tree-dfa.h" | |
7a300452 | 47 | #include "tree-ssa.h" |
cbdd87d4 | 48 | #include "tree-ssa-propagate.h" |
cbdd87d4 | 49 | #include "target.h" |
450ad0cd JH |
50 | #include "ipa-utils.h" |
51 | #include "gimple-pretty-print.h" | |
4484a35a | 52 | #include "tree-ssa-address.h" |
862d0b35 | 53 | #include "langhooks.h" |
19e51b40 | 54 | #include "gimplify-me.h" |
2b5f0895 | 55 | #include "dbgcnt.h" |
9b2b7279 | 56 | #include "builtins.h" |
cbdd87d4 | 57 | |
b3b9f3d0 | 58 | /* Return true when DECL can be referenced from current unit. |
c44c2088 JH |
59 | FROM_DECL (if non-null) specify constructor of variable DECL was taken from. |
60 | We can get declarations that are not possible to reference for various | |
61 | reasons: | |
1389294c | 62 | |
1389294c JH |
63 | 1) When analyzing C++ virtual tables. |
64 | C++ virtual tables do have known constructors even | |
65 | when they are keyed to other compilation unit. | |
66 | Those tables can contain pointers to methods and vars | |
67 | in other units. Those methods have both STATIC and EXTERNAL | |
68 | set. | |
69 | 2) In WHOPR mode devirtualization might lead to reference | |
70 | to method that was partitioned elsehwere. | |
71 | In this case we have static VAR_DECL or FUNCTION_DECL | |
72 | that has no corresponding callgraph/varpool node | |
b3b9f3d0 JH |
73 | declaring the body. |
74 | 3) COMDAT functions referred by external vtables that | |
3e89949e | 75 | we devirtualize only during final compilation stage. |
b3b9f3d0 JH |
76 | At this time we already decided that we will not output |
77 | the function body and thus we can't reference the symbol | |
78 | directly. */ | |
79 | ||
1389294c | 80 | static bool |
c44c2088 | 81 | can_refer_decl_in_current_unit_p (tree decl, tree from_decl) |
1389294c | 82 | { |
2c8326a5 | 83 | varpool_node *vnode; |
1389294c | 84 | struct cgraph_node *node; |
5e20cdc9 | 85 | symtab_node *snode; |
c44c2088 | 86 | |
1632a686 JH |
87 | if (DECL_ABSTRACT (decl)) |
88 | return false; | |
89 | ||
90 | /* We are concerned only about static/external vars and functions. */ | |
91 | if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) | |
92 | || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)) | |
93 | return true; | |
94 | ||
95 | /* Static objects can be referred only if they was not optimized out yet. */ | |
96 | if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) | |
97 | { | |
3aaf0529 JH |
98 | /* Before we start optimizing unreachable code we can be sure all |
99 | static objects are defined. */ | |
100 | if (cgraph_function_flags_ready) | |
101 | return true; | |
1632a686 | 102 | snode = symtab_get_node (decl); |
3aaf0529 | 103 | if (!snode || !snode->definition) |
1632a686 | 104 | return false; |
7de90a6c | 105 | node = dyn_cast <cgraph_node *> (snode); |
1632a686 JH |
106 | return !node || !node->global.inlined_to; |
107 | } | |
108 | ||
6da8be89 | 109 | /* We will later output the initializer, so we can refer to it. |
c44c2088 | 110 | So we are concerned only when DECL comes from initializer of |
3aaf0529 | 111 | external var or var that has been optimized out. */ |
c44c2088 JH |
112 | if (!from_decl |
113 | || TREE_CODE (from_decl) != VAR_DECL | |
3aaf0529 JH |
114 | || (!DECL_EXTERNAL (from_decl) |
115 | && (vnode = varpool_get_node (from_decl)) != NULL | |
116 | && vnode->definition) | |
6da8be89 | 117 | || (flag_ltrans |
6adda80b JH |
118 | && (vnode = varpool_get_node (from_decl)) != NULL |
119 | && vnode->in_other_partition)) | |
c44c2088 | 120 | return true; |
c44c2088 JH |
121 | /* We are folding reference from external vtable. The vtable may reffer |
122 | to a symbol keyed to other compilation unit. The other compilation | |
123 | unit may be in separate DSO and the symbol may be hidden. */ | |
124 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
125 | && DECL_EXTERNAL (decl) | |
a33a931b | 126 | && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT |
67348ccc | 127 | && (!(snode = symtab_get_node (decl)) || !snode->in_other_partition)) |
c44c2088 | 128 | return false; |
b3b9f3d0 JH |
129 | /* When function is public, we always can introduce new reference. |
130 | Exception are the COMDAT functions where introducing a direct | |
131 | reference imply need to include function body in the curren tunit. */ | |
132 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
133 | return true; | |
3aaf0529 JH |
134 | /* We have COMDAT. We are going to check if we still have definition |
135 | or if the definition is going to be output in other partition. | |
136 | Bypass this when gimplifying; all needed functions will be produced. | |
c44c2088 JH |
137 | |
138 | As observed in PR20991 for already optimized out comdat virtual functions | |
073a8998 | 139 | it may be tempting to not necessarily give up because the copy will be |
c44c2088 JH |
140 | output elsewhere when corresponding vtable is output. |
141 | This is however not possible - ABI specify that COMDATs are output in | |
142 | units where they are used and when the other unit was compiled with LTO | |
143 | it is possible that vtable was kept public while the function itself | |
144 | was privatized. */ | |
3aaf0529 | 145 | if (!cgraph_function_flags_ready) |
b3b9f3d0 | 146 | return true; |
c44c2088 | 147 | |
3aaf0529 JH |
148 | snode = symtab_get_node (decl); |
149 | if (!snode | |
150 | || ((!snode->definition || DECL_EXTERNAL (decl)) | |
151 | && (!snode->in_other_partition | |
152 | || (!snode->forced_by_abi && !snode->force_output)))) | |
153 | return false; | |
154 | node = dyn_cast <cgraph_node *> (snode); | |
155 | return !node || !node->global.inlined_to; | |
1389294c JH |
156 | } |
157 | ||
0038d4e0 | 158 | /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into |
c44c2088 JH |
159 | acceptable form for is_gimple_min_invariant. |
160 | FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */ | |
17f39a39 JH |
161 | |
162 | tree | |
c44c2088 | 163 | canonicalize_constructor_val (tree cval, tree from_decl) |
17f39a39 | 164 | { |
50619002 EB |
165 | tree orig_cval = cval; |
166 | STRIP_NOPS (cval); | |
315f5f1b RG |
167 | if (TREE_CODE (cval) == POINTER_PLUS_EXPR |
168 | && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST) | |
17f39a39 | 169 | { |
315f5f1b RG |
170 | tree ptr = TREE_OPERAND (cval, 0); |
171 | if (is_gimple_min_invariant (ptr)) | |
172 | cval = build1_loc (EXPR_LOCATION (cval), | |
173 | ADDR_EXPR, TREE_TYPE (ptr), | |
174 | fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)), | |
175 | ptr, | |
176 | fold_convert (ptr_type_node, | |
177 | TREE_OPERAND (cval, 1)))); | |
17f39a39 JH |
178 | } |
179 | if (TREE_CODE (cval) == ADDR_EXPR) | |
180 | { | |
5a27a197 RG |
181 | tree base = NULL_TREE; |
182 | if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR) | |
ca5f4331 MM |
183 | { |
184 | base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0)); | |
185 | if (base) | |
186 | TREE_OPERAND (cval, 0) = base; | |
187 | } | |
5a27a197 RG |
188 | else |
189 | base = get_base_address (TREE_OPERAND (cval, 0)); | |
7501ca28 RG |
190 | if (!base) |
191 | return NULL_TREE; | |
b3b9f3d0 | 192 | |
7501ca28 RG |
193 | if ((TREE_CODE (base) == VAR_DECL |
194 | || TREE_CODE (base) == FUNCTION_DECL) | |
c44c2088 | 195 | && !can_refer_decl_in_current_unit_p (base, from_decl)) |
1389294c | 196 | return NULL_TREE; |
7501ca28 | 197 | if (TREE_CODE (base) == VAR_DECL) |
46eb666a | 198 | TREE_ADDRESSABLE (base) = 1; |
7501ca28 RG |
199 | else if (TREE_CODE (base) == FUNCTION_DECL) |
200 | { | |
201 | /* Make sure we create a cgraph node for functions we'll reference. | |
202 | They can be non-existent if the reference comes from an entry | |
203 | of an external vtable for example. */ | |
6f99e449 | 204 | cgraph_get_create_node (base); |
7501ca28 | 205 | } |
0038d4e0 | 206 | /* Fixup types in global initializers. */ |
73aef89e RG |
207 | if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0))) |
208 | cval = build_fold_addr_expr (TREE_OPERAND (cval, 0)); | |
50619002 EB |
209 | |
210 | if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval))) | |
211 | cval = fold_convert (TREE_TYPE (orig_cval), cval); | |
212 | return cval; | |
17f39a39 | 213 | } |
846abd0d RB |
214 | if (TREE_OVERFLOW_P (cval)) |
215 | return drop_tree_overflow (cval); | |
50619002 | 216 | return orig_cval; |
17f39a39 | 217 | } |
cbdd87d4 RG |
218 | |
219 | /* If SYM is a constant variable with known value, return the value. | |
220 | NULL_TREE is returned otherwise. */ | |
221 | ||
222 | tree | |
223 | get_symbol_constant_value (tree sym) | |
224 | { | |
6a6dac52 JH |
225 | tree val = ctor_for_folding (sym); |
226 | if (val != error_mark_node) | |
cbdd87d4 | 227 | { |
cbdd87d4 RG |
228 | if (val) |
229 | { | |
9d60be38 | 230 | val = canonicalize_constructor_val (unshare_expr (val), sym); |
1389294c | 231 | if (val && is_gimple_min_invariant (val)) |
17f39a39 | 232 | return val; |
1389294c JH |
233 | else |
234 | return NULL_TREE; | |
cbdd87d4 RG |
235 | } |
236 | /* Variables declared 'const' without an initializer | |
237 | have zero as the initializer if they may not be | |
238 | overridden at link or run time. */ | |
239 | if (!val | |
cbdd87d4 RG |
240 | && (INTEGRAL_TYPE_P (TREE_TYPE (sym)) |
241 | || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym)))) | |
e8160c9a | 242 | return build_zero_cst (TREE_TYPE (sym)); |
cbdd87d4 RG |
243 | } |
244 | ||
245 | return NULL_TREE; | |
246 | } | |
247 | ||
248 | ||
cbdd87d4 RG |
249 | |
250 | /* Subroutine of fold_stmt. We perform several simplifications of the | |
251 | memory reference tree EXPR and make sure to re-gimplify them properly | |
252 | after propagation of constant addresses. IS_LHS is true if the | |
253 | reference is supposed to be an lvalue. */ | |
254 | ||
255 | static tree | |
256 | maybe_fold_reference (tree expr, bool is_lhs) | |
257 | { | |
258 | tree *t = &expr; | |
17f39a39 | 259 | tree result; |
cbdd87d4 | 260 | |
f0eddb90 RG |
261 | if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR |
262 | || TREE_CODE (expr) == REALPART_EXPR | |
263 | || TREE_CODE (expr) == IMAGPART_EXPR) | |
264 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
265 | return fold_unary_loc (EXPR_LOCATION (expr), | |
266 | TREE_CODE (expr), | |
267 | TREE_TYPE (expr), | |
268 | TREE_OPERAND (expr, 0)); | |
269 | else if (TREE_CODE (expr) == BIT_FIELD_REF | |
270 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
271 | return fold_ternary_loc (EXPR_LOCATION (expr), | |
272 | TREE_CODE (expr), | |
273 | TREE_TYPE (expr), | |
274 | TREE_OPERAND (expr, 0), | |
275 | TREE_OPERAND (expr, 1), | |
276 | TREE_OPERAND (expr, 2)); | |
277 | ||
278 | while (handled_component_p (*t)) | |
279 | t = &TREE_OPERAND (*t, 0); | |
cbdd87d4 | 280 | |
f0eddb90 RG |
281 | /* Canonicalize MEM_REFs invariant address operand. Do this first |
282 | to avoid feeding non-canonical MEM_REFs elsewhere. */ | |
283 | if (TREE_CODE (*t) == MEM_REF | |
284 | && !is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0))) | |
cbdd87d4 | 285 | { |
f0eddb90 RG |
286 | bool volatile_p = TREE_THIS_VOLATILE (*t); |
287 | tree tem = fold_binary (MEM_REF, TREE_TYPE (*t), | |
288 | TREE_OPERAND (*t, 0), | |
289 | TREE_OPERAND (*t, 1)); | |
290 | if (tem) | |
291 | { | |
292 | TREE_THIS_VOLATILE (tem) = volatile_p; | |
293 | *t = tem; | |
294 | tem = maybe_fold_reference (expr, is_lhs); | |
295 | if (tem) | |
296 | return tem; | |
297 | return expr; | |
298 | } | |
cbdd87d4 RG |
299 | } |
300 | ||
f0eddb90 RG |
301 | if (!is_lhs |
302 | && (result = fold_const_aggregate_ref (expr)) | |
303 | && is_gimple_min_invariant (result)) | |
304 | return result; | |
cbdd87d4 | 305 | |
70f34814 RG |
306 | /* Fold back MEM_REFs to reference trees. */ |
307 | if (TREE_CODE (*t) == MEM_REF | |
308 | && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR | |
309 | && integer_zerop (TREE_OPERAND (*t, 1)) | |
310 | && (TREE_THIS_VOLATILE (*t) | |
311 | == TREE_THIS_VOLATILE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))) | |
312 | && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*t, 1))) | |
313 | && (TYPE_MAIN_VARIANT (TREE_TYPE (*t)) | |
314 | == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (*t, 1))))) | |
315 | /* We have to look out here to not drop a required conversion | |
316 | from the rhs to the lhs if is_lhs, but we don't have the | |
317 | rhs here to verify that. Thus require strict type | |
318 | compatibility. */ | |
319 | && types_compatible_p (TREE_TYPE (*t), | |
320 | TREE_TYPE (TREE_OPERAND | |
f0eddb90 | 321 | (TREE_OPERAND (*t, 0), 0)))) |
cbdd87d4 | 322 | { |
70f34814 RG |
323 | tree tem; |
324 | *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
325 | tem = maybe_fold_reference (expr, is_lhs); | |
326 | if (tem) | |
327 | return tem; | |
328 | return expr; | |
329 | } | |
4d948885 RG |
330 | else if (TREE_CODE (*t) == TARGET_MEM_REF) |
331 | { | |
332 | tree tem = maybe_fold_tmr (*t); | |
333 | if (tem) | |
334 | { | |
335 | *t = tem; | |
336 | tem = maybe_fold_reference (expr, is_lhs); | |
337 | if (tem) | |
338 | return tem; | |
339 | return expr; | |
340 | } | |
341 | } | |
cbdd87d4 RG |
342 | |
343 | return NULL_TREE; | |
344 | } | |
345 | ||
346 | ||
347 | /* Attempt to fold an assignment statement pointed-to by SI. Returns a | |
348 | replacement rhs for the statement or NULL_TREE if no simplification | |
349 | could be made. It is assumed that the operands have been previously | |
350 | folded. */ | |
351 | ||
352 | static tree | |
353 | fold_gimple_assign (gimple_stmt_iterator *si) | |
354 | { | |
355 | gimple stmt = gsi_stmt (*si); | |
356 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
357 | location_t loc = gimple_location (stmt); | |
358 | ||
359 | tree result = NULL_TREE; | |
360 | ||
361 | switch (get_gimple_rhs_class (subcode)) | |
362 | { | |
363 | case GIMPLE_SINGLE_RHS: | |
364 | { | |
365 | tree rhs = gimple_assign_rhs1 (stmt); | |
366 | ||
4e71066d | 367 | if (REFERENCE_CLASS_P (rhs)) |
cbdd87d4 RG |
368 | return maybe_fold_reference (rhs, false); |
369 | ||
bdf37f7a JH |
370 | else if (TREE_CODE (rhs) == OBJ_TYPE_REF) |
371 | { | |
372 | tree val = OBJ_TYPE_REF_EXPR (rhs); | |
373 | if (is_gimple_min_invariant (val)) | |
374 | return val; | |
375 | else if (flag_devirtualize && virtual_method_call_p (val)) | |
376 | { | |
377 | bool final; | |
378 | vec <cgraph_node *>targets | |
379 | = possible_polymorphic_call_targets (val, &final); | |
2b5f0895 | 380 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
bdf37f7a JH |
381 | { |
382 | tree fndecl; | |
2b5f0895 | 383 | |
bdf37f7a JH |
384 | if (targets.length () == 1) |
385 | fndecl = targets[0]->decl; | |
386 | else | |
387 | fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
2b5f0895 XDL |
388 | if (dump_enabled_p ()) |
389 | { | |
390 | location_t loc = gimple_location (stmt); | |
391 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, | |
392 | "resolving virtual function address " | |
393 | "reference to function %s\n", | |
394 | targets.length () == 1 | |
395 | ? targets[0]->name () | |
396 | : "__builtin_unreachable"); | |
397 | } | |
bdf37f7a JH |
398 | val = fold_convert (TREE_TYPE (val), fndecl); |
399 | STRIP_USELESS_TYPE_CONVERSION (val); | |
400 | return val; | |
401 | } | |
402 | } | |
403 | ||
404 | } | |
cbdd87d4 RG |
405 | else if (TREE_CODE (rhs) == ADDR_EXPR) |
406 | { | |
70f34814 RG |
407 | tree ref = TREE_OPERAND (rhs, 0); |
408 | tree tem = maybe_fold_reference (ref, true); | |
409 | if (tem | |
410 | && TREE_CODE (tem) == MEM_REF | |
411 | && integer_zerop (TREE_OPERAND (tem, 1))) | |
412 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0)); | |
413 | else if (tem) | |
cbdd87d4 RG |
414 | result = fold_convert (TREE_TYPE (rhs), |
415 | build_fold_addr_expr_loc (loc, tem)); | |
70f34814 RG |
416 | else if (TREE_CODE (ref) == MEM_REF |
417 | && integer_zerop (TREE_OPERAND (ref, 1))) | |
418 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0)); | |
cbdd87d4 RG |
419 | } |
420 | ||
421 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
422 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
423 | && (CONSTRUCTOR_NELTS (rhs) | |
424 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
425 | { | |
426 | /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */ | |
427 | unsigned i; | |
428 | tree val; | |
429 | ||
430 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) | |
431 | if (TREE_CODE (val) != INTEGER_CST | |
432 | && TREE_CODE (val) != REAL_CST | |
433 | && TREE_CODE (val) != FIXED_CST) | |
434 | return NULL_TREE; | |
435 | ||
436 | return build_vector_from_ctor (TREE_TYPE (rhs), | |
437 | CONSTRUCTOR_ELTS (rhs)); | |
438 | } | |
439 | ||
440 | else if (DECL_P (rhs)) | |
9d60be38 | 441 | return get_symbol_constant_value (rhs); |
cbdd87d4 RG |
442 | |
443 | /* If we couldn't fold the RHS, hand over to the generic | |
444 | fold routines. */ | |
445 | if (result == NULL_TREE) | |
446 | result = fold (rhs); | |
447 | ||
448 | /* Strip away useless type conversions. Both the NON_LVALUE_EXPR | |
449 | that may have been added by fold, and "useless" type | |
450 | conversions that might now be apparent due to propagation. */ | |
451 | STRIP_USELESS_TYPE_CONVERSION (result); | |
452 | ||
453 | if (result != rhs && valid_gimple_rhs_p (result)) | |
454 | return result; | |
455 | ||
456 | return NULL_TREE; | |
457 | } | |
458 | break; | |
459 | ||
460 | case GIMPLE_UNARY_RHS: | |
461 | { | |
462 | tree rhs = gimple_assign_rhs1 (stmt); | |
463 | ||
464 | result = fold_unary_loc (loc, subcode, gimple_expr_type (stmt), rhs); | |
465 | if (result) | |
466 | { | |
467 | /* If the operation was a conversion do _not_ mark a | |
468 | resulting constant with TREE_OVERFLOW if the original | |
469 | constant was not. These conversions have implementation | |
470 | defined behavior and retaining the TREE_OVERFLOW flag | |
471 | here would confuse later passes such as VRP. */ | |
472 | if (CONVERT_EXPR_CODE_P (subcode) | |
473 | && TREE_CODE (result) == INTEGER_CST | |
474 | && TREE_CODE (rhs) == INTEGER_CST) | |
475 | TREE_OVERFLOW (result) = TREE_OVERFLOW (rhs); | |
476 | ||
477 | STRIP_USELESS_TYPE_CONVERSION (result); | |
478 | if (valid_gimple_rhs_p (result)) | |
479 | return result; | |
480 | } | |
cbdd87d4 RG |
481 | } |
482 | break; | |
483 | ||
484 | case GIMPLE_BINARY_RHS: | |
9b80d091 KT |
485 | /* Try to canonicalize for boolean-typed X the comparisons |
486 | X == 0, X == 1, X != 0, and X != 1. */ | |
315f5f1b RG |
487 | if (gimple_assign_rhs_code (stmt) == EQ_EXPR |
488 | || gimple_assign_rhs_code (stmt) == NE_EXPR) | |
9b80d091 KT |
489 | { |
490 | tree lhs = gimple_assign_lhs (stmt); | |
491 | tree op1 = gimple_assign_rhs1 (stmt); | |
492 | tree op2 = gimple_assign_rhs2 (stmt); | |
493 | tree type = TREE_TYPE (op1); | |
494 | ||
495 | /* Check whether the comparison operands are of the same boolean | |
496 | type as the result type is. | |
497 | Check that second operand is an integer-constant with value | |
498 | one or zero. */ | |
499 | if (TREE_CODE (op2) == INTEGER_CST | |
500 | && (integer_zerop (op2) || integer_onep (op2)) | |
501 | && useless_type_conversion_p (TREE_TYPE (lhs), type)) | |
502 | { | |
503 | enum tree_code cmp_code = gimple_assign_rhs_code (stmt); | |
504 | bool is_logical_not = false; | |
505 | ||
506 | /* X == 0 and X != 1 is a logical-not.of X | |
507 | X == 1 and X != 0 is X */ | |
508 | if ((cmp_code == EQ_EXPR && integer_zerop (op2)) | |
509 | || (cmp_code == NE_EXPR && integer_onep (op2))) | |
510 | is_logical_not = true; | |
511 | ||
512 | if (is_logical_not == false) | |
513 | result = op1; | |
514 | /* Only for one-bit precision typed X the transformation | |
515 | !X -> ~X is valied. */ | |
516 | else if (TYPE_PRECISION (type) == 1) | |
517 | result = build1_loc (gimple_location (stmt), BIT_NOT_EXPR, | |
518 | type, op1); | |
519 | /* Otherwise we use !X -> X ^ 1. */ | |
520 | else | |
521 | result = build2_loc (gimple_location (stmt), BIT_XOR_EXPR, | |
522 | type, op1, build_int_cst (type, 1)); | |
523 | ||
524 | } | |
525 | } | |
cbdd87d4 RG |
526 | |
527 | if (!result) | |
528 | result = fold_binary_loc (loc, subcode, | |
5fbcc0ed RG |
529 | TREE_TYPE (gimple_assign_lhs (stmt)), |
530 | gimple_assign_rhs1 (stmt), | |
531 | gimple_assign_rhs2 (stmt)); | |
cbdd87d4 RG |
532 | |
533 | if (result) | |
534 | { | |
535 | STRIP_USELESS_TYPE_CONVERSION (result); | |
536 | if (valid_gimple_rhs_p (result)) | |
537 | return result; | |
cbdd87d4 RG |
538 | } |
539 | break; | |
540 | ||
0354c0c7 | 541 | case GIMPLE_TERNARY_RHS: |
4e71066d RG |
542 | /* Try to fold a conditional expression. */ |
543 | if (gimple_assign_rhs_code (stmt) == COND_EXPR) | |
544 | { | |
545 | tree op0 = gimple_assign_rhs1 (stmt); | |
546 | tree tem; | |
547 | bool set = false; | |
548 | location_t cond_loc = gimple_location (stmt); | |
549 | ||
550 | if (COMPARISON_CLASS_P (op0)) | |
551 | { | |
552 | fold_defer_overflow_warnings (); | |
553 | tem = fold_binary_loc (cond_loc, | |
554 | TREE_CODE (op0), TREE_TYPE (op0), | |
555 | TREE_OPERAND (op0, 0), | |
556 | TREE_OPERAND (op0, 1)); | |
557 | /* This is actually a conditional expression, not a GIMPLE | |
558 | conditional statement, however, the valid_gimple_rhs_p | |
559 | test still applies. */ | |
560 | set = (tem && is_gimple_condexpr (tem) | |
561 | && valid_gimple_rhs_p (tem)); | |
562 | fold_undefer_overflow_warnings (set, stmt, 0); | |
563 | } | |
564 | else if (is_gimple_min_invariant (op0)) | |
565 | { | |
566 | tem = op0; | |
567 | set = true; | |
568 | } | |
569 | else | |
570 | return NULL_TREE; | |
571 | ||
572 | if (set) | |
573 | result = fold_build3_loc (cond_loc, COND_EXPR, | |
574 | TREE_TYPE (gimple_assign_lhs (stmt)), tem, | |
575 | gimple_assign_rhs2 (stmt), | |
576 | gimple_assign_rhs3 (stmt)); | |
577 | } | |
578 | ||
579 | if (!result) | |
580 | result = fold_ternary_loc (loc, subcode, | |
581 | TREE_TYPE (gimple_assign_lhs (stmt)), | |
582 | gimple_assign_rhs1 (stmt), | |
583 | gimple_assign_rhs2 (stmt), | |
584 | gimple_assign_rhs3 (stmt)); | |
0354c0c7 BS |
585 | |
586 | if (result) | |
587 | { | |
588 | STRIP_USELESS_TYPE_CONVERSION (result); | |
589 | if (valid_gimple_rhs_p (result)) | |
590 | return result; | |
0354c0c7 BS |
591 | } |
592 | break; | |
593 | ||
cbdd87d4 RG |
594 | case GIMPLE_INVALID_RHS: |
595 | gcc_unreachable (); | |
596 | } | |
597 | ||
598 | return NULL_TREE; | |
599 | } | |
600 | ||
601 | /* Attempt to fold a conditional statement. Return true if any changes were | |
602 | made. We only attempt to fold the condition expression, and do not perform | |
603 | any transformation that would require alteration of the cfg. It is | |
604 | assumed that the operands have been previously folded. */ | |
605 | ||
606 | static bool | |
607 | fold_gimple_cond (gimple stmt) | |
608 | { | |
609 | tree result = fold_binary_loc (gimple_location (stmt), | |
610 | gimple_cond_code (stmt), | |
611 | boolean_type_node, | |
612 | gimple_cond_lhs (stmt), | |
613 | gimple_cond_rhs (stmt)); | |
614 | ||
615 | if (result) | |
616 | { | |
617 | STRIP_USELESS_TYPE_CONVERSION (result); | |
618 | if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result)) | |
619 | { | |
620 | gimple_cond_set_condition_from_tree (stmt, result); | |
621 | return true; | |
622 | } | |
623 | } | |
624 | ||
625 | return false; | |
626 | } | |
627 | ||
628 | /* Convert EXPR into a GIMPLE value suitable for substitution on the | |
629 | RHS of an assignment. Insert the necessary statements before | |
630 | iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL | |
631 | is replaced. If the call is expected to produces a result, then it | |
632 | is replaced by an assignment of the new RHS to the result variable. | |
633 | If the result is to be ignored, then the call is replaced by a | |
fe2ef088 MM |
634 | GIMPLE_NOP. A proper VDEF chain is retained by making the first |
635 | VUSE and the last VDEF of the whole sequence be the same as the replaced | |
636 | statement and using new SSA names for stores in between. */ | |
cbdd87d4 RG |
637 | |
638 | void | |
639 | gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr) | |
640 | { | |
641 | tree lhs; | |
cbdd87d4 RG |
642 | gimple stmt, new_stmt; |
643 | gimple_stmt_iterator i; | |
355a7673 | 644 | gimple_seq stmts = NULL; |
e256dfce | 645 | gimple laststore; |
fe2ef088 | 646 | tree reaching_vuse; |
cbdd87d4 RG |
647 | |
648 | stmt = gsi_stmt (*si_p); | |
649 | ||
650 | gcc_assert (is_gimple_call (stmt)); | |
651 | ||
45852dcc | 652 | push_gimplify_context (gimple_in_ssa_p (cfun)); |
cbdd87d4 | 653 | |
e256dfce | 654 | lhs = gimple_call_lhs (stmt); |
cbdd87d4 | 655 | if (lhs == NULL_TREE) |
6e572326 RG |
656 | { |
657 | gimplify_and_add (expr, &stmts); | |
658 | /* We can end up with folding a memcpy of an empty class assignment | |
659 | which gets optimized away by C++ gimplification. */ | |
660 | if (gimple_seq_empty_p (stmts)) | |
661 | { | |
9fdc58de | 662 | pop_gimplify_context (NULL); |
6e572326 RG |
663 | if (gimple_in_ssa_p (cfun)) |
664 | { | |
665 | unlink_stmt_vdef (stmt); | |
666 | release_defs (stmt); | |
667 | } | |
77d19c72 | 668 | gsi_replace (si_p, gimple_build_nop (), true); |
6e572326 RG |
669 | return; |
670 | } | |
671 | } | |
cbdd87d4 | 672 | else |
e256dfce RG |
673 | { |
674 | tree tmp = get_initialized_tmp_var (expr, &stmts, NULL); | |
675 | new_stmt = gimple_build_assign (lhs, tmp); | |
676 | i = gsi_last (stmts); | |
677 | gsi_insert_after_without_update (&i, new_stmt, | |
678 | GSI_CONTINUE_LINKING); | |
679 | } | |
cbdd87d4 RG |
680 | |
681 | pop_gimplify_context (NULL); | |
682 | ||
683 | if (gimple_has_location (stmt)) | |
684 | annotate_all_with_location (stmts, gimple_location (stmt)); | |
685 | ||
e256dfce RG |
686 | /* First iterate over the replacement statements backward, assigning |
687 | virtual operands to their defining statements. */ | |
688 | laststore = NULL; | |
689 | for (i = gsi_last (stmts); !gsi_end_p (i); gsi_prev (&i)) | |
690 | { | |
691 | new_stmt = gsi_stmt (i); | |
949e47e5 JJ |
692 | if ((gimple_assign_single_p (new_stmt) |
693 | && !is_gimple_reg (gimple_assign_lhs (new_stmt))) | |
694 | || (is_gimple_call (new_stmt) | |
695 | && (gimple_call_flags (new_stmt) | |
696 | & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0)) | |
e256dfce RG |
697 | { |
698 | tree vdef; | |
699 | if (!laststore) | |
700 | vdef = gimple_vdef (stmt); | |
701 | else | |
702 | vdef = make_ssa_name (gimple_vop (cfun), new_stmt); | |
703 | gimple_set_vdef (new_stmt, vdef); | |
52f26be4 | 704 | if (vdef && TREE_CODE (vdef) == SSA_NAME) |
e256dfce RG |
705 | SSA_NAME_DEF_STMT (vdef) = new_stmt; |
706 | laststore = new_stmt; | |
707 | } | |
708 | } | |
709 | ||
710 | /* Second iterate over the statements forward, assigning virtual | |
711 | operands to their uses. */ | |
e256dfce | 712 | reaching_vuse = gimple_vuse (stmt); |
cbdd87d4 RG |
713 | for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i)) |
714 | { | |
cbdd87d4 | 715 | new_stmt = gsi_stmt (i); |
a24ac460 RG |
716 | /* If the new statement possibly has a VUSE, update it with exact SSA |
717 | name we know will reach this one. */ | |
718 | if (gimple_has_mem_ops (new_stmt)) | |
e256dfce RG |
719 | gimple_set_vuse (new_stmt, reaching_vuse); |
720 | gimple_set_modified (new_stmt, true); | |
721 | if (gimple_vdef (new_stmt)) | |
722 | reaching_vuse = gimple_vdef (new_stmt); | |
cbdd87d4 RG |
723 | } |
724 | ||
e256dfce RG |
725 | /* If the new sequence does not do a store release the virtual |
726 | definition of the original statement. */ | |
727 | if (reaching_vuse | |
728 | && reaching_vuse == gimple_vuse (stmt)) | |
cbdd87d4 | 729 | { |
e256dfce RG |
730 | tree vdef = gimple_vdef (stmt); |
731 | if (vdef | |
732 | && TREE_CODE (vdef) == SSA_NAME) | |
fe2ef088 MM |
733 | { |
734 | unlink_stmt_vdef (stmt); | |
e256dfce | 735 | release_ssa_name (vdef); |
8a1561bc | 736 | } |
cbdd87d4 RG |
737 | } |
738 | ||
355a7673 MM |
739 | /* Finally replace the original statement with the sequence. */ |
740 | gsi_replace_with_seq (si_p, stmts, false); | |
cbdd87d4 RG |
741 | } |
742 | ||
743 | /* Return the string length, maximum string length or maximum value of | |
744 | ARG in LENGTH. | |
745 | If ARG is an SSA name variable, follow its use-def chains. If LENGTH | |
746 | is not NULL and, for TYPE == 0, its value is not equal to the length | |
747 | we determine or if we are unable to determine the length or value, | |
748 | return false. VISITED is a bitmap of visited variables. | |
749 | TYPE is 0 if string length should be returned, 1 for maximum string | |
750 | length and 2 for maximum value ARG can have. */ | |
751 | ||
752 | static bool | |
753 | get_maxval_strlen (tree arg, tree *length, bitmap visited, int type) | |
754 | { | |
755 | tree var, val; | |
756 | gimple def_stmt; | |
757 | ||
758 | if (TREE_CODE (arg) != SSA_NAME) | |
759 | { | |
cbdd87d4 | 760 | /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */ |
763dc99d AP |
761 | if (TREE_CODE (arg) == ADDR_EXPR |
762 | && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF | |
763 | && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1))) | |
cbdd87d4 RG |
764 | { |
765 | tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
766 | if (TREE_CODE (aop0) == INDIRECT_REF | |
767 | && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME) | |
768 | return get_maxval_strlen (TREE_OPERAND (aop0, 0), | |
769 | length, visited, type); | |
770 | } | |
771 | ||
772 | if (type == 2) | |
773 | { | |
774 | val = arg; | |
775 | if (TREE_CODE (val) != INTEGER_CST | |
776 | || tree_int_cst_sgn (val) < 0) | |
777 | return false; | |
778 | } | |
779 | else | |
780 | val = c_strlen (arg, 1); | |
781 | if (!val) | |
782 | return false; | |
783 | ||
784 | if (*length) | |
785 | { | |
786 | if (type > 0) | |
787 | { | |
788 | if (TREE_CODE (*length) != INTEGER_CST | |
789 | || TREE_CODE (val) != INTEGER_CST) | |
790 | return false; | |
791 | ||
792 | if (tree_int_cst_lt (*length, val)) | |
793 | *length = val; | |
794 | return true; | |
795 | } | |
796 | else if (simple_cst_equal (val, *length) != 1) | |
797 | return false; | |
798 | } | |
799 | ||
800 | *length = val; | |
801 | return true; | |
802 | } | |
803 | ||
491e0b9b RG |
804 | /* If ARG is registered for SSA update we cannot look at its defining |
805 | statement. */ | |
806 | if (name_registered_for_update_p (arg)) | |
807 | return false; | |
808 | ||
cbdd87d4 | 809 | /* If we were already here, break the infinite cycle. */ |
fcaa4ca4 | 810 | if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg))) |
cbdd87d4 | 811 | return true; |
cbdd87d4 RG |
812 | |
813 | var = arg; | |
814 | def_stmt = SSA_NAME_DEF_STMT (var); | |
815 | ||
816 | switch (gimple_code (def_stmt)) | |
817 | { | |
818 | case GIMPLE_ASSIGN: | |
819 | /* The RHS of the statement defining VAR must either have a | |
820 | constant length or come from another SSA_NAME with a constant | |
821 | length. */ | |
822 | if (gimple_assign_single_p (def_stmt) | |
823 | || gimple_assign_unary_nop_p (def_stmt)) | |
824 | { | |
825 | tree rhs = gimple_assign_rhs1 (def_stmt); | |
826 | return get_maxval_strlen (rhs, length, visited, type); | |
827 | } | |
763dc99d AP |
828 | else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR) |
829 | { | |
830 | tree op2 = gimple_assign_rhs2 (def_stmt); | |
831 | tree op3 = gimple_assign_rhs3 (def_stmt); | |
832 | return get_maxval_strlen (op2, length, visited, type) | |
833 | && get_maxval_strlen (op3, length, visited, type); | |
834 | } | |
cbdd87d4 RG |
835 | return false; |
836 | ||
837 | case GIMPLE_PHI: | |
838 | { | |
839 | /* All the arguments of the PHI node must have the same constant | |
840 | length. */ | |
841 | unsigned i; | |
842 | ||
843 | for (i = 0; i < gimple_phi_num_args (def_stmt); i++) | |
844 | { | |
845 | tree arg = gimple_phi_arg (def_stmt, i)->def; | |
846 | ||
847 | /* If this PHI has itself as an argument, we cannot | |
848 | determine the string length of this argument. However, | |
849 | if we can find a constant string length for the other | |
850 | PHI args then we can still be sure that this is a | |
851 | constant string length. So be optimistic and just | |
852 | continue with the next argument. */ | |
853 | if (arg == gimple_phi_result (def_stmt)) | |
854 | continue; | |
855 | ||
856 | if (!get_maxval_strlen (arg, length, visited, type)) | |
857 | return false; | |
858 | } | |
859 | } | |
860 | return true; | |
861 | ||
862 | default: | |
863 | return false; | |
864 | } | |
865 | } | |
866 | ||
867 | ||
868 | /* Fold builtin call in statement STMT. Returns a simplified tree. | |
869 | We may return a non-constant expression, including another call | |
870 | to a different function and with different arguments, e.g., | |
871 | substituting memcpy for strcpy when the string length is known. | |
872 | Note that some builtins expand into inline code that may not | |
873 | be valid in GIMPLE. Callers must take care. */ | |
874 | ||
875 | tree | |
876 | gimple_fold_builtin (gimple stmt) | |
877 | { | |
878 | tree result, val[3]; | |
879 | tree callee, a; | |
880 | int arg_idx, type; | |
881 | bitmap visited; | |
882 | bool ignore; | |
883 | int nargs; | |
884 | location_t loc = gimple_location (stmt); | |
885 | ||
cbdd87d4 RG |
886 | ignore = (gimple_call_lhs (stmt) == NULL); |
887 | ||
888 | /* First try the generic builtin folder. If that succeeds, return the | |
889 | result directly. */ | |
890 | result = fold_call_stmt (stmt, ignore); | |
891 | if (result) | |
892 | { | |
893 | if (ignore) | |
894 | STRIP_NOPS (result); | |
5c944c6c RB |
895 | else |
896 | result = fold_convert (gimple_call_return_type (stmt), result); | |
cbdd87d4 RG |
897 | return result; |
898 | } | |
899 | ||
900 | /* Ignore MD builtins. */ | |
901 | callee = gimple_call_fndecl (stmt); | |
902 | if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD) | |
903 | return NULL_TREE; | |
904 | ||
e7f9dae0 JJ |
905 | /* Give up for always_inline inline builtins until they are |
906 | inlined. */ | |
907 | if (avoid_folding_inline_builtin (callee)) | |
908 | return NULL_TREE; | |
909 | ||
cbdd87d4 RG |
910 | /* If the builtin could not be folded, and it has no argument list, |
911 | we're done. */ | |
912 | nargs = gimple_call_num_args (stmt); | |
913 | if (nargs == 0) | |
914 | return NULL_TREE; | |
915 | ||
916 | /* Limit the work only for builtins we know how to simplify. */ | |
917 | switch (DECL_FUNCTION_CODE (callee)) | |
918 | { | |
919 | case BUILT_IN_STRLEN: | |
920 | case BUILT_IN_FPUTS: | |
921 | case BUILT_IN_FPUTS_UNLOCKED: | |
922 | arg_idx = 0; | |
923 | type = 0; | |
924 | break; | |
925 | case BUILT_IN_STRCPY: | |
926 | case BUILT_IN_STRNCPY: | |
9a7eefec | 927 | case BUILT_IN_STRCAT: |
cbdd87d4 RG |
928 | arg_idx = 1; |
929 | type = 0; | |
930 | break; | |
931 | case BUILT_IN_MEMCPY_CHK: | |
932 | case BUILT_IN_MEMPCPY_CHK: | |
933 | case BUILT_IN_MEMMOVE_CHK: | |
934 | case BUILT_IN_MEMSET_CHK: | |
935 | case BUILT_IN_STRNCPY_CHK: | |
f3fc9b80 | 936 | case BUILT_IN_STPNCPY_CHK: |
cbdd87d4 RG |
937 | arg_idx = 2; |
938 | type = 2; | |
939 | break; | |
940 | case BUILT_IN_STRCPY_CHK: | |
941 | case BUILT_IN_STPCPY_CHK: | |
942 | arg_idx = 1; | |
943 | type = 1; | |
944 | break; | |
945 | case BUILT_IN_SNPRINTF_CHK: | |
946 | case BUILT_IN_VSNPRINTF_CHK: | |
947 | arg_idx = 1; | |
948 | type = 2; | |
949 | break; | |
950 | default: | |
951 | return NULL_TREE; | |
952 | } | |
953 | ||
954 | if (arg_idx >= nargs) | |
955 | return NULL_TREE; | |
956 | ||
957 | /* Try to use the dataflow information gathered by the CCP process. */ | |
958 | visited = BITMAP_ALLOC (NULL); | |
959 | bitmap_clear (visited); | |
960 | ||
961 | memset (val, 0, sizeof (val)); | |
962 | a = gimple_call_arg (stmt, arg_idx); | |
963 | if (!get_maxval_strlen (a, &val[arg_idx], visited, type)) | |
964 | val[arg_idx] = NULL_TREE; | |
965 | ||
966 | BITMAP_FREE (visited); | |
967 | ||
968 | result = NULL_TREE; | |
969 | switch (DECL_FUNCTION_CODE (callee)) | |
970 | { | |
971 | case BUILT_IN_STRLEN: | |
972 | if (val[0] && nargs == 1) | |
973 | { | |
974 | tree new_val = | |
975 | fold_convert (TREE_TYPE (gimple_call_lhs (stmt)), val[0]); | |
976 | ||
977 | /* If the result is not a valid gimple value, or not a cast | |
6e4da084 | 978 | of a valid gimple value, then we cannot use the result. */ |
cbdd87d4 | 979 | if (is_gimple_val (new_val) |
3dbe9454 | 980 | || (CONVERT_EXPR_P (new_val) |
cbdd87d4 RG |
981 | && is_gimple_val (TREE_OPERAND (new_val, 0)))) |
982 | return new_val; | |
983 | } | |
984 | break; | |
985 | ||
986 | case BUILT_IN_STRCPY: | |
987 | if (val[1] && is_gimple_val (val[1]) && nargs == 2) | |
988 | result = fold_builtin_strcpy (loc, callee, | |
989 | gimple_call_arg (stmt, 0), | |
990 | gimple_call_arg (stmt, 1), | |
991 | val[1]); | |
992 | break; | |
993 | ||
994 | case BUILT_IN_STRNCPY: | |
995 | if (val[1] && is_gimple_val (val[1]) && nargs == 3) | |
996 | result = fold_builtin_strncpy (loc, callee, | |
997 | gimple_call_arg (stmt, 0), | |
998 | gimple_call_arg (stmt, 1), | |
999 | gimple_call_arg (stmt, 2), | |
1000 | val[1]); | |
1001 | break; | |
1002 | ||
9a7eefec JJ |
1003 | case BUILT_IN_STRCAT: |
1004 | if (val[1] && is_gimple_val (val[1]) && nargs == 2) | |
1005 | result = fold_builtin_strcat (loc, gimple_call_arg (stmt, 0), | |
1006 | gimple_call_arg (stmt, 1), | |
1007 | val[1]); | |
1008 | break; | |
1009 | ||
cbdd87d4 RG |
1010 | case BUILT_IN_FPUTS: |
1011 | if (nargs == 2) | |
1012 | result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0), | |
1013 | gimple_call_arg (stmt, 1), | |
1014 | ignore, false, val[0]); | |
1015 | break; | |
1016 | ||
1017 | case BUILT_IN_FPUTS_UNLOCKED: | |
1018 | if (nargs == 2) | |
1019 | result = fold_builtin_fputs (loc, gimple_call_arg (stmt, 0), | |
1020 | gimple_call_arg (stmt, 1), | |
1021 | ignore, true, val[0]); | |
1022 | break; | |
1023 | ||
1024 | case BUILT_IN_MEMCPY_CHK: | |
1025 | case BUILT_IN_MEMPCPY_CHK: | |
1026 | case BUILT_IN_MEMMOVE_CHK: | |
1027 | case BUILT_IN_MEMSET_CHK: | |
1028 | if (val[2] && is_gimple_val (val[2]) && nargs == 4) | |
1029 | result = fold_builtin_memory_chk (loc, callee, | |
1030 | gimple_call_arg (stmt, 0), | |
1031 | gimple_call_arg (stmt, 1), | |
1032 | gimple_call_arg (stmt, 2), | |
1033 | gimple_call_arg (stmt, 3), | |
1034 | val[2], ignore, | |
1035 | DECL_FUNCTION_CODE (callee)); | |
1036 | break; | |
1037 | ||
1038 | case BUILT_IN_STRCPY_CHK: | |
1039 | case BUILT_IN_STPCPY_CHK: | |
1040 | if (val[1] && is_gimple_val (val[1]) && nargs == 3) | |
1041 | result = fold_builtin_stxcpy_chk (loc, callee, | |
1042 | gimple_call_arg (stmt, 0), | |
1043 | gimple_call_arg (stmt, 1), | |
1044 | gimple_call_arg (stmt, 2), | |
1045 | val[1], ignore, | |
1046 | DECL_FUNCTION_CODE (callee)); | |
1047 | break; | |
1048 | ||
1049 | case BUILT_IN_STRNCPY_CHK: | |
f3fc9b80 | 1050 | case BUILT_IN_STPNCPY_CHK: |
cbdd87d4 | 1051 | if (val[2] && is_gimple_val (val[2]) && nargs == 4) |
f3fc9b80 | 1052 | result = fold_builtin_stxncpy_chk (loc, gimple_call_arg (stmt, 0), |
cbdd87d4 RG |
1053 | gimple_call_arg (stmt, 1), |
1054 | gimple_call_arg (stmt, 2), | |
1055 | gimple_call_arg (stmt, 3), | |
f3fc9b80 RG |
1056 | val[2], ignore, |
1057 | DECL_FUNCTION_CODE (callee)); | |
cbdd87d4 RG |
1058 | break; |
1059 | ||
1060 | case BUILT_IN_SNPRINTF_CHK: | |
1061 | case BUILT_IN_VSNPRINTF_CHK: | |
1062 | if (val[1] && is_gimple_val (val[1])) | |
1063 | result = gimple_fold_builtin_snprintf_chk (stmt, val[1], | |
1064 | DECL_FUNCTION_CODE (callee)); | |
1065 | break; | |
1066 | ||
1067 | default: | |
1068 | gcc_unreachable (); | |
1069 | } | |
1070 | ||
1071 | if (result && ignore) | |
1072 | result = fold_ignored_result (result); | |
1073 | return result; | |
1074 | } | |
1075 | ||
1ae6fe9b | 1076 | |
cbdd87d4 RG |
1077 | /* Attempt to fold a call statement referenced by the statement iterator GSI. |
1078 | The statement may be replaced by another statement, e.g., if the call | |
1079 | simplifies to a constant value. Return true if any changes were made. | |
1080 | It is assumed that the operands have been previously folded. */ | |
1081 | ||
e021c122 | 1082 | static bool |
ceeffab0 | 1083 | gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace) |
cbdd87d4 RG |
1084 | { |
1085 | gimple stmt = gsi_stmt (*gsi); | |
3b45a007 | 1086 | tree callee; |
e021c122 RG |
1087 | bool changed = false; |
1088 | unsigned i; | |
cbdd87d4 | 1089 | |
e021c122 RG |
1090 | /* Fold *& in call arguments. */ |
1091 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
1092 | if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i))) | |
1093 | { | |
1094 | tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false); | |
1095 | if (tmp) | |
1096 | { | |
1097 | gimple_call_set_arg (stmt, i, tmp); | |
1098 | changed = true; | |
1099 | } | |
1100 | } | |
3b45a007 RG |
1101 | |
1102 | /* Check for virtual calls that became direct calls. */ | |
1103 | callee = gimple_call_fn (stmt); | |
25583c4f | 1104 | if (callee && TREE_CODE (callee) == OBJ_TYPE_REF) |
3b45a007 | 1105 | { |
49c471e3 MJ |
1106 | if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE) |
1107 | { | |
450ad0cd JH |
1108 | if (dump_file && virtual_method_call_p (callee) |
1109 | && !possible_polymorphic_call_target_p | |
1110 | (callee, cgraph_get_node (gimple_call_addr_fndecl | |
1111 | (OBJ_TYPE_REF_EXPR (callee))))) | |
1112 | { | |
1113 | fprintf (dump_file, | |
a70e9985 | 1114 | "Type inheritance inconsistent devirtualization of "); |
450ad0cd JH |
1115 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
1116 | fprintf (dump_file, " to "); | |
1117 | print_generic_expr (dump_file, callee, TDF_SLIM); | |
1118 | fprintf (dump_file, "\n"); | |
1119 | } | |
1120 | ||
49c471e3 | 1121 | gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee)); |
e021c122 RG |
1122 | changed = true; |
1123 | } | |
a70e9985 | 1124 | else if (flag_devirtualize && !inplace && virtual_method_call_p (callee)) |
e021c122 | 1125 | { |
61dd6a2e JH |
1126 | bool final; |
1127 | vec <cgraph_node *>targets | |
1128 | = possible_polymorphic_call_targets (callee, &final); | |
2b5f0895 | 1129 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
e021c122 | 1130 | { |
a70e9985 | 1131 | tree lhs = gimple_call_lhs (stmt); |
2b5f0895 XDL |
1132 | if (dump_enabled_p ()) |
1133 | { | |
1134 | location_t loc = gimple_location (stmt); | |
1135 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, | |
1136 | "folding virtual function call to %s\n", | |
1137 | targets.length () == 1 | |
1138 | ? targets[0]->name () | |
1139 | : "__builtin_unreachable"); | |
1140 | } | |
61dd6a2e | 1141 | if (targets.length () == 1) |
cf3e5a89 JJ |
1142 | { |
1143 | gimple_call_set_fndecl (stmt, targets[0]->decl); | |
1144 | changed = true; | |
a70e9985 JJ |
1145 | /* If the call becomes noreturn, remove the lhs. */ |
1146 | if (lhs && (gimple_call_flags (stmt) & ECF_NORETURN)) | |
1147 | { | |
1148 | if (TREE_CODE (lhs) == SSA_NAME) | |
1149 | { | |
1150 | tree var = create_tmp_var (TREE_TYPE (lhs), NULL); | |
1151 | tree def = get_or_create_ssa_default_def (cfun, var); | |
1152 | gimple new_stmt = gimple_build_assign (lhs, def); | |
1153 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1154 | } | |
1155 | gimple_call_set_lhs (stmt, NULL_TREE); | |
1156 | } | |
cf3e5a89 | 1157 | } |
a70e9985 | 1158 | else |
cf3e5a89 JJ |
1159 | { |
1160 | tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
1161 | gimple new_stmt = gimple_build_call (fndecl, 0); | |
1162 | gimple_set_location (new_stmt, gimple_location (stmt)); | |
a70e9985 JJ |
1163 | if (lhs && TREE_CODE (lhs) == SSA_NAME) |
1164 | { | |
1165 | tree var = create_tmp_var (TREE_TYPE (lhs), NULL); | |
1166 | tree def = get_or_create_ssa_default_def (cfun, var); | |
eb14a79f ML |
1167 | |
1168 | /* To satisfy condition for | |
1169 | cgraph_update_edges_for_call_stmt_node, | |
1170 | we need to preserve GIMPLE_CALL statement | |
1171 | at position of GSI iterator. */ | |
a70e9985 | 1172 | update_call_from_tree (gsi, def); |
eb14a79f | 1173 | gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT); |
a70e9985 JJ |
1174 | } |
1175 | else | |
1176 | gsi_replace (gsi, new_stmt, true); | |
cf3e5a89 JJ |
1177 | return true; |
1178 | } | |
e021c122 | 1179 | } |
49c471e3 | 1180 | } |
e021c122 | 1181 | } |
49c471e3 | 1182 | |
e021c122 RG |
1183 | if (inplace) |
1184 | return changed; | |
1185 | ||
1186 | /* Check for builtins that CCP can handle using information not | |
1187 | available in the generic fold routines. */ | |
5c944c6c | 1188 | if (gimple_call_builtin_p (stmt)) |
e021c122 RG |
1189 | { |
1190 | tree result = gimple_fold_builtin (stmt); | |
f6dbed32 | 1191 | if (result) |
e021c122 RG |
1192 | { |
1193 | if (!update_call_from_tree (gsi, result)) | |
1194 | gimplify_and_update_call_from_tree (gsi, result); | |
1195 | changed = true; | |
1196 | } | |
5c944c6c | 1197 | else if (gimple_call_builtin_p (stmt, BUILT_IN_MD)) |
ea679d55 | 1198 | changed |= targetm.gimple_fold_builtin (gsi); |
3b45a007 | 1199 | } |
368b454d | 1200 | else if (gimple_call_internal_p (stmt)) |
ed9c79e1 | 1201 | { |
368b454d JJ |
1202 | enum tree_code subcode = ERROR_MARK; |
1203 | tree result = NULL_TREE; | |
1204 | switch (gimple_call_internal_fn (stmt)) | |
1205 | { | |
1206 | case IFN_BUILTIN_EXPECT: | |
1207 | result = fold_builtin_expect (gimple_location (stmt), | |
1208 | gimple_call_arg (stmt, 0), | |
1209 | gimple_call_arg (stmt, 1), | |
1210 | gimple_call_arg (stmt, 2)); | |
1211 | break; | |
1212 | case IFN_UBSAN_CHECK_ADD: | |
1213 | subcode = PLUS_EXPR; | |
1214 | break; | |
1215 | case IFN_UBSAN_CHECK_SUB: | |
1216 | subcode = MINUS_EXPR; | |
1217 | break; | |
1218 | case IFN_UBSAN_CHECK_MUL: | |
1219 | subcode = MULT_EXPR; | |
1220 | break; | |
1221 | default: | |
1222 | break; | |
1223 | } | |
1224 | if (subcode != ERROR_MARK) | |
1225 | { | |
1226 | tree arg0 = gimple_call_arg (stmt, 0); | |
1227 | tree arg1 = gimple_call_arg (stmt, 1); | |
1228 | /* x = y + 0; x = y - 0; x = y * 0; */ | |
1229 | if (integer_zerop (arg1)) | |
1230 | result = subcode == MULT_EXPR | |
1231 | ? build_zero_cst (TREE_TYPE (arg0)) | |
1232 | : arg0; | |
1233 | /* x = 0 + y; x = 0 * y; */ | |
1234 | else if (subcode != MINUS_EXPR && integer_zerop (arg0)) | |
1235 | result = subcode == MULT_EXPR | |
1236 | ? build_zero_cst (TREE_TYPE (arg0)) | |
1237 | : arg1; | |
1238 | /* x = y - y; */ | |
1239 | else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0)) | |
1240 | result = build_zero_cst (TREE_TYPE (arg0)); | |
1241 | /* x = y * 1; x = 1 * y; */ | |
1242 | else if (subcode == MULT_EXPR) | |
1243 | { | |
1244 | if (integer_onep (arg1)) | |
1245 | result = arg0; | |
1246 | else if (integer_onep (arg0)) | |
1247 | result = arg1; | |
1248 | } | |
1249 | } | |
ed9c79e1 JJ |
1250 | if (result) |
1251 | { | |
1252 | if (!update_call_from_tree (gsi, result)) | |
1253 | gimplify_and_update_call_from_tree (gsi, result); | |
1254 | changed = true; | |
1255 | } | |
1256 | } | |
3b45a007 | 1257 | |
e021c122 | 1258 | return changed; |
cbdd87d4 RG |
1259 | } |
1260 | ||
1261 | /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument | |
1262 | distinguishes both cases. */ | |
1263 | ||
1264 | static bool | |
1265 | fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace) | |
1266 | { | |
1267 | bool changed = false; | |
1268 | gimple stmt = gsi_stmt (*gsi); | |
1269 | unsigned i; | |
1270 | ||
1271 | /* Fold the main computation performed by the statement. */ | |
1272 | switch (gimple_code (stmt)) | |
1273 | { | |
1274 | case GIMPLE_ASSIGN: | |
1275 | { | |
1276 | unsigned old_num_ops = gimple_num_ops (stmt); | |
5fbcc0ed | 1277 | enum tree_code subcode = gimple_assign_rhs_code (stmt); |
cbdd87d4 | 1278 | tree lhs = gimple_assign_lhs (stmt); |
5fbcc0ed RG |
1279 | tree new_rhs; |
1280 | /* First canonicalize operand order. This avoids building new | |
1281 | trees if this is the only thing fold would later do. */ | |
1282 | if ((commutative_tree_code (subcode) | |
1283 | || commutative_ternary_tree_code (subcode)) | |
1284 | && tree_swap_operands_p (gimple_assign_rhs1 (stmt), | |
1285 | gimple_assign_rhs2 (stmt), false)) | |
1286 | { | |
1287 | tree tem = gimple_assign_rhs1 (stmt); | |
1288 | gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt)); | |
1289 | gimple_assign_set_rhs2 (stmt, tem); | |
1290 | changed = true; | |
1291 | } | |
1292 | new_rhs = fold_gimple_assign (gsi); | |
cbdd87d4 RG |
1293 | if (new_rhs |
1294 | && !useless_type_conversion_p (TREE_TYPE (lhs), | |
1295 | TREE_TYPE (new_rhs))) | |
1296 | new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); | |
1297 | if (new_rhs | |
1298 | && (!inplace | |
1299 | || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) | |
1300 | { | |
1301 | gimple_assign_set_rhs_from_tree (gsi, new_rhs); | |
1302 | changed = true; | |
1303 | } | |
1304 | break; | |
1305 | } | |
1306 | ||
1307 | case GIMPLE_COND: | |
1308 | changed |= fold_gimple_cond (stmt); | |
1309 | break; | |
1310 | ||
1311 | case GIMPLE_CALL: | |
ceeffab0 | 1312 | changed |= gimple_fold_call (gsi, inplace); |
cbdd87d4 RG |
1313 | break; |
1314 | ||
1315 | case GIMPLE_ASM: | |
1316 | /* Fold *& in asm operands. */ | |
38384150 JJ |
1317 | { |
1318 | size_t noutputs; | |
1319 | const char **oconstraints; | |
1320 | const char *constraint; | |
1321 | bool allows_mem, allows_reg; | |
1322 | ||
1323 | noutputs = gimple_asm_noutputs (stmt); | |
1324 | oconstraints = XALLOCAVEC (const char *, noutputs); | |
1325 | ||
1326 | for (i = 0; i < gimple_asm_noutputs (stmt); ++i) | |
1327 | { | |
1328 | tree link = gimple_asm_output_op (stmt, i); | |
1329 | tree op = TREE_VALUE (link); | |
1330 | oconstraints[i] | |
1331 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
1332 | if (REFERENCE_CLASS_P (op) | |
1333 | && (op = maybe_fold_reference (op, true)) != NULL_TREE) | |
1334 | { | |
1335 | TREE_VALUE (link) = op; | |
1336 | changed = true; | |
1337 | } | |
1338 | } | |
1339 | for (i = 0; i < gimple_asm_ninputs (stmt); ++i) | |
1340 | { | |
1341 | tree link = gimple_asm_input_op (stmt, i); | |
1342 | tree op = TREE_VALUE (link); | |
1343 | constraint | |
1344 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
1345 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, | |
1346 | oconstraints, &allows_mem, &allows_reg); | |
1347 | if (REFERENCE_CLASS_P (op) | |
1348 | && (op = maybe_fold_reference (op, !allows_reg && allows_mem)) | |
1349 | != NULL_TREE) | |
1350 | { | |
1351 | TREE_VALUE (link) = op; | |
1352 | changed = true; | |
1353 | } | |
1354 | } | |
1355 | } | |
cbdd87d4 RG |
1356 | break; |
1357 | ||
bd422c4a RG |
1358 | case GIMPLE_DEBUG: |
1359 | if (gimple_debug_bind_p (stmt)) | |
1360 | { | |
1361 | tree val = gimple_debug_bind_get_value (stmt); | |
1362 | if (val | |
1363 | && REFERENCE_CLASS_P (val)) | |
1364 | { | |
1365 | tree tem = maybe_fold_reference (val, false); | |
1366 | if (tem) | |
1367 | { | |
1368 | gimple_debug_bind_set_value (stmt, tem); | |
1369 | changed = true; | |
1370 | } | |
1371 | } | |
3e888a5e RG |
1372 | else if (val |
1373 | && TREE_CODE (val) == ADDR_EXPR) | |
1374 | { | |
1375 | tree ref = TREE_OPERAND (val, 0); | |
1376 | tree tem = maybe_fold_reference (ref, false); | |
1377 | if (tem) | |
1378 | { | |
1379 | tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val)); | |
1380 | gimple_debug_bind_set_value (stmt, tem); | |
1381 | changed = true; | |
1382 | } | |
1383 | } | |
bd422c4a RG |
1384 | } |
1385 | break; | |
1386 | ||
cbdd87d4 RG |
1387 | default:; |
1388 | } | |
1389 | ||
1390 | stmt = gsi_stmt (*gsi); | |
1391 | ||
37376165 RB |
1392 | /* Fold *& on the lhs. */ |
1393 | if (gimple_has_lhs (stmt)) | |
cbdd87d4 RG |
1394 | { |
1395 | tree lhs = gimple_get_lhs (stmt); | |
1396 | if (lhs && REFERENCE_CLASS_P (lhs)) | |
1397 | { | |
1398 | tree new_lhs = maybe_fold_reference (lhs, true); | |
1399 | if (new_lhs) | |
1400 | { | |
1401 | gimple_set_lhs (stmt, new_lhs); | |
1402 | changed = true; | |
1403 | } | |
1404 | } | |
1405 | } | |
1406 | ||
1407 | return changed; | |
1408 | } | |
1409 | ||
1410 | /* Fold the statement pointed to by GSI. In some cases, this function may | |
1411 | replace the whole statement with a new one. Returns true iff folding | |
1412 | makes any changes. | |
1413 | The statement pointed to by GSI should be in valid gimple form but may | |
1414 | be in unfolded state as resulting from for example constant propagation | |
1415 | which can produce *&x = 0. */ | |
1416 | ||
1417 | bool | |
1418 | fold_stmt (gimple_stmt_iterator *gsi) | |
1419 | { | |
1420 | return fold_stmt_1 (gsi, false); | |
1421 | } | |
1422 | ||
59401b92 | 1423 | /* Perform the minimal folding on statement *GSI. Only operations like |
cbdd87d4 RG |
1424 | *&x created by constant propagation are handled. The statement cannot |
1425 | be replaced with a new one. Return true if the statement was | |
1426 | changed, false otherwise. | |
59401b92 | 1427 | The statement *GSI should be in valid gimple form but may |
cbdd87d4 RG |
1428 | be in unfolded state as resulting from for example constant propagation |
1429 | which can produce *&x = 0. */ | |
1430 | ||
1431 | bool | |
59401b92 | 1432 | fold_stmt_inplace (gimple_stmt_iterator *gsi) |
cbdd87d4 | 1433 | { |
59401b92 RG |
1434 | gimple stmt = gsi_stmt (*gsi); |
1435 | bool changed = fold_stmt_1 (gsi, true); | |
1436 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
cbdd87d4 RG |
1437 | return changed; |
1438 | } | |
1439 | ||
e89065a1 SL |
1440 | /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE |
1441 | if EXPR is null or we don't know how. | |
1442 | If non-null, the result always has boolean type. */ | |
1443 | ||
1444 | static tree | |
1445 | canonicalize_bool (tree expr, bool invert) | |
1446 | { | |
1447 | if (!expr) | |
1448 | return NULL_TREE; | |
1449 | else if (invert) | |
1450 | { | |
1451 | if (integer_nonzerop (expr)) | |
1452 | return boolean_false_node; | |
1453 | else if (integer_zerop (expr)) | |
1454 | return boolean_true_node; | |
1455 | else if (TREE_CODE (expr) == SSA_NAME) | |
1456 | return fold_build2 (EQ_EXPR, boolean_type_node, expr, | |
1457 | build_int_cst (TREE_TYPE (expr), 0)); | |
1458 | else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison) | |
1459 | return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false), | |
1460 | boolean_type_node, | |
1461 | TREE_OPERAND (expr, 0), | |
1462 | TREE_OPERAND (expr, 1)); | |
1463 | else | |
1464 | return NULL_TREE; | |
1465 | } | |
1466 | else | |
1467 | { | |
1468 | if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
1469 | return expr; | |
1470 | if (integer_nonzerop (expr)) | |
1471 | return boolean_true_node; | |
1472 | else if (integer_zerop (expr)) | |
1473 | return boolean_false_node; | |
1474 | else if (TREE_CODE (expr) == SSA_NAME) | |
1475 | return fold_build2 (NE_EXPR, boolean_type_node, expr, | |
1476 | build_int_cst (TREE_TYPE (expr), 0)); | |
1477 | else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison) | |
1478 | return fold_build2 (TREE_CODE (expr), | |
1479 | boolean_type_node, | |
1480 | TREE_OPERAND (expr, 0), | |
1481 | TREE_OPERAND (expr, 1)); | |
1482 | else | |
1483 | return NULL_TREE; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | /* Check to see if a boolean expression EXPR is logically equivalent to the | |
1488 | comparison (OP1 CODE OP2). Check for various identities involving | |
1489 | SSA_NAMEs. */ | |
1490 | ||
1491 | static bool | |
1492 | same_bool_comparison_p (const_tree expr, enum tree_code code, | |
1493 | const_tree op1, const_tree op2) | |
1494 | { | |
1495 | gimple s; | |
1496 | ||
1497 | /* The obvious case. */ | |
1498 | if (TREE_CODE (expr) == code | |
1499 | && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0) | |
1500 | && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0)) | |
1501 | return true; | |
1502 | ||
1503 | /* Check for comparing (name, name != 0) and the case where expr | |
1504 | is an SSA_NAME with a definition matching the comparison. */ | |
1505 | if (TREE_CODE (expr) == SSA_NAME | |
1506 | && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
1507 | { | |
1508 | if (operand_equal_p (expr, op1, 0)) | |
1509 | return ((code == NE_EXPR && integer_zerop (op2)) | |
1510 | || (code == EQ_EXPR && integer_nonzerop (op2))); | |
1511 | s = SSA_NAME_DEF_STMT (expr); | |
1512 | if (is_gimple_assign (s) | |
1513 | && gimple_assign_rhs_code (s) == code | |
1514 | && operand_equal_p (gimple_assign_rhs1 (s), op1, 0) | |
1515 | && operand_equal_p (gimple_assign_rhs2 (s), op2, 0)) | |
1516 | return true; | |
1517 | } | |
1518 | ||
1519 | /* If op1 is of the form (name != 0) or (name == 0), and the definition | |
1520 | of name is a comparison, recurse. */ | |
1521 | if (TREE_CODE (op1) == SSA_NAME | |
1522 | && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE) | |
1523 | { | |
1524 | s = SSA_NAME_DEF_STMT (op1); | |
1525 | if (is_gimple_assign (s) | |
1526 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison) | |
1527 | { | |
1528 | enum tree_code c = gimple_assign_rhs_code (s); | |
1529 | if ((c == NE_EXPR && integer_zerop (op2)) | |
1530 | || (c == EQ_EXPR && integer_nonzerop (op2))) | |
1531 | return same_bool_comparison_p (expr, c, | |
1532 | gimple_assign_rhs1 (s), | |
1533 | gimple_assign_rhs2 (s)); | |
1534 | if ((c == EQ_EXPR && integer_zerop (op2)) | |
1535 | || (c == NE_EXPR && integer_nonzerop (op2))) | |
1536 | return same_bool_comparison_p (expr, | |
1537 | invert_tree_comparison (c, false), | |
1538 | gimple_assign_rhs1 (s), | |
1539 | gimple_assign_rhs2 (s)); | |
1540 | } | |
1541 | } | |
1542 | return false; | |
1543 | } | |
1544 | ||
1545 | /* Check to see if two boolean expressions OP1 and OP2 are logically | |
1546 | equivalent. */ | |
1547 | ||
1548 | static bool | |
1549 | same_bool_result_p (const_tree op1, const_tree op2) | |
1550 | { | |
1551 | /* Simple cases first. */ | |
1552 | if (operand_equal_p (op1, op2, 0)) | |
1553 | return true; | |
1554 | ||
1555 | /* Check the cases where at least one of the operands is a comparison. | |
1556 | These are a bit smarter than operand_equal_p in that they apply some | |
1557 | identifies on SSA_NAMEs. */ | |
1558 | if (TREE_CODE_CLASS (TREE_CODE (op2)) == tcc_comparison | |
1559 | && same_bool_comparison_p (op1, TREE_CODE (op2), | |
1560 | TREE_OPERAND (op2, 0), | |
1561 | TREE_OPERAND (op2, 1))) | |
1562 | return true; | |
1563 | if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_comparison | |
1564 | && same_bool_comparison_p (op2, TREE_CODE (op1), | |
1565 | TREE_OPERAND (op1, 0), | |
1566 | TREE_OPERAND (op1, 1))) | |
1567 | return true; | |
1568 | ||
1569 | /* Default case. */ | |
1570 | return false; | |
1571 | } | |
1572 | ||
1573 | /* Forward declarations for some mutually recursive functions. */ | |
1574 | ||
1575 | static tree | |
1576 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
1577 | enum tree_code code2, tree op2a, tree op2b); | |
1578 | static tree | |
1579 | and_var_with_comparison (tree var, bool invert, | |
1580 | enum tree_code code2, tree op2a, tree op2b); | |
1581 | static tree | |
1582 | and_var_with_comparison_1 (gimple stmt, | |
1583 | enum tree_code code2, tree op2a, tree op2b); | |
1584 | static tree | |
1585 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
1586 | enum tree_code code2, tree op2a, tree op2b); | |
1587 | static tree | |
1588 | or_var_with_comparison (tree var, bool invert, | |
1589 | enum tree_code code2, tree op2a, tree op2b); | |
1590 | static tree | |
1591 | or_var_with_comparison_1 (gimple stmt, | |
1592 | enum tree_code code2, tree op2a, tree op2b); | |
1593 | ||
1594 | /* Helper function for and_comparisons_1: try to simplify the AND of the | |
1595 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
1596 | If INVERT is true, invert the value of the VAR before doing the AND. | |
1597 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
1598 | ||
1599 | static tree | |
1600 | and_var_with_comparison (tree var, bool invert, | |
1601 | enum tree_code code2, tree op2a, tree op2b) | |
1602 | { | |
1603 | tree t; | |
1604 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
1605 | ||
1606 | /* We can only deal with variables whose definitions are assignments. */ | |
1607 | if (!is_gimple_assign (stmt)) | |
1608 | return NULL_TREE; | |
1609 | ||
1610 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
1611 | !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b)) | |
1612 | Then we only have to consider the simpler non-inverted cases. */ | |
1613 | if (invert) | |
1614 | t = or_var_with_comparison_1 (stmt, | |
1615 | invert_tree_comparison (code2, false), | |
1616 | op2a, op2b); | |
1617 | else | |
1618 | t = and_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
1619 | return canonicalize_bool (t, invert); | |
1620 | } | |
1621 | ||
1622 | /* Try to simplify the AND of the ssa variable defined by the assignment | |
1623 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
1624 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
1625 | ||
1626 | static tree | |
1627 | and_var_with_comparison_1 (gimple stmt, | |
1628 | enum tree_code code2, tree op2a, tree op2b) | |
1629 | { | |
1630 | tree var = gimple_assign_lhs (stmt); | |
1631 | tree true_test_var = NULL_TREE; | |
1632 | tree false_test_var = NULL_TREE; | |
1633 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
1634 | ||
1635 | /* Check for identities like (var AND (var == 0)) => false. */ | |
1636 | if (TREE_CODE (op2a) == SSA_NAME | |
1637 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
1638 | { | |
1639 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
1640 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
1641 | { | |
1642 | true_test_var = op2a; | |
1643 | if (var == true_test_var) | |
1644 | return var; | |
1645 | } | |
1646 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
1647 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
1648 | { | |
1649 | false_test_var = op2a; | |
1650 | if (var == false_test_var) | |
1651 | return boolean_false_node; | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | /* If the definition is a comparison, recurse on it. */ | |
1656 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
1657 | { | |
1658 | tree t = and_comparisons_1 (innercode, | |
1659 | gimple_assign_rhs1 (stmt), | |
1660 | gimple_assign_rhs2 (stmt), | |
1661 | code2, | |
1662 | op2a, | |
1663 | op2b); | |
1664 | if (t) | |
1665 | return t; | |
1666 | } | |
1667 | ||
1668 | /* If the definition is an AND or OR expression, we may be able to | |
1669 | simplify by reassociating. */ | |
eb9820c0 KT |
1670 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
1671 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
e89065a1 SL |
1672 | { |
1673 | tree inner1 = gimple_assign_rhs1 (stmt); | |
1674 | tree inner2 = gimple_assign_rhs2 (stmt); | |
1675 | gimple s; | |
1676 | tree t; | |
1677 | tree partial = NULL_TREE; | |
eb9820c0 | 1678 | bool is_and = (innercode == BIT_AND_EXPR); |
e89065a1 SL |
1679 | |
1680 | /* Check for boolean identities that don't require recursive examination | |
1681 | of inner1/inner2: | |
1682 | inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var | |
1683 | inner1 AND (inner1 OR inner2) => inner1 | |
1684 | !inner1 AND (inner1 AND inner2) => false | |
1685 | !inner1 AND (inner1 OR inner2) => !inner1 AND inner2 | |
1686 | Likewise for similar cases involving inner2. */ | |
1687 | if (inner1 == true_test_var) | |
1688 | return (is_and ? var : inner1); | |
1689 | else if (inner2 == true_test_var) | |
1690 | return (is_and ? var : inner2); | |
1691 | else if (inner1 == false_test_var) | |
1692 | return (is_and | |
1693 | ? boolean_false_node | |
1694 | : and_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
1695 | else if (inner2 == false_test_var) | |
1696 | return (is_and | |
1697 | ? boolean_false_node | |
1698 | : and_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
1699 | ||
1700 | /* Next, redistribute/reassociate the AND across the inner tests. | |
1701 | Compute the first partial result, (inner1 AND (op2a code op2b)) */ | |
1702 | if (TREE_CODE (inner1) == SSA_NAME | |
1703 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
1704 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
1705 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
1706 | gimple_assign_rhs1 (s), | |
1707 | gimple_assign_rhs2 (s), | |
1708 | code2, op2a, op2b))) | |
1709 | { | |
1710 | /* Handle the AND case, where we are reassociating: | |
1711 | (inner1 AND inner2) AND (op2a code2 op2b) | |
1712 | => (t AND inner2) | |
1713 | If the partial result t is a constant, we win. Otherwise | |
1714 | continue on to try reassociating with the other inner test. */ | |
1715 | if (is_and) | |
1716 | { | |
1717 | if (integer_onep (t)) | |
1718 | return inner2; | |
1719 | else if (integer_zerop (t)) | |
1720 | return boolean_false_node; | |
1721 | } | |
1722 | ||
1723 | /* Handle the OR case, where we are redistributing: | |
1724 | (inner1 OR inner2) AND (op2a code2 op2b) | |
1725 | => (t OR (inner2 AND (op2a code2 op2b))) */ | |
8236c8eb JJ |
1726 | else if (integer_onep (t)) |
1727 | return boolean_true_node; | |
1728 | ||
1729 | /* Save partial result for later. */ | |
1730 | partial = t; | |
e89065a1 SL |
1731 | } |
1732 | ||
1733 | /* Compute the second partial result, (inner2 AND (op2a code op2b)) */ | |
1734 | if (TREE_CODE (inner2) == SSA_NAME | |
1735 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
1736 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
1737 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
1738 | gimple_assign_rhs1 (s), | |
1739 | gimple_assign_rhs2 (s), | |
1740 | code2, op2a, op2b))) | |
1741 | { | |
1742 | /* Handle the AND case, where we are reassociating: | |
1743 | (inner1 AND inner2) AND (op2a code2 op2b) | |
1744 | => (inner1 AND t) */ | |
1745 | if (is_and) | |
1746 | { | |
1747 | if (integer_onep (t)) | |
1748 | return inner1; | |
1749 | else if (integer_zerop (t)) | |
1750 | return boolean_false_node; | |
8236c8eb JJ |
1751 | /* If both are the same, we can apply the identity |
1752 | (x AND x) == x. */ | |
1753 | else if (partial && same_bool_result_p (t, partial)) | |
1754 | return t; | |
e89065a1 SL |
1755 | } |
1756 | ||
1757 | /* Handle the OR case. where we are redistributing: | |
1758 | (inner1 OR inner2) AND (op2a code2 op2b) | |
1759 | => (t OR (inner1 AND (op2a code2 op2b))) | |
1760 | => (t OR partial) */ | |
1761 | else | |
1762 | { | |
1763 | if (integer_onep (t)) | |
1764 | return boolean_true_node; | |
1765 | else if (partial) | |
1766 | { | |
1767 | /* We already got a simplification for the other | |
1768 | operand to the redistributed OR expression. The | |
1769 | interesting case is when at least one is false. | |
1770 | Or, if both are the same, we can apply the identity | |
1771 | (x OR x) == x. */ | |
1772 | if (integer_zerop (partial)) | |
1773 | return t; | |
1774 | else if (integer_zerop (t)) | |
1775 | return partial; | |
1776 | else if (same_bool_result_p (t, partial)) | |
1777 | return t; | |
1778 | } | |
1779 | } | |
1780 | } | |
1781 | } | |
1782 | return NULL_TREE; | |
1783 | } | |
1784 | ||
1785 | /* Try to simplify the AND of two comparisons defined by | |
1786 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
1787 | If this can be done without constructing an intermediate value, | |
1788 | return the resulting tree; otherwise NULL_TREE is returned. | |
1789 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
1790 | in the first comparison but not the second. */ | |
1791 | ||
1792 | static tree | |
1793 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
1794 | enum tree_code code2, tree op2a, tree op2b) | |
1795 | { | |
ae22ac3c | 1796 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
31ed6226 | 1797 | |
e89065a1 SL |
1798 | /* First check for ((x CODE1 y) AND (x CODE2 y)). */ |
1799 | if (operand_equal_p (op1a, op2a, 0) | |
1800 | && operand_equal_p (op1b, op2b, 0)) | |
1801 | { | |
eb9820c0 | 1802 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
1803 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
1804 | TRUTH_ANDIF_EXPR, code1, code2, | |
31ed6226 | 1805 | truth_type, op1a, op1b); |
e89065a1 SL |
1806 | if (t) |
1807 | return t; | |
1808 | } | |
1809 | ||
1810 | /* Likewise the swapped case of the above. */ | |
1811 | if (operand_equal_p (op1a, op2b, 0) | |
1812 | && operand_equal_p (op1b, op2a, 0)) | |
1813 | { | |
eb9820c0 | 1814 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
1815 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
1816 | TRUTH_ANDIF_EXPR, code1, | |
1817 | swap_tree_comparison (code2), | |
31ed6226 | 1818 | truth_type, op1a, op1b); |
e89065a1 SL |
1819 | if (t) |
1820 | return t; | |
1821 | } | |
1822 | ||
1823 | /* If both comparisons are of the same value against constants, we might | |
1824 | be able to merge them. */ | |
1825 | if (operand_equal_p (op1a, op2a, 0) | |
1826 | && TREE_CODE (op1b) == INTEGER_CST | |
1827 | && TREE_CODE (op2b) == INTEGER_CST) | |
1828 | { | |
1829 | int cmp = tree_int_cst_compare (op1b, op2b); | |
1830 | ||
1831 | /* If we have (op1a == op1b), we should either be able to | |
1832 | return that or FALSE, depending on whether the constant op1b | |
1833 | also satisfies the other comparison against op2b. */ | |
1834 | if (code1 == EQ_EXPR) | |
1835 | { | |
1836 | bool done = true; | |
1837 | bool val; | |
1838 | switch (code2) | |
1839 | { | |
1840 | case EQ_EXPR: val = (cmp == 0); break; | |
1841 | case NE_EXPR: val = (cmp != 0); break; | |
1842 | case LT_EXPR: val = (cmp < 0); break; | |
1843 | case GT_EXPR: val = (cmp > 0); break; | |
1844 | case LE_EXPR: val = (cmp <= 0); break; | |
1845 | case GE_EXPR: val = (cmp >= 0); break; | |
1846 | default: done = false; | |
1847 | } | |
1848 | if (done) | |
1849 | { | |
1850 | if (val) | |
1851 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1852 | else | |
1853 | return boolean_false_node; | |
1854 | } | |
1855 | } | |
1856 | /* Likewise if the second comparison is an == comparison. */ | |
1857 | else if (code2 == EQ_EXPR) | |
1858 | { | |
1859 | bool done = true; | |
1860 | bool val; | |
1861 | switch (code1) | |
1862 | { | |
1863 | case EQ_EXPR: val = (cmp == 0); break; | |
1864 | case NE_EXPR: val = (cmp != 0); break; | |
1865 | case LT_EXPR: val = (cmp > 0); break; | |
1866 | case GT_EXPR: val = (cmp < 0); break; | |
1867 | case LE_EXPR: val = (cmp >= 0); break; | |
1868 | case GE_EXPR: val = (cmp <= 0); break; | |
1869 | default: done = false; | |
1870 | } | |
1871 | if (done) | |
1872 | { | |
1873 | if (val) | |
1874 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1875 | else | |
1876 | return boolean_false_node; | |
1877 | } | |
1878 | } | |
1879 | ||
1880 | /* Same business with inequality tests. */ | |
1881 | else if (code1 == NE_EXPR) | |
1882 | { | |
1883 | bool val; | |
1884 | switch (code2) | |
1885 | { | |
1886 | case EQ_EXPR: val = (cmp != 0); break; | |
1887 | case NE_EXPR: val = (cmp == 0); break; | |
1888 | case LT_EXPR: val = (cmp >= 0); break; | |
1889 | case GT_EXPR: val = (cmp <= 0); break; | |
1890 | case LE_EXPR: val = (cmp > 0); break; | |
1891 | case GE_EXPR: val = (cmp < 0); break; | |
1892 | default: | |
1893 | val = false; | |
1894 | } | |
1895 | if (val) | |
1896 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1897 | } | |
1898 | else if (code2 == NE_EXPR) | |
1899 | { | |
1900 | bool val; | |
1901 | switch (code1) | |
1902 | { | |
1903 | case EQ_EXPR: val = (cmp == 0); break; | |
1904 | case NE_EXPR: val = (cmp != 0); break; | |
1905 | case LT_EXPR: val = (cmp <= 0); break; | |
1906 | case GT_EXPR: val = (cmp >= 0); break; | |
1907 | case LE_EXPR: val = (cmp < 0); break; | |
1908 | case GE_EXPR: val = (cmp > 0); break; | |
1909 | default: | |
1910 | val = false; | |
1911 | } | |
1912 | if (val) | |
1913 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1914 | } | |
1915 | ||
1916 | /* Chose the more restrictive of two < or <= comparisons. */ | |
1917 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
1918 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
1919 | { | |
1920 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
1921 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1922 | else | |
1923 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1924 | } | |
1925 | ||
1926 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
1927 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
1928 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
1929 | { | |
1930 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
1931 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
1932 | else | |
1933 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
1934 | } | |
1935 | ||
1936 | /* Check for singleton ranges. */ | |
1937 | else if (cmp == 0 | |
1938 | && ((code1 == LE_EXPR && code2 == GE_EXPR) | |
1939 | || (code1 == GE_EXPR && code2 == LE_EXPR))) | |
1940 | return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b); | |
1941 | ||
1942 | /* Check for disjoint ranges. */ | |
1943 | else if (cmp <= 0 | |
1944 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
1945 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
1946 | return boolean_false_node; | |
1947 | else if (cmp >= 0 | |
1948 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
1949 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
1950 | return boolean_false_node; | |
1951 | } | |
1952 | ||
1953 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
1954 | NAME's definition is a truth value. See if there are any simplifications | |
1955 | that can be done against the NAME's definition. */ | |
1956 | if (TREE_CODE (op1a) == SSA_NAME | |
1957 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
1958 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
1959 | { | |
1960 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
1961 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
1962 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
1963 | switch (gimple_code (stmt)) | |
1964 | { | |
1965 | case GIMPLE_ASSIGN: | |
1966 | /* Try to simplify by copy-propagating the definition. */ | |
1967 | return and_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
1968 | ||
1969 | case GIMPLE_PHI: | |
1970 | /* If every argument to the PHI produces the same result when | |
1971 | ANDed with the second comparison, we win. | |
1972 | Do not do this unless the type is bool since we need a bool | |
1973 | result here anyway. */ | |
1974 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
1975 | { | |
1976 | tree result = NULL_TREE; | |
1977 | unsigned i; | |
1978 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
1979 | { | |
1980 | tree arg = gimple_phi_arg_def (stmt, i); | |
1981 | ||
1982 | /* If this PHI has itself as an argument, ignore it. | |
1983 | If all the other args produce the same result, | |
1984 | we're still OK. */ | |
1985 | if (arg == gimple_phi_result (stmt)) | |
1986 | continue; | |
1987 | else if (TREE_CODE (arg) == INTEGER_CST) | |
1988 | { | |
1989 | if (invert ? integer_nonzerop (arg) : integer_zerop (arg)) | |
1990 | { | |
1991 | if (!result) | |
1992 | result = boolean_false_node; | |
1993 | else if (!integer_zerop (result)) | |
1994 | return NULL_TREE; | |
1995 | } | |
1996 | else if (!result) | |
1997 | result = fold_build2 (code2, boolean_type_node, | |
1998 | op2a, op2b); | |
1999 | else if (!same_bool_comparison_p (result, | |
2000 | code2, op2a, op2b)) | |
2001 | return NULL_TREE; | |
2002 | } | |
0e8b84ec JJ |
2003 | else if (TREE_CODE (arg) == SSA_NAME |
2004 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
e89065a1 | 2005 | { |
6c66f733 JJ |
2006 | tree temp; |
2007 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
2008 | /* In simple cases we can look through PHI nodes, | |
2009 | but we have to be careful with loops. | |
2010 | See PR49073. */ | |
2011 | if (! dom_info_available_p (CDI_DOMINATORS) | |
2012 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
2013 | || dominated_by_p (CDI_DOMINATORS, | |
2014 | gimple_bb (def_stmt), | |
2015 | gimple_bb (stmt))) | |
2016 | return NULL_TREE; | |
2017 | temp = and_var_with_comparison (arg, invert, code2, | |
2018 | op2a, op2b); | |
e89065a1 SL |
2019 | if (!temp) |
2020 | return NULL_TREE; | |
2021 | else if (!result) | |
2022 | result = temp; | |
2023 | else if (!same_bool_result_p (result, temp)) | |
2024 | return NULL_TREE; | |
2025 | } | |
2026 | else | |
2027 | return NULL_TREE; | |
2028 | } | |
2029 | return result; | |
2030 | } | |
2031 | ||
2032 | default: | |
2033 | break; | |
2034 | } | |
2035 | } | |
2036 | return NULL_TREE; | |
2037 | } | |
2038 | ||
2039 | /* Try to simplify the AND of two comparisons, specified by | |
2040 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
2041 | If this can be simplified to a single expression (without requiring | |
2042 | introducing more SSA variables to hold intermediate values), | |
2043 | return the resulting tree. Otherwise return NULL_TREE. | |
2044 | If the result expression is non-null, it has boolean type. */ | |
2045 | ||
2046 | tree | |
2047 | maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
2048 | enum tree_code code2, tree op2a, tree op2b) | |
2049 | { | |
2050 | tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
2051 | if (t) | |
2052 | return t; | |
2053 | else | |
2054 | return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
2055 | } | |
2056 | ||
2057 | /* Helper function for or_comparisons_1: try to simplify the OR of the | |
2058 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
2059 | If INVERT is true, invert the value of VAR before doing the OR. | |
2060 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
2061 | ||
2062 | static tree | |
2063 | or_var_with_comparison (tree var, bool invert, | |
2064 | enum tree_code code2, tree op2a, tree op2b) | |
2065 | { | |
2066 | tree t; | |
2067 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
2068 | ||
2069 | /* We can only deal with variables whose definitions are assignments. */ | |
2070 | if (!is_gimple_assign (stmt)) | |
2071 | return NULL_TREE; | |
2072 | ||
2073 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
2074 | !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b)) | |
2075 | Then we only have to consider the simpler non-inverted cases. */ | |
2076 | if (invert) | |
2077 | t = and_var_with_comparison_1 (stmt, | |
2078 | invert_tree_comparison (code2, false), | |
2079 | op2a, op2b); | |
2080 | else | |
2081 | t = or_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
2082 | return canonicalize_bool (t, invert); | |
2083 | } | |
2084 | ||
2085 | /* Try to simplify the OR of the ssa variable defined by the assignment | |
2086 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
2087 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
2088 | ||
2089 | static tree | |
2090 | or_var_with_comparison_1 (gimple stmt, | |
2091 | enum tree_code code2, tree op2a, tree op2b) | |
2092 | { | |
2093 | tree var = gimple_assign_lhs (stmt); | |
2094 | tree true_test_var = NULL_TREE; | |
2095 | tree false_test_var = NULL_TREE; | |
2096 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
2097 | ||
2098 | /* Check for identities like (var OR (var != 0)) => true . */ | |
2099 | if (TREE_CODE (op2a) == SSA_NAME | |
2100 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
2101 | { | |
2102 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
2103 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
2104 | { | |
2105 | true_test_var = op2a; | |
2106 | if (var == true_test_var) | |
2107 | return var; | |
2108 | } | |
2109 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
2110 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
2111 | { | |
2112 | false_test_var = op2a; | |
2113 | if (var == false_test_var) | |
2114 | return boolean_true_node; | |
2115 | } | |
2116 | } | |
2117 | ||
2118 | /* If the definition is a comparison, recurse on it. */ | |
2119 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
2120 | { | |
2121 | tree t = or_comparisons_1 (innercode, | |
2122 | gimple_assign_rhs1 (stmt), | |
2123 | gimple_assign_rhs2 (stmt), | |
2124 | code2, | |
2125 | op2a, | |
2126 | op2b); | |
2127 | if (t) | |
2128 | return t; | |
2129 | } | |
2130 | ||
2131 | /* If the definition is an AND or OR expression, we may be able to | |
2132 | simplify by reassociating. */ | |
eb9820c0 KT |
2133 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
2134 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
e89065a1 SL |
2135 | { |
2136 | tree inner1 = gimple_assign_rhs1 (stmt); | |
2137 | tree inner2 = gimple_assign_rhs2 (stmt); | |
2138 | gimple s; | |
2139 | tree t; | |
2140 | tree partial = NULL_TREE; | |
eb9820c0 | 2141 | bool is_or = (innercode == BIT_IOR_EXPR); |
e89065a1 SL |
2142 | |
2143 | /* Check for boolean identities that don't require recursive examination | |
2144 | of inner1/inner2: | |
2145 | inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var | |
2146 | inner1 OR (inner1 AND inner2) => inner1 | |
2147 | !inner1 OR (inner1 OR inner2) => true | |
2148 | !inner1 OR (inner1 AND inner2) => !inner1 OR inner2 | |
2149 | */ | |
2150 | if (inner1 == true_test_var) | |
2151 | return (is_or ? var : inner1); | |
2152 | else if (inner2 == true_test_var) | |
2153 | return (is_or ? var : inner2); | |
2154 | else if (inner1 == false_test_var) | |
2155 | return (is_or | |
2156 | ? boolean_true_node | |
2157 | : or_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
2158 | else if (inner2 == false_test_var) | |
2159 | return (is_or | |
2160 | ? boolean_true_node | |
2161 | : or_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
2162 | ||
2163 | /* Next, redistribute/reassociate the OR across the inner tests. | |
2164 | Compute the first partial result, (inner1 OR (op2a code op2b)) */ | |
2165 | if (TREE_CODE (inner1) == SSA_NAME | |
2166 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
2167 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
2168 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
2169 | gimple_assign_rhs1 (s), | |
2170 | gimple_assign_rhs2 (s), | |
2171 | code2, op2a, op2b))) | |
2172 | { | |
2173 | /* Handle the OR case, where we are reassociating: | |
2174 | (inner1 OR inner2) OR (op2a code2 op2b) | |
2175 | => (t OR inner2) | |
2176 | If the partial result t is a constant, we win. Otherwise | |
2177 | continue on to try reassociating with the other inner test. */ | |
8236c8eb | 2178 | if (is_or) |
e89065a1 SL |
2179 | { |
2180 | if (integer_onep (t)) | |
2181 | return boolean_true_node; | |
2182 | else if (integer_zerop (t)) | |
2183 | return inner2; | |
2184 | } | |
2185 | ||
2186 | /* Handle the AND case, where we are redistributing: | |
2187 | (inner1 AND inner2) OR (op2a code2 op2b) | |
2188 | => (t AND (inner2 OR (op2a code op2b))) */ | |
8236c8eb JJ |
2189 | else if (integer_zerop (t)) |
2190 | return boolean_false_node; | |
2191 | ||
2192 | /* Save partial result for later. */ | |
2193 | partial = t; | |
e89065a1 SL |
2194 | } |
2195 | ||
2196 | /* Compute the second partial result, (inner2 OR (op2a code op2b)) */ | |
2197 | if (TREE_CODE (inner2) == SSA_NAME | |
2198 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
2199 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
2200 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
2201 | gimple_assign_rhs1 (s), | |
2202 | gimple_assign_rhs2 (s), | |
2203 | code2, op2a, op2b))) | |
2204 | { | |
2205 | /* Handle the OR case, where we are reassociating: | |
2206 | (inner1 OR inner2) OR (op2a code2 op2b) | |
8236c8eb JJ |
2207 | => (inner1 OR t) |
2208 | => (t OR partial) */ | |
2209 | if (is_or) | |
e89065a1 SL |
2210 | { |
2211 | if (integer_zerop (t)) | |
2212 | return inner1; | |
2213 | else if (integer_onep (t)) | |
2214 | return boolean_true_node; | |
8236c8eb JJ |
2215 | /* If both are the same, we can apply the identity |
2216 | (x OR x) == x. */ | |
2217 | else if (partial && same_bool_result_p (t, partial)) | |
2218 | return t; | |
e89065a1 SL |
2219 | } |
2220 | ||
2221 | /* Handle the AND case, where we are redistributing: | |
2222 | (inner1 AND inner2) OR (op2a code2 op2b) | |
2223 | => (t AND (inner1 OR (op2a code2 op2b))) | |
2224 | => (t AND partial) */ | |
2225 | else | |
2226 | { | |
2227 | if (integer_zerop (t)) | |
2228 | return boolean_false_node; | |
2229 | else if (partial) | |
2230 | { | |
2231 | /* We already got a simplification for the other | |
2232 | operand to the redistributed AND expression. The | |
2233 | interesting case is when at least one is true. | |
2234 | Or, if both are the same, we can apply the identity | |
8236c8eb | 2235 | (x AND x) == x. */ |
e89065a1 SL |
2236 | if (integer_onep (partial)) |
2237 | return t; | |
2238 | else if (integer_onep (t)) | |
2239 | return partial; | |
2240 | else if (same_bool_result_p (t, partial)) | |
8236c8eb | 2241 | return t; |
e89065a1 SL |
2242 | } |
2243 | } | |
2244 | } | |
2245 | } | |
2246 | return NULL_TREE; | |
2247 | } | |
2248 | ||
2249 | /* Try to simplify the OR of two comparisons defined by | |
2250 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
2251 | If this can be done without constructing an intermediate value, | |
2252 | return the resulting tree; otherwise NULL_TREE is returned. | |
2253 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
2254 | in the first comparison but not the second. */ | |
2255 | ||
2256 | static tree | |
2257 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
2258 | enum tree_code code2, tree op2a, tree op2b) | |
2259 | { | |
ae22ac3c | 2260 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
31ed6226 | 2261 | |
e89065a1 SL |
2262 | /* First check for ((x CODE1 y) OR (x CODE2 y)). */ |
2263 | if (operand_equal_p (op1a, op2a, 0) | |
2264 | && operand_equal_p (op1b, op2b, 0)) | |
2265 | { | |
eb9820c0 | 2266 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
2267 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
2268 | TRUTH_ORIF_EXPR, code1, code2, | |
31ed6226 | 2269 | truth_type, op1a, op1b); |
e89065a1 SL |
2270 | if (t) |
2271 | return t; | |
2272 | } | |
2273 | ||
2274 | /* Likewise the swapped case of the above. */ | |
2275 | if (operand_equal_p (op1a, op2b, 0) | |
2276 | && operand_equal_p (op1b, op2a, 0)) | |
2277 | { | |
eb9820c0 | 2278 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
2279 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
2280 | TRUTH_ORIF_EXPR, code1, | |
2281 | swap_tree_comparison (code2), | |
31ed6226 | 2282 | truth_type, op1a, op1b); |
e89065a1 SL |
2283 | if (t) |
2284 | return t; | |
2285 | } | |
2286 | ||
2287 | /* If both comparisons are of the same value against constants, we might | |
2288 | be able to merge them. */ | |
2289 | if (operand_equal_p (op1a, op2a, 0) | |
2290 | && TREE_CODE (op1b) == INTEGER_CST | |
2291 | && TREE_CODE (op2b) == INTEGER_CST) | |
2292 | { | |
2293 | int cmp = tree_int_cst_compare (op1b, op2b); | |
2294 | ||
2295 | /* If we have (op1a != op1b), we should either be able to | |
2296 | return that or TRUE, depending on whether the constant op1b | |
2297 | also satisfies the other comparison against op2b. */ | |
2298 | if (code1 == NE_EXPR) | |
2299 | { | |
2300 | bool done = true; | |
2301 | bool val; | |
2302 | switch (code2) | |
2303 | { | |
2304 | case EQ_EXPR: val = (cmp == 0); break; | |
2305 | case NE_EXPR: val = (cmp != 0); break; | |
2306 | case LT_EXPR: val = (cmp < 0); break; | |
2307 | case GT_EXPR: val = (cmp > 0); break; | |
2308 | case LE_EXPR: val = (cmp <= 0); break; | |
2309 | case GE_EXPR: val = (cmp >= 0); break; | |
2310 | default: done = false; | |
2311 | } | |
2312 | if (done) | |
2313 | { | |
2314 | if (val) | |
2315 | return boolean_true_node; | |
2316 | else | |
2317 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2318 | } | |
2319 | } | |
2320 | /* Likewise if the second comparison is a != comparison. */ | |
2321 | else if (code2 == NE_EXPR) | |
2322 | { | |
2323 | bool done = true; | |
2324 | bool val; | |
2325 | switch (code1) | |
2326 | { | |
2327 | case EQ_EXPR: val = (cmp == 0); break; | |
2328 | case NE_EXPR: val = (cmp != 0); break; | |
2329 | case LT_EXPR: val = (cmp > 0); break; | |
2330 | case GT_EXPR: val = (cmp < 0); break; | |
2331 | case LE_EXPR: val = (cmp >= 0); break; | |
2332 | case GE_EXPR: val = (cmp <= 0); break; | |
2333 | default: done = false; | |
2334 | } | |
2335 | if (done) | |
2336 | { | |
2337 | if (val) | |
2338 | return boolean_true_node; | |
2339 | else | |
2340 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2341 | } | |
2342 | } | |
2343 | ||
2344 | /* See if an equality test is redundant with the other comparison. */ | |
2345 | else if (code1 == EQ_EXPR) | |
2346 | { | |
2347 | bool val; | |
2348 | switch (code2) | |
2349 | { | |
2350 | case EQ_EXPR: val = (cmp == 0); break; | |
2351 | case NE_EXPR: val = (cmp != 0); break; | |
2352 | case LT_EXPR: val = (cmp < 0); break; | |
2353 | case GT_EXPR: val = (cmp > 0); break; | |
2354 | case LE_EXPR: val = (cmp <= 0); break; | |
2355 | case GE_EXPR: val = (cmp >= 0); break; | |
2356 | default: | |
2357 | val = false; | |
2358 | } | |
2359 | if (val) | |
2360 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2361 | } | |
2362 | else if (code2 == EQ_EXPR) | |
2363 | { | |
2364 | bool val; | |
2365 | switch (code1) | |
2366 | { | |
2367 | case EQ_EXPR: val = (cmp == 0); break; | |
2368 | case NE_EXPR: val = (cmp != 0); break; | |
2369 | case LT_EXPR: val = (cmp > 0); break; | |
2370 | case GT_EXPR: val = (cmp < 0); break; | |
2371 | case LE_EXPR: val = (cmp >= 0); break; | |
2372 | case GE_EXPR: val = (cmp <= 0); break; | |
2373 | default: | |
2374 | val = false; | |
2375 | } | |
2376 | if (val) | |
2377 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2378 | } | |
2379 | ||
2380 | /* Chose the less restrictive of two < or <= comparisons. */ | |
2381 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
2382 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2383 | { | |
2384 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
2385 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2386 | else | |
2387 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2388 | } | |
2389 | ||
2390 | /* Likewise chose the less restrictive of two > or >= comparisons. */ | |
2391 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
2392 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2393 | { | |
2394 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
2395 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
2396 | else | |
2397 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
2398 | } | |
2399 | ||
2400 | /* Check for singleton ranges. */ | |
2401 | else if (cmp == 0 | |
2402 | && ((code1 == LT_EXPR && code2 == GT_EXPR) | |
2403 | || (code1 == GT_EXPR && code2 == LT_EXPR))) | |
2404 | return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b); | |
2405 | ||
2406 | /* Check for less/greater pairs that don't restrict the range at all. */ | |
2407 | else if (cmp >= 0 | |
2408 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
2409 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2410 | return boolean_true_node; | |
2411 | else if (cmp <= 0 | |
2412 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
2413 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2414 | return boolean_true_node; | |
2415 | } | |
2416 | ||
2417 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
2418 | NAME's definition is a truth value. See if there are any simplifications | |
2419 | that can be done against the NAME's definition. */ | |
2420 | if (TREE_CODE (op1a) == SSA_NAME | |
2421 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
2422 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
2423 | { | |
2424 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
2425 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
2426 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
2427 | switch (gimple_code (stmt)) | |
2428 | { | |
2429 | case GIMPLE_ASSIGN: | |
2430 | /* Try to simplify by copy-propagating the definition. */ | |
2431 | return or_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
2432 | ||
2433 | case GIMPLE_PHI: | |
2434 | /* If every argument to the PHI produces the same result when | |
2435 | ORed with the second comparison, we win. | |
2436 | Do not do this unless the type is bool since we need a bool | |
2437 | result here anyway. */ | |
2438 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
2439 | { | |
2440 | tree result = NULL_TREE; | |
2441 | unsigned i; | |
2442 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
2443 | { | |
2444 | tree arg = gimple_phi_arg_def (stmt, i); | |
2445 | ||
2446 | /* If this PHI has itself as an argument, ignore it. | |
2447 | If all the other args produce the same result, | |
2448 | we're still OK. */ | |
2449 | if (arg == gimple_phi_result (stmt)) | |
2450 | continue; | |
2451 | else if (TREE_CODE (arg) == INTEGER_CST) | |
2452 | { | |
2453 | if (invert ? integer_zerop (arg) : integer_nonzerop (arg)) | |
2454 | { | |
2455 | if (!result) | |
2456 | result = boolean_true_node; | |
2457 | else if (!integer_onep (result)) | |
2458 | return NULL_TREE; | |
2459 | } | |
2460 | else if (!result) | |
2461 | result = fold_build2 (code2, boolean_type_node, | |
2462 | op2a, op2b); | |
2463 | else if (!same_bool_comparison_p (result, | |
2464 | code2, op2a, op2b)) | |
2465 | return NULL_TREE; | |
2466 | } | |
0e8b84ec JJ |
2467 | else if (TREE_CODE (arg) == SSA_NAME |
2468 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
e89065a1 | 2469 | { |
6c66f733 JJ |
2470 | tree temp; |
2471 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
2472 | /* In simple cases we can look through PHI nodes, | |
2473 | but we have to be careful with loops. | |
2474 | See PR49073. */ | |
2475 | if (! dom_info_available_p (CDI_DOMINATORS) | |
2476 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
2477 | || dominated_by_p (CDI_DOMINATORS, | |
2478 | gimple_bb (def_stmt), | |
2479 | gimple_bb (stmt))) | |
2480 | return NULL_TREE; | |
2481 | temp = or_var_with_comparison (arg, invert, code2, | |
2482 | op2a, op2b); | |
e89065a1 SL |
2483 | if (!temp) |
2484 | return NULL_TREE; | |
2485 | else if (!result) | |
2486 | result = temp; | |
2487 | else if (!same_bool_result_p (result, temp)) | |
2488 | return NULL_TREE; | |
2489 | } | |
2490 | else | |
2491 | return NULL_TREE; | |
2492 | } | |
2493 | return result; | |
2494 | } | |
2495 | ||
2496 | default: | |
2497 | break; | |
2498 | } | |
2499 | } | |
2500 | return NULL_TREE; | |
2501 | } | |
2502 | ||
2503 | /* Try to simplify the OR of two comparisons, specified by | |
2504 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
2505 | If this can be simplified to a single expression (without requiring | |
2506 | introducing more SSA variables to hold intermediate values), | |
2507 | return the resulting tree. Otherwise return NULL_TREE. | |
2508 | If the result expression is non-null, it has boolean type. */ | |
2509 | ||
2510 | tree | |
2511 | maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
2512 | enum tree_code code2, tree op2a, tree op2b) | |
2513 | { | |
2514 | tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
2515 | if (t) | |
2516 | return t; | |
2517 | else | |
2518 | return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
2519 | } | |
cfef45c8 RG |
2520 | |
2521 | ||
2522 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
2523 | ||
2524 | Either NULL_TREE, a simplified but non-constant or a constant | |
2525 | is returned. | |
2526 | ||
2527 | ??? This should go into a gimple-fold-inline.h file to be eventually | |
2528 | privatized with the single valueize function used in the various TUs | |
2529 | to avoid the indirect function call overhead. */ | |
2530 | ||
2531 | tree | |
2532 | gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree)) | |
2533 | { | |
2534 | location_t loc = gimple_location (stmt); | |
2535 | switch (gimple_code (stmt)) | |
2536 | { | |
2537 | case GIMPLE_ASSIGN: | |
2538 | { | |
2539 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
2540 | ||
2541 | switch (get_gimple_rhs_class (subcode)) | |
2542 | { | |
2543 | case GIMPLE_SINGLE_RHS: | |
2544 | { | |
2545 | tree rhs = gimple_assign_rhs1 (stmt); | |
2546 | enum tree_code_class kind = TREE_CODE_CLASS (subcode); | |
2547 | ||
2548 | if (TREE_CODE (rhs) == SSA_NAME) | |
2549 | { | |
2550 | /* If the RHS is an SSA_NAME, return its known constant value, | |
2551 | if any. */ | |
2552 | return (*valueize) (rhs); | |
2553 | } | |
2554 | /* Handle propagating invariant addresses into address | |
2555 | operations. */ | |
2556 | else if (TREE_CODE (rhs) == ADDR_EXPR | |
2557 | && !is_gimple_min_invariant (rhs)) | |
2558 | { | |
d25c4172 | 2559 | HOST_WIDE_INT offset = 0; |
cfef45c8 RG |
2560 | tree base; |
2561 | base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0), | |
2562 | &offset, | |
2563 | valueize); | |
2564 | if (base | |
2565 | && (CONSTANT_CLASS_P (base) | |
2566 | || decl_address_invariant_p (base))) | |
2567 | return build_invariant_address (TREE_TYPE (rhs), | |
2568 | base, offset); | |
2569 | } | |
2570 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
2571 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
2572 | && (CONSTRUCTOR_NELTS (rhs) | |
2573 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
2574 | { | |
2575 | unsigned i; | |
d2a12ae7 | 2576 | tree val, *vec; |
cfef45c8 | 2577 | |
d2a12ae7 RG |
2578 | vec = XALLOCAVEC (tree, |
2579 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))); | |
cfef45c8 RG |
2580 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) |
2581 | { | |
2582 | val = (*valueize) (val); | |
2583 | if (TREE_CODE (val) == INTEGER_CST | |
2584 | || TREE_CODE (val) == REAL_CST | |
2585 | || TREE_CODE (val) == FIXED_CST) | |
d2a12ae7 | 2586 | vec[i] = val; |
cfef45c8 RG |
2587 | else |
2588 | return NULL_TREE; | |
2589 | } | |
2590 | ||
d2a12ae7 | 2591 | return build_vector (TREE_TYPE (rhs), vec); |
cfef45c8 | 2592 | } |
bdf37f7a JH |
2593 | if (subcode == OBJ_TYPE_REF) |
2594 | { | |
2595 | tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs)); | |
2596 | /* If callee is constant, we can fold away the wrapper. */ | |
2597 | if (is_gimple_min_invariant (val)) | |
2598 | return val; | |
2599 | } | |
cfef45c8 RG |
2600 | |
2601 | if (kind == tcc_reference) | |
2602 | { | |
2603 | if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR | |
2604 | || TREE_CODE (rhs) == REALPART_EXPR | |
2605 | || TREE_CODE (rhs) == IMAGPART_EXPR) | |
2606 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
2607 | { | |
2608 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
2609 | return fold_unary_loc (EXPR_LOCATION (rhs), | |
2610 | TREE_CODE (rhs), | |
2611 | TREE_TYPE (rhs), val); | |
2612 | } | |
2613 | else if (TREE_CODE (rhs) == BIT_FIELD_REF | |
2614 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
2615 | { | |
2616 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
2617 | return fold_ternary_loc (EXPR_LOCATION (rhs), | |
2618 | TREE_CODE (rhs), | |
2619 | TREE_TYPE (rhs), val, | |
2620 | TREE_OPERAND (rhs, 1), | |
2621 | TREE_OPERAND (rhs, 2)); | |
2622 | } | |
2623 | else if (TREE_CODE (rhs) == MEM_REF | |
2624 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
2625 | { | |
2626 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
2627 | if (TREE_CODE (val) == ADDR_EXPR | |
2628 | && is_gimple_min_invariant (val)) | |
2629 | { | |
2630 | tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs), | |
2631 | unshare_expr (val), | |
2632 | TREE_OPERAND (rhs, 1)); | |
2633 | if (tem) | |
2634 | rhs = tem; | |
2635 | } | |
2636 | } | |
2637 | return fold_const_aggregate_ref_1 (rhs, valueize); | |
2638 | } | |
2639 | else if (kind == tcc_declaration) | |
2640 | return get_symbol_constant_value (rhs); | |
2641 | return rhs; | |
2642 | } | |
2643 | ||
2644 | case GIMPLE_UNARY_RHS: | |
2645 | { | |
2646 | /* Handle unary operators that can appear in GIMPLE form. | |
2647 | Note that we know the single operand must be a constant, | |
2648 | so this should almost always return a simplified RHS. */ | |
cfef45c8 RG |
2649 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); |
2650 | ||
cfef45c8 RG |
2651 | return |
2652 | fold_unary_ignore_overflow_loc (loc, subcode, | |
2653 | gimple_expr_type (stmt), op0); | |
2654 | } | |
2655 | ||
2656 | case GIMPLE_BINARY_RHS: | |
2657 | { | |
2658 | /* Handle binary operators that can appear in GIMPLE form. */ | |
2659 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
2660 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
2661 | ||
2662 | /* Translate &x + CST into an invariant form suitable for | |
2663 | further propagation. */ | |
2664 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR | |
2665 | && TREE_CODE (op0) == ADDR_EXPR | |
2666 | && TREE_CODE (op1) == INTEGER_CST) | |
2667 | { | |
2668 | tree off = fold_convert (ptr_type_node, op1); | |
4d59a001 RG |
2669 | return build_fold_addr_expr_loc |
2670 | (loc, | |
2671 | fold_build2 (MEM_REF, | |
cfef45c8 RG |
2672 | TREE_TYPE (TREE_TYPE (op0)), |
2673 | unshare_expr (op0), off)); | |
2674 | } | |
2675 | ||
2676 | return fold_binary_loc (loc, subcode, | |
2677 | gimple_expr_type (stmt), op0, op1); | |
2678 | } | |
2679 | ||
2680 | case GIMPLE_TERNARY_RHS: | |
2681 | { | |
2682 | /* Handle ternary operators that can appear in GIMPLE form. */ | |
2683 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
2684 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
2685 | tree op2 = (*valueize) (gimple_assign_rhs3 (stmt)); | |
2686 | ||
d3878abf RG |
2687 | /* Fold embedded expressions in ternary codes. */ |
2688 | if ((subcode == COND_EXPR | |
2689 | || subcode == VEC_COND_EXPR) | |
2690 | && COMPARISON_CLASS_P (op0)) | |
2691 | { | |
2692 | tree op00 = (*valueize) (TREE_OPERAND (op0, 0)); | |
2693 | tree op01 = (*valueize) (TREE_OPERAND (op0, 1)); | |
2694 | tree tem = fold_binary_loc (loc, TREE_CODE (op0), | |
2695 | TREE_TYPE (op0), op00, op01); | |
2696 | if (tem) | |
2697 | op0 = tem; | |
2698 | } | |
2699 | ||
cfef45c8 RG |
2700 | return fold_ternary_loc (loc, subcode, |
2701 | gimple_expr_type (stmt), op0, op1, op2); | |
2702 | } | |
2703 | ||
2704 | default: | |
2705 | gcc_unreachable (); | |
2706 | } | |
2707 | } | |
2708 | ||
2709 | case GIMPLE_CALL: | |
2710 | { | |
25583c4f RS |
2711 | tree fn; |
2712 | ||
2713 | if (gimple_call_internal_p (stmt)) | |
31e071ae MP |
2714 | { |
2715 | enum tree_code subcode = ERROR_MARK; | |
2716 | switch (gimple_call_internal_fn (stmt)) | |
2717 | { | |
2718 | case IFN_UBSAN_CHECK_ADD: | |
2719 | subcode = PLUS_EXPR; | |
2720 | break; | |
2721 | case IFN_UBSAN_CHECK_SUB: | |
2722 | subcode = MINUS_EXPR; | |
2723 | break; | |
2724 | case IFN_UBSAN_CHECK_MUL: | |
2725 | subcode = MULT_EXPR; | |
2726 | break; | |
2727 | default: | |
2728 | return NULL_TREE; | |
2729 | } | |
368b454d JJ |
2730 | tree arg0 = gimple_call_arg (stmt, 0); |
2731 | tree arg1 = gimple_call_arg (stmt, 1); | |
2732 | tree op0 = (*valueize) (arg0); | |
2733 | tree op1 = (*valueize) (arg1); | |
31e071ae MP |
2734 | |
2735 | if (TREE_CODE (op0) != INTEGER_CST | |
2736 | || TREE_CODE (op1) != INTEGER_CST) | |
368b454d JJ |
2737 | { |
2738 | switch (subcode) | |
2739 | { | |
2740 | case MULT_EXPR: | |
2741 | /* x * 0 = 0 * x = 0 without overflow. */ | |
2742 | if (integer_zerop (op0) || integer_zerop (op1)) | |
2743 | return build_zero_cst (TREE_TYPE (arg0)); | |
2744 | break; | |
2745 | case MINUS_EXPR: | |
2746 | /* y - y = 0 without overflow. */ | |
2747 | if (operand_equal_p (op0, op1, 0)) | |
2748 | return build_zero_cst (TREE_TYPE (arg0)); | |
2749 | break; | |
2750 | default: | |
2751 | break; | |
2752 | } | |
2753 | } | |
2754 | tree res | |
2755 | = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1); | |
31e071ae MP |
2756 | if (res |
2757 | && TREE_CODE (res) == INTEGER_CST | |
2758 | && !TREE_OVERFLOW (res)) | |
2759 | return res; | |
2760 | return NULL_TREE; | |
2761 | } | |
25583c4f RS |
2762 | |
2763 | fn = (*valueize) (gimple_call_fn (stmt)); | |
cfef45c8 RG |
2764 | if (TREE_CODE (fn) == ADDR_EXPR |
2765 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
5c944c6c RB |
2766 | && DECL_BUILT_IN (TREE_OPERAND (fn, 0)) |
2767 | && gimple_builtin_call_types_compatible_p (stmt, | |
2768 | TREE_OPERAND (fn, 0))) | |
cfef45c8 RG |
2769 | { |
2770 | tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt)); | |
2771 | tree call, retval; | |
2772 | unsigned i; | |
2773 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
2774 | args[i] = (*valueize) (gimple_call_arg (stmt, i)); | |
2775 | call = build_call_array_loc (loc, | |
2776 | gimple_call_return_type (stmt), | |
2777 | fn, gimple_call_num_args (stmt), args); | |
2778 | retval = fold_call_expr (EXPR_LOCATION (call), call, false); | |
2779 | if (retval) | |
5c944c6c RB |
2780 | { |
2781 | /* fold_call_expr wraps the result inside a NOP_EXPR. */ | |
2782 | STRIP_NOPS (retval); | |
2783 | retval = fold_convert (gimple_call_return_type (stmt), retval); | |
2784 | } | |
cfef45c8 RG |
2785 | return retval; |
2786 | } | |
2787 | return NULL_TREE; | |
2788 | } | |
2789 | ||
2790 | default: | |
2791 | return NULL_TREE; | |
2792 | } | |
2793 | } | |
2794 | ||
2795 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
2796 | Returns NULL_TREE if folding to a constant is not possible, otherwise | |
2797 | returns a constant according to is_gimple_min_invariant. */ | |
2798 | ||
2799 | tree | |
2800 | gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree)) | |
2801 | { | |
2802 | tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize); | |
2803 | if (res && is_gimple_min_invariant (res)) | |
2804 | return res; | |
2805 | return NULL_TREE; | |
2806 | } | |
2807 | ||
2808 | ||
2809 | /* The following set of functions are supposed to fold references using | |
2810 | their constant initializers. */ | |
2811 | ||
2812 | static tree fold_ctor_reference (tree type, tree ctor, | |
2813 | unsigned HOST_WIDE_INT offset, | |
c44c2088 | 2814 | unsigned HOST_WIDE_INT size, tree); |
cfef45c8 RG |
2815 | |
2816 | /* See if we can find constructor defining value of BASE. | |
2817 | When we know the consructor with constant offset (such as | |
2818 | base is array[40] and we do know constructor of array), then | |
2819 | BIT_OFFSET is adjusted accordingly. | |
2820 | ||
2821 | As a special case, return error_mark_node when constructor | |
2822 | is not explicitly available, but it is known to be zero | |
2823 | such as 'static const int a;'. */ | |
2824 | static tree | |
2825 | get_base_constructor (tree base, HOST_WIDE_INT *bit_offset, | |
2826 | tree (*valueize)(tree)) | |
2827 | { | |
2828 | HOST_WIDE_INT bit_offset2, size, max_size; | |
2829 | if (TREE_CODE (base) == MEM_REF) | |
2830 | { | |
2831 | if (!integer_zerop (TREE_OPERAND (base, 1))) | |
2832 | { | |
9541ffee | 2833 | if (!tree_fits_shwi_p (TREE_OPERAND (base, 1))) |
cfef45c8 | 2834 | return NULL_TREE; |
807e902e | 2835 | *bit_offset += (mem_ref_offset (base).to_short_addr () |
cfef45c8 RG |
2836 | * BITS_PER_UNIT); |
2837 | } | |
2838 | ||
2839 | if (valueize | |
2840 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) | |
2841 | base = valueize (TREE_OPERAND (base, 0)); | |
2842 | if (!base || TREE_CODE (base) != ADDR_EXPR) | |
2843 | return NULL_TREE; | |
2844 | base = TREE_OPERAND (base, 0); | |
2845 | } | |
2846 | ||
2847 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
2848 | DECL_INITIAL. If BASE is a nested reference into another | |
2849 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
2850 | the inner reference. */ | |
2851 | switch (TREE_CODE (base)) | |
2852 | { | |
2853 | case VAR_DECL: | |
cfef45c8 | 2854 | case CONST_DECL: |
6a6dac52 JH |
2855 | { |
2856 | tree init = ctor_for_folding (base); | |
2857 | ||
688010ba | 2858 | /* Our semantic is exact opposite of ctor_for_folding; |
6a6dac52 JH |
2859 | NULL means unknown, while error_mark_node is 0. */ |
2860 | if (init == error_mark_node) | |
2861 | return NULL_TREE; | |
2862 | if (!init) | |
2863 | return error_mark_node; | |
2864 | return init; | |
2865 | } | |
cfef45c8 RG |
2866 | |
2867 | case ARRAY_REF: | |
2868 | case COMPONENT_REF: | |
2869 | base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size); | |
2870 | if (max_size == -1 || size != max_size) | |
2871 | return NULL_TREE; | |
2872 | *bit_offset += bit_offset2; | |
2873 | return get_base_constructor (base, bit_offset, valueize); | |
2874 | ||
2875 | case STRING_CST: | |
2876 | case CONSTRUCTOR: | |
2877 | return base; | |
2878 | ||
2879 | default: | |
2880 | return NULL_TREE; | |
2881 | } | |
2882 | } | |
2883 | ||
2884 | /* CTOR is STRING_CST. Fold reference of type TYPE and size SIZE | |
2885 | to the memory at bit OFFSET. | |
2886 | ||
2887 | We do only simple job of folding byte accesses. */ | |
2888 | ||
2889 | static tree | |
2890 | fold_string_cst_ctor_reference (tree type, tree ctor, | |
2891 | unsigned HOST_WIDE_INT offset, | |
2892 | unsigned HOST_WIDE_INT size) | |
2893 | { | |
2894 | if (INTEGRAL_TYPE_P (type) | |
2895 | && (TYPE_MODE (type) | |
2896 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) | |
2897 | && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) | |
2898 | == MODE_INT) | |
2899 | && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1 | |
2900 | && size == BITS_PER_UNIT | |
2901 | && !(offset % BITS_PER_UNIT)) | |
2902 | { | |
2903 | offset /= BITS_PER_UNIT; | |
2904 | if (offset < (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (ctor)) | |
2905 | return build_int_cst_type (type, (TREE_STRING_POINTER (ctor) | |
2906 | [offset])); | |
2907 | /* Folding | |
2908 | const char a[20]="hello"; | |
2909 | return a[10]; | |
2910 | ||
2911 | might lead to offset greater than string length. In this case we | |
2912 | know value is either initialized to 0 or out of bounds. Return 0 | |
2913 | in both cases. */ | |
2914 | return build_zero_cst (type); | |
2915 | } | |
2916 | return NULL_TREE; | |
2917 | } | |
2918 | ||
2919 | /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size | |
2920 | SIZE to the memory at bit OFFSET. */ | |
2921 | ||
2922 | static tree | |
2923 | fold_array_ctor_reference (tree type, tree ctor, | |
2924 | unsigned HOST_WIDE_INT offset, | |
c44c2088 JH |
2925 | unsigned HOST_WIDE_INT size, |
2926 | tree from_decl) | |
cfef45c8 RG |
2927 | { |
2928 | unsigned HOST_WIDE_INT cnt; | |
2929 | tree cfield, cval; | |
807e902e KZ |
2930 | offset_int low_bound; |
2931 | offset_int elt_size; | |
2932 | offset_int index, max_index; | |
2933 | offset_int access_index; | |
b48e22b2 | 2934 | tree domain_type = NULL_TREE, index_type = NULL_TREE; |
cfef45c8 RG |
2935 | HOST_WIDE_INT inner_offset; |
2936 | ||
2937 | /* Compute low bound and elt size. */ | |
eb8f1123 RG |
2938 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE) |
2939 | domain_type = TYPE_DOMAIN (TREE_TYPE (ctor)); | |
cfef45c8 RG |
2940 | if (domain_type && TYPE_MIN_VALUE (domain_type)) |
2941 | { | |
2942 | /* Static constructors for variably sized objects makes no sense. */ | |
2943 | gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST); | |
b48e22b2 | 2944 | index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type)); |
807e902e | 2945 | low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type)); |
cfef45c8 RG |
2946 | } |
2947 | else | |
807e902e | 2948 | low_bound = 0; |
cfef45c8 | 2949 | /* Static constructors for variably sized objects makes no sense. */ |
c3284718 | 2950 | gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) |
cfef45c8 | 2951 | == INTEGER_CST); |
807e902e | 2952 | elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))); |
cfef45c8 RG |
2953 | |
2954 | /* We can handle only constantly sized accesses that are known to not | |
2955 | be larger than size of array element. */ | |
2956 | if (!TYPE_SIZE_UNIT (type) | |
2957 | || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST | |
807e902e KZ |
2958 | || wi::lts_p (elt_size, wi::to_offset (TYPE_SIZE_UNIT (type))) |
2959 | || elt_size == 0) | |
cfef45c8 RG |
2960 | return NULL_TREE; |
2961 | ||
2962 | /* Compute the array index we look for. */ | |
807e902e KZ |
2963 | access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT), |
2964 | elt_size); | |
27bcd47c | 2965 | access_index += low_bound; |
b48e22b2 | 2966 | if (index_type) |
807e902e KZ |
2967 | access_index = wi::ext (access_index, TYPE_PRECISION (index_type), |
2968 | TYPE_SIGN (index_type)); | |
cfef45c8 RG |
2969 | |
2970 | /* And offset within the access. */ | |
27bcd47c | 2971 | inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT); |
cfef45c8 RG |
2972 | |
2973 | /* See if the array field is large enough to span whole access. We do not | |
2974 | care to fold accesses spanning multiple array indexes. */ | |
27bcd47c | 2975 | if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT) |
cfef45c8 RG |
2976 | return NULL_TREE; |
2977 | ||
807e902e | 2978 | index = low_bound - 1; |
b48e22b2 | 2979 | if (index_type) |
807e902e KZ |
2980 | index = wi::ext (index, TYPE_PRECISION (index_type), |
2981 | TYPE_SIGN (index_type)); | |
b48e22b2 | 2982 | |
cfef45c8 RG |
2983 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
2984 | { | |
2985 | /* Array constructor might explicitely set index, or specify range | |
2986 | or leave index NULL meaning that it is next index after previous | |
2987 | one. */ | |
2988 | if (cfield) | |
2989 | { | |
2990 | if (TREE_CODE (cfield) == INTEGER_CST) | |
807e902e | 2991 | max_index = index = wi::to_offset (cfield); |
cfef45c8 RG |
2992 | else |
2993 | { | |
2994 | gcc_assert (TREE_CODE (cfield) == RANGE_EXPR); | |
807e902e KZ |
2995 | index = wi::to_offset (TREE_OPERAND (cfield, 0)); |
2996 | max_index = wi::to_offset (TREE_OPERAND (cfield, 1)); | |
cfef45c8 RG |
2997 | } |
2998 | } | |
2999 | else | |
b48e22b2 | 3000 | { |
807e902e | 3001 | index += 1; |
b48e22b2 | 3002 | if (index_type) |
807e902e KZ |
3003 | index = wi::ext (index, TYPE_PRECISION (index_type), |
3004 | TYPE_SIGN (index_type)); | |
b48e22b2 EB |
3005 | max_index = index; |
3006 | } | |
cfef45c8 RG |
3007 | |
3008 | /* Do we have match? */ | |
807e902e KZ |
3009 | if (wi::cmpu (access_index, index) >= 0 |
3010 | && wi::cmpu (access_index, max_index) <= 0) | |
c44c2088 JH |
3011 | return fold_ctor_reference (type, cval, inner_offset, size, |
3012 | from_decl); | |
cfef45c8 RG |
3013 | } |
3014 | /* When memory is not explicitely mentioned in constructor, | |
3015 | it is 0 (or out of range). */ | |
3016 | return build_zero_cst (type); | |
3017 | } | |
3018 | ||
3019 | /* CTOR is CONSTRUCTOR of an aggregate or vector. | |
3020 | Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */ | |
3021 | ||
3022 | static tree | |
3023 | fold_nonarray_ctor_reference (tree type, tree ctor, | |
3024 | unsigned HOST_WIDE_INT offset, | |
c44c2088 JH |
3025 | unsigned HOST_WIDE_INT size, |
3026 | tree from_decl) | |
cfef45c8 RG |
3027 | { |
3028 | unsigned HOST_WIDE_INT cnt; | |
3029 | tree cfield, cval; | |
3030 | ||
3031 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, | |
3032 | cval) | |
3033 | { | |
3034 | tree byte_offset = DECL_FIELD_OFFSET (cfield); | |
3035 | tree field_offset = DECL_FIELD_BIT_OFFSET (cfield); | |
3036 | tree field_size = DECL_SIZE (cfield); | |
807e902e KZ |
3037 | offset_int bitoffset; |
3038 | offset_int bitoffset_end, access_end; | |
cfef45c8 RG |
3039 | |
3040 | /* Variable sized objects in static constructors makes no sense, | |
3041 | but field_size can be NULL for flexible array members. */ | |
3042 | gcc_assert (TREE_CODE (field_offset) == INTEGER_CST | |
3043 | && TREE_CODE (byte_offset) == INTEGER_CST | |
3044 | && (field_size != NULL_TREE | |
3045 | ? TREE_CODE (field_size) == INTEGER_CST | |
3046 | : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE)); | |
3047 | ||
3048 | /* Compute bit offset of the field. */ | |
807e902e KZ |
3049 | bitoffset = (wi::to_offset (field_offset) |
3050 | + wi::lshift (wi::to_offset (byte_offset), | |
3051 | LOG2_BITS_PER_UNIT)); | |
cfef45c8 RG |
3052 | /* Compute bit offset where the field ends. */ |
3053 | if (field_size != NULL_TREE) | |
807e902e | 3054 | bitoffset_end = bitoffset + wi::to_offset (field_size); |
cfef45c8 | 3055 | else |
807e902e | 3056 | bitoffset_end = 0; |
cfef45c8 | 3057 | |
807e902e | 3058 | access_end = offset_int (offset) + size; |
b8b2b009 JJ |
3059 | |
3060 | /* Is there any overlap between [OFFSET, OFFSET+SIZE) and | |
3061 | [BITOFFSET, BITOFFSET_END)? */ | |
807e902e | 3062 | if (wi::cmps (access_end, bitoffset) > 0 |
cfef45c8 | 3063 | && (field_size == NULL_TREE |
807e902e | 3064 | || wi::lts_p (offset, bitoffset_end))) |
cfef45c8 | 3065 | { |
807e902e | 3066 | offset_int inner_offset = offset_int (offset) - bitoffset; |
cfef45c8 RG |
3067 | /* We do have overlap. Now see if field is large enough to |
3068 | cover the access. Give up for accesses spanning multiple | |
3069 | fields. */ | |
807e902e | 3070 | if (wi::cmps (access_end, bitoffset_end) > 0) |
cfef45c8 | 3071 | return NULL_TREE; |
807e902e | 3072 | if (wi::lts_p (offset, bitoffset)) |
b8b2b009 | 3073 | return NULL_TREE; |
cfef45c8 | 3074 | return fold_ctor_reference (type, cval, |
27bcd47c | 3075 | inner_offset.to_uhwi (), size, |
c44c2088 | 3076 | from_decl); |
cfef45c8 RG |
3077 | } |
3078 | } | |
3079 | /* When memory is not explicitely mentioned in constructor, it is 0. */ | |
3080 | return build_zero_cst (type); | |
3081 | } | |
3082 | ||
3083 | /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE | |
3084 | to the memory at bit OFFSET. */ | |
3085 | ||
3086 | static tree | |
3087 | fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset, | |
c44c2088 | 3088 | unsigned HOST_WIDE_INT size, tree from_decl) |
cfef45c8 RG |
3089 | { |
3090 | tree ret; | |
3091 | ||
3092 | /* We found the field with exact match. */ | |
3093 | if (useless_type_conversion_p (type, TREE_TYPE (ctor)) | |
3094 | && !offset) | |
9d60be38 | 3095 | return canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
cfef45c8 RG |
3096 | |
3097 | /* We are at the end of walk, see if we can view convert the | |
3098 | result. */ | |
3099 | if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset | |
3100 | /* VIEW_CONVERT_EXPR is defined only for matching sizes. */ | |
3101 | && operand_equal_p (TYPE_SIZE (type), | |
3102 | TYPE_SIZE (TREE_TYPE (ctor)), 0)) | |
3103 | { | |
9d60be38 | 3104 | ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
cfef45c8 RG |
3105 | ret = fold_unary (VIEW_CONVERT_EXPR, type, ret); |
3106 | if (ret) | |
3107 | STRIP_NOPS (ret); | |
3108 | return ret; | |
3109 | } | |
3110 | if (TREE_CODE (ctor) == STRING_CST) | |
3111 | return fold_string_cst_ctor_reference (type, ctor, offset, size); | |
3112 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
3113 | { | |
3114 | ||
eb8f1123 RG |
3115 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE |
3116 | || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE) | |
c44c2088 JH |
3117 | return fold_array_ctor_reference (type, ctor, offset, size, |
3118 | from_decl); | |
cfef45c8 | 3119 | else |
c44c2088 JH |
3120 | return fold_nonarray_ctor_reference (type, ctor, offset, size, |
3121 | from_decl); | |
cfef45c8 RG |
3122 | } |
3123 | ||
3124 | return NULL_TREE; | |
3125 | } | |
3126 | ||
3127 | /* Return the tree representing the element referenced by T if T is an | |
3128 | ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA | |
3129 | names using VALUEIZE. Return NULL_TREE otherwise. */ | |
3130 | ||
3131 | tree | |
3132 | fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree)) | |
3133 | { | |
3134 | tree ctor, idx, base; | |
3135 | HOST_WIDE_INT offset, size, max_size; | |
3136 | tree tem; | |
3137 | ||
f8a7df45 RG |
3138 | if (TREE_THIS_VOLATILE (t)) |
3139 | return NULL_TREE; | |
3140 | ||
cfef45c8 RG |
3141 | if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration) |
3142 | return get_symbol_constant_value (t); | |
3143 | ||
3144 | tem = fold_read_from_constant_string (t); | |
3145 | if (tem) | |
3146 | return tem; | |
3147 | ||
3148 | switch (TREE_CODE (t)) | |
3149 | { | |
3150 | case ARRAY_REF: | |
3151 | case ARRAY_RANGE_REF: | |
3152 | /* Constant indexes are handled well by get_base_constructor. | |
3153 | Only special case variable offsets. | |
3154 | FIXME: This code can't handle nested references with variable indexes | |
3155 | (they will be handled only by iteration of ccp). Perhaps we can bring | |
3156 | get_ref_base_and_extent here and make it use a valueize callback. */ | |
3157 | if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME | |
3158 | && valueize | |
3159 | && (idx = (*valueize) (TREE_OPERAND (t, 1))) | |
b48e22b2 | 3160 | && TREE_CODE (idx) == INTEGER_CST) |
cfef45c8 RG |
3161 | { |
3162 | tree low_bound, unit_size; | |
3163 | ||
3164 | /* If the resulting bit-offset is constant, track it. */ | |
3165 | if ((low_bound = array_ref_low_bound (t), | |
b48e22b2 | 3166 | TREE_CODE (low_bound) == INTEGER_CST) |
cfef45c8 | 3167 | && (unit_size = array_ref_element_size (t), |
807e902e | 3168 | tree_fits_uhwi_p (unit_size))) |
cfef45c8 | 3169 | { |
807e902e KZ |
3170 | offset_int woffset |
3171 | = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound), | |
3172 | TYPE_PRECISION (TREE_TYPE (idx))); | |
3173 | ||
3174 | if (wi::fits_shwi_p (woffset)) | |
3175 | { | |
3176 | offset = woffset.to_shwi (); | |
3177 | /* TODO: This code seems wrong, multiply then check | |
3178 | to see if it fits. */ | |
3179 | offset *= tree_to_uhwi (unit_size); | |
3180 | offset *= BITS_PER_UNIT; | |
3181 | ||
3182 | base = TREE_OPERAND (t, 0); | |
3183 | ctor = get_base_constructor (base, &offset, valueize); | |
3184 | /* Empty constructor. Always fold to 0. */ | |
3185 | if (ctor == error_mark_node) | |
3186 | return build_zero_cst (TREE_TYPE (t)); | |
3187 | /* Out of bound array access. Value is undefined, | |
3188 | but don't fold. */ | |
3189 | if (offset < 0) | |
3190 | return NULL_TREE; | |
3191 | /* We can not determine ctor. */ | |
3192 | if (!ctor) | |
3193 | return NULL_TREE; | |
3194 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, | |
3195 | tree_to_uhwi (unit_size) | |
3196 | * BITS_PER_UNIT, | |
3197 | base); | |
3198 | } | |
cfef45c8 RG |
3199 | } |
3200 | } | |
3201 | /* Fallthru. */ | |
3202 | ||
3203 | case COMPONENT_REF: | |
3204 | case BIT_FIELD_REF: | |
3205 | case TARGET_MEM_REF: | |
3206 | case MEM_REF: | |
3207 | base = get_ref_base_and_extent (t, &offset, &size, &max_size); | |
3208 | ctor = get_base_constructor (base, &offset, valueize); | |
3209 | ||
3210 | /* Empty constructor. Always fold to 0. */ | |
3211 | if (ctor == error_mark_node) | |
3212 | return build_zero_cst (TREE_TYPE (t)); | |
3213 | /* We do not know precise address. */ | |
3214 | if (max_size == -1 || max_size != size) | |
3215 | return NULL_TREE; | |
3216 | /* We can not determine ctor. */ | |
3217 | if (!ctor) | |
3218 | return NULL_TREE; | |
3219 | ||
3220 | /* Out of bound array access. Value is undefined, but don't fold. */ | |
3221 | if (offset < 0) | |
3222 | return NULL_TREE; | |
3223 | ||
c44c2088 JH |
3224 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, |
3225 | base); | |
cfef45c8 RG |
3226 | |
3227 | case REALPART_EXPR: | |
3228 | case IMAGPART_EXPR: | |
3229 | { | |
3230 | tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize); | |
3231 | if (c && TREE_CODE (c) == COMPLEX_CST) | |
3232 | return fold_build1_loc (EXPR_LOCATION (t), | |
3233 | TREE_CODE (t), TREE_TYPE (t), c); | |
3234 | break; | |
3235 | } | |
3236 | ||
3237 | default: | |
3238 | break; | |
3239 | } | |
3240 | ||
3241 | return NULL_TREE; | |
3242 | } | |
3243 | ||
3244 | tree | |
3245 | fold_const_aggregate_ref (tree t) | |
3246 | { | |
3247 | return fold_const_aggregate_ref_1 (t, NULL); | |
3248 | } | |
06bc3ec7 | 3249 | |
85942f45 | 3250 | /* Lookup virtual method with index TOKEN in a virtual table V |
ec77d61f JH |
3251 | at OFFSET. |
3252 | Set CAN_REFER if non-NULL to false if method | |
3253 | is not referable or if the virtual table is ill-formed (such as rewriten | |
3254 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
81fa35bd MJ |
3255 | |
3256 | tree | |
85942f45 JH |
3257 | gimple_get_virt_method_for_vtable (HOST_WIDE_INT token, |
3258 | tree v, | |
ec77d61f JH |
3259 | unsigned HOST_WIDE_INT offset, |
3260 | bool *can_refer) | |
81fa35bd | 3261 | { |
85942f45 JH |
3262 | tree vtable = v, init, fn; |
3263 | unsigned HOST_WIDE_INT size; | |
8c311b50 JH |
3264 | unsigned HOST_WIDE_INT elt_size, access_index; |
3265 | tree domain_type; | |
81fa35bd | 3266 | |
ec77d61f JH |
3267 | if (can_refer) |
3268 | *can_refer = true; | |
3269 | ||
9de2f554 | 3270 | /* First of all double check we have virtual table. */ |
81fa35bd | 3271 | if (TREE_CODE (v) != VAR_DECL |
2aa3da06 | 3272 | || !DECL_VIRTUAL_P (v)) |
ec77d61f JH |
3273 | { |
3274 | gcc_assert (in_lto_p); | |
3275 | /* Pass down that we lost track of the target. */ | |
3276 | if (can_refer) | |
3277 | *can_refer = false; | |
3278 | return NULL_TREE; | |
3279 | } | |
9de2f554 | 3280 | |
2aa3da06 JH |
3281 | init = ctor_for_folding (v); |
3282 | ||
9de2f554 | 3283 | /* The virtual tables should always be born with constructors |
2aa3da06 JH |
3284 | and we always should assume that they are avaialble for |
3285 | folding. At the moment we do not stream them in all cases, | |
3286 | but it should never happen that ctor seem unreachable. */ | |
3287 | gcc_assert (init); | |
3288 | if (init == error_mark_node) | |
3289 | { | |
3290 | gcc_assert (in_lto_p); | |
ec77d61f JH |
3291 | /* Pass down that we lost track of the target. */ |
3292 | if (can_refer) | |
3293 | *can_refer = false; | |
2aa3da06 JH |
3294 | return NULL_TREE; |
3295 | } | |
81fa35bd | 3296 | gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE); |
ae7e9ddd | 3297 | size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v)))); |
85942f45 | 3298 | offset *= BITS_PER_UNIT; |
81fa35bd | 3299 | offset += token * size; |
9de2f554 | 3300 | |
8c311b50 JH |
3301 | /* Lookup the value in the constructor that is assumed to be array. |
3302 | This is equivalent to | |
3303 | fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init, | |
3304 | offset, size, NULL); | |
3305 | but in a constant time. We expect that frontend produced a simple | |
3306 | array without indexed initializers. */ | |
3307 | ||
3308 | gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE); | |
3309 | domain_type = TYPE_DOMAIN (TREE_TYPE (init)); | |
3310 | gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type))); | |
3311 | elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init)))); | |
3312 | ||
3313 | access_index = offset / BITS_PER_UNIT / elt_size; | |
3314 | gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0); | |
3315 | ||
3316 | /* This code makes an assumption that there are no | |
3317 | indexed fileds produced by C++ FE, so we can directly index the array. */ | |
3318 | if (access_index < CONSTRUCTOR_NELTS (init)) | |
3319 | { | |
3320 | fn = CONSTRUCTOR_ELT (init, access_index)->value; | |
3321 | gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index); | |
3322 | STRIP_NOPS (fn); | |
3323 | } | |
3324 | else | |
3325 | fn = NULL; | |
9de2f554 JH |
3326 | |
3327 | /* For type inconsistent program we may end up looking up virtual method | |
3328 | in virtual table that does not contain TOKEN entries. We may overrun | |
3329 | the virtual table and pick up a constant or RTTI info pointer. | |
3330 | In any case the call is undefined. */ | |
3331 | if (!fn | |
3332 | || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR) | |
3333 | || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL) | |
3334 | fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
3335 | else | |
3336 | { | |
3337 | fn = TREE_OPERAND (fn, 0); | |
3338 | ||
3339 | /* When cgraph node is missing and function is not public, we cannot | |
3340 | devirtualize. This can happen in WHOPR when the actual method | |
3341 | ends up in other partition, because we found devirtualization | |
3342 | possibility too late. */ | |
3343 | if (!can_refer_decl_in_current_unit_p (fn, vtable)) | |
ec77d61f JH |
3344 | { |
3345 | if (can_refer) | |
3346 | { | |
3347 | *can_refer = false; | |
3348 | return fn; | |
3349 | } | |
3350 | return NULL_TREE; | |
3351 | } | |
9de2f554 | 3352 | } |
81fa35bd | 3353 | |
7501ca28 RG |
3354 | /* Make sure we create a cgraph node for functions we'll reference. |
3355 | They can be non-existent if the reference comes from an entry | |
3356 | of an external vtable for example. */ | |
3357 | cgraph_get_create_node (fn); | |
3358 | ||
81fa35bd MJ |
3359 | return fn; |
3360 | } | |
3361 | ||
85942f45 JH |
3362 | /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN |
3363 | is integer form of OBJ_TYPE_REF_TOKEN of the reference expression. | |
3364 | KNOWN_BINFO carries the binfo describing the true type of | |
ec77d61f JH |
3365 | OBJ_TYPE_REF_OBJECT(REF). |
3366 | Set CAN_REFER if non-NULL to false if method | |
3367 | is not referable or if the virtual table is ill-formed (such as rewriten | |
3368 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
85942f45 JH |
3369 | |
3370 | tree | |
ec77d61f JH |
3371 | gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo, |
3372 | bool *can_refer) | |
85942f45 JH |
3373 | { |
3374 | unsigned HOST_WIDE_INT offset; | |
3375 | tree v; | |
3376 | ||
3377 | v = BINFO_VTABLE (known_binfo); | |
3378 | /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */ | |
3379 | if (!v) | |
3380 | return NULL_TREE; | |
3381 | ||
3382 | if (!vtable_pointer_value_to_vtable (v, &v, &offset)) | |
ec77d61f JH |
3383 | { |
3384 | if (can_refer) | |
3385 | *can_refer = false; | |
3386 | return NULL_TREE; | |
3387 | } | |
3388 | return gimple_get_virt_method_for_vtable (token, v, offset, can_refer); | |
85942f45 JH |
3389 | } |
3390 | ||
06bc3ec7 BS |
3391 | /* Return true iff VAL is a gimple expression that is known to be |
3392 | non-negative. Restricted to floating-point inputs. */ | |
3393 | ||
3394 | bool | |
3395 | gimple_val_nonnegative_real_p (tree val) | |
3396 | { | |
3397 | gimple def_stmt; | |
3398 | ||
3399 | gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))); | |
3400 | ||
3401 | /* Use existing logic for non-gimple trees. */ | |
3402 | if (tree_expr_nonnegative_p (val)) | |
3403 | return true; | |
3404 | ||
3405 | if (TREE_CODE (val) != SSA_NAME) | |
3406 | return false; | |
3407 | ||
3408 | /* Currently we look only at the immediately defining statement | |
3409 | to make this determination, since recursion on defining | |
3410 | statements of operands can lead to quadratic behavior in the | |
3411 | worst case. This is expected to catch almost all occurrences | |
3412 | in practice. It would be possible to implement limited-depth | |
3413 | recursion if important cases are lost. Alternatively, passes | |
3414 | that need this information (such as the pow/powi lowering code | |
3415 | in the cse_sincos pass) could be revised to provide it through | |
3416 | dataflow propagation. */ | |
3417 | ||
3418 | def_stmt = SSA_NAME_DEF_STMT (val); | |
3419 | ||
3420 | if (is_gimple_assign (def_stmt)) | |
3421 | { | |
3422 | tree op0, op1; | |
3423 | ||
3424 | /* See fold-const.c:tree_expr_nonnegative_p for additional | |
3425 | cases that could be handled with recursion. */ | |
3426 | ||
3427 | switch (gimple_assign_rhs_code (def_stmt)) | |
3428 | { | |
3429 | case ABS_EXPR: | |
3430 | /* Always true for floating-point operands. */ | |
3431 | return true; | |
3432 | ||
3433 | case MULT_EXPR: | |
3434 | /* True if the two operands are identical (since we are | |
3435 | restricted to floating-point inputs). */ | |
3436 | op0 = gimple_assign_rhs1 (def_stmt); | |
3437 | op1 = gimple_assign_rhs2 (def_stmt); | |
3438 | ||
3439 | if (op0 == op1 | |
3440 | || operand_equal_p (op0, op1, 0)) | |
3441 | return true; | |
3442 | ||
3443 | default: | |
3444 | return false; | |
3445 | } | |
3446 | } | |
3447 | else if (is_gimple_call (def_stmt)) | |
3448 | { | |
3449 | tree fndecl = gimple_call_fndecl (def_stmt); | |
3450 | if (fndecl | |
3451 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
3452 | { | |
3453 | tree arg1; | |
3454 | ||
3455 | switch (DECL_FUNCTION_CODE (fndecl)) | |
3456 | { | |
3457 | CASE_FLT_FN (BUILT_IN_ACOS): | |
3458 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
3459 | CASE_FLT_FN (BUILT_IN_CABS): | |
3460 | CASE_FLT_FN (BUILT_IN_COSH): | |
3461 | CASE_FLT_FN (BUILT_IN_ERFC): | |
3462 | CASE_FLT_FN (BUILT_IN_EXP): | |
3463 | CASE_FLT_FN (BUILT_IN_EXP10): | |
3464 | CASE_FLT_FN (BUILT_IN_EXP2): | |
3465 | CASE_FLT_FN (BUILT_IN_FABS): | |
3466 | CASE_FLT_FN (BUILT_IN_FDIM): | |
3467 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
3468 | CASE_FLT_FN (BUILT_IN_POW10): | |
3469 | return true; | |
3470 | ||
3471 | CASE_FLT_FN (BUILT_IN_SQRT): | |
3472 | /* sqrt(-0.0) is -0.0, and sqrt is not defined over other | |
3473 | nonnegative inputs. */ | |
3474 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val)))) | |
3475 | return true; | |
3476 | ||
3477 | break; | |
3478 | ||
3479 | CASE_FLT_FN (BUILT_IN_POWI): | |
3480 | /* True if the second argument is an even integer. */ | |
3481 | arg1 = gimple_call_arg (def_stmt, 1); | |
3482 | ||
3483 | if (TREE_CODE (arg1) == INTEGER_CST | |
3484 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) | |
3485 | return true; | |
3486 | ||
3487 | break; | |
3488 | ||
3489 | CASE_FLT_FN (BUILT_IN_POW): | |
3490 | /* True if the second argument is an even integer-valued | |
3491 | real. */ | |
3492 | arg1 = gimple_call_arg (def_stmt, 1); | |
3493 | ||
3494 | if (TREE_CODE (arg1) == REAL_CST) | |
3495 | { | |
3496 | REAL_VALUE_TYPE c; | |
3497 | HOST_WIDE_INT n; | |
3498 | ||
3499 | c = TREE_REAL_CST (arg1); | |
3500 | n = real_to_integer (&c); | |
3501 | ||
3502 | if ((n & 1) == 0) | |
3503 | { | |
3504 | REAL_VALUE_TYPE cint; | |
807e902e | 3505 | real_from_integer (&cint, VOIDmode, n, SIGNED); |
06bc3ec7 BS |
3506 | if (real_identical (&c, &cint)) |
3507 | return true; | |
3508 | } | |
3509 | } | |
3510 | ||
3511 | break; | |
3512 | ||
3513 | default: | |
3514 | return false; | |
3515 | } | |
3516 | } | |
3517 | } | |
3518 | ||
3519 | return false; | |
3520 | } | |
b184c8f1 AM |
3521 | |
3522 | /* Given a pointer value OP0, return a simplified version of an | |
3523 | indirection through OP0, or NULL_TREE if no simplification is | |
3524 | possible. Note that the resulting type may be different from | |
3525 | the type pointed to in the sense that it is still compatible | |
3526 | from the langhooks point of view. */ | |
3527 | ||
3528 | tree | |
3529 | gimple_fold_indirect_ref (tree t) | |
3530 | { | |
3531 | tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype); | |
3532 | tree sub = t; | |
3533 | tree subtype; | |
3534 | ||
3535 | STRIP_NOPS (sub); | |
3536 | subtype = TREE_TYPE (sub); | |
3537 | if (!POINTER_TYPE_P (subtype)) | |
3538 | return NULL_TREE; | |
3539 | ||
3540 | if (TREE_CODE (sub) == ADDR_EXPR) | |
3541 | { | |
3542 | tree op = TREE_OPERAND (sub, 0); | |
3543 | tree optype = TREE_TYPE (op); | |
3544 | /* *&p => p */ | |
3545 | if (useless_type_conversion_p (type, optype)) | |
3546 | return op; | |
3547 | ||
3548 | /* *(foo *)&fooarray => fooarray[0] */ | |
3549 | if (TREE_CODE (optype) == ARRAY_TYPE | |
3550 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST | |
3551 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
3552 | { | |
3553 | tree type_domain = TYPE_DOMAIN (optype); | |
3554 | tree min_val = size_zero_node; | |
3555 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3556 | min_val = TYPE_MIN_VALUE (type_domain); | |
3557 | if (TREE_CODE (min_val) == INTEGER_CST) | |
3558 | return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE); | |
3559 | } | |
3560 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
3561 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
3562 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
3563 | return fold_build1 (REALPART_EXPR, type, op); | |
3564 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
3565 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
3566 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
3567 | { | |
3568 | tree part_width = TYPE_SIZE (type); | |
3569 | tree index = bitsize_int (0); | |
3570 | return fold_build3 (BIT_FIELD_REF, type, op, part_width, index); | |
3571 | } | |
3572 | } | |
3573 | ||
3574 | /* *(p + CST) -> ... */ | |
3575 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
3576 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
3577 | { | |
3578 | tree addr = TREE_OPERAND (sub, 0); | |
3579 | tree off = TREE_OPERAND (sub, 1); | |
3580 | tree addrtype; | |
3581 | ||
3582 | STRIP_NOPS (addr); | |
3583 | addrtype = TREE_TYPE (addr); | |
3584 | ||
3585 | /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */ | |
3586 | if (TREE_CODE (addr) == ADDR_EXPR | |
3587 | && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE | |
3588 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))) | |
cc269bb6 | 3589 | && tree_fits_uhwi_p (off)) |
b184c8f1 | 3590 | { |
ae7e9ddd | 3591 | unsigned HOST_WIDE_INT offset = tree_to_uhwi (off); |
b184c8f1 AM |
3592 | tree part_width = TYPE_SIZE (type); |
3593 | unsigned HOST_WIDE_INT part_widthi | |
9439e9a1 | 3594 | = tree_to_shwi (part_width) / BITS_PER_UNIT; |
b184c8f1 AM |
3595 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; |
3596 | tree index = bitsize_int (indexi); | |
3597 | if (offset / part_widthi | |
e934916c | 3598 | < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))) |
b184c8f1 AM |
3599 | return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0), |
3600 | part_width, index); | |
3601 | } | |
3602 | ||
3603 | /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */ | |
3604 | if (TREE_CODE (addr) == ADDR_EXPR | |
3605 | && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE | |
3606 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))) | |
3607 | { | |
3608 | tree size = TYPE_SIZE_UNIT (type); | |
3609 | if (tree_int_cst_equal (size, off)) | |
3610 | return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0)); | |
3611 | } | |
3612 | ||
3613 | /* *(p + CST) -> MEM_REF <p, CST>. */ | |
3614 | if (TREE_CODE (addr) != ADDR_EXPR | |
3615 | || DECL_P (TREE_OPERAND (addr, 0))) | |
3616 | return fold_build2 (MEM_REF, type, | |
3617 | addr, | |
807e902e | 3618 | wide_int_to_tree (ptype, off)); |
b184c8f1 AM |
3619 | } |
3620 | ||
3621 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
3622 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
3623 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST | |
3624 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype)))) | |
3625 | { | |
3626 | tree type_domain; | |
3627 | tree min_val = size_zero_node; | |
3628 | tree osub = sub; | |
3629 | sub = gimple_fold_indirect_ref (sub); | |
3630 | if (! sub) | |
3631 | sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub); | |
3632 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
3633 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3634 | min_val = TYPE_MIN_VALUE (type_domain); | |
3635 | if (TREE_CODE (min_val) == INTEGER_CST) | |
3636 | return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE); | |
3637 | } | |
3638 | ||
3639 | return NULL_TREE; | |
3640 | } | |
19e51b40 JJ |
3641 | |
3642 | /* Return true if CODE is an operation that when operating on signed | |
3643 | integer types involves undefined behavior on overflow and the | |
3644 | operation can be expressed with unsigned arithmetic. */ | |
3645 | ||
3646 | bool | |
3647 | arith_code_with_undefined_signed_overflow (tree_code code) | |
3648 | { | |
3649 | switch (code) | |
3650 | { | |
3651 | case PLUS_EXPR: | |
3652 | case MINUS_EXPR: | |
3653 | case MULT_EXPR: | |
3654 | case NEGATE_EXPR: | |
3655 | case POINTER_PLUS_EXPR: | |
3656 | return true; | |
3657 | default: | |
3658 | return false; | |
3659 | } | |
3660 | } | |
3661 | ||
3662 | /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic | |
3663 | operation that can be transformed to unsigned arithmetic by converting | |
3664 | its operand, carrying out the operation in the corresponding unsigned | |
3665 | type and converting the result back to the original type. | |
3666 | ||
3667 | Returns a sequence of statements that replace STMT and also contain | |
3668 | a modified form of STMT itself. */ | |
3669 | ||
3670 | gimple_seq | |
3671 | rewrite_to_defined_overflow (gimple stmt) | |
3672 | { | |
3673 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3674 | { | |
3675 | fprintf (dump_file, "rewriting stmt with undefined signed " | |
3676 | "overflow "); | |
3677 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
3678 | } | |
3679 | ||
3680 | tree lhs = gimple_assign_lhs (stmt); | |
3681 | tree type = unsigned_type_for (TREE_TYPE (lhs)); | |
3682 | gimple_seq stmts = NULL; | |
3683 | for (unsigned i = 1; i < gimple_num_ops (stmt); ++i) | |
3684 | { | |
3685 | gimple_seq stmts2 = NULL; | |
3686 | gimple_set_op (stmt, i, | |
3687 | force_gimple_operand (fold_convert (type, | |
3688 | gimple_op (stmt, i)), | |
3689 | &stmts2, true, NULL_TREE)); | |
3690 | gimple_seq_add_seq (&stmts, stmts2); | |
3691 | } | |
3692 | gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt)); | |
3693 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) | |
3694 | gimple_assign_set_rhs_code (stmt, PLUS_EXPR); | |
3695 | gimple_seq_add_stmt (&stmts, stmt); | |
3696 | gimple cvt = gimple_build_assign_with_ops | |
3697 | (NOP_EXPR, lhs, gimple_assign_lhs (stmt), NULL_TREE); | |
3698 | gimple_seq_add_stmt (&stmts, cvt); | |
3699 | ||
3700 | return stmts; | |
3701 | } |