2001-09-18-OptimizeExceptions.txt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. Date: Tue, 18 Sep 2001 00:38:37 -0500 (CDT)
  2. From: Chris Lattner <sabre@nondot.org>
  3. To: Vikram S. Adve <vadve@cs.uiuc.edu>
  4. Subject: Idea for a simple, useful link time optimization
  5. In C++ programs, exceptions suck, and here's why:
  6. 1. In virtually all function calls, you must assume that the function
  7. throws an exception, unless it is defined as 'nothrow'. This means
  8. that every function call has to have code to invoke dtors on objects
  9. locally if one is thrown by the function. Most functions don't throw
  10. exceptions, so this code is dead [with all the bad effects of dead
  11. code, including icache pollution].
  12. 2. Declaring a function nothrow causes catch blocks to be added to every
  13. call that isnot provably nothrow. This makes them very slow.
  14. 3. Extra extraneous exception edges reduce the opportunity for code
  15. motion.
  16. 4. EH is typically implemented with large lookup tables. Ours is going to
  17. be much smaller (than the "standard" way of doing it) to start with,
  18. but eliminating it entirely would be nice. :)
  19. 5. It is physically impossible to correctly put (accurate, correct)
  20. exception specifications on generic, templated code. But it is trivial
  21. to analyze instantiations of said code.
  22. 6. Most large C++ programs throw few exceptions. Most well designed
  23. programs only throw exceptions in specific planned portions of the
  24. code.
  25. Given our _planned_ model of handling exceptions, all of this would be
  26. pretty trivial to eliminate through some pretty simplistic interprocedural
  27. analysis. The DCE factor alone could probably be pretty significant. The
  28. extra code motion opportunities could also be exploited though...
  29. Additionally, this optimization can be implemented in a straight forward
  30. conservative manner, allowing libraries to be optimized or individual
  31. files even (if there are leaf functions visible in the translation unit
  32. that are called).
  33. I think it's a reasonable optimization that hasn't really been addressed
  34. (because assembly is way too low level for this), and could have decent
  35. payoffs... without being a overly complex optimization.
  36. After I wrote all of that, I found this page that is talking about
  37. basically the same thing I just wrote, except that it is translation unit
  38. at a time, tree based approach:
  39. http://www.ocston.org/~jls/ehopt.html
  40. but is very useful from "expected gain" and references perspective. Note
  41. that their compiler is apparently unable to inline functions that use
  42. exceptions, so there numbers are pretty worthless... also our results
  43. would (hopefully) be better because it's interprocedural...
  44. What do you think?
  45. -Chris