When simplifying demanded bits, we currently only report the instruction on which SimplifyDemandedBits was called as changed. However, this is a recursive call, and the actually modified instruction will usually be further up the chain. Additionally, all the intermediate instructions should also be revisited, as additional combines may be possible after the demanded bits simplification. We fix this by explicitly adding them to the worklist.
I originally wrote this patch to address the excessive number of instcombine iterations in an existing test (drops from 5 to 3, which is still not optimal), but found that this also addresses https://bugs.llvm.org/show_bug.cgi?id=44541, though I'm not sure whether this is really a "fix". What happens there is that demanded bits simplifies
%zero = call i16 @passthru(i16 0) %sub = sub nuw nsw i16 %arg, %zero %cmp = icmp slt i16 %sub, 0 %ret = select i1 %cmp, i16 0, i16 %sub
to
%zero = call i16 @passthru(i16 0) %sub = sub nuw nsw i16 %arg, 0 %cmp = icmp slt i16 %sub, 0 %ret = select i1 %cmp, i16 0, i16 %sub
without adding the sub to the worklist (which this patch fixes). Then %cmp = icmp slt i16 %sub, 0 sensibly becomes %cmp = icmp slt i16 %arg, 0. The select is then recognized as a minmax SPF and canonicalized to
%zero = call i16 @passthru(i16 0) %sub = sub nuw nsw i16 %arg, 0 %cmp = icmp sgt i16 0, %sub %ret = select i1 %cmp, i16 0, i16 %sub
At which point we enter an infinite combine loop. Adding the %sub to the worklist is sufficient to get it simplified and avoid the issue. I don't know if that's considered enough, or whether there is also a bug in SPF matching or canonicalization here, in the sense that it needs to special-case this type of pattern, on the off-chance that it could appear through another pathway.