Split EntityMap into entity::PrimaryMap and entity::EntityMap.

The new PrimaryMap replaces the primary EntityMap and the PrimaryEntityData
marker trait which was causing some confusion. We now have a clear
division between the two types of maps:

- PrimaryMap is used to assign entity numbers to the primary data for an
  entity.
- EntityMap is a secondary mapping adding additional info.

The split also means that the secondary EntityMap can now behave as if
all keys have a default value. This means that we can get rid of the
annoying ensure() and get_or_default() methods ther were used everywhere
instead of indexing. Just use normal indexing now; non-existent keys
will return the default value.
This commit is contained in:
Jakob Stoklund Olesen
2017-08-18 15:04:10 -07:00
parent 8599372098
commit 7e08b14cf6
30 changed files with 413 additions and 363 deletions

View File

@@ -6,8 +6,7 @@ use cretonne::ir::instructions::BranchInfo;
use cretonne::ir::function::DisplayFunction;
use cretonne::isa::TargetIsa;
use ssa::{SSABuilder, SideEffects, Block};
use cretonne::entity_map::{EntityMap, PrimaryEntityData};
use cretonne::entity::EntityRef;
use cretonne::entity::{EntityRef, EntityMap};
use std::hash::Hash;
/// Permanent structure used for translating into Cretonne IL.
@@ -38,8 +37,6 @@ struct EbbData {
user_arg_count: usize,
}
impl PrimaryEntityData for EbbData {}
struct Position {
ebb: Ebb,
basic_block: Block,
@@ -231,7 +228,7 @@ impl<'a, Variable> FunctionBuilder<'a, Variable>
pub fn create_ebb(&mut self) -> Ebb {
let ebb = self.func.dfg.make_ebb();
self.builder.ssa.declare_ebb_header_block(ebb);
*self.builder.ebbs.ensure(ebb) = EbbData {
self.builder.ebbs[ebb] = EbbData {
filled: false,
pristine: true,
user_arg_count: 0,
@@ -286,7 +283,7 @@ impl<'a, Variable> FunctionBuilder<'a, Variable>
/// In order to use a variable in a `use_var`, you need to declare its type with this method.
pub fn declare_var(&mut self, var: Variable, ty: Type) {
*self.builder.types.ensure(var) = ty;
self.builder.types[var] = ty;
}
/// Returns the Cretonne IL value corresponding to the utilization at the current program
@@ -560,7 +557,7 @@ impl<'a, Variable> FunctionBuilder<'a, Variable>
fn handle_ssa_side_effects(&mut self, side_effects: SideEffects) {
for split_ebb in side_effects.split_ebbs_created {
self.builder.ebbs.ensure(split_ebb).filled = true
self.builder.ebbs[split_ebb].filled = true
}
for modified_ebb in side_effects.instructions_added_to_ebbs {
self.builder.ebbs[modified_ebb].pristine = false

View File

@@ -9,8 +9,7 @@ use cretonne::ir::{Ebb, Value, Inst, Type, DataFlowGraph, JumpTables, Layout, Cu
InstBuilder};
use cretonne::ir::instructions::BranchInfo;
use std::hash::Hash;
use cretonne::entity_map::{EntityMap, PrimaryEntityData};
use cretonne::entity::EntityRef;
use cretonne::entity::{EntityRef, PrimaryMap, EntityMap};
use cretonne::packed_option::PackedOption;
use cretonne::packed_option::ReservedValue;
use std::u32;
@@ -41,7 +40,7 @@ pub struct SSABuilder<Variable>
variables: EntityMap<Variable, HashMap<Block, Value>>,
// Records the position of the basic blocks and the list of values used but not defined in the
// block.
blocks: EntityMap<Block, BlockData<Variable>>,
blocks: PrimaryMap<Block, BlockData<Variable>>,
// Records the basic blocks at the beginning of the `Ebb`s.
ebb_headers: EntityMap<Ebb, PackedOption<Block>>,
}
@@ -66,7 +65,6 @@ enum BlockData<Variable> {
// The block is implicitely sealed at creation.
EbbBody { predecessor: Block },
}
impl<Variable> PrimaryEntityData for BlockData<Variable> {}
impl<Variable> BlockData<Variable> {
fn add_predecessor(&mut self, pred: Block, inst: Inst) {
@@ -135,7 +133,7 @@ impl<Variable> SSABuilder<Variable>
pub fn new() -> SSABuilder<Variable> {
SSABuilder {
variables: EntityMap::new(),
blocks: EntityMap::new(),
blocks: PrimaryMap::new(),
ebb_headers: EntityMap::new(),
}
}
@@ -193,7 +191,7 @@ impl<Variable> SSABuilder<Variable>
/// The SSA value is passed as an argument because it should be created with
/// `ir::DataFlowGraph::append_result`.
pub fn def_var(&mut self, var: Variable, val: Value, block: Block) {
self.variables.ensure(var).insert(block, val);
self.variables[var].insert(block, val);
}
/// Declares a use of a variable in a given basic block. Returns the SSA value corresponding
@@ -296,7 +294,7 @@ impl<Variable> SSABuilder<Variable>
ebb: ebb,
undef_variables: Vec::new(),
}));
*self.ebb_headers.ensure(ebb) = block.into();
self.ebb_headers[ebb] = block.into();
block
}
/// Gets the header block corresponding to an Ebb, panics if the Ebb or the header block