- 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);
+ if (PrologueData)
+ setHungoffOperand<2>(PrologueData);
+ setValueSubclassDataBit(2, PrologueData != nullptr);
+}
+
+void Function::allocHungoffUselist() {
+ // If we've already allocated a uselist, stop here.
+ if (getNumOperands())
+ return;
+
+ 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) {
+ MDBuilder MDB(getContext());
+ setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count));
+}
+
+Optional<uint64_t> Function::getEntryCount() const {
+ MDNode *MD = getMetadata(LLVMContext::MD_prof);
+ if (MD && MD->getOperand(0))
+ if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
+ if (MDS->getString().equals("function_entry_count")) {
+ ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
+ return CI->getValue().getZExtValue();
+ }
+ return None;