fixup sh bustage after r209483
[official-gcc.git] / gcc / config / sh / sh_treg_combine.cc
blob254847971722dadc5a0cb1ea29b76ea564e92f23
1 /* An SH specific RTL pass that tries to combine comparisons and redundant
2 condition code register stores across multiple basic blocks.
3 Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC 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 3, or (at your option)
10 any later version.
12 GCC 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 GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "machmode.h"
25 #include "basic-block.h"
26 #include "df.h"
27 #include "rtl.h"
28 #include "insn-config.h"
29 #include "insn-codes.h"
30 #include "emit-rtl.h"
31 #include "recog.h"
32 #include "tree-pass.h"
33 #include "target.h"
34 #include "expr.h"
36 #include <algorithm>
37 #include <list>
38 #include <vector>
41 This pass tries to optimize for example this:
42 mov.l @(4,r4),r1
43 tst r1,r1
44 movt r1
45 tst r1,r1
46 bt/s .L5
48 into something simpler:
49 mov.l @(4,r4),r1
50 tst r1,r1
51 bf/s .L5
53 Such sequences can be identified by looking for conditional branches and
54 checking whether the ccreg is set before the conditional branch
55 by testing another register for != 0, which was set by a ccreg store.
56 This can be optimized by eliminating the redundant comparison and
57 inverting the branch condition. There can be multiple comparisons in
58 different basic blocks that all end up in the redunant test insn before the
59 conditional branch. Some example RTL ...
61 Example 1)
62 ----------
64 [bb 3]
65 (set (reg:SI 147 t) (eq:SI (reg:SI 173) (const_int 0)))
66 (set (reg:SI 167) (xor:SI (reg:SI 147 t) (const_int 1)))
67 -> bb 5
69 [bb 4]
70 (set (reg:SI 147 t) (eq:SI (reg:SI 177) (const_int 0)))
71 (set (reg:SI 167) (reg:SI 147 t))
72 -> bb 5
74 [bb 5]
75 (set (reg:SI 147 t) (eq:SI (reg:SI 167) (const_int 0)))
76 (set (pc) (if_then_else (ne (reg:SI 147 t) (const_int 0))
77 (label_ref:SI 50) (pc)))
79 In [bb 4] elimination of the comparison would require inversion of the branch
80 condition and compensation of other BBs.
81 Instead an inverting reg-move can be used:
83 [bb 3]
84 (set (reg:SI 167) (reg:SI 173))
85 -> bb 5
87 [BB 4]
88 (set (reg:SI 167) (not:SI (reg:SI 177)))
89 -> bb 5
91 [bb 5]
92 (set (reg:SI 147 t) (eq:SI (reg:SI 167) (const_int 0)))
93 (set (pc) (if_then_else (ne (reg:SI 147 t) (const_int 0)))
94 (label_ref:SI 50) (pc)))
97 Example 2)
98 ----------
100 [bb 3]
101 (set (reg:SI 147 t) (gt:SI (reg:SI 173) (reg:SI 175)))
102 (set (reg:SI 167) (reg:SI 147 t))
103 -> bb 5
105 [bb 4]
106 (set (reg:SI 147 t) (gt:SI (reg:SI 177) (reg:SI 179)))
107 (set (reg:SI 167) (reg:SI 147 t))
108 -> bb 5
110 [bb 5]
111 (set (reg:SI 147 t) (eq:SI (reg:SI 167) (const_int 0)))
112 (set (pc) (if_then_else (ne (reg:SI 147 t) (const_int 0))
113 (label_ref:SI 51) (pc)))
115 The common comparison is factored out and the branch condition is inverted:
117 [bb 3]
118 (set (reg:SI 167) (reg:SI 173))
119 (set (reg:SI 200) (reg:SI 175))
120 -> bb 5
122 [bb 4]
123 (set (reg:SI 167) (reg:SI 177))
124 (set (reg:SI 200) (reg:SI 179))
125 -> bb 5
127 [bb 5]
128 (set (reg:SI 147 t) (gt:SI (reg:SI 167) (reg:SI 200)))
129 (set (pc) (if_then_else (eq (reg:SI 147 t) (const_int 0))
130 (label_ref:SI 51) (pc)))
133 Example 3)
134 ----------
136 [bb 3]
137 (set (reg:SI 147 t) (gt:SI (reg:SI 173) (reg:SI 175)))
138 (set (reg:SI 167) (reg:SI 147 t))
139 -> bb 5
141 [bb 4]
142 (set (reg:SI 147 t) (ge:SI (reg:SI 179) (reg:SI 177)))
143 (set (reg:SI 167) (reg:SI 147 t))
144 -> bb 5
146 [bb 5]
147 (set (reg:SI 147 t) (eq:SI (reg:SI 167) (const_int 0)))
148 (set (pc) (if_then_else (ne (reg:SI 147 t) (const_int 0))
149 (label_ref:SI 51) (pc)))
151 The T bit lifetime is extended and the branch condition is inverted:
153 [bb 3]
154 (set (reg:SI 147 t) (gt:SI (reg:SI 173) (reg:SI 175)))
155 -> bb 5
157 [bb 4]
158 (set (reg:SI 147 t) (ge:SI (reg:SI 179) (reg:SI 177)))
159 -> bb 5
161 [bb 5]
162 (set (pc) (if_then_else (eq (reg:SI 147 t) (const_int 0))
163 (label_ref:SI 51) (pc)))
166 Example 4)
167 ----------
169 [bb 3]
170 (set (reg:SI 147 t) (eq:SI (reg:SI 173) (const_int 5)))
171 (set (reg:SI 167) (reg:SI 147 t))
172 -> bb 5
174 [bb 4]
175 (set (reg:SI 147 t) (eq:SI (reg:SI 176) (const_int 5)))
176 (set (reg:SI 167) (xor:SI (reg:SI 147 t) (const_int 1)))
177 -> bb 5
179 [bb 5]
180 (set (reg:SI 147 t) (eq:SI (reg:SI 167) (const_int 0)))
181 (set (pc) (if_then_else (ne (reg:SI 147 t) (const_int 0))
182 (label_ref:SI 50) (pc)))
184 In this case the comparisons are the same and could be combined, but the
185 branch condition is different for [bb 3] and [bb 5]. Since the comparison
186 is not a zero comparison, we can't negate one of the operands. The best thing
187 we can do here is to eliminate the comparison before the cbranch and invert
188 the ccreg in one of the BBs. On SH2A this will utilize the 'nott' instruction.
190 [bb 3]
191 (set (reg:SI 147 t) (eq:SI (reg:SI 173) (const_int 5)))
192 -> bb 5
194 [bb 4]
195 (set (reg:SI 147 t) (eq:SI (reg:SI 176) (const_int 5)))
196 (set (reg:SI 147 t) (xor:SI (reg:SI 147 t) (const_int 1)))
197 -> bb 5
199 [bb 5]
200 (set (pc) (if_then_else (eq (reg:SI 147 t) (const_int 0)) // inverted
201 (label_ref:SI 50) (pc)))
204 In order to handle cases such as above the RTL pass does the following:
206 - Find the ccreg sets (comparisons) and ccreg stores
207 (inverting and non-inverting) in all related BBs.
209 - If the comparison types in the BBs are all the same, try to combine the
210 comparisons in the BBs and replace the zero comparison before the cbranch
211 with the common comparison.
213 - If the cstores are the same, move the comparison before the cbranch
214 and replace the comparisons in the BBs with reg-reg copies to get the
215 operands in place (create new pseudo regs).
217 - If the cstores differ, try to apply the special case
218 (eq (reg) (const_int 0)) -> inverted = (not (reg)).
219 for the subordinate cstore types and eliminate the dominating ones.
221 - If the comparison types in the BBs are not the same, or the first approach
222 doesn't work out for some reason, try to eliminate the comparison before the
223 cbranch by extending the lifetime of the ccreg by leaving the individual
224 comparisons but eliminating the cstores.
225 If the cstores are all the same this is straight forward.
226 If they're not, try to reverse the ccreg for the subordinate cstore type
227 and eliminate the dominating one.
230 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
231 // Helper functions
233 #define log_msg(...)\
234 do { if (dump_file != NULL) fprintf (dump_file, __VA_ARGS__); } while (0)
236 #define log_insn(i)\
237 do { if (dump_file != NULL) print_rtl_single (dump_file, \
238 (const_rtx)i); } while (0)
240 #define log_rtx(r)\
241 do { if (dump_file != NULL) print_rtl (dump_file, (const_rtx)r); } while (0)
243 #define log_return(retval, ...)\
244 do { if (dump_file != NULL) fprintf (dump_file, __VA_ARGS__); \
245 return retval; } while (0)
247 #define log_return_void(...)\
248 do { if (dump_file != NULL) fprintf (dump_file, __VA_ARGS__); \
249 return; } while (0)
251 struct set_of_reg
253 // The insn where the search stopped or NULL_RTX.
254 rtx insn;
256 // The set rtx of the specified reg if found, NULL_RTX otherwise.
257 // Notice that the set rtx can also be in a parallel.
258 const_rtx set_rtx;
260 // The set source operand rtx if found, NULL_RTX otherwise.
262 set_src (void) const
264 return set_rtx == NULL_RTX ? NULL_RTX : XEXP (set_rtx, 1);
267 // The set destination operand rtx if found, NULL_RTX otherwise.
269 set_dst (void) const
271 return set_rtx == NULL_RTX ? NULL_RTX : XEXP (set_rtx, 0);
274 bool
275 empty (void) const
277 return insn == NULL_RTX || set_rtx == NULL_RTX;
281 // Given a reg rtx and a start insn find the insn (in the same basic block)
282 // that sets the reg.
283 static set_of_reg
284 find_set_of_reg_bb (rtx reg, rtx insn)
286 set_of_reg result = { insn, NULL_RTX };
288 if (!REG_P (reg) || insn == NULL_RTX)
289 return result;
291 for (result.insn = insn; result.insn != NULL_RTX;
292 result.insn = prev_nonnote_insn_bb (result.insn))
294 if (BARRIER_P (result.insn))
295 return result;
296 if (!NONJUMP_INSN_P (result.insn))
297 continue;
298 if (reg_set_p (reg, result.insn))
300 result.set_rtx = set_of (reg, result.insn);
301 if (result.set_rtx == NULL_RTX || GET_CODE (result.set_rtx) != SET)
302 result.set_rtx = NULL_RTX;
303 return result;
307 return result;
310 static bool
311 reg_dead_after_insn (const_rtx reg, const_rtx insn)
313 return find_regno_note (insn, REG_DEAD, REGNO (reg)) != NULL_RTX;
316 static bool
317 reg_unused_after_insn (const_rtx reg, const_rtx insn)
319 return find_regno_note (insn, REG_UNUSED, REGNO (reg)) != NULL_RTX;
322 // Check whether the two specified basic blocks are adjacent, i.e. there's no
323 // other basic block in between them.
324 static bool
325 is_adjacent_bb (basic_block a, basic_block b)
327 basic_block bb0[] = { a, b };
328 basic_block bb1[] = { b, a };
330 for (int i = 0; i < 2; ++i)
331 for (edge_iterator ei = ei_start (bb0[i]->succs);
332 !ei_end_p (ei); ei_next (&ei))
333 if (ei_edge (ei)->dest == bb1[i])
334 return true;
336 return false;
339 // Internal function of trace_reg_uses.
340 static void
341 trace_reg_uses_1 (rtx reg, rtx start_insn, basic_block bb, int& count,
342 std::vector<basic_block>& visited_bb, rtx abort_at_insn)
344 if (bb == NULL)
345 return;
347 if (std::find (visited_bb.begin (), visited_bb.end (), bb)
348 != visited_bb.end ())
349 log_return_void ("[bb %d] already visited\n", bb->index);
351 visited_bb.push_back (bb);
353 if (BB_END (bb) == NULL_RTX)
354 log_return_void ("[bb %d] BB_END is null\n", bb->index);
356 if (start_insn == NULL_RTX)
357 log_return_void ("[bb %d] start_insn is null\n", bb->index);
359 rtx end_insn = NEXT_INSN (BB_END (bb));
360 if (end_insn == NULL_RTX)
361 log_return_void ("[bb %d] end_insn is null\n", bb->index);
363 for (rtx i = NEXT_INSN (start_insn); i != end_insn; i = NEXT_INSN (i))
365 if (INSN_P (i))
367 if (NONDEBUG_INSN_P (i)
368 && (reg_overlap_mentioned_p (reg, PATTERN (i))
369 || (CALL_P (i) && find_reg_fusage (i, USE, reg))))
371 log_msg ("found use in [bb %d] at insn:\n", bb->index);
372 log_insn (i);
373 log_msg ("\n");
374 count += 1;
377 // Stop following this BB if the reg is set or dies along the way.
378 if (reg_set_p (reg, i) || reg_dead_after_insn (reg, i))
379 return;
382 if (abort_at_insn != NULL_RTX && abort_at_insn == i)
383 return;
386 for (edge_iterator ei = ei_start (bb->succs); !ei_end_p (ei); ei_next (&ei))
388 basic_block succ_bb = ei_edge (ei)->dest;
389 trace_reg_uses_1 (reg, BB_HEAD (succ_bb), succ_bb, count, visited_bb,
390 abort_at_insn);
394 // Trace uses of the specified reg in all basic blocks that are reachable from
395 // the specified insn. If 'abort_at_insn' is not null, abort the trace at
396 // that insn. If the insn 'abort_at_insn' uses the specified reg, it is also
397 // counted.
398 static int
399 trace_reg_uses (rtx reg, rtx start_insn, rtx abort_at_insn)
401 log_msg ("\ntrace_reg_uses\nreg = ");
402 log_rtx (reg);
403 log_msg ("\nstart_insn = ");
404 log_insn (start_insn);
406 int count = 0;
407 std::vector<basic_block> visited_bb;
408 visited_bb.reserve (32);
410 trace_reg_uses_1 (reg, start_insn, BLOCK_FOR_INSN (start_insn),
411 count, visited_bb, abort_at_insn);
412 return count;
415 // FIXME: Remove dependency on SH predicate function somehow.
416 extern int t_reg_operand (rtx, machine_mode);
417 extern int negt_reg_operand (rtx, machine_mode);
419 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
420 // RTL pass class
422 class sh_treg_combine : public rtl_opt_pass
424 public:
425 sh_treg_combine (gcc::context* ctx, bool split_insns, const char* name);
426 virtual ~sh_treg_combine (void);
427 virtual bool gate (function *);
428 virtual unsigned int execute (function *);
430 private:
431 // Type of ccreg store that is supported.
432 enum cstore_type_t
434 cstore_normal = 0,
435 cstore_inverted = 1,
436 cstore_unknown = -1
439 // Type of branch condition that is supported.
440 enum branch_condition_type_t
442 branch_if_true = 1,
443 branch_if_false = 0,
444 unknown_branch_condition = -1
447 // For each basic block there can be a trace entry which consists of an
448 // insn that sets the ccreg (usually a comparison) and a ccreg store.
449 struct bb_entry
451 basic_block bb;
452 set_of_reg setcc;
453 set_of_reg cstore;
454 cstore_type_t cstore_type;
455 std::vector<set_of_reg> cstore_reg_reg_copies;
457 bb_entry (basic_block b)
458 : bb (b), setcc (), cstore (), cstore_type (cstore_unknown) { }
460 rtx comparison_rtx (void) const { return setcc.set_src (); }
463 // A ccreg trace for a conditional branch.
464 struct cbranch_trace
466 rtx cbranch_insn;
467 branch_condition_type_t cbranch_type;
469 // The comparison against zero right before the conditional branch.
470 set_of_reg setcc;
472 // All BBs that are related to the cbranch. The last BB in the list is
473 // the BB of the cbranch itself and might be empty.
474 std::list<bb_entry> bb_entries;
476 cbranch_trace (rtx insn)
477 : cbranch_insn (insn),
478 cbranch_type (unknown_branch_condition),
479 setcc ()
483 basic_block bb (void) const { return BLOCK_FOR_INSN (cbranch_insn); }
486 branch_condition_rtx (void) const
488 rtx x = pc_set (cbranch_insn);
489 return x == NULL_RTX ? NULL_RTX : XEXP (XEXP (x, 1), 0);
492 bool
493 can_invert_condition (void) const
495 // The branch condition can be inverted safely only if the condition
496 // reg is dead after the cbranch.
497 return reg_dead_after_insn (XEXP (branch_condition_rtx (), 0),
498 cbranch_insn);
502 static const pass_data default_pass_data;
504 // Tells whether modified or newly added insns are to be split at the end
505 // of the pass.
506 const bool m_split_insns;
508 // rtx of the ccreg that is obtained from the target.
509 rtx m_ccreg;
511 // Newly added or modified insns.
512 std::vector<rtx> m_touched_insns;
514 // Given an rtx determine whether it's a comparison with a constant zero.
515 static bool is_cmp_eq_zero (const_rtx i);
517 // Update the stored mode of the ccreg from the given branch condition rtx.
518 void update_ccreg_mode (const_rtx cond);
520 // Given an rtx, figure out the branch condition, assuming that it is
521 // in canonical form:
522 // (ne (reg) (const_int 0))
523 // (eq (reg) (const_int 0))
524 branch_condition_type_t branch_condition_type (const_rtx cond) const;
526 // Return true if the specified rtx is either a normal ccreg or
527 // a negated form of the ccreg.
528 bool is_normal_ccreg (const_rtx x) const;
529 bool is_inverted_ccreg (const_rtx x) const;
531 // Given a reg rtx and a start insn rtx, try to find the insn in the same
532 // basic block that sets the specified reg.
533 // Return how the search ended and the insn where it stopped or NULL_RTX.
534 enum record_return_t
536 set_found,
537 set_not_found,
538 other_set_found
540 record_return_t record_set_of_reg (rtx reg, rtx start_insn, bb_entry& e);
542 // Tells whether the cbranch insn of the specified bb_entry can be removed
543 // safely without triggering any side effects.
544 bool can_remove_cstore (const bb_entry& e,
545 const cbranch_trace& trace) const;
547 // Tells whether the setcc insn of the specified bb_entry can be removed
548 // safely without triggering any side effects.
549 bool can_remove_comparison (const bb_entry& e,
550 const cbranch_trace& trace) const;
552 // Tells whether the two specified comparison rtx can be combined into a
553 // single comparison.
554 bool can_combine_comparisons (const_rtx x, const_rtx y) const;
556 // Tells whether the ccreg usage can be extended from the bb_entry on until
557 // the final cbranch of the trace.
558 bool can_extend_ccreg_usage (const bb_entry& e,
559 const cbranch_trace& trace) const;
561 // Create an insn rtx that is a negating reg move (not operation).
562 rtx make_not_reg_insn (rtx dst_reg, rtx src_reg) const;
564 // Create an insn rtx that inverts the ccreg.
565 rtx make_inv_ccreg_insn (void) const;
567 // Adds the specified insn to the set of modified or newly added insns that
568 // might need splitting at the end of the pass.
569 rtx touched_insn (rtx i);
571 // Try to invert the branch condition of the specified trace.
572 bool try_invert_branch_condition (cbranch_trace& trace);
574 // Try to optimize a cbranch trace by combining comparisons in BBs and
575 // eliminate the cstores.
576 bool try_combine_comparisons (cbranch_trace& trace,
577 int cstore_count, int inv_cstore_count,
578 cstore_type_t dominating_cstore);
580 // Try to optimize a cbranch trace by eliminating the cstores in BBs only.
581 bool try_eliminate_cstores (cbranch_trace& trace,
582 int cstore_count, int inv_cstore_count,
583 cstore_type_t dominating_cstore);
585 // Given a branch insn, try to optimize its branch condition.
586 // If any insns are modified or added they are added to 'm_touched_insns'.
587 void try_optimize_cbranch (rtx i);
591 const pass_data sh_treg_combine::default_pass_data =
593 RTL_PASS, // type
594 "", // name (overwritten by the constructor)
595 OPTGROUP_NONE, // optinfo_flags
596 true, // has_execute
597 TV_OPTIMIZE, // tv_id
598 0, // properties_required
599 0, // properties_provided
600 0, // properties_destroyed
601 0, // todo_flags_start
602 TODO_df_finish | TODO_df_verify // todo_flags_finish
603 | TODO_verify_rtl_sharing
606 sh_treg_combine::sh_treg_combine (gcc::context* ctx, bool split_insns,
607 const char* name)
608 : rtl_opt_pass (default_pass_data, ctx),
609 m_split_insns (split_insns),
610 m_ccreg (NULL_RTX)
612 // Overwrite default name in pass_data base class.
613 this->name = name;
616 sh_treg_combine::~sh_treg_combine (void)
620 void sh_treg_combine::update_ccreg_mode (const_rtx cond)
622 if (REG_P (XEXP (cond, 0)) && REGNO (XEXP (cond, 0)) != REGNO (m_ccreg))
623 return;
625 machine_mode m = GET_MODE (XEXP (cond, 0));
626 if (m == GET_MODE (m_ccreg))
627 return;
629 PUT_MODE (m_ccreg, m);
630 log_msg ("updated ccreg mode: ");
631 log_rtx (m_ccreg);
632 log_msg ("\n");
635 bool
636 sh_treg_combine::is_cmp_eq_zero (const_rtx i)
638 return i != NULL_RTX && GET_CODE (i) == EQ
639 && REG_P (XEXP (i, 0)) && XEXP (i, 1) == const0_rtx;
642 sh_treg_combine::branch_condition_type_t
643 sh_treg_combine::branch_condition_type (const_rtx cond) const
645 if (cond == NULL_RTX)
646 return unknown_branch_condition;
648 if (GET_CODE (cond) == NE
649 && REG_P (XEXP (cond, 0)) && REGNO (XEXP (cond, 0)) == REGNO (m_ccreg)
650 && XEXP (cond, 1) == const0_rtx)
651 return branch_if_true;
653 else if (GET_CODE (cond) == EQ
654 && REG_P (XEXP (cond, 0)) && REGNO (XEXP (cond, 0)) == REGNO (m_ccreg)
655 && XEXP (cond, 1) == const0_rtx)
656 return branch_if_false;
658 else
659 return unknown_branch_condition;
662 bool
663 sh_treg_combine::is_normal_ccreg (const_rtx x) const
665 return t_reg_operand (const_cast<rtx> (x), VOIDmode);
668 bool
669 sh_treg_combine::is_inverted_ccreg (const_rtx x) const
671 return negt_reg_operand (const_cast<rtx> (x), VOIDmode);
674 sh_treg_combine::record_return_t
675 sh_treg_combine::record_set_of_reg (rtx reg, rtx start_insn,
676 bb_entry& new_entry)
678 log_msg ("\n[bb %d]\n", new_entry.bb->index);
680 if (start_insn == NULL_RTX)
681 log_return (set_not_found, "set of reg not found. empty BB?\n");
683 new_entry.cstore_type = cstore_unknown;
685 for (rtx i = start_insn; i != NULL_RTX; )
687 new_entry.cstore = find_set_of_reg_bb (reg, i);
689 if (new_entry.cstore.set_src () == NULL_RTX)
690 log_return (set_not_found, "set of reg not found (cstore)\n");
692 log_insn (new_entry.cstore.insn);
693 log_msg ("\n");
695 if (is_normal_ccreg (new_entry.cstore.set_src ()))
697 log_msg ("normal condition store\n");
698 new_entry.cstore_type = cstore_normal;
700 else if (is_inverted_ccreg (new_entry.cstore.set_src ()))
702 log_msg ("inverted condition store\n");
703 new_entry.cstore_type = cstore_inverted;
705 else if (REG_P (new_entry.cstore.set_src ()))
707 // If it's a reg-reg copy follow the copied reg.
708 new_entry.cstore_reg_reg_copies.push_back (new_entry.cstore);
709 reg = new_entry.cstore.set_src ();
710 i = new_entry.cstore.insn;
712 log_msg ("reg-reg copy. tracing ");
713 log_rtx (reg);
714 log_msg ("\n");
715 continue;
717 else
718 log_return (other_set_found, "not a condition store\n");
720 gcc_assert (new_entry.cstore_type != cstore_unknown);
722 // Now see how the ccreg was set.
723 // For now it must be in the same BB.
724 log_msg ("tracing ccreg\n");
725 new_entry.setcc =
726 find_set_of_reg_bb (m_ccreg,
727 prev_nonnote_insn_bb (new_entry.cstore.insn));
729 // If cstore was found but setcc was not found continue anyway, as
730 // for some of the optimization types the setcc is irrelevant.
731 if (new_entry.setcc.set_src () == NULL_RTX)
732 log_return (set_found, "set of ccreg not found\n");
734 else if (GET_CODE (new_entry.setcc.set_rtx) == SET)
736 // Also allow insns that set the ccreg, but are not true comparison
737 // insns, as long as they are sets and not e.g. clobbers.
738 log_insn (new_entry.setcc.insn);
739 log_msg ("\n");
740 return set_found;
742 else
743 // If cstore was found but setcc was not found continue anyway, as
744 // for some of the optimization types the setcc is irrelevant.
745 log_return (set_found, "unknown set of ccreg\n");
748 log_return (set_not_found, "set of reg not found\n");
751 bool
752 sh_treg_combine::can_remove_cstore (const bb_entry& e,
753 const cbranch_trace& trace) const
755 if (volatile_insn_p (PATTERN (e.cstore.insn)))
757 log_msg ("can't remove insn\n");
758 log_insn (e.cstore.insn);
759 log_return (false, "\nbecause it's volatile\n");
762 // On SH there are parallel patterns which store the ccreg multiple times.
763 // In this case it's not safe.
764 rtx cstore_pat = PATTERN (e.cstore.insn);
765 if (GET_CODE (cstore_pat) == PARALLEL)
766 for (int i = 0; i < XVECLEN (cstore_pat, 0); ++i)
768 rtx x = XVECEXP (cstore_pat, 0, i);
770 // It's the cstore set that we're referring to, ignore that one.
771 if (x != e.cstore.set_rtx
772 && GET_CODE (x) == SET && reg_referenced_p (m_ccreg, x))
774 log_msg ("can't remove insn\n");
775 log_insn (e.cstore.insn);
776 log_return (false, "\nbecause it's a multiple ccreg store\n");
780 // If the cstore sets the ccreg (e.g. negc) and the ccreg is used afterwards
781 // it's not safe.
782 if (modified_in_p (m_ccreg, e.cstore.insn)
783 && !(reg_dead_after_insn (m_ccreg, e.cstore.insn)
784 || reg_unused_after_insn (m_ccreg, e.cstore.insn)))
786 log_msg ("can't remove insn\n");
787 log_insn (e.cstore.insn);
788 log_return (false, "\nbecause it sets the ccreg\n");
791 // If the cstore destination reg is copied around check the reg-reg
792 // copies. At every reg-reg copy the copied reg must be dead and there
793 // must not be a usage of the copied regs between the reg-reg copies.
794 // Otherwise we assume that the result of the cstore is used in some
795 // other way.
796 rtx prev_insn = e.cstore.insn;
797 for (std::vector<set_of_reg>::const_reverse_iterator i =
798 e.cstore_reg_reg_copies.rbegin ();
799 i != e.cstore_reg_reg_copies.rend (); ++i)
801 if (!reg_dead_after_insn (i->set_src (), i->insn))
803 log_msg ("can't remove insn\n");
804 log_insn (i->insn);
805 log_return (false, "\nbecause source of reg-reg copy doesn't die\n");
808 if (reg_used_between_p (i->set_src (), prev_insn, i->insn))
810 log_msg ("can't remove insn\n");
811 log_insn (i->insn);
812 log_return (false, "\nbecause reg %d is otherwise used\n",
813 REGNO (i->set_src ()));
816 prev_insn = i->insn;
819 // The cstore_dst reg must die after the test before the cbranch, otherwise
820 // it's not safe to remove the cstore.
821 // If the cstore destination reg is copied around check the effective
822 // destination reg of the cstore. The reg-reg copies are recorded in
823 // reverse order, i.e. the most recent reg-reg copy in the insn list
824 // comes first.
825 rtx cstore_dst = e.cstore_reg_reg_copies.empty ()
826 ? e.cstore.set_dst ()
827 : e.cstore_reg_reg_copies.front ().set_dst ();
829 if (!reg_dead_after_insn (cstore_dst, trace.setcc.insn))
831 log_msg ("can't remove insn\n");
832 log_insn (e.cstore.insn);
833 log_return (false, "\nbecause its effective target reg %d doesn't die "
834 "after trace.setcc.insn\n", REGNO (cstore_dst));
837 // Also check that the cstore_dst reg is not used in other reachable code
838 // paths before it dies.
839 // Count the uses of the effective cstore_dst reg (i.e. the last known reg
840 // that holds the cstore value after reg-reg copies) in all BBs that can be
841 // reached from bb_entry's BB including the BB of the cstore insn.
842 // If we get more than 1 uses we assume that it's used somewhere else and is
843 // not safe to be removed.
844 int cstore_dst_use_count = trace_reg_uses (cstore_dst, e.cstore.insn,
845 trace.setcc.insn);
846 if (cstore_dst_use_count > 1)
848 log_msg ("can't remove insn\n");
849 log_insn (e.cstore.insn);
850 log_return (false, "\nbecause its effective target reg %d is used "
851 "in %d other places\n", REGNO (cstore_dst),
852 cstore_dst_use_count - 1);
855 return true;
858 bool
859 sh_treg_combine::can_remove_comparison (const bb_entry& e,
860 const cbranch_trace&/* trace*/) const
862 // If the ccreg is used otherwise between the comparison and the cstore,
863 // it's not safe.
864 if (reg_used_between_p (m_ccreg, e.setcc.insn, e.cstore.insn))
866 log_msg ("can't remove insn\n");
867 log_insn (e.setcc.insn);
868 log_return (false, "\nbecause the ccreg is used otherwise\n");
871 if (!reg_dead_after_insn (m_ccreg, e.cstore.insn)
872 && !reg_unused_after_insn (m_ccreg, e.cstore.insn))
874 log_msg ("can't remove insn\n");
875 log_insn (e.cstore.insn);
876 log_return (false, "\nbecause ccreg is not dead or unused afterwards\n");
879 // On SH there are also multiple set patterns that can be used for
880 // comparisons, such as "shll". It's not safe to remove those.
881 if (multiple_sets (e.setcc.insn))
883 log_msg ("can't remove insn\n");
884 log_insn (e.cstore.insn);
885 log_return (false, "\nbecause it's a multiple set\n");
888 return true;
892 sh_treg_combine::make_not_reg_insn (rtx dst_reg, rtx src_reg) const
894 // This will to go through expanders and may output multiple insns
895 // for multi-word regs.
896 start_sequence ();
897 expand_simple_unop (GET_MODE (dst_reg), NOT, src_reg, dst_reg, 0);
898 rtx i = get_insns ();
899 end_sequence ();
900 return i;
904 sh_treg_combine::make_inv_ccreg_insn (void) const
906 start_sequence ();
907 rtx i = emit_insn (gen_rtx_SET (VOIDmode, m_ccreg,
908 gen_rtx_fmt_ee (XOR, GET_MODE (m_ccreg),
909 m_ccreg, const1_rtx)));
910 end_sequence ();
911 return i;
915 sh_treg_combine::touched_insn (rtx i)
917 m_touched_insns.push_back (i);
918 return i;
921 bool
922 sh_treg_combine::can_combine_comparisons (const_rtx x, const_rtx y) const
924 if (GET_CODE (x) != GET_CODE (y))
925 return false;
927 rtx x_op0 = XEXP (x, 0);
928 rtx x_op1 = XEXP (x, 1);
930 rtx y_op0 = XEXP (y, 0);
931 rtx y_op1 = XEXP (y, 1);
933 if (!REG_P (x_op0) || !REG_P (y_op0))
934 return false;
936 if (GET_MODE (x_op0) != GET_MODE (y_op0))
937 return false;
939 // rtx_equal_p also compares the reg numbers which we do not care about
940 // here, as long as both are regs and the modes are the same.
941 if (REG_P (x_op1))
942 return REG_P (y_op1) && GET_MODE (x_op1) == GET_MODE (y_op1);
944 return rtx_equal_p (x_op1, y_op1);
947 bool
948 sh_treg_combine::can_extend_ccreg_usage (const bb_entry& e,
949 const cbranch_trace& trace) const
951 // Check if the ccreg is not modified by other insins in the BB path until
952 // the final cbranch of the trace.
953 // Start checking after the cstore that follows the setcc, assuming that
954 // the cstore will be removed.
956 // The assumption here is that the specified bb_entry's BB is a direct
957 // predecessor of the trace.cbranch_insn's BB.
958 if (e.bb != trace.bb () && !is_adjacent_bb (e.bb, trace.bb ()))
959 log_return (false,
960 "can't extend ccreg usage -- [bb %d] and [bb %d] are not adjacent\n",
961 e.bb->index, trace.bb ()->index);
963 if (e.cstore.empty ())
964 log_return (false, "can't extend ccreg usage -- no cstore\n");
966 // The entry's cstore is in the same BB as the final cbranch.
967 if (e.bb == trace.bb ())
969 if (reg_set_between_p (m_ccreg, e.cstore.insn, trace.setcc.insn))
970 log_return (false,
971 "can't extend ccreg usage -- it's modified between e.cstore.insn "
972 "and trace.setcc.insn");
973 else
974 return true;
977 // The entry's cstore and the final cbranch are in different BBs.
978 if (reg_set_between_p (m_ccreg, e.cstore.insn, NEXT_INSN (BB_END (e.bb))))
979 log_return (false,
980 "can't extend ccreg usage -- it's modified in [bb %d]", e.bb->index);
982 if (reg_set_between_p (m_ccreg, PREV_INSN (BB_HEAD (trace.bb ())),
983 trace.setcc.insn))
984 log_return (false,
985 "can't extend ccreg usage -- it's modified in [bb %d]",
986 trace.bb ()->index);
988 return true;
991 bool
992 sh_treg_combine::try_invert_branch_condition (cbranch_trace& trace)
994 log_msg ("inverting branch condition\n");
996 if (!invert_jump_1 (trace.cbranch_insn, JUMP_LABEL (trace.cbranch_insn)))
997 log_return (false, "invert_jump_1 failed\n");
999 if (verify_changes (num_validated_changes ()))
1000 confirm_change_group ();
1001 else
1002 log_return (false, "verify_changed failed\n");
1004 touched_insn (trace.cbranch_insn);
1005 return true;
1008 bool
1009 sh_treg_combine::try_combine_comparisons (cbranch_trace& trace,
1010 int cstore_count,
1011 int inv_cstore_count,
1012 cstore_type_t dominating_cstore)
1014 log_msg ("\ntry_combine_comparisons\n");
1016 // This function will always try to create new pseudos.
1017 if (!can_create_pseudo_p ())
1018 log_return (false, "can't create pseudos\n");
1020 // Check that all ccset insns are comparisons and all comparison types in
1021 // all BBs are the same and could be combined into one single comparison.
1022 rtx comp = NULL_RTX;
1023 rtx comp_insn = NULL_RTX;
1025 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1026 i != trace.bb_entries.end (); ++i)
1028 int i_empty_count = i->setcc.empty () + i->cstore.empty ();
1030 // A completly empty entry is OK (could be the BB of the cbranch).
1031 if (i_empty_count == 2)
1032 continue;
1034 // Otherwise we need both, the setcc and the cstore.
1035 if (i_empty_count != 0)
1036 log_return (false, "bb entry is not a setcc cstore pair\n");
1038 rtx other_comp = i->comparison_rtx ();
1040 if (!COMPARISON_P (other_comp))
1042 log_msg ("setcc is not a comparison:\n");
1043 log_rtx (other_comp);
1044 log_return (false, "\n");
1047 if (comp_insn == NULL_RTX)
1049 comp = other_comp;
1050 comp_insn = i->setcc.insn;
1052 else if (!can_combine_comparisons (comp, other_comp))
1053 return false;
1055 // The goal here is to eliminate all cstores and comparisons in the BBs.
1056 // Thus check if every cstore can actually be removed safely.
1057 if (!can_remove_cstore (*i, trace) || !can_remove_comparison (*i, trace))
1058 return false;
1061 // FIXME: The first operand of the comparison must be a simple reg.
1062 // This effectively prohibits combining div0s comparisons such as
1063 // (lt:SI (xor:SI (reg:SI) (reg:SI)))
1064 if (!REG_P (XEXP (comp, 0)))
1066 log_msg ("comparison operand 0\n");
1067 log_rtx (XEXP (comp, 0));
1068 log_return (false, "\nis not a reg\n");
1071 rtx comp_op0 = gen_reg_rtx (GET_MODE (XEXP (comp, 0)));
1072 rtx comp_op1 = REG_P (XEXP (comp, 1))
1073 ? gen_reg_rtx (GET_MODE (XEXP (comp, 1)))
1074 : XEXP (comp, 1);
1076 // If there are both, inverting and non-inverting cstores, they can only
1077 // be eliminated if the comparison can be inverted. We assume that the
1078 // comparison insns that we find are already minimal and canonicalized.
1079 // There is one special case though, where an integer comparison
1080 // (eq (reg) (const_int 0))
1081 // can be inverted with a sequence
1082 // (eq (not (reg)) (const_int 0))
1083 if (inv_cstore_count != 0 && cstore_count != 0)
1085 if (make_not_reg_insn (comp_op0, comp_op0) == NULL_RTX)
1086 log_return (false, "make_not_reg_insn failed.\n");
1088 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1089 i != trace.bb_entries.end (); ++i)
1091 if (i->setcc.empty () || i->cstore.empty ())
1092 continue;
1094 if (i->cstore_type != dominating_cstore
1095 && !is_cmp_eq_zero (i->comparison_rtx ()))
1097 log_msg ("can't invert comparison in insn\n");
1098 log_insn (i->setcc.insn);
1099 log_return (false,
1100 "\nbecause it's not a (eq (reg) (const_int 0))\n");
1105 if (dominating_cstore == cstore_normal
1106 && !try_invert_branch_condition (trace))
1107 return false;
1109 // Replace the test insn before the cbranch with the common comparison.
1110 // Instead of creating a new insn from scratch we copy the common comparison
1111 // pattern. This simplifies handling parallel comparison patterns, such as
1112 // FP comparisons on SH, which have an extra use on FPSCR.
1113 log_msg ("installing common comparison in [bb %d]\n", trace.bb ()->index);
1115 rtx common_comp_pat = copy_rtx (PATTERN (comp_insn));
1116 rtx common_comp = const_cast<rtx> (set_of (m_ccreg, common_comp_pat));
1118 gcc_assert (common_comp != NULL_RTX);
1120 XEXP (XEXP (common_comp, 1), 0) = comp_op0;
1121 XEXP (XEXP (common_comp, 1), 1) = comp_op1;
1123 log_rtx (common_comp_pat);
1124 log_msg ("\n");
1126 rtx common_comp_insn = touched_insn (emit_insn_after (common_comp_pat,
1127 trace.setcc.insn));
1129 if (REG_P (comp_op0))
1130 add_reg_note (common_comp_insn, REG_DEAD, copy_rtx (comp_op0));
1131 if (REG_P (comp_op1))
1132 add_reg_note (common_comp_insn, REG_DEAD, copy_rtx (comp_op1));
1134 delete_insn (trace.setcc.insn);
1136 // Replace comparison and cstore insns with reg-reg moves in all BBs.
1137 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1138 i != trace.bb_entries.end (); ++i)
1140 if (i->setcc.empty () || i->cstore.empty ())
1141 continue;
1143 rtx i_comp_op0 = XEXP (i->comparison_rtx (), 0);
1144 rtx i_comp_op1 = XEXP (i->comparison_rtx (), 1);
1146 if (i->cstore_type == dominating_cstore)
1148 log_msg ("replacing comparison and cstore with reg move "
1149 "in [bb %d]\n", i->bb->index);
1151 rtx new_i = touched_insn (
1152 emit_insn_after (gen_move_insn (comp_op0, i_comp_op0),
1153 i->setcc.insn));
1155 if (REG_P (i_comp_op0)
1156 && reg_dead_after_insn (i_comp_op0, i->setcc.insn))
1157 add_reg_note (new_i, REG_DEAD, copy_rtx (i_comp_op0));
1159 // If the second operand is a reg, have to emit a move insn.
1160 // Otherwise assume it's a const_int and just reference it.
1161 if (REG_P (comp_op1))
1163 new_i = touched_insn (
1164 emit_insn_after (gen_move_insn (comp_op1, i_comp_op1),
1165 i->setcc.insn));
1167 if (reg_dead_after_insn (i_comp_op1, i->setcc.insn))
1168 add_reg_note (new_i, REG_DEAD, copy_rtx (i_comp_op1));
1171 else
1173 log_msg ("replacing comparison and cstore with inverting reg move "
1174 "in [bb %d]\n", i->bb->index);
1176 rtx new_i = make_not_reg_insn (comp_op0, i_comp_op0);
1177 if (REG_P (i_comp_op0)
1178 && reg_dead_after_insn (i_comp_op0, i->setcc.insn))
1179 add_reg_note (new_i, REG_DEAD, copy_rtx (i_comp_op0));
1181 touched_insn (emit_insn_after (new_i, i->setcc.insn));
1184 delete_insn (i->cstore.insn);
1185 delete_insn (i->setcc.insn);
1188 return true;
1191 bool
1192 sh_treg_combine::try_eliminate_cstores (cbranch_trace& trace,
1193 int cstore_count, int inv_cstore_count,
1194 cstore_type_t dominating_cstore)
1196 log_msg ("\ntry_eliminate_cstores\n");
1198 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1199 i != trace.bb_entries.end (); ++i)
1201 // A completly empty entry is OK (could be the BB of the cbranch).
1202 if (i->setcc.empty () && i->cstore.empty ())
1203 continue;
1205 // We're going to eliminate cstores, but for that they have to be
1206 // there. We don't care about the setcc in this case.
1207 if (i->cstore.empty ())
1208 log_return (false, "bb entry cstore empty -- aborting\n");
1210 // The goal here is to eliminate all cstores in the BBs and extend the
1211 // ccreg usage.
1212 if (!can_extend_ccreg_usage (*i, trace))
1213 return false;
1215 // If the cstore can't be removed we can keep it around as long as
1216 // it doesn't modify the ccreg.
1217 if (!can_remove_cstore (*i, trace)
1218 && modified_in_p (m_ccreg, i->cstore.insn))
1219 log_return (false, "cstore sets ccreg -- aborting\n");
1222 // If there are both, inverting and non-inverting cstores, we'll have to
1223 // invert the ccreg as a replacement for one of them.
1224 if (cstore_count != 0 && inv_cstore_count != 0)
1226 rtx i = make_inv_ccreg_insn ();
1227 if (recog_memoized (i) < 0)
1229 log_msg ("failed to match ccreg inversion insn:\n");
1230 log_rtx (PATTERN (i));
1231 log_return (false, "\naborting\n");
1235 if (dominating_cstore == cstore_normal
1236 && !try_invert_branch_condition (trace))
1237 return false;
1239 // Eliminate cstores in all BBs.
1240 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1241 i != trace.bb_entries.end (); ++i)
1243 if (i->cstore.empty ())
1244 continue;
1246 if (i->cstore_type == dominating_cstore)
1247 log_msg ("removing cstore in [bb %d]\n", i->bb->index);
1248 else
1250 log_msg ("replacing cstore with ccreg inversion in [bb %d]\n",
1251 i->bb->index);
1253 touched_insn (
1254 emit_insn_after (make_inv_ccreg_insn (), i->cstore.insn));
1257 if (can_remove_cstore (*i, trace))
1258 delete_insn (i->cstore.insn);
1261 log_msg ("removing test insn before cbranch\n");
1262 delete_insn (trace.setcc.insn);
1263 return true;
1266 void
1267 sh_treg_combine::try_optimize_cbranch (rtx insn)
1269 cbranch_trace trace (insn);
1271 log_msg ("\n\n--------------------------------------\n");
1272 log_msg ("found cbranch insn in [bb %d]:\n", trace.bb ()->index);
1273 log_insn (insn);
1275 trace.cbranch_type = branch_condition_type (trace.branch_condition_rtx ());
1277 if (trace.cbranch_type == branch_if_true)
1278 log_msg ("condition: branch if true\n");
1279 else if (trace.cbranch_type == branch_if_false)
1280 log_msg ("condition: branch if false\n");
1281 else
1283 log_msg ("unknown branch condition\n");
1284 log_rtx (trace.branch_condition_rtx ());
1285 log_return_void ("\n");
1288 update_ccreg_mode (trace.branch_condition_rtx ());
1290 // Scan the insns backwards for an insn that sets the ccreg by testing a
1291 // reg against zero like
1292 // (set (reg ccreg) (eq (reg) (const_int 0)))
1293 // The testing insn could also be outside of the current basic block, but
1294 // for now we limit the search to the current basic block.
1295 trace.setcc = find_set_of_reg_bb (m_ccreg, prev_nonnote_insn_bb (insn));
1297 if (!is_cmp_eq_zero (trace.setcc.set_src ()))
1298 log_return_void ("could not find set of ccreg in current BB\n");
1300 rtx trace_reg = XEXP (trace.setcc.set_src (), 0);
1302 log_msg ("set of ccreg:\n");
1303 log_insn (trace.setcc.insn);
1305 // See if we can remove the trace.setcc insn safely.
1306 if (reg_used_between_p (m_ccreg, trace.setcc.insn, trace.cbranch_insn))
1307 log_return_void ("ccreg used between testing insn and branch insn\n");
1309 if (volatile_insn_p (PATTERN (trace.setcc.insn)))
1311 log_msg ("can't remove insn\n");
1312 log_insn (trace.setcc.insn);
1313 log_return_void ("\nbecause it's volatile\n");
1316 // Now that we have an insn which tests some reg and sets the condition
1317 // reg before the conditional branch, try to figure out how that tested
1318 // reg was formed, i.e. find all the insns that set the tested reg in
1319 // some way.
1320 // The tested reg might be set in multiple basic blocks so we need to
1321 // check all basic blocks which can reach this current basic block.
1322 // If the set of reg is an inverting or non-inverting store of the condition
1323 // register, check how the ccreg value was obtained.
1324 log_msg ("\ntracing ");
1325 log_rtx (trace_reg);
1326 log_msg ("\n");
1329 // First check the basic block where the conditional branch is in.
1330 // If we find it here there's no point in checking other BBs.
1331 trace.bb_entries.push_front (bb_entry (trace.bb ()));
1333 record_return_t res =
1334 record_set_of_reg (trace_reg, prev_nonnote_insn_bb (trace.setcc.insn),
1335 trace.bb_entries.front ());
1337 if (res == other_set_found)
1338 log_return_void ("other set found - aborting trace\n");
1339 else if (res == set_not_found)
1341 // It seems the initial search in the BB of the conditional branch
1342 // didn't find anything. Now look in all predecessor BBs.
1343 for (edge_iterator ei = ei_start (trace.bb ()->preds);
1344 !ei_end_p (ei); ei_next (&ei))
1346 edge e = ei_edge (ei);
1347 trace.bb_entries.push_front (bb_entry (e->src));
1349 res = record_set_of_reg (trace_reg, BB_END (e->src),
1350 trace.bb_entries.front ());
1351 if (res != set_found)
1352 log_return_void ("set not found - aborting trace\n");
1356 if (dump_file != NULL)
1358 log_msg ("\ncbranch trace summary:\n");
1359 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1360 i != trace.bb_entries.end (); ++i)
1362 log_msg ("\n[bb %d]\n", i->bb->index);
1363 if (!i->setcc.empty ())
1365 log_rtx (i->setcc.set_rtx);
1366 log_msg ("\n");
1368 if (!i->cstore.empty ())
1370 log_rtx (i->cstore.set_rtx);
1371 log_msg ("\n");
1374 for (std::vector<set_of_reg>::const_reverse_iterator j =
1375 i->cstore_reg_reg_copies.rbegin ();
1376 j != i->cstore_reg_reg_copies.rend (); ++j)
1378 log_rtx (j->set_rtx);
1379 log_msg ("\n");
1383 log_rtx (trace.setcc.set_rtx);
1384 log_msg ("\n");
1385 log_rtx (PATTERN (trace.cbranch_insn));
1386 log_msg ("\n");
1389 // Check that we don't have any empty BBs.
1390 // Only the BB with the cbranch may be empty.
1391 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1392 i != trace.bb_entries.end (); ++i)
1393 if (i->setcc.empty () && i->cstore.empty () && i->bb != trace.bb ())
1394 log_return_void ("\n[bb %d] is empty - aborting.\n", i->bb->index);
1396 // Determine the dominating cstore type
1397 // FIXME: Try to take the probabilities of the BBs into account somehow.
1398 int cstore_count = 0;
1399 int inv_cstore_count = 0;
1401 for (std::list<bb_entry>::const_iterator i = trace.bb_entries.begin ();
1402 i != trace.bb_entries.end (); ++i)
1404 if (i->cstore_type == cstore_normal)
1405 cstore_count += 1;
1406 else if (i->cstore_type == cstore_inverted)
1407 inv_cstore_count += 1;
1410 log_msg ("cstore count = %d inverted cstore count = %d\n",
1411 cstore_count, inv_cstore_count);
1413 // This puts a priority on inverting cstores.
1414 cstore_type_t dominating_cstore = inv_cstore_count >= cstore_count
1415 ? cstore_inverted
1416 : cstore_normal;
1418 if (dominating_cstore == cstore_inverted)
1419 log_msg ("will try to eliminate inverted cstore\n");
1420 else if (dominating_cstore == cstore_normal)
1422 log_msg ("will try to eliminate normal cstore\n");
1423 if (!trace.can_invert_condition ())
1424 log_return_void ("branch condition can't be inverted - aborting\n");
1426 else
1427 gcc_unreachable ();
1429 if (try_combine_comparisons (trace, cstore_count, inv_cstore_count,
1430 dominating_cstore))
1431 return;
1433 try_eliminate_cstores (trace, cstore_count, inv_cstore_count,
1434 dominating_cstore);
1437 bool
1438 sh_treg_combine::gate (function *)
1440 return optimize > 0;
1443 unsigned int
1444 sh_treg_combine::execute (function *fun)
1446 unsigned int ccr0 = INVALID_REGNUM;
1447 unsigned int ccr1 = INVALID_REGNUM;
1449 if (targetm.fixed_condition_code_regs (&ccr0, &ccr1)
1450 && ccr0 != INVALID_REGNUM)
1452 // Initially create a reg rtx with VOIDmode.
1453 // When the first conditional branch is discovered, the mode is changed
1454 // to the mode that is actually used by the target.
1455 m_ccreg = gen_rtx_REG (VOIDmode, ccr0);
1458 if (m_ccreg == NULL_RTX)
1459 log_return (0, "no ccreg.\n\n");
1461 if (STORE_FLAG_VALUE != 1)
1462 log_return (0, "unsupported STORE_FLAG_VALUE %d", STORE_FLAG_VALUE);
1464 log_msg ("ccreg: ");
1465 log_rtx (m_ccreg);
1466 log_msg (" STORE_FLAG_VALUE = %d\n", STORE_FLAG_VALUE);
1468 // Look for basic blocks that end with a conditional branch and try to
1469 // optimize them.
1470 basic_block bb;
1471 FOR_EACH_BB_FN (bb, fun)
1473 rtx i = BB_END (bb);
1474 if (any_condjump_p (i) && onlyjump_p (i))
1475 try_optimize_cbranch (i);
1478 log_msg ("\n\n");
1480 // If new insns are created and this pass is executed after all insns
1481 // have been split already, we must split the insns we've changed or added
1482 // ourselves here.
1483 // FIXME: Multi-word operations (which emit multiple insns) are not handled
1484 // properly here, since only one insn will end up in 'm_touched_insns'.
1485 // On SH this is not a problem though.
1486 if (m_split_insns)
1487 for (std::vector<rtx>::const_iterator i = m_touched_insns.begin ();
1488 i != m_touched_insns.end (); ++i)
1490 log_msg ("trying to split insn:\n");
1491 log_insn (*i);
1492 log_msg ("\n");
1493 try_split (PATTERN (*i), *i, 0);
1496 m_touched_insns.clear ();
1497 log_return (0, "\n\n");
1500 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1501 // This allows instantiating the pass somewhere else without having to pull
1502 // in a header file.
1503 opt_pass*
1504 make_pass_sh_treg_combine (gcc::context* ctx, bool split_insns,
1505 const char* name)
1507 return new sh_treg_combine (ctx, split_insns, name);