Skip to content

Commit b654077

Browse files
cg_llvm: remove pointee types and pointercast/bitcast-of-ptr
1 parent 4c96822 commit b654077

File tree

17 files changed

+118
-283
lines changed

17 files changed

+118
-283
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
216216
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
217217
let can_store_through_cast_ptr = false;
218218
if can_store_through_cast_ptr {
219-
let cast_ptr_llty = bx.type_ptr_to(cast.llvm_type(bx));
220-
let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
221-
bx.store(val, cast_dst, self.layout.align.abi);
219+
bx.store(val, dst.llval, self.layout.align.abi);
222220
} else {
223221
// The actual return type is a struct, but the ABI
224222
// adaptation code has cast it into some scalar type. The
@@ -336,7 +334,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
336334
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
337335
PassMode::Cast(cast, _) => cast.llvm_type(cx),
338336
PassMode::Indirect { .. } => {
339-
llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
337+
llargument_tys.push(cx.type_ptr());
340338
cx.type_void()
341339
}
342340
};
@@ -364,9 +362,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
364362
}
365363
cast.llvm_type(cx)
366364
}
367-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
368-
cx.type_ptr_to(arg.memory_ty(cx))
369-
}
365+
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => cx.type_ptr(),
370366
};
371367
llargument_tys.push(llarg_ty);
372368
}
@@ -379,12 +375,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
379375
}
380376

381377
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
382-
unsafe {
383-
llvm::LLVMPointerType(
384-
self.llvm_type(cx),
385-
cx.data_layout().instruction_address_space.0 as c_uint,
386-
)
387-
}
378+
cx.type_ptr_ext(cx.data_layout().instruction_address_space)
388379
}
389380

390381
fn llvm_cconv(&self) -> llvm::CallConv {

compiler/rustc_codegen_llvm/src/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) unsafe fn codegen(
2828
tws => bug!("Unsupported target word size for int: {}", tws),
2929
};
3030
let i8 = llvm::LLVMInt8TypeInContext(llcx);
31-
let i8p = llvm::LLVMPointerType(i8, 0);
31+
let i8p = llvm::LLVMPointerTypeInContext(llcx, 0);
3232
let void = llvm::LLVMVoidTypeInContext(llcx);
3333

