aboutsummaryrefslogtreecommitdiff
path: root/gcc/tree-alias-common.c
blob: d4d2d94387fc0103dcef174fed29c4c861f8c7ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
/* Tree based points-to analysis
   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
   Contributed by Daniel Berlin <dberlin@dberlin.org>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "tree-alias-type.h"
#include "bitmap.h"
#include "tree-alias-common.h"
/* If we have andersen's points-to analysis, include it. */
#ifdef HAVE_BANSHEE
#include "tree-alias-ander.h"
#endif
#include "flags.h"
#include "rtl.h"
#include "tm_p.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "output.h"
#include "errors.h"
#include "expr.h"
#include "diagnostic.h"
#include "tree.h"
#include "c-common.h"
#include "tree-flow.h"
#include "tree-inline.h"
#include "varray.h"
#include "c-tree.h"
#include "tree-simple.h"
#include "hashtab.h"
#include "function.h"
#include "cgraph.h"
#include "tree-pass.h"
#include "timevar.h"

/* Reduce ifdefery later.  */
#ifndef HAVE_BANSHEE
#define HAVE_BANSHEE 0
#endif

/*  This file contains the implementation of the common parts of the
    tree points-to analysis infrastructure.
    
    Overview:
    
    This file contains the points-to analysis driver.  It does two main things:
    1. Keeps track of the PTA data for each variable (IE the data each
    specific PTA implementation wants to keep associated with a
    variable).
    2. Walks the function trees, calling the appropriate functions that
    each PTA implementation has implemented.
    
    In order to speed up PTA queries, the PTA specific data is stored
    in the tree for *_DECL's, in DECL_PTA_ALIASVAR.  This way, we only
    need to use the hash table for non-DECL's.  */
#define FIELD_BASED 0

/*  Array of all created alias_vars.
    Note that this should contain all the alias_vars we wanted
    marked during GC.  */
static GTY((param_is (union alias_var_def))) varray_type alias_vars = NULL;
struct tree_alias_ops *current_alias_ops;

/*  Array of local (to a function) alias_vars.
    Note that this should contain all the alias_vars that are
    local to this function.  We delete these from alias_vars before
    collection.  */
static GTY(()) varray_type local_alias_vars;
static GTY(()) varray_type local_alias_varnums;
tree pta_global_var;
static bitmap addrargs;						 
static alias_var get_alias_var_decl (tree);
static alias_var get_alias_var (tree);
static void find_func_aliases (tree);
static void deal_with_call_aliasing (tree, alias_var);
static alias_var create_fun_alias_var_ptf (tree, tree);
static alias_var create_fun_alias_var (tree, int);
static alias_var create_alias_var (tree);
static void intra_function_call (varray_type);
static void get_values_from_constructor (tree, varray_type *, bitmap, int *);
static bool call_may_clobber (tree);
static bool call_may_return (tree);

/* Return true if a EXPR, which is a CALL_EXPR, may clobber variables.  */

static bool
call_may_clobber (tree expr)
{
  tree callee;
  int flags;

  if (TREE_CODE (expr) != CALL_EXPR)
    return false;

  callee = get_callee_fndecl (expr);
  flags = (callee) ? flags_from_decl_or_type (callee) : 0;
  return (! (flags & (ECF_CONST | ECF_PURE | ECF_NORETURN)));
}

/* Return true if a EXPR, which is a CALL_EXPR, may return.  */

static bool
call_may_return (tree expr)
{
  tree callee;
  int flags;
  
  if (TREE_CODE (expr) != CALL_EXPR)
    return false;

  callee = get_callee_fndecl (expr);
  flags = (callee) ? flags_from_decl_or_type (callee) : 0;
  return ! (flags & ECF_NORETURN);
}

/*  Get the alias_var for DECL.  
    Creates the alias_var if it does not exist already. Also
    handles FUNCTION_DECL properly.  */

static alias_var
get_alias_var_decl (tree decl)
{
  alias_var newvar;
  if (TREE_CODE (decl) == FIELD_DECL)
    abort ();
  if (DECL_P (decl))
    {
      if (DECL_PTA_ALIASVAR (decl))
	return DECL_PTA_ALIASVAR (decl);
    }

  if (TREE_CODE (decl) == FUNCTION_DECL)
    newvar = create_fun_alias_var  (decl, 0);
  else
    {
      newvar = create_alias_var (decl);
      /* Assign globals to global var for purposes of intraprocedural
	 analysis. */
      if ((DECL_CONTEXT (decl) == NULL 
	   || TREE_PUBLIC (decl)
	   || TREE_STATIC (decl)
	   || decl_function_context (decl) == NULL) 
	  && decl != pta_global_var)
	{
	  current_alias_ops->addr_assign (current_alias_ops, 
					  get_alias_var (pta_global_var), 
					  newvar);
	  /* If the global has some DECL_INITIAL, we need to process
	     it here. */
	  if (DECL_INITIAL (decl))
	    find_func_aliases (decl);
	}
    }

  if (!current_alias_ops->ip)
    {
      if (!current_alias_ops->ip_partial
	  || (TREE_CODE (decl) != FUNCTION_DECL
	      && TREE_CODE (decl) != PARM_DECL))
	{
	  VARRAY_PUSH_INT (local_alias_varnums, ALIAS_VAR_VARNUM (newvar));
	  VARRAY_PUSH_TREE (local_alias_vars, decl);
	}
    }
  return newvar;
}

