Skip to content

Commit 8d4ba90

Browse files
committed
simplify expressions of branches
1 parent 5dd986b commit 8d4ba90

File tree

7 files changed

+294
-306
lines changed

7 files changed

+294
-306
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ impl<'tcx> FunctionCoverage<'tcx> {
159159
// can have those operands replaced with `Operand::Zero`.
160160
let mut zero_expressions = FxIndexSet::default();
161161

162+
// If an operand refers to an expression that is always zero, then
163+
// that operand can be replaced with `Operand::Zero`.
164+
let maybe_set_operand_to_zero =
165+
|zero_expressions: &FxIndexSet<ExpressionId>, operand: &mut Operand| match &*operand {
166+
Operand::Expression(id) if zero_expressions.contains(id) => {
167+
*operand = Operand::Zero;
168+
}
169+
_ => (),
170+
};
171+
162172
// For each expression, perform simplifications based on lower-numbered
163173
// expressions, and then update the set of always-zero expressions if
164174
// necessary.
@@ -172,16 +182,8 @@ impl<'tcx> FunctionCoverage<'tcx> {
172182
continue;
173183
};
174184

175-
// If an operand refers to an expression that is always zero, then
176-
// that operand can be replaced with `Operand::Zero`.
177-
let maybe_set_operand_to_zero = |operand: &mut Operand| match &*operand {
178-
Operand::Expression(id) if zero_expressions.contains(id) => {
179-
*operand = Operand::Zero;
180-
}
181-
_ => (),
182-
};
183-
maybe_set_operand_to_zero(&mut expression.lhs);
184-
maybe_set_operand_to_zero(&mut expression.rhs);
185+
maybe_set_operand_to_zero(&zero_expressions, &mut expression.lhs);
186+
maybe_set_operand_to_zero(&zero_expressions, &mut expression.rhs);
185187

186188
// Coverage counter values cannot be negative, so if an expression
187189
// involves subtraction from zero, assume that its RHS must also be zero.
@@ -196,6 +198,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
196198
zero_expressions.insert(id);
197199
}
198200
}
201+
202+
// Do the same simplification for each side of a branch region.
203+
for branch in &mut self.branches {
204+
maybe_set_operand_to_zero(&zero_expressions, &mut branch.true_);
205+
maybe_set_operand_to_zero(&zero_expressions, &mut branch.false_);
206+
}
199207
}
200208

201209
/// Return the source hash, generated from the HIR node structure, and used to indicate whether

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
231231
&mut debug_used_expressions,
232232
);
233233

234+
// TODO: if self.tcx.sess.instrument_coverage_branch() {
234235
self.inject_branch_counters(std::mem::take(branches));
236+
// }
235237

236238
////////////////////////////////////////////////////
237239
// For any remaining `BasicCoverageBlock` counters (that were not associated with

tests/coverage-map/status-quo/async2.cov-map

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,16 @@ Number of file 0 mappings: 1
128128
- Code(Counter(0)) at (prev + 35, 1) to (start + 7, 2)
129129

130130
Function name: async2::non_async_func
131-
Raw bytes (35): 0x[01, 01, 02, 00, 00, 05, 00, 05, 01, 09, 01, 03, 09, 20, 05, 02, 03, 08, 00, 09, 05, 00, 0a, 02, 06, 00, 02, 06, 00, 07, 07, 01, 01, 00, 02]
131+
Raw bytes (33): 0x[01, 01, 01, 05, 00, 05, 01, 09, 01, 03, 09, 20, 05, 00, 03, 08, 00, 09, 05, 00, 0a, 02, 06, 00, 02, 06, 00, 07, 03, 01, 01, 00, 02]
132132
Number of files: 1
133133
- file 0 => global file 1
134-
Number of expressions: 2
135-
- expression 0 operands: lhs = Zero, rhs = Zero
136-
- expression 1 operands: lhs = Counter(1), rhs = Zero
134+
Number of expressions: 1
135+
- expression 0 operands: lhs = Counter(1), rhs = Zero
137136
Number of file 0 mappings: 5
138137
- Code(Counter(0)) at (prev + 9, 1) to (start + 3, 9)
139-
- Branch { true: Counter(1), false: Expression(0, Sub) } at (prev + 3, 8) to (start + 0, 9)
140-
false = (Zero - Zero)
138+
- Branch { true: Counter(1), false: Zero } at (prev + 3, 8) to (start + 0, 9)
141139
- Code(Counter(1)) at (prev + 0, 10) to (start + 2, 6)
142140
- Code(Zero) at (prev + 2, 6) to (start + 0, 7)
143-
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
141+
- Code(Expression(0, Add)) at (prev + 1, 1) to (start + 0, 2)
144142
= (c1 + Zero)
145143

0 commit comments

Comments
 (0)