00001 /* Functions to analyze and validate GIMPLE trees. 00002 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. 00003 Contributed by Diego Novillo <dnovillo@redhat.com> 00004 Rewritten by Jason Merrill <jason@redhat.com> 00005 00006 This file is part of GCC. 00007 00008 GCC is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2, or (at your option) 00011 any later version. 00012 00013 GCC is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with GCC; see the file COPYING. If not, write to 00020 the Free Software Foundation, 59 Temple Place - Suite 330, 00021 Boston, MA 02111-1307, USA. */ 00022 00023 #include "config.h" 00024 #include "system.h" 00025 #include "coretypes.h" 00026 #include "ggc.h" 00027 #include "tm.h" 00028 #include "tree.h" 00029 #include "tree-gimple.h" 00030 #include "tree-flow.h" 00031 #include "output.h" 00032 #include "rtl.h" 00033 #include "expr.h" 00034 #include "bitmap.h" 00035 00036 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */ 00037 00038 static inline bool is_gimple_id (tree); 00039 00040 /* Validation of GIMPLE expressions. */ 00041 00042 /* Return true if T is a GIMPLE RHS for an assignment to a temporary. */ 00043 00044 bool 00045 is_gimple_formal_tmp_rhs (tree t) 00046 { 00047 enum tree_code code = TREE_CODE (t); 00048 00049 switch (TREE_CODE_CLASS (code)) 00050 { 00051 case tcc_unary: 00052 case tcc_binary: 00053 case tcc_comparison: 00054 return true; 00055 00056 default: 00057 break; 00058 } 00059 00060 switch (code) 00061 { 00062 case TRUTH_NOT_EXPR: 00063 case TRUTH_AND_EXPR: 00064 case TRUTH_OR_EXPR: 00065 case TRUTH_XOR_EXPR: 00066 case ADDR_EXPR: 00067 case CALL_EXPR: 00068 case CONSTRUCTOR: 00069 case COMPLEX_EXPR: 00070 case INTEGER_CST: 00071 case REAL_CST: 00072 case STRING_CST: 00073 case COMPLEX_CST: 00074 case VECTOR_CST: 00075 case OBJ_TYPE_REF: 00076 return true; 00077 00078 default: 00079 break; 00080 } 00081 00082 return is_gimple_lvalue (t) || is_gimple_val (t); 00083 } 00084 00085 /* Returns true iff T is a valid RHS for an assignment to a renamed 00086 user -- or front-end generated artificial -- variable. */ 00087 00088 bool 00089 is_gimple_reg_rhs (tree t) 00090 { 00091 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto 00092 and the LHS is a user variable, then we need to introduce a formal 00093 temporary. This way the optimizers can determine that the user 00094 variable is only modified if evaluation of the RHS does not throw. 00095 00096 Don't force a temp of a non-renamable type; the copy could be 00097 arbitrarily expensive. Instead we will generate a V_MAY_DEF for 00098 the assignment. */ 00099 00100 if (is_gimple_reg_type (TREE_TYPE (t)) 00101 && ((TREE_CODE (t) == CALL_EXPR && TREE_SIDE_EFFECTS (t)) 00102 || tree_could_throw_p (t))) 00103 return false; 00104 00105 return is_gimple_formal_tmp_rhs (t); 00106 } 00107 00108 /* Returns true iff T is a valid RHS for an assignment to an un-renamed 00109 LHS, or for a call argument. */ 00110 00111 bool 00112 is_gimple_mem_rhs (tree t) 00113 { 00114 /* If we're dealing with a renamable type, either source or dest must be 00115 a renamed variable. Also force a temporary if the type doesn't need 00116 to be stored in memory, since it's cheap and prevents erroneous 00117 tailcalls (PR 17526). */ 00118 if (is_gimple_reg_type (TREE_TYPE (t)) 00119 || TYPE_MODE (TREE_TYPE (t)) != BLKmode) 00120 return is_gimple_val (t); 00121 else 00122 return is_gimple_formal_tmp_rhs (t); 00123 } 00124 00125 /* Returns the appropriate RHS predicate for this LHS. */ 00126 00127 gimple_predicate 00128 rhs_predicate_for (tree lhs) 00129 { 00130 if (is_gimple_formal_tmp_var (lhs)) 00131 return is_gimple_formal_tmp_rhs; 00132 else if (is_gimple_reg (lhs)) 00133 return is_gimple_reg_rhs; 00134 else 00135 return is_gimple_mem_rhs; 00136 } 00137 00138 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */ 00139 00140 bool 00141 is_gimple_lvalue (tree t) 00142 { 00143 return (is_gimple_addressable (t) 00144 || TREE_CODE (t) == WITH_SIZE_EXPR 00145 /* These are complex lvalues, but don't have addresses, so they 00146 go here. */ 00147 || TREE_CODE (t) == BIT_FIELD_REF); 00148 } 00149 00150 /* Return true if T is a GIMPLE condition. */ 00151 00152 bool 00153 is_gimple_condexpr (tree t) 00154 { 00155 return (is_gimple_val (t) || COMPARISON_CLASS_P (t)); 00156 } 00157 00158 /* Return true if T is something whose address can be taken. */ 00159 00160 bool 00161 is_gimple_addressable (tree t) 00162 { 00163 return (is_gimple_id (t) || handled_component_p (t) 00164 || INDIRECT_REF_P (t)); 00165 } 00166 00167 /* Return true if T is function invariant. Or rather a restricted 00168 form of function invariant. */ 00169 00170 bool 00171 is_gimple_min_invariant (tree t) 00172 { 00173 switch (TREE_CODE (t)) 00174 { 00175 case ADDR_EXPR: 00176 return TREE_INVARIANT (t); 00177 00178 case INTEGER_CST: 00179 case REAL_CST: 00180 case STRING_CST: 00181 case COMPLEX_CST: 00182 case VECTOR_CST: 00183 return true; 00184 00185 default: 00186 return false; 00187 } 00188 } 00189 00190 /* Return true if T looks like a valid GIMPLE statement. */ 00191 00192 bool 00193 is_gimple_stmt (tree t) 00194 { 00195 enum tree_code code = TREE_CODE (t); 00196 00197 if (IS_EMPTY_STMT (t)) 00198 return 1; 00199 00200 switch (code) 00201 { 00202 case BIND_EXPR: 00203 case COND_EXPR: 00204 /* These are only valid if they're void. */ 00205 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t)); 00206 00207 case SWITCH_EXPR: 00208 case GOTO_EXPR: 00209 case RETURN_EXPR: 00210 case LABEL_EXPR: 00211 case CASE_LABEL_EXPR: 00212 case TRY_CATCH_EXPR: 00213 case TRY_FINALLY_EXPR: 00214 case EH_FILTER_EXPR: 00215 case CATCH_EXPR: 00216 case ASM_EXPR: 00217 case RESX_EXPR: 00218 case PHI_NODE: 00219 case STATEMENT_LIST: 00220 /* These are always void. */ 00221 return true; 00222 00223 case CALL_EXPR: 00224 case MODIFY_EXPR: 00225 /* These are valid regardless of their type. */ 00226 return true; 00227 00228 default: 00229 return false; 00230 } 00231 } 00232 00233 /* Return true if T is a variable. */ 00234 00235 bool 00236 is_gimple_variable (tree t) 00237 { 00238 return (TREE_CODE (t) == VAR_DECL 00239 || TREE_CODE (t) == PARM_DECL 00240 || TREE_CODE (t) == RESULT_DECL 00241 || TREE_CODE (t) == SSA_NAME); 00242 } 00243 00244 /* Return true if T is a GIMPLE identifier (something with an address). */ 00245 00246 static inline bool 00247 is_gimple_id (tree t) 00248 { 00249 return (is_gimple_variable (t) 00250 || TREE_CODE (t) == FUNCTION_DECL 00251 || TREE_CODE (t) == LABEL_DECL 00252 || TREE_CODE (t) == CONST_DECL 00253 /* Allow string constants, since they are addressable. */ 00254 || TREE_CODE (t) == STRING_CST); 00255 } 00256 00257 /* Return true if TYPE is a suitable type for a scalar register variable. */ 00258 00259 bool 00260 is_gimple_reg_type (tree type) 00261 { 00262 return (!AGGREGATE_TYPE_P (type) 00263 && TREE_CODE (type) != COMPLEX_TYPE); 00264 } 00265 00266 00267 /* Return true if T is a scalar register variable. */ 00268 00269 bool 00270 is_gimple_reg (tree t) 00271 { 00272 if (TREE_CODE (t) == SSA_NAME) 00273 t = SSA_NAME_VAR (t); 00274 00275 if (!is_gimple_variable (t)) 00276 return false; 00277 if (!is_gimple_reg_type (TREE_TYPE (t))) 00278 return false; 00279 00280 /* A volatile decl is not acceptable because we can't reuse it as 00281 needed. We need to copy it into a temp first. */ 00282 if (TREE_THIS_VOLATILE (t)) 00283 return false; 00284 00285 /* We define "registers" as things that can be renamed as needed, 00286 which with our infrastructure does not apply to memory. */ 00287 if (needs_to_live_in_memory (t)) 00288 return false; 00289 00290 /* Hard register variables are an interesting case. For those that 00291 are call-clobbered, we don't know where all the calls are, since 00292 we don't (want to) take into account which operations will turn 00293 into libcalls at the rtl level. For those that are call-saved, 00294 we don't currently model the fact that calls may in fact change 00295 global hard registers, nor do we examine ASM_CLOBBERS at the tree 00296 level, and so miss variable changes that might imply. All around, 00297 it seems safest to not do too much optimization with these at the 00298 tree level at all. We'll have to rely on the rtl optimizers to 00299 clean this up, as there we've got all the appropriate bits exposed. */ 00300 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)) 00301 return false; 00302 00303 return true; 00304 } 00305 00306 /* Returns true if T is a GIMPLE formal temporary variable. */ 00307 00308 bool 00309 is_gimple_formal_tmp_var (tree t) 00310 { 00311 if (TREE_CODE (t) == SSA_NAME) 00312 return true; 00313 00314 return TREE_CODE (t) == VAR_DECL && DECL_GIMPLE_FORMAL_TEMP_P (t); 00315 } 00316 00317 /* Returns true if T is a GIMPLE formal temporary register variable. */ 00318 00319 bool 00320 is_gimple_formal_tmp_reg (tree t) 00321 { 00322 /* The intent of this is to get hold of a value that won't change. 00323 An SSA_NAME qualifies no matter if its of a user variable or not. */ 00324 if (TREE_CODE (t) == SSA_NAME) 00325 return true; 00326 00327 /* We don't know the lifetime characteristics of user variables. */ 00328 if (!is_gimple_formal_tmp_var (t)) 00329 return false; 00330 00331 /* Finally, it must be capable of being placed in a register. */ 00332 return is_gimple_reg (t); 00333 } 00334 00335 /* Return true if T is a GIMPLE variable whose address is not needed. */ 00336 00337 bool 00338 is_gimple_non_addressable (tree t) 00339 { 00340 if (TREE_CODE (t) == SSA_NAME) 00341 t = SSA_NAME_VAR (t); 00342 00343 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t)); 00344 } 00345 00346 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */ 00347 00348 bool 00349 is_gimple_val (tree t) 00350 { 00351 /* Make loads from volatiles and memory vars explicit. */ 00352 if (is_gimple_variable (t) 00353 && is_gimple_reg_type (TREE_TYPE (t)) 00354 && !is_gimple_reg (t)) 00355 return false; 00356 00357 /* FIXME make these decls. That can happen only when we expose the 00358 entire landing-pad construct at the tree level. */ 00359 if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR) 00360 return 1; 00361 00362 return (is_gimple_variable (t) || is_gimple_min_invariant (t)); 00363 } 00364 00365 /* Similarly, but accept hard registers as inputs to asm statements. */ 00366 00367 bool 00368 is_gimple_asm_val (tree t) 00369 { 00370 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t)) 00371 return true; 00372 00373 return is_gimple_val (t); 00374 } 00375 00376 /* Return true if T is a GIMPLE minimal lvalue. */ 00377 00378 bool 00379 is_gimple_min_lval (tree t) 00380 { 00381 return (is_gimple_id (t) 00382 || TREE_CODE (t) == INDIRECT_REF); 00383 } 00384 00385 /* Return true if T is a typecast operation. */ 00386 00387 bool 00388 is_gimple_cast (tree t) 00389 { 00390 return (TREE_CODE (t) == NOP_EXPR 00391 || TREE_CODE (t) == CONVERT_EXPR 00392 || TREE_CODE (t) == FIX_TRUNC_EXPR 00393 || TREE_CODE (t) == FIX_CEIL_EXPR 00394 || TREE_CODE (t) == FIX_FLOOR_EXPR 00395 || TREE_CODE (t) == FIX_ROUND_EXPR); 00396 } 00397 00398 /* Return true if T is a valid op0 of a CALL_EXPR. */ 00399 00400 bool 00401 is_gimple_call_addr (tree t) 00402 { 00403 return (TREE_CODE (t) == OBJ_TYPE_REF 00404 || is_gimple_val (t)); 00405 } 00406 00407 /* If T makes a function call, return the corresponding CALL_EXPR operand. 00408 Otherwise, return NULL_TREE. */ 00409 00410 tree 00411 get_call_expr_in (tree t) 00412 { 00413 if (TREE_CODE (t) == MODIFY_EXPR) 00414 t = TREE_OPERAND (t, 1); 00415 if (TREE_CODE (t) == WITH_SIZE_EXPR) 00416 t = TREE_OPERAND (t, 0); 00417 if (TREE_CODE (t) == CALL_EXPR) 00418 return t; 00419 return NULL_TREE; 00420 } 00421 00422 /* Given a memory reference expression T, return its base address. 00423 The base address of a memory reference expression is the main 00424 object being referenced. For instance, the base address for 00425 'array[i].fld[j]' is 'array'. You can think of this as stripping 00426 away the offset part from a memory address. 00427 00428 This function calls handled_component_p to strip away all the inner 00429 parts of the memory reference until it reaches the base object. */ 00430 00431 tree 00432 get_base_address (tree t) 00433 { 00434 while (handled_component_p (t)) 00435 t = TREE_OPERAND (t, 0); 00436 00437 if (SSA_VAR_P (t) 00438 || TREE_CODE (t) == STRING_CST 00439 || TREE_CODE (t) == CONSTRUCTOR 00440 || INDIRECT_REF_P (t)) 00441 return t; 00442 else 00443 return NULL_TREE; 00444 } 00445 00446 void 00447 recalculate_side_effects (tree t) 00448 { 00449 enum tree_code code = TREE_CODE (t); 00450 int len = TREE_CODE_LENGTH (code); 00451 int i; 00452 00453 switch (TREE_CODE_CLASS (code)) 00454 { 00455 case tcc_expression: 00456 switch (code) 00457 { 00458 case INIT_EXPR: 00459 case MODIFY_EXPR: 00460 case VA_ARG_EXPR: 00461 case PREDECREMENT_EXPR: 00462 case PREINCREMENT_EXPR: 00463 case POSTDECREMENT_EXPR: 00464 case POSTINCREMENT_EXPR: 00465 /* All of these have side-effects, no matter what their 00466 operands are. */ 00467 return; 00468 00469 default: 00470 break; 00471 } 00472 /* Fall through. */ 00473 00474 case tcc_comparison: /* a comparison expression */ 00475 case tcc_unary: /* a unary arithmetic expression */ 00476 case tcc_binary: /* a binary arithmetic expression */ 00477 case tcc_reference: /* a reference */ 00478 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t); 00479 for (i = 0; i < len; ++i) 00480 { 00481 tree op = TREE_OPERAND (t, i); 00482 if (op && TREE_SIDE_EFFECTS (op)) 00483 TREE_SIDE_EFFECTS (t) = 1; 00484 } 00485 break; 00486 00487 default: 00488 /* Can never be used with non-expressions. */ 00489 gcc_unreachable (); 00490 } 00491 }
1.5.6