-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.cpp
2013 lines (1621 loc) · 58.7 KB
/
schedule.cpp
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
//
// Created by Dejan Grubisic on 11/6/2019.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cerrno>
#include <string.h>
#include <list>
#include<tuple>
#include <vector>
#include <array>
#include <stack>
#include <climits>
#include <unordered_map>
#include <sstream>
#include <set>
#include <algorithm>
using namespace std;
enum State {
s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14,
s15, s16, s17, s18, s19, s20, s21, s22, s23, s24, s25, s26, s27,
s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, sComma, sErr
};
enum Category {
EMPTY, MEMOP, LOADI, ARITHOP, OUTPUT, NOP, INTO, COMMA, CONST, REG, COMMENT
};
static const char *CategoryStr[] = {"EMPTY", "MEMOP", "LOADI", "ARITHOP", "OUTPUT", "NOP", "INTO", "COMMA", "CONST",
"REG"};
enum Operation {
load, store, loadI, add, sub, mult, lshift, rshift, output, nop, err
};
static const char *OperationStr[] = {"load", "store", "loadI", "add", "sub", "mult", "lshift", "rshift", "output",
"nop"};
// 0 - NU, 1 - PR, 2 - VR, 3 - SR
enum RegType {
NU, PR, VR, SR
};
static const char *RegTypeStr[] = {"nu", "pr", "vr", "sr"};
enum RegNum {
R1, R2, R3, Rnone
};
struct InstructionIR {
Operation opcode;
int line_num;
// R1, R2 => R3
// 0 - NU, 1 - PR, 2 - VR, 3 - SR
int registers[3][4];
int constant;
InstructionIR() {
opcode = err;
for (int i = 0; i < 12; ++i) {
registers[i / 4][i % 4] = INT_MAX;
}
constant = -1;
}
InstructionIR(int line, Operation s) : line_num(line), opcode(s) {}
InstructionIR(Operation s, int r1, int r2, int r3, int c, RegType rType = SR) {
opcode = s;
registers[R1][rType] = r1;
registers[R2][rType] = r2;
registers[R3][rType] = r3;
constant = c;
}
string showReg(RegType RT) const {
string rt = "r"; //RegTypeStr[RT];//"r";//
string s1 = (opcode == loadI || opcode == output) ? to_string(constant) : "";
stringstream res;
res << OperationStr[opcode] << " ";
if (opcode != nop) {
res << ((registers[R1][RT] != INT_MAX) ? rt + to_string(registers[R1][RT]) : s1)
<< ((registers[R2][RT] != INT_MAX) ? ", " + rt + to_string(registers[R2][RT]) : "")
<< ((registers[R3][RT] != INT_MAX) ? " => " + rt + to_string(registers[R3][RT]) : "");// << endl;
}
return res.str();
}
void showAllRegs() const {
int tmpReg;
cout << line_num << " opcode " << "\t Const " << "\tR1 (SR,VR,PR,LU) " << " R2 " << "\t R3 " << endl;
cout << OperationStr[opcode] << "\t" << constant << "\t";
for (int i = 0; i < 3; ++i) {
for (int j = 3; j >= 0; --j) {
tmpReg = registers[i][j];
cout << ((j == 3) ? " | " : " ") << ((tmpReg != INT_MAX) ? to_string(tmpReg) : "-") << " ";
}
}
cout << endl;
}
};
struct InstructionBlock {
list <InstructionIR> instructions;
// showIR - print IR in human readable form
void showInstructions() const {
if (instructions.empty()) {
cerr << "ERROR: Not valid instructions, run terminates." << endl;
return;
}
for (const InstructionIR &inst : instructions) {
switch (inst.opcode) {
case load:
cout << "load\t [ sr" << inst.registers[R1][SR] << " ], [ ], [ sr" << inst.registers[R3][SR] << " ]"
<< endl;
break;
case store:
cout << "store\t [ sr" << inst.registers[R1][SR] << " ], [ ], [ sr" << inst.registers[R3][SR]
<< " ]"
<< endl;
break;
case loadI:
cout << "loadI\t [ val " << inst.constant << " ], [ ], [ sr" << inst.registers[R3][SR] << " ]"
<< endl;
break;
case output:
cout << OperationStr[inst.opcode] << "\t [ val " << inst.constant << " ], [ ], [ ]" << endl;
break;
case nop:
cout << "nop\t [ ], [ ], [ ]" << endl;
break;
default: //
cout << OperationStr[inst.opcode] << "\t [ sr" << inst.registers[R1][SR] << " ], [ sr"
<< inst.registers[R2][SR] << " ], [ sr" << inst.registers[R3][SR] << " ]" << endl;
break;
}
}
}
void showIR(RegType rt = SR) const {
if (instructions.empty()) {
cerr << "ERROR: Not valid instructions, run terminates." << endl;
return;
}
for (const InstructionIR &inst : this->instructions) {
cout << inst.showReg(rt) << endl;
}
}
void showAllRegs() const {
if (instructions.empty()) {
cerr << "ERROR: Not valid instructions, run terminates." << endl;
return;
}
for (const InstructionIR &inst : this->instructions) {
inst.showAllRegs();
}
}
};
enum InputMode {
s, h
};
// Parsing funcitons
tuple<InputMode, int, char *> manageInput(int argc, char *argv[]);
//***********************************************************************************************************
int strToInt(const string &str);
void takeErrWord(int line_num, const string &line, int &char_pos, string &err_word);
InstructionBlock scan(char *filename, bool check_semantics, bool display_tokens);
vector <pair<Category, string>> processLine(const string &line, int line_num, bool display_tokens);
pair <State, Category> nextState(State state, char new_char, string &lexeme);
InstructionIR parse(const vector <pair<Category, string>> &words, int line_num);
InstructionIR setupInstruction(InstructionIR &inst, const vector <pair<Category, string>> &words, int line_num);
InstructionIR
checkSemantics(const vector <pair<Category, string>> &words, const vector <Category> &grammar, int line_num);
//***********************************************************************************************************
//TODO: maybe put vector for srToVr and lastUse
struct TableSrVr {
int *srToVr;
int *lastUse;
int n;
TableSrVr(int size) : n(size) {
try {
srToVr = new int[size];
lastUse = new int[size];
} catch (bad_alloc &ba) {
cerr << "ERROR ALLOCATING MEMORY: bad_alloc caugth: " << ba.what() << endl;
exit(1);
}
for (int i = 0; i < size; ++i) {
srToVr[i] = INT_MAX;
lastUse[i] = INT_MAX;
}
}
~TableSrVr() {
delete[] srToVr;
delete[] lastUse;
}
void show() const {
cout << endl << "srToVr: ";
for (int i = 0; i < n; ++i)
cout << srToVr[i] << ", ";
cout << endl << "LU: ";
for (int i = 0; i < n; ++i)
cout << lastUse[i] << ", ";
cout << endl;
}
};
//Registar Renaming
int registerRenaming(const InstructionBlock &block);
int regNormalize(InstructionBlock &block) {
int cnt = 0;
unordered_map<int, int> regMap;
// cout<<"Reg Normalize: "<<endl;
for (auto &ir: block.instructions) {
for (int i = 0; i < 3; ++i) {
if (ir.registers[i][SR] == INT_MAX)
continue;
if (regMap.find(ir.registers[i][SR]) != regMap.end()) {
ir.registers[i][SR] = regMap[ir.registers[i][SR]];
} else {
// cout<<cnt<<" SR = "<< ir.registers[i][SR] <<" => SR_norm = "<<cnt<<endl;
regMap[ir.registers[i][SR]] = cnt;
ir.registers[i][SR] = cnt++;
}
}
}
return regMap.size();
}
void opDef(int regs[][4], TableSrVr &table, int &vrName, RegNum Ri = R3) {
//OP defines
if (table.srToVr[regs[Ri][SR]] == INT_MAX) { //unused srToVr
table.srToVr[regs[Ri][SR]] = vrName++;
}
regs[R3][VR] = table.srToVr[regs[Ri][SR]];
regs[R3][NU] = table.lastUse[regs[Ri][SR]];
table.srToVr[regs[Ri][SR]] = INT_MAX;
table.lastUse[regs[Ri][SR]] = INT_MAX;
}
void opUse(int regs[][4], TableSrVr &table, int &vrName, RegNum Ri, const int &index) {
//OP uses
// cout << "opUse"<<endl;
// cout << regs[Ri][SR]<<endl;//<<" "<<table.srToVr[regs[Ri][SR]]<<endl;
if (table.srToVr[regs[Ri][SR]] == INT_MAX) { //last Use
table.srToVr[regs[Ri][SR]] = vrName++;
}
regs[Ri][VR] = table.srToVr[regs[Ri][SR]];
regs[Ri][NU] = table.lastUse[regs[Ri][SR]];
table.lastUse[regs[Ri][SR]] = index;
}
void setVR(list<InstructionIR>::iterator ins, TableSrVr &table, int &vrName, const int &index) {
// cout << "SET VR"<<endl;
switch (ins->opcode) {
case load:
opDef(ins->registers, table, vrName, R3);
opUse(ins->registers, table, vrName, R1, index);
break;
case loadI:
opDef(ins->registers, table, vrName, R3);
break;
case store:
opUse(ins->registers, table, vrName, R1, index);
opUse(ins->registers, table, vrName, R3, index);
break;
case output:
break;
default: //ARITH
opDef(ins->registers, table, vrName, R3);
opUse(ins->registers, table, vrName, R1, index);
opUse(ins->registers, table, vrName, R2, index);
break;
}
// cout << endl << index << " " << OperationStr[ins->opcode] << ": vrName = " << vrName;
// table.show();
}
int registerRenaming(InstructionBlock &block) {
int vrName = 0;
int index = block.instructions.size() - 1; //just to come to 0
int maxReg = regNormalize(block);
TableSrVr table(maxReg);
// int maxLive = 0;//TODO: maybe this info will be useful
// cout << "Num Reg = " << maxReg << endl;
// block.showIR(SR);
//Go from end of list to begin
for (auto it = prev(block.instructions.end()); it != prev(block.instructions.begin()); --it) {
if (it->opcode == nop) {
it = block.instructions.erase(it);
continue;
}
setVR(it, table, vrName, index);
index--;
}
return vrName;
}
//***********************************************************************************************************
//TODO: Scheduling
struct PrioritySet {
set <pair<int, int>> q;
PrioritySet() {}
bool empty() {
return q.empty();
}
int size() {
return q.size();
}
bool contains(int node_id) {
auto it = find_if(q.begin(), q.end(), [node_id](const pair<int, int> &p) { return p.second == node_id; });
if (it != q.end())
return true;
else
return false;
}
pair<int, int> get( set<pair < int, int>>::iterator it){
return *it;
}
void decKey(pair<int, int> member, int newKey) {
auto it = q.find(member);
if (it != q.end()) {
q.erase(it);
q.insert(make_pair(newKey, member.second));
} else {
insertKey(newKey, member.second);
}
}
void incKey(int key, int value) {
auto it = find_if(q.begin(), q.end(), [value](const pair<int, int> &p) { return p.second == value; });
// There is already key with same id
if (it != q.end()) {
if(it->first < key){
q.erase(it);
insertKey(key, value);
}
} else {
insertKey(key, value);
}
}
void insertKey(int key, int value) {
q.insert(make_pair(key, value));
}
set<pair<int, int>>::iterator erase(set<pair<int, int>>::iterator it){
if (it == q.end())
return it;
return q.erase(it);
}
void erase(int node_id) {
auto it = find_if(q.begin(), q.end(), [node_id](const pair<int, int> &p) { return p.second == node_id; });
if (it != q.end())
q.erase(it);
}
pair<int, int> getMin() {
auto it = q.begin();
pair<int, int> min = *it;
q.erase(it);
return min;
}
pair<int, int> getMax() {
auto rit = q.rbegin();
pair<int, int> max = *rit;
q.erase(next(rit).base());
return max;
}
void show() {
for (auto &x: q) {
cout << "< " << x.first << ", " << x.second << " > ";
}
cout << endl;
}
};
struct PriorityMultiSet {
multiset <pair<int, int>> q;
PriorityMultiSet() {}
bool empty() {
return q.empty();
}
int size() {
return q.size();
}
bool contains(int node_id) {
auto it = find_if(q.begin(), q.end(), [node_id](const pair<int, int> &p) { return p.second == node_id; });
if (it != q.end())
return true;
else
return false;
}
pair<int, int> get( set<pair < int, int>>::iterator it){
return *it;
}
void decKey(pair<int, int> member, int newKey) {
auto it = q.find(member);
if (it != q.end()) {
q.erase(it);
q.insert(make_pair(newKey, member.second));
} else {
insertKey(newKey, member.second);
}
}
void incKey(int key, int value) {
auto it = find_if(q.begin(), q.end(), [value](const pair<int, int> &p) { return p.second == value; });
// There is already key with same id
if (it != q.end()) {
if(it->first < key){
q.erase(it);
insertKey(key, value);
}
} else {
insertKey(key, value);
}
}
void insertKey(int key, int value) {
q.insert(make_pair(key, value));
}
set<pair<int, int>>::iterator erase(set<pair<int, int>>::iterator it){
if (it == q.end())
return it;
return q.erase(it);
}
void erase(int node_id) {
auto it = find_if(q.begin(), q.end(), [node_id](const pair<int, int> &p) { return p.second == node_id; });
if (it != q.end())
q.erase(it);
}
pair<int, int> getMin() {
auto it = q.begin();
pair<int, int> min = *it;
q.erase(it);
return min;
}
pair<int, int> getMax() {
auto rit = q.rbegin();
pair<int, int> max = *rit;
q.erase(next(rit).base());
return max;
}
void show() {
for (auto &x: q) {
cout << "< " << x.first << ", " << x.second << " > ";
}
cout << endl;
}
};
pair<list<int>, int> opUseDef(const InstructionIR &ins, RegType reg) {
// cout<<"OP: "<<OperationStr[op]<<endl;
switch (ins.opcode) {
case load:
return make_pair(list < int > {ins.registers[R1][reg]}, ins.registers[R3][reg]);
case loadI:
return make_pair(list < int > {}, ins.registers[R3][reg]);
case store:
if (ins.registers[R1][reg] != ins.registers[R3][reg])
return make_pair(list < int > {ins.registers[R1][reg], ins.registers[R3][reg]}, INT_MAX);
else
return make_pair(list < int > {ins.registers[R1][reg]}, INT_MAX);
case output:
return make_pair(list < int > {}, INT_MAX);
default: //ARITH
if (ins.registers[R1][reg] != ins.registers[R2][reg])
return make_pair(list < int > {ins.registers[R1][reg], ins.registers[R2][reg]}, ins.registers[R3][reg]);
else
return make_pair(list < int > {ins.registers[R1][reg]}, ins.registers[R3][reg]);
}
}
struct Edge {
int from;
int to;
int weight;
bool typeIO;
Edge (){}
Edge(int from, int to, int weight, bool IO = false) : from(from), to(to), weight(weight), typeIO(IO) {}
string show() const {
stringstream ss;
ss << from << " -> " << to << " [ label = \"" << weight << (typeIO ? " (IO)" : "") << "\" ];";
return ss.str();
}
bool operator ==(const Edge &edgeOther){
if(from == edgeOther.from && to == edgeOther.to &&
weight == edgeOther.weight && typeIO == edgeOther.typeIO){
return true;
}
return false;
}
};
int myHash(int a, int b){
return (a<<16) + b;
}
struct Node {
unordered_map <int, Edge> edgeOut;
unordered_map <int,Edge> edgeIn;
int id;
InstructionIR ins;
int priority; // - priority of scheduling
int memLoc;
int latency;
int fakeLoc; // -
Node() {
priority = 0;
ins = InstructionIR();
memLoc = INT_MAX;
fakeLoc= INT_MAX;
}
void insertEdgeIn(Edge edge) {
// for (auto &x: edgeIn) {
// if (x.from == edge.from && x.to == edge.to) {
// if (x.weight < edge.weight) {
// x.weight = edge.weight;
// x.typeIO = edge.typeIO;
// }
// return;
// }
// }
// edgeIn.push_back(edge);
if (edgeIn.count(myHash(edge.from, edge.to)) != 0){
if(edgeIn[myHash(edge.from, edge.to)].weight < edge.weight){
edgeIn[myHash(edge.from, edge.to)].weight = edge.weight;
edgeIn[myHash(edge.from, edge.to)].typeIO = edge.typeIO;
}
return;
}
edgeIn[myHash(edge.from, edge.to)] = edge;
}
void insertEdgeOut(Edge edge) {
// for (auto &x: edgeOut) {
// if (x.from == edge.from && x.to == edge.to) {
// if (x.weight < edge.weight) {
// x.weight = edge.weight;
// x.typeIO = edge.typeIO;
// }
// return;
// }
// }
// edgeOut.push_back(edge);
if (edgeOut.count(myHash(edge.from, edge.to)) != 0){
if(edgeOut[myHash(edge.from, edge.to)].weight < edge.weight){
edgeOut[myHash(edge.from, edge.to)].weight = edge.weight;
edgeOut[myHash(edge.from, edge.to)].typeIO = edge.typeIO;
}
return;
}
edgeOut[myHash(edge.from, edge.to)] = edge;
return;
}
};
struct Graph {
vector <Node> nodes;
// load, store, loadI, add, sub, mult, lshift, rshift, output, nop, err
int latencyTable[10] = {5, 5, 1, 1, 1, 3, 1, 1, 1, 1};
vector<int> regToNode;
vector <array<int, 3>> listIO; //line_num, Opcode, mem_loc
vector<int> vrConst; //key - vr, val = memLoc
vector<int> vrLatency; //key - vr, val = latency of instr that defines vr
unordered_map<int, int> memoryMap;
list<int> unknownLoadList;
list<int> vrUnknown;
Graph(int insNum, int regNum) : nodes(insNum), regToNode(regNum), vrConst(regNum, INT_MAX), vrLatency(regNum){}
void insertEdge(Edge edge) {
// cout<<edge.show()<<endl;
nodes[edge.from].insertEdgeOut(edge);
nodes[edge.to].insertEdgeIn(edge);
}
void addToListIO(const InstructionIR &ins, const pair<list<int>, int> &useDef) {
int tmp;
switch (ins.opcode) {
case load:
tmp = vrConst[useDef.first.front()];
//Propagate constant from memory, if there is not in memory invalidate vrConst
if(memoryMap.count(tmp) != 0){
vrConst[useDef.second] = memoryMap[tmp];
}else{
vrConst[useDef.second] = INT_MAX;
vrUnknown.push_back(useDef.second);
if(tmp != INT_MAX){ //load that read from uninitialized memory
unknownLoadList.push_back(ins.line_num);
}
}
break;
case store:
tmp = vrConst[useDef.first.back()];
//Store constants in MemoryList if you know address
if( tmp != INT_MAX){
memoryMap[tmp] = vrConst[useDef.first.front()];
}
break;
case output:
tmp = ins.constant;
break;
default:;
}
listIO.push_back({ins.line_num, ins.opcode, tmp});
}
void addEdgesIO(const InstructionIR &ins, const pair<list<int>, int> &useDef) {
array<int, 3> lastInst; // format <ins_id, op, memLoc> //listIO is list of all Load Store Output instructions
bool lastOutput = true;
bool lastStore = true;
addToListIO(ins, useDef);
lastInst = listIO.back();
nodes[lastInst[0]].memLoc = lastInst[2]; // write location into node in graph
if (lastInst[1] == store) {
for (auto rit = listIO.rbegin() + 1; rit != listIO.rend(); ++rit) {
if (lastInst[2] == INT_MAX || (*rit)[2] == INT_MAX || lastInst[2] == (*rit)[2]) {
insertEdge(Edge((*rit)[0], lastInst[0], 1, true)); //should be 1 instead of 5
break;
}
}
} else if (lastInst[1] == load){
for (auto rit = listIO.rbegin() + 1; rit != listIO.rend(); ++rit) {
if ((*rit)[1] == store &&
(lastInst[2] == INT_MAX || (*rit)[2] == INT_MAX || lastInst[2] == (*rit)[2])) {
insertEdge(Edge((*rit)[0], lastInst[0], 5, true));
break;
}
}
}else{ //output
for (auto rit = listIO.rbegin() + 1; rit != listIO.rend(); ++rit) {
if(lastStore && (*rit)[1] == store && (lastInst[2] == INT_MAX || (*rit)[2] == INT_MAX || lastInst[2] == (*rit)[2])) {
insertEdge(Edge((*rit)[0], lastInst[0], 5, true));
lastStore = false;
}else if (lastOutput && (*rit)[1] == output){
insertEdge(Edge((*rit)[0], lastInst[0], 1, true));
lastOutput = false;
}else if(lastOutput == false && lastStore == false){
break;
}
}
}
}
bool constantPropagation(const InstructionIR &ins, const pair<list<int>, int> &useDef, bool fakePropagation = false) {
if (ins.opcode == loadI) {
vrConst[useDef.second] = ins.constant;
nodes[ins.line_num].memLoc = ins.constant;
return true;
}
//add, sub, mult, lshift, rshift
if (useDef.first.size() == 1 && vrConst[useDef.first.front()] != INT_MAX) {
switch (ins.opcode) {
case add:
vrConst[useDef.second] = vrConst[useDef.first.front()] << 1;
break;
case sub:
vrConst[useDef.second] = 0;
break;
case mult:
vrConst[useDef.second] = vrConst[useDef.first.front()] * vrConst[useDef.first.front()];
break;
case lshift:
vrConst[useDef.second] = vrConst[useDef.first.front()] << vrConst[useDef.first.front()];
break;
case rshift:
vrConst[useDef.second] = vrConst[useDef.first.front()] >> vrConst[useDef.first.front()];
break;
default:;
}
if(fakePropagation == false)
nodes[ins.line_num].memLoc = vrConst[useDef.second];
return true;
} else if (useDef.first.size() == 2 &&
vrConst[useDef.first.front()] != INT_MAX && vrConst[useDef.first.back()] != INT_MAX) {
switch (ins.opcode) {
case add:
vrConst[useDef.second] = vrConst[useDef.first.front()] + vrConst[useDef.first.back()];
break;
case sub:
vrConst[useDef.second] = vrConst[useDef.first.front()] - vrConst[useDef.first.back()];
break;
case mult:
vrConst[useDef.second] = vrConst[useDef.first.front()] * vrConst[useDef.first.back()];
break;
case lshift:
vrConst[useDef.second] = vrConst[useDef.first.front()] << vrConst[useDef.first.back()];
break;
case rshift:
vrConst[useDef.second] = vrConst[useDef.first.front()] >> vrConst[useDef.first.back()];
break;
default:;
}
if(fakePropagation == false)
nodes[ins.line_num].memLoc = vrConst[useDef.second];
return true;
} else if(fakePropagation == false){
vrUnknown.push_back(useDef.second);
// vrConst[useDef.second] = INT_MAX;
}
return false;
}
void insertNode(const InstructionIR &ins) {
pair<list<int>, int> useDef = opUseDef(ins, VR);
int id = ins.line_num;
if (useDef.second != INT_MAX) {
regToNode[useDef.second] = id;
vrLatency[useDef.second] = latencyTable[ins.opcode];
}
nodes[id].id = id;
nodes[id].ins = ins;
nodes[id].latency = latencyTable[ins.opcode];
for (const auto &x: useDef.first) {
insertEdge(Edge(regToNode[x], id, vrLatency[x]));
}
if (ins.opcode == load || ins.opcode == store || ins.opcode == output)
addEdgesIO(ins, useDef);
else
constantPropagation(ins, useDef);
}
void relax(const Edge edge, PrioritySet &q) {
int oldDist = nodes[edge.from].priority; // distTo[edge.from];
int newDist = nodes[edge.to].priority + edge.weight; // distTo[edge.to]
if (oldDist < newDist) {
nodes[edge.from].priority = newDist;
q.decKey(make_pair(oldDist, edge.from), newDist);
}
}
void computePriority() {
PrioritySet qmin;
pair<int, int> nodeMin; // dist, line_num
for (auto rit = nodes.rbegin(); rit != nodes.rend(); rit++) {
//all nodes from back to start
if (rit->priority != 0)
continue;
qmin.insertKey(0, rit->id);
while (!qmin.empty()) {
nodeMin = qmin.getMin();
for (const auto &edgeI: nodes[nodeMin.second].edgeIn) {
relax(edgeI.second, qmin);
}
}
}
return;
}
pair<int, int> findReadyIns(set <pair<int, int>> &qReady) {
int funcUnit[2] = {INT_MAX, INT_MAX};
int outputIns = INT_MAX;
int insId, opcode;
for (auto it = qReady.begin(); it != qReady.end() && (funcUnit[0] == INT_MAX || funcUnit[1] == INT_MAX); it++) {
insId = it->second;
opcode = nodes[insId].ins.opcode;
if(funcUnit[0] == INT_MAX){
if(opcode == load || opcode == store){
funcUnit[0] = insId;
}
}else if(opcode == load && nodes[funcUnit[0]].ins.opcode == store){
// Give Priority to Load over Store,
funcUnit[0] = insId;
}
if(funcUnit[1] == INT_MAX && opcode == mult){
funcUnit[1] = insId;
}
}
if(funcUnit[0] == INT_MAX){
for (auto it = qReady.begin(); it != qReady.end(); it++) {
insId = it->second;
opcode = nodes[insId].ins.opcode;
if(opcode != load && opcode != store && opcode != mult){
if(opcode != output){
funcUnit[0] = insId;
}else{
outputIns = insId;
}
break;
}
}
// Make output the least inportant (doesn't open opportunities
if(funcUnit[0] == INT_MAX){
funcUnit[0] = outputIns;
}
}
outputIns = INT_MAX;
if(funcUnit[1] == INT_MAX){
for (auto it = qReady.begin(); it != qReady.end(); it++) {
insId = it->second;
opcode = nodes[insId].ins.opcode;
if(insId == funcUnit[0] || opcode == load || opcode == store || opcode == mult){
continue;
}else if(opcode == output) {
outputIns = insId;
}else{
funcUnit[1] = insId;
break;
}
}
// Make output the least inportant (doesn't open opportunities
if(funcUnit[1] == INT_MAX){
funcUnit[1] = outputIns;
}
}
return make_pair(funcUnit[0], funcUnit[1]);
}
void updateQReady(PriorityMultiSet &qActive, PrioritySet &qReady, int cycle, vector<int> &depCnt) {
list<int> finishedIns;
auto it = qActive.q.begin();
for (; it != qActive.q.end() && it->first <= cycle + 1; it++) {
if(it->first == cycle+1)
finishedIns.push_back(it->second);
}
qActive.q.erase(qActive.q.begin(), it);
for (const auto &ins: finishedIns) {
// cout<<"NodeOut: ";
// cout<< eOut.show()<<" | ";
if (--depCnt[ins] == 0) {
qReady.insertKey(-nodes[ins].priority, ins);
}
// cout<<endl;
}
}
// SCHEDULE************************************************
list <pair<InstructionIR, InstructionIR>> schedule() {
list <pair<InstructionIR, InstructionIR>> listTwoIns;
pair <InstructionIR, InstructionIR> currentIns;
pair<int, int> twoInsIds;
vector<int> dependencyCounter(nodes.size());
int cycle = 0;
PrioritySet qReady; // < priority, ins_id >
PriorityMultiSet qActive; // < finish_time, ins_id >
for (const auto &node: nodes) {
dependencyCounter[node.id] = node.edgeIn.size();
if (node.edgeIn.empty()) {
qReady.insertKey(-node.priority, node.id);
}
}
while (!qReady.empty() || !qActive.empty()) {
// cout<<endl<< cycle <<"-------------------------------------"<<endl;
// cout<<"qReady: ";
// qReady.show();
// cout<<"qActive: ";
// qActive.show();
twoInsIds = findReadyIns(qReady.q);