-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearScan.cpp
More file actions
201 lines (190 loc) · 6.42 KB
/
Copy pathLinearScan.cpp
File metadata and controls
201 lines (190 loc) · 6.42 KB
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
#include "LinearScan.h"
#include <algorithm>
#include <iostream>
#include "LiveVariableAnalysis.h"
#include "MachineCode.h"
LinearScan::LinearScan(MachineUnit* unit) {
this->unit = unit;
for (int i = 4; i < 11; i++)
regs.push_back(i);
}
void LinearScan::allocateRegisters() {
for (auto& f : unit->getFuncs()) {
func = f;
bool success;
success = false;
while (!success) // repeat until all vregs can be mapped
{
computeLiveIntervals();
success = linearScanRegisterAllocation();
if (success) // all vregs can be mapped to real regs
modifyCode();
else // spill vregs that can't be mapped to real regs
genSpillCode();
}
}
}
void LinearScan::makeDuChains() {
LiveVariableAnalysis lva;
lva.pass(func);
du_chains.clear();
int i = 0;
std::map<MachineOperand, std::set<MachineOperand*>> liveVar;
for (auto& bb : func->getBlocks()) {
liveVar.clear();
for (auto& t : bb->getLiveOut())
liveVar[*t].insert(t);
int no;
no = i = bb->getInsts().size() + i;
for (auto inst = bb->getInsts().rbegin(); inst != bb->getInsts().rend();
inst++) {
(*inst)->setNo(no--);
for (auto& def : (*inst)->getDef()) {
if (def->isVReg()) {
auto& uses = liveVar[*def];
du_chains[def].insert(uses.begin(), uses.end());
auto& kill = lva.getAllUses()[*def];
std::set<MachineOperand*> res;
set_difference(uses.begin(), uses.end(), kill.begin(),
kill.end(), inserter(res, res.end()));
liveVar[*def] = res;
}
}
for (auto& use : (*inst)->getUse()) {
if (use->isVReg())
liveVar[*use].insert(use);
}
}
}
}
void LinearScan::computeLiveIntervals() {
makeDuChains();
intervals.clear();
for (auto& du_chain : du_chains) {
int t = -1;
for (auto& use : du_chain.second)
t = std::max(t, use->getParent()->getNo());
Interval* interval = new Interval({du_chain.first->getParent()->getNo(), t, false, 0, 0, {du_chain.first}, du_chain.second});
intervals.push_back(interval);
}
bool change;
change = true;
while (change) {
change = false;
std::vector<Interval*> t(intervals.begin(), intervals.end());
for (size_t i = 0; i < t.size(); i++)
for (size_t j = i + 1; j < t.size(); j++) {
Interval* w1 = t[i];
Interval* w2 = t[j];
if (**w1->defs.begin() == **w2->defs.begin()) {
std::set<MachineOperand*> temp;
set_intersection(w1->uses.begin(), w1->uses.end(),
w2->uses.begin(), w2->uses.end(),
inserter(temp, temp.end()));
if (!temp.empty()) {
change = true;
w1->defs.insert(w2->defs.begin(), w2->defs.end());
w1->uses.insert(w2->uses.begin(), w2->uses.end());
w1->start = std::min(w1->start, w2->start);
w1->end = std::max(w1->end, w2->end);
auto it =
std::find(intervals.begin(), intervals.end(), w2);
if (it != intervals.end())
intervals.erase(it);
}
}
}
}
sort(intervals.begin(), intervals.end(), compareStart);
}
bool LinearScan::linearScanRegisterAllocation() {
bool success = true;
active.clear();
regs.clear();
for (int i = 4; i < 11; i++)
regs.push_back(i);
for (auto& i : intervals) {
expireOldIntervals(i);
if (regs.empty()) {
spillAtInterval(i);
success = false;
} else {
i->rreg = regs.front();
regs.erase(regs.begin());
active.push_back(i);
sort(active.begin(), active.end(), compareEnd);
}
}
return success;
}
void LinearScan::modifyCode() {
for (auto& interval : intervals) {
func->addSavedRegs(interval->rreg);
for (auto def : interval->defs)
def->setReg(interval->rreg);
for (auto use : interval->uses)
use->setReg(interval->rreg);
}
}
void LinearScan::genSpillCode()
{
for (auto& interval : intervals)
{
if (!interval->spill)
continue;
// TODO
/* HINT:
* The vreg should be spilled to memory.
* 1. insert ldr inst before the use of vreg
* 2. insert str inst after the def of vreg
*/
interval->disp = -func->AllocSpace(4);
auto off = new MachineOperand(MachineOperand::IMM, interval->disp);
auto fp = new MachineOperand(MachineOperand::REG, 11);
for (auto use : interval->uses) {
auto temp = new MachineOperand(*use);
auto inst = new LoadMInstruction(use->getParent()->getParent(),
temp, fp, off);
use->getParent()->insertBefore(inst);
}
for (auto def : interval->defs) {
auto temp = new MachineOperand(*def);
auto inst = new StoreMInstruction(def->getParent()->getParent(),
temp, fp, off);
def->getParent()->insertAfter(inst);
}
}
}
void LinearScan::expireOldIntervals(Interval* interval)
{
auto it = active.begin();
while (it != active.end()) {
if ((*it)->end >= interval->start)
return;
regs.push_back((*it)->rreg);
it = active.erase(find(active.begin(), active.end(), *it));
sort(regs.begin(), regs.end());
}
}
void LinearScan::spillAtInterval(Interval* interval)
{
auto spill = active.back();
if (spill->end > interval->end)
{
spill->spill = true;
interval->rreg = spill->rreg;
active.push_back(interval);
sort(active.begin(), active.end(), compareEnd);
} else
{
interval->spill = true;
}
}
bool LinearScan::compareStart(Interval* a, Interval* b)
{
return a->start < b->start;
}
bool LinearScan::compareEnd(Interval* a, Interval* b)
{
return a->end < b->end;
}