11use std:: ops:: Range ;
22
33use rustc_abi:: { Align , HasDataLayout , Primitive , Scalar , Size , WrappingRange } ;
4+ use rustc_ast:: Mutability ;
45use rustc_codegen_ssa:: common;
56use rustc_codegen_ssa:: traits:: * ;
67use rustc_hir:: LangItem ;
@@ -225,12 +226,26 @@ impl<'ll> CodegenCx<'ll, '_> {
225226 ///
226227 /// The returned global variable is a pointer in the default address space for globals.
227228 /// Fails if a symbol with the given name already exists.
228- pub ( crate ) fn static_addr_of_mut (
229+ pub ( crate ) fn static_addr_of (
229230 & self ,
230231 cv : & ' ll Value ,
231232 align : Align ,
232233 kind : Option < & str > ,
234+ mutability : Mutability ,
233235 ) -> & ' ll Value {
236+ if let Mutability :: Not = mutability
237+ && let Some ( & gv) = self . const_globals . borrow ( ) . get ( & cv)
238+ {
239+ unsafe {
240+ // Upgrade the alignment in cases where the same constant is used with different
241+ // alignment requirements
242+ let llalign = align. bytes ( ) as u32 ;
243+ if llalign > llvm:: LLVMGetAlignment ( gv) {
244+ llvm:: LLVMSetAlignment ( gv, llalign) ;
245+ }
246+ }
247+ return gv;
248+ }
234249 let gv = match kind {
235250 Some ( kind) if !self . tcx . sess . fewer_names ( ) => {
236251 let name = self . generate_local_symbol_name ( kind) ;
@@ -247,33 +262,11 @@ impl<'ll> CodegenCx<'ll, '_> {
247262 llvm:: set_initializer ( gv, cv) ;
248263 set_global_alignment ( self , gv, align) ;
249264 llvm:: set_unnamed_address ( gv, llvm:: UnnamedAddr :: Global ) ;
250- gv
251- }
265+ if let Mutability :: Not = mutability {
266+ llvm :: set_global_constant ( gv , true ) ;
252267
253- /// Create a global constant.
254- ///
255- /// The returned global variable is a pointer in the default address space for globals.
256- pub ( crate ) fn static_addr_of_const (
257- & self ,
258- cv : & ' ll Value ,
259- align : Align ,
260- kind : Option < & str > ,
261- ) -> & ' ll Value {
262- if let Some ( & gv) = self . const_globals . borrow ( ) . get ( & cv) {
263- unsafe {
264- // Upgrade the alignment in cases where the same constant is used with different
265- // alignment requirements
266- let llalign = align. bytes ( ) as u32 ;
267- if llalign > llvm:: LLVMGetAlignment ( gv) {
268- llvm:: LLVMSetAlignment ( gv, llalign) ;
269- }
270- }
271- return gv;
268+ self . const_globals . borrow_mut ( ) . insert ( cv, gv) ;
272269 }
273- let gv = self . static_addr_of_mut ( cv, align, kind) ;
274- llvm:: set_global_constant ( gv, true ) ;
275-
276- self . const_globals . borrow_mut ( ) . insert ( cv, gv) ;
277270 gv
278271 }
279272
@@ -760,20 +753,6 @@ impl<'ll> CodegenCx<'ll, '_> {
760753}
761754
762755impl < ' ll > StaticCodegenMethods for CodegenCx < ' ll , ' _ > {
763- /// Get a pointer to a global variable.
764- ///
765- /// The pointer will always be in the default address space. If global variables default to a
766- /// different address space, an addrspacecast is inserted.
767- fn static_addr_of ( & self , alloc : ConstAllocation < ' _ > , kind : Option < & str > ) -> & ' ll Value {
768- // FIXME: should we cache `const_alloc_to_llvm` to avoid repeating this for the
769- // same `ConstAllocation`?
770- let cv = const_alloc_to_llvm ( self , alloc. inner ( ) , /*static*/ false ) ;
771- let gv = self . static_addr_of_const ( cv, alloc. inner ( ) . align , kind) ;
772- // static_addr_of_const returns the bare global variable, which might not be in the default
773- // address space. Cast to the default address space if necessary.
774- self . const_pointercast ( gv, self . type_ptr ( ) )
775- }
776-
777756 fn codegen_static ( & mut self , def_id : DefId ) {
778757 self . codegen_static_item ( def_id)
779758 }
0 commit comments