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