aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/CloneFunction.cpp
diff options
context:
space:
mode:
authorMatt Arsenault <Matthew.Arsenault@amd.com>2023-05-23 10:28:57 +0100
committerMatt Arsenault <Matthew.Arsenault@amd.com>2023-06-02 18:14:28 -0400
commit1536e299e63d7788f38117b0212ca50eb76d7a3b (patch)
tree3e8a95e6e2798f366769d6b817b34cdbe678388d /llvm/lib/Transforms/Utils/CloneFunction.cpp
parent12d967c95f1633bebd1b225ddd53573951a7ca43 (diff)
downloadllvm-1536e299e63d7788f38117b0212ca50eb76d7a3b.zip
llvm-1536e299e63d7788f38117b0212ca50eb76d7a3b.tar.gz
llvm-1536e299e63d7788f38117b0212ca50eb76d7a3b.tar.bz2
InstSimplify: Require instruction be parented
Unlike every other analysis and transform, simplifyInstruction permitted operating on instructions which are not inserted into a function. This created an edge case no other code needs to really worry about, and limited transforms in cases that can make use of the context function. Only the inliner and a handful of other utilities were making use of this, so just fix up these edge cases. Results in some IR ordering differences since cloned blocks are inserted eagerly now. Plus some additional simplifications trigger (e.g. some add 0s now folded out that previously didn't).
Diffstat (limited to 'llvm/lib/Transforms/Utils/CloneFunction.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/CloneFunction.cpp15
1 files changed, 6 insertions, 9 deletions
diff --git a/llvm/lib/Transforms/Utils/CloneFunction.cpp b/llvm/lib/Transforms/Utils/CloneFunction.cpp
index 272970e..d552086 100644
--- a/llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ b/llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -470,9 +470,8 @@ void PruningFunctionCloner::CloneBlock(
// Nope, clone it now.
BasicBlock *NewBB;
- BBEntry = NewBB = BasicBlock::Create(BB->getContext());
- if (BB->hasName())
- NewBB->setName(BB->getName() + NameSuffix);
+ Twine NewName(BB->hasName() ? Twine(BB->getName()) + NameSuffix : "");
+ BBEntry = NewBB = BasicBlock::Create(BB->getContext(), NewName, NewFunc);
// It is only legal to clone a function if a block address within that
// function is never referenced outside of the function. Given that, we
@@ -498,6 +497,7 @@ void PruningFunctionCloner::CloneBlock(
++II) {
Instruction *NewInst = cloneInstruction(II);
+ NewInst->insertInto(NewBB, NewBB->end());
if (HostFuncIsStrictFP) {
// All function calls in the inlined function must get 'strictfp'
@@ -516,8 +516,6 @@ void PruningFunctionCloner::CloneBlock(
// If we can simplify this instruction to some other value, simply add
// a mapping to that value rather than inserting a new instruction into
// the basic block.
- //
- // FIXME: simplifyInstruction should know the context of the new function.
if (Value *V =
simplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
// On the off-chance that this simplifies to an instruction in the old
@@ -528,7 +526,7 @@ void PruningFunctionCloner::CloneBlock(
if (!NewInst->mayHaveSideEffects()) {
VMap[&*II] = V;
- NewInst->deleteValue();
+ NewInst->eraseFromParent();
continue;
}
}
@@ -537,7 +535,6 @@ void PruningFunctionCloner::CloneBlock(
if (II->hasName())
NewInst->setName(II->getName() + NameSuffix);
VMap[&*II] = NewInst; // Add instruction map to value.
- NewInst->insertInto(NewBB, NewBB->end());
if (isa<CallInst>(II) && !II->isDebugOrPseudoInst()) {
hasCalls = true;
hasMemProfMetadata |= II->hasMetadata(LLVMContext::MD_memprof);
@@ -685,8 +682,8 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
if (!NewBB)
continue; // Dead block.
- // Add the new block to the new function.
- NewFunc->insert(NewFunc->end(), NewBB);
+ // Move the new block to preserve the order in the original function.
+ NewBB->moveBefore(NewFunc->end());
// Handle PHI nodes specially, as we have to remove references to dead
// blocks.