diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp index 64ebdb4cc7d7..5e4c8ecbd22c 100644 --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -929,12 +929,36 @@ bool Function::callsFunctionThatReturnsTwice() const { return false; } +static Constant * +getFunctionData(const Function *F, + const LLVMContextImpl::FunctionDataMapTy &Map) { + const auto &Entry = Map.find(F); + assert(Entry != Map.end()); + return cast(Entry->second->getReturnValue()); +} + +/// setFunctionData - Set "Map[F] = Data". Return an updated SubclassData value +/// in which Bit is low iff Data is null. +static unsigned setFunctionData(Function *F, + LLVMContextImpl::FunctionDataMapTy &Map, + Constant *Data, unsigned SCData, unsigned Bit) { + ReturnInst *&Holder = Map[F]; + if (Data) { + if (Holder) + Holder->setOperand(0, Data); + else + Holder = ReturnInst::Create(F->getContext(), Data); + return SCData | (1 << Bit); + } else { + delete Holder; + Map.erase(F); + return SCData & ~(1 << Bit); + } +} + Constant *Function::getPrefixData() const { assert(hasPrefixData()); - const LLVMContextImpl::PrefixDataMapTy &PDMap = - getContext().pImpl->PrefixDataMap; - assert(PDMap.find(this) != PDMap.end()); - return cast(PDMap.find(this)->second->getReturnValue()); + return getFunctionData(this, getContext().pImpl->PrefixDataMap); } void Function::setPrefixData(Constant *PrefixData) { @@ -942,49 +966,24 @@ void Function::setPrefixData(Constant *PrefixData) { return; unsigned SCData = getSubclassDataFromValue(); - LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap; - ReturnInst *&PDHolder = PDMap[this]; - if (PrefixData) { - if (PDHolder) - PDHolder->setOperand(0, PrefixData); - else - PDHolder = ReturnInst::Create(getContext(), PrefixData); - SCData |= (1<<1); - } else { - delete PDHolder; - PDMap.erase(this); - SCData &= ~(1<<1); - } + SCData = setFunctionData(this, getContext().pImpl->PrefixDataMap, PrefixData, + SCData, /*Bit=*/1); setValueSubclassData(SCData); } Constant *Function::getPrologueData() const { assert(hasPrologueData()); - const LLVMContextImpl::PrologueDataMapTy &SOMap = - getContext().pImpl->PrologueDataMap; - assert(SOMap.find(this) != SOMap.end()); - return cast(SOMap.find(this)->second->getReturnValue()); + return getFunctionData(this, getContext().pImpl->PrologueDataMap); } void Function::setPrologueData(Constant *PrologueData) { if (!PrologueData && !hasPrologueData()) return; - unsigned PDData = getSubclassDataFromValue(); - LLVMContextImpl::PrologueDataMapTy &PDMap = getContext().pImpl->PrologueDataMap; - ReturnInst *&PDHolder = PDMap[this]; - if (PrologueData) { - if (PDHolder) - PDHolder->setOperand(0, PrologueData); - else - PDHolder = ReturnInst::Create(getContext(), PrologueData); - PDData |= (1<<2); - } else { - delete PDHolder; - PDMap.erase(this); - PDData &= ~(1<<2); - } - setValueSubclassData(PDData); + unsigned SCData = getSubclassDataFromValue(); + SCData = setFunctionData(this, getContext().pImpl->PrologueDataMap, + PrologueData, SCData, /*Bit=*/2); + setValueSubclassData(SCData); } void Function::setEntryCount(uint64_t Count) { diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h index 1d410692b6aa..2df7d04708e0 100644 --- a/llvm/lib/IR/LLVMContextImpl.h +++ b/llvm/lib/IR/LLVMContextImpl.h @@ -972,16 +972,16 @@ public: /// instructions in different blocks at the same location. DenseMap, unsigned> DiscriminatorTable; + typedef DenseMap FunctionDataMapTy; + /// \brief Mapping from a function to its prefix data, which is stored as the /// operand of an unparented ReturnInst so that the prefix data has a Use. - typedef DenseMap PrefixDataMapTy; - PrefixDataMapTy PrefixDataMap; + FunctionDataMapTy PrefixDataMap; /// \brief Mapping from a function to its prologue data, which is stored as /// the operand of an unparented ReturnInst so that the prologue data has a /// Use. - typedef DenseMap PrologueDataMapTy; - PrologueDataMapTy PrologueDataMap; + FunctionDataMapTy PrologueDataMap; int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);