gimple-match.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* Gimple simplify definitions.
  2. Copyright (C) 2011-2019 Free Software Foundation, Inc.
  3. Contributed by Richard Guenther <rguenther@suse.de>
  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. #ifndef GCC_GIMPLE_MATCH_H
  17. #define GCC_GIMPLE_MATCH_H
  18. /* Helper to transparently allow tree codes and builtin function codes
  19. exist in one storage entity. */
  20. class code_helper
  21. {
  22. public:
  23. code_helper () {}
  24. code_helper (tree_code code) : rep ((int) code) {}
  25. code_helper (combined_fn fn) : rep (-(int) fn) {}
  26. operator tree_code () const { return (tree_code) rep; }
  27. operator combined_fn () const { return (combined_fn) -rep; }
  28. bool is_tree_code () const { return rep > 0; }
  29. bool is_fn_code () const { return rep < 0; }
  30. int get_rep () const { return rep; }
  31. private:
  32. int rep;
  33. };
  34. /* Represents the condition under which an operation should happen,
  35. and the value to use otherwise. The condition applies elementwise
  36. (as for VEC_COND_EXPR) if the values are vectors. */
  37. struct gimple_match_cond
  38. {
  39. enum uncond { UNCOND };
  40. /* Build an unconditional op. */
  41. gimple_match_cond (uncond) : cond (NULL_TREE), else_value (NULL_TREE) {}
  42. gimple_match_cond (tree, tree);
  43. gimple_match_cond any_else () const;
  44. /* The condition under which the operation occurs, or NULL_TREE
  45. if the operation is unconditional. */
  46. tree cond;
  47. /* The value to use when the condition is false. This is NULL_TREE if
  48. the operation is unconditional or if the value doesn't matter. */
  49. tree else_value;
  50. };
  51. inline
  52. gimple_match_cond::gimple_match_cond (tree cond_in, tree else_value_in)
  53. : cond (cond_in), else_value (else_value_in)
  54. {
  55. }
  56. /* Return a gimple_match_cond with the same condition but with an
  57. arbitrary ELSE_VALUE. */
  58. inline gimple_match_cond
  59. gimple_match_cond::any_else () const
  60. {
  61. return gimple_match_cond (cond, NULL_TREE);
  62. }
  63. /* Represents an operation to be simplified, or the result of the
  64. simplification. */
  65. struct gimple_match_op
  66. {
  67. gimple_match_op ();
  68. gimple_match_op (const gimple_match_cond &, code_helper, tree, unsigned int);
  69. gimple_match_op (const gimple_match_cond &,
  70. code_helper, tree, tree);
  71. gimple_match_op (const gimple_match_cond &,
  72. code_helper, tree, tree, tree);
  73. gimple_match_op (const gimple_match_cond &,
  74. code_helper, tree, tree, tree, tree);
  75. gimple_match_op (const gimple_match_cond &,
  76. code_helper, tree, tree, tree, tree, tree);
  77. gimple_match_op (const gimple_match_cond &,
  78. code_helper, tree, tree, tree, tree, tree, tree);
  79. void set_op (code_helper, tree, unsigned int);
  80. void set_op (code_helper, tree, tree);
  81. void set_op (code_helper, tree, tree, tree);
  82. void set_op (code_helper, tree, tree, tree, tree);
  83. void set_op (code_helper, tree, tree, tree, tree, bool);
  84. void set_op (code_helper, tree, tree, tree, tree, tree);
  85. void set_op (code_helper, tree, tree, tree, tree, tree, tree);
  86. void set_value (tree);
  87. tree op_or_null (unsigned int) const;
  88. /* The maximum value of NUM_OPS. */
  89. static const unsigned int MAX_NUM_OPS = 5;
  90. /* The conditions under which the operation is performed, and the value to
  91. use as a fallback. */
  92. gimple_match_cond cond;
  93. /* The operation being performed. */
  94. code_helper code;
  95. /* The type of the result. */
  96. tree type;
  97. /* For a BIT_FIELD_REF, whether the group of bits is stored in reverse order
  98. from the target order. */
  99. bool reverse;
  100. /* The number of operands to CODE. */
  101. unsigned int num_ops;
  102. /* The operands to CODE. Only the first NUM_OPS entries are meaningful. */
  103. tree ops[MAX_NUM_OPS];
  104. };
  105. inline
  106. gimple_match_op::gimple_match_op ()
  107. : cond (gimple_match_cond::UNCOND), type (NULL_TREE), reverse (false),
  108. num_ops (0)
  109. {
  110. }
  111. /* Constructor that takes the condition, code, type and number of
  112. operands, but leaves the caller to fill in the operands. */
  113. inline
  114. gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
  115. code_helper code_in, tree type_in,
  116. unsigned int num_ops_in)
  117. : cond (cond_in), code (code_in), type (type_in), reverse (false),
  118. num_ops (num_ops_in)
  119. {
  120. }
  121. /* Constructors for various numbers of operands. */
  122. inline
  123. gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
  124. code_helper code_in, tree type_in,
  125. tree op0)
  126. : cond (cond_in), code (code_in), type (type_in), reverse (false),
  127. num_ops (1)
  128. {
  129. ops[0] = op0;
  130. }
  131. inline
  132. gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
  133. code_helper code_in, tree type_in,
  134. tree op0, tree op1)
  135. : cond (cond_in), code (code_in), type (type_in), reverse (false),
  136. num_ops (2)
  137. {
  138. ops[0] = op0;
  139. ops[1] = op1;
  140. }
  141. inline
  142. gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
  143. code_helper code_in, tree type_in,
  144. tree op0, tree op1, tree op2)
  145. : cond (cond_in), code (code_in), type (type_in), reverse (false),
  146. num_ops (3)
  147. {
  148. ops[0] = op0;
  149. ops[1] = op1;
  150. ops[2] = op2;
  151. }
  152. inline
  153. gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
  154. code_helper code_in, tree type_in,
  155. tree op0, tree op1, tree op2, tree op3)
  156. : cond (cond_in), code (code_in), type (type_in), reverse (false),
  157. num_ops (4)
  158. {
  159. ops[0] = op0;
  160. ops[1] = op1;
  161. ops[2] = op2;
  162. ops[3] = op3;
  163. }
  164. inline
  165. gimple_match_op::gimple_match_op (const gimple_match_cond &cond_in,
  166. code_helper code_in, tree type_in,
  167. tree op0, tree op1, tree op2, tree op3,
  168. tree op4)
  169. : cond (cond_in), code (code_in), type (type_in), reverse (false),
  170. num_ops (5)
  171. {
  172. ops[0] = op0;
  173. ops[1] = op1;
  174. ops[2] = op2;
  175. ops[3] = op3;
  176. ops[4] = op4;
  177. }
  178. /* Change the operation performed to CODE_IN, the type of the result to
  179. TYPE_IN, and the number of operands to NUM_OPS_IN. The caller needs
  180. to set the operands itself. */
  181. inline void
  182. gimple_match_op::set_op (code_helper code_in, tree type_in,
  183. unsigned int num_ops_in)
  184. {
  185. code = code_in;
  186. type = type_in;
  187. num_ops = num_ops_in;
  188. }
  189. /* Functions for changing the operation performed, for various numbers
  190. of operands. */
  191. inline void
  192. gimple_match_op::set_op (code_helper code_in, tree type_in, tree op0)
  193. {
  194. code = code_in;
  195. type = type_in;
  196. num_ops = 1;
  197. ops[0] = op0;
  198. }
  199. inline void
  200. gimple_match_op::set_op (code_helper code_in, tree type_in, tree op0, tree op1)
  201. {
  202. code = code_in;
  203. type = type_in;
  204. num_ops = 2;
  205. ops[0] = op0;
  206. ops[1] = op1;
  207. }
  208. inline void
  209. gimple_match_op::set_op (code_helper code_in, tree type_in,
  210. tree op0, tree op1, tree op2)
  211. {
  212. code = code_in;
  213. type = type_in;
  214. num_ops = 3;
  215. ops[0] = op0;
  216. ops[1] = op1;
  217. ops[2] = op2;
  218. }
  219. inline void
  220. gimple_match_op::set_op (code_helper code_in, tree type_in,
  221. tree op0, tree op1, tree op2, bool reverse_in)
  222. {
  223. code = code_in;
  224. type = type_in;
  225. reverse = reverse_in;
  226. num_ops = 3;
  227. ops[0] = op0;
  228. ops[1] = op1;
  229. ops[2] = op2;
  230. }
  231. inline void
  232. gimple_match_op::set_op (code_helper code_in, tree type_in,
  233. tree op0, tree op1, tree op2, tree op3)
  234. {
  235. code = code_in;
  236. type = type_in;
  237. num_ops = 4;
  238. ops[0] = op0;
  239. ops[1] = op1;
  240. ops[2] = op2;
  241. ops[3] = op3;
  242. }
  243. inline void
  244. gimple_match_op::set_op (code_helper code_in, tree type_in,
  245. tree op0, tree op1, tree op2, tree op3, tree op4)
  246. {
  247. code = code_in;
  248. type = type_in;
  249. num_ops = 5;
  250. ops[0] = op0;
  251. ops[1] = op1;
  252. ops[2] = op2;
  253. ops[3] = op3;
  254. ops[4] = op4;
  255. }
  256. /* Set the "operation" to be the single value VALUE, such as a constant
  257. or SSA_NAME. */
  258. inline void
  259. gimple_match_op::set_value (tree value)
  260. {
  261. set_op (TREE_CODE (value), TREE_TYPE (value), value);
  262. }
  263. /* Return the value of operand I, or null if there aren't that many
  264. operands. */
  265. inline tree
  266. gimple_match_op::op_or_null (unsigned int i) const
  267. {
  268. return i < num_ops ? ops[i] : NULL_TREE;
  269. }
  270. /* Return whether OP is a non-expression result and a gimple value. */
  271. inline bool
  272. gimple_simplified_result_is_gimple_val (const gimple_match_op *op)
  273. {
  274. return (op->code.is_tree_code ()
  275. && (TREE_CODE_LENGTH ((tree_code) op->code) == 0
  276. || ((tree_code) op->code) == ADDR_EXPR)
  277. && is_gimple_val (op->ops[0]));
  278. }
  279. extern tree (*mprts_hook) (gimple_match_op *);
  280. bool gimple_simplify (gimple *, gimple_match_op *, gimple_seq *,
  281. tree (*)(tree), tree (*)(tree));
  282. bool gimple_resimplify1 (gimple_seq *, gimple_match_op *, tree (*)(tree));
  283. bool gimple_resimplify2 (gimple_seq *, gimple_match_op *, tree (*)(tree));
  284. bool gimple_resimplify3 (gimple_seq *, gimple_match_op *, tree (*)(tree));
  285. bool gimple_resimplify4 (gimple_seq *, gimple_match_op *, tree (*)(tree));
  286. bool gimple_resimplify5 (gimple_seq *, gimple_match_op *, tree (*)(tree));
  287. tree maybe_push_res_to_seq (gimple_match_op *, gimple_seq *,
  288. tree res = NULL_TREE);
  289. void maybe_build_generic_op (gimple_match_op *);
  290. #endif /* GCC_GIMPLE_MATCH_H */