Coverage Report

Created: 2018-07-19 03:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/CodeGen/GlobalISel/LegalizerInfo.h ------------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
/// Interface for Targets to specify which operations they can successfully
11
/// select and how the others should be expanded most efficiently.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CODEGEN_GLOBALISEL_LEGALIZERINFO_H
16
#define LLVM_CODEGEN_GLOBALISEL_LEGALIZERINFO_H
17
18
#include "llvm/ADT/DenseMap.h"
19
#include "llvm/ADT/None.h"
20
#include "llvm/ADT/Optional.h"
21
#include "llvm/ADT/STLExtras.h"
22
#include "llvm/ADT/SmallBitVector.h"
23
#include "llvm/ADT/SmallVector.h"
24
#include "llvm/CodeGen/MachineFunction.h"
25
#include "llvm/CodeGen/TargetOpcodes.h"
26
#include "llvm/Support/raw_ostream.h"
27
#include "llvm/Support/LowLevelTypeImpl.h"
28
#include <cassert>
29
#include <cstdint>
30
#include <tuple>
31
#include <unordered_map>
32
#include <utility>
33
34
namespace llvm {
35
36
extern cl::opt<bool> DisableGISelLegalityCheck;
37
38
class MachineInstr;
39
class MachineIRBuilder;
40
class MachineRegisterInfo;
41
class MCInstrInfo;
42
43
namespace LegalizeActions {
44
enum LegalizeAction : std::uint8_t {
45
  /// The operation is expected to be selectable directly by the target, and
46
  /// no transformation is necessary.
47
  Legal,
48
49
  /// The operation should be synthesized from multiple instructions acting on
50
  /// a narrower scalar base-type. For example a 64-bit add might be
51
  /// implemented in terms of 32-bit add-with-carry.
52
  NarrowScalar,
53
54
  /// The operation should be implemented in terms of a wider scalar
55
  /// base-type. For example a <2 x s8> add could be implemented as a <2
56
  /// x s32> add (ignoring the high bits).
57
  WidenScalar,
58
59
  /// The (vector) operation should be implemented by splitting it into
60
  /// sub-vectors where the operation is legal. For example a <8 x s64> add
61
  /// might be implemented as 4 separate <2 x s64> adds.
62
  FewerElements,
63
64
  /// The (vector) operation should be implemented by widening the input
65
  /// vector and ignoring the lanes added by doing so. For example <2 x i8> is
66
  /// rarely legal, but you might perform an <8 x i8> and then only look at
67
  /// the first two results.
68
  MoreElements,
69
70
  /// The operation itself must be expressed in terms of simpler actions on
71
  /// this target. E.g. a SREM replaced by an SDIV and subtraction.
72
  Lower,
73
74
  /// The operation should be implemented as a call to some kind of runtime
75
  /// support library. For example this usually happens on machines that don't
76
  /// support floating-point operations natively.
77
  Libcall,
78
79
  /// The target wants to do something special with this combination of
80
  /// operand and type. A callback will be issued when it is needed.
81
  Custom,
82
83
  /// This operation is completely unsupported on the target. A programming
84
  /// error has occurred.
85
  Unsupported,
86
87
  /// Sentinel value for when no action was found in the specified table.
88
  NotFound,
89
90
  /// Fall back onto the old rules.
91
  /// TODO: Remove this once we've migrated
92
  UseLegacyRules,
93
};
94
} // end namespace LegalizeActions
95
96
using LegalizeActions::LegalizeAction;
97
98
/// Legalization is decided based on an instruction's opcode, which type slot
99
/// we're considering, and what the existing type is. These aspects are gathered
100
/// together for convenience in the InstrAspect class.
101
struct InstrAspect {
102
  unsigned Opcode;
103
  unsigned Idx = 0;
104
  LLT Type;
105
106
1.64M
  InstrAspect(unsigned Opcode, LLT Type) : Opcode(Opcode), Type(Type) {}
107
  InstrAspect(unsigned Opcode, unsigned Idx, LLT Type)
108
2.51M
      : Opcode(Opcode), Idx(Idx), Type(Type) {}
109
110
  bool operator==(const InstrAspect &RHS) const {
111
    return Opcode == RHS.Opcode && Idx == RHS.Idx && Type == RHS.Type;
112
  }
113
};
114
115
/// The LegalityQuery object bundles together all the information that's needed
116
/// to decide whether a given operation is legal or not.
117
/// For efficiency, it doesn't make a copy of Types so care must be taken not
118
/// to free it before using the query.
119
struct LegalityQuery {
120
  unsigned Opcode;
121
  ArrayRef<LLT> Types;
122
123
  struct MemDesc {
124
    uint64_t Size;
125
    AtomicOrdering Ordering;
126
  };
127
128
  /// Operations which require memory can use this to place requirements on the
129
  /// memory type for each MMO.
130
  ArrayRef<MemDesc> MMODescrs;
131
132
  constexpr LegalityQuery(unsigned Opcode, const ArrayRef<LLT> Types,
133
                          const ArrayRef<MemDesc> MMODescrs)
134
11.7M
      : Opcode(Opcode), Types(Types), MMODescrs(MMODescrs) {}
135
  constexpr LegalityQuery(unsigned Opcode, const ArrayRef<LLT> Types)
136
346k
      : LegalityQuery(Opcode, Types, {}) {}
137
138
  raw_ostream &print(raw_ostream &OS) const;
139
};
140
141
/// The result of a query. It either indicates a final answer of Legal or
142
/// Unsupported or describes an action that must be taken to make an operation
143
/// more legal.
144
struct LegalizeActionStep {
145
  /// The action to take or the final answer.
146
  LegalizeAction Action;
147
  /// If describing an action, the type index to change. Otherwise zero.
148
  unsigned TypeIdx;
149
  /// If describing an action, the new type for TypeIdx. Otherwise LLT{}.
150
  LLT NewType;
151
152
  LegalizeActionStep(LegalizeAction Action, unsigned TypeIdx,
153
                     const LLT &NewType)
154
13.5M
      : Action(Action), TypeIdx(TypeIdx), NewType(NewType) {}
155
156
  bool operator==(const LegalizeActionStep &RHS) const {
157
    return std::tie(Action, TypeIdx, NewType) ==
158
        std::tie(RHS.Action, RHS.TypeIdx, RHS.NewType);
159
  }
160
};
161
162
using LegalityPredicate = std::function<bool (const LegalityQuery &)>;
163
using LegalizeMutation =
164
    std::function<std::pair<unsigned, LLT>(const LegalityQuery &)>;
165
166
namespace LegalityPredicates {
167
struct TypePairAndMemSize {
168
  LLT Type0;
169
  LLT Type1;
170
  uint64_t MemSize;
171
172
5.51M
  bool operator==(const TypePairAndMemSize &Other) const {
173
5.51M
    return Type0 == Other.Type0 && 
Type1 == Other.Type11.55M
&&
174
5.51M
           
MemSize == Other.MemSize1.55M
;
175
5.51M
  }
176
};
177
178
/// True iff P0 and P1 are true.
179
template<typename Predicate>
180
118k
Predicate all(Predicate P0, Predicate P1) {
181
431k
  return [=](const LegalityQuery &Query) {
182
431k
    return P0(Query) && 
P1(Query)431k
;
183
431k
  };
184
118k
}
185
/// True iff all given predicates are true.
186
template<typename Predicate, typename... Args>
187
93
Predicate all(Predicate P0, Predicate P1, Args... args) {
188
93
  return all(all(P0, P1), args...);
189
93
}
std::__1::function<bool (llvm::LegalityQuery const&)> llvm::LegalityPredicates::all<std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)> >(std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)>)
Line
Count
Source
187
31
Predicate all(Predicate P0, Predicate P1, Args... args) {
188
31
  return all(all(P0, P1), args...);
189
31
}
std::__1::function<bool (llvm::LegalityQuery const&)> llvm::LegalityPredicates::all<std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)> >(std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)>, std::__1::function<bool (llvm::LegalityQuery const&)>)
Line
Count
Source
187
62
Predicate all(Predicate P0, Predicate P1, Args... args) {
188
62
  return all(all(P0, P1), args...);
189
62
}
190
/// True iff the given type index is the specified types.
191
LegalityPredicate typeIs(unsigned TypeIdx, LLT TypesInit);
192
/// True iff the given type index is one of the specified types.
193
LegalityPredicate typeInSet(unsigned TypeIdx,
194
                            std::initializer_list<LLT> TypesInit);