/* Get the alias_var for an expression EXPR.
   Note that this function expects to only be handed a RHS or LHS, not
   a MODIFY_EXPR.  */

static alias_var
get_alias_var (tree expr)
{
  /* If it's a decl, get the alias var of the decl. We farm this off
     to get_alias_var_decl so it can abort if the alias var doesn't
     exist, and in case something else *knows* it has a decl, and
     wants the alias var. */

  if (DECL_P (expr))
    return get_alias_var_decl (expr);

  /* True constants have no aliases (unless modifiable strings are on,
     in which case i don't think we'll end up with a STRING_CST anyway) */
  if (TREE_CODE_CLASS (TREE_CODE (expr)) == 'c')
    return NULL;


  switch (TREE_CODE (expr))
    {
    case ARRAY_REF:
      {
	/* Find the first non-array ref, and return it's alias
	   variable */
	tree p;
	for (p = expr; TREE_CODE (p) == ARRAY_REF;
	     p = TREE_OPERAND (p, 0));
	return get_alias_var (p);
      }
      break;
    case COMPONENT_REF:
      {
#if FIELD_BASED
	bool safe = true;
	tree p;
	for (p = expr; 
	     TREE_CODE (p) == COMPONENT_REF || TREE_CODE (p) == INDIRECT_REF;
	     p = TREE_OPERAND (p, 0))
	  {
	    if (TREE_CODE (TREE_TYPE (p)) == UNION_TYPE 
		|| TREE_CODE (TREE_TYPE (p)) == QUAL_UNION_TYPE)
	      {
		safe = false;
		break;
	      }
	  }
	if (!safe)
	  {
	    for (p = expr; TREE_CODE (p) == COMPONENT_REF;
		 p = TREE_OPERAND (p, 0));
	    return get_alias_var (p);
	  }
	else
	  {
	    return get_alias_var (TREE_OPERAND (expr, 1));
	  }
#else
        /* Find the first non-component ref, and return its alias variable. */
	tree p;
	for (p = expr; TREE_CODE (p) == COMPONENT_REF;
	     p = TREE_OPERAND (p, 0));
	return get_alias_var (p);
#endif
      }
      break;
    case REALPART_EXPR:
    case IMAGPART_EXPR:
    case NOP_EXPR:
    case CONVERT_EXPR:
    case FIX_TRUNC_EXPR:
    case FIX_CEIL_EXPR:
    case FIX_FLOOR_EXPR:
    case FIX_ROUND_EXPR:
    case ADDR_EXPR:
    case INDIRECT_REF:
    case BIT_FIELD_REF:
      /* If it's a ref or cast or conversion of something, get the
         alias var of the something. */
      return get_alias_var (TREE_OPERAND (expr, 0));
      break;

    default:
      return NULL;
    }
}

/*  Perform conservative aliasing for an intraprocedural mode function
    call.  ARGS are the arguments that were passed to that function
    call.  */

static void
intra_function_call (varray_type args)
{
  size_t l = VARRAY_ACTIVE_SIZE (args);
  size_t i;
  alias_var av = get_alias_var (pta_global_var);

  /* We assume assignments among the actual parameters. */
  for (i = 0; i < l; i++)
    {
      alias_var argi = VARRAY_GENERIC_PTR (args, i);
      size_t j;
      for (j = 0; j < l; j++)
	{
	  alias_var argj;
	  if (i == j)
	    continue;
	  argj = VARRAY_GENERIC_PTR (args, j);
	  /* Restricted pointers can't be aliased with other
	     restricted pointers. */
	  if (!TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (argi)))
	      || !TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (argj))))
	    /* Do a bit of TBAA to avoid pointless assignments. */
	    if (alias_sets_conflict_p (get_alias_set (ALIAS_VAR_DECL (argi)),
				       get_alias_set (ALIAS_VAR_DECL (argj))))	      
	      current_alias_ops->simple_assign (current_alias_ops, argi, argj);
	}
    }
  /* We assume that an actual parameter can point to any global. */
  for (i = 0; i < l; i++)
    {
      alias_var argav = VARRAY_GENERIC_PTR (args, i);
      /* Restricted pointers can't be aliased with other
	 restricted pointers. */
      if (!TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (argav)))
	  || !TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (av))))
	{
	  /* Arguments can alias globals, and whatever they point to
	     can point to a global as well. */
	  current_alias_ops->simple_assign (current_alias_ops, argav, av);
	}
    }
}

/* Put all pointers in a constructor in an array.  */

