home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / gnu / gcc / bug / 2233 < prev    next >
Encoding:
Text File  |  1992-09-01  |  7.5 KB  |  204 lines

  1. Newsgroups: gnu.gcc.bug
  2. Path: sparky!uunet!cis.ohio-state.edu!twinsun.COM!eggert
  3. From: eggert@twinsun.COM (Paul Eggert)
  4. Subject: GCC 2.2.2 patch to improve quality of code for (expr&const)!=0
  5. Message-ID: <9209012310.AA22819@farside.twinsun.com>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: GNUs Not Usenet
  8. Distribution: gnu
  9. Date: Tue, 1 Sep 1992 23:10:58 GMT
  10. Approved: bug-gcc@prep.ai.mit.edu
  11. Lines: 191
  12.  
  13. The following patch uniformly improves the quality of the generated code for
  14. for all programs that I have looked at on my machine (Sparc, SunOS 4.1.2);
  15. this includes the entire GCC source code.  For example, given the program
  16.  
  17.     void f() { if (g() & 32768) h(); }
  18.  
  19. the patched GCC generates two fewer machine instructions by avoiding an
  20. unnecessary shift and compare, as follows (diff --side-by-side output):
  21.  
  22. ----- GCC 2.2.2 -----            ----- GCC 2.2.2 with patch -----
  23. _f:                    _f:
  24.     save %sp,-112,%sp            save %sp,-112,%sp
  25.     call _g,0                call _g,0
  26.     nop                    nop
  27.     sethi %hi(-32768),%o1          |        sethi %hi(32768),%o1
  28.     and %o0,%o1,%o0              |        andcc %o0,%o1,%g0
  29.     sll %o0,16,%o0              <
  30.     cmp %o0,0              <
  31.     be L2                    be L2
  32.     nop                    nop
  33.     call _h,0                call _h,0
  34.     nop                    nop
  35. L2:                    L2:
  36.     ret                    ret
  37.     restore                    restore
  38.  
  39.  
  40. I'm fairly sure the patch does not introduce a compiler bug on any 2's
  41. complement host, but I'm not sure whether it leads to better quality code on
  42. all hosts.  If this patch loses on some hosts, perhaps a configuration
  43. variable is needed to include it conditionally.
  44.  
  45.  
  46. Tue Sep  1 22:55:59 1992  Paul Eggert  (eggert@twinsun.com)
  47.  
  48.     * c-convert.c, cp-cvt.c (convert_to_integer): Prefer unsigned
  49.     arithmetic if the output type is unsigned and is no more precise than
  50.     the computation type.  This avoids unnecessary sign extension.
  51.  
  52. ===================================================================
  53. RCS file: c-convert.c,v
  54. retrieving revision 2.2
  55. diff -c -r2.2 c-convert.c
  56. *** c-convert.c    1992/04/21 18:00:03    2.2
  57. --- c-convert.c    1992/09/01 22:55:59
  58. ***************
  59. *** 232,242 ****
  60.             {
  61.               /* Don't do unsigned arithmetic where signed was wanted,
  62.                  or vice versa.
  63. !                Exception: if either of the original operands were
  64.                  unsigned then can safely do the work as unsigned.
  65.                  And we may need to do it as unsigned
  66.                  if we truncate to the original size.  */
  67. !             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  68.                     || TREE_UNSIGNED (TREE_TYPE (arg0))
  69.                     || TREE_UNSIGNED (TREE_TYPE (arg1)))
  70.                    ? unsigned_type (typex) : signed_type (typex));
  71. --- 232,247 ----
  72.             {
  73.               /* Don't do unsigned arithmetic where signed was wanted,
  74.                  or vice versa.
  75. !                Exception 1: if we will eventually truncate
  76. !                the result to unsigned, then do the work as unsigned;
  77. !                this can prevent unnecessary sign-extension.
  78. !                Exception 2: if either of the original operands were
  79.                  unsigned then can safely do the work as unsigned.
  80.                  And we may need to do it as unsigned
  81.                  if we truncate to the original size.  */
  82. !             typex = (((outprec <= TYPE_PRECISION (typex)
  83. !                    && TREE_UNSIGNED (type))
  84. !                   || TREE_UNSIGNED (TREE_TYPE (expr))
  85.                     || TREE_UNSIGNED (TREE_TYPE (arg0))
  86.                     || TREE_UNSIGNED (TREE_TYPE (arg1)))
  87.                    ? unsigned_type (typex) : signed_type (typex));
  88. ***************
  89. *** 284,290 ****
  90.             {
  91.           /* Don't do unsigned arithmetic where signed was wanted,
  92.              or vice versa.  */
  93. !         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  94.                ? unsigned_type (typex) : signed_type (typex));
  95.           return convert (type,
  96.                   build_unary_op (ex_form,
  97. --- 289,297 ----
  98.             {
  99.           /* Don't do unsigned arithmetic where signed was wanted,
  100.              or vice versa.  */
  101. !         typex = (((outprec <= TYPE_PRECISION (typex)
  102. !                && TREE_UNSIGNED (type))
  103. !               || TREE_UNSIGNED (TREE_TYPE (expr)))
  104.                ? unsigned_type (typex) : signed_type (typex));
  105.           return convert (type,
  106.                   build_unary_op (ex_form,
  107. ***************
  108. *** 327,333 ****
  109.             {
  110.               /* Don't do unsigned arithmetic where signed was wanted,
  111.                  or vice versa.  */
  112. !             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  113.                    ? unsigned_type (typex) : signed_type (typex));
  114.               return convert (type,
  115.                       fold (build (COND_EXPR, typex,
  116. --- 334,342 ----
  117.             {
  118.               /* Don't do unsigned arithmetic where signed was wanted,
  119.                  or vice versa.  */
  120. !             typex = (((outprec <= TYPE_PRECISION (typex)
  121. !                    && TREE_UNSIGNED (type))
  122. !                   || TREE_UNSIGNED (TREE_TYPE (expr)))
  123.                    ? unsigned_type (typex) : signed_type (typex));
  124.               return convert (type,
  125.                       fold (build (COND_EXPR, typex,
  126. ===================================================================
  127. RCS file: cp-cvt.c,v
  128. retrieving revision 2.2
  129. diff -c -r2.2 cp-cvt.c
  130. *** cp-cvt.c    1992/04/23 03:18:58    2.2
  131. --- cp-cvt.c    1992/09/01 22:56:05
  132. ***************
  133. *** 831,842 ****
  134.             {
  135.               /* Don't do unsigned arithmetic where signed was wanted,
  136.                  or vice versa.
  137. !                Exception: if the original operands were unsigned
  138. !                then can safely do the work as unsigned.
  139.                  And we may need to do it as unsigned
  140.                  if we truncate to the original size.  */
  141. !             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  142. !                   || TREE_UNSIGNED (TREE_TYPE (arg0)))
  143.                    ? unsigned_type (typex) : signed_type (typex));
  144.               return convert (type,
  145.                       build_binary_op_nodefault (ex_form,
  146. --- 831,848 ----
  147.             {
  148.               /* Don't do unsigned arithmetic where signed was wanted,
  149.                  or vice versa.
  150. !                Exception 1: if we will eventually truncate
  151. !                the result, then do the work as unsigned;
  152. !                this can prevent unnecessary sign-extension.
  153. !                Exception 2: if either of the original operands were
  154. !                unsigned then can safely do the work as unsigned.
  155.                  And we may need to do it as unsigned
  156.                  if we truncate to the original size.  */
  157. !             typex = (((outprec <= TYPE_PRECISION (typex)
  158. !                    && TREE_UNSIGNED (type))
  159. !                   || TREE_UNSIGNED (TREE_TYPE (expr))
  160. !                   || TREE_UNSIGNED (TREE_TYPE (arg0))
  161. !                   || TREE_UNSIGNED (TREE_TYPE (arg1)))
  162.                    ? unsigned_type (typex) : signed_type (typex));
  163.               return convert (type,
  164.                       build_binary_op_nodefault (ex_form,
  165. ***************
  166. *** 883,889 ****
  167.             {
  168.           /* Don't do unsigned arithmetic where signed was wanted,
  169.              or vice versa.  */
  170. !         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  171.                ? unsigned_type (typex) : signed_type (typex));
  172.           return convert (type,
  173.                   build_unary_op (ex_form,
  174. --- 889,897 ----
  175.             {
  176.           /* Don't do unsigned arithmetic where signed was wanted,
  177.              or vice versa.  */
  178. !         typex = (((outprec <= TYPE_PRECISION (typex)
  179. !                && TREE_UNSIGNED (type))
  180. !               || TREE_UNSIGNED (TREE_TYPE (expr)))
  181.                ? unsigned_type (typex) : signed_type (typex));
  182.           return convert (type,
  183.                   build_unary_op (ex_form,
  184. ***************
  185. *** 926,932 ****
  186.             {
  187.               /* Don't do unsigned arithmetic where signed was wanted,
  188.                  or vice versa.  */
  189. !             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  190.                    ? unsigned_type (typex) : signed_type (typex));
  191.               return convert (type,
  192.                       fold (build (COND_EXPR, typex,
  193. --- 934,942 ----
  194.             {
  195.               /* Don't do unsigned arithmetic where signed was wanted,
  196.                  or vice versa.  */
  197. !             typex = (((outprec <= TYPE_PRECISION (typex)
  198. !                    && TREE_UNSIGNED (type))
  199. !                   || TREE_UNSIGNED (TREE_TYPE (expr)))
  200.                    ? unsigned_type (typex) : signed_type (typex));
  201.               return convert (type,
  202.                       fold (build (COND_EXPR, typex,
  203.  
  204.