cp-cilkplus.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* This file is part of the Intel(R) Cilk(TM) Plus support
  2. This file contains routines to handle Cilk Plus specific
  3. routines for the C++ Compiler.
  4. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  5. Contributed by Aldy Hernandez <aldyh@redhat.com>.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. GCC is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "cp-tree.h"
  22. #include "diagnostic-core.h"
  23. #include "tree-iterator.h"
  24. #include "tree-inline.h" /* for copy_tree_body_r. */
  25. #include "ggc.h"
  26. #include "cilk.h"
  27. /* Callback for cp_walk_tree to validate the body of a pragma simd loop
  28. or _cilk_for loop.
  29. This function is passed in as a function pointer to walk_tree. *TP is
  30. the current tree pointer, *WALK_SUBTREES is set to 0 by this function if
  31. recursing into TP's subtrees is unnecessary. *DATA is a bool variable that
  32. is set to false if an error has occured. */
  33. static tree
  34. cpp_validate_cilk_plus_loop_aux (tree *tp, int *walk_subtrees, void *data)
  35. {
  36. bool *valid = (bool *) data;
  37. if (!tp || !*tp)
  38. return NULL_TREE;
  39. location_t loc = EXPR_LOCATION (*tp);
  40. if (TREE_CODE (*tp) == THROW_EXPR)
  41. {
  42. error_at (loc, "throw expressions are not allowed inside loops "
  43. "marked with pragma simd");
  44. *walk_subtrees = 0;
  45. *valid = false;
  46. }
  47. else if (TREE_CODE (*tp) == TRY_BLOCK)
  48. {
  49. error_at (loc, "try statements are not allowed inside loops marked "
  50. "with #pragma simd");
  51. *valid = false;
  52. *walk_subtrees = 0;
  53. }
  54. return NULL_TREE;
  55. }
  56. /* Walks through all the subtrees of BODY using walk_tree to make sure
  57. invalid statements/expressions are not found inside BODY. Returns
  58. false if any invalid statements are found. */
  59. bool
  60. cpp_validate_cilk_plus_loop (tree body)
  61. {
  62. bool valid = true;
  63. cp_walk_tree (&body, cpp_validate_cilk_plus_loop_aux,
  64. (void *) &valid, NULL);
  65. return valid;
  66. }
  67. /* Sets the EXCEPTION bit (0x10) in the FRAME.flags field. */
  68. static tree
  69. set_cilk_except_flag (tree frame)
  70. {
  71. tree flags = cilk_dot (frame, CILK_TI_FRAME_FLAGS, 0);
  72. flags = build2 (MODIFY_EXPR, void_type_node, flags,
  73. build2 (BIT_IOR_EXPR, TREE_TYPE (flags), flags,
  74. build_int_cst (TREE_TYPE (flags),
  75. CILK_FRAME_EXCEPTING)));
  76. return flags;
  77. }
  78. /* Sets the frame.EXCEPT_DATA field to the head of the exception pointer. */
  79. static tree
  80. set_cilk_except_data (tree frame)
  81. {
  82. tree except_data = cilk_dot (frame, CILK_TI_FRAME_EXCEPTION, 0);
  83. tree uresume_fn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
  84. tree ret_expr;
  85. uresume_fn = build_call_expr (uresume_fn, 1,
  86. build_int_cst (integer_type_node, 0));
  87. ret_expr = build2 (MODIFY_EXPR, void_type_node, except_data, uresume_fn);
  88. return ret_expr;
  89. }
  90. /* Installs BODY into function FNDECL with appropriate exception handling
  91. code. WD holds information of wrapper function used to pass into the
  92. outlining function, cilk_outline. */
  93. void
  94. cilk_install_body_with_frame_cleanup (tree fndecl, tree orig_body, void *wd)
  95. {
  96. tree frame = make_cilk_frame (fndecl);
  97. tree dtor = create_cilk_function_exit (frame, false, false);
  98. add_local_decl (cfun, frame);
  99. cfun->language = ggc_cleared_alloc<language_function> ();
  100. location_t loc = EXPR_LOCATION (orig_body);
  101. tree list = alloc_stmt_list ();
  102. DECL_SAVED_TREE (fndecl) = list;
  103. tree fptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)), frame);
  104. tree body = cilk_install_body_pedigree_operations (fptr);
  105. gcc_assert (TREE_CODE (body) == STATEMENT_LIST);
  106. tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, fptr);
  107. append_to_statement_list (detach_expr, &body);
  108. cilk_outline (fndecl, &orig_body, (struct wrapper_data *) wd);
  109. append_to_statement_list (orig_body, &body);
  110. if (flag_exceptions)
  111. {
  112. tree except_flag = set_cilk_except_flag (frame);
  113. tree except_data = set_cilk_except_data (frame);
  114. tree catch_list = alloc_stmt_list ();
  115. append_to_statement_list (except_flag, &catch_list);
  116. append_to_statement_list (except_data, &catch_list);
  117. body = create_try_catch_expr (body, catch_list);
  118. }
  119. append_to_statement_list (build_stmt (loc, TRY_FINALLY_EXPR, body, dtor),
  120. &list);
  121. }