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
|
/* Code for GIMPLE range related routines.
Copyright (C) 2019-2022 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 "insn-codes.h"
#include "tree.h"
#include "gimple.h"
#include "ssa.h"
#include "gimple-pretty-print.h"
#include "optabs-tree.h"
#include "gimple-fold.h"
#include "wide-int.h"
#include "fold-const.h"
#include "case-cfn-macros.h"
#include "omp-general.h"
#include "cfgloop.h"
#include "tree-ssa-loop.h"
#include "tree-scalar-evolution.h"
#include "langhooks.h"
#include "vr-values.h"
#include "range.h"
#include "value-query.h"
#include "range-op.h"
#include "gimple-range.h"
// Construct a fur_source, and set the m_query field.
fur_source::fur_source (range_query *q)
{
if (q)
m_query = q;
else if (cfun)
m_query = get_range_query (cfun);
else
m_query = get_global_range_query ();
m_gori = NULL;
}
// Invoke range_of_expr on EXPR.
bool
fur_source::get_operand (irange &r, tree expr)
{
return m_query->range_of_expr (r, expr);
}
// Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
// range_query to get the range on the edge.
bool
fur_source::get_phi_operand (irange &r, tree expr, edge e)
{
return m_query->range_on_edge (r, e, expr);
}
// Default is no relation.
relation_kind
fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
tree op2 ATTRIBUTE_UNUSED)
{
return VREL_NONE;
}
// Default registers nothing.
void
fur_source::register_relation (gimple *s ATTRIBUTE_UNUSED,
relation_kind k ATTRIBUTE_UNUSED,
tree op1 ATTRIBUTE_UNUSED,
tree op2 ATTRIBUTE_UNUSED)
{
}
// Default registers nothing.
void
fur_source::register_relation (edge e ATTRIBUTE_UNUSED,
relation_kind k ATTRIBUTE_UNUSED,
tree op1 ATTRIBUTE_UNUSED,
tree op2 ATTRIBUTE_UNUSED)
{
}
// This version of fur_source will pick a range up off an edge.
class fur_edge : public fur_source
{
public:
fur_edge (edge e, range_query *q = NULL);
virtual bool get_operand (irange &r, tree expr) OVERRIDE;
virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
private:
edge m_edge;
};
// Instantiate an edge based fur_source.
inline
fur_edge::fur_edge (edge e, range_query *q) : fur_source (q)
{
m_edge = e;
}
// Get the value of EXPR on edge m_edge.
bool
fur_edge::get_operand (irange &r, tree expr)
{
return m_query->range_on_edge (r, m_edge, expr);
}
// Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
// range_query to get the range on the edge.
bool
fur_edge::get_phi_operand (irange &r, tree expr, edge e)
{
// Edge to edge recalculations not supoprted yet, until we sort it out.
gcc_checking_assert (e == m_edge);
return m_query->range_on_edge (r, e, expr);
}
// Instantiate a stmt based fur_source.
fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
{
m_stmt = s;
}
// Retreive range of EXPR as it occurs as a use on stmt M_STMT.
bool
fur_stmt::get_operand (irange &r, tree expr)
{
return m_query->range_of_expr (r, expr, m_stmt);
}
// Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
// range_query to get the range on the edge.
bool
fur_stmt::get_phi_operand (irange &r, tree expr, edge e)
{
// Pick up the range of expr from edge E.
fur_edge e_src (e, m_query);
return e_src.get_operand (r, expr);
}
// Return relation based from m_stmt.
relation_kind
fur_stmt::query_relation (tree op1, tree op2)
{
return m_query->query_relation (m_stmt, op1, op2);
}
// Instantiate a stmt based fur_source with a GORI object.
fur_depend::fur_depend (gimple *s, gori_compute *gori, range_query *q)
: fur_stmt (s, q)
{
gcc_checking_assert (gori);
m_gori = gori;
// Set relations if there is an oracle in the range_query.
// This will enable registering of relationships as they are discovered.
m_oracle = q->oracle ();
}
// Register a relation on a stmt if there is an oracle.
void
fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
{
if (m_oracle)
m_oracle->register_stmt (s, k, op1, op2);
}
// Register a relation on an edge if there is an oracle.
void
fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
{
if (m_oracle)
m_oracle->register_edge (e, k, op1, op2);
}
// This version of fur_source will pick a range up from a list of ranges
// supplied by the caller.
class fur_list : public fur_source
{
public:
fur_list (irange &r1);
fur_list (irange &r1, irange &r2);
fur_list (unsigned num, irange *list);
virtual bool get_operand (irange &r, tree expr) OVERRIDE;
virtual bool get_phi_operand (irange &r, tree expr, edge e) OVERRIDE;
private:
int_range_max m_local[2];
irange *m_list;
unsigned m_index;
unsigned m_limit;
};
// One range supplied for unary operations.
fur_list::fur_list (irange &r1) : fur_source (NULL)
{
m_list = m_local;
m_index = 0;
m_limit = 1;
m_local[0] = r1;
}
// Two ranges supplied for binary operations.
fur_list::fur_list (irange &r1, irange &r2) : fur_source (NULL)
{
m_list = m_local;
m_index = 0;
m_limit = 2;
m_local[0] = r1;
m_local[1] = r2;
}
// Arbitrary number of ranges in a vector.
fur_list::fur_list (unsigned num, irange *list) : fur_source (NULL)
{
m_list = list;
m_index = 0;
m_limit = num;
}
// Get the next operand from the vector, ensure types are compatible.
bool
fur_list::get_operand (irange &r, tree expr)
{
if (m_index >= m_limit)
return m_query->range_of_expr (r, expr);
r = m_list[m_index++];
gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
return true;
}
// This will simply pick the next operand from the vector.
bool
fur_list::get_phi_operand (irange &r, tree expr, edge e ATTRIBUTE_UNUSED)
{
return get_operand (r, expr);
}
// Fold stmt S into range R using R1 as the first operand.
bool
fold_range (irange &r, gimple *s, irange &r1)
{
fold_using_range f;
fur_list src (r1);
return f.fold_stmt (r, s, src);
}
// Fold stmt S into range R using R1 and R2 as the first two operands.
bool
fold_range (irange &r, gimple *s, irange &r1, irange &r2)
{
fold_using_range f;
fur_list src (r1, r2);
return f.fold_stmt (r, s, src);
}
// Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
// operands encountered.
bool
fold_range (irange &r, gimple *s, unsigned num_elements, irange *vector)
{
fold_using_range f;
fur_list src (num_elements, vector);
return f.fold_stmt (r, s, src);
}
// Fold stmt S into range R using range query Q.
bool
fold_range (irange &r, gimple *s, range_query *q)
{
fold_using_range f;
fur_stmt src (s, q);
return f.fold_stmt (r, s, src);
}
// Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
bool
fold_range (irange &r, gimple *s, edge on_edge, range_query *q)
{
fold_using_range f;
fur_edge src (on_edge, q);
return f.fold_stmt (r, s, src);
}
// -------------------------------------------------------------------------
// Adjust the range for a pointer difference where the operands came
// from a memchr.
//
// This notices the following sequence:
//
// def = __builtin_memchr (arg, 0, sz)
// n = def - arg
//
// The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
static void
adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
{
tree op0 = gimple_assign_rhs1 (diff_stmt);
tree op1 = gimple_assign_rhs2 (diff_stmt);
tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
gimple *call;
if (TREE_CODE (op0) == SSA_NAME
&& TREE_CODE (op1) == SSA_NAME
&& (call = SSA_NAME_DEF_STMT (op0))
&& is_gimple_call (call)
&& gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
&& TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
&& TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
&& TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
&& TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
&& gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
&& vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
&& integer_zerop (gimple_call_arg (call, 1)))
{
tree max = vrp_val_max (ptrdiff_type_node);
unsigned prec = TYPE_PRECISION (TREE_TYPE (max));
wide_int wmaxm1 = wi::to_wide (max, prec) - 1;
res.intersect (wi::zero (prec), wmaxm1);
}
}
// Adjust the range for an IMAGPART_EXPR.
static void
adjust_imagpart_expr (irange &res, const gimple *stmt)
{
tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
return;
gimple *def_stmt = SSA_NAME_DEF_STMT (name);
if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
{
switch (gimple_call_internal_fn (def_stmt))
{
case IFN_ADD_OVERFLOW:
case IFN_SUB_OVERFLOW:
case IFN_MUL_OVERFLOW:
case IFN_ATOMIC_COMPARE_EXCHANGE:
{
int_range<2> r;
r.set_varying (boolean_type_node);
tree type = TREE_TYPE (gimple_assign_lhs (stmt));
range_cast (r, type);
res.intersect (r);
}
default:
break;
}
return;
}
if (is_gimple_assign (def_stmt)
&& gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
{
tree cst = gimple_assign_rhs1 (def_stmt);
if (TREE_CODE (cst) == COMPLEX_CST)
{
wide_int imag = wi::to_wide (TREE_IMAGPART (cst));
res.intersect (imag, imag);
}
}
}
// Adjust the range for a REALPART_EXPR.
static void
adjust_realpart_expr (irange &res, const gimple *stmt)
{
tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
if (TREE_CODE (name) != SSA_NAME)
return;
gimple *def_stmt = SSA_NAME_DEF_STMT (name);
if (!SSA_NAME_DEF_STMT (name))
return;
if (is_gimple_assign (def_stmt)
&& gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
{
tree cst = gimple_assign_rhs1 (def_stmt);
if (TREE_CODE (cst) == COMPLEX_CST)
{
tree imag = TREE_REALPART (cst);
int_range<2> tmp (imag, imag);
res.intersect (tmp);
}
}
}
// This function looks for situations when walking the use/def chains
// may provide additonal contextual range information not exposed on
// this statement.
static void
gimple_range_adjustment (irange &res, const gimple *stmt)
{
switch (gimple_expr_code (stmt))
{
case POINTER_DIFF_EXPR:
adjust_pointer_diff_expr (res, stmt);
return;
case IMAGPART_EXPR:
adjust_imagpart_expr (res, stmt);
return;
case REALPART_EXPR:
adjust_realpart_expr (res, stmt);
return;
default:
break;
}
}
// Return the base of the RHS of an assignment.
static tree
gimple_range_base_of_assignment (const gimple *stmt)
{
gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
tree op1 = gimple_assign_rhs1 (stmt);
if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
return get_base_address (TREE_OPERAND (op1, 0));
return op1;
}
// Return the first operand of this statement if it is a valid operand
// supported by ranges, otherwise return NULL_TREE. Special case is
// &(SSA_NAME expr), return the SSA_NAME instead of the ADDR expr.
tree
gimple_range_operand1 (const gimple *stmt)
{
gcc_checking_assert (gimple_range_handler (stmt));
switch (gimple_code (stmt))
{
case GIMPLE_COND:
return gimple_cond_lhs (stmt);
case GIMPLE_ASSIGN:
{
tree base = gimple_range_base_of_assignment (stmt);
if (base && TREE_CODE (base) == MEM_REF)
{
// If the base address is an SSA_NAME, we return it
// here. This allows processing of the range of that
// name, while the rest of the expression is simply
// ignored. The code in range_ops will see the
// ADDR_EXPR and do the right thing.
tree ssa = TREE_OPERAND (base, 0);
if (TREE_CODE (ssa) == SSA_NAME)
return ssa;
}
return base;
}
default:
break;
}
return NULL;
}
// Return the second operand of statement STMT, otherwise return NULL_TREE.
tree
gimple_range_operand2 (const gimple *stmt)
{
gcc_checking_assert (gimple_range_handler (stmt));
switch (gimple_code (stmt))
{
case GIMPLE_COND:
return gimple_cond_rhs (stmt);
case GIMPLE_ASSIGN:
if (gimple_num_ops (stmt) >= 3)
return gimple_assign_rhs2 (stmt);
default:
break;
}
return NULL_TREE;
}
// Calculate a range for statement S and return it in R. If NAME is provided it
// represents the SSA_NAME on the LHS of the statement. It is only required
// if there is more than one lhs/output. If a range cannot
// be calculated, return false.
bool
fold_using_range::fold_stmt (irange &r, gimple *s, fur_source &src, tree name)
{
bool res = false;
// If name and S are specified, make sure it is an LHS of S.
gcc_checking_assert (!name || !gimple_get_lhs (s) ||
name == gimple_get_lhs (s));
if (!name)
name = gimple_get_lhs (s);
// Process addresses.
if (gimple_code (s) == GIMPLE_ASSIGN
&& gimple_assign_rhs_code (s) == ADDR_EXPR)
return range_of_address (r, s, src);
if (gimple_range_handler (s))
res = range_of_range_op (r, s, src);
else if (is_a<gphi *>(s))
res = range_of_phi (r, as_a<gphi *> (s), src);
else if (is_a<gcall *>(s))
res = range_of_call (r, as_a<gcall *> (s), src);
else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
res = range_of_cond_expr (r, as_a<gassign *> (s), src);
if (!res)
{
// If no name specified or range is unsupported, bail.
if (!name || !gimple_range_ssa_p (name))
return false;
// We don't understand the stmt, so return the global range.
r = gimple_range_global (name);
return true;
}
if (r.undefined_p ())
return true;
// We sometimes get compatible types copied from operands, make sure
// the correct type is being returned.
if (name && TREE_TYPE (name) != r.type ())
{
gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
range_cast (r, TREE_TYPE (name));
}
return true;
}
// Calculate a range for range_op statement S and return it in R. If any
// If a range cannot be calculated, return false.
bool
fold_using_range::range_of_range_op (irange &r, gimple *s, fur_source &src)
{
int_range_max range1, range2;
tree type = gimple_range_type (s);
if (!type)
return false;
range_operator *handler = gimple_range_handler (s);
gcc_checking_assert (handler);
tree lhs = gimple_get_lhs (s);
tree op1 = gimple_range_operand1 (s);
tree op2 = gimple_range_operand2 (s);
if (src.get_operand (range1, op1))
{
if (!op2)
{
// Fold range, and register any dependency if available.
int_range<2> r2 (type);
handler->fold_range (r, type, range1, r2);
if (lhs && gimple_range_ssa_p (op1))
{
if (src.gori ())
src.gori ()->register_dependency (lhs, op1);
relation_kind rel;
rel = handler->lhs_op1_relation (r, range1, range1);
if (rel != VREL_NONE)
src.register_relation (s, rel, lhs, op1);
}
}
else if (src.get_operand (range2, op2))
{
relation_kind rel = src.query_relation (op1, op2);
if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_NONE)
{
fprintf (dump_file, " folding with relation ");
print_generic_expr (dump_file, op1, TDF_SLIM);
print_relation (dump_file, rel);
print_generic_expr (dump_file, op2, TDF_SLIM);
fputc ('\n', dump_file);
}
// Fold range, and register any dependency if available.
handler->fold_range (r, type, range1, range2, rel);
relation_fold_and_or (r, s, src);
if (lhs)
{
if (src.gori ())
{
src.gori ()->register_dependency (lhs, op1);
src.gori ()->register_dependency (lhs, op2);
}
if (gimple_range_ssa_p (op1))
{
rel = handler->lhs_op1_relation (r, range1, range2);
if (rel != VREL_NONE)
src.register_relation (s, rel, lhs, op1);
}
if (gimple_range_ssa_p (op2))
{
rel= handler->lhs_op2_relation (r, range1, range2);
if (rel != VREL_NONE)
src.register_relation (s, rel, lhs, op2);
}
}
// Check for an existing BB, as we maybe asked to fold an
// artificial statement not in the CFG.
else if (is_a<gcond *> (s) && gimple_bb (s))
{
basic_block bb = gimple_bb (s);
edge e0 = EDGE_SUCC (bb, 0);
edge e1 = EDGE_SUCC (bb, 1);
if (!single_pred_p (e0->dest))
e0 = NULL;
if (!single_pred_p (e1->dest))
e1 = NULL;
src.register_outgoing_edges (as_a<gcond *> (s), r, e0, e1);
}
}
else
r.set_varying (type);
}
else
r.set_varying (type);
// Make certain range-op adjustments that aren't handled any other way.
gimple_range_adjustment (r, s);
return true;
}
// Calculate the range of an assignment containing an ADDR_EXPR.
// Return the range in R.
// If a range cannot be calculated, set it to VARYING and return true.
bool
fold_using_range::range_of_address (irange &r, gimple *stmt, fur_source &src)
{
gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
bool strict_overflow_p;
tree expr = gimple_assign_rhs1 (stmt);
poly_int64 bitsize, bitpos;
tree offset;
machine_mode mode;
int unsignedp, reversep, volatilep;
tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
&bitpos, &offset, &mode, &unsignedp,
&reversep, &volatilep);
if (base != NULL_TREE
&& TREE_CODE (base) == MEM_REF
&& TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
{
tree ssa = TREE_OPERAND (base, 0);
tree lhs = gimple_get_lhs (stmt);
if (lhs && gimple_range_ssa_p (ssa) && src.gori ())
src.gori ()->register_dependency (lhs, ssa);
gcc_checking_assert (irange::supports_type_p (TREE_TYPE (ssa)));
src.get_operand (r, ssa);
range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
poly_offset_int off = 0;
bool off_cst = false;
if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
{
off = mem_ref_offset (base);
if (offset)
off += poly_offset_int::from (wi::to_poly_wide (offset),
SIGNED);
off <<= LOG2_BITS_PER_UNIT;
off += bitpos;
off_cst = true;
}
/* If &X->a is equal to X, the range of X is the result. */
if (off_cst && known_eq (off, 0))
return true;
else if (flag_delete_null_pointer_checks
&& !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
{
/* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
allow going from non-NULL pointer to NULL. */
if (!range_includes_zero_p (&r))
{
/* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
using POINTER_PLUS_EXPR if off_cst and just fall back to
this. */
r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
return true;
}
}
/* If MEM_REF has a "positive" offset, consider it non-NULL
always, for -fdelete-null-pointer-checks also "negative"
ones. Punt for unknown offsets (e.g. variable ones). */
if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
&& off_cst
&& known_ne (off, 0)
&& (flag_delete_null_pointer_checks || known_gt (off, 0)))
{
r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
return true;
}
r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
return true;
}
// Handle "= &a".
if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
{
r = range_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
return true;
}
// Otherwise return varying.
r = int_range<2> (TREE_TYPE (gimple_assign_rhs1 (stmt)));
return true;
}
// Calculate a range for phi statement S and return it in R.
// If a range cannot be calculated, return false.
bool
fold_using_range::range_of_phi (irange &r, gphi *phi, fur_source &src)
{
tree phi_def = gimple_phi_result (phi);
tree type = gimple_range_type (phi);
int_range_max arg_range;
int_range_max equiv_range;
unsigned x;
if (!type)
return false;
// Track if all executable arguments are the same.
tree single_arg = NULL_TREE;
bool seen_arg = false;
// Start with an empty range, unioning in each argument's range.
r.set_undefined ();
for (x = 0; x < gimple_phi_num_args (phi); x++)
{
tree arg = gimple_phi_arg_def (phi, x);
// An argument that is the same as the def provides no new range.
if (arg == phi_def)
continue;
edge e = gimple_phi_arg_edge (phi, x);
// Get the range of the argument on its edge.
src.get_phi_operand (arg_range, arg, e);
if (!arg_range.undefined_p ())
{
// Register potential dependencies for stale value tracking.
// Likewise, if the incoming PHI argument is equivalent to this
// PHI definition, it provides no new info. Accumulate these ranges
// in case all arguments are equivalences.
if (src.query ()->query_relation (e, arg, phi_def, false) == EQ_EXPR)
equiv_range.union_(arg_range);
else
r.union_ (arg_range);
if (gimple_range_ssa_p (arg) && src.gori ())
src.gori ()->register_dependency (phi_def, arg);
// Track if all arguments are the same.
if (!seen_arg)
{
seen_arg = true;
single_arg = arg;
}
else if (single_arg != arg)
single_arg = NULL_TREE;
}
// Once the value reaches varying, stop looking.
if (r.varying_p () && single_arg == NULL_TREE)
break;
}
// If all arguments were equivalences, use the equivalence ranges as no
// arguments were processed.
if (r.undefined_p () && !equiv_range.undefined_p ())
r = equiv_range;
// If the PHI boils down to a single effective argument, look at it.
if (single_arg)
{
// Symbolic arguments are equivalences.
if (gimple_range_ssa_p (single_arg))
src.register_relation (phi, EQ_EXPR, phi_def, single_arg);
else if (src.get_operand (arg_range, single_arg)
&& arg_range.singleton_p ())
{
// Numerical arguments that are a constant can be returned as
// the constant. This can help fold later cases where even this
// constant might have been UNDEFINED via an unreachable edge.
r = arg_range;
return true;
}
}
// If SCEV is available, query if this PHI has any knonwn values.
if (scev_initialized_p () && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
{
value_range loop_range;
class loop *l = loop_containing_stmt (phi);
if (l && loop_outer (l))
{
range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
if (!loop_range.varying_p ())
{
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, " Loops range found for ");
print_generic_expr (dump_file, phi_def, TDF_SLIM);
fprintf (dump_file, ": ");
loop_range.dump (dump_file);
fprintf (dump_file, " and calculated range :");
r.dump (dump_file);
fprintf (dump_file, "\n");
}
r.intersect (loop_range);
}
}
}
return true;
}
// Calculate a range for call statement S and return it in R.
// If a range cannot be calculated, return false.
bool
fold_using_range::range_of_call (irange &r, gcall *call, fur_source &src)
{
tree type = gimple_range_type (call);
if (!type)
return false;
tree lhs = gimple_call_lhs (call);
bool strict_overflow_p;
if (range_of_builtin_call (r, call, src))
;
else if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
r.set (build_int_cst (type, 0), TYPE_MAX_VALUE (type));
else if (gimple_call_nonnull_result_p (call)
|| gimple_call_nonnull_arg (call))
r = range_nonzero (type);
else
r.set_varying (type);
// If there is an LHS, intersect that with what is known.
if (lhs)
{
value_range def;
def = gimple_range_global (lhs);
r.intersect (def);
}
return true;
}
// Return the range of a __builtin_ubsan* in CALL and set it in R.
// CODE is the type of ubsan call (PLUS_EXPR, MINUS_EXPR or
// MULT_EXPR).
void
fold_using_range::range_of_builtin_ubsan_call (irange &r, gcall *call,
tree_code code, fur_source &src)
{
gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR
|| code == MULT_EXPR);
tree type = gimple_range_type (call);
range_operator *op = range_op_handler (code, type);
gcc_checking_assert (op);
int_range_max ir0, ir1;
tree arg0 = gimple_call_arg (call, 0);
tree arg1 = gimple_call_arg (call, 1);
src.get_operand (ir0, arg0);
src.get_operand (ir1, arg1);
// Check for any relation between arg0 and arg1.
relation_kind relation = src.query_relation (arg0, arg1);
bool saved_flag_wrapv = flag_wrapv;
// Pretend the arithmetic is wrapping. If there is any overflow,
// we'll complain, but will actually do wrapping operation.
flag_wrapv = 1;
op->fold_range (r, type, ir0, ir1, relation);
flag_wrapv = saved_flag_wrapv;
// If for both arguments vrp_valueize returned non-NULL, this should
// have been already folded and if not, it wasn't folded because of
// overflow. Avoid removing the UBSAN_CHECK_* calls in that case.
if (r.singleton_p ())
r.set_varying (type);
}
// Return TRUE if we recognize the target character set and return the
// range for lower case and upper case letters.
static bool
get_letter_range (tree type, irange &lowers, irange &uppers)
{
// ASCII
int a = lang_hooks.to_target_charset ('a');
int z = lang_hooks.to_target_charset ('z');
int A = lang_hooks.to_target_charset ('A');
int Z = lang_hooks.to_target_charset ('Z');
if ((z - a == 25) && (Z - A == 25))
{
lowers = int_range<2> (build_int_cst (type, a), build_int_cst (type, z));
uppers = int_range<2> (build_int_cst (type, A), build_int_cst (type, Z));
return true;
}
// Unknown character set.
return false;
}
// For a builtin in CALL, return a range in R if known and return
// TRUE. Otherwise return FALSE.
bool
fold_using_range::range_of_builtin_call (irange &r, gcall *call,
fur_source &src)
{
combined_fn func = gimple_call_combined_fn (call);
if (func == CFN_LAST)
return false;
tree type = gimple_range_type (call);
tree arg;
int mini, maxi, zerov = 0, prec;
scalar_int_mode mode;
switch (func)
{
case CFN_BUILT_IN_CONSTANT_P:
arg = gimple_call_arg (call, 0);
if (src.get_operand (r, arg) && r.singleton_p ())
{
r.set (build_one_cst (type), build_one_cst (type));
return true;
}
if (cfun->after_inlining)
{
r.set_zero (type);
// r.equiv_clear ();
return true;
}
break;
case CFN_BUILT_IN_TOUPPER:
{
arg = gimple_call_arg (call, 0);
// If the argument isn't compatible with the LHS, do nothing.
if (!range_compatible_p (type, TREE_TYPE (arg)))
return false;
if (!src.get_operand (r, arg))
return false;
int_range<3> lowers;
int_range<3> uppers;
if (!get_letter_range (type, lowers, uppers))
return false;
// Return the range passed in without any lower case characters,
// but including all the upper case ones.
lowers.invert ();
r.intersect (lowers);
r.union_ (uppers);
return true;
}
case CFN_BUILT_IN_TOLOWER:
{
arg = gimple_call_arg (call, 0);
// If the argument isn't compatible with the LHS, do nothing.
if (!range_compatible_p (type, TREE_TYPE (arg)))
return false;
if (!src.get_operand (r, arg))
return false;
int_range<3> lowers;
int_range<3> uppers;
if (!get_letter_range (type, lowers, uppers))
return false;
// Return the range passed in without any upper case characters,
// but including all the lower case ones.
uppers.invert ();
r.intersect (uppers);
r.union_ (lowers);
return true;
}
CASE_CFN_FFS:
CASE_CFN_POPCOUNT:
// __builtin_ffs* and __builtin_popcount* return [0, prec].
arg = gimple_call_arg (call, 0);
prec = TYPE_PRECISION (TREE_TYPE (arg));
mini = 0;
maxi = prec;
src.get_operand (r, arg);
// If arg is non-zero, then ffs or popcount are non-zero.
if (!range_includes_zero_p (&r))
mini = 1;
// If some high bits are known to be zero, decrease the maximum.
if (!r.undefined_p ())
{
if (TYPE_SIGN (r.type ()) == SIGNED)
range_cast (r, unsigned_type_for (r.type ()));
wide_int max = r.upper_bound ();
maxi = wi::floor_log2 (max) + 1;
}
r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
return true;
CASE_CFN_PARITY:
r.set (build_zero_cst (type), build_one_cst (type));
return true;
CASE_CFN_CLZ:
// __builtin_c[lt]z* return [0, prec-1], except when the
// argument is 0, but that is undefined behavior.
//
// For __builtin_c[lt]z* consider argument of 0 always undefined
// behavior, for internal fns depending on C?Z_DEFINED_VALUE_AT_ZERO.
arg = gimple_call_arg (call, 0);
prec = TYPE_PRECISION (TREE_TYPE (arg));
mini = 0;
maxi = prec - 1;
mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
if (gimple_call_internal_p (call))
{
if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
&& CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
{
// Only handle the single common value.
if (zerov == prec)
maxi = prec;
else
// Magic value to give up, unless we can prove arg is non-zero.
mini = -2;
}
}
src.get_operand (r, arg);
// From clz of minimum we can compute result maximum.
if (!r.undefined_p ())
{
// From clz of minimum we can compute result maximum.
if (wi::gt_p (r.lower_bound (), 0, TYPE_SIGN (r.type ())))
{
maxi = prec - 1 - wi::floor_log2 (r.lower_bound ());
if (mini == -2)
mini = 0;
}
else if (!range_includes_zero_p (&r))
{
mini = 0;
maxi = prec - 1;
}
if (mini == -2)
break;
// From clz of maximum we can compute result minimum.
wide_int max = r.upper_bound ();
int newmini = prec - 1 - wi::floor_log2 (max);
if (max == 0)
{
// If CLZ_DEFINED_VALUE_AT_ZERO is 2 with VALUE of prec,
// return [prec, prec], otherwise ignore the range.
if (maxi == prec)
mini = prec;
}
else
mini = newmini;
}
if (mini == -2)
break;
r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
return true;
CASE_CFN_CTZ:
// __builtin_ctz* return [0, prec-1], except for when the
// argument is 0, but that is undefined behavior.
//
// For __builtin_ctz* consider argument of 0 always undefined
// behavior, for internal fns depending on CTZ_DEFINED_VALUE_AT_ZERO.
arg = gimple_call_arg (call, 0);
prec = TYPE_PRECISION (TREE_TYPE (arg));
mini = 0;
maxi = prec - 1;
mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
if (gimple_call_internal_p (call))
{
if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
&& CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov) == 2)
{
// Handle only the two common values.
if (zerov == -1)
mini = -1;
else if (zerov == prec)
maxi = prec;
else
// Magic value to give up, unless we can prove arg is non-zero.
mini = -2;
}
}
src.get_operand (r, arg);
if (!r.undefined_p ())
{
// If arg is non-zero, then use [0, prec - 1].
if (!range_includes_zero_p (&r))
{
mini = 0;
maxi = prec - 1;
}
// If some high bits are known to be zero, we can decrease
// the maximum.
wide_int max = r.upper_bound ();
if (max == 0)
{
// Argument is [0, 0]. If CTZ_DEFINED_VALUE_AT_ZERO
// is 2 with value -1 or prec, return [-1, -1] or [prec, prec].
// Otherwise ignore the range.
if (mini == -1)
maxi = -1;
else if (maxi == prec)
mini = prec;
}
// If value at zero is prec and 0 is in the range, we can't lower
// the upper bound. We could create two separate ranges though,
// [0,floor_log2(max)][prec,prec] though.
else if (maxi != prec)
maxi = wi::floor_log2 (max);
}
if (mini == -2)
break;
r.set (build_int_cst (type, mini), build_int_cst (type, maxi));
return true;
CASE_CFN_CLRSB:
arg = gimple_call_arg (call, 0);
prec = TYPE_PRECISION (TREE_TYPE (arg));
r.set (build_int_cst (type, 0), build_int_cst (type, prec - 1));
return true;
case CFN_UBSAN_CHECK_ADD:
range_of_builtin_ubsan_call (r, call, PLUS_EXPR, src);
return true;
case CFN_UBSAN_CHECK_SUB:
range_of_builtin_ubsan_call (r, call, MINUS_EXPR, src);
return true;
case CFN_UBSAN_CHECK_MUL:
range_of_builtin_ubsan_call (r, call, MULT_EXPR, src);
return true;
case CFN_GOACC_DIM_SIZE:
case CFN_GOACC_DIM_POS:
// Optimizing these two internal functions helps the loop
// optimizer eliminate outer comparisons. Size is [1,N]
// and pos is [0,N-1].
{
bool is_pos = func == CFN_GOACC_DIM_POS;
int axis = oacc_get_ifn_dim_arg (call);
int size = oacc_get_fn_dim_size (current_function_decl, axis);
if (!size)
// If it's dynamic, the backend might know a hardware limitation.
size = targetm.goacc.dim_limit (axis);
r.set (build_int_cst (type, is_pos ? 0 : 1),
size
? build_int_cst (type, size - is_pos) : vrp_val_max (type));
return true;
}
case CFN_BUILT_IN_STRLEN:
if (tree lhs = gimple_call_lhs (call))
if (ptrdiff_type_node
&& (TYPE_PRECISION (ptrdiff_type_node)
== TYPE_PRECISION (TREE_TYPE (lhs))))
{
tree type = TREE_TYPE (lhs);
tree max = vrp_val_max (ptrdiff_type_node);
wide_int wmax
= wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
tree range_min = build_zero_cst (type);
// To account for the terminating NULL, the maximum length
// is one less than the maximum array size, which in turn
// is one less than PTRDIFF_MAX (or SIZE_MAX where it's
// smaller than the former type).
// FIXME: Use max_object_size() - 1 here.
tree range_max = wide_int_to_tree (type, wmax - 2);
r.set (range_min, range_max);
return true;
}
break;
default:
break;
}
return false;
}
// Calculate a range for COND_EXPR statement S and return it in R.
// If a range cannot be calculated, return false.
bool
fold_using_range::range_of_cond_expr (irange &r, gassign *s, fur_source &src)
{
int_range_max cond_range, range1, range2;
tree cond = gimple_assign_rhs1 (s);
tree op1 = gimple_assign_rhs2 (s);
tree op2 = gimple_assign_rhs3 (s);
tree type = gimple_range_type (s);
if (!type)
return false;
gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
src.get_operand (cond_range, cond);
src.get_operand (range1, op1);
src.get_operand (range2, op2);
// Try to see if there is a dependence between the COND and either operand
if (src.gori ())
if (src.gori ()->condexpr_adjust (range1, range2, s, cond, op1, op2, src))
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
range1.dump(dump_file);
fprintf (dump_file, " and Range op2: ");
range2.dump(dump_file);
fprintf (dump_file, "\n");
}
// If the condition is known, choose the appropriate expression.
if (cond_range.singleton_p ())
{
// False, pick second operand.
if (cond_range.zero_p ())
r = range2;
else
r = range1;
}
else
{
r = range1;
r.union_ (range2);
}
gcc_checking_assert (r.undefined_p ()
|| range_compatible_p (r.type (), type));
return true;
}
// If SCEV has any information about phi node NAME, return it as a range in R.
void
fold_using_range::range_of_ssa_name_with_loop_info (irange &r, tree name,
class loop *l, gphi *phi,
fur_source &src)
{
gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
tree min, max, type = TREE_TYPE (name);
if (bounds_of_var_in_loop (&min, &max, src.query (), l, phi, name))
{
if (TREE_CODE (min) != INTEGER_CST)
{
if (src.query ()->range_of_expr (r, min, phi) && !r.undefined_p ())
min = wide_int_to_tree (type, r.lower_bound ());
else
min = vrp_val_min (type);
}
if (TREE_CODE (max) != INTEGER_CST)
{
if (src.query ()->range_of_expr (r, max, phi) && !r.undefined_p ())
max = wide_int_to_tree (type, r.upper_bound ());
else
max = vrp_val_max (type);
}
r.set (min, max);
}
else
r.set_varying (type);
}
// -----------------------------------------------------------------------
// Check if an && or || expression can be folded based on relations. ie
// c_2 = a_6 > b_7
// c_3 = a_6 < b_7
// c_4 = c_2 && c_3
// c_2 and c_3 can never be true at the same time,
// Therefore c_4 can always resolve to false based purely on the relations.
void
fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
fur_source &src)
{
// No queries or already folded.
if (!src.gori () || !src.query ()->oracle () || lhs_range.singleton_p ())
return;
// Only care about AND and OR expressions.
enum tree_code code = gimple_expr_code (s);
bool is_and = false;
if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
is_and = true;
else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
return;
tree lhs = gimple_get_lhs (s);
tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
// Deal with || and && only when there is a full set of symbolics.
if (!lhs || !ssa1 || !ssa2
|| (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
|| (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
|| (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
return;
// Now we know its a boolean AND or OR expression with boolean operands.
// Ideally we search dependencies for common names, and see what pops out.
// until then, simply try to resolve direct dependencies.
gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
range_operator *handler1 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa1));
range_operator *handler2 = gimple_range_handler (SSA_NAME_DEF_STMT (ssa2));
// If either handler is not present, no relation can be found.
if (!handler1 || !handler2)
return;
// Both stmts will need to have 2 ssa names in the stmt.
tree ssa1_dep1 = gimple_range_ssa_p (gimple_range_operand1 (ssa1_stmt));
tree ssa1_dep2 = gimple_range_ssa_p (gimple_range_operand2 (ssa1_stmt));
tree ssa2_dep1 = gimple_range_ssa_p (gimple_range_operand1 (ssa2_stmt));
tree ssa2_dep2 = gimple_range_ssa_p (gimple_range_operand2 (ssa2_stmt));
if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
return;
// Make sure they are the same dependencies, and detect the order of the
// relationship.
bool reverse_op2 = true;
if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
reverse_op2 = false;
else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
return;
int_range<2> bool_one (boolean_true_node, boolean_true_node);
relation_kind relation1 = handler1->op1_op2_relation (bool_one);
relation_kind relation2 = handler2->op1_op2_relation (bool_one);
if (relation1 == VREL_NONE || relation2 == VREL_NONE)
return;
if (reverse_op2)
relation2 = relation_negate (relation2);
// x && y is false if the relation intersection of the true cases is NULL.
if (is_and && relation_intersect (relation1, relation2) == VREL_EMPTY)
lhs_range = int_range<2> (boolean_false_node, boolean_false_node);
// x || y is true if the union of the true cases is NO-RELATION..
// ie, one or the other being true covers the full range of possibilties.
else if (!is_and && relation_union (relation1, relation2) == VREL_NONE)
lhs_range = bool_one;
else
return;
range_cast (lhs_range, TREE_TYPE (lhs));
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, " Relation adjustment: ");
print_generic_expr (dump_file, ssa1, TDF_SLIM);
fprintf (dump_file, " and ");
print_generic_expr (dump_file, ssa2, TDF_SLIM);
fprintf (dump_file, " combine to produce ");
lhs_range.dump (dump_file);
fputc ('\n', dump_file);
}
return;
}
// Register any outgoing edge relations from a conditional branch.
void
fur_source::register_outgoing_edges (gcond *s, irange &lhs_range, edge e0, edge e1)
{
int_range_max r;
int_range<2> e0_range, e1_range;
tree name;
range_operator *handler;
basic_block bb = gimple_bb (s);
if (e0)
{
// If this edge is never taken, ignore it.
gcond_edge_range (e0_range, e0);
e0_range.intersect (lhs_range);
if (e0_range.undefined_p ())
e0 = NULL;
}
if (e1)
{
// If this edge is never taken, ignore it.
gcond_edge_range (e1_range, e1);
e1_range.intersect (lhs_range);
if (e1_range.undefined_p ())
e1 = NULL;
}
if (!e0 && !e1)
return;
// First, register the gcond itself. This will catch statements like
// if (a_2 < b_5)
tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (s));
tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (s));
if (ssa1 && ssa2)
{
handler = gimple_range_handler (s);
gcc_checking_assert (handler);
if (e0)
{
relation_kind relation = handler->op1_op2_relation (e0_range);
if (relation != VREL_NONE)
register_relation (e0, relation, ssa1, ssa2);
}
if (e1)
{
relation_kind relation = handler->op1_op2_relation (e1_range);
if (relation != VREL_NONE)
register_relation (e1, relation, ssa1, ssa2);
}
}
// Outgoing relations of GORI exports require a gori engine.
if (!gori ())
return;
// Now look for other relations in the exports. This will find stmts
// leading to the condition such as:
// c_2 = a_4 < b_7
// if (c_2)
FOR_EACH_GORI_EXPORT_NAME (*(gori ()), bb, name)
{
if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
continue;
gimple *stmt = SSA_NAME_DEF_STMT (name);
handler = gimple_range_handler (stmt);
if (!handler)
continue;
tree ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
tree ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
if (ssa1 && ssa2)
{
if (e0 && gori ()->outgoing_edge_range_p (r, e0, name, *m_query)
&& r.singleton_p ())
{
relation_kind relation = handler->op1_op2_relation (r);
if (relation != VREL_NONE)
register_relation (e0, relation, ssa1, ssa2);
}
if (e1 && gori ()->outgoing_edge_range_p (r, e1, name, *m_query)
&& r.singleton_p ())
{
relation_kind relation = handler->op1_op2_relation (r);
if (relation != VREL_NONE)
register_relation (e1, relation, ssa1, ssa2);
}
}
}
}
|