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
78 op0 -> LABEL_DECL | '*' ID
80 op0 -> modify-stmt | NULL_TREE
81 (maybe -> RESULT_DECL | NULL_TREE? seems like some of expand_return
82 depends on getting a MODIFY_EXPR.)
83 | THROW_EXPR? do we need/want such a thing for opts, perhaps
84 to generate an ERT_THROW region? I think so.
85 Hmm...this would only work at the GIMPLE level, where we know that
86 the call args don't have any EH impact. Perhaps
87 annotation of the CALL_EXPR would work better.
93 CASE_LOW -> val | NULL_TREE
94 CASE_HIGH -> val | NULL_TREE
95 CASE_LABEL -> LABEL_DECL FIXME
115 addr-expr-arg : compref | ID
116 lhs: addr-expr-arg | '*' ID | bitfieldref
117 min-lval: ID | '*' ID
120 op0 -> compref | min-lval
125 op0 -> compref | min-lval
127 op0 -> compref | min-lval
132 condition : val | val relop val
135 rhs : varname | CONST
143 (cast here stands for all valid C typecasts)
173 static inline bool is_gimple_id (tree
);
175 /* Validation of GIMPLE expressions. */
177 /* Return nonzero if T is a GIMPLE RHS:
179 rhs : varname | CONST
181 | '&' varname_or_temp
186 | <CONSTRUCTOR <gimple_val ...>>
188 The last option is only valid GIMPLE for vector and complex types;
189 aggregate types should have their constructors decomposed. */
192 is_gimple_rhs (tree t
)
194 enum tree_code code
= TREE_CODE (t
);
196 switch (TREE_CODE_CLASS (code
))
217 /* FIXME lower VA_ARG_EXPR. */
230 if (is_gimple_lvalue (t
) || is_gimple_val (t
))
236 /* Returns nonzero if T is a valid CONSTRUCTOR component in GIMPLE, either
237 a val or another CONSTRUCTOR. */
240 is_gimple_constructor_elt (tree t
)
242 return (is_gimple_val (t
)
243 || TREE_CODE (t
) == CONSTRUCTOR
);
246 /* Return nonzero if T is a valid LHS for a GIMPLE assignment expression. */
249 is_gimple_lvalue (tree t
)
251 return (is_gimple_addr_expr_arg (t
)
252 || TREE_CODE (t
) == INDIRECT_REF
253 /* These are complex lvalues, but don't have addresses, so they
255 || TREE_CODE (t
) == BIT_FIELD_REF
);
259 /* Return nonzero if T is a GIMPLE condition:
266 is_gimple_condexpr (tree t
)
268 return (is_gimple_val (t
)
269 || TREE_CODE_CLASS (TREE_CODE (t
)) == '<');
273 /* Return nonzero if T is a valid operand for '&':
281 is_gimple_addr_expr_arg (tree t
)
283 return (is_gimple_id (t
)
284 || TREE_CODE (t
) == ARRAY_REF
285 || TREE_CODE (t
) == COMPONENT_REF
286 || TREE_CODE (t
) == REALPART_EXPR
287 || TREE_CODE (t
) == IMAGPART_EXPR
);
290 /* Return nonzero if T is function invariant. Or rather a restricted
291 form of function invariant. */
294 is_gimple_min_invariant (tree t
)
296 switch (TREE_CODE (t
))
299 return TREE_INVARIANT (t
);
306 return !TREE_OVERFLOW (t
);
313 /* Return nonzero if T looks like a valid GIMPLE statement. */
316 is_gimple_stmt (tree t
)
318 enum tree_code code
= TREE_CODE (t
);
320 if (IS_EMPTY_STMT (t
))
327 /* These are only valid if they're void. */
328 return VOID_TYPE_P (TREE_TYPE (t
));
334 case CASE_LABEL_EXPR
:
336 case TRY_FINALLY_EXPR
:
343 /* These are always void. */
347 /* FIXME this should be lowered. */
351 /* FIXME should we work harder to make COMPOUND_EXPRs void? */
354 /* These are valid regardless of their type. */
362 /* Return nonzero if T is a variable. */
365 is_gimple_variable (tree t
)
367 return (TREE_CODE (t
) == VAR_DECL
368 || TREE_CODE (t
) == PARM_DECL
369 || TREE_CODE (t
) == RESULT_DECL
370 || TREE_CODE (t
) == SSA_NAME
);
373 /* Return nonzero if T is a GIMPLE identifier (something with an address). */
376 is_gimple_id (tree t
)
378 return (is_gimple_variable (t
)
379 || TREE_CODE (t
) == FUNCTION_DECL
380 || TREE_CODE (t
) == LABEL_DECL
381 /* Allow string constants, since they are addressable. */
382 || TREE_CODE (t
) == STRING_CST
);
385 /* Return nonzero if TYPE is a suitable type for a scalar register
389 is_gimple_reg_type (tree type
)
391 return (!AGGREGATE_TYPE_P (type
)
392 && TREE_CODE (type
) != COMPLEX_TYPE
);
396 /* Return nonzero if T is a scalar register variable. */
399 is_gimple_reg (tree t
)
401 if (TREE_CODE (t
) == SSA_NAME
)
402 t
= SSA_NAME_VAR (t
);
404 return (is_gimple_variable (t
)
405 && is_gimple_reg_type (TREE_TYPE (t
))
406 /* A volatile decl is not acceptable because we can't reuse it as
407 needed. We need to copy it into a temp first. */
408 && ! TREE_THIS_VOLATILE (t
)
409 && ! TREE_ADDRESSABLE (t
)
410 && ! needs_to_live_in_memory (t
));
413 /* Return nonzero if T is a GIMPLE variable whose address is not needed. */
416 is_gimple_non_addressable (tree t
)
418 if (TREE_CODE (t
) == SSA_NAME
)
419 t
= SSA_NAME_VAR (t
);
421 return (is_gimple_variable (t
)
422 && ! TREE_ADDRESSABLE (t
)
423 && ! needs_to_live_in_memory (t
));
426 /* Return nonzero if T is a GIMPLE rvalue, i.e. an identifier or a
430 is_gimple_val (tree t
)
432 /* Make loads from volatiles and memory vars explicit. */
433 if (is_gimple_variable (t
)
434 && is_gimple_reg_type (TREE_TYPE (t
))
435 && !is_gimple_reg (t
))
438 /* FIXME make these decls. That can happen only when we expose the
439 entire landing-pad construct at the tree level. */
440 if (TREE_CODE (t
) == EXC_PTR_EXPR
|| TREE_CODE (t
) == FILTER_EXPR
)
443 return (is_gimple_variable (t
) || is_gimple_min_invariant (t
));
447 /* Return true if T is a GIMPLE minimal lvalue, of the form
449 min_lval: ID | '(' '*' ID ')'
451 This never actually appears in the original SIMPLE grammar, but is
452 repeated in several places. */
455 is_gimple_min_lval (tree t
)
457 return (is_gimple_id (t
)
458 || TREE_CODE (t
) == INDIRECT_REF
);
461 /* Return nonzero if T is a typecast operation of the form
465 is_gimple_cast (tree t
)
467 return (TREE_CODE (t
) == NOP_EXPR
468 || TREE_CODE (t
) == CONVERT_EXPR
469 || TREE_CODE (t
) == FIX_TRUNC_EXPR
470 || TREE_CODE (t
) == FIX_CEIL_EXPR
471 || TREE_CODE (t
) == FIX_FLOOR_EXPR
472 || TREE_CODE (t
) == FIX_ROUND_EXPR
);
476 /* If T makes a function call, return the corresponding CALL_EXPR operand.
477 Otherwise, return NULL_TREE. */
480 get_call_expr_in (tree t
)
482 if (TREE_CODE (t
) == CALL_EXPR
)
484 else if (TREE_CODE (t
) == MODIFY_EXPR
485 && TREE_CODE (TREE_OPERAND (t
, 1)) == CALL_EXPR
)
486 return TREE_OPERAND (t
, 1);
487 else if (TREE_CODE (t
) == RETURN_EXPR
488 && TREE_OPERAND (t
, 0)
489 && TREE_CODE (TREE_OPERAND (t
, 0)) == MODIFY_EXPR
490 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == CALL_EXPR
)
491 return TREE_OPERAND (TREE_OPERAND (t
, 0), 1);
497 /* Given an _EXPR TOP, reorganize all of the nested _EXPRs with the same
498 code so that they only appear as the second operand. This should only
499 be used for tree codes which are truly associative, such as
500 COMPOUND_EXPR and TRUTH_ANDIF_EXPR. Arithmetic is not associative
501 enough, due to the limited precision of arithmetic data types.
503 This transformation is conservative; the operand 0 of a matching tree
504 node will only change if it is also a matching node. */
507 right_assocify_expr (tree top
)
510 enum tree_code code
= TREE_CODE (*p
);
511 while (TREE_CODE (*p
) == code
)
514 tree lhs
= TREE_OPERAND (cur
, 0);
515 if (TREE_CODE (lhs
) == code
)
517 /* There's a left-recursion. If we have ((a, (b, c)), d), we
518 want to rearrange to (a, (b, (c, d))). */
521 /* Replace cur with the lhs; move (a, *) up. */
524 if (code
== COMPOUND_EXPR
)
526 /* We need to give (b, c) the type of c; previously lhs had
528 TREE_TYPE (lhs
) = TREE_TYPE (cur
);
529 if (TREE_SIDE_EFFECTS (cur
))
530 TREE_SIDE_EFFECTS (lhs
) = 1;
533 /* Walk through the op1 chain from there until we find something
534 with a different code. In this case, c. */
535 for (q
= &TREE_OPERAND (lhs
, 1); TREE_CODE (*q
) == code
;
536 q
= &TREE_OPERAND (*q
, 1))
537 TREE_TYPE (*q
) = TREE_TYPE (cur
);
539 /* Change (*, d) into (c, d). */
540 TREE_OPERAND (cur
, 0) = *q
;
542 /* And plug it in where c used to be. */
546 p
= &TREE_OPERAND (cur
, 1);
551 /* Normalize the statement TOP. If it is a COMPOUND_EXPR, reorganize it so
552 that we can traverse it without recursion. If it is null, replace it
556 rationalize_compound_expr (tree top
)
558 if (top
== NULL_TREE
)
559 top
= build_empty_stmt ();
560 else if (TREE_CODE (top
) == COMPOUND_EXPR
)
561 top
= right_assocify_expr (top
);
566 /* Given a memory reference expression, return the base address. Note that,
567 in contrast with get_base_var, this will not recurse inside INDIRECT_REF
568 expressions. Therefore, given the reference PTR->FIELD, this function
569 will return *PTR. Whereas get_base_var would've returned PTR. */
572 get_base_address (tree t
)
577 || TREE_CODE (t
) == INDIRECT_REF
)
580 switch (TREE_CODE (t
))
587 t
= TREE_OPERAND (t
, 0);
601 recalculate_side_effects (tree t
)
603 enum tree_code code
= TREE_CODE (t
);
604 int fro
= first_rtl_op (code
);
607 switch (TREE_CODE_CLASS (code
))
616 case PREDECREMENT_EXPR
:
617 case PREINCREMENT_EXPR
:
618 case POSTDECREMENT_EXPR
:
619 case POSTINCREMENT_EXPR
:
620 /* All of these have side-effects, no matter what their
629 case '<': /* a comparison expression */
630 case '1': /* a unary arithmetic expression */
631 case '2': /* a binary arithmetic expression */
632 case 'r': /* a reference */
633 TREE_SIDE_EFFECTS (t
) = TREE_THIS_VOLATILE (t
);
634 for (i
= 0; i
< fro
; ++i
)
636 tree op
= TREE_OPERAND (t
, i
);
637 if (op
&& TREE_SIDE_EFFECTS (op
))
638 TREE_SIDE_EFFECTS (t
) = 1;