Skip to content

Commit 04303cf

Browse files
cg_ssa: remove pointee types and pointercast/bitcast-of-ptr
1 parent b654077 commit 04303cf

File tree

16 files changed

+75
-201
lines changed

16 files changed

+75
-201
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc_codegen_ssa::traits::{
2727
BaseTypeMethods,
2828
BuilderMethods,
2929
ConstMethods,
30-
DerivedTypeMethods,
3130
LayoutTypeMethods,
3231
HasCodegen,
3332
OverflowOp,

compiler/rustc_codegen_gcc/src/common.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ use crate::context::CodegenCx;
1616
use crate::type_of::LayoutGccExt;
1717

1818
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
19+
pub fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
20+
self.context.new_cast(None, val, ty)
21+
}
22+
1923
pub fn const_bytes(&self, bytes: &[u8]) -> RValue<'gcc> {
2024
bytes_in_context(self, bytes)
2125
}
@@ -242,10 +246,6 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
242246
const_alloc_to_gcc(self, alloc)
243247
}
244248

245-
fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
246-
self.context.new_cast(None, val, ty)
247-
}
248-
249249
fn const_bitcast(&self, value: RValue<'gcc>, typ: Type<'gcc>) -> RValue<'gcc> {
250250
if value.get_type() == self.bool_type.make_pointer() {
251251
if let Some(pointee) = typ.get_pointee() {

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1212
use rustc_codegen_ssa::mir::place::PlaceRef;
1313
use rustc_codegen_ssa::traits::{ArgAbiMethods, BaseTypeMethods, BuilderMethods, ConstMethods, IntrinsicCallMethods};
1414
#[cfg(feature="master")]
15-
use rustc_codegen_ssa::traits::{DerivedTypeMethods, MiscMethods};
15+
use rustc_codegen_ssa::traits::MiscMethods;
1616
use rustc_codegen_ssa::errors::InvalidMonomorphization;
1717
use rustc_middle::bug;
1818
use rustc_middle::ty::{self, Instance, Ty};

compiler/rustc_codegen_gcc/src/type_.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
5454
self.u128_type
5555
}
5656

57+
pub fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
58+
ty.make_pointer()
59+
}
60+
61+
pub fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
62+
// TODO(antoyo): use address_space, perhaps with TYPE_ADDR_SPACE?
63+
ty.make_pointer()
64+
}
65+
66+
pub fn type_i8p(&self) -> Type<'gcc> {
67+
self.type_ptr_to(self.type_i8())
68+
}
69+
70+
pub fn type_i8p_ext(&self, address_space: AddressSpace) -> Type<'gcc> {
71+
self.type_ptr_to_ext(self.type_i8(), address_space)
72+
}
73+
5774
pub fn type_pointee_for_align(&self, align: Align) -> Type<'gcc> {
5875
// FIXME(eddyb) We could find a better approximation if ity.align < align.
5976
let ity = Integer::approximate_align(self, align);
@@ -149,13 +166,12 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
149166
}
150167
}
151168

152-
fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
153-
ty.make_pointer()
169+
fn type_ptr(&self) -> Type<'gcc> {
170+
self.type_ptr_to(self.type_void())
154171
}
155172

156-
fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
157-
// TODO(antoyo): use address_space, perhaps with TYPE_ADDR_SPACE?
158-
ty.make_pointer()
173+
fn type_ptr_ext(&self, address_space: AddressSpace) -> Type<'gcc> {
174+
self.type_ptr_to_ext(self.type_void(), address_space)
159175
}
160176

161177
fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {

compiler/rustc_codegen_llvm/src/common.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Code that is useful in various codegen modules.
22
3-
use crate::consts::{self, const_alloc_to_llvm};
3+
use crate::consts::const_alloc_to_llvm;
44
pub use crate::context::CodegenCx;
55
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef, True};
66
use crate::type_::Type;
@@ -304,10 +304,6 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
304304
const_alloc_to_llvm(self, alloc)
305305
}
306306

307-
fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
308-
consts::ptrcast(val, ty)
309-
}
310-
311307
fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
312308
self.const_bitcast(val, ty)
313309
}

compiler/rustc_codegen_llvm/src/consts.rs

-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ fn check_and_apply_linkage<'ll, 'tcx>(
193193
}
194194
}
195195

196-
pub fn ptrcast<'ll>(val: &'ll Value, ty: &'ll Type) -> &'ll Value {
197-
unsafe { llvm::LLVMConstPointerCast(val, ty) }
198-
}
199-
200196
impl<'ll> CodegenCx<'ll, '_> {
201197
pub(crate) fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
202198
unsafe { llvm::LLVMConstBitCast(val, ty) }

