[llvm-bugs] [Bug 37985] New: Missed optimization opportunity due to select

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jun 29 02:15:24 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=37985

            Bug ID: 37985
           Summary: Missed optimization opportunity due to select
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: max.kazantsev at azul.com
                CC: llvm-bugs at lists.llvm.org

Given example:

const long long N = 100;
long long z = 2325;


long long foo(long long a, long long b) {
    return (a + b) * (a + b);
}

long long test() {
    long long sum = 0;

    for (long long i = 0; i < N; i++) {
      for (long long j = 0; j < N; j++) {
          sum += (i > 0);        // [1]
          sum += j;
          long long a = z;
          long b = i * j;
          sum += (-a * b) + foo(a, b) - b * a;
      }
    }
    return sum;
}

Compile with clang -O3 -emit-llvm -g0

define dso_local i64 @_Z4testv() local_unnamed_addr #1 {
  %1 = load i64, i64* @z, align 8, !tbaa !2
  %2 = mul i64 %1, %1
  %3 = mul i64 %2, 100
  %4 = add i64 %3, 4950
  br label %5

  %6 = phi i64 [ %4, %0 ], [ %20, %5 ]
  %7 = phi i64 [ 328350, %0 ], [ %19, %5 ]
  %8 = phi i64 [ 0, %0 ], [ %17, %5 ]
  %9 = phi i64 [ 0, %0 ], [ %18, %5 ]
  %10 = icmp eq i64 %9, 0
  %11 = select i1 %10, i64 0, i64 100
  %12 = add i64 %8, %6
  %13 = add i64 %12, %11
  %14 = add nuw nsw i64 %7, 656700
  %15 = add i64 %6, %7
  %16 = add i64 %13, %15
  %17 = add i64 %16, 100
  %18 = add nuw nsw i64 %9, 2
  %19 = add nuw nsw i64 %7, 1313400
  %20 = add i64 %15, %14
  %21 = icmp eq i64 %18, 100
  br i1 %21, label %22, label %5

  ret i64 %17
}

Clang in unable to constant-fold calculations in this case: the code contains a
loop. However if we comment away line [1], the code becomes folded:

If we comment away [1]:

define dso_local i64 @_Z4testv() local_unnamed_addr #1 {
  %1 = load i64, i64* @z, align 8, !tbaa !2
  %2 = mul i64 %1, %1
  %3 = mul i64 %2, 10000
  %4 = add i64 %3, 107814217500
  ret i64 %4
}

To me it looks like a failure of peeling heuristic: if we could peel away 1st
iteration of the inner loop, we could optimize the entire thing away.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180629/b995688a/attachment.html>


More information about the llvm-bugs mailing list