tree-ssa-ccp.c revision 225736
1187517Sgonzo/* Conditional constant propagation pass for the GNU compiler.
2187517Sgonzo   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3187517Sgonzo   Free Software Foundation, Inc.
4187517Sgonzo   Adapted from original RTL SSA-CCP by Daniel Berlin <dberlin@dberlin.org>
5187517Sgonzo   Adapted to GIMPLE trees by Diego Novillo <dnovillo@redhat.com>
6187517Sgonzo
7187517SgonzoThis file is part of GCC.
8187517Sgonzo
9187517SgonzoGCC is free software; you can redistribute it and/or modify it
10187517Sgonzounder the terms of the GNU General Public License as published by the
11187517SgonzoFree Software Foundation; either version 2, or (at your option) any
12187517Sgonzolater version.
13187517Sgonzo
14187517SgonzoGCC is distributed in the hope that it will be useful, but WITHOUT
15187517SgonzoANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16187517SgonzoFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17187517Sgonzofor more details.
18187517Sgonzo
19187517SgonzoYou should have received a copy of the GNU General Public License
20187517Sgonzoalong with GCC; see the file COPYING.  If not, write to the Free
21187517SgonzoSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22187517Sgonzo02110-1301, USA.  */
23187517Sgonzo
24187517Sgonzo/* Conditional constant propagation (CCP) is based on the SSA
25187517Sgonzo   propagation engine (tree-ssa-propagate.c).  Constant assignments of
26187517Sgonzo   the form VAR = CST are propagated from the assignments into uses of
27187517Sgonzo   VAR, which in turn may generate new constants.  The simulation uses
28187517Sgonzo   a four level lattice to keep track of constant values associated
29187517Sgonzo   with SSA names.  Given an SSA name V_i, it may take one of the
30187517Sgonzo   following values:
31187517Sgonzo
32187517Sgonzo   	UNINITIALIZED	->  This is the default starting value.  V_i
33187517Sgonzo			    has not been processed yet.
34187517Sgonzo
35187517Sgonzo	UNDEFINED	->  V_i is a local variable whose definition
36187517Sgonzo			    has not been processed yet.  Therefore we
37187517Sgonzo			    don't yet know if its value is a constant
38187517Sgonzo			    or not.
39232847Sgonzo
40232847Sgonzo	CONSTANT	->  V_i has been found to hold a constant
41232847Sgonzo			    value C.
42232847Sgonzo
43187517Sgonzo	VARYING		->  V_i cannot take a constant value, or if it
44187517Sgonzo			    does, it is not possible to determine it
45199497Sgonzo			    at compile time.
46187517Sgonzo
47187517Sgonzo   The core of SSA-CCP is in ccp_visit_stmt and ccp_visit_phi_node:
48187517Sgonzo
49221257Sadrian   1- In ccp_visit_stmt, we are interested in assignments whose RHS
50187517Sgonzo      evaluates into a constant and conditional jumps whose predicate
51232847Sgonzo      evaluates into a boolean true or false.  When an assignment of
52232847Sgonzo      the form V_i = CONST is found, V_i's lattice value is set to
53188883Sgonzo      CONSTANT and CONST is associated with it.  This causes the
54188883Sgonzo      propagation engine to add all the SSA edges coming out the
55188883Sgonzo      assignment into the worklists, so that statements that use V_i
56188883Sgonzo      can be visited.
57188883Sgonzo
58188883Sgonzo      If the statement is a conditional with a constant predicate, we
59188883Sgonzo      mark the outgoing edges as executable or not executable
60187517Sgonzo      depending on the predicate's value.  This is then used when
61187517Sgonzo      visiting PHI nodes to know when a PHI argument can be ignored.
62212413Savg
63187517Sgonzo
64187517Sgonzo   2- In ccp_visit_phi_node, if all the PHI arguments evaluate to the
65187517Sgonzo      same constant C, then the LHS of the PHI is set to C.  This
66187517Sgonzo      evaluation is known as the "meet operation".  Since one of the
67187517Sgonzo      goals of this evaluation is to optimistically return constant
68187517Sgonzo      values as often as possible, it uses two main short cuts:
69187517Sgonzo
70187517Sgonzo      - If an argument is flowing in through a non-executable edge, it
71187517Sgonzo	is ignored.  This is useful in cases like this:
72232847Sgonzo
73187517Sgonzo			if (PRED)
74187517Sgonzo			  a_9 = 3;
75187517Sgonzo			else
76187517Sgonzo			  a_10 = 100;
77187517Sgonzo			a_11 = PHI (a_9, a_10)
78187517Sgonzo
79187517Sgonzo	If PRED is known to always evaluate to false, then we can
80187517Sgonzo	assume that a_11 will always take its value from a_10, meaning
81192822Sgonzo	that instead of consider it VARYING (a_9 and a_10 have
82192822Sgonzo	different values), we can consider it CONSTANT 100.
83187517Sgonzo
84192822Sgonzo      - If an argument has an UNDEFINED value, then it does not affect
85187517Sgonzo	the outcome of the meet operation.  If a variable V_i has an
86187517Sgonzo	UNDEFINED value, it means that either its defining statement
87187517Sgonzo	hasn't been visited yet or V_i has no defining statement, in
88187517Sgonzo	which case the original symbol 'V' is being used
89187517Sgonzo	uninitialized.  Since 'V' is a local variable, the compiler
90187517Sgonzo	may assume any initial value for it.
91187517Sgonzo
92192822Sgonzo
93192822Sgonzo   After propagation, every variable V_i that ends up with a lattice
94187517Sgonzo   value of CONSTANT will have the associated constant value in the
95187517Sgonzo   array CONST_VAL[i].VALUE.  That is fed into substitute_and_fold for
96192822Sgonzo   final substitution and folding.
97187517Sgonzo
98187517Sgonzo
99187517Sgonzo   Constant propagation in stores and loads (STORE-CCP)
100187517Sgonzo   ----------------------------------------------------
101187517Sgonzo
102187517Sgonzo   While CCP has all the logic to propagate constants in GIMPLE
103187517Sgonzo   registers, it is missing the ability to associate constants with
104187517Sgonzo   stores and loads (i.e., pointer dereferences, structures and
105187517Sgonzo   global/aliased variables).  We don't keep loads and stores in
106257338Snwhitehorn   SSA, but we do build a factored use-def web for them (in the
107187517Sgonzo   virtual operands).
108187517Sgonzo
109187517Sgonzo   For instance, consider the following code fragment:
110187517Sgonzo
111187517Sgonzo	  struct A a;
112187517Sgonzo	  const int B = 42;
113187517Sgonzo
114187517Sgonzo	  void foo (int i)
115187517Sgonzo	  {
116191837Sgonzo	    if (i > 10)
117191837Sgonzo	      a.a = 42;
118191837Sgonzo	    else
119191837Sgonzo	      {
120191837Sgonzo		a.b = 21;
121191837Sgonzo		a.a = a.b + 21;
122191837Sgonzo	      }
123191837Sgonzo
124191837Sgonzo	    if (a.a != B)
125191837Sgonzo	      never_executed ();
126187517Sgonzo	  }
127187517Sgonzo
128187517Sgonzo   We should be able to deduce that the predicate 'a.a != B' is always
129187517Sgonzo   false.  To achieve this, we associate constant values to the SSA
130187517Sgonzo   names in the V_MAY_DEF and V_MUST_DEF operands for each store.
131187517Sgonzo   Additionally, since we also glob partial loads/stores with the base
132187517Sgonzo   symbol, we also keep track of the memory reference where the
133187517Sgonzo   constant value was stored (in the MEM_REF field of PROP_VALUE_T).
134187517Sgonzo   For instance,
135187517Sgonzo
136187517Sgonzo        # a_5 = V_MAY_DEF <a_4>
137187517Sgonzo        a.a = 2;
138187517Sgonzo
139187517Sgonzo        # VUSE <a_5>
140187517Sgonzo        x_3 = a.b;
141232847Sgonzo
142187517Sgonzo   In the example above, CCP will associate value '2' with 'a_5', but
143187517Sgonzo   it would be wrong to replace the load from 'a.b' with '2', because
144187517Sgonzo   '2' had been stored into a.a.
145187517Sgonzo
146187517Sgonzo   To support STORE-CCP, it is necessary to add a new value to the
147187517Sgonzo   constant propagation lattice.  When evaluating a load for a memory
148187517Sgonzo   reference we can no longer assume a value of UNDEFINED if we
149187517Sgonzo   haven't seen a preceding store to the same memory location.
150187517Sgonzo   Consider, for instance global variables:
151232847Sgonzo
152232847Sgonzo   	int A;
153232847Sgonzo
154232847Sgonzo   	foo (int i)
155232847Sgonzo  	{
156232847Sgonzo	  if (i_3 > 10)
157187517Sgonzo	    A_4 = 3;
158187517Sgonzo          # A_5 = PHI (A_4, A_2);
159187517Sgonzo
160187517Sgonzo	  # VUSE <A_5>
161187517Sgonzo	  A.0_6 = A;
162187517Sgonzo
163187517Sgonzo	  return A.0_6;
164187517Sgonzo	}
165187517Sgonzo
166187517Sgonzo   The value of A_2 cannot be assumed to be UNDEFINED, as it may have
167187517Sgonzo   been defined outside of foo.  If we were to assume it UNDEFINED, we
168187517Sgonzo   would erroneously optimize the above into 'return 3;'.  Therefore,
169187517Sgonzo   when doing STORE-CCP, we introduce a fifth lattice value
170187517Sgonzo   (UNKNOWN_VAL), which overrides any other value when computing the
171187517Sgonzo   meet operation in PHI nodes.
172187517Sgonzo
173188883Sgonzo   Though STORE-CCP is not too expensive, it does have to do more work
174188883Sgonzo   than regular CCP, so it is only enabled at -O2.  Both regular CCP
175188883Sgonzo   and STORE-CCP use the exact same algorithm.  The only distinction
176191837Sgonzo   is that when doing STORE-CCP, the boolean variable DO_STORE_CCP is
177187517Sgonzo   set to true.  This affects the evaluation of statements and PHI
178187517Sgonzo   nodes.
179191837Sgonzo
180191837Sgonzo   References:
181188883Sgonzo
182188883Sgonzo     Constant propagation with conditional branches,
183187517Sgonzo     Wegman and Zadeck, ACM TOPLAS 13(2):181-210.
184187517Sgonzo
185187517Sgonzo     Building an Optimizing Compiler,
186187517Sgonzo     Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.
187187517Sgonzo
188187517Sgonzo     Advanced Compiler Design and Implementation,
189187517Sgonzo     Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6  */
190187517Sgonzo
191187517Sgonzo#include "config.h"
192187517Sgonzo#include "system.h"
193187517Sgonzo#include "coretypes.h"
194187517Sgonzo#include "tm.h"
195187517Sgonzo#include "tree.h"
196187517Sgonzo#include "flags.h"
197187517Sgonzo#include "rtl.h"
198187517Sgonzo#include "tm_p.h"
199187517Sgonzo#include "ggc.h"
200187517Sgonzo#include "basic-block.h"
201187517Sgonzo#include "output.h"
202187517Sgonzo#include "expr.h"
203187517Sgonzo#include "function.h"
204187517Sgonzo#include "diagnostic.h"
205188883Sgonzo#include "timevar.h"
206188883Sgonzo#include "tree-dump.h"
207188883Sgonzo#include "tree-flow.h"
208188883Sgonzo#include "tree-pass.h"
209187517Sgonzo#include "tree-ssa-propagate.h"
210187517Sgonzo#include "langhooks.h"
211187517Sgonzo#include "target.h"
212187517Sgonzo#include "toplev.h"
213187517Sgonzo
214187517Sgonzo
215191837Sgonzo/* Possible lattice values.  */
216191837Sgonzotypedef enum
217191837Sgonzo{
218187517Sgonzo  UNINITIALIZED = 0,
219187517Sgonzo  UNDEFINED,
220187517Sgonzo  UNKNOWN_VAL,
221187517Sgonzo  CONSTANT,
222187517Sgonzo  VARYING
223187517Sgonzo} ccp_lattice_t;
224187517Sgonzo
225187517Sgonzo/* Array of propagated constant values.  After propagation,
226187517Sgonzo   CONST_VAL[I].VALUE holds the constant value for SSA_NAME(I).  If
227187517Sgonzo   the constant is held in an SSA name representing a memory store
228187517Sgonzo   (i.e., a V_MAY_DEF or V_MUST_DEF), CONST_VAL[I].MEM_REF will
229187517Sgonzo   contain the actual memory reference used to store (i.e., the LHS of
230187517Sgonzo   the assignment doing the store).  */
231187517Sgonzostatic prop_value_t *const_val;
232187517Sgonzo
233187517Sgonzo/* True if we are also propagating constants in stores and loads.  */
234187517Sgonzostatic bool do_store_ccp;
235187517Sgonzo
236187517Sgonzo/* Dump constant propagation value VAL to file OUTF prefixed by PREFIX.  */
237187517Sgonzo
238187517Sgonzostatic void
239187517Sgonzodump_lattice_value (FILE *outf, const char *prefix, prop_value_t val)
240187517Sgonzo{
241187517Sgonzo  switch (val.lattice_val)
242187517Sgonzo    {
243187517Sgonzo    case UNINITIALIZED:
244187517Sgonzo      fprintf (outf, "%sUNINITIALIZED", prefix);
245187517Sgonzo      break;
246187517Sgonzo    case UNDEFINED:
247187517Sgonzo      fprintf (outf, "%sUNDEFINED", prefix);
248187517Sgonzo      break;
249187517Sgonzo    case VARYING:
250187517Sgonzo      fprintf (outf, "%sVARYING", prefix);
251187517Sgonzo      break;
252187517Sgonzo    case UNKNOWN_VAL:
253187517Sgonzo      fprintf (outf, "%sUNKNOWN_VAL", prefix);
254187517Sgonzo      break;
255187517Sgonzo    case CONSTANT:
256187517Sgonzo      fprintf (outf, "%sCONSTANT ", prefix);
257187517Sgonzo      print_generic_expr (outf, val.value, dump_flags);
258187517Sgonzo      break;
259187517Sgonzo    default:
260187517Sgonzo      gcc_unreachable ();
261187517Sgonzo    }
262187517Sgonzo}
263187517Sgonzo
264187517Sgonzo
265187517Sgonzo/* Print lattice value VAL to stderr.  */
266187517Sgonzo
267187517Sgonzovoid debug_lattice_value (prop_value_t val);
268187517Sgonzo
269187517Sgonzovoid
270187517Sgonzodebug_lattice_value (prop_value_t val)
271187517Sgonzo{
272187517Sgonzo  dump_lattice_value (stderr, "", val);
273187517Sgonzo  fprintf (stderr, "\n");
274187517Sgonzo}
275187517Sgonzo
276187517Sgonzo
277187517Sgonzo/* The regular is_gimple_min_invariant does a shallow test of the object.
278187517Sgonzo   It assumes that full gimplification has happened, or will happen on the
279187517Sgonzo   object.  For a value coming from DECL_INITIAL, this is not true, so we
280187517Sgonzo   have to be more strict ourselves.  */
281187517Sgonzo
282187517Sgonzostatic bool
283187517Sgonzoccp_decl_initial_min_invariant (tree t)
284187517Sgonzo{
285187517Sgonzo  if (!is_gimple_min_invariant (t))
286187517Sgonzo    return false;
287187517Sgonzo  if (TREE_CODE (t) == ADDR_EXPR)
288187517Sgonzo    {
289187517Sgonzo      /* Inline and unroll is_gimple_addressable.  */
290187517Sgonzo      while (1)
291187517Sgonzo	{
292187517Sgonzo	  t = TREE_OPERAND (t, 0);
293187517Sgonzo	  if (is_gimple_id (t))
294187517Sgonzo	    return true;
295187517Sgonzo	  if (!handled_component_p (t))
296187517Sgonzo	    return false;
297187517Sgonzo	}
298192822Sgonzo    }
299187517Sgonzo  return true;
300187517Sgonzo}
301187517Sgonzo
302199497Sgonzo
303199497Sgonzo/* Compute a default value for variable VAR and store it in the
304199497Sgonzo   CONST_VAL array.  The following rules are used to get default
305199497Sgonzo   values:
306199497Sgonzo
307199497Sgonzo   1- Global and static variables that are declared constant are
308199497Sgonzo      considered CONSTANT.
309187517Sgonzo
310187517Sgonzo   2- Any other value is considered UNDEFINED.  This is useful when
311187517Sgonzo      considering PHI nodes.  PHI arguments that are undefined do not
312187517Sgonzo      change the constant value of the PHI node, which allows for more
313199497Sgonzo      constants to be propagated.
314187517Sgonzo
315192822Sgonzo   3- If SSA_NAME_VALUE is set and it is a constant, its value is
316191837Sgonzo      used.
317187517Sgonzo
318187517Sgonzo   4- Variables defined by statements other than assignments and PHI
319187517Sgonzo      nodes are considered VARYING.
320187517Sgonzo
321187517Sgonzo   5- Variables that are not GIMPLE registers are considered
322187517Sgonzo      UNKNOWN_VAL, which is really a stronger version of UNDEFINED.
323187517Sgonzo      It's used to avoid the short circuit evaluation implied by
324187517Sgonzo      UNDEFINED in ccp_lattice_meet.  */
325187517Sgonzo
326187517Sgonzostatic prop_value_t
327187517Sgonzoget_default_value (tree var)
328187517Sgonzo{
329187517Sgonzo  tree sym = SSA_NAME_VAR (var);
330187517Sgonzo  prop_value_t val = { UNINITIALIZED, NULL_TREE, NULL_TREE };
331187517Sgonzo
332187517Sgonzo  if (!do_store_ccp && !is_gimple_reg (var))
333187517Sgonzo    {
334192822Sgonzo      /* Short circuit for regular CCP.  We are not interested in any
335187517Sgonzo	 non-register when DO_STORE_CCP is false.  */
336187517Sgonzo      val.lattice_val = VARYING;
337187517Sgonzo    }
338187517Sgonzo  else if (SSA_NAME_VALUE (var)
339187517Sgonzo	   && is_gimple_min_invariant (SSA_NAME_VALUE (var)))
340187517Sgonzo    {
341187517Sgonzo      val.lattice_val = CONSTANT;
342187517Sgonzo      val.value = SSA_NAME_VALUE (var);
343187517Sgonzo    }
344232847Sgonzo  else if (TREE_STATIC (sym)
345187517Sgonzo	   && TREE_READONLY (sym)
346187517Sgonzo	   && !MTAG_P (sym)
347187517Sgonzo	   && DECL_INITIAL (sym)
348187517Sgonzo	   && ccp_decl_initial_min_invariant (DECL_INITIAL (sym)))
349232847Sgonzo    {
350233104Sgonzo      /* Globals and static variables declared 'const' take their
351187517Sgonzo	 initial value.  */
352187517Sgonzo      val.lattice_val = CONSTANT;
353187517Sgonzo      val.value = DECL_INITIAL (sym);
354187517Sgonzo      val.mem_ref = sym;
355221257Sadrian    }
356221257Sadrian  else
357221257Sadrian    {
358221257Sadrian      tree stmt = SSA_NAME_DEF_STMT (var);
359221257Sadrian
360249119Sadrian      if (IS_EMPTY_STMT (stmt))
361249119Sadrian	{
362256174Sadrian	  /* Variables defined by an empty statement are those used
363256174Sadrian	     before being initialized.  If VAR is a local variable, we
364256174Sadrian	     can assume initially that it is UNDEFINED.  If we are
365263228Sadrian	     doing STORE-CCP, function arguments and non-register
366221257Sadrian	     variables are initially UNKNOWN_VAL, because we cannot
367263228Sadrian	     discard the value incoming from outside of this function
368221257Sadrian	     (see ccp_lattice_meet for details).  */
369221257Sadrian	  if (is_gimple_reg (sym) && TREE_CODE (sym) != PARM_DECL)
370221257Sadrian	    val.lattice_val = UNDEFINED;
371221257Sadrian	  else if (do_store_ccp)
372221257Sadrian	    val.lattice_val = UNKNOWN_VAL;
373221257Sadrian	  else
374187517Sgonzo	    val.lattice_val = VARYING;
375187517Sgonzo	}
376232847Sgonzo      else if (TREE_CODE (stmt) == MODIFY_EXPR
377233104Sgonzo	       || TREE_CODE (stmt) == PHI_NODE)
378233104Sgonzo	{
379233104Sgonzo	  /* Any other variable defined by an assignment or a PHI node
380233318Sgonzo	     is considered UNDEFINED (or UNKNOWN_VAL if VAR is not a
381233318Sgonzo	     GIMPLE register).  */
382233318Sgonzo	  val.lattice_val = is_gimple_reg (sym) ? UNDEFINED : UNKNOWN_VAL;
383232847Sgonzo	}
384232847Sgonzo      else
385232847Sgonzo	{
386232847Sgonzo	  /* Otherwise, VAR will never take on a constant value.  */
387191837Sgonzo	  val.lattice_val = VARYING;
388263228Sadrian	}
389199497Sgonzo    }
390187517Sgonzo
391187517Sgonzo  return val;
392187517Sgonzo}
393232847Sgonzo
394199497Sgonzo
395187517Sgonzo/* Get the constant value associated with variable VAR.  If
396187517Sgonzo   MAY_USE_DEFAULT_P is true, call get_default_value on variables that
397187517Sgonzo   have the lattice value UNINITIALIZED.  */
398187517Sgonzo
399187517Sgonzostatic prop_value_t *
400187517Sgonzoget_value (tree var, bool may_use_default_p)
401187517Sgonzo{
402187517Sgonzo  prop_value_t *val = &const_val[SSA_NAME_VERSION (var)];
403187517Sgonzo  if (may_use_default_p && val->lattice_val == UNINITIALIZED)
404187517Sgonzo    *val = get_default_value (var);
405187517Sgonzo
406187517Sgonzo  return val;
407187517Sgonzo}
408187517Sgonzo
409187517Sgonzo
410187517Sgonzo/* Set the value for variable VAR to NEW_VAL.  Return true if the new
411187517Sgonzo   value is different from VAR's previous value.  */
412187517Sgonzo
413187517Sgonzostatic bool
414187517Sgonzoset_lattice_value (tree var, prop_value_t new_val)
415187517Sgonzo{
416187517Sgonzo  prop_value_t *old_val = get_value (var, false);
417187517Sgonzo
418187517Sgonzo  /* Lattice transitions must always be monotonically increasing in
419187517Sgonzo     value.  We allow two exceptions:
420187517Sgonzo
421187517Sgonzo     1- If *OLD_VAL and NEW_VAL are the same, return false to
422187517Sgonzo	inform the caller that this was a non-transition.
423187517Sgonzo
424187517Sgonzo     2- If we are doing store-ccp (i.e., DOING_STORE_CCP is true),
425187517Sgonzo	allow CONSTANT->UNKNOWN_VAL.  The UNKNOWN_VAL state is a
426187517Sgonzo	special type of UNDEFINED state which prevents the short
427187517Sgonzo	circuit evaluation of PHI arguments (see ccp_visit_phi_node
428187517Sgonzo	and ccp_lattice_meet).  */
429187517Sgonzo  gcc_assert (old_val->lattice_val <= new_val.lattice_val
430187517Sgonzo              || (old_val->lattice_val == new_val.lattice_val
431187517Sgonzo		  && old_val->value == new_val.value
432187517Sgonzo		  && old_val->mem_ref == new_val.mem_ref)
433187517Sgonzo	      || (do_store_ccp
434187517Sgonzo		  && old_val->lattice_val == CONSTANT
435187517Sgonzo		  && new_val.lattice_val == UNKNOWN_VAL));
436187517Sgonzo
437187517Sgonzo  if (old_val->lattice_val != new_val.lattice_val)
438187517Sgonzo    {
439187517Sgonzo      if (dump_file && (dump_flags & TDF_DETAILS))
440187517Sgonzo	{
441187517Sgonzo	  dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
442187517Sgonzo	  fprintf (dump_file, ".  %sdding SSA edges to worklist.\n",
443187517Sgonzo	           new_val.lattice_val != UNDEFINED ? "A" : "Not a");
444212413Savg	}
445187517Sgonzo
446187517Sgonzo      *old_val = new_val;
447187517Sgonzo
448187517Sgonzo      /* Transitions UNINITIALIZED -> UNDEFINED are never interesting
449187517Sgonzo	 for propagation purposes.  In these cases return false to
450187517Sgonzo	 avoid doing useless work.  */
451187517Sgonzo      return (new_val.lattice_val != UNDEFINED);
452187517Sgonzo    }
453187517Sgonzo
454187517Sgonzo  return false;
455187517Sgonzo}
456187517Sgonzo
457187517Sgonzo
458187517Sgonzo/* Return the likely CCP lattice value for STMT.
459187517Sgonzo
460187517Sgonzo   If STMT has no operands, then return CONSTANT.
461187517Sgonzo
462187517Sgonzo   Else if any operands of STMT are undefined, then return UNDEFINED.
463187517Sgonzo
464187517Sgonzo   Else if any operands of STMT are constants, then return CONSTANT.
465187517Sgonzo
466187517Sgonzo   Else return VARYING.  */
467187517Sgonzo
468187517Sgonzostatic ccp_lattice_t
469187517Sgonzolikely_value (tree stmt)
470187517Sgonzo{
471187517Sgonzo  bool found_constant;
472187517Sgonzo  stmt_ann_t ann;
473187517Sgonzo  tree use;
474187517Sgonzo  ssa_op_iter iter;
475187517Sgonzo
476187517Sgonzo  ann = stmt_ann (stmt);
477187517Sgonzo
478187517Sgonzo  /* If the statement has volatile operands, it won't fold to a
479187517Sgonzo     constant value.  */
480187517Sgonzo  if (ann->has_volatile_ops)
481187517Sgonzo    return VARYING;
482187517Sgonzo
483187517Sgonzo  /* If we are not doing store-ccp, statements with loads
484187517Sgonzo     and/or stores will never fold into a constant.  */
485187517Sgonzo  if (!do_store_ccp
486187517Sgonzo      && !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
487187517Sgonzo    return VARYING;
488187517Sgonzo
489187517Sgonzo
490187517Sgonzo  /* A CALL_EXPR is assumed to be varying.  NOTE: This may be overly
491187517Sgonzo     conservative, in the presence of const and pure calls.  */
492187517Sgonzo  if (get_call_expr_in (stmt) != NULL_TREE)
493187517Sgonzo    return VARYING;
494187517Sgonzo
495227843Smarius  /* Anything other than assignments and conditional jumps are not
496187517Sgonzo     interesting for CCP.  */
497187517Sgonzo  if (TREE_CODE (stmt) != MODIFY_EXPR
498187517Sgonzo      && TREE_CODE (stmt) != COND_EXPR
499187517Sgonzo      && TREE_CODE (stmt) != SWITCH_EXPR)
500187517Sgonzo    return VARYING;
501187517Sgonzo
502187517Sgonzo  if (is_gimple_min_invariant (get_rhs (stmt)))
503187517Sgonzo    return CONSTANT;
504187517Sgonzo
505187517Sgonzo  found_constant = false;
506  FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
507    {
508      prop_value_t *val = get_value (use, true);
509
510      if (val->lattice_val == VARYING)
511	return VARYING;
512
513      if (val->lattice_val == UNKNOWN_VAL)
514	{
515	  /* UNKNOWN_VAL is invalid when not doing STORE-CCP.  */
516	  gcc_assert (do_store_ccp);
517	  return UNKNOWN_VAL;
518	}
519
520      if (val->lattice_val == CONSTANT)
521	found_constant = true;
522    }
523
524  if (found_constant
525      || ZERO_SSA_OPERANDS (stmt, SSA_OP_USE)
526      || ZERO_SSA_OPERANDS (stmt, SSA_OP_VUSE))
527    return CONSTANT;
528
529  return UNDEFINED;
530}
531
532
533/* Initialize local data structures for CCP.  */
534
535static void
536ccp_initialize (void)
537{
538  basic_block bb;
539
540  const_val = XNEWVEC (prop_value_t, num_ssa_names);
541  memset (const_val, 0, num_ssa_names * sizeof (*const_val));
542
543  /* Initialize simulation flags for PHI nodes and statements.  */
544  FOR_EACH_BB (bb)
545    {
546      block_stmt_iterator i;
547
548      for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
549        {
550	  bool is_varying = false;
551	  tree stmt = bsi_stmt (i);
552
553	  if (likely_value (stmt) == VARYING)
554
555	    {
556	      tree def;
557	      ssa_op_iter iter;
558
559	      /* If the statement will not produce a constant, mark
560		 all its outputs VARYING.  */
561	      FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
562		get_value (def, false)->lattice_val = VARYING;
563
564	      /* Never mark conditional jumps with DONT_SIMULATE_AGAIN,
565		 otherwise the propagator will never add the outgoing
566		 control edges.  */
567	      if (TREE_CODE (stmt) != COND_EXPR
568		  && TREE_CODE (stmt) != SWITCH_EXPR)
569		is_varying = true;
570	    }
571
572	  DONT_SIMULATE_AGAIN (stmt) = is_varying;
573	}
574    }
575
576  /* Now process PHI nodes.  */
577  FOR_EACH_BB (bb)
578    {
579      tree phi;
580
581      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
582	{
583	  int i;
584	  tree arg;
585	  prop_value_t *val = get_value (PHI_RESULT (phi), false);
586
587	  for (i = 0; i < PHI_NUM_ARGS (phi); i++)
588	    {
589	      arg = PHI_ARG_DEF (phi, i);
590
591	      if (TREE_CODE (arg) == SSA_NAME
592		  && get_value (arg, false)->lattice_val == VARYING)
593		{
594		  val->lattice_val = VARYING;
595		  break;
596		}
597	    }
598
599	  DONT_SIMULATE_AGAIN (phi) = (val->lattice_val == VARYING);
600	}
601    }
602}
603
604
605/* Do final substitution of propagated values, cleanup the flowgraph and
606   free allocated storage.  */
607
608static void
609ccp_finalize (void)
610{
611  /* Perform substitutions based on the known constant values.  */
612  substitute_and_fold (const_val, false);
613
614  free (const_val);
615}
616
617
618/* Compute the meet operator between *VAL1 and *VAL2.  Store the result
619   in VAL1.
620
621   		any  M UNDEFINED   = any
622		any  M UNKNOWN_VAL = UNKNOWN_VAL
623		any  M VARYING     = VARYING
624		Ci   M Cj	   = Ci		if (i == j)
625		Ci   M Cj	   = VARYING	if (i != j)
626
627   Lattice values UNKNOWN_VAL and UNDEFINED are similar but have
628   different semantics at PHI nodes.  Both values imply that we don't
629   know whether the variable is constant or not.  However, UNKNOWN_VAL
630   values override all others.  For instance, suppose that A is a
631   global variable:
632
633		+------+
634		|      |
635		|     / \
636		|    /   \
637		|   |  A_1 = 4
638		|    \   /
639		|     \ /
640		| A_3 = PHI (A_2, A_1)
641		| ... = A_3
642		|    |
643		+----+
644
645   If the edge into A_2 is not executable, the first visit to A_3 will
646   yield the constant 4.  But the second visit to A_3 will be with A_2
647   in state UNKNOWN_VAL.  We can no longer conclude that A_3 is 4
648   because A_2 may have been set in another function.  If we had used
649   the lattice value UNDEFINED, we would have had wrongly concluded
650   that A_3 is 4.  */
651
652
653static void
654ccp_lattice_meet (prop_value_t *val1, prop_value_t *val2)
655{
656  if (val1->lattice_val == UNDEFINED)
657    {
658      /* UNDEFINED M any = any   */
659      *val1 = *val2;
660    }
661  else if (val2->lattice_val == UNDEFINED)
662    {
663      /* any M UNDEFINED = any
664         Nothing to do.  VAL1 already contains the value we want.  */
665      ;
666    }
667  else if (val1->lattice_val == UNKNOWN_VAL
668           || val2->lattice_val == UNKNOWN_VAL)
669    {
670      /* UNKNOWN_VAL values are invalid if we are not doing STORE-CCP.  */
671      gcc_assert (do_store_ccp);
672
673      /* any M UNKNOWN_VAL = UNKNOWN_VAL.  */
674      val1->lattice_val = UNKNOWN_VAL;
675      val1->value = NULL_TREE;
676      val1->mem_ref = NULL_TREE;
677    }
678  else if (val1->lattice_val == VARYING
679           || val2->lattice_val == VARYING)
680    {
681      /* any M VARYING = VARYING.  */
682      val1->lattice_val = VARYING;
683      val1->value = NULL_TREE;
684      val1->mem_ref = NULL_TREE;
685    }
686  else if (val1->lattice_val == CONSTANT
687	   && val2->lattice_val == CONSTANT
688	   && simple_cst_equal (val1->value, val2->value) == 1
689	   && (!do_store_ccp
690	       || (val1->mem_ref && val2->mem_ref
691		   && operand_equal_p (val1->mem_ref, val2->mem_ref, 0))))
692    {
693      /* Ci M Cj = Ci		if (i == j)
694	 Ci M Cj = VARYING	if (i != j)
695
696         If these two values come from memory stores, make sure that
697	 they come from the same memory reference.  */
698      val1->lattice_val = CONSTANT;
699      val1->value = val1->value;
700      val1->mem_ref = val1->mem_ref;
701    }
702  else
703    {
704      /* Any other combination is VARYING.  */
705      val1->lattice_val = VARYING;
706      val1->value = NULL_TREE;
707      val1->mem_ref = NULL_TREE;
708    }
709}
710
711
712/* Loop through the PHI_NODE's parameters for BLOCK and compare their
713   lattice values to determine PHI_NODE's lattice value.  The value of a
714   PHI node is determined calling ccp_lattice_meet with all the arguments
715   of the PHI node that are incoming via executable edges.  */
716
717static enum ssa_prop_result
718ccp_visit_phi_node (tree phi)
719{
720  int i;
721  prop_value_t *old_val, new_val;
722
723  if (dump_file && (dump_flags & TDF_DETAILS))
724    {
725      fprintf (dump_file, "\nVisiting PHI node: ");
726      print_generic_expr (dump_file, phi, dump_flags);
727    }
728
729  old_val = get_value (PHI_RESULT (phi), false);
730  switch (old_val->lattice_val)
731    {
732    case VARYING:
733      return SSA_PROP_VARYING;
734
735    case CONSTANT:
736      new_val = *old_val;
737      break;
738
739    case UNKNOWN_VAL:
740      /* To avoid the default value of UNKNOWN_VAL overriding
741         that of its possible constant arguments, temporarily
742	 set the PHI node's default lattice value to be
743	 UNDEFINED.  If the PHI node's old value was UNKNOWN_VAL and
744	 the new value is UNDEFINED, then we prevent the invalid
745	 transition by not calling set_lattice_value.  */
746      gcc_assert (do_store_ccp);
747
748      /* FALLTHRU  */
749
750    case UNDEFINED:
751    case UNINITIALIZED:
752      new_val.lattice_val = UNDEFINED;
753      new_val.value = NULL_TREE;
754      new_val.mem_ref = NULL_TREE;
755      break;
756
757    default:
758      gcc_unreachable ();
759    }
760
761  for (i = 0; i < PHI_NUM_ARGS (phi); i++)
762    {
763      /* Compute the meet operator over all the PHI arguments flowing
764	 through executable edges.  */
765      edge e = PHI_ARG_EDGE (phi, i);
766
767      if (dump_file && (dump_flags & TDF_DETAILS))
768	{
769	  fprintf (dump_file,
770	      "\n    Argument #%d (%d -> %d %sexecutable)\n",
771	      i, e->src->index, e->dest->index,
772	      (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
773	}
774
775      /* If the incoming edge is executable, Compute the meet operator for
776	 the existing value of the PHI node and the current PHI argument.  */
777      if (e->flags & EDGE_EXECUTABLE)
778	{
779	  tree arg = PHI_ARG_DEF (phi, i);
780	  prop_value_t arg_val;
781
782	  if (is_gimple_min_invariant (arg))
783	    {
784	      arg_val.lattice_val = CONSTANT;
785	      arg_val.value = arg;
786	      arg_val.mem_ref = NULL_TREE;
787	    }
788	  else
789	    arg_val = *(get_value (arg, true));
790
791	  ccp_lattice_meet (&new_val, &arg_val);
792
793	  if (dump_file && (dump_flags & TDF_DETAILS))
794	    {
795	      fprintf (dump_file, "\t");
796	      print_generic_expr (dump_file, arg, dump_flags);
797	      dump_lattice_value (dump_file, "\tValue: ", arg_val);
798	      fprintf (dump_file, "\n");
799	    }
800
801	  if (new_val.lattice_val == VARYING)
802	    break;
803	}
804    }
805
806  if (dump_file && (dump_flags & TDF_DETAILS))
807    {
808      dump_lattice_value (dump_file, "\n    PHI node value: ", new_val);
809      fprintf (dump_file, "\n\n");
810    }
811
812  /* Check for an invalid change from UNKNOWN_VAL to UNDEFINED.  */
813  if (do_store_ccp
814      && old_val->lattice_val == UNKNOWN_VAL
815      && new_val.lattice_val == UNDEFINED)
816    return SSA_PROP_NOT_INTERESTING;
817
818  /* Otherwise, make the transition to the new value.  */
819  if (set_lattice_value (PHI_RESULT (phi), new_val))
820    {
821      if (new_val.lattice_val == VARYING)
822	return SSA_PROP_VARYING;
823      else
824	return SSA_PROP_INTERESTING;
825    }
826  else
827    return SSA_PROP_NOT_INTERESTING;
828}
829
830
831/* CCP specific front-end to the non-destructive constant folding
832   routines.
833
834   Attempt to simplify the RHS of STMT knowing that one or more
835   operands are constants.
836
837   If simplification is possible, return the simplified RHS,
838   otherwise return the original RHS.  */
839
840static tree
841ccp_fold (tree stmt)
842{
843  tree rhs = get_rhs (stmt);
844  enum tree_code code = TREE_CODE (rhs);
845  enum tree_code_class kind = TREE_CODE_CLASS (code);
846  tree retval = NULL_TREE;
847
848  if (TREE_CODE (rhs) == SSA_NAME)
849    {
850      /* If the RHS is an SSA_NAME, return its known constant value,
851	 if any.  */
852      return get_value (rhs, true)->value;
853    }
854  else if (do_store_ccp && stmt_makes_single_load (stmt))
855    {
856      /* If the RHS is a memory load, see if the VUSEs associated with
857	 it are a valid constant for that memory load.  */
858      prop_value_t *val = get_value_loaded_by (stmt, const_val);
859      if (val && val->mem_ref)
860	{
861	  if (operand_equal_p (val->mem_ref, rhs, 0))
862	    return val->value;
863
864	  /* If RHS is extracting REALPART_EXPR or IMAGPART_EXPR of a
865	     complex type with a known constant value, return it.  */
866	  if ((TREE_CODE (rhs) == REALPART_EXPR
867	       || TREE_CODE (rhs) == IMAGPART_EXPR)
868	      && operand_equal_p (val->mem_ref, TREE_OPERAND (rhs, 0), 0))
869	    return fold_build1 (TREE_CODE (rhs), TREE_TYPE (rhs), val->value);
870	}
871      return NULL_TREE;
872    }
873
874  /* Unary operators.  Note that we know the single operand must
875     be a constant.  So this should almost always return a
876     simplified RHS.  */
877  if (kind == tcc_unary)
878    {
879      /* Handle unary operators which can appear in GIMPLE form.  */
880      tree op0 = TREE_OPERAND (rhs, 0);
881
882      /* Simplify the operand down to a constant.  */
883      if (TREE_CODE (op0) == SSA_NAME)
884	{
885	  prop_value_t *val = get_value (op0, true);
886	  if (val->lattice_val == CONSTANT)
887	    op0 = get_value (op0, true)->value;
888	}
889
890      if ((code == NOP_EXPR || code == CONVERT_EXPR)
891	  && tree_ssa_useless_type_conversion_1 (TREE_TYPE (rhs),
892		  				 TREE_TYPE (op0)))
893	return op0;
894      return fold_unary (code, TREE_TYPE (rhs), op0);
895    }
896
897  /* Binary and comparison operators.  We know one or both of the
898     operands are constants.  */
899  else if (kind == tcc_binary
900           || kind == tcc_comparison
901           || code == TRUTH_AND_EXPR
902           || code == TRUTH_OR_EXPR
903           || code == TRUTH_XOR_EXPR)
904    {
905      /* Handle binary and comparison operators that can appear in
906         GIMPLE form.  */
907      tree op0 = TREE_OPERAND (rhs, 0);
908      tree op1 = TREE_OPERAND (rhs, 1);
909
910      /* Simplify the operands down to constants when appropriate.  */
911      if (TREE_CODE (op0) == SSA_NAME)
912	{
913	  prop_value_t *val = get_value (op0, true);
914	  if (val->lattice_val == CONSTANT)
915	    op0 = val->value;
916	}
917
918      if (TREE_CODE (op1) == SSA_NAME)
919	{
920	  prop_value_t *val = get_value (op1, true);
921	  if (val->lattice_val == CONSTANT)
922	    op1 = val->value;
923	}
924
925      return fold_binary (code, TREE_TYPE (rhs), op0, op1);
926    }
927
928  /* We may be able to fold away calls to builtin functions if their
929     arguments are constants.  */
930  else if (code == CALL_EXPR
931	   && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
932	   && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0))
933	       == FUNCTION_DECL)
934	   && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (rhs, 0), 0)))
935    {
936      if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_USE))
937	{
938	  tree *orig, var;
939	  tree fndecl, arglist;
940	  size_t i = 0;
941	  ssa_op_iter iter;
942	  use_operand_p var_p;
943
944	  /* Preserve the original values of every operand.  */
945	  orig = XNEWVEC (tree,  NUM_SSA_OPERANDS (stmt, SSA_OP_USE));
946	  FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
947	    orig[i++] = var;
948
949	  /* Substitute operands with their values and try to fold.  */
950	  replace_uses_in (stmt, NULL, const_val);
951	  fndecl = get_callee_fndecl (rhs);
952	  arglist = TREE_OPERAND (rhs, 1);
953	  retval = fold_builtin (fndecl, arglist, false);
954
955	  /* Restore operands to their original form.  */
956	  i = 0;
957	  FOR_EACH_SSA_USE_OPERAND (var_p, stmt, iter, SSA_OP_USE)
958	    SET_USE (var_p, orig[i++]);
959	  free (orig);
960	}
961    }
962  else
963    return rhs;
964
965  /* If we got a simplified form, see if we need to convert its type.  */
966  if (retval)
967    return fold_convert (TREE_TYPE (rhs), retval);
968
969  /* No simplification was possible.  */
970  return rhs;
971}
972
973
974/* Return the tree representing the element referenced by T if T is an
975   ARRAY_REF or COMPONENT_REF into constant aggregates.  Return
976   NULL_TREE otherwise.  */
977
978static tree
979fold_const_aggregate_ref (tree t)
980{
981  prop_value_t *value;
982  tree base, ctor, idx, field;
983  unsigned HOST_WIDE_INT cnt;
984  tree cfield, cval;
985
986  switch (TREE_CODE (t))
987    {
988    case ARRAY_REF:
989      /* Get a CONSTRUCTOR.  If BASE is a VAR_DECL, get its
990	 DECL_INITIAL.  If BASE is a nested reference into another
991	 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
992	 the inner reference.  */
993      base = TREE_OPERAND (t, 0);
994      switch (TREE_CODE (base))
995	{
996	case VAR_DECL:
997	  if (!TREE_READONLY (base)
998	      || TREE_CODE (TREE_TYPE (base)) != ARRAY_TYPE
999	      || !targetm.binds_local_p (base))
1000	    return NULL_TREE;
1001
1002	  ctor = DECL_INITIAL (base);
1003	  break;
1004
1005	case ARRAY_REF:
1006	case COMPONENT_REF:
1007	  ctor = fold_const_aggregate_ref (base);
1008	  break;
1009
1010	default:
1011	  return NULL_TREE;
1012	}
1013
1014      if (ctor == NULL_TREE
1015	  || (TREE_CODE (ctor) != CONSTRUCTOR
1016	      && TREE_CODE (ctor) != STRING_CST)
1017	  || !TREE_STATIC (ctor))
1018	return NULL_TREE;
1019
1020      /* Get the index.  If we have an SSA_NAME, try to resolve it
1021	 with the current lattice value for the SSA_NAME.  */
1022      idx = TREE_OPERAND (t, 1);
1023      switch (TREE_CODE (idx))
1024	{
1025	case SSA_NAME:
1026	  if ((value = get_value (idx, true))
1027	      && value->lattice_val == CONSTANT
1028	      && TREE_CODE (value->value) == INTEGER_CST)
1029	    idx = value->value;
1030	  else
1031	    return NULL_TREE;
1032	  break;
1033
1034	case INTEGER_CST:
1035	  break;
1036
1037	default:
1038	  return NULL_TREE;
1039	}
1040
1041      /* Fold read from constant string.  */
1042      if (TREE_CODE (ctor) == STRING_CST)
1043	{
1044	  if ((TYPE_MODE (TREE_TYPE (t))
1045	       == TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
1046	      && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor))))
1047	          == MODE_INT)
1048	      && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (ctor)))) == 1
1049	      && compare_tree_int (idx, TREE_STRING_LENGTH (ctor)) < 0)
1050	    return build_int_cst (TREE_TYPE (t), (TREE_STRING_POINTER (ctor)
1051					          [TREE_INT_CST_LOW (idx)]));
1052	  return NULL_TREE;
1053	}
1054
1055      /* Whoo-hoo!  I'll fold ya baby.  Yeah!  */
1056      FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
1057	if (tree_int_cst_equal (cfield, idx))
1058	  return cval;
1059      break;
1060
1061    case COMPONENT_REF:
1062      /* Get a CONSTRUCTOR.  If BASE is a VAR_DECL, get its
1063	 DECL_INITIAL.  If BASE is a nested reference into another
1064	 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
1065	 the inner reference.  */
1066      base = TREE_OPERAND (t, 0);
1067      switch (TREE_CODE (base))
1068	{
1069	case VAR_DECL:
1070	  if (!TREE_READONLY (base)
1071	      || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1072	      || !targetm.binds_local_p (base))
1073	    return NULL_TREE;
1074
1075	  ctor = DECL_INITIAL (base);
1076	  break;
1077
1078	case ARRAY_REF:
1079	case COMPONENT_REF:
1080	  ctor = fold_const_aggregate_ref (base);
1081	  break;
1082
1083	default:
1084	  return NULL_TREE;
1085	}
1086
1087      if (ctor == NULL_TREE
1088	  || TREE_CODE (ctor) != CONSTRUCTOR
1089	  || !TREE_STATIC (ctor))
1090	return NULL_TREE;
1091
1092      field = TREE_OPERAND (t, 1);
1093
1094      FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
1095	if (cfield == field
1096	    /* FIXME: Handle bit-fields.  */
1097	    && ! DECL_BIT_FIELD (cfield))
1098	  return cval;
1099      break;
1100
1101    case REALPART_EXPR:
1102    case IMAGPART_EXPR:
1103      {
1104	tree c = fold_const_aggregate_ref (TREE_OPERAND (t, 0));
1105	if (c && TREE_CODE (c) == COMPLEX_CST)
1106	  return fold_build1 (TREE_CODE (t), TREE_TYPE (t), c);
1107	break;
1108      }
1109
1110    default:
1111      break;
1112    }
1113
1114  return NULL_TREE;
1115}
1116
1117/* Evaluate statement STMT.  */
1118
1119static prop_value_t
1120evaluate_stmt (tree stmt)
1121{
1122  prop_value_t val;
1123  tree simplified = NULL_TREE;
1124  ccp_lattice_t likelyvalue = likely_value (stmt);
1125  bool is_constant;
1126
1127  val.mem_ref = NULL_TREE;
1128
1129  fold_defer_overflow_warnings ();
1130
1131  /* If the statement is likely to have a CONSTANT result, then try
1132     to fold the statement to determine the constant value.  */
1133  if (likelyvalue == CONSTANT)
1134    simplified = ccp_fold (stmt);
1135  /* If the statement is likely to have a VARYING result, then do not
1136     bother folding the statement.  */
1137  if (likelyvalue == VARYING)
1138    simplified = get_rhs (stmt);
1139  /* If the statement is an ARRAY_REF or COMPONENT_REF into constant
1140     aggregates, extract the referenced constant.  Otherwise the
1141     statement is likely to have an UNDEFINED value, and there will be
1142     nothing to do.  Note that fold_const_aggregate_ref returns
1143     NULL_TREE if the first case does not match.  */
1144  else if (!simplified)
1145    simplified = fold_const_aggregate_ref (get_rhs (stmt));
1146
1147  is_constant = simplified && is_gimple_min_invariant (simplified);
1148
1149  fold_undefer_overflow_warnings (is_constant, stmt, 0);
1150
1151  if (is_constant)
1152    {
1153      /* The statement produced a constant value.  */
1154      val.lattice_val = CONSTANT;
1155      val.value = simplified;
1156    }
1157  else
1158    {
1159      /* The statement produced a nonconstant value.  If the statement
1160	 had UNDEFINED operands, then the result of the statement
1161	 should be UNDEFINED.  Otherwise, the statement is VARYING.  */
1162      if (likelyvalue == UNDEFINED || likelyvalue == UNKNOWN_VAL)
1163	val.lattice_val = likelyvalue;
1164      else
1165	val.lattice_val = VARYING;
1166
1167      val.value = NULL_TREE;
1168    }
1169
1170  return val;
1171}
1172
1173
1174/* Visit the assignment statement STMT.  Set the value of its LHS to the
1175   value computed by the RHS and store LHS in *OUTPUT_P.  If STMT
1176   creates virtual definitions, set the value of each new name to that
1177   of the RHS (if we can derive a constant out of the RHS).  */
1178
1179static enum ssa_prop_result
1180visit_assignment (tree stmt, tree *output_p)
1181{
1182  prop_value_t val;
1183  tree lhs, rhs;
1184  enum ssa_prop_result retval;
1185
1186  lhs = TREE_OPERAND (stmt, 0);
1187  rhs = TREE_OPERAND (stmt, 1);
1188
1189  if (TREE_CODE (rhs) == SSA_NAME)
1190    {
1191      /* For a simple copy operation, we copy the lattice values.  */
1192      prop_value_t *nval = get_value (rhs, true);
1193      val = *nval;
1194    }
1195  else if (do_store_ccp && stmt_makes_single_load (stmt))
1196    {
1197      /* Same as above, but the RHS is not a gimple register and yet
1198         has a known VUSE.  If STMT is loading from the same memory
1199	 location that created the SSA_NAMEs for the virtual operands,
1200	 we can propagate the value on the RHS.  */
1201      prop_value_t *nval = get_value_loaded_by (stmt, const_val);
1202
1203      if (nval && nval->mem_ref
1204	  && operand_equal_p (nval->mem_ref, rhs, 0))
1205	val = *nval;
1206      else
1207	val = evaluate_stmt (stmt);
1208    }
1209  else
1210    /* Evaluate the statement.  */
1211      val = evaluate_stmt (stmt);
1212
1213  /* If the original LHS was a VIEW_CONVERT_EXPR, modify the constant
1214     value to be a VIEW_CONVERT_EXPR of the old constant value.
1215
1216     ??? Also, if this was a definition of a bitfield, we need to widen
1217     the constant value into the type of the destination variable.  This
1218     should not be necessary if GCC represented bitfields properly.  */
1219  {
1220    tree orig_lhs = TREE_OPERAND (stmt, 0);
1221
1222    if (TREE_CODE (orig_lhs) == VIEW_CONVERT_EXPR
1223	&& val.lattice_val == CONSTANT)
1224      {
1225	tree w = fold_unary (VIEW_CONVERT_EXPR,
1226			     TREE_TYPE (TREE_OPERAND (orig_lhs, 0)),
1227			     val.value);
1228
1229	orig_lhs = TREE_OPERAND (orig_lhs, 0);
1230	if (w && is_gimple_min_invariant (w))
1231	  val.value = w;
1232	else
1233	  {
1234	    val.lattice_val = VARYING;
1235	    val.value = NULL;
1236	  }
1237      }
1238
1239    if (val.lattice_val == CONSTANT
1240	&& TREE_CODE (orig_lhs) == COMPONENT_REF
1241	&& DECL_BIT_FIELD (TREE_OPERAND (orig_lhs, 1)))
1242      {
1243	tree w = widen_bitfield (val.value, TREE_OPERAND (orig_lhs, 1),
1244				 orig_lhs);
1245
1246	if (w && is_gimple_min_invariant (w))
1247	  val.value = w;
1248	else
1249	  {
1250	    val.lattice_val = VARYING;
1251	    val.value = NULL_TREE;
1252	    val.mem_ref = NULL_TREE;
1253	  }
1254      }
1255  }
1256
1257  retval = SSA_PROP_NOT_INTERESTING;
1258
1259  /* Set the lattice value of the statement's output.  */
1260  if (TREE_CODE (lhs) == SSA_NAME)
1261    {
1262      /* If STMT is an assignment to an SSA_NAME, we only have one
1263	 value to set.  */
1264      if (set_lattice_value (lhs, val))
1265	{
1266	  *output_p = lhs;
1267	  if (val.lattice_val == VARYING)
1268	    retval = SSA_PROP_VARYING;
1269	  else
1270	    retval = SSA_PROP_INTERESTING;
1271	}
1272    }
1273  else if (do_store_ccp && stmt_makes_single_store (stmt))
1274    {
1275      /* Otherwise, set the names in V_MAY_DEF/V_MUST_DEF operands
1276	 to the new constant value and mark the LHS as the memory
1277	 reference associated with VAL.  */
1278      ssa_op_iter i;
1279      tree vdef;
1280      bool changed;
1281
1282      /* Stores cannot take on an UNDEFINED value.  */
1283      if (val.lattice_val == UNDEFINED)
1284	val.lattice_val = UNKNOWN_VAL;
1285
1286      /* Mark VAL as stored in the LHS of this assignment.  */
1287      val.mem_ref = lhs;
1288
1289      /* Set the value of every VDEF to VAL.  */
1290      changed = false;
1291      FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, i, SSA_OP_VIRTUAL_DEFS)
1292	changed |= set_lattice_value (vdef, val);
1293
1294      /* Note that for propagation purposes, we are only interested in
1295	 visiting statements that load the exact same memory reference
1296	 stored here.  Those statements will have the exact same list
1297	 of virtual uses, so it is enough to set the output of this
1298	 statement to be its first virtual definition.  */
1299      *output_p = first_vdef (stmt);
1300      if (changed)
1301	{
1302	  if (val.lattice_val == VARYING)
1303	    retval = SSA_PROP_VARYING;
1304	  else
1305	    retval = SSA_PROP_INTERESTING;
1306	}
1307    }
1308
1309  return retval;
1310}
1311
1312
1313/* Visit the conditional statement STMT.  Return SSA_PROP_INTERESTING
1314   if it can determine which edge will be taken.  Otherwise, return
1315   SSA_PROP_VARYING.  */
1316
1317static enum ssa_prop_result
1318visit_cond_stmt (tree stmt, edge *taken_edge_p)
1319{
1320  prop_value_t val;
1321  basic_block block;
1322
1323  block = bb_for_stmt (stmt);
1324  val = evaluate_stmt (stmt);
1325
1326  /* Find which edge out of the conditional block will be taken and add it
1327     to the worklist.  If no single edge can be determined statically,
1328     return SSA_PROP_VARYING to feed all the outgoing edges to the
1329     propagation engine.  */
1330  *taken_edge_p = val.value ? find_taken_edge (block, val.value) : 0;
1331  if (*taken_edge_p)
1332    return SSA_PROP_INTERESTING;
1333  else
1334    return SSA_PROP_VARYING;
1335}
1336
1337
1338/* Evaluate statement STMT.  If the statement produces an output value and
1339   its evaluation changes the lattice value of its output, return
1340   SSA_PROP_INTERESTING and set *OUTPUT_P to the SSA_NAME holding the
1341   output value.
1342
1343   If STMT is a conditional branch and we can determine its truth
1344   value, set *TAKEN_EDGE_P accordingly.  If STMT produces a varying
1345   value, return SSA_PROP_VARYING.  */
1346
1347static enum ssa_prop_result
1348ccp_visit_stmt (tree stmt, edge *taken_edge_p, tree *output_p)
1349{
1350  tree def;
1351  ssa_op_iter iter;
1352
1353  if (dump_file && (dump_flags & TDF_DETAILS))
1354    {
1355      fprintf (dump_file, "\nVisiting statement:\n");
1356      print_generic_stmt (dump_file, stmt, dump_flags);
1357      fprintf (dump_file, "\n");
1358    }
1359
1360  if (TREE_CODE (stmt) == MODIFY_EXPR)
1361    {
1362      /* If the statement is an assignment that produces a single
1363	 output value, evaluate its RHS to see if the lattice value of
1364	 its output has changed.  */
1365      return visit_assignment (stmt, output_p);
1366    }
1367  else if (TREE_CODE (stmt) == COND_EXPR || TREE_CODE (stmt) == SWITCH_EXPR)
1368    {
1369      /* If STMT is a conditional branch, see if we can determine
1370	 which branch will be taken.  */
1371      return visit_cond_stmt (stmt, taken_edge_p);
1372    }
1373
1374  /* Any other kind of statement is not interesting for constant
1375     propagation and, therefore, not worth simulating.  */
1376  if (dump_file && (dump_flags & TDF_DETAILS))
1377    fprintf (dump_file, "No interesting values produced.  Marked VARYING.\n");
1378
1379  /* Definitions made by statements other than assignments to
1380     SSA_NAMEs represent unknown modifications to their outputs.
1381     Mark them VARYING.  */
1382  FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
1383    {
1384      prop_value_t v = { VARYING, NULL_TREE, NULL_TREE };
1385      set_lattice_value (def, v);
1386    }
1387
1388  return SSA_PROP_VARYING;
1389}
1390
1391
1392/* Main entry point for SSA Conditional Constant Propagation.  */
1393
1394static void
1395execute_ssa_ccp (bool store_ccp)
1396{
1397  do_store_ccp = store_ccp;
1398  ccp_initialize ();
1399  ssa_propagate (ccp_visit_stmt, ccp_visit_phi_node);
1400  ccp_finalize ();
1401}
1402
1403
1404static unsigned int
1405do_ssa_ccp (void)
1406{
1407  execute_ssa_ccp (false);
1408  return 0;
1409}
1410
1411
1412static bool
1413gate_ccp (void)
1414{
1415  return flag_tree_ccp != 0;
1416}
1417
1418
1419struct tree_opt_pass pass_ccp =
1420{
1421  "ccp",				/* name */
1422  gate_ccp,				/* gate */
1423  do_ssa_ccp,				/* execute */
1424  NULL,					/* sub */
1425  NULL,					/* next */
1426  0,					/* static_pass_number */
1427  TV_TREE_CCP,				/* tv_id */
1428  PROP_cfg | PROP_ssa | PROP_alias,	/* properties_required */
1429  0,					/* properties_provided */
1430  PROP_smt_usage,			/* properties_destroyed */
1431  0,					/* todo_flags_start */
1432  TODO_cleanup_cfg | TODO_dump_func | TODO_update_ssa
1433    | TODO_ggc_collect | TODO_verify_ssa
1434    | TODO_verify_stmts | TODO_update_smt_usage, /* todo_flags_finish */
1435  0					/* letter */
1436};
1437
1438
1439static unsigned int
1440do_ssa_store_ccp (void)
1441{
1442  /* If STORE-CCP is not enabled, we just run regular CCP.  */
1443  execute_ssa_ccp (flag_tree_store_ccp != 0);
1444  return 0;
1445}
1446
1447static bool
1448gate_store_ccp (void)
1449{
1450  /* STORE-CCP is enabled only with -ftree-store-ccp, but when
1451     -fno-tree-store-ccp is specified, we should run regular CCP.
1452     That's why the pass is enabled with either flag.  */
1453  return flag_tree_store_ccp != 0 || flag_tree_ccp != 0;
1454}
1455
1456
1457struct tree_opt_pass pass_store_ccp =
1458{
1459  "store_ccp",				/* name */
1460  gate_store_ccp,			/* gate */
1461  do_ssa_store_ccp,			/* execute */
1462  NULL,					/* sub */
1463  NULL,					/* next */
1464  0,					/* static_pass_number */
1465  TV_TREE_STORE_CCP,			/* tv_id */
1466  PROP_cfg | PROP_ssa | PROP_alias,	/* properties_required */
1467  0,					/* properties_provided */
1468  PROP_smt_usage,			/* properties_destroyed */
1469  0,					/* todo_flags_start */
1470  TODO_dump_func | TODO_update_ssa
1471    | TODO_ggc_collect | TODO_verify_ssa
1472    | TODO_cleanup_cfg
1473    | TODO_verify_stmts | TODO_update_smt_usage, /* todo_flags_finish */
1474  0					/* letter */
1475};
1476
1477/* Given a constant value VAL for bitfield FIELD, and a destination
1478   variable VAR, return VAL appropriately widened to fit into VAR.  If
1479   FIELD is wider than HOST_WIDE_INT, NULL is returned.  */
1480
1481tree
1482widen_bitfield (tree val, tree field, tree var)
1483{
1484  unsigned HOST_WIDE_INT var_size, field_size;
1485  tree wide_val;
1486  unsigned HOST_WIDE_INT mask;
1487  unsigned int i;
1488
1489  /* We can only do this if the size of the type and field and VAL are
1490     all constants representable in HOST_WIDE_INT.  */
1491  if (!host_integerp (TYPE_SIZE (TREE_TYPE (var)), 1)
1492      || !host_integerp (DECL_SIZE (field), 1)
1493      || !host_integerp (val, 0))
1494    return NULL_TREE;
1495
1496  var_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (var)), 1);
1497  field_size = tree_low_cst (DECL_SIZE (field), 1);
1498
1499  /* Give up if either the bitfield or the variable are too wide.  */
1500  if (field_size > HOST_BITS_PER_WIDE_INT || var_size > HOST_BITS_PER_WIDE_INT)
1501    return NULL_TREE;
1502
1503  gcc_assert (var_size >= field_size);
1504
1505  /* If the sign bit of the value is not set or the field's type is unsigned,
1506     just mask off the high order bits of the value.  */
1507  if (DECL_UNSIGNED (field)
1508      || !(tree_low_cst (val, 0) & (((HOST_WIDE_INT)1) << (field_size - 1))))
1509    {
1510      /* Zero extension.  Build a mask with the lower 'field_size' bits
1511	 set and a BIT_AND_EXPR node to clear the high order bits of
1512	 the value.  */
1513      for (i = 0, mask = 0; i < field_size; i++)
1514	mask |= ((HOST_WIDE_INT) 1) << i;
1515
1516      wide_val = fold_build2 (BIT_AND_EXPR, TREE_TYPE (var), val,
1517			      build_int_cst (TREE_TYPE (var), mask));
1518    }
1519  else
1520    {
1521      /* Sign extension.  Create a mask with the upper 'field_size'
1522	 bits set and a BIT_IOR_EXPR to set the high order bits of the
1523	 value.  */
1524      for (i = 0, mask = 0; i < (var_size - field_size); i++)
1525	mask |= ((HOST_WIDE_INT) 1) << (var_size - i - 1);
1526
1527      wide_val = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (var), val,
1528			      build_int_cst (TREE_TYPE (var), mask));
1529    }
1530
1531  return wide_val;
1532}
1533
1534
1535/* A subroutine of fold_stmt_r.  Attempts to fold *(A+O) to A[X].
1536   BASE is an array type.  OFFSET is a byte displacement.  ORIG_TYPE
1537   is the desired result type.  */
1538
1539static tree
1540maybe_fold_offset_to_array_ref (tree base, tree offset, tree orig_type)
1541{
1542  tree min_idx, idx, elt_offset = integer_zero_node;
1543  tree array_type, elt_type, elt_size;
1544
1545  /* If BASE is an ARRAY_REF, we can pick up another offset (this time
1546     measured in units of the size of elements type) from that ARRAY_REF).
1547     We can't do anything if either is variable.
1548
1549     The case we handle here is *(&A[N]+O).  */
1550  if (TREE_CODE (base) == ARRAY_REF)
1551    {
1552      tree low_bound = array_ref_low_bound (base);
1553
1554      elt_offset = TREE_OPERAND (base, 1);
1555      if (TREE_CODE (low_bound) != INTEGER_CST
1556	  || TREE_CODE (elt_offset) != INTEGER_CST)
1557	return NULL_TREE;
1558
1559      elt_offset = int_const_binop (MINUS_EXPR, elt_offset, low_bound, 0);
1560      base = TREE_OPERAND (base, 0);
1561    }
1562
1563  /* Ignore stupid user tricks of indexing non-array variables.  */
1564  array_type = TREE_TYPE (base);
1565  if (TREE_CODE (array_type) != ARRAY_TYPE)
1566    return NULL_TREE;
1567  elt_type = TREE_TYPE (array_type);
1568  if (!lang_hooks.types_compatible_p (orig_type, elt_type))
1569    return NULL_TREE;
1570
1571  /* If OFFSET and ELT_OFFSET are zero, we don't care about the size of the
1572     element type (so we can use the alignment if it's not constant).
1573     Otherwise, compute the offset as an index by using a division.  If the
1574     division isn't exact, then don't do anything.  */
1575  elt_size = TYPE_SIZE_UNIT (elt_type);
1576  if (integer_zerop (offset))
1577    {
1578      if (TREE_CODE (elt_size) != INTEGER_CST)
1579	elt_size = size_int (TYPE_ALIGN (elt_type));
1580
1581      idx = integer_zero_node;
1582    }
1583  else
1584    {
1585      unsigned HOST_WIDE_INT lquo, lrem;
1586      HOST_WIDE_INT hquo, hrem;
1587
1588      if (TREE_CODE (elt_size) != INTEGER_CST
1589	  || div_and_round_double (TRUNC_DIV_EXPR, 1,
1590				   TREE_INT_CST_LOW (offset),
1591				   TREE_INT_CST_HIGH (offset),
1592				   TREE_INT_CST_LOW (elt_size),
1593				   TREE_INT_CST_HIGH (elt_size),
1594				   &lquo, &hquo, &lrem, &hrem)
1595	  || lrem || hrem)
1596	return NULL_TREE;
1597
1598      idx = build_int_cst_wide (NULL_TREE, lquo, hquo);
1599    }
1600
1601  /* Assume the low bound is zero.  If there is a domain type, get the
1602     low bound, if any, convert the index into that type, and add the
1603     low bound.  */
1604  min_idx = integer_zero_node;
1605  if (TYPE_DOMAIN (array_type))
1606    {
1607      if (TYPE_MIN_VALUE (TYPE_DOMAIN (array_type)))
1608	min_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (array_type));
1609      else
1610	min_idx = fold_convert (TYPE_DOMAIN (array_type), min_idx);
1611
1612      if (TREE_CODE (min_idx) != INTEGER_CST)
1613	return NULL_TREE;
1614
1615      idx = fold_convert (TYPE_DOMAIN (array_type), idx);
1616      elt_offset = fold_convert (TYPE_DOMAIN (array_type), elt_offset);
1617    }
1618
1619  if (!integer_zerop (min_idx))
1620    idx = int_const_binop (PLUS_EXPR, idx, min_idx, 0);
1621  if (!integer_zerop (elt_offset))
1622    idx = int_const_binop (PLUS_EXPR, idx, elt_offset, 0);
1623
1624  return build4 (ARRAY_REF, orig_type, base, idx, min_idx,
1625		 size_int (tree_low_cst (elt_size, 1)
1626			   / (TYPE_ALIGN_UNIT (elt_type))));
1627}
1628
1629
1630/* A subroutine of fold_stmt_r.  Attempts to fold *(S+O) to S.X.
1631   BASE is a record type.  OFFSET is a byte displacement.  ORIG_TYPE
1632   is the desired result type.  */
1633/* ??? This doesn't handle class inheritance.  */
1634
1635static tree
1636maybe_fold_offset_to_component_ref (tree record_type, tree base, tree offset,
1637				    tree orig_type, bool base_is_ptr)
1638{
1639  tree f, t, field_type, tail_array_field, field_offset;
1640
1641  if (TREE_CODE (record_type) != RECORD_TYPE
1642      && TREE_CODE (record_type) != UNION_TYPE
1643      && TREE_CODE (record_type) != QUAL_UNION_TYPE)
1644    return NULL_TREE;
1645
1646  /* Short-circuit silly cases.  */
1647  if (lang_hooks.types_compatible_p (record_type, orig_type))
1648    return NULL_TREE;
1649
1650  tail_array_field = NULL_TREE;
1651  for (f = TYPE_FIELDS (record_type); f ; f = TREE_CHAIN (f))
1652    {
1653      int cmp;
1654
1655      if (TREE_CODE (f) != FIELD_DECL)
1656	continue;
1657      if (DECL_BIT_FIELD (f))
1658	continue;
1659
1660      field_offset = byte_position (f);
1661      if (TREE_CODE (field_offset) != INTEGER_CST)
1662	continue;
1663
1664      /* ??? Java creates "interesting" fields for representing base classes.
1665	 They have no name, and have no context.  With no context, we get into
1666	 trouble with nonoverlapping_component_refs_p.  Skip them.  */
1667      if (!DECL_FIELD_CONTEXT (f))
1668	continue;
1669
1670      /* The previous array field isn't at the end.  */
1671      tail_array_field = NULL_TREE;
1672
1673      /* Check to see if this offset overlaps with the field.  */
1674      cmp = tree_int_cst_compare (field_offset, offset);
1675      if (cmp > 0)
1676	continue;
1677
1678      field_type = TREE_TYPE (f);
1679
1680      /* Here we exactly match the offset being checked.  If the types match,
1681	 then we can return that field.  */
1682      if (cmp == 0
1683	  && lang_hooks.types_compatible_p (orig_type, field_type))
1684	{
1685	  if (base_is_ptr)
1686	    base = build1 (INDIRECT_REF, record_type, base);
1687	  t = build3 (COMPONENT_REF, field_type, base, f, NULL_TREE);
1688	  return t;
1689	}
1690
1691      /* Don't care about offsets into the middle of scalars.  */
1692      if (!AGGREGATE_TYPE_P (field_type))
1693	continue;
1694
1695      /* Check for array at the end of the struct.  This is often
1696	 used as for flexible array members.  We should be able to
1697	 turn this into an array access anyway.  */
1698      if (TREE_CODE (field_type) == ARRAY_TYPE)
1699	tail_array_field = f;
1700
1701      /* Check the end of the field against the offset.  */
1702      if (!DECL_SIZE_UNIT (f)
1703	  || TREE_CODE (DECL_SIZE_UNIT (f)) != INTEGER_CST)
1704	continue;
1705      t = int_const_binop (MINUS_EXPR, offset, field_offset, 1);
1706      if (!tree_int_cst_lt (t, DECL_SIZE_UNIT (f)))
1707	continue;
1708
1709      /* If we matched, then set offset to the displacement into
1710	 this field.  */
1711      offset = t;
1712      goto found;
1713    }
1714
1715  if (!tail_array_field)
1716    return NULL_TREE;
1717
1718  f = tail_array_field;
1719  field_type = TREE_TYPE (f);
1720  offset = int_const_binop (MINUS_EXPR, offset, byte_position (f), 1);
1721
1722 found:
1723  /* If we get here, we've got an aggregate field, and a possibly
1724     nonzero offset into them.  Recurse and hope for a valid match.  */
1725  if (base_is_ptr)
1726    base = build1 (INDIRECT_REF, record_type, base);
1727  base = build3 (COMPONENT_REF, field_type, base, f, NULL_TREE);
1728
1729  t = maybe_fold_offset_to_array_ref (base, offset, orig_type);
1730  if (t)
1731    return t;
1732  return maybe_fold_offset_to_component_ref (field_type, base, offset,
1733					     orig_type, false);
1734}
1735
1736
1737/* A subroutine of fold_stmt_r.  Attempt to simplify *(BASE+OFFSET).
1738   Return the simplified expression, or NULL if nothing could be done.  */
1739
1740static tree
1741maybe_fold_stmt_indirect (tree expr, tree base, tree offset)
1742{
1743  tree t;
1744
1745  /* We may well have constructed a double-nested PLUS_EXPR via multiple
1746     substitutions.  Fold that down to one.  Remove NON_LVALUE_EXPRs that
1747     are sometimes added.  */
1748  base = fold (base);
1749  STRIP_TYPE_NOPS (base);
1750  TREE_OPERAND (expr, 0) = base;
1751
1752  /* One possibility is that the address reduces to a string constant.  */
1753  t = fold_read_from_constant_string (expr);
1754  if (t)
1755    return t;
1756
1757  /* Add in any offset from a PLUS_EXPR.  */
1758  if (TREE_CODE (base) == PLUS_EXPR)
1759    {
1760      tree offset2;
1761
1762      offset2 = TREE_OPERAND (base, 1);
1763      if (TREE_CODE (offset2) != INTEGER_CST)
1764	return NULL_TREE;
1765      base = TREE_OPERAND (base, 0);
1766
1767      offset = int_const_binop (PLUS_EXPR, offset, offset2, 1);
1768    }
1769
1770  if (TREE_CODE (base) == ADDR_EXPR)
1771    {
1772      /* Strip the ADDR_EXPR.  */
1773      base = TREE_OPERAND (base, 0);
1774
1775      /* Fold away CONST_DECL to its value, if the type is scalar.  */
1776      if (TREE_CODE (base) == CONST_DECL
1777	  && ccp_decl_initial_min_invariant (DECL_INITIAL (base)))
1778	return DECL_INITIAL (base);
1779
1780      /* Try folding *(&B+O) to B[X].  */
1781      t = maybe_fold_offset_to_array_ref (base, offset, TREE_TYPE (expr));
1782      if (t)
1783	return t;
1784
1785      /* Try folding *(&B+O) to B.X.  */
1786      t = maybe_fold_offset_to_component_ref (TREE_TYPE (base), base, offset,
1787					      TREE_TYPE (expr), false);
1788      if (t)
1789	return t;
1790
1791      /* Fold *&B to B.  We can only do this if EXPR is the same type
1792	 as BASE.  We can't do this if EXPR is the element type of an array
1793	 and BASE is the array.  */
1794      if (integer_zerop (offset)
1795	  && lang_hooks.types_compatible_p (TREE_TYPE (base),
1796					    TREE_TYPE (expr)))
1797	return base;
1798    }
1799  else
1800    {
1801      /* We can get here for out-of-range string constant accesses,
1802	 such as "_"[3].  Bail out of the entire substitution search
1803	 and arrange for the entire statement to be replaced by a
1804	 call to __builtin_trap.  In all likelihood this will all be
1805	 constant-folded away, but in the meantime we can't leave with
1806	 something that get_expr_operands can't understand.  */
1807
1808      t = base;
1809      STRIP_NOPS (t);
1810      if (TREE_CODE (t) == ADDR_EXPR
1811	  && TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
1812	{
1813	  /* FIXME: Except that this causes problems elsewhere with dead
1814	     code not being deleted, and we die in the rtl expanders
1815	     because we failed to remove some ssa_name.  In the meantime,
1816	     just return zero.  */
1817	  /* FIXME2: This condition should be signaled by
1818	     fold_read_from_constant_string directly, rather than
1819	     re-checking for it here.  */
1820	  return integer_zero_node;
1821	}
1822
1823      /* Try folding *(B+O) to B->X.  Still an improvement.  */
1824      if (POINTER_TYPE_P (TREE_TYPE (base)))
1825	{
1826          t = maybe_fold_offset_to_component_ref (TREE_TYPE (TREE_TYPE (base)),
1827						  base, offset,
1828						  TREE_TYPE (expr), true);
1829	  if (t)
1830	    return t;
1831	}
1832    }
1833
1834  /* Otherwise we had an offset that we could not simplify.  */
1835  return NULL_TREE;
1836}
1837
1838
1839/* A subroutine of fold_stmt_r.  EXPR is a PLUS_EXPR.
1840
1841   A quaint feature extant in our address arithmetic is that there
1842   can be hidden type changes here.  The type of the result need
1843   not be the same as the type of the input pointer.
1844
1845   What we're after here is an expression of the form
1846	(T *)(&array + const)
1847   where the cast doesn't actually exist, but is implicit in the
1848   type of the PLUS_EXPR.  We'd like to turn this into
1849	&array[x]
1850   which may be able to propagate further.  */
1851
1852static tree
1853maybe_fold_stmt_addition (tree expr)
1854{
1855  tree op0 = TREE_OPERAND (expr, 0);
1856  tree op1 = TREE_OPERAND (expr, 1);
1857  tree ptr_type = TREE_TYPE (expr);
1858  tree ptd_type;
1859  tree t;
1860  bool subtract = (TREE_CODE (expr) == MINUS_EXPR);
1861
1862  /* We're only interested in pointer arithmetic.  */
1863  if (!POINTER_TYPE_P (ptr_type))
1864    return NULL_TREE;
1865  /* Canonicalize the integral operand to op1.  */
1866  if (INTEGRAL_TYPE_P (TREE_TYPE (op0)))
1867    {
1868      if (subtract)
1869	return NULL_TREE;
1870      t = op0, op0 = op1, op1 = t;
1871    }
1872  /* It had better be a constant.  */
1873  if (TREE_CODE (op1) != INTEGER_CST)
1874    return NULL_TREE;
1875  /* The first operand should be an ADDR_EXPR.  */
1876  if (TREE_CODE (op0) != ADDR_EXPR)
1877    return NULL_TREE;
1878  op0 = TREE_OPERAND (op0, 0);
1879
1880  /* If the first operand is an ARRAY_REF, expand it so that we can fold
1881     the offset into it.  */
1882  while (TREE_CODE (op0) == ARRAY_REF)
1883    {
1884      tree array_obj = TREE_OPERAND (op0, 0);
1885      tree array_idx = TREE_OPERAND (op0, 1);
1886      tree elt_type = TREE_TYPE (op0);
1887      tree elt_size = TYPE_SIZE_UNIT (elt_type);
1888      tree min_idx;
1889
1890      if (TREE_CODE (array_idx) != INTEGER_CST)
1891	break;
1892      if (TREE_CODE (elt_size) != INTEGER_CST)
1893	break;
1894
1895      /* Un-bias the index by the min index of the array type.  */
1896      min_idx = TYPE_DOMAIN (TREE_TYPE (array_obj));
1897      if (min_idx)
1898	{
1899	  min_idx = TYPE_MIN_VALUE (min_idx);
1900	  if (min_idx)
1901	    {
1902	      if (TREE_CODE (min_idx) != INTEGER_CST)
1903		break;
1904
1905	      array_idx = fold_convert (TREE_TYPE (min_idx), array_idx);
1906	      if (!integer_zerop (min_idx))
1907		array_idx = int_const_binop (MINUS_EXPR, array_idx,
1908					     min_idx, 0);
1909	    }
1910	}
1911
1912      /* Convert the index to a byte offset.  */
1913      array_idx = fold_convert (sizetype, array_idx);
1914      array_idx = int_const_binop (MULT_EXPR, array_idx, elt_size, 0);
1915
1916      /* Update the operands for the next round, or for folding.  */
1917      /* If we're manipulating unsigned types, then folding into negative
1918	 values can produce incorrect results.  Particularly if the type
1919	 is smaller than the width of the pointer.  */
1920      if (subtract
1921	  && TYPE_UNSIGNED (TREE_TYPE (op1))
1922	  && tree_int_cst_lt (array_idx, op1))
1923	return NULL;
1924      op1 = int_const_binop (subtract ? MINUS_EXPR : PLUS_EXPR,
1925			     array_idx, op1, 0);
1926      subtract = false;
1927      op0 = array_obj;
1928    }
1929
1930  /* If we weren't able to fold the subtraction into another array reference,
1931     canonicalize the integer for passing to the array and component ref
1932     simplification functions.  */
1933  if (subtract)
1934    {
1935      if (TYPE_UNSIGNED (TREE_TYPE (op1)))
1936	return NULL;
1937      op1 = fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1);
1938      /* ??? In theory fold should always produce another integer.  */
1939      if (op1 == NULL || TREE_CODE (op1) != INTEGER_CST)
1940	return NULL;
1941    }
1942
1943  ptd_type = TREE_TYPE (ptr_type);
1944
1945  /* At which point we can try some of the same things as for indirects.  */
1946  t = maybe_fold_offset_to_array_ref (op0, op1, ptd_type);
1947  if (!t)
1948    t = maybe_fold_offset_to_component_ref (TREE_TYPE (op0), op0, op1,
1949					    ptd_type, false);
1950  if (t)
1951    t = build1 (ADDR_EXPR, ptr_type, t);
1952
1953  return t;
1954}
1955
1956/* For passing state through walk_tree into fold_stmt_r and its
1957   children.  */
1958
1959struct fold_stmt_r_data
1960{
1961  tree stmt;
1962  bool *changed_p;
1963  bool *inside_addr_expr_p;
1964};
1965
1966/* Subroutine of fold_stmt called via walk_tree.  We perform several
1967   simplifications of EXPR_P, mostly having to do with pointer arithmetic.  */
1968
1969static tree
1970fold_stmt_r (tree *expr_p, int *walk_subtrees, void *data)
1971{
1972  struct fold_stmt_r_data *fold_stmt_r_data = (struct fold_stmt_r_data *) data;
1973  bool *inside_addr_expr_p = fold_stmt_r_data->inside_addr_expr_p;
1974  bool *changed_p = fold_stmt_r_data->changed_p;
1975  tree expr = *expr_p, t;
1976
1977  /* ??? It'd be nice if walk_tree had a pre-order option.  */
1978  switch (TREE_CODE (expr))
1979    {
1980    case INDIRECT_REF:
1981      t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
1982      if (t)
1983	return t;
1984      *walk_subtrees = 0;
1985
1986      t = maybe_fold_stmt_indirect (expr, TREE_OPERAND (expr, 0),
1987				    integer_zero_node);
1988      break;
1989
1990      /* ??? Could handle more ARRAY_REFs here, as a variant of INDIRECT_REF.
1991	 We'd only want to bother decomposing an existing ARRAY_REF if
1992	 the base array is found to have another offset contained within.
1993	 Otherwise we'd be wasting time.  */
1994    case ARRAY_REF:
1995      /* If we are not processing expressions found within an
1996	 ADDR_EXPR, then we can fold constant array references.  */
1997      if (!*inside_addr_expr_p)
1998	t = fold_read_from_constant_string (expr);
1999      else
2000	t = NULL;
2001      break;
2002
2003    case ADDR_EXPR:
2004      *inside_addr_expr_p = true;
2005      t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
2006      *inside_addr_expr_p = false;
2007      if (t)
2008	return t;
2009      *walk_subtrees = 0;
2010
2011      /* Set TREE_INVARIANT properly so that the value is properly
2012	 considered constant, and so gets propagated as expected.  */
2013      if (*changed_p)
2014        recompute_tree_invariant_for_addr_expr (expr);
2015      return NULL_TREE;
2016
2017    case PLUS_EXPR:
2018    case MINUS_EXPR:
2019      t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
2020      if (t)
2021	return t;
2022      t = walk_tree (&TREE_OPERAND (expr, 1), fold_stmt_r, data, NULL);
2023      if (t)
2024	return t;
2025      *walk_subtrees = 0;
2026
2027      t = maybe_fold_stmt_addition (expr);
2028      break;
2029
2030    case COMPONENT_REF:
2031      t = walk_tree (&TREE_OPERAND (expr, 0), fold_stmt_r, data, NULL);
2032      if (t)
2033        return t;
2034      *walk_subtrees = 0;
2035
2036      /* Make sure the FIELD_DECL is actually a field in the type on the lhs.
2037	 We've already checked that the records are compatible, so we should
2038	 come up with a set of compatible fields.  */
2039      {
2040	tree expr_record = TREE_TYPE (TREE_OPERAND (expr, 0));
2041	tree expr_field = TREE_OPERAND (expr, 1);
2042
2043        if (DECL_FIELD_CONTEXT (expr_field) != TYPE_MAIN_VARIANT (expr_record))
2044	  {
2045	    expr_field = find_compatible_field (expr_record, expr_field);
2046	    TREE_OPERAND (expr, 1) = expr_field;
2047	  }
2048      }
2049      break;
2050
2051    case TARGET_MEM_REF:
2052      t = maybe_fold_tmr (expr);
2053      break;
2054
2055    case COND_EXPR:
2056      if (COMPARISON_CLASS_P (TREE_OPERAND (expr, 0)))
2057        {
2058	  tree op0 = TREE_OPERAND (expr, 0);
2059	  tree tem;
2060	  bool set;
2061
2062	  fold_defer_overflow_warnings ();
2063	  tem = fold_binary (TREE_CODE (op0), TREE_TYPE (op0),
2064			     TREE_OPERAND (op0, 0),
2065			     TREE_OPERAND (op0, 1));
2066	  set = tem && is_gimple_condexpr (tem);
2067	  fold_undefer_overflow_warnings (set, fold_stmt_r_data->stmt, 0);
2068	  if (set)
2069	    TREE_OPERAND (expr, 0) = tem;
2070	  t = expr;
2071          break;
2072        }
2073
2074    default:
2075      return NULL_TREE;
2076    }
2077
2078  if (t)
2079    {
2080      *expr_p = t;
2081      *changed_p = true;
2082    }
2083
2084  return NULL_TREE;
2085}
2086
2087
2088/* Return the string length, maximum string length or maximum value of
2089   ARG in LENGTH.
2090   If ARG is an SSA name variable, follow its use-def chains.  If LENGTH
2091   is not NULL and, for TYPE == 0, its value is not equal to the length
2092   we determine or if we are unable to determine the length or value,
2093   return false.  VISITED is a bitmap of visited variables.
2094   TYPE is 0 if string length should be returned, 1 for maximum string
2095   length and 2 for maximum value ARG can have.  */
2096
2097static bool
2098get_maxval_strlen (tree arg, tree *length, bitmap visited, int type)
2099{
2100  tree var, def_stmt, val;
2101
2102  if (TREE_CODE (arg) != SSA_NAME)
2103    {
2104      if (type == 2)
2105	{
2106	  val = arg;
2107	  if (TREE_CODE (val) != INTEGER_CST
2108	      || tree_int_cst_sgn (val) < 0)
2109	    return false;
2110	}
2111      else
2112	val = c_strlen (arg, 1);
2113      if (!val)
2114	return false;
2115
2116      if (*length)
2117	{
2118	  if (type > 0)
2119	    {
2120	      if (TREE_CODE (*length) != INTEGER_CST
2121		  || TREE_CODE (val) != INTEGER_CST)
2122		return false;
2123
2124	      if (tree_int_cst_lt (*length, val))
2125		*length = val;
2126	      return true;
2127	    }
2128	  else if (simple_cst_equal (val, *length) != 1)
2129	    return false;
2130	}
2131
2132      *length = val;
2133      return true;
2134    }
2135
2136  /* If we were already here, break the infinite cycle.  */
2137  if (bitmap_bit_p (visited, SSA_NAME_VERSION (arg)))
2138    return true;
2139  bitmap_set_bit (visited, SSA_NAME_VERSION (arg));
2140
2141  var = arg;
2142  def_stmt = SSA_NAME_DEF_STMT (var);
2143
2144  switch (TREE_CODE (def_stmt))
2145    {
2146      case MODIFY_EXPR:
2147	{
2148	  tree rhs;
2149
2150	  /* The RHS of the statement defining VAR must either have a
2151	     constant length or come from another SSA_NAME with a constant
2152	     length.  */
2153	  rhs = TREE_OPERAND (def_stmt, 1);
2154	  STRIP_NOPS (rhs);
2155	  return get_maxval_strlen (rhs, length, visited, type);
2156	}
2157
2158      case PHI_NODE:
2159	{
2160	  /* All the arguments of the PHI node must have the same constant
2161	     length.  */
2162	  int i;
2163
2164	  for (i = 0; i < PHI_NUM_ARGS (def_stmt); i++)
2165	    {
2166	      tree arg = PHI_ARG_DEF (def_stmt, i);
2167
2168	      /* If this PHI has itself as an argument, we cannot
2169		 determine the string length of this argument.  However,
2170		 if we can find a constant string length for the other
2171		 PHI args then we can still be sure that this is a
2172		 constant string length.  So be optimistic and just
2173		 continue with the next argument.  */
2174	      if (arg == PHI_RESULT (def_stmt))
2175		continue;
2176
2177	      if (!get_maxval_strlen (arg, length, visited, type))
2178		return false;
2179	    }
2180
2181	  return true;
2182	}
2183
2184      default:
2185	break;
2186    }
2187
2188
2189  return false;
2190}
2191
2192
2193/* Fold builtin call FN in statement STMT.  If it cannot be folded into a
2194   constant, return NULL_TREE.  Otherwise, return its constant value.  */
2195
2196static tree
2197ccp_fold_builtin (tree stmt, tree fn)
2198{
2199  tree result, val[3];
2200  tree callee, arglist, a;
2201  int arg_mask, i, type;
2202  bitmap visited;
2203  bool ignore;
2204
2205  ignore = TREE_CODE (stmt) != MODIFY_EXPR;
2206
2207  /* First try the generic builtin folder.  If that succeeds, return the
2208     result directly.  */
2209  callee = get_callee_fndecl (fn);
2210  arglist = TREE_OPERAND (fn, 1);
2211  result = fold_builtin (callee, arglist, ignore);
2212  if (result)
2213    {
2214      if (ignore)
2215	STRIP_NOPS (result);
2216      return result;
2217    }
2218
2219  /* Ignore MD builtins.  */
2220  if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_MD)
2221    return NULL_TREE;
2222
2223  /* If the builtin could not be folded, and it has no argument list,
2224     we're done.  */
2225  if (!arglist)
2226    return NULL_TREE;
2227
2228  /* Limit the work only for builtins we know how to simplify.  */
2229  switch (DECL_FUNCTION_CODE (callee))
2230    {
2231    case BUILT_IN_STRLEN:
2232    case BUILT_IN_FPUTS:
2233    case BUILT_IN_FPUTS_UNLOCKED:
2234      arg_mask = 1;
2235      type = 0;
2236      break;
2237    case BUILT_IN_STRCPY:
2238    case BUILT_IN_STRNCPY:
2239      arg_mask = 2;
2240      type = 0;
2241      break;
2242    case BUILT_IN_MEMCPY_CHK:
2243    case BUILT_IN_MEMPCPY_CHK:
2244    case BUILT_IN_MEMMOVE_CHK:
2245    case BUILT_IN_MEMSET_CHK:
2246    case BUILT_IN_STRNCPY_CHK:
2247      arg_mask = 4;
2248      type = 2;
2249      break;
2250    case BUILT_IN_STRCPY_CHK:
2251    case BUILT_IN_STPCPY_CHK:
2252      arg_mask = 2;
2253      type = 1;
2254      break;
2255    case BUILT_IN_SNPRINTF_CHK:
2256    case BUILT_IN_VSNPRINTF_CHK:
2257      arg_mask = 2;
2258      type = 2;
2259      break;
2260    default:
2261      return NULL_TREE;
2262    }
2263
2264  /* Try to use the dataflow information gathered by the CCP process.  */
2265  visited = BITMAP_ALLOC (NULL);
2266
2267  memset (val, 0, sizeof (val));
2268  for (i = 0, a = arglist;
2269       arg_mask;
2270       i++, arg_mask >>= 1, a = TREE_CHAIN (a))
2271    if (arg_mask & 1)
2272      {
2273	bitmap_clear (visited);
2274	if (!get_maxval_strlen (TREE_VALUE (a), &val[i], visited, type))
2275	  val[i] = NULL_TREE;
2276      }
2277
2278  BITMAP_FREE (visited);
2279
2280  result = NULL_TREE;
2281  switch (DECL_FUNCTION_CODE (callee))
2282    {
2283    case BUILT_IN_STRLEN:
2284      if (val[0])
2285	{
2286	  tree new = fold_convert (TREE_TYPE (fn), val[0]);
2287
2288	  /* If the result is not a valid gimple value, or not a cast
2289	     of a valid gimple value, then we can not use the result.  */
2290	  if (is_gimple_val (new)
2291	      || (is_gimple_cast (new)
2292		  && is_gimple_val (TREE_OPERAND (new, 0))))
2293	    return new;
2294	}
2295      break;
2296
2297    case BUILT_IN_STRCPY:
2298      if (val[1] && is_gimple_val (val[1]))
2299	result = fold_builtin_strcpy (callee, arglist, val[1]);
2300      break;
2301
2302    case BUILT_IN_STRNCPY:
2303      if (val[1] && is_gimple_val (val[1]))
2304	result = fold_builtin_strncpy (callee, arglist, val[1]);
2305      break;
2306
2307    case BUILT_IN_FPUTS:
2308      result = fold_builtin_fputs (arglist,
2309				   TREE_CODE (stmt) != MODIFY_EXPR, 0,
2310				   val[0]);
2311      break;
2312
2313    case BUILT_IN_FPUTS_UNLOCKED:
2314      result = fold_builtin_fputs (arglist,
2315				   TREE_CODE (stmt) != MODIFY_EXPR, 1,
2316				   val[0]);
2317      break;
2318
2319    case BUILT_IN_MEMCPY_CHK:
2320    case BUILT_IN_MEMPCPY_CHK:
2321    case BUILT_IN_MEMMOVE_CHK:
2322    case BUILT_IN_MEMSET_CHK:
2323      if (val[2] && is_gimple_val (val[2]))
2324	result = fold_builtin_memory_chk (callee, arglist, val[2], ignore,
2325					  DECL_FUNCTION_CODE (callee));
2326      break;
2327
2328    case BUILT_IN_STRCPY_CHK:
2329    case BUILT_IN_STPCPY_CHK:
2330      if (val[1] && is_gimple_val (val[1]))
2331	result = fold_builtin_stxcpy_chk (callee, arglist, val[1], ignore,
2332					  DECL_FUNCTION_CODE (callee));
2333      break;
2334
2335    case BUILT_IN_STRNCPY_CHK:
2336      if (val[2] && is_gimple_val (val[2]))
2337	result = fold_builtin_strncpy_chk (arglist, val[2]);
2338      break;
2339
2340    case BUILT_IN_SNPRINTF_CHK:
2341    case BUILT_IN_VSNPRINTF_CHK:
2342      if (val[1] && is_gimple_val (val[1]))
2343	result = fold_builtin_snprintf_chk (arglist, val[1],
2344					    DECL_FUNCTION_CODE (callee));
2345      break;
2346
2347    default:
2348      gcc_unreachable ();
2349    }
2350
2351  if (result && ignore)
2352    result = fold_ignored_result (result);
2353  return result;
2354}
2355
2356
2357/* Fold the statement pointed to by STMT_P.  In some cases, this function may
2358   replace the whole statement with a new one.  Returns true iff folding
2359   makes any changes.  */
2360
2361bool
2362fold_stmt (tree *stmt_p)
2363{
2364  tree rhs, result, stmt;
2365  struct fold_stmt_r_data fold_stmt_r_data;
2366  bool changed = false;
2367  bool inside_addr_expr = false;
2368
2369  stmt = *stmt_p;
2370
2371  fold_stmt_r_data.stmt = stmt;
2372  fold_stmt_r_data.changed_p = &changed;
2373  fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
2374
2375  /* If we replaced constants and the statement makes pointer dereferences,
2376     then we may need to fold instances of *&VAR into VAR, etc.  */
2377  if (walk_tree (stmt_p, fold_stmt_r, &fold_stmt_r_data, NULL))
2378    {
2379      *stmt_p
2380	= build_function_call_expr (implicit_built_in_decls[BUILT_IN_TRAP],
2381				    NULL);
2382      return true;
2383    }
2384
2385  rhs = get_rhs (stmt);
2386  if (!rhs)
2387    return changed;
2388  result = NULL_TREE;
2389
2390  if (TREE_CODE (rhs) == CALL_EXPR)
2391    {
2392      tree callee;
2393
2394      /* Check for builtins that CCP can handle using information not
2395	 available in the generic fold routines.  */
2396      callee = get_callee_fndecl (rhs);
2397      if (callee && DECL_BUILT_IN (callee))
2398	result = ccp_fold_builtin (stmt, rhs);
2399      else
2400	{
2401	  /* Check for resolvable OBJ_TYPE_REF.  The only sorts we can resolve
2402	     here are when we've propagated the address of a decl into the
2403	     object slot.  */
2404	  /* ??? Should perhaps do this in fold proper.  However, doing it
2405	     there requires that we create a new CALL_EXPR, and that requires
2406	     copying EH region info to the new node.  Easier to just do it
2407	     here where we can just smash the call operand. Also
2408	     CALL_EXPR_RETURN_SLOT_OPT needs to be handled correctly and
2409	     copied, fold_ternary does not have not information. */
2410	  callee = TREE_OPERAND (rhs, 0);
2411	  if (TREE_CODE (callee) == OBJ_TYPE_REF
2412	      && lang_hooks.fold_obj_type_ref
2413	      && TREE_CODE (OBJ_TYPE_REF_OBJECT (callee)) == ADDR_EXPR
2414	      && DECL_P (TREE_OPERAND
2415			 (OBJ_TYPE_REF_OBJECT (callee), 0)))
2416	    {
2417	      tree t;
2418
2419	      /* ??? Caution: Broken ADDR_EXPR semantics means that
2420		 looking at the type of the operand of the addr_expr
2421		 can yield an array type.  See silly exception in
2422		 check_pointer_types_r.  */
2423
2424	      t = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (callee)));
2425	      t = lang_hooks.fold_obj_type_ref (callee, t);
2426	      if (t)
2427		{
2428		  TREE_OPERAND (rhs, 0) = t;
2429		  changed = true;
2430		}
2431	    }
2432	}
2433    }
2434
2435  /* If we couldn't fold the RHS, hand over to the generic fold routines.  */
2436  if (result == NULL_TREE)
2437    result = fold (rhs);
2438
2439  /* Strip away useless type conversions.  Both the NON_LVALUE_EXPR that
2440     may have been added by fold, and "useless" type conversions that might
2441     now be apparent due to propagation.  */
2442  STRIP_USELESS_TYPE_CONVERSION (result);
2443
2444  if (result != rhs)
2445    changed |= set_rhs (stmt_p, result);
2446
2447  return changed;
2448}
2449
2450/* Perform the minimal folding on statement STMT.  Only operations like
2451   *&x created by constant propagation are handled.  The statement cannot
2452   be replaced with a new one.  */
2453
2454bool
2455fold_stmt_inplace (tree stmt)
2456{
2457  tree old_stmt = stmt, rhs, new_rhs;
2458  struct fold_stmt_r_data fold_stmt_r_data;
2459  bool changed = false;
2460  bool inside_addr_expr = false;
2461
2462  fold_stmt_r_data.stmt = stmt;
2463  fold_stmt_r_data.changed_p = &changed;
2464  fold_stmt_r_data.inside_addr_expr_p = &inside_addr_expr;
2465
2466  walk_tree (&stmt, fold_stmt_r, &fold_stmt_r_data, NULL);
2467  gcc_assert (stmt == old_stmt);
2468
2469  rhs = get_rhs (stmt);
2470  if (!rhs || rhs == stmt)
2471    return changed;
2472
2473  new_rhs = fold (rhs);
2474  STRIP_USELESS_TYPE_CONVERSION (new_rhs);
2475  if (new_rhs == rhs)
2476    return changed;
2477
2478  changed |= set_rhs (&stmt, new_rhs);
2479  gcc_assert (stmt == old_stmt);
2480
2481  return changed;
2482}
2483
2484/* Convert EXPR into a GIMPLE value suitable for substitution on the
2485   RHS of an assignment.  Insert the necessary statements before
2486   iterator *SI_P.  */
2487
2488static tree
2489convert_to_gimple_builtin (block_stmt_iterator *si_p, tree expr)
2490{
2491  tree_stmt_iterator ti;
2492  tree stmt = bsi_stmt (*si_p);
2493  tree tmp, stmts = NULL;
2494
2495  push_gimplify_context ();
2496  tmp = get_initialized_tmp_var (expr, &stmts, NULL);
2497  pop_gimplify_context (NULL);
2498
2499  if (EXPR_HAS_LOCATION (stmt))
2500    annotate_all_with_locus (&stmts, EXPR_LOCATION (stmt));
2501
2502  /* The replacement can expose previously unreferenced variables.  */
2503  for (ti = tsi_start (stmts); !tsi_end_p (ti); tsi_next (&ti))
2504    {
2505      tree new_stmt = tsi_stmt (ti);
2506      find_new_referenced_vars (tsi_stmt_ptr (ti));
2507      bsi_insert_before (si_p, new_stmt, BSI_NEW_STMT);
2508      mark_new_vars_to_rename (bsi_stmt (*si_p));
2509      bsi_next (si_p);
2510    }
2511
2512  return tmp;
2513}
2514
2515
2516/* A simple pass that attempts to fold all builtin functions.  This pass
2517   is run after we've propagated as many constants as we can.  */
2518
2519static unsigned int
2520execute_fold_all_builtins (void)
2521{
2522  bool cfg_changed = false;
2523  basic_block bb;
2524  FOR_EACH_BB (bb)
2525    {
2526      block_stmt_iterator i;
2527      for (i = bsi_start (bb); !bsi_end_p (i); )
2528	{
2529	  tree *stmtp = bsi_stmt_ptr (i);
2530	  tree old_stmt = *stmtp;
2531	  tree call = get_rhs (*stmtp);
2532	  tree callee, result;
2533	  enum built_in_function fcode;
2534
2535	  if (!call || TREE_CODE (call) != CALL_EXPR)
2536	    {
2537	      bsi_next (&i);
2538	      continue;
2539	    }
2540	  callee = get_callee_fndecl (call);
2541	  if (!callee || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL)
2542	    {
2543	      bsi_next (&i);
2544	      continue;
2545	    }
2546	  fcode = DECL_FUNCTION_CODE (callee);
2547
2548	  result = ccp_fold_builtin (*stmtp, call);
2549	  if (!result)
2550	    switch (DECL_FUNCTION_CODE (callee))
2551	      {
2552	      case BUILT_IN_CONSTANT_P:
2553		/* Resolve __builtin_constant_p.  If it hasn't been
2554		   folded to integer_one_node by now, it's fairly
2555		   certain that the value simply isn't constant.  */
2556		result = integer_zero_node;
2557		break;
2558
2559	      default:
2560		bsi_next (&i);
2561		continue;
2562	      }
2563
2564	  if (dump_file && (dump_flags & TDF_DETAILS))
2565	    {
2566	      fprintf (dump_file, "Simplified\n  ");
2567	      print_generic_stmt (dump_file, *stmtp, dump_flags);
2568	    }
2569
2570	  if (!set_rhs (stmtp, result))
2571	    {
2572	      result = convert_to_gimple_builtin (&i, result);
2573	      if (result)
2574		{
2575		  bool ok = set_rhs (stmtp, result);
2576
2577		  gcc_assert (ok);
2578		}
2579	    }
2580	  mark_new_vars_to_rename (*stmtp);
2581	  if (maybe_clean_or_replace_eh_stmt (old_stmt, *stmtp)
2582	      && tree_purge_dead_eh_edges (bb))
2583	    cfg_changed = true;
2584
2585	  if (dump_file && (dump_flags & TDF_DETAILS))
2586	    {
2587	      fprintf (dump_file, "to\n  ");
2588	      print_generic_stmt (dump_file, *stmtp, dump_flags);
2589	      fprintf (dump_file, "\n");
2590	    }
2591
2592	  /* Retry the same statement if it changed into another
2593	     builtin, there might be new opportunities now.  */
2594	  call = get_rhs (*stmtp);
2595	  if (!call || TREE_CODE (call) != CALL_EXPR)
2596	    {
2597	      bsi_next (&i);
2598	      continue;
2599	    }
2600	  callee = get_callee_fndecl (call);
2601	  if (!callee
2602	      || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2603	      || DECL_FUNCTION_CODE (callee) == fcode)
2604	    bsi_next (&i);
2605	}
2606    }
2607
2608  /* Delete unreachable blocks.  */
2609  if (cfg_changed)
2610    cleanup_tree_cfg ();
2611  return 0;
2612}
2613
2614
2615struct tree_opt_pass pass_fold_builtins =
2616{
2617  "fab",				/* name */
2618  NULL,					/* gate */
2619  execute_fold_all_builtins,		/* execute */
2620  NULL,					/* sub */
2621  NULL,					/* next */
2622  0,					/* static_pass_number */
2623  0,					/* tv_id */
2624  PROP_cfg | PROP_ssa | PROP_alias,	/* properties_required */
2625  0,					/* properties_provided */
2626  0,					/* properties_destroyed */
2627  0,					/* todo_flags_start */
2628  TODO_dump_func
2629    | TODO_verify_ssa
2630    | TODO_update_ssa,			/* todo_flags_finish */
2631  0					/* letter */
2632};
2633