www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6767] New: Range case statements generate horrific code


           Summary: Range case statements generate horrific code
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Mac OS X
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: peter.alexander.au gmail.com

10:43:04 PDT ---
int foo(int n)
  switch (n)
  case 0: .. case 255:
    return 0;

    return 1;

Compiled with all optimisation (-O -inline -release) in DMD 2.055, I get the
following code generated (it's the same without optimisation, too):

00001bb4    pushl    %ebp
00001bb5    movl    %esp,%ebp
00001bb7    pushl    %eax
00001bb8    testl    %eax,%eax
00001bba    je    0x00002571
00001bc0    cmpl    $0x01,%eax
00001bc3    je    0x00002571
00001bc9    cmpl    $0x02,%eax
00001bcc    je    0x00002571
00001bd2    cmpl    $0x03,%eax
00001bd5    je    0x00002571
00001bdb    cmpl    $0x04,%eax
00001bde    je    0x00002571
00001be4    cmpl    $0x05,%eax
00001be7    je    0x00002571

... for all 256 values ...

0000255a    cmpl    $0x000000fd,%eax
0000255f    je    0x00002571
00002561    cmpl    $0x000000fe,%eax
00002566    je    0x00002571
00002568    cmpl    $0x000000ff,%eax
0000256d    je    0x00002571
0000256f    jmp    0x00002577
00002571    movl    %ebp,%esp
00002573    xorl    %eax,%eax
00002575    popl    %ebp
00002576    ret
00002577    movl    %ebp,%esp
00002579    movl    $0x00000001,%eax
0000257e    popl    %ebp
0000257f    ret

Essentially, it has generated a massive O(n) function for what should be a
couple of compares if not better.

Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 04 2011