Issue 233785
Summary [InstCombine] Missed simplification of comparisons of PHIs with conditional predecessors
Labels new issue
Assignees
Reporter 134ARG
    In OpenSSL's `o_names_init_ossl_`, from `openssl/o_names.bc` in llvm-opt-benchmark, final `opt -O3` retains a pointer PHI followed by a null comparison (unrelated instructions omitted):

```llvm
5:
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %o_names_init.exit

8:
  tail call void @CRYPTO_THREAD_lock_free(ptr noundef %.pre1.i) #8
  store ptr null, ptr @obj_lock, align 8, !tbaa !11
 %.pre.i = load ptr, ptr @names_lh, align 8, !tbaa !8
  %9 = icmp ne ptr %.pre.i, null
  br label %o_names_init.exit

o_names_init.exit:
  %10 = phi ptr [ null, %8 ], [ %.pre1.i, %5 ]
  %11 = phi i1 [ %9, %8 ], [ true, %5 ]
  %12 = icmp ne ptr %10, null
  %13 = select i1 %11, i1 %12, i1 false
 %14 = zext i1 %13 to i32
  store i32 %14, ptr @o_names_init_ossl_ret_, align 4, !tbaa !12
  ret void
```

The result is `false` via block `%8` and `%.pre1.i != null` directly from block `%5`. LLVM could perform that null check in `%5` and use a boolean PHI to select its result or `false`, eliminating the reload of `@names_lh` and its comparison. The expected could be:

```llvm
5:
  %7 = icmp eq ptr %6, null
  %8 = icmp ne ptr %.pre1.i, null
  br i1 %7, label %9, label %o_names_init.exit

9:
  tail call void @CRYPTO_THREAD_lock_free(ptr noundef %.pre1.i) #8
  store ptr null, ptr @obj_lock, align 8, !tbaa !11
  br label %o_names_init.exit

o_names_init.exit:
  %10 = phi i1 [ false, %9 ], [ %8, %5 ]
  %11 = zext i1 %10 to i32
  store i32 %11, ptr @o_names_init_ossl_ret_, align 4, !tbaa !12
  ret void
```

A small standalone instance with the same pattern:

```llvm
define i1 @src(i1 noundef %cond, i64 noundef %left, i64 noundef %right) {
start:
  br i1 %cond, label %cond.true, label %cond.false

cond.true: ; preds = %start
  %cmp = icmp sle i64 %right, %left
 %same = icmp eq i64 %left, %right
  br i1 %same, label %end, label %cond.false

cond.false:                                       ; preds = %cond.true, %start
  %left_or_right = phi i64 [ %left, %start ], [ %right, %cond.true ]
  %false = icmp sgt i64 %left_or_right, %left
  br label %end

end:                                              ; preds = %cond.false, %cond.true
  %result = phi i1 [ false, %cond.true ], [ %false, %cond.false ]
  ret i1 %result
}
```

Expected:

```llvm
define i1 @tgt(i1 noundef %cond, i64 noundef %left, i64 noundef %right) {
start:
  %0 = icmp sgt i64 %right, %left
  %result = and i1 %cond, %0
  ret i1 %result
}
```

Godbolt: https://godbolt.org/z/nWYvKjKxT and alive2: https://alive2.llvm.org/ce/z/RelYAI 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to