The current code created a NewBits mask and used it as a mask several times. One of them just before a call to trunc making it unnecessary. A call to getActiveBits can get us the same information for the case. We also ORed with this mask later when we should have just sign extended the known bits.
We also called trunc on the guaranteed to be zero KnownZeros/Ones masks entering this code. Creating appropriately sized temporary APInts is probably better. (This needs to be done in the zero extend code too which I'll do as a follow up)
We also made a recursive call to SimplifyDemandedBits even if we were just going to replace it with ZExt due to upper bits not being used. Unfortunately, now we create the ZExt in two separate places and I didn't see a great way to merge the control flow.
Using isNegative on InputKnownZero is a little confusing because we are testing to see if the sign bit is clear.