195
/// True iff the given types for the given pair of type indexes is one of the
196
/// specified type pairs.
197
LegalityPredicate
198
typePairInSet(unsigned TypeIdx0, unsigned TypeIdx1,
199
              std::initializer_list<std::pair<LLT, LLT>> TypesInit);
200
/// True iff the given types for the given pair of type indexes is one of the
201
/// specified type pairs.
202
LegalityPredicate typePairAndMemSizeInSet(
203
    unsigned TypeIdx0, unsigned TypeIdx1, unsigned MMOIdx,
204
    std::initializer_list<TypePairAndMemSize> TypesAndMemSizeInit);
205
/// True iff the specified type index is a scalar.
206
LegalityPredicate isScalar(unsigned TypeIdx);
207
/// True iff the specified type index is a scalar that's narrower than the given
208
/// size.
209
LegalityPredicate narrowerThan(unsigned TypeIdx, unsigned Size);
210
/// True iff the specified type index is a scalar that's wider than the given
211
/// size.
212
LegalityPredicate widerThan(unsigned TypeIdx, unsigned Size);
213
/// True iff the specified type index is a scalar whose size is not a power of
214
/// 2.
215
LegalityPredicate sizeNotPow2(unsigned TypeIdx);
216
/// True iff the specified MMO index has a size that is not a power of 2
217
LegalityPredicate memSizeInBytesNotPow2(unsigned MMOIdx);
218
/// True iff the specified type index is a vector whose element count is not a
219
/// power of 2.
220
LegalityPredicate numElementsNotPow2(unsigned TypeIdx);
221
/// True iff the specified MMO index has at an atomic ordering of at Ordering or
222
/// stronger.
223
LegalityPredicate atomicOrderingAtLeastOrStrongerThan(unsigned MMOIdx,
224
                                                      AtomicOrdering Ordering);
225
} // end namespace LegalityPredicates
226
227
namespace LegalizeMutations {
228
/// Select this specific type for the given type index.
229
LegalizeMutation changeTo(unsigned TypeIdx, LLT Ty);
230
/// Keep the same type as the given type index.
231
LegalizeMutation changeTo(unsigned TypeIdx, unsigned FromTypeIdx);
232
/// Widen the type for the given type index to the next power of 2.
233
LegalizeMutation widenScalarToNextPow2(unsigned TypeIdx, unsigned Min = 0);
234
/// Add more elements to the type for the given type index to the next power of
235
/// 2.
236
LegalizeMutation moreElementsToNextPow2(unsigned TypeIdx, unsigned Min = 0);
237
} // end namespace LegalizeMutations
238
239
/// A single rule in a legalizer info ruleset.
240
/// The specified action is chosen when the predicate is true. Where appropriate
241
/// for the action (e.g. for WidenScalar) the new type is selected using the
242
/// given mutator.
243
class LegalizeRule {
244
  LegalityPredicate Predicate;
245
  LegalizeAction Action;
246
  LegalizeMutation Mutation;
247
248
public:
249
  LegalizeRule(LegalityPredicate Predicate, LegalizeAction Action,
250
               LegalizeMutation Mutation = nullptr)
251
1.72M
      : Predicate(Predicate), Action(Action), Mutation(Mutation) {}
252
253
  /// Test whether the LegalityQuery matches.
254
11.7M
  bool match(const LegalityQuery &Query) const {
255
11.7M
    return Predicate(Query);
256
11.7M
  }
257
258
9.95M
  LegalizeAction getAction() const { return Action; }
259
260
  /// Determine the change to make.
261
9.95M
  std::pair<unsigned, LLT> determineMutation(const LegalityQuery &Query) const {
262
9.95M
    if (Mutation)
263
1.28M
      return Mutation(Query);
264
8.66M
    return std::make_pair(0, LLT{});
265
8.66M
  }
266
};
267
268
class LegalizeRuleSet {
269
  /// When non-zero, the opcode we are an alias of
270
  unsigned AliasOf;
271
  /// If true, there is another opcode that aliases this one
272
  bool IsAliasedByAnother;
273
  SmallVector<LegalizeRule, 2> Rules;
274
275
#ifndef NDEBUG
276
  /// If bit I is set, this rule set contains a rule that may handle (predicate
277
  /// or perform an action upon (or both)) the type index I. The uncertainty
278
  /// comes from free-form rules executing user-provided lambda functions. We
279
  /// conservatively assume such rules do the right thing and cover all type
280
  /// indices. The bitset is intentionally 1 bit wider than it absolutely needs
281
  /// to be to distinguish such cases from the cases where all type indices are
282
  /// individually handled.
283
  SmallBitVector TypeIdxsCovered{MCOI::OPERAND_LAST_GENERIC -
284
                                 MCOI::OPERAND_FIRST_GENERIC + 2};
285
#endif
286
287
1.81M
  unsigned typeIdx(unsigned TypeIdx) {
288
1.81M
    assert(TypeIdx <=
289
1.81M
               (MCOI::OPERAND_LAST_GENERIC - MCOI::OPERAND_FIRST_GENERIC) &&
290
1.81M
           "Type Index is out of bounds");
291
1.81M
#ifndef NDEBUG
292
1.81M
    TypeIdxsCovered.set(TypeIdx);
293
1.81M
#endif
294
1.81M
    return TypeIdx;
295
1.81M
  }
296
168k
  void markAllTypeIdxsAsCovered() {
297
168k
#ifndef NDEBUG
298
168k
    TypeIdxsCovered.set();
299
168k
#endif
300
168k
  }
301
302
1.72M
  void add(const LegalizeRule &Rule) {
303
1.72M
    assert(AliasOf == 0 &&
304
1.72M
           "RuleSet is aliased, change the representative opcode instead");
305
1.72M
    Rules.push_back(Rule);
306
1.72M
  }
307
308
0
  static bool always(const LegalityQuery &) { return true; }
309
310
  /// Use the given action when the predicate is true.
311
  /// Action should not be an action that requires mutation.
312
  LegalizeRuleSet &actionIf(LegalizeAction Action,
313
684k
                            LegalityPredicate Predicate) {
314
684k
    add({Predicate, Action});
315
684k
    return *this;
316
684k
  }
317
  /// Use the given action when the predicate is true.
318
  /// Action should be an action that requires mutation.
319
  LegalizeRuleSet &actionIf(LegalizeAction Action, LegalityPredicate Predicate,
320
1.04M
                            LegalizeMutation Mutation) {
321
1.04M
    add({Predicate, Action, Mutation});
322
1.04M
    return *this;
323
1.04M
  }
324
  /// Use the given action when type index 0 is any type in the given list.
325
  /// Action should not be an action that requires mutation.
326
  LegalizeRuleSet &actionFor(LegalizeAction Action,
327
260k
                             std::initializer_list<LLT> Types) {
328
260k
    using namespace LegalityPredicates;
329
260k
    return actionIf(Action, typeInSet(typeIdx(0), Types));
330
260k
  }
331
  /// Use the given action when type index 0 is any type in the given list.
332
  /// Action should be an action that requires mutation.
333
  LegalizeRuleSet &actionFor(LegalizeAction Action,
334
                             std::initializer_list<LLT> Types,
335
8.44k
                             LegalizeMutation Mutation) {
336
8.44k
    using namespace LegalityPredicates;
337
8.44k
    return actionIf(Action, typeInSet(typeIdx(0), Types), Mutation);
338
8.44k
  }
339
  /// Use the given action when type indexes 0 and 1 is any type pair in the
340
  /// given list.
341
  /// Action should not be an action that requires mutation.
342
  LegalizeRuleSet &actionFor(LegalizeAction Action,
343
144k
                             std::initializer_list<std::pair<LLT, LLT>> Types) {
344
144k
    using namespace LegalityPredicates;
345
144k
    return actionIf(Action, typePairInSet(typeIdx(0), typeIdx(1), Types));
346
144k
  }
347
  /// Use the given action when type indexes 0 and 1 is any type pair in the
348
  /// given list.
349
  /// Action should be an action that requires mutation.
350
  LegalizeRuleSet &actionFor(LegalizeAction Action,
351
                             std::initializer_list<std::pair<LLT, LLT>> Types,
352
8.44k
                             LegalizeMutation Mutation) {
353
8.44k
    using namespace LegalityPredicates;
354
8.44k
    return actionIf(Action, typePairInSet(typeIdx(0), typeIdx(1), Types),
355
8.44k
                    Mutation);
356
8.44k
  }
357
  /// Use the given action when type indexes 0 and 1 are both in the given list.
358
  /// That is, the type pair is in the cartesian product of the list.
359
  /// Action should not be an action that requires mutation.
360
  LegalizeRuleSet &actionForCartesianProduct(LegalizeAction Action,
361
34.4k
                                             std::initializer_list<LLT> Types) {
362
34.4k
    using namespace LegalityPredicates;
363
34.4k
    return actionIf(Action, all(typeInSet(typeIdx(0), Types),
364
34.4k
                                typeInSet(typeIdx(1), Types)));
365
34.4k
  }
366
  /// Use the given action when type indexes 0 and 1 are both in their
367
  /// respective lists.
368
  /// That is, the type pair is in the cartesian product of the lists
369
  /// Action should not be an action that requires mutation.
370
  LegalizeRuleSet &
371
  actionForCartesianProduct(LegalizeAction Action,
372
                            std::initializer_list<LLT> Types0,
373
83.5k
                            std::initializer_list<LLT> Types1) {
374
83.5k
    using namespace LegalityPredicates;
375
83.5k
    return actionIf(Action, all(typeInSet(typeIdx(0), Types0),
376
83.5k
                                typeInSet(typeIdx(1), Types1)));
377
83.5k
  }
378
  /// Use the given action when type indexes 0, 1, and 2 are all in their
379
  /// respective lists.
380
  /// That is, the type triple is in the cartesian product of the lists
381
  /// Action should not be an action that requires mutation.
382
  LegalizeRuleSet &actionForCartesianProduct(
383
      LegalizeAction Action, std::initializer_list<LLT> Types0,
384
0
      std::initializer_list<LLT> Types1, std::initializer_list<LLT> Types2) {
385
0
    using namespace LegalityPredicates;
386
0
    return actionIf(Action, all(typeInSet(typeIdx(0), Types0),
387
0
                                all(typeInSet(typeIdx(1), Types1),
388
0
                                    typeInSet(typeIdx(2), Types2))));
389
0
  }
390
391
public:
392
3.58M
  LegalizeRuleSet() : AliasOf(0), IsAliasedByAnother(false), Rules() {}
393
394
  bool isAliasedByAnother() { return IsAliasedByAnother; }
395
184k
  void setIsAliasedByAnother() { IsAliasedByAnother = true; }
396
380k
  void aliasTo(unsigned Opcode) {
397
380k
    assert((AliasOf == 0 || AliasOf == Opcode) &&
398
380k
           "Opcode is already aliased to another opcode");
399
380k
    assert(Rules.empty() && "Aliasing will discard rules");
400
380k
    AliasOf = Opcode;
401
380k
  }
402
12.3M
  unsigned getAlias() const { return AliasOf; }
403
404
  /// The instruction is legal if predicate is true.
405
49.7k
  LegalizeRuleSet &legalIf(LegalityPredicate Predicate) {
406
49.7k
    // We have no choice but conservatively assume that the free-form
407
49.7k
    // user-provided Predicate properly handles all type indices:
408
49.7k
    markAllTypeIdxsAsCovered();
409
49.7k
    return actionIf(LegalizeAction::Legal, Predicate);
410
49.7k
  }
411
  /// The instruction is legal when type index 0 is any type in the given list.
412
229k
  LegalizeRuleSet &legalFor(std::initializer_list<LLT> Types) {
413
229k
    return actionFor(LegalizeAction::Legal, Types);
414
229k
  }
415
  /// The instruction is legal when type indexes 0 and 1 is any type pair in the
416
  /// given list.
417
139k
  LegalizeRuleSet &legalFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
418
139k
    return actionFor(LegalizeAction::Legal, Types);
419
139k
  }
