<html>
    <head>
      <base href="https://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 --- - Very long compile time caused by unfolded constant select"
   href="https://llvm.org/bugs/show_bug.cgi?id=24680">24680</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Very long compile time caused by unfolded constant select
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rob.lougher@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Compiling the following test program with:

clang -O2 -emit-llvm -S test.cpp

Will cause the compiler to hang.

// ===================== test.cpp ==================================
typedef float __attribute__((ext_vector_type(16))) v16;
typedef long long __attribute__((ext_vector_type(8))) l8;

bool foo();

static v16 bar(v16 in) {
  for (unsigned i = 0; i < 16; ++i)
    if (foo())
      in[i] = 1;
  return in;
}

static v16 baz(v16 in) {
  for (unsigned i = 0; i < 16; ++i)
    if ((in[i]) > 1)
      in[i] += 1;
  return in;
}

v16 test518() {
  union {
    l8 l;
    v16 v;
  };
  l = (l8) {(long long)&baz};
  return bar(baz(v));
}
// =================================================================


The hang is not infinite as reducing the size of the loops allows
the compiler to complete.

Investigation of the hang indicates that it is caused by instruction
combining creating a huge constant select instruction (after inlining
and unrolling).  We start with an initial constant select which cannot be
folded - instruction combining then combines this with other instructions
to create a larger instruction which also cannot be folded.  This continues
leading to a massive tree.  The asm writer then hangs traversing this tree
when writing the select instruction (the bitcode writer similarly hangs
when enumerating values).

The size of the .ll file for loops of size N shows that the tree
increases rapidly as N increases:

1 60K
2 1.1M
3 19M
4 328M
5 5.8G
6 70+G

The reason the constant select cannot be folded is because it has
the address of a function embedded in it (although constant the value
is not known at compile time).  This can be seen in the smaller
example:

int test() {
  return ((long long)&test)&4 ? 0 : 1;
}

This generates:

define i32 @_Z4testv() {
entry:
  ret i32 select (i1 icmp ne (i64 and
     (i64 ptrtoint (i32 ()* @_Z4testv to i64), i64 4), i64 0), i32 0, i32 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>