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"
35 /* GCC GIMPLE structure
37 Inspired by the SIMPLE C grammar at
39 http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
41 function : FUNCTION_DECL
42 DECL_SAVED_TREE -> compound-stmt
44 compound-stmt: STATEMENT_LIST
59 BIND_EXPR_VARS -> chain of DECLs
60 BIND_EXPR_BLOCK -> BLOCK
61 BIND_EXPR_BODY -> compound-stmt
68 switch-stmt : SWITCH_EXPR
71 op2 -> TREE_VEC of CASE_LABEL_EXPRs
72 The CASE_LABEL_EXPRs are sorted by CASE_LOW,
76 op0 -> LABEL_DECL | val
78 return-stmt : RETURN_EXPR
89 label-stmt : LABEL_EXPR
92 try-stmt : TRY_CATCH_EXPR
103 catch-seq : STATEMENT_LIST
104 members -> CATCH_EXPR
106 modify-stmt : MODIFY_EXPR
110 call-stmt : CALL_EXPR
111 op0 -> val | OBJ_TYPE_REF
114 call-arg-list: TREE_LIST
127 bitfieldref : BIT_FIELD_REF
132 compref : inner-compref
138 inner-compref: min-lval
171 static inline bool is_gimple_id (tree
);
173 /* Validation of GIMPLE expressions. */
175 /* Return true if T is a GIMPLE RHS. */
178 is_gimple_rhs (tree t
)
180 enum tree_code code
= TREE_CODE (t
);
182 switch (TREE_CODE_CLASS (code
))
203 /* FIXME lower VA_ARG_EXPR. */
217 return is_gimple_lvalue (t
) || is_gimple_val (t
);
220 /* Returns true if T is a valid CONSTRUCTOR component in GIMPLE, either
221 a val or another CONSTRUCTOR. */
224 is_gimple_constructor_elt (tree t
)
226 return (is_gimple_val (t
)
227 || TREE_CODE (t
) == CONSTRUCTOR
);
230 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
233 is_gimple_lvalue (tree t
)
235 return (is_gimple_addr_expr_arg (t
)
236 || TREE_CODE (t
) == INDIRECT_REF
237 /* These are complex lvalues, but don't have addresses, so they
239 || TREE_CODE (t
) == BIT_FIELD_REF
);
242 /* Return true if T is a GIMPLE condition. */
245 is_gimple_condexpr (tree t
)
247 return (is_gimple_val (t
)
248 || TREE_CODE_CLASS (TREE_CODE (t
)) == '<');
251 /* Return true if T is a valid operand for ADDR_EXPR. */
254 is_gimple_addr_expr_arg (tree t
)
256 return (is_gimple_id (t
)
257 || TREE_CODE (t
) == ARRAY_REF
258 || TREE_CODE (t
) == ARRAY_RANGE_REF
259 || TREE_CODE (t
) == COMPONENT_REF
260 || TREE_CODE (t
) == REALPART_EXPR
261 || TREE_CODE (t
) == IMAGPART_EXPR
262 || TREE_CODE (t
) == INDIRECT_REF
);
265 /* Return true if T is function invariant. Or rather a restricted
266 form of function invariant. */
269 is_gimple_min_invariant (tree t
)
271 switch (TREE_CODE (t
))
274 return TREE_INVARIANT (t
);
281 return !TREE_OVERFLOW (t
);
288 /* Return true if T looks like a valid GIMPLE statement. */
291 is_gimple_stmt (tree t
)
293 enum tree_code code
= TREE_CODE (t
);
295 if (IS_EMPTY_STMT (t
))
302 /* These are only valid if they're void. */
303 return TREE_TYPE (t
) == NULL
|| VOID_TYPE_P (TREE_TYPE (t
));
309 case CASE_LABEL_EXPR
:
311 case TRY_FINALLY_EXPR
:
318 /* These are always void. */
322 /* FIXME this should be lowered. */
327 /* These are valid regardless of their type. */
335 /* Return true if T is a variable. */
338 is_gimple_variable (tree t
)
340 return (TREE_CODE (t
) == VAR_DECL
341 || TREE_CODE (t
) == PARM_DECL
342 || TREE_CODE (t
) == RESULT_DECL
343 || TREE_CODE (t
) == SSA_NAME
);
346 /* Return true if T is a GIMPLE identifier (something with an address). */
349 is_gimple_id (tree t
)
351 return (is_gimple_variable (t
)
352 || TREE_CODE (t
) == FUNCTION_DECL
353 || TREE_CODE (t
) == LABEL_DECL
354 /* Allow string constants, since they are addressable. */
355 || TREE_CODE (t
) == STRING_CST
);
358 /* Return true if TYPE is a suitable type for a scalar register variable. */
361 is_gimple_reg_type (tree type
)
363 return (!AGGREGATE_TYPE_P (type
)
364 && TREE_CODE (type
) != COMPLEX_TYPE
);
368 /* Return true if T is a scalar register variable. */
371 is_gimple_reg (tree t
)
373 if (TREE_CODE (t
) == SSA_NAME
)
374 t
= SSA_NAME_VAR (t
);
376 return (is_gimple_variable (t
)
377 && is_gimple_reg_type (TREE_TYPE (t
))
378 /* A volatile decl is not acceptable because we can't reuse it as
379 needed. We need to copy it into a temp first. */
380 && ! TREE_THIS_VOLATILE (t
)
381 && ! TREE_ADDRESSABLE (t
)
382 && ! needs_to_live_in_memory (t
));
385 /* Return true if T is a GIMPLE variable whose address is not needed. */
388 is_gimple_non_addressable (tree t
)
390 if (TREE_CODE (t
) == SSA_NAME
)
391 t
= SSA_NAME_VAR (t
);
393 return (is_gimple_variable (t
)
394 && ! TREE_ADDRESSABLE (t
)
395 && ! needs_to_live_in_memory (t
));
398 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
401 is_gimple_val (tree t
)
403 /* Make loads from volatiles and memory vars explicit. */
404 if (is_gimple_variable (t
)
405 && is_gimple_reg_type (TREE_TYPE (t
))
406 && !is_gimple_reg (t
))
409 /* FIXME make these decls. That can happen only when we expose the
410 entire landing-pad construct at the tree level. */
411 if (TREE_CODE (t
) == EXC_PTR_EXPR
|| TREE_CODE (t
) == FILTER_EXPR
)
414 return (is_gimple_variable (t
) || is_gimple_min_invariant (t
));
418 /* Return true if T is a GIMPLE minimal lvalue. */
421 is_gimple_min_lval (tree t
)
423 return (is_gimple_id (t
)
424 || TREE_CODE (t
) == INDIRECT_REF
);
427 /* Return true if T is a typecast operation. */
430 is_gimple_cast (tree t
)
432 return (TREE_CODE (t
) == NOP_EXPR
433 || TREE_CODE (t
) == CONVERT_EXPR
434 || TREE_CODE (t
) == FIX_TRUNC_EXPR
435 || TREE_CODE (t
) == FIX_CEIL_EXPR
436 || TREE_CODE (t
) == FIX_FLOOR_EXPR
437 || TREE_CODE (t
) == FIX_ROUND_EXPR
);
440 /* Return true if T is a valid op0 of a CALL_EXPR. */
443 is_gimple_call_addr (tree t
)
445 return (TREE_CODE (t
) == OBJ_TYPE_REF
446 || is_gimple_val (t
));
449 /* If T makes a function call, return the corresponding CALL_EXPR operand.
450 Otherwise, return NULL_TREE. */
453 get_call_expr_in (tree t
)
455 if (TREE_CODE (t
) == MODIFY_EXPR
)
456 t
= TREE_OPERAND (t
, 1);
457 if (TREE_CODE (t
) == CALL_EXPR
)
462 /* Given a memory reference expression, return the base address. Note that,
463 in contrast with get_base_var, this will not recurse inside INDIRECT_REF
464 expressions. Therefore, given the reference PTR->FIELD, this function
465 will return *PTR. Whereas get_base_var would've returned PTR. */
468 get_base_address (tree t
)
470 while (TREE_CODE (t
) == REALPART_EXPR
|| TREE_CODE (t
) == IMAGPART_EXPR
471 || handled_component_p (t
))
472 t
= TREE_OPERAND (t
, 0);
475 || TREE_CODE (t
) == STRING_CST
476 || TREE_CODE (t
) == CONSTRUCTOR
477 || TREE_CODE (t
) == INDIRECT_REF
)
484 recalculate_side_effects (tree t
)
486 enum tree_code code
= TREE_CODE (t
);
487 int fro
= first_rtl_op (code
);
490 switch (TREE_CODE_CLASS (code
))
498 case PREDECREMENT_EXPR
:
499 case PREINCREMENT_EXPR
:
500 case POSTDECREMENT_EXPR
:
501 case POSTINCREMENT_EXPR
:
502 /* All of these have side-effects, no matter what their
511 case '<': /* a comparison expression */
512 case '1': /* a unary arithmetic expression */
513 case '2': /* a binary arithmetic expression */
514 case 'r': /* a reference */
515 TREE_SIDE_EFFECTS (t
) = TREE_THIS_VOLATILE (t
);
516 for (i
= 0; i
< fro
; ++i
)
518 tree op
= TREE_OPERAND (t
, i
);
519 if (op
&& TREE_SIDE_EFFECTS (op
))
520 TREE_SIDE_EFFECTS (t
) = 1;