-\f
-/* Optimize code of the form:
-
- for (x = a[i]; x; ...)
- ...
- for (x = a[i]; x; ...)
- ...
- foo:
-
- Loop optimize will change the above code into
-
- if (x = a[i])
- for (;;)
- { ...; if (! (x = ...)) break; }
- if (x = a[i])
- for (;;)
- { ...; if (! (x = ...)) break; }
- foo:
-
- In general, if the first test fails, the program can branch
- directly to `foo' and skip the second try which is doomed to fail.
- We run this after loop optimization and before flow analysis. */
-
-/* When comparing the insn patterns, we track the fact that different
- pseudo-register numbers may have been used in each computation.
- The following array stores an equivalence -- same_regs[I] == J means
- that pseudo register I was used in the first set of tests in a context
- where J was used in the second set. We also count the number of such
- pending equivalences. If nonzero, the expressions really aren't the
- same. */
-
-static int *same_regs;
-
-static int num_same_regs;
-
-/* Track any registers modified between the target of the first jump and
- the second jump. They never compare equal. */
-
-static char *modified_regs;
-
-/* Record if memory was modified. */
-
-static int modified_mem;
-
-/* Called via note_stores on each insn between the target of the first
- branch and the second branch. It marks any changed registers. */
-
-static void
-mark_modified_reg (dest, x, data)
- rtx dest;
- rtx x ATTRIBUTE_UNUSED;
- void *data ATTRIBUTE_UNUSED;
-{
- int regno;
- unsigned int i;
-
- if (GET_CODE (dest) == SUBREG)
- dest = SUBREG_REG (dest);
-
- if (GET_CODE (dest) == MEM)
- modified_mem = 1;
-
- if (GET_CODE (dest) != REG)
- return;
-
- regno = REGNO (dest);
- if (regno >= FIRST_PSEUDO_REGISTER)
- modified_regs[regno] = 1;
- else
- for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
- modified_regs[regno + i] = 1;
-}
-
-/* F is the first insn in the chain of insns. */
-
-void
-thread_jumps (f, max_reg, flag_before_loop)
- rtx f;
- int max_reg;
- int flag_before_loop;
-{
- /* Basic algorithm is to find a conditional branch,
- the label it may branch to, and the branch after
- that label. If the two branches test the same condition,
- walk back from both branch paths until the insn patterns
- differ, or code labels are hit. If we make it back to
- the target of the first branch, then we know that the first branch
- will either always succeed or always fail depending on the relative
- senses of the two branches. So adjust the first branch accordingly
- in this case. */
-
- rtx label, b1, b2, t1, t2;
- enum rtx_code code1, code2;
- rtx b1op0, b1op1, b2op0, b2op1;
- int changed = 1;
- int i;
- int *all_reset;
-
- /* Allocate register tables and quick-reset table. */
- modified_regs = (char *) xmalloc (max_reg * sizeof (char));
- same_regs = (int *) xmalloc (max_reg * sizeof (int));
- all_reset = (int *) xmalloc (max_reg * sizeof (int));
- for (i = 0; i < max_reg; i++)
- all_reset[i] = -1;
-
- while (changed)
- {
- changed = 0;
-
- for (b1 = f; b1; b1 = NEXT_INSN (b1))
- {
- rtx set;
- rtx set2;
-
- /* Get to a candidate branch insn. */
- if (GET_CODE (b1) != JUMP_INSN
- || ! any_condjump_p (b1) || JUMP_LABEL (b1) == 0)
- continue;
-
- memset (modified_regs, 0, max_reg * sizeof (char));
- modified_mem = 0;
-
- memcpy (same_regs, all_reset, max_reg * sizeof (int));
- num_same_regs = 0;
-
- label = JUMP_LABEL (b1);
-
- /* Look for a branch after the target. Record any registers and
- memory modified between the target and the branch. Stop when we
- get to a label since we can't know what was changed there. */
- for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
- {
- if (GET_CODE (b2) == CODE_LABEL)
- break;
-
- else if (GET_CODE (b2) == JUMP_INSN)
- {
- /* If this is an unconditional jump and is the only use of
- its target label, we can follow it. */
- if (any_uncondjump_p (b2)
- && onlyjump_p (b2)
- && JUMP_LABEL (b2) != 0
- && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
- {
- b2 = JUMP_LABEL (b2);
- continue;
- }
- else
- break;
- }
-
- if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
- continue;
-
- if (GET_CODE (b2) == CALL_INSN)
- {
- modified_mem = 1;
- for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
- if (call_used_regs[i] && ! fixed_regs[i]
- && i != STACK_POINTER_REGNUM
- && i != FRAME_POINTER_REGNUM
- && i != HARD_FRAME_POINTER_REGNUM
- && i != ARG_POINTER_REGNUM)
- modified_regs[i] = 1;
- }
-
- note_stores (PATTERN (b2), mark_modified_reg, NULL);
- }
-
- /* Check the next candidate branch insn from the label
- of the first. */
- if (b2 == 0
- || GET_CODE (b2) != JUMP_INSN
- || b2 == b1
- || !any_condjump_p (b2)
- || !onlyjump_p (b2))
- continue;
- set = pc_set (b1);
- set2 = pc_set (b2);
-
- /* Get the comparison codes and operands, reversing the
- codes if appropriate. If we don't have comparison codes,
- we can't do anything. */
- b1op0 = XEXP (XEXP (SET_SRC (set), 0), 0);
- b1op1 = XEXP (XEXP (SET_SRC (set), 0), 1);
- code1 = GET_CODE (XEXP (SET_SRC (set), 0));
- if (XEXP (SET_SRC (set), 1) == pc_rtx)
- code1 = reverse_condition (code1);
-
- b2op0 = XEXP (XEXP (SET_SRC (set2), 0), 0);
- b2op1 = XEXP (XEXP (SET_SRC (set2), 0), 1);
- code2 = GET_CODE (XEXP (SET_SRC (set2), 0));
- if (XEXP (SET_SRC (set2), 1) == pc_rtx)
- code2 = reverse_condition (code2);
-
- /* If they test the same things and knowing that B1 branches
- tells us whether or not B2 branches, check if we
- can thread the branch. */
- if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
- && rtx_equal_for_thread_p (b1op1, b2op1, b2)
- && (comparison_dominates_p (code1, code2)
- || (can_reverse_comparison_p (XEXP (SET_SRC (set), 0), b1)
- && comparison_dominates_p (code1,
- reverse_condition (code2)))))
-
- {
- t1 = prev_nonnote_insn (b1);
- t2 = prev_nonnote_insn (b2);
-
- while (t1 != 0 && t2 != 0)
- {
- if (t2 == label)
- {
- /* We have reached the target of the first branch.
- If there are no pending register equivalents,
- we know that this branch will either always
- succeed (if the senses of the two branches are
- the same) or always fail (if not). */
- rtx new_label;
-
- if (num_same_regs != 0)
- break;
-
- if (comparison_dominates_p (code1, code2))
- new_label = JUMP_LABEL (b2);
- else
- new_label = get_label_after (b2);
-
- if (JUMP_LABEL (b1) != new_label)
- {
- rtx prev = PREV_INSN (new_label);
-
- if (flag_before_loop
- && GET_CODE (prev) == NOTE
- && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
- {
- /* Don't thread to the loop label. If a loop
- label is reused, loop optimization will
- be disabled for that loop. */
- new_label = gen_label_rtx ();
- emit_label_after (new_label, PREV_INSN (prev));
- }
- changed |= redirect_jump (b1, new_label, 1);
- }
- break;
- }
-
- /* If either of these is not a normal insn (it might be
- a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
- have already been skipped above.) Similarly, fail
- if the insns are different. */
- if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
- || recog_memoized (t1) != recog_memoized (t2)
- || ! rtx_equal_for_thread_p (PATTERN (t1),
- PATTERN (t2), t2))
- break;
-
- t1 = prev_nonnote_insn (t1);
- t2 = prev_nonnote_insn (t2);
- }
- }
- }
- }
-
- /* Clean up. */
- free (modified_regs);
- free (same_regs);
- free (all_reset);
-}
-\f
-/* This is like RTX_EQUAL_P except that it knows about our handling of
- possibly equivalent registers and knows to consider volatile and
- modified objects as not equal.
-
- YINSN is the insn containing Y. */