1 //===- llvm/CodeGen/GlobalISel/LegalizerInfo.h ------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 /// Interface for Targets to specify which operations they can successfully
10 /// select and how the others should be expanded most efficiently.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_GLOBALISEL_LEGALIZERINFO_H
15 #define LLVM_CODEGEN_GLOBALISEL_LEGALIZERINFO_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallBitVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/TargetOpcodes.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/LowLevelTypeImpl.h"
27 #include "llvm/Support/raw_ostream.h"
31 #include <unordered_map>
36 extern cl::opt<bool> DisableGISelLegalityCheck;
39 class MachineIRBuilder;
40 class MachineRegisterInfo;
42 class GISelChangeObserver;
44 namespace LegalizeActions {
45 enum LegalizeAction : std::uint8_t {
46 /// The operation is expected to be selectable directly by the target, and
47 /// no transformation is necessary.
50 /// The operation should be synthesized from multiple instructions acting on
51 /// a narrower scalar base-type. For example a 64-bit add might be
52 /// implemented in terms of 32-bit add-with-carry.
55 /// The operation should be implemented in terms of a wider scalar
56 /// base-type. For example a <2 x s8> add could be implemented as a <2
57 /// x s32> add (ignoring the high bits).
60 /// The (vector) operation should be implemented by splitting it into
61 /// sub-vectors where the operation is legal. For example a <8 x s64> add
62 /// might be implemented as 4 separate <2 x s64> adds.
65 /// The (vector) operation should be implemented by widening the input
66 /// vector and ignoring the lanes added by doing so. For example <2 x i8> is
67 /// rarely legal, but you might perform an <8 x i8> and then only look at
68 /// the first two results.
71 /// The operation itself must be expressed in terms of simpler actions on
72 /// this target. E.g. a SREM replaced by an SDIV and subtraction.
75 /// The operation should be implemented as a call to some kind of runtime
76 /// support library. For example this usually happens on machines that don't
77 /// support floating-point operations natively.
80 /// The target wants to do something special with this combination of
81 /// operand and type. A callback will be issued when it is needed.
84 /// This operation is completely unsupported on the target. A programming
85 /// error has occurred.
88 /// Sentinel value for when no action was found in the specified table.
91 /// Fall back onto the old rules.
92 /// TODO: Remove this once we've migrated
95 } // end namespace LegalizeActions
96 raw_ostream &operator<<(raw_ostream &OS, LegalizeActions::LegalizeAction Action);
98 using LegalizeActions::LegalizeAction;
100 /// Legalization is decided based on an instruction's opcode, which type slot
101 /// we're considering, and what the existing type is. These aspects are gathered
102 /// together for convenience in the InstrAspect class.
108 InstrAspect(unsigned Opcode, LLT Type) : Opcode(Opcode), Type(Type) {}
109 InstrAspect(unsigned Opcode, unsigned Idx, LLT Type)
110 : Opcode(Opcode), Idx(Idx), Type(Type) {}
112 bool operator==(const InstrAspect &RHS) const {
113 return Opcode == RHS.Opcode && Idx == RHS.Idx && Type == RHS.Type;
117 /// The LegalityQuery object bundles together all the information that's needed
118 /// to decide whether a given operation is legal or not.
119 /// For efficiency, it doesn't make a copy of Types so care must be taken not
120 /// to free it before using the query.
121 struct LegalityQuery {
127 uint64_t AlignInBits;
128 AtomicOrdering Ordering;
131 /// Operations which require memory can use this to place requirements on the
132 /// memory type for each MMO.
133 ArrayRef<MemDesc> MMODescrs;
135 constexpr LegalityQuery(unsigned Opcode, const ArrayRef<LLT> Types,
136 const ArrayRef<MemDesc> MMODescrs)
137 : Opcode(Opcode), Types(Types), MMODescrs(MMODescrs) {}
138 constexpr LegalityQuery(unsigned Opcode, const ArrayRef<LLT> Types)
139 : LegalityQuery(Opcode, Types, {}) {}
141 raw_ostream &print(raw_ostream &OS) const;
144 /// The result of a query. It either indicates a final answer of Legal or
145 /// Unsupported or describes an action that must be taken to make an operation
147 struct LegalizeActionStep {
148 /// The action to take or the final answer.
149 LegalizeAction Action;
150 /// If describing an action, the type index to change. Otherwise zero.
152 /// If describing an action, the new type for TypeIdx. Otherwise LLT{}.
155 LegalizeActionStep(LegalizeAction Action, unsigned TypeIdx,
157 : Action(Action), TypeIdx(TypeIdx), NewType(NewType) {}
159 bool operator==(const LegalizeActionStep &RHS) const {
160 return std::tie(Action, TypeIdx, NewType) ==
161 std::tie(RHS.Action, RHS.TypeIdx, RHS.NewType);
165 using LegalityPredicate = std::function<bool (const LegalityQuery &)>;
166 using LegalizeMutation =
167 std::function<std::pair<unsigned, LLT>(const LegalityQuery &)>;
169 namespace LegalityPredicates {
170 struct TypePairAndMemDesc {
176 bool operator==(const TypePairAndMemDesc &Other) const {
177 return Type0 == Other.Type0 && Type1 == Other.Type1 &&
178 Align == Other.Align &&
179 MemSize == Other.MemSize;
182 /// \returns true if this memory access is legal with for the acecss described
183 /// by \p Other (The alignment is sufficient for the size and result type).
184 bool isCompatible(const TypePairAndMemDesc &Other) const {
185 return Type0 == Other.Type0 && Type1 == Other.Type1 &&
186 Align >= Other.Align &&
187 MemSize == Other.MemSize;
191 /// True iff P0 and P1 are true.
192 template<typename Predicate>
193 Predicate all(Predicate P0, Predicate P1) {
194 return [=](const LegalityQuery &Query) {
195 return P0(Query) && P1(Query);
198 /// True iff all given predicates are true.
199 template<typename Predicate, typename... Args>
200 Predicate all(Predicate P0, Predicate P1, Args... args) {
201 return all(all(P0, P1), args...);
203 /// True iff the given type index is the specified types.
204 LegalityPredicate typeIs(unsigned TypeIdx, LLT TypesInit);
205 /// True iff the given type index is one of the specified types.
206 LegalityPredicate typeInSet(unsigned TypeIdx,
207 std::initializer_list<LLT> TypesInit);
208 /// True iff the given types for the given pair of type indexes is one of the
209 /// specified type pairs.
211 typePairInSet(unsigned TypeIdx0, unsigned TypeIdx1,
212 std::initializer_list<std::pair<LLT, LLT>> TypesInit);
213 /// True iff the given types for the given pair of type indexes is one of the
214 /// specified type pairs.
215 LegalityPredicate typePairAndMemDescInSet(
216 unsigned TypeIdx0, unsigned TypeIdx1, unsigned MMOIdx,
217 std::initializer_list<TypePairAndMemDesc> TypesAndMemDescInit);
218 /// True iff the specified type index is a scalar.
219 LegalityPredicate isScalar(unsigned TypeIdx);
220 /// True iff the specified type index is a vector.
221 LegalityPredicate isVector(unsigned TypeIdx);
222 /// True iff the specified type index is a pointer (with any address space).
223 LegalityPredicate isPointer(unsigned TypeIdx);
224 /// True iff the specified type index is a pointer with the specified address
226 LegalityPredicate isPointer(unsigned TypeIdx, unsigned AddrSpace);
228 /// True iff the specified type index is a scalar that's narrower than the given
230 LegalityPredicate narrowerThan(unsigned TypeIdx, unsigned Size);
232 /// True iff the specified type index is a scalar that's wider than the given
234 LegalityPredicate widerThan(unsigned TypeIdx, unsigned Size);
236 /// True iff the specified type index is a scalar or vector with an element type
237 /// that's narrower than the given size.
238 LegalityPredicate scalarOrEltNarrowerThan(unsigned TypeIdx, unsigned Size);
240 /// True iff the specified type index is a scalar or a vector with an element
241 /// type that's wider than the given size.
242 LegalityPredicate scalarOrEltWiderThan(unsigned TypeIdx, unsigned Size);
244 /// True iff the specified type index is a scalar whose size is not a power of
246 LegalityPredicate sizeNotPow2(unsigned TypeIdx);
248 /// True iff the specified type index is a scalar or vector whose element size
249 /// is not a power of 2.
250 LegalityPredicate scalarOrEltSizeNotPow2(unsigned TypeIdx);
252 /// True iff the specified type indices are both the same bit size.
253 LegalityPredicate sameSize(unsigned TypeIdx0, unsigned TypeIdx1);
254 /// True iff the specified MMO index has a size that is not a power of 2
255 LegalityPredicate memSizeInBytesNotPow2(unsigned MMOIdx);
256 /// True iff the specified type index is a vector whose element count is not a
258 LegalityPredicate numElementsNotPow2(unsigned TypeIdx);
259 /// True iff the specified MMO index has at an atomic ordering of at Ordering or
261 LegalityPredicate atomicOrderingAtLeastOrStrongerThan(unsigned MMOIdx,
262 AtomicOrdering Ordering);
263 } // end namespace LegalityPredicates
265 namespace LegalizeMutations {
266 /// Select this specific type for the given type index.
267 LegalizeMutation changeTo(unsigned TypeIdx, LLT Ty);
269 /// Keep the same type as the given type index.
270 LegalizeMutation changeTo(unsigned TypeIdx, unsigned FromTypeIdx);
272 /// Keep the same scalar or element type as the given type index.
273 LegalizeMutation changeElementTo(unsigned TypeIdx, unsigned FromTypeIdx);
275 /// Keep the same scalar or element type as the given type.
276 LegalizeMutation changeElementTo(unsigned TypeIdx, LLT Ty);
278 /// Widen the scalar type or vector element type for the given type index to the
280 LegalizeMutation widenScalarOrEltToNextPow2(unsigned TypeIdx, unsigned Min = 0);
282 /// Add more elements to the type for the given type index to the next power of
284 LegalizeMutation moreElementsToNextPow2(unsigned TypeIdx, unsigned Min = 0);
285 /// Break up the vector type for the given type index into the element type.
286 LegalizeMutation scalarize(unsigned TypeIdx);
287 } // end namespace LegalizeMutations
289 /// A single rule in a legalizer info ruleset.
290 /// The specified action is chosen when the predicate is true. Where appropriate
291 /// for the action (e.g. for WidenScalar) the new type is selected using the
294 LegalityPredicate Predicate;
295 LegalizeAction Action;
296 LegalizeMutation Mutation;
299 LegalizeRule(LegalityPredicate Predicate, LegalizeAction Action,
300 LegalizeMutation Mutation = nullptr)
301 : Predicate(Predicate), Action(Action), Mutation(Mutation) {}
303 /// Test whether the LegalityQuery matches.
304 bool match(const LegalityQuery &Query) const {
305 return Predicate(Query);
308 LegalizeAction getAction() const { return Action; }
310 /// Determine the change to make.
311 std::pair<unsigned, LLT> determineMutation(const LegalityQuery &Query) const {
313 return Mutation(Query);
314 return std::make_pair(0, LLT{});
318 class LegalizeRuleSet {
319 /// When non-zero, the opcode we are an alias of
321 /// If true, there is another opcode that aliases this one
322 bool IsAliasedByAnother;
323 SmallVector<LegalizeRule, 2> Rules;
326 /// If bit I is set, this rule set contains a rule that may handle (predicate
327 /// or perform an action upon (or both)) the type index I. The uncertainty
328 /// comes from free-form rules executing user-provided lambda functions. We
329 /// conservatively assume such rules do the right thing and cover all type
330 /// indices. The bitset is intentionally 1 bit wider than it absolutely needs
331 /// to be to distinguish such cases from the cases where all type indices are
332 /// individually handled.
333 SmallBitVector TypeIdxsCovered{MCOI::OPERAND_LAST_GENERIC -
334 MCOI::OPERAND_FIRST_GENERIC + 2};
335 SmallBitVector ImmIdxsCovered{MCOI::OPERAND_LAST_GENERIC_IMM -
336 MCOI::OPERAND_FIRST_GENERIC_IMM + 2};
339 unsigned typeIdx(unsigned TypeIdx) {
341 (MCOI::OPERAND_LAST_GENERIC - MCOI::OPERAND_FIRST_GENERIC) &&
342 "Type Index is out of bounds");
344 TypeIdxsCovered.set(TypeIdx);
349 unsigned immIdx(unsigned ImmIdx) {
350 assert(ImmIdx <= (MCOI::OPERAND_LAST_GENERIC_IMM -
351 MCOI::OPERAND_FIRST_GENERIC_IMM) &&
352 "Imm Index is out of bounds");
354 ImmIdxsCovered.set(ImmIdx);
359 void markAllIdxsAsCovered() {
361 TypeIdxsCovered.set();
362 ImmIdxsCovered.set();
366 void add(const LegalizeRule &Rule) {
367 assert(AliasOf == 0 &&
368 "RuleSet is aliased, change the representative opcode instead");
369 Rules.push_back(Rule);
372 static bool always(const LegalityQuery &) { return true; }
374 /// Use the given action when the predicate is true.
375 /// Action should not be an action that requires mutation.
376 LegalizeRuleSet &actionIf(LegalizeAction Action,
377 LegalityPredicate Predicate) {
378 add({Predicate, Action});
381 /// Use the given action when the predicate is true.
382 /// Action should be an action that requires mutation.
383 LegalizeRuleSet &actionIf(LegalizeAction Action, LegalityPredicate Predicate,
384 LegalizeMutation Mutation) {
385 add({Predicate, Action, Mutation});
388 /// Use the given action when type index 0 is any type in the given list.
389 /// Action should not be an action that requires mutation.
390 LegalizeRuleSet &actionFor(LegalizeAction Action,
391 std::initializer_list<LLT> Types) {
392 using namespace LegalityPredicates;
393 return actionIf(Action, typeInSet(typeIdx(0), Types));
395 /// Use the given action when type index 0 is any type in the given list.
396 /// Action should be an action that requires mutation.
397 LegalizeRuleSet &actionFor(LegalizeAction Action,
398 std::initializer_list<LLT> Types,
399 LegalizeMutation Mutation) {
400 using namespace LegalityPredicates;
401 return actionIf(Action, typeInSet(typeIdx(0), Types), Mutation);
403 /// Use the given action when type indexes 0 and 1 is any type pair in the
405 /// Action should not be an action that requires mutation.
406 LegalizeRuleSet &actionFor(LegalizeAction Action,
407 std::initializer_list<std::pair<LLT, LLT>> Types) {
408 using namespace LegalityPredicates;
409 return actionIf(Action, typePairInSet(typeIdx(0), typeIdx(1), Types));
411 /// Use the given action when type indexes 0 and 1 is any type pair in the
413 /// Action should be an action that requires mutation.
414 LegalizeRuleSet &actionFor(LegalizeAction Action,
415 std::initializer_list<std::pair<LLT, LLT>> Types,
416 LegalizeMutation Mutation) {
417 using namespace LegalityPredicates;
418 return actionIf(Action, typePairInSet(typeIdx(0), typeIdx(1), Types),
421 /// Use the given action when type index 0 is any type in the given list and
422 /// imm index 0 is anything. Action should not be an action that requires
424 LegalizeRuleSet &actionForTypeWithAnyImm(LegalizeAction Action,
425 std::initializer_list<LLT> Types) {
426 using namespace LegalityPredicates;
427 immIdx(0); // Inform verifier imm idx 0 is handled.
428 return actionIf(Action, typeInSet(typeIdx(0), Types));
430 /// Use the given action when type indexes 0 and 1 are both in the given list.
431 /// That is, the type pair is in the cartesian product of the list.
432 /// Action should not be an action that requires mutation.
433 LegalizeRuleSet &actionForCartesianProduct(LegalizeAction Action,
434 std::initializer_list<LLT> Types) {
435 using namespace LegalityPredicates;
436 return actionIf(Action, all(typeInSet(typeIdx(0), Types),
437 typeInSet(typeIdx(1), Types)));
439 /// Use the given action when type indexes 0 and 1 are both in their
440 /// respective lists.
441 /// That is, the type pair is in the cartesian product of the lists
442 /// Action should not be an action that requires mutation.
444 actionForCartesianProduct(LegalizeAction Action,
445 std::initializer_list<LLT> Types0,
446 std::initializer_list<LLT> Types1) {
447 using namespace LegalityPredicates;
448 return actionIf(Action, all(typeInSet(typeIdx(0), Types0),
449 typeInSet(typeIdx(1), Types1)));
451 /// Use the given action when type indexes 0, 1, and 2 are all in their
452 /// respective lists.
453 /// That is, the type triple is in the cartesian product of the lists
454 /// Action should not be an action that requires mutation.
455 LegalizeRuleSet &actionForCartesianProduct(
456 LegalizeAction Action, std::initializer_list<LLT> Types0,
457 std::initializer_list<LLT> Types1, std::initializer_list<LLT> Types2) {
458 using namespace LegalityPredicates;
459 return actionIf(Action, all(typeInSet(typeIdx(0), Types0),
460 all(typeInSet(typeIdx(1), Types1),
461 typeInSet(typeIdx(2), Types2))));
465 LegalizeRuleSet() : AliasOf(0), IsAliasedByAnother(false), Rules() {}
467 bool isAliasedByAnother() { return IsAliasedByAnother; }
468 void setIsAliasedByAnother() { IsAliasedByAnother = true; }
469 void aliasTo(unsigned Opcode) {
470 assert((AliasOf == 0 || AliasOf == Opcode) &&
471 "Opcode is already aliased to another opcode");
472 assert(Rules.empty() && "Aliasing will discard rules");
475 unsigned getAlias() const { return AliasOf; }
477 /// The instruction is legal if predicate is true.
478 LegalizeRuleSet &legalIf(LegalityPredicate Predicate) {
479 // We have no choice but conservatively assume that the free-form
480 // user-provided Predicate properly handles all type indices:
481 markAllIdxsAsCovered();
482 return actionIf(LegalizeAction::Legal, Predicate);
484 /// The instruction is legal when type index 0 is any type in the given list.
485 LegalizeRuleSet &legalFor(std::initializer_list<LLT> Types) {
486 return actionFor(LegalizeAction::Legal, Types);
488 /// The instruction is legal when type indexes 0 and 1 is any type pair in the
490 LegalizeRuleSet &legalFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
491 return actionFor(LegalizeAction::Legal, Types);
493 /// The instruction is legal when type index 0 is any type in the given list
494 /// and imm index 0 is anything.
495 LegalizeRuleSet &legalForTypeWithAnyImm(std::initializer_list<LLT> Types) {
496 markAllIdxsAsCovered();
497 return actionForTypeWithAnyImm(LegalizeAction::Legal, Types);
499 /// The instruction is legal when type indexes 0 and 1 along with the memory
500 /// size and minimum alignment is any type and size tuple in the given list.
501 LegalizeRuleSet &legalForTypesWithMemDesc(
502 std::initializer_list<LegalityPredicates::TypePairAndMemDesc>
504 return actionIf(LegalizeAction::Legal,
505 LegalityPredicates::typePairAndMemDescInSet(
506 typeIdx(0), typeIdx(1), /*MMOIdx*/ 0, TypesAndMemDesc));
508 /// The instruction is legal when type indexes 0 and 1 are both in the given
509 /// list. That is, the type pair is in the cartesian product of the list.
510 LegalizeRuleSet &legalForCartesianProduct(std::initializer_list<LLT> Types) {
511 return actionForCartesianProduct(LegalizeAction::Legal, Types);
513 /// The instruction is legal when type indexes 0 and 1 are both their
514 /// respective lists.
515 LegalizeRuleSet &legalForCartesianProduct(std::initializer_list<LLT> Types0,
516 std::initializer_list<LLT> Types1) {
517 return actionForCartesianProduct(LegalizeAction::Legal, Types0, Types1);
519 /// The instruction is legal when type indexes 0, 1, and 2 are both their
520 /// respective lists.
521 LegalizeRuleSet &legalForCartesianProduct(std::initializer_list<LLT> Types0,
522 std::initializer_list<LLT> Types1,
523 std::initializer_list<LLT> Types2) {
524 return actionForCartesianProduct(LegalizeAction::Legal, Types0, Types1,
528 LegalizeRuleSet &alwaysLegal() {
529 using namespace LegalizeMutations;
530 markAllIdxsAsCovered();
531 return actionIf(LegalizeAction::Legal, always);
534 /// The instruction is lowered.
535 LegalizeRuleSet &lower() {
536 using namespace LegalizeMutations;
537 // We have no choice but conservatively assume that predicate-less lowering
538 // properly handles all type indices by design:
539 markAllIdxsAsCovered();
540 return actionIf(LegalizeAction::Lower, always);
542 /// The instruction is lowered if predicate is true. Keep type index 0 as the
544 LegalizeRuleSet &lowerIf(LegalityPredicate Predicate) {
545 using namespace LegalizeMutations;
546 // We have no choice but conservatively assume that lowering with a
547 // free-form user provided Predicate properly handles all type indices:
548 markAllIdxsAsCovered();
549 return actionIf(LegalizeAction::Lower, Predicate);
551 /// The instruction is lowered if predicate is true.
552 LegalizeRuleSet &lowerIf(LegalityPredicate Predicate,
553 LegalizeMutation Mutation) {
554 // We have no choice but conservatively assume that lowering with a
555 // free-form user provided Predicate properly handles all type indices:
556 markAllIdxsAsCovered();
557 return actionIf(LegalizeAction::Lower, Predicate, Mutation);
559 /// The instruction is lowered when type index 0 is any type in the given
560 /// list. Keep type index 0 as the same type.
561 LegalizeRuleSet &lowerFor(std::initializer_list<LLT> Types) {
562 return actionFor(LegalizeAction::Lower, Types,
563 LegalizeMutations::changeTo(0, 0));
565 /// The instruction is lowered when type index 0 is any type in the given
567 LegalizeRuleSet &lowerFor(std::initializer_list<LLT> Types,
568 LegalizeMutation Mutation) {
569 return actionFor(LegalizeAction::Lower, Types, Mutation);
571 /// The instruction is lowered when type indexes 0 and 1 is any type pair in
572 /// the given list. Keep type index 0 as the same type.
573 LegalizeRuleSet &lowerFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
574 return actionFor(LegalizeAction::Lower, Types,
575 LegalizeMutations::changeTo(0, 0));
577 /// The instruction is lowered when type indexes 0 and 1 is any type pair in
579 LegalizeRuleSet &lowerFor(std::initializer_list<std::pair<LLT, LLT>> Types,
580 LegalizeMutation Mutation) {
581 return actionFor(LegalizeAction::Lower, Types, Mutation);
583 /// The instruction is lowered when type indexes 0 and 1 are both in their
584 /// respective lists.
585 LegalizeRuleSet &lowerForCartesianProduct(std::initializer_list<LLT> Types0,
586 std::initializer_list<LLT> Types1) {
587 using namespace LegalityPredicates;
588 return actionForCartesianProduct(LegalizeAction::Lower, Types0, Types1);
590 /// The instruction is lowered when when type indexes 0, 1, and 2 are all in
591 /// their respective lists.
592 LegalizeRuleSet &lowerForCartesianProduct(std::initializer_list<LLT> Types0,
593 std::initializer_list<LLT> Types1,
594 std::initializer_list<LLT> Types2) {
595 using namespace LegalityPredicates;
596 return actionForCartesianProduct(LegalizeAction::Lower, Types0, Types1,
600 /// Like legalIf, but for the Libcall action.
601 LegalizeRuleSet &libcallIf(LegalityPredicate Predicate) {
602 // We have no choice but conservatively assume that a libcall with a
603 // free-form user provided Predicate properly handles all type indices:
604 markAllIdxsAsCovered();
605 return actionIf(LegalizeAction::Libcall, Predicate);
607 LegalizeRuleSet &libcallFor(std::initializer_list<LLT> Types) {
608 return actionFor(LegalizeAction::Libcall, Types);
611 libcallFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
612 return actionFor(LegalizeAction::Libcall, Types);
615 libcallForCartesianProduct(std::initializer_list<LLT> Types) {
616 return actionForCartesianProduct(LegalizeAction::Libcall, Types);
619 libcallForCartesianProduct(std::initializer_list<LLT> Types0,
620 std::initializer_list<LLT> Types1) {
621 return actionForCartesianProduct(LegalizeAction::Libcall, Types0, Types1);
624 /// Widen the scalar to the one selected by the mutation if the predicate is
626 LegalizeRuleSet &widenScalarIf(LegalityPredicate Predicate,
627 LegalizeMutation Mutation) {
628 // We have no choice but conservatively assume that an action with a
629 // free-form user provided Predicate properly handles all type indices:
630 markAllIdxsAsCovered();
631 return actionIf(LegalizeAction::WidenScalar, Predicate, Mutation);
633 /// Narrow the scalar to the one selected by the mutation if the predicate is
635 LegalizeRuleSet &narrowScalarIf(LegalityPredicate Predicate,
636 LegalizeMutation Mutation) {
637 // We have no choice but conservatively assume that an action with a
638 // free-form user provided Predicate properly handles all type indices:
639 markAllIdxsAsCovered();
640 return actionIf(LegalizeAction::NarrowScalar, Predicate, Mutation);
643 /// Add more elements to reach the type selected by the mutation if the
644 /// predicate is true.
645 LegalizeRuleSet &moreElementsIf(LegalityPredicate Predicate,
646 LegalizeMutation Mutation) {
647 // We have no choice but conservatively assume that an action with a
648 // free-form user provided Predicate properly handles all type indices:
649 markAllIdxsAsCovered();
650 return actionIf(LegalizeAction::MoreElements, Predicate, Mutation);
652 /// Remove elements to reach the type selected by the mutation if the
653 /// predicate is true.
654 LegalizeRuleSet &fewerElementsIf(LegalityPredicate Predicate,
655 LegalizeMutation Mutation) {
656 // We have no choice but conservatively assume that an action with a
657 // free-form user provided Predicate properly handles all type indices:
658 markAllIdxsAsCovered();
659 return actionIf(LegalizeAction::FewerElements, Predicate, Mutation);
662 /// The instruction is unsupported.
663 LegalizeRuleSet &unsupported() {
664 markAllIdxsAsCovered();
665 return actionIf(LegalizeAction::Unsupported, always);
667 LegalizeRuleSet &unsupportedIf(LegalityPredicate Predicate) {
668 return actionIf(LegalizeAction::Unsupported, Predicate);
670 LegalizeRuleSet &unsupportedIfMemSizeNotPow2() {
671 return actionIf(LegalizeAction::Unsupported,
672 LegalityPredicates::memSizeInBytesNotPow2(0));
674 LegalizeRuleSet &lowerIfMemSizeNotPow2() {
675 return actionIf(LegalizeAction::Lower,
676 LegalityPredicates::memSizeInBytesNotPow2(0));
679 LegalizeRuleSet &customIf(LegalityPredicate Predicate) {
680 // We have no choice but conservatively assume that a custom action with a
681 // free-form user provided Predicate properly handles all type indices:
682 markAllIdxsAsCovered();
683 return actionIf(LegalizeAction::Custom, Predicate);
685 LegalizeRuleSet &customFor(std::initializer_list<LLT> Types) {
686 return actionFor(LegalizeAction::Custom, Types);
689 /// The instruction is custom when type indexes 0 and 1 is any type pair in the
691 LegalizeRuleSet &customFor(std::initializer_list<std::pair<LLT, LLT>> Types) {
692 return actionFor(LegalizeAction::Custom, Types);
695 LegalizeRuleSet &customForCartesianProduct(std::initializer_list<LLT> Types) {
696 return actionForCartesianProduct(LegalizeAction::Custom, Types);
699 customForCartesianProduct(std::initializer_list<LLT> Types0,
700 std::initializer_list<LLT> Types1) {
701 return actionForCartesianProduct(LegalizeAction::Custom, Types0, Types1);
704 /// Unconditionally custom lower.
705 LegalizeRuleSet &custom() {
706 return customIf(always);
709 /// Widen the scalar to the next power of two that is at least MinSize.
710 /// No effect if the type is not a scalar or is a power of two.
711 LegalizeRuleSet &widenScalarToNextPow2(unsigned TypeIdx,
712 unsigned MinSize = 0) {
713 using namespace LegalityPredicates;
715 LegalizeAction::WidenScalar, sizeNotPow2(typeIdx(TypeIdx)),
716 LegalizeMutations::widenScalarOrEltToNextPow2(TypeIdx, MinSize));
719 /// Widen the scalar or vector element type to the next power of two that is
720 /// at least MinSize. No effect if the scalar size is a power of two.
721 LegalizeRuleSet &widenScalarOrEltToNextPow2(unsigned TypeIdx,
722 unsigned MinSize = 0) {
723 using namespace LegalityPredicates;
725 LegalizeAction::WidenScalar, scalarOrEltSizeNotPow2(typeIdx(TypeIdx)),
726 LegalizeMutations::widenScalarOrEltToNextPow2(TypeIdx, MinSize));
729 LegalizeRuleSet &narrowScalar(unsigned TypeIdx, LegalizeMutation Mutation) {
730 using namespace LegalityPredicates;
731 return actionIf(LegalizeAction::NarrowScalar, isScalar(typeIdx(TypeIdx)),
735 LegalizeRuleSet &scalarize(unsigned TypeIdx) {
736 using namespace LegalityPredicates;
737 return actionIf(LegalizeAction::FewerElements, isVector(typeIdx(TypeIdx)),
738 LegalizeMutations::scalarize(TypeIdx));
741 /// Ensure the scalar or element is at least as wide as Ty.
742 LegalizeRuleSet &minScalarOrElt(unsigned TypeIdx, const LLT &Ty) {
743 using namespace LegalityPredicates;
744 using namespace LegalizeMutations;
745 return actionIf(LegalizeAction::WidenScalar,
746 scalarOrEltNarrowerThan(TypeIdx, Ty.getScalarSizeInBits()),
747 changeElementTo(typeIdx(TypeIdx), Ty));
750 /// Ensure the scalar or element is at least as wide as Ty.
751 LegalizeRuleSet &minScalarOrEltIf(LegalityPredicate Predicate,
752 unsigned TypeIdx, const LLT &Ty) {
753 using namespace LegalityPredicates;
754 using namespace LegalizeMutations;
755 return actionIf(LegalizeAction::WidenScalar,
756 all(Predicate, scalarOrEltNarrowerThan(
757 TypeIdx, Ty.getScalarSizeInBits())),
758 changeElementTo(typeIdx(TypeIdx), Ty));
761 /// Ensure the scalar is at least as wide as Ty.
762 LegalizeRuleSet &minScalar(unsigned TypeIdx, const LLT &Ty) {
763 using namespace LegalityPredicates;
764 using namespace LegalizeMutations;
765 return actionIf(LegalizeAction::WidenScalar,
766 narrowerThan(TypeIdx, Ty.getSizeInBits()),
767 changeTo(typeIdx(TypeIdx), Ty));
770 /// Ensure the scalar is at most as wide as Ty.
771 LegalizeRuleSet &maxScalarOrElt(unsigned TypeIdx, const LLT &Ty) {
772 using namespace LegalityPredicates;
773 using namespace LegalizeMutations;
774 return actionIf(LegalizeAction::NarrowScalar,
775 scalarOrEltWiderThan(TypeIdx, Ty.getScalarSizeInBits()),
776 changeElementTo(typeIdx(TypeIdx), Ty));
779 /// Ensure the scalar is at most as wide as Ty.
780 LegalizeRuleSet &maxScalar(unsigned TypeIdx, const LLT &Ty) {
781 using namespace LegalityPredicates;
782 using namespace LegalizeMutations;
783 return actionIf(LegalizeAction::NarrowScalar,
784 widerThan(TypeIdx, Ty.getSizeInBits()),
785 changeTo(typeIdx(TypeIdx), Ty));
788 /// Conditionally limit the maximum size of the scalar.
789 /// For example, when the maximum size of one type depends on the size of
790 /// another such as extracting N bits from an M bit container.
791 LegalizeRuleSet &maxScalarIf(LegalityPredicate Predicate, unsigned TypeIdx,
793 using namespace LegalityPredicates;
794 using namespace LegalizeMutations;
796 LegalizeAction::NarrowScalar,
797 [=](const LegalityQuery &Query) {
798 return widerThan(TypeIdx, Ty.getSizeInBits()) && Predicate(Query);
800 changeElementTo(typeIdx(TypeIdx), Ty));
803 /// Limit the range of scalar sizes to MinTy and MaxTy.
804 LegalizeRuleSet &clampScalar(unsigned TypeIdx, const LLT &MinTy,
806 assert(MinTy.isScalar() && MaxTy.isScalar() && "Expected scalar types");
807 return minScalar(TypeIdx, MinTy).maxScalar(TypeIdx, MaxTy);
810 /// Limit the range of scalar sizes to MinTy and MaxTy.
811 LegalizeRuleSet &clampScalarOrElt(unsigned TypeIdx, const LLT &MinTy,
813 return minScalarOrElt(TypeIdx, MinTy).maxScalarOrElt(TypeIdx, MaxTy);
816 /// Widen the scalar to match the size of another.
817 LegalizeRuleSet &minScalarSameAs(unsigned TypeIdx, unsigned LargeTypeIdx) {
819 return widenScalarIf(
820 [=](const LegalityQuery &Query) {
821 return Query.Types[LargeTypeIdx].getScalarSizeInBits() >
822 Query.Types[TypeIdx].getSizeInBits();
824 [=](const LegalityQuery &Query) {
825 LLT T = Query.Types[LargeTypeIdx];
826 return std::make_pair(TypeIdx,
827 T.isVector() ? T.getElementType() : T);
831 /// Conditionally widen the scalar or elt to match the size of another.
832 LegalizeRuleSet &minScalarEltSameAsIf(LegalityPredicate Predicate,
833 unsigned TypeIdx, unsigned LargeTypeIdx) {
835 return widenScalarIf(
836 [=](const LegalityQuery &Query) {
837 return Query.Types[LargeTypeIdx].getScalarSizeInBits() >
838 Query.Types[TypeIdx].getScalarSizeInBits() &&
841 [=](const LegalityQuery &Query) {
842 LLT T = Query.Types[LargeTypeIdx];
843 return std::make_pair(TypeIdx, T);
847 /// Add more elements to the vector to reach the next power of two.
848 /// No effect if the type is not a vector or the element count is a power of
850 LegalizeRuleSet &moreElementsToNextPow2(unsigned TypeIdx) {
851 using namespace LegalityPredicates;
852 return actionIf(LegalizeAction::MoreElements,
853 numElementsNotPow2(typeIdx(TypeIdx)),
854 LegalizeMutations::moreElementsToNextPow2(TypeIdx));
857 /// Limit the number of elements in EltTy vectors to at least MinElements.
858 LegalizeRuleSet &clampMinNumElements(unsigned TypeIdx, const LLT &EltTy,
859 unsigned MinElements) {
860 // Mark the type index as covered:
863 LegalizeAction::MoreElements,
864 [=](const LegalityQuery &Query) {
865 LLT VecTy = Query.Types[TypeIdx];
866 return VecTy.isVector() && VecTy.getElementType() == EltTy &&
867 VecTy.getNumElements() < MinElements;
869 [=](const LegalityQuery &Query) {
870 LLT VecTy = Query.Types[TypeIdx];
871 return std::make_pair(
872 TypeIdx, LLT::vector(MinElements, VecTy.getElementType()));
875 /// Limit the number of elements in EltTy vectors to at most MaxElements.
876 LegalizeRuleSet &clampMaxNumElements(unsigned TypeIdx, const LLT &EltTy,
877 unsigned MaxElements) {
878 // Mark the type index as covered:
881 LegalizeAction::FewerElements,
882 [=](const LegalityQuery &Query) {
883 LLT VecTy = Query.Types[TypeIdx];
884 return VecTy.isVector() && VecTy.getElementType() == EltTy &&
885 VecTy.getNumElements() > MaxElements;
887 [=](const LegalityQuery &Query) {
888 LLT VecTy = Query.Types[TypeIdx];
889 LLT NewTy = LLT::scalarOrVector(MaxElements, VecTy.getElementType());
890 return std::make_pair(TypeIdx, NewTy);
893 /// Limit the number of elements for the given vectors to at least MinTy's
894 /// number of elements and at most MaxTy's number of elements.
896 /// No effect if the type is not a vector or does not have the same element
897 /// type as the constraints.
898 /// The element type of MinTy and MaxTy must match.
899 LegalizeRuleSet &clampNumElements(unsigned TypeIdx, const LLT &MinTy,
901 assert(MinTy.getElementType() == MaxTy.getElementType() &&
902 "Expected element types to agree");
904 const LLT &EltTy = MinTy.getElementType();
905 return clampMinNumElements(TypeIdx, EltTy, MinTy.getNumElements())
906 .clampMaxNumElements(TypeIdx, EltTy, MaxTy.getNumElements());
909 /// Fallback on the previous implementation. This should only be used while
911 LegalizeRuleSet &fallback() {
912 add({always, LegalizeAction::UseLegacyRules});
916 /// Check if there is no type index which is obviously not handled by the
917 /// LegalizeRuleSet in any way at all.
918 /// \pre Type indices of the opcode form a dense [0, \p NumTypeIdxs) set.
919 bool verifyTypeIdxsCoverage(unsigned NumTypeIdxs) const;
920 /// Check if there is no imm index which is obviously not handled by the
921 /// LegalizeRuleSet in any way at all.
922 /// \pre Type indices of the opcode form a dense [0, \p NumTypeIdxs) set.
923 bool verifyImmIdxsCoverage(unsigned NumImmIdxs) const;
925 /// Apply the ruleset to the given LegalityQuery.
926 LegalizeActionStep apply(const LegalityQuery &Query) const;
929 class LegalizerInfo {
932 virtual ~LegalizerInfo() = default;
934 unsigned getOpcodeIdxForOpcode(unsigned Opcode) const;
935 unsigned getActionDefinitionsIdx(unsigned Opcode) const;
937 /// Compute any ancillary tables needed to quickly decide how an operation
938 /// should be handled. This must be called after all "set*Action"methods but
939 /// before any query is made or incorrect results may be returned.
940 void computeTables();
942 /// Perform simple self-diagnostic and assert if there is anything obviously
943 /// wrong with the actions set up.
944 void verify(const MCInstrInfo &MII) const;
946 static bool needsLegalizingToDifferentSize(const LegalizeAction Action) {
947 using namespace LegalizeActions;
960 using SizeAndAction = std::pair<uint16_t, LegalizeAction>;
961 using SizeAndActionsVec = std::vector<SizeAndAction>;
962 using SizeChangeStrategy =
963 std::function<SizeAndActionsVec(const SizeAndActionsVec &v)>;
965 /// More friendly way to set an action for common types that have an LLT
967 /// The LegalizeAction must be one for which NeedsLegalizingToDifferentSize
969 void setAction(const InstrAspect &Aspect, LegalizeAction Action) {
970 assert(!needsLegalizingToDifferentSize(Action));
971 TablesInitialized = false;
972 const unsigned OpcodeIdx = Aspect.Opcode - FirstOp;
973 if (SpecifiedActions[OpcodeIdx].size() <= Aspect.Idx)
974 SpecifiedActions[OpcodeIdx].resize(Aspect.Idx + 1);
975 SpecifiedActions[OpcodeIdx][Aspect.Idx][Aspect.Type] = Action;
978 /// The setAction calls record the non-size-changing legalization actions
979 /// to take on specificly-sized types. The SizeChangeStrategy defines what
980 /// to do when the size of the type needs to be changed to reach a legally
981 /// sized type (i.e., one that was defined through a setAction call).
983 /// setAction ({G_ADD, 0, LLT::scalar(32)}, Legal);
984 /// setLegalizeScalarToDifferentSizeStrategy(
985 /// G_ADD, 0, widenToLargerTypesAndNarrowToLargest);
986 /// will end up defining getAction({G_ADD, 0, T}) to return the following
987 /// actions for different scalar types T:
988 /// LLT::scalar(1)..LLT::scalar(31): {WidenScalar, 0, LLT::scalar(32)}
989 /// LLT::scalar(32): {Legal, 0, LLT::scalar(32)}
990 /// LLT::scalar(33)..: {NarrowScalar, 0, LLT::scalar(32)}
992 /// If no SizeChangeAction gets defined, through this function,
993 /// the default is unsupportedForDifferentSizes.
994 void setLegalizeScalarToDifferentSizeStrategy(const unsigned Opcode,
995 const unsigned TypeIdx,
996 SizeChangeStrategy S) {
997 const unsigned OpcodeIdx = Opcode - FirstOp;
998 if (ScalarSizeChangeStrategies[OpcodeIdx].size() <= TypeIdx)
999 ScalarSizeChangeStrategies[OpcodeIdx].resize(TypeIdx + 1);
1000 ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx] = S;
1003 /// See also setLegalizeScalarToDifferentSizeStrategy.
1004 /// This function allows to set the SizeChangeStrategy for vector elements.
1005 void setLegalizeVectorElementToDifferentSizeStrategy(const unsigned Opcode,
1006 const unsigned TypeIdx,
1007 SizeChangeStrategy S) {
1008 const unsigned OpcodeIdx = Opcode - FirstOp;
1009 if (VectorElementSizeChangeStrategies[OpcodeIdx].size() <= TypeIdx)
1010 VectorElementSizeChangeStrategies[OpcodeIdx].resize(TypeIdx + 1);
1011 VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx] = S;
1014 /// A SizeChangeStrategy for the common case where legalization for a
1015 /// particular operation consists of only supporting a specific set of type
1017 /// setAction ({G_DIV, 0, LLT::scalar(32)}, Legal);
1018 /// setAction ({G_DIV, 0, LLT::scalar(64)}, Legal);
1019 /// setLegalizeScalarToDifferentSizeStrategy(
1020 /// G_DIV, 0, unsupportedForDifferentSizes);
1021 /// will result in getAction({G_DIV, 0, T}) to return Legal for s32 and s64,
1022 /// and Unsupported for all other scalar types T.
1023 static SizeAndActionsVec
1024 unsupportedForDifferentSizes(const SizeAndActionsVec &v) {
1025 using namespace LegalizeActions;
1026 return increaseToLargerTypesAndDecreaseToLargest(v, Unsupported,
1030 /// A SizeChangeStrategy for the common case where legalization for a
1031 /// particular operation consists of widening the type to a large legal type,
1032 /// unless there is no such type and then instead it should be narrowed to the
1033 /// largest legal type.
1034 static SizeAndActionsVec
1035 widenToLargerTypesAndNarrowToLargest(const SizeAndActionsVec &v) {
1036 using namespace LegalizeActions;
1037 assert(v.size() > 0 &&
1038 "At least one size that can be legalized towards is needed"
1039 " for this SizeChangeStrategy");
1040 return increaseToLargerTypesAndDecreaseToLargest(v, WidenScalar,
1044 static SizeAndActionsVec
1045 widenToLargerTypesUnsupportedOtherwise(const SizeAndActionsVec &v) {
1046 using namespace LegalizeActions;
1047 return increaseToLargerTypesAndDecreaseToLargest(v, WidenScalar,
1051 static SizeAndActionsVec
1052 narrowToSmallerAndUnsupportedIfTooSmall(const SizeAndActionsVec &v) {
1053 using namespace LegalizeActions;
1054 return decreaseToSmallerTypesAndIncreaseToSmallest(v, NarrowScalar,
1058 static SizeAndActionsVec
1059 narrowToSmallerAndWidenToSmallest(const SizeAndActionsVec &v) {
1060 using namespace LegalizeActions;
1061 assert(v.size() > 0 &&
1062 "At least one size that can be legalized towards is needed"
1063 " for this SizeChangeStrategy");
1064 return decreaseToSmallerTypesAndIncreaseToSmallest(v, NarrowScalar,
1068 /// A SizeChangeStrategy for the common case where legalization for a
1069 /// particular vector operation consists of having more elements in the
1070 /// vector, to a type that is legal. Unless there is no such type and then
1071 /// instead it should be legalized towards the widest vector that's still
1073 /// setAction({G_ADD, LLT::vector(8, 8)}, Legal);
1074 /// setAction({G_ADD, LLT::vector(16, 8)}, Legal);
1075 /// setAction({G_ADD, LLT::vector(2, 32)}, Legal);
1076 /// setAction({G_ADD, LLT::vector(4, 32)}, Legal);
1077 /// setLegalizeVectorElementToDifferentSizeStrategy(
1078 /// G_ADD, 0, moreToWiderTypesAndLessToWidest);
1079 /// will result in the following getAction results:
1080 /// * getAction({G_ADD, LLT::vector(8,8)}) returns
1081 /// (Legal, vector(8,8)).
1082 /// * getAction({G_ADD, LLT::vector(9,8)}) returns
1083 /// (MoreElements, vector(16,8)).
1084 /// * getAction({G_ADD, LLT::vector(8,32)}) returns
1085 /// (FewerElements, vector(4,32)).
1086 static SizeAndActionsVec
1087 moreToWiderTypesAndLessToWidest(const SizeAndActionsVec &v) {
1088 using namespace LegalizeActions;
1089 return increaseToLargerTypesAndDecreaseToLargest(v, MoreElements,
1093 /// Helper function to implement many typical SizeChangeStrategy functions.
1094 static SizeAndActionsVec
1095 increaseToLargerTypesAndDecreaseToLargest(const SizeAndActionsVec &v,
1096 LegalizeAction IncreaseAction,
1097 LegalizeAction DecreaseAction);
1098 /// Helper function to implement many typical SizeChangeStrategy functions.
1099 static SizeAndActionsVec
1100 decreaseToSmallerTypesAndIncreaseToSmallest(const SizeAndActionsVec &v,
1101 LegalizeAction DecreaseAction,
1102 LegalizeAction IncreaseAction);
1104 /// Get the action definitions for the given opcode. Use this to run a
1105 /// LegalityQuery through the definitions.
1106 const LegalizeRuleSet &getActionDefinitions(unsigned Opcode) const;
1108 /// Get the action definition builder for the given opcode. Use this to define
1109 /// the action definitions.
1111 /// It is an error to request an opcode that has already been requested by the
1112 /// multiple-opcode variant.
1113 LegalizeRuleSet &getActionDefinitionsBuilder(unsigned Opcode);
1115 /// Get the action definition builder for the given set of opcodes. Use this
1116 /// to define the action definitions for multiple opcodes at once. The first
1117 /// opcode given will be considered the representative opcode and will hold
1118 /// the definitions whereas the other opcodes will be configured to refer to
1119 /// the representative opcode. This lowers memory requirements and very
1120 /// slightly improves performance.
1122 /// It would be very easy to introduce unexpected side-effects as a result of
1123 /// this aliasing if it were permitted to request different but intersecting
1124 /// sets of opcodes but that is difficult to keep track of. It is therefore an
1125 /// error to request the same opcode twice using this API, to request an
1126 /// opcode that already has definitions, or to use the single-opcode API on an
1127 /// opcode that has already been requested by this API.
1129 getActionDefinitionsBuilder(std::initializer_list<unsigned> Opcodes);
1130 void aliasActionDefinitions(unsigned OpcodeTo, unsigned OpcodeFrom);
1132 /// Determine what action should be taken to legalize the described
1133 /// instruction. Requires computeTables to have been called.
1135 /// \returns a description of the next legalization step to perform.
1136 LegalizeActionStep getAction(const LegalityQuery &Query) const;
1138 /// Determine what action should be taken to legalize the given generic
1141 /// \returns a description of the next legalization step to perform.
1142 LegalizeActionStep getAction(const MachineInstr &MI,
1143 const MachineRegisterInfo &MRI) const;
1145 bool isLegal(const LegalityQuery &Query) const {
1146 return getAction(Query).Action == LegalizeAction::Legal;
1148 bool isLegal(const MachineInstr &MI, const MachineRegisterInfo &MRI) const;
1149 bool isLegalOrCustom(const MachineInstr &MI,
1150 const MachineRegisterInfo &MRI) const;
1152 virtual bool legalizeCustom(MachineInstr &MI, MachineRegisterInfo &MRI,
1153 MachineIRBuilder &MIRBuilder,
1154 GISelChangeObserver &Observer) const;
1156 /// Return true if MI is either legal or has been legalized and false
1158 virtual bool legalizeIntrinsic(MachineInstr &MI, MachineRegisterInfo &MRI,
1159 MachineIRBuilder &MIRBuilder) const;
1161 /// Return the opcode (SEXT/ZEXT/ANYEXT) that should be performed while
1162 /// widening a constant of type SmallTy which targets can override.
1163 /// For eg, the DAG does (SmallTy.isByteSized() ? G_SEXT : G_ZEXT) which
1164 /// will be the default.
1165 virtual unsigned getExtOpcodeForWideningConstant(LLT SmallTy) const;
1168 /// Determine what action should be taken to legalize the given generic
1169 /// instruction opcode, type-index and type. Requires computeTables to have
1172 /// \returns a pair consisting of the kind of legalization that should be
1173 /// performed and the destination type.
1174 std::pair<LegalizeAction, LLT>
1175 getAspectAction(const InstrAspect &Aspect) const;
1177 /// The SizeAndActionsVec is a representation mapping between all natural
1178 /// numbers and an Action. The natural number represents the bit size of
1179 /// the InstrAspect. For example, for a target with native support for 32-bit
1180 /// and 64-bit additions, you'd express that as:
1181 /// setScalarAction(G_ADD, 0,
1182 /// {{1, WidenScalar}, // bit sizes [ 1, 31[
1183 /// {32, Legal}, // bit sizes [32, 33[
1184 /// {33, WidenScalar}, // bit sizes [33, 64[
1185 /// {64, Legal}, // bit sizes [64, 65[
1186 /// {65, NarrowScalar} // bit sizes [65, +inf[
1188 /// It may be that only 64-bit pointers are supported on your target:
1189 /// setPointerAction(G_PTR_ADD, 0, LLT:pointer(1),
1190 /// {{1, Unsupported}, // bit sizes [ 1, 63[
1191 /// {64, Legal}, // bit sizes [64, 65[
1192 /// {65, Unsupported}, // bit sizes [65, +inf[
1194 void setScalarAction(const unsigned Opcode, const unsigned TypeIndex,
1195 const SizeAndActionsVec &SizeAndActions) {
1196 const unsigned OpcodeIdx = Opcode - FirstOp;
1197 SmallVector<SizeAndActionsVec, 1> &Actions = ScalarActions[OpcodeIdx];
1198 setActions(TypeIndex, Actions, SizeAndActions);
1200 void setPointerAction(const unsigned Opcode, const unsigned TypeIndex,
1201 const unsigned AddressSpace,
1202 const SizeAndActionsVec &SizeAndActions) {
1203 const unsigned OpcodeIdx = Opcode - FirstOp;
1204 if (AddrSpace2PointerActions[OpcodeIdx].find(AddressSpace) ==
1205 AddrSpace2PointerActions[OpcodeIdx].end())
1206 AddrSpace2PointerActions[OpcodeIdx][AddressSpace] = {{}};
1207 SmallVector<SizeAndActionsVec, 1> &Actions =
1208 AddrSpace2PointerActions[OpcodeIdx].find(AddressSpace)->second;
1209 setActions(TypeIndex, Actions, SizeAndActions);
1212 /// If an operation on a given vector type (say <M x iN>) isn't explicitly
1213 /// specified, we proceed in 2 stages. First we legalize the underlying scalar
1214 /// (so that there's at least one legal vector with that scalar), then we
1215 /// adjust the number of elements in the vector so that it is legal. The
1216 /// desired action in the first step is controlled by this function.
1217 void setScalarInVectorAction(const unsigned Opcode, const unsigned TypeIndex,
1218 const SizeAndActionsVec &SizeAndActions) {
1219 unsigned OpcodeIdx = Opcode - FirstOp;
1220 SmallVector<SizeAndActionsVec, 1> &Actions =
1221 ScalarInVectorActions[OpcodeIdx];
1222 setActions(TypeIndex, Actions, SizeAndActions);
1225 /// See also setScalarInVectorAction.
1226 /// This function let's you specify the number of elements in a vector that
1227 /// are legal for a legal element size.
1228 void setVectorNumElementAction(const unsigned Opcode,
1229 const unsigned TypeIndex,
1230 const unsigned ElementSize,
1231 const SizeAndActionsVec &SizeAndActions) {
1232 const unsigned OpcodeIdx = Opcode - FirstOp;
1233 if (NumElements2Actions[OpcodeIdx].find(ElementSize) ==
1234 NumElements2Actions[OpcodeIdx].end())
1235 NumElements2Actions[OpcodeIdx][ElementSize] = {{}};
1236 SmallVector<SizeAndActionsVec, 1> &Actions =
1237 NumElements2Actions[OpcodeIdx].find(ElementSize)->second;
1238 setActions(TypeIndex, Actions, SizeAndActions);
1241 /// A partial SizeAndActionsVec potentially doesn't cover all bit sizes,
1242 /// i.e. it's OK if it doesn't start from size 1.
1243 static void checkPartialSizeAndActionsVector(const SizeAndActionsVec& v) {
1244 using namespace LegalizeActions;
1246 // The sizes should be in increasing order
1248 for(auto SizeAndAction: v) {
1249 assert(SizeAndAction.first > prev_size);
1250 prev_size = SizeAndAction.first;
1252 // - for every Widen action, there should be a larger bitsize that
1253 // can be legalized towards (e.g. Legal, Lower, Libcall or Custom
1255 // - for every Narrow action, there should be a smaller bitsize that
1256 // can be legalized towards.
1257 int SmallestNarrowIdx = -1;
1258 int LargestWidenIdx = -1;
1259 int SmallestLegalizableToSameSizeIdx = -1;
1260 int LargestLegalizableToSameSizeIdx = -1;
1261 for(size_t i=0; i<v.size(); ++i) {
1262 switch (v[i].second) {
1265 if (SmallestNarrowIdx == -1)
1266 SmallestNarrowIdx = i;
1270 LargestWidenIdx = i;
1275 if (SmallestLegalizableToSameSizeIdx == -1)
1276 SmallestLegalizableToSameSizeIdx = i;
1277 LargestLegalizableToSameSizeIdx = i;
1280 if (SmallestNarrowIdx != -1) {
1281 assert(SmallestLegalizableToSameSizeIdx != -1);
1282 assert(SmallestNarrowIdx > SmallestLegalizableToSameSizeIdx);
1284 if (LargestWidenIdx != -1)
1285 assert(LargestWidenIdx < LargestLegalizableToSameSizeIdx);
1289 /// A full SizeAndActionsVec must cover all bit sizes, i.e. must start with
1291 static void checkFullSizeAndActionsVector(const SizeAndActionsVec& v) {
1293 // Data structure invariant: The first bit size must be size 1.
1294 assert(v.size() >= 1);
1295 assert(v[0].first == 1);
1296 checkPartialSizeAndActionsVector(v);
1300 /// Sets actions for all bit sizes on a particular generic opcode, type
1301 /// index and scalar or pointer type.
1302 void setActions(unsigned TypeIndex,
1303 SmallVector<SizeAndActionsVec, 1> &Actions,
1304 const SizeAndActionsVec &SizeAndActions) {
1305 checkFullSizeAndActionsVector(SizeAndActions);
1306 if (Actions.size() <= TypeIndex)
1307 Actions.resize(TypeIndex + 1);
1308 Actions[TypeIndex] = SizeAndActions;
1311 static SizeAndAction findAction(const SizeAndActionsVec &Vec,
1312 const uint32_t Size);
1314 /// Returns the next action needed to get the scalar or pointer type closer
1316 /// E.g. findLegalAction({G_REM, 13}) should return
1317 /// (WidenScalar, 32). After that, findLegalAction({G_REM, 32}) will
1318 /// probably be called, which should return (Lower, 32).
1319 /// This is assuming the setScalarAction on G_REM was something like:
1320 /// setScalarAction(G_REM, 0,
1321 /// {{1, WidenScalar}, // bit sizes [ 1, 31[
1322 /// {32, Lower}, // bit sizes [32, 33[
1323 /// {33, NarrowScalar} // bit sizes [65, +inf[
1325 std::pair<LegalizeAction, LLT>
1326 findScalarLegalAction(const InstrAspect &Aspect) const;
1328 /// Returns the next action needed towards legalizing the vector type.
1329 std::pair<LegalizeAction, LLT>
1330 findVectorLegalAction(const InstrAspect &Aspect) const;
1332 static const int FirstOp = TargetOpcode::PRE_ISEL_GENERIC_OPCODE_START;
1333 static const int LastOp = TargetOpcode::PRE_ISEL_GENERIC_OPCODE_END;
1335 // Data structures used temporarily during construction of legality data:
1336 using TypeMap = DenseMap<LLT, LegalizeAction>;
1337 SmallVector<TypeMap, 1> SpecifiedActions[LastOp - FirstOp + 1];
1338 SmallVector<SizeChangeStrategy, 1>
1339 ScalarSizeChangeStrategies[LastOp - FirstOp + 1];
1340 SmallVector<SizeChangeStrategy, 1>
1341 VectorElementSizeChangeStrategies[LastOp - FirstOp + 1];
1342 bool TablesInitialized;
1344 // Data structures used by getAction:
1345 SmallVector<SizeAndActionsVec, 1> ScalarActions[LastOp - FirstOp + 1];
1346 SmallVector<SizeAndActionsVec, 1> ScalarInVectorActions[LastOp - FirstOp + 1];
1347 std::unordered_map<uint16_t, SmallVector<SizeAndActionsVec, 1>>
1348 AddrSpace2PointerActions[LastOp - FirstOp + 1];
1349 std::unordered_map<uint16_t, SmallVector<SizeAndActionsVec, 1>>
1350 NumElements2Actions[LastOp - FirstOp + 1];
1352 LegalizeRuleSet RulesForOpcode[LastOp - FirstOp + 1];
1356 /// Checks that MIR is fully legal, returns an illegal instruction if it's not,
1357 /// nullptr otherwise
1358 const MachineInstr *machineFunctionIsIllegal(const MachineFunction &MF);
1361 } // end namespace llvm.
1363 #endif // LLVM_CODEGEN_GLOBALISEL_LEGALIZERINFO_H