420
  /// The instruction is legal when type indexes 0 and 1 along with the memory
421
  /// size is any type and size tuple in the given list.
422
  LegalizeRuleSet &legalForTypesWithMemSize(
423
      std::initializer_list<LegalityPredicates::TypePairAndMemSize>
424
33.7k
          TypesAndMemSize) {
425
33.7k
    return actionIf(LegalizeAction::Legal,
426
33.7k
                    LegalityPredicates::typePairAndMemSizeInSet(
427
33.7k
                        typeIdx(0), typeIdx(1), /*MMOIdx*/ 0, TypesAndMemSize));
428
33.7k
  }
429
  /// The instruction is legal when type indexes 0 and 1 are both in the given
430
  /// list. That is, the type pair is in the cartesian product of the list.
431
34.4k
  LegalizeRuleSet &legalForCartesianProduct(std::initializer_list<LLT> Types) {
432
34.4k
    return actionForCartesianProduct(LegalizeAction::Legal, Types);
433
34.4k
  }
434
  /// The instruction is legal when type indexes 0 and 1 are both their
435
  /// respective lists.
436
  LegalizeRuleSet &legalForCartesianProduct(std::initializer_list<LLT> Types0,
437
67.4k
                                            std::initializer_list<LLT> Types1) {
438
67.4k
    return actionForCartesianProduct(LegalizeAction::Legal, Types0, Types1);
439
67.4k
  }
440
441
  /// The instruction is lowered.
442
8.44k
  LegalizeRuleSet &lower() {
443
8.44k
    using namespace LegalizeMutations;
444
8.44k
    // We have no choice but conservatively assume that predicate-less lowering
445
8.44k
    // properly handles all type indices by design:
446
8.44k
    markAllTypeIdxsAsCovered();
447
8.44k
    return actionIf(LegalizeAction::Lower, always);
448
8.44k
  }
449
  /// The instruction is lowered if predicate is true. Keep type index 0 as the
450
  /// same type.
451
16.9k
  LegalizeRuleSet &lowerIf(LegalityPredicate Predicate) {
452
16.9k
    using namespace LegalizeMutations;
453
16.9k
    // We have no choice but conservatively assume that lowering with a
454
16.9k
    // free-form user provided Predicate properly handles all type indices:
455
16.9k
    markAllTypeIdxsAsCovered();
456
16.9k
    return actionIf(LegalizeAction::Lower, Predicate);
457
16.9k
  }
458
  /// The instruction is lowered if predicate is true.
459
  LegalizeRuleSet &lowerIf(LegalityPredicate Predicate,
460
0
                           LegalizeMutation Mutation) {
461
0
    // We have no choice but conservatively assume that lowering with a
462
0
    // free-form user provided Predicate properly handles all type indices:
463
0
    markAllTypeIdxsAsCovered();
464
0
    return actionIf(LegalizeAction::Lower, Predicate, Mutation);
465
0
  }
466
  /// The instruction is lowered when type index 0 is any type in the given
