* config/rx/rx.c (ADD_RX_BUILTIN0): New macro, used for builtins
[official-gcc.git] / gcc / tree-ssa-ccp.c
blobd30bd8b2493fe93f0af315a0ffb809c630c15400
1 /* Conditional constant propagation pass for the GNU compiler.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
4 Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
11 later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Conditional constant propagation (CCP) is based on the SSA
23 propagation engine (tree-ssa-propagate.c). Constant assignments of
24 the form VAR = CST are propagated from the assignments into uses of
25 VAR, which in turn may generate new constants. The simulation uses
26 a four level lattice to keep track of constant values associated
27 with SSA names. Given an SSA name V_i, it may take one of the
28 following values:
30 UNINITIALIZED -> the initial state of the value. This value
31 is replaced with a correct initial value
32 the first time the value is used, so the
33 rest of the pass does not need to care about
34 it. Using this value simplifies initialization
35 of the pass, and prevents us from needlessly
36 scanning statements that are never reached.
38 UNDEFINED -> V_i is a local variable whose definition
39 has not been processed yet. Therefore we
40 don't yet know if its value is a constant
41 or not.
43 CONSTANT -> V_i has been found to hold a constant
44 value C.
46 VARYING -> V_i cannot take a constant value, or if it
47 does, it is not possible to determine it
48 at compile time.
50 The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
52 1- In ccp_visit_stmt, we are interested in assignments whose RHS
53 evaluates into a constant and conditional jumps whose predicate
54 evaluates into a boolean true or false. When an assignment of
55 the form V_i = CONST is found, V_i's lattice value is set to
56 CONSTANT and CONST is associated with it. This causes the
57 propagation engine to add all the SSA edges coming out the
58 assignment into the worklists, so that statements that use V_i
59 can be visited.
61 If the statement is a conditional with a constant predicate, we
62 mark the outgoing edges as executable or not executable
63 depending on the predicate's value. This is then used when
64 visiting PHI nodes to know when a PHI argument can be ignored.
67 2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
68 same constant C, then the LHS of the PHI is set to C. This
69 evaluation is known as the "meet operation". Since one of the
70 goals of this evaluation is to optimistically return constant
71 values as often as possible, it uses two main short cuts:
73 - If an argument is flowing in through a non-executable edge, it
74 is ignored. This is useful in cases like this:
76 if (PRED)
77 a_9 = 3;
78 else
79 a_10 = 100;
80 a_11 = PHI (a_9, a_10)
82 If PRED is known to always evaluate to false, then we can
83 assume that a_11 will always take its value from a_10, meaning
84 that instead of consider it VARYING (a_9 and a_10 have
85 different values), we can consider it CONSTANT 100.
87 - If an argument has an UNDEFINED value, then it does not affect
88 the outcome of the meet operation. If a variable V_i has an
89 UNDEFINED value, it means that either its defining statement
90 hasn't been visited yet or V_i has no defining statement, in
91 which case the original symbol 'V' is being used
92 uninitialized. Since 'V' is a local variable, the compiler
93 may assume any initial value for it.
96 After propagation, every variable V_i that ends up with a lattice
97 value of CONSTANT will have the associated constant value in the
98 array CONST_VAL[i].VALUE. That is fed into substitute_and_fold for
99 final substitution and folding.
101 References:
103 Constant propagation with conditional branches,
104 Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
106 Building an Optimizing Compiler,
107 Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
109 Advanced Compiler Design and Implementation,
110 Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 */
112 #include "config.h"
113 #include "system.h"
114 #include "coretypes.h"
115 #include "tm.h"
116 #include "tree.h"
117 #include "flags.h"
118 #include "tm_p.h"
119 #include "basic-block.h"
120 #include "function.h"
121 #include "gimple-pretty-print.h"
122 #include "gimple.h"
123 #include "gimple-ssa.h"
124 #include "tree-cfg.h"
125 #include "tree-phinodes.h"
126 #include "ssa-iterators.h"
127 #include "tree-ssanames.h"
128 #include "tree-pass.h"
129 #include "tree-ssa-propagate.h"
130 #include "value-prof.h"
131 #include "langhooks.h"
132 #include "target.h"
133 #include "diagnostic-core.h"
134 #include "dbgcnt.h"
135 #include "params.h"
136 #include "hash-table.h"
139 /* Possible lattice values. */
140 typedef enum
142 UNINITIALIZED,
143 UNDEFINED,
144 CONSTANT,
145 VARYING
146 } ccp_lattice_t;
148 struct prop_value_d {
149 /* Lattice value. */
150 ccp_lattice_t lattice_val;
152 /* Propagated value. */
153 tree value;
155 /* Mask that applies to the propagated value during CCP. For
156 X with a CONSTANT lattice value X & ~mask == value & ~mask. */
157 double_int mask;
160 typedef struct prop_value_d prop_value_t;
162 /* Array of propagated constant values. After propagation,
163 CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I). If
164 the constant is held in an SSA name representing a memory store
165 (i.e., a VDEF), CONST_VAL[I].MEM_REF will contain the actual
166 memory reference used to store (i.e., the LHS of the assignment
167 doing the store). */
168 static prop_value_t *const_val;
169 static unsigned n_const_val;
171 static void canonicalize_float_value (prop_value_t *);
172 static bool ccp_fold_stmt (gimple_stmt_iterator *);
174 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX. */
176 static void
177 dump_lattice_value (FILE *outf, const char *prefix, prop_value_t val)
179 switch (val.lattice_val)
181 case UNINITIALIZED:
182 fprintf (outf, "%sUNINITIALIZED", prefix);
183 break;
184 case UNDEFINED:
185 fprintf (outf, "%sUNDEFINED", prefix);
186 break;
187 case VARYING:
188 fprintf (outf, "%sVARYING", prefix);
189 break;
190 case CONSTANT:
191 if (TREE_CODE (val.value) != INTEGER_CST
192 || val.mask.is_zero ())
194 fprintf (outf, "%sCONSTANT ", prefix);
195 print_generic_expr (outf, val.value, dump_flags);
197 else
199 double_int cval = tree_to_double_int (val.value).and_not (val.mask);
200 fprintf (outf, "%sCONSTANT " HOST_WIDE_INT_PRINT_DOUBLE_HEX,
201 prefix, cval.high, cval.low);
202 fprintf (outf, " (" HOST_WIDE_INT_PRINT_DOUBLE_HEX ")",
203 val.mask.high, val.mask.low);
205 break;
206 default:
207 gcc_unreachable ();
212 /* Print lattice value VAL to stderr. */
214 void debug_lattice_value (prop_value_t val);
216 DEBUG_FUNCTION void
217 debug_lattice_value (prop_value_t val)
219 dump_lattice_value (stderr, "", val);
220 fprintf (stderr, "\n");
224 /* Compute a default value for variable VAR and store it in the
225 CONST_VAL array. The following rules are used to get default
226 values:
228 1- Global and static variables that are declared constant are
229 considered CONSTANT.
231 2- Any other value is considered UNDEFINED. This is useful when
232 considering PHI nodes. PHI arguments that are undefined do not
233 change the constant value of the PHI node, which allows for more
234 constants to be propagated.
236 3- Variables defined by statements other than assignments and PHI
237 nodes are considered VARYING.
239 4- Initial values of variables that are not GIMPLE registers are
240 considered VARYING. */
242 static prop_value_t
243 get_default_value (tree var)
245 prop_value_t val = { UNINITIALIZED, NULL_TREE, { 0, 0 } };
246 gimple stmt;
248 stmt = SSA_NAME_DEF_STMT (var);
250 if (gimple_nop_p (stmt))
252 /* Variables defined by an empty statement are those used
253 before being initialized. If VAR is a local variable, we
254 can assume initially that it is UNDEFINED, otherwise we must
255 consider it VARYING. */
256 if (!virtual_operand_p (var)
257 && TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
258 val.lattice_val = UNDEFINED;
259 else
261 val.lattice_val = VARYING;
262 val.mask = double_int_minus_one;
265 else if (is_gimple_assign (stmt))
267 tree cst;
268 if (gimple_assign_single_p (stmt)
269 && DECL_P (gimple_assign_rhs1 (stmt))
270 && (cst = get_symbol_constant_value (gimple_assign_rhs1 (stmt))))
272 val.lattice_val = CONSTANT;
273 val.value = cst;
275 else
277 /* Any other variable defined by an assignment is considered
278 UNDEFINED. */
279 val.lattice_val = UNDEFINED;
282 else if ((is_gimple_call (stmt)
283 && gimple_call_lhs (stmt) != NULL_TREE)
284 || gimple_code (stmt) == GIMPLE_PHI)
286 /* A variable defined by a call or a PHI node is considered
287 UNDEFINED. */
288 val.lattice_val = UNDEFINED;
290 else
292 /* Otherwise, VAR will never take on a constant value. */
293 val.lattice_val = VARYING;
294 val.mask = double_int_minus_one;
297 return val;
301 /* Get the constant value associated with variable VAR. */
303 static inline prop_value_t *
304 get_value (tree var)
306 prop_value_t *val;
308 if (const_val == NULL
309 || SSA_NAME_VERSION (var) >= n_const_val)
310 return NULL;
312 val = &const_val[SSA_NAME_VERSION (var)];
313 if (val->lattice_val == UNINITIALIZED)
314 *val = get_default_value (var);
316 canonicalize_float_value (val);
318 return val;
321 /* Return the constant tree value associated with VAR. */
323 static inline tree
324 get_constant_value (tree var)
326 prop_value_t *val;
327 if (TREE_CODE (var) != SSA_NAME)
329 if (is_gimple_min_invariant (var))
330 return var;
331 return NULL_TREE;
333 val = get_value (var);
334 if (val
335 && val->lattice_val == CONSTANT
336 && (TREE_CODE (val->value) != INTEGER_CST
337 || val->mask.is_zero ()))
338 return val->value;
339 return NULL_TREE;
342 /* Sets the value associated with VAR to VARYING. */
344 static inline void
345 set_value_varying (tree var)
347 prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
349 val->lattice_val = VARYING;
350 val->value = NULL_TREE;
351 val->mask = double_int_minus_one;
354 /* For float types, modify the value of VAL to make ccp work correctly
355 for non-standard values (-0, NaN):
357 If HONOR_SIGNED_ZEROS is false, and VAL = -0, we canonicalize it to 0.
358 If HONOR_NANS is false, and VAL is NaN, we canonicalize it to UNDEFINED.
359 This is to fix the following problem (see PR 29921): Suppose we have
361 x = 0.0 * y
363 and we set value of y to NaN. This causes value of x to be set to NaN.
364 When we later determine that y is in fact VARYING, fold uses the fact
365 that HONOR_NANS is false, and we try to change the value of x to 0,
366 causing an ICE. With HONOR_NANS being false, the real appearance of
367 NaN would cause undefined behavior, though, so claiming that y (and x)
368 are UNDEFINED initially is correct. */
370 static void
371 canonicalize_float_value (prop_value_t *val)
373 enum machine_mode mode;
374 tree type;
375 REAL_VALUE_TYPE d;
377 if (val->lattice_val != CONSTANT
378 || TREE_CODE (val->value) != REAL_CST)
379 return;
381 d = TREE_REAL_CST (val->value);
382 type = TREE_TYPE (val->value);
383 mode = TYPE_MODE (type);
385 if (!HONOR_SIGNED_ZEROS (mode)
386 && REAL_VALUE_MINUS_ZERO (d))
388 val->value = build_real (type, dconst0);
389 return;
392 if (!HONOR_NANS (mode)
393 && REAL_VALUE_ISNAN (d))
395 val->lattice_val = UNDEFINED;
396 val->value = NULL;
397 return;
401 /* Return whether the lattice transition is valid. */
403 static bool
404 valid_lattice_transition (prop_value_t old_val, prop_value_t new_val)
406 /* Lattice transitions must always be monotonically increasing in
407 value. */
408 if (old_val.lattice_val < new_val.lattice_val)
409 return true;
411 if (old_val.lattice_val != new_val.lattice_val)
412 return false;
414 if (!old_val.value && !new_val.value)
415 return true;
417 /* Now both lattice values are CONSTANT. */
419 /* Allow transitioning from PHI <&x, not executable> == &x
420 to PHI <&x, &y> == common alignment. */
421 if (TREE_CODE (old_val.value) != INTEGER_CST
422 && TREE_CODE (new_val.value) == INTEGER_CST)
423 return true;
425 /* Bit-lattices have to agree in the still valid bits. */
426 if (TREE_CODE (old_val.value) == INTEGER_CST
427 && TREE_CODE (new_val.value) == INTEGER_CST)
428 return tree_to_double_int (old_val.value).and_not (new_val.mask)
429 == tree_to_double_int (new_val.value).and_not (new_val.mask);
431 /* Otherwise constant values have to agree. */
432 return operand_equal_p (old_val.value, new_val.value, 0);
435 /* Set the value for variable VAR to NEW_VAL. Return true if the new
436 value is different from VAR's previous value. */
438 static bool
439 set_lattice_value (tree var, prop_value_t new_val)
441 /* We can deal with old UNINITIALIZED values just fine here. */
442 prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
444 canonicalize_float_value (&new_val);
446 /* We have to be careful to not go up the bitwise lattice
447 represented by the mask.
448 ??? This doesn't seem to be the best place to enforce this. */
449 if (new_val.lattice_val == CONSTANT
450 && old_val->lattice_val == CONSTANT
451 && TREE_CODE (new_val.value) == INTEGER_CST
452 && TREE_CODE (old_val->value) == INTEGER_CST)
454 double_int diff;
455 diff = tree_to_double_int (new_val.value)
456 ^ tree_to_double_int (old_val->value);
457 new_val.mask = new_val.mask | old_val->mask | diff;
460 gcc_assert (valid_lattice_transition (*old_val, new_val));
462 /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
463 caller that this was a non-transition. */
464 if (old_val->lattice_val != new_val.lattice_val
465 || (new_val.lattice_val == CONSTANT
466 && TREE_CODE (new_val.value) == INTEGER_CST
467 && (TREE_CODE (old_val->value) != INTEGER_CST
468 || new_val.mask != old_val->mask)))
470 /* ??? We would like to delay creation of INTEGER_CSTs from
471 partially constants here. */
473 if (dump_file && (dump_flags & TDF_DETAILS))
475 dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
476 fprintf (dump_file, ". Adding SSA edges to worklist.\n");
479 *old_val = new_val;
481 gcc_assert (new_val.lattice_val != UNINITIALIZED);
482 return true;
485 return false;
488 static prop_value_t get_value_for_expr (tree, bool);
489 static prop_value_t bit_value_binop (enum tree_code, tree, tree, tree);
490 static void bit_value_binop_1 (enum tree_code, tree, double_int *, double_int *,
491 tree, double_int, double_int,
492 tree, double_int, double_int);
494 /* Return a double_int that can be used for bitwise simplifications
495 from VAL. */
497 static double_int
498 value_to_double_int (prop_value_t val)
500 if (val.value
501 && TREE_CODE (val.value) == INTEGER_CST)
502 return tree_to_double_int (val.value);
503 else
504 return double_int_zero;
507 /* Return the value for the address expression EXPR based on alignment
508 information. */
510 static prop_value_t
511 get_value_from_alignment (tree expr)
513 tree type = TREE_TYPE (expr);
514 prop_value_t val;
515 unsigned HOST_WIDE_INT bitpos;
516 unsigned int align;
518 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
520 get_pointer_alignment_1 (expr, &align, &bitpos);
521 val.mask = (POINTER_TYPE_P (type) || TYPE_UNSIGNED (type)
522 ? double_int::mask (TYPE_PRECISION (type))
523 : double_int_minus_one)
524 .and_not (double_int::from_uhwi (align / BITS_PER_UNIT - 1));
525 val.lattice_val = val.mask.is_minus_one () ? VARYING : CONSTANT;
526 if (val.lattice_val == CONSTANT)
527 val.value
528 = double_int_to_tree (type,
529 double_int::from_uhwi (bitpos / BITS_PER_UNIT));
530 else
531 val.value = NULL_TREE;
533 return val;
536 /* Return the value for the tree operand EXPR. If FOR_BITS_P is true
537 return constant bits extracted from alignment information for
538 invariant addresses. */
540 static prop_value_t
541 get_value_for_expr (tree expr, bool for_bits_p)
543 prop_value_t val;
545 if (TREE_CODE (expr) == SSA_NAME)
547 val = *get_value (expr);
548 if (for_bits_p
549 && val.lattice_val == CONSTANT
550 && TREE_CODE (val.value) == ADDR_EXPR)
551 val = get_value_from_alignment (val.value);
553 else if (is_gimple_min_invariant (expr)
554 && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
556 val.lattice_val = CONSTANT;
557 val.value = expr;
558 val.mask = double_int_zero;
559 canonicalize_float_value (&val);
561 else if (TREE_CODE (expr) == ADDR_EXPR)
562 val = get_value_from_alignment (expr);
563 else
565 val.lattice_val = VARYING;
566 val.mask = double_int_minus_one;
567 val.value = NULL_TREE;
569 return val;
572 /* Return the likely CCP lattice value for STMT.
574 If STMT has no operands, then return CONSTANT.
576 Else if undefinedness of operands of STMT cause its value to be
577 undefined, then return UNDEFINED.
579 Else if any operands of STMT are constants, then return CONSTANT.
581 Else return VARYING. */
583 static ccp_lattice_t
584 likely_value (gimple stmt)
586 bool has_constant_operand, has_undefined_operand, all_undefined_operands;
587 tree use;
588 ssa_op_iter iter;
589 unsigned i;
591 enum gimple_code code = gimple_code (stmt);
593 /* This function appears to be called only for assignments, calls,
594 conditionals, and switches, due to the logic in visit_stmt. */
595 gcc_assert (code == GIMPLE_ASSIGN
596 || code == GIMPLE_CALL
597 || code == GIMPLE_COND
598 || code == GIMPLE_SWITCH);
600 /* If the statement has volatile operands, it won't fold to a
601 constant value. */
602 if (gimple_has_volatile_ops (stmt))
603 return VARYING;
605 /* Arrive here for more complex cases. */
606 has_constant_operand = false;
607 has_undefined_operand = false;
608 all_undefined_operands = true;
609 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
611 prop_value_t *val = get_value (use);
613 if (val->lattice_val == UNDEFINED)
614 has_undefined_operand = true;
615 else
616 all_undefined_operands = false;
618 if (val->lattice_val == CONSTANT)
619 has_constant_operand = true;
622 /* There may be constants in regular rhs operands. For calls we
623 have to ignore lhs, fndecl and static chain, otherwise only
624 the lhs. */
625 for (i = (is_gimple_call (stmt) ? 2 : 0) + gimple_has_lhs (stmt);
626 i < gimple_num_ops (stmt); ++i)
628 tree op = gimple_op (stmt, i);
629 if (!op || TREE_CODE (op) == SSA_NAME)
630 continue;
631 if (is_gimple_min_invariant (op))
632 has_constant_operand = true;
635 if (has_constant_operand)
636 all_undefined_operands = false;
638 if (has_undefined_operand
639 && code == GIMPLE_CALL
640 && gimple_call_internal_p (stmt))
641 switch (gimple_call_internal_fn (stmt))
643 /* These 3 builtins use the first argument just as a magic
644 way how to find out a decl uid. */
645 case IFN_GOMP_SIMD_LANE:
646 case IFN_GOMP_SIMD_VF:
647 case IFN_GOMP_SIMD_LAST_LANE:
648 has_undefined_operand = false;
649 break;
650 default:
651 break;
654 /* If the operation combines operands like COMPLEX_EXPR make sure to
655 not mark the result UNDEFINED if only one part of the result is
656 undefined. */
657 if (has_undefined_operand && all_undefined_operands)
658 return UNDEFINED;
659 else if (code == GIMPLE_ASSIGN && has_undefined_operand)
661 switch (gimple_assign_rhs_code (stmt))
663 /* Unary operators are handled with all_undefined_operands. */
664 case PLUS_EXPR:
665 case MINUS_EXPR:
666 case POINTER_PLUS_EXPR:
667 /* Not MIN_EXPR, MAX_EXPR. One VARYING operand may be selected.
668 Not bitwise operators, one VARYING operand may specify the
669 result completely. Not logical operators for the same reason.
670 Not COMPLEX_EXPR as one VARYING operand makes the result partly
671 not UNDEFINED. Not *DIV_EXPR, comparisons and shifts because
672 the undefined operand may be promoted. */
673 return UNDEFINED;
675 case ADDR_EXPR:
676 /* If any part of an address is UNDEFINED, like the index
677 of an ARRAY_EXPR, then treat the result as UNDEFINED. */
678 return UNDEFINED;
680 default:
684 /* If there was an UNDEFINED operand but the result may be not UNDEFINED
685 fall back to CONSTANT. During iteration UNDEFINED may still drop
686 to CONSTANT. */
687 if (has_undefined_operand)
688 return CONSTANT;
690 /* We do not consider virtual operands here -- load from read-only
691 memory may have only VARYING virtual operands, but still be
692 constant. */
693 if (has_constant_operand
694 || gimple_references_memory_p (stmt))
695 return CONSTANT;
697 return VARYING;
700 /* Returns true if STMT cannot be constant. */
702 static bool
703 surely_varying_stmt_p (gimple stmt)
705 /* If the statement has operands that we cannot handle, it cannot be
706 constant. */
707 if (gimple_has_volatile_ops (stmt))
708 return true;
710 /* If it is a call and does not return a value or is not a
711 builtin and not an indirect call, it is varying. */
712 if (is_gimple_call (stmt))
714 tree fndecl;
715 if (!gimple_call_lhs (stmt)
716 || ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
717 && !DECL_BUILT_IN (fndecl)))
718 return true;
721 /* Any other store operation is not interesting. */
722 else if (gimple_vdef (stmt))
723 return true;
725 /* Anything other than assignments and conditional jumps are not
726 interesting for CCP. */
727 if (gimple_code (stmt) != GIMPLE_ASSIGN
728 && gimple_code (stmt) != GIMPLE_COND
729 && gimple_code (stmt) != GIMPLE_SWITCH
730 && gimple_code (stmt) != GIMPLE_CALL)
731 return true;
733 return false;
736 /* Initialize local data structures for CCP. */
738 static void
739 ccp_initialize (void)
741 basic_block bb;
743 n_const_val = num_ssa_names;
744 const_val = XCNEWVEC (prop_value_t, n_const_val);
746 /* Initialize simulation flags for PHI nodes and statements. */
747 FOR_EACH_BB (bb)
749 gimple_stmt_iterator i;
751 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
753 gimple stmt = gsi_stmt (i);
754 bool is_varying;
756 /* If the statement is a control insn, then we do not
757 want to avoid simulating the statement once. Failure
758 to do so means that those edges will never get added. */
759 if (stmt_ends_bb_p (stmt))
760 is_varying = false;
761 else
762 is_varying = surely_varying_stmt_p (stmt);
764 if (is_varying)
766 tree def;
767 ssa_op_iter iter;
769 /* If the statement will not produce a constant, mark
770 all its outputs VARYING. */
771 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
772 set_value_varying (def);
774 prop_set_simulate_again (stmt, !is_varying);
778 /* Now process PHI nodes. We never clear the simulate_again flag on
779 phi nodes, since we do not know which edges are executable yet,
780 except for phi nodes for virtual operands when we do not do store ccp. */
781 FOR_EACH_BB (bb)
783 gimple_stmt_iterator i;
785 for (i = gsi_start_phis (bb); !gsi_end_p (i); gsi_next (&i))
787 gimple phi = gsi_stmt (i);
789 if (virtual_operand_p (gimple_phi_result (phi)))
790 prop_set_simulate_again (phi, false);
791 else
792 prop_set_simulate_again (phi, true);
797 /* Debug count support. Reset the values of ssa names
798 VARYING when the total number ssa names analyzed is
799 beyond the debug count specified. */
801 static void
802 do_dbg_cnt (void)
804 unsigned i;
805 for (i = 0; i < num_ssa_names; i++)
807 if (!dbg_cnt (ccp))
809 const_val[i].lattice_val = VARYING;
810 const_val[i].mask = double_int_minus_one;
811 const_val[i].value = NULL_TREE;
817 /* Do final substitution of propagated values, cleanup the flowgraph and
818 free allocated storage.
820 Return TRUE when something was optimized. */
822 static bool
823 ccp_finalize (void)
825 bool something_changed;
826 unsigned i;
828 do_dbg_cnt ();
830 /* Derive alignment and misalignment information from partially
831 constant pointers in the lattice. */
832 for (i = 1; i < num_ssa_names; ++i)
834 tree name = ssa_name (i);
835 prop_value_t *val;
836 unsigned int tem, align;
838 if (!name
839 || !POINTER_TYPE_P (TREE_TYPE (name)))
840 continue;
842 val = get_value (name);
843 if (val->lattice_val != CONSTANT
844 || TREE_CODE (val->value) != INTEGER_CST)
845 continue;
847 /* Trailing constant bits specify the alignment, trailing value
848 bits the misalignment. */
849 tem = val->mask.low;
850 align = (tem & -tem);
851 if (align > 1)
852 set_ptr_info_alignment (get_ptr_info (name), align,
853 TREE_INT_CST_LOW (val->value) & (align - 1));
856 /* Perform substitutions based on the known constant values. */
857 something_changed = substitute_and_fold (get_constant_value,
858 ccp_fold_stmt, true);
860 free (const_val);
861 const_val = NULL;
862 return something_changed;;
866 /* Compute the meet operator between *VAL1 and *VAL2. Store the result
867 in VAL1.
869 any M UNDEFINED = any
870 any M VARYING = VARYING
871 Ci M Cj = Ci if (i == j)
872 Ci M Cj = VARYING if (i != j)
875 static void
876 ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2)
878 if (val1->lattice_val == UNDEFINED)
880 /* UNDEFINED M any = any */
881 *val1 = *val2;
883 else if (val2->lattice_val == UNDEFINED)
885 /* any M UNDEFINED = any
886 Nothing to do. VAL1 already contains the value we want. */
889 else if (val1->lattice_val == VARYING
890 || val2->lattice_val == VARYING)
892 /* any M VARYING = VARYING. */
893 val1->lattice_val = VARYING;
894 val1->mask = double_int_minus_one;
895 val1->value = NULL_TREE;
897 else if (val1->lattice_val == CONSTANT
898 && val2->lattice_val == CONSTANT
899 && TREE_CODE (val1->value) == INTEGER_CST
900 && TREE_CODE (val2->value) == INTEGER_CST)
902 /* Ci M Cj = Ci if (i == j)
903 Ci M Cj = VARYING if (i != j)
905 For INTEGER_CSTs mask unequal bits. If no equal bits remain,
906 drop to varying. */
907 val1->mask = val1->mask | val2->mask
908 | (tree_to_double_int (val1->value)
909 ^ tree_to_double_int (val2->value));
910 if (val1->mask.is_minus_one ())
912 val1->lattice_val = VARYING;
913 val1->value = NULL_TREE;
916 else if (val1->lattice_val == CONSTANT
917 && val2->lattice_val == CONSTANT
918 && simple_cst_equal (val1->value, val2->value) == 1)
920 /* Ci M Cj = Ci if (i == j)
921 Ci M Cj = VARYING if (i != j)
923 VAL1 already contains the value we want for equivalent values. */
925 else if (val1->lattice_val == CONSTANT
926 && val2->lattice_val == CONSTANT
927 && (TREE_CODE (val1->value) == ADDR_EXPR
928 || TREE_CODE (val2->value) == ADDR_EXPR))
930 /* When not equal addresses are involved try meeting for
931 alignment. */
932 prop_value_t tem = *val2;
933 if (TREE_CODE (val1->value) == ADDR_EXPR)
934 *val1 = get_value_for_expr (val1->value, true);
935 if (TREE_CODE (val2->value) == ADDR_EXPR)
936 tem = get_value_for_expr (val2->value, true);
937 ccp_lattice_meet (val1, &tem);
939 else
941 /* Any other combination is VARYING. */
942 val1->lattice_val = VARYING;
943 val1->mask = double_int_minus_one;
944 val1->value = NULL_TREE;
949 /* Loop through the PHI_NODE's parameters for BLOCK and compare their
950 lattice values to determine PHI_NODE's lattice value. The value of a
951 PHI node is determined calling ccp_lattice_meet with all the arguments
952 of the PHI node that are incoming via executable edges. */
954 static enum ssa_prop_result
955 ccp_visit_phi_node (gimple phi)
957 unsigned i;
958 prop_value_t *old_val, new_val;
960 if (dump_file && (dump_flags & TDF_DETAILS))
962 fprintf (dump_file, "\nVisiting PHI node: ");
963 print_gimple_stmt (dump_file, phi, 0, dump_flags);
966 old_val = get_value (gimple_phi_result (phi));
967 switch (old_val->lattice_val)
969 case VARYING:
970 return SSA_PROP_VARYING;
972 case CONSTANT:
973 new_val = *old_val;
974 break;
976 case UNDEFINED:
977 new_val.lattice_val = UNDEFINED;
978 new_val.value = NULL_TREE;
979 break;
981 default:
982 gcc_unreachable ();
985 for (i = 0; i < gimple_phi_num_args (phi); i++)
987 /* Compute the meet operator over all the PHI arguments flowing
988 through executable edges. */
989 edge e = gimple_phi_arg_edge (phi, i);
991 if (dump_file && (dump_flags & TDF_DETAILS))
993 fprintf (dump_file,
994 "\n Argument #%d (%d -> %d %sexecutable)\n",
995 i, e->src->index, e->dest->index,
996 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
999 /* If the incoming edge is executable, Compute the meet operator for
1000 the existing value of the PHI node and the current PHI argument. */
1001 if (e->flags & EDGE_EXECUTABLE)
1003 tree arg = gimple_phi_arg (phi, i)->def;
1004 prop_value_t arg_val = get_value_for_expr (arg, false);
1006 ccp_lattice_meet (&new_val, &arg_val);
1008 if (dump_file && (dump_flags & TDF_DETAILS))
1010 fprintf (dump_file, "\t");
1011 print_generic_expr (dump_file, arg, dump_flags);
1012 dump_lattice_value (dump_file, "\tValue: ", arg_val);
1013 fprintf (dump_file, "\n");
1016 if (new_val.lattice_val == VARYING)
1017 break;
1021 if (dump_file && (dump_flags & TDF_DETAILS))
1023 dump_lattice_value (dump_file, "\n PHI node value: ", new_val);
1024 fprintf (dump_file, "\n\n");
1027 /* Make the transition to the new value. */
1028 if (set_lattice_value (gimple_phi_result (phi), new_val))
1030 if (new_val.lattice_val == VARYING)
1031 return SSA_PROP_VARYING;
1032 else
1033 return SSA_PROP_INTERESTING;
1035 else
1036 return SSA_PROP_NOT_INTERESTING;
1039 /* Return the constant value for OP or OP otherwise. */
1041 static tree
1042 valueize_op (tree op)
1044 if (TREE_CODE (op) == SSA_NAME)
1046 tree tem = get_constant_value (op);
1047 if (tem)
1048 return tem;
1050 return op;
1053 /* CCP specific front-end to the non-destructive constant folding
1054 routines.
1056 Attempt to simplify the RHS of STMT knowing that one or more
1057 operands are constants.
1059 If simplification is possible, return the simplified RHS,
1060 otherwise return the original RHS or NULL_TREE. */
1062 static tree
1063 ccp_fold (gimple stmt)
1065 location_t loc = gimple_location (stmt);
1066 switch (gimple_code (stmt))
1068 case GIMPLE_COND:
1070 /* Handle comparison operators that can appear in GIMPLE form. */
1071 tree op0 = valueize_op (gimple_cond_lhs (stmt));
1072 tree op1 = valueize_op (gimple_cond_rhs (stmt));
1073 enum tree_code code = gimple_cond_code (stmt);
1074 return fold_binary_loc (loc, code, boolean_type_node, op0, op1);
1077 case GIMPLE_SWITCH:
1079 /* Return the constant switch index. */
1080 return valueize_op (gimple_switch_index (stmt));
1083 case GIMPLE_ASSIGN:
1084 case GIMPLE_CALL:
1085 return gimple_fold_stmt_to_constant_1 (stmt, valueize_op);
1087 default:
1088 gcc_unreachable ();
1092 /* Apply the operation CODE in type TYPE to the value, mask pair
1093 RVAL and RMASK representing a value of type RTYPE and set
1094 the value, mask pair *VAL and *MASK to the result. */
1096 static void
1097 bit_value_unop_1 (enum tree_code code, tree type,
1098 double_int *val, double_int *mask,
1099 tree rtype, double_int rval, double_int rmask)
1101 switch (code)
1103 case BIT_NOT_EXPR:
1104 *mask = rmask;
1105 *val = ~rval;
1106 break;
1108 case NEGATE_EXPR:
1110 double_int temv, temm;
1111 /* Return ~rval + 1. */
1112 bit_value_unop_1 (BIT_NOT_EXPR, type, &temv, &temm, type, rval, rmask);
1113 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1114 type, temv, temm,
1115 type, double_int_one, double_int_zero);
1116 break;
1119 CASE_CONVERT:
1121 bool uns;
1123 /* First extend mask and value according to the original type. */
1124 uns = TYPE_UNSIGNED (rtype);
1125 *mask = rmask.ext (TYPE_PRECISION (rtype), uns);
1126 *val = rval.ext (TYPE_PRECISION (rtype), uns);
1128 /* Then extend mask and value according to the target type. */
1129 uns = TYPE_UNSIGNED (type);
1130 *mask = (*mask).ext (TYPE_PRECISION (type), uns);
1131 *val = (*val).ext (TYPE_PRECISION (type), uns);
1132 break;
1135 default:
1136 *mask = double_int_minus_one;
1137 break;
1141 /* Apply the operation CODE in type TYPE to the value, mask pairs
1142 R1VAL, R1MASK and R2VAL, R2MASK representing a values of type R1TYPE
1143 and R2TYPE and set the value, mask pair *VAL and *MASK to the result. */
1145 static void
1146 bit_value_binop_1 (enum tree_code code, tree type,
1147 double_int *val, double_int *mask,
1148 tree r1type, double_int r1val, double_int r1mask,
1149 tree r2type, double_int r2val, double_int r2mask)
1151 bool uns = TYPE_UNSIGNED (type);
1152 /* Assume we'll get a constant result. Use an initial varying value,
1153 we fall back to varying in the end if necessary. */
1154 *mask = double_int_minus_one;
1155 switch (code)
1157 case BIT_AND_EXPR:
1158 /* The mask is constant where there is a known not
1159 set bit, (m1 | m2) & ((v1 | m1) & (v2 | m2)) */
1160 *mask = (r1mask | r2mask) & (r1val | r1mask) & (r2val | r2mask);
1161 *val = r1val & r2val;
1162 break;
1164 case BIT_IOR_EXPR:
1165 /* The mask is constant where there is a known
1166 set bit, (m1 | m2) & ~((v1 & ~m1) | (v2 & ~m2)). */
1167 *mask = (r1mask | r2mask)
1168 .and_not (r1val.and_not (r1mask) | r2val.and_not (r2mask));
1169 *val = r1val | r2val;
1170 break;
1172 case BIT_XOR_EXPR:
1173 /* m1 | m2 */
1174 *mask = r1mask | r2mask;
1175 *val = r1val ^ r2val;
1176 break;
1178 case LROTATE_EXPR:
1179 case RROTATE_EXPR:
1180 if (r2mask.is_zero ())
1182 HOST_WIDE_INT shift = r2val.low;
1183 if (code == RROTATE_EXPR)
1184 shift = -shift;
1185 *mask = r1mask.lrotate (shift, TYPE_PRECISION (type));
1186 *val = r1val.lrotate (shift, TYPE_PRECISION (type));
1188 break;
1190 case LSHIFT_EXPR:
1191 case RSHIFT_EXPR:
1192 /* ??? We can handle partially known shift counts if we know
1193 its sign. That way we can tell that (x << (y | 8)) & 255
1194 is zero. */
1195 if (r2mask.is_zero ())
1197 HOST_WIDE_INT shift = r2val.low;
1198 if (code == RSHIFT_EXPR)
1199 shift = -shift;
1200 /* We need to know if we are doing a left or a right shift
1201 to properly shift in zeros for left shift and unsigned
1202 right shifts and the sign bit for signed right shifts.
1203 For signed right shifts we shift in varying in case
1204 the sign bit was varying. */
1205 if (shift > 0)
1207 *mask = r1mask.llshift (shift, TYPE_PRECISION (type));
1208 *val = r1val.llshift (shift, TYPE_PRECISION (type));
1210 else if (shift < 0)
1212 shift = -shift;
1213 *mask = r1mask.rshift (shift, TYPE_PRECISION (type), !uns);
1214 *val = r1val.rshift (shift, TYPE_PRECISION (type), !uns);
1216 else
1218 *mask = r1mask;
1219 *val = r1val;
1222 break;
1224 case PLUS_EXPR:
1225 case POINTER_PLUS_EXPR:
1227 double_int lo, hi;
1228 /* Do the addition with unknown bits set to zero, to give carry-ins of
1229 zero wherever possible. */
1230 lo = r1val.and_not (r1mask) + r2val.and_not (r2mask);
1231 lo = lo.ext (TYPE_PRECISION (type), uns);
1232 /* Do the addition with unknown bits set to one, to give carry-ins of
1233 one wherever possible. */
1234 hi = (r1val | r1mask) + (r2val | r2mask);
1235 hi = hi.ext (TYPE_PRECISION (type), uns);
1236 /* Each bit in the result is known if (a) the corresponding bits in
1237 both inputs are known, and (b) the carry-in to that bit position
1238 is known. We can check condition (b) by seeing if we got the same
1239 result with minimised carries as with maximised carries. */
1240 *mask = r1mask | r2mask | (lo ^ hi);
1241 *mask = (*mask).ext (TYPE_PRECISION (type), uns);
1242 /* It shouldn't matter whether we choose lo or hi here. */
1243 *val = lo;
1244 break;
1247 case MINUS_EXPR:
1249 double_int temv, temm;
1250 bit_value_unop_1 (NEGATE_EXPR, r2type, &temv, &temm,
1251 r2type, r2val, r2mask);
1252 bit_value_binop_1 (PLUS_EXPR, type, val, mask,
1253 r1type, r1val, r1mask,
1254 r2type, temv, temm);
1255 break;
1258 case MULT_EXPR:
1260 /* Just track trailing zeros in both operands and transfer
1261 them to the other. */
1262 int r1tz = (r1val | r1mask).trailing_zeros ();
1263 int r2tz = (r2val | r2mask).trailing_zeros ();
1264 if (r1tz + r2tz >= HOST_BITS_PER_DOUBLE_INT)
1266 *mask = double_int_zero;
1267 *val = double_int_zero;
1269 else if (r1tz + r2tz > 0)
1271 *mask = ~double_int::mask (r1tz + r2tz);
1272 *mask = (*mask).ext (TYPE_PRECISION (type), uns);
1273 *val = double_int_zero;
1275 break;
1278 case EQ_EXPR:
1279 case NE_EXPR:
1281 double_int m = r1mask | r2mask;
1282 if (r1val.and_not (m) != r2val.and_not (m))
1284 *mask = double_int_zero;
1285 *val = ((code == EQ_EXPR) ? double_int_zero : double_int_one);
1287 else
1289 /* We know the result of a comparison is always one or zero. */
1290 *mask = double_int_one;
1291 *val = double_int_zero;
1293 break;
1296 case GE_EXPR:
1297 case GT_EXPR:
1299 double_int tem = r1val;
1300 r1val = r2val;
1301 r2val = tem;
1302 tem = r1mask;
1303 r1mask = r2mask;
1304 r2mask = tem;
1305 code = swap_tree_comparison (code);
1307 /* Fallthru. */
1308 case LT_EXPR:
1309 case LE_EXPR:
1311 int minmax, maxmin;
1312 /* If the most significant bits are not known we know nothing. */
1313 if (r1mask.is_negative () || r2mask.is_negative ())
1314 break;
1316 /* For comparisons the signedness is in the comparison operands. */
1317 uns = TYPE_UNSIGNED (r1type);
1319 /* If we know the most significant bits we know the values
1320 value ranges by means of treating varying bits as zero
1321 or one. Do a cross comparison of the max/min pairs. */
1322 maxmin = (r1val | r1mask).cmp (r2val.and_not (r2mask), uns);
1323 minmax = r1val.and_not (r1mask).cmp (r2val | r2mask, uns);
1324 if (maxmin < 0) /* r1 is less than r2. */
1326 *mask = double_int_zero;
1327 *val = double_int_one;
1329 else if (minmax > 0) /* r1 is not less or equal to r2. */
1331 *mask = double_int_zero;
1332 *val = double_int_zero;
1334 else if (maxmin == minmax) /* r1 and r2 are equal. */
1336 /* This probably should never happen as we'd have
1337 folded the thing during fully constant value folding. */
1338 *mask = double_int_zero;
1339 *val = (code == LE_EXPR ? double_int_one : double_int_zero);
1341 else
1343 /* We know the result of a comparison is always one or zero. */
1344 *mask = double_int_one;
1345 *val = double_int_zero;
1347 break;
1350 default:;
1354 /* Return the propagation value when applying the operation CODE to
1355 the value RHS yielding type TYPE. */
1357 static prop_value_t
1358 bit_value_unop (enum tree_code code, tree type, tree rhs)
1360 prop_value_t rval = get_value_for_expr (rhs, true);
1361 double_int value, mask;
1362 prop_value_t val;
1364 if (rval.lattice_val == UNDEFINED)
1365 return rval;
1367 gcc_assert ((rval.lattice_val == CONSTANT
1368 && TREE_CODE (rval.value) == INTEGER_CST)
1369 || rval.mask.is_minus_one ());
1370 bit_value_unop_1 (code, type, &value, &mask,
1371 TREE_TYPE (rhs), value_to_double_int (rval), rval.mask);
1372 if (!mask.is_minus_one ())
1374 val.lattice_val = CONSTANT;
1375 val.mask = mask;
1376 /* ??? Delay building trees here. */
1377 val.value = double_int_to_tree (type, value);
1379 else
1381 val.lattice_val = VARYING;
1382 val.value = NULL_TREE;
1383 val.mask = double_int_minus_one;
1385 return val;
1388 /* Return the propagation value when applying the operation CODE to
1389 the values RHS1 and RHS2 yielding type TYPE. */
1391 static prop_value_t
1392 bit_value_binop (enum tree_code code, tree type, tree rhs1, tree rhs2)
1394 prop_value_t r1val = get_value_for_expr (rhs1, true);
1395 prop_value_t r2val = get_value_for_expr (rhs2, true);
1396 double_int value, mask;
1397 prop_value_t val;
1399 if (r1val.lattice_val == UNDEFINED
1400 || r2val.lattice_val == UNDEFINED)
1402 val.lattice_val = VARYING;
1403 val.value = NULL_TREE;
1404 val.mask = double_int_minus_one;
1405 return val;
1408 gcc_assert ((r1val.lattice_val == CONSTANT
1409 && TREE_CODE (r1val.value) == INTEGER_CST)
1410 || r1val.mask.is_minus_one ());
1411 gcc_assert ((r2val.lattice_val == CONSTANT
1412 && TREE_CODE (r2val.value) == INTEGER_CST)
1413 || r2val.mask.is_minus_one ());
1414 bit_value_binop_1 (code, type, &value, &mask,
1415 TREE_TYPE (rhs1), value_to_double_int (r1val), r1val.mask,
1416 TREE_TYPE (rhs2), value_to_double_int (r2val), r2val.mask);
1417 if (!mask.is_minus_one ())
1419 val.lattice_val = CONSTANT;
1420 val.mask = mask;
1421 /* ??? Delay building trees here. */
1422 val.value = double_int_to_tree (type, value);
1424 else
1426 val.lattice_val = VARYING;
1427 val.value = NULL_TREE;
1428 val.mask = double_int_minus_one;
1430 return val;
1433 /* Return the propagation value when applying __builtin_assume_aligned to
1434 its arguments. */
1436 static prop_value_t
1437 bit_value_assume_aligned (gimple stmt)
1439 tree ptr = gimple_call_arg (stmt, 0), align, misalign = NULL_TREE;
1440 tree type = TREE_TYPE (ptr);
1441 unsigned HOST_WIDE_INT aligni, misaligni = 0;
1442 prop_value_t ptrval = get_value_for_expr (ptr, true);
1443 prop_value_t alignval;
1444 double_int value, mask;
1445 prop_value_t val;
1446 if (ptrval.lattice_val == UNDEFINED)
1447 return ptrval;
1448 gcc_assert ((ptrval.lattice_val == CONSTANT
1449 && TREE_CODE (ptrval.value) == INTEGER_CST)
1450 || ptrval.mask.is_minus_one ());
1451 align = gimple_call_arg (stmt, 1);
1452 if (!host_integerp (align, 1))
1453 return ptrval;
1454 aligni = tree_low_cst (align, 1);
1455 if (aligni <= 1
1456 || (aligni & (aligni - 1)) != 0)
1457 return ptrval;
1458 if (gimple_call_num_args (stmt) > 2)
1460 misalign = gimple_call_arg (stmt, 2);
1461 if (!host_integerp (misalign, 1))
1462 return ptrval;
1463 misaligni = tree_low_cst (misalign, 1);
1464 if (misaligni >= aligni)
1465 return ptrval;
1467 align = build_int_cst_type (type, -aligni);
1468 alignval = get_value_for_expr (align, true);
1469 bit_value_binop_1 (BIT_AND_EXPR, type, &value, &mask,
1470 type, value_to_double_int (ptrval), ptrval.mask,
1471 type, value_to_double_int (alignval), alignval.mask);
1472 if (!mask.is_minus_one ())
1474 val.lattice_val = CONSTANT;
1475 val.mask = mask;
1476 gcc_assert ((mask.low & (aligni - 1)) == 0);
1477 gcc_assert ((value.low & (aligni - 1)) == 0);
1478 value.low |= misaligni;
1479 /* ??? Delay building trees here. */
1480 val.value = double_int_to_tree (type, value);
1482 else
1484 val.lattice_val = VARYING;
1485 val.value = NULL_TREE;
1486 val.mask = double_int_minus_one;
1488 return val;
1491 /* Evaluate statement STMT.
1492 Valid only for assignments, calls, conditionals, and switches. */
1494 static prop_value_t
1495 evaluate_stmt (gimple stmt)
1497 prop_value_t val;
1498 tree simplified = NULL_TREE;
1499 ccp_lattice_t likelyvalue = likely_value (stmt);
1500 bool is_constant = false;
1501 unsigned int align;
1503 if (dump_file && (dump_flags & TDF_DETAILS))
1505 fprintf (dump_file, "which is likely ");
1506 switch (likelyvalue)
1508 case CONSTANT:
1509 fprintf (dump_file, "CONSTANT");
1510 break;
1511 case UNDEFINED:
1512 fprintf (dump_file, "UNDEFINED");
1513 break;
1514 case VARYING:
1515 fprintf (dump_file, "VARYING");
1516 break;
1517 default:;
1519 fprintf (dump_file, "\n");
1522 /* If the statement is likely to have a CONSTANT result, then try
1523 to fold the statement to determine the constant value. */
1524 /* FIXME. This is the only place that we call ccp_fold.
1525 Since likely_value never returns CONSTANT for calls, we will
1526 not attempt to fold them, including builtins that may profit. */
1527 if (likelyvalue == CONSTANT)
1529 fold_defer_overflow_warnings ();
1530 simplified = ccp_fold (stmt);
1531 is_constant = simplified && is_gimple_min_invariant (simplified);
1532 fold_undefer_overflow_warnings (is_constant, stmt, 0);
1533 if (is_constant)
1535 /* The statement produced a constant value. */
1536 val.lattice_val = CONSTANT;
1537 val.value = simplified;
1538 val.mask = double_int_zero;
1541 /* If the statement is likely to have a VARYING result, then do not
1542 bother folding the statement. */
1543 else if (likelyvalue == VARYING)
1545 enum gimple_code code = gimple_code (stmt);
1546 if (code == GIMPLE_ASSIGN)
1548 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1550 /* Other cases cannot satisfy is_gimple_min_invariant
1551 without folding. */
1552 if (get_gimple_rhs_class (subcode) == GIMPLE_SINGLE_RHS)
1553 simplified = gimple_assign_rhs1 (stmt);
1555 else if (code == GIMPLE_SWITCH)
1556 simplified = gimple_switch_index (stmt);
1557 else
1558 /* These cannot satisfy is_gimple_min_invariant without folding. */
1559 gcc_assert (code == GIMPLE_CALL || code == GIMPLE_COND);
1560 is_constant = simplified && is_gimple_min_invariant (simplified);
1561 if (is_constant)
1563 /* The statement produced a constant value. */
1564 val.lattice_val = CONSTANT;
1565 val.value = simplified;
1566 val.mask = double_int_zero;
1570 /* Resort to simplification for bitwise tracking. */
1571 if (flag_tree_bit_ccp
1572 && (likelyvalue == CONSTANT || is_gimple_call (stmt))
1573 && !is_constant)
1575 enum gimple_code code = gimple_code (stmt);
1576 val.lattice_val = VARYING;
1577 val.value = NULL_TREE;
1578 val.mask = double_int_minus_one;
1579 if (code == GIMPLE_ASSIGN)
1581 enum tree_code subcode = gimple_assign_rhs_code (stmt);
1582 tree rhs1 = gimple_assign_rhs1 (stmt);
1583 switch (get_gimple_rhs_class (subcode))
1585 case GIMPLE_SINGLE_RHS:
1586 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1587 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1588 val = get_value_for_expr (rhs1, true);
1589 break;
1591 case GIMPLE_UNARY_RHS:
1592 if ((INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1593 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1594 && (INTEGRAL_TYPE_P (gimple_expr_type (stmt))
1595 || POINTER_TYPE_P (gimple_expr_type (stmt))))
1596 val = bit_value_unop (subcode, gimple_expr_type (stmt), rhs1);
1597 break;
1599 case GIMPLE_BINARY_RHS:
1600 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1601 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1603 tree lhs = gimple_assign_lhs (stmt);
1604 tree rhs2 = gimple_assign_rhs2 (stmt);
1605 val = bit_value_binop (subcode,
1606 TREE_TYPE (lhs), rhs1, rhs2);
1608 break;
1610 default:;
1613 else if (code == GIMPLE_COND)
1615 enum tree_code code = gimple_cond_code (stmt);
1616 tree rhs1 = gimple_cond_lhs (stmt);
1617 tree rhs2 = gimple_cond_rhs (stmt);
1618 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1619 || POINTER_TYPE_P (TREE_TYPE (rhs1)))
1620 val = bit_value_binop (code, TREE_TYPE (rhs1), rhs1, rhs2);
1622 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1624 tree fndecl = gimple_call_fndecl (stmt);
1625 switch (DECL_FUNCTION_CODE (fndecl))
1627 case BUILT_IN_MALLOC:
1628 case BUILT_IN_REALLOC:
1629 case BUILT_IN_CALLOC:
1630 case BUILT_IN_STRDUP:
1631 case BUILT_IN_STRNDUP:
1632 val.lattice_val = CONSTANT;
1633 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1634 val.mask = double_int::from_shwi
1635 (~(((HOST_WIDE_INT) MALLOC_ABI_ALIGNMENT)
1636 / BITS_PER_UNIT - 1));
1637 break;
1639 case BUILT_IN_ALLOCA:
1640 case BUILT_IN_ALLOCA_WITH_ALIGN:
1641 align = (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN
1642 ? TREE_INT_CST_LOW (gimple_call_arg (stmt, 1))
1643 : BIGGEST_ALIGNMENT);
1644 val.lattice_val = CONSTANT;
1645 val.value = build_int_cst (TREE_TYPE (gimple_get_lhs (stmt)), 0);
1646 val.mask = double_int::from_shwi (~(((HOST_WIDE_INT) align)
1647 / BITS_PER_UNIT - 1));
1648 break;
1650 /* These builtins return their first argument, unmodified. */
1651 case BUILT_IN_MEMCPY:
1652 case BUILT_IN_MEMMOVE:
1653 case BUILT_IN_MEMSET:
1654 case BUILT_IN_STRCPY:
1655 case BUILT_IN_STRNCPY:
1656 case BUILT_IN_MEMCPY_CHK:
1657 case BUILT_IN_MEMMOVE_CHK:
1658 case BUILT_IN_MEMSET_CHK:
1659 case BUILT_IN_STRCPY_CHK:
1660 case BUILT_IN_STRNCPY_CHK:
1661 val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
1662 break;
1664 case BUILT_IN_ASSUME_ALIGNED:
1665 val = bit_value_assume_aligned (stmt);
1666 break;
1668 default:;
1671 is_constant = (val.lattice_val == CONSTANT);
1674 if (!is_constant)
1676 /* The statement produced a nonconstant value. If the statement
1677 had UNDEFINED operands, then the result of the statement
1678 should be UNDEFINED. Otherwise, the statement is VARYING. */
1679 if (likelyvalue == UNDEFINED)
1681 val.lattice_val = likelyvalue;
1682 val.mask = double_int_zero;
1684 else
1686 val.lattice_val = VARYING;
1687 val.mask = double_int_minus_one;
1690 val.value = NULL_TREE;
1693 return val;
1696 typedef hash_table <pointer_hash <gimple_statement_d> > gimple_htab;
1698 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
1699 each matching BUILT_IN_STACK_RESTORE. Mark visited phis in VISITED. */
1701 static void
1702 insert_clobber_before_stack_restore (tree saved_val, tree var,
1703 gimple_htab *visited)
1705 gimple stmt, clobber_stmt;
1706 tree clobber;
1707 imm_use_iterator iter;
1708 gimple_stmt_iterator i;
1709 gimple *slot;
1711 FOR_EACH_IMM_USE_STMT (stmt, iter, saved_val)
1712 if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
1714 clobber = build_constructor (TREE_TYPE (var),
1715 NULL);
1716 TREE_THIS_VOLATILE (clobber) = 1;
1717 clobber_stmt = gimple_build_assign (var, clobber);
1719 i = gsi_for_stmt (stmt);
1720 gsi_insert_before (&i, clobber_stmt, GSI_SAME_STMT);
1722 else if (gimple_code (stmt) == GIMPLE_PHI)
1724 if (!visited->is_created ())
1725 visited->create (10);
1727 slot = visited->find_slot (stmt, INSERT);
1728 if (*slot != NULL)
1729 continue;
1731 *slot = stmt;
1732 insert_clobber_before_stack_restore (gimple_phi_result (stmt), var,
1733 visited);
1735 else if (gimple_assign_ssa_name_copy_p (stmt))
1736 insert_clobber_before_stack_restore (gimple_assign_lhs (stmt), var,
1737 visited);
1738 else
1739 gcc_assert (is_gimple_debug (stmt));
1742 /* Advance the iterator to the previous non-debug gimple statement in the same
1743 or dominating basic block. */
1745 static inline void
1746 gsi_prev_dom_bb_nondebug (gimple_stmt_iterator *i)
1748 basic_block dom;
1750 gsi_prev_nondebug (i);
1751 while (gsi_end_p (*i))
1753 dom = get_immediate_dominator (CDI_DOMINATORS, i->bb);
1754 if (dom == NULL || dom == ENTRY_BLOCK_PTR)
1755 return;
1757 *i = gsi_last_bb (dom);
1761 /* Find a BUILT_IN_STACK_SAVE dominating gsi_stmt (I), and insert
1762 a clobber of VAR before each matching BUILT_IN_STACK_RESTORE.
1764 It is possible that BUILT_IN_STACK_SAVE cannot be find in a dominator when a
1765 previous pass (such as DOM) duplicated it along multiple paths to a BB. In
1766 that case the function gives up without inserting the clobbers. */
1768 static void
1769 insert_clobbers_for_var (gimple_stmt_iterator i, tree var)
1771 gimple stmt;
1772 tree saved_val;
1773 gimple_htab visited;
1775 for (; !gsi_end_p (i); gsi_prev_dom_bb_nondebug (&i))
1777 stmt = gsi_stmt (i);
1779 if (!gimple_call_builtin_p (stmt, BUILT_IN_STACK_SAVE))
1780 continue;
1782 saved_val = gimple_call_lhs (stmt);
1783 if (saved_val == NULL_TREE)
1784 continue;
1786 insert_clobber_before_stack_restore (saved_val, var, &visited);
1787 break;
1790 if (visited.is_created ())
1791 visited.dispose ();
1794 /* Detects a __builtin_alloca_with_align with constant size argument. Declares
1795 fixed-size array and returns the address, if found, otherwise returns
1796 NULL_TREE. */
1798 static tree
1799 fold_builtin_alloca_with_align (gimple stmt)
1801 unsigned HOST_WIDE_INT size, threshold, n_elem;
1802 tree lhs, arg, block, var, elem_type, array_type;
1804 /* Get lhs. */
1805 lhs = gimple_call_lhs (stmt);
1806 if (lhs == NULL_TREE)
1807 return NULL_TREE;
1809 /* Detect constant argument. */
1810 arg = get_constant_value (gimple_call_arg (stmt, 0));
1811 if (arg == NULL_TREE
1812 || TREE_CODE (arg) != INTEGER_CST
1813 || !host_integerp (arg, 1))
1814 return NULL_TREE;
1816 size = TREE_INT_CST_LOW (arg);
1818 /* Heuristic: don't fold large allocas. */
1819 threshold = (unsigned HOST_WIDE_INT)PARAM_VALUE (PARAM_LARGE_STACK_FRAME);
1820 /* In case the alloca is located at function entry, it has the same lifetime
1821 as a declared array, so we allow a larger size. */
1822 block = gimple_block (stmt);
1823 if (!(cfun->after_inlining
1824 && TREE_CODE (BLOCK_SUPERCONTEXT (block)) == FUNCTION_DECL))
1825 threshold /= 10;
1826 if (size > threshold)
1827 return NULL_TREE;
1829 /* Declare array. */
1830 elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
1831 n_elem = size * 8 / BITS_PER_UNIT;
1832 array_type = build_array_type_nelts (elem_type, n_elem);
1833 var = create_tmp_var (array_type, NULL);
1834 DECL_ALIGN (var) = TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
1836 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (lhs);
1837 if (pi != NULL && !pi->pt.anything)
1839 bool singleton_p;
1840 unsigned uid;
1841 singleton_p = pt_solution_singleton_p (&pi->pt, &uid);
1842 gcc_assert (singleton_p);
1843 SET_DECL_PT_UID (var, uid);
1847 /* Fold alloca to the address of the array. */
1848 return fold_convert (TREE_TYPE (lhs), build_fold_addr_expr (var));
1851 /* Fold the stmt at *GSI with CCP specific information that propagating
1852 and regular folding does not catch. */
1854 static bool
1855 ccp_fold_stmt (gimple_stmt_iterator *gsi)
1857 gimple stmt = gsi_stmt (*gsi);
1859 switch (gimple_code (stmt))
1861 case GIMPLE_COND:
1863 prop_value_t val;
1864 /* Statement evaluation will handle type mismatches in constants
1865 more gracefully than the final propagation. This allows us to
1866 fold more conditionals here. */
1867 val = evaluate_stmt (stmt);
1868 if (val.lattice_val != CONSTANT
1869 || !val.mask.is_zero ())
1870 return false;
1872 if (dump_file)
1874 fprintf (dump_file, "Folding predicate ");
1875 print_gimple_expr (dump_file, stmt, 0, 0);
1876 fprintf (dump_file, " to ");
1877 print_generic_expr (dump_file, val.value, 0);
1878 fprintf (dump_file, "\n");
1881 if (integer_zerop (val.value))
1882 gimple_cond_make_false (stmt);
1883 else
1884 gimple_cond_make_true (stmt);
1886 return true;
1889 case GIMPLE_CALL:
1891 tree lhs = gimple_call_lhs (stmt);
1892 int flags = gimple_call_flags (stmt);
1893 tree val;
1894 tree argt;
1895 bool changed = false;
1896 unsigned i;
1898 /* If the call was folded into a constant make sure it goes
1899 away even if we cannot propagate into all uses because of
1900 type issues. */
1901 if (lhs
1902 && TREE_CODE (lhs) == SSA_NAME
1903 && (val = get_constant_value (lhs))
1904 /* Don't optimize away calls that have side-effects. */
1905 && (flags & (ECF_CONST|ECF_PURE)) != 0
1906 && (flags & ECF_LOOPING_CONST_OR_PURE) == 0)
1908 tree new_rhs = unshare_expr (val);
1909 bool res;
1910 if (!useless_type_conversion_p (TREE_TYPE (lhs),
1911 TREE_TYPE (new_rhs)))
1912 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
1913 res = update_call_from_tree (gsi, new_rhs);
1914 gcc_assert (res);
1915 return true;
1918 /* Internal calls provide no argument types, so the extra laxity
1919 for normal calls does not apply. */
1920 if (gimple_call_internal_p (stmt))
1921 return false;
1923 /* The heuristic of fold_builtin_alloca_with_align differs before and
1924 after inlining, so we don't require the arg to be changed into a
1925 constant for folding, but just to be constant. */
1926 if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
1928 tree new_rhs = fold_builtin_alloca_with_align (stmt);
1929 if (new_rhs)
1931 bool res = update_call_from_tree (gsi, new_rhs);
1932 tree var = TREE_OPERAND (TREE_OPERAND (new_rhs, 0),0);
1933 gcc_assert (res);
1934 insert_clobbers_for_var (*gsi, var);
1935 return true;
1939 /* Propagate into the call arguments. Compared to replace_uses_in
1940 this can use the argument slot types for type verification
1941 instead of the current argument type. We also can safely
1942 drop qualifiers here as we are dealing with constants anyway. */
1943 argt = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
1944 for (i = 0; i < gimple_call_num_args (stmt) && argt;
1945 ++i, argt = TREE_CHAIN (argt))
1947 tree arg = gimple_call_arg (stmt, i);
1948 if (TREE_CODE (arg) == SSA_NAME
1949 && (val = get_constant_value (arg))
1950 && useless_type_conversion_p
1951 (TYPE_MAIN_VARIANT (TREE_VALUE (argt)),
1952 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1954 gimple_call_set_arg (stmt, i, unshare_expr (val));
1955 changed = true;
1959 return changed;
1962 case GIMPLE_ASSIGN:
1964 tree lhs = gimple_assign_lhs (stmt);
1965 tree val;
1967 /* If we have a load that turned out to be constant replace it
1968 as we cannot propagate into all uses in all cases. */
1969 if (gimple_assign_single_p (stmt)
1970 && TREE_CODE (lhs) == SSA_NAME
1971 && (val = get_constant_value (lhs)))
1973 tree rhs = unshare_expr (val);
1974 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
1975 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs);
1976 gimple_assign_set_rhs_from_tree (gsi, rhs);
1977 return true;
1980 return false;
1983 default:
1984 return false;
1988 /* Visit the assignment statement STMT. Set the value of its LHS to the
1989 value computed by the RHS and store LHS in *OUTPUT_P. If STMT
1990 creates virtual definitions, set the value of each new name to that
1991 of the RHS (if we can derive a constant out of the RHS).
1992 Value-returning call statements also perform an assignment, and
1993 are handled here. */
1995 static enum ssa_prop_result
1996 visit_assignment (gimple stmt, tree *output_p)
1998 prop_value_t val;
1999 enum ssa_prop_result retval;
2001 tree lhs = gimple_get_lhs (stmt);
2003 gcc_assert (gimple_code (stmt) != GIMPLE_CALL
2004 || gimple_call_lhs (stmt) != NULL_TREE);
2006 if (gimple_assign_single_p (stmt)
2007 && gimple_assign_rhs_code (stmt) == SSA_NAME)
2008 /* For a simple copy operation, we copy the lattice values. */
2009 val = *get_value (gimple_assign_rhs1 (stmt));
2010 else
2011 /* Evaluate the statement, which could be
2012 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2013 val = evaluate_stmt (stmt);
2015 retval = SSA_PROP_NOT_INTERESTING;
2017 /* Set the lattice value of the statement's output. */
2018 if (TREE_CODE (lhs) == SSA_NAME)
2020 /* If STMT is an assignment to an SSA_NAME, we only have one
2021 value to set. */
2022 if (set_lattice_value (lhs, val))
2024 *output_p = lhs;
2025 if (val.lattice_val == VARYING)
2026 retval = SSA_PROP_VARYING;
2027 else
2028 retval = SSA_PROP_INTERESTING;
2032 return retval;
2036 /* Visit the conditional statement STMT. Return SSA_PROP_INTERESTING
2037 if it can determine which edge will be taken. Otherwise, return
2038 SSA_PROP_VARYING. */
2040 static enum ssa_prop_result
2041 visit_cond_stmt (gimple stmt, edge *taken_edge_p)
2043 prop_value_t val;
2044 basic_block block;
2046 block = gimple_bb (stmt);
2047 val = evaluate_stmt (stmt);
2048 if (val.lattice_val != CONSTANT
2049 || !val.mask.is_zero ())
2050 return SSA_PROP_VARYING;
2052 /* Find which edge out of the conditional block will be taken and add it
2053 to the worklist. If no single edge can be determined statically,
2054 return SSA_PROP_VARYING to feed all the outgoing edges to the
2055 propagation engine. */
2056 *taken_edge_p = find_taken_edge (block, val.value);
2057 if (*taken_edge_p)
2058 return SSA_PROP_INTERESTING;
2059 else
2060 return SSA_PROP_VARYING;
2064 /* Evaluate statement STMT. If the statement produces an output value and
2065 its evaluation changes the lattice value of its output, return
2066 SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
2067 output value.
2069 If STMT is a conditional branch and we can determine its truth
2070 value, set *TAKEN_EDGE_P accordingly. If STMT produces a varying
2071 value, return SSA_PROP_VARYING. */
2073 static enum ssa_prop_result
2074 ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
2076 tree def;
2077 ssa_op_iter iter;
2079 if (dump_file && (dump_flags & TDF_DETAILS))
2081 fprintf (dump_file, "\nVisiting statement:\n");
2082 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2085 switch (gimple_code (stmt))
2087 case GIMPLE_ASSIGN:
2088 /* If the statement is an assignment that produces a single
2089 output value, evaluate its RHS to see if the lattice value of
2090 its output has changed. */
2091 return visit_assignment (stmt, output_p);
2093 case GIMPLE_CALL:
2094 /* A value-returning call also performs an assignment. */
2095 if (gimple_call_lhs (stmt) != NULL_TREE)
2096 return visit_assignment (stmt, output_p);
2097 break;
2099 case GIMPLE_COND:
2100 case GIMPLE_SWITCH:
2101 /* If STMT is a conditional branch, see if we can determine
2102 which branch will be taken. */
2103 /* FIXME. It appears that we should be able to optimize
2104 computed GOTOs here as well. */
2105 return visit_cond_stmt (stmt, taken_edge_p);
2107 default:
2108 break;
2111 /* Any other kind of statement is not interesting for constant
2112 propagation and, therefore, not worth simulating. */
2113 if (dump_file && (dump_flags & TDF_DETAILS))
2114 fprintf (dump_file, "No interesting values produced. Marked VARYING.\n");
2116 /* Definitions made by statements other than assignments to
2117 SSA_NAMEs represent unknown modifications to their outputs.
2118 Mark them VARYING. */
2119 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
2121 prop_value_t v = { VARYING, NULL_TREE, { -1, (HOST_WIDE_INT) -1 } };
2122 set_lattice_value (def, v);
2125 return SSA_PROP_VARYING;
2129 /* Main entry point for SSA Conditional Constant Propagation. */
2131 static unsigned int
2132 do_ssa_ccp (void)
2134 unsigned int todo = 0;
2135 calculate_dominance_info (CDI_DOMINATORS);
2136 ccp_initialize ();
2137 ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
2138 if (ccp_finalize ())
2139 todo = (TODO_cleanup_cfg | TODO_update_ssa);
2140 free_dominance_info (CDI_DOMINATORS);
2141 return todo;
2145 static bool
2146 gate_ccp (void)
2148 return flag_tree_ccp != 0;
2152 namespace {
2154 const pass_data pass_data_ccp =
2156 GIMPLE_PASS, /* type */
2157 "ccp", /* name */
2158 OPTGROUP_NONE, /* optinfo_flags */
2159 true, /* has_gate */
2160 true, /* has_execute */
2161 TV_TREE_CCP, /* tv_id */
2162 ( PROP_cfg | PROP_ssa ), /* properties_required */
2163 0, /* properties_provided */
2164 0, /* properties_destroyed */
2165 0, /* todo_flags_start */
2166 ( TODO_verify_ssa | TODO_update_address_taken
2167 | TODO_verify_stmts ), /* todo_flags_finish */
2170 class pass_ccp : public gimple_opt_pass
2172 public:
2173 pass_ccp (gcc::context *ctxt)
2174 : gimple_opt_pass (pass_data_ccp, ctxt)
2177 /* opt_pass methods: */
2178 opt_pass * clone () { return new pass_ccp (m_ctxt); }
2179 bool gate () { return gate_ccp (); }
2180 unsigned int execute () { return do_ssa_ccp (); }
2182 }; // class pass_ccp
2184 } // anon namespace
2186 gimple_opt_pass *
2187 make_pass_ccp (gcc::context *ctxt)
2189 return new pass_ccp (ctxt);
2194 /* Try to optimize out __builtin_stack_restore. Optimize it out
2195 if there is another __builtin_stack_restore in the same basic
2196 block and no calls or ASM_EXPRs are in between, or if this block's
2197 only outgoing edge is to EXIT_BLOCK and there are no calls or
2198 ASM_EXPRs after this __builtin_stack_restore. */
2200 static tree
2201 optimize_stack_restore (gimple_stmt_iterator i)
2203 tree callee;
2204 gimple stmt;
2206 basic_block bb = gsi_bb (i);
2207 gimple call = gsi_stmt (i);
2209 if (gimple_code (call) != GIMPLE_CALL
2210 || gimple_call_num_args (call) != 1
2211 || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2212 || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2213 return NULL_TREE;
2215 for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2217 stmt = gsi_stmt (i);
2218 if (gimple_code (stmt) == GIMPLE_ASM)
2219 return NULL_TREE;
2220 if (gimple_code (stmt) != GIMPLE_CALL)
2221 continue;
2223 callee = gimple_call_fndecl (stmt);
2224 if (!callee
2225 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2226 /* All regular builtins are ok, just obviously not alloca. */
2227 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
2228 || DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA_WITH_ALIGN)
2229 return NULL_TREE;
2231 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_RESTORE)
2232 goto second_stack_restore;
2235 if (!gsi_end_p (i))
2236 return NULL_TREE;
2238 /* Allow one successor of the exit block, or zero successors. */
2239 switch (EDGE_COUNT (bb->succs))
2241 case 0:
2242 break;
2243 case 1:
2244 if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR)
2245 return NULL_TREE;
2246 break;
2247 default:
2248 return NULL_TREE;
2250 second_stack_restore:
2252 /* If there's exactly one use, then zap the call to __builtin_stack_save.
2253 If there are multiple uses, then the last one should remove the call.
2254 In any case, whether the call to __builtin_stack_save can be removed
2255 or not is irrelevant to removing the call to __builtin_stack_restore. */
2256 if (has_single_use (gimple_call_arg (call, 0)))
2258 gimple stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2259 if (is_gimple_call (stack_save))
2261 callee = gimple_call_fndecl (stack_save);
2262 if (callee
2263 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
2264 && DECL_FUNCTION_CODE (callee) == BUILT_IN_STACK_SAVE)
2266 gimple_stmt_iterator stack_save_gsi;
2267 tree rhs;
2269 stack_save_gsi = gsi_for_stmt (stack_save);
2270 rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2271 update_call_from_tree (&stack_save_gsi, rhs);
2276 /* No effect, so the statement will be deleted. */
2277 return integer_zero_node;
2280 /* If va_list type is a simple pointer and nothing special is needed,
2281 optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
2282 __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
2283 pointer assignment. */
2285 static tree
2286 optimize_stdarg_builtin (gimple call)
2288 tree callee, lhs, rhs, cfun_va_list;
2289 bool va_list_simple_ptr;
2290 location_t loc = gimple_location (call);
2292 if (gimple_code (call) != GIMPLE_CALL)
2293 return NULL_TREE;
2295 callee = gimple_call_fndecl (call);
2297 cfun_va_list = targetm.fn_abi_va_list (callee);
2298 va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
2299 && (TREE_TYPE (cfun_va_list) == void_type_node
2300 || TREE_TYPE (cfun_va_list) == char_type_node);
2302 switch (DECL_FUNCTION_CODE (callee))
2304 case BUILT_IN_VA_START:
2305 if (!va_list_simple_ptr
2306 || targetm.expand_builtin_va_start != NULL
2307 || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
2308 return NULL_TREE;
2310 if (gimple_call_num_args (call) != 2)
2311 return NULL_TREE;
2313 lhs = gimple_call_arg (call, 0);
2314 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2315 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2316 != TYPE_MAIN_VARIANT (cfun_va_list))
2317 return NULL_TREE;
2319 lhs = build_fold_indirect_ref_loc (loc, lhs);
2320 rhs = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_NEXT_ARG),
2321 1, integer_zero_node);
2322 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2323 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2325 case BUILT_IN_VA_COPY:
2326 if (!va_list_simple_ptr)
2327 return NULL_TREE;
2329 if (gimple_call_num_args (call) != 2)
2330 return NULL_TREE;
2332 lhs = gimple_call_arg (call, 0);
2333 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
2334 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
2335 != TYPE_MAIN_VARIANT (cfun_va_list))
2336 return NULL_TREE;
2338 lhs = build_fold_indirect_ref_loc (loc, lhs);
2339 rhs = gimple_call_arg (call, 1);
2340 if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
2341 != TYPE_MAIN_VARIANT (cfun_va_list))
2342 return NULL_TREE;
2344 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2345 return build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, rhs);
2347 case BUILT_IN_VA_END:
2348 /* No effect, so the statement will be deleted. */
2349 return integer_zero_node;
2351 default:
2352 gcc_unreachable ();
2356 /* Attemp to make the block of __builtin_unreachable I unreachable by changing
2357 the incoming jumps. Return true if at least one jump was changed. */
2359 static bool
2360 optimize_unreachable (gimple_stmt_iterator i)
2362 basic_block bb = gsi_bb (i);
2363 gimple_stmt_iterator gsi;
2364 gimple stmt;
2365 edge_iterator ei;
2366 edge e;
2367 bool ret;
2369 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2371 stmt = gsi_stmt (gsi);
2373 if (is_gimple_debug (stmt))
2374 continue;
2376 if (gimple_code (stmt) == GIMPLE_LABEL)
2378 /* Verify we do not need to preserve the label. */
2379 if (FORCED_LABEL (gimple_label_label (stmt)))
2380 return false;
2382 continue;
2385 /* Only handle the case that __builtin_unreachable is the first statement
2386 in the block. We rely on DCE to remove stmts without side-effects
2387 before __builtin_unreachable. */
2388 if (gsi_stmt (gsi) != gsi_stmt (i))
2389 return false;
2392 ret = false;
2393 FOR_EACH_EDGE (e, ei, bb->preds)
2395 gsi = gsi_last_bb (e->src);
2396 if (gsi_end_p (gsi))
2397 continue;
2399 stmt = gsi_stmt (gsi);
2400 if (gimple_code (stmt) == GIMPLE_COND)
2402 if (e->flags & EDGE_TRUE_VALUE)
2403 gimple_cond_make_false (stmt);
2404 else if (e->flags & EDGE_FALSE_VALUE)
2405 gimple_cond_make_true (stmt);
2406 else
2407 gcc_unreachable ();
2408 update_stmt (stmt);
2410 else
2412 /* Todo: handle other cases, f.i. switch statement. */
2413 continue;
2416 ret = true;
2419 return ret;
2422 /* A simple pass that attempts to fold all builtin functions. This pass
2423 is run after we've propagated as many constants as we can. */
2425 static unsigned int
2426 execute_fold_all_builtins (void)
2428 bool cfg_changed = false;
2429 basic_block bb;
2430 unsigned int todoflags = 0;
2432 FOR_EACH_BB (bb)
2434 gimple_stmt_iterator i;
2435 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
2437 gimple stmt, old_stmt;
2438 tree callee, result;
2439 enum built_in_function fcode;
2441 stmt = gsi_stmt (i);
2443 if (gimple_code (stmt) != GIMPLE_CALL)
2445 /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
2446 after the last GIMPLE DSE they aren't needed and might
2447 unnecessarily keep the SSA_NAMEs live. */
2448 if (gimple_clobber_p (stmt))
2450 tree lhs = gimple_assign_lhs (stmt);
2451 if (TREE_CODE (lhs) == MEM_REF
2452 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
2454 unlink_stmt_vdef (stmt);
2455 gsi_remove (&i, true);
2456 release_defs (stmt);
2457 continue;
2460 gsi_next (&i);
2461 continue;
2463 callee = gimple_call_fndecl (stmt);
2464 if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2466 gsi_next (&i);
2467 continue;
2469 fcode = DECL_FUNCTION_CODE (callee);
2471 result = gimple_fold_builtin (stmt);
2473 if (result)
2474 gimple_remove_stmt_histograms (cfun, stmt);
2476 if (!result)
2477 switch (DECL_FUNCTION_CODE (callee))
2479 case BUILT_IN_CONSTANT_P:
2480 /* Resolve __builtin_constant_p. If it hasn't been
2481 folded to integer_one_node by now, it's fairly
2482 certain that the value simply isn't constant. */
2483 result = integer_zero_node;
2484 break;
2486 case BUILT_IN_ASSUME_ALIGNED:
2487 /* Remove __builtin_assume_aligned. */
2488 result = gimple_call_arg (stmt, 0);
2489 break;
2491 case BUILT_IN_STACK_RESTORE:
2492 result = optimize_stack_restore (i);
2493 if (result)
2494 break;
2495 gsi_next (&i);
2496 continue;
2498 case BUILT_IN_UNREACHABLE:
2499 if (optimize_unreachable (i))
2500 cfg_changed = true;
2501 break;
2503 case BUILT_IN_VA_START:
2504 case BUILT_IN_VA_END:
2505 case BUILT_IN_VA_COPY:
2506 /* These shouldn't be folded before pass_stdarg. */
2507 result = optimize_stdarg_builtin (stmt);
2508 if (result)
2509 break;
2510 /* FALLTHRU */
2512 default:
2513 gsi_next (&i);
2514 continue;
2517 if (result == NULL_TREE)
2518 break;
2520 if (dump_file && (dump_flags & TDF_DETAILS))
2522 fprintf (dump_file, "Simplified\n ");
2523 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2526 old_stmt = stmt;
2527 if (!update_call_from_tree (&i, result))
2529 gimplify_and_update_call_from_tree (&i, result);
2530 todoflags |= TODO_update_address_taken;
2533 stmt = gsi_stmt (i);
2534 update_stmt (stmt);
2536 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt)
2537 && gimple_purge_dead_eh_edges (bb))
2538 cfg_changed = true;
2540 if (dump_file && (dump_flags & TDF_DETAILS))
2542 fprintf (dump_file, "to\n ");
2543 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
2544 fprintf (dump_file, "\n");
2547 /* Retry the same statement if it changed into another
2548 builtin, there might be new opportunities now. */
2549 if (gimple_code (stmt) != GIMPLE_CALL)
2551 gsi_next (&i);
2552 continue;
2554 callee = gimple_call_fndecl (stmt);
2555 if (!callee
2556 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2557 || DECL_FUNCTION_CODE (callee) == fcode)
2558 gsi_next (&i);
2562 /* Delete unreachable blocks. */
2563 if (cfg_changed)
2564 todoflags |= TODO_cleanup_cfg;
2566 return todoflags;
2570 namespace {
2572 const pass_data pass_data_fold_builtins =
2574 GIMPLE_PASS, /* type */
2575 "fab", /* name */
2576 OPTGROUP_NONE, /* optinfo_flags */
2577 false, /* has_gate */
2578 true, /* has_execute */
2579 TV_NONE, /* tv_id */
2580 ( PROP_cfg | PROP_ssa ), /* properties_required */
2581 0, /* properties_provided */
2582 0, /* properties_destroyed */
2583 0, /* todo_flags_start */
2584 ( TODO_verify_ssa | TODO_update_ssa ), /* todo_flags_finish */
2587 class pass_fold_builtins : public gimple_opt_pass
2589 public:
2590 pass_fold_builtins (gcc::context *ctxt)
2591 : gimple_opt_pass (pass_data_fold_builtins, ctxt)
2594 /* opt_pass methods: */
2595 opt_pass * clone () { return new pass_fold_builtins (m_ctxt); }
2596 unsigned int execute () { return execute_fold_all_builtins (); }
2598 }; // class pass_fold_builtins
2600 } // anon namespace
2602 gimple_opt_pass *
2603 make_pass_fold_builtins (gcc::context *ctxt)
2605 return new pass_fold_builtins (ctxt);