[llvm] [llvm-objdump] Add the --visualize-jumps option (PR #74858)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 14 01:01:43 PST 2023
================
@@ -166,6 +169,115 @@ class SourcePrinter {
StringRef Delimiter = "; ");
};
+struct VisualizeJumpsMode {
+ enum Chars_t { Off, ASCII, Unicode };
+ enum Colors_t { BlackAndWhite, ThreeBit, Auto };
+
+ Chars_t Chars;
+ Colors_t Colors;
+
+ VisualizeJumpsMode() : Chars(Off), Colors(BlackAndWhite) {}
+ VisualizeJumpsMode(Chars_t Chars, Colors_t Colors)
+ : Chars(Chars), Colors(Colors) {}
+
+ static VisualizeJumpsMode GetDefault() {
+ return VisualizeJumpsMode(Unicode, Auto);
+ }
+
+ bool enabled() const { return Chars != Off; }
+ bool color_enabled() const { return enabled() && Colors != BlackAndWhite; }
+ bool unicode_enabled() const { return Chars == Unicode; }
+
+ void ResolveAutoColor(raw_ostream &OS) {
+ if (Colors == Auto)
+ Colors = OS.has_colors() ? ThreeBit : BlackAndWhite;
+ }
+};
+
+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;
----------------
jh7370 wrote:
Same question: why `int` not `size_t`?
https://github.com/llvm/llvm-project/pull/74858
More information about the llvm-commits
mailing list