26 #define DEBUG_TYPE "legalize-types" 32 void DAGTypeLegalizer::PerformExpensiveChecks() {
79 if (Node.getNodeId() ==
NewNode)
82 for (
unsigned i = 0,
e = Node.getNumValues(); i !=
e; ++i) {
86 auto ResId = (ValueToIdMap.count(Res)) ? ValueToIdMap[Res] : 0;
89 if (ResId && (ReplacedValues.find(ResId) != ReplacedValues.end())) {
94 if (UI.getUse().getResNo() == i)
96 "Remapped value has non-trivial use!");
100 auto NewValId = ReplacedValues[ResId];
101 auto I = ReplacedValues.find(NewValId);
102 while (
I != ReplacedValues.end()) {
103 NewValId =
I->second;
104 I = ReplacedValues.find(NewValId);
106 SDValue NewVal = getSDValue(NewValId);
109 "ReplacedValues maps to a new node!");
111 if (ResId && PromotedIntegers.find(ResId) != PromotedIntegers.end())
113 if (ResId && SoftenedFloats.find(ResId) != SoftenedFloats.end())
115 if (ResId && ScalarizedVectors.find(ResId) != ScalarizedVectors.end())
117 if (ResId && ExpandedIntegers.find(ResId) != ExpandedIntegers.end())
119 if (ResId && ExpandedFloats.find(ResId) != ExpandedFloats.end())
121 if (ResId && SplitVectors.find(ResId) != SplitVectors.end())
123 if (ResId && WidenedVectors.find(ResId) != WidenedVectors.end())
125 if (ResId && PromotedFloats.find(ResId) != PromotedFloats.end())
132 if ((Node.getNodeId() ==
NewNode && Mapped > 1) ||
133 (Node.getNodeId() !=
NewNode && Mapped != 0)) {
134 dbgs() <<
"Unprocessed value in a map!";
137 }
else if (isTypeLegal(Res.getValueType()) || IgnoreNodeResults(&Node)) {
139 dbgs() <<
"Value with legal type was transformed!";
144 dbgs() <<
"Processed value not in any map!";
146 }
else if (Mapped & (Mapped - 1)) {
147 dbgs() <<
"Value in multiple maps!";
154 dbgs() <<
" ReplacedValues";
156 dbgs() <<
" PromotedIntegers";
158 dbgs() <<
" SoftenedFloats";
160 dbgs() <<
" ScalarizedVectors";
162 dbgs() <<
" ExpandedIntegers";
164 dbgs() <<
" ExpandedFloats";
166 dbgs() <<
" SplitVectors";
168 dbgs() <<
" WidenedVectors";
170 dbgs() <<
" PromotedFloats";
178 for (
unsigned i = 0,
e = NewNodes.
size(); i !=
e; ++i) {
182 assert(UI->getNodeId() ==
NewNode &&
"NewNode used by non-NewNode!");
190 bool Changed =
false;
206 if (Node.getNumOperands() == 0) {
208 Worklist.push_back(&Node);
215 while (!Worklist.empty()) {
216 #ifndef EXPENSIVE_CHECKS 219 PerformExpensiveChecks();
221 SDNode *N = Worklist.back();
224 "Node should be ready if on worklist!");
227 if (IgnoreNodeResults(N)) {
234 for (
unsigned i = 0, NumResults = N->
getNumValues(); i < NumResults; ++i) {
238 switch (getTypeAction(ResultVT)) {
248 PromoteIntegerResult(N, i);
252 ExpandIntegerResult(N, i);
256 SoftenFloatResult(N, i);
260 ExpandFloatResult(N, i);
264 ScalarizeVectorResult(N, i);
268 SplitVectorResult(N, i);
272 WidenVectorResult(N, i);
276 PromoteFloatResult(N, i);
287 bool NeedsReanalyzing =
false;
289 for (i = 0; i != NumOperands; ++i) {
295 EVT OpVT =
Op.getValueType();
296 switch (getTypeAction(OpVT)) {
304 NeedsReanalyzing = PromoteIntegerOperand(N, i);
308 NeedsReanalyzing = ExpandIntegerOperand(N, i);
312 NeedsReanalyzing = SoftenFloatOperand(N, i);
316 NeedsReanalyzing = ExpandFloatOperand(N, i);
320 NeedsReanalyzing = ScalarizeVectorOperand(N, i);
324 NeedsReanalyzing = SplitVectorOperand(N, i);
328 NeedsReanalyzing = WidenVectorOperand(N, i);
332 NeedsReanalyzing = PromoteFloatOperand(N, i);
342 if (NeedsReanalyzing) {
348 SDNode *M = AnalyzeNewNode(N);
356 "Node morphing changed the number of results!");
367 if (i == NumOperands) {
391 Worklist.push_back(User);
409 Worklist.push_back(User);
413 #ifndef EXPENSIVE_CHECKS 416 PerformExpensiveChecks();
434 if (!IgnoreNodeResults(&Node))
435 for (
unsigned i = 0, NumVals = Node.getNumValues(); i < NumVals; ++i)
436 if (!isTypeLegal(Node.getValueType(i))) {
437 dbgs() <<
"Result type " << i <<
" illegal: ";
443 for (
unsigned i = 0, NumOps = Node.getNumOperands(); i < NumOps; ++i)
444 if (!IgnoreNodeResults(Node.getOperand(i).getNode()) &&
445 !isTypeLegal(Node.getOperand(i).getValueType())) {
446 dbgs() <<
"Operand type " << i <<
" illegal: ";
447 Node.getOperand(i).dump(&DAG);
452 if (Node.getNodeId() ==
NewNode)
453 dbgs() <<
"New node not analyzed?\n";
455 dbgs() <<
"Unanalyzed node not noticed?\n";
456 else if (Node.getNodeId() > 0)
457 dbgs() <<
"Operand not processed?\n";
459 dbgs() <<
"Not added to worklist?\n";
464 Node.dump(&DAG);
dbgs() <<
"\n";
493 std::vector<SDValue> NewOps;
494 unsigned NumProcessed = 0;
504 if (!NewOps.empty()) {
506 NewOps.push_back(Op);
507 }
else if (Op != OrigOp) {
510 NewOps.push_back(Op);
515 if (!NewOps.empty()) {
538 Worklist.push_back(N);
545 void DAGTypeLegalizer::AnalyzeNewValue(
SDValue &Val) {
554 void DAGTypeLegalizer::RemapValue(
SDValue &V) {
555 auto Id = getTableId(V);
559 void DAGTypeLegalizer::RemapId(
TableId &
Id) {
560 auto I = ReplacedValues.find(Id);
561 if (
I != ReplacedValues.end()) {
562 assert(Id !=
I->second &&
"Id is mapped to itself.");
584 DTL(dtl), NodesToAnalyze(nta) {}
589 "Invalid node ID for RAUW deletion!");
592 assert(E &&
"Node not replaced?");
607 void NodeUpdated(
SDNode *N)
override {
613 "Invalid node ID for RAUW deletion!");
632 NodeUpdateListener NUL(*
this, NodesToAnalyze);
637 auto FromId = getTableId(From);
638 auto ToId = getTableId(To);
641 ReplacedValues[FromId] = ToId;
642 DAG.ReplaceAllUsesOfValueWith(From, To);
645 while (!NodesToAnalyze.
empty()) {
655 SDNode *M = AnalyzeNewNode(N);
661 "Node morphing changed the number of results!");
671 auto OldValId = getTableId(OldVal);
672 auto NewValId = getTableId(NewVal);
673 DAG.ReplaceAllUsesOfValueWith(OldVal, NewVal);
674 if (OldValId != NewValId)
675 ReplacedValues[OldValId] = NewValId;
688 TLI.getTypeToTransformTo(*DAG.getContext(), Op.
getValueType()) &&
689 "Invalid type for promoted integer");
690 AnalyzeNewValue(Result);
692 auto &OpIdEntry = PromotedIntegers[getTableId(Op)];
693 assert((OpIdEntry == 0) &&
"Node is already promoted!");
694 OpIdEntry = getTableId(Result);
697 DAG.transferDbgValues(Op, Result);
700 void DAGTypeLegalizer::SetSoftenedFloat(
SDValue Op,
SDValue Result) {
702 TLI.getTypeToTransformTo(*DAG.getContext(), Op.
getValueType()) &&
703 "Invalid type for softened float");
704 AnalyzeNewValue(Result);
706 auto &OpIdEntry = SoftenedFloats[getTableId(Op)];
707 assert((OpIdEntry == 0) &&
"Node is already converted to integer!");
708 OpIdEntry = getTableId(Result);
711 void DAGTypeLegalizer::SetPromotedFloat(
SDValue Op,
SDValue Result) {
713 TLI.getTypeToTransformTo(*DAG.getContext(), Op.
getValueType()) &&
714 "Invalid type for promoted float");
715 AnalyzeNewValue(Result);
717 auto &OpIdEntry = PromotedFloats[getTableId(Op)];
718 assert((OpIdEntry == 0) &&
"Node is already promoted!");
719 OpIdEntry = getTableId(Result);
722 void DAGTypeLegalizer::SetScalarizedVector(
SDValue Op,
SDValue Result) {
727 "Invalid type for scalarized vector");
728 AnalyzeNewValue(Result);
730 auto &OpIdEntry = ScalarizedVectors[getTableId(Op)];
731 assert((OpIdEntry == 0) &&
"Node is already scalarized!");
732 OpIdEntry = getTableId(Result);
737 std::pair<TableId, TableId> &
Entry = ExpandedIntegers[getTableId(Op)];
738 assert((Entry.first != 0) &&
"Operand isn't expanded");
739 Lo = getSDValue(Entry.first);
740 Hi = getSDValue(Entry.second);
746 TLI.getTypeToTransformTo(*DAG.getContext(), Op.
getValueType()) &&
748 "Invalid type for expanded integer");
755 if (DAG.getDataLayout().isBigEndian()) {
766 std::pair<TableId, TableId> &
Entry = ExpandedIntegers[getTableId(Op)];
767 assert((Entry.first == 0) &&
"Node already expanded");
768 Entry.first = getTableId(Lo);
769 Entry.second = getTableId(Hi);
774 std::pair<TableId, TableId> &
Entry = ExpandedFloats[getTableId(Op)];
775 assert((Entry.first != 0) &&
"Operand isn't expanded");
776 Lo = getSDValue(Entry.first);
777 Hi = getSDValue(Entry.second);
783 TLI.getTypeToTransformTo(*DAG.getContext(), Op.
getValueType()) &&
785 "Invalid type for expanded float");
790 std::pair<TableId, TableId> &
Entry = ExpandedFloats[getTableId(Op)];
791 assert((Entry.first == 0) &&
"Node already expanded");
792 Entry.first = getTableId(Lo);
793 Entry.second = getTableId(Hi);
798 std::pair<TableId, TableId> &
Entry = SplitVectors[getTableId(Op)];
799 Lo = getSDValue(Entry.first);
800 Hi = getSDValue(Entry.second);
812 "Invalid type for split vector");
818 std::pair<TableId, TableId> &
Entry = SplitVectors[getTableId(Op)];
819 assert((Entry.first == 0) &&
"Node already split");
820 Entry.first = getTableId(Lo);
821 Entry.second = getTableId(Hi);
824 void DAGTypeLegalizer::SetWidenedVector(
SDValue Op,
SDValue Result) {
826 TLI.getTypeToTransformTo(*DAG.getContext(), Op.
getValueType()) &&
827 "Invalid type for widened vector");
828 AnalyzeNewValue(Result);
830 auto &OpIdEntry = WidenedVectors[getTableId(Op)];
831 assert((OpIdEntry == 0) &&
"Node already widened!");
832 OpIdEntry = getTableId(Result);
848 SDValue DAGTypeLegalizer::BitConvertVectorToIntegerVector(
SDValue Op) {
878 bool DAGTypeLegalizer::CustomLowerNode(
SDNode *N,
EVT VT,
bool LegalizeResult) {
885 TLI.ReplaceNodeResults(N, Results, DAG);
887 TLI.LowerOperationWrapper(N, Results, DAG);
898 SetExpandedInteger(
SDValue(N, 0), Results[0], Results[1]);
900 ReplaceValueWith(
SDValue(N, 1), Results[2]);
906 "Custom lowering returned the wrong number of results!");
907 for (
unsigned i = 0,
e = Results.
size(); i !=
e; ++i) {
908 ReplaceValueWith(
SDValue(N, i), Results[i]);
916 bool DAGTypeLegalizer::CustomWidenLowerNode(
SDNode *N,
EVT VT) {
922 TLI.ReplaceNodeResults(N, Results, DAG);
930 "Custom lowering returned the wrong number of results!");
931 for (
unsigned i = 0,
e = Results.
size(); i !=
e; ++i) {
934 ReplaceValueWith(
SDValue(N, i), Results[i]);
936 SetWidenedVector(
SDValue(N, i), Results[i]);
941 SDValue DAGTypeLegalizer::DisintegrateMERGE_VALUES(
SDNode *N,
unsigned ResNo) {
950 void DAGTypeLegalizer::GetPairElements(
SDValue Pair,
953 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), Pair.
getValueType());
955 DAG.getIntPtrConstant(0, dl));
957 DAG.getIntPtrConstant(1, dl));
970 EVT ShiftAmtVT = TLI.getShiftAmountTy(NVT, DAG.getDataLayout(),
false);
975 return DAG.getNode(
ISD::OR, dlHi, NVT, Lo, Hi);
985 EVT BoolVT = getSetCCResultType(ValVT);
988 return DAG.getNode(ExtendCode, dl, BoolVT, Bool);
992 void DAGTypeLegalizer::SplitInteger(
SDValue Op,
999 unsigned ReqShiftAmountInBits =
1002 TLI.getScalarShiftAmountTy(DAG.getDataLayout(), Op.
getValueType());
1003 if (ReqShiftAmountInBits > ShiftAmountTy.getSizeInBits())
1012 void DAGTypeLegalizer::SplitInteger(
SDValue Op,
1016 SplitInteger(Op, HalfVT, HalfVT, Lo, Hi);
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
static MVT getIntegerVT(unsigned BitWidth)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
EVT getValueType() const
Return the ValueType of the referenced return value.
void setFlags(SDNodeFlags NewFlags)
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant, which is required to be operand #1) half of the integer or float value specified as operand #0.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool LegalizeTypes()
This transforms the SelectionDAG into a SelectionDAG that only uses types natively supported by the t...
This class represents lattice values for constants.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
void push_back(const T &Elt)
Clients of various APIs that cause global effects on the DAG can optionally implement this interface...
friend struct DAGUpdateListener
DAGUpdateListener is a friend so it can manipulate the listener stack.
static ISD::NodeType getExtendForContent(BooleanContent Content)
Function Alias Analysis Results
TypeSize getScalarValueSizeInBits() const
This takes an arbitrary SelectionDAG as input and hacks on it until only value types the target machi...
const SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
SDNode * getNode() const
get the SDNode which holds the desired result
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
const T & back() const
Return the last element of the SetVector.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
This node's ID needs to be set to the number of its unprocessed operands.
Shift and rotation operations.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
bool remove(const value_type &X)
Remove an item from the set vector.
void pop_back()
Remove the last element of the SetVector.
SelectionDAG & getDAG() const
iterator_range< allnodes_iterator > allnodes()
bool insert(const value_type &X)
Insert a new element into the SetVector.
op_iterator op_begin() const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
static cl::opt< bool > EnableExpensiveChecks("enable-legalize-types-checking", cl::Hidden)
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
bool run()
This is the main entry point for the type legalizer.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
This is a new node, not before seen, that was created in the process of legalizing some other node...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const SDValue & getOperand(unsigned Num) const
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
This class provides iterator support for SDUse operands that use a specific SDNode.
std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
This class contains a discriminated union of information about pointers in memory operands...
unsigned getNumOperands() const
Return the number of values used by this operation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void NoteDeletion(SDNode *Old, SDNode *New)
void dump() const
Dump this node, for debugging.
A SetVector that performs no allocations if smaller than a certain size.
BlockVerifier::State From
void setNode(SDNode *N)
set the SDNode
EVT getVectorElementType() const
Given a vector type, return the type of each element.
testing::Matcher< const detail::ErrorHolder & > Failed()
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
This is a node that has already been processed.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT...
static use_iterator use_end()
ZERO_EXTEND - Used for integer types, zeroing the new bits.
ANY_EXTEND - Used for integer types. The high bits are undefined.
ElementCount getVectorElementCount() const
int getNodeId() const
Return the unique node id.
bool isVector() const
Return true if this is a vector value type.
LLVM_NODISCARD bool empty() const
bool empty() const
Determine if the SetVector is empty or not.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
This class is used to form a handle around another node that is persistent and is updated across invo...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
All operands have been processed, so this node is ready to be handled.
TRUNCATE - Completely drop the high bits.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
DAGTypeLegalizer(SelectionDAG &dag)