3434
if kind == AllocatorKind::Default {

compiler/rustc_codegen_llvm/src/back/write.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::back::profiling::{
44
};
55
use crate::base;
66
use crate::common;
7-
use crate::consts;
87
use crate::errors::{
98
CopyBitcode, FromLlvmDiag, FromLlvmOptimizationDiag, LlvmError, WithLlvmError, WriteBytecode,
109
};
@@ -992,7 +991,7 @@ fn create_msvc_imps(
992991
let prefix = if cgcx.target_arch == "x86" { "\x01__imp__" } else { "\x01__imp_" };
993992

994993
unsafe {
995-
let i8p_ty = Type::i8p_llcx(llcx);
994+
let ptr_ty = Type::ptr_llcx(llcx);
996995
let globals = base::iter_globals(llmod)
997996
.filter(|&val| {
998997
llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage
@@ -1012,8 +1011,8 @@ fn create_msvc_imps(
10121011
.collect::<Vec<_>>();
10131012

10141013
for (imp_name, val) in globals {
1015-
let imp = llvm::LLVMAddGlobal(llmod, i8p_ty, imp_name.as_ptr().cast());
1016-
llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty));
1014+
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr().cast());
1015+
llvm::LLVMSetInitializer(imp, val);
10171016
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
10181017
}
10191018
}

compiler/rustc_codegen_llvm/src/base.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen
123123
// happen after the llvm.used variables are created.
124124
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
125125
unsafe {
126-
let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
127-
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
126+
llvm::LLVMReplaceAllUsesWith(old_g, new_g);
128127
llvm::LLVMDeleteGlobal(old_g);
129128
}
130129
}

compiler/rustc_codegen_llvm/src/builder.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
652652
flags: MemFlags,
653653
) -> &'ll Value {
654654
debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
655-
let ptr = self.check_store(val, ptr);
655+
let ptr = self.check_store(ptr);
656656
unsafe {
657657
let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
658658
let align =
@@ -682,7 +682,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
682682
size: Size,
683683
) {
684684
debug!("Store {:?} -> {:?}", val, ptr);
685-
let ptr = self.check_store(val, ptr);
685+
let ptr = self.check_store(ptr);
686686
unsafe {
687687
let store = llvm::LLVMRustBuildAtomicStore(
688688
self.llbuilder,
@@ -873,8 +873,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
873873
assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
874874
let size = self.intcast(size, self.type_isize(), false);
875875
let is_volatile = flags.contains(MemFlags::VOLATILE);
876-
let dst = self.pointercast(dst, self.type_i8p());
877-
let src = self.pointercast(src, self.type_i8p());
878876
unsafe {
879877
llvm::LLVMRustBuildMemCpy(
880878
self.llbuilder,
@@ -900,8 +898,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
900898
assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
901899
let size = self.intcast(size, self.type_isize(), false);
902900
let is_volatile = flags.contains(MemFlags::VOLATILE);
903-
let dst = self.pointercast(dst, self.type_i8p());
904-
let src = self.pointercast(src, self.type_i8p());
905901
unsafe {
906902
llvm::LLVMRustBuildMemMove(
907903
self.llbuilder,
@@ -924,7 +920,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
924920
flags: MemFlags,
925921
) {
926922
let is_volatile = flags.contains(MemFlags::VOLATILE);
927-
let ptr = self.pointercast(ptr, self.type_i8p());
928923
unsafe {
929924
llvm::LLVMRustBuildMemSet(
930925
self.llbuilder,
@@ -981,7 +976,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
981976
}
982977

983978
fn cleanup_landing_pad(&mut self, pers_fn: &'ll Value) -> (&'ll Value, &'ll Value) {
984-
let ty = self.type_struct(&[self.type_i8p(), self.type_i32()], false);
979+
let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
985980
let landing_pad = self.landing_pad(ty, pers_fn, 0);
986981
unsafe {
987982
llvm::LLVMSetCleanup(landing_pad, llvm::True);
@@ -990,14 +985,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
990985
}
991986

992987
fn filter_landing_pad(&mut self, pers_fn: &'ll Value) -> (&'ll Value, &'ll Value) {
993-
let ty = self.type_struct(&[self.type_i8p(), self.type_i32()], false);
988+
let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
994989
let landing_pad = self.landing_pad(ty, pers_fn, 1);
995-
self.add_clause(landing_pad, self.const_array(self.type_i8p(), &[]));
990+
self.add_clause(landing_pad, self.const_array(self.type_ptr(), &[]));
996991
(self.extract_value(landing_pad, 0), self.extract_value(landing_pad, 1))
997992
}
998993

999994
fn resume(&mut self, exn0: &'ll Value, exn1: &'ll Value) {
1000-
let ty = self.type_struct(&[self.type_i8p(), self.type_i32()], false);
995+
let ty = self.type_struct(&[self.type_ptr(), self.type_i32()], false);
1001996
let mut exn = self.const_poison(ty);
1002997
exn = self.insert_value(exn, exn0, 0);
1003998
exn = self.insert_value(exn, exn1, 1);
@@ -1161,7 +1156,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11611156

11621157
let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
11631158
let llty = self.cx.type_func(
1164-
&[self.cx.type_i8p(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_i32()],
1159+
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_i32()],
11651160
self.cx.type_void(),
11661161
);
11671162
let args = &[fn_name, hash, num_counters, index];
@@ -1387,23 +1382,12 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13871382
ret.expect("LLVM does not have support for catchret")
13881383
}
13891384

1390-
fn check_store(&mut self, val: &'ll Value, ptr: &'ll Value) -> &'ll Value {
1385+
fn check_store(&mut self, ptr: &'ll Value) -> &'ll Value {
13911386
let dest_ptr_ty = self.cx.val_ty(ptr);
1392-
let stored_ty = self.cx.val_ty(val);
1393-
let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
13941387

13951388
assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
13961389

1397-
if dest_ptr_ty == stored_ptr_ty {
1398-
ptr
1399-
} else {
1400-
debug!(
1401-
"type mismatch in store. \
1402-
Expected {:?}, got {:?}; inserting bitcast",
1403-
dest_ptr_ty, stored_ptr_ty
1404-
);
1405-
self.bitcast(ptr, stored_ptr_ty)
1406-
}
1390+
ptr
14071391
}
14081392

14091393
fn check_call<'b>(
@@ -1468,7 +1452,6 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
14681452
return;
14691453
}
14701454

1471-
let ptr = self.pointercast(ptr, self.cx.type_i8p());
14721455
self.call_intrinsic(intrinsic, &[self.cx.const_u64(size), ptr]);
14731456
}
14741457

compiler/rustc_codegen_llvm/src/callee.rs

+1-35
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
//! and methods are represented as just a fn ptr and not a full
55
//! closure.
66
7-
use crate::abi::FnAbiLlvmExt;
87
use crate::attributes;
98
use crate::common;
109
use crate::context::CodegenCx;
1110
use crate::llvm;
1211
use crate::value::Value;
13-
use rustc_codegen_ssa::traits::*;
1412

1513
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
1614
use rustc_middle::ty::{self, Instance, TypeVisitableExt};
@@ -45,39 +43,7 @@ pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) ->
4543
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
4644

4745
let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
48-
// Create a fn pointer with the new signature.
49-
let llptrty = fn_abi.ptr_to_llvm_type(cx);
50-
51-
// This is subtle and surprising, but sometimes we have to bitcast
52-
// the resulting fn pointer. The reason has to do with external
53-
// functions. If you have two crates that both bind the same C
54-
// library, they may not use precisely the same types: for
55-
// example, they will probably each declare their own structs,
56-
// which are distinct types from LLVM's point of view (nominal
57-
// types).
58-
//
59-
// Now, if those two crates are linked into an application, and
60-
// they contain inlined code, you can wind up with a situation
61-
// where both of those functions wind up being loaded into this
62-
// application simultaneously. In that case, the same function
63-
// (from LLVM's point of view) requires two types. But of course
64-
// LLVM won't allow one function to have two types.
65-
//
66-
// What we currently do, therefore, is declare the function with
67-
// one of the two types (whichever happens to come first) and then
68-
// bitcast as needed when the function is referenced to make sure
69-
// it has the type we expect.
70-
//
71-
// This can occur on either a crate-local or crate-external
72-
// reference. It also occurs when testing libcore and in some
73-
// other weird situations. Annoying.
74-
if cx.val_ty(llfn) != llptrty {
75-
debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
76-
cx.const_ptrcast(llfn, llptrty)
77-
} else {
78-
debug!("get_fn: not casting pointer!");
79-
llfn
80-
}
46+
llfn
8147
} else {
8248
let instance_def_id = instance.def_id();
8349
let llfn = if tcx.sess.target.arch == "x86" &&

compiler/rustc_codegen_llvm/src/common.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::consts::{self, 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;
7-
use crate::type_of::LayoutLlvmExt;
87
use crate::value::Value;
98

109
use rustc_ast::Mutability;
@@ -13,7 +12,6 @@ use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
1312
use rustc_hir::def_id::DefId;
1413
use rustc_middle::bug;
1514
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
16-
use rustc_middle::ty::layout::LayoutOf;
1715
use rustc_middle::ty::TyCtxt;
1816
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
1917
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer};
@@ -211,11 +209,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
211209
})
212210
.1;
213211
let len = s.len();
214-
let cs = consts::ptrcast(
215-
str_global,
216-
self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),
217-
);
218-
(cs, self.const_usize(len as u64))
212+
(str_global, self.const_usize(len as u64))
219213
}
220214

221215
fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
@@ -292,7 +286,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
292286
let llval = unsafe {
293287
llvm::LLVMConstInBoundsGEP2(
294288
self.type_i8(),
295-
self.const_bitcast(base_addr, self.type_i8p_ext(base_addr_space)),
289+
self.const_bitcast(base_addr, self.type_ptr_ext(base_addr_space)),
296290
&self.const_usize(offset.bytes()),
297291
1,
298292
)
@@ -322,7 +316,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
322316
unsafe {
323317
llvm::LLVMConstInBoundsGEP2(
324318
self.type_i8(),
325-
self.const_bitcast(base_addr, self.type_i8p()),
319+
base_addr,
326320
&self.const_usize(offset.bytes()),
327321
1,
328322
)

compiler/rustc_codegen_llvm/src/consts.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<
103103
value: Primitive::Pointer(address_space),
104104
valid_range: WrappingRange::full(dl.pointer_size),
105105
},
106-
cx.type_i8p_ext(address_space),
106+
cx.type_ptr_ext(address_space),
107107
));
108108
next_offset = offset + pointer_size;
109109
}
@@ -179,7 +179,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
179179
})
180180
});
181181
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
182-
llvm::LLVMSetInitializer(g2, cx.const_ptrcast(g1, llty));
182+
llvm::LLVMSetInitializer(g2, g1);
183183
g2
184184
}
185185
} else if cx.tcx.sess.target.arch == "x86" &&
@@ -251,7 +251,7 @@ impl<'ll> CodegenCx<'ll, '_> {
251251
let g = if def_id.is_local() && !self.tcx.is_foreign_item(def_id) {
252252
let llty = self.layout_of(ty).llvm_type(self);
253253
if let Some(g) = self.get_declared_value(sym) {
254-
if self.val_ty(g) != self.type_ptr_to(llty) {
254+
if self.val_ty(g) != self.type_ptr() {
255255
span_bug!(self.tcx.def_span(def_id), "Conflicting types for static");
256256
}
257257
}
@@ -552,16 +552,14 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
552552
}
553553
}
554554

555-
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
555+
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
556556
fn add_used_global(&self, global: &'ll Value) {
557-
let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_i8p()) };
558-
self.used_statics.borrow_mut().push(cast);
557+
self.used_statics.borrow_mut().push(global);
559558
}
560559

561560
/// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
562-
/// an array of i8*.
561+
/// an array of ptr.
563562
fn add_compiler_used_global(&self, global: &'ll Value) {
564-
let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_i8p()) };
565-
self.compiler_used_statics.borrow_mut().push(cast);
563+
self.compiler_used_statics.borrow_mut().push(global);
566564
}
567565
}

0 commit comments

Comments
 (0)