[ValueTracking] recognize sub X, (X -nsw Y) as not overflowing
authorSanjay Patel <spatel@rotateright.com>
Tue, 14 Jun 2022 18:51:49 +0000 (14:51 -0400)
committerSanjay Patel <spatel@rotateright.com>
Tue, 14 Jun 2022 18:51:49 +0000 (14:51 -0400)
This extends a similar pattern from D125500.
If we know that operand 1 (RHS) of a subtract is itself a
non-overflowing subtract from operand 0 (LHS), then the
final/outer subtract is also non-overflowing:
https://alive2.llvm.org/ce/z/Bqan8v

InstCombine uses this analysis to trigger a narrowing
optimization, so that is what the first changed test shows.

The last test models the motivating case from issue #48013.
In that example, we determine 'nsw' on the first sub from
the srem, then we determine that the 2nd sub can be narrowed,
and that leads to eliminating both subtracts.

This works for unsigned sub too, but I left that out to keep
the patch minimal. If this looks ok, I will follow up with
that change. There are also several missing subtract narrowing
optimizations demonstrated in the tests above the diffs shown
here - those should be handled in InstCombine with another set
of patches.

Differential Revision: https://reviews.llvm.org/D127754

llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstCombine/sub.ll

index 521f516285a67ac08e4faf17cfb67e5745315448..a6c256f32dfa644f48debc3514cd82fa6d81dfcb 100644 (file)
@@ -4987,9 +4987,15 @@ OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
   // X - (X % ?)
   // The remainder of a value can't have greater magnitude than itself,
   // so the subtraction can't overflow.
-  if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) &&
-      isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT))
-    return OverflowResult::NeverOverflows;
+
+  // X - (X -nsw ?)
+  // In the minimal case, this would simplify to "?", so there's no subtract
+  // at all. But if this analysis is used to peek through casts, for example,
+  // then determining no-overflow may allow other transforms.
+  if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
+      match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
+    if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT))
+      return OverflowResult::NeverOverflows;
 
   // If LHS and RHS each have at least two sign bits, the subtraction
   // cannot overflow.
index 190d305e023fd3a160a1d5145f2a7188b3463262..3480a6a3313c10958a113d63f8a957412aca30ad 100644 (file)
@@ -1847,10 +1847,7 @@ define i32 @nuw_zext_zext_different_width(i8 %x, i7 %y) {
 
 define i16 @sext_nsw_noundef(i8 noundef %x, i8 %y) {
 ; CHECK-LABEL: @sext_nsw_noundef(
-; CHECK-NEXT:    [[D:%.*]] = sub nsw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[EX:%.*]] = sext i8 [[X]] to i16
-; CHECK-NEXT:    [[ED:%.*]] = sext i8 [[D]] to i16
-; CHECK-NEXT:    [[Z:%.*]] = sub nsw i16 [[EX]], [[ED]]
+; CHECK-NEXT:    [[Z:%.*]] = sext i8 [[Y:%.*]] to i16
 ; CHECK-NEXT:    ret i16 [[Z]]
 ;
   %d = sub nsw i8 %x, %y
@@ -1860,6 +1857,8 @@ define i16 @sext_nsw_noundef(i8 noundef %x, i8 %y) {
   ret i16 %z
 }
 
+; negative test - requires noundef
+
 define i16 @sext_nsw(i8 %x, i8 %y) {
 ; CHECK-LABEL: @sext_nsw(
 ; CHECK-NEXT:    [[D:%.*]] = sub nsw i8 [[X:%.*]], [[Y:%.*]]
@@ -1875,6 +1874,8 @@ define i16 @sext_nsw(i8 %x, i8 %y) {
   ret i16 %z
 }
 
+; negative test - requires nsw
+
 define i16 @sext_noundef(i8 noundef %x, i8 %y) {
 ; CHECK-LABEL: @sext_noundef(
 ; CHECK-NEXT:    [[D:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
@@ -1890,6 +1891,8 @@ define i16 @sext_noundef(i8 noundef %x, i8 %y) {
   ret i16 %z
 }
 
+; negative test - must have common operand
+
 define i16 @sext_nsw_noundef_wrong_val(i8 noundef %x, i8 noundef %y, i8 noundef %q) {
 ; CHECK-LABEL: @sext_nsw_noundef_wrong_val(
 ; CHECK-NEXT:    [[D:%.*]] = sub nsw i8 [[X:%.*]], [[Y:%.*]]
@@ -1905,13 +1908,12 @@ define i16 @sext_nsw_noundef_wrong_val(i8 noundef %x, i8 noundef %y, i8 noundef
   ret i16 %z
 }
 
+; two no-wrap analyses combine to allow reduction
+
 define i16 @srem_sext_noundef(i8 noundef %x, i8 %y) {
 ; CHECK-LABEL: @srem_sext_noundef(
 ; CHECK-NEXT:    [[R:%.*]] = srem i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[D:%.*]] = sub nsw i8 [[X]], [[R]]
-; CHECK-NEXT:    [[SD:%.*]] = sext i8 [[D]] to i16
-; CHECK-NEXT:    [[SX:%.*]] = sext i8 [[X]] to i16
-; CHECK-NEXT:    [[Z:%.*]] = sub nsw i16 [[SX]], [[SD]]
+; CHECK-NEXT:    [[Z:%.*]] = sext i8 [[R]] to i16
 ; CHECK-NEXT:    ret i16 [[Z]]
 ;
   %r = srem i8 %x, %y