1 /* Functions to analyze and validate GIMPLE trees.
2 Copyright (C) 2002, 2003, 2004 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"
30 #include "tree-flow.h"
36 /* GCC GIMPLE structure
38 Inspired by the SIMPLE C grammar at
40 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
42 function : FUNCTION_DECL
43 DECL_SAVED_TREE -> compound-stmt
45 compound-stmt: STATEMENT_LIST
60 BIND_EXPR_VARS -> chain of DECLs
61 BIND_EXPR_BLOCK -> BLOCK
62 BIND_EXPR_BODY -> compound-stmt
69 switch-stmt : SWITCH_EXPR
72 op2 -> TREE_VEC of CASE_LABEL_EXPRs
73 The CASE_LABEL_EXPRs are sorted by CASE_LOW,
77 op0 -> LABEL_DECL | val
79 return-stmt : RETURN_EXPR
90 label-stmt : LABEL_EXPR
93 try-stmt : TRY_CATCH_EXPR
104 catch-seq : STATEMENT_LIST
105 members -> CATCH_EXPR
107 modify-stmt : MODIFY_EXPR
111 call-stmt : CALL_EXPR
112 op0 -> val | OBJ_TYPE_REF
115 call-arg-list: TREE_LIST
121 with-size-arg: addr-expr-arg
125 indirectref : INDIRECT_REF
138 bitfieldref : BIT_FIELD_REF
143 compref : inner-compref
149 inner-compref: min-lval
190 static inline bool is_gimple_id (tree);
192 /* Validation of GIMPLE expressions. */
194 /* Return true if T is a GIMPLE RHS for an assignment to a temporary. */
197 is_gimple_tmp_rhs (tree t)
199 enum tree_code code = TREE_CODE (t);
201 switch (TREE_CODE_CLASS (code))
222 /* FIXME lower VA_ARG_EXPR. */
236 return is_gimple_lvalue (t) || is_gimple_val (t);
239 /* Returns true iff T is a valid RHS for an assignment to a renamed user
243 is_gimple_reg_rhs (tree t)
245 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
246 the LHS is a user variable, then we need to introduce a temporary.
247 ie temp = RHS; LHS = temp.
249 This way the optimizers can determine that the user variable is
250 only modified if evaluation of the RHS does not throw. */
251 if (is_gimple_reg_type (TREE_TYPE (t))
252 && TREE_SIDE_EFFECTS (t)
253 && (TREE_CODE (t) == CALL_EXPR
254 || (flag_non_call_exceptions && tree_could_trap_p (t))))
255 return is_gimple_val (t);
257 /* Don't force a temp of a non-renamable type; the copy could be
258 arbitrarily expensive. Instead we will generate a V_MAY_DEF for
260 return is_gimple_tmp_rhs (t);
263 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
264 LHS, or for a call argument. */
267 is_gimple_mem_rhs (tree t)
269 /* If we're dealing with a renamable type, either source or dest
270 must be a renamed variable. */
271 if (is_gimple_reg_type (TREE_TYPE (t)))
272 return is_gimple_val (t);
274 return is_gimple_tmp_rhs (t);
277 /* Returns the appropriate RHS predicate for this LHS. */
280 rhs_predicate_for (tree lhs)
282 if (is_gimple_tmp_var (lhs))
283 return is_gimple_tmp_rhs;
284 else if (is_gimple_reg (lhs))
285 return is_gimple_reg_rhs;
287 return is_gimple_mem_rhs;
290 /* Returns true if T is a valid CONSTRUCTOR component in GIMPLE, either
291 a val or another CONSTRUCTOR. */
294 is_gimple_constructor_elt (tree t)
296 return (is_gimple_val (t)
297 || TREE_CODE (t) == CONSTRUCTOR);
300 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
303 is_gimple_lvalue (tree t)
305 return (is_gimple_addr_expr_arg (t)
306 || TREE_CODE (t) == INDIRECT_REF
307 || TREE_CODE (t) == WITH_SIZE_EXPR
308 /* These are complex lvalues, but don't have addresses, so they
310 || TREE_CODE (t) == BIT_FIELD_REF);
313 /* Return true if T is a GIMPLE condition. */
316 is_gimple_condexpr (tree t)
318 return (is_gimple_val (t)
319 || TREE_CODE_CLASS (TREE_CODE (t)) == '<');
322 /* Return true if T is a valid operand for ADDR_EXPR. */
325 is_gimple_addr_expr_arg (tree t)
327 return (is_gimple_id (t)
328 || TREE_CODE (t) == ARRAY_REF
329 || TREE_CODE (t) == ARRAY_RANGE_REF
330 || TREE_CODE (t) == COMPONENT_REF
331 || TREE_CODE (t) == REALPART_EXPR
332 || TREE_CODE (t) == IMAGPART_EXPR
333 || TREE_CODE (t) == INDIRECT_REF);
336 /* Return true if T is function invariant. Or rather a restricted
337 form of function invariant. */
340 is_gimple_min_invariant (tree t)
342 switch (TREE_CODE (t))
345 return TREE_INVARIANT (t);
352 return !TREE_OVERFLOW (t);
359 /* Return true if T looks like a valid GIMPLE statement. */
362 is_gimple_stmt (tree t)
364 enum tree_code code = TREE_CODE (t);
366 if (IS_EMPTY_STMT (t))
373 /* These are only valid if they're void. */
374 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
380 case CASE_LABEL_EXPR:
382 case TRY_FINALLY_EXPR:
389 /* These are always void. */
393 /* FIXME this should be lowered. */
398 /* These are valid regardless of their type. */
406 /* Return true if T is a variable. */
409 is_gimple_variable (tree t)
411 return (TREE_CODE (t) == VAR_DECL
412 || TREE_CODE (t) == PARM_DECL
413 || TREE_CODE (t) == RESULT_DECL
414 || TREE_CODE (t) == SSA_NAME);
417 /* Return true if T is a GIMPLE identifier (something with an address). */
420 is_gimple_id (tree t)
422 return (is_gimple_variable (t)
423 || TREE_CODE (t) == FUNCTION_DECL
424 || TREE_CODE (t) == LABEL_DECL
425 /* Allow string constants, since they are addressable. */
426 || TREE_CODE (t) == STRING_CST);
429 /* Return true if TYPE is a suitable type for a scalar register variable. */
432 is_gimple_reg_type (tree type)
434 return (!AGGREGATE_TYPE_P (type)
435 && TREE_CODE (type) != COMPLEX_TYPE);
439 /* Return true if T is a scalar register variable. */
442 is_gimple_reg (tree t)
444 if (TREE_CODE (t) == SSA_NAME)
445 t = SSA_NAME_VAR (t);
447 return (is_gimple_variable (t)
448 && is_gimple_reg_type (TREE_TYPE (t))
449 /* A volatile decl is not acceptable because we can't reuse it as
450 needed. We need to copy it into a temp first. */
451 && ! TREE_THIS_VOLATILE (t)
452 && ! TREE_ADDRESSABLE (t)
453 && ! needs_to_live_in_memory (t));
456 /* Return true if T is a GIMPLE variable whose address is not needed. */
459 is_gimple_non_addressable (tree t)
461 if (TREE_CODE (t) == SSA_NAME)
462 t = SSA_NAME_VAR (t);
464 return (is_gimple_variable (t)
465 && ! TREE_ADDRESSABLE (t)
466 && ! needs_to_live_in_memory (t));
469 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
472 is_gimple_val (tree t)
474 /* Make loads from volatiles and memory vars explicit. */
475 if (is_gimple_variable (t)
476 && is_gimple_reg_type (TREE_TYPE (t))
477 && !is_gimple_reg (t))
480 /* FIXME make these decls. That can happen only when we expose the
481 entire landing-pad construct at the tree level. */
482 if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
485 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
489 /* Return true if T is a GIMPLE minimal lvalue. */
492 is_gimple_min_lval (tree t)
494 return (is_gimple_id (t)
495 || TREE_CODE (t) == INDIRECT_REF);
498 /* Return true if T is a typecast operation. */
501 is_gimple_cast (tree t)
503 return (TREE_CODE (t) == NOP_EXPR
504 || TREE_CODE (t) == CONVERT_EXPR
505 || TREE_CODE (t) == FIX_TRUNC_EXPR
506 || TREE_CODE (t) == FIX_CEIL_EXPR
507 || TREE_CODE (t) == FIX_FLOOR_EXPR
508 || TREE_CODE (t) == FIX_ROUND_EXPR);
511 /* Return true if T is a valid op0 of a CALL_EXPR. */
514 is_gimple_call_addr (tree t)
516 return (TREE_CODE (t) == OBJ_TYPE_REF
517 || is_gimple_val (t));
520 /* If T makes a function call, return the corresponding CALL_EXPR operand.
521 Otherwise, return NULL_TREE. */
524 get_call_expr_in (tree t)
526 if (TREE_CODE (t) == MODIFY_EXPR)
527 t = TREE_OPERAND (t, 1);
528 if (TREE_CODE (t) == WITH_SIZE_EXPR)
529 t = TREE_OPERAND (t, 0);
530 if (TREE_CODE (t) == CALL_EXPR)
535 /* Given a memory reference expression, return the base address. Note that,
536 in contrast with get_base_var, this will not recurse inside INDIRECT_REF
537 expressions. Therefore, given the reference PTR->FIELD, this function
538 will return *PTR. Whereas get_base_var would've returned PTR. */
541 get_base_address (tree t)
543 while (TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR
544 || handled_component_p (t))
545 t = TREE_OPERAND (t, 0);
548 || TREE_CODE (t) == STRING_CST
549 || TREE_CODE (t) == CONSTRUCTOR
550 || TREE_CODE (t) == INDIRECT_REF)
557 recalculate_side_effects (tree t)
559 enum tree_code code = TREE_CODE (t);
560 int fro = first_rtl_op (code);
563 switch (TREE_CODE_CLASS (code))
571 case PREDECREMENT_EXPR:
572 case PREINCREMENT_EXPR:
573 case POSTDECREMENT_EXPR:
574 case POSTINCREMENT_EXPR:
575 /* All of these have side-effects, no matter what their
584 case '<': /* a comparison expression */
585 case '1': /* a unary arithmetic expression */
586 case '2': /* a binary arithmetic expression */
587 case 'r': /* a reference */
588 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
589 for (i = 0; i < fro; ++i)
591 tree op = TREE_OPERAND (t, i);
592 if (op && TREE_SIDE_EFFECTS (op))
593 TREE_SIDE_EFFECTS (t) = 1;