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:
Jakob Stoklund Olesen
2017-04-25 15:50:59 -07:00
parent a332c3d024
commit c36aedfd03
5 changed files with 10 additions and 18 deletions

View File

@@ -64,7 +64,7 @@ pub fn relax_branches(func: &mut Function, isa: &TargetIsa) {
} }
while let Some(inst) = pos.next_inst() { 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); let size = encinfo.bytes(enc);
// See if this might be a branch that is out of range. // See if this might be a branch that is out of range.

View File

@@ -148,6 +148,11 @@ impl<K, V> EntityMap<K, V>
} }
&mut self.elems[k.index()] &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`. /// Immutable indexing into an `EntityMap`.

View File

@@ -668,11 +668,7 @@ impl<'a> Verifier<'a> {
/// instruction (if any) matches how the ISA would encode it. /// instruction (if any) matches how the ISA would encode it.
fn verify_encoding(&self, inst: Inst) -> Result { fn verify_encoding(&self, inst: Inst) -> Result {
if let Some(isa) = self.isa { if let Some(isa) = self.isa {
let encoding = self.func let encoding = self.func.encodings.get_or_default(inst);
.encodings
.get(inst)
.cloned()
.unwrap_or_default();
if encoding.is_legal() { if encoding.is_legal() {
let verify_encoding = let verify_encoding =
isa.encode(&self.func.dfg, isa.encode(&self.func.dfg,

View File

@@ -189,13 +189,7 @@ fn write_instruction(w: &mut Write,
if !func.locations.is_empty() { if !func.locations.is_empty() {
let regs = isa.register_info(); let regs = isa.register_info();
for &r in func.dfg.inst_results(inst) { for &r in func.dfg.inst_results(inst) {
write!(s, write!(s, ",{}", func.locations.get_or_default(r).display(&regs))?
",{}",
func.locations
.get(r)
.cloned()
.unwrap_or_default()
.display(&regs))?
} }
} }
write!(s, "]")?; write!(s, "]")?;

View File

@@ -106,10 +106,7 @@ impl SubTest for TestBinEmit {
// Give an encoding to any instruction that doesn't already have one. // Give an encoding to any instruction that doesn't already have one.
for ebb in func.layout.ebbs() { for ebb in func.layout.ebbs() {
for inst in func.layout.ebb_insts(ebb) { for inst in func.layout.ebb_insts(ebb) {
if !func.encodings if !func.encodings.get_or_default(inst).is_legal() {
.get(inst)
.map(|e| e.is_legal())
.unwrap_or(false) {
if let Ok(enc) = isa.encode(&func.dfg, if let Ok(enc) = isa.encode(&func.dfg,
&func.dfg[inst], &func.dfg[inst],
func.dfg.ctrl_typevar(inst)) { func.dfg.ctrl_typevar(inst)) {
@@ -157,7 +154,7 @@ impl SubTest for TestBinEmit {
ebb); ebb);
for inst in func.layout.ebb_insts(ebb) { for inst in func.layout.ebb_insts(ebb) {
sink.text.clear(); 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. // Send legal encodings into the emitter.
if enc.is_legal() { if enc.is_legal() {