aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineFunctionAttributes.h
blob: 4344640cb3770dc0ebbb9025b9f972db245be468 (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
95
96
97
98
99
100
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEFUNCTIONATTRIBUTES_H
#define RCC_CODEGEN_MACHINEFUNCTIONATTRIBUTES_H

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

#include <cstdint>
#include <optional>
#include <string_view>

namespace rcc {

enum class MachineFunctionAttribute : std::uint8_t {
  ExposesReturnsTwice,
  HasWinCFI,
  HasFakeUses,
  CallsEHReturn,
  CallsUnwindInit,
  HasEHContTarget,
  HasEHScopes,
  HasEHFunclets,
  IsOutlined,
  DebugInstrRef,
  TracksDebugUserValues,
  Count,
};

[[nodiscard]] std::string_view
getMachineFunctionAttributeName(MachineFunctionAttribute Attribute) noexcept;

class MachineFunctionAttributes final {
public:
  constexpr MachineFunctionAttributes() noexcept = default;

  [[nodiscard]] static Expected<MachineFunctionAttributes> fromRawFlags(std::uint16_t RawFlags);

  [[nodiscard]] constexpr bool has(MachineFunctionAttribute Attribute) const noexcept {
    if (!isValidAttribute(Attribute))
      return false;
    return (Flags & flagFor(Attribute)) != 0;
  }

  constexpr void set(MachineFunctionAttribute Attribute) noexcept {
    if (isValidAttribute(Attribute))
      Flags |= flagFor(Attribute);
  }

  constexpr void clear(MachineFunctionAttribute Attribute) noexcept {
    if (isValidAttribute(Attribute))
      Flags &= static_cast<std::uint16_t>(~flagFor(Attribute));
  }

  [[nodiscard]] constexpr std::uint16_t rawFlags() const noexcept {
    return Flags;
  }

  [[nodiscard]] std::optional<Alignment> alignment() const noexcept {
    return FunctionAlignment;
  }

  void setAlignment(Alignment NewAlignment) noexcept {
    FunctionAlignment = NewAlignment;
  }

  void clearAlignment() noexcept {
    FunctionAlignment.reset();
  }

  [[nodiscard]] Expected<void> verify() const;

  friend bool operator==(const MachineFunctionAttributes &Left,
                         const MachineFunctionAttributes &Right) = default;

private:
  static constexpr std::uint16_t AttributeCount =
      static_cast<std::uint16_t>(MachineFunctionAttribute::Count);
  static_assert(AttributeCount < 16);

  static constexpr std::uint16_t ValidFlags =
      static_cast<std::uint16_t>((std::uint16_t{1} << AttributeCount) - 1);

  [[nodiscard]] static constexpr bool
  isValidAttribute(MachineFunctionAttribute Attribute) noexcept {
    return static_cast<std::uint16_t>(Attribute) < AttributeCount;
  }

  [[nodiscard]] static constexpr std::uint16_t
  flagFor(MachineFunctionAttribute Attribute) noexcept {
    return static_cast<std::uint16_t>(std::uint16_t{1} << static_cast<std::uint16_t>(Attribute));
  }

  std::uint16_t Flags = 0;
  std::optional<Alignment> FunctionAlignment;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEFUNCTIONATTRIBUTES_H