[tree-ssa] Fun with exceptions -- opinions needed

law@redhat.com law@redhat.com
Tue Jun 17 00:45:00 GMT 2003


In message <20030616201907.00A591590F1@kanga.canids.net>, Felix Lee writes:
 >fche@redhat.com (Frank Ch. Eigler):
 >> law wrote:
 >> > [...]  OK.  So think about what happens if "get" throws an exception
 >> > in the statement "i = get (strm.5)".  Right, the side effect of
 >> > assigning a new value to "i" does not happen.
 >> Can someone explain why such a side-effect would be expected?  I
 >> couldn't find a reference in Stroustrup about what is supposed to
 >> happen to the LHS of an assignment interrupted by an exception.
 >
 >I thought the issue was a missed optimization.
Nope, it's a case of over-optimization.

 If you have:
 >    i = 0;
 >    try {
 >        i = foo ();
 >    }
 >    catch (...) {
 >        L1:;
 >    }
 >    if (i != 0) {
 >       ...
 >    }
 >    L2:;
 >then the optimizer should be able to connect L1 to L2, bypassing
 >the test, but it can't because it will think i can change, unless
 >you separate the two effects of i = foo() with a temp.
Without a temporary, the optimizers will assume that the statement
"i = 0" is dead since "i" is unconditionally overwritten in the
try block.  In this case the statement "i = 0" will be deleted.
Now if you take an exception in "foo", "i" will have an undefined
value at the statement if (i != 0).

With a temporary:
 >    i = 0;
 >    try {
 >        temp = foo ();
 >        i = temp;
 >    }
 >    catch (...) {
 >        L1:;
 >    }
 >    if (i != 0) {
 >       ...
 >    }
 >    L2:;
Like this the compiler will have "i = temp" in a basic block by
itself.  That block is conditionally executed based on whether or
not "foo" throws.  

Thus the assignment "i = 0" and "i = temp" can both reach the 
conditional.  The first reaches if an exception is thrown, the second
reaches if the exception is not thrown.  Note in both circumstances
"i" has a well-defined value at the conditional.

jeff




More information about the Gcc mailing list