annotate gcc/tree-ssa-threadupdate.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* Thread edges through blocks and update the control flow and SSA graphs.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 any later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 GNU General Public License for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "config.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "system.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "coretypes.h"
111
kono
parents: 67
diff changeset
23 #include "backend.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "tree.h"
111
kono
parents: 67
diff changeset
25 #include "gimple.h"
kono
parents: 67
diff changeset
26 #include "cfghooks.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 #include "tree-pass.h"
111
kono
parents: 67
diff changeset
28 #include "ssa.h"
kono
parents: 67
diff changeset
29 #include "fold-const.h"
kono
parents: 67
diff changeset
30 #include "cfganal.h"
kono
parents: 67
diff changeset
31 #include "gimple-iterator.h"
kono
parents: 67
diff changeset
32 #include "tree-ssa.h"
kono
parents: 67
diff changeset
33 #include "tree-ssa-threadupdate.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 #include "cfgloop.h"
111
kono
parents: 67
diff changeset
35 #include "dbgcnt.h"
kono
parents: 67
diff changeset
36 #include "tree-cfg.h"
kono
parents: 67
diff changeset
37 #include "tree-vectorizer.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 /* Given a block B, update the CFG and SSA graph to reflect redirecting
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 one or more in-edges to B to instead reach the destination of an
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 out-edge from B while preserving any side effects in B.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 i.e., given A->B and B->C, change A->B to be A->C yet still preserve the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 side effects of executing B.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 1. Make a copy of B (including its outgoing edges and statements). Call
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 the copy B'. Note B' has no incoming edges or PHIs at this time.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 2. Remove the control statement at the end of B' and all outgoing edges
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 except B'->C.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 3. Add a new argument to each PHI in C with the same value as the existing
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 argument associated with edge B->C. Associate the new PHI arguments
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 with the edge B'->C.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 4. For each PHI in B, find or create a PHI in B' with an identical
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 PHI_RESULT. Add an argument to the PHI in B' which has the same
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 value as the PHI in B associated with the edge A->B. Associate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 the new argument in the PHI in B' with the edge A->B.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 5. Change the edge A->B to A->B'.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 5a. This automatically deletes any PHI arguments associated with the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 edge A->B in B.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 5b. This automatically associates each new argument added in step 4
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 with the edge A->B'.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 6. Repeat for other incoming edges into B.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 7. Put the duplicated resources in B and all the B' blocks into SSA form.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 Note that block duplication can be minimized by first collecting the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 set of unique destination blocks that the incoming edges should
111
kono
parents: 67
diff changeset
75 be threaded to.
kono
parents: 67
diff changeset
76
kono
parents: 67
diff changeset
77 We reduce the number of edges and statements we create by not copying all
kono
parents: 67
diff changeset
78 the outgoing edges and the control statement in step #1. We instead create
kono
parents: 67
diff changeset
79 a template block without the outgoing edges and duplicate the template.
kono
parents: 67
diff changeset
80
kono
parents: 67
diff changeset
81 Another case this code handles is threading through a "joiner" block. In
kono
parents: 67
diff changeset
82 this case, we do not know the destination of the joiner block, but one
kono
parents: 67
diff changeset
83 of the outgoing edges from the joiner block leads to a threadable path. This
kono
parents: 67
diff changeset
84 case largely works as outlined above, except the duplicate of the joiner
kono
parents: 67
diff changeset
85 block still contains a full set of outgoing edges and its control statement.
kono
parents: 67
diff changeset
86 We just redirect one of its outgoing edges to our jump threading path. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 /* Steps #5 and #6 of the above algorithm are best implemented by walking
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 all the incoming edges which thread to the same destination edge at
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 the same time. That avoids lots of table lookups to get information
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 for the destination edge.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 To realize that implementation we create a list of incoming edges
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 which thread to the same outgoing edge. Thus to implement steps
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 #5 and #6 we traverse our hash table of outgoing edge information.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 For each entry we walk the list of incoming edges which thread to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 the current outgoing edge. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 struct el
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 struct el *next;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 /* Main data structure recording information regarding B's duplicate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 blocks. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 /* We need to efficiently record the unique thread destinations of this
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 block and specific information associated with those destinations. We
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 may have many incoming edges threaded to the same outgoing edge. This
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 can be naturally implemented with a hash table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113
111
kono
parents: 67
diff changeset
114 struct redirection_data : free_ptr_hash<redirection_data>
kono
parents: 67
diff changeset
115 {
kono
parents: 67
diff changeset
116 /* We support wiring up two block duplicates in a jump threading path.
kono
parents: 67
diff changeset
117
kono
parents: 67
diff changeset
118 One is a normal block copy where we remove the control statement
kono
parents: 67
diff changeset
119 and wire up its single remaining outgoing edge to the thread path.
kono
parents: 67
diff changeset
120
kono
parents: 67
diff changeset
121 The other is a joiner block where we leave the control statement
kono
parents: 67
diff changeset
122 in place, but wire one of the outgoing edges to a thread path.
kono
parents: 67
diff changeset
123
kono
parents: 67
diff changeset
124 In theory we could have multiple block duplicates in a jump
kono
parents: 67
diff changeset
125 threading path, but I haven't tried that.
kono
parents: 67
diff changeset
126
kono
parents: 67
diff changeset
127 The duplicate blocks appear in this array in the same order in
kono
parents: 67
diff changeset
128 which they appear in the jump thread path. */
kono
parents: 67
diff changeset
129 basic_block dup_blocks[2];
kono
parents: 67
diff changeset
130
kono
parents: 67
diff changeset
131 /* The jump threading path. */
kono
parents: 67
diff changeset
132 vec<jump_thread_edge *> *path;
kono
parents: 67
diff changeset
133
kono
parents: 67
diff changeset
134 /* A list of incoming edges which we want to thread to the
kono
parents: 67
diff changeset
135 same path. */
kono
parents: 67
diff changeset
136 struct el *incoming_edges;
kono
parents: 67
diff changeset
137
kono
parents: 67
diff changeset
138 /* hash_table support. */
kono
parents: 67
diff changeset
139 static inline hashval_t hash (const redirection_data *);
kono
parents: 67
diff changeset
140 static inline int equal (const redirection_data *, const redirection_data *);
kono
parents: 67
diff changeset
141 };
kono
parents: 67
diff changeset
142
kono
parents: 67
diff changeset
143 /* Dump a jump threading path, including annotations about each
kono
parents: 67
diff changeset
144 edge in the path. */
kono
parents: 67
diff changeset
145
kono
parents: 67
diff changeset
146 static void
kono
parents: 67
diff changeset
147 dump_jump_thread_path (FILE *dump_file, vec<jump_thread_edge *> path,
kono
parents: 67
diff changeset
148 bool registering)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 {
111
kono
parents: 67
diff changeset
150 fprintf (dump_file,
kono
parents: 67
diff changeset
151 " %s%s jump thread: (%d, %d) incoming edge; ",
kono
parents: 67
diff changeset
152 (registering ? "Registering" : "Cancelling"),
kono
parents: 67
diff changeset
153 (path[0]->type == EDGE_FSM_THREAD ? " FSM": ""),
kono
parents: 67
diff changeset
154 path[0]->e->src->index, path[0]->e->dest->index);
kono
parents: 67
diff changeset
155
kono
parents: 67
diff changeset
156 for (unsigned int i = 1; i < path.length (); i++)
kono
parents: 67
diff changeset
157 {
kono
parents: 67
diff changeset
158 /* We can get paths with a NULL edge when the final destination
kono
parents: 67
diff changeset
159 of a jump thread turns out to be a constant address. We dump
kono
parents: 67
diff changeset
160 those paths when debugging, so we have to be prepared for that
kono
parents: 67
diff changeset
161 possibility here. */
kono
parents: 67
diff changeset
162 if (path[i]->e == NULL)
kono
parents: 67
diff changeset
163 continue;
kono
parents: 67
diff changeset
164
kono
parents: 67
diff changeset
165 if (path[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
kono
parents: 67
diff changeset
166 fprintf (dump_file, " (%d, %d) joiner; ",
kono
parents: 67
diff changeset
167 path[i]->e->src->index, path[i]->e->dest->index);
kono
parents: 67
diff changeset
168 if (path[i]->type == EDGE_COPY_SRC_BLOCK)
kono
parents: 67
diff changeset
169 fprintf (dump_file, " (%d, %d) normal;",
kono
parents: 67
diff changeset
170 path[i]->e->src->index, path[i]->e->dest->index);
kono
parents: 67
diff changeset
171 if (path[i]->type == EDGE_NO_COPY_SRC_BLOCK)
kono
parents: 67
diff changeset
172 fprintf (dump_file, " (%d, %d) nocopy;",
kono
parents: 67
diff changeset
173 path[i]->e->src->index, path[i]->e->dest->index);
kono
parents: 67
diff changeset
174 if (path[0]->type == EDGE_FSM_THREAD)
kono
parents: 67
diff changeset
175 fprintf (dump_file, " (%d, %d) ",
kono
parents: 67
diff changeset
176 path[i]->e->src->index, path[i]->e->dest->index);
kono
parents: 67
diff changeset
177 }
kono
parents: 67
diff changeset
178 fputc ('\n', dump_file);
kono
parents: 67
diff changeset
179 }
kono
parents: 67
diff changeset
180
kono
parents: 67
diff changeset
181 /* Simple hashing function. For any given incoming edge E, we're going
kono
parents: 67
diff changeset
182 to be most concerned with the final destination of its jump thread
kono
parents: 67
diff changeset
183 path. So hash on the block index of the final edge in the path. */
kono
parents: 67
diff changeset
184
kono
parents: 67
diff changeset
185 inline hashval_t
kono
parents: 67
diff changeset
186 redirection_data::hash (const redirection_data *p)
kono
parents: 67
diff changeset
187 {
kono
parents: 67
diff changeset
188 vec<jump_thread_edge *> *path = p->path;
kono
parents: 67
diff changeset
189 return path->last ()->e->dest->index;
kono
parents: 67
diff changeset
190 }
kono
parents: 67
diff changeset
191
kono
parents: 67
diff changeset
192 /* Given two hash table entries, return true if they have the same
kono
parents: 67
diff changeset
193 jump threading path. */
kono
parents: 67
diff changeset
194 inline int
kono
parents: 67
diff changeset
195 redirection_data::equal (const redirection_data *p1, const redirection_data *p2)
kono
parents: 67
diff changeset
196 {
kono
parents: 67
diff changeset
197 vec<jump_thread_edge *> *path1 = p1->path;
kono
parents: 67
diff changeset
198 vec<jump_thread_edge *> *path2 = p2->path;
kono
parents: 67
diff changeset
199
kono
parents: 67
diff changeset
200 if (path1->length () != path2->length ())
kono
parents: 67
diff changeset
201 return false;
kono
parents: 67
diff changeset
202
kono
parents: 67
diff changeset
203 for (unsigned int i = 1; i < path1->length (); i++)
kono
parents: 67
diff changeset
204 {
kono
parents: 67
diff changeset
205 if ((*path1)[i]->type != (*path2)[i]->type
kono
parents: 67
diff changeset
206 || (*path1)[i]->e != (*path2)[i]->e)
kono
parents: 67
diff changeset
207 return false;
kono
parents: 67
diff changeset
208 }
kono
parents: 67
diff changeset
209
kono
parents: 67
diff changeset
210 return true;
kono
parents: 67
diff changeset
211 }
kono
parents: 67
diff changeset
212
kono
parents: 67
diff changeset
213 /* Rather than search all the edges in jump thread paths each time
kono
parents: 67
diff changeset
214 DOM is able to simply if control statement, we build a hash table
kono
parents: 67
diff changeset
215 with the deleted edges. We only care about the address of the edge,
kono
parents: 67
diff changeset
216 not its contents. */
kono
parents: 67
diff changeset
217 struct removed_edges : nofree_ptr_hash<edge_def>
kono
parents: 67
diff changeset
218 {
kono
parents: 67
diff changeset
219 static hashval_t hash (edge e) { return htab_hash_pointer (e); }
kono
parents: 67
diff changeset
220 static bool equal (edge e1, edge e2) { return e1 == e2; }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222
111
kono
parents: 67
diff changeset
223 static hash_table<removed_edges> *removed_edges;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 /* Data structure of information to pass to hash table traversal routines. */
111
kono
parents: 67
diff changeset
226 struct ssa_local_info_t
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 /* The current block we are working on. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 basic_block bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230
111
kono
parents: 67
diff changeset
231 /* We only create a template block for the first duplicated block in a
kono
parents: 67
diff changeset
232 jump threading path as we may need many duplicates of that block.
kono
parents: 67
diff changeset
233
kono
parents: 67
diff changeset
234 The second duplicate block in a path is specific to that path. Creating
kono
parents: 67
diff changeset
235 and sharing a template for that block is considerably more difficult. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 basic_block template_block;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
111
kono
parents: 67
diff changeset
238 /* Blocks duplicated for the thread. */
kono
parents: 67
diff changeset
239 bitmap duplicate_blocks;
kono
parents: 67
diff changeset
240
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 /* TRUE if we thread one or more jumps, FALSE otherwise. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 bool jumps_threaded;
111
kono
parents: 67
diff changeset
243
kono
parents: 67
diff changeset
244 /* When we have multiple paths through a joiner which reach different
kono
parents: 67
diff changeset
245 final destinations, then we may need to correct for potential
kono
parents: 67
diff changeset
246 profile insanities. */
kono
parents: 67
diff changeset
247 bool need_profile_correction;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
249
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 /* Passes which use the jump threading code register jump threading
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 opportunities as they are discovered. We keep the registered
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 jump threading opportunities in this vector as edge pairs
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 (original_edge, target_edge). */
111
kono
parents: 67
diff changeset
254 static vec<vec<jump_thread_edge *> *> paths;
kono
parents: 67
diff changeset
255
kono
parents: 67
diff changeset
256 /* When we start updating the CFG for threading, data necessary for jump
kono
parents: 67
diff changeset
257 threading is attached to the AUX field for the incoming edge. Use these
kono
parents: 67
diff changeset
258 macros to access the underlying structure attached to the AUX field. */
kono
parents: 67
diff changeset
259 #define THREAD_PATH(E) ((vec<jump_thread_edge *> *)(E)->aux)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
260
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 /* Jump threading statistics. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
262
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 struct thread_stats_d
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 unsigned long num_threaded_edges;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 struct thread_stats_d thread_stats;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
270
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 /* Remove the last statement in block BB if it is a control statement
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 Also remove all outgoing edges except the edge which reaches DEST_BB.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 If DEST_BB is NULL, then remove all outgoing edges. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
274
111
kono
parents: 67
diff changeset
275 void
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 remove_ctrl_stmt_and_useless_edges (basic_block bb, basic_block dest_bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 gimple_stmt_iterator gsi;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 edge_iterator ei;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 gsi = gsi_last_bb (bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
283
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 /* If the duplicate ends with a control statement, then remove it.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 Note that if we are duplicating the template block rather than the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 original basic block, then the duplicate might not have any real
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 statements in it. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 if (!gsi_end_p (gsi)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 && gsi_stmt (gsi)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 gsi_remove (&gsi, true);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 if (e->dest != dest_bb)
111
kono
parents: 67
diff changeset
299 {
kono
parents: 67
diff changeset
300 free_dom_edge_info (e);
kono
parents: 67
diff changeset
301 remove_edge (e);
kono
parents: 67
diff changeset
302 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 else
111
kono
parents: 67
diff changeset
304 {
kono
parents: 67
diff changeset
305 e->probability = profile_probability::always ();
kono
parents: 67
diff changeset
306 ei_next (&ei);
kono
parents: 67
diff changeset
307 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 }
111
kono
parents: 67
diff changeset
309
kono
parents: 67
diff changeset
310 /* If the remaining edge is a loop exit, there must have
kono
parents: 67
diff changeset
311 a removed edge that was not a loop exit.
kono
parents: 67
diff changeset
312
kono
parents: 67
diff changeset
313 In that case BB and possibly other blocks were previously
kono
parents: 67
diff changeset
314 in the loop, but are now outside the loop. Thus, we need
kono
parents: 67
diff changeset
315 to update the loop structures. */
kono
parents: 67
diff changeset
316 if (single_succ_p (bb)
kono
parents: 67
diff changeset
317 && loop_outer (bb->loop_father)
kono
parents: 67
diff changeset
318 && loop_exit_edge_p (bb->loop_father, single_succ_edge (bb)))
kono
parents: 67
diff changeset
319 loops_state_set (LOOPS_NEED_FIXUP);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321
111
kono
parents: 67
diff changeset
322 /* Create a duplicate of BB. Record the duplicate block in an array
kono
parents: 67
diff changeset
323 indexed by COUNT stored in RD. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
324
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 static void
111
kono
parents: 67
diff changeset
326 create_block_for_threading (basic_block bb,
kono
parents: 67
diff changeset
327 struct redirection_data *rd,
kono
parents: 67
diff changeset
328 unsigned int count,
kono
parents: 67
diff changeset
329 bitmap *duplicate_blocks)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 {
111
kono
parents: 67
diff changeset
331 edge_iterator ei;
kono
parents: 67
diff changeset
332 edge e;
kono
parents: 67
diff changeset
333
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 /* We can use the generic block duplication code and simply remove
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 the stuff we do not need. */
111
kono
parents: 67
diff changeset
336 rd->dup_blocks[count] = duplicate_block (bb, NULL, NULL);
kono
parents: 67
diff changeset
337
kono
parents: 67
diff changeset
338 FOR_EACH_EDGE (e, ei, rd->dup_blocks[count]->succs)
kono
parents: 67
diff changeset
339 e->aux = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
340
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 /* Zero out the profile, since the block is unreachable for now. */
111
kono
parents: 67
diff changeset
342 rd->dup_blocks[count]->count = profile_count::uninitialized ();
kono
parents: 67
diff changeset
343 if (duplicate_blocks)
kono
parents: 67
diff changeset
344 bitmap_set_bit (*duplicate_blocks, rd->dup_blocks[count]->index);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
346
111
kono
parents: 67
diff changeset
347 /* Main data structure to hold information for duplicates of BB. */
kono
parents: 67
diff changeset
348
kono
parents: 67
diff changeset
349 static hash_table<redirection_data> *redirection_data;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
350
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 /* Given an outgoing edge E lookup and return its entry in our hash table.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
352
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 If INSERT is true, then we insert the entry into the hash table if
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 it is not already present. INCOMING_EDGE is added to the list of incoming
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 edges associated with E in the hash table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
356
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 static struct redirection_data *
111
kono
parents: 67
diff changeset
358 lookup_redirection_data (edge e, enum insert_option insert)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 {
111
kono
parents: 67
diff changeset
360 struct redirection_data **slot;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 struct redirection_data *elt;
111
kono
parents: 67
diff changeset
362 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
363
kono
parents: 67
diff changeset
364 /* Build a hash table element so we can see if E is already
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 in the table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 elt = XNEW (struct redirection_data);
111
kono
parents: 67
diff changeset
367 elt->path = path;
kono
parents: 67
diff changeset
368 elt->dup_blocks[0] = NULL;
kono
parents: 67
diff changeset
369 elt->dup_blocks[1] = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 elt->incoming_edges = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
371
111
kono
parents: 67
diff changeset
372 slot = redirection_data->find_slot (elt, insert);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 /* This will only happen if INSERT is false and the entry is not
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 in the hash table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 if (slot == NULL)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 free (elt);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 return NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
381
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 /* This will only happen if E was not in the hash table and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 INSERT is true. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 if (*slot == NULL)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 {
111
kono
parents: 67
diff changeset
386 *slot = elt;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
387 elt->incoming_edges = XNEW (struct el);
111
kono
parents: 67
diff changeset
388 elt->incoming_edges->e = e;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 elt->incoming_edges->next = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 return elt;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 /* E was in the hash table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 /* Free ELT as we do not need it anymore, we will extract the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 relevant entry from the hash table itself. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 free (elt);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
398
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 /* Get the entry stored in the hash table. */
111
kono
parents: 67
diff changeset
400 elt = *slot;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
401
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 /* If insertion was requested, then we need to add INCOMING_EDGE
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 to the list of incoming edges associated with E. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 if (insert)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 {
111
kono
parents: 67
diff changeset
406 struct el *el = XNEW (struct el);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
407 el->next = elt->incoming_edges;
111
kono
parents: 67
diff changeset
408 el->e = e;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 elt->incoming_edges = el;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
411
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 return elt;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
415
111
kono
parents: 67
diff changeset
416 /* Similar to copy_phi_args, except that the PHI arg exists, it just
kono
parents: 67
diff changeset
417 does not have a value associated with it. */
kono
parents: 67
diff changeset
418
kono
parents: 67
diff changeset
419 static void
kono
parents: 67
diff changeset
420 copy_phi_arg_into_existing_phi (edge src_e, edge tgt_e)
kono
parents: 67
diff changeset
421 {
kono
parents: 67
diff changeset
422 int src_idx = src_e->dest_idx;
kono
parents: 67
diff changeset
423 int tgt_idx = tgt_e->dest_idx;
kono
parents: 67
diff changeset
424
kono
parents: 67
diff changeset
425 /* Iterate over each PHI in e->dest. */
kono
parents: 67
diff changeset
426 for (gphi_iterator gsi = gsi_start_phis (src_e->dest),
kono
parents: 67
diff changeset
427 gsi2 = gsi_start_phis (tgt_e->dest);
kono
parents: 67
diff changeset
428 !gsi_end_p (gsi);
kono
parents: 67
diff changeset
429 gsi_next (&gsi), gsi_next (&gsi2))
kono
parents: 67
diff changeset
430 {
kono
parents: 67
diff changeset
431 gphi *src_phi = gsi.phi ();
kono
parents: 67
diff changeset
432 gphi *dest_phi = gsi2.phi ();
kono
parents: 67
diff changeset
433 tree val = gimple_phi_arg_def (src_phi, src_idx);
kono
parents: 67
diff changeset
434 source_location locus = gimple_phi_arg_location (src_phi, src_idx);
kono
parents: 67
diff changeset
435
kono
parents: 67
diff changeset
436 SET_PHI_ARG_DEF (dest_phi, tgt_idx, val);
kono
parents: 67
diff changeset
437 gimple_phi_arg_set_location (dest_phi, tgt_idx, locus);
kono
parents: 67
diff changeset
438 }
kono
parents: 67
diff changeset
439 }
kono
parents: 67
diff changeset
440
kono
parents: 67
diff changeset
441 /* Given ssa_name DEF, backtrack jump threading PATH from node IDX
kono
parents: 67
diff changeset
442 to see if it has constant value in a flow sensitive manner. Set
kono
parents: 67
diff changeset
443 LOCUS to location of the constant phi arg and return the value.
kono
parents: 67
diff changeset
444 Return DEF directly if either PATH or idx is ZERO. */
kono
parents: 67
diff changeset
445
kono
parents: 67
diff changeset
446 static tree
kono
parents: 67
diff changeset
447 get_value_locus_in_path (tree def, vec<jump_thread_edge *> *path,
kono
parents: 67
diff changeset
448 basic_block bb, int idx, source_location *locus)
kono
parents: 67
diff changeset
449 {
kono
parents: 67
diff changeset
450 tree arg;
kono
parents: 67
diff changeset
451 gphi *def_phi;
kono
parents: 67
diff changeset
452 basic_block def_bb;
kono
parents: 67
diff changeset
453
kono
parents: 67
diff changeset
454 if (path == NULL || idx == 0)
kono
parents: 67
diff changeset
455 return def;
kono
parents: 67
diff changeset
456
kono
parents: 67
diff changeset
457 def_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (def));
kono
parents: 67
diff changeset
458 if (!def_phi)
kono
parents: 67
diff changeset
459 return def;
kono
parents: 67
diff changeset
460
kono
parents: 67
diff changeset
461 def_bb = gimple_bb (def_phi);
kono
parents: 67
diff changeset
462 /* Don't propagate loop invariants into deeper loops. */
kono
parents: 67
diff changeset
463 if (!def_bb || bb_loop_depth (def_bb) < bb_loop_depth (bb))
kono
parents: 67
diff changeset
464 return def;
kono
parents: 67
diff changeset
465
kono
parents: 67
diff changeset
466 /* Backtrack jump threading path from IDX to see if def has constant
kono
parents: 67
diff changeset
467 value. */
kono
parents: 67
diff changeset
468 for (int j = idx - 1; j >= 0; j--)
kono
parents: 67
diff changeset
469 {
kono
parents: 67
diff changeset
470 edge e = (*path)[j]->e;
kono
parents: 67
diff changeset
471 if (e->dest == def_bb)
kono
parents: 67
diff changeset
472 {
kono
parents: 67
diff changeset
473 arg = gimple_phi_arg_def (def_phi, e->dest_idx);
kono
parents: 67
diff changeset
474 if (is_gimple_min_invariant (arg))
kono
parents: 67
diff changeset
475 {
kono
parents: 67
diff changeset
476 *locus = gimple_phi_arg_location (def_phi, e->dest_idx);
kono
parents: 67
diff changeset
477 return arg;
kono
parents: 67
diff changeset
478 }
kono
parents: 67
diff changeset
479 break;
kono
parents: 67
diff changeset
480 }
kono
parents: 67
diff changeset
481 }
kono
parents: 67
diff changeset
482
kono
parents: 67
diff changeset
483 return def;
kono
parents: 67
diff changeset
484 }
kono
parents: 67
diff changeset
485
kono
parents: 67
diff changeset
486 /* For each PHI in BB, copy the argument associated with SRC_E to TGT_E.
kono
parents: 67
diff changeset
487 Try to backtrack jump threading PATH from node IDX to see if the arg
kono
parents: 67
diff changeset
488 has constant value, copy constant value instead of argument itself
kono
parents: 67
diff changeset
489 if yes. */
kono
parents: 67
diff changeset
490
kono
parents: 67
diff changeset
491 static void
kono
parents: 67
diff changeset
492 copy_phi_args (basic_block bb, edge src_e, edge tgt_e,
kono
parents: 67
diff changeset
493 vec<jump_thread_edge *> *path, int idx)
kono
parents: 67
diff changeset
494 {
kono
parents: 67
diff changeset
495 gphi_iterator gsi;
kono
parents: 67
diff changeset
496 int src_indx = src_e->dest_idx;
kono
parents: 67
diff changeset
497
kono
parents: 67
diff changeset
498 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents: 67
diff changeset
499 {
kono
parents: 67
diff changeset
500 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
501 tree def = gimple_phi_arg_def (phi, src_indx);
kono
parents: 67
diff changeset
502 source_location locus = gimple_phi_arg_location (phi, src_indx);
kono
parents: 67
diff changeset
503
kono
parents: 67
diff changeset
504 if (TREE_CODE (def) == SSA_NAME
kono
parents: 67
diff changeset
505 && !virtual_operand_p (gimple_phi_result (phi)))
kono
parents: 67
diff changeset
506 def = get_value_locus_in_path (def, path, bb, idx, &locus);
kono
parents: 67
diff changeset
507
kono
parents: 67
diff changeset
508 add_phi_arg (phi, def, tgt_e, locus);
kono
parents: 67
diff changeset
509 }
kono
parents: 67
diff changeset
510 }
kono
parents: 67
diff changeset
511
kono
parents: 67
diff changeset
512 /* We have recently made a copy of ORIG_BB, including its outgoing
kono
parents: 67
diff changeset
513 edges. The copy is NEW_BB. Every PHI node in every direct successor of
kono
parents: 67
diff changeset
514 ORIG_BB has a new argument associated with edge from NEW_BB to the
kono
parents: 67
diff changeset
515 successor. Initialize the PHI argument so that it is equal to the PHI
kono
parents: 67
diff changeset
516 argument associated with the edge from ORIG_BB to the successor.
kono
parents: 67
diff changeset
517 PATH and IDX are used to check if the new PHI argument has constant
kono
parents: 67
diff changeset
518 value in a flow sensitive manner. */
kono
parents: 67
diff changeset
519
kono
parents: 67
diff changeset
520 static void
kono
parents: 67
diff changeset
521 update_destination_phis (basic_block orig_bb, basic_block new_bb,
kono
parents: 67
diff changeset
522 vec<jump_thread_edge *> *path, int idx)
kono
parents: 67
diff changeset
523 {
kono
parents: 67
diff changeset
524 edge_iterator ei;
kono
parents: 67
diff changeset
525 edge e;
kono
parents: 67
diff changeset
526
kono
parents: 67
diff changeset
527 FOR_EACH_EDGE (e, ei, orig_bb->succs)
kono
parents: 67
diff changeset
528 {
kono
parents: 67
diff changeset
529 edge e2 = find_edge (new_bb, e->dest);
kono
parents: 67
diff changeset
530 copy_phi_args (e->dest, e, e2, path, idx);
kono
parents: 67
diff changeset
531 }
kono
parents: 67
diff changeset
532 }
kono
parents: 67
diff changeset
533
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 /* Given a duplicate block and its single destination (both stored
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 in RD). Create an edge between the duplicate and its single
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 destination.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
537
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
538 Add an additional argument to any PHI nodes at the single
111
kono
parents: 67
diff changeset
539 destination. IDX is the start node in jump threading path
kono
parents: 67
diff changeset
540 we start to check to see if the new PHI argument has constant
kono
parents: 67
diff changeset
541 value along the jump threading path. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
542
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
543 static void
111
kono
parents: 67
diff changeset
544 create_edge_and_update_destination_phis (struct redirection_data *rd,
kono
parents: 67
diff changeset
545 basic_block bb, int idx)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
546 {
111
kono
parents: 67
diff changeset
547 edge e = make_single_succ_edge (bb, rd->path->last ()->e->dest, EDGE_FALLTHRU);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
548
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 rescan_loop_exit (e, true, false);
111
kono
parents: 67
diff changeset
550
kono
parents: 67
diff changeset
551 /* We used to copy the thread path here. That was added in 2007
kono
parents: 67
diff changeset
552 and dutifully updated through the representation changes in 2013.
kono
parents: 67
diff changeset
553
kono
parents: 67
diff changeset
554 In 2013 we added code to thread from an interior node through
kono
parents: 67
diff changeset
555 the backedge to another interior node. That runs after the code
kono
parents: 67
diff changeset
556 to thread through loop headers from outside the loop.
kono
parents: 67
diff changeset
557
kono
parents: 67
diff changeset
558 The latter may delete edges in the CFG, including those
kono
parents: 67
diff changeset
559 which appeared in the jump threading path we copied here. Thus
kono
parents: 67
diff changeset
560 we'd end up using a dangling pointer.
kono
parents: 67
diff changeset
561
kono
parents: 67
diff changeset
562 After reviewing the 2007/2011 code, I can't see how anything
kono
parents: 67
diff changeset
563 depended on copying the AUX field and clearly copying the jump
kono
parents: 67
diff changeset
564 threading path is problematical due to embedded edge pointers.
kono
parents: 67
diff changeset
565 It has been removed. */
kono
parents: 67
diff changeset
566 e->aux = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
567
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 /* If there are any PHI nodes at the destination of the outgoing edge
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 from the duplicate block, then we will need to add a new argument
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 to them. The argument should have the same value as the argument
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
571 associated with the outgoing edge stored in RD. */
111
kono
parents: 67
diff changeset
572 copy_phi_args (e->dest, rd->path->last ()->e, e, rd->path, idx);
kono
parents: 67
diff changeset
573 }
kono
parents: 67
diff changeset
574
kono
parents: 67
diff changeset
575 /* Look through PATH beginning at START and return TRUE if there are
kono
parents: 67
diff changeset
576 any additional blocks that need to be duplicated. Otherwise,
kono
parents: 67
diff changeset
577 return FALSE. */
kono
parents: 67
diff changeset
578 static bool
kono
parents: 67
diff changeset
579 any_remaining_duplicated_blocks (vec<jump_thread_edge *> *path,
kono
parents: 67
diff changeset
580 unsigned int start)
kono
parents: 67
diff changeset
581 {
kono
parents: 67
diff changeset
582 for (unsigned int i = start + 1; i < path->length (); i++)
kono
parents: 67
diff changeset
583 {
kono
parents: 67
diff changeset
584 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
kono
parents: 67
diff changeset
585 || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
kono
parents: 67
diff changeset
586 return true;
kono
parents: 67
diff changeset
587 }
kono
parents: 67
diff changeset
588 return false;
kono
parents: 67
diff changeset
589 }
kono
parents: 67
diff changeset
590
kono
parents: 67
diff changeset
591
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
592 /* Compute the amount of profile count coming into the jump threading
111
kono
parents: 67
diff changeset
593 path stored in RD that we are duplicating, returned in PATH_IN_COUNT_PTR and
kono
parents: 67
diff changeset
594 PATH_IN_FREQ_PTR, as well as the amount of counts flowing out of the
kono
parents: 67
diff changeset
595 duplicated path, returned in PATH_OUT_COUNT_PTR. LOCAL_INFO is used to
kono
parents: 67
diff changeset
596 identify blocks duplicated for jump threading, which have duplicated
kono
parents: 67
diff changeset
597 edges that need to be ignored in the analysis. Return true if path contains
kono
parents: 67
diff changeset
598 a joiner, false otherwise.
kono
parents: 67
diff changeset
599
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
600 In the non-joiner case, this is straightforward - all the counts
111
kono
parents: 67
diff changeset
601 flowing into the jump threading path should flow through the duplicated
kono
parents: 67
diff changeset
602 block and out of the duplicated path.
kono
parents: 67
diff changeset
603
kono
parents: 67
diff changeset
604 In the joiner case, it is very tricky. Some of the counts flowing into
kono
parents: 67
diff changeset
605 the original path go offpath at the joiner. The problem is that while
kono
parents: 67
diff changeset
606 we know how much total count goes off-path in the original control flow,
kono
parents: 67
diff changeset
607 we don't know how many of the counts corresponding to just the jump
kono
parents: 67
diff changeset
608 threading path go offpath at the joiner.
kono
parents: 67
diff changeset
609
kono
parents: 67
diff changeset
610 For example, assume we have the following control flow and identified
kono
parents: 67
diff changeset
611 jump threading paths:
kono
parents: 67
diff changeset
612
kono
parents: 67
diff changeset
613 A B C
kono
parents: 67
diff changeset
614 \ | /
kono
parents: 67
diff changeset
615 Ea \ |Eb / Ec
kono
parents: 67
diff changeset
616 \ | /
kono
parents: 67
diff changeset
617 v v v
kono
parents: 67
diff changeset
618 J <-- Joiner
kono
parents: 67
diff changeset
619 / \
kono
parents: 67
diff changeset
620 Eoff/ \Eon
kono
parents: 67
diff changeset
621 / \
kono
parents: 67
diff changeset
622 v v
kono
parents: 67
diff changeset
623 Soff Son <--- Normal
kono
parents: 67
diff changeset
624 /\
kono
parents: 67
diff changeset
625 Ed/ \ Ee
kono
parents: 67
diff changeset
626 / \
kono
parents: 67
diff changeset
627 v v
kono
parents: 67
diff changeset
628 D E
kono
parents: 67
diff changeset
629
kono
parents: 67
diff changeset
630 Jump threading paths: A -> J -> Son -> D (path 1)
kono
parents: 67
diff changeset
631 C -> J -> Son -> E (path 2)
kono
parents: 67
diff changeset
632
kono
parents: 67
diff changeset
633 Note that the control flow could be more complicated:
kono
parents: 67
diff changeset
634 - Each jump threading path may have more than one incoming edge. I.e. A and
kono
parents: 67
diff changeset
635 Ea could represent multiple incoming blocks/edges that are included in
kono
parents: 67
diff changeset
636 path 1.
kono
parents: 67
diff changeset
637 - There could be EDGE_NO_COPY_SRC_BLOCK edges after the joiner (either
kono
parents: 67
diff changeset
638 before or after the "normal" copy block). These are not duplicated onto
kono
parents: 67
diff changeset
639 the jump threading path, as they are single-successor.
kono
parents: 67
diff changeset
640 - Any of the blocks along the path may have other incoming edges that
kono
parents: 67
diff changeset
641 are not part of any jump threading path, but add profile counts along
kono
parents: 67
diff changeset
642 the path.
kono
parents: 67
diff changeset
643
kono
parents: 67
diff changeset
644 In the above example, after all jump threading is complete, we will
kono
parents: 67
diff changeset
645 end up with the following control flow:
kono
parents: 67
diff changeset
646
kono
parents: 67
diff changeset
647 A B C
kono
parents: 67
diff changeset
648 | | |
kono
parents: 67
diff changeset
649 Ea| |Eb |Ec
kono
parents: 67
diff changeset
650 | | |
kono
parents: 67
diff changeset
651 v v v
kono
parents: 67
diff changeset
652 Ja J Jc
kono
parents: 67
diff changeset
653 / \ / \Eon' / \
kono
parents: 67
diff changeset
654 Eona/ \ ---/---\-------- \Eonc
kono
parents: 67
diff changeset
655 / \ / / \ \
kono
parents: 67
diff changeset
656 v v v v v
kono
parents: 67
diff changeset
657 Sona Soff Son Sonc
kono
parents: 67
diff changeset
658 \ /\ /
kono
parents: 67
diff changeset
659 \___________ / \ _____/
kono
parents: 67
diff changeset
660 \ / \/
kono
parents: 67
diff changeset
661 vv v
kono
parents: 67
diff changeset
662 D E
kono
parents: 67
diff changeset
663
kono
parents: 67
diff changeset
664 The main issue to notice here is that when we are processing path 1
kono
parents: 67
diff changeset
665 (A->J->Son->D) we need to figure out the outgoing edge weights to
kono
parents: 67
diff changeset
666 the duplicated edges Ja->Sona and Ja->Soff, while ensuring that the
kono
parents: 67
diff changeset
667 sum of the incoming weights to D remain Ed. The problem with simply
kono
parents: 67
diff changeset
668 assuming that Ja (and Jc when processing path 2) has the same outgoing
kono
parents: 67
diff changeset
669 probabilities to its successors as the original block J, is that after
kono
parents: 67
diff changeset
670 all paths are processed and other edges/counts removed (e.g. none
kono
parents: 67
diff changeset
671 of Ec will reach D after processing path 2), we may end up with not
kono
parents: 67
diff changeset
672 enough count flowing along duplicated edge Sona->D.
kono
parents: 67
diff changeset
673
kono
parents: 67
diff changeset
674 Therefore, in the case of a joiner, we keep track of all counts
kono
parents: 67
diff changeset
675 coming in along the current path, as well as from predecessors not
kono
parents: 67
diff changeset
676 on any jump threading path (Eb in the above example). While we
kono
parents: 67
diff changeset
677 first assume that the duplicated Eona for Ja->Sona has the same
kono
parents: 67
diff changeset
678 probability as the original, we later compensate for other jump
kono
parents: 67
diff changeset
679 threading paths that may eliminate edges. We do that by keep track
kono
parents: 67
diff changeset
680 of all counts coming into the original path that are not in a jump
kono
parents: 67
diff changeset
681 thread (Eb in the above example, but as noted earlier, there could
kono
parents: 67
diff changeset
682 be other predecessors incoming to the path at various points, such
kono
parents: 67
diff changeset
683 as at Son). Call this cumulative non-path count coming into the path
kono
parents: 67
diff changeset
684 before D as Enonpath. We then ensure that the count from Sona->D is as at
kono
parents: 67
diff changeset
685 least as big as (Ed - Enonpath), but no bigger than the minimum
kono
parents: 67
diff changeset
686 weight along the jump threading path. The probabilities of both the
kono
parents: 67
diff changeset
687 original and duplicated joiner block J and Ja will be adjusted
kono
parents: 67
diff changeset
688 accordingly after the updates. */
kono
parents: 67
diff changeset
689
kono
parents: 67
diff changeset
690 static bool
kono
parents: 67
diff changeset
691 compute_path_counts (struct redirection_data *rd,
kono
parents: 67
diff changeset
692 ssa_local_info_t *local_info,
kono
parents: 67
diff changeset
693 profile_count *path_in_count_ptr,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
694 profile_count *path_out_count_ptr)
111
kono
parents: 67
diff changeset
695 {
kono
parents: 67
diff changeset
696 edge e = rd->incoming_edges->e;
kono
parents: 67
diff changeset
697 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
698 edge elast = path->last ()->e;
kono
parents: 67
diff changeset
699 profile_count nonpath_count = profile_count::zero ();
kono
parents: 67
diff changeset
700 bool has_joiner = false;
kono
parents: 67
diff changeset
701 profile_count path_in_count = profile_count::zero ();
kono
parents: 67
diff changeset
702
kono
parents: 67
diff changeset
703 /* Start by accumulating incoming edge counts to the path's first bb
kono
parents: 67
diff changeset
704 into a couple buckets:
kono
parents: 67
diff changeset
705 path_in_count: total count of incoming edges that flow into the
kono
parents: 67
diff changeset
706 current path.
kono
parents: 67
diff changeset
707 nonpath_count: total count of incoming edges that are not
kono
parents: 67
diff changeset
708 flowing along *any* path. These are the counts
kono
parents: 67
diff changeset
709 that will still flow along the original path after
kono
parents: 67
diff changeset
710 all path duplication is done by potentially multiple
kono
parents: 67
diff changeset
711 calls to this routine.
kono
parents: 67
diff changeset
712 (any other incoming edge counts are for a different jump threading
kono
parents: 67
diff changeset
713 path that will be handled by a later call to this routine.)
kono
parents: 67
diff changeset
714 To make this easier, start by recording all incoming edges that flow into
kono
parents: 67
diff changeset
715 the current path in a bitmap. We could add up the path's incoming edge
kono
parents: 67
diff changeset
716 counts here, but we still need to walk all the first bb's incoming edges
kono
parents: 67
diff changeset
717 below to add up the counts of the other edges not included in this jump
kono
parents: 67
diff changeset
718 threading path. */
kono
parents: 67
diff changeset
719 struct el *next, *el;
kono
parents: 67
diff changeset
720 auto_bitmap in_edge_srcs;
kono
parents: 67
diff changeset
721 for (el = rd->incoming_edges; el; el = next)
kono
parents: 67
diff changeset
722 {
kono
parents: 67
diff changeset
723 next = el->next;
kono
parents: 67
diff changeset
724 bitmap_set_bit (in_edge_srcs, el->e->src->index);
kono
parents: 67
diff changeset
725 }
kono
parents: 67
diff changeset
726 edge ein;
kono
parents: 67
diff changeset
727 edge_iterator ei;
kono
parents: 67
diff changeset
728 FOR_EACH_EDGE (ein, ei, e->dest->preds)
kono
parents: 67
diff changeset
729 {
kono
parents: 67
diff changeset
730 vec<jump_thread_edge *> *ein_path = THREAD_PATH (ein);
kono
parents: 67
diff changeset
731 /* Simply check the incoming edge src against the set captured above. */
kono
parents: 67
diff changeset
732 if (ein_path
kono
parents: 67
diff changeset
733 && bitmap_bit_p (in_edge_srcs, (*ein_path)[0]->e->src->index))
kono
parents: 67
diff changeset
734 {
kono
parents: 67
diff changeset
735 /* It is necessary but not sufficient that the last path edges
kono
parents: 67
diff changeset
736 are identical. There may be different paths that share the
kono
parents: 67
diff changeset
737 same last path edge in the case where the last edge has a nocopy
kono
parents: 67
diff changeset
738 source block. */
kono
parents: 67
diff changeset
739 gcc_assert (ein_path->last ()->e == elast);
kono
parents: 67
diff changeset
740 path_in_count += ein->count ();
kono
parents: 67
diff changeset
741 }
kono
parents: 67
diff changeset
742 else if (!ein_path)
kono
parents: 67
diff changeset
743 {
kono
parents: 67
diff changeset
744 /* Keep track of the incoming edges that are not on any jump-threading
kono
parents: 67
diff changeset
745 path. These counts will still flow out of original path after all
kono
parents: 67
diff changeset
746 jump threading is complete. */
kono
parents: 67
diff changeset
747 nonpath_count += ein->count ();
kono
parents: 67
diff changeset
748 }
kono
parents: 67
diff changeset
749 }
kono
parents: 67
diff changeset
750
kono
parents: 67
diff changeset
751 /* Now compute the fraction of the total count coming into the first
kono
parents: 67
diff changeset
752 path bb that is from the current threading path. */
kono
parents: 67
diff changeset
753 profile_count total_count = e->dest->count;
kono
parents: 67
diff changeset
754 /* Handle incoming profile insanities. */
kono
parents: 67
diff changeset
755 if (total_count < path_in_count)
kono
parents: 67
diff changeset
756 path_in_count = total_count;
kono
parents: 67
diff changeset
757 profile_probability onpath_scale = path_in_count.probability_in (total_count);
kono
parents: 67
diff changeset
758
kono
parents: 67
diff changeset
759 /* Walk the entire path to do some more computation in order to estimate
kono
parents: 67
diff changeset
760 how much of the path_in_count will flow out of the duplicated threading
kono
parents: 67
diff changeset
761 path. In the non-joiner case this is straightforward (it should be
kono
parents: 67
diff changeset
762 the same as path_in_count, although we will handle incoming profile
kono
parents: 67
diff changeset
763 insanities by setting it equal to the minimum count along the path).
kono
parents: 67
diff changeset
764
kono
parents: 67
diff changeset
765 In the joiner case, we need to estimate how much of the path_in_count
kono
parents: 67
diff changeset
766 will stay on the threading path after the joiner's conditional branch.
kono
parents: 67
diff changeset
767 We don't really know for sure how much of the counts
kono
parents: 67
diff changeset
768 associated with this path go to each successor of the joiner, but we'll
kono
parents: 67
diff changeset
769 estimate based on the fraction of the total count coming into the path
kono
parents: 67
diff changeset
770 bb was from the threading paths (computed above in onpath_scale).
kono
parents: 67
diff changeset
771 Afterwards, we will need to do some fixup to account for other threading
kono
parents: 67
diff changeset
772 paths and possible profile insanities.
kono
parents: 67
diff changeset
773
kono
parents: 67
diff changeset
774 In order to estimate the joiner case's counts we also need to update
kono
parents: 67
diff changeset
775 nonpath_count with any additional counts coming into the path. Other
kono
parents: 67
diff changeset
776 blocks along the path may have additional predecessors from outside
kono
parents: 67
diff changeset
777 the path. */
kono
parents: 67
diff changeset
778 profile_count path_out_count = path_in_count;
kono
parents: 67
diff changeset
779 profile_count min_path_count = path_in_count;
kono
parents: 67
diff changeset
780 for (unsigned int i = 1; i < path->length (); i++)
kono
parents: 67
diff changeset
781 {
kono
parents: 67
diff changeset
782 edge epath = (*path)[i]->e;
kono
parents: 67
diff changeset
783 profile_count cur_count = epath->count ();
kono
parents: 67
diff changeset
784 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
kono
parents: 67
diff changeset
785 {
kono
parents: 67
diff changeset
786 has_joiner = true;
kono
parents: 67
diff changeset
787 cur_count = cur_count.apply_probability (onpath_scale);
kono
parents: 67
diff changeset
788 }
kono
parents: 67
diff changeset
789 /* In the joiner case we need to update nonpath_count for any edges
kono
parents: 67
diff changeset
790 coming into the path that will contribute to the count flowing
kono
parents: 67
diff changeset
791 into the path successor. */
kono
parents: 67
diff changeset
792 if (has_joiner && epath != elast)
kono
parents: 67
diff changeset
793 {
kono
parents: 67
diff changeset
794 /* Look for other incoming edges after joiner. */
kono
parents: 67
diff changeset
795 FOR_EACH_EDGE (ein, ei, epath->dest->preds)
kono
parents: 67
diff changeset
796 {
kono
parents: 67
diff changeset
797 if (ein != epath
kono
parents: 67
diff changeset
798 /* Ignore in edges from blocks we have duplicated for a
kono
parents: 67
diff changeset
799 threading path, which have duplicated edge counts until
kono
parents: 67
diff changeset
800 they are redirected by an invocation of this routine. */
kono
parents: 67
diff changeset
801 && !bitmap_bit_p (local_info->duplicate_blocks,
kono
parents: 67
diff changeset
802 ein->src->index))
kono
parents: 67
diff changeset
803 nonpath_count += ein->count ();
kono
parents: 67
diff changeset
804 }
kono
parents: 67
diff changeset
805 }
kono
parents: 67
diff changeset
806 if (cur_count < path_out_count)
kono
parents: 67
diff changeset
807 path_out_count = cur_count;
kono
parents: 67
diff changeset
808 if (epath->count () < min_path_count)
kono
parents: 67
diff changeset
809 min_path_count = epath->count ();
kono
parents: 67
diff changeset
810 }
kono
parents: 67
diff changeset
811
kono
parents: 67
diff changeset
812 /* We computed path_out_count above assuming that this path targeted
kono
parents: 67
diff changeset
813 the joiner's on-path successor with the same likelihood as it
kono
parents: 67
diff changeset
814 reached the joiner. However, other thread paths through the joiner
kono
parents: 67
diff changeset
815 may take a different path through the normal copy source block
kono
parents: 67
diff changeset
816 (i.e. they have a different elast), meaning that they do not
kono
parents: 67
diff changeset
817 contribute any counts to this path's elast. As a result, it may
kono
parents: 67
diff changeset
818 turn out that this path must have more count flowing to the on-path
kono
parents: 67
diff changeset
819 successor of the joiner. Essentially, all of this path's elast
kono
parents: 67
diff changeset
820 count must be contributed by this path and any nonpath counts
kono
parents: 67
diff changeset
821 (since any path through the joiner with a different elast will not
kono
parents: 67
diff changeset
822 include a copy of this elast in its duplicated path).
kono
parents: 67
diff changeset
823 So ensure that this path's path_out_count is at least the
kono
parents: 67
diff changeset
824 difference between elast->count () and nonpath_count. Otherwise the edge
kono
parents: 67
diff changeset
825 counts after threading will not be sane. */
kono
parents: 67
diff changeset
826 if (local_info->need_profile_correction
kono
parents: 67
diff changeset
827 && has_joiner && path_out_count < elast->count () - nonpath_count)
kono
parents: 67
diff changeset
828 {
kono
parents: 67
diff changeset
829 path_out_count = elast->count () - nonpath_count;
kono
parents: 67
diff changeset
830 /* But neither can we go above the minimum count along the path
kono
parents: 67
diff changeset
831 we are duplicating. This can be an issue due to profile
kono
parents: 67
diff changeset
832 insanities coming in to this pass. */
kono
parents: 67
diff changeset
833 if (path_out_count > min_path_count)
kono
parents: 67
diff changeset
834 path_out_count = min_path_count;
kono
parents: 67
diff changeset
835 }
kono
parents: 67
diff changeset
836
kono
parents: 67
diff changeset
837 *path_in_count_ptr = path_in_count;
kono
parents: 67
diff changeset
838 *path_out_count_ptr = path_out_count;
kono
parents: 67
diff changeset
839 return has_joiner;
kono
parents: 67
diff changeset
840 }
kono
parents: 67
diff changeset
841
kono
parents: 67
diff changeset
842
kono
parents: 67
diff changeset
843 /* Update the counts and frequencies for both an original path
kono
parents: 67
diff changeset
844 edge EPATH and its duplicate EDUP. The duplicate source block
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
845 will get a count of PATH_IN_COUNT and PATH_IN_FREQ,
111
kono
parents: 67
diff changeset
846 and the duplicate edge EDUP will have a count of PATH_OUT_COUNT. */
kono
parents: 67
diff changeset
847 static void
kono
parents: 67
diff changeset
848 update_profile (edge epath, edge edup, profile_count path_in_count,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
849 profile_count path_out_count)
111
kono
parents: 67
diff changeset
850 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
851
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
852 /* First update the duplicated block's count. */
111
kono
parents: 67
diff changeset
853 if (edup)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
854 {
111
kono
parents: 67
diff changeset
855 basic_block dup_block = edup->src;
kono
parents: 67
diff changeset
856
kono
parents: 67
diff changeset
857 /* Edup's count is reduced by path_out_count. We need to redistribute
kono
parents: 67
diff changeset
858 probabilities to the remaining edges. */
kono
parents: 67
diff changeset
859
kono
parents: 67
diff changeset
860 edge esucc;
kono
parents: 67
diff changeset
861 edge_iterator ei;
kono
parents: 67
diff changeset
862 profile_probability edup_prob
kono
parents: 67
diff changeset
863 = path_out_count.probability_in (path_in_count);
kono
parents: 67
diff changeset
864
kono
parents: 67
diff changeset
865 /* Either scale up or down the remaining edges.
kono
parents: 67
diff changeset
866 probabilities are always in range <0,1> and thus we can't do
kono
parents: 67
diff changeset
867 both by same loop. */
kono
parents: 67
diff changeset
868 if (edup->probability > edup_prob)
kono
parents: 67
diff changeset
869 {
kono
parents: 67
diff changeset
870 profile_probability rev_scale
kono
parents: 67
diff changeset
871 = (profile_probability::always () - edup->probability)
kono
parents: 67
diff changeset
872 / (profile_probability::always () - edup_prob);
kono
parents: 67
diff changeset
873 FOR_EACH_EDGE (esucc, ei, dup_block->succs)
kono
parents: 67
diff changeset
874 if (esucc != edup)
kono
parents: 67
diff changeset
875 esucc->probability /= rev_scale;
kono
parents: 67
diff changeset
876 }
kono
parents: 67
diff changeset
877 else if (edup->probability < edup_prob)
kono
parents: 67
diff changeset
878 {
kono
parents: 67
diff changeset
879 profile_probability scale
kono
parents: 67
diff changeset
880 = (profile_probability::always () - edup_prob)
kono
parents: 67
diff changeset
881 / (profile_probability::always () - edup->probability);
kono
parents: 67
diff changeset
882 FOR_EACH_EDGE (esucc, ei, dup_block->succs)
kono
parents: 67
diff changeset
883 if (esucc != edup)
kono
parents: 67
diff changeset
884 esucc->probability *= scale;
kono
parents: 67
diff changeset
885 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
886 if (edup_prob.initialized_p ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
887 edup->probability = edup_prob;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
888
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
889 gcc_assert (!dup_block->count.initialized_p ());
111
kono
parents: 67
diff changeset
890 dup_block->count = path_in_count;
kono
parents: 67
diff changeset
891 }
kono
parents: 67
diff changeset
892
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
893 if (path_in_count == profile_count::zero ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
894 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
895
111
kono
parents: 67
diff changeset
896 profile_count final_count = epath->count () - path_out_count;
kono
parents: 67
diff changeset
897
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
898 /* Now update the original block's count in the
111
kono
parents: 67
diff changeset
899 opposite manner - remove the counts/freq that will flow
kono
parents: 67
diff changeset
900 into the duplicated block. Handle underflow due to precision/
kono
parents: 67
diff changeset
901 rounding issues. */
kono
parents: 67
diff changeset
902 epath->src->count -= path_in_count;
kono
parents: 67
diff changeset
903
kono
parents: 67
diff changeset
904 /* Next update this path edge's original and duplicated counts. We know
kono
parents: 67
diff changeset
905 that the duplicated path will have path_out_count flowing
kono
parents: 67
diff changeset
906 out of it (in the joiner case this is the count along the duplicated path
kono
parents: 67
diff changeset
907 out of the duplicated joiner). This count can then be removed from the
kono
parents: 67
diff changeset
908 original path edge. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
909
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
910 edge esucc;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
911 edge_iterator ei;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
912 profile_probability epath_prob = final_count.probability_in (epath->src->count);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
913
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
914 if (epath->probability > epath_prob)
111
kono
parents: 67
diff changeset
915 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
916 profile_probability rev_scale
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
917 = (profile_probability::always () - epath->probability)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
918 / (profile_probability::always () - epath_prob);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
919 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
920 if (esucc != epath)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
921 esucc->probability /= rev_scale;
111
kono
parents: 67
diff changeset
922 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
923 else if (epath->probability < epath_prob)
111
kono
parents: 67
diff changeset
924 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
925 profile_probability scale
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
926 = (profile_probability::always () - epath_prob)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
927 / (profile_probability::always () - epath->probability);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
928 FOR_EACH_EDGE (esucc, ei, epath->src->succs)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
929 if (esucc != epath)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
930 esucc->probability *= scale;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
931 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
932 if (epath_prob.initialized_p ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
933 epath->probability = epath_prob;
111
kono
parents: 67
diff changeset
934 }
kono
parents: 67
diff changeset
935
kono
parents: 67
diff changeset
936 /* Wire up the outgoing edges from the duplicate blocks and
kono
parents: 67
diff changeset
937 update any PHIs as needed. Also update the profile counts
kono
parents: 67
diff changeset
938 on the original and duplicate blocks and edges. */
kono
parents: 67
diff changeset
939 void
kono
parents: 67
diff changeset
940 ssa_fix_duplicate_block_edges (struct redirection_data *rd,
kono
parents: 67
diff changeset
941 ssa_local_info_t *local_info)
kono
parents: 67
diff changeset
942 {
kono
parents: 67
diff changeset
943 bool multi_incomings = (rd->incoming_edges->next != NULL);
kono
parents: 67
diff changeset
944 edge e = rd->incoming_edges->e;
kono
parents: 67
diff changeset
945 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
946 edge elast = path->last ()->e;
kono
parents: 67
diff changeset
947 profile_count path_in_count = profile_count::zero ();
kono
parents: 67
diff changeset
948 profile_count path_out_count = profile_count::zero ();
kono
parents: 67
diff changeset
949
kono
parents: 67
diff changeset
950 /* First determine how much profile count to move from original
kono
parents: 67
diff changeset
951 path to the duplicate path. This is tricky in the presence of
kono
parents: 67
diff changeset
952 a joiner (see comments for compute_path_counts), where some portion
kono
parents: 67
diff changeset
953 of the path's counts will flow off-path from the joiner. In the
kono
parents: 67
diff changeset
954 non-joiner case the path_in_count and path_out_count should be the
kono
parents: 67
diff changeset
955 same. */
kono
parents: 67
diff changeset
956 bool has_joiner = compute_path_counts (rd, local_info,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
957 &path_in_count, &path_out_count);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
958
111
kono
parents: 67
diff changeset
959 for (unsigned int count = 0, i = 1; i < path->length (); i++)
kono
parents: 67
diff changeset
960 {
kono
parents: 67
diff changeset
961 edge epath = (*path)[i]->e;
kono
parents: 67
diff changeset
962
kono
parents: 67
diff changeset
963 /* If we were threading through an joiner block, then we want
kono
parents: 67
diff changeset
964 to keep its control statement and redirect an outgoing edge.
kono
parents: 67
diff changeset
965 Else we want to remove the control statement & edges, then create
kono
parents: 67
diff changeset
966 a new outgoing edge. In both cases we may need to update PHIs. */
kono
parents: 67
diff changeset
967 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
kono
parents: 67
diff changeset
968 {
kono
parents: 67
diff changeset
969 edge victim;
kono
parents: 67
diff changeset
970 edge e2;
kono
parents: 67
diff changeset
971
kono
parents: 67
diff changeset
972 gcc_assert (has_joiner);
kono
parents: 67
diff changeset
973
kono
parents: 67
diff changeset
974 /* This updates the PHIs at the destination of the duplicate
kono
parents: 67
diff changeset
975 block. Pass 0 instead of i if we are threading a path which
kono
parents: 67
diff changeset
976 has multiple incoming edges. */
kono
parents: 67
diff changeset
977 update_destination_phis (local_info->bb, rd->dup_blocks[count],
kono
parents: 67
diff changeset
978 path, multi_incomings ? 0 : i);
kono
parents: 67
diff changeset
979
kono
parents: 67
diff changeset
980 /* Find the edge from the duplicate block to the block we're
kono
parents: 67
diff changeset
981 threading through. That's the edge we want to redirect. */
kono
parents: 67
diff changeset
982 victim = find_edge (rd->dup_blocks[count], (*path)[i]->e->dest);
kono
parents: 67
diff changeset
983
kono
parents: 67
diff changeset
984 /* If there are no remaining blocks on the path to duplicate,
kono
parents: 67
diff changeset
985 then redirect VICTIM to the final destination of the jump
kono
parents: 67
diff changeset
986 threading path. */
kono
parents: 67
diff changeset
987 if (!any_remaining_duplicated_blocks (path, i))
kono
parents: 67
diff changeset
988 {
kono
parents: 67
diff changeset
989 e2 = redirect_edge_and_branch (victim, elast->dest);
kono
parents: 67
diff changeset
990 /* If we redirected the edge, then we need to copy PHI arguments
kono
parents: 67
diff changeset
991 at the target. If the edge already existed (e2 != victim
kono
parents: 67
diff changeset
992 case), then the PHIs in the target already have the correct
kono
parents: 67
diff changeset
993 arguments. */
kono
parents: 67
diff changeset
994 if (e2 == victim)
kono
parents: 67
diff changeset
995 copy_phi_args (e2->dest, elast, e2,
kono
parents: 67
diff changeset
996 path, multi_incomings ? 0 : i);
kono
parents: 67
diff changeset
997 }
kono
parents: 67
diff changeset
998 else
kono
parents: 67
diff changeset
999 {
kono
parents: 67
diff changeset
1000 /* Redirect VICTIM to the next duplicated block in the path. */
kono
parents: 67
diff changeset
1001 e2 = redirect_edge_and_branch (victim, rd->dup_blocks[count + 1]);
kono
parents: 67
diff changeset
1002
kono
parents: 67
diff changeset
1003 /* We need to update the PHIs in the next duplicated block. We
kono
parents: 67
diff changeset
1004 want the new PHI args to have the same value as they had
kono
parents: 67
diff changeset
1005 in the source of the next duplicate block.
kono
parents: 67
diff changeset
1006
kono
parents: 67
diff changeset
1007 Thus, we need to know which edge we traversed into the
kono
parents: 67
diff changeset
1008 source of the duplicate. Furthermore, we may have
kono
parents: 67
diff changeset
1009 traversed many edges to reach the source of the duplicate.
kono
parents: 67
diff changeset
1010
kono
parents: 67
diff changeset
1011 Walk through the path starting at element I until we
kono
parents: 67
diff changeset
1012 hit an edge marked with EDGE_COPY_SRC_BLOCK. We want
kono
parents: 67
diff changeset
1013 the edge from the prior element. */
kono
parents: 67
diff changeset
1014 for (unsigned int j = i + 1; j < path->length (); j++)
kono
parents: 67
diff changeset
1015 {
kono
parents: 67
diff changeset
1016 if ((*path)[j]->type == EDGE_COPY_SRC_BLOCK)
kono
parents: 67
diff changeset
1017 {
kono
parents: 67
diff changeset
1018 copy_phi_arg_into_existing_phi ((*path)[j - 1]->e, e2);
kono
parents: 67
diff changeset
1019 break;
kono
parents: 67
diff changeset
1020 }
kono
parents: 67
diff changeset
1021 }
kono
parents: 67
diff changeset
1022 }
kono
parents: 67
diff changeset
1023
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1024 /* Update the counts of both the original block
111
kono
parents: 67
diff changeset
1025 and path edge, and the duplicates. The path duplicate's
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1026 incoming count are the totals for all edges
111
kono
parents: 67
diff changeset
1027 incoming to this jump threading path computed earlier.
kono
parents: 67
diff changeset
1028 And we know that the duplicated path will have path_out_count
kono
parents: 67
diff changeset
1029 flowing out of it (i.e. along the duplicated path out of the
kono
parents: 67
diff changeset
1030 duplicated joiner). */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1031 update_profile (epath, e2, path_in_count, path_out_count);
111
kono
parents: 67
diff changeset
1032 }
kono
parents: 67
diff changeset
1033 else if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK)
kono
parents: 67
diff changeset
1034 {
kono
parents: 67
diff changeset
1035 remove_ctrl_stmt_and_useless_edges (rd->dup_blocks[count], NULL);
kono
parents: 67
diff changeset
1036 create_edge_and_update_destination_phis (rd, rd->dup_blocks[count],
kono
parents: 67
diff changeset
1037 multi_incomings ? 0 : i);
kono
parents: 67
diff changeset
1038 if (count == 1)
kono
parents: 67
diff changeset
1039 single_succ_edge (rd->dup_blocks[1])->aux = NULL;
kono
parents: 67
diff changeset
1040
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1041 /* Update the counts of both the original block
111
kono
parents: 67
diff changeset
1042 and path edge, and the duplicates. Since we are now after
kono
parents: 67
diff changeset
1043 any joiner that may have existed on the path, the count
kono
parents: 67
diff changeset
1044 flowing along the duplicated threaded path is path_out_count.
kono
parents: 67
diff changeset
1045 If we didn't have a joiner, then cur_path_freq was the sum
kono
parents: 67
diff changeset
1046 of the total frequencies along all incoming edges to the
kono
parents: 67
diff changeset
1047 thread path (path_in_freq). If we had a joiner, it would have
kono
parents: 67
diff changeset
1048 been updated at the end of that handling to the edge frequency
kono
parents: 67
diff changeset
1049 along the duplicated joiner path edge. */
kono
parents: 67
diff changeset
1050 update_profile (epath, EDGE_SUCC (rd->dup_blocks[count], 0),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1051 path_out_count, path_out_count);
111
kono
parents: 67
diff changeset
1052 }
kono
parents: 67
diff changeset
1053 else
kono
parents: 67
diff changeset
1054 {
kono
parents: 67
diff changeset
1055 /* No copy case. In this case we don't have an equivalent block
kono
parents: 67
diff changeset
1056 on the duplicated thread path to update, but we do need
kono
parents: 67
diff changeset
1057 to remove the portion of the counts/freqs that were moved
kono
parents: 67
diff changeset
1058 to the duplicated path from the counts/freqs flowing through
kono
parents: 67
diff changeset
1059 this block on the original path. Since all the no-copy edges
kono
parents: 67
diff changeset
1060 are after any joiner, the removed count is the same as
kono
parents: 67
diff changeset
1061 path_out_count.
kono
parents: 67
diff changeset
1062
kono
parents: 67
diff changeset
1063 If we didn't have a joiner, then cur_path_freq was the sum
kono
parents: 67
diff changeset
1064 of the total frequencies along all incoming edges to the
kono
parents: 67
diff changeset
1065 thread path (path_in_freq). If we had a joiner, it would have
kono
parents: 67
diff changeset
1066 been updated at the end of that handling to the edge frequency
kono
parents: 67
diff changeset
1067 along the duplicated joiner path edge. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1068 update_profile (epath, NULL, path_out_count, path_out_count);
111
kono
parents: 67
diff changeset
1069 }
kono
parents: 67
diff changeset
1070
kono
parents: 67
diff changeset
1071 /* Increment the index into the duplicated path when we processed
kono
parents: 67
diff changeset
1072 a duplicated block. */
kono
parents: 67
diff changeset
1073 if ((*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK
kono
parents: 67
diff changeset
1074 || (*path)[i]->type == EDGE_COPY_SRC_BLOCK)
kono
parents: 67
diff changeset
1075 {
kono
parents: 67
diff changeset
1076 count++;
kono
parents: 67
diff changeset
1077 }
kono
parents: 67
diff changeset
1078 }
kono
parents: 67
diff changeset
1079 }
kono
parents: 67
diff changeset
1080
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1081 /* Hash table traversal callback routine to create duplicate blocks. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1082
111
kono
parents: 67
diff changeset
1083 int
kono
parents: 67
diff changeset
1084 ssa_create_duplicates (struct redirection_data **slot,
kono
parents: 67
diff changeset
1085 ssa_local_info_t *local_info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1086 {
111
kono
parents: 67
diff changeset
1087 struct redirection_data *rd = *slot;
kono
parents: 67
diff changeset
1088
kono
parents: 67
diff changeset
1089 /* The second duplicated block in a jump threading path is specific
kono
parents: 67
diff changeset
1090 to the path. So it gets stored in RD rather than in LOCAL_DATA.
kono
parents: 67
diff changeset
1091
kono
parents: 67
diff changeset
1092 Each time we're called, we have to look through the path and see
kono
parents: 67
diff changeset
1093 if a second block needs to be duplicated.
kono
parents: 67
diff changeset
1094
kono
parents: 67
diff changeset
1095 Note the search starts with the third edge on the path. The first
kono
parents: 67
diff changeset
1096 edge is the incoming edge, the second edge always has its source
kono
parents: 67
diff changeset
1097 duplicated. Thus we start our search with the third edge. */
kono
parents: 67
diff changeset
1098 vec<jump_thread_edge *> *path = rd->path;
kono
parents: 67
diff changeset
1099 for (unsigned int i = 2; i < path->length (); i++)
kono
parents: 67
diff changeset
1100 {
kono
parents: 67
diff changeset
1101 if ((*path)[i]->type == EDGE_COPY_SRC_BLOCK
kono
parents: 67
diff changeset
1102 || (*path)[i]->type == EDGE_COPY_SRC_JOINER_BLOCK)
kono
parents: 67
diff changeset
1103 {
kono
parents: 67
diff changeset
1104 create_block_for_threading ((*path)[i]->e->src, rd, 1,
kono
parents: 67
diff changeset
1105 &local_info->duplicate_blocks);
kono
parents: 67
diff changeset
1106 break;
kono
parents: 67
diff changeset
1107 }
kono
parents: 67
diff changeset
1108 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1109
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1110 /* Create a template block if we have not done so already. Otherwise
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1111 use the template to create a new block. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1112 if (local_info->template_block == NULL)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1113 {
111
kono
parents: 67
diff changeset
1114 create_block_for_threading ((*path)[1]->e->src, rd, 0,
kono
parents: 67
diff changeset
1115 &local_info->duplicate_blocks);
kono
parents: 67
diff changeset
1116 local_info->template_block = rd->dup_blocks[0];
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1117
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1118 /* We do not create any outgoing edges for the template. We will
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1119 take care of that in a later traversal. That way we do not
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1120 create edges that are going to just be deleted. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1121 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1122 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1123 {
111
kono
parents: 67
diff changeset
1124 create_block_for_threading (local_info->template_block, rd, 0,
kono
parents: 67
diff changeset
1125 &local_info->duplicate_blocks);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1126
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1127 /* Go ahead and wire up outgoing edges and update PHIs for the duplicate
111
kono
parents: 67
diff changeset
1128 block. */
kono
parents: 67
diff changeset
1129 ssa_fix_duplicate_block_edges (rd, local_info);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1130 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1131
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1132 /* Keep walking the hash table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1133 return 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1134 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1135
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1136 /* We did not create any outgoing edges for the template block during
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1137 block creation. This hash table traversal callback creates the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1138 outgoing edge for the template block. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1139
111
kono
parents: 67
diff changeset
1140 inline int
kono
parents: 67
diff changeset
1141 ssa_fixup_template_block (struct redirection_data **slot,
kono
parents: 67
diff changeset
1142 ssa_local_info_t *local_info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1143 {
111
kono
parents: 67
diff changeset
1144 struct redirection_data *rd = *slot;
kono
parents: 67
diff changeset
1145
kono
parents: 67
diff changeset
1146 /* If this is the template block halt the traversal after updating
kono
parents: 67
diff changeset
1147 it appropriately.
kono
parents: 67
diff changeset
1148
kono
parents: 67
diff changeset
1149 If we were threading through an joiner block, then we want
kono
parents: 67
diff changeset
1150 to keep its control statement and redirect an outgoing edge.
kono
parents: 67
diff changeset
1151 Else we want to remove the control statement & edges, then create
kono
parents: 67
diff changeset
1152 a new outgoing edge. In both cases we may need to update PHIs. */
kono
parents: 67
diff changeset
1153 if (rd->dup_blocks[0] && rd->dup_blocks[0] == local_info->template_block)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1154 {
111
kono
parents: 67
diff changeset
1155 ssa_fix_duplicate_block_edges (rd, local_info);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1156 return 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1157 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1158
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1159 return 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1160 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1161
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1162 /* Hash table traversal callback to redirect each incoming edge
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1163 associated with this hash table element to its new destination. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1164
111
kono
parents: 67
diff changeset
1165 int
kono
parents: 67
diff changeset
1166 ssa_redirect_edges (struct redirection_data **slot,
kono
parents: 67
diff changeset
1167 ssa_local_info_t *local_info)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1168 {
111
kono
parents: 67
diff changeset
1169 struct redirection_data *rd = *slot;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1170 struct el *next, *el;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1171
111
kono
parents: 67
diff changeset
1172 /* Walk over all the incoming edges associated with this hash table
kono
parents: 67
diff changeset
1173 entry. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1174 for (el = rd->incoming_edges; el; el = next)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1175 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1176 edge e = el->e;
111
kono
parents: 67
diff changeset
1177 vec<jump_thread_edge *> *path = THREAD_PATH (e);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1178
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1179 /* Go ahead and free this element from the list. Doing this now
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1180 avoids the need for another list walk when we destroy the hash
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1181 table. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1182 next = el->next;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1183 free (el);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1184
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1185 thread_stats.num_threaded_edges++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1186
111
kono
parents: 67
diff changeset
1187 if (rd->dup_blocks[0])
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1188 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1189 edge e2;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1190
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1191 if (dump_file && (dump_flags & TDF_DETAILS))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1192 fprintf (dump_file, " Threaded jump %d --> %d to %d\n",
111
kono
parents: 67
diff changeset
1193 e->src->index, e->dest->index, rd->dup_blocks[0]->index);
kono
parents: 67
diff changeset
1194
kono
parents: 67
diff changeset
1195 /* Redirect the incoming edge (possibly to the joiner block) to the
kono
parents: 67
diff changeset
1196 appropriate duplicate block. */
kono
parents: 67
diff changeset
1197 e2 = redirect_edge_and_branch (e, rd->dup_blocks[0]);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1198 gcc_assert (e == e2);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1199 flush_pending_stmts (e2);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1200 }
111
kono
parents: 67
diff changeset
1201
kono
parents: 67
diff changeset
1202 /* Go ahead and clear E->aux. It's not needed anymore and failure
kono
parents: 67
diff changeset
1203 to clear it will cause all kinds of unpleasant problems later. */
kono
parents: 67
diff changeset
1204 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
1205 e->aux = NULL;
kono
parents: 67
diff changeset
1206
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1207 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1208
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1209 /* Indicate that we actually threaded one or more jumps. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1210 if (rd->incoming_edges)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1211 local_info->jumps_threaded = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1212
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1213 return 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1214 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1215
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1216 /* Return true if this block has no executable statements other than
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1217 a simple ctrl flow instruction. When the number of outgoing edges
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1218 is one, this is equivalent to a "forwarder" block. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1219
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1220 static bool
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1221 redirection_block_p (basic_block bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1222 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1223 gimple_stmt_iterator gsi;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1224
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1225 /* Advance to the first executable statement. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1226 gsi = gsi_start_bb (bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1227 while (!gsi_end_p (gsi)
111
kono
parents: 67
diff changeset
1228 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1229 || is_gimple_debug (gsi_stmt (gsi))
111
kono
parents: 67
diff changeset
1230 || gimple_nop_p (gsi_stmt (gsi))
kono
parents: 67
diff changeset
1231 || gimple_clobber_p (gsi_stmt (gsi))))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1232 gsi_next (&gsi);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1233
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1234 /* Check if this is an empty block. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1235 if (gsi_end_p (gsi))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1236 return true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1237
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1238 /* Test that we've reached the terminating control statement. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1239 return gsi_stmt (gsi)
111
kono
parents: 67
diff changeset
1240 && (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
kono
parents: 67
diff changeset
1241 || gimple_code (gsi_stmt (gsi)) == GIMPLE_GOTO
kono
parents: 67
diff changeset
1242 || gimple_code (gsi_stmt (gsi)) == GIMPLE_SWITCH);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1243 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1244
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1245 /* BB is a block which ends with a COND_EXPR or SWITCH_EXPR and when BB
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1246 is reached via one or more specific incoming edges, we know which
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1247 outgoing edge from BB will be traversed.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1248
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1249 We want to redirect those incoming edges to the target of the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1250 appropriate outgoing edge. Doing so avoids a conditional branch
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1251 and may expose new optimization opportunities. Note that we have
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1252 to update dominator tree and SSA graph after such changes.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1253
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1254 The key to keeping the SSA graph update manageable is to duplicate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1255 the side effects occurring in BB so that those side effects still
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1256 occur on the paths which bypass BB after redirecting edges.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1257
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1258 We accomplish this by creating duplicates of BB and arranging for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1259 the duplicates to unconditionally pass control to one specific
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1260 successor of BB. We then revector the incoming edges into BB to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1261 the appropriate duplicate of BB.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1262
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1263 If NOLOOP_ONLY is true, we only perform the threading as long as it
111
kono
parents: 67
diff changeset
1264 does not affect the structure of the loops in a nontrivial way.
kono
parents: 67
diff changeset
1265
kono
parents: 67
diff changeset
1266 If JOINERS is true, then thread through joiner blocks as well. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1267
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1268 static bool
111
kono
parents: 67
diff changeset
1269 thread_block_1 (basic_block bb, bool noloop_only, bool joiners)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1270 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1271 /* E is an incoming edge into BB that we may or may not want to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1272 redirect to a duplicate of BB. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1273 edge e, e2;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1274 edge_iterator ei;
111
kono
parents: 67
diff changeset
1275 ssa_local_info_t local_info;
kono
parents: 67
diff changeset
1276
kono
parents: 67
diff changeset
1277 local_info.duplicate_blocks = BITMAP_ALLOC (NULL);
kono
parents: 67
diff changeset
1278 local_info.need_profile_correction = false;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1279
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1280 /* To avoid scanning a linear array for the element we need we instead
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1281 use a hash table. For normal code there should be no noticeable
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1282 difference. However, if we have a block with a large number of
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1283 incoming and outgoing edges such linear searches can get expensive. */
111
kono
parents: 67
diff changeset
1284 redirection_data
kono
parents: 67
diff changeset
1285 = new hash_table<struct redirection_data> (EDGE_COUNT (bb->succs));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1286
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1287 /* Record each unique threaded destination into a hash table for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1288 efficient lookups. */
111
kono
parents: 67
diff changeset
1289 edge last = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1290 FOR_EACH_EDGE (e, ei, bb->preds)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1291 {
111
kono
parents: 67
diff changeset
1292 if (e->aux == NULL)
kono
parents: 67
diff changeset
1293 continue;
kono
parents: 67
diff changeset
1294
kono
parents: 67
diff changeset
1295 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
1296
kono
parents: 67
diff changeset
1297 if (((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && !joiners)
kono
parents: 67
diff changeset
1298 || ((*path)[1]->type == EDGE_COPY_SRC_BLOCK && joiners))
kono
parents: 67
diff changeset
1299 continue;
kono
parents: 67
diff changeset
1300
kono
parents: 67
diff changeset
1301 e2 = path->last ()->e;
kono
parents: 67
diff changeset
1302 if (!e2 || noloop_only)
kono
parents: 67
diff changeset
1303 {
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1304 /* If NOLOOP_ONLY is true, we only allow threading through the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1305 header of a loop to exit edges. */
111
kono
parents: 67
diff changeset
1306
kono
parents: 67
diff changeset
1307 /* One case occurs when there was loop header buried in a jump
kono
parents: 67
diff changeset
1308 threading path that crosses loop boundaries. We do not try
kono
parents: 67
diff changeset
1309 and thread this elsewhere, so just cancel the jump threading
kono
parents: 67
diff changeset
1310 request by clearing the AUX field now. */
kono
parents: 67
diff changeset
1311 if (bb->loop_father != e2->src->loop_father
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1312 && (!loop_exit_edge_p (e2->src->loop_father, e2)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1313 || flow_loop_nested_p (bb->loop_father,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1314 e2->dest->loop_father)))
111
kono
parents: 67
diff changeset
1315 {
kono
parents: 67
diff changeset
1316 /* Since this case is not handled by our special code
kono
parents: 67
diff changeset
1317 to thread through a loop header, we must explicitly
kono
parents: 67
diff changeset
1318 cancel the threading request here. */
kono
parents: 67
diff changeset
1319 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
1320 e->aux = NULL;
kono
parents: 67
diff changeset
1321 continue;
kono
parents: 67
diff changeset
1322 }
kono
parents: 67
diff changeset
1323
kono
parents: 67
diff changeset
1324 /* Another case occurs when trying to thread through our
kono
parents: 67
diff changeset
1325 own loop header, possibly from inside the loop. We will
kono
parents: 67
diff changeset
1326 thread these later. */
kono
parents: 67
diff changeset
1327 unsigned int i;
kono
parents: 67
diff changeset
1328 for (i = 1; i < path->length (); i++)
kono
parents: 67
diff changeset
1329 {
kono
parents: 67
diff changeset
1330 if ((*path)[i]->e->src == bb->loop_father->header
kono
parents: 67
diff changeset
1331 && (!loop_exit_edge_p (bb->loop_father, e2)
kono
parents: 67
diff changeset
1332 || (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK))
kono
parents: 67
diff changeset
1333 break;
kono
parents: 67
diff changeset
1334 }
kono
parents: 67
diff changeset
1335
kono
parents: 67
diff changeset
1336 if (i != path->length ())
kono
parents: 67
diff changeset
1337 continue;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1338
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1339 /* Loop parallelization can be confused by the result of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1340 threading through the loop exit test back into the loop.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1341 However, theading those jumps seems to help other codes.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1342
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1343 I have been unable to find anything related to the shape of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1344 the CFG, the contents of the affected blocks, etc which would
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1345 allow a more sensible test than what we're using below which
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1346 merely avoids the optimization when parallelizing loops. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1347 if (flag_tree_parallelize_loops > 1)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1348 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1349 for (i = 1; i < path->length (); i++)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1350 if (bb->loop_father == e2->src->loop_father
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1351 && loop_exits_from_bb_p (bb->loop_father,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1352 (*path)[i]->e->src)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1353 && !loop_exit_edge_p (bb->loop_father, e2))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1354 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1355
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1356 if (i != path->length ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1357 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1358 delete_jump_thread_path (path);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1359 e->aux = NULL;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1360 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1361 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1362 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1363 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1364
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1365 /* Insert the outgoing edge into the hash table if it is not
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1366 already in the hash table. */
111
kono
parents: 67
diff changeset
1367 lookup_redirection_data (e, INSERT);
kono
parents: 67
diff changeset
1368
kono
parents: 67
diff changeset
1369 /* When we have thread paths through a common joiner with different
kono
parents: 67
diff changeset
1370 final destinations, then we may need corrections to deal with
kono
parents: 67
diff changeset
1371 profile insanities. See the big comment before compute_path_counts. */
kono
parents: 67
diff changeset
1372 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
kono
parents: 67
diff changeset
1373 {
kono
parents: 67
diff changeset
1374 if (!last)
kono
parents: 67
diff changeset
1375 last = e2;
kono
parents: 67
diff changeset
1376 else if (e2 != last)
kono
parents: 67
diff changeset
1377 local_info.need_profile_correction = true;
kono
parents: 67
diff changeset
1378 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1379 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1380
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1381 /* We do not update dominance info. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1382 free_dominance_info (CDI_DOMINATORS);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1383
111
kono
parents: 67
diff changeset
1384 /* We know we only thread through the loop header to loop exits.
kono
parents: 67
diff changeset
1385 Let the basic block duplication hook know we are not creating
kono
parents: 67
diff changeset
1386 a multiple entry loop. */
kono
parents: 67
diff changeset
1387 if (noloop_only
kono
parents: 67
diff changeset
1388 && bb == bb->loop_father->header)
kono
parents: 67
diff changeset
1389 set_loop_copy (bb->loop_father, loop_outer (bb->loop_father));
kono
parents: 67
diff changeset
1390
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1391 /* Now create duplicates of BB.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1392
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1393 Note that for a block with a high outgoing degree we can waste
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1394 a lot of time and memory creating and destroying useless edges.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1395
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1396 So we first duplicate BB and remove the control structure at the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1397 tail of the duplicate as well as all outgoing edges from the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1398 duplicate. We then use that duplicate block as a template for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1399 the rest of the duplicates. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1400 local_info.template_block = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1401 local_info.bb = bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1402 local_info.jumps_threaded = false;
111
kono
parents: 67
diff changeset
1403 redirection_data->traverse <ssa_local_info_t *, ssa_create_duplicates>
kono
parents: 67
diff changeset
1404 (&local_info);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1405
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1406 /* The template does not have an outgoing edge. Create that outgoing
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1407 edge and update PHI nodes as the edge's target as necessary.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1408
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1409 We do this after creating all the duplicates to avoid creating
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1410 unnecessary edges. */
111
kono
parents: 67
diff changeset
1411 redirection_data->traverse <ssa_local_info_t *, ssa_fixup_template_block>
kono
parents: 67
diff changeset
1412 (&local_info);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1413
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1414 /* The hash table traversals above created the duplicate blocks (and the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1415 statements within the duplicate blocks). This loop creates PHI nodes for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1416 the duplicated blocks and redirects the incoming edges into BB to reach
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1417 the duplicates of BB. */
111
kono
parents: 67
diff changeset
1418 redirection_data->traverse <ssa_local_info_t *, ssa_redirect_edges>
kono
parents: 67
diff changeset
1419 (&local_info);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1420
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1421 /* Done with this block. Clear REDIRECTION_DATA. */
111
kono
parents: 67
diff changeset
1422 delete redirection_data;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1423 redirection_data = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1424
111
kono
parents: 67
diff changeset
1425 if (noloop_only
kono
parents: 67
diff changeset
1426 && bb == bb->loop_father->header)
kono
parents: 67
diff changeset
1427 set_loop_copy (bb->loop_father, NULL);
kono
parents: 67
diff changeset
1428
kono
parents: 67
diff changeset
1429 BITMAP_FREE (local_info.duplicate_blocks);
kono
parents: 67
diff changeset
1430 local_info.duplicate_blocks = NULL;
kono
parents: 67
diff changeset
1431
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1432 /* Indicate to our caller whether or not any jumps were threaded. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1433 return local_info.jumps_threaded;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1434 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1435
111
kono
parents: 67
diff changeset
1436 /* Wrapper for thread_block_1 so that we can first handle jump
kono
parents: 67
diff changeset
1437 thread paths which do not involve copying joiner blocks, then
kono
parents: 67
diff changeset
1438 handle jump thread paths which have joiner blocks.
kono
parents: 67
diff changeset
1439
kono
parents: 67
diff changeset
1440 By doing things this way we can be as aggressive as possible and
kono
parents: 67
diff changeset
1441 not worry that copying a joiner block will create a jump threading
kono
parents: 67
diff changeset
1442 opportunity. */
kono
parents: 67
diff changeset
1443
kono
parents: 67
diff changeset
1444 static bool
kono
parents: 67
diff changeset
1445 thread_block (basic_block bb, bool noloop_only)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1446 {
111
kono
parents: 67
diff changeset
1447 bool retval;
kono
parents: 67
diff changeset
1448 retval = thread_block_1 (bb, noloop_only, false);
kono
parents: 67
diff changeset
1449 retval |= thread_block_1 (bb, noloop_only, true);
kono
parents: 67
diff changeset
1450 return retval;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1451 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1452
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1453 /* Callback for dfs_enumerate_from. Returns true if BB is different
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1454 from STOP and DBDS_CE_STOP. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1455
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1456 static basic_block dbds_ce_stop;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1457 static bool
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1458 dbds_continue_enumeration_p (const_basic_block bb, const void *stop)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1459 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1460 return (bb != (const_basic_block) stop
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1461 && bb != dbds_ce_stop);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1462 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1463
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1464 /* Evaluates the dominance relationship of latch of the LOOP and BB, and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1465 returns the state. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1466
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1467 enum bb_dom_status
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1468 determine_bb_domination_status (struct loop *loop, basic_block bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1469 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1470 basic_block *bblocks;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1471 unsigned nblocks, i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1472 bool bb_reachable = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1473 edge_iterator ei;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1474 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1475
111
kono
parents: 67
diff changeset
1476 /* This function assumes BB is a successor of LOOP->header.
kono
parents: 67
diff changeset
1477 If that is not the case return DOMST_NONDOMINATING which
kono
parents: 67
diff changeset
1478 is always safe. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1479 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1480 bool ok = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1481
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1482 FOR_EACH_EDGE (e, ei, bb->preds)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1483 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1484 if (e->src == loop->header)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1485 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1486 ok = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1487 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1488 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1489 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1490
111
kono
parents: 67
diff changeset
1491 if (!ok)
kono
parents: 67
diff changeset
1492 return DOMST_NONDOMINATING;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1493 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1494
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1495 if (bb == loop->latch)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1496 return DOMST_DOMINATING;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1497
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1498 /* Check that BB dominates LOOP->latch, and that it is back-reachable
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1499 from it. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1500
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1501 bblocks = XCNEWVEC (basic_block, loop->num_nodes);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1502 dbds_ce_stop = loop->header;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1503 nblocks = dfs_enumerate_from (loop->latch, 1, dbds_continue_enumeration_p,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1504 bblocks, loop->num_nodes, bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1505 for (i = 0; i < nblocks; i++)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1506 FOR_EACH_EDGE (e, ei, bblocks[i]->preds)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1507 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1508 if (e->src == loop->header)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1509 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1510 free (bblocks);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1511 return DOMST_NONDOMINATING;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1512 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1513 if (e->src == bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1514 bb_reachable = true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1515 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1516
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1517 free (bblocks);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1518 return (bb_reachable ? DOMST_DOMINATING : DOMST_LOOP_BROKEN);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1519 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1520
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1521 /* Thread jumps through the header of LOOP. Returns true if cfg changes.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1522 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading from entry edges
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1523 to the inside of the loop. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1524
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1525 static bool
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1526 thread_through_loop_header (struct loop *loop, bool may_peel_loop_headers)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1527 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1528 basic_block header = loop->header;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1529 edge e, tgt_edge, latch = loop_latch_edge (loop);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1530 edge_iterator ei;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1531 basic_block tgt_bb, atgt_bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1532 enum bb_dom_status domst;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1533
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1534 /* We have already threaded through headers to exits, so all the threading
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1535 requests now are to the inside of the loop. We need to avoid creating
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1536 irreducible regions (i.e., loops with more than one entry block), and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1537 also loop with several latch edges, or new subloops of the loop (although
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1538 there are cases where it might be appropriate, it is difficult to decide,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1539 and doing it wrongly may confuse other optimizers).
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1540
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1541 We could handle more general cases here. However, the intention is to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1542 preserve some information about the loop, which is impossible if its
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1543 structure changes significantly, in a way that is not well understood.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1544 Thus we only handle few important special cases, in which also updating
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1545 of the loop-carried information should be feasible:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1546
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1547 1) Propagation of latch edge to a block that dominates the latch block
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1548 of a loop. This aims to handle the following idiom:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1549
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1550 first = 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1551 while (1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1552 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1553 if (first)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1554 initialize;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1555 first = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1556 body;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1557 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1558
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1559 After threading the latch edge, this becomes
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1560
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1561 first = 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1562 if (first)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1563 initialize;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1564 while (1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1565 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1566 first = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1567 body;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1568 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1569
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1570 The original header of the loop is moved out of it, and we may thread
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1571 the remaining edges through it without further constraints.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1572
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1573 2) All entry edges are propagated to a single basic block that dominates
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1574 the latch block of the loop. This aims to handle the following idiom
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1575 (normally created for "for" loops):
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1576
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1577 i = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1578 while (1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1579 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1580 if (i >= 100)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1581 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1582 body;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1583 i++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1584 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1585
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1586 This becomes
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1587
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1588 i = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1589 while (1)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1590 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1591 body;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1592 i++;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1593 if (i >= 100)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1594 break;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1595 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1596 */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1597
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1598 /* Threading through the header won't improve the code if the header has just
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1599 one successor. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1600 if (single_succ_p (header))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1601 goto fail;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1602
111
kono
parents: 67
diff changeset
1603 if (!may_peel_loop_headers && !redirection_block_p (loop->header))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1604 goto fail;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1605 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1606 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1607 tgt_bb = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1608 tgt_edge = NULL;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1609 FOR_EACH_EDGE (e, ei, header->preds)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1610 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1611 if (!e->aux)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1612 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1613 if (e == latch)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1614 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1615
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1616 /* If latch is not threaded, and there is a header
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1617 edge that is not threaded, we would create loop
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1618 with multiple entries. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1619 goto fail;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1620 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1621
111
kono
parents: 67
diff changeset
1622 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
1623
kono
parents: 67
diff changeset
1624 if ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
kono
parents: 67
diff changeset
1625 goto fail;
kono
parents: 67
diff changeset
1626 tgt_edge = (*path)[1]->e;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1627 atgt_bb = tgt_edge->dest;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1628 if (!tgt_bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1629 tgt_bb = atgt_bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1630 /* Two targets of threading would make us create loop
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1631 with multiple entries. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1632 else if (tgt_bb != atgt_bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1633 goto fail;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1634 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1635
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1636 if (!tgt_bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1637 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1638 /* There are no threading requests. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1639 return false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1640 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1641
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1642 /* Redirecting to empty loop latch is useless. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1643 if (tgt_bb == loop->latch
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1644 && empty_block_p (loop->latch))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1645 goto fail;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1646 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1647
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1648 /* The target block must dominate the loop latch, otherwise we would be
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1649 creating a subloop. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1650 domst = determine_bb_domination_status (loop, tgt_bb);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1651 if (domst == DOMST_NONDOMINATING)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1652 goto fail;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1653 if (domst == DOMST_LOOP_BROKEN)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1654 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1655 /* If the loop ceased to exist, mark it as such, and thread through its
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1656 original header. */
111
kono
parents: 67
diff changeset
1657 mark_loop_for_removal (loop);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1658 return thread_block (header, false);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1659 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1660
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1661 if (tgt_bb->loop_father->header == tgt_bb)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1662 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1663 /* If the target of the threading is a header of a subloop, we need
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1664 to create a preheader for it, so that the headers of the two loops
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1665 do not merge. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1666 if (EDGE_COUNT (tgt_bb->preds) > 2)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1667 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1668 tgt_bb = create_preheader (tgt_bb->loop_father, 0);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1669 gcc_assert (tgt_bb != NULL);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1670 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1671 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1672 tgt_bb = split_edge (tgt_edge);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1673 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1674
111
kono
parents: 67
diff changeset
1675 basic_block new_preheader;
kono
parents: 67
diff changeset
1676
kono
parents: 67
diff changeset
1677 /* Now consider the case entry edges are redirected to the new entry
kono
parents: 67
diff changeset
1678 block. Remember one entry edge, so that we can find the new
kono
parents: 67
diff changeset
1679 preheader (its destination after threading). */
kono
parents: 67
diff changeset
1680 FOR_EACH_EDGE (e, ei, header->preds)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1681 {
111
kono
parents: 67
diff changeset
1682 if (e->aux)
kono
parents: 67
diff changeset
1683 break;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1684 }
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
1685
111
kono
parents: 67
diff changeset
1686 /* The duplicate of the header is the new preheader of the loop. Ensure
kono
parents: 67
diff changeset
1687 that it is placed correctly in the loop hierarchy. */
kono
parents: 67
diff changeset
1688 set_loop_copy (loop, loop_outer (loop));
kono
parents: 67
diff changeset
1689
kono
parents: 67
diff changeset
1690 thread_block (header, false);
kono
parents: 67
diff changeset
1691 set_loop_copy (loop, NULL);
kono
parents: 67
diff changeset
1692 new_preheader = e->dest;
kono
parents: 67
diff changeset
1693
kono
parents: 67
diff changeset
1694 /* Create the new latch block. This is always necessary, as the latch
kono
parents: 67
diff changeset
1695 must have only a single successor, but the original header had at
kono
parents: 67
diff changeset
1696 least two successors. */
kono
parents: 67
diff changeset
1697 loop->latch = NULL;
kono
parents: 67
diff changeset
1698 mfb_kj_edge = single_succ_edge (new_preheader);
kono
parents: 67
diff changeset
1699 loop->header = mfb_kj_edge->dest;
kono
parents: 67
diff changeset
1700 latch = make_forwarder_block (tgt_bb, mfb_keep_just, NULL);
kono
parents: 67
diff changeset
1701 loop->header = latch->dest;
kono
parents: 67
diff changeset
1702 loop->latch = latch->src;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1703 return true;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1704
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1705 fail:
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1706 /* We failed to thread anything. Cancel the requests. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1707 FOR_EACH_EDGE (e, ei, header->preds)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1708 {
111
kono
parents: 67
diff changeset
1709 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
1710
kono
parents: 67
diff changeset
1711 if (path)
kono
parents: 67
diff changeset
1712 {
kono
parents: 67
diff changeset
1713 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
1714 e->aux = NULL;
kono
parents: 67
diff changeset
1715 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1716 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1717 return false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1718 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1719
111
kono
parents: 67
diff changeset
1720 /* E1 and E2 are edges into the same basic block. Return TRUE if the
kono
parents: 67
diff changeset
1721 PHI arguments associated with those edges are equal or there are no
kono
parents: 67
diff changeset
1722 PHI arguments, otherwise return FALSE. */
kono
parents: 67
diff changeset
1723
kono
parents: 67
diff changeset
1724 static bool
kono
parents: 67
diff changeset
1725 phi_args_equal_on_edges (edge e1, edge e2)
kono
parents: 67
diff changeset
1726 {
kono
parents: 67
diff changeset
1727 gphi_iterator gsi;
kono
parents: 67
diff changeset
1728 int indx1 = e1->dest_idx;
kono
parents: 67
diff changeset
1729 int indx2 = e2->dest_idx;
kono
parents: 67
diff changeset
1730
kono
parents: 67
diff changeset
1731 for (gsi = gsi_start_phis (e1->dest); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents: 67
diff changeset
1732 {
kono
parents: 67
diff changeset
1733 gphi *phi = gsi.phi ();
kono
parents: 67
diff changeset
1734
kono
parents: 67
diff changeset
1735 if (!operand_equal_p (gimple_phi_arg_def (phi, indx1),
kono
parents: 67
diff changeset
1736 gimple_phi_arg_def (phi, indx2), 0))
kono
parents: 67
diff changeset
1737 return false;
kono
parents: 67
diff changeset
1738 }
kono
parents: 67
diff changeset
1739 return true;
kono
parents: 67
diff changeset
1740 }
kono
parents: 67
diff changeset
1741
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1742 /* Return the number of non-debug statements and non-virtual PHIs in a
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1743 block. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1744
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1745 static unsigned int
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1746 count_stmts_and_phis_in_block (basic_block bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1747 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1748 unsigned int num_stmts = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1749
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1750 gphi_iterator gpi;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1751 for (gpi = gsi_start_phis (bb); !gsi_end_p (gpi); gsi_next (&gpi))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1752 if (!virtual_operand_p (PHI_RESULT (gpi.phi ())))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1753 num_stmts++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1754
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1755 gimple_stmt_iterator gsi;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1756 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1757 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1758 gimple *stmt = gsi_stmt (gsi);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1759 if (!is_gimple_debug (stmt))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1760 num_stmts++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1761 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1762
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1763 return num_stmts;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1764 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1765
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1766
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1767 /* Walk through the registered jump threads and convert them into a
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1768 form convenient for this pass.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1769
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1770 Any block which has incoming edges threaded to outgoing edges
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1771 will have its entry in THREADED_BLOCK set.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1772
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1773 Any threaded edge will have its new outgoing edge stored in the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1774 original edge's AUX field.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1775
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1776 This form avoids the need to walk all the edges in the CFG to
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1777 discover blocks which need processing and avoids unnecessary
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1778 hash table lookups to map from threaded edge to new target. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1779
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1780 static void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1781 mark_threaded_blocks (bitmap threaded_blocks)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1782 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1783 unsigned int i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1784 bitmap_iterator bi;
111
kono
parents: 67
diff changeset
1785 auto_bitmap tmp;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1786 basic_block bb;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1787 edge e;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1788 edge_iterator ei;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1789
111
kono
parents: 67
diff changeset
1790 /* It is possible to have jump threads in which one is a subpath
kono
parents: 67
diff changeset
1791 of the other. ie, (A, B), (B, C), (C, D) where B is a joiner
kono
parents: 67
diff changeset
1792 block and (B, C), (C, D) where no joiner block exists.
kono
parents: 67
diff changeset
1793
kono
parents: 67
diff changeset
1794 When this occurs ignore the jump thread request with the joiner
kono
parents: 67
diff changeset
1795 block. It's totally subsumed by the simpler jump thread request.
kono
parents: 67
diff changeset
1796
kono
parents: 67
diff changeset
1797 This results in less block copying, simpler CFGs. More importantly,
kono
parents: 67
diff changeset
1798 when we duplicate the joiner block, B, in this case we will create
kono
parents: 67
diff changeset
1799 a new threading opportunity that we wouldn't be able to optimize
kono
parents: 67
diff changeset
1800 until the next jump threading iteration.
kono
parents: 67
diff changeset
1801
kono
parents: 67
diff changeset
1802 So first convert the jump thread requests which do not require a
kono
parents: 67
diff changeset
1803 joiner block. */
kono
parents: 67
diff changeset
1804 for (i = 0; i < paths.length (); i++)
kono
parents: 67
diff changeset
1805 {
kono
parents: 67
diff changeset
1806 vec<jump_thread_edge *> *path = paths[i];
kono
parents: 67
diff changeset
1807
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1808 if (path->length () > 1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1809 && (*path)[1]->type != EDGE_COPY_SRC_JOINER_BLOCK)
111
kono
parents: 67
diff changeset
1810 {
kono
parents: 67
diff changeset
1811 edge e = (*path)[0]->e;
kono
parents: 67
diff changeset
1812 e->aux = (void *)path;
kono
parents: 67
diff changeset
1813 bitmap_set_bit (tmp, e->dest->index);
kono
parents: 67
diff changeset
1814 }
kono
parents: 67
diff changeset
1815 }
kono
parents: 67
diff changeset
1816
kono
parents: 67
diff changeset
1817 /* Now iterate again, converting cases where we want to thread
kono
parents: 67
diff changeset
1818 through a joiner block, but only if no other edge on the path
kono
parents: 67
diff changeset
1819 already has a jump thread attached to it. We do this in two passes,
kono
parents: 67
diff changeset
1820 to avoid situations where the order in the paths vec can hide overlapping
kono
parents: 67
diff changeset
1821 threads (the path is recorded on the incoming edge, so we would miss
kono
parents: 67
diff changeset
1822 cases where the second path starts at a downstream edge on the same
kono
parents: 67
diff changeset
1823 path). First record all joiner paths, deleting any in the unexpected
kono
parents: 67
diff changeset
1824 case where there is already a path for that incoming edge. */
kono
parents: 67
diff changeset
1825 for (i = 0; i < paths.length ();)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1826 {
111
kono
parents: 67
diff changeset
1827 vec<jump_thread_edge *> *path = paths[i];
kono
parents: 67
diff changeset
1828
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1829 if (path->length () > 1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1830 && (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK)
111
kono
parents: 67
diff changeset
1831 {
kono
parents: 67
diff changeset
1832 /* Attach the path to the starting edge if none is yet recorded. */
kono
parents: 67
diff changeset
1833 if ((*path)[0]->e->aux == NULL)
kono
parents: 67
diff changeset
1834 {
kono
parents: 67
diff changeset
1835 (*path)[0]->e->aux = path;
kono
parents: 67
diff changeset
1836 i++;
kono
parents: 67
diff changeset
1837 }
kono
parents: 67
diff changeset
1838 else
kono
parents: 67
diff changeset
1839 {
kono
parents: 67
diff changeset
1840 paths.unordered_remove (i);
kono
parents: 67
diff changeset
1841 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents: 67
diff changeset
1842 dump_jump_thread_path (dump_file, *path, false);
kono
parents: 67
diff changeset
1843 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
1844 }
kono
parents: 67
diff changeset
1845 }
kono
parents: 67
diff changeset
1846 else
kono
parents: 67
diff changeset
1847 {
kono
parents: 67
diff changeset
1848 i++;
kono
parents: 67
diff changeset
1849 }
kono
parents: 67
diff changeset
1850 }
kono
parents: 67
diff changeset
1851
kono
parents: 67
diff changeset
1852 /* Second, look for paths that have any other jump thread attached to
kono
parents: 67
diff changeset
1853 them, and either finish converting them or cancel them. */
kono
parents: 67
diff changeset
1854 for (i = 0; i < paths.length ();)
kono
parents: 67
diff changeset
1855 {
kono
parents: 67
diff changeset
1856 vec<jump_thread_edge *> *path = paths[i];
kono
parents: 67
diff changeset
1857 edge e = (*path)[0]->e;
kono
parents: 67
diff changeset
1858
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1859 if (path->length () > 1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1860 && (*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK && e->aux == path)
111
kono
parents: 67
diff changeset
1861 {
kono
parents: 67
diff changeset
1862 unsigned int j;
kono
parents: 67
diff changeset
1863 for (j = 1; j < path->length (); j++)
kono
parents: 67
diff changeset
1864 if ((*path)[j]->e->aux != NULL)
kono
parents: 67
diff changeset
1865 break;
kono
parents: 67
diff changeset
1866
kono
parents: 67
diff changeset
1867 /* If we iterated through the entire path without exiting the loop,
kono
parents: 67
diff changeset
1868 then we are good to go, record it. */
kono
parents: 67
diff changeset
1869 if (j == path->length ())
kono
parents: 67
diff changeset
1870 {
kono
parents: 67
diff changeset
1871 bitmap_set_bit (tmp, e->dest->index);
kono
parents: 67
diff changeset
1872 i++;
kono
parents: 67
diff changeset
1873 }
kono
parents: 67
diff changeset
1874 else
kono
parents: 67
diff changeset
1875 {
kono
parents: 67
diff changeset
1876 e->aux = NULL;
kono
parents: 67
diff changeset
1877 paths.unordered_remove (i);
kono
parents: 67
diff changeset
1878 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents: 67
diff changeset
1879 dump_jump_thread_path (dump_file, *path, false);
kono
parents: 67
diff changeset
1880 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
1881 }
kono
parents: 67
diff changeset
1882 }
kono
parents: 67
diff changeset
1883 else
kono
parents: 67
diff changeset
1884 {
kono
parents: 67
diff changeset
1885 i++;
kono
parents: 67
diff changeset
1886 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1887 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1888
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1889 /* When optimizing for size, prune all thread paths where statement
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1890 duplication is necessary.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1891
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1892 We walk the jump thread path looking for copied blocks. There's
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1893 two types of copied blocks.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1894
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1895 EDGE_COPY_SRC_JOINER_BLOCK is always copied and thus we will
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1896 cancel the jump threading request when optimizing for size.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1897
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1898 EDGE_COPY_SRC_BLOCK which is copied, but some of its statements
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1899 will be killed by threading. If threading does not kill all of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1900 its statements, then we should cancel the jump threading request
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1901 when optimizing for size. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1902 if (optimize_function_for_size_p (cfun))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1903 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1904 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1905 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1906 FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (cfun, i)->preds)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1907 if (e->aux)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1908 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1909 vec<jump_thread_edge *> *path = THREAD_PATH (e);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1910
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1911 unsigned int j;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1912 for (j = 1; j < path->length (); j++)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1913 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1914 bb = (*path)[j]->e->src;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1915 if (redirection_block_p (bb))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1916 ;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1917 else if ((*path)[j]->type == EDGE_COPY_SRC_JOINER_BLOCK
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1918 || ((*path)[j]->type == EDGE_COPY_SRC_BLOCK
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1919 && (count_stmts_and_phis_in_block (bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1920 != estimate_threading_killed_stmts (bb))))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1921 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1922 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1923
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1924 if (j != path->length ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1925 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1926 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1927 dump_jump_thread_path (dump_file, *path, 0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1928 delete_jump_thread_path (path);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1929 e->aux = NULL;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1930 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1931 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1932 bitmap_set_bit (threaded_blocks, i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1933 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1934 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1935 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1936 else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1937 bitmap_copy (threaded_blocks, tmp);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1938
111
kono
parents: 67
diff changeset
1939 /* If we have a joiner block (J) which has two successors S1 and S2 and
kono
parents: 67
diff changeset
1940 we are threading though S1 and the final destination of the thread
kono
parents: 67
diff changeset
1941 is S2, then we must verify that any PHI nodes in S2 have the same
kono
parents: 67
diff changeset
1942 PHI arguments for the edge J->S2 and J->S1->...->S2.
kono
parents: 67
diff changeset
1943
kono
parents: 67
diff changeset
1944 We used to detect this prior to registering the jump thread, but
kono
parents: 67
diff changeset
1945 that prohibits propagation of edge equivalences into non-dominated
kono
parents: 67
diff changeset
1946 PHI nodes as the equivalency test might occur before propagation.
kono
parents: 67
diff changeset
1947
kono
parents: 67
diff changeset
1948 This must also occur after we truncate any jump threading paths
kono
parents: 67
diff changeset
1949 as this scenario may only show up after truncation.
kono
parents: 67
diff changeset
1950
kono
parents: 67
diff changeset
1951 This works for now, but will need improvement as part of the FSA
kono
parents: 67
diff changeset
1952 optimization.
kono
parents: 67
diff changeset
1953
kono
parents: 67
diff changeset
1954 Note since we've moved the thread request data to the edges,
kono
parents: 67
diff changeset
1955 we have to iterate on those rather than the threaded_edges vector. */
kono
parents: 67
diff changeset
1956 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
kono
parents: 67
diff changeset
1957 {
kono
parents: 67
diff changeset
1958 bb = BASIC_BLOCK_FOR_FN (cfun, i);
kono
parents: 67
diff changeset
1959 FOR_EACH_EDGE (e, ei, bb->preds)
kono
parents: 67
diff changeset
1960 {
kono
parents: 67
diff changeset
1961 if (e->aux)
kono
parents: 67
diff changeset
1962 {
kono
parents: 67
diff changeset
1963 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
1964 bool have_joiner = ((*path)[1]->type == EDGE_COPY_SRC_JOINER_BLOCK);
kono
parents: 67
diff changeset
1965
kono
parents: 67
diff changeset
1966 if (have_joiner)
kono
parents: 67
diff changeset
1967 {
kono
parents: 67
diff changeset
1968 basic_block joiner = e->dest;
kono
parents: 67
diff changeset
1969 edge final_edge = path->last ()->e;
kono
parents: 67
diff changeset
1970 basic_block final_dest = final_edge->dest;
kono
parents: 67
diff changeset
1971 edge e2 = find_edge (joiner, final_dest);
kono
parents: 67
diff changeset
1972
kono
parents: 67
diff changeset
1973 if (e2 && !phi_args_equal_on_edges (e2, final_edge))
kono
parents: 67
diff changeset
1974 {
kono
parents: 67
diff changeset
1975 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
1976 e->aux = NULL;
kono
parents: 67
diff changeset
1977 }
kono
parents: 67
diff changeset
1978 }
kono
parents: 67
diff changeset
1979 }
kono
parents: 67
diff changeset
1980 }
kono
parents: 67
diff changeset
1981 }
kono
parents: 67
diff changeset
1982
kono
parents: 67
diff changeset
1983 /* Look for jump threading paths which cross multiple loop headers.
kono
parents: 67
diff changeset
1984
kono
parents: 67
diff changeset
1985 The code to thread through loop headers will change the CFG in ways
kono
parents: 67
diff changeset
1986 that invalidate the cached loop iteration information. So we must
kono
parents: 67
diff changeset
1987 detect that case and wipe the cached information. */
kono
parents: 67
diff changeset
1988 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, bi)
kono
parents: 67
diff changeset
1989 {
kono
parents: 67
diff changeset
1990 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
kono
parents: 67
diff changeset
1991 FOR_EACH_EDGE (e, ei, bb->preds)
kono
parents: 67
diff changeset
1992 {
kono
parents: 67
diff changeset
1993 if (e->aux)
kono
parents: 67
diff changeset
1994 {
kono
parents: 67
diff changeset
1995 vec<jump_thread_edge *> *path = THREAD_PATH (e);
kono
parents: 67
diff changeset
1996
kono
parents: 67
diff changeset
1997 for (unsigned int i = 0, crossed_headers = 0;
kono
parents: 67
diff changeset
1998 i < path->length ();
kono
parents: 67
diff changeset
1999 i++)
kono
parents: 67
diff changeset
2000 {
kono
parents: 67
diff changeset
2001 basic_block dest = (*path)[i]->e->dest;
kono
parents: 67
diff changeset
2002 basic_block src = (*path)[i]->e->src;
kono
parents: 67
diff changeset
2003 /* If we enter a loop. */
kono
parents: 67
diff changeset
2004 if (flow_loop_nested_p (src->loop_father, dest->loop_father))
kono
parents: 67
diff changeset
2005 ++crossed_headers;
kono
parents: 67
diff changeset
2006 /* If we step from a block outside an irreducible region
kono
parents: 67
diff changeset
2007 to a block inside an irreducible region, then we have
kono
parents: 67
diff changeset
2008 crossed into a loop. */
kono
parents: 67
diff changeset
2009 else if (! (src->flags & BB_IRREDUCIBLE_LOOP)
kono
parents: 67
diff changeset
2010 && (dest->flags & BB_IRREDUCIBLE_LOOP))
kono
parents: 67
diff changeset
2011 ++crossed_headers;
kono
parents: 67
diff changeset
2012 if (crossed_headers > 1)
kono
parents: 67
diff changeset
2013 {
kono
parents: 67
diff changeset
2014 vect_free_loop_info_assumptions
kono
parents: 67
diff changeset
2015 ((*path)[path->length () - 1]->e->dest->loop_father);
kono
parents: 67
diff changeset
2016 break;
kono
parents: 67
diff changeset
2017 }
kono
parents: 67
diff changeset
2018 }
kono
parents: 67
diff changeset
2019 }
kono
parents: 67
diff changeset
2020 }
kono
parents: 67
diff changeset
2021 }
kono
parents: 67
diff changeset
2022 }
kono
parents: 67
diff changeset
2023
kono
parents: 67
diff changeset
2024
kono
parents: 67
diff changeset
2025 /* Verify that the REGION is a valid jump thread. A jump thread is a special
kono
parents: 67
diff changeset
2026 case of SEME Single Entry Multiple Exits region in which all nodes in the
kono
parents: 67
diff changeset
2027 REGION have exactly one incoming edge. The only exception is the first block
kono
parents: 67
diff changeset
2028 that may not have been connected to the rest of the cfg yet. */
kono
parents: 67
diff changeset
2029
kono
parents: 67
diff changeset
2030 DEBUG_FUNCTION void
kono
parents: 67
diff changeset
2031 verify_jump_thread (basic_block *region, unsigned n_region)
kono
parents: 67
diff changeset
2032 {
kono
parents: 67
diff changeset
2033 for (unsigned i = 0; i < n_region; i++)
kono
parents: 67
diff changeset
2034 gcc_assert (EDGE_COUNT (region[i]->preds) <= 1);
kono
parents: 67
diff changeset
2035 }
kono
parents: 67
diff changeset
2036
kono
parents: 67
diff changeset
2037 /* Return true when BB is one of the first N items in BBS. */
kono
parents: 67
diff changeset
2038
kono
parents: 67
diff changeset
2039 static inline bool
kono
parents: 67
diff changeset
2040 bb_in_bbs (basic_block bb, basic_block *bbs, int n)
kono
parents: 67
diff changeset
2041 {
kono
parents: 67
diff changeset
2042 for (int i = 0; i < n; i++)
kono
parents: 67
diff changeset
2043 if (bb == bbs[i])
kono
parents: 67
diff changeset
2044 return true;
kono
parents: 67
diff changeset
2045
kono
parents: 67
diff changeset
2046 return false;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2047 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2048
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2049 DEBUG_FUNCTION void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2050 debug_path (FILE *dump_file, int pathno)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2051 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2052 vec<jump_thread_edge *> *p = paths[pathno];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2053 fprintf (dump_file, "path: ");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2054 for (unsigned i = 0; i < p->length (); ++i)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2055 fprintf (dump_file, "%d -> %d, ",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2056 (*p)[i]->e->src->index, (*p)[i]->e->dest->index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2057 fprintf (dump_file, "\n");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2058 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2059
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2060 DEBUG_FUNCTION void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2061 debug_all_paths ()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2062 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2063 for (unsigned i = 0; i < paths.length (); ++i)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2064 debug_path (stderr, i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2065 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2066
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2067 /* Rewire a jump_thread_edge so that the source block is now a
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2068 threaded source block.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2069
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2070 PATH_NUM is an index into the global path table PATHS.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2071 EDGE_NUM is the jump thread edge number into said path.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2072
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2073 Returns TRUE if we were able to successfully rewire the edge. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2074
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2075 static bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2076 rewire_first_differing_edge (unsigned path_num, unsigned edge_num)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2077 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2078 vec<jump_thread_edge *> *path = paths[path_num];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2079 edge &e = (*path)[edge_num]->e;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2080 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2081 fprintf (dump_file, "rewiring edge candidate: %d -> %d\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2082 e->src->index, e->dest->index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2083 basic_block src_copy = get_bb_copy (e->src);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2084 if (src_copy == NULL)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2085 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2086 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2087 fprintf (dump_file, "ignoring candidate: there is no src COPY\n");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2088 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2089 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2090 edge new_edge = find_edge (src_copy, e->dest);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2091 /* If the previously threaded paths created a flow graph where we
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2092 can no longer figure out where to go, give up. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2093 if (new_edge == NULL)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2094 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2095 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2096 fprintf (dump_file, "ignoring candidate: we lost our way\n");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2097 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2098 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2099 e = new_edge;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2100 return true;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2101 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2102
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2103 /* After an FSM path has been jump threaded, adjust the remaining FSM
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2104 paths that are subsets of this path, so these paths can be safely
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2105 threaded within the context of the new threaded path.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2106
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2107 For example, suppose we have just threaded:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2108
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2109 5 -> 6 -> 7 -> 8 -> 12 => 5 -> 6' -> 7' -> 8' -> 12'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2110
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2111 And we have an upcoming threading candidate:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2112 5 -> 6 -> 7 -> 8 -> 15 -> 20
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2113
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2114 This function adjusts the upcoming path into:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2115 8' -> 15 -> 20
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2116
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2117 CURR_PATH_NUM is an index into the global paths table. It
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2118 specifies the path that was just threaded. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2119
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2120 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2121 adjust_paths_after_duplication (unsigned curr_path_num)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2122 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2123 vec<jump_thread_edge *> *curr_path = paths[curr_path_num];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2124 gcc_assert ((*curr_path)[0]->type == EDGE_FSM_THREAD);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2125
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2126 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2127 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2128 fprintf (dump_file, "just threaded: ");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2129 debug_path (dump_file, curr_path_num);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2130 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2132 /* Iterate through all the other paths and adjust them. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2133 for (unsigned cand_path_num = 0; cand_path_num < paths.length (); )
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2134 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2135 if (cand_path_num == curr_path_num)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2136 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2137 ++cand_path_num;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2138 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2139 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2140 /* Make sure the candidate to adjust starts with the same path
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2141 as the recently threaded path and is an FSM thread. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2142 vec<jump_thread_edge *> *cand_path = paths[cand_path_num];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2143 if ((*cand_path)[0]->type != EDGE_FSM_THREAD
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2144 || (*cand_path)[0]->e != (*curr_path)[0]->e)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2145 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2146 ++cand_path_num;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2147 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2148 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2149 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2150 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2151 fprintf (dump_file, "adjusting candidate: ");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2152 debug_path (dump_file, cand_path_num);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2153 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2154
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2155 /* Chop off from the candidate path any prefix it shares with
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2156 the recently threaded path. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2157 unsigned minlength = MIN (curr_path->length (), cand_path->length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2158 unsigned j;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2159 for (j = 0; j < minlength; ++j)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2160 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2161 edge cand_edge = (*cand_path)[j]->e;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2162 edge curr_edge = (*curr_path)[j]->e;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2163
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2164 /* Once the prefix no longer matches, adjust the first
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2165 non-matching edge to point from an adjusted edge to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2166 wherever it was going. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2167 if (cand_edge != curr_edge)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2168 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2169 gcc_assert (cand_edge->src == curr_edge->src);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2170 if (!rewire_first_differing_edge (cand_path_num, j))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2171 goto remove_candidate_from_list;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2172 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2173 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2174 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2175 if (j == minlength)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2176 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2177 /* If we consumed the max subgraph we could look at, and
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2178 still didn't find any different edges, it's the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2179 last edge after MINLENGTH. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2180 if (cand_path->length () > minlength)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2181 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2182 if (!rewire_first_differing_edge (cand_path_num, j))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2183 goto remove_candidate_from_list;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2184 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2185 else if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2186 fprintf (dump_file, "adjusting first edge after MINLENGTH.\n");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2187 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2188 if (j > 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2189 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2190 /* If we are removing everything, delete the entire candidate. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2191 if (j == cand_path->length ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2192 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2193 remove_candidate_from_list:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2194 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2195 fprintf (dump_file, "adjusted candidate: [EMPTY]\n");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2196 delete_jump_thread_path (cand_path);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2197 paths.unordered_remove (cand_path_num);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2198 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2199 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2200 /* Otherwise, just remove the redundant sub-path. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2201 cand_path->block_remove (0, j);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2202 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2203 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2204 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2205 fprintf (dump_file, "adjusted candidate: ");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2206 debug_path (dump_file, cand_path_num);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2207 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2208 ++cand_path_num;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2209 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2210 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2211
111
kono
parents: 67
diff changeset
2212 /* Duplicates a jump-thread path of N_REGION basic blocks.
kono
parents: 67
diff changeset
2213 The ENTRY edge is redirected to the duplicate of the region.
kono
parents: 67
diff changeset
2214
kono
parents: 67
diff changeset
2215 Remove the last conditional statement in the last basic block in the REGION,
kono
parents: 67
diff changeset
2216 and create a single fallthru edge pointing to the same destination as the
kono
parents: 67
diff changeset
2217 EXIT edge.
kono
parents: 67
diff changeset
2218
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2219 CURRENT_PATH_NO is an index into the global paths[] table
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2220 specifying the jump-thread path.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2221
111
kono
parents: 67
diff changeset
2222 Returns false if it is unable to copy the region, true otherwise. */
kono
parents: 67
diff changeset
2223
kono
parents: 67
diff changeset
2224 static bool
kono
parents: 67
diff changeset
2225 duplicate_thread_path (edge entry, edge exit, basic_block *region,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2226 unsigned n_region, unsigned current_path_no)
111
kono
parents: 67
diff changeset
2227 {
kono
parents: 67
diff changeset
2228 unsigned i;
kono
parents: 67
diff changeset
2229 struct loop *loop = entry->dest->loop_father;
kono
parents: 67
diff changeset
2230 edge exit_copy;
kono
parents: 67
diff changeset
2231 edge redirected;
kono
parents: 67
diff changeset
2232 profile_count curr_count;
kono
parents: 67
diff changeset
2233
kono
parents: 67
diff changeset
2234 if (!can_copy_bbs_p (region, n_region))
kono
parents: 67
diff changeset
2235 return false;
kono
parents: 67
diff changeset
2236
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2237 if (dump_file && (dump_flags & TDF_DETAILS))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2238 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2239 fprintf (dump_file, "\nabout to thread: ");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2240 debug_path (dump_file, current_path_no);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2241 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2242
111
kono
parents: 67
diff changeset
2243 /* Some sanity checking. Note that we do not check for all possible
kono
parents: 67
diff changeset
2244 missuses of the functions. I.e. if you ask to copy something weird,
kono
parents: 67
diff changeset
2245 it will work, but the state of structures probably will not be
kono
parents: 67
diff changeset
2246 correct. */
kono
parents: 67
diff changeset
2247 for (i = 0; i < n_region; i++)
kono
parents: 67
diff changeset
2248 {
kono
parents: 67
diff changeset
2249 /* We do not handle subloops, i.e. all the blocks must belong to the
kono
parents: 67
diff changeset
2250 same loop. */
kono
parents: 67
diff changeset
2251 if (region[i]->loop_father != loop)
kono
parents: 67
diff changeset
2252 return false;
kono
parents: 67
diff changeset
2253 }
kono
parents: 67
diff changeset
2254
kono
parents: 67
diff changeset
2255 initialize_original_copy_tables ();
kono
parents: 67
diff changeset
2256
kono
parents: 67
diff changeset
2257 set_loop_copy (loop, loop);
kono
parents: 67
diff changeset
2258
kono
parents: 67
diff changeset
2259 basic_block *region_copy = XNEWVEC (basic_block, n_region);
kono
parents: 67
diff changeset
2260 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
kono
parents: 67
diff changeset
2261 split_edge_bb_loc (entry), false);
kono
parents: 67
diff changeset
2262
kono
parents: 67
diff changeset
2263 /* Fix up: copy_bbs redirects all edges pointing to copied blocks. The
kono
parents: 67
diff changeset
2264 following code ensures that all the edges exiting the jump-thread path are
kono
parents: 67
diff changeset
2265 redirected back to the original code: these edges are exceptions
kono
parents: 67
diff changeset
2266 invalidating the property that is propagated by executing all the blocks of
kono
parents: 67
diff changeset
2267 the jump-thread path in order. */
kono
parents: 67
diff changeset
2268
kono
parents: 67
diff changeset
2269 curr_count = entry->count ();
kono
parents: 67
diff changeset
2270
kono
parents: 67
diff changeset
2271 for (i = 0; i < n_region; i++)
kono
parents: 67
diff changeset
2272 {
kono
parents: 67
diff changeset
2273 edge e;
kono
parents: 67
diff changeset
2274 edge_iterator ei;
kono
parents: 67
diff changeset
2275 basic_block bb = region_copy[i];
kono
parents: 67
diff changeset
2276
kono
parents: 67
diff changeset
2277 /* Watch inconsistent profile. */
kono
parents: 67
diff changeset
2278 if (curr_count > region[i]->count)
kono
parents: 67
diff changeset
2279 curr_count = region[i]->count;
kono
parents: 67
diff changeset
2280 /* Scale current BB. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2281 if (region[i]->count.nonzero_p () && curr_count.initialized_p ())
111
kono
parents: 67
diff changeset
2282 {
kono
parents: 67
diff changeset
2283 /* In the middle of the path we only scale the frequencies.
kono
parents: 67
diff changeset
2284 In last BB we need to update probabilities of outgoing edges
kono
parents: 67
diff changeset
2285 because we know which one is taken at the threaded path. */
kono
parents: 67
diff changeset
2286 if (i + 1 != n_region)
kono
parents: 67
diff changeset
2287 scale_bbs_frequencies_profile_count (region + i, 1,
kono
parents: 67
diff changeset
2288 region[i]->count - curr_count,
kono
parents: 67
diff changeset
2289 region[i]->count);
kono
parents: 67
diff changeset
2290 else
kono
parents: 67
diff changeset
2291 update_bb_profile_for_threading (region[i],
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2292 curr_count,
111
kono
parents: 67
diff changeset
2293 exit);
kono
parents: 67
diff changeset
2294 scale_bbs_frequencies_profile_count (region_copy + i, 1, curr_count,
kono
parents: 67
diff changeset
2295 region_copy[i]->count);
kono
parents: 67
diff changeset
2296 }
kono
parents: 67
diff changeset
2297
kono
parents: 67
diff changeset
2298 if (single_succ_p (bb))
kono
parents: 67
diff changeset
2299 {
kono
parents: 67
diff changeset
2300 /* Make sure the successor is the next node in the path. */
kono
parents: 67
diff changeset
2301 gcc_assert (i + 1 == n_region
kono
parents: 67
diff changeset
2302 || region_copy[i + 1] == single_succ_edge (bb)->dest);
kono
parents: 67
diff changeset
2303 if (i + 1 != n_region)
kono
parents: 67
diff changeset
2304 {
kono
parents: 67
diff changeset
2305 curr_count = single_succ_edge (bb)->count ();
kono
parents: 67
diff changeset
2306 }
kono
parents: 67
diff changeset
2307 continue;
kono
parents: 67
diff changeset
2308 }
kono
parents: 67
diff changeset
2309
kono
parents: 67
diff changeset
2310 /* Special case the last block on the path: make sure that it does not
kono
parents: 67
diff changeset
2311 jump back on the copied path, including back to itself. */
kono
parents: 67
diff changeset
2312 if (i + 1 == n_region)
kono
parents: 67
diff changeset
2313 {
kono
parents: 67
diff changeset
2314 FOR_EACH_EDGE (e, ei, bb->succs)
kono
parents: 67
diff changeset
2315 if (bb_in_bbs (e->dest, region_copy, n_region))
kono
parents: 67
diff changeset
2316 {
kono
parents: 67
diff changeset
2317 basic_block orig = get_bb_original (e->dest);
kono
parents: 67
diff changeset
2318 if (orig)
kono
parents: 67
diff changeset
2319 redirect_edge_and_branch_force (e, orig);
kono
parents: 67
diff changeset
2320 }
kono
parents: 67
diff changeset
2321 continue;
kono
parents: 67
diff changeset
2322 }
kono
parents: 67
diff changeset
2323
kono
parents: 67
diff changeset
2324 /* Redirect all other edges jumping to non-adjacent blocks back to the
kono
parents: 67
diff changeset
2325 original code. */
kono
parents: 67
diff changeset
2326 FOR_EACH_EDGE (e, ei, bb->succs)
kono
parents: 67
diff changeset
2327 if (region_copy[i + 1] != e->dest)
kono
parents: 67
diff changeset
2328 {
kono
parents: 67
diff changeset
2329 basic_block orig = get_bb_original (e->dest);
kono
parents: 67
diff changeset
2330 if (orig)
kono
parents: 67
diff changeset
2331 redirect_edge_and_branch_force (e, orig);
kono
parents: 67
diff changeset
2332 }
kono
parents: 67
diff changeset
2333 else
kono
parents: 67
diff changeset
2334 {
kono
parents: 67
diff changeset
2335 curr_count = e->count ();
kono
parents: 67
diff changeset
2336 }
kono
parents: 67
diff changeset
2337 }
kono
parents: 67
diff changeset
2338
kono
parents: 67
diff changeset
2339
kono
parents: 67
diff changeset
2340 if (flag_checking)
kono
parents: 67
diff changeset
2341 verify_jump_thread (region_copy, n_region);
kono
parents: 67
diff changeset
2342
kono
parents: 67
diff changeset
2343 /* Remove the last branch in the jump thread path. */
kono
parents: 67
diff changeset
2344 remove_ctrl_stmt_and_useless_edges (region_copy[n_region - 1], exit->dest);
kono
parents: 67
diff changeset
2345
kono
parents: 67
diff changeset
2346 /* And fixup the flags on the single remaining edge. */
kono
parents: 67
diff changeset
2347 edge fix_e = find_edge (region_copy[n_region - 1], exit->dest);
kono
parents: 67
diff changeset
2348 fix_e->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE | EDGE_ABNORMAL);
kono
parents: 67
diff changeset
2349 fix_e->flags |= EDGE_FALLTHRU;
kono
parents: 67
diff changeset
2350
kono
parents: 67
diff changeset
2351 edge e = make_edge (region_copy[n_region - 1], exit->dest, EDGE_FALLTHRU);
kono
parents: 67
diff changeset
2352
kono
parents: 67
diff changeset
2353 if (e)
kono
parents: 67
diff changeset
2354 {
kono
parents: 67
diff changeset
2355 rescan_loop_exit (e, true, false);
kono
parents: 67
diff changeset
2356 e->probability = profile_probability::always ();
kono
parents: 67
diff changeset
2357 }
kono
parents: 67
diff changeset
2358
kono
parents: 67
diff changeset
2359 /* Redirect the entry and add the phi node arguments. */
kono
parents: 67
diff changeset
2360 if (entry->dest == loop->header)
kono
parents: 67
diff changeset
2361 mark_loop_for_removal (loop);
kono
parents: 67
diff changeset
2362 redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
kono
parents: 67
diff changeset
2363 gcc_assert (redirected != NULL);
kono
parents: 67
diff changeset
2364 flush_pending_stmts (entry);
kono
parents: 67
diff changeset
2365
kono
parents: 67
diff changeset
2366 /* Add the other PHI node arguments. */
kono
parents: 67
diff changeset
2367 add_phi_args_after_copy (region_copy, n_region, NULL);
kono
parents: 67
diff changeset
2368
kono
parents: 67
diff changeset
2369 free (region_copy);
kono
parents: 67
diff changeset
2370
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2371 adjust_paths_after_duplication (current_path_no);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2372
111
kono
parents: 67
diff changeset
2373 free_original_copy_tables ();
kono
parents: 67
diff changeset
2374 return true;
kono
parents: 67
diff changeset
2375 }
kono
parents: 67
diff changeset
2376
kono
parents: 67
diff changeset
2377 /* Return true when PATH is a valid jump-thread path. */
kono
parents: 67
diff changeset
2378
kono
parents: 67
diff changeset
2379 static bool
kono
parents: 67
diff changeset
2380 valid_jump_thread_path (vec<jump_thread_edge *> *path)
kono
parents: 67
diff changeset
2381 {
kono
parents: 67
diff changeset
2382 unsigned len = path->length ();
kono
parents: 67
diff changeset
2383
kono
parents: 67
diff changeset
2384 /* Check that the path is connected. */
kono
parents: 67
diff changeset
2385 for (unsigned int j = 0; j < len - 1; j++)
kono
parents: 67
diff changeset
2386 {
kono
parents: 67
diff changeset
2387 edge e = (*path)[j]->e;
kono
parents: 67
diff changeset
2388 if (e->dest != (*path)[j+1]->e->src)
kono
parents: 67
diff changeset
2389 return false;
kono
parents: 67
diff changeset
2390 }
kono
parents: 67
diff changeset
2391 return true;
kono
parents: 67
diff changeset
2392 }
kono
parents: 67
diff changeset
2393
kono
parents: 67
diff changeset
2394 /* Remove any queued jump threads that include edge E.
kono
parents: 67
diff changeset
2395
kono
parents: 67
diff changeset
2396 We don't actually remove them here, just record the edges into ax
kono
parents: 67
diff changeset
2397 hash table. That way we can do the search once per iteration of
kono
parents: 67
diff changeset
2398 DOM/VRP rather than for every case where DOM optimizes away a COND_EXPR. */
kono
parents: 67
diff changeset
2399
kono
parents: 67
diff changeset
2400 void
kono
parents: 67
diff changeset
2401 remove_jump_threads_including (edge_def *e)
kono
parents: 67
diff changeset
2402 {
kono
parents: 67
diff changeset
2403 if (!paths.exists ())
kono
parents: 67
diff changeset
2404 return;
kono
parents: 67
diff changeset
2405
kono
parents: 67
diff changeset
2406 if (!removed_edges)
kono
parents: 67
diff changeset
2407 removed_edges = new hash_table<struct removed_edges> (17);
kono
parents: 67
diff changeset
2408
kono
parents: 67
diff changeset
2409 edge *slot = removed_edges->find_slot (e, INSERT);
kono
parents: 67
diff changeset
2410 *slot = e;
kono
parents: 67
diff changeset
2411 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2412
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2413 /* Walk through all blocks and thread incoming edges to the appropriate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2414 outgoing edge for each edge pair recorded in THREADED_EDGES.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2415
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2416 It is the caller's responsibility to fix the dominance information
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2417 and rewrite duplicated SSA_NAMEs back into SSA form.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2418
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2419 If MAY_PEEL_LOOP_HEADERS is false, we avoid threading edges through
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2420 loop headers if it does not simplify the loop.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2421
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2422 Returns true if one or more edges were threaded, false otherwise. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2423
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2424 bool
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2425 thread_through_all_blocks (bool may_peel_loop_headers)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2426 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2427 bool retval = false;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2428 unsigned int i;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2429 struct loop *loop;
111
kono
parents: 67
diff changeset
2430 auto_bitmap threaded_blocks;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2431 hash_set<edge> visited_starting_edges;
111
kono
parents: 67
diff changeset
2432
kono
parents: 67
diff changeset
2433 if (!paths.exists ())
kono
parents: 67
diff changeset
2434 {
kono
parents: 67
diff changeset
2435 retval = false;
kono
parents: 67
diff changeset
2436 goto out;
kono
parents: 67
diff changeset
2437 }
kono
parents: 67
diff changeset
2438
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2439 memset (&thread_stats, 0, sizeof (thread_stats));
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2440
111
kono
parents: 67
diff changeset
2441 /* Remove any paths that referenced removed edges. */
kono
parents: 67
diff changeset
2442 if (removed_edges)
kono
parents: 67
diff changeset
2443 for (i = 0; i < paths.length (); )
kono
parents: 67
diff changeset
2444 {
kono
parents: 67
diff changeset
2445 unsigned int j;
kono
parents: 67
diff changeset
2446 vec<jump_thread_edge *> *path = paths[i];
kono
parents: 67
diff changeset
2447
kono
parents: 67
diff changeset
2448 for (j = 0; j < path->length (); j++)
kono
parents: 67
diff changeset
2449 {
kono
parents: 67
diff changeset
2450 edge e = (*path)[j]->e;
kono
parents: 67
diff changeset
2451 if (removed_edges->find_slot (e, NO_INSERT))
kono
parents: 67
diff changeset
2452 break;
kono
parents: 67
diff changeset
2453 }
kono
parents: 67
diff changeset
2454
kono
parents: 67
diff changeset
2455 if (j != path->length ())
kono
parents: 67
diff changeset
2456 {
kono
parents: 67
diff changeset
2457 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
2458 paths.unordered_remove (i);
kono
parents: 67
diff changeset
2459 continue;
kono
parents: 67
diff changeset
2460 }
kono
parents: 67
diff changeset
2461 i++;
kono
parents: 67
diff changeset
2462 }
kono
parents: 67
diff changeset
2463
kono
parents: 67
diff changeset
2464 /* Jump-thread all FSM threads before other jump-threads. */
kono
parents: 67
diff changeset
2465 for (i = 0; i < paths.length ();)
kono
parents: 67
diff changeset
2466 {
kono
parents: 67
diff changeset
2467 vec<jump_thread_edge *> *path = paths[i];
kono
parents: 67
diff changeset
2468 edge entry = (*path)[0]->e;
kono
parents: 67
diff changeset
2469
kono
parents: 67
diff changeset
2470 /* Only code-generate FSM jump-threads in this loop. */
kono
parents: 67
diff changeset
2471 if ((*path)[0]->type != EDGE_FSM_THREAD)
kono
parents: 67
diff changeset
2472 {
kono
parents: 67
diff changeset
2473 i++;
kono
parents: 67
diff changeset
2474 continue;
kono
parents: 67
diff changeset
2475 }
kono
parents: 67
diff changeset
2476
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2477 /* Do not jump-thread twice from the same starting edge.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2478
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2479 Previously we only checked that we weren't threading twice
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2480 from the same BB, but that was too restrictive. Imagine a
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2481 path that starts from GIMPLE_COND(x_123 == 0,...), where both
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2482 edges out of this conditional yield paths that can be
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2483 threaded (for example, both lead to an x_123==0 or x_123!=0
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2484 conditional further down the line. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2485 if (visited_starting_edges.contains (entry)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2486 /* We may not want to realize this jump thread path for
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2487 various reasons. So check it first. */
111
kono
parents: 67
diff changeset
2488 || !valid_jump_thread_path (path))
kono
parents: 67
diff changeset
2489 {
kono
parents: 67
diff changeset
2490 /* Remove invalid FSM jump-thread paths. */
kono
parents: 67
diff changeset
2491 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
2492 paths.unordered_remove (i);
kono
parents: 67
diff changeset
2493 continue;
kono
parents: 67
diff changeset
2494 }
kono
parents: 67
diff changeset
2495
kono
parents: 67
diff changeset
2496 unsigned len = path->length ();
kono
parents: 67
diff changeset
2497 edge exit = (*path)[len - 1]->e;
kono
parents: 67
diff changeset
2498 basic_block *region = XNEWVEC (basic_block, len - 1);
kono
parents: 67
diff changeset
2499
kono
parents: 67
diff changeset
2500 for (unsigned int j = 0; j < len - 1; j++)
kono
parents: 67
diff changeset
2501 region[j] = (*path)[j]->e->dest;
kono
parents: 67
diff changeset
2502
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2503 if (duplicate_thread_path (entry, exit, region, len - 1, i))
111
kono
parents: 67
diff changeset
2504 {
kono
parents: 67
diff changeset
2505 /* We do not update dominance info. */
kono
parents: 67
diff changeset
2506 free_dominance_info (CDI_DOMINATORS);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2507 visited_starting_edges.add (entry);
111
kono
parents: 67
diff changeset
2508 retval = true;
kono
parents: 67
diff changeset
2509 thread_stats.num_threaded_edges++;
kono
parents: 67
diff changeset
2510 }
kono
parents: 67
diff changeset
2511
kono
parents: 67
diff changeset
2512 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
2513 paths.unordered_remove (i);
kono
parents: 67
diff changeset
2514 free (region);
kono
parents: 67
diff changeset
2515 }
kono
parents: 67
diff changeset
2516
kono
parents: 67
diff changeset
2517 /* Remove from PATHS all the jump-threads starting with an edge already
kono
parents: 67
diff changeset
2518 jump-threaded. */
kono
parents: 67
diff changeset
2519 for (i = 0; i < paths.length ();)
kono
parents: 67
diff changeset
2520 {
kono
parents: 67
diff changeset
2521 vec<jump_thread_edge *> *path = paths[i];
kono
parents: 67
diff changeset
2522 edge entry = (*path)[0]->e;
kono
parents: 67
diff changeset
2523
kono
parents: 67
diff changeset
2524 /* Do not jump-thread twice from the same block. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2525 if (visited_starting_edges.contains (entry))
111
kono
parents: 67
diff changeset
2526 {
kono
parents: 67
diff changeset
2527 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
2528 paths.unordered_remove (i);
kono
parents: 67
diff changeset
2529 }
kono
parents: 67
diff changeset
2530 else
kono
parents: 67
diff changeset
2531 i++;
kono
parents: 67
diff changeset
2532 }
kono
parents: 67
diff changeset
2533
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2534 mark_threaded_blocks (threaded_blocks);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2535
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2536 initialize_original_copy_tables ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2537
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2538 /* The order in which we process jump threads can be important.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2539
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2540 Consider if we have two jump threading paths A and B. If the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2541 target edge of A is the starting edge of B and we thread path A
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2542 first, then we create an additional incoming edge into B->dest that
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2543 we can not discover as a jump threading path on this iteration.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2544
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2545 If we instead thread B first, then the edge into B->dest will have
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2546 already been redirected before we process path A and path A will
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2547 natually, with no further work, target the redirected path for B.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2548
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2549 An post-order is sufficient here. Compute the ordering first, then
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2550 process the blocks. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2551 if (!bitmap_empty_p (threaded_blocks))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2552 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2553 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2554 unsigned int postorder_num = post_order_compute (postorder, false, false);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2555 for (unsigned int i = 0; i < postorder_num; i++)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2556 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2557 unsigned int indx = postorder[i];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2558 if (bitmap_bit_p (threaded_blocks, indx))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2559 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2560 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, indx);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2561 retval |= thread_block (bb, true);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2562 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2563 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2564 free (postorder);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2565 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2566
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2567 /* Then perform the threading through loop headers. We start with the
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2568 innermost loop, so that the changes in cfg we perform won't affect
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2569 further threading. */
111
kono
parents: 67
diff changeset
2570 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2571 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2572 if (!loop->header
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2573 || !bitmap_bit_p (threaded_blocks, loop->header->index))
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2574 continue;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2575
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2576 retval |= thread_through_loop_header (loop, may_peel_loop_headers);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2577 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2578
111
kono
parents: 67
diff changeset
2579 /* All jump threading paths should have been resolved at this
kono
parents: 67
diff changeset
2580 point. Verify that is the case. */
kono
parents: 67
diff changeset
2581 basic_block bb;
kono
parents: 67
diff changeset
2582 FOR_EACH_BB_FN (bb, cfun)
kono
parents: 67
diff changeset
2583 {
kono
parents: 67
diff changeset
2584 edge_iterator ei;
kono
parents: 67
diff changeset
2585 edge e;
kono
parents: 67
diff changeset
2586 FOR_EACH_EDGE (e, ei, bb->preds)
kono
parents: 67
diff changeset
2587 gcc_assert (e->aux == NULL);
kono
parents: 67
diff changeset
2588 }
kono
parents: 67
diff changeset
2589
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2590 statistics_counter_event (cfun, "Jumps threaded",
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2591 thread_stats.num_threaded_edges);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2592
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2593 free_original_copy_tables ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2594
111
kono
parents: 67
diff changeset
2595 paths.release ();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2596
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2597 if (retval)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2598 loops_state_set (LOOPS_NEED_FIXUP);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2599
111
kono
parents: 67
diff changeset
2600 out:
kono
parents: 67
diff changeset
2601 delete removed_edges;
kono
parents: 67
diff changeset
2602 removed_edges = NULL;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2603 return retval;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2604 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2605
111
kono
parents: 67
diff changeset
2606 /* Delete the jump threading path PATH. We have to explicitly delete
kono
parents: 67
diff changeset
2607 each entry in the vector, then the container. */
kono
parents: 67
diff changeset
2608
kono
parents: 67
diff changeset
2609 void
kono
parents: 67
diff changeset
2610 delete_jump_thread_path (vec<jump_thread_edge *> *path)
kono
parents: 67
diff changeset
2611 {
kono
parents: 67
diff changeset
2612 for (unsigned int i = 0; i < path->length (); i++)
kono
parents: 67
diff changeset
2613 delete (*path)[i];
kono
parents: 67
diff changeset
2614 path->release();
kono
parents: 67
diff changeset
2615 delete path;
kono
parents: 67
diff changeset
2616 }
kono
parents: 67
diff changeset
2617
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2618 /* Register a jump threading opportunity. We queue up all the jump
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2619 threading opportunities discovered by a pass and update the CFG
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2620 and SSA form all at once.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2621
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2622 E is the edge we can thread, E2 is the new target edge, i.e., we
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2623 are effectively recording that E->dest can be changed to E2->dest
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2624 after fixing the SSA graph. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2625
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2626 void
111
kono
parents: 67
diff changeset
2627 register_jump_thread (vec<jump_thread_edge *> *path)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2628 {
111
kono
parents: 67
diff changeset
2629 if (!dbg_cnt (registered_jump_thread))
kono
parents: 67
diff changeset
2630 {
kono
parents: 67
diff changeset
2631 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
2632 return;
kono
parents: 67
diff changeset
2633 }
kono
parents: 67
diff changeset
2634
kono
parents: 67
diff changeset
2635 /* First make sure there are no NULL outgoing edges on the jump threading
kono
parents: 67
diff changeset
2636 path. That can happen for jumping to a constant address. */
kono
parents: 67
diff changeset
2637 for (unsigned int i = 0; i < path->length (); i++)
kono
parents: 67
diff changeset
2638 {
kono
parents: 67
diff changeset
2639 if ((*path)[i]->e == NULL)
kono
parents: 67
diff changeset
2640 {
kono
parents: 67
diff changeset
2641 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents: 67
diff changeset
2642 {
kono
parents: 67
diff changeset
2643 fprintf (dump_file,
kono
parents: 67
diff changeset
2644 "Found NULL edge in jump threading path. Cancelling jump thread:\n");
kono
parents: 67
diff changeset
2645 dump_jump_thread_path (dump_file, *path, false);
kono
parents: 67
diff changeset
2646 }
kono
parents: 67
diff changeset
2647
kono
parents: 67
diff changeset
2648 delete_jump_thread_path (path);
kono
parents: 67
diff changeset
2649 return;
kono
parents: 67
diff changeset
2650 }
kono
parents: 67
diff changeset
2651
kono
parents: 67
diff changeset
2652 /* Only the FSM threader is allowed to thread across
kono
parents: 67
diff changeset
2653 backedges in the CFG. */
kono
parents: 67
diff changeset
2654 if (flag_checking
kono
parents: 67
diff changeset
2655 && (*path)[0]->type != EDGE_FSM_THREAD)
kono
parents: 67
diff changeset
2656 gcc_assert (((*path)[i]->e->flags & EDGE_DFS_BACK) == 0);
kono
parents: 67
diff changeset
2657 }
kono
parents: 67
diff changeset
2658
kono
parents: 67
diff changeset
2659 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents: 67
diff changeset
2660 dump_jump_thread_path (dump_file, *path, true);
kono
parents: 67
diff changeset
2661
kono
parents: 67
diff changeset
2662 if (!paths.exists ())
kono
parents: 67
diff changeset
2663 paths.create (5);
kono
parents: 67
diff changeset
2664
kono
parents: 67
diff changeset
2665 paths.safe_push (path);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2666 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2667
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2668 /* Return how many uses of T there are within BB, as long as there
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2669 aren't any uses outside BB. If there are any uses outside BB,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2670 return -1 if there's at most one use within BB, or -2 if there is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2671 more than one use within BB. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2672
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2673 static int
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2674 uses_in_bb (tree t, basic_block bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2675 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2676 int uses = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2677 bool outside_bb = false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2678
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2679 imm_use_iterator iter;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2680 use_operand_p use_p;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2681 FOR_EACH_IMM_USE_FAST (use_p, iter, t)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2682 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2683 if (is_gimple_debug (USE_STMT (use_p)))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2684 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2685
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2686 if (gimple_bb (USE_STMT (use_p)) != bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2687 outside_bb = true;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2688 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2689 uses++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2690
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2691 if (outside_bb && uses > 1)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2692 return -2;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2693 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2694
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2695 if (outside_bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2696 return -1;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2697
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2698 return uses;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2699 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2700
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2701 /* Starting from the final control flow stmt in BB, assuming it will
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2702 be removed, follow uses in to-be-removed stmts back to their defs
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2703 and count how many defs are to become dead and be removed as
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2704 well. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2705
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2706 unsigned int
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2707 estimate_threading_killed_stmts (basic_block bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2708 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2709 int killed_stmts = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2710 hash_map<tree, int> ssa_remaining_uses;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2711 auto_vec<gimple *, 4> dead_worklist;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2712
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2713 /* If the block has only two predecessors, threading will turn phi
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2714 dsts into either src, so count them as dead stmts. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2715 bool drop_all_phis = EDGE_COUNT (bb->preds) == 2;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2716
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2717 if (drop_all_phis)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2718 for (gphi_iterator gsi = gsi_start_phis (bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2719 !gsi_end_p (gsi); gsi_next (&gsi))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2720 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2721 gphi *phi = gsi.phi ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2722 tree dst = gimple_phi_result (phi);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2723
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2724 /* We don't count virtual PHIs as stmts in
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2725 record_temporary_equivalences_from_phis. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2726 if (virtual_operand_p (dst))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2727 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2728
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2729 killed_stmts++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2730 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2731
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2732 if (gsi_end_p (gsi_last_bb (bb)))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2733 return killed_stmts;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2734
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2735 gimple *stmt = gsi_stmt (gsi_last_bb (bb));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2736 if (gimple_code (stmt) != GIMPLE_COND
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2737 && gimple_code (stmt) != GIMPLE_GOTO
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2738 && gimple_code (stmt) != GIMPLE_SWITCH)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2739 return killed_stmts;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2740
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2741 /* The control statement is always dead. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2742 killed_stmts++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2743 dead_worklist.quick_push (stmt);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2744 while (!dead_worklist.is_empty ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2745 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2746 stmt = dead_worklist.pop ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2747
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2748 ssa_op_iter iter;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2749 use_operand_p use_p;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2750 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2751 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2752 tree t = USE_FROM_PTR (use_p);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2753 gimple *def = SSA_NAME_DEF_STMT (t);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2754
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2755 if (gimple_bb (def) == bb
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2756 && (gimple_code (def) != GIMPLE_PHI
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2757 || !drop_all_phis)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2758 && !gimple_has_side_effects (def))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2759 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2760 int *usesp = ssa_remaining_uses.get (t);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2761 int uses;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2762
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2763 if (usesp)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2764 uses = *usesp;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2765 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2766 uses = uses_in_bb (t, bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2767
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2768 gcc_assert (uses);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2769
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2770 /* Don't bother recording the expected use count if we
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2771 won't find any further uses within BB. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2772 if (!usesp && (uses < -1 || uses > 1))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2773 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2774 usesp = &ssa_remaining_uses.get_or_insert (t);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2775 *usesp = uses;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2776 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2777
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2778 if (uses < 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2779 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2780
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2781 --uses;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2782 if (usesp)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2783 *usesp = uses;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2784
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2785 if (!uses)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2786 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2787 killed_stmts++;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2788 if (usesp)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2789 ssa_remaining_uses.remove (t);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2790 if (gimple_code (def) != GIMPLE_PHI)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2791 dead_worklist.safe_push (def);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2792 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2793 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2794 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2795 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2796
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2797 if (dump_file)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2798 fprintf (dump_file, "threading bb %i kills %i stmts\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2799 bb->index, killed_stmts);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2800
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2801 return killed_stmts;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2802 }