[ValueTracking] Add support for X*X self-multiplication
authorSimon Pilgrim <llvm-dev@redking.me.uk>
Tue, 8 Feb 2022 13:33:18 +0000 (13:33 +0000)
committerSimon Pilgrim <llvm-dev@redking.me.uk>
Tue, 8 Feb 2022 13:33:27 +0000 (13:33 +0000)
D108992 added KnownBits handling for 'Quadratic Reciprocity' self-multiplication patterns (bit[1] == 0), which can be used for non-undef values (poison is OK).

This patch adds noundef selfmultiply handling to value tracking so demanded bits patterns can make use of it.

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

llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstCombine/mul-masked-bits.ll

index a93430c..9fe538d 100644 (file)
@@ -451,7 +451,11 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
     }
   }
 
-  Known = KnownBits::mul(Known, Known2);
+  bool SelfMultiply = Op0 == Op1;
+  // TODO: SelfMultiply can be poison, but not undef.
+  SelfMultiply &=
+      isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
+  Known = KnownBits::mul(Known, Known2, SelfMultiply);
 
   // Only make use of no-wrap flags if we failed to compute the sign bit
   // directly.  This matters if the multiplication always overflows, in
index 1914f9f..ead179f 100644 (file)
@@ -70,8 +70,7 @@ define <4 x i32> @combine_mul_self_demandedbits_vector(<4 x i32> %x) {
 ; CHECK-LABEL: @combine_mul_self_demandedbits_vector(
 ; CHECK-NEXT:    [[TMP1:%.*]] = freeze <4 x i32> [[X:%.*]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = mul <4 x i32> [[TMP1]], [[TMP1]]
-; CHECK-NEXT:    [[TMP3:%.*]] = and <4 x i32> [[TMP2]], <i32 -3, i32 -3, i32 -3, i32 -3>
-; CHECK-NEXT:    ret <4 x i32> [[TMP3]]
+; CHECK-NEXT:    ret <4 x i32> [[TMP2]]
 ;
   %1 = freeze <4 x i32> %x
   %2 = mul <4 x i32> %1, %1