[LLVMbugs] [Bug 12084] New: Bad optimization of numeric literal masks

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Feb 25 23:16:01 PST 2012


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

             Bug #: 12084
           Summary: Bad optimization of numeric literal masks
           Product: clang
           Version: trunk
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: release blocker
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: rokicki at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified


The version of clang I am using is that sent with XCode 4.3; it
reports itself as

Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix

The following code compiles correctly without optimization, but does
not compile correctly with any level of optimization from -O1 up:


#include <iostream>
using namespace std ;
int countbits(int w2, int w3) {
   const int mask1 = 0x55555555 ;
   const int mask2 = 0x33333333 ;
   w2 = ((w2 >> 1) & mask1) + (w3 & mask1) + ((w3 >> 1) & mask1) ;
   return (w2 & mask2) + ((w2 >> 2) & mask2) ;
}
int main(int argc, char *argv[]) {
   cout << countbits(-1, -1) << endl ;
}

The problem can be seen directly in the assembly output; one of
the immediate constants is incorrect.  Also, it returns the
incorrect value of course due to this bad code.

The correct output:

solarium:cube20 rokicki$ c++ -o bc8 bc8.cpp ; ./bc8
1717986918

The incorrect output:

solarium:cube20 rokicki$ c++ -O1 -o bc8 bc8.cpp ; ./bc8
1181116006

The g++ compiler works fine for this case.

This is derived from the longer routine attached here, which
exhibits similar behavior (it's a bit count routine for a
large region of memory.  I know there's a popcnt instruction.)


#include <iostream>
#include <cstring>
using namespace std ;
const int PAGESIZE = 40320 * 24 / 2 / 8 ;
int b[PAGESIZE] ;
int countbits(int *a) {
   int r = 0 ;
   const int mask1 = 0x55555555 ;
   const int mask2 = 0x33333333 ;
   const int mask3 = 0x0f0f0f0f ;
   for (int i=0; i<PAGESIZE; i += 24) {
      int w1 = *a++ ;
      int w2 = *a++ ;
      int w3 = *a++ ;
      w1 = (w1 & mask1) + ((w1 >> 1) & mask1) + (w2 & mask1) ;
      w2 = ((w2 >> 1) & mask1) + (w3 & mask1) + ((w3 >> 1) & mask1) ;
      int s1 = (w1 & mask2) + ((w1 >> 2) & mask2) +
               (w2 & mask2) + ((w2 >> 2) & mask2) ;
      s1 = (s1 & mask3) + ((s1 >> 4) & mask3) ;
      w1 = *a++ ;
      w2 = *a++ ;
      w3 = *a++ ;
      w1 = (w1 & mask1) + ((w1 >> 1) & mask1) + (w2 & mask1) ;
      w2 = ((w2 >> 1) & mask1) + (w3 & mask1) + ((w3 >> 1) & mask1) ;
      int s2 = (w1 & mask2) + ((w1 >> 2) & mask2) +
               (w2 & mask2) + ((w2 >> 2) & mask2) ;
      s1 += (s2 & mask3) + ((s2 >> 4) & mask3) ;
      r += 255 & ((s1 >> 24) + (s1 >> 16) + (s1 >> 8) + s1) ;
   }
   return r ;
}
int main(int argc, char *argv[]) {
   memset(b, -1, sizeof(b)) ;
   cout << countbits(b) << endl ;
}

-- 
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