Issue 174633
Summary Missed Optimization: For positive integers x and y, `x%y==0` implies `x>=y`.
Labels new issue
Assignees
Reporter madisonsilver
    For positive integers x and y, `(x%y==0)` implies `x>=y`, so `(x%y==0)&(x<y)` must be false.

Rust Source: https://rust.godbolt.org/z/EE3YcT686
Alive2: https://alive2.llvm.org/ce/z/fLU-H-

```llvm
define noundef zeroext i1 @src(i64 noundef range(i64 1, 0) %x, i64 noundef range(i64 1, 0) %y) unnamed_addr {
start:
  %_5 = urem i64 %x, %y
  %_4 = icmp eq i64 %_5, 0
 %_6 = icmp ult i64 %x, %y
  %_0 = and i1 %_6, %_4
  ret i1 %_0
}

define noundef zeroext i1 @tgt(i64 noundef range(i64 1, 0) %x, i64 noundef range(i64 1, 0) %y) unnamed_addr {
start:
    ret i1 0
}
```

----

Longer example:

Rust Source: https://rust.godbolt.org/z/hvxnhcEfh
Alive2: https://alive2.llvm.org/ce/z/2SkEms

```llvm
define noundef range(i8 0, 5) i8 @src(ptr noundef nonnull readonly align 1 captures(none) %data.0, i64 noundef %data.1, i64 noundef %chunk_size) unnamed_addr {
start:
  %0 = icmp eq i64 %chunk_size, 0
  br i1 %0, label %bb13, label %bb2

bb2:
  %1 = icmp eq i64 %data.1, 0
  br i1 %1, label %bb13, label %bb4

bb13:
 %_0.sroa.0.0 = phi i8 [ 0, %start ], [ 1, %bb2 ], [ %., %bb6 ], [ 3, %bb4 ]
 ret i8 %_0.sroa.0.0

bb4:
  %rem.i = urem i64 %data.1, %chunk_size
  %2 = icmp eq i64 %rem.i, 0
  br i1 %2, label %bb6, label %bb13

bb6:
  %_2.i = icmp ult i64 %data.1, %chunk_size ; This cannot be true, so we can simplify/remove this block
  %. = select i1 %_2.i, i8 4, i8 2
  br label %bb13
}

define noundef range(i8 0, 4) i8 @tgt(ptr noundef nonnull readonly align 1 captures(none) %data.0, i64 noundef %data.1, i64 noundef %chunk_size) unnamed_addr {
start:
  %0 = icmp eq i64 %chunk_size, 0
  br i1 %0, label %bb13, label %bb2

bb2:
  %1 = icmp eq i64 %data.1, 0
  br i1 %1, label %bb13, label %bb4

bb13:
  %_0.sroa.0.0 = phi i8 [ 0, %start ], [ 1, %bb2 ], [ %., %bb6 ], [ 3, %bb4 ]
  ret i8 %_0.sroa.0.0

bb4:
 %rem.i = urem i64 %data.1, %chunk_size
  %2 = icmp eq i64 %rem.i, 0
  br i1 %2, label %bb6, label %bb13

bb6:
  %. = select i1 0, i8 4, i8 2 ; Replacing %_2.i with 0 allows for further optimizations not shown here 
  br label %bb13
}
```

----

Related Rust issue: https://github.com/rust-lang/rust/issues/128077.  The linked issue has more examples: https://rust.godbolt.org/z/zbrK8fxGT
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to