|  | 
|  | 1 | +//===- bolt/Core/MCInstUtils.h ----------------------------------*- C++ -*-===// | 
|  | 2 | +// | 
|  | 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | +// See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
|  | 6 | +// | 
|  | 7 | +//===----------------------------------------------------------------------===// | 
|  | 8 | + | 
|  | 9 | +#ifndef BOLT_CORE_MCINSTUTILS_H | 
|  | 10 | +#define BOLT_CORE_MCINSTUTILS_H | 
|  | 11 | + | 
|  | 12 | +#include "bolt/Core/BinaryBasicBlock.h" | 
|  | 13 | + | 
|  | 14 | +#include <functional> | 
|  | 15 | +#include <map> | 
|  | 16 | +#include <variant> | 
|  | 17 | + | 
|  | 18 | +namespace llvm { | 
|  | 19 | +namespace bolt { | 
|  | 20 | + | 
|  | 21 | +class BinaryFunction; | 
|  | 22 | + | 
|  | 23 | +/// MCInstReference represents a reference to a constant MCInst as stored either | 
|  | 24 | +/// in a BinaryFunction (i.e. before a CFG is created), or in a BinaryBasicBlock | 
|  | 25 | +/// (after a CFG is created). | 
|  | 26 | +class MCInstReference { | 
|  | 27 | +  using nocfg_const_iterator = std::map<uint32_t, MCInst>::const_iterator; | 
|  | 28 | + | 
|  | 29 | +  // Two cases are possible: | 
|  | 30 | +  // * functions with CFG reconstructed - a function stores a collection of | 
|  | 31 | +  //   basic blocks, each basic block stores a contiguous vector of MCInst | 
|  | 32 | +  // * functions without CFG - there are no basic blocks created, | 
|  | 33 | +  //   the instructions are directly stored in std::map in BinaryFunction | 
|  | 34 | +  // | 
|  | 35 | +  // In both cases, the direct parent of MCInst is stored together with an | 
|  | 36 | +  // iterator pointing to the instruction. | 
|  | 37 | + | 
|  | 38 | +  // Helper struct: CFG is available, the direct parent is a basic block, | 
|  | 39 | +  // iterator's type is `MCInst *`. | 
|  | 40 | +  struct RefInBB { | 
|  | 41 | +    RefInBB(const BinaryBasicBlock *BB, const MCInst *Inst) | 
|  | 42 | +        : BB(BB), It(Inst) {} | 
|  | 43 | +    RefInBB(const RefInBB &Other) = default; | 
|  | 44 | +    RefInBB &operator=(const RefInBB &Other) = default; | 
|  | 45 | + | 
|  | 46 | +    const BinaryBasicBlock *BB; | 
|  | 47 | +    BinaryBasicBlock::const_iterator It; | 
|  | 48 | + | 
|  | 49 | +    bool operator<(const RefInBB &Other) const { | 
|  | 50 | +      if (BB != Other.BB) | 
|  | 51 | +        return std::less<const BinaryBasicBlock *>{}(BB, Other.BB); | 
|  | 52 | +      return It < Other.It; | 
|  | 53 | +    } | 
|  | 54 | + | 
|  | 55 | +    bool operator==(const RefInBB &Other) const { | 
|  | 56 | +      return BB == Other.BB && It == Other.It; | 
|  | 57 | +    } | 
|  | 58 | +  }; | 
|  | 59 | + | 
|  | 60 | +  // Helper struct: CFG is *not* available, the direct parent is a function, | 
|  | 61 | +  // iterator's type is std::map<uint32_t, MCInst>::iterator (the mapped value | 
|  | 62 | +  // is an instruction's offset). | 
|  | 63 | +  struct RefInBF { | 
|  | 64 | +    RefInBF(const BinaryFunction *BF, nocfg_const_iterator It) | 
|  | 65 | +        : BF(BF), It(It) {} | 
|  | 66 | +    RefInBF(const RefInBF &Other) = default; | 
|  | 67 | +    RefInBF &operator=(const RefInBF &Other) = default; | 
|  | 68 | + | 
|  | 69 | +    const BinaryFunction *BF; | 
|  | 70 | +    nocfg_const_iterator It; | 
|  | 71 | + | 
|  | 72 | +    bool operator<(const RefInBF &Other) const { | 
|  | 73 | +      if (BF != Other.BF) | 
|  | 74 | +        return std::less<const BinaryFunction *>{}(BF, Other.BF); | 
|  | 75 | +      return It->first < Other.It->first; | 
|  | 76 | +    } | 
|  | 77 | + | 
|  | 78 | +    bool operator==(const RefInBF &Other) const { | 
|  | 79 | +      return BF == Other.BF && It->first == Other.It->first; | 
|  | 80 | +    } | 
|  | 81 | +  }; | 
|  | 82 | + | 
|  | 83 | +  std::variant<RefInBB, RefInBF> Reference; | 
|  | 84 | + | 
|  | 85 | +  // Utility methods to be used like this: | 
|  | 86 | +  // | 
|  | 87 | +  //     if (auto *Ref = tryGetRefInBB()) | 
|  | 88 | +  //       return Ref->doSomething(...); | 
|  | 89 | +  //     return getRefInBF().doSomethingElse(...); | 
|  | 90 | +  const RefInBB *tryGetRefInBB() const { | 
|  | 91 | +    assert(std::get_if<RefInBB>(&Reference) || | 
|  | 92 | +           std::get_if<RefInBF>(&Reference)); | 
|  | 93 | +    return std::get_if<RefInBB>(&Reference); | 
|  | 94 | +  } | 
|  | 95 | +  const RefInBF &getRefInBF() const { | 
|  | 96 | +    assert(std::get_if<RefInBF>(&Reference)); | 
|  | 97 | +    return *std::get_if<RefInBF>(&Reference); | 
|  | 98 | +  } | 
|  | 99 | + | 
|  | 100 | +public: | 
|  | 101 | +  /// Constructs an empty reference. | 
|  | 102 | +  MCInstReference() : Reference(RefInBB(nullptr, nullptr)) {} | 
|  | 103 | +  /// Constructs a reference to the instruction inside the basic block. | 
|  | 104 | +  MCInstReference(const BinaryBasicBlock *BB, const MCInst *Inst) | 
|  | 105 | +      : Reference(RefInBB(BB, Inst)) { | 
|  | 106 | +    assert(BB && Inst && "Neither BB nor Inst should be nullptr"); | 
|  | 107 | +  } | 
|  | 108 | +  /// Constructs a reference to the instruction inside the basic block. | 
|  | 109 | +  MCInstReference(const BinaryBasicBlock *BB, unsigned Index) | 
|  | 110 | +      : Reference(RefInBB(BB, &BB->getInstructionAtIndex(Index))) { | 
|  | 111 | +    assert(BB && "Basic block should not be nullptr"); | 
|  | 112 | +  } | 
|  | 113 | +  /// Constructs a reference to the instruction inside the function without | 
|  | 114 | +  /// CFG information. | 
|  | 115 | +  MCInstReference(const BinaryFunction *BF, nocfg_const_iterator It) | 
|  | 116 | +      : Reference(RefInBF(BF, It)) { | 
|  | 117 | +    assert(BF && "Function should not be nullptr"); | 
|  | 118 | +  } | 
|  | 119 | + | 
|  | 120 | +  /// Locates an instruction inside a function and returns a reference. | 
|  | 121 | +  static MCInstReference get(const MCInst *Inst, const BinaryFunction &BF); | 
|  | 122 | + | 
|  | 123 | +  bool operator<(const MCInstReference &Other) const { | 
|  | 124 | +    return Reference < Other.Reference; | 
|  | 125 | +  } | 
|  | 126 | + | 
|  | 127 | +  bool operator==(const MCInstReference &Other) const { | 
|  | 128 | +    return Reference == Other.Reference; | 
|  | 129 | +  } | 
|  | 130 | + | 
|  | 131 | +  const MCInst &getMCInst() const { | 
|  | 132 | +    if (auto *Ref = tryGetRefInBB()) | 
|  | 133 | +      return *Ref->It; | 
|  | 134 | +    return getRefInBF().It->second; | 
|  | 135 | +  } | 
|  | 136 | + | 
|  | 137 | +  operator const MCInst &() const { return getMCInst(); } | 
|  | 138 | + | 
|  | 139 | +  operator bool() const { | 
|  | 140 | +    if (auto *Ref = tryGetRefInBB()) | 
|  | 141 | +      return Ref->BB != nullptr; | 
|  | 142 | +    return getRefInBF().BF != nullptr; | 
|  | 143 | +  } | 
|  | 144 | + | 
|  | 145 | +  bool hasCFG() const { | 
|  | 146 | +    return static_cast<bool>(*this) && tryGetRefInBB() != nullptr; | 
|  | 147 | +  } | 
|  | 148 | + | 
|  | 149 | +  const BinaryFunction *getFunction() const { | 
|  | 150 | +    if (auto *Ref = tryGetRefInBB()) | 
|  | 151 | +      return Ref->BB->getFunction(); | 
|  | 152 | +    return getRefInBF().BF; | 
|  | 153 | +  } | 
|  | 154 | + | 
|  | 155 | +  const BinaryBasicBlock *getBasicBlock() const { | 
|  | 156 | +    if (auto *Ref = tryGetRefInBB()) | 
|  | 157 | +      return Ref->BB; | 
|  | 158 | +    return nullptr; | 
|  | 159 | +  } | 
|  | 160 | + | 
|  | 161 | +  raw_ostream &print(raw_ostream &OS) const; | 
|  | 162 | +}; | 
|  | 163 | + | 
|  | 164 | +static inline raw_ostream &operator<<(raw_ostream &OS, | 
|  | 165 | +                                      const MCInstReference &Ref) { | 
|  | 166 | +  return Ref.print(OS); | 
|  | 167 | +} | 
|  | 168 | + | 
|  | 169 | +} // namespace bolt | 
|  | 170 | +} // namespace llvm | 
|  | 171 | + | 
|  | 172 | +#endif | 
0 commit comments