[LLVMbugs] [Bug 14708] New: Optimize `c == 'a' || c == 'A';`

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Dec 24 16:43:19 PST 2012


http://llvm.org/bugs/show_bug.cgi?id=14708

             Bug #: 14708
           Summary: Optimize `c == 'a' || c == 'A';`
           Product: new-bugs
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: silvas at purdue.edu
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified


Currently, we codegen:

int is_an_a(int c) {
    return c == 'a' || c == 'A';
}

as

0000000000000000 <is_an_a>:
   0:   83 ff 41                cmp    $0x41,%edi
   3:   0f 94 c0                sete   %al
   6:   83 ff 61                cmp    $0x61,%edi
   9:   0f 94 c1                sete   %cl
   c:   08 c1                   or     %al,%cl
   e:   0f b6 c1                movzbl %cl,%eax
  11:   c3                      retq

It would be better to transform it into:

int is_an_a2(int c) {
    return (c &~ ' ') == 'A';
}

which codegens as:

0000000000000000 <is_an_a2>:
   0:   83 e7 df                and    $0xffffffdf,%edi
   3:   83 ff 41                cmp    $0x41,%edi
   6:   0f 94 c0                sete   %al
   9:   0f b6 c0                movzbl %al,%eax
   c:   c3                      retq

Which is 13 bytes (vs. 18) and has only 1 SETcc (vs. 2).

These two are equivalent <http://rise4fun.com/Z3/sBea>.

This might be a special case of a more general optimization, but I can't tell
off the top of my head.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list