static void
get_values_from_constructor (tree constructor, varray_type *vals, 
			     bitmap addrargs, int *i)
{
  tree elt_list;
  switch (TREE_CODE (constructor))
    {
    case CONSTRUCTOR:
      {
	for (elt_list = CONSTRUCTOR_ELTS (constructor);
	     elt_list;
	     elt_list = TREE_CHAIN (elt_list))
	  {
	    tree value = TREE_VALUE (elt_list);
	    if (TREE_CODE (value) == TREE_LIST
		|| TREE_CODE (value) == CONSTRUCTOR)
	      {
		get_values_from_constructor (value, vals, addrargs, i);			      }
	    else
	      {
		alias_var aav;
		aav = get_alias_var (value);
		if (aav)
		  VARRAY_PUSH_GENERIC_PTR (*vals, aav);
		if (TREE_CODE (value) == ADDR_EXPR)
		  bitmap_set_bit (addrargs, *i);
		*i = *i + 1;
	      }
	  }
      }
      break;
    case TREE_LIST:
      for (elt_list = constructor;
	   elt_list;
	   elt_list = TREE_CHAIN (elt_list))
	{
	  get_values_from_constructor (TREE_VALUE (elt_list), vals, addrargs, i);
	}
      break;
    default:
      abort();
    }
}

/* Deal with the possible return values of a call that we don't have
   actual PTA info about.  */

static void
deal_with_call_aliasing (tree callargs, alias_var lhsAV)
{
  tree arg, argp;
  
  for (argp = callargs;
       argp;
       argp = TREE_CHAIN (argp))
    {
      arg = TREE_VALUE (argp);
      /* If we take the address of a variable directly in the
	 argument, the return value could be the address of that
	 variable.  */ 
      if (TREE_CODE (arg) == ADDR_EXPR)
	current_alias_ops->addr_assign (current_alias_ops, lhsAV,
					get_alias_var (arg));
      /* If we pass in a pointer, we could return that pointer.  */
      else if (POINTER_TYPE_P (TREE_TYPE (arg)))
	{
	  alias_var argtv = get_alias_var (arg);
	  if (argtv)
	    current_alias_ops->simple_assign (current_alias_ops, lhsAV,
					      argtv);
	}
    }
}

/* Find the operand of the component ref that actually is doing
   something to the DECL  */
static tree
find_op_of_decl (tree cref)
{
  while (!DECL_P (TREE_OPERAND (cref, 0)))
    {
      cref = TREE_OPERAND (cref, 0);
    }
  return cref;
}


/*  Tree walker that is the heart of the aliasing infrastructure.
    TP is a pointer to the current tree.
    WALK_SUBTREES specifies whether to continue traversing subtrees or
    not.
    Returns NULL_TREE when we should stop.
    
    This function is the main part of the aliasing infrastructure. It
    walks the trees, calling the appropriate alias analyzer functions to process
    various statements.  */

