aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/TargetBranchInfo.h
blob: 39c51222a2d367a99f39a0640192bf3240b4bfdd (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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_TARGETBRANCHINFO_H
#define RCC_CODEGEN_TARGETBRANCHINFO_H

#include "rcc/Support/Error.h"

#include <cstdint>
#include <optional>

namespace rcc {

class MachineBasicBlock;
class MachineFunction;
class MachineInstr;
class TargetDescription;

struct MachineBranchAnalysis final {
  MachineInstr *Conditional = nullptr;
  MachineInstr *Unconditional = nullptr;
  MachineBasicBlock *TrueTarget = nullptr;
  MachineBasicBlock *FalseTarget = nullptr;
  bool HasUnanalyzableTerminator = false;
};

class TargetBranchInfo {
public:
  virtual ~TargetBranchInfo() = default;

  [[nodiscard]] virtual Expected<MachineBranchAnalysis>
  analyzeBranch(MachineBasicBlock &Block, MachineFunction &Function,
                const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<void>
  reverseBranchCondition(MachineInstr &Branch, MachineBasicBlock &NewTarget,
                         MachineFunction &Function, const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<void> removeBranch(MachineInstr &Branch, MachineFunction &Function,
                                                    const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<bool>
  rewriteBranchTarget(MachineBasicBlock &Block, MachineBasicBlock &OldTarget,
                      MachineBasicBlock &NewTarget, MachineFunction &Function,
                      const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<MachineInstr *>
  insertUnconditionalBranch(MachineBasicBlock &Block, MachineBasicBlock &TargetBlock,
                            MachineFunction &Function, const TargetDescription &Target) const = 0;

  [[nodiscard]] virtual Expected<std::uint32_t>
  instructionSize(const MachineInstr &Instruction, const MachineFunction &Function,
                  const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<MachineBasicBlock *>
  branchTarget(MachineInstr &Instruction, MachineFunction &Function,
               const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<std::optional<bool>>
  evaluateBranchCondition(const MachineInstr &, const MachineFunction &,
                          const TargetDescription &) const {
    return std::optional<bool>{};
  }
  [[nodiscard]] virtual bool isBranchOffsetInRange(const MachineInstr &Instruction,
                                                   std::int64_t Offset) const noexcept = 0;
  [[nodiscard]] virtual Expected<bool> relaxBranch(MachineInstr &Instruction, std::int64_t Offset,
                                                   MachineFunction &Function,
                                                   const TargetDescription &Target) const = 0;
};

} // namespace rcc

#endif // RCC_CODEGEN_TARGETBRANCHINFO_H