2020-01-01 19:25:28 +01:00
|
|
|
use super::operand::OperandRef;
|
|
|
|
use super::operand::OperandValue::{Immediate, Pair, Ref};
|
|
|
|
use super::place::PlaceRef;
|
2022-10-17 08:29:40 +11:00
|
|
|
use super::{CachedLlbb, FunctionCx, LocalRef};
|
2020-01-01 19:25:28 +01:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
use crate::base;
|
|
|
|
use crate::common::{self, IntPredicate};
|
2019-02-09 23:31:47 +09:00
|
|
|
use crate::meth;
|
2020-01-01 19:25:28 +01:00
|
|
|
use crate::traits::*;
|
2019-02-09 23:31:47 +09:00
|
|
|
use crate::MemFlags;
|
2020-01-01 19:25:28 +01:00
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2021-09-04 19:25:09 +02:00
|
|
|
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::vec::Idx;
|
2022-08-23 00:07:26 +00:00
|
|
|
use rustc_middle::mir::{self, AssertKind, SwitchTargets};
|
2021-09-02 00:09:34 +03:00
|
|
|
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
|
2021-09-20 20:22:55 +04:00
|
|
|
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, Instance, Ty, TypeVisitableExt};
|
Use `br` instead of `switch` in more cases.
`codegen_switchint_terminator` already uses `br` instead of `switch`
when there is one normal target plus the `otherwise` target. But there's
another common case with two normal targets and an `otherwise` target
that points to an empty unreachable BB. This comes up a lot when
switching on the tags of enums that use niches.
The pattern looks like this:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
switch i64 %_6, label %bb3 [
i64 0, label %bb4
i64 1, label %bb2
]
bb3: ; preds = %bb1
unreachable
```
This commit adds code to convert the `switch` to a `br`:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
%6 = icmp eq i64 %_6, 0
br i1 %6, label %bb4, label %bb2
bb3: ; No predecessors!
unreachable
```
This has a surprisingly large effect on compile times, with reductions
of 5% on debug builds of some crates. The reduction is all due to LLVM
taking less time. Maybe LLVM is just much better at handling `br` than
`switch`.
The resulting code is still suboptimal.
- The `icmp`, `select`, `icmp` sequence is silly, converting an `i1` to an `i64`
and back to an `i1`. But with the current code structure it's hard to avoid,
and LLVM will easily clean it up, in opt builds at least.
- `bb3` is usually now truly dead code (though not always, so it can't
be removed universally).
2022-10-20 18:59:07 +11:00
|
|
|
use rustc_session::config::OptLevel;
|
2020-07-08 11:04:10 +10:00
|
|
|
use rustc_span::source_map::Span;
|
|
|
|
use rustc_span::{sym, Symbol};
|
2022-03-31 22:50:41 -07:00
|
|
|
use rustc_symbol_mangling::typeid::typeid_for_fnabi;
|
2022-08-25 19:18:01 +10:00
|
|
|
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode, Reg};
|
2022-07-14 22:42:47 +01:00
|
|
|
use rustc_target::abi::{self, HasDataLayout, WrappingRange};
|
2018-10-03 13:49:57 +02:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
// Indicates if we are in the middle of merging a BB's successor into it. This
|
|
|
|
// can happen when BB jumps directly to its successor and the successor has no
|
|
|
|
// other predecessors.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum MergingSucc {
|
|
|
|
False,
|
|
|
|
True,
|
|
|
|
}
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Used by `FunctionCx::codegen_terminator` for emitting common patterns
|
|
|
|
/// e.g., creating a basic block, calling a function, etc.
|
2019-10-29 16:24:25 +02:00
|
|
|
struct TerminatorCodegenHelper<'tcx> {
|
|
|
|
bb: mir::BasicBlock,
|
|
|
|
terminator: &'tcx mir::Terminator<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:11:11 +02:00
|
|
|
impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
|
2021-05-15 09:17:46 +03:00
|
|
|
/// Returns the appropriate `Funclet` for the current funclet, if on MSVC,
|
|
|
|
/// either already previously cached, or newly created, by `landing_pad_for`.
|
2019-10-29 18:11:11 +02:00
|
|
|
fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2021-05-15 09:17:46 +03:00
|
|
|
fx: &'b mut FunctionCx<'a, 'tcx, Bx>,
|
2019-10-29 18:11:11 +02:00
|
|
|
) -> Option<&'b Bx::Funclet> {
|
2023-01-17 00:00:00 +00:00
|
|
|
let cleanup_kinds = (&fx.cleanup_kinds).as_ref()?;
|
|
|
|
let funclet_bb = cleanup_kinds[self.bb].funclet_bb(self.bb)?;
|
|
|
|
// If `landing_pad_for` hasn't been called yet to create the `Funclet`,
|
|
|
|
// it has to be now. This may not seem necessary, as RPO should lead
|
|
|
|
// to all the unwind edges being visited (and so to `landing_pad_for`
|
|
|
|
// getting called for them), before building any of the blocks inside
|
|
|
|
// the funclet itself - however, if MIR contains edges that end up not
|
|
|
|
// being needed in the LLVM IR after monomorphization, the funclet may
|
|
|
|
// be unreachable, and we don't have yet a way to skip building it in
|
|
|
|
// such an eventuality (which may be a better solution than this).
|
|
|
|
if fx.funclets[funclet_bb].is_none() {
|
|
|
|
fx.landing_pad_for(funclet_bb);
|
2021-05-15 09:17:46 +03:00
|
|
|
}
|
2023-01-17 00:00:00 +00:00
|
|
|
Some(
|
|
|
|
fx.funclets[funclet_bb]
|
|
|
|
.as_ref()
|
|
|
|
.expect("landing_pad_for didn't also create funclets entry"),
|
|
|
|
)
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2022-10-19 10:53:38 +11:00
|
|
|
/// Get a basic block (creating it if necessary), possibly with cleanup
|
|
|
|
/// stuff in it or next to it.
|
|
|
|
fn llbb_with_cleanup<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-11-04 19:52:19 -05:00
|
|
|
target: mir::BasicBlock,
|
2019-02-02 16:34:09 +00:00
|
|
|
) -> Bx::BasicBlock {
|
2022-10-17 08:29:40 +11:00
|
|
|
let (needs_landing_pad, is_cleanupret) = self.llbb_characteristics(fx, target);
|
|
|
|
let mut lltarget = fx.llbb(target);
|
|
|
|
if needs_landing_pad {
|
|
|
|
lltarget = fx.landing_pad_for(target);
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
if is_cleanupret {
|
|
|
|
// MSVC cross-funclet jump - need a trampoline
|
2022-10-19 10:59:02 +11:00
|
|
|
debug_assert!(base::wants_msvc_seh(fx.cx.tcx().sess));
|
2022-10-19 10:53:38 +11:00
|
|
|
debug!("llbb_with_cleanup: creating cleanup trampoline for {:?}", target);
|
2019-02-02 16:34:09 +00:00
|
|
|
let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target);
|
2022-10-19 10:34:45 +11:00
|
|
|
let trampoline_llbb = Bx::append_block(fx.cx, fx.llfn, name);
|
|
|
|
let mut trampoline_bx = Bx::build(fx.cx, trampoline_llbb);
|
2022-02-18 15:20:19 +01:00
|
|
|
trampoline_bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
|
2022-10-19 10:34:45 +11:00
|
|
|
trampoline_llbb
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
lltarget
|
|
|
|
}
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
fn llbb_characteristics<Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
&self,
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
|
|
|
target: mir::BasicBlock,
|
|
|
|
) -> (bool, bool) {
|
2023-01-17 00:00:00 +00:00
|
|
|
if let Some(ref cleanup_kinds) = fx.cleanup_kinds {
|
|
|
|
let funclet_bb = cleanup_kinds[self.bb].funclet_bb(self.bb);
|
|
|
|
let target_funclet = cleanup_kinds[target].funclet_bb(target);
|
|
|
|
let (needs_landing_pad, is_cleanupret) = match (funclet_bb, target_funclet) {
|
|
|
|
(None, None) => (false, false),
|
|
|
|
(None, Some(_)) => (true, false),
|
|
|
|
(Some(f), Some(t_f)) => (f != t_f, f != t_f),
|
|
|
|
(Some(_), None) => {
|
|
|
|
let span = self.terminator.source_info.span;
|
|
|
|
span_bug!(span, "{:?} - jump out of cleanup?", self.terminator);
|
2022-10-17 08:29:40 +11:00
|
|
|
}
|
2023-01-17 00:00:00 +00:00
|
|
|
};
|
|
|
|
(needs_landing_pad, is_cleanupret)
|
|
|
|
} else {
|
|
|
|
let needs_landing_pad = !fx.mir[self.bb].is_cleanup && fx.mir[target].is_cleanup;
|
|
|
|
let is_cleanupret = false;
|
|
|
|
(needs_landing_pad, is_cleanupret)
|
|
|
|
}
|
2022-10-17 08:29:40 +11:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:11:11 +02:00
|
|
|
fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
bx: &mut Bx,
|
|
|
|
target: mir::BasicBlock,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
|
|
|
let (needs_landing_pad, is_cleanupret) = self.llbb_characteristics(fx, target);
|
|
|
|
if mergeable_succ && !needs_landing_pad && !is_cleanupret {
|
|
|
|
// We can merge the successor into this bb, so no need for a `br`.
|
|
|
|
MergingSucc::True
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
2022-10-17 08:29:40 +11:00
|
|
|
let mut lltarget = fx.llbb(target);
|
|
|
|
if needs_landing_pad {
|
|
|
|
lltarget = fx.landing_pad_for(target);
|
|
|
|
}
|
|
|
|
if is_cleanupret {
|
|
|
|
// micro-optimization: generate a `ret` rather than a jump
|
|
|
|
// to a trampoline.
|
|
|
|
bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget));
|
|
|
|
} else {
|
|
|
|
bx.br(lltarget);
|
|
|
|
}
|
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 23:47:15 +03:00
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
/// Call `fn_ptr` of `fn_abi` with the arguments `llargs`, the optional
|
2019-02-02 16:34:09 +00:00
|
|
|
/// return destination `destination` and the cleanup function `cleanup`.
|
2019-10-29 18:11:11 +02:00
|
|
|
fn do_call<Bx: BuilderMethods<'a, 'tcx>>(
|
2019-02-02 16:34:09 +00:00
|
|
|
&self,
|
2019-10-29 18:11:11 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
bx: &mut Bx,
|
2021-08-26 21:58:34 +03:00
|
|
|
fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
|
2019-02-02 16:34:09 +00:00
|
|
|
fn_ptr: Bx::Value,
|
|
|
|
llargs: &[Bx::Value],
|
|
|
|
destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
2022-06-25 16:36:11 +02:00
|
|
|
copied_constant_arguments: &[PlaceRef<'tcx, <Bx as BackendTypes>::Value>],
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
2020-03-26 05:32:52 -04:00
|
|
|
// If there is a cleanup block and the function we're calling can unwind, then
|
|
|
|
// do an invoke, otherwise do a call.
|
2021-08-03 15:09:57 -07:00
|
|
|
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
|
2022-01-14 23:54:26 +00:00
|
|
|
|
|
|
|
let unwind_block = if let Some(cleanup) = cleanup.filter(|_| fn_abi.can_unwind) {
|
2022-10-19 10:53:38 +11:00
|
|
|
Some(self.llbb_with_cleanup(fx, cleanup))
|
2022-01-14 23:54:26 +00:00
|
|
|
} else if fx.mir[self.bb].is_cleanup
|
|
|
|
&& fn_abi.can_unwind
|
|
|
|
&& !base::wants_msvc_seh(fx.cx.tcx().sess)
|
|
|
|
{
|
|
|
|
// Exception must not propagate out of the execution of a cleanup (doing so
|
|
|
|
// can cause undefined behaviour). We insert a double unwind guard for
|
|
|
|
// functions that can potentially unwind to protect against this.
|
|
|
|
//
|
|
|
|
// This is not necessary for SEH which does not use successive unwinding
|
|
|
|
// like Itanium EH. EH frames in SEH are different from normal function
|
|
|
|
// frames and SEH will abort automatically if an exception tries to
|
|
|
|
// propagate out from cleanup.
|
|
|
|
Some(fx.double_unwind_guard())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(unwind_block) = unwind_block {
|
2021-05-06 17:37:19 +03:00
|
|
|
let ret_llbb = if let Some((_, target)) = destination {
|
|
|
|
fx.llbb(target)
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
fx.unreachable_block()
|
|
|
|
};
|
2022-10-01 17:01:31 +00:00
|
|
|
let invokeret = bx.invoke(
|
|
|
|
fn_ty,
|
|
|
|
Some(&fn_abi),
|
|
|
|
fn_ptr,
|
|
|
|
&llargs,
|
|
|
|
ret_llbb,
|
|
|
|
unwind_block,
|
|
|
|
self.funclet(fx),
|
|
|
|
);
|
2022-01-14 23:54:26 +00:00
|
|
|
if fx.mir[self.bb].is_cleanup {
|
2022-02-26 12:52:07 -05:00
|
|
|
bx.do_not_inline(invokeret);
|
2022-01-14 23:54:26 +00:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
if let Some((ret_dest, target)) = destination {
|
2022-02-18 15:37:31 +01:00
|
|
|
bx.switch_to_block(fx.llbb(target));
|
|
|
|
fx.set_debug_loc(bx, self.terminator.source_info);
|
2022-06-25 16:36:11 +02:00
|
|
|
for tmp in copied_constant_arguments {
|
2022-06-22 11:24:34 +02:00
|
|
|
bx.lifetime_end(tmp.llval, tmp.layout.size);
|
|
|
|
}
|
2022-02-18 15:37:31 +01:00
|
|
|
fx.store_return(bx, ret_dest, &fn_abi.ret, invokeret);
|
2018-09-20 15:47:22 +02:00
|
|
|
}
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
2022-10-01 17:01:31 +00:00
|
|
|
let llret = bx.call(fn_ty, Some(&fn_abi), fn_ptr, &llargs, self.funclet(fx));
|
2019-10-29 16:24:25 +02:00
|
|
|
if fx.mir[self.bb].is_cleanup {
|
2022-02-26 12:52:07 -05:00
|
|
|
// Cleanup is always the cold path. Don't inline
|
|
|
|
// drop glue. Also, when there is a deeply-nested
|
|
|
|
// struct, there are "symmetry" issues that cause
|
|
|
|
// exponential inlining - see issue #41696.
|
|
|
|
bx.do_not_inline(llret);
|
2016-05-29 22:01:06 +03:00
|
|
|
}
|
2017-05-23 23:47:15 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Some((ret_dest, target)) = destination {
|
2022-06-25 16:36:11 +02:00
|
|
|
for tmp in copied_constant_arguments {
|
2022-06-22 11:24:34 +02:00
|
|
|
bx.lifetime_end(tmp.llval, tmp.layout.size);
|
|
|
|
}
|
2019-10-29 16:35:26 +02:00
|
|
|
fx.store_return(bx, ret_dest, &fn_abi.ret, llret);
|
2022-10-17 08:29:40 +11:00
|
|
|
self.funclet_br(fx, bx, target, mergeable_succ)
|
2017-05-23 23:47:15 +03:00
|
|
|
} else {
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.unreachable();
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2017-05-23 23:47:15 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-04 19:25:09 +02:00
|
|
|
|
|
|
|
/// Generates inline assembly with optional `destination` and `cleanup`.
|
|
|
|
fn do_inlineasm<Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
&self,
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, Bx>,
|
|
|
|
bx: &mut Bx,
|
|
|
|
template: &[InlineAsmTemplatePiece],
|
|
|
|
operands: &[InlineAsmOperandRef<'tcx, Bx>],
|
|
|
|
options: InlineAsmOptions,
|
|
|
|
line_spans: &[Span],
|
|
|
|
destination: Option<mir::BasicBlock>,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
|
|
|
instance: Instance<'_>,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
2021-09-04 19:25:09 +02:00
|
|
|
if let Some(cleanup) = cleanup {
|
|
|
|
let ret_llbb = if let Some(target) = destination {
|
|
|
|
fx.llbb(target)
|
|
|
|
} else {
|
|
|
|
fx.unreachable_block()
|
|
|
|
};
|
|
|
|
|
|
|
|
bx.codegen_inline_asm(
|
|
|
|
template,
|
|
|
|
&operands,
|
|
|
|
options,
|
|
|
|
line_spans,
|
|
|
|
instance,
|
2022-10-19 10:53:38 +11:00
|
|
|
Some((ret_llbb, self.llbb_with_cleanup(fx, cleanup), self.funclet(fx))),
|
2021-09-04 19:25:09 +02:00
|
|
|
);
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2021-09-04 19:25:09 +02:00
|
|
|
} else {
|
|
|
|
bx.codegen_inline_asm(template, &operands, options, line_spans, instance, None);
|
|
|
|
|
|
|
|
if let Some(target) = destination {
|
2022-10-17 08:29:40 +11:00
|
|
|
self.funclet_br(fx, bx, target, mergeable_succ)
|
2021-09-04 19:25:09 +02:00
|
|
|
} else {
|
|
|
|
bx.unreachable();
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2021-09-04 19:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-02-11 22:57:09 +02:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Codegen implementations for some terminator variants.
|
2019-10-26 01:41:17 -04:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2019-02-02 16:34:09 +00:00
|
|
|
/// Generates code for a `Resume` terminator.
|
2022-11-09 11:04:10 +11:00
|
|
|
fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, bx: &mut Bx) {
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Some(funclet) = helper.funclet(self) {
|
|
|
|
bx.cleanup_ret(funclet, None);
|
|
|
|
} else {
|
2022-11-09 11:04:10 +11:00
|
|
|
let slot = self.get_personality_slot(bx);
|
2022-12-03 18:27:18 +00:00
|
|
|
let exn0 = slot.project_field(bx, 0);
|
|
|
|
let exn0 = bx.load_operand(exn0).immediate();
|
|
|
|
let exn1 = slot.project_field(bx, 1);
|
|
|
|
let exn1 = bx.load_operand(exn1).immediate();
|
2022-11-09 11:04:10 +11:00
|
|
|
slot.storage_dead(bx);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2022-12-03 18:27:18 +00:00
|
|
|
bx.resume(exn0, exn1);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_switchint_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2019-02-02 16:34:09 +00:00
|
|
|
discr: &mir::Operand<'tcx>,
|
2020-10-11 01:14:12 +02:00
|
|
|
targets: &SwitchTargets,
|
2019-02-02 16:34:09 +00:00
|
|
|
) {
|
2022-11-09 11:04:10 +11:00
|
|
|
let discr = self.codegen_operand(bx, &discr);
|
2022-12-03 16:03:27 -08:00
|
|
|
let switch_ty = discr.layout.ty;
|
2020-10-10 17:36:04 +02:00
|
|
|
let mut target_iter = targets.iter();
|
|
|
|
if target_iter.len() == 1 {
|
Use `br` instead of `switch` in more cases.
`codegen_switchint_terminator` already uses `br` instead of `switch`
when there is one normal target plus the `otherwise` target. But there's
another common case with two normal targets and an `otherwise` target
that points to an empty unreachable BB. This comes up a lot when
switching on the tags of enums that use niches.
The pattern looks like this:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
switch i64 %_6, label %bb3 [
i64 0, label %bb4
i64 1, label %bb2
]
bb3: ; preds = %bb1
unreachable
```
This commit adds code to convert the `switch` to a `br`:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
%6 = icmp eq i64 %_6, 0
br i1 %6, label %bb4, label %bb2
bb3: ; No predecessors!
unreachable
```
This has a surprisingly large effect on compile times, with reductions
of 5% on debug builds of some crates. The reduction is all due to LLVM
taking less time. Maybe LLVM is just much better at handling `br` than
`switch`.
The resulting code is still suboptimal.
- The `icmp`, `select`, `icmp` sequence is silly, converting an `i1` to an `i64`
and back to an `i1`. But with the current code structure it's hard to avoid,
and LLVM will easily clean it up, in opt builds at least.
- `bb3` is usually now truly dead code (though not always, so it can't
be removed universally).
2022-10-20 18:59:07 +11:00
|
|
|
// If there are two targets (one conditional, one fallback), emit `br` instead of
|
|
|
|
// `switch`.
|
2020-10-10 17:36:04 +02:00
|
|
|
let (test_value, target) = target_iter.next().unwrap();
|
2022-10-19 10:53:38 +11:00
|
|
|
let lltrue = helper.llbb_with_cleanup(self, target);
|
|
|
|
let llfalse = helper.llbb_with_cleanup(self, targets.otherwise());
|
2019-02-02 16:34:09 +00:00
|
|
|
if switch_ty == bx.tcx().types.bool {
|
Use `br` instead of `switch` in more cases.
`codegen_switchint_terminator` already uses `br` instead of `switch`
when there is one normal target plus the `otherwise` target. But there's
another common case with two normal targets and an `otherwise` target
that points to an empty unreachable BB. This comes up a lot when
switching on the tags of enums that use niches.
The pattern looks like this:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
switch i64 %_6, label %bb3 [
i64 0, label %bb4
i64 1, label %bb2
]
bb3: ; preds = %bb1
unreachable
```
This commit adds code to convert the `switch` to a `br`:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
%6 = icmp eq i64 %_6, 0
br i1 %6, label %bb4, label %bb2
bb3: ; No predecessors!
unreachable
```
This has a surprisingly large effect on compile times, with reductions
of 5% on debug builds of some crates. The reduction is all due to LLVM
taking less time. Maybe LLVM is just much better at handling `br` than
`switch`.
The resulting code is still suboptimal.
- The `icmp`, `select`, `icmp` sequence is silly, converting an `i1` to an `i64`
and back to an `i1`. But with the current code structure it's hard to avoid,
and LLVM will easily clean it up, in opt builds at least.
- `bb3` is usually now truly dead code (though not always, so it can't
be removed universally).
2022-10-20 18:59:07 +11:00
|
|
|
// Don't generate trivial icmps when switching on bool.
|
2020-10-10 17:36:04 +02:00
|
|
|
match test_value {
|
|
|
|
0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
|
|
|
|
1 => bx.cond_br(discr.immediate(), lltrue, llfalse),
|
|
|
|
_ => bug!(),
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
let switch_llty = bx.immediate_backend_type(bx.layout_of(switch_ty));
|
2020-10-10 17:36:04 +02:00
|
|
|
let llval = bx.const_uint_big(switch_llty, test_value);
|
2019-02-02 16:34:09 +00:00
|
|
|
let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval);
|
|
|
|
bx.cond_br(cmp, lltrue, llfalse);
|
2017-05-11 02:01:25 +03:00
|
|
|
}
|
Use `br` instead of `switch` in more cases.
`codegen_switchint_terminator` already uses `br` instead of `switch`
when there is one normal target plus the `otherwise` target. But there's
another common case with two normal targets and an `otherwise` target
that points to an empty unreachable BB. This comes up a lot when
switching on the tags of enums that use niches.
The pattern looks like this:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
switch i64 %_6, label %bb3 [
i64 0, label %bb4
i64 1, label %bb2
]
bb3: ; preds = %bb1
unreachable
```
This commit adds code to convert the `switch` to a `br`:
```
bb1: ; preds = %bb6
%3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
%4 = sub i8 %3, 2
%5 = icmp eq i8 %4, 0
%_6 = select i1 %5, i64 0, i64 1
%6 = icmp eq i64 %_6, 0
br i1 %6, label %bb4, label %bb2
bb3: ; No predecessors!
unreachable
```
This has a surprisingly large effect on compile times, with reductions
of 5% on debug builds of some crates. The reduction is all due to LLVM
taking less time. Maybe LLVM is just much better at handling `br` than
`switch`.
The resulting code is still suboptimal.
- The `icmp`, `select`, `icmp` sequence is silly, converting an `i1` to an `i64`
and back to an `i1`. But with the current code structure it's hard to avoid,
and LLVM will easily clean it up, in opt builds at least.
- `bb3` is usually now truly dead code (though not always, so it can't
be removed universally).
2022-10-20 18:59:07 +11:00
|
|
|
} else if self.cx.sess().opts.optimize == OptLevel::No
|
|
|
|
&& target_iter.len() == 2
|
|
|
|
&& self.mir[targets.otherwise()].is_empty_unreachable()
|
|
|
|
{
|
|
|
|
// In unoptimized builds, if there are two normal targets and the `otherwise` target is
|
|
|
|
// an unreachable BB, emit `br` instead of `switch`. This leaves behind the unreachable
|
|
|
|
// BB, which will usually (but not always) be dead code.
|
|
|
|
//
|
|
|
|
// Why only in unoptimized builds?
|
|
|
|
// - In unoptimized builds LLVM uses FastISel which does not support switches, so it
|
|
|
|
// must fall back to the to the slower SelectionDAG isel. Therefore, using `br` gives
|
|
|
|
// significant compile time speedups for unoptimized builds.
|
|
|
|
// - In optimized builds the above doesn't hold, and using `br` sometimes results in
|
|
|
|
// worse generated code because LLVM can no longer tell that the value being switched
|
|
|
|
// on can only have two values, e.g. 0 and 1.
|
|
|
|
//
|
|
|
|
let (test_value1, target1) = target_iter.next().unwrap();
|
|
|
|
let (_test_value2, target2) = target_iter.next().unwrap();
|
|
|
|
let ll1 = helper.llbb_with_cleanup(self, target1);
|
|
|
|
let ll2 = helper.llbb_with_cleanup(self, target2);
|
|
|
|
let switch_llty = bx.immediate_backend_type(bx.layout_of(switch_ty));
|
|
|
|
let llval = bx.const_uint_big(switch_llty, test_value1);
|
|
|
|
let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval);
|
|
|
|
bx.cond_br(cmp, ll1, ll2);
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
2018-12-08 18:42:31 +01:00
|
|
|
bx.switch(
|
|
|
|
discr.immediate(),
|
2022-10-19 10:53:38 +11:00
|
|
|
helper.llbb_with_cleanup(self, targets.otherwise()),
|
|
|
|
target_iter.map(|(value, target)| (value, helper.llbb_with_cleanup(self, target))),
|
2019-02-02 16:34:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
fn codegen_return_terminator(&mut self, bx: &mut Bx) {
|
2019-08-12 15:27:31 +03:00
|
|
|
// Call `va_end` if this is the definition of a C-variadic function.
|
2019-10-29 16:35:26 +02:00
|
|
|
if self.fn_abi.c_variadic {
|
2019-08-12 15:27:31 +03:00
|
|
|
// The `VaList` "spoofed" argument is just after all the real arguments.
|
2019-10-29 16:35:26 +02:00
|
|
|
let va_list_arg_idx = self.fn_abi.args.len();
|
2019-08-12 15:27:31 +03:00
|
|
|
match self.locals[mir::Local::new(1 + va_list_arg_idx)] {
|
|
|
|
LocalRef::Place(va_list) => {
|
2019-03-07 03:50:50 +00:00
|
|
|
bx.va_end(va_list.llval);
|
|
|
|
}
|
2019-08-12 15:27:31 +03:00
|
|
|
_ => bug!("C-variadic function must have a `VaList` place"),
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 16:35:26 +02:00
|
|
|
if self.fn_abi.ret.layout.abi.is_uninhabited() {
|
2019-04-03 15:44:49 -07:00
|
|
|
// Functions with uninhabited return values are marked `noreturn`,
|
|
|
|
// so we should make sure that we never actually do.
|
2019-12-05 23:59:30 +01:00
|
|
|
// We play it safe by using a well-defined `abort`, but we could go for immediate UB
|
|
|
|
// if that turns out to be helpful.
|
2019-04-03 15:44:49 -07:00
|
|
|
bx.abort();
|
2019-12-05 23:59:30 +01:00
|
|
|
// `abort` does not terminate the block, so we still need to generate
|
|
|
|
// an `unreachable` terminator after it.
|
2019-04-03 15:44:49 -07:00
|
|
|
bx.unreachable();
|
|
|
|
return;
|
|
|
|
}
|
2022-08-25 17:52:37 +10:00
|
|
|
let llval = match &self.fn_abi.ret.mode {
|
2020-11-14 14:29:40 +01:00
|
|
|
PassMode::Ignore | PassMode::Indirect { .. } => {
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.ret_void();
|
|
|
|
return;
|
2016-02-11 22:57:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
PassMode::Direct(_) | PassMode::Pair(..) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let op = self.codegen_consume(bx, mir::Place::return_place().as_ref());
|
2019-02-02 16:34:09 +00:00
|
|
|
if let Ref(llval, _, align) = op.val {
|
2021-07-04 18:53:04 +02:00
|
|
|
bx.load(bx.backend_type(op.layout), llval, align)
|
2017-02-02 06:44:30 +02:00
|
|
|
} else {
|
2022-11-09 11:04:10 +11:00
|
|
|
op.immediate_or_packed_pair(bx)
|
2015-11-08 19:11:11 +01:00
|
|
|
}
|
2015-10-26 14:35:18 -04:00
|
|
|
}
|
|
|
|
|
2022-08-25 22:19:38 +10:00
|
|
|
PassMode::Cast(cast_ty, _) => {
|
2019-02-02 16:34:09 +00:00
|
|
|
let op = match self.locals[mir::RETURN_PLACE] {
|
|
|
|
LocalRef::Operand(Some(op)) => op,
|
|
|
|
LocalRef::Operand(None) => bug!("use of return before def"),
|
|
|
|
LocalRef::Place(cg_place) => OperandRef {
|
|
|
|
val: Ref(cg_place.llval, None, cg_place.align),
|
|
|
|
layout: cg_place.layout,
|
2018-11-30 15:53:44 +00:00
|
|
|
},
|
2019-02-02 16:34:09 +00:00
|
|
|
LocalRef::UnsizedPlace(_) => bug!("return type must be sized"),
|
|
|
|
};
|
|
|
|
let llslot = match op.val {
|
|
|
|
Immediate(_) | Pair(..) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let scratch = PlaceRef::alloca(bx, self.fn_abi.ret.layout);
|
|
|
|
op.val.store(bx, scratch);
|
2019-02-02 16:34:09 +00:00
|
|
|
scratch.llval
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
Ref(llval, _, align) => {
|
|
|
|
assert_eq!(align, op.layout.align.abi, "return place is unaligned!");
|
|
|
|
llval
|
2018-11-30 15:53:44 +00:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
};
|
2022-08-25 17:52:37 +10:00
|
|
|
let ty = bx.cast_backend_type(cast_ty);
|
2021-07-04 18:53:04 +02:00
|
|
|
let addr = bx.pointercast(llslot, bx.type_ptr_to(ty));
|
|
|
|
bx.load(ty, addr, self.fn_abi.ret.layout.align.abi)
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
bx.ret(llval);
|
|
|
|
}
|
2018-11-30 15:53:44 +00:00
|
|
|
|
2022-08-30 12:44:00 -07:00
|
|
|
#[tracing::instrument(level = "trace", skip(self, helper, bx))]
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_drop_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2020-03-31 14:27:48 -03:00
|
|
|
location: mir::Place<'tcx>,
|
2019-02-02 16:34:09 +00:00
|
|
|
target: mir::BasicBlock,
|
|
|
|
unwind: Option<mir::BasicBlock>,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
2020-04-12 10:28:41 -07:00
|
|
|
let ty = location.ty(self.mir, bx.tcx()).ty;
|
2020-10-24 02:21:18 +02:00
|
|
|
let ty = self.monomorphize(ty);
|
2019-05-23 12:45:22 -05:00
|
|
|
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
|
|
|
|
// we don't actually need to drop anything.
|
2022-10-17 08:29:40 +11:00
|
|
|
return helper.funclet_br(self, bx, target, mergeable_succ);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
let place = self.codegen_place(bx, location.as_ref());
|
2019-02-02 16:34:09 +00:00
|
|
|
let (args1, args2);
|
|
|
|
let mut args = if let Some(llextra) = place.llextra {
|
|
|
|
args2 = [place.llval, llextra];
|
|
|
|
&args2[..]
|
|
|
|
} else {
|
|
|
|
args1 = [place.llval];
|
|
|
|
&args1[..]
|
|
|
|
};
|
2023-02-15 05:11:27 +00:00
|
|
|
let (drop_fn, fn_abi) =
|
|
|
|
match ty.kind() {
|
|
|
|
// FIXME(eddyb) perhaps move some of this logic into
|
|
|
|
// `Instance::resolve_drop_in_place`?
|
|
|
|
ty::Dynamic(_, _, ty::Dyn) => {
|
|
|
|
// IN THIS ARM, WE HAVE:
|
|
|
|
// ty = *mut (dyn Trait)
|
|
|
|
// which is: exists<T> ( *mut T, Vtable<T: Trait> )
|
|
|
|
// args[0] args[1]
|
|
|
|
//
|
|
|
|
// args = ( Data, Vtable )
|
|
|
|
// |
|
|
|
|
// v
|
|
|
|
// /-------\
|
|
|
|
// | ... |
|
|
|
|
// \-------/
|
|
|
|
//
|
|
|
|
let virtual_drop = Instance {
|
|
|
|
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
|
|
|
|
substs: drop_fn.substs,
|
|
|
|
};
|
|
|
|
debug!("ty = {:?}", ty);
|
|
|
|
debug!("drop_fn = {:?}", drop_fn);
|
|
|
|
debug!("args = {:?}", args);
|
|
|
|
let fn_abi = bx.fn_abi_of_instance(virtual_drop, ty::List::empty());
|
|
|
|
let vtable = args[1];
|
|
|
|
// Truncate vtable off of args list
|
|
|
|
args = &args[..1];
|
|
|
|
(
|
|
|
|
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
|
|
|
|
.get_fn(bx, vtable, ty, &fn_abi),
|
|
|
|
fn_abi,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ty::Dynamic(_, _, ty::DynStar) => {
|
|
|
|
// IN THIS ARM, WE HAVE:
|
|
|
|
// ty = *mut (dyn* Trait)
|
|
|
|
// which is: *mut exists<T: sizeof(T) == sizeof(usize)> (T, Vtable<T: Trait>)
|
|
|
|
//
|
|
|
|
// args = [ * ]
|
|
|
|
// |
|
|
|
|
// v
|
|
|
|
// ( Data, Vtable )
|
|
|
|
// |
|
|
|
|
// v
|
|
|
|
// /-------\
|
|
|
|
// | ... |
|
|
|
|
// \-------/
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// WE CAN CONVERT THIS INTO THE ABOVE LOGIC BY DOING
|
|
|
|
//
|
|
|
|
// data = &(*args[0]).0 // gives a pointer to Data above (really the same pointer)
|
|
|
|
// vtable = (*args[0]).1 // loads the vtable out
|
|
|
|
// (data, vtable) // an equivalent Rust `*mut dyn Trait`
|
|
|
|
//
|
|
|
|
// SO THEN WE CAN USE THE ABOVE CODE.
|
|
|
|
let virtual_drop = Instance {
|
|
|
|
def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0),
|
|
|
|
substs: drop_fn.substs,
|
|
|
|
};
|
|
|
|
debug!("ty = {:?}", ty);
|
|
|
|
debug!("drop_fn = {:?}", drop_fn);
|
|
|
|
debug!("args = {:?}", args);
|
|
|
|
let fn_abi = bx.fn_abi_of_instance(virtual_drop, ty::List::empty());
|
|
|
|
let meta_ptr = place.project_field(bx, 1);
|
|
|
|
let meta = bx.load_operand(meta_ptr);
|
|
|
|
// Truncate vtable off of args list
|
|
|
|
args = &args[..1];
|
|
|
|
debug!("args' = {:?}", args);
|
|
|
|
(
|
|
|
|
meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE)
|
|
|
|
.get_fn(bx, meta.immediate(), ty, &fn_abi),
|
|
|
|
fn_abi,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => (bx.get_fn_addr(drop_fn), bx.fn_abi_of_instance(drop_fn, ty::List::empty())),
|
|
|
|
};
|
2019-11-06 12:29:09 -05:00
|
|
|
helper.do_call(
|
|
|
|
self,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2019-11-06 12:29:09 -05:00
|
|
|
fn_abi,
|
|
|
|
drop_fn,
|
|
|
|
args,
|
2019-02-02 16:34:09 +00:00
|
|
|
Some((ReturnDest::Nothing, target)),
|
|
|
|
unwind,
|
2022-06-22 11:24:34 +02:00
|
|
|
&[],
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ,
|
|
|
|
)
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_assert_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2019-02-02 16:34:09 +00:00
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
cond: &mir::Operand<'tcx>,
|
|
|
|
expected: bool,
|
|
|
|
msg: &mir::AssertMessage<'tcx>,
|
|
|
|
target: mir::BasicBlock,
|
|
|
|
cleanup: Option<mir::BasicBlock>,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
2019-02-02 16:34:09 +00:00
|
|
|
let span = terminator.source_info.span;
|
2022-11-09 11:04:10 +11:00
|
|
|
let cond = self.codegen_operand(bx, cond).immediate();
|
2019-02-02 16:34:09 +00:00
|
|
|
let mut const_cond = bx.const_to_opt_u128(cond, false).map(|c| c == 1);
|
|
|
|
|
|
|
|
// This case can currently arise only from functions marked
|
|
|
|
// with #[rustc_inherit_overflow_checks] and inlined from
|
|
|
|
// another crate (mostly core::num generic/#[inline] fns),
|
|
|
|
// while the current crate doesn't use overflow checks.
|
2023-02-08 21:17:29 +00:00
|
|
|
if !bx.cx().check_overflow() {
|
2023-02-11 17:29:12 +00:00
|
|
|
let overflow_not_to_check = match msg {
|
2023-02-11 16:41:37 +00:00
|
|
|
AssertKind::OverflowNeg(..) => true,
|
|
|
|
AssertKind::Overflow(op, ..) => op.is_checkable(),
|
|
|
|
_ => false,
|
|
|
|
};
|
2023-02-11 17:29:12 +00:00
|
|
|
if overflow_not_to_check {
|
2019-02-02 16:34:09 +00:00
|
|
|
const_cond = Some(expected);
|
2016-06-08 19:26:19 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-06-08 19:26:19 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Don't codegen the panic block if success if known.
|
|
|
|
if const_cond == Some(expected) {
|
2022-10-17 08:29:40 +11:00
|
|
|
return helper.funclet_br(self, bx, target, mergeable_succ);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-06-09 18:15:15 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Pass the condition through llvm.expect for branch hinting.
|
|
|
|
let cond = bx.expect(cond, expected);
|
|
|
|
|
|
|
|
// Create the failure block and the conditional branch to it.
|
2022-10-19 10:53:38 +11:00
|
|
|
let lltarget = helper.llbb_with_cleanup(self, target);
|
2022-02-18 15:10:56 +01:00
|
|
|
let panic_block = bx.append_sibling_block("panic");
|
2019-02-02 16:34:09 +00:00
|
|
|
if expected {
|
2022-02-18 15:10:56 +01:00
|
|
|
bx.cond_br(cond, lltarget, panic_block);
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
2022-02-18 15:10:56 +01:00
|
|
|
bx.cond_br(cond, panic_block, lltarget);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// After this point, bx is the block for the call to panic.
|
2022-02-18 15:37:31 +01:00
|
|
|
bx.switch_to_block(panic_block);
|
2022-11-09 11:04:10 +11:00
|
|
|
self.set_debug_loc(bx, terminator.source_info);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
// Get the location information.
|
2022-11-09 11:04:10 +11:00
|
|
|
let location = self.get_caller_location(bx, terminator.source_info).immediate();
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
// Put together the arguments to the panic entry point.
|
2019-07-24 10:24:55 +02:00
|
|
|
let (lang_item, args) = match msg {
|
2020-02-12 19:40:31 +01:00
|
|
|
AssertKind::BoundsCheck { ref len, ref index } => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let len = self.codegen_operand(bx, len).immediate();
|
|
|
|
let index = self.codegen_operand(bx, index).immediate();
|
2020-03-09 16:56:45 +01:00
|
|
|
// It's `fn panic_bounds_check(index: usize, len: usize)`,
|
|
|
|
// and `#[track_caller]` adds an implicit third argument.
|
2020-08-18 11:47:27 +01:00
|
|
|
(LangItem::PanicBoundsCheck, vec![index, len, location])
|
2016-01-30 00:18:47 +02:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => {
|
2022-06-28 17:34:24 +00:00
|
|
|
let msg = bx.const_str(msg.description());
|
2020-03-09 11:16:23 +01:00
|
|
|
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
|
|
|
|
// as two arguments, and `#[track_caller]` adds an implicit third argument.
|
2020-08-18 11:47:27 +01:00
|
|
|
(LangItem::Panic, vec![msg.0, msg.1, location])
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
};
|
2016-01-30 00:18:47 +02:00
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
let (fn_abi, llfn) = common::build_langcall(bx, Some(span), lang_item);
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Codegen the actual panic invoke/call.
|
2022-10-17 08:29:40 +11:00
|
|
|
let merging_succ = helper.do_call(self, bx, fn_abi, llfn, &args, None, cleanup, &[], false);
|
|
|
|
assert_eq!(merging_succ, MergingSucc::False);
|
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2022-01-12 20:45:31 +00:00
|
|
|
fn codegen_abort_terminator(
|
|
|
|
&mut self,
|
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2022-01-12 20:45:31 +00:00
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
) {
|
|
|
|
let span = terminator.source_info.span;
|
2022-11-09 11:04:10 +11:00
|
|
|
self.set_debug_loc(bx, terminator.source_info);
|
2022-01-12 20:45:31 +00:00
|
|
|
|
|
|
|
// Obtain the panic entry point.
|
2022-12-21 13:49:48 +01:00
|
|
|
let (fn_abi, llfn) = common::build_langcall(bx, Some(span), LangItem::PanicCannotUnwind);
|
2022-01-12 20:45:31 +00:00
|
|
|
|
|
|
|
// Codegen the actual panic invoke/call.
|
2022-10-17 08:29:40 +11:00
|
|
|
let merging_succ = helper.do_call(self, bx, fn_abi, llfn, &[], None, None, &[], false);
|
|
|
|
assert_eq!(merging_succ, MergingSucc::False);
|
2022-01-12 20:45:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
/// Returns `Some` if this is indeed a panic intrinsic and codegen is done.
|
2020-02-29 09:40:40 +01:00
|
|
|
fn codegen_panic_intrinsic(
|
|
|
|
&mut self,
|
|
|
|
helper: &TerminatorCodegenHelper<'tcx>,
|
|
|
|
bx: &mut Bx,
|
2020-07-08 11:04:10 +10:00
|
|
|
intrinsic: Option<Symbol>,
|
2020-02-29 09:40:40 +01:00
|
|
|
instance: Option<Instance<'tcx>>,
|
2020-09-21 06:52:37 +03:00
|
|
|
source_info: mir::SourceInfo,
|
2022-04-16 09:27:54 -04:00
|
|
|
target: Option<mir::BasicBlock>,
|
2020-02-29 09:40:40 +01:00
|
|
|
cleanup: Option<mir::BasicBlock>,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> Option<MergingSucc> {
|
2020-03-12 19:38:09 +01:00
|
|
|
// Emit a panic or a no-op for `assert_*` intrinsics.
|
2020-02-29 09:40:40 +01:00
|
|
|
// These are intrinsics that compile to panics so that we can get a message
|
|
|
|
// which mentions the offending type, even from a const context.
|
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-13 08:43:27 +01:00
|
|
|
enum AssertIntrinsic {
|
|
|
|
Inhabited,
|
|
|
|
ZeroValid,
|
2022-12-12 15:47:32 +01:00
|
|
|
MemUninitializedValid,
|
2020-11-25 17:00:28 -05:00
|
|
|
}
|
2020-02-29 09:40:40 +01:00
|
|
|
let panic_intrinsic = intrinsic.and_then(|i| match i {
|
2020-07-08 11:04:10 +10:00
|
|
|
sym::assert_inhabited => Some(AssertIntrinsic::Inhabited),
|
|
|
|
sym::assert_zero_valid => Some(AssertIntrinsic::ZeroValid),
|
2022-12-12 15:47:32 +01:00
|
|
|
sym::assert_mem_uninitialized_valid => Some(AssertIntrinsic::MemUninitializedValid),
|
2020-02-29 09:40:40 +01:00
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
if let Some(intrinsic) = panic_intrinsic {
|
2020-03-13 08:43:27 +01:00
|
|
|
use AssertIntrinsic::*;
|
2022-07-14 22:42:47 +01:00
|
|
|
|
2020-02-29 09:40:40 +01:00
|
|
|
let ty = instance.unwrap().substs.type_at(0);
|
|
|
|
let layout = bx.layout_of(ty);
|
|
|
|
let do_panic = match intrinsic {
|
2020-03-13 08:43:27 +01:00
|
|
|
Inhabited => layout.abi.is_uninhabited(),
|
2023-02-14 00:59:40 +00:00
|
|
|
ZeroValid => !bx
|
|
|
|
.tcx()
|
|
|
|
.permits_zero_init(bx.param_env().and(ty))
|
|
|
|
.expect("expected to have layout during codegen"),
|
|
|
|
MemUninitializedValid => !bx
|
|
|
|
.tcx()
|
|
|
|
.permits_uninit_init(bx.param_env().and(ty))
|
|
|
|
.expect("expected to have layout during codegen"),
|
2020-02-29 09:40:40 +01:00
|
|
|
};
|
2022-10-17 08:29:40 +11:00
|
|
|
Some(if do_panic {
|
2022-02-16 13:04:48 -05:00
|
|
|
let msg_str = with_no_visible_paths!({
|
|
|
|
with_no_trimmed_paths!({
|
2021-09-20 20:22:55 +04:00
|
|
|
if layout.abi.is_uninhabited() {
|
|
|
|
// Use this error even for the other intrinsics as it is more precise.
|
|
|
|
format!("attempted to instantiate uninhabited type `{}`", ty)
|
|
|
|
} else if intrinsic == ZeroValid {
|
|
|
|
format!("attempted to zero-initialize type `{}`, which is invalid", ty)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"attempted to leave type `{}` uninitialized, which is invalid",
|
|
|
|
ty
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2020-09-02 10:40:56 +03:00
|
|
|
});
|
2022-06-28 17:34:24 +00:00
|
|
|
let msg = bx.const_str(&msg_str);
|
2020-02-29 09:40:40 +01:00
|
|
|
|
|
|
|
// Obtain the panic entry point.
|
2022-04-27 18:58:59 +08:00
|
|
|
let (fn_abi, llfn) =
|
2022-12-21 13:49:48 +01:00
|
|
|
common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind);
|
2020-02-29 09:40:40 +01:00
|
|
|
|
|
|
|
// Codegen the actual panic invoke/call.
|
|
|
|
helper.do_call(
|
|
|
|
self,
|
|
|
|
bx,
|
|
|
|
fn_abi,
|
|
|
|
llfn,
|
2022-12-21 13:49:48 +01:00
|
|
|
&[msg.0, msg.1],
|
2022-04-16 09:27:54 -04:00
|
|
|
target.as_ref().map(|bb| (ReturnDest::Nothing, *bb)),
|
2020-02-29 09:40:40 +01:00
|
|
|
cleanup,
|
2022-06-22 11:24:34 +02:00
|
|
|
&[],
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ,
|
|
|
|
)
|
2020-02-29 09:40:40 +01:00
|
|
|
} else {
|
|
|
|
// a NOP
|
2022-04-16 09:27:54 -04:00
|
|
|
let target = target.unwrap();
|
2022-10-17 08:29:40 +11:00
|
|
|
helper.funclet_br(self, bx, target, mergeable_succ)
|
|
|
|
})
|
2020-02-29 09:40:40 +01:00
|
|
|
} else {
|
2022-10-17 08:29:40 +11:00
|
|
|
None
|
2020-02-29 09:40:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:24:25 +02:00
|
|
|
fn codegen_call_terminator(
|
2019-02-02 16:34:09 +00:00
|
|
|
&mut self,
|
2019-10-29 16:24:25 +02:00
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2019-02-02 16:34:09 +00:00
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
func: &mir::Operand<'tcx>,
|
2020-12-30 12:59:07 +01:00
|
|
|
args: &[mir::Operand<'tcx>],
|
2022-04-16 09:27:54 -04:00
|
|
|
destination: mir::Place<'tcx>,
|
|
|
|
target: Option<mir::BasicBlock>,
|
2019-02-02 16:34:09 +00:00
|
|
|
cleanup: Option<mir::BasicBlock>,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span: Span,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
2020-09-21 06:52:37 +03:00
|
|
|
let source_info = terminator.source_info;
|
|
|
|
let span = source_info.span;
|
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
|
2022-11-09 11:04:10 +11:00
|
|
|
let callee = self.codegen_operand(bx, func);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let (instance, mut llfn) = match *callee.layout.ty.kind() {
|
2019-02-02 16:34:09 +00:00
|
|
|
ty::FnDef(def_id, substs) => (
|
|
|
|
Some(
|
2022-12-11 19:46:58 +00:00
|
|
|
ty::Instance::expect_resolve(
|
|
|
|
bx.tcx(),
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
def_id,
|
|
|
|
substs,
|
|
|
|
)
|
|
|
|
.polymorphize(bx.tcx()),
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
2019-02-02 16:34:09 +00:00
|
|
|
None,
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
2019-02-02 16:34:09 +00:00
|
|
|
ty::FnPtr(_) => (None, Some(callee.immediate())),
|
|
|
|
_ => bug!("{} is not callable", callee.layout.ty),
|
|
|
|
};
|
|
|
|
let def = instance.map(|i| i.def);
|
2019-10-29 21:46:25 +02:00
|
|
|
|
|
|
|
if let Some(ty::InstanceDef::DropGlue(_, None)) = def {
|
|
|
|
// Empty drop glue; a no-op.
|
2022-04-16 09:27:54 -04:00
|
|
|
let target = target.unwrap();
|
2022-10-17 08:29:40 +11:00
|
|
|
return helper.funclet_br(self, bx, target, mergeable_succ);
|
2019-10-29 21:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) avoid computing this if possible, when `instance` is
|
|
|
|
// available - right now `sig` is only needed for getting the `abi`
|
|
|
|
// and figuring out how many extra args were passed to a C-variadic `fn`.
|
2019-02-02 16:34:09 +00:00
|
|
|
let sig = callee.layout.ty.fn_sig(bx.tcx());
|
2019-10-29 22:08:50 +02:00
|
|
|
let abi = sig.abi();
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
// Handle intrinsics old codegen wants Expr's for, ourselves.
|
|
|
|
let intrinsic = match def {
|
2020-07-08 11:04:10 +10:00
|
|
|
Some(ty::InstanceDef::Intrinsic(def_id)) => Some(bx.tcx().item_name(def_id)),
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => None,
|
|
|
|
};
|
2016-06-05 14:38:29 +03:00
|
|
|
|
2019-10-29 22:08:50 +02:00
|
|
|
let extra_args = &args[sig.inputs().skip_binder().len()..];
|
2021-09-02 00:29:15 +03:00
|
|
|
let extra_args = bx.tcx().mk_type_list(extra_args.iter().map(|op_arg| {
|
|
|
|
let op_ty = op_arg.ty(self.mir, bx.tcx());
|
|
|
|
self.monomorphize(op_ty)
|
|
|
|
}));
|
2019-10-29 22:08:50 +02:00
|
|
|
|
|
|
|
let fn_abi = match instance {
|
2021-09-02 00:29:15 +03:00
|
|
|
Some(instance) => bx.fn_abi_of_instance(instance, extra_args),
|
|
|
|
None => bx.fn_abi_of_fn_ptr(sig, extra_args),
|
2019-10-29 22:08:50 +02:00
|
|
|
};
|
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
if intrinsic == Some(sym::transmute) {
|
2022-10-17 08:29:40 +11:00
|
|
|
return if let Some(target) = target {
|
2022-11-09 11:04:10 +11:00
|
|
|
self.codegen_transmute(bx, &args[0], destination);
|
2022-10-17 08:29:40 +11:00
|
|
|
helper.funclet_br(self, bx, target, mergeable_succ)
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
// If we are trying to transmute to an uninhabited type,
|
|
|
|
// it is likely there is no allotted destination. In fact,
|
|
|
|
// transmuting to an uninhabited type is UB, which means
|
|
|
|
// we can do what we like. Here, we declare that transmuting
|
|
|
|
// into an uninhabited type is impossible, so anything following
|
|
|
|
// it must be unreachable.
|
2020-03-31 18:16:47 +02:00
|
|
|
assert_eq!(fn_abi.ret.layout.abi, abi::Abi::Uninhabited);
|
2019-02-02 16:34:09 +00:00
|
|
|
bx.unreachable();
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
|
|
|
};
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
if let Some(merging_succ) = self.codegen_panic_intrinsic(
|
2020-02-29 09:40:40 +01:00
|
|
|
&helper,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2020-02-29 09:40:40 +01:00
|
|
|
intrinsic,
|
|
|
|
instance,
|
2020-09-21 06:52:37 +03:00
|
|
|
source_info,
|
2022-04-16 09:27:54 -04:00
|
|
|
target,
|
2020-02-29 09:40:40 +01:00
|
|
|
cleanup,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ,
|
2020-02-29 09:40:40 +01:00
|
|
|
) {
|
2022-10-17 08:29:40 +11:00
|
|
|
return merging_succ;
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2016-05-17 01:06:52 +03:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// The arguments we'll be passing. Plus one to account for outptr, if used.
|
2019-10-29 16:35:26 +02:00
|
|
|
let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize;
|
2019-02-02 16:34:09 +00:00
|
|
|
let mut llargs = Vec::with_capacity(arg_count);
|
|
|
|
|
|
|
|
// Prepare the return value destination
|
2022-04-16 09:27:54 -04:00
|
|
|
let ret_dest = if target.is_some() {
|
2019-02-02 16:34:09 +00:00
|
|
|
let is_intrinsic = intrinsic.is_some();
|
2022-11-09 11:04:10 +11:00
|
|
|
self.make_return_dest(bx, destination, &fn_abi.ret, &mut llargs, is_intrinsic)
|
2019-02-02 16:34:09 +00:00
|
|
|
} else {
|
|
|
|
ReturnDest::Nothing
|
|
|
|
};
|
2016-04-08 15:37:56 +12:00
|
|
|
|
2020-07-08 11:04:10 +10:00
|
|
|
if intrinsic == Some(sym::caller_location) {
|
2022-10-17 08:29:40 +11:00
|
|
|
return if let Some(target) = target {
|
2022-11-09 11:04:10 +11:00
|
|
|
let location =
|
|
|
|
self.get_caller_location(bx, mir::SourceInfo { span: fn_span, ..source_info });
|
2019-10-09 08:25:41 -07:00
|
|
|
|
|
|
|
if let ReturnDest::IndirectOperand(tmp, _) = ret_dest {
|
2022-11-09 11:04:10 +11:00
|
|
|
location.val.store(bx, tmp);
|
2019-10-09 08:25:41 -07:00
|
|
|
}
|
2022-11-09 11:04:10 +11:00
|
|
|
self.store_return(bx, ret_dest, &fn_abi.ret, location.immediate());
|
2022-10-17 08:29:40 +11:00
|
|
|
helper.funclet_br(self, bx, target, mergeable_succ)
|
|
|
|
} else {
|
|
|
|
MergingSucc::False
|
|
|
|
};
|
2019-10-09 08:25:41 -07:00
|
|
|
}
|
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
match intrinsic {
|
|
|
|
None | Some(sym::drop_in_place) => {}
|
2021-01-23 08:57:04 +00:00
|
|
|
Some(sym::copy_nonoverlapping) => unreachable!(),
|
2021-01-23 03:55:41 +00:00
|
|
|
Some(intrinsic) => {
|
|
|
|
let dest = match ret_dest {
|
|
|
|
_ if fn_abi.ret.is_indirect() => llargs[0],
|
|
|
|
ReturnDest::Nothing => {
|
|
|
|
bx.const_undef(bx.type_ptr_to(bx.arg_memory_ty(&fn_abi.ret)))
|
|
|
|
}
|
|
|
|
ReturnDest::IndirectOperand(dst, _) | ReturnDest::Store(dst) => dst.llval,
|
|
|
|
ReturnDest::DirectOperand(_) => {
|
|
|
|
bug!("Cannot use direct operand with an intrinsic call")
|
|
|
|
}
|
|
|
|
};
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
let args: Vec<_> = args
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, arg)| {
|
|
|
|
// The indices passed to simd_shuffle* in the
|
|
|
|
// third argument must be constant. This is
|
|
|
|
// checked by const-qualification, which also
|
|
|
|
// promotes any complex rvalues to constants.
|
|
|
|
if i == 2 && intrinsic.as_str().starts_with("simd_shuffle") {
|
|
|
|
if let mir::Operand::Constant(constant) = arg {
|
|
|
|
let c = self.eval_mir_constant(constant);
|
2021-09-16 02:59:49 +00:00
|
|
|
let (llval, ty) = self.simd_shuffle_indices(
|
|
|
|
&bx,
|
|
|
|
constant.span,
|
|
|
|
self.monomorphize(constant.ty()),
|
|
|
|
c,
|
|
|
|
);
|
2021-01-23 03:55:41 +00:00
|
|
|
return OperandRef {
|
|
|
|
val: Immediate(llval),
|
|
|
|
layout: bx.layout_of(ty),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "shuffle indices must be constant");
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2018-08-19 17:36:04 +02:00
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
self.codegen_operand(bx, arg)
|
2021-01-23 03:55:41 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-01-23 08:57:04 +00:00
|
|
|
Self::codegen_intrinsic_call(
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2021-01-23 03:55:41 +00:00
|
|
|
*instance.as_ref().unwrap(),
|
|
|
|
&fn_abi,
|
|
|
|
&args,
|
|
|
|
dest,
|
|
|
|
span,
|
|
|
|
);
|
2017-09-20 02:32:22 +03:00
|
|
|
|
2021-01-23 03:55:41 +00:00
|
|
|
if let ReturnDest::IndirectOperand(dst, _) = ret_dest {
|
2022-11-09 11:04:10 +11:00
|
|
|
self.store_return(bx, ret_dest, &fn_abi.ret, dst.llval);
|
2021-01-23 03:55:41 +00:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
return if let Some(target) = target {
|
|
|
|
helper.funclet_br(self, bx, target, mergeable_succ)
|
2021-01-23 03:55:41 +00:00
|
|
|
} else {
|
|
|
|
bx.unreachable();
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
|
|
|
};
|
2021-01-23 03:55:41 +00:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
// Split the rust-call tupled arguments off.
|
|
|
|
let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() {
|
|
|
|
let (tup, args) = args.split_last().unwrap();
|
|
|
|
(args, Some(tup))
|
|
|
|
} else {
|
2021-02-16 00:30:06 +01:00
|
|
|
(args, None)
|
2019-02-02 16:34:09 +00:00
|
|
|
};
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2022-06-25 16:36:11 +02:00
|
|
|
let mut copied_constant_arguments = vec![];
|
2019-02-02 16:34:09 +00:00
|
|
|
'make_args: for (i, arg) in first_args.iter().enumerate() {
|
2022-11-09 11:04:10 +11:00
|
|
|
let mut op = self.codegen_operand(bx, arg);
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) {
|
2022-08-22 11:25:56 -07:00
|
|
|
match op.val {
|
|
|
|
Pair(data_ptr, meta) => {
|
|
|
|
// In the case of Rc<Self>, we need to explicitly pass a
|
|
|
|
// *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack
|
|
|
|
// that is understood elsewhere in the compiler as a method on
|
|
|
|
// `dyn Trait`.
|
|
|
|
// To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until
|
2022-11-19 03:06:21 +00:00
|
|
|
// we get a value of a built-in pointer type.
|
|
|
|
//
|
|
|
|
// This is also relevant for `Pin<&mut Self>`, where we need to peel the `Pin`.
|
2022-08-22 11:25:56 -07:00
|
|
|
'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
|
|
|
|
&& !op.layout.ty.is_region_ptr()
|
|
|
|
{
|
|
|
|
for i in 0..op.layout.fields.count() {
|
2022-11-09 11:04:10 +11:00
|
|
|
let field = op.extract_field(bx, i);
|
2022-08-22 11:25:56 -07:00
|
|
|
if !field.layout.is_zst() {
|
|
|
|
// we found the one non-zero-sized field that is allowed
|
|
|
|
// now find *its* non-zero-sized field, or stop if it's a
|
|
|
|
// pointer
|
|
|
|
op = field;
|
|
|
|
continue 'descend_newtypes;
|
|
|
|
}
|
2018-09-20 03:16:10 -04:00
|
|
|
}
|
2022-08-22 11:25:56 -07:00
|
|
|
|
|
|
|
span_bug!(span, "receiver has no non-zero-sized fields {:?}", op);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2018-09-20 03:16:10 -04:00
|
|
|
|
2022-08-22 11:25:56 -07:00
|
|
|
// now that we have `*dyn Trait` or `&dyn Trait`, split it up into its
|
|
|
|
// data pointer and vtable. Look up the method in the vtable, and pass
|
|
|
|
// the data pointer as the first argument
|
|
|
|
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2022-08-22 11:25:56 -07:00
|
|
|
meta,
|
|
|
|
op.layout.ty,
|
|
|
|
&fn_abi,
|
|
|
|
));
|
|
|
|
llargs.push(data_ptr);
|
|
|
|
continue 'make_args;
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2022-08-22 11:25:56 -07:00
|
|
|
Ref(data_ptr, Some(meta), _) => {
|
|
|
|
// by-value dynamic dispatch
|
|
|
|
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2022-08-22 11:25:56 -07:00
|
|
|
meta,
|
|
|
|
op.layout.ty,
|
|
|
|
&fn_abi,
|
|
|
|
));
|
|
|
|
llargs.push(data_ptr);
|
|
|
|
continue;
|
2017-09-20 05:16:06 +03:00
|
|
|
}
|
2022-08-29 06:00:58 +00:00
|
|
|
Immediate(_) => {
|
2022-11-19 03:06:21 +00:00
|
|
|
// See comment above explaining why we peel these newtypes
|
|
|
|
'descend_newtypes: while !op.layout.ty.is_unsafe_ptr()
|
|
|
|
&& !op.layout.ty.is_region_ptr()
|
|
|
|
{
|
|
|
|
for i in 0..op.layout.fields.count() {
|
|
|
|
let field = op.extract_field(bx, i);
|
|
|
|
if !field.layout.is_zst() {
|
|
|
|
// we found the one non-zero-sized field that is allowed
|
|
|
|
// now find *its* non-zero-sized field, or stop if it's a
|
|
|
|
// pointer
|
|
|
|
op = field;
|
|
|
|
continue 'descend_newtypes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
span_bug!(span, "receiver has no non-zero-sized fields {:?}", op);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we've actually unwrapped the rcvr down
|
|
|
|
// to a pointer or ref to `dyn* Trait`.
|
|
|
|
if !op.layout.ty.builtin_deref(true).unwrap().ty.is_dyn_star() {
|
2022-08-29 06:00:58 +00:00
|
|
|
span_bug!(span, "can't codegen a virtual call on {:#?}", op);
|
|
|
|
}
|
|
|
|
let place = op.deref(bx.cx());
|
2022-11-09 11:04:10 +11:00
|
|
|
let data_ptr = place.project_field(bx, 0);
|
|
|
|
let meta_ptr = place.project_field(bx, 1);
|
2022-08-29 06:00:58 +00:00
|
|
|
let meta = bx.load_operand(meta_ptr);
|
|
|
|
llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2022-08-29 06:00:58 +00:00
|
|
|
meta.immediate(),
|
|
|
|
op.layout.ty,
|
|
|
|
&fn_abi,
|
|
|
|
));
|
|
|
|
llargs.push(data_ptr.llval);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
span_bug!(span, "can't codegen a virtual call on {:#?}", op);
|
|
|
|
}
|
2017-09-20 02:32:22 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The callee needs to own the argument memory if we pass it
|
|
|
|
// by-ref, so make a local copy of non-immediate constants.
|
|
|
|
match (arg, op.val) {
|
|
|
|
(&mir::Operand::Copy(_), Ref(_, None, _))
|
|
|
|
| (&mir::Operand::Constant(_), Ref(_, None, _)) => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let tmp = PlaceRef::alloca(bx, op.layout);
|
2022-06-22 11:24:34 +02:00
|
|
|
bx.lifetime_start(tmp.llval, tmp.layout.size);
|
2022-11-09 11:04:10 +11:00
|
|
|
op.val.store(bx, tmp);
|
2019-02-02 16:34:09 +00:00
|
|
|
op.val = Ref(tmp.llval, None, tmp.align);
|
2022-06-25 16:36:11 +02:00
|
|
|
copied_constant_arguments.push(tmp);
|
2017-09-20 02:32:22 +03:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2017-09-20 02:32:22 +03:00
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
self.codegen_argument(bx, op, &mut llargs, &fn_abi.args[i]);
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
let num_untupled = untuple.map(|tup| {
|
2022-11-09 11:04:10 +11:00
|
|
|
self.codegen_arguments_untupled(bx, tup, &mut llargs, &fn_abi.args[first_args.len()..])
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
});
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2019-11-07 06:04:14 -08:00
|
|
|
let needs_location =
|
2019-12-06 17:05:51 -08:00
|
|
|
instance.map_or(false, |i| i.def.requires_caller_location(self.cx.tcx()));
|
2019-11-07 06:04:14 -08:00
|
|
|
if needs_location {
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
let mir_args = if let Some(num_untupled) = num_untupled {
|
|
|
|
first_args.len() + num_untupled
|
|
|
|
} else {
|
|
|
|
args.len()
|
|
|
|
};
|
2019-11-07 06:04:14 -08:00
|
|
|
assert_eq!(
|
|
|
|
fn_abi.args.len(),
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
mir_args + 1,
|
|
|
|
"#[track_caller] fn's must have 1 more argument in their ABI than in their MIR: {:?} {:?} {:?}",
|
|
|
|
instance,
|
|
|
|
fn_span,
|
|
|
|
fn_abi,
|
2019-11-07 06:04:14 -08:00
|
|
|
);
|
2020-09-21 06:52:37 +03:00
|
|
|
let location =
|
2022-11-09 11:04:10 +11:00
|
|
|
self.get_caller_location(bx, mir::SourceInfo { span: fn_span, ..source_info });
|
2020-06-09 15:34:23 -04:00
|
|
|
debug!(
|
|
|
|
"codegen_call_terminator({:?}): location={:?} (fn_span {:?})",
|
|
|
|
terminator, location, fn_span
|
|
|
|
);
|
|
|
|
|
2019-12-06 17:05:51 -08:00
|
|
|
let last_arg = fn_abi.args.last().unwrap();
|
2022-11-09 11:04:10 +11:00
|
|
|
self.codegen_argument(bx, location, &mut llargs, last_arg);
|
2019-11-07 06:04:14 -08:00
|
|
|
}
|
|
|
|
|
2021-10-07 15:33:13 -07:00
|
|
|
let (is_indirect_call, fn_ptr) = match (llfn, instance) {
|
|
|
|
(Some(llfn), _) => (true, llfn),
|
|
|
|
(None, Some(instance)) => (false, bx.get_fn_addr(instance)),
|
2019-02-02 16:34:09 +00:00
|
|
|
_ => span_bug!(span, "no llfn for call"),
|
|
|
|
};
|
|
|
|
|
2021-10-07 15:33:13 -07:00
|
|
|
// For backends that support CFI using type membership (i.e., testing whether a given
|
|
|
|
// pointer is associated with a type identifier).
|
|
|
|
if bx.tcx().sess.is_sanitizer_cfi_enabled() && is_indirect_call {
|
|
|
|
// Emit type metadata and checks.
|
|
|
|
// FIXME(rcvalle): Add support for generalized identifiers.
|
|
|
|
// FIXME(rcvalle): Create distinct unnamed MDNodes for internal identifiers.
|
|
|
|
let typeid = typeid_for_fnabi(bx.tcx(), fn_abi);
|
2022-03-31 22:50:41 -07:00
|
|
|
let typeid_metadata = self.cx.typeid_metadata(typeid);
|
2021-10-07 15:33:13 -07:00
|
|
|
|
|
|
|
// Test whether the function pointer is associated with the type identifier.
|
|
|
|
let cond = bx.type_test(fn_ptr, typeid_metadata);
|
2022-02-18 15:10:56 +01:00
|
|
|
let bb_pass = bx.append_sibling_block("type_test.pass");
|
|
|
|
let bb_fail = bx.append_sibling_block("type_test.fail");
|
|
|
|
bx.cond_br(cond, bb_pass, bb_fail);
|
2021-10-07 15:33:13 -07:00
|
|
|
|
2022-02-18 15:37:31 +01:00
|
|
|
bx.switch_to_block(bb_pass);
|
2022-10-17 08:29:40 +11:00
|
|
|
let merging_succ = helper.do_call(
|
2021-10-07 15:33:13 -07:00
|
|
|
self,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2021-10-07 15:33:13 -07:00
|
|
|
fn_abi,
|
|
|
|
fn_ptr,
|
|
|
|
&llargs,
|
2022-04-16 09:27:54 -04:00
|
|
|
target.as_ref().map(|&target| (ret_dest, target)),
|
2021-10-07 15:33:13 -07:00
|
|
|
cleanup,
|
2022-06-25 16:36:11 +02:00
|
|
|
&copied_constant_arguments,
|
2022-10-17 08:29:40 +11:00
|
|
|
false,
|
2021-10-07 15:33:13 -07:00
|
|
|
);
|
2022-10-17 08:29:40 +11:00
|
|
|
assert_eq!(merging_succ, MergingSucc::False);
|
2021-10-07 15:33:13 -07:00
|
|
|
|
2022-02-18 15:37:31 +01:00
|
|
|
bx.switch_to_block(bb_fail);
|
|
|
|
bx.abort();
|
|
|
|
bx.unreachable();
|
2021-10-07 15:33:13 -07:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
return MergingSucc::False;
|
2021-10-07 15:33:13 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 12:29:09 -05:00
|
|
|
helper.do_call(
|
|
|
|
self,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2019-11-06 12:29:09 -05:00
|
|
|
fn_abi,
|
|
|
|
fn_ptr,
|
|
|
|
&llargs,
|
2022-04-16 09:27:54 -04:00
|
|
|
target.as_ref().map(|&target| (ret_dest, target)),
|
2019-02-02 16:34:09 +00:00
|
|
|
cleanup,
|
2022-06-25 16:36:11 +02:00
|
|
|
&copied_constant_arguments,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ,
|
|
|
|
)
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2020-05-11 17:53:32 +01:00
|
|
|
|
|
|
|
fn codegen_asm_terminator(
|
|
|
|
&mut self,
|
|
|
|
helper: TerminatorCodegenHelper<'tcx>,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2020-05-11 17:53:32 +01:00
|
|
|
terminator: &mir::Terminator<'tcx>,
|
|
|
|
template: &[ast::InlineAsmTemplatePiece],
|
|
|
|
operands: &[mir::InlineAsmOperand<'tcx>],
|
|
|
|
options: ast::InlineAsmOptions,
|
2020-05-26 20:07:59 +01:00
|
|
|
line_spans: &[Span],
|
2020-05-11 17:53:32 +01:00
|
|
|
destination: Option<mir::BasicBlock>,
|
2021-09-04 19:25:09 +02:00
|
|
|
cleanup: Option<mir::BasicBlock>,
|
2021-10-21 04:56:36 +09:00
|
|
|
instance: Instance<'_>,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ: bool,
|
|
|
|
) -> MergingSucc {
|
2020-05-11 17:53:32 +01:00
|
|
|
let span = terminator.source_info.span;
|
|
|
|
|
|
|
|
let operands: Vec<_> = operands
|
|
|
|
.iter()
|
|
|
|
.map(|op| match *op {
|
|
|
|
mir::InlineAsmOperand::In { reg, ref value } => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let value = self.codegen_operand(bx, value);
|
2020-05-11 17:53:32 +01:00
|
|
|
InlineAsmOperandRef::In { reg, value }
|
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::Out { reg, late, ref place } => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let place = place.map(|place| self.codegen_place(bx, place.as_ref()));
|
2020-05-11 17:53:32 +01:00
|
|
|
InlineAsmOperandRef::Out { reg, late, place }
|
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::InOut { reg, late, ref in_value, ref out_place } => {
|
2022-11-09 11:04:10 +11:00
|
|
|
let in_value = self.codegen_operand(bx, in_value);
|
2020-05-11 17:53:32 +01:00
|
|
|
let out_place =
|
2022-11-09 11:04:10 +11:00
|
|
|
out_place.map(|out_place| self.codegen_place(bx, out_place.as_ref()));
|
2020-05-11 17:53:32 +01:00
|
|
|
InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
|
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::Const { ref value } => {
|
2021-04-06 05:50:55 +01:00
|
|
|
let const_value = self
|
|
|
|
.eval_mir_constant(value)
|
|
|
|
.unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
|
2021-04-11 20:51:28 +01:00
|
|
|
let string = common::asm_const_to_str(
|
|
|
|
bx.tcx(),
|
|
|
|
span,
|
|
|
|
const_value,
|
|
|
|
bx.layout_of(value.ty()),
|
|
|
|
);
|
2021-04-06 05:50:55 +01:00
|
|
|
InlineAsmOperandRef::Const { string }
|
2020-05-11 17:53:32 +01:00
|
|
|
}
|
|
|
|
mir::InlineAsmOperand::SymFn { ref value } => {
|
2020-10-24 02:21:18 +02:00
|
|
|
let literal = self.monomorphize(value.literal);
|
2021-03-08 16:18:03 +00:00
|
|
|
if let ty::FnDef(def_id, substs) = *literal.ty().kind() {
|
2020-05-24 02:04:49 +01:00
|
|
|
let instance = ty::Instance::resolve_for_fn_ptr(
|
2020-05-11 17:53:32 +01:00
|
|
|
bx.tcx(),
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
def_id,
|
|
|
|
substs,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
InlineAsmOperandRef::SymFn { instance }
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "invalid type for asm sym (fn)");
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 15:52:38 +01:00
|
|
|
mir::InlineAsmOperand::SymStatic { def_id } => {
|
|
|
|
InlineAsmOperandRef::SymStatic { def_id }
|
2020-05-11 17:53:32 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-09-04 19:25:09 +02:00
|
|
|
helper.do_inlineasm(
|
|
|
|
self,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx,
|
2021-09-04 19:25:09 +02:00
|
|
|
template,
|
|
|
|
&operands,
|
|
|
|
options,
|
|
|
|
line_spans,
|
|
|
|
destination,
|
|
|
|
cleanup,
|
|
|
|
instance,
|
2022-10-17 08:29:40 +11:00
|
|
|
mergeable_succ,
|
|
|
|
)
|
2020-05-11 17:53:32 +01:00
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 01:41:17 -04:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2022-10-17 08:29:40 +11:00
|
|
|
pub fn codegen_block(&mut self, mut bb: mir::BasicBlock) {
|
|
|
|
let llbb = match self.try_llbb(bb) {
|
|
|
|
Some(llbb) => llbb,
|
|
|
|
None => return,
|
|
|
|
};
|
2022-11-09 11:04:10 +11:00
|
|
|
let bx = &mut Bx::build(self.cx, llbb);
|
2019-11-06 00:04:53 -05:00
|
|
|
let mir = self.mir;
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
// MIR basic blocks stop at any function call. This may not be the case
|
|
|
|
// for the backend's basic blocks, in which case we might be able to
|
|
|
|
// combine multiple MIR basic blocks into a single backend basic block.
|
|
|
|
loop {
|
|
|
|
let data = &mir[bb];
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
debug!("codegen_block({:?}={:?})", bb, data);
|
|
|
|
|
|
|
|
for statement in &data.statements {
|
|
|
|
self.codegen_statement(bx, statement);
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
let merging_succ = self.codegen_terminator(bx, bb, data.terminator());
|
|
|
|
if let MergingSucc::False = merging_succ {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are merging the successor into the produced backend basic
|
|
|
|
// block. Record that the successor should be skipped when it is
|
|
|
|
// reached.
|
|
|
|
//
|
|
|
|
// Note: we must not have already generated code for the successor.
|
|
|
|
// This is implicitly ensured by the reverse postorder traversal,
|
|
|
|
// and the assertion explicitly guarantees that.
|
|
|
|
let mut successors = data.terminator().successors();
|
|
|
|
let succ = successors.next().unwrap();
|
|
|
|
assert!(matches!(self.cached_llbbs[succ], CachedLlbb::None));
|
|
|
|
self.cached_llbbs[succ] = CachedLlbb::Skip;
|
|
|
|
bb = succ;
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn codegen_terminator(
|
|
|
|
&mut self,
|
2022-11-09 11:04:10 +11:00
|
|
|
bx: &mut Bx,
|
2019-02-02 16:34:09 +00:00
|
|
|
bb: mir::BasicBlock,
|
2019-10-29 16:24:25 +02:00
|
|
|
terminator: &'tcx mir::Terminator<'tcx>,
|
2022-10-17 08:29:40 +11:00
|
|
|
) -> MergingSucc {
|
2019-02-02 16:34:09 +00:00
|
|
|
debug!("codegen_terminator: {:?}", terminator);
|
|
|
|
|
2023-01-17 00:00:00 +00:00
|
|
|
let helper = TerminatorCodegenHelper { bb, terminator };
|
2019-02-02 16:34:09 +00:00
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
let mergeable_succ = || {
|
|
|
|
// Note: any call to `switch_to_block` will invalidate a `true` value
|
|
|
|
// of `mergeable_succ`.
|
|
|
|
let mut successors = terminator.successors();
|
|
|
|
if let Some(succ) = successors.next()
|
|
|
|
&& successors.next().is_none()
|
|
|
|
&& let &[succ_pred] = self.mir.basic_blocks.predecessors()[succ].as_slice()
|
|
|
|
{
|
|
|
|
// bb has a single successor, and bb is its only predecessor. This
|
|
|
|
// makes it a candidate for merging.
|
|
|
|
assert_eq!(succ_pred, bb);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-09 11:04:10 +11:00
|
|
|
self.set_debug_loc(bx, terminator.source_info);
|
2019-02-02 16:34:09 +00:00
|
|
|
match terminator.kind {
|
2022-10-17 08:29:40 +11:00
|
|
|
mir::TerminatorKind::Resume => {
|
|
|
|
self.codegen_resume_terminator(helper, bx);
|
|
|
|
MergingSucc::False
|
|
|
|
}
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
mir::TerminatorKind::Abort => {
|
2022-01-12 20:45:31 +00:00
|
|
|
self.codegen_abort_terminator(helper, bx, terminator);
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Goto { target } => {
|
2022-10-17 08:29:40 +11:00
|
|
|
helper.funclet_br(self, bx, target, mergeable_succ())
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 16:03:27 -08:00
|
|
|
mir::TerminatorKind::SwitchInt { ref discr, ref targets } => {
|
|
|
|
self.codegen_switchint_terminator(helper, bx, discr, targets);
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
2015-12-13 05:48:43 -08:00
|
|
|
|
2019-02-02 16:34:09 +00:00
|
|
|
mir::TerminatorKind::Return => {
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_return_terminator(bx);
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Unreachable => {
|
|
|
|
bx.unreachable();
|
2022-10-17 08:29:40 +11:00
|
|
|
MergingSucc::False
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 09:56:54 +02:00
|
|
|
mir::TerminatorKind::Drop { place, target, unwind } => {
|
2022-10-17 08:29:40 +11:00
|
|
|
self.codegen_drop_terminator(helper, bx, place, target, unwind, mergeable_succ())
|
2019-02-02 16:34:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 08:29:40 +11:00
|
|
|
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => self
|
|
|
|
.codegen_assert_terminator(
|
|
|
|
helper,
|
|
|
|
bx,
|
|
|
|
terminator,
|
|
|
|
cond,
|
|
|
|
expected,
|
|
|
|
msg,
|
|
|
|
target,
|
|
|
|
cleanup,
|
|
|
|
mergeable_succ(),
|
|
|
|
),
|
2019-02-02 16:34:09 +00:00
|
|
|
|
|
|
|
mir::TerminatorKind::DropAndReplace { .. } => {
|
|
|
|
bug!("undesugared DropAndReplace in codegen: {:?}", terminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
2022-04-16 09:27:54 -04:00
|
|
|
destination,
|
|
|
|
target,
|
2019-02-02 16:34:09 +00:00
|
|
|
cleanup,
|
|
|
|
from_hir_call: _,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span,
|
2022-10-17 08:29:40 +11:00
|
|
|
} => self.codegen_call_terminator(
|
|
|
|
helper,
|
|
|
|
bx,
|
|
|
|
terminator,
|
|
|
|
func,
|
|
|
|
args,
|
|
|
|
destination,
|
|
|
|
target,
|
|
|
|
cleanup,
|
|
|
|
fn_span,
|
|
|
|
mergeable_succ(),
|
|
|
|
),
|
2018-05-08 16:10:16 +03:00
|
|
|
mir::TerminatorKind::GeneratorDrop | mir::TerminatorKind::Yield { .. } => {
|
|
|
|
bug!("generator ops in codegen")
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-06-02 09:15:24 +02:00
|
|
|
mir::TerminatorKind::FalseEdge { .. } | mir::TerminatorKind::FalseUnwind { .. } => {
|
2018-05-08 16:10:16 +03:00
|
|
|
bug!("borrowck false edges in codegen")
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2020-02-17 21:36:01 +00:00
|
|
|
|
2020-05-26 20:07:59 +01:00
|
|
|
mir::TerminatorKind::InlineAsm {
|
|
|
|
template,
|
|
|
|
ref operands,
|
|
|
|
options,
|
|
|
|
line_spans,
|
|
|
|
destination,
|
2021-09-04 19:25:09 +02:00
|
|
|
cleanup,
|
2022-10-17 08:29:40 +11:00
|
|
|
} => self.codegen_asm_terminator(
|
|
|
|
helper,
|
|
|
|
bx,
|
|
|
|
terminator,
|
|
|
|
template,
|
|
|
|
operands,
|
|
|
|
options,
|
|
|
|
line_spans,
|
|
|
|
destination,
|
|
|
|
cleanup,
|
|
|
|
self.instance,
|
|
|
|
mergeable_succ(),
|
|
|
|
),
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn codegen_argument(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
op: OperandRef<'tcx, Bx::Value>,
|
|
|
|
llargs: &mut Vec<Bx::Value>,
|
2019-10-29 16:35:26 +02:00
|
|
|
arg: &ArgAbi<'tcx, Ty<'tcx>>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) {
|
2022-08-25 22:19:38 +10:00
|
|
|
match arg.mode {
|
|
|
|
PassMode::Ignore => return,
|
|
|
|
PassMode::Cast(_, true) => {
|
|
|
|
// Fill padding with undef value, where applicable.
|
|
|
|
llargs.push(bx.const_undef(bx.reg_backend_type(&Reg::i32())));
|
|
|
|
}
|
|
|
|
PassMode::Pair(..) => match op.val {
|
2017-10-10 20:54:50 +03:00
|
|
|
Pair(a, b) => {
|
|
|
|
llargs.push(a);
|
|
|
|
llargs.push(b);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-19 15:30:23 +02:00
|
|
|
_ => bug!("codegen_argument: {:?} invalid for pair argument", op),
|
2022-08-25 22:19:38 +10:00
|
|
|
},
|
|
|
|
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => match op.val {
|
2018-08-03 23:50:13 +09:00
|
|
|
Ref(a, Some(b), _) => {
|
2018-05-29 00:12:55 +09:00
|
|
|
llargs.push(a);
|
|
|
|
llargs.push(b);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => bug!("codegen_argument: {:?} invalid for unsized indirect argument", op),
|
2022-08-25 22:19:38 +10:00
|
|
|
},
|
|
|
|
_ => {}
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
// Force by-ref if we have to load through a cast pointer.
|
2017-02-06 17:27:09 +01:00
|
|
|
let (mut llval, align, by_ref) = match op.val {
|
2016-06-20 23:55:14 +03:00
|
|
|
Immediate(_) | Pair(..) => match arg.mode {
|
2022-08-25 22:19:38 +10:00
|
|
|
PassMode::Indirect { .. } | PassMode::Cast(..) => {
|
2019-09-12 19:04:30 +03:00
|
|
|
let scratch = PlaceRef::alloca(bx, arg.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
op.val.store(bx, scratch);
|
2017-12-01 19:16:39 +02:00
|
|
|
(scratch.llval, scratch.align, true)
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
2018-09-09 01:16:45 +03:00
|
|
|
_ => (op.immediate_or_packed_pair(bx), arg.layout.align.abi, false),
|
2016-03-06 13:23:20 +02:00
|
|
|
},
|
2018-08-03 23:50:13 +09:00
|
|
|
Ref(llval, _, align) => {
|
2018-09-09 01:16:45 +03:00
|
|
|
if arg.is_indirect() && align < arg.layout.align.abi {
|
2017-12-01 19:16:39 +02:00
|
|
|
// `foo(packed.large_field)`. We can't pass the (unaligned) field directly. I
|
|
|
|
// think that ATM (Rust 1.16) we only pass temporaries, but we shouldn't
|
|
|
|
// have scary latent bugs around.
|
|
|
|
|
2019-09-12 19:04:30 +03:00
|
|
|
let scratch = PlaceRef::alloca(bx, arg.layout);
|
2018-11-02 23:38:16 +01:00
|
|
|
base::memcpy_ty(
|
|
|
|
bx,
|
|
|
|
scratch.llval,
|
|
|
|
scratch.align,
|
|
|
|
llval,
|
|
|
|
align,
|
|
|
|
op.layout,
|
|
|
|
MemFlags::empty(),
|
|
|
|
);
|
2017-12-01 19:16:39 +02:00
|
|
|
(scratch.llval, scratch.align, true)
|
|
|
|
} else {
|
|
|
|
(llval, align, true)
|
|
|
|
}
|
2017-02-06 17:27:09 +01:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if by_ref && !arg.is_indirect() {
|
|
|
|
// Have to load the argument, maybe while casting it.
|
2022-08-25 22:19:38 +10:00
|
|
|
if let PassMode::Cast(ty, _) = &arg.mode {
|
2022-08-25 17:52:37 +10:00
|
|
|
let llty = bx.cast_backend_type(ty);
|
2021-07-04 18:53:04 +02:00
|
|
|
let addr = bx.pointercast(llval, bx.type_ptr_to(llty));
|
|
|
|
llval = bx.load(llty, addr, align.min(arg.layout.align.abi));
|
2016-03-06 13:23:20 +02:00
|
|
|
} else {
|
2017-12-01 14:31:47 +02:00
|
|
|
// We can't use `PlaceRef::load` here because the argument
|
2017-09-26 14:41:06 +03:00
|
|
|
// may have a type we don't treat as immediate, but the ABI
|
|
|
|
// used for this call is passing it by-value. In that case,
|
|
|
|
// the load would just produce `OperandValue::Ref` instead
|
|
|
|
// of the `OperandValue::Immediate` we need for the call.
|
2021-07-04 18:53:04 +02:00
|
|
|
llval = bx.load(bx.backend_type(arg.layout), llval, align);
|
2021-08-29 11:06:55 +02:00
|
|
|
if let abi::Abi::Scalar(scalar) = arg.layout.abi {
|
2017-09-26 14:41:06 +03:00
|
|
|
if scalar.is_bool() {
|
2021-08-29 11:06:55 +02:00
|
|
|
bx.range_metadata(llval, WrappingRange { start: 0, end: 1 });
|
2017-09-26 14:41:06 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
// We store bools as `i8` so we need to truncate to `i1`.
|
2020-08-29 18:10:01 +02:00
|
|
|
llval = bx.to_immediate(llval, arg.layout);
|
2017-09-23 15:04:37 +03:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llargs.push(llval);
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn codegen_arguments_untupled(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
operand: &mir::Operand<'tcx>,
|
|
|
|
llargs: &mut Vec<Bx::Value>,
|
2019-10-29 16:35:26 +02:00
|
|
|
args: &[ArgAbi<'tcx, Ty<'tcx>>],
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
) -> usize {
|
2019-10-14 01:38:38 -04:00
|
|
|
let tuple = self.codegen_operand(bx, operand);
|
2016-03-06 13:23:20 +02:00
|
|
|
|
2016-04-21 11:43:01 +12:00
|
|
|
// Handle both by-ref and immediate tuples.
|
2018-08-03 23:50:13 +09:00
|
|
|
if let Ref(llval, None, align) = tuple.val {
|
2019-08-29 14:24:50 -04:00
|
|
|
let tuple_ptr = PlaceRef::new_sized_aligned(llval, tuple.layout, align);
|
2017-10-09 19:56:41 +03:00
|
|
|
for i in 0..tuple.layout.fields.count() {
|
2018-01-05 07:12:32 +02:00
|
|
|
let field_ptr = tuple_ptr.project_field(bx, i);
|
2018-10-05 15:08:49 +02:00
|
|
|
let field = bx.load_operand(field_ptr);
|
|
|
|
self.codegen_argument(bx, field, llargs, &args[i]);
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
2018-08-03 23:50:13 +09:00
|
|
|
} else if let Ref(_, Some(_), _) = tuple.val {
|
2018-05-29 00:12:55 +09:00
|
|
|
bug!("closure arguments must be sized")
|
2017-10-09 19:56:41 +03:00
|
|
|
} else {
|
|
|
|
// If the tuple is immediate, the elements are as well.
|
|
|
|
for i in 0..tuple.layout.fields.count() {
|
2018-01-05 07:12:32 +02:00
|
|
|
let op = tuple.extract_field(bx, i);
|
2018-05-08 16:10:16 +03:00
|
|
|
self.codegen_argument(bx, op, llargs, &args[i]);
|
2016-04-20 15:27:15 +12:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
tuple.layout.fields.count()
|
2016-03-06 13:23:20 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 06:52:37 +03:00
|
|
|
fn get_caller_location(
|
|
|
|
&mut self,
|
|
|
|
bx: &mut Bx,
|
|
|
|
mut source_info: mir::SourceInfo,
|
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
|
|
|
let tcx = bx.tcx();
|
|
|
|
|
|
|
|
let mut span_to_caller_location = |span: Span| {
|
2019-10-27 17:31:12 -07:00
|
|
|
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
|
2020-09-21 06:52:37 +03:00
|
|
|
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
|
|
|
|
let const_loc = tcx.const_caller_location((
|
2021-04-19 23:27:02 +01:00
|
|
|
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
|
2019-10-27 17:31:12 -07:00
|
|
|
caller.line as u32,
|
|
|
|
caller.col_display as u32 + 1,
|
|
|
|
));
|
2020-02-15 12:57:46 +13:00
|
|
|
OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty())
|
2020-09-21 06:52:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Walk up the `SourceScope`s, in case some of them are from MIR inlining.
|
|
|
|
// If so, the starting `source_info.span` is in the innermost inlined
|
|
|
|
// function, and will be replaced with outer callsite spans as long
|
|
|
|
// as the inlined functions were `#[track_caller]`.
|
|
|
|
loop {
|
|
|
|
let scope_data = &self.mir.source_scopes[source_info.scope];
|
|
|
|
|
|
|
|
if let Some((callee, callsite_span)) = scope_data.inlined {
|
|
|
|
// Stop inside the most nested non-`#[track_caller]` function,
|
|
|
|
// before ever reaching its caller (which is irrelevant).
|
|
|
|
if !callee.def.requires_caller_location(tcx) {
|
|
|
|
return span_to_caller_location(source_info.span);
|
|
|
|
}
|
|
|
|
source_info.span = callsite_span;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip past all of the parents with `inlined: None`.
|
|
|
|
match scope_data.inlined_parent_scope {
|
|
|
|
Some(parent) => source_info.scope = parent,
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No inlined `SourceScope`s, or all of them were `#[track_caller]`.
|
|
|
|
self.caller_location.unwrap_or_else(|| span_to_caller_location(source_info.span))
|
2019-10-24 17:35:02 -07:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn get_personality_slot(&mut self, bx: &mut Bx) -> PlaceRef<'tcx, Bx::Value> {
|
2018-08-28 17:50:57 +02:00
|
|
|
let cx = bx.cx();
|
2017-06-01 21:50:53 +03:00
|
|
|
if let Some(slot) = self.personality_slot {
|
2016-01-02 00:45:21 +02:00
|
|
|
slot
|
|
|
|
} else {
|
2018-09-20 15:47:22 +02:00
|
|
|
let layout = cx.layout_of(
|
|
|
|
cx.tcx().intern_tup(&[cx.tcx().mk_mut_ptr(cx.tcx().types.u8), cx.tcx().types.i32]),
|
2018-01-21 13:33:21 +08:00
|
|
|
);
|
2019-09-12 19:04:30 +03:00
|
|
|
let slot = PlaceRef::alloca(bx, layout);
|
2017-06-01 21:50:53 +03:00
|
|
|
self.personality_slot = Some(slot);
|
2016-12-10 20:32:44 -07:00
|
|
|
slot
|
2016-01-02 00:45:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:17:46 +03:00
|
|
|
/// Returns the landing/cleanup pad wrapper around the given basic block.
|
|
|
|
// FIXME(eddyb) rename this to `eh_pad_for`.
|
|
|
|
fn landing_pad_for(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock {
|
|
|
|
if let Some(landing_pad) = self.landing_pads[bb] {
|
|
|
|
return landing_pad;
|
2016-05-29 22:01:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-15 09:17:46 +03:00
|
|
|
let landing_pad = self.landing_pad_for_uncached(bb);
|
|
|
|
self.landing_pads[bb] = Some(landing_pad);
|
2017-03-14 01:08:21 +02:00
|
|
|
landing_pad
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:17:46 +03:00
|
|
|
// FIXME(eddyb) rename this to `eh_pad_for_uncached`.
|
|
|
|
fn landing_pad_for_uncached(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock {
|
2021-05-06 17:37:19 +03:00
|
|
|
let llbb = self.llbb(bb);
|
2018-01-05 07:04:08 +02:00
|
|
|
if base::wants_msvc_seh(self.cx.sess()) {
|
2021-05-15 09:17:46 +03:00
|
|
|
let funclet;
|
|
|
|
let ret_llbb;
|
|
|
|
match self.mir[bb].terminator.as_ref().map(|t| &t.kind) {
|
|
|
|
// This is a basic block that we're aborting the program for,
|
|
|
|
// notably in an `extern` function. These basic blocks are inserted
|
|
|
|
// so that we assert that `extern` functions do indeed not panic,
|
|
|
|
// and if they do we abort the process.
|
|
|
|
//
|
|
|
|
// On MSVC these are tricky though (where we're doing funclets). If
|
|
|
|
// we were to do a cleanuppad (like below) the normal functions like
|
|
|
|
// `longjmp` would trigger the abort logic, terminating the
|
|
|
|
// program. Instead we insert the equivalent of `catch(...)` for C++
|
|
|
|
// which magically doesn't trigger when `longjmp` files over this
|
|
|
|
// frame.
|
|
|
|
//
|
|
|
|
// Lots more discussion can be found on #48251 but this codegen is
|
|
|
|
// modeled after clang's for:
|
|
|
|
//
|
|
|
|
// try {
|
|
|
|
// foo();
|
|
|
|
// } catch (...) {
|
|
|
|
// bar();
|
|
|
|
// }
|
|
|
|
Some(&mir::TerminatorKind::Abort) => {
|
2022-10-19 10:34:45 +11:00
|
|
|
let cs_llbb =
|
2022-02-18 15:20:19 +01:00
|
|
|
Bx::append_block(self.cx, self.llfn, &format!("cs_funclet{:?}", bb));
|
2022-10-19 10:34:45 +11:00
|
|
|
let cp_llbb =
|
2022-02-18 15:20:19 +01:00
|
|
|
Bx::append_block(self.cx, self.llfn, &format!("cp_funclet{:?}", bb));
|
2022-10-19 10:34:45 +11:00
|
|
|
ret_llbb = cs_llbb;
|
2021-05-15 09:17:46 +03:00
|
|
|
|
2022-10-19 10:34:45 +11:00
|
|
|
let mut cs_bx = Bx::build(self.cx, cs_llbb);
|
|
|
|
let cs = cs_bx.catch_switch(None, None, &[cp_llbb]);
|
2021-05-15 09:17:46 +03:00
|
|
|
|
|
|
|
// The "null" here is actually a RTTI type descriptor for the
|
|
|
|
// C++ personality function, but `catch (...)` has no type so
|
|
|
|
// it's null. The 64 here is actually a bitfield which
|
|
|
|
// represents that this is a catch-all block.
|
2022-10-19 10:34:45 +11:00
|
|
|
let mut cp_bx = Bx::build(self.cx, cp_llbb);
|
2021-05-15 09:17:46 +03:00
|
|
|
let null = cp_bx.const_null(
|
|
|
|
cp_bx.type_i8p_ext(cp_bx.cx().data_layout().instruction_address_space),
|
|
|
|
);
|
|
|
|
let sixty_four = cp_bx.const_i32(64);
|
|
|
|
funclet = cp_bx.catch_pad(cs, &[null, sixty_four, null]);
|
|
|
|
cp_bx.br(llbb);
|
|
|
|
}
|
|
|
|
_ => {
|
2022-10-19 10:34:45 +11:00
|
|
|
let cleanup_llbb =
|
2022-02-18 15:20:19 +01:00
|
|
|
Bx::append_block(self.cx, self.llfn, &format!("funclet_{:?}", bb));
|
2022-10-19 10:34:45 +11:00
|
|
|
ret_llbb = cleanup_llbb;
|
|
|
|
let mut cleanup_bx = Bx::build(self.cx, cleanup_llbb);
|
2021-05-15 09:17:46 +03:00
|
|
|
funclet = cleanup_bx.cleanup_pad(None, &[]);
|
|
|
|
cleanup_bx.br(llbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.funclets[bb] = Some(funclet);
|
|
|
|
ret_llbb
|
|
|
|
} else {
|
2022-10-19 10:34:45 +11:00
|
|
|
let cleanup_llbb = Bx::append_block(self.cx, self.llfn, "cleanup");
|
|
|
|
let mut cleanup_bx = Bx::build(self.cx, cleanup_llbb);
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2021-05-15 09:17:46 +03:00
|
|
|
let llpersonality = self.cx.eh_personality();
|
2022-12-03 18:27:18 +00:00
|
|
|
let (exn0, exn1) = cleanup_bx.cleanup_landing_pad(llpersonality);
|
2017-06-01 21:50:53 +03:00
|
|
|
|
2022-10-19 10:34:45 +11:00
|
|
|
let slot = self.get_personality_slot(&mut cleanup_bx);
|
|
|
|
slot.storage_live(&mut cleanup_bx);
|
2022-12-03 18:27:18 +00:00
|
|
|
Pair(exn0, exn1).store(&mut cleanup_bx, slot);
|
2017-06-01 21:50:53 +03:00
|
|
|
|
2022-10-19 10:34:45 +11:00
|
|
|
cleanup_bx.br(llbb);
|
|
|
|
cleanup_llbb
|
2021-05-15 09:17:46 +03:00
|
|
|
}
|
2015-12-19 16:48:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn unreachable_block(&mut self) -> Bx::BasicBlock {
|
2016-02-01 11:04:46 +01:00
|
|
|
self.unreachable_block.unwrap_or_else(|| {
|
2022-02-18 15:20:19 +01:00
|
|
|
let llbb = Bx::append_block(self.cx, self.llfn, "unreachable");
|
|
|
|
let mut bx = Bx::build(self.cx, llbb);
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.unreachable();
|
2022-02-18 15:20:19 +01:00
|
|
|
self.unreachable_block = Some(llbb);
|
|
|
|
llbb
|
2016-02-01 11:04:46 +01:00
|
|
|
})
|
2015-12-19 16:47:52 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 23:54:26 +00:00
|
|
|
fn double_unwind_guard(&mut self) -> Bx::BasicBlock {
|
|
|
|
self.double_unwind_guard.unwrap_or_else(|| {
|
|
|
|
assert!(!base::wants_msvc_seh(self.cx.sess()));
|
|
|
|
|
2022-02-18 15:20:19 +01:00
|
|
|
let llbb = Bx::append_block(self.cx, self.llfn, "abort");
|
|
|
|
let mut bx = Bx::build(self.cx, llbb);
|
2022-02-14 19:44:24 +00:00
|
|
|
self.set_debug_loc(&mut bx, mir::SourceInfo::outermost(self.mir.span));
|
|
|
|
|
2022-01-14 23:54:26 +00:00
|
|
|
let llpersonality = self.cx.eh_personality();
|
2022-12-03 18:27:18 +00:00
|
|
|
bx.cleanup_landing_pad(llpersonality);
|
2022-01-14 23:54:26 +00:00
|
|
|
|
2022-12-21 13:49:48 +01:00
|
|
|
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, LangItem::PanicCannotUnwind);
|
2022-01-14 23:54:26 +00:00
|
|
|
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
|
|
|
|
|
2022-10-01 17:01:31 +00:00
|
|
|
let llret = bx.call(fn_ty, Some(&fn_abi), fn_ptr, &[], None);
|
2022-02-26 12:52:07 -05:00
|
|
|
bx.do_not_inline(llret);
|
2022-01-14 23:54:26 +00:00
|
|
|
|
|
|
|
bx.unreachable();
|
|
|
|
|
|
|
|
self.double_unwind_guard = Some(llbb);
|
|
|
|
llbb
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:37:19 +03:00
|
|
|
/// Get the backend `BasicBlock` for a MIR `BasicBlock`, either already
|
|
|
|
/// cached in `self.cached_llbbs`, or created on demand (and cached).
|
|
|
|
// FIXME(eddyb) rename `llbb` and other `ll`-prefixed things to use a
|
|
|
|
// more backend-agnostic prefix such as `cg` (i.e. this would be `cgbb`).
|
|
|
|
pub fn llbb(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock {
|
2022-10-17 08:29:40 +11:00
|
|
|
self.try_llbb(bb).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `llbb`, but may fail if the basic block should be skipped.
|
|
|
|
pub fn try_llbb(&mut self, bb: mir::BasicBlock) -> Option<Bx::BasicBlock> {
|
|
|
|
match self.cached_llbbs[bb] {
|
|
|
|
CachedLlbb::None => {
|
|
|
|
// FIXME(eddyb) only name the block if `fewer_names` is `false`.
|
|
|
|
let llbb = Bx::append_block(self.cx, self.llfn, &format!("{:?}", bb));
|
|
|
|
self.cached_llbbs[bb] = CachedLlbb::Some(llbb);
|
|
|
|
Some(llbb)
|
|
|
|
}
|
|
|
|
CachedLlbb::Some(llbb) => Some(llbb),
|
|
|
|
CachedLlbb::Skip => None,
|
|
|
|
}
|
2021-05-06 17:37:19 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn make_return_dest(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2020-03-31 14:31:34 -03:00
|
|
|
dest: mir::Place<'tcx>,
|
2019-10-29 16:35:26 +02:00
|
|
|
fn_ret: &ArgAbi<'tcx, Ty<'tcx>>,
|
2019-11-04 19:52:19 -05:00
|
|
|
llargs: &mut Vec<Bx::Value>,
|
|
|
|
is_intrinsic: bool,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> ReturnDest<'tcx, Bx::Value> {
|
2019-02-28 22:43:53 +00:00
|
|
|
// If the return is ignored, we can just return a do-nothing `ReturnDest`.
|
2017-09-20 18:17:23 +03:00
|
|
|
if fn_ret.is_ignore() {
|
2016-04-06 17:57:42 +12:00
|
|
|
return ReturnDest::Nothing;
|
|
|
|
}
|
2019-10-20 16:09:36 -04:00
|
|
|
let dest = if let Some(index) = dest.as_local() {
|
2016-06-20 23:55:14 +03:00
|
|
|
match self.locals[index] {
|
2017-12-01 14:31:47 +02:00
|
|
|
LocalRef::Place(dest) => dest,
|
2018-05-29 00:12:55 +09:00
|
|
|
LocalRef::UnsizedPlace(_) => bug!("return type must be sized"),
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(None) => {
|
2019-02-28 22:43:53 +00:00
|
|
|
// Handle temporary places, specifically `Operand` ones, as
|
|
|
|
// they don't have `alloca`s.
|
2017-09-20 18:17:23 +03:00
|
|
|
return if fn_ret.is_indirect() {
|
2016-06-20 23:55:14 +03:00
|
|
|
// Odd, but possible, case, we have an operand temporary,
|
|
|
|
// but the calling convention has an indirect return.
|
2019-09-12 19:04:30 +03:00
|
|
|
let tmp = PlaceRef::alloca(bx, fn_ret.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_live(bx);
|
2017-02-06 17:27:09 +01:00
|
|
|
llargs.push(tmp.llval);
|
2017-06-01 21:50:53 +03:00
|
|
|
ReturnDest::IndirectOperand(tmp, index)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else if is_intrinsic {
|
|
|
|
// Currently, intrinsics always need a location to store
|
2019-02-28 22:43:53 +00:00
|
|
|
// the result, so we create a temporary `alloca` for the
|
|
|
|
// result.
|
2019-09-12 19:04:30 +03:00
|
|
|
let tmp = PlaceRef::alloca(bx, fn_ret.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_live(bx);
|
2017-06-01 21:50:53 +03:00
|
|
|
ReturnDest::IndirectOperand(tmp, index)
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
|
|
|
ReturnDest::DirectOperand(index)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
LocalRef::Operand(Some(_)) => {
|
2017-12-01 14:39:51 +02:00
|
|
|
bug!("place local already assigned to");
|
2016-04-06 17:57:42 +12:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_place(
|
|
|
|
bx,
|
2020-01-14 02:10:05 -03:00
|
|
|
mir::PlaceRef { local: dest.local, projection: &dest.projection },
|
2019-07-02 20:29:45 +02:00
|
|
|
)
|
2016-04-06 17:57:42 +12:00
|
|
|
};
|
2017-09-20 18:17:23 +03:00
|
|
|
if fn_ret.is_indirect() {
|
2018-09-09 01:16:45 +03:00
|
|
|
if dest.align < dest.layout.align.abi {
|
2017-12-01 19:16:39 +02:00
|
|
|
// Currently, MIR code generation does not create calls
|
|
|
|
// that store directly to fields of packed structs (in
|
2019-02-28 22:43:53 +00:00
|
|
|
// fact, the calls it creates write only to temps).
|
2017-12-01 19:16:39 +02:00
|
|
|
//
|
|
|
|
// If someone changes that, please update this code path
|
|
|
|
// to create a temporary.
|
2019-10-14 01:38:38 -04:00
|
|
|
span_bug!(self.mir.span, "can't directly store to unaligned value");
|
2017-03-09 13:28:26 +02:00
|
|
|
}
|
2017-12-01 19:16:39 +02:00
|
|
|
llargs.push(dest.llval);
|
|
|
|
ReturnDest::Nothing
|
2016-04-06 17:57:42 +12:00
|
|
|
} else {
|
2017-06-25 12:41:24 +03:00
|
|
|
ReturnDest::Store(dest)
|
2016-04-06 17:57:42 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:35:01 -03:00
|
|
|
fn codegen_transmute(&mut self, bx: &mut Bx, src: &mir::Operand<'tcx>, dst: mir::Place<'tcx>) {
|
2019-10-20 16:09:36 -04:00
|
|
|
if let Some(index) = dst.as_local() {
|
2017-02-06 17:37:58 +01:00
|
|
|
match self.locals[index] {
|
2019-10-14 01:38:38 -04:00
|
|
|
LocalRef::Place(place) => self.codegen_transmute_into(bx, src, place),
|
2018-05-29 00:12:55 +09:00
|
|
|
LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"),
|
2017-02-06 17:37:58 +01:00
|
|
|
LocalRef::Operand(None) => {
|
2020-01-14 01:51:59 -03:00
|
|
|
let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst.as_ref()));
|
2022-01-12 03:19:52 +00:00
|
|
|
assert!(!dst_layout.ty.has_erasable_regions());
|
2019-09-12 19:04:30 +03:00
|
|
|
let place = PlaceRef::alloca(bx, dst_layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
place.storage_live(bx);
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_transmute_into(bx, src, place);
|
2018-09-14 17:48:57 +02:00
|
|
|
let op = bx.load_operand(place);
|
2018-01-05 07:12:32 +02:00
|
|
|
place.storage_dead(bx);
|
2017-02-06 17:37:58 +01:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2020-02-08 19:20:45 +02:00
|
|
|
self.debug_introduce_local(bx, index);
|
2017-02-06 17:37:58 +01:00
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
LocalRef::Operand(Some(op)) => {
|
2017-02-06 17:37:58 +01:00
|
|
|
assert!(op.layout.is_zst(), "assigning to initialized SSAtemp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-14 01:51:59 -03:00
|
|
|
let dst = self.codegen_place(bx, dst.as_ref());
|
2019-10-14 01:38:38 -04:00
|
|
|
self.codegen_transmute_into(bx, src, dst);
|
2017-02-06 17:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn codegen_transmute_into(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
src: &mir::Operand<'tcx>,
|
|
|
|
dst: PlaceRef<'tcx, Bx::Value>,
|
|
|
|
) {
|
2019-10-14 01:38:38 -04:00
|
|
|
let src = self.codegen_operand(bx, src);
|
2020-12-07 17:33:43 +02:00
|
|
|
|
|
|
|
// Special-case transmutes between scalars as simple bitcasts.
|
2021-08-29 11:06:55 +02:00
|
|
|
match (src.layout.abi, dst.layout.abi) {
|
2020-12-07 17:33:43 +02:00
|
|
|
(abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => {
|
|
|
|
// HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers.
|
2023-01-22 23:03:58 -05:00
|
|
|
let src_is_ptr = matches!(src_scalar.primitive(), abi::Pointer(_));
|
|
|
|
let dst_is_ptr = matches!(dst_scalar.primitive(), abi::Pointer(_));
|
2022-12-11 17:27:01 -05:00
|
|
|
if src_is_ptr == dst_is_ptr {
|
2020-12-07 17:33:43 +02:00
|
|
|
assert_eq!(src.layout.size, dst.layout.size);
|
|
|
|
|
|
|
|
// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
|
|
|
|
// conversions allow handling `bool`s the same as `u8`s.
|
|
|
|
let src = bx.from_immediate(src.immediate());
|
2022-12-11 17:27:01 -05:00
|
|
|
// LLVM also doesn't like `bitcast`s between pointers in different address spaces.
|
|
|
|
let src_as_dst = if src_is_ptr {
|
|
|
|
bx.pointercast(src, bx.backend_type(dst.layout))
|
|
|
|
} else {
|
|
|
|
bx.bitcast(src, bx.backend_type(dst.layout))
|
|
|
|
};
|
2020-12-07 17:33:43 +02:00
|
|
|
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:00:25 +01:00
|
|
|
let llty = bx.backend_type(src.layout);
|
|
|
|
let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty));
|
2018-09-09 01:16:45 +03:00
|
|
|
let align = src.layout.align.abi.min(dst.align);
|
2019-08-29 14:24:50 -04:00
|
|
|
src.val.store(bx, PlaceRef::new_sized_aligned(cast_ptr, src.layout, align));
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the return value of a function call into it's final location.
|
2018-09-20 15:47:22 +02:00
|
|
|
fn store_return(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: ReturnDest<'tcx, Bx::Value>,
|
2019-10-29 16:35:26 +02:00
|
|
|
ret_abi: &ArgAbi<'tcx, Ty<'tcx>>,
|
2018-09-20 15:47:22 +02:00
|
|
|
llval: Bx::Value,
|
|
|
|
) {
|
2016-04-04 19:21:27 +12:00
|
|
|
use self::ReturnDest::*;
|
|
|
|
|
2016-06-08 00:35:01 +03:00
|
|
|
match dest {
|
|
|
|
Nothing => (),
|
2019-10-29 16:35:26 +02:00
|
|
|
Store(dst) => bx.store_arg(&ret_abi, llval, dst),
|
2016-06-20 23:55:14 +03:00
|
|
|
IndirectOperand(tmp, index) => {
|
2018-09-14 17:48:57 +02:00
|
|
|
let op = bx.load_operand(tmp);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_dead(bx);
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2020-02-08 19:20:45 +02:00
|
|
|
self.debug_introduce_local(bx, index);
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
DirectOperand(index) => {
|
2016-06-08 00:35:01 +03:00
|
|
|
// If there is a cast, we have to store and reload.
|
2022-08-25 22:19:38 +10:00
|
|
|
let op = if let PassMode::Cast(..) = ret_abi.mode {
|
2019-10-29 16:35:26 +02:00
|
|
|
let tmp = PlaceRef::alloca(bx, ret_abi.layout);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_live(bx);
|
2019-10-29 16:35:26 +02:00
|
|
|
bx.store_arg(&ret_abi, llval, tmp);
|
2018-09-14 17:48:57 +02:00
|
|
|
let op = bx.load_operand(tmp);
|
2018-01-05 07:12:32 +02:00
|
|
|
tmp.storage_dead(bx);
|
2017-06-01 21:50:53 +03:00
|
|
|
op
|
2016-04-08 15:37:56 +12:00
|
|
|
} else {
|
2019-10-29 16:35:26 +02:00
|
|
|
OperandRef::from_immediate_or_packed_pair(bx, llval, ret_abi.layout)
|
2016-06-08 00:35:01 +03:00
|
|
|
};
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = LocalRef::Operand(Some(op));
|
2020-02-08 19:20:45 +02:00
|
|
|
self.debug_introduce_local(bx, index);
|
2016-04-04 19:21:27 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:48:44 +03:00
|
|
|
enum ReturnDest<'tcx, V> {
|
2019-02-28 22:43:53 +00:00
|
|
|
// Do nothing; the return value is indirect or ignored.
|
2016-04-04 19:21:27 +12:00
|
|
|
Nothing,
|
2019-02-28 22:43:53 +00:00
|
|
|
// Store the return value to the pointer.
|
2018-08-02 17:48:44 +03:00
|
|
|
Store(PlaceRef<'tcx, V>),
|
2019-02-28 22:43:53 +00:00
|
|
|
// Store an indirect return value to an operand local place.
|
2018-08-02 17:48:44 +03:00
|
|
|
IndirectOperand(PlaceRef<'tcx, V>, mir::Local),
|
2019-02-28 22:43:53 +00:00
|
|
|
// Store a direct return value to an operand local place.
|
2016-06-20 23:55:14 +03:00
|
|
|
DirectOperand(mir::Local),
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|