public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Henrik Mannerström" <henrik.mannerstrom@helsinki.fi>
To: gcc-help@gcc.gnu.org
Subject: Re: Removing bound checks from a switch statement
Date: Wed, 17 Feb 2010 18:11:00 -0000	[thread overview]
Message-ID: <20100217195557.12445lqh1zsn1j1p@webmail.helsinki.fi> (raw)

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];
   }
}

             reply	other threads:[~2010-02-17 17:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-17 18:11 Henrik Mannerström [this message]
2010-02-17 19:00 ` Jeff Law
  -- strict thread matches above, loose matches on Subject: below --
2010-02-17 14:22 Henrik Mannerström
2010-02-18 14:44 ` Christoph Groth
2010-02-18 16:32   ` Henrik Mannerström

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100217195557.12445lqh1zsn1j1p@webmail.helsinki.fi \
    --to=henrik.mannerstrom@helsinki.fi \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).