static void
find_func_aliases (tree stp)
{
  if (TREE_CODE (stp) == RETURN_EXPR)
    {
      stp = TREE_OPERAND (stp, 0);
      if (!stp)
	return;
    }
  
  if (TREE_CODE (stp) == MODIFY_EXPR
      || (DECL_P (stp) && DECL_INITIAL (stp) != NULL_TREE ))
    {
      tree op0, op1;
      alias_var lhsAV = NULL;
      alias_var rhsAV = NULL;

      if (DECL_P (stp))
	{
	  op0 = stp;
	  op1 = DECL_INITIAL (stp);
	}
      else
	{
	  op0 = TREE_OPERAND (stp, 0);
	  op1 = TREE_OPERAND (stp, 1);
	}
      /* lhsAV should always have an alias variable */
      lhsAV = get_alias_var (op0);
      if (!lhsAV)
	return;
      /* rhsAV might not have one, c.f. c = 5 */
      rhsAV = get_alias_var (op1);
#if !FIELD_BASED
      while (TREE_CODE (op1) == COMPONENT_REF 
	     && TREE_CODE (TREE_OPERAND (op1, 0)) == COMPONENT_REF)
	{
	  op1 = TREE_OPERAND (op1, 0);
	}
      while (TREE_CODE (op1) == BIT_FIELD_REF)
	{
	  op1 = TREE_OPERAND (op1, 0);
	}
      /* Take care of fact that we may have multi-level component
	 refs. */ 
      if (TREE_CODE (op1) == COMPONENT_REF)
	op1 = find_op_of_decl (op1);
#endif
      
      /* You would think we could test rhsAV at the top, rather than
	 50 separate times, but we can't, because it can be NULL for
	 operator assignments, where we'd still collect the individual
	 alias vars for the operator. */

      /* Note that structures are treated as a single alias
	 variable, since we can disambiguate based on TBAA first,
	 and fall back on points-to. */
      /* x = <something> */
      if (is_gimple_variable (op0))
	{
	  /* x = y */
	  if (is_gimple_variable (op1))
	    {
	      if (rhsAV != NULL)
		current_alias_ops->simple_assign (current_alias_ops, lhsAV,
						  rhsAV);
	    }
	  /* x = foo.y */
	  else if (TREE_CODE (op1) == COMPONENT_REF 
		   && DECL_P (TREE_OPERAND (op1, 0)))
	    {
	         if (rhsAV != NULL)
		current_alias_ops->simple_assign (current_alias_ops, lhsAV,
						  rhsAV);
	    }
	  /* x = (cast) [maybe-addr-expr] y */
	  else if (is_gimple_cast (op1))
	    {
	      tree stripped_op1 = op1;
	      STRIP_NOPS (stripped_op1);
	      if (rhsAV != NULL)
		{
		  if (TREE_CODE (stripped_op1) == ADDR_EXPR)
		    current_alias_ops->addr_assign (current_alias_ops, lhsAV, 
						    rhsAV);
		  else
		    current_alias_ops->simple_assign (current_alias_ops, lhsAV,
						      rhsAV);
		}
	    }
	  /* x = *y or x = foo->y */
	  else if (TREE_CODE (op1) == INDIRECT_REF 
		   || TREE_CODE (op1) == ARRAY_REF
		   || (TREE_CODE (op1) == COMPONENT_REF
		       && TREE_CODE (TREE_OPERAND (op1, 0)) == INDIRECT_REF))
	    {
	      if (rhsAV != NULL)
		current_alias_ops->ptr_assign (current_alias_ops, lhsAV,
					       rhsAV);
	    }
	  /* x = &y = x = &foo.y */
	  else if (TREE_CODE (op1) == ADDR_EXPR)
	    {
	      if (rhsAV != NULL)
		current_alias_ops->addr_assign (current_alias_ops, lhsAV,
						rhsAV);
	    }
	  /* x = func(...) */
	  else if (TREE_CODE (op1) == CALL_EXPR)
	    {
	      /* Heap assignment. These are __attribute__ malloc or
		 something, i'll deal with it later. */
	      if (0)
		{}
	      else
		{
		  
		  /* NORETURN functions have no effect on aliasing. */
		  if (call_may_return (op1))
		    {		      
		      varray_type args;
		      tree arg;
		      tree callop0, callop1;
		      int argnum;
		      
		      /* Collect the arguments */
		      VARRAY_GENERIC_PTR_INIT (args, 1, "Arguments");
		      bitmap_clear (addrargs);
		      callop1 = TREE_OPERAND (op1, 1);
		      callop0 = TREE_OPERAND (op1, 0);
		      for (arg = callop1, argnum = 0;
			   arg;
			   arg = TREE_CHAIN (arg), argnum++)
			{
			  alias_var aav = get_alias_var (TREE_VALUE (arg));
			  if (aav)
			    {
			      VARRAY_PUSH_GENERIC_PTR (args, aav);
			      if (TREE_CODE (TREE_VALUE (arg)) == ADDR_EXPR)
				bitmap_set_bit (addrargs, argnum);
			    }
			}
		      /* Simulate the call */
		      if (current_alias_ops->function_call (current_alias_ops, lhsAV,
							    get_alias_var (callop0),
							    args, addrargs))
			{ 
			  if (call_may_clobber (op1)
			      && !current_alias_ops->ip
                              && flag_argument_noalias != 2)
			    {
			      intra_function_call (args);
			    }
			  if (POINTER_TYPE_P (TREE_TYPE (op0)))
			    deal_with_call_aliasing (callop1, lhsAV);
			}
		    }
		}
	    }
	  /* x = op (...) */
	  else
	    {
	      bitmap_clear (addrargs);
	      if (TREE_CODE (op1) == CONSTRUCTOR)
	        {
		  varray_type ops;
		  int i = 0;
		  VARRAY_GENERIC_PTR_INIT (ops, 1, "Operands");
		  get_values_from_constructor (op1, &ops, addrargs, &i);
		  current_alias_ops->op_assign (current_alias_ops, lhsAV,
						ops, op1, addrargs);
		}
	      else
		switch (TREE_CODE_CLASS (TREE_CODE (op1)))
		  {
		  case 'e':  /* an expression */
		  case 's':  /* an expression with side effects */
		  case '<':  /* a comparison expression */
		  case '1':  /* a unary arithmetic expression */
		  case 'r':  /* a reference */
		  case '2':  /* a binary arithmetic expression */
		    {
		      tree op;
		      varray_type ops;
		      int i;
		      VARRAY_GENERIC_PTR_INIT (ops, 1, "Operands");
		      for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (op1)); i++)
			{
			  alias_var aav;
			  op = TREE_OPERAND (op1, i);
			  aav = get_alias_var (op);
			  if (aav)
			    VARRAY_PUSH_GENERIC_PTR (ops, aav);
			  if (TREE_CODE (op) == ADDR_EXPR)
			    bitmap_set_bit (addrargs, i);
			}
		      current_alias_ops->op_assign (current_alias_ops, lhsAV,
						    ops, op1, addrargs);
		    }
		    break;
		  default:
		    break;
		  }
	    }
	}
      /* *x = <something> */
      else
	{
	  /* x.f = y  or x->f = y */
	  if (TREE_CODE (op0) == COMPONENT_REF 
	      && is_gimple_variable (op1))
	    {
	      if (rhsAV != NULL)
		current_alias_ops->simple_assign (current_alias_ops, lhsAV,
						  rhsAV);
	    }
	  /* x.f = &y or x->f = &y */
	  else if (TREE_CODE (op0) == COMPONENT_REF 
		   && TREE_CODE (op1) == ADDR_EXPR)
	    {
	      if (rhsAV != NULL)
		current_alias_ops->addr_assign (current_alias_ops, lhsAV,
						rhsAV);
	    }
	  /* *x.f = y or *x->f = y */
	  else if ((TREE_CODE (op0) == INDIRECT_REF 
		    || TREE_CODE (op0) == ARRAY_REF)
		   && TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
		   && is_gimple_variable (op1))
	    {
	      if (rhsAV != NULL)
		current_alias_ops->assign_ptr (current_alias_ops, lhsAV,
					       rhsAV);
	    }
	  /* *x = &y */
	  else if ((TREE_CODE (op0) == INDIRECT_REF
		    || TREE_CODE (op0) == ARRAY_REF)
		   && TREE_CODE (op1) == ADDR_EXPR)
	    {
	      /* This becomes temp = &y and *x = temp . */
	      alias_var tempvar;
	      tree temp = create_tmp_var_raw (void_type_node, "aliastmp");
	      tempvar = current_alias_ops->add_var (current_alias_ops, temp);
	      current_alias_ops->addr_assign (current_alias_ops, tempvar,
					      rhsAV);
	      current_alias_ops->assign_ptr (current_alias_ops, lhsAV,
					     tempvar);
	    }

	  /* *x = *y */
	  else if ((TREE_CODE (op0) == INDIRECT_REF 
		    || TREE_CODE (op0) == ARRAY_REF)
		   && (TREE_CODE (op1) == INDIRECT_REF
		       || TREE_CODE (op1) == ARRAY_REF))
	    {
	      /* This becomes temp = *y and *x = temp . */
	      alias_var tempvar;
	      tree temp;
	      temp = create_tmp_var_raw (void_type_node, "aliastmp");
	      tempvar = current_alias_ops->add_var (current_alias_ops, temp);
	      current_alias_ops->ptr_assign (current_alias_ops, tempvar,
					     rhsAV);
	      current_alias_ops->assign_ptr (current_alias_ops, lhsAV,
					     tempvar);
	    }

	  /* *x = (cast) y */
	  else if ((TREE_CODE (op0) == INDIRECT_REF 
		    || TREE_CODE (op0) == ARRAY_REF)
		   && is_gimple_cast (op1))
	    {
	      if (rhsAV != NULL)
		{
		  /* This becomes temp = (cast) y and  *x = temp. */
		  alias_var tempvar;
		  tree temp;
		  temp = create_tmp_var_raw (void_type_node, "aliastmp");
		  tempvar = current_alias_ops->add_var (current_alias_ops,
							temp);
		  current_alias_ops->simple_assign (current_alias_ops,
						    tempvar, rhsAV);
		  current_alias_ops->assign_ptr (current_alias_ops, lhsAV,
						 tempvar);
		}
	    }
	  /* *x = <something else> */
	  else
	    {
	      if (rhsAV != NULL)
		current_alias_ops->assign_ptr (current_alias_ops, lhsAV,
					       rhsAV);
	    }
	}
    }
  /* Calls without return values. */
  else if (TREE_CODE (stp) == CALL_EXPR)
    {
      alias_var callvar;
      varray_type args;
      tree arg;
      callvar = get_alias_var (TREE_OPERAND (stp, 0));
      if (callvar != NULL)
	{
	
	  /* NORETURN and CONST functions with no return value
	     have no effect on aliasing (as may be seen above,
	     const functions that return a value might have an
	     effect on aliasing, since the return value can point
	     to one of the arguments.  */
	  if (call_may_clobber (stp))
	    {
	      int argnum;
	      VARRAY_GENERIC_PTR_INIT (args, 1, "Arguments");
	      bitmap_clear (addrargs);
	      for (arg = TREE_OPERAND (stp, 1), argnum=0; 
		   arg; 
		   arg = TREE_CHAIN (arg), argnum++)
		{
		  alias_var aav = get_alias_var (TREE_VALUE (arg));
		  if (aav)
		    {
		      VARRAY_PUSH_GENERIC_PTR (args, aav);
		      if (TREE_CODE (TREE_VALUE (arg)) == ADDR_EXPR)
			bitmap_set_bit (addrargs, argnum);
		    }
		  
		}
	      
	      if (current_alias_ops->function_call (current_alias_ops, NULL,
						    callvar, args, addrargs))
		if (!current_alias_ops->ip && flag_argument_noalias != 2)
		  intra_function_call (args);
	    }
	}
  }
}

