aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineFunctionCloner.h
blob: 658614f84746cbe0159410fd8714201ac49ec088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEFUNCTIONCLONER_H
#define RCC_CODEGEN_MACHINEFUNCTIONCLONER_H

#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/Register.h"
#include "rcc/CodeGen/StableID.h"
#include "rcc/Support/Error.h"

#include <memory>
#include <optional>
#include <string>
#include <unordered_map>

namespace rcc {

class InstructionSet;
class MachineBasicBlock;
class MachineFunction;
class MachineInstr;

struct MachineFunctionCloneOptions final {
  MachineFunctionStableID NewStableID;
  std::optional<std::string> NewName;
};

struct MachineFunctionCloneResult final {
  MachineFunctionCloneResult();
  MachineFunctionCloneResult(const MachineFunctionCloneResult &) = delete;
  MachineFunctionCloneResult &operator=(const MachineFunctionCloneResult &) = delete;
  MachineFunctionCloneResult(MachineFunctionCloneResult &&) noexcept;
  MachineFunctionCloneResult &operator=(MachineFunctionCloneResult &&) noexcept;
  ~MachineFunctionCloneResult();

  std::unique_ptr<MachineFunction> ClonedFunction;
  std::unordered_map<MachineBlockStableID, MachineBlockStableID> BlockStableIDs;
  std::unordered_map<MachineInstrStableID, MachineInstrStableID> InstructionStableIDs;
  std::unordered_map<Register, Register> VirtualRegisters;
  std::unordered_map<FrameIndex, FrameIndex> FrameIndices;
  std::unordered_map<ConstantPoolIndex, ConstantPoolIndex> ConstantPoolIndices;
  std::unordered_map<JumpTableIndex, JumpTableIndex> JumpTableIndices;
};

class MachineFunctionCloneContext final {
public:
  [[nodiscard]] const MachineFunction &sourceFunction() const noexcept {
    return Source;
  }

  [[nodiscard]] MachineFunction &destinationFunction() const noexcept {
    return Destination;
  }

  [[nodiscard]] Expected<MachineBasicBlock *> mapBlock(const MachineBasicBlock &SourceBlock) const;
  [[nodiscard]] Expected<MachineInstr *>
  mapInstruction(const MachineInstr &SourceInstruction) const;
  [[nodiscard]] Expected<MachineBlockStableID>
  mapBlockStableID(MachineBlockStableID SourceID) const;
  [[nodiscard]] Expected<MachineInstrStableID>
  mapInstructionStableID(MachineInstrStableID SourceID) const;
  [[nodiscard]] Expected<Register> mapRegister(Register SourceRegister) const;
  [[nodiscard]] Expected<FrameIndex> mapFrameIndex(FrameIndex SourceIndex) const;
  [[nodiscard]] Expected<ConstantPoolIndex>
  mapConstantPoolIndex(ConstantPoolIndex SourceIndex) const;
  [[nodiscard]] Expected<JumpTableIndex> mapJumpTableIndex(JumpTableIndex SourceIndex) const;

private:
  MachineFunctionCloneContext(
      const MachineFunction &Source, MachineFunction &Destination,
      const MachineFunctionCloneResult &Result,
      const std::unordered_map<const MachineBasicBlock *, MachineBasicBlock *> &Blocks,
      const std::unordered_map<const MachineInstr *, MachineInstr *> &Instructions) noexcept
      : Source(Source), Destination(Destination), Result(Result), Blocks(Blocks),
        Instructions(Instructions) {}

  const MachineFunction &Source;
  MachineFunction &Destination;
  const MachineFunctionCloneResult &Result;
  const std::unordered_map<const MachineBasicBlock *, MachineBasicBlock *> &Blocks;
  const std::unordered_map<const MachineInstr *, MachineInstr *> &Instructions;

  friend Expected<MachineFunctionCloneResult>
  cloneMachineFunction(const MachineFunction &Source, const InstructionSet &Instructions,
                       MachineFunctionCloneOptions Options);
};

[[nodiscard]] Expected<MachineFunctionCloneResult>
cloneMachineFunction(const MachineFunction &Source, const InstructionSet &Instructions,
                     MachineFunctionCloneOptions Options);

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEFUNCTIONCLONER_H