904-flatten-switch-stmt-00.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. Hi,
  2. The attached patch makes sure that we create smaller object code for
  3. simple switch statements. We just make sure to flatten the switch
  4. statement into an if-else chain, basically.
  5. This fixes a size-regression as compared to gcc-3.4, as can be seen
  6. below.
  7. 2007-04-15 Bernhard Fischer <..>
  8. * stmt.c (expand_case): Do not create a complex binary tree when
  9. optimizing for size but rather use the simple ordered list.
  10. (emit_case_nodes): do not emit jumps to the default_label when
  11. optimizing for size.
  12. Not regtested so far.
  13. Comments?
  14. Attached is the test switch.c mentioned below.
  15. $ for i in 2.95 3.3 3.4 4.0 4.1 4.2.orig-HEAD 4.3.orig-HEAD 4.3-HEAD;do
  16. gcc-$i -DCHAIN -Os -o switch-CHAIN-$i.o -c switch.c ;done
  17. $ for i in 2.95 3.3 3.4 4.0 4.1 4.2.orig-HEAD 4.3.orig-HEAD 4.3-HEAD;do
  18. gcc-$i -UCHAIN -Os -o switch-$i.o -c switch.c ;done
  19. $ size switch-*.o
  20. text data bss dec hex filename
  21. 169 0 0 169 a9 switch-2.95.o
  22. 115 0 0 115 73 switch-3.3.o
  23. 103 0 0 103 67 switch-3.4.o
  24. 124 0 0 124 7c switch-4.0.o
  25. 124 0 0 124 7c switch-4.1.o
  26. 124 0 0 124 7c switch-4.2.orig-HEAD.o
  27. 95 0 0 95 5f switch-4.3-HEAD.o
  28. 124 0 0 124 7c switch-4.3.orig-HEAD.o
  29. 166 0 0 166 a6 switch-CHAIN-2.95.o
  30. 111 0 0 111 6f switch-CHAIN-3.3.o
  31. 95 0 0 95 5f switch-CHAIN-3.4.o
  32. 95 0 0 95 5f switch-CHAIN-4.0.o
  33. 95 0 0 95 5f switch-CHAIN-4.1.o
  34. 95 0 0 95 5f switch-CHAIN-4.2.orig-HEAD.o
  35. 95 0 0 95 5f switch-CHAIN-4.3-HEAD.o
  36. 95 0 0 95 5f switch-CHAIN-4.3.orig-HEAD.o
  37. Content-Type: text/x-diff; charset=us-ascii
  38. Content-Disposition: attachment; filename="gcc-4.3.gcc-flatten-switch-stmt.00.diff"
  39. Index: gcc-4.2.0/gcc/stmt.c
  40. ===================================================================
  41. --- gcc-4.2.0.orig/gcc/stmt.c (revision 123843)
  42. +++ gcc-4.2.0/gcc/stmt.c (working copy)
  43. @@ -2517,7 +2517,11 @@ expand_case (tree exp)
  44. use_cost_table
  45. = (TREE_CODE (orig_type) != ENUMERAL_TYPE
  46. && estimate_case_costs (case_list));
  47. - balance_case_nodes (&case_list, NULL);
  48. + /* When optimizing for size, we want a straight list to avoid
  49. + jumps as much as possible. This basically creates an if-else
  50. + chain. */
  51. + if (!optimize_size)
  52. + balance_case_nodes (&case_list, NULL);
  53. emit_case_nodes (index, case_list, default_label, index_type);
  54. emit_jump (default_label);
  55. }
  56. @@ -3075,6 +3079,7 @@ emit_case_nodes (rtx index, case_node_pt
  57. {
  58. if (!node_has_low_bound (node, index_type))
  59. {
  60. + if (!optimize_size) /* don't jl to the .default_label. */
  61. emit_cmp_and_jump_insns (index,
  62. convert_modes
  63. (mode, imode,
  64. Content-Type: text/x-csrc; charset=us-ascii
  65. Content-Disposition: attachment; filename="switch.c"
  66. int
  67. commutative_tree_code (int code)
  68. {
  69. #define CASE(val, ret) case val:/* __asm__("# val="#val ",ret="#ret);*/ return ret;
  70. #ifndef CHAIN
  71. switch (code)
  72. {
  73. # if 1
  74. CASE(1,3)
  75. CASE(3,2)
  76. CASE(5,8)
  77. CASE(7,1)
  78. CASE(33,4)
  79. CASE(44,9)
  80. CASE(55,10)
  81. CASE(66,-1)
  82. CASE(77,99)
  83. CASE(666,0)
  84. # else
  85. case 1:
  86. return 3;
  87. case 3:
  88. return 2;
  89. case 5:
  90. return 8;
  91. case 7:
  92. return 1;
  93. case 33:
  94. return 4;
  95. case 44:
  96. return 9;
  97. case 55:
  98. return 10;
  99. case 66:
  100. return -1;
  101. case 77:
  102. return 99;
  103. case 666:
  104. return 0;
  105. # endif
  106. default:
  107. break;
  108. }
  109. return 4711;
  110. #else
  111. if (code == 1)
  112. return 3;
  113. else if (code == 3)
  114. return 2;
  115. else if (code == 5)
  116. return 8;
  117. else if (code == 7)
  118. return 1;
  119. else if (code == 33)
  120. return 4;
  121. else if (code == 44)
  122. return 9;
  123. else if (code == 55)
  124. return 10;
  125. else if (code == 66)
  126. return -1;
  127. else if (code == 77)
  128. return 99;
  129. else if (code == 666)
  130. return 0;
  131. else
  132. return 4711;
  133. #endif
  134. }
  135. --AhhlLboLdkugWU4S--