aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/include/llvm/Support/InstructionCost.h
blob: d357470468aa9faa5583649e810f57bdc281ecc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once 
 
#ifdef __GNUC__ 
#pragma GCC diagnostic push 
#pragma GCC diagnostic ignored "-Wunused-parameter" 
#endif 
 
//===- InstructionCost.h ----------------------------------------*- C++ -*-===// 
// 
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 
// See https://llvm.org/LICENSE.txt for license information. 
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 
// 
//===----------------------------------------------------------------------===// 
/// \file 
/// This file defines an InstructionCost class that is used when calculating 
/// the cost of an instruction, or a group of instructions. In addition to a 
/// numeric value representing the cost the class also contains a state that 
/// can be used to encode particular properties, i.e. a cost being invalid or 
/// unknown. 
/// 
//===----------------------------------------------------------------------===// 
 
#ifndef LLVM_SUPPORT_INSTRUCTIONCOST_H 
#define LLVM_SUPPORT_INSTRUCTIONCOST_H 
 
#include "llvm/ADT/Optional.h" 
 
namespace llvm { 
 
class raw_ostream; 
 
class InstructionCost { 
public: 
  using CostType = int; 
 
  /// These states can currently be used to indicate whether a cost is valid or 
  /// invalid. Examples of an invalid cost might be where the cost is 
  /// prohibitively expensive and the user wants to prevent certain 
  /// optimizations being performed. Or perhaps the cost is simply unknown 
  /// because the operation makes no sense in certain circumstances. These 
  /// states can be expanded in future to support other cases if necessary. 
  enum CostState { Valid, Invalid }; 
 
private: 
  CostType Value; 
  CostState State; 
 
  void propagateState(const InstructionCost &RHS) { 
    if (RHS.State == Invalid) 
      State = Invalid; 
  } 
 
public: 
  InstructionCost() = default; 
 
  InstructionCost(CostState) = delete; 
  InstructionCost(CostType Val) : Value(Val), State(Valid) {} 
 
  static InstructionCost getInvalid(CostType Val = 0) { 
    InstructionCost Tmp(Val); 
    Tmp.setInvalid(); 
    return Tmp; 
  } 
 
  bool isValid() const { return State == Valid; } 
  void setValid() { State = Valid; } 
  void setInvalid() { State = Invalid; } 
  CostState getState() const { return State; } 
 
  /// This function is intended to be used as sparingly as possible, since the 
  /// class provides the full range of operator support required for arithmetic 
  /// and comparisons. 
  Optional<CostType> getValue() const { 
    if (isValid()) 
      return Value; 
    return None; 
  } 
 
  /// For all of the arithmetic operators provided here any invalid state is 
  /// perpetuated and cannot be removed. Once a cost becomes invalid it stays 
  /// invalid, and it also inherits any invalid state from the RHS. Regardless 
  /// of the state, arithmetic and comparisons work on the actual values in the 
  /// same way as they would on a basic type, such as integer. 
 
  InstructionCost &operator+=(const InstructionCost &RHS) { 
    propagateState(RHS); 
    Value += RHS.Value; 
    return *this; 
  } 
 
  InstructionCost &operator+=(const CostType RHS) { 
    InstructionCost RHS2(RHS); 
    *this += RHS2; 
    return *this; 
  } 
 
  InstructionCost &operator-=(const InstructionCost &RHS) { 
    propagateState(RHS); 
    Value -= RHS.Value; 
    return *this; 
  } 
 
  InstructionCost &operator-=(const CostType RHS) { 
    InstructionCost RHS2(RHS); 
    *this -= RHS2; 
    return *this; 
  } 
 
  InstructionCost &operator*=(const InstructionCost &RHS) { 
    propagateState(RHS); 
    Value *= RHS.Value; 
    return *this; 
  } 
 
  InstructionCost &operator*=(const CostType RHS) { 
    InstructionCost RHS2(RHS); 
    *this *= RHS2; 
    return *this; 
  } 
 
