Index: include/llvm/CodeGen/GlobalISel/GISelKnownBits.h =================================================================== --- include/llvm/CodeGen/GlobalISel/GISelKnownBits.h +++ include/llvm/CodeGen/GlobalISel/GISelKnownBits.h @@ -47,6 +47,17 @@ APInt getKnownZeroes(Register R); APInt getKnownOnes(Register R); + /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use + /// this predicate to simplify operations downstream. + /// Mask is known to be zero for bits that V cannot have. + bool maskedValueIsZero(Register Val, const APInt &Mask) { + return Mask.isSubsetOf(getKnownBits(Val).Zero); + } + + /// \return true if the sign bit of Op is known to be zero. We use this + /// predicate to simplify operations downstream. + bool signBitIsZero(Register Op); + // FIXME: Is this the right place for G_FRAME_INDEX? Should it be in // TargetLowering? void computeKnownBitsForFrameIndex(Register R, KnownBits &Known, Index: lib/CodeGen/GlobalISel/GISelKnownBits.cpp =================================================================== --- lib/CodeGen/GlobalISel/GISelKnownBits.cpp +++ lib/CodeGen/GlobalISel/GISelKnownBits.cpp @@ -75,6 +75,12 @@ return Known; } +bool GISelKnownBits::signBitIsZero(Register R) { + LLT Ty = MRI.getType(R); + unsigned BitWidth = Ty.getScalarSizeInBits(); + return maskedValueIsZero(R, APInt::getSignMask(BitWidth)); +} + APInt GISelKnownBits::getKnownZeroes(Register R) { return getKnownBits(R).Zero; } Index: unittests/CodeGen/GlobalISel/KnownBitsTest.cpp =================================================================== --- unittests/CodeGen/GlobalISel/KnownBitsTest.cpp +++ unittests/CodeGen/GlobalISel/KnownBitsTest.cpp @@ -86,3 +86,19 @@ APInt Zeroes = Info.getKnownZeroes(SrcReg); EXPECT_EQ(Known.Zero, Zeroes); } + +TEST_F(GISelMITest, TestSignBitIsZero) { + if (!TM) + return; + + const LLT S32 = LLT::scalar(32); + auto SignBit = B.buildConstant(S32, 0x8000000); + auto Zero = B.buildConstant(S32, 0); + + GISelKnownBits KnownBits(*MF); + + EXPECT_TRUE(KnownBits.signBitIsZero(Zero.getReg(0))); + EXPECT_FALSE(KnownBits.signBitIsZero(Zero.getReg(0))); + EXPECT_FALSE(KnownBits.signBitIsZero(SignBit.getReg(0))); + EXPECT_TRUE(KnownBits.signBitIsZero(SignBit.getReg(0))); +}