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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 05:13:02 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
----------------
nikic wrote:

> @elhewaty `%and2` should be `and i16 %and, %y` :(

Yeah, sorry for the typo...

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

And by -1 is fully redundant, so always safe to drop without modifying flags.

> is this a valid `nuw nsw` case?

Yes, it's valid to use the same test case for nuw/nsw. Just varying the flag is enough, the and constant can stay the same.

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


More information about the llvm-commits mailing list