[llvm-branch-commits] [llvm] [BOLT] Factor out MCInstReference from gadget scanner (NFC) (PR #138655)
Maksim Panchenko via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed May 28 11:38:05 PDT 2025
================
@@ -0,0 +1,168 @@
+//===- bolt/Core/MCInstUtils.h ----------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef BOLT_CORE_MCINSTUTILS_H
+#define BOLT_CORE_MCINSTUTILS_H
+
+#include "bolt/Core/BinaryBasicBlock.h"
+
+#include <map>
+#include <tuple>
+#include <variant>
+
+namespace llvm {
+namespace bolt {
+
+class BinaryFunction;
+
+/// MCInstReference represents a reference to a constant MCInst as stored either
+/// in a BinaryFunction (i.e. before a CFG is created), or in a BinaryBasicBlock
+/// (after a CFG is created).
+class MCInstReference {
+ using nocfg_const_iterator = std::map<uint32_t, MCInst>::const_iterator;
+
+ // Two cases are possible:
+ // * functions with CFG reconstructed - a function stores a collection of
+ // basic blocks, each basic block stores a contiguous vector of MCInst
+ // * functions without CFG - there are no basic blocks created,
+ // the instructions are directly stored in std::map in BinaryFunction
+ //
+ // In both cases, the direct parent of MCInst is stored together with an
+ // iterator pointing to the instruction.
+
+ // Helper struct: CFG is available, the direct parent is a basic block,
+ // iterator's type is `MCInst *`.
+ struct RefInBB {
+ RefInBB(const BinaryBasicBlock *BB, const MCInst *Inst)
+ : BB(BB), It(Inst) {}
+ RefInBB(const RefInBB &Other) = default;
+ RefInBB &operator=(const RefInBB &Other) = default;
+
+ const BinaryBasicBlock *BB;
+ BinaryBasicBlock::const_iterator It;
+
+ bool operator<(const RefInBB &Other) const {
----------------
maksfb wrote:
What are expected uses for this comparison? I'm concerned about non-deterministic order of `BinaryBasicBlock *`.
https://github.com/llvm/llvm-project/pull/138655
More information about the llvm-branch-commits
mailing list