1 /* Implements exceptiom handling.
2 Copyright (C) 1989, 92-95, 1996 Free Software Foundation, Inc.
3 Contributed by Mike Stump <mrs@cygnus.com>.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file contains the exception handling code for the compiler.
25 Exception handling is a mechanism by which complex flows of control
26 can be designated. The central concepts are the `exception region',
27 the associated `exception handler' for that region and the concept of
28 throwing an exception, and the context of the throw.
30 Restrictions are, the regions must be non-overlapping, they can be
31 nested, and there can be zero or more per function. For each
32 region, there is one associated handler. Regions only ever
33 surround possible context points of throws. Regions with no such
34 context points can be optimized away, as they are trivial, and it
35 is not possible for the associated handler to ever be used during a
38 Semantics are, when an exception is thrown, control is transferred
39 to a handler, and the code of the exception handler is executed.
40 As control is transferred, the machine state (stack pointer, all
41 callee saved registers and possibly the frame pointer) is restored.
43 The handler that is selected by a throw, is the handler associated
44 with the smallest (most nested) region that contains the context of
45 the throw, if such a region exists. If no region exists, the
46 search for a handler continues in the function that called the
47 function that contains the current context of the throw, with the
48 context of the throw then becoming that point in the code that
49 contains the call instruction.
52 One can add to the basic model the concepts of thrown exception
53 type, and thrown exception value. Semantics are as above, except a
54 further check is done when finding a handler for the thrown
55 exception to see if the given handler can handle the thrown
56 exception based upon the exception object's type and possibly its
57 value. A common optimization is when two regions are identical,
58 the handlers are combined into just one handler so the first check
59 of the resulting handler is for the inner (nested) region's
60 handler, and the second one is for the outer region's handler. To
61 separate these two notions of handlers, we can call the subhandlers
62 `catch blocks', and use the name `handler' to refer to the
63 combination of the two. Currently, this layer of functionality is
64 managed by the various front ends.
67 To mark the start of a exception handling region,
68 expand_eh_region_start () is called. To mark the end, and
69 associate a handler for the region expand_eh_region_end () is used.
70 The front end can use this interface, if useful. The back end
71 creates exception regions with these routines. Another interface
72 the front end can use, is TARGET_EXPR. TARGET_EXPR gives an
73 unwind-protect style interface a la emacs.
76 In this implementation, regions do not span more than one function.
78 In order to help with the task of finding the associated handler for
79 a region, an exception table is built which associates handlers
80 with regions. A 3-tuple, containing a reference to the start, the
81 end and the handler is sufficient for the exception table.
83 In order to help with the task of restoring callee saved registers
84 and performing other associated function exit actions, function
85 `unwinders' can be generated within those function for which a
86 generic function unwinder called __unwind_function () cannot work.
87 Whether the generic __unwind_function can work is machine dependent
88 and possibly function dependent. The macro DOESNT_NEEED_UNWINDER
89 decides if the current function being compiled needs an unwinder or
92 The default is for unwinders to be used, as the default generic
93 function unwinder only calls abort (). The compiler-generated per
94 function function unwinders simply modify the context of thrown
95 exception to be that of the call site, and then arrange for control
96 to be transferred to __throw instead of the function's caller on
97 return, and then return. */
106 #include "function.h"
107 #include "insn-flags.h"
109 #include "insn-codes.h"
111 #include "hard-reg-set.h"
112 #include "insn-config.h"
116 /* List of labels use for exception handlers. Created by
117 find_exception_handler_labels for the optimization passes. */
119 rtx exception_handler_labels;
121 /* Nonzero means that throw was used. Used for now, because __throw
122 is emitted statically in each file. */
126 /* A stack used for keeping track of the currectly active exception
127 handling region. As exceptions regions are started, an entry
128 describing the region is pushed onto this stack. The current
129 region can be found by looking at the top of the stack, and as we
130 end regions, entries are poped. */
132 struct eh_stack ehstack;
134 /* A queue used for tracking which exception regions have closed, but
135 whose handlers have not yet been expanded. As we end regions, we
136 enqueue the entry onto this queue. Entries are dequeue from the
137 queue during expand_leftover_cleanups and expand_start_all_catch,
138 and the handlers for regions are expanded in groups in an effort to
139 group all the handlers together in the same region of program space
140 to improve page performance. We should redo things, so that we
141 either take RTL for the handler, or we expand the handler expressed
142 as a tree immediately at region end time. */
144 struct eh_queue ehqueue;
146 /* Insns for the catch clauses. */
150 /* A list of actions for handlers for regions that are not yet
155 /* Stacks to keep track of various labels. */
157 /* Keeps track of the label to resume to, should one want to resume
158 the normal control flow out of a handler. Also used to rethrow
159 exceptions caught in handlers, as if they were physically emitted
162 struct label_node *caught_return_label_stack = NULL;
164 /* A spare data area for the front end's own use. */
166 struct label_node *false_label_stack = NULL;
168 /* The rtx for the saved PC value. */
172 rtx expand_builtin_return_addr PROTO((enum built_in_function, int, rtx));
174 /* Various support routines to manipulate the various data structures
175 used by the exception handling code. */
177 /* Push a label entry onto the given STACK. */
180 push_label_entry (stack, rlabel, tlabel)
181 struct label_node **stack;
185 struct label_node *newnode
186 = (struct label_node *) xmalloc (sizeof (struct label_node));
189 newnode->u.rlabel = rlabel;
191 newnode->u.tlabel = tlabel;
192 newnode->chain = *stack;
196 /* Pop a label entry from the given STACK. */
199 pop_label_entry (stack)
200 struct label_node **stack;
203 struct label_node *tempnode;
209 label = tempnode->u.rlabel;
210 *stack = (*stack)->chain;
216 /* Return the top element of the given STACK. */
219 top_label_entry (stack)
220 struct label_node **stack;
225 return (*stack)->u.tlabel;
230 static struct eh_entry *
231 copy_eh_entry (entry)
232 struct eh_entry *entry;
234 struct eh_entry *newentry;
236 newentry = (struct eh_entry *) xmalloc (sizeof (struct eh_entry));
237 bcopy ((char *) entry, (char *) newentry, sizeof (struct eh_entry));
242 /* Push an entry onto the given STACK. */
245 push_eh_entry (stack)
246 struct eh_stack *stack;
248 struct eh_node *node = (struct eh_node *) xmalloc (sizeof (struct eh_node));
249 struct eh_entry *entry = (struct eh_entry *) xmalloc (sizeof (struct eh_entry));
251 entry->start_label = gen_label_rtx ();
252 entry->end_label = gen_label_rtx ();
253 entry->exception_handler_label = gen_label_rtx ();
254 entry->finalization = NULL_TREE;
257 node->chain = stack->top;
260 return entry->start_label;
263 /* Pop an entry from the given STACK. */
265 static struct eh_entry *
267 struct eh_stack *stack;
269 struct eh_node *tempnode;
270 struct eh_entry *tempentry;
272 tempnode = stack->top;
273 tempentry = tempnode->entry;
274 stack->top = stack->top->chain;
280 /* Enqueue an ENTRY onto the given QUEUE. */
283 enqueue_eh_entry (queue, entry)
284 struct eh_queue *queue;
285 struct eh_entry *entry;
287 struct eh_node *node = (struct eh_node *) xmalloc (sizeof (struct eh_node));
292 if (queue->head == NULL)
298 queue->tail->chain = node;
303 /* Dequeue an entry from the given QUEUE. */
305 static struct eh_entry *
306 dequeue_eh_entry (queue)
307 struct eh_queue *queue;
309 struct eh_node *tempnode;
310 struct eh_entry *tempentry;
312 if (queue->head == NULL)
315 tempnode = queue->head;
316 queue->head = queue->head->chain;
318 tempentry = tempnode->entry;
324 /* Routine to see if exception exception handling is turned on.
325 DO_WARN is non-zero if we want to inform the user that exception
326 handling is turned off. */
332 if (! flag_exceptions)
334 static int warned = 0;
335 if (! warned && do_warn)
337 error ("exception handling disabled, use -fexceptions to enable");
345 /* Given the return address in ADDR, compute the new pc to throw.
346 This has to work for the current frame of the current function, and
347 the one above it in the case of throw. */
350 eh_outer_context (addr)
353 /* First mask out any unwanted bits. */
354 #ifdef MASK_RETURN_ADDR
355 emit_insn (gen_rtx (SET, Pmode,
358 addr, MASK_RETURN_ADDR)));
361 /* Then subtract out enough to get into the prior region. If this
362 is defined, assume we don't need to subtract anything, as it is
363 already within the region. */
364 #if ! defined (RETURN_ADDR_OFFSET)
365 addr = plus_constant (addr, -1);
371 /* Output a note marking the start of an exception handling region. */
374 expand_eh_region_start ()
378 /* This is the old code. */
383 /* Maybe do this to prevent jumping in and so on... */
387 note = emit_note (NULL_PTR, NOTE_INSN_EH_REGION_BEG);
388 emit_label (push_eh_entry (&ehstack));
389 NOTE_BLOCK_NUMBER (note)
390 = CODE_LABEL_NUMBER (ehstack.top->entry->exception_handler_label);
393 /* Output a note marking the end of an exception handling region.
394 HANDLER is the the handler for the exception region. */
397 expand_eh_region_end (handler)
402 struct eh_entry *entry;
407 entry = pop_eh_entry (&ehstack);
409 note = emit_note (NULL_PTR, NOTE_INSN_EH_REGION_END);
410 NOTE_BLOCK_NUMBER (note) = CODE_LABEL_NUMBER (entry->exception_handler_label);
412 emit_label (entry->end_label);
414 /* Put in something that takes up space, as otherwise the end
415 address for the EH region could have the exact same address as
416 the outer region, causing us to miss the fact that resuming
417 exception handling with this PC value would be inside the outer
419 emit_insn (gen_nop ());
421 entry->finalization = handler;
423 enqueue_eh_entry (&ehqueue, entry);
427 /* Makebe do this to prevent jumping in and so on... */
432 /* Emit a call to __throw and note that we threw something. */
438 emit_indirect_jump (throw_libfunc);
440 SYMBOL_REF_USED (throw_libfunc) = 1;
441 emit_library_call (throw_libfunc, 0, VOIDmode, 0);
447 /* An internal throw with an indirect CONTEXT we want to throw from. */
450 expand_internal_throw_indirect (context)
453 emit_move_insn (eh_saved_pc_rtx, context);
457 /* An internal throw with a direct CONTEXT we want to throw from. The
458 context should be a label. */
461 expand_internal_throw (context)
464 expand_internal_throw_indirect (gen_rtx (LABEL_REF, Pmode, context));
467 /* Called from expand_exception_blocks and expand_end_catch_block to
468 expand any pending handlers. */
471 expand_leftover_cleanups ()
473 struct eh_entry *entry;
475 while ((entry = dequeue_eh_entry (&ehqueue)) != 0)
479 emit_label (entry->exception_handler_label);
481 expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
483 prev = get_last_insn ();
484 if (! (prev && GET_CODE (prev) == BARRIER))
486 /* The below can be optimized away, and we could just fall into the
487 next EH handler, if we are certain they are nested. */
488 /* Code to throw out to outer context, if we fall off end of the
490 expand_internal_throw (entry->end_label);
493 /* leftover try block, opps. */
494 if (entry->finalization == integer_zero_node)
501 /* Generate RTL for the start of all the catch blocks. Used for
502 arranging for the exception handling code to be placed farther out
503 of line than normal. */
506 expand_start_all_catch ()
508 struct eh_entry *entry;
514 emit_line_note (input_filename, lineno);
515 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
517 /* The label for the exception handling block we will save. This is
518 Lresume, in the documention. */
519 expand_label (label);
521 /* Put in something that takes up space, as otherwise the end
522 address for the EH region could have the exact same address as
523 the outer region, causing us to miss the fact that resuming
524 exception handling with this PC value would be inside the outer
526 emit_insn (gen_nop ());
528 push_label_entry (&caught_return_label_stack, NULL_RTX, label);
530 /* Start a new sequence for all the catch blocks. We will add this
531 to the gloabl sequence catch_clauses, when we have completed all
532 the handlers in this handler-seq. */
539 entry = dequeue_eh_entry (&ehqueue);
540 emit_label (entry->exception_handler_label);
542 expand_expr (entry->finalization, const0_rtx, VOIDmode, 0);
544 /* When we get down to the matching entry, stop. */
545 if (entry->finalization == integer_zero_node)
548 prev = get_last_insn ();
549 if (! (prev && GET_CODE (prev) == BARRIER))
551 /* The below can be optimized away, and we could just fall into the
552 next EH handler, if we are certain they are nested. */
553 /* Code to throw out to outer context, if we fall off end of the
555 expand_internal_throw (entry->end_label);
562 /* Generate RTL for the end of all the catch blocks. */
565 expand_end_all_catch ()
567 rtx new_catch_clause;
572 /* Code to throw out to outer context, if we fall off end of catch
573 handlers. This is rethrow (Lresume, same id, same obj); in the
575 expand_internal_throw (DECL_RTL (top_label_entry (&caught_return_label_stack)));
577 /* Now we have the complete catch sequence. */
578 new_catch_clause = get_insns ();
581 /* This level of catch blocks is done, so set up the successful
582 catch jump label for the next layer of catch blocks. */
583 pop_label_entry (&caught_return_label_stack);
585 /* Add the new sequence of catches to the main one for this function. */
586 push_to_sequence (catch_clauses);
587 emit_insns (new_catch_clause);
588 catch_clauses = get_insns ();
591 /* Here we fall through into the continuation code. */
594 /* End all the pending exception regions from protect_list that have
595 been started, but not yet completed. */
598 end_protect_partials ()
602 expand_eh_region_end (TREE_VALUE (protect_list));
603 protect_list = TREE_CHAIN (protect_list);
607 /* The exception table that we build that is used for looking up and
608 dispatching exceptions, it's size, and it's maximum size before we
609 have to extend it. */
610 static int *eh_table;
611 static int eh_table_size;
612 static int eh_table_max_size;
614 /* Note the need for an exception table entry for region N. If we
615 don't need to output an explicit exception table, avoid all the
616 extra work. Called during final_scan_insn time. */
619 add_eh_table_entry (n)
622 #ifndef OMIT_EH_TABLE
623 if (eh_table_size >= eh_table_max_size)
627 eh_table_max_size += eh_table_max_size>>1;
629 if (eh_table_max_size < 0)
632 if ((eh_table = (int *) realloc (eh_table, eh_table_max_size))
634 fatal ("virtual memory exhausted");
638 eh_table_max_size = 252;
639 eh_table = (int *) xmalloc (eh_table_max_size * sizeof (int));
642 eh_table[eh_table_size++] = n;
646 /* Conditional to test to see if we need to output an exception table.
647 Note, on some platforms, we don't have to output a table
648 explicitly. This routine doesn't mean we don't have one. */
659 /* Output an entry N for the exception table to the specified FILE. */
662 output_exception_table_entry (file, n)
669 ASM_GENERATE_INTERNAL_LABEL (buf, "LEHB", n);
670 sym = gen_rtx (SYMBOL_REF, Pmode, buf);
671 assemble_integer (sym, POINTER_SIZE / BITS_PER_UNIT, 1);
673 ASM_GENERATE_INTERNAL_LABEL (buf, "LEHE", n);
674 sym = gen_rtx (SYMBOL_REF, Pmode, buf);
675 assemble_integer (sym, POINTER_SIZE / BITS_PER_UNIT, 1);
677 ASM_GENERATE_INTERNAL_LABEL (buf, "L", n);
678 sym = gen_rtx (SYMBOL_REF, Pmode, buf);
679 assemble_integer (sym, POINTER_SIZE / BITS_PER_UNIT, 1);
681 putc ('\n', file); /* blank line */
684 /* Output the exception table if we have one and need one. */
687 output_exception_table ()
690 extern FILE *asm_out_file;
695 exception_section ();
697 /* Beginning marker for table. */
698 assemble_align (GET_MODE_ALIGNMENT (ptr_mode));
699 assemble_label ("__EXCEPTION_TABLE__");
701 assemble_integer (const0_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
702 assemble_integer (const0_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
703 assemble_integer (const0_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
704 putc ('\n', asm_out_file); /* blank line */
706 for (i = 0; i < eh_table_size; ++i)
707 output_exception_table_entry (asm_out_file, eh_table[i]);
711 /* Ending marker for table. */
712 assemble_label ("__EXCEPTION_END__");
713 assemble_integer (constm1_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
714 assemble_integer (constm1_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
715 assemble_integer (constm1_rtx, POINTER_SIZE / BITS_PER_UNIT, 1);
716 putc ('\n', asm_out_file); /* blank line */
719 /* Generate code to initialize the exception table at program startup
723 register_exception_table ()
725 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__register_exceptions"), 0,
727 gen_rtx (SYMBOL_REF, Pmode, "__EXCEPTION_TABLE__"),
731 /* Emit the RTL for the start of the per function unwinder for the
737 #ifdef DOESNT_NEED_UNWINDER
738 if (DOESNT_NEED_UNWINDER)
742 expand_eh_region_start ();
745 /* Emit the RTL for the end of the per function unwinder for the
752 rtx return_val_rtx, ret_val, label, end, insns;
757 #ifdef DOESNT_NEED_UNWINDER
758 if (DOESNT_NEED_UNWINDER)
762 expr = make_node (RTL_EXPR);
763 TREE_TYPE (expr) = void_type_node;
764 RTL_EXPR_RTL (expr) = const0_rtx;
765 TREE_SIDE_EFFECTS (expr) = 1;
766 start_sequence_for_rtl_expr (expr);
768 ret_val = expand_builtin_return_addr (BUILT_IN_RETURN_ADDRESS,
769 0, hard_frame_pointer_rtx);
770 return_val_rtx = copy_to_reg (ret_val);
772 return_val_rtx = eh_outer_context (return_val_rtx);
774 emit_move_insn (eh_saved_pc_rtx, return_val_rtx);
777 emit_move_insn (ret_val, throw_libfunc);
779 label = gen_label_rtx ();
780 emit_move_insn (ret_val, gen_rtx (LABEL_REF, Pmode, label));
783 #ifdef RETURN_ADDR_OFFSET
784 return_val_rtx = plus_constant (ret_val, -RETURN_ADDR_OFFSET);
785 if (return_val_rtx != ret_val)
786 emit_move_insn (ret_val, return_val_rtx);
789 end = gen_label_rtx ();
792 RTL_EXPR_SEQUENCE (expr) = get_insns ();
794 expand_eh_region_end (expr);
798 #ifndef JUMP_TO_THROW
803 expand_leftover_cleanups ();
808 /* Emit the RTL for the per function unwinder for the current
809 function, if needed. Called after all the code that needs unwind
810 protection is output. */
818 start_eh_unwinder ();
819 insns = get_insns ();
823 emit_insns_after (insns, get_insns ());
828 /* Scan the current insns and build a list of handler labels. Called
829 after the last exception handling region is added to the current
830 function (when the rtl is almost all built for the current
831 function) and before the jump optimization pass. */
834 find_exception_handler_labels ()
837 int max_labelno = max_label_num ();
838 int min_labelno = get_first_label_num ();
841 exception_handler_labels = NULL_RTX;
843 /* If we aren't doing exception handling, there isn't much to check. */
847 /* First we generate a handy reference to each label. */
849 labels = (rtx *) alloca ((max_labelno - min_labelno) * sizeof (rtx));
850 labels -= min_labelno;
852 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
854 if (GET_CODE (insn) == CODE_LABEL)
855 if (CODE_LABEL_NUMBER (insn) >= min_labelno
856 && CODE_LABEL_NUMBER (insn) < max_labelno)
857 labels[CODE_LABEL_NUMBER (insn)] = insn;
860 /* Then for each start of a region, we add its label to the list. */
861 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
863 if (GET_CODE (insn) == NOTE
864 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
866 rtx label = NULL_RTX;
868 if (NOTE_BLOCK_NUMBER (insn) >= min_labelno
869 && NOTE_BLOCK_NUMBER (insn) < max_labelno)
871 label = labels[NOTE_BLOCK_NUMBER (insn)];
874 exception_handler_labels
875 = gen_rtx (EXPR_LIST, VOIDmode,
876 label, exception_handler_labels);
878 warning ("didn't find handler for EH region %d",
879 NOTE_BLOCK_NUMBER (insn));
882 warning ("mismatched EH region %d", NOTE_BLOCK_NUMBER (insn));
887 /* Do some sanity checking on the exception_handler_labels list. Can
888 be called after find_exception_handler_labels is called to build
889 the list of exception handlers for the current function, and before
890 we finish processing the current function. */
893 check_exception_handler_labels ()
897 /* If we aren't doing exception handling, there isn't much to check. */
901 for (handler = exception_handler_labels;
903 handler = XEXP (handler, 1))
905 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
907 if (GET_CODE (insn) == CODE_LABEL)
909 if (CODE_LABEL_NUMBER (insn)
910 == CODE_LABEL_NUMBER (XEXP (handler, 0)))
912 if (insn != XEXP (handler, 0))
913 warning ("mismatched handler %d",
914 CODE_LABEL_NUMBER (insn));
919 if (insn == NULL_RTX)
920 warning ("handler not found %d",
921 CODE_LABEL_NUMBER (XEXP (handler, 0)));
924 /* Now go through, and make sure that for each region we have, that we
925 have the corresponding label. */
926 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
928 if (GET_CODE (insn) == NOTE
929 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG ||
930 NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
932 for (handler = exception_handler_labels;
934 handler = XEXP (handler, 1))
936 if (CODE_LABEL_NUMBER (XEXP (handler, 0))
937 == NOTE_BLOCK_NUMBER (insn))
940 if (handler == NULL_RTX)
941 warning ("region exists, no handler %d",
942 NOTE_BLOCK_NUMBER (insn));
947 /* This group of functions initializes the exception handling data
948 structures at the start of the compilation, initializes the data
949 structures at the start of a function, saves and restores the
950 exception handling data structures for the start/end of a nested
953 /* Toplevel initialization for EH things. */
958 eh_saved_pc_rtx = gen_rtx (MEM, ptr_mode,
959 gen_rtx (SYMBOL_REF, Pmode, "__eh_pc"));
962 /* Initialize various EH things. */
965 init_eh_for_function ()
968 ehqueue.head = ehqueue.tail = 0;
969 catch_clauses = NULL_RTX;
970 false_label_stack = 0;
971 caught_return_label_stack = 0;
972 protect_list = NULL_TREE;
975 /* Save various EH things for the current function into the save area
982 p->ehstack = ehstack;
983 p->ehqueue = ehqueue;
984 p->catch_clauses = catch_clauses;
985 p->false_label_stack = false_label_stack;
986 p->caught_return_label_stack = caught_return_label_stack;
987 p->protect_list = protect_list;
992 /* Restore various EH things for the current function from the save
993 area denoted by P. */
996 restore_eh_status (p)
999 protect_list = p->protect_list;
1000 caught_return_label_stack = p->caught_return_label_stack;
1001 false_label_stack = p->false_label_stack;
1002 catch_clauses = p->catch_clauses;
1003 ehqueue = p->ehqueue;
1004 ehstack = p->ehstack;
1007 /* This section is for the exception handling specific optimization
1008 pass. First are the internal routines, and then the main
1009 optimization pass. */
1011 /* Determine if the given INSN can throw an exception. */
1017 /* The only things that can possibly throw are calls. */
1018 if (GET_CODE (insn) == CALL_INSN)
1021 #ifdef ASYNCH_EXCEPTIONS
1022 /* If we wanted asynchronous exceptions, then everything but NOTEs
1023 and CODE_LABELs could throw. */
1024 if (GET_CODE (insn) != NOTE && GET_CODE (insn) != CODE_LABEL)
1031 /* Scan a region, looking for a matching end, and decide if the region
1032 can be removed. INSN is the start of the region, N is the region
1033 number, and DELETE_OUTER is to note if anything in this region can
1037 scan_region (insn, n, delete_outer)
1044 /* Assume we can delete the region. */
1047 insn = NEXT_INSN (insn);
1049 /* Look for the matching end. */
1050 while (! (GET_CODE (insn) == NOTE
1051 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1053 /* If anything can throw, we can't remove the region. */
1054 if (delete && can_throw (insn))
1059 /* Watch out for and handle nested regions. */
1060 if (GET_CODE (insn) == NOTE
1061 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
1063 insn = scan_region (insn, NOTE_BLOCK_NUMBER (insn), &delete);
1066 insn = NEXT_INSN (insn);
1069 /* The _BEG/_END NOTEs must match and nest. */
1070 if (NOTE_BLOCK_NUMBER (insn) != n)
1073 /* If anything can throw, we can throw. */
1078 /* Delete the start and end of the region. */
1079 delete_insn (start);
1082 /* Only do this part if we have built the exception handler
1084 if (exception_handler_labels)
1086 rtx x, *prev = &exception_handler_labels;
1088 /* Find it in the list of handlers. */
1089 for (x = exception_handler_labels; x; x = XEXP (x, 1))
1091 rtx label = XEXP (x, 0);
1092 if (CODE_LABEL_NUMBER (label) == n)
1094 /* If we are the last reference to the handler,
1096 if (--LABEL_NUSES (label) == 0)
1097 delete_insn (label);
1101 /* Remove it from the list of exception handler
1102 labels, if we are optimizing. If we are not, then
1103 leave it in the list, as we are not really going to
1104 remove the region. */
1105 *prev = XEXP (x, 1);
1112 prev = &XEXP (x, 1);
1119 /* Perform various interesting optimizations for exception handling
1122 We find empty exception regions, and remove them. The jump
1123 optimization code will remove the handler if nothing else uses it. */
1126 exception_optimize ()
1128 rtx insn, regions = NULL_RTX;
1131 /* First remove empty regions. */
1132 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1134 if (GET_CODE (insn) == NOTE
1135 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
1137 insn = scan_region (insn, NOTE_BLOCK_NUMBER (insn), &n);