2015-10-21 17:42:25 -04:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use libc::c_uint;
|
2018-09-20 15:47:22 +02:00
|
|
|
use llvm;
|
2018-11-03 15:48:29 +01:00
|
|
|
use llvm_util;
|
2018-05-02 13:14:30 +02:00
|
|
|
use rustc::ty::{self, Ty, TypeFoldable, UpvarSubsts};
|
2018-09-13 14:58:19 +02:00
|
|
|
use rustc::ty::layout::{LayoutOf, TyLayout, HasTyCtxt};
|
2016-12-16 20:52:20 -07:00
|
|
|
use rustc::mir::{self, Mir};
|
2016-12-18 16:05:40 -07:00
|
|
|
use rustc::ty::subst::Substs;
|
2018-07-26 11:41:10 -06:00
|
|
|
use rustc::session::config::DebugInfo;
|
2016-03-22 19:23:36 +02:00
|
|
|
use base;
|
2018-09-20 15:47:22 +02:00
|
|
|
use debuginfo::{self, VariableAccess, VariableKind, FunctionDebugContext};
|
|
|
|
use common::Funclet;
|
2017-04-21 21:02:14 -04:00
|
|
|
use monomorphize::Instance;
|
2018-09-20 15:47:22 +02:00
|
|
|
use abi::{FnType, PassMode};
|
|
|
|
use interfaces::*;
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
use syntax_pos::{DUMMY_SP, NO_EXPANSION, BytePos, Span};
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::symbol::keywords;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-07-31 22:33:41 +08:00
|
|
|
use std::iter;
|
2016-03-08 14:38:13 +02:00
|
|
|
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
use rustc_data_structures::bit_set::BitSet;
|
2018-07-25 16:14:04 +03:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2016-03-11 18:00:52 +13:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub use self::constant::codegen_static_initializer;
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2016-12-18 17:04:00 -07:00
|
|
|
use self::analyze::CleanupKind;
|
2017-12-01 19:16:39 +02:00
|
|
|
use self::place::PlaceRef;
|
2016-06-05 19:38:22 +03:00
|
|
|
use rustc::mir::traversal;
|
2016-03-11 13:14:51 +13:00
|
|
|
|
Various improvements to MIR and LLVM IR Construction
Primarily affects the MIR construction, which indirectly improves LLVM
IR generation, but some LLVM IR changes have been made too.
* Handle "statement expressions" more intelligently. These are
expressions that always evaluate to `()`. Previously a temporary would
be generated as a destination to translate into, which is unnecessary.
This affects assignment, augmented assignment, `return`, `break` and
`continue`.
* Avoid inserting drops for non-drop types in more places. Scheduled
drops were already skipped for types that we knew wouldn't need
dropping at construction time. However manually-inserted drops like
those for `x` in `x = y;` were still generated. `build_drop` now takes
a type parameter like its `schedule_drop` counterpart and checks to
see if the type needs dropping.
* Avoid generating an extra temporary for an assignment where the types
involved don't need dropping. Previously an expression like
`a = b + 1;` would result in a temporary for `b + 1`. This is so the
RHS can be evaluated, then the LHS evaluated and dropped and have
everything work correctly. However, this isn't necessary if the `LHS`
doesn't need a drop, as we can just overwrite the existing value.
* Improves lvalue analysis to allow treating an `Rvalue::Use` as an
operand in certain conditions. The reason for it never being an
operand is so it can be zeroed/drop-filled, but this is only true for
types that need dropping.
The first two changes result in significantly fewer MIR blocks being
generated, as previously almost every statement would end up generating
a new block due to the drop of the `()` temporary being generated.
2016-04-15 12:36:16 +12:00
|
|
|
use self::operand::{OperandRef, OperandValue};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
/// Master context for codegenning from MIR.
|
2018-09-20 15:47:22 +02:00
|
|
|
pub struct FunctionCx<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> {
|
2018-01-16 09:31:48 +01:00
|
|
|
instance: Instance<'tcx>,
|
|
|
|
|
2016-12-18 23:04:25 -07:00
|
|
|
mir: &'a mir::Mir<'tcx>,
|
2016-12-18 11:08:57 -07:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
debug_context: FunctionDebugContext<Bx::DIScope>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
llfn: Bx::Value,
|
2015-12-19 16:47:52 +02:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
cx: &'a Bx::CodegenCx,
|
2016-12-19 17:48:41 -07:00
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
fn_ty: FnType<'tcx, Ty<'tcx>>,
|
2016-12-19 19:16:36 -07:00
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
/// When unwinding is initiated, we have to store this personality
|
|
|
|
/// value somewhere so that we can load it and re-use it in the
|
|
|
|
/// resume instruction. The personality is (afaik) some kind of
|
|
|
|
/// value used for C++ unwinding, which must filter by type: we
|
|
|
|
/// don't really care about it very much. Anyway, this value
|
|
|
|
/// contains an alloca into which the personality is stored and
|
|
|
|
/// then later loaded when generating the DIVERGE_BLOCK.
|
2018-09-20 15:47:22 +02:00
|
|
|
personality_slot: Option<PlaceRef<'tcx, Bx::Value,>>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
|
|
/// A `Block` for each MIR `BasicBlock`
|
2018-09-20 15:47:22 +02:00
|
|
|
blocks: IndexVec<mir::BasicBlock, Bx::BasicBlock>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
/// The funclet status of each basic block
|
2016-06-07 17:28:36 +03:00
|
|
|
cleanup_kinds: IndexVec<mir::BasicBlock, analyze::CleanupKind>,
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2017-05-23 23:47:15 +03:00
|
|
|
/// When targeting MSVC, this stores the cleanup info for each funclet
|
2018-09-20 15:47:22 +02:00
|
|
|
/// BB. This is initialized as we compute the funclets' head block in RPO.
|
|
|
|
funclets: IndexVec<mir::BasicBlock, Option<Funclet<'static, Bx::Value>>>,
|
2017-05-23 23:47:15 +03:00
|
|
|
|
2016-05-29 22:01:06 +03:00
|
|
|
/// This stores the landing-pad block for a given BB, computed lazily on GNU
|
|
|
|
/// and eagerly on MSVC.
|
2018-09-20 15:47:22 +02:00
|
|
|
landing_pads: IndexVec<mir::BasicBlock, Option<Bx::BasicBlock>>,
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2015-12-19 16:47:52 +02:00
|
|
|
/// Cached unreachable block
|
2018-09-20 15:47:22 +02:00
|
|
|
unreachable_block: Option<Bx::BasicBlock>,
|
2015-12-19 16:47:52 +02:00
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
/// The location where each MIR arg/var/tmp/ret is stored. This is
|
2017-12-01 14:31:47 +02:00
|
|
|
/// usually an `PlaceRef` representing an alloca, but not always:
|
2015-11-02 09:39:59 -05:00
|
|
|
/// sometimes we can skip the alloca and just store the value
|
|
|
|
/// directly using an `OperandRef`, which makes for tighter LLVM
|
|
|
|
/// IR. The conditions for using an `OperandRef` are as follows:
|
|
|
|
///
|
2017-09-21 20:40:50 +03:00
|
|
|
/// - the type of the local must be judged "immediate" by `is_llvm_immediate`
|
2015-11-02 09:39:59 -05:00
|
|
|
/// - the operand must never be referenced indirectly
|
|
|
|
/// - we should not take its address using the `&` operator
|
2017-12-01 14:39:51 +02:00
|
|
|
/// - nor should it appear in a place path like `tmp.a`
|
2015-11-02 09:39:59 -05:00
|
|
|
/// - the operand must be defined by an rvalue that can generate immediate
|
|
|
|
/// values
|
2015-11-03 15:50:04 -05:00
|
|
|
///
|
|
|
|
/// Avoiding allocs can also be important for certain intrinsics,
|
|
|
|
/// notably `expect`.
|
2018-09-20 15:47:22 +02:00
|
|
|
locals: IndexVec<mir::Local, LocalRef<'tcx, Bx::Value>>,
|
2016-04-07 22:35:11 +03:00
|
|
|
|
|
|
|
/// Debug information for MIR scopes.
|
2018-09-20 15:47:22 +02:00
|
|
|
scopes: IndexVec<mir::SourceScope, debuginfo::MirDebugScope<Bx::DIScope>>,
|
2016-12-18 16:05:40 -07:00
|
|
|
|
|
|
|
/// If this function is being monomorphized, this contains the type substitutions used.
|
|
|
|
param_substs: &'tcx Substs<'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2016-12-18 16:05:40 -07:00
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
2018-03-03 08:23:28 -05:00
|
|
|
where T: TypeFoldable<'tcx>
|
2017-04-21 21:02:14 -04:00
|
|
|
{
|
2018-09-20 15:47:22 +02:00
|
|
|
self.cx.tcx().subst_and_normalize_erasing_regions(
|
2018-03-09 12:53:17 -05:00
|
|
|
self.param_substs,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
value,
|
|
|
|
)
|
2016-12-18 16:05:40 -07:00
|
|
|
}
|
|
|
|
|
2018-08-07 17:14:40 +02:00
|
|
|
pub fn set_debug_loc(
|
|
|
|
&mut self,
|
2018-09-20 15:47:22 +02:00
|
|
|
bx: &Bx,
|
2018-08-07 17:14:40 +02:00
|
|
|
source_info: mir::SourceInfo
|
|
|
|
) {
|
2016-12-19 14:38:16 -07:00
|
|
|
let (scope, span) = self.debug_loc(source_info);
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.set_source_location(&self.debug_context, scope, span);
|
2016-12-19 14:38:16 -07:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn debug_loc(&self, source_info: mir::SourceInfo) -> (Option<Bx::DIScope>, Span) {
|
2016-08-24 19:34:31 -07:00
|
|
|
// Bail out if debug info emission is not enabled.
|
2016-12-18 11:08:57 -07:00
|
|
|
match self.debug_context {
|
2016-08-24 19:34:31 -07:00
|
|
|
FunctionDebugContext::DebugInfoDisabled |
|
|
|
|
FunctionDebugContext::FunctionWithoutDebugInfo => {
|
2016-12-16 13:25:18 -07:00
|
|
|
return (self.scopes[source_info.scope].scope_metadata, source_info.span);
|
2016-08-24 19:34:31 -07:00
|
|
|
}
|
|
|
|
FunctionDebugContext::RegularContext(_) =>{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to have a good line stepping behavior in debugger, we overwrite debug
|
|
|
|
// locations of macro expansions with that of the outermost expansion site
|
|
|
|
// (unless the crate is being compiled with `-Z debug-macros`).
|
2017-07-31 23:04:34 +03:00
|
|
|
if source_info.span.ctxt() == NO_EXPANSION ||
|
2018-01-05 07:04:08 +02:00
|
|
|
self.cx.sess().opts.debugging_opts.debug_macros {
|
2017-07-31 23:04:34 +03:00
|
|
|
let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
|
2016-12-19 14:38:16 -07:00
|
|
|
(scope, source_info.span)
|
2016-08-24 19:34:31 -07:00
|
|
|
} else {
|
|
|
|
// Walk up the macro expansion chain until we reach a non-expanded span.
|
2017-08-11 20:34:14 +02:00
|
|
|
// We also stop at the function body level because no line stepping can occur
|
2017-02-09 18:35:19 -08:00
|
|
|
// at the level above that.
|
2016-08-24 19:34:31 -07:00
|
|
|
let mut span = source_info.span;
|
2017-07-31 23:04:34 +03:00
|
|
|
while span.ctxt() != NO_EXPANSION && span.ctxt() != self.mir.span.ctxt() {
|
|
|
|
if let Some(info) = span.ctxt().outer().expn_info() {
|
2017-03-17 04:04:41 +00:00
|
|
|
span = info.call_site;
|
2016-08-24 19:34:31 -07:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 23:04:34 +03:00
|
|
|
let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
|
2017-02-08 22:09:51 -08:00
|
|
|
// Use span of the outermost expansion site, while keeping the original lexical scope.
|
2016-12-19 14:38:16 -07:00
|
|
|
(scope, span)
|
2016-08-24 19:34:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DILocations inherit source file name from the parent DIScope. Due to macro expansions
|
|
|
|
// it may so happen that the current span belongs to a different file than the DIScope
|
2018-05-28 14:16:09 +03:00
|
|
|
// corresponding to span's containing source scope. If so, we need to create a DIScope
|
2016-08-24 19:34:31 -07:00
|
|
|
// "extension" into that file.
|
2018-05-28 14:16:09 +03:00
|
|
|
fn scope_metadata_for_loc(&self, scope_id: mir::SourceScope, pos: BytePos)
|
2018-09-20 15:47:22 +02:00
|
|
|
-> Option<Bx::DIScope> {
|
2016-08-24 19:34:31 -07:00
|
|
|
let scope_metadata = self.scopes[scope_id].scope_metadata;
|
|
|
|
if pos < self.scopes[scope_id].file_start_pos ||
|
|
|
|
pos >= self.scopes[scope_id].file_end_pos {
|
2018-09-20 15:47:22 +02:00
|
|
|
let sm = self.cx.sess().source_map();
|
2017-04-24 19:01:19 +02:00
|
|
|
let defining_crate = self.debug_context.get_ref(DUMMY_SP).defining_crate;
|
2018-09-20 15:47:22 +02:00
|
|
|
Some(self.cx.extend_scope_to_file(
|
|
|
|
scope_metadata.unwrap(),
|
|
|
|
&sm.lookup_char_pos(pos).file,
|
|
|
|
defining_crate,
|
|
|
|
))
|
2016-08-24 19:34:31 -07:00
|
|
|
} else {
|
|
|
|
scope_metadata
|
|
|
|
}
|
2016-06-07 19:21:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:48:44 +03:00
|
|
|
enum LocalRef<'tcx, V> {
|
|
|
|
Place(PlaceRef<'tcx, V>),
|
2018-05-29 00:12:55 +09:00
|
|
|
/// `UnsizedPlace(p)`: `p` itself is a thin pointer (indirect place).
|
|
|
|
/// `*p` is the fat pointer that references the actual unsized place.
|
|
|
|
/// Every time it is initialized, we have to reallocate the place
|
|
|
|
/// and update the fat pointer. That's the reason why it is indirect.
|
2018-08-02 17:48:44 +03:00
|
|
|
UnsizedPlace(PlaceRef<'tcx, V>),
|
|
|
|
Operand(Option<OperandRef<'tcx, V>>),
|
2015-11-02 09:39:59 -05:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
impl<'tcx, V: CodegenObject> LocalRef<'tcx, V> {
|
|
|
|
fn new_operand<Cx: CodegenMethods<'tcx, Value = V>>(
|
|
|
|
cx: &Cx,
|
2018-08-02 17:48:44 +03:00
|
|
|
layout: TyLayout<'tcx>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> LocalRef<'tcx, V> {
|
2017-09-20 18:17:23 +03:00
|
|
|
if layout.is_zst() {
|
2016-04-17 14:37:52 +12:00
|
|
|
// Zero-size temporaries aren't always initialized, which
|
|
|
|
// doesn't matter because they don't contain data, but
|
|
|
|
// we need something in the operand.
|
2018-01-05 07:04:08 +02:00
|
|
|
LocalRef::Operand(Some(OperandRef::new_zst(cx, layout)))
|
2016-04-17 14:37:52 +12:00
|
|
|
} else {
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(None)
|
2016-04-17 14:37:52 +12:00
|
|
|
}
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn codegen_mir<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
cx: &'a Bx::CodegenCx,
|
|
|
|
llfn: Bx::Value,
|
2016-12-18 11:08:57 -07:00
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
instance: Instance<'tcx>,
|
2017-02-13 10:51:06 +02:00
|
|
|
sig: ty::FnSig<'tcx>,
|
2016-12-18 11:08:57 -07:00
|
|
|
) {
|
2018-09-20 15:47:22 +02:00
|
|
|
let fn_ty = cx.new_fn_type(sig, &[]);
|
2016-12-31 04:55:29 +02:00
|
|
|
debug!("fn_ty: {:?}", fn_ty);
|
2016-12-19 07:33:35 -07:00
|
|
|
let debug_context =
|
2018-09-20 15:47:22 +02:00
|
|
|
cx.create_function_debug_context(instance, sig, llfn, mir);
|
|
|
|
let bx = Bx::new_block(cx, llfn, "start");
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2017-05-23 23:47:15 +03:00
|
|
|
if mir.basic_blocks().iter().any(|bb| bb.is_cleanup) {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.set_personality_fn(cx.eh_personality());
|
2017-05-23 23:47:15 +03:00
|
|
|
}
|
2015-11-02 09:39:59 -05:00
|
|
|
|
2017-05-23 23:47:15 +03:00
|
|
|
let cleanup_kinds = analyze::cleanup_kinds(&mir);
|
2017-03-08 20:08:09 +02:00
|
|
|
// Allocate a `Block` for every basic block, except
|
|
|
|
// the start block, if nothing loops back to it.
|
|
|
|
let reentrant_start_block = !mir.predecessors_for(mir::START_BLOCK).is_empty();
|
2018-09-20 15:47:22 +02:00
|
|
|
let block_bxs: IndexVec<mir::BasicBlock, Bx::BasicBlock> =
|
2016-08-24 19:34:31 -07:00
|
|
|
mir.basic_blocks().indices().map(|bb| {
|
2017-03-08 20:08:09 +02:00
|
|
|
if bb == mir::START_BLOCK && !reentrant_start_block {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.llbb()
|
2016-08-24 19:34:31 -07:00
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.build_sibling_block(&format!("{:?}", bb)).llbb()
|
2016-08-24 19:34:31 -07:00
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
// Compute debuginfo scopes from MIR scopes.
|
2018-09-20 15:47:22 +02:00
|
|
|
let scopes = cx.create_mir_scopes(mir, &debug_context);
|
2018-02-26 18:59:47 -08:00
|
|
|
let (landing_pads, funclets) = create_funclets(mir, &bx, &cleanup_kinds, &block_bxs);
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2018-01-05 07:34:28 +02:00
|
|
|
let mut fx = FunctionCx {
|
2018-01-16 09:31:48 +01:00
|
|
|
instance,
|
2017-08-06 22:54:09 -07:00
|
|
|
mir,
|
|
|
|
llfn,
|
|
|
|
fn_ty,
|
2018-01-05 07:04:08 +02:00
|
|
|
cx,
|
2017-06-01 21:50:53 +03:00
|
|
|
personality_slot: None,
|
2018-01-05 07:12:32 +02:00
|
|
|
blocks: block_bxs,
|
2016-08-24 19:34:31 -07:00
|
|
|
unreachable_block: None,
|
2017-08-06 22:54:09 -07:00
|
|
|
cleanup_kinds,
|
|
|
|
landing_pads,
|
2018-09-20 15:47:22 +02:00
|
|
|
funclets,
|
2017-08-06 22:54:09 -07:00
|
|
|
scopes,
|
2016-08-24 19:34:31 -07:00
|
|
|
locals: IndexVec::new(),
|
2017-08-06 22:54:09 -07:00
|
|
|
debug_context,
|
2016-12-18 16:05:40 -07:00
|
|
|
param_substs: {
|
|
|
|
assert!(!instance.substs.needs_infer());
|
|
|
|
instance.substs
|
|
|
|
},
|
2016-08-24 19:34:31 -07:00
|
|
|
};
|
|
|
|
|
2018-02-17 17:46:15 +02:00
|
|
|
let memory_locals = analyze::non_ssa_locals(&fx);
|
2016-12-18 16:05:40 -07:00
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
// Allocate variable and temp allocas
|
2018-01-05 07:34:28 +02:00
|
|
|
fx.locals = {
|
|
|
|
let args = arg_local_refs(&bx, &fx, &fx.scopes, &memory_locals);
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
let allocate_local = |local| {
|
2016-09-25 01:38:27 +02:00
|
|
|
let decl = &mir.local_decls[local];
|
2018-08-28 17:50:57 +02:00
|
|
|
let layout = bx.cx().layout_of(fx.monomorphize(&decl.ty));
|
2017-09-20 18:17:23 +03:00
|
|
|
assert!(!layout.ty.has_erasable_regions());
|
2016-06-20 23:55:14 +03:00
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
if let Some(name) = decl.name {
|
|
|
|
// User variable
|
2018-05-29 13:55:21 +03:00
|
|
|
let debug_scope = fx.scopes[decl.visibility_scope];
|
2018-09-20 15:47:22 +02:00
|
|
|
let dbg = debug_scope.is_valid() &&
|
|
|
|
bx.cx().sess().opts.debuginfo == DebugInfo::Full;
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2018-07-22 19:23:39 +03:00
|
|
|
if !memory_locals.contains(local) && !dbg {
|
2016-09-25 01:38:27 +02:00
|
|
|
debug!("alloc: {:?} ({}) -> operand", local, name);
|
2018-08-28 17:50:57 +02:00
|
|
|
return LocalRef::new_operand(bx.cx(), layout);
|
2016-08-24 19:34:31 -07:00
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("alloc: {:?} ({}) -> place", local, name);
|
2018-05-29 00:12:55 +09:00
|
|
|
if layout.is_unsized() {
|
|
|
|
let indirect_place =
|
|
|
|
PlaceRef::alloca_unsized_indirect(&bx, layout, &name.as_str());
|
|
|
|
// FIXME: add an appropriate debuginfo
|
|
|
|
LocalRef::UnsizedPlace(indirect_place)
|
|
|
|
} else {
|
|
|
|
let place = PlaceRef::alloca(&bx, layout, &name.as_str());
|
|
|
|
if dbg {
|
|
|
|
let (scope, span) = fx.debug_loc(mir::SourceInfo {
|
|
|
|
span: decl.source_info.span,
|
|
|
|
scope: decl.visibility_scope,
|
|
|
|
});
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.declare_local(&fx.debug_context, name, layout.ty, scope.unwrap(),
|
2018-05-29 00:12:55 +09:00
|
|
|
VariableAccess::DirectVariable { alloca: place.llval },
|
|
|
|
VariableKind::LocalVariable, span);
|
|
|
|
}
|
|
|
|
LocalRef::Place(place)
|
2016-09-25 01:38:27 +02:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
} else {
|
2017-12-01 14:39:51 +02:00
|
|
|
// Temporary or return place
|
2018-01-05 07:34:28 +02:00
|
|
|
if local == mir::RETURN_PLACE && fx.fn_ty.ret.is_indirect() {
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("alloc: {:?} (return place) -> place", local);
|
2018-09-20 15:47:22 +02:00
|
|
|
let llretptr = fx.cx.get_param(llfn, 0);
|
2017-12-01 19:16:39 +02:00
|
|
|
LocalRef::Place(PlaceRef::new_sized(llretptr, layout, layout.align))
|
2018-07-22 19:23:39 +03:00
|
|
|
} else if memory_locals.contains(local) {
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("alloc: {:?} -> place", local);
|
2018-05-29 00:12:55 +09:00
|
|
|
if layout.is_unsized() {
|
|
|
|
let indirect_place =
|
|
|
|
PlaceRef::alloca_unsized_indirect(&bx, layout, &format!("{:?}", local));
|
|
|
|
LocalRef::UnsizedPlace(indirect_place)
|
|
|
|
} else {
|
|
|
|
LocalRef::Place(PlaceRef::alloca(&bx, layout, &format!("{:?}", local)))
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
} else {
|
|
|
|
// If this is an immediate local, we do not create an
|
|
|
|
// alloca in advance. Instead we wait until we see the
|
|
|
|
// definition and update the operand there.
|
|
|
|
debug!("alloc: {:?} -> operand", local);
|
2018-08-28 17:50:57 +02:00
|
|
|
LocalRef::new_operand(bx.cx(), layout)
|
2016-09-25 01:38:27 +02:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
};
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
let retptr = allocate_local(mir::RETURN_PLACE);
|
2016-09-25 01:38:27 +02:00
|
|
|
iter::once(retptr)
|
|
|
|
.chain(args.into_iter())
|
2016-09-26 22:55:07 +02:00
|
|
|
.chain(mir.vars_and_temps_iter().map(allocate_local))
|
2016-09-25 01:38:27 +02:00
|
|
|
.collect()
|
2016-06-20 23:55:14 +03:00
|
|
|
};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2017-03-08 20:08:09 +02:00
|
|
|
// Branch to the START block, if it's not the entry block.
|
|
|
|
if reentrant_start_block {
|
2018-01-05 07:34:28 +02:00
|
|
|
bx.br(fx.blocks[mir::START_BLOCK]);
|
2017-03-08 20:08:09 +02:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
// Up until here, IR instructions for this function have explicitly not been annotated with
|
|
|
|
// source code location, so we don't step into call setup code. From here on, source location
|
|
|
|
// emitting should be enabled.
|
2018-01-05 07:34:28 +02:00
|
|
|
debuginfo::start_emitting_source_locations(&fx.debug_context);
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-12-18 17:04:00 -07:00
|
|
|
let rpo = traversal::reverse_postorder(&mir);
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
let mut visited = BitSet::new_empty(mir.basic_blocks().len());
|
2016-05-29 22:01:06 +03:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
// Codegen the body of each block using reverse postorder
|
2016-03-11 13:14:51 +13:00
|
|
|
for (bb, _) in rpo {
|
2016-03-11 18:00:52 +13:00
|
|
|
visited.insert(bb.index());
|
2018-05-08 16:10:16 +03:00
|
|
|
fx.codegen_block(bb);
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
2016-03-06 16:30:21 +02:00
|
|
|
|
2016-03-11 19:42:47 +13:00
|
|
|
// Remove blocks that haven't been visited, or have no
|
|
|
|
// predecessors.
|
2016-06-07 21:20:50 +03:00
|
|
|
for bb in mir.basic_blocks().indices() {
|
2016-03-11 19:42:47 +13:00
|
|
|
// Unreachable block
|
2016-03-11 18:00:52 +13:00
|
|
|
if !visited.contains(bb.index()) {
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_mir: block {:?} was not visited", bb);
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.delete_basic_block(fx.blocks[bb]);
|
2016-03-11 18:00:52 +13:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
fn create_funclets<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
|
2018-02-26 18:59:47 -08:00
|
|
|
mir: &'a Mir<'tcx>,
|
2018-09-20 15:47:22 +02:00
|
|
|
bx: &Bx,
|
2017-05-23 23:47:15 +03:00
|
|
|
cleanup_kinds: &IndexVec<mir::BasicBlock, CleanupKind>,
|
2018-09-20 15:47:22 +02:00
|
|
|
block_bxs: &IndexVec<mir::BasicBlock, Bx::BasicBlock>)
|
|
|
|
-> (IndexVec<mir::BasicBlock, Option<Bx::BasicBlock>>,
|
|
|
|
IndexVec<mir::BasicBlock, Option<Funclet<'static, Bx::Value>>>)
|
2017-05-23 23:47:15 +03:00
|
|
|
{
|
2018-01-05 07:12:32 +02:00
|
|
|
block_bxs.iter_enumerated().zip(cleanup_kinds).map(|((bb, &llbb), cleanup_kind)| {
|
2017-05-23 23:47:15 +03:00
|
|
|
match *cleanup_kind {
|
2018-09-20 15:47:22 +02:00
|
|
|
CleanupKind::Funclet if base::wants_msvc_seh(bx.cx().sess()) => {}
|
2018-02-26 18:59:47 -08:00
|
|
|
_ => return (None, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
let cleanup;
|
|
|
|
let ret_llbb;
|
|
|
|
match 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) => {
|
|
|
|
let cs_bx = bx.build_sibling_block(&format!("cs_funclet{:?}", bb));
|
|
|
|
let cp_bx = bx.build_sibling_block(&format!("cp_funclet{:?}", bb));
|
|
|
|
ret_llbb = cs_bx.llbb();
|
|
|
|
|
|
|
|
let cs = cs_bx.catch_switch(None, None, 1);
|
|
|
|
cs_bx.add_handler(cs, cp_bx.llbb());
|
|
|
|
|
|
|
|
// 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.
|
2018-09-06 13:52:15 -07:00
|
|
|
let null = bx.cx().const_null(bx.cx().type_i8p());
|
2018-09-06 11:57:42 -07:00
|
|
|
let sixty_four = bx.cx().const_i32(64);
|
2018-02-26 18:59:47 -08:00
|
|
|
cleanup = cp_bx.catch_pad(cs, &[null, sixty_four, null]);
|
|
|
|
cp_bx.br(llbb);
|
|
|
|
}
|
|
|
|
_ => {
|
2018-01-05 07:12:32 +02:00
|
|
|
let cleanup_bx = bx.build_sibling_block(&format!("funclet_{:?}", bb));
|
2018-02-26 18:59:47 -08:00
|
|
|
ret_llbb = cleanup_bx.llbb();
|
|
|
|
cleanup = cleanup_bx.cleanup_pad(None, &[]);
|
2018-01-05 07:12:32 +02:00
|
|
|
cleanup_bx.br(llbb);
|
2017-05-23 23:47:15 +03:00
|
|
|
}
|
2018-02-26 18:59:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
(Some(ret_llbb), Some(Funclet::new(cleanup)))
|
2017-05-23 23:47:15 +03:00
|
|
|
}).unzip()
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
/// Produce, for each argument, a `Value` pointing at the
|
2017-12-01 14:39:51 +02:00
|
|
|
/// argument's value. As arguments are places, these are always
|
2015-10-21 17:42:25 -04:00
|
|
|
/// indirect.
|
2018-09-20 15:47:22 +02:00
|
|
|
fn arg_local_refs<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
|
|
|
|
bx: &Bx,
|
|
|
|
fx: &FunctionCx<'a, 'tcx, Bx>,
|
|
|
|
scopes: &IndexVec<
|
|
|
|
mir::SourceScope,
|
|
|
|
debuginfo::MirDebugScope<Bx::DIScope>
|
|
|
|
>,
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
memory_locals: &BitSet<mir::Local>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> Vec<LocalRef<'tcx, Bx::Value>> {
|
2018-01-05 07:34:28 +02:00
|
|
|
let mir = fx.mir;
|
2018-01-05 07:12:32 +02:00
|
|
|
let tcx = bx.tcx();
|
2016-03-06 16:30:21 +02:00
|
|
|
let mut idx = 0;
|
2018-01-05 07:34:28 +02:00
|
|
|
let mut llarg_idx = fx.fn_ty.ret.is_indirect() as usize;
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
// Get the argument scope, if it exists and if we need it.
|
2018-05-28 14:16:09 +03:00
|
|
|
let arg_scope = scopes[mir::OUTERMOST_SOURCE_SCOPE];
|
2018-09-20 15:47:22 +02:00
|
|
|
let arg_scope = if bx.cx().sess().opts.debuginfo == DebugInfo::Full {
|
2018-06-27 13:12:47 +03:00
|
|
|
arg_scope.scope_metadata
|
2016-05-31 20:27:36 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-09-26 22:50:03 +02:00
|
|
|
mir.args_iter().enumerate().map(|(arg_index, local)| {
|
2016-09-25 01:38:27 +02:00
|
|
|
let arg_decl = &mir.local_decls[local];
|
2016-03-08 14:24:44 +02:00
|
|
|
|
Avoid unnecessary copies of arguments that are simple bindings
Initially MIR differentiated between arguments and locals, which
introduced a need to add extra copies assigning the argument to a
local, even for simple bindings. This differentiation no longer exists,
but we're still creating those copies, bloating the MIR and LLVM IR we
emit.
Additionally, the current approach means that we create debug info for
both the incoming argument (marking it as an argument), and then
immediately shadow it a local that goes by the same name. This can be
confusing when using e.g. "info args" in gdb, or when e.g. a debugger
with a GUI displays the function arguments separately from the local
variables, especially when the binding is mutable, because the argument
doesn't change, while the local variable does.
2017-10-11 20:49:36 +02:00
|
|
|
let name = if let Some(name) = arg_decl.name {
|
|
|
|
name.as_str().to_string()
|
|
|
|
} else {
|
|
|
|
format!("arg{}", arg_index)
|
|
|
|
};
|
|
|
|
|
2016-09-27 02:03:35 +02:00
|
|
|
if Some(local) == mir.spread_arg {
|
|
|
|
// This argument (e.g. the last argument in the "rust-call" ABI)
|
|
|
|
// is a tuple that was spread at the ABI level and now we have
|
|
|
|
// to reconstruct it into a tuple local variable, from multiple
|
|
|
|
// individual LLVM function arguments.
|
|
|
|
|
2018-01-05 07:34:28 +02:00
|
|
|
let arg_ty = fx.monomorphize(&arg_decl.ty);
|
2016-09-27 02:03:35 +02:00
|
|
|
let tupled_arg_tys = match arg_ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Tuple(ref tys) => tys,
|
2016-09-27 02:03:35 +02:00
|
|
|
_ => bug!("spread argument isn't a tuple?!")
|
|
|
|
};
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2018-08-28 17:50:57 +02:00
|
|
|
let place = PlaceRef::alloca(bx, bx.cx().layout_of(arg_ty), &name);
|
2017-09-20 05:16:06 +03:00
|
|
|
for i in 0..tupled_arg_tys.len() {
|
2018-01-05 07:34:28 +02:00
|
|
|
let arg = &fx.fn_ty.args[idx];
|
2016-09-27 02:03:35 +02:00
|
|
|
idx += 1;
|
2018-01-12 23:32:14 +01:00
|
|
|
if arg.pad.is_some() {
|
|
|
|
llarg_idx += 1;
|
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.store_fn_arg(arg, &mut llarg_idx, place.project_field(bx, i));
|
2016-03-09 14:20:22 +02:00
|
|
|
}
|
2016-10-13 14:55:31 -04:00
|
|
|
|
|
|
|
// Now that we have one alloca that contains the aggregate value,
|
|
|
|
// we can create one debuginfo entry for the argument.
|
2016-12-10 20:32:44 -07:00
|
|
|
arg_scope.map(|scope| {
|
2016-10-13 14:55:31 -04:00
|
|
|
let variable_access = VariableAccess::DirectVariable {
|
2017-12-01 14:39:51 +02:00
|
|
|
alloca: place.llval
|
2016-10-13 14:55:31 -04:00
|
|
|
};
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.declare_local(
|
2018-01-05 07:34:28 +02:00
|
|
|
&fx.debug_context,
|
2016-12-18 23:04:25 -07:00
|
|
|
arg_decl.name.unwrap_or(keywords::Invalid.name()),
|
2018-07-04 16:36:49 +03:00
|
|
|
arg_ty, scope,
|
2016-12-18 23:04:25 -07:00
|
|
|
variable_access,
|
|
|
|
VariableKind::ArgumentVariable(arg_index + 1),
|
|
|
|
DUMMY_SP
|
|
|
|
);
|
2016-12-10 20:32:44 -07:00
|
|
|
});
|
2016-10-13 14:55:31 -04:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
return LocalRef::Place(place);
|
2016-03-08 14:24:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 07:34:28 +02:00
|
|
|
let arg = &fx.fn_ty.args[idx];
|
2016-03-06 16:30:21 +02:00
|
|
|
idx += 1;
|
2017-10-10 20:54:50 +03:00
|
|
|
if arg.pad.is_some() {
|
2017-09-20 05:16:06 +03:00
|
|
|
llarg_idx += 1;
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
2017-09-20 05:16:06 +03:00
|
|
|
|
2018-07-22 19:23:39 +03:00
|
|
|
if arg_scope.is_none() && !memory_locals.contains(local) {
|
2017-09-20 05:16:06 +03:00
|
|
|
// We don't have to cast or keep the argument in the alloca.
|
|
|
|
// FIXME(eddyb): We should figure out how to use llvm.dbg.value instead
|
|
|
|
// of putting everything in allocas just so we can use llvm.dbg.declare.
|
2017-10-10 20:54:50 +03:00
|
|
|
let local = |op| LocalRef::Operand(Some(op));
|
|
|
|
match arg.mode {
|
|
|
|
PassMode::Ignore => {
|
2018-08-28 17:50:57 +02:00
|
|
|
return local(OperandRef::new_zst(bx.cx(), arg.layout));
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
|
|
|
PassMode::Direct(_) => {
|
2018-09-20 15:47:22 +02:00
|
|
|
let llarg = bx.cx().get_param(bx.llfn(), llarg_idx as c_uint);
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.set_value_name(llarg, &name);
|
2017-10-10 20:54:50 +03:00
|
|
|
llarg_idx += 1;
|
|
|
|
return local(
|
2018-01-05 07:12:32 +02:00
|
|
|
OperandRef::from_immediate_or_packed_pair(bx, llarg, arg.layout));
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
|
|
|
PassMode::Pair(..) => {
|
2018-09-20 15:47:22 +02:00
|
|
|
let a = bx.cx().get_param(bx.llfn(), llarg_idx as c_uint);
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.set_value_name(a, &(name.clone() + ".0"));
|
2017-10-10 20:54:50 +03:00
|
|
|
llarg_idx += 1;
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
let b = bx.cx().get_param(bx.llfn(), llarg_idx as c_uint);
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.set_value_name(b, &(name + ".1"));
|
2017-10-10 20:54:50 +03:00
|
|
|
llarg_idx += 1;
|
|
|
|
|
|
|
|
return local(OperandRef {
|
|
|
|
val: OperandValue::Pair(a, b),
|
|
|
|
layout: arg.layout
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {}
|
2017-09-20 05:16:06 +03:00
|
|
|
}
|
2017-10-10 20:54:50 +03:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:32:21 +09:00
|
|
|
let place = if arg.is_sized_indirect() {
|
2017-10-10 20:54:50 +03:00
|
|
|
// Don't copy an indirect argument to an alloca, the caller
|
|
|
|
// already put it in a temporary alloca and gave it up.
|
|
|
|
// FIXME: lifetimes
|
2018-09-20 15:47:22 +02:00
|
|
|
let llarg = bx.cx().get_param(bx.llfn(), llarg_idx as c_uint);
|
2018-01-05 07:12:32 +02:00
|
|
|
bx.set_value_name(llarg, &name);
|
2017-09-20 05:16:06 +03:00
|
|
|
llarg_idx += 1;
|
2017-12-01 19:16:39 +02:00
|
|
|
PlaceRef::new_sized(llarg, arg.layout, arg.layout.align)
|
2018-05-29 00:12:55 +09:00
|
|
|
} else if arg.is_unsized_indirect() {
|
|
|
|
// As the storage for the indirect argument lives during
|
|
|
|
// the whole function call, we just copy the fat pointer.
|
2018-09-20 15:47:22 +02:00
|
|
|
let llarg = bx.cx().get_param(bx.llfn(), llarg_idx as c_uint);
|
2018-05-29 00:12:55 +09:00
|
|
|
llarg_idx += 1;
|
2018-09-20 15:47:22 +02:00
|
|
|
let llextra = bx.cx().get_param(bx.llfn(), llarg_idx as c_uint);
|
2018-05-29 00:12:55 +09:00
|
|
|
llarg_idx += 1;
|
|
|
|
let indirect_operand = OperandValue::Pair(llarg, llextra);
|
|
|
|
|
|
|
|
let tmp = PlaceRef::alloca_unsized_indirect(bx, arg.layout, &name);
|
2018-09-14 17:48:57 +02:00
|
|
|
indirect_operand.store(bx, tmp);
|
2018-05-29 00:12:55 +09:00
|
|
|
tmp
|
2016-03-06 16:30:21 +02:00
|
|
|
} else {
|
2018-01-05 07:12:32 +02:00
|
|
|
let tmp = PlaceRef::alloca(bx, arg.layout, &name);
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.store_fn_arg(arg, &mut llarg_idx, tmp);
|
2017-06-25 12:41:24 +03:00
|
|
|
tmp
|
2016-03-06 16:30:21 +02:00
|
|
|
};
|
2016-12-10 20:32:44 -07:00
|
|
|
arg_scope.map(|scope| {
|
2016-04-16 21:51:26 +03:00
|
|
|
// Is this a regular argument?
|
|
|
|
if arg_index > 0 || mir.upvar_decls.is_empty() {
|
2017-09-14 22:09:09 +02:00
|
|
|
// The Rust ABI passes indirect variables using a pointer and a manual copy, so we
|
|
|
|
// need to insert a deref here, but the C ABI uses a pointer and a copy using the
|
2018-06-29 10:28:51 +02:00
|
|
|
// byval attribute, for which LLVM always does the deref itself,
|
|
|
|
// so we must not add it.
|
|
|
|
let variable_access = VariableAccess::DirectVariable {
|
2017-12-01 14:39:51 +02:00
|
|
|
alloca: place.llval
|
2017-09-14 22:09:09 +02:00
|
|
|
};
|
2017-10-10 20:54:50 +03:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.declare_local(
|
2018-01-05 07:34:28 +02:00
|
|
|
&fx.debug_context,
|
2016-12-18 23:04:25 -07:00
|
|
|
arg_decl.name.unwrap_or(keywords::Invalid.name()),
|
2017-09-20 18:17:23 +03:00
|
|
|
arg.layout.ty,
|
2018-07-04 16:36:49 +03:00
|
|
|
scope,
|
2017-09-14 22:09:09 +02:00
|
|
|
variable_access,
|
2016-12-18 23:04:25 -07:00
|
|
|
VariableKind::ArgumentVariable(arg_index + 1),
|
|
|
|
DUMMY_SP
|
|
|
|
);
|
2016-04-16 21:51:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or is it the closure environment?
|
2017-09-20 18:17:23 +03:00
|
|
|
let (closure_layout, env_ref) = match arg.layout.ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::RawPtr(ty::TypeAndMut { ty, .. }) |
|
2018-08-28 17:50:57 +02:00
|
|
|
ty::Ref(_, ty, _) => (bx.cx().layout_of(ty), true),
|
2017-09-20 18:17:23 +03:00
|
|
|
_ => (arg.layout, false)
|
2016-04-16 21:51:26 +03:00
|
|
|
};
|
2017-07-05 14:57:26 -07:00
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
let (def_id, upvar_substs) = match closure_layout.ty.sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs)),
|
|
|
|
ty::Generator(def_id, substs, _) => (def_id, UpvarSubsts::Generator(substs)),
|
2017-09-20 18:17:23 +03:00
|
|
|
_ => bug!("upvar_decls with non-closure arg0 type `{}`", closure_layout.ty)
|
2016-04-16 21:51:26 +03:00
|
|
|
};
|
2018-05-02 13:14:30 +02:00
|
|
|
let upvar_tys = upvar_substs.upvar_tys(def_id, tcx);
|
2016-04-16 21:51:26 +03:00
|
|
|
|
2018-08-09 16:35:25 -07:00
|
|
|
// Store the pointer to closure data in an alloca for debuginfo
|
|
|
|
// because that's what the llvm.dbg.declare intrinsic expects.
|
|
|
|
|
|
|
|
// FIXME(eddyb) this shouldn't be necessary but SROA seems to
|
|
|
|
// mishandle DW_OP_plus not preceded by DW_OP_deref, i.e. it
|
|
|
|
// doesn't actually strip the offset when splitting the closure
|
|
|
|
// environment into its components so it ends up out of bounds.
|
|
|
|
// (cuviper) It seems to be fine without the alloca on LLVM 6 and later.
|
2018-11-03 15:48:29 +01:00
|
|
|
let env_alloca = !env_ref && llvm_util::get_major_version() < 6;
|
2018-08-09 16:35:25 -07:00
|
|
|
let env_ptr = if env_alloca {
|
|
|
|
let scratch = PlaceRef::alloca(bx,
|
2018-08-28 17:50:57 +02:00
|
|
|
bx.cx().layout_of(tcx.mk_mut_ptr(arg.layout.ty)),
|
2018-08-09 16:35:25 -07:00
|
|
|
"__debuginfo_env_ptr");
|
|
|
|
bx.store(place.llval, scratch.llval, scratch.align);
|
|
|
|
scratch.llval
|
|
|
|
} else {
|
|
|
|
place.llval
|
|
|
|
};
|
|
|
|
|
2016-04-16 21:51:26 +03:00
|
|
|
for (i, (decl, ty)) in mir.upvar_decls.iter().zip(upvar_tys).enumerate() {
|
2017-09-20 18:17:23 +03:00
|
|
|
let byte_offset_of_var_in_env = closure_layout.fields.offset(i).bytes();
|
2016-12-19 21:14:27 -05:00
|
|
|
|
2016-04-16 21:51:26 +03:00
|
|
|
let ops = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
[llvm::LLVMRustDIBuilderCreateOpDeref(),
|
2018-01-19 21:43:53 -08:00
|
|
|
llvm::LLVMRustDIBuilderCreateOpPlusUconst(),
|
2016-04-16 21:51:26 +03:00
|
|
|
byte_offset_of_var_in_env as i64,
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateOpDeref()]
|
2016-04-16 21:51:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// The environment and the capture can each be indirect.
|
2018-08-09 16:35:25 -07:00
|
|
|
|
|
|
|
// FIXME(eddyb) see above why we sometimes have to keep
|
|
|
|
// a pointer in an alloca for debuginfo atm.
|
|
|
|
let mut ops = if env_ref || env_alloca { &ops[..] } else { &ops[1..] };
|
2016-04-16 21:51:26 +03:00
|
|
|
|
2018-08-22 01:35:02 +01:00
|
|
|
let ty = if let (true, &ty::Ref(_, ty, _)) = (decl.by_ref, &ty.sty) {
|
2018-05-02 15:21:05 +02:00
|
|
|
ty
|
2016-04-16 21:51:26 +03:00
|
|
|
} else {
|
|
|
|
ops = &ops[..ops.len() - 1];
|
|
|
|
ty
|
|
|
|
};
|
|
|
|
|
|
|
|
let variable_access = VariableAccess::IndirectVariable {
|
2018-08-09 16:35:25 -07:00
|
|
|
alloca: env_ptr,
|
2016-04-16 21:51:26 +03:00
|
|
|
address_operations: &ops
|
|
|
|
};
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.declare_local(
|
2018-01-05 07:34:28 +02:00
|
|
|
&fx.debug_context,
|
2016-12-18 23:04:25 -07:00
|
|
|
decl.debug_name,
|
|
|
|
ty,
|
2018-07-04 16:36:49 +03:00
|
|
|
scope,
|
2016-12-18 23:04:25 -07:00
|
|
|
variable_access,
|
2018-05-21 18:30:55 +03:00
|
|
|
VariableKind::LocalVariable,
|
2016-12-18 23:04:25 -07:00
|
|
|
DUMMY_SP
|
|
|
|
);
|
2016-04-16 21:51:26 +03:00
|
|
|
}
|
2016-12-10 20:32:44 -07:00
|
|
|
});
|
2018-05-29 00:12:55 +09:00
|
|
|
if arg.is_unsized_indirect() {
|
|
|
|
LocalRef::UnsizedPlace(place)
|
|
|
|
} else {
|
|
|
|
LocalRef::Place(place)
|
|
|
|
}
|
2016-03-06 16:30:21 +02:00
|
|
|
}).collect()
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2015-11-03 06:35:09 -05:00
|
|
|
mod analyze;
|
2015-10-21 17:42:25 -04:00
|
|
|
mod block;
|
2018-09-20 15:47:22 +02:00
|
|
|
pub mod constant;
|
2017-12-01 14:39:51 +02:00
|
|
|
pub mod place;
|
2017-06-25 12:41:24 +03:00
|
|
|
pub mod operand;
|
2016-01-30 19:32:50 +02:00
|
|
|
mod rvalue;
|
2015-10-21 17:42:25 -04:00
|
|
|
mod statement;
|