use crate::cursor::{Cursor, FuncCursor};
use crate::ir::{self, InstBuilder};
use crate::isa::TargetIsa;
pub fn expand_global_value(
inst: ir::Inst,
func: &mut ir::Function,
isa: &dyn TargetIsa,
global_value: ir::GlobalValue,
) {
crate::trace!(
"expanding global value: {:?}: {}",
inst,
func.dfg.display_inst(inst)
);
match func.global_values[global_value] {
ir::GlobalValueData::VMContext => vmctx_addr(inst, func),
ir::GlobalValueData::IAddImm {
base,
offset,
global_type,
} => iadd_imm_addr(inst, func, base, offset.into(), global_type),
ir::GlobalValueData::Load {
base,
offset,
global_type,
readonly,
} => load_addr(inst, func, base, offset, global_type, readonly, isa),
ir::GlobalValueData::Symbol { tls, .. } => symbol(inst, func, global_value, isa, tls),
ir::GlobalValueData::DynScaleTargetConst { vector_type } => {
const_vector_scale(inst, func, vector_type, isa)
}
}
}
fn const_vector_scale(inst: ir::Inst, func: &mut ir::Function, ty: ir::Type, isa: &dyn TargetIsa) {
assert!(ty.bytes() <= 16);
let base_bytes = std::cmp::max(ty.bytes(), 16);
let scale = (isa.dynamic_vector_bytes(ty) / base_bytes) as i64;
assert!(scale > 0);
let pos = FuncCursor::new(func).at_inst(inst);
pos.func.dfg.replace(inst).iconst(isa.pointer_type(), scale);
}
fn vmctx_addr(inst: ir::Inst, func: &mut ir::Function) {
let vmctx = func
.special_param(ir::ArgumentPurpose::VMContext)
.expect("Missing vmctx parameter");
let result = func.dfg.first_result(inst);
func.dfg.clear_results(inst);
func.dfg.change_to_alias(result, vmctx);
func.layout.remove_inst(inst);
}
fn iadd_imm_addr(
inst: ir::Inst,
func: &mut ir::Function,
base: ir::GlobalValue,
offset: i64,
global_type: ir::Type,
) {
let mut pos = FuncCursor::new(func).at_inst(inst);
let lhs = if let ir::GlobalValueData::VMContext = pos.func.global_values[base] {
pos.func
.special_param(ir::ArgumentPurpose::VMContext)
.expect("Missing vmctx parameter")
} else {
pos.ins().global_value(global_type, base)
};
pos.func.dfg.replace(inst).iadd_imm(lhs, offset);
}
fn load_addr(
inst: ir::Inst,
func: &mut ir::Function,
base: ir::GlobalValue,
offset: ir::immediates::Offset32,
global_type: ir::Type,
readonly: bool,
isa: &dyn TargetIsa,
) {
let ptr_ty = isa.pointer_type();
let mut pos = FuncCursor::new(func).at_inst(inst);
pos.use_srcloc(inst);
let base_addr = if let ir::GlobalValueData::VMContext = pos.func.global_values[base] {
pos.func
.special_param(ir::ArgumentPurpose::VMContext)
.expect("Missing vmctx parameter")
} else {
pos.ins().global_value(ptr_ty, base)
};
let mut mflags = ir::MemFlags::trusted();
if readonly {
mflags.set_readonly();
}
pos.func
.dfg
.replace(inst)
.load(global_type, mflags, base_addr, offset);
}
fn symbol(
inst: ir::Inst,
func: &mut ir::Function,
gv: ir::GlobalValue,
isa: &dyn TargetIsa,
tls: bool,
) {
let ptr_ty = isa.pointer_type();
if tls {
func.dfg.replace(inst).tls_value(ptr_ty, gv);
} else {
func.dfg.replace(inst).symbol_value(ptr_ty, gv);
}
}