comparison gcc/tree-ssa-pre.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE. 1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 Copyright (C) 2001-2017 Free Software Foundation, Inc. 2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher 3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 <stevenb@suse.de> 4 <stevenb@suse.de>
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
47 #include "tree-scalar-evolution.h" 47 #include "tree-scalar-evolution.h"
48 #include "params.h" 48 #include "params.h"
49 #include "dbgcnt.h" 49 #include "dbgcnt.h"
50 #include "domwalk.h" 50 #include "domwalk.h"
51 #include "tree-ssa-propagate.h" 51 #include "tree-ssa-propagate.h"
52 #include "tree-ssa-dce.h"
52 #include "tree-cfgcleanup.h" 53 #include "tree-cfgcleanup.h"
53 #include "alias.h" 54 #include "alias.h"
54 55
55 /* Even though this file is called tree-ssa-pre.c, we actually 56 /* Even though this file is called tree-ssa-pre.c, we actually
56 implement a bit more than just PRE here. All of them piggy-back 57 implement a bit more than just PRE here. All of them piggy-back
398 expression_for_id (unsigned int id) 399 expression_for_id (unsigned int id)
399 { 400 {
400 return expressions[id]; 401 return expressions[id];
401 } 402 }
402 403
403 /* Free the expression id field in all of our expressions,
404 and then destroy the expressions array. */
405
406 static void
407 clear_expression_ids (void)
408 {
409 expressions.release ();
410 }
411
412 static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes"); 404 static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes");
413 405
414 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */ 406 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
415 407
416 static pre_expr 408 static pre_expr
669 break; 661 break;
670 case NAME: 662 case NAME:
671 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id; 663 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
672 break; 664 break;
673 case NARY: 665 case NARY:
666 gcc_assert (!PRE_EXPR_NARY (expr)->predicated_values);
674 id = PRE_EXPR_NARY (expr)->value_id; 667 id = PRE_EXPR_NARY (expr)->value_id;
675 break; 668 break;
676 case REFERENCE: 669 case REFERENCE:
677 id = PRE_EXPR_REFERENCE (expr)->value_id; 670 id = PRE_EXPR_REFERENCE (expr)->value_id;
678 break; 671 break;
683 we assign value-ids only to expressions that have a result 676 we assign value-ids only to expressions that have a result
684 in set_hashtable_value_ids. */ 677 in set_hashtable_value_ids. */
685 return id; 678 return id;
686 } 679 }
687 680
688 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */ 681 /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
689 682
690 static tree 683 static tree
691 sccvn_valnum_from_value_id (unsigned int val) 684 vn_valnum_from_value_id (unsigned int val)
692 { 685 {
693 bitmap_iterator bi; 686 bitmap_iterator bi;
694 unsigned int i; 687 unsigned int i;
695 bitmap exprset = value_expressions[val]; 688 bitmap exprset = value_expressions[val];
696 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi) 689 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
700 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum; 693 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
701 else if (vexpr->kind == CONSTANT) 694 else if (vexpr->kind == CONSTANT)
702 return PRE_EXPR_CONSTANT (vexpr); 695 return PRE_EXPR_CONSTANT (vexpr);
703 } 696 }
704 return NULL_TREE; 697 return NULL_TREE;
705 }
706
707 /* Remove an expression EXPR from a bitmapped set. */
708
709 static void
710 bitmap_remove_expr_from_set (bitmap_set_t set, pre_expr expr)
711 {
712 unsigned int val = get_expr_value_id (expr);
713 bitmap_clear_bit (&set->values, val);
714 bitmap_clear_bit (&set->expressions, get_expression_id (expr));
715 } 698 }
716 699
717 /* Insert an expression EXPR into a bitmapped set. */ 700 /* Insert an expression EXPR into a bitmapped set. */
718 701
719 static void 702 static void
811 static void 794 static void
812 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b) 795 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
813 { 796 {
814 unsigned int i; 797 unsigned int i;
815 bitmap_iterator bi; 798 bitmap_iterator bi;
816 pre_expr to_remove = NULL; 799 unsigned to_remove = -1U;
800 bitmap_and_compl_into (&a->values, &b->values);
817 FOR_EACH_EXPR_ID_IN_SET (a, i, bi) 801 FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
818 { 802 {
819 if (to_remove) 803 if (to_remove != -1U)
820 { 804 {
821 bitmap_remove_expr_from_set (a, to_remove); 805 bitmap_clear_bit (&a->expressions, to_remove);
822 to_remove = NULL; 806 to_remove = -1U;
823 } 807 }
824 pre_expr expr = expression_for_id (i); 808 pre_expr expr = expression_for_id (i);
825 if (bitmap_bit_p (&b->values, get_expr_value_id (expr))) 809 if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
826 to_remove = expr; 810 to_remove = i;
827 } 811 }
828 if (to_remove) 812 if (to_remove != -1U)
829 bitmap_remove_expr_from_set (a, to_remove); 813 bitmap_clear_bit (&a->expressions, to_remove);
830 } 814 }
831 815
832 816
833 /* Return true if bitmapped set SET contains the value VALUE_ID. */ 817 /* Return true if bitmapped set SET contains the value VALUE_ID. */
834 818
837 { 821 {
838 if (value_id_constant_p (value_id)) 822 if (value_id_constant_p (value_id))
839 return true; 823 return true;
840 824
841 return bitmap_bit_p (&set->values, value_id); 825 return bitmap_bit_p (&set->values, value_id);
842 }
843
844 static inline bool
845 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
846 {
847 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
848 } 826 }
849 827
850 /* Return true if two bitmap sets are equal. */ 828 /* Return true if two bitmap sets are equal. */
851 829
852 static bool 830 static bool
1235 1213
1236 static inline pre_expr 1214 static inline pre_expr
1237 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2, 1215 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1238 bitmap_set_t set3 = NULL) 1216 bitmap_set_t set3 = NULL)
1239 { 1217 {
1240 pre_expr result; 1218 pre_expr result = NULL;
1241 1219
1242 result = bitmap_find_leader (set1, val); 1220 if (set1)
1221 result = bitmap_find_leader (set1, val);
1243 if (!result && set2) 1222 if (!result && set2)
1244 result = bitmap_find_leader (set2, val); 1223 result = bitmap_find_leader (set2, val);
1245 if (!result && set3) 1224 if (!result && set3)
1246 result = bitmap_find_leader (set3, val); 1225 result = bitmap_find_leader (set3, val);
1247 return result; 1226 return result;
1264 return PRE_EXPR_NARY (e)->type; 1243 return PRE_EXPR_NARY (e)->type;
1265 } 1244 }
1266 gcc_unreachable (); 1245 gcc_unreachable ();
1267 } 1246 }
1268 1247
1269 /* Get a representative SSA_NAME for a given expression. 1248 /* Get a representative SSA_NAME for a given expression that is available in B.
1270 Since all of our sub-expressions are treated as values, we require 1249 Since all of our sub-expressions are treated as values, we require
1271 them to be SSA_NAME's for simplicity. 1250 them to be SSA_NAME's for simplicity.
1272 Prior versions of GVNPRE used to use "value handles" here, so that 1251 Prior versions of GVNPRE used to use "value handles" here, so that
1273 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In 1252 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1274 either case, the operands are really values (IE we do not expect 1253 either case, the operands are really values (IE we do not expect
1275 them to be usable without finding leaders). */ 1254 them to be usable without finding leaders). */
1276 1255
1277 static tree 1256 static tree
1278 get_representative_for (const pre_expr e) 1257 get_representative_for (const pre_expr e, basic_block b = NULL)
1279 { 1258 {
1280 tree name; 1259 tree name, valnum = NULL_TREE;
1281 unsigned int value_id = get_expr_value_id (e); 1260 unsigned int value_id = get_expr_value_id (e);
1282 1261
1283 switch (e->kind) 1262 switch (e->kind)
1284 { 1263 {
1285 case NAME: 1264 case NAME:
1296 bitmap exprs = value_expressions[value_id]; 1275 bitmap exprs = value_expressions[value_id];
1297 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi) 1276 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1298 { 1277 {
1299 pre_expr rep = expression_for_id (i); 1278 pre_expr rep = expression_for_id (i);
1300 if (rep->kind == NAME) 1279 if (rep->kind == NAME)
1301 return VN_INFO (PRE_EXPR_NAME (rep))->valnum; 1280 {
1281 tree name = PRE_EXPR_NAME (rep);
1282 valnum = VN_INFO (name)->valnum;
1283 gimple *def = SSA_NAME_DEF_STMT (name);
1284 /* We have to return either a new representative or one
1285 that can be used for expression simplification and thus
1286 is available in B. */
1287 if (! b
1288 || gimple_nop_p (def)
1289 || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1290 return name;
1291 }
1302 else if (rep->kind == CONSTANT) 1292 else if (rep->kind == CONSTANT)
1303 return PRE_EXPR_CONSTANT (rep); 1293 return PRE_EXPR_CONSTANT (rep);
1304 } 1294 }
1305 } 1295 }
1306 break; 1296 break;
1311 the program as set to an SSA_NAME, as the result of phi translation. 1301 the program as set to an SSA_NAME, as the result of phi translation.
1312 Create one here. 1302 Create one here.
1313 ??? We should be able to re-use this when we insert the statement 1303 ??? We should be able to re-use this when we insert the statement
1314 to compute it. */ 1304 to compute it. */
1315 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp"); 1305 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1316 VN_INFO_GET (name)->value_id = value_id; 1306 VN_INFO (name)->value_id = value_id;
1317 VN_INFO (name)->valnum = name; 1307 VN_INFO (name)->valnum = valnum ? valnum : name;
1318 /* ??? For now mark this SSA name for release by SCCVN. */ 1308 /* ??? For now mark this SSA name for release by VN. */
1319 VN_INFO (name)->needs_insertion = true; 1309 VN_INFO (name)->needs_insertion = true;
1320 add_to_value (value_id, get_or_alloc_expr_for_name (name)); 1310 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1321 if (dump_file && (dump_flags & TDF_DETAILS)) 1311 if (dump_file && (dump_flags & TDF_DETAILS))
1322 { 1312 {
1323 fprintf (dump_file, "Created SSA_NAME representative "); 1313 fprintf (dump_file, "Created SSA_NAME representative ");
1329 1319
1330 return name; 1320 return name;
1331 } 1321 }
1332 1322
1333 1323
1334
1335 static pre_expr 1324 static pre_expr
1336 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, 1325 phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1337 basic_block pred, basic_block phiblock);
1338 1326
1339 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of 1327 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1340 the phis in PRED. Return NULL if we can't find a leader for each part 1328 the phis in PRED. Return NULL if we can't find a leader for each part
1341 of the translated expression. */ 1329 of the translated expression. */
1342 1330
1343 static pre_expr 1331 static pre_expr
1344 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, 1332 phi_translate_1 (bitmap_set_t dest,
1345 basic_block pred, basic_block phiblock) 1333 pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1346 { 1334 {
1335 basic_block pred = e->src;
1336 basic_block phiblock = e->dest;
1347 switch (expr->kind) 1337 switch (expr->kind)
1348 { 1338 {
1349 case NARY: 1339 case NARY:
1350 { 1340 {
1351 unsigned int i; 1341 unsigned int i;
1362 else 1352 else
1363 { 1353 {
1364 pre_expr leader, result; 1354 pre_expr leader, result;
1365 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id; 1355 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1366 leader = find_leader_in_sets (op_val_id, set1, set2); 1356 leader = find_leader_in_sets (op_val_id, set1, set2);
1367 result = phi_translate (leader, set1, set2, pred, phiblock); 1357 result = phi_translate (dest, leader, set1, set2, e);
1368 if (result && result != leader) 1358 if (result && result != leader)
1369 newnary->op[i] = get_representative_for (result); 1359 /* If op has a leader in the sets we translate make
1360 sure to use the value of the translated expression.
1361 We might need a new representative for that. */
1362 newnary->op[i] = get_representative_for (result, pred);
1370 else if (!result) 1363 else if (!result)
1371 return NULL; 1364 return NULL;
1372 1365
1373 changed |= newnary->op[i] != nary->op[i]; 1366 changed |= newnary->op[i] != nary->op[i];
1374 } 1367 }
1391 /* Do not allow simplifications to non-constants over 1384 /* Do not allow simplifications to non-constants over
1392 backedges as this will likely result in a loop PHI node 1385 backedges as this will likely result in a loop PHI node
1393 to be inserted and increased register pressure. 1386 to be inserted and increased register pressure.
1394 See PR77498 - this avoids doing predcoms work in 1387 See PR77498 - this avoids doing predcoms work in
1395 a less efficient way. */ 1388 a less efficient way. */
1396 if (find_edge (pred, phiblock)->flags & EDGE_DFS_BACK) 1389 if (e->flags & EDGE_DFS_BACK)
1397 ; 1390 ;
1398 else 1391 else
1399 { 1392 {
1400 unsigned value_id = get_expr_value_id (constant); 1393 unsigned value_id = get_expr_value_id (constant);
1401 constant = find_leader_in_sets (value_id, set1, set2, 1394 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1395 dest has what we computed into ANTIC_OUT sofar
1396 so pick from that - since topological sorting
1397 by sorted_array_from_bitmap_set isn't perfect
1398 we may lose some cases here. */
1399 constant = find_leader_in_sets (value_id, dest,
1402 AVAIL_OUT (pred)); 1400 AVAIL_OUT (pred));
1403 if (constant) 1401 if (constant)
1404 return constant; 1402 {
1403 if (dump_file && (dump_flags & TDF_DETAILS))
1404 {
1405 fprintf (dump_file, "simplifying ");
1406 print_pre_expr (dump_file, expr);
1407 fprintf (dump_file, " translated %d -> %d to ",
1408 phiblock->index, pred->index);
1409 PRE_EXPR_NARY (expr) = newnary;
1410 print_pre_expr (dump_file, expr);
1411 PRE_EXPR_NARY (expr) = nary;
1412 fprintf (dump_file, " to ");
1413 print_pre_expr (dump_file, constant);
1414 fprintf (dump_file, "\n");
1415 }
1416 return constant;
1417 }
1405 } 1418 }
1406 } 1419 }
1407 else 1420 else
1408 return constant; 1421 return constant;
1409 } 1422 }
1410 1423
1424 /* vn_nary_* do not valueize operands. */
1425 for (i = 0; i < newnary->length; ++i)
1426 if (TREE_CODE (newnary->op[i]) == SSA_NAME)
1427 newnary->op[i] = VN_INFO (newnary->op[i])->valnum;
1411 tree result = vn_nary_op_lookup_pieces (newnary->length, 1428 tree result = vn_nary_op_lookup_pieces (newnary->length,
1412 newnary->opcode, 1429 newnary->opcode,
1413 newnary->type, 1430 newnary->type,
1414 &newnary->op[0], 1431 &newnary->op[0],
1415 &nary); 1432 &nary);
1417 return get_or_alloc_expr_for_constant (result); 1434 return get_or_alloc_expr_for_constant (result);
1418 1435
1419 expr = pre_expr_pool.allocate (); 1436 expr = pre_expr_pool.allocate ();
1420 expr->kind = NARY; 1437 expr->kind = NARY;
1421 expr->id = 0; 1438 expr->id = 0;
1422 if (nary) 1439 if (nary && !nary->predicated_values)
1423 { 1440 {
1424 PRE_EXPR_NARY (expr) = nary; 1441 PRE_EXPR_NARY (expr) = nary;
1425 new_val_id = nary->value_id; 1442 new_val_id = nary->value_id;
1426 get_or_alloc_expression_id (expr); 1443 get_or_alloc_expression_id (expr);
1427 /* When we end up re-using a value number make sure that
1428 doesn't have unrelated (which we can't check here)
1429 range or points-to info on it. */
1430 if (result
1431 && INTEGRAL_TYPE_P (TREE_TYPE (result))
1432 && SSA_NAME_RANGE_INFO (result)
1433 && ! SSA_NAME_IS_DEFAULT_DEF (result))
1434 {
1435 if (! VN_INFO (result)->info.range_info)
1436 {
1437 VN_INFO (result)->info.range_info
1438 = SSA_NAME_RANGE_INFO (result);
1439 VN_INFO (result)->range_info_anti_range_p
1440 = SSA_NAME_ANTI_RANGE_P (result);
1441 }
1442 if (dump_file && (dump_flags & TDF_DETAILS))
1443 {
1444 fprintf (dump_file, "clearing range info of ");
1445 print_generic_expr (dump_file, result);
1446 fprintf (dump_file, "\n");
1447 }
1448 SSA_NAME_RANGE_INFO (result) = NULL;
1449 }
1450 else if (result
1451 && POINTER_TYPE_P (TREE_TYPE (result))
1452 && SSA_NAME_PTR_INFO (result)
1453 && ! SSA_NAME_IS_DEFAULT_DEF (result))
1454 {
1455 if (! VN_INFO (result)->info.ptr_info)
1456 VN_INFO (result)->info.ptr_info
1457 = SSA_NAME_PTR_INFO (result);
1458 if (dump_file && (dump_flags & TDF_DETAILS))
1459 {
1460 fprintf (dump_file, "clearing points-to info of ");
1461 print_generic_expr (dump_file, result);
1462 fprintf (dump_file, "\n");
1463 }
1464 SSA_NAME_PTR_INFO (result) = NULL;
1465 }
1466 } 1444 }
1467 else 1445 else
1468 { 1446 {
1469 new_val_id = get_next_value_id (); 1447 new_val_id = get_next_value_id ();
1470 value_expressions.safe_grow_cleared (get_max_value_id () + 1); 1448 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1517 break; 1495 break;
1518 continue; 1496 continue;
1519 } 1497 }
1520 op_val_id = VN_INFO (op[n])->value_id; 1498 op_val_id = VN_INFO (op[n])->value_id;
1521 leader = find_leader_in_sets (op_val_id, set1, set2); 1499 leader = find_leader_in_sets (op_val_id, set1, set2);
1522 opresult = phi_translate (leader, set1, set2, pred, phiblock); 1500 opresult = phi_translate (dest, leader, set1, set2, e);
1523 if (opresult && opresult != leader) 1501 if (opresult && opresult != leader)
1524 { 1502 {
1525 tree name = get_representative_for (opresult); 1503 tree name = get_representative_for (opresult);
1526 changed |= name != op[n]; 1504 changed |= name != op[n];
1527 op[n] = name; 1505 op[n] = name;
1564 } 1542 }
1565 1543
1566 if (changed || newvuse != vuse) 1544 if (changed || newvuse != vuse)
1567 { 1545 {
1568 unsigned int new_val_id; 1546 unsigned int new_val_id;
1569 pre_expr constant;
1570 1547
1571 tree result = vn_reference_lookup_pieces (newvuse, ref->set, 1548 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1572 ref->type, 1549 ref->type,
1573 newoperands.exists () 1550 newoperands.exists ()
1574 ? newoperands : operands, 1551 ? newoperands : operands,
1609 expr = pre_expr_pool.allocate (); 1586 expr = pre_expr_pool.allocate ();
1610 expr->kind = REFERENCE; 1587 expr->kind = REFERENCE;
1611 expr->id = 0; 1588 expr->id = 0;
1612 1589
1613 if (newref) 1590 if (newref)
1614 { 1591 new_val_id = newref->value_id;
1615 PRE_EXPR_REFERENCE (expr) = newref;
1616 constant = fully_constant_expression (expr);
1617 if (constant != expr)
1618 return constant;
1619
1620 new_val_id = newref->value_id;
1621 get_or_alloc_expression_id (expr);
1622 }
1623 else 1592 else
1624 { 1593 {
1625 if (changed || !same_valid) 1594 if (changed || !same_valid)
1626 { 1595 {
1627 new_val_id = get_next_value_id (); 1596 new_val_id = get_next_value_id ();
1635 newref = vn_reference_insert_pieces (newvuse, ref->set, 1604 newref = vn_reference_insert_pieces (newvuse, ref->set,
1636 ref->type, 1605 ref->type,
1637 newoperands, 1606 newoperands,
1638 result, new_val_id); 1607 result, new_val_id);
1639 newoperands = vNULL; 1608 newoperands = vNULL;
1640 PRE_EXPR_REFERENCE (expr) = newref;
1641 constant = fully_constant_expression (expr);
1642 if (constant != expr)
1643 return constant;
1644 get_or_alloc_expression_id (expr);
1645 } 1609 }
1610 PRE_EXPR_REFERENCE (expr) = newref;
1611 get_or_alloc_expression_id (expr);
1646 add_to_value (new_val_id, expr); 1612 add_to_value (new_val_id, expr);
1647 } 1613 }
1648 newoperands.release (); 1614 newoperands.release ();
1649 return expr; 1615 return expr;
1650 } 1616 }
1657 /* If the SSA name is defined by a PHI node in this block, 1623 /* If the SSA name is defined by a PHI node in this block,
1658 translate it. */ 1624 translate it. */
1659 if (gimple_code (def_stmt) == GIMPLE_PHI 1625 if (gimple_code (def_stmt) == GIMPLE_PHI
1660 && gimple_bb (def_stmt) == phiblock) 1626 && gimple_bb (def_stmt) == phiblock)
1661 { 1627 {
1662 edge e = find_edge (pred, gimple_bb (def_stmt));
1663 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx); 1628 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1664 1629
1665 /* Handle constant. */ 1630 /* Handle constant. */
1666 if (is_gimple_min_invariant (def)) 1631 if (is_gimple_min_invariant (def))
1667 return get_or_alloc_expr_for_constant (def); 1632 return get_or_alloc_expr_for_constant (def);
1680 } 1645 }
1681 1646
1682 /* Wrapper around phi_translate_1 providing caching functionality. */ 1647 /* Wrapper around phi_translate_1 providing caching functionality. */
1683 1648
1684 static pre_expr 1649 static pre_expr
1685 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, 1650 phi_translate (bitmap_set_t dest, pre_expr expr,
1686 basic_block pred, basic_block phiblock) 1651 bitmap_set_t set1, bitmap_set_t set2, edge e)
1687 { 1652 {
1688 expr_pred_trans_t slot = NULL; 1653 expr_pred_trans_t slot = NULL;
1689 pre_expr phitrans; 1654 pre_expr phitrans;
1690 1655
1691 if (!expr) 1656 if (!expr)
1699 return expr; 1664 return expr;
1700 1665
1701 /* Don't add translations of NAMEs as those are cheap to translate. */ 1666 /* Don't add translations of NAMEs as those are cheap to translate. */
1702 if (expr->kind != NAME) 1667 if (expr->kind != NAME)
1703 { 1668 {
1704 if (phi_trans_add (&slot, expr, pred)) 1669 if (phi_trans_add (&slot, expr, e->src))
1705 return slot->v; 1670 return slot->v;
1706 /* Store NULL for the value we want to return in the case of 1671 /* Store NULL for the value we want to return in the case of
1707 recursing. */ 1672 recursing. */
1708 slot->v = NULL; 1673 slot->v = NULL;
1709 } 1674 }
1710 1675
1711 /* Translate. */ 1676 /* Translate. */
1712 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock); 1677 basic_block saved_valueize_bb = vn_context_bb;
1678 vn_context_bb = e->src;
1679 phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1680 vn_context_bb = saved_valueize_bb;
1713 1681
1714 if (slot) 1682 if (slot)
1715 { 1683 {
1716 if (phitrans) 1684 if (phitrans)
1717 slot->v = phitrans; 1685 slot->v = phitrans;
1728 /* For each expression in SET, translate the values through phi nodes 1696 /* For each expression in SET, translate the values through phi nodes
1729 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting 1697 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1730 expressions in DEST. */ 1698 expressions in DEST. */
1731 1699
1732 static void 1700 static void
1733 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred, 1701 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1734 basic_block phiblock)
1735 { 1702 {
1736 vec<pre_expr> exprs; 1703 vec<pre_expr> exprs;
1737 pre_expr expr; 1704 pre_expr expr;
1738 int i; 1705 int i;
1739 1706
1740 if (gimple_seq_empty_p (phi_nodes (phiblock))) 1707 if (gimple_seq_empty_p (phi_nodes (e->dest)))
1741 { 1708 {
1742 bitmap_set_copy (dest, set); 1709 bitmap_set_copy (dest, set);
1743 return; 1710 return;
1744 } 1711 }
1745 1712
1746 exprs = sorted_array_from_bitmap_set (set); 1713 exprs = sorted_array_from_bitmap_set (set);
1747 FOR_EACH_VEC_ELT (exprs, i, expr) 1714 FOR_EACH_VEC_ELT (exprs, i, expr)
1748 { 1715 {
1749 pre_expr translated; 1716 pre_expr translated;
1750 translated = phi_translate (expr, set, NULL, pred, phiblock); 1717 translated = phi_translate (dest, expr, set, NULL, e);
1751 if (!translated) 1718 if (!translated)
1752 continue; 1719 continue;
1753 1720
1754 /* We might end up with multiple expressions from SET being 1721 bitmap_insert_into_set (dest, translated);
1755 translated to the same value. In this case we do not want
1756 to retain the NARY or REFERENCE expression but prefer a NAME
1757 which would be the leader. */
1758 if (translated->kind == NAME)
1759 bitmap_value_replace_in_set (dest, translated);
1760 else
1761 bitmap_value_insert_into_set (dest, translated);
1762 } 1722 }
1763 exprs.release (); 1723 exprs.release ();
1764 } 1724 }
1765 1725
1766 /* Find the leader for a value (i.e., the name representing that 1726 /* Find the leader for a value (i.e., the name representing that
1959 int i; 1919 int i;
1960 1920
1961 FOR_EACH_VEC_ELT (exprs, i, expr) 1921 FOR_EACH_VEC_ELT (exprs, i, expr)
1962 { 1922 {
1963 if (!valid_in_sets (set1, set2, expr)) 1923 if (!valid_in_sets (set1, set2, expr))
1964 bitmap_remove_expr_from_set (set1, expr); 1924 {
1925 unsigned int val = get_expr_value_id (expr);
1926 bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1927 /* We are entered with possibly multiple expressions for a value
1928 so before removing a value from the set see if there's an
1929 expression for it left. */
1930 if (! bitmap_find_leader (set1, val))
1931 bitmap_clear_bit (&set1->values, val);
1932 }
1965 } 1933 }
1966 exprs.release (); 1934 exprs.release ();
1967 } 1935 }
1968 1936
1969 /* Clean the set of expressions that are no longer valid in SET because 1937 /* Clean the set of expressions that are no longer valid in SET because
1972 static void 1940 static void
1973 prune_clobbered_mems (bitmap_set_t set, basic_block block) 1941 prune_clobbered_mems (bitmap_set_t set, basic_block block)
1974 { 1942 {
1975 bitmap_iterator bi; 1943 bitmap_iterator bi;
1976 unsigned i; 1944 unsigned i;
1977 pre_expr to_remove = NULL; 1945 unsigned to_remove = -1U;
1946 bool any_removed = false;
1978 1947
1979 FOR_EACH_EXPR_ID_IN_SET (set, i, bi) 1948 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1980 { 1949 {
1981 /* Remove queued expr. */ 1950 /* Remove queued expr. */
1982 if (to_remove) 1951 if (to_remove != -1U)
1983 { 1952 {
1984 bitmap_remove_expr_from_set (set, to_remove); 1953 bitmap_clear_bit (&set->expressions, to_remove);
1985 to_remove = NULL; 1954 any_removed = true;
1955 to_remove = -1U;
1986 } 1956 }
1987 1957
1988 pre_expr expr = expression_for_id (i); 1958 pre_expr expr = expression_for_id (i);
1989 if (expr->kind == REFERENCE) 1959 if (expr->kind == REFERENCE)
1990 { 1960 {
1996 && ((gimple_bb (def_stmt) != block 1966 && ((gimple_bb (def_stmt) != block
1997 && !dominated_by_p (CDI_DOMINATORS, 1967 && !dominated_by_p (CDI_DOMINATORS,
1998 block, gimple_bb (def_stmt))) 1968 block, gimple_bb (def_stmt)))
1999 || (gimple_bb (def_stmt) == block 1969 || (gimple_bb (def_stmt) == block
2000 && value_dies_in_block_x (expr, block)))) 1970 && value_dies_in_block_x (expr, block))))
2001 to_remove = expr; 1971 to_remove = i;
2002 } 1972 }
2003 } 1973 }
2004 else if (expr->kind == NARY) 1974 else if (expr->kind == NARY)
2005 { 1975 {
2006 vn_nary_op_t nary = PRE_EXPR_NARY (expr); 1976 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2008 a possible exit point. 1978 a possible exit point.
2009 ??? This is overly conservative if we translate AVAIL_OUT 1979 ??? This is overly conservative if we translate AVAIL_OUT
2010 as the available expression might be after the exit point. */ 1980 as the available expression might be after the exit point. */
2011 if (BB_MAY_NOTRETURN (block) 1981 if (BB_MAY_NOTRETURN (block)
2012 && vn_nary_may_trap (nary)) 1982 && vn_nary_may_trap (nary))
2013 to_remove = expr; 1983 to_remove = i;
2014 } 1984 }
2015 } 1985 }
2016 1986
2017 /* Remove queued expr. */ 1987 /* Remove queued expr. */
2018 if (to_remove) 1988 if (to_remove != -1U)
2019 bitmap_remove_expr_from_set (set, to_remove); 1989 {
1990 bitmap_clear_bit (&set->expressions, to_remove);
1991 any_removed = true;
1992 }
1993
1994 /* Above we only removed expressions, now clean the set of values
1995 which no longer have any corresponding expression. We cannot
1996 clear the value at the time we remove an expression since there
1997 may be multiple expressions per value.
1998 If we'd queue possibly to be removed values we could use
1999 the bitmap_find_leader way to see if there's still an expression
2000 for it. For some ratio of to be removed values and number of
2001 values/expressions in the set this might be faster than rebuilding
2002 the value-set. */
2003 if (any_removed)
2004 {
2005 bitmap_clear (&set->values);
2006 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2007 {
2008 pre_expr expr = expression_for_id (i);
2009 unsigned int value_id = get_expr_value_id (expr);
2010 bitmap_set_bit (&set->values, value_id);
2011 }
2012 }
2020 } 2013 }
2021 2014
2022 static sbitmap has_abnormal_preds; 2015 static sbitmap has_abnormal_preds;
2023 2016
2024 /* Compute the ANTIC set for BLOCK. 2017 /* Compute the ANTIC set for BLOCK.
2027 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK) 2020 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2028 else if succs(BLOCK) == 1 then 2021 else if succs(BLOCK) == 1 then
2029 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) 2022 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2030 2023
2031 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK]) 2024 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2032 */ 2025
2026 Note that clean() is deferred until after the iteration. */
2033 2027
2034 static bool 2028 static bool
2035 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge) 2029 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2036 { 2030 {
2037 bitmap_set_t S, old, ANTIC_OUT; 2031 bitmap_set_t S, old, ANTIC_OUT;
2038 bitmap_iterator bi;
2039 unsigned int bii;
2040 edge e; 2032 edge e;
2041 edge_iterator ei; 2033 edge_iterator ei;
2042 2034
2035 bool was_visited = BB_VISITED (block);
2043 bool changed = ! BB_VISITED (block); 2036 bool changed = ! BB_VISITED (block);
2044 BB_VISITED (block) = 1; 2037 BB_VISITED (block) = 1;
2045 old = ANTIC_OUT = S = NULL; 2038 old = ANTIC_OUT = S = NULL;
2046 2039
2047 /* If any edges from predecessors are abnormal, antic_in is empty, 2040 /* If any edges from predecessors are abnormal, antic_in is empty,
2057 ; 2050 ;
2058 /* If we have one successor, we could have some phi nodes to 2051 /* If we have one successor, we could have some phi nodes to
2059 translate through. */ 2052 translate through. */
2060 else if (single_succ_p (block)) 2053 else if (single_succ_p (block))
2061 { 2054 {
2062 basic_block succ_bb = single_succ (block); 2055 e = single_succ_edge (block);
2063 gcc_assert (BB_VISITED (succ_bb)); 2056 gcc_assert (BB_VISITED (e->dest));
2064 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb); 2057 phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2065 } 2058 }
2066 /* If we have multiple successors, we take the intersection of all of 2059 /* If we have multiple successors, we take the intersection of all of
2067 them. Note that in the case of loop exit phi nodes, we may have 2060 them. Note that in the case of loop exit phi nodes, we may have
2068 phis to translate through. */ 2061 phis to translate through. */
2069 else 2062 else
2070 { 2063 {
2071 size_t i; 2064 size_t i;
2072 basic_block bprime, first = NULL; 2065 edge first = NULL;
2073 2066
2074 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs)); 2067 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2075 FOR_EACH_EDGE (e, ei, block->succs) 2068 FOR_EACH_EDGE (e, ei, block->succs)
2076 { 2069 {
2077 if (!first 2070 if (!first
2078 && BB_VISITED (e->dest)) 2071 && BB_VISITED (e->dest))
2079 first = e->dest; 2072 first = e;
2080 else if (BB_VISITED (e->dest)) 2073 else if (BB_VISITED (e->dest))
2081 worklist.quick_push (e->dest); 2074 worklist.quick_push (e);
2082 else 2075 else
2083 { 2076 {
2084 /* Unvisited successors get their ANTIC_IN replaced by the 2077 /* Unvisited successors get their ANTIC_IN replaced by the
2085 maximal set to arrive at a maximum ANTIC_IN solution. 2078 maximal set to arrive at a maximum ANTIC_IN solution.
2086 We can ignore them in the intersection operation and thus 2079 We can ignore them in the intersection operation and thus
2093 2086
2094 /* Of multiple successors we have to have visited one already 2087 /* Of multiple successors we have to have visited one already
2095 which is guaranteed by iteration order. */ 2088 which is guaranteed by iteration order. */
2096 gcc_assert (first != NULL); 2089 gcc_assert (first != NULL);
2097 2090
2098 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first); 2091 phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2099 2092
2100 /* If we have multiple successors we need to intersect the ANTIC_OUT 2093 /* If we have multiple successors we need to intersect the ANTIC_OUT
2101 sets. For values that's a simple intersection but for 2094 sets. For values that's a simple intersection but for
2102 expressions it is a union. Given we want to have a single 2095 expressions it is a union. Given we want to have a single
2103 expression per value in our sets we have to canonicalize. 2096 expression per value in our sets we have to canonicalize.
2104 Avoid randomness and running into cycles like for PR82129 and 2097 Avoid randomness and running into cycles like for PR82129 and
2105 canonicalize the expression we choose to the one with the 2098 canonicalize the expression we choose to the one with the
2106 lowest id. This requires we actually compute the union first. */ 2099 lowest id. This requires we actually compute the union first. */
2107 FOR_EACH_VEC_ELT (worklist, i, bprime) 2100 FOR_EACH_VEC_ELT (worklist, i, e)
2108 { 2101 {
2109 if (!gimple_seq_empty_p (phi_nodes (bprime))) 2102 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2110 { 2103 {
2111 bitmap_set_t tmp = bitmap_set_new (); 2104 bitmap_set_t tmp = bitmap_set_new ();
2112 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime); 2105 phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2113 bitmap_and_into (&ANTIC_OUT->values, &tmp->values); 2106 bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2114 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions); 2107 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2115 bitmap_set_free (tmp); 2108 bitmap_set_free (tmp);
2116 } 2109 }
2117 else 2110 else
2118 { 2111 {
2119 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (bprime)->values); 2112 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2120 bitmap_ior_into (&ANTIC_OUT->expressions, 2113 bitmap_ior_into (&ANTIC_OUT->expressions,
2121 &ANTIC_IN (bprime)->expressions); 2114 &ANTIC_IN (e->dest)->expressions);
2122 } 2115 }
2123 } 2116 }
2124 if (! worklist.is_empty ()) 2117 if (! worklist.is_empty ())
2125 { 2118 {
2126 /* Prune expressions not in the value set, canonicalizing to 2119 /* Prune expressions not in the value set. */
2127 expression with lowest ID. */
2128 bitmap_iterator bi; 2120 bitmap_iterator bi;
2129 unsigned int i; 2121 unsigned int i;
2130 unsigned int to_clear = -1U; 2122 unsigned int to_clear = -1U;
2131 bitmap seen_value = BITMAP_ALLOC (NULL);
2132 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi) 2123 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2133 { 2124 {
2134 if (to_clear != -1U) 2125 if (to_clear != -1U)
2135 { 2126 {
2136 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear); 2127 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2137 to_clear = -1U; 2128 to_clear = -1U;
2138 } 2129 }
2139 pre_expr expr = expression_for_id (i); 2130 pre_expr expr = expression_for_id (i);
2140 unsigned int value_id = get_expr_value_id (expr); 2131 unsigned int value_id = get_expr_value_id (expr);
2141 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id) 2132 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2142 || !bitmap_set_bit (seen_value, value_id))
2143 to_clear = i; 2133 to_clear = i;
2144 } 2134 }
2145 if (to_clear != -1U) 2135 if (to_clear != -1U)
2146 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear); 2136 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2147 BITMAP_FREE (seen_value);
2148 } 2137 }
2149 } 2138 }
2150 2139
2151 /* Prune expressions that are clobbered in block and thus become 2140 /* Prune expressions that are clobbered in block and thus become
2152 invalid if translated from ANTIC_OUT to ANTIC_IN. */ 2141 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2159 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block), 2148 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2160 TMP_GEN (block)); 2149 TMP_GEN (block));
2161 2150
2162 /* Then union in the ANTIC_OUT - TMP_GEN values, 2151 /* Then union in the ANTIC_OUT - TMP_GEN values,
2163 to get ANTIC_OUT U EXP_GEN - TMP_GEN */ 2152 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2164 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi) 2153 bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2165 bitmap_value_insert_into_set (ANTIC_IN (block), 2154 bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2166 expression_for_id (bii)); 2155
2167 2156 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2168 clean (ANTIC_IN (block)); 2157 because it can cause non-convergence, see for example PR81181. */
2158
2159 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2160 we properly represent the maximum expression set, thus not prune
2161 values without expressions during the iteration. */
2162 if (was_visited
2163 && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2164 {
2165 if (dump_file && (dump_flags & TDF_DETAILS))
2166 fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2167 "shrinks the set\n");
2168 /* Prune expressions not in the value set. */
2169 bitmap_iterator bi;
2170 unsigned int i;
2171 unsigned int to_clear = -1U;
2172 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2173 {
2174 if (to_clear != -1U)
2175 {
2176 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2177 to_clear = -1U;
2178 }
2179 pre_expr expr = expression_for_id (i);
2180 unsigned int value_id = get_expr_value_id (expr);
2181 if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2182 to_clear = i;
2183 }
2184 if (to_clear != -1U)
2185 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2186 }
2169 2187
2170 if (!bitmap_set_equal (old, ANTIC_IN (block))) 2188 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2171 changed = true; 2189 changed = true;
2172 2190
2173 maybe_dump_sets: 2191 maybe_dump_sets:
2241 the successors. For recurrences like IV's, we will end up 2259 the successors. For recurrences like IV's, we will end up
2242 generating a new value in the set on each go around (i + 3 (VH.1) 2260 generating a new value in the set on each go around (i + 3 (VH.1)
2243 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */ 2261 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2244 else if (single_succ_p (block)) 2262 else if (single_succ_p (block))
2245 { 2263 {
2246 basic_block succ = single_succ (block); 2264 e = single_succ_edge (block);
2247 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK)) 2265 if (!(e->flags & EDGE_DFS_BACK))
2248 phi_translate_set (PA_OUT, PA_IN (succ), block, succ); 2266 phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2249 } 2267 }
2250 /* If we have multiple successors, we take the union of all of 2268 /* If we have multiple successors, we take the union of all of
2251 them. */ 2269 them. */
2252 else 2270 else
2253 { 2271 {
2254 size_t i; 2272 size_t i;
2255 basic_block bprime; 2273
2256 2274 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2257 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2258 FOR_EACH_EDGE (e, ei, block->succs) 2275 FOR_EACH_EDGE (e, ei, block->succs)
2259 { 2276 {
2260 if (e->flags & EDGE_DFS_BACK) 2277 if (e->flags & EDGE_DFS_BACK)
2261 continue; 2278 continue;
2262 worklist.quick_push (e->dest); 2279 worklist.quick_push (e);
2263 } 2280 }
2264 if (worklist.length () > 0) 2281 if (worklist.length () > 0)
2265 { 2282 {
2266 FOR_EACH_VEC_ELT (worklist, i, bprime) 2283 FOR_EACH_VEC_ELT (worklist, i, e)
2267 { 2284 {
2268 unsigned int i; 2285 unsigned int i;
2269 bitmap_iterator bi; 2286 bitmap_iterator bi;
2270 2287
2271 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi) 2288 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2272 bitmap_value_insert_into_set (PA_OUT, 2289 bitmap_value_insert_into_set (PA_OUT,
2273 expression_for_id (i)); 2290 expression_for_id (i));
2274 if (!gimple_seq_empty_p (phi_nodes (bprime))) 2291 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2275 { 2292 {
2276 bitmap_set_t pa_in = bitmap_set_new (); 2293 bitmap_set_t pa_in = bitmap_set_new ();
2277 phi_translate_set (pa_in, PA_IN (bprime), block, bprime); 2294 phi_translate_set (pa_in, PA_IN (e->dest), e);
2278 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi) 2295 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2279 bitmap_value_insert_into_set (PA_OUT, 2296 bitmap_value_insert_into_set (PA_OUT,
2280 expression_for_id (i)); 2297 expression_for_id (i));
2281 bitmap_set_free (pa_in); 2298 bitmap_set_free (pa_in);
2282 } 2299 }
2283 else 2300 else
2284 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi) 2301 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2285 bitmap_value_insert_into_set (PA_OUT, 2302 bitmap_value_insert_into_set (PA_OUT,
2286 expression_for_id (i)); 2303 expression_for_id (i));
2287 } 2304 }
2288 } 2305 }
2289 } 2306 }
2395 } 2412 }
2396 /* Theoretically possible, but *highly* unlikely. */ 2413 /* Theoretically possible, but *highly* unlikely. */
2397 gcc_checking_assert (num_iterations < 500); 2414 gcc_checking_assert (num_iterations < 500);
2398 } 2415 }
2399 2416
2417 /* We have to clean after the dataflow problem converged as cleaning
2418 can cause non-convergence because it is based on expressions
2419 rather than values. */
2420 FOR_EACH_BB_FN (block, cfun)
2421 clean (ANTIC_IN (block));
2422
2400 statistics_histogram_event (cfun, "compute_antic iterations", 2423 statistics_histogram_event (cfun, "compute_antic iterations",
2401 num_iterations); 2424 num_iterations);
2402 2425
2403 if (do_partial_partial) 2426 if (do_partial_partial)
2404 { 2427 {
2405 /* For partial antic we ignore backedges and thus we do not need 2428 /* For partial antic we ignore backedges and thus we do not need
2406 to perform any iteration when we process blocks in postorder. */ 2429 to perform any iteration when we process blocks in postorder. */
2407 int postorder_num 2430 for (i = postorder.length () - 1; i >= 0; i--)
2408 = pre_and_rev_post_order_compute (NULL, postorder.address (), false);
2409 for (i = postorder_num - 1 ; i >= 0; i--)
2410 { 2431 {
2411 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]); 2432 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2412 compute_partial_antic_aux (block, 2433 compute_partial_antic_aux (block,
2413 bitmap_bit_p (has_abnormal_preds, 2434 bitmap_bit_p (has_abnormal_preds,
2414 block->index)); 2435 block->index));
2446 return NULL_TREE; 2467 return NULL_TREE;
2447 tree offset = currop->op0; 2468 tree offset = currop->op0;
2448 if (TREE_CODE (baseop) == ADDR_EXPR 2469 if (TREE_CODE (baseop) == ADDR_EXPR
2449 && handled_component_p (TREE_OPERAND (baseop, 0))) 2470 && handled_component_p (TREE_OPERAND (baseop, 0)))
2450 { 2471 {
2451 HOST_WIDE_INT off; 2472 poly_int64 off;
2452 tree base; 2473 tree base;
2453 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0), 2474 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2454 &off); 2475 &off);
2455 gcc_assert (base); 2476 gcc_assert (base);
2456 offset = int_const_binop (PLUS_EXPR, offset, 2477 offset = int_const_binop (PLUS_EXPR, offset,
2730 { 2751 {
2731 /* We may hit the NAME/CONSTANT case if we have to convert types 2752 /* We may hit the NAME/CONSTANT case if we have to convert types
2732 that value numbering saw through. */ 2753 that value numbering saw through. */
2733 case NAME: 2754 case NAME:
2734 folded = PRE_EXPR_NAME (expr); 2755 folded = PRE_EXPR_NAME (expr);
2756 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2757 return NULL_TREE;
2735 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded))) 2758 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2736 return folded; 2759 return folded;
2737 break; 2760 break;
2738 case CONSTANT: 2761 case CONSTANT:
2739 { 2762 {
2748 { 2771 {
2749 vn_reference_t ref = PRE_EXPR_REFERENCE (expr); 2772 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2750 unsigned int operand = 1; 2773 unsigned int operand = 1;
2751 vn_reference_op_t currop = &ref->operands[0]; 2774 vn_reference_op_t currop = &ref->operands[0];
2752 tree sc = NULL_TREE; 2775 tree sc = NULL_TREE;
2753 tree fn; 2776 tree fn = find_or_generate_expression (block, currop->op0, stmts);
2754 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2755 fn = currop->op0;
2756 else
2757 fn = find_or_generate_expression (block, currop->op0, stmts);
2758 if (!fn) 2777 if (!fn)
2759 return NULL_TREE; 2778 return NULL_TREE;
2760 if (currop->op1) 2779 if (currop->op1)
2761 { 2780 {
2762 sc = find_or_generate_expression (block, currop->op1, stmts); 2781 sc = find_or_generate_expression (block, currop->op1, stmts);
2770 &operand, stmts); 2789 &operand, stmts);
2771 if (!arg) 2790 if (!arg)
2772 return NULL_TREE; 2791 return NULL_TREE;
2773 args.quick_push (arg); 2792 args.quick_push (arg);
2774 } 2793 }
2775 gcall *call 2794 gcall *call = gimple_build_call_vec (fn, args);
2776 = gimple_build_call_vec ((TREE_CODE (fn) == FUNCTION_DECL
2777 ? build_fold_addr_expr (fn) : fn), args);
2778 gimple_call_set_with_bounds (call, currop->with_bounds);
2779 if (sc) 2795 if (sc)
2780 gimple_call_set_chain (call, sc); 2796 gimple_call_set_chain (call, sc);
2781 tree forcedname = make_ssa_name (currop->type); 2797 tree forcedname = make_ssa_name (currop->type);
2782 gimple_call_set_lhs (call, forcedname); 2798 gimple_call_set_lhs (call, forcedname);
2799 /* There's no CCP pass after PRE which would re-compute alignment
2800 information so make sure we re-materialize this here. */
2801 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2802 && args.length () - 2 <= 1
2803 && tree_fits_uhwi_p (args[1])
2804 && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2805 {
2806 unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2807 unsigned HOST_WIDE_INT hmisalign
2808 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2809 if ((halign & (halign - 1)) == 0
2810 && (hmisalign & ~(halign - 1)) == 0)
2811 set_ptr_info_alignment (get_ptr_info (forcedname),
2812 halign, hmisalign);
2813 }
2783 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block)); 2814 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2784 gimple_seq_add_stmt_without_update (&forced_stmts, call); 2815 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2785 folded = forcedname; 2816 folded = forcedname;
2786 } 2817 }
2787 else 2818 else
2903 tree forcedname = gimple_get_lhs (stmt); 2934 tree forcedname = gimple_get_lhs (stmt);
2904 pre_expr nameexpr; 2935 pre_expr nameexpr;
2905 2936
2906 if (forcedname != folded) 2937 if (forcedname != folded)
2907 { 2938 {
2908 VN_INFO_GET (forcedname)->valnum = forcedname; 2939 VN_INFO (forcedname)->valnum = forcedname;
2909 VN_INFO (forcedname)->value_id = get_next_value_id (); 2940 VN_INFO (forcedname)->value_id = get_next_value_id ();
2910 nameexpr = get_or_alloc_expr_for_name (forcedname); 2941 nameexpr = get_or_alloc_expr_for_name (forcedname);
2911 add_to_value (VN_INFO (forcedname)->value_id, nameexpr); 2942 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2912 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr); 2943 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2913 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr); 2944 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2929 The value may already exist in either NEW_SETS, or AVAIL_OUT, because 2960 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2930 we are creating the expression by pieces, and this particular piece of 2961 we are creating the expression by pieces, and this particular piece of
2931 the expression may have been represented. There is no harm in replacing 2962 the expression may have been represented. There is no harm in replacing
2932 here. */ 2963 here. */
2933 value_id = get_expr_value_id (expr); 2964 value_id = get_expr_value_id (expr);
2934 VN_INFO_GET (name)->value_id = value_id; 2965 VN_INFO (name)->value_id = value_id;
2935 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id); 2966 VN_INFO (name)->valnum = vn_valnum_from_value_id (value_id);
2936 if (VN_INFO (name)->valnum == NULL_TREE) 2967 if (VN_INFO (name)->valnum == NULL_TREE)
2937 VN_INFO (name)->valnum = name; 2968 VN_INFO (name)->valnum = name;
2938 gcc_assert (VN_INFO (name)->valnum != NULL_TREE); 2969 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2939 nameexpr = get_or_alloc_expr_for_name (name); 2970 nameexpr = get_or_alloc_expr_for_name (name);
2940 add_to_value (value_id, nameexpr); 2971 add_to_value (value_id, nameexpr);
3006 builtexpr = create_expression_by_pieces (bprime, eprime, 3037 builtexpr = create_expression_by_pieces (bprime, eprime,
3007 &stmts, type); 3038 &stmts, type);
3008 gcc_assert (!(pred->flags & EDGE_ABNORMAL)); 3039 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3009 if (!gimple_seq_empty_p (stmts)) 3040 if (!gimple_seq_empty_p (stmts))
3010 { 3041 {
3011 gsi_insert_seq_on_edge (pred, stmts); 3042 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3043 gcc_assert (! new_bb);
3012 insertions = true; 3044 insertions = true;
3013 } 3045 }
3014 if (!builtexpr) 3046 if (!builtexpr)
3015 { 3047 {
3016 /* We cannot insert a PHI node if we failed to insert 3048 /* We cannot insert a PHI node if we failed to insert
3034 3066
3035 /* Now build a phi for the new variable. */ 3067 /* Now build a phi for the new variable. */
3036 temp = make_temp_ssa_name (type, NULL, "prephitmp"); 3068 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3037 phi = create_phi_node (temp, block); 3069 phi = create_phi_node (temp, block);
3038 3070
3039 VN_INFO_GET (temp)->value_id = val; 3071 VN_INFO (temp)->value_id = val;
3040 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val); 3072 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3041 if (VN_INFO (temp)->valnum == NULL_TREE) 3073 if (VN_INFO (temp)->valnum == NULL_TREE)
3042 VN_INFO (temp)->valnum = temp; 3074 VN_INFO (temp)->valnum = temp;
3043 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp)); 3075 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3044 FOR_EACH_EDGE (pred, ei, block->preds) 3076 FOR_EACH_EDGE (pred, ei, block->preds)
3045 { 3077 {
3188 /* We should never run insertion for the exit block 3220 /* We should never run insertion for the exit block
3189 and so not come across fake pred edges. */ 3221 and so not come across fake pred edges. */
3190 gcc_assert (!(pred->flags & EDGE_FAKE)); 3222 gcc_assert (!(pred->flags & EDGE_FAKE));
3191 bprime = pred->src; 3223 bprime = pred->src;
3192 /* We are looking at ANTIC_OUT of bprime. */ 3224 /* We are looking at ANTIC_OUT of bprime. */
3193 eprime = phi_translate (expr, ANTIC_IN (block), NULL, 3225 eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3194 bprime, block);
3195 3226
3196 /* eprime will generally only be NULL if the 3227 /* eprime will generally only be NULL if the
3197 value of the expression, translated 3228 value of the expression, translated
3198 through the PHI for this predecessor, is 3229 through the PHI for this predecessor, is
3199 undefined. If that is the case, we can't 3230 undefined. If that is the case, we can't
3280 PRE_EXPR_CONSTANT (edoubleprime) : 3311 PRE_EXPR_CONSTANT (edoubleprime) :
3281 PRE_EXPR_NAME (edoubleprime)); 3312 PRE_EXPR_NAME (edoubleprime));
3282 gimple_stmt_iterator gsi = gsi_after_labels (block); 3313 gimple_stmt_iterator gsi = gsi_after_labels (block);
3283 gsi_insert_before (&gsi, assign, GSI_NEW_STMT); 3314 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3284 3315
3285 VN_INFO_GET (temp)->value_id = val; 3316 VN_INFO (temp)->value_id = val;
3286 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val); 3317 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3287 if (VN_INFO (temp)->valnum == NULL_TREE) 3318 if (VN_INFO (temp)->valnum == NULL_TREE)
3288 VN_INFO (temp)->valnum = temp; 3319 VN_INFO (temp)->valnum = temp;
3289 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp)); 3320 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3290 pre_expr newe = get_or_alloc_expr_for_name (temp); 3321 pre_expr newe = get_or_alloc_expr_for_name (temp);
3291 add_to_value (val, newe); 3322 add_to_value (val, newe);
3344 3375
3345 /* We should never run insertion for the exit block 3376 /* We should never run insertion for the exit block
3346 and so not come across fake pred edges. */ 3377 and so not come across fake pred edges. */
3347 gcc_assert (!(pred->flags & EDGE_FAKE)); 3378 gcc_assert (!(pred->flags & EDGE_FAKE));
3348 bprime = pred->src; 3379 bprime = pred->src;
3349 eprime = phi_translate (expr, ANTIC_IN (block), 3380 eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3350 PA_IN (block), 3381 PA_IN (block), pred);
3351 bprime, block);
3352 3382
3353 /* eprime will generally only be NULL if the 3383 /* eprime will generally only be NULL if the
3354 value of the expression, translated 3384 value of the expression, translated
3355 through the PHI for this predecessor, is 3385 through the PHI for this predecessor, is
3356 undefined. If that is the case, we can't 3386 undefined. If that is the case, we can't
3723 gimple *stmt; 3753 gimple *stmt;
3724 basic_block dom; 3754 basic_block dom;
3725 3755
3726 /* Pick a block from the worklist. */ 3756 /* Pick a block from the worklist. */
3727 block = worklist[--sp]; 3757 block = worklist[--sp];
3758 vn_context_bb = block;
3728 3759
3729 /* Initially, the set of available values in BLOCK is that of 3760 /* Initially, the set of available values in BLOCK is that of
3730 its immediate dominator. */ 3761 its immediate dominator. */
3731 dom = get_immediate_dominator (CDI_DOMINATORS, block); 3762 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3732 if (dom) 3763 if (dom)
3794 3825
3795 if (gimple_vdef (stmt)) 3826 if (gimple_vdef (stmt))
3796 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt); 3827 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3797 3828
3798 if (gimple_has_side_effects (stmt) 3829 if (gimple_has_side_effects (stmt)
3799 || stmt_could_throw_p (stmt) 3830 || stmt_could_throw_p (cfun, stmt)
3800 || is_gimple_debug (stmt)) 3831 || is_gimple_debug (stmt))
3801 continue; 3832 continue;
3802 3833
3803 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE) 3834 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3804 { 3835 {
3864 if (code == COND_EXPR 3895 if (code == COND_EXPR
3865 || code == VEC_COND_EXPR) 3896 || code == VEC_COND_EXPR)
3866 continue; 3897 continue;
3867 3898
3868 vn_nary_op_lookup_stmt (stmt, &nary); 3899 vn_nary_op_lookup_stmt (stmt, &nary);
3869 if (!nary) 3900 if (!nary || nary->predicated_values)
3870 continue; 3901 continue;
3871 3902
3872 /* If the NARY traps and there was a preceding 3903 /* If the NARY traps and there was a preceding
3873 point in the block that might not return avoid 3904 point in the block that might not return avoid
3874 adding the nary to EXP_GEN. */ 3905 adding the nary to EXP_GEN. */
4024 for (son = first_dom_son (CDI_DOMINATORS, block); 4055 for (son = first_dom_son (CDI_DOMINATORS, block);
4025 son; 4056 son;
4026 son = next_dom_son (CDI_DOMINATORS, son)) 4057 son = next_dom_son (CDI_DOMINATORS, son))
4027 worklist[sp++] = son; 4058 worklist[sp++] = son;
4028 } 4059 }
4060 vn_context_bb = NULL;
4029 4061
4030 free (worklist); 4062 free (worklist);
4031 }
4032
4033 /* Cheap DCE of a known set of possibly dead stmts.
4034
4035 Because we don't follow exactly the standard PRE algorithm, and decide not
4036 to insert PHI nodes sometimes, and because value numbering of casts isn't
4037 perfect, we sometimes end up inserting dead code. This simple DCE-like
4038 pass removes any insertions we made that weren't actually used. */
4039
4040 static void
4041 remove_dead_inserted_code (void)
4042 {
4043 /* ??? Re-use inserted_exprs as worklist not only as initial set.
4044 This may end up removing non-inserted code as well. If we
4045 keep inserted_exprs unchanged we could restrict new worklist
4046 elements to members of inserted_exprs. */
4047 bitmap worklist = inserted_exprs;
4048 while (! bitmap_empty_p (worklist))
4049 {
4050 /* Pop item. */
4051 unsigned i = bitmap_first_set_bit (worklist);
4052 bitmap_clear_bit (worklist, i);
4053
4054 tree def = ssa_name (i);
4055 /* Removed by somebody else or still in use. */
4056 if (! def || ! has_zero_uses (def))
4057 continue;
4058
4059 gimple *t = SSA_NAME_DEF_STMT (def);
4060 if (gimple_has_side_effects (t))
4061 continue;
4062
4063 /* Add uses to the worklist. */
4064 ssa_op_iter iter;
4065 use_operand_p use_p;
4066 FOR_EACH_PHI_OR_STMT_USE (use_p, t, iter, SSA_OP_USE)
4067 {
4068 tree use = USE_FROM_PTR (use_p);
4069 if (TREE_CODE (use) == SSA_NAME
4070 && ! SSA_NAME_IS_DEFAULT_DEF (use))
4071 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4072 }
4073
4074 /* Remove stmt. */
4075 if (dump_file && (dump_flags & TDF_DETAILS))
4076 {
4077 fprintf (dump_file, "Removing unnecessary insertion:");
4078 print_gimple_stmt (dump_file, t, 0);
4079 }
4080 gimple_stmt_iterator gsi = gsi_for_stmt (t);
4081 if (gimple_code (t) == GIMPLE_PHI)
4082 remove_phi_node (&gsi, true);
4083 else
4084 {
4085 gsi_remove (&gsi, true);
4086 release_defs (t);
4087 }
4088 }
4089 } 4063 }
4090 4064
4091 4065
4092 /* Initialize data structures used by PRE. */ 4066 /* Initialize data structures used by PRE. */
4093 4067
4129 4103
4130 static void 4104 static void
4131 fini_pre () 4105 fini_pre ()
4132 { 4106 {
4133 value_expressions.release (); 4107 value_expressions.release ();
4108 expressions.release ();
4134 BITMAP_FREE (inserted_exprs); 4109 BITMAP_FREE (inserted_exprs);
4135 bitmap_obstack_release (&grand_bitmap_obstack); 4110 bitmap_obstack_release (&grand_bitmap_obstack);
4136 bitmap_set_pool.release (); 4111 bitmap_set_pool.release ();
4137 pre_expr_pool.release (); 4112 pre_expr_pool.release ();
4138 delete phi_translate_table; 4113 delete phi_translate_table;
4171 { return flag_tree_pre != 0 || flag_code_hoisting != 0; } 4146 { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4172 virtual unsigned int execute (function *); 4147 virtual unsigned int execute (function *);
4173 4148
4174 }; // class pass_pre 4149 }; // class pass_pre
4175 4150
4151 /* Valueization hook for RPO VN when we are calling back to it
4152 at ANTIC compute time. */
4153
4154 static tree
4155 pre_valueize (tree name)
4156 {
4157 if (TREE_CODE (name) == SSA_NAME)
4158 {
4159 tree tem = VN_INFO (name)->valnum;
4160 if (tem != VN_TOP && tem != name)
4161 {
4162 if (TREE_CODE (tem) != SSA_NAME
4163 || SSA_NAME_IS_DEFAULT_DEF (tem))
4164 return tem;
4165 /* We create temporary SSA names for representatives that
4166 do not have a definition (yet) but are not default defs either
4167 assume they are fine to use. */
4168 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4169 if (! def_bb
4170 || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4171 return tem;
4172 /* ??? Now we could look for a leader. Ideally we'd somehow
4173 expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4174 }
4175 }
4176 return name;
4177 }
4178
4176 unsigned int 4179 unsigned int
4177 pass_pre::execute (function *fun) 4180 pass_pre::execute (function *fun)
4178 { 4181 {
4179 unsigned int todo = 0; 4182 unsigned int todo = 0;
4180 4183
4181 do_partial_partial = 4184 do_partial_partial =
4182 flag_tree_partial_pre && optimize_function_for_speed_p (fun); 4185 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4183 4186
4184 /* This has to happen before SCCVN runs because 4187 /* This has to happen before VN runs because
4185 loop_optimizer_init may create new phis, etc. */ 4188 loop_optimizer_init may create new phis, etc. */
4186 loop_optimizer_init (LOOPS_NORMAL); 4189 loop_optimizer_init (LOOPS_NORMAL);
4187 split_critical_edges (); 4190 split_critical_edges ();
4188 4191 scev_initialize ();
4189 run_scc_vn (VN_WALK); 4192
4193 run_rpo_vn (VN_WALK);
4190 4194
4191 init_pre (); 4195 init_pre ();
4192 scev_initialize (); 4196
4193 4197 vn_valueize = pre_valueize;
4194 /* Collect and value number expressions computed in each basic block. */
4195 compute_avail ();
4196 4198
4197 /* Insert can get quite slow on an incredibly large number of basic 4199 /* Insert can get quite slow on an incredibly large number of basic
4198 blocks due to some quadratic behavior. Until this behavior is 4200 blocks due to some quadratic behavior. Until this behavior is
4199 fixed, don't run it when he have an incredibly large number of 4201 fixed, don't run it when he have an incredibly large number of
4200 bb's. If we aren't going to run insert, there is no point in 4202 bb's. If we aren't going to run insert, there is no point in
4201 computing ANTIC, either, even though it's plenty fast. */ 4203 computing ANTIC, either, even though it's plenty fast nor do
4204 we require AVAIL. */
4202 if (n_basic_blocks_for_fn (fun) < 4000) 4205 if (n_basic_blocks_for_fn (fun) < 4000)
4203 { 4206 {
4207 compute_avail ();
4204 compute_antic (); 4208 compute_antic ();
4205 insert (); 4209 insert ();
4206 } 4210 }
4207 4211
4208 /* Make sure to remove fake edges before committing our inserts. 4212 /* Make sure to remove fake edges before committing our inserts.
4213 4217
4214 /* Eliminate folds statements which might (should not...) end up 4218 /* Eliminate folds statements which might (should not...) end up
4215 not keeping virtual operands up-to-date. */ 4219 not keeping virtual operands up-to-date. */
4216 gcc_assert (!need_ssa_update_p (fun)); 4220 gcc_assert (!need_ssa_update_p (fun));
4217 4221
4218 /* Remove all the redundant expressions. */
4219 todo |= vn_eliminate (inserted_exprs);
4220
4221 statistics_counter_event (fun, "Insertions", pre_stats.insertions); 4222 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4222 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert); 4223 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4223 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert); 4224 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4224 statistics_counter_event (fun, "New PHIs", pre_stats.phis); 4225 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4225 4226
4226 clear_expression_ids (); 4227 todo |= eliminate_with_rpo_vn (inserted_exprs);
4228
4229 vn_valueize = NULL;
4230
4231 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4232 to insert PHI nodes sometimes, and because value numbering of casts isn't
4233 perfect, we sometimes end up inserting dead code. This simple DCE-like
4234 pass removes any insertions we made that weren't actually used. */
4235 simple_dce_from_worklist (inserted_exprs);
4236
4237 fini_pre ();
4227 4238
4228 scev_finalize (); 4239 scev_finalize ();
4229 remove_dead_inserted_code ();
4230 fini_pre ();
4231 loop_optimizer_finalize (); 4240 loop_optimizer_finalize ();
4232
4233 /* Restore SSA info before tail-merging as that resets it as well. */
4234 scc_vn_restore_ssa_info ();
4235 4241
4236 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which 4242 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4237 case we can merge the block with the remaining predecessor of the block. 4243 case we can merge the block with the remaining predecessor of the block.
4238 It should either: 4244 It should either:
4239 - call merge_blocks after each tail merge iteration 4245 - call merge_blocks after each tail merge iteration
4240 - call merge_blocks after all tail merge iterations 4246 - call merge_blocks after all tail merge iterations
4241 - mark TODO_cleanup_cfg when necessary 4247 - mark TODO_cleanup_cfg when necessary
4242 - share the cfg cleanup with fini_pre. */ 4248 - share the cfg cleanup with fini_pre. */
4243 todo |= tail_merge_optimize (todo); 4249 todo |= tail_merge_optimize (todo);
4244 4250
4245 free_scc_vn (); 4251 free_rpo_vn ();
4246 4252
4247 /* Tail merging invalidates the virtual SSA web, together with 4253 /* Tail merging invalidates the virtual SSA web, together with
4248 cfg-cleanup opportunities exposed by PRE this will wreck the 4254 cfg-cleanup opportunities exposed by PRE this will wreck the
4249 SSA updating machinery. So make sure to run update-ssa 4255 SSA updating machinery. So make sure to run update-ssa
4250 manually, before eventually scheduling cfg-cleanup as part of 4256 manually, before eventually scheduling cfg-cleanup as part of