(&self, output: &Output<'_, impl Function, impl RegInfo>)
| 44 | /// the function based on the block frequencies provided. |
| 45 | #[must_use] |
| 46 | pub fn evaluate(&self, output: &Output<'_, impl Function, impl RegInfo>) -> f32 { |
| 47 | let mut score = 0.0; |
| 48 | let reginfo = output.reginfo(); |
| 49 | let func = output.function(); |
| 50 | for block in func.blocks() { |
| 51 | let freq = func.block_frequency(block); |
| 52 | for inst in output.output_insts(block) { |
| 53 | match inst { |
| 54 | OutputInst::Inst { |
| 55 | inst, |
| 56 | operand_allocs, |
| 57 | } => { |
| 58 | // Penalize instruction operands that are assigned to |
| 59 | // memory instead of registers. |
| 60 | for (&op, &alloc) in func.inst_operands(inst).iter().zip(operand_allocs) { |
| 61 | if !alloc.is_memory(reginfo) { |
| 62 | continue; |
| 63 | } |
| 64 | let class = match op.constraint() { |
| 65 | OperandConstraint::Class(class) => class, |
| 66 | OperandConstraint::Fixed(_) => continue, |
| 67 | OperandConstraint::Reuse(idx) => { |
| 68 | let OperandConstraint::Class(class) = |
| 69 | func.inst_operands(inst)[idx].constraint() |
| 70 | else { |
| 71 | unreachable!() |
| 72 | }; |
| 73 | class |
| 74 | } |
| 75 | }; |
| 76 | let cost = match op.kind() { |
| 77 | OperandKind::Def(_) | OperandKind::EarlyDef(_) => { |
| 78 | self.store_cost - self.move_cost |
| 79 | } |
| 80 | OperandKind::Use(_) => self.load_cost - self.move_cost, |
| 81 | OperandKind::DefGroup(_) |
| 82 | | OperandKind::UseGroup(_) |
| 83 | | OperandKind::EarlyDefGroup(_) |
| 84 | | OperandKind::NonAllocatable => continue, |
| 85 | }; |
| 86 | |
| 87 | // Scale this with the class spill cost: a class |
| 88 | // spill cost of 0 means that there is no cost to |
| 89 | // choosing a spill slot instead of a register. |
| 90 | score += cost * freq * reginfo.class_spill_cost(class); |
| 91 | } |
| 92 | } |
| 93 | OutputInst::Rematerialize { value, to } => { |
| 94 | // Treat a rematerialization as having 2 parts: actually |
| 95 | // computing the value and then moving it to its |
| 96 | // destination. |
| 97 | let remat_cost = match func.can_rematerialize(value).unwrap().0 { |
| 98 | RematCost::CheaperThanMove => 0.0, |
| 99 | RematCost::CheaperThanLoad => self.remat_cost, |
| 100 | }; |
| 101 | let move_cost = match to.is_memory(reginfo) { |
| 102 | false => self.move_cost, |
| 103 | true => self.store_cost, |
nothing calls this directly
no test coverage detected