<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 --- - Performance regression in simple linear search"
   href="https://llvm.org/bugs/show_bug.cgi?id=25268">25268</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Performance regression in simple linear search
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.7
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>chris@detrino.org
          </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>struct Ret {
  std::size_t index;
  std::size_t pos;
};

Ret Test1(std::size_t const *array, std::size_t pos) {
  std::size_t index = 0;
  while (true) {
    auto next = array[index];
    if (pos < next) break;
    pos -= next;
    ++index;
  }
  return {index, pos};
}

This generates the following IR with clang 3.7 -O2 -S -c -std=c++11 -emit-llvm:

; Function Attrs: nounwind readonly uwtable
define { i64, i64 } @_Z5Test1PKmm(i64* nocapture readonly %array, i64 %pos) #3
{
  br label %1

; <label>:1                                       ; preds = %1, %0
  %index.0 = phi i64 [ 0, %0 ], [ %index.1, %1 ]
  %.01 = phi i64 [ %pos, %0 ], [ %.1, %1 ]
  %2 = getelementptr inbounds i64, i64* %array, i64 %index.0
  %3 = load i64, i64* %2, align 8, !tbaa !1
  %4 = icmp ult i64 %.01, %3
  %5 = zext i1 %4 to i64
  %6 = xor i64 %5, 1
  %index.1 = add i64 %6, %index.0
  %7 = select i1 %4, i64 0, i64 %3
  %.1 = sub i64 %.01, %7
  br i1 %4, label %8, label %1

; <label>:8                                       ; preds = %1
  %.1.lcssa = phi i64 [ %.1, %1 ]
  %index.1.lcssa = phi i64 [ %index.1, %1 ]
  %9 = insertvalue { i64, i64 } undef, i64 %index.1.lcssa, 0
  %10 = insertvalue { i64, i64 } %9, i64 %.1.lcssa, 1
  ret { i64, i64 } %10
}

And using Clang 3.5 -O2 -S -c -std=c++11 -emit-llvm:

; Function Attrs: nounwind readonly uwtable
define { i64, i64 } @_Z5Test1PKmm(i64* nocapture readonly %array, i64 %pos) #3
{
  %1 = load i64* %array, align 8, !tbaa !1
  %2 = icmp ugt i64 %1, %pos
  br i1 %2, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader,
%.lr.ph
  %3 = phi i64 [ %7, %.lr.ph ], [ %1, %.lr.ph.preheader ]
  %.02 = phi i64 [ %4, %.lr.ph ], [ %pos, %.lr.ph.preheader ]
  %index.01 = phi i64 [ %5, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %4 = sub i64 %.02, %3
  %5 = add i64 %index.01, 1
  %6 = getelementptr inbounds i64* %array, i64 %5
  %7 = load i64* %6, align 8, !tbaa !1
  %8 = icmp ult i64 %4, %7
  br i1 %8, label %._crit_edge.loopexit, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.lcssa6 = phi i64 [ %5, %.lr.ph ]
  %.lcssa = phi i64 [ %4, %.lr.ph ]
  br label %._crit_edge

._crit_edge:                                      ; preds =
%._crit_edge.loopexit, %0
  %.0.lcssa = phi i64 [ %pos, %0 ], [ %.lcssa, %._crit_edge.loopexit ]
  %index.0.lcssa = phi i64 [ 0, %0 ], [ %.lcssa6, %._crit_edge.loopexit ]
  %9 = insertvalue { i64, i64 } undef, i64 %index.0.lcssa, 0
  %10 = insertvalue { i64, i64 } %9, i64 %.0.lcssa, 1
  ret { i64, i64 } %10
}

I have measured 3.7 to be 7x slower than 3.5 on my computer for this function.

This is a reduced testcase from a B+Tree library that caused the overall
implementation to be 2x slower.

I can recover the same assembly using 3.7 that I get in 3.5 by writing it this
way instead:

Ret Test2(std::size_t const *array, std::size_t pos) {
  std::size_t index = 0;
  auto next = array[index];
  while (pos >= next) {
    pos -= next;
    ++index;
    next = array[index];
  }
  return {index, pos};
}

The problem still exists in trunk.</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>