Skip to content

Commit 6ef970b

Browse files
committed
[ValueTracking] Consistently propagate DemandedElts is computeKnownBits
1 parent 1dfbd07 commit 6ef970b

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,23 +1091,23 @@ static void computeKnownBitsFromOperator(const Operator *I,
10911091
break;
10921092
}
10931093
case Instruction::UDiv: {
1094-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1095-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1094+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1095+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
10961096
Known =
10971097
KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
10981098
break;
10991099
}
11001100
case Instruction::SDiv: {
1101-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1102-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1101+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1102+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
11031103
Known =
11041104
KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
11051105
break;
11061106
}
11071107
case Instruction::Select: {
11081108
auto ComputeForArm = [&](Value *Arm, bool Invert) {
11091109
KnownBits Res(Known.getBitWidth());
1110-
computeKnownBits(Arm, Res, Depth + 1, Q);
1110+
computeKnownBits(Arm, DemandedElts, Res, Depth + 1, Q);
11111111
adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Depth, Q);
11121112
return Res;
11131113
};
@@ -1142,7 +1142,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
11421142

11431143
assert(SrcBitWidth && "SrcBitWidth can't be zero");
11441144
Known = Known.anyextOrTrunc(SrcBitWidth);
1145-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1145+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
11461146
if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
11471147
Inst && Inst->hasNonNeg() && !Known.isNegative())
11481148
Known.makeNonNegative();
@@ -1164,7 +1164,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
11641164
if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
11651165
V->getType()->isFPOrFPVectorTy()) {
11661166
Type *FPType = V->getType()->getScalarType();
1167-
KnownFPClass Result = computeKnownFPClass(V, fcAllFlags, Depth + 1, Q);
1167+
KnownFPClass Result =
1168+
computeKnownFPClass(V, DemandedElts, fcAllFlags, Depth + 1, Q);
11681169
FPClassTest FPClasses = Result.KnownFPClasses;
11691170

11701171
// TODO: Treat it as zero/poison if the use of I is unreachable.
@@ -1245,7 +1246,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
12451246
unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
12461247

12471248
Known = Known.trunc(SrcBitWidth);
1248-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1249+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
12491250
// If the sign bit of the input is known set or clear, then we know the
12501251
// top bits of the result.
12511252
Known = Known.sext(BitWidth);
@@ -1305,14 +1306,14 @@ static void computeKnownBitsFromOperator(const Operator *I,
13051306
break;
13061307
}
13071308
case Instruction::SRem:
1308-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1309-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1309+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1310+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
13101311
Known = KnownBits::srem(Known, Known2);
13111312
break;
13121313

13131314
case Instruction::URem:
1314-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1315-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1315+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1316+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
13161317
Known = KnownBits::urem(Known, Known2);
13171318
break;
13181319
case Instruction::Alloca:
@@ -1465,17 +1466,17 @@ static void computeKnownBitsFromOperator(const Operator *I,
14651466

14661467
unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
14671468
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1468-
Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator();
1469+
Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
14691470

14701471
// Ok, we have a PHI of the form L op= R. Check for low
14711472
// zero bits.
14721473
RecQ.CxtI = RInst;
1473-
computeKnownBits(R, Known2, Depth + 1, RecQ);
1474+
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
14741475

14751476
// We need to take the minimum number of known bits
14761477
KnownBits Known3(BitWidth);
14771478
RecQ.CxtI = LInst;
1478-
computeKnownBits(L, Known3, Depth + 1, RecQ);
1479+
computeKnownBits(L, DemandedElts, Known3, Depth + 1, RecQ);
14791480

14801481
Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
14811482
Known3.countMinTrailingZeros()));
@@ -1548,7 +1549,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
15481549
// want to waste time spinning around in loops.
15491550
// TODO: See if we can base recursion limiter on number of incoming phi
15501551
// edges so we don't overly clamp analysis.
1551-
computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ);
1552+
computeKnownBits(IncValue, DemandedElts, Known2,
1553+
MaxAnalysisRecursionDepth - 1, RecQ);
15521554

