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);
--- /dev/null
+; 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
+}