use crate::ir::pcc::*;
use crate::ir::{self, types, Constant, ConstantData, ValueLabel};
use crate::machinst::*;
use crate::ranges::Ranges;
use crate::timing;
use crate::trace;
use crate::CodegenError;
use crate::{LabelValueLoc, ValueLocRange};
use regalloc2::{
Edit, Function as RegallocFunction, InstOrEdit, InstRange, MachineEnv, Operand,
OperandConstraint, OperandKind, PRegSet, RegClass,
};
use rustc_hash::FxHashMap;
use core::mem::take;
use cranelift_entity::{entity_impl, Keys};
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;
pub type InsnIndex = regalloc2::Inst;
trait ToBackwardsInsnIndex {
fn to_backwards_insn_index(&self, num_insts: usize) -> BackwardsInsnIndex;
}
impl ToBackwardsInsnIndex for InsnIndex {
fn to_backwards_insn_index(&self, num_insts: usize) -> BackwardsInsnIndex {
BackwardsInsnIndex::new(num_insts - self.index() - 1)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "enable-serde",
derive(::serde::Serialize, ::serde::Deserialize)
)]
pub struct BackwardsInsnIndex(InsnIndex);
impl BackwardsInsnIndex {
pub fn new(i: usize) -> Self {
BackwardsInsnIndex(InsnIndex::new(i))
}
}
pub type BlockIndex = regalloc2::Block;
pub trait VCodeInst: MachInst + MachInstEmit {}
impl<I: MachInst + MachInstEmit> VCodeInst for I {}
pub struct VCode<I: VCodeInst> {
vreg_types: Vec<Type>,
insts: Vec<I>,
user_stack_maps: FxHashMap<BackwardsInsnIndex, ir::UserStackMap>,
operands: Vec<Operand>,
operand_ranges: Ranges,
clobbers: FxHashMap<InsnIndex, PRegSet>,
srclocs: Vec<RelSourceLoc>,
entry: BlockIndex,
block_ranges: Ranges,
block_succ_range: Ranges,
block_succs: Vec<regalloc2::Block>,
block_pred_range: Ranges,
block_preds: Vec<regalloc2::Block>,
block_params_range: Ranges,
block_params: Vec<regalloc2::VReg>,
branch_block_args: Vec<regalloc2::VReg>,
branch_block_arg_range: Ranges,
branch_block_arg_succ_range: Ranges,
block_order: BlockLoweringOrder,
pub(crate) abi: Callee<I::ABIMachineSpec>,
emit_info: I::Info,
pub(crate) constants: VCodeConstants,
debug_value_labels: Vec<(VReg, InsnIndex, InsnIndex, u32)>,
pub(crate) sigs: SigSet,
facts: Vec<Option<Fact>>,
}
pub struct EmitResult {
pub buffer: MachBufferFinalized<Stencil>,
pub bb_offsets: Vec<CodeOffset>,
pub bb_edges: Vec<(CodeOffset, CodeOffset)>,
pub func_body_len: CodeOffset,
pub disasm: Option<String>,
pub sized_stackslot_offsets: PrimaryMap<StackSlot, u32>,
pub dynamic_stackslot_offsets: PrimaryMap<DynamicStackSlot, u32>,
pub value_labels_ranges: ValueLabelsRanges,
pub frame_size: u32,
}
pub struct VCodeBuilder<I: VCodeInst> {
pub(crate) vcode: VCode<I>,
direction: VCodeBuildDirection,
debug_info: FxHashMap<ValueLabel, Vec<(InsnIndex, InsnIndex, VReg)>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VCodeBuildDirection {
Backward,
}
impl<I: VCodeInst> VCodeBuilder<I> {
pub fn new(
sigs: SigSet,
abi: Callee<I::ABIMachineSpec>,
emit_info: I::Info,
block_order: BlockLoweringOrder,
constants: VCodeConstants,
direction: VCodeBuildDirection,
) -> Self {
let vcode = VCode::new(sigs, abi, emit_info, block_order, constants);
VCodeBuilder {
vcode,
direction,
debug_info: FxHashMap::default(),
}
}
pub fn init_retval_area(&mut self, vregs: &mut VRegAllocator<I>) -> CodegenResult<()> {
self.vcode.abi.init_retval_area(&self.vcode.sigs, vregs)
}
pub fn abi(&self) -> &Callee<I::ABIMachineSpec> {
&self.vcode.abi
}
pub fn abi_mut(&mut self) -> &mut Callee<I::ABIMachineSpec> {
&mut self.vcode.abi
}
pub fn sigs(&self) -> &SigSet {
&self.vcode.sigs
}
pub fn sigs_mut(&mut self) -> &mut SigSet {
&mut self.vcode.sigs
}
pub fn block_order(&self) -> &BlockLoweringOrder {
&self.vcode.block_order
}
pub fn set_entry(&mut self, block: BlockIndex) {
self.vcode.entry = block;
}
pub fn end_bb(&mut self) {
let end_idx = self.vcode.insts.len();
self.vcode.block_ranges.push_end(end_idx);
let succ_end = self.vcode.block_succs.len();
self.vcode.block_succ_range.push_end(succ_end);
let block_params_end = self.vcode.block_params.len();
self.vcode.block_params_range.push_end(block_params_end);
let branch_block_arg_succ_end = self.vcode.branch_block_arg_range.len();
self.vcode
.branch_block_arg_succ_range
.push_end(branch_block_arg_succ_end);
}
pub fn add_block_param(&mut self, param: VirtualReg) {
self.vcode.block_params.push(param.into());
}
fn add_branch_args_for_succ(&mut self, args: &[Reg]) {
self.vcode
.branch_block_args
.extend(args.iter().map(|&arg| VReg::from(arg)));
let end = self.vcode.branch_block_args.len();
self.vcode.branch_block_arg_range.push_end(end);
}
pub fn push(&mut self, insn: I, loc: RelSourceLoc) {
self.vcode.insts.push(insn);
self.vcode.srclocs.push(loc);
}
pub fn add_succ(&mut self, block: BlockIndex, args: &[Reg]) {
self.vcode.block_succs.push(block);
self.add_branch_args_for_succ(args);
}
pub fn add_value_label(&mut self, reg: Reg, label: ValueLabel) {
let inst = InsnIndex::new(self.vcode.insts.len());
let labels = self.debug_info.entry(label).or_insert_with(|| vec![]);
let last = labels
.last()
.map(|(_start, end, _vreg)| *end)
.unwrap_or(InsnIndex::new(0));
labels.push((last, inst, reg.into()));
}
pub fn constants(&mut self) -> &mut VCodeConstants {
&mut self.vcode.constants
}
fn compute_preds_from_succs(&mut self) {
let mut starts = vec![0u32; self.vcode.num_blocks()];
for succ in &self.vcode.block_succs {
starts[succ.index()] += 1;
}
self.vcode.block_pred_range.reserve(starts.len());
let mut end = 0;
for count in starts.iter_mut() {
let start = end;
end += *count;
*count = start;
self.vcode.block_pred_range.push_end(end as usize);
}
let end = end as usize;
debug_assert_eq!(end, self.vcode.block_succs.len());
self.vcode.block_preds.resize(end, BlockIndex::invalid());
for (pred, range) in self.vcode.block_succ_range.iter() {
let pred = BlockIndex::new(pred);
for succ in &self.vcode.block_succs[range] {
let pos = &mut starts[succ.index()];
self.vcode.block_preds[*pos as usize] = pred;
*pos += 1;
}
}
debug_assert!(self.vcode.block_preds.iter().all(|pred| pred.is_valid()));
}
fn reverse_and_finalize(&mut self, vregs: &VRegAllocator<I>) {
let n_insts = self.vcode.insts.len();
if n_insts == 0 {
return;
}
self.vcode.block_ranges.reverse_index();
self.vcode.block_ranges.reverse_target(n_insts);
self.vcode.block_params_range.reverse_index();
self.vcode.block_succ_range.reverse_index();
self.vcode.insts.reverse();
self.vcode.srclocs.reverse();
self.vcode.branch_block_arg_succ_range.reverse_index();
let translate = |inst: InsnIndex| InsnIndex::new(n_insts - inst.index());
for (label, tuples) in &self.debug_info {
for &(start, end, vreg) in tuples {
let vreg = vregs.resolve_vreg_alias(vreg);
let fwd_start = translate(end);
let fwd_end = translate(start);
self.vcode
.debug_value_labels
.push((vreg, fwd_start, fwd_end, label.as_u32()));
}
}
self.vcode
.debug_value_labels
.sort_unstable_by_key(|(vreg, _, _, _)| *vreg);
}
fn collect_operands(&mut self, vregs: &VRegAllocator<I>) {
let allocatable = PRegSet::from(self.vcode.machine_env());
for (i, insn) in self.vcode.insts.iter_mut().enumerate() {
let mut op_collector =
OperandCollector::new(&mut self.vcode.operands, allocatable, |vreg| {
vregs.resolve_vreg_alias(vreg)
});
insn.get_operands(&mut op_collector);
let (ops, clobbers) = op_collector.finish();
self.vcode.operand_ranges.push_end(ops);
if clobbers != PRegSet::default() {
self.vcode.clobbers.insert(InsnIndex::new(i), clobbers);
}
if let Some((dst, src)) = insn.is_move() {
assert!(
src.is_virtual(),
"the real register {src:?} was used as the source of a move instruction"
);
assert!(
dst.to_reg().is_virtual(),
"the real register {:?} was used as the destination of a move instruction",
dst.to_reg()
);
}
}
for arg in &mut self.vcode.branch_block_args {
let new_arg = vregs.resolve_vreg_alias(*arg);
trace!("operandcollector: block arg {:?} -> {:?}", arg, new_arg);
*arg = new_arg;
}
}
pub fn build(mut self, mut vregs: VRegAllocator<I>) -> VCode<I> {
self.vcode.vreg_types = take(&mut vregs.vreg_types);
self.vcode.facts = take(&mut vregs.facts);
if self.direction == VCodeBuildDirection::Backward {
self.reverse_and_finalize(&vregs);
}
self.collect_operands(&vregs);
self.compute_preds_from_succs();
self.vcode.debug_value_labels.sort_unstable();
vregs.debug_assert_no_vreg_aliases(self.vcode.operands.iter().map(|op| op.vreg()));
vregs.debug_assert_no_vreg_aliases(self.vcode.block_params.iter().copied());
vregs.debug_assert_no_vreg_aliases(self.vcode.branch_block_args.iter().copied());
vregs.debug_assert_no_vreg_aliases(
self.vcode.debug_value_labels.iter().map(|&(vreg, ..)| vreg),
);
vregs.debug_assert_no_vreg_aliases(
self.vcode
.facts
.iter()
.zip(&vregs.vreg_types)
.enumerate()
.filter(|(_, (fact, _))| fact.is_some())
.map(|(vreg, (_, &ty))| {
let (regclasses, _) = I::rc_for_type(ty).unwrap();
VReg::new(vreg, regclasses[0])
}),
);
self.vcode
}
pub fn add_user_stack_map(
&mut self,
inst: BackwardsInsnIndex,
entries: &[ir::UserStackMapEntry],
) {
let stack_map = ir::UserStackMap::new(entries, self.vcode.abi.sized_stackslot_offsets());
let old_entry = self.vcode.user_stack_maps.insert(inst, stack_map);
debug_assert!(old_entry.is_none());
}
}
const NO_INST_OFFSET: CodeOffset = u32::MAX;
impl<I: VCodeInst> VCode<I> {
fn new(
sigs: SigSet,
abi: Callee<I::ABIMachineSpec>,
emit_info: I::Info,
block_order: BlockLoweringOrder,
constants: VCodeConstants,
) -> Self {
let n_blocks = block_order.lowered_order().len();
VCode {
sigs,
vreg_types: vec![],
insts: Vec::with_capacity(10 * n_blocks),
user_stack_maps: FxHashMap::default(),
operands: Vec::with_capacity(30 * n_blocks),
operand_ranges: Ranges::with_capacity(10 * n_blocks),
clobbers: FxHashMap::default(),
srclocs: Vec::with_capacity(10 * n_blocks),
entry: BlockIndex::new(0),
block_ranges: Ranges::with_capacity(n_blocks),
block_succ_range: Ranges::with_capacity(n_blocks),
block_succs: Vec::with_capacity(n_blocks),
block_pred_range: Ranges::default(),
block_preds: Vec::new(),
block_params_range: Ranges::with_capacity(n_blocks),
block_params: Vec::with_capacity(5 * n_blocks),
branch_block_args: Vec::with_capacity(10 * n_blocks),
branch_block_arg_range: Ranges::with_capacity(2 * n_blocks),
branch_block_arg_succ_range: Ranges::with_capacity(n_blocks),
block_order,
abi,
emit_info,
constants,
debug_value_labels: vec![],
facts: vec![],
}
}
pub fn machine_env(&self) -> &MachineEnv {
self.abi.machine_env(&self.sigs)
}
pub fn num_blocks(&self) -> usize {
self.block_ranges.len()
}
pub fn num_insts(&self) -> usize {
self.insts.len()
}
fn compute_clobbers(&self, regalloc: ®alloc2::Output) -> Vec<Writable<RealReg>> {
let mut clobbered = PRegSet::default();
for (_, Edit::Move { to, .. }) in ®alloc.edits {
if let Some(preg) = to.as_reg() {
clobbered.add(preg);
}
}
for (i, range) in self.operand_ranges.iter() {
if !self.insts[i].is_included_in_clobbers() {
continue;
}
let operands = &self.operands[range.clone()];
let allocs = ®alloc.allocs[range];
for (operand, alloc) in operands.iter().zip(allocs.iter()) {
if operand.kind() == OperandKind::Def {
if let Some(preg) = alloc.as_reg() {
clobbered.add(preg);
}
}
}
if let Some(&inst_clobbered) = self.clobbers.get(&InsnIndex::new(i)) {
clobbered.union_from(inst_clobbered);
}
}
clobbered
.into_iter()
.map(|preg| Writable::from_reg(RealReg::from(preg)))
.collect()
}
pub fn emit(
mut self,
regalloc: ®alloc2::Output,
want_disasm: bool,
flags: &settings::Flags,
ctrl_plane: &mut ControlPlane,
) -> EmitResult
where
I: VCodeInst,
{
use core::fmt::Write;
let _tt = timing::vcode_emit();
let mut buffer = MachBuffer::new();
let mut bb_starts: Vec<Option<CodeOffset>> = vec![];
buffer.reserve_labels_for_blocks(self.num_blocks());
buffer.register_constants(&self.constants);
let mut final_order: SmallVec<[BlockIndex; 16]> = smallvec![];
let mut cold_blocks: SmallVec<[BlockIndex; 16]> = smallvec![];
for block in 0..self.num_blocks() {
let block = BlockIndex::new(block);
if self.block_order.is_cold(block) {
cold_blocks.push(block);
} else {
final_order.push(block);
}
}
final_order.extend(cold_blocks.clone());
let clobbers = self.compute_clobbers(regalloc);
self.abi
.compute_frame_layout(&self.sigs, regalloc.num_spillslots, clobbers);
let mut cur_srcloc = None;
let mut last_offset = None;
let mut inst_offsets = vec![];
let mut state = I::State::new(&self.abi, std::mem::take(ctrl_plane));
let mut disasm = String::new();
if !self.debug_value_labels.is_empty() {
inst_offsets.resize(self.insts.len(), NO_INST_OFFSET);
}
let mut ra_edits_per_block: SmallVec<[u32; 64]> = smallvec![];
let mut edit_idx = 0;
for block in 0..self.num_blocks() {
let end_inst = InsnIndex::new(self.block_ranges.get(block).end);
let start_edit_idx = edit_idx;
while edit_idx < regalloc.edits.len() && regalloc.edits[edit_idx].0.inst() < end_inst {
edit_idx += 1;
}
let end_edit_idx = edit_idx;
ra_edits_per_block.push((end_edit_idx - start_edit_idx) as u32);
}
let is_forward_edge_cfi_enabled = self.abi.is_forward_edge_cfi_enabled();
let mut bb_padding = match flags.bb_padding_log2_minus_one() {
0 => Vec::new(),
n => vec![0; 1 << (n - 1)],
};
let mut total_bb_padding = 0;
for (block_order_idx, &block) in final_order.iter().enumerate() {
trace!("emitting block {:?}", block);
state.on_new_block();
let new_offset = I::align_basic_block(buffer.cur_offset());
while new_offset > buffer.cur_offset() {
let nop = I::gen_nop((new_offset - buffer.cur_offset()) as usize);
nop.emit(&mut buffer, &self.emit_info, &mut Default::default());
}
assert_eq!(buffer.cur_offset(), new_offset);
let do_emit = |inst: &I,
disasm: &mut String,
buffer: &mut MachBuffer<I>,
state: &mut I::State| {
if want_disasm && !inst.is_args() {
let mut s = state.clone();
writeln!(disasm, " {}", inst.pretty_print_inst(&mut s)).unwrap();
}
inst.emit(buffer, &self.emit_info, state);
};
if block == self.entry {
trace!(" -> entry block");
buffer.start_srcloc(Default::default());
for inst in &self.abi.gen_prologue() {
do_emit(&inst, &mut disasm, &mut buffer, &mut state);
}
buffer.end_srcloc();
}
buffer.bind_label(MachLabel::from_block(block), state.ctrl_plane_mut());
if want_disasm {
writeln!(&mut disasm, "block{}:", block.index()).unwrap();
}
if flags.machine_code_cfg_info() {
let cur_offset = buffer.cur_offset();
if last_offset.is_some() && cur_offset <= last_offset.unwrap() {
for i in (0..bb_starts.len()).rev() {
if bb_starts[i].is_some() && cur_offset > bb_starts[i].unwrap() {
break;
}
bb_starts[i] = None;
}
}
bb_starts.push(Some(cur_offset));
last_offset = Some(cur_offset);
}
if let Some(block_start) = I::gen_block_start(
self.block_order.is_indirect_branch_target(block),
is_forward_edge_cfi_enabled,
) {
do_emit(&block_start, &mut disasm, &mut buffer, &mut state);
}
for inst_or_edit in regalloc.block_insts_and_edits(&self, block) {
match inst_or_edit {
InstOrEdit::Inst(iix) => {
if !self.debug_value_labels.is_empty() {
if !self.block_order.is_cold(block) {
inst_offsets[iix.index()] = buffer.cur_offset();
}
}
let srcloc = self.srclocs[iix.index()];
if cur_srcloc != Some(srcloc) {
if cur_srcloc.is_some() {
buffer.end_srcloc();
}
buffer.start_srcloc(srcloc);
cur_srcloc = Some(srcloc);
}
let stack_map_disasm = if self.insts[iix.index()].is_safepoint() {
let (user_stack_map, user_stack_map_disasm) = {
let index = iix.to_backwards_insn_index(self.num_insts());
let user_stack_map = self.user_stack_maps.remove(&index);
let user_stack_map_disasm =
user_stack_map.as_ref().map(|m| format!(" ; {m:?}"));
(user_stack_map, user_stack_map_disasm)
};
state.pre_safepoint(user_stack_map);
user_stack_map_disasm
} else {
None
};
if self.insts[iix.index()].is_term() == MachTerminator::Ret {
for inst in self.abi.gen_epilogue() {
do_emit(&inst, &mut disasm, &mut buffer, &mut state);
}
} else {
let mut allocs = regalloc.inst_allocs(iix).iter();
self.insts[iix.index()].get_operands(
&mut |reg: &mut Reg, constraint, _kind, _pos| {
let alloc = allocs
.next()
.expect("enough allocations for all operands")
.as_reg()
.expect("only register allocations, not stack allocations")
.into();
if let OperandConstraint::FixedReg(rreg) = constraint {
debug_assert_eq!(Reg::from(rreg), alloc);
}
*reg = alloc;
},
);
debug_assert!(allocs.next().is_none());
do_emit(
&self.insts[iix.index()],
&mut disasm,
&mut buffer,
&mut state,
);
if let Some(stack_map_disasm) = stack_map_disasm {
disasm.push_str(&stack_map_disasm);
disasm.push('\n');
}
}
}
InstOrEdit::Edit(Edit::Move { from, to }) => {
match (from.as_reg(), to.as_reg()) {
(Some(from), Some(to)) => {
let from_rreg = Reg::from(from);
let to_rreg = Writable::from_reg(Reg::from(to));
debug_assert_eq!(from.class(), to.class());
let ty = I::canonical_type_for_rc(from.class());
let mv = I::gen_move(to_rreg, from_rreg, ty);
do_emit(&mv, &mut disasm, &mut buffer, &mut state);
}
(Some(from), None) => {
let to = to.as_stack().unwrap();
let from_rreg = RealReg::from(from);
let spill = self.abi.gen_spill(to, from_rreg);
do_emit(&spill, &mut disasm, &mut buffer, &mut state);
}
(None, Some(to)) => {
let from = from.as_stack().unwrap();
let to_rreg = Writable::from_reg(RealReg::from(to));
let reload = self.abi.gen_reload(to_rreg, from);
do_emit(&reload, &mut disasm, &mut buffer, &mut state);
}
(None, None) => {
panic!("regalloc2 should have eliminated stack-to-stack moves!");
}
}
}
}
}
if cur_srcloc.is_some() {
buffer.end_srcloc();
cur_srcloc = None;
}
let worst_case_next_bb = if block_order_idx < final_order.len() - 1 {
let next_block = final_order[block_order_idx + 1];
let next_block_range = self.block_ranges.get(next_block.index());
let next_block_size = next_block_range.len() as u32;
let next_block_ra_insertions = ra_edits_per_block[next_block.index()];
I::worst_case_size() * (next_block_size + next_block_ra_insertions)
} else {
0
};
let padding = if bb_padding.is_empty() {
0
} else {
bb_padding.len() as u32 + I::LabelUse::ALIGN - 1
};
if buffer.island_needed(padding + worst_case_next_bb) {
buffer.emit_island(padding + worst_case_next_bb, ctrl_plane);
}
if !bb_padding.is_empty() {
buffer.put_data(&bb_padding);
buffer.align_to(I::LabelUse::ALIGN);
total_bb_padding += bb_padding.len();
if total_bb_padding > (150 << 20) {
bb_padding = Vec::new();
}
}
}
debug_assert!(
self.user_stack_maps.is_empty(),
"any stack maps should have been consumed by instruction emission, still have: {:#?}",
self.user_stack_maps,
);
buffer.optimize_branches(ctrl_plane);
*ctrl_plane = state.take_ctrl_plane();
let func_body_len = buffer.cur_offset();
let mut bb_edges = vec![];
let mut bb_offsets = vec![];
if flags.machine_code_cfg_info() {
for block in 0..self.num_blocks() {
if bb_starts[block].is_none() {
continue;
}
let from = bb_starts[block].unwrap();
bb_offsets.push(from);
let succs = self.block_succs(BlockIndex::new(block));
for &succ in succs.iter() {
let to = buffer.resolve_label_offset(MachLabel::from_block(succ));
bb_edges.push((from, to));
}
}
}
self.monotonize_inst_offsets(&mut inst_offsets[..], func_body_len);
let value_labels_ranges =
self.compute_value_labels_ranges(regalloc, &inst_offsets[..], func_body_len);
let frame_size = self.abi.frame_size();
EmitResult {
buffer: buffer.finish(&self.constants, ctrl_plane),
bb_offsets,
bb_edges,
func_body_len,
disasm: if want_disasm { Some(disasm) } else { None },
sized_stackslot_offsets: self.abi.sized_stackslot_offsets().clone(),
dynamic_stackslot_offsets: self.abi.dynamic_stackslot_offsets().clone(),
value_labels_ranges,
frame_size,
}
}
fn monotonize_inst_offsets(&self, inst_offsets: &mut [CodeOffset], func_body_len: u32) {
if self.debug_value_labels.is_empty() {
return;
}
let mut next_offset = func_body_len;
for inst_index in (0..(inst_offsets.len() - 1)).rev() {
let inst_offset = inst_offsets[inst_index];
if inst_offset == NO_INST_OFFSET {
continue;
}
if inst_offset > next_offset {
trace!(
"Fixing code offset of the removed Inst {}: {} -> {}",
inst_index,
inst_offset,
next_offset
);
inst_offsets[inst_index] = next_offset;
continue;
}
next_offset = inst_offset;
}
}
fn compute_value_labels_ranges(
&self,
regalloc: ®alloc2::Output,
inst_offsets: &[CodeOffset],
func_body_len: u32,
) -> ValueLabelsRanges {
if self.debug_value_labels.is_empty() {
return ValueLabelsRanges::default();
}
let mut value_labels_ranges: ValueLabelsRanges = HashMap::new();
for &(label, from, to, alloc) in ®alloc.debug_locations {
let ranges = value_labels_ranges
.entry(ValueLabel::from_u32(label))
.or_insert_with(|| vec![]);
let from_offset = inst_offsets[from.inst().index()];
let to_offset = if to.inst().index() == inst_offsets.len() {
func_body_len
} else {
inst_offsets[to.inst().index()]
};
if from_offset == NO_INST_OFFSET
|| to_offset == NO_INST_OFFSET
|| from_offset == to_offset
{
continue;
}
let loc = if let Some(preg) = alloc.as_reg() {
LabelValueLoc::Reg(Reg::from(preg))
} else {
let slot = alloc.as_stack().unwrap();
let slot_offset = self.abi.get_spillslot_offset(slot);
let slot_base_to_caller_sp_offset = self.abi.slot_base_to_caller_sp_offset();
let caller_sp_to_cfa_offset =
crate::isa::unwind::systemv::caller_sp_to_cfa_offset();
let cfa_to_sp_offset =
-((slot_base_to_caller_sp_offset + caller_sp_to_cfa_offset) as i64);
LabelValueLoc::CFAOffset(cfa_to_sp_offset + slot_offset)
};
let start = from_offset + 1;
let end = to_offset + 1;
if let Some(last_loc_range) = ranges.last_mut() {
if last_loc_range.loc == loc && last_loc_range.end == start {
trace!(
"Extending debug range for VL{} in {:?} to {}",
label,
loc,
end
);
last_loc_range.end = end;
continue;
}
}
trace!(
"Recording debug range for VL{} in {:?}: [Inst {}..Inst {}) [{}..{})",
label,
loc,
from.inst().index(),
to.inst().index(),
start,
end
);
ranges.push(ValueLocRange { loc, start, end });
}
value_labels_ranges
}
pub fn bindex_to_bb(&self, block: BlockIndex) -> Option<ir::Block> {
self.block_order.lowered_order()[block.index()].orig_block()
}
pub fn vreg_type(&self, vreg: VReg) -> Type {
self.vreg_types[vreg.vreg()]
}
pub fn vreg_fact(&self, vreg: VReg) -> Option<&Fact> {
self.facts[vreg.vreg()].as_ref()
}
pub fn set_vreg_fact(&mut self, vreg: VReg, fact: Fact) {
trace!("set fact on {}: {:?}", vreg, fact);
self.facts[vreg.vreg()] = Some(fact);
}
pub fn inst_defines_facts(&self, inst: InsnIndex) -> bool {
self.inst_operands(inst)
.iter()
.filter(|o| o.kind() == OperandKind::Def)
.map(|o| o.vreg())
.any(|vreg| self.facts[vreg.vreg()].is_some())
}
pub fn get_user_stack_map(&self, inst: InsnIndex) -> Option<&ir::UserStackMap> {
let index = inst.to_backwards_insn_index(self.num_insts());
self.user_stack_maps.get(&index)
}
}
impl<I: VCodeInst> std::ops::Index<InsnIndex> for VCode<I> {
type Output = I;
fn index(&self, idx: InsnIndex) -> &Self::Output {
&self.insts[idx.index()]
}
}
impl<I: VCodeInst> RegallocFunction for VCode<I> {
fn num_insts(&self) -> usize {
self.insts.len()
}
fn num_blocks(&self) -> usize {
self.block_ranges.len()
}
fn entry_block(&self) -> BlockIndex {
self.entry
}
fn block_insns(&self, block: BlockIndex) -> InstRange {
let range = self.block_ranges.get(block.index());
InstRange::new(InsnIndex::new(range.start), InsnIndex::new(range.end))
}
fn block_succs(&self, block: BlockIndex) -> &[BlockIndex] {
let range = self.block_succ_range.get(block.index());
&self.block_succs[range]
}
fn block_preds(&self, block: BlockIndex) -> &[BlockIndex] {
let range = self.block_pred_range.get(block.index());
&self.block_preds[range]
}
fn block_params(&self, block: BlockIndex) -> &[VReg] {
if block == self.entry {
return &[];
}
let range = self.block_params_range.get(block.index());
&self.block_params[range]
}
fn branch_blockparams(&self, block: BlockIndex, _insn: InsnIndex, succ_idx: usize) -> &[VReg] {
let succ_range = self.branch_block_arg_succ_range.get(block.index());
debug_assert!(succ_idx < succ_range.len());
let branch_block_args = self.branch_block_arg_range.get(succ_range.start + succ_idx);
&self.branch_block_args[branch_block_args]
}
fn is_ret(&self, insn: InsnIndex) -> bool {
match self.insts[insn.index()].is_term() {
MachTerminator::None => self.insts[insn.index()].is_trap(),
MachTerminator::Ret | MachTerminator::RetCall => true,
MachTerminator::Uncond | MachTerminator::Cond | MachTerminator::Indirect => false,
}
}
fn is_branch(&self, insn: InsnIndex) -> bool {
match self.insts[insn.index()].is_term() {
MachTerminator::Cond | MachTerminator::Uncond | MachTerminator::Indirect => true,
_ => false,
}
}
fn inst_operands(&self, insn: InsnIndex) -> &[Operand] {
let range = self.operand_ranges.get(insn.index());
&self.operands[range]
}
fn inst_clobbers(&self, insn: InsnIndex) -> PRegSet {
self.clobbers.get(&insn).cloned().unwrap_or_default()
}
fn num_vregs(&self) -> usize {
self.vreg_types.len()
}
fn debug_value_labels(&self) -> &[(VReg, InsnIndex, InsnIndex, u32)] {
&self.debug_value_labels
}
fn spillslot_size(&self, regclass: RegClass) -> usize {
self.abi.get_spillslot_size(regclass) as usize
}
fn allow_multiple_vreg_defs(&self) -> bool {
true
}
}
impl<I: VCodeInst> Debug for VRegAllocator<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "VRegAllocator {{")?;
let mut alias_keys = self.vreg_aliases.keys().cloned().collect::<Vec<_>>();
alias_keys.sort_unstable();
for key in alias_keys {
let dest = self.vreg_aliases.get(&key).unwrap();
writeln!(f, " {:?} := {:?}", Reg::from(key), Reg::from(*dest))?;
}
for (vreg, fact) in self.facts.iter().enumerate() {
if let Some(fact) = fact {
writeln!(f, " v{vreg} ! {fact}")?;
}
}
writeln!(f, "}}")
}
}
impl<I: VCodeInst> fmt::Debug for VCode<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "VCode {{")?;
writeln!(f, " Entry block: {}", self.entry.index())?;
let mut state = Default::default();
for block in 0..self.num_blocks() {
let block = BlockIndex::new(block);
writeln!(
f,
"Block {}({:?}):",
block.index(),
self.block_params(block)
)?;
if let Some(bb) = self.bindex_to_bb(block) {
writeln!(f, " (original IR block: {bb})")?;
}
for (succ_idx, succ) in self.block_succs(block).iter().enumerate() {
writeln!(
f,
" (successor: Block {}({:?}))",
succ.index(),
self.branch_blockparams(block, InsnIndex::new(0) , succ_idx)
)?;
}
for inst in self.block_ranges.get(block.index()) {
writeln!(
f,
" Inst {}: {}",
inst,
self.insts[inst].pretty_print_inst(&mut state)
)?;
if !self.operands.is_empty() {
for operand in self.inst_operands(InsnIndex::new(inst)) {
if operand.kind() == OperandKind::Def {
if let Some(fact) = &self.facts[operand.vreg().vreg()] {
writeln!(f, " v{} ! {}", operand.vreg().vreg(), fact)?;
}
}
}
}
if let Some(user_stack_map) = self.get_user_stack_map(InsnIndex::new(inst)) {
writeln!(f, " {user_stack_map:?}")?;
}
}
}
writeln!(f, "}}")?;
Ok(())
}
}
pub struct VRegAllocator<I> {
vreg_types: Vec<Type>,
vreg_aliases: FxHashMap<regalloc2::VReg, regalloc2::VReg>,
deferred_error: Option<CodegenError>,
facts: Vec<Option<Fact>>,
_inst: core::marker::PhantomData<I>,
}
impl<I: VCodeInst> VRegAllocator<I> {
pub fn with_capacity(capacity: usize) -> Self {
let capacity = first_user_vreg_index() + capacity;
let mut vreg_types = Vec::with_capacity(capacity);
vreg_types.resize(first_user_vreg_index(), types::INVALID);
Self {
vreg_types,
vreg_aliases: FxHashMap::with_capacity_and_hasher(capacity, Default::default()),
deferred_error: None,
facts: Vec::with_capacity(capacity),
_inst: core::marker::PhantomData::default(),
}
}
pub fn alloc(&mut self, ty: Type) -> CodegenResult<ValueRegs<Reg>> {
if self.deferred_error.is_some() {
return Err(CodegenError::CodeTooLarge);
}
let v = self.vreg_types.len();
let (regclasses, tys) = I::rc_for_type(ty)?;
if v + regclasses.len() >= VReg::MAX {
return Err(CodegenError::CodeTooLarge);
}
let regs: ValueRegs<Reg> = match regclasses {
&[rc0] => ValueRegs::one(VReg::new(v, rc0).into()),
&[rc0, rc1] => ValueRegs::two(VReg::new(v, rc0).into(), VReg::new(v + 1, rc1).into()),
_ => panic!("Value must reside in 1 or 2 registers"),
};
for (®_ty, ®) in tys.iter().zip(regs.regs().iter()) {
let vreg = reg.to_virtual_reg().unwrap();
debug_assert_eq!(self.vreg_types.len(), vreg.index());
self.vreg_types.push(reg_ty);
}
self.facts.resize(self.vreg_types.len(), None);
Ok(regs)
}
pub fn alloc_with_deferred_error(&mut self, ty: Type) -> ValueRegs<Reg> {
match self.alloc(ty) {
Ok(x) => x,
Err(e) => {
self.deferred_error = Some(e);
self.bogus_for_deferred_error(ty)
}
}
}
pub fn take_deferred_error(&mut self) -> Option<CodegenError> {
self.deferred_error.take()
}
fn bogus_for_deferred_error(&self, ty: Type) -> ValueRegs<Reg> {
let (regclasses, _tys) = I::rc_for_type(ty).expect("must have valid type");
match regclasses {
&[rc0] => ValueRegs::one(VReg::new(0, rc0).into()),
&[rc0, rc1] => ValueRegs::two(VReg::new(0, rc0).into(), VReg::new(1, rc1).into()),
_ => panic!("Value must reside in 1 or 2 registers"),
}
}
pub fn set_vreg_alias(&mut self, from: Reg, to: Reg) {
let from = from.into();
let resolved_to = self.resolve_vreg_alias(to.into());
assert_ne!(resolved_to, from);
if let Some(fact) = self.facts[from.vreg()].take() {
self.set_fact(resolved_to, fact);
}
let old_alias = self.vreg_aliases.insert(from, resolved_to);
debug_assert_eq!(old_alias, None);
}
fn resolve_vreg_alias(&self, mut vreg: regalloc2::VReg) -> regalloc2::VReg {
while let Some(to) = self.vreg_aliases.get(&vreg) {
vreg = *to;
}
vreg
}
#[inline]
fn debug_assert_no_vreg_aliases(&self, mut list: impl Iterator<Item = VReg>) {
debug_assert!(list.all(|vreg| !self.vreg_aliases.contains_key(&vreg)));
}
fn set_fact(&mut self, vreg: regalloc2::VReg, fact: Fact) -> Option<Fact> {
trace!("vreg {:?} has fact: {:?}", vreg, fact);
debug_assert!(!self.vreg_aliases.contains_key(&vreg));
self.facts[vreg.vreg()].replace(fact)
}
pub fn set_fact_if_missing(&mut self, vreg: VirtualReg, fact: Fact) {
let vreg = self.resolve_vreg_alias(vreg.into());
if self.facts[vreg.vreg()].is_none() {
self.set_fact(vreg, fact);
}
}
pub fn alloc_with_maybe_fact(
&mut self,
ty: Type,
fact: Option<Fact>,
) -> CodegenResult<ValueRegs<Reg>> {
let result = self.alloc(ty)?;
assert!(result.len() == 1 || fact.is_none());
if let Some(fact) = fact {
self.set_fact(result.regs()[0].into(), fact);
}
Ok(result)
}
}
#[derive(Default)]
pub struct VCodeConstants {
constants: PrimaryMap<VCodeConstant, VCodeConstantData>,
pool_uses: HashMap<Constant, VCodeConstant>,
well_known_uses: HashMap<*const [u8], VCodeConstant>,
u64s: HashMap<[u8; 8], VCodeConstant>,
}
impl VCodeConstants {
pub fn with_capacity(expected_num_constants: usize) -> Self {
Self {
constants: PrimaryMap::with_capacity(expected_num_constants),
pool_uses: HashMap::with_capacity(expected_num_constants),
well_known_uses: HashMap::new(),
u64s: HashMap::new(),
}
}
pub fn insert(&mut self, data: VCodeConstantData) -> VCodeConstant {
match data {
VCodeConstantData::Generated(_) => self.constants.push(data),
VCodeConstantData::Pool(constant, _) => match self.pool_uses.get(&constant) {
None => {
let vcode_constant = self.constants.push(data);
self.pool_uses.insert(constant, vcode_constant);
vcode_constant
}
Some(&vcode_constant) => vcode_constant,
},
VCodeConstantData::WellKnown(data_ref) => {
match self.well_known_uses.entry(data_ref as *const [u8]) {
Entry::Vacant(v) => {
let vcode_constant = self.constants.push(data);
v.insert(vcode_constant);
vcode_constant
}
Entry::Occupied(o) => *o.get(),
}
}
VCodeConstantData::U64(value) => match self.u64s.entry(value) {
Entry::Vacant(v) => {
let vcode_constant = self.constants.push(data);
v.insert(vcode_constant);
vcode_constant
}
Entry::Occupied(o) => *o.get(),
},
}
}
pub fn len(&self) -> usize {
self.constants.len()
}
pub fn keys(&self) -> Keys<VCodeConstant> {
self.constants.keys()
}
pub fn iter(&self) -> impl Iterator<Item = (VCodeConstant, &VCodeConstantData)> {
self.constants.iter()
}
pub fn get(&self, c: VCodeConstant) -> &VCodeConstantData {
&self.constants[c]
}
pub fn pool_uses(&self, constant: &VCodeConstantData) -> bool {
match constant {
VCodeConstantData::Pool(c, _) => self.pool_uses.contains_key(c),
_ => false,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct VCodeConstant(u32);
entity_impl!(VCodeConstant);
pub enum VCodeConstantData {
Pool(Constant, ConstantData),
WellKnown(&'static [u8]),
Generated(ConstantData),
U64([u8; 8]),
}
impl VCodeConstantData {
pub fn as_slice(&self) -> &[u8] {
match self {
VCodeConstantData::Pool(_, d) | VCodeConstantData::Generated(d) => d.as_slice(),
VCodeConstantData::WellKnown(d) => d,
VCodeConstantData::U64(value) => &value[..],
}
}
pub fn alignment(&self) -> u32 {
if self.as_slice().len() <= 8 {
8
} else {
16
}
}
}
#[cfg(test)]
mod test {
use super::*;
use std::mem::size_of;
#[test]
fn size_of_constant_structs() {
assert_eq!(size_of::<Constant>(), 4);
assert_eq!(size_of::<VCodeConstant>(), 4);
assert_eq!(size_of::<ConstantData>(), 24);
assert_eq!(size_of::<VCodeConstantData>(), 32);
assert_eq!(
size_of::<PrimaryMap<VCodeConstant, VCodeConstantData>>(),
24
);
}
}