Fix all clippy warnings (#564)
* Fix all clippy warnings * Revert usage of inclusive ranges * Remove redundant function argument * Revert use of unavailable pointer methods * Introduce ContiguousCaseRange
This commit is contained in:
committed by
Dan Gohman
parent
586a8835e9
commit
b288c6001a
@@ -8,7 +8,7 @@ type EntryIndex = u64;
|
||||
|
||||
/// Unlike with `br_table`, `Switch` cases may be sparse or non-0-based.
|
||||
/// They emit efficient code using branches, jump tables, or a combination of both.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Switch {
|
||||
cases: HashMap<EntryIndex, Ebb>,
|
||||
}
|
||||
@@ -16,7 +16,7 @@ pub struct Switch {
|
||||
impl Switch {
|
||||
/// Create a new empty switch
|
||||
pub fn new() -> Self {
|
||||
Switch {
|
||||
Self {
|
||||
cases: HashMap::new(),
|
||||
}
|
||||
}
|
||||
@@ -31,23 +31,23 @@ impl Switch {
|
||||
);
|
||||
}
|
||||
|
||||
fn collect_contiguous_case_ranges(self) -> Vec<(EntryIndex, Vec<Ebb>)> {
|
||||
fn collect_contiguous_case_ranges(self) -> Vec<ContiguousCaseRange> {
|
||||
debug!("build_contiguous_case_ranges before: {:#?}", self.cases);
|
||||
let mut cases = self.cases.into_iter().collect::<Vec<(_, _)>>();
|
||||
cases.sort_by_key(|&(index, _)| index);
|
||||
|
||||
let mut contiguous_case_ranges: Vec<(EntryIndex, Vec<Ebb>)> = vec![];
|
||||
let mut contiguous_case_ranges: Vec<ContiguousCaseRange> = vec![];
|
||||
let mut last_index = None;
|
||||
for (index, ebb) in cases {
|
||||
match last_index {
|
||||
None => contiguous_case_ranges.push((index, vec![])),
|
||||
None => contiguous_case_ranges.push(ContiguousCaseRange::new(index)),
|
||||
Some(last_index) => {
|
||||
if index > last_index + 1 {
|
||||
contiguous_case_ranges.push((index, vec![]));
|
||||
contiguous_case_ranges.push(ContiguousCaseRange::new(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
contiguous_case_ranges.last_mut().unwrap().1.push(ebb);
|
||||
contiguous_case_ranges.last_mut().unwrap().ebbs.push(ebb);
|
||||
last_index = Some(index);
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ impl Switch {
|
||||
bx: &mut FunctionBuilder,
|
||||
val: Value,
|
||||
otherwise: Ebb,
|
||||
contiguous_case_ranges: Vec<(EntryIndex, Vec<Ebb>)>,
|
||||
contiguous_case_ranges: Vec<ContiguousCaseRange>,
|
||||
) -> Vec<(EntryIndex, Ebb, Vec<Ebb>)> {
|
||||
let mut cases_and_jt_ebbs = Vec::new();
|
||||
|
||||
@@ -79,7 +79,7 @@ impl Switch {
|
||||
return cases_and_jt_ebbs;
|
||||
}
|
||||
|
||||
let mut stack: Vec<(Option<Ebb>, Vec<(EntryIndex, Vec<Ebb>)>)> = Vec::new();
|
||||
let mut stack: Vec<(Option<Ebb>, Vec<ContiguousCaseRange>)> = Vec::new();
|
||||
stack.push((None, contiguous_case_ranges));
|
||||
|
||||
while let Some((ebb, contiguous_case_ranges)) = stack.pop() {
|
||||
@@ -103,9 +103,11 @@ impl Switch {
|
||||
let left_ebb = bx.create_ebb();
|
||||
let right_ebb = bx.create_ebb();
|
||||
|
||||
let should_take_right_side =
|
||||
bx.ins()
|
||||
.icmp_imm(IntCC::UnsignedGreaterThanOrEqual, val, right[0].0 as i64);
|
||||
let should_take_right_side = bx.ins().icmp_imm(
|
||||
IntCC::UnsignedGreaterThanOrEqual,
|
||||
val,
|
||||
right[0].first_index as i64,
|
||||
);
|
||||
bx.ins().brnz(should_take_right_side, right_ebb, &[]);
|
||||
bx.ins().jump(left_ebb, &[]);
|
||||
|
||||
@@ -121,10 +123,10 @@ impl Switch {
|
||||
bx: &mut FunctionBuilder,
|
||||
val: Value,
|
||||
otherwise: Ebb,
|
||||
contiguous_case_ranges: Vec<(EntryIndex, Vec<Ebb>)>,
|
||||
contiguous_case_ranges: Vec<ContiguousCaseRange>,
|
||||
cases_and_jt_ebbs: &mut Vec<(EntryIndex, Ebb, Vec<Ebb>)>,
|
||||
) {
|
||||
for (first_index, ebbs) in contiguous_case_ranges.into_iter().rev() {
|
||||
for ContiguousCaseRange { first_index, ebbs } in contiguous_case_ranges.into_iter().rev() {
|
||||
if ebbs.len() == 1 {
|
||||
let is_good_val = bx.ins().icmp_imm(IntCC::Equal, val, first_index as i64);
|
||||
bx.ins().brnz(is_good_val, ebbs[0], &[]);
|
||||
@@ -180,6 +182,21 @@ impl Switch {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ContiguousCaseRange {
|
||||
first_index: EntryIndex,
|
||||
ebbs: Vec<Ebb>,
|
||||
}
|
||||
|
||||
impl ContiguousCaseRange {
|
||||
fn new(first_index: EntryIndex) -> Self {
|
||||
Self {
|
||||
first_index,
|
||||
ebbs: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user