[llvm-bugs] [Bug 33885] New: Useless PHI nodes created prevent constant folding

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jul 21 18:03:20 PDT 2017


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

            Bug ID: 33885
           Summary: Useless PHI nodes created prevent constant folding
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: davide at freebsd.org
                CC: llvm-bugs at lists.llvm.org

/home/davide/work/llvm-project-20170507/build-release/bin/clang -O2 -flto
golden.c -o golden.o -c
/home/davide/work/llvm-project-20170507/build-release/bin/clang -O2 -flto fib.c
-o fib.o -c
/home/davide/work/llvm-project-20170507/build-release/bin/clang
-fuse-ld=/home/davide/work/llvm-project-20170507/build-release/bin/ld.lld
-Wl,-save-temps golden.o fib.o -o patatino
/home/davide/work/llvm-project-20170507/build-release/bin/llvm-dis
patatino.0.4.opt.bc -o -

$ cat golden.c

#include <stdio.h>
extern int fib(int);

double approx_gr() {
  return (double)fib(10) / fib(9);
}

int main() {
  printf("%lf\n", approx_gr());
  return 0;
}

$ cat fib.c

int fib(int n) {
  int a=0, b=1, t=0;

  while (n--) {
    t = b;
    b = b + a;
    a = t;
  }
  return b;
}

The change triggering this is:

commit 5afb2ffdac5bb150a503e6ddbb9df6a7e486afaf
Author: Evgeny Stupachenko <evstupac at gmail.com>
Date:   Thu Mar 2 17:38:46 2017 +0000

    The patch turns on epilogue unroll for loops with constant recurency start.

The optimized bitcode reverting the change is:

define i32 @main() local_unnamed_addr #0 {
fib.exit17:
  %0 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8],
[5 x i8]* @.str, i64
0, i64 0), double 0x3FF9E4129E4129E4)
  ret i32 0
}


while ToT looks like:

define i32 @main() local_unnamed_addr #0 {
  br label %1

; <label>:1:                                      ; preds = %1, %0
  %2 = phi i32 [ %5, %1 ], [ 34, %0 ]
  %3 = phi i32 [ %2, %1 ], [ 21, %0 ]
  %4 = phi i32 [ %6, %1 ], [ 2, %0 ]
  %5 = add nsw i32 %2, %3
  %6 = add nsw i32 %4, -1
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %fib.exit, label %1, !llvm.loop !2

fib.exit:                                         ; preds = %1
  %8 = sitofp i32 %5 to double
  %9 = fdiv double %8, 5.500000e+01
  %10 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8],
[5 x i8]* @.str, i64
0, i64 0), double %9)
  ret i32 0
}

-- 
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/20170722/28273db3/attachment-0001.html>


More information about the llvm-bugs mailing list