llvm.org GIT mirror llvm / 3b666f3
IR: Fix ConstantArray::replaceUsesOfWithOnConstant() 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 Duncan P. N. Exon Smith 6 years ago
2 changed file(s) with 31 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
27002700 return;
27012701 }
27022702
2703 // Check for any other type of constant-folding.
2704 if (Constant *C = getImpl(getType(), Values)) {
2705 replaceUsesOfWithOnConstantImpl(C);
2706 return;
2707 }
2708
27032709 // Check to see if we have this array type already.
27042710 LLVMContextImpl::ArrayConstantsTy::LookupKey Lookup(
27052711 cast(getType()), makeArrayRef(Values));
273273
274274 #undef CHECK
275275
276 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
277 LLVMContext Context;
278 std::unique_ptr M(new Module("MyModule", Context));
279
280 Type *IntTy = Type::getInt8Ty(Context);
281 ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
282 Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
283 ConstantInt::get(IntTy, 1)};
284 Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
285
286 Constant *Global = new GlobalVariable(*M, IntTy, false,
287 GlobalValue::ExternalLinkage, nullptr);
288 Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
289 Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
290 Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
291 ASSERT_NE(A01, A0G);
292
293 GlobalVariable *RefArray =
294 new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
295 ASSERT_EQ(A0G, RefArray->getInitializer());
296
297 GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
298 ASSERT_EQ(A01, RefArray->getInitializer());
299 }
300
276301 } // end anonymous namespace
277302 } // end namespace llvm