Skip to content

Commit 6237579

Browse files
committed
add comments
1 parent e2eb1ff commit 6237579

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,59 +1886,83 @@ static Instruction *foldSelectICmpBinOp(SelectInst &SI, ICmpInst *ICI,
18861886

18871887
KnownBits Known;
18881888
if (TValBop->isBitwiseLogicOp()) {
1889+
// We handle if we know specific knownbits from cond of selectinst.
1890+
// ex) X&Y==-1 ? X^Y : False
18891891
if (SKB != SpecialKnownBits::NothingSpecial && XOrder && YOrder) {
1892+
// No common bits between X, Y
18901893
if (SKB & SpecialKnownBits::NoCommonBits) {
18911894
if (SKB & (SpecialKnownBits::AllBitsEnabled)) {
1895+
// If X op Y == -1, then XOR must be -1
18921896
if (TValBop->getOpcode() == Instruction::Xor)
18931897
Known = KnownBits::makeConstant(APInt(BitWidth, -1));
18941898
}
1899+
// If Trueval is X&Y then it should be 0.
18951900
if (TValBop->getOpcode() == Instruction::And)
18961901
Known = KnownBits::makeConstant(APInt(BitWidth, 0));
1902+
// X|Y can be replace with X^Y, X^Y can be replace with X|Y
1903+
// This replacing is meaningful when falseval is same.
18971904
else if ((match(TVal, m_c_Or(m_Specific(X), m_Specific(Y))) &&
18981905
match(FVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) ||
18991906
(match(TVal, m_c_Xor(m_Specific(X), m_Specific(Y))) &&
19001907
match(FVal, m_c_Or(m_Specific(X), m_Specific(Y)))))
19011908
return IC.replaceInstUsesWith(SI, FVal);
1909+
// All common bits between X, Y
19021910
} else if (SKB & SpecialKnownBits::AllCommonBits) {
1911+
// We can replace (X&Y) and (X|Y) to X or Y
19031912
if (TValBop->getOpcode() == Instruction::And ||
19041913
TValBop->getOpcode() == Instruction::Or)
19051914
if (TValBop->hasOneUse())
19061915
return IC.replaceOperand(SI, 1, X);
19071916
} else if (SKB & SpecialKnownBits::AllBitsEnabled) {
1917+
// We can replace (X|Y) to -1
19081918
if (TValBop->getOpcode() == Instruction::Or)
19091919
Known = KnownBits::makeConstant(APInt(BitWidth, -1));
19101920
}
19111921
} else {
19121922
KnownBits XKnown, YKnown, Temp;
19131923
KnownBits TValBop0KB, TValBop1KB;
1924+
// computeKnowBits calculates the KnownBits in the branching condition
1925+
// that the specified variable passes in the execution flow. however, it
1926+
// does not contain the SelectInst condition, so there is an optimization
1927+
// opportunity to update the knownbits obtained by calculating KnownBits
1928+
// with the SelectInst condition.
19141929
XKnown = IC.computeKnownBits(X, 0, &SI);
19151930
IC.computeKnownBitsFromCond(X, ICI, XKnown, 0, &SI, false);
19161931
YKnown = IC.computeKnownBits(Y, 0, &SI);
19171932
IC.computeKnownBitsFromCond(Y, ICI, YKnown, 0, &SI, false);
1918-
1919-
// Estimate additional KnownBits from the relationship between X and Y
19201933
CmpInst::Predicate Pred = ICI->getPredicate();
19211934
if (Pred == ICmpInst::ICMP_EQ) {
1935+
// Estimate additional KnownBits from the relationship between X and Y
19221936
if (CmpLHSBop->getOpcode() == Instruction::And) {
1937+
// The bit that are set to 1 at `~C&Y` must be 0 in X
1938+
// The bit that are set to 1 at `~C&X` must be 0 in Y
19231939
XKnown.Zero |= ~*C & YKnown.One;
19241940
YKnown.Zero |= ~*C & XKnown.One;
19251941
}
19261942
if (CmpLHSBop->getOpcode() == Instruction::Or) {
1943+
// The bit that are set to 0 at `C&Y` must be 1 in X
1944+
// The bit that are set to 0 at `C&X` must be 1 in Y
19271945
XKnown.One |= *C & YKnown.Zero;
19281946
YKnown.One |= *C & XKnown.Zero;
19291947
}
19301948
if (CmpLHSBop->getOpcode() == Instruction::Xor) {
1949+
// If X^Y==C, then X and Y must be either (1,0) or (0,1) for the
1950+
// enabled bits in C.
19311951
XKnown.One |= *C & YKnown.Zero;
19321952
XKnown.Zero |= *C & YKnown.One;
19331953
YKnown.One |= *C & XKnown.Zero;
19341954
YKnown.Zero |= *C & XKnown.One;
1955+
// If X^Y==C, then X and Y must be either (0,0) or (1,1) for the
1956+
// disabled bits in C.
19351957
XKnown.Zero |= ~*C & YKnown.Zero;
19361958
XKnown.One |= ~*C & YKnown.One;
19371959
YKnown.Zero |= ~*C & XKnown.Zero;
19381960
YKnown.One |= ~*C & XKnown.One;
19391961
}
19401962
}
19411963

1964+
// If TrueVal has X or Y, return the corresponding KnownBits, otherwise
1965+
// compute and return new KnownBits.
19421966
auto getTValBopKB = [&](unsigned OpNum) -> KnownBits {
19431967
unsigned Order = OpNum + 1;
19441968
if (Order == XOrder)

0 commit comments

Comments
 (0)