GlobalOpt: If we have an inbounds GEP from a ConstantAggregateZero global that we...
authorBenjamin Kramer <benny.kra@googlemail.com>
Wed, 28 Mar 2012 14:50:09 +0000 (14:50 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Wed, 28 Mar 2012 14:50:09 +0000 (14:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153576 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/GlobalOpt.cpp
test/Transforms/GlobalOpt/zeroinitializer-gep-load.ll [new file with mode: 0644]

index a32e55095455e4c9f8852a9cb99ae95f0b6a2462..1522aa408b6b259c2ad08ac1276a995c3d72af50 100644 (file)
@@ -341,6 +341,12 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
           dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, TD, TLI));
         if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
           SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
+
+        // If the initializer is an all-null value and we have an inbounds GEP,
+        // we already know what the result of any load from that GEP is.
+        // TODO: Handle splats.
+        if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
+          SubInit = Constant::getNullValue(GEP->getType()->getElementType());
       }
       Changed |= CleanupConstantGlobalUsers(GEP, SubInit, TD, TLI);
 
diff --git a/test/Transforms/GlobalOpt/zeroinitializer-gep-load.ll b/test/Transforms/GlobalOpt/zeroinitializer-gep-load.ll
new file mode 100644 (file)
index 0000000..d613601
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: opt < %s -S -globalopt | FileCheck %s
+
+@zero = internal global [10 x i32] zeroinitializer
+
+define i32 @test1(i64 %idx) nounwind {
+  %arrayidx = getelementptr inbounds [10 x i32]* @zero, i64 0, i64 %idx
+  %l = load i32* %arrayidx
+  ret i32 %l
+; CHECK: @test1
+; CHECK: ret i32 0
+}