Re: Removing bound checks from a switch statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

Below is a short program that illustrates my problem. The variable R is of type enum reactions so the compiler should know that it does not need to check it's value in the switch statement on line 10. The assembler output shows that a check is nevertheless inserted. %ebx contains the value or R and .L9 the address of the jump table. Compiled with g++ -S -g -dA -O2 swloop.cc on a X86_64 machine.

Should not the type system ensure that R is within bounds?

Regards,
Henrik

<from the asm-output>
# swloop.cc:10
.loc 1 10 0
cmpl	$5, %ebx
ja	.L2
# basic block 4
mov	%ebx, %eax
jmp	*.L9(,%rax,8)
</from the asm-output>

enum reactions {R0 = 0, R1, R2, R3, R4, R5};

#include <cstdio>

int main() {
  const enum reactions next_reaction[] = {R1, R2, R3, R4, R5};
  enum reactions R = R0;

  while (R != R5) {
    switch (R) { /* This is line 10 */
    case R0:
      printf("R0\n");
      break;
    case R1:
      printf("R1\n");
      break;
    case R2:
      printf("R2\n");
      break;
    case R3:
      printf("R3\n");
      break;
    case R4:
      printf("R4\n");
      break;
    case R5:
      printf("R5\n");
      break;
    }
    R = next_reaction[R];
  }
}



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux