rucc_codegen/compare.rs
1//! Taking out a comparison the machine has already made.
2//!
3//! Design: `spec/optimizer/37-machine-level-optimization.md` section 37.4.
4//!
5//! A comparison produces no value. It sets a few bits nobody named and the instruction behind it
6//! reads them, so a comparison that sets the bits that are already there is one nothing could tell
7//! had run. There are two ways for that to happen, and both of them are about an instruction a
8//! little way in front rather than about a dataflow the whole function takes part in.
9//!
10//! The same comparison twice. `if (x == y) ... else if (x != y)` and every expression that asks a
11//! question and then asks its negation come out as two comparisons of the same two registers with
12//! nothing between them but the bytes each one kept. The second asks what the first asked and the
13//! answer has not moved.
14//!
15//! A comparison against zero of something arithmetic has just worked out. `if (a & MASK)` is an
16//! `and` and then a comparison of its result against zero, and the `and` set the bits that
17//! comparison would have set on its way past. This is the common one by a long way: at `-O2` over
18//! the SQLite amalgamation there are 2250 of these and 0 of the other shape.
19//!
20//! # Why it runs after the layout rather than before
21//!
22//! Because this is the second pass to work on a pair of instructions whose middle has to stay
23//! empty, and the first is the block layout. A branch on a comparison is written there as the
24//! comparison with its byte taken off and a jump that reads the condition state, and what is
25//! between those two is live and is not a register, so anything that ran afterwards and put an
26//! instruction between them would be wrong. Running last is the whole of what makes this safe,
27//! which is the sentence section 37.4 uses about the layout itself.
28//!
29//! It also makes the two shapes one shape. A comparison the layout folded a branch into is a
30//! comparison that keeps nothing, one whose byte something else wanted is a comparison that keeps
31//! a byte, and after the layout both are sitting in a block to be looked at the same way. Before
32//! the layout the first kind does not exist yet, so a pass that ran earlier would have to either
33//! leave every branch alone or undo the fusion to get at one.
34//!
35//! # How the rewrite is made
36//!
37//! Through [`crate::changes`], one comparison at a time, because one comparison is all a change
38//! here is: a comparison that is already made is already made whatever happened to the one in
39//! front of it, so there is nothing to be all of or none of.
40//!
41//! What the framework is for here is the other half of it, which is the shape. What is left of a
42//! comparison is a different instruction with a different name and one operand rather than three,
43//! and that it is an instruction this machine has is now asked rather than believed. The condition
44//! state is the half nothing can check, because it is not a register and is in no operand vector,
45//! and the argument that the bits are already the bits stays the walk's own.
46//!
47//! # What a block boundary is
48//!
49//! The end of everything this knows. The state a comparison leaves is not a register and nothing
50//! in this back end carries one from a block to its successors: the layout writes the jump that
51//! reads a comparison into the same block as the comparison, which is the only place one is read
52//! at all. So the walk starts each block knowing nothing, which is what makes it a walk rather
53//! than a dataflow.
54//!
55//! # What it will not do
56//!
57//! A comparison with anything between it and the instruction that already made it that writes the
58//! condition state. The target says which instructions those are and says it about every name it
59//! does not recognise, so an opcode added to a rule set and not to that description makes this
60//! find less rather than making it wrong.
61//!
62//! A comparison of a register something wrote in between. The bits are still the bits the earlier
63//! instruction left, but they are about what the register held then and the comparison is about
64//! what it holds now. Every definition between the two is checked against the registers the
65//! earlier one was about, which are physical by the time this runs and so are the ones the machine
66//! will really read.
67//!
68//! A comparison against zero after arithmetic whose condition reads a part of the condition state
69//! the arithmetic did not leave the way a comparison would have. `subl` says whether its answer
70//! was zero and a comparison of that answer against zero would agree, and it says whether the
71//! subtraction overflowed where the comparison would have said it did not, so a signed `<` after
72//! one reads a sign and an overflow that no longer belong together. [`rucc_target::Zeroing`] is
73//! where each instruction says which conditions it is good for, and every condition that ends up
74//! reading what the arithmetic left has to be one of them, including the ones behind the
75//! comparison rather than on it.
76//!
77//! A comparison against zero after arithmetic that wrote a different number of bits. `andl` leaves
78//! a statement about thirty two bits and `cmpq $0` asks about sixty four, and on this machine the
79//! upper half is then zero and the two disagree about the sign.
80//!
81//! A comparison against zero after arithmetic whose condition state nothing is found to read. That
82//! is a comparison that is dead rather than redundant, and taking a dead one out is a different
83//! question: it needs no earlier instruction at all, so answering it here would mean answering it
84//! only where an earlier instruction happened to be.
85
86use std::collections::HashMap;
87
88use rucc_base::Interner;
89use rucc_mir::{self as mir, Role};
90use rucc_target::{Compare, FlagInsts, MachineInsts, Reads, RegClass, Zeroing};
91
92use crate::changes::{self, Changes, Plan};
93
94/// A register, and the file it is drawn from.
95///
96/// The class as well as the number, because the two files number from zero and `xmm0` is not
97/// `rax`. The width is deliberately not here: `%al` and `%eax` are one register, so a write of
98/// either is a write of the other and a statement about what the other held is a statement about
99/// a value that has moved.
100type Place = (RegClass, mir::Reg);
101
102/// Takes out every comparison whose condition state the instruction in front of it already left.
103///
104/// Gives back how many went, which the tests read and nothing else does.
105pub fn redundant(
106 func: &mut mir::Func,
107 insts: &FlagInsts,
108 machine: &MachineInsts,
109 names: &mut Interner,
110) -> usize {
111 // Every name the rewrite could want, before the walk rather than inside it. The walk holds a
112 // name it read out of the interner while it edits the function, and interning a new one there
113 // would be the same interner borrowed twice.
114 let opcodes: HashMap<&str, mir::Opcode> = insts
115 .compares
116 .iter()
117 .filter_map(|entry| entry.kept)
118 .map(|kept| (kept, mir::Opcode::new(names.intern(&format!("{}{kept}", insts.prefix)))))
119 .collect();
120 let names = &*names;
121 let mut counts = changes::Reads::of(func);
122 let mut gone = 0;
123 for block in func.blocks().collect::<Vec<_>>() {
124 let sequence: Vec<mir::Inst> = func.insts(block).collect();
125 let mut left: Option<Left> = None;
126 for at in 0..sequence.len() {
127 let inst = sequence[at];
128 let Some(name) = opcode(func, insts, names, inst) else {
129 left = None;
130 continue;
131 };
132 left = if let Some(entry) = insts.compare(name) {
133 let already = left
134 .as_ref()
135 .is_some_and(|had| had.answers(func, insts, names, &sequence, at, entry));
136 // What the earlier instruction left comes to this one's answer, and it is worked
137 // out here rather than after the rewrite because one of the answers to what is
138 // left of an instruction is that there is nothing left of it.
139 let after = stale(func, inst, left);
140 if already && took(func, &opcodes, &mut counts, machine, names, inst, entry) {
141 gone += 1;
142 after
143 } else {
144 // Either the comparison is one nothing has made yet or it is one the target
145 // would not have what is left of, and both of those are a comparison that runs
146 // and leaves its own answer behind.
147 stale(func, inst, Some(Left::made(func, entry, inst)))
148 }
149 } else if let Some(zeroing) = insts.zeroed(name) {
150 // Before the general question of whether the name writes the condition state,
151 // because every one of these does and this is what it wrote there.
152 Left::zeroed(func, insts, name, zeroing, inst)
153 } else if (insts.writes)(name) {
154 None
155 } else {
156 stale(func, inst, left)
157 };
158 }
159 }
160 gone
161}
162
163/// What the condition state holds, and which registers it is a statement about.
164struct Left {
165 /// Which of the two ways it got there.
166 how: How,
167 /// The registers the statement is about, which anything writing one of makes it stale.
168 about: Vec<Place>,
169}
170
171/// The two ways the condition state comes to hold something this pass can use.
172enum How {
173 /// A comparison made it, and this is the question it asked.
174 Made {
175 /// The name of the comparison that keeps nothing, which is what says two are the same.
176 asks: &'static str,
177 /// What it compared, in the order it read them.
178 read: Vec<Place>,
179 /// The constant it compared against, if it compared against one.
180 imm: Option<i64>,
181 },
182 /// Arithmetic left it, and this is what a comparison against zero has to look like to be one
183 /// the arithmetic already made.
184 Zeroed {
185 /// How wide the value it wrote is.
186 width: u32,
187 /// Which conditions may read what it left.
188 covers: Zeroing,
189 },
190}
191
192impl Left {
193 /// What a comparison leaves behind.
194 fn made(func: &mir::Func, entry: &Compare, inst: mir::Inst) -> Self {
195 let read: Vec<Place> = reads(func, inst).into_iter().map(|(_, place)| place).collect();
196 Self {
197 how: How::Made {
198 asks: entry.asks,
199 read: read.clone(),
200 imm: func[inst].imm.map(|at| func[at].0),
201 },
202 about: read,
203 }
204 }
205
206 /// What arithmetic leaves behind, when what it wrote is one register of a width the
207 /// description names.
208 ///
209 /// The statement is about the register it wrote rather than about the ones it read, which is
210 /// what makes its own definition not something that makes it stale: what it wrote is the value
211 /// the comparison it stands in for is about.
212 fn zeroed(
213 func: &mir::Func,
214 insts: &FlagInsts,
215 name: &str,
216 zeroing: &Zeroing,
217 inst: mir::Inst,
218 ) -> Option<Self> {
219 let written = writes(func, inst);
220 let [(at, def)] = written[..] else { return None };
221 let width = (insts.width)(name, at)?;
222 Some(Self { how: How::Zeroed { width, covers: *zeroing }, about: vec![def] })
223 }
224
225 /// Whether this comparison is one the condition state already answers.
226 fn answers(
227 &self,
228 func: &mir::Func,
229 insts: &FlagInsts,
230 names: &Interner,
231 sequence: &[mir::Inst],
232 at: usize,
233 entry: &Compare,
234 ) -> bool {
235 let inst = sequence[at];
236 let asked: Vec<Place> = reads(func, inst).into_iter().map(|(_, place)| place).collect();
237 let against = func[inst].imm.map(|at| func[at].0);
238 match &self.how {
239 // The same question about the same values, so every bit of the answer is the bit that
240 // is already there and what reads it is not something anyone has to ask.
241 How::Made { asks, read, imm } => {
242 *asks == entry.asks && *read == asked && *imm == against
243 }
244 How::Zeroed { width, covers } => {
245 if against != Some(0) || self.about != asked {
246 return false;
247 }
248 let [(index, _)] = reads(func, inst)[..] else { return false };
249 let Some(name) = opcode(func, insts, names, inst) else { return false };
250 if (insts.width)(name, index) != Some(*width) {
251 return false;
252 }
253 let conditions = conditions(func, insts, names, sequence, at);
254 !conditions.is_empty() && conditions.iter().all(|&reads| covers.covers(reads))
255 }
256 }
257 }
258}
259
260/// The conditions that read what an instruction leaves in the condition state.
261///
262/// Its own first, which is where a comparison that keeps a byte carries the condition it is about,
263/// and then the ones behind it as far as whatever writes the condition state next. Both halves
264/// matter and for one reason: the rewrite leaves the readers where they are and takes the
265/// comparison out from under them, so each of them ends up reading what the instruction further
266/// back left instead.
267fn conditions(
268 func: &mir::Func,
269 insts: &FlagInsts,
270 names: &Interner,
271 sequence: &[mir::Inst],
272 at: usize,
273) -> Vec<Reads> {
274 let mut found = Vec::new();
275 let Some(name) = opcode(func, insts, names, sequence[at]) else { return found };
276 found.extend(insts.reads(name));
277 for &inst in &sequence[at + 1..] {
278 let Some(name) = opcode(func, insts, names, inst) else { break };
279 // What it reads before whether it writes, because an instruction can do both and the read
280 // it does is a read of what is there now. An add with carry is the one that does, and
281 // asking the questions the other way round would count it as the end of the walk and never
282 // count the carry it took off the comparison this is about to remove.
283 found.extend(insts.reads(name));
284 if (insts.writes)(name) {
285 break;
286 }
287 }
288 found
289}
290
291/// The same state, unless the instruction wrote a register it was a statement about.
292fn stale(func: &mir::Func, inst: mir::Inst, left: Option<Left>) -> Option<Left> {
293 let left = left?;
294 let touched = writes(func, inst).iter().any(|&(_, place)| left.about.contains(&place));
295 (!touched).then_some(left)
296}
297
298/// The registers an instruction reads, each with the index it reads it at.
299fn reads(func: &mir::Func, inst: mir::Inst) -> Vec<(u8, Place)> {
300 picked(func, inst, Role::Use)
301}
302
303/// The registers an instruction writes, each with the index it writes it at.
304fn writes(func: &mir::Func, inst: mir::Inst) -> Vec<(u8, Place)> {
305 let mut found = picked(func, inst, Role::Def);
306 found.extend(picked(func, inst, Role::EarlyDef));
307 found
308}
309
310/// The operands in that role, each with the index it is at.
311fn picked(func: &mir::Func, inst: mir::Inst, role: Role) -> Vec<(u8, Place)> {
312 func[func[inst].operands]
313 .iter()
314 .enumerate()
315 .filter(|(_, operand)| operand.role == role)
316 .filter_map(|(at, operand)| Some((u8::try_from(at).ok()?, (operand.class, operand.reg))))
317 .collect()
318}
319
320/// Turns a comparison into what is left of it, which is a byte or nothing at all, and says whether
321/// that was a change the target had.
322///
323/// The byte keeps the register it was going to and the constant goes, because what the constant
324/// was for was the comparison and the comparison is the part that is not happening. Nothing else
325/// about the instruction moves, which is what keeps this a rewrite of one instruction rather than
326/// a rewrite of the block around it.
327///
328/// One change is one set, since a comparison that is already made is already made whatever the one
329/// before it came to. What the set is for here is the other half of [`crate::changes`], which is
330/// the shape: the instruction the byte is left as is one this target has to have, and this is where
331/// that is asked rather than believed.
332fn took(
333 func: &mut mir::Func,
334 opcodes: &HashMap<&str, mir::Opcode>,
335 counts: &mut changes::Reads,
336 machine: &MachineInsts,
337 names: &Interner,
338 inst: mir::Inst,
339 entry: &Compare,
340) -> bool {
341 let mut set = Changes::new();
342 match entry.kept {
343 None => set.remove(inst),
344 Some(kept) => {
345 let Some(&opcode) = opcodes.get(kept) else { return false };
346 let byte: Vec<mir::Operand> = func[func[inst].operands]
347 .iter()
348 .filter(|operand| operand.role != Role::Use)
349 .copied()
350 .collect();
351 set.rewrite(inst, Plan { opcode, operands: byte, imm: None, ..Plan::of(func, inst) });
352 }
353 }
354 set.commit(func, counts, names, machine).is_ok()
355}
356
357/// The name this target knows an instruction by, for an instruction that is one of this target's.
358///
359/// The opcode in machine IR carries the target's prefix, because a function in the middle of being
360/// compiled holds instructions of one machine and the prefix is what says which. Anything without
361/// it is not something this description covers, and the rest of the pass treats that as knowing
362/// nothing rather than as knowing it is safe.
363fn opcode<'a>(
364 func: &mir::Func,
365 insts: &FlagInsts,
366 names: &'a Interner,
367 inst: mir::Inst,
368) -> Option<&'a str> {
369 names.resolve(func[inst].opcode.name()).strip_prefix(insts.prefix)
370}
371
372#[cfg(test)]
373mod tests {
374 use rucc_target::x86_64::{FLAGS, GPR, MACHINE};
375
376 use super::*;
377
378 /// A function with one block, and the names it was built with.
379 fn empty() -> (Interner, mir::Func, mir::Block) {
380 let mut names = Interner::new();
381 let mut func = mir::Func::new(names.intern("f"));
382 let block = func.create_block();
383 (names, func, block)
384 }
385
386 /// The opcode of that name on this target.
387 fn op(names: &mut Interner, name: &str) -> mir::Opcode {
388 mir::Opcode::new(names.intern(&format!("{}{name}", FLAGS.prefix)))
389 }
390
391 /// The pass, over the machine this crate has a backend for.
392 fn takes(func: &mut mir::Func, names: &mut Interner) -> usize {
393 redundant(func, &FLAGS, &MACHINE, names)
394 }
395
396 /// What every instruction in a block came to, as opcodes with the target's prefix taken off.
397 fn shape(func: &mir::Func, names: &Interner, block: mir::Block) -> Vec<String> {
398 func.insts(block)
399 .map(|inst| {
400 names
401 .resolve(func[inst].opcode.name())
402 .strip_prefix(FLAGS.prefix)
403 .unwrap_or("")
404 .to_owned()
405 })
406 .collect()
407 }
408
409 /// The shape the issue is named after: the same comparison made twice with nothing between the
410 /// two but the byte the first one kept. The second asks what the first asked, so what is left
411 /// of it is the byte alone.
412 #[test]
413 fn the_same_comparison_twice_leaves_one_comparison_and_two_bytes() {
414 let (mut names, mut func, block) = empty();
415 let value = func.new_vreg(GPR);
416 let first = func.new_vreg(GPR);
417 let second = func.new_vreg(GPR);
418 let ne = op(&mut names, "cmp_set_ne_ri_32");
419 let e = op(&mut names, "cmp_set_e_ri_32");
420 func.build(block, ne).def(first, GPR).uses(value, GPR).imm(0).finish();
421 func.build(block, e).def(second, GPR).uses(value, GPR).imm(0).finish();
422
423 assert_eq!(takes(&mut func, &mut names), 1);
424 assert_eq!(shape(&func, &names, block), ["cmp_set_ne_ri_32", "set_e"]);
425 }
426
427 /// The same two comparisons with something writing the compared register in between. The bits
428 /// are the bits the first one left and they are about a value that has moved on.
429 #[test]
430 fn a_comparison_of_a_register_something_wrote_in_between_stays() {
431 let (mut names, mut func, block) = empty();
432 let value = func.new_vreg(GPR);
433 let other = func.new_vreg(GPR);
434 let first = func.new_vreg(GPR);
435 let second = func.new_vreg(GPR);
436 let ne = op(&mut names, "cmp_set_ne_ri_32");
437 let e = op(&mut names, "cmp_set_e_ri_32");
438 let copy = op(&mut names, "mov_rr_64");
439 func.build(block, ne).def(first, GPR).uses(value, GPR).imm(0).finish();
440 func.build(block, copy).def(value, GPR).uses(other, GPR).finish();
441 func.build(block, e).def(second, GPR).uses(value, GPR).imm(0).finish();
442
443 assert_eq!(takes(&mut func, &mut names), 0);
444 assert_eq!(shape(&func, &names, block).len(), 3);
445 }
446
447 /// The common shape, which is `if (a & MASK)`. The `and` clears the carry and the overflow and
448 /// sets the zero and the sign from what it wrote, which is every bit the comparison would have
449 /// set and the same values, so every condition may read it.
450 #[test]
451 fn a_comparison_against_zero_after_a_bitwise_operation_goes() {
452 for condition in ["e", "l", "b"] {
453 let (mut names, mut func, block) = empty();
454 let value = func.new_vreg(GPR);
455 let byte = func.new_vreg(GPR);
456 let and = op(&mut names, "and_ri_32");
457 let cmp = op(&mut names, &format!("cmp_set_{condition}_ri_32"));
458 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
459 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
460
461 assert_eq!(takes(&mut func, &mut names), 1, "set{condition}");
462 assert_eq!(
463 shape(&func, &names, block),
464 ["and_ri_32".to_owned(), format!("set_{condition}")]
465 );
466 }
467 }
468
469 /// The same after a subtraction, which is the one that is only half true. The zero bit is what
470 /// a comparison of the answer against zero would have set it to, and the overflow is not, so
471 /// the conditions built out of the sign and the overflow together have to stay.
472 #[test]
473 fn a_comparison_against_zero_after_a_subtraction_goes_only_for_the_zero_conditions() {
474 for (condition, left) in [("e", 1), ("ne", 1), ("l", 0), ("ge", 0), ("a", 0)] {
475 let (mut names, mut func, block) = empty();
476 let value = func.new_vreg(GPR);
477 let other = func.new_vreg(GPR);
478 let byte = func.new_vreg(GPR);
479 let sub = op(&mut names, "sub_rr_32");
480 let cmp = op(&mut names, &format!("cmp_set_{condition}_ri_32"));
481 func.build(block, sub).def(value, GPR).uses(value, GPR).uses(other, GPR).finish();
482 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
483
484 assert_eq!(takes(&mut func, &mut names), left, "set{condition}");
485 }
486 }
487
488 /// A comparison the layout already folded a branch into, which keeps no byte at all. There is
489 /// nothing left of one of those, and the jump behind it reads what the `and` left.
490 #[test]
491 fn a_comparison_that_keeps_nothing_is_taken_out_and_the_jump_reads_what_is_there() {
492 let (mut names, mut func, block) = empty();
493 let value = func.new_vreg(GPR);
494 let and = op(&mut names, "and_ri_32");
495 let cmp = op(&mut names, "cmp_ri_32");
496 let jump = op(&mut names, "jcc_l");
497 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
498 func.build(block, cmp).uses(value, GPR).imm(0).finish();
499 func.build(block, jump).finish();
500
501 assert_eq!(takes(&mut func, &mut names), 1);
502 assert_eq!(shape(&func, &names, block), ["and_ri_32", "jcc_l"]);
503 }
504
505 /// The same three instructions with a subtraction in front. The condition is behind the
506 /// comparison rather than on it, so finding it means looking at what reads what the comparison
507 /// would have left, and a signed `<` is not something a subtraction answers.
508 #[test]
509 fn a_comparison_that_keeps_nothing_is_refused_on_the_condition_behind_it() {
510 let (mut names, mut func, block) = empty();
511 let value = func.new_vreg(GPR);
512 let other = func.new_vreg(GPR);
513 let sub = op(&mut names, "sub_rr_32");
514 let cmp = op(&mut names, "cmp_ri_32");
515 let jump = op(&mut names, "jcc_l");
516 func.build(block, sub).def(value, GPR).uses(value, GPR).uses(other, GPR).finish();
517 func.build(block, cmp).uses(value, GPR).imm(0).finish();
518 func.build(block, jump).finish();
519
520 assert_eq!(takes(&mut func, &mut names), 0);
521 assert_eq!(shape(&func, &names, block).len(), 3);
522 }
523
524 /// Arithmetic that wrote half of what the comparison is asking about. The upper half is zero
525 /// because this machine writes it that way, so the two agree about whether the value is zero
526 /// and disagree about its sign, and the description has no way to say half of one condition.
527 #[test]
528 fn a_comparison_wider_than_the_arithmetic_in_front_of_it_stays() {
529 let (mut names, mut func, block) = empty();
530 let value = func.new_vreg(GPR);
531 let byte = func.new_vreg(GPR);
532 let and = op(&mut names, "and_ri_32");
533 let cmp = op(&mut names, "cmp_set_e_ri_64");
534 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
535 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
536
537 assert_eq!(takes(&mut func, &mut names), 0);
538 assert_eq!(shape(&func, &names, block).len(), 2);
539 }
540
541 /// Something between the two that writes the condition state. A multiply is not in the
542 /// description's list because this machine leaves the zero bit undefined after one, so what it
543 /// left is not something to read and not something to reason from either.
544 #[test]
545 fn anything_that_writes_the_condition_state_in_between_makes_the_comparison_stay() {
546 let (mut names, mut func, block) = empty();
547 let value = func.new_vreg(GPR);
548 let other = func.new_vreg(GPR);
549 let byte = func.new_vreg(GPR);
550 let and = op(&mut names, "and_ri_32");
551 let mul = op(&mut names, "imul_rr_32");
552 let cmp = op(&mut names, "cmp_set_e_ri_32");
553 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
554 func.build(block, mul).def(other, GPR).uses(other, GPR).uses(other, GPR).finish();
555 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
556
557 assert_eq!(takes(&mut func, &mut names), 0);
558 assert_eq!(shape(&func, &names, block).len(), 3);
559 }
560
561 /// A comparison that keeps nothing and whose condition state nothing is found to read. It is
562 /// dead rather than redundant, and this pass is not the one that answers that.
563 #[test]
564 fn a_comparison_nothing_is_found_to_read_stays() {
565 let (mut names, mut func, block) = empty();
566 let value = func.new_vreg(GPR);
567 let and = op(&mut names, "and_ri_32");
568 let cmp = op(&mut names, "cmp_ri_32");
569 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
570 func.build(block, cmp).uses(value, GPR).imm(0).finish();
571
572 assert_eq!(takes(&mut func, &mut names), 0);
573 assert_eq!(shape(&func, &names, block).len(), 2);
574 }
575
576 /// The condition state does not cross a block boundary, and neither does this.
577 #[test]
578 fn a_comparison_in_another_block_is_not_one_the_arithmetic_answers() {
579 let (mut names, mut func, block) = empty();
580 let next = func.create_block();
581 let value = func.new_vreg(GPR);
582 let byte = func.new_vreg(GPR);
583 let and = op(&mut names, "and_ri_32");
584 let cmp = op(&mut names, "cmp_set_e_ri_32");
585 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
586 *func.succs_mut(block) = vec![mir::BlockCall::to(next)];
587 func.build(next, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
588
589 assert_eq!(takes(&mut func, &mut names), 0);
590 assert_eq!(shape(&func, &names, next).len(), 1);
591 }
592}