LLVM 22.0.0git
SDNodeInfo.cpp
Go to the documentation of this file.
1//==------------------------------------------------------------------------==//
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
11
12using namespace llvm;
13
14static void reportNodeError(const SelectionDAG &DAG, const SDNode *N,
15 const Twine &Msg) {
16 std::string S;
18 SS << "invalid node: " << Msg << '\n';
19 N->printrWithDepth(SS, &DAG, 2);
21}
22
23static void checkResultType(const SelectionDAG &DAG, const SDNode *N,
24 unsigned ResIdx, EVT ExpectedVT) {
25 EVT ActualVT = N->getValueType(ResIdx);
26 if (ActualVT != ExpectedVT)
28 DAG, N,
29 "result #" + Twine(ResIdx) + " has invalid type; expected " +
30 ExpectedVT.getEVTString() + ", got " + ActualVT.getEVTString());
31}
32
33static void checkOperandType(const SelectionDAG &DAG, const SDNode *N,
34 unsigned OpIdx, EVT ExpectedVT) {
35 EVT ActualVT = N->getOperand(OpIdx).getValueType();
36 if (ActualVT != ExpectedVT)
38 DAG, N,
39 "operand #" + Twine(OpIdx) + " has invalid type; expected " +
40 ExpectedVT.getEVTString() + ", got " + ActualVT.getEVTString());
41}
42
43void SDNodeInfo::verifyNode(const SelectionDAG &DAG, const SDNode *N) const {
44 const SDNodeDesc &Desc = getDesc(N->getOpcode());
45 bool HasChain = Desc.hasProperty(SDNPHasChain);
46 bool HasOutGlue = Desc.hasProperty(SDNPOutGlue);
47 bool HasInGlue = Desc.hasProperty(SDNPInGlue);
48 bool HasOptInGlue = Desc.hasProperty(SDNPOptInGlue);
49 bool IsVariadic = Desc.hasProperty(SDNPVariadic);
50
51 unsigned ActualNumResults = N->getNumValues();
52 unsigned ExpectedNumResults = Desc.NumResults + HasChain + HasOutGlue;
53
54 if (ActualNumResults != ExpectedNumResults)
55 reportNodeError(DAG, N,
56 "invalid number of results; expected " +
57 Twine(ExpectedNumResults) + ", got " +
58 Twine(ActualNumResults));
59
60 // Chain result comes after all normal results.
61 if (HasChain) {
62 unsigned ChainResIdx = Desc.NumResults;
63 checkResultType(DAG, N, ChainResIdx, MVT::Other);
64 }
65
66 // Glue result comes last.
67 if (HasOutGlue) {
68 unsigned GlueResIdx = Desc.NumResults + HasChain;
69 checkResultType(DAG, N, GlueResIdx, MVT::Glue);
70 }
71
72 // In the most general case, the operands of a node go in the following order:
73 // chain, fix#0, ..., fix#M-1, var#0, ... var#N-1, glue
74 // If the number of operands is < 0, M can be any;
75 // If the node has SDNPVariadic property, N can be any.
76 bool HasOptionalOperands = Desc.NumOperands < 0 || IsVariadic;
77
78 unsigned ActualNumOperands = N->getNumOperands();
79 unsigned ExpectedMinNumOperands =
80 (Desc.NumOperands >= 0 ? Desc.NumOperands : 0) + HasChain + HasInGlue;
81
82 // Check the lower bound.
83 if (ActualNumOperands < ExpectedMinNumOperands) {
84 StringRef How = HasOptionalOperands ? "at least " : "";
85 reportNodeError(DAG, N,
86 "invalid number of operands; expected " + How +
87 Twine(ExpectedMinNumOperands) + ", got " +
88 Twine(ActualNumOperands));
89 }
90
91 // Check the upper bound. We can only do this if the number of fixed operands
92 // is known and there are no variadic operands.
93 if (Desc.NumOperands >= 0 && !IsVariadic) {
94 // Account for optional input glue.
95 unsigned ExpectedMaxNumOperands = ExpectedMinNumOperands + HasOptInGlue;
96 if (ActualNumOperands > ExpectedMaxNumOperands) {
97 StringRef How = HasOptInGlue ? "at most " : "";
98 reportNodeError(DAG, N,
99 "invalid number of operands; expected " + How +
100 Twine(ExpectedMaxNumOperands) + ", got " +
101 Twine(ActualNumOperands));
102 }
103 }
104
105 // Chain operand comes first.
106 if (HasChain)
107 checkOperandType(DAG, N, 0, MVT::Other);
108
109 // Glue operand comes last.
110 if (HasInGlue)
111 checkOperandType(DAG, N, ActualNumOperands - 1, MVT::Glue);
112 if (HasOptInGlue && ActualNumOperands >= 1 &&
113 N->getOperand(ActualNumOperands - 1).getValueType() == MVT::Glue)
114 HasInGlue = true;
115
116 // Check variadic operands. These should be Register or RegisterMask.
117 if (IsVariadic && Desc.NumOperands >= 0) {
118 unsigned VarOpStart = HasChain + Desc.NumOperands;
119 unsigned VarOpEnd = ActualNumOperands - HasInGlue;
120 for (unsigned OpIdx = VarOpStart; OpIdx != VarOpEnd; ++OpIdx) {
121 unsigned OpOpcode = N->getOperand(OpIdx).getOpcode();
122 if (OpOpcode != ISD::Register && OpOpcode != ISD::RegisterMask)
123 reportNodeError(DAG, N,
124 "variadic operand #" + Twine(OpIdx) +
125 " must be Register or RegisterMask");
126 }
127 }
128}
MachineInstr unsigned OpIdx
static void checkOperandType(const SelectionDAG &DAG, const SDNode *N, unsigned OpIdx, EVT ExpectedVT)
Definition: SDNodeInfo.cpp:33
static void reportNodeError(const SelectionDAG &DAG, const SDNode *N, const Twine &Msg)
Definition: SDNodeInfo.cpp:14
static void checkResultType(const SelectionDAG &DAG, const SDNode *N, unsigned ResIdx, EVT ExpectedVT)
Definition: SDNodeInfo.cpp:23
const SDNodeDesc & getDesc(unsigned Opcode) const
Returns the description of a node with the given opcode.
Definition: SDNodeInfo.h:95
void verifyNode(const SelectionDAG &DAG, const SDNode *N) const
Definition: SDNodeInfo.cpp:43
Represents one node in the SelectionDAG.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:229
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
@ RegisterMask
Definition: ISDOpcodes.h:85
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
@ SDNPOptInGlue
Definition: SDNodeInfo.h:26
@ SDNPVariadic
Definition: SDNodeInfo.h:28
@ SDNPInGlue
Definition: SDNodeInfo.h:25
@ SDNPOutGlue
Definition: SDNodeInfo.h:24
@ SDNPHasChain
Definition: SDNodeInfo.h:23
#define N
Description of the encoding of one expression Op.
Extended Value Type.
Definition: ValueTypes.h:35
LLVM_ABI std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
Definition: ValueTypes.cpp:162