From a661a8a9bb77cad40a8d86e224fd5089470aa3a3 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 26 Mar 2018 21:54:36 -0700 Subject: [PATCH] Factor out common ways to call `encode` from a dfg or func. --- lib/cretonne/src/ir/dfg.rs | 8 +++++++- lib/cretonne/src/ir/function.rs | 9 ++++++++- lib/cretonne/src/legalizer/mod.rs | 8 ++------ lib/cretonne/src/verifier/mod.rs | 6 +----- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/cretonne/src/ir/dfg.rs b/lib/cretonne/src/ir/dfg.rs index 344483a6d9..d23606ee13 100644 --- a/lib/cretonne/src/ir/dfg.rs +++ b/lib/cretonne/src/ir/dfg.rs @@ -1,7 +1,7 @@ //! Data flow graph tracking Instructions, Values, and EBBs. use entity::{PrimaryMap, EntityMap}; -use isa::TargetIsa; +use isa::{TargetIsa, Encoding, Legalize}; use ir; use ir::builder::ReplaceBuilder; use ir::extfunc::ExtFuncData; @@ -645,6 +645,12 @@ impl DataFlowGraph { self.value_type(self.first_result(inst)) } } + + /// Wrapper around `TargetIsa::encode` for encoding an existing instruction + /// in the `DataFlowGraph`. + pub fn encode(&self, inst: Inst, isa: &TargetIsa) -> Result { + isa.encode(&self, &self[inst], self.ctrl_typevar(inst)) + } } /// Allow immutable access to instructions via indexing. diff --git a/lib/cretonne/src/ir/function.rs b/lib/cretonne/src/ir/function.rs index 91ee7eca75..8ee332526f 100644 --- a/lib/cretonne/src/ir/function.rs +++ b/lib/cretonne/src/ir/function.rs @@ -10,7 +10,7 @@ use ir::{ExternalName, CallConv, Signature, DataFlowGraph, Layout}; use ir::{InstEncodings, ValueLocations, JumpTables, StackSlots, EbbOffsets, SourceLocs}; use ir::{Ebb, JumpTableData, JumpTable, StackSlotData, StackSlot, SigRef, ExtFuncData, FuncRef, GlobalVarData, GlobalVar, HeapData, Heap}; -use isa::{TargetIsa, EncInfo}; +use isa::{TargetIsa, EncInfo, Legalize}; use std::fmt; use write::write_function; @@ -176,6 +176,13 @@ impl Function { iter: self.layout.ebb_insts(ebb), } } + + /// Wrapper around `DataFlowGraph::encode` which assigns `inst` the resulting encoding. + pub fn update_encoding(&mut self, inst: ir::Inst, isa: &TargetIsa) -> Result<(), Legalize> { + self.dfg.encode(inst, isa).map( + |e| { self.encodings[inst] = e; }, + ) + } } /// Wrapper type capable of displaying a `Function` with correct ISA annotations. diff --git a/lib/cretonne/src/legalizer/mod.rs b/lib/cretonne/src/legalizer/mod.rs index ae40c42581..68f24ee0da 100644 --- a/lib/cretonne/src/legalizer/mod.rs +++ b/lib/cretonne/src/legalizer/mod.rs @@ -72,12 +72,8 @@ pub fn legalize_function(func: &mut ir::Function, cfg: &mut ControlFlowGraph, is split::simplify_branch_arguments(&mut pos.func.dfg, inst); } - match isa.encode( - &pos.func.dfg, - &pos.func.dfg[inst], - pos.func.dfg.ctrl_typevar(inst), - ) { - Ok(encoding) => pos.func.encodings[inst] = encoding, + match pos.func.update_encoding(inst, isa) { + Ok(()) => {} Err(action) => { // We should transform the instruction into legal equivalents. let changed = action(inst, pos.func, cfg, isa); diff --git a/lib/cretonne/src/verifier/mod.rs b/lib/cretonne/src/verifier/mod.rs index b19510a485..8b6eb5e972 100644 --- a/lib/cretonne/src/verifier/mod.rs +++ b/lib/cretonne/src/verifier/mod.rs @@ -1058,11 +1058,7 @@ impl<'a> Verifier<'a> { if let Some(text) = needs_enc { // This instruction needs an encoding, so generate an error. // Provide the ISA default encoding as a hint. - match isa.encode( - &self.func.dfg, - &self.func.dfg[inst], - self.func.dfg.ctrl_typevar(inst), - ) { + match self.func.dfg.encode(inst, isa) { Ok(enc) => { return err!( inst,