Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9cf4f49

Browse files
committed
[DAG] Move SelectionDAG implementation to KnownBits::setInReg(). NFCI.
1 parent 3e3e276 commit 9cf4f49

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ struct KnownBits {
179179
return *this;
180180
}
181181

182+
/// Return known bits for a in-register sign extension of the value we're
183+
/// tracking.
184+
KnownBits sextInReg(unsigned SrcBitWidth) const;
185+
182186
/// Return a KnownBits with the extracted bits
183187
/// [bitPosition,bitPosition+numBits).
184188
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,38 +2999,9 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
29992999
}
30003000
break;
30013001
case ISD::SIGN_EXTEND_INREG: {
3002-
EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3003-
unsigned EBits = EVT.getScalarSizeInBits();
3004-
3005-
// Sign extension. Compute the demanded bits in the result that are not
3006-
// present in the input.
3007-
APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
3008-
3009-
APInt InSignMask = APInt::getSignMask(EBits);
3010-
APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
3011-
3012-
// If the sign extended bits are demanded, we know that the sign
3013-
// bit is demanded.
3014-
InSignMask = InSignMask.zext(BitWidth);
3015-
if (NewBits.getBoolValue())
3016-
InputDemandedBits |= InSignMask;
3017-
30183002
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3019-
Known.One &= InputDemandedBits;
3020-
Known.Zero &= InputDemandedBits;
3021-
3022-
// If the sign bit of the input is known set or clear, then we know the
3023-
// top bits of the result.
3024-
if (Known.Zero.intersects(InSignMask)) { // Input sign bit known clear
3025-
Known.Zero |= NewBits;
3026-
Known.One &= ~NewBits;
3027-
} else if (Known.One.intersects(InSignMask)) { // Input sign bit known set
3028-
Known.One |= NewBits;
3029-
Known.Zero &= ~NewBits;
3030-
} else { // Input sign bit unknown
3031-
Known.Zero &= ~NewBits;
3032-
Known.One &= ~NewBits;
3033-
}
3003+
EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3004+
Known = Known.sextInReg(EVT.getScalarSizeInBits());
30343005
break;
30353006
}
30363007
case ISD::CTTZ:

llvm/lib/Support/KnownBits.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,41 @@ KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
8383
return KnownOut;
8484
}
8585

86+
KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const {
87+
unsigned BitWidth = getBitWidth();
88+
assert(BitWidth >= SrcBitWidth && "Illegal sext-in-register");
89+
90+
// Sign extension. Compute the demanded bits in the result that are not
91+
// present in the input.
92+
APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth);
93+
94+
// If the sign extended bits are demanded, we know that the sign
95+
// bit is demanded.
96+
APInt InSignMask = APInt::getSignMask(SrcBitWidth).zext(BitWidth);
97+
APInt InDemandedBits = APInt::getLowBitsSet(BitWidth, SrcBitWidth);
98+
if (NewBits.getBoolValue())
99+
InDemandedBits |= InSignMask;
100+
101+
KnownBits Result;
102+
Result.One = One & InDemandedBits;
103+
Result.Zero = Zero & InDemandedBits;
104+
105+
// If the sign bit of the input is known set or clear, then we know the
106+
// top bits of the result.
107+
if (Result.Zero.intersects(InSignMask)) { // Input sign bit known clear
108+
Result.Zero |= NewBits;
109+
Result.One &= ~NewBits;
110+
} else if (Result.One.intersects(InSignMask)) { // Input sign bit known set
111+
Result.One |= NewBits;
112+
Result.Zero &= ~NewBits;
113+
} else { // Input sign bit unknown
114+
Result.Zero &= ~NewBits;
115+
Result.One &= ~NewBits;
116+
}
117+
118+
return Result;
119+
}
120+
86121
KnownBits KnownBits::makeGE(const APInt &Val) const {
87122
// Count the number of leading bit positions where our underlying value is
88123
// known to be less than or equal to Val.

0 commit comments

Comments
 (0)