[llvm] [llvm-objdump] Add the --visualize-jumps option (PR #74858)

Nick Desaulniers via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 8 09:02:25 PST 2023


================
@@ -503,5 +506,194 @@ SourcePrinter::SourcePrinter(const object::ObjectFile *Obj,
   Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
 }
 
+// TODO Light/dark shades? 256-color terminals?
+const raw_ostream::Colors LineColors[] = {
+  raw_ostream::RED,
+  raw_ostream::GREEN,
+  raw_ostream::YELLOW,
+  raw_ostream::BLUE,
+  raw_ostream::MAGENTA,
+  raw_ostream::CYAN,
+};
+
+void ControlFlowPrinter::addEdge(uint64_t From, uint64_t To) {
+  auto It = Targets.find(To);
+  if (It == Targets.end())
+    It = Targets.insert(std::make_pair(To, ControlFlowTarget(To, PickColor()))).first;
+  It->second.addSource(From);
+}
+
+void ControlFlowPrinter::finalise() {
+  if (!enabled()) {
+    setControlFlowColumnWidth(0);
+    return;
+  }
+
+  SmallVector<ControlFlowTarget *> SortedTargets;
+  for (auto &[Addr, Info] : Targets) {
+    SortedTargets.push_back(&Info);
+  }
+  std::sort(SortedTargets.begin(), SortedTargets.end(),
+            [](ControlFlowTarget *LHS, ControlFlowTarget *RHS) {
+              return LHS->Length() < RHS->Length();
+            });
+
+  // FIXME This is O(n^3) in the worst case, can we do better?
+  for (auto &T : SortedTargets) {
+    int Column = 0;
+    for (;; ++Column)
+      if (!std::any_of(SortedTargets.begin(), SortedTargets.end(),
+                       [T, Column](ControlFlowTarget *T2) {
+                         return T != T2 && T2->Column == Column &&
+                                T->Overlaps(*T2);
+                       }))
+        break;
+    T->Column = Column;
+    MaxColumn = std::max(MaxColumn, Column);
+
+#if 1
+    LLVM_DEBUG(
+      dbgs() << "Target: 0x" << Twine::utohexstr(T->Target) << " (" << T->Length()
+             << ") Column " << Column << ":\n";
+      for (auto Source : T->Sources)
+        dbgs() << "  Source: 0x" << Twine::utohexstr(Source) << "\n";
+    );
+#endif
+  }
+
+  setControlFlowColumnWidth(MaxColumn * 2 + 4);
+}
+
+const char *ControlFlowPrinter::getLineChar(LineChar C) const {
+  bool IsASCII =
+      (OutputMode & VisualizeJumpsMode::CharsMask) == VisualizeJumpsMode::CharsASCII;
----------------
nickdesaulniers wrote:

nearly every reference to `OutputMode` is verbose, and a few of the lambdas it's captured in look redundant.  Consider making helper methods for these comparisons.  I suspect you can avoid the lambdas with such methods.

https://github.com/llvm/llvm-project/pull/74858


More information about the llvm-commits mailing list