1 /* Functions to analyze and validate GIMPLE trees.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4 Rewritten by Jason Merrill <jason@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "coretypes.h"
29 #include "tree-gimple.h"
35 /* GCC GIMPLE structure
37 Inspired by the SIMPLE C grammar at
39 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
43 DECL_SAVED_TREE -> block
46 BIND_EXPR_VARS -> DECL chain
47 BIND_EXPR_BLOCK -> BLOCK
48 BIND_EXPR_BODY -> compound-stmt
51 op0 -> non-compound-stmt
54 (or other alternate solution)
55 stmt: compound-stmt | non-compound-stmt
74 op2 -> array of case labels (as LABEL_DECLs?)
75 FIXME: add case value info
76 The SWITCH_LABELS (op2) are sorted in ascending order, and the
77 last label in the vector is always the default case.
80 op0 -> LABEL_DECL | '*' ID
84 | MODIFY_EXPR -> RESULT_DECL, varname
85 | THROW_EXPR? do we need/want such a thing for opts, perhaps
86 to generate an ERT_THROW region? I think so.
87 Hmm...this would only work at the GIMPLE level, where we know that
88 the call args don't have any EH impact. Perhaps
89 annotation of the CALL_EXPR would work better.
95 CASE_LOW -> val | NULL_TREE
96 CASE_HIGH -> val | NULL_TREE
97 CASE_LABEL -> LABEL_DECL FIXME
117 addr-expr-arg : compref | ID
118 lhs: addr-expr-arg | '*' ID | bitfieldref
119 min-lval: ID | '*' ID
143 inner_compref : compref | min_lval
151 condition : val | val relop val
154 rhs : varname | CONST
162 (cast here stands for all valid C typecasts)
192 static inline bool is_gimple_id (tree);
194 /* Validation of GIMPLE expressions. */
196 /* Return nonzero if T is a GIMPLE RHS:
198 rhs : varname | CONST
200 | '&' varname_or_temp
205 | <CONSTRUCTOR <gimple_val ...>>
207 The last option is only valid GIMPLE for vector and complex types;
208 aggregate types should have their constructors decomposed. */
211 is_gimple_rhs (tree t)
213 enum tree_code code = TREE_CODE (t);
215 switch (TREE_CODE_CLASS (code))
236 /* FIXME lower VA_ARG_EXPR. */
249 if (is_gimple_lvalue (t) || is_gimple_val (t))
255 /* Returns nonzero if T is a valid CONSTRUCTOR component in GIMPLE, either
256 a val or another CONSTRUCTOR. */
259 is_gimple_constructor_elt (tree t)
261 return (is_gimple_val (t)
262 || TREE_CODE (t) == CONSTRUCTOR);
265 /* Return nonzero if T is a valid LHS for a GIMPLE assignment expression. */
268 is_gimple_lvalue (tree t)
270 return (is_gimple_addr_expr_arg (t)
271 || TREE_CODE (t) == INDIRECT_REF
272 /* These are complex lvalues, but don't have addresses, so they
274 || TREE_CODE (t) == BIT_FIELD_REF);
278 /* Return nonzero if T is a GIMPLE condition:
285 is_gimple_condexpr (tree t)
287 return (is_gimple_val (t)
288 || TREE_CODE_CLASS (TREE_CODE (t)) == '<');
292 /* Return nonzero if T is a valid operand for '&':
300 is_gimple_addr_expr_arg (tree t)
302 return (is_gimple_id (t)
303 || TREE_CODE (t) == ARRAY_REF
304 || TREE_CODE (t) == ARRAY_RANGE_REF
305 || TREE_CODE (t) == COMPONENT_REF
306 || TREE_CODE (t) == REALPART_EXPR
307 || TREE_CODE (t) == IMAGPART_EXPR
308 || TREE_CODE (t) == INDIRECT_REF);
311 /* Return nonzero if T is function invariant. Or rather a restricted
312 form of function invariant. */
315 is_gimple_min_invariant (tree t)
317 switch (TREE_CODE (t))
320 return TREE_INVARIANT (t);
327 return !TREE_OVERFLOW (t);
334 /* Return nonzero if T looks like a valid GIMPLE statement. */
337 is_gimple_stmt (tree t)
339 enum tree_code code = TREE_CODE (t);
341 if (IS_EMPTY_STMT (t))
348 /* These are only valid if they're void. */
349 return VOID_TYPE_P (TREE_TYPE (t));
355 case CASE_LABEL_EXPR:
357 case TRY_FINALLY_EXPR:
364 /* These are always void. */
368 /* FIXME this should be lowered. */
372 /* FIXME should we work harder to make COMPOUND_EXPRs void? */
375 /* These are valid regardless of their type. */
383 /* Return nonzero if T is a variable. */
386 is_gimple_variable (tree t)
388 return (TREE_CODE (t) == VAR_DECL
389 || TREE_CODE (t) == PARM_DECL
390 || TREE_CODE (t) == RESULT_DECL
391 || TREE_CODE (t) == SSA_NAME);
394 /* Return nonzero if T is a GIMPLE identifier (something with an address). */
397 is_gimple_id (tree t)
399 return (is_gimple_variable (t)
400 || TREE_CODE (t) == FUNCTION_DECL
401 || TREE_CODE (t) == LABEL_DECL
402 /* Allow string constants, since they are addressable. */
403 || TREE_CODE (t) == STRING_CST);
406 /* Return nonzero if TYPE is a suitable type for a scalar register
410 is_gimple_reg_type (tree type)
412 return (!AGGREGATE_TYPE_P (type)
413 && TREE_CODE (type) != COMPLEX_TYPE);
417 /* Return nonzero if T is a scalar register variable. */
420 is_gimple_reg (tree t)
422 if (TREE_CODE (t) == SSA_NAME)
423 t = SSA_NAME_VAR (t);
425 return (is_gimple_variable (t)
426 && is_gimple_reg_type (TREE_TYPE (t))
427 /* A volatile decl is not acceptable because we can't reuse it as
428 needed. We need to copy it into a temp first. */
429 && ! TREE_THIS_VOLATILE (t)
430 && ! TREE_ADDRESSABLE (t)
431 && ! needs_to_live_in_memory (t));
434 /* Return nonzero if T is a GIMPLE variable whose address is not needed. */
437 is_gimple_non_addressable (tree t)
439 if (TREE_CODE (t) == SSA_NAME)
440 t = SSA_NAME_VAR (t);
442 return (is_gimple_variable (t)
443 && ! TREE_ADDRESSABLE (t)
444 && ! needs_to_live_in_memory (t));
447 /* Return nonzero if T is a GIMPLE rvalue, i.e. an identifier or a
451 is_gimple_val (tree t)
453 /* Make loads from volatiles and memory vars explicit. */
454 if (is_gimple_variable (t)
455 && is_gimple_reg_type (TREE_TYPE (t))
456 && !is_gimple_reg (t))
459 /* FIXME make these decls. That can happen only when we expose the
460 entire landing-pad construct at the tree level. */
461 if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
464 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
468 /* Return true if T is a GIMPLE minimal lvalue, of the form
470 min_lval: ID | '(' '*' ID ')'
472 This never actually appears in the original SIMPLE grammar, but is
473 repeated in several places. */
476 is_gimple_min_lval (tree t)
478 return (is_gimple_id (t)
479 || TREE_CODE (t) == INDIRECT_REF);
482 /* Return nonzero if T is a typecast operation of the form
486 is_gimple_cast (tree t)
488 return (TREE_CODE (t) == NOP_EXPR
489 || TREE_CODE (t) == CONVERT_EXPR
490 || TREE_CODE (t) == FIX_TRUNC_EXPR
491 || TREE_CODE (t) == FIX_CEIL_EXPR
492 || TREE_CODE (t) == FIX_FLOOR_EXPR
493 || TREE_CODE (t) == FIX_ROUND_EXPR);
497 /* If T makes a function call, return the corresponding CALL_EXPR operand.
498 Otherwise, return NULL_TREE. */
501 get_call_expr_in (tree t)
503 if (TREE_CODE (t) == CALL_EXPR)
505 else if (TREE_CODE (t) == MODIFY_EXPR
506 && TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR)
507 return TREE_OPERAND (t, 1);
508 else if (TREE_CODE (t) == RETURN_EXPR
509 && TREE_OPERAND (t, 0)
510 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
511 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == CALL_EXPR)
512 return TREE_OPERAND (TREE_OPERAND (t, 0), 1);
518 /* Given an _EXPR TOP, reorganize all of the nested _EXPRs with the same
519 code so that they only appear as the second operand. This should only
520 be used for tree codes which are truly associative, such as
521 COMPOUND_EXPR and TRUTH_ANDIF_EXPR. Arithmetic is not associative
522 enough, due to the limited precision of arithmetic data types.
524 This transformation is conservative; the operand 0 of a matching tree
525 node will only change if it is also a matching node. */
528 right_assocify_expr (tree top)
531 enum tree_code code = TREE_CODE (*p);
532 while (TREE_CODE (*p) == code)
535 tree lhs = TREE_OPERAND (cur, 0);
536 if (TREE_CODE (lhs) == code)
538 /* There's a left-recursion. If we have ((a, (b, c)), d), we
539 want to rearrange to (a, (b, (c, d))). */
542 /* Replace cur with the lhs; move (a, *) up. */
545 if (code == COMPOUND_EXPR)
547 /* We need to give (b, c) the type of c; previously lhs had
549 TREE_TYPE (lhs) = TREE_TYPE (cur);
550 if (TREE_SIDE_EFFECTS (cur))
551 TREE_SIDE_EFFECTS (lhs) = 1;
554 /* Walk through the op1 chain from there until we find something
555 with a different code. In this case, c. */
556 for (q = &TREE_OPERAND (lhs, 1); TREE_CODE (*q) == code;
557 q = &TREE_OPERAND (*q, 1))
558 TREE_TYPE (*q) = TREE_TYPE (cur);
560 /* Change (*, d) into (c, d). */
561 TREE_OPERAND (cur, 0) = *q;
563 /* And plug it in where c used to be. */
567 p = &TREE_OPERAND (cur, 1);
572 /* Normalize the statement TOP. If it is a COMPOUND_EXPR, reorganize it so
573 that we can traverse it without recursion. If it is null, replace it
577 rationalize_compound_expr (tree top)
579 if (top == NULL_TREE)
580 top = build_empty_stmt ();
581 else if (TREE_CODE (top) == COMPOUND_EXPR)
582 top = right_assocify_expr (top);
587 /* Given a memory reference expression, return the base address. Note that,
588 in contrast with get_base_var, this will not recurse inside INDIRECT_REF
589 expressions. Therefore, given the reference PTR->FIELD, this function
590 will return *PTR. Whereas get_base_var would've returned PTR. */
593 get_base_address (tree t)
598 || TREE_CODE (t) == STRING_CST
599 || TREE_CODE (t) == CONSTRUCTOR
600 || TREE_CODE (t) == INDIRECT_REF)
603 if (TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR)
604 t = TREE_OPERAND (t, 0);
605 else if (handled_component_p (t))
606 t = get_base_address (TREE_OPERAND (t, 0));
617 recalculate_side_effects (tree t)
619 enum tree_code code = TREE_CODE (t);
620 int fro = first_rtl_op (code);
623 switch (TREE_CODE_CLASS (code))
632 case PREDECREMENT_EXPR:
633 case PREINCREMENT_EXPR:
634 case POSTDECREMENT_EXPR:
635 case POSTINCREMENT_EXPR:
636 /* All of these have side-effects, no matter what their
645 case '<': /* a comparison expression */
646 case '1': /* a unary arithmetic expression */
647 case '2': /* a binary arithmetic expression */
648 case 'r': /* a reference */
649 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
650 for (i = 0; i < fro; ++i)
652 tree op = TREE_OPERAND (t, i);
653 if (op && TREE_SIDE_EFFECTS (op))
654 TREE_SIDE_EFFECTS (t) = 1;