MLIR 23.0.0git
InferIntDivisibilityOpInterface.h
Go to the documentation of this file.
1//===- InferIntDivisibilityOpInterface.h - Integer Divisibility -*- C++ -*-===//
2//
3// Part of the LLVM 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// This file contains definitions of the integer divisibility inference
10// interface defined in `InferIntDivisibilityOpInterface.td`.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_INTERFACES_INFERINTDIVISIBILITYOPINTERFACE_H
15#define MLIR_INTERFACES_INFERINTDIVISIBILITYOPINTERFACE_H
16
18#include <numeric>
19#include <optional>
20
21namespace mlir {
22
23/// Statically known divisibility information for an integer SSA value.
24/// Tracks separate divisors for the unsigned and signed interpretations of
25/// the value so that subsequent analyses can use whichever is more precise.
27public:
29 ConstantIntDivisibility(uint64_t udiv, uint64_t sdiv)
30 : udivVal(udiv), sdivVal(sdiv) {}
31
32 bool operator==(const ConstantIntDivisibility &other) const {
33 return udivVal == other.udivVal && sdivVal == other.sdivVal;
34 }
35
36 uint64_t udiv() const { return this->udivVal; }
37 uint64_t sdiv() const { return this->sdivVal; }
38
39 // Returns the union (computed separately for signed and unsigned bounds)
40 // for this divisibility and `other`.
43 /*udiv=*/std::gcd(udiv(), other.udiv()),
44 /*sdiv=*/std::gcd(sdiv(), other.sdiv()));
45 }
46
47private:
48 uint64_t udivVal;
49 uint64_t sdivVal;
50
53};
54
57 os << "ConstantIntDivisibility(udiv = " << div.udivVal
58 << ", sdiv = " << div.sdivVal << ")";
59 return os;
60}
61
62/// This lattice value represents the integer divisibility of an SSA value.
64public:
66 : value(std::move(value)) {}
68 std::optional<ConstantIntDivisibility> value = std::nullopt)
69 : value(std::move(value)) {}
70 // Gets the minimum divisibility of 1 that is used to indicate that the value
71 // cannot be analyzed further.
75
76 bool isUninitialized() const { return !value.has_value(); }
78 assert(!isUninitialized());
79 return *value;
80 }
81
82 bool operator==(const IntegerDivisibility &rhs) const {
83 return value == rhs.value;
84 }
85
87 const IntegerDivisibility &rhs) {
88 if (lhs.isUninitialized()) {
89 return rhs;
90 }
91 if (rhs.isUninitialized()) {
92 return lhs;
93 }
94 return IntegerDivisibility(lhs.getValue().getUnion(rhs.getValue()));
95 }
96
97 void print(raw_ostream &os) const { os << value; }
98
99private:
100 std::optional<ConstantIntDivisibility> value;
101};
102
104 const IntegerDivisibility &div) {
105 div.print(os);
106 return os;
107}
108
109/// The type of the `setResultDivs` callback provided to ops implementing
110/// InferIntDivisibilityOpInterface. It should be called once for each integer
111/// result value and be passed the ConstantIntDivisibility corresponding to
112/// that value.
115
116} // end namespace mlir
117
118#include "mlir/Interfaces/InferIntDivisibilityOpInterface.h.inc"
119
120#endif // MLIR_INTERFACES_INFERINTDIVISIBILITYOPINTERFACE_H
lhs
#define div(a, b)
Statically known divisibility information for an integer SSA value.
bool operator==(const ConstantIntDivisibility &other) const
friend raw_ostream & operator<<(raw_ostream &os, const ConstantIntDivisibility &div)
ConstantIntDivisibility(uint64_t udiv, uint64_t sdiv)
ConstantIntDivisibility getUnion(const ConstantIntDivisibility &other) const
This lattice value represents the integer divisibility of an SSA value.
static IntegerDivisibility join(const IntegerDivisibility &lhs, const IntegerDivisibility &rhs)
IntegerDivisibility(std::optional< ConstantIntDivisibility > value=std::nullopt)
IntegerDivisibility(ConstantIntDivisibility value)
bool operator==(const IntegerDivisibility &rhs) const
const ConstantIntDivisibility & getValue() const
static IntegerDivisibility getMinDivisibility()
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Include the generated interface declarations.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
llvm::function_ref< void(Value, const ConstantIntDivisibility &)> SetIntDivisibilityFn
The type of the setResultDivs callback provided to ops implementing InferIntDivisibilityOpInterface.