This reintroduces a fix for a problem where the branch folding optimization can go into an infinite loop shuffling blocks at the end of a function. This problem was originally fixed in D14996 but that change was reverted as seen in D22839 because it was causing compile time to blow up in some circumstances. At the time this was reverted I believed that the original problem could no longer be reproduced, but I have since seen it with a particular set of optimization options with the spec2006/483 benchmark.
The test case I am adding here is a version of the 483 code that was reduced using bugpoint. I am not clear as to exactly why this particular case is triggering the problem, but I wasn't able to reduce it any further. I suspect that the state it is in now just happens to trigger different behavior prior to branch folding when optimizing for size.
In any event, the important thing is that the potential for the problem still exists. The root cause is that when the branch folding code is hit there is a block near the end of the function with a function call followed by a branch to a block that is higher in the function, then three or more exception handling blocks which are possible successors by way of exceptions in the called function. In this case, the code prior to this patch would rotate the exception blocks to the end of the function in infinite succession.
The compilation time problem was caused by the previous attempt to find a non-exception handling block beyond the block to be moved. In a function containing a large number of exception handling blocks this led to quadratic or worse behavior. The new fix simply gives up if the block following the block to be moved is an exception handling block.