diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h --- a/llvm/include/llvm/Support/KnownBits.h +++ b/llvm/include/llvm/Support/KnownBits.h @@ -293,9 +293,30 @@ return KnownBits(~C, C); } + /// Returns KnownBits information that is known to be true for both this and + /// RHS. + /// + /// When an operation is known to return one of its operands, this can be used + /// to combine information about the known bits of the operands to get the + /// information that must be true about the result. + KnownBits intersectWith(const KnownBits &RHS) const { + return KnownBits(Zero & RHS.Zero, One & RHS.One); + } + + /// Returns KnownBits information that is known to be true for either this or + /// RHS or both. + /// + /// This can be used to combine different sources of information about the + /// known bits of a single value, e.g. information about the low bits and the + /// high bits of the result of a multiplication. + KnownBits unionWith(const KnownBits &RHS) const { + return KnownBits(Zero | RHS.Zero, One | RHS.One); + } + /// Compute known bits common to LHS and RHS. + LLVM_DEPRECATED("use intersectWith instead", "intersectWith") static KnownBits commonBits(const KnownBits &LHS, const KnownBits &RHS) { - return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One); + return LHS.intersectWith(RHS); } /// Return true if LHS and RHS have no common bits set. diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -668,8 +668,7 @@ if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A)))) { KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth); - Known.Zero |= RHSKnown.Zero; - Known.One |= RHSKnown.One; + Known = Known.unionWith(RHSKnown); // assume(v & b = a) } else if (match(Cmp, m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A)))) { @@ -758,9 +757,8 @@ // For those bits in RHS that are known, we can propagate them to known // bits in V shifted to the right by C. RHSKnown.Zero.lshrInPlace(C); - Known.Zero |= RHSKnown.Zero; RHSKnown.One.lshrInPlace(C); - Known.One |= RHSKnown.One; + Known = Known.unionWith(RHSKnown); // assume(~(v << c) = a) } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), m_Value(A))) && @@ -1053,8 +1051,8 @@ continue; } - Known = KnownBits::commonBits( - Known, KF(Known2, KnownBits::makeConstant(APInt(32, ShiftAmt)))); + Known = Known.intersectWith( + KF(Known2, KnownBits::makeConstant(APInt(32, ShiftAmt)))); } // If the known bits conflict, the result is poison. Return a 0 and hope the @@ -1242,7 +1240,7 @@ computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); if (SPF == SPF_ABS) { // RHS from matchSelectPattern returns the negation part of abs pattern. @@ -1680,7 +1678,7 @@ } } - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); // If all bits have been ruled out, there's no need to check // more operands. if (Known.isUnknown()) @@ -1699,8 +1697,7 @@ computeKnownBitsFromRangeMetadata(*MD, Known); if (const Value *RV = cast(I)->getReturnedArgOperand()) { computeKnownBits(RV, Known2, Depth + 1, Q); - Known.Zero |= Known2.Zero; - Known.One |= Known2.One; + Known = Known.unionWith(Known2); } if (const IntrinsicInst *II = dyn_cast(I)) { switch (II->getIntrinsicID()) { @@ -1872,7 +1869,7 @@ if (!!DemandedRHS) { const Value *RHS = Shuf->getOperand(1); computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } break; } @@ -1905,7 +1902,7 @@ DemandedVecElts.clearBit(EltIdx); if (!!DemandedVecElts) { computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } break; } diff --git a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp --- a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp +++ b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp @@ -115,7 +115,7 @@ computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } // Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is @@ -191,7 +191,7 @@ Depth + 1); // Known bits are the values that are shared by every demanded element. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); // If we don't know any bits, early out. if (Known.isUnknown()) @@ -235,7 +235,7 @@ // For COPYs we don't do anything, don't increase the depth. computeKnownBitsImpl(SrcReg, Known2, DemandedElts, Depth + (Opcode != TargetOpcode::COPY)); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); // If we reach a point where we don't know anything // just stop looking through the operands. if (Known.One == 0 && Known.Zero == 0) diff --git a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp --- a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp @@ -505,7 +505,7 @@ return; } DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); - DestLOI.Known = KnownBits::commonBits(DestLOI.Known, SrcLOI->Known); + DestLOI.Known = DestLOI.Known.intersectWith(SrcLOI->Known); } } diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -3051,7 +3051,7 @@ } // Known bits are the values that are shared by every demanded element. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); // If we don't know any bits, early out. if (Known.isUnknown()) @@ -3074,7 +3074,7 @@ if (!!DemandedLHS) { SDValue LHS = Op.getOperand(0); Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } // If we don't know any bits, early out. if (Known.isUnknown()) @@ -3082,7 +3082,7 @@ if (!!DemandedRHS) { SDValue RHS = Op.getOperand(1); Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } break; } @@ -3100,7 +3100,7 @@ if (!!DemandedSub) { SDValue Sub = Op.getOperand(i); Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } // If we don't know any bits, early out. if (Known.isUnknown()) @@ -3130,7 +3130,7 @@ } if (!!DemandedSrcElts) { Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } break; } @@ -3220,8 +3220,7 @@ if (DemandedElts[i]) { unsigned Shifts = IsLE ? i : NumElts - 1 - i; unsigned Offset = (Shifts % SubScale) * BitWidth; - Known = KnownBits::commonBits(Known, - Known2.extractBits(BitWidth, Offset)); + Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset)); // If we don't know any bits, early out. if (Known.isUnknown()) break; @@ -3319,7 +3318,7 @@ Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; case ISD::SELECT_CC: Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1); @@ -3329,7 +3328,7 @@ Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; case ISD::SMULO: case ISD::UMULO: @@ -3409,8 +3408,7 @@ Known2.One.lshrInPlace(Amt); Known2.Zero.lshrInPlace(Amt); } - Known.One |= Known2.One; - Known.Zero |= Known2.Zero; + Known = Known.unionWith(Known2); } break; case ISD::SHL_PARTS: @@ -3780,11 +3778,11 @@ Known.Zero.setAllBits(); if (DemandedVal) { Known2 = computeKnownBits(InVal, Depth + 1); - Known = KnownBits::commonBits(Known, Known2.zextOrTrunc(BitWidth)); + Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth)); } if (!!DemandedVecElts) { Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } break; } diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -1195,7 +1195,7 @@ return true; if (!!DemandedVecElts) - Known = KnownBits::commonBits(Known, KnownVec); + Known = Known.intersectWith(KnownVec); return false; } @@ -1223,9 +1223,9 @@ Known.Zero.setAllBits(); Known.One.setAllBits(); if (!!DemandedSubElts) - Known = KnownBits::commonBits(Known, KnownSub); + Known = Known.intersectWith(KnownSub); if (!!DemandedSrcElts) - Known = KnownBits::commonBits(Known, KnownSrc); + Known = Known.intersectWith(KnownSrc); // Attempt to avoid multi-use src if we don't need anything from it. if (!DemandedBits.isAllOnes() || !DemandedSubElts.isAllOnes() || @@ -1287,7 +1287,7 @@ return true; // Known bits are shared by every demanded subvector element. if (!!DemandedSubElts) - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } break; } @@ -1311,13 +1311,13 @@ if (SimplifyDemandedBits(Op0, DemandedBits, DemandedLHS, Known2, TLO, Depth + 1)) return true; - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } if (!!DemandedRHS) { if (SimplifyDemandedBits(Op1, DemandedBits, DemandedRHS, Known2, TLO, Depth + 1)) return true; - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } // Attempt to avoid multi-use ops if we don't need anything from them. @@ -1619,7 +1619,7 @@ return true; // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; case ISD::VSELECT: if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, DemandedElts, @@ -1632,7 +1632,7 @@ assert(!Known2.hasConflict() && "Bits known to be one AND zero?"); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; case ISD::SELECT_CC: if (SimplifyDemandedBits(Op.getOperand(3), DemandedBits, Known, TLO, @@ -1649,7 +1649,7 @@ return true; // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; case ISD::SETCC: { SDValue Op0 = Op.getOperand(0); @@ -1997,8 +1997,7 @@ Known2.Zero <<= (IsFSHL ? Amt : (BitWidth - Amt)); Known.One.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt); Known.Zero.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt); - Known.One |= Known2.One; - Known.Zero |= Known2.Zero; + Known = Known.unionWith(Known2); // Attempt to avoid multi-use ops if we don't need anything from them. if (!Demanded0.isAllOnes() || !Demanded1.isAllOnes() || diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp --- a/llvm/lib/Support/KnownBits.cpp +++ b/llvm/lib/Support/KnownBits.cpp @@ -129,7 +129,7 @@ // are common to these two values are also known in the result. KnownBits L = LHS.makeGE(RHS.getMinValue()); KnownBits R = RHS.makeGE(LHS.getMinValue()); - return KnownBits::commonBits(L, R); + return L.intersectWith(R); } KnownBits KnownBits::umin(const KnownBits &LHS, const KnownBits &RHS) { @@ -208,7 +208,7 @@ KnownBits SpecificShift; SpecificShift.Zero = LHS.Zero << ShiftAmt; SpecificShift.One = LHS.One << ShiftAmt; - Known = KnownBits::commonBits(Known, SpecificShift); + Known = Known.intersectWith(SpecificShift); if (Known.isUnknown()) break; } @@ -261,7 +261,7 @@ KnownBits SpecificShift = LHS; SpecificShift.Zero.lshrInPlace(ShiftAmt); SpecificShift.One.lshrInPlace(ShiftAmt); - Known = KnownBits::commonBits(Known, SpecificShift); + Known = Known.intersectWith(SpecificShift); if (Known.isUnknown()) break; } @@ -319,7 +319,7 @@ KnownBits SpecificShift = LHS; SpecificShift.Zero.ashrInPlace(ShiftAmt); SpecificShift.One.ashrInPlace(ShiftAmt); - Known = KnownBits::commonBits(Known, SpecificShift); + Known = Known.intersectWith(SpecificShift); if (Known.isUnknown()) break; } diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp --- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -2064,7 +2064,7 @@ KnownBits Known2; Known = DAG.computeKnownBits(Op->getOperand(0), Depth + 1); Known2 = DAG.computeKnownBits(Op->getOperand(1), Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; } case AArch64ISD::BICi: { diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -20010,7 +20010,7 @@ return; KnownBits KnownRHS = DAG.computeKnownBits(Op.getOperand(1), Depth+1); - Known = KnownBits::commonBits(Known, KnownRHS); + Known = Known.intersectWith(KnownRHS); return; } case ISD::INTRINSIC_W_CHAIN: { @@ -20092,7 +20092,7 @@ KnownOp1 = KnownBits::mul( KnownOp1, KnownBits::makeConstant(APInt(32, -1))); - Known = KnownBits::commonBits(KnownOp0, KnownOp1); + Known = KnownOp0.intersectWith(KnownOp1); break; } } diff --git a/llvm/lib/Target/Lanai/LanaiISelLowering.cpp b/llvm/lib/Target/Lanai/LanaiISelLowering.cpp --- a/llvm/lib/Target/Lanai/LanaiISelLowering.cpp +++ b/llvm/lib/Target/Lanai/LanaiISelLowering.cpp @@ -1499,7 +1499,7 @@ KnownBits Known2; Known = DAG.computeKnownBits(Op->getOperand(0), Depth + 1); Known2 = DAG.computeKnownBits(Op->getOperand(1), Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; } } diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -12493,7 +12493,7 @@ KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(3), Depth + 1); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; } case RISCVISD::REMUW: { diff --git a/llvm/lib/Target/Sparc/SparcISelLowering.cpp b/llvm/lib/Target/Sparc/SparcISelLowering.cpp --- a/llvm/lib/Target/Sparc/SparcISelLowering.cpp +++ b/llvm/lib/Target/Sparc/SparcISelLowering.cpp @@ -2031,7 +2031,7 @@ Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; } } diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp --- a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp +++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp @@ -7279,7 +7279,7 @@ DAG.computeKnownBits(Op.getOperand(OpNo), Src0DemE, Depth + 1); KnownBits RHSKnown = DAG.computeKnownBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1); - Known = KnownBits::commonBits(LHSKnown, RHSKnown); + Known = LHSKnown.intersectWith(RHSKnown); } void diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -38750,11 +38750,11 @@ KnownBits Known2; if (!!DemandedLHS) { Known2 = DAG.computeKnownBits(Op.getOperand(0), DemandedLHS, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } if (!!DemandedRHS) { Known2 = DAG.computeKnownBits(Op.getOperand(1), DemandedRHS, Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } if (Known.countMinLeadingZeros() < BitWidth) @@ -38841,7 +38841,7 @@ KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); break; } case X86ISD::BEXTR: @@ -38951,7 +38951,7 @@ break; } KnownBits Known2 = KnownBits::makeConstant(EltBits[I]); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } return; } @@ -39002,7 +39002,7 @@ continue; KnownBits Known2 = DAG.computeKnownBits(Ops[i], DemandedOps[i], Depth + 1); - Known = KnownBits::commonBits(Known, Known2); + Known = Known.intersectWith(Known2); } } } @@ -43946,7 +43946,7 @@ return true; KnownScl = KnownScl.trunc(VecVT.getScalarSizeInBits()); - Known = KnownBits::commonBits(KnownVec, KnownScl); + Known = KnownVec.intersectWith(KnownScl); return false; } break; @@ -54851,8 +54851,8 @@ if (VT.isVector() && OpVT.isVector() && OpVT.isInteger()) { bool CanMakeSigned = false; if (ISD::isUnsignedIntSetCC(CC)) { - KnownBits CmpKnown = KnownBits::commonBits(DAG.computeKnownBits(LHS), - DAG.computeKnownBits(RHS)); + KnownBits CmpKnown = + DAG.computeKnownBits(LHS).intersectWith(DAG.computeKnownBits(RHS)); // If we know LHS/RHS share the same sign bit at each element we can // make this signed. // NOTE: `computeKnownBits` on a vector type aggregates common bits diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -384,7 +384,7 @@ return I; // Only known if known in both the LHS and RHS. - Known = KnownBits::commonBits(LHSKnown, RHSKnown); + Known = LHSKnown.intersectWith(RHSKnown); break; } case Instruction::Trunc: {