Fix dynamic alloca detection in CloneBasicBlock

Summary:
Simply check AI->isStaticAlloca instead of reimplementing checks for
static/dynamic allocas.

Reviewers: efriedma

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D82328
This commit is contained in:
Arthur Eubanks 2020-06-22 13:22:16 -07:00
parent 516803dc86
commit d335c1317b
2 changed files with 62 additions and 6 deletions

View File

@ -46,7 +46,7 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
if (BB->hasName())
NewBB->setName(BB->getName() + NameSuffix);
bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
bool hasCalls = false, hasDynamicAllocas = false;
Module *TheModule = F ? F->getParent() : nullptr;
// Loop over all instructions, and copy them over.
@ -62,18 +62,15 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
hasCalls |= (isa<CallInst>(I) && !isa<DbgInfoIntrinsic>(I));
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
if (isa<ConstantInt>(AI->getArraySize()))
hasStaticAllocas = true;
else
if (!AI->isStaticAlloca()) {
hasDynamicAllocas = true;
}
}
}
if (CodeInfo) {
CodeInfo->ContainsCalls |= hasCalls;
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
BB != &BB->getParent()->getEntryBlock();
}
return NewBB;
}

View File

@ -659,6 +659,65 @@ static int GetDICompileUnitCount(const Module& M) {
return 0;
}
TEST(CloneFunction, CloneEmptyFunction) {
StringRef ImplAssembly = R"(
define void @foo() {
ret void
}
declare void @bar()
)";
LLVMContext Context;
SMDiagnostic Error;
auto ImplModule = parseAssemblyString(ImplAssembly, Error, Context);
EXPECT_TRUE(ImplModule != nullptr);
auto *ImplFunction = ImplModule->getFunction("foo");
EXPECT_TRUE(ImplFunction != nullptr);
auto *DeclFunction = ImplModule->getFunction("bar");
EXPECT_TRUE(DeclFunction != nullptr);
ValueToValueMapTy VMap;
SmallVector<ReturnInst *, 8> Returns;
ClonedCodeInfo CCI;
CloneFunctionInto(DeclFunction, ImplFunction, VMap, true, Returns, "", &CCI);
EXPECT_FALSE(verifyModule(*ImplModule, &errs()));
EXPECT_FALSE(CCI.ContainsCalls);
EXPECT_FALSE(CCI.ContainsDynamicAllocas);
}
TEST(CloneFunction, CloneFunctionWithInalloca) {
StringRef ImplAssembly = R"(
declare void @a(i32* inalloca)
define void @foo() {
%a = alloca inalloca i32
call void @a(i32* inalloca %a)
ret void
}
declare void @bar()
)";
LLVMContext Context;
SMDiagnostic Error;
auto ImplModule = parseAssemblyString(ImplAssembly, Error, Context);
EXPECT_TRUE(ImplModule != nullptr);
auto *ImplFunction = ImplModule->getFunction("foo");
EXPECT_TRUE(ImplFunction != nullptr);
auto *DeclFunction = ImplModule->getFunction("bar");
EXPECT_TRUE(DeclFunction != nullptr);
ValueToValueMapTy VMap;
SmallVector<ReturnInst *, 8> Returns;
ClonedCodeInfo CCI;
CloneFunctionInto(DeclFunction, ImplFunction, VMap, true, Returns, "", &CCI);
EXPECT_FALSE(verifyModule(*ImplModule, &errs()));
EXPECT_TRUE(CCI.ContainsCalls);
EXPECT_TRUE(CCI.ContainsDynamicAllocas);
}
TEST(CloneFunction, CloneFunctionToDifferentModule) {
StringRef ImplAssembly = R"(
define void @foo() {