LLVM 20.0.0git
Legality.h
Go to the documentation of this file.
1//===- Legality.h -----------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Legality checks for the Sandbox Vectorizer.
10//
11
12#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_LEGALITY_H
13#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_LEGALITY_H
14
15#include "llvm/ADT/ArrayRef.h"
17#include "llvm/IR/DataLayout.h"
21
22namespace llvm::sandboxir {
23
24class LegalityAnalysis;
25class Value;
26
27enum class LegalityResultID {
28 Pack, ///> Collect scalar values.
29 Widen, ///> Vectorize by combining scalars to a vector.
30};
31
32/// The reason for vectorizing or not vectorizing.
33enum class ResultReason {
43};
44
45#ifndef NDEBUG
46struct ToStr {
48 switch (ID) {
50 return "Pack";
52 return "Widen";
53 }
54 llvm_unreachable("Unknown LegalityResultID enum");
55 }
56
57 static const char *getVecReason(ResultReason Reason) {
58 switch (Reason) {
60 return "NotInstructions";
62 return "DiffOpcodes";
64 return "DiffTypes";
66 return "DiffMathFlags";
68 return "DiffWrapFlags";
70 return "NotConsecutive";
72 return "CantSchedule";
74 return "Unimplemented";
76 return "Infeasible";
77 }
78 llvm_unreachable("Unknown ResultReason enum");
79 }
80};
81#endif // NDEBUG
82
83/// The legality outcome is represented by a class rather than an enum class
84/// because in some cases the legality checks are expensive and look for a
85/// particular instruction that can be passed along to the vectorizer to avoid
86/// repeating the same expensive computation.
88protected:
90 /// Only Legality can create LegalityResults.
92 friend class LegalityAnalysis;
93
94 /// We shouldn't need copies.
95 LegalityResult(const LegalityResult &) = delete;
97
98public:
99 virtual ~LegalityResult() {}
101#ifndef NDEBUG
102 virtual void print(raw_ostream &OS) const {
104 }
105 LLVM_DUMP_METHOD void dump() const;
107 LR.print(OS);
108 return OS;
109 }
110#endif // NDEBUG
111};
112
113/// Base class for results with reason.
115 [[maybe_unused]] ResultReason Reason;
117 : LegalityResult(ID), Reason(Reason) {}
118 friend class Pack; // For constructor.
119
120public:
121 ResultReason getReason() const { return Reason; }
122#ifndef NDEBUG
123 void print(raw_ostream &OS) const override {
125 OS << " Reason: " << ToStr::getVecReason(Reason);
126 }
127#endif
128};
129
130class Widen final : public LegalityResult {
131 friend class LegalityAnalysis;
133
134public:
135 static bool classof(const LegalityResult *From) {
136 return From->getSubclassID() == LegalityResultID::Widen;
137 }
138};
139
140class Pack final : public LegalityResultWithReason {
141 Pack(ResultReason Reason)
143 friend class LegalityAnalysis; // For constructor.
144
145public:
146 static bool classof(const LegalityResult *From) {
147 return From->getSubclassID() == LegalityResultID::Pack;
148 }
149};
150
151/// Performs the legality analysis and returns a LegalityResult object.
153 Scheduler Sched;
154 /// Owns the legality result objects created by createLegalityResult().
156 /// Checks opcodes, types and other IR-specifics and returns a ResultReason
157 /// object if not vectorizable, or nullptr otherwise.
158 std::optional<ResultReason>
159 notVectorizableBasedOnOpcodesAndTypes(ArrayRef<Value *> Bndl);
160
161 ScalarEvolution &SE;
162 const DataLayout &DL;
163
164public:
166 Context &Ctx)
167 : Sched(AA, Ctx), SE(SE), DL(DL) {}
168 /// A LegalityResult factory.
169 template <typename ResultT, typename... ArgsT>
170 ResultT &createLegalityResult(ArgsT... Args) {
171 ResultPool.push_back(std::unique_ptr<ResultT>(new ResultT(Args...)));
172 return cast<ResultT>(*ResultPool.back());
173 }
174 /// Checks if it's legal to vectorize the instructions in \p Bndl.
175 /// \Returns a LegalityResult object owned by LegalityAnalysis.
176 /// \p SkipScheduling skips the scheduler check and is only meant for testing.
177 // TODO: Try to remove the SkipScheduling argument by refactoring the tests.
179 bool SkipScheduling = false);
180};
181
182} // namespace llvm::sandboxir
183
184#endif // LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_LEGALITY_H
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
The main scalar evolution driver.
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Performs the legality analysis and returns a LegalityResult object.
Definition: Legality.h:152
LegalityAnalysis(AAResults &AA, ScalarEvolution &SE, const DataLayout &DL, Context &Ctx)
Definition: Legality.h:165
ResultT & createLegalityResult(ArgsT... Args)
A LegalityResult factory.
Definition: Legality.h:170
const LegalityResult & canVectorize(ArrayRef< Value * > Bndl, bool SkipScheduling=false)
Checks if it's legal to vectorize the instructions in Bndl.
Definition: Legality.cpp:187
Base class for results with reason.
Definition: Legality.h:114
void print(raw_ostream &OS) const override
Definition: Legality.h:123
The legality outcome is represented by a class rather than an enum class because in some cases the le...
Definition: Legality.h:87
LegalityResult & operator=(const LegalityResult &)=delete
friend raw_ostream & operator<<(raw_ostream &OS, const LegalityResult &LR)
Definition: Legality.h:106
LLVM_DUMP_METHOD void dump() const
Definition: Legality.cpp:22
LegalityResultID getSubclassID() const
Definition: Legality.h:100
virtual void print(raw_ostream &OS) const
Definition: Legality.h:102
LegalityResult(LegalityResultID ID)
Only Legality can create LegalityResults.
Definition: Legality.h:91
LegalityResult(const LegalityResult &)=delete
We shouldn't need copies.
static bool classof(const LegalityResult *From)
Definition: Legality.h:146
The list scheduler.
Definition: Scheduler.h:107
static bool classof(const LegalityResult *From)
Definition: Legality.h:135
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Widen
ā€¨Collect scalar values.
ResultReason
The reason for vectorizing or not vectorizing.
Definition: Legality.h:33
static const char * getVecReason(ResultReason Reason)
Definition: Legality.h:57
static const char * getLegalityResultID(LegalityResultID ID)
Definition: Legality.h:47