My Project
SDBM.h
Go to the documentation of this file.
1 //===- SDBM.h - MLIR SDBM declaration ---------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A striped difference-bound matrix (SDBM) is a set in Z^N (or R^N) defined
10 // as {(x_1, ... x_n) | f(x_1, ... x_n) >= 0} where f is an SDBM expression.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_SDBM_SDBM_H
15 #define MLIR_DIALECT_SDBM_SDBM_H
16 
17 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/DenseMap.h"
19 
20 namespace mlir {
21 
22 class MLIRContext;
23 class SDBMDialect;
24 class SDBMExpr;
25 class SDBMTermExpr;
26 
35 class IntInfty {
36 public:
37  constexpr static int64_t infty = std::numeric_limits<int64_t>::max();
38 
39  /*implicit*/ IntInfty(int64_t v) : value(v) {}
40 
41  IntInfty &operator=(int64_t v) {
42  value = v;
43  return *this;
44  }
45 
46  static IntInfty infinity() { return IntInfty(infty); }
47 
48  int64_t getValue() const { return value; }
49  explicit operator int64_t() const { return value; }
50 
51  bool isFinite() { return value != infty; }
52 
53 private:
54  int64_t value;
55 };
56 
57 inline IntInfty operator+(IntInfty lhs, IntInfty rhs) {
58  if (!lhs.isFinite() || !rhs.isFinite())
59  return IntInfty::infty;
60 
61  // Check for overflows, treating the sum of two values adding up to INT_MAX as
62  // overflow. Convert values to unsigned to get an extra bit and avoid the
63  // undefined behavior of signed integer overflows.
64  assert((lhs.getValue() <= 0 || rhs.getValue() <= 0 ||
65  static_cast<uint64_t>(lhs.getValue()) +
66  static_cast<uint64_t>(rhs.getValue()) <
67  static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) &&
68  "IntInfty overflow");
69  // Check for underflows by converting values to unsigned to avoid undefined
70  // behavior of signed integers perform the addition (bitwise result is same
71  // because numbers are required to be two's complement in C++) and check if
72  // the sign bit remains negative.
73  assert((lhs.getValue() >= 0 || rhs.getValue() >= 0 ||
74  ((static_cast<uint64_t>(lhs.getValue()) +
75  static_cast<uint64_t>(rhs.getValue())) >>
76  63) == 1) &&
77  "IntInfty underflow");
78 
79  return lhs.getValue() + rhs.getValue();
80 }
81 
82 inline bool operator<(IntInfty lhs, IntInfty rhs) {
83  return lhs.getValue() < rhs.getValue();
84 }
85 
86 inline bool operator<=(IntInfty lhs, IntInfty rhs) {
87  return lhs.getValue() <= rhs.getValue();
88 }
89 
90 inline bool operator==(IntInfty lhs, IntInfty rhs) {
91  return lhs.getValue() == rhs.getValue();
92 }
93 
94 inline bool operator!=(IntInfty lhs, IntInfty rhs) { return !(lhs == rhs); }
95 
98 class SDBM {
99 public:
102  static SDBM get(ArrayRef<SDBMExpr> inequalities,
103  ArrayRef<SDBMExpr> equalities);
104 
105  void getSDBMExpressions(SDBMDialect *dialect,
106  SmallVectorImpl<SDBMExpr> &inequalities,
107  SmallVectorImpl<SDBMExpr> &equalities);
108 
109  void print(raw_ostream &os);
110  void dump();
111 
112  IntInfty operator()(int i, int j) { return at(i, j); }
113 
114 private:
118  IntInfty &at(int i, int j) { return matrix[i * getNumVariables() + j]; }
119 
124  void convertDBMElement(unsigned row, unsigned col, SDBMTermExpr rowExpr,
125  SDBMTermExpr colExpr,
126  SmallVectorImpl<SDBMExpr> &inequalities,
127  SmallVectorImpl<SDBMExpr> &equalities);
128 
131  void convertDBMDiagonalElement(unsigned pos, SDBMTermExpr expr,
132  SmallVectorImpl<SDBMExpr> &inequalities);
133 
135  unsigned getNumVariables() const {
136  return 1 + numDims + numSymbols + numTemporaries;
137  }
138 
140  unsigned getDimPosition(unsigned position) const { return 1 + position; }
141 
143  unsigned getSymbolPosition(unsigned position) const {
144  return 1 + numDims + position;
145  }
146 
148  unsigned getTemporaryPosition(unsigned position) const {
149  return 1 + numDims + numSymbols + position;
150  }
151 
153  unsigned numDims;
155  unsigned numSymbols;
157  unsigned numTemporaries;
158 
184  std::vector<IntInfty> matrix;
185 
192  DenseMap<unsigned, SDBMExpr> stripeToPoint;
193 };
194 
195 } // namespace mlir
196 
197 #endif // MLIR_DIALECT_SDBM_SDBM_H
Definition: InferTypeOpInterface.cpp:20
IntInfty operator()(int i, int j)
Definition: SDBM.h:112
Definition: LLVM.h:48
bool operator!=(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:94
IntInfty operator+(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:57
Definition: LLVM.h:34
Definition: LLVM.h:37
IntInfty & operator=(int64_t v)
Definition: SDBM.h:41
int64_t getValue() const
Definition: SDBM.h:48
static IntInfty infinity()
Definition: SDBM.h:46
bool operator==(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:90
bool operator<(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:82
Definition: SDBM.h:35
IntInfty(int64_t v)
Definition: SDBM.h:39
static constexpr int64_t infty
Definition: SDBM.h:37
bool isFinite()
Definition: SDBM.h:51
void print(OpAsmPrinter &p, AffineIfOp op)
Definition: AffineOps.cpp:1671
Definition: SDBMDialect.h:18
Definition: SDBM.h:98
bool operator<=(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:86
Definition: SDBMExpr.h:221