Mercurial > hg > CbC > CbC_gcc
annotate gcc/tree-ssa-phiopt.c @ 63:b7f97abdc517 gcc-4.6-20100522
update gcc from gcc-4.5.0 to gcc-4.6
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 24 May 2010 12:47:05 +0900 |
parents | 77e2b8dfacca |
children | f6334be47118 |
rev | line source |
---|---|
0 | 1 /* Optimization of PHI nodes by converting them into straightline code. |
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, | |
3 Inc. | |
4 | |
5 This file is part of GCC. | |
6 | |
7 GCC is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3, or (at your option) any | |
10 later version. | |
11 | |
12 GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with GCC; see the file COPYING3. If not see | |
19 <http://www.gnu.org/licenses/>. */ | |
20 | |
21 #include "config.h" | |
22 #include "system.h" | |
23 #include "coretypes.h" | |
24 #include "tm.h" | |
25 #include "ggc.h" | |
26 #include "tree.h" | |
27 #include "flags.h" | |
28 #include "tm_p.h" | |
29 #include "basic-block.h" | |
30 #include "timevar.h" | |
31 #include "diagnostic.h" | |
32 #include "tree-flow.h" | |
33 #include "tree-pass.h" | |
34 #include "tree-dump.h" | |
35 #include "langhooks.h" | |
36 #include "pointer-set.h" | |
37 #include "domwalk.h" | |
38 | |
39 static unsigned int tree_ssa_phiopt (void); | |
40 static unsigned int tree_ssa_phiopt_worker (bool); | |
41 static bool conditional_replacement (basic_block, basic_block, | |
42 edge, edge, gimple, tree, tree); | |
43 static bool value_replacement (basic_block, basic_block, | |
44 edge, edge, gimple, tree, tree); | |
45 static bool minmax_replacement (basic_block, basic_block, | |
46 edge, edge, gimple, tree, tree); | |
47 static bool abs_replacement (basic_block, basic_block, | |
48 edge, edge, gimple, tree, tree); | |
49 static bool cond_store_replacement (basic_block, basic_block, edge, edge, | |
50 struct pointer_set_t *); | |
51 static struct pointer_set_t * get_non_trapping (void); | |
52 static void replace_phi_edge_with_variable (basic_block, edge, gimple, tree); | |
53 | |
54 /* This pass tries to replaces an if-then-else block with an | |
55 assignment. We have four kinds of transformations. Some of these | |
56 transformations are also performed by the ifcvt RTL optimizer. | |
57 | |
58 Conditional Replacement | |
59 ----------------------- | |
60 | |
61 This transformation, implemented in conditional_replacement, | |
62 replaces | |
63 | |
64 bb0: | |
65 if (cond) goto bb2; else goto bb1; | |
66 bb1: | |
67 bb2: | |
68 x = PHI <0 (bb1), 1 (bb0), ...>; | |
69 | |
70 with | |
71 | |
72 bb0: | |
73 x' = cond; | |
74 goto bb2; | |
75 bb2: | |
76 x = PHI <x' (bb0), ...>; | |
77 | |
78 We remove bb1 as it becomes unreachable. This occurs often due to | |
79 gimplification of conditionals. | |
80 | |
81 Value Replacement | |
82 ----------------- | |
83 | |
84 This transformation, implemented in value_replacement, replaces | |
85 | |
86 bb0: | |
87 if (a != b) goto bb2; else goto bb1; | |
88 bb1: | |
89 bb2: | |
90 x = PHI <a (bb1), b (bb0), ...>; | |
91 | |
92 with | |
93 | |
94 bb0: | |
95 bb2: | |
96 x = PHI <b (bb0), ...>; | |
97 | |
98 This opportunity can sometimes occur as a result of other | |
99 optimizations. | |
100 | |
101 ABS Replacement | |
102 --------------- | |
103 | |
104 This transformation, implemented in abs_replacement, replaces | |
105 | |
106 bb0: | |
107 if (a >= 0) goto bb2; else goto bb1; | |
108 bb1: | |
109 x = -a; | |
110 bb2: | |
111 x = PHI <x (bb1), a (bb0), ...>; | |
112 | |
113 with | |
114 | |
115 bb0: | |
116 x' = ABS_EXPR< a >; | |
117 bb2: | |
118 x = PHI <x' (bb0), ...>; | |
119 | |
120 MIN/MAX Replacement | |
121 ------------------- | |
122 | |
123 This transformation, minmax_replacement replaces | |
124 | |
125 bb0: | |
126 if (a <= b) goto bb2; else goto bb1; | |
127 bb1: | |
128 bb2: | |
129 x = PHI <b (bb1), a (bb0), ...>; | |
130 | |
131 with | |
132 | |
133 bb0: | |
134 x' = MIN_EXPR (a, b) | |
135 bb2: | |
136 x = PHI <x' (bb0), ...>; | |
137 | |
138 A similar transformation is done for MAX_EXPR. */ | |
139 | |
140 static unsigned int | |
141 tree_ssa_phiopt (void) | |
142 { | |
143 return tree_ssa_phiopt_worker (false); | |
144 } | |
145 | |
146 /* This pass tries to transform conditional stores into unconditional | |
147 ones, enabling further simplifications with the simpler then and else | |
148 blocks. In particular it replaces this: | |
149 | |
150 bb0: | |
151 if (cond) goto bb2; else goto bb1; | |
152 bb1: | |
153 *p = RHS | |
154 bb2: | |
155 | |
156 with | |
157 | |
158 bb0: | |
159 if (cond) goto bb1; else goto bb2; | |
160 bb1: | |
161 condtmp' = *p; | |
162 bb2: | |
163 condtmp = PHI <RHS, condtmp'> | |
164 *p = condtmp | |
165 | |
166 This transformation can only be done under several constraints, | |
167 documented below. */ | |
168 | |
169 static unsigned int | |
170 tree_ssa_cs_elim (void) | |
171 { | |
172 return tree_ssa_phiopt_worker (true); | |
173 } | |
174 | |
175 /* For conditional store replacement we need a temporary to | |
176 put the old contents of the memory in. */ | |
177 static tree condstoretemp; | |
178 | |
179 /* The core routine of conditional store replacement and normal | |
180 phi optimizations. Both share much of the infrastructure in how | |
181 to match applicable basic block patterns. DO_STORE_ELIM is true | |
182 when we want to do conditional store replacement, false otherwise. */ | |
183 static unsigned int | |
184 tree_ssa_phiopt_worker (bool do_store_elim) | |
185 { | |
186 basic_block bb; | |
187 basic_block *bb_order; | |
188 unsigned n, i; | |
189 bool cfgchanged = false; | |
190 struct pointer_set_t *nontrap = 0; | |
191 | |
192 if (do_store_elim) | |
193 { | |
194 condstoretemp = NULL_TREE; | |
195 /* Calculate the set of non-trapping memory accesses. */ | |
196 nontrap = get_non_trapping (); | |
197 } | |
198 | |
199 /* Search every basic block for COND_EXPR we may be able to optimize. | |
200 | |
201 We walk the blocks in order that guarantees that a block with | |
202 a single predecessor is processed before the predecessor. | |
203 This ensures that we collapse inner ifs before visiting the | |
204 outer ones, and also that we do not try to visit a removed | |
205 block. */ | |
206 bb_order = blocks_in_phiopt_order (); | |
207 n = n_basic_blocks - NUM_FIXED_BLOCKS; | |
208 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
209 for (i = 0; i < n; i++) |
0 | 210 { |
211 gimple cond_stmt, phi; | |
212 basic_block bb1, bb2; | |
213 edge e1, e2; | |
214 tree arg0, arg1; | |
215 | |
216 bb = bb_order[i]; | |
217 | |
218 cond_stmt = last_stmt (bb); | |
219 /* Check to see if the last statement is a GIMPLE_COND. */ | |
220 if (!cond_stmt | |
221 || gimple_code (cond_stmt) != GIMPLE_COND) | |
222 continue; | |
223 | |
224 e1 = EDGE_SUCC (bb, 0); | |
225 bb1 = e1->dest; | |
226 e2 = EDGE_SUCC (bb, 1); | |
227 bb2 = e2->dest; | |
228 | |
229 /* We cannot do the optimization on abnormal edges. */ | |
230 if ((e1->flags & EDGE_ABNORMAL) != 0 | |
231 || (e2->flags & EDGE_ABNORMAL) != 0) | |
232 continue; | |
233 | |
234 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */ | |
235 if (EDGE_COUNT (bb1->succs) == 0 | |
236 || bb2 == NULL | |
237 || EDGE_COUNT (bb2->succs) == 0) | |
238 continue; | |
239 | |
240 /* Find the bb which is the fall through to the other. */ | |
241 if (EDGE_SUCC (bb1, 0)->dest == bb2) | |
242 ; | |
243 else if (EDGE_SUCC (bb2, 0)->dest == bb1) | |
244 { | |
245 basic_block bb_tmp = bb1; | |
246 edge e_tmp = e1; | |
247 bb1 = bb2; | |
248 bb2 = bb_tmp; | |
249 e1 = e2; | |
250 e2 = e_tmp; | |
251 } | |
252 else | |
253 continue; | |
254 | |
255 e1 = EDGE_SUCC (bb1, 0); | |
256 | |
257 /* Make sure that bb1 is just a fall through. */ | |
258 if (!single_succ_p (bb1) | |
259 || (e1->flags & EDGE_FALLTHRU) == 0) | |
260 continue; | |
261 | |
262 /* Also make sure that bb1 only have one predecessor and that it | |
263 is bb. */ | |
264 if (!single_pred_p (bb1) | |
265 || single_pred (bb1) != bb) | |
266 continue; | |
267 | |
268 if (do_store_elim) | |
269 { | |
270 /* bb1 is the middle block, bb2 the join block, bb the split block, | |
271 e1 the fallthrough edge from bb1 to bb2. We can't do the | |
272 optimization if the join block has more than two predecessors. */ | |
273 if (EDGE_COUNT (bb2->preds) > 2) | |
274 continue; | |
275 if (cond_store_replacement (bb1, bb2, e1, e2, nontrap)) | |
276 cfgchanged = true; | |
277 } | |
278 else | |
279 { | |
280 gimple_seq phis = phi_nodes (bb2); | |
281 | |
282 /* Check to make sure that there is only one PHI node. | |
283 TODO: we could do it with more than one iff the other PHI nodes | |
284 have the same elements for these two edges. */ | |
285 if (! gimple_seq_singleton_p (phis)) | |
286 continue; | |
287 | |
288 phi = gsi_stmt (gsi_start (phis)); | |
289 arg0 = gimple_phi_arg_def (phi, e1->dest_idx); | |
290 arg1 = gimple_phi_arg_def (phi, e2->dest_idx); | |
291 | |
292 /* Something is wrong if we cannot find the arguments in the PHI | |
293 node. */ | |
294 gcc_assert (arg0 != NULL && arg1 != NULL); | |
295 | |
296 /* Do the replacement of conditional if it can be done. */ | |
297 if (conditional_replacement (bb, bb1, e1, e2, phi, arg0, arg1)) | |
298 cfgchanged = true; | |
299 else if (value_replacement (bb, bb1, e1, e2, phi, arg0, arg1)) | |
300 cfgchanged = true; | |
301 else if (abs_replacement (bb, bb1, e1, e2, phi, arg0, arg1)) | |
302 cfgchanged = true; | |
303 else if (minmax_replacement (bb, bb1, e1, e2, phi, arg0, arg1)) | |
304 cfgchanged = true; | |
305 } | |
306 } | |
307 | |
308 free (bb_order); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
309 |
0 | 310 if (do_store_elim) |
311 pointer_set_destroy (nontrap); | |
312 /* If the CFG has changed, we should cleanup the CFG. */ | |
313 if (cfgchanged && do_store_elim) | |
314 { | |
315 /* In cond-store replacement we have added some loads on edges | |
316 and new VOPS (as we moved the store, and created a load). */ | |
317 gsi_commit_edge_inserts (); | |
318 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals; | |
319 } | |
320 else if (cfgchanged) | |
321 return TODO_cleanup_cfg; | |
322 return 0; | |
323 } | |
324 | |
325 /* Returns the list of basic blocks in the function in an order that guarantees | |
326 that if a block X has just a single predecessor Y, then Y is after X in the | |
327 ordering. */ | |
328 | |
329 basic_block * | |
330 blocks_in_phiopt_order (void) | |
331 { | |
332 basic_block x, y; | |
333 basic_block *order = XNEWVEC (basic_block, n_basic_blocks); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
334 unsigned n = n_basic_blocks - NUM_FIXED_BLOCKS; |
0 | 335 unsigned np, i; |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
336 sbitmap visited = sbitmap_alloc (last_basic_block); |
0 | 337 |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
338 #define MARK_VISITED(BB) (SET_BIT (visited, (BB)->index)) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
339 #define VISITED_P(BB) (TEST_BIT (visited, (BB)->index)) |
0 | 340 |
341 sbitmap_zero (visited); | |
342 | |
343 MARK_VISITED (ENTRY_BLOCK_PTR); | |
344 FOR_EACH_BB (x) | |
345 { | |
346 if (VISITED_P (x)) | |
347 continue; | |
348 | |
349 /* Walk the predecessors of x as long as they have precisely one | |
350 predecessor and add them to the list, so that they get stored | |
351 after x. */ | |
352 for (y = x, np = 1; | |
353 single_pred_p (y) && !VISITED_P (single_pred (y)); | |
354 y = single_pred (y)) | |
355 np++; | |
356 for (y = x, i = n - np; | |
357 single_pred_p (y) && !VISITED_P (single_pred (y)); | |
358 y = single_pred (y), i++) | |
359 { | |
360 order[i] = y; | |
361 MARK_VISITED (y); | |
362 } | |
363 order[i] = y; | |
364 MARK_VISITED (y); | |
365 | |
366 gcc_assert (i == n - 1); | |
367 n -= np; | |
368 } | |
369 | |
370 sbitmap_free (visited); | |
371 gcc_assert (n == 0); | |
372 return order; | |
373 | |
374 #undef MARK_VISITED | |
375 #undef VISITED_P | |
376 } | |
377 | |
378 | |
379 /* Return TRUE if block BB has no executable statements, otherwise return | |
380 FALSE. */ | |
381 | |
382 bool | |
383 empty_block_p (basic_block bb) | |
384 { | |
385 /* BB must have no executable statements. */ | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
386 gimple_stmt_iterator gsi = gsi_after_labels (bb); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
387 if (gsi_end_p (gsi)) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
388 return true; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
389 if (is_gimple_debug (gsi_stmt (gsi))) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
390 gsi_next_nondebug (&gsi); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
391 return gsi_end_p (gsi); |
0 | 392 } |
393 | |
394 /* Replace PHI node element whose edge is E in block BB with variable NEW. | |
395 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK | |
396 is known to have two edges, one of which must reach BB). */ | |
397 | |
398 static void | |
399 replace_phi_edge_with_variable (basic_block cond_block, | |
400 edge e, gimple phi, tree new_tree) | |
401 { | |
402 basic_block bb = gimple_bb (phi); | |
403 basic_block block_to_remove; | |
404 gimple_stmt_iterator gsi; | |
405 | |
406 /* Change the PHI argument to new. */ | |
407 SET_USE (PHI_ARG_DEF_PTR (phi, e->dest_idx), new_tree); | |
408 | |
409 /* Remove the empty basic block. */ | |
410 if (EDGE_SUCC (cond_block, 0)->dest == bb) | |
411 { | |
412 EDGE_SUCC (cond_block, 0)->flags |= EDGE_FALLTHRU; | |
413 EDGE_SUCC (cond_block, 0)->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE); | |
414 EDGE_SUCC (cond_block, 0)->probability = REG_BR_PROB_BASE; | |
415 EDGE_SUCC (cond_block, 0)->count += EDGE_SUCC (cond_block, 1)->count; | |
416 | |
417 block_to_remove = EDGE_SUCC (cond_block, 1)->dest; | |
418 } | |
419 else | |
420 { | |
421 EDGE_SUCC (cond_block, 1)->flags |= EDGE_FALLTHRU; | |
422 EDGE_SUCC (cond_block, 1)->flags | |
423 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE); | |
424 EDGE_SUCC (cond_block, 1)->probability = REG_BR_PROB_BASE; | |
425 EDGE_SUCC (cond_block, 1)->count += EDGE_SUCC (cond_block, 0)->count; | |
426 | |
427 block_to_remove = EDGE_SUCC (cond_block, 0)->dest; | |
428 } | |
429 delete_basic_block (block_to_remove); | |
430 | |
431 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */ | |
432 gsi = gsi_last_bb (cond_block); | |
433 gsi_remove (&gsi, true); | |
434 | |
435 if (dump_file && (dump_flags & TDF_DETAILS)) | |
436 fprintf (dump_file, | |
437 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n", | |
438 cond_block->index, | |
439 bb->index); | |
440 } | |
441 | |
442 /* The function conditional_replacement does the main work of doing the | |
443 conditional replacement. Return true if the replacement is done. | |
444 Otherwise return false. | |
445 BB is the basic block where the replacement is going to be done on. ARG0 | |
446 is argument 0 from PHI. Likewise for ARG1. */ | |
447 | |
448 static bool | |
449 conditional_replacement (basic_block cond_bb, basic_block middle_bb, | |
450 edge e0, edge e1, gimple phi, | |
451 tree arg0, tree arg1) | |
452 { | |
453 tree result; | |
454 gimple stmt, new_stmt; | |
455 tree cond; | |
456 gimple_stmt_iterator gsi; | |
457 edge true_edge, false_edge; | |
458 tree new_var, new_var2; | |
459 | |
460 /* FIXME: Gimplification of complex type is too hard for now. */ | |
461 if (TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE | |
462 || TREE_CODE (TREE_TYPE (arg1)) == COMPLEX_TYPE) | |
463 return false; | |
464 | |
465 /* The PHI arguments have the constants 0 and 1, then convert | |
466 it to the conditional. */ | |
467 if ((integer_zerop (arg0) && integer_onep (arg1)) | |
468 || (integer_zerop (arg1) && integer_onep (arg0))) | |
469 ; | |
470 else | |
471 return false; | |
472 | |
473 if (!empty_block_p (middle_bb)) | |
474 return false; | |
475 | |
476 /* At this point we know we have a GIMPLE_COND with two successors. | |
477 One successor is BB, the other successor is an empty block which | |
478 falls through into BB. | |
479 | |
480 There is a single PHI node at the join point (BB) and its arguments | |
481 are constants (0, 1). | |
482 | |
483 So, given the condition COND, and the two PHI arguments, we can | |
484 rewrite this PHI into non-branching code: | |
485 | |
486 dest = (COND) or dest = COND' | |
487 | |
488 We use the condition as-is if the argument associated with the | |
489 true edge has the value one or the argument associated with the | |
490 false edge as the value zero. Note that those conditions are not | |
491 the same since only one of the outgoing edges from the GIMPLE_COND | |
492 will directly reach BB and thus be associated with an argument. */ | |
493 | |
494 stmt = last_stmt (cond_bb); | |
495 result = PHI_RESULT (phi); | |
496 | |
497 /* To handle special cases like floating point comparison, it is easier and | |
498 less error-prone to build a tree and gimplify it on the fly though it is | |
499 less efficient. */ | |
500 cond = fold_build2 (gimple_cond_code (stmt), boolean_type_node, | |
501 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt)); | |
502 | |
503 /* We need to know which is the true edge and which is the false | |
504 edge so that we know when to invert the condition below. */ | |
505 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); | |
506 if ((e0 == true_edge && integer_zerop (arg0)) | |
507 || (e0 == false_edge && integer_onep (arg0)) | |
508 || (e1 == true_edge && integer_zerop (arg1)) | |
509 || (e1 == false_edge && integer_onep (arg1))) | |
510 cond = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (cond), cond); | |
511 | |
512 /* Insert our new statements at the end of conditional block before the | |
513 COND_STMT. */ | |
514 gsi = gsi_for_stmt (stmt); | |
515 new_var = force_gimple_operand_gsi (&gsi, cond, true, NULL, true, | |
516 GSI_SAME_STMT); | |
517 | |
518 if (!useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (new_var))) | |
519 { | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
520 source_location locus_0, locus_1; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
521 |
0 | 522 new_var2 = create_tmp_var (TREE_TYPE (result), NULL); |
523 add_referenced_var (new_var2); | |
524 new_stmt = gimple_build_assign_with_ops (CONVERT_EXPR, new_var2, | |
525 new_var, NULL); | |
526 new_var2 = make_ssa_name (new_var2, new_stmt); | |
527 gimple_assign_set_lhs (new_stmt, new_var2); | |
528 gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT); | |
529 new_var = new_var2; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
530 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
531 /* Set the locus to the first argument, unless is doesn't have one. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
532 locus_0 = gimple_phi_arg_location (phi, 0); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
533 locus_1 = gimple_phi_arg_location (phi, 1); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
534 if (locus_0 == UNKNOWN_LOCATION) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
535 locus_0 = locus_1; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
536 gimple_set_location (new_stmt, locus_0); |
0 | 537 } |
538 | |
539 replace_phi_edge_with_variable (cond_bb, e1, phi, new_var); | |
540 | |
541 /* Note that we optimized this PHI. */ | |
542 return true; | |
543 } | |
544 | |
545 /* The function value_replacement does the main work of doing the value | |
546 replacement. Return true if the replacement is done. Otherwise return | |
547 false. | |
548 BB is the basic block where the replacement is going to be done on. ARG0 | |
549 is argument 0 from the PHI. Likewise for ARG1. */ | |
550 | |
551 static bool | |
552 value_replacement (basic_block cond_bb, basic_block middle_bb, | |
553 edge e0, edge e1, gimple phi, | |
554 tree arg0, tree arg1) | |
555 { | |
556 gimple cond; | |
557 edge true_edge, false_edge; | |
558 enum tree_code code; | |
559 | |
560 /* If the type says honor signed zeros we cannot do this | |
561 optimization. */ | |
562 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1)))) | |
563 return false; | |
564 | |
565 if (!empty_block_p (middle_bb)) | |
566 return false; | |
567 | |
568 cond = last_stmt (cond_bb); | |
569 code = gimple_cond_code (cond); | |
570 | |
571 /* This transformation is only valid for equality comparisons. */ | |
572 if (code != NE_EXPR && code != EQ_EXPR) | |
573 return false; | |
574 | |
575 /* We need to know which is the true edge and which is the false | |
576 edge so that we know if have abs or negative abs. */ | |
577 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); | |
578 | |
579 /* At this point we know we have a COND_EXPR with two successors. | |
580 One successor is BB, the other successor is an empty block which | |
581 falls through into BB. | |
582 | |
583 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR. | |
584 | |
585 There is a single PHI node at the join point (BB) with two arguments. | |
586 | |
587 We now need to verify that the two arguments in the PHI node match | |
588 the two arguments to the equality comparison. */ | |
589 | |
590 if ((operand_equal_for_phi_arg_p (arg0, gimple_cond_lhs (cond)) | |
591 && operand_equal_for_phi_arg_p (arg1, gimple_cond_rhs (cond))) | |
592 || (operand_equal_for_phi_arg_p (arg1, gimple_cond_lhs (cond)) | |
593 && operand_equal_for_phi_arg_p (arg0, gimple_cond_rhs (cond)))) | |
594 { | |
595 edge e; | |
596 tree arg; | |
597 | |
598 /* For NE_EXPR, we want to build an assignment result = arg where | |
599 arg is the PHI argument associated with the true edge. For | |
600 EQ_EXPR we want the PHI argument associated with the false edge. */ | |
601 e = (code == NE_EXPR ? true_edge : false_edge); | |
602 | |
603 /* Unfortunately, E may not reach BB (it may instead have gone to | |
604 OTHER_BLOCK). If that is the case, then we want the single outgoing | |
605 edge from OTHER_BLOCK which reaches BB and represents the desired | |
606 path from COND_BLOCK. */ | |
607 if (e->dest == middle_bb) | |
608 e = single_succ_edge (e->dest); | |
609 | |
610 /* Now we know the incoming edge to BB that has the argument for the | |
611 RHS of our new assignment statement. */ | |
612 if (e0 == e) | |
613 arg = arg0; | |
614 else | |
615 arg = arg1; | |
616 | |
617 replace_phi_edge_with_variable (cond_bb, e1, phi, arg); | |
618 | |
619 /* Note that we optimized this PHI. */ | |
620 return true; | |
621 } | |
622 return false; | |
623 } | |
624 | |
625 /* The function minmax_replacement does the main work of doing the minmax | |
626 replacement. Return true if the replacement is done. Otherwise return | |
627 false. | |
628 BB is the basic block where the replacement is going to be done on. ARG0 | |
629 is argument 0 from the PHI. Likewise for ARG1. */ | |
630 | |
631 static bool | |
632 minmax_replacement (basic_block cond_bb, basic_block middle_bb, | |
633 edge e0, edge e1, gimple phi, | |
634 tree arg0, tree arg1) | |
635 { | |
636 tree result, type; | |
637 gimple cond, new_stmt; | |
638 edge true_edge, false_edge; | |
639 enum tree_code cmp, minmax, ass_code; | |
640 tree smaller, larger, arg_true, arg_false; | |
641 gimple_stmt_iterator gsi, gsi_from; | |
642 | |
643 type = TREE_TYPE (PHI_RESULT (phi)); | |
644 | |
645 /* The optimization may be unsafe due to NaNs. */ | |
646 if (HONOR_NANS (TYPE_MODE (type))) | |
647 return false; | |
648 | |
649 cond = last_stmt (cond_bb); | |
650 cmp = gimple_cond_code (cond); | |
651 result = PHI_RESULT (phi); | |
652 | |
653 /* This transformation is only valid for order comparisons. Record which | |
654 operand is smaller/larger if the result of the comparison is true. */ | |
655 if (cmp == LT_EXPR || cmp == LE_EXPR) | |
656 { | |
657 smaller = gimple_cond_lhs (cond); | |
658 larger = gimple_cond_rhs (cond); | |
659 } | |
660 else if (cmp == GT_EXPR || cmp == GE_EXPR) | |
661 { | |
662 smaller = gimple_cond_rhs (cond); | |
663 larger = gimple_cond_lhs (cond); | |
664 } | |
665 else | |
666 return false; | |
667 | |
668 /* We need to know which is the true edge and which is the false | |
669 edge so that we know if have abs or negative abs. */ | |
670 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); | |
671 | |
672 /* Forward the edges over the middle basic block. */ | |
673 if (true_edge->dest == middle_bb) | |
674 true_edge = EDGE_SUCC (true_edge->dest, 0); | |
675 if (false_edge->dest == middle_bb) | |
676 false_edge = EDGE_SUCC (false_edge->dest, 0); | |
677 | |
678 if (true_edge == e0) | |
679 { | |
680 gcc_assert (false_edge == e1); | |
681 arg_true = arg0; | |
682 arg_false = arg1; | |
683 } | |
684 else | |
685 { | |
686 gcc_assert (false_edge == e0); | |
687 gcc_assert (true_edge == e1); | |
688 arg_true = arg1; | |
689 arg_false = arg0; | |
690 } | |
691 | |
692 if (empty_block_p (middle_bb)) | |
693 { | |
694 if (operand_equal_for_phi_arg_p (arg_true, smaller) | |
695 && operand_equal_for_phi_arg_p (arg_false, larger)) | |
696 { | |
697 /* Case | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
698 |
0 | 699 if (smaller < larger) |
700 rslt = smaller; | |
701 else | |
702 rslt = larger; */ | |
703 minmax = MIN_EXPR; | |
704 } | |
705 else if (operand_equal_for_phi_arg_p (arg_false, smaller) | |
706 && operand_equal_for_phi_arg_p (arg_true, larger)) | |
707 minmax = MAX_EXPR; | |
708 else | |
709 return false; | |
710 } | |
711 else | |
712 { | |
713 /* Recognize the following case, assuming d <= u: | |
714 | |
715 if (a <= u) | |
716 b = MAX (a, d); | |
717 x = PHI <b, u> | |
718 | |
719 This is equivalent to | |
720 | |
721 b = MAX (a, d); | |
722 x = MIN (b, u); */ | |
723 | |
724 gimple assign = last_and_only_stmt (middle_bb); | |
725 tree lhs, op0, op1, bound; | |
726 | |
727 if (!assign | |
728 || gimple_code (assign) != GIMPLE_ASSIGN) | |
729 return false; | |
730 | |
731 lhs = gimple_assign_lhs (assign); | |
732 ass_code = gimple_assign_rhs_code (assign); | |
733 if (ass_code != MAX_EXPR && ass_code != MIN_EXPR) | |
734 return false; | |
735 op0 = gimple_assign_rhs1 (assign); | |
736 op1 = gimple_assign_rhs2 (assign); | |
737 | |
738 if (true_edge->src == middle_bb) | |
739 { | |
740 /* We got here if the condition is true, i.e., SMALLER < LARGER. */ | |
741 if (!operand_equal_for_phi_arg_p (lhs, arg_true)) | |
742 return false; | |
743 | |
744 if (operand_equal_for_phi_arg_p (arg_false, larger)) | |
745 { | |
746 /* Case | |
747 | |
748 if (smaller < larger) | |
749 { | |
750 r' = MAX_EXPR (smaller, bound) | |
751 } | |
752 r = PHI <r', larger> --> to be turned to MIN_EXPR. */ | |
753 if (ass_code != MAX_EXPR) | |
754 return false; | |
755 | |
756 minmax = MIN_EXPR; | |
757 if (operand_equal_for_phi_arg_p (op0, smaller)) | |
758 bound = op1; | |
759 else if (operand_equal_for_phi_arg_p (op1, smaller)) | |
760 bound = op0; | |
761 else | |
762 return false; | |
763 | |
764 /* We need BOUND <= LARGER. */ | |
765 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, | |
766 bound, larger))) | |
767 return false; | |
768 } | |
769 else if (operand_equal_for_phi_arg_p (arg_false, smaller)) | |
770 { | |
771 /* Case | |
772 | |
773 if (smaller < larger) | |
774 { | |
775 r' = MIN_EXPR (larger, bound) | |
776 } | |
777 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */ | |
778 if (ass_code != MIN_EXPR) | |
779 return false; | |
780 | |
781 minmax = MAX_EXPR; | |
782 if (operand_equal_for_phi_arg_p (op0, larger)) | |
783 bound = op1; | |
784 else if (operand_equal_for_phi_arg_p (op1, larger)) | |
785 bound = op0; | |
786 else | |
787 return false; | |
788 | |
789 /* We need BOUND >= SMALLER. */ | |
790 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, | |
791 bound, smaller))) | |
792 return false; | |
793 } | |
794 else | |
795 return false; | |
796 } | |
797 else | |
798 { | |
799 /* We got here if the condition is false, i.e., SMALLER > LARGER. */ | |
800 if (!operand_equal_for_phi_arg_p (lhs, arg_false)) | |
801 return false; | |
802 | |
803 if (operand_equal_for_phi_arg_p (arg_true, larger)) | |
804 { | |
805 /* Case | |
806 | |
807 if (smaller > larger) | |
808 { | |
809 r' = MIN_EXPR (smaller, bound) | |
810 } | |
811 r = PHI <r', larger> --> to be turned to MAX_EXPR. */ | |
812 if (ass_code != MIN_EXPR) | |
813 return false; | |
814 | |
815 minmax = MAX_EXPR; | |
816 if (operand_equal_for_phi_arg_p (op0, smaller)) | |
817 bound = op1; | |
818 else if (operand_equal_for_phi_arg_p (op1, smaller)) | |
819 bound = op0; | |
820 else | |
821 return false; | |
822 | |
823 /* We need BOUND >= LARGER. */ | |
824 if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, | |
825 bound, larger))) | |
826 return false; | |
827 } | |
828 else if (operand_equal_for_phi_arg_p (arg_true, smaller)) | |
829 { | |
830 /* Case | |
831 | |
832 if (smaller > larger) | |
833 { | |
834 r' = MAX_EXPR (larger, bound) | |
835 } | |
836 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */ | |
837 if (ass_code != MAX_EXPR) | |
838 return false; | |
839 | |
840 minmax = MIN_EXPR; | |
841 if (operand_equal_for_phi_arg_p (op0, larger)) | |
842 bound = op1; | |
843 else if (operand_equal_for_phi_arg_p (op1, larger)) | |
844 bound = op0; | |
845 else | |
846 return false; | |
847 | |
848 /* We need BOUND <= SMALLER. */ | |
849 if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, | |
850 bound, smaller))) | |
851 return false; | |
852 } | |
853 else | |
854 return false; | |
855 } | |
856 | |
857 /* Move the statement from the middle block. */ | |
858 gsi = gsi_last_bb (cond_bb); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
859 gsi_from = gsi_last_nondebug_bb (middle_bb); |
0 | 860 gsi_move_before (&gsi_from, &gsi); |
861 } | |
862 | |
863 /* Emit the statement to compute min/max. */ | |
864 result = duplicate_ssa_name (PHI_RESULT (phi), NULL); | |
865 new_stmt = gimple_build_assign_with_ops (minmax, result, arg0, arg1); | |
866 gsi = gsi_last_bb (cond_bb); | |
867 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT); | |
868 | |
869 replace_phi_edge_with_variable (cond_bb, e1, phi, result); | |
870 return true; | |
871 } | |
872 | |
873 /* The function absolute_replacement does the main work of doing the absolute | |
874 replacement. Return true if the replacement is done. Otherwise return | |
875 false. | |
876 bb is the basic block where the replacement is going to be done on. arg0 | |
877 is argument 0 from the phi. Likewise for arg1. */ | |
878 | |
879 static bool | |
880 abs_replacement (basic_block cond_bb, basic_block middle_bb, | |
881 edge e0 ATTRIBUTE_UNUSED, edge e1, | |
882 gimple phi, tree arg0, tree arg1) | |
883 { | |
884 tree result; | |
885 gimple new_stmt, cond; | |
886 gimple_stmt_iterator gsi; | |
887 edge true_edge, false_edge; | |
888 gimple assign; | |
889 edge e; | |
890 tree rhs, lhs; | |
891 bool negate; | |
892 enum tree_code cond_code; | |
893 | |
894 /* If the type says honor signed zeros we cannot do this | |
895 optimization. */ | |
896 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1)))) | |
897 return false; | |
898 | |
899 /* OTHER_BLOCK must have only one executable statement which must have the | |
900 form arg0 = -arg1 or arg1 = -arg0. */ | |
901 | |
902 assign = last_and_only_stmt (middle_bb); | |
903 /* If we did not find the proper negation assignment, then we can not | |
904 optimize. */ | |
905 if (assign == NULL) | |
906 return false; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
907 |
0 | 908 /* If we got here, then we have found the only executable statement |
909 in OTHER_BLOCK. If it is anything other than arg = -arg1 or | |
910 arg1 = -arg0, then we can not optimize. */ | |
911 if (gimple_code (assign) != GIMPLE_ASSIGN) | |
912 return false; | |
913 | |
914 lhs = gimple_assign_lhs (assign); | |
915 | |
916 if (gimple_assign_rhs_code (assign) != NEGATE_EXPR) | |
917 return false; | |
918 | |
919 rhs = gimple_assign_rhs1 (assign); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
920 |
0 | 921 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */ |
922 if (!(lhs == arg0 && rhs == arg1) | |
923 && !(lhs == arg1 && rhs == arg0)) | |
924 return false; | |
925 | |
926 cond = last_stmt (cond_bb); | |
927 result = PHI_RESULT (phi); | |
928 | |
929 /* Only relationals comparing arg[01] against zero are interesting. */ | |
930 cond_code = gimple_cond_code (cond); | |
931 if (cond_code != GT_EXPR && cond_code != GE_EXPR | |
932 && cond_code != LT_EXPR && cond_code != LE_EXPR) | |
933 return false; | |
934 | |
935 /* Make sure the conditional is arg[01] OP y. */ | |
936 if (gimple_cond_lhs (cond) != rhs) | |
937 return false; | |
938 | |
939 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond))) | |
940 ? real_zerop (gimple_cond_rhs (cond)) | |
941 : integer_zerop (gimple_cond_rhs (cond))) | |
942 ; | |
943 else | |
944 return false; | |
945 | |
946 /* We need to know which is the true edge and which is the false | |
947 edge so that we know if have abs or negative abs. */ | |
948 extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); | |
949 | |
950 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we | |
951 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if | |
952 the false edge goes to OTHER_BLOCK. */ | |
953 if (cond_code == GT_EXPR || cond_code == GE_EXPR) | |
954 e = true_edge; | |
955 else | |
956 e = false_edge; | |
957 | |
958 if (e->dest == middle_bb) | |
959 negate = true; | |
960 else | |
961 negate = false; | |
962 | |
963 result = duplicate_ssa_name (result, NULL); | |
964 | |
965 if (negate) | |
966 { | |
967 tree tmp = create_tmp_var (TREE_TYPE (result), NULL); | |
968 add_referenced_var (tmp); | |
969 lhs = make_ssa_name (tmp, NULL); | |
970 } | |
971 else | |
972 lhs = result; | |
973 | |
974 /* Build the modify expression with abs expression. */ | |
975 new_stmt = gimple_build_assign_with_ops (ABS_EXPR, lhs, rhs, NULL); | |
976 | |
977 gsi = gsi_last_bb (cond_bb); | |
978 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT); | |
979 | |
980 if (negate) | |
981 { | |
982 /* Get the right GSI. We want to insert after the recently | |
983 added ABS_EXPR statement (which we know is the first statement | |
984 in the block. */ | |
985 new_stmt = gimple_build_assign_with_ops (NEGATE_EXPR, result, lhs, NULL); | |
986 | |
987 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT); | |
988 } | |
989 | |
990 replace_phi_edge_with_variable (cond_bb, e1, phi, result); | |
991 | |
992 /* Note that we optimized this PHI. */ | |
993 return true; | |
994 } | |
995 | |
996 /* Auxiliary functions to determine the set of memory accesses which | |
997 can't trap because they are preceded by accesses to the same memory | |
998 portion. We do that for INDIRECT_REFs, so we only need to track | |
999 the SSA_NAME of the pointer indirectly referenced. The algorithm | |
1000 simply is a walk over all instructions in dominator order. When | |
1001 we see an INDIRECT_REF we determine if we've already seen a same | |
1002 ref anywhere up to the root of the dominator tree. If we do the | |
1003 current access can't trap. If we don't see any dominating access | |
1004 the current access might trap, but might also make later accesses | |
1005 non-trapping, so we remember it. We need to be careful with loads | |
1006 or stores, for instance a load might not trap, while a store would, | |
1007 so if we see a dominating read access this doesn't mean that a later | |
1008 write access would not trap. Hence we also need to differentiate the | |
1009 type of access(es) seen. | |
1010 | |
1011 ??? We currently are very conservative and assume that a load might | |
1012 trap even if a store doesn't (write-only memory). This probably is | |
1013 overly conservative. */ | |
1014 | |
1015 /* A hash-table of SSA_NAMEs, and in which basic block an INDIRECT_REF | |
1016 through it was seen, which would constitute a no-trap region for | |
1017 same accesses. */ | |
1018 struct name_to_bb | |
1019 { | |
1020 tree ssa_name; | |
1021 basic_block bb; | |
1022 unsigned store : 1; | |
1023 }; | |
1024 | |
1025 /* The hash table for remembering what we've seen. */ | |
1026 static htab_t seen_ssa_names; | |
1027 | |
1028 /* The set of INDIRECT_REFs which can't trap. */ | |
1029 static struct pointer_set_t *nontrap_set; | |
1030 | |
1031 /* The hash function, based on the pointer to the pointer SSA_NAME. */ | |
1032 static hashval_t | |
1033 name_to_bb_hash (const void *p) | |
1034 { | |
1035 const_tree n = ((const struct name_to_bb *)p)->ssa_name; | |
1036 return htab_hash_pointer (n) ^ ((const struct name_to_bb *)p)->store; | |
1037 } | |
1038 | |
1039 /* The equality function of *P1 and *P2. SSA_NAMEs are shared, so | |
1040 it's enough to simply compare them for equality. */ | |
1041 static int | |
1042 name_to_bb_eq (const void *p1, const void *p2) | |
1043 { | |
1044 const struct name_to_bb *n1 = (const struct name_to_bb *)p1; | |
1045 const struct name_to_bb *n2 = (const struct name_to_bb *)p2; | |
1046 | |
1047 return n1->ssa_name == n2->ssa_name && n1->store == n2->store; | |
1048 } | |
1049 | |
1050 /* We see the expression EXP in basic block BB. If it's an interesting | |
1051 expression (an INDIRECT_REF through an SSA_NAME) possibly insert the | |
1052 expression into the set NONTRAP or the hash table of seen expressions. | |
1053 STORE is true if this expression is on the LHS, otherwise it's on | |
1054 the RHS. */ | |
1055 static void | |
1056 add_or_mark_expr (basic_block bb, tree exp, | |
1057 struct pointer_set_t *nontrap, bool store) | |
1058 { | |
1059 if (INDIRECT_REF_P (exp) | |
1060 && TREE_CODE (TREE_OPERAND (exp, 0)) == SSA_NAME) | |
1061 { | |
1062 tree name = TREE_OPERAND (exp, 0); | |
1063 struct name_to_bb map; | |
1064 void **slot; | |
1065 struct name_to_bb *n2bb; | |
1066 basic_block found_bb = 0; | |
1067 | |
1068 /* Try to find the last seen INDIRECT_REF through the same | |
1069 SSA_NAME, which can trap. */ | |
1070 map.ssa_name = name; | |
1071 map.bb = 0; | |
1072 map.store = store; | |
1073 slot = htab_find_slot (seen_ssa_names, &map, INSERT); | |
1074 n2bb = (struct name_to_bb *) *slot; | |
1075 if (n2bb) | |
1076 found_bb = n2bb->bb; | |
1077 | |
1078 /* If we've found a trapping INDIRECT_REF, _and_ it dominates EXP | |
1079 (it's in a basic block on the path from us to the dominator root) | |
1080 then we can't trap. */ | |
1081 if (found_bb && found_bb->aux == (void *)1) | |
1082 { | |
1083 pointer_set_insert (nontrap, exp); | |
1084 } | |
1085 else | |
1086 { | |
1087 /* EXP might trap, so insert it into the hash table. */ | |
1088 if (n2bb) | |
1089 { | |
1090 n2bb->bb = bb; | |
1091 } | |
1092 else | |
1093 { | |
1094 n2bb = XNEW (struct name_to_bb); | |
1095 n2bb->ssa_name = name; | |
1096 n2bb->bb = bb; | |
1097 n2bb->store = store; | |
1098 *slot = n2bb; | |
1099 } | |
1100 } | |
1101 } | |
1102 } | |
1103 | |
1104 /* Called by walk_dominator_tree, when entering the block BB. */ | |
1105 static void | |
1106 nt_init_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb) | |
1107 { | |
1108 gimple_stmt_iterator gsi; | |
1109 /* Mark this BB as being on the path to dominator root. */ | |
1110 bb->aux = (void*)1; | |
1111 | |
1112 /* And walk the statements in order. */ | |
1113 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1114 { | |
1115 gimple stmt = gsi_stmt (gsi); | |
1116 | |
1117 if (is_gimple_assign (stmt)) | |
1118 { | |
1119 add_or_mark_expr (bb, gimple_assign_lhs (stmt), nontrap_set, true); | |
1120 add_or_mark_expr (bb, gimple_assign_rhs1 (stmt), nontrap_set, false); | |
1121 if (get_gimple_rhs_num_ops (gimple_assign_rhs_code (stmt)) > 1) | |
1122 add_or_mark_expr (bb, gimple_assign_rhs2 (stmt), nontrap_set, | |
1123 false); | |
1124 } | |
1125 } | |
1126 } | |
1127 | |
1128 /* Called by walk_dominator_tree, when basic block BB is exited. */ | |
1129 static void | |
1130 nt_fini_block (struct dom_walk_data *data ATTRIBUTE_UNUSED, basic_block bb) | |
1131 { | |
1132 /* This BB isn't on the path to dominator root anymore. */ | |
1133 bb->aux = NULL; | |
1134 } | |
1135 | |
1136 /* This is the entry point of gathering non trapping memory accesses. | |
1137 It will do a dominator walk over the whole function, and it will | |
1138 make use of the bb->aux pointers. It returns a set of trees | |
1139 (the INDIRECT_REFs itself) which can't trap. */ | |
1140 static struct pointer_set_t * | |
1141 get_non_trapping (void) | |
1142 { | |
1143 struct pointer_set_t *nontrap; | |
1144 struct dom_walk_data walk_data; | |
1145 | |
1146 nontrap = pointer_set_create (); | |
1147 seen_ssa_names = htab_create (128, name_to_bb_hash, name_to_bb_eq, | |
1148 free); | |
1149 /* We're going to do a dominator walk, so ensure that we have | |
1150 dominance information. */ | |
1151 calculate_dominance_info (CDI_DOMINATORS); | |
1152 | |
1153 /* Setup callbacks for the generic dominator tree walker. */ | |
1154 nontrap_set = nontrap; | |
1155 walk_data.dom_direction = CDI_DOMINATORS; | |
1156 walk_data.initialize_block_local_data = NULL; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1157 walk_data.before_dom_children = nt_init_block; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1158 walk_data.after_dom_children = nt_fini_block; |
0 | 1159 walk_data.global_data = NULL; |
1160 walk_data.block_local_data_size = 0; | |
1161 | |
1162 init_walk_dominator_tree (&walk_data); | |
1163 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR); | |
1164 fini_walk_dominator_tree (&walk_data); | |
1165 htab_delete (seen_ssa_names); | |
1166 | |
1167 return nontrap; | |
1168 } | |
1169 | |
1170 /* Do the main work of conditional store replacement. We already know | |
1171 that the recognized pattern looks like so: | |
1172 | |
1173 split: | |
1174 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1) | |
1175 MIDDLE_BB: | |
1176 something | |
1177 fallthrough (edge E0) | |
1178 JOIN_BB: | |
1179 some more | |
1180 | |
1181 We check that MIDDLE_BB contains only one store, that that store | |
1182 doesn't trap (not via NOTRAP, but via checking if an access to the same | |
1183 memory location dominates us) and that the store has a "simple" RHS. */ | |
1184 | |
1185 static bool | |
1186 cond_store_replacement (basic_block middle_bb, basic_block join_bb, | |
1187 edge e0, edge e1, struct pointer_set_t *nontrap) | |
1188 { | |
1189 gimple assign = last_and_only_stmt (middle_bb); | |
1190 tree lhs, rhs, name; | |
1191 gimple newphi, new_stmt; | |
1192 gimple_stmt_iterator gsi; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1193 source_location locus; |
0 | 1194 enum tree_code code; |
1195 | |
1196 /* Check if middle_bb contains of only one store. */ | |
1197 if (!assign | |
1198 || gimple_code (assign) != GIMPLE_ASSIGN) | |
1199 return false; | |
1200 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1201 locus = gimple_location (assign); |
0 | 1202 lhs = gimple_assign_lhs (assign); |
1203 rhs = gimple_assign_rhs1 (assign); | |
1204 if (!INDIRECT_REF_P (lhs)) | |
1205 return false; | |
1206 | |
1207 /* RHS is either a single SSA_NAME or a constant. */ | |
1208 code = gimple_assign_rhs_code (assign); | |
1209 if (get_gimple_rhs_class (code) != GIMPLE_SINGLE_RHS | |
1210 || (code != SSA_NAME && !is_gimple_min_invariant (rhs))) | |
1211 return false; | |
1212 /* Prove that we can move the store down. We could also check | |
1213 TREE_THIS_NOTRAP here, but in that case we also could move stores, | |
1214 whose value is not available readily, which we want to avoid. */ | |
1215 if (!pointer_set_contains (nontrap, lhs)) | |
1216 return false; | |
1217 | |
1218 /* Now we've checked the constraints, so do the transformation: | |
1219 1) Remove the single store. */ | |
1220 mark_symbols_for_renaming (assign); | |
1221 gsi = gsi_for_stmt (assign); | |
1222 gsi_remove (&gsi, true); | |
1223 | |
1224 /* 2) Create a temporary where we can store the old content | |
1225 of the memory touched by the store, if we need to. */ | |
1226 if (!condstoretemp || TREE_TYPE (lhs) != TREE_TYPE (condstoretemp)) | |
1227 { | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1228 condstoretemp = create_tmp_reg (TREE_TYPE (lhs), "cstore"); |
0 | 1229 get_var_ann (condstoretemp); |
1230 } | |
1231 add_referenced_var (condstoretemp); | |
1232 | |
1233 /* 3) Insert a load from the memory of the store to the temporary | |
1234 on the edge which did not contain the store. */ | |
1235 lhs = unshare_expr (lhs); | |
1236 new_stmt = gimple_build_assign (condstoretemp, lhs); | |
1237 name = make_ssa_name (condstoretemp, new_stmt); | |
1238 gimple_assign_set_lhs (new_stmt, name); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1239 gimple_set_location (new_stmt, locus); |
0 | 1240 mark_symbols_for_renaming (new_stmt); |
1241 gsi_insert_on_edge (e1, new_stmt); | |
1242 | |
1243 /* 4) Create a PHI node at the join block, with one argument | |
1244 holding the old RHS, and the other holding the temporary | |
1245 where we stored the old memory contents. */ | |
1246 newphi = create_phi_node (condstoretemp, join_bb); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1247 add_phi_arg (newphi, rhs, e0, locus); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1248 add_phi_arg (newphi, name, e1, locus); |
0 | 1249 |
1250 lhs = unshare_expr (lhs); | |
1251 new_stmt = gimple_build_assign (lhs, PHI_RESULT (newphi)); | |
1252 mark_symbols_for_renaming (new_stmt); | |
1253 | |
1254 /* 5) Insert that PHI node. */ | |
1255 gsi = gsi_after_labels (join_bb); | |
1256 if (gsi_end_p (gsi)) | |
1257 { | |
1258 gsi = gsi_last_bb (join_bb); | |
1259 gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT); | |
1260 } | |
1261 else | |
1262 gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT); | |
1263 | |
1264 return true; | |
1265 } | |
1266 | |
1267 /* Always do these optimizations if we have SSA | |
1268 trees to work on. */ | |
1269 static bool | |
1270 gate_phiopt (void) | |
1271 { | |
1272 return 1; | |
1273 } | |
1274 | |
1275 struct gimple_opt_pass pass_phiopt = | |
1276 { | |
1277 { | |
1278 GIMPLE_PASS, | |
1279 "phiopt", /* name */ | |
1280 gate_phiopt, /* gate */ | |
1281 tree_ssa_phiopt, /* execute */ | |
1282 NULL, /* sub */ | |
1283 NULL, /* next */ | |
1284 0, /* static_pass_number */ | |
1285 TV_TREE_PHIOPT, /* tv_id */ | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1286 PROP_cfg | PROP_ssa, /* properties_required */ |
0 | 1287 0, /* properties_provided */ |
1288 0, /* properties_destroyed */ | |
1289 0, /* todo_flags_start */ | |
1290 TODO_dump_func | |
1291 | TODO_ggc_collect | |
1292 | TODO_verify_ssa | |
1293 | TODO_verify_flow | |
1294 | TODO_verify_stmts /* todo_flags_finish */ | |
1295 } | |
1296 }; | |
1297 | |
1298 static bool | |
1299 gate_cselim (void) | |
1300 { | |
1301 return flag_tree_cselim; | |
1302 } | |
1303 | |
1304 struct gimple_opt_pass pass_cselim = | |
1305 { | |
1306 { | |
1307 GIMPLE_PASS, | |
1308 "cselim", /* name */ | |
1309 gate_cselim, /* gate */ | |
1310 tree_ssa_cs_elim, /* execute */ | |
1311 NULL, /* sub */ | |
1312 NULL, /* next */ | |
1313 0, /* static_pass_number */ | |
1314 TV_TREE_PHIOPT, /* tv_id */ | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1315 PROP_cfg | PROP_ssa, /* properties_required */ |
0 | 1316 0, /* properties_provided */ |
1317 0, /* properties_destroyed */ | |
1318 0, /* todo_flags_start */ | |
1319 TODO_dump_func | |
1320 | TODO_ggc_collect | |
1321 | TODO_verify_ssa | |
1322 | TODO_verify_flow | |
1323 | TODO_verify_stmts /* todo_flags_finish */ | |
1324 } | |
1325 }; |