diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index 395ac3907ba..9124c111935 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -2144,9 +2144,10 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, // If all indices are known integers and normalized, we can do a simple // check for the "inbounds" property. - if (!Unknown && !inBounds && - isa(C) && isInBoundsIndices(Idxs)) - return ConstantExpr::getInBoundsGetElementPtr(C, Idxs); + if (!Unknown && !inBounds) + if (auto *GV = dyn_cast(C)) + if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs)) + return ConstantExpr::getInBoundsGetElementPtr(C, Idxs); return nullptr; } diff --git a/test/Assembler/ConstantExprNoFold.ll b/test/Assembler/ConstantExprNoFold.ll index d21441372bf..04ad3f5b130 100644 --- a/test/Assembler/ConstantExprNoFold.ll +++ b/test/Assembler/ConstantExprNoFold.ll @@ -25,3 +25,15 @@ target datalayout = "p:32:32" ; CHECK: @E = global i64 addrspace(1)* addrspacecast (i64* @A to i64 addrspace(1)*) @E = global i64 addrspace(1)* addrspacecast(i64* @A to i64 addrspace(1)*) + +; Don't add an inbounds on @weak.gep, since @weak may be null. +; CHECK: @weak.gep = global i32* getelementptr (i32* @weak, i32 1) +@weak.gep = global i32* getelementptr (i32* @weak, i32 1) +@weak = extern_weak global i32 + +; Don't add an inbounds on @glob.a3, since it's not inbounds. +; CHECK: @glob.a3 = alias getelementptr (i32* @glob.a2, i32 1) +@glob = global i32 0 +@glob.a3 = alias getelementptr (i32* @glob.a2, i32 1) +@glob.a2 = alias getelementptr (i32* @glob.a1, i32 1) +@glob.a1 = alias i32* @glob