52 #define DEBUG_TYPE "legalizevectorops" 56 class VectorLegalizer {
68 LegalizedNodes.
insert(std::make_pair(From, To));
71 LegalizedNodes.
insert(std::make_pair(To, To));
162 bool VectorLegalizer::Run() {
164 bool HasVectors =
false;
166 E = std::prev(DAG.allnodes_end());
I != std::next(
E); ++
I) {
171 HasVectors |= J->isVector();
188 DAG.AssignTopologicalOrder();
190 E = std::prev(DAG.allnodes_end());
I != std::next(
E); ++
I)
194 SDValue OldRoot = DAG.getRoot();
195 assert(LegalizedNodes.count(OldRoot) &&
"Root didn't get legalized?");
196 DAG.setRoot(LegalizedNodes[OldRoot]);
198 LegalizedNodes.clear();
201 DAG.RemoveDeadNodes();
217 if (I != LegalizedNodes.
end())
return I->second;
229 bool HasVectorValue =
false;
240 return TranslateLegalizeResults(Op, Result);
242 if (
SDValue Lowered = TLI.LowerOperation(Result, DAG)) {
243 if (Lowered == Result)
244 return TranslateLegalizeResults(Op, Lowered);
251 "There are still live users of the old chain!");
252 return LegalizeOp(Lowered);
254 return TranslateLegalizeResults(Op, Lowered);
259 return LegalizeOp(ExpandLoad(Op));
269 switch (TLI.getTruncStoreAction(ValVT, StVT)) {
272 return TranslateLegalizeResults(Op, Result);
274 SDValue Lowered = TLI.LowerOperation(Result, DAG);
275 Changed = Lowered != Result;
276 return TranslateLegalizeResults(Op, Lowered);
280 return LegalizeOp(ExpandStore(Op));
284 HasVectorValue =
true;
289 HasVectorValue |= J->isVector();
291 return TranslateLegalizeResults(Op, Result);
296 return TranslateLegalizeResults(Op, Result);
318 Action = TLI.getStrictFPOperationAction(Node->getOpcode(),
319 Node->getValueType(0));
397 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
400 Action = TLI.getOperationAction(Node->getOpcode(),
401 cast<VTSDNode>(Node->getOperand(1))->getVT());
405 Action = TLI.getOperationAction(Node->getOpcode(),
406 Node->getOperand(0).getValueType());
409 Action = TLI.getOperationAction(Node->getOpcode(),
410 cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
413 Action = TLI.getOperationAction(Node->getOpcode(),
414 cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
418 LLVM_DEBUG(
dbgs() <<
"\nLegalizing vector op: "; Node->dump(&DAG));
423 Result = Promote(Op);
431 if (
SDValue Tmp1 = TLI.LowerOperation(Op, DAG)) {
445 Result = LegalizeOp(Result);
451 AddLegalizedOperand(Op, Result);
462 return PromoteINT_TO_FP(Op);
466 return PromoteFP_TO_INT(Op);
476 "Can't promote a vector with multiple results!");
487 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())
498 NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()))
499 return DAG.getNode(
ISD::FP_ROUND, dl, VT, Op, DAG.getIntPtrConstant(0, dl));
510 "Vectors have different number of elements!");
519 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.
getOperand(j));
535 "Vectors have different number of elements!");
566 if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.
isByteSized()) {
582 EVT WideVT = TLI.getPointerTy(DAG.getDataLayout());
585 "Could not handle the sophisticated case when the widest integer is" 588 "Type is not legalized?");
592 unsigned RemainingBytes = SrcVT.getStoreSize();
594 while (RemainingBytes > 0) {
596 unsigned LoadBytes = WideBytes;
598 if (RemainingBytes >= LoadBytes) {
600 DAG.getLoad(WideVT, dl, Chain, BasePTR,
606 while (RemainingBytes < LoadBytes) {
611 DAG.getExtLoad(
ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
617 RemainingBytes -= LoadBytes;
620 BasePTR = DAG.getObjectPtrOffset(dl, BasePTR, LoadBytes);
628 SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, dl, WideVT);
630 unsigned BitOffset = 0;
631 unsigned WideIdx = 0;
634 for (
unsigned Idx = 0; Idx != NumElem; ++Idx) {
637 if (BitOffset < WideBits) {
638 ShAmt = DAG.getConstant(
639 BitOffset, dl, TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
644 BitOffset += SrcEltBits;
645 if (BitOffset >= WideBits) {
647 BitOffset -= WideBits;
649 ShAmt = DAG.getConstant(
650 SrcEltBits - BitOffset, dl,
651 TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
663 Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
666 Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
670 DAG.getConstant(WideBits - SrcEltBits, dl,
671 TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
674 Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
683 SDValue Scalarized = TLI.scalarizeVectorLoad(LD, DAG);
694 AddLegalizedOperand(Op.
getValue(0), Value);
695 AddLegalizedOperand(Op.
getValue(1), NewChain);
697 return (Op.
getResNo() ? NewChain : Value);
702 SDValue TF = TLI.scalarizeVectorStore(ST, DAG);
703 AddLegalizedOperand(Op, TF);
710 return ExpandSEXTINREG(Op);
712 return ExpandANY_EXTEND_VECTOR_INREG(Op);
714 return ExpandSIGN_EXTEND_VECTOR_INREG(Op);
716 return ExpandZERO_EXTEND_VECTOR_INREG(Op);
718 return ExpandBSWAP(Op);
720 return ExpandVSELECT(Op);
722 return ExpandSELECT(Op);
724 return ExpandUINT_TO_FLOAT(Op);
726 return ExpandFNEG(Op);
728 return ExpandFSUB(Op);
730 return UnrollVSETCC(Op);
732 return ExpandBITREVERSE(Op);
735 return ExpandCTLZ(Op);
737 return ExpandCTTZ_ZERO_UNDEF(Op);
755 return ExpandStrictFPOp(Op);
757 return DAG.UnrollVectorOp(Op.
getNode());
784 return DAG.UnrollVectorOp(Op.
getNode());
792 Mask = DAG.getSelect(DL, BitTy, Mask,
795 DAG.getConstant(0, DL, BitTy));
798 Mask = DAG.getSplatBuildVector(MaskTy, DL, Mask);
806 SDValue AllOnes = DAG.getConstant(
822 return DAG.UnrollVectorOp(Op.
getNode());
829 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT);
833 return DAG.getNode(
ISD::SRA, DL, VT, Op, ShiftSz);
838 SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(
SDValue Op) {
848 ShuffleMask.
resize(NumSrcElements, -1);
851 int ExtLaneScale = NumSrcElements / NumElements;
852 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
853 for (
int i = 0; i < NumElements; ++i)
854 ShuffleMask[i * ExtLaneScale + EndianOffset] = i;
858 DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask));
861 SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(
SDValue Op) {
869 Op = DAG.getAnyExtendVectorInReg(Src, DL, VT);
876 SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT);
878 DAG.getNode(
ISD::SHL, DL, VT, Op, ShiftAmount),
885 SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(
SDValue Op) {
894 SDValue Zero = DAG.getConstant(0, DL, SrcVT);
899 ShuffleMask.
reserve(NumSrcElements);
900 for (
int i = 0; i < NumSrcElements; ++i)
903 int ExtLaneScale = NumSrcElements / NumElements;
904 int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
905 for (
int i = 0; i < NumElements; ++i)
906 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
909 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
915 for (
int J = ScalarSizeInBytes - 1; J >= 0; --J)
916 ShuffleMask.
push_back((
I * ScalarSizeInBytes) + J);
928 if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT))
929 return DAG.UnrollVectorOp(Op.
getNode());
933 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask);
942 return DAG.UnrollVectorOp(Op.
getNode());
948 if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) {
953 if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) &&
955 (TLI.isOperationLegalOrCustom(
ISD::SHL, ByteVT) &&
956 TLI.isOperationLegalOrCustom(
ISD::SRL, ByteVT) &&
957 TLI.isOperationLegalOrCustomOrPromote(
ISD::AND, ByteVT) &&
958 TLI.isOperationLegalOrCustomOrPromote(
ISD::OR, ByteVT)))) {
961 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
970 if (!TLI.isOperationLegalOrCustom(
ISD::SHL, VT) ||
971 !TLI.isOperationLegalOrCustom(
ISD::SRL, VT) ||
972 !TLI.isOperationLegalOrCustomOrPromote(
ISD::AND, VT) ||
973 !TLI.isOperationLegalOrCustomOrPromote(
ISD::OR, VT))
974 return DAG.UnrollVectorOp(Op.
getNode());
1003 return DAG.UnrollVectorOp(Op.
getNode());
1009 return DAG.UnrollVectorOp(Op.
getNode());
1017 SDValue AllOnes = DAG.getConstant(
1034 return DAG.UnrollVectorOp(Op.
getNode());
1037 assert((BW == 64 || BW == 32) &&
1038 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
1040 SDValue HalfWord = DAG.getConstant(BW / 2, DL, VT);
1045 uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF;
1046 SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT);
1074 return DAG.UnrollVectorOp(Op.
getNode());
1082 if (TLI.isOperationLegalOrCustom(
ISD::FNEG, VT) &&
1083 TLI.isOperationLegalOrCustom(
ISD::FADD, VT))
1086 return DAG.UnrollVectorOp(Op.
getNode());
1095 TLI.isOperationLegalOrCustom(
ISD::CTLZ, VT)) {
1110 TLI.isOperationLegalOrCustom(
ISD::CTPOP, VT) &&
1111 TLI.isOperationLegalOrCustom(
ISD::SRL, VT) &&
1112 TLI.isOperationLegalOrCustomOrPromote(
ISD::OR, VT) &&
1113 TLI.isOperationLegalOrCustomOrPromote(
ISD::XOR, VT)) {
1116 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
1118 for (
unsigned i = 1; i != NumBitsPerElt; i *= 2)
1123 Res = DAG.getNOT(DL, Res, VT);
1128 return DAG.UnrollVectorOp(Op.
getNode());
1139 return DAG.UnrollVectorOp(Op.
getNode());
1154 for (
unsigned i = 0; i < NumElems; ++i) {
1156 SDValue Idx = DAG.getConstant(i, dl,
1163 for (
unsigned j = 1; j < NumOpers; ++j) {
1180 SDValue Result = DAG.getBuildVector(VT, dl, OpValues);
1183 AddLegalizedOperand(Op.
getValue(0), Result);
1184 AddLegalizedOperand(Op.
getValue(1), NewChain);
1197 for (
unsigned i = 0; i < NumElems; ++i) {
1200 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
1203 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
1205 TLI.getSetCCResultType(DAG.getDataLayout(),
1206 *DAG.getContext(), TmpEltVT),
1207 LHSElem, RHSElem, CC);
1208 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i],
1211 DAG.getConstant(0, dl, EltVT));
1213 return DAG.getBuildVector(VT, dl, Ops);
1217 return VectorLegalizer(*this).Run();
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
void push_back(const T &Elt)
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
virtual MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
EVT getValueType() const
Return the ValueType of the referenced return value.
Constrained versions of libm-equivalent floating point intrinsics.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
Compute iterated dominance frontiers using a linear time algorithm.
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
bool isVector() const
Return true if this is a vector value type.
const SDValue & getBasePtr() const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
const SDValue & getValue() const
bool LegalizeVectors()
This transforms the SelectionDAG into a SelectionDAG that only uses vector math operations supported ...
AAMDNodes getAAInfo() const
Returns the AA info that describes the dereference.
unsigned getVectorNumElements() const
const SDValue & getChain() const
unsigned getAlignment() const
Constrained versions of the binary floating point operators.
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
[US]{MIN/MAX} - Binary minimum or maximum or signed or unsigned integers.
const SDNodeFlags getFlags() const
SDNode * getNode() const
get the SDNode which holds the desired result
void reserve(size_type N)
bool isByteSized() const
Return true if the bit size is a multiple of 8.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
unsigned getValueSizeInBits() const
Returns the size of the value in bits.
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
bool isTruncatingStore() const
Return true if the op does a truncation before store.
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Shift and rotation operations.
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
unsigned getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
This file implements a class to represent arbitrary precision integral constant values and operations...
unsigned getScalarSizeInBits() const
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Simple integer binary arithmetic operators.
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
MVT getVectorElementType() const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
This class is used to represent ISD::STORE nodes.
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a vector with the specified, possibly variable...
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Simple binary floating point operators.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
iterator_range< value_op_iterator > op_values() const
bool isRound() const
Return true if the size is a power-of-two number of bytes.
const SDValue & getOperand(unsigned Num) const
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Bit counting operators with an undefined result for zero inputs.
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
TokenFactor - This node takes multiple tokens as input and produces a single token result...
const TargetLowering & getTargetLoweringInfo() const
Iterator for intrusive lists based on ilist_node.
Returns platform specific canonical encoding of a floating point number.
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
EVT getVectorElementType() const
Given a vector type, return the type of each element.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
X = FP_ROUND_INREG(Y, VT) - This operator takes an FP register, and rounds it to a floating point val...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Byte Swap and Counting operators.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachinePointerInfo getWithOffset(int64_t O) const
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...
EVT getMemoryVT() const
Return the type of the in-memory value.
Select(COND, TRUEVAL, FALSEVAL).
ZERO_EXTEND - Used for integer types, zeroing the new bits.
ANY_EXTEND - Used for integer types. The high bits are undefined.
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
FMINNAN/FMAXNAN - Behave identically to FMINNUM/FMAXNUM, except that when a single input is NaN...
bool isVector() const
Return true if this is a vector value type.
Bitwise operators - logical and, logical or, logical xor.
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Flags getFlags() const
Return the raw flags of the source value,.
unsigned getOpcode() const
SDValue getValue(unsigned R) const
const MachinePointerInfo & getPointerInfo() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
FMA - Perform a * b + c with no intermediate rounding step.
unsigned getResNo() const
get the index which selects a specific result in the SDNode
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
SetCC operator - This evaluates to a true value iff the condition is true.
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
unsigned getNumOperands() const
const SDValue & getOperand(unsigned i) const
TRUNCATE - Completely drop the high bits.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW, FLOG, FLOG2, FLOG10, FEXP, FEXP2, FCEIL, FTRUNC, FRINT, FNEARBYINT, FROUND, FFLOOR - Perform various unary floating point operations.
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl< int > &ShuffleMask)
This file describes how to lower LLVM code to machine code.
This class is used to represent ISD::LOAD nodes.