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 if (insts.writes)(name) {
280 break;
281 }
282 found.extend(insts.reads(name));
283 }
284 found
285}
286
287/// The same state, unless the instruction wrote a register it was a statement about.
288fn stale(func: &mir::Func, inst: mir::Inst, left: Option<Left>) -> Option<Left> {
289 let left = left?;
290 let touched = writes(func, inst).iter().any(|&(_, place)| left.about.contains(&place));
291 (!touched).then_some(left)
292}
293
294/// The registers an instruction reads, each with the index it reads it at.
295fn reads(func: &mir::Func, inst: mir::Inst) -> Vec<(u8, Place)> {
296 picked(func, inst, Role::Use)
297}
298
299/// The registers an instruction writes, each with the index it writes it at.
300fn writes(func: &mir::Func, inst: mir::Inst) -> Vec<(u8, Place)> {
301 let mut found = picked(func, inst, Role::Def);
302 found.extend(picked(func, inst, Role::EarlyDef));
303 found
304}
305
306/// The operands in that role, each with the index it is at.
307fn picked(func: &mir::Func, inst: mir::Inst, role: Role) -> Vec<(u8, Place)> {
308 func[func[inst].operands]
309 .iter()
310 .enumerate()
311 .filter(|(_, operand)| operand.role == role)
312 .filter_map(|(at, operand)| Some((u8::try_from(at).ok()?, (operand.class, operand.reg))))
313 .collect()
314}
315
316/// Turns a comparison into what is left of it, which is a byte or nothing at all, and says whether
317/// that was a change the target had.
318///
319/// The byte keeps the register it was going to and the constant goes, because what the constant
320/// was for was the comparison and the comparison is the part that is not happening. Nothing else
321/// about the instruction moves, which is what keeps this a rewrite of one instruction rather than
322/// a rewrite of the block around it.
323///
324/// One change is one set, since a comparison that is already made is already made whatever the one
325/// before it came to. What the set is for here is the other half of [`crate::changes`], which is
326/// the shape: the instruction the byte is left as is one this target has to have, and this is where
327/// that is asked rather than believed.
328fn took(
329 func: &mut mir::Func,
330 opcodes: &HashMap<&str, mir::Opcode>,
331 counts: &mut changes::Reads,
332 machine: &MachineInsts,
333 names: &Interner,
334 inst: mir::Inst,
335 entry: &Compare,
336) -> bool {
337 let mut set = Changes::new();
338 match entry.kept {
339 None => set.remove(inst),
340 Some(kept) => {
341 let Some(&opcode) = opcodes.get(kept) else { return false };
342 let byte: Vec<mir::Operand> = func[func[inst].operands]
343 .iter()
344 .filter(|operand| operand.role != Role::Use)
345 .copied()
346 .collect();
347 set.rewrite(inst, Plan { opcode, operands: byte, imm: None, ..Plan::of(func, inst) });
348 }
349 }
350 set.commit(func, counts, names, machine).is_ok()
351}
352
353/// The name this target knows an instruction by, for an instruction that is one of this target's.
354///
355/// The opcode in machine IR carries the target's prefix, because a function in the middle of being
356/// compiled holds instructions of one machine and the prefix is what says which. Anything without
357/// it is not something this description covers, and the rest of the pass treats that as knowing
358/// nothing rather than as knowing it is safe.
359fn opcode<'a>(
360 func: &mir::Func,
361 insts: &FlagInsts,
362 names: &'a Interner,
363 inst: mir::Inst,
364) -> Option<&'a str> {
365 names.resolve(func[inst].opcode.name()).strip_prefix(insts.prefix)
366}
367
368#[cfg(test)]
369mod tests {
370 use rucc_target::x86_64::{FLAGS, GPR, MACHINE};
371
372 use super::*;
373
374 /// A function with one block, and the names it was built with.
375 fn empty() -> (Interner, mir::Func, mir::Block) {
376 let mut names = Interner::new();
377 let mut func = mir::Func::new(names.intern("f"));
378 let block = func.create_block();
379 (names, func, block)
380 }
381
382 /// The opcode of that name on this target.
383 fn op(names: &mut Interner, name: &str) -> mir::Opcode {
384 mir::Opcode::new(names.intern(&format!("{}{name}", FLAGS.prefix)))
385 }
386
387 /// The pass, over the machine this crate has a backend for.
388 fn takes(func: &mut mir::Func, names: &mut Interner) -> usize {
389 redundant(func, &FLAGS, &MACHINE, names)
390 }
391
392 /// What every instruction in a block came to, as opcodes with the target's prefix taken off.
393 fn shape(func: &mir::Func, names: &Interner, block: mir::Block) -> Vec<String> {
394 func.insts(block)
395 .map(|inst| {
396 names
397 .resolve(func[inst].opcode.name())
398 .strip_prefix(FLAGS.prefix)
399 .unwrap_or("")
400 .to_owned()
401 })
402 .collect()
403 }
404
405 /// The shape the issue is named after: the same comparison made twice with nothing between the
406 /// two but the byte the first one kept. The second asks what the first asked, so what is left
407 /// of it is the byte alone.
408 #[test]
409 fn the_same_comparison_twice_leaves_one_comparison_and_two_bytes() {
410 let (mut names, mut func, block) = empty();
411 let value = func.new_vreg(GPR);
412 let first = func.new_vreg(GPR);
413 let second = func.new_vreg(GPR);
414 let ne = op(&mut names, "cmp_set_ne_ri_32");
415 let e = op(&mut names, "cmp_set_e_ri_32");
416 func.build(block, ne).def(first, GPR).uses(value, GPR).imm(0).finish();
417 func.build(block, e).def(second, GPR).uses(value, GPR).imm(0).finish();
418
419 assert_eq!(takes(&mut func, &mut names), 1);
420 assert_eq!(shape(&func, &names, block), ["cmp_set_ne_ri_32", "set_e"]);
421 }
422
423 /// The same two comparisons with something writing the compared register in between. The bits
424 /// are the bits the first one left and they are about a value that has moved on.
425 #[test]
426 fn a_comparison_of_a_register_something_wrote_in_between_stays() {
427 let (mut names, mut func, block) = empty();
428 let value = func.new_vreg(GPR);
429 let other = func.new_vreg(GPR);
430 let first = func.new_vreg(GPR);
431 let second = func.new_vreg(GPR);
432 let ne = op(&mut names, "cmp_set_ne_ri_32");
433 let e = op(&mut names, "cmp_set_e_ri_32");
434 let copy = op(&mut names, "mov_rr_64");
435 func.build(block, ne).def(first, GPR).uses(value, GPR).imm(0).finish();
436 func.build(block, copy).def(value, GPR).uses(other, GPR).finish();
437 func.build(block, e).def(second, GPR).uses(value, GPR).imm(0).finish();
438
439 assert_eq!(takes(&mut func, &mut names), 0);
440 assert_eq!(shape(&func, &names, block).len(), 3);
441 }
442
443 /// The common shape, which is `if (a & MASK)`. The `and` clears the carry and the overflow and
444 /// sets the zero and the sign from what it wrote, which is every bit the comparison would have
445 /// set and the same values, so every condition may read it.
446 #[test]
447 fn a_comparison_against_zero_after_a_bitwise_operation_goes() {
448 for condition in ["e", "l", "b"] {
449 let (mut names, mut func, block) = empty();
450 let value = func.new_vreg(GPR);
451 let byte = func.new_vreg(GPR);
452 let and = op(&mut names, "and_ri_32");
453 let cmp = op(&mut names, &format!("cmp_set_{condition}_ri_32"));
454 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
455 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
456
457 assert_eq!(takes(&mut func, &mut names), 1, "set{condition}");
458 assert_eq!(
459 shape(&func, &names, block),
460 ["and_ri_32".to_owned(), format!("set_{condition}")]
461 );
462 }
463 }
464
465 /// The same after a subtraction, which is the one that is only half true. The zero bit is what
466 /// a comparison of the answer against zero would have set it to, and the overflow is not, so
467 /// the conditions built out of the sign and the overflow together have to stay.
468 #[test]
469 fn a_comparison_against_zero_after_a_subtraction_goes_only_for_the_zero_conditions() {
470 for (condition, left) in [("e", 1), ("ne", 1), ("l", 0), ("ge", 0), ("a", 0)] {
471 let (mut names, mut func, block) = empty();
472 let value = func.new_vreg(GPR);
473 let other = func.new_vreg(GPR);
474 let byte = func.new_vreg(GPR);
475 let sub = op(&mut names, "sub_rr_32");
476 let cmp = op(&mut names, &format!("cmp_set_{condition}_ri_32"));
477 func.build(block, sub).def(value, GPR).uses(value, GPR).uses(other, GPR).finish();
478 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
479
480 assert_eq!(takes(&mut func, &mut names), left, "set{condition}");
481 }
482 }
483
484 /// A comparison the layout already folded a branch into, which keeps no byte at all. There is
485 /// nothing left of one of those, and the jump behind it reads what the `and` left.
486 #[test]
487 fn a_comparison_that_keeps_nothing_is_taken_out_and_the_jump_reads_what_is_there() {
488 let (mut names, mut func, block) = empty();
489 let value = func.new_vreg(GPR);
490 let and = op(&mut names, "and_ri_32");
491 let cmp = op(&mut names, "cmp_ri_32");
492 let jump = op(&mut names, "jcc_l");
493 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
494 func.build(block, cmp).uses(value, GPR).imm(0).finish();
495 func.build(block, jump).finish();
496
497 assert_eq!(takes(&mut func, &mut names), 1);
498 assert_eq!(shape(&func, &names, block), ["and_ri_32", "jcc_l"]);
499 }
500
501 /// The same three instructions with a subtraction in front. The condition is behind the
502 /// comparison rather than on it, so finding it means looking at what reads what the comparison
503 /// would have left, and a signed `<` is not something a subtraction answers.
504 #[test]
505 fn a_comparison_that_keeps_nothing_is_refused_on_the_condition_behind_it() {
506 let (mut names, mut func, block) = empty();
507 let value = func.new_vreg(GPR);
508 let other = func.new_vreg(GPR);
509 let sub = op(&mut names, "sub_rr_32");
510 let cmp = op(&mut names, "cmp_ri_32");
511 let jump = op(&mut names, "jcc_l");
512 func.build(block, sub).def(value, GPR).uses(value, GPR).uses(other, GPR).finish();
513 func.build(block, cmp).uses(value, GPR).imm(0).finish();
514 func.build(block, jump).finish();
515
516 assert_eq!(takes(&mut func, &mut names), 0);
517 assert_eq!(shape(&func, &names, block).len(), 3);
518 }
519
520 /// Arithmetic that wrote half of what the comparison is asking about. The upper half is zero
521 /// because this machine writes it that way, so the two agree about whether the value is zero
522 /// and disagree about its sign, and the description has no way to say half of one condition.
523 #[test]
524 fn a_comparison_wider_than_the_arithmetic_in_front_of_it_stays() {
525 let (mut names, mut func, block) = empty();
526 let value = func.new_vreg(GPR);
527 let byte = func.new_vreg(GPR);
528 let and = op(&mut names, "and_ri_32");
529 let cmp = op(&mut names, "cmp_set_e_ri_64");
530 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
531 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
532
533 assert_eq!(takes(&mut func, &mut names), 0);
534 assert_eq!(shape(&func, &names, block).len(), 2);
535 }
536
537 /// Something between the two that writes the condition state. A multiply is not in the
538 /// description's list because this machine leaves the zero bit undefined after one, so what it
539 /// left is not something to read and not something to reason from either.
540 #[test]
541 fn anything_that_writes_the_condition_state_in_between_makes_the_comparison_stay() {
542 let (mut names, mut func, block) = empty();
543 let value = func.new_vreg(GPR);
544 let other = func.new_vreg(GPR);
545 let byte = func.new_vreg(GPR);
546 let and = op(&mut names, "and_ri_32");
547 let mul = op(&mut names, "imul_rr_32");
548 let cmp = op(&mut names, "cmp_set_e_ri_32");
549 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
550 func.build(block, mul).def(other, GPR).uses(other, GPR).uses(other, GPR).finish();
551 func.build(block, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
552
553 assert_eq!(takes(&mut func, &mut names), 0);
554 assert_eq!(shape(&func, &names, block).len(), 3);
555 }
556
557 /// A comparison that keeps nothing and whose condition state nothing is found to read. It is
558 /// dead rather than redundant, and this pass is not the one that answers that.
559 #[test]
560 fn a_comparison_nothing_is_found_to_read_stays() {
561 let (mut names, mut func, block) = empty();
562 let value = func.new_vreg(GPR);
563 let and = op(&mut names, "and_ri_32");
564 let cmp = op(&mut names, "cmp_ri_32");
565 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
566 func.build(block, cmp).uses(value, GPR).imm(0).finish();
567
568 assert_eq!(takes(&mut func, &mut names), 0);
569 assert_eq!(shape(&func, &names, block).len(), 2);
570 }
571
572 /// The condition state does not cross a block boundary, and neither does this.
573 #[test]
574 fn a_comparison_in_another_block_is_not_one_the_arithmetic_answers() {
575 let (mut names, mut func, block) = empty();
576 let next = func.create_block();
577 let value = func.new_vreg(GPR);
578 let byte = func.new_vreg(GPR);
579 let and = op(&mut names, "and_ri_32");
580 let cmp = op(&mut names, "cmp_set_e_ri_32");
581 func.build(block, and).def(value, GPR).uses(value, GPR).imm(255).finish();
582 *func.succs_mut(block) = vec![mir::BlockCall::to(next)];
583 func.build(next, cmp).def(byte, GPR).uses(value, GPR).imm(0).finish();
584
585 assert_eq!(takes(&mut func, &mut names), 0);
586 assert_eq!(shape(&func, &names, next).len(), 1);
587 }
588}