https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82404

            Bug ID: 82404
           Summary: Unnecessary instructions in switch table
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Following code

enum class eShape { eSquare, eCircle, eShpere, eTetraeder };

double test_switch_native(eShape shape, double r) {
    switch(shape) {
    case eShape::eSquare:    return 17;
    case eShape::eCircle:    return 16;
    case eShape::eShpere:    return 21;
    case eShape::eTetraeder: return 0;
    }
}

with flags -O2 and -O3 produces assembly:

test_switch_native(eShape, double):
  cmp edi, 3       <== do not need this
  ja .L2           <== do not need this
  mov edi, edi     <== do not need this
  movsd xmm0, QWORD PTR CSWTCH.0[0+rdi*8]
  ret
.L2:
  rep ret          <== do not need this
CSWTCH.0:
  .long 0
  .long 1076953088
  .long 0
  .long 1076887552
  .long 0
  .long 1077215232
  .long 0
  .long 0


Clang produces much sorter assembly:

test_switch_native(eShape, double):         # @test_switch_native(eShape,
double)
        movsxd  rax, edi
        movsd   xmm0, qword ptr [8*rax +
.Lswitch.table._Z18test_switch_native6eShaped] # xmm0 = mem[0],zero
        ret
.Lswitch.table._Z18test_switch_native6eShaped:
        .quad   4625478292286210048     # double 17
        .quad   4625196817309499392     # double 16
        .quad   4626604192193052672     # double 21
        .quad   0                       # double 0

Reply via email to