From 3c89256d71658651735544ef18362b0fc961d9b3 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Tue, 28 Apr 2020 12:06:48 -0700 Subject: [PATCH] Attributor::ArgumentReplacementMap: Use unique_ptr to simplify memory management --- llvm/include/llvm/Transforms/IPO/Attributor.h | 2 +- llvm/lib/Transforms/IPO/Attributor.cpp | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index 8763b9f..18a0b0c 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -1246,7 +1246,7 @@ private: ///} /// Map to remember all requested signature changes (= argument replacements). - DenseMap> + DenseMap, 8>> ArgumentReplacementMap; /// The set of functions we are deriving attributes for. diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index ba08061..aafeb6e 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -493,9 +493,6 @@ Attributor::~Attributor() { // the destructor manually. for (auto &It : QueryMap) It.getSecond()->~QueryMapValueTy(); - - for (auto &It : ArgumentReplacementMap) - DeleteContainerPointers(It.second); } bool Attributor::isAssumedDead(const AbstractAttribute &AA, @@ -1390,13 +1387,14 @@ bool Attributor::registerFunctionSignatureRewrite( "Cannot register an invalid rewrite"); Function *Fn = Arg.getParent(); - SmallVectorImpl &ARIs = ArgumentReplacementMap[Fn]; + SmallVectorImpl> &ARIs = + ArgumentReplacementMap[Fn]; if (ARIs.empty()) ARIs.resize(Fn->arg_size()); // If we have a replacement already with less than or equal new arguments, // ignore this request. - ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()]; + std::unique_ptr &ARI = ARIs[Arg.getArgNo()]; if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) { LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n"); return false; @@ -1404,17 +1402,16 @@ bool Attributor::registerFunctionSignatureRewrite( // If we have a replacement already but we like the new one better, delete // the old. - if (ARI) - delete ARI; + ARI.reset(); LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in " << Arg.getParent()->getName() << " with " << ReplacementTypes.size() << " replacements\n"); // Remember the replacement. - ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, - std::move(CalleeRepairCB), - std::move(ACSRepairCB)); + ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes, + std::move(CalleeRepairCB), + std::move(ACSRepairCB))); return true; } @@ -1430,7 +1427,7 @@ ChangeStatus Attributor::rewriteFunctionSignatures( if (ToBeDeletedFunctions.count(OldFn)) continue; - const SmallVectorImpl &ARIs = It.getSecond(); + const SmallVectorImpl> &ARIs = It.getSecond(); assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!"); SmallVector NewArgumentTypes; @@ -1439,7 +1436,7 @@ ChangeStatus Attributor::rewriteFunctionSignatures( // Collect replacement argument types and copy over existing attributes. AttributeList OldFnAttributeList = OldFn->getAttributes(); for (Argument &Arg : OldFn->args()) { - if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) { + if (const std::unique_ptr &ARI = ARIs[Arg.getArgNo()]) { NewArgumentTypes.append(ARI->ReplacementTypes.begin(), ARI->ReplacementTypes.end()); NewArgumentAttributes.append(ARI->getNumReplacementArgs(), @@ -1501,7 +1498,7 @@ ChangeStatus Attributor::rewriteFunctionSignatures( for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) { unsigned NewFirstArgNum = NewArgOperands.size(); (void)NewFirstArgNum; // only used inside assert. - if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { + if (const std::unique_ptr &ARI = ARIs[OldArgNum]) { if (ARI->ACSRepairCB) ARI->ACSRepairCB(*ARI, ACS, NewArgOperands); assert(ARI->getNumReplacementArgs() + NewFirstArgNum == @@ -1566,7 +1563,8 @@ ChangeStatus Attributor::rewriteFunctionSignatures( auto NewFnArgIt = NewFn->arg_begin(); for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum, ++OldFnArgIt) { - if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) { + if (const std::unique_ptr &ARI = + ARIs[OldArgNum]) { if (ARI->CalleeRepairCB) ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt); NewFnArgIt += ARI->ReplacementTypes.size(); -- 2.7.4