stmt.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /* Expands front end tree to back end RTL for GCC
  2. Copyright (C) 1987-2015 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. /* This file handles the generation of rtl code from tree structure
  16. above the level of expressions, using subroutines in exp*.c and emit-rtl.c.
  17. The functions whose names start with `expand_' are called by the
  18. expander to generate RTL instructions for various kinds of constructs. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "tm.h"
  23. #include "rtl.h"
  24. #include "hard-reg-set.h"
  25. #include "hash-set.h"
  26. #include "machmode.h"
  27. #include "vec.h"
  28. #include "double-int.h"
  29. #include "input.h"
  30. #include "alias.h"
  31. #include "symtab.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "fold-const.h"
  36. #include "varasm.h"
  37. #include "stor-layout.h"
  38. #include "tm_p.h"
  39. #include "flags.h"
  40. #include "except.h"
  41. #include "function.h"
  42. #include "insn-config.h"
  43. #include "hashtab.h"
  44. #include "statistics.h"
  45. #include "real.h"
  46. #include "fixed-value.h"
  47. #include "expmed.h"
  48. #include "dojump.h"
  49. #include "explow.h"
  50. #include "calls.h"
  51. #include "emit-rtl.h"
  52. #include "stmt.h"
  53. #include "expr.h"
  54. #include "libfuncs.h"
  55. #include "recog.h"
  56. #include "diagnostic-core.h"
  57. #include "output.h"
  58. #include "langhooks.h"
  59. #include "predict.h"
  60. #include "insn-codes.h"
  61. #include "optabs.h"
  62. #include "target.h"
  63. #include "cfganal.h"
  64. #include "basic-block.h"
  65. #include "tree-ssa-alias.h"
  66. #include "internal-fn.h"
  67. #include "gimple-expr.h"
  68. #include "is-a.h"
  69. #include "gimple.h"
  70. #include "regs.h"
  71. #include "alloc-pool.h"
  72. #include "pretty-print.h"
  73. #include "params.h"
  74. #include "dumpfile.h"
  75. #include "builtins.h"
  76. /* Functions and data structures for expanding case statements. */
  77. /* Case label structure, used to hold info on labels within case
  78. statements. We handle "range" labels; for a single-value label
  79. as in C, the high and low limits are the same.
  80. We start with a vector of case nodes sorted in ascending order, and
  81. the default label as the last element in the vector. Before expanding
  82. to RTL, we transform this vector into a list linked via the RIGHT
  83. fields in the case_node struct. Nodes with higher case values are
  84. later in the list.
  85. Switch statements can be output in three forms. A branch table is
  86. used if there are more than a few labels and the labels are dense
  87. within the range between the smallest and largest case value. If a
  88. branch table is used, no further manipulations are done with the case
  89. node chain.
  90. The alternative to the use of a branch table is to generate a series
  91. of compare and jump insns. When that is done, we use the LEFT, RIGHT,
  92. and PARENT fields to hold a binary tree. Initially the tree is
  93. totally unbalanced, with everything on the right. We balance the tree
  94. with nodes on the left having lower case values than the parent
  95. and nodes on the right having higher values. We then output the tree
  96. in order.
  97. For very small, suitable switch statements, we can generate a series
  98. of simple bit test and branches instead. */
  99. struct case_node
  100. {
  101. struct case_node *left; /* Left son in binary tree */
  102. struct case_node *right; /* Right son in binary tree; also node chain */
  103. struct case_node *parent; /* Parent of node in binary tree */
  104. tree low; /* Lowest index value for this label */
  105. tree high; /* Highest index value for this label */
  106. tree code_label; /* Label to jump to when node matches */
  107. int prob; /* Probability of taking this case. */
  108. /* Probability of reaching subtree rooted at this node */
  109. int subtree_prob;
  110. };
  111. typedef struct case_node case_node;
  112. typedef struct case_node *case_node_ptr;
  113. extern basic_block label_to_block_fn (struct function *, tree);
  114. static bool check_unique_operand_names (tree, tree, tree);
  115. static char *resolve_operand_name_1 (char *, tree, tree, tree);
  116. static void balance_case_nodes (case_node_ptr *, case_node_ptr);
  117. static int node_has_low_bound (case_node_ptr, tree);
  118. static int node_has_high_bound (case_node_ptr, tree);
  119. static int node_is_bounded (case_node_ptr, tree);
  120. static void emit_case_nodes (rtx, case_node_ptr, rtx, int, tree);
  121. /* Return the rtx-label that corresponds to a LABEL_DECL,
  122. creating it if necessary. */
  123. rtx
  124. label_rtx (tree label)
  125. {
  126. gcc_assert (TREE_CODE (label) == LABEL_DECL);
  127. if (!DECL_RTL_SET_P (label))
  128. {
  129. rtx_code_label *r = gen_label_rtx ();
  130. SET_DECL_RTL (label, r);
  131. if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
  132. LABEL_PRESERVE_P (r) = 1;
  133. }
  134. return DECL_RTL (label);
  135. }
  136. /* As above, but also put it on the forced-reference list of the
  137. function that contains it. */
  138. rtx
  139. force_label_rtx (tree label)
  140. {
  141. rtx_insn *ref = as_a <rtx_insn *> (label_rtx (label));
  142. tree function = decl_function_context (label);
  143. gcc_assert (function);
  144. forced_labels = gen_rtx_INSN_LIST (VOIDmode, ref, forced_labels);
  145. return ref;
  146. }
  147. /* Add an unconditional jump to LABEL as the next sequential instruction. */
  148. void
  149. emit_jump (rtx label)
  150. {
  151. do_pending_stack_adjust ();
  152. emit_jump_insn (gen_jump (label));
  153. emit_barrier ();
  154. }
  155. /* Handle goto statements and the labels that they can go to. */
  156. /* Specify the location in the RTL code of a label LABEL,
  157. which is a LABEL_DECL tree node.
  158. This is used for the kind of label that the user can jump to with a
  159. goto statement, and for alternatives of a switch or case statement.
  160. RTL labels generated for loops and conditionals don't go through here;
  161. they are generated directly at the RTL level, by other functions below.
  162. Note that this has nothing to do with defining label *names*.
  163. Languages vary in how they do that and what that even means. */
  164. void
  165. expand_label (tree label)
  166. {
  167. rtx_insn *label_r = as_a <rtx_insn *> (label_rtx (label));
  168. do_pending_stack_adjust ();
  169. emit_label (label_r);
  170. if (DECL_NAME (label))
  171. LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
  172. if (DECL_NONLOCAL (label))
  173. {
  174. expand_builtin_setjmp_receiver (NULL);
  175. nonlocal_goto_handler_labels
  176. = gen_rtx_INSN_LIST (VOIDmode, label_r,
  177. nonlocal_goto_handler_labels);
  178. }
  179. if (FORCED_LABEL (label))
  180. forced_labels = gen_rtx_INSN_LIST (VOIDmode, label_r, forced_labels);
  181. if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
  182. maybe_set_first_label_num (label_r);
  183. }
  184. /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
  185. OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
  186. inputs and NOUTPUTS outputs to this extended-asm. Upon return,
  187. *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
  188. memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
  189. constraint allows the use of a register operand. And, *IS_INOUT
  190. will be true if the operand is read-write, i.e., if it is used as
  191. an input as well as an output. If *CONSTRAINT_P is not in
  192. canonical form, it will be made canonical. (Note that `+' will be
  193. replaced with `=' as part of this process.)
  194. Returns TRUE if all went well; FALSE if an error occurred. */
  195. bool
  196. parse_output_constraint (const char **constraint_p, int operand_num,
  197. int ninputs, int noutputs, bool *allows_mem,
  198. bool *allows_reg, bool *is_inout)
  199. {
  200. const char *constraint = *constraint_p;
  201. const char *p;
  202. /* Assume the constraint doesn't allow the use of either a register
  203. or memory. */
  204. *allows_mem = false;
  205. *allows_reg = false;
  206. /* Allow the `=' or `+' to not be at the beginning of the string,
  207. since it wasn't explicitly documented that way, and there is a
  208. large body of code that puts it last. Swap the character to
  209. the front, so as not to uglify any place else. */
  210. p = strchr (constraint, '=');
  211. if (!p)
  212. p = strchr (constraint, '+');
  213. /* If the string doesn't contain an `=', issue an error
  214. message. */
  215. if (!p)
  216. {
  217. error ("output operand constraint lacks %<=%>");
  218. return false;
  219. }
  220. /* If the constraint begins with `+', then the operand is both read
  221. from and written to. */
  222. *is_inout = (*p == '+');
  223. /* Canonicalize the output constraint so that it begins with `='. */
  224. if (p != constraint || *is_inout)
  225. {
  226. char *buf;
  227. size_t c_len = strlen (constraint);
  228. if (p != constraint)
  229. warning (0, "output constraint %qc for operand %d "
  230. "is not at the beginning",
  231. *p, operand_num);
  232. /* Make a copy of the constraint. */
  233. buf = XALLOCAVEC (char, c_len + 1);
  234. strcpy (buf, constraint);
  235. /* Swap the first character and the `=' or `+'. */
  236. buf[p - constraint] = buf[0];
  237. /* Make sure the first character is an `='. (Until we do this,
  238. it might be a `+'.) */
  239. buf[0] = '=';
  240. /* Replace the constraint with the canonicalized string. */
  241. *constraint_p = ggc_alloc_string (buf, c_len);
  242. constraint = *constraint_p;
  243. }
  244. /* Loop through the constraint string. */
  245. for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
  246. switch (*p)
  247. {
  248. case '+':
  249. case '=':
  250. error ("operand constraint contains incorrectly positioned "
  251. "%<+%> or %<=%>");
  252. return false;
  253. case '%':
  254. if (operand_num + 1 == ninputs + noutputs)
  255. {
  256. error ("%<%%%> constraint used with last operand");
  257. return false;
  258. }
  259. break;
  260. case '?': case '!': case '*': case '&': case '#':
  261. case '$': case '^':
  262. case 'E': case 'F': case 'G': case 'H':
  263. case 's': case 'i': case 'n':
  264. case 'I': case 'J': case 'K': case 'L': case 'M':
  265. case 'N': case 'O': case 'P': case ',':
  266. break;
  267. case '0': case '1': case '2': case '3': case '4':
  268. case '5': case '6': case '7': case '8': case '9':
  269. case '[':
  270. error ("matching constraint not valid in output operand");
  271. return false;
  272. case '<': case '>':
  273. /* ??? Before flow, auto inc/dec insns are not supposed to exist,
  274. excepting those that expand_call created. So match memory
  275. and hope. */
  276. *allows_mem = true;
  277. break;
  278. case 'g': case 'X':
  279. *allows_reg = true;
  280. *allows_mem = true;
  281. break;
  282. default:
  283. if (!ISALPHA (*p))
  284. break;
  285. enum constraint_num cn = lookup_constraint (p);
  286. if (reg_class_for_constraint (cn) != NO_REGS
  287. || insn_extra_address_constraint (cn))
  288. *allows_reg = true;
  289. else if (insn_extra_memory_constraint (cn))
  290. *allows_mem = true;
  291. else
  292. {
  293. /* Otherwise we can't assume anything about the nature of
  294. the constraint except that it isn't purely registers.
  295. Treat it like "g" and hope for the best. */
  296. *allows_reg = true;
  297. *allows_mem = true;
  298. }
  299. break;
  300. }
  301. return true;
  302. }
  303. /* Similar, but for input constraints. */
  304. bool
  305. parse_input_constraint (const char **constraint_p, int input_num,
  306. int ninputs, int noutputs, int ninout,
  307. const char * const * constraints,
  308. bool *allows_mem, bool *allows_reg)
  309. {
  310. const char *constraint = *constraint_p;
  311. const char *orig_constraint = constraint;
  312. size_t c_len = strlen (constraint);
  313. size_t j;
  314. bool saw_match = false;
  315. /* Assume the constraint doesn't allow the use of either
  316. a register or memory. */
  317. *allows_mem = false;
  318. *allows_reg = false;
  319. /* Make sure constraint has neither `=', `+', nor '&'. */
  320. for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
  321. switch (constraint[j])
  322. {
  323. case '+': case '=': case '&':
  324. if (constraint == orig_constraint)
  325. {
  326. error ("input operand constraint contains %qc", constraint[j]);
  327. return false;
  328. }
  329. break;
  330. case '%':
  331. if (constraint == orig_constraint
  332. && input_num + 1 == ninputs - ninout)
  333. {
  334. error ("%<%%%> constraint used with last operand");
  335. return false;
  336. }
  337. break;
  338. case '<': case '>':
  339. case '?': case '!': case '*': case '#':
  340. case '$': case '^':
  341. case 'E': case 'F': case 'G': case 'H':
  342. case 's': case 'i': case 'n':
  343. case 'I': case 'J': case 'K': case 'L': case 'M':
  344. case 'N': case 'O': case 'P': case ',':
  345. break;
  346. /* Whether or not a numeric constraint allows a register is
  347. decided by the matching constraint, and so there is no need
  348. to do anything special with them. We must handle them in
  349. the default case, so that we don't unnecessarily force
  350. operands to memory. */
  351. case '0': case '1': case '2': case '3': case '4':
  352. case '5': case '6': case '7': case '8': case '9':
  353. {
  354. char *end;
  355. unsigned long match;
  356. saw_match = true;
  357. match = strtoul (constraint + j, &end, 10);
  358. if (match >= (unsigned long) noutputs)
  359. {
  360. error ("matching constraint references invalid operand number");
  361. return false;
  362. }
  363. /* Try and find the real constraint for this dup. Only do this
  364. if the matching constraint is the only alternative. */
  365. if (*end == '\0'
  366. && (j == 0 || (j == 1 && constraint[0] == '%')))
  367. {
  368. constraint = constraints[match];
  369. *constraint_p = constraint;
  370. c_len = strlen (constraint);
  371. j = 0;
  372. /* ??? At the end of the loop, we will skip the first part of
  373. the matched constraint. This assumes not only that the
  374. other constraint is an output constraint, but also that
  375. the '=' or '+' come first. */
  376. break;
  377. }
  378. else
  379. j = end - constraint;
  380. /* Anticipate increment at end of loop. */
  381. j--;
  382. }
  383. /* Fall through. */
  384. case 'g': case 'X':
  385. *allows_reg = true;
  386. *allows_mem = true;
  387. break;
  388. default:
  389. if (! ISALPHA (constraint[j]))
  390. {
  391. error ("invalid punctuation %qc in constraint", constraint[j]);
  392. return false;
  393. }
  394. enum constraint_num cn = lookup_constraint (constraint + j);
  395. if (reg_class_for_constraint (cn) != NO_REGS
  396. || insn_extra_address_constraint (cn))
  397. *allows_reg = true;
  398. else if (insn_extra_memory_constraint (cn))
  399. *allows_mem = true;
  400. else
  401. {
  402. /* Otherwise we can't assume anything about the nature of
  403. the constraint except that it isn't purely registers.
  404. Treat it like "g" and hope for the best. */
  405. *allows_reg = true;
  406. *allows_mem = true;
  407. }
  408. break;
  409. }
  410. if (saw_match && !*allows_reg)
  411. warning (0, "matching constraint does not allow a register");
  412. return true;
  413. }
  414. /* Return DECL iff there's an overlap between *REGS and DECL, where DECL
  415. can be an asm-declared register. Called via walk_tree. */
  416. static tree
  417. decl_overlaps_hard_reg_set_p (tree *declp, int *walk_subtrees ATTRIBUTE_UNUSED,
  418. void *data)
  419. {
  420. tree decl = *declp;
  421. const HARD_REG_SET *const regs = (const HARD_REG_SET *) data;
  422. if (TREE_CODE (decl) == VAR_DECL)
  423. {
  424. if (DECL_HARD_REGISTER (decl)
  425. && REG_P (DECL_RTL (decl))
  426. && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
  427. {
  428. rtx reg = DECL_RTL (decl);
  429. if (overlaps_hard_reg_set_p (*regs, GET_MODE (reg), REGNO (reg)))
  430. return decl;
  431. }
  432. walk_subtrees = 0;
  433. }
  434. else if (TYPE_P (decl) || TREE_CODE (decl) == PARM_DECL)
  435. walk_subtrees = 0;
  436. return NULL_TREE;
  437. }
  438. /* If there is an overlap between *REGS and DECL, return the first overlap
  439. found. */
  440. tree
  441. tree_overlaps_hard_reg_set (tree decl, HARD_REG_SET *regs)
  442. {
  443. return walk_tree (&decl, decl_overlaps_hard_reg_set_p, regs, NULL);
  444. }
  445. /* A subroutine of expand_asm_operands. Check that all operand names
  446. are unique. Return true if so. We rely on the fact that these names
  447. are identifiers, and so have been canonicalized by get_identifier,
  448. so all we need are pointer comparisons. */
  449. static bool
  450. check_unique_operand_names (tree outputs, tree inputs, tree labels)
  451. {
  452. tree i, j, i_name = NULL_TREE;
  453. for (i = outputs; i ; i = TREE_CHAIN (i))
  454. {
  455. i_name = TREE_PURPOSE (TREE_PURPOSE (i));
  456. if (! i_name)
  457. continue;
  458. for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
  459. if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
  460. goto failure;
  461. }
  462. for (i = inputs; i ; i = TREE_CHAIN (i))
  463. {
  464. i_name = TREE_PURPOSE (TREE_PURPOSE (i));
  465. if (! i_name)
  466. continue;
  467. for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
  468. if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
  469. goto failure;
  470. for (j = outputs; j ; j = TREE_CHAIN (j))
  471. if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
  472. goto failure;
  473. }
  474. for (i = labels; i ; i = TREE_CHAIN (i))
  475. {
  476. i_name = TREE_PURPOSE (i);
  477. if (! i_name)
  478. continue;
  479. for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
  480. if (simple_cst_equal (i_name, TREE_PURPOSE (j)))
  481. goto failure;
  482. for (j = inputs; j ; j = TREE_CHAIN (j))
  483. if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
  484. goto failure;
  485. }
  486. return true;
  487. failure:
  488. error ("duplicate asm operand name %qs", TREE_STRING_POINTER (i_name));
  489. return false;
  490. }
  491. /* A subroutine of expand_asm_operands. Resolve the names of the operands
  492. in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
  493. STRING and in the constraints to those numbers. */
  494. tree
  495. resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
  496. {
  497. char *buffer;
  498. char *p;
  499. const char *c;
  500. tree t;
  501. check_unique_operand_names (outputs, inputs, labels);
  502. /* Substitute [<name>] in input constraint strings. There should be no
  503. named operands in output constraints. */
  504. for (t = inputs; t ; t = TREE_CHAIN (t))
  505. {
  506. c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
  507. if (strchr (c, '[') != NULL)
  508. {
  509. p = buffer = xstrdup (c);
  510. while ((p = strchr (p, '[')) != NULL)
  511. p = resolve_operand_name_1 (p, outputs, inputs, NULL);
  512. TREE_VALUE (TREE_PURPOSE (t))
  513. = build_string (strlen (buffer), buffer);
  514. free (buffer);
  515. }
  516. }
  517. /* Now check for any needed substitutions in the template. */
  518. c = TREE_STRING_POINTER (string);
  519. while ((c = strchr (c, '%')) != NULL)
  520. {
  521. if (c[1] == '[')
  522. break;
  523. else if (ISALPHA (c[1]) && c[2] == '[')
  524. break;
  525. else
  526. {
  527. c += 1 + (c[1] == '%');
  528. continue;
  529. }
  530. }
  531. if (c)
  532. {
  533. /* OK, we need to make a copy so we can perform the substitutions.
  534. Assume that we will not need extra space--we get to remove '['
  535. and ']', which means we cannot have a problem until we have more
  536. than 999 operands. */
  537. buffer = xstrdup (TREE_STRING_POINTER (string));
  538. p = buffer + (c - TREE_STRING_POINTER (string));
  539. while ((p = strchr (p, '%')) != NULL)
  540. {
  541. if (p[1] == '[')
  542. p += 1;
  543. else if (ISALPHA (p[1]) && p[2] == '[')
  544. p += 2;
  545. else
  546. {
  547. p += 1 + (p[1] == '%');
  548. continue;
  549. }
  550. p = resolve_operand_name_1 (p, outputs, inputs, labels);
  551. }
  552. string = build_string (strlen (buffer), buffer);
  553. free (buffer);
  554. }
  555. return string;
  556. }
  557. /* A subroutine of resolve_operand_names. P points to the '[' for a
  558. potential named operand of the form [<name>]. In place, replace
  559. the name and brackets with a number. Return a pointer to the
  560. balance of the string after substitution. */
  561. static char *
  562. resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels)
  563. {
  564. char *q;
  565. int op;
  566. tree t;
  567. /* Collect the operand name. */
  568. q = strchr (++p, ']');
  569. if (!q)
  570. {
  571. error ("missing close brace for named operand");
  572. return strchr (p, '\0');
  573. }
  574. *q = '\0';
  575. /* Resolve the name to a number. */
  576. for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
  577. {
  578. tree name = TREE_PURPOSE (TREE_PURPOSE (t));
  579. if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
  580. goto found;
  581. }
  582. for (t = inputs; t ; t = TREE_CHAIN (t), op++)
  583. {
  584. tree name = TREE_PURPOSE (TREE_PURPOSE (t));
  585. if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
  586. goto found;
  587. }
  588. for (t = labels; t ; t = TREE_CHAIN (t), op++)
  589. {
  590. tree name = TREE_PURPOSE (t);
  591. if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
  592. goto found;
  593. }
  594. error ("undefined named operand %qs", identifier_to_locale (p));
  595. op = 0;
  596. found:
  597. /* Replace the name with the number. Unfortunately, not all libraries
  598. get the return value of sprintf correct, so search for the end of the
  599. generated string by hand. */
  600. sprintf (--p, "%d", op);
  601. p = strchr (p, '\0');
  602. /* Verify the no extra buffer space assumption. */
  603. gcc_assert (p <= q);
  604. /* Shift the rest of the buffer down to fill the gap. */
  605. memmove (p, q + 1, strlen (q + 1) + 1);
  606. return p;
  607. }
  608. /* Generate RTL to return directly from the current function.
  609. (That is, we bypass any return value.) */
  610. void
  611. expand_naked_return (void)
  612. {
  613. rtx end_label;
  614. clear_pending_stack_adjust ();
  615. do_pending_stack_adjust ();
  616. end_label = naked_return_label;
  617. if (end_label == 0)
  618. end_label = naked_return_label = gen_label_rtx ();
  619. emit_jump (end_label);
  620. }
  621. /* Generate code to jump to LABEL if OP0 and OP1 are equal in mode MODE. PROB
  622. is the probability of jumping to LABEL. */
  623. static void
  624. do_jump_if_equal (machine_mode mode, rtx op0, rtx op1, rtx label,
  625. int unsignedp, int prob)
  626. {
  627. gcc_assert (prob <= REG_BR_PROB_BASE);
  628. do_compare_rtx_and_jump (op0, op1, EQ, unsignedp, mode,
  629. NULL_RTX, NULL_RTX, label, prob);
  630. }
  631. /* Do the insertion of a case label into case_list. The labels are
  632. fed to us in descending order from the sorted vector of case labels used
  633. in the tree part of the middle end. So the list we construct is
  634. sorted in ascending order.
  635. LABEL is the case label to be inserted. LOW and HIGH are the bounds
  636. against which the index is compared to jump to LABEL and PROB is the
  637. estimated probability LABEL is reached from the switch statement. */
  638. static struct case_node *
  639. add_case_node (struct case_node *head, tree low, tree high,
  640. tree label, int prob, alloc_pool case_node_pool)
  641. {
  642. struct case_node *r;
  643. gcc_checking_assert (low);
  644. gcc_checking_assert (high && (TREE_TYPE (low) == TREE_TYPE (high)));
  645. /* Add this label to the chain. */
  646. r = (struct case_node *) pool_alloc (case_node_pool);
  647. r->low = low;
  648. r->high = high;
  649. r->code_label = label;
  650. r->parent = r->left = NULL;
  651. r->prob = prob;
  652. r->subtree_prob = prob;
  653. r->right = head;
  654. return r;
  655. }
  656. /* Dump ROOT, a list or tree of case nodes, to file. */
  657. static void
  658. dump_case_nodes (FILE *f, struct case_node *root,
  659. int indent_step, int indent_level)
  660. {
  661. if (root == 0)
  662. return;
  663. indent_level++;
  664. dump_case_nodes (f, root->left, indent_step, indent_level);
  665. fputs (";; ", f);
  666. fprintf (f, "%*s", indent_step * indent_level, "");
  667. print_dec (root->low, f, TYPE_SIGN (TREE_TYPE (root->low)));
  668. if (!tree_int_cst_equal (root->low, root->high))
  669. {
  670. fprintf (f, " ... ");
  671. print_dec (root->high, f, TYPE_SIGN (TREE_TYPE (root->high)));
  672. }
  673. fputs ("\n", f);
  674. dump_case_nodes (f, root->right, indent_step, indent_level);
  675. }
  676. #ifndef HAVE_casesi
  677. #define HAVE_casesi 0
  678. #endif
  679. #ifndef HAVE_tablejump
  680. #define HAVE_tablejump 0
  681. #endif
  682. /* Return the smallest number of different values for which it is best to use a
  683. jump-table instead of a tree of conditional branches. */
  684. static unsigned int
  685. case_values_threshold (void)
  686. {
  687. unsigned int threshold = PARAM_VALUE (PARAM_CASE_VALUES_THRESHOLD);
  688. if (threshold == 0)
  689. threshold = targetm.case_values_threshold ();
  690. return threshold;
  691. }
  692. /* Return true if a switch should be expanded as a decision tree.
  693. RANGE is the difference between highest and lowest case.
  694. UNIQ is number of unique case node targets, not counting the default case.
  695. COUNT is the number of comparisons needed, not counting the default case. */
  696. static bool
  697. expand_switch_as_decision_tree_p (tree range,
  698. unsigned int uniq ATTRIBUTE_UNUSED,
  699. unsigned int count)
  700. {
  701. int max_ratio;
  702. /* If neither casesi or tablejump is available, or flag_jump_tables
  703. over-ruled us, we really have no choice. */
  704. if (!HAVE_casesi && !HAVE_tablejump)
  705. return true;
  706. if (!flag_jump_tables)
  707. return true;
  708. #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
  709. if (flag_pic)
  710. return true;
  711. #endif
  712. /* If the switch is relatively small such that the cost of one
  713. indirect jump on the target are higher than the cost of a
  714. decision tree, go with the decision tree.
  715. If range of values is much bigger than number of values,
  716. or if it is too large to represent in a HOST_WIDE_INT,
  717. make a sequence of conditional branches instead of a dispatch.
  718. The definition of "much bigger" depends on whether we are
  719. optimizing for size or for speed. If the former, the maximum
  720. ratio range/count = 3, because this was found to be the optimal
  721. ratio for size on i686-pc-linux-gnu, see PR11823. The ratio
  722. 10 is much older, and was probably selected after an extensive
  723. benchmarking investigation on numerous platforms. Or maybe it
  724. just made sense to someone at some point in the history of GCC,
  725. who knows... */
  726. max_ratio = optimize_insn_for_size_p () ? 3 : 10;
  727. if (count < case_values_threshold ()
  728. || ! tree_fits_uhwi_p (range)
  729. || compare_tree_int (range, max_ratio * count) > 0)
  730. return true;
  731. return false;
  732. }
  733. /* Generate a decision tree, switching on INDEX_EXPR and jumping to
  734. one of the labels in CASE_LIST or to the DEFAULT_LABEL.
  735. DEFAULT_PROB is the estimated probability that it jumps to
  736. DEFAULT_LABEL.
  737. We generate a binary decision tree to select the appropriate target
  738. code. This is done as follows:
  739. If the index is a short or char that we do not have
  740. an insn to handle comparisons directly, convert it to
  741. a full integer now, rather than letting each comparison
  742. generate the conversion.
  743. Load the index into a register.
  744. The list of cases is rearranged into a binary tree,
  745. nearly optimal assuming equal probability for each case.
  746. The tree is transformed into RTL, eliminating redundant
  747. test conditions at the same time.
  748. If program flow could reach the end of the decision tree
  749. an unconditional jump to the default code is emitted.
  750. The above process is unaware of the CFG. The caller has to fix up
  751. the CFG itself. This is done in cfgexpand.c. */
  752. static void
  753. emit_case_decision_tree (tree index_expr, tree index_type,
  754. struct case_node *case_list, rtx default_label,
  755. int default_prob)
  756. {
  757. rtx index = expand_normal (index_expr);
  758. if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
  759. && ! have_insn_for (COMPARE, GET_MODE (index)))
  760. {
  761. int unsignedp = TYPE_UNSIGNED (index_type);
  762. machine_mode wider_mode;
  763. for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
  764. wider_mode = GET_MODE_WIDER_MODE (wider_mode))
  765. if (have_insn_for (COMPARE, wider_mode))
  766. {
  767. index = convert_to_mode (wider_mode, index, unsignedp);
  768. break;
  769. }
  770. }
  771. do_pending_stack_adjust ();
  772. if (MEM_P (index))
  773. {
  774. index = copy_to_reg (index);
  775. if (TREE_CODE (index_expr) == SSA_NAME)
  776. set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (index_expr), index);
  777. }
  778. balance_case_nodes (&case_list, NULL);
  779. if (dump_file && (dump_flags & TDF_DETAILS))
  780. {
  781. int indent_step = ceil_log2 (TYPE_PRECISION (index_type)) + 2;
  782. fprintf (dump_file, ";; Expanding GIMPLE switch as decision tree:\n");
  783. dump_case_nodes (dump_file, case_list, indent_step, 0);
  784. }
  785. emit_case_nodes (index, case_list, default_label, default_prob, index_type);
  786. if (default_label)
  787. emit_jump (default_label);
  788. }
  789. /* Return the sum of probabilities of outgoing edges of basic block BB. */
  790. static int
  791. get_outgoing_edge_probs (basic_block bb)
  792. {
  793. edge e;
  794. edge_iterator ei;
  795. int prob_sum = 0;
  796. if (!bb)
  797. return 0;
  798. FOR_EACH_EDGE (e, ei, bb->succs)
  799. prob_sum += e->probability;
  800. return prob_sum;
  801. }
  802. /* Computes the conditional probability of jumping to a target if the branch
  803. instruction is executed.
  804. TARGET_PROB is the estimated probability of jumping to a target relative
  805. to some basic block BB.
  806. BASE_PROB is the probability of reaching the branch instruction relative
  807. to the same basic block BB. */
  808. static inline int
  809. conditional_probability (int target_prob, int base_prob)
  810. {
  811. if (base_prob > 0)
  812. {
  813. gcc_assert (target_prob >= 0);
  814. gcc_assert (target_prob <= base_prob);
  815. return GCOV_COMPUTE_SCALE (target_prob, base_prob);
  816. }
  817. return -1;
  818. }
  819. /* Generate a dispatch tabler, switching on INDEX_EXPR and jumping to
  820. one of the labels in CASE_LIST or to the DEFAULT_LABEL.
  821. MINVAL, MAXVAL, and RANGE are the extrema and range of the case
  822. labels in CASE_LIST. STMT_BB is the basic block containing the statement.
  823. First, a jump insn is emitted. First we try "casesi". If that
  824. fails, try "tablejump". A target *must* have one of them (or both).
  825. Then, a table with the target labels is emitted.
  826. The process is unaware of the CFG. The caller has to fix up
  827. the CFG itself. This is done in cfgexpand.c. */
  828. static void
  829. emit_case_dispatch_table (tree index_expr, tree index_type,
  830. struct case_node *case_list, rtx default_label,
  831. tree minval, tree maxval, tree range,
  832. basic_block stmt_bb)
  833. {
  834. int i, ncases;
  835. struct case_node *n;
  836. rtx *labelvec;
  837. rtx fallback_label = label_rtx (case_list->code_label);
  838. rtx_code_label *table_label = gen_label_rtx ();
  839. bool has_gaps = false;
  840. edge default_edge = stmt_bb ? EDGE_SUCC (stmt_bb, 0) : NULL;
  841. int default_prob = default_edge ? default_edge->probability : 0;
  842. int base = get_outgoing_edge_probs (stmt_bb);
  843. bool try_with_tablejump = false;
  844. int new_default_prob = conditional_probability (default_prob,
  845. base);
  846. if (! try_casesi (index_type, index_expr, minval, range,
  847. table_label, default_label, fallback_label,
  848. new_default_prob))
  849. {
  850. /* Index jumptables from zero for suitable values of minval to avoid
  851. a subtraction. For the rationale see:
  852. "http://gcc.gnu.org/ml/gcc-patches/2001-10/msg01234.html". */
  853. if (optimize_insn_for_speed_p ()
  854. && compare_tree_int (minval, 0) > 0
  855. && compare_tree_int (minval, 3) < 0)
  856. {
  857. minval = build_int_cst (index_type, 0);
  858. range = maxval;
  859. has_gaps = true;
  860. }
  861. try_with_tablejump = true;
  862. }
  863. /* Get table of labels to jump to, in order of case index. */
  864. ncases = tree_to_shwi (range) + 1;
  865. labelvec = XALLOCAVEC (rtx, ncases);
  866. memset (labelvec, 0, ncases * sizeof (rtx));
  867. for (n = case_list; n; n = n->right)
  868. {
  869. /* Compute the low and high bounds relative to the minimum
  870. value since that should fit in a HOST_WIDE_INT while the
  871. actual values may not. */
  872. HOST_WIDE_INT i_low
  873. = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
  874. n->low, minval));
  875. HOST_WIDE_INT i_high
  876. = tree_to_uhwi (fold_build2 (MINUS_EXPR, index_type,
  877. n->high, minval));
  878. HOST_WIDE_INT i;
  879. for (i = i_low; i <= i_high; i ++)
  880. labelvec[i]
  881. = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
  882. }
  883. /* Fill in the gaps with the default. We may have gaps at
  884. the beginning if we tried to avoid the minval subtraction,
  885. so substitute some label even if the default label was
  886. deemed unreachable. */
  887. if (!default_label)
  888. default_label = fallback_label;
  889. for (i = 0; i < ncases; i++)
  890. if (labelvec[i] == 0)
  891. {
  892. has_gaps = true;
  893. labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
  894. }
  895. if (has_gaps)
  896. {
  897. /* There is at least one entry in the jump table that jumps
  898. to default label. The default label can either be reached
  899. through the indirect jump or the direct conditional jump
  900. before that. Split the probability of reaching the
  901. default label among these two jumps. */
  902. new_default_prob = conditional_probability (default_prob/2,
  903. base);
  904. default_prob /= 2;
  905. base -= default_prob;
  906. }
  907. else
  908. {
  909. base -= default_prob;
  910. default_prob = 0;
  911. }
  912. if (default_edge)
  913. default_edge->probability = default_prob;
  914. /* We have altered the probability of the default edge. So the probabilities
  915. of all other edges need to be adjusted so that it sums up to
  916. REG_BR_PROB_BASE. */
  917. if (base)
  918. {
  919. edge e;
  920. edge_iterator ei;
  921. FOR_EACH_EDGE (e, ei, stmt_bb->succs)
  922. e->probability = GCOV_COMPUTE_SCALE (e->probability, base);
  923. }
  924. if (try_with_tablejump)
  925. {
  926. bool ok = try_tablejump (index_type, index_expr, minval, range,
  927. table_label, default_label, new_default_prob);
  928. gcc_assert (ok);
  929. }
  930. /* Output the table. */
  931. emit_label (table_label);
  932. if (CASE_VECTOR_PC_RELATIVE || flag_pic)
  933. emit_jump_table_data (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
  934. gen_rtx_LABEL_REF (Pmode,
  935. table_label),
  936. gen_rtvec_v (ncases, labelvec),
  937. const0_rtx, const0_rtx));
  938. else
  939. emit_jump_table_data (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
  940. gen_rtvec_v (ncases, labelvec)));
  941. /* Record no drop-through after the table. */
  942. emit_barrier ();
  943. }
  944. /* Reset the aux field of all outgoing edges of basic block BB. */
  945. static inline void
  946. reset_out_edges_aux (basic_block bb)
  947. {
  948. edge e;
  949. edge_iterator ei;
  950. FOR_EACH_EDGE (e, ei, bb->succs)
  951. e->aux = (void *)0;
  952. }
  953. /* Compute the number of case labels that correspond to each outgoing edge of
  954. STMT. Record this information in the aux field of the edge. */
  955. static inline void
  956. compute_cases_per_edge (gswitch *stmt)
  957. {
  958. basic_block bb = gimple_bb (stmt);
  959. reset_out_edges_aux (bb);
  960. int ncases = gimple_switch_num_labels (stmt);
  961. for (int i = ncases - 1; i >= 1; --i)
  962. {
  963. tree elt = gimple_switch_label (stmt, i);
  964. tree lab = CASE_LABEL (elt);
  965. basic_block case_bb = label_to_block_fn (cfun, lab);
  966. edge case_edge = find_edge (bb, case_bb);
  967. case_edge->aux = (void *)((intptr_t)(case_edge->aux) + 1);
  968. }
  969. }
  970. /* Terminate a case (Pascal/Ada) or switch (C) statement
  971. in which ORIG_INDEX is the expression to be tested.
  972. If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
  973. type as given in the source before any compiler conversions.
  974. Generate the code to test it and jump to the right place. */
  975. void
  976. expand_case (gswitch *stmt)
  977. {
  978. tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
  979. rtx default_label = NULL_RTX;
  980. unsigned int count, uniq;
  981. int i;
  982. int ncases = gimple_switch_num_labels (stmt);
  983. tree index_expr = gimple_switch_index (stmt);
  984. tree index_type = TREE_TYPE (index_expr);
  985. tree elt;
  986. basic_block bb = gimple_bb (stmt);
  987. /* A list of case labels; it is first built as a list and it may then
  988. be rearranged into a nearly balanced binary tree. */
  989. struct case_node *case_list = 0;
  990. /* A pool for case nodes. */
  991. alloc_pool case_node_pool;
  992. /* An ERROR_MARK occurs for various reasons including invalid data type.
  993. ??? Can this still happen, with GIMPLE and all? */
  994. if (index_type == error_mark_node)
  995. return;
  996. /* cleanup_tree_cfg removes all SWITCH_EXPR with their index
  997. expressions being INTEGER_CST. */
  998. gcc_assert (TREE_CODE (index_expr) != INTEGER_CST);
  999. case_node_pool = create_alloc_pool ("struct case_node pool",
  1000. sizeof (struct case_node),
  1001. 100);
  1002. do_pending_stack_adjust ();
  1003. /* Find the default case target label. */
  1004. default_label = label_rtx (CASE_LABEL (gimple_switch_default_label (stmt)));
  1005. edge default_edge = EDGE_SUCC (bb, 0);
  1006. int default_prob = default_edge->probability;
  1007. /* Get upper and lower bounds of case values. */
  1008. elt = gimple_switch_label (stmt, 1);
  1009. minval = fold_convert (index_type, CASE_LOW (elt));
  1010. elt = gimple_switch_label (stmt, ncases - 1);
  1011. if (CASE_HIGH (elt))
  1012. maxval = fold_convert (index_type, CASE_HIGH (elt));
  1013. else
  1014. maxval = fold_convert (index_type, CASE_LOW (elt));
  1015. /* Compute span of values. */
  1016. range = fold_build2 (MINUS_EXPR, index_type, maxval, minval);
  1017. /* Listify the labels queue and gather some numbers to decide
  1018. how to expand this switch(). */
  1019. uniq = 0;
  1020. count = 0;
  1021. hash_set<tree> seen_labels;
  1022. compute_cases_per_edge (stmt);
  1023. for (i = ncases - 1; i >= 1; --i)
  1024. {
  1025. elt = gimple_switch_label (stmt, i);
  1026. tree low = CASE_LOW (elt);
  1027. gcc_assert (low);
  1028. tree high = CASE_HIGH (elt);
  1029. gcc_assert (! high || tree_int_cst_lt (low, high));
  1030. tree lab = CASE_LABEL (elt);
  1031. /* Count the elements.
  1032. A range counts double, since it requires two compares. */
  1033. count++;
  1034. if (high)
  1035. count++;
  1036. /* If we have not seen this label yet, then increase the
  1037. number of unique case node targets seen. */
  1038. if (!seen_labels.add (lab))
  1039. uniq++;
  1040. /* The bounds on the case range, LOW and HIGH, have to be converted
  1041. to case's index type TYPE. Note that the original type of the
  1042. case index in the source code is usually "lost" during
  1043. gimplification due to type promotion, but the case labels retain the
  1044. original type. Make sure to drop overflow flags. */
  1045. low = fold_convert (index_type, low);
  1046. if (TREE_OVERFLOW (low))
  1047. low = wide_int_to_tree (index_type, low);
  1048. /* The canonical from of a case label in GIMPLE is that a simple case
  1049. has an empty CASE_HIGH. For the casesi and tablejump expanders,
  1050. the back ends want simple cases to have high == low. */
  1051. if (! high)
  1052. high = low;
  1053. high = fold_convert (index_type, high);
  1054. if (TREE_OVERFLOW (high))
  1055. high = wide_int_to_tree (index_type, high);
  1056. basic_block case_bb = label_to_block_fn (cfun, lab);
  1057. edge case_edge = find_edge (bb, case_bb);
  1058. case_list = add_case_node (
  1059. case_list, low, high, lab,
  1060. case_edge->probability / (intptr_t)(case_edge->aux),
  1061. case_node_pool);
  1062. }
  1063. reset_out_edges_aux (bb);
  1064. /* cleanup_tree_cfg removes all SWITCH_EXPR with a single
  1065. destination, such as one with a default case only.
  1066. It also removes cases that are out of range for the switch
  1067. type, so we should never get a zero here. */
  1068. gcc_assert (count > 0);
  1069. rtx_insn *before_case = get_last_insn ();
  1070. /* Decide how to expand this switch.
  1071. The two options at this point are a dispatch table (casesi or
  1072. tablejump) or a decision tree. */
  1073. if (expand_switch_as_decision_tree_p (range, uniq, count))
  1074. emit_case_decision_tree (index_expr, index_type,
  1075. case_list, default_label,
  1076. default_prob);
  1077. else
  1078. emit_case_dispatch_table (index_expr, index_type,
  1079. case_list, default_label,
  1080. minval, maxval, range, bb);
  1081. reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
  1082. free_temp_slots ();
  1083. free_alloc_pool (case_node_pool);
  1084. }
  1085. /* Expand the dispatch to a short decrement chain if there are few cases
  1086. to dispatch to. Likewise if neither casesi nor tablejump is available,
  1087. or if flag_jump_tables is set. Otherwise, expand as a casesi or a
  1088. tablejump. The index mode is always the mode of integer_type_node.
  1089. Trap if no case matches the index.
  1090. DISPATCH_INDEX is the index expression to switch on. It should be a
  1091. memory or register operand.
  1092. DISPATCH_TABLE is a set of case labels. The set should be sorted in
  1093. ascending order, be contiguous, starting with value 0, and contain only
  1094. single-valued case labels. */
  1095. void
  1096. expand_sjlj_dispatch_table (rtx dispatch_index,
  1097. vec<tree> dispatch_table)
  1098. {
  1099. tree index_type = integer_type_node;
  1100. machine_mode index_mode = TYPE_MODE (index_type);
  1101. int ncases = dispatch_table.length ();
  1102. do_pending_stack_adjust ();
  1103. rtx_insn *before_case = get_last_insn ();
  1104. /* Expand as a decrement-chain if there are 5 or fewer dispatch
  1105. labels. This covers more than 98% of the cases in libjava,
  1106. and seems to be a reasonable compromise between the "old way"
  1107. of expanding as a decision tree or dispatch table vs. the "new
  1108. way" with decrement chain or dispatch table. */
  1109. if (dispatch_table.length () <= 5
  1110. || (!HAVE_casesi && !HAVE_tablejump)
  1111. || !flag_jump_tables)
  1112. {
  1113. /* Expand the dispatch as a decrement chain:
  1114. "switch(index) {case 0: do_0; case 1: do_1; ...; case N: do_N;}"
  1115. ==>
  1116. if (index == 0) do_0; else index--;
  1117. if (index == 0) do_1; else index--;
  1118. ...
  1119. if (index == 0) do_N; else index--;
  1120. This is more efficient than a dispatch table on most machines.
  1121. The last "index--" is redundant but the code is trivially dead
  1122. and will be cleaned up by later passes. */
  1123. rtx index = copy_to_mode_reg (index_mode, dispatch_index);
  1124. rtx zero = CONST0_RTX (index_mode);
  1125. for (int i = 0; i < ncases; i++)
  1126. {
  1127. tree elt = dispatch_table[i];
  1128. rtx lab = label_rtx (CASE_LABEL (elt));
  1129. do_jump_if_equal (index_mode, index, zero, lab, 0, -1);
  1130. force_expand_binop (index_mode, sub_optab,
  1131. index, CONST1_RTX (index_mode),
  1132. index, 0, OPTAB_DIRECT);
  1133. }
  1134. }
  1135. else
  1136. {
  1137. /* Similar to expand_case, but much simpler. */
  1138. struct case_node *case_list = 0;
  1139. alloc_pool case_node_pool = create_alloc_pool ("struct sjlj_case pool",
  1140. sizeof (struct case_node),
  1141. ncases);
  1142. tree index_expr = make_tree (index_type, dispatch_index);
  1143. tree minval = build_int_cst (index_type, 0);
  1144. tree maxval = CASE_LOW (dispatch_table.last ());
  1145. tree range = maxval;
  1146. rtx_code_label *default_label = gen_label_rtx ();
  1147. for (int i = ncases - 1; i >= 0; --i)
  1148. {
  1149. tree elt = dispatch_table[i];
  1150. tree low = CASE_LOW (elt);
  1151. tree lab = CASE_LABEL (elt);
  1152. case_list = add_case_node (case_list, low, low, lab, 0, case_node_pool);
  1153. }
  1154. emit_case_dispatch_table (index_expr, index_type,
  1155. case_list, default_label,
  1156. minval, maxval, range,
  1157. BLOCK_FOR_INSN (before_case));
  1158. emit_label (default_label);
  1159. free_alloc_pool (case_node_pool);
  1160. }
  1161. /* Dispatching something not handled? Trap! */
  1162. expand_builtin_trap ();
  1163. reorder_insns (NEXT_INSN (before_case), get_last_insn (), before_case);
  1164. free_temp_slots ();
  1165. }
  1166. /* Take an ordered list of case nodes
  1167. and transform them into a near optimal binary tree,
  1168. on the assumption that any target code selection value is as
  1169. likely as any other.
  1170. The transformation is performed by splitting the ordered
  1171. list into two equal sections plus a pivot. The parts are
  1172. then attached to the pivot as left and right branches. Each
  1173. branch is then transformed recursively. */
  1174. static void
  1175. balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
  1176. {
  1177. case_node_ptr np;
  1178. np = *head;
  1179. if (np)
  1180. {
  1181. int i = 0;
  1182. int ranges = 0;
  1183. case_node_ptr *npp;
  1184. case_node_ptr left;
  1185. /* Count the number of entries on branch. Also count the ranges. */
  1186. while (np)
  1187. {
  1188. if (!tree_int_cst_equal (np->low, np->high))
  1189. ranges++;
  1190. i++;
  1191. np = np->right;
  1192. }
  1193. if (i > 2)
  1194. {
  1195. /* Split this list if it is long enough for that to help. */
  1196. npp = head;
  1197. left = *npp;
  1198. /* If there are just three nodes, split at the middle one. */
  1199. if (i == 3)
  1200. npp = &(*npp)->right;
  1201. else
  1202. {
  1203. /* Find the place in the list that bisects the list's total cost,
  1204. where ranges count as 2.
  1205. Here I gets half the total cost. */
  1206. i = (i + ranges + 1) / 2;
  1207. while (1)
  1208. {
  1209. /* Skip nodes while their cost does not reach that amount. */
  1210. if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
  1211. i--;
  1212. i--;
  1213. if (i <= 0)
  1214. break;
  1215. npp = &(*npp)->right;
  1216. }
  1217. }
  1218. *head = np = *npp;
  1219. *npp = 0;
  1220. np->parent = parent;
  1221. np->left = left;
  1222. /* Optimize each of the two split parts. */
  1223. balance_case_nodes (&np->left, np);
  1224. balance_case_nodes (&np->right, np);
  1225. np->subtree_prob = np->prob;
  1226. np->subtree_prob += np->left->subtree_prob;
  1227. np->subtree_prob += np->right->subtree_prob;
  1228. }
  1229. else
  1230. {
  1231. /* Else leave this branch as one level,
  1232. but fill in `parent' fields. */
  1233. np = *head;
  1234. np->parent = parent;
  1235. np->subtree_prob = np->prob;
  1236. for (; np->right; np = np->right)
  1237. {
  1238. np->right->parent = np;
  1239. (*head)->subtree_prob += np->right->subtree_prob;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. /* Search the parent sections of the case node tree
  1245. to see if a test for the lower bound of NODE would be redundant.
  1246. INDEX_TYPE is the type of the index expression.
  1247. The instructions to generate the case decision tree are
  1248. output in the same order as nodes are processed so it is
  1249. known that if a parent node checks the range of the current
  1250. node minus one that the current node is bounded at its lower
  1251. span. Thus the test would be redundant. */
  1252. static int
  1253. node_has_low_bound (case_node_ptr node, tree index_type)
  1254. {
  1255. tree low_minus_one;
  1256. case_node_ptr pnode;
  1257. /* If the lower bound of this node is the lowest value in the index type,
  1258. we need not test it. */
  1259. if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
  1260. return 1;
  1261. /* If this node has a left branch, the value at the left must be less
  1262. than that at this node, so it cannot be bounded at the bottom and
  1263. we need not bother testing any further. */
  1264. if (node->left)
  1265. return 0;
  1266. low_minus_one = fold_build2 (MINUS_EXPR, TREE_TYPE (node->low),
  1267. node->low,
  1268. build_int_cst (TREE_TYPE (node->low), 1));
  1269. /* If the subtraction above overflowed, we can't verify anything.
  1270. Otherwise, look for a parent that tests our value - 1. */
  1271. if (! tree_int_cst_lt (low_minus_one, node->low))
  1272. return 0;
  1273. for (pnode = node->parent; pnode; pnode = pnode->parent)
  1274. if (tree_int_cst_equal (low_minus_one, pnode->high))
  1275. return 1;
  1276. return 0;
  1277. }
  1278. /* Search the parent sections of the case node tree
  1279. to see if a test for the upper bound of NODE would be redundant.
  1280. INDEX_TYPE is the type of the index expression.
  1281. The instructions to generate the case decision tree are
  1282. output in the same order as nodes are processed so it is
  1283. known that if a parent node checks the range of the current
  1284. node plus one that the current node is bounded at its upper
  1285. span. Thus the test would be redundant. */
  1286. static int
  1287. node_has_high_bound (case_node_ptr node, tree index_type)
  1288. {
  1289. tree high_plus_one;
  1290. case_node_ptr pnode;
  1291. /* If there is no upper bound, obviously no test is needed. */
  1292. if (TYPE_MAX_VALUE (index_type) == NULL)
  1293. return 1;
  1294. /* If the upper bound of this node is the highest value in the type
  1295. of the index expression, we need not test against it. */
  1296. if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
  1297. return 1;
  1298. /* If this node has a right branch, the value at the right must be greater
  1299. than that at this node, so it cannot be bounded at the top and
  1300. we need not bother testing any further. */
  1301. if (node->right)
  1302. return 0;
  1303. high_plus_one = fold_build2 (PLUS_EXPR, TREE_TYPE (node->high),
  1304. node->high,
  1305. build_int_cst (TREE_TYPE (node->high), 1));
  1306. /* If the addition above overflowed, we can't verify anything.
  1307. Otherwise, look for a parent that tests our value + 1. */
  1308. if (! tree_int_cst_lt (node->high, high_plus_one))
  1309. return 0;
  1310. for (pnode = node->parent; pnode; pnode = pnode->parent)
  1311. if (tree_int_cst_equal (high_plus_one, pnode->low))
  1312. return 1;
  1313. return 0;
  1314. }
  1315. /* Search the parent sections of the
  1316. case node tree to see if both tests for the upper and lower
  1317. bounds of NODE would be redundant. */
  1318. static int
  1319. node_is_bounded (case_node_ptr node, tree index_type)
  1320. {
  1321. return (node_has_low_bound (node, index_type)
  1322. && node_has_high_bound (node, index_type));
  1323. }
  1324. /* Emit step-by-step code to select a case for the value of INDEX.
  1325. The thus generated decision tree follows the form of the
  1326. case-node binary tree NODE, whose nodes represent test conditions.
  1327. INDEX_TYPE is the type of the index of the switch.
  1328. Care is taken to prune redundant tests from the decision tree
  1329. by detecting any boundary conditions already checked by
  1330. emitted rtx. (See node_has_high_bound, node_has_low_bound
  1331. and node_is_bounded, above.)
  1332. Where the test conditions can be shown to be redundant we emit
  1333. an unconditional jump to the target code. As a further
  1334. optimization, the subordinates of a tree node are examined to
  1335. check for bounded nodes. In this case conditional and/or
  1336. unconditional jumps as a result of the boundary check for the
  1337. current node are arranged to target the subordinates associated
  1338. code for out of bound conditions on the current node.
  1339. We can assume that when control reaches the code generated here,
  1340. the index value has already been compared with the parents
  1341. of this node, and determined to be on the same side of each parent
  1342. as this node is. Thus, if this node tests for the value 51,
  1343. and a parent tested for 52, we don't need to consider
  1344. the possibility of a value greater than 51. If another parent
  1345. tests for the value 50, then this node need not test anything. */
  1346. static void
  1347. emit_case_nodes (rtx index, case_node_ptr node, rtx default_label,
  1348. int default_prob, tree index_type)
  1349. {
  1350. /* If INDEX has an unsigned type, we must make unsigned branches. */
  1351. int unsignedp = TYPE_UNSIGNED (index_type);
  1352. int probability;
  1353. int prob = node->prob, subtree_prob = node->subtree_prob;
  1354. machine_mode mode = GET_MODE (index);
  1355. machine_mode imode = TYPE_MODE (index_type);
  1356. /* Handle indices detected as constant during RTL expansion. */
  1357. if (mode == VOIDmode)
  1358. mode = imode;
  1359. /* See if our parents have already tested everything for us.
  1360. If they have, emit an unconditional jump for this node. */
  1361. if (node_is_bounded (node, index_type))
  1362. emit_jump (label_rtx (node->code_label));
  1363. else if (tree_int_cst_equal (node->low, node->high))
  1364. {
  1365. probability = conditional_probability (prob, subtree_prob + default_prob);
  1366. /* Node is single valued. First see if the index expression matches
  1367. this node and then check our children, if any. */
  1368. do_jump_if_equal (mode, index,
  1369. convert_modes (mode, imode,
  1370. expand_normal (node->low),
  1371. unsignedp),
  1372. label_rtx (node->code_label), unsignedp, probability);
  1373. /* Since this case is taken at this point, reduce its weight from
  1374. subtree_weight. */
  1375. subtree_prob -= prob;
  1376. if (node->right != 0 && node->left != 0)
  1377. {
  1378. /* This node has children on both sides.
  1379. Dispatch to one side or the other
  1380. by comparing the index value with this node's value.
  1381. If one subtree is bounded, check that one first,
  1382. so we can avoid real branches in the tree. */
  1383. if (node_is_bounded (node->right, index_type))
  1384. {
  1385. probability = conditional_probability (
  1386. node->right->prob,
  1387. subtree_prob + default_prob);
  1388. emit_cmp_and_jump_insns (index,
  1389. convert_modes
  1390. (mode, imode,
  1391. expand_normal (node->high),
  1392. unsignedp),
  1393. GT, NULL_RTX, mode, unsignedp,
  1394. label_rtx (node->right->code_label),
  1395. probability);
  1396. emit_case_nodes (index, node->left, default_label, default_prob,
  1397. index_type);
  1398. }
  1399. else if (node_is_bounded (node->left, index_type))
  1400. {
  1401. probability = conditional_probability (
  1402. node->left->prob,
  1403. subtree_prob + default_prob);
  1404. emit_cmp_and_jump_insns (index,
  1405. convert_modes
  1406. (mode, imode,
  1407. expand_normal (node->high),
  1408. unsignedp),
  1409. LT, NULL_RTX, mode, unsignedp,
  1410. label_rtx (node->left->code_label),
  1411. probability);
  1412. emit_case_nodes (index, node->right, default_label, default_prob, index_type);
  1413. }
  1414. /* If both children are single-valued cases with no
  1415. children, finish up all the work. This way, we can save
  1416. one ordered comparison. */
  1417. else if (tree_int_cst_equal (node->right->low, node->right->high)
  1418. && node->right->left == 0
  1419. && node->right->right == 0
  1420. && tree_int_cst_equal (node->left->low, node->left->high)
  1421. && node->left->left == 0
  1422. && node->left->right == 0)
  1423. {
  1424. /* Neither node is bounded. First distinguish the two sides;
  1425. then emit the code for one side at a time. */
  1426. /* See if the value matches what the right hand side
  1427. wants. */
  1428. probability = conditional_probability (
  1429. node->right->prob,
  1430. subtree_prob + default_prob);
  1431. do_jump_if_equal (mode, index,
  1432. convert_modes (mode, imode,
  1433. expand_normal (node->right->low),
  1434. unsignedp),
  1435. label_rtx (node->right->code_label),
  1436. unsignedp, probability);
  1437. /* See if the value matches what the left hand side
  1438. wants. */
  1439. probability = conditional_probability (
  1440. node->left->prob,
  1441. subtree_prob + default_prob);
  1442. do_jump_if_equal (mode, index,
  1443. convert_modes (mode, imode,
  1444. expand_normal (node->left->low),
  1445. unsignedp),
  1446. label_rtx (node->left->code_label),
  1447. unsignedp, probability);
  1448. }
  1449. else
  1450. {
  1451. /* Neither node is bounded. First distinguish the two sides;
  1452. then emit the code for one side at a time. */
  1453. tree test_label
  1454. = build_decl (curr_insn_location (),
  1455. LABEL_DECL, NULL_TREE, void_type_node);
  1456. /* The default label could be reached either through the right
  1457. subtree or the left subtree. Divide the probability
  1458. equally. */
  1459. probability = conditional_probability (
  1460. node->right->subtree_prob + default_prob/2,
  1461. subtree_prob + default_prob);
  1462. /* See if the value is on the right. */
  1463. emit_cmp_and_jump_insns (index,
  1464. convert_modes
  1465. (mode, imode,
  1466. expand_normal (node->high),
  1467. unsignedp),
  1468. GT, NULL_RTX, mode, unsignedp,
  1469. label_rtx (test_label),
  1470. probability);
  1471. default_prob /= 2;
  1472. /* Value must be on the left.
  1473. Handle the left-hand subtree. */
  1474. emit_case_nodes (index, node->left, default_label, default_prob, index_type);
  1475. /* If left-hand subtree does nothing,
  1476. go to default. */
  1477. if (default_label)
  1478. emit_jump (default_label);
  1479. /* Code branches here for the right-hand subtree. */
  1480. expand_label (test_label);
  1481. emit_case_nodes (index, node->right, default_label, default_prob, index_type);
  1482. }
  1483. }
  1484. else if (node->right != 0 && node->left == 0)
  1485. {
  1486. /* Here we have a right child but no left so we issue a conditional
  1487. branch to default and process the right child.
  1488. Omit the conditional branch to default if the right child
  1489. does not have any children and is single valued; it would
  1490. cost too much space to save so little time. */
  1491. if (node->right->right || node->right->left
  1492. || !tree_int_cst_equal (node->right->low, node->right->high))
  1493. {
  1494. if (!node_has_low_bound (node, index_type))
  1495. {
  1496. probability = conditional_probability (
  1497. default_prob/2,
  1498. subtree_prob + default_prob);
  1499. emit_cmp_and_jump_insns (index,
  1500. convert_modes
  1501. (mode, imode,
  1502. expand_normal (node->high),
  1503. unsignedp),
  1504. LT, NULL_RTX, mode, unsignedp,
  1505. default_label,
  1506. probability);
  1507. default_prob /= 2;
  1508. }
  1509. emit_case_nodes (index, node->right, default_label, default_prob, index_type);
  1510. }
  1511. else
  1512. {
  1513. probability = conditional_probability (
  1514. node->right->subtree_prob,
  1515. subtree_prob + default_prob);
  1516. /* We cannot process node->right normally
  1517. since we haven't ruled out the numbers less than
  1518. this node's value. So handle node->right explicitly. */
  1519. do_jump_if_equal (mode, index,
  1520. convert_modes
  1521. (mode, imode,
  1522. expand_normal (node->right->low),
  1523. unsignedp),
  1524. label_rtx (node->right->code_label), unsignedp, probability);
  1525. }
  1526. }
  1527. else if (node->right == 0 && node->left != 0)
  1528. {
  1529. /* Just one subtree, on the left. */
  1530. if (node->left->left || node->left->right
  1531. || !tree_int_cst_equal (node->left->low, node->left->high))
  1532. {
  1533. if (!node_has_high_bound (node, index_type))
  1534. {
  1535. probability = conditional_probability (
  1536. default_prob/2,
  1537. subtree_prob + default_prob);
  1538. emit_cmp_and_jump_insns (index,
  1539. convert_modes
  1540. (mode, imode,
  1541. expand_normal (node->high),
  1542. unsignedp),
  1543. GT, NULL_RTX, mode, unsignedp,
  1544. default_label,
  1545. probability);
  1546. default_prob /= 2;
  1547. }
  1548. emit_case_nodes (index, node->left, default_label,
  1549. default_prob, index_type);
  1550. }
  1551. else
  1552. {
  1553. probability = conditional_probability (
  1554. node->left->subtree_prob,
  1555. subtree_prob + default_prob);
  1556. /* We cannot process node->left normally
  1557. since we haven't ruled out the numbers less than
  1558. this node's value. So handle node->left explicitly. */
  1559. do_jump_if_equal (mode, index,
  1560. convert_modes
  1561. (mode, imode,
  1562. expand_normal (node->left->low),
  1563. unsignedp),
  1564. label_rtx (node->left->code_label), unsignedp, probability);
  1565. }
  1566. }
  1567. }
  1568. else
  1569. {
  1570. /* Node is a range. These cases are very similar to those for a single
  1571. value, except that we do not start by testing whether this node
  1572. is the one to branch to. */
  1573. if (node->right != 0 && node->left != 0)
  1574. {
  1575. /* Node has subtrees on both sides.
  1576. If the right-hand subtree is bounded,
  1577. test for it first, since we can go straight there.
  1578. Otherwise, we need to make a branch in the control structure,
  1579. then handle the two subtrees. */
  1580. tree test_label = 0;
  1581. if (node_is_bounded (node->right, index_type))
  1582. {
  1583. /* Right hand node is fully bounded so we can eliminate any
  1584. testing and branch directly to the target code. */
  1585. probability = conditional_probability (
  1586. node->right->subtree_prob,
  1587. subtree_prob + default_prob);
  1588. emit_cmp_and_jump_insns (index,
  1589. convert_modes
  1590. (mode, imode,
  1591. expand_normal (node->high),
  1592. unsignedp),
  1593. GT, NULL_RTX, mode, unsignedp,
  1594. label_rtx (node->right->code_label),
  1595. probability);
  1596. }
  1597. else
  1598. {
  1599. /* Right hand node requires testing.
  1600. Branch to a label where we will handle it later. */
  1601. test_label = build_decl (curr_insn_location (),
  1602. LABEL_DECL, NULL_TREE, void_type_node);
  1603. probability = conditional_probability (
  1604. node->right->subtree_prob + default_prob/2,
  1605. subtree_prob + default_prob);
  1606. emit_cmp_and_jump_insns (index,
  1607. convert_modes
  1608. (mode, imode,
  1609. expand_normal (node->high),
  1610. unsignedp),
  1611. GT, NULL_RTX, mode, unsignedp,
  1612. label_rtx (test_label),
  1613. probability);
  1614. default_prob /= 2;
  1615. }
  1616. /* Value belongs to this node or to the left-hand subtree. */
  1617. probability = conditional_probability (
  1618. prob,
  1619. subtree_prob + default_prob);
  1620. emit_cmp_and_jump_insns (index,
  1621. convert_modes
  1622. (mode, imode,
  1623. expand_normal (node->low),
  1624. unsignedp),
  1625. GE, NULL_RTX, mode, unsignedp,
  1626. label_rtx (node->code_label),
  1627. probability);
  1628. /* Handle the left-hand subtree. */
  1629. emit_case_nodes (index, node->left, default_label, default_prob, index_type);
  1630. /* If right node had to be handled later, do that now. */
  1631. if (test_label)
  1632. {
  1633. /* If the left-hand subtree fell through,
  1634. don't let it fall into the right-hand subtree. */
  1635. if (default_label)
  1636. emit_jump (default_label);
  1637. expand_label (test_label);
  1638. emit_case_nodes (index, node->right, default_label, default_prob, index_type);
  1639. }
  1640. }
  1641. else if (node->right != 0 && node->left == 0)
  1642. {
  1643. /* Deal with values to the left of this node,
  1644. if they are possible. */
  1645. if (!node_has_low_bound (node, index_type))
  1646. {
  1647. probability = conditional_probability (
  1648. default_prob/2,
  1649. subtree_prob + default_prob);
  1650. emit_cmp_and_jump_insns (index,
  1651. convert_modes
  1652. (mode, imode,
  1653. expand_normal (node->low),
  1654. unsignedp),
  1655. LT, NULL_RTX, mode, unsignedp,
  1656. default_label,
  1657. probability);
  1658. default_prob /= 2;
  1659. }
  1660. /* Value belongs to this node or to the right-hand subtree. */
  1661. probability = conditional_probability (
  1662. prob,
  1663. subtree_prob + default_prob);
  1664. emit_cmp_and_jump_insns (index,
  1665. convert_modes
  1666. (mode, imode,
  1667. expand_normal (node->high),
  1668. unsignedp),
  1669. LE, NULL_RTX, mode, unsignedp,
  1670. label_rtx (node->code_label),
  1671. probability);
  1672. emit_case_nodes (index, node->right, default_label, default_prob, index_type);
  1673. }
  1674. else if (node->right == 0 && node->left != 0)
  1675. {
  1676. /* Deal with values to the right of this node,
  1677. if they are possible. */
  1678. if (!node_has_high_bound (node, index_type))
  1679. {
  1680. probability = conditional_probability (
  1681. default_prob/2,
  1682. subtree_prob + default_prob);
  1683. emit_cmp_and_jump_insns (index,
  1684. convert_modes
  1685. (mode, imode,
  1686. expand_normal (node->high),
  1687. unsignedp),
  1688. GT, NULL_RTX, mode, unsignedp,
  1689. default_label,
  1690. probability);
  1691. default_prob /= 2;
  1692. }
  1693. /* Value belongs to this node or to the left-hand subtree. */
  1694. probability = conditional_probability (
  1695. prob,
  1696. subtree_prob + default_prob);
  1697. emit_cmp_and_jump_insns (index,
  1698. convert_modes
  1699. (mode, imode,
  1700. expand_normal (node->low),
  1701. unsignedp),
  1702. GE, NULL_RTX, mode, unsignedp,
  1703. label_rtx (node->code_label),
  1704. probability);
  1705. emit_case_nodes (index, node->left, default_label, default_prob, index_type);
  1706. }
  1707. else
  1708. {
  1709. /* Node has no children so we check low and high bounds to remove
  1710. redundant tests. Only one of the bounds can exist,
  1711. since otherwise this node is bounded--a case tested already. */
  1712. int high_bound = node_has_high_bound (node, index_type);
  1713. int low_bound = node_has_low_bound (node, index_type);
  1714. if (!high_bound && low_bound)
  1715. {
  1716. probability = conditional_probability (
  1717. default_prob,
  1718. subtree_prob + default_prob);
  1719. emit_cmp_and_jump_insns (index,
  1720. convert_modes
  1721. (mode, imode,
  1722. expand_normal (node->high),
  1723. unsignedp),
  1724. GT, NULL_RTX, mode, unsignedp,
  1725. default_label,
  1726. probability);
  1727. }
  1728. else if (!low_bound && high_bound)
  1729. {
  1730. probability = conditional_probability (
  1731. default_prob,
  1732. subtree_prob + default_prob);
  1733. emit_cmp_and_jump_insns (index,
  1734. convert_modes
  1735. (mode, imode,
  1736. expand_normal (node->low),
  1737. unsignedp),
  1738. LT, NULL_RTX, mode, unsignedp,
  1739. default_label,
  1740. probability);
  1741. }
  1742. else if (!low_bound && !high_bound)
  1743. {
  1744. /* Widen LOW and HIGH to the same width as INDEX. */
  1745. tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
  1746. tree low = build1 (CONVERT_EXPR, type, node->low);
  1747. tree high = build1 (CONVERT_EXPR, type, node->high);
  1748. rtx low_rtx, new_index, new_bound;
  1749. /* Instead of doing two branches, emit one unsigned branch for
  1750. (index-low) > (high-low). */
  1751. low_rtx = expand_expr (low, NULL_RTX, mode, EXPAND_NORMAL);
  1752. new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
  1753. NULL_RTX, unsignedp,
  1754. OPTAB_WIDEN);
  1755. new_bound = expand_expr (fold_build2 (MINUS_EXPR, type,
  1756. high, low),
  1757. NULL_RTX, mode, EXPAND_NORMAL);
  1758. probability = conditional_probability (
  1759. default_prob,
  1760. subtree_prob + default_prob);
  1761. emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
  1762. mode, 1, default_label, probability);
  1763. }
  1764. emit_jump (label_rtx (node->code_label));
  1765. }
  1766. }
  1767. }