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
use ir;
use isa;
use regalloc::RegDiversions;
use regalloc::liveness::Liveness;
use timing;
use verifier::Result;
pub fn verify_locations(
isa: &isa::TargetIsa,
func: &ir::Function,
liveness: Option<&Liveness>,
) -> Result {
let _tt = timing::verify_locations();
let verifier = LocationVerifier {
isa,
func,
reginfo: isa.register_info(),
encinfo: isa.encoding_info(),
liveness,
};
verifier.check_constraints()?;
Ok(())
}
struct LocationVerifier<'a> {
isa: &'a isa::TargetIsa,
func: &'a ir::Function,
reginfo: isa::RegInfo,
encinfo: isa::EncInfo,
liveness: Option<&'a Liveness>,
}
impl<'a> LocationVerifier<'a> {
fn check_constraints(&self) -> Result {
let dfg = &self.func.dfg;
let mut divert = RegDiversions::new();
for ebb in self.func.layout.ebbs() {
divert.clear();
for inst in self.func.layout.ebb_insts(ebb) {
let enc = self.func.encodings[inst];
if enc.is_legal() {
self.check_enc_constraints(inst, enc, &divert)?
} else {
self.check_ghost_results(inst)?;
}
if let Some(sig) = dfg.call_signature(inst) {
self.check_call_abi(inst, sig, &divert)?;
}
let opcode = dfg[inst].opcode();
if opcode.is_return() {
self.check_return_abi(inst, &divert)?;
} else if opcode.is_branch() {
if !divert.is_empty() {
self.check_cfg_edges(inst, &divert)?;
}
}
self.update_diversions(inst, &mut divert)?;
}
}
Ok(())
}
fn check_enc_constraints(
&self,
inst: ir::Inst,
enc: isa::Encoding,
divert: &RegDiversions,
) -> Result {
let constraints = self.encinfo.operand_constraints(enc).expect(
"check_enc_constraints requires a legal encoding",
);
if constraints.satisfied(inst, divert, self.func) {
return Ok(());
}
err!(
inst,
"{} constraints not satisfied",
self.encinfo.display(enc)
)
}
fn check_ghost_results(&self, inst: ir::Inst) -> Result {
let results = self.func.dfg.inst_results(inst);
for &res in results {
let loc = self.func.locations[res];
if loc.is_assigned() {
return err!(
inst,
"ghost result {} value must not have a location ({}).",
res,
loc.display(&self.reginfo)
);
}
}
Ok(())
}
fn check_call_abi(&self, inst: ir::Inst, sig: ir::SigRef, divert: &RegDiversions) -> Result {
let sig = &self.func.dfg.signatures[sig];
let varargs = self.func.dfg.inst_variable_args(inst);
let results = self.func.dfg.inst_results(inst);
for (abi, &value) in sig.params.iter().zip(varargs) {
self.check_abi_location(
inst,
value,
abi,
divert.get(value, &self.func.locations),
ir::StackSlotKind::OutgoingArg,
)?;
}
for (abi, &value) in sig.returns.iter().zip(results) {
self.check_abi_location(
inst,
value,
abi,
self.func.locations[value],
ir::StackSlotKind::OutgoingArg,
)?;
}
Ok(())
}
fn check_return_abi(&self, inst: ir::Inst, divert: &RegDiversions) -> Result {
let sig = &self.func.signature;
let varargs = self.func.dfg.inst_variable_args(inst);
for (abi, &value) in sig.returns.iter().zip(varargs) {
self.check_abi_location(
inst,
value,
abi,
divert.get(value, &self.func.locations),
ir::StackSlotKind::IncomingArg,
)?;
}
Ok(())
}
fn check_abi_location(
&self,
inst: ir::Inst,
value: ir::Value,
abi: &ir::AbiParam,
loc: ir::ValueLoc,
want_kind: ir::StackSlotKind,
) -> Result {
match abi.location {
ir::ArgumentLoc::Unassigned => {}
ir::ArgumentLoc::Reg(reg) => {
if loc != ir::ValueLoc::Reg(reg) {
return err!(
inst,
"ABI expects {} in {}, got {}",
value,
abi.location.display(&self.reginfo),
loc.display(&self.reginfo)
);
}
}
ir::ArgumentLoc::Stack(offset) => {
if let ir::ValueLoc::Stack(ss) = loc {
let slot = &self.func.stack_slots[ss];
if slot.kind != want_kind {
return err!(
inst,
"call argument {} should be in a {} slot, but {} is {}",
value,
want_kind,
ss,
slot.kind
);
}
if slot.offset.unwrap() != offset {
return err!(
inst,
"ABI expects {} at stack offset {}, but {} is at {}",
value,
offset,
ss,
slot.offset.unwrap()
);
}
} else {
return err!(
inst,
"ABI expects {} at stack offset {}, got {}",
value,
offset,
loc.display(&self.reginfo)
);
}
}
}
Ok(())
}
fn update_diversions(&self, inst: ir::Inst, divert: &mut RegDiversions) -> Result {
let (arg, src) = match self.func.dfg[inst] {
ir::InstructionData::RegMove { arg, src, .. } |
ir::InstructionData::RegSpill { arg, src, .. } => (arg, ir::ValueLoc::Reg(src)),
ir::InstructionData::RegFill { arg, src, .. } => (arg, ir::ValueLoc::Stack(src)),
_ => return Ok(()),
};
if let Some(d) = divert.diversion(arg) {
if d.to != src {
return err!(
inst,
"inconsistent with current diversion to {}",
d.to.display(&self.reginfo)
);
}
} else if self.func.locations[arg] != src {
return err!(
inst,
"inconsistent with global location {}",
self.func.locations[arg].display(&self.reginfo)
);
}
divert.apply(&self.func.dfg[inst]);
Ok(())
}
fn check_cfg_edges(&self, inst: ir::Inst, divert: &RegDiversions) -> Result {
use ir::instructions::BranchInfo::*;
let liveness = match self.liveness {
Some(l) => l,
None => return Ok(()),
};
let dfg = &self.func.dfg;
match dfg.analyze_branch(inst) {
NotABranch => {
panic!(
"No branch information for {}",
dfg.display_inst(inst, self.isa)
)
}
SingleDest(ebb, _) => {
for d in divert.all() {
let lr = &liveness[d.value];
if lr.is_livein(ebb, liveness.context(&self.func.layout)) {
return err!(
inst,
"{} is diverted to {} and live in to {}",
d.value,
d.to.display(&self.reginfo),
ebb
);
}
}
}
Table(jt) => {
for d in divert.all() {
let lr = &liveness[d.value];
for (_, ebb) in self.func.jump_tables[jt].entries() {
if lr.is_livein(ebb, liveness.context(&self.func.layout)) {
return err!(
inst,
"{} is diverted to {} and live in to {}",
d.value,
d.to.display(&self.reginfo),
ebb
);
}
}
}
}
}
Ok(())
}
}