[llvm] r345988 - [ValueTracking] allow non-canonical shuffles when computing signbits

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 2 08:51:48 PDT 2018


Author: spatel
Date: Fri Nov  2 08:51:47 2018
New Revision: 345988

URL: http://llvm.org/viewvc/llvm-project?rev=345988&view=rev
Log:
[ValueTracking] allow non-canonical shuffles when computing signbits

This possibility is noted in D53987 for a different case,
so we need to adjust the existing code.

Modified:
    llvm/trunk/lib/Analysis/ValueTracking.cpp
    llvm/trunk/unittests/Analysis/ValueTrackingTest.cpp

Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=345988&r1=345987&r2=345988&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Fri Nov  2 08:51:47 2018
@@ -2511,27 +2511,29 @@ static unsigned ComputeNumSignBitsImpl(c
     // extended, shifted, etc).
     return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
 
-  case Instruction::ShuffleVector:
+  case Instruction::ShuffleVector: {
     // If the shuffle mask contains any undefined elements, that element of the
     // result is undefined. Propagating information from a source operand may
     // not be correct in that case, so just bail out.
     if (cast<ShuffleVectorInst>(U)->getMask()->containsUndefElement())
       break;
 
-    assert((!isa<UndefValue>(U->getOperand(0)) ||
-            !isa<UndefValue>(U->getOperand(1)))
-           && "Should have simplified shuffle with 2 undef inputs");
+    // If everything is undef, we can't say anything. This should be simplified.
+    Value *Op0 = U->getOperand(0), *Op1 = U->getOperand(1);
+    if (isa<UndefValue>(Op0) && isa<UndefValue>(Op1))
+      break;
 
     // Look through shuffle of 1 source vector.
-    if (isa<UndefValue>(U->getOperand(0)))
-      return ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
-    if (isa<UndefValue>(U->getOperand(1)))
-      return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
+    if (isa<UndefValue>(Op0))
+      return ComputeNumSignBits(Op1, Depth + 1, Q);
+    if (isa<UndefValue>(Op1))
+      return ComputeNumSignBits(Op0, Depth + 1, Q);
 
     // TODO: We can look through shuffles of 2 sources by computing the minimum
     // sign bits for each operand (similar to what we do for binops).
     break;
   }
+  }
 
   // Finally, if we can prove that the top bits of the result are 0's or 1's,
   // use this information.

Modified: llvm/trunk/unittests/Analysis/ValueTrackingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/ValueTrackingTest.cpp?rev=345988&r1=345987&r2=345988&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/ValueTrackingTest.cpp (original)
+++ llvm/trunk/unittests/Analysis/ValueTrackingTest.cpp Fri Nov  2 08:51:47 2018
@@ -494,6 +494,26 @@ TEST(ValueTracking, ComputeNumSignBits_P
   EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
 }
 
+// No guarantees for canonical IR in this analysis, so this just bails out. 
+TEST(ValueTracking, ComputeNumSignBits_Shuffle) {
+  StringRef Assembly = "define <2 x i32> @f() { "
+                       "  %val = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0> "
+                       "  ret <2 x i32> %val "
+                       "} ";
+
+  LLVMContext Context;
+  SMDiagnostic Error;
+  auto M = parseAssemblyString(Assembly, Error, Context);
+  assert(M && "Bad assembly?");
+
+  auto *F = M->getFunction("f");
+  assert(F && "Bad assembly?");
+
+  auto *RVal =
+      cast<ReturnInst>(F->getEntryBlock().getTerminator())->getOperand(0);
+  EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
+}
+
 TEST(ValueTracking, ComputeKnownBits) {
   StringRef Assembly = "define i32 @f(i32 %a, i32 %b) { "
                        "  %ash = mul i32 %a, 8 "




More information about the llvm-commits mailing list