LLVM 22.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
23#include <limits>
24#include <tuple>
25
26namespace llvm {
27
28class raw_ostream;
29
31public:
32 using CostType = int64_t;
33
34 /// CostState describes the state of a cost.
35 enum CostState {
36 Valid, /// < The cost value represents a valid cost, even when the
37 /// cost-value is large.
38 Invalid /// < Invalid indicates there is no way to represent the cost as a
39 /// numeric value. This state exists to represent a possible issue,
40 /// e.g. if the cost-model knows the operation cannot be expanded
41 /// into a valid code-sequence by the code-generator. While some
42 /// passes may assert that the calculated cost must be valid, it is
43 /// up to individual passes how to interpret an Invalid cost. For
44 /// example, a transformation pass could choose not to perform a
45 /// transformation if the resulting cost would end up Invalid.
46 /// Because some passes may assert a cost is Valid, it is not
47 /// recommended to use Invalid costs to model 'Unknown'.
48 /// Note that Invalid is semantically different from a (very) high,
49 /// but valid cost, which intentionally indicates no issue, but
50 /// rather a strong preference not to select a certain operation.
51 };
52
53private:
54 CostType Value = 0;
55 CostState State = Valid;
56
57 void propagateState(const InstructionCost &RHS) {
58 if (RHS.State == Invalid)
59 State = Invalid;
60 }
61
62 static CostType getMaxValue() { return std::numeric_limits<CostType>::max(); }
63 static CostType getMinValue() { return std::numeric_limits<CostType>::min(); }
64
65public:
66 // A default constructed InstructionCost is a valid zero cost
67 InstructionCost() = default;
68
70 InstructionCost(CostType Val) : Value(Val), State(Valid) {}
71
72 static InstructionCost getMax() { return getMaxValue(); }
73 static InstructionCost getMin() { return getMinValue(); }
75 InstructionCost Tmp(Val);
76 Tmp.setInvalid();
77 return Tmp;
78 }
79
80 bool isValid() const { return State == Valid; }
81 void setValid() { State = Valid; }
82 void setInvalid() { State = Invalid; }
83 CostState getState() const { return State; }
84
85 /// This function is intended to be used as sparingly as possible, since the
86 /// class provides the full range of operator support required for arithmetic
87 /// and comparisons.
89 assert(isValid());
90 return Value;
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 return std::tie(State, Value) < std::tie(RHS.State, RHS.Value);
197 }
198
199 bool operator==(const InstructionCost &RHS) const {
200 return State == RHS.State && Value == RHS.Value;
201 }
202
203 bool operator!=(const InstructionCost &RHS) const { return !(*this == RHS); }
204
205 bool operator==(const CostType RHS) const {
206 InstructionCost RHS2(RHS);
207 return *this == RHS2;
208 }
209
210 bool operator!=(const CostType RHS) const { return !(*this == RHS); }
211
212 bool operator>(const InstructionCost &RHS) const { return RHS < *this; }
213
214 bool operator<=(const InstructionCost &RHS) const { return !(RHS < *this); }
215
216 bool operator>=(const InstructionCost &RHS) const { return !(*this < RHS); }
217
218 bool operator<(const CostType RHS) const {
219 InstructionCost RHS2(RHS);
220 return *this < RHS2;
221 }
222
223 bool operator>(const CostType RHS) const {
224 InstructionCost RHS2(RHS);
225 return *this > RHS2;
226 }
227
228 bool operator<=(const CostType RHS) const {
229 InstructionCost RHS2(RHS);
230 return *this <= RHS2;
231 }
232
233 bool operator>=(const CostType RHS) const {
234 InstructionCost RHS2(RHS);
235 return *this >= RHS2;
236 }
237
238 LLVM_ABI void print(raw_ostream &OS) const;
239
240 template <class Function>
241 auto map(const Function &F) const -> InstructionCost {
242 if (isValid())
243 return F(Value);
244 return getInvalid();
245 }
246};
247
249 const InstructionCost &RHS) {
250 InstructionCost LHS2(LHS);
251 LHS2 += RHS;
252 return LHS2;
253}
254
256 const InstructionCost &RHS) {
257 InstructionCost LHS2(LHS);
258 LHS2 -= RHS;
259 return LHS2;
260}
261
263 const InstructionCost &RHS) {
264 InstructionCost LHS2(LHS);
265 LHS2 *= RHS;
266 return LHS2;
267}
268
270 const InstructionCost &RHS) {
271 InstructionCost LHS2(LHS);
272 LHS2 /= RHS;
273 return LHS2;
274}
275
277 V.print(OS);
278 return OS;
279}
280
281} // namespace llvm
282
283#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
#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)
LLVM_ABI 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)
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
CostType getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
CostState getState() const
InstructionCost & operator/=(const CostType RHS)
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
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:758
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2235
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
APInt operator-(APInt)
Definition: APInt.h:2188
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:706
APInt operator+(APInt a, const APInt &b)
Definition: APInt.h:2193
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:732
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt operator/(const DynamicAPInt &A, int64_t B)
Definition: DynamicAPInt.h:563