]>
Commit | Line | Data |
---|---|---|
cbdd87d4 | 1 | /* Statement simplification on GIMPLE. |
5624e564 | 2 | Copyright (C) 2010-2015 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" | |
c7131fb2 | 24 | #include "backend.h" |
9fdcd34e | 25 | #include "predict.h" |
cbdd87d4 | 26 | #include "tree.h" |
c7131fb2 | 27 | #include "gimple.h" |
36566b39 | 28 | #include "rtl.h" |
c7131fb2 AM |
29 | #include "ssa.h" |
30 | #include "alias.h" | |
31 | #include "fold-const.h" | |
36566b39 | 32 | #include "flags.h" |
36566b39 PK |
33 | #include "insn-config.h" |
34 | #include "expmed.h" | |
35 | #include "dojump.h" | |
36 | #include "explow.h" | |
37 | #include "calls.h" | |
38 | #include "emit-rtl.h" | |
39 | #include "varasm.h" | |
40 | #include "stmt.h" | |
41 | #include "expr.h" | |
42 | #include "stor-layout.h" | |
7ee2468b | 43 | #include "dumpfile.h" |
60393bbc | 44 | #include "dominance.h" |
2fb9a547 AM |
45 | #include "internal-fn.h" |
46 | #include "gimple-fold.h" | |
45b0be94 | 47 | #include "gimplify.h" |
5be5c238 | 48 | #include "gimple-iterator.h" |
442b4905 AM |
49 | #include "tree-into-ssa.h" |
50 | #include "tree-dfa.h" | |
7a300452 | 51 | #include "tree-ssa.h" |
cbdd87d4 | 52 | #include "tree-ssa-propagate.h" |
cbdd87d4 | 53 | #include "target.h" |
c582198b | 54 | #include "cgraph.h" |
450ad0cd JH |
55 | #include "ipa-utils.h" |
56 | #include "gimple-pretty-print.h" | |
4484a35a | 57 | #include "tree-ssa-address.h" |
862d0b35 | 58 | #include "langhooks.h" |
19e51b40 | 59 | #include "gimplify-me.h" |
2b5f0895 | 60 | #include "dbgcnt.h" |
9b2b7279 | 61 | #include "builtins.h" |
fef5a0d9 | 62 | #include "output.h" |
e0ee10ed RB |
63 | #include "tree-eh.h" |
64 | #include "gimple-match.h" | |
cbdd87d4 | 65 | |
b3b9f3d0 | 66 | /* Return true when DECL can be referenced from current unit. |
c44c2088 JH |
67 | FROM_DECL (if non-null) specify constructor of variable DECL was taken from. |
68 | We can get declarations that are not possible to reference for various | |
69 | reasons: | |
1389294c | 70 | |
1389294c JH |
71 | 1) When analyzing C++ virtual tables. |
72 | C++ virtual tables do have known constructors even | |
73 | when they are keyed to other compilation unit. | |
74 | Those tables can contain pointers to methods and vars | |
75 | in other units. Those methods have both STATIC and EXTERNAL | |
76 | set. | |
77 | 2) In WHOPR mode devirtualization might lead to reference | |
78 | to method that was partitioned elsehwere. | |
79 | In this case we have static VAR_DECL or FUNCTION_DECL | |
80 | that has no corresponding callgraph/varpool node | |
b3b9f3d0 JH |
81 | declaring the body. |
82 | 3) COMDAT functions referred by external vtables that | |
3e89949e | 83 | we devirtualize only during final compilation stage. |
b3b9f3d0 JH |
84 | At this time we already decided that we will not output |
85 | the function body and thus we can't reference the symbol | |
86 | directly. */ | |
87 | ||
1389294c | 88 | static bool |
c44c2088 | 89 | can_refer_decl_in_current_unit_p (tree decl, tree from_decl) |
1389294c | 90 | { |
2c8326a5 | 91 | varpool_node *vnode; |
1389294c | 92 | struct cgraph_node *node; |
5e20cdc9 | 93 | symtab_node *snode; |
c44c2088 | 94 | |
00de328a | 95 | if (DECL_ABSTRACT_P (decl)) |
1632a686 JH |
96 | return false; |
97 | ||
98 | /* We are concerned only about static/external vars and functions. */ | |
99 | if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) | |
100 | || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)) | |
101 | return true; | |
102 | ||
103 | /* Static objects can be referred only if they was not optimized out yet. */ | |
104 | if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) | |
105 | { | |
3aaf0529 JH |
106 | /* Before we start optimizing unreachable code we can be sure all |
107 | static objects are defined. */ | |
3dafb85c | 108 | if (symtab->function_flags_ready) |
3aaf0529 | 109 | return true; |
d52f5295 | 110 | snode = symtab_node::get (decl); |
3aaf0529 | 111 | if (!snode || !snode->definition) |
1632a686 | 112 | return false; |
7de90a6c | 113 | node = dyn_cast <cgraph_node *> (snode); |
1632a686 JH |
114 | return !node || !node->global.inlined_to; |
115 | } | |
116 | ||
6da8be89 | 117 | /* We will later output the initializer, so we can refer to it. |
c44c2088 | 118 | So we are concerned only when DECL comes from initializer of |
3aaf0529 | 119 | external var or var that has been optimized out. */ |
c44c2088 JH |
120 | if (!from_decl |
121 | || TREE_CODE (from_decl) != VAR_DECL | |
3aaf0529 | 122 | || (!DECL_EXTERNAL (from_decl) |
9041d2e6 | 123 | && (vnode = varpool_node::get (from_decl)) != NULL |
3aaf0529 | 124 | && vnode->definition) |
6da8be89 | 125 | || (flag_ltrans |
9041d2e6 | 126 | && (vnode = varpool_node::get (from_decl)) != NULL |
6adda80b | 127 | && vnode->in_other_partition)) |
c44c2088 | 128 | return true; |
c44c2088 JH |
129 | /* We are folding reference from external vtable. The vtable may reffer |
130 | to a symbol keyed to other compilation unit. The other compilation | |
131 | unit may be in separate DSO and the symbol may be hidden. */ | |
132 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
133 | && DECL_EXTERNAL (decl) | |
a33a931b | 134 | && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT |
d52f5295 | 135 | && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition)) |
c44c2088 | 136 | return false; |
b3b9f3d0 JH |
137 | /* When function is public, we always can introduce new reference. |
138 | Exception are the COMDAT functions where introducing a direct | |
139 | reference imply need to include function body in the curren tunit. */ | |
140 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
141 | return true; | |
3aaf0529 JH |
142 | /* We have COMDAT. We are going to check if we still have definition |
143 | or if the definition is going to be output in other partition. | |
144 | Bypass this when gimplifying; all needed functions will be produced. | |
c44c2088 JH |
145 | |
146 | As observed in PR20991 for already optimized out comdat virtual functions | |
073a8998 | 147 | it may be tempting to not necessarily give up because the copy will be |
c44c2088 JH |
148 | output elsewhere when corresponding vtable is output. |
149 | This is however not possible - ABI specify that COMDATs are output in | |
150 | units where they are used and when the other unit was compiled with LTO | |
151 | it is possible that vtable was kept public while the function itself | |
152 | was privatized. */ | |
3dafb85c | 153 | if (!symtab->function_flags_ready) |
b3b9f3d0 | 154 | return true; |
c44c2088 | 155 | |
d52f5295 | 156 | snode = symtab_node::get (decl); |
3aaf0529 JH |
157 | if (!snode |
158 | || ((!snode->definition || DECL_EXTERNAL (decl)) | |
159 | && (!snode->in_other_partition | |
160 | || (!snode->forced_by_abi && !snode->force_output)))) | |
161 | return false; | |
162 | node = dyn_cast <cgraph_node *> (snode); | |
163 | return !node || !node->global.inlined_to; | |
1389294c JH |
164 | } |
165 | ||
0038d4e0 | 166 | /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into |
c44c2088 JH |
167 | acceptable form for is_gimple_min_invariant. |
168 | FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */ | |
17f39a39 JH |
169 | |
170 | tree | |
c44c2088 | 171 | canonicalize_constructor_val (tree cval, tree from_decl) |
17f39a39 | 172 | { |
50619002 EB |
173 | tree orig_cval = cval; |
174 | STRIP_NOPS (cval); | |
315f5f1b RG |
175 | if (TREE_CODE (cval) == POINTER_PLUS_EXPR |
176 | && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST) | |
17f39a39 | 177 | { |
315f5f1b RG |
178 | tree ptr = TREE_OPERAND (cval, 0); |
179 | if (is_gimple_min_invariant (ptr)) | |
180 | cval = build1_loc (EXPR_LOCATION (cval), | |
181 | ADDR_EXPR, TREE_TYPE (ptr), | |
182 | fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)), | |
183 | ptr, | |
184 | fold_convert (ptr_type_node, | |
185 | TREE_OPERAND (cval, 1)))); | |
17f39a39 JH |
186 | } |
187 | if (TREE_CODE (cval) == ADDR_EXPR) | |
188 | { | |
5a27a197 RG |
189 | tree base = NULL_TREE; |
190 | if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR) | |
ca5f4331 MM |
191 | { |
192 | base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0)); | |
193 | if (base) | |
194 | TREE_OPERAND (cval, 0) = base; | |
195 | } | |
5a27a197 RG |
196 | else |
197 | base = get_base_address (TREE_OPERAND (cval, 0)); | |
7501ca28 RG |
198 | if (!base) |
199 | return NULL_TREE; | |
b3b9f3d0 | 200 | |
7501ca28 RG |
201 | if ((TREE_CODE (base) == VAR_DECL |
202 | || TREE_CODE (base) == FUNCTION_DECL) | |
c44c2088 | 203 | && !can_refer_decl_in_current_unit_p (base, from_decl)) |
1389294c | 204 | return NULL_TREE; |
7501ca28 | 205 | if (TREE_CODE (base) == VAR_DECL) |
46eb666a | 206 | TREE_ADDRESSABLE (base) = 1; |
7501ca28 RG |
207 | else if (TREE_CODE (base) == FUNCTION_DECL) |
208 | { | |
209 | /* Make sure we create a cgraph node for functions we'll reference. | |
210 | They can be non-existent if the reference comes from an entry | |
211 | of an external vtable for example. */ | |
d52f5295 | 212 | cgraph_node::get_create (base); |
7501ca28 | 213 | } |
0038d4e0 | 214 | /* Fixup types in global initializers. */ |
73aef89e RG |
215 | if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0))) |
216 | cval = build_fold_addr_expr (TREE_OPERAND (cval, 0)); | |
50619002 EB |
217 | |
218 | if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval))) | |
219 | cval = fold_convert (TREE_TYPE (orig_cval), cval); | |
220 | return cval; | |
17f39a39 | 221 | } |
846abd0d RB |
222 | if (TREE_OVERFLOW_P (cval)) |
223 | return drop_tree_overflow (cval); | |
50619002 | 224 | return orig_cval; |
17f39a39 | 225 | } |
cbdd87d4 RG |
226 | |
227 | /* If SYM is a constant variable with known value, return the value. | |
228 | NULL_TREE is returned otherwise. */ | |
229 | ||
230 | tree | |
231 | get_symbol_constant_value (tree sym) | |
232 | { | |
6a6dac52 JH |
233 | tree val = ctor_for_folding (sym); |
234 | if (val != error_mark_node) | |
cbdd87d4 | 235 | { |
cbdd87d4 RG |
236 | if (val) |
237 | { | |
9d60be38 | 238 | val = canonicalize_constructor_val (unshare_expr (val), sym); |
1389294c | 239 | if (val && is_gimple_min_invariant (val)) |
17f39a39 | 240 | return val; |
1389294c JH |
241 | else |
242 | return NULL_TREE; | |
cbdd87d4 RG |
243 | } |
244 | /* Variables declared 'const' without an initializer | |
245 | have zero as the initializer if they may not be | |
246 | overridden at link or run time. */ | |
247 | if (!val | |
b8a8c472 | 248 | && is_gimple_reg_type (TREE_TYPE (sym))) |
e8160c9a | 249 | return build_zero_cst (TREE_TYPE (sym)); |
cbdd87d4 RG |
250 | } |
251 | ||
252 | return NULL_TREE; | |
253 | } | |
254 | ||
255 | ||
cbdd87d4 RG |
256 | |
257 | /* Subroutine of fold_stmt. We perform several simplifications of the | |
258 | memory reference tree EXPR and make sure to re-gimplify them properly | |
259 | after propagation of constant addresses. IS_LHS is true if the | |
260 | reference is supposed to be an lvalue. */ | |
261 | ||
262 | static tree | |
263 | maybe_fold_reference (tree expr, bool is_lhs) | |
264 | { | |
17f39a39 | 265 | tree result; |
cbdd87d4 | 266 | |
f0eddb90 RG |
267 | if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR |
268 | || TREE_CODE (expr) == REALPART_EXPR | |
269 | || TREE_CODE (expr) == IMAGPART_EXPR) | |
270 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
271 | return fold_unary_loc (EXPR_LOCATION (expr), | |
272 | TREE_CODE (expr), | |
273 | TREE_TYPE (expr), | |
274 | TREE_OPERAND (expr, 0)); | |
275 | else if (TREE_CODE (expr) == BIT_FIELD_REF | |
276 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
277 | return fold_ternary_loc (EXPR_LOCATION (expr), | |
278 | TREE_CODE (expr), | |
279 | TREE_TYPE (expr), | |
280 | TREE_OPERAND (expr, 0), | |
281 | TREE_OPERAND (expr, 1), | |
282 | TREE_OPERAND (expr, 2)); | |
283 | ||
f0eddb90 RG |
284 | if (!is_lhs |
285 | && (result = fold_const_aggregate_ref (expr)) | |
286 | && is_gimple_min_invariant (result)) | |
287 | return result; | |
cbdd87d4 | 288 | |
cbdd87d4 RG |
289 | return NULL_TREE; |
290 | } | |
291 | ||
292 | ||
293 | /* Attempt to fold an assignment statement pointed-to by SI. Returns a | |
294 | replacement rhs for the statement or NULL_TREE if no simplification | |
295 | could be made. It is assumed that the operands have been previously | |
296 | folded. */ | |
297 | ||
298 | static tree | |
299 | fold_gimple_assign (gimple_stmt_iterator *si) | |
300 | { | |
301 | gimple stmt = gsi_stmt (*si); | |
302 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
303 | location_t loc = gimple_location (stmt); | |
304 | ||
305 | tree result = NULL_TREE; | |
306 | ||
307 | switch (get_gimple_rhs_class (subcode)) | |
308 | { | |
309 | case GIMPLE_SINGLE_RHS: | |
310 | { | |
311 | tree rhs = gimple_assign_rhs1 (stmt); | |
312 | ||
8c00ba08 JW |
313 | if (TREE_CLOBBER_P (rhs)) |
314 | return NULL_TREE; | |
315 | ||
4e71066d | 316 | if (REFERENCE_CLASS_P (rhs)) |
cbdd87d4 RG |
317 | return maybe_fold_reference (rhs, false); |
318 | ||
bdf37f7a JH |
319 | else if (TREE_CODE (rhs) == OBJ_TYPE_REF) |
320 | { | |
321 | tree val = OBJ_TYPE_REF_EXPR (rhs); | |
322 | if (is_gimple_min_invariant (val)) | |
323 | return val; | |
f8a39967 | 324 | else if (flag_devirtualize && virtual_method_call_p (rhs)) |
bdf37f7a JH |
325 | { |
326 | bool final; | |
327 | vec <cgraph_node *>targets | |
f8a39967 | 328 | = possible_polymorphic_call_targets (rhs, stmt, &final); |
2b5f0895 | 329 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
bdf37f7a | 330 | { |
2b5f0895 XDL |
331 | if (dump_enabled_p ()) |
332 | { | |
807b7d62 | 333 | location_t loc = gimple_location_safe (stmt); |
2b5f0895 XDL |
334 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
335 | "resolving virtual function address " | |
336 | "reference to function %s\n", | |
337 | targets.length () == 1 | |
338 | ? targets[0]->name () | |
3ef276e4 | 339 | : "NULL"); |
2b5f0895 | 340 | } |
3ef276e4 RB |
341 | if (targets.length () == 1) |
342 | { | |
343 | val = fold_convert (TREE_TYPE (val), | |
344 | build_fold_addr_expr_loc | |
345 | (loc, targets[0]->decl)); | |
346 | STRIP_USELESS_TYPE_CONVERSION (val); | |
347 | } | |
348 | else | |
349 | /* We can not use __builtin_unreachable here because it | |
350 | can not have address taken. */ | |
351 | val = build_int_cst (TREE_TYPE (val), 0); | |
bdf37f7a JH |
352 | return val; |
353 | } | |
354 | } | |
355 | ||
356 | } | |
cbdd87d4 RG |
357 | else if (TREE_CODE (rhs) == ADDR_EXPR) |
358 | { | |
70f34814 RG |
359 | tree ref = TREE_OPERAND (rhs, 0); |
360 | tree tem = maybe_fold_reference (ref, true); | |
361 | if (tem | |
362 | && TREE_CODE (tem) == MEM_REF | |
363 | && integer_zerop (TREE_OPERAND (tem, 1))) | |
364 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0)); | |
365 | else if (tem) | |
cbdd87d4 RG |
366 | result = fold_convert (TREE_TYPE (rhs), |
367 | build_fold_addr_expr_loc (loc, tem)); | |
70f34814 RG |
368 | else if (TREE_CODE (ref) == MEM_REF |
369 | && integer_zerop (TREE_OPERAND (ref, 1))) | |
370 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0)); | |
cbdd87d4 RG |
371 | } |
372 | ||
373 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
374 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
375 | && (CONSTRUCTOR_NELTS (rhs) | |
376 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
377 | { | |
378 | /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */ | |
379 | unsigned i; | |
380 | tree val; | |
381 | ||
382 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) | |
383 | if (TREE_CODE (val) != INTEGER_CST | |
384 | && TREE_CODE (val) != REAL_CST | |
385 | && TREE_CODE (val) != FIXED_CST) | |
386 | return NULL_TREE; | |
387 | ||
388 | return build_vector_from_ctor (TREE_TYPE (rhs), | |
389 | CONSTRUCTOR_ELTS (rhs)); | |
390 | } | |
391 | ||
392 | else if (DECL_P (rhs)) | |
9d60be38 | 393 | return get_symbol_constant_value (rhs); |
cbdd87d4 RG |
394 | |
395 | /* If we couldn't fold the RHS, hand over to the generic | |
396 | fold routines. */ | |
397 | if (result == NULL_TREE) | |
398 | result = fold (rhs); | |
399 | ||
400 | /* Strip away useless type conversions. Both the NON_LVALUE_EXPR | |
401 | that may have been added by fold, and "useless" type | |
402 | conversions that might now be apparent due to propagation. */ | |
403 | STRIP_USELESS_TYPE_CONVERSION (result); | |
404 | ||
405 | if (result != rhs && valid_gimple_rhs_p (result)) | |
406 | return result; | |
407 | ||
408 | return NULL_TREE; | |
409 | } | |
410 | break; | |
411 | ||
412 | case GIMPLE_UNARY_RHS: | |
cbdd87d4 RG |
413 | break; |
414 | ||
415 | case GIMPLE_BINARY_RHS: | |
cbdd87d4 RG |
416 | break; |
417 | ||
0354c0c7 | 418 | case GIMPLE_TERNARY_RHS: |
4e71066d RG |
419 | /* Try to fold a conditional expression. */ |
420 | if (gimple_assign_rhs_code (stmt) == COND_EXPR) | |
421 | { | |
422 | tree op0 = gimple_assign_rhs1 (stmt); | |
423 | tree tem; | |
424 | bool set = false; | |
425 | location_t cond_loc = gimple_location (stmt); | |
426 | ||
427 | if (COMPARISON_CLASS_P (op0)) | |
428 | { | |
429 | fold_defer_overflow_warnings (); | |
430 | tem = fold_binary_loc (cond_loc, | |
431 | TREE_CODE (op0), TREE_TYPE (op0), | |
432 | TREE_OPERAND (op0, 0), | |
433 | TREE_OPERAND (op0, 1)); | |
434 | /* This is actually a conditional expression, not a GIMPLE | |
435 | conditional statement, however, the valid_gimple_rhs_p | |
436 | test still applies. */ | |
437 | set = (tem && is_gimple_condexpr (tem) | |
438 | && valid_gimple_rhs_p (tem)); | |
439 | fold_undefer_overflow_warnings (set, stmt, 0); | |
440 | } | |
441 | else if (is_gimple_min_invariant (op0)) | |
442 | { | |
443 | tem = op0; | |
444 | set = true; | |
445 | } | |
446 | else | |
447 | return NULL_TREE; | |
448 | ||
449 | if (set) | |
450 | result = fold_build3_loc (cond_loc, COND_EXPR, | |
451 | TREE_TYPE (gimple_assign_lhs (stmt)), tem, | |
452 | gimple_assign_rhs2 (stmt), | |
453 | gimple_assign_rhs3 (stmt)); | |
454 | } | |
455 | ||
456 | if (!result) | |
457 | result = fold_ternary_loc (loc, subcode, | |
458 | TREE_TYPE (gimple_assign_lhs (stmt)), | |
459 | gimple_assign_rhs1 (stmt), | |
460 | gimple_assign_rhs2 (stmt), | |
461 | gimple_assign_rhs3 (stmt)); | |
0354c0c7 BS |
462 | |
463 | if (result) | |
464 | { | |
465 | STRIP_USELESS_TYPE_CONVERSION (result); | |
466 | if (valid_gimple_rhs_p (result)) | |
467 | return result; | |
0354c0c7 BS |
468 | } |
469 | break; | |
470 | ||
cbdd87d4 RG |
471 | case GIMPLE_INVALID_RHS: |
472 | gcc_unreachable (); | |
473 | } | |
474 | ||
475 | return NULL_TREE; | |
476 | } | |
477 | ||
fef5a0d9 RB |
478 | |
479 | /* Replace a statement at *SI_P with a sequence of statements in STMTS, | |
480 | adjusting the replacement stmts location and virtual operands. | |
481 | If the statement has a lhs the last stmt in the sequence is expected | |
482 | to assign to that lhs. */ | |
483 | ||
484 | static void | |
485 | gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts) | |
486 | { | |
487 | gimple stmt = gsi_stmt (*si_p); | |
488 | ||
489 | if (gimple_has_location (stmt)) | |
490 | annotate_all_with_location (stmts, gimple_location (stmt)); | |
491 | ||
492 | /* First iterate over the replacement statements backward, assigning | |
493 | virtual operands to their defining statements. */ | |
494 | gimple laststore = NULL; | |
495 | for (gimple_stmt_iterator i = gsi_last (stmts); | |
496 | !gsi_end_p (i); gsi_prev (&i)) | |
497 | { | |
498 | gimple new_stmt = gsi_stmt (i); | |
499 | if ((gimple_assign_single_p (new_stmt) | |
500 | && !is_gimple_reg (gimple_assign_lhs (new_stmt))) | |
501 | || (is_gimple_call (new_stmt) | |
502 | && (gimple_call_flags (new_stmt) | |
503 | & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0)) | |
504 | { | |
505 | tree vdef; | |
506 | if (!laststore) | |
507 | vdef = gimple_vdef (stmt); | |
508 | else | |
509 | vdef = make_ssa_name (gimple_vop (cfun), new_stmt); | |
510 | gimple_set_vdef (new_stmt, vdef); | |
511 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
512 | SSA_NAME_DEF_STMT (vdef) = new_stmt; | |
513 | laststore = new_stmt; | |
514 | } | |
515 | } | |
516 | ||
517 | /* Second iterate over the statements forward, assigning virtual | |
518 | operands to their uses. */ | |
519 | tree reaching_vuse = gimple_vuse (stmt); | |
520 | for (gimple_stmt_iterator i = gsi_start (stmts); | |
521 | !gsi_end_p (i); gsi_next (&i)) | |
522 | { | |
523 | gimple new_stmt = gsi_stmt (i); | |
524 | /* If the new statement possibly has a VUSE, update it with exact SSA | |
525 | name we know will reach this one. */ | |
526 | if (gimple_has_mem_ops (new_stmt)) | |
527 | gimple_set_vuse (new_stmt, reaching_vuse); | |
528 | gimple_set_modified (new_stmt, true); | |
529 | if (gimple_vdef (new_stmt)) | |
530 | reaching_vuse = gimple_vdef (new_stmt); | |
531 | } | |
532 | ||
533 | /* If the new sequence does not do a store release the virtual | |
534 | definition of the original statement. */ | |
535 | if (reaching_vuse | |
536 | && reaching_vuse == gimple_vuse (stmt)) | |
537 | { | |
538 | tree vdef = gimple_vdef (stmt); | |
539 | if (vdef | |
540 | && TREE_CODE (vdef) == SSA_NAME) | |
541 | { | |
542 | unlink_stmt_vdef (stmt); | |
543 | release_ssa_name (vdef); | |
544 | } | |
545 | } | |
546 | ||
547 | /* Finally replace the original statement with the sequence. */ | |
548 | gsi_replace_with_seq (si_p, stmts, false); | |
549 | } | |
550 | ||
cbdd87d4 RG |
551 | /* Convert EXPR into a GIMPLE value suitable for substitution on the |
552 | RHS of an assignment. Insert the necessary statements before | |
553 | iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL | |
554 | is replaced. If the call is expected to produces a result, then it | |
555 | is replaced by an assignment of the new RHS to the result variable. | |
556 | If the result is to be ignored, then the call is replaced by a | |
fe2ef088 MM |
557 | GIMPLE_NOP. A proper VDEF chain is retained by making the first |
558 | VUSE and the last VDEF of the whole sequence be the same as the replaced | |
559 | statement and using new SSA names for stores in between. */ | |
cbdd87d4 RG |
560 | |
561 | void | |
562 | gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr) | |
563 | { | |
564 | tree lhs; | |
cbdd87d4 RG |
565 | gimple stmt, new_stmt; |
566 | gimple_stmt_iterator i; | |
355a7673 | 567 | gimple_seq stmts = NULL; |
cbdd87d4 RG |
568 | |
569 | stmt = gsi_stmt (*si_p); | |
570 | ||
571 | gcc_assert (is_gimple_call (stmt)); | |
572 | ||
45852dcc | 573 | push_gimplify_context (gimple_in_ssa_p (cfun)); |
cbdd87d4 | 574 | |
e256dfce | 575 | lhs = gimple_call_lhs (stmt); |
cbdd87d4 | 576 | if (lhs == NULL_TREE) |
6e572326 RG |
577 | { |
578 | gimplify_and_add (expr, &stmts); | |
579 | /* We can end up with folding a memcpy of an empty class assignment | |
580 | which gets optimized away by C++ gimplification. */ | |
581 | if (gimple_seq_empty_p (stmts)) | |
582 | { | |
9fdc58de | 583 | pop_gimplify_context (NULL); |
6e572326 RG |
584 | if (gimple_in_ssa_p (cfun)) |
585 | { | |
586 | unlink_stmt_vdef (stmt); | |
587 | release_defs (stmt); | |
588 | } | |
77d19c72 | 589 | gsi_replace (si_p, gimple_build_nop (), true); |
6e572326 RG |
590 | return; |
591 | } | |
592 | } | |
cbdd87d4 | 593 | else |
e256dfce RG |
594 | { |
595 | tree tmp = get_initialized_tmp_var (expr, &stmts, NULL); | |
596 | new_stmt = gimple_build_assign (lhs, tmp); | |
597 | i = gsi_last (stmts); | |
598 | gsi_insert_after_without_update (&i, new_stmt, | |
599 | GSI_CONTINUE_LINKING); | |
600 | } | |
cbdd87d4 RG |
601 | |
602 | pop_gimplify_context (NULL); | |
603 | ||
fef5a0d9 RB |
604 | gsi_replace_with_seq_vops (si_p, stmts); |
605 | } | |
cbdd87d4 | 606 | |
fef5a0d9 RB |
607 | |
608 | /* Replace the call at *GSI with the gimple value VAL. */ | |
609 | ||
610 | static void | |
611 | replace_call_with_value (gimple_stmt_iterator *gsi, tree val) | |
612 | { | |
613 | gimple stmt = gsi_stmt (*gsi); | |
614 | tree lhs = gimple_call_lhs (stmt); | |
615 | gimple repl; | |
616 | if (lhs) | |
e256dfce | 617 | { |
fef5a0d9 RB |
618 | if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val))) |
619 | val = fold_convert (TREE_TYPE (lhs), val); | |
620 | repl = gimple_build_assign (lhs, val); | |
621 | } | |
622 | else | |
623 | repl = gimple_build_nop (); | |
624 | tree vdef = gimple_vdef (stmt); | |
625 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
626 | { | |
627 | unlink_stmt_vdef (stmt); | |
628 | release_ssa_name (vdef); | |
629 | } | |
630 | gsi_replace (gsi, repl, true); | |
631 | } | |
632 | ||
633 | /* Replace the call at *GSI with the new call REPL and fold that | |
634 | again. */ | |
635 | ||
636 | static void | |
637 | replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple repl) | |
638 | { | |
639 | gimple stmt = gsi_stmt (*gsi); | |
640 | gimple_call_set_lhs (repl, gimple_call_lhs (stmt)); | |
641 | gimple_set_location (repl, gimple_location (stmt)); | |
642 | if (gimple_vdef (stmt) | |
643 | && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) | |
644 | { | |
645 | gimple_set_vdef (repl, gimple_vdef (stmt)); | |
646 | gimple_set_vuse (repl, gimple_vuse (stmt)); | |
647 | SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl; | |
648 | } | |
649 | gsi_replace (gsi, repl, true); | |
650 | fold_stmt (gsi); | |
651 | } | |
652 | ||
653 | /* Return true if VAR is a VAR_DECL or a component thereof. */ | |
654 | ||
655 | static bool | |
656 | var_decl_component_p (tree var) | |
657 | { | |
658 | tree inner = var; | |
659 | while (handled_component_p (inner)) | |
660 | inner = TREE_OPERAND (inner, 0); | |
661 | return SSA_VAR_P (inner); | |
662 | } | |
663 | ||
664 | /* Fold function call to builtin mem{{,p}cpy,move}. Return | |
86c5a5c3 | 665 | false if no simplification can be made. |
fef5a0d9 RB |
666 | If ENDP is 0, return DEST (like memcpy). |
667 | If ENDP is 1, return DEST+LEN (like mempcpy). | |
668 | If ENDP is 2, return DEST+LEN-1 (like stpcpy). | |
669 | If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap | |
670 | (memmove). */ | |
671 | ||
672 | static bool | |
673 | gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, | |
674 | tree dest, tree src, int endp) | |
675 | { | |
676 | gimple stmt = gsi_stmt (*gsi); | |
677 | tree lhs = gimple_call_lhs (stmt); | |
678 | tree len = gimple_call_arg (stmt, 2); | |
679 | tree destvar, srcvar; | |
680 | location_t loc = gimple_location (stmt); | |
681 | ||
682 | /* If the LEN parameter is zero, return DEST. */ | |
683 | if (integer_zerop (len)) | |
684 | { | |
685 | gimple repl; | |
686 | if (gimple_call_lhs (stmt)) | |
687 | repl = gimple_build_assign (gimple_call_lhs (stmt), dest); | |
688 | else | |
689 | repl = gimple_build_nop (); | |
690 | tree vdef = gimple_vdef (stmt); | |
691 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
e256dfce | 692 | { |
fef5a0d9 RB |
693 | unlink_stmt_vdef (stmt); |
694 | release_ssa_name (vdef); | |
695 | } | |
696 | gsi_replace (gsi, repl, true); | |
697 | return true; | |
698 | } | |
699 | ||
700 | /* If SRC and DEST are the same (and not volatile), return | |
701 | DEST{,+LEN,+LEN-1}. */ | |
702 | if (operand_equal_p (src, dest, 0)) | |
703 | { | |
704 | unlink_stmt_vdef (stmt); | |
705 | if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) | |
706 | release_ssa_name (gimple_vdef (stmt)); | |
707 | if (!lhs) | |
708 | { | |
709 | gsi_replace (gsi, gimple_build_nop (), true); | |
710 | return true; | |
711 | } | |
712 | goto done; | |
713 | } | |
714 | else | |
715 | { | |
716 | tree srctype, desttype; | |
717 | unsigned int src_align, dest_align; | |
718 | tree off0; | |
719 | ||
720 | /* Build accesses at offset zero with a ref-all character type. */ | |
721 | off0 = build_int_cst (build_pointer_type_for_mode (char_type_node, | |
722 | ptr_mode, true), 0); | |
723 | ||
724 | /* If we can perform the copy efficiently with first doing all loads | |
725 | and then all stores inline it that way. Currently efficiently | |
726 | means that we can load all the memory into a single integer | |
727 | register which is what MOVE_MAX gives us. */ | |
728 | src_align = get_pointer_alignment (src); | |
729 | dest_align = get_pointer_alignment (dest); | |
730 | if (tree_fits_uhwi_p (len) | |
731 | && compare_tree_int (len, MOVE_MAX) <= 0 | |
732 | /* ??? Don't transform copies from strings with known length this | |
733 | confuses the tree-ssa-strlen.c. This doesn't handle | |
734 | the case in gcc.dg/strlenopt-8.c which is XFAILed for that | |
735 | reason. */ | |
736 | && !c_strlen (src, 2)) | |
737 | { | |
738 | unsigned ilen = tree_to_uhwi (len); | |
739 | if (exact_log2 (ilen) != -1) | |
740 | { | |
741 | tree type = lang_hooks.types.type_for_size (ilen * 8, 1); | |
742 | if (type | |
743 | && TYPE_MODE (type) != BLKmode | |
744 | && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT | |
745 | == ilen * 8) | |
746 | /* If the destination pointer is not aligned we must be able | |
747 | to emit an unaligned store. */ | |
748 | && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type)) | |
749 | || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align))) | |
750 | { | |
751 | tree srctype = type; | |
752 | tree desttype = type; | |
753 | if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))) | |
754 | srctype = build_aligned_type (type, src_align); | |
755 | tree srcmem = fold_build2 (MEM_REF, srctype, src, off0); | |
756 | tree tem = fold_const_aggregate_ref (srcmem); | |
757 | if (tem) | |
758 | srcmem = tem; | |
759 | else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)) | |
760 | && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), | |
761 | src_align)) | |
762 | srcmem = NULL_TREE; | |
763 | if (srcmem) | |
764 | { | |
765 | gimple new_stmt; | |
766 | if (is_gimple_reg_type (TREE_TYPE (srcmem))) | |
767 | { | |
768 | new_stmt = gimple_build_assign (NULL_TREE, srcmem); | |
769 | if (gimple_in_ssa_p (cfun)) | |
770 | srcmem = make_ssa_name (TREE_TYPE (srcmem), | |
771 | new_stmt); | |
772 | else | |
b731b390 | 773 | srcmem = create_tmp_reg (TREE_TYPE (srcmem)); |
fef5a0d9 RB |
774 | gimple_assign_set_lhs (new_stmt, srcmem); |
775 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
776 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
777 | } | |
778 | if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))) | |
779 | desttype = build_aligned_type (type, dest_align); | |
780 | new_stmt | |
781 | = gimple_build_assign (fold_build2 (MEM_REF, desttype, | |
782 | dest, off0), | |
783 | srcmem); | |
784 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
785 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
786 | if (gimple_vdef (new_stmt) | |
787 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
788 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
789 | if (!lhs) | |
790 | { | |
791 | gsi_replace (gsi, new_stmt, true); | |
792 | return true; | |
793 | } | |
794 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
795 | goto done; | |
796 | } | |
797 | } | |
798 | } | |
799 | } | |
800 | ||
801 | if (endp == 3) | |
802 | { | |
803 | /* Both DEST and SRC must be pointer types. | |
804 | ??? This is what old code did. Is the testing for pointer types | |
805 | really mandatory? | |
806 | ||
807 | If either SRC is readonly or length is 1, we can use memcpy. */ | |
808 | if (!dest_align || !src_align) | |
809 | return false; | |
810 | if (readonly_data_expr (src) | |
811 | || (tree_fits_uhwi_p (len) | |
812 | && (MIN (src_align, dest_align) / BITS_PER_UNIT | |
813 | >= tree_to_uhwi (len)))) | |
814 | { | |
815 | tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
816 | if (!fn) | |
817 | return false; | |
818 | gimple_call_set_fndecl (stmt, fn); | |
819 | gimple_call_set_arg (stmt, 0, dest); | |
820 | gimple_call_set_arg (stmt, 1, src); | |
821 | fold_stmt (gsi); | |
822 | return true; | |
823 | } | |
824 | ||
825 | /* If *src and *dest can't overlap, optimize into memcpy as well. */ | |
826 | if (TREE_CODE (src) == ADDR_EXPR | |
827 | && TREE_CODE (dest) == ADDR_EXPR) | |
828 | { | |
829 | tree src_base, dest_base, fn; | |
830 | HOST_WIDE_INT src_offset = 0, dest_offset = 0; | |
831 | HOST_WIDE_INT size = -1; | |
832 | HOST_WIDE_INT maxsize = -1; | |
833 | ||
834 | srcvar = TREE_OPERAND (src, 0); | |
835 | src_base = get_ref_base_and_extent (srcvar, &src_offset, | |
836 | &size, &maxsize); | |
837 | destvar = TREE_OPERAND (dest, 0); | |
838 | dest_base = get_ref_base_and_extent (destvar, &dest_offset, | |
839 | &size, &maxsize); | |
840 | if (tree_fits_uhwi_p (len)) | |
841 | maxsize = tree_to_uhwi (len); | |
842 | else | |
843 | maxsize = -1; | |
844 | src_offset /= BITS_PER_UNIT; | |
845 | dest_offset /= BITS_PER_UNIT; | |
846 | if (SSA_VAR_P (src_base) | |
847 | && SSA_VAR_P (dest_base)) | |
848 | { | |
849 | if (operand_equal_p (src_base, dest_base, 0) | |
850 | && ranges_overlap_p (src_offset, maxsize, | |
851 | dest_offset, maxsize)) | |
852 | return false; | |
853 | } | |
854 | else if (TREE_CODE (src_base) == MEM_REF | |
855 | && TREE_CODE (dest_base) == MEM_REF) | |
856 | { | |
857 | if (! operand_equal_p (TREE_OPERAND (src_base, 0), | |
858 | TREE_OPERAND (dest_base, 0), 0)) | |
859 | return false; | |
860 | offset_int off = mem_ref_offset (src_base) + src_offset; | |
861 | if (!wi::fits_shwi_p (off)) | |
862 | return false; | |
863 | src_offset = off.to_shwi (); | |
864 | ||
865 | off = mem_ref_offset (dest_base) + dest_offset; | |
866 | if (!wi::fits_shwi_p (off)) | |
867 | return false; | |
868 | dest_offset = off.to_shwi (); | |
869 | if (ranges_overlap_p (src_offset, maxsize, | |
870 | dest_offset, maxsize)) | |
871 | return false; | |
872 | } | |
873 | else | |
874 | return false; | |
875 | ||
876 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
877 | if (!fn) | |
878 | return false; | |
879 | gimple_call_set_fndecl (stmt, fn); | |
880 | gimple_call_set_arg (stmt, 0, dest); | |
881 | gimple_call_set_arg (stmt, 1, src); | |
882 | fold_stmt (gsi); | |
883 | return true; | |
884 | } | |
885 | ||
886 | /* If the destination and source do not alias optimize into | |
887 | memcpy as well. */ | |
888 | if ((is_gimple_min_invariant (dest) | |
889 | || TREE_CODE (dest) == SSA_NAME) | |
890 | && (is_gimple_min_invariant (src) | |
891 | || TREE_CODE (src) == SSA_NAME)) | |
892 | { | |
893 | ao_ref destr, srcr; | |
894 | ao_ref_init_from_ptr_and_size (&destr, dest, len); | |
895 | ao_ref_init_from_ptr_and_size (&srcr, src, len); | |
896 | if (!refs_may_alias_p_1 (&destr, &srcr, false)) | |
897 | { | |
898 | tree fn; | |
899 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
900 | if (!fn) | |
901 | return false; | |
902 | gimple_call_set_fndecl (stmt, fn); | |
903 | gimple_call_set_arg (stmt, 0, dest); | |
904 | gimple_call_set_arg (stmt, 1, src); | |
905 | fold_stmt (gsi); | |
906 | return true; | |
907 | } | |
908 | } | |
909 | ||
910 | return false; | |
911 | } | |
912 | ||
913 | if (!tree_fits_shwi_p (len)) | |
914 | return false; | |
915 | /* FIXME: | |
916 | This logic lose for arguments like (type *)malloc (sizeof (type)), | |
917 | since we strip the casts of up to VOID return value from malloc. | |
918 | Perhaps we ought to inherit type from non-VOID argument here? */ | |
919 | STRIP_NOPS (src); | |
920 | STRIP_NOPS (dest); | |
921 | if (!POINTER_TYPE_P (TREE_TYPE (src)) | |
922 | || !POINTER_TYPE_P (TREE_TYPE (dest))) | |
923 | return false; | |
924 | /* In the following try to find a type that is most natural to be | |
925 | used for the memcpy source and destination and that allows | |
926 | the most optimization when memcpy is turned into a plain assignment | |
927 | using that type. In theory we could always use a char[len] type | |
928 | but that only gains us that the destination and source possibly | |
929 | no longer will have their address taken. */ | |
930 | /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */ | |
931 | if (TREE_CODE (src) == POINTER_PLUS_EXPR) | |
932 | { | |
933 | tree tem = TREE_OPERAND (src, 0); | |
934 | STRIP_NOPS (tem); | |
935 | if (tem != TREE_OPERAND (src, 0)) | |
936 | src = build1 (NOP_EXPR, TREE_TYPE (tem), src); | |
937 | } | |
938 | if (TREE_CODE (dest) == POINTER_PLUS_EXPR) | |
939 | { | |
940 | tree tem = TREE_OPERAND (dest, 0); | |
941 | STRIP_NOPS (tem); | |
942 | if (tem != TREE_OPERAND (dest, 0)) | |
943 | dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest); | |
944 | } | |
945 | srctype = TREE_TYPE (TREE_TYPE (src)); | |
946 | if (TREE_CODE (srctype) == ARRAY_TYPE | |
947 | && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) | |
948 | { | |
949 | srctype = TREE_TYPE (srctype); | |
950 | STRIP_NOPS (src); | |
951 | src = build1 (NOP_EXPR, build_pointer_type (srctype), src); | |
952 | } | |
953 | desttype = TREE_TYPE (TREE_TYPE (dest)); | |
954 | if (TREE_CODE (desttype) == ARRAY_TYPE | |
955 | && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
956 | { | |
957 | desttype = TREE_TYPE (desttype); | |
958 | STRIP_NOPS (dest); | |
959 | dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest); | |
960 | } | |
961 | if (TREE_ADDRESSABLE (srctype) | |
962 | || TREE_ADDRESSABLE (desttype)) | |
963 | return false; | |
964 | ||
965 | /* Make sure we are not copying using a floating-point mode or | |
966 | a type whose size possibly does not match its precision. */ | |
967 | if (FLOAT_MODE_P (TYPE_MODE (desttype)) | |
968 | || TREE_CODE (desttype) == BOOLEAN_TYPE | |
969 | || TREE_CODE (desttype) == ENUMERAL_TYPE) | |
970 | desttype = bitwise_type_for_mode (TYPE_MODE (desttype)); | |
971 | if (FLOAT_MODE_P (TYPE_MODE (srctype)) | |
972 | || TREE_CODE (srctype) == BOOLEAN_TYPE | |
973 | || TREE_CODE (srctype) == ENUMERAL_TYPE) | |
974 | srctype = bitwise_type_for_mode (TYPE_MODE (srctype)); | |
975 | if (!srctype) | |
976 | srctype = desttype; | |
977 | if (!desttype) | |
978 | desttype = srctype; | |
979 | if (!srctype) | |
980 | return false; | |
981 | ||
982 | src_align = get_pointer_alignment (src); | |
983 | dest_align = get_pointer_alignment (dest); | |
984 | if (dest_align < TYPE_ALIGN (desttype) | |
985 | || src_align < TYPE_ALIGN (srctype)) | |
986 | return false; | |
987 | ||
988 | destvar = dest; | |
989 | STRIP_NOPS (destvar); | |
990 | if (TREE_CODE (destvar) == ADDR_EXPR | |
991 | && var_decl_component_p (TREE_OPERAND (destvar, 0)) | |
992 | && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
993 | destvar = fold_build2 (MEM_REF, desttype, destvar, off0); | |
994 | else | |
995 | destvar = NULL_TREE; | |
996 | ||
997 | srcvar = src; | |
998 | STRIP_NOPS (srcvar); | |
999 | if (TREE_CODE (srcvar) == ADDR_EXPR | |
1000 | && var_decl_component_p (TREE_OPERAND (srcvar, 0)) | |
1001 | && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) | |
1002 | { | |
1003 | if (!destvar | |
1004 | || src_align >= TYPE_ALIGN (desttype)) | |
1005 | srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype, | |
1006 | srcvar, off0); | |
1007 | else if (!STRICT_ALIGNMENT) | |
1008 | { | |
1009 | srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype), | |
1010 | src_align); | |
1011 | srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0); | |
1012 | } | |
e256dfce | 1013 | else |
fef5a0d9 RB |
1014 | srcvar = NULL_TREE; |
1015 | } | |
1016 | else | |
1017 | srcvar = NULL_TREE; | |
1018 | ||
1019 | if (srcvar == NULL_TREE && destvar == NULL_TREE) | |
1020 | return false; | |
1021 | ||
1022 | if (srcvar == NULL_TREE) | |
1023 | { | |
1024 | STRIP_NOPS (src); | |
1025 | if (src_align >= TYPE_ALIGN (desttype)) | |
1026 | srcvar = fold_build2 (MEM_REF, desttype, src, off0); | |
1027 | else | |
1028 | { | |
1029 | if (STRICT_ALIGNMENT) | |
1030 | return false; | |
1031 | srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype), | |
1032 | src_align); | |
1033 | srcvar = fold_build2 (MEM_REF, srctype, src, off0); | |
1034 | } | |
1035 | } | |
1036 | else if (destvar == NULL_TREE) | |
1037 | { | |
1038 | STRIP_NOPS (dest); | |
1039 | if (dest_align >= TYPE_ALIGN (srctype)) | |
1040 | destvar = fold_build2 (MEM_REF, srctype, dest, off0); | |
1041 | else | |
1042 | { | |
1043 | if (STRICT_ALIGNMENT) | |
1044 | return false; | |
1045 | desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype), | |
1046 | dest_align); | |
1047 | destvar = fold_build2 (MEM_REF, desttype, dest, off0); | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | gimple new_stmt; | |
1052 | if (is_gimple_reg_type (TREE_TYPE (srcvar))) | |
1053 | { | |
1054 | new_stmt = gimple_build_assign (NULL_TREE, srcvar); | |
1055 | if (gimple_in_ssa_p (cfun)) | |
1056 | srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt); | |
1057 | else | |
b731b390 | 1058 | srcvar = create_tmp_reg (TREE_TYPE (srcvar)); |
fef5a0d9 RB |
1059 | gimple_assign_set_lhs (new_stmt, srcvar); |
1060 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
1061 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1062 | } | |
1063 | new_stmt = gimple_build_assign (destvar, srcvar); | |
1064 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
1065 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
1066 | if (gimple_vdef (new_stmt) | |
1067 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
1068 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
1069 | if (!lhs) | |
1070 | { | |
1071 | gsi_replace (gsi, new_stmt, true); | |
1072 | return true; | |
1073 | } | |
1074 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1075 | } | |
1076 | ||
1077 | done: | |
1078 | if (endp == 0 || endp == 3) | |
1079 | len = NULL_TREE; | |
1080 | else if (endp == 2) | |
1081 | len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len, | |
1082 | ssize_int (1)); | |
1083 | if (endp == 2 || endp == 1) | |
1084 | dest = fold_build_pointer_plus_loc (loc, dest, len); | |
1085 | ||
1086 | dest = force_gimple_operand_gsi (gsi, dest, false, NULL_TREE, true, | |
1087 | GSI_SAME_STMT); | |
1088 | gimple repl = gimple_build_assign (lhs, dest); | |
1089 | gsi_replace (gsi, repl, true); | |
1090 | return true; | |
1091 | } | |
1092 | ||
1093 | /* Fold function call to builtin memset or bzero at *GSI setting the | |
1094 | memory of size LEN to VAL. Return whether a simplification was made. */ | |
1095 | ||
1096 | static bool | |
1097 | gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len) | |
1098 | { | |
1099 | gimple stmt = gsi_stmt (*gsi); | |
1100 | tree etype; | |
1101 | unsigned HOST_WIDE_INT length, cval; | |
1102 | ||
1103 | /* If the LEN parameter is zero, return DEST. */ | |
1104 | if (integer_zerop (len)) | |
1105 | { | |
1106 | replace_call_with_value (gsi, gimple_call_arg (stmt, 0)); | |
1107 | return true; | |
1108 | } | |
1109 | ||
1110 | if (! tree_fits_uhwi_p (len)) | |
1111 | return false; | |
1112 | ||
1113 | if (TREE_CODE (c) != INTEGER_CST) | |
1114 | return false; | |
1115 | ||
1116 | tree dest = gimple_call_arg (stmt, 0); | |
1117 | tree var = dest; | |
1118 | if (TREE_CODE (var) != ADDR_EXPR) | |
1119 | return false; | |
1120 | ||
1121 | var = TREE_OPERAND (var, 0); | |
1122 | if (TREE_THIS_VOLATILE (var)) | |
1123 | return false; | |
1124 | ||
1125 | etype = TREE_TYPE (var); | |
1126 | if (TREE_CODE (etype) == ARRAY_TYPE) | |
1127 | etype = TREE_TYPE (etype); | |
1128 | ||
1129 | if (!INTEGRAL_TYPE_P (etype) | |
1130 | && !POINTER_TYPE_P (etype)) | |
1131 | return NULL_TREE; | |
1132 | ||
1133 | if (! var_decl_component_p (var)) | |
1134 | return NULL_TREE; | |
1135 | ||
1136 | length = tree_to_uhwi (len); | |
1137 | if (GET_MODE_SIZE (TYPE_MODE (etype)) != length | |
1138 | || get_pointer_alignment (dest) / BITS_PER_UNIT < length) | |
1139 | return NULL_TREE; | |
1140 | ||
1141 | if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) | |
1142 | return NULL_TREE; | |
1143 | ||
1144 | if (integer_zerop (c)) | |
1145 | cval = 0; | |
1146 | else | |
1147 | { | |
1148 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64) | |
1149 | return NULL_TREE; | |
1150 | ||
1151 | cval = TREE_INT_CST_LOW (c); | |
1152 | cval &= 0xff; | |
1153 | cval |= cval << 8; | |
1154 | cval |= cval << 16; | |
1155 | cval |= (cval << 31) << 1; | |
1156 | } | |
1157 | ||
1158 | var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0)); | |
1159 | gimple store = gimple_build_assign (var, build_int_cst_type (etype, cval)); | |
1160 | gimple_set_vuse (store, gimple_vuse (stmt)); | |
1161 | tree vdef = gimple_vdef (stmt); | |
1162 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
1163 | { | |
1164 | gimple_set_vdef (store, gimple_vdef (stmt)); | |
1165 | SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store; | |
1166 | } | |
1167 | gsi_insert_before (gsi, store, GSI_SAME_STMT); | |
1168 | if (gimple_call_lhs (stmt)) | |
1169 | { | |
1170 | gimple asgn = gimple_build_assign (gimple_call_lhs (stmt), dest); | |
1171 | gsi_replace (gsi, asgn, true); | |
1172 | } | |
1173 | else | |
1174 | { | |
1175 | gimple_stmt_iterator gsi2 = *gsi; | |
1176 | gsi_prev (gsi); | |
1177 | gsi_remove (&gsi2, true); | |
1178 | } | |
1179 | ||
1180 | return true; | |
1181 | } | |
1182 | ||
1183 | ||
1184 | /* Return the string length, maximum string length or maximum value of | |
1185 | ARG in LENGTH. | |
1186 | If ARG is an SSA name variable, follow its use-def chains. If LENGTH | |
1187 | is not NULL and, for TYPE == 0, its value is not equal to the length | |
1188 | we determine or if we are unable to determine the length or value, | |
1189 | return false. VISITED is a bitmap of visited variables. | |
1190 | TYPE is 0 if string length should be returned, 1 for maximum string | |
1191 | length and 2 for maximum value ARG can have. */ | |
1192 | ||
1193 | static bool | |
dcb7fae2 | 1194 | get_maxval_strlen (tree arg, tree *length, bitmap *visited, int type) |
fef5a0d9 RB |
1195 | { |
1196 | tree var, val; | |
1197 | gimple def_stmt; | |
1198 | ||
1199 | if (TREE_CODE (arg) != SSA_NAME) | |
1200 | { | |
1201 | /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */ | |
1202 | if (TREE_CODE (arg) == ADDR_EXPR | |
1203 | && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF | |
1204 | && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1))) | |
1205 | { | |
1206 | tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
1207 | if (TREE_CODE (aop0) == INDIRECT_REF | |
1208 | && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME) | |
1209 | return get_maxval_strlen (TREE_OPERAND (aop0, 0), | |
1210 | length, visited, type); | |
1211 | } | |
1212 | ||
1213 | if (type == 2) | |
1214 | { | |
1215 | val = arg; | |
1216 | if (TREE_CODE (val) != INTEGER_CST | |
1217 | || tree_int_cst_sgn (val) < 0) | |
1218 | return false; | |
1219 | } | |
1220 | else | |
1221 | val = c_strlen (arg, 1); | |
1222 | if (!val) | |
1223 | return false; | |
1224 | ||
1225 | if (*length) | |
1226 | { | |
1227 | if (type > 0) | |
1228 | { | |
1229 | if (TREE_CODE (*length) != INTEGER_CST | |
1230 | || TREE_CODE (val) != INTEGER_CST) | |
1231 | return false; | |
1232 | ||
1233 | if (tree_int_cst_lt (*length, val)) | |
1234 | *length = val; | |
1235 | return true; | |
1236 | } | |
1237 | else if (simple_cst_equal (val, *length) != 1) | |
1238 | return false; | |
1239 | } | |
1240 | ||
1241 | *length = val; | |
1242 | return true; | |
1243 | } | |
1244 | ||
1245 | /* If ARG is registered for SSA update we cannot look at its defining | |
1246 | statement. */ | |
1247 | if (name_registered_for_update_p (arg)) | |
1248 | return false; | |
1249 | ||
1250 | /* If we were already here, break the infinite cycle. */ | |
dcb7fae2 RB |
1251 | if (!*visited) |
1252 | *visited = BITMAP_ALLOC (NULL); | |
1253 | if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg))) | |
fef5a0d9 RB |
1254 | return true; |
1255 | ||
1256 | var = arg; | |
1257 | def_stmt = SSA_NAME_DEF_STMT (var); | |
1258 | ||
1259 | switch (gimple_code (def_stmt)) | |
1260 | { | |
1261 | case GIMPLE_ASSIGN: | |
1262 | /* The RHS of the statement defining VAR must either have a | |
1263 | constant length or come from another SSA_NAME with a constant | |
1264 | length. */ | |
1265 | if (gimple_assign_single_p (def_stmt) | |
1266 | || gimple_assign_unary_nop_p (def_stmt)) | |
1267 | { | |
1268 | tree rhs = gimple_assign_rhs1 (def_stmt); | |
1269 | return get_maxval_strlen (rhs, length, visited, type); | |
1270 | } | |
1271 | else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR) | |
1272 | { | |
1273 | tree op2 = gimple_assign_rhs2 (def_stmt); | |
1274 | tree op3 = gimple_assign_rhs3 (def_stmt); | |
1275 | return get_maxval_strlen (op2, length, visited, type) | |
1276 | && get_maxval_strlen (op3, length, visited, type); | |
1277 | } | |
1278 | return false; | |
1279 | ||
1280 | case GIMPLE_PHI: | |
1281 | { | |
1282 | /* All the arguments of the PHI node must have the same constant | |
1283 | length. */ | |
1284 | unsigned i; | |
1285 | ||
1286 | for (i = 0; i < gimple_phi_num_args (def_stmt); i++) | |
1287 | { | |
1288 | tree arg = gimple_phi_arg (def_stmt, i)->def; | |
1289 | ||
1290 | /* If this PHI has itself as an argument, we cannot | |
1291 | determine the string length of this argument. However, | |
1292 | if we can find a constant string length for the other | |
1293 | PHI args then we can still be sure that this is a | |
1294 | constant string length. So be optimistic and just | |
1295 | continue with the next argument. */ | |
1296 | if (arg == gimple_phi_result (def_stmt)) | |
1297 | continue; | |
1298 | ||
1299 | if (!get_maxval_strlen (arg, length, visited, type)) | |
1300 | return false; | |
1301 | } | |
1302 | } | |
1303 | return true; | |
1304 | ||
1305 | default: | |
1306 | return false; | |
1307 | } | |
1308 | } | |
1309 | ||
dcb7fae2 RB |
1310 | tree |
1311 | get_maxval_strlen (tree arg, int type) | |
1312 | { | |
1313 | bitmap visited = NULL; | |
1314 | tree len = NULL_TREE; | |
1315 | if (!get_maxval_strlen (arg, &len, &visited, type)) | |
1316 | len = NULL_TREE; | |
1317 | if (visited) | |
1318 | BITMAP_FREE (visited); | |
1319 | ||
1320 | return len; | |
1321 | } | |
1322 | ||
fef5a0d9 RB |
1323 | |
1324 | /* Fold function call to builtin strcpy with arguments DEST and SRC. | |
1325 | If LEN is not NULL, it represents the length of the string to be | |
1326 | copied. Return NULL_TREE if no simplification can be made. */ | |
1327 | ||
1328 | static bool | |
1329 | gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi, | |
dcb7fae2 | 1330 | tree dest, tree src) |
fef5a0d9 | 1331 | { |
dcb7fae2 | 1332 | location_t loc = gimple_location (gsi_stmt (*gsi)); |
fef5a0d9 RB |
1333 | tree fn; |
1334 | ||
1335 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
1336 | if (operand_equal_p (src, dest, 0)) | |
1337 | { | |
1338 | replace_call_with_value (gsi, dest); | |
1339 | return true; | |
1340 | } | |
1341 | ||
1342 | if (optimize_function_for_size_p (cfun)) | |
1343 | return false; | |
1344 | ||
1345 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1346 | if (!fn) | |
1347 | return false; | |
1348 | ||
1579e1f8 | 1349 | tree len = get_maxval_strlen (src, 0); |
fef5a0d9 | 1350 | if (!len) |
dcb7fae2 | 1351 | return false; |
fef5a0d9 RB |
1352 | |
1353 | len = fold_convert_loc (loc, size_type_node, len); | |
1354 | len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1)); | |
1355 | len = force_gimple_operand_gsi (gsi, len, true, | |
1356 | NULL_TREE, true, GSI_SAME_STMT); | |
1357 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1358 | replace_call_with_call_and_fold (gsi, repl); | |
1359 | return true; | |
1360 | } | |
1361 | ||
1362 | /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN. | |
1363 | If SLEN is not NULL, it represents the length of the source string. | |
1364 | Return NULL_TREE if no simplification can be made. */ | |
1365 | ||
1366 | static bool | |
dcb7fae2 RB |
1367 | gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi, |
1368 | tree dest, tree src, tree len) | |
fef5a0d9 | 1369 | { |
dcb7fae2 | 1370 | location_t loc = gimple_location (gsi_stmt (*gsi)); |
fef5a0d9 RB |
1371 | tree fn; |
1372 | ||
1373 | /* If the LEN parameter is zero, return DEST. */ | |
1374 | if (integer_zerop (len)) | |
1375 | { | |
1376 | replace_call_with_value (gsi, dest); | |
1377 | return true; | |
1378 | } | |
1379 | ||
1380 | /* We can't compare slen with len as constants below if len is not a | |
1381 | constant. */ | |
dcb7fae2 | 1382 | if (TREE_CODE (len) != INTEGER_CST) |
fef5a0d9 RB |
1383 | return false; |
1384 | ||
fef5a0d9 | 1385 | /* Now, we must be passed a constant src ptr parameter. */ |
1579e1f8 | 1386 | tree slen = get_maxval_strlen (src, 0); |
dcb7fae2 | 1387 | if (!slen || TREE_CODE (slen) != INTEGER_CST) |
fef5a0d9 RB |
1388 | return false; |
1389 | ||
1390 | slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1)); | |
1391 | ||
1392 | /* We do not support simplification of this case, though we do | |
1393 | support it when expanding trees into RTL. */ | |
1394 | /* FIXME: generate a call to __builtin_memset. */ | |
1395 | if (tree_int_cst_lt (slen, len)) | |
1396 | return false; | |
1397 | ||
1398 | /* OK transform into builtin memcpy. */ | |
1399 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1400 | if (!fn) | |
1401 | return false; | |
1402 | ||
1403 | len = fold_convert_loc (loc, size_type_node, len); | |
1404 | len = force_gimple_operand_gsi (gsi, len, true, | |
1405 | NULL_TREE, true, GSI_SAME_STMT); | |
1406 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1407 | replace_call_with_call_and_fold (gsi, repl); | |
1408 | return true; | |
1409 | } | |
1410 | ||
1411 | /* Simplify a call to the strcat builtin. DST and SRC are the arguments | |
1412 | to the call. | |
1413 | ||
1414 | Return NULL_TREE if no simplification was possible, otherwise return the | |
1415 | simplified form of the call as a tree. | |
1416 | ||
1417 | The simplified form may be a constant or other expression which | |
1418 | computes the same value, but in a more efficient manner (including | |
1419 | calls to other builtin functions). | |
1420 | ||
1421 | The call may contain arguments which need to be evaluated, but | |
1422 | which are not useful to determine the result of the call. In | |
1423 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
1424 | COMPOUND_EXPR will be an argument which must be evaluated. | |
1425 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
1426 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
1427 | form of the builtin function call. */ | |
1428 | ||
1429 | static bool | |
dcb7fae2 | 1430 | gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src) |
fef5a0d9 RB |
1431 | { |
1432 | gimple stmt = gsi_stmt (*gsi); | |
dcb7fae2 | 1433 | location_t loc = gimple_location (stmt); |
fef5a0d9 RB |
1434 | |
1435 | const char *p = c_getstr (src); | |
1436 | ||
1437 | /* If the string length is zero, return the dst parameter. */ | |
1438 | if (p && *p == '\0') | |
1439 | { | |
1440 | replace_call_with_value (gsi, dst); | |
1441 | return true; | |
1442 | } | |
1443 | ||
1444 | if (!optimize_bb_for_speed_p (gimple_bb (stmt))) | |
1445 | return false; | |
1446 | ||
1447 | /* See if we can store by pieces into (dst + strlen(dst)). */ | |
1448 | tree newdst; | |
1449 | tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN); | |
1450 | tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1451 | ||
1452 | if (!strlen_fn || !memcpy_fn) | |
1453 | return false; | |
1454 | ||
1455 | /* If the length of the source string isn't computable don't | |
1456 | split strcat into strlen and memcpy. */ | |
dcb7fae2 | 1457 | tree len = get_maxval_strlen (src, 0); |
fef5a0d9 | 1458 | if (! len) |
fef5a0d9 RB |
1459 | return false; |
1460 | ||
1461 | /* Create strlen (dst). */ | |
1462 | gimple_seq stmts = NULL, stmts2; | |
1463 | gimple repl = gimple_build_call (strlen_fn, 1, dst); | |
1464 | gimple_set_location (repl, loc); | |
1465 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 1466 | newdst = make_ssa_name (size_type_node); |
fef5a0d9 | 1467 | else |
b731b390 | 1468 | newdst = create_tmp_reg (size_type_node); |
fef5a0d9 RB |
1469 | gimple_call_set_lhs (repl, newdst); |
1470 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1471 | ||
1472 | /* Create (dst p+ strlen (dst)). */ | |
1473 | newdst = fold_build_pointer_plus_loc (loc, dst, newdst); | |
1474 | newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE); | |
1475 | gimple_seq_add_seq_without_update (&stmts, stmts2); | |
1476 | ||
1477 | len = fold_convert_loc (loc, size_type_node, len); | |
1478 | len = size_binop_loc (loc, PLUS_EXPR, len, | |
1479 | build_int_cst (size_type_node, 1)); | |
1480 | len = force_gimple_operand (len, &stmts2, true, NULL_TREE); | |
1481 | gimple_seq_add_seq_without_update (&stmts, stmts2); | |
1482 | ||
1483 | repl = gimple_build_call (memcpy_fn, 3, newdst, src, len); | |
1484 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1485 | if (gimple_call_lhs (stmt)) | |
1486 | { | |
1487 | repl = gimple_build_assign (gimple_call_lhs (stmt), dst); | |
1488 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1489 | gsi_replace_with_seq_vops (gsi, stmts); | |
1490 | /* gsi now points at the assignment to the lhs, get a | |
1491 | stmt iterator to the memcpy call. | |
1492 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
1493 | CFG isn't built yet. */ | |
1494 | gimple_stmt_iterator gsi2 = *gsi; | |
1495 | gsi_prev (&gsi2); | |
1496 | fold_stmt (&gsi2); | |
1497 | } | |
1498 | else | |
1499 | { | |
1500 | gsi_replace_with_seq_vops (gsi, stmts); | |
1501 | fold_stmt (gsi); | |
1502 | } | |
1503 | return true; | |
1504 | } | |
1505 | ||
07f1cf56 RB |
1506 | /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE |
1507 | are the arguments to the call. */ | |
1508 | ||
1509 | static bool | |
1510 | gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi) | |
1511 | { | |
1512 | gimple stmt = gsi_stmt (*gsi); | |
1513 | tree dest = gimple_call_arg (stmt, 0); | |
1514 | tree src = gimple_call_arg (stmt, 1); | |
1515 | tree size = gimple_call_arg (stmt, 2); | |
1516 | tree fn; | |
1517 | const char *p; | |
1518 | ||
1519 | ||
1520 | p = c_getstr (src); | |
1521 | /* If the SRC parameter is "", return DEST. */ | |
1522 | if (p && *p == '\0') | |
1523 | { | |
1524 | replace_call_with_value (gsi, dest); | |
1525 | return true; | |
1526 | } | |
1527 | ||
1528 | if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size)) | |
1529 | return false; | |
1530 | ||
1531 | /* If __builtin_strcat_chk is used, assume strcat is available. */ | |
1532 | fn = builtin_decl_explicit (BUILT_IN_STRCAT); | |
1533 | if (!fn) | |
1534 | return false; | |
1535 | ||
1536 | gimple repl = gimple_build_call (fn, 2, dest, src); | |
1537 | replace_call_with_call_and_fold (gsi, repl); | |
1538 | return true; | |
1539 | } | |
1540 | ||
ad03a744 RB |
1541 | /* Simplify a call to the strncat builtin. */ |
1542 | ||
1543 | static bool | |
1544 | gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi) | |
1545 | { | |
1546 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
1547 | tree dst = gimple_call_arg (stmt, 0); | |
1548 | tree src = gimple_call_arg (stmt, 1); | |
1549 | tree len = gimple_call_arg (stmt, 2); | |
1550 | ||
1551 | const char *p = c_getstr (src); | |
1552 | ||
1553 | /* If the requested length is zero, or the src parameter string | |
1554 | length is zero, return the dst parameter. */ | |
1555 | if (integer_zerop (len) || (p && *p == '\0')) | |
1556 | { | |
1557 | replace_call_with_value (gsi, dst); | |
1558 | return true; | |
1559 | } | |
1560 | ||
1561 | /* If the requested len is greater than or equal to the string | |
1562 | length, call strcat. */ | |
1563 | if (TREE_CODE (len) == INTEGER_CST && p | |
1564 | && compare_tree_int (len, strlen (p)) >= 0) | |
1565 | { | |
1566 | tree fn = builtin_decl_implicit (BUILT_IN_STRCAT); | |
1567 | ||
1568 | /* If the replacement _DECL isn't initialized, don't do the | |
1569 | transformation. */ | |
1570 | if (!fn) | |
1571 | return false; | |
1572 | ||
1573 | gcall *repl = gimple_build_call (fn, 2, dst, src); | |
1574 | replace_call_with_call_and_fold (gsi, repl); | |
1575 | return true; | |
1576 | } | |
1577 | ||
1578 | return false; | |
1579 | } | |
1580 | ||
745583f9 RB |
1581 | /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC, |
1582 | LEN, and SIZE. */ | |
1583 | ||
1584 | static bool | |
1585 | gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi) | |
1586 | { | |
1587 | gimple stmt = gsi_stmt (*gsi); | |
1588 | tree dest = gimple_call_arg (stmt, 0); | |
1589 | tree src = gimple_call_arg (stmt, 1); | |
1590 | tree len = gimple_call_arg (stmt, 2); | |
1591 | tree size = gimple_call_arg (stmt, 3); | |
1592 | tree fn; | |
1593 | const char *p; | |
1594 | ||
1595 | p = c_getstr (src); | |
1596 | /* If the SRC parameter is "" or if LEN is 0, return DEST. */ | |
1597 | if ((p && *p == '\0') | |
1598 | || integer_zerop (len)) | |
1599 | { | |
1600 | replace_call_with_value (gsi, dest); | |
1601 | return true; | |
1602 | } | |
1603 | ||
1604 | if (! tree_fits_uhwi_p (size)) | |
1605 | return false; | |
1606 | ||
1607 | if (! integer_all_onesp (size)) | |
1608 | { | |
1609 | tree src_len = c_strlen (src, 1); | |
1610 | if (src_len | |
1611 | && tree_fits_uhwi_p (src_len) | |
1612 | && tree_fits_uhwi_p (len) | |
1613 | && ! tree_int_cst_lt (len, src_len)) | |
1614 | { | |
1615 | /* If LEN >= strlen (SRC), optimize into __strcat_chk. */ | |
1616 | fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK); | |
1617 | if (!fn) | |
1618 | return false; | |
1619 | ||
1620 | gimple repl = gimple_build_call (fn, 3, dest, src, size); | |
1621 | replace_call_with_call_and_fold (gsi, repl); | |
1622 | return true; | |
1623 | } | |
1624 | return false; | |
1625 | } | |
1626 | ||
1627 | /* If __builtin_strncat_chk is used, assume strncat is available. */ | |
1628 | fn = builtin_decl_explicit (BUILT_IN_STRNCAT); | |
1629 | if (!fn) | |
1630 | return false; | |
1631 | ||
1632 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1633 | replace_call_with_call_and_fold (gsi, repl); | |
1634 | return true; | |
1635 | } | |
1636 | ||
fef5a0d9 RB |
1637 | /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments |
1638 | to the call. IGNORE is true if the value returned | |
1639 | by the builtin will be ignored. UNLOCKED is true is true if this | |
1640 | actually a call to fputs_unlocked. If LEN in non-NULL, it represents | |
1641 | the known length of the string. Return NULL_TREE if no simplification | |
1642 | was possible. */ | |
1643 | ||
1644 | static bool | |
1645 | gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi, | |
fef5a0d9 | 1646 | tree arg0, tree arg1, |
dcb7fae2 | 1647 | bool unlocked) |
fef5a0d9 | 1648 | { |
dcb7fae2 RB |
1649 | gimple stmt = gsi_stmt (*gsi); |
1650 | ||
fef5a0d9 RB |
1651 | /* If we're using an unlocked function, assume the other unlocked |
1652 | functions exist explicitly. */ | |
1653 | tree const fn_fputc = (unlocked | |
1654 | ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED) | |
1655 | : builtin_decl_implicit (BUILT_IN_FPUTC)); | |
1656 | tree const fn_fwrite = (unlocked | |
1657 | ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED) | |
1658 | : builtin_decl_implicit (BUILT_IN_FWRITE)); | |
1659 | ||
1660 | /* If the return value is used, don't do the transformation. */ | |
dcb7fae2 | 1661 | if (gimple_call_lhs (stmt)) |
fef5a0d9 RB |
1662 | return false; |
1663 | ||
fef5a0d9 RB |
1664 | /* Get the length of the string passed to fputs. If the length |
1665 | can't be determined, punt. */ | |
dcb7fae2 | 1666 | tree len = get_maxval_strlen (arg0, 0); |
fef5a0d9 RB |
1667 | if (!len |
1668 | || TREE_CODE (len) != INTEGER_CST) | |
1669 | return false; | |
1670 | ||
1671 | switch (compare_tree_int (len, 1)) | |
1672 | { | |
1673 | case -1: /* length is 0, delete the call entirely . */ | |
1674 | replace_call_with_value (gsi, integer_zero_node); | |
1675 | return true; | |
1676 | ||
1677 | case 0: /* length is 1, call fputc. */ | |
1678 | { | |
1679 | const char *p = c_getstr (arg0); | |
1680 | if (p != NULL) | |
1681 | { | |
1682 | if (!fn_fputc) | |
1683 | return false; | |
1684 | ||
1685 | gimple repl = gimple_build_call (fn_fputc, 2, | |
1686 | build_int_cst | |
1687 | (integer_type_node, p[0]), arg1); | |
1688 | replace_call_with_call_and_fold (gsi, repl); | |
1689 | return true; | |
1690 | } | |
1691 | } | |
1692 | /* FALLTHROUGH */ | |
1693 | case 1: /* length is greater than 1, call fwrite. */ | |
1694 | { | |
1695 | /* If optimizing for size keep fputs. */ | |
1696 | if (optimize_function_for_size_p (cfun)) | |
1697 | return false; | |
1698 | /* New argument list transforming fputs(string, stream) to | |
1699 | fwrite(string, 1, len, stream). */ | |
1700 | if (!fn_fwrite) | |
1701 | return false; | |
1702 | ||
1703 | gimple repl = gimple_build_call (fn_fwrite, 4, arg0, | |
1704 | size_one_node, len, arg1); | |
1705 | replace_call_with_call_and_fold (gsi, repl); | |
1706 | return true; | |
1707 | } | |
1708 | default: | |
1709 | gcc_unreachable (); | |
1710 | } | |
1711 | return false; | |
1712 | } | |
1713 | ||
1714 | /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
1715 | DEST, SRC, LEN, and SIZE are the arguments to the call. | |
1716 | IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_* | |
1717 | code of the builtin. If MAXLEN is not NULL, it is maximum length | |
1718 | passed as third argument. */ | |
1719 | ||
1720 | static bool | |
1721 | gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, | |
fef5a0d9 | 1722 | tree dest, tree src, tree len, tree size, |
fef5a0d9 RB |
1723 | enum built_in_function fcode) |
1724 | { | |
dcb7fae2 RB |
1725 | gimple stmt = gsi_stmt (*gsi); |
1726 | location_t loc = gimple_location (stmt); | |
1727 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
fef5a0d9 RB |
1728 | tree fn; |
1729 | ||
1730 | /* If SRC and DEST are the same (and not volatile), return DEST | |
1731 | (resp. DEST+LEN for __mempcpy_chk). */ | |
1732 | if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) | |
1733 | { | |
1734 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
1735 | { | |
1736 | replace_call_with_value (gsi, dest); | |
1737 | return true; | |
1738 | } | |
1739 | else | |
1740 | { | |
1741 | tree temp = fold_build_pointer_plus_loc (loc, dest, len); | |
1742 | temp = force_gimple_operand_gsi (gsi, temp, | |
1743 | false, NULL_TREE, true, | |
1744 | GSI_SAME_STMT); | |
1745 | replace_call_with_value (gsi, temp); | |
1746 | return true; | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | if (! tree_fits_uhwi_p (size)) | |
1751 | return false; | |
1752 | ||
dcb7fae2 | 1753 | tree maxlen = get_maxval_strlen (len, 2); |
fef5a0d9 RB |
1754 | if (! integer_all_onesp (size)) |
1755 | { | |
1756 | if (! tree_fits_uhwi_p (len)) | |
1757 | { | |
1758 | /* If LEN is not constant, try MAXLEN too. | |
1759 | For MAXLEN only allow optimizing into non-_ocs function | |
1760 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
1761 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
1762 | { | |
1763 | if (fcode == BUILT_IN_MEMPCPY_CHK && ignore) | |
1764 | { | |
1765 | /* (void) __mempcpy_chk () can be optimized into | |
1766 | (void) __memcpy_chk (). */ | |
1767 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); | |
1768 | if (!fn) | |
1769 | return false; | |
1770 | ||
1771 | gimple repl = gimple_build_call (fn, 4, dest, src, len, size); | |
1772 | replace_call_with_call_and_fold (gsi, repl); | |
1773 | return true; | |
1774 | } | |
1775 | return false; | |
1776 | } | |
1777 | } | |
1778 | else | |
1779 | maxlen = len; | |
1780 | ||
1781 | if (tree_int_cst_lt (size, maxlen)) | |
1782 | return false; | |
1783 | } | |
1784 | ||
1785 | fn = NULL_TREE; | |
1786 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
1787 | mem{cpy,pcpy,move,set} is available. */ | |
1788 | switch (fcode) | |
1789 | { | |
1790 | case BUILT_IN_MEMCPY_CHK: | |
1791 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY); | |
1792 | break; | |
1793 | case BUILT_IN_MEMPCPY_CHK: | |
1794 | fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); | |
1795 | break; | |
1796 | case BUILT_IN_MEMMOVE_CHK: | |
1797 | fn = builtin_decl_explicit (BUILT_IN_MEMMOVE); | |
1798 | break; | |
1799 | case BUILT_IN_MEMSET_CHK: | |
1800 | fn = builtin_decl_explicit (BUILT_IN_MEMSET); | |
1801 | break; | |
1802 | default: | |
1803 | break; | |
1804 | } | |
1805 | ||
1806 | if (!fn) | |
1807 | return false; | |
1808 | ||
1809 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1810 | replace_call_with_call_and_fold (gsi, repl); | |
1811 | return true; | |
1812 | } | |
1813 | ||
1814 | /* Fold a call to the __st[rp]cpy_chk builtin. | |
1815 | DEST, SRC, and SIZE are the arguments to the call. | |
1816 | IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_* | |
1817 | code of the builtin. If MAXLEN is not NULL, it is maximum length of | |
1818 | strings passed as second argument. */ | |
1819 | ||
1820 | static bool | |
1821 | gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, | |
dcb7fae2 | 1822 | tree dest, |
fef5a0d9 | 1823 | tree src, tree size, |
fef5a0d9 RB |
1824 | enum built_in_function fcode) |
1825 | { | |
dcb7fae2 RB |
1826 | gimple stmt = gsi_stmt (*gsi); |
1827 | location_t loc = gimple_location (stmt); | |
1828 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
fef5a0d9 RB |
1829 | tree len, fn; |
1830 | ||
1831 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
1832 | if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) | |
1833 | { | |
1834 | replace_call_with_value (gsi, dest); | |
1835 | return true; | |
1836 | } | |
1837 | ||
1838 | if (! tree_fits_uhwi_p (size)) | |
1839 | return false; | |
1840 | ||
dcb7fae2 | 1841 | tree maxlen = get_maxval_strlen (src, 1); |
fef5a0d9 RB |
1842 | if (! integer_all_onesp (size)) |
1843 | { | |
1844 | len = c_strlen (src, 1); | |
1845 | if (! len || ! tree_fits_uhwi_p (len)) | |
1846 | { | |
1847 | /* If LEN is not constant, try MAXLEN too. | |
1848 | For MAXLEN only allow optimizing into non-_ocs function | |
1849 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
1850 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
1851 | { | |
1852 | if (fcode == BUILT_IN_STPCPY_CHK) | |
1853 | { | |
1854 | if (! ignore) | |
1855 | return false; | |
1856 | ||
1857 | /* If return value of __stpcpy_chk is ignored, | |
1858 | optimize into __strcpy_chk. */ | |
1859 | fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK); | |
1860 | if (!fn) | |
1861 | return false; | |
1862 | ||
1863 | gimple repl = gimple_build_call (fn, 3, dest, src, size); | |
1864 | replace_call_with_call_and_fold (gsi, repl); | |
1865 | return true; | |
1866 | } | |
1867 | ||
1868 | if (! len || TREE_SIDE_EFFECTS (len)) | |
1869 | return false; | |
1870 | ||
1871 | /* If c_strlen returned something, but not a constant, | |
1872 | transform __strcpy_chk into __memcpy_chk. */ | |
1873 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); | |
1874 | if (!fn) | |
1875 | return false; | |
1876 | ||
1877 | len = fold_convert_loc (loc, size_type_node, len); | |
1878 | len = size_binop_loc (loc, PLUS_EXPR, len, | |
1879 | build_int_cst (size_type_node, 1)); | |
1880 | len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE, | |
1881 | true, GSI_SAME_STMT); | |
1882 | gimple repl = gimple_build_call (fn, 4, dest, src, len, size); | |
1883 | replace_call_with_call_and_fold (gsi, repl); | |
1884 | return true; | |
1885 | } | |
e256dfce | 1886 | } |
fef5a0d9 RB |
1887 | else |
1888 | maxlen = len; | |
1889 | ||
1890 | if (! tree_int_cst_lt (maxlen, size)) | |
1891 | return false; | |
e256dfce RG |
1892 | } |
1893 | ||
fef5a0d9 RB |
1894 | /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ |
1895 | fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK | |
1896 | ? BUILT_IN_STPCPY : BUILT_IN_STRCPY); | |
1897 | if (!fn) | |
1898 | return false; | |
1899 | ||
1900 | gimple repl = gimple_build_call (fn, 2, dest, src); | |
1901 | replace_call_with_call_and_fold (gsi, repl); | |
1902 | return true; | |
1903 | } | |
1904 | ||
1905 | /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE | |
1906 | are the arguments to the call. If MAXLEN is not NULL, it is maximum | |
1907 | length passed as third argument. IGNORE is true if return value can be | |
1908 | ignored. FCODE is the BUILT_IN_* code of the builtin. */ | |
1909 | ||
1910 | static bool | |
1911 | gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, | |
1912 | tree dest, tree src, | |
dcb7fae2 | 1913 | tree len, tree size, |
fef5a0d9 RB |
1914 | enum built_in_function fcode) |
1915 | { | |
dcb7fae2 RB |
1916 | gimple stmt = gsi_stmt (*gsi); |
1917 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
fef5a0d9 RB |
1918 | tree fn; |
1919 | ||
1920 | if (fcode == BUILT_IN_STPNCPY_CHK && ignore) | |
cbdd87d4 | 1921 | { |
fef5a0d9 RB |
1922 | /* If return value of __stpncpy_chk is ignored, |
1923 | optimize into __strncpy_chk. */ | |
1924 | fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); | |
1925 | if (fn) | |
1926 | { | |
1927 | gimple repl = gimple_build_call (fn, 4, dest, src, len, size); | |
1928 | replace_call_with_call_and_fold (gsi, repl); | |
1929 | return true; | |
1930 | } | |
cbdd87d4 RG |
1931 | } |
1932 | ||
fef5a0d9 RB |
1933 | if (! tree_fits_uhwi_p (size)) |
1934 | return false; | |
1935 | ||
dcb7fae2 | 1936 | tree maxlen = get_maxval_strlen (len, 2); |
fef5a0d9 | 1937 | if (! integer_all_onesp (size)) |
cbdd87d4 | 1938 | { |
fef5a0d9 | 1939 | if (! tree_fits_uhwi_p (len)) |
fe2ef088 | 1940 | { |
fef5a0d9 RB |
1941 | /* If LEN is not constant, try MAXLEN too. |
1942 | For MAXLEN only allow optimizing into non-_ocs function | |
1943 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
1944 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
1945 | return false; | |
8a1561bc | 1946 | } |
fef5a0d9 RB |
1947 | else |
1948 | maxlen = len; | |
1949 | ||
1950 | if (tree_int_cst_lt (size, maxlen)) | |
1951 | return false; | |
cbdd87d4 RG |
1952 | } |
1953 | ||
fef5a0d9 RB |
1954 | /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */ |
1955 | fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK | |
1956 | ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY); | |
1957 | if (!fn) | |
1958 | return false; | |
1959 | ||
1960 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1961 | replace_call_with_call_and_fold (gsi, repl); | |
1962 | return true; | |
cbdd87d4 RG |
1963 | } |
1964 | ||
2625bb5d RB |
1965 | /* Fold function call to builtin stpcpy with arguments DEST and SRC. |
1966 | Return NULL_TREE if no simplification can be made. */ | |
1967 | ||
1968 | static bool | |
1969 | gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi) | |
1970 | { | |
1971 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
1972 | location_t loc = gimple_location (stmt); | |
1973 | tree dest = gimple_call_arg (stmt, 0); | |
1974 | tree src = gimple_call_arg (stmt, 1); | |
1975 | tree fn, len, lenp1; | |
1976 | ||
1977 | /* If the result is unused, replace stpcpy with strcpy. */ | |
1978 | if (gimple_call_lhs (stmt) == NULL_TREE) | |
1979 | { | |
1980 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
1981 | if (!fn) | |
1982 | return false; | |
1983 | gimple_call_set_fndecl (stmt, fn); | |
1984 | fold_stmt (gsi); | |
1985 | return true; | |
1986 | } | |
1987 | ||
1988 | len = c_strlen (src, 1); | |
1989 | if (!len | |
1990 | || TREE_CODE (len) != INTEGER_CST) | |
1991 | return false; | |
1992 | ||
1993 | if (optimize_function_for_size_p (cfun) | |
1994 | /* If length is zero it's small enough. */ | |
1995 | && !integer_zerop (len)) | |
1996 | return false; | |
1997 | ||
1998 | /* If the source has a known length replace stpcpy with memcpy. */ | |
1999 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
2000 | if (!fn) | |
2001 | return false; | |
2002 | ||
2003 | gimple_seq stmts = NULL; | |
2004 | tree tem = gimple_convert (&stmts, loc, size_type_node, len); | |
2005 | lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, | |
2006 | tem, build_int_cst (size_type_node, 1)); | |
2007 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); | |
2008 | gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1); | |
2009 | gimple_set_vuse (repl, gimple_vuse (stmt)); | |
2010 | gimple_set_vdef (repl, gimple_vdef (stmt)); | |
2011 | if (gimple_vdef (repl) | |
2012 | && TREE_CODE (gimple_vdef (repl)) == SSA_NAME) | |
2013 | SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl; | |
2014 | gsi_insert_before (gsi, repl, GSI_SAME_STMT); | |
2015 | /* Replace the result with dest + len. */ | |
2016 | stmts = NULL; | |
2017 | tem = gimple_convert (&stmts, loc, sizetype, len); | |
2018 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); | |
2019 | gassign *ret = gimple_build_assign (gimple_call_lhs (stmt), | |
2020 | POINTER_PLUS_EXPR, dest, tem); | |
2021 | gsi_replace (gsi, ret, true); | |
2022 | /* Finally fold the memcpy call. */ | |
2023 | gimple_stmt_iterator gsi2 = *gsi; | |
2024 | gsi_prev (&gsi2); | |
2025 | fold_stmt (&gsi2); | |
2026 | return true; | |
2027 | } | |
2028 | ||
fef5a0d9 RB |
2029 | /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return |
2030 | NULL_TREE if a normal call should be emitted rather than expanding | |
2031 | the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or | |
2032 | BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length | |
2033 | passed as second argument. */ | |
cbdd87d4 RG |
2034 | |
2035 | static bool | |
fef5a0d9 | 2036 | gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi, |
dcb7fae2 | 2037 | enum built_in_function fcode) |
cbdd87d4 | 2038 | { |
538dd0b7 | 2039 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
fef5a0d9 RB |
2040 | tree dest, size, len, fn, fmt, flag; |
2041 | const char *fmt_str; | |
cbdd87d4 | 2042 | |
fef5a0d9 RB |
2043 | /* Verify the required arguments in the original call. */ |
2044 | if (gimple_call_num_args (stmt) < 5) | |
2045 | return false; | |
cbdd87d4 | 2046 | |
fef5a0d9 RB |
2047 | dest = gimple_call_arg (stmt, 0); |
2048 | len = gimple_call_arg (stmt, 1); | |
2049 | flag = gimple_call_arg (stmt, 2); | |
2050 | size = gimple_call_arg (stmt, 3); | |
2051 | fmt = gimple_call_arg (stmt, 4); | |
2052 | ||
2053 | if (! tree_fits_uhwi_p (size)) | |
2054 | return false; | |
2055 | ||
2056 | if (! integer_all_onesp (size)) | |
2057 | { | |
dcb7fae2 | 2058 | tree maxlen = get_maxval_strlen (len, 2); |
fef5a0d9 | 2059 | if (! tree_fits_uhwi_p (len)) |
cbdd87d4 | 2060 | { |
fef5a0d9 RB |
2061 | /* If LEN is not constant, try MAXLEN too. |
2062 | For MAXLEN only allow optimizing into non-_ocs function | |
2063 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2064 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
cbdd87d4 RG |
2065 | return false; |
2066 | } | |
2067 | else | |
fef5a0d9 | 2068 | maxlen = len; |
cbdd87d4 | 2069 | |
fef5a0d9 RB |
2070 | if (tree_int_cst_lt (size, maxlen)) |
2071 | return false; | |
2072 | } | |
cbdd87d4 | 2073 | |
fef5a0d9 RB |
2074 | if (!init_target_chars ()) |
2075 | return false; | |
cbdd87d4 | 2076 | |
fef5a0d9 RB |
2077 | /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0 |
2078 | or if format doesn't contain % chars or is "%s". */ | |
2079 | if (! integer_zerop (flag)) | |
2080 | { | |
2081 | fmt_str = c_getstr (fmt); | |
2082 | if (fmt_str == NULL) | |
2083 | return false; | |
2084 | if (strchr (fmt_str, target_percent) != NULL | |
2085 | && strcmp (fmt_str, target_percent_s)) | |
2086 | return false; | |
cbdd87d4 RG |
2087 | } |
2088 | ||
fef5a0d9 RB |
2089 | /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is |
2090 | available. */ | |
2091 | fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK | |
2092 | ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF); | |
2093 | if (!fn) | |
491e0b9b RG |
2094 | return false; |
2095 | ||
fef5a0d9 RB |
2096 | /* Replace the called function and the first 5 argument by 3 retaining |
2097 | trailing varargs. */ | |
2098 | gimple_call_set_fndecl (stmt, fn); | |
2099 | gimple_call_set_fntype (stmt, TREE_TYPE (fn)); | |
2100 | gimple_call_set_arg (stmt, 0, dest); | |
2101 | gimple_call_set_arg (stmt, 1, len); | |
2102 | gimple_call_set_arg (stmt, 2, fmt); | |
2103 | for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i) | |
2104 | gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2)); | |
2105 | gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2); | |
2106 | fold_stmt (gsi); | |
2107 | return true; | |
2108 | } | |
cbdd87d4 | 2109 | |
fef5a0d9 RB |
2110 | /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS. |
2111 | Return NULL_TREE if a normal call should be emitted rather than | |
2112 | expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK | |
2113 | or BUILT_IN_VSPRINTF_CHK. */ | |
cbdd87d4 | 2114 | |
fef5a0d9 RB |
2115 | static bool |
2116 | gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi, | |
2117 | enum built_in_function fcode) | |
2118 | { | |
538dd0b7 | 2119 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
fef5a0d9 RB |
2120 | tree dest, size, len, fn, fmt, flag; |
2121 | const char *fmt_str; | |
2122 | unsigned nargs = gimple_call_num_args (stmt); | |
cbdd87d4 | 2123 | |
fef5a0d9 RB |
2124 | /* Verify the required arguments in the original call. */ |
2125 | if (nargs < 4) | |
2126 | return false; | |
2127 | dest = gimple_call_arg (stmt, 0); | |
2128 | flag = gimple_call_arg (stmt, 1); | |
2129 | size = gimple_call_arg (stmt, 2); | |
2130 | fmt = gimple_call_arg (stmt, 3); | |
2131 | ||
2132 | if (! tree_fits_uhwi_p (size)) | |
2133 | return false; | |
2134 | ||
2135 | len = NULL_TREE; | |
2136 | ||
2137 | if (!init_target_chars ()) | |
2138 | return false; | |
2139 | ||
2140 | /* Check whether the format is a literal string constant. */ | |
2141 | fmt_str = c_getstr (fmt); | |
2142 | if (fmt_str != NULL) | |
2143 | { | |
2144 | /* If the format doesn't contain % args or %%, we know the size. */ | |
2145 | if (strchr (fmt_str, target_percent) == 0) | |
cbdd87d4 | 2146 | { |
fef5a0d9 RB |
2147 | if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4) |
2148 | len = build_int_cstu (size_type_node, strlen (fmt_str)); | |
2149 | } | |
2150 | /* If the format is "%s" and first ... argument is a string literal, | |
2151 | we know the size too. */ | |
2152 | else if (fcode == BUILT_IN_SPRINTF_CHK | |
2153 | && strcmp (fmt_str, target_percent_s) == 0) | |
2154 | { | |
2155 | tree arg; | |
cbdd87d4 | 2156 | |
fef5a0d9 RB |
2157 | if (nargs == 5) |
2158 | { | |
2159 | arg = gimple_call_arg (stmt, 4); | |
2160 | if (POINTER_TYPE_P (TREE_TYPE (arg))) | |
2161 | { | |
2162 | len = c_strlen (arg, 1); | |
2163 | if (! len || ! tree_fits_uhwi_p (len)) | |
2164 | len = NULL_TREE; | |
2165 | } | |
2166 | } | |
2167 | } | |
2168 | } | |
cbdd87d4 | 2169 | |
fef5a0d9 RB |
2170 | if (! integer_all_onesp (size)) |
2171 | { | |
2172 | if (! len || ! tree_int_cst_lt (len, size)) | |
2173 | return false; | |
2174 | } | |
cbdd87d4 | 2175 | |
fef5a0d9 RB |
2176 | /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0 |
2177 | or if format doesn't contain % chars or is "%s". */ | |
2178 | if (! integer_zerop (flag)) | |
2179 | { | |
2180 | if (fmt_str == NULL) | |
2181 | return false; | |
2182 | if (strchr (fmt_str, target_percent) != NULL | |
2183 | && strcmp (fmt_str, target_percent_s)) | |
2184 | return false; | |
2185 | } | |
cbdd87d4 | 2186 | |
fef5a0d9 RB |
2187 | /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */ |
2188 | fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK | |
2189 | ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF); | |
2190 | if (!fn) | |
2191 | return false; | |
2192 | ||
2193 | /* Replace the called function and the first 4 argument by 2 retaining | |
2194 | trailing varargs. */ | |
2195 | gimple_call_set_fndecl (stmt, fn); | |
2196 | gimple_call_set_fntype (stmt, TREE_TYPE (fn)); | |
2197 | gimple_call_set_arg (stmt, 0, dest); | |
2198 | gimple_call_set_arg (stmt, 1, fmt); | |
2199 | for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i) | |
2200 | gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2)); | |
2201 | gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2); | |
2202 | fold_stmt (gsi); | |
2203 | return true; | |
2204 | } | |
2205 | ||
35770bb2 RB |
2206 | /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG. |
2207 | ORIG may be null if this is a 2-argument call. We don't attempt to | |
2208 | simplify calls with more than 3 arguments. | |
2209 | ||
2210 | Return NULL_TREE if no simplification was possible, otherwise return the | |
2211 | simplified form of the call as a tree. If IGNORED is true, it means that | |
2212 | the caller does not use the returned value of the function. */ | |
2213 | ||
2214 | static bool | |
dcb7fae2 | 2215 | gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi) |
35770bb2 RB |
2216 | { |
2217 | gimple stmt = gsi_stmt (*gsi); | |
2218 | tree dest = gimple_call_arg (stmt, 0); | |
2219 | tree fmt = gimple_call_arg (stmt, 1); | |
2220 | tree orig = NULL_TREE; | |
2221 | const char *fmt_str = NULL; | |
2222 | ||
2223 | /* Verify the required arguments in the original call. We deal with two | |
2224 | types of sprintf() calls: 'sprintf (str, fmt)' and | |
2225 | 'sprintf (dest, "%s", orig)'. */ | |
2226 | if (gimple_call_num_args (stmt) > 3) | |
2227 | return false; | |
2228 | ||
2229 | if (gimple_call_num_args (stmt) == 3) | |
2230 | orig = gimple_call_arg (stmt, 2); | |
2231 | ||
2232 | /* Check whether the format is a literal string constant. */ | |
2233 | fmt_str = c_getstr (fmt); | |
2234 | if (fmt_str == NULL) | |
2235 | return false; | |
2236 | ||
2237 | if (!init_target_chars ()) | |
2238 | return false; | |
2239 | ||
2240 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2241 | if (strchr (fmt_str, target_percent) == NULL) | |
2242 | { | |
2243 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2244 | ||
2245 | if (!fn) | |
2246 | return false; | |
2247 | ||
2248 | /* Don't optimize sprintf (buf, "abc", ptr++). */ | |
2249 | if (orig) | |
2250 | return false; | |
2251 | ||
2252 | /* Convert sprintf (str, fmt) into strcpy (str, fmt) when | |
2253 | 'format' is known to contain no % formats. */ | |
2254 | gimple_seq stmts = NULL; | |
2255 | gimple repl = gimple_build_call (fn, 2, dest, fmt); | |
2256 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2257 | if (gimple_call_lhs (stmt)) | |
2258 | { | |
2259 | repl = gimple_build_assign (gimple_call_lhs (stmt), | |
2260 | build_int_cst (integer_type_node, | |
2261 | strlen (fmt_str))); | |
2262 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2263 | gsi_replace_with_seq_vops (gsi, stmts); | |
2264 | /* gsi now points at the assignment to the lhs, get a | |
2265 | stmt iterator to the memcpy call. | |
2266 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2267 | CFG isn't built yet. */ | |
2268 | gimple_stmt_iterator gsi2 = *gsi; | |
2269 | gsi_prev (&gsi2); | |
2270 | fold_stmt (&gsi2); | |
2271 | } | |
2272 | else | |
2273 | { | |
2274 | gsi_replace_with_seq_vops (gsi, stmts); | |
2275 | fold_stmt (gsi); | |
2276 | } | |
2277 | return true; | |
2278 | } | |
2279 | ||
2280 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
2281 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) | |
2282 | { | |
2283 | tree fn; | |
2284 | fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2285 | ||
2286 | if (!fn) | |
2287 | return false; | |
2288 | ||
2289 | /* Don't crash on sprintf (str1, "%s"). */ | |
2290 | if (!orig) | |
2291 | return false; | |
2292 | ||
dcb7fae2 RB |
2293 | tree orig_len = NULL_TREE; |
2294 | if (gimple_call_lhs (stmt)) | |
35770bb2 | 2295 | { |
dcb7fae2 | 2296 | orig_len = get_maxval_strlen (orig, 0); |
d7e78447 | 2297 | if (!orig_len) |
35770bb2 RB |
2298 | return false; |
2299 | } | |
2300 | ||
2301 | /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */ | |
2302 | gimple_seq stmts = NULL; | |
2303 | gimple repl = gimple_build_call (fn, 2, dest, orig); | |
2304 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2305 | if (gimple_call_lhs (stmt)) | |
2306 | { | |
d7e78447 RB |
2307 | if (!useless_type_conversion_p (integer_type_node, |
2308 | TREE_TYPE (orig_len))) | |
2309 | orig_len = fold_convert (integer_type_node, orig_len); | |
2310 | repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); | |
35770bb2 RB |
2311 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2312 | gsi_replace_with_seq_vops (gsi, stmts); | |
2313 | /* gsi now points at the assignment to the lhs, get a | |
2314 | stmt iterator to the memcpy call. | |
2315 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2316 | CFG isn't built yet. */ | |
2317 | gimple_stmt_iterator gsi2 = *gsi; | |
2318 | gsi_prev (&gsi2); | |
2319 | fold_stmt (&gsi2); | |
2320 | } | |
2321 | else | |
2322 | { | |
2323 | gsi_replace_with_seq_vops (gsi, stmts); | |
2324 | fold_stmt (gsi); | |
2325 | } | |
2326 | return true; | |
2327 | } | |
2328 | return false; | |
2329 | } | |
2330 | ||
d7e78447 RB |
2331 | /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE, |
2332 | FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't | |
2333 | attempt to simplify calls with more than 4 arguments. | |
35770bb2 | 2334 | |
d7e78447 RB |
2335 | Return NULL_TREE if no simplification was possible, otherwise return the |
2336 | simplified form of the call as a tree. If IGNORED is true, it means that | |
2337 | the caller does not use the returned value of the function. */ | |
2338 | ||
2339 | static bool | |
dcb7fae2 | 2340 | gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi) |
d7e78447 | 2341 | { |
538dd0b7 | 2342 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
d7e78447 RB |
2343 | tree dest = gimple_call_arg (stmt, 0); |
2344 | tree destsize = gimple_call_arg (stmt, 1); | |
2345 | tree fmt = gimple_call_arg (stmt, 2); | |
2346 | tree orig = NULL_TREE; | |
2347 | const char *fmt_str = NULL; | |
2348 | ||
2349 | if (gimple_call_num_args (stmt) > 4) | |
2350 | return false; | |
2351 | ||
2352 | if (gimple_call_num_args (stmt) == 4) | |
2353 | orig = gimple_call_arg (stmt, 3); | |
2354 | ||
2355 | if (!tree_fits_uhwi_p (destsize)) | |
2356 | return false; | |
2357 | unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize); | |
2358 | ||
2359 | /* Check whether the format is a literal string constant. */ | |
2360 | fmt_str = c_getstr (fmt); | |
2361 | if (fmt_str == NULL) | |
2362 | return false; | |
2363 | ||
2364 | if (!init_target_chars ()) | |
2365 | return false; | |
2366 | ||
2367 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2368 | if (strchr (fmt_str, target_percent) == NULL) | |
2369 | { | |
2370 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2371 | if (!fn) | |
2372 | return false; | |
2373 | ||
2374 | /* Don't optimize snprintf (buf, 4, "abc", ptr++). */ | |
2375 | if (orig) | |
2376 | return false; | |
2377 | ||
2378 | /* We could expand this as | |
2379 | memcpy (str, fmt, cst - 1); str[cst - 1] = '\0'; | |
2380 | or to | |
2381 | memcpy (str, fmt_with_nul_at_cstm1, cst); | |
2382 | but in the former case that might increase code size | |
2383 | and in the latter case grow .rodata section too much. | |
2384 | So punt for now. */ | |
2385 | size_t len = strlen (fmt_str); | |
2386 | if (len >= destlen) | |
2387 | return false; | |
2388 | ||
2389 | gimple_seq stmts = NULL; | |
2390 | gimple repl = gimple_build_call (fn, 2, dest, fmt); | |
2391 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2392 | if (gimple_call_lhs (stmt)) | |
2393 | { | |
2394 | repl = gimple_build_assign (gimple_call_lhs (stmt), | |
2395 | build_int_cst (integer_type_node, len)); | |
2396 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2397 | gsi_replace_with_seq_vops (gsi, stmts); | |
2398 | /* gsi now points at the assignment to the lhs, get a | |
2399 | stmt iterator to the memcpy call. | |
2400 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2401 | CFG isn't built yet. */ | |
2402 | gimple_stmt_iterator gsi2 = *gsi; | |
2403 | gsi_prev (&gsi2); | |
2404 | fold_stmt (&gsi2); | |
2405 | } | |
2406 | else | |
2407 | { | |
2408 | gsi_replace_with_seq_vops (gsi, stmts); | |
2409 | fold_stmt (gsi); | |
2410 | } | |
2411 | return true; | |
2412 | } | |
2413 | ||
2414 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
2415 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) | |
2416 | { | |
2417 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2418 | if (!fn) | |
2419 | return false; | |
2420 | ||
2421 | /* Don't crash on snprintf (str1, cst, "%s"). */ | |
2422 | if (!orig) | |
2423 | return false; | |
2424 | ||
dcb7fae2 | 2425 | tree orig_len = get_maxval_strlen (orig, 0); |
af9db3a7 | 2426 | if (!orig_len || TREE_CODE (orig_len) != INTEGER_CST) |
dcb7fae2 | 2427 | return false; |
d7e78447 RB |
2428 | |
2429 | /* We could expand this as | |
2430 | memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0'; | |
2431 | or to | |
2432 | memcpy (str1, str2_with_nul_at_cstm1, cst); | |
2433 | but in the former case that might increase code size | |
2434 | and in the latter case grow .rodata section too much. | |
2435 | So punt for now. */ | |
2436 | if (compare_tree_int (orig_len, destlen) >= 0) | |
2437 | return false; | |
2438 | ||
2439 | /* Convert snprintf (str1, cst, "%s", str2) into | |
2440 | strcpy (str1, str2) if strlen (str2) < cst. */ | |
2441 | gimple_seq stmts = NULL; | |
2442 | gimple repl = gimple_build_call (fn, 2, dest, orig); | |
2443 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2444 | if (gimple_call_lhs (stmt)) | |
2445 | { | |
2446 | if (!useless_type_conversion_p (integer_type_node, | |
2447 | TREE_TYPE (orig_len))) | |
2448 | orig_len = fold_convert (integer_type_node, orig_len); | |
2449 | repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); | |
2450 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2451 | gsi_replace_with_seq_vops (gsi, stmts); | |
2452 | /* gsi now points at the assignment to the lhs, get a | |
2453 | stmt iterator to the memcpy call. | |
2454 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2455 | CFG isn't built yet. */ | |
2456 | gimple_stmt_iterator gsi2 = *gsi; | |
2457 | gsi_prev (&gsi2); | |
2458 | fold_stmt (&gsi2); | |
2459 | } | |
2460 | else | |
2461 | { | |
2462 | gsi_replace_with_seq_vops (gsi, stmts); | |
2463 | fold_stmt (gsi); | |
2464 | } | |
2465 | return true; | |
2466 | } | |
2467 | return false; | |
2468 | } | |
35770bb2 | 2469 | |
edd7ae68 RB |
2470 | /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins. |
2471 | FP, FMT, and ARG are the arguments to the call. We don't fold calls with | |
2472 | more than 3 arguments, and ARG may be null in the 2-argument case. | |
2473 | ||
2474 | Return NULL_TREE if no simplification was possible, otherwise return the | |
2475 | simplified form of the call as a tree. FCODE is the BUILT_IN_* | |
2476 | code of the function to be simplified. */ | |
2477 | ||
2478 | static bool | |
2479 | gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi, | |
2480 | tree fp, tree fmt, tree arg, | |
2481 | enum built_in_function fcode) | |
2482 | { | |
2483 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
2484 | tree fn_fputc, fn_fputs; | |
2485 | const char *fmt_str = NULL; | |
2486 | ||
2487 | /* If the return value is used, don't do the transformation. */ | |
2488 | if (gimple_call_lhs (stmt) != NULL_TREE) | |
2489 | return false; | |
2490 | ||
2491 | /* Check whether the format is a literal string constant. */ | |
2492 | fmt_str = c_getstr (fmt); | |
2493 | if (fmt_str == NULL) | |
2494 | return false; | |
2495 | ||
2496 | if (fcode == BUILT_IN_FPRINTF_UNLOCKED) | |
2497 | { | |
2498 | /* If we're using an unlocked function, assume the other | |
2499 | unlocked functions exist explicitly. */ | |
2500 | fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED); | |
2501 | fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED); | |
2502 | } | |
2503 | else | |
2504 | { | |
2505 | fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC); | |
2506 | fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS); | |
2507 | } | |
2508 | ||
2509 | if (!init_target_chars ()) | |
2510 | return false; | |
2511 | ||
2512 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2513 | if (strchr (fmt_str, target_percent) == NULL) | |
2514 | { | |
2515 | if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK | |
2516 | && arg) | |
2517 | return false; | |
2518 | ||
2519 | /* If the format specifier was "", fprintf does nothing. */ | |
2520 | if (fmt_str[0] == '\0') | |
2521 | { | |
2522 | replace_call_with_value (gsi, NULL_TREE); | |
2523 | return true; | |
2524 | } | |
2525 | ||
2526 | /* When "string" doesn't contain %, replace all cases of | |
2527 | fprintf (fp, string) with fputs (string, fp). The fputs | |
2528 | builtin will take care of special cases like length == 1. */ | |
2529 | if (fn_fputs) | |
2530 | { | |
2531 | gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp); | |
2532 | replace_call_with_call_and_fold (gsi, repl); | |
2533 | return true; | |
2534 | } | |
2535 | } | |
2536 | ||
2537 | /* The other optimizations can be done only on the non-va_list variants. */ | |
2538 | else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK) | |
2539 | return false; | |
2540 | ||
2541 | /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */ | |
2542 | else if (strcmp (fmt_str, target_percent_s) == 0) | |
2543 | { | |
2544 | if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg))) | |
2545 | return false; | |
2546 | if (fn_fputs) | |
2547 | { | |
2548 | gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp); | |
2549 | replace_call_with_call_and_fold (gsi, repl); | |
2550 | return true; | |
2551 | } | |
2552 | } | |
2553 | ||
2554 | /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */ | |
2555 | else if (strcmp (fmt_str, target_percent_c) == 0) | |
2556 | { | |
2557 | if (!arg | |
2558 | || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg))) | |
2559 | return false; | |
2560 | if (fn_fputc) | |
2561 | { | |
2562 | gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp); | |
2563 | replace_call_with_call_and_fold (gsi, repl); | |
2564 | return true; | |
2565 | } | |
2566 | } | |
2567 | ||
2568 | return false; | |
2569 | } | |
2570 | ||
ad03a744 RB |
2571 | /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins. |
2572 | FMT and ARG are the arguments to the call; we don't fold cases with | |
2573 | more than 2 arguments, and ARG may be null if this is a 1-argument case. | |
2574 | ||
2575 | Return NULL_TREE if no simplification was possible, otherwise return the | |
2576 | simplified form of the call as a tree. FCODE is the BUILT_IN_* | |
2577 | code of the function to be simplified. */ | |
2578 | ||
2579 | static bool | |
2580 | gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt, | |
2581 | tree arg, enum built_in_function fcode) | |
2582 | { | |
2583 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
2584 | tree fn_putchar, fn_puts, newarg; | |
2585 | const char *fmt_str = NULL; | |
2586 | ||
2587 | /* If the return value is used, don't do the transformation. */ | |
2588 | if (gimple_call_lhs (stmt) != NULL_TREE) | |
2589 | return false; | |
2590 | ||
2591 | /* Check whether the format is a literal string constant. */ | |
2592 | fmt_str = c_getstr (fmt); | |
2593 | if (fmt_str == NULL) | |
2594 | return false; | |
2595 | ||
2596 | if (fcode == BUILT_IN_PRINTF_UNLOCKED) | |
2597 | { | |
2598 | /* If we're using an unlocked function, assume the other | |
2599 | unlocked functions exist explicitly. */ | |
2600 | fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED); | |
2601 | fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED); | |
2602 | } | |
2603 | else | |
2604 | { | |
2605 | fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR); | |
2606 | fn_puts = builtin_decl_implicit (BUILT_IN_PUTS); | |
2607 | } | |
2608 | ||
2609 | if (!init_target_chars ()) | |
2610 | return false; | |
2611 | ||
2612 | if (strcmp (fmt_str, target_percent_s) == 0 | |
2613 | || strchr (fmt_str, target_percent) == NULL) | |
2614 | { | |
2615 | const char *str; | |
2616 | ||
2617 | if (strcmp (fmt_str, target_percent_s) == 0) | |
2618 | { | |
2619 | if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
2620 | return false; | |
2621 | ||
2622 | if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg))) | |
2623 | return false; | |
2624 | ||
2625 | str = c_getstr (arg); | |
2626 | if (str == NULL) | |
2627 | return false; | |
2628 | } | |
2629 | else | |
2630 | { | |
2631 | /* The format specifier doesn't contain any '%' characters. */ | |
2632 | if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK | |
2633 | && arg) | |
2634 | return false; | |
2635 | str = fmt_str; | |
2636 | } | |
2637 | ||
2638 | /* If the string was "", printf does nothing. */ | |
2639 | if (str[0] == '\0') | |
2640 | { | |
2641 | replace_call_with_value (gsi, NULL_TREE); | |
2642 | return true; | |
2643 | } | |
2644 | ||
2645 | /* If the string has length of 1, call putchar. */ | |
2646 | if (str[1] == '\0') | |
2647 | { | |
2648 | /* Given printf("c"), (where c is any one character,) | |
2649 | convert "c"[0] to an int and pass that to the replacement | |
2650 | function. */ | |
2651 | newarg = build_int_cst (integer_type_node, str[0]); | |
2652 | if (fn_putchar) | |
2653 | { | |
2654 | gcall *repl = gimple_build_call (fn_putchar, 1, newarg); | |
2655 | replace_call_with_call_and_fold (gsi, repl); | |
2656 | return true; | |
2657 | } | |
2658 | } | |
2659 | else | |
2660 | { | |
2661 | /* If the string was "string\n", call puts("string"). */ | |
2662 | size_t len = strlen (str); | |
2663 | if ((unsigned char)str[len - 1] == target_newline | |
2664 | && (size_t) (int) len == len | |
2665 | && (int) len > 0) | |
2666 | { | |
2667 | char *newstr; | |
2668 | tree offset_node, string_cst; | |
2669 | ||
2670 | /* Create a NUL-terminated string that's one char shorter | |
2671 | than the original, stripping off the trailing '\n'. */ | |
2672 | newarg = build_string_literal (len, str); | |
2673 | string_cst = string_constant (newarg, &offset_node); | |
2674 | gcc_checking_assert (string_cst | |
2675 | && (TREE_STRING_LENGTH (string_cst) | |
2676 | == (int) len) | |
2677 | && integer_zerop (offset_node) | |
2678 | && (unsigned char) | |
2679 | TREE_STRING_POINTER (string_cst)[len - 1] | |
2680 | == target_newline); | |
2681 | /* build_string_literal creates a new STRING_CST, | |
2682 | modify it in place to avoid double copying. */ | |
2683 | newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst)); | |
2684 | newstr[len - 1] = '\0'; | |
2685 | if (fn_puts) | |
2686 | { | |
2687 | gcall *repl = gimple_build_call (fn_puts, 1, newarg); | |
2688 | replace_call_with_call_and_fold (gsi, repl); | |
2689 | return true; | |
2690 | } | |
2691 | } | |
2692 | else | |
2693 | /* We'd like to arrange to call fputs(string,stdout) here, | |
2694 | but we need stdout and don't have a way to get it yet. */ | |
2695 | return false; | |
2696 | } | |
2697 | } | |
2698 | ||
2699 | /* The other optimizations can be done only on the non-va_list variants. */ | |
2700 | else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
2701 | return false; | |
2702 | ||
2703 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ | |
2704 | else if (strcmp (fmt_str, target_percent_s_newline) == 0) | |
2705 | { | |
2706 | if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg))) | |
2707 | return false; | |
2708 | if (fn_puts) | |
2709 | { | |
2710 | gcall *repl = gimple_build_call (fn_puts, 1, arg); | |
2711 | replace_call_with_call_and_fold (gsi, repl); | |
2712 | return true; | |
2713 | } | |
2714 | } | |
2715 | ||
2716 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
2717 | else if (strcmp (fmt_str, target_percent_c) == 0) | |
2718 | { | |
2719 | if (!arg || ! useless_type_conversion_p (integer_type_node, | |
2720 | TREE_TYPE (arg))) | |
2721 | return false; | |
2722 | if (fn_putchar) | |
2723 | { | |
2724 | gcall *repl = gimple_build_call (fn_putchar, 1, arg); | |
2725 | replace_call_with_call_and_fold (gsi, repl); | |
2726 | return true; | |
2727 | } | |
2728 | } | |
2729 | ||
2730 | return false; | |
2731 | } | |
2732 | ||
edd7ae68 | 2733 | |
fef5a0d9 RB |
2734 | |
2735 | /* Fold a call to __builtin_strlen with known length LEN. */ | |
2736 | ||
2737 | static bool | |
dcb7fae2 | 2738 | gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi) |
fef5a0d9 | 2739 | { |
dcb7fae2 RB |
2740 | gimple stmt = gsi_stmt (*gsi); |
2741 | tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0); | |
fef5a0d9 RB |
2742 | if (!len) |
2743 | return false; | |
2813904b | 2744 | len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT); |
fef5a0d9 RB |
2745 | replace_call_with_value (gsi, len); |
2746 | return true; | |
cbdd87d4 RG |
2747 | } |
2748 | ||
2749 | ||
dcb7fae2 RB |
2750 | /* Fold the non-target builtin at *GSI and return whether any simplification |
2751 | was made. */ | |
cbdd87d4 | 2752 | |
fef5a0d9 | 2753 | static bool |
dcb7fae2 | 2754 | gimple_fold_builtin (gimple_stmt_iterator *gsi) |
cbdd87d4 | 2755 | { |
538dd0b7 | 2756 | gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi)); |
fef5a0d9 | 2757 | tree callee = gimple_call_fndecl (stmt); |
cbdd87d4 | 2758 | |
dcb7fae2 RB |
2759 | /* Give up for always_inline inline builtins until they are |
2760 | inlined. */ | |
2761 | if (avoid_folding_inline_builtin (callee)) | |
2762 | return false; | |
cbdd87d4 | 2763 | |
edd7ae68 RB |
2764 | unsigned n = gimple_call_num_args (stmt); |
2765 | enum built_in_function fcode = DECL_FUNCTION_CODE (callee); | |
2766 | switch (fcode) | |
cbdd87d4 | 2767 | { |
dcb7fae2 RB |
2768 | case BUILT_IN_BZERO: |
2769 | return gimple_fold_builtin_memset (gsi, integer_zero_node, | |
2770 | gimple_call_arg (stmt, 1)); | |
2771 | case BUILT_IN_MEMSET: | |
2772 | return gimple_fold_builtin_memset (gsi, | |
2773 | gimple_call_arg (stmt, 1), | |
2774 | gimple_call_arg (stmt, 2)); | |
2775 | case BUILT_IN_BCOPY: | |
2776 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 1), | |
2777 | gimple_call_arg (stmt, 0), 3); | |
2778 | case BUILT_IN_MEMCPY: | |
2779 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
2780 | gimple_call_arg (stmt, 1), 0); | |
2781 | case BUILT_IN_MEMPCPY: | |
2782 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
2783 | gimple_call_arg (stmt, 1), 1); | |
2784 | case BUILT_IN_MEMMOVE: | |
2785 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
2786 | gimple_call_arg (stmt, 1), 3); | |
2787 | case BUILT_IN_SPRINTF_CHK: | |
2788 | case BUILT_IN_VSPRINTF_CHK: | |
edd7ae68 | 2789 | return gimple_fold_builtin_sprintf_chk (gsi, fcode); |
dcb7fae2 RB |
2790 | case BUILT_IN_STRCAT_CHK: |
2791 | return gimple_fold_builtin_strcat_chk (gsi); | |
745583f9 RB |
2792 | case BUILT_IN_STRNCAT_CHK: |
2793 | return gimple_fold_builtin_strncat_chk (gsi); | |
cbdd87d4 | 2794 | case BUILT_IN_STRLEN: |
dcb7fae2 | 2795 | return gimple_fold_builtin_strlen (gsi); |
cbdd87d4 | 2796 | case BUILT_IN_STRCPY: |
dcb7fae2 | 2797 | return gimple_fold_builtin_strcpy (gsi, |
fef5a0d9 | 2798 | gimple_call_arg (stmt, 0), |
dcb7fae2 | 2799 | gimple_call_arg (stmt, 1)); |
cbdd87d4 | 2800 | case BUILT_IN_STRNCPY: |
dcb7fae2 | 2801 | return gimple_fold_builtin_strncpy (gsi, |
fef5a0d9 RB |
2802 | gimple_call_arg (stmt, 0), |
2803 | gimple_call_arg (stmt, 1), | |
dcb7fae2 | 2804 | gimple_call_arg (stmt, 2)); |
9a7eefec | 2805 | case BUILT_IN_STRCAT: |
dcb7fae2 RB |
2806 | return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0), |
2807 | gimple_call_arg (stmt, 1)); | |
ad03a744 RB |
2808 | case BUILT_IN_STRNCAT: |
2809 | return gimple_fold_builtin_strncat (gsi); | |
cbdd87d4 | 2810 | case BUILT_IN_FPUTS: |
dcb7fae2 RB |
2811 | return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0), |
2812 | gimple_call_arg (stmt, 1), false); | |
cbdd87d4 | 2813 | case BUILT_IN_FPUTS_UNLOCKED: |
dcb7fae2 RB |
2814 | return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0), |
2815 | gimple_call_arg (stmt, 1), true); | |
cbdd87d4 RG |
2816 | case BUILT_IN_MEMCPY_CHK: |
2817 | case BUILT_IN_MEMPCPY_CHK: | |
2818 | case BUILT_IN_MEMMOVE_CHK: | |
2819 | case BUILT_IN_MEMSET_CHK: | |
dcb7fae2 | 2820 | return gimple_fold_builtin_memory_chk (gsi, |
fef5a0d9 RB |
2821 | gimple_call_arg (stmt, 0), |
2822 | gimple_call_arg (stmt, 1), | |
2823 | gimple_call_arg (stmt, 2), | |
2824 | gimple_call_arg (stmt, 3), | |
edd7ae68 | 2825 | fcode); |
2625bb5d RB |
2826 | case BUILT_IN_STPCPY: |
2827 | return gimple_fold_builtin_stpcpy (gsi); | |
cbdd87d4 RG |
2828 | case BUILT_IN_STRCPY_CHK: |
2829 | case BUILT_IN_STPCPY_CHK: | |
dcb7fae2 | 2830 | return gimple_fold_builtin_stxcpy_chk (gsi, |
fef5a0d9 RB |
2831 | gimple_call_arg (stmt, 0), |
2832 | gimple_call_arg (stmt, 1), | |
2833 | gimple_call_arg (stmt, 2), | |
edd7ae68 | 2834 | fcode); |
cbdd87d4 | 2835 | case BUILT_IN_STRNCPY_CHK: |
f3fc9b80 | 2836 | case BUILT_IN_STPNCPY_CHK: |
fef5a0d9 RB |
2837 | return gimple_fold_builtin_stxncpy_chk (gsi, |
2838 | gimple_call_arg (stmt, 0), | |
2839 | gimple_call_arg (stmt, 1), | |
2840 | gimple_call_arg (stmt, 2), | |
2841 | gimple_call_arg (stmt, 3), | |
edd7ae68 | 2842 | fcode); |
cbdd87d4 RG |
2843 | case BUILT_IN_SNPRINTF_CHK: |
2844 | case BUILT_IN_VSNPRINTF_CHK: | |
edd7ae68 | 2845 | return gimple_fold_builtin_snprintf_chk (gsi, fcode); |
d7e78447 | 2846 | case BUILT_IN_SNPRINTF: |
dcb7fae2 | 2847 | return gimple_fold_builtin_snprintf (gsi); |
d7e78447 | 2848 | case BUILT_IN_SPRINTF: |
dcb7fae2 | 2849 | return gimple_fold_builtin_sprintf (gsi); |
edd7ae68 RB |
2850 | case BUILT_IN_FPRINTF: |
2851 | case BUILT_IN_FPRINTF_UNLOCKED: | |
2852 | case BUILT_IN_VFPRINTF: | |
2853 | if (n == 2 || n == 3) | |
2854 | return gimple_fold_builtin_fprintf (gsi, | |
2855 | gimple_call_arg (stmt, 0), | |
2856 | gimple_call_arg (stmt, 1), | |
2857 | n == 3 | |
2858 | ? gimple_call_arg (stmt, 2) | |
2859 | : NULL_TREE, | |
2860 | fcode); | |
2861 | break; | |
2862 | case BUILT_IN_FPRINTF_CHK: | |
2863 | case BUILT_IN_VFPRINTF_CHK: | |
2864 | if (n == 3 || n == 4) | |
2865 | return gimple_fold_builtin_fprintf (gsi, | |
2866 | gimple_call_arg (stmt, 0), | |
2867 | gimple_call_arg (stmt, 2), | |
2868 | n == 4 | |
2869 | ? gimple_call_arg (stmt, 3) | |
2870 | : NULL_TREE, | |
2871 | fcode); | |
2872 | break; | |
ad03a744 RB |
2873 | case BUILT_IN_PRINTF: |
2874 | case BUILT_IN_PRINTF_UNLOCKED: | |
2875 | case BUILT_IN_VPRINTF: | |
2876 | if (n == 1 || n == 2) | |
2877 | return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0), | |
2878 | n == 2 | |
2879 | ? gimple_call_arg (stmt, 1) | |
2880 | : NULL_TREE, fcode); | |
2881 | break; | |
2882 | case BUILT_IN_PRINTF_CHK: | |
2883 | case BUILT_IN_VPRINTF_CHK: | |
2884 | if (n == 2 || n == 3) | |
2885 | return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1), | |
2886 | n == 3 | |
2887 | ? gimple_call_arg (stmt, 2) | |
2888 | : NULL_TREE, fcode); | |
fef5a0d9 RB |
2889 | default:; |
2890 | } | |
2891 | ||
2892 | /* Try the generic builtin folder. */ | |
2893 | bool ignore = (gimple_call_lhs (stmt) == NULL); | |
2894 | tree result = fold_call_stmt (stmt, ignore); | |
2895 | if (result) | |
2896 | { | |
2897 | if (ignore) | |
2898 | STRIP_NOPS (result); | |
2899 | else | |
2900 | result = fold_convert (gimple_call_return_type (stmt), result); | |
2901 | if (!update_call_from_tree (gsi, result)) | |
2902 | gimplify_and_update_call_from_tree (gsi, result); | |
2903 | return true; | |
2904 | } | |
2905 | ||
2906 | return false; | |
2907 | } | |
2908 | ||
1304953e JJ |
2909 | /* Return true if ARG0 CODE ARG1 in infinite signed precision operation |
2910 | doesn't fit into TYPE. The test for overflow should be regardless of | |
2911 | -fwrapv, and even for unsigned types. */ | |
2912 | ||
2913 | bool | |
2914 | arith_overflowed_p (enum tree_code code, const_tree type, | |
2915 | const_tree arg0, const_tree arg1) | |
2916 | { | |
2917 | typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int; | |
2918 | typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > | |
2919 | widest2_int_cst; | |
2920 | widest2_int warg0 = widest2_int_cst (arg0); | |
2921 | widest2_int warg1 = widest2_int_cst (arg1); | |
2922 | widest2_int wres; | |
2923 | switch (code) | |
2924 | { | |
2925 | case PLUS_EXPR: wres = wi::add (warg0, warg1); break; | |
2926 | case MINUS_EXPR: wres = wi::sub (warg0, warg1); break; | |
2927 | case MULT_EXPR: wres = wi::mul (warg0, warg1); break; | |
2928 | default: gcc_unreachable (); | |
2929 | } | |
2930 | signop sign = TYPE_SIGN (type); | |
2931 | if (sign == UNSIGNED && wi::neg_p (wres)) | |
2932 | return true; | |
2933 | return wi::min_precision (wres, sign) > TYPE_PRECISION (type); | |
2934 | } | |
2935 | ||
cbdd87d4 RG |
2936 | /* Attempt to fold a call statement referenced by the statement iterator GSI. |
2937 | The statement may be replaced by another statement, e.g., if the call | |
2938 | simplifies to a constant value. Return true if any changes were made. | |
2939 | It is assumed that the operands have been previously folded. */ | |
2940 | ||
e021c122 | 2941 | static bool |
ceeffab0 | 2942 | gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace) |
cbdd87d4 | 2943 | { |
538dd0b7 | 2944 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
3b45a007 | 2945 | tree callee; |
e021c122 RG |
2946 | bool changed = false; |
2947 | unsigned i; | |
cbdd87d4 | 2948 | |
e021c122 RG |
2949 | /* Fold *& in call arguments. */ |
2950 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
2951 | if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i))) | |
2952 | { | |
2953 | tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false); | |
2954 | if (tmp) | |
2955 | { | |
2956 | gimple_call_set_arg (stmt, i, tmp); | |
2957 | changed = true; | |
2958 | } | |
2959 | } | |
3b45a007 RG |
2960 | |
2961 | /* Check for virtual calls that became direct calls. */ | |
2962 | callee = gimple_call_fn (stmt); | |
25583c4f | 2963 | if (callee && TREE_CODE (callee) == OBJ_TYPE_REF) |
3b45a007 | 2964 | { |
49c471e3 MJ |
2965 | if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE) |
2966 | { | |
450ad0cd JH |
2967 | if (dump_file && virtual_method_call_p (callee) |
2968 | && !possible_polymorphic_call_target_p | |
6f8091fc JH |
2969 | (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl |
2970 | (OBJ_TYPE_REF_EXPR (callee))))) | |
450ad0cd JH |
2971 | { |
2972 | fprintf (dump_file, | |
a70e9985 | 2973 | "Type inheritance inconsistent devirtualization of "); |
450ad0cd JH |
2974 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
2975 | fprintf (dump_file, " to "); | |
2976 | print_generic_expr (dump_file, callee, TDF_SLIM); | |
2977 | fprintf (dump_file, "\n"); | |
2978 | } | |
2979 | ||
49c471e3 | 2980 | gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee)); |
e021c122 RG |
2981 | changed = true; |
2982 | } | |
a70e9985 | 2983 | else if (flag_devirtualize && !inplace && virtual_method_call_p (callee)) |
e021c122 | 2984 | { |
61dd6a2e JH |
2985 | bool final; |
2986 | vec <cgraph_node *>targets | |
058d0a90 | 2987 | = possible_polymorphic_call_targets (callee, stmt, &final); |
2b5f0895 | 2988 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
e021c122 | 2989 | { |
a70e9985 | 2990 | tree lhs = gimple_call_lhs (stmt); |
2b5f0895 XDL |
2991 | if (dump_enabled_p ()) |
2992 | { | |
807b7d62 | 2993 | location_t loc = gimple_location_safe (stmt); |
2b5f0895 XDL |
2994 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
2995 | "folding virtual function call to %s\n", | |
2996 | targets.length () == 1 | |
2997 | ? targets[0]->name () | |
2998 | : "__builtin_unreachable"); | |
2999 | } | |
61dd6a2e | 3000 | if (targets.length () == 1) |
cf3e5a89 JJ |
3001 | { |
3002 | gimple_call_set_fndecl (stmt, targets[0]->decl); | |
3003 | changed = true; | |
a70e9985 JJ |
3004 | /* If the call becomes noreturn, remove the lhs. */ |
3005 | if (lhs && (gimple_call_flags (stmt) & ECF_NORETURN)) | |
3006 | { | |
3007 | if (TREE_CODE (lhs) == SSA_NAME) | |
3008 | { | |
b731b390 | 3009 | tree var = create_tmp_var (TREE_TYPE (lhs)); |
a70e9985 JJ |
3010 | tree def = get_or_create_ssa_default_def (cfun, var); |
3011 | gimple new_stmt = gimple_build_assign (lhs, def); | |
3012 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
3013 | } | |
3014 | gimple_call_set_lhs (stmt, NULL_TREE); | |
3015 | } | |
0b986c6a | 3016 | maybe_remove_unused_call_args (cfun, stmt); |
cf3e5a89 | 3017 | } |
a70e9985 | 3018 | else |
cf3e5a89 JJ |
3019 | { |
3020 | tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
3021 | gimple new_stmt = gimple_build_call (fndecl, 0); | |
3022 | gimple_set_location (new_stmt, gimple_location (stmt)); | |
a70e9985 JJ |
3023 | if (lhs && TREE_CODE (lhs) == SSA_NAME) |
3024 | { | |
b731b390 | 3025 | tree var = create_tmp_var (TREE_TYPE (lhs)); |
a70e9985 | 3026 | tree def = get_or_create_ssa_default_def (cfun, var); |
eb14a79f ML |
3027 | |
3028 | /* To satisfy condition for | |
3029 | cgraph_update_edges_for_call_stmt_node, | |
3030 | we need to preserve GIMPLE_CALL statement | |
3031 | at position of GSI iterator. */ | |
a70e9985 | 3032 | update_call_from_tree (gsi, def); |
eb14a79f | 3033 | gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT); |
a70e9985 JJ |
3034 | } |
3035 | else | |
42e52a51 RB |
3036 | { |
3037 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
3038 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
3039 | gsi_replace (gsi, new_stmt, false); | |
3040 | } | |
cf3e5a89 JJ |
3041 | return true; |
3042 | } | |
e021c122 | 3043 | } |
49c471e3 | 3044 | } |
e021c122 | 3045 | } |
49c471e3 | 3046 | |
f2d3d07e RH |
3047 | /* Check for indirect calls that became direct calls, and then |
3048 | no longer require a static chain. */ | |
3049 | if (gimple_call_chain (stmt)) | |
3050 | { | |
3051 | tree fn = gimple_call_fndecl (stmt); | |
3052 | if (fn && !DECL_STATIC_CHAIN (fn)) | |
3053 | { | |
3054 | gimple_call_set_chain (stmt, NULL); | |
3055 | changed = true; | |
3056 | } | |
3057 | else | |
3058 | { | |
3059 | tree tmp = maybe_fold_reference (gimple_call_chain (stmt), false); | |
3060 | if (tmp) | |
3061 | { | |
3062 | gimple_call_set_chain (stmt, tmp); | |
3063 | changed = true; | |
3064 | } | |
3065 | } | |
3066 | } | |
3067 | ||
e021c122 RG |
3068 | if (inplace) |
3069 | return changed; | |
3070 | ||
3071 | /* Check for builtins that CCP can handle using information not | |
3072 | available in the generic fold routines. */ | |
fef5a0d9 RB |
3073 | if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) |
3074 | { | |
3075 | if (gimple_fold_builtin (gsi)) | |
3076 | changed = true; | |
3077 | } | |
3078 | else if (gimple_call_builtin_p (stmt, BUILT_IN_MD)) | |
e021c122 | 3079 | { |
ea679d55 | 3080 | changed |= targetm.gimple_fold_builtin (gsi); |
3b45a007 | 3081 | } |
368b454d | 3082 | else if (gimple_call_internal_p (stmt)) |
ed9c79e1 | 3083 | { |
368b454d JJ |
3084 | enum tree_code subcode = ERROR_MARK; |
3085 | tree result = NULL_TREE; | |
1304953e JJ |
3086 | bool cplx_result = false; |
3087 | tree overflow = NULL_TREE; | |
368b454d JJ |
3088 | switch (gimple_call_internal_fn (stmt)) |
3089 | { | |
3090 | case IFN_BUILTIN_EXPECT: | |
3091 | result = fold_builtin_expect (gimple_location (stmt), | |
3092 | gimple_call_arg (stmt, 0), | |
3093 | gimple_call_arg (stmt, 1), | |
3094 | gimple_call_arg (stmt, 2)); | |
3095 | break; | |
0e82f089 MP |
3096 | case IFN_UBSAN_OBJECT_SIZE: |
3097 | if (integer_all_onesp (gimple_call_arg (stmt, 2)) | |
3098 | || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST | |
3099 | && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST | |
3100 | && tree_int_cst_le (gimple_call_arg (stmt, 1), | |
3101 | gimple_call_arg (stmt, 2)))) | |
3102 | { | |
3103 | gsi_replace (gsi, gimple_build_nop (), true); | |
3104 | unlink_stmt_vdef (stmt); | |
3105 | release_defs (stmt); | |
3106 | return true; | |
3107 | } | |
3108 | break; | |
368b454d JJ |
3109 | case IFN_UBSAN_CHECK_ADD: |
3110 | subcode = PLUS_EXPR; | |
3111 | break; | |
3112 | case IFN_UBSAN_CHECK_SUB: | |
3113 | subcode = MINUS_EXPR; | |
3114 | break; | |
3115 | case IFN_UBSAN_CHECK_MUL: | |
3116 | subcode = MULT_EXPR; | |
3117 | break; | |
1304953e JJ |
3118 | case IFN_ADD_OVERFLOW: |
3119 | subcode = PLUS_EXPR; | |
3120 | cplx_result = true; | |
3121 | break; | |
3122 | case IFN_SUB_OVERFLOW: | |
3123 | subcode = MINUS_EXPR; | |
3124 | cplx_result = true; | |
3125 | break; | |
3126 | case IFN_MUL_OVERFLOW: | |
3127 | subcode = MULT_EXPR; | |
3128 | cplx_result = true; | |
3129 | break; | |
368b454d JJ |
3130 | default: |
3131 | break; | |
3132 | } | |
3133 | if (subcode != ERROR_MARK) | |
3134 | { | |
3135 | tree arg0 = gimple_call_arg (stmt, 0); | |
3136 | tree arg1 = gimple_call_arg (stmt, 1); | |
1304953e JJ |
3137 | tree type = TREE_TYPE (arg0); |
3138 | if (cplx_result) | |
3139 | { | |
3140 | tree lhs = gimple_call_lhs (stmt); | |
3141 | if (lhs == NULL_TREE) | |
3142 | type = NULL_TREE; | |
3143 | else | |
3144 | type = TREE_TYPE (TREE_TYPE (lhs)); | |
3145 | } | |
3146 | if (type == NULL_TREE) | |
3147 | ; | |
368b454d | 3148 | /* x = y + 0; x = y - 0; x = y * 0; */ |
1304953e JJ |
3149 | else if (integer_zerop (arg1)) |
3150 | result = subcode == MULT_EXPR ? integer_zero_node : arg0; | |
368b454d JJ |
3151 | /* x = 0 + y; x = 0 * y; */ |
3152 | else if (subcode != MINUS_EXPR && integer_zerop (arg0)) | |
1304953e | 3153 | result = subcode == MULT_EXPR ? integer_zero_node : arg1; |
368b454d JJ |
3154 | /* x = y - y; */ |
3155 | else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0)) | |
1304953e | 3156 | result = integer_zero_node; |
368b454d | 3157 | /* x = y * 1; x = 1 * y; */ |
1304953e JJ |
3158 | else if (subcode == MULT_EXPR && integer_onep (arg1)) |
3159 | result = arg0; | |
3160 | else if (subcode == MULT_EXPR && integer_onep (arg0)) | |
3161 | result = arg1; | |
3162 | else if (TREE_CODE (arg0) == INTEGER_CST | |
3163 | && TREE_CODE (arg1) == INTEGER_CST) | |
368b454d | 3164 | { |
1304953e JJ |
3165 | if (cplx_result) |
3166 | result = int_const_binop (subcode, fold_convert (type, arg0), | |
3167 | fold_convert (type, arg1)); | |
3168 | else | |
3169 | result = int_const_binop (subcode, arg0, arg1); | |
3170 | if (result && arith_overflowed_p (subcode, type, arg0, arg1)) | |
3171 | { | |
3172 | if (cplx_result) | |
3173 | overflow = build_one_cst (type); | |
3174 | else | |
3175 | result = NULL_TREE; | |
3176 | } | |
3177 | } | |
3178 | if (result) | |
3179 | { | |
3180 | if (result == integer_zero_node) | |
3181 | result = build_zero_cst (type); | |
3182 | else if (cplx_result && TREE_TYPE (result) != type) | |
3183 | { | |
3184 | if (TREE_CODE (result) == INTEGER_CST) | |
3185 | { | |
3186 | if (arith_overflowed_p (PLUS_EXPR, type, result, | |
3187 | integer_zero_node)) | |
3188 | overflow = build_one_cst (type); | |
3189 | } | |
3190 | else if ((!TYPE_UNSIGNED (TREE_TYPE (result)) | |
3191 | && TYPE_UNSIGNED (type)) | |
3192 | || (TYPE_PRECISION (type) | |
3193 | < (TYPE_PRECISION (TREE_TYPE (result)) | |
3194 | + (TYPE_UNSIGNED (TREE_TYPE (result)) | |
3195 | && !TYPE_UNSIGNED (type))))) | |
3196 | result = NULL_TREE; | |
3197 | if (result) | |
3198 | result = fold_convert (type, result); | |
3199 | } | |
368b454d JJ |
3200 | } |
3201 | } | |
1304953e | 3202 | |
ed9c79e1 JJ |
3203 | if (result) |
3204 | { | |
1304953e JJ |
3205 | if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result)) |
3206 | result = drop_tree_overflow (result); | |
3207 | if (cplx_result) | |
3208 | { | |
3209 | if (overflow == NULL_TREE) | |
3210 | overflow = build_zero_cst (TREE_TYPE (result)); | |
3211 | tree ctype = build_complex_type (TREE_TYPE (result)); | |
3212 | if (TREE_CODE (result) == INTEGER_CST | |
3213 | && TREE_CODE (overflow) == INTEGER_CST) | |
3214 | result = build_complex (ctype, result, overflow); | |
3215 | else | |
3216 | result = build2_loc (gimple_location (stmt), COMPLEX_EXPR, | |
3217 | ctype, result, overflow); | |
3218 | } | |
ed9c79e1 JJ |
3219 | if (!update_call_from_tree (gsi, result)) |
3220 | gimplify_and_update_call_from_tree (gsi, result); | |
3221 | changed = true; | |
3222 | } | |
3223 | } | |
3b45a007 | 3224 | |
e021c122 | 3225 | return changed; |
cbdd87d4 RG |
3226 | } |
3227 | ||
e0ee10ed | 3228 | |
89a79e96 RB |
3229 | /* Return true whether NAME has a use on STMT. */ |
3230 | ||
3231 | static bool | |
3232 | has_use_on_stmt (tree name, gimple stmt) | |
3233 | { | |
3234 | imm_use_iterator iter; | |
3235 | use_operand_p use_p; | |
3236 | FOR_EACH_IMM_USE_FAST (use_p, iter, name) | |
3237 | if (USE_STMT (use_p) == stmt) | |
3238 | return true; | |
3239 | return false; | |
3240 | } | |
3241 | ||
e0ee10ed RB |
3242 | /* Worker for fold_stmt_1 dispatch to pattern based folding with |
3243 | gimple_simplify. | |
3244 | ||
3245 | Replaces *GSI with the simplification result in RCODE and OPS | |
3246 | and the associated statements in *SEQ. Does the replacement | |
3247 | according to INPLACE and returns true if the operation succeeded. */ | |
3248 | ||
3249 | static bool | |
3250 | replace_stmt_with_simplification (gimple_stmt_iterator *gsi, | |
3251 | code_helper rcode, tree *ops, | |
3252 | gimple_seq *seq, bool inplace) | |
3253 | { | |
3254 | gimple stmt = gsi_stmt (*gsi); | |
3255 | ||
3256 | /* Play safe and do not allow abnormals to be mentioned in | |
89a79e96 RB |
3257 | newly created statements. See also maybe_push_res_to_seq. |
3258 | As an exception allow such uses if there was a use of the | |
3259 | same SSA name on the old stmt. */ | |
e0ee10ed | 3260 | if ((TREE_CODE (ops[0]) == SSA_NAME |
89a79e96 RB |
3261 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[0]) |
3262 | && !has_use_on_stmt (ops[0], stmt)) | |
e0ee10ed RB |
3263 | || (ops[1] |
3264 | && TREE_CODE (ops[1]) == SSA_NAME | |
89a79e96 RB |
3265 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[1]) |
3266 | && !has_use_on_stmt (ops[1], stmt)) | |
e0ee10ed RB |
3267 | || (ops[2] |
3268 | && TREE_CODE (ops[2]) == SSA_NAME | |
89a79e96 RB |
3269 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[2]) |
3270 | && !has_use_on_stmt (ops[2], stmt))) | |
e0ee10ed RB |
3271 | return false; |
3272 | ||
538dd0b7 | 3273 | if (gcond *cond_stmt = dyn_cast <gcond *> (stmt)) |
e0ee10ed RB |
3274 | { |
3275 | gcc_assert (rcode.is_tree_code ()); | |
3276 | if (TREE_CODE_CLASS ((enum tree_code)rcode) == tcc_comparison | |
3277 | /* GIMPLE_CONDs condition may not throw. */ | |
3278 | && (!flag_exceptions | |
3279 | || !cfun->can_throw_non_call_exceptions | |
3280 | || !operation_could_trap_p (rcode, | |
3281 | FLOAT_TYPE_P (TREE_TYPE (ops[0])), | |
3282 | false, NULL_TREE))) | |
538dd0b7 | 3283 | gimple_cond_set_condition (cond_stmt, rcode, ops[0], ops[1]); |
e0ee10ed | 3284 | else if (rcode == SSA_NAME) |
538dd0b7 | 3285 | gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0], |
e0ee10ed RB |
3286 | build_zero_cst (TREE_TYPE (ops[0]))); |
3287 | else if (rcode == INTEGER_CST) | |
3288 | { | |
3289 | if (integer_zerop (ops[0])) | |
538dd0b7 | 3290 | gimple_cond_make_false (cond_stmt); |
e0ee10ed | 3291 | else |
538dd0b7 | 3292 | gimple_cond_make_true (cond_stmt); |
e0ee10ed RB |
3293 | } |
3294 | else if (!inplace) | |
3295 | { | |
3296 | tree res = maybe_push_res_to_seq (rcode, boolean_type_node, | |
3297 | ops, seq); | |
3298 | if (!res) | |
3299 | return false; | |
538dd0b7 | 3300 | gimple_cond_set_condition (cond_stmt, NE_EXPR, res, |
e0ee10ed RB |
3301 | build_zero_cst (TREE_TYPE (res))); |
3302 | } | |
3303 | else | |
3304 | return false; | |
3305 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3306 | { | |
3307 | fprintf (dump_file, "gimple_simplified to "); | |
3308 | if (!gimple_seq_empty_p (*seq)) | |
3309 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
3310 | print_gimple_stmt (dump_file, gsi_stmt (*gsi), | |
3311 | 0, TDF_SLIM); | |
3312 | } | |
3313 | gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT); | |
3314 | return true; | |
3315 | } | |
3316 | else if (is_gimple_assign (stmt) | |
3317 | && rcode.is_tree_code ()) | |
3318 | { | |
3319 | if (!inplace | |
f3582e54 | 3320 | || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (rcode)) |
e0ee10ed RB |
3321 | { |
3322 | maybe_build_generic_op (rcode, | |
3323 | TREE_TYPE (gimple_assign_lhs (stmt)), | |
3324 | &ops[0], ops[1], ops[2]); | |
00d66391 | 3325 | gimple_assign_set_rhs_with_ops (gsi, rcode, ops[0], ops[1], ops[2]); |
e0ee10ed RB |
3326 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3327 | { | |
3328 | fprintf (dump_file, "gimple_simplified to "); | |
3329 | if (!gimple_seq_empty_p (*seq)) | |
3330 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
3331 | print_gimple_stmt (dump_file, gsi_stmt (*gsi), | |
3332 | 0, TDF_SLIM); | |
3333 | } | |
3334 | gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT); | |
3335 | return true; | |
3336 | } | |
3337 | } | |
37d486ab RB |
3338 | else if (rcode.is_fn_code () |
3339 | && gimple_call_builtin_p (stmt, rcode)) | |
3340 | { | |
3341 | unsigned i; | |
3342 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
3343 | { | |
3344 | gcc_assert (ops[i] != NULL_TREE); | |
3345 | gimple_call_set_arg (stmt, i, ops[i]); | |
3346 | } | |
3347 | if (i < 3) | |
3348 | gcc_assert (ops[i] == NULL_TREE); | |
3349 | return true; | |
3350 | } | |
e0ee10ed RB |
3351 | else if (!inplace) |
3352 | { | |
3353 | if (gimple_has_lhs (stmt)) | |
3354 | { | |
3355 | tree lhs = gimple_get_lhs (stmt); | |
de665bbd RB |
3356 | if (!maybe_push_res_to_seq (rcode, TREE_TYPE (lhs), |
3357 | ops, seq, lhs)) | |
3358 | return false; | |
e0ee10ed RB |
3359 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3360 | { | |
3361 | fprintf (dump_file, "gimple_simplified to "); | |
3362 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
3363 | } | |
3364 | gsi_replace_with_seq_vops (gsi, *seq); | |
3365 | return true; | |
3366 | } | |
3367 | else | |
3368 | gcc_unreachable (); | |
3369 | } | |
3370 | ||
3371 | return false; | |
3372 | } | |
3373 | ||
040292e7 RB |
3374 | /* Canonicalize MEM_REFs invariant address operand after propagation. */ |
3375 | ||
3376 | static bool | |
3377 | maybe_canonicalize_mem_ref_addr (tree *t) | |
3378 | { | |
3379 | bool res = false; | |
3380 | ||
3381 | if (TREE_CODE (*t) == ADDR_EXPR) | |
3382 | t = &TREE_OPERAND (*t, 0); | |
3383 | ||
3384 | while (handled_component_p (*t)) | |
3385 | t = &TREE_OPERAND (*t, 0); | |
3386 | ||
3387 | /* Canonicalize MEM [&foo.bar, 0] which appears after propagating | |
3388 | of invariant addresses into a SSA name MEM_REF address. */ | |
3389 | if (TREE_CODE (*t) == MEM_REF | |
3390 | || TREE_CODE (*t) == TARGET_MEM_REF) | |
3391 | { | |
3392 | tree addr = TREE_OPERAND (*t, 0); | |
3393 | if (TREE_CODE (addr) == ADDR_EXPR | |
3394 | && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF | |
3395 | || handled_component_p (TREE_OPERAND (addr, 0)))) | |
3396 | { | |
3397 | tree base; | |
3398 | HOST_WIDE_INT coffset; | |
3399 | base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), | |
3400 | &coffset); | |
3401 | if (!base) | |
3402 | gcc_unreachable (); | |
3403 | ||
3404 | TREE_OPERAND (*t, 0) = build_fold_addr_expr (base); | |
3405 | TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR, | |
3406 | TREE_OPERAND (*t, 1), | |
3407 | size_int (coffset)); | |
3408 | res = true; | |
3409 | } | |
3410 | gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL | |
3411 | || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0))); | |
3412 | } | |
3413 | ||
3414 | /* Canonicalize back MEM_REFs to plain reference trees if the object | |
3415 | accessed is a decl that has the same access semantics as the MEM_REF. */ | |
3416 | if (TREE_CODE (*t) == MEM_REF | |
3417 | && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR | |
f3dccf50 RB |
3418 | && integer_zerop (TREE_OPERAND (*t, 1)) |
3419 | && MR_DEPENDENCE_CLIQUE (*t) == 0) | |
040292e7 RB |
3420 | { |
3421 | tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
3422 | tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1)); | |
3423 | if (/* Same volatile qualification. */ | |
3424 | TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl) | |
3425 | /* Same TBAA behavior with -fstrict-aliasing. */ | |
3426 | && !TYPE_REF_CAN_ALIAS_ALL (alias_type) | |
3427 | && (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) | |
3428 | == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type))) | |
3429 | /* Same alignment. */ | |
3430 | && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t)) | |
3431 | /* We have to look out here to not drop a required conversion | |
3432 | from the rhs to the lhs if *t appears on the lhs or vice-versa | |
3433 | if it appears on the rhs. Thus require strict type | |
3434 | compatibility. */ | |
3435 | && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl))) | |
3436 | { | |
3437 | *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
3438 | res = true; | |
3439 | } | |
3440 | } | |
3441 | ||
3442 | /* Canonicalize TARGET_MEM_REF in particular with respect to | |
3443 | the indexes becoming constant. */ | |
3444 | else if (TREE_CODE (*t) == TARGET_MEM_REF) | |
3445 | { | |
3446 | tree tem = maybe_fold_tmr (*t); | |
3447 | if (tem) | |
3448 | { | |
3449 | *t = tem; | |
3450 | res = true; | |
3451 | } | |
3452 | } | |
3453 | ||
3454 | return res; | |
3455 | } | |
3456 | ||
cbdd87d4 RG |
3457 | /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument |
3458 | distinguishes both cases. */ | |
3459 | ||
3460 | static bool | |
e0ee10ed | 3461 | fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree)) |
cbdd87d4 RG |
3462 | { |
3463 | bool changed = false; | |
3464 | gimple stmt = gsi_stmt (*gsi); | |
3465 | unsigned i; | |
3466 | ||
040292e7 RB |
3467 | /* First do required canonicalization of [TARGET_]MEM_REF addresses |
3468 | after propagation. | |
3469 | ??? This shouldn't be done in generic folding but in the | |
3470 | propagation helpers which also know whether an address was | |
89a79e96 RB |
3471 | propagated. |
3472 | Also canonicalize operand order. */ | |
040292e7 RB |
3473 | switch (gimple_code (stmt)) |
3474 | { | |
3475 | case GIMPLE_ASSIGN: | |
3476 | if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS) | |
3477 | { | |
3478 | tree *rhs = gimple_assign_rhs1_ptr (stmt); | |
3479 | if ((REFERENCE_CLASS_P (*rhs) | |
3480 | || TREE_CODE (*rhs) == ADDR_EXPR) | |
3481 | && maybe_canonicalize_mem_ref_addr (rhs)) | |
3482 | changed = true; | |
3483 | tree *lhs = gimple_assign_lhs_ptr (stmt); | |
3484 | if (REFERENCE_CLASS_P (*lhs) | |
3485 | && maybe_canonicalize_mem_ref_addr (lhs)) | |
3486 | changed = true; | |
3487 | } | |
89a79e96 RB |
3488 | else |
3489 | { | |
3490 | /* Canonicalize operand order. */ | |
3491 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
3492 | if (TREE_CODE_CLASS (code) == tcc_comparison | |
3493 | || commutative_tree_code (code) | |
3494 | || commutative_ternary_tree_code (code)) | |
3495 | { | |
3496 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
3497 | tree rhs2 = gimple_assign_rhs2 (stmt); | |
3498 | if (tree_swap_operands_p (rhs1, rhs2, false)) | |
3499 | { | |
3500 | gimple_assign_set_rhs1 (stmt, rhs2); | |
3501 | gimple_assign_set_rhs2 (stmt, rhs1); | |
3502 | if (TREE_CODE_CLASS (code) == tcc_comparison) | |
3503 | gimple_assign_set_rhs_code (stmt, | |
3504 | swap_tree_comparison (code)); | |
3505 | changed = true; | |
3506 | } | |
3507 | } | |
3508 | } | |
040292e7 RB |
3509 | break; |
3510 | case GIMPLE_CALL: | |
3511 | { | |
3512 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
3513 | { | |
3514 | tree *arg = gimple_call_arg_ptr (stmt, i); | |
3515 | if (REFERENCE_CLASS_P (*arg) | |
3516 | && maybe_canonicalize_mem_ref_addr (arg)) | |
3517 | changed = true; | |
3518 | } | |
3519 | tree *lhs = gimple_call_lhs_ptr (stmt); | |
3520 | if (*lhs | |
3521 | && REFERENCE_CLASS_P (*lhs) | |
3522 | && maybe_canonicalize_mem_ref_addr (lhs)) | |
3523 | changed = true; | |
3524 | break; | |
3525 | } | |
3526 | case GIMPLE_ASM: | |
3527 | { | |
538dd0b7 DM |
3528 | gasm *asm_stmt = as_a <gasm *> (stmt); |
3529 | for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i) | |
040292e7 | 3530 | { |
538dd0b7 | 3531 | tree link = gimple_asm_output_op (asm_stmt, i); |
040292e7 RB |
3532 | tree op = TREE_VALUE (link); |
3533 | if (REFERENCE_CLASS_P (op) | |
3534 | && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link))) | |
3535 | changed = true; | |
3536 | } | |
538dd0b7 | 3537 | for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i) |
040292e7 | 3538 | { |
538dd0b7 | 3539 | tree link = gimple_asm_input_op (asm_stmt, i); |
040292e7 RB |
3540 | tree op = TREE_VALUE (link); |
3541 | if ((REFERENCE_CLASS_P (op) | |
3542 | || TREE_CODE (op) == ADDR_EXPR) | |
3543 | && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link))) | |
3544 | changed = true; | |
3545 | } | |
3546 | } | |
3547 | break; | |
3548 | case GIMPLE_DEBUG: | |
3549 | if (gimple_debug_bind_p (stmt)) | |
3550 | { | |
3551 | tree *val = gimple_debug_bind_get_value_ptr (stmt); | |
3552 | if (*val | |
3553 | && (REFERENCE_CLASS_P (*val) | |
3554 | || TREE_CODE (*val) == ADDR_EXPR) | |
3555 | && maybe_canonicalize_mem_ref_addr (val)) | |
3556 | changed = true; | |
3557 | } | |
3558 | break; | |
89a79e96 RB |
3559 | case GIMPLE_COND: |
3560 | { | |
3561 | /* Canonicalize operand order. */ | |
3562 | tree lhs = gimple_cond_lhs (stmt); | |
3563 | tree rhs = gimple_cond_rhs (stmt); | |
3564 | if (tree_swap_operands_p (lhs, rhs, false)) | |
3565 | { | |
3566 | gcond *gc = as_a <gcond *> (stmt); | |
3567 | gimple_cond_set_lhs (gc, rhs); | |
3568 | gimple_cond_set_rhs (gc, lhs); | |
3569 | gimple_cond_set_code (gc, | |
3570 | swap_tree_comparison (gimple_cond_code (gc))); | |
3571 | changed = true; | |
3572 | } | |
3573 | } | |
040292e7 RB |
3574 | default:; |
3575 | } | |
3576 | ||
e0ee10ed RB |
3577 | /* Dispatch to pattern-based folding. */ |
3578 | if (!inplace | |
3579 | || is_gimple_assign (stmt) | |
3580 | || gimple_code (stmt) == GIMPLE_COND) | |
3581 | { | |
3582 | gimple_seq seq = NULL; | |
3583 | code_helper rcode; | |
3584 | tree ops[3] = {}; | |
0ff093d8 RB |
3585 | if (gimple_simplify (stmt, &rcode, ops, inplace ? NULL : &seq, |
3586 | valueize, valueize)) | |
e0ee10ed RB |
3587 | { |
3588 | if (replace_stmt_with_simplification (gsi, rcode, ops, &seq, inplace)) | |
3589 | changed = true; | |
3590 | else | |
3591 | gimple_seq_discard (seq); | |
3592 | } | |
3593 | } | |
3594 | ||
3595 | stmt = gsi_stmt (*gsi); | |
3596 | ||
cbdd87d4 RG |
3597 | /* Fold the main computation performed by the statement. */ |
3598 | switch (gimple_code (stmt)) | |
3599 | { | |
3600 | case GIMPLE_ASSIGN: | |
3601 | { | |
819ec64c RB |
3602 | /* Try to canonicalize for boolean-typed X the comparisons |
3603 | X == 0, X == 1, X != 0, and X != 1. */ | |
3604 | if (gimple_assign_rhs_code (stmt) == EQ_EXPR | |
3605 | || gimple_assign_rhs_code (stmt) == NE_EXPR) | |
5fbcc0ed | 3606 | { |
819ec64c RB |
3607 | tree lhs = gimple_assign_lhs (stmt); |
3608 | tree op1 = gimple_assign_rhs1 (stmt); | |
3609 | tree op2 = gimple_assign_rhs2 (stmt); | |
3610 | tree type = TREE_TYPE (op1); | |
3611 | ||
3612 | /* Check whether the comparison operands are of the same boolean | |
3613 | type as the result type is. | |
3614 | Check that second operand is an integer-constant with value | |
3615 | one or zero. */ | |
3616 | if (TREE_CODE (op2) == INTEGER_CST | |
3617 | && (integer_zerop (op2) || integer_onep (op2)) | |
3618 | && useless_type_conversion_p (TREE_TYPE (lhs), type)) | |
3619 | { | |
3620 | enum tree_code cmp_code = gimple_assign_rhs_code (stmt); | |
3621 | bool is_logical_not = false; | |
3622 | ||
3623 | /* X == 0 and X != 1 is a logical-not.of X | |
3624 | X == 1 and X != 0 is X */ | |
3625 | if ((cmp_code == EQ_EXPR && integer_zerop (op2)) | |
3626 | || (cmp_code == NE_EXPR && integer_onep (op2))) | |
3627 | is_logical_not = true; | |
3628 | ||
3629 | if (is_logical_not == false) | |
3630 | gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1); | |
3631 | /* Only for one-bit precision typed X the transformation | |
3632 | !X -> ~X is valied. */ | |
3633 | else if (TYPE_PRECISION (type) == 1) | |
3634 | gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1); | |
3635 | /* Otherwise we use !X -> X ^ 1. */ | |
3636 | else | |
3637 | gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1, | |
3638 | build_int_cst (type, 1)); | |
3639 | changed = true; | |
3640 | break; | |
3641 | } | |
5fbcc0ed | 3642 | } |
819ec64c RB |
3643 | |
3644 | unsigned old_num_ops = gimple_num_ops (stmt); | |
3645 | tree lhs = gimple_assign_lhs (stmt); | |
3646 | tree new_rhs = fold_gimple_assign (gsi); | |
cbdd87d4 RG |
3647 | if (new_rhs |
3648 | && !useless_type_conversion_p (TREE_TYPE (lhs), | |
3649 | TREE_TYPE (new_rhs))) | |
3650 | new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); | |
3651 | if (new_rhs | |
3652 | && (!inplace | |
3653 | || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) | |
3654 | { | |
3655 | gimple_assign_set_rhs_from_tree (gsi, new_rhs); | |
3656 | changed = true; | |
3657 | } | |
3658 | break; | |
3659 | } | |
3660 | ||
cbdd87d4 | 3661 | case GIMPLE_CALL: |
ceeffab0 | 3662 | changed |= gimple_fold_call (gsi, inplace); |
cbdd87d4 RG |
3663 | break; |
3664 | ||
3665 | case GIMPLE_ASM: | |
3666 | /* Fold *& in asm operands. */ | |
38384150 | 3667 | { |
538dd0b7 | 3668 | gasm *asm_stmt = as_a <gasm *> (stmt); |
38384150 JJ |
3669 | size_t noutputs; |
3670 | const char **oconstraints; | |
3671 | const char *constraint; | |
3672 | bool allows_mem, allows_reg; | |
3673 | ||
538dd0b7 | 3674 | noutputs = gimple_asm_noutputs (asm_stmt); |
38384150 JJ |
3675 | oconstraints = XALLOCAVEC (const char *, noutputs); |
3676 | ||
538dd0b7 | 3677 | for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i) |
38384150 | 3678 | { |
538dd0b7 | 3679 | tree link = gimple_asm_output_op (asm_stmt, i); |
38384150 JJ |
3680 | tree op = TREE_VALUE (link); |
3681 | oconstraints[i] | |
3682 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
3683 | if (REFERENCE_CLASS_P (op) | |
3684 | && (op = maybe_fold_reference (op, true)) != NULL_TREE) | |
3685 | { | |
3686 | TREE_VALUE (link) = op; | |
3687 | changed = true; | |
3688 | } | |
3689 | } | |
538dd0b7 | 3690 | for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i) |
38384150 | 3691 | { |
538dd0b7 | 3692 | tree link = gimple_asm_input_op (asm_stmt, i); |
38384150 JJ |
3693 | tree op = TREE_VALUE (link); |
3694 | constraint | |
3695 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
3696 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, | |
3697 | oconstraints, &allows_mem, &allows_reg); | |
3698 | if (REFERENCE_CLASS_P (op) | |
3699 | && (op = maybe_fold_reference (op, !allows_reg && allows_mem)) | |
3700 | != NULL_TREE) | |
3701 | { | |
3702 | TREE_VALUE (link) = op; | |
3703 | changed = true; | |
3704 | } | |
3705 | } | |
3706 | } | |
cbdd87d4 RG |
3707 | break; |
3708 | ||
bd422c4a RG |
3709 | case GIMPLE_DEBUG: |
3710 | if (gimple_debug_bind_p (stmt)) | |
3711 | { | |
3712 | tree val = gimple_debug_bind_get_value (stmt); | |
3713 | if (val | |
3714 | && REFERENCE_CLASS_P (val)) | |
3715 | { | |
3716 | tree tem = maybe_fold_reference (val, false); | |
3717 | if (tem) | |
3718 | { | |
3719 | gimple_debug_bind_set_value (stmt, tem); | |
3720 | changed = true; | |
3721 | } | |
3722 | } | |
3e888a5e RG |
3723 | else if (val |
3724 | && TREE_CODE (val) == ADDR_EXPR) | |
3725 | { | |
3726 | tree ref = TREE_OPERAND (val, 0); | |
3727 | tree tem = maybe_fold_reference (ref, false); | |
3728 | if (tem) | |
3729 | { | |
3730 | tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val)); | |
3731 | gimple_debug_bind_set_value (stmt, tem); | |
3732 | changed = true; | |
3733 | } | |
3734 | } | |
bd422c4a RG |
3735 | } |
3736 | break; | |
3737 | ||
cbdd87d4 RG |
3738 | default:; |
3739 | } | |
3740 | ||
3741 | stmt = gsi_stmt (*gsi); | |
3742 | ||
37376165 RB |
3743 | /* Fold *& on the lhs. */ |
3744 | if (gimple_has_lhs (stmt)) | |
cbdd87d4 RG |
3745 | { |
3746 | tree lhs = gimple_get_lhs (stmt); | |
3747 | if (lhs && REFERENCE_CLASS_P (lhs)) | |
3748 | { | |
3749 | tree new_lhs = maybe_fold_reference (lhs, true); | |
3750 | if (new_lhs) | |
3751 | { | |
3752 | gimple_set_lhs (stmt, new_lhs); | |
3753 | changed = true; | |
3754 | } | |
3755 | } | |
3756 | } | |
3757 | ||
3758 | return changed; | |
3759 | } | |
3760 | ||
e0ee10ed RB |
3761 | /* Valueziation callback that ends up not following SSA edges. */ |
3762 | ||
3763 | tree | |
3764 | no_follow_ssa_edges (tree) | |
3765 | { | |
3766 | return NULL_TREE; | |
3767 | } | |
3768 | ||
45cc9f96 RB |
3769 | /* Valueization callback that ends up following single-use SSA edges only. */ |
3770 | ||
3771 | tree | |
3772 | follow_single_use_edges (tree val) | |
3773 | { | |
3774 | if (TREE_CODE (val) == SSA_NAME | |
3775 | && !has_single_use (val)) | |
3776 | return NULL_TREE; | |
3777 | return val; | |
3778 | } | |
3779 | ||
cbdd87d4 RG |
3780 | /* Fold the statement pointed to by GSI. In some cases, this function may |
3781 | replace the whole statement with a new one. Returns true iff folding | |
3782 | makes any changes. | |
3783 | The statement pointed to by GSI should be in valid gimple form but may | |
3784 | be in unfolded state as resulting from for example constant propagation | |
3785 | which can produce *&x = 0. */ | |
3786 | ||
3787 | bool | |
3788 | fold_stmt (gimple_stmt_iterator *gsi) | |
3789 | { | |
e0ee10ed RB |
3790 | return fold_stmt_1 (gsi, false, no_follow_ssa_edges); |
3791 | } | |
3792 | ||
3793 | bool | |
3794 | fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree)) | |
3795 | { | |
3796 | return fold_stmt_1 (gsi, false, valueize); | |
cbdd87d4 RG |
3797 | } |
3798 | ||
59401b92 | 3799 | /* Perform the minimal folding on statement *GSI. Only operations like |
cbdd87d4 RG |
3800 | *&x created by constant propagation are handled. The statement cannot |
3801 | be replaced with a new one. Return true if the statement was | |
3802 | changed, false otherwise. | |
59401b92 | 3803 | The statement *GSI should be in valid gimple form but may |
cbdd87d4 RG |
3804 | be in unfolded state as resulting from for example constant propagation |
3805 | which can produce *&x = 0. */ | |
3806 | ||
3807 | bool | |
59401b92 | 3808 | fold_stmt_inplace (gimple_stmt_iterator *gsi) |
cbdd87d4 | 3809 | { |
59401b92 | 3810 | gimple stmt = gsi_stmt (*gsi); |
e0ee10ed | 3811 | bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges); |
59401b92 | 3812 | gcc_assert (gsi_stmt (*gsi) == stmt); |
cbdd87d4 RG |
3813 | return changed; |
3814 | } | |
3815 | ||
e89065a1 SL |
3816 | /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE |
3817 | if EXPR is null or we don't know how. | |
3818 | If non-null, the result always has boolean type. */ | |
3819 | ||
3820 | static tree | |
3821 | canonicalize_bool (tree expr, bool invert) | |
3822 | { | |
3823 | if (!expr) | |
3824 | return NULL_TREE; | |
3825 | else if (invert) | |
3826 | { | |
3827 | if (integer_nonzerop (expr)) | |
3828 | return boolean_false_node; | |
3829 | else if (integer_zerop (expr)) | |
3830 | return boolean_true_node; | |
3831 | else if (TREE_CODE (expr) == SSA_NAME) | |
3832 | return fold_build2 (EQ_EXPR, boolean_type_node, expr, | |
3833 | build_int_cst (TREE_TYPE (expr), 0)); | |
98209db3 | 3834 | else if (COMPARISON_CLASS_P (expr)) |
e89065a1 SL |
3835 | return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false), |
3836 | boolean_type_node, | |
3837 | TREE_OPERAND (expr, 0), | |
3838 | TREE_OPERAND (expr, 1)); | |
3839 | else | |
3840 | return NULL_TREE; | |
3841 | } | |
3842 | else | |
3843 | { | |
3844 | if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
3845 | return expr; | |
3846 | if (integer_nonzerop (expr)) | |
3847 | return boolean_true_node; | |
3848 | else if (integer_zerop (expr)) | |
3849 | return boolean_false_node; | |
3850 | else if (TREE_CODE (expr) == SSA_NAME) | |
3851 | return fold_build2 (NE_EXPR, boolean_type_node, expr, | |
3852 | build_int_cst (TREE_TYPE (expr), 0)); | |
98209db3 | 3853 | else if (COMPARISON_CLASS_P (expr)) |
e89065a1 SL |
3854 | return fold_build2 (TREE_CODE (expr), |
3855 | boolean_type_node, | |
3856 | TREE_OPERAND (expr, 0), | |
3857 | TREE_OPERAND (expr, 1)); | |
3858 | else | |
3859 | return NULL_TREE; | |
3860 | } | |
3861 | } | |
3862 | ||
3863 | /* Check to see if a boolean expression EXPR is logically equivalent to the | |
3864 | comparison (OP1 CODE OP2). Check for various identities involving | |
3865 | SSA_NAMEs. */ | |
3866 | ||
3867 | static bool | |
3868 | same_bool_comparison_p (const_tree expr, enum tree_code code, | |
3869 | const_tree op1, const_tree op2) | |
3870 | { | |
3871 | gimple s; | |
3872 | ||
3873 | /* The obvious case. */ | |
3874 | if (TREE_CODE (expr) == code | |
3875 | && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0) | |
3876 | && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0)) | |
3877 | return true; | |
3878 | ||
3879 | /* Check for comparing (name, name != 0) and the case where expr | |
3880 | is an SSA_NAME with a definition matching the comparison. */ | |
3881 | if (TREE_CODE (expr) == SSA_NAME | |
3882 | && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
3883 | { | |
3884 | if (operand_equal_p (expr, op1, 0)) | |
3885 | return ((code == NE_EXPR && integer_zerop (op2)) | |
3886 | || (code == EQ_EXPR && integer_nonzerop (op2))); | |
3887 | s = SSA_NAME_DEF_STMT (expr); | |
3888 | if (is_gimple_assign (s) | |
3889 | && gimple_assign_rhs_code (s) == code | |
3890 | && operand_equal_p (gimple_assign_rhs1 (s), op1, 0) | |
3891 | && operand_equal_p (gimple_assign_rhs2 (s), op2, 0)) | |
3892 | return true; | |
3893 | } | |
3894 | ||
3895 | /* If op1 is of the form (name != 0) or (name == 0), and the definition | |
3896 | of name is a comparison, recurse. */ | |
3897 | if (TREE_CODE (op1) == SSA_NAME | |
3898 | && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE) | |
3899 | { | |
3900 | s = SSA_NAME_DEF_STMT (op1); | |
3901 | if (is_gimple_assign (s) | |
3902 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison) | |
3903 | { | |
3904 | enum tree_code c = gimple_assign_rhs_code (s); | |
3905 | if ((c == NE_EXPR && integer_zerop (op2)) | |
3906 | || (c == EQ_EXPR && integer_nonzerop (op2))) | |
3907 | return same_bool_comparison_p (expr, c, | |
3908 | gimple_assign_rhs1 (s), | |
3909 | gimple_assign_rhs2 (s)); | |
3910 | if ((c == EQ_EXPR && integer_zerop (op2)) | |
3911 | || (c == NE_EXPR && integer_nonzerop (op2))) | |
3912 | return same_bool_comparison_p (expr, | |
3913 | invert_tree_comparison (c, false), | |
3914 | gimple_assign_rhs1 (s), | |
3915 | gimple_assign_rhs2 (s)); | |
3916 | } | |
3917 | } | |
3918 | return false; | |
3919 | } | |
3920 | ||
3921 | /* Check to see if two boolean expressions OP1 and OP2 are logically | |
3922 | equivalent. */ | |
3923 | ||
3924 | static bool | |
3925 | same_bool_result_p (const_tree op1, const_tree op2) | |
3926 | { | |
3927 | /* Simple cases first. */ | |
3928 | if (operand_equal_p (op1, op2, 0)) | |
3929 | return true; | |
3930 | ||
3931 | /* Check the cases where at least one of the operands is a comparison. | |
3932 | These are a bit smarter than operand_equal_p in that they apply some | |
3933 | identifies on SSA_NAMEs. */ | |
98209db3 | 3934 | if (COMPARISON_CLASS_P (op2) |
e89065a1 SL |
3935 | && same_bool_comparison_p (op1, TREE_CODE (op2), |
3936 | TREE_OPERAND (op2, 0), | |
3937 | TREE_OPERAND (op2, 1))) | |
3938 | return true; | |
98209db3 | 3939 | if (COMPARISON_CLASS_P (op1) |
e89065a1 SL |
3940 | && same_bool_comparison_p (op2, TREE_CODE (op1), |
3941 | TREE_OPERAND (op1, 0), | |
3942 | TREE_OPERAND (op1, 1))) | |
3943 | return true; | |
3944 | ||
3945 | /* Default case. */ | |
3946 | return false; | |
3947 | } | |
3948 | ||
3949 | /* Forward declarations for some mutually recursive functions. */ | |
3950 | ||
3951 | static tree | |
3952 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
3953 | enum tree_code code2, tree op2a, tree op2b); | |
3954 | static tree | |
3955 | and_var_with_comparison (tree var, bool invert, | |
3956 | enum tree_code code2, tree op2a, tree op2b); | |
3957 | static tree | |
3958 | and_var_with_comparison_1 (gimple stmt, | |
3959 | enum tree_code code2, tree op2a, tree op2b); | |
3960 | static tree | |
3961 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
3962 | enum tree_code code2, tree op2a, tree op2b); | |
3963 | static tree | |
3964 | or_var_with_comparison (tree var, bool invert, | |
3965 | enum tree_code code2, tree op2a, tree op2b); | |
3966 | static tree | |
3967 | or_var_with_comparison_1 (gimple stmt, | |
3968 | enum tree_code code2, tree op2a, tree op2b); | |
3969 | ||
3970 | /* Helper function for and_comparisons_1: try to simplify the AND of the | |
3971 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
3972 | If INVERT is true, invert the value of the VAR before doing the AND. | |
3973 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
3974 | ||
3975 | static tree | |
3976 | and_var_with_comparison (tree var, bool invert, | |
3977 | enum tree_code code2, tree op2a, tree op2b) | |
3978 | { | |
3979 | tree t; | |
3980 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
3981 | ||
3982 | /* We can only deal with variables whose definitions are assignments. */ | |
3983 | if (!is_gimple_assign (stmt)) | |
3984 | return NULL_TREE; | |
3985 | ||
3986 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
3987 | !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b)) | |
3988 | Then we only have to consider the simpler non-inverted cases. */ | |
3989 | if (invert) | |
3990 | t = or_var_with_comparison_1 (stmt, | |
3991 | invert_tree_comparison (code2, false), | |
3992 | op2a, op2b); | |
3993 | else | |
3994 | t = and_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
3995 | return canonicalize_bool (t, invert); | |
3996 | } | |
3997 | ||
3998 | /* Try to simplify the AND of the ssa variable defined by the assignment | |
3999 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
4000 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
4001 | ||
4002 | static tree | |
4003 | and_var_with_comparison_1 (gimple stmt, | |
4004 | enum tree_code code2, tree op2a, tree op2b) | |
4005 | { | |
4006 | tree var = gimple_assign_lhs (stmt); | |
4007 | tree true_test_var = NULL_TREE; | |
4008 | tree false_test_var = NULL_TREE; | |
4009 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
4010 | ||
4011 | /* Check for identities like (var AND (var == 0)) => false. */ | |
4012 | if (TREE_CODE (op2a) == SSA_NAME | |
4013 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
4014 | { | |
4015 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
4016 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
4017 | { | |
4018 | true_test_var = op2a; | |
4019 | if (var == true_test_var) | |
4020 | return var; | |
4021 | } | |
4022 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
4023 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
4024 | { | |
4025 | false_test_var = op2a; | |
4026 | if (var == false_test_var) | |
4027 | return boolean_false_node; | |
4028 | } | |
4029 | } | |
4030 | ||
4031 | /* If the definition is a comparison, recurse on it. */ | |
4032 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
4033 | { | |
4034 | tree t = and_comparisons_1 (innercode, | |
4035 | gimple_assign_rhs1 (stmt), | |
4036 | gimple_assign_rhs2 (stmt), | |
4037 | code2, | |
4038 | op2a, | |
4039 | op2b); | |
4040 | if (t) | |
4041 | return t; | |
4042 | } | |
4043 | ||
4044 | /* If the definition is an AND or OR expression, we may be able to | |
4045 | simplify by reassociating. */ | |
eb9820c0 KT |
4046 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
4047 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
e89065a1 SL |
4048 | { |
4049 | tree inner1 = gimple_assign_rhs1 (stmt); | |
4050 | tree inner2 = gimple_assign_rhs2 (stmt); | |
4051 | gimple s; | |
4052 | tree t; | |
4053 | tree partial = NULL_TREE; | |
eb9820c0 | 4054 | bool is_and = (innercode == BIT_AND_EXPR); |
e89065a1 SL |
4055 | |
4056 | /* Check for boolean identities that don't require recursive examination | |
4057 | of inner1/inner2: | |
4058 | inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var | |
4059 | inner1 AND (inner1 OR inner2) => inner1 | |
4060 | !inner1 AND (inner1 AND inner2) => false | |
4061 | !inner1 AND (inner1 OR inner2) => !inner1 AND inner2 | |
4062 | Likewise for similar cases involving inner2. */ | |
4063 | if (inner1 == true_test_var) | |
4064 | return (is_and ? var : inner1); | |
4065 | else if (inner2 == true_test_var) | |
4066 | return (is_and ? var : inner2); | |
4067 | else if (inner1 == false_test_var) | |
4068 | return (is_and | |
4069 | ? boolean_false_node | |
4070 | : and_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
4071 | else if (inner2 == false_test_var) | |
4072 | return (is_and | |
4073 | ? boolean_false_node | |
4074 | : and_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
4075 | ||
4076 | /* Next, redistribute/reassociate the AND across the inner tests. | |
4077 | Compute the first partial result, (inner1 AND (op2a code op2b)) */ | |
4078 | if (TREE_CODE (inner1) == SSA_NAME | |
4079 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
4080 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
4081 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
4082 | gimple_assign_rhs1 (s), | |
4083 | gimple_assign_rhs2 (s), | |
4084 | code2, op2a, op2b))) | |
4085 | { | |
4086 | /* Handle the AND case, where we are reassociating: | |
4087 | (inner1 AND inner2) AND (op2a code2 op2b) | |
4088 | => (t AND inner2) | |
4089 | If the partial result t is a constant, we win. Otherwise | |
4090 | continue on to try reassociating with the other inner test. */ | |
4091 | if (is_and) | |
4092 | { | |
4093 | if (integer_onep (t)) | |
4094 | return inner2; | |
4095 | else if (integer_zerop (t)) | |
4096 | return boolean_false_node; | |
4097 | } | |
4098 | ||
4099 | /* Handle the OR case, where we are redistributing: | |
4100 | (inner1 OR inner2) AND (op2a code2 op2b) | |
4101 | => (t OR (inner2 AND (op2a code2 op2b))) */ | |
8236c8eb JJ |
4102 | else if (integer_onep (t)) |
4103 | return boolean_true_node; | |
4104 | ||
4105 | /* Save partial result for later. */ | |
4106 | partial = t; | |
e89065a1 SL |
4107 | } |
4108 | ||
4109 | /* Compute the second partial result, (inner2 AND (op2a code op2b)) */ | |
4110 | if (TREE_CODE (inner2) == SSA_NAME | |
4111 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
4112 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
4113 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
4114 | gimple_assign_rhs1 (s), | |
4115 | gimple_assign_rhs2 (s), | |
4116 | code2, op2a, op2b))) | |
4117 | { | |
4118 | /* Handle the AND case, where we are reassociating: | |
4119 | (inner1 AND inner2) AND (op2a code2 op2b) | |
4120 | => (inner1 AND t) */ | |
4121 | if (is_and) | |
4122 | { | |
4123 | if (integer_onep (t)) | |
4124 | return inner1; | |
4125 | else if (integer_zerop (t)) | |
4126 | return boolean_false_node; | |
8236c8eb JJ |
4127 | /* If both are the same, we can apply the identity |
4128 | (x AND x) == x. */ | |
4129 | else if (partial && same_bool_result_p (t, partial)) | |
4130 | return t; | |
e89065a1 SL |
4131 | } |
4132 | ||
4133 | /* Handle the OR case. where we are redistributing: | |
4134 | (inner1 OR inner2) AND (op2a code2 op2b) | |
4135 | => (t OR (inner1 AND (op2a code2 op2b))) | |
4136 | => (t OR partial) */ | |
4137 | else | |
4138 | { | |
4139 | if (integer_onep (t)) | |
4140 | return boolean_true_node; | |
4141 | else if (partial) | |
4142 | { | |
4143 | /* We already got a simplification for the other | |
4144 | operand to the redistributed OR expression. The | |
4145 | interesting case is when at least one is false. | |
4146 | Or, if both are the same, we can apply the identity | |
4147 | (x OR x) == x. */ | |
4148 | if (integer_zerop (partial)) | |
4149 | return t; | |
4150 | else if (integer_zerop (t)) | |
4151 | return partial; | |
4152 | else if (same_bool_result_p (t, partial)) | |
4153 | return t; | |
4154 | } | |
4155 | } | |
4156 | } | |
4157 | } | |
4158 | return NULL_TREE; | |
4159 | } | |
4160 | ||
4161 | /* Try to simplify the AND of two comparisons defined by | |
4162 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
4163 | If this can be done without constructing an intermediate value, | |
4164 | return the resulting tree; otherwise NULL_TREE is returned. | |
4165 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
4166 | in the first comparison but not the second. */ | |
4167 | ||
4168 | static tree | |
4169 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
4170 | enum tree_code code2, tree op2a, tree op2b) | |
4171 | { | |
ae22ac3c | 4172 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
31ed6226 | 4173 | |
e89065a1 SL |
4174 | /* First check for ((x CODE1 y) AND (x CODE2 y)). */ |
4175 | if (operand_equal_p (op1a, op2a, 0) | |
4176 | && operand_equal_p (op1b, op2b, 0)) | |
4177 | { | |
eb9820c0 | 4178 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
4179 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
4180 | TRUTH_ANDIF_EXPR, code1, code2, | |
31ed6226 | 4181 | truth_type, op1a, op1b); |
e89065a1 SL |
4182 | if (t) |
4183 | return t; | |
4184 | } | |
4185 | ||
4186 | /* Likewise the swapped case of the above. */ | |
4187 | if (operand_equal_p (op1a, op2b, 0) | |
4188 | && operand_equal_p (op1b, op2a, 0)) | |
4189 | { | |
eb9820c0 | 4190 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
4191 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
4192 | TRUTH_ANDIF_EXPR, code1, | |
4193 | swap_tree_comparison (code2), | |
31ed6226 | 4194 | truth_type, op1a, op1b); |
e89065a1 SL |
4195 | if (t) |
4196 | return t; | |
4197 | } | |
4198 | ||
4199 | /* If both comparisons are of the same value against constants, we might | |
4200 | be able to merge them. */ | |
4201 | if (operand_equal_p (op1a, op2a, 0) | |
4202 | && TREE_CODE (op1b) == INTEGER_CST | |
4203 | && TREE_CODE (op2b) == INTEGER_CST) | |
4204 | { | |
4205 | int cmp = tree_int_cst_compare (op1b, op2b); | |
4206 | ||
4207 | /* If we have (op1a == op1b), we should either be able to | |
4208 | return that or FALSE, depending on whether the constant op1b | |
4209 | also satisfies the other comparison against op2b. */ | |
4210 | if (code1 == EQ_EXPR) | |
4211 | { | |
4212 | bool done = true; | |
4213 | bool val; | |
4214 | switch (code2) | |
4215 | { | |
4216 | case EQ_EXPR: val = (cmp == 0); break; | |
4217 | case NE_EXPR: val = (cmp != 0); break; | |
4218 | case LT_EXPR: val = (cmp < 0); break; | |
4219 | case GT_EXPR: val = (cmp > 0); break; | |
4220 | case LE_EXPR: val = (cmp <= 0); break; | |
4221 | case GE_EXPR: val = (cmp >= 0); break; | |
4222 | default: done = false; | |
4223 | } | |
4224 | if (done) | |
4225 | { | |
4226 | if (val) | |
4227 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4228 | else | |
4229 | return boolean_false_node; | |
4230 | } | |
4231 | } | |
4232 | /* Likewise if the second comparison is an == comparison. */ | |
4233 | else if (code2 == EQ_EXPR) | |
4234 | { | |
4235 | bool done = true; | |
4236 | bool val; | |
4237 | switch (code1) | |
4238 | { | |
4239 | case EQ_EXPR: val = (cmp == 0); break; | |
4240 | case NE_EXPR: val = (cmp != 0); break; | |
4241 | case LT_EXPR: val = (cmp > 0); break; | |
4242 | case GT_EXPR: val = (cmp < 0); break; | |
4243 | case LE_EXPR: val = (cmp >= 0); break; | |
4244 | case GE_EXPR: val = (cmp <= 0); break; | |
4245 | default: done = false; | |
4246 | } | |
4247 | if (done) | |
4248 | { | |
4249 | if (val) | |
4250 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4251 | else | |
4252 | return boolean_false_node; | |
4253 | } | |
4254 | } | |
4255 | ||
4256 | /* Same business with inequality tests. */ | |
4257 | else if (code1 == NE_EXPR) | |
4258 | { | |
4259 | bool val; | |
4260 | switch (code2) | |
4261 | { | |
4262 | case EQ_EXPR: val = (cmp != 0); break; | |
4263 | case NE_EXPR: val = (cmp == 0); break; | |
4264 | case LT_EXPR: val = (cmp >= 0); break; | |
4265 | case GT_EXPR: val = (cmp <= 0); break; | |
4266 | case LE_EXPR: val = (cmp > 0); break; | |
4267 | case GE_EXPR: val = (cmp < 0); break; | |
4268 | default: | |
4269 | val = false; | |
4270 | } | |
4271 | if (val) | |
4272 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4273 | } | |
4274 | else if (code2 == NE_EXPR) | |
4275 | { | |
4276 | bool val; | |
4277 | switch (code1) | |
4278 | { | |
4279 | case EQ_EXPR: val = (cmp == 0); break; | |
4280 | case NE_EXPR: val = (cmp != 0); break; | |
4281 | case LT_EXPR: val = (cmp <= 0); break; | |
4282 | case GT_EXPR: val = (cmp >= 0); break; | |
4283 | case LE_EXPR: val = (cmp < 0); break; | |
4284 | case GE_EXPR: val = (cmp > 0); break; | |
4285 | default: | |
4286 | val = false; | |
4287 | } | |
4288 | if (val) | |
4289 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4290 | } | |
4291 | ||
4292 | /* Chose the more restrictive of two < or <= comparisons. */ | |
4293 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
4294 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
4295 | { | |
4296 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
4297 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4298 | else | |
4299 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4300 | } | |
4301 | ||
4302 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
4303 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
4304 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
4305 | { | |
4306 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
4307 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4308 | else | |
4309 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4310 | } | |
4311 | ||
4312 | /* Check for singleton ranges. */ | |
4313 | else if (cmp == 0 | |
4314 | && ((code1 == LE_EXPR && code2 == GE_EXPR) | |
4315 | || (code1 == GE_EXPR && code2 == LE_EXPR))) | |
4316 | return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b); | |
4317 | ||
4318 | /* Check for disjoint ranges. */ | |
4319 | else if (cmp <= 0 | |
4320 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
4321 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
4322 | return boolean_false_node; | |
4323 | else if (cmp >= 0 | |
4324 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
4325 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
4326 | return boolean_false_node; | |
4327 | } | |
4328 | ||
4329 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
4330 | NAME's definition is a truth value. See if there are any simplifications | |
4331 | that can be done against the NAME's definition. */ | |
4332 | if (TREE_CODE (op1a) == SSA_NAME | |
4333 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
4334 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
4335 | { | |
4336 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
4337 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
4338 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
4339 | switch (gimple_code (stmt)) | |
4340 | { | |
4341 | case GIMPLE_ASSIGN: | |
4342 | /* Try to simplify by copy-propagating the definition. */ | |
4343 | return and_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
4344 | ||
4345 | case GIMPLE_PHI: | |
4346 | /* If every argument to the PHI produces the same result when | |
4347 | ANDed with the second comparison, we win. | |
4348 | Do not do this unless the type is bool since we need a bool | |
4349 | result here anyway. */ | |
4350 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
4351 | { | |
4352 | tree result = NULL_TREE; | |
4353 | unsigned i; | |
4354 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
4355 | { | |
4356 | tree arg = gimple_phi_arg_def (stmt, i); | |
4357 | ||
4358 | /* If this PHI has itself as an argument, ignore it. | |
4359 | If all the other args produce the same result, | |
4360 | we're still OK. */ | |
4361 | if (arg == gimple_phi_result (stmt)) | |
4362 | continue; | |
4363 | else if (TREE_CODE (arg) == INTEGER_CST) | |
4364 | { | |
4365 | if (invert ? integer_nonzerop (arg) : integer_zerop (arg)) | |
4366 | { | |
4367 | if (!result) | |
4368 | result = boolean_false_node; | |
4369 | else if (!integer_zerop (result)) | |
4370 | return NULL_TREE; | |
4371 | } | |
4372 | else if (!result) | |
4373 | result = fold_build2 (code2, boolean_type_node, | |
4374 | op2a, op2b); | |
4375 | else if (!same_bool_comparison_p (result, | |
4376 | code2, op2a, op2b)) | |
4377 | return NULL_TREE; | |
4378 | } | |
0e8b84ec JJ |
4379 | else if (TREE_CODE (arg) == SSA_NAME |
4380 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
e89065a1 | 4381 | { |
6c66f733 JJ |
4382 | tree temp; |
4383 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
4384 | /* In simple cases we can look through PHI nodes, | |
4385 | but we have to be careful with loops. | |
4386 | See PR49073. */ | |
4387 | if (! dom_info_available_p (CDI_DOMINATORS) | |
4388 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
4389 | || dominated_by_p (CDI_DOMINATORS, | |
4390 | gimple_bb (def_stmt), | |
4391 | gimple_bb (stmt))) | |
4392 | return NULL_TREE; | |
4393 | temp = and_var_with_comparison (arg, invert, code2, | |
4394 | op2a, op2b); | |
e89065a1 SL |
4395 | if (!temp) |
4396 | return NULL_TREE; | |
4397 | else if (!result) | |
4398 | result = temp; | |
4399 | else if (!same_bool_result_p (result, temp)) | |
4400 | return NULL_TREE; | |
4401 | } | |
4402 | else | |
4403 | return NULL_TREE; | |
4404 | } | |
4405 | return result; | |
4406 | } | |
4407 | ||
4408 | default: | |
4409 | break; | |
4410 | } | |
4411 | } | |
4412 | return NULL_TREE; | |
4413 | } | |
4414 | ||
4415 | /* Try to simplify the AND of two comparisons, specified by | |
4416 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
4417 | If this can be simplified to a single expression (without requiring | |
4418 | introducing more SSA variables to hold intermediate values), | |
4419 | return the resulting tree. Otherwise return NULL_TREE. | |
4420 | If the result expression is non-null, it has boolean type. */ | |
4421 | ||
4422 | tree | |
4423 | maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
4424 | enum tree_code code2, tree op2a, tree op2b) | |
4425 | { | |
4426 | tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
4427 | if (t) | |
4428 | return t; | |
4429 | else | |
4430 | return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
4431 | } | |
4432 | ||
4433 | /* Helper function for or_comparisons_1: try to simplify the OR of the | |
4434 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
4435 | If INVERT is true, invert the value of VAR before doing the OR. | |
4436 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
4437 | ||
4438 | static tree | |
4439 | or_var_with_comparison (tree var, bool invert, | |
4440 | enum tree_code code2, tree op2a, tree op2b) | |
4441 | { | |
4442 | tree t; | |
4443 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
4444 | ||
4445 | /* We can only deal with variables whose definitions are assignments. */ | |
4446 | if (!is_gimple_assign (stmt)) | |
4447 | return NULL_TREE; | |
4448 | ||
4449 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
4450 | !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b)) | |
4451 | Then we only have to consider the simpler non-inverted cases. */ | |
4452 | if (invert) | |
4453 | t = and_var_with_comparison_1 (stmt, | |
4454 | invert_tree_comparison (code2, false), | |
4455 | op2a, op2b); | |
4456 | else | |
4457 | t = or_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
4458 | return canonicalize_bool (t, invert); | |
4459 | } | |
4460 | ||
4461 | /* Try to simplify the OR of the ssa variable defined by the assignment | |
4462 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
4463 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
4464 | ||
4465 | static tree | |
4466 | or_var_with_comparison_1 (gimple stmt, | |
4467 | enum tree_code code2, tree op2a, tree op2b) | |
4468 | { | |
4469 | tree var = gimple_assign_lhs (stmt); | |
4470 | tree true_test_var = NULL_TREE; | |
4471 | tree false_test_var = NULL_TREE; | |
4472 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
4473 | ||
4474 | /* Check for identities like (var OR (var != 0)) => true . */ | |
4475 | if (TREE_CODE (op2a) == SSA_NAME | |
4476 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
4477 | { | |
4478 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
4479 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
4480 | { | |
4481 | true_test_var = op2a; | |
4482 | if (var == true_test_var) | |
4483 | return var; | |
4484 | } | |
4485 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
4486 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
4487 | { | |
4488 | false_test_var = op2a; | |
4489 | if (var == false_test_var) | |
4490 | return boolean_true_node; | |
4491 | } | |
4492 | } | |
4493 | ||
4494 | /* If the definition is a comparison, recurse on it. */ | |
4495 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
4496 | { | |
4497 | tree t = or_comparisons_1 (innercode, | |
4498 | gimple_assign_rhs1 (stmt), | |
4499 | gimple_assign_rhs2 (stmt), | |
4500 | code2, | |
4501 | op2a, | |
4502 | op2b); | |
4503 | if (t) | |
4504 | return t; | |
4505 | } | |
4506 | ||
4507 | /* If the definition is an AND or OR expression, we may be able to | |
4508 | simplify by reassociating. */ | |
eb9820c0 KT |
4509 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
4510 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
e89065a1 SL |
4511 | { |
4512 | tree inner1 = gimple_assign_rhs1 (stmt); | |
4513 | tree inner2 = gimple_assign_rhs2 (stmt); | |
4514 | gimple s; | |
4515 | tree t; | |
4516 | tree partial = NULL_TREE; | |
eb9820c0 | 4517 | bool is_or = (innercode == BIT_IOR_EXPR); |
e89065a1 SL |
4518 | |
4519 | /* Check for boolean identities that don't require recursive examination | |
4520 | of inner1/inner2: | |
4521 | inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var | |
4522 | inner1 OR (inner1 AND inner2) => inner1 | |
4523 | !inner1 OR (inner1 OR inner2) => true | |
4524 | !inner1 OR (inner1 AND inner2) => !inner1 OR inner2 | |
4525 | */ | |
4526 | if (inner1 == true_test_var) | |
4527 | return (is_or ? var : inner1); | |
4528 | else if (inner2 == true_test_var) | |
4529 | return (is_or ? var : inner2); | |
4530 | else if (inner1 == false_test_var) | |
4531 | return (is_or | |
4532 | ? boolean_true_node | |
4533 | : or_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
4534 | else if (inner2 == false_test_var) | |
4535 | return (is_or | |
4536 | ? boolean_true_node | |
4537 | : or_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
4538 | ||
4539 | /* Next, redistribute/reassociate the OR across the inner tests. | |
4540 | Compute the first partial result, (inner1 OR (op2a code op2b)) */ | |
4541 | if (TREE_CODE (inner1) == SSA_NAME | |
4542 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
4543 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
4544 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
4545 | gimple_assign_rhs1 (s), | |
4546 | gimple_assign_rhs2 (s), | |
4547 | code2, op2a, op2b))) | |
4548 | { | |
4549 | /* Handle the OR case, where we are reassociating: | |
4550 | (inner1 OR inner2) OR (op2a code2 op2b) | |
4551 | => (t OR inner2) | |
4552 | If the partial result t is a constant, we win. Otherwise | |
4553 | continue on to try reassociating with the other inner test. */ | |
8236c8eb | 4554 | if (is_or) |
e89065a1 SL |
4555 | { |
4556 | if (integer_onep (t)) | |
4557 | return boolean_true_node; | |
4558 | else if (integer_zerop (t)) | |
4559 | return inner2; | |
4560 | } | |
4561 | ||
4562 | /* Handle the AND case, where we are redistributing: | |
4563 | (inner1 AND inner2) OR (op2a code2 op2b) | |
4564 | => (t AND (inner2 OR (op2a code op2b))) */ | |
8236c8eb JJ |
4565 | else if (integer_zerop (t)) |
4566 | return boolean_false_node; | |
4567 | ||
4568 | /* Save partial result for later. */ | |
4569 | partial = t; | |
e89065a1 SL |
4570 | } |
4571 | ||
4572 | /* Compute the second partial result, (inner2 OR (op2a code op2b)) */ | |
4573 | if (TREE_CODE (inner2) == SSA_NAME | |
4574 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
4575 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
4576 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
4577 | gimple_assign_rhs1 (s), | |
4578 | gimple_assign_rhs2 (s), | |
4579 | code2, op2a, op2b))) | |
4580 | { | |
4581 | /* Handle the OR case, where we are reassociating: | |
4582 | (inner1 OR inner2) OR (op2a code2 op2b) | |
8236c8eb JJ |
4583 | => (inner1 OR t) |
4584 | => (t OR partial) */ | |
4585 | if (is_or) | |
e89065a1 SL |
4586 | { |
4587 | if (integer_zerop (t)) | |
4588 | return inner1; | |
4589 | else if (integer_onep (t)) | |
4590 | return boolean_true_node; | |
8236c8eb JJ |
4591 | /* If both are the same, we can apply the identity |
4592 | (x OR x) == x. */ | |
4593 | else if (partial && same_bool_result_p (t, partial)) | |
4594 | return t; | |
e89065a1 SL |
4595 | } |
4596 | ||
4597 | /* Handle the AND case, where we are redistributing: | |
4598 | (inner1 AND inner2) OR (op2a code2 op2b) | |
4599 | => (t AND (inner1 OR (op2a code2 op2b))) | |
4600 | => (t AND partial) */ | |
4601 | else | |
4602 | { | |
4603 | if (integer_zerop (t)) | |
4604 | return boolean_false_node; | |
4605 | else if (partial) | |
4606 | { | |
4607 | /* We already got a simplification for the other | |
4608 | operand to the redistributed AND expression. The | |
4609 | interesting case is when at least one is true. | |
4610 | Or, if both are the same, we can apply the identity | |
8236c8eb | 4611 | (x AND x) == x. */ |
e89065a1 SL |
4612 | if (integer_onep (partial)) |
4613 | return t; | |
4614 | else if (integer_onep (t)) | |
4615 | return partial; | |
4616 | else if (same_bool_result_p (t, partial)) | |
8236c8eb | 4617 | return t; |
e89065a1 SL |
4618 | } |
4619 | } | |
4620 | } | |
4621 | } | |
4622 | return NULL_TREE; | |
4623 | } | |
4624 | ||
4625 | /* Try to simplify the OR of two comparisons defined by | |
4626 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
4627 | If this can be done without constructing an intermediate value, | |
4628 | return the resulting tree; otherwise NULL_TREE is returned. | |
4629 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
4630 | in the first comparison but not the second. */ | |
4631 | ||
4632 | static tree | |
4633 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
4634 | enum tree_code code2, tree op2a, tree op2b) | |
4635 | { | |
ae22ac3c | 4636 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
31ed6226 | 4637 | |
e89065a1 SL |
4638 | /* First check for ((x CODE1 y) OR (x CODE2 y)). */ |
4639 | if (operand_equal_p (op1a, op2a, 0) | |
4640 | && operand_equal_p (op1b, op2b, 0)) | |
4641 | { | |
eb9820c0 | 4642 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
4643 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
4644 | TRUTH_ORIF_EXPR, code1, code2, | |
31ed6226 | 4645 | truth_type, op1a, op1b); |
e89065a1 SL |
4646 | if (t) |
4647 | return t; | |
4648 | } | |
4649 | ||
4650 | /* Likewise the swapped case of the above. */ | |
4651 | if (operand_equal_p (op1a, op2b, 0) | |
4652 | && operand_equal_p (op1b, op2a, 0)) | |
4653 | { | |
eb9820c0 | 4654 | /* Result will be either NULL_TREE, or a combined comparison. */ |
e89065a1 SL |
4655 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
4656 | TRUTH_ORIF_EXPR, code1, | |
4657 | swap_tree_comparison (code2), | |
31ed6226 | 4658 | truth_type, op1a, op1b); |
e89065a1 SL |
4659 | if (t) |
4660 | return t; | |
4661 | } | |
4662 | ||
4663 | /* If both comparisons are of the same value against constants, we might | |
4664 | be able to merge them. */ | |
4665 | if (operand_equal_p (op1a, op2a, 0) | |
4666 | && TREE_CODE (op1b) == INTEGER_CST | |
4667 | && TREE_CODE (op2b) == INTEGER_CST) | |
4668 | { | |
4669 | int cmp = tree_int_cst_compare (op1b, op2b); | |
4670 | ||
4671 | /* If we have (op1a != op1b), we should either be able to | |
4672 | return that or TRUE, depending on whether the constant op1b | |
4673 | also satisfies the other comparison against op2b. */ | |
4674 | if (code1 == NE_EXPR) | |
4675 | { | |
4676 | bool done = true; | |
4677 | bool val; | |
4678 | switch (code2) | |
4679 | { | |
4680 | case EQ_EXPR: val = (cmp == 0); break; | |
4681 | case NE_EXPR: val = (cmp != 0); break; | |
4682 | case LT_EXPR: val = (cmp < 0); break; | |
4683 | case GT_EXPR: val = (cmp > 0); break; | |
4684 | case LE_EXPR: val = (cmp <= 0); break; | |
4685 | case GE_EXPR: val = (cmp >= 0); break; | |
4686 | default: done = false; | |
4687 | } | |
4688 | if (done) | |
4689 | { | |
4690 | if (val) | |
4691 | return boolean_true_node; | |
4692 | else | |
4693 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4694 | } | |
4695 | } | |
4696 | /* Likewise if the second comparison is a != comparison. */ | |
4697 | else if (code2 == NE_EXPR) | |
4698 | { | |
4699 | bool done = true; | |
4700 | bool val; | |
4701 | switch (code1) | |
4702 | { | |
4703 | case EQ_EXPR: val = (cmp == 0); break; | |
4704 | case NE_EXPR: val = (cmp != 0); break; | |
4705 | case LT_EXPR: val = (cmp > 0); break; | |
4706 | case GT_EXPR: val = (cmp < 0); break; | |
4707 | case LE_EXPR: val = (cmp >= 0); break; | |
4708 | case GE_EXPR: val = (cmp <= 0); break; | |
4709 | default: done = false; | |
4710 | } | |
4711 | if (done) | |
4712 | { | |
4713 | if (val) | |
4714 | return boolean_true_node; | |
4715 | else | |
4716 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4717 | } | |
4718 | } | |
4719 | ||
4720 | /* See if an equality test is redundant with the other comparison. */ | |
4721 | else if (code1 == EQ_EXPR) | |
4722 | { | |
4723 | bool val; | |
4724 | switch (code2) | |
4725 | { | |
4726 | case EQ_EXPR: val = (cmp == 0); break; | |
4727 | case NE_EXPR: val = (cmp != 0); break; | |
4728 | case LT_EXPR: val = (cmp < 0); break; | |
4729 | case GT_EXPR: val = (cmp > 0); break; | |
4730 | case LE_EXPR: val = (cmp <= 0); break; | |
4731 | case GE_EXPR: val = (cmp >= 0); break; | |
4732 | default: | |
4733 | val = false; | |
4734 | } | |
4735 | if (val) | |
4736 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4737 | } | |
4738 | else if (code2 == EQ_EXPR) | |
4739 | { | |
4740 | bool val; | |
4741 | switch (code1) | |
4742 | { | |
4743 | case EQ_EXPR: val = (cmp == 0); break; | |
4744 | case NE_EXPR: val = (cmp != 0); break; | |
4745 | case LT_EXPR: val = (cmp > 0); break; | |
4746 | case GT_EXPR: val = (cmp < 0); break; | |
4747 | case LE_EXPR: val = (cmp >= 0); break; | |
4748 | case GE_EXPR: val = (cmp <= 0); break; | |
4749 | default: | |
4750 | val = false; | |
4751 | } | |
4752 | if (val) | |
4753 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4754 | } | |
4755 | ||
4756 | /* Chose the less restrictive of two < or <= comparisons. */ | |
4757 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
4758 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
4759 | { | |
4760 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
4761 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4762 | else | |
4763 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4764 | } | |
4765 | ||
4766 | /* Likewise chose the less restrictive of two > or >= comparisons. */ | |
4767 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
4768 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
4769 | { | |
4770 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
4771 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4772 | else | |
4773 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4774 | } | |
4775 | ||
4776 | /* Check for singleton ranges. */ | |
4777 | else if (cmp == 0 | |
4778 | && ((code1 == LT_EXPR && code2 == GT_EXPR) | |
4779 | || (code1 == GT_EXPR && code2 == LT_EXPR))) | |
4780 | return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b); | |
4781 | ||
4782 | /* Check for less/greater pairs that don't restrict the range at all. */ | |
4783 | else if (cmp >= 0 | |
4784 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
4785 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
4786 | return boolean_true_node; | |
4787 | else if (cmp <= 0 | |
4788 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
4789 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
4790 | return boolean_true_node; | |
4791 | } | |
4792 | ||
4793 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
4794 | NAME's definition is a truth value. See if there are any simplifications | |
4795 | that can be done against the NAME's definition. */ | |
4796 | if (TREE_CODE (op1a) == SSA_NAME | |
4797 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
4798 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
4799 | { | |
4800 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
4801 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
4802 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
4803 | switch (gimple_code (stmt)) | |
4804 | { | |
4805 | case GIMPLE_ASSIGN: | |
4806 | /* Try to simplify by copy-propagating the definition. */ | |
4807 | return or_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
4808 | ||
4809 | case GIMPLE_PHI: | |
4810 | /* If every argument to the PHI produces the same result when | |
4811 | ORed with the second comparison, we win. | |
4812 | Do not do this unless the type is bool since we need a bool | |
4813 | result here anyway. */ | |
4814 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
4815 | { | |
4816 | tree result = NULL_TREE; | |
4817 | unsigned i; | |
4818 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
4819 | { | |
4820 | tree arg = gimple_phi_arg_def (stmt, i); | |
4821 | ||
4822 | /* If this PHI has itself as an argument, ignore it. | |
4823 | If all the other args produce the same result, | |
4824 | we're still OK. */ | |
4825 | if (arg == gimple_phi_result (stmt)) | |
4826 | continue; | |
4827 | else if (TREE_CODE (arg) == INTEGER_CST) | |
4828 | { | |
4829 | if (invert ? integer_zerop (arg) : integer_nonzerop (arg)) | |
4830 | { | |
4831 | if (!result) | |
4832 | result = boolean_true_node; | |
4833 | else if (!integer_onep (result)) | |
4834 | return NULL_TREE; | |
4835 | } | |
4836 | else if (!result) | |
4837 | result = fold_build2 (code2, boolean_type_node, | |
4838 | op2a, op2b); | |
4839 | else if (!same_bool_comparison_p (result, | |
4840 | code2, op2a, op2b)) | |
4841 | return NULL_TREE; | |
4842 | } | |
0e8b84ec JJ |
4843 | else if (TREE_CODE (arg) == SSA_NAME |
4844 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
e89065a1 | 4845 | { |
6c66f733 JJ |
4846 | tree temp; |
4847 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
4848 | /* In simple cases we can look through PHI nodes, | |
4849 | but we have to be careful with loops. | |
4850 | See PR49073. */ | |
4851 | if (! dom_info_available_p (CDI_DOMINATORS) | |
4852 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
4853 | || dominated_by_p (CDI_DOMINATORS, | |
4854 | gimple_bb (def_stmt), | |
4855 | gimple_bb (stmt))) | |
4856 | return NULL_TREE; | |
4857 | temp = or_var_with_comparison (arg, invert, code2, | |
4858 | op2a, op2b); | |
e89065a1 SL |
4859 | if (!temp) |
4860 | return NULL_TREE; | |
4861 | else if (!result) | |
4862 | result = temp; | |
4863 | else if (!same_bool_result_p (result, temp)) | |
4864 | return NULL_TREE; | |
4865 | } | |
4866 | else | |
4867 | return NULL_TREE; | |
4868 | } | |
4869 | return result; | |
4870 | } | |
4871 | ||
4872 | default: | |
4873 | break; | |
4874 | } | |
4875 | } | |
4876 | return NULL_TREE; | |
4877 | } | |
4878 | ||
4879 | /* Try to simplify the OR of two comparisons, specified by | |
4880 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
4881 | If this can be simplified to a single expression (without requiring | |
4882 | introducing more SSA variables to hold intermediate values), | |
4883 | return the resulting tree. Otherwise return NULL_TREE. | |
4884 | If the result expression is non-null, it has boolean type. */ | |
4885 | ||
4886 | tree | |
4887 | maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
4888 | enum tree_code code2, tree op2a, tree op2b) | |
4889 | { | |
4890 | tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
4891 | if (t) | |
4892 | return t; | |
4893 | else | |
4894 | return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
4895 | } | |
cfef45c8 RG |
4896 | |
4897 | ||
4898 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
4899 | ||
4900 | Either NULL_TREE, a simplified but non-constant or a constant | |
4901 | is returned. | |
4902 | ||
4903 | ??? This should go into a gimple-fold-inline.h file to be eventually | |
4904 | privatized with the single valueize function used in the various TUs | |
4905 | to avoid the indirect function call overhead. */ | |
4906 | ||
4907 | tree | |
d2a85801 RB |
4908 | gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree), |
4909 | tree (*gvalueize) (tree)) | |
cfef45c8 | 4910 | { |
45cc9f96 RB |
4911 | code_helper rcode; |
4912 | tree ops[3] = {}; | |
4913 | /* ??? The SSA propagators do not correctly deal with following SSA use-def | |
4914 | edges if there are intermediate VARYING defs. For this reason | |
4915 | do not follow SSA edges here even though SCCVN can technically | |
4916 | just deal fine with that. */ | |
0ff093d8 | 4917 | if (gimple_simplify (stmt, &rcode, ops, NULL, gvalueize, valueize) |
45cc9f96 RB |
4918 | && rcode.is_tree_code () |
4919 | && (TREE_CODE_LENGTH ((tree_code) rcode) == 0 | |
4920 | || ((tree_code) rcode) == ADDR_EXPR) | |
4921 | && is_gimple_val (ops[0])) | |
4922 | { | |
4923 | tree res = ops[0]; | |
4924 | if (dump_file && dump_flags & TDF_DETAILS) | |
4925 | { | |
4926 | fprintf (dump_file, "Match-and-simplified "); | |
4927 | print_gimple_expr (dump_file, stmt, 0, TDF_SLIM); | |
4928 | fprintf (dump_file, " to "); | |
4929 | print_generic_expr (dump_file, res, 0); | |
4930 | fprintf (dump_file, "\n"); | |
4931 | } | |
4932 | return res; | |
4933 | } | |
4934 | ||
cfef45c8 RG |
4935 | location_t loc = gimple_location (stmt); |
4936 | switch (gimple_code (stmt)) | |
4937 | { | |
4938 | case GIMPLE_ASSIGN: | |
4939 | { | |
4940 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
4941 | ||
4942 | switch (get_gimple_rhs_class (subcode)) | |
4943 | { | |
4944 | case GIMPLE_SINGLE_RHS: | |
4945 | { | |
4946 | tree rhs = gimple_assign_rhs1 (stmt); | |
4947 | enum tree_code_class kind = TREE_CODE_CLASS (subcode); | |
4948 | ||
4949 | if (TREE_CODE (rhs) == SSA_NAME) | |
4950 | { | |
4951 | /* If the RHS is an SSA_NAME, return its known constant value, | |
4952 | if any. */ | |
4953 | return (*valueize) (rhs); | |
4954 | } | |
4955 | /* Handle propagating invariant addresses into address | |
4956 | operations. */ | |
4957 | else if (TREE_CODE (rhs) == ADDR_EXPR | |
4958 | && !is_gimple_min_invariant (rhs)) | |
4959 | { | |
d25c4172 | 4960 | HOST_WIDE_INT offset = 0; |
cfef45c8 RG |
4961 | tree base; |
4962 | base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0), | |
4963 | &offset, | |
4964 | valueize); | |
4965 | if (base | |
4966 | && (CONSTANT_CLASS_P (base) | |
4967 | || decl_address_invariant_p (base))) | |
4968 | return build_invariant_address (TREE_TYPE (rhs), | |
4969 | base, offset); | |
4970 | } | |
4971 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
4972 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
4973 | && (CONSTRUCTOR_NELTS (rhs) | |
4974 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
4975 | { | |
4976 | unsigned i; | |
d2a12ae7 | 4977 | tree val, *vec; |
cfef45c8 | 4978 | |
d2a12ae7 RG |
4979 | vec = XALLOCAVEC (tree, |
4980 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))); | |
cfef45c8 RG |
4981 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) |
4982 | { | |
4983 | val = (*valueize) (val); | |
4984 | if (TREE_CODE (val) == INTEGER_CST | |
4985 | || TREE_CODE (val) == REAL_CST | |
4986 | || TREE_CODE (val) == FIXED_CST) | |
d2a12ae7 | 4987 | vec[i] = val; |
cfef45c8 RG |
4988 | else |
4989 | return NULL_TREE; | |
4990 | } | |
4991 | ||
d2a12ae7 | 4992 | return build_vector (TREE_TYPE (rhs), vec); |
cfef45c8 | 4993 | } |
bdf37f7a JH |
4994 | if (subcode == OBJ_TYPE_REF) |
4995 | { | |
4996 | tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs)); | |
4997 | /* If callee is constant, we can fold away the wrapper. */ | |
4998 | if (is_gimple_min_invariant (val)) | |
4999 | return val; | |
5000 | } | |
cfef45c8 RG |
5001 | |
5002 | if (kind == tcc_reference) | |
5003 | { | |
5004 | if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR | |
5005 | || TREE_CODE (rhs) == REALPART_EXPR | |
5006 | || TREE_CODE (rhs) == IMAGPART_EXPR) | |
5007 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
5008 | { | |
5009 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
5010 | return fold_unary_loc (EXPR_LOCATION (rhs), | |
5011 | TREE_CODE (rhs), | |
5012 | TREE_TYPE (rhs), val); | |
5013 | } | |
5014 | else if (TREE_CODE (rhs) == BIT_FIELD_REF | |
5015 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
5016 | { | |
5017 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
5018 | return fold_ternary_loc (EXPR_LOCATION (rhs), | |
5019 | TREE_CODE (rhs), | |
5020 | TREE_TYPE (rhs), val, | |
5021 | TREE_OPERAND (rhs, 1), | |
5022 | TREE_OPERAND (rhs, 2)); | |
5023 | } | |
5024 | else if (TREE_CODE (rhs) == MEM_REF | |
5025 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
5026 | { | |
5027 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
5028 | if (TREE_CODE (val) == ADDR_EXPR | |
5029 | && is_gimple_min_invariant (val)) | |
5030 | { | |
5031 | tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs), | |
5032 | unshare_expr (val), | |
5033 | TREE_OPERAND (rhs, 1)); | |
5034 | if (tem) | |
5035 | rhs = tem; | |
5036 | } | |
5037 | } | |
5038 | return fold_const_aggregate_ref_1 (rhs, valueize); | |
5039 | } | |
5040 | else if (kind == tcc_declaration) | |
5041 | return get_symbol_constant_value (rhs); | |
5042 | return rhs; | |
5043 | } | |
5044 | ||
5045 | case GIMPLE_UNARY_RHS: | |
f3582e54 | 5046 | return NULL_TREE; |
cfef45c8 RG |
5047 | |
5048 | case GIMPLE_BINARY_RHS: | |
5049 | { | |
5050 | /* Handle binary operators that can appear in GIMPLE form. */ | |
5051 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
5052 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
5053 | ||
5054 | /* Translate &x + CST into an invariant form suitable for | |
5055 | further propagation. */ | |
5056 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR | |
5057 | && TREE_CODE (op0) == ADDR_EXPR | |
5058 | && TREE_CODE (op1) == INTEGER_CST) | |
5059 | { | |
5060 | tree off = fold_convert (ptr_type_node, op1); | |
4d59a001 RG |
5061 | return build_fold_addr_expr_loc |
5062 | (loc, | |
5063 | fold_build2 (MEM_REF, | |
cfef45c8 RG |
5064 | TREE_TYPE (TREE_TYPE (op0)), |
5065 | unshare_expr (op0), off)); | |
5066 | } | |
5067 | ||
5068 | return fold_binary_loc (loc, subcode, | |
5069 | gimple_expr_type (stmt), op0, op1); | |
5070 | } | |
5071 | ||
5072 | case GIMPLE_TERNARY_RHS: | |
5073 | { | |
5074 | /* Handle ternary operators that can appear in GIMPLE form. */ | |
5075 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
5076 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
5077 | tree op2 = (*valueize) (gimple_assign_rhs3 (stmt)); | |
5078 | ||
d3878abf RG |
5079 | /* Fold embedded expressions in ternary codes. */ |
5080 | if ((subcode == COND_EXPR | |
5081 | || subcode == VEC_COND_EXPR) | |
5082 | && COMPARISON_CLASS_P (op0)) | |
5083 | { | |
5084 | tree op00 = (*valueize) (TREE_OPERAND (op0, 0)); | |
5085 | tree op01 = (*valueize) (TREE_OPERAND (op0, 1)); | |
5086 | tree tem = fold_binary_loc (loc, TREE_CODE (op0), | |
5087 | TREE_TYPE (op0), op00, op01); | |
5088 | if (tem) | |
5089 | op0 = tem; | |
5090 | } | |
5091 | ||
cfef45c8 RG |
5092 | return fold_ternary_loc (loc, subcode, |
5093 | gimple_expr_type (stmt), op0, op1, op2); | |
5094 | } | |
5095 | ||
5096 | default: | |
5097 | gcc_unreachable (); | |
5098 | } | |
5099 | } | |
5100 | ||
5101 | case GIMPLE_CALL: | |
5102 | { | |
25583c4f | 5103 | tree fn; |
538dd0b7 | 5104 | gcall *call_stmt = as_a <gcall *> (stmt); |
25583c4f RS |
5105 | |
5106 | if (gimple_call_internal_p (stmt)) | |
31e071ae MP |
5107 | { |
5108 | enum tree_code subcode = ERROR_MARK; | |
5109 | switch (gimple_call_internal_fn (stmt)) | |
5110 | { | |
5111 | case IFN_UBSAN_CHECK_ADD: | |
5112 | subcode = PLUS_EXPR; | |
5113 | break; | |
5114 | case IFN_UBSAN_CHECK_SUB: | |
5115 | subcode = MINUS_EXPR; | |
5116 | break; | |
5117 | case IFN_UBSAN_CHECK_MUL: | |
5118 | subcode = MULT_EXPR; | |
5119 | break; | |
5120 | default: | |
5121 | return NULL_TREE; | |
5122 | } | |
368b454d JJ |
5123 | tree arg0 = gimple_call_arg (stmt, 0); |
5124 | tree arg1 = gimple_call_arg (stmt, 1); | |
5125 | tree op0 = (*valueize) (arg0); | |
5126 | tree op1 = (*valueize) (arg1); | |
31e071ae MP |
5127 | |
5128 | if (TREE_CODE (op0) != INTEGER_CST | |
5129 | || TREE_CODE (op1) != INTEGER_CST) | |
368b454d JJ |
5130 | { |
5131 | switch (subcode) | |
5132 | { | |
5133 | case MULT_EXPR: | |
5134 | /* x * 0 = 0 * x = 0 without overflow. */ | |
5135 | if (integer_zerop (op0) || integer_zerop (op1)) | |
5136 | return build_zero_cst (TREE_TYPE (arg0)); | |
5137 | break; | |
5138 | case MINUS_EXPR: | |
5139 | /* y - y = 0 without overflow. */ | |
5140 | if (operand_equal_p (op0, op1, 0)) | |
5141 | return build_zero_cst (TREE_TYPE (arg0)); | |
5142 | break; | |
5143 | default: | |
5144 | break; | |
5145 | } | |
5146 | } | |
5147 | tree res | |
5148 | = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1); | |
31e071ae MP |
5149 | if (res |
5150 | && TREE_CODE (res) == INTEGER_CST | |
5151 | && !TREE_OVERFLOW (res)) | |
5152 | return res; | |
5153 | return NULL_TREE; | |
5154 | } | |
25583c4f RS |
5155 | |
5156 | fn = (*valueize) (gimple_call_fn (stmt)); | |
cfef45c8 RG |
5157 | if (TREE_CODE (fn) == ADDR_EXPR |
5158 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
5c944c6c RB |
5159 | && DECL_BUILT_IN (TREE_OPERAND (fn, 0)) |
5160 | && gimple_builtin_call_types_compatible_p (stmt, | |
5161 | TREE_OPERAND (fn, 0))) | |
cfef45c8 RG |
5162 | { |
5163 | tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt)); | |
a6a0570f | 5164 | tree retval; |
cfef45c8 RG |
5165 | unsigned i; |
5166 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
5167 | args[i] = (*valueize) (gimple_call_arg (stmt, i)); | |
a6a0570f | 5168 | retval = fold_builtin_call_array (loc, |
538dd0b7 | 5169 | gimple_call_return_type (call_stmt), |
cfef45c8 | 5170 | fn, gimple_call_num_args (stmt), args); |
cfef45c8 | 5171 | if (retval) |
5c944c6c RB |
5172 | { |
5173 | /* fold_call_expr wraps the result inside a NOP_EXPR. */ | |
5174 | STRIP_NOPS (retval); | |
538dd0b7 DM |
5175 | retval = fold_convert (gimple_call_return_type (call_stmt), |
5176 | retval); | |
5c944c6c | 5177 | } |
cfef45c8 RG |
5178 | return retval; |
5179 | } | |
5180 | return NULL_TREE; | |
5181 | } | |
5182 | ||
5183 | default: | |
5184 | return NULL_TREE; | |
5185 | } | |
5186 | } | |
5187 | ||
5188 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
5189 | Returns NULL_TREE if folding to a constant is not possible, otherwise | |
5190 | returns a constant according to is_gimple_min_invariant. */ | |
5191 | ||
5192 | tree | |
5193 | gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree)) | |
5194 | { | |
5195 | tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize); | |
5196 | if (res && is_gimple_min_invariant (res)) | |
5197 | return res; | |
5198 | return NULL_TREE; | |
5199 | } | |
5200 | ||
5201 | ||
5202 | /* The following set of functions are supposed to fold references using | |
5203 | their constant initializers. */ | |
5204 | ||
cfef45c8 RG |
5205 | /* See if we can find constructor defining value of BASE. |
5206 | When we know the consructor with constant offset (such as | |
5207 | base is array[40] and we do know constructor of array), then | |
5208 | BIT_OFFSET is adjusted accordingly. | |
5209 | ||
5210 | As a special case, return error_mark_node when constructor | |
5211 | is not explicitly available, but it is known to be zero | |
5212 | such as 'static const int a;'. */ | |
5213 | static tree | |
5214 | get_base_constructor (tree base, HOST_WIDE_INT *bit_offset, | |
5215 | tree (*valueize)(tree)) | |
5216 | { | |
5217 | HOST_WIDE_INT bit_offset2, size, max_size; | |
5218 | if (TREE_CODE (base) == MEM_REF) | |
5219 | { | |
5220 | if (!integer_zerop (TREE_OPERAND (base, 1))) | |
5221 | { | |
9541ffee | 5222 | if (!tree_fits_shwi_p (TREE_OPERAND (base, 1))) |
cfef45c8 | 5223 | return NULL_TREE; |
807e902e | 5224 | *bit_offset += (mem_ref_offset (base).to_short_addr () |
cfef45c8 RG |
5225 | * BITS_PER_UNIT); |
5226 | } | |
5227 | ||
5228 | if (valueize | |
5229 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) | |
5230 | base = valueize (TREE_OPERAND (base, 0)); | |
5231 | if (!base || TREE_CODE (base) != ADDR_EXPR) | |
5232 | return NULL_TREE; | |
5233 | base = TREE_OPERAND (base, 0); | |
5234 | } | |
5235 | ||
5236 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
5237 | DECL_INITIAL. If BASE is a nested reference into another | |
5238 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
5239 | the inner reference. */ | |
5240 | switch (TREE_CODE (base)) | |
5241 | { | |
5242 | case VAR_DECL: | |
cfef45c8 | 5243 | case CONST_DECL: |
6a6dac52 JH |
5244 | { |
5245 | tree init = ctor_for_folding (base); | |
5246 | ||
688010ba | 5247 | /* Our semantic is exact opposite of ctor_for_folding; |
6a6dac52 JH |
5248 | NULL means unknown, while error_mark_node is 0. */ |
5249 | if (init == error_mark_node) | |
5250 | return NULL_TREE; | |
5251 | if (!init) | |
5252 | return error_mark_node; | |
5253 | return init; | |
5254 | } | |
cfef45c8 RG |
5255 | |
5256 | case ARRAY_REF: | |
5257 | case COMPONENT_REF: | |
5258 | base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size); | |
5259 | if (max_size == -1 || size != max_size) | |
5260 | return NULL_TREE; | |
5261 | *bit_offset += bit_offset2; | |
5262 | return get_base_constructor (base, bit_offset, valueize); | |
5263 | ||
5264 | case STRING_CST: | |
5265 | case CONSTRUCTOR: | |
5266 | return base; | |
5267 | ||
5268 | default: | |
5269 | return NULL_TREE; | |
5270 | } | |
5271 | } | |
5272 | ||
cfef45c8 RG |
5273 | /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size |
5274 | SIZE to the memory at bit OFFSET. */ | |
5275 | ||
5276 | static tree | |
5277 | fold_array_ctor_reference (tree type, tree ctor, | |
5278 | unsigned HOST_WIDE_INT offset, | |
c44c2088 JH |
5279 | unsigned HOST_WIDE_INT size, |
5280 | tree from_decl) | |
cfef45c8 RG |
5281 | { |
5282 | unsigned HOST_WIDE_INT cnt; | |
5283 | tree cfield, cval; | |
807e902e KZ |
5284 | offset_int low_bound; |
5285 | offset_int elt_size; | |
5286 | offset_int index, max_index; | |
5287 | offset_int access_index; | |
b48e22b2 | 5288 | tree domain_type = NULL_TREE, index_type = NULL_TREE; |
cfef45c8 RG |
5289 | HOST_WIDE_INT inner_offset; |
5290 | ||
5291 | /* Compute low bound and elt size. */ | |
eb8f1123 RG |
5292 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE) |
5293 | domain_type = TYPE_DOMAIN (TREE_TYPE (ctor)); | |
cfef45c8 RG |
5294 | if (domain_type && TYPE_MIN_VALUE (domain_type)) |
5295 | { | |
5296 | /* Static constructors for variably sized objects makes no sense. */ | |
5297 | gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST); | |
b48e22b2 | 5298 | index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type)); |
807e902e | 5299 | low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type)); |
cfef45c8 RG |
5300 | } |
5301 | else | |
807e902e | 5302 | low_bound = 0; |
cfef45c8 | 5303 | /* Static constructors for variably sized objects makes no sense. */ |
c3284718 | 5304 | gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) |
cfef45c8 | 5305 | == INTEGER_CST); |
807e902e | 5306 | elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))); |
cfef45c8 RG |
5307 | |
5308 | /* We can handle only constantly sized accesses that are known to not | |
5309 | be larger than size of array element. */ | |
5310 | if (!TYPE_SIZE_UNIT (type) | |
5311 | || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST | |
807e902e KZ |
5312 | || wi::lts_p (elt_size, wi::to_offset (TYPE_SIZE_UNIT (type))) |
5313 | || elt_size == 0) | |
cfef45c8 RG |
5314 | return NULL_TREE; |
5315 | ||
5316 | /* Compute the array index we look for. */ | |
807e902e KZ |
5317 | access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT), |
5318 | elt_size); | |
27bcd47c | 5319 | access_index += low_bound; |
b48e22b2 | 5320 | if (index_type) |
807e902e KZ |
5321 | access_index = wi::ext (access_index, TYPE_PRECISION (index_type), |
5322 | TYPE_SIGN (index_type)); | |
cfef45c8 RG |
5323 | |
5324 | /* And offset within the access. */ | |
27bcd47c | 5325 | inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT); |
cfef45c8 RG |
5326 | |
5327 | /* See if the array field is large enough to span whole access. We do not | |
5328 | care to fold accesses spanning multiple array indexes. */ | |
27bcd47c | 5329 | if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT) |
cfef45c8 RG |
5330 | return NULL_TREE; |
5331 | ||
807e902e | 5332 | index = low_bound - 1; |
b48e22b2 | 5333 | if (index_type) |
807e902e KZ |
5334 | index = wi::ext (index, TYPE_PRECISION (index_type), |
5335 | TYPE_SIGN (index_type)); | |
b48e22b2 | 5336 | |
cfef45c8 RG |
5337 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
5338 | { | |
5339 | /* Array constructor might explicitely set index, or specify range | |
5340 | or leave index NULL meaning that it is next index after previous | |
5341 | one. */ | |
5342 | if (cfield) | |
5343 | { | |
5344 | if (TREE_CODE (cfield) == INTEGER_CST) | |
807e902e | 5345 | max_index = index = wi::to_offset (cfield); |
cfef45c8 RG |
5346 | else |
5347 | { | |
5348 | gcc_assert (TREE_CODE (cfield) == RANGE_EXPR); | |
807e902e KZ |
5349 | index = wi::to_offset (TREE_OPERAND (cfield, 0)); |
5350 | max_index = wi::to_offset (TREE_OPERAND (cfield, 1)); | |
cfef45c8 RG |
5351 | } |
5352 | } | |
5353 | else | |
b48e22b2 | 5354 | { |
807e902e | 5355 | index += 1; |
b48e22b2 | 5356 | if (index_type) |
807e902e KZ |
5357 | index = wi::ext (index, TYPE_PRECISION (index_type), |
5358 | TYPE_SIGN (index_type)); | |
b48e22b2 EB |
5359 | max_index = index; |
5360 | } | |
cfef45c8 RG |
5361 | |
5362 | /* Do we have match? */ | |
807e902e KZ |
5363 | if (wi::cmpu (access_index, index) >= 0 |
5364 | && wi::cmpu (access_index, max_index) <= 0) | |
c44c2088 JH |
5365 | return fold_ctor_reference (type, cval, inner_offset, size, |
5366 | from_decl); | |
cfef45c8 RG |
5367 | } |
5368 | /* When memory is not explicitely mentioned in constructor, | |
5369 | it is 0 (or out of range). */ | |
5370 | return build_zero_cst (type); | |
5371 | } | |
5372 | ||
5373 | /* CTOR is CONSTRUCTOR of an aggregate or vector. | |
5374 | Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */ | |
5375 | ||
5376 | static tree | |
5377 | fold_nonarray_ctor_reference (tree type, tree ctor, | |
5378 | unsigned HOST_WIDE_INT offset, | |
c44c2088 JH |
5379 | unsigned HOST_WIDE_INT size, |
5380 | tree from_decl) | |
cfef45c8 RG |
5381 | { |
5382 | unsigned HOST_WIDE_INT cnt; | |
5383 | tree cfield, cval; | |
5384 | ||
5385 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, | |
5386 | cval) | |
5387 | { | |
5388 | tree byte_offset = DECL_FIELD_OFFSET (cfield); | |
5389 | tree field_offset = DECL_FIELD_BIT_OFFSET (cfield); | |
5390 | tree field_size = DECL_SIZE (cfield); | |
807e902e KZ |
5391 | offset_int bitoffset; |
5392 | offset_int bitoffset_end, access_end; | |
cfef45c8 RG |
5393 | |
5394 | /* Variable sized objects in static constructors makes no sense, | |
5395 | but field_size can be NULL for flexible array members. */ | |
5396 | gcc_assert (TREE_CODE (field_offset) == INTEGER_CST | |
5397 | && TREE_CODE (byte_offset) == INTEGER_CST | |
5398 | && (field_size != NULL_TREE | |
5399 | ? TREE_CODE (field_size) == INTEGER_CST | |
5400 | : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE)); | |
5401 | ||
5402 | /* Compute bit offset of the field. */ | |
807e902e KZ |
5403 | bitoffset = (wi::to_offset (field_offset) |
5404 | + wi::lshift (wi::to_offset (byte_offset), | |
5405 | LOG2_BITS_PER_UNIT)); | |
cfef45c8 RG |
5406 | /* Compute bit offset where the field ends. */ |
5407 | if (field_size != NULL_TREE) | |
807e902e | 5408 | bitoffset_end = bitoffset + wi::to_offset (field_size); |
cfef45c8 | 5409 | else |
807e902e | 5410 | bitoffset_end = 0; |
cfef45c8 | 5411 | |
807e902e | 5412 | access_end = offset_int (offset) + size; |
b8b2b009 JJ |
5413 | |
5414 | /* Is there any overlap between [OFFSET, OFFSET+SIZE) and | |
5415 | [BITOFFSET, BITOFFSET_END)? */ | |
807e902e | 5416 | if (wi::cmps (access_end, bitoffset) > 0 |
cfef45c8 | 5417 | && (field_size == NULL_TREE |
807e902e | 5418 | || wi::lts_p (offset, bitoffset_end))) |
cfef45c8 | 5419 | { |
807e902e | 5420 | offset_int inner_offset = offset_int (offset) - bitoffset; |
cfef45c8 RG |
5421 | /* We do have overlap. Now see if field is large enough to |
5422 | cover the access. Give up for accesses spanning multiple | |
5423 | fields. */ | |
807e902e | 5424 | if (wi::cmps (access_end, bitoffset_end) > 0) |
cfef45c8 | 5425 | return NULL_TREE; |
807e902e | 5426 | if (wi::lts_p (offset, bitoffset)) |
b8b2b009 | 5427 | return NULL_TREE; |
cfef45c8 | 5428 | return fold_ctor_reference (type, cval, |
27bcd47c | 5429 | inner_offset.to_uhwi (), size, |
c44c2088 | 5430 | from_decl); |
cfef45c8 RG |
5431 | } |
5432 | } | |
5433 | /* When memory is not explicitely mentioned in constructor, it is 0. */ | |
5434 | return build_zero_cst (type); | |
5435 | } | |
5436 | ||
5437 | /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE | |
5438 | to the memory at bit OFFSET. */ | |
5439 | ||
8403c2cf | 5440 | tree |
cfef45c8 | 5441 | fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset, |
c44c2088 | 5442 | unsigned HOST_WIDE_INT size, tree from_decl) |
cfef45c8 RG |
5443 | { |
5444 | tree ret; | |
5445 | ||
5446 | /* We found the field with exact match. */ | |
5447 | if (useless_type_conversion_p (type, TREE_TYPE (ctor)) | |
5448 | && !offset) | |
9d60be38 | 5449 | return canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
cfef45c8 RG |
5450 | |
5451 | /* We are at the end of walk, see if we can view convert the | |
5452 | result. */ | |
5453 | if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset | |
5454 | /* VIEW_CONVERT_EXPR is defined only for matching sizes. */ | |
3d8208ce TP |
5455 | && !compare_tree_int (TYPE_SIZE (type), size) |
5456 | && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size)) | |
cfef45c8 | 5457 | { |
9d60be38 | 5458 | ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
cfef45c8 RG |
5459 | ret = fold_unary (VIEW_CONVERT_EXPR, type, ret); |
5460 | if (ret) | |
86c5a5c3 | 5461 | STRIP_USELESS_TYPE_CONVERSION (ret); |
cfef45c8 RG |
5462 | return ret; |
5463 | } | |
b2505143 RB |
5464 | /* For constants and byte-aligned/sized reads try to go through |
5465 | native_encode/interpret. */ | |
5466 | if (CONSTANT_CLASS_P (ctor) | |
5467 | && BITS_PER_UNIT == 8 | |
5468 | && offset % BITS_PER_UNIT == 0 | |
5469 | && size % BITS_PER_UNIT == 0 | |
5470 | && size <= MAX_BITSIZE_MODE_ANY_MODE) | |
5471 | { | |
5472 | unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; | |
5473 | if (native_encode_expr (ctor, buf, size / BITS_PER_UNIT, | |
5474 | offset / BITS_PER_UNIT) > 0) | |
5475 | return native_interpret_expr (type, buf, size / BITS_PER_UNIT); | |
5476 | } | |
cfef45c8 RG |
5477 | if (TREE_CODE (ctor) == CONSTRUCTOR) |
5478 | { | |
5479 | ||
eb8f1123 RG |
5480 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE |
5481 | || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE) | |
c44c2088 JH |
5482 | return fold_array_ctor_reference (type, ctor, offset, size, |
5483 | from_decl); | |
cfef45c8 | 5484 | else |
c44c2088 JH |
5485 | return fold_nonarray_ctor_reference (type, ctor, offset, size, |
5486 | from_decl); | |
cfef45c8 RG |
5487 | } |
5488 | ||
5489 | return NULL_TREE; | |
5490 | } | |
5491 | ||
5492 | /* Return the tree representing the element referenced by T if T is an | |
5493 | ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA | |
5494 | names using VALUEIZE. Return NULL_TREE otherwise. */ | |
5495 | ||
5496 | tree | |
5497 | fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree)) | |
5498 | { | |
5499 | tree ctor, idx, base; | |
5500 | HOST_WIDE_INT offset, size, max_size; | |
5501 | tree tem; | |
5502 | ||
f8a7df45 RG |
5503 | if (TREE_THIS_VOLATILE (t)) |
5504 | return NULL_TREE; | |
5505 | ||
3a65ee74 | 5506 | if (DECL_P (t)) |
cfef45c8 RG |
5507 | return get_symbol_constant_value (t); |
5508 | ||
5509 | tem = fold_read_from_constant_string (t); | |
5510 | if (tem) | |
5511 | return tem; | |
5512 | ||
5513 | switch (TREE_CODE (t)) | |
5514 | { | |
5515 | case ARRAY_REF: | |
5516 | case ARRAY_RANGE_REF: | |
5517 | /* Constant indexes are handled well by get_base_constructor. | |
5518 | Only special case variable offsets. | |
5519 | FIXME: This code can't handle nested references with variable indexes | |
5520 | (they will be handled only by iteration of ccp). Perhaps we can bring | |
5521 | get_ref_base_and_extent here and make it use a valueize callback. */ | |
5522 | if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME | |
5523 | && valueize | |
5524 | && (idx = (*valueize) (TREE_OPERAND (t, 1))) | |
b48e22b2 | 5525 | && TREE_CODE (idx) == INTEGER_CST) |
cfef45c8 RG |
5526 | { |
5527 | tree low_bound, unit_size; | |
5528 | ||
5529 | /* If the resulting bit-offset is constant, track it. */ | |
5530 | if ((low_bound = array_ref_low_bound (t), | |
b48e22b2 | 5531 | TREE_CODE (low_bound) == INTEGER_CST) |
cfef45c8 | 5532 | && (unit_size = array_ref_element_size (t), |
807e902e | 5533 | tree_fits_uhwi_p (unit_size))) |
cfef45c8 | 5534 | { |
807e902e KZ |
5535 | offset_int woffset |
5536 | = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound), | |
5537 | TYPE_PRECISION (TREE_TYPE (idx))); | |
5538 | ||
5539 | if (wi::fits_shwi_p (woffset)) | |
5540 | { | |
5541 | offset = woffset.to_shwi (); | |
5542 | /* TODO: This code seems wrong, multiply then check | |
5543 | to see if it fits. */ | |
5544 | offset *= tree_to_uhwi (unit_size); | |
5545 | offset *= BITS_PER_UNIT; | |
5546 | ||
5547 | base = TREE_OPERAND (t, 0); | |
5548 | ctor = get_base_constructor (base, &offset, valueize); | |
5549 | /* Empty constructor. Always fold to 0. */ | |
5550 | if (ctor == error_mark_node) | |
5551 | return build_zero_cst (TREE_TYPE (t)); | |
5552 | /* Out of bound array access. Value is undefined, | |
5553 | but don't fold. */ | |
5554 | if (offset < 0) | |
5555 | return NULL_TREE; | |
5556 | /* We can not determine ctor. */ | |
5557 | if (!ctor) | |
5558 | return NULL_TREE; | |
5559 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, | |
5560 | tree_to_uhwi (unit_size) | |
5561 | * BITS_PER_UNIT, | |
5562 | base); | |
5563 | } | |
cfef45c8 RG |
5564 | } |
5565 | } | |
5566 | /* Fallthru. */ | |
5567 | ||
5568 | case COMPONENT_REF: | |
5569 | case BIT_FIELD_REF: | |
5570 | case TARGET_MEM_REF: | |
5571 | case MEM_REF: | |
5572 | base = get_ref_base_and_extent (t, &offset, &size, &max_size); | |
5573 | ctor = get_base_constructor (base, &offset, valueize); | |
5574 | ||
5575 | /* Empty constructor. Always fold to 0. */ | |
5576 | if (ctor == error_mark_node) | |
5577 | return build_zero_cst (TREE_TYPE (t)); | |
5578 | /* We do not know precise address. */ | |
5579 | if (max_size == -1 || max_size != size) | |
5580 | return NULL_TREE; | |
5581 | /* We can not determine ctor. */ | |
5582 | if (!ctor) | |
5583 | return NULL_TREE; | |
5584 | ||
5585 | /* Out of bound array access. Value is undefined, but don't fold. */ | |
5586 | if (offset < 0) | |
5587 | return NULL_TREE; | |
5588 | ||
c44c2088 JH |
5589 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, |
5590 | base); | |
cfef45c8 RG |
5591 | |
5592 | case REALPART_EXPR: | |
5593 | case IMAGPART_EXPR: | |
5594 | { | |
5595 | tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize); | |
5596 | if (c && TREE_CODE (c) == COMPLEX_CST) | |
5597 | return fold_build1_loc (EXPR_LOCATION (t), | |
5598 | TREE_CODE (t), TREE_TYPE (t), c); | |
5599 | break; | |
5600 | } | |
5601 | ||
5602 | default: | |
5603 | break; | |
5604 | } | |
5605 | ||
5606 | return NULL_TREE; | |
5607 | } | |
5608 | ||
5609 | tree | |
5610 | fold_const_aggregate_ref (tree t) | |
5611 | { | |
5612 | return fold_const_aggregate_ref_1 (t, NULL); | |
5613 | } | |
06bc3ec7 | 5614 | |
85942f45 | 5615 | /* Lookup virtual method with index TOKEN in a virtual table V |
ec77d61f JH |
5616 | at OFFSET. |
5617 | Set CAN_REFER if non-NULL to false if method | |
5618 | is not referable or if the virtual table is ill-formed (such as rewriten | |
5619 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
81fa35bd MJ |
5620 | |
5621 | tree | |
85942f45 JH |
5622 | gimple_get_virt_method_for_vtable (HOST_WIDE_INT token, |
5623 | tree v, | |
ec77d61f JH |
5624 | unsigned HOST_WIDE_INT offset, |
5625 | bool *can_refer) | |
81fa35bd | 5626 | { |
85942f45 JH |
5627 | tree vtable = v, init, fn; |
5628 | unsigned HOST_WIDE_INT size; | |
8c311b50 JH |
5629 | unsigned HOST_WIDE_INT elt_size, access_index; |
5630 | tree domain_type; | |
81fa35bd | 5631 | |
ec77d61f JH |
5632 | if (can_refer) |
5633 | *can_refer = true; | |
5634 | ||
9de2f554 | 5635 | /* First of all double check we have virtual table. */ |
81fa35bd | 5636 | if (TREE_CODE (v) != VAR_DECL |
2aa3da06 | 5637 | || !DECL_VIRTUAL_P (v)) |
ec77d61f | 5638 | { |
ec77d61f JH |
5639 | /* Pass down that we lost track of the target. */ |
5640 | if (can_refer) | |
5641 | *can_refer = false; | |
5642 | return NULL_TREE; | |
5643 | } | |
9de2f554 | 5644 | |
2aa3da06 JH |
5645 | init = ctor_for_folding (v); |
5646 | ||
9de2f554 | 5647 | /* The virtual tables should always be born with constructors |
2aa3da06 JH |
5648 | and we always should assume that they are avaialble for |
5649 | folding. At the moment we do not stream them in all cases, | |
5650 | but it should never happen that ctor seem unreachable. */ | |
5651 | gcc_assert (init); | |
5652 | if (init == error_mark_node) | |
5653 | { | |
5654 | gcc_assert (in_lto_p); | |
ec77d61f JH |
5655 | /* Pass down that we lost track of the target. */ |
5656 | if (can_refer) | |
5657 | *can_refer = false; | |
2aa3da06 JH |
5658 | return NULL_TREE; |
5659 | } | |
81fa35bd | 5660 | gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE); |
ae7e9ddd | 5661 | size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v)))); |
85942f45 | 5662 | offset *= BITS_PER_UNIT; |
81fa35bd | 5663 | offset += token * size; |
9de2f554 | 5664 | |
8c311b50 JH |
5665 | /* Lookup the value in the constructor that is assumed to be array. |
5666 | This is equivalent to | |
5667 | fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init, | |
5668 | offset, size, NULL); | |
5669 | but in a constant time. We expect that frontend produced a simple | |
5670 | array without indexed initializers. */ | |
5671 | ||
5672 | gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE); | |
5673 | domain_type = TYPE_DOMAIN (TREE_TYPE (init)); | |
5674 | gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type))); | |
5675 | elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init)))); | |
5676 | ||
5677 | access_index = offset / BITS_PER_UNIT / elt_size; | |
5678 | gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0); | |
5679 | ||
5680 | /* This code makes an assumption that there are no | |
5681 | indexed fileds produced by C++ FE, so we can directly index the array. */ | |
5682 | if (access_index < CONSTRUCTOR_NELTS (init)) | |
5683 | { | |
5684 | fn = CONSTRUCTOR_ELT (init, access_index)->value; | |
5685 | gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index); | |
5686 | STRIP_NOPS (fn); | |
5687 | } | |
5688 | else | |
5689 | fn = NULL; | |
9de2f554 JH |
5690 | |
5691 | /* For type inconsistent program we may end up looking up virtual method | |
5692 | in virtual table that does not contain TOKEN entries. We may overrun | |
5693 | the virtual table and pick up a constant or RTTI info pointer. | |
5694 | In any case the call is undefined. */ | |
5695 | if (!fn | |
5696 | || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR) | |
5697 | || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL) | |
5698 | fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
5699 | else | |
5700 | { | |
5701 | fn = TREE_OPERAND (fn, 0); | |
5702 | ||
5703 | /* When cgraph node is missing and function is not public, we cannot | |
5704 | devirtualize. This can happen in WHOPR when the actual method | |
5705 | ends up in other partition, because we found devirtualization | |
5706 | possibility too late. */ | |
5707 | if (!can_refer_decl_in_current_unit_p (fn, vtable)) | |
ec77d61f JH |
5708 | { |
5709 | if (can_refer) | |
5710 | { | |
5711 | *can_refer = false; | |
5712 | return fn; | |
5713 | } | |
5714 | return NULL_TREE; | |
5715 | } | |
9de2f554 | 5716 | } |
81fa35bd | 5717 | |
7501ca28 RG |
5718 | /* Make sure we create a cgraph node for functions we'll reference. |
5719 | They can be non-existent if the reference comes from an entry | |
5720 | of an external vtable for example. */ | |
d52f5295 | 5721 | cgraph_node::get_create (fn); |
7501ca28 | 5722 | |
81fa35bd MJ |
5723 | return fn; |
5724 | } | |
5725 | ||
85942f45 JH |
5726 | /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN |
5727 | is integer form of OBJ_TYPE_REF_TOKEN of the reference expression. | |
5728 | KNOWN_BINFO carries the binfo describing the true type of | |
ec77d61f JH |
5729 | OBJ_TYPE_REF_OBJECT(REF). |
5730 | Set CAN_REFER if non-NULL to false if method | |
5731 | is not referable or if the virtual table is ill-formed (such as rewriten | |
5732 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
85942f45 JH |
5733 | |
5734 | tree | |
ec77d61f JH |
5735 | gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo, |
5736 | bool *can_refer) | |
85942f45 JH |
5737 | { |
5738 | unsigned HOST_WIDE_INT offset; | |
5739 | tree v; | |
5740 | ||
5741 | v = BINFO_VTABLE (known_binfo); | |
5742 | /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */ | |
5743 | if (!v) | |
5744 | return NULL_TREE; | |
5745 | ||
5746 | if (!vtable_pointer_value_to_vtable (v, &v, &offset)) | |
ec77d61f JH |
5747 | { |
5748 | if (can_refer) | |
5749 | *can_refer = false; | |
5750 | return NULL_TREE; | |
5751 | } | |
5752 | return gimple_get_virt_method_for_vtable (token, v, offset, can_refer); | |
85942f45 JH |
5753 | } |
5754 | ||
06bc3ec7 BS |
5755 | /* Return true iff VAL is a gimple expression that is known to be |
5756 | non-negative. Restricted to floating-point inputs. */ | |
5757 | ||
5758 | bool | |
5759 | gimple_val_nonnegative_real_p (tree val) | |
5760 | { | |
5761 | gimple def_stmt; | |
5762 | ||
5763 | gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))); | |
5764 | ||
5765 | /* Use existing logic for non-gimple trees. */ | |
5766 | if (tree_expr_nonnegative_p (val)) | |
5767 | return true; | |
5768 | ||
5769 | if (TREE_CODE (val) != SSA_NAME) | |
5770 | return false; | |
5771 | ||
5772 | /* Currently we look only at the immediately defining statement | |
5773 | to make this determination, since recursion on defining | |
5774 | statements of operands can lead to quadratic behavior in the | |
5775 | worst case. This is expected to catch almost all occurrences | |
5776 | in practice. It would be possible to implement limited-depth | |
5777 | recursion if important cases are lost. Alternatively, passes | |
5778 | that need this information (such as the pow/powi lowering code | |
5779 | in the cse_sincos pass) could be revised to provide it through | |
5780 | dataflow propagation. */ | |
5781 | ||
5782 | def_stmt = SSA_NAME_DEF_STMT (val); | |
5783 | ||
5784 | if (is_gimple_assign (def_stmt)) | |
5785 | { | |
5786 | tree op0, op1; | |
5787 | ||
5788 | /* See fold-const.c:tree_expr_nonnegative_p for additional | |
5789 | cases that could be handled with recursion. */ | |
5790 | ||
5791 | switch (gimple_assign_rhs_code (def_stmt)) | |
5792 | { | |
5793 | case ABS_EXPR: | |
5794 | /* Always true for floating-point operands. */ | |
5795 | return true; | |
5796 | ||
5797 | case MULT_EXPR: | |
5798 | /* True if the two operands are identical (since we are | |
5799 | restricted to floating-point inputs). */ | |
5800 | op0 = gimple_assign_rhs1 (def_stmt); | |
5801 | op1 = gimple_assign_rhs2 (def_stmt); | |
5802 | ||
5803 | if (op0 == op1 | |
5804 | || operand_equal_p (op0, op1, 0)) | |
5805 | return true; | |
5806 | ||
5807 | default: | |
5808 | return false; | |
5809 | } | |
5810 | } | |
5811 | else if (is_gimple_call (def_stmt)) | |
5812 | { | |
5813 | tree fndecl = gimple_call_fndecl (def_stmt); | |
5814 | if (fndecl | |
5815 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
5816 | { | |
5817 | tree arg1; | |
5818 | ||
5819 | switch (DECL_FUNCTION_CODE (fndecl)) | |
5820 | { | |
5821 | CASE_FLT_FN (BUILT_IN_ACOS): | |
5822 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
5823 | CASE_FLT_FN (BUILT_IN_CABS): | |
5824 | CASE_FLT_FN (BUILT_IN_COSH): | |
5825 | CASE_FLT_FN (BUILT_IN_ERFC): | |
5826 | CASE_FLT_FN (BUILT_IN_EXP): | |
5827 | CASE_FLT_FN (BUILT_IN_EXP10): | |
5828 | CASE_FLT_FN (BUILT_IN_EXP2): | |
5829 | CASE_FLT_FN (BUILT_IN_FABS): | |
5830 | CASE_FLT_FN (BUILT_IN_FDIM): | |
5831 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
5832 | CASE_FLT_FN (BUILT_IN_POW10): | |
5833 | return true; | |
5834 | ||
5835 | CASE_FLT_FN (BUILT_IN_SQRT): | |
5836 | /* sqrt(-0.0) is -0.0, and sqrt is not defined over other | |
5837 | nonnegative inputs. */ | |
3d3dbadd | 5838 | if (!HONOR_SIGNED_ZEROS (val)) |
06bc3ec7 BS |
5839 | return true; |
5840 | ||
5841 | break; | |
5842 | ||
5843 | CASE_FLT_FN (BUILT_IN_POWI): | |
5844 | /* True if the second argument is an even integer. */ | |
5845 | arg1 = gimple_call_arg (def_stmt, 1); | |
5846 | ||
5847 | if (TREE_CODE (arg1) == INTEGER_CST | |
5848 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) | |
5849 | return true; | |
5850 | ||
5851 | break; | |
5852 | ||
5853 | CASE_FLT_FN (BUILT_IN_POW): | |
5854 | /* True if the second argument is an even integer-valued | |
5855 | real. */ | |
5856 | arg1 = gimple_call_arg (def_stmt, 1); | |
5857 | ||
5858 | if (TREE_CODE (arg1) == REAL_CST) | |
5859 | { | |
5860 | REAL_VALUE_TYPE c; | |
5861 | HOST_WIDE_INT n; | |
5862 | ||
5863 | c = TREE_REAL_CST (arg1); | |
5864 | n = real_to_integer (&c); | |
5865 | ||
5866 | if ((n & 1) == 0) | |
5867 | { | |
5868 | REAL_VALUE_TYPE cint; | |
807e902e | 5869 | real_from_integer (&cint, VOIDmode, n, SIGNED); |
06bc3ec7 BS |
5870 | if (real_identical (&c, &cint)) |
5871 | return true; | |
5872 | } | |
5873 | } | |
5874 | ||
5875 | break; | |
5876 | ||
5877 | default: | |
5878 | return false; | |
5879 | } | |
5880 | } | |
5881 | } | |
5882 | ||
5883 | return false; | |
5884 | } | |
b184c8f1 AM |
5885 | |
5886 | /* Given a pointer value OP0, return a simplified version of an | |
5887 | indirection through OP0, or NULL_TREE if no simplification is | |
5888 | possible. Note that the resulting type may be different from | |
5889 | the type pointed to in the sense that it is still compatible | |
5890 | from the langhooks point of view. */ | |
5891 | ||
5892 | tree | |
5893 | gimple_fold_indirect_ref (tree t) | |
5894 | { | |
5895 | tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype); | |
5896 | tree sub = t; | |
5897 | tree subtype; | |
5898 | ||
5899 | STRIP_NOPS (sub); | |
5900 | subtype = TREE_TYPE (sub); | |
5901 | if (!POINTER_TYPE_P (subtype)) | |
5902 | return NULL_TREE; | |
5903 | ||
5904 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5905 | { | |
5906 | tree op = TREE_OPERAND (sub, 0); | |
5907 | tree optype = TREE_TYPE (op); | |
5908 | /* *&p => p */ | |
5909 | if (useless_type_conversion_p (type, optype)) | |
5910 | return op; | |
5911 | ||
5912 | /* *(foo *)&fooarray => fooarray[0] */ | |
5913 | if (TREE_CODE (optype) == ARRAY_TYPE | |
5914 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST | |
5915 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
5916 | { | |
5917 | tree type_domain = TYPE_DOMAIN (optype); | |
5918 | tree min_val = size_zero_node; | |
5919 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5920 | min_val = TYPE_MIN_VALUE (type_domain); | |
5921 | if (TREE_CODE (min_val) == INTEGER_CST) | |
5922 | return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE); | |
5923 | } | |
5924 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
5925 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
5926 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
5927 | return fold_build1 (REALPART_EXPR, type, op); | |
5928 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
5929 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
5930 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
5931 | { | |
5932 | tree part_width = TYPE_SIZE (type); | |
5933 | tree index = bitsize_int (0); | |
5934 | return fold_build3 (BIT_FIELD_REF, type, op, part_width, index); | |
5935 | } | |
5936 | } | |
5937 | ||
5938 | /* *(p + CST) -> ... */ | |
5939 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
5940 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
5941 | { | |
5942 | tree addr = TREE_OPERAND (sub, 0); | |
5943 | tree off = TREE_OPERAND (sub, 1); | |
5944 | tree addrtype; | |
5945 | ||
5946 | STRIP_NOPS (addr); | |
5947 | addrtype = TREE_TYPE (addr); | |
5948 | ||
5949 | /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */ | |
5950 | if (TREE_CODE (addr) == ADDR_EXPR | |
5951 | && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE | |
5952 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))) | |
cc269bb6 | 5953 | && tree_fits_uhwi_p (off)) |
b184c8f1 | 5954 | { |
ae7e9ddd | 5955 | unsigned HOST_WIDE_INT offset = tree_to_uhwi (off); |
b184c8f1 AM |
5956 | tree part_width = TYPE_SIZE (type); |
5957 | unsigned HOST_WIDE_INT part_widthi | |
9439e9a1 | 5958 | = tree_to_shwi (part_width) / BITS_PER_UNIT; |
b184c8f1 AM |
5959 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; |
5960 | tree index = bitsize_int (indexi); | |
5961 | if (offset / part_widthi | |
e934916c | 5962 | < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))) |
b184c8f1 AM |
5963 | return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0), |
5964 | part_width, index); | |
5965 | } | |
5966 | ||
5967 | /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */ | |
5968 | if (TREE_CODE (addr) == ADDR_EXPR | |
5969 | && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE | |
5970 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))) | |
5971 | { | |
5972 | tree size = TYPE_SIZE_UNIT (type); | |
5973 | if (tree_int_cst_equal (size, off)) | |
5974 | return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0)); | |
5975 | } | |
5976 | ||
5977 | /* *(p + CST) -> MEM_REF <p, CST>. */ | |
5978 | if (TREE_CODE (addr) != ADDR_EXPR | |
5979 | || DECL_P (TREE_OPERAND (addr, 0))) | |
5980 | return fold_build2 (MEM_REF, type, | |
5981 | addr, | |
807e902e | 5982 | wide_int_to_tree (ptype, off)); |
b184c8f1 AM |
5983 | } |
5984 | ||
5985 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
5986 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
5987 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST | |
5988 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype)))) | |
5989 | { | |
5990 | tree type_domain; | |
5991 | tree min_val = size_zero_node; | |
5992 | tree osub = sub; | |
5993 | sub = gimple_fold_indirect_ref (sub); | |
5994 | if (! sub) | |
5995 | sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub); | |
5996 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5997 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5998 | min_val = TYPE_MIN_VALUE (type_domain); | |
5999 | if (TREE_CODE (min_val) == INTEGER_CST) | |
6000 | return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE); | |
6001 | } | |
6002 | ||
6003 | return NULL_TREE; | |
6004 | } | |
19e51b40 JJ |
6005 | |
6006 | /* Return true if CODE is an operation that when operating on signed | |
6007 | integer types involves undefined behavior on overflow and the | |
6008 | operation can be expressed with unsigned arithmetic. */ | |
6009 | ||
6010 | bool | |
6011 | arith_code_with_undefined_signed_overflow (tree_code code) | |
6012 | { | |
6013 | switch (code) | |
6014 | { | |
6015 | case PLUS_EXPR: | |
6016 | case MINUS_EXPR: | |
6017 | case MULT_EXPR: | |
6018 | case NEGATE_EXPR: | |
6019 | case POINTER_PLUS_EXPR: | |
6020 | return true; | |
6021 | default: | |
6022 | return false; | |
6023 | } | |
6024 | } | |
6025 | ||
6026 | /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic | |
6027 | operation that can be transformed to unsigned arithmetic by converting | |
6028 | its operand, carrying out the operation in the corresponding unsigned | |
6029 | type and converting the result back to the original type. | |
6030 | ||
6031 | Returns a sequence of statements that replace STMT and also contain | |
6032 | a modified form of STMT itself. */ | |
6033 | ||
6034 | gimple_seq | |
6035 | rewrite_to_defined_overflow (gimple stmt) | |
6036 | { | |
6037 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
6038 | { | |
6039 | fprintf (dump_file, "rewriting stmt with undefined signed " | |
6040 | "overflow "); | |
6041 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
6042 | } | |
6043 | ||
6044 | tree lhs = gimple_assign_lhs (stmt); | |
6045 | tree type = unsigned_type_for (TREE_TYPE (lhs)); | |
6046 | gimple_seq stmts = NULL; | |
6047 | for (unsigned i = 1; i < gimple_num_ops (stmt); ++i) | |
6048 | { | |
6049 | gimple_seq stmts2 = NULL; | |
6050 | gimple_set_op (stmt, i, | |
6051 | force_gimple_operand (fold_convert (type, | |
6052 | gimple_op (stmt, i)), | |
6053 | &stmts2, true, NULL_TREE)); | |
6054 | gimple_seq_add_seq (&stmts, stmts2); | |
6055 | } | |
6056 | gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt)); | |
6057 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) | |
6058 | gimple_assign_set_rhs_code (stmt, PLUS_EXPR); | |
6059 | gimple_seq_add_stmt (&stmts, stmt); | |
0d0e4a03 | 6060 | gimple cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt)); |
19e51b40 JJ |
6061 | gimple_seq_add_stmt (&stmts, cvt); |
6062 | ||
6063 | return stmts; | |
6064 | } | |
d4f5cd5e | 6065 | |
3d2cf79f | 6066 | |
c26de36d RB |
6067 | /* The valueization hook we use for the gimple_build API simplification. |
6068 | This makes us match fold_buildN behavior by only combining with | |
6069 | statements in the sequence(s) we are currently building. */ | |
6070 | ||
6071 | static tree | |
6072 | gimple_build_valueize (tree op) | |
6073 | { | |
6074 | if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL) | |
6075 | return op; | |
6076 | return NULL_TREE; | |
6077 | } | |
6078 | ||
3d2cf79f | 6079 | /* Build the expression CODE OP0 of type TYPE with location LOC, |
c26de36d | 6080 | simplifying it first if possible. Returns the built |
3d2cf79f RB |
6081 | expression value and appends statements possibly defining it |
6082 | to SEQ. */ | |
6083 | ||
6084 | tree | |
6085 | gimple_build (gimple_seq *seq, location_t loc, | |
c26de36d | 6086 | enum tree_code code, tree type, tree op0) |
3d2cf79f | 6087 | { |
c26de36d | 6088 | tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize); |
3d2cf79f RB |
6089 | if (!res) |
6090 | { | |
6091 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 6092 | res = make_ssa_name (type); |
3d2cf79f | 6093 | else |
b731b390 | 6094 | res = create_tmp_reg (type); |
3d2cf79f RB |
6095 | gimple stmt; |
6096 | if (code == REALPART_EXPR | |
6097 | || code == IMAGPART_EXPR | |
6098 | || code == VIEW_CONVERT_EXPR) | |
0d0e4a03 | 6099 | stmt = gimple_build_assign (res, code, build1 (code, type, op0)); |
3d2cf79f | 6100 | else |
0d0e4a03 | 6101 | stmt = gimple_build_assign (res, code, op0); |
3d2cf79f RB |
6102 | gimple_set_location (stmt, loc); |
6103 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6104 | } | |
6105 | return res; | |
6106 | } | |
6107 | ||
6108 | /* Build the expression OP0 CODE OP1 of type TYPE with location LOC, | |
c26de36d | 6109 | simplifying it first if possible. Returns the built |
3d2cf79f RB |
6110 | expression value and appends statements possibly defining it |
6111 | to SEQ. */ | |
6112 | ||
6113 | tree | |
6114 | gimple_build (gimple_seq *seq, location_t loc, | |
c26de36d | 6115 | enum tree_code code, tree type, tree op0, tree op1) |
3d2cf79f | 6116 | { |
c26de36d | 6117 | tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize); |
3d2cf79f RB |
6118 | if (!res) |
6119 | { | |
6120 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 6121 | res = make_ssa_name (type); |
3d2cf79f | 6122 | else |
b731b390 | 6123 | res = create_tmp_reg (type); |
0d0e4a03 | 6124 | gimple stmt = gimple_build_assign (res, code, op0, op1); |
3d2cf79f RB |
6125 | gimple_set_location (stmt, loc); |
6126 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6127 | } | |
6128 | return res; | |
6129 | } | |
6130 | ||
6131 | /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC, | |
c26de36d | 6132 | simplifying it first if possible. Returns the built |
3d2cf79f RB |
6133 | expression value and appends statements possibly defining it |
6134 | to SEQ. */ | |
6135 | ||
6136 | tree | |
6137 | gimple_build (gimple_seq *seq, location_t loc, | |
c26de36d | 6138 | enum tree_code code, tree type, tree op0, tree op1, tree op2) |
3d2cf79f RB |
6139 | { |
6140 | tree res = gimple_simplify (code, type, op0, op1, op2, | |
c26de36d | 6141 | seq, gimple_build_valueize); |
3d2cf79f RB |
6142 | if (!res) |
6143 | { | |
6144 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 6145 | res = make_ssa_name (type); |
3d2cf79f | 6146 | else |
b731b390 | 6147 | res = create_tmp_reg (type); |
3d2cf79f RB |
6148 | gimple stmt; |
6149 | if (code == BIT_FIELD_REF) | |
0d0e4a03 JJ |
6150 | stmt = gimple_build_assign (res, code, |
6151 | build3 (code, type, op0, op1, op2)); | |
3d2cf79f | 6152 | else |
0d0e4a03 | 6153 | stmt = gimple_build_assign (res, code, op0, op1, op2); |
3d2cf79f RB |
6154 | gimple_set_location (stmt, loc); |
6155 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6156 | } | |
6157 | return res; | |
6158 | } | |
6159 | ||
6160 | /* Build the call FN (ARG0) with a result of type TYPE | |
6161 | (or no result if TYPE is void) with location LOC, | |
c26de36d | 6162 | simplifying it first if possible. Returns the built |
3d2cf79f RB |
6163 | expression value (or NULL_TREE if TYPE is void) and appends |
6164 | statements possibly defining it to SEQ. */ | |
6165 | ||
6166 | tree | |
6167 | gimple_build (gimple_seq *seq, location_t loc, | |
c26de36d | 6168 | enum built_in_function fn, tree type, tree arg0) |
3d2cf79f | 6169 | { |
c26de36d | 6170 | tree res = gimple_simplify (fn, type, arg0, seq, gimple_build_valueize); |
3d2cf79f RB |
6171 | if (!res) |
6172 | { | |
6173 | tree decl = builtin_decl_implicit (fn); | |
6174 | gimple stmt = gimple_build_call (decl, 1, arg0); | |
6175 | if (!VOID_TYPE_P (type)) | |
6176 | { | |
6177 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 6178 | res = make_ssa_name (type); |
3d2cf79f | 6179 | else |
b731b390 | 6180 | res = create_tmp_reg (type); |
3d2cf79f RB |
6181 | gimple_call_set_lhs (stmt, res); |
6182 | } | |
6183 | gimple_set_location (stmt, loc); | |
6184 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6185 | } | |
6186 | return res; | |
6187 | } | |
6188 | ||
6189 | /* Build the call FN (ARG0, ARG1) with a result of type TYPE | |
6190 | (or no result if TYPE is void) with location LOC, | |
c26de36d | 6191 | simplifying it first if possible. Returns the built |
3d2cf79f RB |
6192 | expression value (or NULL_TREE if TYPE is void) and appends |
6193 | statements possibly defining it to SEQ. */ | |
6194 | ||
6195 | tree | |
6196 | gimple_build (gimple_seq *seq, location_t loc, | |
c26de36d | 6197 | enum built_in_function fn, tree type, tree arg0, tree arg1) |
3d2cf79f | 6198 | { |
c26de36d | 6199 | tree res = gimple_simplify (fn, type, arg0, arg1, seq, gimple_build_valueize); |
3d2cf79f RB |
6200 | if (!res) |
6201 | { | |
6202 | tree decl = builtin_decl_implicit (fn); | |
6203 | gimple stmt = gimple_build_call (decl, 2, arg0, arg1); | |
6204 | if (!VOID_TYPE_P (type)) | |
6205 | { | |
6206 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 6207 | res = make_ssa_name (type); |
3d2cf79f | 6208 | else |
b731b390 | 6209 | res = create_tmp_reg (type); |
3d2cf79f RB |
6210 | gimple_call_set_lhs (stmt, res); |
6211 | } | |
6212 | gimple_set_location (stmt, loc); | |
6213 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6214 | } | |
6215 | return res; | |
6216 | } | |
6217 | ||
6218 | /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE | |
6219 | (or no result if TYPE is void) with location LOC, | |
c26de36d | 6220 | simplifying it first if possible. Returns the built |
3d2cf79f RB |
6221 | expression value (or NULL_TREE if TYPE is void) and appends |
6222 | statements possibly defining it to SEQ. */ | |
6223 | ||
6224 | tree | |
6225 | gimple_build (gimple_seq *seq, location_t loc, | |
6226 | enum built_in_function fn, tree type, | |
c26de36d | 6227 | tree arg0, tree arg1, tree arg2) |
3d2cf79f | 6228 | { |
c26de36d RB |
6229 | tree res = gimple_simplify (fn, type, arg0, arg1, arg2, |
6230 | seq, gimple_build_valueize); | |
3d2cf79f RB |
6231 | if (!res) |
6232 | { | |
6233 | tree decl = builtin_decl_implicit (fn); | |
6234 | gimple stmt = gimple_build_call (decl, 3, arg0, arg1, arg2); | |
6235 | if (!VOID_TYPE_P (type)) | |
6236 | { | |
6237 | if (gimple_in_ssa_p (cfun)) | |
b731b390 | 6238 | res = make_ssa_name (type); |
3d2cf79f | 6239 | else |
b731b390 | 6240 | res = create_tmp_reg (type); |
3d2cf79f RB |
6241 | gimple_call_set_lhs (stmt, res); |
6242 | } | |
6243 | gimple_set_location (stmt, loc); | |
6244 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6245 | } | |
6246 | return res; | |
6247 | } | |
6248 | ||
6249 | /* Build the conversion (TYPE) OP with a result of type TYPE | |
6250 | with location LOC if such conversion is neccesary in GIMPLE, | |
6251 | simplifying it first. | |
6252 | Returns the built expression value and appends | |
6253 | statements possibly defining it to SEQ. */ | |
d4f5cd5e RB |
6254 | |
6255 | tree | |
6256 | gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op) | |
6257 | { | |
6258 | if (useless_type_conversion_p (type, TREE_TYPE (op))) | |
6259 | return op; | |
3d2cf79f | 6260 | return gimple_build (seq, loc, NOP_EXPR, type, op); |
d4f5cd5e | 6261 | } |