-
Notifications
You must be signed in to change notification settings - Fork 1
/
fa.cc
4013 lines (3797 loc) · 126 KB
/
fa.cc
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
/* -*-Mode: c++;-*-
Copyright (c) 2004-2010 John Plevyak, All Rights Reserved
*/
#include "ifadefs.h"
#include "fa.h"
#include "ast.h"
#include "builtin.h"
#include "clone.h"
#include "fail.h"
#include "fun.h"
#include "graph.h"
#include "if1.h"
#include "log.h"
#include "pattern.h"
#include "pdb.h"
#include "pnode.h"
#include "prim.h"
#include "timer.h"
#include "var.h"
/* runtime options
*/
bool fgraph_pass_contours = false;
int analysis_pass = 0;
AType *bottom_type = 0;
AType *nil_type = 0;
AType *unknown_type = 0;
AType *void_type = 0;
AType *top_type = 0;
AType *any_type = 0;
AType *bool_type = 0;
AType *true_type = 0;
AType *false_type = 0;
AType *size_type = 0;
AType *anyint_type = 0;
AType *anynum_kind = 0;
AType *symbol_type = 0;
AType *string_type = 0;
AType *tuple_type = 0;
AType *anytype_type = 0;
AType *function_type = 0;
static int avar_id = 1;
static int aedge_id = 1;
static int creation_set_id = 1;
static int entry_set_id = 1;
FA *fa = 0;
static Timer pass_timer, match_timer, extend_timer;
static ChainHash<AType *, ATypeChainHashFns> cannonical_atypes;
static ChainHash<Setters *, SettersHashFns> cannonical_setters;
static ChainHash<SettersClasses *, SettersClassesHashFns> cannonical_setters_classes;
static ChainHash<ATypeFold *, ATypeFoldChainHashFns> type_fold_cache;
static ChainHash<ATypeViolation *, ATypeViolationHashFuns> type_violation_hash;
static Que(AEdge, edge_worklist_link) edge_worklist;
static Que(AVar, send_worklist_link) send_worklist;
static Que(EntrySet, es_worklist_link) es_worklist;
static Vec<EntrySet *> entry_set_done;
static Vec<ATypeViolation *> type_violations;
static int application(PNode *p, EntrySet *es, AVar *fun, CreationSet *s, Vec<AVar *> &args, Vec<cchar *> &names,
int is_closure, Partial_kind partial, PNode *visibility_point, Vec<CreationSet *> *closures);
AEdge::AEdge() : from(0), to(0), pnode(0), fun(0), match(0), in_edge_worklist(0) { id = aedge_id++; }
AVar::AVar(Var *v, void *acontour)
: var(v),
contour(acontour),
lvalue(0),
gen(0),
in(bottom_type),
out(bottom_type),
restrict(0),
container(0),
setters(0),
setter_class(0),
mark_map(0),
cs_map(0),
match_cache(0),
type(0),
ivar_offset(0),
in_send_worklist(0),
contour_is_entry_set(0),
is_lvalue(0),
live(0),
live_arg(0),
is_if_arg(0) {
id = avar_id++;
}
AType::AType(AType &a) {
hash = 0;
this->copy(a);
}
AType::AType(CreationSet *cs) {
hash = 0;
set_add(cs);
}
AVar *unique_AVar(Var *v, void *contour) {
assert(contour);
AVar *av = v->avars.get(contour);
if (av) return av;
av = new AVar(v, contour);
v->avars.put(contour, av);
return av;
}
AVar *unique_AVar(Var *v, EntrySet *es) {
assert(es);
AVar *av = v->avars.get(es);
if (av) return av;
av = new AVar(v, es);
v->avars.put(es, av);
av->contour_is_entry_set = 1;
if (v->sym->is_lvalue) {
av->lvalue = new AVar(v, es);
av->lvalue->is_lvalue = 1;
av->lvalue->contour_is_entry_set = 1;
}
return av;
}
CreationSet::CreationSet(Sym *s)
: sym(s),
dfs_color(DFS_white),
clone_for_constants(0),
added_element_var(0),
closure_used(0),
tuple_able(0),
atype(0),
equiv(0),
type(0) {
id = creation_set_id++;
}
CreationSet::CreationSet(CreationSet *cs)
: dfs_color(DFS_white), added_element_var(0), closure_used(0), tuple_able(0), atype(0), equiv(0), type(0) {
sym = cs->sym;
id = creation_set_id++;
clone_for_constants = cs->clone_for_constants;
forv_AVar(v, cs->vars) {
AVar *iv = unique_AVar(v->var, this);
add_var_constraint(iv);
vars.add(iv);
if (iv->var->sym->name) var_map.put(iv->var->sym->name, iv);
}
sym->creators.add(this);
}
EntrySet::EntrySet(Fun *af) : fun(af), dfs_color(DFS_white), in_es_worklist(0), split(0), equiv(0) {
id = entry_set_id++;
}
AVar *make_AVar(Var *v, EntrySet *es) {
if (v->sym->nesting_depth) {
if (v->sym->nesting_depth != es->fun->sym->nesting_depth + 1)
return unique_AVar(v, es->display[v->sym->nesting_depth - 1]);
return unique_AVar(v, es);
}
if (v->is_internal) return unique_AVar(v, es);
return unique_AVar(v, GLOBAL_CONTOUR);
}
// static inline AVar *make_AVar(Var *v, AEdge *e) { return make_AVar(v, e->to); }
AType *make_AType(CreationSet *cs) {
if (cs->atype) return cs->atype;
return cs->atype = type_cannonicalize(new AType(cs));
}
AType *make_abstract_type(Sym *s) {
s = unalias_type(s);
AType *a = s->abstract_type;
if (a) return a;
CreationSet *cs = new CreationSet(s);
return s->abstract_type = make_AType(cs);
}
AType *make_AType(Vec<CreationSet *> &css) {
AType *t = new AType();
t->set_union(css);
return type_cannonicalize(t);
}
AType *AType::constants() {
AType *t = new AType();
forv_CreationSet(cs, this->sorted) if (cs->sym->constant) t->set_add(cs);
return type_cannonicalize(t);
}
void update_in(AVar *v, AType *t) {
AType *tt = type_union(v->in, t);
if (tt != v->in) {
assert(tt && tt != top_type);
v->in = tt;
if (v->restrict) tt = type_intersection(v->in, v->restrict);
if (tt != v->out) {
assert(tt != top_type);
v->out = tt;
forv_AVar(vv, v->arg_of_send.asvec) {
if (!vv->in_send_worklist) {
vv->in_send_worklist = 1;
send_worklist.enqueue(vv);
}
}
if (v->is_if_arg) {
EntrySet *es = (EntrySet *)v->contour;
if (!es->in_es_worklist) {
es->in_es_worklist = 1;
es_worklist.enqueue(es);
}
}
forv_AVar(vv, v->forward) if (vv) update_in(vv, tt);
}
}
}
void update_gen(AVar *v, AType *t) {
if (v->gen) {
AType *tt = type_union(v->gen, t);
if (tt == v->gen) return;
v->gen = tt;
} else
v->gen = t;
update_in(v, v->gen);
}
static void flow_var_to_var(AVar *a, AVar *b) {
if (a == b) return;
if (a->forward.set_in(b)) return;
a->forward.set_add(b);
b->backward.set_add(a);
update_in(b, a->out);
}
void flow_vars(AVar *v, AVar *vv) {
if (v->lvalue) {
if (vv->lvalue) {
flow_var_to_var(v, vv);
flow_var_to_var(vv->lvalue, v->lvalue);
} else {
flow_var_to_var(v, vv);
flow_var_to_var(vv, v->lvalue);
}
} else {
if (vv->lvalue) {
flow_var_to_var(v, vv);
flow_var_to_var(vv->lvalue, v);
} else
flow_var_to_var(v, vv);
}
}
void flow_vars_assign(AVar *rhs, AVar *lhs) {
flow_var_to_var(rhs, lhs);
if (lhs->lvalue) flow_var_to_var(rhs, lhs->lvalue);
}
CreationSet *creation_point(AVar *v, Sym *s, int nvars) {
CreationSet *cs = v->cs_map ? v->cs_map->get(s) : 0;
EntrySet *es = (EntrySet *)v->contour;
if (cs) {
assert(cs->sym == s);
goto Lfound;
}
if (s == sym_closure) goto Lunique;
if ((void *)es == GLOBAL_CONTOUR) es = 0;
if (es && es->split) {
AVar *oldv = make_AVar(v->var, es->split);
cs = oldv->cs_map ? oldv->cs_map->get(s) : 0;
if (cs) {
assert(cs->sym == s);
goto Lfound;
}
}
forv_CreationSet(x, s->creators) {
if (s->abstract_type && x == s->abstract_type->v[0]) continue;
if (nvars != -1 || x->vars.n != nvars) continue;
cs = x;
goto Lfound;
}
Lunique:
// new creation set
cs = new CreationSet(s);
s->creators.add(cs);
forv_Sym(h, s->has) {
assert(h->var);
AVar *iv = unique_AVar(h->var, cs);
add_var_constraint(iv);
cs->vars.add(iv);
if (h->name) cs->var_map.put(h->name, iv);
}
Lfound:
if (!v->cs_map) v->cs_map = new CSMap;
v->cs_map->put(s, cs);
cs->defs.set_add(v);
if (v->contour_is_entry_set) ((EntrySet *)v->contour)->creates.set_add(cs);
update_gen(v, make_AType(cs));
return cs;
}
// all float combos become doubles
// all signed/unsigned combos become signed
// all int combos below 32 bits become signed 32 bits, above become signed 64
// bits
Sym *coerce_num(Sym *a, Sym *b) {
if (a == b) return a;
if (a == sym_string || b == sym_string) return sym_string;
if (a->num_kind == b->num_kind) {
if (a->num_index > b->num_index)
return a;
else
return b;
}
if (b->num_kind == IF1_NUM_KIND_FLOAT) {
Sym *t = b;
b = a;
a = t;
}
if (b->num_kind == IF1_NUM_KIND_COMPLEX) {
Sym *t = b;
b = a;
a = t;
}
if (a->num_kind == IF1_NUM_KIND_COMPLEX) {
if (b->num_kind == IF1_NUM_KIND_FLOAT) {
if (a->num_index > b->num_index) return a;
return if1->complex_types[b->num_index];
}
if (int_type_precision[b->num_kind] <= float_type_precision[a->num_kind]) return a;
if (int_type_precision[b->num_kind] >= 32) return sym_complex32;
return sym_complex64;
}
if (a->num_kind == IF1_NUM_KIND_FLOAT) {
if (int_type_precision[b->num_kind] <= float_type_precision[a->num_kind]) return a;
if (int_type_precision[b->num_kind] >= 32) return sym_float32;
return sym_float64;
}
// mixed signed and unsigned
if (a->num_index >= IF1_INT_TYPE_64 || b->num_index >= IF1_INT_TYPE_64)
return sym_int64;
else if (a->num_index >= IF1_INT_TYPE_32 || b->num_index >= IF1_INT_TYPE_32)
return sym_int32;
else if (a->num_index >= IF1_INT_TYPE_16 || b->num_index >= IF1_INT_TYPE_16)
return sym_int16;
else if (a->num_index >= IF1_INT_TYPE_8 || b->num_index >= IF1_INT_TYPE_8)
return sym_int8;
return sym_bool;
}
AType *type_num_fold(Prim *p, AType *a, AType *b) {
(void)p;
p = 0; // for now
a = type_intersection(a, anynum_kind);
b = type_intersection(b, anynum_kind);
ATypeFold f(p, a, b), *ff;
if ((ff = type_fold_cache.get(&f))) return ff->result;
AType *r = new AType();
forv_CreationSet(acs, a->sorted) {
Sym *atype = acs->sym->type;
forv_CreationSet(bcs, b->sorted) {
Sym *btype = bcs->sym->type;
r->set_add(coerce_num(atype, btype)->abstract_type->v[0]);
}
}
r = type_cannonicalize(r);
type_fold_cache.put(new ATypeFold(p, a, b, r));
return r;
}
void qsort_pointers(void **left, void **right) {
Lagain:
if (right - left < 5) {
for (void **y = right - 1; y > left; y--) {
for (void **x = left; x < y; x++) {
if (x[0] > x[1]) {
void *t = x[0];
x[0] = x[1];
x[1] = t;
}
}
}
} else {
void **i = left + 1, **j = right - 1, *x = *left;
for (;;) {
while (x < *j) j--;
while (i < j && *i < x) i++;
if (i >= j) break;
void *t = *i;
*i = *j;
*j = t;
i++;
j--;
}
if (j == right - 1) {
*left = *(right - 1);
*(right - 1) = x;
right--;
goto Lagain;
}
if (left < j) qsort_pointers(left, j + 1);
if (j + 2 < right) qsort_pointers(j + 1, right);
}
}
AType *type_cannonicalize(AType *t) {
assert(!t->sorted.n);
assert(!t->union_map.n);
assert(!t->intersection_map.n);
int consts = 0, rebuild = 0, nulls = 0;
Vec<CreationSet *> nonconsts;
forv_CreationSet(cs, *t) if (cs) {
// strip out constants if the base type is included
CreationSet *base_cs = 0;
if (cs->sym->is_constant || (cs->sym->type->num_kind && cs->sym != cs->sym->type))
base_cs = cs->sym->type->abstract_type->v[0];
else if (cs->sym->type_kind == Type_TAGGED)
base_cs = cs->sym->type->specializes[0]->abstract_type->v[0];
if (base_cs) {
if (t->set_in(base_cs)) {
rebuild = 1;
continue;
}
consts++;
nonconsts.set_add(base_cs);
} else {
if (!cs->sym->is_unique_type) // e.g. nil, void, or unknown
nonconsts.set_add(cs);
else
nulls = 1;
}
t->sorted.add(cs);
}
if (consts > fa->num_constants_per_variable) rebuild = 1;
if (rebuild) {
t->sorted.clear();
t->sorted.append(nonconsts);
t->clear();
t->set_union(t->sorted);
}
if (t->sorted.n > 1) qsort_by_id(t->sorted);
unsigned int h = 0;
for (int i = 0; i < t->sorted.n; i++) h = (uint)(intptr_t)t->sorted[i] * open_hash_primes[i % 256];
t->hash = h ? h : h + 1; // 0 is empty
AType *tt = cannonical_atypes.put(t);
if (!tt) tt = t;
// compute "type" (without constants)
if (nonconsts.n) {
if (nulls || consts)
tt->type = make_AType(nonconsts);
else
tt->type = tt;
} else
tt->type = bottom_type;
return tt;
}
AType *type_union(AType *a, AType *b) {
AType *r;
if ((r = a->union_map.get(b))) return r;
if (a == b || b == bottom_type) {
r = a;
goto Ldone;
}
if (a == bottom_type) {
r = b;
goto Ldone;
}
{
AType *ab = type_diff(a, b);
AType *ba = type_diff(b, a);
r = new AType(*ab);
forv_CreationSet(x, ba->sorted) r->set_add(x);
forv_CreationSet(x, a->sorted) if (b->in(x)) r->set_add(x);
r = type_cannonicalize(r);
}
Ldone:
a->union_map.put(b, r);
return r;
}
static inline int subsumed_by(Sym *a, Sym *b) {
return (a == b) || a->type == b ||
((b->type_kind == Type_SUM || a->is_symbol || a->is_fun) && b->specializers.set_in(a->type));
}
AType *type_diff(AType *a, AType *b) {
AType *r;
if ((r = a->diff_map.get(b))) return r;
if (b == bottom_type) {
r = a;
goto Ldone;
}
r = new AType();
forv_CreationSet(aa, a->sorted) {
if (aa->defs.n && b->set_in(aa)) continue;
forv_CreationSet(bb, b->sorted) if (!bb->defs.n) {
if (subsumed_by(aa->sym, bb->sym)) goto Lnext;
}
r->set_add(aa);
Lnext:;
}
r = type_cannonicalize(r);
Ldone:
a->diff_map.put(b, r);
return r;
}
AType *type_intersection(AType *a, AType *b) {
AType *r;
if ((r = a->intersection_map.get(b))) return r;
if (a == b || a == bottom_type || b == top_type) {
r = a;
goto Ldone;
}
if (a == top_type || b == bottom_type) {
r = b;
goto Ldone;
}
r = new AType();
forv_CreationSet(aa, a->sorted) {
forv_CreationSet(bb, b->sorted) {
if (aa->defs.n) {
if (bb->defs.n) {
if (aa == bb) {
r->set_add(aa);
goto Lnexta;
}
} else {
if (subsumed_by(aa->sym, bb->sym)) {
r->set_add(aa);
goto Lnexta;
}
}
} else {
if (bb->defs.n) {
if (subsumed_by(bb->sym, aa->sym)) r->set_add(bb);
} else {
if (subsumed_by(aa->sym, bb->sym)) {
r->set_add(aa);
goto Lnexta;
} else if (subsumed_by(bb->sym, aa->sym))
r->set_add(bb);
}
}
}
Lnexta:;
}
r = type_cannonicalize(r);
Ldone:
a->intersection_map.put(b, r);
return r;
}
static void fill_rets(EntrySet *es, int n) {
es->fun->rets.fill(n);
es->rets.fill(n);
for (int i = 0; i < n; i++)
if (!es->rets[i]) {
if (!i)
es->rets[i] = make_AVar(es->fun->sym->ret->var, es);
else {
if (!es->fun->rets[i]) {
Var *v = new Var(es->fun->sym->ret);
es->fun->rets[i] = v;
es->fun->fa_all_Vars.add(v);
}
es->rets[i] = make_AVar(es->fun->rets.v[i], es);
}
}
}
static int same_eq_classes(Setters *s, Setters *ss) {
if (s == ss) return 1;
if (!s || !ss) return 0;
Vec<Setters *> sc1, sc2;
forv_AVar(av, *s) if (av) {
assert(av->setter_class);
sc1.set_add(av->setter_class);
}
forv_AVar(av, *ss) if (av) {
assert(av->setter_class);
sc2.set_add(av->setter_class);
}
if (sc1.some_disjunction(sc2)) return 0;
return 1;
}
static int edge_nest_compatible_with_entry_set(AEdge *e, EntrySet *es) {
if (!es->fun->sym->nesting_depth) return 1;
int ef_nd = e->from->fun->sym->nesting_depth, es_nd = es->fun->sym->nesting_depth;
int n = ef_nd < es_nd ? ef_nd : es_nd; // MIN
for (int i = 0; i < n; i++)
if (e->from->display[i] != es->display.v[i]) return 0;
if (ef_nd < es_nd) // down call
if (es->display[es_nd - 1] != e->from) return 0;
return 1;
}
static int different_marked_args(AVar *a1, AVar *a2, int offset, AVar *basis = 0) {
Vec<void *> marks1, marks2;
AVar *basis1 = basis ? basis : a2;
int found1 = 0, found2 = 0;
if (a1->mark_map) {
form_Map(MarkElem, x, *a1->mark_map) {
if (basis1->mark_map) {
int m = basis1->mark_map->get(x->key);
if (m) {
found1 = 1;
if (m - offset == x->value) marks1.set_add(x->key);
}
}
}
}
if (a2->mark_map) {
form_Map(MarkElem, x, *a2->mark_map) {
if (basis) {
if (basis->mark_map) {
int m = basis->mark_map->get(x->key);
if (m) {
found2 = 1;
if (m - offset == x->value) marks2.set_add(x->key);
}
}
} else {
found2 = 1;
marks2.set_add(x->key);
}
}
}
return found1 && found2 && marks1.some_disjunction(marks2);
}
static int edge_type_compatible_with_edge(AEdge *e, AEdge *ee, EntrySet *es, int fmark = 0) {
assert(e->args.n && ee->args.n);
forv_MPosition(p, e->match->fun->positional_arg_positions) {
AVar *e_arg = e->args.get(p), *ee_arg = ee->args.get(p);
if (!e_arg || !ee_arg) continue;
AType *etype = type_intersection(e_arg->out->type, e->match->formal_filters.get(p));
AType *eetype = type_intersection(ee_arg->out->type, ee->match->formal_filters.get(p));
if (!fmark) {
if (etype->n && eetype->n && etype != eetype) return 0;
} else {
AVar *es_arg = es->args.get(p);
if (different_marked_args(ee_arg, e_arg, 2, es_arg)) return 0;
}
}
if (e->rets.n != ee->rets.n) return 0;
for (int i = 0; i < e->rets.n; i++) {
if (ee->rets[i]->lvalue && e->rets.v[i]->lvalue) {
if (!fmark) {
if (ee->rets[i]->lvalue->out->type->n && e->rets.v[i]->lvalue->out->type->n &&
ee->rets[i]->lvalue->out->type != e->rets.v[i]->lvalue->out->type)
return 0;
} else {
if (different_marked_args(ee->rets[i]->lvalue, e->rets.v[i]->lvalue, 1, es->rets[i]->lvalue)) return 0;
}
}
}
return 1;
}
static int edge_type_compatible_with_entry_set(AEdge *e, EntrySet *es, int fmark = 0) {
assert(e->args.n && es->args.n);
if (!es->split) {
forv_MPosition(p, e->match->fun->positional_arg_positions) {
AVar *es_arg = es->args.get(p), *e_arg = e->args.get(p);
if (!e_arg) continue;
AType *etype = type_intersection(e_arg->out->type, e->match->formal_filters.get(p));
if (!fmark) {
if (etype->n && es_arg->out->type->n && etype != es_arg->out->type) return 0;
} else if (different_marked_args(e_arg, es_arg, 2))
return 0;
}
if (es->rets.n != e->rets.n) return 0;
for (int i = 0; i < e->rets.n; i++) {
if (es->rets[i]->lvalue && e->rets.v[i]->lvalue) {
if (!fmark) {
if (es->rets[i]->lvalue->out->type->n && e->rets.v[i]->lvalue->out->type->n &&
es->rets[i]->lvalue->out->type != e->rets.v[i]->lvalue->out->type)
return 0;
} else if (different_marked_args(es->rets[i]->lvalue, e->rets.v[i]->lvalue, 1))
return 0;
}
}
} else {
forv_AEdge(ee, es->edges) if (ee) {
if (!ee->args.n) continue;
if (!edge_type_compatible_with_edge(e, ee, es, fmark)) return 0;
}
}
return 1;
}
static int sset_compatible(AVar *av1, AVar *av2) {
if (!same_eq_classes(av1->setters, av2->setters)) return 0;
if (av1->lvalue && av2->lvalue)
if (!same_eq_classes(av1->lvalue->setters, av2->lvalue->setters)) return 0;
return 1;
}
static int edge_sset_compatible_with_edge(AEdge *e, AEdge *ee) {
assert(e->args.n && ee->args.n);
forv_MPosition(p, e->match->fun->positional_arg_positions) {
AVar *eav = e->args.get(p), *eeav = ee->args.get(p);
if (eav && eeav)
if (!sset_compatible(eav, eeav)) return 0;
}
if (e->rets.n != ee->rets.n) return 0;
for (int i = 0; i < e->rets.n; i++)
if (!sset_compatible(e->rets[i], ee->rets.v[i])) return 0;
return 1;
}
static int edge_sset_compatible_with_entry_set(AEdge *e, EntrySet *es) {
assert(e->args.n && es->args.n);
if (!es->split) {
forv_MPosition(p, e->match->fun->positional_arg_positions) {
AVar *av = e->args.get(p);
if (av)
if (!sset_compatible(av, es->args.get(p))) return 0;
}
if (es->rets.n != e->rets.n) return 0;
for (int i = 0; i < es->rets.n; i++)
if (!sset_compatible(e->rets[i], es->rets.v[i])) return 0;
} else {
forv_AEdge(ee, es->edges) if (ee) {
if (!ee->args.n) continue;
if (!edge_sset_compatible_with_edge(e, ee)) return 0;
}
}
return 1;
}
static int edge_constant_compatible_with_entry_set(AEdge *e, EntrySet *es) {
forv_MPosition(p, e->match->fun->positional_arg_positions) {
AVar *av = es->args.get(p);
if (av->var->sym->clone_for_constants) {
AType css;
av->out->set_disjunction(*e->args.get(p)->out, css);
forv_CreationSet(cs, css) if (cs) if (cs->sym->constant) return 0;
}
}
return 1;
}
static void update_display(AEdge *e, EntrySet *es) {
// add any we need
for (int i = es->display.n; i < es->fun->sym->nesting_depth; i++)
if (i < e->from->display.n)
es->display.add(e->from->display[i]);
else
es->display.add(e->from);
// verify everything
for (int i = 0; i < es->fun->sym->nesting_depth; i++)
if (i < e->from->display.n)
assert(es->display[i] == e->from->display.v[i]);
else
assert(es->display[i] == e->from);
}
static void set_entry_set(AEdge *e, EntrySet *es = 0) {
EntrySet *new_es = es;
if (!es) {
new_es = new EntrySet(e->match->fun);
e->match->fun->ess.add(new_es);
}
e->to = new_es;
new_es->edges.put(e);
if (new_es->fun->sym->nesting_depth) update_display(e, new_es);
forv_MPosition(p, e->match->fun->positional_arg_positions) {
Var *v = e->match->fun->args.get(p);
AVar *av = make_AVar(v, new_es);
new_es->args.put(p, av);
}
fill_rets(new_es, e->pnode->lvals.n);
}
static AEdge *new_AEdge(Fun *f, PNode *p, EntrySet *from) {
AEdge *e = new AEdge;
e->pnode = p;
e->from = from;
e->fun = f;
return e;
}
static AEdge *new_AEdge(Match *m, PNode *p, EntrySet *from) {
AEdge *e = new AEdge;
e->pnode = p;
e->from = from;
e->fun = m->fun;
e->match = m;
return e;
}
static AEdge *copy_AEdge(AEdge *ee, EntrySet *to) {
AEdge *e = new_AEdge(ee->match, ee->pnode, ee->from);
set_entry_set(e, to);
if (!e->args.n) e->args.copy(ee->args);
if (!e->rets.n) e->rets.copy(ee->rets);
Vec<AEdge *> *ve = ee->from->out_edge_map.get(ee->pnode);
if (!ve) ee->from->out_edge_map.put(ee->pnode, (ve = new Vec<AEdge *>));
ve->set_add(e);
return e;
}
#if 0
static int
initial_compatibility(AEdge *e, EntrySet *es) {
forv_AEdge(ee, es->edges) if (ee)
forv_MPosition(p, e->match->fun->positional_arg_positions)
if (e->initial_types.get(p) != ee->initial_types.get(p))
return 0;
return 1;
}
#endif
static int entry_set_compatibility(AEdge *e, EntrySet *es) {
int val = INT_MAX;
if (e->match->fun->split_unique) return 0;
if (!edge_nest_compatible_with_entry_set(e, es)) return 0;
switch (edge_type_compatible_with_entry_set(e, es)) {
case 1:
break;
case 0:
#if 0
// eager splitting doesn't help
if (analysis_pass == 0 && !initial_compatibility(e, es))
return 0;
#endif
val -= 4;
break;
case -1:
return 0;
}
if (!edge_sset_compatible_with_entry_set(e, es)) val -= 2;
if (e->match->fun->clone_for_constants)
if (!edge_constant_compatible_with_entry_set(e, es)) val -= 1;
return val;
}
static AEdge *set_or_copy_AEdge(AEdge *e, EntrySet *es, Vec<AEdge *> &ees) {
if (!ees.n) {
set_entry_set(e, es);
ees.add(e);
return e;
} else {
AEdge *new_e = copy_AEdge(e, es);
ees.add(new_e);
return new_e;
}
}
static int find_best_entry_sets(AEdge *e, Vec<AEdge *> &edges) {
EntrySet *es = NULL;
int val = -1;
forv_EntrySet(x, e->match->fun->ess) {
int v = entry_set_compatibility(e, x);
if (v > 0 && v > val) {
es = x;
if (v == INT_MAX) break;
val = v;
}
}
if (es) {
set_or_copy_AEdge(e, es, edges);
return 1;
}
return 0;
}
static int check_edge(AEdge *e, EntrySet *es) {
form_MPositionAVar(x, e->args) {
if (!x->key->is_positional()) continue;
AType *filter = e->match->formal_filters.get(x->key);
AType *es_filter = es->filters.get(x->key);
if (filter) {
if (es_filter) filter = type_intersection(filter, es_filter);
} else
filter = es_filter;
if (filter && type_intersection(x->value->out, filter) == bottom_type) return 0;
}
return 1;
}
static int check_split(AEdge *e, Vec<AEdge *> &ees) {
if (!e->from) return 0;
if (Vec<EntrySet *> *ess = e->from->pending_es_backedge_map.get(e)) {
forv_EntrySet(es, *ess) set_or_copy_AEdge(e, es, ees);
return 1;
}
if (e->from->split) {
Vec<AEdge *> *m = e->from->split->out_edge_map.get(e->pnode);
if (m) {
forv_AEdge(ee, *m) if (ee) {
if (!check_edge(e, ee->to)) continue;
if (ee->match->fun == e->match->fun) {
if (e->match->fun->split_unique || !edge_nest_compatible_with_entry_set(e, ee->to)) {
set_entry_set(e);
e->to->split = ee->to;
ees.add(e);
return 1;
} else
set_or_copy_AEdge(e, ee->to, ees);
}
}
if (ees.n) return 1;
}
}
return 0;
}
// check results of last session read from disk
static int check_es_db(AEdge *e, Vec<AEdge *> &ees) { return 0; }
static void make_entry_set(AEdge *e, Vec<AEdge *> &edges, EntrySet *split = 0, EntrySet *preference = 0) {
if (e->to) {
edges.add(e);
return;
}
if (check_split(e, edges)) return;
if (check_es_db(e, edges)) return;
EntrySet *es = 0;
if (!split) {
if (find_best_entry_sets(e, edges)) return;
}
if (!es) es = preference;
set_entry_set(e, es);
if (!es) e->to->split = split;
edges.add(e);
}
void flow_var_type_permit(AVar *v, AType *t) {
if (!v->restrict)
v->restrict = t;
else
v->restrict = type_union(t, v->restrict);
AType *tt = type_intersection(v->in, v->restrict);
if (tt != v->out) {
assert(tt != top_type);
v->out = tt;
forv_AVar(vv, v->arg_of_send.asvec) {
if (!vv->in_send_worklist) {
vv->in_send_worklist = 1;
send_worklist.enqueue(vv);
}
}
forv_AVar(vv, v->forward) if (vv) update_in(vv, v->out);
}
}
// static inline void flow_var_type_permit(AVar *v, Sym *s) { flow_var_type_permit(v, make_abstract_type(s)); }
void add_var_constraint(AVar *av, Sym *s) {
if (!s) s = av->var->sym;
assert(s->type_kind != Type_VARIABLE);
s = unalias_type(s);
if (s->type && !s->is_pattern) {
if (s->is_external && (s->type->num_kind || s->type == sym_string || s->type->is_system_type))
update_gen(av, s->type->abstract_type);
if (s->is_constant) // for constants, the abstract type is the concrete
// type
update_gen(av, make_abstract_type(s));
if (s->is_symbol || s->is_fun) update_gen(av, make_abstract_type(s));
if (s->type_kind != Type_NONE) update_gen(av, make_abstract_type(s->meta_type));
}
}
AVar *get_element_avar(CreationSet *cs) {
if (!cs->sym->element) return 0;
AVar *elem = unique_AVar(cs->sym->element->var, cs);
cs->added_element_var = 1;
return elem;
}
void set_container(AVar *av, AVar *container) {
assert(!av->container || av->container == container);
av->container = container;
if (av->lvalue) av->lvalue->container = container;
}
void fill_tvals(Fun *fn, PNode *p, int n) {
p->tvals.fill(n);
for (int i = 0; i < n; i++) {
if (!p->tvals[i]) {
Sym *s = new_Sym();
s->nesting_depth = fn->sym->nesting_depth + 1;
s->in = fn->sym;
p->tvals[i] = new Var(s);
p->tvals[i]->is_internal = 1;
s->var = p->tvals[i];
fn->fa_all_Vars.add(p->tvals[i]);
}