summaryrefslogtreecommitdiff
path: root/libbanshee/engine/setst-sort.c
blob: 5d89817ed00a92b19fecd868f9aed69a185dbe18 (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
/*
 * Copyright (c) 2000-2001
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <regions.h>
#include <assert.h>
#include <stdio.h>
#include "bounds.h"
#include "setst-sort.h"


struct setst_union_
{
#ifdef NONSPEC
  sort_kind sort;
#endif
  int type;
  stamp st;
  gen_e_list exprs;
  gen_e_list proj_cache;
};

struct setst_inter_
{
#ifdef NONSPEC
  sort_kind sort;
#endif
  int type;
  stamp st;
  gen_e_list exprs;
};

struct setst_constant_
{
#ifdef NONSPEC
  sort_kind sort;
#endif
  int type;
  stamp st;
  char *name;
};

typedef struct setst_inter_ *setst_inter_;
typedef struct setst_union_ *setst_union_;
typedef struct setst_constant_ *setst_constant_;

static region tlb_cache_region;
static jcoll_dict tlb_dict;
static setst_var_list setst_vars;
static bool setst_changed = FALSE;

region setst_region;
term_hash setst_hash;
struct setst_stats setst_stats;

stamp setst_get_stamp(gen_e e)
{
#ifdef NONSPEC
  assert(e->sort == setst_sort);
#endif
  
  if ( ((setst_term)e)->type == VAR_TYPE)
    return st_get_stamp( (setst_var)e );
  
  else
    return ((setst_term)e)->st;
}

static bool eq(gen_e e1, gen_e e2)
{
  return ( setst_get_stamp(e1) == setst_get_stamp(e2) );
}

static gen_e_list get_union(gen_e e)
{
  assert ( ((setst_term)e)->type == UNION_TYPE);

  return ( (setst_union_) e)->exprs;
}

static gen_e_list get_inter(gen_e e)
{
  assert ( ((setst_term)e)->type == INTER_TYPE);

  return ( (setst_inter_) e)->exprs;
}

static void update_lower_bound(setst_var v, gen_e e)
{      
  if (setst_is_var(e))
    {
      if (st_add_lb(v,(setst_var)e))
	{
	  setst_stats.redundant_var++;
	}
      else
	{
	  setst_stats.added_var++;
	  setst_changed = TRUE;
	}
    }
  else
    {
      if (st_add_source(v, e,setst_get_stamp(e)))
	{
	  setst_stats.redundant_source++;
	}
      else
	{
	  setst_stats.added_source++;
	  setst_changed = TRUE;
	}
    }
  
}

static void update_upper_bound(setst_var v, gen_e e)
{
  assert(! setst_is_var(e));

  if (st_add_sink(v,e,setst_get_stamp(e)))
    {
      setst_stats.redundant_sink++;
    }
  else
    {
      setst_stats.added_sink++;
      setst_changed = TRUE;
    }
}


void setst_inclusion(con_match_fn_ptr con_match,gen_e e1, gen_e e2)
{
  if (eq(e1,e2))
    return;

  else if ( setst_is_zero(e1) || setst_is_one(e2) )
    return;

  else if (setst_is_union(e1))
    {
      gen_e_list_scanner scan;
      gen_e temp;

      gen_e_list exprs = get_union(e1);
      
      gen_e_list_scan(exprs,&scan);
      while (gen_e_list_next(&scan,&temp))
	{
	  setst_inclusion(con_match,temp,e2);
	}

      return;
    }

  else if (setst_is_inter(e2))
    {
      gen_e_list_scanner scan;
      gen_e temp;
      
      gen_e_list exprs = get_inter(e2);

      gen_e_list_scan(exprs,&scan);
      while (gen_e_list_next(&scan,&temp))
	{
	  setst_inclusion(con_match,e1,temp);
	}

      return;
    }

  else if (setst_is_var(e2))
    {
      setst_var v = (setst_var)e2;

      update_lower_bound(v,e1);
    }

  else if (setst_is_var(e1))
    {
      setst_var v = (setst_var)e1;

      update_upper_bound(v,e2);
    }

  else con_match(e1,e2);
}

#ifdef NONSPEC
static struct setst_term zero = {ZERO_TYPE,setst_sort,ZERO_TYPE};
static struct setst_term one  = {ONE_TYPE,setst_sort,ONE_TYPE};
#else
static struct setst_term zero = {ZERO_TYPE,ZERO_TYPE};
static struct setst_term one  = {ONE_TYPE,ONE_TYPE};
#endif /* NONSPEC */

gen_e setst_zero(void)
{
  return (gen_e)&zero;
}

gen_e setst_one(void)
{
  return (gen_e)&one;
}

gen_e setst_fresh(const char *name)
{
  setst_var v = st_fresh(setst_region,name);
  setst_var_list_cons(v,setst_vars);
  return (gen_e)v;
}

gen_e setst_fresh_large(const char *name)
{
  setst_var v = st_fresh_large(setst_region,name);
  setst_var_list_cons(v,setst_vars);
  return (gen_e)v;
}

gen_e setst_fresh_small(const char *name)
{
  setst_var v = st_fresh_small(setst_region,name);
  setst_var_list_cons(v,setst_vars);
  return (gen_e)v;
}

gen_e setst_constant(const char *str) deletes
{
  stamp st[2];
  gen_e result;
  char *name = rstrdup(setst_region,str);

  assert (str != NULL);
  
  st[0] = CONSTANT_TYPE;
  st[1] = stamp_string(name); 

  if ( (result = term_hash_find(setst_hash,st,2)) == NULL)
    {
      setst_constant_ c = ralloc(setst_region, struct setst_constant_);
      c->type = CONSTANT_TYPE;
      c->st = stamp_fresh();
      c->name = name;

      result = (gen_e) c;
      term_hash_insert(setst_hash,result,st,2);
      
      setst_stats.distinct_constants++;
      
      return result;
    }
  
  else
    {
      setst_stats.hashed_constants++;
      return result;
    }
}

static bool filter_zero(const gen_e e)
{
  return (!setst_is_zero(e));
}


static bool filter_one(const gen_e e)
{
  return (!setst_is_one(e));
}

gen_e setst_union(gen_e_list exprs) deletes
{
  gen_e_list filtered = gen_e_list_filter(setst_region,exprs,filter_zero);
  
  if ( gen_e_list_empty(filtered) )
    {
      setst_stats.filtered_unions++;
      return setst_zero();
    }
  else if (gen_e_list_length(filtered) == 1)
    {
      setst_stats.filtered_unions++;
      return gen_e_list_head(filtered);
    }

  else 
    {
      int i = 0;
      gen_e temp,result;
      gen_e_list_scanner scan;
      stamp st[ gen_e_list_length(filtered) + 1 ];
     
      st[0] = UNION_TYPE;

      gen_e_list_scan(filtered,&scan);
      while (gen_e_list_next(&scan,&temp))
	{
	  st[++i] = setst_get_stamp(temp);
	}
      
      if ( (result = 
	   term_hash_find(setst_hash,st,gen_e_list_length(filtered)+1)) 
	   == NULL )
	{
	  struct setst_union_ *u = ralloc(setst_region,struct setst_union_);
	  
	  u->type = UNION_TYPE;
	  u->st = stamp_fresh();
	  u->proj_cache = new_gen_e_list(setst_region);
	  u->exprs = filtered;
	 
	  result = (gen_e)u;
	  term_hash_insert(setst_hash,result,st,gen_e_list_length(filtered)+1);
	
	  setst_stats.distinct_unions++;
	  return result;
	}
      else
	{
	  setst_stats.hashed_unions++;
	  return result;
	}
    }
}

gen_e setst_inter(gen_e_list exprs) deletes
{
  gen_e_list filtered = gen_e_list_filter(setst_region,exprs,filter_one);
  
  if ( gen_e_list_empty(filtered) )
    {
      setst_stats.filtered_intersections++;
      return setst_one();
    }
  else if (gen_e_list_length(filtered) == 1)
    {
      setst_stats.filtered_intersections++;
      return gen_e_list_head(filtered);
    }

  else 
    {
      int i = 0;
      gen_e temp,result;
      gen_e_list_scanner scan;
      stamp st[ gen_e_list_length(filtered) + 1 ];
     
      st[0] = INTER_TYPE;

      gen_e_list_scan(filtered,&scan);
      while (gen_e_list_next(&scan,&temp))
	{
	  st[++i] = setst_get_stamp(temp);
	}
      
      if ( (result = 
	   term_hash_find(setst_hash,st,gen_e_list_length(filtered)+1)) 
	   == NULL )
	{
	  struct setst_inter_ *u = ralloc(setst_region,struct setst_inter_);
	  
	  u->type = UNION_TYPE;
	  u->st = stamp_fresh();
	  u->exprs = filtered;
	 
	  result = (gen_e)u;
	  term_hash_insert(setst_hash,result,st,gen_e_list_length(filtered)+1);
	  
	  setst_stats.distinct_intersections++;
	  
	  return result;
	}
      else 
	{
	  setst_stats.hashed_intersections++;
	  return result;
	}
    }
}


gen_e_list setst_get_union(gen_e e)
{
  assert (((setst_term)e)->type == UNION_TYPE);

  return ((setst_union_)e)->exprs;
}


gen_e_list setst_get_inter(gen_e e)
{
  assert (((setst_term)e)->type == INTER_TYPE);

  return ((setst_inter_)e)->exprs;
}

static void invalidate_tlb_cache(void)
{
  assert(tlb_cache_region);

  jcoll_delete_dict(tlb_dict);
  setst_var_list_app(setst_vars,st_clear_tlb_cache);
  deleteregion_ptr(&tlb_cache_region);
  
  tlb_cache_region = newregion();
  tlb_dict = jcoll_create_dict(tlb_cache_region,setst_get_stamp);
}

static void set_tlb_cache(setst_var v,jcoll j)
{
  st_set_tlb_cache(v,j);
}

static void collect_sinks(bounds b,setst_var v)
{
  gen_e sink;
  gen_e_list_scanner scan;

  gen_e_list_scan(st_get_sinks(v),&scan);

  while (gen_e_list_next(&scan,&sink))
    {
      bounds_add(b,sink,setst_get_stamp(sink));
    }  
}

static void collect_sources(bounds b, setst_var v)
{
  gen_e source;
  gen_e_list_scanner scan;

  gen_e_list_scan(st_get_sources(v),&scan);

  while (gen_e_list_next(&scan,&source))
    {
      bounds_add(b,source,setst_get_stamp(source));
    }  
}

static void collect_lower_bounds(bounds b, setst_var v)
{
  setst_var lb;
  setst_var_list_scanner scan;

  setst_var_list_scan(st_get_lbs(v),&scan);

  while (setst_var_list_next(&scan,&lb))
    {
      bounds_add(b,(gen_e)lb,st_get_stamp(lb));
    }  
}
 
static void apply_sources(setst_var witness, bounds sources)
{
  gen_e source;
  gen_e_list_scanner scan;

  gen_e_list_scan(bounds_exprs(sources),&scan);

  while (gen_e_list_next(&scan,&source))
    {
      if ( st_add_source(witness,source,setst_get_stamp(source)))
	setst_stats.redundant_source++;
	
      else
	setst_stats.added_source++;
    }  
}

static void apply_sinks(setst_var witness, bounds sinks)
{
  gen_e sink;
  gen_e_list_scanner scan;

  gen_e_list_scan(bounds_exprs(sinks),&scan);

  while (gen_e_list_next(&scan,&sink))
    {
      if (st_add_sink(witness,sink,setst_get_stamp(sink)))
	setst_stats.redundant_sink++;
	
      else
	setst_stats.added_sink++;
    }  
}

static void apply_lower_bounds(setst_var witness,bounds lower)
{
  gen_e lb;
  gen_e_list_scanner scan;

  gen_e_list_scan(bounds_exprs(lower),&scan);

  while (gen_e_list_next(&scan,&lb))
    {
      if (st_add_lb(witness,(setst_var)lb))
	setst_stats.redundant_var++;
      else
	setst_stats.added_var++;
    }  
}

static void collapse_cycle(setst_var witness, setst_var_list cycle) deletes
{
  setst_var_list_scanner var_scan;
  setst_var temp;
  region scratch_rgn = newregion();

  bounds sources = bounds_create(scratch_rgn);
  bounds sinks = bounds_create(scratch_rgn);
  bounds lower = bounds_create(scratch_rgn);
 

  setst_stats.cycles_collapsed++;

  /* force at least another iteration */
  setst_changed = TRUE;

  /* collect all bounds */
  setst_var_list_scan(cycle,&var_scan);
  while (setst_var_list_next(&var_scan,&temp))
    {
      collect_sources(sources,temp);
      collect_sinks(sinks,temp);
      collect_lower_bounds(lower,temp);  
    }
 
  /* unify all vars */
  st_unify(witness,cycle);

  /* add all bounds back */
  apply_sources(witness,sources);
  apply_sinks(witness,sinks);
  apply_lower_bounds(witness,lower);
 
  /* cleanup */
  bounds_delete(sources);
  bounds_delete(sinks);
  bounds_delete(lower);
  deleteregion(scratch_rgn);

  /* remove self edges */
  st_repair_bounds(witness);
}
/*
static bool cycle_detect(setst_var goal, setst_var_list path,
			 setst_var_list *result)
{
  int pos = st_get_path_pos(goal);
  setst_stats.cycles_searched++;
  
  if (pos)
    {  
      setst_var_list_scanner scan;
      setst_var temp;
      setst_var_list cycle = new_setst_var_list(tlb_cache_region);
      
      setst_var_list_scan(path,&scan);
      while(setst_var_list_next(&scan,&temp))
	{
	  if (st_get_path_pos(temp) >= pos)
	    setst_var_list_cons(temp,cycle);
	}
      
      *result = cycle;
      return TRUE;
    }
  
  else 
    return FALSE;
}

*/
static bool cycle_detect(setst_var goal, setst_var_list path,
			 setst_var_list *result)
{
  setst_var_list cycle = 
    setst_var_list_reverse(setst_var_list_copy(tlb_cache_region,path));

  setst_stats.cycles_searched++;

  while (!setst_var_list_empty(cycle) && 
	 !eq((gen_e)setst_var_list_head(cycle),(gen_e)goal))
    {
      setst_var_list_tail(cycle);
    }
  
  if (setst_var_list_empty(cycle))
    {
      return FALSE;
    }
  else
    {
      *result = cycle;
      return TRUE;
    }
}

static jcoll tlb_aux(gen_e e,int path_len,setst_var_list path) deletes
{
  if (setst_is_var(e))
    {
      setst_var_list cycle;
      setst_var v = (setst_var)e;
      if ( cycle_detect(v,path,&cycle) )
	{
	  setst_stats.cycles_length += setst_var_list_length(cycle);
	  collapse_cycle(v,cycle);
	  return NULL;
	}
      else
	{
	  if (st_get_tlb_cache(v) != NULL)
	    return st_get_tlb_cache(v);
	  else 
	    {
	      jcoll result;
	      setst_var_list_scanner scan;
	      setst_var lb;
	      jcoll_list jvars = new_jcoll_list(tlb_cache_region);
	      
	      gen_e_list sources = gen_e_list_copy(tlb_cache_region,
						   st_get_sources(v));
	      
	      st_set_path_pos(v,path_len);
	      setst_var_list_scan(st_get_lbs(v),&scan);
	      while (setst_var_list_next(&scan,&lb))
		{
		  setst_var_list_cons(v,path);
		  jcoll_list_cons(tlb_aux((gen_e)lb,++path_len,path),
				  jvars);
		  setst_var_list_tail(path); 
		}
	      
	      if (! gen_e_list_empty(sources))
		jcoll_list_cons(jcoll_create_chain(tlb_dict,sources),
				jvars);
	      result = jcoll_jjoin(tlb_dict,jvars);
	      set_tlb_cache(v,result);
	      st_set_path_pos(v,0);
	      return result;
	    }
	  
	}
    }
  else if (setst_is_union(e))
    {
      gen_e_list_scanner scan;
      gen_e temp;
      jcoll_list jexprs = new_jcoll_list(tlb_cache_region);
      
      gen_e_list_scan(setst_get_union(e),&scan);
      while (gen_e_list_next(&scan,&temp))
	{
	  jcoll_list_cons(tlb_aux(temp,++path_len,path),jexprs);
	}
      
      return jcoll_jjoin(tlb_dict,jexprs);
    }
  else
    {
      fail("Unmatched case in setst tlb computation\n");
      return NULL;
    }
}
static gen_e_list tlb(gen_e e)
{
  return jcoll_flatten(tlb_dict,
		       tlb_aux(e,1,new_setst_var_list(tlb_cache_region)) );
}
static void match_sinks(incl_fn_ptr setst_incl)
{
  gen_e_list_scanner tlb_scanner, sink_scanner;
  setst_var_list_scanner var_scanner;
  setst_var v;
  gen_e lb, sink;
  
  setst_var_list_scan(setst_vars,&var_scanner);
  
  while (setst_var_list_next(&var_scanner,&v))
    {
      gen_e_list tlbs = tlb((gen_e)v);
      gen_e_list snks = st_get_sinks(v);
      
      
      if(gen_e_list_empty(st_get_sinks(v)))
	{
	  setst_stats.no_sinks++;
	  continue;
	}
      else if(st_get_seen(v))
	{
	  setst_stats.incycle_vars++;
	  continue;
	}
      else if (gen_e_list_length(tlbs) == st_get_src_sz(v) 
	       && gen_e_list_length(snks) == st_get_snk_sz(v) )
	{
	  setst_stats.unchanged_vars++;
	  continue;
	}
      st_set_seen(v,TRUE);
      
      st_set_src_sz(v,gen_e_list_length(tlbs));
      st_set_snk_sz(v,gen_e_list_length(snks));
      
      gen_e_list_scan(tlbs,&tlb_scanner);
      
      while (gen_e_list_next(&tlb_scanner,&lb))
	{
	  gen_e_list_scan(snks,&sink_scanner);
	  
	  while (gen_e_list_next(&sink_scanner,&sink))
	    setst_incl(lb,sink);
	}
    }
}
static void iterate(incl_fn_ptr setst_incl)
{
  setst_var_list_scanner var_scanner;
  setst_var v; 
  /* static int iterations = 0; */
  setst_changed = FALSE;
  
  setst_var_list_scan(setst_vars,&var_scanner);
  while (setst_var_list_next(&var_scanner,&v))
    {
      st_set_seen(v,FALSE);
    }
  
  invalidate_tlb_cache();
  match_sinks(setst_incl);
  
  /*  fprintf(stderr,"Iterations : %d\n",++iterations); */
  
  if (setst_changed)
    iterate(setst_incl);
}  
gen_e_list setst_tlb(gen_e e,incl_fn_ptr setst_incl) deletes
{
  if (! setst_changed)
    {
      return tlb(e);
    }
  else
    {
      iterate(setst_incl);
      return tlb(e);
    }
  
}

void setst_set_proj_cache(gen_e e, gen_e elem)
{
  if (setst_is_union(e))
    {
      setst_union_ u = (setst_union_)e;
      gen_e_list_cons(elem,u->proj_cache);
    }
}

gen_e_list setst_get_proj_cache(gen_e e)
{

  if (setst_is_union(e))
    {
      setst_union_ u = (setst_union_)e;
      return u->proj_cache;
    }
  else
    {
      fail("Term does not cache projections\n");
      return NULL;
    }
}

void setst_init(void)
{
  setst_region = newregion();
  tlb_cache_region = newregion();
  setst_hash = make_term_hash(setst_region);
  setst_vars = new_setst_var_list(setst_region);
  tlb_dict = jcoll_create_dict(tlb_cache_region,setst_get_stamp);
}

void setst_reset(void) deletes
{
  term_hash_delete(setst_hash);
  deleteregion_ptr(&setst_region);

  setst_region = newregion();
  setst_hash = make_term_hash(setst_region);
  setst_vars = new_setst_var_list(setst_region);
  invalidate_tlb_cache();
  setst_changed = FALSE;
}

bool setst_is_zero(gen_e e)
{
  return ((setst_term)e)->type == ZERO_TYPE;
}

bool setst_is_one(gen_e e)
{  
  return ((setst_term)e)->type == ONE_TYPE;
}

bool setst_is_var(gen_e e)
{
  return ((setst_term)e)->type == VAR_TYPE;
}

bool setst_is_union(gen_e e)
{
  return ((setst_term)e)->type == UNION_TYPE;
}

bool setst_is_inter(gen_e e)
{
  return ((setst_term)e)->type == INTER_TYPE;
}

char *setst_get_constant_name(gen_e e)
{
  assert( ((setst_term)e)->type == CONSTANT_TYPE );
  
  return ((setst_constant_)e)->name;
}

void setst_print_stats(FILE *f)
{ 
  fprintf(f,"\n========== SetST Var Stats ==========\n");
  fprintf(f,"Fresh : %d\n",setst_stats.fresh); 
  fprintf(f,"Fresh Small : %d\n",setst_stats.fresh_small);
  fprintf(f,"Fresh Large : %d\n",setst_stats.fresh_large);
  fprintf(f,"Total : %d\n",setst_stats.fresh + setst_stats.fresh_small 
	  + setst_stats.fresh_large);
  fprintf(f,"\n========== SetST Sort Stats ==========\n");
  fprintf(f,"\n");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Additions");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Var:   %d\n",setst_stats.added_var);
  fprintf(f,"Source: %d\n",setst_stats.added_source);
  fprintf(f,"Sink:   %d",setst_stats.added_sink);
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Total:  %d",setst_stats.added_var + setst_stats.added_source
	  + setst_stats.added_sink);
  fprintf(f,"\n");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Redundant");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Var:   %d\n",setst_stats.redundant_var);
  fprintf(f,"Source: %d\n",setst_stats.redundant_source);
  fprintf(f,"Sink:   %d",setst_stats.redundant_sink);
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Total:  %d\n",
	 setst_stats.redundant_var + setst_stats.redundant_source
	  + setst_stats.redundant_sink);
 
  fprintf(f,"\n");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Iteration Optimizations");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Skipped vars:   %d\n",setst_stats.incycle_vars);
  fprintf(f,"Unchanged vars: %d\n",setst_stats.unchanged_vars);
  fprintf(f,"Vars w/o sinks: %d\n",setst_stats.no_sinks);
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Cycles");
  fprintf(f,"\n------------------------------\n");
  fprintf(f,"Collapsed:      %d\n",setst_stats.cycles_collapsed);
  fprintf(f,"Searched:       %d\n",setst_stats.cycles_searched);
  fprintf(f,"Hit rate:       %f\n",
	 ((float)setst_stats.cycles_collapsed)/((float)setst_stats.cycles_searched));
  fprintf(f,"Average Length: %f\n",
	 ((float)setst_stats.cycles_length) / ((float)setst_stats.cycles_collapsed));
  fprintf(f,"=====================================\n");
}