[llvm] r324424 - Place undefined globals in .bss instead of .data

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 6 15:22:14 PST 2018


Author: efriedma
Date: Tue Feb  6 15:22:14 2018
New Revision: 324424

URL: http://llvm.org/viewvc/llvm-project?rev=324424&view=rev
Log:
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!


Added:
    llvm/trunk/test/CodeGen/X86/undef-globals-bss.ll
Modified:
    llvm/trunk/lib/Target/TargetLoweringObjectFile.cpp
    llvm/trunk/test/CodeGen/ARM/memfunc.ll

Modified: llvm/trunk/lib/Target/TargetLoweringObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetLoweringObjectFile.cpp?rev=324424&r1=324423&r2=324424&view=diff
==============================================================================
--- llvm/trunk/lib/Target/TargetLoweringObjectFile.cpp (original)
+++ llvm/trunk/lib/Target/TargetLoweringObjectFile.cpp Tue Feb  6 15:22:14 2018
@@ -52,11 +52,24 @@ TargetLoweringObjectFile::~TargetLowerin
   delete Mang;
 }
 
+static bool isNullOrUndef(const Constant *C) {
+  // Check that the constant isn't all zeros or undefs.
+  if (C->isNullValue() || isa<UndefValue>(C))
+    return true;
+  if (!isa<ConstantAggregate>(C))
+    return false;
+  for (auto Operand : C->operand_values()) {
+    if (!isNullOrUndef(cast<Constant>(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.

Modified: llvm/trunk/test/CodeGen/ARM/memfunc.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/ARM/memfunc.ll?rev=324424&r1=324423&r2=324424&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/ARM/memfunc.ll (original)
+++ llvm/trunk/test/CodeGen/ARM/memfunc.ll Tue Feb  6 15:22:14 2018
@@ -421,8 +421,10 @@ entry:
 ; CHECK: arr5:
 ; CHECK-NOT: .p2align
 ; CHECK: arr6:
-; CHECK: .p2align 4
-; CHECK: arr8:
+; CHECK-IOS: arr8,128,4
+; CHECK-DARWIN: arr8,128,4
+; CHECK-EABI: arr8,128,16
+; CHECK-GNUEABI: arr8,128,16
 ; CHECK: .p2align 4
 ; CHECK: arr9:
 

Added: llvm/trunk/test/CodeGen/X86/undef-globals-bss.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/undef-globals-bss.ll?rev=324424&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/undef-globals-bss.ll (added)
+++ llvm/trunk/test/CodeGen/X86/undef-globals-bss.ll Tue Feb  6 15:22:14 2018
@@ -0,0 +1,12 @@
+; RUN: llc < %s | FileCheck %s
+target triple = "x86_64-apple-darwin"
+
+; CHECK: .zerofill __DATA,__bss,_vals,8000000,2 ## @vals
+ at vals = internal unnamed_addr global [2000000 x i32] undef, align 4
+
+; CHECK: .zerofill __DATA,__bss,_struct,8000040,3 ## @struct
+ at struct = internal global { i1, [8000000 x i8], [7 x i8], i64, { [4 x i32], { i8 }, i1 } }
+                { i1 false, [8000000 x i8] zeroinitializer, [7 x i8] undef, i64 0,
+                        { [4 x i32], { i8 }, i1 }
+                        { [4 x i32] zeroinitializer, { i8 } { i8 undef }, i1 false }
+                }, align 8




More information about the llvm-commits mailing list