diff --git a/lib/cretonne/src/binemit/relaxation.rs b/lib/cretonne/src/binemit/relaxation.rs index 179540dad0..74158a61e4 100644 --- a/lib/cretonne/src/binemit/relaxation.rs +++ b/lib/cretonne/src/binemit/relaxation.rs @@ -64,7 +64,7 @@ pub fn relax_branches(func: &mut Function, isa: &TargetIsa) { } while let Some(inst) = pos.next_inst() { - let enc = func.encodings.get(inst).cloned().unwrap_or_default(); + let enc = func.encodings.get_or_default(inst); let size = encinfo.bytes(enc); // See if this might be a branch that is out of range. diff --git a/lib/cretonne/src/entity_map.rs b/lib/cretonne/src/entity_map.rs index f9ac258f92..cee4215a4e 100644 --- a/lib/cretonne/src/entity_map.rs +++ b/lib/cretonne/src/entity_map.rs @@ -148,6 +148,11 @@ impl EntityMap } &mut self.elems[k.index()] } + + /// Get the element at `k` or the default value if `k` is out of range. + pub fn get_or_default(&self, k: K) -> V { + self.elems.get(k.index()).cloned().unwrap_or_default() + } } /// Immutable indexing into an `EntityMap`. diff --git a/lib/cretonne/src/verifier/mod.rs b/lib/cretonne/src/verifier/mod.rs index 6bf9b155c6..e3d5607c99 100644 --- a/lib/cretonne/src/verifier/mod.rs +++ b/lib/cretonne/src/verifier/mod.rs @@ -668,11 +668,7 @@ impl<'a> Verifier<'a> { /// instruction (if any) matches how the ISA would encode it. fn verify_encoding(&self, inst: Inst) -> Result { if let Some(isa) = self.isa { - let encoding = self.func - .encodings - .get(inst) - .cloned() - .unwrap_or_default(); + let encoding = self.func.encodings.get_or_default(inst); if encoding.is_legal() { let verify_encoding = isa.encode(&self.func.dfg, diff --git a/lib/cretonne/src/write.rs b/lib/cretonne/src/write.rs index fcfb843c0c..5d46ccb624 100644 --- a/lib/cretonne/src/write.rs +++ b/lib/cretonne/src/write.rs @@ -189,13 +189,7 @@ fn write_instruction(w: &mut Write, if !func.locations.is_empty() { let regs = isa.register_info(); for &r in func.dfg.inst_results(inst) { - write!(s, - ",{}", - func.locations - .get(r) - .cloned() - .unwrap_or_default() - .display(®s))? + write!(s, ",{}", func.locations.get_or_default(r).display(®s))? } } write!(s, "]")?; diff --git a/src/filetest/binemit.rs b/src/filetest/binemit.rs index 449e0887e6..2f695bff1e 100644 --- a/src/filetest/binemit.rs +++ b/src/filetest/binemit.rs @@ -106,10 +106,7 @@ impl SubTest for TestBinEmit { // Give an encoding to any instruction that doesn't already have one. for ebb in func.layout.ebbs() { for inst in func.layout.ebb_insts(ebb) { - if !func.encodings - .get(inst) - .map(|e| e.is_legal()) - .unwrap_or(false) { + if !func.encodings.get_or_default(inst).is_legal() { if let Ok(enc) = isa.encode(&func.dfg, &func.dfg[inst], func.dfg.ctrl_typevar(inst)) { @@ -157,7 +154,7 @@ impl SubTest for TestBinEmit { ebb); for inst in func.layout.ebb_insts(ebb) { sink.text.clear(); - let enc = func.encodings.get(inst).cloned().unwrap_or_default(); + let enc = func.encodings.get_or_default(inst); // Send legal encodings into the emitter. if enc.is_legal() {