This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/54491] New: interval membership optimization


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54491

             Bug #: 54491
           Summary: interval membership optimization
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: neleai@seznam.cz


A common optimization to test range membership b \in [c,d] is to use integer
overflow as in function range below. 

gcc does this optimization when both c and d are constants. However inlining
disables this optimization. 

Same optimization can be automatically applied for c arbitrary expression and 
d=c+positive_constant 


unsigned int range(unsigned int b,unsigned int c){
  if (b-c<=42) return c;
  return b;
}
unsigned int foo(unsigned int b,unsigned int c){
  if (c<=b && b <=42+c) return c;
  return b;
}
unsigned int bar(unsigned int b){
  return foo(b,42);
}
unsigned int baz(unsigned int b){
  if (42<=b && b<=84) return 42;
  return b;
}

 .file "a.c"
  .text
  .p2align 4,,15
  .globl  range
  .type range, @function
range:
.LFB0:
  .cfi_startproc
  movl  %edi, %eax
  subl  %esi, %eax
  cmpl  $42, %eax
  movl  %esi, %eax
  cmova %edi, %eax
  ret
  .cfi_endproc
.LFE0:
  .size range, .-range
  .p2align 4,,15
  .globl  foo
  .type foo, @function
foo:
.LFB1:
  .cfi_startproc
  cmpl  %edi, %esi
  movl  %edi, %eax
  ja  .L5
  leal  42(%rsi), %edx
  cmpl  %edx, %edi
  cmovbe  %esi, %eax
.L5:
  rep; ret
  .cfi_endproc
.LFE1:
  .size foo, .-foo
  .p2align 4,,15
  .globl  bar
  .type bar, @function
bar:
.LFB2:
  .cfi_startproc
  cmpl  $41, %edi
  movl  %edi, %eax
  jbe .L10
  cmpl  $85, %edi
  movl  $42, %edx
  cmovb %edx, %eax
.L10:
  rep; ret

  .cfi_endproc
.LFE2:
  .size bar, .-bar
  .p2align 4,,15
  .globl  baz
  .type baz, @function
baz:
.LFB3:
  .cfi_startproc
  leal  -42(%rdi), %edx
  movl  $42, %eax
  cmpl  $42, %edx
  cmova %edi, %eax
  ret
  .cfi_endproc
.LFE3:
  .size baz, .-baz
  .ident  "GCC: (Debian 20120820-1) 4.8.0 20120820 (experimental) [trunk
revision 190537]"
  .section  .note.GNU-stack,"",@progbits


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]