In some IR shapes constant terminator instructions prevent FindFunctionBackedges() from detecting the true loop headers in F since it's only done once at the beginning of the pass. When ProcessBlock() later detects and convert these constant TIs it is non-trivial to determine if BB, and any of its successors, are loop headers. If we do not have accurate loop header analysis in JumpThreading there is a possibility of a miscompile when threading an edge across the LH. Please see PR42085 for more analysis and the original the failing test cases.
This patch folds all constant TIs before calculating loop header analysis which is what a normal Clang invocation of the pass manager pipeline hands to JT. This allows FindFunctionBackedges() to locate the loop headers in F. It also helps remove unreachable code regions since we remove these inacurate edges from the IR before we calculate Unreachable BBs using dominators in runImpl().