LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 964 - switch lowering makes ugly code for a simple scanning loop
Summary: switch lowering makes ugly code for a simple scanning loop
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Common Code Generator Code (show other bugs)
Version: 1.8
Hardware: All All
: P normal
Assignee: Chris Lattner
URL:
Keywords: code-quality
Depends on:
Blocks:
 
Reported: 2006-10-22 14:07 PDT by Chris Lattner
Modified: 2010-02-22 12:41 PST (History)
1 user (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Lattner 2006-10-22 14:07:06 PDT
Consider this function (very similar to strhr):

void foo(unsigned char C);
const char *FindChar(const char *CurPtr) {
  unsigned char C;
  do
    C = *CurPtr++;
  while (C != 'x' && C != '\0');

  foo(C);
  return CurPtr;
}

We currently compile the loop to:

LBB1_1: #bb
        movb (%esi), %al
        incl %esi
        cmpb $119, %al
        jg LBB1_4       #bb
LBB1_3: #bb
        testb %al, %al
        je LBB1_2       #bb7
        jmp LBB1_1      #bb
LBB1_4: #bb
        cmpb $120, %al
        jne LBB1_1      #bb

It seems that switch lowering could produce something like:

        cmpb $120, %al
        je out
        testb %al, %al
        jnz LBB1_1       #bb7

which would be much faster.

-Chris
Comment 1 Chris Lattner 2006-10-22 14:11:43 PDT
GCC generates code very close to the desired code:

L3:
        movzbl  (%esi), %eax
        addl    $1, %esi
        cmpb    $120, %al
        je      L4
        testb   %al, %al
        jne     L3
Comment 2 Chris Lattner 2006-10-22 16:38:24 PDT
Implemented.  Testcase here: Regression/CodeGen/Generic/SwitchLowering.ll

Patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20061016/038932.html

We now compile the inner loop to:

LBB1_1: #bb
        movb (%esi), %al
        incl %esi
        testb %al, %al
        je LBB1_2       #bb7
LBB1_3: #bb
        cmpb $120, %al
        jne LBB1_1      #bb

-Chris