From: Vedant Kumar Date: Tue, 6 Oct 2015 20:31:57 +0000 (+0000) Subject: [Function] Clean up {prefix,prologue} data routines (NFC) X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=30c5e43964388ab63d45b6a94556cbafafb01c57;ds=sidebyside [Function] Clean up {prefix,prologue} data routines (NFC) Factor out some common code used to get+set function prefix/prologue data. This may come in handy if we ever decide to store personality functions in the same way we store prefix/prologue data. Differential Revision: http://reviews.llvm.org/D13120 Reviewed-by: bogner git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249460 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp index 64ebdb4cc7d..5e4c8ecbd22 100644 --- a/lib/IR/Function.cpp +++ b/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/lib/IR/LLVMContextImpl.h b/lib/IR/LLVMContextImpl.h index 1d410692b6a..2df7d04708e 100644 --- a/lib/IR/LLVMContextImpl.h +++ b/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);