Skip to content

Commit 3fd6b00

Browse files
committed
rustc_trans: query LLVM types from a layout instead of a Ty.
1 parent 1477119 commit 3fd6b00

File tree

16 files changed

+114
-116
lines changed

16 files changed

+114
-116
lines changed

src/librustc_trans/abi.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use cabi_nvptx64;
3232
use cabi_hexagon;
3333
use mir::lvalue::LvalueRef;
3434
use type_::Type;
35+
use type_of::LayoutLlvmExt;
3536

3637
use rustc::hir;
3738
use rustc::ty::{self, Ty};
@@ -506,7 +507,7 @@ impl<'a, 'tcx> ArgType<'tcx> {
506507
/// Get the LLVM type for an lvalue of the original Rust type of
507508
/// this argument/return, i.e. the result of `type_of::type_of`.
508509
pub fn memory_ty(&self, ccx: &CrateContext<'a, 'tcx>) -> Type {
509-
ccx.llvm_type_of(self.layout.ty)
510+
self.layout.llvm_type(ccx)
510511
}
511512

512513
/// Store a direct/indirect value described by this ArgType into a
@@ -934,7 +935,7 @@ impl<'a, 'tcx> FnType<'tcx> {
934935
} else if let Some(cast) = self.ret.cast {
935936
cast.llvm_type(ccx)
936937
} else {
937-
ccx.immediate_llvm_type_of(self.ret.layout.ty)
938+
self.ret.layout.immediate_llvm_type(ccx)
938939
};
939940

940941
{
@@ -952,7 +953,7 @@ impl<'a, 'tcx> FnType<'tcx> {
952953
} else if let Some(cast) = arg.cast {
953954
cast.llvm_type(ccx)
954955
} else {
955-
ccx.immediate_llvm_type_of(arg.layout.ty)
956+
arg.layout.immediate_llvm_type(ccx)
956957
};
957958

958959
llargument_tys.push(llarg_ty);

src/librustc_trans/asm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use llvm::{self, ValueRef};
1414
use common::*;
1515
use type_::Type;
16+
use type_of::LayoutLlvmExt;
1617
use builder::Builder;
1718

