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

Sanjoy Das sanjoy at playingwithpointers.com
Fri Jun 19 16:20:32 PDT 2015


Author: sanjoy
Date: Fri Jun 19 18:20:31 2015
New Revision: 240191

URL: http://llvm.org/viewvc/llvm-project?rev=240191&view=rev
Log:
[CallGraph] Given -print-callgraph a stable printing order.

Summary:
Since FunctionMap has llvm::Function pointers as keys, the order in
which the traversal happens can differ from run to run, causing spurious
FileCheck failures.  Have CallGraph::print sort the CallGraphNodes by
name before printing them.

Reviewers: bogner, chandlerc

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D10575

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

Modified: llvm/trunk/lib/Analysis/IPA/CallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/IPA/CallGraph.cpp?rev=240191&r1=240190&r2=240191&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/IPA/CallGraph.cpp (original)
+++ llvm/trunk/lib/Analysis/IPA/CallGraph.cpp Fri Jun 19 18:20:31 2015
@@ -98,8 +98,26 @@ void CallGraph::print(raw_ostream &OS) c
     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)

Modified: llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll?rev=240191&r1=240190&r2=240191&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll (original)
+++ llvm/trunk/test/Analysis/CallGraph/non-leaf-intrinsics.ll Fri Jun 19 18:20:31 2015
@@ -25,8 +25,8 @@ entry:
 ; 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





More information about the llvm-commits mailing list