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