compiler/rustc_codegen_llvm/src/type_.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ impl<'ll> CodegenCx<'ll, '_> {
6969
unsafe { llvm::LLVMVectorType(ty, len as c_uint) }
7070
}
7171

72-
pub(crate) fn type_ptr(&self) -> &'ll Type {
73-
self.type_ptr_ext(AddressSpace::DATA)
74-
}
75-
76-
pub(crate) fn type_ptr_ext(&self, address_space: AddressSpace) -> &'ll Type {
77-
unsafe { llvm::LLVMPointerTypeInContext(self.llcx, address_space.0) }
78-
}
79-
8072
pub(crate) fn func_params_types(&self, ty: &'ll Type) -> Vec<&'ll Type> {
8173
unsafe {
8274
let n_args = llvm::LLVMCountParamTypes(ty) as usize;
@@ -191,12 +183,12 @@ impl<'ll, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
191183
unsafe { llvm::LLVMRustGetTypeKind(ty).to_generic() }
192184
}
193185

194-
fn type_ptr_to(&self, _ty: &'ll Type) -> &'ll Type {
195-
self.type_ptr()
186+
fn type_ptr(&self) -> &'ll Type {
187+
self.type_ptr_ext(AddressSpace::DATA)
196188
}
197189

198-
fn type_ptr_to_ext(&self, _ty: &'ll Type, address_space: AddressSpace) -> &'ll Type {
199-
self.type_ptr_ext(address_space)
190+
fn type_ptr_ext(&self, address_space: AddressSpace) -> &'ll Type {
191+
unsafe { llvm::LLVMPointerTypeInContext(self.llcx, address_space.0) }
200192
}
201193

202194
fn element_type(&self, ty: &'ll Type) -> &'ll Type {

compiler/rustc_codegen_ssa/src/base.rs

+13-49
Original file line numberDiff line numberDiff line change
@@ -165,50 +165,27 @@ pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
165165
cx.tcx().vtable_trait_upcasting_coercion_new_vptr_slot((source, target));
166166

167167
if let Some(entry_idx) = vptr_entry_idx {
168-
let ptr_ty = cx.type_i8p();
168+
let ptr_ty = cx.type_ptr();
169169
let ptr_align = cx.tcx().data_layout.pointer_align.abi;
170-
let vtable_ptr_ty = vtable_ptr_ty(cx, target, target_dyn_kind);
171-
let llvtable = bx.pointercast(old_info, bx.type_ptr_to(ptr_ty));
172170
let gep = bx.inbounds_gep(
173171
ptr_ty,
174-
llvtable,
172+
old_info,
175173
&[bx.const_usize(u64::try_from(entry_idx).unwrap())],
176174
);
177175
let new_vptr = bx.load(ptr_ty, gep, ptr_align);
178176
bx.nonnull_metadata(new_vptr);
179177
// VTable loads are invariant.
180178
bx.set_invariant_load(new_vptr);
181-
bx.pointercast(new_vptr, vtable_ptr_ty)
179+
new_vptr
182180
} else {
183181
old_info
184182
}
185183
}
186-
(_, &ty::Dynamic(ref data, _, target_dyn_kind)) => {
187-
let vtable_ptr_ty = vtable_ptr_ty(cx, target, target_dyn_kind);
188-
cx.const_ptrcast(meth::get_vtable(cx, source, data.principal()), vtable_ptr_ty)
189-
}
184+
(_, &ty::Dynamic(ref data, _, _)) => meth::get_vtable(cx, source, data.principal()),
190185
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}", source, target),
191186
}
192187
}
193188

194-
// Returns the vtable pointer type of a `dyn` or `dyn*` type
195-
fn vtable_ptr_ty<'tcx, Cx: CodegenMethods<'tcx>>(
196-
cx: &Cx,
197-
target: Ty<'tcx>,
198-
kind: ty::DynKind,
199-
) -> <Cx as BackendTypes>::Type {
200-
cx.scalar_pair_element_backend_type(
201-
cx.layout_of(match kind {
202-
// vtable is the second field of `*mut dyn Trait`
203-
ty::Dyn => Ty::new_mut_ptr(cx.tcx(), target),
204-
// vtable is the second field of `dyn* Trait`
205-
ty::DynStar => target,
206-
}),
207-
1,
208-
true,
209-
)
210-
}
211-
212189
/// Coerces `src` to `dst_ty`. `src_ty` must be a pointer.
213190
pub fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
214191
bx: &mut Bx,
@@ -222,8 +199,7 @@ pub fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
222199
(&ty::Ref(_, a, _), &ty::Ref(_, b, _) | &ty::RawPtr(ty::TypeAndMut { ty: b, .. }))
223200
| (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }), &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
224201
assert_eq!(bx.cx().type_is_sized(a), old_info.is_none());
225-
let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(bx.cx().layout_of(b)));
226-
(bx.pointercast(src, ptr_ty), unsized_info(bx, a, b, old_info))
202+
(src, unsized_info(bx, a, b, old_info))
227203
}
228204
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
229205
assert_eq!(def_a, def_b);
@@ -248,11 +224,7 @@ pub fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
248224
assert_eq!(result, None);
249225
result = Some(unsize_ptr(bx, src, src_f.ty, dst_f.ty, old_info));
250226
}
251-
let (lldata, llextra) = result.unwrap();
252-
let lldata_ty = bx.cx().scalar_pair_element_backend_type(dst_layout, 0, true);
253-
let llextra_ty = bx.cx().scalar_pair_element_backend_type(dst_layout, 1, true);
254-
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
255-
(bx.bitcast(lldata, lldata_ty), bx.bitcast(llextra, llextra_ty))
227+
result.unwrap()
256228
}
257229
_ => bug!("unsize_ptr: called on bad types"),
258230
}
@@ -271,11 +243,9 @@ pub fn cast_to_dyn_star<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
271243
matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
272244
"destination type must be a dyn*"
273245
);
274-
// FIXME(dyn-star): We can remove this when all supported LLVMs use opaque ptrs only.
275-
let unit_ptr = bx.cx().type_ptr_to(bx.cx().type_struct(&[], false));
276246
let src = match bx.cx().type_kind(bx.cx().backend_type(src_ty_and_layout)) {
277-
TypeKind::Pointer => bx.pointercast(src, unit_ptr),
278-
TypeKind::Integer => bx.inttoptr(src, unit_ptr),
247+
TypeKind::Pointer => src,
248+
TypeKind::Integer => bx.inttoptr(src, bx.type_ptr()),
279249
// FIXME(dyn-star): We probably have to do a bitcast first, then inttoptr.
280250
kind => bug!("unexpected TypeKind for left-hand side of `dyn*` cast: {kind:?}"),
281251
};
@@ -398,11 +368,6 @@ pub fn memcpy_ty<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
398368
if flags == MemFlags::empty()
399369
&& let Some(bty) = bx.cx().scalar_copy_backend_type(layout)
400370
{
401-
// I look forward to only supporting opaque pointers
402-
let pty = bx.type_ptr_to(bty);
403-
let src = bx.pointercast(src, pty);
404-
let dst = bx.pointercast(dst, pty);
405-
406371
let temp = bx.load(bty, src, src_align);
407372
bx.store(temp, dst, dst_align);
408373
} else {
@@ -456,7 +421,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
456421
// The entry function is either `int main(void)` or `int main(int argc, char **argv)`,
457422
// depending on whether the target needs `argc` and `argv` to be passed in.
458423
let llfty = if cx.sess().target.main_needs_argc_argv {
459-
cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int())
424+
cx.type_func(&[cx.type_int(), cx.type_ptr()], cx.type_int())
460425
} else {
461426
cx.type_func(&[], cx.type_int())
462427
};
@@ -490,7 +455,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
490455
bx.insert_reference_to_gdb_debug_scripts_section_global();
491456