467
  /// list. Keep type index 0 as the same type.
468
8.44k
  LegalizeRuleSet &lowerFor(std::initializer_list<LLT> Types) {
469
8.44k
    return actionFor(LegalizeAction::Lower, Types,
470
8.44k
                     LegalizeMutations::changeTo(0, 0));
471
8.44k
  }
472
  /// The instruction is lowered when type index 0 is any type in the given
473
  /// list.
474
  LegalizeRuleSet &lowerFor(std::initializer_list<LLT> Types,
475
0
                            LegalizeMutation Mutation) {
476
0
    return actionFor(LegalizeAction::Lower, Types, Mutation);
477
0
  }
478
  /// The instruction is lowered when type indexes 0 and 1 is any type pair in
479
  /// the given list. Keep type index 0 as the same type.
480
8.44k
  LegalizeRuleSet &lowerFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
481
8.44k
    return actionFor(LegalizeAction::Lower, Types,
482
8.44k
                     LegalizeMutations::changeTo(0, 0));
483
8.44k
  }
484
  /// The instruction is lowered when type indexes 0 and 1 is any type pair in
485
  /// the given list.
486
  LegalizeRuleSet &lowerFor(std::initializer_list<std::pair<LLT, LLT>> Types,
487
0
                            LegalizeMutation Mutation) {
488
0
    return actionFor(LegalizeAction::Lower, Types, Mutation);
489
0
  }
490
  /// The instruction is lowered when type indexes 0 and 1 are both in their
491
  /// respective lists.
492
  LegalizeRuleSet &lowerForCartesianProduct(std::initializer_list<LLT> Types0,
493
                                            std::initializer_list<LLT> Types1) {
494
    using namespace LegalityPredicates;
495
    return actionForCartesianProduct(LegalizeAction::Lower, Types0, Types1);
496
  }
497
  /// The instruction is lowered when when type indexes 0, 1, and 2 are all in
498
  /// their respective lists.
499
  LegalizeRuleSet &lowerForCartesianProduct(std::initializer_list<LLT> Types0,
500
                                            std::initializer_list<LLT> Types1,
501
                                            std::initializer_list<LLT> Types2) {
502
    using namespace LegalityPredicates;
503
    return actionForCartesianProduct(LegalizeAction::Lower, Types0, Types1,
504
                                     Types2);
505
  }
506
507
  /// Like legalIf, but for the Libcall action.
508
  LegalizeRuleSet &libcallIf(LegalityPredicate Predicate) {
509
    // We have no choice but conservatively assume that a libcall with a
510
    // free-form user provided Predicate properly handles all type indices:
511
    markAllTypeIdxsAsCovered();
512
    return actionIf(LegalizeAction::Libcall, Predicate);
513
  }
514
28.0k
  LegalizeRuleSet &libcallFor(std::initializer_list<LLT> Types) {
515
28.0k
    return actionFor(LegalizeAction::Libcall, Types);
516
28.0k
  }
517
  LegalizeRuleSet &
518
5.11k
  libcallFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
519
5.11k
    return actionFor(LegalizeAction::Libcall, Types);
520
5.11k
  }
521
  LegalizeRuleSet &
522
0
  libcallForCartesianProduct(std::initializer_list<LLT> Types) {
523
0
    return actionForCartesianProduct(LegalizeAction::Libcall, Types);
524
0
  }
525
  LegalizeRuleSet &
526
  libcallForCartesianProduct(std::initializer_list<LLT> Types0,
527
5.11k
                             std::initializer_list<LLT> Types1) {
528
5.11k
    return actionForCartesianProduct(LegalizeAction::Libcall, Types0, Types1);
529
5.11k
  }
530
531
  /// Widen the scalar to the one selected by the mutation if the predicate is
532
  /// true.
533
  LegalizeRuleSet &widenScalarIf(LegalityPredicate Predicate,
534
16.8k
                                 LegalizeMutation Mutation) {
535
16.8k
    // We have no choice but conservatively assume that an action with a
536
16.8k
    // free-form user provided Predicate properly handles all type indices:
537
16.8k
    markAllTypeIdxsAsCovered();
538
16.8k
    return actionIf(LegalizeAction::WidenScalar, Predicate, Mutation);
539
16.8k
  }
540
  /// Narrow the scalar to the one selected by the mutation if the predicate is
541
  /// true.
542
  LegalizeRuleSet &narrowScalarIf(LegalityPredicate Predicate,
543
                                  LegalizeMutation Mutation) {
544
    // We have no choice but conservatively assume that an action with a
545
    // free-form user provided Predicate properly handles all type indices:
546
    markAllTypeIdxsAsCovered();
547
    return actionIf(LegalizeAction::NarrowScalar, Predicate, Mutation);
548
  }
549
550
  /// Add more elements to reach the type selected by the mutation if the
551
  /// predicate is true.
552
  LegalizeRuleSet &moreElementsIf(LegalityPredicate Predicate,
553
                                  LegalizeMutation Mutation) {
554
    // We have no choice but conservatively assume that an action with a
555
    // free-form user provided Predicate properly handles all type indices:
556
    markAllTypeIdxsAsCovered();
557
    return actionIf(LegalizeAction::MoreElements, Predicate, Mutation);
558
  }
559
  /// Remove elements to reach the type selected by the mutation if the
560
  /// predicate is true.
561
  LegalizeRuleSet &fewerElementsIf(LegalityPredicate Predicate,
562
76.6k
                                   LegalizeMutation Mutation) {
563
76.6k
    // We have no choice but conservatively assume that an action with a
564
76.6k
    // free-form user provided Predicate properly handles all type indices:
565
76.6k
    markAllTypeIdxsAsCovered();
566
76.6k
    return actionIf(LegalizeAction::FewerElements, Predicate, Mutation);
567
76.6k
  }
568
569
  /// The instruction is unsupported.
570
  LegalizeRuleSet &unsupported() {
571
    return actionIf(LegalizeAction::Unsupported, always);
572
  }
573
27.6k
  LegalizeRuleSet &unsupportedIf(LegalityPredicate Predicate) {
574
27.6k
    return actionIf(LegalizeAction::Unsupported, Predicate);
575
27.6k
  }
576
25.3k
  LegalizeRuleSet &unsupportedIfMemSizeNotPow2() {
577
25.3k
    return actionIf(LegalizeAction::Unsupported,
578
25.3k
                    LegalityPredicates::memSizeInBytesNotPow2(0));
579
25.3k
  }
580
581
  LegalizeRuleSet &customIf(LegalityPredicate Predicate) {
582
    // We have no choice but conservatively assume that a custom action with a
583
    // free-form user provided Predicate properly handles all type indices:
584
    markAllTypeIdxsAsCovered();
585
    return actionIf(LegalizeAction::Custom, Predicate);
586
  }
587
2.55k
  LegalizeRuleSet &customFor(std::initializer_list<LLT> Types) {
588
2.55k
    return actionFor(LegalizeAction::Custom, Types);
589
2.55k
  }
590
0
  LegalizeRuleSet &customForCartesianProduct(std::initializer_list<LLT> Types) {
591
0
    return actionForCartesianProduct(LegalizeAction::Custom, Types);
592
0
  }
593
  LegalizeRuleSet &
594
  customForCartesianProduct(std::initializer_list<LLT> Types0,
595
11.0k
                            std::initializer_list<LLT> Types1) {
596
11.0k
    return actionForCartesianProduct(LegalizeAction::Custom, Types0, Types1);
597
11.0k
  }
598
599
  /// Widen the scalar to the next power of two that is at least MinSize.
600
  /// No effect if the type is not a scalar or is a power of two.
601
  LegalizeRuleSet &widenScalarToNextPow2(unsigned TypeIdx,
602
233k
                                         unsigned MinSize = 0) {
603
233k
    using namespace LegalityPredicates;
604
233k
    return actionIf(LegalizeAction::WidenScalar, sizeNotPow2(typeIdx(TypeIdx)),
605
233k
                    LegalizeMutations::widenScalarToNextPow2(TypeIdx, MinSize));
606
233k
  }
