summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/IR/Function.cpp19
-rw-r--r--llvm/lib/IR/MDBuilder.cpp10
-rw-r--r--llvm/lib/IR/Verifier.cpp33
3 files changed, 62 insertions, 0 deletions
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index f312f711c60..a5d4d1c4b1e 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -19,10 +19,13 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/IR/CallSite.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/RWMutex.h"
@@ -986,3 +989,19 @@ void llvm::overrideFunctionAttribute(StringRef Kind, StringRef Value,
Attrs = Attrs.addAttribute(Ctx, AttributeSet::FunctionIndex, Kind, Value);
F.setAttributes(Attrs);
}
+
+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;
+}
diff --git a/llvm/lib/IR/MDBuilder.cpp b/llvm/lib/IR/MDBuilder.cpp
index a9010114044..354592df4c9 100644
--- a/llvm/lib/IR/MDBuilder.cpp
+++ b/llvm/lib/IR/MDBuilder.cpp
@@ -53,6 +53,16 @@ MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
return MDNode::get(Context, Vals);
}
+MDNode *MDBuilder::createFunctionEntryCount(uint64_t Count) {
+ SmallVector<Metadata *, 2> Vals(2);
+ Vals[0] = createString("function_entry_count");
+
+ Type *Int64Ty = Type::getInt64Ty(Context);
+ Vals[1] = createConstant(ConstantInt::get(Int64Ty, Count));
+
+ return MDNode::get(Context, Vals);
+}
+
MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 003b6064cbe..4e863cb99b5 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -400,6 +400,8 @@ private:
bool isReturnValue, const Value *V);
void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
const Value *V);
+ void VerifyFunctionMetadata(
+ const SmallVector<std::pair<unsigned, MDNode *>, 4> MDs);
void VerifyConstantExprBitcastType(const ConstantExpr *CE);
void VerifyStatepoint(ImmutableCallSite CS);
@@ -1463,6 +1465,36 @@ void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
}
}
+void Verifier::VerifyFunctionMetadata(
+ const SmallVector<std::pair<unsigned, MDNode *>, 4> MDs) {
+ if (MDs.empty())
+ return;
+
+ for (unsigned i = 0; i < MDs.size(); i++) {
+ if (MDs[i].first == LLVMContext::MD_prof) {
+ MDNode *MD = MDs[i].second;
+ Assert(MD->getNumOperands() == 2,
+ "!prof annotations should have exactly 2 operands", MD);
+
+ // Check first operand.
+ Assert(MD->getOperand(0) != nullptr, "first operand should not be null",
+ MD);
+ Assert(isa<MDString>(MD->getOperand(0)),
+ "expected string with name of the !prof annotation", MD);
+ MDString *MDS = cast<MDString>(MD->getOperand(0));
+ StringRef ProfName = MDS->getString();
+ Assert(ProfName.equals("function_entry_count"),
+ "first operand should be 'function_entry_count'", MD);
+
+ // Check second operand.
+ Assert(MD->getOperand(1) != nullptr, "second operand should not be null",
+ MD);
+ Assert(isa<ConstantAsMetadata>(MD->getOperand(1)),
+ "expected integer argument to function_entry_count", MD);
+ }
+ }
+}
+
void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) {
if (CE->getOpcode() != Instruction::BitCast)
return;
@@ -1713,6 +1745,7 @@ void Verifier::visitFunction(const Function &F) {
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
F.getAllMetadata(MDs);
assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
+ VerifyFunctionMetadata(MDs);
if (F.isMaterializable()) {
// Function has a body somewhere we can't see.
OpenPOWER on IntegriCloud