LLVM 20.0.0git
InstructionCost.h
Go to the documentation of this file.
1//===- InstructionCost.h ----------------------------------------*- 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/// \file
9/// This file defines an InstructionCost class that is used when calculating
10/// the cost of an instruction, or a group of instructions. In addition to a
11/// numeric value representing the cost the class also contains a state that
12/// can be used to encode particular properties, such as a cost being invalid.
13/// Operations on InstructionCost implement saturation arithmetic, so that
14/// accumulating costs on large cost-values don't overflow.
15///
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_SUPPORT_INSTRUCTIONCOST_H
19#define LLVM_SUPPORT_INSTRUCTIONCOST_H
20
22#include <limits>
23#include <optional>
24
25namespace llvm {
26
27class raw_ostream;
28
30public:
31 using CostType = int64_t;
32
33 /// CostState describes the state of a cost.
34 enum CostState {
35 Valid, /// < The cost value represents a valid cost, even when the
36 /// cost-value is large.
37 Invalid /// < Invalid indicates there is no way to represent the cost as a
38 /// numeric value. This state exists to represent a possible issue,
39 /// e.g. if the cost-model knows the operation cannot be expanded
40 /// into a valid code-sequence by the code-generator. While some
41 /// passes may assert that the calculated cost must be valid, it is
42 /// up to individual passes how to interpret an Invalid cost. For
43 /// example, a transformation pass could choose not to perform a
44 /// transformation if the resulting cost would end up Invalid.
45 /// Because some passes may assert a cost is Valid, it is not
46 /// recommended to use Invalid costs to model 'Unknown'.
47 /// Note that Invalid is semantically different from a (very) high,
48 /// but valid cost, which intentionally indicates no issue, but
49 /// rather a strong preference not to select a certain operation.
50 };
51
52private:
53 CostType Value = 0;
54 CostState State = Valid;
55
56 void propagateState(const InstructionCost &RHS) {
57 if (RHS.State == Invalid)
58 State = Invalid;
59 }
60
61 static CostType getMaxValue() { return std::numeric_limits<CostType>::max(); }
62 static CostType getMinValue() { return std::numeric_limits<CostType>::min(); }
63
64public:
65 // A default constructed InstructionCost is a valid zero cost
66 InstructionCost() = default;
67
69 InstructionCost(CostType Val) : Value(Val), State(Valid) {}
70
71 static InstructionCost getMax() { return getMaxValue(); }
72 static InstructionCost getMin() { return getMinValue(); }
74 InstructionCost Tmp(Val);
75 Tmp.setInvalid();
76 return Tmp;
77 }
78
79 bool isValid() const { return State == Valid; }
80 void setValid() { State = Valid; }
81 void setInvalid() { State = Invalid; }
82 CostState getState() const { return State; }
83
84 /// This function is intended to be used as sparingly as possible, since the
85 /// class provides the full range of operator support required for arithmetic
86 /// and comparisons.
87 std::optional<CostType> getValue() const {
88 if (isValid())
89 return Value;
90 return std::nullopt;
91 }
92
93 /// For all of the arithmetic operators provided here any invalid state is
94 /// perpetuated and cannot be removed. Once a cost becomes invalid it stays
95 /// invalid, and it also inherits any invalid state from the RHS.
96 /// Arithmetic work on the actual values is implemented with saturation,
97 /// to avoid overflow when using more extreme cost values.
98
100 propagateState(RHS);
101
102 // Saturating addition.
104 if (AddOverflow(Value, RHS.Value, Result))
105 Result = RHS.Value > 0 ? getMaxValue() : getMinValue();
106
107 Value = Result;
108 return *this;
109 }
110
112 InstructionCost RHS2(RHS);
113 *this += RHS2;
114 return *this;
115 }
116
118 propagateState(RHS);
119
120 // Saturating subtract.
122 if (SubOverflow(Value, RHS.Value, Result))
123 Result = RHS.Value > 0 ? getMinValue() : getMaxValue();
124 Value = Result;
125 return *this;
126 }
127
129 InstructionCost RHS2(RHS);
130 *this -= RHS2;
131 return *this;
132 }
133
135 propagateState(RHS);
136
137 // Saturating multiply.
139 if (MulOverflow(Value, RHS.Value, Result)) {
140 if ((Value > 0 && RHS.Value > 0) || (Value < 0 && RHS.Value < 0))
141 Result = getMaxValue();
142 else
143 Result = getMinValue();
144 }
145
146 Value = Result;
147 return *this;
148 }
149
151 InstructionCost RHS2(RHS);
152 *this *= RHS2;
153 return *this;
154 }
155
157 propagateState(RHS);
158 Value /= RHS.Value;
159 return *this;
160 }
161
163 InstructionCost RHS2(RHS);
164 *this /= RHS2;
165 return *this;
166 }
167
169 *this += 1;
170 return *this;
171 }
172
174 InstructionCost Copy = *this;
175 ++*this;
176 return Copy;
177 }
178
180 *this -= 1;
181 return *this;
182 }
183
185 InstructionCost Copy = *this;
186 --*this;
187 return Copy;
188 }
189
190 /// For the comparison operators we have chosen to use lexicographical
191 /// ordering where valid costs are always considered to be less than invalid
192 /// costs. This avoids having to add asserts to the comparison operators that
193 /// the states are valid and users can test for validity of the cost
194 /// explicitly.
195 bool operator<(const InstructionCost &RHS) const {
196 if (State != RHS.State)
197 return State < RHS.State;
198 return Value < RHS.Value;
199 }
200
201 bool operator==(const InstructionCost &RHS) const {
202 return State == RHS.State && Value == RHS.Value;
203 }
204
205 bool operator!=(const InstructionCost &RHS) const { return !(*this == RHS); }
206
207 bool operator==(const CostType RHS) const {
208 InstructionCost RHS2(RHS);
209 return *this == RHS2;
210 }
211
212 bool operator!=(const CostType RHS) const { return !(*this == RHS); }
213
214 bool operator>(const InstructionCost &RHS) const { return RHS < *this; }
215
216 bool operator<=(const InstructionCost &RHS) const { return !(RHS < *this); }
217
218 bool operator>=(const InstructionCost &RHS) const { return !(*this < RHS); }
219
220 bool operator<(const CostType RHS) const {
221 InstructionCost RHS2(RHS);
222 return *this < RHS2;
223 }
224
225 bool operator>(const CostType RHS) const {
226 InstructionCost RHS2(RHS);
227 return *this > RHS2;
228 }
229
230 bool operator<=(const CostType RHS) const {
231 InstructionCost RHS2(RHS);
232 return *this <= RHS2;
233 }
234
235 bool operator>=(const CostType RHS) const {
236 InstructionCost RHS2(RHS);
237 return *this >= RHS2;
238 }
239
240 void print(raw_ostream &OS) const;
241
242 template <class Function>
243 auto map(const Function &F) const -> InstructionCost {
244 if (isValid())
245 return F(Value);
246 return getInvalid();
247 }
248};
249
251 const InstructionCost &RHS) {
252 InstructionCost LHS2(LHS);
253 LHS2 += RHS;
254 return LHS2;
255}
256
258 const InstructionCost &RHS) {
259 InstructionCost LHS2(LHS);
260 LHS2 -= RHS;
261 return LHS2;
262}
263
265 const InstructionCost &RHS) {
266 InstructionCost LHS2(LHS);
267 LHS2 *= RHS;
268 return LHS2;
269}
270
272 const InstructionCost &RHS) {
273 InstructionCost LHS2(LHS);
274 LHS2 /= RHS;
275 return LHS2;
276}
277
279 V.print(OS);
280 return OS;
281}
282
283} // namespace llvm
284
285#endif
#define F(x, y, z)
Definition: MD5.cpp:55
raw_pwrite_stream & OS
Value * RHS
Value * LHS
InstructionCost & operator+=(const CostType RHS)
bool operator!=(const CostType RHS) const
bool operator==(const InstructionCost &RHS) const
InstructionCost & operator*=(const InstructionCost &RHS)
auto map(const Function &F) const -> InstructionCost
InstructionCost & operator--()
InstructionCost & operator++()
static InstructionCost getMin()
InstructionCost operator++(int)
static InstructionCost getInvalid(CostType Val=0)
InstructionCost & operator-=(const CostType RHS)
void print(raw_ostream &OS) const
bool operator>(const InstructionCost &RHS) const
InstructionCost operator--(int)
static InstructionCost getMax()
InstructionCost & operator+=(const InstructionCost &RHS)
For all of the arithmetic operators provided here any invalid state is perpetuated and cannot be remo...
bool operator>=(const CostType RHS) const
bool operator<=(const CostType RHS) const
bool operator!=(const InstructionCost &RHS) const
InstructionCost & operator-=(const InstructionCost &RHS)
CostState
CostState describes the state of a cost.
@ Invalid
< The cost value represents a valid cost, even when the cost-value is large.
InstructionCost & operator/=(const InstructionCost &RHS)
std::optional< CostType > getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
bool operator>=(const InstructionCost &RHS) const
InstructionCost(CostType Val)
bool operator<(const InstructionCost &RHS) const
For the comparison operators we have chosen to use lexicographical ordering where valid costs are alw...
InstructionCost & operator*=(const CostType RHS)
InstructionCost(CostState)=delete
bool operator==(const CostType RHS) const
bool operator<=(const InstructionCost &RHS) const
bool operator<(const CostType RHS) const
bool operator>(const CostType RHS) const
CostState getState() const
InstructionCost & operator/=(const CostType RHS)
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::enable_if_t< std::is_signed_v< T >, T > MulOverflow(T X, T Y, T &Result)
Multiply two signed integers, computing the two's complement truncated result, returning true if an o...
Definition: MathExtras.h:753
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2204
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:303
APInt operator-(APInt)
Definition: APInt.h:2157
std::enable_if_t< std::is_signed_v< T >, T > AddOverflow(T X, T Y, T &Result)
Add two signed integers, computing the two's complement truncated result, returning true if overflow ...
Definition: MathExtras.h:701
APInt operator+(APInt a, const APInt &b)
Definition: APInt.h:2162
std::enable_if_t< std::is_signed_v< T >, T > SubOverflow(T X, T Y, T &Result)
Subtract two signed integers, computing the two's complement truncated result, returning true if an o...
Definition: MathExtras.h:727
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt operator/(const DynamicAPInt &A, int64_t B)
Definition: DynamicAPInt.h:550