25 #define DEBUG_TYPE "legalize-types"
31 void DAGTypeLegalizer::PerformExpensiveChecks() {
78 if (Node.getNodeId() ==
NewNode)
81 for (
unsigned i = 0, e = Node.getNumValues(); i != e; ++i) {
83 EVT VT = Res.getValueType();
87 if (ReplacedValues.find(Res) != ReplacedValues.end()) {
92 if (UI.getUse().getResNo() == i)
94 "Remapped value has non-trivial use!");
98 SDValue NewVal = ReplacedValues[Res];
100 while (I != ReplacedValues.end()) {
102 I = ReplacedValues.find(NewVal);
105 "ReplacedValues maps to a new node!");
107 if (PromotedIntegers.find(Res) != PromotedIntegers.end())
109 if (SoftenedFloats.find(Res) != SoftenedFloats.end())
111 if (ScalarizedVectors.find(Res) != ScalarizedVectors.end())
113 if (ExpandedIntegers.find(Res) != ExpandedIntegers.end())
115 if (ExpandedFloats.find(Res) != ExpandedFloats.end())
117 if (SplitVectors.find(Res) != SplitVectors.end())
119 if (WidenedVectors.find(Res) != WidenedVectors.end())
121 if (PromotedFloats.find(Res) != PromotedFloats.end())
128 if ((Node.getNodeId() ==
NewNode && Mapped > 1) ||
129 (Node.getNodeId() !=
NewNode && Mapped != 0)) {
130 dbgs() <<
"Unprocessed value in a map!";
133 }
else if (isTypeLegal(VT) || IgnoreNodeResults(&Node)) {
135 dbgs() <<
"Value with legal type was transformed!";
143 isLegalInHWReg(VT))) {
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) {
207 AddToWorklist(&Node);
214 while (!Worklist.empty()) {
215 #ifndef EXPENSIVE_CHECKS
218 PerformExpensiveChecks();
220 SDNode *N = Worklist.back();
223 "Node should be ready if on worklist!");
225 if (IgnoreNodeResults(N))
230 for (
unsigned i = 0, NumResults = N->
getNumValues(); i < NumResults; ++i) {
232 switch (getTypeAction(ResultVT)) {
241 PromoteIntegerResult(N, i);
245 ExpandIntegerResult(N, i);
249 Changed = SoftenFloatResult(N, i);
253 assert(isLegalInHWReg(ResultVT) &&
254 "Unchanged SoftenFloatResult should be legal in register!");
257 ExpandFloatResult(N, i);
261 ScalarizeVectorResult(N, i);
265 SplitVectorResult(N, i);
269 WidenVectorResult(N, i);
273 PromoteFloatResult(N, i);
284 bool NeedsReanalyzing =
false;
286 for (i = 0; i != NumOperands; ++i) {
291 switch (getTypeAction(OpVT)) {
298 NeedsReanalyzing = PromoteIntegerOperand(N, i);
302 NeedsReanalyzing = ExpandIntegerOperand(N, i);
306 NeedsReanalyzing = SoftenFloatOperand(N, i);
310 NeedsReanalyzing = ExpandFloatOperand(N, i);
314 NeedsReanalyzing = ScalarizeVectorOperand(N, i);
318 NeedsReanalyzing = SplitVectorOperand(N, i);
322 NeedsReanalyzing = WidenVectorOperand(N, i);
326 NeedsReanalyzing = PromoteFloatOperand(N, i);
336 if (NeedsReanalyzing) {
342 SDNode *M = AnalyzeNewNode(N);
350 "Node morphing changed the number of results!");
351 for (
unsigned i = 0, e = N->
getNumValues(); i != e; ++i)
361 if (i == NumOperands) {
384 Worklist.push_back(User);
402 Worklist.push_back(User);
406 #ifndef EXPENSIVE_CHECKS
409 PerformExpensiveChecks();
432 if (!IgnoreNodeResults(&Node))
433 for (
unsigned i = 0, NumVals = Node.getNumValues(); i < NumVals; ++i)
434 if (!isTypeLegal(Node.getValueType(i)) &&
436 dbgs() <<
"Result type " << i <<
" illegal: ";
442 for (
unsigned i = 0, NumOps = Node.getNumOperands(); i < NumOps; ++i)
443 if (!IgnoreNodeResults(Node.getOperand(i).getNode()) &&
444 !isTypeLegal(Node.getOperand(i).getValueType()) &&
445 !TLI.
isTypeLegal(Node.getOperand(i).getValueType())) {
446 dbgs() <<
"Operand type " << i <<
" illegal: ";
447 Node.getOperand(i).dump();
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";
496 std::vector<SDValue> NewOps;
497 unsigned NumProcessed = 0;
507 if (!NewOps.empty()) {
509 NewOps.push_back(Op);
510 }
else if (Op != OrigOp) {
513 NewOps.push_back(Op);
518 if (!NewOps.empty()) {
542 Worklist.push_back(N);
549 void DAGTypeLegalizer::AnalyzeNewValue(
SDValue &Val) {
569 void DAGTypeLegalizer::ExpungeNode(
SDNode *N) {
576 if (ReplacedValues.find(
SDValue(N, i)) != ReplacedValues.end())
585 E = PromotedIntegers.end(); I !=
E; ++
I) {
586 assert(I->first.getNode() !=
N);
587 RemapValue(I->second);
591 E = SoftenedFloats.end(); I !=
E; ++
I) {
592 assert(I->first.getNode() !=
N);
593 RemapValue(I->second);
597 E = ScalarizedVectors.end(); I !=
E; ++
I) {
598 assert(I->first.getNode() !=
N);
599 RemapValue(I->second);
603 E = WidenedVectors.end(); I !=
E; ++
I) {
604 assert(I->first.getNode() !=
N);
605 RemapValue(I->second);
609 I = ExpandedIntegers.begin(),
E = ExpandedIntegers.end(); I !=
E; ++
I){
610 assert(I->first.getNode() !=
N);
611 RemapValue(I->second.first);
612 RemapValue(I->second.second);
616 I = ExpandedFloats.begin(),
E = ExpandedFloats.end(); I !=
E; ++
I) {
617 assert(I->first.getNode() !=
N);
618 RemapValue(I->second.first);
619 RemapValue(I->second.second);
623 I = SplitVectors.begin(),
E = SplitVectors.end(); I !=
E; ++
I) {
624 assert(I->first.getNode() !=
N);
625 RemapValue(I->second.first);
626 RemapValue(I->second.second);
630 E = ReplacedValues.end(); I !=
E; ++
I)
631 RemapValue(I->second);
633 for (
unsigned i = 0, e = N->
getNumValues(); i != e; ++i)
634 ReplacedValues.erase(
SDValue(N, i));
639 void DAGTypeLegalizer::RemapValue(
SDValue &N) {
641 if (I != ReplacedValues.end()) {
644 RemapValue(I->second);
663 DTL(dtl), NodesToAnalyze(nta) {}
668 "Invalid node ID for RAUW deletion!");
671 assert(E &&
"Node not replaced?");
672 DTL.NoteDeletion(N, E);
676 NodesToAnalyze.remove(N);
683 NodesToAnalyze.insert(E);
686 void NodeUpdated(
SDNode *N)
override {
692 "Invalid node ID for RAUW deletion!");
694 NodesToAnalyze.insert(N);
702 void DAGTypeLegalizer::ReplaceValueWith(
SDValue From,
SDValue To) {
712 NodeUpdateListener NUL(*
this, NodesToAnalyze);
718 ReplacedValues[From] = To;
721 while (!NodesToAnalyze.
empty()) {
731 SDNode *M = AnalyzeNewNode(N);
737 "Node morphing changed the number of results!");
738 for (
unsigned i = 0, e = N->
getNumValues(); i != e; ++i) {
748 ReplacedValues[OldVal] = NewVal;
759 void DAGTypeLegalizer::SetPromotedInteger(
SDValue Op,
SDValue Result) {
762 "Invalid type for promoted integer");
763 AnalyzeNewValue(Result);
770 void DAGTypeLegalizer::SetSoftenedFloat(
SDValue Op,
SDValue Result) {
777 "Invalid type for softened float");
778 AnalyzeNewValue(Result);
787 "Node is already converted to integer!");
791 void DAGTypeLegalizer::SetPromotedFloat(
SDValue Op,
SDValue Result) {
794 "Invalid type for promoted float");
795 AnalyzeNewValue(Result);
802 void DAGTypeLegalizer::SetScalarizedVector(
SDValue Op,
SDValue Result) {
807 "Invalid type for scalarized vector");
808 AnalyzeNewValue(Result);
810 SDValue &OpEntry = ScalarizedVectors[
Op];
817 std::pair<SDValue, SDValue> &Entry = ExpandedIntegers[
Op];
818 RemapValue(Entry.first);
819 RemapValue(Entry.second);
820 assert(Entry.first.getNode() &&
"Operand isn't expanded");
830 "Invalid type for expanded integer");
836 std::pair<SDValue, SDValue> &Entry = ExpandedIntegers[
Op];
837 assert(!Entry.first.getNode() &&
"Node already expanded");
844 std::pair<SDValue, SDValue> &Entry = ExpandedFloats[
Op];
845 RemapValue(Entry.first);
846 RemapValue(Entry.second);
847 assert(Entry.first.getNode() &&
"Operand isn't expanded");
857 "Invalid type for expanded float");
863 std::pair<SDValue, SDValue> &Entry = ExpandedFloats[
Op];
864 assert(!Entry.first.getNode() &&
"Node already expanded");
871 std::pair<SDValue, SDValue> &Entry = SplitVectors[
Op];
872 RemapValue(Entry.first);
873 RemapValue(Entry.second);
874 assert(Entry.first.getNode() &&
"Operand isn't split");
886 "Invalid type for split vector");
892 std::pair<SDValue, SDValue> &Entry = SplitVectors[
Op];
893 assert(!Entry.first.getNode() &&
"Node already split");
898 void DAGTypeLegalizer::SetWidenedVector(
SDValue Op,
SDValue Result) {
901 "Invalid type for widened vector");
902 AnalyzeNewValue(Result);
922 SDValue DAGTypeLegalizer::BitConvertVectorToIntegerVector(
SDValue Op) {
952 bool DAGTypeLegalizer::CustomLowerNode(
SDNode *N,
EVT VT,
bool LegalizeResult) {
972 SetExpandedInteger(
SDValue(N, 0), Results[0], Results[1]);
974 ReplaceValueWith(
SDValue(N, 1), Results[2]);
980 "Custom lowering returned the wrong number of results!");
981 for (
unsigned i = 0, e = Results.
size(); i != e; ++i) {
982 ReplaceValueWith(
SDValue(N, i), Results[i]);
990 bool DAGTypeLegalizer::CustomWidenLowerNode(
SDNode *N,
EVT VT) {
1004 "Custom lowering returned the wrong number of results!");
1005 for (
unsigned i = 0, e = Results.
size(); i != e; ++i)
1006 SetWidenedVector(
SDValue(N, i), Results[i]);
1010 SDValue DAGTypeLegalizer::DisintegrateMERGE_VALUES(
SDNode *N,
unsigned ResNo) {
1011 for (
unsigned i = 0, e = N->
getNumValues(); i != e; ++i)
1019 void DAGTypeLegalizer::GetPairElements(
SDValue Pair,
1055 }
else if (NumOps == 1) {
1059 }
else if (NumOps == 2) {
1065 for (
unsigned i = 0; i < NumOps; ++i)
1073 std::pair<SDValue, SDValue>
1079 TargetLowering::ArgListEntry Entry;
1085 Entry.IsSExt = isSigned;
1086 Entry.IsZExt = !isSigned;
1087 Args.push_back(Entry);
1095 CLI.setDebugLoc(
SDLoc(Node))
1099 .setSExtResult(isSigned)
1100 .setZExtResult(!isSigned);
1114 EVT BoolVT = getSetCCResultType(ValVT);
1117 return DAG.
getNode(ExtendCode, dl, BoolVT, Bool);
1130 "Unexpected types in WidenTargetBoolean");
1133 Bool = ModifyToType(Bool, WideVT, WithZeroes);
1134 return PromoteTargetBoolean(Bool, ValVT);
1138 void DAGTypeLegalizer::SplitInteger(
SDValue Op,
1153 void DAGTypeLegalizer::SplitInteger(
SDValue Op,
1157 SplitInteger(Op, HalfVT, HalfVT, Lo, Hi);
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
void push_back(const T &Elt)
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
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.
LLVMContext * getContext() const
bool LegalizeTypes()
This transforms the SelectionDAG into a SelectionDAG that only uses types natively supported by the t...
Clients of various APIs that cause global effects on the DAG can optionally implement this interface...
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit...
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
static ISD::NodeType getExtendForContent(BooleanContent Content)
Function Alias Analysis Results
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
unsigned getNumOperands() const
Return the number of values used by this operation.
unsigned getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned Num) const
This takes an arbitrary SelectionDAG as input and hacks on it until only value types the target machi...
void setNodeId(int Id)
Set unique node id.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LegalizeAction getOperationAction(unsigned Op, EVT VT) const
Return how this operation should be treated: either it is legal, needs to be promoted to a larger siz...
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
SDValue getExternalSymbol(const char *Sym, EVT VT)
bool isVector() const
Return true if this is a vector value type.
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
This node's ID needs to be set to the number of its unprocessed operands.
Shift and rotation operations.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
void pop_back()
Remove the last element of the SetVector.
iterator_range< allnodes_iterator > allnodes()
LLVM_NODISCARD bool empty() const
EVT getVectorElementType() const
Given a vector type, return the type of each element.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
bool empty() const
Determine if the SetVector is empty or not.
const DataLayout & getDataLayout() const
static cl::opt< bool > EnableExpensiveChecks("enable-legalize-types-checking", cl::Hidden)
SDNode * getNode() const
get the SDNode which holds the desired result
bool run()
This is the main entry point for the type legalizer.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
The instances of the Type class are immutable: once they are created, they are never changed...
This is a new node, not before seen, that was created in the process of legalizing some other node...
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
unsigned getScalarValueSizeInBits() 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.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
std::vector< ArgListEntry > ArgListTy
This structure contains all information that is necessary for lowering calls.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, bool isSigned, const SDLoc &dl, bool doesNotReturn=false, bool isReturnValueUsed=true) const
Returns a pair of (return value, chain).
This class contains a discriminated union of information about pointers in memory operands...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, unsigned Alignment=0, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands...
SDValue CreateStackTemporary(EVT VT, unsigned minAlign=1)
Create a stack temporary, suitable for holding the specified value type.
virtual void ReplaceNodeResults(SDNode *, SmallVectorImpl< SDValue > &, SelectionDAG &) const
This callback is invoked when a node result type is illegal for the target, and the operation was reg...
A SetVector that performs no allocations if smaller than a certain size.
void dump() const
Dump this node, for debugging.
void setNode(SDNode *N)
set the SDNode
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
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.
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
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...
op_iterator op_begin() const
static use_iterator use_end()
ZERO_EXTEND - Used for integer types, zeroing the new bits.
MVT::ElementCount getVectorElementCount() const
ANY_EXTEND - Used for integer types. The high bits are undefined.
llvm_unreachable("Unknown RegBank!")
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
const T & back() const
Return the last element of the SetVector.
int getNodeId() const
Return the unique node id.
EVT getValueType() const
Return the ValueType of the referenced return value.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
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())
MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
All operands have been processed, so this node is ready to be handled.
void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.Val alone...
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
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.
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.