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