607
608
  LegalizeRuleSet &narrowScalar(unsigned TypeIdx, LegalizeMutation Mutation) {
609
    using namespace LegalityPredicates;
610
    return actionIf(LegalizeAction::NarrowScalar, isScalar(typeIdx(TypeIdx)),
611
                    Mutation);
612
  }
613
614
  /// Ensure the scalar is at least as wide as Ty.
615
293k
  LegalizeRuleSet &minScalar(unsigned TypeIdx, const LLT &Ty) {
616
293k
    using namespace LegalityPredicates;
617
293k
    using namespace LegalizeMutations;
618
293k
    return actionIf(LegalizeAction::WidenScalar,
619
293k
                    narrowerThan(TypeIdx, Ty.getSizeInBits()),
620
293k
                    changeTo(typeIdx(TypeIdx), Ty));
621
293k
  }
622
623
  /// Ensure the scalar is at most as wide as Ty.
624
297k
  LegalizeRuleSet &maxScalar(unsigned TypeIdx, const LLT &Ty) {
625
297k
    using namespace LegalityPredicates;
626
297k
    using namespace LegalizeMutations;
627
297k
    return actionIf(LegalizeAction::NarrowScalar,
628
297k
                    widerThan(TypeIdx, Ty.getSizeInBits()),
629
297k
                    changeTo(typeIdx(TypeIdx), Ty));
630
297k
  }
631
632
  /// Conditionally limit the maximum size of the scalar.
633
  /// For example, when the maximum size of one type depends on the size of
634
  /// another such as extracting N bits from an M bit container.
635
  LegalizeRuleSet &maxScalarIf(LegalityPredicate Predicate, unsigned TypeIdx,
636
33.7k
                               const LLT &Ty) {
637
33.7k
    using namespace LegalityPredicates;
638
33.7k
    using namespace LegalizeMutations;
639
33.7k
    return actionIf(LegalizeAction::NarrowScalar,
640
33.7k
                    [=](const LegalityQuery &Query) {
641
0
                      return widerThan(TypeIdx, Ty.getSizeInBits()) &&
642
0
                             Predicate(Query);
643
0
                    },
644
33.7k
                    changeTo(typeIdx(TypeIdx), Ty));
645
33.7k
  }
646
647
  /// Limit the range of scalar sizes to MinTy and MaxTy.
648
  LegalizeRuleSet &clampScalar(unsigned TypeIdx, const LLT &MinTy,
649
273k
                               const LLT &MaxTy) {
650
273k
    assert(MinTy.isScalar() && MaxTy.isScalar() && "Expected scalar types");
651
273k
    return minScalar(TypeIdx, MinTy).maxScalar(TypeIdx, MaxTy);
652
273k
  }
653
654
  /// Add more elements to the vector to reach the next power of two.
655
  /// No effect if the type is not a vector or the element count is a power of
656
  /// two.
657
8.44k
  LegalizeRuleSet &moreElementsToNextPow2(unsigned TypeIdx) {
658
8.44k
    using namespace LegalityPredicates;
659
8.44k
    return actionIf(LegalizeAction::MoreElements,
660
8.44k
                    numElementsNotPow2(typeIdx(TypeIdx)),
661
8.44k
                    LegalizeMutations::moreElementsToNextPow2(TypeIdx));
662
8.44k
  }
663
664
  /// Limit the number of elements in EltTy vectors to at least MinElements.
665
  LegalizeRuleSet &clampMinNumElements(unsigned TypeIdx, const LLT &EltTy,
666
33.7k
                                       unsigned MinElements) {
667
33.7k
    // Mark the type index as covered:
668
33.7k
    typeIdx(TypeIdx);
669
33.7k
    return actionIf(
670
33.7k
        LegalizeAction::MoreElements,
671
33.7k
        [=](const LegalityQuery &Query) {
672
8.18k
          LLT VecTy = Query.Types[TypeIdx];
673
8.18k
          return VecTy.isVector() && VecTy.getElementType() == EltTy &&
674
8.18k
                 
VecTy.getNumElements() < MinElements5.03k
;
675
8.18k
        },
676
33.7k
        [=](const LegalityQuery &Query) {
677
0
          LLT VecTy = Query.Types[TypeIdx];
678
0
          return std::make_pair(
679
0
              TypeIdx, LLT::vector(MinElements, VecTy.getScalarSizeInBits()));
680
0
        });
681
33.7k
  }
682
  /// Limit the number of elements in EltTy vectors to at most MaxElements.
683
  LegalizeRuleSet &clampMaxNumElements(unsigned TypeIdx, const LLT &EltTy,
684
33.7k
                                       unsigned MaxElements) {
685
33.7k
    // Mark the type index as covered:
686
33.7k
    typeIdx(TypeIdx);
687
33.7k
    return actionIf(
688
33.7k
        LegalizeAction::FewerElements,
689
33.7k
        [=](const LegalityQuery &Query) {
690
8.18k
          LLT VecTy = Query.Types[TypeIdx];
691
8.18k
          return VecTy.isVector() && VecTy.getElementType() == EltTy &&
692
8.18k
                 
VecTy.getNumElements() > MaxElements5.03k
;
693
8.18k
        },
694
33.7k
        [=](const LegalityQuery &Query) {
695
5.03k
          LLT VecTy = Query.Types[TypeIdx];
696
5.03k
          return std::make_pair(
697
5.03k
              TypeIdx, LLT::vector(MaxElements, VecTy.getScalarSizeInBits()));
698
5.03k
        });
699
33.7k
  }
700
  /// Limit the number of elements for the given vectors to at least MinTy's
701
  /// number of elements and at most MaxTy's number of elements.
702
  ///
703
  /// No effect if the type is not a vector or does not have the same element
704
  /// type as the constraints.
705
  /// The element type of MinTy and MaxTy must match.
706
  LegalizeRuleSet &clampNumElements(unsigned TypeIdx, const LLT &MinTy,
707
33.7k
                                    const LLT &MaxTy) {
708
33.7k
    assert(MinTy.getElementType() == MaxTy.getElementType() &&
709
33.7k
           "Expected element types to agree");
710
33.7k
711
33.7k
    const LLT &EltTy = MinTy.getElementType();
712
33.7k
    return clampMinNumElements(TypeIdx, EltTy, MinTy.getNumElements())
713
33.7k
        .clampMaxNumElements(TypeIdx, EltTy, MaxTy.getNumElements());
714
33.7k
  }
715
716
  /// Fallback on the previous implementation. This should only be used while
717
  /// porting a rule.
718
  LegalizeRuleSet &fallback() {
719
    add({always, LegalizeAction::UseLegacyRules});
720
    return *this;
721
  }
722
723
  /// Check if there is no type index which is obviously not handled by the
724
  /// LegalizeRuleSet in any way at all.
725
  /// \pre Type indices of the opcode form a dense [0, \p NumTypeIdxs) set.
726
  bool verifyTypeIdxsCoverage(unsigned NumTypeIdxs) const;
727
728
  /// Apply the ruleset to the given LegalityQuery.
729
  LegalizeActionStep apply(const LegalityQuery &Query) const;
