-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunicache.c
1641 lines (1389 loc) · 39.5 KB
/
unicache.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "cache.h"
#include "tcfg.h"
#include "address.h"
#include "common.h"
#include "bpred.h"
#include "loops.h"
#define CINFTY assoc_l2
extern tcfg_node_t** tcfg;
extern int num_tcfg_nodes;
extern prog_t prog;
/* The instruction set */
extern isa_t* isa;
extern int gcd(int a, int b);
extern int nsets, bsize, assoc;
/* sudiptac ::: adding options for level 1 data cache */
extern int nsets_dl1, bsize_dl1, assoc_dl1, cache_dl1_lat;
/* sudiptac ::: adding options for level 2 cache
* (it could be unified or separate instruction cache) */
extern int nsets_l2, bsize_l2, assoc_l2, cache_dl2_lat, cache_il2_lat;
extern int mem_lat[2];
extern int enable_il2cache;
extern int enable_ul2cache;
/* cleekee: bpred info */
extern int bpred_scheme;
extern de_inst_t ***mp_insts;
extern int *num_mp_insts;
#ifdef _DEBUG
static void dumpCacheBB(acs_p** acs, FILE* fp);
int analysis = 0;
int unified = 0;
#endif
int l1_d1_ps = 0;
int l1_i1_ps = 0;
int i1_u1_ps = 0;
int u1_d1_ps = 0;
int opt = 0;
extern ric_p getAddrBaseOffset(de_inst_t* inst, int base, int offset, int opt);
extern ric_p getAddrBaseIndex(de_inst_t* inst, int base, int index, int opt);
int X, Y, B;
/* Error message routine */
void prerr(char* msg) {
printf("PANIC ***** %s. Exiting now.......\n", msg);
exit(-1);
}
/* cleekee: Print ACS (debugging purpose) - can remove */
void printAcs(acs_p** acs_print) {
int i, numset;
acs_p* acs_set_print = NULL;
mem_blk_set_t* mem_blk_h_print = NULL;
for (numset = 0; numset < MAX_CACHE_SET; numset++) {
printf("set %i:\n", numset);
for (i = 0; i < CACHE_SET_SIZE; i++) {
acs_set_print = acs_print[numset];
if (acs_set_print != NULL) {
if (acs_set_print[i] != NULL
)
mem_blk_h_print = acs_set_print[i];
else
printf("-");
} else
printf("N/A");
while (mem_blk_h_print != NULL) {
printf("%i ", mem_blk_h_print->block);
mem_blk_h_print = mem_blk_h_print->next;
}
printf("\n");
}
}
printf("\n");
}
/* Create a cache set */
acs_p* makeCacheSet(void) {
acs_p* ret;
/* To accomodate the victim cache block, cache-set size is set
* one more than the given parameter */
ret = (acs_p *) malloc((CACHE_SET_SIZE + 1) * sizeof(acs_p));
CHECK_MEM(ret);
memset(ret, 0, (CACHE_SET_SIZE + 1) * sizeof(acs_p));
return ret;
}
/* Free a set of linked memory blocks */
void freeMemBlock(mem_blk_set_t* head) {
if (!head)
return;
freeMemBlock(head->next);
head->next = NULL;
#ifdef _DELETE
printf("Freeing mem block = %x\n", head);
#endif
free(head);
}
/* Free an abstract cache line */
void freeCacheLine(acs_p acl) {
if (!acl)
return;
freeMemBlock(acl);
}
/* Free an abstract cache set */
void freeCacheSet(acs_p* acs) {
int i;
if (!acs)
return;
for (i = 0; i <= CACHE_SET_SIZE; i++) {
freeCacheLine(acs[i]);
acs[i] = NULL;
}
free(acs);
}
/* Free an abstract cache state */
void freeCacheState(acs_p** acs) {
int i;
if (!acs)
return;
for (i = 0; i < MAX_CACHE_SET; i++) {
freeCacheSet(acs[i]);
acs[i] = NULL;
}
free(acs);
}
/* Get all memory referenced by this address range */
mem_blk_set_t* getMemoryBlocks(ric_p addr) {
mem_blk_set_t* mem_set = NULL;
mem_blk_set_t* temp;
int i;
int prev = -1;
int count = 0;
for (i = addr->lower_bound; i <= addr->upper_bound; i += addr->stride) {
if (prev == GET_MEM(i)
)
continue;
prev = GET_MEM(i);
temp = (mem_blk_set_t *) malloc(sizeof(mem_blk_set_t));
CHECK_MEM(temp);
/* Assume that all addresses are aligned */
temp->block = prev;
temp->next = mem_set;
mem_set = temp;
count++;
/* if number of memory blocks for this address set is more than
* the cache size then the cache is flushed */
if (count > MAX_CACHE_SET * CACHE_SET_SIZE
)
break;
if (!addr->stride)
break;
}
return mem_set;
}
/* Make an empty cache block */
acs_p makeEmpty(void) {
acs_p ret;
ret = (acs_p) malloc(sizeof(acs_s));
CHECK_MEM(ret);
/* Nothing in the cache block */
// ret->mem_blk_h = NULL;
return ret;
}
/* Returns 1 if a particular memory block is present in a given
* Cache block */
int isResident(mem_blk_set_t* mem_blk_h, mem_blk_set_t* item) {
mem_blk_set_t* iter;
for (iter = mem_blk_h; iter; iter = iter->next) {
if (iter->block == item->block)
return 1;
}
return 0;
}
/* Check for a given set of memory blocks whether its
* superset (may not be proper) is present in any of
* the cache block */
int checkForInclusionSingle(acs_p* acs_in, mem_blk_set_t* mem_blk_set) {
mem_blk_set_t* iter;
int i;
for (i = 0; i <= CACHE_SET_SIZE; i++) {
for (iter = mem_blk_set; iter; iter = iter->next) {
if (!acs_in[i])
continue;
else if (isResident(acs_in[i], iter))
break;
}
if (iter) {
return i;
}
}
/* oops .. Not found memory block in the cache */
return -1;
}
/* Count the number of memory blocks present */
int getCardinality(mem_blk_set_t* mem_blk_set) {
mem_blk_set_t* iter;
int count = 0;
for (iter = mem_blk_set; iter; iter = iter->next)
count++;
return count;
}
/* Make a copy of the cache block */
acs_p makeCopy(acs_p acs_in) {
acs_p ret = NULL;
mem_blk_set_t* iter;
if (!acs_in)
return NULL;
for (iter = acs_in; iter; iter = iter->next) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = ret;
ret = temp;
}
return ret;
}
/* Make a cache block from a set of memory blocks */
acs_p makeCacheBlock(mem_blk_set_t* mem_blk_set) {
acs_p ret = NULL;
mem_blk_set_t* iter;
if (!mem_blk_set)
return NULL;
for (iter = mem_blk_set; iter; iter = iter->next) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = ret;
ret = temp;
}
return ret;
}
/* Set intersection of the contents of two cache blocks */
/* and return the result */
acs_p Intersect(acs_p acs1, acs_p acs2) {
acs_p ret = NULL;
mem_blk_set_t* iter;
if (!acs1 || !acs2)
return NULL;
for (iter = acs2; iter; iter = iter->next) {
/* If the cache block is present in both of the
* argument cache blocks, then it is present in
* the return cache block. Useful for must
* analysis */
if (isResident(acs1, iter)) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = ret;
ret = temp;
}
}
return ret;
}
/* Set union of the contents of two cache blocks */
/* and return the result */
acs_p Union(acs_p acs1, acs_p acs2) {
acs_p ret = NULL;
mem_blk_set_t* iter;
if (!acs1 && !acs2)
return NULL;
if (!acs2)
return ret;
for (iter = acs2; iter; iter = iter->next) {
if (!isResident(ret, iter)) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = ret;
ret = temp;
}
}
return ret;
}
/* Set union of one cache block ans a set of memory blocks
*/
acs_p UnionCacheMem(acs_p acs1, mem_blk_set_t* mem_blk_set) {
acs_p ret = NULL;
mem_blk_set_t* iter;
if (!mem_blk_set)
return acs1;
for (iter = mem_blk_set; iter; iter = iter->next) {
if (!isResident(ret, iter)) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = ret;
ret = temp;
}
}
return ret;
}
/* check number of conflicting memory blocks with memory block containing
* instruction "cinst" inside basic block bbi */
static int
checkForConflicts(tcfg_node_t* bbi, de_inst_t* cinst, int age,
char first_word, int *conflicts) {
cfg_node_t* bb = bbi->bb;
de_inst_t* inst = bb->code;
int prev_blk = -1;
int blk;
int cblk;
int shift = 0;
if (first_word)
cblk = GET_MEM(cinst->addr);
else
cblk = GET_MEM(cinst->addr + SIZE_OF_WORD);
// There used to be a loop here that would go through each instruction
// in the basic block. Instead, all we need to know is how many entire
// cache lines lie between inst and cinst, divided by the number of
// cache sets to give the number of conflicting lines.
assert(cinst->addr >= inst->addr);
shift = (cinst->addr - inst->addr)/(MAX_CACHE_SET*SIZE_OF_BLOCK);
// age=0 if the cinst is in the first cache line of the bb
// the inst!=cinst is there to match the behaviour of the old loop,
// but that seems like a bug - i think it might be safe to remove that
// condition and get lower wcets.
if (inst != cinst && GET_MEM(inst->addr) == cblk) {
age = 0;
assert(shift == 0);
}
*conflicts = age + shift;
if (age != -1 && age + shift < CACHE_SET_SIZE
)
return 1;
return 0;
}
/* Check for a given set of memory blocks whether its
* superset (may not be proper) is present in the entire
* cache */
int checkForOnePresence(acs_p** acs_in, mem_blk_set_t* mem_blk_set) {
mem_blk_set_t* iter;
int i, k;
for (iter = mem_blk_set; iter; iter = iter->next) {
k = GET_SET(iter->block);
for (i = 0; i < CACHE_SET_SIZE; i++) {
if (acs_in[k][i] && isResident(acs_in[k][i], iter))
return 1;
}
}
return 0;
}
/* Check for a given set of memory blocks whether its
* superset (may not be proper) is present in the entire
* cache */
int checkForPresence(acs_p** acs_in, mem_blk_set_t* mem_blk_set) {
mem_blk_set_t* iter;
int i, k;
for (iter = mem_blk_set; iter; iter = iter->next) {
k = GET_SET(iter->block);
for (i = 0; i < CACHE_SET_SIZE; i++) {
if (acs_in[k] && acs_in[k][i]
&& isResident(acs_in[k][i], iter))
break;
}
if (i == CACHE_SET_SIZE
)
return -1;
}
/* return the position of memory block in the corresponding cache set */
return i;
}
/* Check for a given set of memory blocks whether its
* superset (may not be proper) is present in the entire
* cache */
int checkForVictim(acs_p** acs_in, mem_blk_set_t* mem_blk_set) {
mem_blk_set_t* iter;
int i;
for (iter = mem_blk_set; iter; iter = iter->next) {
for (i = 0; i < MAX_CACHE_SET; i++) {
if (acs_in[i][PSEUDO]
&& isResident(acs_in[i][PSEUDO], iter))
return 1;
}
}
return 0;
}
/* Check for a given set of memory blocks whether its
* superset (may not be proper) is present in any of
* the cache block */
int checkForInclusion(acs_p* acs_in, mem_blk_set_t* mem_blk_set) {
mem_blk_set_t* iter;
int i;
for (i = 0; i < CACHE_SET_SIZE; i++) {
for (iter = mem_blk_set; iter; iter = iter->next) {
if (acs_in[i] && !isResident(acs_in[i], iter))
break;
}
if (!iter)
return i;
}
/* oops .. Not found memory block in the cache */
return -1;
}
/* Get the memory blocks mapping to the same set */
mem_blk_set_t* getMemoryBlockOfSet(mem_blk_set_t* mem_blk, int set) {
mem_blk_set_t* head = NULL;
mem_blk_set_t* iter;
int st;
if (!mem_blk)
return NULL;
for (iter = mem_blk; iter; iter = iter->next) {
st = GET_SET(iter->block);
if (st == set) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = head;
head = temp;
}
}
return head;
}
/* Take the set difference of a cache line and a memory block */
acs_p Difference(acs_p acs, mem_blk_set_t* mem_blk) {
mem_blk_set_t* iter;
acs_p ret = NULL;
if (!acs)
return NULL;
for (iter = acs; iter; iter = iter->next) {
if (iter->block != mem_blk->block) {
mem_blk_set_t* temp = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = iter->block;
temp->next = ret;
ret = temp;
}
}
return ret;
}
/* Copy an abstract cache state */
acs_p* copy_cache(acs_p* acs) {
int i;
acs_p* dest = NULL;
if (!acs)
return NULL;
/* Allocate memory for the copying destination */
dest = makeCacheSet();
for (i = 0; i <= CACHE_SET_SIZE; i++) {
dest[i] = makeCopy(acs[i]);
}
return dest;
}
/* abstract cache set update for a singleton memory block */
acs_p* update_singleton(acs_p* acs, mem_blk_set_t* mem_blk_set) {
int line = 0;
acs_p* ret;
acs_p cur, free_p;
int i;
mem_blk_set_t* temp = NULL;
temp = (mem_blk_set_t *) malloc(sizeof(mem_blk_set_t));
CHECK_MEM(temp);
temp->block = mem_blk_set->block;
temp->next = NULL;
ret = makeCacheSet();
for (line = 0; line < CACHE_SET_SIZE; line++) {
/* block is not present in the cache line */
if (!acs) {
line = CACHE_SET_SIZE;
break;
}
/* block is present in the cache line */
if (acs[line] && isResident(acs[line], temp))
break;
}
/* The memory block is present in the cache */
if (line < CACHE_SET_SIZE)
{
for (i = 1; i < line; i++)
ret[i] = makeCopy(acs[i - 1]);
if (line > 0) {
cur = Difference(acs[line], temp);
ret[line] = Union(cur, acs[line - 1]);
#ifdef MEM_FREE
freeCacheLine(cur);
cur = NULL;
#endif
}
for (i = line + 1; i < CACHE_SET_SIZE; i++)
ret[i] = makeCopy(acs[i]);
ret[0] = makeCacheBlock(temp);
/* For May analysis */
if (line == 0 && acs[0]) {
cur = Difference(acs[0], temp);
free_p = ret[1];
ret[1] = Union(cur, ret[1]);
#ifdef MEM_FREE
freeCacheLine(cur);
cur = NULL;
if (free_p) {
freeCacheLine(free_p);
free_p = NULL;
}
#endif
}
}
/* The memory block is not present in the cache */
else {
if (acs) {
for (i = 1; i < CACHE_SET_SIZE; i++)
ret[i] = makeCopy(acs[i - 1]);
}
ret[0] = makeCacheBlock(temp);
if (acs) {
/* For persistence analysis collect the victim cache
* blocks */
ret[PSEUDO] = Union(acs[PSEUDO], acs[CACHE_SET_SIZE - 1]);
}
}
#ifdef MEM_FREE
free(temp);
#endif
return ret;
}
/* Must join of cache analysis */
acs_p* joinCacheMust(acs_p* acs1, acs_p* arg) {
acs_p temp = NULL;
acs_p val = NULL;
acs_p val2 = NULL;
int i, j;
acs_p* acs;
if (!acs1)
return copy_cache(arg);
acs = makeCacheSet();
/* Do cache join for all the abstract cache sets of a cache */
for (i = 0; i < CACHE_SET_SIZE; i++) {
temp = NULL;
val = NULL;
/* If one memory block is present in more than one cache
* block take the cache block which is older in age.
* Following code take care of this in case of must
* analysis. */
for (j = i; j >= 0; j--) {
val = temp;
val2 = Intersect(acs1[i], arg[j]);
temp = Union(temp, val2);
#ifdef MEM_FREE
freeCacheLine(val);
freeCacheLine(val2);
val = NULL;
#endif
}
for (j = i; j >= 0; j--) {
val = temp;
val2 = Intersect(acs1[i], arg[j]);
temp = Union(temp, val2);
#ifdef MEM_FREE
freeCacheLine(val);
freeCacheLine(val2);
val = NULL;
#endif
}
acs[i] = temp;
}
return acs;
}
/* May join of cache analysis */
acs_p* joinCacheMay(acs_p* acs1, acs_p* arg) {
acs_p temp = NULL;
acs_p val = NULL;
int i, j;
acs_p* acs;
mem_blk_set_t* iter;
if (!acs1)
return copy_cache(arg);
acs = makeCacheSet();
for (i = 0; i < CACHE_SET_SIZE; i++) {
temp = NULL;
val = NULL;
/* PRESENT in ACS-0 but not in ACS-1 */
if (acs1[i]) {
for (iter = acs1[i]; iter; iter = iter->next) {
for (j = i - 1; j >= 0; j--) {
if (arg[j] && isResident(arg[j], iter)) {
break;
}
}
if (j < 0) {
mem_blk_set_t* cur = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
cur->block = iter->block;
cur->next = NULL;
val = temp;
temp = UnionCacheMem(temp, cur);
#ifdef MEM_FREE
freeCacheLine(val);
free(cur);
cur = NULL;
val = NULL;
#endif
}
}
}
/* PRESENT in ACS-1 but not in ACS-0 */
if (arg[i]) {
for (iter = arg[i]; iter; iter = iter->next) {
for (j = i - 1; j >= 0; j--) {
if (acs1[j] && isResident(acs1[j], iter)) {
break;
}
}
if (j < 0) {
mem_blk_set_t* cur = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
cur->block = iter->block;
cur->next = NULL;
val = temp;
temp = UnionCacheMem(temp, cur);
#ifdef MEM_FREE
freeCacheLine(val);
free(cur);
val = NULL;
cur = NULL;
#endif
}
}
}
acs[i] = temp;
}
return acs;
}
/* Persistence join of cache analysis */
acs_p* joinCachePS(acs_p* acs1, acs_p* arg) {
acs_p temp = NULL;
acs_p val = NULL;
int i, j;
acs_p* acs;
mem_blk_set_t* iter;
if (!acs1)
return copy_cache(arg);
acs = makeCacheSet();
for (i = 0; i <= CACHE_SET_SIZE; i++) {
temp = NULL;
val = NULL;
if (acs1[i]) {
for (iter = acs1[i]; iter; iter = iter->next) {
for (j = i + 1; j <= CACHE_SET_SIZE; j++) {
if (arg[j] && isResident(arg[j], iter)) {
break;
}
}
if (j > CACHE_SET_SIZE)
{
mem_blk_set_t* cur = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
cur->block = iter->block;
cur->next = NULL;
val = temp;
temp = UnionCacheMem(temp, cur);
#ifdef MEM_FREE
freeCacheLine(val);
free(cur);
val = NULL;
cur = NULL;
#endif
}
}
}
if (arg[i]) {
for (iter = arg[i]; iter; iter = iter->next) {
for (j = i + 1; j <= CACHE_SET_SIZE; j++) {
if (acs1[j] && isResident(acs1[j], iter)) {
break;
}
}
if (j > CACHE_SET_SIZE)
{
mem_blk_set_t* cur = (mem_blk_set_t *) malloc(
sizeof(mem_blk_set_t));
cur->block = iter->block;
cur->next = NULL;
val = temp;
temp = UnionCacheMem(temp, cur);
#ifdef MEM_FREE
freeCacheLine(val);
free(cur);
val = NULL;
cur = NULL;
#endif
}
}
}
acs[i] = temp;
}
return acs;
}
/* Join function during data cache update. Depends on the
* direction of analysis (Must, May and Persistence) */
acs_p* joinCache(acs_p* acs1, acs_p* arg, ANALYSIS_T type) {
/* Join operation for for must cache analysis */
if (type == MUST) {
return joinCacheMust(acs1, arg);
}
/* Join function for MAY analysis */
else if (type == MAY) {
return joinCacheMay(acs1, arg);
}
/* Join function for persistence analysis */
else if (type == PERSISTENCE) {
return joinCachePS(acs1, arg);
}
return NULL;
}
/* Instruction cache update function */
acs_p** update_abs_inst(acs_p** acs_in, unsigned addr) {
mem_blk_set_t mem_blk;
acs_p* temp;
acs_p* cur;
acs_p** acs_out;
int set, i;
mem_blk.block = GET_MEM(addr);
mem_blk.next = NULL;
acs_out = (acs_p **) malloc(MAX_CACHE_SET * sizeof(acs_p *));
memset(acs_out, 0, MAX_CACHE_SET * sizeof(acs_p *));
if (acs_in) {
for (i = 0; i < MAX_CACHE_SET; i++)
acs_out[i] = copy_cache(acs_in[i]);
}
/* Each instruction corresponds to two addresses. Update
* the instruction cache state accordingly */
set = GET_SET(mem_blk.block);
if (acs_in)
temp = update_singleton(acs_in[set], &mem_blk);
else
temp = update_singleton(NULL, &mem_blk);
mem_blk.block = GET_MEM(addr + SIZE_OF_WORD);
set = GET_SET(mem_blk.block);
cur = acs_out[set];
acs_out[set] = update_singleton(temp, &mem_blk);
/* free up memory */
#ifdef MEM_FREE
freeCacheSet(temp);
freeCacheSet(cur);
temp = cur = NULL;
#endif
return acs_out;
}
/* Check whether two abstract cache blocks are same or not */
int is_same_cache_block(acs_p acs1, acs_p acs2) {
mem_blk_set_t* iter;
if (!acs1 && !acs2)
return 1;
if (!acs1 || !acs2)
return 0;
for (iter = acs1; iter; iter = iter->next) {
if (!isResident(acs2, iter))
return 0;
}
if (getCardinality(acs1) == getCardinality(acs2))
return 1;
return 0;
}
/* Check whether two abstract cache states are identical */
int checkEquality(acs_p* acs1, acs_p* acs2) {
int i;
if (!acs1 && !acs2)
return 1;
if (!acs1 || !acs2)
return 0;
for (i = 0; i <= CACHE_SET_SIZE; i++) {
if (!acs1[i] && !acs2[i])
continue;
else if (!acs1[i] || !acs2[i])
return 0;
else if (!is_same_cache_block(acs1[i], acs2[i]))
return 0;
}
return 1;
}
/* Transfer/update function for l2 instruction abstract cache */
void transforml2InstCacheState(tcfg_node_t* bbi, int* change_flag,
ANALYSIS_T type, int iteration_count) {
de_inst_t* inst;
int n_inst;
acs_p** acs_out = NULL;
acs_p** cur_acs = NULL;
acs_p** prev_acs;
acs_p* cur_acs_set;
int k;
assert(bbi);
assert(bbi->bb);
inst = bbi->bb->code;
/* save a copy to check the change in abstract cache states */
acs_out = (acs_p **) malloc(MAX_CACHE_SET * sizeof(acs_p *));
CHECK_MEM(acs_out);
memset(acs_out, 0, MAX_CACHE_SET * sizeof(acs_p *));
if (!bbi->acs_out) {
bbi->acs_out = (acs_p **) malloc(MAX_CACHE_SET * sizeof(acs_p *));
CHECK_MEM(bbi->acs_out);
memset(bbi->acs_out, 0, MAX_CACHE_SET * sizeof(acs_p *));
}
for (k = 0; k < MAX_CACHE_SET; k++) {
acs_out[k] = copy_cache(bbi->acs_out[k]);
if (!bbi->acs_out[k])
bbi->acs_out[k] = makeCacheSet();
}
for (n_inst = 0; n_inst < bbi->bb->num_inst; n_inst++) {
/* Use cache access classification method */
if (inst_chmc_l1[bbi->id][n_inst] == ALL_MISS) {
cur_acs = bbi->acs_out;
/* cleekee: bbi->acs_out is already updated if bpred is enabled */
if (!n_inst && bpred_scheme == NO_BPRED)
bbi->acs_out = update_abs_inst(bbi->acs_in, inst->addr);
else
bbi->acs_out = update_abs_inst(bbi->acs_out, inst->addr);
#ifdef MEM_FREE
freeCacheState(cur_acs);
cur_acs = NULL;
#endif
} else if (inst_chmc_l1[bbi->id][n_inst] == NOT_CLASSIFIED
|| inst_chmc_l1[bbi->id][n_inst] == PS) {
cur_acs = bbi->acs_out;
/* cleekee: bbi->acs_out is already updated if bpred is enabled */
if (!n_inst && bpred_scheme == NO_BPRED) {
prev_acs = bbi->acs_in;
bbi->acs_out = update_abs_inst(bbi->acs_in, inst->addr);
} else {
prev_acs = bbi->acs_out;
bbi->acs_out = update_abs_inst(bbi->acs_out, inst->addr);
}
for (k = 0; k < MAX_CACHE_SET; k++) {
cur_acs_set = bbi->acs_out[k];
bbi->acs_out[k] = joinCache(prev_acs[k], bbi->acs_out[k], type);
#ifdef MEM_FREE
freeCacheSet(cur_acs_set);
cur_acs_set = NULL;
#endif
}
#ifdef MEM_FREE
freeCacheState(cur_acs);
cur_acs = NULL;
#endif
}
/* L2 cache is not accessed at all. So no change in abstract cache state,
* just copy the incoming abstract cache state */
else {
if (!n_inst) {
for (k = 0; k < MAX_CACHE_SET; k++) {