1819
use rustc::hir;
@@ -44,7 +45,7 @@ pub fn trans_inline_asm<'a, 'tcx>(
4445
if out.is_indirect {
4546
indirect_outputs.push(lvalue.load(bcx).immediate());
4647
} else {
47-
output_types.push(bcx.ccx.llvm_type_of(lvalue.layout.ty));
48+
output_types.push(lvalue.layout.llvm_type(bcx.ccx));
4849
}
4950
}
5051
if !indirect_outputs.is_empty() {

src/librustc_trans/base.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc::middle::lang_items::StartFnLangItem;
4040
use rustc::middle::trans::{Linkage, Visibility, Stats};
4141
use rustc::middle::cstore::{EncodedMetadata, EncodedMetadataHashes};
4242
use rustc::ty::{self, Ty, TyCtxt};
43-
use rustc::ty::layout::{Align, FullLayout};
43+
use rustc::ty::layout::{self, Align, FullLayout, LayoutOf};
4444
use rustc::ty::maps::Providers;
4545
use rustc::dep_graph::{DepNode, DepKind, DepConstructor};
4646
use rustc::middle::cstore::{self, LinkMeta, LinkagePreference};
@@ -68,7 +68,7 @@ use symbol_names_test;
6868
use time_graph;
6969
use trans_item::{TransItem, BaseTransItemExt, TransItemExt, DefPathBasedNames};
7070
use type_::Type;
71-
use type_of;
71+
use type_of::{self, LayoutLlvmExt};
7272
use rustc::util::nodemap::{NodeSet, FxHashMap, FxHashSet, DefIdSet};
7373
use CrateInfo;
7474

@@ -228,13 +228,13 @@ pub fn unsize_thin_ptr<'a, 'tcx>(
228228
(&ty::TyRawPtr(ty::TypeAndMut { ty: a, .. }),
229229
&ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) => {
230230
assert!(bcx.ccx.shared().type_is_sized(a));
231-
let ptr_ty = bcx.ccx.llvm_type_of(b).ptr_to();
231+
let ptr_ty = bcx.ccx.layout_of(b).llvm_type(bcx.ccx).ptr_to();
232232
(bcx.pointercast(src, ptr_ty), unsized_info(bcx.ccx, a, b, None))
233233
}
234234
(&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
235235
let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
236236
assert!(bcx.ccx.shared().type_is_sized(a));
237-
let ptr_ty = bcx.ccx.llvm_type_of(b).ptr_to();
237+
let ptr_ty = bcx.ccx.layout_of(b).llvm_type(bcx.ccx).ptr_to();
238238
(bcx.pointercast(src, ptr_ty), unsized_info(bcx.ccx, a, b, None))
239239
}
240240
_ => bug!("unsize_thin_ptr: called on bad types"),
@@ -371,8 +371,8 @@ pub fn from_immediate(bcx: &Builder, val: ValueRef) -> ValueRef {
371371
}
372372
}
373373

374-
pub fn to_immediate(bcx: &Builder, val: ValueRef, ty: Ty) -> ValueRef {
375-
if ty.is_bool() {
374+
pub fn to_immediate(bcx: &Builder, val: ValueRef, layout: layout::FullLayout) -> ValueRef {
375+
if let layout::Abi::Scalar(layout::Int(layout::I1, _)) = layout.abi {
376376
bcx.trunc(val, Type::i1(bcx.ccx))
377377
} else {
378378
val

src/librustc_trans/callee.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ use consts;
2020
use declare;
2121
use llvm::{self, ValueRef};
2222
use monomorphize::Instance;
23+
use type_of::LayoutLlvmExt;
24+
2325
use rustc::hir::def_id::DefId;
2426
use rustc::ty::{self, TypeFoldable};
27+
use rustc::ty::layout::LayoutOf;
2528
use rustc::traits;
2629
use rustc::ty::subst::Substs;
2730
use rustc_back::PanicStrategy;
@@ -55,7 +58,7 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
5558

5659
// Create a fn pointer with the substituted signature.
5760
let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig(ccx, fn_ty));
58-
let llptrty = ccx.llvm_type_of(fn_ptr_ty);
61+
let llptrty = ccx.layout_of(fn_ptr_ty).llvm_type(ccx);
5962

6063
let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
6164
// This is subtle and surprising, but sometimes we have to bitcast

src/librustc_trans/common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use builder::Builder;
2424
use consts;
2525
use declare;
2626
use type_::Type;
27+
use type_of::LayoutLlvmExt;
2728
use value::Value;
2829
use rustc::traits;
2930
use rustc::ty::{self, Ty, TyCtxt};
@@ -254,7 +255,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> Va
254255
pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
255256
let len = s.len();
256257
let cs = consts::ptrcast(C_cstr(cx, s, false),
257-
cx.llvm_type_of(cx.tcx().mk_str()).ptr_to());
258+
cx.layout_of(cx.tcx().mk_str()).llvm_type(cx).ptr_to());
258259
let empty = C_array(Type::i8(cx), &[]);
259260
assert_eq!(abi::FAT_PTR_ADDR, 0);
260261
assert_eq!(abi::FAT_PTR_EXTRA, 1);

src/librustc_trans/consts.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use common::{self, CrateContext, val_ty};
2121
use declare;
2222
use monomorphize::Instance;
2323
use type_::Type;
24+
use type_of::LayoutLlvmExt;
2425
use rustc::ty;
25-
use rustc::ty::layout::Align;
26+
use rustc::ty::layout::{Align, LayoutOf};
2627

2728
use rustc::hir;
2829

@@ -112,7 +113,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
112113
let ty = common::instance_ty(ccx.tcx(), &instance);
113114
let g = if let Some(id) = ccx.tcx().hir.as_local_node_id(def_id) {
114115

115-
let llty = ccx.llvm_type_of(ty);
116+
let llty = ccx.layout_of(ty).llvm_type(ccx);
116117
let (g, attrs) = match ccx.tcx().hir.get(id) {
117118
hir_map::NodeItem(&hir::Item {
118119
ref attrs, span, node: hir::ItemStatic(..), ..
@@ -157,7 +158,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
157158
}
158159
};
159160
let llty2 = match ty.sty {
160-
ty::TyRawPtr(ref mt) => ccx.llvm_type_of(mt.ty),
161+
ty::TyRawPtr(ref mt) => ccx.layout_of(mt.ty).llvm_type(ccx),
161162
_ => {
162163
ccx.sess().span_fatal(span, "must have type `*const T` or `*mut T`");
163164
}
@@ -206,7 +207,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
206207

207208
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
208209
// FIXME(nagisa): investigate whether it can be changed into define_global
209-
let g = declare::declare_global(ccx, &sym, ccx.llvm_type_of(ty));
210+
let g = declare::declare_global(ccx, &sym, ccx.layout_of(ty).llvm_type(ccx));
210211
// Thread-local statics in some other crate need to *always* be linked
211212
// against in a thread-local fashion, so we need to be sure to apply the
212213
// thread-local attribute locally if it was present remotely. If we
@@ -266,7 +267,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
266267

267268
let instance = Instance::mono(ccx.tcx(), def_id);
268269
let ty = common::instance_ty(ccx.tcx(), &instance);
269-
let llty = ccx.llvm_type_of(ty);
270+
let llty = ccx.layout_of(ty).llvm_type(ccx);
270271
let g = if val_llty == llty {
271272
g
272273
} else {

src/librustc_trans/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use monomorphize::Instance;
2424

2525
use partitioning::CodegenUnit;
2626
use type_::Type;
27+
use type_of::LayoutLlvmExt;
28+
2729
use rustc_data_structures::base_n;
2830
use rustc::middle::trans::Stats;
2931
use rustc_data_structures::stable_hasher::StableHashingContextProvider;
@@ -397,7 +399,7 @@ impl<'a, 'tcx> LocalCrateContext<'a, 'tcx> {
397399
let mut str_slice_ty = Type::named_struct(&dummy_ccx, "str_slice");
398400
str_slice_ty.set_struct_body(&[
399401
Type::array(&Type::i8(&dummy_ccx), 0),
400-
dummy_ccx.llvm_type_of(shared.tcx.mk_str()).ptr_to(),
402+
dummy_ccx.layout_of(shared.tcx.mk_str()).llvm_type(&dummy_ccx).ptr_to(),
401403
Type::array(&Type::i8(&dummy_ccx), 0),
402404
Type::isize(&dummy_ccx),
403405
Type::array(&Type::i8(&dummy_ccx), 0)

src/librustc_trans/intrinsic.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use common::*;
2121
use declare;
2222
use glue;
2323
use type_::Type;
24+
use type_of::LayoutLlvmExt;
2425
use rustc::ty::{self, Ty};
2526
use rustc::ty::layout::{HasDataLayout, LayoutOf};
2627
use rustc::hir;
@@ -104,7 +105,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
104105
let ret_ty = sig.output();
105106
let name = &*tcx.item_name(def_id);
106107

107-
let llret_ty = ccx.llvm_type_of(ret_ty);
108+
let llret_ty = ccx.layout_of(ret_ty).llvm_type(ccx);
108109
let result = LvalueRef::new_sized(llresult, fn_ty.ret.layout, Alignment::AbiAligned);
109110

110111
let simple = get_simple_intrinsic(ccx, name);
@@ -243,7 +244,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
243244
unsafe {
244245
llvm::LLVMSetAlignment(load, ccx.align_of(tp_ty).abi() as u32);
245246
}
246-
to_immediate(bcx, load, tp_ty)
247+
to_immediate(bcx, load, ccx.layout_of(tp_ty))
247248
},
248249
"volatile_store" => {
249250
let tp_ty = substs.type_at(0);

src/librustc_trans/mir/block.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
662662
if arg.layout.ty == bcx.tcx().types.bool {
663663
llval = bcx.load_range_assert(llval, 0, 2, llvm::False, None);
664664
// We store bools as i8 so we need to truncate to i1.
665-
llval = base::to_immediate(bcx, llval, arg.layout.ty);
665+
llval = base::to_immediate(bcx, llval, arg.layout);
666666
} else if let Some(ty) = arg.cast {
667667
llval = bcx.load(bcx.pointercast(llval, ty.llvm_type(bcx.ccx).ptr_to()),
668668
(align | Alignment::Packed(arg.layout.align(bcx.ccx)))
@@ -682,45 +682,37 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
682682
args: &[ArgType<'tcx>]) {
683683
let tuple = self.trans_operand(bcx, operand);
684684

685-
let arg_types = match tuple.layout.ty.sty {
686-
ty::TyTuple(ref tys, _) => tys,
687-
_ => span_bug!(self.mir.span,
688-
"bad final argument to \"rust-call\" fn {:?}", tuple.layout.ty)
689-
};
690-
691685
// Handle both by-ref and immediate tuples.
692686
match tuple.val {
693687
Ref(llval, align) => {
694688
let tuple_ptr = LvalueRef::new_sized(llval, tuple.layout, align);
695-
for n in 0..arg_types.len() {
696-
let field_ptr = tuple_ptr.project_field(bcx, n);
697-
self.trans_argument(bcx, field_ptr.load(bcx), llargs, &args[n]);
689+
for i in 0..tuple.layout.fields.count() {
690+
let field_ptr = tuple_ptr.project_field(bcx, i);
691+
self.trans_argument(bcx, field_ptr.load(bcx), llargs, &args[i]);
698692
}
699693

700694
}
701695
Immediate(llval) => {
702-
for (n, &ty) in arg_types.iter().enumerate() {
703-
let mut elem = bcx.extract_value(llval, tuple.layout.llvm_field_index(n));
704-
// Truncate bools to i1, if needed
705-
elem = base::to_immediate(bcx, elem, ty);
696+
for i in 0..tuple.layout.fields.count() {
697+
let field = tuple.layout.field(bcx.ccx, i);
698+
let elem = bcx.extract_value(llval, tuple.layout.llvm_field_index(i));
706699
// If the tuple is immediate, the elements are as well
707700
let op = OperandRef {
708-
val: Immediate(elem),
709-
layout: bcx.ccx.layout_of(ty),
701+
val: Immediate(base::to_immediate(bcx, elem, field)),
702+
layout: field,
710703
};
711-
self.trans_argument(bcx, op, llargs, &args[n]);
704+
self.trans_argument(bcx, op, llargs, &args[i]);
712705
}
713706
}
714707
Pair(a, b) => {
715708
let elems = [a, b];
716-
for (n, &ty) in arg_types.iter().enumerate() {
717-
let elem = base::to_immediate(bcx, elems[n], ty);
709+
for i in 0..tuple.layout.fields.count() {
718710
// Pair is always made up of immediates
719711
let op = OperandRef {
720-
val: Immediate(elem),
721-
layout: bcx.ccx.layout_of(ty),
712+
val: Immediate(elems[i]),
713+
layout: tuple.layout.field(bcx.ccx, i),
722714
};
723-
self.trans_argument(bcx, op, llargs, &args[n]);
715+
self.trans_argument(bcx, op, llargs, &args[i]);
724716
}
725717
}
726718
}
@@ -891,7 +883,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
891883
src: &mir::Operand<'tcx>,
892884
dst: LvalueRef<'tcx>) {
893885
let src = self.trans_operand(bcx, src);
894-
let llty = bcx.ccx.llvm_type_of(src.layout.ty);
886+
let llty = src.layout.llvm_type(bcx.ccx);
895887
let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to());
896888
let align = src.layout.align(bcx.ccx).min(dst.layout.align(bcx.ccx));
897889
src.val.store(bcx,

src/librustc_trans/mir/constant.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a, 'tcx> Const<'tcx> {
8787
cv: &ConstVal,
8888
ty: Ty<'tcx>)
8989
-> Const<'tcx> {
90-
let llty = ccx.llvm_type_of(ty);
90+
let llty = ccx.layout_of(ty).llvm_type(ccx);
9191
let val = match *cv {
9292
ConstVal::Float(v) => {
9393
let bits = match v.ty {
@@ -139,7 +139,7 @@ impl<'a, 'tcx> Const<'tcx> {
139139
}
140140

141141
pub fn to_operand(&self, ccx: &CrateContext<'a, 'tcx>) -> OperandRef<'tcx> {
142-
let llty = ccx.immediate_llvm_type_of(self.ty);
142+
let llty = ccx.layout_of(self.ty).immediate_llvm_type(ccx);
143143
let llvalty = val_ty(self.llval);
144144

145145
let val = if llty == llvalty && common::type_is_imm_pair(ccx, self.ty) {
@@ -489,7 +489,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
489489
let llelem = if iv < len as u128 {
490490
const_get_elt(base.llval, iv as u64)
491491
} else {
492-
C_undef(self.ccx.llvm_type_of(projected_ty))
492+
C_undef(self.ccx.layout_of(projected_ty).llvm_type(self.ccx))
493493
};
494494

495495
(Base::Value(llelem), ptr::null_mut())
@@ -543,7 +543,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
543543
let elem_ty = array_ty.builtin_index().unwrap_or_else(|| {
544544
bug!("bad array type {:?}", array_ty)
545545
});
546-
let llunitty = self.ccx.llvm_type_of(elem_ty);
546+
let llunitty = self.ccx.layout_of(elem_ty).llvm_type(self.ccx);
547547
// If the array contains enums, an LLVM array won't work.
548548
let val = if fields.iter().all(|&f| val_ty(f) == llunitty) {
549549
C_array(llunitty, fields)
@@ -665,7 +665,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
665665

666666
let unsized_ty = cast_ty.builtin_deref(true, ty::NoPreference)
667667
.expect("consts: unsizing got non-pointer target type").ty;
668-
let ptr_ty = self.ccx.llvm_type_of(unsized_ty).ptr_to();
668+
let ptr_ty = self.ccx.layout_of(unsized_ty).llvm_type(self.ccx).ptr_to();
669669
let base = consts::ptrcast(base, ptr_ty);
670670
let info = base::unsized_info(self.ccx, pointee_ty,
671671
unsized_ty, old_info);
@@ -681,7 +681,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
681681
debug_assert!(common::type_is_immediate(self.ccx, cast_ty));
682682
let r_t_in = CastTy::from_ty(operand.ty).expect("bad input type for cast");
683683
let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
684-
let ll_t_out = self.ccx.immediate_llvm_type_of(cast_ty);
684+
let ll_t_out = self.ccx.layout_of(cast_ty).immediate_llvm_type(self.ccx);
685685
let llval = operand.llval;
686686
let signed = match self.ccx.layout_of(operand.ty).abi {
687687
layout::Abi::Scalar(layout::Int(_, signed)) => signed,
@@ -734,7 +734,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
734734
} else { // cast to thin-ptr
735735
// Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
736736
// pointer-cast of that pointer to desired pointer type.
737-
let llcast_ty = self.ccx.immediate_llvm_type_of(cast_ty);
737+
let llcast_ty = self.ccx.layout_of(cast_ty)
738+
.immediate_llvm_type(self.ccx);
738739
consts::ptrcast(data_ptr, llcast_ty)
739740
}
740741
} else {
@@ -1041,7 +1042,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
10411042

10421043
let result = result.unwrap_or_else(|_| {
10431044
// We've errored, so we don't have to produce working code.
1044-
let llty = bcx.ccx.llvm_type_of(ty);
1045+
let llty = bcx.ccx.layout_of(ty).llvm_type(bcx.ccx);
10451046
Const::new(C_undef(llty), ty)
10461047
});
10471048

@@ -1100,7 +1101,7 @@ fn trans_const_adt<'a, 'tcx>(
11001101
_ => 0,
11011102
};
11021103
let discr_ty = l.field(ccx, 0).ty;
1103-
let discr = C_int(ccx.llvm_type_of(discr_ty), discr as i64);
1104+
let discr = C_int(ccx.layout_of(discr_ty).llvm_type(ccx), discr as i64);
11041105
if let layout::Abi::Scalar(_) = l.abi {
11051106
Const::new(discr, t)
11061107
} else {
@@ -1130,7 +1131,7 @@ fn trans_const_adt<'a, 'tcx>(
11301131
} else {
11311132
// Always use null even if it's not the `discrfield`th
11321133
// field; see #8506.
1133-
Const::new(C_null(ccx.llvm_type_of(t)), t)
1134+
Const::new(C_null(ccx.layout_of(t).llvm_type(ccx)), t)
11341135
}
11351136
}
11361137
_ => bug!("trans_const_adt: cannot handle type {} repreented as {:#?}", t, l)

0 commit comments

Comments
 (0)