tree-switch-conversion.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /* Tree switch conversion for GNU compiler.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef TREE_SWITCH_CONVERSION_H
  16. #define TREE_SWITCH_CONVERSION_H
  17. namespace tree_switch_conversion {
  18. /* Type of cluster. */
  19. enum cluster_type
  20. {
  21. SIMPLE_CASE,
  22. JUMP_TABLE,
  23. BIT_TEST
  24. };
  25. #define PRINT_CASE(f,c) print_generic_expr (f, c)
  26. /* Abstract base class for representing a cluster of cases.
  27. Here is the inheritance hierarachy, and the enum_cluster_type
  28. values for the concrete subclasses:
  29. cluster
  30. |-simple_cluster (SIMPLE_CASE)
  31. `-group_cluster
  32. |-jump_table_cluster (JUMP_TABLE)
  33. `-bit_test_cluster (BIT_TEST). */
  34. struct cluster
  35. {
  36. /* Constructor. */
  37. cluster (tree case_label_expr, basic_block case_bb, profile_probability prob,
  38. profile_probability subtree_prob);
  39. /* Destructor. */
  40. virtual ~cluster ()
  41. {}
  42. /* Return type. */
  43. virtual cluster_type get_type () = 0;
  44. /* Get low value covered by a cluster. */
  45. virtual tree get_low () = 0;
  46. /* Get high value covered by a cluster. */
  47. virtual tree get_high () = 0;
  48. /* Debug content of a cluster. */
  49. virtual void debug () = 0;
  50. /* Dump content of a cluster. */
  51. virtual void dump (FILE *f, bool details = false) = 0;
  52. /* Emit GIMPLE code to handle the cluster. */
  53. virtual void emit (tree, tree, tree, basic_block) = 0;
  54. /* Return true if a cluster handles only a single case value and the
  55. value is not a range. */
  56. virtual bool is_single_value_p ()
  57. {
  58. return false;
  59. }
  60. /* Return range of a cluster. If value would overflow in type of LOW,
  61. then return 0. */
  62. static unsigned HOST_WIDE_INT get_range (tree low, tree high)
  63. {
  64. tree r = fold_build2 (MINUS_EXPR, TREE_TYPE (low), high, low);
  65. if (!tree_fits_uhwi_p (r))
  66. return 0;
  67. return tree_to_uhwi (r) + 1;
  68. }
  69. /* Case label. */
  70. tree m_case_label_expr;
  71. /* Basic block of the case. */
  72. basic_block m_case_bb;
  73. /* Probability of taking this cluster. */
  74. profile_probability m_prob;
  75. /* Probability of reaching subtree rooted at this node. */
  76. profile_probability m_subtree_prob;
  77. protected:
  78. /* Default constructor. */
  79. cluster () {}
  80. };
  81. cluster::cluster (tree case_label_expr, basic_block case_bb,
  82. profile_probability prob, profile_probability subtree_prob):
  83. m_case_label_expr (case_label_expr), m_case_bb (case_bb), m_prob (prob),
  84. m_subtree_prob (subtree_prob)
  85. {
  86. }
  87. /* Subclass of cluster representing a simple contiguous range
  88. from [low..high]. */
  89. struct simple_cluster: public cluster
  90. {
  91. /* Constructor. */
  92. simple_cluster (tree low, tree high, tree case_label_expr,
  93. basic_block case_bb, profile_probability prob);
  94. /* Destructor. */
  95. ~simple_cluster ()
  96. {}
  97. cluster_type
  98. get_type ()
  99. {
  100. return SIMPLE_CASE;
  101. }
  102. tree
  103. get_low ()
  104. {
  105. return m_low;
  106. }
  107. tree
  108. get_high ()
  109. {
  110. return m_high;
  111. }
  112. void
  113. debug ()
  114. {
  115. dump (stderr);
  116. }
  117. void
  118. dump (FILE *f, bool details ATTRIBUTE_UNUSED = false)
  119. {
  120. PRINT_CASE (f, get_low ());
  121. if (get_low () != get_high ())
  122. {
  123. fprintf (f, "-");
  124. PRINT_CASE (f, get_high ());
  125. }
  126. fprintf (f, " ");
  127. }
  128. void emit (tree, tree, tree, basic_block)
  129. {
  130. gcc_unreachable ();
  131. }
  132. bool is_single_value_p ()
  133. {
  134. return tree_int_cst_equal (get_low (), get_high ());
  135. }
  136. /* Low value of the case. */
  137. tree m_low;
  138. /* High value of the case. */
  139. tree m_high;
  140. /* True if case is a range. */
  141. bool m_range_p;
  142. };
  143. simple_cluster::simple_cluster (tree low, tree high, tree case_label_expr,
  144. basic_block case_bb, profile_probability prob):
  145. cluster (case_label_expr, case_bb, prob, prob),
  146. m_low (low), m_high (high)
  147. {
  148. m_range_p = m_high != NULL;
  149. if (m_high == NULL)
  150. m_high = m_low;
  151. }
  152. /* Abstract subclass of jump table and bit test cluster,
  153. handling a collection of simple_cluster instances. */
  154. struct group_cluster: public cluster
  155. {
  156. /* Constructor. */
  157. group_cluster (vec<cluster *> &clusters, unsigned start, unsigned end);
  158. /* Destructor. */
  159. ~group_cluster ();
  160. tree
  161. get_low ()
  162. {
  163. return m_cases[0]->get_low ();
  164. }
  165. tree
  166. get_high ()
  167. {
  168. return m_cases[m_cases.length () - 1]->get_high ();
  169. }
  170. void
  171. debug ()
  172. {
  173. dump (stderr);
  174. }
  175. void dump (FILE *f, bool details = false);
  176. /* List of simple clusters handled by the group. */
  177. vec<simple_cluster *> m_cases;
  178. };
  179. /* Concrete subclass of group_cluster representing a collection
  180. of cases to be implemented as a jump table.
  181. The "emit" vfunc gernerates a nested switch statement which
  182. is later lowered to a jump table. */
  183. struct jump_table_cluster: public group_cluster
  184. {
  185. /* Constructor. */
  186. jump_table_cluster (vec<cluster *> &clusters, unsigned start, unsigned end)
  187. : group_cluster (clusters, start, end)
  188. {}
  189. cluster_type
  190. get_type ()
  191. {
  192. return JUMP_TABLE;
  193. }
  194. void emit (tree index_expr, tree index_type,
  195. tree default_label_expr, basic_block default_bb);
  196. /* Find jump tables of given CLUSTERS, where all members of the vector
  197. are of type simple_cluster. New clusters are returned. */
  198. static vec<cluster *> find_jump_tables (vec<cluster *> &clusters);
  199. /* Return true when cluster starting at START and ending at END (inclusive)
  200. can build a jump-table. */
  201. static bool can_be_handled (const vec<cluster *> &clusters, unsigned start,
  202. unsigned end);
  203. /* Return true if cluster starting at START and ending at END (inclusive)
  204. is profitable transformation. */
  205. static bool is_beneficial (const vec<cluster *> &clusters, unsigned start,
  206. unsigned end);
  207. /* Return the smallest number of different values for which it is best
  208. to use a jump-table instead of a tree of conditional branches. */
  209. static inline unsigned int case_values_threshold (void);
  210. /* Return whether jump table expansion is allowed. */
  211. static bool is_enabled (void);
  212. /* Max growth ratio for code that is optimized for size. */
  213. static const unsigned HOST_WIDE_INT max_ratio_for_size = 3;
  214. /* Max growth ratio for code that is optimized for speed. */
  215. static const unsigned HOST_WIDE_INT max_ratio_for_speed = 8;
  216. };
  217. /* A GIMPLE switch statement can be expanded to a short sequence of bit-wise
  218. comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
  219. where CST and MINVAL are integer constants. This is better than a series
  220. of compare-and-banch insns in some cases, e.g. we can implement:
  221. if ((x==4) || (x==6) || (x==9) || (x==11))
  222. as a single bit test:
  223. if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
  224. This transformation is only applied if the number of case targets is small,
  225. if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
  226. performed in "word_mode".
  227. The following example shows the code the transformation generates:
  228. int bar(int x)
  229. {
  230. switch (x)
  231. {
  232. case '0': case '1': case '2': case '3': case '4':
  233. case '5': case '6': case '7': case '8': case '9':
  234. case 'A': case 'B': case 'C': case 'D': case 'E':
  235. case 'F':
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. ==>
  241. bar (int x)
  242. {
  243. tmp1 = x - 48;
  244. if (tmp1 > (70 - 48)) goto L2;
  245. tmp2 = 1 << tmp1;
  246. tmp3 = 0b11111100000001111111111;
  247. if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
  248. L1:
  249. return 1;
  250. L2:
  251. return 0;
  252. }
  253. TODO: There are still some improvements to this transformation that could
  254. be implemented:
  255. * A narrower mode than word_mode could be used if that is cheaper, e.g.
  256. for x86_64 where a narrower-mode shift may result in smaller code.
  257. * The compounded constant could be shifted rather than the one. The
  258. test would be either on the sign bit or on the least significant bit,
  259. depending on the direction of the shift. On some machines, the test
  260. for the branch would be free if the bit to test is already set by the
  261. shift operation.
  262. This transformation was contributed by Roger Sayle, see this e-mail:
  263. http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
  264. */
  265. struct bit_test_cluster: public group_cluster
  266. {
  267. /* Constructor. */
  268. bit_test_cluster (vec<cluster *> &clusters, unsigned start, unsigned end,
  269. bool handles_entire_switch)
  270. :group_cluster (clusters, start, end),
  271. m_handles_entire_switch (handles_entire_switch)
  272. {}
  273. cluster_type
  274. get_type ()
  275. {
  276. return BIT_TEST;
  277. }
  278. /* Expand a switch statement by a short sequence of bit-wise
  279. comparisons. "switch(x)" is effectively converted into
  280. "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
  281. integer constants.
  282. INDEX_EXPR is the value being switched on.
  283. MINVAL is the lowest case value of in the case nodes,
  284. and RANGE is highest value minus MINVAL. MINVAL and RANGE
  285. are not guaranteed to be of the same type as INDEX_EXPR
  286. (the gimplifier doesn't change the type of case label values,
  287. and MINVAL and RANGE are derived from those values).
  288. MAXVAL is MINVAL + RANGE.
  289. There *MUST* be max_case_bit_tests or less unique case
  290. node targets. */
  291. void emit (tree index_expr, tree index_type,
  292. tree default_label_expr, basic_block default_bb);
  293. /* Find bit tests of given CLUSTERS, where all members of the vector
  294. are of type simple_cluster. New clusters are returned. */
  295. static vec<cluster *> find_bit_tests (vec<cluster *> &clusters);
  296. /* Return true when RANGE of case values with UNIQ labels
  297. can build a bit test. */
  298. static bool can_be_handled (unsigned HOST_WIDE_INT range, unsigned uniq);
  299. /* Return true when cluster starting at START and ending at END (inclusive)
  300. can build a bit test. */
  301. static bool can_be_handled (const vec<cluster *> &clusters, unsigned start,
  302. unsigned end);
  303. /* Return true when COUNT of cases of UNIQ labels is beneficial for bit test
  304. transformation. */
  305. static bool is_beneficial (unsigned count, unsigned uniq);
  306. /* Return true if cluster starting at START and ending at END (inclusive)
  307. is profitable transformation. */
  308. static bool is_beneficial (const vec<cluster *> &clusters, unsigned start,
  309. unsigned end);
  310. /* Split the basic block at the statement pointed to by GSIP, and insert
  311. a branch to the target basic block of E_TRUE conditional on tree
  312. expression COND.
  313. It is assumed that there is already an edge from the to-be-split
  314. basic block to E_TRUE->dest block. This edge is removed, and the
  315. profile information on the edge is re-used for the new conditional
  316. jump.
  317. The CFG is updated. The dominator tree will not be valid after
  318. this transformation, but the immediate dominators are updated if
  319. UPDATE_DOMINATORS is true.
  320. Returns the newly created basic block. */
  321. static basic_block hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
  322. tree cond,
  323. basic_block case_bb,
  324. profile_probability prob);
  325. /* True when the jump table handles an entire switch statement. */
  326. bool m_handles_entire_switch;
  327. /* Maximum number of different basic blocks that can be handled by
  328. a bit test. */
  329. static const int m_max_case_bit_tests = 3;
  330. };
  331. /* Helper struct to find minimal clusters. */
  332. struct min_cluster_item
  333. {
  334. /* Constructor. */
  335. min_cluster_item (unsigned count, unsigned start, unsigned non_jt_cases):
  336. m_count (count), m_start (start), m_non_jt_cases (non_jt_cases)
  337. {}
  338. /* Count of clusters. */
  339. unsigned m_count;
  340. /* Index where is cluster boundary. */
  341. unsigned m_start;
  342. /* Total number of cases that will not be in a jump table. */
  343. unsigned m_non_jt_cases;
  344. };
  345. /* Helper struct to represent switch decision tree. */
  346. struct case_tree_node
  347. {
  348. /* Empty Constructor. */
  349. case_tree_node ();
  350. /* Return true when it has a child. */
  351. bool has_child ()
  352. {
  353. return m_left != NULL || m_right != NULL;
  354. }
  355. /* Left son in binary tree. */
  356. case_tree_node *m_left;
  357. /* Right son in binary tree; also node chain. */
  358. case_tree_node *m_right;
  359. /* Parent of node in binary tree. */
  360. case_tree_node *m_parent;
  361. /* Cluster represented by this tree node. */
  362. cluster *m_c;
  363. };
  364. inline
  365. case_tree_node::case_tree_node ():
  366. m_left (NULL), m_right (NULL), m_parent (NULL), m_c (NULL)
  367. {
  368. }
  369. unsigned int
  370. jump_table_cluster::case_values_threshold (void)
  371. {
  372. unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
  373. if (threshold == 0)
  374. threshold = targetm.case_values_threshold ();
  375. return threshold;
  376. }
  377. /* Return whether jump table expansion is allowed. */
  378. bool jump_table_cluster::is_enabled (void)
  379. {
  380. /* If neither casesi or tablejump is available, or flag_jump_tables
  381. over-ruled us, we really have no choice. */
  382. if (!targetm.have_casesi () && !targetm.have_tablejump ())
  383. return false;
  384. if (!flag_jump_tables)
  385. return false;
  386. #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
  387. if (flag_pic)
  388. return false;
  389. #endif
  390. return true;
  391. }
  392. /* A case_bit_test represents a set of case nodes that may be
  393. selected from using a bit-wise comparison. HI and LO hold
  394. the integer to be tested against, TARGET_EDGE contains the
  395. edge to the basic block to jump to upon success and BITS
  396. counts the number of case nodes handled by this test,
  397. typically the number of bits set in HI:LO. The LABEL field
  398. is used to quickly identify all cases in this set without
  399. looking at label_to_block for every case label. */
  400. struct case_bit_test
  401. {
  402. wide_int mask;
  403. basic_block target_bb;
  404. tree label;
  405. int bits;
  406. /* Comparison function for qsort to order bit tests by decreasing
  407. probability of execution. */
  408. static int cmp (const void *p1, const void *p2);
  409. };
  410. struct switch_decision_tree
  411. {
  412. /* Constructor. */
  413. switch_decision_tree (gswitch *swtch): m_switch (swtch), m_phi_mapping (),
  414. m_case_bbs (), m_case_node_pool ("struct case_node pool"),
  415. m_case_list (NULL)
  416. {
  417. }
  418. /* Analyze switch statement and return true when the statement is expanded
  419. as decision tree. */
  420. bool analyze_switch_statement ();
  421. /* Attempt to expand CLUSTERS as a decision tree. Return true when
  422. expanded. */
  423. bool try_switch_expansion (vec<cluster *> &clusters);
  424. /* Compute the number of case labels that correspond to each outgoing edge of
  425. switch statement. Record this information in the aux field of the edge.
  426. */
  427. void compute_cases_per_edge ();
  428. /* Before switch transformation, record all SSA_NAMEs defined in switch BB
  429. and used in a label basic block. */
  430. void record_phi_operand_mapping ();
  431. /* Append new operands to PHI statements that were introduced due to
  432. addition of new edges to case labels. */
  433. void fix_phi_operands_for_edges ();
  434. /* Generate a decision tree, switching on INDEX_EXPR and jumping to
  435. one of the labels in CASE_LIST or to the DEFAULT_LABEL.
  436. We generate a binary decision tree to select the appropriate target
  437. code. */
  438. void emit (basic_block bb, tree index_expr,
  439. profile_probability default_prob, tree index_type);
  440. /* Emit step-by-step code to select a case for the value of INDEX.
  441. The thus generated decision tree follows the form of the
  442. case-node binary tree NODE, whose nodes represent test conditions.
  443. DEFAULT_PROB is probability of cases leading to default BB.
  444. INDEX_TYPE is the type of the index of the switch. */
  445. basic_block emit_case_nodes (basic_block bb, tree index,
  446. case_tree_node *node,
  447. profile_probability default_prob,
  448. tree index_type, location_t);
  449. /* Take an ordered list of case nodes
  450. and transform them into a near optimal binary tree,
  451. on the assumption that any target code selection value is as
  452. likely as any other.
  453. The transformation is performed by splitting the ordered
  454. list into two equal sections plus a pivot. The parts are
  455. then attached to the pivot as left and right branches. Each
  456. branch is then transformed recursively. */
  457. static void balance_case_nodes (case_tree_node **head,
  458. case_tree_node *parent);
  459. /* Dump ROOT, a list or tree of case nodes, to file F. */
  460. static void dump_case_nodes (FILE *f, case_tree_node *root, int indent_step,
  461. int indent_level);
  462. /* Add an unconditional jump to CASE_BB that happens in basic block BB. */
  463. static void emit_jump (basic_block bb, basic_block case_bb);
  464. /* Generate code to compare OP0 with OP1 so that the condition codes are
  465. set and to jump to LABEL_BB if the condition is true.
  466. COMPARISON is the GIMPLE comparison (EQ, NE, GT, etc.).
  467. PROB is the probability of jumping to LABEL_BB. */
  468. static basic_block emit_cmp_and_jump_insns (basic_block bb, tree op0,
  469. tree op1, tree_code comparison,
  470. basic_block label_bb,
  471. profile_probability prob,
  472. location_t);
  473. /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE.
  474. PROB is the probability of jumping to LABEL_BB. */
  475. static basic_block do_jump_if_equal (basic_block bb, tree op0, tree op1,
  476. basic_block label_bb,
  477. profile_probability prob,
  478. location_t);
  479. /* Reset the aux field of all outgoing edges of switch basic block. */
  480. static inline void reset_out_edges_aux (gswitch *swtch);
  481. /* Switch statement. */
  482. gswitch *m_switch;
  483. /* Map of PHI nodes that have to be fixed after expansion. */
  484. hash_map<tree, tree> m_phi_mapping;
  485. /* List of basic blocks that belong to labels of the switch. */
  486. auto_vec<basic_block> m_case_bbs;
  487. /* Basic block with default label. */
  488. basic_block m_default_bb;
  489. /* A pool for case nodes. */
  490. object_allocator<case_tree_node> m_case_node_pool;
  491. /* Balanced tree of case nodes. */
  492. case_tree_node *m_case_list;
  493. };
  494. /*
  495. Switch initialization conversion
  496. The following pass changes simple initializations of scalars in a switch
  497. statement into initializations from a static array. Obviously, the values
  498. must be constant and known at compile time and a default branch must be
  499. provided. For example, the following code:
  500. int a,b;
  501. switch (argc)
  502. {
  503. case 1:
  504. case 2:
  505. a_1 = 8;
  506. b_1 = 6;
  507. break;
  508. case 3:
  509. a_2 = 9;
  510. b_2 = 5;
  511. break;
  512. case 12:
  513. a_3 = 10;
  514. b_3 = 4;
  515. break;
  516. default:
  517. a_4 = 16;
  518. b_4 = 1;
  519. break;
  520. }
  521. a_5 = PHI <a_1, a_2, a_3, a_4>
  522. b_5 = PHI <b_1, b_2, b_3, b_4>
  523. is changed into:
  524. static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
  525. static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
  526. 16, 16, 10};
  527. if (((unsigned) argc) - 1 < 11)
  528. {
  529. a_6 = CSWTCH02[argc - 1];
  530. b_6 = CSWTCH01[argc - 1];
  531. }
  532. else
  533. {
  534. a_7 = 16;
  535. b_7 = 1;
  536. }
  537. a_5 = PHI <a_6, a_7>
  538. b_b = PHI <b_6, b_7>
  539. There are further constraints. Specifically, the range of values across all
  540. case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
  541. eight) times the number of the actual switch branches.
  542. This transformation was contributed by Martin Jambor, see this e-mail:
  543. http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
  544. /* The main structure of the pass. */
  545. struct switch_conversion
  546. {
  547. /* Constructor. */
  548. switch_conversion ();
  549. /* Destructor. */
  550. ~switch_conversion ();
  551. /* The following function is invoked on every switch statement (the current
  552. one is given in SWTCH) and runs the individual phases of switch
  553. conversion on it one after another until one fails or the conversion
  554. is completed. On success, NULL is in m_reason, otherwise points
  555. to a string with the reason why the conversion failed. */
  556. void expand (gswitch *swtch);
  557. /* Collection information about SWTCH statement. */
  558. void collect (gswitch *swtch);
  559. /* Checks whether the range given by individual case statements of the switch
  560. switch statement isn't too big and whether the number of branches actually
  561. satisfies the size of the new array. */
  562. bool check_range ();
  563. /* Checks whether all but the final BB basic blocks are empty. */
  564. bool check_all_empty_except_final ();
  565. /* This function checks whether all required values in phi nodes in final_bb
  566. are constants. Required values are those that correspond to a basic block
  567. which is a part of the examined switch statement. It returns true if the
  568. phi nodes are OK, otherwise false. */
  569. bool check_final_bb ();
  570. /* The following function allocates default_values, target_{in,out}_names and
  571. constructors arrays. The last one is also populated with pointers to
  572. vectors that will become constructors of new arrays. */
  573. void create_temp_arrays ();
  574. /* Populate the array of default values in the order of phi nodes.
  575. DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch
  576. if the range is non-contiguous or the default case has standard
  577. structure, otherwise it is the first non-default case instead. */
  578. void gather_default_values (tree default_case);
  579. /* The following function populates the vectors in the constructors array with
  580. future contents of the static arrays. The vectors are populated in the
  581. order of phi nodes. */
  582. void build_constructors ();
  583. /* If all values in the constructor vector are products of a linear function
  584. a * x + b, then return true. When true, COEFF_A and COEFF_B and
  585. coefficients of the linear function. Note that equal values are special
  586. case of a linear function with a and b equal to zero. */
  587. bool contains_linear_function_p (vec<constructor_elt, va_gc> *vec,
  588. wide_int *coeff_a, wide_int *coeff_b);
  589. /* Return type which should be used for array elements, either TYPE's
  590. main variant or, for integral types, some smaller integral type
  591. that can still hold all the constants. */
  592. tree array_value_type (tree type, int num);
  593. /* Create an appropriate array type and declaration and assemble a static
  594. array variable. Also create a load statement that initializes
  595. the variable in question with a value from the static array. SWTCH is
  596. the switch statement being converted, NUM is the index to
  597. arrays of constructors, default values and target SSA names
  598. for this particular array. ARR_INDEX_TYPE is the type of the index
  599. of the new array, PHI is the phi node of the final BB that corresponds
  600. to the value that will be loaded from the created array. TIDX
  601. is an ssa name of a temporary variable holding the index for loads from the
  602. new array. */
  603. void build_one_array (int num, tree arr_index_type,
  604. gphi *phi, tree tidx);
  605. /* Builds and initializes static arrays initialized with values gathered from
  606. the switch statement. Also creates statements that load values from
  607. them. */
  608. void build_arrays ();
  609. /* Generates and appropriately inserts loads of default values at the position
  610. given by GSI. Returns the last inserted statement. */
  611. gassign *gen_def_assigns (gimple_stmt_iterator *gsi);
  612. /* Deletes the unused bbs and edges that now contain the switch statement and
  613. its empty branch bbs. BBD is the now dead BB containing
  614. the original switch statement, FINAL is the last BB of the converted
  615. switch statement (in terms of succession). */
  616. void prune_bbs (basic_block bbd, basic_block final, basic_block default_bb);
  617. /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
  618. from the basic block loading values from an array and E2F from the basic
  619. block loading default values. BBF is the last switch basic block (see the
  620. bbf description in the comment below). */
  621. void fix_phi_nodes (edge e1f, edge e2f, basic_block bbf);
  622. /* Creates a check whether the switch expression value actually falls into the
  623. range given by all the cases. If it does not, the temporaries are loaded
  624. with default values instead. */
  625. void gen_inbound_check ();
  626. /* Switch statement for which switch conversion takes place. */
  627. gswitch *m_switch;
  628. /* The expression used to decide the switch branch. */
  629. tree m_index_expr;
  630. /* The following integer constants store the minimum and maximum value
  631. covered by the case labels. */
  632. tree m_range_min;
  633. tree m_range_max;
  634. /* The difference between the above two numbers. Stored here because it
  635. is used in all the conversion heuristics, as well as for some of the
  636. transformation, and it is expensive to re-compute it all the time. */
  637. tree m_range_size;
  638. /* Basic block that contains the actual GIMPLE_SWITCH. */
  639. basic_block m_switch_bb;
  640. /* Basic block that is the target of the default case. */
  641. basic_block m_default_bb;
  642. /* The single successor block of all branches out of the GIMPLE_SWITCH,
  643. if such a block exists. Otherwise NULL. */
  644. basic_block m_final_bb;
  645. /* The probability of the default edge in the replaced switch. */
  646. profile_probability m_default_prob;
  647. /* The count of the default edge in the replaced switch. */
  648. profile_count m_default_count;
  649. /* Combined count of all other (non-default) edges in the replaced switch. */
  650. profile_count m_other_count;
  651. /* Number of phi nodes in the final bb (that we'll be replacing). */
  652. int m_phi_count;
  653. /* Constructors of new static arrays. */
  654. vec<constructor_elt, va_gc> **m_constructors;
  655. /* Array of default values, in the same order as phi nodes. */
  656. tree *m_default_values;
  657. /* Array of ssa names that are initialized with a value from a new static
  658. array. */
  659. tree *m_target_inbound_names;
  660. /* Array of ssa names that are initialized with the default value if the
  661. switch expression is out of range. */
  662. tree *m_target_outbound_names;
  663. /* VOP SSA_NAME. */
  664. tree m_target_vop;
  665. /* The first load statement that loads a temporary from a new static array.
  666. */
  667. gimple *m_arr_ref_first;
  668. /* The last load statement that loads a temporary from a new static array. */
  669. gimple *m_arr_ref_last;
  670. /* String reason why the case wasn't a good candidate that is written to the
  671. dump file, if there is one. */
  672. const char *m_reason;
  673. /* True if default case is not used for any value between range_min and
  674. range_max inclusive. */
  675. bool m_contiguous_range;
  676. /* True if default case does not have the required shape for other case
  677. labels. */
  678. bool m_default_case_nonstandard;
  679. /* Number of uniq labels for non-default edges. */
  680. unsigned int m_uniq;
  681. /* Count is number of non-default edges. */
  682. unsigned int m_count;
  683. /* True if CFG has been changed. */
  684. bool m_cfg_altered;
  685. };
  686. void
  687. switch_decision_tree::reset_out_edges_aux (gswitch *swtch)
  688. {
  689. basic_block bb = gimple_bb (swtch);
  690. edge e;
  691. edge_iterator ei;
  692. FOR_EACH_EDGE (e, ei, bb->succs)
  693. e->aux = (void *) 0;
  694. }
  695. } // tree_switch_conversion namespace
  696. #endif // TREE_SWITCH_CONVERSION_H