[llvm] [IR] Add nowrap flags for trunc instruction (PR #85592)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 04:49:13 PDT 2024


================
@@ -1021,3 +1021,37 @@ define i16 @PR44545(i32 %t0, i32 %data) {
   %sub = add nsw i16 %cast, -1
   ret i16 %sub
 }
+
+; Make sure that SimplifyDemandedBits drops the nowrap flags
+define i16 @drop_nsw_trunc(i32 %x) {
+; CHECK-LABEL: @drop_nsw_trunc(
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[X:%.*]] to i16
+; CHECK-NEXT:    [[B:%.*]] = and i16 [[TMP1]], -2
+; CHECK-NEXT:    ret i16 [[B]]
+;
+  %t = and i32 %x, -2
+  %b = trunc nsw i32 %t to i16
----------------
elhewaty wrote:

@dtcxzyw 

> What I understand from your example, is that SimplifyDemandedBits will drop the flag because it may be caused by the %and instruction as it preserves the least 8 bits it assumes that the flag exists because of this %and. given that the %and got eliminated then it's okay now to drop the flag.

What do you think of this?

> For the NSW test. Should the NSW test be similar to this?
> 
> ```
> define i8 @test_nsw(i16 %x, i16 %y) {
>     %and = and i16 %x, -1 
>     %and2 = and i16 %and, %y
>     %res = trunc nsw i16 %and2 to i8
>     ret i8 %res
> }
> ```

Assume we change %x to %and, why it works with -2 and not working with -1?

is this a valid `nuw nsw` case?

```
define i8 @drop_both_trunc(i16 %x, i16 %y) {
  %and = and i16 %x, 255
  %and2 = and i16 %and, %y
  %res = trunc nuw nsw i16 %and2 to i8
  ret i8 %res
}
```



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


More information about the llvm-commits mailing list