xref: /aosp_15_r20/external/llvm/docs/HistoricalNotes/2001-09-18-OptimizeExceptions.txt (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard WorkerDate: Tue, 18 Sep 2001 00:38:37 -0500 (CDT)
2*9880d681SAndroid Build Coastguard WorkerFrom: Chris Lattner <[email protected]>
3*9880d681SAndroid Build Coastguard WorkerTo: Vikram S. Adve <[email protected]>
4*9880d681SAndroid Build Coastguard WorkerSubject: Idea for a simple, useful link time optimization
5*9880d681SAndroid Build Coastguard Worker
6*9880d681SAndroid Build Coastguard Worker
7*9880d681SAndroid Build Coastguard WorkerIn C++ programs, exceptions suck, and here's why:
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard Worker1. In virtually all function calls, you must assume that the function
10*9880d681SAndroid Build Coastguard Worker   throws an exception, unless it is defined as 'nothrow'.  This means
11*9880d681SAndroid Build Coastguard Worker   that every function call has to have code to invoke dtors on objects
12*9880d681SAndroid Build Coastguard Worker   locally if one is thrown by the function.  Most functions don't throw
13*9880d681SAndroid Build Coastguard Worker   exceptions, so this code is dead [with all the bad effects of dead
14*9880d681SAndroid Build Coastguard Worker   code, including icache pollution].
15*9880d681SAndroid Build Coastguard Worker2. Declaring a function nothrow causes catch blocks to be added to every
16*9880d681SAndroid Build Coastguard Worker   call that isnot  provably nothrow.  This makes them very slow.
17*9880d681SAndroid Build Coastguard Worker3. Extra extraneous exception edges reduce the opportunity for code
18*9880d681SAndroid Build Coastguard Worker   motion.
19*9880d681SAndroid Build Coastguard Worker4. EH is typically implemented with large lookup tables.  Ours is going to
20*9880d681SAndroid Build Coastguard Worker   be much smaller (than the "standard" way of doing it) to start with,
21*9880d681SAndroid Build Coastguard Worker   but eliminating it entirely would be nice. :)
22*9880d681SAndroid Build Coastguard Worker5. It is physically impossible to correctly put (accurate, correct)
23*9880d681SAndroid Build Coastguard Worker   exception specifications on generic, templated code.  But it is trivial
24*9880d681SAndroid Build Coastguard Worker   to analyze instantiations of said code.
25*9880d681SAndroid Build Coastguard Worker6. Most large C++ programs throw few exceptions.  Most well designed
26*9880d681SAndroid Build Coastguard Worker   programs only throw exceptions in specific planned portions of the
27*9880d681SAndroid Build Coastguard Worker   code.
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard WorkerGiven our _planned_ model of handling exceptions, all of this would be
30*9880d681SAndroid Build Coastguard Workerpretty trivial to eliminate through some pretty simplistic interprocedural
31*9880d681SAndroid Build Coastguard Workeranalysis.  The DCE factor alone could probably be pretty significant.  The
32*9880d681SAndroid Build Coastguard Workerextra code motion opportunities could also be exploited though...
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard WorkerAdditionally, this optimization can be implemented in a straight forward
35*9880d681SAndroid Build Coastguard Workerconservative manner, allowing libraries to be optimized or individual
36*9880d681SAndroid Build Coastguard Workerfiles even (if there are leaf functions visible in the translation unit
37*9880d681SAndroid Build Coastguard Workerthat are called).
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard WorkerI think it's a reasonable optimization that hasn't really been addressed
40*9880d681SAndroid Build Coastguard Worker(because assembly is way too low level for this), and could have decent
41*9880d681SAndroid Build Coastguard Workerpayoffs... without being a overly complex optimization.
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard WorkerAfter I wrote all of that, I found this page that is talking about
44*9880d681SAndroid Build Coastguard Workerbasically the same thing I just wrote, except that it is translation unit
45*9880d681SAndroid Build Coastguard Workerat a time, tree based approach:
46*9880d681SAndroid Build Coastguard Workerhttp://www.ocston.org/~jls/ehopt.html
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Workerbut is very useful from "expected gain" and references perspective.  Note
49*9880d681SAndroid Build Coastguard Workerthat their compiler is apparently unable to inline functions that use
50*9880d681SAndroid Build Coastguard Workerexceptions, so there numbers are pretty worthless... also our results
51*9880d681SAndroid Build Coastguard Workerwould (hopefully) be better because it's interprocedural...
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard WorkerWhat do you think?
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker-Chris
56*9880d681SAndroid Build Coastguard Worker
57