use crate::CodegenError;
use crate::FxHashMap;
use crate::ir::pcc::*;
use crate::ir::{self, Constant, ConstantData, ValueLabel, types};
use crate::ranges::Ranges;
use crate::timing;
use crate::trace;
use crate::{LabelValueLoc, ValueLocRange};
use crate::{machinst::*, trace_log_enabled};
use regalloc2::{
Edit, Function as RegallocFunction, InstOrEdit, InstPosition, InstRange, Operand,
OperandConstraint, OperandKind, PRegSet, ProgPoint, RegClass,
};
use crate::HashMap;
use crate::hash_map::Entry;
use core::cmp::Ordering;
use core::fmt::{self, Write};
use core::mem::take;
use core::ops::Range;
use cranelift_entity::{Keys, entity_impl};
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>,
debug_tags: FxHashMap<BackwardsInsnIndex, Range<u32>>,
debug_tag_pool: Vec<ir::DebugTag>,
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>>,
log2_min_function_alignment: u8,
}
pub struct EmitResult {
pub buffer: MachBufferFinalized<Stencil>,
pub bb_offsets: Vec<CodeOffset>,
pub bb_edges: Vec<(CodeOffset, CodeOffset)>,
pub disasm: Option<String>,
pub value_labels_ranges: ValueLabelsRanges,
}
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,
log2_min_function_alignment: u8,
) -> Self {
let vcode = VCode::new(
sigs,
abi,
emit_info,
block_order,
constants,
log2_min_function_alignment,
);
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) {
assert!(!insn.is_low_level_branch()); 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 next_inst_index = self.vcode.insts.len();
if next_inst_index == 0 {
return;
}
let next_inst = InsnIndex::new(next_inst_index);
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, next_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.abi.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());
}
pub fn add_debug_tags(&mut self, inst: BackwardsInsnIndex, entries: &[ir::DebugTag]) {
let start = u32::try_from(self.vcode.debug_tag_pool.len()).unwrap();
self.vcode.debug_tag_pool.extend(entries.iter().cloned());
let end = u32::try_from(self.vcode.debug_tag_pool.len()).unwrap();
self.vcode.debug_tags.insert(inst, start..end);
}
}
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,
log2_min_function_alignment: u8,
) -> 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(),
debug_tags: FxHashMap::default(),
debug_tag_pool: vec![],
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![],
log2_min_function_alignment,
}
}
pub fn num_blocks(&self) -> usize {
self.block_ranges.len()
}
pub fn num_insts(&self) -> usize {
self.insts.len()
}
fn compute_clobbers_and_function_calls(
&self,
regalloc: ®alloc2::Output,
) -> (Vec<Writable<RealReg>>, FunctionCalls) {
let mut clobbered = PRegSet::default();
let mut function_calls = FunctionCalls::None;
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() {
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);
}
}
}
function_calls.update(self.insts[i].call_type());
if self.insts[i].is_included_in_clobbers() {
if let Some(&inst_clobbered) = self.clobbers.get(&InsnIndex::new(i)) {
clobbered.union_from(inst_clobbered);
}
}
}
let clobbered_regs = clobbered
.into_iter()
.map(|preg| Writable::from_reg(RealReg::from(preg)))
.collect();
(clobbered_regs, function_calls)
}
pub fn emit(
mut self,
regalloc: ®alloc2::Output,
want_disasm: bool,
flags: &settings::Flags,
ctrl_plane: &mut ControlPlane,
) -> EmitResult
where
I: VCodeInst,
{
let _tt = timing::vcode_emit();
let mut buffer = MachBuffer::new();
buffer.set_log2_min_function_alignment(self.log2_min_function_alignment);
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, function_calls) = self.compute_clobbers_and_function_calls(regalloc);
self.abi.compute_frame_layout(
&self.sigs,
regalloc.num_spillslots,
clobbers,
function_calls,
);
let mut cur_srcloc = None;
let mut last_offset = None;
let mut inst_offsets = vec![];
let mut state = I::State::new(&self.abi, core::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 = if want_disasm {
user_stack_map.as_ref().map(|m| format!(" ; {m:?}"))
} else {
None
};
(user_stack_map, user_stack_map_disasm)
};
state.pre_safepoint(user_stack_map);
user_stack_map_disasm
} else {
None
};
let mut debug_tag_disasm = None;
let mut place_debug_tags =
|this: &VCode<I>, pos: MachDebugTagPos, buffer: &mut MachBuffer<I>| {
let debug_tag_range = {
let index = iix.to_backwards_insn_index(this.num_insts());
this.debug_tags.get(&index)
};
if let Some(range) = debug_tag_range {
let start = usize::try_from(range.start).unwrap();
let end = usize::try_from(range.end).unwrap();
let tags = &this.debug_tag_pool[start..end];
if want_disasm {
debug_tag_disasm =
Some(format!(" ; ^-- debug @ {pos:?}: {tags:?}"));
}
buffer.push_debug_tags(pos, tags);
}
};
let debug_tag_pos =
if self.insts[iix.index()].call_type() == CallType::Regular {
MachDebugTagPos::Post
} else {
MachDebugTagPos::Pre
};
if debug_tag_pos == MachDebugTagPos::Pre {
place_debug_tags(&self, debug_tag_pos, &mut buffer);
}
if self.insts[iix.index()].is_term() == MachTerminator::Ret {
log::trace!("emitting epilogue");
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");
if let Some(alloc) = alloc.as_reg() {
let alloc: Reg = alloc.into();
if let OperandConstraint::FixedReg(rreg) = constraint {
debug_assert_eq!(Reg::from(rreg), alloc);
}
*reg = alloc;
} else if let Some(alloc) = alloc.as_stack() {
let alloc: Reg = alloc.into();
*reg = alloc;
}
},
);
debug_assert!(allocs.next().is_none());
log::trace!("emitting: {:?}", self.insts[iix.index()]);
do_emit(
&self.insts[iix.index()],
&mut disasm,
&mut buffer,
&mut state,
);
if debug_tag_pos == MachDebugTagPos::Post {
place_debug_tags(&self, debug_tag_pos, &mut buffer);
}
if let Some(stack_map_disasm) = stack_map_disasm {
disasm.push_str(&stack_map_disasm);
disasm.push('\n');
}
if let Some(debug_tag_disasm) = debug_tag_disasm {
disasm.push_str(&debug_tag_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);
buffer.set_frame_layout(self.abi.frame_slot_metadata());
EmitResult {
buffer: buffer.finish(&self.constants, ctrl_plane),
bb_offsets,
bb_edges,
disasm: if want_disasm { Some(disasm) } else { None },
value_labels_ranges,
}
}
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();
}
if trace_log_enabled!() {
self.log_value_labels_ranges(regalloc, inst_offsets);
}
let mut value_labels_ranges: ValueLabelsRanges = HashMap::new();
for &(label, from, to, alloc) in ®alloc.debug_locations {
let label = ValueLabel::from_u32(label);
let ranges = value_labels_ranges.entry(label).or_insert_with(|| vec![]);
let prog_point_to_inst = |prog_point: ProgPoint| {
let mut inst = prog_point.inst();
if prog_point.pos() == InstPosition::After {
inst = inst.next();
}
inst.index()
};
let inst_to_offset = |inst_index: usize| {
for offset in &inst_offsets[inst_index..] {
if *offset != NO_INST_OFFSET {
return *offset;
}
}
func_body_len
};
let from_inst_index = prog_point_to_inst(from);
let to_inst_index = prog_point_to_inst(to);
let from_offset = inst_to_offset(from_inst_index);
let to_offset = inst_to_offset(to_inst_index);
if from_offset == to_offset {
continue;
}
let loc = if let Some(preg) = alloc.as_reg() {
LabelValueLoc::Reg(Reg::from(preg))
} else {
#[cfg(not(feature = "unwind"))]
continue;
#[cfg(feature = "unwind")]
{
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)
}
};
if let Some(last_loc_range) = ranges.last_mut() {
if last_loc_range.loc == loc && last_loc_range.end == from_offset {
trace!(
"Extending debug range for {:?} in {:?} to Inst {} ({})",
label, loc, to_inst_index, to_offset
);
last_loc_range.end = to_offset;
continue;
}
}
trace!(
"Recording debug range for {:?} in {:?}: [Inst {}..Inst {}) [{}..{})",
label, loc, from_inst_index, to_inst_index, from_offset, to_offset
);
ranges.push(ValueLocRange {
loc,
start: from_offset,
end: to_offset,
});
}
value_labels_ranges
}
fn log_value_labels_ranges(&self, regalloc: ®alloc2::Output, inst_offsets: &[CodeOffset]) {
debug_assert!(trace_log_enabled!());
let mut labels = vec![];
for &(label, _, _, _) in ®alloc.debug_locations {
if Some(&label) == labels.last() {
continue;
}
labels.push(label);
}
let mut vregs = vec![];
for &(vreg, start, end, label) in &self.debug_value_labels {
if matches!(labels.binary_search(&label), Ok(_)) {
vregs.push((label, start, end, vreg));
}
}
vregs.sort_unstable_by(
|(l_label, l_start, _, _), (r_label, r_start, _, _)| match l_label.cmp(r_label) {
Ordering::Equal => l_start.cmp(r_start),
cmp => cmp,
},
);
#[derive(PartialEq)]
enum Mode {
Measure,
Emit,
}
#[derive(PartialEq)]
enum Row {
Head,
Line,
Inst(usize, usize),
}
let mut widths = vec![0; 3 + 2 * labels.len()];
let mut row = String::new();
let mut output_row = |row_kind: Row, mode: Mode| {
let mut column_index = 0;
row.clear();
macro_rules! output_cell_impl {
($fill:literal, $span:literal, $($cell_fmt:tt)*) => {
let column_start = row.len();
{
row.push('|');
write!(row, $($cell_fmt)*).unwrap();
}
let next_column_index = column_index + $span;
let expected_width: usize = widths[column_index..next_column_index].iter().sum();
if mode == Mode::Measure {
let actual_width = row.len() - column_start;
if actual_width > expected_width {
widths[next_column_index - 1] += actual_width - expected_width;
}
} else {
let column_end = column_start + expected_width;
while row.len() != column_end {
row.push($fill);
}
}
column_index = next_column_index;
};
}
macro_rules! output_cell {
($($cell_fmt:tt)*) => {
output_cell_impl!(' ', 1, $($cell_fmt)*);
};
}
match row_kind {
Row::Head => {
output_cell!("BB");
output_cell!("Inst");
output_cell!("IP");
for label in &labels {
output_cell_impl!(' ', 2, "{:?}", ValueLabel::from_u32(*label));
}
}
Row::Line => {
debug_assert!(mode == Mode::Emit);
for _ in 0..3 {
output_cell_impl!('-', 1, "");
}
for _ in &labels {
output_cell_impl!('-', 2, "");
}
}
Row::Inst(block_index, inst_index) => {
debug_assert!(inst_index < self.num_insts());
if self.block_ranges.get(block_index).start == inst_index {
output_cell!("B{}", block_index);
} else {
output_cell!("");
}
output_cell!("Inst {inst_index} ");
output_cell!("{} ", inst_offsets[inst_index]);
for label in &labels {
use regalloc2::Inst;
let vreg_cmp = |inst: usize,
vreg_label: &u32,
range_start: &Inst,
range_end: &Inst| {
match vreg_label.cmp(&label) {
Ordering::Equal => {
if range_end.index() <= inst {
Ordering::Less
} else if range_start.index() > inst {
Ordering::Greater
} else {
Ordering::Equal
}
}
cmp => cmp,
}
};
let vreg_index =
vregs.binary_search_by(|(l, s, e, _)| vreg_cmp(inst_index, l, s, e));
if let Ok(vreg_index) = vreg_index {
let mut prev_vreg = None;
if inst_index > 0 {
let prev_vreg_index = vregs.binary_search_by(|(l, s, e, _)| {
vreg_cmp(inst_index - 1, l, s, e)
});
if let Ok(prev_vreg_index) = prev_vreg_index {
prev_vreg = Some(vregs[prev_vreg_index].3);
}
}
let vreg = vregs[vreg_index].3;
if Some(vreg) == prev_vreg {
output_cell!("*");
} else {
output_cell!("{}", vreg);
}
} else {
output_cell!("");
}
let inst_prog_point = ProgPoint::before(Inst::new(inst_index));
let range_index = regalloc.debug_locations.binary_search_by(
|(range_label, range_start, range_end, _)| match range_label.cmp(label)
{
Ordering::Equal => {
if *range_end <= inst_prog_point {
Ordering::Less
} else if *range_start > inst_prog_point {
Ordering::Greater
} else {
Ordering::Equal
}
}
cmp => cmp,
},
);
if let Ok(range_index) = range_index {
if let Some(reg) = regalloc.debug_locations[range_index].3.as_reg() {
output_cell!("{:?}", Reg::from(reg));
} else {
output_cell!("Stk");
}
} else {
output_cell!("");
}
}
}
}
row.push('|');
if mode == Mode::Emit {
trace!("{}", row.as_str());
}
};
for block_index in 0..self.num_blocks() {
for inst_index in self.block_ranges.get(block_index) {
output_row(Row::Inst(block_index, inst_index), Mode::Measure);
}
}
output_row(Row::Head, Mode::Measure);
output_row(Row::Head, Mode::Emit);
output_row(Row::Line, Mode::Emit);
for block_index in 0..self.num_blocks() {
for inst_index in self.block_ranges.get(block_index) {
output_row(Row::Inst(block_index, inst_index), Mode::Emit);
}
}
}
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> core::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::Branch => false,
}
}
fn is_branch(&self, insn: InsnIndex) -> bool {
match self.insts[insn.index()].is_term() {
MachTerminator::Branch => 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 check = |vreg: regalloc2::VReg| -> CodegenResult<Reg> {
Reg::from_virtual_reg_checked(vreg).ok_or(CodegenError::CodeTooLarge)
};
let regs: ValueRegs<Reg> = match regclasses {
&[rc0] => ValueRegs::one(check(VReg::new(v, rc0))?),
&[rc0, rc1] => ValueRegs::two(check(VReg::new(v, rc0))?, check(VReg::new(v + 1, rc1))?),
_ => 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 core::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>(), 3 * size_of::<usize>());
assert_eq!(size_of::<VCodeConstantData>(), 4 * size_of::<usize>());
assert_eq!(
size_of::<PrimaryMap<VCodeConstant, VCodeConstantData>>(),
3 * size_of::<usize>()
);
}
}