<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 --- - LICM::sink has exponential complexity on certain inputs"
   href="http://llvm.org/bugs/show_bug.cgi?id=19835">19835</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LICM::sink has exponential complexity on certain inputs
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>eugeni.stepanov@gmail.com
          </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>$ cat 1.cc
#define Q(i, j) b##j = a##j + x; a##i = a##j * b##j;
#define D(i) int a##i; int b##i;

int f(int x) {
  D(0)
  D(1)
  D(2)
  D(3)
  D(4)
  D(5)
  D(6)
  D(7)
  D(8)
  D(9)

  for (int i = 0; i < 100; ++i) {
    int volatile z = x;
    int a0 = z;
    Q(1, 0)
    Q(2, 1)
    Q(3, 2)
    Q(4, 3)
    Q(5, 4)
    Q(6, 5)
    Q(7, 6)
    Q(8, 7)
    Q(9, 8)
  }
  return a9;
}

$ ./bin/clang++ -S -emit-llvm -O1 1.cc -o - |& grep '%.*=' | wc -l
1027

Playing with the number of macro calls in the above source, I see that the
number of instructions in the IR is 2**(N+1) + 3.

IR itself contains lots of duplicate instructions:

define i32 @_Z1fi(i32 %x) #0 {
entry:
  %z = alloca i32, align 4
  br label %for.body

for.body:                                         ; preds = %for.body, %entry
  %i.033 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  store volatile i32 %x, i32* %z, align 4
  %z.0.z.0. = load volatile i32* %z, align 4
  %inc = add nsw i32 %i.033, 1
  %exitcond = icmp eq i32 %inc, 100
  br i1 %exitcond, label %for.end, label %for.body

for.end:                                          ; preds = %for.body
  %add.le1275 = add nsw i32 %z.0.z.0., %x
  %add.le1273 = add nsw i32 %z.0.z.0., %x
  %add.le1271 = add nsw i32 %z.0.z.0., %x
  %add.le1269 = add nsw i32 %z.0.z.0., %x
  %add.le1267 = add nsw i32 %z.0.z.0., %x
  %add.le1265 = add nsw i32 %z.0.z.0., %x
  %add.le1263 = add nsw i32 %z.0.z.0., %x
  %add.le1261 = add nsw i32 %z.0.z.0., %x
  %add.le1259 = add nsw i32 %z.0.z.0., %x
  %add.le1257 = add nsw i32 %z.0.z.0., %x
  %add.le1255 = add nsw i32 %z.0.z.0., %x
  %add.le1253 = add nsw i32 %z.0.z.0., %x
  %add.le1251 = add nsw i32 %z.0.z.0., %x
  %add.le1249 = add nsw i32 %z.0.z.0., %x
  %add.le1247 = add nsw i32 %z.0.z.0., %x
  %add.le1245 = add nsw i32 %z.0.z.0., %x
  %add.le1243 = add nsw i32 %z.0.z.0., %x
  %add.le1241 = add nsw i32 %z.0.z.0., %x
  %add.le1239 = add nsw i32 %z.0.z.0., %x
  %add.le1237 = add nsw i32 %z.0.z.0., %x
  %add.le1235 = add nsw i32 %z.0.z.0., %x
  %add.le1233 = add nsw i32 %z.0.z.0., %x
...
  %add.le = add nsw i32 %z.0.z.0., %x
  %mul.le1019 = mul nsw i32 %add.le, %z.0.z.0.
  %mul.le1016 = mul nsw i32 %add.le1023, %z.0.z.0.
  %mul.le1013 = mul nsw i32 %add.le1025, %z.0.z.0.
  %mul.le1010 = mul nsw i32 %add.le1027, %z.0.z.0.
  %mul.le1007 = mul nsw i32 %add.le1029, %z.0.z.0.
  %mul.le1004 = mul nsw i32 %add.le1031, %z.0.z.0.
  %mul.le1001 = mul nsw i32 %add.le1033, %z.0.z.0.
  %mul.le998 = mul nsw i32 %add.le1035, %z.0.z.0.
  %mul.le995 = mul nsw i32 %add.le1037, %z.0.z.0.
  %mul.le992 = mul nsw i32 %add.le1039, %z.0.z.0.
  %mul.le989 = mul nsw i32 %add.le1041, %z.0.z.0.
  %mul.le986 = mul nsw i32 %add.le1043, %z.0.z.0.
  %mul.le983 = mul nsw i32 %add.le1045, %z.0.z.0.
  %mul.le980 = mul nsw i32 %add.le1047, %z.0.z.0.
  %mul.le977 = mul nsw i32 %add.le1049, %z.0.z.0.
  %mul.le974 = mul nsw i32 %add.le1051, %z.0.z.0.
  %mul.le971 = mul nsw i32 %add.le1053, %z.0.z.0.
...


The problem is in LICM::sink, which does not handle the following pattern well:

f = op g
e = op f, g
d = op e
c = op d, e
b = op c
a = op b, c

When an instruction with N uses is sunk, each of its operands get N new uses
(all of them - phi nodes). In the example above, if a had 1 use, c would have
2, e would have 4, and g would have 8.

This has regressed in r201148:
<a href="http://llvm.org/viewvc/llvm-project?rev=201148&view=rev">http://llvm.org/viewvc/llvm-project?rev=201148&view=rev</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>