aboutsummaryrefslogtreecommitdiff
path: root/lib/IR
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2015-12-19 08:52:49 +0000
committerVedant Kumar <vsk@apple.com>2015-12-19 08:52:49 +0000
commitef112d46b98642b59c657f1560f7e1c21b13a0da (patch)
treef929d6c04f77fecadf40f7f39f76de330fcbad21 /lib/IR
parentd3d7d026d3fdf198e8970a522aec11e5e2a5d683 (diff)
Re-reapply "[IR] Move optional data in llvm::Function into a hungoff uselist"
Make personality functions, prefix data, and prologue data hungoff operands of Function. This is based on the email thread "[RFC] Clean up the way we store optional Function data" on llvm-dev. Thanks to sanjoyd, majnemer, rnk, loladiro, and dexonsmith for feedback! Includes a fix to scrub value subclass data in dropAllReferences. Does not use binary literals. Differential Revision: http://reviews.llvm.org/D13829 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256095 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/IR')
-rw-r--r--lib/IR/AsmWriter.cpp18
-rw-r--r--lib/IR/Function.cpp131
-rw-r--r--lib/IR/LLVMContextImpl.h11
-rw-r--r--lib/IR/TypeFinder.cpp10
4 files changed, 62 insertions, 108 deletions
diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp
index 51a0faee81c..124fc56e78f 100644
--- a/lib/IR/AsmWriter.cpp
+++ b/lib/IR/AsmWriter.cpp
@@ -103,17 +103,9 @@ static OrderMap orderModule(const Module *M) {
orderValue(&A, OM);
}
for (const Function &F : *M) {
- if (F.hasPrefixData())
- if (!isa<GlobalValue>(F.getPrefixData()))
- orderValue(F.getPrefixData(), OM);
-
- if (F.hasPrologueData())
- if (!isa<GlobalValue>(F.getPrologueData()))
- orderValue(F.getPrologueData(), OM);
-
- if (F.hasPersonalityFn())
- if (!isa<GlobalValue>(F.getPersonalityFn()))
- orderValue(F.getPersonalityFn(), OM);
+ for (const Use &U : F.operands())
+ if (!isa<GlobalValue>(U.get()))
+ orderValue(U.get(), OM);
orderValue(&F, OM);
@@ -263,8 +255,8 @@ static UseListOrderStack predictUseListOrder(const Module *M) {
for (const GlobalAlias &A : M->aliases())
predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
for (const Function &F : *M)
- if (F.hasPrefixData())
- predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
+ for (const Use &U : F.operands())
+ predictValueUseListOrder(U.get(), nullptr, OM, Stack);
return Stack;
}
diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp
index 5e4d2d2054e..f5974a63f45 100644
--- a/lib/IR/Function.cpp
+++ b/lib/IR/Function.cpp
@@ -279,9 +279,6 @@ Function::~Function() {
// Remove the function from the on-the-side GC table.
clearGC();
-
- // FIXME: needed by operator delete
- setFunctionNumOperands(1);
}
void Function::BuildLazyArguments() const {
@@ -328,14 +325,15 @@ void Function::dropAllReferences() {
while (!BasicBlocks.empty())
BasicBlocks.begin()->eraseFromParent();
- // Prefix and prologue data are stored in a side table.
- setPrefixData(nullptr);
- setPrologueData(nullptr);
+ // Drop uses of any optional data (real or placeholder).
+ if (getNumOperands()) {
+ User::dropAllReferences();
+ setNumHungOffUseOperands(0);
+ setValueSubclassData(getSubclassDataFromValue() & ~0xe);
+ }
// Metadata is stored in a side-table.
clearMetadata();
-
- setPersonalityFn(nullptr);
}
void Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
@@ -425,18 +423,12 @@ void Function::copyAttributesFrom(const GlobalValue *Src) {
setGC(SrcF->getGC());
else
clearGC();
+ if (SrcF->hasPersonalityFn())
+ setPersonalityFn(SrcF->getPersonalityFn());
if (SrcF->hasPrefixData())
setPrefixData(SrcF->getPrefixData());
- else
- setPrefixData(nullptr);
if (SrcF->hasPrologueData())
setPrologueData(SrcF->getPrologueData());
- else
- setPrologueData(nullptr);
- if (SrcF->hasPersonalityFn())
- setPersonalityFn(SrcF->getPersonalityFn());
- else
- setPersonalityFn(nullptr);
}
/// \brief This does the actual lookup of an intrinsic ID which
@@ -944,61 +936,67 @@ 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<Constant>(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::getPersonalityFn() const {
+ assert(hasPersonalityFn() && getNumOperands());
+ return cast<Constant>(Op<0>());
+}
+
+void Function::setPersonalityFn(Constant *Fn) {
+ if (Fn)
+ setHungoffOperand<0>(Fn);
+ setValueSubclassDataBit(3, Fn != nullptr);
}
Constant *Function::getPrefixData() const {
- assert(hasPrefixData());
- return getFunctionData(this, getContext().pImpl->PrefixDataMap);
+ assert(hasPrefixData() && getNumOperands());
+ return cast<Constant>(Op<1>());
}
void Function::setPrefixData(Constant *PrefixData) {
- if (!PrefixData && !hasPrefixData())
- return;
-
- unsigned SCData = getSubclassDataFromValue();
- SCData = setFunctionData(this, getContext().pImpl->PrefixDataMap, PrefixData,
- SCData, /*Bit=*/1);
- setValueSubclassData(SCData);
+ if (PrefixData)
+ setHungoffOperand<1>(PrefixData);
+ setValueSubclassDataBit(1, PrefixData != nullptr);
}
Constant *Function::getPrologueData() const {
- assert(hasPrologueData());
- return getFunctionData(this, getContext().pImpl->PrologueDataMap);
+ assert(hasPrologueData() && getNumOperands());
+ return cast<Constant>(Op<2>());
}
void Function::setPrologueData(Constant *PrologueData) {
- if (!PrologueData && !hasPrologueData())
+ if (PrologueData)
+ setHungoffOperand<2>(PrologueData);
+ setValueSubclassDataBit(2, PrologueData != nullptr);
+}
+
+void Function::allocHungoffUselist() {
+ // If we've already allocated a uselist, stop here.
+ if (getNumOperands())
return;
- unsigned SCData = getSubclassDataFromValue();
- SCData = setFunctionData(this, getContext().pImpl->PrologueDataMap,
- PrologueData, SCData, /*Bit=*/2);
- setValueSubclassData(SCData);
+ allocHungoffUses(3, /*IsPhi=*/ false);
+ setNumHungOffUseOperands(3);
+
+ // Initialize the uselist with placeholder operands to allow traversal.
+ auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
+ Op<0>().set(CPN);
+ Op<1>().set(CPN);
+ Op<2>().set(CPN);
+}
+
+template <int Idx>
+void Function::setHungoffOperand(Constant *C) {
+ assert(C && "Cannot set hungoff operand to nullptr");
+ allocHungoffUselist();
+ Op<Idx>().set(C);
+}
+
+void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
+ assert(Bit < 16 && "SubclassData contains only 16 bits");
+ if (On)
+ setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
+ else
+ setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
}
void Function::setEntryCount(uint64_t Count) {
@@ -1016,22 +1014,3 @@ Optional<uint64_t> Function::getEntryCount() const {
}
return None;
}
-
-void Function::setPersonalityFn(Constant *C) {
- if (!C) {
- if (hasPersonalityFn()) {
- // Note, the num operands is used to compute the offset of the operand, so
- // the order here matters. Clearing the operand then clearing the num
- // operands ensures we have the correct offset to the operand.
- Op<0>().set(nullptr);
- setFunctionNumOperands(0);
- }
- } else {
- // Note, the num operands is used to compute the offset of the operand, so
- // the order here matters. We need to set num operands to 1 first so that
- // we get the correct offset to the first operand when we set it.
- if (!hasPersonalityFn())
- setFunctionNumOperands(1);
- Op<0>().set(C);
- }
-}
diff --git a/lib/IR/LLVMContextImpl.h b/lib/IR/LLVMContextImpl.h
index ae987e65bcb..a24114d0a0a 100644
--- a/lib/IR/LLVMContextImpl.h
+++ b/lib/IR/LLVMContextImpl.h
@@ -1014,17 +1014,6 @@ public:
/// instructions in different blocks at the same location.
DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
- typedef DenseMap<const Function *, ReturnInst *> 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.
- 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.
- FunctionDataMapTy PrologueDataMap;
-
int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
diff --git a/lib/IR/TypeFinder.cpp b/lib/IR/TypeFinder.cpp
index 2ea0550ba45..b5bdab0865b 100644
--- a/lib/IR/TypeFinder.cpp
+++ b/lib/IR/TypeFinder.cpp
@@ -44,14 +44,8 @@ void TypeFinder::run(const Module &M, bool onlyNamed) {
for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
incorporateType(FI->getType());
- if (FI->hasPrefixData())
- incorporateValue(FI->getPrefixData());
-
- if (FI->hasPrologueData())
- incorporateValue(FI->getPrologueData());
-
- if (FI->hasPersonalityFn())
- incorporateValue(FI->getPersonalityFn());
+ for (const Use &U : FI->operands())
+ incorporateValue(U.get());
// First incorporate the arguments.
for (Function::const_arg_iterator AI = FI->arg_begin(),