  InstructionCost &operator/=(const InstructionCost &RHS) { 
    propagateState(RHS); 
    Value /= RHS.Value; 
    return *this; 
  } 
 
  InstructionCost &operator/=(const CostType RHS) { 
    InstructionCost RHS2(RHS); 
    *this /= RHS2; 
    return *this; 
  } 
 
  InstructionCost &operator++() { 
    *this += 1; 
    return *this; 
  } 
 
  InstructionCost operator++(int) { 
    InstructionCost Copy = *this; 
    ++*this; 
    return Copy; 
  } 
 
  InstructionCost &operator--() { 
    *this -= 1; 
    return *this; 
  } 
 
  InstructionCost operator--(int) { 
    InstructionCost Copy = *this; 
    --*this; 
    return Copy; 
  } 
 
  bool operator==(const InstructionCost &RHS) const { 
    return State == RHS.State && Value == RHS.Value; 
  } 
 
  bool operator!=(const InstructionCost &RHS) const { return !(*this == RHS); } 
 
  bool operator==(const CostType RHS) const { 
    return State == Valid && Value == RHS; 
  } 
 
  bool operator!=(const CostType RHS) const { return !(*this == RHS); } 
 
  /// For the comparison operators we have chosen to use total ordering with 
  /// the following rules: 
  ///  1. If either of the states != Valid then a lexicographical order is 
  ///     applied based upon the state. 
  ///  2. If both states are valid then order based upon value. 
  /// This avoids having to add asserts the comparison operators that the states 
  /// are valid and users can test for validity of the cost explicitly. 
  bool operator<(const InstructionCost &RHS) const { 
    if (State != Valid || RHS.State != Valid) 
      return State < RHS.State; 
    return Value < RHS.Value; 
  } 
 
  bool operator>(const InstructionCost &RHS) const { return RHS < *this; } 
 
  bool operator<=(const InstructionCost &RHS) const { return !(RHS < *this); } 
 
  bool operator>=(const InstructionCost &RHS) const { return !(*this < RHS); } 
 
  bool operator<(const CostType RHS) const { 
    InstructionCost RHS2(RHS); 
    return *this < RHS2; 
  } 
 
  bool operator>(const CostType RHS) const { 
    InstructionCost RHS2(RHS); 
    return *this > RHS2; 
  } 
 
  bool operator<=(const CostType RHS) const { 
    InstructionCost RHS2(RHS); 
    return *this <= RHS2; 
  } 
 
  bool operator>=(const CostType RHS) const { 
    InstructionCost RHS2(RHS); 
    return *this >= RHS2; 
  } 
 
  void print(raw_ostream &OS) const; 
}; 
 
inline InstructionCost operator+(const InstructionCost &LHS, 
                                 const InstructionCost &RHS) { 
  InstructionCost LHS2(LHS); 
  LHS2 += RHS; 
  return LHS2; 
} 
 
inline InstructionCost operator-(const InstructionCost &LHS, 
                                 const InstructionCost &RHS) { 
  InstructionCost LHS2(LHS); 
  LHS2 -= RHS; 
  return LHS2; 
} 
 
inline InstructionCost operator*(const InstructionCost &LHS, 
                                 const InstructionCost &RHS) { 
  InstructionCost LHS2(LHS); 
  LHS2 *= RHS; 
  return LHS2; 
} 
 
inline InstructionCost operator/(const InstructionCost &LHS, 
                                 const InstructionCost &RHS) { 
  InstructionCost LHS2(LHS); 
  LHS2 /= RHS; 
  return LHS2; 
} 
 
inline raw_ostream &operator<<(raw_ostream &OS, const InstructionCost &V) { 
  V.print(OS); 
  return OS; 
} 
 
} // namespace llvm 
 
#endif 
 
#ifdef __GNUC__ 
#pragma GCC diagnostic pop 
#endif