[llvm] r215803 - IR: Don't add inbounds to GEPs of extern_weak variables

Duncan P. N. Exon Smith dexonsmith at apple.com
Fri Aug 15 18:54:32 PDT 2014


Author: dexonsmith
Date: Fri Aug 15 20:54:32 2014
New Revision: 215803

URL: http://llvm.org/viewvc/llvm-project?rev=215803&view=rev
Log:
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.

Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Assembler/ConstantExprNoFold.ll

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=215803&r1=215802&r2=215803&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Fri Aug 15 20:54:32 2014
@@ -2144,9 +2144,10 @@ static Constant *ConstantFoldGetElementP
 
   // 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;
 }

Modified: llvm/trunk/test/Assembler/ConstantExprNoFold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprNoFold.ll?rev=215803&r1=215802&r2=215803&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprNoFold.ll (original)
+++ llvm/trunk/test/Assembler/ConstantExprNoFold.ll Fri Aug 15 20:54:32 2014
@@ -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)
+ at weak.gep = global i32* getelementptr (i32* @weak, i32 1)
+ at 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)
+ at glob = global i32 0
+ at glob.a3 = alias getelementptr (i32* @glob.a2, i32 1)
+ at glob.a2 = alias getelementptr (i32* @glob.a1, i32 1)
+ at glob.a1 = alias i32* @glob





More information about the llvm-commits mailing list