tree-switch-conversion.h 26 KB

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