/*  Create the alias_var for a function definition DECL, it's
    arguments, and it's return value. If FORCE is true, we force 
    creation of the alias_var, regardless of whether one exists already.
    
    This includes creation of alias_var's for
    - The function itself.
    - The arguments.
    - The return value.  */

static alias_var
create_fun_alias_var (tree decl, int force)
{
  alias_var avar, retvar;
  tree rdecl;
  varray_type params = NULL;

  if (!force)
    {
      if (DECL_PTA_ALIASVAR (decl))
        return DECL_PTA_ALIASVAR (decl);
    }

  VARRAY_GENERIC_PTR_INIT (params, 1, "Arguments");
  if (DECL_ARGUMENTS (decl) != NULL)
    {
      tree arg;
      for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
	{
	  alias_var var = get_alias_var (arg);
	  VARRAY_PUSH_GENERIC_PTR (params, var);
	  /* Incoming pointers can point to pta_global_var, unless
	     either we are interprocedural, or we can do ip on all
	     statics + this function has been defined + it's not an
	     external function. */
	  if (POINTER_TYPE_P (TREE_TYPE (arg))
	      && !current_alias_ops->ip
	      /* FIXME: Need to let analyzer decide in partial case. */
	      && (!current_alias_ops->ip_partial
		  || !cgraph_local_info (decl)->local))
	    current_alias_ops->simple_assign (current_alias_ops, var,
					      get_alias_var (pta_global_var));
	}
    }
  else if (TYPE_ARG_TYPES (TREE_TYPE (decl)) != NULL)
    {
      tree arg;
      /* FIXME: Handle varargs */
      for (arg = TYPE_ARG_TYPES (TREE_TYPE (decl));
	   arg && TREE_VALUE (arg) != void_type_node;
	   arg = TREE_CHAIN (arg))
	{
	  tree fakedecl = create_tmp_var_raw (TREE_VALUE (arg), "normarg");
	  alias_var var;
	  DECL_CONTEXT (fakedecl) = current_function_decl; 
	  var = get_alias_var (fakedecl);
	  VARRAY_PUSH_GENERIC_PTR (params, var);

	  /* Incoming pointers can point to pta_global_var, unless
	     either we are interprocedural, or we can do ip on all
	     statics + this function has been defined + it's not an
	     external function. */
	  if (POINTER_TYPE_P (TREE_TYPE (fakedecl))
	      && !current_alias_ops->ip
	      /* FIXME: need to let analyzer decide in partial case. */
	      && (!current_alias_ops->ip_partial
		  || !TREE_STATIC (decl)
		  || TREE_PUBLIC (decl)))
	    current_alias_ops->simple_assign (current_alias_ops, var,
					      get_alias_var (pta_global_var));
	}
    }
  /* Functions declared like void f() are *not* equivalent to void
     f(void). You can pass an argument to them. Thus, we need to
     create some fake argument that would alias any actuals that get
     passed to our function.  */
  else
    {
      tree fakedecl = create_tmp_var_raw (void_type_node, "fakearg");
      alias_var fakevar;
      DECL_CONTEXT (fakedecl) = current_function_decl;
      fakevar = get_alias_var (fakedecl);
      VARRAY_PUSH_GENERIC_PTR (params, fakevar);
    }

  if (!DECL_RESULT (decl))
    {
      rdecl = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (decl)), "_rv_");
      retvar = current_alias_ops->add_var (current_alias_ops, rdecl);
      DECL_PTA_ALIASVAR (rdecl) = retvar;
    }
  else
    {
      retvar = current_alias_ops->add_var (current_alias_ops,
					   DECL_RESULT (decl));
      DECL_PTA_ALIASVAR (DECL_RESULT (decl)) = retvar;
    }
  VARRAY_PUSH_GENERIC_PTR (alias_vars, retvar);
  ALIAS_VAR_VARNUM (retvar) = VARRAY_ACTIVE_SIZE (alias_vars) - 1;
  avar = current_alias_ops->add_var (current_alias_ops, decl);
  VARRAY_PUSH_GENERIC_PTR (alias_vars, avar);
  ALIAS_VAR_VARNUM (avar) = VARRAY_ACTIVE_SIZE (alias_vars) - 1;

  current_alias_ops->function_def (current_alias_ops, avar, params, retvar);
  DECL_PTA_ALIASVAR (decl) = avar;

  /* FIXME: Also, if this is a defining declaration then add the annotation
     to all extern definitions of the function. */
  return avar;
}

