[llvm] [BOLT] Refactor MCInstReference and move it to Core (NFC) (PR #155846)
Anatoly Trosinenko via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 23 06:20:20 PDT 2025
================
@@ -0,0 +1,175 @@
+//===- 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 <variant>
+
+namespace llvm {
+class MCCodeEmitter;
+}
+
+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).
+///
+/// The reference may be invalidated when the function containing the referenced
+/// instruction is modified.
+class MCInstReference {
+public:
+ using nocfg_const_iterator = std::map<uint32_t, MCInst>::const_iterator;
+
+ /// Constructs an empty reference.
+ MCInstReference() : Reference(RefInBB(nullptr, /*Index=*/0)) {}
+ /// Constructs a reference to the instruction inside the basic block.
+ MCInstReference(const BinaryBasicBlock *BB, const MCInst *Inst)
----------------
atrosinenko wrote:
Thank you for pointing this out, I replaced a number of pointer-typed arguments with references in 0558f1b3892087c43d8d2659cddc9aca6f3ef479 and now it should be clearer to the user when empty references can be returned (the privately-used `RefInBB` and `RefInBF` helper classes still use pointers, though). Furthermore, I made `MCInstReference::get` accept both arguments by reference in 5a7414cf1b7ce5883af02cd6ab886614ed84f5c7: while it could have sense to return empty reference for `(nullptr, SomeFunction)` arguments (but even this usage is slightly questionable), it is definitely strange to pass unrelated instruction and function and expect the `MCInstReference::get` to silently return an empty reference.
https://github.com/llvm/llvm-project/pull/155846
More information about the llvm-commits
mailing list