Skip to content

Commit 68cabc0

Browse files
committedDec 7, 2022
cg_ssa, cg_llvm: remove unnecessary pointercast/bitcast-of-ptr
1 parent 0fcc9be commit 68cabc0

File tree

19 files changed

+46
-218
lines changed

19 files changed

+46
-218
lines changed
 

Diff for: ‎compiler/rustc_codegen_llvm/src/abi.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
226226
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
227227
let can_store_through_cast_ptr = false;
228228
if can_store_through_cast_ptr {
229-
let cast_ptr_llty = bx.type_ptr();
230-
let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
231-
bx.store(val, cast_dst, self.layout.align.abi);
229+
bx.store(val, dst.llval, self.layout.align.abi);
232230
} else {
233231
// The actual return type is a struct, but the ABI
234232
// adaptation code has cast it into some scalar type. The

Diff for: ‎compiler/rustc_codegen_llvm/src/back/write.rs

+1-2
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::llvm::{self, DiagnosticInfo, PassManager};
98
use crate::llvm_util;
109
use crate::type_::Type;
@@ -948,7 +947,7 @@ fn create_msvc_imps(
948947

949948
for (imp_name, val) in globals {
950949
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr().cast());
951-
llvm::LLVMSetInitializer(imp, consts::ptrcast(val, ptr_ty));
950+
llvm::LLVMSetInitializer(imp, val);
952951
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
953952
}
954953
}

Diff for: ‎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
}

Diff for: ‎compiler/rustc_codegen_llvm/src/builder.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
854854
assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
855855
let size = self.intcast(size, self.type_isize(), false);
856856
let is_volatile = flags.contains(MemFlags::VOLATILE);
857-
let dst = self.pointercast(dst, self.type_ptr());
858-
let src = self.pointercast(src, self.type_ptr());
859857
unsafe {
860858
llvm::LLVMRustBuildMemCpy(
861859
self.llbuilder,
@@ -881,8 +879,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
881879
assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memmove not supported");
882880
let size = self.intcast(size, self.type_isize(), false);
883881
let is_volatile = flags.contains(MemFlags::VOLATILE);
884-
let dst = self.pointercast(dst, self.type_ptr());
885-
let src = self.pointercast(src, self.type_ptr());
886882
unsafe {
887883
llvm::LLVMRustBuildMemMove(
888884
self.llbuilder,
@@ -905,7 +901,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
905901
flags: MemFlags,
906902
) {
907903
let is_volatile = flags.contains(MemFlags::VOLATILE);
908-
let ptr = self.pointercast(ptr, self.type_ptr());
909904
unsafe {
910905
llvm::LLVMRustBuildMemSet(
911906
self.llbuilder,
@@ -1342,20 +1337,10 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
13421337

13431338
fn check_store(&mut self, ptr: &'ll Value) -> &'ll Value {
13441339
let dest_ptr_ty = self.cx.val_ty(ptr);
1345-
let stored_ptr_ty = self.cx.type_ptr();
13461340

13471341
assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
13481342

1349-
if dest_ptr_ty == stored_ptr_ty {
1350-
ptr
1351-
} else {
1352-
debug!(
1353-
"type mismatch in store. \
1354-
Expected {:?}, got {:?}; inserting bitcast",
1355-
dest_ptr_ty, stored_ptr_ty
1356-
);
1357-
self.bitcast(ptr, stored_ptr_ty)
1358-
}
1343+
ptr
13591344
}
13601345

13611346
fn check_call<'b>(
@@ -1420,7 +1405,6 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
14201405
return;
14211406
}
14221407

1423-
let ptr = self.pointercast(ptr, self.cx.type_ptr());
14241408
self.call_intrinsic(intrinsic, &[self.cx.const_u64(size), ptr]);
14251409
}
14261410

Diff for: ‎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, TypeVisitable};
@@ -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" &&

Diff for: ‎compiler/rustc_codegen_llvm/src/common.rs

+4-9
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;
@@ -202,8 +202,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
202202
})
203203
.1;
204204
let len = s.len();
205-
let cs = consts::ptrcast(str_global, self.type_ptr());
206-
(cs, self.const_usize(len as u64))
205+
(str_global, self.const_usize(len as u64))
207206
}
208207

209208
fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
@@ -312,19 +311,15 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
312311
let llval = unsafe {
313312
llvm::LLVMRustConstInBoundsGEP2(
314313
self.type_i8(),
315-
self.const_bitcast(base_addr, self.type_ptr()),
314+
base_addr,
316315
&self.const_usize(offset.bytes()),
317316
1,
318317
)
319318
};
320-
self.const_bitcast(llval, llty)
319+
llval
321320
};
322321
PlaceRef::new_sized(llval, layout)
323322
}
324-
325-
fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
326-
consts::ptrcast(val, ty)
327-
}
328323
}
329324

330325
/// Get the [LLVM type][Type] of a [`Value`].

Diff for: ‎compiler/rustc_codegen_llvm/src/consts.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ fn check_and_apply_linkage<'ll, 'tcx>(
209209
}
210210
}
211211

