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