c-gimplify.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Tree lowering pass. This pass gimplifies the tree representation built
  2. by the C-based front ends. The structure of gimplified, or
  3. language-independent, trees is dictated by the grammar described in this
  4. file.
  5. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  6. Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net>
  7. Re-written to support lowering of whole function trees, documentation
  8. and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com>
  9. This file is part of GCC.
  10. GCC is free software; you can redistribute it and/or modify it under
  11. the terms of the GNU General Public License as published by the Free
  12. Software Foundation; either version 3, or (at your option) any later
  13. version.
  14. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with GCC; see the file COPYING3. If not see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "config.h"
  22. #include "system.h"
  23. #include "coretypes.h"
  24. #include "tm.h"
  25. #include "hash-set.h"
  26. #include "machmode.h"
  27. #include "vec.h"
  28. #include "double-int.h"
  29. #include "input.h"
  30. #include "alias.h"
  31. #include "symtab.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "fold-const.h"
  36. #include "c-common.h"
  37. #include "predict.h"
  38. #include "vec.h"
  39. #include "hashtab.h"
  40. #include "hash-set.h"
  41. #include "machmode.h"
  42. #include "hard-reg-set.h"
  43. #include "input.h"
  44. #include "function.h"
  45. #include "basic-block.h"
  46. #include "tree-ssa-alias.h"
  47. #include "internal-fn.h"
  48. #include "gimple-expr.h"
  49. #include "is-a.h"
  50. #include "gimple.h"
  51. #include "gimplify.h"
  52. #include "tree-inline.h"
  53. #include "diagnostic-core.h"
  54. #include "langhooks.h"
  55. #include "langhooks-def.h"
  56. #include "flags.h"
  57. #include "dumpfile.h"
  58. #include "c-pretty-print.h"
  59. #include "hash-map.h"
  60. #include "plugin-api.h"
  61. #include "ipa-ref.h"
  62. #include "cgraph.h"
  63. #include "cilk.h"
  64. #include "c-ubsan.h"
  65. /* The gimplification pass converts the language-dependent trees
  66. (ld-trees) emitted by the parser into language-independent trees
  67. (li-trees) that are the target of SSA analysis and transformations.
  68. Language-independent trees are based on the SIMPLE intermediate
  69. representation used in the McCAT compiler framework:
  70. "Designing the McCAT Compiler Based on a Family of Structured
  71. Intermediate Representations,"
  72. L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
  73. Proceedings of the 5th International Workshop on Languages and
  74. Compilers for Parallel Computing, no. 757 in Lecture Notes in
  75. Computer Science, New Haven, Connecticut, pp. 406-420,
  76. Springer-Verlag, August 3-5, 1992.
  77. http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
  78. Basically, we walk down gimplifying the nodes that we encounter. As we
  79. walk back up, we check that they fit our constraints, and copy them
  80. into temporaries if not. */
  81. /* Callback for c_genericize. */
  82. static tree
  83. ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees, void *data)
  84. {
  85. hash_set<tree> *pset = (hash_set<tree> *) data;
  86. /* Since walk_tree doesn't call the callback function on the decls
  87. in BIND_EXPR_VARS, we have to walk them manually. */
  88. if (TREE_CODE (*tp) == BIND_EXPR)
  89. {
  90. for (tree decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
  91. {
  92. if (TREE_STATIC (decl))
  93. {
  94. *walk_subtrees = 0;
  95. continue;
  96. }
  97. walk_tree (&DECL_INITIAL (decl), ubsan_walk_array_refs_r, pset,
  98. pset);
  99. walk_tree (&DECL_SIZE (decl), ubsan_walk_array_refs_r, pset, pset);
  100. walk_tree (&DECL_SIZE_UNIT (decl), ubsan_walk_array_refs_r, pset,
  101. pset);
  102. }
  103. }
  104. else if (TREE_CODE (*tp) == ADDR_EXPR
  105. && TREE_CODE (TREE_OPERAND (*tp, 0)) == ARRAY_REF)
  106. {
  107. ubsan_maybe_instrument_array_ref (&TREE_OPERAND (*tp, 0), true);
  108. /* Make sure ubsan_maybe_instrument_array_ref is not called again
  109. on the ARRAY_REF, the above call might not instrument anything
  110. as the index might be constant or masked, so ensure it is not
  111. walked again and walk its subtrees manually. */
  112. tree aref = TREE_OPERAND (*tp, 0);
  113. pset->add (aref);
  114. *walk_subtrees = 0;
  115. walk_tree (&TREE_OPERAND (aref, 0), ubsan_walk_array_refs_r, pset, pset);
  116. walk_tree (&TREE_OPERAND (aref, 1), ubsan_walk_array_refs_r, pset, pset);
  117. walk_tree (&TREE_OPERAND (aref, 2), ubsan_walk_array_refs_r, pset, pset);
  118. walk_tree (&TREE_OPERAND (aref, 3), ubsan_walk_array_refs_r, pset, pset);
  119. }
  120. else if (TREE_CODE (*tp) == ARRAY_REF)
  121. ubsan_maybe_instrument_array_ref (tp, false);
  122. return NULL_TREE;
  123. }
  124. /* Gimplification of statement trees. */
  125. /* Convert the tree representation of FNDECL from C frontend trees to
  126. GENERIC. */
  127. void
  128. c_genericize (tree fndecl)
  129. {
  130. FILE *dump_orig;
  131. int local_dump_flags;
  132. struct cgraph_node *cgn;
  133. if (flag_sanitize & SANITIZE_BOUNDS)
  134. {
  135. hash_set<tree> pset;
  136. walk_tree (&DECL_SAVED_TREE (fndecl), ubsan_walk_array_refs_r, &pset,
  137. &pset);
  138. }
  139. /* Dump the C-specific tree IR. */
  140. dump_orig = get_dump_info (TDI_original, &local_dump_flags);
  141. if (dump_orig)
  142. {
  143. fprintf (dump_orig, "\n;; Function %s",
  144. lang_hooks.decl_printable_name (fndecl, 2));
  145. fprintf (dump_orig, " (%s)\n",
  146. (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
  147. : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
  148. fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
  149. fprintf (dump_orig, "\n");
  150. if (local_dump_flags & TDF_RAW)
  151. dump_node (DECL_SAVED_TREE (fndecl),
  152. TDF_SLIM | local_dump_flags, dump_orig);
  153. else
  154. print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
  155. fprintf (dump_orig, "\n");
  156. }
  157. /* Dump all nested functions now. */
  158. cgn = cgraph_node::get_create (fndecl);
  159. for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
  160. c_genericize (cgn->decl);
  161. }
  162. static void
  163. add_block_to_enclosing (tree block)
  164. {
  165. unsigned i;
  166. tree enclosing;
  167. gbind *bind;
  168. vec<gbind *> stack = gimple_bind_expr_stack ();
  169. FOR_EACH_VEC_ELT (stack, i, bind)
  170. if (gimple_bind_block (bind))
  171. break;
  172. enclosing = gimple_bind_block (bind);
  173. BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
  174. }
  175. /* Genericize a scope by creating a new BIND_EXPR.
  176. BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
  177. In the latter case, we need to create a new BLOCK and add it to the
  178. BLOCK_SUBBLOCKS of the enclosing block.
  179. BODY is a chain of C _STMT nodes for the contents of the scope, to be
  180. genericized. */
  181. tree
  182. c_build_bind_expr (location_t loc, tree block, tree body)
  183. {
  184. tree decls, bind;
  185. if (block == NULL_TREE)
  186. decls = NULL_TREE;
  187. else if (TREE_CODE (block) == BLOCK)
  188. decls = BLOCK_VARS (block);
  189. else
  190. {
  191. decls = block;
  192. if (DECL_ARTIFICIAL (decls))
  193. block = NULL_TREE;
  194. else
  195. {
  196. block = make_node (BLOCK);
  197. BLOCK_VARS (block) = decls;
  198. add_block_to_enclosing (block);
  199. }
  200. }
  201. if (!body)
  202. body = build_empty_stmt (loc);
  203. if (decls || block)
  204. {
  205. bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
  206. TREE_SIDE_EFFECTS (bind) = 1;
  207. SET_EXPR_LOCATION (bind, loc);
  208. }
  209. else
  210. bind = body;
  211. return bind;
  212. }
  213. /* Gimplification of expression trees. */
  214. /* Do C-specific gimplification on *EXPR_P. PRE_P and POST_P are as in
  215. gimplify_expr. */
  216. int
  217. c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
  218. gimple_seq *post_p ATTRIBUTE_UNUSED)
  219. {
  220. enum tree_code code = TREE_CODE (*expr_p);
  221. switch (code)
  222. {
  223. case LSHIFT_EXPR:
  224. case RSHIFT_EXPR:
  225. {
  226. /* We used to convert the right operand of a shift-expression
  227. to an integer_type_node in the FEs. But it is unnecessary
  228. and not desirable for diagnostics and sanitizers. We keep
  229. this here to not pessimize the code, but we convert to an
  230. unsigned type, because negative shift counts are undefined
  231. anyway.
  232. We should get rid of this conversion when we have a proper
  233. type demotion/promotion pass. */
  234. tree *op1_p = &TREE_OPERAND (*expr_p, 1);
  235. if (TREE_CODE (TREE_TYPE (*op1_p)) != VECTOR_TYPE
  236. && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
  237. unsigned_type_node)
  238. && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
  239. integer_type_node))
  240. *op1_p = convert (unsigned_type_node, *op1_p);
  241. break;
  242. }
  243. case DECL_EXPR:
  244. /* This is handled mostly by gimplify.c, but we have to deal with
  245. not warning about int x = x; as it is a GCC extension to turn off
  246. this warning but only if warn_init_self is zero. */
  247. if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
  248. && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
  249. && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
  250. && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
  251. && !warn_init_self)
  252. TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
  253. break;
  254. case PREINCREMENT_EXPR:
  255. case PREDECREMENT_EXPR:
  256. case POSTINCREMENT_EXPR:
  257. case POSTDECREMENT_EXPR:
  258. {
  259. tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
  260. if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
  261. {
  262. if (!TYPE_OVERFLOW_WRAPS (type))
  263. type = unsigned_type_for (type);
  264. return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
  265. }
  266. break;
  267. }
  268. case CILK_SPAWN_STMT:
  269. gcc_assert
  270. (fn_contains_cilk_spawn_p (cfun)
  271. && cilk_detect_spawn_and_unwrap (expr_p));
  272. /* If errors are seen, then just process it as a CALL_EXPR. */
  273. if (!seen_error ())
  274. return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
  275. case MODIFY_EXPR:
  276. case INIT_EXPR:
  277. case CALL_EXPR:
  278. if (fn_contains_cilk_spawn_p (cfun)
  279. && cilk_detect_spawn_and_unwrap (expr_p)
  280. /* If an error is found, the spawn wrapper is removed and the
  281. original expression (MODIFY/INIT/CALL_EXPR) is processes as
  282. it is supposed to be. */
  283. && !seen_error ())
  284. return (enum gimplify_status) gimplify_cilk_spawn (expr_p);
  285. default:;
  286. }
  287. return GS_UNHANDLED;
  288. }