492457
let isize_ty = cx.type_isize();
493-
let i8pp_ty = cx.type_ptr_to(cx.type_i8p());
458+
let ptr_ty = cx.type_ptr();
494459
let (arg_argc, arg_argv) = get_argc_argv(cx, &mut bx);
495460

496461
let (start_fn, start_ty, args) = if let EntryFnType::Main { sigpipe } = entry_type {
@@ -509,12 +474,11 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
509474
let i8_ty = cx.type_i8();
510475
let arg_sigpipe = bx.const_u8(sigpipe);
511476

512-
let start_ty =
513-
cx.type_func(&[cx.val_ty(rust_main), isize_ty, i8pp_ty, i8_ty], isize_ty);
477+
let start_ty = cx.type_func(&[cx.val_ty(rust_main), isize_ty, ptr_ty, i8_ty], isize_ty);
514478
(start_fn, start_ty, vec![rust_main, arg_argc, arg_argv, arg_sigpipe])
515479
} else {
516480
debug!("using user-defined start fn");
517-
let start_ty = cx.type_func(&[isize_ty, i8pp_ty], isize_ty);
481+
let start_ty = cx.type_func(&[isize_ty, ptr_ty], isize_ty);
518482
(rust_main, start_ty, vec![arg_argc, arg_argv])
519483
};
520484

