Skip to content

Commit

Permalink
Remove CPlace::inner and make CPlaceInner private
Browse files Browse the repository at this point in the history
This makes it easier to add and remove variants as necessary
  • Loading branch information
bjorn3 committed Mar 27, 2023
1 parent e564790 commit b6cf0a6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
31 changes: 1 addition & 30 deletions src/abi/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::borrow::Cow;
use rustc_middle::mir;
use rustc_target::abi::call::PassMode;

use cranelift_codegen::entity::EntityRef;

use crate::prelude::*;

pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
Expand Down Expand Up @@ -91,34 +89,7 @@ pub(super) fn add_local_place_comments<'tcx>(
largest_niche: _,
} = layout.0.0;

let (kind, extra) = match *place.inner() {
CPlaceInner::Var(place_local, var) => {
assert_eq!(local, place_local);
("ssa", Cow::Owned(format!(",var={}", var.index())))
}
CPlaceInner::VarPair(place_local, var1, var2) => {
assert_eq!(local, place_local);
("ssa", Cow::Owned(format!("var=({}, {})", var1.index(), var2.index())))
}
CPlaceInner::Addr(ptr, meta) => {
let meta = if let Some(meta) = meta {
Cow::Owned(format!("meta={}", meta))
} else {
Cow::Borrowed("")
};
match ptr.debug_base_and_offset() {
(crate::pointer::PointerBase::Addr(addr), offset) => {
("reuse", format!("storage={}{}{}", addr, offset, meta).into())
}
(crate::pointer::PointerBase::Stack(stack_slot), offset) => {
("stack", format!("storage={}{}{}", stack_slot, offset, meta).into())
}
(crate::pointer::PointerBase::Dangling(align), offset) => {
("zst", format!("align={},offset={}", align.bytes(), offset).into())
}
}
}
};
let (kind, extra) = place.debug_comment();

fx.add_global_comment(format!(
"{:<5} {:5} {:30} {:4}b {}, {}{}{}",
Expand Down
4 changes: 2 additions & 2 deletions src/abi/returning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
let (ret_temp_place, return_ptr) = match ret_arg_abi.mode {
PassMode::Ignore => (None, None),
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
if matches!(ret_place.inner(), CPlaceInner::Addr(_, None)) {
if let Some(ret_ptr) = ret_place.try_to_ptr() {
// This is an optimization to prevent unnecessary copies of the return value when
// the return place is already a memory place as opposed to a register.
// This match arm can be safely removed.
(None, Some(ret_place.to_ptr().get_addr(fx)))
(None, Some(ret_ptr.get_addr(fx)))
} else {
let place = CPlace::new_stack_slot(fx, ret_arg_abi.layout);
(Some(place), Some(place.to_ptr().get_addr(fx)))
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod prelude {
pub(crate) use crate::common::*;
pub(crate) use crate::debuginfo::{DebugContext, UnwindContext};
pub(crate) use crate::pointer::Pointer;
pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
pub(crate) use crate::value_and_place::{CPlace, CValue};
}

struct PrintOnPanic<F: Fn() -> String>(F);
Expand Down
41 changes: 35 additions & 6 deletions src/value_and_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::prelude::*;

use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::immediates::Offset32;

fn codegen_field<'tcx>(
Expand Down Expand Up @@ -325,7 +326,7 @@ pub(crate) struct CPlace<'tcx> {
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum CPlaceInner {
enum CPlaceInner {
Var(Local, Variable),
VarPair(Local, Variable, Variable),
Addr(Pointer, Option<Value>),
Expand All @@ -336,10 +337,6 @@ impl<'tcx> CPlace<'tcx> {
self.layout
}

pub(crate) fn inner(&self) -> &CPlaceInner {
&self.inner
}

pub(crate) fn new_stack_slot(
fx: &mut FunctionCx<'_, '_, 'tcx>,
layout: TyAndLayout<'tcx>,
Expand Down Expand Up @@ -431,6 +428,30 @@ impl<'tcx> CPlace<'tcx> {
}
}

pub(crate) fn debug_comment(self) -> (&'static str, String) {
match self.inner {
CPlaceInner::Var(_local, var) => ("ssa", format!("var={}", var.index())),
CPlaceInner::VarPair(_local, var1, var2) => {
("ssa", format!("var=({}, {})", var1.index(), var2.index()))
}
CPlaceInner::Addr(ptr, meta) => {
let meta =
if let Some(meta) = meta { format!(",meta={}", meta) } else { String::new() };
match ptr.debug_base_and_offset() {
(crate::pointer::PointerBase::Addr(addr), offset) => {
("reuse", format!("storage={}{}{}", addr, offset, meta))
}
(crate::pointer::PointerBase::Stack(stack_slot), offset) => {
("stack", format!("storage={}{}{}", stack_slot, offset, meta))
}
(crate::pointer::PointerBase::Dangling(align), offset) => {
("zst", format!("align={},offset={}", align.bytes(), offset))
}
}
}
}
}

#[track_caller]
pub(crate) fn to_ptr(self) -> Pointer {
match self.to_ptr_maybe_unsized() {
Expand All @@ -449,6 +470,14 @@ impl<'tcx> CPlace<'tcx> {
}
}

pub(crate) fn try_to_ptr(self) -> Option<Pointer> {
match self.inner {
CPlaceInner::Var(_, _) | CPlaceInner::VarPair(_, _, _) => None,
CPlaceInner::Addr(ptr, None) => Some(ptr),
CPlaceInner::Addr(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
}
}

pub(crate) fn write_cvalue(self, fx: &mut FunctionCx<'_, '_, 'tcx>, from: CValue<'tcx>) {
assert_assignable(fx, from.layout().ty, self.layout().ty, 16);

Expand Down Expand Up @@ -527,7 +556,7 @@ impl<'tcx> CPlace<'tcx> {
format!(
"{}: {:?}: {:?} <- {:?}: {:?}",
method,
self.inner(),
self.inner,
self.layout().ty,
from.0,
from.layout().ty
Expand Down

0 comments on commit b6cf0a6

Please sign in to comment.