Exception handlers are currently represented implicitly in the IR via guard and leave instructions, and complemented by ProtectedRegionAnalysis - a standalone analysis that provides a more concrete region tree representation. This makes it relatively complicated and expansive for other analysis and optimizations to correctly reason about exception regions.
One serious flaw that I only recently discovered (after tweaking the inliner/devirt) was register allocation not accounting for live-outs variables in finally handlers due to the lack of explicit successor edges, which lead to reuse of vars that are live after endfinally/resume:

(LiveReg_Loop_Finally - improper regalloc for phi @2 and conv @2)
This sample also demonstrates that GVN unsafely propagates the value of store $loc1, r2 into the finally handler (which is fine in the sample above, but it doesn't actually check for in-between exceptions).
At first, these are some things that need to be done:
Exception handlers are currently represented implicitly in the IR via guard and leave instructions, and complemented by ProtectedRegionAnalysis - a standalone analysis that provides a more concrete region tree representation. This makes it relatively complicated and expansive for other analysis and optimizations to correctly reason about exception regions.
One serious flaw that I only recently discovered (after tweaking the inliner/devirt) was register allocation not accounting for live-outs variables in finally handlers due to the lack of explicit successor edges, which lead to reuse of vars that are live after endfinally/resume:
(LiveReg_Loop_Finally - improper regalloc for
phi @2andconv @2)This sample also demonstrates that GVN unsafely propagates the value of
store $loc1, r2into the finally handler (which is fine in the sample above, but it doesn't actually check for in-between exceptions).At first, these are some things that need to be done:
leaveinstructions can point to different targets), the simplest solution I can think of would be to add a backedge from theresumeto the guard instruction. But this feels unnatural and I'm unsure if there would be other more profound side effects (it would most certainly interfere with loop analysis, but it seems simple enough to ignore them).