@@ -541,7 +505,7 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
541505
} else {
542506
// The Rust start function doesn't need `argc` and `argv`, so just pass zeros.
543507
let arg_argc = bx.const_int(cx.type_int(), 0);
544-
let arg_argv = bx.const_null(cx.type_ptr_to(cx.type_i8p()));
508+
let arg_argv = bx.const_null(cx.type_ptr());
545509
(arg_argc, arg_argv)
546510
}
547511
}

compiler/rustc_codegen_ssa/src/meth.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ impl<'a, 'tcx> VirtualIndex {
2323
// Load the data pointer from the object.
2424
debug!("get_fn({llvtable:?}, {ty:?}, {self:?})");
2525
let llty = bx.fn_ptr_backend_type(fn_abi);
26-
let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
2726

2827
if bx.cx().sess().opts.unstable_opts.virtual_function_elimination
2928
&& bx.cx().sess().lto() == Lto::Fat
@@ -33,7 +32,7 @@ impl<'a, 'tcx> VirtualIndex {
3332
.unwrap();
3433
let vtable_byte_offset = self.0 * bx.data_layout().pointer_size.bytes();
3534
let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
36-
bx.pointercast(func, llty)
35+
func
3736
} else {
3837
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
3938
let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
@@ -54,7 +53,6 @@ impl<'a, 'tcx> VirtualIndex {
5453
debug!("get_int({:?}, {:?})", llvtable, self);
5554

5655
let llty = bx.type_isize();
57-
let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
5856
let usize_align = bx.tcx().data_layout.pointer_align.abi;
5957
let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
6058
let ptr = bx.load(llty, gep, usize_align);

compiler/rustc_codegen_ssa/src/mir/block.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
439439
ZeroSized => bug!("ZST return value shouldn't be in PassMode::Cast"),
440440
};
441441
let ty = bx.cast_backend_type(cast_ty);
442-
let addr = bx.pointercast(llslot, bx.type_ptr_to(ty));
443-
bx.load(ty, addr, self.fn_abi.ret.layout.align.abi)
442+
bx.load(ty, llslot, self.fn_abi.ret.layout.align.abi)
444443
}
445444
};
446445
bx.ret(llval);
@@ -853,9 +852,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
853852
Some(intrinsic) => {
854853
let dest = match ret_dest {
855854
_ if fn_abi.ret.is_indirect() => llargs[0],
856-
ReturnDest::Nothing => {
857-
bx.const_undef(bx.type_ptr_to(bx.arg_memory_ty(&fn_abi.ret)))
858-
}
855+
ReturnDest::Nothing => bx.const_undef(bx.type_ptr()),
859856
ReturnDest::IndirectOperand(dst, _) | ReturnDest::Store(dst) => dst.llval,
860857
ReturnDest::DirectOperand(_) => {
861858
bug!("Cannot use direct operand with an intrinsic call")
@@ -1428,8 +1425,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14281425
// Have to load the argument, maybe while casting it.
14291426
if let PassMode::Cast(ty, _) = &arg.mode {
14301427
let llty = bx.cast_backend_type(ty);
1431-
let addr = bx.pointercast(llval, bx.type_ptr_to(llty));
1432-
llval = bx.load(llty, addr, align.min(arg.layout.align.abi));
1428+
llval = bx.load(llty, llval, align.min(arg.layout.align.abi));
14331429
} else {
14341430
// We can't use `PlaceRef::load` here because the argument
14351431
// may have a type we don't treat as immediate, but the ABI
@@ -1634,7 +1630,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16341630
// represents that this is a catch-all block.
16351631
bx = Bx::build(self.cx, cp_llbb);
16361632
let null =
1637-
bx.const_null(bx.type_i8p_ext(bx.cx().data_layout().instruction_address_space));
1633+
bx.const_null(bx.type_ptr_ext(bx.cx().data_layout().instruction_address_space));
16381634
let sixty_four = bx.const_i32(64);
16391635
funclet = Some(bx.catch_pad(cs, &[null, sixty_four, null]));
16401636
} else {

0 commit comments

Comments
 (0)