212-
pub fn ptrcast<'ll>(val: &'ll Value, ty: &'ll Type) -> &'ll Value {
213-
unsafe { llvm::LLVMConstPointerCast(val, ty) }
214-
}
215-
216212
impl<'ll> CodegenCx<'ll, '_> {
217213
pub(crate) fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
218214
unsafe { llvm::LLVMConstBitCast(val, ty) }
@@ -572,14 +568,12 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
572568

573569
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
574570
fn add_used_global(&self, global: &'ll Value) {
575-
let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_ptr()) };
576-
self.used_statics.borrow_mut().push(cast);
571+
self.used_statics.borrow_mut().push(global);
577572
}
578573

579574
/// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
580575
/// an array of ptr.
581576
fn add_compiler_used_global(&self, global: &'ll Value) {
582-
let cast = unsafe { llvm::LLVMConstPointerCast(global, self.type_ptr()) };
583-
self.compiler_used_statics.borrow_mut().push(cast);
577+
self.compiler_used_statics.borrow_mut().push(global);
584578
}
585579
}

Diff for: ‎compiler/rustc_codegen_llvm/src/context.rs

-13
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ pub struct CodegenCx<'ll, 'tcx> {
5959
/// Cache of constant strings,
6060
pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
6161

62-
/// Reverse-direction for const ptrs cast from globals.
63-
///
64-
/// Key is a Value holding a `*T`,
65-
/// Val is a Value holding a `*[T]`.
66-
///
67-
/// Needed because LLVM loses pointer->pointee association
68-
/// when we ptrcast, and we have to ptrcast during codegen
69-
/// of a `[T]` const because we form a slice, a `(*T,usize)` pair, not
70-
/// a pointer to an LLVM array type. Similar for trait objects.
71-
pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
72-
7362
/// Cache of emitted const globals (value -> global)
7463
pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
7564

@@ -435,7 +424,6 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
435424
instances: Default::default(),
436425
vtables: Default::default(),
437426
const_str_cache: Default::default(),
438-
const_unsized: Default::default(),
439427
const_globals: Default::default(),
440428
statics_to_rauw: RefCell::new(Vec::new()),
441429
used_statics: RefCell::new(Vec::new()),
@@ -903,7 +891,6 @@ impl<'ll> CodegenCx<'ll, '_> {
903891
self.declare_global("rust_eh_catch_typeinfo", ty)
904892
}
905893
};
906-
let eh_catch_typeinfo = self.const_bitcast(eh_catch_typeinfo, self.type_ptr());
907894
self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
908895
eh_catch_typeinfo
909896
}

Diff for: ‎compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use rustc_span::DebuggerVisualizerType;
1818
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
1919
pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
2020
if needs_gdb_debug_scripts_section(bx) {
21-
let gdb_debug_scripts_section =
22-
bx.const_bitcast(get_or_insert_gdb_debug_scripts_section_global(bx), bx.type_ptr());
21+
let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx);
2322
// Load just the first byte as that's all that's necessary to force
2423
// LLVM to keep around the reference to the global.
2524
let volative_load_instruction = bx.volatile_load(bx.type_i8(), gdb_debug_scripts_section);

