llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
masked_type.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <cstdint>
8 
9 #include <optional>
10 
11 #include <llvm/Support/raw_ostream.h>
12 
13 
14 
15 namespace tags {
16 
20 class MaskedType {
21 public:
22  MaskedType(uint64_t value);
23  MaskedType(uint64_t value, uint64_t known);
24 
25  MaskedType operator+(const MaskedType &that) const;
26  MaskedType operator-(const MaskedType &that) const;
27  MaskedType operator&(const MaskedType &that) const;
28  MaskedType operator|(const MaskedType &that) const;
29  MaskedType operator^(const MaskedType &that) const;
30  MaskedType operator~() const;
31 
32  bool operator==(const MaskedType &that) const
33  {
34  return value_ == that.value_ && known_ == that.known_;
35  }
36 
37  void dump(llvm::raw_ostream &os) const;
38 
39  uint64_t GetValue() const { return value_; }
40  uint64_t GetKnown() const { return known_; }
41 
42  std::optional<int64_t> AsConst() const
43  {
44  if (known_ == static_cast<uint64_t>(-1)) {
45  return value_;
46  } else {
47  return std::nullopt;
48  }
49  }
50 
51  bool IsAligned() const
52  {
53  return (known_ & 7) == 7 && (value_ & 7) == 0;
54  }
55 
56 private:
57  uint64_t value_;
58  uint64_t known_;
59 };
60 
61 }
62 
63 // ----------------------------------------------------------------------------
64 inline llvm::raw_ostream &
65 operator<<(llvm::raw_ostream &os, const tags::MaskedType &m)
66 {
67  m.dump(os);
68  return os;
69 }
70 
tags::MaskedType
Definition: masked_type.h:20