OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [tree-ssa-loop-unswitch.c] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.