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

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 13 08:29:35 PST 2023


================
@@ -166,6 +169,114 @@ class SourcePrinter {
                                StringRef Delimiter = "; ");
 };
 
+enum VisualizeJumpsMode : int {
+  Off = 0,
+
+  ColorAuto = 0x1,
+  Color3Bit,
+  //Color8Bit, // TODO GNU objdump can use more colors in 256-color terminals
+
+  CharsASCII = 0x10,
+  CharsUnicode = 0x20,
+
+  ColorMask = 0xf,
+  CharsMask = 0xf0,
+};
+
+
+extern const raw_ostream::Colors LineColors[6];
+
+class ControlFlowPrinter {
+  struct ControlFlowTarget {
+    uint64_t Target;
+    SmallVector<uint64_t, 4> Sources;
+    int Column;
+    raw_ostream::Colors Color;
+
+    ControlFlowTarget(uint64_t Target, raw_ostream::Colors Color)
+        : Target(Target), Column(~0U), Color(Color), High(Target), Low(Target) {}
+    ControlFlowTarget(const ControlFlowTarget &) = delete;
+    ControlFlowTarget(ControlFlowTarget &&) = default;
+
+    void addSource(uint64_t Source) {
+      Sources.push_back(Source);
+      Low = std::min(Low, Source);
+      High = std::max(High, Source);
+    }
+
+    uint64_t Length() const { return High - Low; }
+
+    bool Overlaps(ControlFlowTarget &Other) const {
+      return !(Other.Low > High || Other.High < Low);
+    }
+
+    bool ActiveAt(uint64_t Addr, bool BeforeInst = false,
+                  bool AfterInst = false) const {
+      if (BeforeInst)
+        return Addr > Low && Addr <= High;
+      else if (AfterInst)
+        return Addr >= Low && Addr < High;
+      else
+        return Addr >= Low && Addr <= High;
+    }
+
+    bool StartsAt(uint64_t Addr) const { return Addr == Low; }
+    bool EndsAt(uint64_t Addr) const { return Addr == High; }
+    bool TargetAt(uint64_t Addr) const { return Addr == Target; }
+
+    bool HorizontalAt(uint64_t Addr) const {
+      return Addr == Target ||
+             std::any_of(Sources.begin(), Sources.end(),
+                         [Addr](uint64_t Src) { return Src == Addr; });
+    }
+
+  private:
+    uint64_t High, Low;
+  };
+
+  VisualizeJumpsMode OutputMode;
+  DenseMap<uint64_t, ControlFlowTarget> Targets;
+  int MaxColumn;
+  const MCSubtargetInfo &STI;
+
+  int NextColorIdx;
+  raw_ostream::Colors PickColor() {
+    if ((OutputMode & VisualizeJumpsMode::ColorMask) ==
+        VisualizeJumpsMode::Off)
+      return raw_ostream::RESET;
+    auto Ret = LineColors[NextColorIdx];
+    NextColorIdx = (NextColorIdx + 1) % (sizeof(LineColors) / sizeof(LineColors[0]));
+    return Ret;
+  }
----------------
ostannard wrote:

Done

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


More information about the llvm-commits mailing list