730
};
731
732
class LegalizerInfo {
733
public:
734
  LegalizerInfo();
735
27.7k
  virtual ~LegalizerInfo() = default;
736
737
  unsigned getOpcodeIdxForOpcode(unsigned Opcode) const;
738
  unsigned getActionDefinitionsIdx(unsigned Opcode) const;
739
740
  /// Compute any ancillary tables needed to quickly decide how an operation
741
  /// should be handled. This must be called after all "set*Action"methods but
742
  /// before any query is made or incorrect results may be returned.
743
  void computeTables();
744
745
  /// Perform simple self-diagnostic and assert if there is anything obviously
746
  /// wrong with the actions set up.
747
  void verify(const MCInstrInfo &MII) const;
748
749
221
  static bool needsLegalizingToDifferentSize(const LegalizeAction Action) {
750
221
    using namespace LegalizeActions;
751
221
    switch (Action) {
752
221
    case NarrowScalar:
753
74
    case WidenScalar:
754
74
    case FewerElements:
755
74
    case MoreElements:
756
74
    case Unsupported:
757
74
      return true;
758
147
    default:
759
147
      return false;
760
221
    }
761
221
  }
762
763
  using SizeAndAction = std::pair<uint16_t, LegalizeAction>;
764
  using SizeAndActionsVec = std::vector<SizeAndAction>;
765
  using SizeChangeStrategy =
766
      std::function<SizeAndActionsVec(const SizeAndActionsVec &v)>;
767
768
  /// More friendly way to set an action for common types that have an LLT
769
  /// representation.
770
  /// The LegalizeAction must be one for which NeedsLegalizingToDifferentSize
771
  /// returns false.
772
2.17M
  void setAction(const InstrAspect &Aspect, LegalizeAction Action) {
773
2.17M
    assert(!needsLegalizingToDifferentSize(Action));
774
2.17M
    TablesInitialized = false;
775
2.17M
    const unsigned OpcodeIdx = Aspect.Opcode - FirstOp;
776
2.17M
    if (SpecifiedActions[OpcodeIdx].size() <= Aspect.Idx)
777
507k
      SpecifiedActions[OpcodeIdx].resize(Aspect.Idx + 1);
778
2.17M
    SpecifiedActions[OpcodeIdx][Aspect.Idx][Aspect.Type] = Action;
779
2.17M
  }
780
781
  /// The setAction calls record the non-size-changing legalization actions
782
  /// to take on specificly-sized types. The SizeChangeStrategy defines what
783
  /// to do when the size of the type needs to be changed to reach a legally
784
  /// sized type (i.e., one that was defined through a setAction call).
785
  /// e.g.
786
  /// setAction ({G_ADD, 0, LLT::scalar(32)}, Legal);
787
  /// setLegalizeScalarToDifferentSizeStrategy(
788
  ///   G_ADD, 0, widenToLargerTypesAndNarrowToLargest);
789
  /// will end up defining getAction({G_ADD, 0, T}) to return the following 
790
  /// actions for different scalar types T:
791
  ///  LLT::scalar(1)..LLT::scalar(31): {WidenScalar, 0, LLT::scalar(32)}
792
  ///  LLT::scalar(32):                 {Legal, 0, LLT::scalar(32)}
793
  ///  LLT::scalar(33)..:               {NarrowScalar, 0, LLT::scalar(32)}
794
  ///
795
  /// If no SizeChangeAction gets defined, through this function,
796
  /// the default is unsupportedForDifferentSizes.
797
  void setLegalizeScalarToDifferentSizeStrategy(const unsigned Opcode,
798
                                                const unsigned TypeIdx,
799
489k
                                                SizeChangeStrategy S) {
800
489k
    const unsigned OpcodeIdx = Opcode - FirstOp;
801
489k
    if (ScalarSizeChangeStrategies[OpcodeIdx].size() <= TypeIdx)
802
452k
      ScalarSizeChangeStrategies[OpcodeIdx].resize(TypeIdx + 1);
803
489k
    ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx] = S;
804
489k
  }
805
806
  /// See also setLegalizeScalarToDifferentSizeStrategy.
807
  /// This function allows to set the SizeChangeStrategy for vector elements.
808
  void setLegalizeVectorElementToDifferentSizeStrategy(const unsigned Opcode,
809
                                                       const unsigned TypeIdx,
810
                                                       SizeChangeStrategy S) {
811
    const unsigned OpcodeIdx = Opcode - FirstOp;
812
    if (VectorElementSizeChangeStrategies[OpcodeIdx].size() <= TypeIdx)
813
      VectorElementSizeChangeStrategies[OpcodeIdx].resize(TypeIdx + 1);
814
    VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx] = S;
815
  }
816
817
  /// A SizeChangeStrategy for the common case where legalization for a 
818
  /// particular operation consists of only supporting a specific set of type
819
  /// sizes. E.g.
820
  ///   setAction ({G_DIV, 0, LLT::scalar(32)}, Legal);
821
  ///   setAction ({G_DIV, 0, LLT::scalar(64)}, Legal);
822
  ///   setLegalizeScalarToDifferentSizeStrategy(
823
  ///     G_DIV, 0, unsupportedForDifferentSizes);
824
  /// will result in getAction({G_DIV, 0, T}) to return Legal for s32 and s64,
825
  /// and Unsupported for all other scalar types T.
826
  static SizeAndActionsVec
827
1.00M
  unsupportedForDifferentSizes(const SizeAndActionsVec &v) {
828
1.00M
    using namespace LegalizeActions;
829
1.00M
    return increaseToLargerTypesAndDecreaseToLargest(v, Unsupported,
830
1.00M
                                                     Unsupported);
831
1.00M
  }
832
833
  /// A SizeChangeStrategy for the common case where legalization for a
834
  /// particular operation consists of widening the type to a large legal type,
835
  /// unless there is no such type and then instead it should be narrowed to the
836
  /// largest legal type.
837
  static SizeAndActionsVec
838
28.8k
  widenToLargerTypesAndNarrowToLargest(const SizeAndActionsVec &v) {
839
28.8k
    using namespace LegalizeActions;
840
28.8k
    assert(v.size() > 0 &&
841
28.8k
           "At least one size that can be legalized towards is needed"
842
28.8k
           " for this SizeChangeStrategy");
843
28.8k
    return increaseToLargerTypesAndDecreaseToLargest(v, WidenScalar,
844
28.8k
                                                     NarrowScalar);
845
28.8k
  }
846
847
  static SizeAndActionsVec
848
24.3k
  widenToLargerTypesUnsupportedOtherwise(const SizeAndActionsVec &v) {
849
24.3k
    using namespace LegalizeActions;
850
24.3k
    return increaseToLargerTypesAndDecreaseToLargest(v, WidenScalar,
851
24.3k
                                                     Unsupported);
852
24.3k
  }
853
854
  static SizeAndActionsVec
855
22.1k
  narrowToSmallerAndUnsupportedIfTooSmall(const SizeAndActionsVec &v) {
856
22.1k
    using namespace LegalizeActions;
857
22.1k
    return decreaseToSmallerTypesAndIncreaseToSmallest(v, NarrowScalar,
858
22.1k
                                                       Unsupported);
859
22.1k
  }
860
861
  static SizeAndActionsVec
862
24.3k
  narrowToSmallerAndWidenToSmallest(const SizeAndActionsVec &v) {
863
24.3k
    using namespace LegalizeActions;
864
24.3k
    assert(v.size() > 0 &&
865
24.3k
           "At least one size that can be legalized towards is needed"
866
24.3k
           " for this SizeChangeStrategy");
867
24.3k
    return decreaseToSmallerTypesAndIncreaseToSmallest(v, NarrowScalar,
868
24.3k
                                                       WidenScalar);
869
24.3k
  }
870
871
  /// A SizeChangeStrategy for the common case where legalization for a
872
  /// particular vector operation consists of having more elements in the
873
  /// vector, to a type that is legal. Unless there is no such type and then
874
  /// instead it should be legalized towards the widest vector that's still
875
  /// legal. E.g.
876
  ///   setAction({G_ADD, LLT::vector(8, 8)}, Legal);
877
  ///   setAction({G_ADD, LLT::vector(16, 8)}, Legal);
878
  ///   setAction({G_ADD, LLT::vector(2, 32)}, Legal);
879
  ///   setAction({G_ADD, LLT::vector(4, 32)}, Legal);
