Add the following pattern to TryToUnfoldSelectInCurrBB()
bb: %p = phi [0, %bb1], [1, %bb2], [0, %bb3], [1, %bb4], ... %c = cmp %p, 0 %s = select %c, trueval, falseval
The Select in the above pattern will be unfolded and then jump-threaded. The current implementation does not allow CMP in the middle of PHI and Select.
Do you intentionally go through all users of the PHI by removing the check PN->hasOneUse() ?