<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 --- - Too strong optimization with -O2 generates invalid code"
   href="http://llvm.org/bugs/show_bug.cgi?id=20644">20644</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Too strong optimization with -O2 generates invalid code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sylvestre@debian.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code compiled with -O2:
clang++ -O2 -Wall -g -o segfault segfault.cpp && ./segfault 
is segfaulting with 3.4, 3.5 and trunk. gcc is fine.

Commenting some items in the switch/case or commenting the cout removes the
segfault.

LLDB is confused too. It seems that fractional is wrongly optimized:

Process 10217 launched: '/tmp/segfault' (x86_64)
0x2
00
Process 10217 stopped
* thread #1: tid = 10217, 0x0000000000400afb
segfault`InterPrediction::test(SampleYVector const&) [inlined]
getMargins(fractional=<unavailable>) + 6 at segault.cpp:47, name = 'segfault',
stop reason = invalid address (fault address: 0x1000400c08)
    frame #0: 0x0000000000400afb segfault`InterPrediction::test(SampleYVector
const&) [inlined] getMargins(fractional=<unavailable>) + 6 at segault.cpp:47
   44      void getMargins(const int fractional, int &topLeftMargin)
   45      {
   46          switch (fractional)
-> 47          {
   48              case 0:
   49                  topLeftMargin = 0;
   50                  break;
(lldb) p fractional
error: Couldn't materialize struct: size of variable fractional (4) disagrees
with the ValueObject's size (0)
Errored out in Execute, couldn't PrepareToExecuteJITExpression


------------------------
#include <iostream>

class SampleYVector
{

public:
    SampleYVector()
        :x(0)
        ,y(0)
        {}
    SampleYVector(int x_, int y_)
        :x(x_)
        ,y(y_)
        {}

    SampleYVector& operator&= (const int mask)
        {
            x &= mask;
            y &= mask;
            return *this;
        }

    int x;
    int y;
};

SampleYVector operator& (const SampleYVector &lhs, const int mask)
{
    SampleYVector temp(lhs);
    temp &= mask;
    return temp;
}

struct InterPrediction
{
public:

    int leftDataMargin;

    void test(const SampleYVector &mv);

};

void getMargins(const int fractional, int &topLeftMargin)
{
    switch (fractional)
    {
        case 0:
            topLeftMargin = 0;
            break;

        case 1:
            topLeftMargin = 3;
            break;

        case 2:
            topLeftMargin = 3;
            break;

        case 3:
            topLeftMargin = 2;
            break;

    }
}

void InterPrediction::test(const SampleYVector &mv)
{
    const SampleYVector mvFrac = mv & 3;
    std::cout << mvFrac.x << "x" << mvFrac.y << '\n';

    const int fractionalX = 0;
    std::cout << fractionalX << mvFrac.x << std::endl;

    getMargins(mvFrac.x, leftDataMargin);
}

int main()
{
    SampleYVector mv(0, 2);
    InterPrediction interPrediction;
    interPrediction.test(mv);
    return 0;
}
------------------------


Initially reported:
<a href="http://bugs.debian.org/758007">http://bugs.debian.org/758007</a></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>