[PATCH] [CallGraph] Given -print-callgraph a stable printing order.

Sanjoy Das sanjoy at playingwithpointers.com
Fri Jun 19 16:24:53 PDT 2015


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D10575

Files:
  llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
  llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll

Index: llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll
===================================================================
--- llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll
+++ llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll
@@ -25,8 +25,8 @@
 ; CHECK: Call graph node <<null function>>
 ; CHECK:  CS<0x0> calls function 'f'
 
-; CHECK: Call graph node for function: 'calls_statepoint'
-; CHECK-NEXT:  CS<[[addr_0:[^>]+]]> calls external node
-
 ; CHECK: Call graph node for function: 'calls_patchpoint'
 ; CHECK-NEXT:  CS<[[addr_1:[^>]+]]> calls external node
+
+; CHECK: Call graph node for function: 'calls_statepoint'
+; CHECK-NEXT:  CS<[[addr_0:[^>]+]]> calls external node
Index: llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
===================================================================
--- llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
+++ llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
@@ -98,8 +98,26 @@
     OS << "<<null function: 0x" << Root << ">>\n";
   }
 
-  for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
-    I->second->print(OS);
+  // Print in a deterministic order by sorting CallGraphNodes by name.  We do
+  // this here to avoid slowing down the non-printing fast path.
+
+  SmallVector<CallGraphNode *, 16> Nodes;
+  Nodes.reserve(FunctionMap.size());
+
+  for (auto I = begin(), E = end(); I != E; ++I)
+    Nodes.push_back(I->second);
+
+  std::sort(Nodes.begin(), Nodes.end(),
+            [](CallGraphNode *LHS, CallGraphNode *RHS) {
+    if (Function *LF = LHS->getFunction())
+      if (Function *RF = RHS->getFunction())
+        return LF->getName() < RF->getName();
+
+    return RHS->getFunction() != nullptr;
+  });
+
+  for (CallGraphNode *CN : Nodes)
+    CN->print(OS);
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10575.28061.patch
Type: text/x-patch
Size: 1812 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150619/bda048a2/attachment.bin>


More information about the llvm-commits mailing list