diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -116,6 +116,8 @@ void clearArguments(); + void deleteBodyImpl(bool ShouldDrop); + /// Function ctor - If the (optional) Module argument is specified, the /// function is automatically inserted into the end of the function list for /// the module. @@ -667,7 +669,7 @@ /// the linkage to external. /// void deleteBody() { - dropAllReferences(); + deleteBodyImpl(/*ShouldDrop=*/false); setLinkage(ExternalLinkage); } @@ -882,7 +884,9 @@ /// function, dropping all references deletes the entire body of the function, /// including any contained basic blocks. /// - void dropAllReferences(); + void dropAllReferences() { + deleteBodyImpl(/*ShouldDrop=*/true); + } /// hasAddressTaken - returns true if there are any uses of this function /// other than direct calls or invokes to it, or blockaddress expressions. diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -518,15 +518,7 @@ Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); } -// dropAllReferences() - This function causes all the subinstructions to "let -// go" of all references that they are maintaining. This allows one to -// 'delete' a whole class at a time, even though there may be circular -// references... first all references are dropped, and all use counts go to -// zero. Then everything is deleted for real. Note that no operations are -// valid on an object that has "dropped all references", except operator -// delete. -// -void Function::dropAllReferences() { +void Function::deleteBodyImpl(bool ShouldDrop) { setIsMaterializable(false); for (BasicBlock &BB : *this) @@ -537,10 +529,18 @@ while (!BasicBlocks.empty()) BasicBlocks.begin()->eraseFromParent(); - // Drop uses of any optional data (real or placeholder). if (getNumOperands()) { - User::dropAllReferences(); - setNumHungOffUseOperands(0); + if (ShouldDrop) { + // Drop uses of any optional data (real or placeholder). + User::dropAllReferences(); + setNumHungOffUseOperands(0); + } else { + // The code needs to match Function::allocHungoffUselist(). + auto *CPN = ConstantPointerNull::get(PointerType::get(getContext(), 0)); + Op<0>().set(CPN); + Op<1>().set(CPN); + Op<2>().set(CPN); + } setValueSubclassData(getSubclassDataFromValue() & ~0xe); }