1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
|
/* Gimple range GORI functions.
Copyright (C) 2017-2024 Free Software Foundation, Inc.
Contributed by Andrew MacLeod <amacleod@redhat.com>
and Aldy Hernandez <aldyh@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "ssa.h"
#include "gimple-pretty-print.h"
#include "gimple-range.h"
// Return TRUE if GS is a logical && or || expression.
static inline bool
is_gimple_logical_p (const gimple *gs)
{
// Look for boolean and/or condition.
if (is_gimple_assign (gs))
switch (gimple_expr_code (gs))
{
case TRUTH_AND_EXPR:
case TRUTH_OR_EXPR:
return true;
case BIT_AND_EXPR:
case BIT_IOR_EXPR:
// Bitwise operations on single bits are logical too.
if (types_compatible_p (TREE_TYPE (gimple_assign_rhs1 (gs)),
boolean_type_node))
return true;
break;
default:
break;
}
return false;
}
/* RANGE_DEF_CHAIN is used to determine which SSA names in a block can
have range information calculated for them, and what the
dependencies on each other are.
Information for a basic block is calculated once and stored. It is
only calculated the first time a query is made, so if no queries
are made, there is little overhead.
The def_chain bitmap is indexed by SSA_NAME_VERSION. Bits are set
within this bitmap to indicate SSA names that are defined in the
SAME block and used to calculate this SSA name.
<bb 2> :
_1 = x_4(D) + -2;
_2 = _1 * 4;
j_7 = foo ();
q_5 = _2 + 3;
if (q_5 <= 13)
_1 : x_4(D)
_2 : 1 x_4(D)
q_5 : _1 _2 x_4(D)
This dump indicates the bits set in the def_chain vector.
as well as demonstrates the def_chain bits for the related ssa_names.
Checking the chain for _2 indicates that _1 and x_4 are used in
its evaluation.
Def chains also only include statements which are valid gimple
so a def chain will only span statements for which the range
engine implements operations for. */
// Construct a range_def_chain.
range_def_chain::range_def_chain ()
{
bitmap_obstack_initialize (&m_bitmaps);
m_def_chain.create (0);
m_def_chain.safe_grow_cleared (num_ssa_names);
m_logical_depth = 0;
}
// Destruct a range_def_chain.
range_def_chain::~range_def_chain ()
{
m_def_chain.release ();
bitmap_obstack_release (&m_bitmaps);
}
// Return true if NAME is in the def chain of DEF. If BB is provided,
// only return true if the defining statement of DEF is in BB.
bool
range_def_chain::in_chain_p (tree name, tree def)
{
gcc_checking_assert (gimple_range_ssa_p (def));
gcc_checking_assert (gimple_range_ssa_p (name));
// Get the definition chain for DEF.
bitmap chain = get_def_chain (def);
if (chain == NULL)
return false;
return bitmap_bit_p (chain, SSA_NAME_VERSION (name));
}
// Add either IMP or the import list B to the import set of DATA.
void
range_def_chain::set_import (struct rdc &data, tree imp, bitmap b)
{
// If there are no imports, just return
if (imp == NULL_TREE && !b)
return;
if (!data.m_import)
data.m_import = BITMAP_ALLOC (&m_bitmaps);
if (imp != NULL_TREE)
bitmap_set_bit (data.m_import, SSA_NAME_VERSION (imp));
else
bitmap_ior_into (data.m_import, b);
}
// Return the import list for NAME.
bitmap
range_def_chain::get_imports (tree name)
{
if (!has_def_chain (name))
get_def_chain (name);
bitmap i = m_def_chain[SSA_NAME_VERSION (name)].m_import;
return i;
}
// Return true if IMPORT is an import to NAMEs def chain.
bool
range_def_chain::chain_import_p (tree name, tree import)
{
bitmap b = get_imports (name);
if (b)
return bitmap_bit_p (b, SSA_NAME_VERSION (import));
return false;
}
// Build def_chains for NAME if it is in BB. Copy the def chain into RESULT.
void
range_def_chain::register_dependency (tree name, tree dep, basic_block bb)
{
if (!gimple_range_ssa_p (dep))
return;
unsigned v = SSA_NAME_VERSION (name);
if (v >= m_def_chain.length ())
m_def_chain.safe_grow_cleared (num_ssa_names + 1);
struct rdc &src = m_def_chain[v];
gimple *def_stmt = SSA_NAME_DEF_STMT (dep);
unsigned dep_v = SSA_NAME_VERSION (dep);
bitmap b;
// Set the direct dependency cache entries.
if (!src.ssa1)
src.ssa1 = SSA_NAME_VERSION (dep);
else if (!src.ssa2 && src.ssa1 != SSA_NAME_VERSION (dep))
src.ssa2 = SSA_NAME_VERSION (dep);
// Don't calculate imports or export/dep chains if BB is not provided.
// This is usually the case for when the temporal cache wants the direct
// dependencies of a stmt.
if (!bb)
return;
if (!src.bm)
src.bm = BITMAP_ALLOC (&m_bitmaps);
// Add this operand into the result.
bitmap_set_bit (src.bm, dep_v);
if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
{
// Get the def chain for the operand.
b = get_def_chain (dep);
// If there was one, copy it into result. Access def_chain directly
// as the get_def_chain request above could reallocate the vector.
if (b)
bitmap_ior_into (m_def_chain[v].bm, b);
// And copy the import list.
set_import (m_def_chain[v], NULL_TREE, get_imports (dep));
}
else
// Originated outside the block, so it is an import.
set_import (src, dep, NULL);
}
bool
range_def_chain::def_chain_in_bitmap_p (tree name, bitmap b)
{
bitmap a = get_def_chain (name);
if (a && b)
return bitmap_intersect_p (a, b);
return false;
}
void
range_def_chain::add_def_chain_to_bitmap (bitmap b, tree name)
{
bitmap r = get_def_chain (name);
if (r)
bitmap_ior_into (b, r);
}
// Return TRUE if NAME has been processed for a def_chain.
inline bool
range_def_chain::has_def_chain (tree name)
{
// Ensure there is an entry in the internal vector.
unsigned v = SSA_NAME_VERSION (name);
if (v >= m_def_chain.length ())
m_def_chain.safe_grow_cleared (num_ssa_names + 1);
return (m_def_chain[v].ssa1 != 0);
}
// Calculate the def chain for NAME and all of its dependent
// operands. Only using names in the same BB. Return the bitmap of
// all names in the m_def_chain. This only works for supported range
// statements.
bitmap
range_def_chain::get_def_chain (tree name)
{
tree ssa[3];
unsigned v = SSA_NAME_VERSION (name);
// If it has already been processed, just return the cached value.
if (has_def_chain (name) && m_def_chain[v].bm)
return m_def_chain[v].bm;
// No definition chain for default defs.
if (SSA_NAME_IS_DEFAULT_DEF (name))
{
// A Default def is always an import.
set_import (m_def_chain[v], name, NULL);
return NULL;
}
gimple *stmt = SSA_NAME_DEF_STMT (name);
unsigned count = gimple_range_ssa_names (ssa, 3, stmt);
if (count == 0)
{
// Stmts not understood or with no operands are always imports.
set_import (m_def_chain[v], name, NULL);
return NULL;
}
// Terminate the def chains if we see too many cascading stmts.
if (m_logical_depth == param_ranger_logical_depth)
return NULL;
// Increase the depth if we have a pair of ssa-names.
if (count > 1)
m_logical_depth++;
for (unsigned x = 0; x < count; x++)
register_dependency (name, ssa[x], gimple_bb (stmt));
if (count > 1)
m_logical_depth--;
return m_def_chain[v].bm;
}
// Dump what we know for basic block BB to file F.
void
range_def_chain::dump (FILE *f, basic_block bb, const char *prefix)
{
unsigned x, y;
bitmap_iterator bi;
// Dump the def chain for each SSA_NAME defined in BB.
for (x = 1; x < num_ssa_names; x++)
{
tree name = ssa_name (x);
if (!name)
continue;
gimple *stmt = SSA_NAME_DEF_STMT (name);
if (!stmt || (bb && gimple_bb (stmt) != bb))
continue;
bitmap chain = (has_def_chain (name) ? get_def_chain (name) : NULL);
if (chain && !bitmap_empty_p (chain))
{
fprintf (f, prefix);
print_generic_expr (f, name, TDF_SLIM);
fprintf (f, " : ");
bitmap imports = get_imports (name);
EXECUTE_IF_SET_IN_BITMAP (chain, 0, y, bi)
{
print_generic_expr (f, ssa_name (y), TDF_SLIM);
if (imports && bitmap_bit_p (imports, y))
fprintf (f, "(I)");
fprintf (f, " ");
}
fprintf (f, "\n");
}
}
}
// -------------------------------------------------------------------
/* GORI_MAP is used to accumulate what SSA names in a block can
generate range information, and provides tools for the block ranger
to enable it to efficiently calculate these ranges.
GORI stands for "Generates Outgoing Range Information."
It utilizes the range_def_chain class to construct def_chains.
Information for a basic block is calculated once and stored. It is
only calculated the first time a query is made. If no queries are
made, there is little overhead.
one bitmap is maintained for each basic block:
m_outgoing : a set bit indicates a range can be generated for a name.
Generally speaking, the m_outgoing vector is the union of the
entire def_chain of all SSA names used in the last statement of the
block which generate ranges. */
// Initialize a gori-map structure.
gori_map::gori_map ()
{
m_outgoing.create (0);
m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
m_incoming.create (0);
m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
m_maybe_variant = BITMAP_ALLOC (&m_bitmaps);
}
// Free any memory the GORI map allocated.
gori_map::~gori_map ()
{
m_incoming.release ();
m_outgoing.release ();
}
// Return the bitmap vector of all export from BB. Calculate if necessary.
bitmap
gori_map::exports (basic_block bb)
{
if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
calculate_gori (bb);
return m_outgoing[bb->index];
}
// Return the bitmap vector of all imports to BB. Calculate if necessary.
bitmap
gori_map::imports (basic_block bb)
{
if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
calculate_gori (bb);
return m_incoming[bb->index];
}
// Return true if NAME is can have ranges generated for it from basic
// block BB.
bool
gori_map::is_export_p (tree name, basic_block bb)
{
// If no BB is specified, test if it is exported anywhere in the IL.
if (!bb)
return bitmap_bit_p (m_maybe_variant, SSA_NAME_VERSION (name));
return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
}
// Set or clear the m_maybe_variant bit to determine if ranges will be tracked
// for NAME. A clear bit means they will NOT be tracked.
void
gori_map::set_range_invariant (tree name, bool invariant)
{
if (invariant)
bitmap_clear_bit (m_maybe_variant, SSA_NAME_VERSION (name));
else
bitmap_set_bit (m_maybe_variant, SSA_NAME_VERSION (name));
}
// Return true if NAME is an import to block BB.
bool
gori_map::is_import_p (tree name, basic_block bb)
{
// If no BB is specified, test if it is exported anywhere in the IL.
return bitmap_bit_p (imports (bb), SSA_NAME_VERSION (name));
}
// If NAME is non-NULL and defined in block BB, calculate the def
// chain and add it to m_outgoing.
void
gori_map::maybe_add_gori (tree name, basic_block bb)
{
if (name)
{
// Check if there is a def chain, regardless of the block.
add_def_chain_to_bitmap (m_outgoing[bb->index], name);
// Check for any imports.
bitmap imp = get_imports (name);
// If there were imports, add them so we can recompute
if (imp)
bitmap_ior_into (m_incoming[bb->index], imp);
// This name is always an import.
if (gimple_bb (SSA_NAME_DEF_STMT (name)) != bb)
bitmap_set_bit (m_incoming[bb->index], SSA_NAME_VERSION (name));
// Def chain doesn't include itself, and even if there isn't a
// def chain, this name should be added to exports.
bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
}
}
// Calculate all the required information for BB.
void
gori_map::calculate_gori (basic_block bb)
{
tree name;
if (bb->index >= (signed int)m_outgoing.length ())
{
m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
}
gcc_checking_assert (m_outgoing[bb->index] == NULL);
m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
m_incoming[bb->index] = BITMAP_ALLOC (&m_bitmaps);
if (single_succ_p (bb))
return;
// If this block's last statement may generate range information, go
// calculate it.
gimple *stmt = gimple_outgoing_range_stmt_p (bb);
if (!stmt)
return;
if (is_a<gcond *> (stmt))
{
gcond *gc = as_a<gcond *>(stmt);
name = gimple_range_ssa_p (gimple_cond_lhs (gc));
maybe_add_gori (name, gimple_bb (stmt));
name = gimple_range_ssa_p (gimple_cond_rhs (gc));
maybe_add_gori (name, gimple_bb (stmt));
}
else
{
// Do not process switches if they are too large.
if (EDGE_COUNT (bb->succs) > (unsigned)param_vrp_switch_limit)
return;
gswitch *gs = as_a<gswitch *>(stmt);
name = gimple_range_ssa_p (gimple_switch_index (gs));
maybe_add_gori (name, gimple_bb (stmt));
}
// Add this bitmap to the aggregate list of all outgoing names.
bitmap_ior_into (m_maybe_variant, m_outgoing[bb->index]);
}
// Dump the table information for BB to file F.
void
gori_map::dump (FILE *f, basic_block bb, bool verbose)
{
// BB was not processed.
if (!m_outgoing[bb->index] || bitmap_empty_p (m_outgoing[bb->index]))
return;
tree name;
bitmap imp = imports (bb);
if (!bitmap_empty_p (imp))
{
if (verbose)
fprintf (f, "bb<%u> Imports: ",bb->index);
else
fprintf (f, "Imports: ");
FOR_EACH_GORI_IMPORT_NAME (*this, bb, name)
{
print_generic_expr (f, name, TDF_SLIM);
fprintf (f, " ");
}
fputc ('\n', f);
}
if (verbose)
fprintf (f, "bb<%u> Exports: ",bb->index);
else
fprintf (f, "Exports: ");
// Dump the export vector.
FOR_EACH_GORI_EXPORT_NAME (*this, bb, name)
{
print_generic_expr (f, name, TDF_SLIM);
fprintf (f, " ");
}
fputc ('\n', f);
range_def_chain::dump (f, bb, " ");
}
// Dump the entire GORI map structure to file F.
void
gori_map::dump (FILE *f)
{
basic_block bb;
FOR_EACH_BB_FN (bb, cfun)
dump (f, bb);
}
DEBUG_FUNCTION void
debug (gori_map &g)
{
g.dump (stderr);
}
// -------------------------------------------------------------------
// Construct a gori_compute object.
gori_compute::gori_compute (int not_executable_flag)
: outgoing (param_vrp_switch_limit), tracer ("GORI ")
{
m_not_executable_flag = not_executable_flag;
// Create a boolean_type true and false range.
m_bool_zero = range_false ();
m_bool_one = range_true ();
if (dump_file && (param_ranger_debug & RANGER_DEBUG_GORI))
tracer.enable_trace ();
}
// Given the switch S, return an evaluation in R for NAME when the lhs
// evaluates to LHS. Returning false means the name being looked for
// was not resolvable.
bool
gori_compute::compute_operand_range_switch (vrange &r, gswitch *s,
const vrange &lhs,
tree name, fur_source &src)
{
tree op1 = gimple_switch_index (s);
// If name matches, the range is simply the range from the edge.
// Empty ranges are viral as they are on a path which isn't
// executable.
if (op1 == name || lhs.undefined_p ())
{
r = lhs;
return true;
}
// If op1 is in the definition chain, pass lhs back.
if (gimple_range_ssa_p (op1) && in_chain_p (name, op1))
return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name, src);
return false;
}
// Return an evaluation for NAME as it would appear in STMT when the
// statement's lhs evaluates to LHS. If successful, return TRUE and
// store the evaluation in R, otherwise return FALSE.
bool
gori_compute::compute_operand_range (vrange &r, gimple *stmt,
const vrange &lhs, tree name,
fur_source &src, value_relation *rel)
{
value_relation vrel;
value_relation *vrel_ptr = rel;
// Empty ranges are viral as they are on an unexecutable path.
if (lhs.undefined_p ())
{
r.set_undefined ();
return true;
}
if (is_a<gswitch *> (stmt))
return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name,
src);
gimple_range_op_handler handler (stmt);
if (!handler)
return false;
tree op1 = gimple_range_ssa_p (handler.operand1 ());
tree op2 = gimple_range_ssa_p (handler.operand2 ());
// If there is a relation betwen op1 and op2, use it instead as it is
// likely to be more applicable.
if (op1 && op2)
{
Value_Range r1, r2;
r1.set_varying (TREE_TYPE (op1));
r2.set_varying (TREE_TYPE (op2));
relation_kind k = handler.op1_op2_relation (lhs, r1, r2);
if (k != VREL_VARYING)
{
vrel.set_relation (k, op1, op2);
vrel_ptr = &vrel;
}
}
// Handle end of lookup first.
if (op1 == name)
return compute_operand1_range (r, handler, lhs, src, vrel_ptr);
if (op2 == name)
return compute_operand2_range (r, handler, lhs, src, vrel_ptr);
// NAME is not in this stmt, but one of the names in it ought to be
// derived from it.
bool op1_in_chain = op1 && in_chain_p (name, op1);
bool op2_in_chain = op2 && in_chain_p (name, op2);
// If neither operand is derived, then this stmt tells us nothing.
if (!op1_in_chain && !op2_in_chain)
return false;
// If either operand is in the def chain of the other (or they are equal), it
// will be evaluated twice and can result in an exponential time calculation.
// Instead just evaluate the one operand.
if (op1_in_chain && op2_in_chain)
{
if (in_chain_p (op1, op2) || op1 == op2)
op1_in_chain = false;
else if (in_chain_p (op2, op1))
op2_in_chain = false;
}
bool res = false;
// If the lhs doesn't tell us anything only a relation can possibly enhance
// the result.
if (lhs.varying_p ())
{
if (!vrel_ptr)
return false;
// If there is a relation (ie: x != y) , it can only be relevant if
// a) both elements are in the defchain
// c = x > y // (x and y are in c's defchain)
if (op1_in_chain)
res = in_chain_p (vrel_ptr->op1 (), op1)
&& in_chain_p (vrel_ptr->op2 (), op1);
if (!res && op2_in_chain)
res = in_chain_p (vrel_ptr->op1 (), op2)
|| in_chain_p (vrel_ptr->op2 (), op2);
if (!res)
{
// or b) one relation element is in the defchain of the other and the
// other is the LHS of this stmt.
// x = y + 2
if (vrel_ptr->op1 () == handler.lhs ()
&& (vrel_ptr->op2 () == op1 || vrel_ptr->op2 () == op2))
res = true;
else if (vrel_ptr->op2 () == handler.lhs ()
&& (vrel_ptr->op1 () == op1 || vrel_ptr->op1 () == op2))
res = true;
}
if (!res)
return false;
}
// Process logicals as they have special handling.
if (is_gimple_logical_p (stmt))
{
// If the lhs doesn't tell us anything, neither will combining operands.
if (lhs.varying_p ())
return false;
unsigned idx;
if ((idx = tracer.header ("compute_operand ")))
{
print_generic_expr (dump_file, name, TDF_SLIM);
fprintf (dump_file, " with LHS = ");
lhs.dump (dump_file);
fprintf (dump_file, " at stmt ");
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
}
tree type = TREE_TYPE (name);
Value_Range op1_trange (type), op1_frange (type);
Value_Range op2_trange (type), op2_frange (type);
compute_logical_operands (op1_trange, op1_frange, handler,
as_a <irange> (lhs),
name, src, op1, op1_in_chain);
compute_logical_operands (op2_trange, op2_frange, handler,
as_a <irange> (lhs),
name, src, op2, op2_in_chain);
res = logical_combine (r,
gimple_expr_code (stmt),
as_a <irange> (lhs),
op1_trange, op1_frange, op2_trange, op2_frange);
if (idx)
tracer.trailer (idx, "compute_operand", res, name, r);
return res;
}
// Follow the appropriate operands now.
if (op1_in_chain && op2_in_chain)
return compute_operand1_and_operand2_range (r, handler, lhs, name, src,
vrel_ptr);
Value_Range vr;
gimple *src_stmt;
if (op1_in_chain)
{
vr.set_type (TREE_TYPE (op1));
if (!compute_operand1_range (vr, handler, lhs, src, vrel_ptr))
return false;
src_stmt = SSA_NAME_DEF_STMT (op1);
}
else
{
gcc_checking_assert (op2_in_chain);
vr.set_type (TREE_TYPE (op2));
if (!compute_operand2_range (vr, handler, lhs, src, vrel_ptr))
return false;
src_stmt = SSA_NAME_DEF_STMT (op2);
}
gcc_checking_assert (src_stmt);
// Then feed this range back as the LHS of the defining statement.
return compute_operand_range (r, src_stmt, vr, name, src, vrel_ptr);
// If neither operand is derived, this statement tells us nothing.
}
// Return TRUE if range R is either a true or false compatible range.
static bool
range_is_either_true_or_false (const irange &r)
{
if (r.undefined_p ())
return false;
// This is complicated by the fact that Ada has multi-bit booleans,
// so true can be ~[0, 0] (i.e. [1,MAX]).
tree type = r.type ();
gcc_checking_assert (range_compatible_p (type, boolean_type_node));
return (r.singleton_p ()
|| !r.contains_p (wi::zero (TYPE_PRECISION (type))));
}
// Evaluate a binary logical expression by combining the true and
// false ranges for each of the operands based on the result value in
// the LHS.
bool
gori_compute::logical_combine (vrange &r, enum tree_code code,
const irange &lhs,
const vrange &op1_true, const vrange &op1_false,
const vrange &op2_true, const vrange &op2_false)
{
if (op1_true.varying_p () && op1_false.varying_p ()
&& op2_true.varying_p () && op2_false.varying_p ())
return false;
unsigned idx;
if ((idx = tracer.header ("logical_combine")))
{
switch (code)
{
case TRUTH_OR_EXPR:
case BIT_IOR_EXPR:
fprintf (dump_file, " || ");
break;
case TRUTH_AND_EXPR:
case BIT_AND_EXPR:
fprintf (dump_file, " && ");
break;
default:
break;
}
fprintf (dump_file, " with LHS = ");
lhs.dump (dump_file);
fputc ('\n', dump_file);
tracer.print (idx, "op1_true = ");
op1_true.dump (dump_file);
fprintf (dump_file, " op1_false = ");
op1_false.dump (dump_file);
fputc ('\n', dump_file);
tracer.print (idx, "op2_true = ");
op2_true.dump (dump_file);
fprintf (dump_file, " op2_false = ");
op2_false.dump (dump_file);
fputc ('\n', dump_file);
}
// This is not a simple fold of a logical expression, rather it
// determines ranges which flow through the logical expression.
//
// Assuming x_8 is an unsigned char, and relational statements:
// b_1 = x_8 < 20
// b_2 = x_8 > 5
// consider the logical expression and branch:
// c_2 = b_1 && b_2
// if (c_2)
//
// To determine the range of x_8 on either edge of the branch, one
// must first determine what the range of x_8 is when the boolean
// values of b_1 and b_2 are both true and false.
// b_1 TRUE x_8 = [0, 19]
// b_1 FALSE x_8 = [20, 255]
// b_2 TRUE x_8 = [6, 255]
// b_2 FALSE x_8 = [0,5].
//
// These ranges are then combined based on the expected outcome of
// the branch. The range on the TRUE side of the branch must satisfy
// b_1 == true && b_2 == true
//
// In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
// must be true. The range of x_8 on the true side must be the
// intersection of both ranges since both must be true. Thus the
// range of x_8 on the true side is [6, 19].
//
// To determine the ranges on the FALSE side, all 3 combinations of
// failing ranges must be considered, and combined as any of them
// can cause the false result.
//
// If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
// FALSE results and combine them. If we fell back to VARYING any
// range restrictions that have been discovered up to this point
// would be lost.
if (!range_is_either_true_or_false (lhs))
{
bool res;
Value_Range r1 (r);
if (logical_combine (r1, code, m_bool_zero, op1_true, op1_false,
op2_true, op2_false)
&& logical_combine (r, code, m_bool_one, op1_true, op1_false,
op2_true, op2_false))
{
r.union_ (r1);
res = true;
}
else
res = false;
if (idx && res)
{
tracer.print (idx, "logical_combine produced ");
r.dump (dump_file);
fputc ('\n', dump_file);
}
return res;
}
switch (code)
{
// A logical AND combines ranges from 2 boolean conditions.
// c_2 = b_1 && b_2
case TRUTH_AND_EXPR:
case BIT_AND_EXPR:
if (!lhs.zero_p ())
{
// The TRUE side is the intersection of the 2 true ranges.
r = op1_true;
r.intersect (op2_true);
}
else
{
// The FALSE side is the union of the other 3 cases.
Value_Range ff (op1_false);
ff.intersect (op2_false);
Value_Range tf (op1_true);
tf.intersect (op2_false);
Value_Range ft (op1_false);
ft.intersect (op2_true);
r = ff;
r.union_ (tf);
r.union_ (ft);
}
break;
// A logical OR combines ranges from 2 boolean conditions.
// c_2 = b_1 || b_2
case TRUTH_OR_EXPR:
case BIT_IOR_EXPR:
if (lhs.zero_p ())
{
// An OR operation will only take the FALSE path if both
// operands are false simultaneously, which means they should
// be intersected. !(x || y) == !x && !y
r = op1_false;
r.intersect (op2_false);
}
else
{
// The TRUE side of an OR operation will be the union of
// the other three combinations.
Value_Range tt (op1_true);
tt.intersect (op2_true);
Value_Range tf (op1_true);
tf.intersect (op2_false);
Value_Range ft (op1_false);
ft.intersect (op2_true);
r = tt;
r.union_ (tf);
r.union_ (ft);
}
break;
default:
gcc_unreachable ();
}
if (idx)
tracer.trailer (idx, "logical_combine", true, NULL_TREE, r);
return true;
}
// Given a logical STMT, calculate true and false ranges for each
// potential path of NAME, assuming NAME came through the OP chain if
// OP_IN_CHAIN is true.
void
gori_compute::compute_logical_operands (vrange &true_range, vrange &false_range,
gimple_range_op_handler &handler,
const irange &lhs,
tree name, fur_source &src,
tree op, bool op_in_chain)
{
gimple *stmt = handler.stmt ();
gimple *src_stmt = gimple_range_ssa_p (op) ? SSA_NAME_DEF_STMT (op) : NULL;
if (!op_in_chain || !src_stmt || chain_import_p (handler.lhs (), op))
{
// If op is not in the def chain, or defined in this block,
// use its known value on entry to the block.
src.get_operand (true_range, name);
false_range = true_range;
unsigned idx;
if ((idx = tracer.header ("logical_operand")))
{
print_generic_expr (dump_file, op, TDF_SLIM);
fprintf (dump_file, " not in computation chain. Queried.\n");
tracer.trailer (idx, "logical_operand", true, NULL_TREE, true_range);
}
return;
}
enum tree_code code = gimple_expr_code (stmt);
// Optimize [0 = x | y], since neither operand can ever be non-zero.
if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
{
if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name,
src))
src.get_operand (false_range, name);
true_range = false_range;
return;
}
// Optimize [1 = x & y], since neither operand can ever be zero.
if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
{
if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
src.get_operand (true_range, name);
false_range = true_range;
return;
}
// Calculate ranges for true and false on both sides, since the false
// path is not always a simple inversion of the true side.
if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
src.get_operand (true_range, name);
if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name, src))
src.get_operand (false_range, name);
}
// This routine will try to refine the ranges of OP1 and OP2 given a relation
// K between them. In order to perform this refinement, one of the operands
// must be in the definition chain of the other. The use is refined using
// op1/op2_range on the statement, and the definition is then recalculated
// using the relation.
bool
gori_compute::refine_using_relation (tree op1, vrange &op1_range,
tree op2, vrange &op2_range,
fur_source &src, relation_kind k)
{
gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
if (k == VREL_VARYING || k == VREL_EQ || k == VREL_UNDEFINED)
return false;
bool change = false;
bool op1_def_p = in_chain_p (op2, op1);
if (!op1_def_p)
if (!in_chain_p (op1, op2))
return false;
tree def_op = op1_def_p ? op1 : op2;
tree use_op = op1_def_p ? op2 : op1;
if (!op1_def_p)
k = relation_swap (k);
// op1_def is true if we want to look up op1, otherwise we want op2.
// if neither is the case, we returned in the above check.
gimple *def_stmt = SSA_NAME_DEF_STMT (def_op);
gimple_range_op_handler op_handler (def_stmt);
if (!op_handler)
return false;
tree def_op1 = op_handler.operand1 ();
tree def_op2 = op_handler.operand2 ();
// if the def isn't binary, the relation will not be useful.
if (!def_op2)
return false;
// Determine if op2 is directly referenced as an operand.
if (def_op1 == use_op)
{
// def_stmt has op1 in the 1st operand position.
Value_Range other_op (TREE_TYPE (def_op2));
src.get_operand (other_op, def_op2);
// Using op1_range as the LHS, and relation REL, evaluate op2.
tree type = TREE_TYPE (def_op1);
Value_Range new_result (type);
if (!op_handler.op1_range (new_result, type,
op1_def_p ? op1_range : op2_range,
other_op, relation_trio::lhs_op1 (k)))
return false;
if (op1_def_p)
{
change |= op2_range.intersect (new_result);
// Recalculate op2.
if (op_handler.fold_range (new_result, type, op2_range, other_op))
{
change |= op1_range.intersect (new_result);
}
}
else
{
change |= op1_range.intersect (new_result);
// Recalculate op1.
if (op_handler.fold_range (new_result, type, op1_range, other_op))
{
change |= op2_range.intersect (new_result);
}
}
}
else if (def_op2 == use_op)
{
// def_stmt has op1 in the 1st operand position.
Value_Range other_op (TREE_TYPE (def_op1));
src.get_operand (other_op, def_op1);
// Using op1_range as the LHS, and relation REL, evaluate op2.
tree type = TREE_TYPE (def_op2);
Value_Range new_result (type);
if (!op_handler.op2_range (new_result, type,
op1_def_p ? op1_range : op2_range,
other_op, relation_trio::lhs_op2 (k)))
return false;
if (op1_def_p)
{
change |= op2_range.intersect (new_result);
// Recalculate op1.
if (op_handler.fold_range (new_result, type, other_op, op2_range))
{
change |= op1_range.intersect (new_result);
}
}
else
{
change |= op1_range.intersect (new_result);
// Recalculate op2.
if (op_handler.fold_range (new_result, type, other_op, op1_range))
{
change |= op2_range.intersect (new_result);
}
}
}
return change;
}
// Calculate a range for NAME from the operand 1 position of STMT
// assuming the result of the statement is LHS. Return the range in
// R, or false if no range could be calculated.
bool
gori_compute::compute_operand1_range (vrange &r,
gimple_range_op_handler &handler,
const vrange &lhs,
fur_source &src, value_relation *rel)
{
gimple *stmt = handler.stmt ();
tree op1 = handler.operand1 ();
tree op2 = handler.operand2 ();
tree lhs_name = gimple_get_lhs (stmt);
relation_trio trio;
if (rel)
trio = rel->create_trio (lhs_name, op1, op2);
Value_Range op1_range (TREE_TYPE (op1));
Value_Range op2_range (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
// Fetch the known range for op1 in this block.
src.get_operand (op1_range, op1);
// Now range-op calculate and put that result in r.
if (op2)
{
src.get_operand (op2_range, op2);
relation_kind op_op = trio.op1_op2 ();
if (op_op != VREL_VARYING)
refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
// If op1 == op2, create a new trio for just this call.
if (op1 == op2 && gimple_range_ssa_p (op1))
trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
if (!handler.calc_op1 (r, lhs, op2_range, trio))
return false;
}
else
{
// We pass op1_range to the unary operation. Normally it's a
// hidden range_for_type parameter, but sometimes having the
// actual range can result in better information.
if (!handler.calc_op1 (r, lhs, op1_range, trio))
return false;
}
unsigned idx;
if ((idx = tracer.header ("compute op 1 (")))
{
print_generic_expr (dump_file, op1, TDF_SLIM);
fprintf (dump_file, ") at ");
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
tracer.print (idx, "LHS =");
lhs.dump (dump_file);
if (op2 && TREE_CODE (op2) == SSA_NAME)
{
fprintf (dump_file, ", ");
print_generic_expr (dump_file, op2, TDF_SLIM);
fprintf (dump_file, " = ");
op2_range.dump (dump_file);
}
fprintf (dump_file, "\n");
tracer.print (idx, "Computes ");
print_generic_expr (dump_file, op1, TDF_SLIM);
fprintf (dump_file, " = ");
r.dump (dump_file);
fprintf (dump_file, " intersect Known range : ");
op1_range.dump (dump_file);
fputc ('\n', dump_file);
}
r.intersect (op1_range);
if (idx)
tracer.trailer (idx, "produces ", true, op1, r);
return true;
}
// Calculate a range for NAME from the operand 2 position of S
// assuming the result of the statement is LHS. Return the range in
// R, or false if no range could be calculated.
bool
gori_compute::compute_operand2_range (vrange &r,
gimple_range_op_handler &handler,
const vrange &lhs,
fur_source &src, value_relation *rel)
{
gimple *stmt = handler.stmt ();
tree op1 = handler.operand1 ();
tree op2 = handler.operand2 ();
tree lhs_name = gimple_get_lhs (stmt);
Value_Range op1_range (TREE_TYPE (op1));
Value_Range op2_range (TREE_TYPE (op2));
src.get_operand (op1_range, op1);
src.get_operand (op2_range, op2);
relation_trio trio;
if (rel)
trio = rel->create_trio (lhs_name, op1, op2);
relation_kind op_op = trio.op1_op2 ();
if (op_op != VREL_VARYING)
refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
// If op1 == op2, create a new trio for this stmt.
if (op1 == op2 && gimple_range_ssa_p (op1))
trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
// Intersect with range for op2 based on lhs and op1.
if (!handler.calc_op2 (r, lhs, op1_range, trio))
return false;
unsigned idx;
if ((idx = tracer.header ("compute op 2 (")))
{
print_generic_expr (dump_file, op2, TDF_SLIM);
fprintf (dump_file, ") at ");
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
tracer.print (idx, "LHS = ");
lhs.dump (dump_file);
if (TREE_CODE (op1) == SSA_NAME)
{
fprintf (dump_file, ", ");
print_generic_expr (dump_file, op1, TDF_SLIM);
fprintf (dump_file, " = ");
op1_range.dump (dump_file);
}
fprintf (dump_file, "\n");
tracer.print (idx, "Computes ");
print_generic_expr (dump_file, op2, TDF_SLIM);
fprintf (dump_file, " = ");
r.dump (dump_file);
fprintf (dump_file, " intersect Known range : ");
op2_range.dump (dump_file);
fputc ('\n', dump_file);
}
// Intersect the calculated result with the known result and return if done.
r.intersect (op2_range);
if (idx)
tracer.trailer (idx, " produces ", true, op2, r);
return true;
}
// Calculate a range for NAME from both operand positions of S
// assuming the result of the statement is LHS. Return the range in
// R, or false if no range could be calculated.
bool
gori_compute::compute_operand1_and_operand2_range (vrange &r,
gimple_range_op_handler
&handler,
const vrange &lhs,
tree name,
fur_source &src,
value_relation *rel)
{
Value_Range op_range (TREE_TYPE (name));
Value_Range vr (TREE_TYPE (handler.operand2 ()));
// Calculate a good a range through op2.
if (!compute_operand2_range (vr, handler, lhs, src, rel))
return false;
gimple *src_stmt = SSA_NAME_DEF_STMT (handler.operand2 ());
gcc_checking_assert (src_stmt);
// Then feed this range back as the LHS of the defining statement.
if (!compute_operand_range (r, src_stmt, vr, name, src, rel))
return false;
// Now get the range thru op1.
vr.set_type (TREE_TYPE (handler.operand1 ()));
if (!compute_operand1_range (vr, handler, lhs, src, rel))
return false;
src_stmt = SSA_NAME_DEF_STMT (handler.operand1 ());
gcc_checking_assert (src_stmt);
// Then feed this range back as the LHS of the defining statement.
if (!compute_operand_range (op_range, src_stmt, vr, name, src, rel))
return false;
// Both operands have to be simultaneously true, so perform an intersection.
r.intersect (op_range);
return true;
}
// Return TRUE if NAME can be recomputed on any edge exiting BB. If any
// direct dependent is exported, it may also change the computed value of NAME.
bool
gori_compute::may_recompute_p (tree name, basic_block bb, int depth)
{
tree dep1 = depend1 (name);
tree dep2 = depend2 (name);
// If the first dependency is not set, there is no recomputation.
// Dependencies reflect original IL, not current state. Check if the
// SSA_NAME is still valid as well.
if (!dep1)
return false;
// Don't recalculate PHIs or statements with side_effects.
gimple *s = SSA_NAME_DEF_STMT (name);
if (is_a<gphi *> (s) || gimple_has_side_effects (s))
return false;
if (!dep2)
{
// -1 indicates a default param, convert it to the real default.
if (depth == -1)
{
depth = (int)param_ranger_recompute_depth;
gcc_checking_assert (depth >= 1);
}
bool res = (bb ? is_export_p (dep1, bb) : is_export_p (dep1));
if (res || depth <= 1)
return res;
// Check another level of recomputation.
return may_recompute_p (dep1, bb, --depth);
}
// Two dependencies terminate the depth of the search.
if (bb)
return is_export_p (dep1, bb) || is_export_p (dep2, bb);
else
return is_export_p (dep1) || is_export_p (dep2);
}
// Return TRUE if NAME can be recomputed on edge E. If any direct dependent
// is exported on edge E, it may change the computed value of NAME.
bool
gori_compute::may_recompute_p (tree name, edge e, int depth)
{
gcc_checking_assert (e);
return may_recompute_p (name, e->src, depth);
}
// Return TRUE if a range can be calculated or recomputed for NAME on any
// edge exiting BB.
bool
gori_compute::has_edge_range_p (tree name, basic_block bb)
{
// Check if NAME is an export or can be recomputed.
if (bb)
return is_export_p (name, bb) || may_recompute_p (name, bb);
// If no block is specified, check for anywhere in the IL.
return is_export_p (name) || may_recompute_p (name);
}
// Return TRUE if a range can be calculated or recomputed for NAME on edge E.
bool
gori_compute::has_edge_range_p (tree name, edge e)
{
gcc_checking_assert (e);
return has_edge_range_p (name, e->src);
}
// Calculate a range on edge E and return it in R. Try to evaluate a
// range for NAME on this edge. Return FALSE if this is either not a
// control edge or NAME is not defined by this edge.
bool
gori_compute::outgoing_edge_range_p (vrange &r, edge e, tree name,
range_query &q)
{
unsigned idx;
if ((e->flags & m_not_executable_flag))
{
r.set_undefined ();
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Outgoing edge %d->%d unexecutable.\n",
e->src->index, e->dest->index);
return true;
}
gcc_checking_assert (gimple_range_ssa_p (name));
int_range_max lhs;
// Determine if there is an outgoing edge.
gimple *stmt = outgoing.edge_range_p (lhs, e);
if (!stmt)
return false;
fur_stmt src (stmt, &q);
// If NAME can be calculated on the edge, use that.
if (is_export_p (name, e->src))
{
bool res;
if ((idx = tracer.header ("outgoing_edge")))
{
fprintf (dump_file, " for ");
print_generic_expr (dump_file, name, TDF_SLIM);
fprintf (dump_file, " on edge %d->%d\n",
e->src->index, e->dest->index);
}
if ((res = compute_operand_range (r, stmt, lhs, name, src)))
{
// Sometimes compatible types get interchanged. See PR97360.
// Make sure we are returning the type of the thing we asked for.
if (!r.undefined_p () && r.type () != TREE_TYPE (name))
{
gcc_checking_assert (range_compatible_p (r.type (),
TREE_TYPE (name)));
range_cast (r, TREE_TYPE (name));
}
}
if (idx)
tracer.trailer (idx, "outgoing_edge", res, name, r);
return res;
}
// If NAME isn't exported, check if it can be recomputed.
else if (may_recompute_p (name, e))
{
gimple *def_stmt = SSA_NAME_DEF_STMT (name);
if ((idx = tracer.header ("recomputation")))
{
fprintf (dump_file, " attempt on edge %d->%d for ",
e->src->index, e->dest->index);
print_gimple_stmt (dump_file, def_stmt, 0, TDF_SLIM);
}
// Simply calculate DEF_STMT on edge E using the range query Q.
fold_range (r, def_stmt, e, &q);
if (idx)
tracer.trailer (idx, "recomputation", true, name, r);
return true;
}
return false;
}
// Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
// to further resolve R1 and R2 if there are any dependencies between
// OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
// as the origination source location for operands..
// Effectively, use COND an the edge condition and solve for OP1 on the true
// edge and OP2 on the false edge.
bool
gori_compute::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
tree op1, tree op2, fur_source &src)
{
tree ssa1 = gimple_range_ssa_p (op1);
tree ssa2 = gimple_range_ssa_p (op2);
if (!ssa1 && !ssa2)
return false;
if (TREE_CODE (cond) != SSA_NAME)
return false;
gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
if (!cond_def
|| TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
return false;
tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
if (!range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
return false;
range_op_handler hand (gimple_assign_rhs_code (cond_def));
if (!hand)
return false;
tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
// Only solve if there is one SSA name in the condition.
if ((!c1 && !c2) || (c1 && c2))
return false;
// Pick up the current values of each part of the condition.
tree rhs1 = gimple_assign_rhs1 (cond_def);
tree rhs2 = gimple_assign_rhs2 (cond_def);
Value_Range cl (TREE_TYPE (rhs1));
Value_Range cr (TREE_TYPE (rhs2));
src.get_operand (cl, rhs1);
src.get_operand (cr, rhs2);
tree cond_name = c1 ? c1 : c2;
gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
// Evaluate the value of COND_NAME on the true and false edges, using either
// the op1 or op2 routines based on its location.
Value_Range cond_true (type), cond_false (type);
if (c1)
{
if (!hand.op1_range (cond_false, type, m_bool_zero, cr))
return false;
if (!hand.op1_range (cond_true, type, m_bool_one, cr))
return false;
cond_false.intersect (cl);
cond_true.intersect (cl);
}
else
{
if (!hand.op2_range (cond_false, type, m_bool_zero, cl))
return false;
if (!hand.op2_range (cond_true, type, m_bool_one, cl))
return false;
cond_false.intersect (cr);
cond_true.intersect (cr);
}
unsigned idx;
if ((idx = tracer.header ("cond_expr evaluation : ")))
{
fprintf (dump_file, " range1 = ");
r1.dump (dump_file);
fprintf (dump_file, ", range2 = ");
r1.dump (dump_file);
fprintf (dump_file, "\n");
}
// Now solve for SSA1 or SSA2 if they are in the dependency chain.
if (ssa1 && in_chain_p (ssa1, cond_name))
{
Value_Range tmp1 (TREE_TYPE (ssa1));
if (compute_operand_range (tmp1, def_stmt, cond_true, ssa1, src))
r1.intersect (tmp1);
}
if (ssa2 && in_chain_p (ssa2, cond_name))
{
Value_Range tmp2 (TREE_TYPE (ssa2));
if (compute_operand_range (tmp2, def_stmt, cond_false, ssa2, src))
r2.intersect (tmp2);
}
if (idx)
{
tracer.print (idx, "outgoing: range1 = ");
r1.dump (dump_file);
fprintf (dump_file, ", range2 = ");
r1.dump (dump_file);
fprintf (dump_file, "\n");
tracer.trailer (idx, "cond_expr", true, cond_name, cond_true);
}
return true;
}
// Dump what is known to GORI computes to listing file F.
void
gori_compute::dump (FILE *f)
{
gori_map::dump (f);
}
// ------------------------------------------------------------------------
// GORI iterator. Although we have bitmap iterators, don't expose that it
// is currently a bitmap. Use an export iterator to hide future changes.
// Construct a basic iterator over an export bitmap.
gori_export_iterator::gori_export_iterator (bitmap b)
{
bm = b;
if (b)
bmp_iter_set_init (&bi, b, 1, &y);
}
// Move to the next export bitmap spot.
void
gori_export_iterator::next ()
{
bmp_iter_next (&bi, &y);
}
// Fetch the name of the next export in the export list. Return NULL if
// iteration is done.
tree
gori_export_iterator::get_name ()
{
if (!bm)
return NULL_TREE;
while (bmp_iter_set (&bi, &y))
{
tree t = ssa_name (y);
if (t)
return t;
next ();
}
return NULL_TREE;
}
// This is a helper class to set up STMT with a known LHS for further GORI
// processing.
class gori_stmt_info : public gimple_range_op_handler
{
public:
gori_stmt_info (vrange &lhs, gimple *stmt, range_query *q);
Value_Range op1_range;
Value_Range op2_range;
tree ssa1;
tree ssa2;
};
// Uses query Q to get the known ranges on STMT with a LHS range
// for op1_range and op2_range and set ssa1 and ssa2 if either or both of
// those operands are SSA_NAMES.
gori_stmt_info::gori_stmt_info (vrange &lhs, gimple *stmt, range_query *q)
: gimple_range_op_handler (stmt)
{
ssa1 = NULL;
ssa2 = NULL;
// Don't handle switches as yet for vector processing.
if (is_a<gswitch *> (stmt))
return;
// No frther processing for VARYING or undefined.
if (lhs.undefined_p () || lhs.varying_p ())
return;
// If there is no range-op handler, we are also done.
if (!*this)
return;
// Only evaluate logical cases if both operands must be the same as the LHS.
// Otherwise its becomes exponential in time, as well as more complicated.
if (is_gimple_logical_p (stmt))
{
gcc_checking_assert (range_compatible_p (lhs.type (), boolean_type_node));
enum tree_code code = gimple_expr_code (stmt);
if (code == TRUTH_OR_EXPR || code == BIT_IOR_EXPR)
{
// [0, 0] = x || y means both x and y must be zero.
if (!lhs.singleton_p () || !lhs.zero_p ())
return;
}
else if (code == TRUTH_AND_EXPR || code == BIT_AND_EXPR)
{
// [1, 1] = x && y means both x and y must be one.
if (!lhs.singleton_p () || lhs.zero_p ())
return;
}
}
tree op1 = operand1 ();
tree op2 = operand2 ();
ssa1 = gimple_range_ssa_p (op1);
ssa2 = gimple_range_ssa_p (op2);
// If both operands are the same, only process one of them.
if (ssa1 && ssa1 == ssa2)
ssa2 = NULL_TREE;
// Extract current ranges for the operands.
fur_stmt src (stmt, q);
if (op1)
{
op1_range.set_type (TREE_TYPE (op1));
src.get_operand (op1_range, op1);
}
// And satisfy the second operand for single op satements.
if (op2)
{
op2_range.set_type (TREE_TYPE (op2));
src.get_operand (op2_range, op2);
}
else if (op1)
op2_range = op1_range;
return;
}
// Process STMT using LHS as the range of the LHS. Invoke GORI processing
// to resolve ranges for all SSA_NAMES feeding STMT which may be altered
// based on LHS. Fill R with the results, and resolve all incoming
// ranges using range-query Q.
static void
gori_calc_operands (vrange &lhs, gimple *stmt, ssa_cache &r, range_query *q)
{
struct gori_stmt_info si(lhs, stmt, q);
if (!si)
return;
Value_Range tmp;
// Now evaluate operand ranges, and set them in the edge cache.
// If there was already a range, leave it and do no further evaluation.
if (si.ssa1 && !r.has_range (si.ssa1))
{
tmp.set_type (TREE_TYPE (si.ssa1));
if (si.calc_op1 (tmp, lhs, si.op2_range))
si.op1_range.intersect (tmp);
r.set_range (si.ssa1, si.op1_range);
gimple *src = SSA_NAME_DEF_STMT (si.ssa1);
// If defintion is in the same basic lock, evaluate it.
if (src && gimple_bb (src) == gimple_bb (stmt))
gori_calc_operands (si.op1_range, src, r, q);
}
if (si.ssa2 && !r.has_range (si.ssa2))
{
tmp.set_type (TREE_TYPE (si.ssa2));
if (si.calc_op2 (tmp, lhs, si.op1_range))
si.op2_range.intersect (tmp);
r.set_range (si.ssa2, si.op2_range);
gimple *src = SSA_NAME_DEF_STMT (si.ssa2);
if (src && gimple_bb (src) == gimple_bb (stmt))
gori_calc_operands (si.op2_range, src, r, q);
}
}
// Use ssa_cache R as a repository for all outgoing ranges on edge E that
// can be calculated. Use OGR if present to establish starting edge ranges,
// and Q to resolve operand values. If Q is NULL use the current range
// query available to the system.
bool
gori_on_edge (ssa_cache &r, edge e, range_query *q, gimple_outgoing_range *ogr)
{
// Start with an empty vector
r.clear ();
int_range_max lhs;
// Determine if there is an outgoing edge.
gimple *stmt;
if (ogr)
stmt = ogr->edge_range_p (lhs, e);
else
{
stmt = gimple_outgoing_range_stmt_p (e->src);
if (stmt && is_a<gcond *> (stmt))
gcond_edge_range (lhs, e);
else
stmt = NULL;
}
if (!stmt)
return false;
gori_calc_operands (lhs, stmt, r, q);
return true;
}
// Helper for GORI_NAME_ON_EDGE which uses query Q to determine if STMT
// provides a range for NAME, and returns it in R if so. If it does not,
// continue processing feeding statments until we run out of statements
// or fine a range for NAME.
bool
gori_name_helper (vrange &r, tree name, vrange &lhs, gimple *stmt,
range_query *q)
{
struct gori_stmt_info si(lhs, stmt, q);
if (!si)
return false;
if (si.ssa1 == name)
return si.calc_op1 (r, lhs, si.op2_range);
if (si.ssa2 == name)
return si.calc_op2 (r, lhs, si.op1_range);
Value_Range tmp;
// Now evaluate operand ranges, and set them in the edge cache.
// If there was already a range, leave it and do no further evaluation.
if (si.ssa1)
{
tmp.set_type (TREE_TYPE (si.ssa1));
if (si.calc_op1 (tmp, lhs, si.op2_range))
si.op1_range.intersect (tmp);
gimple *src = SSA_NAME_DEF_STMT (si.ssa1);
// If defintion is in the same basic lock, evaluate it.
if (src && gimple_bb (src) == gimple_bb (stmt))
if (gori_name_helper (r, name, si.op1_range, src, q))
return true;
}
if (si.ssa2)
{
tmp.set_type (TREE_TYPE (si.ssa2));
if (si.calc_op2 (tmp, lhs, si.op1_range))
si.op2_range.intersect (tmp);
gimple *src = SSA_NAME_DEF_STMT (si.ssa2);
if (src && gimple_bb (src) == gimple_bb (stmt))
if (gori_name_helper (r, name, si.op2_range, src, q))
return true;
}
return false;
}
// Check if NAME has an outgoing range on edge E. Use query Q to evaluate
// the operands. Return TRUE and the range in R if there is an outgoing range.
// This is like gori_on_edge except it only looks for the single name and
// does not require an ssa_cache.
bool
gori_name_on_edge (vrange &r, tree name, edge e, range_query *q)
{
int_range_max lhs;
gimple *stmt = gimple_outgoing_range_stmt_p (e->src);
if (!stmt || !is_a<gcond *> (stmt))
return false;
gcond_edge_range (lhs, e);
return gori_name_helper (r, name, lhs, stmt, q);
}
|