llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Redundant phi elimination

Open davidbolvansky opened this issue 6 years ago • 5 comments

Bugzilla Link 44287
Version trunk
OS Linux
CC @topperc,@rotateright,@vdsered

Extended Description

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

davidbolvansky avatar Dec 12 '19 14:12 davidbolvansky

I ran the current version of opt and opt 9.0.0 for the generated IR with -print-after-all -O3

I found roughly the same parts of the log where phi is eliminated in opt 9.0.0, but left as-is for the current opt.

Opt (trunk) https://godbolt.org/z/eceWEd9qT

At the beginning, we have %inc.lcssa = phi i32 [ %inc, %while.body ], [ %inc, %while.cond ], the value %inc is the same for each pair of value + label (see line 22). Then, IndVarSimplifyPass (starts at line 64) transforms it to %inc.lcssa = phi i32 [ %1, %while.body ], [ %max, %while.cond ] (see line 94). As a result, GVN failed to eliminate the phi function (starts at line 230).

Opt (9.0.0) https://godbolt.org/z/PfdndKa9n

We have the same phi function at start (see line 22). Then, Induction Variable Simplification (starts at line 29) transforms it to %inc.lcssa = phi i32 [ %1, %while.body ], [ %1, %while.cond ] (the values are equal). It is left untouched until GVN (starts at line 202) where the phi function is eliminated and we have just %idxprom5 = zext i32 %1 to i64 (line 224).

I'm not sure, but I think, this is the root of the problem

dseredkin avatar May 27 '21 17:05 dseredkin

I generated IR representation from the original C++ code in order to simplify the problem a little bit https://godbolt.org/z/Y7sboPorE

dseredkin avatar May 27 '21 16:05 dseredkin

and if we change condition to: while (++len <= max)

We have: 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 = call i32 @​llvm.smax.i32(i32 %0, i32 0) %7 = add nuw i32 %6, 1 %8 = zext i32 %6 to i64 br label %9

I dont know why we generate smax at all, there was no final decision to canonicalize select pattern to min/max intrinsic..

davidbolvansky avatar May 16 '21 23:05 davidbolvansky

https://godbolt.org/z/j8MYf3Mqf It is neither better for clang 12

dseredkin avatar May 16 '21 11:05 dseredkin

assigned to @vdsered

davidbolvansky avatar Dec 12 '19 14:12 davidbolvansky