/*  Create an alias variable for a pointer-to-member function DECL of 
    type TYPE, it's arguments, and it's return value.
    Returns the alias_var for the PTF.
    
    This includes creating alias_var's for
    - The function itself.
    - The arguments.
    - The return value.  */

static alias_var
create_fun_alias_var_ptf (tree decl, tree type)
{
  alias_var avar, retvar;
  tree rdecl;
  varray_type params = NULL;

  if (DECL_PTA_ALIASVAR (decl))
    return DECL_PTA_ALIASVAR (decl);

  VARRAY_GENERIC_PTR_INIT (params, 1, "Arguments");

  if (TYPE_ARG_TYPES  (type) != NULL)
    {
      tree arg;
      /* FIXME: Handle varargs */
      for (arg = TYPE_ARG_TYPES (type);
	   arg && TREE_VALUE (arg) != void_type_node;
	   arg = TREE_CHAIN (arg))
	{
	  tree fakedecl = create_tmp_var_raw (TREE_VALUE (arg), "ptfarg");
	  alias_var var;
	  DECL_CONTEXT (fakedecl) = DECL_CONTEXT (decl);
	  var = get_alias_var (fakedecl);
	  VARRAY_PUSH_GENERIC_PTR (params, var);
	}
    }
  /* Functions declared like void f() are *not* equivalent to void
     f(void). You can pass an argument to them. Thus, we need to
     create some fake argument that would alias any actuals that get
     passed to our function.  */
  else
    {
      tree fakedecl = create_tmp_var_raw (void_type_node, "fakearg");
      alias_var fakevar;
      DECL_CONTEXT (fakedecl) = DECL_CONTEXT (decl);
      fakevar = get_alias_var (fakedecl);
      VARRAY_PUSH_GENERIC_PTR (params, fakevar);
    }

  rdecl = create_tmp_var_raw (TREE_TYPE (type), "_rv_");
  retvar = current_alias_ops->add_var (current_alias_ops, rdecl);
  VARRAY_PUSH_GENERIC_PTR (alias_vars, retvar);
  ALIAS_VAR_VARNUM (retvar) = VARRAY_ACTIVE_SIZE (alias_vars) - 1;

  avar = current_alias_ops->add_var (current_alias_ops, decl);
  VARRAY_PUSH_GENERIC_PTR (alias_vars, avar);
  ALIAS_VAR_VARNUM (avar) = VARRAY_ACTIVE_SIZE (alias_vars) - 1;

  current_alias_ops->function_def (current_alias_ops, avar, params, retvar);
  DECL_PTA_ALIASVAR (decl) = avar;

  return avar;
}

