Index: include/llvm/IR/DebugCounterPrinter.h
===================================================================
--- /dev/null
+++ include/llvm/IR/DebugCounterPrinter.h
@@ -0,0 +1,32 @@
+//===- DebugCounterPrinter.h - DebugCounterPrinter --------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+///
+/// The header file for the DebugCounterPrinterPass pass.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_IR_DEBUGCOUNTERPRINTER_H
+#define LLVM_IR_DEBUGCOUNTERPRINTER_H
+
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+struct DebugCounterPrinterPass : PassInfoMixin<DebugCounterPrinterPass> {
+  /// \brief Run the pass over the function.
+  ///
+  /// This will print out DebugCounter info on each counter.
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
+};
+
+ModulePass *createDebugCounterPrinterPass();
+} // namespace llvm
+
+#endif
Index: include/llvm/InitializePasses.h
===================================================================
--- include/llvm/InitializePasses.h
+++ include/llvm/InitializePasses.h
@@ -117,6 +117,7 @@
 void initializeDataFlowSanitizerPass(PassRegistry&);
 void initializeDeadInstEliminationPass(PassRegistry&);
 void initializeDeadMachineInstructionElimPass(PassRegistry&);
+void initializeDebugCounterPrinterLegacyPassPass(PassRegistry&);
 void initializeDelinearizationPass(PassRegistry&);
 void initializeDemandedBitsWrapperPassPass(PassRegistry&);
 void initializeDependenceAnalysisPass(PassRegistry&);
Index: lib/IR/CMakeLists.txt
===================================================================
--- lib/IR/CMakeLists.txt
+++ lib/IR/CMakeLists.txt
@@ -14,6 +14,7 @@
   Core.cpp
   DIBuilder.cpp
   DataLayout.cpp
+  DebugCounterPrinter.cpp
   DebugInfo.cpp
   DebugInfoMetadata.cpp
   DebugLoc.cpp
Index: lib/IR/Core.cpp
===================================================================
--- lib/IR/Core.cpp
+++ lib/IR/Core.cpp
@@ -45,6 +45,7 @@
 #define DEBUG_TYPE "ir"
 
 void llvm::initializeCore(PassRegistry &Registry) {
+  initializeDebugCounterPrinterLegacyPassPass(Registry);
   initializeDominatorTreeWrapperPassPass(Registry);
   initializePrintModulePassWrapperPass(Registry);
   initializePrintFunctionPassWrapperPass(Registry);
Index: lib/IR/DebugCounterPrinter.cpp
===================================================================
--- /dev/null
+++ lib/IR/DebugCounterPrinter.cpp
@@ -0,0 +1,50 @@
+//===- DebugCounterPrinter.cpp - Printer for DebugCounter ----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file prints debug counter info as a module pass.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/DebugCounterPrinter.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/DebugCounter.h"
+
+using namespace llvm;
+
+PreservedAnalyses DebugCounterPrinterPass::run(Module &M,
+                                               ModuleAnalysisManager &MAM) {
+  auto DC = &DebugCounter::instance();
+  DC->print(dbgs());
+  return PreservedAnalyses::all();
+}
+
+namespace {
+/// Legacy pass for printing out debug counter info.
+struct DebugCounterPrinterLegacyPass : ModulePass {
+  static char ID;
+  DebugCounterPrinterLegacyPass() : ModulePass(ID) {
+    initializeDebugCounterPrinterLegacyPassPass(
+        *PassRegistry::getPassRegistry());
+  }
+
+  bool runOnModule(Module &M) override {
+    auto DC = &DebugCounter::instance();
+    DC->print(dbgs());
+    return false;
+  }
+};
+} // namespace
+
+char DebugCounterPrinterLegacyPass::ID = 0;
+INITIALIZE_PASS(DebugCounterPrinterLegacyPass, "debug-counter-printer",
+                "Print DebugCounter", false, true)
+
+ModulePass *llvm::createDebugCounterPrinterPass() {
+  return new DebugCounterPrinterLegacyPass();
+}
\ No newline at end of file
Index: lib/Passes/PassBuilder.cpp
===================================================================
--- lib/Passes/PassBuilder.cpp
+++ lib/Passes/PassBuilder.cpp
@@ -53,6 +53,7 @@
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
 #include "llvm/CodeGen/UnreachableBlockElim.h"
+#include "llvm/IR/DebugCounterPrinter.h"
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/PassManager.h"
Index: lib/Passes/PassRegistry.def
===================================================================
--- lib/Passes/PassRegistry.def
+++ lib/Passes/PassRegistry.def
@@ -44,6 +44,7 @@
 MODULE_PASS("constmerge", ConstantMergePass())
 MODULE_PASS("cross-dso-cfi", CrossDSOCFIPass())
 MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
+MODULE_PASS("debug-counter-printer", DebugCounterPrinterPass())
 MODULE_PASS("elim-avail-extern", EliminateAvailableExternallyPass())
 MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
 MODULE_PASS("function-import", FunctionImportPass())
Index: lib/Support/DebugCounter.cpp
===================================================================
--- lib/Support/DebugCounter.cpp
+++ lib/Support/DebugCounter.cpp
@@ -1,4 +1,5 @@
 #include "llvm/Support/DebugCounter.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -101,11 +102,18 @@
 }
 
 void DebugCounter::print(raw_ostream &OS) const {
+  SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
+                                          RegisteredCounters.end());
+  sort(CounterNames.begin(), CounterNames.end());
+
+  auto &Us = instance();
   OS << "Counters and values:\n";
-  for (const auto &KV : Counters)
-    OS << left_justify(RegisteredCounters[KV.first], 32) << ": {"
-       << KV.second.Count << "," << KV.second.Skip << ","
-       << KV.second.StopAfter << "}\n";
+  for (auto &CounterName : CounterNames) {
+    unsigned CounterID = getCounterId(CounterName);
+    OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
+       << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
+       << "," << Us.Counters[CounterID].StopAfter << "}\n";
+  }
 }
 
 LLVM_DUMP_METHOD void DebugCounter::dump() const {
Index: test/Other/debug-counter-printer.ll
===================================================================
--- /dev/null
+++ test/Other/debug-counter-printer.ll
@@ -0,0 +1,30 @@
+; RUN: opt -S -debug-counter=early-cse-skip=1,early-cse-count=1 -early-cse \
+; RUN:        -debug-counter=newgvn-vn-skip=1,newgvn-vn-count=2 -newgvn \
+; RUN:        -instcombine -debug-counter-printer < %s 2>&1 | FileCheck %s
+;; Test debug counter prints correct info in right order.
+; CHECK-LABEL: Counters and values:
+; CHECK:       early-cse
+; CHECK-SAME:  {4,1,1}
+; CHECK:       instcombine-visit
+; CHECK-SAME:  {12,0,-1}
+; CHECK:       newgvn-vn
+; CHECK-SAME:  {9,1,2}
+define i32 @f1(i32 %a, i32 %b) {
+bb:
+  %add1 = add i32 %a, %b
+  %add2 = add i32 %a, %b
+  %add3 = add i32 %a, %b
+  %add4 = add i32 %a, %b
+  %ret1 = add i32 %add1, %add2
+  %ret2 = add i32 %add3, %add4
+  %ret = add i32 %ret1, %ret2
+  ret i32 %ret
+}
+
+define i32 @f2(i32 %a, i32 %b) {
+bb:
+  %add1 = add i32 %a, %b
+  %add2 = add i32 %a, %b
+  %ret = add i32 %add1, %add2
+  ret i32 %ret
+}
\ No newline at end of file