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
use flowgraph::{BasicBlock, ControlFlowGraph};
use ir::entities::AnyEntity;
use ir::{ExpandedProgramPoint, Function, Inst, ProgramOrder, ProgramPoint, Value};
use isa::TargetIsa;
use regalloc::liveness::Liveness;
use regalloc::liverange::LiveRange;
use std::cmp::Ordering;
use timing;
use verifier::{VerifierErrors, VerifierStepResult};
pub fn verify_liveness(
isa: &TargetIsa,
func: &Function,
cfg: &ControlFlowGraph,
liveness: &Liveness,
errors: &mut VerifierErrors,
) -> VerifierStepResult<()> {
let _tt = timing::verify_liveness();
let verifier = LivenessVerifier {
isa,
func,
cfg,
liveness,
};
verifier.check_ebbs(errors)?;
verifier.check_insts(errors)?;
Ok(())
}
struct LivenessVerifier<'a> {
isa: &'a TargetIsa,
func: &'a Function,
cfg: &'a ControlFlowGraph,
liveness: &'a Liveness,
}
impl<'a> LivenessVerifier<'a> {
fn check_ebbs(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
for ebb in self.func.layout.ebbs() {
for &val in self.func.dfg.ebb_params(ebb) {
let lr = match self.liveness.get(val) {
Some(lr) => lr,
None => return fatal!(errors, ebb, "EBB arg {} has no live range", val),
};
self.check_lr(ebb.into(), val, lr, errors)?;
}
}
Ok(())
}
fn check_insts(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
for ebb in self.func.layout.ebbs() {
for inst in self.func.layout.ebb_insts(ebb) {
let encoding = self.func.encodings[inst];
for &val in self.func.dfg.inst_results(inst) {
let lr = match self.liveness.get(val) {
Some(lr) => lr,
None => return fatal!(errors, inst, "{} has no live range", val),
};
self.check_lr(inst.into(), val, lr, errors)?;
if encoding.is_legal() {
if lr.affinity.is_unassigned() {
return fatal!(
errors,
inst,
"{} is a ghost value defined by a real [{}] instruction",
val,
self.isa.encoding_info().display(encoding)
);
}
} else if !lr.affinity.is_unassigned() {
return fatal!(
errors,
inst,
"{} is a real {} value defined by a ghost instruction",
val,
lr.affinity.display(&self.isa.register_info())
);
}
}
for &val in self.func.dfg.inst_args(inst) {
let lr = match self.liveness.get(val) {
Some(lr) => lr,
None => return fatal!(errors, inst, "{} has no live range", val),
};
if !self.live_at_use(lr, inst) {
return fatal!(errors, inst, "{} is not live at this use", val);
}
if encoding.is_legal() && lr.affinity.is_unassigned() {
return fatal!(
errors,
inst,
"{} is a ghost value used by a real [{}] instruction",
val,
self.isa.encoding_info().display(encoding)
);
}
}
}
}
Ok(())
}
fn live_at_use(&self, lr: &LiveRange, inst: Inst) -> bool {
let ctx = self.liveness.context(&self.func.layout);
if ctx.order.cmp(lr.def(), inst) == Ordering::Less
&& ctx.order.cmp(inst, lr.def_local_end()) != Ordering::Greater
{
return true;
}
match lr.livein_local_end(ctx.order.inst_ebb(inst).unwrap(), ctx) {
Some(end) => ctx.order.cmp(inst, end) != Ordering::Greater,
None => false,
}
}
fn check_lr(
&self,
def: ProgramPoint,
val: Value,
lr: &LiveRange,
errors: &mut VerifierErrors,
) -> VerifierStepResult<()> {
let l = &self.func.layout;
let loc: AnyEntity = match def.into() {
ExpandedProgramPoint::Ebb(e) => e.into(),
ExpandedProgramPoint::Inst(i) => i.into(),
};
if lr.def() != def {
return fatal!(
errors,
loc,
"Wrong live range def ({}) for {}",
lr.def(),
val
);
}
if lr.is_dead() {
if !lr.is_local() {
return fatal!(errors, loc, "Dead live range {} should be local", val);
} else {
return Ok(());
}
}
let def_ebb = match def.into() {
ExpandedProgramPoint::Ebb(e) => e,
ExpandedProgramPoint::Inst(i) => l.inst_ebb(i).unwrap(),
};
match lr.def_local_end().into() {
ExpandedProgramPoint::Ebb(e) => {
return fatal!(
errors,
loc,
"Def local range for {} can't end at {}",
val,
e
)
}
ExpandedProgramPoint::Inst(i) => {
if self.func.layout.inst_ebb(i) != Some(def_ebb) {
return fatal!(errors, loc, "Def local end for {} in wrong ebb", val);
}
}
}
for (mut ebb, end) in lr.liveins(self.liveness.context(l)) {
if !l.is_ebb_inserted(ebb) {
return fatal!(
errors,
loc,
"{} livein at {} which is not in the layout",
val,
ebb
);
}
let end_ebb = match l.inst_ebb(end) {
Some(e) => e,
None => {
return fatal!(
errors,
loc,
"{} livein for {} ends at {} which is not in the layout",
val,
ebb,
end
)
}
};
loop {
for BasicBlock { inst: pred, .. } in self.cfg.pred_iter(ebb) {
if !self.live_at_use(lr, pred) {
return fatal!(
errors,
pred,
"{} is live in to {} but not live at predecessor",
val,
ebb
);
}
}
if ebb == end_ebb {
break;
}
ebb = match l.next_ebb(ebb) {
Some(e) => e,
None => {
return fatal!(
errors,
loc,
"end of {} livein ({}) never reached",
val,
end_ebb
)
}
};
}
}
Ok(())
}
}