Browse Source

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.
pull/3/head
Jakob Stoklund Olesen 8 years ago
parent
commit
c36aedfd03
  1. 2
      lib/cretonne/src/binemit/relaxation.rs
  2. 5
      lib/cretonne/src/entity_map.rs
  3. 6
      lib/cretonne/src/verifier/mod.rs
  4. 8
      lib/cretonne/src/write.rs
  5. 7
      src/filetest/binemit.rs

2
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.

5
lib/cretonne/src/entity_map.rs

@ -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`.

6
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,

8
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(&regs))?
write!(s, ",{}", func.locations.get_or_default(r).display(&regs))?
}
}
write!(s, "]")?;

7
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() {

Loading…
Cancel
Save