IR: Fix ConstantArray::replaceUsesOfWithOnConstant()
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 19 Aug 2014 02:21:00 +0000 (02:21 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 19 Aug 2014 02:21:00 +0000 (02:21 +0000)
Previously, `ConstantArray::replaceUsesOfWithOnConstant()` neglected to
check whether it becomes a `ConstantDataArray`.  Call
`ConstantArray::getImpl()` to check for that.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215965 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/Constants.cpp
unittests/IR/ConstantsTest.cpp

index f0429ede8206339aba7a3cd280c87793e0a22f2f..c2c3c40df1c4dba6604da278ad8c395f0ff8bb4e 100644 (file)
@@ -2701,6 +2701,12 @@ void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
     return;
   }
 
+  // Check for any other type of constant-folding.
+  if (Constant *C = getImpl(getType(), Values)) {
+    replaceUsesOfWithOnConstantImpl(C);
+    return;
+  }
+
   // Check to see if we have this array type already.
   LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup(
     cast<ArrayType>(getType()), makeArrayRef(Values));
index 0cd854998286a529a343d3091d0a22814e796861..52f098e969e06a2d7b1ea37da675113150fa2ccf 100644 (file)
@@ -274,5 +274,30 @@ TEST(ConstantsTest, ReplaceWithConstantTest) {
 
 #undef CHECK
 
+TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
+  LLVMContext Context;
+  std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+  Type *IntTy = Type::getInt8Ty(Context);
+  ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
+  Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
+                          ConstantInt::get(IntTy, 1)};
+  Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
+
+  Constant *Global = new GlobalVariable(*M, IntTy, false,
+                                        GlobalValue::ExternalLinkage, nullptr);
+  Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
+  Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
+  Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
+  ASSERT_NE(A01, A0G);
+
+  GlobalVariable *RefArray =
+      new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
+  ASSERT_EQ(A0G, RefArray->getInitializer());
+
+  GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
+  ASSERT_EQ(A01, RefArray->getInitializer());
+}
+
 }  // end anonymous namespace
 }  // end namespace llvm