IR: Don't add inbounds to GEPs of extern_weak variables

Global variables that have `extern_weak` linkage may be null, so it's
incorrect to add `inbounds` when constant folding.

This also fixes a bug when parsing global aliases, whose forward
reference placeholders are global variables with `extern_weak` linkage.
If GEPs to these aliases are encountered before the alias itself, the
GEPs would incorrectly gain the `inbounds` keyword as well.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215803 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2014-08-16 01:54:32 +00:00
parent 89cea3c36b
commit 7a5cb43115
2 changed files with 16 additions and 3 deletions

View File

@ -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<GlobalVariable>(C) && isInBoundsIndices(Idxs))
return ConstantExpr::getInBoundsGetElementPtr(C, Idxs);
if (!Unknown && !inBounds)
if (auto *GV = dyn_cast<GlobalVariable>(C))
if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs))
return ConstantExpr::getInBoundsGetElementPtr(C, Idxs);
return nullptr;
}

View File

@ -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