llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
lattice.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 <optional>
8 #include <llvm/ADT/APFloat.h>
9 #include <llvm/ADT/APInt.h>
10 #include <llvm/Support/raw_ostream.h>
11 
12 using APInt = llvm::APInt;
13 using APFloat = llvm::APFloat;
14 
15 class Global;
16 
17 
18 
22 class Lattice {
23 public:
25  enum class Kind {
27  UNKNOWN,
31  INT,
33  MASK,
35  FLOAT,
37  FLOAT_ZERO,
39  FRAME,
41  GLOBAL,
43  POINTER,
45  RANGE,
47  UNDEFINED,
48  };
49 
50 public:
51  Lattice(const Lattice &that);
52  ~Lattice();
53 
54  Kind GetKind() const { return kind_; }
55  bool IsUnknown() const { return GetKind() == Kind::UNKNOWN; }
56  bool IsOverdefined() const { return GetKind() == Kind::OVERDEFINED; }
57  bool IsUndefined() const { return GetKind() == Kind::UNDEFINED; }
58  bool IsInt() const { return GetKind() == Kind::INT; }
59  bool IsMask() const { return GetKind() == Kind::MASK; }
60  bool IsFloat() const { return GetKind() == Kind::FLOAT; }
61  bool IsFloatZero() const { return GetKind() == Kind::FLOAT_ZERO; }
62  bool IsGlobal() const { return GetKind() == Kind::GLOBAL; }
63  bool IsFrame() const { return GetKind() == Kind::FRAME; }
64  bool IsPointer() const { return GetKind() == Kind::POINTER; }
65  bool IsRange() const { return GetKind() == Kind::RANGE; }
66 
67  bool IsPointerLike() const { return IsPointer() || IsFrame() || IsGlobal(); }
68 
69  APInt GetInt() const { assert(IsInt()); return intVal_; }
70  APInt GetKnown() const { assert(IsMask()); return maskVal_.Known; }
71  APInt GetValue() const { assert(IsMask()); return maskVal_.Value; }
72  APFloat GetFloat() const { assert(IsFloat()); return floatVal_; }
73  unsigned GetFrameObject() const { assert(IsFrame()); return frameVal_.Obj; }
74  int64_t GetFrameOffset() const { assert(IsFrame()); return frameVal_.Off; }
75  Global *GetGlobalSymbol() const { assert(IsGlobal()); return globalVal_.Sym; }
76  int64_t GetGlobalOffset() const { assert(IsGlobal()); return globalVal_.Off; }
77  Global *GetRange() const { assert(IsRange()); return globalVal_.Sym; }
78 
79  bool IsTrue() const;
80  bool IsFalse() const;
81 
83  std::optional<APInt> AsInt() const
84  {
85  return IsInt() ? std::optional<APInt>(intVal_) : std::nullopt;
86  }
87 
89  std::optional<APFloat> AsFloat() const
90  {
91  return IsFloat() ? std::optional<APFloat>(floatVal_) : std::nullopt;
92  }
93 
95  bool operator != (const Lattice &that) const { return !(*this == that); }
96 
98  bool operator == (const Lattice &that) const;
99 
101  Lattice &operator = (const Lattice &that);
102 
104  Lattice LUB(const Lattice &that) const;
105 
106 public:
108  static Lattice Unknown();
110  static Lattice Overdefined();
112  static Lattice Undefined();
114  static Lattice Pointer();
116  static Lattice CreateFrame(unsigned obj, int64_t off);
118  static Lattice CreateGlobal(Global *g, int64_t Off = 0);
120  static Lattice CreateRange(Global *g);
122  static Lattice CreateInteger(int64_t i);
124  static Lattice CreateInteger(const APInt &i);
126  static Lattice CreateMask(const APInt &known, const APInt &values);
128  static Lattice CreateFloat(double f);
130  static Lattice CreateFloat(const APFloat &f);
132  static Lattice CreateFloatZero();
133 
134 private:
136  Lattice(Kind kind) : kind_(kind) {}
138  Kind kind_;
139 
141  union {
143  APInt intVal_;
145  APFloat floatVal_;
147  struct {
149  APInt Known;
151  APInt Value;
152  } maskVal_;
154  struct {
156  unsigned Obj;
158  int64_t Off;
159  } frameVal_;
161  struct {
165  int64_t Off;
166  } globalVal_;
167  };
168 };
169 
170 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Lattice &l);
Lattice::operator!=
bool operator!=(const Lattice &that) const
Checks if two values are not identical.
Definition: lattice.h:95
Lattice::maskVal_
struct Lattice::@11::@13 maskVal_
Bit mask value.
Lattice::intVal_
APInt intVal_
Integer value.
Definition: lattice.h:143
Lattice
Definition: lattice.h:22
Lattice::floatVal_
APFloat floatVal_
Double value.
Definition: lattice.h:145
Lattice::Kind
Kind
Enumeration of lattice value kinds.
Definition: lattice.h:25
Lattice::CreateRange
static Lattice CreateRange(Global *g)
Creates a global value.
Definition: lattice.cpp:369
Lattice::Undefined
static Lattice Undefined()
Creates an undefined value.
Definition: lattice.cpp:339
Lattice::Kind::FRAME
@ FRAME
Offset into the frame.
Lattice::CreateFrame
static Lattice CreateFrame(unsigned obj, int64_t off)
Creates a frame value.
Definition: lattice.cpp:351
Lattice::AsInt
std::optional< APInt > AsInt() const
Returns some integer, if the value is one.
Definition: lattice.h:83
Lattice::Kind::UNDEFINED
@ UNDEFINED
Constant, undefined.
Lattice::LUB
Lattice LUB(const Lattice &that) const
Least upper bound operator.
Definition: lattice.cpp:252
Lattice::globalVal_
struct Lattice::@11::@15 globalVal_
Global value.
Lattice::Pointer
static Lattice Pointer()
Creates a unknown pointer value.
Definition: lattice.cpp:345
Lattice::Kind::MASK
@ MASK
Integral bit mask.
Lattice::CreateInteger
static Lattice CreateInteger(int64_t i)
Creates an integral value from an integer.
Definition: lattice.cpp:377
Lattice::Known
APInt Known
Mask indicating the bits which have known values.
Definition: lattice.h:149
Lattice::operator=
Lattice & operator=(const Lattice &that)
Assigns a value to a lattice.
Definition: lattice.cpp:185
Lattice::Kind::INT
@ INT
Constant integer.
Lattice::Kind::GLOBAL
@ GLOBAL
Constant symbol with a potential offset.
Lattice::CreateFloat
static Lattice CreateFloat(double f)
Creates a floating value from a double.
Definition: lattice.cpp:400
Lattice::operator==
bool operator==(const Lattice &that) const
Checks if two values are identical.
Definition: lattice.cpp:145
Lattice::Kind::UNKNOWN
@ UNKNOWN
Top - value not encountered yet.
Lattice::Kind::OVERDEFINED
@ OVERDEFINED
Bot - value is not constant.
Lattice::Off
int64_t Off
Relative offset.
Definition: lattice.h:158
Lattice::CreateFloatZero
static Lattice CreateFloatZero()
Creates a float-point zero.
Definition: lattice.cpp:414
Lattice::Value
APInt Value
Mask indicating the values of those bits.
Definition: lattice.h:151
Lattice::Kind::FLOAT_ZERO
@ FLOAT_ZERO
Positive or negative float zero.
Lattice::CreateGlobal
static Lattice CreateGlobal(Global *g, int64_t Off=0)
Creates a global value.
Definition: lattice.cpp:360
Lattice::Unknown
static Lattice Unknown()
Creates an unknown value.
Definition: lattice.cpp:327
Lattice::Overdefined
static Lattice Overdefined()
Creates an overdefined value.
Definition: lattice.cpp:333
Lattice::Kind::RANGE
@ RANGE
Any offset into a pointer.
Lattice::frameVal_
struct Lattice::@11::@14 frameVal_
Frame value.
Lattice::CreateMask
static Lattice CreateMask(const APInt &known, const APInt &values)
Creates a mask value;.
Definition: lattice.cpp:391
Lattice::Obj
unsigned Obj
Object identifier.
Definition: lattice.h:156
Lattice::AsFloat
std::optional< APFloat > AsFloat() const
Returns some float, if the value is one.
Definition: lattice.h:89
Lattice::Kind::FLOAT
@ FLOAT
Constant floating-point.
Lattice::Kind::POINTER
@ POINTER
Pointer which is not null.
Lattice::Sym
Global * Sym
Base pointer.
Definition: lattice.h:163
Global
Definition: global.h:23