Add an EntityMap::get_or_default() method.
This covers a common pattern for secondary entity maps: Get the value in the map or the default value for out-of-range keys.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -148,6 +148,11 @@ impl<K, V> EntityMap<K, V>
|
||||
}
|
||||
&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`.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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, "]")?;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user