Browse Source

CodeGen: When emitting stores for an initializer, only emit a GEP if we really need the store.

This avoids emitting many dead GEPs for large zero-initialized arrays.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162701 91177308-0d34-0410-b5e6-96231b3b80d8
Benjamin Kramer 13 years ago
parent
commit
cfa07e3519
2 changed files with 12 additions and 7 deletions
  1. 10 7
      lib/CodeGen/CGDecl.cpp
  2. 2 0
      test/CodeGen/init.c

+ 10 - 7
lib/CodeGen/CGDecl.cpp

@@ -719,10 +719,11 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
         dyn_cast<llvm::ConstantDataSequential>(Init)) {
     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
       llvm::Constant *Elt = CDS->getElementAsConstant(i);
-      
-      // Get a pointer to the element and emit it.
-      emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
-                                   isVolatile, Builder);
+
+      // If necessary, get a pointer to the element and emit it.
+      if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
+        emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
+                                     isVolatile, Builder);
     }
     return;
   }
@@ -732,9 +733,11 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
 
   for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
     llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
-    // Get a pointer to the element and emit it.
-    emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
-                                 isVolatile, Builder);
+
+    // If necessary, get a pointer to the element and emit it.
+    if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
+      emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
+                                   isVolatile, Builder);
   }
 }
 

+ 2 - 0
test/CodeGen/init.c

@@ -69,6 +69,8 @@ char test8(int X) {
 // CHECK: store i8 97
 // CHECK: store i8 98
 // CHECK: store i8 99
+// CHECK-NOT: getelementptr
+// CHECK: load
 }
 
 void bar(void*);