diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-08-19 02:21:00 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2014-08-19 02:21:00 +0000 |
commit | b11cd6fbc583be97371d9cc6d95eec70de074630 (patch) | |
tree | 8a7fcb6178b1eca90f2ee184285fd8c3db624883 /llvm | |
parent | d8c60542a49b020eac3dc182189e3d595b3ad3bd (diff) | |
download | bcm5719-llvm-b11cd6fbc583be97371d9cc6d95eec70de074630.tar.gz bcm5719-llvm-b11cd6fbc583be97371d9cc6d95eec70de074630.zip |
IR: Fix ConstantArray::replaceUsesOfWithOnConstant()
Previously, `ConstantArray::replaceUsesOfWithOnConstant()` neglected to
check whether it becomes a `ConstantDataArray`. Call
`ConstantArray::getImpl()` to check for that.
llvm-svn: 215965
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 6 | ||||
-rw-r--r-- | llvm/unittests/IR/ConstantsTest.cpp | 25 |
2 files changed, 31 insertions, 0 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index f0429ede820..c2c3c40df1c 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -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)); diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp index 0cd85499828..52f098e969e 100644 --- a/llvm/unittests/IR/ConstantsTest.cpp +++ b/llvm/unittests/IR/ConstantsTest.cpp @@ -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 |