1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 19:23:23 +01:00
Change lookup functions to const functions.

llvm-svn: 248818
This commit is contained in:
Dehao Chen 2015-09-29 18:28:15 +00:00
parent 5edbee3b7f
commit a883fbf7ee
2 changed files with 64 additions and 55 deletions

View File

@ -18,6 +18,7 @@
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <system_error> #include <system_error>
@ -188,16 +189,16 @@ public:
Num); Num);
} }
/// Return the sample record at the given location.
/// Each location is specified by \p LineOffset and \p Discriminator.
SampleRecord &sampleRecordAt(const LineLocation &Loc) {
return BodySamples[Loc];
}
/// Return the number of samples collected at the given location. /// Return the number of samples collected at the given location.
/// Each location is specified by \p LineOffset and \p Discriminator. /// Each location is specified by \p LineOffset and \p Discriminator.
unsigned samplesAt(int LineOffset, unsigned Discriminator) { /// If the location is not found in profile, return error.
return sampleRecordAt(LineLocation(LineOffset, Discriminator)).getSamples(); ErrorOr<unsigned> findSamplesAt(int LineOffset,
unsigned Discriminator) const {
const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
if (ret == BodySamples.end())
return std::error_code();
else
return ret->second.getSamples();
} }
bool empty() const { return BodySamples.empty(); } bool empty() const { return BodySamples.empty(); }
@ -219,7 +220,7 @@ public:
for (const auto &I : Other.getBodySamples()) { for (const auto &I : Other.getBodySamples()) {
const LineLocation &Loc = I.first; const LineLocation &Loc = I.first;
const SampleRecord &Rec = I.second; const SampleRecord &Rec = I.second;
sampleRecordAt(Loc).merge(Rec); BodySamples[Loc].merge(Rec);
} }
} }

View File

@ -43,6 +43,7 @@
#include "llvm/ProfileData/SampleProfReader.h" #include "llvm/ProfileData/SampleProfReader.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h" #include "llvm/Transforms/IPO.h"
#include <cctype> #include <cctype>
@ -63,11 +64,12 @@ static cl::opt<unsigned> SampleProfileMaxPropagateIterations(
"sample block/edge weights through the CFG.")); "sample block/edge weights through the CFG."));
namespace { namespace {
typedef DenseMap<BasicBlock *, unsigned> BlockWeightMap; typedef DenseMap<const BasicBlock *, unsigned> BlockWeightMap;
typedef DenseMap<BasicBlock *, BasicBlock *> EquivalenceClassMap; typedef DenseMap<const BasicBlock *, const BasicBlock *> EquivalenceClassMap;
typedef std::pair<BasicBlock *, BasicBlock *> Edge; typedef std::pair<const BasicBlock *, const BasicBlock *> Edge;
typedef DenseMap<Edge, unsigned> EdgeWeightMap; typedef DenseMap<Edge, unsigned> EdgeWeightMap;
typedef DenseMap<BasicBlock *, SmallVector<BasicBlock *, 8>> BlockEdgeMap; typedef DenseMap<const BasicBlock *, SmallVector<const BasicBlock *, 8>>
BlockEdgeMap;
/// \brief Sample profile pass. /// \brief Sample profile pass.
/// ///
@ -101,11 +103,11 @@ protected:
bool runOnFunction(Function &F); bool runOnFunction(Function &F);
unsigned getFunctionLoc(Function &F); unsigned getFunctionLoc(Function &F);
bool emitAnnotations(Function &F); bool emitAnnotations(Function &F);
unsigned getInstWeight(Instruction &I); ErrorOr<unsigned> getInstWeight(const Instruction &I) const;
unsigned getBlockWeight(BasicBlock *BB); ErrorOr<unsigned> getBlockWeight(const BasicBlock *BB) const;
void printEdgeWeight(raw_ostream &OS, Edge E); void printEdgeWeight(raw_ostream &OS, Edge E);
void printBlockWeight(raw_ostream &OS, BasicBlock *BB); void printBlockWeight(raw_ostream &OS, const BasicBlock *BB) const;
void printBlockEquivalence(raw_ostream &OS, BasicBlock *BB); void printBlockEquivalence(raw_ostream &OS, const BasicBlock *BB);
bool computeBlockWeights(Function &F); bool computeBlockWeights(Function &F);
void findEquivalenceClasses(Function &F); void findEquivalenceClasses(Function &F);
void findEquivalencesFor(BasicBlock *BB1, void findEquivalencesFor(BasicBlock *BB1,
@ -134,7 +136,7 @@ protected:
EdgeWeightMap EdgeWeights; EdgeWeightMap EdgeWeights;
/// \brief Set of visited blocks during propagation. /// \brief Set of visited blocks during propagation.
SmallPtrSet<BasicBlock *, 128> VisitedBlocks; SmallPtrSet<const BasicBlock *, 128> VisitedBlocks;
/// \brief Set of visited edges during propagation. /// \brief Set of visited edges during propagation.
SmallSet<Edge, 128> VisitedEdges; SmallSet<Edge, 128> VisitedEdges;
@ -186,8 +188,8 @@ void SampleProfileLoader::printEdgeWeight(raw_ostream &OS, Edge E) {
/// \param OS Stream to emit the output to. /// \param OS Stream to emit the output to.
/// \param BB Block to print. /// \param BB Block to print.
void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS, void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS,
BasicBlock *BB) { const BasicBlock *BB) {
BasicBlock *Equiv = EquivalenceClass[BB]; const BasicBlock *Equiv = EquivalenceClass[BB];
OS << "equivalence[" << BB->getName() OS << "equivalence[" << BB->getName()
<< "]: " << ((Equiv) ? EquivalenceClass[BB]->getName() : "NONE") << "\n"; << "]: " << ((Equiv) ? EquivalenceClass[BB]->getName() : "NONE") << "\n";
} }
@ -196,8 +198,11 @@ void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS,
/// ///
/// \param OS Stream to emit the output to. /// \param OS Stream to emit the output to.
/// \param BB Block to print. /// \param BB Block to print.
void SampleProfileLoader::printBlockWeight(raw_ostream &OS, BasicBlock *BB) { void SampleProfileLoader::printBlockWeight(raw_ostream &OS,
OS << "weight[" << BB->getName() << "]: " << BlockWeights[BB] << "\n"; const BasicBlock *BB) const {
const auto &I = BlockWeights.find(BB);
unsigned W = (I == BlockWeights.end() ? 0 : I->second);
OS << "weight[" << BB->getName() << "]: " << W << "\n";
} }
/// \brief Get the weight for an instruction. /// \brief Get the weight for an instruction.
@ -210,51 +215,51 @@ void SampleProfileLoader::printBlockWeight(raw_ostream &OS, BasicBlock *BB) {
/// ///
/// \param Inst Instruction to query. /// \param Inst Instruction to query.
/// ///
/// \returns The profiled weight of I. /// \returns the weight of \p Inst.
unsigned SampleProfileLoader::getInstWeight(Instruction &Inst) { ErrorOr<unsigned>
SampleProfileLoader::getInstWeight(const Instruction &Inst) const {
DebugLoc DLoc = Inst.getDebugLoc(); DebugLoc DLoc = Inst.getDebugLoc();
if (!DLoc) if (!DLoc)
return 0; return std::error_code();
unsigned Lineno = DLoc.getLine(); unsigned Lineno = DLoc.getLine();
if (Lineno < HeaderLineno) if (Lineno < HeaderLineno)
return 0; return std::error_code();
const DILocation *DIL = DLoc; const DILocation *DIL = DLoc;
int LOffset = Lineno - HeaderLineno; ErrorOr<unsigned> R =
unsigned Discriminator = DIL->getDiscriminator(); Samples->findSamplesAt(Lineno - HeaderLineno, DIL->getDiscriminator());
unsigned Weight = Samples->samplesAt(LOffset, Discriminator); if (R)
DEBUG(dbgs() << " " << Lineno << "." << Discriminator << ":" << Inst DEBUG(dbgs() << " " << Lineno << "." << DIL->getDiscriminator() << ":"
<< " (line offset: " << LOffset << "." << Discriminator << Inst << " (line offset: " << Lineno - HeaderLineno << "."
<< " - weight: " << Weight << ")\n"); << DIL->getDiscriminator() << " - weight: " << R.get()
return Weight; << ")\n");
return R;
} }
/// \brief Compute the weight of a basic block. /// \brief Compute the weight of a basic block.
/// ///
/// The weight of basic block \p BB is the maximum weight of all the /// The weight of basic block \p BB is the maximum weight of all the
/// instructions in BB. The weight of \p BB is computed and cached in /// instructions in BB.
/// the BlockWeights map.
/// ///
/// \param BB The basic block to query. /// \param BB The basic block to query.
/// ///
/// \returns The computed weight of BB. /// \returns the weight for \p BB.
unsigned SampleProfileLoader::getBlockWeight(BasicBlock *BB) { ErrorOr<unsigned>
// If we've computed BB's weight before, return it. SampleProfileLoader::getBlockWeight(const BasicBlock *BB) const {
std::pair<BlockWeightMap::iterator, bool> Entry = bool Found = false;
BlockWeights.insert(std::make_pair(BB, 0));
if (!Entry.second)
return Entry.first->second;
// Otherwise, compute and cache BB's weight.
unsigned Weight = 0; unsigned Weight = 0;
for (auto &I : BB->getInstList()) { for (auto &I : BB->getInstList()) {
unsigned InstWeight = getInstWeight(I); const ErrorOr<unsigned> &R = getInstWeight(I);
if (InstWeight > Weight) if (R && R.get() >= Weight) {
Weight = InstWeight; Weight = R.get();
Found = true;
}
} }
Entry.first->second = Weight; if (Found)
return Weight; return Weight;
else
return std::error_code();
} }
/// \brief Compute and store the weights of every basic block. /// \brief Compute and store the weights of every basic block.
@ -266,9 +271,12 @@ unsigned SampleProfileLoader::getBlockWeight(BasicBlock *BB) {
bool SampleProfileLoader::computeBlockWeights(Function &F) { bool SampleProfileLoader::computeBlockWeights(Function &F) {
bool Changed = false; bool Changed = false;
DEBUG(dbgs() << "Block weights\n"); DEBUG(dbgs() << "Block weights\n");
for (auto &BB : F) { for (const auto &BB : F) {
unsigned Weight = getBlockWeight(&BB); ErrorOr<unsigned> Weight = getBlockWeight(&BB);
Changed |= (Weight > 0); if (Weight) {
BlockWeights[&BB] = Weight.get();
Changed = true;
}
DEBUG(printBlockWeight(dbgs(), &BB)); DEBUG(printBlockWeight(dbgs(), &BB));
} }
@ -301,7 +309,7 @@ bool SampleProfileLoader::computeBlockWeights(Function &F) {
void SampleProfileLoader::findEquivalencesFor( void SampleProfileLoader::findEquivalencesFor(
BasicBlock *BB1, SmallVector<BasicBlock *, 8> Descendants, BasicBlock *BB1, SmallVector<BasicBlock *, 8> Descendants,
DominatorTreeBase<BasicBlock> *DomTree) { DominatorTreeBase<BasicBlock> *DomTree) {
for (auto *BB2 : Descendants) { for (const auto *BB2 : Descendants) {
bool IsDomParent = DomTree->dominates(BB2, BB1); bool IsDomParent = DomTree->dominates(BB2, BB1);
bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2); bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2);
if (BB1 != BB2 && VisitedBlocks.insert(BB2).second && IsDomParent && if (BB1 != BB2 && VisitedBlocks.insert(BB2).second && IsDomParent &&
@ -385,8 +393,8 @@ void SampleProfileLoader::findEquivalenceClasses(Function &F) {
// to all the blocks in that equivalence class. // to all the blocks in that equivalence class.
DEBUG(dbgs() << "\nAssign the same weight to all blocks in the same class\n"); DEBUG(dbgs() << "\nAssign the same weight to all blocks in the same class\n");
for (auto &BI : F) { for (auto &BI : F) {
BasicBlock *BB = &BI; const BasicBlock *BB = &BI;
BasicBlock *EquivBB = EquivalenceClass[BB]; const BasicBlock *EquivBB = EquivalenceClass[BB];
if (BB != EquivBB) if (BB != EquivBB)
BlockWeights[BB] = BlockWeights[EquivBB]; BlockWeights[BB] = BlockWeights[EquivBB];
DEBUG(printBlockWeight(dbgs(), BB)); DEBUG(printBlockWeight(dbgs(), BB));