/*  Create the alias_var for a *_DECL node DECL.
    Returns the alias_var for DECL.
    
    This function also handles creation of alias_var's for PTF 
    variables.  */

static alias_var
create_alias_var (tree decl)
{
  alias_var avar;

  if (!DECL_P (decl))
    abort ();
  
  if (DECL_P (decl))
    {
      if (DECL_PTA_ALIASVAR (decl))
	return DECL_PTA_ALIASVAR (decl);
    }

  if (POINTER_TYPE_P (TREE_TYPE (decl))
      && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == FUNCTION_TYPE)
    {
      avar = create_fun_alias_var_ptf (decl, TREE_TYPE (TREE_TYPE (decl)));
    }
  else
    avar = current_alias_ops->add_var (current_alias_ops, decl);

  if (DECL_P (decl))
    {
      DECL_PTA_ALIASVAR (decl) = avar;
    }

  VARRAY_PUSH_GENERIC_PTR (alias_vars, avar);
  ALIAS_VAR_VARNUM (avar) = VARRAY_ACTIVE_SIZE (alias_vars) - 1;
  return avar;
}

/* Create points-to sets for the current function.  */

static void
create_alias_vars (void)
{
  basic_block bb;
#if HAVE_BANSHEE
  if (flag_tree_points_to == PTA_ANDERSEN)
    current_alias_ops = andersen_alias_ops;
  else
#endif
   {
     current_alias_ops = NULL;
     flag_tree_points_to = PTA_NONE;
     return;
   }

  pta_global_var = build_decl (VAR_DECL, get_identifier (".pta_global_var"),
			       size_type_node);
  DECL_ARTIFICIAL (pta_global_var) = 1;
  TREE_READONLY (pta_global_var) = 1;
  DECL_EXTERNAL (pta_global_var) = 0;
  TREE_STATIC (pta_global_var) = 1;
  TREE_USED (pta_global_var) = 1;
  DECL_CONTEXT (pta_global_var) = current_function_decl; 
  TREE_THIS_VOLATILE (pta_global_var) = 1;
  TREE_ADDRESSABLE (pta_global_var) = 0;

  init_alias_vars ();

  DECL_PTA_ALIASVAR (current_function_decl) = NULL;
  get_alias_var (current_function_decl);

  /* First, walk the variables and their DECL_INITIAL's */
  if (cfun->unexpanded_var_list)
    {
      tree vars, var;
      for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
	{
	  var = TREE_VALUE (vars);
	  if (TREE_CODE (var) != LABEL_DECL
	      && decl_function_context (var) == NULL
	      && DECL_INITIAL (var))
	    find_func_aliases (var);
	}
    }

  /* Now walk all statements and derive aliases.  */
  FOR_EACH_BB (bb)
    {
      block_stmt_iterator bsi; 
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
	find_func_aliases (bsi_stmt (bsi));
    }

  pta_global_var = NULL_TREE;
}

struct tree_opt_pass pass_build_pta = 
{
  "pta",				/* name */
  NULL,					/* gate */
  create_alias_vars,			/* execute */
  NULL,					/* sub */
  NULL,					/* next */
  0,					/* static_pass_number */
  TV_TREE_PTA,				/* tv_id */
  PROP_cfg,				/* properties_required */
  PROP_pta,				/* properties_provided */
  0,					/* properties_destroyed */
  0,					/* todo_flags_start */
  0					/* todo_flags_finish */
};
 

/* Delete created points-to sets.  */

static void
delete_alias_vars (void)
{
  size_t i;

  if (flag_tree_points_to != PTA_ANDERSEN)
    return;

  for (i = 0; i < VARRAY_ACTIVE_SIZE (local_alias_vars); i++)
    {
      tree key = VARRAY_TREE (local_alias_vars, i);
      if (DECL_P (key))
	DECL_PTA_ALIASVAR (key) = NULL;
      else
	abort ();
    }

  for (i = 0; i < VARRAY_ACTIVE_SIZE (local_alias_varnums); i ++)
    VARRAY_GENERIC_PTR (alias_vars, VARRAY_INT (local_alias_varnums, i)) = NULL;
  if (!current_alias_ops->ip && !current_alias_ops->ip_partial)
    {
      /*      VARRAY_CLEAR (alias_vars); */
      VARRAY_CLEAR (local_alias_vars);
      VARRAY_CLEAR (local_alias_varnums);
    }
  BITMAP_XFREE (addrargs);
  current_alias_ops->cleanup (current_alias_ops);
}

