OSDN Git Service

* gimplify.c (is_gimple_tmp_var): Move to tree-gimple.c.
[pf3gnuchains/gcc-fork.git] / gcc / tree-gimple.c
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>
5
6 This file is part of GCC.
7
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)
11 any later version.
12
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.
17
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.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "ggc.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "tree-gimple.h"
30 #include "tree-flow.h"
31 #include "output.h"
32 #include "rtl.h"
33 #include "expr.h"
34 #include "bitmap.h"
35
36 /* GCC GIMPLE structure
37
38    Inspired by the SIMPLE C grammar at
39
40         http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
41
42    function     : FUNCTION_DECL
43                         DECL_SAVED_TREE -> compound-stmt
44
45    compound-stmt: STATEMENT_LIST
46                         members -> stmt
47
48    stmt         : block
49                 | if-stmt
50                 | switch-stmt
51                 | goto-stmt
52                 | return-stmt
53                 | resx-stmt
54                 | label-stmt
55                 | try-stmt
56                 | modify-stmt
57                 | call-stmt
58
59    block        : BIND_EXPR
60                         BIND_EXPR_VARS -> chain of DECLs
61                         BIND_EXPR_BLOCK -> BLOCK
62                         BIND_EXPR_BODY -> compound-stmt
63
64    if-stmt      : COND_EXPR
65                         op0 -> condition
66                         op1 -> compound-stmt
67                         op2 -> compound-stmt
68
69    switch-stmt  : SWITCH_EXPR
70                         op0 -> val
71                         op1 -> NULL
72                         op2 -> TREE_VEC of CASE_LABEL_EXPRs
73                             The CASE_LABEL_EXPRs are sorted by CASE_LOW,
74                             and default is last.
75
76    goto-stmt    : GOTO_EXPR
77                         op0 -> LABEL_DECL | val
78
79    return-stmt  : RETURN_EXPR
80                         op0 -> return-value
81
82    return-value : NULL
83                 | RESULT_DECL
84                 | MODIFY_EXPR
85                         op0 -> RESULT_DECL
86                         op1 -> lhs
87
88    resx-stmt    : RESX_EXPR
89
90    label-stmt   : LABEL_EXPR
91                         op0 -> LABEL_DECL
92
93    try-stmt     : TRY_CATCH_EXPR
94                         op0 -> compound-stmt
95                         op1 -> handler
96                 | TRY_FINALLY_EXPR
97                         op0 -> compound-stmt
98                         op1 -> compound-stmt
99
100    handler      : catch-seq
101                 | EH_FILTER_EXPR
102                 | compound-stmt
103
104    catch-seq    : STATEMENT_LIST
105                         members -> CATCH_EXPR
106
107    modify-stmt  : MODIFY_EXPR
108                         op0 -> lhs
109                         op1 -> rhs
110
111    call-stmt    : CALL_EXPR
112                         op0 -> val | OBJ_TYPE_REF
113                         op1 -> call-arg-list
114
115    call-arg-list: TREE_LIST
116                         members -> lhs
117
118    addr-expr-arg: ID
119                 | compref
120
121    with-size-arg: addr-expr-arg
122                 | indirectref
123                 | call-stmt
124
125    indirectref  : INDIRECT_REF
126                         op0 -> val
127
128    lhs          : addr-expr-arg
129                 | bitfieldref
130                 | indirectref
131                 | WITH_SIZE_EXPR
132                         op0 -> with-size-arg
133                         op1 -> val
134
135    min-lval     : ID
136                 | indirectref
137
138    bitfieldref  : BIT_FIELD_REF
139                         op0 -> inner-compref
140                         op1 -> CONST
141                         op2 -> var
142
143    compref      : inner-compref
144                 | REALPART_EXPR
145                         op0 -> inner-compref
146                 | IMAGPART_EXPR
147                         op0 -> inner-compref
148
149    inner-compref: min-lval
150                 | COMPONENT_REF
151                         op0 -> inner-compref
152                         op1 -> FIELD_DECL
153                         op2 -> val
154                 | ARRAY_REF
155                         op0 -> inner-compref
156                         op1 -> val
157                         op2 -> val
158                         op3 -> val
159                 | ARRAY_RANGE_REF
160                         op0 -> inner-compref
161                         op1 -> val
162                         op2 -> val
163                         op3 -> val
164                 | VIEW_CONVERT_EXPR
165                         op0 -> inner-compref
166
167    condition    : val
168                 | RELOP
169                         op0 -> val
170                         op1 -> val
171
172    val          : ID
173                 | CONST
174
175    rhs          : lhs
176                 | CONST
177                 | call-stmt
178                 | ADDR_EXPR
179                         op0 -> addr-expr-arg
180                 | UNOP
181                         op0 -> val
182                 | BINOP
183                         op0 -> val
184                         op1 -> val
185                 | RELOP
186                         op0 -> val
187                         op1 -> val
188 */
189
190 static inline bool is_gimple_id (tree);
191
192 /* Validation of GIMPLE expressions.  */
193
194 /* Return true if T is a GIMPLE RHS for an assignment to a temporary.  */
195
196 bool
197 is_gimple_tmp_rhs (tree t)
198 {
199   enum tree_code code = TREE_CODE (t);
200
201   switch (TREE_CODE_CLASS (code))
202     {
203     case '1':
204     case '2':
205     case '<':
206       return true;
207
208     default:
209       break;
210     }
211
212   switch (code)
213     {
214     case TRUTH_NOT_EXPR:
215     case TRUTH_AND_EXPR:
216     case TRUTH_OR_EXPR:
217     case TRUTH_XOR_EXPR:
218     case ADDR_EXPR:
219     case CALL_EXPR:
220     case CONSTRUCTOR:
221     case COMPLEX_EXPR:
222     case INTEGER_CST:
223     case REAL_CST:
224     case STRING_CST:
225     case COMPLEX_CST:
226     case VECTOR_CST:
227     case OBJ_TYPE_REF:
228       return true;
229
230     default:
231       break;
232     }
233
234   return is_gimple_lvalue (t) || is_gimple_val (t);
235 }
236
237 /* Returns true iff T is a valid RHS for an assignment to a renamed user
238    variable.  */
239
240 bool
241 is_gimple_reg_rhs (tree t)
242 {
243   /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto and
244      the LHS is a user variable, then we need to introduce a temporary.
245      ie temp = RHS; LHS = temp.
246
247      This way the optimizers can determine that the user variable is
248      only modified if evaluation of the RHS does not throw.  */
249   if (is_gimple_reg_type (TREE_TYPE (t))
250       && TREE_SIDE_EFFECTS (t)
251       && (TREE_CODE (t) == CALL_EXPR
252           || (flag_non_call_exceptions && tree_could_trap_p (t))))
253     return is_gimple_val (t);
254   else
255     /* Don't force a temp of a non-renamable type; the copy could be
256        arbitrarily expensive.  Instead we will generate a V_MAY_DEF for
257        the assignment.  */
258     return is_gimple_tmp_rhs (t);
259 }
260
261 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
262    LHS, or for a call argument.  */
263
264 bool
265 is_gimple_mem_rhs (tree t)
266 {
267   /* If we're dealing with a renamable type, either source or dest
268      must be a renamed variable.  */
269   if (is_gimple_reg_type (TREE_TYPE (t)))
270     return is_gimple_val (t);
271   else
272     return is_gimple_tmp_rhs (t);
273 }
274
275 /* Returns the appropriate RHS predicate for this LHS.  */
276
277 gimple_predicate
278 rhs_predicate_for (tree lhs)
279 {
280   if (is_gimple_tmp_var (lhs))
281     return is_gimple_tmp_rhs;
282   else if (is_gimple_reg (lhs))
283     return is_gimple_reg_rhs;
284   else
285     return is_gimple_mem_rhs;
286 }
287
288 /* Returns true if T is a valid CONSTRUCTOR component in GIMPLE, either
289    a val or another CONSTRUCTOR.  */
290
291 bool
292 is_gimple_constructor_elt (tree t)
293 {
294   return (is_gimple_val (t)
295           || TREE_CODE (t) == CONSTRUCTOR);
296 }
297
298 /*  Return true if T is a valid LHS for a GIMPLE assignment expression.  */
299
300 bool
301 is_gimple_lvalue (tree t)
302 {
303   return (is_gimple_addr_expr_arg (t)
304           || TREE_CODE (t) == INDIRECT_REF
305           || TREE_CODE (t) == WITH_SIZE_EXPR
306           /* These are complex lvalues, but don't have addresses, so they
307              go here.  */
308           || TREE_CODE (t) == BIT_FIELD_REF);
309 }
310
311 /*  Return true if T is a GIMPLE condition.  */
312
313 bool
314 is_gimple_condexpr (tree t)
315 {
316   return (is_gimple_val (t)
317           || TREE_CODE_CLASS (TREE_CODE (t)) == '<');
318 }
319
320 /*  Return true if T is a valid operand for ADDR_EXPR.  */
321
322 bool
323 is_gimple_addr_expr_arg (tree t)
324 {
325   return (is_gimple_id (t)
326           || TREE_CODE (t) == ARRAY_REF
327           || TREE_CODE (t) == ARRAY_RANGE_REF
328           || TREE_CODE (t) == COMPONENT_REF
329           || TREE_CODE (t) == REALPART_EXPR
330           || TREE_CODE (t) == IMAGPART_EXPR
331           || TREE_CODE (t) == INDIRECT_REF);
332 }
333
334 /* Return true if T is function invariant.  Or rather a restricted
335    form of function invariant.  */
336
337 bool
338 is_gimple_min_invariant (tree t)
339 {
340   switch (TREE_CODE (t))
341     {
342     case ADDR_EXPR:
343       return TREE_INVARIANT (t);
344
345     case INTEGER_CST:
346     case REAL_CST:
347     case STRING_CST:
348     case COMPLEX_CST:
349     case VECTOR_CST:
350       return !TREE_OVERFLOW (t);
351
352     default:
353       return false;
354     }
355 }
356
357 /* Return true if T looks like a valid GIMPLE statement.  */
358
359 bool
360 is_gimple_stmt (tree t)
361 {
362   enum tree_code code = TREE_CODE (t);
363
364   if (IS_EMPTY_STMT (t))
365     return 1;
366
367   switch (code)
368     {
369     case BIND_EXPR:
370     case COND_EXPR:
371       /* These are only valid if they're void.  */
372       return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
373
374     case SWITCH_EXPR:
375     case GOTO_EXPR:
376     case RETURN_EXPR:
377     case LABEL_EXPR:
378     case CASE_LABEL_EXPR:
379     case TRY_CATCH_EXPR:
380     case TRY_FINALLY_EXPR:
381     case EH_FILTER_EXPR:
382     case CATCH_EXPR:
383     case ASM_EXPR:
384     case RESX_EXPR:
385     case PHI_NODE:
386     case STATEMENT_LIST:
387       /* These are always void.  */
388       return true;
389
390     case CALL_EXPR:
391     case MODIFY_EXPR:
392       /* These are valid regardless of their type.  */
393       return true;
394
395     default:
396       return false;
397     }
398 }
399
400 /* Return true if T is a variable.  */
401
402 bool
403 is_gimple_variable (tree t)
404 {
405   return (TREE_CODE (t) == VAR_DECL
406           || TREE_CODE (t) == PARM_DECL
407           || TREE_CODE (t) == RESULT_DECL
408           || TREE_CODE (t) == SSA_NAME);
409 }
410
411 /*  Return true if T is a GIMPLE identifier (something with an address).  */
412
413 static inline bool
414 is_gimple_id (tree t)
415 {
416   return (is_gimple_variable (t)
417           || TREE_CODE (t) == FUNCTION_DECL
418           || TREE_CODE (t) == LABEL_DECL
419           /* Allow string constants, since they are addressable.  */
420           || TREE_CODE (t) == STRING_CST);
421 }
422
423 /* Return true if TYPE is a suitable type for a scalar register variable.  */
424
425 bool
426 is_gimple_reg_type (tree type)
427 {
428   return (!AGGREGATE_TYPE_P (type)
429           && TREE_CODE (type) != COMPLEX_TYPE);
430 }
431
432
433 /* Return true if T is a scalar register variable.  */
434
435 bool
436 is_gimple_reg (tree t)
437 {
438   if (TREE_CODE (t) == SSA_NAME)
439     t = SSA_NAME_VAR (t);
440
441   return (is_gimple_variable (t)
442           && is_gimple_reg_type (TREE_TYPE (t))
443           /* A volatile decl is not acceptable because we can't reuse it as
444              needed.  We need to copy it into a temp first.  */
445           && ! TREE_THIS_VOLATILE (t)
446           && ! TREE_ADDRESSABLE (t)
447           && ! needs_to_live_in_memory (t));
448 }
449
450 /* Returns true if T is a GIMPLE temporary variable, false otherwise.  */
451
452 bool
453 is_gimple_tmp_var (tree t)
454 {
455   /* FIXME this could trigger for other local artificials, too.  */
456   return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t)
457           && !TREE_STATIC (t) && !DECL_EXTERNAL (t));
458 }
459
460 /* Returns true if T is a GIMPLE temporary register variable.  */
461
462 bool
463 is_gimple_tmp_reg (tree t)
464 {
465   /* The intent of this is to get hold of a value that won't change.
466      An SSA_NAME qualifies no matter if its of a user variable or not.  */
467   if (TREE_CODE (t) == SSA_NAME)
468     return true;
469
470   /* We don't know the lifetime characteristics of user variables.  */
471   if (TREE_CODE (t) != VAR_DECL || !DECL_ARTIFICIAL (t))
472     return false;
473
474   /* Finally, it must be capable of being placed in a register.  */
475   return is_gimple_reg (t);
476 }
477
478 /* Return true if T is a GIMPLE variable whose address is not needed.  */
479
480 bool
481 is_gimple_non_addressable (tree t)
482 {
483   if (TREE_CODE (t) == SSA_NAME)
484     t = SSA_NAME_VAR (t);
485
486   return (is_gimple_variable (t)
487           && ! TREE_ADDRESSABLE (t)
488           && ! needs_to_live_in_memory (t));
489 }
490
491 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant.  */
492
493 bool
494 is_gimple_val (tree t)
495 {
496   /* Make loads from volatiles and memory vars explicit.  */
497   if (is_gimple_variable (t)
498       && is_gimple_reg_type (TREE_TYPE (t))
499       && !is_gimple_reg (t))
500     return false;
501
502   /* FIXME make these decls.  That can happen only when we expose the
503      entire landing-pad construct at the tree level.  */
504   if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
505     return 1;
506
507   return (is_gimple_variable (t) || is_gimple_min_invariant (t));
508 }
509
510
511 /* Return true if T is a GIMPLE minimal lvalue.  */
512
513 bool
514 is_gimple_min_lval (tree t)
515 {
516   return (is_gimple_id (t)
517           || TREE_CODE (t) == INDIRECT_REF);
518 }
519
520 /* Return true if T is a typecast operation.  */
521
522 bool
523 is_gimple_cast (tree t)
524 {
525   return (TREE_CODE (t) == NOP_EXPR
526           || TREE_CODE (t) == CONVERT_EXPR
527           || TREE_CODE (t) == FIX_TRUNC_EXPR
528           || TREE_CODE (t) == FIX_CEIL_EXPR
529           || TREE_CODE (t) == FIX_FLOOR_EXPR
530           || TREE_CODE (t) == FIX_ROUND_EXPR);
531 }
532
533 /* Return true if T is a valid op0 of a CALL_EXPR.  */
534
535 bool
536 is_gimple_call_addr (tree t)
537 {
538   return (TREE_CODE (t) == OBJ_TYPE_REF
539           || is_gimple_val (t));
540 }
541
542 /* If T makes a function call, return the corresponding CALL_EXPR operand.
543    Otherwise, return NULL_TREE.  */
544
545 tree
546 get_call_expr_in (tree t)
547 {
548   if (TREE_CODE (t) == MODIFY_EXPR)
549     t = TREE_OPERAND (t, 1);
550   if (TREE_CODE (t) == WITH_SIZE_EXPR)
551     t = TREE_OPERAND (t, 0);
552   if (TREE_CODE (t) == CALL_EXPR)
553     return t;
554   return NULL_TREE;
555 }
556
557 /* Given a memory reference expression, return the base address.  Note that,
558    in contrast with get_base_var, this will not recurse inside INDIRECT_REF
559    expressions.  Therefore, given the reference PTR->FIELD, this function
560    will return *PTR.  Whereas get_base_var would've returned PTR.  */
561
562 tree
563 get_base_address (tree t)
564 {
565   while (TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR
566          || handled_component_p (t))
567     t = TREE_OPERAND (t, 0);
568   
569   if (SSA_VAR_P (t)
570       || TREE_CODE (t) == STRING_CST
571       || TREE_CODE (t) == CONSTRUCTOR
572       || TREE_CODE (t) == INDIRECT_REF)
573     return t;
574   else
575     return NULL_TREE;
576 }
577
578 void
579 recalculate_side_effects (tree t)
580 {
581   enum tree_code code = TREE_CODE (t);
582   int fro = first_rtl_op (code);
583   int i;
584
585   switch (TREE_CODE_CLASS (code))
586     {
587     case 'e':
588       switch (code)
589         {
590         case INIT_EXPR:
591         case MODIFY_EXPR:
592         case VA_ARG_EXPR:
593         case PREDECREMENT_EXPR:
594         case PREINCREMENT_EXPR:
595         case POSTDECREMENT_EXPR:
596         case POSTINCREMENT_EXPR:
597           /* All of these have side-effects, no matter what their
598              operands are.  */
599           return;
600
601         default:
602           break;
603         }
604       /* Fall through.  */
605
606     case '<':  /* a comparison expression */
607     case '1':  /* a unary arithmetic expression */
608     case '2':  /* a binary arithmetic expression */
609     case 'r':  /* a reference */
610       TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
611       for (i = 0; i < fro; ++i)
612         {
613           tree op = TREE_OPERAND (t, i);
614           if (op && TREE_SIDE_EFFECTS (op))
615             TREE_SIDE_EFFECTS (t) = 1;
616         }
617       break;
618    }
619 }