Auto merge of #120268 - DianQK:otherwise_is_last_variant_switchs, r=oli-obk
Replace the default branch with an unreachable branch If it is the last variant Fixes #119520. Fixes #110097. LLVM currently has limited ability to eliminate dead branches in switches, even with the patch of https://github.com/llvm/llvm-project/issues/73446. The main reasons are as follows: - Additional costs are required to calculate the range of values, and there exist many scenarios that cannot be analyzed accurately. - Matching values by bitwise calculation cannot handle odd branches, nor can it handle values like `-1, 0, 1`. See [SimplifyCFG.cpp#L5424](https://github.com/llvm/llvm-project/blob/llvmorg-17.0.6/llvm/lib/Transforms/Utils/SimplifyCFG.cpp#L5424) and https://llvm.godbolt.org/z/qYMqhvMa8 - The current range information is continuous, even if the metadata for the range is submitted. See [ConstantRange.cpp#L1869-L1870](https://github.com/llvm/llvm-project/blob/llvmorg-17.0.6/llvm/lib/IR/ConstantRange.cpp#L1869-L1870). - The metadata of the range may be lost in passes such as SROA. See https://rust.godbolt.org/z/e7f87vKMK. Although we can make improvements, I think it would be more appropriate to put this issue to rustc first. After all, we can easily know the possible values. Note that we've currently found a slow compilation problem in the presence of unreachable branches. See https://github.com/llvm/llvm-project/issues/78578. r? compiler
This commit is contained in:
commit
14fbc3c005
38 changed files with 1407 additions and 166 deletions
|
@ -11,6 +11,8 @@ pub struct MirPatch<'tcx> {
|
|||
resume_block: Option<BasicBlock>,
|
||||
// Only for unreachable in cleanup path.
|
||||
unreachable_cleanup_block: Option<BasicBlock>,
|
||||
// Only for unreachable not in cleanup path.
|
||||
unreachable_no_cleanup_block: Option<BasicBlock>,
|
||||
// Cached block for UnwindTerminate (with reason)
|
||||
terminate_block: Option<(BasicBlock, UnwindTerminateReason)>,
|
||||
body_span: Span,
|
||||
|
@ -27,6 +29,7 @@ impl<'tcx> MirPatch<'tcx> {
|
|||
next_local: body.local_decls.len(),
|
||||
resume_block: None,
|
||||
unreachable_cleanup_block: None,
|
||||
unreachable_no_cleanup_block: None,
|
||||
terminate_block: None,
|
||||
body_span: body.span,
|
||||
};
|
||||
|
@ -43,9 +46,12 @@ impl<'tcx> MirPatch<'tcx> {
|
|||
// Check if we already have an unreachable block
|
||||
if matches!(block.terminator().kind, TerminatorKind::Unreachable)
|
||||
&& block.statements.is_empty()
|
||||
&& block.is_cleanup
|
||||
{
|
||||
result.unreachable_cleanup_block = Some(bb);
|
||||
if block.is_cleanup {
|
||||
result.unreachable_cleanup_block = Some(bb);
|
||||
} else {
|
||||
result.unreachable_no_cleanup_block = Some(bb);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -95,6 +101,23 @@ impl<'tcx> MirPatch<'tcx> {
|
|||
bb
|
||||
}
|
||||
|
||||
pub fn unreachable_no_cleanup_block(&mut self) -> BasicBlock {
|
||||
if let Some(bb) = self.unreachable_no_cleanup_block {
|
||||
return bb;
|
||||
}
|
||||
|
||||
let bb = self.new_block(BasicBlockData {
|
||||
statements: vec![],
|
||||
terminator: Some(Terminator {
|
||||
source_info: SourceInfo::outermost(self.body_span),
|
||||
kind: TerminatorKind::Unreachable,
|
||||
}),
|
||||
is_cleanup: false,
|
||||
});
|
||||
self.unreachable_no_cleanup_block = Some(bb);
|
||||
bb
|
||||
}
|
||||
|
||||
pub fn terminate_block(&mut self, reason: UnwindTerminateReason) -> BasicBlock {
|
||||
if let Some((cached_bb, cached_reason)) = self.terminate_block
|
||||
&& reason == cached_reason
|
||||
|
|
|
@ -74,6 +74,17 @@ impl SwitchTargets {
|
|||
pub fn target_for_value(&self, value: u128) -> BasicBlock {
|
||||
self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
|
||||
}
|
||||
|
||||
/// Adds a new target to the switch. But You cannot add an already present value.
|
||||
#[inline]
|
||||
pub fn add_target(&mut self, value: u128, bb: BasicBlock) {
|
||||
let value = Pu128(value);
|
||||
if self.values.contains(&value) {
|
||||
bug!("target value {:?} already present", value);
|
||||
}
|
||||
self.values.push(value);
|
||||
self.targets.insert(self.targets.len() - 1, bb);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SwitchTargetsIter<'a> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue