Reimplement simplify_cfg for SwitchInt
First example of optimisation that applies to many more cases than originally.
This commit is contained in:
parent
92c56f607b
commit
64182a587c
1 changed files with 18 additions and 14 deletions
|
@ -30,26 +30,30 @@ impl<'l, 'tcx> MirPass<'tcx> for SimplifyBranches<'l> {
|
||||||
for block in mir.basic_blocks_mut() {
|
for block in mir.basic_blocks_mut() {
|
||||||
let terminator = block.terminator_mut();
|
let terminator = block.terminator_mut();
|
||||||
terminator.kind = match terminator.kind {
|
terminator.kind = match terminator.kind {
|
||||||
// TerminatorKind::If { ref targets, cond: Operand::Constant(Constant {
|
TerminatorKind::SwitchInt { discr: Operand::Constant(Constant {
|
||||||
// literal: Literal::Value {
|
literal: Literal::Value { ref value }, ..
|
||||||
// value: ConstVal::Bool(cond)
|
}), ref values, ref targets, .. } => {
|
||||||
// }, ..
|
if let Some(ref constint) = value.to_const_int() {
|
||||||
// }) } => {
|
let (otherwise, targets) = targets.split_last().unwrap();
|
||||||
// if cond {
|
let mut ret = TerminatorKind::Goto { target: *otherwise };
|
||||||
// TerminatorKind::Goto { target: targets.0 }
|
for (v, t) in values.iter().zip(targets.iter()) {
|
||||||
// } else {
|
if v == constint {
|
||||||
// TerminatorKind::Goto { target: targets.1 }
|
ret = TerminatorKind::Goto { target: *t };
|
||||||
// }
|
break;
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
},
|
||||||
TerminatorKind::Assert { target, cond: Operand::Constant(Constant {
|
TerminatorKind::Assert { target, cond: Operand::Constant(Constant {
|
||||||
literal: Literal::Value {
|
literal: Literal::Value {
|
||||||
value: ConstVal::Bool(cond)
|
value: ConstVal::Bool(cond)
|
||||||
}, ..
|
}, ..
|
||||||
}), expected, .. } if cond == expected => {
|
}), expected, .. } if cond == expected => {
|
||||||
TerminatorKind::Goto { target: target }
|
TerminatorKind::Goto { target: target }
|
||||||
}
|
},
|
||||||
|
|
||||||
_ => continue
|
_ => continue
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue