<div dir="ltr">ASan reports a bunch of memory leaks after this patch:<div><br></div><div><a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/8040/steps/check-llvm%20asan/logs/stdio">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/8040/steps/check-llvm%20asan/logs/stdio</a><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 24, 2015 at 1:31 PM, Rong Xu via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: xur<br>
Date: Tue Nov 24 15:31:25 2015<br>
New Revision: 254021<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=254021&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=254021&view=rev</a><br>
Log:<br>
[PGO] MST based PGO instrumentation infrastructure<br>
<br>
This patch implements a minimum spanning tree (MST) based instrumentation for<br>
PGO. The use of MST guarantees minimum number of CFG edges getting<br>
instrumented. An addition optimization is to instrument the less executed<br>
edges to further reduce the instrumentation overhead. The patch contains both the<br>
instrumentation and the use of the profile to set the branch weights.<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D12781" rel="noreferrer" target="_blank">http://reviews.llvm.org/D12781</a><br>
<br>
Added:<br>
    llvm/trunk/lib/Transforms/Instrumentation/CFGMST.h<br>
    llvm/trunk/lib/Transforms/Instrumentation/PGOInstrumentation.cpp<br>
    llvm/trunk/test/Transforms/PGOProfile/<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/branch1.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/branch2.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/criticaledge.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/landingpad.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/loop1.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/loop2.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/loop3.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/single_bb.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/Inputs/switch.proftext<br>
    llvm/trunk/test/Transforms/PGOProfile/branch1_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/branch1_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/branch2_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/branch2_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/checksum_mismatch.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/criticaledge_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/criticaledge_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/landingpad_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/landingpad_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/loop1_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/loop1_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/loop2_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/loop2_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/loop3_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/loop3_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/noprofile_use.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/single_bb_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/switch_gen.ll<br>
    llvm/trunk/test/Transforms/PGOProfile/switch_use.ll<br>
Modified:<br>
    llvm/trunk/include/llvm/IR/DiagnosticInfo.h<br>
    llvm/trunk/include/llvm/InitializePasses.h<br>
    llvm/trunk/include/llvm/LinkAllPasses.h<br>
    llvm/trunk/include/llvm/Transforms/Instrumentation.h<br>
    llvm/trunk/lib/IR/DiagnosticInfo.cpp<br>
    llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt<br>
    llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt<br>
    llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp<br>
    llvm/trunk/lib/Transforms/Instrumentation/LLVMBuild.txt<br>
<br>
Modified: llvm/trunk/include/llvm/IR/DiagnosticInfo.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DiagnosticInfo.h?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DiagnosticInfo.h?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/IR/DiagnosticInfo.h (original)<br>
+++ llvm/trunk/include/llvm/IR/DiagnosticInfo.h Tue Nov 24 15:31:25 2015<br>
@@ -60,6 +60,7 @@ enum DiagnosticKind {<br>
   DK_OptimizationRemarkAnalysisAliasing,<br>
   DK_OptimizationFailure,<br>
   DK_MIRParser,<br>
+  DK_PGOProfile,<br>
   DK_FirstPluginKind<br>
 };<br>
<br>
@@ -248,6 +249,31 @@ private:<br>
<br>
   /// Message to report.<br>
   const Twine &Msg;<br>
+};<br>
+<br>
+/// Diagnostic information for the PGO profiler.<br>
+class DiagnosticInfoPGOProfile : public DiagnosticInfo {<br>
+public:<br>
+  DiagnosticInfoPGOProfile(const char *FileName, const Twine &Msg,<br>
+                           DiagnosticSeverity Severity = DS_Error)<br>
+      : DiagnosticInfo(DK_PGOProfile, Severity), FileName(FileName), Msg(Msg) {}<br>
+<br>
+  /// \see DiagnosticInfo::print.<br>
+  void print(DiagnosticPrinter &DP) const override;<br>
+<br>
+  static bool classof(const DiagnosticInfo *DI) {<br>
+    return DI->getKind() == DK_PGOProfile;<br>
+  }<br>
+<br>
+  const char *getFileName() const { return FileName; }<br>
+  const Twine &getMsg() const { return Msg; }<br>
+<br>
+private:<br>
+  /// Name of the input file associated with this diagnostic.<br>
+  const char *FileName;<br>
+<br>
+  /// Message to report.<br>
+  const Twine &Msg;<br>
 };<br>
<br>
 /// Common features for diagnostics dealing with optimization remarks.<br>
<br>
Modified: llvm/trunk/include/llvm/InitializePasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/InitializePasses.h (original)<br>
+++ llvm/trunk/include/llvm/InitializePasses.h Tue Nov 24 15:31:25 2015<br>
@@ -117,6 +117,8 @@ void initializeEdgeBundlesPass(PassRegis<br>
 void initializeExpandPostRAPass(PassRegistry&);<br>
 void initializeAAResultsWrapperPassPass(PassRegistry &);<br>
 void initializeGCOVProfilerPass(PassRegistry&);<br>
+void initializePGOInstrumentationGenPass(PassRegistry&);<br>
+void initializePGOInstrumentationUsePass(PassRegistry&);<br>
 void initializeInstrProfilingPass(PassRegistry&);<br>
 void initializeAddressSanitizerPass(PassRegistry&);<br>
 void initializeAddressSanitizerModulePass(PassRegistry&);<br>
<br>
Modified: llvm/trunk/include/llvm/LinkAllPasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/LinkAllPasses.h (original)<br>
+++ llvm/trunk/include/llvm/LinkAllPasses.h Tue Nov 24 15:31:25 2015<br>
@@ -85,6 +85,8 @@ namespace {<br>
       (void) llvm::createDomOnlyViewerPass();<br>
       (void) llvm::createDomViewerPass();<br>
       (void) llvm::createGCOVProfilerPass();<br>
+      (void) llvm::createPGOInstrumentationGenPass();<br>
+      (void) llvm::createPGOInstrumentationUsePass();<br>
       (void) llvm::createInstrProfilingPass();<br>
       (void) llvm::createFunctionInliningPass();<br>
       (void) llvm::createAlwaysInlinerPass();<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Instrumentation.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Instrumentation.h?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Instrumentation.h?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Instrumentation.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Instrumentation.h Tue Nov 24 15:31:25 2015<br>
@@ -79,6 +79,11 @@ struct GCOVOptions {<br>
 ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =<br>
                                    GCOVOptions::getDefault());<br>
<br>
+// PGO Instrumention<br>
+ModulePass *createPGOInstrumentationGenPass();<br>
+ModulePass *<br>
+createPGOInstrumentationUsePass(StringRef Filename = StringRef(""));<br>
+<br>
 /// Options for the frontend instrumentation based profiling pass.<br>
 struct InstrProfOptions {<br>
   InstrProfOptions() : NoRedZone(false) {}<br>
@@ -149,6 +154,24 @@ FunctionPass *createBoundsCheckingPass()<br>
 /// protect against stack-based overflow vulnerabilities.<br>
 FunctionPass *createSafeStackPass(const TargetMachine *TM = nullptr);<br>
<br>
+/// \brief Calculate what to divide by to scale counts.<br>
+///<br>
+/// Given the maximum count, calculate a divisor that will scale all the<br>
+/// weights to strictly less than UINT32_MAX.<br>
+static inline uint64_t calculateCountScale(uint64_t MaxCount) {<br>
+  return MaxCount < UINT32_MAX ? 1 : MaxCount / UINT32_MAX + 1;<br>
+}<br>
+<br>
+/// \brief Scale an individual branch count.<br>
+///<br>
+/// Scale a 64-bit weight down to 32-bits using \c Scale.<br>
+///<br>
+static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {<br>
+  uint64_t Scaled = Count / Scale;<br>
+  assert(Scaled <= UINT32_MAX && "overflow 32-bits");<br>
+  return Scaled;<br>
+}<br>
+<br>
 } // End llvm namespace<br>
<br>
 #endif<br>
<br>
Modified: llvm/trunk/lib/IR/DiagnosticInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/DiagnosticInfo.cpp?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/DiagnosticInfo.cpp?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/IR/DiagnosticInfo.cpp (original)<br>
+++ llvm/trunk/lib/IR/DiagnosticInfo.cpp Tue Nov 24 15:31:25 2015<br>
@@ -132,6 +132,12 @@ void DiagnosticInfoSampleProfile::print(<br>
   DP << getMsg();<br>
 }<br>
<br>
+void DiagnosticInfoPGOProfile::print(DiagnosticPrinter &DP) const {<br>
+  if (getFileName())<br>
+    DP << getFileName() << ": ";<br>
+  DP << getMsg();<br>
+}<br>
+<br>
 bool DiagnosticInfoOptimizationBase::isLocationAvailable() const {<br>
   return getDebugLoc();<br>
 }<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/LLVMBuild.txt Tue Nov 24 15:31:25 2015<br>
@@ -20,4 +20,4 @@ type = Library<br>
 name = IPO<br>
 parent = Transforms<br>
 library_name = ipo<br>
-required_libraries = Analysis Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize<br>
+required_libraries = Analysis Core InstCombine IRReader Linker Object ProfileData Scalar Support TransformUtils Vectorize Instrumentation<br>
<br>
Added: llvm/trunk/lib/Transforms/Instrumentation/CFGMST.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CFGMST.h?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CFGMST.h?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/CFGMST.h (added)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/CFGMST.h Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,210 @@<br>
+//===-- CFGMST.h - Minimum Spanning Tree for CFG -------*- C++ -*-===//<br>
+//<br>
+//                      The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements Union-find algorithm to compute Minimum Spanning Tree<br>
+// for a given CFG.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/ADT/DenseMap.h"<br>
+#include "llvm/Support/Debug.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+#include "llvm/Support/BranchProbability.h"<br>
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"<br>
+#include "llvm/Analysis/CFG.h"<br>
+#include "llvm/Analysis/BranchProbabilityInfo.h"<br>
+#include "llvm/Analysis/BlockFrequencyInfo.h"<br>
+#include <string><br>
+#include <vector><br>
+#include <utility><br>
+<br>
+namespace llvm {<br>
+<br>
+#define DEBUG_TYPE "cfgmst"<br>
+<br>
+template <class Edge, class BBInfo> class CFGMST {<br>
+public:<br>
+  Function &F;<br>
+<br>
+  // Store all the edges in CFG. It may contain some stale edges<br>
+  // when Removed is set.<br>
+  std::vector<std::unique_ptr<Edge>> AllEdges;<br>
+<br>
+  // This map records the auxiliary information for each BB.<br>
+  DenseMap<const BasicBlock *, std::unique_ptr<BBInfo>> BBInfos;<br>
+<br>
+  // Find the root group of the G and compress the path from G to the root.<br>
+  BBInfo *findAndCompressGroup(BBInfo *G) {<br>
+    if (G->Group != G)<br>
+      G->Group = findAndCompressGroup(static_cast<BBInfo *>(G->Group));<br>
+    return static_cast<BBInfo *>(G->Group);<br>
+  }<br>
+<br>
+  // Union BB1 and BB2 into the same group and return true.<br>
+  // Returns false if BB1 and BB2 are already in the same group.<br>
+  bool unionGroups(const BasicBlock *BB1, const BasicBlock *BB2) {<br>
+    BBInfo *BB1G = findAndCompressGroup(&getBBInfo(BB1));<br>
+    BBInfo *BB2G = findAndCompressGroup(&getBBInfo(BB2));<br>
+<br>
+    if (BB1G == BB2G)<br>
+      return false;<br>
+<br>
+    // Make the smaller rank tree a direct child or the root of high rank tree.<br>
+    if (BB1G->Rank < BB2G->Rank)<br>
+      BB1G->Group = BB2G;<br>
+    else {<br>
+      BB2G->Group = BB1G;<br>
+      // If the ranks are the same, increment root of one tree by one.<br>
+      if (BB1G->Rank == BB2G->Rank)<br>
+        BB1G->Rank++;<br>
+    }<br>
+    return true;<br>
+  }<br>
+<br>
+  // Give BB, return the auxiliary information.<br>
+  BBInfo &getBBInfo(const BasicBlock *BB) const {<br>
+    auto It = BBInfos.find(BB);<br>
+    assert(It->second.get() != nullptr);<br>
+    return *It->second.get();<br>
+  }<br>
+<br>
+  // Traverse the CFG using a stack. Find all the edges and assign the weight.<br>
+  // Edges with large weight will be put into MST first so they are less likely<br>
+  // to be instrumented.<br>
+  void buildEdges() {<br>
+    DEBUG(dbgs() << "Build Edge on " << F.getName() << "\n");<br>
+<br>
+    const BasicBlock *BB = &(F.getEntryBlock());<br>
+    // Add a fake edge to the entry.<br>
+    addEdge(nullptr, BB, BFI->getEntryFreq());<br>
+<br>
+    // Special handling for single BB functions.<br>
+    if (succ_empty(BB)) {<br>
+      addEdge(BB, nullptr, BFI->getEntryFreq());<br>
+      return;<br>
+    }<br>
+<br>
+    static const uint32_t CriticalEdgeMultiplier = 1000;<br>
+<br>
+    for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {<br>
+      TerminatorInst *TI = BB->getTerminator();<br>
+      uint64_t BBWeight = BFI->getBlockFreq(&*BB).getFrequency();<br>
+      uint64_t Weight;<br>
+      if (int successors = TI->getNumSuccessors()) {<br>
+        for (uint32_t i = 0; i != successors; ++i) {<br>
+          BasicBlock *TargetBB = TI->getSuccessor(i);<br>
+          bool Critical = isCriticalEdge(TI, i);<br>
+          uint64_t scaleFactor = BBWeight;<br>
+          if (Critical) {<br>
+            if (scaleFactor < UINT64_MAX / CriticalEdgeMultiplier)<br>
+              scaleFactor *= CriticalEdgeMultiplier;<br>
+            else<br>
+              scaleFactor = UINT64_MAX;<br>
+          }<br>
+          Weight = BPI->getEdgeProbability(&*BB, TargetBB).scale(scaleFactor);<br>
+          addEdge(&*BB, TargetBB, Weight).IsCritical = Critical;<br>
+          DEBUG(dbgs() << "  Edge: from " << BB->getName() << " to "<br>
+                       << TargetBB->getName() << "  w=" << Weight << "\n");<br>
+        }<br>
+      } else {<br>
+        addEdge(&*BB, nullptr, BBWeight);<br>
+        DEBUG(dbgs() << "  Edge: from " << BB->getName() << " to exit"<br>
+                     << " w = " << BBWeight << "\n");<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
+  // Sort CFG edges based on its weight.<br>
+  void sortEdgesByWeight() {<br>
+    std::sort(<br>
+        AllEdges.begin(), AllEdges.end(),<br>
+        [](const std::unique_ptr<Edge> &lhs, const std::unique_ptr<Edge> &rhs) {<br>
+          return lhs->Weight > rhs->Weight;<br>
+        });<br>
+  }<br>
+<br>
+  // Traverse all the edges and compute the Minimum Weight Spanning Tree<br>
+  // using union-find algorithm.<br>
+  void computeMinimumSpanningTree() {<br>
+    // First, put all the critical edge with landing-pad as the Dest to MST.<br>
+    // This works around the insufficient support of critical edges split<br>
+    // when destination BB is a landing pad.<br>
+    for (auto &Ei : AllEdges) {<br>
+      if (Ei->Removed)<br>
+        continue;<br>
+      if (Ei->IsCritical) {<br>
+        if (Ei->DestBB && Ei->DestBB->isLandingPad()) {<br>
+          if (unionGroups(Ei->SrcBB, Ei->DestBB))<br>
+            Ei->InMST = true;<br>
+        }<br>
+      }<br>
+    }<br>
+<br>
+    for (auto &Ei : AllEdges) {<br>
+      if (Ei->Removed)<br>
+        continue;<br>
+      if (unionGroups(Ei->SrcBB, Ei->DestBB))<br>
+        Ei->InMST = true;<br>
+    }<br>
+  }<br>
+<br>
+  // Dump the Debug information about the instrumentation.<br>
+  void dumpEdges(raw_ostream &OS, const StringRef Message = StringRef()) const {<br>
+    if (!Message.empty())<br>
+      OS << Message << "\n";<br>
+    OS << "  Number of Basic Blocks: " << BBInfos.size() << "\n";<br>
+    for (auto &BI : BBInfos) {<br>
+      const BasicBlock *BB = BI.first;<br>
+      OS << "  BB: " << (BB == nullptr ? "FakeNode" : BB->getName()) << "  "<br>
+         << BI.second->infoString() << "\n";<br>
+    }<br>
+<br>
+    OS << "  Number of Edges: " << AllEdges.size()<br>
+       << " (*: Instrument, C: CriticalEdge, -: Removed)\n";<br>
+    uint32_t Count = 0;<br>
+    for (auto &EI : AllEdges) {<br>
+      OS << "  Edge " << Count++ << ": " << getBBInfo(EI->SrcBB).Index << "-->"<br>
+         << getBBInfo(EI->DestBB).Index << EI->infoString() << "\n";<br>
+    };<br>
+  }<br>
+<br>
+  // Add an edge to AllEdges with weight W.<br>
+  Edge &addEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W) {<br>
+    uint32_t Index = BBInfos.size();<br>
+    auto Iter = BBInfos.end();<br>
+    bool Inserted;<br>
+    std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Src, nullptr));<br>
+    if (Inserted) {<br>
+      // Newly inserted, update the real info.<br>
+      Iter->second = std::move(llvm::make_unique<BBInfo>(Index));<br>
+      Index++;<br>
+    }<br>
+    std::tie(Iter, Inserted) = BBInfos.insert(std::make_pair(Dest, nullptr));<br>
+    if (Inserted)<br>
+      // Newly inserted, update the real info.<br>
+      Iter->second = std::move(llvm::make_unique<BBInfo>(Index));<br>
+    AllEdges.emplace_back(new Edge(Src, Dest, W));<br>
+    return *AllEdges.back();<br>
+  }<br>
+<br>
+  BranchProbabilityInfo *BPI;<br>
+  BlockFrequencyInfo *BFI;<br>
+<br>
+public:<br>
+  CFGMST(Function &Func, BranchProbabilityInfo *BPI_ = nullptr,<br>
+         BlockFrequencyInfo *BFI_ = nullptr)<br>
+      : F(Func), BPI(BPI_), BFI(BFI_) {<br>
+    buildEdges();<br>
+    sortEdgesByWeight();<br>
+    computeMinimumSpanningTree();<br>
+  }<br>
+};<br>
+<br>
+#undef DEBUG_TYPE // "cfgmst"<br>
+} // end namespace llvm<br>
<br>
Modified: llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt Tue Nov 24 15:31:25 2015<br>
@@ -6,6 +6,7 @@ add_llvm_library(LLVMInstrumentation<br>
   MemorySanitizer.cpp<br>
   Instrumentation.cpp<br>
   InstrProfiling.cpp<br>
+  PGOInstrumentation.cpp<br>
   SafeStack.cpp<br>
   SanitizerCoverage.cpp<br>
   ThreadSanitizer.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp Tue Nov 24 15:31:25 2015<br>
@@ -60,6 +60,8 @@ void llvm::initializeInstrumentation(Pas<br>
   initializeAddressSanitizerModulePass(Registry);<br>
   initializeBoundsCheckingPass(Registry);<br>
   initializeGCOVProfilerPass(Registry);<br>
+  initializePGOInstrumentationGenPass(Registry);<br>
+  initializePGOInstrumentationUsePass(Registry);<br>
   initializeInstrProfilingPass(Registry);<br>
   initializeMemorySanitizerPass(Registry);<br>
   initializeThreadSanitizerPass(Registry);<br>
<br>
Modified: llvm/trunk/lib/Transforms/Instrumentation/LLVMBuild.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/LLVMBuild.txt?rev=254021&r1=254020&r2=254021&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/LLVMBuild.txt?rev=254021&r1=254020&r2=254021&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/LLVMBuild.txt (original)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/LLVMBuild.txt Tue Nov 24 15:31:25 2015<br>
@@ -19,4 +19,4 @@<br>
 type = Library<br>
 name = Instrumentation<br>
 parent = Transforms<br>
-required_libraries = Analysis Core MC Support TransformUtils<br>
+required_libraries = Analysis Core MC Support TransformUtils ProfileData<br>
<br>
Added: llvm/trunk/lib/Transforms/Instrumentation/PGOInstrumentation.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/PGOInstrumentation.cpp?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/PGOInstrumentation.cpp?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (added)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/PGOInstrumentation.cpp Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,718 @@<br>
+//===- PGOInstru.cpp - PGO Instrumentation --------===//<br>
+//<br>
+//                      The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements PGO instrumentation using a minimum spanning tree based<br>
+// on the  following paper.<br>
+//   [1] Donald E. Knuth, Francis R. Stevenson. Optimal measurement of points<br>
+//   for program frequency counts. BIT Numerical Mathematics 1973, Volume 13,<br>
+//   Issue 3, pp 313-322<br>
+// The idea of the algorithm based on the fact that for each node (except for<br>
+// the entry and exit), the sum of incoming edge counts equals the sum of<br>
+// outgoing edge counts. The count of edge on spanning tree can be derived from<br>
+// those edges not on the spanning tree. Knuth proves this method instruments<br>
+// the minimum number of edges.<br>
+//<br>
+// The minimal spanning tree here is actually a maximum weight tree -- on-tree<br>
+// edges have higher frequencies (most likely to execute). The idea is to<br>
+// instrument those less frequently executed edges which speeds up the<br>
+// instrumented binaries.<br>
+//<br>
+// This file contains two passes:<br>
+// (1) Pass PGOInstrumentationGen which instruments the IR to generate edge<br>
+// count profile, and<br>
+// (2) Pass PGOInstrumentationUse which reads the edge count profile and<br>
+// annotates the branch weight.<br>
+// These two passes are mutually exclusive, and they are called at the same<br>
+// compilation point (so they see the same IR). For PGOInstrumentationGen,<br>
+// the real work is done instrumentOneFunc(). For PGOInstrumentationUse, the<br>
+// real work in done in class PGOUseFunc and the profile is opened in module<br>
+// level and passed to each PGOUseFunc instance.<br>
+// The shared code for PGOInstrumentationGen and PGOInstrumentationUse is put<br>
+// in class FuncPGOInstrumentation.<br>
+//<br>
+// Class PGOEdge represents a CFG edge and some auxiliary information. Class<br>
+// BBInfo contains auxiliary information for a BB. These two classes are used<br>
+// in PGOGenFunc. Class PGOUseEdge and UseBBInfo are the derived class of<br>
+// PGOEdge and BBInfo, respectively. They contains extra data structure used<br>
+// in populating profile counters.<br>
+// The MST implementation is in Class CFGMST.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/Transforms/Instrumentation.h"<br>
+#include "llvm/ADT/Statistic.h"<br>
+#include "llvm/ADT/DenseMap.h"<br>
+#include "llvm/ADT/STLExtras.h"<br>
+#include "llvm/IR/InstIterator.h"<br>
+#include "llvm/IR/Instructions.h"<br>
+#include "llvm/IR/IntrinsicInst.h"<br>
+#include "llvm/IR/IRBuilder.h"<br>
+#include "llvm/IR/MDBuilder.h"<br>
+#include "llvm/IR/DiagnosticInfo.h"<br>
+#include "llvm/Pass.h"<br>
+#include "llvm/IR/Module.h"<br>
+#include "llvm/Support/Debug.h"<br>
+#include "llvm/Support/BranchProbability.h"<br>
+#include "llvm/Support/JamCRC.h"<br>
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"<br>
+#include "llvm/ProfileData/InstrProfReader.h"<br>
+#include "llvm/Analysis/CFG.h"<br>
+#include "llvm/Analysis/BranchProbabilityInfo.h"<br>
+#include "llvm/Analysis/BlockFrequencyInfo.h"<br>
+#include <string><br>
+#include <vector><br>
+#include <utility><br>
+#include "CFGMST.h"<br>
+<br>
+using namespace llvm;<br>
+<br>
+#define DEBUG_TYPE "pgo-instr"<br>
+<br>
+STATISTIC(NumOfPGOInstrument, "Number of edges instrumented.");<br>
+STATISTIC(NumOfPGOEdge, "Number of edges.");<br>
+STATISTIC(NumOfPGOBB, "Number of basic-blocks.");<br>
+STATISTIC(NumOfPGOSplit, "Number of critical edge splits.");<br>
+STATISTIC(NumOfPGOFunc, "Number of functions having valid profile counts.");<br>
+STATISTIC(NumOfPGOMismatch, "Number of functions having mismatch profile.");<br>
+STATISTIC(NumOfPGOMissing, "Number of functions without profile.");<br>
+<br>
+static cl::opt<std::string><br>
+    PGOProfileFile("pgo-profile-file", cl::init(""), cl::Hidden,<br>
+                   cl::value_desc("filename"),<br>
+                   cl::desc("Specify the path of profile data file"));<br>
+<br>
+namespace {<br>
+class PGOInstrumentationGen : public ModulePass {<br>
+public:<br>
+  static char ID;<br>
+<br>
+  PGOInstrumentationGen() : ModulePass(ID) {<br>
+    initializePGOInstrumentationGenPass(*PassRegistry::getPassRegistry());<br>
+  }<br>
+<br>
+  const char *getPassName() const override {<br>
+    return "PGOInstrumentationGenPass";<br>
+  }<br>
+<br>
+private:<br>
+  bool runOnModule(Module &M) override;<br>
+<br>
+  void getAnalysisUsage(AnalysisUsage &AU) const override {<br>
+    AU.addRequired<BlockFrequencyInfoWrapperPass>();<br>
+    AU.addRequired<BranchProbabilityInfoWrapperPass>();<br>
+  }<br>
+};<br>
+<br>
+class PGOInstrumentationUse : public ModulePass {<br>
+public:<br>
+  static char ID;<br>
+<br>
+  // Provide the profile filename as the parameter.<br>
+  PGOInstrumentationUse(StringRef Filename = StringRef(""))<br>
+      : ModulePass(ID), ProfileFileName(Filename) {<br>
+    if (!PGOProfileFile.empty())<br>
+      ProfileFileName = StringRef(PGOProfileFile);<br>
+    initializePGOInstrumentationUsePass(*PassRegistry::getPassRegistry());<br>
+  }<br>
+<br>
+  const char *getPassName() const override {<br>
+    return "PGOInstrumentationUsePass";<br>
+  }<br>
+<br>
+private:<br>
+  StringRef ProfileFileName;<br>
+  std::unique_ptr<IndexedInstrProfReader> PGOReader;<br>
+  bool runOnModule(Module &M) override;<br>
+<br>
+  void getAnalysisUsage(AnalysisUsage &AU) const override {<br>
+    AU.addRequired<BlockFrequencyInfoWrapperPass>();<br>
+    AU.addRequired<BranchProbabilityInfoWrapperPass>();<br>
+  }<br>
+};<br>
+} // end anonymous namespace<br>
+<br>
+char PGOInstrumentationGen::ID = 0;<br>
+INITIALIZE_PASS_BEGIN(PGOInstrumentationGen, "pgo-instr-gen",<br>
+                      "PGO instrumentation.", false, false)<br>
+INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)<br>
+INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)<br>
+INITIALIZE_PASS_END(PGOInstrumentationGen, "pgo-instr-gen",<br>
+                    "PGO instrumentation.", false, false)<br>
+<br>
+ModulePass *llvm::createPGOInstrumentationGenPass() {<br>
+  return new PGOInstrumentationGen();<br>
+}<br>
+<br>
+char PGOInstrumentationUse::ID = 0;<br>
+INITIALIZE_PASS_BEGIN(PGOInstrumentationUse, "pgo-instr-use",<br>
+                      "Read PGO instrumentation profile.", false, false)<br>
+INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)<br>
+INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)<br>
+INITIALIZE_PASS_END(PGOInstrumentationUse, "pgo-instr-use",<br>
+                    "Read PGO instrumentation profile.", false, false)<br>
+<br>
+ModulePass *llvm::createPGOInstrumentationUsePass(StringRef Filename) {<br>
+  return new PGOInstrumentationUse(Filename);<br>
+}<br>
+<br>
+namespace {<br>
+/// \brief An MST based instrumentation for PGO<br>
+///<br>
+/// Implements a Minimum Spanning Tree (MST) based instrumentation for PGO<br>
+/// in the function level.<br>
+//<br>
+// This class implements the CFG edges. Note the CFG can be a multi-graph.<br>
+struct PGOEdge {<br>
+  const BasicBlock *SrcBB;<br>
+  const BasicBlock *DestBB;<br>
+  uint64_t Weight;<br>
+  bool InMST;<br>
+  bool Removed;<br>
+  bool IsCritical;<br>
+  PGOEdge(const BasicBlock *Src, const BasicBlock *Dest, unsigned W = 1)<br>
+      : SrcBB(Src), DestBB(Dest), Weight(W), InMST(false), Removed(false),<br>
+        IsCritical(false) {}<br>
+  // Return the information string of an edge.<br>
+  const std::string infoString() const {<br>
+    std::string Str = (Removed ? "-" : " ");<br>
+    Str += (InMST ? " " : "*");<br>
+    Str += (IsCritical ? "c" : " ");<br>
+    Str += "  W=" + std::to_string(Weight);<br>
+    return Str;<br>
+  }<br>
+};<br>
+<br>
+// This class stores the auxiliary information for each BB.<br>
+struct BBInfo {<br>
+  BBInfo *Group;<br>
+  uint32_t Index;<br>
+  uint32_t Rank;<br>
+<br>
+  BBInfo(unsigned IX) : Group(this), Index(IX), Rank(0) {}<br>
+<br>
+  // Return the information string of this object.<br>
+  const std::string infoString() const {<br>
+    return "Index=" + std::to_string(Index);<br>
+  }<br>
+};<br>
+<br>
+// This class implements the CFG edges. Note the CFG can be a multi-graph.<br>
+template <class Edge, class BBInfo> class FuncPGOInstrumentation {<br>
+private:<br>
+  Function &F;<br>
+  void computeCFGHash();<br>
+<br>
+public:<br>
+  std::string FuncName;<br>
+  GlobalVariable *FuncNameVar;<br>
+  // CFG hash value for this function.<br>
+  uint64_t FunctionHash;<br>
+<br>
+  // The Minimum Spanning Tree of function CFG.<br>
+  CFGMST<Edge, BBInfo> MST;<br>
+<br>
+  // Give an edge, find the BB that will be instrumented.<br>
+  // Return nullptr if there is no BB to be instrumented.<br>
+  BasicBlock *getInstrBB(Edge *E);<br>
+<br>
+  // Return the auxiliary BB information.<br>
+  BBInfo &getBBInfo(const BasicBlock *BB) const { return MST.getBBInfo(BB); }<br>
+<br>
+  // Dump edges and BB information.<br>
+  void dumpInfo(std::string Str = "") const {<br>
+    std::string Message = "Dump Function " + FuncName + " Hash: " +<br>
+                          std::to_string(FunctionHash) + "\t" + Str;<br>
+    MST.dumpEdges(dbgs(), Message);<br>
+  }<br>
+<br>
+  FuncPGOInstrumentation(Function &Func, bool CreateGlobalVar = false,<br>
+                         BranchProbabilityInfo *BPI_ = nullptr,<br>
+                         BlockFrequencyInfo *BFI_ = nullptr)<br>
+      : F(Func), FunctionHash(0), MST(F, BPI_, BFI_) {<br>
+    FuncName = getPGOFuncName(F);<br>
+    computeCFGHash();<br>
+    DEBUG(dumpInfo("after CFGMST"));<br>
+<br>
+    NumOfPGOBB += MST.BBInfos.size();<br>
+    for (auto &Ei : MST.AllEdges) {<br>
+      if (Ei->Removed)<br>
+        continue;<br>
+      NumOfPGOEdge++;<br>
+      if (!Ei->InMST)<br>
+        NumOfPGOInstrument++;<br>
+    }<br>
+<br>
+    if (CreateGlobalVar)<br>
+      FuncNameVar = createPGOFuncNameVar(F, FuncName);<br>
+  };<br>
+};<br>
+<br>
+// Compute Hash value for the CFG: the lower 32 bits are CRC32 of the index<br>
+// value of each BB in the CFG. The higher 32 bits record the number of edges.<br>
+template <class Edge, class BBInfo><br>
+void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {<br>
+  std::vector<char> Indexes;<br>
+  JamCRC JC;<br>
+  for (auto &BB : F) {<br>
+    const TerminatorInst *TI = BB.getTerminator();<br>
+    for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {<br>
+      BasicBlock *Succ = TI->getSuccessor(s);<br>
+      uint32_t Index = getBBInfo(Succ).Index;<br>
+      for (int i = 0; i < sizeof(uint32_t) / sizeof(char); i++)<br>
+        Indexes.push_back((char)(Index >> (i * sizeof(char))));<br>
+    }<br>
+  }<br>
+  JC.update(Indexes);<br>
+  FunctionHash = MST.AllEdges.size() << 32 | JC.getCRC();<br>
+}<br>
+<br>
+template <class Edge, class BBInfo><br>
+BasicBlock *FuncPGOInstrumentation<Edge, BBInfo>::getInstrBB(Edge *E) {<br>
+  if (E->InMST || E->Removed)<br>
+    return nullptr;<br>
+<br>
+  BasicBlock *SrcBB = const_cast<BasicBlock *>(E->SrcBB);<br>
+  BasicBlock *DestBB = const_cast<BasicBlock *>(E->DestBB);<br>
+  // For a fake edge, instrument the real BB.<br>
+  if (SrcBB == nullptr)<br>
+    return DestBB;<br>
+  if (DestBB == nullptr)<br>
+    return SrcBB;<br>
+<br>
+  // Instrument the SrcBB if it has a single successor,<br>
+  // otherwise, the DestBB if this is not a critical edge.<br>
+  TerminatorInst *TI = SrcBB->getTerminator();<br>
+  if (TI->getNumSuccessors() <= 1)<br>
+    return SrcBB;<br>
+  if (!E->IsCritical)<br>
+    return DestBB;<br>
+<br>
+  // For a critical edge, we have to split. Instrument the newly<br>
+  // created BB.<br>
+  NumOfPGOSplit++;<br>
+  DEBUG(dbgs() << "Split critical edge: " << getBBInfo(SrcBB).Index << " --> "<br>
+               << getBBInfo(DestBB).Index << "\n");<br>
+  unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);<br>
+  BasicBlock *InstrBB = SplitCriticalEdge(TI, SuccNum);<br>
+  assert(InstrBB && "Critical edge is not split");<br>
+<br>
+  E->Removed = true;<br>
+  return InstrBB;<br>
+}<br>
+<br>
+// Visit all edge and instrument the edges not in MST.<br>
+// Critical edges will be split.<br>
+static void instrumentOneFunc(Function &F, Module *M,<br>
+                              BranchProbabilityInfo *BPI,<br>
+                              BlockFrequencyInfo *BFI) {<br>
+  unsigned NumCounters = 0;<br>
+  FuncPGOInstrumentation<PGOEdge, BBInfo> FuncInfo(F, true, BPI, BFI);<br>
+  for (auto &Ei : FuncInfo.MST.AllEdges) {<br>
+    if (!Ei->InMST && !Ei->Removed)<br>
+      NumCounters++;<br>
+  }<br>
+<br>
+  uint32_t j = 0;<br>
+  for (auto &Ei : FuncInfo.MST.AllEdges) {<br>
+    BasicBlock *InstrBB = FuncInfo.getInstrBB(Ei.get());<br>
+    if (!InstrBB)<br>
+      continue;<br>
+<br>
+    IRBuilder<> Builder(InstrBB, InstrBB->getFirstInsertionPt());<br>
+    assert(Builder.GetInsertPoint() != InstrBB->end() &&<br>
+           "Cannot get the Instrumentation point");<br>
+    auto *I8PtrTy = Type::getInt8PtrTy(M->getContext());<br>
+    Builder.CreateCall(<br>
+        Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment),<br>
+        {llvm::ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy),<br>
+         Builder.getInt64(FuncInfo.FunctionHash), Builder.getInt32(NumCounters),<br>
+         Builder.getInt32(j++)});<br>
+  }<br>
+}<br>
+<br>
+struct PGOUseEdge : public PGOEdge {<br>
+  bool CountValid;<br>
+  uint64_t CountValue;<br>
+  PGOUseEdge(const BasicBlock *Src, const BasicBlock *Dest, unsigned W = 1)<br>
+      : PGOEdge(Src, Dest, W), CountValid(false), CountValue(0) {}<br>
+<br>
+  // Set edge count value<br>
+  void setEdgeCount(uint64_t Value) {<br>
+    CountValue = Value;<br>
+    CountValid = true;<br>
+  }<br>
+<br>
+  // Return the information string for this object.<br>
+  const std::string infoString() const {<br>
+    if (!CountValid)<br>
+      return PGOEdge::infoString();<br>
+    return PGOEdge::infoString() + "  Count=" + std::to_string(CountValue);<br>
+  }<br>
+};<br>
+<br>
+typedef SmallVector<PGOUseEdge *, 2> DirectEdges;<br>
+<br>
+// This class stores the auxiliary information for each BB.<br>
+struct UseBBInfo : public BBInfo {<br>
+  uint64_t CountValue;<br>
+  bool CountValid;<br>
+  int32_t UnknownCountInEdge;<br>
+  int32_t UnknownCountOutEdge;<br>
+  DirectEdges InEdges;<br>
+  DirectEdges OutEdges;<br>
+  UseBBInfo(unsigned IX)<br>
+      : BBInfo(IX), CountValue(0), CountValid(false), UnknownCountInEdge(0),<br>
+        UnknownCountOutEdge(0) {}<br>
+  UseBBInfo(unsigned IX, uint64_t C)<br>
+      : BBInfo(IX), CountValue(C), CountValid(true), UnknownCountInEdge(0),<br>
+        UnknownCountOutEdge(0) {}<br>
+<br>
+  // Set the profile count value for this BB.<br>
+  void setBBInfoCount(uint64_t Value) {<br>
+    CountValue = Value;<br>
+    CountValid = true;<br>
+  }<br>
+<br>
+  // Return the information string of this object.<br>
+  const std::string infoString() const {<br>
+    if (!CountValid)<br>
+      return BBInfo::infoString();<br>
+    return BBInfo::infoString() + "  Count=" + std::to_string(CountValue);<br>
+  }<br>
+};<br>
+<br>
+// Sum up the count values for all the edges.<br>
+static uint64_t sumEdgeCount(const ArrayRef<PGOUseEdge *> Edges) {<br>
+  uint64_t Total = 0;<br>
+  for (auto &Ei : Edges) {<br>
+    if (Ei->Removed)<br>
+      continue;<br>
+    Total += Ei->CountValue;<br>
+  }<br>
+  return Total;<br>
+}<br>
+<br>
+class PGOUseFunc {<br>
+private:<br>
+  Function &F;<br>
+  Module *M;<br>
+  // This member stores the shared information with class PGOGenFunc.<br>
+  FuncPGOInstrumentation<PGOUseEdge, UseBBInfo> FuncInfo;<br>
+<br>
+  // Return the auxiliary BB information.<br>
+  UseBBInfo &getBBInfo(const BasicBlock *BB) const {<br>
+    return FuncInfo.getBBInfo(BB);<br>
+  }<br>
+<br>
+  // The maximum count value in the profile. This is only used in PGO use<br>
+  // compilation.<br>
+  uint64_t ProgramMaxCount;<br>
+<br>
+  // Find the Instrumented BB and set the value.<br>
+  void setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);<br>
+<br>
+  // Set the edge counter value for the unknown edge -- there should be only<br>
+  // one unknown edge.<br>
+  void setEdgeCount(DirectEdges &Edges, uint64_t Value);<br>
+<br>
+  // Return FuncName string;<br>
+  const std::string getFuncName() const { return FuncInfo.FuncName; }<br>
+<br>
+  // Set the hot/cold inline hints based on the count values.<br>
+  void applyFunctionAttributes(uint64_t EntryCount, uint64_t MaxCount) {<br>
+    if (ProgramMaxCount == 0)<br>
+      return;<br>
+    // Threshold of the hot functions.<br>
+    const BranchProbability HotFunctionThreshold(1, 100);<br>
+    // Threshold of the cold functions.<br>
+    const BranchProbability ColdFunctionThreshold(2, 10000);<br>
+    if (EntryCount >= HotFunctionThreshold.scale(ProgramMaxCount))<br>
+      F.addFnAttr(llvm::Attribute::InlineHint);<br>
+    else if (MaxCount <= ColdFunctionThreshold.scale(ProgramMaxCount))<br>
+      F.addFnAttr(llvm::Attribute::Cold);<br>
+  }<br>
+<br>
+public:<br>
+  PGOUseFunc(Function &Func, Module *Modu,<br>
+             BranchProbabilityInfo *BPI_ = nullptr,<br>
+             BlockFrequencyInfo *BFI_ = nullptr)<br>
+      : F(Func), M(Modu), FuncInfo(Func, false, BPI_, BFI_) {}<br>
+<br>
+  // Read counts for the instrumented BB from profile.<br>
+  bool readCounters(IndexedInstrProfReader *PGOReader);<br>
+<br>
+  // Populate the counts for all BBs.<br>
+  void populateCounters();<br>
+<br>
+  // Set the branch weights based on the count values.<br>
+  void setBranchWeights();<br>
+};<br>
+<br>
+// Visit all the edges and assign the count value for the instrumented<br>
+// edges and the BB.<br>
+void PGOUseFunc::setInstrumentedCounts(<br>
+    const std::vector<uint64_t> &CountFromProfile) {<br>
+<br>
+  // Use a worklist as we will update the vector during the iteration.<br>
+  std::vector<PGOUseEdge *> WorkList;<br>
+  for (auto &Ei : FuncInfo.MST.AllEdges)<br>
+    WorkList.push_back(Ei.get());<br>
+<br>
+  uint32_t j = 0;<br>
+  for (auto &Ei : WorkList) {<br>
+    BasicBlock *InstrBB = FuncInfo.getInstrBB(Ei);<br>
+    if (!InstrBB)<br>
+      continue;<br>
+    uint64_t CountValue = CountFromProfile[j++];<br>
+    if (!Ei->Removed) {<br>
+      getBBInfo(InstrBB).setBBInfoCount(CountValue);<br>
+      Ei->setEdgeCount(CountValue);<br>
+      continue;<br>
+    }<br>
+<br>
+    // Need to add two new edges.<br>
+    BasicBlock *SrcBB = const_cast<BasicBlock *>(Ei->SrcBB);<br>
+    BasicBlock *DestBB = const_cast<BasicBlock *>(Ei->DestBB);<br>
+    // Add new edge of SrcBB->InstrBB.<br>
+    PGOUseEdge &NewEdge = FuncInfo.MST.addEdge(SrcBB, InstrBB, 0);<br>
+    NewEdge.setEdgeCount(CountValue);<br>
+    // Add new edge of InstrBB->DestBB.<br>
+    PGOUseEdge &NewEdge1 = FuncInfo.MST.addEdge(InstrBB, DestBB, 0);<br>
+    NewEdge1.setEdgeCount(CountValue);<br>
+    NewEdge1.InMST = true;<br>
+    getBBInfo(InstrBB).setBBInfoCount(CountValue);<br>
+  }<br>
+}<br>
+<br>
+// Set the count value for the unknown edges. There should be one and only one<br>
+// unknown edge in Edges vector.<br>
+void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) {<br>
+  for (auto &Ei : Edges) {<br>
+    if (Ei->CountValid)<br>
+      continue;<br>
+    Ei->setEdgeCount(Value);<br>
+<br>
+    getBBInfo(Ei->SrcBB).UnknownCountOutEdge--;<br>
+    getBBInfo(Ei->DestBB).UnknownCountInEdge--;<br>
+    return;<br>
+  }<br>
+  llvm_unreachable("Cannot find the unknown count edge");<br>
+}<br>
+<br>
+// Read the profile from ProfileFileName and assign the value to the<br>
+// instrumented BB and the edges. This function also updates ProgramMaxCount.<br>
+// Return true if the profile are successfully read, and false on errors.<br>
+bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader) {<br>
+  auto &Ctx = M->getContext();<br>
+  ErrorOr<InstrProfRecord> Result =<br>
+      PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash);<br>
+  if (std::error_code EC = Result.getError()) {<br>
+    if (EC == instrprof_error::unknown_function)<br>
+      NumOfPGOMissing++;<br>
+    else if (EC == instrprof_error::hash_mismatch ||<br>
+             EC == llvm::instrprof_error::malformed)<br>
+      NumOfPGOMismatch++;<br>
+<br>
+    std::string Msg = EC.message() + std::string(" ") + F.getName().str();<br>
+    Ctx.diagnose(<br>
+        DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));<br>
+    return false;<br>
+  }<br>
+  std::vector<uint64_t> &CountFromProfile = Result.get().Counts;<br>
+<br>
+  NumOfPGOFunc++;<br>
+  DEBUG(dbgs() << CountFromProfile.size() << " counts\n");<br>
+  uint64_t ValueSum = 0;<br>
+  for (unsigned i = 0, e = CountFromProfile.size(); i < e; i++) {<br>
+    DEBUG(dbgs() << "  " << i << ": " << CountFromProfile[i] << "\n");<br>
+    ValueSum += CountFromProfile[i];<br>
+  }<br>
+<br>
+  DEBUG(dbgs() << "SUM =  " << ValueSum << "\n");<br>
+<br>
+  getBBInfo(nullptr).UnknownCountOutEdge = 2;<br>
+  getBBInfo(nullptr).UnknownCountInEdge = 2;<br>
+<br>
+  setInstrumentedCounts(CountFromProfile);<br>
+  ProgramMaxCount = PGOReader->getMaximumFunctionCount();<br>
+  return true;<br>
+}<br>
+<br>
+// Populate the counters from instrumented BBs to all BBs.<br>
+// In the end of this operation, all BBs should have a valid count value.<br>
+void PGOUseFunc::populateCounters() {<br>
+  // First set up Count variable for all BBs.<br>
+  for (auto &Ei : FuncInfo.MST.AllEdges) {<br>
+    if (Ei->Removed)<br>
+      continue;<br>
+<br>
+    const BasicBlock *SrcBB = Ei->SrcBB;<br>
+    const BasicBlock *DestBB = Ei->DestBB;<br>
+    UseBBInfo &SrcInfo = getBBInfo(SrcBB);<br>
+    UseBBInfo &DestInfo = getBBInfo(DestBB);<br>
+    SrcInfo.OutEdges.push_back(Ei.get());<br>
+    DestInfo.InEdges.push_back(Ei.get());<br>
+    SrcInfo.UnknownCountOutEdge++;<br>
+    DestInfo.UnknownCountInEdge++;<br>
+<br>
+    if (!Ei->CountValid)<br>
+      continue;<br>
+    DestInfo.UnknownCountInEdge--;<br>
+    SrcInfo.UnknownCountOutEdge--;<br>
+  }<br>
+<br>
+  bool Changes = true;<br>
+  unsigned NumPasses = 0;<br>
+  while (Changes) {<br>
+    NumPasses++;<br>
+    Changes = false;<br>
+<br>
+    // For efficient traversal, it's better to start from the end as most<br>
+    // of the instrumented edges are at the end.<br>
+    for (auto &BB : reverse(F)) {<br>
+      UseBBInfo &Count = getBBInfo(&BB);<br>
+      if (!Count.CountValid) {<br>
+        if (Count.UnknownCountOutEdge == 0) {<br>
+          Count.CountValue = sumEdgeCount(Count.OutEdges);<br>
+          Count.CountValid = true;<br>
+          Changes = true;<br>
+        } else if (Count.UnknownCountInEdge == 0) {<br>
+          Count.CountValue = sumEdgeCount(Count.InEdges);<br>
+          Count.CountValid = true;<br>
+          Changes = true;<br>
+        }<br>
+      }<br>
+      if (Count.CountValid) {<br>
+        if (Count.UnknownCountOutEdge == 1) {<br>
+          uint64_t Total = Count.CountValue - sumEdgeCount(Count.OutEdges);<br>
+          setEdgeCount(Count.OutEdges, Total);<br>
+          Changes = true;<br>
+        }<br>
+        if (Count.UnknownCountInEdge == 1) {<br>
+          uint64_t Total = Count.CountValue - sumEdgeCount(Count.InEdges);<br>
+          setEdgeCount(Count.InEdges, Total);<br>
+          Changes = true;<br>
+        }<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
+  DEBUG(dbgs() << "Populate counts in " << NumPasses << " passes.\n");<br>
+  // Assert every BB has a valid counter.<br>
+  uint64_t FuncEntryCount = getBBInfo(&*F.begin()).CountValue;<br>
+  uint64_t FuncMaxCount = FuncEntryCount;<br>
+  for (auto &BB : F) {<br>
+    assert(getBBInfo(&BB).CountValid && "BB count is not valid");<br>
+    uint64_t Count = getBBInfo(&BB).CountValue;<br>
+    if (Count > FuncMaxCount)<br>
+      FuncMaxCount = Count;<br>
+  }<br>
+  applyFunctionAttributes(FuncEntryCount, FuncMaxCount);<br>
+<br>
+  DEBUG(FuncInfo.dumpInfo("after reading profile."));<br>
+}<br>
+<br>
+// Assign the scaled count values to the BB with multiple out edges.<br>
+void PGOUseFunc::setBranchWeights() {<br>
+  // Generate MD_prof metadata for every branch instruction.<br>
+  DEBUG(dbgs() << "\nSetting branch weights.\n");<br>
+  MDBuilder MDB(M->getContext());<br>
+  for (auto &BB : F) {<br>
+    TerminatorInst *TI = BB.getTerminator();<br>
+    if (TI->getNumSuccessors() < 2)<br>
+      continue;<br>
+    if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))<br>
+      continue;<br>
+    if (getBBInfo(&BB).CountValue == 0)<br>
+      continue;<br>
+<br>
+    // We have a non-zero Branch BB.<br>
+    const UseBBInfo &BBCountInfo = getBBInfo(&BB);<br>
+    unsigned Size = BBCountInfo.OutEdges.size();<br>
+    SmallVector<unsigned, 2> EdgeCounts(Size, 0);<br>
+    uint64_t MaxCount = 0;<br>
+    for (unsigned s = 0; s < Size; s++) {<br>
+      const PGOUseEdge *E = BBCountInfo.OutEdges[s];<br>
+      const BasicBlock *SrcBB = E->SrcBB;<br>
+      const BasicBlock *DestBB = E->DestBB;<br>
+      if (DestBB == 0)<br>
+        continue;<br>
+      unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);<br>
+      uint64_t EdgeCount = E->CountValue;<br>
+      if (EdgeCount > MaxCount)<br>
+        MaxCount = EdgeCount;<br>
+      EdgeCounts[SuccNum] = EdgeCount;<br>
+    }<br>
+    assert(MaxCount > 0 && "Bad max count");<br>
+    uint64_t Scale = calculateCountScale(MaxCount);<br>
+    SmallVector<unsigned, 4> Weights;<br>
+    for (const auto &ECI : EdgeCounts)<br>
+      Weights.push_back(scaleBranchCount(ECI, Scale));<br>
+<br>
+    TI->setMetadata(llvm::LLVMContext::MD_prof,<br>
+                    MDB.createBranchWeights(Weights));<br>
+    DEBUG(dbgs() << "Weight is: "; for (const auto &W<br>
+                                        : Weights) dbgs()<br>
+                                   << W << " ";<br>
+          dbgs() << "\n";);<br>
+  }<br>
+}<br>
+} // end anonymous namespace<br>
+<br>
+bool PGOInstrumentationGen::runOnModule(Module &M) {<br>
+  for (auto &F : M) {<br>
+    if (F.isDeclaration())<br>
+      continue;<br>
+    BranchProbabilityInfo *BPI =<br>
+        &(getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI());<br>
+    BlockFrequencyInfo *BFI =<br>
+        &(getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI());<br>
+    instrumentOneFunc(F, &M, BPI, BFI);<br>
+  }<br>
+  return true;<br>
+}<br>
+<br>
+static void setPGOCountOnFunc(PGOUseFunc &Func,<br>
+                              IndexedInstrProfReader *PGOReader) {<br>
+  if (Func.readCounters(PGOReader)) {<br>
+    Func.populateCounters();<br>
+    Func.setBranchWeights();<br>
+  }<br>
+}<br>
+<br>
+bool PGOInstrumentationUse::runOnModule(Module &M) {<br>
+  DEBUG(dbgs() << "Read in profile counters: ");<br>
+  auto &Ctx = M.getContext();<br>
+  // Read the counter array from file.<br>
+  auto ReaderOrErr = IndexedInstrProfReader::create(ProfileFileName);<br>
+  if (std::error_code EC = ReaderOrErr.getError()) {<br>
+    Ctx.diagnose(<br>
+        DiagnosticInfoPGOProfile(ProfileFileName.data(), EC.message()));<br>
+    return false;<br>
+  }<br>
+<br>
+  PGOReader = std::move(ReaderOrErr.get());<br>
+  if (!PGOReader) {<br>
+    Ctx.diagnose(DiagnosticInfoPGOProfile(ProfileFileName.data(),<br>
+                                          "Cannot get PGOReader"));<br>
+    return false;<br>
+  }<br>
+<br>
+  for (auto &F : M) {<br>
+    if (F.isDeclaration())<br>
+      continue;<br>
+    BranchProbabilityInfo *BPI =<br>
+        &(getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI());<br>
+    BlockFrequencyInfo *BFI =<br>
+        &(getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI());<br>
+    PGOUseFunc Func(F, &M, BPI, BFI);<br>
+    setPGOCountOnFunc(Func, PGOReader.get());<br>
+  }<br>
+  return true;<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/branch1.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/branch1.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/branch1.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/branch1.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/branch1.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,6 @@<br>
+_Z9test_br_1i<br>
+23925403969<br>
+2<br>
+3<br>
+2<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/branch2.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/branch2.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/branch2.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/branch2.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/branch2.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,6 @@<br>
+_Z9test_br_2i<br>
+29368252703<br>
+2<br>
+1<br>
+1<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/criticaledge.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/criticaledge.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/criticaledge.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/criticaledge.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/criticaledge.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,17 @@<br>
+_Z17test_criticalEdgeii<br>
+82348442248<br>
+8<br>
+2<br>
+1<br>
+2<br>
+2<br>
+0<br>
+1<br>
+2<br>
+1<br>
+<br>
+<stdin>:_ZL3bari<br>
+12884901887<br>
+1<br>
+7<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/landingpad.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/landingpad.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/landingpad.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/landingpad.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/landingpad.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,14 @@<br>
+_Z3fooi<br>
+57451243578<br>
+4<br>
+3<br>
+1<br>
+2<br>
+0<br>
+<br>
+_Z3bari<br>
+23319865734<br>
+2<br>
+1<br>
+2<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/loop1.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/loop1.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/loop1.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/loop1.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/loop1.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,6 @@<br>
+_Z15test_simple_fori<br>
+32052181608<br>
+2<br>
+96<br>
+4<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/loop2.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/loop2.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/loop2.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/loop2.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/loop2.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,6 @@<br>
+_Z13test_do_whilei<br>
+29706172832<br>
+2<br>
+96<br>
+4<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/loop3.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/loop3.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/loop3.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/loop3.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/loop3.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,8 @@<br>
+_Z15test_nested_foriii<br>
+75296580464<br>
+4<br>
+186<br>
+33<br>
+10<br>
+6<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/single_bb.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/single_bb.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/single_bb.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/single_bb.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/single_bb.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,5 @@<br>
+_Z9single_bbv<br>
+12884999999<br>
+1<br>
+1<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/Inputs/switch.proftext<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/switch.proftext?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/Inputs/switch.proftext?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/Inputs/switch.proftext (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/Inputs/switch.proftext Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,8 @@<br>
+_Z13test_switch_1i<br>
+46628225222<br>
+4<br>
+0<br>
+0<br>
+0<br>
+3<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/branch1_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch1_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch1_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/branch1_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/branch1_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,21 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z9test_br_1i = private constant [13 x i8] c"_Z9test_br_1i"<br>
+<br>
+define i32 @_Z9test_br_1i(i32 %i) {<br>
+entry:<br>
+  %cmp = icmp sgt i32 %i, 0<br>
+  br i1 %cmp, label %if.then, label %if.end<br>
+<br>
+if.then:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__llvm_profile_name__Z9test_br_1i, i32 0, i32 0), i64 23925403969, i32 2, i32 1)<br>
+  %add = add nsw i32 %i, 2<br>
+  br label %if.end<br>
+<br>
+if.end:<br>
+  %retv = phi i32 [ %add, %if.then ], [ %i, %entry ]<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__llvm_profile_name__Z9test_br_1i, i32 0, i32 0), i64 23925403969, i32 2, i32 0)<br>
+  ret i32 %retv<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/branch1_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch1_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch1_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/branch1_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/branch1_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,21 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/branch1.proftext -o %T/branch1.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/branch1.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z9test_br_1i(i32 %i) {<br>
+entry:<br>
+  %cmp = icmp sgt i32 %i, 0<br>
+  br i1 %cmp, label %if.then, label %if.end<br>
+; CHECK: !prof !0<br>
+<br>
+if.then:<br>
+  %add = add nsw i32 %i, 2<br>
+  br label %if.end<br>
+<br>
+if.end:<br>
+  %retv = phi i32 [ %add, %if.then ], [ %i, %entry ]<br>
+  ret i32 %retv<br>
+}<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 2, i32 1}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/branch2_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch2_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch2_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/branch2_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/branch2_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,25 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z9test_br_2i = private constant [13 x i8] c"_Z9test_br_2i"<br>
+<br>
+define i32 @_Z9test_br_2i(i32 %i) {<br>
+entry:<br>
+  %cmp = icmp sgt i32 %i, 0<br>
+  br i1 %cmp, label %if.then, label %if.else<br>
+<br>
+if.then:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__llvm_profile_name__Z9test_br_2i, i32 0, i32 0), i64 29368252703, i32 2, i32 0)<br>
+  %add = add nsw i32 %i, 2<br>
+  br label %if.end<br>
+<br>
+if.else:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__llvm_profile_name__Z9test_br_2i, i32 0, i32 0), i64 29368252703, i32 2, i32 1)<br>
+  %sub = sub nsw i32 %i, 2<br>
+  br label %if.end<br>
+<br>
+if.end:<br>
+  %retv = phi i32 [ %add, %if.then ], [ %sub, %if.else ]<br>
+  ret i32 %retv<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/branch2_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch2_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/branch2_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/branch2_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/branch2_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,25 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/branch2.proftext -o %T/branch2.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/branch2.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z9test_br_2i(i32 %i) {<br>
+entry:<br>
+  %cmp = icmp sgt i32 %i, 0<br>
+  br i1 %cmp, label %if.then, label %if.else<br>
+; CHECK: !prof !0<br>
+<br>
+if.then:<br>
+  %add = add nsw i32 %i, 2<br>
+  br label %if.end<br>
+<br>
+if.else:<br>
+  %sub = sub nsw i32 %i, 2<br>
+  br label %if.end<br>
+<br>
+if.end:<br>
+  %retv = phi i32 [ %add, %if.then ], [ %sub, %if.else ]<br>
+  ret i32 %retv<br>
+}<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 1, i32 1}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/checksum_mismatch.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/checksum_mismatch.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/checksum_mismatch.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/checksum_mismatch.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/checksum_mismatch.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,18 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/single_bb.proftext -o %T/single_bb.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/single_bb.profdata -S 2>&1 | FileCheck %s<br>
+<br>
+; CHECK: Function control flow change detected (hash mismatch) _Z9single_bbv<br>
+; CHECK: No profile data available for function _ZL8uncalledii<br>
+<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z9single_bbv() {<br>
+entry:<br>
+  ret i32 0<br>
+}<br>
+<br>
+define i32 @_ZL8uncalledii(i32 %i, i32 %j) {<br>
+  %mul = mul nsw i32 %i, %j<br>
+  ret i32 %mul<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/criticaledge_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/criticaledge_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/criticaledge_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/criticaledge_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/criticaledge_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,80 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z17test_criticalEdgeii = private constant [23 x i8] c"_Z17test_criticalEdgeii"<br>
+; CHECK: @"__llvm_profile_name_<stdin>:_ZL3bari" = private constant [16 x i8] c"<stdin>:_ZL3bari"<br>
+<br>
+define i32 @_Z17test_criticalEdgeii(i32 %i, i32 %j) {<br>
+entry:<br>
+  switch i32 %i, label %sw.default [<br>
+    i32 1, label %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a><br>
+    i32 2, label %sw.bb1<br>
+    i32 3, label %sw.bb3<br>
+    i32 4, label %sw.bb3<br>
+; CHECK:    i32 3, label %entry.sw.bb3_crit_edge<br>
+; CHECK:    i32 4, label %entry.sw.bb3_crit_edge1<br>
+    i32 5, label %sw.bb3<br>
+  ]<br>
+<br>
+; CHECK: entry.sw.bb3_crit_edge1:<br>
+; CHECK:   call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 1)<br>
+; CHECK:   br label %sw.bb3<br>
+<br>
+; CHECK: entry.sw.bb3_crit_edge:<br>
+; CHECK:   call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 0)<br>
+; CHECK:   br label %sw.bb3<br>
+<br>
+<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a>:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 5)<br>
+  %call = call i32 @_ZL3bari(i32 2)<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb1:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 4)<br>
+  %call2 = call i32 @_ZL3bari(i32 1024)<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb3:<br>
+  %cmp = icmp eq i32 %j, 2<br>
+  br i1 %cmp, label %if.then, label %if.end<br>
+<br>
+if.then:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 2)<br>
+  %call4 = call i32 @_ZL3bari(i32 4)<br>
+  br label %return<br>
+<br>
+if.end:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 3)<br>
+  %call5 = call i32 @_ZL3bari(i32 8)<br>
+  br label %sw.epilog<br>
+<br>
+sw.default:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 6)<br>
+  %call6 = call i32 @_ZL3bari(i32 32)<br>
+  %cmp7 = icmp sgt i32 %j, 10<br>
+  br i1 %cmp7, label %if.then8, label %if.end9<br>
+<br>
+if.then8:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @__llvm_profile_name__Z17test_criticalEdgeii, i32 0, i32 0), i64 82348442248, i32 8, i32 7)<br>
+  %add = add nsw i32 %call6, 10<br>
+  br label %if.end9<br>
+<br>
+if.end9:<br>
+  %res.0 = phi i32 [ %add, %if.then8 ], [ %call6, %sw.default ]<br>
+  br label %sw.epilog<br>
+<br>
+sw.epilog:<br>
+  %res.1 = phi i32 [ %res.0, %if.end9 ], [ %call5, %if.end ], [ %call2, %sw.bb1 ], [ %call, %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a> ]<br>
+  br label %return<br>
+<br>
+return:<br>
+  %retval = phi i32 [ %res.1, %sw.epilog ], [ %call4, %if.then ]<br>
+  ret i32 %retval<br>
+}<br>
+<br>
+define internal i32 @_ZL3bari(i32 %i) {<br>
+entry:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @"__llvm_profile_name_<stdin>:_ZL3bari", i32 0, i32 0), i64 12884901887, i32 1, i32 0)<br>
+  ret i32 %i<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/criticaledge_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/criticaledge_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/criticaledge_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/criticaledge_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/criticaledge_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,75 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/criticaledge.proftext -o %T/criticaledge.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/criticaledge.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z17test_criticalEdgeii(i32 %i, i32 %j) {<br>
+entry:<br>
+  switch i32 %i, label %sw.default [<br>
+    i32 1, label %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a><br>
+    i32 2, label %sw.bb1<br>
+    i32 3, label %sw.bb3<br>
+    i32 4, label %sw.bb3<br>
+; CHECK:    i32 3, label %entry.sw.bb3_crit_edge<br>
+; CHECK:    i32 4, label %entry.sw.bb3_crit_edge1<br>
+    i32 5, label %sw.bb3<br>
+  ]<br>
+; CHECK: !prof !0<br>
+<br>
+; CHECK: entry.sw.bb3_crit_edge1:<br>
+; CHECK:   br label %sw.bb3<br>
+; CHECK: entry.sw.bb3_crit_edge:<br>
+; CHECK:   br label %sw.bb3<br>
+<br>
+<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a>:<br>
+  %call = call i32 @_ZL3bari(i32 2)<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb1:<br>
+  %call2 = call i32 @_ZL3bari(i32 1024)<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb3:<br>
+  %cmp = icmp eq i32 %j, 2<br>
+  br i1 %cmp, label %if.then, label %if.end<br>
+; CHECK: !prof !1<br>
+<br>
+if.then:<br>
+  %call4 = call i32 @_ZL3bari(i32 4)<br>
+  br label %return<br>
+<br>
+if.end:<br>
+  %call5 = call i32 @_ZL3bari(i32 8)<br>
+  br label %sw.epilog<br>
+<br>
+sw.default:<br>
+  %call6 = call i32 @_ZL3bari(i32 32)<br>
+  %cmp7 = icmp sgt i32 %j, 10<br>
+  br i1 %cmp7, label %if.then8, label %if.end9<br>
+; CHECK: !prof !2<br>
+<br>
+if.then8:<br>
+  %add = add nsw i32 %call6, 10<br>
+  br label %if.end9<br>
+<br>
+if.end9:<br>
+  %res.0 = phi i32 [ %add, %if.then8 ], [ %call6, %sw.default ]<br>
+  br label %sw.epilog<br>
+<br>
+sw.epilog:<br>
+  %res.1 = phi i32 [ %res.0, %if.end9 ], [ %call5, %if.end ], [ %call2, %sw.bb1 ], [ %call, %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a> ]<br>
+  br label %return<br>
+<br>
+return:<br>
+  %retval = phi i32 [ %res.1, %sw.epilog ], [ %call4, %if.then ]<br>
+  ret i32 %retval<br>
+}<br>
+<br>
+define internal i32 @_ZL3bari(i32 %i) {<br>
+entry:<br>
+  ret i32 %i<br>
+}<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 2, i32 1, i32 0, i32 2, i32 1, i32 1}<br>
+; CHECK: !1 = !{!"branch_weights", i32 2, i32 2}<br>
+; CHECK: !2 = !{!"branch_weights", i32 1, i32 1}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/landingpad_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/landingpad_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/landingpad_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/landingpad_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/landingpad_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,95 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@val = global i32 0, align 4<br>
+@_ZTIi = external constant i8*<br>
+; CHECK: @__llvm_profile_name__Z3bari = private constant [7 x i8] c"_Z3bari"<br>
+; CHECK: @__llvm_profile_name__Z3fooi = private constant [7 x i8] c"_Z3fooi"<br>
+<br>
+define i32 @_Z3bari(i32 %i) {<br>
+entry:<br>
+  %rem = srem i32 %i, 3<br>
+  %tobool = icmp ne i32 %rem, 0<br>
+  br i1 %tobool, label %if.then, label %if.end<br>
+<br>
+if.then:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @__llvm_profile_name__Z3bari, i32 0, i32 0), i64 23319865734, i32 2, i32 1)<br>
+  %exception = call i8* @__cxa_allocate_exception(i64 4)<br>
+  %tmp = bitcast i8* %exception to i32*<br>
+  store i32 %i, i32* %tmp, align 16<br>
+  call void @__cxa_throw(i8* %exception, i8* bitcast (i8** @_ZTIi to i8*), i8* null)<br>
+  unreachable<br>
+<br>
+if.end:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @__llvm_profile_name__Z3bari, i32 0, i32 0), i64 23319865734, i32 2, i32 0)<br>
+  ret i32 0<br>
+}<br>
+<br>
+declare i8* @__cxa_allocate_exception(i64)<br>
+<br>
+declare void @__cxa_throw(i8*, i8*, i8*)<br>
+<br>
+define i32 @_Z3fooi(i32 %i) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {<br>
+entry:<br>
+  %rem = srem i32 %i, 2<br>
+  %tobool = icmp ne i32 %rem, 0<br>
+  br i1 %tobool, label %if.then, label %if.end<br>
+<br>
+if.then:<br>
+  %mul = mul nsw i32 %i, 7<br>
+  %call = invoke i32 @_Z3bari(i32 %mul)<br>
+          to label %invoke.cont unwind label %lpad<br>
+<br>
+invoke.cont:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @__llvm_profile_name__Z3fooi, i32 0, i32 0), i64 57451243578, i32 4, i32 1)<br>
+  br label %if.end<br>
+<br>
+lpad:<br>
+  %tmp = landingpad { i8*, i32 }<br>
+          catch i8* bitcast (i8** @_ZTIi to i8*)<br>
+  %tmp1 = extractvalue { i8*, i32 } %tmp, 0<br>
+  %tmp2 = extractvalue { i8*, i32 } %tmp, 1<br>
+  br label %catch.dispatch<br>
+<br>
+catch.dispatch:<br>
+  %tmp3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))<br>
+  %matches = icmp eq i32 %tmp2, %tmp3<br>
+  br i1 %matches, label %catch, label %eh.resume<br>
+<br>
+catch:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @__llvm_profile_name__Z3fooi, i32 0, i32 0), i64 57451243578, i32 4, i32 2)<br>
+  %tmp4 = call i8* @__cxa_begin_catch(i8* %tmp1)<br>
+  %tmp5 = bitcast i8* %tmp4 to i32*<br>
+  %tmp6 = load i32, i32* %tmp5, align 4<br>
+  %tmp7 = load i32, i32* @val, align 4<br>
+  %sub = sub nsw i32 %tmp7, %tmp6<br>
+  store i32 %sub, i32* @val, align 4<br>
+  call void @__cxa_end_catch()<br>
+  br label %try.cont<br>
+<br>
+try.cont:<br>
+  ret i32 -1<br>
+<br>
+if.end:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @__llvm_profile_name__Z3fooi, i32 0, i32 0), i64 57451243578, i32 4, i32 0)<br>
+  %tmp8 = load i32, i32* @val, align 4<br>
+  %add = add nsw i32 %tmp8, %i<br>
+  store i32 %add, i32* @val, align 4<br>
+  br label %try.cont<br>
+<br>
+eh.resume:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @__llvm_profile_name__Z3fooi, i32 0, i32 0), i64 57451243578, i32 4, i32 3)<br>
+  %lpad.val = insertvalue { i8*, i32 } undef, i8* %tmp1, 0<br>
+  %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %tmp2, 1<br>
+  resume { i8*, i32 } %lpad.val3<br>
+}<br>
+<br>
+declare i32 @__gxx_personality_v0(...)<br>
+<br>
+declare i32 @llvm.eh.typeid.for(i8*)<br>
+<br>
+declare i8* @__cxa_begin_catch(i8*)<br>
+<br>
+declare void @__cxa_end_catch()<br>
+<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/landingpad_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/landingpad_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/landingpad_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/landingpad_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/landingpad_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,94 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/landingpad.proftext -o %T/landingpad.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/landingpad.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@val = global i32 0, align 4<br>
+@_ZTIi = external constant i8*<br>
+<br>
+define i32 @_Z3bari(i32 %i) {<br>
+entry:<br>
+  %rem = srem i32 %i, 3<br>
+  %tobool = icmp ne i32 %rem, 0<br>
+  br i1 %tobool, label %if.then, label %if.end<br>
+; CHECK: !prof !0<br>
+<br>
+if.then:<br>
+  %exception = call i8* @__cxa_allocate_exception(i64 4)<br>
+  %tmp = bitcast i8* %exception to i32*<br>
+  store i32 %i, i32* %tmp, align 16<br>
+  call void @__cxa_throw(i8* %exception, i8* bitcast (i8** @_ZTIi to i8*), i8* null)<br>
+  unreachable<br>
+<br>
+if.end:<br>
+  ret i32 0<br>
+}<br>
+<br>
+declare i8* @__cxa_allocate_exception(i64)<br>
+<br>
+declare void @__cxa_throw(i8*, i8*, i8*)<br>
+<br>
+define i32 @_Z3fooi(i32 %i) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {<br>
+entry:<br>
+  %rem = srem i32 %i, 2<br>
+  %tobool = icmp ne i32 %rem, 0<br>
+  br i1 %tobool, label %if.then, label %if.end<br>
+; CHECK: !prof !1<br>
+<br>
+if.then:<br>
+  %mul = mul nsw i32 %i, 7<br>
+  %call = invoke i32 @_Z3bari(i32 %mul)<br>
+          to label %invoke.cont unwind label %lpad<br>
+<br>
+invoke.cont:<br>
+  br label %if.end<br>
+<br>
+lpad:<br>
+  %tmp = landingpad { i8*, i32 }<br>
+          catch i8* bitcast (i8** @_ZTIi to i8*)<br>
+  %tmp1 = extractvalue { i8*, i32 } %tmp, 0<br>
+  %tmp2 = extractvalue { i8*, i32 } %tmp, 1<br>
+  br label %catch.dispatch<br>
+<br>
+catch.dispatch:<br>
+  %tmp3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))<br>
+  %matches = icmp eq i32 %tmp2, %tmp3<br>
+  br i1 %matches, label %catch, label %eh.resume<br>
+; CHECK: !prof !2<br>
+<br>
+catch:<br>
+  %tmp4 = call i8* @__cxa_begin_catch(i8* %tmp1)<br>
+  %tmp5 = bitcast i8* %tmp4 to i32*<br>
+  %tmp6 = load i32, i32* %tmp5, align 4<br>
+  %tmp7 = load i32, i32* @val, align 4<br>
+  %sub = sub nsw i32 %tmp7, %tmp6<br>
+  store i32 %sub, i32* @val, align 4<br>
+  call void @__cxa_end_catch()<br>
+  br label %try.cont<br>
+<br>
+try.cont:<br>
+  ret i32 -1<br>
+<br>
+if.end:<br>
+  %tmp8 = load i32, i32* @val, align 4<br>
+  %add = add nsw i32 %tmp8, %i<br>
+  store i32 %add, i32* @val, align 4<br>
+  br label %try.cont<br>
+<br>
+eh.resume:<br>
+  %lpad.val = insertvalue { i8*, i32 } undef, i8* %tmp1, 0<br>
+  %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %tmp2, 1<br>
+  resume { i8*, i32 } %lpad.val3<br>
+}<br>
+<br>
+declare i32 @__gxx_personality_v0(...)<br>
+<br>
+declare i32 @llvm.eh.typeid.for(i8*)<br>
+<br>
+declare i8* @__cxa_begin_catch(i8*)<br>
+<br>
+declare void @__cxa_end_catch()<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 2, i32 1}<br>
+; CHECK: !1 = !{!"branch_weights", i32 3, i32 2}<br>
+; CHECK: !2 = !{!"branch_weights", i32 2, i32 0}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/loop1_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop1_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop1_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/loop1_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/loop1_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,29 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z15test_simple_fori = private constant [20 x i8] c"_Z15test_simple_fori"<br>
+<br>
+define i32 @_Z15test_simple_fori(i32 %n) {<br>
+entry:<br>
+  br label %for.cond<br>
+<br>
+for.cond:<br>
+  %i = phi i32 [ 0, %entry ], [ %inc1, %for.inc ]<br>
+  %sum = phi i32 [ 1, %entry ], [ %inc, %for.inc ]<br>
+  %cmp = icmp slt i32 %i, %n<br>
+  br i1 %cmp, label %for.body, label %for.end<br>
+<br>
+for.body:<br>
+  %inc = add nsw i32 %sum, 1<br>
+  br label %for.inc<br>
+<br>
+for.inc:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @__llvm_profile_name__Z15test_simple_fori, i32 0, i32 0), i64 32052181608, i32 2, i32 0)<br>
+  %inc1 = add nsw i32 %i, 1<br>
+  br label %for.cond<br>
+<br>
+for.end:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @__llvm_profile_name__Z15test_simple_fori, i32 0, i32 0), i64 32052181608, i32 2, i32 1)<br>
+  ret i32 %sum<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/loop1_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop1_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop1_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/loop1_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/loop1_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,29 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/loop1.proftext -o %T/loop1.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/loop1.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z15test_simple_fori(i32 %n) {<br>
+entry:<br>
+  br label %for.cond<br>
+<br>
+for.cond:<br>
+  %i = phi i32 [ 0, %entry ], [ %inc1, %for.inc ]<br>
+  %sum = phi i32 [ 1, %entry ], [ %inc, %for.inc ]<br>
+  %cmp = icmp slt i32 %i, %n<br>
+  br i1 %cmp, label %for.body, label %for.end<br>
+; CHECK: !prof !0<br>
+<br>
+for.body:<br>
+  %inc = add nsw i32 %sum, 1<br>
+  br label %for.inc<br>
+<br>
+for.inc:<br>
+  %inc1 = add nsw i32 %i, 1<br>
+  br label %for.cond<br>
+<br>
+for.end:<br>
+  ret i32 %sum<br>
+}<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 96, i32 4}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/loop2_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop2_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop2_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/loop2_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/loop2_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,26 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z13test_do_whilei = private constant [18 x i8] c"_Z13test_do_whilei"<br>
+<br>
+define i32 @_Z13test_do_whilei(i32 %n) {<br>
+entry:<br>
+  br label %do.body<br>
+<br>
+do.body:<br>
+  %i.0 = phi i32 [ 0, %entry ], [ %inc1, %do.cond ]<br>
+  %sum = phi i32 [ 1, %entry ], [ %inc, %do.cond ]<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @__llvm_profile_name__Z13test_do_whilei, i32 0, i32 0), i64 29706172832, i32 2, i32 0)<br>
+  %inc = add nsw i32 %sum, 1<br>
+  br label %do.cond<br>
+<br>
+do.cond:<br>
+  %inc1 = add nsw i32 %i.0, 1<br>
+  %cmp = icmp slt i32 %i.0, %n<br>
+  br i1 %cmp, label %do.body, label %do.end<br>
+<br>
+do.end:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @__llvm_profile_name__Z13test_do_whilei, i32 0, i32 0), i64 29706172832, i32 2, i32 1)<br>
+  ret i32 %inc<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/loop2_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop2_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop2_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/loop2_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/loop2_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,26 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/loop2.proftext -o %T/loop2.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/loop2.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z13test_do_whilei(i32 %n) {<br>
+entry:<br>
+  br label %do.body<br>
+<br>
+do.body:<br>
+  %i.0 = phi i32 [ 0, %entry ], [ %inc1, %do.cond ]<br>
+  %sum = phi i32 [ 1, %entry ], [ %inc, %do.cond ]<br>
+  %inc = add nsw i32 %sum, 1<br>
+  br label %do.cond<br>
+<br>
+do.cond:<br>
+  %inc1 = add nsw i32 %i.0, 1<br>
+  %cmp = icmp slt i32 %i.0, %n<br>
+  br i1 %cmp, label %do.body, label %do.end<br>
+; CHECK: !prof !0<br>
+<br>
+do.end:<br>
+  ret i32 %inc<br>
+}<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 92, i32 4}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/loop3_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop3_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop3_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/loop3_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/loop3_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,63 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z15test_nested_foriii = private constant [22 x i8] c"_Z15test_nested_foriii"<br>
+<br>
+define i32 @_Z15test_nested_foriii(i32 %r, i32 %s, i32 %t) {<br>
+entry:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__llvm_profile_name__Z15test_nested_foriii, i32 0, i32 0), i64 75296580464, i32 4, i32 3)<br>
+  br label %for.cond<br>
+<br>
+for.cond:<br>
+  %i.0 = phi i32 [ 0, %entry ], [ %inc12, %for.inc11 ]<br>
+  %nested_for_sum.0 = phi i32 [ 1, %entry ], [ %nested_for_sum.1, %for.inc11 ]<br>
+  %cmp = icmp slt i32 %i.0, %r<br>
+  br i1 %cmp, label %for.body, label %for.end13<br>
+<br>
+for.body:<br>
+  br label %for.cond1<br>
+<br>
+for.cond1:<br>
+  %j.0 = phi i32 [ 0, %for.body ], [ %inc9, %for.inc8 ]<br>
+  %nested_for_sum.1 = phi i32 [ %nested_for_sum.0, %for.body ], [ %nested_for_sum.2, %for.inc8 ]<br>
+  %cmp2 = icmp slt i32 %j.0, %s<br>
+  br i1 %cmp2, label %for.body3, label %for.end10<br>
+<br>
+for.body3:<br>
+  br label %for.cond4<br>
+<br>
+for.cond4:<br>
+  %k.0 = phi i32 [ 0, %for.body3 ], [ %inc7, %for.inc ]<br>
+  %nested_for_sum.2 = phi i32 [ %nested_for_sum.1, %for.body3 ], [ %inc, %for.inc ]<br>
+  %cmp5 = icmp slt i32 %k.0, %t<br>
+  br i1 %cmp5, label %for.body6, label %for.end<br>
+<br>
+for.body6:<br>
+  %inc = add nsw i32 %nested_for_sum.2, 1<br>
+  br label %for.inc<br>
+<br>
+for.inc:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__llvm_profile_name__Z15test_nested_foriii, i32 0, i32 0), i64 75296580464, i32 4, i32 0)<br>
+  %inc7 = add nsw i32 %k.0, 1<br>
+  br label %for.cond4<br>
+<br>
+for.end:<br>
+  br label %for.inc8<br>
+<br>
+for.inc8:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__llvm_profile_name__Z15test_nested_foriii, i32 0, i32 0), i64 75296580464, i32 4, i32 1)<br>
+  %inc9 = add nsw i32 %j.0, 1<br>
+  br label %for.cond1<br>
+<br>
+for.end10:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__llvm_profile_name__Z15test_nested_foriii, i32 0, i32 0), i64 75296580464, i32 4, i32 2)<br>
+  br label %for.inc11<br>
+<br>
+for.inc11:<br>
+  %inc12 = add nsw i32 %i.0, 1<br>
+  br label %for.cond<br>
+<br>
+for.end13:<br>
+  ret i32 %nested_for_sum.0<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/loop3_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop3_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/loop3_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/loop3_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/loop3_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,65 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/loop3.proftext -o %T/loop3.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/loop3.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z15test_nested_foriii(i32 %r, i32 %s, i32 %t) {<br>
+entry:<br>
+  br label %for.cond<br>
+<br>
+for.cond:<br>
+  %i.0 = phi i32 [ 0, %entry ], [ %inc12, %for.inc11 ]<br>
+  %nested_for_sum.0 = phi i32 [ 1, %entry ], [ %nested_for_sum.1, %for.inc11 ]<br>
+  %cmp = icmp slt i32 %i.0, %r<br>
+  br i1 %cmp, label %for.body, label %for.end13<br>
+; CHECK: !prof !0<br>
+<br>
+for.body:<br>
+  br label %for.cond1<br>
+<br>
+for.cond1:<br>
+  %j.0 = phi i32 [ 0, %for.body ], [ %inc9, %for.inc8 ]<br>
+  %nested_for_sum.1 = phi i32 [ %nested_for_sum.0, %for.body ], [ %nested_for_sum.2, %for.inc8 ]<br>
+  %cmp2 = icmp slt i32 %j.0, %s<br>
+  br i1 %cmp2, label %for.body3, label %for.end10<br>
+; CHECK: !prof !1<br>
+<br>
+for.body3:<br>
+  br label %for.cond4<br>
+<br>
+for.cond4:<br>
+  %k.0 = phi i32 [ 0, %for.body3 ], [ %inc7, %for.inc ]<br>
+  %nested_for_sum.2 = phi i32 [ %nested_for_sum.1, %for.body3 ], [ %inc, %for.inc ]<br>
+  %cmp5 = icmp slt i32 %k.0, %t<br>
+  br i1 %cmp5, label %for.body6, label %for.end<br>
+; CHECK: !prof !2<br>
+<br>
+for.body6:<br>
+  %inc = add nsw i32 %nested_for_sum.2, 1<br>
+  br label %for.inc<br>
+<br>
+for.inc:<br>
+  %inc7 = add nsw i32 %k.0, 1<br>
+  br label %for.cond4<br>
+<br>
+for.end:<br>
+  br label %for.inc8<br>
+<br>
+for.inc8:<br>
+  %inc9 = add nsw i32 %j.0, 1<br>
+  br label %for.cond1<br>
+<br>
+for.end10:<br>
+  br label %for.inc11<br>
+<br>
+for.inc11:<br>
+  %inc12 = add nsw i32 %i.0, 1<br>
+  br label %for.cond<br>
+<br>
+for.end13:<br>
+  ret i32 %nested_for_sum.0<br>
+}<br>
+<br>
+; CHECK: !0 = !{!"branch_weights", i32 10, i32 6}<br>
+; CHECK: !1 = !{!"branch_weights", i32 33, i32 10}<br>
+; CHECK: !2 = !{!"branch_weights", i32 186, i32 33}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/noprofile_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/noprofile_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/noprofile_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/noprofile_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/noprofile_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,9 @@<br>
+; RUN: not opt < %s -pgo-instr-use -pgo-profile-file=%T/notexisting.profdata -S  2>&1 |grep 'notexisting.profdata: No such file or directory'<br>
+<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z9single_bbv() {<br>
+entry:<br>
+  ret i32 0<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/single_bb_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/single_bb_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/single_bb_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/single_bb_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/single_bb_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,11 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z9single_bbv = private constant [13 x i8] c"_Z9single_bbv"<br>
+<br>
+define i32 @_Z9single_bbv() {<br>
+entry:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__llvm_profile_name__Z9single_bbv, i32 0, i32 0), i64 12884901887, i32 1, i32 0)<br>
+  ret i32 0<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/switch_gen.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/switch_gen.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/switch_gen.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/switch_gen.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/switch_gen.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,38 @@<br>
+; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+; CHECK: @__llvm_profile_name__Z13test_switch_1i = private constant [18 x i8] c"_Z13test_switch_1i"<br>
+<br>
+define i32 @_Z13test_switch_1i(i32 %i) {<br>
+entry:<br>
+  switch i32 %i, label %sw.default [<br>
+    i32 1, label %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a><br>
+    i32 2, label %sw.bb1<br>
+    i32 3, label %sw.bb3<br>
+  ]<br>
+<br>
+<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a>:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @__llvm_profile_name__Z13test_switch_1i, i32 0, i32 0), i64 46628225222, i32 4, i32 2)<br>
+  %add = add nsw i32 %i, 2<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb1:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @__llvm_profile_name__Z13test_switch_1i, i32 0, i32 0), i64 46628225222, i32 4, i32 0)<br>
+  %add2 = add nsw i32 %i, 100<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb3:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @__llvm_profile_name__Z13test_switch_1i, i32 0, i32 0), i64 46628225222, i32 4, i32 1)<br>
+  %add4 = add nsw i32 %i, 4<br>
+  br label %sw.epilog<br>
+<br>
+sw.default:<br>
+; CHECK: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @__llvm_profile_name__Z13test_switch_1i, i32 0, i32 0), i64 46628225222, i32 4, i32 3)<br>
+  %add5 = add nsw i32 %i, 1<br>
+  br label %sw.epilog<br>
+<br>
+sw.epilog:<br>
+  %retv = phi i32 [ %add5, %sw.default ], [ %add4, %sw.bb3 ], [ %add2, %sw.bb1 ], [ %add, %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a> ]<br>
+  ret i32 %retv<br>
+}<br>
<br>
Added: llvm/trunk/test/Transforms/PGOProfile/switch_use.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/switch_use.ll?rev=254021&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/PGOProfile/switch_use.ll?rev=254021&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/PGOProfile/switch_use.ll (added)<br>
+++ llvm/trunk/test/Transforms/PGOProfile/switch_use.ll Tue Nov 24 15:31:25 2015<br>
@@ -0,0 +1,36 @@<br>
+; RUN: llvm-profdata merge %S/Inputs/switch.proftext -o %T/switch.profdata<br>
+; RUN: opt < %s -pgo-instr-use -pgo-profile-file=%T/switch.profdata -S | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @_Z13test_switch_1i(i32 %i) {<br>
+entry:<br>
+  switch i32 %i, label %sw.default [<br>
+    i32 1, label %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a><br>
+    i32 2, label %sw.bb1<br>
+    i32 3, label %sw.bb3<br>
+  ]<br>
+; CHECK: !prof !0<br>
+<br>
+<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a>:<br>
+  %add = add nsw i32 %i, 2<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb1:<br>
+  %add2 = add nsw i32 %i, 100<br>
+  br label %sw.epilog<br>
+<br>
+sw.bb3:<br>
+  %add4 = add nsw i32 %i, 4<br>
+  br label %sw.epilog<br>
+<br>
+sw.default:<br>
+  %add5 = add nsw i32 %i, 1<br>
+  br label %sw.epilog<br>
+<br>
+sw.epilog:<br>
+  %retv = phi i32 [ %add5, %sw.default ], [ %add4, %sw.bb3 ], [ %add2, %sw.bb1 ], [ %add, %<a href="http://sw.bb" rel="noreferrer" target="_blank">sw.bb</a> ]<br>
+  ret i32 %retv<br>
+}<br>
+<br>
+;CHECK: !0 = !{!"branch_weights", i32 3, i32 0, i32 0, i32 0}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</div>