gimple-expr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /* Gimple decl, type, and expression support functions.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Contributed by Aldy Hernandez <aldyh@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "input.h"
  34. #include "function.h"
  35. #include "basic-block.h"
  36. #include "tree-ssa-alias.h"
  37. #include "internal-fn.h"
  38. #include "tree-eh.h"
  39. #include "gimple-expr.h"
  40. #include "is-a.h"
  41. #include "gimple.h"
  42. #include "stringpool.h"
  43. #include "gimplify.h"
  44. #include "stor-layout.h"
  45. #include "demangle.h"
  46. #include "gimple-ssa.h"
  47. /* ----- Type related ----- */
  48. /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
  49. useless type conversion, otherwise return false.
  50. This function implicitly defines the middle-end type system. With
  51. the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
  52. holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
  53. the following invariants shall be fulfilled:
  54. 1) useless_type_conversion_p is transitive.
  55. If a < b and b < c then a < c.
  56. 2) useless_type_conversion_p is not symmetric.
  57. From a < b does not follow a > b.
  58. 3) Types define the available set of operations applicable to values.
  59. A type conversion is useless if the operations for the target type
  60. is a subset of the operations for the source type. For example
  61. casts to void* are useless, casts from void* are not (void* can't
  62. be dereferenced or offsetted, but copied, hence its set of operations
  63. is a strict subset of that of all other data pointer types). Casts
  64. to const T* are useless (can't be written to), casts from const T*
  65. to T* are not. */
  66. bool
  67. useless_type_conversion_p (tree outer_type, tree inner_type)
  68. {
  69. /* Do the following before stripping toplevel qualifiers. */
  70. if (POINTER_TYPE_P (inner_type)
  71. && POINTER_TYPE_P (outer_type))
  72. {
  73. /* Do not lose casts between pointers to different address spaces. */
  74. if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
  75. != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
  76. return false;
  77. }
  78. /* From now on qualifiers on value types do not matter. */
  79. inner_type = TYPE_MAIN_VARIANT (inner_type);
  80. outer_type = TYPE_MAIN_VARIANT (outer_type);
  81. if (inner_type == outer_type)
  82. return true;
  83. /* If we know the canonical types, compare them. */
  84. if (TYPE_CANONICAL (inner_type)
  85. && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
  86. return true;
  87. /* Changes in machine mode are never useless conversions unless we
  88. deal with aggregate types in which case we defer to later checks. */
  89. if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
  90. && !AGGREGATE_TYPE_P (inner_type))
  91. return false;
  92. /* If both the inner and outer types are integral types, then the
  93. conversion is not necessary if they have the same mode and
  94. signedness and precision, and both or neither are boolean. */
  95. if (INTEGRAL_TYPE_P (inner_type)
  96. && INTEGRAL_TYPE_P (outer_type))
  97. {
  98. /* Preserve changes in signedness or precision. */
  99. if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
  100. || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
  101. return false;
  102. /* Preserve conversions to/from BOOLEAN_TYPE if types are not
  103. of precision one. */
  104. if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
  105. != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
  106. && TYPE_PRECISION (outer_type) != 1)
  107. return false;
  108. /* We don't need to preserve changes in the types minimum or
  109. maximum value in general as these do not generate code
  110. unless the types precisions are different. */
  111. return true;
  112. }
  113. /* Scalar floating point types with the same mode are compatible. */
  114. else if (SCALAR_FLOAT_TYPE_P (inner_type)
  115. && SCALAR_FLOAT_TYPE_P (outer_type))
  116. return true;
  117. /* Fixed point types with the same mode are compatible. */
  118. else if (FIXED_POINT_TYPE_P (inner_type)
  119. && FIXED_POINT_TYPE_P (outer_type))
  120. return true;
  121. /* We need to take special care recursing to pointed-to types. */
  122. else if (POINTER_TYPE_P (inner_type)
  123. && POINTER_TYPE_P (outer_type))
  124. {
  125. /* Do not lose casts to function pointer types. */
  126. if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
  127. || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
  128. && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
  129. || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
  130. return false;
  131. /* We do not care for const qualification of the pointed-to types
  132. as const qualification has no semantic value to the middle-end. */
  133. /* Otherwise pointers/references are equivalent. */
  134. return true;
  135. }
  136. /* Recurse for complex types. */
  137. else if (TREE_CODE (inner_type) == COMPLEX_TYPE
  138. && TREE_CODE (outer_type) == COMPLEX_TYPE)
  139. return useless_type_conversion_p (TREE_TYPE (outer_type),
  140. TREE_TYPE (inner_type));
  141. /* Recurse for vector types with the same number of subparts. */
  142. else if (TREE_CODE (inner_type) == VECTOR_TYPE
  143. && TREE_CODE (outer_type) == VECTOR_TYPE
  144. && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
  145. return useless_type_conversion_p (TREE_TYPE (outer_type),
  146. TREE_TYPE (inner_type));
  147. else if (TREE_CODE (inner_type) == ARRAY_TYPE
  148. && TREE_CODE (outer_type) == ARRAY_TYPE)
  149. {
  150. /* Preserve string attributes. */
  151. if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
  152. return false;
  153. /* Conversions from array types with unknown extent to
  154. array types with known extent are not useless. */
  155. if (!TYPE_DOMAIN (inner_type)
  156. && TYPE_DOMAIN (outer_type))
  157. return false;
  158. /* Nor are conversions from array types with non-constant size to
  159. array types with constant size or to different size. */
  160. if (TYPE_SIZE (outer_type)
  161. && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
  162. && (!TYPE_SIZE (inner_type)
  163. || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
  164. || !tree_int_cst_equal (TYPE_SIZE (outer_type),
  165. TYPE_SIZE (inner_type))))
  166. return false;
  167. /* Check conversions between arrays with partially known extents.
  168. If the array min/max values are constant they have to match.
  169. Otherwise allow conversions to unknown and variable extents.
  170. In particular this declares conversions that may change the
  171. mode to BLKmode as useless. */
  172. if (TYPE_DOMAIN (inner_type)
  173. && TYPE_DOMAIN (outer_type)
  174. && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
  175. {
  176. tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
  177. tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
  178. tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
  179. tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
  180. /* After gimplification a variable min/max value carries no
  181. additional information compared to a NULL value. All that
  182. matters has been lowered to be part of the IL. */
  183. if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
  184. inner_min = NULL_TREE;
  185. if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
  186. outer_min = NULL_TREE;
  187. if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
  188. inner_max = NULL_TREE;
  189. if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
  190. outer_max = NULL_TREE;
  191. /* Conversions NULL / variable <- cst are useless, but not
  192. the other way around. */
  193. if (outer_min
  194. && (!inner_min
  195. || !tree_int_cst_equal (inner_min, outer_min)))
  196. return false;
  197. if (outer_max
  198. && (!inner_max
  199. || !tree_int_cst_equal (inner_max, outer_max)))
  200. return false;
  201. }
  202. /* Recurse on the element check. */
  203. return useless_type_conversion_p (TREE_TYPE (outer_type),
  204. TREE_TYPE (inner_type));
  205. }
  206. else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
  207. || TREE_CODE (inner_type) == METHOD_TYPE)
  208. && TREE_CODE (inner_type) == TREE_CODE (outer_type))
  209. {
  210. tree outer_parm, inner_parm;
  211. /* If the return types are not compatible bail out. */
  212. if (!useless_type_conversion_p (TREE_TYPE (outer_type),
  213. TREE_TYPE (inner_type)))
  214. return false;
  215. /* Method types should belong to a compatible base class. */
  216. if (TREE_CODE (inner_type) == METHOD_TYPE
  217. && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
  218. TYPE_METHOD_BASETYPE (inner_type)))
  219. return false;
  220. /* A conversion to an unprototyped argument list is ok. */
  221. if (!prototype_p (outer_type))
  222. return true;
  223. /* If the unqualified argument types are compatible the conversion
  224. is useless. */
  225. if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
  226. return true;
  227. for (outer_parm = TYPE_ARG_TYPES (outer_type),
  228. inner_parm = TYPE_ARG_TYPES (inner_type);
  229. outer_parm && inner_parm;
  230. outer_parm = TREE_CHAIN (outer_parm),
  231. inner_parm = TREE_CHAIN (inner_parm))
  232. if (!useless_type_conversion_p
  233. (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
  234. TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
  235. return false;
  236. /* If there is a mismatch in the number of arguments the functions
  237. are not compatible. */
  238. if (outer_parm || inner_parm)
  239. return false;
  240. /* Defer to the target if necessary. */
  241. if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
  242. return comp_type_attributes (outer_type, inner_type) != 0;
  243. return true;
  244. }
  245. /* For aggregates we rely on TYPE_CANONICAL exclusively and require
  246. explicit conversions for types involving to be structurally
  247. compared types. */
  248. else if (AGGREGATE_TYPE_P (inner_type)
  249. && TREE_CODE (inner_type) == TREE_CODE (outer_type))
  250. return false;
  251. return false;
  252. }
  253. /* ----- Decl related ----- */
  254. /* Set sequence SEQ to be the GIMPLE body for function FN. */
  255. void
  256. gimple_set_body (tree fndecl, gimple_seq seq)
  257. {
  258. struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
  259. if (fn == NULL)
  260. {
  261. /* If FNDECL still does not have a function structure associated
  262. with it, then it does not make sense for it to receive a
  263. GIMPLE body. */
  264. gcc_assert (seq == NULL);
  265. }
  266. else
  267. fn->gimple_body = seq;
  268. }
  269. /* Return the body of GIMPLE statements for function FN. After the
  270. CFG pass, the function body doesn't exist anymore because it has
  271. been split up into basic blocks. In this case, it returns
  272. NULL. */
  273. gimple_seq
  274. gimple_body (tree fndecl)
  275. {
  276. struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
  277. return fn ? fn->gimple_body : NULL;
  278. }
  279. /* Return true when FNDECL has Gimple body either in unlowered
  280. or CFG form. */
  281. bool
  282. gimple_has_body_p (tree fndecl)
  283. {
  284. struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
  285. return (gimple_body (fndecl) || (fn && fn->cfg));
  286. }
  287. /* Return a printable name for symbol DECL. */
  288. const char *
  289. gimple_decl_printable_name (tree decl, int verbosity)
  290. {
  291. if (!DECL_NAME (decl))
  292. return NULL;
  293. if (DECL_ASSEMBLER_NAME_SET_P (decl))
  294. {
  295. const char *str, *mangled_str;
  296. int dmgl_opts = DMGL_NO_OPTS;
  297. if (verbosity >= 2)
  298. {
  299. dmgl_opts = DMGL_VERBOSE
  300. | DMGL_ANSI
  301. | DMGL_GNU_V3
  302. | DMGL_RET_POSTFIX;
  303. if (TREE_CODE (decl) == FUNCTION_DECL)
  304. dmgl_opts |= DMGL_PARAMS;
  305. }
  306. mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
  307. str = cplus_demangle_v3 (mangled_str, dmgl_opts);
  308. return (str) ? str : mangled_str;
  309. }
  310. return IDENTIFIER_POINTER (DECL_NAME (decl));
  311. }
  312. /* Create a new VAR_DECL and copy information from VAR to it. */
  313. tree
  314. copy_var_decl (tree var, tree name, tree type)
  315. {
  316. tree copy = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, name, type);
  317. TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (var);
  318. TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (var);
  319. DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (var);
  320. DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (var);
  321. DECL_IGNORED_P (copy) = DECL_IGNORED_P (var);
  322. DECL_CONTEXT (copy) = DECL_CONTEXT (var);
  323. TREE_NO_WARNING (copy) = TREE_NO_WARNING (var);
  324. TREE_USED (copy) = 1;
  325. DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
  326. DECL_ATTRIBUTES (copy) = DECL_ATTRIBUTES (var);
  327. return copy;
  328. }
  329. /* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
  330. coalescing together, false otherwise.
  331. This must stay consistent with var_map_base_init in tree-ssa-live.c. */
  332. bool
  333. gimple_can_coalesce_p (tree name1, tree name2)
  334. {
  335. /* First check the SSA_NAME's associated DECL. We only want to
  336. coalesce if they have the same DECL or both have no associated DECL. */
  337. tree var1 = SSA_NAME_VAR (name1);
  338. tree var2 = SSA_NAME_VAR (name2);
  339. var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
  340. var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
  341. if (var1 != var2)
  342. return false;
  343. /* Now check the types. If the types are the same, then we should
  344. try to coalesce V1 and V2. */
  345. tree t1 = TREE_TYPE (name1);
  346. tree t2 = TREE_TYPE (name2);
  347. if (t1 == t2)
  348. return true;
  349. /* If the types are not the same, check for a canonical type match. This
  350. (for example) allows coalescing when the types are fundamentally the
  351. same, but just have different names.
  352. Note pointer types with different address spaces may have the same
  353. canonical type. Those are rejected for coalescing by the
  354. types_compatible_p check. */
  355. if (TYPE_CANONICAL (t1)
  356. && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
  357. && types_compatible_p (t1, t2))
  358. return true;
  359. return false;
  360. }
  361. /* Strip off a legitimate source ending from the input string NAME of
  362. length LEN. Rather than having to know the names used by all of
  363. our front ends, we strip off an ending of a period followed by
  364. up to five characters. (Java uses ".class".) */
  365. static inline void
  366. remove_suffix (char *name, int len)
  367. {
  368. int i;
  369. for (i = 2; i < 8 && len > i; i++)
  370. {
  371. if (name[len - i] == '.')
  372. {
  373. name[len - i] = '\0';
  374. break;
  375. }
  376. }
  377. }
  378. /* Create a new temporary name with PREFIX. Return an identifier. */
  379. static GTY(()) unsigned int tmp_var_id_num;
  380. tree
  381. create_tmp_var_name (const char *prefix)
  382. {
  383. char *tmp_name;
  384. if (prefix)
  385. {
  386. char *preftmp = ASTRDUP (prefix);
  387. remove_suffix (preftmp, strlen (preftmp));
  388. clean_symbol_name (preftmp);
  389. prefix = preftmp;
  390. }
  391. ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix ? prefix : "T", tmp_var_id_num++);
  392. return get_identifier (tmp_name);
  393. }
  394. /* Create a new temporary variable declaration of type TYPE.
  395. Do NOT push it into the current binding. */
  396. tree
  397. create_tmp_var_raw (tree type, const char *prefix)
  398. {
  399. tree tmp_var;
  400. tmp_var = build_decl (input_location,
  401. VAR_DECL, prefix ? create_tmp_var_name (prefix) : NULL,
  402. type);
  403. /* The variable was declared by the compiler. */
  404. DECL_ARTIFICIAL (tmp_var) = 1;
  405. /* And we don't want debug info for it. */
  406. DECL_IGNORED_P (tmp_var) = 1;
  407. /* Make the variable writable. */
  408. TREE_READONLY (tmp_var) = 0;
  409. DECL_EXTERNAL (tmp_var) = 0;
  410. TREE_STATIC (tmp_var) = 0;
  411. TREE_USED (tmp_var) = 1;
  412. return tmp_var;
  413. }
  414. /* Create a new temporary variable declaration of type TYPE. DO push the
  415. variable into the current binding. Further, assume that this is called
  416. only from gimplification or optimization, at which point the creation of
  417. certain types are bugs. */
  418. tree
  419. create_tmp_var (tree type, const char *prefix)
  420. {
  421. tree tmp_var;
  422. /* We don't allow types that are addressable (meaning we can't make copies),
  423. or incomplete. We also used to reject every variable size objects here,
  424. but now support those for which a constant upper bound can be obtained.
  425. The processing for variable sizes is performed in gimple_add_tmp_var,
  426. point at which it really matters and possibly reached via paths not going
  427. through this function, e.g. after direct calls to create_tmp_var_raw. */
  428. gcc_assert (!TREE_ADDRESSABLE (type) && COMPLETE_TYPE_P (type));
  429. tmp_var = create_tmp_var_raw (type, prefix);
  430. gimple_add_tmp_var (tmp_var);
  431. return tmp_var;
  432. }
  433. /* Create a new temporary variable declaration of type TYPE by calling
  434. create_tmp_var and if TYPE is a vector or a complex number, mark the new
  435. temporary as gimple register. */
  436. tree
  437. create_tmp_reg (tree type, const char *prefix)
  438. {
  439. tree tmp;
  440. tmp = create_tmp_var (type, prefix);
  441. if (TREE_CODE (type) == COMPLEX_TYPE
  442. || TREE_CODE (type) == VECTOR_TYPE)
  443. DECL_GIMPLE_REG_P (tmp) = 1;
  444. return tmp;
  445. }
  446. /* Create a new temporary variable declaration of type TYPE by calling
  447. create_tmp_var and if TYPE is a vector or a complex number, mark the new
  448. temporary as gimple register. */
  449. tree
  450. create_tmp_reg_fn (struct function *fn, tree type, const char *prefix)
  451. {
  452. tree tmp;
  453. tmp = create_tmp_var_raw (type, prefix);
  454. gimple_add_tmp_var_fn (fn, tmp);
  455. if (TREE_CODE (type) == COMPLEX_TYPE
  456. || TREE_CODE (type) == VECTOR_TYPE)
  457. DECL_GIMPLE_REG_P (tmp) = 1;
  458. return tmp;
  459. }
  460. /* ----- Expression related ----- */
  461. /* Extract the operands and code for expression EXPR into *SUBCODE_P,
  462. *OP1_P, *OP2_P and *OP3_P respectively. */
  463. void
  464. extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
  465. tree *op2_p, tree *op3_p)
  466. {
  467. enum gimple_rhs_class grhs_class;
  468. *subcode_p = TREE_CODE (expr);
  469. grhs_class = get_gimple_rhs_class (*subcode_p);
  470. if (grhs_class == GIMPLE_TERNARY_RHS)
  471. {
  472. *op1_p = TREE_OPERAND (expr, 0);
  473. *op2_p = TREE_OPERAND (expr, 1);
  474. *op3_p = TREE_OPERAND (expr, 2);
  475. }
  476. else if (grhs_class == GIMPLE_BINARY_RHS)
  477. {
  478. *op1_p = TREE_OPERAND (expr, 0);
  479. *op2_p = TREE_OPERAND (expr, 1);
  480. *op3_p = NULL_TREE;
  481. }
  482. else if (grhs_class == GIMPLE_UNARY_RHS)
  483. {
  484. *op1_p = TREE_OPERAND (expr, 0);
  485. *op2_p = NULL_TREE;
  486. *op3_p = NULL_TREE;
  487. }
  488. else if (grhs_class == GIMPLE_SINGLE_RHS)
  489. {
  490. *op1_p = expr;
  491. *op2_p = NULL_TREE;
  492. *op3_p = NULL_TREE;
  493. }
  494. else
  495. gcc_unreachable ();
  496. }
  497. /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
  498. void
  499. gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
  500. tree *lhs_p, tree *rhs_p)
  501. {
  502. gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
  503. || TREE_CODE (cond) == TRUTH_NOT_EXPR
  504. || is_gimple_min_invariant (cond)
  505. || SSA_VAR_P (cond));
  506. extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
  507. /* Canonicalize conditionals of the form 'if (!VAL)'. */
  508. if (*code_p == TRUTH_NOT_EXPR)
  509. {
  510. *code_p = EQ_EXPR;
  511. gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
  512. *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
  513. }
  514. /* Canonicalize conditionals of the form 'if (VAL)' */
  515. else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
  516. {
  517. *code_p = NE_EXPR;
  518. gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
  519. *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
  520. }
  521. }
  522. /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
  523. bool
  524. is_gimple_lvalue (tree t)
  525. {
  526. return (is_gimple_addressable (t)
  527. || TREE_CODE (t) == WITH_SIZE_EXPR
  528. /* These are complex lvalues, but don't have addresses, so they
  529. go here. */
  530. || TREE_CODE (t) == BIT_FIELD_REF);
  531. }
  532. /* Return true if T is a GIMPLE condition. */
  533. bool
  534. is_gimple_condexpr (tree t)
  535. {
  536. return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
  537. && !tree_could_throw_p (t)
  538. && is_gimple_val (TREE_OPERAND (t, 0))
  539. && is_gimple_val (TREE_OPERAND (t, 1))));
  540. }
  541. /* Return true if T is a gimple address. */
  542. bool
  543. is_gimple_address (const_tree t)
  544. {
  545. tree op;
  546. if (TREE_CODE (t) != ADDR_EXPR)
  547. return false;
  548. op = TREE_OPERAND (t, 0);
  549. while (handled_component_p (op))
  550. {
  551. if ((TREE_CODE (op) == ARRAY_REF
  552. || TREE_CODE (op) == ARRAY_RANGE_REF)
  553. && !is_gimple_val (TREE_OPERAND (op, 1)))
  554. return false;
  555. op = TREE_OPERAND (op, 0);
  556. }
  557. if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
  558. return true;
  559. switch (TREE_CODE (op))
  560. {
  561. case PARM_DECL:
  562. case RESULT_DECL:
  563. case LABEL_DECL:
  564. case FUNCTION_DECL:
  565. case VAR_DECL:
  566. case CONST_DECL:
  567. return true;
  568. default:
  569. return false;
  570. }
  571. }
  572. /* Return true if T is a gimple invariant address. */
  573. bool
  574. is_gimple_invariant_address (const_tree t)
  575. {
  576. const_tree op;
  577. if (TREE_CODE (t) != ADDR_EXPR)
  578. return false;
  579. op = strip_invariant_refs (TREE_OPERAND (t, 0));
  580. if (!op)
  581. return false;
  582. if (TREE_CODE (op) == MEM_REF)
  583. {
  584. const_tree op0 = TREE_OPERAND (op, 0);
  585. return (TREE_CODE (op0) == ADDR_EXPR
  586. && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
  587. || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
  588. }
  589. return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
  590. }
  591. /* Return true if T is a gimple invariant address at IPA level
  592. (so addresses of variables on stack are not allowed). */
  593. bool
  594. is_gimple_ip_invariant_address (const_tree t)
  595. {
  596. const_tree op;
  597. if (TREE_CODE (t) != ADDR_EXPR)
  598. return false;
  599. op = strip_invariant_refs (TREE_OPERAND (t, 0));
  600. if (!op)
  601. return false;
  602. if (TREE_CODE (op) == MEM_REF)
  603. {
  604. const_tree op0 = TREE_OPERAND (op, 0);
  605. return (TREE_CODE (op0) == ADDR_EXPR
  606. && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
  607. || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
  608. }
  609. return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
  610. }
  611. /* Return true if T is a GIMPLE minimal invariant. It's a restricted
  612. form of function invariant. */
  613. bool
  614. is_gimple_min_invariant (const_tree t)
  615. {
  616. if (TREE_CODE (t) == ADDR_EXPR)
  617. return is_gimple_invariant_address (t);
  618. return is_gimple_constant (t);
  619. }
  620. /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
  621. form of gimple minimal invariant. */
  622. bool
  623. is_gimple_ip_invariant (const_tree t)
  624. {
  625. if (TREE_CODE (t) == ADDR_EXPR)
  626. return is_gimple_ip_invariant_address (t);
  627. return is_gimple_constant (t);
  628. }
  629. /* Return true if T is a non-aggregate register variable. */
  630. bool
  631. is_gimple_reg (tree t)
  632. {
  633. if (virtual_operand_p (t))
  634. return false;
  635. if (TREE_CODE (t) == SSA_NAME)
  636. return true;
  637. if (!is_gimple_variable (t))
  638. return false;
  639. if (!is_gimple_reg_type (TREE_TYPE (t)))
  640. return false;
  641. /* A volatile decl is not acceptable because we can't reuse it as
  642. needed. We need to copy it into a temp first. */
  643. if (TREE_THIS_VOLATILE (t))
  644. return false;
  645. /* We define "registers" as things that can be renamed as needed,
  646. which with our infrastructure does not apply to memory. */
  647. if (needs_to_live_in_memory (t))
  648. return false;
  649. /* Hard register variables are an interesting case. For those that
  650. are call-clobbered, we don't know where all the calls are, since
  651. we don't (want to) take into account which operations will turn
  652. into libcalls at the rtl level. For those that are call-saved,
  653. we don't currently model the fact that calls may in fact change
  654. global hard registers, nor do we examine ASM_CLOBBERS at the tree
  655. level, and so miss variable changes that might imply. All around,
  656. it seems safest to not do too much optimization with these at the
  657. tree level at all. We'll have to rely on the rtl optimizers to
  658. clean this up, as there we've got all the appropriate bits exposed. */
  659. if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
  660. return false;
  661. /* Complex and vector values must have been put into SSA-like form.
  662. That is, no assignments to the individual components. */
  663. if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
  664. || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
  665. return DECL_GIMPLE_REG_P (t);
  666. return true;
  667. }
  668. /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
  669. bool
  670. is_gimple_val (tree t)
  671. {
  672. /* Make loads from volatiles and memory vars explicit. */
  673. if (is_gimple_variable (t)
  674. && is_gimple_reg_type (TREE_TYPE (t))
  675. && !is_gimple_reg (t))
  676. return false;
  677. return (is_gimple_variable (t) || is_gimple_min_invariant (t));
  678. }
  679. /* Similarly, but accept hard registers as inputs to asm statements. */
  680. bool
  681. is_gimple_asm_val (tree t)
  682. {
  683. if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
  684. return true;
  685. return is_gimple_val (t);
  686. }
  687. /* Return true if T is a GIMPLE minimal lvalue. */
  688. bool
  689. is_gimple_min_lval (tree t)
  690. {
  691. if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
  692. return false;
  693. return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
  694. }
  695. /* Return true if T is a valid function operand of a CALL_EXPR. */
  696. bool
  697. is_gimple_call_addr (tree t)
  698. {
  699. return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
  700. }
  701. /* Return true if T is a valid address operand of a MEM_REF. */
  702. bool
  703. is_gimple_mem_ref_addr (tree t)
  704. {
  705. return (is_gimple_reg (t)
  706. || TREE_CODE (t) == INTEGER_CST
  707. || (TREE_CODE (t) == ADDR_EXPR
  708. && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
  709. || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
  710. }
  711. /* Mark X addressable. Unlike the langhook we expect X to be in gimple
  712. form and we don't do any syntax checking. */
  713. void
  714. mark_addressable (tree x)
  715. {
  716. while (handled_component_p (x))
  717. x = TREE_OPERAND (x, 0);
  718. if (TREE_CODE (x) == MEM_REF
  719. && TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
  720. x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
  721. if (TREE_CODE (x) != VAR_DECL
  722. && TREE_CODE (x) != PARM_DECL
  723. && TREE_CODE (x) != RESULT_DECL)
  724. return;
  725. TREE_ADDRESSABLE (x) = 1;
  726. /* Also mark the artificial SSA_NAME that points to the partition of X. */
  727. if (TREE_CODE (x) == VAR_DECL
  728. && !DECL_EXTERNAL (x)
  729. && !TREE_STATIC (x)
  730. && cfun->gimple_df != NULL
  731. && cfun->gimple_df->decls_to_pointers != NULL)
  732. {
  733. tree *namep = cfun->gimple_df->decls_to_pointers->get (x);
  734. if (namep)
  735. TREE_ADDRESSABLE (*namep) = 1;
  736. }
  737. }
  738. /* Returns true iff T is a valid RHS for an assignment to a renamed
  739. user -- or front-end generated artificial -- variable. */
  740. bool
  741. is_gimple_reg_rhs (tree t)
  742. {
  743. return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
  744. }
  745. #include "gt-gimple-expr.h"