summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2012-05-26 13:59:43 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2012-05-26 13:59:43 +0000
commit937338cf64350a7d05d0c956dc8e8564e959cb7b (patch)
tree986770448d7197905604cb67a67e36ca61b6cbda
parent9632f77f8dde9d2c9c9cb9d022b846a82c918cbd (diff)
downloadllvm-937338cf64350a7d05d0c956dc8e8564e959cb7b.tar.gz
llvm-937338cf64350a7d05d0c956dc8e8564e959cb7b.tar.bz2
llvm-937338cf64350a7d05d0c956dc8e8564e959cb7b.tar.xz
Add support for branch weight metadata to MDBuilder and use it in various places.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157515 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Support/MDBuilder.h23
-rw-r--r--lib/Transforms/Utils/LowerExpectIntrinsic.cpp44
-rw-r--r--lib/Transforms/Utils/SimplifyCFG.cpp11
3 files changed, 46 insertions, 32 deletions
diff --git a/include/llvm/Support/MDBuilder.h b/include/llvm/Support/MDBuilder.h
index 40f028a432..855e58d699 100644
--- a/include/llvm/Support/MDBuilder.h
+++ b/include/llvm/Support/MDBuilder.h
@@ -49,6 +49,29 @@ namespace llvm {
return MDNode::get(Context, Op);
}
+ //===------------------------------------------------------------------===//
+ // Prof metadata.
+ //===------------------------------------------------------------------===//
+
+ /// \brief Return metadata containing two branch weights.
+ MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight) {
+ uint32_t Weights[] = { TrueWeight, FalseWeight };
+ return createBranchWeights(Weights);
+ }
+
+ /// \brief Return metadata containing a number of branch weights.
+ MDNode *createBranchWeights(ArrayRef<uint32_t> Weights) {
+ assert(Weights.size() >= 2 && "Need at least two branch weights!");
+
+ SmallVector<Value *, 4> Vals(Weights.size()+1);
+ Vals[0] = createString("branch_weights");
+
+ Type *Int32Ty = Type::getInt32Ty(Context);
+ for (unsigned i = 0, e = Weights.size(); i != e; ++i)
+ Vals[i+1] = ConstantInt::get(Int32Ty, Weights[i]);
+
+ return MDNode::get(Context, Vals);
+ }
//===------------------------------------------------------------------===//
// Range metadata.
diff --git a/lib/Transforms/Utils/LowerExpectIntrinsic.cpp b/lib/Transforms/Utils/LowerExpectIntrinsic.cpp
index c70ced18e4..f96581393d 100644
--- a/lib/Transforms/Utils/LowerExpectIntrinsic.cpp
+++ b/lib/Transforms/Utils/LowerExpectIntrinsic.cpp
@@ -23,6 +23,7 @@
#include "llvm/Transforms/Scalar.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/MDBuilder.h"
#include "llvm/ADT/Statistic.h"
#include <vector>
@@ -70,24 +71,18 @@ bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
if (!ExpectedValue)
return false;
- LLVMContext &Context = CI->getContext();
- Type *Int32Ty = Type::getInt32Ty(Context);
-
SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
- std::vector<Value *> Vec;
- unsigned n = SI->getNumCases();
- Vec.resize(n + 1 + 1); // +1 for MDString and +1 for default case
-
- Vec[0] = MDString::get(Context, "branch_weights");
- Vec[1] = ConstantInt::get(Int32Ty, Case == SI->case_default() ?
- LikelyBranchWeight : UnlikelyBranchWeight);
- for (unsigned i = 0; i < n; ++i) {
- Vec[i + 1 + 1] = ConstantInt::get(Int32Ty, i == Case.getCaseIndex() ?
- LikelyBranchWeight : UnlikelyBranchWeight);
- }
+ unsigned n = SI->getNumCases(); // +1 for default case.
+ std::vector<uint32_t> Weights(n + 1);
- MDNode *WeightsNode = llvm::MDNode::get(Context, Vec);
- SI->setMetadata(LLVMContext::MD_prof, WeightsNode);
+ Weights[0] = Case == SI->case_default() ? LikelyBranchWeight
+ : UnlikelyBranchWeight;
+ for (unsigned i = 0; i != n; ++i)
+ Weights[i + 1] = i == Case.getCaseIndex() ? LikelyBranchWeight
+ : UnlikelyBranchWeight;
+
+ SI->setMetadata(LLVMContext::MD_prof,
+ MDBuilder(CI->getContext()).createBranchWeights(Weights));
SI->setCondition(ArgValue);
return true;
@@ -120,20 +115,17 @@ bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
if (!ExpectedValue)
return false;
- LLVMContext &Context = CI->getContext();
- Type *Int32Ty = Type::getInt32Ty(Context);
- bool Likely = ExpectedValue->isOne();
+ MDBuilder MDB(CI->getContext());
+ MDNode *Node;
// If expect value is equal to 1 it means that we are more likely to take
// branch 0, in other case more likely is branch 1.
- Value *Ops[] = {
- MDString::get(Context, "branch_weights"),
- ConstantInt::get(Int32Ty, Likely ? LikelyBranchWeight : UnlikelyBranchWeight),
- ConstantInt::get(Int32Ty, Likely ? UnlikelyBranchWeight : LikelyBranchWeight)
- };
+ if (ExpectedValue->isOne())
+ Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
+ else
+ Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
- MDNode *WeightsNode = MDNode::get(Context, Ops);
- BI->setMetadata(LLVMContext::MD_prof, WeightsNode);
+ BI->setMetadata(LLVMContext::MD_prof, Node);
CmpI->setOperand(0, ArgValue);
return true;
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp
index 66dd2c954e..9b04ef29f4 100644
--- a/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -37,6 +37,7 @@
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/IRBuilder.h"
+#include "llvm/Support/MDBuilder.h"
#include "llvm/Support/NoFolder.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
@@ -1740,12 +1741,10 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
ProbTrue = ProbTrue.udiv(GCD);
ProbFalse = ProbFalse.udiv(GCD);
- LLVMContext &Context = BI->getContext();
- Value *Ops[3];
- Ops[0] = BI->getMetadata(LLVMContext::MD_prof)->getOperand(0);
- Ops[1] = ConstantInt::get(Context, ProbTrue);
- Ops[2] = ConstantInt::get(Context, ProbFalse);
- PBI->setMetadata(LLVMContext::MD_prof, MDNode::get(Context, Ops));
+ MDBuilder MDB(BI->getContext());
+ MDNode *N = MDB.createBranchWeights(ProbTrue.getZExtValue(),
+ ProbFalse.getZExtValue());
+ PBI->setMetadata(LLVMContext::MD_prof, N);
} else {
PBI->setMetadata(LLVMContext::MD_prof, NULL);
}