xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/gimple-expr.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /* Gimple decl, type, and expression support functions.
2 
3    Copyright (C) 2007-2017 Free Software Foundation, Inc.
4    Contributed by Aldy Hernandez <aldyh@redhat.com>
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12 
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 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.
17 
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/>.  */
21 
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "stringpool.h"
29 #include "gimple-ssa.h"
30 #include "fold-const.h"
31 #include "tree-eh.h"
32 #include "gimplify.h"
33 #include "stor-layout.h"
34 #include "demangle.h"
35 #include "hash-set.h"
36 #include "rtl.h"
37 #include "tree-pass.h"
38 
39 /* ----- Type related -----  */
40 
41 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
42    useless type conversion, otherwise return false.
43 
44    This function implicitly defines the middle-end type system.  With
45    the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
46    holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
47    the following invariants shall be fulfilled:
48 
49      1) useless_type_conversion_p is transitive.
50 	If a < b and b < c then a < c.
51 
52      2) useless_type_conversion_p is not symmetric.
53 	From a < b does not follow a > b.
54 
55      3) Types define the available set of operations applicable to values.
56 	A type conversion is useless if the operations for the target type
57 	is a subset of the operations for the source type.  For example
58 	casts to void* are useless, casts from void* are not (void* can't
59 	be dereferenced or offsetted, but copied, hence its set of operations
60 	is a strict subset of that of all other data pointer types).  Casts
61 	to const T* are useless (can't be written to), casts from const T*
62 	to T* are not.  */
63 
64 bool
65 useless_type_conversion_p (tree outer_type, tree inner_type)
66 {
67   /* Do the following before stripping toplevel qualifiers.  */
68   if (POINTER_TYPE_P (inner_type)
69       && POINTER_TYPE_P (outer_type))
70     {
71       /* Do not lose casts between pointers to different address spaces.  */
72       if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
73 	  != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
74 	return false;
75       /* Do not lose casts to function pointer types.  */
76       if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
77 	   || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
78 	  && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
79 	       || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
80 	return false;
81     }
82 
83   /* From now on qualifiers on value types do not matter.  */
84   inner_type = TYPE_MAIN_VARIANT (inner_type);
85   outer_type = TYPE_MAIN_VARIANT (outer_type);
86 
87   if (inner_type == outer_type)
88     return true;
89 
90   /* Changes in machine mode are never useless conversions because the RTL
91      middle-end expects explicit conversions between modes.  */
92   if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
93     return false;
94 
95   /* If both the inner and outer types are integral types, then the
96      conversion is not necessary if they have the same mode and
97      signedness and precision, and both or neither are boolean.  */
98   if (INTEGRAL_TYPE_P (inner_type)
99       && INTEGRAL_TYPE_P (outer_type))
100     {
101       /* Preserve changes in signedness or precision.  */
102       if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
103 	  || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
104 	return false;
105 
106       /* Preserve conversions to/from BOOLEAN_TYPE if types are not
107 	 of precision one.  */
108       if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
109 	   != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
110 	  && TYPE_PRECISION (outer_type) != 1)
111 	return false;
112 
113       /* We don't need to preserve changes in the types minimum or
114 	 maximum value in general as these do not generate code
115 	 unless the types precisions are different.  */
116       return true;
117     }
118 
119   /* Scalar floating point types with the same mode are compatible.  */
120   else if (SCALAR_FLOAT_TYPE_P (inner_type)
121 	   && SCALAR_FLOAT_TYPE_P (outer_type))
122     return true;
123 
124   /* Fixed point types with the same mode are compatible.  */
125   else if (FIXED_POINT_TYPE_P (inner_type)
126 	   && FIXED_POINT_TYPE_P (outer_type))
127     return TYPE_SATURATING (inner_type) == TYPE_SATURATING (outer_type);
128 
129   /* We need to take special care recursing to pointed-to types.  */
130   else if (POINTER_TYPE_P (inner_type)
131 	   && POINTER_TYPE_P (outer_type))
132     {
133       /* We do not care for const qualification of the pointed-to types
134 	 as const qualification has no semantic value to the middle-end.  */
135 
136       /* Otherwise pointers/references are equivalent.  */
137       return true;
138     }
139 
140   /* Recurse for complex types.  */
141   else if (TREE_CODE (inner_type) == COMPLEX_TYPE
142 	   && TREE_CODE (outer_type) == COMPLEX_TYPE)
143     return useless_type_conversion_p (TREE_TYPE (outer_type),
144 				      TREE_TYPE (inner_type));
145 
146   /* Recurse for vector types with the same number of subparts.  */
147   else if (TREE_CODE (inner_type) == VECTOR_TYPE
148 	   && TREE_CODE (outer_type) == VECTOR_TYPE
149 	   && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
150     return useless_type_conversion_p (TREE_TYPE (outer_type),
151 				      TREE_TYPE (inner_type));
152 
153   else if (TREE_CODE (inner_type) == ARRAY_TYPE
154 	   && TREE_CODE (outer_type) == ARRAY_TYPE)
155     {
156       /* Preserve various attributes.  */
157       if (TYPE_REVERSE_STORAGE_ORDER (inner_type)
158 	  != TYPE_REVERSE_STORAGE_ORDER (outer_type))
159 	return false;
160       if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
161 	return false;
162 
163       /* Conversions from array types with unknown extent to
164 	 array types with known extent are not useless.  */
165       if (!TYPE_DOMAIN (inner_type) && TYPE_DOMAIN (outer_type))
166 	return false;
167 
168       /* Nor are conversions from array types with non-constant size to
169          array types with constant size or to different size.  */
170       if (TYPE_SIZE (outer_type)
171 	  && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
172 	  && (!TYPE_SIZE (inner_type)
173 	      || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
174 	      || !tree_int_cst_equal (TYPE_SIZE (outer_type),
175 				      TYPE_SIZE (inner_type))))
176 	return false;
177 
178       /* Check conversions between arrays with partially known extents.
179 	 If the array min/max values are constant they have to match.
180 	 Otherwise allow conversions to unknown and variable extents.
181 	 In particular this declares conversions that may change the
182 	 mode to BLKmode as useless.  */
183       if (TYPE_DOMAIN (inner_type)
184 	  && TYPE_DOMAIN (outer_type)
185 	  && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
186 	{
187 	  tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
188 	  tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
189 	  tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
190 	  tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
191 
192 	  /* After gimplification a variable min/max value carries no
193 	     additional information compared to a NULL value.  All that
194 	     matters has been lowered to be part of the IL.  */
195 	  if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
196 	    inner_min = NULL_TREE;
197 	  if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
198 	    outer_min = NULL_TREE;
199 	  if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
200 	    inner_max = NULL_TREE;
201 	  if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
202 	    outer_max = NULL_TREE;
203 
204 	  /* Conversions NULL / variable <- cst are useless, but not
205 	     the other way around.  */
206 	  if (outer_min
207 	      && (!inner_min
208 		  || !tree_int_cst_equal (inner_min, outer_min)))
209 	    return false;
210 	  if (outer_max
211 	      && (!inner_max
212 		  || !tree_int_cst_equal (inner_max, outer_max)))
213 	    return false;
214 	}
215 
216       /* Recurse on the element check.  */
217       return useless_type_conversion_p (TREE_TYPE (outer_type),
218 					TREE_TYPE (inner_type));
219     }
220 
221   else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
222 	    || TREE_CODE (inner_type) == METHOD_TYPE)
223 	   && TREE_CODE (inner_type) == TREE_CODE (outer_type))
224     {
225       tree outer_parm, inner_parm;
226 
227       /* If the return types are not compatible bail out.  */
228       if (!useless_type_conversion_p (TREE_TYPE (outer_type),
229 				      TREE_TYPE (inner_type)))
230 	return false;
231 
232       /* Method types should belong to a compatible base class.  */
233       if (TREE_CODE (inner_type) == METHOD_TYPE
234 	  && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
235 					 TYPE_METHOD_BASETYPE (inner_type)))
236 	return false;
237 
238       /* A conversion to an unprototyped argument list is ok.  */
239       if (!prototype_p (outer_type))
240 	return true;
241 
242       /* If the unqualified argument types are compatible the conversion
243 	 is useless.  */
244       if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
245 	return true;
246 
247       for (outer_parm = TYPE_ARG_TYPES (outer_type),
248 	   inner_parm = TYPE_ARG_TYPES (inner_type);
249 	   outer_parm && inner_parm;
250 	   outer_parm = TREE_CHAIN (outer_parm),
251 	   inner_parm = TREE_CHAIN (inner_parm))
252 	if (!useless_type_conversion_p
253 	       (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
254 		TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
255 	  return false;
256 
257       /* If there is a mismatch in the number of arguments the functions
258 	 are not compatible.  */
259       if (outer_parm || inner_parm)
260 	return false;
261 
262       /* Defer to the target if necessary.  */
263       if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
264 	return comp_type_attributes (outer_type, inner_type) != 0;
265 
266       return true;
267     }
268 
269   /* For aggregates we rely on TYPE_CANONICAL exclusively and require
270      explicit conversions for types involving to be structurally
271      compared types.  */
272   else if (AGGREGATE_TYPE_P (inner_type)
273 	   && TREE_CODE (inner_type) == TREE_CODE (outer_type))
274     return TYPE_CANONICAL (inner_type)
275 	   && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type);
276 
277   else if (TREE_CODE (inner_type) == OFFSET_TYPE
278 	   && TREE_CODE (outer_type) == OFFSET_TYPE)
279     return useless_type_conversion_p (TREE_TYPE (outer_type),
280 				      TREE_TYPE (inner_type))
281 	   && useless_type_conversion_p
282 	        (TYPE_OFFSET_BASETYPE (outer_type),
283 		 TYPE_OFFSET_BASETYPE (inner_type));
284 
285   return false;
286 }
287 
288 
289 /* ----- Decl related -----  */
290 
291 /* Set sequence SEQ to be the GIMPLE body for function FN.  */
292 
293 void
294 gimple_set_body (tree fndecl, gimple_seq seq)
295 {
296   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
297   if (fn == NULL)
298     {
299       /* If FNDECL still does not have a function structure associated
300 	 with it, then it does not make sense for it to receive a
301 	 GIMPLE body.  */
302       gcc_assert (seq == NULL);
303     }
304   else
305     fn->gimple_body = seq;
306 }
307 
308 
309 /* Return the body of GIMPLE statements for function FN.  After the
310    CFG pass, the function body doesn't exist anymore because it has
311    been split up into basic blocks.  In this case, it returns
312    NULL.  */
313 
314 gimple_seq
315 gimple_body (tree fndecl)
316 {
317   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
318   return fn ? fn->gimple_body : NULL;
319 }
320 
321 /* Return true when FNDECL has Gimple body either in unlowered
322    or CFG form.  */
323 bool
324 gimple_has_body_p (tree fndecl)
325 {
326   struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
327   return (gimple_body (fndecl) || (fn && fn->cfg && !(fn->curr_properties & PROP_rtl)));
328 }
329 
330 /* Return a printable name for symbol DECL.  */
331 
332 const char *
333 gimple_decl_printable_name (tree decl, int verbosity)
334 {
335   if (!DECL_NAME (decl))
336     return NULL;
337 
338   if (DECL_ASSEMBLER_NAME_SET_P (decl))
339     {
340       const char *str, *mangled_str;
341       int dmgl_opts = DMGL_NO_OPTS;
342 
343       if (verbosity >= 2)
344 	{
345 	  dmgl_opts = DMGL_VERBOSE
346 		      | DMGL_ANSI
347 		      | DMGL_GNU_V3
348 		      | DMGL_RET_POSTFIX;
349 	  if (TREE_CODE (decl) == FUNCTION_DECL)
350 	    dmgl_opts |= DMGL_PARAMS;
351 	}
352 
353       mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
354       str = cplus_demangle_v3 (mangled_str, dmgl_opts);
355       return (str) ? str : mangled_str;
356     }
357 
358   return IDENTIFIER_POINTER (DECL_NAME (decl));
359 }
360 
361 
362 /* Create a new VAR_DECL and copy information from VAR to it.  */
363 
364 tree
365 copy_var_decl (tree var, tree name, tree type)
366 {
367   tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
368 
369   TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
370   TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
371   DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
372   DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
373   DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
374   DECL_CONTEXT (copy) = DECL_CONTEXT (var);
375   TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
376   TREE_USED (copy) = 1;
377   DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
378   DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
379   if (DECL_USER_ALIGN (var))
380     {
381       SET_DECL_ALIGN (copy, DECL_ALIGN (var));
382       DECL_USER_ALIGN (copy) = 1;
383     }
384 
385   return copy;
386 }
387 
388 /* Strip off a legitimate source ending from the input string NAME of
389    length LEN.  Rather than having to know the names used by all of
390    our front ends, we strip off an ending of a period followed by
391    up to five characters.  (Java uses ".class".)  */
392 
393 static inline void
394 remove_suffix (char *name, int len)
395 {
396   int i;
397 
398   for (i = 2;  i < 8 && len > i;  i++)
399     {
400       if (name[len - i] == '.')
401 	{
402 	  name[len - i] = '\0';
403 	  break;
404 	}
405     }
406 }
407 
408 /* Create a new temporary name with PREFIX.  Return an identifier.  */
409 
410 static GTY(()) unsigned int tmp_var_id_num;
411 
412 tree
413 create_tmp_var_name (const char *prefix)
414 {
415   char *tmp_name;
416 
417   if (prefix)
418     {
419       char *preftmp = ASTRDUP (prefix);
420 
421       remove_suffix (preftmp, strlen (preftmp));
422       clean_symbol_name (preftmp);
423 
424       prefix = preftmp;
425     }
426 
427   ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
428   return get_identifier (tmp_name);
429 }
430 
431 /* Create a new temporary variable declaration of type TYPE.
432    Do NOT push it into the current binding.  */
433 
434 tree
435 create_tmp_var_raw (tree type, const char *prefix)
436 {
437   tree tmp_var;
438 
439   tmp_var = build_decl (input_location,
440 			VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
441 			type);
442 
443   /* The variable was declared by the compiler.  */
444   DECL_ARTIFICIAL (tmp_var) = 1;
445   /* And we don't want debug info for it.  */
446   DECL_IGNORED_P (tmp_var) = 1;
447 
448   /* Make the variable writable.  */
449   TREE_READONLY (tmp_var) = 0;
450 
451   DECL_EXTERNAL (tmp_var) = 0;
452   TREE_STATIC (tmp_var) = 0;
453   TREE_USED (tmp_var) = 1;
454 
455   return tmp_var;
456 }
457 
458 /* Create a new temporary variable declaration of type TYPE.  DO push the
459    variable into the current binding.  Further, assume that this is called
460    only from gimplification or optimization, at which point the creation of
461    certain types are bugs.  */
462 
463 tree
464 create_tmp_var (tree type, const char *prefix)
465 {
466   tree tmp_var;
467 
468   /* We don't allow types that are addressable (meaning we can't make copies),
469      or incomplete.  We also used to reject every variable size objects here,
470      but now support those for which a constant upper bound can be obtained.
471      The processing for variable sizes is performed in gimple_add_tmp_var,
472      point at which it really matters and possibly reached via paths not going
473      through this function, e.g. after direct calls to create_tmp_var_raw.  */
474   gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
475 
476   tmp_var = create_tmp_var_raw (type, prefix);
477   gimple_add_tmp_var (tmp_var);
478   return tmp_var;
479 }
480 
481 /* Create a new temporary variable declaration of type TYPE by calling
482    create_tmp_var and if TYPE is a vector or a complex number, mark the new
483    temporary as gimple register.  */
484 
485 tree
486 create_tmp_reg (tree type, const char *prefix)
487 {
488   tree tmp;
489 
490   tmp = create_tmp_var (type, prefix);
491   if (TREE_CODE (type) == COMPLEX_TYPE
492       || TREE_CODE (type) == VECTOR_TYPE)
493     DECL_GIMPLE_REG_P (tmp) = 1;
494 
495   return tmp;
496 }
497 
498 /* Create a new temporary variable declaration of type TYPE by calling
499    create_tmp_var and if TYPE is a vector or a complex number, mark the new
500    temporary as gimple register.  */
501 
502 tree
503 create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
504 {
505   tree tmp;
506 
507   tmp = create_tmp_var_raw (type, prefix);
508   gimple_add_tmp_var_fn (fn, tmp);
509   if (TREE_CODE (type) == COMPLEX_TYPE
510       || TREE_CODE (type) == VECTOR_TYPE)
511     DECL_GIMPLE_REG_P (tmp) = 1;
512 
513   return tmp;
514 }
515 
516 
517 /* ----- Expression related -----  */
518 
519 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
520    *OP1_P, *OP2_P and *OP3_P respectively.  */
521 
522 void
523 extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
524 		       tree *op2_p, tree *op3_p)
525 {
526   enum gimple_rhs_class grhs_class;
527 
528   *subcode_p = TREE_CODE (expr);
529   grhs_class = get_gimple_rhs_class (*subcode_p);
530 
531   if (grhs_class == GIMPLE_TERNARY_RHS)
532     {
533       *op1_p = TREE_OPERAND (expr, 0);
534       *op2_p = TREE_OPERAND (expr, 1);
535       *op3_p = TREE_OPERAND (expr, 2);
536     }
537   else if (grhs_class == GIMPLE_BINARY_RHS)
538     {
539       *op1_p = TREE_OPERAND (expr, 0);
540       *op2_p = TREE_OPERAND (expr, 1);
541       *op3_p = NULL_TREE;
542     }
543   else if (grhs_class == GIMPLE_UNARY_RHS)
544     {
545       *op1_p = TREE_OPERAND (expr, 0);
546       *op2_p = NULL_TREE;
547       *op3_p = NULL_TREE;
548     }
549   else if (grhs_class == GIMPLE_SINGLE_RHS)
550     {
551       *op1_p = expr;
552       *op2_p = NULL_TREE;
553       *op3_p = NULL_TREE;
554     }
555   else
556     gcc_unreachable ();
557 }
558 
559 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND.  */
560 
561 void
562 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
563                                tree *lhs_p, tree *rhs_p)
564 {
565   gcc_assert (COMPARISON_CLASS_P (cond)
566 	      || TREE_CODE (cond) == TRUTH_NOT_EXPR
567 	      || is_gimple_min_invariant (cond)
568 	      || SSA_VAR_P (cond));
569 
570   extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
571 
572   /* Canonicalize conditionals of the form 'if (!VAL)'.  */
573   if (*code_p == TRUTH_NOT_EXPR)
574     {
575       *code_p = EQ_EXPR;
576       gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
577       *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
578     }
579   /* Canonicalize conditionals of the form 'if (VAL)'  */
580   else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
581     {
582       *code_p = NE_EXPR;
583       gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
584       *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
585     }
586 }
587 
588 /*  Return true if T is a valid LHS for a GIMPLE assignment expression.  */
589 
590 bool
591 is_gimple_lvalue (tree t)
592 {
593   return (is_gimple_addressable (t)
594 	  || TREE_CODE (t) == WITH_SIZE_EXPR
595 	  /* These are complex lvalues, but don't have addresses, so they
596 	     go here.  */
597 	  || TREE_CODE (t) == BIT_FIELD_REF);
598 }
599 
600 /*  Return true if T is a GIMPLE condition.  */
601 
602 bool
603 is_gimple_condexpr (tree t)
604 {
605   return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
606 				&& !tree_could_throw_p (t)
607 				&& is_gimple_val (TREE_OPERAND (t, 0))
608 				&& is_gimple_val (TREE_OPERAND (t, 1))));
609 }
610 
611 /* Return true if T is a gimple address.  */
612 
613 bool
614 is_gimple_address (const_tree t)
615 {
616   tree op;
617 
618   if (TREE_CODE (t) != ADDR_EXPR)
619     return false;
620 
621   op = TREE_OPERAND (t, 0);
622   while (handled_component_p (op))
623     {
624       if ((TREE_CODE (op) == ARRAY_REF
625 	   || TREE_CODE (op) == ARRAY_RANGE_REF)
626 	  && !is_gimple_val (TREE_OPERAND (op, 1)))
627 	    return false;
628 
629       op = TREE_OPERAND (op, 0);
630     }
631 
632   if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
633     return true;
634 
635   switch (TREE_CODE (op))
636     {
637     case PARM_DECL:
638     case RESULT_DECL:
639     case LABEL_DECL:
640     case FUNCTION_DECL:
641     case VAR_DECL:
642     case CONST_DECL:
643       return true;
644 
645     default:
646       return false;
647     }
648 }
649 
650 /* Return true if T is a gimple invariant address.  */
651 
652 bool
653 is_gimple_invariant_address (const_tree t)
654 {
655   const_tree op;
656 
657   if (TREE_CODE (t) != ADDR_EXPR)
658     return false;
659 
660   op = strip_invariant_refs (TREE_OPERAND (t, 0));
661   if (!op)
662     return false;
663 
664   if (TREE_CODE (op) == MEM_REF)
665     {
666       const_tree op0 = TREE_OPERAND (op, 0);
667       return (TREE_CODE (op0) == ADDR_EXPR
668 	      && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
669 		  || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
670     }
671 
672   return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
673 }
674 
675 /* Return true if T is a gimple invariant address at IPA level
676    (so addresses of variables on stack are not allowed).  */
677 
678 bool
679 is_gimple_ip_invariant_address (const_tree t)
680 {
681   const_tree op;
682 
683   if (TREE_CODE (t) != ADDR_EXPR)
684     return false;
685 
686   op = strip_invariant_refs (TREE_OPERAND (t, 0));
687   if (!op)
688     return false;
689 
690   if (TREE_CODE (op) == MEM_REF)
691     {
692       const_tree op0 = TREE_OPERAND (op, 0);
693       return (TREE_CODE (op0) == ADDR_EXPR
694 	      && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
695 		  || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
696     }
697 
698   return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
699 }
700 
701 /* Return true if T is a GIMPLE minimal invariant.  It's a restricted
702    form of function invariant.  */
703 
704 bool
705 is_gimple_min_invariant (const_tree t)
706 {
707   if (TREE_CODE (t) == ADDR_EXPR)
708     return is_gimple_invariant_address (t);
709 
710   return is_gimple_constant (t);
711 }
712 
713 /* Return true if T is a GIMPLE interprocedural invariant.  It's a restricted
714    form of gimple minimal invariant.  */
715 
716 bool
717 is_gimple_ip_invariant (const_tree t)
718 {
719   if (TREE_CODE (t) == ADDR_EXPR)
720     return is_gimple_ip_invariant_address (t);
721 
722   return is_gimple_constant (t);
723 }
724 
725 /* Return true if T is a non-aggregate register variable.  */
726 
727 bool
728 is_gimple_reg (tree t)
729 {
730   if (virtual_operand_p (t))
731     return false;
732 
733   if (TREE_CODE (t) == SSA_NAME)
734     return true;
735 
736   if (!is_gimple_variable (t))
737     return false;
738 
739   if (!is_gimple_reg_type (TREE_TYPE (t)))
740     return false;
741 
742   /* A volatile decl is not acceptable because we can't reuse it as
743      needed.  We need to copy it into a temp first.  */
744   if (TREE_THIS_VOLATILE (t))
745     return false;
746 
747   /* We define "registers" as things that can be renamed as needed,
748      which with our infrastructure does not apply to memory.  */
749   if (needs_to_live_in_memory (t))
750     return false;
751 
752   /* Hard register variables are an interesting case.  For those that
753      are call-clobbered, we don't know where all the calls are, since
754      we don't (want to) take into account which operations will turn
755      into libcalls at the rtl level.  For those that are call-saved,
756      we don't currently model the fact that calls may in fact change
757      global hard registers, nor do we examine ASM_CLOBBERS at the tree
758      level, and so miss variable changes that might imply.  All around,
759      it seems safest to not do too much optimization with these at the
760      tree level at all.  We'll have to rely on the rtl optimizers to
761      clean this up, as there we've got all the appropriate bits exposed.  */
762   if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
763     return false;
764 
765   /* Complex and vector values must have been put into SSA-like form.
766      That is, no assignments to the individual components.  */
767   if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
768       || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
769     return DECL_GIMPLE_REG_P (t);
770 
771   return true;
772 }
773 
774 
775 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant.  */
776 
777 bool
778 is_gimple_val (tree t)
779 {
780   /* Make loads from volatiles and memory vars explicit.  */
781   if (is_gimple_variable (t)
782       && is_gimple_reg_type (TREE_TYPE (t))
783       && !is_gimple_reg (t))
784     return false;
785 
786   return (is_gimple_variable (t) || is_gimple_min_invariant (t));
787 }
788 
789 /* Similarly, but accept hard registers as inputs to asm statements.  */
790 
791 bool
792 is_gimple_asm_val (tree t)
793 {
794   if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
795     return true;
796 
797   return is_gimple_val (t);
798 }
799 
800 /* Return true if T is a GIMPLE minimal lvalue.  */
801 
802 bool
803 is_gimple_min_lval (tree t)
804 {
805   if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
806     return false;
807   return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
808 }
809 
810 /* Return true if T is a valid function operand of a CALL_EXPR.  */
811 
812 bool
813 is_gimple_call_addr (tree t)
814 {
815   return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
816 }
817 
818 /* Return true if T is a valid address operand of a MEM_REF.  */
819 
820 bool
821 is_gimple_mem_ref_addr (tree t)
822 {
823   return (is_gimple_reg (t)
824 	  || TREE_CODE (t) == INTEGER_CST
825 	  || (TREE_CODE (t) == ADDR_EXPR
826 	      && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
827 		  || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
828 }
829 
830 /* Hold trees marked addressable during expand.  */
831 
832 static hash_set<tree> *mark_addressable_queue;
833 
834 /* Mark X as addressable or queue it up if called during expand.  We
835    don't want to apply it immediately during expand because decls are
836    made addressable at that point due to RTL-only concerns, such as
837    uses of memcpy for block moves, and TREE_ADDRESSABLE changes
838    is_gimple_reg, which might make it seem like a variable that used
839    to be a gimple_reg shouldn't have been an SSA name.  So we queue up
840    this flag setting and only apply it when we're done with GIMPLE and
841    only RTL issues matter.  */
842 
843 static void
844 mark_addressable_1 (tree x)
845 {
846   if (!currently_expanding_to_rtl)
847     {
848       TREE_ADDRESSABLE (x) = 1;
849       return;
850     }
851 
852   if (!mark_addressable_queue)
853     mark_addressable_queue = new hash_set<tree>();
854   mark_addressable_queue->add (x);
855 }
856 
857 /* Adaptor for mark_addressable_1 for use in hash_set traversal.  */
858 
859 bool
860 mark_addressable_2 (tree const &x, void * ATTRIBUTE_UNUSED = NULL)
861 {
862   mark_addressable_1 (x);
863   return false;
864 }
865 
866 /* Mark all queued trees as addressable, and empty the queue.  To be
867    called right after clearing CURRENTLY_EXPANDING_TO_RTL.  */
868 
869 void
870 flush_mark_addressable_queue ()
871 {
872   gcc_assert (!currently_expanding_to_rtl);
873   if (mark_addressable_queue)
874     {
875       mark_addressable_queue->traverse<void*, mark_addressable_2> (NULL);
876       delete mark_addressable_queue;
877       mark_addressable_queue = NULL;
878     }
879 }
880 
881 /* Mark X addressable.  Unlike the langhook we expect X to be in gimple
882    form and we don't do any syntax checking.  */
883 
884 void
885 mark_addressable (tree x)
886 {
887   while (handled_component_p (x))
888     x = TREE_OPERAND (x, 0);
889   if (TREE_CODE (x) == MEM_REF
890       && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
891     x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
892   if (!VAR_P (x)
893       && TREE_CODE (x) != PARM_DECL
894       && TREE_CODE (x) != RESULT_DECL)
895     return;
896   mark_addressable_1 (x);
897 
898   /* Also mark the artificial SSA_NAME that points to the partition of X.  */
899   if (TREE_CODE (x) == VAR_DECL
900       && !DECL_EXTERNAL (x)
901       && !TREE_STATIC (x)
902       && cfun->gimple_df != NULL
903       && cfun->gimple_df->decls_to_pointers != NULL)
904     {
905       tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
906       if (namep)
907 	mark_addressable_1 (*namep);
908     }
909 }
910 
911 /* Returns true iff T is a valid RHS for an assignment to a renamed
912    user -- or front-end generated artificial -- variable.  */
913 
914 bool
915 is_gimple_reg_rhs (tree t)
916 {
917   return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
918 }
919 
920 #include "gt-gimple-expr.h"
921