[llvm] r216023 - IR: Fix a missed case when threading OnlyIfReduced through ConstantExpr

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Aug 19 14:18:21 PDT 2014


Author: dexonsmith
Date: Tue Aug 19 16:18:21 2014
New Revision: 216023

URL: http://llvm.org/viewvc/llvm-project?rev=216023&view=rev
Log:
IR: Fix a missed case when threading OnlyIfReduced through ConstantExpr

In r216015 I missed propagating `OnlyIfReduced` through the inline
versions of `getGetElementPtr()` (I was relying on compile failures on
mismatches between the header and source signatures to get them all).

Modified:
    llvm/trunk/include/llvm/IR/Constants.h
    llvm/trunk/unittests/IR/ConstantsTest.cpp

Modified: llvm/trunk/include/llvm/IR/Constants.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Constants.h?rev=216023&r1=216022&r2=216023&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Constants.h (original)
+++ llvm/trunk/include/llvm/IR/Constants.h Tue Aug 19 16:18:21 2014
@@ -1036,9 +1036,9 @@ public:
   static Constant *getGetElementPtr(Constant *C, ArrayRef<Constant *> IdxList,
                                     bool InBounds = false,
                                     Type *OnlyIfReducedTy = nullptr) {
-    return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
-                                            IdxList.size()),
-                            InBounds);
+    return getGetElementPtr(
+        C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
+        InBounds, OnlyIfReducedTy);
   }
   static Constant *getGetElementPtr(Constant *C, Constant *Idx,
                                     bool InBounds = false,
@@ -1046,7 +1046,7 @@ public:
     // This form of the function only exists to avoid ambiguous overload
     // warnings about whether to convert Idx to ArrayRef<Constant *> or
     // ArrayRef<Value *>.
-    return getGetElementPtr(C, cast<Value>(Idx), InBounds);
+    return getGetElementPtr(C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
   }
   static Constant *getGetElementPtr(Constant *C, ArrayRef<Value *> IdxList,
                                     bool InBounds = false,

Modified: llvm/trunk/unittests/IR/ConstantsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ConstantsTest.cpp?rev=216023&r1=216022&r2=216023&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ConstantsTest.cpp (original)
+++ llvm/trunk/unittests/IR/ConstantsTest.cpp Tue Aug 19 16:18:21 2014
@@ -322,5 +322,30 @@ TEST(ConstantsTest, ConstantExprReplaceW
   ASSERT_EQ(Int2, Ref->getInitializer());
 }
 
+TEST(ConstantsTest, GEPReplaceWithConstant) {
+  LLVMContext Context;
+  std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+  Type *IntTy = Type::getInt32Ty(Context);
+  Type *PtrTy = PointerType::get(IntTy, 0);
+  auto *C1 = ConstantInt::get(IntTy, 1);
+  auto *Placeholder = new GlobalVariable(
+      *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
+  auto *GEP = ConstantExpr::getGetElementPtr(Placeholder, C1);
+  ASSERT_EQ(GEP->getOperand(0), Placeholder);
+
+  auto *Ref =
+      new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
+  ASSERT_EQ(GEP, Ref->getInitializer());
+
+  auto *Global = new GlobalVariable(*M, PtrTy, false,
+                                    GlobalValue::ExternalLinkage, nullptr);
+  auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
+                                    "alias", Global, M.get());
+  Placeholder->replaceAllUsesWith(Alias);
+  ASSERT_EQ(GEP, Ref->getInitializer());
+  ASSERT_EQ(GEP->getOperand(0), Alias);
+}
+
 }  // end anonymous namespace
 }  // end namespace llvm





More information about the llvm-commits mailing list