Skip to content

Commit a68096c

Browse files
committed
[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. llvm-svn: 345988
1 parent 1005679 commit a68096c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,27 +2511,29 @@ static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
25112511
// extended, shifted, etc).
25122512
return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
25132513

2514-
case Instruction::ShuffleVector:
2514+
case Instruction::ShuffleVector: {
25152515
// If the shuffle mask contains any undefined elements, that element of the
25162516
// result is undefined. Propagating information from a source operand may
25172517
// not be correct in that case, so just bail out.
25182518
if (cast<ShuffleVectorInst>(U)->getMask()->containsUndefElement())
25192519
break;
25202520

2521-
assert((!isa<UndefValue>(U->getOperand(0)) ||
2522-
!isa<UndefValue>(U->getOperand(1)))
2523-
&& "Should have simplified shuffle with 2 undef inputs");
2521+
// If everything is undef, we can't say anything. This should be simplified.
2522+
Value *Op0 = U->getOperand(0), *Op1 = U->getOperand(1);
2523+
if (isa<UndefValue>(Op0) && isa<UndefValue>(Op1))
2524+
break;
25242525

25252526
// Look through shuffle of 1 source vector.
2526-
if (isa<UndefValue>(U->getOperand(0)))
2527-
return ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2528-
if (isa<UndefValue>(U->getOperand(1)))
2529-
return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2527+
if (isa<UndefValue>(Op0))
2528+
return ComputeNumSignBits(Op1, Depth + 1, Q);
2529+
if (isa<UndefValue>(Op1))
2530+
return ComputeNumSignBits(Op0, Depth + 1, Q);
25302531

25312532
// TODO: We can look through shuffles of 2 sources by computing the minimum
25322533
// sign bits for each operand (similar to what we do for binops).
25332534
break;
25342535
}
2536+
}
25352537

25362538
// Finally, if we can prove that the top bits of the result are 0's or 1's,
25372539
// use this information.

llvm/unittests/Analysis/ValueTrackingTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,26 @@ TEST(ValueTracking, ComputeNumSignBits_PR32045) {
494494
EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
495495
}
496496

497+
// No guarantees for canonical IR in this analysis, so this just bails out.
498+
TEST(ValueTracking, ComputeNumSignBits_Shuffle) {
499+
StringRef Assembly = "define <2 x i32> @f() { "
500+
" %val = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0> "
501+
" ret <2 x i32> %val "
502+
"} ";
503+
504+
LLVMContext Context;
505+
SMDiagnostic Error;
506+
auto M = parseAssemblyString(Assembly, Error, Context);
507+
assert(M && "Bad assembly?");
508+
509+
auto *F = M->getFunction("f");
510+
assert(F && "Bad assembly?");
511+
512+
auto *RVal =
513+
cast<ReturnInst>(F->getEntryBlock().getTerminator())->getOperand(0);
514+
EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
515+
}
516+
497517
TEST(ValueTracking, ComputeKnownBits) {
498518
StringRef Assembly = "define i32 @f(i32 %a, i32 %b) { "
499519
" %ash = mul i32 %a, 8 "

0 commit comments

Comments
 (0)