Index: lib/Analysis/ConstantFolding.cpp =================================================================== --- lib/Analysis/ConstantFolding.cpp +++ lib/Analysis/ConstantFolding.cpp @@ -512,7 +512,10 @@ // a different type, so we can try to walk down through the initial // elements of an aggregate to see if some part of th e aggregate is // castable to implement the "load" semantic model. - C = C->getAggregateElement(0u); + Constant *D = C->getAggregateElement(0U); + for (unsigned i = 0; D && D->getType()->isEmptyTy(); ++i) + D = C->getAggregateElement(i); + C = D; } while (C); return nullptr; @@ -1206,6 +1209,10 @@ C = C->getAggregateElement(CE->getOperand(i)); if (!C) return nullptr; + + // Don't step through empty types. The aliasing is too hard to figure out. + if (C->getType()->isEmptyTy()) + return nullptr; } return C; } @@ -1222,6 +1229,10 @@ C = C->getAggregateElement(Indices[i]); if (!C) return nullptr; + + // Don't step through empty types. The aliasing is too hard to figure out. + if (C->getType()->isEmptyTy()) + return nullptr; } return C; } Index: test/Transforms/GlobalOpt/empty-type-field.ll =================================================================== --- test/Transforms/GlobalOpt/empty-type-field.ll +++ test/Transforms/GlobalOpt/empty-type-field.ll @@ -0,0 +1,29 @@ +; RUN: opt < %s -S -globalopt -instcombine | FileCheck %s + +; ModuleID = 'bugpoint-passinput.bc' +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.qux = type { [0 x double], double } + +@qux = internal global %struct.qux { [0 x double] zeroinitializer, double 4.053854e+04 }, align 8 + +%struct.gorp = type { [0 x i8*], i8* } + +@gorp = internal unnamed_addr constant %struct.gorp { [0 x i8*] zeroinitializer, i8* inttoptr (i64 1 to i8*) }, align 8 + +define void @foo() { +entry: + %0 = load double* getelementptr inbounds (%struct.qux* @qux, i32 0, i32 0, i32 0), align 1 + call void @bar(double %0) +; CHECK-NOT: @bar(double 0.000000e+00) + + %1 = load i64* bitcast (%struct.gorp* @gorp to i64*), align 1 + call fastcc void @blarg(i64 %1) +; CHECK-NOT: @blarg(i64 0) + + ret void +} + +declare void @bar(double) +declare void @blarg(i64) \ No newline at end of file