public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Clustering switch cases
@ 2010-08-27 13:49 Paulo J. Matos
  2010-08-27 15:03 ` Ian Lance Taylor
       [not found] ` <D84AB18A31AC674EBE9B5300634A800601BF0B0932@EXCHANGEVS.IceraSemi.local>
  0 siblings, 2 replies; 12+ messages in thread
From: Paulo J. Matos @ 2010-08-27 13:49 UTC (permalink / raw)
  To: gcc

Hi,

I have been analysing the gcc4.4 code due to the way it's handling:
1  extern void f(const char *);
2  extern void g(int);
3
4  #define C(n) case n: f(#n); break
5
6  void g(int n)
7  {
8      switch(n)
9      {
10         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
11         C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17);
C(18); C(19);
12         C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27);
C(28); C(29);
13
14         C(1000); C(1001); C(1002); C(1003); C(1004); C(1005);
C(1006); C(1007); C(1008); C(1009);
15     }
16 }

The interesting thing about this is that GCC generates much better code if I do:
1  extern void f(const char *);
2  extern void g(int);
3
4  #define C(n) case n: f(#n); break
5
6  void g(int n)
7  {
8      switch(n)
9      {
10         C(0); C(1); C(2); C(3); C(4); C(5); C(6); C(7); C(8); C(9);
11         C(10); C(11); C(12); C(13); C(14); C(15); C(16); C(17);
C(18); C(19);
12         C(20); C(21); C(22); C(23); C(24); C(25); C(26); C(27);
C(28); C(29);
13     }
14     switch(n)
15     {
16         C(1000); C(1001); C(1002); C(1003); C(1004); C(1005);
C(1006); C(1007); C(1008); C(1009);
17     }
18 }

In the first case, it generates a binary tree, and in the second two
jump tables. The jump tables solution is much more elegant (at least
in our situation), generating less code and being faster.
Now, what I am wondering is the reason why GCC doesn't try to cluster
the cases trying to find for clusters of contiguous values in the
switch.

If there is no specific reason then I would implement such pass, which
would before expansion split switches according to value clustering,
since I find it would be a good code improvement.

Currently GCC seems to only use jump table is the range of the switch
is not much bigger than its count, which works well in most cases
except when you have big switches with clusters of contiguous values
(like the first example I sent).

Any comments on this would be appreciated.

-- 
PMatos

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-09-03 10:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-27 13:49 Clustering switch cases Paulo J. Matos
2010-08-27 15:03 ` Ian Lance Taylor
2010-08-27 15:09   ` Paulo J. Matos
2010-08-27 15:27   ` Richard Guenther
2010-08-28  0:21     ` Xinliang David Li
2010-08-28 11:08     ` Paulo J. Matos
2010-08-31 15:59       ` Rahul Kharche
     [not found]       ` <201009010034.18924.paul@codesourcery.com>
2010-09-01  8:39         ` Richard Guenther
2010-09-01  9:15           ` Paulo J. Matos
2010-09-01  9:33             ` Richard Guenther
2010-09-01 12:37               ` Paulo J. Matos
     [not found] ` <D84AB18A31AC674EBE9B5300634A800601BF0B0932@EXCHANGEVS.IceraSemi.local>
     [not found]   ` <AANLkTinFhrgJw3Wz8aLhBU6PMYpJzBFTRxLUH3eoz4M0@mail.gmail.com>
     [not found]     ` <4D60B0700D1DB54A8C0C6E9BE691637008965A28@EXCHANGEVS.IceraSemi.local>
     [not found]       ` <AANLkTinkjc4Y9baHXXK7v3ibQNwMtwGhA_oGmaFbykkA@mail.gmail.com>
2010-09-03 10:29         ` Rahul Kharche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).