tree-switch-conversion.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /* Lower GIMPLE_SWITCH expressions to something more efficient than
  2. a jump table.
  3. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 3, or (at your option) any
  8. later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY 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, write to the Free
  15. Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. /* This file handles the lowering of GIMPLE_SWITCH to an indexed
  18. load, or a series of bit-test-and-branch expressions. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "tm.h"
  23. #include "line-map.h"
  24. #include "params.h"
  25. #include "flags.h"
  26. #include "hash-set.h"
  27. #include "machmode.h"
  28. #include "vec.h"
  29. #include "double-int.h"
  30. #include "input.h"
  31. #include "alias.h"
  32. #include "symtab.h"
  33. #include "wide-int.h"
  34. #include "inchash.h"
  35. #include "tree.h"
  36. #include "fold-const.h"
  37. #include "varasm.h"
  38. #include "stor-layout.h"
  39. #include "predict.h"
  40. #include "hard-reg-set.h"
  41. #include "function.h"
  42. #include "dominance.h"
  43. #include "cfg.h"
  44. #include "cfganal.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 "gimple-iterator.h"
  53. #include "gimplify-me.h"
  54. #include "gimple-ssa.h"
  55. #include "hash-map.h"
  56. #include "plugin-api.h"
  57. #include "ipa-ref.h"
  58. #include "cgraph.h"
  59. #include "tree-cfg.h"
  60. #include "tree-phinodes.h"
  61. #include "stringpool.h"
  62. #include "tree-ssanames.h"
  63. #include "tree-pass.h"
  64. #include "gimple-pretty-print.h"
  65. #include "cfgloop.h"
  66. /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode
  67. type in the GIMPLE type system that is language-independent? */
  68. #include "langhooks.h"
  69. /* Need to include expr.h and optabs.h for lshift_cheap_p. */
  70. #include "hashtab.h"
  71. #include "rtl.h"
  72. #include "statistics.h"
  73. #include "real.h"
  74. #include "fixed-value.h"
  75. #include "insn-config.h"
  76. #include "expmed.h"
  77. #include "dojump.h"
  78. #include "explow.h"
  79. #include "calls.h"
  80. #include "emit-rtl.h"
  81. #include "stmt.h"
  82. #include "expr.h"
  83. #include "insn-codes.h"
  84. #include "optabs.h"
  85. /* Maximum number of case bit tests.
  86. FIXME: This should be derived from PARAM_CASE_VALUES_THRESHOLD and
  87. targetm.case_values_threshold(), or be its own param. */
  88. #define MAX_CASE_BIT_TESTS 3
  89. /* Split the basic block at the statement pointed to by GSIP, and insert
  90. a branch to the target basic block of E_TRUE conditional on tree
  91. expression COND.
  92. It is assumed that there is already an edge from the to-be-split
  93. basic block to E_TRUE->dest block. This edge is removed, and the
  94. profile information on the edge is re-used for the new conditional
  95. jump.
  96. The CFG is updated. The dominator tree will not be valid after
  97. this transformation, but the immediate dominators are updated if
  98. UPDATE_DOMINATORS is true.
  99. Returns the newly created basic block. */
  100. static basic_block
  101. hoist_edge_and_branch_if_true (gimple_stmt_iterator *gsip,
  102. tree cond, edge e_true,
  103. bool update_dominators)
  104. {
  105. tree tmp;
  106. gcond *cond_stmt;
  107. edge e_false;
  108. basic_block new_bb, split_bb = gsi_bb (*gsip);
  109. bool dominated_e_true = false;
  110. gcc_assert (e_true->src == split_bb);
  111. if (update_dominators
  112. && get_immediate_dominator (CDI_DOMINATORS, e_true->dest) == split_bb)
  113. dominated_e_true = true;
  114. tmp = force_gimple_operand_gsi (gsip, cond, /*simple=*/true, NULL,
  115. /*before=*/true, GSI_SAME_STMT);
  116. cond_stmt = gimple_build_cond_from_tree (tmp, NULL_TREE, NULL_TREE);
  117. gsi_insert_before (gsip, cond_stmt, GSI_SAME_STMT);
  118. e_false = split_block (split_bb, cond_stmt);
  119. new_bb = e_false->dest;
  120. redirect_edge_pred (e_true, split_bb);
  121. e_true->flags &= ~EDGE_FALLTHRU;
  122. e_true->flags |= EDGE_TRUE_VALUE;
  123. e_false->flags &= ~EDGE_FALLTHRU;
  124. e_false->flags |= EDGE_FALSE_VALUE;
  125. e_false->probability = REG_BR_PROB_BASE - e_true->probability;
  126. e_false->count = split_bb->count - e_true->count;
  127. new_bb->count = e_false->count;
  128. if (update_dominators)
  129. {
  130. if (dominated_e_true)
  131. set_immediate_dominator (CDI_DOMINATORS, e_true->dest, split_bb);
  132. set_immediate_dominator (CDI_DOMINATORS, e_false->dest, split_bb);
  133. }
  134. return new_bb;
  135. }
  136. /* Return true if a switch should be expanded as a bit test.
  137. RANGE is the difference between highest and lowest case.
  138. UNIQ is number of unique case node targets, not counting the default case.
  139. COUNT is the number of comparisons needed, not counting the default case. */
  140. static bool
  141. expand_switch_using_bit_tests_p (tree range,
  142. unsigned int uniq,
  143. unsigned int count, bool speed_p)
  144. {
  145. return (((uniq == 1 && count >= 3)
  146. || (uniq == 2 && count >= 5)
  147. || (uniq == 3 && count >= 6))
  148. && lshift_cheap_p (speed_p)
  149. && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
  150. && compare_tree_int (range, 0) > 0);
  151. }
  152. /* Implement switch statements with bit tests
  153. A GIMPLE switch statement can be expanded to a short sequence of bit-wise
  154. comparisons. "switch(x)" is converted into "if ((1 << (x-MINVAL)) & CST)"
  155. where CST and MINVAL are integer constants. This is better than a series
  156. of compare-and-banch insns in some cases, e.g. we can implement:
  157. if ((x==4) || (x==6) || (x==9) || (x==11))
  158. as a single bit test:
  159. if ((1<<x) & ((1<<4)|(1<<6)|(1<<9)|(1<<11)))
  160. This transformation is only applied if the number of case targets is small,
  161. if CST constains at least 3 bits, and "1 << x" is cheap. The bit tests are
  162. performed in "word_mode".
  163. The following example shows the code the transformation generates:
  164. int bar(int x)
  165. {
  166. switch (x)
  167. {
  168. case '0': case '1': case '2': case '3': case '4':
  169. case '5': case '6': case '7': case '8': case '9':
  170. case 'A': case 'B': case 'C': case 'D': case 'E':
  171. case 'F':
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. ==>
  177. bar (int x)
  178. {
  179. tmp1 = x - 48;
  180. if (tmp1 > (70 - 48)) goto L2;
  181. tmp2 = 1 << tmp1;
  182. tmp3 = 0b11111100000001111111111;
  183. if ((tmp2 & tmp3) != 0) goto L1 ; else goto L2;
  184. L1:
  185. return 1;
  186. L2:
  187. return 0;
  188. }
  189. TODO: There are still some improvements to this transformation that could
  190. be implemented:
  191. * A narrower mode than word_mode could be used if that is cheaper, e.g.
  192. for x86_64 where a narrower-mode shift may result in smaller code.
  193. * The compounded constant could be shifted rather than the one. The
  194. test would be either on the sign bit or on the least significant bit,
  195. depending on the direction of the shift. On some machines, the test
  196. for the branch would be free if the bit to test is already set by the
  197. shift operation.
  198. This transformation was contributed by Roger Sayle, see this e-mail:
  199. http://gcc.gnu.org/ml/gcc-patches/2003-01/msg01950.html
  200. */
  201. /* A case_bit_test represents a set of case nodes that may be
  202. selected from using a bit-wise comparison. HI and LO hold
  203. the integer to be tested against, TARGET_EDGE contains the
  204. edge to the basic block to jump to upon success and BITS
  205. counts the number of case nodes handled by this test,
  206. typically the number of bits set in HI:LO. The LABEL field
  207. is used to quickly identify all cases in this set without
  208. looking at label_to_block for every case label. */
  209. struct case_bit_test
  210. {
  211. wide_int mask;
  212. edge target_edge;
  213. tree label;
  214. int bits;
  215. };
  216. /* Comparison function for qsort to order bit tests by decreasing
  217. probability of execution. Our best guess comes from a measured
  218. profile. If the profile counts are equal, break even on the
  219. number of case nodes, i.e. the node with the most cases gets
  220. tested first.
  221. TODO: Actually this currently runs before a profile is available.
  222. Therefore the case-as-bit-tests transformation should be done
  223. later in the pass pipeline, or something along the lines of
  224. "Efficient and effective branch reordering using profile data"
  225. (Yang et. al., 2002) should be implemented (although, how good
  226. is a paper is called "Efficient and effective ..." when the
  227. latter is implied by the former, but oh well...). */
  228. static int
  229. case_bit_test_cmp (const void *p1, const void *p2)
  230. {
  231. const struct case_bit_test *const d1 = (const struct case_bit_test *) p1;
  232. const struct case_bit_test *const d2 = (const struct case_bit_test *) p2;
  233. if (d2->target_edge->count != d1->target_edge->count)
  234. return d2->target_edge->count - d1->target_edge->count;
  235. if (d2->bits != d1->bits)
  236. return d2->bits - d1->bits;
  237. /* Stabilize the sort. */
  238. return LABEL_DECL_UID (d2->label) - LABEL_DECL_UID (d1->label);
  239. }
  240. /* Expand a switch statement by a short sequence of bit-wise
  241. comparisons. "switch(x)" is effectively converted into
  242. "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
  243. integer constants.
  244. INDEX_EXPR is the value being switched on.
  245. MINVAL is the lowest case value of in the case nodes,
  246. and RANGE is highest value minus MINVAL. MINVAL and RANGE
  247. are not guaranteed to be of the same type as INDEX_EXPR
  248. (the gimplifier doesn't change the type of case label values,
  249. and MINVAL and RANGE are derived from those values).
  250. MAXVAL is MINVAL + RANGE.
  251. There *MUST* be MAX_CASE_BIT_TESTS or less unique case
  252. node targets. */
  253. static void
  254. emit_case_bit_tests (gswitch *swtch, tree index_expr,
  255. tree minval, tree range, tree maxval)
  256. {
  257. struct case_bit_test test[MAX_CASE_BIT_TESTS];
  258. unsigned int i, j, k;
  259. unsigned int count;
  260. basic_block switch_bb = gimple_bb (swtch);
  261. basic_block default_bb, new_default_bb, new_bb;
  262. edge default_edge;
  263. bool update_dom = dom_info_available_p (CDI_DOMINATORS);
  264. vec<basic_block> bbs_to_fix_dom = vNULL;
  265. tree index_type = TREE_TYPE (index_expr);
  266. tree unsigned_index_type = unsigned_type_for (index_type);
  267. unsigned int branch_num = gimple_switch_num_labels (swtch);
  268. gimple_stmt_iterator gsi;
  269. gassign *shift_stmt;
  270. tree idx, tmp, csui;
  271. tree word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
  272. tree word_mode_zero = fold_convert (word_type_node, integer_zero_node);
  273. tree word_mode_one = fold_convert (word_type_node, integer_one_node);
  274. int prec = TYPE_PRECISION (word_type_node);
  275. wide_int wone = wi::one (prec);
  276. memset (&test, 0, sizeof (test));
  277. /* Get the edge for the default case. */
  278. tmp = gimple_switch_default_label (swtch);
  279. default_bb = label_to_block (CASE_LABEL (tmp));
  280. default_edge = find_edge (switch_bb, default_bb);
  281. /* Go through all case labels, and collect the case labels, profile
  282. counts, and other information we need to build the branch tests. */
  283. count = 0;
  284. for (i = 1; i < branch_num; i++)
  285. {
  286. unsigned int lo, hi;
  287. tree cs = gimple_switch_label (swtch, i);
  288. tree label = CASE_LABEL (cs);
  289. edge e = find_edge (switch_bb, label_to_block (label));
  290. for (k = 0; k < count; k++)
  291. if (e == test[k].target_edge)
  292. break;
  293. if (k == count)
  294. {
  295. gcc_checking_assert (count < MAX_CASE_BIT_TESTS);
  296. test[k].mask = wi::zero (prec);
  297. test[k].target_edge = e;
  298. test[k].label = label;
  299. test[k].bits = 1;
  300. count++;
  301. }
  302. else
  303. test[k].bits++;
  304. lo = tree_to_uhwi (int_const_binop (MINUS_EXPR,
  305. CASE_LOW (cs), minval));
  306. if (CASE_HIGH (cs) == NULL_TREE)
  307. hi = lo;
  308. else
  309. hi = tree_to_uhwi (int_const_binop (MINUS_EXPR,
  310. CASE_HIGH (cs), minval));
  311. for (j = lo; j <= hi; j++)
  312. test[k].mask |= wi::lshift (wone, j);
  313. }
  314. qsort (test, count, sizeof (*test), case_bit_test_cmp);
  315. /* If all values are in the 0 .. BITS_PER_WORD-1 range, we can get rid of
  316. the minval subtractions, but it might make the mask constants more
  317. expensive. So, compare the costs. */
  318. if (compare_tree_int (minval, 0) > 0
  319. && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
  320. {
  321. int cost_diff;
  322. HOST_WIDE_INT m = tree_to_uhwi (minval);
  323. rtx reg = gen_raw_REG (word_mode, 10000);
  324. bool speed_p = optimize_bb_for_speed_p (gimple_bb (swtch));
  325. cost_diff = set_rtx_cost (gen_rtx_PLUS (word_mode, reg,
  326. GEN_INT (-m)), speed_p);
  327. for (i = 0; i < count; i++)
  328. {
  329. rtx r = immed_wide_int_const (test[i].mask, word_mode);
  330. cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r), speed_p);
  331. r = immed_wide_int_const (wi::lshift (test[i].mask, m), word_mode);
  332. cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r), speed_p);
  333. }
  334. if (cost_diff > 0)
  335. {
  336. for (i = 0; i < count; i++)
  337. test[i].mask = wi::lshift (test[i].mask, m);
  338. minval = build_zero_cst (TREE_TYPE (minval));
  339. range = maxval;
  340. }
  341. }
  342. /* We generate two jumps to the default case label.
  343. Split the default edge, so that we don't have to do any PHI node
  344. updating. */
  345. new_default_bb = split_edge (default_edge);
  346. if (update_dom)
  347. {
  348. bbs_to_fix_dom.create (10);
  349. bbs_to_fix_dom.quick_push (switch_bb);
  350. bbs_to_fix_dom.quick_push (default_bb);
  351. bbs_to_fix_dom.quick_push (new_default_bb);
  352. }
  353. /* Now build the test-and-branch code. */
  354. gsi = gsi_last_bb (switch_bb);
  355. /* idx = (unsigned)x - minval. */
  356. idx = fold_convert (unsigned_index_type, index_expr);
  357. idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx,
  358. fold_convert (unsigned_index_type, minval));
  359. idx = force_gimple_operand_gsi (&gsi, idx,
  360. /*simple=*/true, NULL_TREE,
  361. /*before=*/true, GSI_SAME_STMT);
  362. /* if (idx > range) goto default */
  363. range = force_gimple_operand_gsi (&gsi,
  364. fold_convert (unsigned_index_type, range),
  365. /*simple=*/true, NULL_TREE,
  366. /*before=*/true, GSI_SAME_STMT);
  367. tmp = fold_build2 (GT_EXPR, boolean_type_node, idx, range);
  368. new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, default_edge, update_dom);
  369. if (update_dom)
  370. bbs_to_fix_dom.quick_push (new_bb);
  371. gcc_assert (gimple_bb (swtch) == new_bb);
  372. gsi = gsi_last_bb (new_bb);
  373. /* Any blocks dominated by the GIMPLE_SWITCH, but that are not successors
  374. of NEW_BB, are still immediately dominated by SWITCH_BB. Make it so. */
  375. if (update_dom)
  376. {
  377. vec<basic_block> dom_bbs;
  378. basic_block dom_son;
  379. dom_bbs = get_dominated_by (CDI_DOMINATORS, new_bb);
  380. FOR_EACH_VEC_ELT (dom_bbs, i, dom_son)
  381. {
  382. edge e = find_edge (new_bb, dom_son);
  383. if (e && single_pred_p (e->dest))
  384. continue;
  385. set_immediate_dominator (CDI_DOMINATORS, dom_son, switch_bb);
  386. bbs_to_fix_dom.safe_push (dom_son);
  387. }
  388. dom_bbs.release ();
  389. }
  390. /* csui = (1 << (word_mode) idx) */
  391. csui = make_ssa_name (word_type_node);
  392. tmp = fold_build2 (LSHIFT_EXPR, word_type_node, word_mode_one,
  393. fold_convert (word_type_node, idx));
  394. tmp = force_gimple_operand_gsi (&gsi, tmp,
  395. /*simple=*/false, NULL_TREE,
  396. /*before=*/true, GSI_SAME_STMT);
  397. shift_stmt = gimple_build_assign (csui, tmp);
  398. gsi_insert_before (&gsi, shift_stmt, GSI_SAME_STMT);
  399. update_stmt (shift_stmt);
  400. /* for each unique set of cases:
  401. if (const & csui) goto target */
  402. for (k = 0; k < count; k++)
  403. {
  404. tmp = wide_int_to_tree (word_type_node, test[k].mask);
  405. tmp = fold_build2 (BIT_AND_EXPR, word_type_node, csui, tmp);
  406. tmp = force_gimple_operand_gsi (&gsi, tmp,
  407. /*simple=*/true, NULL_TREE,
  408. /*before=*/true, GSI_SAME_STMT);
  409. tmp = fold_build2 (NE_EXPR, boolean_type_node, tmp, word_mode_zero);
  410. new_bb = hoist_edge_and_branch_if_true (&gsi, tmp, test[k].target_edge,
  411. update_dom);
  412. if (update_dom)
  413. bbs_to_fix_dom.safe_push (new_bb);
  414. gcc_assert (gimple_bb (swtch) == new_bb);
  415. gsi = gsi_last_bb (new_bb);
  416. }
  417. /* We should have removed all edges now. */
  418. gcc_assert (EDGE_COUNT (gsi_bb (gsi)->succs) == 0);
  419. /* If nothing matched, go to the default label. */
  420. make_edge (gsi_bb (gsi), new_default_bb, EDGE_FALLTHRU);
  421. /* The GIMPLE_SWITCH is now redundant. */
  422. gsi_remove (&gsi, true);
  423. if (update_dom)
  424. {
  425. /* Fix up the dominator tree. */
  426. iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
  427. bbs_to_fix_dom.release ();
  428. }
  429. }
  430. /*
  431. Switch initialization conversion
  432. The following pass changes simple initializations of scalars in a switch
  433. statement into initializations from a static array. Obviously, the values
  434. must be constant and known at compile time and a default branch must be
  435. provided. For example, the following code:
  436. int a,b;
  437. switch (argc)
  438. {
  439. case 1:
  440. case 2:
  441. a_1 = 8;
  442. b_1 = 6;
  443. break;
  444. case 3:
  445. a_2 = 9;
  446. b_2 = 5;
  447. break;
  448. case 12:
  449. a_3 = 10;
  450. b_3 = 4;
  451. break;
  452. default:
  453. a_4 = 16;
  454. b_4 = 1;
  455. break;
  456. }
  457. a_5 = PHI <a_1, a_2, a_3, a_4>
  458. b_5 = PHI <b_1, b_2, b_3, b_4>
  459. is changed into:
  460. static const int = CSWTCH01[] = {6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 4};
  461. static const int = CSWTCH02[] = {8, 8, 9, 16, 16, 16, 16, 16, 16, 16,
  462. 16, 16, 10};
  463. if (((unsigned) argc) - 1 < 11)
  464. {
  465. a_6 = CSWTCH02[argc - 1];
  466. b_6 = CSWTCH01[argc - 1];
  467. }
  468. else
  469. {
  470. a_7 = 16;
  471. b_7 = 1;
  472. }
  473. a_5 = PHI <a_6, a_7>
  474. b_b = PHI <b_6, b_7>
  475. There are further constraints. Specifically, the range of values across all
  476. case labels must not be bigger than SWITCH_CONVERSION_BRANCH_RATIO (default
  477. eight) times the number of the actual switch branches.
  478. This transformation was contributed by Martin Jambor, see this e-mail:
  479. http://gcc.gnu.org/ml/gcc-patches/2008-07/msg00011.html */
  480. /* The main structure of the pass. */
  481. struct switch_conv_info
  482. {
  483. /* The expression used to decide the switch branch. */
  484. tree index_expr;
  485. /* The following integer constants store the minimum and maximum value
  486. covered by the case labels. */
  487. tree range_min;
  488. tree range_max;
  489. /* The difference between the above two numbers. Stored here because it
  490. is used in all the conversion heuristics, as well as for some of the
  491. transformation, and it is expensive to re-compute it all the time. */
  492. tree range_size;
  493. /* Basic block that contains the actual GIMPLE_SWITCH. */
  494. basic_block switch_bb;
  495. /* Basic block that is the target of the default case. */
  496. basic_block default_bb;
  497. /* The single successor block of all branches out of the GIMPLE_SWITCH,
  498. if such a block exists. Otherwise NULL. */
  499. basic_block final_bb;
  500. /* The probability of the default edge in the replaced switch. */
  501. int default_prob;
  502. /* The count of the default edge in the replaced switch. */
  503. gcov_type default_count;
  504. /* Combined count of all other (non-default) edges in the replaced switch. */
  505. gcov_type other_count;
  506. /* Number of phi nodes in the final bb (that we'll be replacing). */
  507. int phi_count;
  508. /* Array of default values, in the same order as phi nodes. */
  509. tree *default_values;
  510. /* Constructors of new static arrays. */
  511. vec<constructor_elt, va_gc> **constructors;
  512. /* Array of ssa names that are initialized with a value from a new static
  513. array. */
  514. tree *target_inbound_names;
  515. /* Array of ssa names that are initialized with the default value if the
  516. switch expression is out of range. */
  517. tree *target_outbound_names;
  518. /* The first load statement that loads a temporary from a new static array.
  519. */
  520. gimple arr_ref_first;
  521. /* The last load statement that loads a temporary from a new static array. */
  522. gimple arr_ref_last;
  523. /* String reason why the case wasn't a good candidate that is written to the
  524. dump file, if there is one. */
  525. const char *reason;
  526. /* Parameters for expand_switch_using_bit_tests. Should be computed
  527. the same way as in expand_case. */
  528. unsigned int uniq;
  529. unsigned int count;
  530. };
  531. /* Collect information about GIMPLE_SWITCH statement SWTCH into INFO. */
  532. static void
  533. collect_switch_conv_info (gswitch *swtch, struct switch_conv_info *info)
  534. {
  535. unsigned int branch_num = gimple_switch_num_labels (swtch);
  536. tree min_case, max_case;
  537. unsigned int count, i;
  538. edge e, e_default;
  539. edge_iterator ei;
  540. memset (info, 0, sizeof (*info));
  541. /* The gimplifier has already sorted the cases by CASE_LOW and ensured there
  542. is a default label which is the first in the vector.
  543. Collect the bits we can deduce from the CFG. */
  544. info->index_expr = gimple_switch_index (swtch);
  545. info->switch_bb = gimple_bb (swtch);
  546. info->default_bb =
  547. label_to_block (CASE_LABEL (gimple_switch_default_label (swtch)));
  548. e_default = find_edge (info->switch_bb, info->default_bb);
  549. info->default_prob = e_default->probability;
  550. info->default_count = e_default->count;
  551. FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
  552. if (e != e_default)
  553. info->other_count += e->count;
  554. /* See if there is one common successor block for all branch
  555. targets. If it exists, record it in FINAL_BB.
  556. Start with the destination of the default case as guess
  557. or its destination in case it is a forwarder block. */
  558. if (! single_pred_p (e_default->dest))
  559. info->final_bb = e_default->dest;
  560. else if (single_succ_p (e_default->dest)
  561. && ! single_pred_p (single_succ (e_default->dest)))
  562. info->final_bb = single_succ (e_default->dest);
  563. /* Require that all switch destinations are either that common
  564. FINAL_BB or a forwarder to it. */
  565. if (info->final_bb)
  566. FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
  567. {
  568. if (e->dest == info->final_bb)
  569. continue;
  570. if (single_pred_p (e->dest)
  571. && single_succ_p (e->dest)
  572. && single_succ (e->dest) == info->final_bb)
  573. continue;
  574. info->final_bb = NULL;
  575. break;
  576. }
  577. /* Get upper and lower bounds of case values, and the covered range. */
  578. min_case = gimple_switch_label (swtch, 1);
  579. max_case = gimple_switch_label (swtch, branch_num - 1);
  580. info->range_min = CASE_LOW (min_case);
  581. if (CASE_HIGH (max_case) != NULL_TREE)
  582. info->range_max = CASE_HIGH (max_case);
  583. else
  584. info->range_max = CASE_LOW (max_case);
  585. info->range_size =
  586. int_const_binop (MINUS_EXPR, info->range_max, info->range_min);
  587. /* Get a count of the number of case labels. Single-valued case labels
  588. simply count as one, but a case range counts double, since it may
  589. require two compares if it gets lowered as a branching tree. */
  590. count = 0;
  591. for (i = 1; i < branch_num; i++)
  592. {
  593. tree elt = gimple_switch_label (swtch, i);
  594. count++;
  595. if (CASE_HIGH (elt)
  596. && ! tree_int_cst_equal (CASE_LOW (elt), CASE_HIGH (elt)))
  597. count++;
  598. }
  599. info->count = count;
  600. /* Get the number of unique non-default targets out of the GIMPLE_SWITCH
  601. block. Assume a CFG cleanup would have already removed degenerate
  602. switch statements, this allows us to just use EDGE_COUNT. */
  603. info->uniq = EDGE_COUNT (gimple_bb (swtch)->succs) - 1;
  604. }
  605. /* Checks whether the range given by individual case statements of the SWTCH
  606. switch statement isn't too big and whether the number of branches actually
  607. satisfies the size of the new array. */
  608. static bool
  609. check_range (struct switch_conv_info *info)
  610. {
  611. gcc_assert (info->range_size);
  612. if (!tree_fits_uhwi_p (info->range_size))
  613. {
  614. info->reason = "index range way too large or otherwise unusable";
  615. return false;
  616. }
  617. if (tree_to_uhwi (info->range_size)
  618. > ((unsigned) info->count * SWITCH_CONVERSION_BRANCH_RATIO))
  619. {
  620. info->reason = "the maximum range-branch ratio exceeded";
  621. return false;
  622. }
  623. return true;
  624. }
  625. /* Checks whether all but the FINAL_BB basic blocks are empty. */
  626. static bool
  627. check_all_empty_except_final (struct switch_conv_info *info)
  628. {
  629. edge e;
  630. edge_iterator ei;
  631. FOR_EACH_EDGE (e, ei, info->switch_bb->succs)
  632. {
  633. if (e->dest == info->final_bb)
  634. continue;
  635. if (!empty_block_p (e->dest))
  636. {
  637. info->reason = "bad case - a non-final BB not empty";
  638. return false;
  639. }
  640. }
  641. return true;
  642. }
  643. /* This function checks whether all required values in phi nodes in final_bb
  644. are constants. Required values are those that correspond to a basic block
  645. which is a part of the examined switch statement. It returns true if the
  646. phi nodes are OK, otherwise false. */
  647. static bool
  648. check_final_bb (struct switch_conv_info *info)
  649. {
  650. gphi_iterator gsi;
  651. info->phi_count = 0;
  652. for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
  653. {
  654. gphi *phi = gsi.phi ();
  655. unsigned int i;
  656. info->phi_count++;
  657. for (i = 0; i < gimple_phi_num_args (phi); i++)
  658. {
  659. basic_block bb = gimple_phi_arg_edge (phi, i)->src;
  660. if (bb == info->switch_bb
  661. || (single_pred_p (bb) && single_pred (bb) == info->switch_bb))
  662. {
  663. tree reloc, val;
  664. val = gimple_phi_arg_def (phi, i);
  665. if (!is_gimple_ip_invariant (val))
  666. {
  667. info->reason = "non-invariant value from a case";
  668. return false; /* Non-invariant argument. */
  669. }
  670. reloc = initializer_constant_valid_p (val, TREE_TYPE (val));
  671. if ((flag_pic && reloc != null_pointer_node)
  672. || (!flag_pic && reloc == NULL_TREE))
  673. {
  674. if (reloc)
  675. info->reason
  676. = "value from a case would need runtime relocations";
  677. else
  678. info->reason
  679. = "value from a case is not a valid initializer";
  680. return false;
  681. }
  682. }
  683. }
  684. }
  685. return true;
  686. }
  687. /* The following function allocates default_values, target_{in,out}_names and
  688. constructors arrays. The last one is also populated with pointers to
  689. vectors that will become constructors of new arrays. */
  690. static void
  691. create_temp_arrays (struct switch_conv_info *info)
  692. {
  693. int i;
  694. info->default_values = XCNEWVEC (tree, info->phi_count * 3);
  695. /* ??? Macros do not support multi argument templates in their
  696. argument list. We create a typedef to work around that problem. */
  697. typedef vec<constructor_elt, va_gc> *vec_constructor_elt_gc;
  698. info->constructors = XCNEWVEC (vec_constructor_elt_gc, info->phi_count);
  699. info->target_inbound_names = info->default_values + info->phi_count;
  700. info->target_outbound_names = info->target_inbound_names + info->phi_count;
  701. for (i = 0; i < info->phi_count; i++)
  702. vec_alloc (info->constructors[i], tree_to_uhwi (info->range_size) + 1);
  703. }
  704. /* Free the arrays created by create_temp_arrays(). The vectors that are
  705. created by that function are not freed here, however, because they have
  706. already become constructors and must be preserved. */
  707. static void
  708. free_temp_arrays (struct switch_conv_info *info)
  709. {
  710. XDELETEVEC (info->constructors);
  711. XDELETEVEC (info->default_values);
  712. }
  713. /* Populate the array of default values in the order of phi nodes.
  714. DEFAULT_CASE is the CASE_LABEL_EXPR for the default switch branch. */
  715. static void
  716. gather_default_values (tree default_case, struct switch_conv_info *info)
  717. {
  718. gphi_iterator gsi;
  719. basic_block bb = label_to_block (CASE_LABEL (default_case));
  720. edge e;
  721. int i = 0;
  722. gcc_assert (CASE_LOW (default_case) == NULL_TREE);
  723. if (bb == info->final_bb)
  724. e = find_edge (info->switch_bb, bb);
  725. else
  726. e = single_succ_edge (bb);
  727. for (gsi = gsi_start_phis (info->final_bb); !gsi_end_p (gsi); gsi_next (&gsi))
  728. {
  729. gphi *phi = gsi.phi ();
  730. tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
  731. gcc_assert (val);
  732. info->default_values[i++] = val;
  733. }
  734. }
  735. /* The following function populates the vectors in the constructors array with
  736. future contents of the static arrays. The vectors are populated in the
  737. order of phi nodes. SWTCH is the switch statement being converted. */
  738. static void
  739. build_constructors (gswitch *swtch, struct switch_conv_info *info)
  740. {
  741. unsigned i, branch_num = gimple_switch_num_labels (swtch);
  742. tree pos = info->range_min;
  743. for (i = 1; i < branch_num; i++)
  744. {
  745. tree cs = gimple_switch_label (swtch, i);
  746. basic_block bb = label_to_block (CASE_LABEL (cs));
  747. edge e;
  748. tree high;
  749. gphi_iterator gsi;
  750. int j;
  751. if (bb == info->final_bb)
  752. e = find_edge (info->switch_bb, bb);
  753. else
  754. e = single_succ_edge (bb);
  755. gcc_assert (e);
  756. while (tree_int_cst_lt (pos, CASE_LOW (cs)))
  757. {
  758. int k;
  759. for (k = 0; k < info->phi_count; k++)
  760. {
  761. constructor_elt elt;
  762. elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
  763. elt.value
  764. = unshare_expr_without_location (info->default_values[k]);
  765. info->constructors[k]->quick_push (elt);
  766. }
  767. pos = int_const_binop (PLUS_EXPR, pos,
  768. build_int_cst (TREE_TYPE (pos), 1));
  769. }
  770. gcc_assert (tree_int_cst_equal (pos, CASE_LOW (cs)));
  771. j = 0;
  772. if (CASE_HIGH (cs))
  773. high = CASE_HIGH (cs);
  774. else
  775. high = CASE_LOW (cs);
  776. for (gsi = gsi_start_phis (info->final_bb);
  777. !gsi_end_p (gsi); gsi_next (&gsi))
  778. {
  779. gphi *phi = gsi.phi ();
  780. tree val = PHI_ARG_DEF_FROM_EDGE (phi, e);
  781. tree low = CASE_LOW (cs);
  782. pos = CASE_LOW (cs);
  783. do
  784. {
  785. constructor_elt elt;
  786. elt.index = int_const_binop (MINUS_EXPR, pos, info->range_min);
  787. elt.value = unshare_expr_without_location (val);
  788. info->constructors[j]->quick_push (elt);
  789. pos = int_const_binop (PLUS_EXPR, pos,
  790. build_int_cst (TREE_TYPE (pos), 1));
  791. } while (!tree_int_cst_lt (high, pos)
  792. && tree_int_cst_lt (low, pos));
  793. j++;
  794. }
  795. }
  796. }
  797. /* If all values in the constructor vector are the same, return the value.
  798. Otherwise return NULL_TREE. Not supposed to be called for empty
  799. vectors. */
  800. static tree
  801. constructor_contains_same_values_p (vec<constructor_elt, va_gc> *vec)
  802. {
  803. unsigned int i;
  804. tree prev = NULL_TREE;
  805. constructor_elt *elt;
  806. FOR_EACH_VEC_SAFE_ELT (vec, i, elt)
  807. {
  808. if (!prev)
  809. prev = elt->value;
  810. else if (!operand_equal_p (elt->value, prev, OEP_ONLY_CONST))
  811. return NULL_TREE;
  812. }
  813. return prev;
  814. }
  815. /* Return type which should be used for array elements, either TYPE,
  816. or for integral type some smaller integral type that can still hold
  817. all the constants. */
  818. static tree
  819. array_value_type (gswitch *swtch, tree type, int num,
  820. struct switch_conv_info *info)
  821. {
  822. unsigned int i, len = vec_safe_length (info->constructors[num]);
  823. constructor_elt *elt;
  824. machine_mode mode;
  825. int sign = 0;
  826. tree smaller_type;
  827. if (!INTEGRAL_TYPE_P (type))
  828. return type;
  829. mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (TYPE_MODE (type)));
  830. if (GET_MODE_SIZE (TYPE_MODE (type)) <= GET_MODE_SIZE (mode))
  831. return type;
  832. if (len < (optimize_bb_for_size_p (gimple_bb (swtch)) ? 2 : 32))
  833. return type;
  834. FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
  835. {
  836. wide_int cst;
  837. if (TREE_CODE (elt->value) != INTEGER_CST)
  838. return type;
  839. cst = elt->value;
  840. while (1)
  841. {
  842. unsigned int prec = GET_MODE_BITSIZE (mode);
  843. if (prec > HOST_BITS_PER_WIDE_INT)
  844. return type;
  845. if (sign >= 0 && cst == wi::zext (cst, prec))
  846. {
  847. if (sign == 0 && cst == wi::sext (cst, prec))
  848. break;
  849. sign = 1;
  850. break;
  851. }
  852. if (sign <= 0 && cst == wi::sext (cst, prec))
  853. {
  854. sign = -1;
  855. break;
  856. }
  857. if (sign == 1)
  858. sign = 0;
  859. mode = GET_MODE_WIDER_MODE (mode);
  860. if (mode == VOIDmode
  861. || GET_MODE_SIZE (mode) >= GET_MODE_SIZE (TYPE_MODE (type)))
  862. return type;
  863. }
  864. }
  865. if (sign == 0)
  866. sign = TYPE_UNSIGNED (type) ? 1 : -1;
  867. smaller_type = lang_hooks.types.type_for_mode (mode, sign >= 0);
  868. if (GET_MODE_SIZE (TYPE_MODE (type))
  869. <= GET_MODE_SIZE (TYPE_MODE (smaller_type)))
  870. return type;
  871. return smaller_type;
  872. }
  873. /* Create an appropriate array type and declaration and assemble a static array
  874. variable. Also create a load statement that initializes the variable in
  875. question with a value from the static array. SWTCH is the switch statement
  876. being converted, NUM is the index to arrays of constructors, default values
  877. and target SSA names for this particular array. ARR_INDEX_TYPE is the type
  878. of the index of the new array, PHI is the phi node of the final BB that
  879. corresponds to the value that will be loaded from the created array. TIDX
  880. is an ssa name of a temporary variable holding the index for loads from the
  881. new array. */
  882. static void
  883. build_one_array (gswitch *swtch, int num, tree arr_index_type,
  884. gphi *phi, tree tidx, struct switch_conv_info *info)
  885. {
  886. tree name, cst;
  887. gimple load;
  888. gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
  889. location_t loc = gimple_location (swtch);
  890. gcc_assert (info->default_values[num]);
  891. name = copy_ssa_name (PHI_RESULT (phi));
  892. info->target_inbound_names[num] = name;
  893. cst = constructor_contains_same_values_p (info->constructors[num]);
  894. if (cst)
  895. load = gimple_build_assign (name, cst);
  896. else
  897. {
  898. tree array_type, ctor, decl, value_type, fetch, default_type;
  899. default_type = TREE_TYPE (info->default_values[num]);
  900. value_type = array_value_type (swtch, default_type, num, info);
  901. array_type = build_array_type (value_type, arr_index_type);
  902. if (default_type != value_type)
  903. {
  904. unsigned int i;
  905. constructor_elt *elt;
  906. FOR_EACH_VEC_SAFE_ELT (info->constructors[num], i, elt)
  907. elt->value = fold_convert (value_type, elt->value);
  908. }
  909. ctor = build_constructor (array_type, info->constructors[num]);
  910. TREE_CONSTANT (ctor) = true;
  911. TREE_STATIC (ctor) = true;
  912. decl = build_decl (loc, VAR_DECL, NULL_TREE, array_type);
  913. TREE_STATIC (decl) = 1;
  914. DECL_INITIAL (decl) = ctor;
  915. DECL_NAME (decl) = create_tmp_var_name ("CSWTCH");
  916. DECL_ARTIFICIAL (decl) = 1;
  917. TREE_CONSTANT (decl) = 1;
  918. TREE_READONLY (decl) = 1;
  919. varpool_node::finalize_decl (decl);
  920. fetch = build4 (ARRAY_REF, value_type, decl, tidx, NULL_TREE,
  921. NULL_TREE);
  922. if (default_type != value_type)
  923. {
  924. fetch = fold_convert (default_type, fetch);
  925. fetch = force_gimple_operand_gsi (&gsi, fetch, true, NULL_TREE,
  926. true, GSI_SAME_STMT);
  927. }
  928. load = gimple_build_assign (name, fetch);
  929. }
  930. gsi_insert_before (&gsi, load, GSI_SAME_STMT);
  931. update_stmt (load);
  932. info->arr_ref_last = load;
  933. }
  934. /* Builds and initializes static arrays initialized with values gathered from
  935. the SWTCH switch statement. Also creates statements that load values from
  936. them. */
  937. static void
  938. build_arrays (gswitch *swtch, struct switch_conv_info *info)
  939. {
  940. tree arr_index_type;
  941. tree tidx, sub, utype;
  942. gimple stmt;
  943. gimple_stmt_iterator gsi;
  944. gphi_iterator gpi;
  945. int i;
  946. location_t loc = gimple_location (swtch);
  947. gsi = gsi_for_stmt (swtch);
  948. /* Make sure we do not generate arithmetics in a subrange. */
  949. utype = TREE_TYPE (info->index_expr);
  950. if (TREE_TYPE (utype))
  951. utype = lang_hooks.types.type_for_mode (TYPE_MODE (TREE_TYPE (utype)), 1);
  952. else
  953. utype = lang_hooks.types.type_for_mode (TYPE_MODE (utype), 1);
  954. arr_index_type = build_index_type (info->range_size);
  955. tidx = make_ssa_name (utype);
  956. sub = fold_build2_loc (loc, MINUS_EXPR, utype,
  957. fold_convert_loc (loc, utype, info->index_expr),
  958. fold_convert_loc (loc, utype, info->range_min));
  959. sub = force_gimple_operand_gsi (&gsi, sub,
  960. false, NULL, true, GSI_SAME_STMT);
  961. stmt = gimple_build_assign (tidx, sub);
  962. gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
  963. update_stmt (stmt);
  964. info->arr_ref_first = stmt;
  965. for (gpi = gsi_start_phis (info->final_bb), i = 0;
  966. !gsi_end_p (gpi); gsi_next (&gpi), i++)
  967. build_one_array (swtch, i, arr_index_type, gpi.phi (), tidx, info);
  968. }
  969. /* Generates and appropriately inserts loads of default values at the position
  970. given by BSI. Returns the last inserted statement. */
  971. static gassign *
  972. gen_def_assigns (gimple_stmt_iterator *gsi, struct switch_conv_info *info)
  973. {
  974. int i;
  975. gassign *assign = NULL;
  976. for (i = 0; i < info->phi_count; i++)
  977. {
  978. tree name = copy_ssa_name (info->target_inbound_names[i]);
  979. info->target_outbound_names[i] = name;
  980. assign = gimple_build_assign (name, info->default_values[i]);
  981. gsi_insert_before (gsi, assign, GSI_SAME_STMT);
  982. update_stmt (assign);
  983. }
  984. return assign;
  985. }
  986. /* Deletes the unused bbs and edges that now contain the switch statement and
  987. its empty branch bbs. BBD is the now dead BB containing the original switch
  988. statement, FINAL is the last BB of the converted switch statement (in terms
  989. of succession). */
  990. static void
  991. prune_bbs (basic_block bbd, basic_block final)
  992. {
  993. edge_iterator ei;
  994. edge e;
  995. for (ei = ei_start (bbd->succs); (e = ei_safe_edge (ei)); )
  996. {
  997. basic_block bb;
  998. bb = e->dest;
  999. remove_edge (e);
  1000. if (bb != final)
  1001. delete_basic_block (bb);
  1002. }
  1003. delete_basic_block (bbd);
  1004. }
  1005. /* Add values to phi nodes in final_bb for the two new edges. E1F is the edge
  1006. from the basic block loading values from an array and E2F from the basic
  1007. block loading default values. BBF is the last switch basic block (see the
  1008. bbf description in the comment below). */
  1009. static void
  1010. fix_phi_nodes (edge e1f, edge e2f, basic_block bbf,
  1011. struct switch_conv_info *info)
  1012. {
  1013. gphi_iterator gsi;
  1014. int i;
  1015. for (gsi = gsi_start_phis (bbf), i = 0;
  1016. !gsi_end_p (gsi); gsi_next (&gsi), i++)
  1017. {
  1018. gphi *phi = gsi.phi ();
  1019. add_phi_arg (phi, info->target_inbound_names[i], e1f, UNKNOWN_LOCATION);
  1020. add_phi_arg (phi, info->target_outbound_names[i], e2f, UNKNOWN_LOCATION);
  1021. }
  1022. }
  1023. /* Creates a check whether the switch expression value actually falls into the
  1024. range given by all the cases. If it does not, the temporaries are loaded
  1025. with default values instead. SWTCH is the switch statement being converted.
  1026. bb0 is the bb with the switch statement, however, we'll end it with a
  1027. condition instead.
  1028. bb1 is the bb to be used when the range check went ok. It is derived from
  1029. the switch BB
  1030. bb2 is the bb taken when the expression evaluated outside of the range
  1031. covered by the created arrays. It is populated by loads of default
  1032. values.
  1033. bbF is a fall through for both bb1 and bb2 and contains exactly what
  1034. originally followed the switch statement.
  1035. bbD contains the switch statement (in the end). It is unreachable but we
  1036. still need to strip off its edges.
  1037. */
  1038. static void
  1039. gen_inbound_check (gswitch *swtch, struct switch_conv_info *info)
  1040. {
  1041. tree label_decl1 = create_artificial_label (UNKNOWN_LOCATION);
  1042. tree label_decl2 = create_artificial_label (UNKNOWN_LOCATION);
  1043. tree label_decl3 = create_artificial_label (UNKNOWN_LOCATION);
  1044. glabel *label1, *label2, *label3;
  1045. tree utype, tidx;
  1046. tree bound;
  1047. gcond *cond_stmt;
  1048. gassign *last_assign;
  1049. gimple_stmt_iterator gsi;
  1050. basic_block bb0, bb1, bb2, bbf, bbd;
  1051. edge e01, e02, e21, e1d, e1f, e2f;
  1052. location_t loc = gimple_location (swtch);
  1053. gcc_assert (info->default_values);
  1054. bb0 = gimple_bb (swtch);
  1055. tidx = gimple_assign_lhs (info->arr_ref_first);
  1056. utype = TREE_TYPE (tidx);
  1057. /* (end of) block 0 */
  1058. gsi = gsi_for_stmt (info->arr_ref_first);
  1059. gsi_next (&gsi);
  1060. bound = fold_convert_loc (loc, utype, info->range_size);
  1061. cond_stmt = gimple_build_cond (LE_EXPR, tidx, bound, NULL_TREE, NULL_TREE);
  1062. gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
  1063. update_stmt (cond_stmt);
  1064. /* block 2 */
  1065. label2 = gimple_build_label (label_decl2);
  1066. gsi_insert_before (&gsi, label2, GSI_SAME_STMT);
  1067. last_assign = gen_def_assigns (&gsi, info);
  1068. /* block 1 */
  1069. label1 = gimple_build_label (label_decl1);
  1070. gsi_insert_before (&gsi, label1, GSI_SAME_STMT);
  1071. /* block F */
  1072. gsi = gsi_start_bb (info->final_bb);
  1073. label3 = gimple_build_label (label_decl3);
  1074. gsi_insert_before (&gsi, label3, GSI_SAME_STMT);
  1075. /* cfg fix */
  1076. e02 = split_block (bb0, cond_stmt);
  1077. bb2 = e02->dest;
  1078. e21 = split_block (bb2, last_assign);
  1079. bb1 = e21->dest;
  1080. remove_edge (e21);
  1081. e1d = split_block (bb1, info->arr_ref_last);
  1082. bbd = e1d->dest;
  1083. remove_edge (e1d);
  1084. /* flags and profiles of the edge for in-range values */
  1085. e01 = make_edge (bb0, bb1, EDGE_TRUE_VALUE);
  1086. e01->probability = REG_BR_PROB_BASE - info->default_prob;
  1087. e01->count = info->other_count;
  1088. /* flags and profiles of the edge taking care of out-of-range values */
  1089. e02->flags &= ~EDGE_FALLTHRU;
  1090. e02->flags |= EDGE_FALSE_VALUE;
  1091. e02->probability = info->default_prob;
  1092. e02->count = info->default_count;
  1093. bbf = info->final_bb;
  1094. e1f = make_edge (bb1, bbf, EDGE_FALLTHRU);
  1095. e1f->probability = REG_BR_PROB_BASE;
  1096. e1f->count = info->other_count;
  1097. e2f = make_edge (bb2, bbf, EDGE_FALLTHRU);
  1098. e2f->probability = REG_BR_PROB_BASE;
  1099. e2f->count = info->default_count;
  1100. /* frequencies of the new BBs */
  1101. bb1->frequency = EDGE_FREQUENCY (e01);
  1102. bb2->frequency = EDGE_FREQUENCY (e02);
  1103. bbf->frequency = EDGE_FREQUENCY (e1f) + EDGE_FREQUENCY (e2f);
  1104. /* Tidy blocks that have become unreachable. */
  1105. prune_bbs (bbd, info->final_bb);
  1106. /* Fixup the PHI nodes in bbF. */
  1107. fix_phi_nodes (e1f, e2f, bbf, info);
  1108. /* Fix the dominator tree, if it is available. */
  1109. if (dom_info_available_p (CDI_DOMINATORS))
  1110. {
  1111. vec<basic_block> bbs_to_fix_dom;
  1112. set_immediate_dominator (CDI_DOMINATORS, bb1, bb0);
  1113. set_immediate_dominator (CDI_DOMINATORS, bb2, bb0);
  1114. if (! get_immediate_dominator (CDI_DOMINATORS, bbf))
  1115. /* If bbD was the immediate dominator ... */
  1116. set_immediate_dominator (CDI_DOMINATORS, bbf, bb0);
  1117. bbs_to_fix_dom.create (4);
  1118. bbs_to_fix_dom.quick_push (bb0);
  1119. bbs_to_fix_dom.quick_push (bb1);
  1120. bbs_to_fix_dom.quick_push (bb2);
  1121. bbs_to_fix_dom.quick_push (bbf);
  1122. iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
  1123. bbs_to_fix_dom.release ();
  1124. }
  1125. }
  1126. /* The following function is invoked on every switch statement (the current one
  1127. is given in SWTCH) and runs the individual phases of switch conversion on it
  1128. one after another until one fails or the conversion is completed.
  1129. Returns NULL on success, or a pointer to a string with the reason why the
  1130. conversion failed. */
  1131. static const char *
  1132. process_switch (gswitch *swtch)
  1133. {
  1134. struct switch_conv_info info;
  1135. /* Group case labels so that we get the right results from the heuristics
  1136. that decide on the code generation approach for this switch. */
  1137. group_case_labels_stmt (swtch);
  1138. /* If this switch is now a degenerate case with only a default label,
  1139. there is nothing left for us to do. */
  1140. if (gimple_switch_num_labels (swtch) < 2)
  1141. return "switch is a degenerate case";
  1142. collect_switch_conv_info (swtch, &info);
  1143. /* No error markers should reach here (they should be filtered out
  1144. during gimplification). */
  1145. gcc_checking_assert (TREE_TYPE (info.index_expr) != error_mark_node);
  1146. /* A switch on a constant should have been optimized in tree-cfg-cleanup. */
  1147. gcc_checking_assert (! TREE_CONSTANT (info.index_expr));
  1148. if (info.uniq <= MAX_CASE_BIT_TESTS)
  1149. {
  1150. if (expand_switch_using_bit_tests_p (info.range_size,
  1151. info.uniq, info.count,
  1152. optimize_bb_for_speed_p
  1153. (gimple_bb (swtch))))
  1154. {
  1155. if (dump_file)
  1156. fputs (" expanding as bit test is preferable\n", dump_file);
  1157. emit_case_bit_tests (swtch, info.index_expr, info.range_min,
  1158. info.range_size, info.range_max);
  1159. loops_state_set (LOOPS_NEED_FIXUP);
  1160. return NULL;
  1161. }
  1162. if (info.uniq <= 2)
  1163. /* This will be expanded as a decision tree in stmt.c:expand_case. */
  1164. return " expanding as jumps is preferable";
  1165. }
  1166. /* If there is no common successor, we cannot do the transformation. */
  1167. if (! info.final_bb)
  1168. return "no common successor to all case label target blocks found";
  1169. /* Check the case label values are within reasonable range: */
  1170. if (!check_range (&info))
  1171. {
  1172. gcc_assert (info.reason);
  1173. return info.reason;
  1174. }
  1175. /* For all the cases, see whether they are empty, the assignments they
  1176. represent constant and so on... */
  1177. if (! check_all_empty_except_final (&info))
  1178. {
  1179. gcc_assert (info.reason);
  1180. return info.reason;
  1181. }
  1182. if (!check_final_bb (&info))
  1183. {
  1184. gcc_assert (info.reason);
  1185. return info.reason;
  1186. }
  1187. /* At this point all checks have passed and we can proceed with the
  1188. transformation. */
  1189. create_temp_arrays (&info);
  1190. gather_default_values (gimple_switch_default_label (swtch), &info);
  1191. build_constructors (swtch, &info);
  1192. build_arrays (swtch, &info); /* Build the static arrays and assignments. */
  1193. gen_inbound_check (swtch, &info); /* Build the bounds check. */
  1194. /* Cleanup: */
  1195. free_temp_arrays (&info);
  1196. return NULL;
  1197. }
  1198. /* The main function of the pass scans statements for switches and invokes
  1199. process_switch on them. */
  1200. namespace {
  1201. const pass_data pass_data_convert_switch =
  1202. {
  1203. GIMPLE_PASS, /* type */
  1204. "switchconv", /* name */
  1205. OPTGROUP_NONE, /* optinfo_flags */
  1206. TV_TREE_SWITCH_CONVERSION, /* tv_id */
  1207. ( PROP_cfg | PROP_ssa ), /* properties_required */
  1208. 0, /* properties_provided */
  1209. 0, /* properties_destroyed */
  1210. 0, /* todo_flags_start */
  1211. TODO_update_ssa, /* todo_flags_finish */
  1212. };
  1213. class pass_convert_switch : public gimple_opt_pass
  1214. {
  1215. public:
  1216. pass_convert_switch (gcc::context *ctxt)
  1217. : gimple_opt_pass (pass_data_convert_switch, ctxt)
  1218. {}
  1219. /* opt_pass methods: */
  1220. virtual bool gate (function *) { return flag_tree_switch_conversion != 0; }
  1221. virtual unsigned int execute (function *);
  1222. }; // class pass_convert_switch
  1223. unsigned int
  1224. pass_convert_switch::execute (function *fun)
  1225. {
  1226. basic_block bb;
  1227. FOR_EACH_BB_FN (bb, fun)
  1228. {
  1229. const char *failure_reason;
  1230. gimple stmt = last_stmt (bb);
  1231. if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
  1232. {
  1233. if (dump_file)
  1234. {
  1235. expanded_location loc = expand_location (gimple_location (stmt));
  1236. fprintf (dump_file, "beginning to process the following "
  1237. "SWITCH statement (%s:%d) : ------- \n",
  1238. loc.file, loc.line);
  1239. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  1240. putc ('\n', dump_file);
  1241. }
  1242. failure_reason = process_switch (as_a <gswitch *> (stmt));
  1243. if (! failure_reason)
  1244. {
  1245. if (dump_file)
  1246. {
  1247. fputs ("Switch converted\n", dump_file);
  1248. fputs ("--------------------------------\n", dump_file);
  1249. }
  1250. /* Make no effort to update the post-dominator tree. It is actually not
  1251. that hard for the transformations we have performed, but it is not
  1252. supported by iterate_fix_dominators. */
  1253. free_dominance_info (CDI_POST_DOMINATORS);
  1254. }
  1255. else
  1256. {
  1257. if (dump_file)
  1258. {
  1259. fputs ("Bailing out - ", dump_file);
  1260. fputs (failure_reason, dump_file);
  1261. fputs ("\n--------------------------------\n", dump_file);
  1262. }
  1263. }
  1264. }
  1265. }
  1266. return 0;
  1267. }
  1268. } // anon namespace
  1269. gimple_opt_pass *
  1270. make_pass_convert_switch (gcc::context *ctxt)
  1271. {
  1272. return new pass_convert_switch (ctxt);
  1273. }