From cd07a3e2f9e7c35e90f82bc33cfec0be59c27568 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 6 Feb 2018 23:22:14 +0000 Subject: 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 (https://github.com/rust-lang/rust/issues/41315). Differential Revision: https://reviews.llvm.org/D41705 Patch by varkor! llvm-svn: 324424 --- llvm/lib/Target/TargetLoweringObjectFile.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Target/TargetLoweringObjectFile.cpp') diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp index 72baf5985ea..6654eab7f01 100644 --- a/llvm/lib/Target/TargetLoweringObjectFile.cpp +++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp @@ -52,11 +52,24 @@ TargetLoweringObjectFile::~TargetLoweringObjectFile() { delete Mang; } +static bool isNullOrUndef(const Constant *C) { + // Check that the constant isn't all zeros or undefs. + if (C->isNullValue() || isa(C)) + return true; + if (!isa(C)) + return false; + for (auto Operand : C->operand_values()) { + if (!isNullOrUndef(cast(Operand))) + return false; + } + return true; +} + static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { const Constant *C = GV->getInitializer(); // Must have zero initializer. - if (!C->isNullValue()) + if (!isNullOrUndef(C)) return false; // Leave constant zeros in readonly constant sections, so they can be shared. -- cgit v1.2.3