38 #define DEBUG_TYPE "legalizer-info" 41 "disable-gisel-legality-check",
42 cl::desc(
"Don't verify that MIR is fully legal between GlobalISel passes"),
46 OS << Opcode <<
", Tys={";
47 for (
const auto &
Type : Types) {
52 OS << Opcode <<
", MMOs={";
53 for (
const auto &MMODescr : MMODescrs) {
54 OS << MMODescr.Size <<
", ";
65 LLVM_DEBUG(
dbgs() <<
".. fallback to legacy rules (no rules defined)\n");
68 for (
const auto &Rule : Rules) {
69 if (Rule.match(Query)) {
71 std::pair<unsigned, LLT>
Mutation = Rule.determineMutation(Query);
72 LLVM_DEBUG(
dbgs() <<
".. .. " << (
unsigned)Rule.getAction() <<
", " 73 << Mutation.first <<
", " << Mutation.second <<
"\n");
74 assert((Query.
Types[Mutation.first] != Mutation.second ||
75 Rule.getAction() ==
Lower ||
78 "Simple loop detected");
79 return {Rule.getAction(), Mutation.first, Mutation.second};
91 dbgs() <<
".. type index coverage check SKIPPED: no rules defined\n");
94 const int64_t FirstUncovered = TypeIdxsCovered.find_first_unset();
95 if (FirstUncovered < 0) {
97 " user-defined predicate detected\n");
100 const bool AllCovered = (FirstUncovered >= NumTypeIdxs);
101 LLVM_DEBUG(
dbgs() <<
".. the first uncovered type index: " << FirstUncovered
102 <<
", " << (AllCovered ?
"OK" :
"FAIL") <<
"\n");
113 setScalarAction(TargetOpcode::G_ANYEXT, 1, {{1,
Legal}});
114 setScalarAction(TargetOpcode::G_ZEXT, 1, {{1,
Legal}});
115 setScalarAction(TargetOpcode::G_SEXT, 1, {{1,
Legal}});
116 setScalarAction(TargetOpcode::G_TRUNC, 0, {{1,
Legal}});
117 setScalarAction(TargetOpcode::G_TRUNC, 1, {{1,
Legal}});
119 setScalarAction(TargetOpcode::G_INTRINSIC, 0, {{1,
Legal}});
120 setScalarAction(TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS, 0, {{1,
Legal}});
141 setScalarAction(TargetOpcode::G_FNEG, 0, {{1,
Lower}});
145 assert(TablesInitialized ==
false);
147 for (
unsigned OpcodeIdx = 0; OpcodeIdx <= LastOp - FirstOp; ++OpcodeIdx) {
148 const unsigned Opcode = FirstOp + OpcodeIdx;
149 for (
unsigned TypeIdx = 0; TypeIdx != SpecifiedActions[OpcodeIdx].size();
156 std::map<uint16_t, SizeAndActionsVec> AddressSpace2SpecifiedActions;
158 std::map<uint16_t, SizeAndActionsVec> ElemSize2SpecifiedActions;
159 for (
auto LLT2Action : SpecifiedActions[OpcodeIdx][TypeIdx]) {
160 const LLT Type = LLT2Action.first;
163 auto SizeAction = std::make_pair(Type.
getSizeInBits(), Action);
169 .push_back(SizeAction);
171 ScalarSpecifiedActions.push_back(SizeAction);
179 if (TypeIdx < ScalarSizeChangeStrategies[OpcodeIdx].
size() &&
180 ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx] !=
nullptr)
181 S = ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx];
183 ScalarSpecifiedActions.end());
184 checkPartialSizeAndActionsVector(ScalarSpecifiedActions);
185 setScalarAction(Opcode, TypeIdx, S(ScalarSpecifiedActions));
189 for (
auto PointerSpecifiedActions : AddressSpace2SpecifiedActions) {
190 llvm::sort(PointerSpecifiedActions.second.begin(),
191 PointerSpecifiedActions.second.end());
192 checkPartialSizeAndActionsVector(PointerSpecifiedActions.second);
196 Opcode, TypeIdx, PointerSpecifiedActions.first,
202 for (
auto VectorSpecifiedActions : ElemSize2SpecifiedActions) {
203 llvm::sort(VectorSpecifiedActions.second.begin(),
204 VectorSpecifiedActions.second.end());
205 const uint16_t ElementSize = VectorSpecifiedActions.first;
206 ElementSizesSeen.push_back({ElementSize,
Legal});
207 checkPartialSizeAndActionsVector(VectorSpecifiedActions.second);
213 for (
SizeAndAction BitsizeAndAction : VectorSpecifiedActions.second) {
214 assert(BitsizeAndAction.first % ElementSize == 0);
215 const uint16_t NumElements = BitsizeAndAction.first / ElementSize;
216 NumElementsActions.push_back({NumElements, BitsizeAndAction.second});
218 setVectorNumElementAction(
219 Opcode, TypeIdx, ElementSize,
222 llvm::sort(ElementSizesSeen.begin(), ElementSizesSeen.end());
225 if (TypeIdx < VectorElementSizeChangeStrategies[OpcodeIdx].
size() &&
226 VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx] !=
nullptr)
227 VectorElementSizeChangeStrategy =
228 VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx];
229 setScalarInVectorAction(
230 Opcode, TypeIdx, VectorElementSizeChangeStrategy(ElementSizesSeen));
234 TablesInitialized =
true;
241 std::pair<LegalizeAction, LLT>
242 LegalizerInfo::getAspectAction(
const InstrAspect &Aspect)
const {
243 assert(TablesInitialized &&
"backend forgot to call computeTables");
247 return findScalarLegalAction(Aspect);
249 return findVectorLegalAction(Aspect);
260 if (MI.
getOpcode() == TargetOpcode::G_UNMERGE_VALUES && TypeIdx == 1)
266 assert(Opcode >= FirstOp && Opcode <= LastOp &&
"Unsupported opcode");
267 return Opcode - FirstOp;
272 if (
unsigned Alias = RulesForOpcode[OpcodeIdx].getAlias()) {
273 LLVM_DEBUG(
dbgs() <<
".. opcode " << Opcode <<
" is aliased to " << Alias
277 << RulesForOpcode[OpcodeIdx].getAlias() <<
"\n");
278 assert(RulesForOpcode[OpcodeIdx].getAlias() == 0 &&
"Cannot chain aliases");
287 return RulesForOpcode[OpcodeIdx];
292 auto &
Result = RulesForOpcode[OpcodeIdx];
293 assert(!
Result.isAliasedByAnother() &&
"Modifying this opcode will modify aliases");
298 std::initializer_list<unsigned> Opcodes) {
299 unsigned Representative = *Opcodes.begin();
301 assert(Opcodes.begin() != Opcodes.end() &&
302 Opcodes.begin() + 1 != Opcodes.end() &&
303 "Initializer list must have at least two opcodes");
305 for (
auto I = Opcodes.begin() + 1,
E = Opcodes.end();
I !=
E; ++
I)
309 Return.setIsAliasedByAnother();
314 unsigned OpcodeFrom) {
315 assert(OpcodeTo != OpcodeFrom &&
"Cannot alias to self");
316 assert(OpcodeTo >= FirstOp && OpcodeTo <= LastOp &&
"Unsupported opcode");
318 RulesForOpcode[OpcodeFromIdx].
aliasTo(OpcodeTo);
328 for (
unsigned i = 0; i < Query.
Types.size(); ++i) {
329 auto Action = getAspectAction({Query.
Opcode, i, Query.
Types[i]});
330 if (Action.first !=
Legal) {
332 <<
" Action=" << (
unsigned)Action.first <<
", " 333 << Action.second <<
"\n");
334 return {Action.first, i, Action.second};
350 if (!OpInfo[i].isGenericType())
356 if (SeenTypes[TypeIdx])
359 SeenTypes.
set(TypeIdx);
368 {MMO->getSize() * 8, MMO->getOrdering()});
388 unsigned LargestSizeSoFar = 0;
389 if (v.size() >= 1 && v[0].first != 1)
390 result.push_back({1, IncreaseAction});
391 for (
size_t i = 0; i < v.size(); ++i) {
392 result.push_back(v[i]);
393 LargestSizeSoFar = v[i].first;
394 if (i + 1 < v.size() && v[i + 1].first != v[i].first + 1) {
395 result.push_back({LargestSizeSoFar + 1, IncreaseAction});
396 LargestSizeSoFar = v[i].first + 1;
399 result.push_back({LargestSizeSoFar + 1, DecreaseAction});
408 if (v.size() == 0 || v[0].first != 1)
409 result.push_back({1, IncreaseAction});
410 for (
size_t i = 0; i < v.size(); ++i) {
411 result.push_back(v[i]);
412 if (i + 1 == v.size() || v[i + 1].first != v[i].first + 1) {
413 result.push_back({v[i].first + 1, DecreaseAction});
425 auto VecIt = std::upper_bound(
426 Vec.begin(), Vec.end(),
Size,
428 return Size < lhs.first;
430 assert(VecIt != Vec.begin() &&
"Does Vec not start with size 1?");
432 int VecIdx = VecIt - Vec.begin();
440 return {
Size, Action};
455 for (
int i = VecIdx - 1; i >= 0; --i)
458 return {Vec[i].first, Action};
464 for (std::size_t i = VecIdx + 1; i < Vec.size(); ++i)
467 return {Vec[i].first, Action};
479 std::pair<LegalizeAction, LLT>
480 LegalizerInfo::findScalarLegalAction(
const InstrAspect &Aspect)
const {
487 AddrSpace2PointerActions[OpcodeIdx].
end()) {
492 ? AddrSpace2PointerActions[OpcodeIdx]
495 : ScalarActions[OpcodeIdx];
496 if (Aspect.
Idx >= Actions.
size())
508 std::pair<LegalizeAction, LLT>
509 LegalizerInfo::findVectorLegalAction(
const InstrAspect &Aspect)
const {
516 const unsigned TypeIdx = Aspect.
Idx;
517 if (TypeIdx >= ScalarInVectorActions[OpcodeIdx].
size())
520 ScalarInVectorActions[OpcodeIdx][TypeIdx];
522 LLT IntermediateType;
523 auto ElementSizeAndAction =
527 if (ElementSizeAndAction.second !=
Legal)
528 return {ElementSizeAndAction.second, IntermediateType};
530 auto i = NumElements2Actions[OpcodeIdx].find(
531 IntermediateType.getScalarSizeInBits());
532 if (i == NumElements2Actions[OpcodeIdx].
end()) {
533 return {
NotFound, IntermediateType};
536 auto NumElementsAndAction =
537 findAction(NumElementsVec, IntermediateType.getNumElements());
538 return {NumElementsAndAction.second,
540 IntermediateType.getScalarSizeInBits())};
546 std::vector<unsigned> FailedOpcodes;
547 for (
unsigned Opcode = FirstOp; Opcode <= LastOp; ++Opcode) {
549 const unsigned NumTypeIdxs = std::accumulate(
553 ?
std::max(OpInfo.getGenericTypeIndex() + 1U, Acc)
557 <<
"): " << NumTypeIdxs <<
" type ind" 558 << (NumTypeIdxs == 1 ?
"ex" :
"ices") <<
"\n");
561 FailedOpcodes.push_back(Opcode);
563 if (!FailedOpcodes.empty()) {
564 errs() <<
"The following opcodes have ill-defined legalization rules:";
565 for (
unsigned Opcode : FailedOpcodes)
570 ", try -debug-only=legalizer-info for details");
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
static SizeAndActionsVec increaseToLargerTypesAndDecreaseToLargest(const SizeAndActionsVec &v, LegalizeAction IncreaseAction, LegalizeAction DecreaseAction)
Helper function to implement many typical SizeChangeStrategy functions.
unsigned getScalarSizeInBits() const
The operation should be implemented in terms of a wider scalar base-type.
raw_ostream & print(raw_ostream &OS) const
std::function< SizeAndActionsVec(const SizeAndActionsVec &v)> SizeChangeStrategy
The LegalityQuery object bundles together all the information that's needed to decide whether a given...
Describe properties that are true of each instruction in the target description file.
unsigned getReg() const
getReg - Returns the register number.
static LLT getTypeFromTypeIdx(const MachineInstr &MI, const MachineRegisterInfo &MRI, unsigned OpIdx, unsigned TypeIdx)
Helper function to get LLT for the given type index.
std::vector< SizeAndAction > SizeAndActionsVec
The (vector) operation should be implemented by splitting it into sub-vectors where the operation is ...
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit...
static SizeAndActionsVec narrowToSmallerAndUnsupportedIfTooSmall(const SizeAndActionsVec &v)
LLT getType(unsigned Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
const_opInfo_iterator opInfo_begin() const
iterator_range< mmo_iterator > memoperands()
const LegalizeRuleSet & getActionDefinitions(unsigned Opcode) const
Get the action definitions for the given opcode.
static SizeAndActionsVec unsupportedForDifferentSizes(const SizeAndActionsVec &v)
A SizeChangeStrategy for the common case where legalization for a particular operation consists of on...
The operation should be synthesized from multiple instructions acting on a narrower scalar base-type...
LegalizeActionStep getAction(const LegalityQuery &Query) const
Determine what action should be taken to legalize the described instruction.
static SizeAndActionsVec widenToLargerTypesUnsupportedOtherwise(const SizeAndActionsVec &v)
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
This operation is completely unsupported on the target.
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
void verify(const MCInstrInfo &MII) const
Perform simple self-diagnostic and assert if there is anything obviously wrong with the actions set u...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
The (vector) operation should be implemented by widening the input vector and ignoring the lanes adde...
void computeTables()
Compute any ancillary tables needed to quickly decide how an operation should be handled.
void aliasActionDefinitions(unsigned OpcodeTo, unsigned OpcodeFrom)
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
The operation itself must be expressed in terms of simpler actions on this target.
bool isGenericType() const
virtual const LegalizerInfo * getLegalizerInfo() const
bool isLegal(const MachineInstr &MI, const MachineRegisterInfo &MRI) const
static SizeAndActionsVec decreaseToSmallerTypesAndIncreaseToSmallest(const SizeAndActionsVec &v, LegalizeAction DecreaseAction, LegalizeAction IncreaseAction)
Helper function to implement many typical SizeChangeStrategy functions.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
The instances of the Type class are immutable: once they are created, they are never changed...
Sentinel value for when no action was found in the specified table.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Interface to description of machine instruction set.
bool verifyTypeIdxsCoverage(unsigned NumTypeIdxs) const
Check if there is no type index which is obviously not handled by the LegalizeRuleSet in any way at a...
unsigned getAddressSpace() const
Legalization is decided based on an instruction's opcode, which type slot we're considering, and what the existing type is.
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
cl::opt< bool > DisableGISelLegalityCheck
LegalizeRuleSet & getActionDefinitionsBuilder(unsigned Opcode)
Get the action definition builder for the given opcode.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void sort(IteratorTy Start, IteratorTy End)
const MachineInstr * machineFunctionIsIllegal(const MachineFunction &MF)
Checks that MIR is fully legal, returns an illegal instruction if it's not, nullptr otherwise...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
The target wants to do something special with this combination of operand and type.
unsigned getOpcodeIdxForOpcode(unsigned Opcode) const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
virtual bool legalizeCustom(MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &MIRBuilder) const
Representation of each machine instruction.
LegalizeAction Action
The action to take or the final answer.
static bool needsLegalizingToDifferentSize(const LegalizeAction Action)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Fall back onto the old rules.
static SizeAndActionsVec widenToLargerTypesAndNarrowToLargest(const SizeAndActionsVec &v)
A SizeChangeStrategy for the common case where legalization for a particular operation consists of wi...
static SizeAndActionsVec moreToWiderTypesAndLessToWidest(const SizeAndActionsVec &v)
A SizeChangeStrategy for the common case where legalization for a particular vector operation consist...
LegalizeActionStep apply(const LegalityQuery &Query) const
Apply the ruleset to the given LegalityQuery.
const_opInfo_iterator opInfo_end() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
void aliasTo(unsigned Opcode)
void setLegalizeScalarToDifferentSizeStrategy(const unsigned Opcode, const unsigned TypeIdx, SizeChangeStrategy S)
The setAction calls record the non-size-changing legalization actions to take on specificly-sized typ...
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space (defaulting to 0).
uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
const MCOperandInfo * OpInfo
unsigned getActionDefinitionsIdx(unsigned Opcode) const
This class implements an extremely fast bulk output stream that can only output to a stream...
unsigned getGenericTypeIndex() const
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
static LLT vector(uint16_t NumElements, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
This holds information about one operand of a machine instruction, indicating the register class for ...
const MachineOperand & getOperand(unsigned i) const
The operation is expected to be selectable directly by the target, and no transformation is necessary...
std::pair< uint16_t, LegalizeAction > SizeAndAction