[llvm] [InstCombine] Propagate NSW/NUW flags for `(X - Y) - Z -> X - (Y + Z)` (PR #72693)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 11:48:55 PST 2023


================
@@ -17,18 +17,42 @@ define i8 @t0(i8 %x, i8 %y, i8 %z) {
   ret i8 %r
 }
 
-; No flags are propagated
+; NSW/NUW flags are propagated
 define i8 @t1_flags(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @t1_flags(
-; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
+; CHECK-NEXT:    [[TMP1:%.*]] = add nuw nsw i8 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw nsw i8 [[X:%.*]], [[TMP1]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %o0 = sub nuw nsw i8 %x, %y
   %r = sub nuw nsw i8 %o0, %z
   ret i8 %r
 }
 
+; NUW flags are propagated
+define i8 @t1_flags_nuw_only(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @t1_flags_nuw_only(
+; CHECK-NEXT:    [[TMP1:%.*]] = add nuw i8 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = sub nuw i8 [[X:%.*]], [[TMP1]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %o0 = sub nuw i8 %x, %y
+  %r = sub nuw i8 %o0, %z
+  ret i8 %r
+}
+
+; Negative test: NSW flags cannot be propagated
+define i8 @t1_flags_nsw_only(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @t1_flags_nsw_only(
+; CHECK-NEXT:    [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
+; CHECK-NEXT:    ret i8 [[R]]
+;
+  %o0 = sub nsw i8 %x, %y
+  %r = sub nsw i8 %o0, %z
+  ret i8 %r
+}
----------------
nikic wrote:

should also test with nsw nuw on one instruction.

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


More information about the llvm-commits mailing list