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