[llvm-bugs] [Bug 44287] New: Redundant phi elimination

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 12 06:56:32 PST 2019


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

            Bug ID: 44287
           Summary: Redundant phi elimination
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

int comp(int max, int pos, unsigned char *cur)
{
        unsigned char *p = cur + pos;
        int len = 0;
        while (++len != max)
                if (p[len] != cur[len])
                        break;
        return cur[len];
}

Clang 9
define dso_local i32 @comp(i32, i32, i8* nocapture readonly) local_unnamed_addr
#0 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds i8, i8* %2, i64 %4
  %6 = zext i32 %0 to i64
  br label %7

7:                                                ; preds = %11, %3
  %8 = phi i64 [ %9, %11 ], [ 0, %3 ]
  %9 = add nuw nsw i64 %8, 1
  %10 = icmp eq i64 %9, %6
  br i1 %10, label %17, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, i8* %5, i64 %9
  %13 = load i8, i8* %12, align 1, !tbaa !2
  %14 = getelementptr inbounds i8, i8* %2, i64 %9
  %15 = load i8, i8* %14, align 1, !tbaa !2
  %16 = icmp eq i8 %13, %15
  br i1 %16, label %7, label %17

17:                                               ; preds = %11, %7
  %18 = and i64 %9, 4294967295
  %19 = getelementptr inbounds i8, i8* %2, i64 %18
  %20 = load i8, i8* %19, align 1, !tbaa !2
  %21 = zext i8 %20 to i32
  ret i32 %21
}

Clang 10
define dso_local i32 @comp(i32 %0, i32 %1, i8* nocapture readonly %2)
local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds i8, i8* %2, i64 %4
  %6 = zext i32 %0 to i64
  br label %7

7:                                                ; preds = %11, %3
  %8 = phi i64 [ %9, %11 ], [ 0, %3 ]
  %9 = add nuw nsw i64 %8, 1
  %10 = icmp eq i64 %9, %6
  br i1 %10, label %19, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, i8* %5, i64 %9
  %13 = load i8, i8* %12, align 1, !tbaa !2
  %14 = getelementptr inbounds i8, i8* %2, i64 %9
  %15 = load i8, i8* %14, align 1, !tbaa !2
  %16 = icmp eq i8 %13, %15
  br i1 %16, label %7, label %17

17:                                               ; preds = %11
  %18 = and i64 %9, 4294967295
  br label %19

19:                                               ; preds = %7, %17
  %20 = phi i64 [ %18, %17 ], [ %6, %7 ]
  %21 = getelementptr inbounds i8, i8* %2, i64 %20
  %22 = load i8, i8* %21, align 1, !tbaa !2
  %23 = zext i8 %22 to i32
  ret i32 %23
}


See new phi "%20 = phi i64 [ %18, %17 ], [ %6, %7 ]".

Codegen is worse a bit:

Clang 9
comp:                                   # @comp
        movsxd  rax, esi
        mov     r8d, edi
        add     rax, rdx
        mov     edi, 1
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        mov     rsi, rdi
        cmp     r8, rdi
        je      .LBB0_3
        movzx   ecx, byte ptr [rax + rsi]
        lea     rdi, [rsi + 1]
        cmp     cl, byte ptr [rdx + rsi]
        je      .LBB0_1
.LBB0_3:
        mov     eax, esi
        movzx   eax, byte ptr [rdx + rax]
        ret

Clang 10
comp:                                   # @comp
        movsxd  rax, esi
        mov     r8d, edi
        lea     r9, [rdx + 1]
        lea     rdi, [rdx + rax]
        add     rdi, 1
        lea     rax, [r8 - 1]
        xor     esi, esi
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        cmp     rax, rsi
        je      .LBB0_4
        movzx   ecx, byte ptr [rdi + rsi]
        cmp     cl, byte ptr [r9 + rsi]
        lea     rsi, [rsi + 1]
        je      .LBB0_1
        mov     r8d, esi
.LBB0_4:
        movzx   eax, byte ptr [rdx + r8]
        ret


https://godbolt.org/z/2zMDVf

-- 
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/20191212/272ae4d4/attachment.html>


More information about the llvm-bugs mailing list