[llvm] [LVI] Learn value ranges from ctpop results (PR #121945)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 01:30:02 PST 2025


================
@@ -0,0 +1,138 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=correlated-propagation %s | FileCheck %s
+
+declare void @use(i1)
+
+define void @ctpop1(i8 %v) {
+; CHECK-LABEL: define void @ctpop1(
+; CHECK-SAME: i8 [[V:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[RES:%.*]] = call range(i8 0, 9) i8 @llvm.ctpop.i8(i8 [[V]])
+; CHECK-NEXT:    [[C0_0:%.*]] = icmp samesign uge i8 [[RES]], 3
+; CHECK-NEXT:    [[C0_1:%.*]] = icmp samesign ule i8 [[RES]], 7
+; CHECK-NEXT:    [[C0:%.*]] = and i1 [[C0_0]], [[C0_1]]
+; CHECK-NEXT:    br i1 [[C0]], label %[[RANGE_3_8:.*]], label %[[ED:.*]]
+; CHECK:       [[RANGE_3_8]]:
+; CHECK-NEXT:    call void @use(i1 true)
+; CHECK-NEXT:    call void @use(i1 false)
+; CHECK-NEXT:    call void @use(i1 true)
+; CHECK-NEXT:    call void @use(i1 false)
+; CHECK-NEXT:    ret void
+; CHECK:       [[ED]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %res = call range(i8 0, 9) i8 @llvm.ctpop.i8(i8 %v)
+  %c0.0 = icmp uge i8 %res, 3
+  %c0.1 = icmp ule i8 %res, 7
+  %c0 = and i1 %c0.0, %c0.1
+  br i1 %c0, label %range.3.8, label %ed
+
+range.3.8:
+  %cmp0 = icmp uge i8 %v, 7
+  call void @use(i1 %cmp0) ; true
+  %cmp1 = icmp ult i8 %v, 7
+  call void @use(i1 %cmp1) ; false
----------------
nikic wrote:

```suggestion
  %cmp1 = icmp uge i8 %v, 8
  call void @use(i1 %cmp1)
```
For these sorts of tests, it's best to check two adjacent values where one folds and one doesn't. This allows you to test where exactly the boundary is. Otherwise it's not clear that the range is actually correct.

https://github.com/llvm/llvm-project/pull/121945


More information about the llvm-commits mailing list