[cfe-dev] why does clang(llvm) do not optimize static throw/catch constructs?

Reid Kleckner rnk at google.com
Wed Mar 18 11:11:35 PDT 2015


On Tue, Mar 17, 2015 at 9:44 PM, Dennis Luehring <dl.soluz at gmx.net> wrote:

> tested with http://gcc.godbolt.org/ x86 clang 3.7 (experimental)
>
> this silly throw/catch construct isn't optmized down to return 1
>
> isn't there any exception-flow analysis in clang/llvm (even detecting this
> stupid case)
> or is there a C++ standard limitation that forbits such optimizations - or
> does it overall
> not make sense trying to optimize try/catch-flow in any way
>
> int main()
> {
>   try
>   {
>     throw 1;
>   }
>   catch(int e)
>   {
>     return e;
>   }
>   return 0;
> }
>

We could actually optimize this example in LLVM if we wanted to, but this
we cannot:

void g();
void f() {
  try {
    throw 42;
  } catch (int e) {
    g();
  }
}

g's definition can rethrow, without ever having seen 'e':
void g() { throw; }

C++ requires EH schemes to maintain thread-local state, and it's pretty
opaque to LLVM. Optimizing it would require whole-program knowledge.

Even noexcept annotations won't help:
void g() noexcept {
  try {
    throw;
  } catch (int e) {
    // Now I have a copy of the exception object.
  }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150318/43827077/attachment.html>


More information about the cfe-dev mailing list