1/* Loop unswitching.
2   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING.  If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA.  */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
26#include "rtl.h"
27#include "tm_p.h"
28#include "hard-reg-set.h"
29#include "basic-block.h"
30#include "output.h"
31#include "diagnostic.h"
32#include "tree-flow.h"
33#include "tree-dump.h"
34#include "timevar.h"
35#include "cfgloop.h"
36#include "domwalk.h"
37#include "params.h"
38#include "tree-pass.h"
39
40/* This file implements the loop unswitching, i.e. transformation of loops like
41
42   while (A)
43     {
44       if (inv)
45         B;
46
47       X;
48
49       if (!inv)
50	 C;
51     }
52
53   where inv is the loop invariant, into
54
55   if (inv)
56     {
57       while (A)
58	 {
59           B;
60	   X;
61	 }
62     }
63   else
64     {
65       while (A)
66	 {
67	   X;
68	   C;
69	 }
70     }
71
72   Inv is considered invariant iff the values it compares are both invariant;
73   tree-ssa-loop-im.c ensures that all the suitable conditions are in this
74   shape.  */
75
76static struct loop *tree_unswitch_loop (struct loops *, struct loop *, basic_block,
77				   tree);
78static bool tree_unswitch_single_loop (struct loops *, struct loop *, int);
79static tree tree_may_unswitch_on (basic_block, struct loop *);
80
81/* Main entry point.  Perform loop unswitching on all suitable LOOPS.  */
82
83unsigned int
84tree_ssa_unswitch_loops (struct loops *loops)
85{
86  int i, num;
87  struct loop *loop;
88  bool changed = false;
89
90  /* Go through inner loops (only original ones).  */
91  num = loops->num;
92
93  for (i = 1; i < num; i++)
94    {
95      /* Removed loop?  */
96      loop = loops->parray[i];
97      if (!loop)
98	continue;
99
100      if (loop->inner)
101	continue;
102
103      changed |= tree_unswitch_single_loop (loops, loop, 0);
104    }
105
106  if (changed)
107    return TODO_cleanup_cfg;
108  return 0;
109}
110
111/* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
112   basic blocks (for what it means see comments below).  */
113
114static tree
115tree_may_unswitch_on (basic_block bb, struct loop *loop)
116{
117  tree stmt, def, cond, use;
118  basic_block def_bb;
119  ssa_op_iter iter;
120
121  /* BB must end in a simple conditional jump.  */
122  stmt = last_stmt (bb);
123  if (!stmt || TREE_CODE (stmt) != COND_EXPR)
124    return NULL_TREE;
125
126  /* Condition must be invariant.  */
127  FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
128    {
129      def = SSA_NAME_DEF_STMT (use);
130      def_bb = bb_for_stmt (def);
131      if (def_bb
132	  && flow_bb_inside_loop_p (loop, def_bb))
133	return NULL_TREE;
134    }
135
136  cond = COND_EXPR_COND (stmt);
137  /* To keep the things simple, we do not directly remove the conditions,
138     but just replace tests with 0/1.  Prevent the infinite loop where we
139     would unswitch again on such a condition.  */
140  if (integer_zerop (cond) || integer_nonzerop (cond))
141    return NULL_TREE;
142
143  return cond;
144}
145
146/* Simplifies COND using checks in front of the entry of the LOOP.  Just very
147   simplish (sufficient to prevent us from duplicating loop in unswitching
148   unnecessarily).  */
149
150static tree
151simplify_using_entry_checks (struct loop *loop, tree cond)
152{
153  edge e = loop_preheader_edge (loop);
154  tree stmt;
155
156  while (1)
157    {
158      stmt = last_stmt (e->src);
159      if (stmt
160	  && TREE_CODE (stmt) == COND_EXPR
161	  && operand_equal_p (COND_EXPR_COND (stmt), cond, 0))
162	return (e->flags & EDGE_TRUE_VALUE
163		? boolean_true_node
164		: boolean_false_node);
165
166      if (!single_pred_p (e->src))
167	return cond;
168
169      e = single_pred_edge (e->src);
170      if (e->src == ENTRY_BLOCK_PTR)
171	return cond;
172    }
173}
174
175/* Unswitch single LOOP.  NUM is number of unswitchings done; we do not allow
176   it to grow too much, it is too easy to create example on that the code would
177   grow exponentially.  */
178
179static bool
180tree_unswitch_single_loop (struct loops *loops, struct loop *loop, int num)
181{
182  basic_block *bbs;
183  struct loop *nloop;
184  unsigned i;
185  tree cond = NULL_TREE, stmt;
186  bool changed = false;
187
188  /* Do not unswitch too much.  */
189  if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
190    {
191      if (dump_file && (dump_flags & TDF_DETAILS))
192	fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
193      return false;
194    }
195
196  /* Only unswitch innermost loops.  */
197  if (loop->inner)
198    {
199      if (dump_file && (dump_flags & TDF_DETAILS))
200	fprintf (dump_file, ";; Not unswitching, not innermost loop\n");
201      return false;
202    }
203
204  /* The loop should not be too large, to limit code growth.  */
205  if (tree_num_loop_insns (loop)
206      > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
207    {
208      if (dump_file && (dump_flags & TDF_DETAILS))
209	fprintf (dump_file, ";; Not unswitching, loop too big\n");
210      return false;
211    }
212
213  i = 0;
214  bbs = get_loop_body (loop);
215
216  while (1)
217    {
218      /* Find a bb to unswitch on.  */
219      for (; i < loop->num_nodes; i++)
220	if ((cond = tree_may_unswitch_on (bbs[i], loop)))
221	  break;
222
223      if (i == loop->num_nodes)
224	{
225	  free (bbs);
226	  return changed;
227	}
228
229      cond = simplify_using_entry_checks (loop, cond);
230      stmt = last_stmt (bbs[i]);
231      if (integer_nonzerop (cond))
232	{
233	  /* Remove false path.  */
234	  COND_EXPR_COND (stmt) = boolean_true_node;
235	  changed = true;
236	}
237      else if (integer_zerop (cond))
238	{
239	  /* Remove true path.  */
240	  COND_EXPR_COND (stmt) = boolean_false_node;
241	  changed = true;
242	}
243      else
244	break;
245
246      update_stmt (stmt);
247      i++;
248    }
249
250  if (dump_file && (dump_flags & TDF_DETAILS))
251    fprintf (dump_file, ";; Unswitching loop\n");
252
253  initialize_original_copy_tables ();
254  /* Unswitch the loop on this condition.  */
255  nloop = tree_unswitch_loop (loops, loop, bbs[i], cond);
256  if (!nloop)
257    {
258      free_original_copy_tables ();
259      free (bbs);
260      return changed;
261    }
262
263  /* Update the SSA form after unswitching.  */
264  update_ssa (TODO_update_ssa);
265  free_original_copy_tables ();
266
267  /* Invoke itself on modified loops.  */
268  tree_unswitch_single_loop (loops, nloop, num + 1);
269  tree_unswitch_single_loop (loops, loop, num + 1);
270  free (bbs);
271  return true;
272}
273
274/* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON.  We only support
275   unswitching of innermost loops.  COND is the condition determining which
276   loop is entered -- the new loop is entered if COND is true.  Returns NULL
277   if impossible, new loop otherwise.  */
278
279static struct loop *
280tree_unswitch_loop (struct loops *loops, struct loop *loop,
281		    basic_block unswitch_on, tree cond)
282{
283  basic_block condition_bb;
284
285  /* Some sanity checking.  */
286  gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
287  gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
288  gcc_assert (loop->inner == NULL);
289
290  return loop_version (loops, loop, unshare_expr (cond),
291		       &condition_bb, false);
292}
293