2020-10-22 21:30:03 +00:00
|
|
|
use super::Error;
|
|
|
|
|
|
|
|
use super::graph;
|
2020-10-23 03:28:16 +00:00
|
|
|
|
2020-10-22 21:30:03 +00:00
|
|
|
use graph::{BasicCoverageBlock, BcbBranch, CoverageGraph, TraverseCoverageGraphWithLoops};
|
2020-10-23 03:28:16 +00:00
|
|
|
|
2023-06-29 06:50:52 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-10-22 21:30:03 +00:00
|
|
|
use rustc_data_structures::graph::WithNumNodes;
|
|
|
|
use rustc_index::bit_set::BitSet;
|
2023-06-29 06:50:52 +00:00
|
|
|
use rustc_index::IndexVec;
|
2020-10-23 07:45:07 +00:00
|
|
|
use rustc_middle::mir::coverage::*;
|
|
|
|
|
2023-07-08 03:43:29 +00:00
|
|
|
use std::fmt::{self, Debug};
|
|
|
|
|
2023-09-19 10:26:23 +00:00
|
|
|
const NESTED_INDENT: &str = " ";
|
|
|
|
|
2023-07-08 03:43:29 +00:00
|
|
|
/// The coverage counter or counter expression associated with a particular
|
|
|
|
/// BCB node or BCB edge.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(super) enum BcbCounter {
|
2023-08-14 02:16:29 +00:00
|
|
|
Counter { id: CounterId },
|
2023-07-08 03:43:29 +00:00
|
|
|
Expression { id: ExpressionId, lhs: Operand, op: Op, rhs: Operand },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BcbCounter {
|
|
|
|
fn is_expression(&self) -> bool {
|
|
|
|
matches!(self, Self::Expression { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn as_operand(&self) -> Operand {
|
|
|
|
match *self {
|
|
|
|
BcbCounter::Counter { id, .. } => Operand::Counter(id),
|
|
|
|
BcbCounter::Expression { id, .. } => Operand::Expression(id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for BcbCounter {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Counter { id, .. } => write!(fmt, "Counter({:?})", id.index()),
|
|
|
|
Self::Expression { id, lhs, op, rhs } => write!(
|
|
|
|
fmt,
|
|
|
|
"Expression({:?}) = {:?} {} {:?}",
|
|
|
|
id.index(),
|
|
|
|
lhs,
|
|
|
|
match op {
|
|
|
|
Op::Add => "+",
|
|
|
|
Op::Subtract => "-",
|
|
|
|
},
|
|
|
|
rhs,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 06:50:52 +00:00
|
|
|
/// Generates and stores coverage counter and coverage expression information
|
|
|
|
/// associated with nodes/edges in the BCB graph.
|
2020-11-03 05:32:48 +00:00
|
|
|
pub(super) struct CoverageCounters {
|
2023-06-29 02:36:19 +00:00
|
|
|
next_counter_id: CounterId,
|
2023-06-29 02:14:04 +00:00
|
|
|
next_expression_id: ExpressionId,
|
2023-06-29 04:25:28 +00:00
|
|
|
|
2023-06-29 06:50:52 +00:00
|
|
|
/// Coverage counters/expressions that are associated with individual BCBs.
|
2023-07-08 03:43:29 +00:00
|
|
|
bcb_counters: IndexVec<BasicCoverageBlock, Option<BcbCounter>>,
|
2023-06-29 06:50:52 +00:00
|
|
|
/// Coverage counters/expressions that are associated with the control-flow
|
|
|
|
/// edge between two BCBs.
|
2023-07-08 03:43:29 +00:00
|
|
|
bcb_edge_counters: FxHashMap<(BasicCoverageBlock, BasicCoverageBlock), BcbCounter>,
|
2023-06-29 06:50:52 +00:00
|
|
|
/// Tracks which BCBs have a counter associated with some incoming edge.
|
|
|
|
/// Only used by debug assertions, to verify that BCBs with incoming edge
|
|
|
|
/// counters do not have their own physical counters (expressions are allowed).
|
|
|
|
bcb_has_incoming_edge_counters: BitSet<BasicCoverageBlock>,
|
2023-06-29 04:25:28 +00:00
|
|
|
/// Expression nodes that are not directly associated with any particular
|
|
|
|
/// BCB/edge, but are needed as operands to more complex expressions.
|
2023-07-08 03:43:29 +00:00
|
|
|
/// These are always [`BcbCounter::Expression`].
|
|
|
|
pub(super) intermediate_expressions: Vec<BcbCounter>,
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageCounters {
|
2023-08-14 02:16:29 +00:00
|
|
|
pub(super) fn new(basic_coverage_blocks: &CoverageGraph) -> Self {
|
2023-06-29 06:50:52 +00:00
|
|
|
let num_bcbs = basic_coverage_blocks.num_nodes();
|
|
|
|
|
2020-10-23 07:45:07 +00:00
|
|
|
Self {
|
2023-06-29 02:36:19 +00:00
|
|
|
next_counter_id: CounterId::START,
|
2023-06-29 02:14:04 +00:00
|
|
|
next_expression_id: ExpressionId::START,
|
2023-06-29 04:25:28 +00:00
|
|
|
|
2023-06-29 06:50:52 +00:00
|
|
|
bcb_counters: IndexVec::from_elem_n(None, num_bcbs),
|
|
|
|
bcb_edge_counters: FxHashMap::default(),
|
|
|
|
bcb_has_incoming_edge_counters: BitSet::new_empty(num_bcbs),
|
2023-06-29 04:25:28 +00:00
|
|
|
intermediate_expressions: Vec::new(),
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 03:43:29 +00:00
|
|
|
/// Makes [`BcbCounter`] `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
|
2023-09-17 12:22:21 +00:00
|
|
|
/// indirectly associated with coverage spans, and accumulates additional `Expression`s
|
2020-10-22 21:30:03 +00:00
|
|
|
/// representing intermediate values.
|
|
|
|
pub fn make_bcb_counters(
|
|
|
|
&mut self,
|
2023-06-29 06:50:52 +00:00
|
|
|
basic_coverage_blocks: &CoverageGraph,
|
2023-09-17 12:22:21 +00:00
|
|
|
bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool,
|
2023-06-29 04:25:28 +00:00
|
|
|
) -> Result<(), Error> {
|
2023-09-17 12:22:21 +00:00
|
|
|
MakeBcbCounters::new(self, basic_coverage_blocks).make_bcb_counters(bcb_has_coverage_spans)
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 10:34:52 +00:00
|
|
|
fn make_counter(&mut self) -> BcbCounter {
|
2023-09-19 10:26:23 +00:00
|
|
|
let id = self.next_counter();
|
|
|
|
BcbCounter::Counter { id }
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 10:34:52 +00:00
|
|
|
fn make_expression(&mut self, lhs: Operand, op: Op, rhs: Operand) -> BcbCounter {
|
2020-10-23 07:45:07 +00:00
|
|
|
let id = self.next_expression();
|
2023-09-19 10:26:23 +00:00
|
|
|
BcbCounter::Expression { id, lhs, op, rhs }
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Counter IDs start from one and go up.
|
2023-06-29 02:36:19 +00:00
|
|
|
fn next_counter(&mut self) -> CounterId {
|
2020-10-23 07:45:07 +00:00
|
|
|
let next = self.next_counter_id;
|
2023-09-06 07:53:04 +00:00
|
|
|
self.next_counter_id = self.next_counter_id + 1;
|
2023-06-29 02:36:19 +00:00
|
|
|
next
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 02:14:04 +00:00
|
|
|
/// Expression IDs start from 0 and go up.
|
|
|
|
/// (Counter IDs and Expression IDs are distinguished by the `Operand` enum.)
|
|
|
|
fn next_expression(&mut self) -> ExpressionId {
|
|
|
|
let next = self.next_expression_id;
|
2023-09-06 07:53:04 +00:00
|
|
|
self.next_expression_id = self.next_expression_id + 1;
|
2023-06-29 02:14:04 +00:00
|
|
|
next
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
2023-06-29 06:50:52 +00:00
|
|
|
|
|
|
|
fn set_bcb_counter(
|
|
|
|
&mut self,
|
|
|
|
bcb: BasicCoverageBlock,
|
2023-07-08 03:43:29 +00:00
|
|
|
counter_kind: BcbCounter,
|
2023-06-29 06:50:52 +00:00
|
|
|
) -> Result<Operand, Error> {
|
|
|
|
debug_assert!(
|
|
|
|
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
|
|
|
|
// have an expression (to be injected into an existing `BasicBlock` represented by this
|
|
|
|
// `BasicCoverageBlock`).
|
|
|
|
counter_kind.is_expression() || !self.bcb_has_incoming_edge_counters.contains(bcb),
|
|
|
|
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
|
|
|
|
);
|
|
|
|
let operand = counter_kind.as_operand();
|
|
|
|
if let Some(replaced) = self.bcb_counters[bcb].replace(counter_kind) {
|
|
|
|
Error::from_string(format!(
|
|
|
|
"attempt to set a BasicCoverageBlock coverage counter more than once; \
|
|
|
|
{bcb:?} already had counter {replaced:?}",
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(operand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_bcb_edge_counter(
|
|
|
|
&mut self,
|
|
|
|
from_bcb: BasicCoverageBlock,
|
|
|
|
to_bcb: BasicCoverageBlock,
|
2023-07-08 03:43:29 +00:00
|
|
|
counter_kind: BcbCounter,
|
2023-06-29 06:50:52 +00:00
|
|
|
) -> Result<Operand, Error> {
|
|
|
|
if level_enabled!(tracing::Level::DEBUG) {
|
|
|
|
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
|
|
|
|
// have an expression (to be injected into an existing `BasicBlock` represented by this
|
|
|
|
// `BasicCoverageBlock`).
|
|
|
|
if self.bcb_counter(to_bcb).is_some_and(|c| !c.is_expression()) {
|
|
|
|
return Error::from_string(format!(
|
|
|
|
"attempt to add an incoming edge counter from {from_bcb:?} when the target BCB already \
|
|
|
|
has a `Counter`"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.bcb_has_incoming_edge_counters.insert(to_bcb);
|
|
|
|
let operand = counter_kind.as_operand();
|
|
|
|
if let Some(replaced) = self.bcb_edge_counters.insert((from_bcb, to_bcb), counter_kind) {
|
|
|
|
Error::from_string(format!(
|
|
|
|
"attempt to set an edge counter more than once; from_bcb: \
|
|
|
|
{from_bcb:?} already had counter {replaced:?}",
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(operand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 03:43:29 +00:00
|
|
|
pub(super) fn bcb_counter(&self, bcb: BasicCoverageBlock) -> Option<&BcbCounter> {
|
2023-06-29 06:50:52 +00:00
|
|
|
self.bcb_counters[bcb].as_ref()
|
|
|
|
}
|
|
|
|
|
2023-07-08 03:43:29 +00:00
|
|
|
pub(super) fn take_bcb_counter(&mut self, bcb: BasicCoverageBlock) -> Option<BcbCounter> {
|
2023-06-29 06:50:52 +00:00
|
|
|
self.bcb_counters[bcb].take()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn drain_bcb_counters(
|
|
|
|
&mut self,
|
2023-07-08 03:43:29 +00:00
|
|
|
) -> impl Iterator<Item = (BasicCoverageBlock, BcbCounter)> + '_ {
|
2023-06-29 06:50:52 +00:00
|
|
|
self.bcb_counters
|
|
|
|
.iter_enumerated_mut()
|
|
|
|
.filter_map(|(bcb, counter)| Some((bcb, counter.take()?)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn drain_bcb_edge_counters(
|
|
|
|
&mut self,
|
2023-07-08 03:43:29 +00:00
|
|
|
) -> impl Iterator<Item = ((BasicCoverageBlock, BasicCoverageBlock), BcbCounter)> + '_ {
|
2023-06-29 06:50:52 +00:00
|
|
|
self.bcb_edge_counters.drain()
|
|
|
|
}
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
2020-10-22 21:30:03 +00:00
|
|
|
|
|
|
|
/// Traverse the `CoverageGraph` and add either a `Counter` or `Expression` to every BCB, to be
|
2023-09-17 12:22:21 +00:00
|
|
|
/// injected with coverage spans. `Expressions` have no runtime overhead, so if a viable expression
|
2020-10-22 21:30:03 +00:00
|
|
|
/// (adding or subtracting two other counters or expressions) can compute the same result as an
|
|
|
|
/// embedded counter, an `Expression` should be used.
|
2023-06-29 04:02:08 +00:00
|
|
|
struct MakeBcbCounters<'a> {
|
2020-10-22 21:30:03 +00:00
|
|
|
coverage_counters: &'a mut CoverageCounters,
|
2023-06-29 06:50:52 +00:00
|
|
|
basic_coverage_blocks: &'a CoverageGraph,
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 04:02:08 +00:00
|
|
|
impl<'a> MakeBcbCounters<'a> {
|
2020-10-22 21:30:03 +00:00
|
|
|
fn new(
|
|
|
|
coverage_counters: &'a mut CoverageCounters,
|
2023-06-29 06:50:52 +00:00
|
|
|
basic_coverage_blocks: &'a CoverageGraph,
|
2020-10-22 21:30:03 +00:00
|
|
|
) -> Self {
|
|
|
|
Self { coverage_counters, basic_coverage_blocks }
|
|
|
|
}
|
|
|
|
|
2020-10-30 23:09:05 +00:00
|
|
|
/// If two `BasicCoverageBlock`s branch from another `BasicCoverageBlock`, one of the branches
|
2020-10-22 21:30:03 +00:00
|
|
|
/// can be counted by `Expression` by subtracting the other branch from the branching
|
|
|
|
/// block. Otherwise, the `BasicCoverageBlock` executed the least should have the `Counter`.
|
|
|
|
/// One way to predict which branch executes the least is by considering loops. A loop is exited
|
|
|
|
/// at a branch, so the branch that jumps to a `BasicCoverageBlock` outside the loop is almost
|
|
|
|
/// always executed less than the branch that does not exit the loop.
|
|
|
|
///
|
|
|
|
/// Returns any non-code-span expressions created to represent intermediate values (such as to
|
|
|
|
/// add two counters so the result can be subtracted from another counter), or an Error with
|
|
|
|
/// message for subsequent debugging.
|
2023-09-17 12:22:21 +00:00
|
|
|
fn make_bcb_counters(
|
|
|
|
&mut self,
|
|
|
|
bcb_has_coverage_spans: impl Fn(BasicCoverageBlock) -> bool,
|
|
|
|
) -> Result<(), Error> {
|
2020-10-22 21:30:03 +00:00
|
|
|
debug!("make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock");
|
|
|
|
|
2020-10-30 23:09:05 +00:00
|
|
|
// Walk the `CoverageGraph`. For each `BasicCoverageBlock` node with an associated
|
2023-09-17 12:22:21 +00:00
|
|
|
// coverage span, add a counter. If the `BasicCoverageBlock` branches, add a counter or
|
2020-10-30 23:09:05 +00:00
|
|
|
// expression to each branch `BasicCoverageBlock` (if the branch BCB has only one incoming
|
|
|
|
// edge) or edge from the branching BCB to the branch BCB (if the branch BCB has multiple
|
|
|
|
// incoming edges).
|
|
|
|
//
|
|
|
|
// The `TraverseCoverageGraphWithLoops` traversal ensures that, when a loop is encountered,
|
|
|
|
// all `BasicCoverageBlock` nodes in the loop are visited before visiting any node outside
|
|
|
|
// the loop. The `traversal` state includes a `context_stack`, providing a way to know if
|
|
|
|
// the current BCB is in one or more nested loops or not.
|
2020-10-22 21:30:03 +00:00
|
|
|
let mut traversal = TraverseCoverageGraphWithLoops::new(&self.basic_coverage_blocks);
|
|
|
|
while let Some(bcb) = traversal.next(self.basic_coverage_blocks) {
|
2023-09-17 12:22:21 +00:00
|
|
|
if bcb_has_coverage_spans(bcb) {
|
2023-09-17 12:22:21 +00:00
|
|
|
debug!("{:?} has at least one coverage span. Get or make its counter", bcb);
|
2023-06-29 04:25:28 +00:00
|
|
|
let branching_counter_operand = self.get_or_make_counter_operand(bcb)?;
|
2020-10-22 21:30:03 +00:00
|
|
|
|
|
|
|
if self.bcb_needs_branch_counters(bcb) {
|
2023-06-29 04:25:28 +00:00
|
|
|
self.make_branch_counters(&mut traversal, bcb, branching_counter_operand)?;
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug!(
|
2023-09-17 12:22:21 +00:00
|
|
|
"{:?} does not have any coverage spans. A counter will only be added if \
|
2020-10-22 21:30:03 +00:00
|
|
|
and when a covered BCB has an expression dependency.",
|
|
|
|
bcb,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if traversal.is_complete() {
|
2023-06-29 04:25:28 +00:00
|
|
|
Ok(())
|
2020-10-22 21:30:03 +00:00
|
|
|
} else {
|
|
|
|
Error::from_string(format!(
|
|
|
|
"`TraverseCoverageGraphWithLoops` missed some `BasicCoverageBlock`s: {:?}",
|
|
|
|
traversal.unvisited(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_branch_counters(
|
|
|
|
&mut self,
|
|
|
|
traversal: &mut TraverseCoverageGraphWithLoops,
|
|
|
|
branching_bcb: BasicCoverageBlock,
|
2023-06-26 12:28:48 +00:00
|
|
|
branching_counter_operand: Operand,
|
2020-10-22 21:30:03 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let branches = self.bcb_branches(branching_bcb);
|
|
|
|
debug!(
|
|
|
|
"{:?} has some branch(es) without counters:\n {}",
|
|
|
|
branching_bcb,
|
|
|
|
branches
|
|
|
|
.iter()
|
2023-06-29 06:50:52 +00:00
|
|
|
.map(|branch| { format!("{:?}: {:?}", branch, self.branch_counter(branch)) })
|
2020-10-22 21:30:03 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n "),
|
|
|
|
);
|
|
|
|
|
2020-10-30 23:09:05 +00:00
|
|
|
// Use the `traversal` state to decide if a subset of the branches exit a loop, making it
|
|
|
|
// likely that branch is executed less than branches that do not exit the same loop. In this
|
|
|
|
// case, any branch that does not exit the loop (and has not already been assigned a
|
|
|
|
// counter) should be counted by expression, if possible. (If a preferred expression branch
|
|
|
|
// is not selected based on the loop context, select any branch without an existing
|
|
|
|
// counter.)
|
2020-10-22 21:30:03 +00:00
|
|
|
let expression_branch = self.choose_preferred_expression_branch(traversal, &branches);
|
2020-10-30 23:09:05 +00:00
|
|
|
|
|
|
|
// Assign a Counter or Expression to each branch, plus additional `Expression`s, as needed,
|
|
|
|
// to sum up intermediate results.
|
2020-10-22 21:30:03 +00:00
|
|
|
let mut some_sumup_counter_operand = None;
|
|
|
|
for branch in branches {
|
2020-10-30 23:09:05 +00:00
|
|
|
// Skip the selected `expression_branch`, if any. It's expression will be assigned after
|
|
|
|
// all others.
|
2020-10-22 21:30:03 +00:00
|
|
|
if branch != expression_branch {
|
|
|
|
let branch_counter_operand = if branch.is_only_path_to_target() {
|
|
|
|
debug!(
|
|
|
|
" {:?} has only one incoming edge (from {:?}), so adding a \
|
|
|
|
counter",
|
|
|
|
branch, branching_bcb
|
|
|
|
);
|
2023-06-29 04:25:28 +00:00
|
|
|
self.get_or_make_counter_operand(branch.target_bcb)?
|
2020-10-22 21:30:03 +00:00
|
|
|
} else {
|
|
|
|
debug!(" {:?} has multiple incoming edges, so adding an edge counter", branch);
|
2023-06-29 04:25:28 +00:00
|
|
|
self.get_or_make_edge_counter_operand(branching_bcb, branch.target_bcb)?
|
2020-10-22 21:30:03 +00:00
|
|
|
};
|
|
|
|
if let Some(sumup_counter_operand) =
|
|
|
|
some_sumup_counter_operand.replace(branch_counter_operand)
|
|
|
|
{
|
|
|
|
let intermediate_expression = self.coverage_counters.make_expression(
|
|
|
|
branch_counter_operand,
|
|
|
|
Op::Add,
|
|
|
|
sumup_counter_operand,
|
|
|
|
);
|
2023-09-19 10:29:47 +00:00
|
|
|
debug!(" [new intermediate expression: {:?}]", intermediate_expression);
|
2023-06-26 12:28:48 +00:00
|
|
|
let intermediate_expression_operand = intermediate_expression.as_operand();
|
2023-06-29 04:25:28 +00:00
|
|
|
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
|
2020-10-22 21:30:03 +00:00
|
|
|
some_sumup_counter_operand.replace(intermediate_expression_operand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-30 23:09:05 +00:00
|
|
|
|
|
|
|
// Assign the final expression to the `expression_branch` by subtracting the total of all
|
|
|
|
// other branches from the counter of the branching BCB.
|
2020-10-22 21:30:03 +00:00
|
|
|
let sumup_counter_operand =
|
|
|
|
some_sumup_counter_operand.expect("sumup_counter_operand should have a value");
|
|
|
|
debug!(
|
|
|
|
"Making an expression for the selected expression_branch: {:?} \
|
|
|
|
(expression_branch predecessors: {:?})",
|
|
|
|
expression_branch,
|
|
|
|
self.bcb_predecessors(expression_branch.target_bcb),
|
|
|
|
);
|
|
|
|
let expression = self.coverage_counters.make_expression(
|
|
|
|
branching_counter_operand,
|
|
|
|
Op::Subtract,
|
|
|
|
sumup_counter_operand,
|
|
|
|
);
|
2023-09-19 10:29:47 +00:00
|
|
|
debug!("{:?} gets an expression: {:?}", expression_branch, expression);
|
2020-10-22 21:30:03 +00:00
|
|
|
let bcb = expression_branch.target_bcb;
|
|
|
|
if expression_branch.is_only_path_to_target() {
|
2023-06-29 06:50:52 +00:00
|
|
|
self.coverage_counters.set_bcb_counter(bcb, expression)?;
|
2020-10-22 21:30:03 +00:00
|
|
|
} else {
|
2023-06-29 06:50:52 +00:00
|
|
|
self.coverage_counters.set_bcb_edge_counter(branching_bcb, bcb, expression)?;
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-29 04:25:28 +00:00
|
|
|
fn get_or_make_counter_operand(&mut self, bcb: BasicCoverageBlock) -> Result<Operand, Error> {
|
|
|
|
self.recursive_get_or_make_counter_operand(bcb, 1)
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recursive_get_or_make_counter_operand(
|
|
|
|
&mut self,
|
|
|
|
bcb: BasicCoverageBlock,
|
|
|
|
debug_indent_level: usize,
|
2023-06-26 12:28:48 +00:00
|
|
|
) -> Result<Operand, Error> {
|
2020-10-30 23:09:05 +00:00
|
|
|
// If the BCB already has a counter, return it.
|
2023-06-29 06:50:52 +00:00
|
|
|
if let Some(counter_kind) = &self.coverage_counters.bcb_counters[bcb] {
|
2020-10-30 23:09:05 +00:00
|
|
|
debug!(
|
2023-09-19 10:29:47 +00:00
|
|
|
"{}{:?} already has a counter: {:?}",
|
2020-10-30 23:09:05 +00:00
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
bcb,
|
2023-09-19 10:29:47 +00:00
|
|
|
counter_kind,
|
2020-10-30 23:09:05 +00:00
|
|
|
);
|
2023-06-26 12:28:48 +00:00
|
|
|
return Ok(counter_kind.as_operand());
|
2020-10-30 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
|
|
|
|
// Also, a BCB that loops back to itself gets a simple `Counter`. This may indicate the
|
|
|
|
// program results in a tight infinite loop, but it should still compile.
|
|
|
|
let one_path_to_target = self.bcb_has_one_path_to_target(bcb);
|
|
|
|
if one_path_to_target || self.bcb_predecessors(bcb).contains(&bcb) {
|
2023-09-19 10:34:52 +00:00
|
|
|
let counter_kind = self.coverage_counters.make_counter();
|
2020-10-30 23:09:05 +00:00
|
|
|
if one_path_to_target {
|
2020-10-22 21:30:03 +00:00
|
|
|
debug!(
|
2023-09-19 10:29:47 +00:00
|
|
|
"{}{:?} gets a new counter: {:?}",
|
2020-10-22 21:30:03 +00:00
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
bcb,
|
2023-09-19 10:29:47 +00:00
|
|
|
counter_kind,
|
2020-10-22 21:30:03 +00:00
|
|
|
);
|
|
|
|
} else {
|
2020-10-30 23:09:05 +00:00
|
|
|
debug!(
|
|
|
|
"{}{:?} has itself as its own predecessor. It can't be part of its own \
|
2023-09-19 10:29:47 +00:00
|
|
|
Expression sum, so it will get its own new counter: {:?}. (Note, the compiled \
|
2020-10-30 23:09:05 +00:00
|
|
|
code will generate an infinite loop.)",
|
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
bcb,
|
2023-09-19 10:29:47 +00:00
|
|
|
counter_kind,
|
2020-10-30 23:09:05 +00:00
|
|
|
);
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
2023-06-29 06:50:52 +00:00
|
|
|
return self.coverage_counters.set_bcb_counter(bcb, counter_kind);
|
2020-10-30 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A BCB with multiple incoming edges can compute its count by `Expression`, summing up the
|
|
|
|
// counters and/or expressions of its incoming edges. This will recursively get or create
|
|
|
|
// counters for those incoming edges first, then call `make_expression()` to sum them up,
|
|
|
|
// with additional intermediate expressions as needed.
|
2022-06-03 16:42:42 +00:00
|
|
|
let mut predecessors = self.bcb_predecessors(bcb).to_owned().into_iter();
|
2020-10-30 23:09:05 +00:00
|
|
|
debug!(
|
|
|
|
"{}{:?} has multiple incoming edges and will get an expression that sums them up...",
|
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
bcb,
|
|
|
|
);
|
|
|
|
let first_edge_counter_operand = self.recursive_get_or_make_edge_counter_operand(
|
|
|
|
predecessors.next().unwrap(),
|
|
|
|
bcb,
|
|
|
|
debug_indent_level + 1,
|
|
|
|
)?;
|
|
|
|
let mut some_sumup_edge_counter_operand = None;
|
|
|
|
for predecessor in predecessors {
|
|
|
|
let edge_counter_operand = self.recursive_get_or_make_edge_counter_operand(
|
|
|
|
predecessor,
|
|
|
|
bcb,
|
|
|
|
debug_indent_level + 1,
|
|
|
|
)?;
|
|
|
|
if let Some(sumup_edge_counter_operand) =
|
|
|
|
some_sumup_edge_counter_operand.replace(edge_counter_operand)
|
|
|
|
{
|
|
|
|
let intermediate_expression = self.coverage_counters.make_expression(
|
|
|
|
sumup_edge_counter_operand,
|
|
|
|
Op::Add,
|
|
|
|
edge_counter_operand,
|
|
|
|
);
|
|
|
|
debug!(
|
2023-09-19 10:29:47 +00:00
|
|
|
"{}new intermediate expression: {:?}",
|
2020-10-30 23:09:05 +00:00
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
2023-09-19 10:29:47 +00:00
|
|
|
intermediate_expression
|
2020-10-30 23:09:05 +00:00
|
|
|
);
|
2023-06-26 12:28:48 +00:00
|
|
|
let intermediate_expression_operand = intermediate_expression.as_operand();
|
2023-06-29 04:25:28 +00:00
|
|
|
self.coverage_counters.intermediate_expressions.push(intermediate_expression);
|
2020-10-30 23:09:05 +00:00
|
|
|
some_sumup_edge_counter_operand.replace(intermediate_expression_operand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let counter_kind = self.coverage_counters.make_expression(
|
|
|
|
first_edge_counter_operand,
|
|
|
|
Op::Add,
|
|
|
|
some_sumup_edge_counter_operand.unwrap(),
|
|
|
|
);
|
|
|
|
debug!(
|
2023-09-19 10:29:47 +00:00
|
|
|
"{}{:?} gets a new counter (sum of predecessor counters): {:?}",
|
2020-10-30 23:09:05 +00:00
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
bcb,
|
2023-09-19 10:29:47 +00:00
|
|
|
counter_kind
|
2020-10-30 23:09:05 +00:00
|
|
|
);
|
2023-06-29 06:50:52 +00:00
|
|
|
self.coverage_counters.set_bcb_counter(bcb, counter_kind)
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_or_make_edge_counter_operand(
|
|
|
|
&mut self,
|
|
|
|
from_bcb: BasicCoverageBlock,
|
|
|
|
to_bcb: BasicCoverageBlock,
|
2023-06-26 12:28:48 +00:00
|
|
|
) -> Result<Operand, Error> {
|
2023-06-29 04:25:28 +00:00
|
|
|
self.recursive_get_or_make_edge_counter_operand(from_bcb, to_bcb, 1)
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recursive_get_or_make_edge_counter_operand(
|
|
|
|
&mut self,
|
|
|
|
from_bcb: BasicCoverageBlock,
|
|
|
|
to_bcb: BasicCoverageBlock,
|
|
|
|
debug_indent_level: usize,
|
2023-06-26 12:28:48 +00:00
|
|
|
) -> Result<Operand, Error> {
|
2020-10-30 23:09:05 +00:00
|
|
|
// If the source BCB has only one successor (assumed to be the given target), an edge
|
|
|
|
// counter is unnecessary. Just get or make a counter for the source BCB.
|
|
|
|
let successors = self.bcb_successors(from_bcb).iter();
|
|
|
|
if successors.len() == 1 {
|
2023-06-29 04:25:28 +00:00
|
|
|
return self.recursive_get_or_make_counter_operand(from_bcb, debug_indent_level + 1);
|
2020-10-30 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the edge already has a counter, return it.
|
2023-06-29 06:50:52 +00:00
|
|
|
if let Some(counter_kind) =
|
|
|
|
self.coverage_counters.bcb_edge_counters.get(&(from_bcb, to_bcb))
|
|
|
|
{
|
2020-10-30 23:09:05 +00:00
|
|
|
debug!(
|
2023-09-19 10:29:47 +00:00
|
|
|
"{}Edge {:?}->{:?} already has a counter: {:?}",
|
2020-10-30 23:09:05 +00:00
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
from_bcb,
|
|
|
|
to_bcb,
|
2023-09-19 10:29:47 +00:00
|
|
|
counter_kind
|
2020-10-30 23:09:05 +00:00
|
|
|
);
|
2023-06-26 12:28:48 +00:00
|
|
|
return Ok(counter_kind.as_operand());
|
2020-10-30 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new counter to count this edge.
|
2023-09-19 10:34:52 +00:00
|
|
|
let counter_kind = self.coverage_counters.make_counter();
|
2020-10-30 23:09:05 +00:00
|
|
|
debug!(
|
2023-09-19 10:29:47 +00:00
|
|
|
"{}Edge {:?}->{:?} gets a new counter: {:?}",
|
2020-10-30 23:09:05 +00:00
|
|
|
NESTED_INDENT.repeat(debug_indent_level),
|
|
|
|
from_bcb,
|
|
|
|
to_bcb,
|
2023-09-19 10:29:47 +00:00
|
|
|
counter_kind
|
2020-10-30 23:09:05 +00:00
|
|
|
);
|
2023-06-29 06:50:52 +00:00
|
|
|
self.coverage_counters.set_bcb_edge_counter(from_bcb, to_bcb, counter_kind)
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 23:09:05 +00:00
|
|
|
/// Select a branch for the expression, either the recommended `reloop_branch`, or if none was
|
|
|
|
/// found, select any branch.
|
2020-10-22 21:30:03 +00:00
|
|
|
fn choose_preferred_expression_branch(
|
|
|
|
&self,
|
|
|
|
traversal: &TraverseCoverageGraphWithLoops,
|
2020-12-30 11:59:07 +00:00
|
|
|
branches: &[BcbBranch],
|
2020-10-22 21:30:03 +00:00
|
|
|
) -> BcbBranch {
|
2023-10-12 06:41:11 +00:00
|
|
|
let good_reloop_branch = self.find_good_reloop_branch(traversal, &branches);
|
|
|
|
if let Some(reloop_branch) = good_reloop_branch {
|
|
|
|
assert!(self.branch_has_no_counter(&reloop_branch));
|
|
|
|
debug!("Selecting reloop branch {reloop_branch:?} to get an expression");
|
|
|
|
reloop_branch
|
2020-10-22 21:30:03 +00:00
|
|
|
} else {
|
2023-06-29 06:50:52 +00:00
|
|
|
let &branch_without_counter =
|
|
|
|
branches.iter().find(|&branch| self.branch_has_no_counter(branch)).expect(
|
2020-10-22 21:30:03 +00:00
|
|
|
"needs_branch_counters was `true` so there should be at least one \
|
|
|
|
branch",
|
|
|
|
);
|
|
|
|
debug!(
|
|
|
|
"Selecting any branch={:?} that still needs a counter, to get the \
|
|
|
|
`Expression` because there was no `reloop_branch`, or it already had a \
|
|
|
|
counter",
|
|
|
|
branch_without_counter
|
|
|
|
);
|
|
|
|
branch_without_counter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 06:41:11 +00:00
|
|
|
/// Tries to find a branch that leads back to the top of a loop, and that
|
|
|
|
/// doesn't already have a counter. Such branches are good candidates to
|
|
|
|
/// be given an expression (instead of a physical counter), because they
|
|
|
|
/// will tend to be executed more times than a loop-exit branch.
|
|
|
|
fn find_good_reloop_branch(
|
2020-10-22 21:30:03 +00:00
|
|
|
&self,
|
|
|
|
traversal: &TraverseCoverageGraphWithLoops,
|
2020-12-30 11:59:07 +00:00
|
|
|
branches: &[BcbBranch],
|
2020-10-22 21:30:03 +00:00
|
|
|
) -> Option<BcbBranch> {
|
2023-10-12 06:41:11 +00:00
|
|
|
// Consider each loop on the current traversal context stack, top-down.
|
|
|
|
for reloop_bcbs in traversal.reloop_bcbs_per_loop() {
|
|
|
|
let mut all_branches_exit_this_loop = true;
|
|
|
|
|
|
|
|
// Try to find a branch that doesn't exit this loop and doesn't
|
|
|
|
// already have a counter.
|
|
|
|
for &branch in branches {
|
|
|
|
// A branch is a reloop branch if it dominates any BCB that has
|
|
|
|
// an edge back to the loop header. (Other branches are exits.)
|
|
|
|
let is_reloop_branch = reloop_bcbs.iter().any(|&reloop_bcb| {
|
|
|
|
self.basic_coverage_blocks.dominates(branch.target_bcb, reloop_bcb)
|
|
|
|
});
|
|
|
|
|
|
|
|
if is_reloop_branch {
|
|
|
|
all_branches_exit_this_loop = false;
|
|
|
|
if self.branch_has_no_counter(&branch) {
|
|
|
|
// We found a good branch to be given an expression.
|
|
|
|
return Some(branch);
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
2023-10-12 06:41:11 +00:00
|
|
|
// Keep looking for another reloop branch without a counter.
|
|
|
|
} else {
|
|
|
|
// This branch exits the loop.
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 06:41:11 +00:00
|
|
|
|
|
|
|
if !all_branches_exit_this_loop {
|
|
|
|
// We found one or more reloop branches, but all of them already
|
|
|
|
// have counters. Let the caller choose one of the exit branches.
|
|
|
|
debug!("All reloop branches had counters; skip checking the other loops");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All of the branches exit this loop, so keep looking for a good
|
|
|
|
// reloop branch for one of the outer loops.
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
2023-10-12 06:41:11 +00:00
|
|
|
|
|
|
|
None
|
2020-10-22 21:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-06-03 16:42:42 +00:00
|
|
|
fn bcb_predecessors(&self, bcb: BasicCoverageBlock) -> &[BasicCoverageBlock] {
|
2020-10-22 21:30:03 +00:00
|
|
|
&self.basic_coverage_blocks.predecessors[bcb]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-06-03 16:42:42 +00:00
|
|
|
fn bcb_successors(&self, bcb: BasicCoverageBlock) -> &[BasicCoverageBlock] {
|
2020-10-22 21:30:03 +00:00
|
|
|
&self.basic_coverage_blocks.successors[bcb]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bcb_branches(&self, from_bcb: BasicCoverageBlock) -> Vec<BcbBranch> {
|
|
|
|
self.bcb_successors(from_bcb)
|
|
|
|
.iter()
|
|
|
|
.map(|&to_bcb| BcbBranch::from_to(from_bcb, to_bcb, &self.basic_coverage_blocks))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bcb_needs_branch_counters(&self, bcb: BasicCoverageBlock) -> bool {
|
2023-06-29 06:50:52 +00:00
|
|
|
let branch_needs_a_counter = |branch: &BcbBranch| self.branch_has_no_counter(branch);
|
2020-10-22 21:30:03 +00:00
|
|
|
let branches = self.bcb_branches(bcb);
|
|
|
|
branches.len() > 1 && branches.iter().any(branch_needs_a_counter)
|
|
|
|
}
|
|
|
|
|
2023-06-29 06:50:52 +00:00
|
|
|
fn branch_has_no_counter(&self, branch: &BcbBranch) -> bool {
|
|
|
|
self.branch_counter(branch).is_none()
|
|
|
|
}
|
|
|
|
|
2023-07-08 03:43:29 +00:00
|
|
|
fn branch_counter(&self, branch: &BcbBranch) -> Option<&BcbCounter> {
|
2023-06-29 06:50:52 +00:00
|
|
|
let to_bcb = branch.target_bcb;
|
|
|
|
if let Some(from_bcb) = branch.edge_from_bcb {
|
|
|
|
self.coverage_counters.bcb_edge_counters.get(&(from_bcb, to_bcb))
|
|
|
|
} else {
|
|
|
|
self.coverage_counters.bcb_counters[to_bcb].as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:30:03 +00:00
|
|
|
/// Returns true if the BasicCoverageBlock has zero or one incoming edge. (If zero, it should be
|
|
|
|
/// the entry point for the function.)
|
|
|
|
#[inline]
|
|
|
|
fn bcb_has_one_path_to_target(&self, bcb: BasicCoverageBlock) -> bool {
|
|
|
|
self.bcb_predecessors(bcb).len() <= 1
|
|
|
|
}
|
|
|
|
}
|