<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Bitcast of <4 x i1> to i4 gives unexpected result"
   href="http://llvm.org/bugs/show_bug.cgi?id=15215">15215</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Bitcast of <4 x i1> to i4 gives unexpected result
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.2
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>other
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kai.trojahner@rtt.ag
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=9985" name="attach_9985" title="Demonstrator">attachment 9985</a> <a href="attachment.cgi?id=9985&action=edit" title="Demonstrator">[details]</a></span>
Demonstrator

On x86, the following two LLVM IR functions give different results.

define i32 @bad(<4 x i32> %input) {
entry:
  %0 = trunc <4 x i32> %input to <4 x i1>
  %1 = bitcast <4 x i1> %0 to i4
  %2 = zext i4 %1 to i32
  ret i32 %2
}

define i32 @good(<4 x i32> %input) {
entry:
  %0 = trunc <4 x i32> %input to <4 x i1>
  %1 = extractelement <4 x i1> %0, i32 0
  %e1 = select i1 %1, i32 1, i32 0
  %2 = extractelement <4 x i1> %0, i32 1
  %e2 = select i1 %2, i32 2, i32 0
  %3 = extractelement <4 x i1> %0, i32 2
  %e3 = select i1 %3, i32 4, i32 0
  %4 = extractelement <4 x i1> %0, i32 3
  %e4 = select i1 %4, i32 8, i32 0
  %5 = or i32 %e1, %e2
  %6 = or i32 %5, %e3
  %7 = or i32 %6, %e4
  ret i32 %7
}

A demonstrator program illustrates the effect:

#include <emmintrin.h>
#include <stdio.h>

extern int bad(__m128i input);
extern int good(__m128i input);

int main() {
  int i;
  __m128i input[16];

  input[0] = _mm_set_epi32(0, 0, 0, 0);
  input[1] = _mm_set_epi32(0, 0, 0, -1);
  input[2] = _mm_set_epi32(0, 0, -1, 0);
  input[3] = _mm_set_epi32(0, 0, -1, -1);
  input[4] = _mm_set_epi32(0, -1, 0, 0);
  input[5] = _mm_set_epi32(0, -1, 0, -1);
  input[6] = _mm_set_epi32(0, -1, -1, 0);
  input[7] = _mm_set_epi32(0, -1, -1, -1);
  input[8] = _mm_set_epi32(-1, 0, 0, 0);
  input[9] = _mm_set_epi32(-1, 0, 0, -1);
  input[10]= _mm_set_epi32(-1, 0, -1, 0);
  input[11]= _mm_set_epi32(-1, 0, -1, -1);
  input[12]= _mm_set_epi32(-1, -1, 0, 0);
  input[13]= _mm_set_epi32(-1, -1, 0, -1);
  input[14]= _mm_set_epi32(-1, -1, -1, 0);
  input[15]= _mm_set_epi32(-1, -1, -1, -1);

  for (i=0; i<16; i++) {
    printf("permutation %i, good: %x: bad: %x\n",
           i, good(input[i]), bad(input[i]));
  }
  return 0;
}

On my machine (Xeon 5160, SSE2, Windows 7, Cygwin, demonstrator compiled with
MSVC), the program yields:

permutation 0, good: 0: bad: 0
permutation 1, good: 1: bad: 0
permutation 2, good: 2: bad: 1
permutation 3, good: 3: bad: 1
permutation 4, good: 4: bad: 0
permutation 5, good: 5: bad: 0
permutation 6, good: 6: bad: 1
permutation 7, good: 7: bad: 1
permutation 8, good: 8: bad: 0
permutation 9, good: 9: bad: 0
permutation 10, good: a: bad: 1
permutation 11, good: b: bad: 1
permutation 12, good: c: bad: 0
permutation 13, good: d: bad: 0
permutation 14, good: e: bad: 1
permutation 15, good: f: bad: 1

Even more bizarrely, on a different machine (Xeon W3565, SSE4.1, Linux,
demonstrator compiled with GCC), the program yields:

permutation 0, good: 0: bad: 0
permutation 1, good: 1: bad: 1
permutation 2, good: 2: bad: 0
permutation 3, good: 3: bad: 1
permutation 4, good: 4: bad: 0
permutation 5, good: 5: bad: 1
permutation 6, good: 6: bad: 0
permutation 7, good: 7: bad: 1
permutation 8, good: 8: bad: 0
permutation 9, good: 9: bad: 1
permutation 10, good: a: bad: 0
permutation 11, good: b: bad: 1
permutation 12, good: c: bad: 0
permutation 13, good: d: bad: 1
permutation 14, good: e: bad: 0
permutation 15, good: f: bad: 1</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>