Diff for: ‎compiler/rustc_codegen_llvm/src/intrinsic.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
165165
let ptr = args[0].immediate();
166166
let load = if let PassMode::Cast(ty, _) = &fn_abi.ret.mode {
167167
let llty = ty.llvm_type(self);
168-
let ptr = self.pointercast(ptr, self.type_ptr());
169168
self.volatile_load(llty, ptr)
170169
} else {
171170
self.volatile_load(self.layout_of(tp_ty).llvm_type(self), ptr)
@@ -319,18 +318,12 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
319318
self.const_bool(true)
320319
} else if use_integer_compare {
321320
let integer_ty = self.type_ix(layout.size().bits());
322-
let ptr_ty = self.type_ptr();
323-
let a_ptr = self.bitcast(a, ptr_ty);
324-
let a_val = self.load(integer_ty, a_ptr, layout.align().abi);
325-
let b_ptr = self.bitcast(b, ptr_ty);
326-
let b_val = self.load(integer_ty, b_ptr, layout.align().abi);
321+
let a_val = self.load(integer_ty, a, layout.align().abi);
322+
let b_val = self.load(integer_ty, b, layout.align().abi);
327323
self.icmp(IntPredicate::IntEQ, a_val, b_val)
328324
} else {
329-
let ptr_ty = self.type_ptr();
330-
let a_ptr = self.bitcast(a, ptr_ty);
331-
let b_ptr = self.bitcast(b, ptr_ty);
332325
let n = self.const_usize(layout.size().bytes());
333-
let cmp = self.call_intrinsic("memcmp", &[a_ptr, b_ptr, n]);
326+
let cmp = self.call_intrinsic("memcmp", &[a, b, n]);
334327
match self.cx.sess().target.arch.as_ref() {
335328
"avr" | "msp430" => self.icmp(IntPredicate::IntEQ, cmp, self.const_i16(0)),
336329
_ => self.icmp(IntPredicate::IntEQ, cmp, self.const_i32(0)),
@@ -386,9 +379,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
386379

387380
if !fn_abi.ret.is_ignore() {
388381
if let PassMode::Cast(_, _) = &fn_abi.ret.mode {
389-
let ptr_llty = self.type_ptr();
390-
let ptr = self.pointercast(result.llval, ptr_llty);
391-
self.store(llval, ptr, result.align);
382+
self.store(llval, result.llval, result.align);
392383
} else {
393384
OperandRef::from_immediate_or_packed_pair(self, llval, result.layout)
394385
.val
@@ -412,9 +403,7 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
412403
fn type_test(&mut self, pointer: Self::Value, typeid: Self::Value) -> Self::Value {
413404
// Test the called operand using llvm.type.test intrinsic. The LowerTypeTests link-time
414405
// optimization pass replaces calls to this intrinsic with code to test type membership.
415-
let ptr_ty = self.type_ptr();
416-
let bitcast = self.bitcast(pointer, ptr_ty);
417-
self.call_intrinsic("llvm.type.test", &[bitcast, typeid])
406+
self.call_intrinsic("llvm.type.test", &[pointer, typeid])
418407
}
419408

420409
fn type_checked_load(
@@ -748,7 +737,6 @@ fn codegen_emcc_try<'ll>(
748737
let catch_data_1 =
749738
bx.inbounds_gep(catch_data_type, catch_data, &[bx.const_usize(0), bx.const_usize(1)]);
750739
bx.store(is_rust_panic, catch_data_1, i8_align);
751-
let catch_data = bx.bitcast(catch_data, bx.type_ptr());
752740

753741
let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
754742
bx.call(catch_ty, None, catch_func, &[data, catch_data], None);
@@ -897,8 +885,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
897885
let place = PlaceRef::alloca(bx, args[0].layout);
898886
args[0].val.store(bx, place);
899887
let int_ty = bx.type_ix(expected_bytes * 8);
900-
let ptr = bx.pointercast(place.llval, bx.cx.type_ptr());
901-
bx.load(int_ty, ptr, Align::ONE)
888+
bx.load(int_ty, place.llval, Align::ONE)
902889
}
903890
_ => return_error!(
904891
"invalid bitmask `{}`, expected `u{}` or `[u8; {}]`",
@@ -1145,7 +1132,6 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
11451132
let ptr = bx.alloca(bx.type_ix(expected_bytes * 8), Align::ONE);
11461133
bx.store(ze, ptr, Align::ONE);
11471134
let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
1148-
let ptr = bx.pointercast(ptr, bx.cx.type_ptr());
11491135
return Ok(bx.load(array_ty, ptr, Align::ONE));
11501136
}
11511137
_ => return_error!(
@@ -1763,11 +1749,7 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#,
17631749
_ => return_error!("expected pointer, got `{}`", out_elem),
17641750
}
17651751

1766-
if in_elem == out_elem {
1767-
return Ok(args[0].immediate());
1768-
} else {
1769-
return Ok(bx.pointercast(args[0].immediate(), llret_ty));
1770-
}
1752+
return Ok(args[0].immediate());
17711753
}
17721754

17731755
if name == sym::simd_expose_addr {

Diff for: ‎compiler/rustc_codegen_llvm/src/va_arg.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ fn emit_direct_ptr_va_arg<'ll, 'tcx>(
3232
allow_higher_align: bool,
3333
) -> (&'ll Value, Align) {
3434
let va_list_ty = bx.type_ptr();
35-
let va_list_ptr_ty = bx.type_ptr();
36-
let va_list_addr = if list.layout.llvm_type(bx.cx) != va_list_ptr_ty {
37-
bx.bitcast(list.immediate(), va_list_ptr_ty)
38-
} else {
39-
list.immediate()
40-
};
35+
let va_list_addr = list.immediate();
4136

4237
let ptr = bx.load(va_list_ty, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
4338

@@ -55,9 +50,9 @@ fn emit_direct_ptr_va_arg<'ll, 'tcx>(
5550
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::Big {
5651
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
5752
let adjusted = bx.inbounds_gep(bx.type_i8(), addr, &[adjusted_size]);
58-
(bx.bitcast(adjusted, bx.type_ptr()), addr_align)
53+
(adjusted, addr_align)
5954
} else {
60-
(bx.bitcast(addr, bx.type_ptr()), addr_align)
55+
(addr, addr_align)
6156
}
6257
}
6358

@@ -157,7 +152,6 @@ fn emit_aapcs_va_arg<'ll, 'tcx>(
157152
reg_addr = bx.gep(bx.type_i8(), reg_addr, &[offset]);
158153
}
159154
let reg_type = layout.llvm_type(bx);
160-
let reg_addr = bx.bitcast(reg_addr, bx.type_ptr());
161155
let reg_value = bx.load(reg_type, reg_addr, layout.align.abi);
162156
bx.br(end);
163157

0 commit comments

Comments
 (0)