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 addressable : addr-expr-arg
124 with-size-arg: addressable
127 indirectref : INDIRECT_REF
139 bitfieldref : BIT_FIELD_REF
144 compref : inner-compref
150 inner-compref: min-lval
191 static inline bool is_gimple_id (tree
);
193 /* Validation of GIMPLE expressions. */
195 /* Return true if T is a GIMPLE RHS for an assignment to a temporary. */
198 is_gimple_tmp_rhs (tree t
)
200 enum tree_code code
= TREE_CODE (t
);
202 switch (TREE_CODE_CLASS (code
))
235 return is_gimple_lvalue (t
) || is_gimple_val (t
);
238 /* Returns true iff T is a valid RHS for an assignment to a renamed user
242 is_gimple_reg_rhs (tree t
)
244 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
245 the LHS is a user variable, then we need to introduce a temporary.
246 ie temp = RHS; LHS = temp.
248 This way the optimizers can determine that the user variable is
249 only modified if evaluation of the RHS does not throw. */
250 if (is_gimple_reg_type (TREE_TYPE (t
))
251 && TREE_SIDE_EFFECTS (t
)
252 && (TREE_CODE (t
) == CALL_EXPR
253 || (flag_non_call_exceptions
&& tree_could_trap_p (t
))))
254 return is_gimple_val (t
);
256 /* Don't force a temp of a non-renamable type; the copy could be
257 arbitrarily expensive. Instead we will generate a V_MAY_DEF for
259 return is_gimple_tmp_rhs (t
);
262 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
263 LHS, or for a call argument. */
266 is_gimple_mem_rhs (tree t
)
268 /* If we're dealing with a renamable type, either source or dest
269 must be a renamed variable. */
270 if (is_gimple_reg_type (TREE_TYPE (t
)))
271 return is_gimple_val (t
);
273 return is_gimple_tmp_rhs (t
);
276 /* Returns the appropriate RHS predicate for this LHS. */
279 rhs_predicate_for (tree lhs
)
281 if (is_gimple_tmp_var (lhs
))
282 return is_gimple_tmp_rhs
;
283 else if (is_gimple_reg (lhs
))
284 return is_gimple_reg_rhs
;
286 return is_gimple_mem_rhs
;
289 /* Returns true if T is a valid CONSTRUCTOR component in GIMPLE, either
290 a val or another CONSTRUCTOR. */
293 is_gimple_constructor_elt (tree t
)
295 return (is_gimple_val (t
)
296 || TREE_CODE (t
) == CONSTRUCTOR
);
299 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
302 is_gimple_lvalue (tree t
)
304 return (is_gimple_addressable (t
)
305 || TREE_CODE (t
) == WITH_SIZE_EXPR
306 /* These are complex lvalues, but don't have addresses, so they
308 || TREE_CODE (t
) == BIT_FIELD_REF
);
311 /* Return true if T is a GIMPLE condition. */
314 is_gimple_condexpr (tree t
)
316 return (is_gimple_val (t
)
317 || TREE_CODE_CLASS (TREE_CODE (t
)) == '<');
320 /* Return true if T is something whose address can be taken. */
323 is_gimple_addressable (tree t
)
325 return (is_gimple_id (t
) || handled_component_p (t
)
326 || TREE_CODE (t
) == REALPART_EXPR
327 || TREE_CODE (t
) == IMAGPART_EXPR
328 || TREE_CODE (t
) == INDIRECT_REF
);
331 /* Return true if T is function invariant. Or rather a restricted
332 form of function invariant. */
335 is_gimple_min_invariant (tree t
)
337 switch (TREE_CODE (t
))
340 return TREE_INVARIANT (t
);
347 return !TREE_OVERFLOW (t
);
354 /* Return true if T looks like a valid GIMPLE statement. */
357 is_gimple_stmt (tree t
)
359 enum tree_code code
= TREE_CODE (t
);
361 if (IS_EMPTY_STMT (t
))
368 /* These are only valid if they're void. */
369 return TREE_TYPE (t
) == NULL
|| VOID_TYPE_P (TREE_TYPE (t
));
375 case CASE_LABEL_EXPR
:
377 case TRY_FINALLY_EXPR
:
384 /* These are always void. */
389 /* These are valid regardless of their type. */
397 /* Return true if T is a variable. */
400 is_gimple_variable (tree t
)
402 return (TREE_CODE (t
) == VAR_DECL
403 || TREE_CODE (t
) == PARM_DECL
404 || TREE_CODE (t
) == RESULT_DECL
405 || TREE_CODE (t
) == SSA_NAME
);
408 /* Return true if T is a GIMPLE identifier (something with an address). */
411 is_gimple_id (tree t
)
413 return (is_gimple_variable (t
)
414 || TREE_CODE (t
) == FUNCTION_DECL
415 || TREE_CODE (t
) == LABEL_DECL
416 || TREE_CODE (t
) == CONST_DECL
417 /* Allow string constants, since they are addressable. */
418 || TREE_CODE (t
) == STRING_CST
);
421 /* Return true if TYPE is a suitable type for a scalar register variable. */
424 is_gimple_reg_type (tree type
)
426 return (!AGGREGATE_TYPE_P (type
)
427 && TREE_CODE (type
) != COMPLEX_TYPE
);
431 /* Return true if T is a scalar register variable. */
434 is_gimple_reg (tree t
)
436 if (TREE_CODE (t
) == SSA_NAME
)
437 t
= SSA_NAME_VAR (t
);
439 return (is_gimple_variable (t
)
440 && is_gimple_reg_type (TREE_TYPE (t
))
441 /* A volatile decl is not acceptable because we can't reuse it as
442 needed. We need to copy it into a temp first. */
443 && ! TREE_THIS_VOLATILE (t
)
444 && ! TREE_ADDRESSABLE (t
)
445 && ! needs_to_live_in_memory (t
));
448 /* Returns true if T is a GIMPLE temporary variable, false otherwise. */
451 is_gimple_tmp_var (tree t
)
453 /* FIXME this could trigger for other local artificials, too. */
454 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
)
455 && !TREE_STATIC (t
) && !DECL_EXTERNAL (t
));
458 /* Returns true if T is a GIMPLE temporary register variable. */
461 is_gimple_tmp_reg (tree t
)
463 /* The intent of this is to get hold of a value that won't change.
464 An SSA_NAME qualifies no matter if its of a user variable or not. */
465 if (TREE_CODE (t
) == SSA_NAME
)
468 /* We don't know the lifetime characteristics of user variables. */
469 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_ARTIFICIAL (t
))
472 /* Finally, it must be capable of being placed in a register. */
473 return is_gimple_reg (t
);
476 /* Return true if T is a GIMPLE variable whose address is not needed. */
479 is_gimple_non_addressable (tree t
)
481 if (TREE_CODE (t
) == SSA_NAME
)
482 t
= SSA_NAME_VAR (t
);
484 return (is_gimple_variable (t
)
485 && ! TREE_ADDRESSABLE (t
)
486 && ! needs_to_live_in_memory (t
));
489 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
492 is_gimple_val (tree t
)
494 /* Make loads from volatiles and memory vars explicit. */
495 if (is_gimple_variable (t
)
496 && is_gimple_reg_type (TREE_TYPE (t
))
497 && !is_gimple_reg (t
))
500 /* FIXME make these decls. That can happen only when we expose the
501 entire landing-pad construct at the tree level. */
502 if (TREE_CODE (t
) == EXC_PTR_EXPR
|| TREE_CODE (t
) == FILTER_EXPR
)
505 return (is_gimple_variable (t
) || is_gimple_min_invariant (t
));
509 /* Return true if T is a GIMPLE minimal lvalue. */
512 is_gimple_min_lval (tree t
)
514 return (is_gimple_id (t
)
515 || TREE_CODE (t
) == INDIRECT_REF
);
518 /* Return true if T is a typecast operation. */
521 is_gimple_cast (tree t
)
523 return (TREE_CODE (t
) == NOP_EXPR
524 || TREE_CODE (t
) == CONVERT_EXPR
525 || TREE_CODE (t
) == FIX_TRUNC_EXPR
526 || TREE_CODE (t
) == FIX_CEIL_EXPR
527 || TREE_CODE (t
) == FIX_FLOOR_EXPR
528 || TREE_CODE (t
) == FIX_ROUND_EXPR
);
531 /* Return true if T is a valid op0 of a CALL_EXPR. */
534 is_gimple_call_addr (tree t
)
536 return (TREE_CODE (t
) == OBJ_TYPE_REF
537 || is_gimple_val (t
));
540 /* If T makes a function call, return the corresponding CALL_EXPR operand.
541 Otherwise, return NULL_TREE. */
544 get_call_expr_in (tree t
)
546 if (TREE_CODE (t
) == MODIFY_EXPR
)
547 t
= TREE_OPERAND (t
, 1);
548 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
549 t
= TREE_OPERAND (t
, 0);
550 if (TREE_CODE (t
) == CALL_EXPR
)
555 /* Given a memory reference expression, return the base address. Note that,
556 in contrast with get_base_var, this will not recurse inside INDIRECT_REF
557 expressions. Therefore, given the reference PTR->FIELD, this function
558 will return *PTR. Whereas get_base_var would've returned PTR. */
561 get_base_address (tree t
)
563 while (TREE_CODE (t
) == REALPART_EXPR
|| TREE_CODE (t
) == IMAGPART_EXPR
564 || handled_component_p (t
))
565 t
= TREE_OPERAND (t
, 0);
568 || TREE_CODE (t
) == STRING_CST
569 || TREE_CODE (t
) == CONSTRUCTOR
570 || TREE_CODE (t
) == INDIRECT_REF
)
577 recalculate_side_effects (tree t
)
579 enum tree_code code
= TREE_CODE (t
);
580 int fro
= first_rtl_op (code
);
583 switch (TREE_CODE_CLASS (code
))
591 case PREDECREMENT_EXPR
:
592 case PREINCREMENT_EXPR
:
593 case POSTDECREMENT_EXPR
:
594 case POSTINCREMENT_EXPR
:
595 /* All of these have side-effects, no matter what their
604 case '<': /* a comparison expression */
605 case '1': /* a unary arithmetic expression */
606 case '2': /* a binary arithmetic expression */
607 case 'r': /* a reference */
608 TREE_SIDE_EFFECTS (t
) = TREE_THIS_VOLATILE (t
);
609 for (i
= 0; i
< fro
; ++i
)
611 tree op
= TREE_OPERAND (t
, i
);
612 if (op
&& TREE_SIDE_EFFECTS (op
))
613 TREE_SIDE_EFFECTS (t
) = 1;