880
  ///   setLegalizeVectorElementToDifferentSizeStrategy(
881
  ///     G_ADD, 0, moreToWiderTypesAndLessToWidest);
882
  /// will result in the following getAction results:
883
  ///   * getAction({G_ADD, LLT::vector(8,8)}) returns
884
  ///       (Legal, vector(8,8)).
885
  ///   * getAction({G_ADD, LLT::vector(9,8)}) returns
886
  ///       (MoreElements, vector(16,8)).
887
  ///   * getAction({G_ADD, LLT::vector(8,32)}) returns
888
  ///       (FewerElements, vector(4,32)).
889
  static SizeAndActionsVec
890
456k
  moreToWiderTypesAndLessToWidest(const SizeAndActionsVec &v) {
891
456k
    using namespace LegalizeActions;
892
456k
    return increaseToLargerTypesAndDecreaseToLargest(v, MoreElements,
893
456k
                                                     FewerElements);
894
456k
  }
895
896
  /// Helper function to implement many typical SizeChangeStrategy functions.
897
  static SizeAndActionsVec
898
  increaseToLargerTypesAndDecreaseToLargest(const SizeAndActionsVec &v,
899
                                            LegalizeAction IncreaseAction,
900
                                            LegalizeAction DecreaseAction);
901
  /// Helper function to implement many typical SizeChangeStrategy functions.
902
  static SizeAndActionsVec
903
  decreaseToSmallerTypesAndIncreaseToSmallest(const SizeAndActionsVec &v,
904
                                              LegalizeAction DecreaseAction,
905
                                              LegalizeAction IncreaseAction);
906
907
  /// Get the action definitions for the given opcode. Use this to run a
908
  /// LegalityQuery through the definitions.
909
  const LegalizeRuleSet &getActionDefinitions(unsigned Opcode) const;
910
911
  /// Get the action definition builder for the given opcode. Use this to define
912
  /// the action definitions.
913
  ///
914
  /// It is an error to request an opcode that has already been requested by the
915
  /// multiple-opcode variant.
916
  LegalizeRuleSet &getActionDefinitionsBuilder(unsigned Opcode);
917
918
  /// Get the action definition builder for the given set of opcodes. Use this
919
  /// to define the action definitions for multiple opcodes at once. The first
920
  /// opcode given will be considered the representative opcode and will hold
921
  /// the definitions whereas the other opcodes will be configured to refer to
922
  /// the representative opcode. This lowers memory requirements and very
923
  /// slightly improves performance.
924
  ///
925
  /// It would be very easy to introduce unexpected side-effects as a result of
926
  /// this aliasing if it were permitted to request different but intersecting
927
  /// sets of opcodes but that is difficult to keep track of. It is therefore an
928
  /// error to request the same opcode twice using this API, to request an
929
  /// opcode that already has definitions, or to use the single-opcode API on an
930
  /// opcode that has already been requested by this API.
931
  LegalizeRuleSet &
932
  getActionDefinitionsBuilder(std::initializer_list<unsigned> Opcodes);
933
  void aliasActionDefinitions(unsigned OpcodeTo, unsigned OpcodeFrom);
934
935
  /// Determine what action should be taken to legalize the described
936
  /// instruction. Requires computeTables to have been called.
937
  ///
938
  /// \returns a description of the next legalization step to perform.
939
  LegalizeActionStep getAction(const LegalityQuery &Query) const;
940
941
  /// Determine what action should be taken to legalize the given generic
942
  /// instruction.
943
  ///
944
  /// \returns a description of the next legalization step to perform.
945
  LegalizeActionStep getAction(const MachineInstr &MI,
946
                               const MachineRegisterInfo &MRI) const;
947
948
  bool isLegal(const MachineInstr &MI, const MachineRegisterInfo &MRI) const;
949
950
  virtual bool legalizeCustom(MachineInstr &MI,
951
                              MachineRegisterInfo &MRI,
952
                              MachineIRBuilder &MIRBuilder) const;
953
954
private:
955
  /// Determine what action should be taken to legalize the given generic
956
  /// instruction opcode, type-index and type. Requires computeTables to have
957
  /// been called.
958
  ///
959
  /// \returns a pair consisting of the kind of legalization that should be
960
  /// performed and the destination type.
961
  std::pair<LegalizeAction, LLT>
962
  getAspectAction(const InstrAspect &Aspect) const;
963
964
  /// The SizeAndActionsVec is a representation mapping between all natural
965
  /// numbers and an Action. The natural number represents the bit size of
966
  /// the InstrAspect. For example, for a target with native support for 32-bit
967
  /// and 64-bit additions, you'd express that as:
968
  /// setScalarAction(G_ADD, 0,
969
  ///           {{1, WidenScalar},  // bit sizes [ 1, 31[
970
  ///            {32, Legal},       // bit sizes [32, 33[
971
  ///            {33, WidenScalar}, // bit sizes [33, 64[
972
  ///            {64, Legal},       // bit sizes [64, 65[
973
  ///            {65, NarrowScalar} // bit sizes [65, +inf[
974
  ///           });
975
  /// It may be that only 64-bit pointers are supported on your target:
976
  /// setPointerAction(G_GEP, 0, LLT:pointer(1),
977
  ///           {{1, Unsupported},  // bit sizes [ 1, 63[
978
  ///            {64, Legal},       // bit sizes [64, 65[
979
  ///            {65, Unsupported}, // bit sizes [65, +inf[
980
  ///           });
981
  void setScalarAction(const unsigned Opcode, const unsigned TypeIndex,
982
837k
                       const SizeAndActionsVec &SizeAndActions) {
983
837k
    const unsigned OpcodeIdx = Opcode - FirstOp;
984
837k
    SmallVector<SizeAndActionsVec, 1> &Actions = ScalarActions[OpcodeIdx];
985
837k
    setActions(TypeIndex, Actions, SizeAndActions);
986
837k
  }
987
  void setPointerAction(const unsigned Opcode, const unsigned TypeIndex,
988
                        const unsigned AddressSpace,
989
145k
                        const SizeAndActionsVec &SizeAndActions) {
990
145k
    const unsigned OpcodeIdx = Opcode - FirstOp;
991
145k
    if (AddrSpace2PointerActions[OpcodeIdx].find(AddressSpace) ==
992
145k
        AddrSpace2PointerActions[OpcodeIdx].end())
993
120k
      AddrSpace2PointerActions[OpcodeIdx][AddressSpace] = {{}};
994
145k
    SmallVector<SizeAndActionsVec, 1> &Actions =
995
145k
        AddrSpace2PointerActions[OpcodeIdx].find(AddressSpace)->second;
996
145k
    setActions(TypeIndex, Actions, SizeAndActions);
997
145k
  }
998
999
  /// If an operation on a given vector type (say <M x iN>) isn't explicitly
1000
  /// specified, we proceed in 2 stages. First we legalize the underlying scalar
1001
  /// (so that there's at least one legal vector with that scalar), then we
1002
  /// adjust the number of elements in the vector so that it is legal. The
1003
  /// desired action in the first step is controlled by this function.
1004
  void setScalarInVectorAction(const unsigned Opcode, const unsigned TypeIndex,
1005
522k
                               const SizeAndActionsVec &SizeAndActions) {
1006
522k
    unsigned OpcodeIdx = Opcode - FirstOp;
1007
522k
    SmallVector<SizeAndActionsVec, 1> &Actions =
1008
522k
        ScalarInVectorActions[OpcodeIdx];
1009
522k
    setActions(TypeIndex, Actions, SizeAndActions);
1010
522k
  }
1011
1012
  /// See also setScalarInVectorAction.
1013
  /// This function let's you specify the number of elements in a vector that
1014
  /// are legal for a legal element size.