15531555
// See if we can further use a conditional branch into the phi
15541556
// to help us determine the range of the value.
@@ -1619,9 +1621,10 @@ static void computeKnownBitsFromOperator(const Operator *I,
16191621
}
16201622
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
16211623
switch (II->getIntrinsicID()) {
1622-
default: break;
1624+
default:
1625+
break;
16231626
case Intrinsic::abs: {
1624-
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1627+
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
16251628
bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
16261629
Known = Known2.abs(IntMinIsPoison);
16271630
break;
@@ -1637,7 +1640,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
16371640
Known.One |= Known2.One.byteSwap();
16381641
break;
16391642
case Intrinsic::ctlz: {
1640-
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1643+
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
16411644
// If we have a known 1, its position is our upper bound.
16421645
unsigned PossibleLZ = Known2.countMaxLeadingZeros();
16431646
// If this call is poison for 0 input, the result will be less than 2^n.
@@ -1648,7 +1651,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
16481651
break;
16491652
}
16501653
case Intrinsic::cttz: {
1651-
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1654+
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
16521655
// If we have a known 1, its position is our upper bound.
16531656
unsigned PossibleTZ = Known2.countMaxTrailingZeros();
16541657
// If this call is poison for 0 input, the result will be less than 2^n.
@@ -1659,7 +1662,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
16591662
break;
16601663
}
16611664
case Intrinsic::ctpop: {
1662-
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1665+
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
16631666
// We can bound the space the count needs. Also, bits known to be zero
16641667
// can't contribute to the population.
16651668
unsigned BitsPossiblySet = Known2.countMaxPopulation();
@@ -1681,8 +1684,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
16811684
ShiftAmt = BitWidth - ShiftAmt;
16821685

16831686
KnownBits Known3(BitWidth);
1684-
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1685-
computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
1687+
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1688+
computeKnownBits(I->getOperand(1), DemandedElts, Known3, Depth + 1, Q);
16861689

16871690
Known.Zero =
16881691
Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
@@ -1691,27 +1694,30 @@ static void computeKnownBitsFromOperator(const Operator *I,
16911694
break;
16921695
}
16931696
case Intrinsic::uadd_sat:
1694-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1695-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1697+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1698+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
16961699
Known = KnownBits::uadd_sat(Known, Known2);
16971700
break;
16981701
case Intrinsic::usub_sat:
1699-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1700-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1702+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1703+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17011704
Known = KnownBits::usub_sat(Known, Known2);
17021705
break;
17031706
case Intrinsic::sadd_sat:
1704-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1705-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1707+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1708+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17061709
Known = KnownBits::sadd_sat(Known, Known2);
17071710
break;
17081711
case Intrinsic::ssub_sat:
1709-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1710-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1712+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1713+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17111714
Known = KnownBits::ssub_sat(Known, Known2);
17121715
break;
17131716
// Vec reverse preserves bits from input vec.
17141717
case Intrinsic::vector_reverse:
1718+
computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known,
1719+
Depth + 1, Q);
1720+
break;
17151721
// for min/max/and/or reduce, any bit common to each element in the
17161722
// input vec is set in the output.
17171723
case Intrinsic::vector_reduce_and:
@@ -1738,31 +1744,31 @@ static void computeKnownBitsFromOperator(const Operator *I,
17381744
break;
17391745
}
17401746
case Intrinsic::umin:
1741-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1742-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1747+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1748+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17431749
Known = KnownBits::umin(Known, Known2);
17441750
break;
17451751
case Intrinsic::umax:
1746-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1747-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1752+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1753+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17481754
Known = KnownBits::umax(Known, Known2);
17491755
break;
17501756
case Intrinsic::smin:
1751-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1752-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1757+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1758+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17531759
Known = KnownBits::smin(Known, Known2);
17541760
break;
17551761
case Intrinsic::smax:
1756-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1757-
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1762+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1763+
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
17581764
Known = KnownBits::smax(Known, Known2);
17591765
break;
17601766
case Intrinsic::ptrmask: {
1761-
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1767+
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
17621768

17631769
const Value *Mask = I->getOperand(1);
17641770
Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1765-
computeKnownBits(Mask, Known2, Depth + 1, Q);
1771+
computeKnownBits(Mask, DemandedElts, Known2, Depth + 1, Q);
17661772
// TODO: 1-extend would be more precise.
17671773
Known &= Known2.anyextOrTrunc(BitWidth);
17681774
break;

llvm/test/Analysis/ValueTracking/known-bits.ll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ define <4 x i1> @vec_reverse_known_bits_fail(<4 x i8> %xx) {
2626

2727
define i1 @vec_reverse_known_bits_demanded(<4 x i8> %xx) {
2828
; CHECK-LABEL: @vec_reverse_known_bits_demanded(
29-
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 127, i8 55, i8 -128, i8 123>
30-
; CHECK-NEXT: [[REV:%.*]] = call <4 x i8> @llvm.vector.reverse.v4i8(<4 x i8> [[X]])
31-
; CHECK-NEXT: [[ELE:%.*]] = extractelement <4 x i8> [[REV]], i64 1
32-
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[ELE]], 0
33-
; CHECK-NEXT: ret i1 [[R]]
29+
; CHECK-NEXT: ret i1 true
3430
;
3531
%x = or <4 x i8> %xx, <i8 127, i8 55, i8 128, i8 123>
3632
%rev = call <4 x i8> @llvm.vector.reverse(<4 x i8> %x)

0 commit comments

Comments
 (0)