1 /* Tree based points-to analysis 2 Copyright (C) 2005-2019 Free Software Foundation, Inc. 3 Contributed by Daniel Berlin <dberlin@dberlin.org> 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "backend.h" 25 #include "rtl.h" 26 #include "tree.h" 27 #include "gimple.h" 28 #include "alloc-pool.h" 29 #include "tree-pass.h" 30 #include "ssa.h" 31 #include "cgraph.h" 32 #include "tree-pretty-print.h" 33 #include "diagnostic-core.h" 34 #include "fold-const.h" 35 #include "stor-layout.h" 36 #include "stmt.h" 37 #include "gimple-iterator.h" 38 #include "tree-into-ssa.h" 39 #include "tree-dfa.h" 40 #include "params.h" 41 #include "gimple-walk.h" 42 #include "varasm.h" 43 #include "stringpool.h" 44 #include "attribs.h" 45 #include "tree-ssa.h" 46 47 /* The idea behind this analyzer is to generate set constraints from the 48 program, then solve the resulting constraints in order to generate the 49 points-to sets. 50 51 Set constraints are a way of modeling program analysis problems that 52 involve sets. They consist of an inclusion constraint language, 53 describing the variables (each variable is a set) and operations that 54 are involved on the variables, and a set of rules that derive facts 55 from these operations. To solve a system of set constraints, you derive 56 all possible facts under the rules, which gives you the correct sets 57 as a consequence. 58 59 See "Efficient Field-sensitive pointer analysis for C" by "David 60 J. Pearce and Paul H. J. Kelly and Chris Hankin, at 61 http://citeseer.ist.psu.edu/pearce04efficient.html 62 63 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines 64 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at 65 http://citeseer.ist.psu.edu/heintze01ultrafast.html 66 67 There are three types of real constraint expressions, DEREF, 68 ADDRESSOF, and SCALAR. Each constraint expression consists 69 of a constraint type, a variable, and an offset. 70 71 SCALAR is a constraint expression type used to represent x, whether 72 it appears on the LHS or the RHS of a statement. 73 DEREF is a constraint expression type used to represent *x, whether 74 it appears on the LHS or the RHS of a statement. 75 ADDRESSOF is a constraint expression used to represent &x, whether 76 it appears on the LHS or the RHS of a statement. 77 78 Each pointer variable in the program is assigned an integer id, and 79 each field of a structure variable is assigned an integer id as well. 80 81 Structure variables are linked to their list of fields through a "next 82 field" in each variable that points to the next field in offset 83 order. 84 Each variable for a structure field has 85 86 1. "size", that tells the size in bits of that field. 87 2. "fullsize, that tells the size in bits of the entire structure. 88 3. "offset", that tells the offset in bits from the beginning of the 89 structure to this field. 90 91 Thus, 92 struct f 93 { 94 int a; 95 int b; 96 } foo; 97 int *bar; 98 99 looks like 100 101 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b 102 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL 103 bar -> id 3, size 32, offset 0, fullsize 32, next NULL 104 105 106 In order to solve the system of set constraints, the following is 107 done: 108 109 1. Each constraint variable x has a solution set associated with it, 110 Sol(x). 111 112 2. Constraints are separated into direct, copy, and complex. 113 Direct constraints are ADDRESSOF constraints that require no extra 114 processing, such as P = &Q 115 Copy constraints are those of the form P = Q. 116 Complex constraints are all the constraints involving dereferences 117 and offsets (including offsetted copies). 118 119 3. All direct constraints of the form P = &Q are processed, such 120 that Q is added to Sol(P) 121 122 4. All complex constraints for a given constraint variable are stored in a 123 linked list attached to that variable's node. 124 125 5. A directed graph is built out of the copy constraints. Each 126 constraint variable is a node in the graph, and an edge from 127 Q to P is added for each copy constraint of the form P = Q 128 129 6. The graph is then walked, and solution sets are 130 propagated along the copy edges, such that an edge from Q to P 131 causes Sol(P) <- Sol(P) union Sol(Q). 132 133 7. As we visit each node, all complex constraints associated with 134 that node are processed by adding appropriate copy edges to the graph, or the 135 appropriate variables to the solution set. 136 137 8. The process of walking the graph is iterated until no solution 138 sets change. 139 140 Prior to walking the graph in steps 6 and 7, We perform static 141 cycle elimination on the constraint graph, as well 142 as off-line variable substitution. 143 144 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted 145 on and turned into anything), but isn't. You can just see what offset 146 inside the pointed-to struct it's going to access. 147 148 TODO: Constant bounded arrays can be handled as if they were structs of the 149 same number of elements. 150 151 TODO: Modeling heap and incoming pointers becomes much better if we 152 add fields to them as we discover them, which we could do. 153 154 TODO: We could handle unions, but to be honest, it's probably not 155 worth the pain or slowdown. */ 156 157 /* IPA-PTA optimizations possible. 158 159 When the indirect function called is ANYTHING we can add disambiguation 160 based on the function signatures (or simply the parameter count which 161 is the varinfo size). We also do not need to consider functions that 162 do not have their address taken. 163 164 The is_global_var bit which marks escape points is overly conservative 165 in IPA mode. Split it to is_escape_point and is_global_var - only 166 externally visible globals are escape points in IPA mode. 167 There is now is_ipa_escape_point but this is only used in a few 168 selected places. 169 170 The way we introduce DECL_PT_UID to avoid fixing up all points-to 171 sets in the translation unit when we copy a DECL during inlining 172 pessimizes precision. The advantage is that the DECL_PT_UID keeps 173 compile-time and memory usage overhead low - the points-to sets 174 do not grow or get unshared as they would during a fixup phase. 175 An alternative solution is to delay IPA PTA until after all 176 inlining transformations have been applied. 177 178 The way we propagate clobber/use information isn't optimized. 179 It should use a new complex constraint that properly filters 180 out local variables of the callee (though that would make 181 the sets invalid after inlining). OTOH we might as well 182 admit defeat to WHOPR and simply do all the clobber/use analysis 183 and propagation after PTA finished but before we threw away 184 points-to information for memory variables. WHOPR and PTA 185 do not play along well anyway - the whole constraint solving 186 would need to be done in WPA phase and it will be very interesting 187 to apply the results to local SSA names during LTRANS phase. 188 189 We probably should compute a per-function unit-ESCAPE solution 190 propagating it simply like the clobber / uses solutions. The 191 solution can go alongside the non-IPA espaced solution and be 192 used to query which vars escape the unit through a function. 193 This is also required to make the escaped-HEAP trick work in IPA mode. 194 195 We never put function decls in points-to sets so we do not 196 keep the set of called functions for indirect calls. 197 198 And probably more. */ 199 200 static bool use_field_sensitive = true; 201 static int in_ipa_mode = 0; 202 203 /* Used for predecessor bitmaps. */ 204 static bitmap_obstack predbitmap_obstack; 205 206 /* Used for points-to sets. */ 207 static bitmap_obstack pta_obstack; 208 209 /* Used for oldsolution members of variables. */ 210 static bitmap_obstack oldpta_obstack; 211 212 /* Used for per-solver-iteration bitmaps. */ 213 static bitmap_obstack iteration_obstack; 214 215 static unsigned int create_variable_info_for (tree, const char *, bool); 216 typedef struct constraint_graph *constraint_graph_t; 217 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool); 218 219 struct constraint; 220 typedef struct constraint *constraint_t; 221 222 223 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \ 224 if (a) \ 225 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d) 226 227 static struct constraint_stats 228 { 229 unsigned int total_vars; 230 unsigned int nonpointer_vars; 231 unsigned int unified_vars_static; 232 unsigned int unified_vars_dynamic; 233 unsigned int iterations; 234 unsigned int num_edges; 235 unsigned int num_implicit_edges; 236 unsigned int points_to_sets_created; 237 } stats; 238 239 struct variable_info 240 { 241 /* ID of this variable */ 242 unsigned int id; 243 244 /* True if this is a variable created by the constraint analysis, such as 245 heap variables and constraints we had to break up. */ 246 unsigned int is_artificial_var : 1; 247 248 /* True if this is a special variable whose solution set should not be 249 changed. */ 250 unsigned int is_special_var : 1; 251 252 /* True for variables whose size is not known or variable. */ 253 unsigned int is_unknown_size_var : 1; 254 255 /* True for (sub-)fields that represent a whole variable. */ 256 unsigned int is_full_var : 1; 257 258 /* True if this is a heap variable. */ 259 unsigned int is_heap_var : 1; 260 261 /* True if this is a register variable. */ 262 unsigned int is_reg_var : 1; 263 264 /* True if this field may contain pointers. */ 265 unsigned int may_have_pointers : 1; 266 267 /* True if this field has only restrict qualified pointers. */ 268 unsigned int only_restrict_pointers : 1; 269 270 /* True if this represents a heap var created for a restrict qualified 271 pointer. */ 272 unsigned int is_restrict_var : 1; 273 274 /* True if this represents a global variable. */ 275 unsigned int is_global_var : 1; 276 277 /* True if this represents a module escape point for IPA analysis. */ 278 unsigned int is_ipa_escape_point : 1; 279 280 /* True if this represents a IPA function info. */ 281 unsigned int is_fn_info : 1; 282 283 /* ??? Store somewhere better. */ 284 unsigned short ruid; 285 286 /* The ID of the variable for the next field in this structure 287 or zero for the last field in this structure. */ 288 unsigned next; 289 290 /* The ID of the variable for the first field in this structure. */ 291 unsigned head; 292 293 /* Offset of this variable, in bits, from the base variable */ 294 unsigned HOST_WIDE_INT offset; 295 296 /* Size of the variable, in bits. */ 297 unsigned HOST_WIDE_INT size; 298 299 /* Full size of the base variable, in bits. */ 300 unsigned HOST_WIDE_INT fullsize; 301 302 /* In IPA mode the shadow UID in case the variable needs to be duplicated in 303 the final points-to solution because it reaches its containing 304 function recursively. Zero if none is needed. */ 305 unsigned int shadow_var_uid; 306 307 /* Name of this variable */ 308 const char *name; 309 310 /* Tree that this variable is associated with. */ 311 tree decl; 312 313 /* Points-to set for this variable. */ 314 bitmap solution; 315 316 /* Old points-to set for this variable. */ 317 bitmap oldsolution; 318 }; 319 typedef struct variable_info *varinfo_t; 320 321 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT); 322 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t, 323 unsigned HOST_WIDE_INT); 324 static varinfo_t lookup_vi_for_tree (tree); 325 static inline bool type_can_have_subvars (const_tree); 326 static void make_param_constraints (varinfo_t); 327 328 /* Pool of variable info structures. */ 329 static object_allocator<variable_info> variable_info_pool 330 ("Variable info pool"); 331 332 /* Map varinfo to final pt_solution. */ 333 static hash_map<varinfo_t, pt_solution *> *final_solutions; 334 struct obstack final_solutions_obstack; 335 336 /* Table of variable info structures for constraint variables. 337 Indexed directly by variable info id. */ 338 static vec<varinfo_t> varmap; 339 340 /* Return the varmap element N */ 341 342 static inline varinfo_t 343 get_varinfo (unsigned int n) 344 { 345 return varmap[n]; 346 } 347 348 /* Return the next variable in the list of sub-variables of VI 349 or NULL if VI is the last sub-variable. */ 350 351 static inline varinfo_t 352 vi_next (varinfo_t vi) 353 { 354 return get_varinfo (vi->next); 355 } 356 357 /* Static IDs for the special variables. Variable ID zero is unused 358 and used as terminator for the sub-variable chain. */ 359 enum { nothing_id = 1, anything_id = 2, string_id = 3, 360 escaped_id = 4, nonlocal_id = 5, 361 storedanything_id = 6, integer_id = 7 }; 362 363 /* Return a new variable info structure consisting for a variable 364 named NAME, and using constraint graph node NODE. Append it 365 to the vector of variable info structures. */ 366 367 static varinfo_t 368 new_var_info (tree t, const char *name, bool add_id) 369 { 370 unsigned index = varmap.length (); 371 varinfo_t ret = variable_info_pool.allocate (); 372 373 if (dump_file && add_id) 374 { 375 char *tempname = xasprintf ("%s(%d)", name, index); 376 name = ggc_strdup (tempname); 377 free (tempname); 378 } 379 380 ret->id = index; 381 ret->name = name; 382 ret->decl = t; 383 /* Vars without decl are artificial and do not have sub-variables. */ 384 ret->is_artificial_var = (t == NULL_TREE); 385 ret->is_special_var = false; 386 ret->is_unknown_size_var = false; 387 ret->is_full_var = (t == NULL_TREE); 388 ret->is_heap_var = false; 389 ret->may_have_pointers = true; 390 ret->only_restrict_pointers = false; 391 ret->is_restrict_var = false; 392 ret->ruid = 0; 393 ret->is_global_var = (t == NULL_TREE); 394 ret->is_ipa_escape_point = false; 395 ret->is_fn_info = false; 396 if (t && DECL_P (t)) 397 ret->is_global_var = (is_global_var (t) 398 /* We have to treat even local register variables 399 as escape points. */ 400 || (VAR_P (t) && DECL_HARD_REGISTER (t))); 401 ret->is_reg_var = (t && TREE_CODE (t) == SSA_NAME); 402 ret->solution = BITMAP_ALLOC (&pta_obstack); 403 ret->oldsolution = NULL; 404 ret->next = 0; 405 ret->shadow_var_uid = 0; 406 ret->head = ret->id; 407 408 stats.total_vars++; 409 410 varmap.safe_push (ret); 411 412 return ret; 413 } 414 415 /* A map mapping call statements to per-stmt variables for uses 416 and clobbers specific to the call. */ 417 static hash_map<gimple *, varinfo_t> *call_stmt_vars; 418 419 /* Lookup or create the variable for the call statement CALL. */ 420 421 static varinfo_t 422 get_call_vi (gcall *call) 423 { 424 varinfo_t vi, vi2; 425 426 bool existed; 427 varinfo_t *slot_p = &call_stmt_vars->get_or_insert (call, &existed); 428 if (existed) 429 return *slot_p; 430 431 vi = new_var_info (NULL_TREE, "CALLUSED", true); 432 vi->offset = 0; 433 vi->size = 1; 434 vi->fullsize = 2; 435 vi->is_full_var = true; 436 vi->is_reg_var = true; 437 438 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED", true); 439 vi2->offset = 1; 440 vi2->size = 1; 441 vi2->fullsize = 2; 442 vi2->is_full_var = true; 443 vi2->is_reg_var = true; 444 445 vi->next = vi2->id; 446 447 *slot_p = vi; 448 return vi; 449 } 450 451 /* Lookup the variable for the call statement CALL representing 452 the uses. Returns NULL if there is nothing special about this call. */ 453 454 static varinfo_t 455 lookup_call_use_vi (gcall *call) 456 { 457 varinfo_t *slot_p = call_stmt_vars->get (call); 458 if (slot_p) 459 return *slot_p; 460 461 return NULL; 462 } 463 464 /* Lookup the variable for the call statement CALL representing 465 the clobbers. Returns NULL if there is nothing special about this call. */ 466 467 static varinfo_t 468 lookup_call_clobber_vi (gcall *call) 469 { 470 varinfo_t uses = lookup_call_use_vi (call); 471 if (!uses) 472 return NULL; 473 474 return vi_next (uses); 475 } 476 477 /* Lookup or create the variable for the call statement CALL representing 478 the uses. */ 479 480 static varinfo_t 481 get_call_use_vi (gcall *call) 482 { 483 return get_call_vi (call); 484 } 485 486 /* Lookup or create the variable for the call statement CALL representing 487 the clobbers. */ 488 489 static varinfo_t ATTRIBUTE_UNUSED 490 get_call_clobber_vi (gcall *call) 491 { 492 return vi_next (get_call_vi (call)); 493 } 494 495 496 enum constraint_expr_type {SCALAR, DEREF, ADDRESSOF}; 497 498 /* An expression that appears in a constraint. */ 499 500 struct constraint_expr 501 { 502 /* Constraint type. */ 503 constraint_expr_type type; 504 505 /* Variable we are referring to in the constraint. */ 506 unsigned int var; 507 508 /* Offset, in bits, of this constraint from the beginning of 509 variables it ends up referring to. 510 511 IOW, in a deref constraint, we would deref, get the result set, 512 then add OFFSET to each member. */ 513 HOST_WIDE_INT offset; 514 }; 515 516 /* Use 0x8000... as special unknown offset. */ 517 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN 518 519 typedef struct constraint_expr ce_s; 520 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool); 521 static void get_constraint_for (tree, vec<ce_s> *); 522 static void get_constraint_for_rhs (tree, vec<ce_s> *); 523 static void do_deref (vec<ce_s> *); 524 525 /* Our set constraints are made up of two constraint expressions, one 526 LHS, and one RHS. 527 528 As described in the introduction, our set constraints each represent an 529 operation between set valued variables. 530 */ 531 struct constraint 532 { 533 struct constraint_expr lhs; 534 struct constraint_expr rhs; 535 }; 536 537 /* List of constraints that we use to build the constraint graph from. */ 538 539 static vec<constraint_t> constraints; 540 static object_allocator<constraint> constraint_pool ("Constraint pool"); 541 542 /* The constraint graph is represented as an array of bitmaps 543 containing successor nodes. */ 544 545 struct constraint_graph 546 { 547 /* Size of this graph, which may be different than the number of 548 nodes in the variable map. */ 549 unsigned int size; 550 551 /* Explicit successors of each node. */ 552 bitmap *succs; 553 554 /* Implicit predecessors of each node (Used for variable 555 substitution). */ 556 bitmap *implicit_preds; 557 558 /* Explicit predecessors of each node (Used for variable substitution). */ 559 bitmap *preds; 560 561 /* Indirect cycle representatives, or -1 if the node has no indirect 562 cycles. */ 563 int *indirect_cycles; 564 565 /* Representative node for a node. rep[a] == a unless the node has 566 been unified. */ 567 unsigned int *rep; 568 569 /* Equivalence class representative for a label. This is used for 570 variable substitution. */ 571 int *eq_rep; 572 573 /* Pointer equivalence label for a node. All nodes with the same 574 pointer equivalence label can be unified together at some point 575 (either during constraint optimization or after the constraint 576 graph is built). */ 577 unsigned int *pe; 578 579 /* Pointer equivalence representative for a label. This is used to 580 handle nodes that are pointer equivalent but not location 581 equivalent. We can unite these once the addressof constraints 582 are transformed into initial points-to sets. */ 583 int *pe_rep; 584 585 /* Pointer equivalence label for each node, used during variable 586 substitution. */ 587 unsigned int *pointer_label; 588 589 /* Location equivalence label for each node, used during location 590 equivalence finding. */ 591 unsigned int *loc_label; 592 593 /* Pointed-by set for each node, used during location equivalence 594 finding. This is pointed-by rather than pointed-to, because it 595 is constructed using the predecessor graph. */ 596 bitmap *pointed_by; 597 598 /* Points to sets for pointer equivalence. This is *not* the actual 599 points-to sets for nodes. */ 600 bitmap *points_to; 601 602 /* Bitmap of nodes where the bit is set if the node is a direct 603 node. Used for variable substitution. */ 604 sbitmap direct_nodes; 605 606 /* Bitmap of nodes where the bit is set if the node is address 607 taken. Used for variable substitution. */ 608 bitmap address_taken; 609 610 /* Vector of complex constraints for each graph node. Complex 611 constraints are those involving dereferences or offsets that are 612 not 0. */ 613 vec<constraint_t> *complex; 614 }; 615 616 static constraint_graph_t graph; 617 618 /* During variable substitution and the offline version of indirect 619 cycle finding, we create nodes to represent dereferences and 620 address taken constraints. These represent where these start and 621 end. */ 622 #define FIRST_REF_NODE (varmap).length () 623 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1)) 624 625 /* Return the representative node for NODE, if NODE has been unioned 626 with another NODE. 627 This function performs path compression along the way to finding 628 the representative. */ 629 630 static unsigned int 631 find (unsigned int node) 632 { 633 gcc_checking_assert (node < graph->size); 634 if (graph->rep[node] != node) 635 return graph->rep[node] = find (graph->rep[node]); 636 return node; 637 } 638 639 /* Union the TO and FROM nodes to the TO nodes. 640 Note that at some point in the future, we may want to do 641 union-by-rank, in which case we are going to have to return the 642 node we unified to. */ 643 644 static bool 645 unite (unsigned int to, unsigned int from) 646 { 647 gcc_checking_assert (to < graph->size && from < graph->size); 648 if (to != from && graph->rep[from] != to) 649 { 650 graph->rep[from] = to; 651 return true; 652 } 653 return false; 654 } 655 656 /* Create a new constraint consisting of LHS and RHS expressions. */ 657 658 static constraint_t 659 new_constraint (const struct constraint_expr lhs, 660 const struct constraint_expr rhs) 661 { 662 constraint_t ret = constraint_pool.allocate (); 663 ret->lhs = lhs; 664 ret->rhs = rhs; 665 return ret; 666 } 667 668 /* Print out constraint C to FILE. */ 669 670 static void 671 dump_constraint (FILE *file, constraint_t c) 672 { 673 if (c->lhs.type == ADDRESSOF) 674 fprintf (file, "&"); 675 else if (c->lhs.type == DEREF) 676 fprintf (file, "*"); 677 fprintf (file, "%s", get_varinfo (c->lhs.var)->name); 678 if (c->lhs.offset == UNKNOWN_OFFSET) 679 fprintf (file, " + UNKNOWN"); 680 else if (c->lhs.offset != 0) 681 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset); 682 fprintf (file, " = "); 683 if (c->rhs.type == ADDRESSOF) 684 fprintf (file, "&"); 685 else if (c->rhs.type == DEREF) 686 fprintf (file, "*"); 687 fprintf (file, "%s", get_varinfo (c->rhs.var)->name); 688 if (c->rhs.offset == UNKNOWN_OFFSET) 689 fprintf (file, " + UNKNOWN"); 690 else if (c->rhs.offset != 0) 691 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset); 692 } 693 694 695 void debug_constraint (constraint_t); 696 void debug_constraints (void); 697 void debug_constraint_graph (void); 698 void debug_solution_for_var (unsigned int); 699 void debug_sa_points_to_info (void); 700 void debug_varinfo (varinfo_t); 701 void debug_varmap (void); 702 703 /* Print out constraint C to stderr. */ 704 705 DEBUG_FUNCTION void 706 debug_constraint (constraint_t c) 707 { 708 dump_constraint (stderr, c); 709 fprintf (stderr, "\n"); 710 } 711 712 /* Print out all constraints to FILE */ 713 714 static void 715 dump_constraints (FILE *file, int from) 716 { 717 int i; 718 constraint_t c; 719 for (i = from; constraints.iterate (i, &c); i++) 720 if (c) 721 { 722 dump_constraint (file, c); 723 fprintf (file, "\n"); 724 } 725 } 726 727 /* Print out all constraints to stderr. */ 728 729 DEBUG_FUNCTION void 730 debug_constraints (void) 731 { 732 dump_constraints (stderr, 0); 733 } 734 735 /* Print the constraint graph in dot format. */ 736 737 static void 738 dump_constraint_graph (FILE *file) 739 { 740 unsigned int i; 741 742 /* Only print the graph if it has already been initialized: */ 743 if (!graph) 744 return; 745 746 /* Prints the header of the dot file: */ 747 fprintf (file, "strict digraph {\n"); 748 fprintf (file, " node [\n shape = box\n ]\n"); 749 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n"); 750 fprintf (file, "\n // List of nodes and complex constraints in " 751 "the constraint graph:\n"); 752 753 /* The next lines print the nodes in the graph together with the 754 complex constraints attached to them. */ 755 for (i = 1; i < graph->size; i++) 756 { 757 if (i == FIRST_REF_NODE) 758 continue; 759 if (find (i) != i) 760 continue; 761 if (i < FIRST_REF_NODE) 762 fprintf (file, "\"%s\"", get_varinfo (i)->name); 763 else 764 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name); 765 if (graph->complex[i].exists ()) 766 { 767 unsigned j; 768 constraint_t c; 769 fprintf (file, " [label=\"\\N\\n"); 770 for (j = 0; graph->complex[i].iterate (j, &c); ++j) 771 { 772 dump_constraint (file, c); 773 fprintf (file, "\\l"); 774 } 775 fprintf (file, "\"]"); 776 } 777 fprintf (file, ";\n"); 778 } 779 780 /* Go over the edges. */ 781 fprintf (file, "\n // Edges in the constraint graph:\n"); 782 for (i = 1; i < graph->size; i++) 783 { 784 unsigned j; 785 bitmap_iterator bi; 786 if (find (i) != i) 787 continue; 788 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi) 789 { 790 unsigned to = find (j); 791 if (i == to) 792 continue; 793 if (i < FIRST_REF_NODE) 794 fprintf (file, "\"%s\"", get_varinfo (i)->name); 795 else 796 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name); 797 fprintf (file, " -> "); 798 if (to < FIRST_REF_NODE) 799 fprintf (file, "\"%s\"", get_varinfo (to)->name); 800 else 801 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name); 802 fprintf (file, ";\n"); 803 } 804 } 805 806 /* Prints the tail of the dot file. */ 807 fprintf (file, "}\n"); 808 } 809 810 /* Print out the constraint graph to stderr. */ 811 812 DEBUG_FUNCTION void 813 debug_constraint_graph (void) 814 { 815 dump_constraint_graph (stderr); 816 } 817 818 /* SOLVER FUNCTIONS 819 820 The solver is a simple worklist solver, that works on the following 821 algorithm: 822 823 sbitmap changed_nodes = all zeroes; 824 changed_count = 0; 825 For each node that is not already collapsed: 826 changed_count++; 827 set bit in changed nodes 828 829 while (changed_count > 0) 830 { 831 compute topological ordering for constraint graph 832 833 find and collapse cycles in the constraint graph (updating 834 changed if necessary) 835 836 for each node (n) in the graph in topological order: 837 changed_count--; 838 839 Process each complex constraint associated with the node, 840 updating changed if necessary. 841 842 For each outgoing edge from n, propagate the solution from n to 843 the destination of the edge, updating changed as necessary. 844 845 } */ 846 847 /* Return true if two constraint expressions A and B are equal. */ 848 849 static bool 850 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b) 851 { 852 return a.type == b.type && a.var == b.var && a.offset == b.offset; 853 } 854 855 /* Return true if constraint expression A is less than constraint expression 856 B. This is just arbitrary, but consistent, in order to give them an 857 ordering. */ 858 859 static bool 860 constraint_expr_less (struct constraint_expr a, struct constraint_expr b) 861 { 862 if (a.type == b.type) 863 { 864 if (a.var == b.var) 865 return a.offset < b.offset; 866 else 867 return a.var < b.var; 868 } 869 else 870 return a.type < b.type; 871 } 872 873 /* Return true if constraint A is less than constraint B. This is just 874 arbitrary, but consistent, in order to give them an ordering. */ 875 876 static bool 877 constraint_less (const constraint_t &a, const constraint_t &b) 878 { 879 if (constraint_expr_less (a->lhs, b->lhs)) 880 return true; 881 else if (constraint_expr_less (b->lhs, a->lhs)) 882 return false; 883 else 884 return constraint_expr_less (a->rhs, b->rhs); 885 } 886 887 /* Return true if two constraints A and B are equal. */ 888 889 static bool 890 constraint_equal (struct constraint a, struct constraint b) 891 { 892 return constraint_expr_equal (a.lhs, b.lhs) 893 && constraint_expr_equal (a.rhs, b.rhs); 894 } 895 896 897 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */ 898 899 static constraint_t 900 constraint_vec_find (vec<constraint_t> vec, 901 struct constraint lookfor) 902 { 903 unsigned int place; 904 constraint_t found; 905 906 if (!vec.exists ()) 907 return NULL; 908 909 place = vec.lower_bound (&lookfor, constraint_less); 910 if (place >= vec.length ()) 911 return NULL; 912 found = vec[place]; 913 if (!constraint_equal (*found, lookfor)) 914 return NULL; 915 return found; 916 } 917 918 /* Union two constraint vectors, TO and FROM. Put the result in TO. 919 Returns true of TO set is changed. */ 920 921 static bool 922 constraint_set_union (vec<constraint_t> *to, 923 vec<constraint_t> *from) 924 { 925 int i; 926 constraint_t c; 927 bool any_change = false; 928 929 FOR_EACH_VEC_ELT (*from, i, c) 930 { 931 if (constraint_vec_find (*to, *c) == NULL) 932 { 933 unsigned int place = to->lower_bound (c, constraint_less); 934 to->safe_insert (place, c); 935 any_change = true; 936 } 937 } 938 return any_change; 939 } 940 941 /* Expands the solution in SET to all sub-fields of variables included. */ 942 943 static bitmap 944 solution_set_expand (bitmap set, bitmap *expanded) 945 { 946 bitmap_iterator bi; 947 unsigned j; 948 949 if (*expanded) 950 return *expanded; 951 952 *expanded = BITMAP_ALLOC (&iteration_obstack); 953 954 /* In a first pass expand to the head of the variables we need to 955 add all sub-fields off. This avoids quadratic behavior. */ 956 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi) 957 { 958 varinfo_t v = get_varinfo (j); 959 if (v->is_artificial_var 960 || v->is_full_var) 961 continue; 962 bitmap_set_bit (*expanded, v->head); 963 } 964 965 /* In the second pass now expand all head variables with subfields. */ 966 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi) 967 { 968 varinfo_t v = get_varinfo (j); 969 if (v->head != j) 970 continue; 971 for (v = vi_next (v); v != NULL; v = vi_next (v)) 972 bitmap_set_bit (*expanded, v->id); 973 } 974 975 /* And finally set the rest of the bits from SET. */ 976 bitmap_ior_into (*expanded, set); 977 978 return *expanded; 979 } 980 981 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the 982 process. */ 983 984 static bool 985 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc, 986 bitmap *expanded_delta) 987 { 988 bool changed = false; 989 bitmap_iterator bi; 990 unsigned int i; 991 992 /* If the solution of DELTA contains anything it is good enough to transfer 993 this to TO. */ 994 if (bitmap_bit_p (delta, anything_id)) 995 return bitmap_set_bit (to, anything_id); 996 997 /* If the offset is unknown we have to expand the solution to 998 all subfields. */ 999 if (inc == UNKNOWN_OFFSET) 1000 { 1001 delta = solution_set_expand (delta, expanded_delta); 1002 changed |= bitmap_ior_into (to, delta); 1003 return changed; 1004 } 1005 1006 /* For non-zero offset union the offsetted solution into the destination. */ 1007 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi) 1008 { 1009 varinfo_t vi = get_varinfo (i); 1010 1011 /* If this is a variable with just one field just set its bit 1012 in the result. */ 1013 if (vi->is_artificial_var 1014 || vi->is_unknown_size_var 1015 || vi->is_full_var) 1016 changed |= bitmap_set_bit (to, i); 1017 else 1018 { 1019 HOST_WIDE_INT fieldoffset = vi->offset + inc; 1020 unsigned HOST_WIDE_INT size = vi->size; 1021 1022 /* If the offset makes the pointer point to before the 1023 variable use offset zero for the field lookup. */ 1024 if (fieldoffset < 0) 1025 vi = get_varinfo (vi->head); 1026 else 1027 vi = first_or_preceding_vi_for_offset (vi, fieldoffset); 1028 1029 do 1030 { 1031 changed |= bitmap_set_bit (to, vi->id); 1032 if (vi->is_full_var 1033 || vi->next == 0) 1034 break; 1035 1036 /* We have to include all fields that overlap the current field 1037 shifted by inc. */ 1038 vi = vi_next (vi); 1039 } 1040 while (vi->offset < fieldoffset + size); 1041 } 1042 } 1043 1044 return changed; 1045 } 1046 1047 /* Insert constraint C into the list of complex constraints for graph 1048 node VAR. */ 1049 1050 static void 1051 insert_into_complex (constraint_graph_t graph, 1052 unsigned int var, constraint_t c) 1053 { 1054 vec<constraint_t> complex = graph->complex[var]; 1055 unsigned int place = complex.lower_bound (c, constraint_less); 1056 1057 /* Only insert constraints that do not already exist. */ 1058 if (place >= complex.length () 1059 || !constraint_equal (*c, *complex[place])) 1060 graph->complex[var].safe_insert (place, c); 1061 } 1062 1063 1064 /* Condense two variable nodes into a single variable node, by moving 1065 all associated info from FROM to TO. Returns true if TO node's 1066 constraint set changes after the merge. */ 1067 1068 static bool 1069 merge_node_constraints (constraint_graph_t graph, unsigned int to, 1070 unsigned int from) 1071 { 1072 unsigned int i; 1073 constraint_t c; 1074 bool any_change = false; 1075 1076 gcc_checking_assert (find (from) == to); 1077 1078 /* Move all complex constraints from src node into to node */ 1079 FOR_EACH_VEC_ELT (graph->complex[from], i, c) 1080 { 1081 /* In complex constraints for node FROM, we may have either 1082 a = *FROM, and *FROM = a, or an offseted constraint which are 1083 always added to the rhs node's constraints. */ 1084 1085 if (c->rhs.type == DEREF) 1086 c->rhs.var = to; 1087 else if (c->lhs.type == DEREF) 1088 c->lhs.var = to; 1089 else 1090 c->rhs.var = to; 1091 1092 } 1093 any_change = constraint_set_union (&graph->complex[to], 1094 &graph->complex[from]); 1095 graph->complex[from].release (); 1096 return any_change; 1097 } 1098 1099 1100 /* Remove edges involving NODE from GRAPH. */ 1101 1102 static void 1103 clear_edges_for_node (constraint_graph_t graph, unsigned int node) 1104 { 1105 if (graph->succs[node]) 1106 BITMAP_FREE (graph->succs[node]); 1107 } 1108 1109 /* Merge GRAPH nodes FROM and TO into node TO. */ 1110 1111 static void 1112 merge_graph_nodes (constraint_graph_t graph, unsigned int to, 1113 unsigned int from) 1114 { 1115 if (graph->indirect_cycles[from] != -1) 1116 { 1117 /* If we have indirect cycles with the from node, and we have 1118 none on the to node, the to node has indirect cycles from the 1119 from node now that they are unified. 1120 If indirect cycles exist on both, unify the nodes that they 1121 are in a cycle with, since we know they are in a cycle with 1122 each other. */ 1123 if (graph->indirect_cycles[to] == -1) 1124 graph->indirect_cycles[to] = graph->indirect_cycles[from]; 1125 } 1126 1127 /* Merge all the successor edges. */ 1128 if (graph->succs[from]) 1129 { 1130 if (!graph->succs[to]) 1131 graph->succs[to] = BITMAP_ALLOC (&pta_obstack); 1132 bitmap_ior_into (graph->succs[to], 1133 graph->succs[from]); 1134 } 1135 1136 clear_edges_for_node (graph, from); 1137 } 1138 1139 1140 /* Add an indirect graph edge to GRAPH, going from TO to FROM if 1141 it doesn't exist in the graph already. */ 1142 1143 static void 1144 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to, 1145 unsigned int from) 1146 { 1147 if (to == from) 1148 return; 1149 1150 if (!graph->implicit_preds[to]) 1151 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack); 1152 1153 if (bitmap_set_bit (graph->implicit_preds[to], from)) 1154 stats.num_implicit_edges++; 1155 } 1156 1157 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if 1158 it doesn't exist in the graph already. 1159 Return false if the edge already existed, true otherwise. */ 1160 1161 static void 1162 add_pred_graph_edge (constraint_graph_t graph, unsigned int to, 1163 unsigned int from) 1164 { 1165 if (!graph->preds[to]) 1166 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack); 1167 bitmap_set_bit (graph->preds[to], from); 1168 } 1169 1170 /* Add a graph edge to GRAPH, going from FROM to TO if 1171 it doesn't exist in the graph already. 1172 Return false if the edge already existed, true otherwise. */ 1173 1174 static bool 1175 add_graph_edge (constraint_graph_t graph, unsigned int to, 1176 unsigned int from) 1177 { 1178 if (to == from) 1179 { 1180 return false; 1181 } 1182 else 1183 { 1184 bool r = false; 1185 1186 if (!graph->succs[from]) 1187 graph->succs[from] = BITMAP_ALLOC (&pta_obstack); 1188 if (bitmap_set_bit (graph->succs[from], to)) 1189 { 1190 r = true; 1191 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE) 1192 stats.num_edges++; 1193 } 1194 return r; 1195 } 1196 } 1197 1198 1199 /* Initialize the constraint graph structure to contain SIZE nodes. */ 1200 1201 static void 1202 init_graph (unsigned int size) 1203 { 1204 unsigned int j; 1205 1206 graph = XCNEW (struct constraint_graph); 1207 graph->size = size; 1208 graph->succs = XCNEWVEC (bitmap, graph->size); 1209 graph->indirect_cycles = XNEWVEC (int, graph->size); 1210 graph->rep = XNEWVEC (unsigned int, graph->size); 1211 /* ??? Macros do not support template types with multiple arguments, 1212 so we use a typedef to work around it. */ 1213 typedef vec<constraint_t> vec_constraint_t_heap; 1214 graph->complex = XCNEWVEC (vec_constraint_t_heap, size); 1215 graph->pe = XCNEWVEC (unsigned int, graph->size); 1216 graph->pe_rep = XNEWVEC (int, graph->size); 1217 1218 for (j = 0; j < graph->size; j++) 1219 { 1220 graph->rep[j] = j; 1221 graph->pe_rep[j] = -1; 1222 graph->indirect_cycles[j] = -1; 1223 } 1224 } 1225 1226 /* Build the constraint graph, adding only predecessor edges right now. */ 1227 1228 static void 1229 build_pred_graph (void) 1230 { 1231 int i; 1232 constraint_t c; 1233 unsigned int j; 1234 1235 graph->implicit_preds = XCNEWVEC (bitmap, graph->size); 1236 graph->preds = XCNEWVEC (bitmap, graph->size); 1237 graph->pointer_label = XCNEWVEC (unsigned int, graph->size); 1238 graph->loc_label = XCNEWVEC (unsigned int, graph->size); 1239 graph->pointed_by = XCNEWVEC (bitmap, graph->size); 1240 graph->points_to = XCNEWVEC (bitmap, graph->size); 1241 graph->eq_rep = XNEWVEC (int, graph->size); 1242 graph->direct_nodes = sbitmap_alloc (graph->size); 1243 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack); 1244 bitmap_clear (graph->direct_nodes); 1245 1246 for (j = 1; j < FIRST_REF_NODE; j++) 1247 { 1248 if (!get_varinfo (j)->is_special_var) 1249 bitmap_set_bit (graph->direct_nodes, j); 1250 } 1251 1252 for (j = 0; j < graph->size; j++) 1253 graph->eq_rep[j] = -1; 1254 1255 for (j = 0; j < varmap.length (); j++) 1256 graph->indirect_cycles[j] = -1; 1257 1258 FOR_EACH_VEC_ELT (constraints, i, c) 1259 { 1260 struct constraint_expr lhs = c->lhs; 1261 struct constraint_expr rhs = c->rhs; 1262 unsigned int lhsvar = lhs.var; 1263 unsigned int rhsvar = rhs.var; 1264 1265 if (lhs.type == DEREF) 1266 { 1267 /* *x = y. */ 1268 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR) 1269 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar); 1270 } 1271 else if (rhs.type == DEREF) 1272 { 1273 /* x = *y */ 1274 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR) 1275 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar); 1276 else 1277 bitmap_clear_bit (graph->direct_nodes, lhsvar); 1278 } 1279 else if (rhs.type == ADDRESSOF) 1280 { 1281 varinfo_t v; 1282 1283 /* x = &y */ 1284 if (graph->points_to[lhsvar] == NULL) 1285 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack); 1286 bitmap_set_bit (graph->points_to[lhsvar], rhsvar); 1287 1288 if (graph->pointed_by[rhsvar] == NULL) 1289 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack); 1290 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar); 1291 1292 /* Implicitly, *x = y */ 1293 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar); 1294 1295 /* All related variables are no longer direct nodes. */ 1296 bitmap_clear_bit (graph->direct_nodes, rhsvar); 1297 v = get_varinfo (rhsvar); 1298 if (!v->is_full_var) 1299 { 1300 v = get_varinfo (v->head); 1301 do 1302 { 1303 bitmap_clear_bit (graph->direct_nodes, v->id); 1304 v = vi_next (v); 1305 } 1306 while (v != NULL); 1307 } 1308 bitmap_set_bit (graph->address_taken, rhsvar); 1309 } 1310 else if (lhsvar > anything_id 1311 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0) 1312 { 1313 /* x = y */ 1314 add_pred_graph_edge (graph, lhsvar, rhsvar); 1315 /* Implicitly, *x = *y */ 1316 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, 1317 FIRST_REF_NODE + rhsvar); 1318 } 1319 else if (lhs.offset != 0 || rhs.offset != 0) 1320 { 1321 if (rhs.offset != 0) 1322 bitmap_clear_bit (graph->direct_nodes, lhs.var); 1323 else if (lhs.offset != 0) 1324 bitmap_clear_bit (graph->direct_nodes, rhs.var); 1325 } 1326 } 1327 } 1328 1329 /* Build the constraint graph, adding successor edges. */ 1330 1331 static void 1332 build_succ_graph (void) 1333 { 1334 unsigned i, t; 1335 constraint_t c; 1336 1337 FOR_EACH_VEC_ELT (constraints, i, c) 1338 { 1339 struct constraint_expr lhs; 1340 struct constraint_expr rhs; 1341 unsigned int lhsvar; 1342 unsigned int rhsvar; 1343 1344 if (!c) 1345 continue; 1346 1347 lhs = c->lhs; 1348 rhs = c->rhs; 1349 lhsvar = find (lhs.var); 1350 rhsvar = find (rhs.var); 1351 1352 if (lhs.type == DEREF) 1353 { 1354 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR) 1355 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar); 1356 } 1357 else if (rhs.type == DEREF) 1358 { 1359 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR) 1360 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar); 1361 } 1362 else if (rhs.type == ADDRESSOF) 1363 { 1364 /* x = &y */ 1365 gcc_checking_assert (find (rhs.var) == rhs.var); 1366 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar); 1367 } 1368 else if (lhsvar > anything_id 1369 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0) 1370 { 1371 add_graph_edge (graph, lhsvar, rhsvar); 1372 } 1373 } 1374 1375 /* Add edges from STOREDANYTHING to all non-direct nodes that can 1376 receive pointers. */ 1377 t = find (storedanything_id); 1378 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i) 1379 { 1380 if (!bitmap_bit_p (graph->direct_nodes, i) 1381 && get_varinfo (i)->may_have_pointers) 1382 add_graph_edge (graph, find (i), t); 1383 } 1384 1385 /* Everything stored to ANYTHING also potentially escapes. */ 1386 add_graph_edge (graph, find (escaped_id), t); 1387 } 1388 1389 1390 /* Changed variables on the last iteration. */ 1391 static bitmap changed; 1392 1393 /* Strongly Connected Component visitation info. */ 1394 1395 struct scc_info 1396 { 1397 scc_info (size_t size); 1398 ~scc_info (); 1399 1400 auto_sbitmap visited; 1401 auto_sbitmap deleted; 1402 unsigned int *dfs; 1403 unsigned int *node_mapping; 1404 int current_index; 1405 auto_vec<unsigned> scc_stack; 1406 }; 1407 1408 1409 /* Recursive routine to find strongly connected components in GRAPH. 1410 SI is the SCC info to store the information in, and N is the id of current 1411 graph node we are processing. 1412 1413 This is Tarjan's strongly connected component finding algorithm, as 1414 modified by Nuutila to keep only non-root nodes on the stack. 1415 The algorithm can be found in "On finding the strongly connected 1416 connected components in a directed graph" by Esko Nuutila and Eljas 1417 Soisalon-Soininen, in Information Processing Letters volume 49, 1418 number 1, pages 9-14. */ 1419 1420 static void 1421 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) 1422 { 1423 unsigned int i; 1424 bitmap_iterator bi; 1425 unsigned int my_dfs; 1426 1427 bitmap_set_bit (si->visited, n); 1428 si->dfs[n] = si->current_index ++; 1429 my_dfs = si->dfs[n]; 1430 1431 /* Visit all the successors. */ 1432 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi) 1433 { 1434 unsigned int w; 1435 1436 if (i > LAST_REF_NODE) 1437 break; 1438 1439 w = find (i); 1440 if (bitmap_bit_p (si->deleted, w)) 1441 continue; 1442 1443 if (!bitmap_bit_p (si->visited, w)) 1444 scc_visit (graph, si, w); 1445 1446 unsigned int t = find (w); 1447 gcc_checking_assert (find (n) == n); 1448 if (si->dfs[t] < si->dfs[n]) 1449 si->dfs[n] = si->dfs[t]; 1450 } 1451 1452 /* See if any components have been identified. */ 1453 if (si->dfs[n] == my_dfs) 1454 { 1455 if (si->scc_stack.length () > 0 1456 && si->dfs[si->scc_stack.last ()] >= my_dfs) 1457 { 1458 bitmap scc = BITMAP_ALLOC (NULL); 1459 unsigned int lowest_node; 1460 bitmap_iterator bi; 1461 1462 bitmap_set_bit (scc, n); 1463 1464 while (si->scc_stack.length () != 0 1465 && si->dfs[si->scc_stack.last ()] >= my_dfs) 1466 { 1467 unsigned int w = si->scc_stack.pop (); 1468 1469 bitmap_set_bit (scc, w); 1470 } 1471 1472 lowest_node = bitmap_first_set_bit (scc); 1473 gcc_assert (lowest_node < FIRST_REF_NODE); 1474 1475 /* Collapse the SCC nodes into a single node, and mark the 1476 indirect cycles. */ 1477 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi) 1478 { 1479 if (i < FIRST_REF_NODE) 1480 { 1481 if (unite (lowest_node, i)) 1482 unify_nodes (graph, lowest_node, i, false); 1483 } 1484 else 1485 { 1486 unite (lowest_node, i); 1487 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node; 1488 } 1489 } 1490 } 1491 bitmap_set_bit (si->deleted, n); 1492 } 1493 else 1494 si->scc_stack.safe_push (n); 1495 } 1496 1497 /* Unify node FROM into node TO, updating the changed count if 1498 necessary when UPDATE_CHANGED is true. */ 1499 1500 static void 1501 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from, 1502 bool update_changed) 1503 { 1504 gcc_checking_assert (to != from && find (to) == to); 1505 1506 if (dump_file && (dump_flags & TDF_DETAILS)) 1507 fprintf (dump_file, "Unifying %s to %s\n", 1508 get_varinfo (from)->name, 1509 get_varinfo (to)->name); 1510 1511 if (update_changed) 1512 stats.unified_vars_dynamic++; 1513 else 1514 stats.unified_vars_static++; 1515 1516 merge_graph_nodes (graph, to, from); 1517 if (merge_node_constraints (graph, to, from)) 1518 { 1519 if (update_changed) 1520 bitmap_set_bit (changed, to); 1521 } 1522 1523 /* Mark TO as changed if FROM was changed. If TO was already marked 1524 as changed, decrease the changed count. */ 1525 1526 if (update_changed 1527 && bitmap_clear_bit (changed, from)) 1528 bitmap_set_bit (changed, to); 1529 varinfo_t fromvi = get_varinfo (from); 1530 if (fromvi->solution) 1531 { 1532 /* If the solution changes because of the merging, we need to mark 1533 the variable as changed. */ 1534 varinfo_t tovi = get_varinfo (to); 1535 if (bitmap_ior_into (tovi->solution, fromvi->solution)) 1536 { 1537 if (update_changed) 1538 bitmap_set_bit (changed, to); 1539 } 1540 1541 BITMAP_FREE (fromvi->solution); 1542 if (fromvi->oldsolution) 1543 BITMAP_FREE (fromvi->oldsolution); 1544 1545 if (stats.iterations > 0 1546 && tovi->oldsolution) 1547 BITMAP_FREE (tovi->oldsolution); 1548 } 1549 if (graph->succs[to]) 1550 bitmap_clear_bit (graph->succs[to], to); 1551 } 1552 1553 /* Information needed to compute the topological ordering of a graph. */ 1554 1555 struct topo_info 1556 { 1557 /* sbitmap of visited nodes. */ 1558 sbitmap visited; 1559 /* Array that stores the topological order of the graph, *in 1560 reverse*. */ 1561 vec<unsigned> topo_order; 1562 }; 1563 1564 1565 /* Initialize and return a topological info structure. */ 1566 1567 static struct topo_info * 1568 init_topo_info (void) 1569 { 1570 size_t size = graph->size; 1571 struct topo_info *ti = XNEW (struct topo_info); 1572 ti->visited = sbitmap_alloc (size); 1573 bitmap_clear (ti->visited); 1574 ti->topo_order.create (1); 1575 return ti; 1576 } 1577 1578 1579 /* Free the topological sort info pointed to by TI. */ 1580 1581 static void 1582 free_topo_info (struct topo_info *ti) 1583 { 1584 sbitmap_free (ti->visited); 1585 ti->topo_order.release (); 1586 free (ti); 1587 } 1588 1589 /* Visit the graph in topological order, and store the order in the 1590 topo_info structure. */ 1591 1592 static void 1593 topo_visit (constraint_graph_t graph, struct topo_info *ti, 1594 unsigned int n) 1595 { 1596 bitmap_iterator bi; 1597 unsigned int j; 1598 1599 bitmap_set_bit (ti->visited, n); 1600 1601 if (graph->succs[n]) 1602 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi) 1603 { 1604 if (!bitmap_bit_p (ti->visited, j)) 1605 topo_visit (graph, ti, j); 1606 } 1607 1608 ti->topo_order.safe_push (n); 1609 } 1610 1611 /* Process a constraint C that represents x = *(y + off), using DELTA as the 1612 starting solution for y. */ 1613 1614 static void 1615 do_sd_constraint (constraint_graph_t graph, constraint_t c, 1616 bitmap delta, bitmap *expanded_delta) 1617 { 1618 unsigned int lhs = c->lhs.var; 1619 bool flag = false; 1620 bitmap sol = get_varinfo (lhs)->solution; 1621 unsigned int j; 1622 bitmap_iterator bi; 1623 HOST_WIDE_INT roffset = c->rhs.offset; 1624 1625 /* Our IL does not allow this. */ 1626 gcc_checking_assert (c->lhs.offset == 0); 1627 1628 /* If the solution of Y contains anything it is good enough to transfer 1629 this to the LHS. */ 1630 if (bitmap_bit_p (delta, anything_id)) 1631 { 1632 flag |= bitmap_set_bit (sol, anything_id); 1633 goto done; 1634 } 1635 1636 /* If we do not know at with offset the rhs is dereferenced compute 1637 the reachability set of DELTA, conservatively assuming it is 1638 dereferenced at all valid offsets. */ 1639 if (roffset == UNKNOWN_OFFSET) 1640 { 1641 delta = solution_set_expand (delta, expanded_delta); 1642 /* No further offset processing is necessary. */ 1643 roffset = 0; 1644 } 1645 1646 /* For each variable j in delta (Sol(y)), add 1647 an edge in the graph from j to x, and union Sol(j) into Sol(x). */ 1648 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi) 1649 { 1650 varinfo_t v = get_varinfo (j); 1651 HOST_WIDE_INT fieldoffset = v->offset + roffset; 1652 unsigned HOST_WIDE_INT size = v->size; 1653 unsigned int t; 1654 1655 if (v->is_full_var) 1656 ; 1657 else if (roffset != 0) 1658 { 1659 if (fieldoffset < 0) 1660 v = get_varinfo (v->head); 1661 else 1662 v = first_or_preceding_vi_for_offset (v, fieldoffset); 1663 } 1664 1665 /* We have to include all fields that overlap the current field 1666 shifted by roffset. */ 1667 do 1668 { 1669 t = find (v->id); 1670 1671 /* Adding edges from the special vars is pointless. 1672 They don't have sets that can change. */ 1673 if (get_varinfo (t)->is_special_var) 1674 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution); 1675 /* Merging the solution from ESCAPED needlessly increases 1676 the set. Use ESCAPED as representative instead. */ 1677 else if (v->id == escaped_id) 1678 flag |= bitmap_set_bit (sol, escaped_id); 1679 else if (v->may_have_pointers 1680 && add_graph_edge (graph, lhs, t)) 1681 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution); 1682 1683 if (v->is_full_var 1684 || v->next == 0) 1685 break; 1686 1687 v = vi_next (v); 1688 } 1689 while (v->offset < fieldoffset + size); 1690 } 1691 1692 done: 1693 /* If the LHS solution changed, mark the var as changed. */ 1694 if (flag) 1695 { 1696 get_varinfo (lhs)->solution = sol; 1697 bitmap_set_bit (changed, lhs); 1698 } 1699 } 1700 1701 /* Process a constraint C that represents *(x + off) = y using DELTA 1702 as the starting solution for x. */ 1703 1704 static void 1705 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta) 1706 { 1707 unsigned int rhs = c->rhs.var; 1708 bitmap sol = get_varinfo (rhs)->solution; 1709 unsigned int j; 1710 bitmap_iterator bi; 1711 HOST_WIDE_INT loff = c->lhs.offset; 1712 bool escaped_p = false; 1713 1714 /* Our IL does not allow this. */ 1715 gcc_checking_assert (c->rhs.offset == 0); 1716 1717 /* If the solution of y contains ANYTHING simply use the ANYTHING 1718 solution. This avoids needlessly increasing the points-to sets. */ 1719 if (bitmap_bit_p (sol, anything_id)) 1720 sol = get_varinfo (find (anything_id))->solution; 1721 1722 /* If the solution for x contains ANYTHING we have to merge the 1723 solution of y into all pointer variables which we do via 1724 STOREDANYTHING. */ 1725 if (bitmap_bit_p (delta, anything_id)) 1726 { 1727 unsigned t = find (storedanything_id); 1728 if (add_graph_edge (graph, t, rhs)) 1729 { 1730 if (bitmap_ior_into (get_varinfo (t)->solution, sol)) 1731 bitmap_set_bit (changed, t); 1732 } 1733 return; 1734 } 1735 1736 /* If we do not know at with offset the rhs is dereferenced compute 1737 the reachability set of DELTA, conservatively assuming it is 1738 dereferenced at all valid offsets. */ 1739 if (loff == UNKNOWN_OFFSET) 1740 { 1741 delta = solution_set_expand (delta, expanded_delta); 1742 loff = 0; 1743 } 1744 1745 /* For each member j of delta (Sol(x)), add an edge from y to j and 1746 union Sol(y) into Sol(j) */ 1747 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi) 1748 { 1749 varinfo_t v = get_varinfo (j); 1750 unsigned int t; 1751 HOST_WIDE_INT fieldoffset = v->offset + loff; 1752 unsigned HOST_WIDE_INT size = v->size; 1753 1754 if (v->is_full_var) 1755 ; 1756 else if (loff != 0) 1757 { 1758 if (fieldoffset < 0) 1759 v = get_varinfo (v->head); 1760 else 1761 v = first_or_preceding_vi_for_offset (v, fieldoffset); 1762 } 1763 1764 /* We have to include all fields that overlap the current field 1765 shifted by loff. */ 1766 do 1767 { 1768 if (v->may_have_pointers) 1769 { 1770 /* If v is a global variable then this is an escape point. */ 1771 if (v->is_global_var 1772 && !escaped_p) 1773 { 1774 t = find (escaped_id); 1775 if (add_graph_edge (graph, t, rhs) 1776 && bitmap_ior_into (get_varinfo (t)->solution, sol)) 1777 bitmap_set_bit (changed, t); 1778 /* Enough to let rhs escape once. */ 1779 escaped_p = true; 1780 } 1781 1782 if (v->is_special_var) 1783 break; 1784 1785 t = find (v->id); 1786 if (add_graph_edge (graph, t, rhs) 1787 && bitmap_ior_into (get_varinfo (t)->solution, sol)) 1788 bitmap_set_bit (changed, t); 1789 } 1790 1791 if (v->is_full_var 1792 || v->next == 0) 1793 break; 1794 1795 v = vi_next (v); 1796 } 1797 while (v->offset < fieldoffset + size); 1798 } 1799 } 1800 1801 /* Handle a non-simple (simple meaning requires no iteration), 1802 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */ 1803 1804 static void 1805 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta, 1806 bitmap *expanded_delta) 1807 { 1808 if (c->lhs.type == DEREF) 1809 { 1810 if (c->rhs.type == ADDRESSOF) 1811 { 1812 gcc_unreachable (); 1813 } 1814 else 1815 { 1816 /* *x = y */ 1817 do_ds_constraint (c, delta, expanded_delta); 1818 } 1819 } 1820 else if (c->rhs.type == DEREF) 1821 { 1822 /* x = *y */ 1823 if (!(get_varinfo (c->lhs.var)->is_special_var)) 1824 do_sd_constraint (graph, c, delta, expanded_delta); 1825 } 1826 else 1827 { 1828 bitmap tmp; 1829 bool flag = false; 1830 1831 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR 1832 && c->rhs.offset != 0 && c->lhs.offset == 0); 1833 tmp = get_varinfo (c->lhs.var)->solution; 1834 1835 flag = set_union_with_increment (tmp, delta, c->rhs.offset, 1836 expanded_delta); 1837 1838 if (flag) 1839 bitmap_set_bit (changed, c->lhs.var); 1840 } 1841 } 1842 1843 /* Initialize and return a new SCC info structure. */ 1844 1845 scc_info::scc_info (size_t size) : 1846 visited (size), deleted (size), current_index (0), scc_stack (1) 1847 { 1848 bitmap_clear (visited); 1849 bitmap_clear (deleted); 1850 node_mapping = XNEWVEC (unsigned int, size); 1851 dfs = XCNEWVEC (unsigned int, size); 1852 1853 for (size_t i = 0; i < size; i++) 1854 node_mapping[i] = i; 1855 } 1856 1857 /* Free an SCC info structure pointed to by SI */ 1858 1859 scc_info::~scc_info () 1860 { 1861 free (node_mapping); 1862 free (dfs); 1863 } 1864 1865 1866 /* Find indirect cycles in GRAPH that occur, using strongly connected 1867 components, and note them in the indirect cycles map. 1868 1869 This technique comes from Ben Hardekopf and Calvin Lin, 1870 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of 1871 Lines of Code", submitted to PLDI 2007. */ 1872 1873 static void 1874 find_indirect_cycles (constraint_graph_t graph) 1875 { 1876 unsigned int i; 1877 unsigned int size = graph->size; 1878 scc_info si (size); 1879 1880 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ ) 1881 if (!bitmap_bit_p (si.visited, i) && find (i) == i) 1882 scc_visit (graph, &si, i); 1883 } 1884 1885 /* Compute a topological ordering for GRAPH, and store the result in the 1886 topo_info structure TI. */ 1887 1888 static void 1889 compute_topo_order (constraint_graph_t graph, 1890 struct topo_info *ti) 1891 { 1892 unsigned int i; 1893 unsigned int size = graph->size; 1894 1895 for (i = 0; i != size; ++i) 1896 if (!bitmap_bit_p (ti->visited, i) && find (i) == i) 1897 topo_visit (graph, ti, i); 1898 } 1899 1900 /* Structure used to for hash value numbering of pointer equivalence 1901 classes. */ 1902 1903 typedef struct equiv_class_label 1904 { 1905 hashval_t hashcode; 1906 unsigned int equivalence_class; 1907 bitmap labels; 1908 } *equiv_class_label_t; 1909 typedef const struct equiv_class_label *const_equiv_class_label_t; 1910 1911 /* Equiv_class_label hashtable helpers. */ 1912 1913 struct equiv_class_hasher : free_ptr_hash <equiv_class_label> 1914 { 1915 static inline hashval_t hash (const equiv_class_label *); 1916 static inline bool equal (const equiv_class_label *, 1917 const equiv_class_label *); 1918 }; 1919 1920 /* Hash function for a equiv_class_label_t */ 1921 1922 inline hashval_t 1923 equiv_class_hasher::hash (const equiv_class_label *ecl) 1924 { 1925 return ecl->hashcode; 1926 } 1927 1928 /* Equality function for two equiv_class_label_t's. */ 1929 1930 inline bool 1931 equiv_class_hasher::equal (const equiv_class_label *eql1, 1932 const equiv_class_label *eql2) 1933 { 1934 return (eql1->hashcode == eql2->hashcode 1935 && bitmap_equal_p (eql1->labels, eql2->labels)); 1936 } 1937 1938 /* A hashtable for mapping a bitmap of labels->pointer equivalence 1939 classes. */ 1940 static hash_table<equiv_class_hasher> *pointer_equiv_class_table; 1941 1942 /* A hashtable for mapping a bitmap of labels->location equivalence 1943 classes. */ 1944 static hash_table<equiv_class_hasher> *location_equiv_class_table; 1945 1946 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with 1947 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS 1948 is equivalent to. */ 1949 1950 static equiv_class_label * 1951 equiv_class_lookup_or_add (hash_table<equiv_class_hasher> *table, 1952 bitmap labels) 1953 { 1954 equiv_class_label **slot; 1955 equiv_class_label ecl; 1956 1957 ecl.labels = labels; 1958 ecl.hashcode = bitmap_hash (labels); 1959 slot = table->find_slot (&ecl, INSERT); 1960 if (!*slot) 1961 { 1962 *slot = XNEW (struct equiv_class_label); 1963 (*slot)->labels = labels; 1964 (*slot)->hashcode = ecl.hashcode; 1965 (*slot)->equivalence_class = 0; 1966 } 1967 1968 return *slot; 1969 } 1970 1971 /* Perform offline variable substitution. 1972 1973 This is a worst case quadratic time way of identifying variables 1974 that must have equivalent points-to sets, including those caused by 1975 static cycles, and single entry subgraphs, in the constraint graph. 1976 1977 The technique is described in "Exploiting Pointer and Location 1978 Equivalence to Optimize Pointer Analysis. In the 14th International 1979 Static Analysis Symposium (SAS), August 2007." It is known as the 1980 "HU" algorithm, and is equivalent to value numbering the collapsed 1981 constraint graph including evaluating unions. 1982 1983 The general method of finding equivalence classes is as follows: 1984 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints. 1985 Initialize all non-REF nodes to be direct nodes. 1986 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh 1987 variable} 1988 For each constraint containing the dereference, we also do the same 1989 thing. 1990 1991 We then compute SCC's in the graph and unify nodes in the same SCC, 1992 including pts sets. 1993 1994 For each non-collapsed node x: 1995 Visit all unvisited explicit incoming edges. 1996 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y 1997 where y->x. 1998 Lookup the equivalence class for pts(x). 1999 If we found one, equivalence_class(x) = found class. 2000 Otherwise, equivalence_class(x) = new class, and new_class is 2001 added to the lookup table. 2002 2003 All direct nodes with the same equivalence class can be replaced 2004 with a single representative node. 2005 All unlabeled nodes (label == 0) are not pointers and all edges 2006 involving them can be eliminated. 2007 We perform these optimizations during rewrite_constraints 2008 2009 In addition to pointer equivalence class finding, we also perform 2010 location equivalence class finding. This is the set of variables 2011 that always appear together in points-to sets. We use this to 2012 compress the size of the points-to sets. */ 2013 2014 /* Current maximum pointer equivalence class id. */ 2015 static int pointer_equiv_class; 2016 2017 /* Current maximum location equivalence class id. */ 2018 static int location_equiv_class; 2019 2020 /* Recursive routine to find strongly connected components in GRAPH, 2021 and label it's nodes with DFS numbers. */ 2022 2023 static void 2024 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) 2025 { 2026 unsigned int i; 2027 bitmap_iterator bi; 2028 unsigned int my_dfs; 2029 2030 gcc_checking_assert (si->node_mapping[n] == n); 2031 bitmap_set_bit (si->visited, n); 2032 si->dfs[n] = si->current_index ++; 2033 my_dfs = si->dfs[n]; 2034 2035 /* Visit all the successors. */ 2036 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi) 2037 { 2038 unsigned int w = si->node_mapping[i]; 2039 2040 if (bitmap_bit_p (si->deleted, w)) 2041 continue; 2042 2043 if (!bitmap_bit_p (si->visited, w)) 2044 condense_visit (graph, si, w); 2045 2046 unsigned int t = si->node_mapping[w]; 2047 gcc_checking_assert (si->node_mapping[n] == n); 2048 if (si->dfs[t] < si->dfs[n]) 2049 si->dfs[n] = si->dfs[t]; 2050 } 2051 2052 /* Visit all the implicit predecessors. */ 2053 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi) 2054 { 2055 unsigned int w = si->node_mapping[i]; 2056 2057 if (bitmap_bit_p (si->deleted, w)) 2058 continue; 2059 2060 if (!bitmap_bit_p (si->visited, w)) 2061 condense_visit (graph, si, w); 2062 2063 unsigned int t = si->node_mapping[w]; 2064 gcc_assert (si->node_mapping[n] == n); 2065 if (si->dfs[t] < si->dfs[n]) 2066 si->dfs[n] = si->dfs[t]; 2067 } 2068 2069 /* See if any components have been identified. */ 2070 if (si->dfs[n] == my_dfs) 2071 { 2072 while (si->scc_stack.length () != 0 2073 && si->dfs[si->scc_stack.last ()] >= my_dfs) 2074 { 2075 unsigned int w = si->scc_stack.pop (); 2076 si->node_mapping[w] = n; 2077 2078 if (!bitmap_bit_p (graph->direct_nodes, w)) 2079 bitmap_clear_bit (graph->direct_nodes, n); 2080 2081 /* Unify our nodes. */ 2082 if (graph->preds[w]) 2083 { 2084 if (!graph->preds[n]) 2085 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack); 2086 bitmap_ior_into (graph->preds[n], graph->preds[w]); 2087 } 2088 if (graph->implicit_preds[w]) 2089 { 2090 if (!graph->implicit_preds[n]) 2091 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack); 2092 bitmap_ior_into (graph->implicit_preds[n], 2093 graph->implicit_preds[w]); 2094 } 2095 if (graph->points_to[w]) 2096 { 2097 if (!graph->points_to[n]) 2098 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); 2099 bitmap_ior_into (graph->points_to[n], 2100 graph->points_to[w]); 2101 } 2102 } 2103 bitmap_set_bit (si->deleted, n); 2104 } 2105 else 2106 si->scc_stack.safe_push (n); 2107 } 2108 2109 /* Label pointer equivalences. 2110 2111 This performs a value numbering of the constraint graph to 2112 discover which variables will always have the same points-to sets 2113 under the current set of constraints. 2114 2115 The way it value numbers is to store the set of points-to bits 2116 generated by the constraints and graph edges. This is just used as a 2117 hash and equality comparison. The *actual set of points-to bits* is 2118 completely irrelevant, in that we don't care about being able to 2119 extract them later. 2120 2121 The equality values (currently bitmaps) just have to satisfy a few 2122 constraints, the main ones being: 2123 1. The combining operation must be order independent. 2124 2. The end result of a given set of operations must be unique iff the 2125 combination of input values is unique 2126 3. Hashable. */ 2127 2128 static void 2129 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) 2130 { 2131 unsigned int i, first_pred; 2132 bitmap_iterator bi; 2133 2134 bitmap_set_bit (si->visited, n); 2135 2136 /* Label and union our incoming edges's points to sets. */ 2137 first_pred = -1U; 2138 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi) 2139 { 2140 unsigned int w = si->node_mapping[i]; 2141 if (!bitmap_bit_p (si->visited, w)) 2142 label_visit (graph, si, w); 2143 2144 /* Skip unused edges */ 2145 if (w == n || graph->pointer_label[w] == 0) 2146 continue; 2147 2148 if (graph->points_to[w]) 2149 { 2150 if (!graph->points_to[n]) 2151 { 2152 if (first_pred == -1U) 2153 first_pred = w; 2154 else 2155 { 2156 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); 2157 bitmap_ior (graph->points_to[n], 2158 graph->points_to[first_pred], 2159 graph->points_to[w]); 2160 } 2161 } 2162 else 2163 bitmap_ior_into (graph->points_to[n], graph->points_to[w]); 2164 } 2165 } 2166 2167 /* Indirect nodes get fresh variables and a new pointer equiv class. */ 2168 if (!bitmap_bit_p (graph->direct_nodes, n)) 2169 { 2170 if (!graph->points_to[n]) 2171 { 2172 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); 2173 if (first_pred != -1U) 2174 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]); 2175 } 2176 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n); 2177 graph->pointer_label[n] = pointer_equiv_class++; 2178 equiv_class_label_t ecl; 2179 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table, 2180 graph->points_to[n]); 2181 ecl->equivalence_class = graph->pointer_label[n]; 2182 return; 2183 } 2184 2185 /* If there was only a single non-empty predecessor the pointer equiv 2186 class is the same. */ 2187 if (!graph->points_to[n]) 2188 { 2189 if (first_pred != -1U) 2190 { 2191 graph->pointer_label[n] = graph->pointer_label[first_pred]; 2192 graph->points_to[n] = graph->points_to[first_pred]; 2193 } 2194 return; 2195 } 2196 2197 if (!bitmap_empty_p (graph->points_to[n])) 2198 { 2199 equiv_class_label_t ecl; 2200 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table, 2201 graph->points_to[n]); 2202 if (ecl->equivalence_class == 0) 2203 ecl->equivalence_class = pointer_equiv_class++; 2204 else 2205 { 2206 BITMAP_FREE (graph->points_to[n]); 2207 graph->points_to[n] = ecl->labels; 2208 } 2209 graph->pointer_label[n] = ecl->equivalence_class; 2210 } 2211 } 2212 2213 /* Print the pred graph in dot format. */ 2214 2215 static void 2216 dump_pred_graph (struct scc_info *si, FILE *file) 2217 { 2218 unsigned int i; 2219 2220 /* Only print the graph if it has already been initialized: */ 2221 if (!graph) 2222 return; 2223 2224 /* Prints the header of the dot file: */ 2225 fprintf (file, "strict digraph {\n"); 2226 fprintf (file, " node [\n shape = box\n ]\n"); 2227 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n"); 2228 fprintf (file, "\n // List of nodes and complex constraints in " 2229 "the constraint graph:\n"); 2230 2231 /* The next lines print the nodes in the graph together with the 2232 complex constraints attached to them. */ 2233 for (i = 1; i < graph->size; i++) 2234 { 2235 if (i == FIRST_REF_NODE) 2236 continue; 2237 if (si->node_mapping[i] != i) 2238 continue; 2239 if (i < FIRST_REF_NODE) 2240 fprintf (file, "\"%s\"", get_varinfo (i)->name); 2241 else 2242 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name); 2243 if (graph->points_to[i] 2244 && !bitmap_empty_p (graph->points_to[i])) 2245 { 2246 if (i < FIRST_REF_NODE) 2247 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name); 2248 else 2249 fprintf (file, "[label=\"*%s = {", 2250 get_varinfo (i - FIRST_REF_NODE)->name); 2251 unsigned j; 2252 bitmap_iterator bi; 2253 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi) 2254 fprintf (file, " %d", j); 2255 fprintf (file, " }\"]"); 2256 } 2257 fprintf (file, ";\n"); 2258 } 2259 2260 /* Go over the edges. */ 2261 fprintf (file, "\n // Edges in the constraint graph:\n"); 2262 for (i = 1; i < graph->size; i++) 2263 { 2264 unsigned j; 2265 bitmap_iterator bi; 2266 if (si->node_mapping[i] != i) 2267 continue; 2268 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi) 2269 { 2270 unsigned from = si->node_mapping[j]; 2271 if (from < FIRST_REF_NODE) 2272 fprintf (file, "\"%s\"", get_varinfo (from)->name); 2273 else 2274 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name); 2275 fprintf (file, " -> "); 2276 if (i < FIRST_REF_NODE) 2277 fprintf (file, "\"%s\"", get_varinfo (i)->name); 2278 else 2279 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name); 2280 fprintf (file, ";\n"); 2281 } 2282 } 2283 2284 /* Prints the tail of the dot file. */ 2285 fprintf (file, "}\n"); 2286 } 2287 2288 /* Perform offline variable substitution, discovering equivalence 2289 classes, and eliminating non-pointer variables. */ 2290 2291 static struct scc_info * 2292 perform_var_substitution (constraint_graph_t graph) 2293 { 2294 unsigned int i; 2295 unsigned int size = graph->size; 2296 scc_info *si = new scc_info (size); 2297 2298 bitmap_obstack_initialize (&iteration_obstack); 2299 pointer_equiv_class_table = new hash_table<equiv_class_hasher> (511); 2300 location_equiv_class_table 2301 = new hash_table<equiv_class_hasher> (511); 2302 pointer_equiv_class = 1; 2303 location_equiv_class = 1; 2304 2305 /* Condense the nodes, which means to find SCC's, count incoming 2306 predecessors, and unite nodes in SCC's. */ 2307 for (i = 1; i < FIRST_REF_NODE; i++) 2308 if (!bitmap_bit_p (si->visited, si->node_mapping[i])) 2309 condense_visit (graph, si, si->node_mapping[i]); 2310 2311 if (dump_file && (dump_flags & TDF_GRAPH)) 2312 { 2313 fprintf (dump_file, "\n\n// The constraint graph before var-substitution " 2314 "in dot format:\n"); 2315 dump_pred_graph (si, dump_file); 2316 fprintf (dump_file, "\n\n"); 2317 } 2318 2319 bitmap_clear (si->visited); 2320 /* Actually the label the nodes for pointer equivalences */ 2321 for (i = 1; i < FIRST_REF_NODE; i++) 2322 if (!bitmap_bit_p (si->visited, si->node_mapping[i])) 2323 label_visit (graph, si, si->node_mapping[i]); 2324 2325 /* Calculate location equivalence labels. */ 2326 for (i = 1; i < FIRST_REF_NODE; i++) 2327 { 2328 bitmap pointed_by; 2329 bitmap_iterator bi; 2330 unsigned int j; 2331 2332 if (!graph->pointed_by[i]) 2333 continue; 2334 pointed_by = BITMAP_ALLOC (&iteration_obstack); 2335 2336 /* Translate the pointed-by mapping for pointer equivalence 2337 labels. */ 2338 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi) 2339 { 2340 bitmap_set_bit (pointed_by, 2341 graph->pointer_label[si->node_mapping[j]]); 2342 } 2343 /* The original pointed_by is now dead. */ 2344 BITMAP_FREE (graph->pointed_by[i]); 2345 2346 /* Look up the location equivalence label if one exists, or make 2347 one otherwise. */ 2348 equiv_class_label_t ecl; 2349 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by); 2350 if (ecl->equivalence_class == 0) 2351 ecl->equivalence_class = location_equiv_class++; 2352 else 2353 { 2354 if (dump_file && (dump_flags & TDF_DETAILS)) 2355 fprintf (dump_file, "Found location equivalence for node %s\n", 2356 get_varinfo (i)->name); 2357 BITMAP_FREE (pointed_by); 2358 } 2359 graph->loc_label[i] = ecl->equivalence_class; 2360 2361 } 2362 2363 if (dump_file && (dump_flags & TDF_DETAILS)) 2364 for (i = 1; i < FIRST_REF_NODE; i++) 2365 { 2366 unsigned j = si->node_mapping[i]; 2367 if (j != i) 2368 { 2369 fprintf (dump_file, "%s node id %d ", 2370 bitmap_bit_p (graph->direct_nodes, i) 2371 ? "Direct" : "Indirect", i); 2372 if (i < FIRST_REF_NODE) 2373 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name); 2374 else 2375 fprintf (dump_file, "\"*%s\"", 2376 get_varinfo (i - FIRST_REF_NODE)->name); 2377 fprintf (dump_file, " mapped to SCC leader node id %d ", j); 2378 if (j < FIRST_REF_NODE) 2379 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name); 2380 else 2381 fprintf (dump_file, "\"*%s\"\n", 2382 get_varinfo (j - FIRST_REF_NODE)->name); 2383 } 2384 else 2385 { 2386 fprintf (dump_file, 2387 "Equivalence classes for %s node id %d ", 2388 bitmap_bit_p (graph->direct_nodes, i) 2389 ? "direct" : "indirect", i); 2390 if (i < FIRST_REF_NODE) 2391 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name); 2392 else 2393 fprintf (dump_file, "\"*%s\"", 2394 get_varinfo (i - FIRST_REF_NODE)->name); 2395 fprintf (dump_file, 2396 ": pointer %d, location %d\n", 2397 graph->pointer_label[i], graph->loc_label[i]); 2398 } 2399 } 2400 2401 /* Quickly eliminate our non-pointer variables. */ 2402 2403 for (i = 1; i < FIRST_REF_NODE; i++) 2404 { 2405 unsigned int node = si->node_mapping[i]; 2406 2407 if (graph->pointer_label[node] == 0) 2408 { 2409 if (dump_file && (dump_flags & TDF_DETAILS)) 2410 fprintf (dump_file, 2411 "%s is a non-pointer variable, eliminating edges.\n", 2412 get_varinfo (node)->name); 2413 stats.nonpointer_vars++; 2414 clear_edges_for_node (graph, node); 2415 } 2416 } 2417 2418 return si; 2419 } 2420 2421 /* Free information that was only necessary for variable 2422 substitution. */ 2423 2424 static void 2425 free_var_substitution_info (struct scc_info *si) 2426 { 2427 delete si; 2428 free (graph->pointer_label); 2429 free (graph->loc_label); 2430 free (graph->pointed_by); 2431 free (graph->points_to); 2432 free (graph->eq_rep); 2433 sbitmap_free (graph->direct_nodes); 2434 delete pointer_equiv_class_table; 2435 pointer_equiv_class_table = NULL; 2436 delete location_equiv_class_table; 2437 location_equiv_class_table = NULL; 2438 bitmap_obstack_release (&iteration_obstack); 2439 } 2440 2441 /* Return an existing node that is equivalent to NODE, which has 2442 equivalence class LABEL, if one exists. Return NODE otherwise. */ 2443 2444 static unsigned int 2445 find_equivalent_node (constraint_graph_t graph, 2446 unsigned int node, unsigned int label) 2447 { 2448 /* If the address version of this variable is unused, we can 2449 substitute it for anything else with the same label. 2450 Otherwise, we know the pointers are equivalent, but not the 2451 locations, and we can unite them later. */ 2452 2453 if (!bitmap_bit_p (graph->address_taken, node)) 2454 { 2455 gcc_checking_assert (label < graph->size); 2456 2457 if (graph->eq_rep[label] != -1) 2458 { 2459 /* Unify the two variables since we know they are equivalent. */ 2460 if (unite (graph->eq_rep[label], node)) 2461 unify_nodes (graph, graph->eq_rep[label], node, false); 2462 return graph->eq_rep[label]; 2463 } 2464 else 2465 { 2466 graph->eq_rep[label] = node; 2467 graph->pe_rep[label] = node; 2468 } 2469 } 2470 else 2471 { 2472 gcc_checking_assert (label < graph->size); 2473 graph->pe[node] = label; 2474 if (graph->pe_rep[label] == -1) 2475 graph->pe_rep[label] = node; 2476 } 2477 2478 return node; 2479 } 2480 2481 /* Unite pointer equivalent but not location equivalent nodes in 2482 GRAPH. This may only be performed once variable substitution is 2483 finished. */ 2484 2485 static void 2486 unite_pointer_equivalences (constraint_graph_t graph) 2487 { 2488 unsigned int i; 2489 2490 /* Go through the pointer equivalences and unite them to their 2491 representative, if they aren't already. */ 2492 for (i = 1; i < FIRST_REF_NODE; i++) 2493 { 2494 unsigned int label = graph->pe[i]; 2495 if (label) 2496 { 2497 int label_rep = graph->pe_rep[label]; 2498 2499 if (label_rep == -1) 2500 continue; 2501 2502 label_rep = find (label_rep); 2503 if (label_rep >= 0 && unite (label_rep, find (i))) 2504 unify_nodes (graph, label_rep, i, false); 2505 } 2506 } 2507 } 2508 2509 /* Move complex constraints to the GRAPH nodes they belong to. */ 2510 2511 static void 2512 move_complex_constraints (constraint_graph_t graph) 2513 { 2514 int i; 2515 constraint_t c; 2516 2517 FOR_EACH_VEC_ELT (constraints, i, c) 2518 { 2519 if (c) 2520 { 2521 struct constraint_expr lhs = c->lhs; 2522 struct constraint_expr rhs = c->rhs; 2523 2524 if (lhs.type == DEREF) 2525 { 2526 insert_into_complex (graph, lhs.var, c); 2527 } 2528 else if (rhs.type == DEREF) 2529 { 2530 if (!(get_varinfo (lhs.var)->is_special_var)) 2531 insert_into_complex (graph, rhs.var, c); 2532 } 2533 else if (rhs.type != ADDRESSOF && lhs.var > anything_id 2534 && (lhs.offset != 0 || rhs.offset != 0)) 2535 { 2536 insert_into_complex (graph, rhs.var, c); 2537 } 2538 } 2539 } 2540 } 2541 2542 2543 /* Optimize and rewrite complex constraints while performing 2544 collapsing of equivalent nodes. SI is the SCC_INFO that is the 2545 result of perform_variable_substitution. */ 2546 2547 static void 2548 rewrite_constraints (constraint_graph_t graph, 2549 struct scc_info *si) 2550 { 2551 int i; 2552 constraint_t c; 2553 2554 if (flag_checking) 2555 { 2556 for (unsigned int j = 0; j < graph->size; j++) 2557 gcc_assert (find (j) == j); 2558 } 2559 2560 FOR_EACH_VEC_ELT (constraints, i, c) 2561 { 2562 struct constraint_expr lhs = c->lhs; 2563 struct constraint_expr rhs = c->rhs; 2564 unsigned int lhsvar = find (lhs.var); 2565 unsigned int rhsvar = find (rhs.var); 2566 unsigned int lhsnode, rhsnode; 2567 unsigned int lhslabel, rhslabel; 2568 2569 lhsnode = si->node_mapping[lhsvar]; 2570 rhsnode = si->node_mapping[rhsvar]; 2571 lhslabel = graph->pointer_label[lhsnode]; 2572 rhslabel = graph->pointer_label[rhsnode]; 2573 2574 /* See if it is really a non-pointer variable, and if so, ignore 2575 the constraint. */ 2576 if (lhslabel == 0) 2577 { 2578 if (dump_file && (dump_flags & TDF_DETAILS)) 2579 { 2580 2581 fprintf (dump_file, "%s is a non-pointer variable, " 2582 "ignoring constraint:", 2583 get_varinfo (lhs.var)->name); 2584 dump_constraint (dump_file, c); 2585 fprintf (dump_file, "\n"); 2586 } 2587 constraints[i] = NULL; 2588 continue; 2589 } 2590 2591 if (rhslabel == 0) 2592 { 2593 if (dump_file && (dump_flags & TDF_DETAILS)) 2594 { 2595 2596 fprintf (dump_file, "%s is a non-pointer variable, " 2597 "ignoring constraint:", 2598 get_varinfo (rhs.var)->name); 2599 dump_constraint (dump_file, c); 2600 fprintf (dump_file, "\n"); 2601 } 2602 constraints[i] = NULL; 2603 continue; 2604 } 2605 2606 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel); 2607 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel); 2608 c->lhs.var = lhsvar; 2609 c->rhs.var = rhsvar; 2610 } 2611 } 2612 2613 /* Eliminate indirect cycles involving NODE. Return true if NODE was 2614 part of an SCC, false otherwise. */ 2615 2616 static bool 2617 eliminate_indirect_cycles (unsigned int node) 2618 { 2619 if (graph->indirect_cycles[node] != -1 2620 && !bitmap_empty_p (get_varinfo (node)->solution)) 2621 { 2622 unsigned int i; 2623 auto_vec<unsigned> queue; 2624 int queuepos; 2625 unsigned int to = find (graph->indirect_cycles[node]); 2626 bitmap_iterator bi; 2627 2628 /* We can't touch the solution set and call unify_nodes 2629 at the same time, because unify_nodes is going to do 2630 bitmap unions into it. */ 2631 2632 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi) 2633 { 2634 if (find (i) == i && i != to) 2635 { 2636 if (unite (to, i)) 2637 queue.safe_push (i); 2638 } 2639 } 2640 2641 for (queuepos = 0; 2642 queue.iterate (queuepos, &i); 2643 queuepos++) 2644 { 2645 unify_nodes (graph, to, i, true); 2646 } 2647 return true; 2648 } 2649 return false; 2650 } 2651 2652 /* Solve the constraint graph GRAPH using our worklist solver. 2653 This is based on the PW* family of solvers from the "Efficient Field 2654 Sensitive Pointer Analysis for C" paper. 2655 It works by iterating over all the graph nodes, processing the complex 2656 constraints and propagating the copy constraints, until everything stops 2657 changed. This corresponds to steps 6-8 in the solving list given above. */ 2658 2659 static void 2660 solve_graph (constraint_graph_t graph) 2661 { 2662 unsigned int size = graph->size; 2663 unsigned int i; 2664 bitmap pts; 2665 2666 changed = BITMAP_ALLOC (NULL); 2667 2668 /* Mark all initial non-collapsed nodes as changed. */ 2669 for (i = 1; i < size; i++) 2670 { 2671 varinfo_t ivi = get_varinfo (i); 2672 if (find (i) == i && !bitmap_empty_p (ivi->solution) 2673 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i])) 2674 || graph->complex[i].length () > 0)) 2675 bitmap_set_bit (changed, i); 2676 } 2677 2678 /* Allocate a bitmap to be used to store the changed bits. */ 2679 pts = BITMAP_ALLOC (&pta_obstack); 2680 2681 while (!bitmap_empty_p (changed)) 2682 { 2683 unsigned int i; 2684 struct topo_info *ti = init_topo_info (); 2685 stats.iterations++; 2686 2687 bitmap_obstack_initialize (&iteration_obstack); 2688 2689 compute_topo_order (graph, ti); 2690 2691 while (ti->topo_order.length () != 0) 2692 { 2693 2694 i = ti->topo_order.pop (); 2695 2696 /* If this variable is not a representative, skip it. */ 2697 if (find (i) != i) 2698 continue; 2699 2700 /* In certain indirect cycle cases, we may merge this 2701 variable to another. */ 2702 if (eliminate_indirect_cycles (i) && find (i) != i) 2703 continue; 2704 2705 /* If the node has changed, we need to process the 2706 complex constraints and outgoing edges again. */ 2707 if (bitmap_clear_bit (changed, i)) 2708 { 2709 unsigned int j; 2710 constraint_t c; 2711 bitmap solution; 2712 vec<constraint_t> complex = graph->complex[i]; 2713 varinfo_t vi = get_varinfo (i); 2714 bool solution_empty; 2715 2716 /* Compute the changed set of solution bits. If anything 2717 is in the solution just propagate that. */ 2718 if (bitmap_bit_p (vi->solution, anything_id)) 2719 { 2720 /* If anything is also in the old solution there is 2721 nothing to do. 2722 ??? But we shouldn't ended up with "changed" set ... */ 2723 if (vi->oldsolution 2724 && bitmap_bit_p (vi->oldsolution, anything_id)) 2725 continue; 2726 bitmap_copy (pts, get_varinfo (find (anything_id))->solution); 2727 } 2728 else if (vi->oldsolution) 2729 bitmap_and_compl (pts, vi->solution, vi->oldsolution); 2730 else 2731 bitmap_copy (pts, vi->solution); 2732 2733 if (bitmap_empty_p (pts)) 2734 continue; 2735 2736 if (vi->oldsolution) 2737 bitmap_ior_into (vi->oldsolution, pts); 2738 else 2739 { 2740 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack); 2741 bitmap_copy (vi->oldsolution, pts); 2742 } 2743 2744 solution = vi->solution; 2745 solution_empty = bitmap_empty_p (solution); 2746 2747 /* Process the complex constraints */ 2748 bitmap expanded_pts = NULL; 2749 FOR_EACH_VEC_ELT (complex, j, c) 2750 { 2751 /* XXX: This is going to unsort the constraints in 2752 some cases, which will occasionally add duplicate 2753 constraints during unification. This does not 2754 affect correctness. */ 2755 c->lhs.var = find (c->lhs.var); 2756 c->rhs.var = find (c->rhs.var); 2757 2758 /* The only complex constraint that can change our 2759 solution to non-empty, given an empty solution, 2760 is a constraint where the lhs side is receiving 2761 some set from elsewhere. */ 2762 if (!solution_empty || c->lhs.type != DEREF) 2763 do_complex_constraint (graph, c, pts, &expanded_pts); 2764 } 2765 BITMAP_FREE (expanded_pts); 2766 2767 solution_empty = bitmap_empty_p (solution); 2768 2769 if (!solution_empty) 2770 { 2771 bitmap_iterator bi; 2772 unsigned eff_escaped_id = find (escaped_id); 2773 2774 /* Propagate solution to all successors. */ 2775 unsigned to_remove = ~0U; 2776 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 2777 0, j, bi) 2778 { 2779 if (to_remove != ~0U) 2780 { 2781 bitmap_clear_bit (graph->succs[i], to_remove); 2782 to_remove = ~0U; 2783 } 2784 unsigned int to = find (j); 2785 if (to != j) 2786 { 2787 /* Update the succ graph, avoiding duplicate 2788 work. */ 2789 to_remove = j; 2790 if (! bitmap_set_bit (graph->succs[i], to)) 2791 continue; 2792 /* We eventually end up processing 'to' twice 2793 as it is undefined whether bitmap iteration 2794 iterates over bits set during iteration. 2795 Play safe instead of doing tricks. */ 2796 } 2797 /* Don't try to propagate to ourselves. */ 2798 if (to == i) 2799 continue; 2800 2801 bitmap tmp = get_varinfo (to)->solution; 2802 bool flag = false; 2803 2804 /* If we propagate from ESCAPED use ESCAPED as 2805 placeholder. */ 2806 if (i == eff_escaped_id) 2807 flag = bitmap_set_bit (tmp, escaped_id); 2808 else 2809 flag = bitmap_ior_into (tmp, pts); 2810 2811 if (flag) 2812 bitmap_set_bit (changed, to); 2813 } 2814 if (to_remove != ~0U) 2815 bitmap_clear_bit (graph->succs[i], to_remove); 2816 } 2817 } 2818 } 2819 free_topo_info (ti); 2820 bitmap_obstack_release (&iteration_obstack); 2821 } 2822 2823 BITMAP_FREE (pts); 2824 BITMAP_FREE (changed); 2825 bitmap_obstack_release (&oldpta_obstack); 2826 } 2827 2828 /* Map from trees to variable infos. */ 2829 static hash_map<tree, varinfo_t> *vi_for_tree; 2830 2831 2832 /* Insert ID as the variable id for tree T in the vi_for_tree map. */ 2833 2834 static void 2835 insert_vi_for_tree (tree t, varinfo_t vi) 2836 { 2837 gcc_assert (vi); 2838 gcc_assert (!vi_for_tree->put (t, vi)); 2839 } 2840 2841 /* Find the variable info for tree T in VI_FOR_TREE. If T does not 2842 exist in the map, return NULL, otherwise, return the varinfo we found. */ 2843 2844 static varinfo_t 2845 lookup_vi_for_tree (tree t) 2846 { 2847 varinfo_t *slot = vi_for_tree->get (t); 2848 if (slot == NULL) 2849 return NULL; 2850 2851 return *slot; 2852 } 2853 2854 /* Return a printable name for DECL */ 2855 2856 static const char * 2857 alias_get_name (tree decl) 2858 { 2859 const char *res = "NULL"; 2860 if (dump_file) 2861 { 2862 char *temp = NULL; 2863 if (TREE_CODE (decl) == SSA_NAME) 2864 { 2865 res = get_name (decl); 2866 temp = xasprintf ("%s_%u", res ? res : "", SSA_NAME_VERSION (decl)); 2867 } 2868 else if (HAS_DECL_ASSEMBLER_NAME_P (decl) 2869 && DECL_ASSEMBLER_NAME_SET_P (decl)) 2870 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl)); 2871 else if (DECL_P (decl)) 2872 { 2873 res = get_name (decl); 2874 if (!res) 2875 temp = xasprintf ("D.%u", DECL_UID (decl)); 2876 } 2877 2878 if (temp) 2879 { 2880 res = ggc_strdup (temp); 2881 free (temp); 2882 } 2883 } 2884 2885 return res; 2886 } 2887 2888 /* Find the variable id for tree T in the map. 2889 If T doesn't exist in the map, create an entry for it and return it. */ 2890 2891 static varinfo_t 2892 get_vi_for_tree (tree t) 2893 { 2894 varinfo_t *slot = vi_for_tree->get (t); 2895 if (slot == NULL) 2896 { 2897 unsigned int id = create_variable_info_for (t, alias_get_name (t), false); 2898 return get_varinfo (id); 2899 } 2900 2901 return *slot; 2902 } 2903 2904 /* Get a scalar constraint expression for a new temporary variable. */ 2905 2906 static struct constraint_expr 2907 new_scalar_tmp_constraint_exp (const char *name, bool add_id) 2908 { 2909 struct constraint_expr tmp; 2910 varinfo_t vi; 2911 2912 vi = new_var_info (NULL_TREE, name, add_id); 2913 vi->offset = 0; 2914 vi->size = -1; 2915 vi->fullsize = -1; 2916 vi->is_full_var = 1; 2917 vi->is_reg_var = 1; 2918 2919 tmp.var = vi->id; 2920 tmp.type = SCALAR; 2921 tmp.offset = 0; 2922 2923 return tmp; 2924 } 2925 2926 /* Get a constraint expression vector from an SSA_VAR_P node. 2927 If address_p is true, the result will be taken its address of. */ 2928 2929 static void 2930 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p) 2931 { 2932 struct constraint_expr cexpr; 2933 varinfo_t vi; 2934 2935 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */ 2936 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t)); 2937 2938 if (TREE_CODE (t) == SSA_NAME 2939 && SSA_NAME_IS_DEFAULT_DEF (t)) 2940 { 2941 /* For parameters, get at the points-to set for the actual parm 2942 decl. */ 2943 if (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL 2944 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL) 2945 { 2946 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p); 2947 return; 2948 } 2949 /* For undefined SSA names return nothing. */ 2950 else if (!ssa_defined_default_def_p (t)) 2951 { 2952 cexpr.var = nothing_id; 2953 cexpr.type = SCALAR; 2954 cexpr.offset = 0; 2955 results->safe_push (cexpr); 2956 return; 2957 } 2958 } 2959 2960 /* For global variables resort to the alias target. */ 2961 if (VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t))) 2962 { 2963 varpool_node *node = varpool_node::get (t); 2964 if (node && node->alias && node->analyzed) 2965 { 2966 node = node->ultimate_alias_target (); 2967 /* Canonicalize the PT uid of all aliases to the ultimate target. 2968 ??? Hopefully the set of aliases can't change in a way that 2969 changes the ultimate alias target. */ 2970 gcc_assert ((! DECL_PT_UID_SET_P (node->decl) 2971 || DECL_PT_UID (node->decl) == DECL_UID (node->decl)) 2972 && (! DECL_PT_UID_SET_P (t) 2973 || DECL_PT_UID (t) == DECL_UID (node->decl))); 2974 DECL_PT_UID (t) = DECL_UID (node->decl); 2975 t = node->decl; 2976 } 2977 2978 /* If this is decl may bind to NULL note that. */ 2979 if (address_p 2980 && (! node || ! node->nonzero_address ())) 2981 { 2982 cexpr.var = nothing_id; 2983 cexpr.type = SCALAR; 2984 cexpr.offset = 0; 2985 results->safe_push (cexpr); 2986 } 2987 } 2988 2989 vi = get_vi_for_tree (t); 2990 cexpr.var = vi->id; 2991 cexpr.type = SCALAR; 2992 cexpr.offset = 0; 2993 2994 /* If we are not taking the address of the constraint expr, add all 2995 sub-fiels of the variable as well. */ 2996 if (!address_p 2997 && !vi->is_full_var) 2998 { 2999 for (; vi; vi = vi_next (vi)) 3000 { 3001 cexpr.var = vi->id; 3002 results->safe_push (cexpr); 3003 } 3004 return; 3005 } 3006 3007 results->safe_push (cexpr); 3008 } 3009 3010 /* Process constraint T, performing various simplifications and then 3011 adding it to our list of overall constraints. */ 3012 3013 static void 3014 process_constraint (constraint_t t) 3015 { 3016 struct constraint_expr rhs = t->rhs; 3017 struct constraint_expr lhs = t->lhs; 3018 3019 gcc_assert (rhs.var < varmap.length ()); 3020 gcc_assert (lhs.var < varmap.length ()); 3021 3022 /* If we didn't get any useful constraint from the lhs we get 3023 &ANYTHING as fallback from get_constraint_for. Deal with 3024 it here by turning it into *ANYTHING. */ 3025 if (lhs.type == ADDRESSOF 3026 && lhs.var == anything_id) 3027 lhs.type = DEREF; 3028 3029 /* ADDRESSOF on the lhs is invalid. */ 3030 gcc_assert (lhs.type != ADDRESSOF); 3031 3032 /* We shouldn't add constraints from things that cannot have pointers. 3033 It's not completely trivial to avoid in the callers, so do it here. */ 3034 if (rhs.type != ADDRESSOF 3035 && !get_varinfo (rhs.var)->may_have_pointers) 3036 return; 3037 3038 /* Likewise adding to the solution of a non-pointer var isn't useful. */ 3039 if (!get_varinfo (lhs.var)->may_have_pointers) 3040 return; 3041 3042 /* This can happen in our IR with things like n->a = *p */ 3043 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id) 3044 { 3045 /* Split into tmp = *rhs, *lhs = tmp */ 3046 struct constraint_expr tmplhs; 3047 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp", true); 3048 process_constraint (new_constraint (tmplhs, rhs)); 3049 process_constraint (new_constraint (lhs, tmplhs)); 3050 } 3051 else if ((rhs.type != SCALAR || rhs.offset != 0) && lhs.type == DEREF) 3052 { 3053 /* Split into tmp = &rhs, *lhs = tmp */ 3054 struct constraint_expr tmplhs; 3055 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp", true); 3056 process_constraint (new_constraint (tmplhs, rhs)); 3057 process_constraint (new_constraint (lhs, tmplhs)); 3058 } 3059 else 3060 { 3061 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0); 3062 constraints.safe_push (t); 3063 } 3064 } 3065 3066 3067 /* Return the position, in bits, of FIELD_DECL from the beginning of its 3068 structure. */ 3069 3070 static HOST_WIDE_INT 3071 bitpos_of_field (const tree fdecl) 3072 { 3073 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl)) 3074 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl))) 3075 return -1; 3076 3077 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT 3078 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl))); 3079 } 3080 3081 3082 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the 3083 resulting constraint expressions in *RESULTS. */ 3084 3085 static void 3086 get_constraint_for_ptr_offset (tree ptr, tree offset, 3087 vec<ce_s> *results) 3088 { 3089 struct constraint_expr c; 3090 unsigned int j, n; 3091 HOST_WIDE_INT rhsoffset; 3092 3093 /* If we do not do field-sensitive PTA adding offsets to pointers 3094 does not change the points-to solution. */ 3095 if (!use_field_sensitive) 3096 { 3097 get_constraint_for_rhs (ptr, results); 3098 return; 3099 } 3100 3101 /* If the offset is not a non-negative integer constant that fits 3102 in a HOST_WIDE_INT, we have to fall back to a conservative 3103 solution which includes all sub-fields of all pointed-to 3104 variables of ptr. */ 3105 if (offset == NULL_TREE 3106 || TREE_CODE (offset) != INTEGER_CST) 3107 rhsoffset = UNKNOWN_OFFSET; 3108 else 3109 { 3110 /* Sign-extend the offset. */ 3111 offset_int soffset = offset_int::from (wi::to_wide (offset), SIGNED); 3112 if (!wi::fits_shwi_p (soffset)) 3113 rhsoffset = UNKNOWN_OFFSET; 3114 else 3115 { 3116 /* Make sure the bit-offset also fits. */ 3117 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi (); 3118 rhsoffset = rhsunitoffset * (unsigned HOST_WIDE_INT) BITS_PER_UNIT; 3119 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT) 3120 rhsoffset = UNKNOWN_OFFSET; 3121 } 3122 } 3123 3124 get_constraint_for_rhs (ptr, results); 3125 if (rhsoffset == 0) 3126 return; 3127 3128 /* As we are eventually appending to the solution do not use 3129 vec::iterate here. */ 3130 n = results->length (); 3131 for (j = 0; j < n; j++) 3132 { 3133 varinfo_t curr; 3134 c = (*results)[j]; 3135 curr = get_varinfo (c.var); 3136 3137 if (c.type == ADDRESSOF 3138 /* If this varinfo represents a full variable just use it. */ 3139 && curr->is_full_var) 3140 ; 3141 else if (c.type == ADDRESSOF 3142 /* If we do not know the offset add all subfields. */ 3143 && rhsoffset == UNKNOWN_OFFSET) 3144 { 3145 varinfo_t temp = get_varinfo (curr->head); 3146 do 3147 { 3148 struct constraint_expr c2; 3149 c2.var = temp->id; 3150 c2.type = ADDRESSOF; 3151 c2.offset = 0; 3152 if (c2.var != c.var) 3153 results->safe_push (c2); 3154 temp = vi_next (temp); 3155 } 3156 while (temp); 3157 } 3158 else if (c.type == ADDRESSOF) 3159 { 3160 varinfo_t temp; 3161 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset; 3162 3163 /* If curr->offset + rhsoffset is less than zero adjust it. */ 3164 if (rhsoffset < 0 3165 && curr->offset < offset) 3166 offset = 0; 3167 3168 /* We have to include all fields that overlap the current 3169 field shifted by rhsoffset. And we include at least 3170 the last or the first field of the variable to represent 3171 reachability of off-bound addresses, in particular &object + 1, 3172 conservatively correct. */ 3173 temp = first_or_preceding_vi_for_offset (curr, offset); 3174 c.var = temp->id; 3175 c.offset = 0; 3176 temp = vi_next (temp); 3177 while (temp 3178 && temp->offset < offset + curr->size) 3179 { 3180 struct constraint_expr c2; 3181 c2.var = temp->id; 3182 c2.type = ADDRESSOF; 3183 c2.offset = 0; 3184 results->safe_push (c2); 3185 temp = vi_next (temp); 3186 } 3187 } 3188 else if (c.type == SCALAR) 3189 { 3190 gcc_assert (c.offset == 0); 3191 c.offset = rhsoffset; 3192 } 3193 else 3194 /* We shouldn't get any DEREFs here. */ 3195 gcc_unreachable (); 3196 3197 (*results)[j] = c; 3198 } 3199 } 3200 3201 3202 /* Given a COMPONENT_REF T, return the constraint_expr vector for it. 3203 If address_p is true the result will be taken its address of. 3204 If lhs_p is true then the constraint expression is assumed to be used 3205 as the lhs. */ 3206 3207 static void 3208 get_constraint_for_component_ref (tree t, vec<ce_s> *results, 3209 bool address_p, bool lhs_p) 3210 { 3211 tree orig_t = t; 3212 poly_int64 bitsize = -1; 3213 poly_int64 bitmaxsize = -1; 3214 poly_int64 bitpos; 3215 bool reverse; 3216 tree forzero; 3217 3218 /* Some people like to do cute things like take the address of 3219 &0->a.b */ 3220 forzero = t; 3221 while (handled_component_p (forzero) 3222 || INDIRECT_REF_P (forzero) 3223 || TREE_CODE (forzero) == MEM_REF) 3224 forzero = TREE_OPERAND (forzero, 0); 3225 3226 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero)) 3227 { 3228 struct constraint_expr temp; 3229 3230 temp.offset = 0; 3231 temp.var = integer_id; 3232 temp.type = SCALAR; 3233 results->safe_push (temp); 3234 return; 3235 } 3236 3237 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize, &reverse); 3238 3239 /* We can end up here for component references on a 3240 VIEW_CONVERT_EXPR <>(&foobar) or things like a 3241 BIT_FIELD_REF <&MEM[(void *)&b + 4B], ...>. So for 3242 symbolic constants simply give up. */ 3243 if (TREE_CODE (t) == ADDR_EXPR) 3244 { 3245 constraint_expr result; 3246 result.type = SCALAR; 3247 result.var = anything_id; 3248 result.offset = 0; 3249 results->safe_push (result); 3250 return; 3251 } 3252 3253 /* Avoid creating pointer-offset constraints, so handle MEM_REF 3254 offsets directly. Pretend to take the address of the base, 3255 we'll take care of adding the required subset of sub-fields below. */ 3256 if (TREE_CODE (t) == MEM_REF 3257 && !integer_zerop (TREE_OPERAND (t, 0))) 3258 { 3259 poly_offset_int off = mem_ref_offset (t); 3260 off <<= LOG2_BITS_PER_UNIT; 3261 off += bitpos; 3262 poly_int64 off_hwi; 3263 if (off.to_shwi (&off_hwi)) 3264 bitpos = off_hwi; 3265 else 3266 { 3267 bitpos = 0; 3268 bitmaxsize = -1; 3269 } 3270 get_constraint_for_1 (TREE_OPERAND (t, 0), results, false, lhs_p); 3271 do_deref (results); 3272 } 3273 else 3274 get_constraint_for_1 (t, results, true, lhs_p); 3275 3276 /* Strip off nothing_id. */ 3277 if (results->length () == 2) 3278 { 3279 gcc_assert ((*results)[0].var == nothing_id); 3280 results->unordered_remove (0); 3281 } 3282 gcc_assert (results->length () == 1); 3283 struct constraint_expr &result = results->last (); 3284 3285 if (result.type == SCALAR 3286 && get_varinfo (result.var)->is_full_var) 3287 /* For single-field vars do not bother about the offset. */ 3288 result.offset = 0; 3289 else if (result.type == SCALAR) 3290 { 3291 /* In languages like C, you can access one past the end of an 3292 array. You aren't allowed to dereference it, so we can 3293 ignore this constraint. When we handle pointer subtraction, 3294 we may have to do something cute here. */ 3295 3296 if (maybe_lt (poly_uint64 (bitpos), get_varinfo (result.var)->fullsize) 3297 && maybe_ne (bitmaxsize, 0)) 3298 { 3299 /* It's also not true that the constraint will actually start at the 3300 right offset, it may start in some padding. We only care about 3301 setting the constraint to the first actual field it touches, so 3302 walk to find it. */ 3303 struct constraint_expr cexpr = result; 3304 varinfo_t curr; 3305 results->pop (); 3306 cexpr.offset = 0; 3307 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr)) 3308 { 3309 if (ranges_maybe_overlap_p (poly_int64 (curr->offset), 3310 curr->size, bitpos, bitmaxsize)) 3311 { 3312 cexpr.var = curr->id; 3313 results->safe_push (cexpr); 3314 if (address_p) 3315 break; 3316 } 3317 } 3318 /* If we are going to take the address of this field then 3319 to be able to compute reachability correctly add at least 3320 the last field of the variable. */ 3321 if (address_p && results->length () == 0) 3322 { 3323 curr = get_varinfo (cexpr.var); 3324 while (curr->next != 0) 3325 curr = vi_next (curr); 3326 cexpr.var = curr->id; 3327 results->safe_push (cexpr); 3328 } 3329 else if (results->length () == 0) 3330 /* Assert that we found *some* field there. The user couldn't be 3331 accessing *only* padding. */ 3332 /* Still the user could access one past the end of an array 3333 embedded in a struct resulting in accessing *only* padding. */ 3334 /* Or accessing only padding via type-punning to a type 3335 that has a filed just in padding space. */ 3336 { 3337 cexpr.type = SCALAR; 3338 cexpr.var = anything_id; 3339 cexpr.offset = 0; 3340 results->safe_push (cexpr); 3341 } 3342 } 3343 else if (known_eq (bitmaxsize, 0)) 3344 { 3345 if (dump_file && (dump_flags & TDF_DETAILS)) 3346 fprintf (dump_file, "Access to zero-sized part of variable, " 3347 "ignoring\n"); 3348 } 3349 else 3350 if (dump_file && (dump_flags & TDF_DETAILS)) 3351 fprintf (dump_file, "Access to past the end of variable, ignoring\n"); 3352 } 3353 else if (result.type == DEREF) 3354 { 3355 /* If we do not know exactly where the access goes say so. Note 3356 that only for non-structure accesses we know that we access 3357 at most one subfiled of any variable. */ 3358 HOST_WIDE_INT const_bitpos; 3359 if (!bitpos.is_constant (&const_bitpos) 3360 || const_bitpos == -1 3361 || maybe_ne (bitsize, bitmaxsize) 3362 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t)) 3363 || result.offset == UNKNOWN_OFFSET) 3364 result.offset = UNKNOWN_OFFSET; 3365 else 3366 result.offset += const_bitpos; 3367 } 3368 else if (result.type == ADDRESSOF) 3369 { 3370 /* We can end up here for component references on constants like 3371 VIEW_CONVERT_EXPR <>({ 0, 1, 2, 3 })[i]. */ 3372 result.type = SCALAR; 3373 result.var = anything_id; 3374 result.offset = 0; 3375 } 3376 else 3377 gcc_unreachable (); 3378 } 3379 3380 3381 /* Dereference the constraint expression CONS, and return the result. 3382 DEREF (ADDRESSOF) = SCALAR 3383 DEREF (SCALAR) = DEREF 3384 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp)) 3385 This is needed so that we can handle dereferencing DEREF constraints. */ 3386 3387 static void 3388 do_deref (vec<ce_s> *constraints) 3389 { 3390 struct constraint_expr *c; 3391 unsigned int i = 0; 3392 3393 FOR_EACH_VEC_ELT (*constraints, i, c) 3394 { 3395 if (c->type == SCALAR) 3396 c->type = DEREF; 3397 else if (c->type == ADDRESSOF) 3398 c->type = SCALAR; 3399 else if (c->type == DEREF) 3400 { 3401 struct constraint_expr tmplhs; 3402 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp", true); 3403 process_constraint (new_constraint (tmplhs, *c)); 3404 c->var = tmplhs.var; 3405 } 3406 else 3407 gcc_unreachable (); 3408 } 3409 } 3410 3411 /* Given a tree T, return the constraint expression for taking the 3412 address of it. */ 3413 3414 static void 3415 get_constraint_for_address_of (tree t, vec<ce_s> *results) 3416 { 3417 struct constraint_expr *c; 3418 unsigned int i; 3419 3420 get_constraint_for_1 (t, results, true, true); 3421 3422 FOR_EACH_VEC_ELT (*results, i, c) 3423 { 3424 if (c->type == DEREF) 3425 c->type = SCALAR; 3426 else 3427 c->type = ADDRESSOF; 3428 } 3429 } 3430 3431 /* Given a tree T, return the constraint expression for it. */ 3432 3433 static void 3434 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p, 3435 bool lhs_p) 3436 { 3437 struct constraint_expr temp; 3438 3439 /* x = integer is all glommed to a single variable, which doesn't 3440 point to anything by itself. That is, of course, unless it is an 3441 integer constant being treated as a pointer, in which case, we 3442 will return that this is really the addressof anything. This 3443 happens below, since it will fall into the default case. The only 3444 case we know something about an integer treated like a pointer is 3445 when it is the NULL pointer, and then we just say it points to 3446 NULL. 3447 3448 Do not do that if -fno-delete-null-pointer-checks though, because 3449 in that case *NULL does not fail, so it _should_ alias *anything. 3450 It is not worth adding a new option or renaming the existing one, 3451 since this case is relatively obscure. */ 3452 if ((TREE_CODE (t) == INTEGER_CST 3453 && integer_zerop (t)) 3454 /* The only valid CONSTRUCTORs in gimple with pointer typed 3455 elements are zero-initializer. But in IPA mode we also 3456 process global initializers, so verify at least. */ 3457 || (TREE_CODE (t) == CONSTRUCTOR 3458 && CONSTRUCTOR_NELTS (t) == 0)) 3459 { 3460 if (flag_delete_null_pointer_checks) 3461 temp.var = nothing_id; 3462 else 3463 temp.var = nonlocal_id; 3464 temp.type = ADDRESSOF; 3465 temp.offset = 0; 3466 results->safe_push (temp); 3467 return; 3468 } 3469 3470 /* String constants are read-only, ideally we'd have a CONST_DECL 3471 for those. */ 3472 if (TREE_CODE (t) == STRING_CST) 3473 { 3474 temp.var = string_id; 3475 temp.type = SCALAR; 3476 temp.offset = 0; 3477 results->safe_push (temp); 3478 return; 3479 } 3480 3481 switch (TREE_CODE_CLASS (TREE_CODE (t))) 3482 { 3483 case tcc_expression: 3484 { 3485 switch (TREE_CODE (t)) 3486 { 3487 case ADDR_EXPR: 3488 get_constraint_for_address_of (TREE_OPERAND (t, 0), results); 3489 return; 3490 default:; 3491 } 3492 break; 3493 } 3494 case tcc_reference: 3495 { 3496 switch (TREE_CODE (t)) 3497 { 3498 case MEM_REF: 3499 { 3500 struct constraint_expr cs; 3501 varinfo_t vi, curr; 3502 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0), 3503 TREE_OPERAND (t, 1), results); 3504 do_deref (results); 3505 3506 /* If we are not taking the address then make sure to process 3507 all subvariables we might access. */ 3508 if (address_p) 3509 return; 3510 3511 cs = results->last (); 3512 if (cs.type == DEREF 3513 && type_can_have_subvars (TREE_TYPE (t))) 3514 { 3515 /* For dereferences this means we have to defer it 3516 to solving time. */ 3517 results->last ().offset = UNKNOWN_OFFSET; 3518 return; 3519 } 3520 if (cs.type != SCALAR) 3521 return; 3522 3523 vi = get_varinfo (cs.var); 3524 curr = vi_next (vi); 3525 if (!vi->is_full_var 3526 && curr) 3527 { 3528 unsigned HOST_WIDE_INT size; 3529 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t)))) 3530 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t))); 3531 else 3532 size = -1; 3533 for (; curr; curr = vi_next (curr)) 3534 { 3535 if (curr->offset - vi->offset < size) 3536 { 3537 cs.var = curr->id; 3538 results->safe_push (cs); 3539 } 3540 else 3541 break; 3542 } 3543 } 3544 return; 3545 } 3546 case ARRAY_REF: 3547 case ARRAY_RANGE_REF: 3548 case COMPONENT_REF: 3549 case IMAGPART_EXPR: 3550 case REALPART_EXPR: 3551 case BIT_FIELD_REF: 3552 get_constraint_for_component_ref (t, results, address_p, lhs_p); 3553 return; 3554 case VIEW_CONVERT_EXPR: 3555 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p, 3556 lhs_p); 3557 return; 3558 /* We are missing handling for TARGET_MEM_REF here. */ 3559 default:; 3560 } 3561 break; 3562 } 3563 case tcc_exceptional: 3564 { 3565 switch (TREE_CODE (t)) 3566 { 3567 case SSA_NAME: 3568 { 3569 get_constraint_for_ssa_var (t, results, address_p); 3570 return; 3571 } 3572 case CONSTRUCTOR: 3573 { 3574 unsigned int i; 3575 tree val; 3576 auto_vec<ce_s> tmp; 3577 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val) 3578 { 3579 struct constraint_expr *rhsp; 3580 unsigned j; 3581 get_constraint_for_1 (val, &tmp, address_p, lhs_p); 3582 FOR_EACH_VEC_ELT (tmp, j, rhsp) 3583 results->safe_push (*rhsp); 3584 tmp.truncate (0); 3585 } 3586 /* We do not know whether the constructor was complete, 3587 so technically we have to add &NOTHING or &ANYTHING 3588 like we do for an empty constructor as well. */ 3589 return; 3590 } 3591 default:; 3592 } 3593 break; 3594 } 3595 case tcc_declaration: 3596 { 3597 get_constraint_for_ssa_var (t, results, address_p); 3598 return; 3599 } 3600 case tcc_constant: 3601 { 3602 /* We cannot refer to automatic variables through constants. */ 3603 temp.type = ADDRESSOF; 3604 temp.var = nonlocal_id; 3605 temp.offset = 0; 3606 results->safe_push (temp); 3607 return; 3608 } 3609 default:; 3610 } 3611 3612 /* The default fallback is a constraint from anything. */ 3613 temp.type = ADDRESSOF; 3614 temp.var = anything_id; 3615 temp.offset = 0; 3616 results->safe_push (temp); 3617 } 3618 3619 /* Given a gimple tree T, return the constraint expression vector for it. */ 3620 3621 static void 3622 get_constraint_for (tree t, vec<ce_s> *results) 3623 { 3624 gcc_assert (results->length () == 0); 3625 3626 get_constraint_for_1 (t, results, false, true); 3627 } 3628 3629 /* Given a gimple tree T, return the constraint expression vector for it 3630 to be used as the rhs of a constraint. */ 3631 3632 static void 3633 get_constraint_for_rhs (tree t, vec<ce_s> *results) 3634 { 3635 gcc_assert (results->length () == 0); 3636 3637 get_constraint_for_1 (t, results, false, false); 3638 } 3639 3640 3641 /* Efficiently generates constraints from all entries in *RHSC to all 3642 entries in *LHSC. */ 3643 3644 static void 3645 process_all_all_constraints (vec<ce_s> lhsc, 3646 vec<ce_s> rhsc) 3647 { 3648 struct constraint_expr *lhsp, *rhsp; 3649 unsigned i, j; 3650 3651 if (lhsc.length () <= 1 || rhsc.length () <= 1) 3652 { 3653 FOR_EACH_VEC_ELT (lhsc, i, lhsp) 3654 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 3655 process_constraint (new_constraint (*lhsp, *rhsp)); 3656 } 3657 else 3658 { 3659 struct constraint_expr tmp; 3660 tmp = new_scalar_tmp_constraint_exp ("allalltmp", true); 3661 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 3662 process_constraint (new_constraint (tmp, *rhsp)); 3663 FOR_EACH_VEC_ELT (lhsc, i, lhsp) 3664 process_constraint (new_constraint (*lhsp, tmp)); 3665 } 3666 } 3667 3668 /* Handle aggregate copies by expanding into copies of the respective 3669 fields of the structures. */ 3670 3671 static void 3672 do_structure_copy (tree lhsop, tree rhsop) 3673 { 3674 struct constraint_expr *lhsp, *rhsp; 3675 auto_vec<ce_s> lhsc; 3676 auto_vec<ce_s> rhsc; 3677 unsigned j; 3678 3679 get_constraint_for (lhsop, &lhsc); 3680 get_constraint_for_rhs (rhsop, &rhsc); 3681 lhsp = &lhsc[0]; 3682 rhsp = &rhsc[0]; 3683 if (lhsp->type == DEREF 3684 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id) 3685 || rhsp->type == DEREF) 3686 { 3687 if (lhsp->type == DEREF) 3688 { 3689 gcc_assert (lhsc.length () == 1); 3690 lhsp->offset = UNKNOWN_OFFSET; 3691 } 3692 if (rhsp->type == DEREF) 3693 { 3694 gcc_assert (rhsc.length () == 1); 3695 rhsp->offset = UNKNOWN_OFFSET; 3696 } 3697 process_all_all_constraints (lhsc, rhsc); 3698 } 3699 else if (lhsp->type == SCALAR 3700 && (rhsp->type == SCALAR 3701 || rhsp->type == ADDRESSOF)) 3702 { 3703 HOST_WIDE_INT lhssize, lhsoffset; 3704 HOST_WIDE_INT rhssize, rhsoffset; 3705 bool reverse; 3706 unsigned k = 0; 3707 if (!get_ref_base_and_extent_hwi (lhsop, &lhsoffset, &lhssize, &reverse) 3708 || !get_ref_base_and_extent_hwi (rhsop, &rhsoffset, &rhssize, 3709 &reverse)) 3710 { 3711 process_all_all_constraints (lhsc, rhsc); 3712 return; 3713 } 3714 for (j = 0; lhsc.iterate (j, &lhsp);) 3715 { 3716 varinfo_t lhsv, rhsv; 3717 rhsp = &rhsc[k]; 3718 lhsv = get_varinfo (lhsp->var); 3719 rhsv = get_varinfo (rhsp->var); 3720 if (lhsv->may_have_pointers 3721 && (lhsv->is_full_var 3722 || rhsv->is_full_var 3723 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size, 3724 rhsv->offset + lhsoffset, rhsv->size))) 3725 process_constraint (new_constraint (*lhsp, *rhsp)); 3726 if (!rhsv->is_full_var 3727 && (lhsv->is_full_var 3728 || (lhsv->offset + rhsoffset + lhsv->size 3729 > rhsv->offset + lhsoffset + rhsv->size))) 3730 { 3731 ++k; 3732 if (k >= rhsc.length ()) 3733 break; 3734 } 3735 else 3736 ++j; 3737 } 3738 } 3739 else 3740 gcc_unreachable (); 3741 } 3742 3743 /* Create constraints ID = { rhsc }. */ 3744 3745 static void 3746 make_constraints_to (unsigned id, vec<ce_s> rhsc) 3747 { 3748 struct constraint_expr *c; 3749 struct constraint_expr includes; 3750 unsigned int j; 3751 3752 includes.var = id; 3753 includes.offset = 0; 3754 includes.type = SCALAR; 3755 3756 FOR_EACH_VEC_ELT (rhsc, j, c) 3757 process_constraint (new_constraint (includes, *c)); 3758 } 3759 3760 /* Create a constraint ID = OP. */ 3761 3762 static void 3763 make_constraint_to (unsigned id, tree op) 3764 { 3765 auto_vec<ce_s> rhsc; 3766 get_constraint_for_rhs (op, &rhsc); 3767 make_constraints_to (id, rhsc); 3768 } 3769 3770 /* Create a constraint ID = &FROM. */ 3771 3772 static void 3773 make_constraint_from (varinfo_t vi, int from) 3774 { 3775 struct constraint_expr lhs, rhs; 3776 3777 lhs.var = vi->id; 3778 lhs.offset = 0; 3779 lhs.type = SCALAR; 3780 3781 rhs.var = from; 3782 rhs.offset = 0; 3783 rhs.type = ADDRESSOF; 3784 process_constraint (new_constraint (lhs, rhs)); 3785 } 3786 3787 /* Create a constraint ID = FROM. */ 3788 3789 static void 3790 make_copy_constraint (varinfo_t vi, int from) 3791 { 3792 struct constraint_expr lhs, rhs; 3793 3794 lhs.var = vi->id; 3795 lhs.offset = 0; 3796 lhs.type = SCALAR; 3797 3798 rhs.var = from; 3799 rhs.offset = 0; 3800 rhs.type = SCALAR; 3801 process_constraint (new_constraint (lhs, rhs)); 3802 } 3803 3804 /* Make constraints necessary to make OP escape. */ 3805 3806 static void 3807 make_escape_constraint (tree op) 3808 { 3809 make_constraint_to (escaped_id, op); 3810 } 3811 3812 /* Add constraints to that the solution of VI is transitively closed. */ 3813 3814 static void 3815 make_transitive_closure_constraints (varinfo_t vi) 3816 { 3817 struct constraint_expr lhs, rhs; 3818 3819 /* VAR = *(VAR + UNKNOWN); */ 3820 lhs.type = SCALAR; 3821 lhs.var = vi->id; 3822 lhs.offset = 0; 3823 rhs.type = DEREF; 3824 rhs.var = vi->id; 3825 rhs.offset = UNKNOWN_OFFSET; 3826 process_constraint (new_constraint (lhs, rhs)); 3827 } 3828 3829 /* Add constraints to that the solution of VI has all subvariables added. */ 3830 3831 static void 3832 make_any_offset_constraints (varinfo_t vi) 3833 { 3834 struct constraint_expr lhs, rhs; 3835 3836 /* VAR = VAR + UNKNOWN; */ 3837 lhs.type = SCALAR; 3838 lhs.var = vi->id; 3839 lhs.offset = 0; 3840 rhs.type = SCALAR; 3841 rhs.var = vi->id; 3842 rhs.offset = UNKNOWN_OFFSET; 3843 process_constraint (new_constraint (lhs, rhs)); 3844 } 3845 3846 /* Temporary storage for fake var decls. */ 3847 struct obstack fake_var_decl_obstack; 3848 3849 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */ 3850 3851 static tree 3852 build_fake_var_decl (tree type) 3853 { 3854 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl); 3855 memset (decl, 0, sizeof (struct tree_var_decl)); 3856 TREE_SET_CODE (decl, VAR_DECL); 3857 TREE_TYPE (decl) = type; 3858 DECL_UID (decl) = allocate_decl_uid (); 3859 SET_DECL_PT_UID (decl, -1); 3860 layout_decl (decl, 0); 3861 return decl; 3862 } 3863 3864 /* Create a new artificial heap variable with NAME. 3865 Return the created variable. */ 3866 3867 static varinfo_t 3868 make_heapvar (const char *name, bool add_id) 3869 { 3870 varinfo_t vi; 3871 tree heapvar; 3872 3873 heapvar = build_fake_var_decl (ptr_type_node); 3874 DECL_EXTERNAL (heapvar) = 1; 3875 3876 vi = new_var_info (heapvar, name, add_id); 3877 vi->is_artificial_var = true; 3878 vi->is_heap_var = true; 3879 vi->is_unknown_size_var = true; 3880 vi->offset = 0; 3881 vi->fullsize = ~0; 3882 vi->size = ~0; 3883 vi->is_full_var = true; 3884 insert_vi_for_tree (heapvar, vi); 3885 3886 return vi; 3887 } 3888 3889 /* Create a new artificial heap variable with NAME and make a 3890 constraint from it to LHS. Set flags according to a tag used 3891 for tracking restrict pointers. */ 3892 3893 static varinfo_t 3894 make_constraint_from_restrict (varinfo_t lhs, const char *name, bool add_id) 3895 { 3896 varinfo_t vi = make_heapvar (name, add_id); 3897 vi->is_restrict_var = 1; 3898 vi->is_global_var = 1; 3899 vi->may_have_pointers = 1; 3900 make_constraint_from (lhs, vi->id); 3901 return vi; 3902 } 3903 3904 /* Create a new artificial heap variable with NAME and make a 3905 constraint from it to LHS. Set flags according to a tag used 3906 for tracking restrict pointers and make the artificial heap 3907 point to global memory. */ 3908 3909 static varinfo_t 3910 make_constraint_from_global_restrict (varinfo_t lhs, const char *name, 3911 bool add_id) 3912 { 3913 varinfo_t vi = make_constraint_from_restrict (lhs, name, add_id); 3914 make_copy_constraint (vi, nonlocal_id); 3915 return vi; 3916 } 3917 3918 /* In IPA mode there are varinfos for different aspects of reach 3919 function designator. One for the points-to set of the return 3920 value, one for the variables that are clobbered by the function, 3921 one for its uses and one for each parameter (including a single 3922 glob for remaining variadic arguments). */ 3923 3924 enum { fi_clobbers = 1, fi_uses = 2, 3925 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 }; 3926 3927 /* Get a constraint for the requested part of a function designator FI 3928 when operating in IPA mode. */ 3929 3930 static struct constraint_expr 3931 get_function_part_constraint (varinfo_t fi, unsigned part) 3932 { 3933 struct constraint_expr c; 3934 3935 gcc_assert (in_ipa_mode); 3936 3937 if (fi->id == anything_id) 3938 { 3939 /* ??? We probably should have a ANYFN special variable. */ 3940 c.var = anything_id; 3941 c.offset = 0; 3942 c.type = SCALAR; 3943 } 3944 else if (fi->decl && TREE_CODE (fi->decl) == FUNCTION_DECL) 3945 { 3946 varinfo_t ai = first_vi_for_offset (fi, part); 3947 if (ai) 3948 c.var = ai->id; 3949 else 3950 c.var = anything_id; 3951 c.offset = 0; 3952 c.type = SCALAR; 3953 } 3954 else 3955 { 3956 c.var = fi->id; 3957 c.offset = part; 3958 c.type = DEREF; 3959 } 3960 3961 return c; 3962 } 3963 3964 /* For non-IPA mode, generate constraints necessary for a call on the 3965 RHS. */ 3966 3967 static void 3968 handle_rhs_call (gcall *stmt, vec<ce_s> *results) 3969 { 3970 struct constraint_expr rhsc; 3971 unsigned i; 3972 bool returns_uses = false; 3973 3974 for (i = 0; i < gimple_call_num_args (stmt); ++i) 3975 { 3976 tree arg = gimple_call_arg (stmt, i); 3977 int flags = gimple_call_arg_flags (stmt, i); 3978 3979 /* If the argument is not used we can ignore it. */ 3980 if (flags & EAF_UNUSED) 3981 continue; 3982 3983 /* As we compute ESCAPED context-insensitive we do not gain 3984 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE 3985 set. The argument would still get clobbered through the 3986 escape solution. */ 3987 if ((flags & EAF_NOCLOBBER) 3988 && (flags & EAF_NOESCAPE)) 3989 { 3990 varinfo_t uses = get_call_use_vi (stmt); 3991 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true); 3992 tem->is_reg_var = true; 3993 make_constraint_to (tem->id, arg); 3994 make_any_offset_constraints (tem); 3995 if (!(flags & EAF_DIRECT)) 3996 make_transitive_closure_constraints (tem); 3997 make_copy_constraint (uses, tem->id); 3998 returns_uses = true; 3999 } 4000 else if (flags & EAF_NOESCAPE) 4001 { 4002 struct constraint_expr lhs, rhs; 4003 varinfo_t uses = get_call_use_vi (stmt); 4004 varinfo_t clobbers = get_call_clobber_vi (stmt); 4005 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true); 4006 tem->is_reg_var = true; 4007 make_constraint_to (tem->id, arg); 4008 make_any_offset_constraints (tem); 4009 if (!(flags & EAF_DIRECT)) 4010 make_transitive_closure_constraints (tem); 4011 make_copy_constraint (uses, tem->id); 4012 make_copy_constraint (clobbers, tem->id); 4013 /* Add *tem = nonlocal, do not add *tem = callused as 4014 EAF_NOESCAPE parameters do not escape to other parameters 4015 and all other uses appear in NONLOCAL as well. */ 4016 lhs.type = DEREF; 4017 lhs.var = tem->id; 4018 lhs.offset = 0; 4019 rhs.type = SCALAR; 4020 rhs.var = nonlocal_id; 4021 rhs.offset = 0; 4022 process_constraint (new_constraint (lhs, rhs)); 4023 returns_uses = true; 4024 } 4025 else 4026 make_escape_constraint (arg); 4027 } 4028 4029 /* If we added to the calls uses solution make sure we account for 4030 pointers to it to be returned. */ 4031 if (returns_uses) 4032 { 4033 rhsc.var = get_call_use_vi (stmt)->id; 4034 rhsc.offset = UNKNOWN_OFFSET; 4035 rhsc.type = SCALAR; 4036 results->safe_push (rhsc); 4037 } 4038 4039 /* The static chain escapes as well. */ 4040 if (gimple_call_chain (stmt)) 4041 make_escape_constraint (gimple_call_chain (stmt)); 4042 4043 /* And if we applied NRV the address of the return slot escapes as well. */ 4044 if (gimple_call_return_slot_opt_p (stmt) 4045 && gimple_call_lhs (stmt) != NULL_TREE 4046 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt)))) 4047 { 4048 auto_vec<ce_s> tmpc; 4049 struct constraint_expr lhsc, *c; 4050 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc); 4051 lhsc.var = escaped_id; 4052 lhsc.offset = 0; 4053 lhsc.type = SCALAR; 4054 FOR_EACH_VEC_ELT (tmpc, i, c) 4055 process_constraint (new_constraint (lhsc, *c)); 4056 } 4057 4058 /* Regular functions return nonlocal memory. */ 4059 rhsc.var = nonlocal_id; 4060 rhsc.offset = 0; 4061 rhsc.type = SCALAR; 4062 results->safe_push (rhsc); 4063 } 4064 4065 /* For non-IPA mode, generate constraints necessary for a call 4066 that returns a pointer and assigns it to LHS. This simply makes 4067 the LHS point to global and escaped variables. */ 4068 4069 static void 4070 handle_lhs_call (gcall *stmt, tree lhs, int flags, vec<ce_s> rhsc, 4071 tree fndecl) 4072 { 4073 auto_vec<ce_s> lhsc; 4074 4075 get_constraint_for (lhs, &lhsc); 4076 /* If the store is to a global decl make sure to 4077 add proper escape constraints. */ 4078 lhs = get_base_address (lhs); 4079 if (lhs 4080 && DECL_P (lhs) 4081 && is_global_var (lhs)) 4082 { 4083 struct constraint_expr tmpc; 4084 tmpc.var = escaped_id; 4085 tmpc.offset = 0; 4086 tmpc.type = SCALAR; 4087 lhsc.safe_push (tmpc); 4088 } 4089 4090 /* If the call returns an argument unmodified override the rhs 4091 constraints. */ 4092 if (flags & ERF_RETURNS_ARG 4093 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt)) 4094 { 4095 tree arg; 4096 rhsc.create (0); 4097 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK); 4098 get_constraint_for (arg, &rhsc); 4099 process_all_all_constraints (lhsc, rhsc); 4100 rhsc.release (); 4101 } 4102 else if (flags & ERF_NOALIAS) 4103 { 4104 varinfo_t vi; 4105 struct constraint_expr tmpc; 4106 rhsc.create (0); 4107 vi = make_heapvar ("HEAP", true); 4108 /* We are marking allocated storage local, we deal with it becoming 4109 global by escaping and setting of vars_contains_escaped_heap. */ 4110 DECL_EXTERNAL (vi->decl) = 0; 4111 vi->is_global_var = 0; 4112 /* If this is not a real malloc call assume the memory was 4113 initialized and thus may point to global memory. All 4114 builtin functions with the malloc attribute behave in a sane way. */ 4115 if (!fndecl 4116 || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) 4117 make_constraint_from (vi, nonlocal_id); 4118 tmpc.var = vi->id; 4119 tmpc.offset = 0; 4120 tmpc.type = ADDRESSOF; 4121 rhsc.safe_push (tmpc); 4122 process_all_all_constraints (lhsc, rhsc); 4123 rhsc.release (); 4124 } 4125 else 4126 process_all_all_constraints (lhsc, rhsc); 4127 } 4128 4129 /* For non-IPA mode, generate constraints necessary for a call of a 4130 const function that returns a pointer in the statement STMT. */ 4131 4132 static void 4133 handle_const_call (gcall *stmt, vec<ce_s> *results) 4134 { 4135 struct constraint_expr rhsc; 4136 unsigned int k; 4137 bool need_uses = false; 4138 4139 /* Treat nested const functions the same as pure functions as far 4140 as the static chain is concerned. */ 4141 if (gimple_call_chain (stmt)) 4142 { 4143 varinfo_t uses = get_call_use_vi (stmt); 4144 make_constraint_to (uses->id, gimple_call_chain (stmt)); 4145 need_uses = true; 4146 } 4147 4148 /* And if we applied NRV the address of the return slot escapes as well. */ 4149 if (gimple_call_return_slot_opt_p (stmt) 4150 && gimple_call_lhs (stmt) != NULL_TREE 4151 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt)))) 4152 { 4153 varinfo_t uses = get_call_use_vi (stmt); 4154 auto_vec<ce_s> tmpc; 4155 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc); 4156 make_constraints_to (uses->id, tmpc); 4157 need_uses = true; 4158 } 4159 4160 if (need_uses) 4161 { 4162 varinfo_t uses = get_call_use_vi (stmt); 4163 make_any_offset_constraints (uses); 4164 make_transitive_closure_constraints (uses); 4165 rhsc.var = uses->id; 4166 rhsc.offset = 0; 4167 rhsc.type = SCALAR; 4168 results->safe_push (rhsc); 4169 } 4170 4171 /* May return offsetted arguments. */ 4172 varinfo_t tem = NULL; 4173 if (gimple_call_num_args (stmt) != 0) 4174 { 4175 tem = new_var_info (NULL_TREE, "callarg", true); 4176 tem->is_reg_var = true; 4177 } 4178 for (k = 0; k < gimple_call_num_args (stmt); ++k) 4179 { 4180 tree arg = gimple_call_arg (stmt, k); 4181 auto_vec<ce_s> argc; 4182 get_constraint_for_rhs (arg, &argc); 4183 make_constraints_to (tem->id, argc); 4184 } 4185 if (tem) 4186 { 4187 ce_s ce; 4188 ce.type = SCALAR; 4189 ce.var = tem->id; 4190 ce.offset = UNKNOWN_OFFSET; 4191 results->safe_push (ce); 4192 } 4193 4194 /* May return addresses of globals. */ 4195 rhsc.var = nonlocal_id; 4196 rhsc.offset = 0; 4197 rhsc.type = ADDRESSOF; 4198 results->safe_push (rhsc); 4199 } 4200 4201 /* For non-IPA mode, generate constraints necessary for a call to a 4202 pure function in statement STMT. */ 4203 4204 static void 4205 handle_pure_call (gcall *stmt, vec<ce_s> *results) 4206 { 4207 struct constraint_expr rhsc; 4208 unsigned i; 4209 varinfo_t uses = NULL; 4210 4211 /* Memory reached from pointer arguments is call-used. */ 4212 for (i = 0; i < gimple_call_num_args (stmt); ++i) 4213 { 4214 tree arg = gimple_call_arg (stmt, i); 4215 if (!uses) 4216 { 4217 uses = get_call_use_vi (stmt); 4218 make_any_offset_constraints (uses); 4219 make_transitive_closure_constraints (uses); 4220 } 4221 make_constraint_to (uses->id, arg); 4222 } 4223 4224 /* The static chain is used as well. */ 4225 if (gimple_call_chain (stmt)) 4226 { 4227 if (!uses) 4228 { 4229 uses = get_call_use_vi (stmt); 4230 make_any_offset_constraints (uses); 4231 make_transitive_closure_constraints (uses); 4232 } 4233 make_constraint_to (uses->id, gimple_call_chain (stmt)); 4234 } 4235 4236 /* And if we applied NRV the address of the return slot. */ 4237 if (gimple_call_return_slot_opt_p (stmt) 4238 && gimple_call_lhs (stmt) != NULL_TREE 4239 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt)))) 4240 { 4241 if (!uses) 4242 { 4243 uses = get_call_use_vi (stmt); 4244 make_any_offset_constraints (uses); 4245 make_transitive_closure_constraints (uses); 4246 } 4247 auto_vec<ce_s> tmpc; 4248 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc); 4249 make_constraints_to (uses->id, tmpc); 4250 } 4251 4252 /* Pure functions may return call-used and nonlocal memory. */ 4253 if (uses) 4254 { 4255 rhsc.var = uses->id; 4256 rhsc.offset = 0; 4257 rhsc.type = SCALAR; 4258 results->safe_push (rhsc); 4259 } 4260 rhsc.var = nonlocal_id; 4261 rhsc.offset = 0; 4262 rhsc.type = SCALAR; 4263 results->safe_push (rhsc); 4264 } 4265 4266 4267 /* Return the varinfo for the callee of CALL. */ 4268 4269 static varinfo_t 4270 get_fi_for_callee (gcall *call) 4271 { 4272 tree decl, fn = gimple_call_fn (call); 4273 4274 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF) 4275 fn = OBJ_TYPE_REF_EXPR (fn); 4276 4277 /* If we can directly resolve the function being called, do so. 4278 Otherwise, it must be some sort of indirect expression that 4279 we should still be able to handle. */ 4280 decl = gimple_call_addr_fndecl (fn); 4281 if (decl) 4282 return get_vi_for_tree (decl); 4283 4284 /* If the function is anything other than a SSA name pointer we have no 4285 clue and should be getting ANYFN (well, ANYTHING for now). */ 4286 if (!fn || TREE_CODE (fn) != SSA_NAME) 4287 return get_varinfo (anything_id); 4288 4289 if (SSA_NAME_IS_DEFAULT_DEF (fn) 4290 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL 4291 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL)) 4292 fn = SSA_NAME_VAR (fn); 4293 4294 return get_vi_for_tree (fn); 4295 } 4296 4297 /* Create constraints for assigning call argument ARG to the incoming parameter 4298 INDEX of function FI. */ 4299 4300 static void 4301 find_func_aliases_for_call_arg (varinfo_t fi, unsigned index, tree arg) 4302 { 4303 struct constraint_expr lhs; 4304 lhs = get_function_part_constraint (fi, fi_parm_base + index); 4305 4306 auto_vec<ce_s, 2> rhsc; 4307 get_constraint_for_rhs (arg, &rhsc); 4308 4309 unsigned j; 4310 struct constraint_expr *rhsp; 4311 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 4312 process_constraint (new_constraint (lhs, *rhsp)); 4313 } 4314 4315 /* Return true if FNDECL may be part of another lto partition. */ 4316 4317 static bool 4318 fndecl_maybe_in_other_partition (tree fndecl) 4319 { 4320 cgraph_node *fn_node = cgraph_node::get (fndecl); 4321 if (fn_node == NULL) 4322 return true; 4323 4324 return fn_node->in_other_partition; 4325 } 4326 4327 /* Create constraints for the builtin call T. Return true if the call 4328 was handled, otherwise false. */ 4329 4330 static bool 4331 find_func_aliases_for_builtin_call (struct function *fn, gcall *t) 4332 { 4333 tree fndecl = gimple_call_fndecl (t); 4334 auto_vec<ce_s, 2> lhsc; 4335 auto_vec<ce_s, 4> rhsc; 4336 varinfo_t fi; 4337 4338 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL)) 4339 /* ??? All builtins that are handled here need to be handled 4340 in the alias-oracle query functions explicitly! */ 4341 switch (DECL_FUNCTION_CODE (fndecl)) 4342 { 4343 /* All the following functions return a pointer to the same object 4344 as their first argument points to. The functions do not add 4345 to the ESCAPED solution. The functions make the first argument 4346 pointed to memory point to what the second argument pointed to 4347 memory points to. */ 4348 case BUILT_IN_STRCPY: 4349 case BUILT_IN_STRNCPY: 4350 case BUILT_IN_BCOPY: 4351 case BUILT_IN_MEMCPY: 4352 case BUILT_IN_MEMMOVE: 4353 case BUILT_IN_MEMPCPY: 4354 case BUILT_IN_STPCPY: 4355 case BUILT_IN_STPNCPY: 4356 case BUILT_IN_STRCAT: 4357 case BUILT_IN_STRNCAT: 4358 case BUILT_IN_STRCPY_CHK: 4359 case BUILT_IN_STRNCPY_CHK: 4360 case BUILT_IN_MEMCPY_CHK: 4361 case BUILT_IN_MEMMOVE_CHK: 4362 case BUILT_IN_MEMPCPY_CHK: 4363 case BUILT_IN_STPCPY_CHK: 4364 case BUILT_IN_STPNCPY_CHK: 4365 case BUILT_IN_STRCAT_CHK: 4366 case BUILT_IN_STRNCAT_CHK: 4367 case BUILT_IN_TM_MEMCPY: 4368 case BUILT_IN_TM_MEMMOVE: 4369 { 4370 tree res = gimple_call_lhs (t); 4371 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl) 4372 == BUILT_IN_BCOPY ? 1 : 0)); 4373 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl) 4374 == BUILT_IN_BCOPY ? 0 : 1)); 4375 if (res != NULL_TREE) 4376 { 4377 get_constraint_for (res, &lhsc); 4378 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY 4379 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY 4380 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY 4381 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK 4382 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK 4383 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK) 4384 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc); 4385 else 4386 get_constraint_for (dest, &rhsc); 4387 process_all_all_constraints (lhsc, rhsc); 4388 lhsc.truncate (0); 4389 rhsc.truncate (0); 4390 } 4391 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); 4392 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); 4393 do_deref (&lhsc); 4394 do_deref (&rhsc); 4395 process_all_all_constraints (lhsc, rhsc); 4396 return true; 4397 } 4398 case BUILT_IN_MEMSET: 4399 case BUILT_IN_MEMSET_CHK: 4400 case BUILT_IN_TM_MEMSET: 4401 { 4402 tree res = gimple_call_lhs (t); 4403 tree dest = gimple_call_arg (t, 0); 4404 unsigned i; 4405 ce_s *lhsp; 4406 struct constraint_expr ac; 4407 if (res != NULL_TREE) 4408 { 4409 get_constraint_for (res, &lhsc); 4410 get_constraint_for (dest, &rhsc); 4411 process_all_all_constraints (lhsc, rhsc); 4412 lhsc.truncate (0); 4413 } 4414 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); 4415 do_deref (&lhsc); 4416 if (flag_delete_null_pointer_checks 4417 && integer_zerop (gimple_call_arg (t, 1))) 4418 { 4419 ac.type = ADDRESSOF; 4420 ac.var = nothing_id; 4421 } 4422 else 4423 { 4424 ac.type = SCALAR; 4425 ac.var = integer_id; 4426 } 4427 ac.offset = 0; 4428 FOR_EACH_VEC_ELT (lhsc, i, lhsp) 4429 process_constraint (new_constraint (*lhsp, ac)); 4430 return true; 4431 } 4432 case BUILT_IN_POSIX_MEMALIGN: 4433 { 4434 tree ptrptr = gimple_call_arg (t, 0); 4435 get_constraint_for (ptrptr, &lhsc); 4436 do_deref (&lhsc); 4437 varinfo_t vi = make_heapvar ("HEAP", true); 4438 /* We are marking allocated storage local, we deal with it becoming 4439 global by escaping and setting of vars_contains_escaped_heap. */ 4440 DECL_EXTERNAL (vi->decl) = 0; 4441 vi->is_global_var = 0; 4442 struct constraint_expr tmpc; 4443 tmpc.var = vi->id; 4444 tmpc.offset = 0; 4445 tmpc.type = ADDRESSOF; 4446 rhsc.safe_push (tmpc); 4447 process_all_all_constraints (lhsc, rhsc); 4448 return true; 4449 } 4450 case BUILT_IN_ASSUME_ALIGNED: 4451 { 4452 tree res = gimple_call_lhs (t); 4453 tree dest = gimple_call_arg (t, 0); 4454 if (res != NULL_TREE) 4455 { 4456 get_constraint_for (res, &lhsc); 4457 get_constraint_for (dest, &rhsc); 4458 process_all_all_constraints (lhsc, rhsc); 4459 } 4460 return true; 4461 } 4462 /* All the following functions do not return pointers, do not 4463 modify the points-to sets of memory reachable from their 4464 arguments and do not add to the ESCAPED solution. */ 4465 case BUILT_IN_SINCOS: 4466 case BUILT_IN_SINCOSF: 4467 case BUILT_IN_SINCOSL: 4468 case BUILT_IN_FREXP: 4469 case BUILT_IN_FREXPF: 4470 case BUILT_IN_FREXPL: 4471 case BUILT_IN_GAMMA_R: 4472 case BUILT_IN_GAMMAF_R: 4473 case BUILT_IN_GAMMAL_R: 4474 case BUILT_IN_LGAMMA_R: 4475 case BUILT_IN_LGAMMAF_R: 4476 case BUILT_IN_LGAMMAL_R: 4477 case BUILT_IN_MODF: 4478 case BUILT_IN_MODFF: 4479 case BUILT_IN_MODFL: 4480 case BUILT_IN_REMQUO: 4481 case BUILT_IN_REMQUOF: 4482 case BUILT_IN_REMQUOL: 4483 case BUILT_IN_FREE: 4484 return true; 4485 case BUILT_IN_STRDUP: 4486 case BUILT_IN_STRNDUP: 4487 case BUILT_IN_REALLOC: 4488 if (gimple_call_lhs (t)) 4489 { 4490 handle_lhs_call (t, gimple_call_lhs (t), 4491 gimple_call_return_flags (t) | ERF_NOALIAS, 4492 vNULL, fndecl); 4493 get_constraint_for_ptr_offset (gimple_call_lhs (t), 4494 NULL_TREE, &lhsc); 4495 get_constraint_for_ptr_offset (gimple_call_arg (t, 0), 4496 NULL_TREE, &rhsc); 4497 do_deref (&lhsc); 4498 do_deref (&rhsc); 4499 process_all_all_constraints (lhsc, rhsc); 4500 lhsc.truncate (0); 4501 rhsc.truncate (0); 4502 /* For realloc the resulting pointer can be equal to the 4503 argument as well. But only doing this wouldn't be 4504 correct because with ptr == 0 realloc behaves like malloc. */ 4505 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC) 4506 { 4507 get_constraint_for (gimple_call_lhs (t), &lhsc); 4508 get_constraint_for (gimple_call_arg (t, 0), &rhsc); 4509 process_all_all_constraints (lhsc, rhsc); 4510 } 4511 return true; 4512 } 4513 break; 4514 /* String / character search functions return a pointer into the 4515 source string or NULL. */ 4516 case BUILT_IN_INDEX: 4517 case BUILT_IN_STRCHR: 4518 case BUILT_IN_STRRCHR: 4519 case BUILT_IN_MEMCHR: 4520 case BUILT_IN_STRSTR: 4521 case BUILT_IN_STRPBRK: 4522 if (gimple_call_lhs (t)) 4523 { 4524 tree src = gimple_call_arg (t, 0); 4525 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); 4526 constraint_expr nul; 4527 nul.var = nothing_id; 4528 nul.offset = 0; 4529 nul.type = ADDRESSOF; 4530 rhsc.safe_push (nul); 4531 get_constraint_for (gimple_call_lhs (t), &lhsc); 4532 process_all_all_constraints (lhsc, rhsc); 4533 } 4534 return true; 4535 /* Pure functions that return something not based on any object and 4536 that use the memory pointed to by their arguments (but not 4537 transitively). */ 4538 case BUILT_IN_STRCMP: 4539 case BUILT_IN_STRCMP_EQ: 4540 case BUILT_IN_STRNCMP: 4541 case BUILT_IN_STRNCMP_EQ: 4542 case BUILT_IN_STRCASECMP: 4543 case BUILT_IN_STRNCASECMP: 4544 case BUILT_IN_MEMCMP: 4545 case BUILT_IN_BCMP: 4546 case BUILT_IN_STRSPN: 4547 case BUILT_IN_STRCSPN: 4548 { 4549 varinfo_t uses = get_call_use_vi (t); 4550 make_any_offset_constraints (uses); 4551 make_constraint_to (uses->id, gimple_call_arg (t, 0)); 4552 make_constraint_to (uses->id, gimple_call_arg (t, 1)); 4553 /* No constraints are necessary for the return value. */ 4554 return true; 4555 } 4556 case BUILT_IN_STRLEN: 4557 { 4558 varinfo_t uses = get_call_use_vi (t); 4559 make_any_offset_constraints (uses); 4560 make_constraint_to (uses->id, gimple_call_arg (t, 0)); 4561 /* No constraints are necessary for the return value. */ 4562 return true; 4563 } 4564 case BUILT_IN_OBJECT_SIZE: 4565 case BUILT_IN_CONSTANT_P: 4566 { 4567 /* No constraints are necessary for the return value or the 4568 arguments. */ 4569 return true; 4570 } 4571 /* Trampolines are special - they set up passing the static 4572 frame. */ 4573 case BUILT_IN_INIT_TRAMPOLINE: 4574 { 4575 tree tramp = gimple_call_arg (t, 0); 4576 tree nfunc = gimple_call_arg (t, 1); 4577 tree frame = gimple_call_arg (t, 2); 4578 unsigned i; 4579 struct constraint_expr lhs, *rhsp; 4580 if (in_ipa_mode) 4581 { 4582 varinfo_t nfi = NULL; 4583 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR); 4584 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0)); 4585 if (nfi) 4586 { 4587 lhs = get_function_part_constraint (nfi, fi_static_chain); 4588 get_constraint_for (frame, &rhsc); 4589 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 4590 process_constraint (new_constraint (lhs, *rhsp)); 4591 rhsc.truncate (0); 4592 4593 /* Make the frame point to the function for 4594 the trampoline adjustment call. */ 4595 get_constraint_for (tramp, &lhsc); 4596 do_deref (&lhsc); 4597 get_constraint_for (nfunc, &rhsc); 4598 process_all_all_constraints (lhsc, rhsc); 4599 4600 return true; 4601 } 4602 } 4603 /* Else fallthru to generic handling which will let 4604 the frame escape. */ 4605 break; 4606 } 4607 case BUILT_IN_ADJUST_TRAMPOLINE: 4608 { 4609 tree tramp = gimple_call_arg (t, 0); 4610 tree res = gimple_call_lhs (t); 4611 if (in_ipa_mode && res) 4612 { 4613 get_constraint_for (res, &lhsc); 4614 get_constraint_for (tramp, &rhsc); 4615 do_deref (&rhsc); 4616 process_all_all_constraints (lhsc, rhsc); 4617 } 4618 return true; 4619 } 4620 CASE_BUILT_IN_TM_STORE (1): 4621 CASE_BUILT_IN_TM_STORE (2): 4622 CASE_BUILT_IN_TM_STORE (4): 4623 CASE_BUILT_IN_TM_STORE (8): 4624 CASE_BUILT_IN_TM_STORE (FLOAT): 4625 CASE_BUILT_IN_TM_STORE (DOUBLE): 4626 CASE_BUILT_IN_TM_STORE (LDOUBLE): 4627 CASE_BUILT_IN_TM_STORE (M64): 4628 CASE_BUILT_IN_TM_STORE (M128): 4629 CASE_BUILT_IN_TM_STORE (M256): 4630 { 4631 tree addr = gimple_call_arg (t, 0); 4632 tree src = gimple_call_arg (t, 1); 4633 4634 get_constraint_for (addr, &lhsc); 4635 do_deref (&lhsc); 4636 get_constraint_for (src, &rhsc); 4637 process_all_all_constraints (lhsc, rhsc); 4638 return true; 4639 } 4640 CASE_BUILT_IN_TM_LOAD (1): 4641 CASE_BUILT_IN_TM_LOAD (2): 4642 CASE_BUILT_IN_TM_LOAD (4): 4643 CASE_BUILT_IN_TM_LOAD (8): 4644 CASE_BUILT_IN_TM_LOAD (FLOAT): 4645 CASE_BUILT_IN_TM_LOAD (DOUBLE): 4646 CASE_BUILT_IN_TM_LOAD (LDOUBLE): 4647 CASE_BUILT_IN_TM_LOAD (M64): 4648 CASE_BUILT_IN_TM_LOAD (M128): 4649 CASE_BUILT_IN_TM_LOAD (M256): 4650 { 4651 tree dest = gimple_call_lhs (t); 4652 tree addr = gimple_call_arg (t, 0); 4653 4654 get_constraint_for (dest, &lhsc); 4655 get_constraint_for (addr, &rhsc); 4656 do_deref (&rhsc); 4657 process_all_all_constraints (lhsc, rhsc); 4658 return true; 4659 } 4660 /* Variadic argument handling needs to be handled in IPA 4661 mode as well. */ 4662 case BUILT_IN_VA_START: 4663 { 4664 tree valist = gimple_call_arg (t, 0); 4665 struct constraint_expr rhs, *lhsp; 4666 unsigned i; 4667 get_constraint_for_ptr_offset (valist, NULL_TREE, &lhsc); 4668 do_deref (&lhsc); 4669 /* The va_list gets access to pointers in variadic 4670 arguments. Which we know in the case of IPA analysis 4671 and otherwise are just all nonlocal variables. */ 4672 if (in_ipa_mode) 4673 { 4674 fi = lookup_vi_for_tree (fn->decl); 4675 rhs = get_function_part_constraint (fi, ~0); 4676 rhs.type = ADDRESSOF; 4677 } 4678 else 4679 { 4680 rhs.var = nonlocal_id; 4681 rhs.type = ADDRESSOF; 4682 rhs.offset = 0; 4683 } 4684 FOR_EACH_VEC_ELT (lhsc, i, lhsp) 4685 process_constraint (new_constraint (*lhsp, rhs)); 4686 /* va_list is clobbered. */ 4687 make_constraint_to (get_call_clobber_vi (t)->id, valist); 4688 return true; 4689 } 4690 /* va_end doesn't have any effect that matters. */ 4691 case BUILT_IN_VA_END: 4692 return true; 4693 /* Alternate return. Simply give up for now. */ 4694 case BUILT_IN_RETURN: 4695 { 4696 fi = NULL; 4697 if (!in_ipa_mode 4698 || !(fi = get_vi_for_tree (fn->decl))) 4699 make_constraint_from (get_varinfo (escaped_id), anything_id); 4700 else if (in_ipa_mode 4701 && fi != NULL) 4702 { 4703 struct constraint_expr lhs, rhs; 4704 lhs = get_function_part_constraint (fi, fi_result); 4705 rhs.var = anything_id; 4706 rhs.offset = 0; 4707 rhs.type = SCALAR; 4708 process_constraint (new_constraint (lhs, rhs)); 4709 } 4710 return true; 4711 } 4712 case BUILT_IN_GOMP_PARALLEL: 4713 case BUILT_IN_GOACC_PARALLEL: 4714 { 4715 if (in_ipa_mode) 4716 { 4717 unsigned int fnpos, argpos; 4718 switch (DECL_FUNCTION_CODE (fndecl)) 4719 { 4720 case BUILT_IN_GOMP_PARALLEL: 4721 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */ 4722 fnpos = 0; 4723 argpos = 1; 4724 break; 4725 case BUILT_IN_GOACC_PARALLEL: 4726 /* __builtin_GOACC_parallel (flags_m, fn, mapnum, hostaddrs, 4727 sizes, kinds, ...). */ 4728 fnpos = 1; 4729 argpos = 3; 4730 break; 4731 default: 4732 gcc_unreachable (); 4733 } 4734 4735 tree fnarg = gimple_call_arg (t, fnpos); 4736 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR); 4737 tree fndecl = TREE_OPERAND (fnarg, 0); 4738 if (fndecl_maybe_in_other_partition (fndecl)) 4739 /* Fallthru to general call handling. */ 4740 break; 4741 4742 tree arg = gimple_call_arg (t, argpos); 4743 4744 varinfo_t fi = get_vi_for_tree (fndecl); 4745 find_func_aliases_for_call_arg (fi, 0, arg); 4746 return true; 4747 } 4748 /* Else fallthru to generic call handling. */ 4749 break; 4750 } 4751 /* printf-style functions may have hooks to set pointers to 4752 point to somewhere into the generated string. Leave them 4753 for a later exercise... */ 4754 default: 4755 /* Fallthru to general call handling. */; 4756 } 4757 4758 return false; 4759 } 4760 4761 /* Create constraints for the call T. */ 4762 4763 static void 4764 find_func_aliases_for_call (struct function *fn, gcall *t) 4765 { 4766 tree fndecl = gimple_call_fndecl (t); 4767 varinfo_t fi; 4768 4769 if (fndecl != NULL_TREE 4770 && fndecl_built_in_p (fndecl) 4771 && find_func_aliases_for_builtin_call (fn, t)) 4772 return; 4773 4774 fi = get_fi_for_callee (t); 4775 if (!in_ipa_mode 4776 || (fi->decl && fndecl && !fi->is_fn_info)) 4777 { 4778 auto_vec<ce_s, 16> rhsc; 4779 int flags = gimple_call_flags (t); 4780 4781 /* Const functions can return their arguments and addresses 4782 of global memory but not of escaped memory. */ 4783 if (flags & (ECF_CONST|ECF_NOVOPS)) 4784 { 4785 if (gimple_call_lhs (t)) 4786 handle_const_call (t, &rhsc); 4787 } 4788 /* Pure functions can return addresses in and of memory 4789 reachable from their arguments, but they are not an escape 4790 point for reachable memory of their arguments. */ 4791 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE)) 4792 handle_pure_call (t, &rhsc); 4793 else 4794 handle_rhs_call (t, &rhsc); 4795 if (gimple_call_lhs (t)) 4796 handle_lhs_call (t, gimple_call_lhs (t), 4797 gimple_call_return_flags (t), rhsc, fndecl); 4798 } 4799 else 4800 { 4801 auto_vec<ce_s, 2> rhsc; 4802 tree lhsop; 4803 unsigned j; 4804 4805 /* Assign all the passed arguments to the appropriate incoming 4806 parameters of the function. */ 4807 for (j = 0; j < gimple_call_num_args (t); j++) 4808 { 4809 tree arg = gimple_call_arg (t, j); 4810 find_func_aliases_for_call_arg (fi, j, arg); 4811 } 4812 4813 /* If we are returning a value, assign it to the result. */ 4814 lhsop = gimple_call_lhs (t); 4815 if (lhsop) 4816 { 4817 auto_vec<ce_s, 2> lhsc; 4818 struct constraint_expr rhs; 4819 struct constraint_expr *lhsp; 4820 bool aggr_p = aggregate_value_p (lhsop, gimple_call_fntype (t)); 4821 4822 get_constraint_for (lhsop, &lhsc); 4823 rhs = get_function_part_constraint (fi, fi_result); 4824 if (aggr_p) 4825 { 4826 auto_vec<ce_s, 2> tem; 4827 tem.quick_push (rhs); 4828 do_deref (&tem); 4829 gcc_checking_assert (tem.length () == 1); 4830 rhs = tem[0]; 4831 } 4832 FOR_EACH_VEC_ELT (lhsc, j, lhsp) 4833 process_constraint (new_constraint (*lhsp, rhs)); 4834 4835 /* If we pass the result decl by reference, honor that. */ 4836 if (aggr_p) 4837 { 4838 struct constraint_expr lhs; 4839 struct constraint_expr *rhsp; 4840 4841 get_constraint_for_address_of (lhsop, &rhsc); 4842 lhs = get_function_part_constraint (fi, fi_result); 4843 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 4844 process_constraint (new_constraint (lhs, *rhsp)); 4845 rhsc.truncate (0); 4846 } 4847 } 4848 4849 /* If we use a static chain, pass it along. */ 4850 if (gimple_call_chain (t)) 4851 { 4852 struct constraint_expr lhs; 4853 struct constraint_expr *rhsp; 4854 4855 get_constraint_for (gimple_call_chain (t), &rhsc); 4856 lhs = get_function_part_constraint (fi, fi_static_chain); 4857 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 4858 process_constraint (new_constraint (lhs, *rhsp)); 4859 } 4860 } 4861 } 4862 4863 /* Walk statement T setting up aliasing constraints according to the 4864 references found in T. This function is the main part of the 4865 constraint builder. AI points to auxiliary alias information used 4866 when building alias sets and computing alias grouping heuristics. */ 4867 4868 static void 4869 find_func_aliases (struct function *fn, gimple *origt) 4870 { 4871 gimple *t = origt; 4872 auto_vec<ce_s, 16> lhsc; 4873 auto_vec<ce_s, 16> rhsc; 4874 varinfo_t fi; 4875 4876 /* Now build constraints expressions. */ 4877 if (gimple_code (t) == GIMPLE_PHI) 4878 { 4879 /* For a phi node, assign all the arguments to 4880 the result. */ 4881 get_constraint_for (gimple_phi_result (t), &lhsc); 4882 for (unsigned i = 0; i < gimple_phi_num_args (t); i++) 4883 { 4884 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc); 4885 process_all_all_constraints (lhsc, rhsc); 4886 rhsc.truncate (0); 4887 } 4888 } 4889 /* In IPA mode, we need to generate constraints to pass call 4890 arguments through their calls. There are two cases, 4891 either a GIMPLE_CALL returning a value, or just a plain 4892 GIMPLE_CALL when we are not. 4893 4894 In non-ipa mode, we need to generate constraints for each 4895 pointer passed by address. */ 4896 else if (is_gimple_call (t)) 4897 find_func_aliases_for_call (fn, as_a <gcall *> (t)); 4898 4899 /* Otherwise, just a regular assignment statement. Only care about 4900 operations with pointer result, others are dealt with as escape 4901 points if they have pointer operands. */ 4902 else if (is_gimple_assign (t)) 4903 { 4904 /* Otherwise, just a regular assignment statement. */ 4905 tree lhsop = gimple_assign_lhs (t); 4906 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL; 4907 4908 if (rhsop && TREE_CLOBBER_P (rhsop)) 4909 /* Ignore clobbers, they don't actually store anything into 4910 the LHS. */ 4911 ; 4912 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop))) 4913 do_structure_copy (lhsop, rhsop); 4914 else 4915 { 4916 enum tree_code code = gimple_assign_rhs_code (t); 4917 4918 get_constraint_for (lhsop, &lhsc); 4919 4920 if (code == POINTER_PLUS_EXPR) 4921 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), 4922 gimple_assign_rhs2 (t), &rhsc); 4923 else if (code == BIT_AND_EXPR 4924 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST) 4925 { 4926 /* Aligning a pointer via a BIT_AND_EXPR is offsetting 4927 the pointer. Handle it by offsetting it by UNKNOWN. */ 4928 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), 4929 NULL_TREE, &rhsc); 4930 } 4931 else if (CONVERT_EXPR_CODE_P (code) 4932 || gimple_assign_single_p (t)) 4933 /* See through conversions, single RHS are handled by 4934 get_constraint_for_rhs. */ 4935 get_constraint_for_rhs (rhsop, &rhsc); 4936 else if (code == COND_EXPR) 4937 { 4938 /* The result is a merge of both COND_EXPR arms. */ 4939 auto_vec<ce_s, 2> tmp; 4940 struct constraint_expr *rhsp; 4941 unsigned i; 4942 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc); 4943 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp); 4944 FOR_EACH_VEC_ELT (tmp, i, rhsp) 4945 rhsc.safe_push (*rhsp); 4946 } 4947 else if (truth_value_p (code)) 4948 /* Truth value results are not pointer (parts). Or at least 4949 very unreasonable obfuscation of a part. */ 4950 ; 4951 else 4952 { 4953 /* All other operations are possibly offsetting merges. */ 4954 auto_vec<ce_s, 4> tmp; 4955 struct constraint_expr *rhsp; 4956 unsigned i, j; 4957 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), 4958 NULL_TREE, &rhsc); 4959 for (i = 2; i < gimple_num_ops (t); ++i) 4960 { 4961 get_constraint_for_ptr_offset (gimple_op (t, i), 4962 NULL_TREE, &tmp); 4963 FOR_EACH_VEC_ELT (tmp, j, rhsp) 4964 rhsc.safe_push (*rhsp); 4965 tmp.truncate (0); 4966 } 4967 } 4968 process_all_all_constraints (lhsc, rhsc); 4969 } 4970 /* If there is a store to a global variable the rhs escapes. */ 4971 if ((lhsop = get_base_address (lhsop)) != NULL_TREE 4972 && DECL_P (lhsop)) 4973 { 4974 varinfo_t vi = get_vi_for_tree (lhsop); 4975 if ((! in_ipa_mode && vi->is_global_var) 4976 || vi->is_ipa_escape_point) 4977 make_escape_constraint (rhsop); 4978 } 4979 } 4980 /* Handle escapes through return. */ 4981 else if (gimple_code (t) == GIMPLE_RETURN 4982 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE) 4983 { 4984 greturn *return_stmt = as_a <greturn *> (t); 4985 fi = NULL; 4986 if (!in_ipa_mode 4987 || !(fi = get_vi_for_tree (fn->decl))) 4988 make_escape_constraint (gimple_return_retval (return_stmt)); 4989 else if (in_ipa_mode) 4990 { 4991 struct constraint_expr lhs ; 4992 struct constraint_expr *rhsp; 4993 unsigned i; 4994 4995 lhs = get_function_part_constraint (fi, fi_result); 4996 get_constraint_for_rhs (gimple_return_retval (return_stmt), &rhsc); 4997 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 4998 process_constraint (new_constraint (lhs, *rhsp)); 4999 } 5000 } 5001 /* Handle asms conservatively by adding escape constraints to everything. */ 5002 else if (gasm *asm_stmt = dyn_cast <gasm *> (t)) 5003 { 5004 unsigned i, noutputs; 5005 const char **oconstraints; 5006 const char *constraint; 5007 bool allows_mem, allows_reg, is_inout; 5008 5009 noutputs = gimple_asm_noutputs (asm_stmt); 5010 oconstraints = XALLOCAVEC (const char *, noutputs); 5011 5012 for (i = 0; i < noutputs; ++i) 5013 { 5014 tree link = gimple_asm_output_op (asm_stmt, i); 5015 tree op = TREE_VALUE (link); 5016 5017 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); 5018 oconstraints[i] = constraint; 5019 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, 5020 &allows_reg, &is_inout); 5021 5022 /* A memory constraint makes the address of the operand escape. */ 5023 if (!allows_reg && allows_mem) 5024 make_escape_constraint (build_fold_addr_expr (op)); 5025 5026 /* The asm may read global memory, so outputs may point to 5027 any global memory. */ 5028 if (op) 5029 { 5030 auto_vec<ce_s, 2> lhsc; 5031 struct constraint_expr rhsc, *lhsp; 5032 unsigned j; 5033 get_constraint_for (op, &lhsc); 5034 rhsc.var = nonlocal_id; 5035 rhsc.offset = 0; 5036 rhsc.type = SCALAR; 5037 FOR_EACH_VEC_ELT (lhsc, j, lhsp) 5038 process_constraint (new_constraint (*lhsp, rhsc)); 5039 } 5040 } 5041 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i) 5042 { 5043 tree link = gimple_asm_input_op (asm_stmt, i); 5044 tree op = TREE_VALUE (link); 5045 5046 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); 5047 5048 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints, 5049 &allows_mem, &allows_reg); 5050 5051 /* A memory constraint makes the address of the operand escape. */ 5052 if (!allows_reg && allows_mem) 5053 make_escape_constraint (build_fold_addr_expr (op)); 5054 /* Strictly we'd only need the constraint to ESCAPED if 5055 the asm clobbers memory, otherwise using something 5056 along the lines of per-call clobbers/uses would be enough. */ 5057 else if (op) 5058 make_escape_constraint (op); 5059 } 5060 } 5061 } 5062 5063 5064 /* Create a constraint adding to the clobber set of FI the memory 5065 pointed to by PTR. */ 5066 5067 static void 5068 process_ipa_clobber (varinfo_t fi, tree ptr) 5069 { 5070 vec<ce_s> ptrc = vNULL; 5071 struct constraint_expr *c, lhs; 5072 unsigned i; 5073 get_constraint_for_rhs (ptr, &ptrc); 5074 lhs = get_function_part_constraint (fi, fi_clobbers); 5075 FOR_EACH_VEC_ELT (ptrc, i, c) 5076 process_constraint (new_constraint (lhs, *c)); 5077 ptrc.release (); 5078 } 5079 5080 /* Walk statement T setting up clobber and use constraints according to the 5081 references found in T. This function is a main part of the 5082 IPA constraint builder. */ 5083 5084 static void 5085 find_func_clobbers (struct function *fn, gimple *origt) 5086 { 5087 gimple *t = origt; 5088 auto_vec<ce_s, 16> lhsc; 5089 auto_vec<ce_s, 16> rhsc; 5090 varinfo_t fi; 5091 5092 /* Add constraints for clobbered/used in IPA mode. 5093 We are not interested in what automatic variables are clobbered 5094 or used as we only use the information in the caller to which 5095 they do not escape. */ 5096 gcc_assert (in_ipa_mode); 5097 5098 /* If the stmt refers to memory in any way it better had a VUSE. */ 5099 if (gimple_vuse (t) == NULL_TREE) 5100 return; 5101 5102 /* We'd better have function information for the current function. */ 5103 fi = lookup_vi_for_tree (fn->decl); 5104 gcc_assert (fi != NULL); 5105 5106 /* Account for stores in assignments and calls. */ 5107 if (gimple_vdef (t) != NULL_TREE 5108 && gimple_has_lhs (t)) 5109 { 5110 tree lhs = gimple_get_lhs (t); 5111 tree tem = lhs; 5112 while (handled_component_p (tem)) 5113 tem = TREE_OPERAND (tem, 0); 5114 if ((DECL_P (tem) 5115 && !auto_var_in_fn_p (tem, fn->decl)) 5116 || INDIRECT_REF_P (tem) 5117 || (TREE_CODE (tem) == MEM_REF 5118 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR 5119 && auto_var_in_fn_p 5120 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl)))) 5121 { 5122 struct constraint_expr lhsc, *rhsp; 5123 unsigned i; 5124 lhsc = get_function_part_constraint (fi, fi_clobbers); 5125 get_constraint_for_address_of (lhs, &rhsc); 5126 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 5127 process_constraint (new_constraint (lhsc, *rhsp)); 5128 rhsc.truncate (0); 5129 } 5130 } 5131 5132 /* Account for uses in assigments and returns. */ 5133 if (gimple_assign_single_p (t) 5134 || (gimple_code (t) == GIMPLE_RETURN 5135 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE)) 5136 { 5137 tree rhs = (gimple_assign_single_p (t) 5138 ? gimple_assign_rhs1 (t) 5139 : gimple_return_retval (as_a <greturn *> (t))); 5140 tree tem = rhs; 5141 while (handled_component_p (tem)) 5142 tem = TREE_OPERAND (tem, 0); 5143 if ((DECL_P (tem) 5144 && !auto_var_in_fn_p (tem, fn->decl)) 5145 || INDIRECT_REF_P (tem) 5146 || (TREE_CODE (tem) == MEM_REF 5147 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR 5148 && auto_var_in_fn_p 5149 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl)))) 5150 { 5151 struct constraint_expr lhs, *rhsp; 5152 unsigned i; 5153 lhs = get_function_part_constraint (fi, fi_uses); 5154 get_constraint_for_address_of (rhs, &rhsc); 5155 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 5156 process_constraint (new_constraint (lhs, *rhsp)); 5157 rhsc.truncate (0); 5158 } 5159 } 5160 5161 if (gcall *call_stmt = dyn_cast <gcall *> (t)) 5162 { 5163 varinfo_t cfi = NULL; 5164 tree decl = gimple_call_fndecl (t); 5165 struct constraint_expr lhs, rhs; 5166 unsigned i, j; 5167 5168 /* For builtins we do not have separate function info. For those 5169 we do not generate escapes for we have to generate clobbers/uses. */ 5170 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL)) 5171 switch (DECL_FUNCTION_CODE (decl)) 5172 { 5173 /* The following functions use and clobber memory pointed to 5174 by their arguments. */ 5175 case BUILT_IN_STRCPY: 5176 case BUILT_IN_STRNCPY: 5177 case BUILT_IN_BCOPY: 5178 case BUILT_IN_MEMCPY: 5179 case BUILT_IN_MEMMOVE: 5180 case BUILT_IN_MEMPCPY: 5181 case BUILT_IN_STPCPY: 5182 case BUILT_IN_STPNCPY: 5183 case BUILT_IN_STRCAT: 5184 case BUILT_IN_STRNCAT: 5185 case BUILT_IN_STRCPY_CHK: 5186 case BUILT_IN_STRNCPY_CHK: 5187 case BUILT_IN_MEMCPY_CHK: 5188 case BUILT_IN_MEMMOVE_CHK: 5189 case BUILT_IN_MEMPCPY_CHK: 5190 case BUILT_IN_STPCPY_CHK: 5191 case BUILT_IN_STPNCPY_CHK: 5192 case BUILT_IN_STRCAT_CHK: 5193 case BUILT_IN_STRNCAT_CHK: 5194 { 5195 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl) 5196 == BUILT_IN_BCOPY ? 1 : 0)); 5197 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl) 5198 == BUILT_IN_BCOPY ? 0 : 1)); 5199 unsigned i; 5200 struct constraint_expr *rhsp, *lhsp; 5201 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); 5202 lhs = get_function_part_constraint (fi, fi_clobbers); 5203 FOR_EACH_VEC_ELT (lhsc, i, lhsp) 5204 process_constraint (new_constraint (lhs, *lhsp)); 5205 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); 5206 lhs = get_function_part_constraint (fi, fi_uses); 5207 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 5208 process_constraint (new_constraint (lhs, *rhsp)); 5209 return; 5210 } 5211 /* The following function clobbers memory pointed to by 5212 its argument. */ 5213 case BUILT_IN_MEMSET: 5214 case BUILT_IN_MEMSET_CHK: 5215 case BUILT_IN_POSIX_MEMALIGN: 5216 { 5217 tree dest = gimple_call_arg (t, 0); 5218 unsigned i; 5219 ce_s *lhsp; 5220 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); 5221 lhs = get_function_part_constraint (fi, fi_clobbers); 5222 FOR_EACH_VEC_ELT (lhsc, i, lhsp) 5223 process_constraint (new_constraint (lhs, *lhsp)); 5224 return; 5225 } 5226 /* The following functions clobber their second and third 5227 arguments. */ 5228 case BUILT_IN_SINCOS: 5229 case BUILT_IN_SINCOSF: 5230 case BUILT_IN_SINCOSL: 5231 { 5232 process_ipa_clobber (fi, gimple_call_arg (t, 1)); 5233 process_ipa_clobber (fi, gimple_call_arg (t, 2)); 5234 return; 5235 } 5236 /* The following functions clobber their second argument. */ 5237 case BUILT_IN_FREXP: 5238 case BUILT_IN_FREXPF: 5239 case BUILT_IN_FREXPL: 5240 case BUILT_IN_LGAMMA_R: 5241 case BUILT_IN_LGAMMAF_R: 5242 case BUILT_IN_LGAMMAL_R: 5243 case BUILT_IN_GAMMA_R: 5244 case BUILT_IN_GAMMAF_R: 5245 case BUILT_IN_GAMMAL_R: 5246 case BUILT_IN_MODF: 5247 case BUILT_IN_MODFF: 5248 case BUILT_IN_MODFL: 5249 { 5250 process_ipa_clobber (fi, gimple_call_arg (t, 1)); 5251 return; 5252 } 5253 /* The following functions clobber their third argument. */ 5254 case BUILT_IN_REMQUO: 5255 case BUILT_IN_REMQUOF: 5256 case BUILT_IN_REMQUOL: 5257 { 5258 process_ipa_clobber (fi, gimple_call_arg (t, 2)); 5259 return; 5260 } 5261 /* The following functions neither read nor clobber memory. */ 5262 case BUILT_IN_ASSUME_ALIGNED: 5263 case BUILT_IN_FREE: 5264 return; 5265 /* Trampolines are of no interest to us. */ 5266 case BUILT_IN_INIT_TRAMPOLINE: 5267 case BUILT_IN_ADJUST_TRAMPOLINE: 5268 return; 5269 case BUILT_IN_VA_START: 5270 case BUILT_IN_VA_END: 5271 return; 5272 case BUILT_IN_GOMP_PARALLEL: 5273 case BUILT_IN_GOACC_PARALLEL: 5274 { 5275 unsigned int fnpos, argpos; 5276 unsigned int implicit_use_args[2]; 5277 unsigned int num_implicit_use_args = 0; 5278 switch (DECL_FUNCTION_CODE (decl)) 5279 { 5280 case BUILT_IN_GOMP_PARALLEL: 5281 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */ 5282 fnpos = 0; 5283 argpos = 1; 5284 break; 5285 case BUILT_IN_GOACC_PARALLEL: 5286 /* __builtin_GOACC_parallel (flags_m, fn, mapnum, hostaddrs, 5287 sizes, kinds, ...). */ 5288 fnpos = 1; 5289 argpos = 3; 5290 implicit_use_args[num_implicit_use_args++] = 4; 5291 implicit_use_args[num_implicit_use_args++] = 5; 5292 break; 5293 default: 5294 gcc_unreachable (); 5295 } 5296 5297 tree fnarg = gimple_call_arg (t, fnpos); 5298 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR); 5299 tree fndecl = TREE_OPERAND (fnarg, 0); 5300 if (fndecl_maybe_in_other_partition (fndecl)) 5301 /* Fallthru to general call handling. */ 5302 break; 5303 5304 varinfo_t cfi = get_vi_for_tree (fndecl); 5305 5306 tree arg = gimple_call_arg (t, argpos); 5307 5308 /* Parameter passed by value is used. */ 5309 lhs = get_function_part_constraint (fi, fi_uses); 5310 struct constraint_expr *rhsp; 5311 get_constraint_for (arg, &rhsc); 5312 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 5313 process_constraint (new_constraint (lhs, *rhsp)); 5314 rhsc.truncate (0); 5315 5316 /* Handle parameters used by the call, but not used in cfi, as 5317 implicitly used by cfi. */ 5318 lhs = get_function_part_constraint (cfi, fi_uses); 5319 for (unsigned i = 0; i < num_implicit_use_args; ++i) 5320 { 5321 tree arg = gimple_call_arg (t, implicit_use_args[i]); 5322 get_constraint_for (arg, &rhsc); 5323 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 5324 process_constraint (new_constraint (lhs, *rhsp)); 5325 rhsc.truncate (0); 5326 } 5327 5328 /* The caller clobbers what the callee does. */ 5329 lhs = get_function_part_constraint (fi, fi_clobbers); 5330 rhs = get_function_part_constraint (cfi, fi_clobbers); 5331 process_constraint (new_constraint (lhs, rhs)); 5332 5333 /* The caller uses what the callee does. */ 5334 lhs = get_function_part_constraint (fi, fi_uses); 5335 rhs = get_function_part_constraint (cfi, fi_uses); 5336 process_constraint (new_constraint (lhs, rhs)); 5337 5338 return; 5339 } 5340 /* printf-style functions may have hooks to set pointers to 5341 point to somewhere into the generated string. Leave them 5342 for a later exercise... */ 5343 default: 5344 /* Fallthru to general call handling. */; 5345 } 5346 5347 /* Parameters passed by value are used. */ 5348 lhs = get_function_part_constraint (fi, fi_uses); 5349 for (i = 0; i < gimple_call_num_args (t); i++) 5350 { 5351 struct constraint_expr *rhsp; 5352 tree arg = gimple_call_arg (t, i); 5353 5354 if (TREE_CODE (arg) == SSA_NAME 5355 || is_gimple_min_invariant (arg)) 5356 continue; 5357 5358 get_constraint_for_address_of (arg, &rhsc); 5359 FOR_EACH_VEC_ELT (rhsc, j, rhsp) 5360 process_constraint (new_constraint (lhs, *rhsp)); 5361 rhsc.truncate (0); 5362 } 5363 5364 /* Build constraints for propagating clobbers/uses along the 5365 callgraph edges. */ 5366 cfi = get_fi_for_callee (call_stmt); 5367 if (cfi->id == anything_id) 5368 { 5369 if (gimple_vdef (t)) 5370 make_constraint_from (first_vi_for_offset (fi, fi_clobbers), 5371 anything_id); 5372 make_constraint_from (first_vi_for_offset (fi, fi_uses), 5373 anything_id); 5374 return; 5375 } 5376 5377 /* For callees without function info (that's external functions), 5378 ESCAPED is clobbered and used. */ 5379 if (cfi->decl 5380 && TREE_CODE (cfi->decl) == FUNCTION_DECL 5381 && !cfi->is_fn_info) 5382 { 5383 varinfo_t vi; 5384 5385 if (gimple_vdef (t)) 5386 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers), 5387 escaped_id); 5388 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id); 5389 5390 /* Also honor the call statement use/clobber info. */ 5391 if ((vi = lookup_call_clobber_vi (call_stmt)) != NULL) 5392 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers), 5393 vi->id); 5394 if ((vi = lookup_call_use_vi (call_stmt)) != NULL) 5395 make_copy_constraint (first_vi_for_offset (fi, fi_uses), 5396 vi->id); 5397 return; 5398 } 5399 5400 /* Otherwise the caller clobbers and uses what the callee does. 5401 ??? This should use a new complex constraint that filters 5402 local variables of the callee. */ 5403 if (gimple_vdef (t)) 5404 { 5405 lhs = get_function_part_constraint (fi, fi_clobbers); 5406 rhs = get_function_part_constraint (cfi, fi_clobbers); 5407 process_constraint (new_constraint (lhs, rhs)); 5408 } 5409 lhs = get_function_part_constraint (fi, fi_uses); 5410 rhs = get_function_part_constraint (cfi, fi_uses); 5411 process_constraint (new_constraint (lhs, rhs)); 5412 } 5413 else if (gimple_code (t) == GIMPLE_ASM) 5414 { 5415 /* ??? Ick. We can do better. */ 5416 if (gimple_vdef (t)) 5417 make_constraint_from (first_vi_for_offset (fi, fi_clobbers), 5418 anything_id); 5419 make_constraint_from (first_vi_for_offset (fi, fi_uses), 5420 anything_id); 5421 } 5422 } 5423 5424 5425 /* Find the first varinfo in the same variable as START that overlaps with 5426 OFFSET. Return NULL if we can't find one. */ 5427 5428 static varinfo_t 5429 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset) 5430 { 5431 /* If the offset is outside of the variable, bail out. */ 5432 if (offset >= start->fullsize) 5433 return NULL; 5434 5435 /* If we cannot reach offset from start, lookup the first field 5436 and start from there. */ 5437 if (start->offset > offset) 5438 start = get_varinfo (start->head); 5439 5440 while (start) 5441 { 5442 /* We may not find a variable in the field list with the actual 5443 offset when we have glommed a structure to a variable. 5444 In that case, however, offset should still be within the size 5445 of the variable. */ 5446 if (offset >= start->offset 5447 && (offset - start->offset) < start->size) 5448 return start; 5449 5450 start = vi_next (start); 5451 } 5452 5453 return NULL; 5454 } 5455 5456 /* Find the first varinfo in the same variable as START that overlaps with 5457 OFFSET. If there is no such varinfo the varinfo directly preceding 5458 OFFSET is returned. */ 5459 5460 static varinfo_t 5461 first_or_preceding_vi_for_offset (varinfo_t start, 5462 unsigned HOST_WIDE_INT offset) 5463 { 5464 /* If we cannot reach offset from start, lookup the first field 5465 and start from there. */ 5466 if (start->offset > offset) 5467 start = get_varinfo (start->head); 5468 5469 /* We may not find a variable in the field list with the actual 5470 offset when we have glommed a structure to a variable. 5471 In that case, however, offset should still be within the size 5472 of the variable. 5473 If we got beyond the offset we look for return the field 5474 directly preceding offset which may be the last field. */ 5475 while (start->next 5476 && offset >= start->offset 5477 && !((offset - start->offset) < start->size)) 5478 start = vi_next (start); 5479 5480 return start; 5481 } 5482 5483 5484 /* This structure is used during pushing fields onto the fieldstack 5485 to track the offset of the field, since bitpos_of_field gives it 5486 relative to its immediate containing type, and we want it relative 5487 to the ultimate containing object. */ 5488 5489 struct fieldoff 5490 { 5491 /* Offset from the base of the base containing object to this field. */ 5492 HOST_WIDE_INT offset; 5493 5494 /* Size, in bits, of the field. */ 5495 unsigned HOST_WIDE_INT size; 5496 5497 unsigned has_unknown_size : 1; 5498 5499 unsigned must_have_pointers : 1; 5500 5501 unsigned may_have_pointers : 1; 5502 5503 unsigned only_restrict_pointers : 1; 5504 5505 tree restrict_pointed_type; 5506 }; 5507 typedef struct fieldoff fieldoff_s; 5508 5509 5510 /* qsort comparison function for two fieldoff's PA and PB */ 5511 5512 static int 5513 fieldoff_compare (const void *pa, const void *pb) 5514 { 5515 const fieldoff_s *foa = (const fieldoff_s *)pa; 5516 const fieldoff_s *fob = (const fieldoff_s *)pb; 5517 unsigned HOST_WIDE_INT foasize, fobsize; 5518 5519 if (foa->offset < fob->offset) 5520 return -1; 5521 else if (foa->offset > fob->offset) 5522 return 1; 5523 5524 foasize = foa->size; 5525 fobsize = fob->size; 5526 if (foasize < fobsize) 5527 return -1; 5528 else if (foasize > fobsize) 5529 return 1; 5530 return 0; 5531 } 5532 5533 /* Sort a fieldstack according to the field offset and sizes. */ 5534 static void 5535 sort_fieldstack (vec<fieldoff_s> fieldstack) 5536 { 5537 fieldstack.qsort (fieldoff_compare); 5538 } 5539 5540 /* Return true if T is a type that can have subvars. */ 5541 5542 static inline bool 5543 type_can_have_subvars (const_tree t) 5544 { 5545 /* Aggregates without overlapping fields can have subvars. */ 5546 return TREE_CODE (t) == RECORD_TYPE; 5547 } 5548 5549 /* Return true if V is a tree that we can have subvars for. 5550 Normally, this is any aggregate type. Also complex 5551 types which are not gimple registers can have subvars. */ 5552 5553 static inline bool 5554 var_can_have_subvars (const_tree v) 5555 { 5556 /* Volatile variables should never have subvars. */ 5557 if (TREE_THIS_VOLATILE (v)) 5558 return false; 5559 5560 /* Non decls or memory tags can never have subvars. */ 5561 if (!DECL_P (v)) 5562 return false; 5563 5564 return type_can_have_subvars (TREE_TYPE (v)); 5565 } 5566 5567 /* Return true if T is a type that does contain pointers. */ 5568 5569 static bool 5570 type_must_have_pointers (tree type) 5571 { 5572 if (POINTER_TYPE_P (type)) 5573 return true; 5574 5575 if (TREE_CODE (type) == ARRAY_TYPE) 5576 return type_must_have_pointers (TREE_TYPE (type)); 5577 5578 /* A function or method can have pointers as arguments, so track 5579 those separately. */ 5580 if (TREE_CODE (type) == FUNCTION_TYPE 5581 || TREE_CODE (type) == METHOD_TYPE) 5582 return true; 5583 5584 return false; 5585 } 5586 5587 static bool 5588 field_must_have_pointers (tree t) 5589 { 5590 return type_must_have_pointers (TREE_TYPE (t)); 5591 } 5592 5593 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all 5594 the fields of TYPE onto fieldstack, recording their offsets along 5595 the way. 5596 5597 OFFSET is used to keep track of the offset in this entire 5598 structure, rather than just the immediately containing structure. 5599 Returns false if the caller is supposed to handle the field we 5600 recursed for. */ 5601 5602 static bool 5603 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack, 5604 HOST_WIDE_INT offset) 5605 { 5606 tree field; 5607 bool empty_p = true; 5608 5609 if (TREE_CODE (type) != RECORD_TYPE) 5610 return false; 5611 5612 /* If the vector of fields is growing too big, bail out early. 5613 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make 5614 sure this fails. */ 5615 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE) 5616 return false; 5617 5618 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 5619 if (TREE_CODE (field) == FIELD_DECL) 5620 { 5621 bool push = false; 5622 HOST_WIDE_INT foff = bitpos_of_field (field); 5623 tree field_type = TREE_TYPE (field); 5624 5625 if (!var_can_have_subvars (field) 5626 || TREE_CODE (field_type) == QUAL_UNION_TYPE 5627 || TREE_CODE (field_type) == UNION_TYPE) 5628 push = true; 5629 else if (!push_fields_onto_fieldstack 5630 (field_type, fieldstack, offset + foff) 5631 && (DECL_SIZE (field) 5632 && !integer_zerop (DECL_SIZE (field)))) 5633 /* Empty structures may have actual size, like in C++. So 5634 see if we didn't push any subfields and the size is 5635 nonzero, push the field onto the stack. */ 5636 push = true; 5637 5638 if (push) 5639 { 5640 fieldoff_s *pair = NULL; 5641 bool has_unknown_size = false; 5642 bool must_have_pointers_p; 5643 5644 if (!fieldstack->is_empty ()) 5645 pair = &fieldstack->last (); 5646 5647 /* If there isn't anything at offset zero, create sth. */ 5648 if (!pair 5649 && offset + foff != 0) 5650 { 5651 fieldoff_s e 5652 = {0, offset + foff, false, false, true, false, NULL_TREE}; 5653 pair = fieldstack->safe_push (e); 5654 } 5655 5656 if (!DECL_SIZE (field) 5657 || !tree_fits_uhwi_p (DECL_SIZE (field))) 5658 has_unknown_size = true; 5659 5660 /* If adjacent fields do not contain pointers merge them. */ 5661 must_have_pointers_p = field_must_have_pointers (field); 5662 if (pair 5663 && !has_unknown_size 5664 && !must_have_pointers_p 5665 && !pair->must_have_pointers 5666 && !pair->has_unknown_size 5667 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff) 5668 { 5669 pair->size += tree_to_uhwi (DECL_SIZE (field)); 5670 } 5671 else 5672 { 5673 fieldoff_s e; 5674 e.offset = offset + foff; 5675 e.has_unknown_size = has_unknown_size; 5676 if (!has_unknown_size) 5677 e.size = tree_to_uhwi (DECL_SIZE (field)); 5678 else 5679 e.size = -1; 5680 e.must_have_pointers = must_have_pointers_p; 5681 e.may_have_pointers = true; 5682 e.only_restrict_pointers 5683 = (!has_unknown_size 5684 && POINTER_TYPE_P (field_type) 5685 && TYPE_RESTRICT (field_type)); 5686 if (e.only_restrict_pointers) 5687 e.restrict_pointed_type = TREE_TYPE (field_type); 5688 fieldstack->safe_push (e); 5689 } 5690 } 5691 5692 empty_p = false; 5693 } 5694 5695 return !empty_p; 5696 } 5697 5698 /* Count the number of arguments DECL has, and set IS_VARARGS to true 5699 if it is a varargs function. */ 5700 5701 static unsigned int 5702 count_num_arguments (tree decl, bool *is_varargs) 5703 { 5704 unsigned int num = 0; 5705 tree t; 5706 5707 /* Capture named arguments for K&R functions. They do not 5708 have a prototype and thus no TYPE_ARG_TYPES. */ 5709 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t)) 5710 ++num; 5711 5712 /* Check if the function has variadic arguments. */ 5713 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 5714 if (TREE_VALUE (t) == void_type_node) 5715 break; 5716 if (!t) 5717 *is_varargs = true; 5718 5719 return num; 5720 } 5721 5722 /* Creation function node for DECL, using NAME, and return the index 5723 of the variable we've created for the function. If NONLOCAL_p, create 5724 initial constraints. */ 5725 5726 static varinfo_t 5727 create_function_info_for (tree decl, const char *name, bool add_id, 5728 bool nonlocal_p) 5729 { 5730 struct function *fn = DECL_STRUCT_FUNCTION (decl); 5731 varinfo_t vi, prev_vi; 5732 tree arg; 5733 unsigned int i; 5734 bool is_varargs = false; 5735 unsigned int num_args = count_num_arguments (decl, &is_varargs); 5736 5737 /* Create the variable info. */ 5738 5739 vi = new_var_info (decl, name, add_id); 5740 vi->offset = 0; 5741 vi->size = 1; 5742 vi->fullsize = fi_parm_base + num_args; 5743 vi->is_fn_info = 1; 5744 vi->may_have_pointers = false; 5745 if (is_varargs) 5746 vi->fullsize = ~0; 5747 insert_vi_for_tree (vi->decl, vi); 5748 5749 prev_vi = vi; 5750 5751 /* Create a variable for things the function clobbers and one for 5752 things the function uses. */ 5753 { 5754 varinfo_t clobbervi, usevi; 5755 const char *newname; 5756 char *tempname; 5757 5758 tempname = xasprintf ("%s.clobber", name); 5759 newname = ggc_strdup (tempname); 5760 free (tempname); 5761 5762 clobbervi = new_var_info (NULL, newname, false); 5763 clobbervi->offset = fi_clobbers; 5764 clobbervi->size = 1; 5765 clobbervi->fullsize = vi->fullsize; 5766 clobbervi->is_full_var = true; 5767 clobbervi->is_global_var = false; 5768 clobbervi->is_reg_var = true; 5769 5770 gcc_assert (prev_vi->offset < clobbervi->offset); 5771 prev_vi->next = clobbervi->id; 5772 prev_vi = clobbervi; 5773 5774 tempname = xasprintf ("%s.use", name); 5775 newname = ggc_strdup (tempname); 5776 free (tempname); 5777 5778 usevi = new_var_info (NULL, newname, false); 5779 usevi->offset = fi_uses; 5780 usevi->size = 1; 5781 usevi->fullsize = vi->fullsize; 5782 usevi->is_full_var = true; 5783 usevi->is_global_var = false; 5784 usevi->is_reg_var = true; 5785 5786 gcc_assert (prev_vi->offset < usevi->offset); 5787 prev_vi->next = usevi->id; 5788 prev_vi = usevi; 5789 } 5790 5791 /* And one for the static chain. */ 5792 if (fn->static_chain_decl != NULL_TREE) 5793 { 5794 varinfo_t chainvi; 5795 const char *newname; 5796 char *tempname; 5797 5798 tempname = xasprintf ("%s.chain", name); 5799 newname = ggc_strdup (tempname); 5800 free (tempname); 5801 5802 chainvi = new_var_info (fn->static_chain_decl, newname, false); 5803 chainvi->offset = fi_static_chain; 5804 chainvi->size = 1; 5805 chainvi->fullsize = vi->fullsize; 5806 chainvi->is_full_var = true; 5807 chainvi->is_global_var = false; 5808 5809 insert_vi_for_tree (fn->static_chain_decl, chainvi); 5810 5811 if (nonlocal_p 5812 && chainvi->may_have_pointers) 5813 make_constraint_from (chainvi, nonlocal_id); 5814 5815 gcc_assert (prev_vi->offset < chainvi->offset); 5816 prev_vi->next = chainvi->id; 5817 prev_vi = chainvi; 5818 } 5819 5820 /* Create a variable for the return var. */ 5821 if (DECL_RESULT (decl) != NULL 5822 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) 5823 { 5824 varinfo_t resultvi; 5825 const char *newname; 5826 char *tempname; 5827 tree resultdecl = decl; 5828 5829 if (DECL_RESULT (decl)) 5830 resultdecl = DECL_RESULT (decl); 5831 5832 tempname = xasprintf ("%s.result", name); 5833 newname = ggc_strdup (tempname); 5834 free (tempname); 5835 5836 resultvi = new_var_info (resultdecl, newname, false); 5837 resultvi->offset = fi_result; 5838 resultvi->size = 1; 5839 resultvi->fullsize = vi->fullsize; 5840 resultvi->is_full_var = true; 5841 if (DECL_RESULT (decl)) 5842 resultvi->may_have_pointers = true; 5843 5844 if (DECL_RESULT (decl)) 5845 insert_vi_for_tree (DECL_RESULT (decl), resultvi); 5846 5847 if (nonlocal_p 5848 && DECL_RESULT (decl) 5849 && DECL_BY_REFERENCE (DECL_RESULT (decl))) 5850 make_constraint_from (resultvi, nonlocal_id); 5851 5852 gcc_assert (prev_vi->offset < resultvi->offset); 5853 prev_vi->next = resultvi->id; 5854 prev_vi = resultvi; 5855 } 5856 5857 /* We also need to make function return values escape. Nothing 5858 escapes by returning from main though. */ 5859 if (nonlocal_p 5860 && !MAIN_NAME_P (DECL_NAME (decl))) 5861 { 5862 varinfo_t fi, rvi; 5863 fi = lookup_vi_for_tree (decl); 5864 rvi = first_vi_for_offset (fi, fi_result); 5865 if (rvi && rvi->offset == fi_result) 5866 make_copy_constraint (get_varinfo (escaped_id), rvi->id); 5867 } 5868 5869 /* Set up variables for each argument. */ 5870 arg = DECL_ARGUMENTS (decl); 5871 for (i = 0; i < num_args; i++) 5872 { 5873 varinfo_t argvi; 5874 const char *newname; 5875 char *tempname; 5876 tree argdecl = decl; 5877 5878 if (arg) 5879 argdecl = arg; 5880 5881 tempname = xasprintf ("%s.arg%d", name, i); 5882 newname = ggc_strdup (tempname); 5883 free (tempname); 5884 5885 argvi = new_var_info (argdecl, newname, false); 5886 argvi->offset = fi_parm_base + i; 5887 argvi->size = 1; 5888 argvi->is_full_var = true; 5889 argvi->fullsize = vi->fullsize; 5890 if (arg) 5891 argvi->may_have_pointers = true; 5892 5893 if (arg) 5894 insert_vi_for_tree (arg, argvi); 5895 5896 if (nonlocal_p 5897 && argvi->may_have_pointers) 5898 make_constraint_from (argvi, nonlocal_id); 5899 5900 gcc_assert (prev_vi->offset < argvi->offset); 5901 prev_vi->next = argvi->id; 5902 prev_vi = argvi; 5903 if (arg) 5904 arg = DECL_CHAIN (arg); 5905 } 5906 5907 /* Add one representative for all further args. */ 5908 if (is_varargs) 5909 { 5910 varinfo_t argvi; 5911 const char *newname; 5912 char *tempname; 5913 tree decl; 5914 5915 tempname = xasprintf ("%s.varargs", name); 5916 newname = ggc_strdup (tempname); 5917 free (tempname); 5918 5919 /* We need sth that can be pointed to for va_start. */ 5920 decl = build_fake_var_decl (ptr_type_node); 5921 5922 argvi = new_var_info (decl, newname, false); 5923 argvi->offset = fi_parm_base + num_args; 5924 argvi->size = ~0; 5925 argvi->is_full_var = true; 5926 argvi->is_heap_var = true; 5927 argvi->fullsize = vi->fullsize; 5928 5929 if (nonlocal_p 5930 && argvi->may_have_pointers) 5931 make_constraint_from (argvi, nonlocal_id); 5932 5933 gcc_assert (prev_vi->offset < argvi->offset); 5934 prev_vi->next = argvi->id; 5935 prev_vi = argvi; 5936 } 5937 5938 return vi; 5939 } 5940 5941 5942 /* Return true if FIELDSTACK contains fields that overlap. 5943 FIELDSTACK is assumed to be sorted by offset. */ 5944 5945 static bool 5946 check_for_overlaps (vec<fieldoff_s> fieldstack) 5947 { 5948 fieldoff_s *fo = NULL; 5949 unsigned int i; 5950 HOST_WIDE_INT lastoffset = -1; 5951 5952 FOR_EACH_VEC_ELT (fieldstack, i, fo) 5953 { 5954 if (fo->offset == lastoffset) 5955 return true; 5956 lastoffset = fo->offset; 5957 } 5958 return false; 5959 } 5960 5961 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP. 5962 This will also create any varinfo structures necessary for fields 5963 of DECL. DECL is a function parameter if HANDLE_PARAM is set. 5964 HANDLED_STRUCT_TYPE is used to register struct types reached by following 5965 restrict pointers. This is needed to prevent infinite recursion. 5966 If ADD_RESTRICT, pretend that the pointer NAME is restrict even if DECL 5967 does not advertise it. */ 5968 5969 static varinfo_t 5970 create_variable_info_for_1 (tree decl, const char *name, bool add_id, 5971 bool handle_param, bitmap handled_struct_type, 5972 bool add_restrict = false) 5973 { 5974 varinfo_t vi, newvi; 5975 tree decl_type = TREE_TYPE (decl); 5976 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type); 5977 auto_vec<fieldoff_s> fieldstack; 5978 fieldoff_s *fo; 5979 unsigned int i; 5980 5981 if (!declsize 5982 || !tree_fits_uhwi_p (declsize)) 5983 { 5984 vi = new_var_info (decl, name, add_id); 5985 vi->offset = 0; 5986 vi->size = ~0; 5987 vi->fullsize = ~0; 5988 vi->is_unknown_size_var = true; 5989 vi->is_full_var = true; 5990 vi->may_have_pointers = true; 5991 return vi; 5992 } 5993 5994 /* Collect field information. */ 5995 if (use_field_sensitive 5996 && var_can_have_subvars (decl) 5997 /* ??? Force us to not use subfields for globals in IPA mode. 5998 Else we'd have to parse arbitrary initializers. */ 5999 && !(in_ipa_mode 6000 && is_global_var (decl))) 6001 { 6002 fieldoff_s *fo = NULL; 6003 bool notokay = false; 6004 unsigned int i; 6005 6006 push_fields_onto_fieldstack (decl_type, &fieldstack, 0); 6007 6008 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++) 6009 if (fo->has_unknown_size 6010 || fo->offset < 0) 6011 { 6012 notokay = true; 6013 break; 6014 } 6015 6016 /* We can't sort them if we have a field with a variable sized type, 6017 which will make notokay = true. In that case, we are going to return 6018 without creating varinfos for the fields anyway, so sorting them is a 6019 waste to boot. */ 6020 if (!notokay) 6021 { 6022 sort_fieldstack (fieldstack); 6023 /* Due to some C++ FE issues, like PR 22488, we might end up 6024 what appear to be overlapping fields even though they, 6025 in reality, do not overlap. Until the C++ FE is fixed, 6026 we will simply disable field-sensitivity for these cases. */ 6027 notokay = check_for_overlaps (fieldstack); 6028 } 6029 6030 if (notokay) 6031 fieldstack.release (); 6032 } 6033 6034 /* If we didn't end up collecting sub-variables create a full 6035 variable for the decl. */ 6036 if (fieldstack.length () == 0 6037 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE) 6038 { 6039 vi = new_var_info (decl, name, add_id); 6040 vi->offset = 0; 6041 vi->may_have_pointers = true; 6042 vi->fullsize = tree_to_uhwi (declsize); 6043 vi->size = vi->fullsize; 6044 vi->is_full_var = true; 6045 if (POINTER_TYPE_P (decl_type) 6046 && (TYPE_RESTRICT (decl_type) || add_restrict)) 6047 vi->only_restrict_pointers = 1; 6048 if (vi->only_restrict_pointers 6049 && !type_contains_placeholder_p (TREE_TYPE (decl_type)) 6050 && handle_param 6051 && !bitmap_bit_p (handled_struct_type, 6052 TYPE_UID (TREE_TYPE (decl_type)))) 6053 { 6054 varinfo_t rvi; 6055 tree heapvar = build_fake_var_decl (TREE_TYPE (decl_type)); 6056 DECL_EXTERNAL (heapvar) = 1; 6057 if (var_can_have_subvars (heapvar)) 6058 bitmap_set_bit (handled_struct_type, 6059 TYPE_UID (TREE_TYPE (decl_type))); 6060 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true, 6061 true, handled_struct_type); 6062 if (var_can_have_subvars (heapvar)) 6063 bitmap_clear_bit (handled_struct_type, 6064 TYPE_UID (TREE_TYPE (decl_type))); 6065 rvi->is_restrict_var = 1; 6066 insert_vi_for_tree (heapvar, rvi); 6067 make_constraint_from (vi, rvi->id); 6068 make_param_constraints (rvi); 6069 } 6070 fieldstack.release (); 6071 return vi; 6072 } 6073 6074 vi = new_var_info (decl, name, add_id); 6075 vi->fullsize = tree_to_uhwi (declsize); 6076 if (fieldstack.length () == 1) 6077 vi->is_full_var = true; 6078 for (i = 0, newvi = vi; 6079 fieldstack.iterate (i, &fo); 6080 ++i, newvi = vi_next (newvi)) 6081 { 6082 const char *newname = NULL; 6083 char *tempname; 6084 6085 if (dump_file) 6086 { 6087 if (fieldstack.length () != 1) 6088 { 6089 tempname 6090 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC 6091 "+" HOST_WIDE_INT_PRINT_DEC, name, 6092 fo->offset, fo->size); 6093 newname = ggc_strdup (tempname); 6094 free (tempname); 6095 } 6096 } 6097 else 6098 newname = "NULL"; 6099 6100 if (newname) 6101 newvi->name = newname; 6102 newvi->offset = fo->offset; 6103 newvi->size = fo->size; 6104 newvi->fullsize = vi->fullsize; 6105 newvi->may_have_pointers = fo->may_have_pointers; 6106 newvi->only_restrict_pointers = fo->only_restrict_pointers; 6107 if (handle_param 6108 && newvi->only_restrict_pointers 6109 && !type_contains_placeholder_p (fo->restrict_pointed_type) 6110 && !bitmap_bit_p (handled_struct_type, 6111 TYPE_UID (fo->restrict_pointed_type))) 6112 { 6113 varinfo_t rvi; 6114 tree heapvar = build_fake_var_decl (fo->restrict_pointed_type); 6115 DECL_EXTERNAL (heapvar) = 1; 6116 if (var_can_have_subvars (heapvar)) 6117 bitmap_set_bit (handled_struct_type, 6118 TYPE_UID (fo->restrict_pointed_type)); 6119 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true, 6120 true, handled_struct_type); 6121 if (var_can_have_subvars (heapvar)) 6122 bitmap_clear_bit (handled_struct_type, 6123 TYPE_UID (fo->restrict_pointed_type)); 6124 rvi->is_restrict_var = 1; 6125 insert_vi_for_tree (heapvar, rvi); 6126 make_constraint_from (newvi, rvi->id); 6127 make_param_constraints (rvi); 6128 } 6129 if (i + 1 < fieldstack.length ()) 6130 { 6131 varinfo_t tem = new_var_info (decl, name, false); 6132 newvi->next = tem->id; 6133 tem->head = vi->id; 6134 } 6135 } 6136 6137 return vi; 6138 } 6139 6140 static unsigned int 6141 create_variable_info_for (tree decl, const char *name, bool add_id) 6142 { 6143 /* First see if we are dealing with an ifunc resolver call and 6144 assiociate that with a call to the resolver function result. */ 6145 cgraph_node *node; 6146 if (in_ipa_mode 6147 && TREE_CODE (decl) == FUNCTION_DECL 6148 && (node = cgraph_node::get (decl)) 6149 && node->ifunc_resolver) 6150 { 6151 varinfo_t fi = get_vi_for_tree (node->get_alias_target ()->decl); 6152 constraint_expr rhs 6153 = get_function_part_constraint (fi, fi_result); 6154 fi = new_var_info (NULL_TREE, "ifuncres", true); 6155 fi->is_reg_var = true; 6156 constraint_expr lhs; 6157 lhs.type = SCALAR; 6158 lhs.var = fi->id; 6159 lhs.offset = 0; 6160 process_constraint (new_constraint (lhs, rhs)); 6161 insert_vi_for_tree (decl, fi); 6162 return fi->id; 6163 } 6164 6165 varinfo_t vi = create_variable_info_for_1 (decl, name, add_id, false, NULL); 6166 unsigned int id = vi->id; 6167 6168 insert_vi_for_tree (decl, vi); 6169 6170 if (!VAR_P (decl)) 6171 return id; 6172 6173 /* Create initial constraints for globals. */ 6174 for (; vi; vi = vi_next (vi)) 6175 { 6176 if (!vi->may_have_pointers 6177 || !vi->is_global_var) 6178 continue; 6179 6180 /* Mark global restrict qualified pointers. */ 6181 if ((POINTER_TYPE_P (TREE_TYPE (decl)) 6182 && TYPE_RESTRICT (TREE_TYPE (decl))) 6183 || vi->only_restrict_pointers) 6184 { 6185 varinfo_t rvi 6186 = make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT", 6187 true); 6188 /* ??? For now exclude reads from globals as restrict sources 6189 if those are not (indirectly) from incoming parameters. */ 6190 rvi->is_restrict_var = false; 6191 continue; 6192 } 6193 6194 /* In non-IPA mode the initializer from nonlocal is all we need. */ 6195 if (!in_ipa_mode 6196 || DECL_HARD_REGISTER (decl)) 6197 make_copy_constraint (vi, nonlocal_id); 6198 6199 /* In IPA mode parse the initializer and generate proper constraints 6200 for it. */ 6201 else 6202 { 6203 varpool_node *vnode = varpool_node::get (decl); 6204 6205 /* For escaped variables initialize them from nonlocal. */ 6206 if (!vnode->all_refs_explicit_p ()) 6207 make_copy_constraint (vi, nonlocal_id); 6208 6209 /* If this is a global variable with an initializer and we are in 6210 IPA mode generate constraints for it. */ 6211 ipa_ref *ref; 6212 for (unsigned idx = 0; vnode->iterate_reference (idx, ref); ++idx) 6213 { 6214 auto_vec<ce_s> rhsc; 6215 struct constraint_expr lhs, *rhsp; 6216 unsigned i; 6217 get_constraint_for_address_of (ref->referred->decl, &rhsc); 6218 lhs.var = vi->id; 6219 lhs.offset = 0; 6220 lhs.type = SCALAR; 6221 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 6222 process_constraint (new_constraint (lhs, *rhsp)); 6223 /* If this is a variable that escapes from the unit 6224 the initializer escapes as well. */ 6225 if (!vnode->all_refs_explicit_p ()) 6226 { 6227 lhs.var = escaped_id; 6228 lhs.offset = 0; 6229 lhs.type = SCALAR; 6230 FOR_EACH_VEC_ELT (rhsc, i, rhsp) 6231 process_constraint (new_constraint (lhs, *rhsp)); 6232 } 6233 } 6234 } 6235 } 6236 6237 return id; 6238 } 6239 6240 /* Print out the points-to solution for VAR to FILE. */ 6241 6242 static void 6243 dump_solution_for_var (FILE *file, unsigned int var) 6244 { 6245 varinfo_t vi = get_varinfo (var); 6246 unsigned int i; 6247 bitmap_iterator bi; 6248 6249 /* Dump the solution for unified vars anyway, this avoids difficulties 6250 in scanning dumps in the testsuite. */ 6251 fprintf (file, "%s = { ", vi->name); 6252 vi = get_varinfo (find (var)); 6253 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) 6254 fprintf (file, "%s ", get_varinfo (i)->name); 6255 fprintf (file, "}"); 6256 6257 /* But note when the variable was unified. */ 6258 if (vi->id != var) 6259 fprintf (file, " same as %s", vi->name); 6260 6261 fprintf (file, "\n"); 6262 } 6263 6264 /* Print the points-to solution for VAR to stderr. */ 6265 6266 DEBUG_FUNCTION void 6267 debug_solution_for_var (unsigned int var) 6268 { 6269 dump_solution_for_var (stderr, var); 6270 } 6271 6272 /* Register the constraints for function parameter related VI. */ 6273 6274 static void 6275 make_param_constraints (varinfo_t vi) 6276 { 6277 for (; vi; vi = vi_next (vi)) 6278 { 6279 if (vi->only_restrict_pointers) 6280 ; 6281 else if (vi->may_have_pointers) 6282 make_constraint_from (vi, nonlocal_id); 6283 6284 if (vi->is_full_var) 6285 break; 6286 } 6287 } 6288 6289 /* Create varinfo structures for all of the variables in the 6290 function for intraprocedural mode. */ 6291 6292 static void 6293 intra_create_variable_infos (struct function *fn) 6294 { 6295 tree t; 6296 bitmap handled_struct_type = NULL; 6297 bool this_parm_in_ctor = DECL_CXX_CONSTRUCTOR_P (fn->decl); 6298 6299 /* For each incoming pointer argument arg, create the constraint ARG 6300 = NONLOCAL or a dummy variable if it is a restrict qualified 6301 passed-by-reference argument. */ 6302 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t)) 6303 { 6304 if (handled_struct_type == NULL) 6305 handled_struct_type = BITMAP_ALLOC (NULL); 6306 6307 varinfo_t p 6308 = create_variable_info_for_1 (t, alias_get_name (t), false, true, 6309 handled_struct_type, this_parm_in_ctor); 6310 insert_vi_for_tree (t, p); 6311 6312 make_param_constraints (p); 6313 6314 this_parm_in_ctor = false; 6315 } 6316 6317 if (handled_struct_type != NULL) 6318 BITMAP_FREE (handled_struct_type); 6319 6320 /* Add a constraint for a result decl that is passed by reference. */ 6321 if (DECL_RESULT (fn->decl) 6322 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl))) 6323 { 6324 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl)); 6325 6326 for (p = result_vi; p; p = vi_next (p)) 6327 make_constraint_from (p, nonlocal_id); 6328 } 6329 6330 /* Add a constraint for the incoming static chain parameter. */ 6331 if (fn->static_chain_decl != NULL_TREE) 6332 { 6333 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl); 6334 6335 for (p = chain_vi; p; p = vi_next (p)) 6336 make_constraint_from (p, nonlocal_id); 6337 } 6338 } 6339 6340 /* Structure used to put solution bitmaps in a hashtable so they can 6341 be shared among variables with the same points-to set. */ 6342 6343 typedef struct shared_bitmap_info 6344 { 6345 bitmap pt_vars; 6346 hashval_t hashcode; 6347 } *shared_bitmap_info_t; 6348 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t; 6349 6350 /* Shared_bitmap hashtable helpers. */ 6351 6352 struct shared_bitmap_hasher : free_ptr_hash <shared_bitmap_info> 6353 { 6354 static inline hashval_t hash (const shared_bitmap_info *); 6355 static inline bool equal (const shared_bitmap_info *, 6356 const shared_bitmap_info *); 6357 }; 6358 6359 /* Hash function for a shared_bitmap_info_t */ 6360 6361 inline hashval_t 6362 shared_bitmap_hasher::hash (const shared_bitmap_info *bi) 6363 { 6364 return bi->hashcode; 6365 } 6366 6367 /* Equality function for two shared_bitmap_info_t's. */ 6368 6369 inline bool 6370 shared_bitmap_hasher::equal (const shared_bitmap_info *sbi1, 6371 const shared_bitmap_info *sbi2) 6372 { 6373 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars); 6374 } 6375 6376 /* Shared_bitmap hashtable. */ 6377 6378 static hash_table<shared_bitmap_hasher> *shared_bitmap_table; 6379 6380 /* Lookup a bitmap in the shared bitmap hashtable, and return an already 6381 existing instance if there is one, NULL otherwise. */ 6382 6383 static bitmap 6384 shared_bitmap_lookup (bitmap pt_vars) 6385 { 6386 shared_bitmap_info **slot; 6387 struct shared_bitmap_info sbi; 6388 6389 sbi.pt_vars = pt_vars; 6390 sbi.hashcode = bitmap_hash (pt_vars); 6391 6392 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT); 6393 if (!slot) 6394 return NULL; 6395 else 6396 return (*slot)->pt_vars; 6397 } 6398 6399 6400 /* Add a bitmap to the shared bitmap hashtable. */ 6401 6402 static void 6403 shared_bitmap_add (bitmap pt_vars) 6404 { 6405 shared_bitmap_info **slot; 6406 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info); 6407 6408 sbi->pt_vars = pt_vars; 6409 sbi->hashcode = bitmap_hash (pt_vars); 6410 6411 slot = shared_bitmap_table->find_slot (sbi, INSERT); 6412 gcc_assert (!*slot); 6413 *slot = sbi; 6414 } 6415 6416 6417 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */ 6418 6419 static void 6420 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt, 6421 tree fndecl) 6422 { 6423 unsigned int i; 6424 bitmap_iterator bi; 6425 varinfo_t escaped_vi = get_varinfo (find (escaped_id)); 6426 bool everything_escaped 6427 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id); 6428 6429 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi) 6430 { 6431 varinfo_t vi = get_varinfo (i); 6432 6433 /* The only artificial variables that are allowed in a may-alias 6434 set are heap variables. */ 6435 if (vi->is_artificial_var && !vi->is_heap_var) 6436 continue; 6437 6438 if (everything_escaped 6439 || (escaped_vi->solution 6440 && bitmap_bit_p (escaped_vi->solution, i))) 6441 { 6442 pt->vars_contains_escaped = true; 6443 pt->vars_contains_escaped_heap |= vi->is_heap_var; 6444 } 6445 6446 if (vi->is_restrict_var) 6447 pt->vars_contains_restrict = true; 6448 6449 if (VAR_P (vi->decl) 6450 || TREE_CODE (vi->decl) == PARM_DECL 6451 || TREE_CODE (vi->decl) == RESULT_DECL) 6452 { 6453 /* If we are in IPA mode we will not recompute points-to 6454 sets after inlining so make sure they stay valid. */ 6455 if (in_ipa_mode 6456 && !DECL_PT_UID_SET_P (vi->decl)) 6457 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl)); 6458 6459 /* Add the decl to the points-to set. Note that the points-to 6460 set contains global variables. */ 6461 bitmap_set_bit (into, DECL_PT_UID (vi->decl)); 6462 if (vi->is_global_var 6463 /* In IPA mode the escaped_heap trick doesn't work as 6464 ESCAPED is escaped from the unit but 6465 pt_solution_includes_global needs to answer true for 6466 all variables not automatic within a function. 6467 For the same reason is_global_var is not the 6468 correct flag to track - local variables from other 6469 functions also need to be considered global. 6470 Conveniently all HEAP vars are not put in function 6471 scope. */ 6472 || (in_ipa_mode 6473 && fndecl 6474 && ! auto_var_in_fn_p (vi->decl, fndecl))) 6475 pt->vars_contains_nonlocal = true; 6476 6477 /* If we have a variable that is interposable record that fact 6478 for pointer comparison simplification. */ 6479 if (VAR_P (vi->decl) 6480 && (TREE_STATIC (vi->decl) || DECL_EXTERNAL (vi->decl)) 6481 && ! decl_binds_to_current_def_p (vi->decl)) 6482 pt->vars_contains_interposable = true; 6483 6484 /* If this is a local variable we can have overlapping lifetime 6485 of different function invocations through recursion duplicate 6486 it with its shadow variable. */ 6487 if (in_ipa_mode 6488 && vi->shadow_var_uid != 0) 6489 { 6490 bitmap_set_bit (into, vi->shadow_var_uid); 6491 pt->vars_contains_nonlocal = true; 6492 } 6493 } 6494 6495 else if (TREE_CODE (vi->decl) == FUNCTION_DECL 6496 || TREE_CODE (vi->decl) == LABEL_DECL) 6497 { 6498 /* Nothing should read/write from/to code so we can 6499 save bits by not including them in the points-to bitmaps. 6500 Still mark the points-to set as containing global memory 6501 to make code-patching possible - see PR70128. */ 6502 pt->vars_contains_nonlocal = true; 6503 } 6504 } 6505 } 6506 6507 6508 /* Compute the points-to solution *PT for the variable VI. */ 6509 6510 static struct pt_solution 6511 find_what_var_points_to (tree fndecl, varinfo_t orig_vi) 6512 { 6513 unsigned int i; 6514 bitmap_iterator bi; 6515 bitmap finished_solution; 6516 bitmap result; 6517 varinfo_t vi; 6518 struct pt_solution *pt; 6519 6520 /* This variable may have been collapsed, let's get the real 6521 variable. */ 6522 vi = get_varinfo (find (orig_vi->id)); 6523 6524 /* See if we have already computed the solution and return it. */ 6525 pt_solution **slot = &final_solutions->get_or_insert (vi); 6526 if (*slot != NULL) 6527 return **slot; 6528 6529 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution); 6530 memset (pt, 0, sizeof (struct pt_solution)); 6531 6532 /* Translate artificial variables into SSA_NAME_PTR_INFO 6533 attributes. */ 6534 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) 6535 { 6536 varinfo_t vi = get_varinfo (i); 6537 6538 if (vi->is_artificial_var) 6539 { 6540 if (vi->id == nothing_id) 6541 pt->null = 1; 6542 else if (vi->id == escaped_id) 6543 { 6544 if (in_ipa_mode) 6545 pt->ipa_escaped = 1; 6546 else 6547 pt->escaped = 1; 6548 /* Expand some special vars of ESCAPED in-place here. */ 6549 varinfo_t evi = get_varinfo (find (escaped_id)); 6550 if (bitmap_bit_p (evi->solution, nonlocal_id)) 6551 pt->nonlocal = 1; 6552 } 6553 else if (vi->id == nonlocal_id) 6554 pt->nonlocal = 1; 6555 else if (vi->is_heap_var) 6556 /* We represent heapvars in the points-to set properly. */ 6557 ; 6558 else if (vi->id == string_id) 6559 /* Nobody cares - STRING_CSTs are read-only entities. */ 6560 ; 6561 else if (vi->id == anything_id 6562 || vi->id == integer_id) 6563 pt->anything = 1; 6564 } 6565 } 6566 6567 /* Instead of doing extra work, simply do not create 6568 elaborate points-to information for pt_anything pointers. */ 6569 if (pt->anything) 6570 return *pt; 6571 6572 /* Share the final set of variables when possible. */ 6573 finished_solution = BITMAP_GGC_ALLOC (); 6574 stats.points_to_sets_created++; 6575 6576 set_uids_in_ptset (finished_solution, vi->solution, pt, fndecl); 6577 result = shared_bitmap_lookup (finished_solution); 6578 if (!result) 6579 { 6580 shared_bitmap_add (finished_solution); 6581 pt->vars = finished_solution; 6582 } 6583 else 6584 { 6585 pt->vars = result; 6586 bitmap_clear (finished_solution); 6587 } 6588 6589 return *pt; 6590 } 6591 6592 /* Given a pointer variable P, fill in its points-to set. */ 6593 6594 static void 6595 find_what_p_points_to (tree fndecl, tree p) 6596 { 6597 struct ptr_info_def *pi; 6598 tree lookup_p = p; 6599 varinfo_t vi; 6600 bool nonnull = get_ptr_nonnull (p); 6601 6602 /* For parameters, get at the points-to set for the actual parm 6603 decl. */ 6604 if (TREE_CODE (p) == SSA_NAME 6605 && SSA_NAME_IS_DEFAULT_DEF (p) 6606 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL 6607 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL)) 6608 lookup_p = SSA_NAME_VAR (p); 6609 6610 vi = lookup_vi_for_tree (lookup_p); 6611 if (!vi) 6612 return; 6613 6614 pi = get_ptr_info (p); 6615 pi->pt = find_what_var_points_to (fndecl, vi); 6616 /* Conservatively set to NULL from PTA (to true). */ 6617 pi->pt.null = 1; 6618 /* Preserve pointer nonnull computed by VRP. See get_ptr_nonnull 6619 in gcc/tree-ssaname.c for more information. */ 6620 if (nonnull) 6621 set_ptr_nonnull (p); 6622 } 6623 6624 6625 /* Query statistics for points-to solutions. */ 6626 6627 static struct { 6628 unsigned HOST_WIDE_INT pt_solution_includes_may_alias; 6629 unsigned HOST_WIDE_INT pt_solution_includes_no_alias; 6630 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias; 6631 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias; 6632 } pta_stats; 6633 6634 void 6635 dump_pta_stats (FILE *s) 6636 { 6637 fprintf (s, "\nPTA query stats:\n"); 6638 fprintf (s, " pt_solution_includes: " 6639 HOST_WIDE_INT_PRINT_DEC" disambiguations, " 6640 HOST_WIDE_INT_PRINT_DEC" queries\n", 6641 pta_stats.pt_solution_includes_no_alias, 6642 pta_stats.pt_solution_includes_no_alias 6643 + pta_stats.pt_solution_includes_may_alias); 6644 fprintf (s, " pt_solutions_intersect: " 6645 HOST_WIDE_INT_PRINT_DEC" disambiguations, " 6646 HOST_WIDE_INT_PRINT_DEC" queries\n", 6647 pta_stats.pt_solutions_intersect_no_alias, 6648 pta_stats.pt_solutions_intersect_no_alias 6649 + pta_stats.pt_solutions_intersect_may_alias); 6650 } 6651 6652 6653 /* Reset the points-to solution *PT to a conservative default 6654 (point to anything). */ 6655 6656 void 6657 pt_solution_reset (struct pt_solution *pt) 6658 { 6659 memset (pt, 0, sizeof (struct pt_solution)); 6660 pt->anything = true; 6661 pt->null = true; 6662 } 6663 6664 /* Set the points-to solution *PT to point only to the variables 6665 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains 6666 global variables and VARS_CONTAINS_RESTRICT specifies whether 6667 it contains restrict tag variables. */ 6668 6669 void 6670 pt_solution_set (struct pt_solution *pt, bitmap vars, 6671 bool vars_contains_nonlocal) 6672 { 6673 memset (pt, 0, sizeof (struct pt_solution)); 6674 pt->vars = vars; 6675 pt->vars_contains_nonlocal = vars_contains_nonlocal; 6676 pt->vars_contains_escaped 6677 = (cfun->gimple_df->escaped.anything 6678 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars)); 6679 } 6680 6681 /* Set the points-to solution *PT to point only to the variable VAR. */ 6682 6683 void 6684 pt_solution_set_var (struct pt_solution *pt, tree var) 6685 { 6686 memset (pt, 0, sizeof (struct pt_solution)); 6687 pt->vars = BITMAP_GGC_ALLOC (); 6688 bitmap_set_bit (pt->vars, DECL_PT_UID (var)); 6689 pt->vars_contains_nonlocal = is_global_var (var); 6690 pt->vars_contains_escaped 6691 = (cfun->gimple_df->escaped.anything 6692 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var))); 6693 } 6694 6695 /* Computes the union of the points-to solutions *DEST and *SRC and 6696 stores the result in *DEST. This changes the points-to bitmap 6697 of *DEST and thus may not be used if that might be shared. 6698 The points-to bitmap of *SRC and *DEST will not be shared after 6699 this function if they were not before. */ 6700 6701 static void 6702 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src) 6703 { 6704 dest->anything |= src->anything; 6705 if (dest->anything) 6706 { 6707 pt_solution_reset (dest); 6708 return; 6709 } 6710 6711 dest->nonlocal |= src->nonlocal; 6712 dest->escaped |= src->escaped; 6713 dest->ipa_escaped |= src->ipa_escaped; 6714 dest->null |= src->null; 6715 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal; 6716 dest->vars_contains_escaped |= src->vars_contains_escaped; 6717 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap; 6718 if (!src->vars) 6719 return; 6720 6721 if (!dest->vars) 6722 dest->vars = BITMAP_GGC_ALLOC (); 6723 bitmap_ior_into (dest->vars, src->vars); 6724 } 6725 6726 /* Return true if the points-to solution *PT is empty. */ 6727 6728 bool 6729 pt_solution_empty_p (struct pt_solution *pt) 6730 { 6731 if (pt->anything 6732 || pt->nonlocal) 6733 return false; 6734 6735 if (pt->vars 6736 && !bitmap_empty_p (pt->vars)) 6737 return false; 6738 6739 /* If the solution includes ESCAPED, check if that is empty. */ 6740 if (pt->escaped 6741 && !pt_solution_empty_p (&cfun->gimple_df->escaped)) 6742 return false; 6743 6744 /* If the solution includes ESCAPED, check if that is empty. */ 6745 if (pt->ipa_escaped 6746 && !pt_solution_empty_p (&ipa_escaped_pt)) 6747 return false; 6748 6749 return true; 6750 } 6751 6752 /* Return true if the points-to solution *PT only point to a single var, and 6753 return the var uid in *UID. */ 6754 6755 bool 6756 pt_solution_singleton_or_null_p (struct pt_solution *pt, unsigned *uid) 6757 { 6758 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped 6759 || pt->vars == NULL 6760 || !bitmap_single_bit_set_p (pt->vars)) 6761 return false; 6762 6763 *uid = bitmap_first_set_bit (pt->vars); 6764 return true; 6765 } 6766 6767 /* Return true if the points-to solution *PT includes global memory. */ 6768 6769 bool 6770 pt_solution_includes_global (struct pt_solution *pt) 6771 { 6772 if (pt->anything 6773 || pt->nonlocal 6774 || pt->vars_contains_nonlocal 6775 /* The following is a hack to make the malloc escape hack work. 6776 In reality we'd need different sets for escaped-through-return 6777 and escaped-to-callees and passes would need to be updated. */ 6778 || pt->vars_contains_escaped_heap) 6779 return true; 6780 6781 /* 'escaped' is also a placeholder so we have to look into it. */ 6782 if (pt->escaped) 6783 return pt_solution_includes_global (&cfun->gimple_df->escaped); 6784 6785 if (pt->ipa_escaped) 6786 return pt_solution_includes_global (&ipa_escaped_pt); 6787 6788 return false; 6789 } 6790 6791 /* Return true if the points-to solution *PT includes the variable 6792 declaration DECL. */ 6793 6794 static bool 6795 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl) 6796 { 6797 if (pt->anything) 6798 return true; 6799 6800 if (pt->nonlocal 6801 && is_global_var (decl)) 6802 return true; 6803 6804 if (pt->vars 6805 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl))) 6806 return true; 6807 6808 /* If the solution includes ESCAPED, check it. */ 6809 if (pt->escaped 6810 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl)) 6811 return true; 6812 6813 /* If the solution includes ESCAPED, check it. */ 6814 if (pt->ipa_escaped 6815 && pt_solution_includes_1 (&ipa_escaped_pt, decl)) 6816 return true; 6817 6818 return false; 6819 } 6820 6821 bool 6822 pt_solution_includes (struct pt_solution *pt, const_tree decl) 6823 { 6824 bool res = pt_solution_includes_1 (pt, decl); 6825 if (res) 6826 ++pta_stats.pt_solution_includes_may_alias; 6827 else 6828 ++pta_stats.pt_solution_includes_no_alias; 6829 return res; 6830 } 6831 6832 /* Return true if both points-to solutions PT1 and PT2 have a non-empty 6833 intersection. */ 6834 6835 static bool 6836 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2) 6837 { 6838 if (pt1->anything || pt2->anything) 6839 return true; 6840 6841 /* If either points to unknown global memory and the other points to 6842 any global memory they alias. */ 6843 if ((pt1->nonlocal 6844 && (pt2->nonlocal 6845 || pt2->vars_contains_nonlocal)) 6846 || (pt2->nonlocal 6847 && pt1->vars_contains_nonlocal)) 6848 return true; 6849 6850 /* If either points to all escaped memory and the other points to 6851 any escaped memory they alias. */ 6852 if ((pt1->escaped 6853 && (pt2->escaped 6854 || pt2->vars_contains_escaped)) 6855 || (pt2->escaped 6856 && pt1->vars_contains_escaped)) 6857 return true; 6858 6859 /* Check the escaped solution if required. 6860 ??? Do we need to check the local against the IPA escaped sets? */ 6861 if ((pt1->ipa_escaped || pt2->ipa_escaped) 6862 && !pt_solution_empty_p (&ipa_escaped_pt)) 6863 { 6864 /* If both point to escaped memory and that solution 6865 is not empty they alias. */ 6866 if (pt1->ipa_escaped && pt2->ipa_escaped) 6867 return true; 6868 6869 /* If either points to escaped memory see if the escaped solution 6870 intersects with the other. */ 6871 if ((pt1->ipa_escaped 6872 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2)) 6873 || (pt2->ipa_escaped 6874 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1))) 6875 return true; 6876 } 6877 6878 /* Now both pointers alias if their points-to solution intersects. */ 6879 return (pt1->vars 6880 && pt2->vars 6881 && bitmap_intersect_p (pt1->vars, pt2->vars)); 6882 } 6883 6884 bool 6885 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2) 6886 { 6887 bool res = pt_solutions_intersect_1 (pt1, pt2); 6888 if (res) 6889 ++pta_stats.pt_solutions_intersect_may_alias; 6890 else 6891 ++pta_stats.pt_solutions_intersect_no_alias; 6892 return res; 6893 } 6894 6895 6896 /* Dump points-to information to OUTFILE. */ 6897 6898 static void 6899 dump_sa_points_to_info (FILE *outfile) 6900 { 6901 unsigned int i; 6902 6903 fprintf (outfile, "\nPoints-to sets\n\n"); 6904 6905 if (dump_flags & TDF_STATS) 6906 { 6907 fprintf (outfile, "Stats:\n"); 6908 fprintf (outfile, "Total vars: %d\n", stats.total_vars); 6909 fprintf (outfile, "Non-pointer vars: %d\n", 6910 stats.nonpointer_vars); 6911 fprintf (outfile, "Statically unified vars: %d\n", 6912 stats.unified_vars_static); 6913 fprintf (outfile, "Dynamically unified vars: %d\n", 6914 stats.unified_vars_dynamic); 6915 fprintf (outfile, "Iterations: %d\n", stats.iterations); 6916 fprintf (outfile, "Number of edges: %d\n", stats.num_edges); 6917 fprintf (outfile, "Number of implicit edges: %d\n", 6918 stats.num_implicit_edges); 6919 } 6920 6921 for (i = 1; i < varmap.length (); i++) 6922 { 6923 varinfo_t vi = get_varinfo (i); 6924 if (!vi->may_have_pointers) 6925 continue; 6926 dump_solution_for_var (outfile, i); 6927 } 6928 } 6929 6930 6931 /* Debug points-to information to stderr. */ 6932 6933 DEBUG_FUNCTION void 6934 debug_sa_points_to_info (void) 6935 { 6936 dump_sa_points_to_info (stderr); 6937 } 6938 6939 6940 /* Initialize the always-existing constraint variables for NULL 6941 ANYTHING, READONLY, and INTEGER */ 6942 6943 static void 6944 init_base_vars (void) 6945 { 6946 struct constraint_expr lhs, rhs; 6947 varinfo_t var_anything; 6948 varinfo_t var_nothing; 6949 varinfo_t var_string; 6950 varinfo_t var_escaped; 6951 varinfo_t var_nonlocal; 6952 varinfo_t var_storedanything; 6953 varinfo_t var_integer; 6954 6955 /* Variable ID zero is reserved and should be NULL. */ 6956 varmap.safe_push (NULL); 6957 6958 /* Create the NULL variable, used to represent that a variable points 6959 to NULL. */ 6960 var_nothing = new_var_info (NULL_TREE, "NULL", false); 6961 gcc_assert (var_nothing->id == nothing_id); 6962 var_nothing->is_artificial_var = 1; 6963 var_nothing->offset = 0; 6964 var_nothing->size = ~0; 6965 var_nothing->fullsize = ~0; 6966 var_nothing->is_special_var = 1; 6967 var_nothing->may_have_pointers = 0; 6968 var_nothing->is_global_var = 0; 6969 6970 /* Create the ANYTHING variable, used to represent that a variable 6971 points to some unknown piece of memory. */ 6972 var_anything = new_var_info (NULL_TREE, "ANYTHING", false); 6973 gcc_assert (var_anything->id == anything_id); 6974 var_anything->is_artificial_var = 1; 6975 var_anything->size = ~0; 6976 var_anything->offset = 0; 6977 var_anything->fullsize = ~0; 6978 var_anything->is_special_var = 1; 6979 6980 /* Anything points to anything. This makes deref constraints just 6981 work in the presence of linked list and other p = *p type loops, 6982 by saying that *ANYTHING = ANYTHING. */ 6983 lhs.type = SCALAR; 6984 lhs.var = anything_id; 6985 lhs.offset = 0; 6986 rhs.type = ADDRESSOF; 6987 rhs.var = anything_id; 6988 rhs.offset = 0; 6989 6990 /* This specifically does not use process_constraint because 6991 process_constraint ignores all anything = anything constraints, since all 6992 but this one are redundant. */ 6993 constraints.safe_push (new_constraint (lhs, rhs)); 6994 6995 /* Create the STRING variable, used to represent that a variable 6996 points to a string literal. String literals don't contain 6997 pointers so STRING doesn't point to anything. */ 6998 var_string = new_var_info (NULL_TREE, "STRING", false); 6999 gcc_assert (var_string->id == string_id); 7000 var_string->is_artificial_var = 1; 7001 var_string->offset = 0; 7002 var_string->size = ~0; 7003 var_string->fullsize = ~0; 7004 var_string->is_special_var = 1; 7005 var_string->may_have_pointers = 0; 7006 7007 /* Create the ESCAPED variable, used to represent the set of escaped 7008 memory. */ 7009 var_escaped = new_var_info (NULL_TREE, "ESCAPED", false); 7010 gcc_assert (var_escaped->id == escaped_id); 7011 var_escaped->is_artificial_var = 1; 7012 var_escaped->offset = 0; 7013 var_escaped->size = ~0; 7014 var_escaped->fullsize = ~0; 7015 var_escaped->is_special_var = 0; 7016 7017 /* Create the NONLOCAL variable, used to represent the set of nonlocal 7018 memory. */ 7019 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL", false); 7020 gcc_assert (var_nonlocal->id == nonlocal_id); 7021 var_nonlocal->is_artificial_var = 1; 7022 var_nonlocal->offset = 0; 7023 var_nonlocal->size = ~0; 7024 var_nonlocal->fullsize = ~0; 7025 var_nonlocal->is_special_var = 1; 7026 7027 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */ 7028 lhs.type = SCALAR; 7029 lhs.var = escaped_id; 7030 lhs.offset = 0; 7031 rhs.type = DEREF; 7032 rhs.var = escaped_id; 7033 rhs.offset = 0; 7034 process_constraint (new_constraint (lhs, rhs)); 7035 7036 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the 7037 whole variable escapes. */ 7038 lhs.type = SCALAR; 7039 lhs.var = escaped_id; 7040 lhs.offset = 0; 7041 rhs.type = SCALAR; 7042 rhs.var = escaped_id; 7043 rhs.offset = UNKNOWN_OFFSET; 7044 process_constraint (new_constraint (lhs, rhs)); 7045 7046 /* *ESCAPED = NONLOCAL. This is true because we have to assume 7047 everything pointed to by escaped points to what global memory can 7048 point to. */ 7049 lhs.type = DEREF; 7050 lhs.var = escaped_id; 7051 lhs.offset = 0; 7052 rhs.type = SCALAR; 7053 rhs.var = nonlocal_id; 7054 rhs.offset = 0; 7055 process_constraint (new_constraint (lhs, rhs)); 7056 7057 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because 7058 global memory may point to global memory and escaped memory. */ 7059 lhs.type = SCALAR; 7060 lhs.var = nonlocal_id; 7061 lhs.offset = 0; 7062 rhs.type = ADDRESSOF; 7063 rhs.var = nonlocal_id; 7064 rhs.offset = 0; 7065 process_constraint (new_constraint (lhs, rhs)); 7066 rhs.type = ADDRESSOF; 7067 rhs.var = escaped_id; 7068 rhs.offset = 0; 7069 process_constraint (new_constraint (lhs, rhs)); 7070 7071 /* Create the STOREDANYTHING variable, used to represent the set of 7072 variables stored to *ANYTHING. */ 7073 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING", false); 7074 gcc_assert (var_storedanything->id == storedanything_id); 7075 var_storedanything->is_artificial_var = 1; 7076 var_storedanything->offset = 0; 7077 var_storedanything->size = ~0; 7078 var_storedanything->fullsize = ~0; 7079 var_storedanything->is_special_var = 0; 7080 7081 /* Create the INTEGER variable, used to represent that a variable points 7082 to what an INTEGER "points to". */ 7083 var_integer = new_var_info (NULL_TREE, "INTEGER", false); 7084 gcc_assert (var_integer->id == integer_id); 7085 var_integer->is_artificial_var = 1; 7086 var_integer->size = ~0; 7087 var_integer->fullsize = ~0; 7088 var_integer->offset = 0; 7089 var_integer->is_special_var = 1; 7090 7091 /* INTEGER = ANYTHING, because we don't know where a dereference of 7092 a random integer will point to. */ 7093 lhs.type = SCALAR; 7094 lhs.var = integer_id; 7095 lhs.offset = 0; 7096 rhs.type = ADDRESSOF; 7097 rhs.var = anything_id; 7098 rhs.offset = 0; 7099 process_constraint (new_constraint (lhs, rhs)); 7100 } 7101 7102 /* Initialize things necessary to perform PTA */ 7103 7104 static void 7105 init_alias_vars (void) 7106 { 7107 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1); 7108 7109 bitmap_obstack_initialize (&pta_obstack); 7110 bitmap_obstack_initialize (&oldpta_obstack); 7111 bitmap_obstack_initialize (&predbitmap_obstack); 7112 7113 constraints.create (8); 7114 varmap.create (8); 7115 vi_for_tree = new hash_map<tree, varinfo_t>; 7116 call_stmt_vars = new hash_map<gimple *, varinfo_t>; 7117 7118 memset (&stats, 0, sizeof (stats)); 7119 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511); 7120 init_base_vars (); 7121 7122 gcc_obstack_init (&fake_var_decl_obstack); 7123 7124 final_solutions = new hash_map<varinfo_t, pt_solution *>; 7125 gcc_obstack_init (&final_solutions_obstack); 7126 } 7127 7128 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the 7129 predecessor edges. */ 7130 7131 static void 7132 remove_preds_and_fake_succs (constraint_graph_t graph) 7133 { 7134 unsigned int i; 7135 7136 /* Clear the implicit ref and address nodes from the successor 7137 lists. */ 7138 for (i = 1; i < FIRST_REF_NODE; i++) 7139 { 7140 if (graph->succs[i]) 7141 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE, 7142 FIRST_REF_NODE * 2); 7143 } 7144 7145 /* Free the successor list for the non-ref nodes. */ 7146 for (i = FIRST_REF_NODE + 1; i < graph->size; i++) 7147 { 7148 if (graph->succs[i]) 7149 BITMAP_FREE (graph->succs[i]); 7150 } 7151 7152 /* Now reallocate the size of the successor list as, and blow away 7153 the predecessor bitmaps. */ 7154 graph->size = varmap.length (); 7155 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size); 7156 7157 free (graph->implicit_preds); 7158 graph->implicit_preds = NULL; 7159 free (graph->preds); 7160 graph->preds = NULL; 7161 bitmap_obstack_release (&predbitmap_obstack); 7162 } 7163 7164 /* Solve the constraint set. */ 7165 7166 static void 7167 solve_constraints (void) 7168 { 7169 struct scc_info *si; 7170 7171 /* Sort varinfos so that ones that cannot be pointed to are last. 7172 This makes bitmaps more efficient. */ 7173 unsigned int *map = XNEWVEC (unsigned int, varmap.length ()); 7174 for (unsigned i = 0; i < integer_id + 1; ++i) 7175 map[i] = i; 7176 /* Start with non-register vars (as possibly address-taken), followed 7177 by register vars as conservative set of vars never appearing in 7178 the points-to solution bitmaps. */ 7179 unsigned j = integer_id + 1; 7180 for (unsigned i = integer_id + 1; i < varmap.length (); ++i) 7181 if (! varmap[i]->is_reg_var) 7182 map[i] = j++; 7183 for (unsigned i = integer_id + 1; i < varmap.length (); ++i) 7184 if (varmap[i]->is_reg_var) 7185 map[i] = j++; 7186 /* Shuffle varmap according to map. */ 7187 for (unsigned i = integer_id + 1; i < varmap.length (); ++i) 7188 { 7189 while (map[varmap[i]->id] != i) 7190 std::swap (varmap[i], varmap[map[varmap[i]->id]]); 7191 gcc_assert (bitmap_empty_p (varmap[i]->solution)); 7192 varmap[i]->id = i; 7193 varmap[i]->next = map[varmap[i]->next]; 7194 varmap[i]->head = map[varmap[i]->head]; 7195 } 7196 /* Finally rewrite constraints. */ 7197 for (unsigned i = 0; i < constraints.length (); ++i) 7198 { 7199 constraints[i]->lhs.var = map[constraints[i]->lhs.var]; 7200 constraints[i]->rhs.var = map[constraints[i]->rhs.var]; 7201 } 7202 free (map); 7203 7204 if (dump_file) 7205 fprintf (dump_file, 7206 "\nCollapsing static cycles and doing variable " 7207 "substitution\n"); 7208 7209 init_graph (varmap.length () * 2); 7210 7211 if (dump_file) 7212 fprintf (dump_file, "Building predecessor graph\n"); 7213 build_pred_graph (); 7214 7215 if (dump_file) 7216 fprintf (dump_file, "Detecting pointer and location " 7217 "equivalences\n"); 7218 si = perform_var_substitution (graph); 7219 7220 if (dump_file) 7221 fprintf (dump_file, "Rewriting constraints and unifying " 7222 "variables\n"); 7223 rewrite_constraints (graph, si); 7224 7225 build_succ_graph (); 7226 7227 free_var_substitution_info (si); 7228 7229 /* Attach complex constraints to graph nodes. */ 7230 move_complex_constraints (graph); 7231 7232 if (dump_file) 7233 fprintf (dump_file, "Uniting pointer but not location equivalent " 7234 "variables\n"); 7235 unite_pointer_equivalences (graph); 7236 7237 if (dump_file) 7238 fprintf (dump_file, "Finding indirect cycles\n"); 7239 find_indirect_cycles (graph); 7240 7241 /* Implicit nodes and predecessors are no longer necessary at this 7242 point. */ 7243 remove_preds_and_fake_succs (graph); 7244 7245 if (dump_file && (dump_flags & TDF_GRAPH)) 7246 { 7247 fprintf (dump_file, "\n\n// The constraint graph before solve-graph " 7248 "in dot format:\n"); 7249 dump_constraint_graph (dump_file); 7250 fprintf (dump_file, "\n\n"); 7251 } 7252 7253 if (dump_file) 7254 fprintf (dump_file, "Solving graph\n"); 7255 7256 solve_graph (graph); 7257 7258 if (dump_file && (dump_flags & TDF_GRAPH)) 7259 { 7260 fprintf (dump_file, "\n\n// The constraint graph after solve-graph " 7261 "in dot format:\n"); 7262 dump_constraint_graph (dump_file); 7263 fprintf (dump_file, "\n\n"); 7264 } 7265 7266 if (dump_file) 7267 dump_sa_points_to_info (dump_file); 7268 } 7269 7270 /* Create points-to sets for the current function. See the comments 7271 at the start of the file for an algorithmic overview. */ 7272 7273 static void 7274 compute_points_to_sets (void) 7275 { 7276 basic_block bb; 7277 varinfo_t vi; 7278 7279 timevar_push (TV_TREE_PTA); 7280 7281 init_alias_vars (); 7282 7283 intra_create_variable_infos (cfun); 7284 7285 /* Now walk all statements and build the constraint set. */ 7286 FOR_EACH_BB_FN (bb, cfun) 7287 { 7288 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); 7289 gsi_next (&gsi)) 7290 { 7291 gphi *phi = gsi.phi (); 7292 7293 if (! virtual_operand_p (gimple_phi_result (phi))) 7294 find_func_aliases (cfun, phi); 7295 } 7296 7297 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); 7298 gsi_next (&gsi)) 7299 { 7300 gimple *stmt = gsi_stmt (gsi); 7301 7302 find_func_aliases (cfun, stmt); 7303 } 7304 } 7305 7306 if (dump_file) 7307 { 7308 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n"); 7309 dump_constraints (dump_file, 0); 7310 } 7311 7312 /* From the constraints compute the points-to sets. */ 7313 solve_constraints (); 7314 7315 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */ 7316 cfun->gimple_df->escaped = find_what_var_points_to (cfun->decl, 7317 get_varinfo (escaped_id)); 7318 7319 /* Make sure the ESCAPED solution (which is used as placeholder in 7320 other solutions) does not reference itself. This simplifies 7321 points-to solution queries. */ 7322 cfun->gimple_df->escaped.escaped = 0; 7323 7324 /* Compute the points-to sets for pointer SSA_NAMEs. */ 7325 unsigned i; 7326 tree ptr; 7327 7328 FOR_EACH_SSA_NAME (i, ptr, cfun) 7329 { 7330 if (POINTER_TYPE_P (TREE_TYPE (ptr))) 7331 find_what_p_points_to (cfun->decl, ptr); 7332 } 7333 7334 /* Compute the call-used/clobbered sets. */ 7335 FOR_EACH_BB_FN (bb, cfun) 7336 { 7337 gimple_stmt_iterator gsi; 7338 7339 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 7340 { 7341 gcall *stmt; 7342 struct pt_solution *pt; 7343 7344 stmt = dyn_cast <gcall *> (gsi_stmt (gsi)); 7345 if (!stmt) 7346 continue; 7347 7348 pt = gimple_call_use_set (stmt); 7349 if (gimple_call_flags (stmt) & ECF_CONST) 7350 memset (pt, 0, sizeof (struct pt_solution)); 7351 else if ((vi = lookup_call_use_vi (stmt)) != NULL) 7352 { 7353 *pt = find_what_var_points_to (cfun->decl, vi); 7354 /* Escaped (and thus nonlocal) variables are always 7355 implicitly used by calls. */ 7356 /* ??? ESCAPED can be empty even though NONLOCAL 7357 always escaped. */ 7358 pt->nonlocal = 1; 7359 pt->escaped = 1; 7360 } 7361 else 7362 { 7363 /* If there is nothing special about this call then 7364 we have made everything that is used also escape. */ 7365 *pt = cfun->gimple_df->escaped; 7366 pt->nonlocal = 1; 7367 } 7368 7369 pt = gimple_call_clobber_set (stmt); 7370 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS)) 7371 memset (pt, 0, sizeof (struct pt_solution)); 7372 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL) 7373 { 7374 *pt = find_what_var_points_to (cfun->decl, vi); 7375 /* Escaped (and thus nonlocal) variables are always 7376 implicitly clobbered by calls. */ 7377 /* ??? ESCAPED can be empty even though NONLOCAL 7378 always escaped. */ 7379 pt->nonlocal = 1; 7380 pt->escaped = 1; 7381 } 7382 else 7383 { 7384 /* If there is nothing special about this call then 7385 we have made everything that is used also escape. */ 7386 *pt = cfun->gimple_df->escaped; 7387 pt->nonlocal = 1; 7388 } 7389 } 7390 } 7391 7392 timevar_pop (TV_TREE_PTA); 7393 } 7394 7395 7396 /* Delete created points-to sets. */ 7397 7398 static void 7399 delete_points_to_sets (void) 7400 { 7401 unsigned int i; 7402 7403 delete shared_bitmap_table; 7404 shared_bitmap_table = NULL; 7405 if (dump_file && (dump_flags & TDF_STATS)) 7406 fprintf (dump_file, "Points to sets created:%d\n", 7407 stats.points_to_sets_created); 7408 7409 delete vi_for_tree; 7410 delete call_stmt_vars; 7411 bitmap_obstack_release (&pta_obstack); 7412 constraints.release (); 7413 7414 for (i = 0; i < graph->size; i++) 7415 graph->complex[i].release (); 7416 free (graph->complex); 7417 7418 free (graph->rep); 7419 free (graph->succs); 7420 free (graph->pe); 7421 free (graph->pe_rep); 7422 free (graph->indirect_cycles); 7423 free (graph); 7424 7425 varmap.release (); 7426 variable_info_pool.release (); 7427 constraint_pool.release (); 7428 7429 obstack_free (&fake_var_decl_obstack, NULL); 7430 7431 delete final_solutions; 7432 obstack_free (&final_solutions_obstack, NULL); 7433 } 7434 7435 struct vls_data 7436 { 7437 unsigned short clique; 7438 bool escaped_p; 7439 bitmap rvars; 7440 }; 7441 7442 /* Mark "other" loads and stores as belonging to CLIQUE and with 7443 base zero. */ 7444 7445 static bool 7446 visit_loadstore (gimple *, tree base, tree ref, void *data) 7447 { 7448 unsigned short clique = ((vls_data *) data)->clique; 7449 bitmap rvars = ((vls_data *) data)->rvars; 7450 bool escaped_p = ((vls_data *) data)->escaped_p; 7451 if (TREE_CODE (base) == MEM_REF 7452 || TREE_CODE (base) == TARGET_MEM_REF) 7453 { 7454 tree ptr = TREE_OPERAND (base, 0); 7455 if (TREE_CODE (ptr) == SSA_NAME) 7456 { 7457 /* For parameters, get at the points-to set for the actual parm 7458 decl. */ 7459 if (SSA_NAME_IS_DEFAULT_DEF (ptr) 7460 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL 7461 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL)) 7462 ptr = SSA_NAME_VAR (ptr); 7463 7464 /* We need to make sure 'ptr' doesn't include any of 7465 the restrict tags we added bases for in its points-to set. */ 7466 varinfo_t vi = lookup_vi_for_tree (ptr); 7467 if (! vi) 7468 return false; 7469 7470 vi = get_varinfo (find (vi->id)); 7471 if (bitmap_intersect_p (rvars, vi->solution) 7472 || (escaped_p && bitmap_bit_p (vi->solution, escaped_id))) 7473 return false; 7474 } 7475 7476 /* Do not overwrite existing cliques (that includes clique, base 7477 pairs we just set). */ 7478 if (MR_DEPENDENCE_CLIQUE (base) == 0) 7479 { 7480 MR_DEPENDENCE_CLIQUE (base) = clique; 7481 MR_DEPENDENCE_BASE (base) = 0; 7482 } 7483 } 7484 7485 /* For plain decl accesses see whether they are accesses to globals 7486 and rewrite them to MEM_REFs with { clique, 0 }. */ 7487 if (VAR_P (base) 7488 && is_global_var (base) 7489 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store 7490 ops callback. */ 7491 && base != ref) 7492 { 7493 tree *basep = &ref; 7494 while (handled_component_p (*basep)) 7495 basep = &TREE_OPERAND (*basep, 0); 7496 gcc_assert (VAR_P (*basep)); 7497 tree ptr = build_fold_addr_expr (*basep); 7498 tree zero = build_int_cst (TREE_TYPE (ptr), 0); 7499 *basep = build2 (MEM_REF, TREE_TYPE (*basep), ptr, zero); 7500 MR_DEPENDENCE_CLIQUE (*basep) = clique; 7501 MR_DEPENDENCE_BASE (*basep) = 0; 7502 } 7503 7504 return false; 7505 } 7506 7507 struct msdi_data { 7508 tree ptr; 7509 unsigned short *clique; 7510 unsigned short *last_ruid; 7511 varinfo_t restrict_var; 7512 }; 7513 7514 /* If BASE is a MEM_REF then assign a clique, base pair to it, updating 7515 CLIQUE, *RESTRICT_VAR and LAST_RUID as passed via DATA. 7516 Return whether dependence info was assigned to BASE. */ 7517 7518 static bool 7519 maybe_set_dependence_info (gimple *, tree base, tree, void *data) 7520 { 7521 tree ptr = ((msdi_data *)data)->ptr; 7522 unsigned short &clique = *((msdi_data *)data)->clique; 7523 unsigned short &last_ruid = *((msdi_data *)data)->last_ruid; 7524 varinfo_t restrict_var = ((msdi_data *)data)->restrict_var; 7525 if ((TREE_CODE (base) == MEM_REF 7526 || TREE_CODE (base) == TARGET_MEM_REF) 7527 && TREE_OPERAND (base, 0) == ptr) 7528 { 7529 /* Do not overwrite existing cliques. This avoids overwriting dependence 7530 info inlined from a function with restrict parameters inlined 7531 into a function with restrict parameters. This usually means we 7532 prefer to be precise in innermost loops. */ 7533 if (MR_DEPENDENCE_CLIQUE (base) == 0) 7534 { 7535 if (clique == 0) 7536 { 7537 if (cfun->last_clique == 0) 7538 cfun->last_clique = 1; 7539 clique = 1; 7540 } 7541 if (restrict_var->ruid == 0) 7542 restrict_var->ruid = ++last_ruid; 7543 MR_DEPENDENCE_CLIQUE (base) = clique; 7544 MR_DEPENDENCE_BASE (base) = restrict_var->ruid; 7545 return true; 7546 } 7547 } 7548 return false; 7549 } 7550 7551 /* Clear dependence info for the clique DATA. */ 7552 7553 static bool 7554 clear_dependence_clique (gimple *, tree base, tree, void *data) 7555 { 7556 unsigned short clique = (uintptr_t)data; 7557 if ((TREE_CODE (base) == MEM_REF 7558 || TREE_CODE (base) == TARGET_MEM_REF) 7559 && MR_DEPENDENCE_CLIQUE (base) == clique) 7560 { 7561 MR_DEPENDENCE_CLIQUE (base) = 0; 7562 MR_DEPENDENCE_BASE (base) = 0; 7563 } 7564 7565 return false; 7566 } 7567 7568 /* Compute the set of independend memory references based on restrict 7569 tags and their conservative propagation to the points-to sets. */ 7570 7571 static void 7572 compute_dependence_clique (void) 7573 { 7574 /* First clear the special "local" clique. */ 7575 basic_block bb; 7576 if (cfun->last_clique != 0) 7577 FOR_EACH_BB_FN (bb, cfun) 7578 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); 7579 !gsi_end_p (gsi); gsi_next (&gsi)) 7580 { 7581 gimple *stmt = gsi_stmt (gsi); 7582 walk_stmt_load_store_ops (stmt, (void *)(uintptr_t) 1, 7583 clear_dependence_clique, 7584 clear_dependence_clique); 7585 } 7586 7587 unsigned short clique = 0; 7588 unsigned short last_ruid = 0; 7589 bitmap rvars = BITMAP_ALLOC (NULL); 7590 bool escaped_p = false; 7591 for (unsigned i = 0; i < num_ssa_names; ++i) 7592 { 7593 tree ptr = ssa_name (i); 7594 if (!ptr || !POINTER_TYPE_P (TREE_TYPE (ptr))) 7595 continue; 7596 7597 /* Avoid all this when ptr is not dereferenced? */ 7598 tree p = ptr; 7599 if (SSA_NAME_IS_DEFAULT_DEF (ptr) 7600 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL 7601 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL)) 7602 p = SSA_NAME_VAR (ptr); 7603 varinfo_t vi = lookup_vi_for_tree (p); 7604 if (!vi) 7605 continue; 7606 vi = get_varinfo (find (vi->id)); 7607 bitmap_iterator bi; 7608 unsigned j; 7609 varinfo_t restrict_var = NULL; 7610 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi) 7611 { 7612 varinfo_t oi = get_varinfo (j); 7613 if (oi->is_restrict_var) 7614 { 7615 if (restrict_var) 7616 { 7617 if (dump_file && (dump_flags & TDF_DETAILS)) 7618 { 7619 fprintf (dump_file, "found restrict pointed-to " 7620 "for "); 7621 print_generic_expr (dump_file, ptr); 7622 fprintf (dump_file, " but not exclusively\n"); 7623 } 7624 restrict_var = NULL; 7625 break; 7626 } 7627 restrict_var = oi; 7628 } 7629 /* NULL is the only other valid points-to entry. */ 7630 else if (oi->id != nothing_id) 7631 { 7632 restrict_var = NULL; 7633 break; 7634 } 7635 } 7636 /* Ok, found that ptr must(!) point to a single(!) restrict 7637 variable. */ 7638 /* ??? PTA isn't really a proper propagation engine to compute 7639 this property. 7640 ??? We could handle merging of two restricts by unifying them. */ 7641 if (restrict_var) 7642 { 7643 /* Now look at possible dereferences of ptr. */ 7644 imm_use_iterator ui; 7645 gimple *use_stmt; 7646 bool used = false; 7647 msdi_data data = { ptr, &clique, &last_ruid, restrict_var }; 7648 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr) 7649 used |= walk_stmt_load_store_ops (use_stmt, &data, 7650 maybe_set_dependence_info, 7651 maybe_set_dependence_info); 7652 if (used) 7653 { 7654 /* Add all subvars to the set of restrict pointed-to set. */ 7655 for (unsigned sv = restrict_var->head; sv != 0; 7656 sv = get_varinfo (sv)->next) 7657 bitmap_set_bit (rvars, sv); 7658 varinfo_t escaped = get_varinfo (find (escaped_id)); 7659 if (bitmap_bit_p (escaped->solution, restrict_var->id)) 7660 escaped_p = true; 7661 } 7662 } 7663 } 7664 7665 if (clique != 0) 7666 { 7667 /* Assign the BASE id zero to all accesses not based on a restrict 7668 pointer. That way they get disambiguated against restrict 7669 accesses but not against each other. */ 7670 /* ??? For restricts derived from globals (thus not incoming 7671 parameters) we can't restrict scoping properly thus the following 7672 is too aggressive there. For now we have excluded those globals from 7673 getting into the MR_DEPENDENCE machinery. */ 7674 vls_data data = { clique, escaped_p, rvars }; 7675 basic_block bb; 7676 FOR_EACH_BB_FN (bb, cfun) 7677 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); 7678 !gsi_end_p (gsi); gsi_next (&gsi)) 7679 { 7680 gimple *stmt = gsi_stmt (gsi); 7681 walk_stmt_load_store_ops (stmt, &data, 7682 visit_loadstore, visit_loadstore); 7683 } 7684 } 7685 7686 BITMAP_FREE (rvars); 7687 } 7688 7689 /* Compute points-to information for every SSA_NAME pointer in the 7690 current function and compute the transitive closure of escaped 7691 variables to re-initialize the call-clobber states of local variables. */ 7692 7693 unsigned int 7694 compute_may_aliases (void) 7695 { 7696 if (cfun->gimple_df->ipa_pta) 7697 { 7698 if (dump_file) 7699 { 7700 fprintf (dump_file, "\nNot re-computing points-to information " 7701 "because IPA points-to information is available.\n\n"); 7702 7703 /* But still dump what we have remaining it. */ 7704 dump_alias_info (dump_file); 7705 } 7706 7707 return 0; 7708 } 7709 7710 /* For each pointer P_i, determine the sets of variables that P_i may 7711 point-to. Compute the reachability set of escaped and call-used 7712 variables. */ 7713 compute_points_to_sets (); 7714 7715 /* Debugging dumps. */ 7716 if (dump_file) 7717 dump_alias_info (dump_file); 7718 7719 /* Compute restrict-based memory disambiguations. */ 7720 compute_dependence_clique (); 7721 7722 /* Deallocate memory used by aliasing data structures and the internal 7723 points-to solution. */ 7724 delete_points_to_sets (); 7725 7726 gcc_assert (!need_ssa_update_p (cfun)); 7727 7728 return 0; 7729 } 7730 7731 /* A dummy pass to cause points-to information to be computed via 7732 TODO_rebuild_alias. */ 7733 7734 namespace { 7735 7736 const pass_data pass_data_build_alias = 7737 { 7738 GIMPLE_PASS, /* type */ 7739 "alias", /* name */ 7740 OPTGROUP_NONE, /* optinfo_flags */ 7741 TV_NONE, /* tv_id */ 7742 ( PROP_cfg | PROP_ssa ), /* properties_required */ 7743 0, /* properties_provided */ 7744 0, /* properties_destroyed */ 7745 0, /* todo_flags_start */ 7746 TODO_rebuild_alias, /* todo_flags_finish */ 7747 }; 7748 7749 class pass_build_alias : public gimple_opt_pass 7750 { 7751 public: 7752 pass_build_alias (gcc::context *ctxt) 7753 : gimple_opt_pass (pass_data_build_alias, ctxt) 7754 {} 7755 7756 /* opt_pass methods: */ 7757 virtual bool gate (function *) { return flag_tree_pta; } 7758 7759 }; // class pass_build_alias 7760 7761 } // anon namespace 7762 7763 gimple_opt_pass * 7764 make_pass_build_alias (gcc::context *ctxt) 7765 { 7766 return new pass_build_alias (ctxt); 7767 } 7768 7769 /* A dummy pass to cause points-to information to be computed via 7770 TODO_rebuild_alias. */ 7771 7772 namespace { 7773 7774 const pass_data pass_data_build_ealias = 7775 { 7776 GIMPLE_PASS, /* type */ 7777 "ealias", /* name */ 7778 OPTGROUP_NONE, /* optinfo_flags */ 7779 TV_NONE, /* tv_id */ 7780 ( PROP_cfg | PROP_ssa ), /* properties_required */ 7781 0, /* properties_provided */ 7782 0, /* properties_destroyed */ 7783 0, /* todo_flags_start */ 7784 TODO_rebuild_alias, /* todo_flags_finish */ 7785 }; 7786 7787 class pass_build_ealias : public gimple_opt_pass 7788 { 7789 public: 7790 pass_build_ealias (gcc::context *ctxt) 7791 : gimple_opt_pass (pass_data_build_ealias, ctxt) 7792 {} 7793 7794 /* opt_pass methods: */ 7795 virtual bool gate (function *) { return flag_tree_pta; } 7796 7797 }; // class pass_build_ealias 7798 7799 } // anon namespace 7800 7801 gimple_opt_pass * 7802 make_pass_build_ealias (gcc::context *ctxt) 7803 { 7804 return new pass_build_ealias (ctxt); 7805 } 7806 7807 7808 /* IPA PTA solutions for ESCAPED. */ 7809 struct pt_solution ipa_escaped_pt 7810 = { true, false, false, false, false, 7811 false, false, false, false, false, NULL }; 7812 7813 /* Associate node with varinfo DATA. Worker for 7814 cgraph_for_symbol_thunks_and_aliases. */ 7815 static bool 7816 associate_varinfo_to_alias (struct cgraph_node *node, void *data) 7817 { 7818 if ((node->alias 7819 || (node->thunk.thunk_p 7820 && ! node->global.inlined_to)) 7821 && node->analyzed 7822 && !node->ifunc_resolver) 7823 insert_vi_for_tree (node->decl, (varinfo_t)data); 7824 return false; 7825 } 7826 7827 /* Dump varinfo VI to FILE. */ 7828 7829 static void 7830 dump_varinfo (FILE *file, varinfo_t vi) 7831 { 7832 if (vi == NULL) 7833 return; 7834 7835 fprintf (file, "%u: %s\n", vi->id, vi->name); 7836 7837 const char *sep = " "; 7838 if (vi->is_artificial_var) 7839 fprintf (file, "%sartificial", sep); 7840 if (vi->is_special_var) 7841 fprintf (file, "%sspecial", sep); 7842 if (vi->is_unknown_size_var) 7843 fprintf (file, "%sunknown-size", sep); 7844 if (vi->is_full_var) 7845 fprintf (file, "%sfull", sep); 7846 if (vi->is_heap_var) 7847 fprintf (file, "%sheap", sep); 7848 if (vi->may_have_pointers) 7849 fprintf (file, "%smay-have-pointers", sep); 7850 if (vi->only_restrict_pointers) 7851 fprintf (file, "%sonly-restrict-pointers", sep); 7852 if (vi->is_restrict_var) 7853 fprintf (file, "%sis-restrict-var", sep); 7854 if (vi->is_global_var) 7855 fprintf (file, "%sglobal", sep); 7856 if (vi->is_ipa_escape_point) 7857 fprintf (file, "%sipa-escape-point", sep); 7858 if (vi->is_fn_info) 7859 fprintf (file, "%sfn-info", sep); 7860 if (vi->ruid) 7861 fprintf (file, "%srestrict-uid:%u", sep, vi->ruid); 7862 if (vi->next) 7863 fprintf (file, "%snext:%u", sep, vi->next); 7864 if (vi->head != vi->id) 7865 fprintf (file, "%shead:%u", sep, vi->head); 7866 if (vi->offset) 7867 fprintf (file, "%soffset:" HOST_WIDE_INT_PRINT_DEC, sep, vi->offset); 7868 if (vi->size != ~(unsigned HOST_WIDE_INT)0) 7869 fprintf (file, "%ssize:" HOST_WIDE_INT_PRINT_DEC, sep, vi->size); 7870 if (vi->fullsize != ~(unsigned HOST_WIDE_INT)0 7871 && vi->fullsize != vi->size) 7872 fprintf (file, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC, sep, 7873 vi->fullsize); 7874 fprintf (file, "\n"); 7875 7876 if (vi->solution && !bitmap_empty_p (vi->solution)) 7877 { 7878 bitmap_iterator bi; 7879 unsigned i; 7880 fprintf (file, " solution: {"); 7881 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) 7882 fprintf (file, " %u", i); 7883 fprintf (file, " }\n"); 7884 } 7885 7886 if (vi->oldsolution && !bitmap_empty_p (vi->oldsolution) 7887 && !bitmap_equal_p (vi->solution, vi->oldsolution)) 7888 { 7889 bitmap_iterator bi; 7890 unsigned i; 7891 fprintf (file, " oldsolution: {"); 7892 EXECUTE_IF_SET_IN_BITMAP (vi->oldsolution, 0, i, bi) 7893 fprintf (file, " %u", i); 7894 fprintf (file, " }\n"); 7895 } 7896 } 7897 7898 /* Dump varinfo VI to stderr. */ 7899 7900 DEBUG_FUNCTION void 7901 debug_varinfo (varinfo_t vi) 7902 { 7903 dump_varinfo (stderr, vi); 7904 } 7905 7906 /* Dump varmap to FILE. */ 7907 7908 static void 7909 dump_varmap (FILE *file) 7910 { 7911 if (varmap.length () == 0) 7912 return; 7913 7914 fprintf (file, "variables:\n"); 7915 7916 for (unsigned int i = 0; i < varmap.length (); ++i) 7917 { 7918 varinfo_t vi = get_varinfo (i); 7919 dump_varinfo (file, vi); 7920 } 7921 7922 fprintf (file, "\n"); 7923 } 7924 7925 /* Dump varmap to stderr. */ 7926 7927 DEBUG_FUNCTION void 7928 debug_varmap (void) 7929 { 7930 dump_varmap (stderr); 7931 } 7932 7933 /* Compute whether node is refered to non-locally. Worker for 7934 cgraph_for_symbol_thunks_and_aliases. */ 7935 static bool 7936 refered_from_nonlocal_fn (struct cgraph_node *node, void *data) 7937 { 7938 bool *nonlocal_p = (bool *)data; 7939 *nonlocal_p |= (node->used_from_other_partition 7940 || node->externally_visible 7941 || node->force_output 7942 || lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl))); 7943 return false; 7944 } 7945 7946 /* Same for varpool nodes. */ 7947 static bool 7948 refered_from_nonlocal_var (struct varpool_node *node, void *data) 7949 { 7950 bool *nonlocal_p = (bool *)data; 7951 *nonlocal_p |= (node->used_from_other_partition 7952 || node->externally_visible 7953 || node->force_output); 7954 return false; 7955 } 7956 7957 /* Execute the driver for IPA PTA. */ 7958 static unsigned int 7959 ipa_pta_execute (void) 7960 { 7961 struct cgraph_node *node; 7962 varpool_node *var; 7963 unsigned int from = 0; 7964 7965 in_ipa_mode = 1; 7966 7967 init_alias_vars (); 7968 7969 if (dump_file && (dump_flags & TDF_DETAILS)) 7970 { 7971 symtab->dump (dump_file); 7972 fprintf (dump_file, "\n"); 7973 } 7974 7975 if (dump_file) 7976 { 7977 fprintf (dump_file, "Generating generic constraints\n\n"); 7978 dump_constraints (dump_file, from); 7979 fprintf (dump_file, "\n"); 7980 from = constraints.length (); 7981 } 7982 7983 /* Build the constraints. */ 7984 FOR_EACH_DEFINED_FUNCTION (node) 7985 { 7986 varinfo_t vi; 7987 /* Nodes without a body are not interesting. Especially do not 7988 visit clones at this point for now - we get duplicate decls 7989 there for inline clones at least. */ 7990 if (!node->has_gimple_body_p () || node->global.inlined_to) 7991 continue; 7992 node->get_body (); 7993 7994 gcc_assert (!node->clone_of); 7995 7996 /* For externally visible or attribute used annotated functions use 7997 local constraints for their arguments. 7998 For local functions we see all callers and thus do not need initial 7999 constraints for parameters. */ 8000 bool nonlocal_p = (node->used_from_other_partition 8001 || node->externally_visible 8002 || node->force_output 8003 || lookup_attribute ("noipa", 8004 DECL_ATTRIBUTES (node->decl))); 8005 node->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn, 8006 &nonlocal_p, true); 8007 8008 vi = create_function_info_for (node->decl, 8009 alias_get_name (node->decl), false, 8010 nonlocal_p); 8011 if (dump_file 8012 && from != constraints.length ()) 8013 { 8014 fprintf (dump_file, 8015 "Generating intial constraints for %s", node->name ()); 8016 if (DECL_ASSEMBLER_NAME_SET_P (node->decl)) 8017 fprintf (dump_file, " (%s)", 8018 IDENTIFIER_POINTER 8019 (DECL_ASSEMBLER_NAME (node->decl))); 8020 fprintf (dump_file, "\n\n"); 8021 dump_constraints (dump_file, from); 8022 fprintf (dump_file, "\n"); 8023 8024 from = constraints.length (); 8025 } 8026 8027 node->call_for_symbol_thunks_and_aliases 8028 (associate_varinfo_to_alias, vi, true); 8029 } 8030 8031 /* Create constraints for global variables and their initializers. */ 8032 FOR_EACH_VARIABLE (var) 8033 { 8034 if (var->alias && var->analyzed) 8035 continue; 8036 8037 varinfo_t vi = get_vi_for_tree (var->decl); 8038 8039 /* For the purpose of IPA PTA unit-local globals are not 8040 escape points. */ 8041 bool nonlocal_p = (var->used_from_other_partition 8042 || var->externally_visible 8043 || var->force_output); 8044 var->call_for_symbol_and_aliases (refered_from_nonlocal_var, 8045 &nonlocal_p, true); 8046 if (nonlocal_p) 8047 vi->is_ipa_escape_point = true; 8048 } 8049 8050 if (dump_file 8051 && from != constraints.length ()) 8052 { 8053 fprintf (dump_file, 8054 "Generating constraints for global initializers\n\n"); 8055 dump_constraints (dump_file, from); 8056 fprintf (dump_file, "\n"); 8057 from = constraints.length (); 8058 } 8059 8060 FOR_EACH_DEFINED_FUNCTION (node) 8061 { 8062 struct function *func; 8063 basic_block bb; 8064 8065 /* Nodes without a body are not interesting. */ 8066 if (!node->has_gimple_body_p () || node->clone_of) 8067 continue; 8068 8069 if (dump_file) 8070 { 8071 fprintf (dump_file, 8072 "Generating constraints for %s", node->name ()); 8073 if (DECL_ASSEMBLER_NAME_SET_P (node->decl)) 8074 fprintf (dump_file, " (%s)", 8075 IDENTIFIER_POINTER 8076 (DECL_ASSEMBLER_NAME (node->decl))); 8077 fprintf (dump_file, "\n"); 8078 } 8079 8080 func = DECL_STRUCT_FUNCTION (node->decl); 8081 gcc_assert (cfun == NULL); 8082 8083 /* Build constriants for the function body. */ 8084 FOR_EACH_BB_FN (bb, func) 8085 { 8086 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); 8087 gsi_next (&gsi)) 8088 { 8089 gphi *phi = gsi.phi (); 8090 8091 if (! virtual_operand_p (gimple_phi_result (phi))) 8092 find_func_aliases (func, phi); 8093 } 8094 8095 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); 8096 gsi_next (&gsi)) 8097 { 8098 gimple *stmt = gsi_stmt (gsi); 8099 8100 find_func_aliases (func, stmt); 8101 find_func_clobbers (func, stmt); 8102 } 8103 } 8104 8105 if (dump_file) 8106 { 8107 fprintf (dump_file, "\n"); 8108 dump_constraints (dump_file, from); 8109 fprintf (dump_file, "\n"); 8110 from = constraints.length (); 8111 } 8112 } 8113 8114 /* From the constraints compute the points-to sets. */ 8115 solve_constraints (); 8116 8117 /* Now post-process solutions to handle locals from different 8118 runtime instantiations coming in through recursive invocations. */ 8119 unsigned shadow_var_cnt = 0; 8120 for (unsigned i = 1; i < varmap.length (); ++i) 8121 { 8122 varinfo_t fi = get_varinfo (i); 8123 if (fi->is_fn_info 8124 && fi->decl) 8125 /* Automatic variables pointed to by their containing functions 8126 parameters need this treatment. */ 8127 for (varinfo_t ai = first_vi_for_offset (fi, fi_parm_base); 8128 ai; ai = vi_next (ai)) 8129 { 8130 varinfo_t vi = get_varinfo (find (ai->id)); 8131 bitmap_iterator bi; 8132 unsigned j; 8133 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi) 8134 { 8135 varinfo_t pt = get_varinfo (j); 8136 if (pt->shadow_var_uid == 0 8137 && pt->decl 8138 && auto_var_in_fn_p (pt->decl, fi->decl)) 8139 { 8140 pt->shadow_var_uid = allocate_decl_uid (); 8141 shadow_var_cnt++; 8142 } 8143 } 8144 } 8145 /* As well as global variables which are another way of passing 8146 arguments to recursive invocations. */ 8147 else if (fi->is_global_var) 8148 { 8149 for (varinfo_t ai = fi; ai; ai = vi_next (ai)) 8150 { 8151 varinfo_t vi = get_varinfo (find (ai->id)); 8152 bitmap_iterator bi; 8153 unsigned j; 8154 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi) 8155 { 8156 varinfo_t pt = get_varinfo (j); 8157 if (pt->shadow_var_uid == 0 8158 && pt->decl 8159 && auto_var_p (pt->decl)) 8160 { 8161 pt->shadow_var_uid = allocate_decl_uid (); 8162 shadow_var_cnt++; 8163 } 8164 } 8165 } 8166 } 8167 } 8168 if (shadow_var_cnt && dump_file && (dump_flags & TDF_DETAILS)) 8169 fprintf (dump_file, "Allocated %u shadow variables for locals " 8170 "maybe leaking into recursive invocations of their containing " 8171 "functions\n", shadow_var_cnt); 8172 8173 /* Compute the global points-to sets for ESCAPED. 8174 ??? Note that the computed escape set is not correct 8175 for the whole unit as we fail to consider graph edges to 8176 externally visible functions. */ 8177 ipa_escaped_pt = find_what_var_points_to (NULL, get_varinfo (escaped_id)); 8178 8179 /* Make sure the ESCAPED solution (which is used as placeholder in 8180 other solutions) does not reference itself. This simplifies 8181 points-to solution queries. */ 8182 ipa_escaped_pt.ipa_escaped = 0; 8183 8184 /* Assign the points-to sets to the SSA names in the unit. */ 8185 FOR_EACH_DEFINED_FUNCTION (node) 8186 { 8187 tree ptr; 8188 struct function *fn; 8189 unsigned i; 8190 basic_block bb; 8191 8192 /* Nodes without a body are not interesting. */ 8193 if (!node->has_gimple_body_p () || node->clone_of) 8194 continue; 8195 8196 fn = DECL_STRUCT_FUNCTION (node->decl); 8197 8198 /* Compute the points-to sets for pointer SSA_NAMEs. */ 8199 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr) 8200 { 8201 if (ptr 8202 && POINTER_TYPE_P (TREE_TYPE (ptr))) 8203 find_what_p_points_to (node->decl, ptr); 8204 } 8205 8206 /* Compute the call-use and call-clobber sets for indirect calls 8207 and calls to external functions. */ 8208 FOR_EACH_BB_FN (bb, fn) 8209 { 8210 gimple_stmt_iterator gsi; 8211 8212 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 8213 { 8214 gcall *stmt; 8215 struct pt_solution *pt; 8216 varinfo_t vi, fi; 8217 tree decl; 8218 8219 stmt = dyn_cast <gcall *> (gsi_stmt (gsi)); 8220 if (!stmt) 8221 continue; 8222 8223 /* Handle direct calls to functions with body. */ 8224 decl = gimple_call_fndecl (stmt); 8225 8226 { 8227 tree called_decl = NULL_TREE; 8228 if (gimple_call_builtin_p (stmt, BUILT_IN_GOMP_PARALLEL)) 8229 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0); 8230 else if (gimple_call_builtin_p (stmt, BUILT_IN_GOACC_PARALLEL)) 8231 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 1), 0); 8232 8233 if (called_decl != NULL_TREE 8234 && !fndecl_maybe_in_other_partition (called_decl)) 8235 decl = called_decl; 8236 } 8237 8238 if (decl 8239 && (fi = lookup_vi_for_tree (decl)) 8240 && fi->is_fn_info) 8241 { 8242 *gimple_call_clobber_set (stmt) 8243 = find_what_var_points_to 8244 (node->decl, first_vi_for_offset (fi, fi_clobbers)); 8245 *gimple_call_use_set (stmt) 8246 = find_what_var_points_to 8247 (node->decl, first_vi_for_offset (fi, fi_uses)); 8248 } 8249 /* Handle direct calls to external functions. */ 8250 else if (decl && (!fi || fi->decl)) 8251 { 8252 pt = gimple_call_use_set (stmt); 8253 if (gimple_call_flags (stmt) & ECF_CONST) 8254 memset (pt, 0, sizeof (struct pt_solution)); 8255 else if ((vi = lookup_call_use_vi (stmt)) != NULL) 8256 { 8257 *pt = find_what_var_points_to (node->decl, vi); 8258 /* Escaped (and thus nonlocal) variables are always 8259 implicitly used by calls. */ 8260 /* ??? ESCAPED can be empty even though NONLOCAL 8261 always escaped. */ 8262 pt->nonlocal = 1; 8263 pt->ipa_escaped = 1; 8264 } 8265 else 8266 { 8267 /* If there is nothing special about this call then 8268 we have made everything that is used also escape. */ 8269 *pt = ipa_escaped_pt; 8270 pt->nonlocal = 1; 8271 } 8272 8273 pt = gimple_call_clobber_set (stmt); 8274 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS)) 8275 memset (pt, 0, sizeof (struct pt_solution)); 8276 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL) 8277 { 8278 *pt = find_what_var_points_to (node->decl, vi); 8279 /* Escaped (and thus nonlocal) variables are always 8280 implicitly clobbered by calls. */ 8281 /* ??? ESCAPED can be empty even though NONLOCAL 8282 always escaped. */ 8283 pt->nonlocal = 1; 8284 pt->ipa_escaped = 1; 8285 } 8286 else 8287 { 8288 /* If there is nothing special about this call then 8289 we have made everything that is used also escape. */ 8290 *pt = ipa_escaped_pt; 8291 pt->nonlocal = 1; 8292 } 8293 } 8294 /* Handle indirect calls. */ 8295 else if ((fi = get_fi_for_callee (stmt))) 8296 { 8297 /* We need to accumulate all clobbers/uses of all possible 8298 callees. */ 8299 fi = get_varinfo (find (fi->id)); 8300 /* If we cannot constrain the set of functions we'll end up 8301 calling we end up using/clobbering everything. */ 8302 if (bitmap_bit_p (fi->solution, anything_id) 8303 || bitmap_bit_p (fi->solution, nonlocal_id) 8304 || bitmap_bit_p (fi->solution, escaped_id)) 8305 { 8306 pt_solution_reset (gimple_call_clobber_set (stmt)); 8307 pt_solution_reset (gimple_call_use_set (stmt)); 8308 } 8309 else 8310 { 8311 bitmap_iterator bi; 8312 unsigned i; 8313 struct pt_solution *uses, *clobbers; 8314 8315 uses = gimple_call_use_set (stmt); 8316 clobbers = gimple_call_clobber_set (stmt); 8317 memset (uses, 0, sizeof (struct pt_solution)); 8318 memset (clobbers, 0, sizeof (struct pt_solution)); 8319 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi) 8320 { 8321 struct pt_solution sol; 8322 8323 vi = get_varinfo (i); 8324 if (!vi->is_fn_info) 8325 { 8326 /* ??? We could be more precise here? */ 8327 uses->nonlocal = 1; 8328 uses->ipa_escaped = 1; 8329 clobbers->nonlocal = 1; 8330 clobbers->ipa_escaped = 1; 8331 continue; 8332 } 8333 8334 if (!uses->anything) 8335 { 8336 sol = find_what_var_points_to 8337 (node->decl, 8338 first_vi_for_offset (vi, fi_uses)); 8339 pt_solution_ior_into (uses, &sol); 8340 } 8341 if (!clobbers->anything) 8342 { 8343 sol = find_what_var_points_to 8344 (node->decl, 8345 first_vi_for_offset (vi, fi_clobbers)); 8346 pt_solution_ior_into (clobbers, &sol); 8347 } 8348 } 8349 } 8350 } 8351 else 8352 gcc_unreachable (); 8353 } 8354 } 8355 8356 fn->gimple_df->ipa_pta = true; 8357 8358 /* We have to re-set the final-solution cache after each function 8359 because what is a "global" is dependent on function context. */ 8360 final_solutions->empty (); 8361 obstack_free (&final_solutions_obstack, NULL); 8362 gcc_obstack_init (&final_solutions_obstack); 8363 } 8364 8365 delete_points_to_sets (); 8366 8367 in_ipa_mode = 0; 8368 8369 return 0; 8370 } 8371 8372 namespace { 8373 8374 const pass_data pass_data_ipa_pta = 8375 { 8376 SIMPLE_IPA_PASS, /* type */ 8377 "pta", /* name */ 8378 OPTGROUP_NONE, /* optinfo_flags */ 8379 TV_IPA_PTA, /* tv_id */ 8380 0, /* properties_required */ 8381 0, /* properties_provided */ 8382 0, /* properties_destroyed */ 8383 0, /* todo_flags_start */ 8384 0, /* todo_flags_finish */ 8385 }; 8386 8387 class pass_ipa_pta : public simple_ipa_opt_pass 8388 { 8389 public: 8390 pass_ipa_pta (gcc::context *ctxt) 8391 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt) 8392 {} 8393 8394 /* opt_pass methods: */ 8395 virtual bool gate (function *) 8396 { 8397 return (optimize 8398 && flag_ipa_pta 8399 /* Don't bother doing anything if the program has errors. */ 8400 && !seen_error ()); 8401 } 8402 8403 opt_pass * clone () { return new pass_ipa_pta (m_ctxt); } 8404 8405 virtual unsigned int execute (function *) { return ipa_pta_execute (); } 8406 8407 }; // class pass_ipa_pta 8408 8409 } // anon namespace 8410 8411 simple_ipa_opt_pass * 8412 make_pass_ipa_pta (gcc::context *ctxt) 8413 { 8414 return new pass_ipa_pta (ctxt); 8415 } 8416