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,36 @@ +; RUN: opt < %s -S -globalopt -instcombine | FileCheck %s + +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 + +%struct.norb = type { [1 x double], double } + +@norb = internal global %struct.norb { [1 x double] zeroinitializer, double 4.053854e+04 }, 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: call void @bar(double 4.053854e+04) + + %1 = load i64* bitcast (%struct.gorp* @gorp to i64*), align 1 + call fastcc void @blarg(i64 %1) +; CHECK: call fastcc void @blarg(i64 1) + + %2 = load double* getelementptr inbounds (%struct.norb* @norb, i32 0, i32 0, i32 1), align 1 + call void @bar(double %0) +; CHECK: call void @bar(double 4.053854e+04) + + ret void +} + +declare void @bar(double) +declare void @blarg(i64) \ No newline at end of file