-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSymInstr.cpp
1711 lines (1484 loc) · 50.1 KB
/
SymInstr.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
#include <stdio.h>
#include <stdlib.h>
#include "SymInstr.h"
#include "Struct.h"
using namespace std;
SymInstrOperand::SymInstrOperand(): dataFlowBit((size_t)-1)
{
}
void SymInstrOperand::Print(SymInstrFunction* f)
{
switch (type)
{
case SYMOPERAND_REG:
f->PrintRegister(reg);
break;
case SYMOPERAND_IMMED:
fprintf(stderr, "%lld", (long long)immed);
break;
case SYMOPERAND_STACK_VAR:
if (reg == SYMVAR_FRAME_SIZE)
fprintf(stderr, "framesize");
else if (reg == SYMVAR_NEG_FRAME_SIZE)
fprintf(stderr, "negframesize");
else
fprintf(stderr, "stack%d", reg);
if (immed != 0)
fprintf(stderr, "+%lld", (long long)immed);
break;
case SYMOPERAND_GLOBAL_VAR:
fprintf(stderr, "global%lld", (long long)immed);
break;
case SYMOPERAND_BLOCK:
fprintf(stderr, "%s:%d", func->GetName().c_str(), (int)block->GetIndex());
break;
}
if ((access != SYMOPERAND_READ) && (access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(reg)) && (dataFlowBit != (size_t)-1))
fprintf(stderr, "{%d}", (int)dataFlowBit);
if (useDefChain.size() != 0)
{
fprintf(stderr, "{defs ");
for (vector<size_t>::iterator i = useDefChain.begin(); i != useDefChain.end(); i++)
{
fprintf(stderr, "%d", (int)*i);
if ((i + 1) != useDefChain.end())
fprintf(stderr, ",");
}
fprintf(stderr, "}");
}
}
SymInstr::SymInstr(): m_flags(0)
{
}
SymInstr::~SymInstr()
{
}
void SymInstr::AddRegisterOperand(uint32_t reg, SymInstrOperandAccess access)
{
SymInstrOperand operand;
operand.type = SYMOPERAND_REG;
operand.access = access;
operand.reg = reg;
m_operands.push_back(operand);
if ((operand.access != SYMOPERAND_READ) && (operand.access != SYMOPERAND_IGNORED) && (reg == SYMREG_IP))
EnableFlag(SYMFLAG_CONTROL_FLOW);
}
void SymInstr::AddImmediateOperand(int64_t immed)
{
SymInstrOperand operand;
operand.type = SYMOPERAND_IMMED;
operand.access = SYMOPERAND_READ;
operand.immed = immed;
m_operands.push_back(operand);
}
void SymInstr::AddStackVarOperand(uint32_t var, int64_t offset)
{
SymInstrOperand operand;
operand.type = SYMOPERAND_STACK_VAR;
operand.access = SYMOPERAND_READ;
operand.reg = var;
operand.immed = offset;
m_operands.push_back(operand);
}
void SymInstr::AddGlobalVarOperand(int64_t dataOffset)
{
SymInstrOperand operand;
operand.type = SYMOPERAND_GLOBAL_VAR;
operand.access = SYMOPERAND_READ;
operand.immed = dataOffset;
m_operands.push_back(operand);
}
void SymInstr::AddBlockOperand(Function* func, ILBlock* block)
{
SymInstrOperand operand;
operand.type = SYMOPERAND_BLOCK;
operand.access = SYMOPERAND_READ;
operand.func = func;
operand.block = block;
m_operands.push_back(operand);
}
bool SymInstr::UpdateInstruction(SymInstrFunction* func, const Settings& settings, vector<SymInstr*>& replacement)
{
return false;
}
SymInstrBlock::SymInstrBlock(size_t i): m_index(i)
{
}
SymInstrBlock::~SymInstrBlock()
{
for (vector<SymInstr*>::iterator i = m_instrs.begin(); i != m_instrs.end(); i++)
delete *i;
}
void SymInstrBlock::ReplaceInstruction(size_t i, const std::vector<SymInstr*>& replacement)
{
delete m_instrs[i];
m_instrs.erase(m_instrs.begin() + i);
m_instrs.insert(m_instrs.begin() + i, replacement.begin(), replacement.end());
}
void SymInstrBlock::InsertInstructions(size_t i, const std::vector<SymInstr*>& code)
{
m_instrs.insert(m_instrs.begin() + i, code.begin(), code.end());
}
void SymInstrBlock::ResetDataFlowInfo(size_t defs, size_t regs)
{
m_defPreserve.Reset(defs, true);
m_defGenerate.Reset(defs, false);
m_defReachIn.Reset(defs, false);
m_defReachOut.Reset(defs, false);
m_liveDef.Reset(regs, false);
m_liveUse.Reset(regs, false);
m_liveIn.Reset(regs, false);
m_liveOut.Reset(regs, false);
for (vector<SymInstr*>::iterator i = m_instrs.begin(); i != m_instrs.end(); i++)
for (vector<SymInstrOperand>::iterator j = (*i)->GetOperands().begin(); j != (*i)->GetOperands().end(); j++)
j->useDefChain.clear();
}
bool SymInstrBlock::IsRegisterLiveAtDefinition(uint32_t reg, size_t instr)
{
// The register is always live for the instruction it is defined at
for (vector<SymInstrOperand>::iterator i = m_instrs[instr]->GetOperands().begin(); i != m_instrs[instr]->GetOperands().end(); i++)
{
if ((i->type == SYMOPERAND_REG) && (i->access != SYMOPERAND_READ) && (i->access != SYMOPERAND_IGNORED) && (i->reg == reg))
return true;
}
// Determine if there is a definition of this register before the requested instruction
bool hasDefBeforeInstr = false;
if (m_liveIn.GetBit(reg))
hasDefBeforeInstr = true;
else
{
for (size_t i = 0; i < instr; i++)
{
for (vector<SymInstrOperand>::iterator j = m_instrs[i]->GetOperands().begin(); j != m_instrs[i]->GetOperands().end(); j++)
{
if ((j->type == SYMOPERAND_REG) && (j->access != SYMOPERAND_READ) &&
(j->access != SYMOPERAND_TEMPORARY) && (j->access != SYMOPERAND_IGNORED) && (j->reg == reg))
hasDefBeforeInstr = true;
}
}
}
if (!hasDefBeforeInstr)
{
// Register is not defined, so it cannot be live
return false;
}
// Determine if the definition is still live after the instruction (that is, it is used before being defined again)
for (size_t i = instr + 1; i < m_instrs.size(); i++)
{
bool written = false;
for (vector<SymInstrOperand>::iterator j = m_instrs[i]->GetOperands().begin(); j != m_instrs[i]->GetOperands().end(); j++)
{
if ((j->type == SYMOPERAND_REG) && (j->access != SYMOPERAND_WRITE) &&
(j->access != SYMOPERAND_TEMPORARY) && (j->access != SYMOPERAND_IGNORED) && (j->reg == reg))
{
// Register is being used before another definition, it is live
return true;
}
if ((j->type == SYMOPERAND_REG) && (j->access != SYMOPERAND_READ) &&
(j->access != SYMOPERAND_TEMPORARY) && (j->access != SYMOPERAND_IGNORED) && (j->reg == reg))
written = true;
}
if (written)
{
// Register is being defined before the original definition was used, register is not live at
// the instruction requested
return false;
}
}
// Reached end of block, use block level liveness data from the data flow analysis
return m_liveOut.GetBit(reg);
}
bool SymInstrBlock::EmitCode(SymInstrFunction* func, OutputBlock* out)
{
for (vector<SymInstr*>::iterator i = m_instrs.begin(); i != m_instrs.end(); i++)
{
if (!(*i)->EmitInstruction(func, out))
return false;
}
return true;
}
void SymInstrBlock::Print(SymInstrFunction* func)
{
fprintf(stderr, "%d: entry ", (int)m_index);
for (set<SymInstrBlock*>::iterator i = m_entryBlocks.begin(); i != m_entryBlocks.end(); i++)
fprintf(stderr, "%d ", (int)(*i)->m_index);
fprintf(stderr, ", exit ");
for (set<SymInstrBlock*>::iterator i = m_exitBlocks.begin(); i != m_exitBlocks.end(); i++)
fprintf(stderr, "%d ", (int)(*i)->m_index);
size_t count = 0;
for (size_t i = 0; i < m_liveOut.GetSize(); i++)
{
if (m_liveIn.GetBit(i))
count++;
}
if (count > 0)
{
fprintf(stderr, ", live start ");
for (size_t i = 0; i < m_liveIn.GetSize(); i++)
{
if (m_liveIn.GetBit(i))
{
func->PrintRegister((uint32_t)i);
fprintf(stderr, " ");
}
}
}
count = 0;
for (size_t i = 0; i < m_liveOut.GetSize(); i++)
{
if (m_liveOut.GetBit(i))
count++;
}
if (count > 0)
{
fprintf(stderr, ", live end ");
for (size_t i = 0; i < m_liveOut.GetSize(); i++)
{
if (m_liveOut.GetBit(i))
{
func->PrintRegister((uint32_t)i);
fprintf(stderr, " ");
}
}
}
fprintf(stderr, "\n");
for (size_t i = 0; i < m_instrs.size(); i++)
{
fprintf(stderr, "\t[%d] ", (int)i);
m_instrs[i]->Print(func);
fprintf(stderr, "\n");
}
}
SymInstrFunction::SymInstrFunction(const Settings& settings, Function* func): m_settings(settings), m_function(func)
{
}
SymInstrFunction::~SymInstrFunction()
{
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
delete *i;
}
void SymInstrFunction::PerformDataFlowAnalysis()
{
// Find all definitions of all registers and organize them by register
map< uint32_t, vector<size_t> > regDefs;
vector< pair<SymInstrBlock*, size_t> > defs;
size_t bitCount = 0;
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
for (size_t j = 0; j < (*i)->GetInstructions().size(); j++)
{
for (vector<SymInstrOperand>::iterator k = (*i)->GetInstructions()[j]->GetOperands().begin();
k != (*i)->GetInstructions()[j]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access != SYMOPERAND_READ) &&
(k->access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(k->reg)))
{
regDefs[k->reg].push_back(bitCount);
defs.push_back(pair<SymInstrBlock*, size_t>(*i, j));
k->dataFlowBit = bitCount++;
}
}
}
}
for (vector<SymInstrBlock*>::const_iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
(*i)->ResetDataFlowInfo(bitCount, m_symRegClass.size());
m_exitReachingDefs.Reset(bitCount, false);
m_defUseChains.clear();
m_defUseChains.resize(bitCount);
// Populate definition preservation and generation information
map< uint32_t, size_t > regDef;
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
for (size_t j = 0; j < (*i)->GetInstructions().size(); j++)
{
for (vector<SymInstrOperand>::iterator k = (*i)->GetInstructions()[j]->GetOperands().begin();
k != (*i)->GetInstructions()[j]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access != SYMOPERAND_READ) &&
(k->access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(k->reg)))
{
// Kill other definitions of this register
if (regDef.find(k->reg) != regDef.end())
{
// Variable already defined in this block, kill the old definition
(*i)->GetGeneratedDefinitions().SetBit(regDef[k->reg], false);
}
for (vector<size_t>::iterator x = regDefs[k->reg].begin(); x != regDefs[k->reg].end(); x++)
(*i)->GetPreservedDefinitions().SetBit(*x, false);
regDef[k->reg] = k->dataFlowBit;
(*i)->GetGeneratedDefinitions().SetBit(k->dataFlowBit, true);
}
}
}
}
// Iteratively compute reaching definitions at the block level
BitVector tempVector;
tempVector.Reset(bitCount, false);
bool changed = true;
while (changed)
{
changed = false;
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
// Add definitions from output of all predecessor blocks to the input of the current block
tempVector = (*i)->GetReachingDefinitionsInput();
for (set<SymInstrBlock*>::const_iterator j = (*i)->GetEntryBlocks().begin();
j != (*i)->GetEntryBlocks().end(); j++)
tempVector.Union((*j)->GetReachingDefinitionsOutput());
if (tempVector != (*i)->GetReachingDefinitionsInput())
{
changed = true;
(*i)->GetReachingDefinitionsInput() = tempVector;
}
// Compute reaching definitions output for this block (generate | (input & preserved))
tempVector = (*i)->GetReachingDefinitionsInput();
tempVector.Intersection((*i)->GetPreservedDefinitions());
tempVector.Union((*i)->GetGeneratedDefinitions());
if (tempVector != (*i)->GetReachingDefinitionsOutput())
{
changed = true;
(*i)->GetReachingDefinitionsOutput() = tempVector;
}
}
// Update reaching definitions information for exit block
tempVector = m_exitReachingDefs;
for (set<SymInstrBlock*>::iterator i = m_exitBlocks.begin(); i != m_exitBlocks.end(); i++)
tempVector.Union((*i)->GetReachingDefinitionsOutput());
if (tempVector != m_exitReachingDefs)
{
changed = true;
m_exitReachingDefs = tempVector;
}
}
// Use reaching definitions information to generate definition->use and use->definition chains
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
// Copy reaching definitions into temporary vector, as it will be updated per-instruction to generate
// localized information
tempVector = (*i)->GetReachingDefinitionsInput();
// For each instruction, iterate over all register uses and update information
for (size_t j = 0; j < (*i)->GetInstructions().size(); j++)
{
for (vector<SymInstrOperand>::iterator k = (*i)->GetInstructions()[j]->GetOperands().begin();
k != (*i)->GetInstructions()[j]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access != SYMOPERAND_WRITE) &&
(k->access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(k->reg)))
{
// Add reachable definitions to the use->definition and definition->use chains
for (vector<size_t>::iterator n = regDefs[k->reg].begin(); n != regDefs[k->reg].end(); n++)
{
if (tempVector.GetBit(*n))
{
// Definition is reachable
k->useDefChain.push_back(*n);
m_defUseChains[*n].push_back(pair<SymInstrBlock*, size_t>(*i, j));
}
}
}
}
for (vector<SymInstrOperand>::const_iterator k = (*i)->GetInstructions()[j]->GetOperands().begin();
k != (*i)->GetInstructions()[j]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access != SYMOPERAND_READ) &&
(k->access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(k->reg)))
{
// Instruction is a definition, update reaching definitions information according to the
// register being written
for (vector<size_t>::iterator n = regDefs[k->reg].begin(); n != regDefs[k->reg].end(); n++)
tempVector.SetBit(*n, false);
// Set the bit for this definition
tempVector.SetBit(k->dataFlowBit, true);
}
}
}
}
// Save definition information
m_regDefs = regDefs;
m_defLocs = defs;
// Populate liveness analysis information
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
for (size_t j = 0; j < (*i)->GetInstructions().size(); j++)
{
// Check uses of registers
for (vector<SymInstrOperand>::iterator k = (*i)->GetInstructions()[j]->GetOperands().begin();
k != (*i)->GetInstructions()[j]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access != SYMOPERAND_WRITE) && (k->access != SYMOPERAND_TEMPORARY) &&
(k->access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(k->reg)))
{
if (m_regDefs[k->reg].size() == 0)
{
// Ignore registers that have no definitions
continue;
}
if (!(*i)->GetLiveDefinitions().GetBit(k->reg))
{
// Used before definition
(*i)->GetLiveUses().SetBit(k->reg, true);
}
}
}
// Check definitions of registers
for (vector<SymInstrOperand>::iterator k = (*i)->GetInstructions()[j]->GetOperands().begin();
k != (*i)->GetInstructions()[j]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access != SYMOPERAND_READ) && (k->access != SYMOPERAND_TEMPORARY) &&
(k->access != SYMOPERAND_IGNORED) && (!SYMREG_IS_SPECIAL_REG(k->reg)))
{
if (m_regDefs[k->reg].size() == 0)
{
// Ignore registers that have no definitions
continue;
}
if (!(*i)->GetLiveUses().GetBit(k->reg))
{
// Defined before use
(*i)->GetLiveDefinitions().SetBit(k->reg, true);
}
}
}
}
}
// Iteratively compute liveness at the block level
tempVector.Reset(m_symRegClass.size(), false);
changed = true;
while (changed)
{
changed = false;
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
// Add liveness of all successor blocks to the output of the current block
tempVector.Reset(m_symRegClass.size(), false);
for (set<SymInstrBlock*>::const_iterator j = (*i)->GetExitBlocks().begin();
j != (*i)->GetExitBlocks().end(); j++)
tempVector.Union((*j)->GetLiveInput());
if (tempVector != (*i)->GetLiveOutput())
{
changed = true;
(*i)->GetLiveOutput() = tempVector;
}
// Compute liveness at start of this block ((out - defined) | used)
tempVector = (*i)->GetLiveOutput();
tempVector.Difference((*i)->GetLiveDefinitions());
tempVector.Union((*i)->GetLiveUses());
if (tempVector != (*i)->GetLiveInput())
{
changed = true;
(*i)->GetLiveInput() = tempVector;
}
}
}
}
void SymInstrFunction::SplitRegisters()
{
// For each register, check definitions to see if they are fully separated in usage
for (map< uint32_t, vector<size_t> >::iterator i = m_regDefs.begin(); i != m_regDefs.end(); i++)
{
if (i->second.size() <= 1)
{
// If there isn't more than one definition, there is nothing to split
continue;
}
// Initialize definitions to be separate
vector<size_t> defSet;
size_t sets = i->second.size();
for (size_t j = 0; j < i->second.size(); j++)
defSet.push_back(j);
// Combine definition->use chains that share uses in common. A read/write register access
// must force a merge of two definition->use chains. Any that are not combined after this
// process are fully separate and can be assigned a new symbolic register.
for (size_t j = 0; j < i->second.size(); j++)
{
// Check all other definitions
for (size_t k = j + 1; k < i->second.size(); k++)
{
// Don't need to check definitions that are already in common
if (defSet[j] == defSet[k])
continue;
// Check for uses in common
// TODO: Use something better than O(n^2) here?
bool common = false;
for (vector< pair<SymInstrBlock*, size_t> >::iterator a = m_defUseChains[i->second[j]].begin();
a != m_defUseChains[i->second[j]].end(); a++)
{
for (vector< pair<SymInstrBlock*, size_t> >::iterator b = m_defUseChains[i->second[k]].begin();
b != m_defUseChains[i->second[k]].end(); b++)
{
if ((a->first == b->first) && (a->second == b->second))
{
common = true;
break;
}
}
}
// Check for read/write registers
for (vector< pair<SymInstrBlock*, size_t> >::iterator a = m_defUseChains[i->second[j]].begin();
a != m_defUseChains[i->second[j]].end(); a++)
{
for (vector<SymInstrOperand>::iterator operand = a->first->GetInstructions()[a->second]->GetOperands().begin();
operand != a->first->GetInstructions()[a->second]->GetOperands().end(); operand++)
{
if ((operand->type == SYMOPERAND_REG) && (operand->dataFlowBit == i->second[k]) &&
(operand->access = SYMOPERAND_READ_WRITE))
common = true;
}
}
for (vector< pair<SymInstrBlock*, size_t> >::iterator a = m_defUseChains[i->second[k]].begin();
a != m_defUseChains[i->second[k]].end(); a++)
{
for (vector<SymInstrOperand>::iterator operand = a->first->GetInstructions()[a->second]->GetOperands().begin();
operand != a->first->GetInstructions()[a->second]->GetOperands().end(); operand++)
{
if ((operand->type == SYMOPERAND_REG) && (operand->dataFlowBit == i->second[j]) &&
(operand->access = SYMOPERAND_READ_WRITE))
common = true;
}
}
if (common)
{
// These two definitions have uses in common, merge them into the same set and delete the merged set
size_t fromSet = defSet[k];
size_t toSet = defSet[j];
sets--;
for (size_t a = 0; a < i->second.size(); a++)
{
if (defSet[a] == fromSet)
defSet[a] = toSet;
else if (defSet[a] > fromSet)
defSet[a]--;
}
}
}
}
// Create a new register for each additional set of definitions and rewrite its uses
for (size_t j = 1; j < sets; j++)
{
uint32_t fromReg = i->first;
uint32_t toReg = AddRegister(m_symRegClass[fromReg], m_symRegType[fromReg], m_symRegOffset[fromReg], m_symRegVar[fromReg]);
for (size_t k = 0; k < i->second.size(); k++)
{
if (defSet[k] != j)
continue;
if (m_settings.internalDebug)
fprintf(stderr, "Splitting reg%d from def %d into reg%d\n", fromReg, (int)i->second[k], toReg);
if (m_alreadySpilled.count(fromReg) != 0)
m_alreadySpilled.insert(toReg);
SymInstrBlock* defBlock = m_defLocs[i->second[k]].first;
size_t defInstr = m_defLocs[i->second[k]].second;
for (vector<SymInstrOperand>::iterator operand = defBlock->GetInstructions()[defInstr]->GetOperands().begin();
operand != defBlock->GetInstructions()[defInstr]->GetOperands().end(); operand++)
{
if ((operand->type == SYMOPERAND_REG) && (operand->reg == fromReg))
operand->reg = toReg;
}
for (vector< pair<SymInstrBlock*, size_t> >::iterator a = m_defUseChains[i->second[k]].begin();
a != m_defUseChains[i->second[k]].end(); a++)
{
for (vector<SymInstrOperand>::iterator operand = a->first->GetInstructions()[a->second]->GetOperands().begin();
operand != a->first->GetInstructions()[a->second]->GetOperands().end(); operand++)
{
if ((operand->type == SYMOPERAND_REG) && (operand->reg == fromReg))
operand->reg = toReg;
}
}
}
}
}
}
bool SymInstrFunction::SpillRegister(uint32_t reg)
{
vector< pair<SymInstrBlock*, size_t> > uses;
// Generate code to save register contents after each definition
for (vector<size_t>::iterator i = m_regDefs[reg].begin(); i != m_regDefs[reg].end(); i++)
{
SymInstrBlock* block = m_defLocs[*i].first;
size_t instr = m_defLocs[*i].second;
vector<SymInstr*> code;
// Let the architecture subclass generate the appropriate instructions for the store
if (!GenerateSpillStore(reg, m_symRegVar[reg], m_symRegOffset[reg], m_stackVarTypes[m_symRegVar[reg]], code))
return false;
// Place the instructions into the instruction stream
block->InsertInstructions(instr + 1, code);
// Update definition and use information to account for inserted code
for (vector< pair<SymInstrBlock*, size_t> >::iterator j = m_defLocs.begin(); j != m_defLocs.end(); j++)
{
if (j->first != block)
continue;
if (j->second <= instr)
continue;
j->second += code.size();
}
for (vector< pair<SymInstrBlock*, size_t> >::iterator j = uses.begin(); j != uses.end(); j++)
{
if (j->first != block)
continue;
if (j->second <= instr)
continue;
j->second += code.size();
}
for (vector< vector< pair<SymInstrBlock*, size_t> > >::iterator j = m_defUseChains.begin(); j != m_defUseChains.end(); j++)
{
for (vector< pair<SymInstrBlock*, size_t> >::iterator k = j->begin(); k != j->end(); k++)
{
if (k->first != block)
continue;
if (k->second <= instr)
continue;
k->second += code.size();
}
}
// Add uses of this definition to the global uses list
for (vector< pair<SymInstrBlock*, size_t> >::iterator j = m_defUseChains[*i].begin(); j != m_defUseChains[*i].end(); j++)
{
bool found = false;
for (vector< pair<SymInstrBlock*, size_t> >::iterator k = uses.begin(); k != uses.end(); k++)
{
if ((j->first == k->first) && (j->second == k->second))
{
found = true;
break;
}
}
if (!found)
uses.push_back(*j);
}
}
// Generate code to restore register contents before each use
for (vector< pair<SymInstrBlock*, size_t> >::iterator i = uses.begin(); i != uses.end(); i++)
{
SymInstrBlock* block = i->first;
size_t instr = i->second;
vector<SymInstr*> code;
// Let the architecture subclass generate the appropriate instructions for the load
if (!GenerateSpillLoad(reg, m_symRegVar[reg], m_symRegOffset[reg], m_stackVarTypes[m_symRegVar[reg]], code))
return false;
// Place the instructions into the instruction stream
block->InsertInstructions(instr, code);
// Update definition and use information to account for inserted code
for (vector< pair<SymInstrBlock*, size_t> >::iterator j = m_defLocs.begin(); j != m_defLocs.end(); j++)
{
if (j->first != block)
continue;
if (j->second < instr)
continue;
j->second += code.size();
}
for (vector< pair<SymInstrBlock*, size_t> >::iterator j = uses.begin(); j != uses.end(); j++)
{
if (j->first != block)
continue;
if (j->second < instr)
continue;
j->second += code.size();
}
for (vector< vector< pair<SymInstrBlock*, size_t> > >::iterator j = m_defUseChains.begin(); j != m_defUseChains.end(); j++)
{
for (vector< pair<SymInstrBlock*, size_t> >::iterator k = j->begin(); k != j->end(); k++)
{
if (k->first != block)
continue;
if (k->second < instr)
continue;
k->second += code.size();
}
}
}
return true;
}
SymInstrBlock* SymInstrFunction::AddBlock(ILBlock* il)
{
SymInstrBlock* block = new SymInstrBlock(il->GetIndex());
m_blocks.push_back(block);
m_blockMap[il] = block;
return block;
}
void SymInstrFunction::AddExitBlock(SymInstrBlock* block, SymInstrBlock* exitBlock)
{
block->AddExitBlock(exitBlock);
exitBlock->AddEntryBlock(block);
}
void SymInstrFunction::InitializeBlocks(Function* func)
{
// First create all blocks
for (vector<ILBlock*>::const_iterator i = func->GetIL().begin(); i != func->GetIL().end(); i++)
AddBlock(*i);
// Now update exit block information
for (vector<ILBlock*>::const_iterator i = func->GetIL().begin(); i != func->GetIL().end(); i++)
for (set<ILBlock*>::const_iterator j = (*i)->GetExitBlocks().begin(); j != (*i)->GetExitBlocks().end(); j++)
AddExitBlock(GetBlock(*i), GetBlock(*j));
for (set<ILBlock*>::const_iterator i = func->GetExitBlocks().begin(); i != func->GetExitBlocks().end(); i++)
m_exitBlocks.insert(GetBlock(*i));
}
SymInstrBlock* SymInstrFunction::GetBlock(ILBlock* il) const
{
map<ILBlock*, SymInstrBlock*>::const_iterator i = m_blockMap.find(il);
if (i == m_blockMap.end())
return NULL;
return i->second;
}
int64_t SymInstrFunction::GetStackVarOffset(uint32_t var) const
{
if (var == SYMVAR_FRAME_SIZE)
return m_stackFrameSize;
if (var == SYMVAR_NEG_FRAME_SIZE)
return -m_stackFrameSize;
return m_stackVarOffsets[var];
}
uint32_t SymInstrFunction::AddRegister(uint32_t cls, ILParameterType type, int64_t offset, uint32_t var)
{
uint32_t i = (uint32_t)m_symRegClass.size();
m_symRegClass.push_back(cls);
m_symRegType.push_back(type);
m_symRegVar.push_back(var);
m_symRegOffset.push_back(offset);
m_symRegAssignment.push_back(SYMREG_NONE); // Will be filled in by register allocator
return i;
}
void SymInstrFunction::AssignRegister(uint32_t reg, uint32_t native)
{
m_symRegAssignment[reg] = native;
}
uint32_t SymInstrFunction::AddStackVar(int64_t offset, bool param, size_t width, ILParameterType type)
{
uint32_t i = (uint32_t)m_stackVarOffsets.size();
m_stackVarOffsets.push_back(offset);
m_stackVarIsParam.push_back(param);
m_stackVarWidths.push_back(width);
m_stackVarTypes.push_back(type);
return i;
}
bool SymInstrFunction::AllocateRegisters()
{
m_alreadySpilled.clear();
// Delete instructions that aren't used
bool changed = true;
while (changed)
{
changed = false;
PerformDataFlowAnalysis();
for (vector<SymInstrBlock*>::iterator i = m_blocks.begin(); i != m_blocks.end(); i++)
{
for (size_t j = 0; j < (*i)->GetInstructions().size(); j++)
{
if ((*i)->GetInstructions()[j]->IsFlagSet(SYMFLAG_CONTROL_FLOW))
continue;
if ((*i)->GetInstructions()[j]->IsFlagSet(SYMFLAG_MEMORY_BARRIER))
continue;
if ((*i)->GetInstructions()[j]->IsFlagSet(SYMFLAG_CALL))
continue;
if ((*i)->GetInstructions()[j]->IsFlagSet(SYMFLAG_STACK))
continue;
// If all variables defined by the instruction have no uses, the instruction can be deleted
bool canDelete = false;
for (size_t k = 0; k < (*i)->GetInstructions()[j]->GetOperands().size(); k++)
{
if (((*i)->GetInstructions()[j]->GetOperands()[k].access == SYMOPERAND_READ) ||
((*i)->GetInstructions()[j]->GetOperands()[k].access == SYMOPERAND_TEMPORARY) ||
((*i)->GetInstructions()[j]->GetOperands()[k].access == SYMOPERAND_IGNORED))
continue;
if (SYMREG_IS_SPECIAL_REG((*i)->GetInstructions()[j]->GetOperands()[k].reg))
{
canDelete = false;
break;
}
if (m_defUseChains[(*i)->GetInstructions()[j]->GetOperands()[k].dataFlowBit].size() != 0)
{
canDelete = false;
break;
}
canDelete = true;
}
if (!canDelete)
continue;
// Instruction can be deleted
vector<SymInstr*> empty;
(*i)->ReplaceInstruction(j, empty);
j--;
changed = true;
}
}
}
while (true) // May need to spill one or more times
{
// Perform an initial data flow analysis path so that we can analyze the symbolic register usage and see
// if there are any that can be split into multiple registers to reduce register pressure when the same
// register is used for multiple non-overlapping purposes. These are called "webs" in the literature.
PerformDataFlowAnalysis();
if (m_settings.internalDebug)
Print();
SplitRegisters();
// Data flow information may have changed, recalculate it before continuing
PerformDataFlowAnalysis();
// Compute register interference graph
m_regInterference.clear();
m_regInterference.resize(m_symRegClass.size());
m_preferSameReg.clear();
m_preferSameReg.resize(m_symRegClass.size());
m_preferSameRegCount.clear();
m_preferSameRegCount.resize(m_symRegClass.size());
for (uint32_t i = 0; i < (uint32_t)m_symRegClass.size(); i++)
{
// Analyze each definition of the current register
for (vector<size_t>::iterator j = m_regDefs[i].begin(); j != m_regDefs[i].end(); j++)
{
SymInstrBlock* defBlock = m_defLocs[*j].first;
size_t defInstr = m_defLocs[*j].second;
// Check all other registers, if a register is live at this definition, the two registers interfere
for (uint32_t k = 0; k < (uint32_t)m_symRegClass.size(); k++)
{
if (i == k)
continue;
if (defBlock->IsRegisterLiveAtDefinition(k, defInstr))
{
// Register is live at this definition, add to interference graph
m_regInterference[i].insert(k);
m_regInterference[k].insert(i);
}
}
// If this is a temporary register usage, register interferes with all other registers used by this instruction
bool tempUse = false;
for (vector<SymInstrOperand>::iterator k = defBlock->GetInstructions()[defInstr]->GetOperands().begin();
k != defBlock->GetInstructions()[defInstr]->GetOperands().end(); k++)
{
if ((k->type == SYMOPERAND_REG) && (k->access == SYMOPERAND_TEMPORARY) && (k->reg == i))
{
tempUse = true;
break;
}
}
if (tempUse)
{
// Using as temporary, interfere with any other registers, even reads, since the register is being
// used as part of the instruction itself
for (vector<SymInstrOperand>::iterator k = defBlock->GetInstructions()[defInstr]->GetOperands().begin();