struct tree_opt_pass pass_del_pta = 
{
  "pta",				/* name */
  NULL,					/* gate */
  delete_alias_vars,			/* execute */
  NULL,					/* sub */
  NULL,					/* next */
  0,					/* static_pass_number */
  TV_TREE_PTA,				/* tv_id */
  PROP_pta,				/* properties_required */
  0,					/* properties_provided */
  PROP_pta,				/* properties_destroyed */
  0,					/* todo_flags_start */
  0					/* todo_flags_finish */
};
 

/*  Initialize points-to analysis machinery.  */

void
init_alias_vars (void)
{
  current_alias_ops->init (current_alias_ops);
  addrargs = BITMAP_XMALLOC ();
  VARRAY_TREE_INIT (local_alias_vars, 10, "Local alias vars");
  VARRAY_INT_INIT (local_alias_varnums, 10, "Local alias varnums");
  if ((!current_alias_ops->ip && !current_alias_ops->ip_partial)
      || alias_vars == NULL)
    VARRAY_GENERIC_PTR_INIT (alias_vars, 10, "Alias vars");
}

/* Return true if PTR can't point to anything (i.e. it has an empty
   points-to set.  */
bool 
empty_points_to_set (tree ptr)
{
 alias_var ptrtv;
  
#if !FIELD_BASED
#else
  if (TREE_CODE (ptr) == COMPONENT_REF)
    ptr = TREE_OPERAND (ptr, 1);
#endif

  if (DECL_P (ptr))
    {
      ptrtv = DECL_PTA_ALIASVAR (ptr);
      if (!ptrtv)
	return true;
    }
  else
    abort ();

  return current_alias_ops->empty_points_to_set (current_alias_ops, ptrtv);
}

/* Return true if PTR and VAR have the same points-to set.  */

bool
same_points_to_set (tree ptr, tree var)
{
  alias_var ptrtv, vartv;
  
#if !FIELD_BASED
#else
  if (TREE_CODE (ptr) == COMPONENT_REF)
    ptr = TREE_OPERAND (ptr, 1);
  if (TREE_CODE (var) == COMPONENT_REF)
    var = TREE_OPERAND (var, 1);
#endif

  if (ptr == var)
    return true;

  if (DECL_P (ptr))
    {
      ptrtv = DECL_PTA_ALIASVAR (ptr);
      if (!ptrtv)
	return false;
    }
  else
    abort ();

  if (DECL_P (var))
    {
      vartv = DECL_PTA_ALIASVAR (var);
      if (!vartv)
	return false;
    }
  else
    abort ();

  return current_alias_ops->same_points_to_set (current_alias_ops, vartv, ptrtv);
}

/*  Determine whether two variables (PTR and VAR) may-alias.
    Returns TRUE if PTR may-alias VAR.  */

bool
ptr_may_alias_var (tree ptr, tree var)
{
  alias_var ptrtv, vartv;

#if !FIELD_BASED
#else
  if (TREE_CODE (ptr) == COMPONENT_REF)
    ptr = TREE_OPERAND (ptr, 1);
  if (TREE_CODE (var) == COMPONENT_REF)
    var = TREE_OPERAND (var, 1);
#endif

  if (ptr == var)
    return true;

  if (DECL_P (ptr))
    {
      ptrtv = DECL_PTA_ALIASVAR (ptr);
      if (!ptrtv)
	return false;
    }
  else
    abort ();

  if (DECL_P (var))
    {
      vartv = DECL_PTA_ALIASVAR (var);
      if (!vartv)
	return false;
    }
  else
    abort ();

  return current_alias_ops->may_alias (current_alias_ops, ptrtv, vartv);
}

#define MASK_POINTER(P)	((unsigned)((unsigned long)(P) & 0xffff))

const char *
alias_get_name (tree t)
{
  const char *name;

#if FIELD_BASED
  if (TREE_CODE (t) == FIELD_DECL)
    {
      /* First get the name of the field, then the prefix, then smash them
	 together.  */
      const char *fieldname = IDENTIFIER_POINTER (DECL_NAME (t));
      const char *prefix = alias_get_name (DECL_CONTEXT (t));
      char *smashed;
      size_t neededlen = strlen (fieldname) + strlen (prefix) + 2;
      smashed = ggc_alloc (neededlen);
      sprintf (smashed, "%s.%s", prefix, fieldname);
      name = smashed;

    }
  else if (TYPE_P (t))
    {
      if (TYPE_NAME (t) && IDENTIFIER_POINTER (TYPE_NAME (t)))
	name = IDENTIFIER_POINTER (TYPE_NAME (t));
      else
	name = "<unnamed type>";
    }
  else
#endif
    {
      if (TREE_CODE (t) == FUNCTION_DECL)
	name = IDENTIFIER_POINTER (DECL_NAME (t));
      else if (TREE_CODE (t) == RESULT_DECL)
	name = "<return value>";
      else
	name = get_name (t);
    }

  if (!name)
    {
      char *namep;
      /* 2 = UF
	 4 = the masked pointer
	 2 = the <> around it
	 1 = the terminator. */
      namep = ggc_alloc (2 + 4 + 2 + 1);
      sprintf (namep, "<UV%x>", MASK_POINTER (t));
      return namep;
    }

  return name;
}

#include "gt-tree-alias-common.h"