Skip to content

Commit cd07a3e

Browse files
author
Eli Friedman
committed
Place undefined globals in .bss instead of .data
Following up on the discussion from http://lists.llvm.org/pipermail/llvm-dev/2017-April/112305.html, undef values are now placed in the .bss as well as null values. This prevents undef global values taking up potentially huge amounts of space in the .data section. The following two lines now both generate equivalent .bss data: @vals1 = internal unnamed_addr global [20000000 x i32] zeroinitializer, align 4 @Vals2 = internal unnamed_addr global [20000000 x i32] undef, align 4 ; previously unaccounted for This is primarily motivated by the corresponding issue in the Rust compiler (rust-lang/rust#41315). Differential Revision: https://reviews.llvm.org/D41705 Patch by varkor! llvm-svn: 324424
1 parent a3d37f0 commit cd07a3e

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

llvm/lib/Target/TargetLoweringObjectFile.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,24 @@ TargetLoweringObjectFile::~TargetLoweringObjectFile() {
5252
delete Mang;
5353
}
5454

55+
static bool isNullOrUndef(const Constant *C) {
56+
// Check that the constant isn't all zeros or undefs.
57+
if (C->isNullValue() || isa<UndefValue>(C))
58+
return true;
59+
if (!isa<ConstantAggregate>(C))
60+
return false;
61+
for (auto Operand : C->operand_values()) {
62+
if (!isNullOrUndef(cast<Constant>(Operand)))
63+
return false;
64+
}
65+
return true;
66+
}
67+
5568
static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
5669
const Constant *C = GV->getInitializer();
5770

5871
// Must have zero initializer.
59-
if (!C->isNullValue())
72+
if (!isNullOrUndef(C))
6073
return false;
6174

6275
// Leave constant zeros in readonly constant sections, so they can be shared.

llvm/test/CodeGen/ARM/memfunc.ll

+4-2
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,10 @@ entry:
421421
; CHECK: arr5:
422422
; CHECK-NOT: .p2align
423423
; CHECK: arr6:
424-
; CHECK: .p2align 4
425-
; CHECK: arr8:
424+
; CHECK-IOS: arr8,128,4
425+
; CHECK-DARWIN: arr8,128,4
426+
; CHECK-EABI: arr8,128,16
427+
; CHECK-GNUEABI: arr8,128,16
426428
; CHECK: .p2align 4
427429
; CHECK: arr9:
428430

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; RUN: llc < %s | FileCheck %s
2+
target triple = "x86_64-apple-darwin"
3+
4+
; CHECK: .zerofill __DATA,__bss,_vals,8000000,2 ## @vals
5+
@vals = internal unnamed_addr global [2000000 x i32] undef, align 4
6+
7+
; CHECK: .zerofill __DATA,__bss,_struct,8000040,3 ## @struct
8+
@struct = internal global { i1, [8000000 x i8], [7 x i8], i64, { [4 x i32], { i8 }, i1 } }
9+
{ i1 false, [8000000 x i8] zeroinitializer, [7 x i8] undef, i64 0,
10+
{ [4 x i32], { i8 }, i1 }
11+
{ [4 x i32] zeroinitializer, { i8 } { i8 undef }, i1 false }
12+
}, align 8

0 commit comments

Comments
 (0)