1015
  void setVectorNumElementAction(const unsigned Opcode,
1016
                                 const unsigned TypeIndex,
1017
                                 const unsigned ElementSize,
1018
456k
                                 const SizeAndActionsVec &SizeAndActions) {
1019
456k
    const unsigned OpcodeIdx = Opcode - FirstOp;
1020
456k
    if (NumElements2Actions[OpcodeIdx].find(ElementSize) ==
1021
456k
        NumElements2Actions[OpcodeIdx].end())
1022
342k
      NumElements2Actions[OpcodeIdx][ElementSize] = {{}};
1023
456k
    SmallVector<SizeAndActionsVec, 1> &Actions =
1024
456k
        NumElements2Actions[OpcodeIdx].find(ElementSize)->second;
1025
456k
    setActions(TypeIndex, Actions, SizeAndActions);
1026
456k
  }
1027
1028
  /// A partial SizeAndActionsVec potentially doesn't cover all bit sizes,
1029
  /// i.e. it's OK if it doesn't start from size 1.
1030
1.12M
  static void checkPartialSizeAndActionsVector(const SizeAndActionsVec& v) {
1031
1.12M
    using namespace LegalizeActions;
1032
1.12M
#ifndef NDEBUG
1033
1.12M
    // The sizes should be in increasing order
1034
1.12M
    int prev_size = -1;
1035
1.12M
    for(auto SizeAndAction: v) {
1036
1.12M
      assert(SizeAndAction.first > prev_size);
1037
1.12M
      prev_size = SizeAndAction.first;
1038
1.12M
    }
1039
1.12M
    // - for every Widen action, there should be a larger bitsize that
1040
1.12M
    //   can be legalized towards (e.g. Legal, Lower, Libcall or Custom
1041
1.12M
    //   action).
1042
1.12M
    // - for every Narrow action, there should be a smaller bitsize that
1043
1.12M
    //   can be legalized towards.
1044
1.12M
    int SmallestNarrowIdx = -1;
1045
1.12M
    int LargestWidenIdx = -1;
1046
1.12M
    int SmallestLegalizableToSameSizeIdx = -1;
1047
1.12M
    int LargestLegalizableToSameSizeIdx = -1;
1048
1.12M
    for(size_t i=0; i<v.size(); ++i) {
1049
1.12M
      switch (v[i].second) {
1050
1.12M
        case FewerElements:
1051
1.12M
        case NarrowScalar:
1052
1.12M
          if (SmallestNarrowIdx == -1)
1053
1.12M
            SmallestNarrowIdx = i;
1054
1.12M
          break;
1055
1.12M
        case WidenScalar:
1056
1.12M
        case MoreElements:
1057
1.12M
          LargestWidenIdx = i;
1058
1.12M
          break;
1059
1.12M
        case Unsupported:
1060
1.12M
          break;
1061
1.12M
        default:
1062
1.12M
          if (SmallestLegalizableToSameSizeIdx == -1)
1063
1.12M
            SmallestLegalizableToSameSizeIdx = i;
1064
1.12M
          LargestLegalizableToSameSizeIdx = i;
1065
1.12M
      }
1066
1.12M
    }
1067
1.12M
    if (SmallestNarrowIdx != -1) {
1068
1.12M
      assert(SmallestLegalizableToSameSizeIdx != -1);
1069
1.12M
      assert(SmallestNarrowIdx > SmallestLegalizableToSameSizeIdx);
1070
1.12M
    }
1071
1.12M
    if (LargestWidenIdx != -1)
1072
1.12M
      assert(LargestWidenIdx < LargestLegalizableToSameSizeIdx);
1073
1.12M
#endif
1074
1.12M
  }
1075
1076
  /// A full SizeAndActionsVec must cover all bit sizes, i.e. must start with
1077
  /// from size 1.
1078
1.96M
  static void checkFullSizeAndActionsVector(const SizeAndActionsVec& v) {
1079
1.96M
#ifndef NDEBUG
1080
1.96M
    // Data structure invariant: The first bit size must be size 1.
1081
1.96M
    assert(v.size() >= 1);
1082
1.96M
    assert(v[0].first == 1);
1083
1.96M
    checkPartialSizeAndActionsVector(v);
1084
1.96M
#endif
1085
1.96M
  }
1086
1087
  /// Sets actions for all bit sizes on a particular generic opcode, type
1088
  /// index and scalar or pointer type.
1089
  void setActions(unsigned TypeIndex,
1090
                  SmallVector<SizeAndActionsVec, 1> &Actions,
1091
1.96M
                  const SizeAndActionsVec &SizeAndActions) {
1092
1.96M
    checkFullSizeAndActionsVector(SizeAndActions);
1093
1.96M
    if (Actions.size() <= TypeIndex)
1094
1.46M
      Actions.resize(TypeIndex + 1);
1095
1.96M
    Actions[TypeIndex] = SizeAndActions;
1096
1.96M
  }
1097
1098
  static SizeAndAction findAction(const SizeAndActionsVec &Vec,
1099
                                  const uint32_t Size);
1100
1101
  /// Returns the next action needed to get the scalar or pointer type closer
1102
  /// to being legal
1103
  /// E.g. findLegalAction({G_REM, 13}) should return
1104
  /// (WidenScalar, 32). After that, findLegalAction({G_REM, 32}) will
1105
  /// probably be called, which should return (Lower, 32).
1106
  /// This is assuming the setScalarAction on G_REM was something like:
1107
  /// setScalarAction(G_REM, 0,
1108
  ///           {{1, WidenScalar},  // bit sizes [ 1, 31[
1109
  ///            {32, Lower},       // bit sizes [32, 33[
1110
  ///            {33, NarrowScalar} // bit sizes [65, +inf[
1111
  ///           });
1112
  std::pair<LegalizeAction, LLT>
1113
  findScalarLegalAction(const InstrAspect &Aspect) const;
1114
1115
  /// Returns the next action needed towards legalizing the vector type.
1116
  std::pair<LegalizeAction, LLT>
1117
  findVectorLegalAction(const InstrAspect &Aspect) const;
1118
1119
  static const int FirstOp = TargetOpcode::PRE_ISEL_GENERIC_OPCODE_START;
1120
  static const int LastOp = TargetOpcode::PRE_ISEL_GENERIC_OPCODE_END;
1121
1122
  // Data structures used temporarily during construction of legality data:
1123
  using TypeMap = DenseMap<LLT, LegalizeAction>;
1124
  SmallVector<TypeMap, 1> SpecifiedActions[LastOp - FirstOp + 1];
1125
  SmallVector<SizeChangeStrategy, 1>
1126
      ScalarSizeChangeStrategies[LastOp - FirstOp + 1];
1127
  SmallVector<SizeChangeStrategy, 1>
1128
      VectorElementSizeChangeStrategies[LastOp - FirstOp + 1];
1129
  bool TablesInitialized;
1130
1131
  // Data structures used by getAction:
1132
  SmallVector<SizeAndActionsVec, 1> ScalarActions[LastOp - FirstOp + 1];
1133
  SmallVector<SizeAndActionsVec, 1> ScalarInVectorActions[LastOp - FirstOp + 1];
1134
  std::unordered_map<uint16_t, SmallVector<SizeAndActionsVec, 1>>
1135
      AddrSpace2PointerActions[LastOp - FirstOp + 1];
1136
  std::unordered_map<uint16_t, SmallVector<SizeAndActionsVec, 1>>
1137
      NumElements2Actions[LastOp - FirstOp + 1];
1138
1139
  LegalizeRuleSet RulesForOpcode[LastOp - FirstOp + 1];
1140
};
1141
1142
#ifndef NDEBUG
1143
/// Checks that MIR is fully legal, returns an illegal instruction if it's not,
1144
/// nullptr otherwise
1145
const MachineInstr *machineFunctionIsIllegal(const MachineFunction &MF);
1146
#endif
1147
1148
} // end namespace llvm.
1149
1150
#endif // LLVM_CODEGEN_GLOBALISEL_LEGALIZERINFO_H