xref: /aosp_15_r20/external/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the SelectionDAG::LegalizeVectors method.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // The vector legalizer looks for vector operations which might need to be
13*9880d681SAndroid Build Coastguard Worker // scalarized and legalizes them. This is a separate step from Legalize because
14*9880d681SAndroid Build Coastguard Worker // scalarizing can introduce illegal types.  For example, suppose we have an
15*9880d681SAndroid Build Coastguard Worker // ISD::SDIV of type v2i64 on x86-32.  The type is legal (for example, addition
16*9880d681SAndroid Build Coastguard Worker // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the
17*9880d681SAndroid Build Coastguard Worker // operation, which introduces nodes with the illegal type i64 which must be
18*9880d681SAndroid Build Coastguard Worker // expanded.  Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC;
19*9880d681SAndroid Build Coastguard Worker // the operation must be unrolled, which introduces nodes with the illegal
20*9880d681SAndroid Build Coastguard Worker // type i8 which must be promoted.
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker // This does not legalize vector manipulations like ISD::BUILD_VECTOR,
23*9880d681SAndroid Build Coastguard Worker // or operations that happen to take a vector which are custom-lowered;
24*9880d681SAndroid Build Coastguard Worker // the legalization for such operations never produces nodes
25*9880d681SAndroid Build Coastguard Worker // with illegal types, so it's okay to put off legalizing them until
26*9880d681SAndroid Build Coastguard Worker // SelectionDAG::Legalize runs.
27*9880d681SAndroid Build Coastguard Worker //
28*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAG.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
32*9880d681SAndroid Build Coastguard Worker using namespace llvm;
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker namespace {
35*9880d681SAndroid Build Coastguard Worker class VectorLegalizer {
36*9880d681SAndroid Build Coastguard Worker   SelectionDAG& DAG;
37*9880d681SAndroid Build Coastguard Worker   const TargetLowering &TLI;
38*9880d681SAndroid Build Coastguard Worker   bool Changed; // Keep track of whether anything changed
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker   /// For nodes that are of legal width, and that have more than one use, this
41*9880d681SAndroid Build Coastguard Worker   /// map indicates what regularized operand to use.  This allows us to avoid
42*9880d681SAndroid Build Coastguard Worker   /// legalizing the same thing more than once.
43*9880d681SAndroid Build Coastguard Worker   SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes;
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   /// \brief Adds a node to the translation cache.
AddLegalizedOperand(SDValue From,SDValue To)46*9880d681SAndroid Build Coastguard Worker   void AddLegalizedOperand(SDValue From, SDValue To) {
47*9880d681SAndroid Build Coastguard Worker     LegalizedNodes.insert(std::make_pair(From, To));
48*9880d681SAndroid Build Coastguard Worker     // If someone requests legalization of the new node, return itself.
49*9880d681SAndroid Build Coastguard Worker     if (From != To)
50*9880d681SAndroid Build Coastguard Worker       LegalizedNodes.insert(std::make_pair(To, To));
51*9880d681SAndroid Build Coastguard Worker   }
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   /// \brief Legalizes the given node.
54*9880d681SAndroid Build Coastguard Worker   SDValue LegalizeOp(SDValue Op);
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker   /// \brief Assuming the node is legal, "legalize" the results.
57*9880d681SAndroid Build Coastguard Worker   SDValue TranslateLegalizeResults(SDValue Op, SDValue Result);
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker   /// \brief Implements unrolling a VSETCC.
60*9880d681SAndroid Build Coastguard Worker   SDValue UnrollVSETCC(SDValue Op);
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker   /// \brief Implement expand-based legalization of vector operations.
63*9880d681SAndroid Build Coastguard Worker   ///
64*9880d681SAndroid Build Coastguard Worker   /// This is just a high-level routine to dispatch to specific code paths for
65*9880d681SAndroid Build Coastguard Worker   /// operations to legalize them.
66*9880d681SAndroid Build Coastguard Worker   SDValue Expand(SDValue Op);
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   /// \brief Implements expansion for FNEG; falls back to UnrollVectorOp if
69*9880d681SAndroid Build Coastguard Worker   /// FSUB isn't legal.
70*9880d681SAndroid Build Coastguard Worker   ///
71*9880d681SAndroid Build Coastguard Worker   /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
72*9880d681SAndroid Build Coastguard Worker   /// SINT_TO_FLOAT and SHR on vectors isn't legal.
73*9880d681SAndroid Build Coastguard Worker   SDValue ExpandUINT_TO_FLOAT(SDValue Op);
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker   /// \brief Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
76*9880d681SAndroid Build Coastguard Worker   SDValue ExpandSEXTINREG(SDValue Op);
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   /// \brief Implement expansion for ANY_EXTEND_VECTOR_INREG.
79*9880d681SAndroid Build Coastguard Worker   ///
80*9880d681SAndroid Build Coastguard Worker   /// Shuffles the low lanes of the operand into place and bitcasts to the proper
81*9880d681SAndroid Build Coastguard Worker   /// type. The contents of the bits in the extended part of each element are
82*9880d681SAndroid Build Coastguard Worker   /// undef.
83*9880d681SAndroid Build Coastguard Worker   SDValue ExpandANY_EXTEND_VECTOR_INREG(SDValue Op);
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker   /// \brief Implement expansion for SIGN_EXTEND_VECTOR_INREG.
86*9880d681SAndroid Build Coastguard Worker   ///
87*9880d681SAndroid Build Coastguard Worker   /// Shuffles the low lanes of the operand into place, bitcasts to the proper
88*9880d681SAndroid Build Coastguard Worker   /// type, then shifts left and arithmetic shifts right to introduce a sign
89*9880d681SAndroid Build Coastguard Worker   /// extension.
90*9880d681SAndroid Build Coastguard Worker   SDValue ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op);
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker   /// \brief Implement expansion for ZERO_EXTEND_VECTOR_INREG.
93*9880d681SAndroid Build Coastguard Worker   ///
94*9880d681SAndroid Build Coastguard Worker   /// Shuffles the low lanes of the operand into place and blends zeros into
95*9880d681SAndroid Build Coastguard Worker   /// the remaining lanes, finally bitcasting to the proper type.
96*9880d681SAndroid Build Coastguard Worker   SDValue ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op);
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker   /// \brief Expand bswap of vectors into a shuffle if legal.
99*9880d681SAndroid Build Coastguard Worker   SDValue ExpandBSWAP(SDValue Op);
100*9880d681SAndroid Build Coastguard Worker 
101*9880d681SAndroid Build Coastguard Worker   /// \brief Implement vselect in terms of XOR, AND, OR when blend is not
102*9880d681SAndroid Build Coastguard Worker   /// supported by the target.
103*9880d681SAndroid Build Coastguard Worker   SDValue ExpandVSELECT(SDValue Op);
104*9880d681SAndroid Build Coastguard Worker   SDValue ExpandSELECT(SDValue Op);
105*9880d681SAndroid Build Coastguard Worker   SDValue ExpandLoad(SDValue Op);
106*9880d681SAndroid Build Coastguard Worker   SDValue ExpandStore(SDValue Op);
107*9880d681SAndroid Build Coastguard Worker   SDValue ExpandFNEG(SDValue Op);
108*9880d681SAndroid Build Coastguard Worker   SDValue ExpandBITREVERSE(SDValue Op);
109*9880d681SAndroid Build Coastguard Worker   SDValue ExpandCTLZ_CTTZ_ZERO_UNDEF(SDValue Op);
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker   /// \brief Implements vector promotion.
112*9880d681SAndroid Build Coastguard Worker   ///
113*9880d681SAndroid Build Coastguard Worker   /// This is essentially just bitcasting the operands to a different type and
114*9880d681SAndroid Build Coastguard Worker   /// bitcasting the result back to the original type.
115*9880d681SAndroid Build Coastguard Worker   SDValue Promote(SDValue Op);
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker   /// \brief Implements [SU]INT_TO_FP vector promotion.
118*9880d681SAndroid Build Coastguard Worker   ///
119*9880d681SAndroid Build Coastguard Worker   /// This is a [zs]ext of the input operand to the next size up.
120*9880d681SAndroid Build Coastguard Worker   SDValue PromoteINT_TO_FP(SDValue Op);
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   /// \brief Implements FP_TO_[SU]INT vector promotion of the result type.
123*9880d681SAndroid Build Coastguard Worker   ///
124*9880d681SAndroid Build Coastguard Worker   /// It is promoted to the next size up integer type.  The result is then
125*9880d681SAndroid Build Coastguard Worker   /// truncated back to the original type.
126*9880d681SAndroid Build Coastguard Worker   SDValue PromoteFP_TO_INT(SDValue Op, bool isSigned);
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker public:
129*9880d681SAndroid Build Coastguard Worker   /// \brief Begin legalizer the vector operations in the DAG.
130*9880d681SAndroid Build Coastguard Worker   bool Run();
VectorLegalizer(SelectionDAG & dag)131*9880d681SAndroid Build Coastguard Worker   VectorLegalizer(SelectionDAG& dag) :
132*9880d681SAndroid Build Coastguard Worker       DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
133*9880d681SAndroid Build Coastguard Worker };
134*9880d681SAndroid Build Coastguard Worker 
Run()135*9880d681SAndroid Build Coastguard Worker bool VectorLegalizer::Run() {
136*9880d681SAndroid Build Coastguard Worker   // Before we start legalizing vector nodes, check if there are any vectors.
137*9880d681SAndroid Build Coastguard Worker   bool HasVectors = false;
138*9880d681SAndroid Build Coastguard Worker   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
139*9880d681SAndroid Build Coastguard Worker        E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
140*9880d681SAndroid Build Coastguard Worker     // Check if the values of the nodes contain vectors. We don't need to check
141*9880d681SAndroid Build Coastguard Worker     // the operands because we are going to check their values at some point.
142*9880d681SAndroid Build Coastguard Worker     for (SDNode::value_iterator J = I->value_begin(), E = I->value_end();
143*9880d681SAndroid Build Coastguard Worker          J != E; ++J)
144*9880d681SAndroid Build Coastguard Worker       HasVectors |= J->isVector();
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker     // If we found a vector node we can start the legalization.
147*9880d681SAndroid Build Coastguard Worker     if (HasVectors)
148*9880d681SAndroid Build Coastguard Worker       break;
149*9880d681SAndroid Build Coastguard Worker   }
150*9880d681SAndroid Build Coastguard Worker 
151*9880d681SAndroid Build Coastguard Worker   // If this basic block has no vectors then no need to legalize vectors.
152*9880d681SAndroid Build Coastguard Worker   if (!HasVectors)
153*9880d681SAndroid Build Coastguard Worker     return false;
154*9880d681SAndroid Build Coastguard Worker 
155*9880d681SAndroid Build Coastguard Worker   // The legalize process is inherently a bottom-up recursive process (users
156*9880d681SAndroid Build Coastguard Worker   // legalize their uses before themselves).  Given infinite stack space, we
157*9880d681SAndroid Build Coastguard Worker   // could just start legalizing on the root and traverse the whole graph.  In
158*9880d681SAndroid Build Coastguard Worker   // practice however, this causes us to run out of stack space on large basic
159*9880d681SAndroid Build Coastguard Worker   // blocks.  To avoid this problem, compute an ordering of the nodes where each
160*9880d681SAndroid Build Coastguard Worker   // node is only legalized after all of its operands are legalized.
161*9880d681SAndroid Build Coastguard Worker   DAG.AssignTopologicalOrder();
162*9880d681SAndroid Build Coastguard Worker   for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
163*9880d681SAndroid Build Coastguard Worker        E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
164*9880d681SAndroid Build Coastguard Worker     LegalizeOp(SDValue(&*I, 0));
165*9880d681SAndroid Build Coastguard Worker 
166*9880d681SAndroid Build Coastguard Worker   // Finally, it's possible the root changed.  Get the new root.
167*9880d681SAndroid Build Coastguard Worker   SDValue OldRoot = DAG.getRoot();
168*9880d681SAndroid Build Coastguard Worker   assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
169*9880d681SAndroid Build Coastguard Worker   DAG.setRoot(LegalizedNodes[OldRoot]);
170*9880d681SAndroid Build Coastguard Worker 
171*9880d681SAndroid Build Coastguard Worker   LegalizedNodes.clear();
172*9880d681SAndroid Build Coastguard Worker 
173*9880d681SAndroid Build Coastguard Worker   // Remove dead nodes now.
174*9880d681SAndroid Build Coastguard Worker   DAG.RemoveDeadNodes();
175*9880d681SAndroid Build Coastguard Worker 
176*9880d681SAndroid Build Coastguard Worker   return Changed;
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker 
TranslateLegalizeResults(SDValue Op,SDValue Result)179*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
180*9880d681SAndroid Build Coastguard Worker   // Generic legalization: just pass the operand through.
181*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
182*9880d681SAndroid Build Coastguard Worker     AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
183*9880d681SAndroid Build Coastguard Worker   return Result.getValue(Op.getResNo());
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker 
LegalizeOp(SDValue Op)186*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
187*9880d681SAndroid Build Coastguard Worker   // Note that LegalizeOp may be reentered even from single-use nodes, which
188*9880d681SAndroid Build Coastguard Worker   // means that we always must cache transformed nodes.
189*9880d681SAndroid Build Coastguard Worker   DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
190*9880d681SAndroid Build Coastguard Worker   if (I != LegalizedNodes.end()) return I->second;
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker   SDNode* Node = Op.getNode();
193*9880d681SAndroid Build Coastguard Worker 
194*9880d681SAndroid Build Coastguard Worker   // Legalize the operands
195*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 8> Ops;
196*9880d681SAndroid Build Coastguard Worker   for (const SDValue &Op : Node->op_values())
197*9880d681SAndroid Build Coastguard Worker     Ops.push_back(LegalizeOp(Op));
198*9880d681SAndroid Build Coastguard Worker 
199*9880d681SAndroid Build Coastguard Worker   SDValue Result = SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops), 0);
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker   bool HasVectorValue = false;
202*9880d681SAndroid Build Coastguard Worker   if (Op.getOpcode() == ISD::LOAD) {
203*9880d681SAndroid Build Coastguard Worker     LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
204*9880d681SAndroid Build Coastguard Worker     ISD::LoadExtType ExtType = LD->getExtensionType();
205*9880d681SAndroid Build Coastguard Worker     if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD)
206*9880d681SAndroid Build Coastguard Worker       switch (TLI.getLoadExtAction(LD->getExtensionType(), LD->getValueType(0),
207*9880d681SAndroid Build Coastguard Worker                                    LD->getMemoryVT())) {
208*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("This action is not supported yet!");
209*9880d681SAndroid Build Coastguard Worker       case TargetLowering::Legal:
210*9880d681SAndroid Build Coastguard Worker         return TranslateLegalizeResults(Op, Result);
211*9880d681SAndroid Build Coastguard Worker       case TargetLowering::Custom:
212*9880d681SAndroid Build Coastguard Worker         if (SDValue Lowered = TLI.LowerOperation(Result, DAG)) {
213*9880d681SAndroid Build Coastguard Worker           if (Lowered == Result)
214*9880d681SAndroid Build Coastguard Worker             return TranslateLegalizeResults(Op, Lowered);
215*9880d681SAndroid Build Coastguard Worker           Changed = true;
216*9880d681SAndroid Build Coastguard Worker           if (Lowered->getNumValues() != Op->getNumValues()) {
217*9880d681SAndroid Build Coastguard Worker             // This expanded to something other than the load. Assume the
218*9880d681SAndroid Build Coastguard Worker             // lowering code took care of any chain values, and just handle the
219*9880d681SAndroid Build Coastguard Worker             // returned value.
220*9880d681SAndroid Build Coastguard Worker             assert(Result.getValue(1).use_empty() &&
221*9880d681SAndroid Build Coastguard Worker                    "There are still live users of the old chain!");
222*9880d681SAndroid Build Coastguard Worker             return LegalizeOp(Lowered);
223*9880d681SAndroid Build Coastguard Worker           }
224*9880d681SAndroid Build Coastguard Worker           return TranslateLegalizeResults(Op, Lowered);
225*9880d681SAndroid Build Coastguard Worker         }
226*9880d681SAndroid Build Coastguard Worker       case TargetLowering::Expand:
227*9880d681SAndroid Build Coastguard Worker         Changed = true;
228*9880d681SAndroid Build Coastguard Worker         return LegalizeOp(ExpandLoad(Op));
229*9880d681SAndroid Build Coastguard Worker       }
230*9880d681SAndroid Build Coastguard Worker   } else if (Op.getOpcode() == ISD::STORE) {
231*9880d681SAndroid Build Coastguard Worker     StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
232*9880d681SAndroid Build Coastguard Worker     EVT StVT = ST->getMemoryVT();
233*9880d681SAndroid Build Coastguard Worker     MVT ValVT = ST->getValue().getSimpleValueType();
234*9880d681SAndroid Build Coastguard Worker     if (StVT.isVector() && ST->isTruncatingStore())
235*9880d681SAndroid Build Coastguard Worker       switch (TLI.getTruncStoreAction(ValVT, StVT)) {
236*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("This action is not supported yet!");
237*9880d681SAndroid Build Coastguard Worker       case TargetLowering::Legal:
238*9880d681SAndroid Build Coastguard Worker         return TranslateLegalizeResults(Op, Result);
239*9880d681SAndroid Build Coastguard Worker       case TargetLowering::Custom: {
240*9880d681SAndroid Build Coastguard Worker         SDValue Lowered = TLI.LowerOperation(Result, DAG);
241*9880d681SAndroid Build Coastguard Worker         Changed = Lowered != Result;
242*9880d681SAndroid Build Coastguard Worker         return TranslateLegalizeResults(Op, Lowered);
243*9880d681SAndroid Build Coastguard Worker       }
244*9880d681SAndroid Build Coastguard Worker       case TargetLowering::Expand:
245*9880d681SAndroid Build Coastguard Worker         Changed = true;
246*9880d681SAndroid Build Coastguard Worker         return LegalizeOp(ExpandStore(Op));
247*9880d681SAndroid Build Coastguard Worker       }
248*9880d681SAndroid Build Coastguard Worker   } else if (Op.getOpcode() == ISD::MSCATTER || Op.getOpcode() == ISD::MSTORE)
249*9880d681SAndroid Build Coastguard Worker     HasVectorValue = true;
250*9880d681SAndroid Build Coastguard Worker 
251*9880d681SAndroid Build Coastguard Worker   for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
252*9880d681SAndroid Build Coastguard Worker        J != E;
253*9880d681SAndroid Build Coastguard Worker        ++J)
254*9880d681SAndroid Build Coastguard Worker     HasVectorValue |= J->isVector();
255*9880d681SAndroid Build Coastguard Worker   if (!HasVectorValue)
256*9880d681SAndroid Build Coastguard Worker     return TranslateLegalizeResults(Op, Result);
257*9880d681SAndroid Build Coastguard Worker 
258*9880d681SAndroid Build Coastguard Worker   EVT QueryType;
259*9880d681SAndroid Build Coastguard Worker   switch (Op.getOpcode()) {
260*9880d681SAndroid Build Coastguard Worker   default:
261*9880d681SAndroid Build Coastguard Worker     return TranslateLegalizeResults(Op, Result);
262*9880d681SAndroid Build Coastguard Worker   case ISD::ADD:
263*9880d681SAndroid Build Coastguard Worker   case ISD::SUB:
264*9880d681SAndroid Build Coastguard Worker   case ISD::MUL:
265*9880d681SAndroid Build Coastguard Worker   case ISD::SDIV:
266*9880d681SAndroid Build Coastguard Worker   case ISD::UDIV:
267*9880d681SAndroid Build Coastguard Worker   case ISD::SREM:
268*9880d681SAndroid Build Coastguard Worker   case ISD::UREM:
269*9880d681SAndroid Build Coastguard Worker   case ISD::SDIVREM:
270*9880d681SAndroid Build Coastguard Worker   case ISD::UDIVREM:
271*9880d681SAndroid Build Coastguard Worker   case ISD::FADD:
272*9880d681SAndroid Build Coastguard Worker   case ISD::FSUB:
273*9880d681SAndroid Build Coastguard Worker   case ISD::FMUL:
274*9880d681SAndroid Build Coastguard Worker   case ISD::FDIV:
275*9880d681SAndroid Build Coastguard Worker   case ISD::FREM:
276*9880d681SAndroid Build Coastguard Worker   case ISD::AND:
277*9880d681SAndroid Build Coastguard Worker   case ISD::OR:
278*9880d681SAndroid Build Coastguard Worker   case ISD::XOR:
279*9880d681SAndroid Build Coastguard Worker   case ISD::SHL:
280*9880d681SAndroid Build Coastguard Worker   case ISD::SRA:
281*9880d681SAndroid Build Coastguard Worker   case ISD::SRL:
282*9880d681SAndroid Build Coastguard Worker   case ISD::ROTL:
283*9880d681SAndroid Build Coastguard Worker   case ISD::ROTR:
284*9880d681SAndroid Build Coastguard Worker   case ISD::BSWAP:
285*9880d681SAndroid Build Coastguard Worker   case ISD::BITREVERSE:
286*9880d681SAndroid Build Coastguard Worker   case ISD::CTLZ:
287*9880d681SAndroid Build Coastguard Worker   case ISD::CTTZ:
288*9880d681SAndroid Build Coastguard Worker   case ISD::CTLZ_ZERO_UNDEF:
289*9880d681SAndroid Build Coastguard Worker   case ISD::CTTZ_ZERO_UNDEF:
290*9880d681SAndroid Build Coastguard Worker   case ISD::CTPOP:
291*9880d681SAndroid Build Coastguard Worker   case ISD::SELECT:
292*9880d681SAndroid Build Coastguard Worker   case ISD::VSELECT:
293*9880d681SAndroid Build Coastguard Worker   case ISD::SELECT_CC:
294*9880d681SAndroid Build Coastguard Worker   case ISD::SETCC:
295*9880d681SAndroid Build Coastguard Worker   case ISD::ZERO_EXTEND:
296*9880d681SAndroid Build Coastguard Worker   case ISD::ANY_EXTEND:
297*9880d681SAndroid Build Coastguard Worker   case ISD::TRUNCATE:
298*9880d681SAndroid Build Coastguard Worker   case ISD::SIGN_EXTEND:
299*9880d681SAndroid Build Coastguard Worker   case ISD::FP_TO_SINT:
300*9880d681SAndroid Build Coastguard Worker   case ISD::FP_TO_UINT:
301*9880d681SAndroid Build Coastguard Worker   case ISD::FNEG:
302*9880d681SAndroid Build Coastguard Worker   case ISD::FABS:
303*9880d681SAndroid Build Coastguard Worker   case ISD::FMINNUM:
304*9880d681SAndroid Build Coastguard Worker   case ISD::FMAXNUM:
305*9880d681SAndroid Build Coastguard Worker   case ISD::FMINNAN:
306*9880d681SAndroid Build Coastguard Worker   case ISD::FMAXNAN:
307*9880d681SAndroid Build Coastguard Worker   case ISD::FCOPYSIGN:
308*9880d681SAndroid Build Coastguard Worker   case ISD::FSQRT:
309*9880d681SAndroid Build Coastguard Worker   case ISD::FSIN:
310*9880d681SAndroid Build Coastguard Worker   case ISD::FCOS:
311*9880d681SAndroid Build Coastguard Worker   case ISD::FPOWI:
312*9880d681SAndroid Build Coastguard Worker   case ISD::FPOW:
313*9880d681SAndroid Build Coastguard Worker   case ISD::FLOG:
314*9880d681SAndroid Build Coastguard Worker   case ISD::FLOG2:
315*9880d681SAndroid Build Coastguard Worker   case ISD::FLOG10:
316*9880d681SAndroid Build Coastguard Worker   case ISD::FEXP:
317*9880d681SAndroid Build Coastguard Worker   case ISD::FEXP2:
318*9880d681SAndroid Build Coastguard Worker   case ISD::FCEIL:
319*9880d681SAndroid Build Coastguard Worker   case ISD::FTRUNC:
320*9880d681SAndroid Build Coastguard Worker   case ISD::FRINT:
321*9880d681SAndroid Build Coastguard Worker   case ISD::FNEARBYINT:
322*9880d681SAndroid Build Coastguard Worker   case ISD::FROUND:
323*9880d681SAndroid Build Coastguard Worker   case ISD::FFLOOR:
324*9880d681SAndroid Build Coastguard Worker   case ISD::FP_ROUND:
325*9880d681SAndroid Build Coastguard Worker   case ISD::FP_EXTEND:
326*9880d681SAndroid Build Coastguard Worker   case ISD::FMA:
327*9880d681SAndroid Build Coastguard Worker   case ISD::SIGN_EXTEND_INREG:
328*9880d681SAndroid Build Coastguard Worker   case ISD::ANY_EXTEND_VECTOR_INREG:
329*9880d681SAndroid Build Coastguard Worker   case ISD::SIGN_EXTEND_VECTOR_INREG:
330*9880d681SAndroid Build Coastguard Worker   case ISD::ZERO_EXTEND_VECTOR_INREG:
331*9880d681SAndroid Build Coastguard Worker   case ISD::SMIN:
332*9880d681SAndroid Build Coastguard Worker   case ISD::SMAX:
333*9880d681SAndroid Build Coastguard Worker   case ISD::UMIN:
334*9880d681SAndroid Build Coastguard Worker   case ISD::UMAX:
335*9880d681SAndroid Build Coastguard Worker     QueryType = Node->getValueType(0);
336*9880d681SAndroid Build Coastguard Worker     break;
337*9880d681SAndroid Build Coastguard Worker   case ISD::FP_ROUND_INREG:
338*9880d681SAndroid Build Coastguard Worker     QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
339*9880d681SAndroid Build Coastguard Worker     break;
340*9880d681SAndroid Build Coastguard Worker   case ISD::SINT_TO_FP:
341*9880d681SAndroid Build Coastguard Worker   case ISD::UINT_TO_FP:
342*9880d681SAndroid Build Coastguard Worker     QueryType = Node->getOperand(0).getValueType();
343*9880d681SAndroid Build Coastguard Worker     break;
344*9880d681SAndroid Build Coastguard Worker   case ISD::MSCATTER:
345*9880d681SAndroid Build Coastguard Worker     QueryType = cast<MaskedScatterSDNode>(Node)->getValue().getValueType();
346*9880d681SAndroid Build Coastguard Worker     break;
347*9880d681SAndroid Build Coastguard Worker   case ISD::MSTORE:
348*9880d681SAndroid Build Coastguard Worker     QueryType = cast<MaskedStoreSDNode>(Node)->getValue().getValueType();
349*9880d681SAndroid Build Coastguard Worker     break;
350*9880d681SAndroid Build Coastguard Worker   }
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker   switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
353*9880d681SAndroid Build Coastguard Worker   default: llvm_unreachable("This action is not supported yet!");
354*9880d681SAndroid Build Coastguard Worker   case TargetLowering::Promote:
355*9880d681SAndroid Build Coastguard Worker     Result = Promote(Op);
356*9880d681SAndroid Build Coastguard Worker     Changed = true;
357*9880d681SAndroid Build Coastguard Worker     break;
358*9880d681SAndroid Build Coastguard Worker   case TargetLowering::Legal:
359*9880d681SAndroid Build Coastguard Worker     break;
360*9880d681SAndroid Build Coastguard Worker   case TargetLowering::Custom: {
361*9880d681SAndroid Build Coastguard Worker     if (SDValue Tmp1 = TLI.LowerOperation(Op, DAG)) {
362*9880d681SAndroid Build Coastguard Worker       Result = Tmp1;
363*9880d681SAndroid Build Coastguard Worker       break;
364*9880d681SAndroid Build Coastguard Worker     }
365*9880d681SAndroid Build Coastguard Worker     // FALL THROUGH
366*9880d681SAndroid Build Coastguard Worker   }
367*9880d681SAndroid Build Coastguard Worker   case TargetLowering::Expand:
368*9880d681SAndroid Build Coastguard Worker     Result = Expand(Op);
369*9880d681SAndroid Build Coastguard Worker   }
370*9880d681SAndroid Build Coastguard Worker 
371*9880d681SAndroid Build Coastguard Worker   // Make sure that the generated code is itself legal.
372*9880d681SAndroid Build Coastguard Worker   if (Result != Op) {
373*9880d681SAndroid Build Coastguard Worker     Result = LegalizeOp(Result);
374*9880d681SAndroid Build Coastguard Worker     Changed = true;
375*9880d681SAndroid Build Coastguard Worker   }
376*9880d681SAndroid Build Coastguard Worker 
377*9880d681SAndroid Build Coastguard Worker   // Note that LegalizeOp may be reentered even from single-use nodes, which
378*9880d681SAndroid Build Coastguard Worker   // means that we always must cache transformed nodes.
379*9880d681SAndroid Build Coastguard Worker   AddLegalizedOperand(Op, Result);
380*9880d681SAndroid Build Coastguard Worker   return Result;
381*9880d681SAndroid Build Coastguard Worker }
382*9880d681SAndroid Build Coastguard Worker 
Promote(SDValue Op)383*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::Promote(SDValue Op) {
384*9880d681SAndroid Build Coastguard Worker   // For a few operations there is a specific concept for promotion based on
385*9880d681SAndroid Build Coastguard Worker   // the operand's type.
386*9880d681SAndroid Build Coastguard Worker   switch (Op.getOpcode()) {
387*9880d681SAndroid Build Coastguard Worker   case ISD::SINT_TO_FP:
388*9880d681SAndroid Build Coastguard Worker   case ISD::UINT_TO_FP:
389*9880d681SAndroid Build Coastguard Worker     // "Promote" the operation by extending the operand.
390*9880d681SAndroid Build Coastguard Worker     return PromoteINT_TO_FP(Op);
391*9880d681SAndroid Build Coastguard Worker   case ISD::FP_TO_UINT:
392*9880d681SAndroid Build Coastguard Worker   case ISD::FP_TO_SINT:
393*9880d681SAndroid Build Coastguard Worker     // Promote the operation by extending the operand.
394*9880d681SAndroid Build Coastguard Worker     return PromoteFP_TO_INT(Op, Op->getOpcode() == ISD::FP_TO_SINT);
395*9880d681SAndroid Build Coastguard Worker   }
396*9880d681SAndroid Build Coastguard Worker 
397*9880d681SAndroid Build Coastguard Worker   // There are currently two cases of vector promotion:
398*9880d681SAndroid Build Coastguard Worker   // 1) Bitcasting a vector of integers to a different type to a vector of the
399*9880d681SAndroid Build Coastguard Worker   //    same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64.
400*9880d681SAndroid Build Coastguard Worker   // 2) Extending a vector of floats to a vector of the same number of larger
401*9880d681SAndroid Build Coastguard Worker   //    floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32.
402*9880d681SAndroid Build Coastguard Worker   MVT VT = Op.getSimpleValueType();
403*9880d681SAndroid Build Coastguard Worker   assert(Op.getNode()->getNumValues() == 1 &&
404*9880d681SAndroid Build Coastguard Worker          "Can't promote a vector with multiple results!");
405*9880d681SAndroid Build Coastguard Worker   MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
406*9880d681SAndroid Build Coastguard Worker   SDLoc dl(Op);
407*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 4> Operands(Op.getNumOperands());
408*9880d681SAndroid Build Coastguard Worker 
409*9880d681SAndroid Build Coastguard Worker   for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
410*9880d681SAndroid Build Coastguard Worker     if (Op.getOperand(j).getValueType().isVector())
411*9880d681SAndroid Build Coastguard Worker       if (Op.getOperand(j)
412*9880d681SAndroid Build Coastguard Worker               .getValueType()
413*9880d681SAndroid Build Coastguard Worker               .getVectorElementType()
414*9880d681SAndroid Build Coastguard Worker               .isFloatingPoint() &&
415*9880d681SAndroid Build Coastguard Worker           NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())
416*9880d681SAndroid Build Coastguard Worker         Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Op.getOperand(j));
417*9880d681SAndroid Build Coastguard Worker       else
418*9880d681SAndroid Build Coastguard Worker         Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
419*9880d681SAndroid Build Coastguard Worker     else
420*9880d681SAndroid Build Coastguard Worker       Operands[j] = Op.getOperand(j);
421*9880d681SAndroid Build Coastguard Worker   }
422*9880d681SAndroid Build Coastguard Worker 
423*9880d681SAndroid Build Coastguard Worker   Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands, Op.getNode()->getFlags());
424*9880d681SAndroid Build Coastguard Worker   if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) ||
425*9880d681SAndroid Build Coastguard Worker       (VT.isVector() && VT.getVectorElementType().isFloatingPoint() &&
426*9880d681SAndroid Build Coastguard Worker        NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()))
427*9880d681SAndroid Build Coastguard Worker     return DAG.getNode(ISD::FP_ROUND, dl, VT, Op, DAG.getIntPtrConstant(0, dl));
428*9880d681SAndroid Build Coastguard Worker   else
429*9880d681SAndroid Build Coastguard Worker     return DAG.getNode(ISD::BITCAST, dl, VT, Op);
430*9880d681SAndroid Build Coastguard Worker }
431*9880d681SAndroid Build Coastguard Worker 
PromoteINT_TO_FP(SDValue Op)432*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) {
433*9880d681SAndroid Build Coastguard Worker   // INT_TO_FP operations may require the input operand be promoted even
434*9880d681SAndroid Build Coastguard Worker   // when the type is otherwise legal.
435*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getOperand(0).getValueType();
436*9880d681SAndroid Build Coastguard Worker   assert(Op.getNode()->getNumValues() == 1 &&
437*9880d681SAndroid Build Coastguard Worker          "Can't promote a vector with multiple results!");
438*9880d681SAndroid Build Coastguard Worker 
439*9880d681SAndroid Build Coastguard Worker   // Normal getTypeToPromoteTo() doesn't work here, as that will promote
440*9880d681SAndroid Build Coastguard Worker   // by widening the vector w/ the same element width and twice the number
441*9880d681SAndroid Build Coastguard Worker   // of elements. We want the other way around, the same number of elements,
442*9880d681SAndroid Build Coastguard Worker   // each twice the width.
443*9880d681SAndroid Build Coastguard Worker   //
444*9880d681SAndroid Build Coastguard Worker   // Increase the bitwidth of the element to the next pow-of-two
445*9880d681SAndroid Build Coastguard Worker   // (which is greater than 8 bits).
446*9880d681SAndroid Build Coastguard Worker 
447*9880d681SAndroid Build Coastguard Worker   EVT NVT = VT.widenIntegerVectorElementType(*DAG.getContext());
448*9880d681SAndroid Build Coastguard Worker   assert(NVT.isSimple() && "Promoting to a non-simple vector type!");
449*9880d681SAndroid Build Coastguard Worker   SDLoc dl(Op);
450*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 4> Operands(Op.getNumOperands());
451*9880d681SAndroid Build Coastguard Worker 
452*9880d681SAndroid Build Coastguard Worker   unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
453*9880d681SAndroid Build Coastguard Worker     ISD::SIGN_EXTEND;
454*9880d681SAndroid Build Coastguard Worker   for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
455*9880d681SAndroid Build Coastguard Worker     if (Op.getOperand(j).getValueType().isVector())
456*9880d681SAndroid Build Coastguard Worker       Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
457*9880d681SAndroid Build Coastguard Worker     else
458*9880d681SAndroid Build Coastguard Worker       Operands[j] = Op.getOperand(j);
459*9880d681SAndroid Build Coastguard Worker   }
460*9880d681SAndroid Build Coastguard Worker 
461*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands);
462*9880d681SAndroid Build Coastguard Worker }
463*9880d681SAndroid Build Coastguard Worker 
464*9880d681SAndroid Build Coastguard Worker // For FP_TO_INT we promote the result type to a vector type with wider
465*9880d681SAndroid Build Coastguard Worker // elements and then truncate the result.  This is different from the default
466*9880d681SAndroid Build Coastguard Worker // PromoteVector which uses bitcast to promote thus assumning that the
467*9880d681SAndroid Build Coastguard Worker // promoted vector type has the same overall size.
PromoteFP_TO_INT(SDValue Op,bool isSigned)468*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op, bool isSigned) {
469*9880d681SAndroid Build Coastguard Worker   assert(Op.getNode()->getNumValues() == 1 &&
470*9880d681SAndroid Build Coastguard Worker          "Can't promote a vector with multiple results!");
471*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
472*9880d681SAndroid Build Coastguard Worker 
473*9880d681SAndroid Build Coastguard Worker   EVT NewVT;
474*9880d681SAndroid Build Coastguard Worker   unsigned NewOpc;
475*9880d681SAndroid Build Coastguard Worker   while (1) {
476*9880d681SAndroid Build Coastguard Worker     NewVT = VT.widenIntegerVectorElementType(*DAG.getContext());
477*9880d681SAndroid Build Coastguard Worker     assert(NewVT.isSimple() && "Promoting to a non-simple vector type!");
478*9880d681SAndroid Build Coastguard Worker     if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewVT)) {
479*9880d681SAndroid Build Coastguard Worker       NewOpc = ISD::FP_TO_SINT;
480*9880d681SAndroid Build Coastguard Worker       break;
481*9880d681SAndroid Build Coastguard Worker     }
482*9880d681SAndroid Build Coastguard Worker     if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewVT)) {
483*9880d681SAndroid Build Coastguard Worker       NewOpc = ISD::FP_TO_UINT;
484*9880d681SAndroid Build Coastguard Worker       break;
485*9880d681SAndroid Build Coastguard Worker     }
486*9880d681SAndroid Build Coastguard Worker   }
487*9880d681SAndroid Build Coastguard Worker 
488*9880d681SAndroid Build Coastguard Worker   SDLoc loc(Op);
489*9880d681SAndroid Build Coastguard Worker   SDValue promoted  = DAG.getNode(NewOpc, SDLoc(Op), NewVT, Op.getOperand(0));
490*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT, promoted);
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker 
493*9880d681SAndroid Build Coastguard Worker 
ExpandLoad(SDValue Op)494*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
495*9880d681SAndroid Build Coastguard Worker   LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
496*9880d681SAndroid Build Coastguard Worker 
497*9880d681SAndroid Build Coastguard Worker   EVT SrcVT = LD->getMemoryVT();
498*9880d681SAndroid Build Coastguard Worker   EVT SrcEltVT = SrcVT.getScalarType();
499*9880d681SAndroid Build Coastguard Worker   unsigned NumElem = SrcVT.getVectorNumElements();
500*9880d681SAndroid Build Coastguard Worker 
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker   SDValue NewChain;
503*9880d681SAndroid Build Coastguard Worker   SDValue Value;
504*9880d681SAndroid Build Coastguard Worker   if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) {
505*9880d681SAndroid Build Coastguard Worker     SDLoc dl(Op);
506*9880d681SAndroid Build Coastguard Worker 
507*9880d681SAndroid Build Coastguard Worker     SmallVector<SDValue, 8> Vals;
508*9880d681SAndroid Build Coastguard Worker     SmallVector<SDValue, 8> LoadChains;
509*9880d681SAndroid Build Coastguard Worker 
510*9880d681SAndroid Build Coastguard Worker     EVT DstEltVT = LD->getValueType(0).getScalarType();
511*9880d681SAndroid Build Coastguard Worker     SDValue Chain = LD->getChain();
512*9880d681SAndroid Build Coastguard Worker     SDValue BasePTR = LD->getBasePtr();
513*9880d681SAndroid Build Coastguard Worker     ISD::LoadExtType ExtType = LD->getExtensionType();
514*9880d681SAndroid Build Coastguard Worker 
515*9880d681SAndroid Build Coastguard Worker     // When elements in a vector is not byte-addressable, we cannot directly
516*9880d681SAndroid Build Coastguard Worker     // load each element by advancing pointer, which could only address bytes.
517*9880d681SAndroid Build Coastguard Worker     // Instead, we load all significant words, mask bits off, and concatenate
518*9880d681SAndroid Build Coastguard Worker     // them to form each element. Finally, they are extended to destination
519*9880d681SAndroid Build Coastguard Worker     // scalar type to build the destination vector.
520*9880d681SAndroid Build Coastguard Worker     EVT WideVT = TLI.getPointerTy(DAG.getDataLayout());
521*9880d681SAndroid Build Coastguard Worker 
522*9880d681SAndroid Build Coastguard Worker     assert(WideVT.isRound() &&
523*9880d681SAndroid Build Coastguard Worker            "Could not handle the sophisticated case when the widest integer is"
524*9880d681SAndroid Build Coastguard Worker            " not power of 2.");
525*9880d681SAndroid Build Coastguard Worker     assert(WideVT.bitsGE(SrcEltVT) &&
526*9880d681SAndroid Build Coastguard Worker            "Type is not legalized?");
527*9880d681SAndroid Build Coastguard Worker 
528*9880d681SAndroid Build Coastguard Worker     unsigned WideBytes = WideVT.getStoreSize();
529*9880d681SAndroid Build Coastguard Worker     unsigned Offset = 0;
530*9880d681SAndroid Build Coastguard Worker     unsigned RemainingBytes = SrcVT.getStoreSize();
531*9880d681SAndroid Build Coastguard Worker     SmallVector<SDValue, 8> LoadVals;
532*9880d681SAndroid Build Coastguard Worker 
533*9880d681SAndroid Build Coastguard Worker     while (RemainingBytes > 0) {
534*9880d681SAndroid Build Coastguard Worker       SDValue ScalarLoad;
535*9880d681SAndroid Build Coastguard Worker       unsigned LoadBytes = WideBytes;
536*9880d681SAndroid Build Coastguard Worker 
537*9880d681SAndroid Build Coastguard Worker       if (RemainingBytes >= LoadBytes) {
538*9880d681SAndroid Build Coastguard Worker         ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR,
539*9880d681SAndroid Build Coastguard Worker                                  LD->getPointerInfo().getWithOffset(Offset),
540*9880d681SAndroid Build Coastguard Worker                                  LD->isVolatile(), LD->isNonTemporal(),
541*9880d681SAndroid Build Coastguard Worker                                  LD->isInvariant(),
542*9880d681SAndroid Build Coastguard Worker                                  MinAlign(LD->getAlignment(), Offset),
543*9880d681SAndroid Build Coastguard Worker                                  LD->getAAInfo());
544*9880d681SAndroid Build Coastguard Worker       } else {
545*9880d681SAndroid Build Coastguard Worker         EVT LoadVT = WideVT;
546*9880d681SAndroid Build Coastguard Worker         while (RemainingBytes < LoadBytes) {
547*9880d681SAndroid Build Coastguard Worker           LoadBytes >>= 1; // Reduce the load size by half.
548*9880d681SAndroid Build Coastguard Worker           LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3);
549*9880d681SAndroid Build Coastguard Worker         }
550*9880d681SAndroid Build Coastguard Worker         ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
551*9880d681SAndroid Build Coastguard Worker                                     LD->getPointerInfo().getWithOffset(Offset),
552*9880d681SAndroid Build Coastguard Worker                                     LoadVT, LD->isVolatile(),
553*9880d681SAndroid Build Coastguard Worker                                     LD->isNonTemporal(), LD->isInvariant(),
554*9880d681SAndroid Build Coastguard Worker                                     MinAlign(LD->getAlignment(), Offset),
555*9880d681SAndroid Build Coastguard Worker                                     LD->getAAInfo());
556*9880d681SAndroid Build Coastguard Worker       }
557*9880d681SAndroid Build Coastguard Worker 
558*9880d681SAndroid Build Coastguard Worker       RemainingBytes -= LoadBytes;
559*9880d681SAndroid Build Coastguard Worker       Offset += LoadBytes;
560*9880d681SAndroid Build Coastguard Worker       BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
561*9880d681SAndroid Build Coastguard Worker                             DAG.getConstant(LoadBytes, dl,
562*9880d681SAndroid Build Coastguard Worker                                             BasePTR.getValueType()));
563*9880d681SAndroid Build Coastguard Worker 
564*9880d681SAndroid Build Coastguard Worker       LoadVals.push_back(ScalarLoad.getValue(0));
565*9880d681SAndroid Build Coastguard Worker       LoadChains.push_back(ScalarLoad.getValue(1));
566*9880d681SAndroid Build Coastguard Worker     }
567*9880d681SAndroid Build Coastguard Worker 
568*9880d681SAndroid Build Coastguard Worker     // Extract bits, pack and extend/trunc them into destination type.
569*9880d681SAndroid Build Coastguard Worker     unsigned SrcEltBits = SrcEltVT.getSizeInBits();
570*9880d681SAndroid Build Coastguard Worker     SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, dl, WideVT);
571*9880d681SAndroid Build Coastguard Worker 
572*9880d681SAndroid Build Coastguard Worker     unsigned BitOffset = 0;
573*9880d681SAndroid Build Coastguard Worker     unsigned WideIdx = 0;
574*9880d681SAndroid Build Coastguard Worker     unsigned WideBits = WideVT.getSizeInBits();
575*9880d681SAndroid Build Coastguard Worker 
576*9880d681SAndroid Build Coastguard Worker     for (unsigned Idx = 0; Idx != NumElem; ++Idx) {
577*9880d681SAndroid Build Coastguard Worker       SDValue Lo, Hi, ShAmt;
578*9880d681SAndroid Build Coastguard Worker 
579*9880d681SAndroid Build Coastguard Worker       if (BitOffset < WideBits) {
580*9880d681SAndroid Build Coastguard Worker         ShAmt = DAG.getConstant(
581*9880d681SAndroid Build Coastguard Worker             BitOffset, dl, TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
582*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt);
583*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask);
584*9880d681SAndroid Build Coastguard Worker       }
585*9880d681SAndroid Build Coastguard Worker 
586*9880d681SAndroid Build Coastguard Worker       BitOffset += SrcEltBits;
587*9880d681SAndroid Build Coastguard Worker       if (BitOffset >= WideBits) {
588*9880d681SAndroid Build Coastguard Worker         WideIdx++;
589*9880d681SAndroid Build Coastguard Worker         BitOffset -= WideBits;
590*9880d681SAndroid Build Coastguard Worker         if (BitOffset > 0) {
591*9880d681SAndroid Build Coastguard Worker           ShAmt = DAG.getConstant(
592*9880d681SAndroid Build Coastguard Worker               SrcEltBits - BitOffset, dl,
593*9880d681SAndroid Build Coastguard Worker               TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
594*9880d681SAndroid Build Coastguard Worker           Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt);
595*9880d681SAndroid Build Coastguard Worker           Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask);
596*9880d681SAndroid Build Coastguard Worker         }
597*9880d681SAndroid Build Coastguard Worker       }
598*9880d681SAndroid Build Coastguard Worker 
599*9880d681SAndroid Build Coastguard Worker       if (Hi.getNode())
600*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi);
601*9880d681SAndroid Build Coastguard Worker 
602*9880d681SAndroid Build Coastguard Worker       switch (ExtType) {
603*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Unknown extended-load op!");
604*9880d681SAndroid Build Coastguard Worker       case ISD::EXTLOAD:
605*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
606*9880d681SAndroid Build Coastguard Worker         break;
607*9880d681SAndroid Build Coastguard Worker       case ISD::ZEXTLOAD:
608*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
609*9880d681SAndroid Build Coastguard Worker         break;
610*9880d681SAndroid Build Coastguard Worker       case ISD::SEXTLOAD:
611*9880d681SAndroid Build Coastguard Worker         ShAmt =
612*9880d681SAndroid Build Coastguard Worker             DAG.getConstant(WideBits - SrcEltBits, dl,
613*9880d681SAndroid Build Coastguard Worker                             TLI.getShiftAmountTy(WideVT, DAG.getDataLayout()));
614*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt);
615*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt);
616*9880d681SAndroid Build Coastguard Worker         Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
617*9880d681SAndroid Build Coastguard Worker         break;
618*9880d681SAndroid Build Coastguard Worker       }
619*9880d681SAndroid Build Coastguard Worker       Vals.push_back(Lo);
620*9880d681SAndroid Build Coastguard Worker     }
621*9880d681SAndroid Build Coastguard Worker 
622*9880d681SAndroid Build Coastguard Worker     NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
623*9880d681SAndroid Build Coastguard Worker     Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
624*9880d681SAndroid Build Coastguard Worker                         Op.getNode()->getValueType(0), Vals);
625*9880d681SAndroid Build Coastguard Worker   } else {
626*9880d681SAndroid Build Coastguard Worker     SDValue Scalarized = TLI.scalarizeVectorLoad(LD, DAG);
627*9880d681SAndroid Build Coastguard Worker 
628*9880d681SAndroid Build Coastguard Worker     NewChain = Scalarized.getValue(1);
629*9880d681SAndroid Build Coastguard Worker     Value = Scalarized.getValue(0);
630*9880d681SAndroid Build Coastguard Worker   }
631*9880d681SAndroid Build Coastguard Worker 
632*9880d681SAndroid Build Coastguard Worker   AddLegalizedOperand(Op.getValue(0), Value);
633*9880d681SAndroid Build Coastguard Worker   AddLegalizedOperand(Op.getValue(1), NewChain);
634*9880d681SAndroid Build Coastguard Worker 
635*9880d681SAndroid Build Coastguard Worker   return (Op.getResNo() ? NewChain : Value);
636*9880d681SAndroid Build Coastguard Worker }
637*9880d681SAndroid Build Coastguard Worker 
ExpandStore(SDValue Op)638*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandStore(SDValue Op) {
639*9880d681SAndroid Build Coastguard Worker   StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
640*9880d681SAndroid Build Coastguard Worker 
641*9880d681SAndroid Build Coastguard Worker   EVT StVT = ST->getMemoryVT();
642*9880d681SAndroid Build Coastguard Worker   EVT MemSclVT = StVT.getScalarType();
643*9880d681SAndroid Build Coastguard Worker   unsigned ScalarSize = MemSclVT.getSizeInBits();
644*9880d681SAndroid Build Coastguard Worker 
645*9880d681SAndroid Build Coastguard Worker   // Round odd types to the next pow of two.
646*9880d681SAndroid Build Coastguard Worker   if (!isPowerOf2_32(ScalarSize)) {
647*9880d681SAndroid Build Coastguard Worker     // FIXME: This is completely broken and inconsistent with ExpandLoad
648*9880d681SAndroid Build Coastguard Worker     // handling.
649*9880d681SAndroid Build Coastguard Worker 
650*9880d681SAndroid Build Coastguard Worker     // For sub-byte element sizes, this ends up with 0 stride between elements,
651*9880d681SAndroid Build Coastguard Worker     // so the same element just gets re-written to the same location. There seem
652*9880d681SAndroid Build Coastguard Worker     // to be tests explicitly testing for this broken behavior though.  tests
653*9880d681SAndroid Build Coastguard Worker     // for this broken behavior.
654*9880d681SAndroid Build Coastguard Worker 
655*9880d681SAndroid Build Coastguard Worker     LLVMContext &Ctx = *DAG.getContext();
656*9880d681SAndroid Build Coastguard Worker 
657*9880d681SAndroid Build Coastguard Worker     EVT NewMemVT
658*9880d681SAndroid Build Coastguard Worker       = EVT::getVectorVT(Ctx,
659*9880d681SAndroid Build Coastguard Worker                          MemSclVT.getIntegerVT(Ctx, NextPowerOf2(ScalarSize)),
660*9880d681SAndroid Build Coastguard Worker                          StVT.getVectorNumElements());
661*9880d681SAndroid Build Coastguard Worker 
662*9880d681SAndroid Build Coastguard Worker     SDValue NewVectorStore
663*9880d681SAndroid Build Coastguard Worker       = DAG.getTruncStore(ST->getChain(), SDLoc(Op), ST->getValue(),
664*9880d681SAndroid Build Coastguard Worker                           ST->getBasePtr(),
665*9880d681SAndroid Build Coastguard Worker                           ST->getPointerInfo(), NewMemVT,
666*9880d681SAndroid Build Coastguard Worker                           ST->isVolatile(), ST->isNonTemporal(),
667*9880d681SAndroid Build Coastguard Worker                           ST->getAlignment(),
668*9880d681SAndroid Build Coastguard Worker                           ST->getAAInfo());
669*9880d681SAndroid Build Coastguard Worker     ST = cast<StoreSDNode>(NewVectorStore.getNode());
670*9880d681SAndroid Build Coastguard Worker   }
671*9880d681SAndroid Build Coastguard Worker 
672*9880d681SAndroid Build Coastguard Worker   SDValue TF = TLI.scalarizeVectorStore(ST, DAG);
673*9880d681SAndroid Build Coastguard Worker   AddLegalizedOperand(Op, TF);
674*9880d681SAndroid Build Coastguard Worker   return TF;
675*9880d681SAndroid Build Coastguard Worker }
676*9880d681SAndroid Build Coastguard Worker 
Expand(SDValue Op)677*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::Expand(SDValue Op) {
678*9880d681SAndroid Build Coastguard Worker   switch (Op->getOpcode()) {
679*9880d681SAndroid Build Coastguard Worker   case ISD::SIGN_EXTEND_INREG:
680*9880d681SAndroid Build Coastguard Worker     return ExpandSEXTINREG(Op);
681*9880d681SAndroid Build Coastguard Worker   case ISD::ANY_EXTEND_VECTOR_INREG:
682*9880d681SAndroid Build Coastguard Worker     return ExpandANY_EXTEND_VECTOR_INREG(Op);
683*9880d681SAndroid Build Coastguard Worker   case ISD::SIGN_EXTEND_VECTOR_INREG:
684*9880d681SAndroid Build Coastguard Worker     return ExpandSIGN_EXTEND_VECTOR_INREG(Op);
685*9880d681SAndroid Build Coastguard Worker   case ISD::ZERO_EXTEND_VECTOR_INREG:
686*9880d681SAndroid Build Coastguard Worker     return ExpandZERO_EXTEND_VECTOR_INREG(Op);
687*9880d681SAndroid Build Coastguard Worker   case ISD::BSWAP:
688*9880d681SAndroid Build Coastguard Worker     return ExpandBSWAP(Op);
689*9880d681SAndroid Build Coastguard Worker   case ISD::VSELECT:
690*9880d681SAndroid Build Coastguard Worker     return ExpandVSELECT(Op);
691*9880d681SAndroid Build Coastguard Worker   case ISD::SELECT:
692*9880d681SAndroid Build Coastguard Worker     return ExpandSELECT(Op);
693*9880d681SAndroid Build Coastguard Worker   case ISD::UINT_TO_FP:
694*9880d681SAndroid Build Coastguard Worker     return ExpandUINT_TO_FLOAT(Op);
695*9880d681SAndroid Build Coastguard Worker   case ISD::FNEG:
696*9880d681SAndroid Build Coastguard Worker     return ExpandFNEG(Op);
697*9880d681SAndroid Build Coastguard Worker   case ISD::SETCC:
698*9880d681SAndroid Build Coastguard Worker     return UnrollVSETCC(Op);
699*9880d681SAndroid Build Coastguard Worker   case ISD::BITREVERSE:
700*9880d681SAndroid Build Coastguard Worker     return ExpandBITREVERSE(Op);
701*9880d681SAndroid Build Coastguard Worker   case ISD::CTLZ_ZERO_UNDEF:
702*9880d681SAndroid Build Coastguard Worker   case ISD::CTTZ_ZERO_UNDEF:
703*9880d681SAndroid Build Coastguard Worker     return ExpandCTLZ_CTTZ_ZERO_UNDEF(Op);
704*9880d681SAndroid Build Coastguard Worker   default:
705*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
706*9880d681SAndroid Build Coastguard Worker   }
707*9880d681SAndroid Build Coastguard Worker }
708*9880d681SAndroid Build Coastguard Worker 
ExpandSELECT(SDValue Op)709*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
710*9880d681SAndroid Build Coastguard Worker   // Lower a select instruction where the condition is a scalar and the
711*9880d681SAndroid Build Coastguard Worker   // operands are vectors. Lower this select to VSELECT and implement it
712*9880d681SAndroid Build Coastguard Worker   // using XOR AND OR. The selector bit is broadcasted.
713*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
714*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
715*9880d681SAndroid Build Coastguard Worker 
716*9880d681SAndroid Build Coastguard Worker   SDValue Mask = Op.getOperand(0);
717*9880d681SAndroid Build Coastguard Worker   SDValue Op1 = Op.getOperand(1);
718*9880d681SAndroid Build Coastguard Worker   SDValue Op2 = Op.getOperand(2);
719*9880d681SAndroid Build Coastguard Worker 
720*9880d681SAndroid Build Coastguard Worker   assert(VT.isVector() && !Mask.getValueType().isVector()
721*9880d681SAndroid Build Coastguard Worker          && Op1.getValueType() == Op2.getValueType() && "Invalid type");
722*9880d681SAndroid Build Coastguard Worker 
723*9880d681SAndroid Build Coastguard Worker   unsigned NumElem = VT.getVectorNumElements();
724*9880d681SAndroid Build Coastguard Worker 
725*9880d681SAndroid Build Coastguard Worker   // If we can't even use the basic vector operations of
726*9880d681SAndroid Build Coastguard Worker   // AND,OR,XOR, we will have to scalarize the op.
727*9880d681SAndroid Build Coastguard Worker   // Notice that the operation may be 'promoted' which means that it is
728*9880d681SAndroid Build Coastguard Worker   // 'bitcasted' to another type which is handled.
729*9880d681SAndroid Build Coastguard Worker   // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
730*9880d681SAndroid Build Coastguard Worker   if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
731*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
732*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::OR,  VT) == TargetLowering::Expand ||
733*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::BUILD_VECTOR,  VT) == TargetLowering::Expand)
734*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
735*9880d681SAndroid Build Coastguard Worker 
736*9880d681SAndroid Build Coastguard Worker   // Generate a mask operand.
737*9880d681SAndroid Build Coastguard Worker   EVT MaskTy = VT.changeVectorElementTypeToInteger();
738*9880d681SAndroid Build Coastguard Worker 
739*9880d681SAndroid Build Coastguard Worker   // What is the size of each element in the vector mask.
740*9880d681SAndroid Build Coastguard Worker   EVT BitTy = MaskTy.getScalarType();
741*9880d681SAndroid Build Coastguard Worker 
742*9880d681SAndroid Build Coastguard Worker   Mask = DAG.getSelect(DL, BitTy, Mask,
743*9880d681SAndroid Build Coastguard Worker           DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), DL,
744*9880d681SAndroid Build Coastguard Worker                           BitTy),
745*9880d681SAndroid Build Coastguard Worker           DAG.getConstant(0, DL, BitTy));
746*9880d681SAndroid Build Coastguard Worker 
747*9880d681SAndroid Build Coastguard Worker   // Broadcast the mask so that the entire vector is all-one or all zero.
748*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 8> Ops(NumElem, Mask);
749*9880d681SAndroid Build Coastguard Worker   Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, Ops);
750*9880d681SAndroid Build Coastguard Worker 
751*9880d681SAndroid Build Coastguard Worker   // Bitcast the operands to be the same type as the mask.
752*9880d681SAndroid Build Coastguard Worker   // This is needed when we select between FP types because
753*9880d681SAndroid Build Coastguard Worker   // the mask is a vector of integers.
754*9880d681SAndroid Build Coastguard Worker   Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
755*9880d681SAndroid Build Coastguard Worker   Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
756*9880d681SAndroid Build Coastguard Worker 
757*9880d681SAndroid Build Coastguard Worker   SDValue AllOnes = DAG.getConstant(
758*9880d681SAndroid Build Coastguard Worker             APInt::getAllOnesValue(BitTy.getSizeInBits()), DL, MaskTy);
759*9880d681SAndroid Build Coastguard Worker   SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
760*9880d681SAndroid Build Coastguard Worker 
761*9880d681SAndroid Build Coastguard Worker   Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
762*9880d681SAndroid Build Coastguard Worker   Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
763*9880d681SAndroid Build Coastguard Worker   SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
764*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
765*9880d681SAndroid Build Coastguard Worker }
766*9880d681SAndroid Build Coastguard Worker 
ExpandSEXTINREG(SDValue Op)767*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) {
768*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
769*9880d681SAndroid Build Coastguard Worker 
770*9880d681SAndroid Build Coastguard Worker   // Make sure that the SRA and SHL instructions are available.
771*9880d681SAndroid Build Coastguard Worker   if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
772*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
773*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
774*9880d681SAndroid Build Coastguard Worker 
775*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
776*9880d681SAndroid Build Coastguard Worker   EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT();
777*9880d681SAndroid Build Coastguard Worker 
778*9880d681SAndroid Build Coastguard Worker   unsigned BW = VT.getScalarType().getSizeInBits();
779*9880d681SAndroid Build Coastguard Worker   unsigned OrigBW = OrigTy.getScalarType().getSizeInBits();
780*9880d681SAndroid Build Coastguard Worker   SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT);
781*9880d681SAndroid Build Coastguard Worker 
782*9880d681SAndroid Build Coastguard Worker   Op = Op.getOperand(0);
783*9880d681SAndroid Build Coastguard Worker   Op =   DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz);
784*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker 
787*9880d681SAndroid Build Coastguard Worker // Generically expand a vector anyext in register to a shuffle of the relevant
788*9880d681SAndroid Build Coastguard Worker // lanes into the appropriate locations, with other lanes left undef.
ExpandANY_EXTEND_VECTOR_INREG(SDValue Op)789*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDValue Op) {
790*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
791*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
792*9880d681SAndroid Build Coastguard Worker   int NumElements = VT.getVectorNumElements();
793*9880d681SAndroid Build Coastguard Worker   SDValue Src = Op.getOperand(0);
794*9880d681SAndroid Build Coastguard Worker   EVT SrcVT = Src.getValueType();
795*9880d681SAndroid Build Coastguard Worker   int NumSrcElements = SrcVT.getVectorNumElements();
796*9880d681SAndroid Build Coastguard Worker 
797*9880d681SAndroid Build Coastguard Worker   // Build a base mask of undef shuffles.
798*9880d681SAndroid Build Coastguard Worker   SmallVector<int, 16> ShuffleMask;
799*9880d681SAndroid Build Coastguard Worker   ShuffleMask.resize(NumSrcElements, -1);
800*9880d681SAndroid Build Coastguard Worker 
801*9880d681SAndroid Build Coastguard Worker   // Place the extended lanes into the correct locations.
802*9880d681SAndroid Build Coastguard Worker   int ExtLaneScale = NumSrcElements / NumElements;
803*9880d681SAndroid Build Coastguard Worker   int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
804*9880d681SAndroid Build Coastguard Worker   for (int i = 0; i < NumElements; ++i)
805*9880d681SAndroid Build Coastguard Worker     ShuffleMask[i * ExtLaneScale + EndianOffset] = i;
806*9880d681SAndroid Build Coastguard Worker 
807*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(
808*9880d681SAndroid Build Coastguard Worker       ISD::BITCAST, DL, VT,
809*9880d681SAndroid Build Coastguard Worker       DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask));
810*9880d681SAndroid Build Coastguard Worker }
811*9880d681SAndroid Build Coastguard Worker 
ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op)812*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDValue Op) {
813*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
814*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
815*9880d681SAndroid Build Coastguard Worker   SDValue Src = Op.getOperand(0);
816*9880d681SAndroid Build Coastguard Worker   EVT SrcVT = Src.getValueType();
817*9880d681SAndroid Build Coastguard Worker 
818*9880d681SAndroid Build Coastguard Worker   // First build an any-extend node which can be legalized above when we
819*9880d681SAndroid Build Coastguard Worker   // recurse through it.
820*9880d681SAndroid Build Coastguard Worker   Op = DAG.getAnyExtendVectorInReg(Src, DL, VT);
821*9880d681SAndroid Build Coastguard Worker 
822*9880d681SAndroid Build Coastguard Worker   // Now we need sign extend. Do this by shifting the elements. Even if these
823*9880d681SAndroid Build Coastguard Worker   // aren't legal operations, they have a better chance of being legalized
824*9880d681SAndroid Build Coastguard Worker   // without full scalarization than the sign extension does.
825*9880d681SAndroid Build Coastguard Worker   unsigned EltWidth = VT.getVectorElementType().getSizeInBits();
826*9880d681SAndroid Build Coastguard Worker   unsigned SrcEltWidth = SrcVT.getVectorElementType().getSizeInBits();
827*9880d681SAndroid Build Coastguard Worker   SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT);
828*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::SRA, DL, VT,
829*9880d681SAndroid Build Coastguard Worker                      DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount),
830*9880d681SAndroid Build Coastguard Worker                      ShiftAmount);
831*9880d681SAndroid Build Coastguard Worker }
832*9880d681SAndroid Build Coastguard Worker 
833*9880d681SAndroid Build Coastguard Worker // Generically expand a vector zext in register to a shuffle of the relevant
834*9880d681SAndroid Build Coastguard Worker // lanes into the appropriate locations, a blend of zero into the high bits,
835*9880d681SAndroid Build Coastguard Worker // and a bitcast to the wider element type.
ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op)836*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op) {
837*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
838*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
839*9880d681SAndroid Build Coastguard Worker   int NumElements = VT.getVectorNumElements();
840*9880d681SAndroid Build Coastguard Worker   SDValue Src = Op.getOperand(0);
841*9880d681SAndroid Build Coastguard Worker   EVT SrcVT = Src.getValueType();
842*9880d681SAndroid Build Coastguard Worker   int NumSrcElements = SrcVT.getVectorNumElements();
843*9880d681SAndroid Build Coastguard Worker 
844*9880d681SAndroid Build Coastguard Worker   // Build up a zero vector to blend into this one.
845*9880d681SAndroid Build Coastguard Worker   SDValue Zero = DAG.getConstant(0, DL, SrcVT);
846*9880d681SAndroid Build Coastguard Worker 
847*9880d681SAndroid Build Coastguard Worker   // Shuffle the incoming lanes into the correct position, and pull all other
848*9880d681SAndroid Build Coastguard Worker   // lanes from the zero vector.
849*9880d681SAndroid Build Coastguard Worker   SmallVector<int, 16> ShuffleMask;
850*9880d681SAndroid Build Coastguard Worker   ShuffleMask.reserve(NumSrcElements);
851*9880d681SAndroid Build Coastguard Worker   for (int i = 0; i < NumSrcElements; ++i)
852*9880d681SAndroid Build Coastguard Worker     ShuffleMask.push_back(i);
853*9880d681SAndroid Build Coastguard Worker 
854*9880d681SAndroid Build Coastguard Worker   int ExtLaneScale = NumSrcElements / NumElements;
855*9880d681SAndroid Build Coastguard Worker   int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
856*9880d681SAndroid Build Coastguard Worker   for (int i = 0; i < NumElements; ++i)
857*9880d681SAndroid Build Coastguard Worker     ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
858*9880d681SAndroid Build Coastguard Worker 
859*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::BITCAST, DL, VT,
860*9880d681SAndroid Build Coastguard Worker                      DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
861*9880d681SAndroid Build Coastguard Worker }
862*9880d681SAndroid Build Coastguard Worker 
createBSWAPShuffleMask(EVT VT,SmallVectorImpl<int> & ShuffleMask)863*9880d681SAndroid Build Coastguard Worker static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
864*9880d681SAndroid Build Coastguard Worker   int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
865*9880d681SAndroid Build Coastguard Worker   for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
866*9880d681SAndroid Build Coastguard Worker     for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
867*9880d681SAndroid Build Coastguard Worker       ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
868*9880d681SAndroid Build Coastguard Worker }
869*9880d681SAndroid Build Coastguard Worker 
ExpandBSWAP(SDValue Op)870*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) {
871*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
872*9880d681SAndroid Build Coastguard Worker 
873*9880d681SAndroid Build Coastguard Worker   // Generate a byte wise shuffle mask for the BSWAP.
874*9880d681SAndroid Build Coastguard Worker   SmallVector<int, 16> ShuffleMask;
875*9880d681SAndroid Build Coastguard Worker   createBSWAPShuffleMask(VT, ShuffleMask);
876*9880d681SAndroid Build Coastguard Worker   EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
877*9880d681SAndroid Build Coastguard Worker 
878*9880d681SAndroid Build Coastguard Worker   // Only emit a shuffle if the mask is legal.
879*9880d681SAndroid Build Coastguard Worker   if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT))
880*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
881*9880d681SAndroid Build Coastguard Worker 
882*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
883*9880d681SAndroid Build Coastguard Worker   Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0));
884*9880d681SAndroid Build Coastguard Worker   Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask);
885*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::BITCAST, DL, VT, Op);
886*9880d681SAndroid Build Coastguard Worker }
887*9880d681SAndroid Build Coastguard Worker 
ExpandBITREVERSE(SDValue Op)888*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandBITREVERSE(SDValue Op) {
889*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
890*9880d681SAndroid Build Coastguard Worker 
891*9880d681SAndroid Build Coastguard Worker   // If we have the scalar operation, it's probably cheaper to unroll it.
892*9880d681SAndroid Build Coastguard Worker   if (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, VT.getScalarType()))
893*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
894*9880d681SAndroid Build Coastguard Worker 
895*9880d681SAndroid Build Coastguard Worker   // If the vector element width is a whole number of bytes, test if its legal
896*9880d681SAndroid Build Coastguard Worker   // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte
897*9880d681SAndroid Build Coastguard Worker   // vector. This greatly reduces the number of bit shifts necessary.
898*9880d681SAndroid Build Coastguard Worker   unsigned ScalarSizeInBits = VT.getScalarSizeInBits();
899*9880d681SAndroid Build Coastguard Worker   if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) {
900*9880d681SAndroid Build Coastguard Worker     SmallVector<int, 16> BSWAPMask;
901*9880d681SAndroid Build Coastguard Worker     createBSWAPShuffleMask(VT, BSWAPMask);
902*9880d681SAndroid Build Coastguard Worker 
903*9880d681SAndroid Build Coastguard Worker     EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size());
904*9880d681SAndroid Build Coastguard Worker     if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) &&
905*9880d681SAndroid Build Coastguard Worker         (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, ByteVT) ||
906*9880d681SAndroid Build Coastguard Worker          (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) &&
907*9880d681SAndroid Build Coastguard Worker           TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) &&
908*9880d681SAndroid Build Coastguard Worker           TLI.isOperationLegalOrCustomOrPromote(ISD::AND, ByteVT) &&
909*9880d681SAndroid Build Coastguard Worker           TLI.isOperationLegalOrCustomOrPromote(ISD::OR, ByteVT)))) {
910*9880d681SAndroid Build Coastguard Worker       SDLoc DL(Op);
911*9880d681SAndroid Build Coastguard Worker       Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0));
912*9880d681SAndroid Build Coastguard Worker       Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
913*9880d681SAndroid Build Coastguard Worker                                 BSWAPMask);
914*9880d681SAndroid Build Coastguard Worker       Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op);
915*9880d681SAndroid Build Coastguard Worker       return DAG.getNode(ISD::BITCAST, DL, VT, Op);
916*9880d681SAndroid Build Coastguard Worker     }
917*9880d681SAndroid Build Coastguard Worker   }
918*9880d681SAndroid Build Coastguard Worker 
919*9880d681SAndroid Build Coastguard Worker   // If we have the appropriate vector bit operations, it is better to use them
920*9880d681SAndroid Build Coastguard Worker   // than unrolling and expanding each component.
921*9880d681SAndroid Build Coastguard Worker   if (!TLI.isOperationLegalOrCustom(ISD::SHL, VT) ||
922*9880d681SAndroid Build Coastguard Worker       !TLI.isOperationLegalOrCustom(ISD::SRL, VT) ||
923*9880d681SAndroid Build Coastguard Worker       !TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) ||
924*9880d681SAndroid Build Coastguard Worker       !TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT))
925*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
926*9880d681SAndroid Build Coastguard Worker 
927*9880d681SAndroid Build Coastguard Worker   // Let LegalizeDAG handle this later.
928*9880d681SAndroid Build Coastguard Worker   return Op;
929*9880d681SAndroid Build Coastguard Worker }
930*9880d681SAndroid Build Coastguard Worker 
ExpandVSELECT(SDValue Op)931*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
932*9880d681SAndroid Build Coastguard Worker   // Implement VSELECT in terms of XOR, AND, OR
933*9880d681SAndroid Build Coastguard Worker   // on platforms which do not support blend natively.
934*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
935*9880d681SAndroid Build Coastguard Worker 
936*9880d681SAndroid Build Coastguard Worker   SDValue Mask = Op.getOperand(0);
937*9880d681SAndroid Build Coastguard Worker   SDValue Op1 = Op.getOperand(1);
938*9880d681SAndroid Build Coastguard Worker   SDValue Op2 = Op.getOperand(2);
939*9880d681SAndroid Build Coastguard Worker 
940*9880d681SAndroid Build Coastguard Worker   EVT VT = Mask.getValueType();
941*9880d681SAndroid Build Coastguard Worker 
942*9880d681SAndroid Build Coastguard Worker   // If we can't even use the basic vector operations of
943*9880d681SAndroid Build Coastguard Worker   // AND,OR,XOR, we will have to scalarize the op.
944*9880d681SAndroid Build Coastguard Worker   // Notice that the operation may be 'promoted' which means that it is
945*9880d681SAndroid Build Coastguard Worker   // 'bitcasted' to another type which is handled.
946*9880d681SAndroid Build Coastguard Worker   // This operation also isn't safe with AND, OR, XOR when the boolean
947*9880d681SAndroid Build Coastguard Worker   // type is 0/1 as we need an all ones vector constant to mask with.
948*9880d681SAndroid Build Coastguard Worker   // FIXME: Sign extend 1 to all ones if thats legal on the target.
949*9880d681SAndroid Build Coastguard Worker   if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
950*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
951*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
952*9880d681SAndroid Build Coastguard Worker       TLI.getBooleanContents(Op1.getValueType()) !=
953*9880d681SAndroid Build Coastguard Worker           TargetLowering::ZeroOrNegativeOneBooleanContent)
954*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
955*9880d681SAndroid Build Coastguard Worker 
956*9880d681SAndroid Build Coastguard Worker   // If the mask and the type are different sizes, unroll the vector op. This
957*9880d681SAndroid Build Coastguard Worker   // can occur when getSetCCResultType returns something that is different in
958*9880d681SAndroid Build Coastguard Worker   // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
959*9880d681SAndroid Build Coastguard Worker   if (VT.getSizeInBits() != Op1.getValueType().getSizeInBits())
960*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
961*9880d681SAndroid Build Coastguard Worker 
962*9880d681SAndroid Build Coastguard Worker   // Bitcast the operands to be the same type as the mask.
963*9880d681SAndroid Build Coastguard Worker   // This is needed when we select between FP types because
964*9880d681SAndroid Build Coastguard Worker   // the mask is a vector of integers.
965*9880d681SAndroid Build Coastguard Worker   Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
966*9880d681SAndroid Build Coastguard Worker   Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
967*9880d681SAndroid Build Coastguard Worker 
968*9880d681SAndroid Build Coastguard Worker   SDValue AllOnes = DAG.getConstant(
969*9880d681SAndroid Build Coastguard Worker     APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), DL, VT);
970*9880d681SAndroid Build Coastguard Worker   SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
971*9880d681SAndroid Build Coastguard Worker 
972*9880d681SAndroid Build Coastguard Worker   Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
973*9880d681SAndroid Build Coastguard Worker   Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
974*9880d681SAndroid Build Coastguard Worker   SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
975*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
976*9880d681SAndroid Build Coastguard Worker }
977*9880d681SAndroid Build Coastguard Worker 
ExpandUINT_TO_FLOAT(SDValue Op)978*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
979*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getOperand(0).getValueType();
980*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Op);
981*9880d681SAndroid Build Coastguard Worker 
982*9880d681SAndroid Build Coastguard Worker   // Make sure that the SINT_TO_FP and SRL instructions are available.
983*9880d681SAndroid Build Coastguard Worker   if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
984*9880d681SAndroid Build Coastguard Worker       TLI.getOperationAction(ISD::SRL,        VT) == TargetLowering::Expand)
985*9880d681SAndroid Build Coastguard Worker     return DAG.UnrollVectorOp(Op.getNode());
986*9880d681SAndroid Build Coastguard Worker 
987*9880d681SAndroid Build Coastguard Worker  EVT SVT = VT.getScalarType();
988*9880d681SAndroid Build Coastguard Worker   assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
989*9880d681SAndroid Build Coastguard Worker       "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
990*9880d681SAndroid Build Coastguard Worker 
991*9880d681SAndroid Build Coastguard Worker   unsigned BW = SVT.getSizeInBits();
992*9880d681SAndroid Build Coastguard Worker   SDValue HalfWord = DAG.getConstant(BW/2, DL, VT);
993*9880d681SAndroid Build Coastguard Worker 
994*9880d681SAndroid Build Coastguard Worker   // Constants to clear the upper part of the word.
995*9880d681SAndroid Build Coastguard Worker   // Notice that we can also use SHL+SHR, but using a constant is slightly
996*9880d681SAndroid Build Coastguard Worker   // faster on x86.
997*9880d681SAndroid Build Coastguard Worker   uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
998*9880d681SAndroid Build Coastguard Worker   SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT);
999*9880d681SAndroid Build Coastguard Worker 
1000*9880d681SAndroid Build Coastguard Worker   // Two to the power of half-word-size.
1001*9880d681SAndroid Build Coastguard Worker   SDValue TWOHW = DAG.getConstantFP(1 << (BW/2), DL, Op.getValueType());
1002*9880d681SAndroid Build Coastguard Worker 
1003*9880d681SAndroid Build Coastguard Worker   // Clear upper part of LO, lower HI
1004*9880d681SAndroid Build Coastguard Worker   SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
1005*9880d681SAndroid Build Coastguard Worker   SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
1006*9880d681SAndroid Build Coastguard Worker 
1007*9880d681SAndroid Build Coastguard Worker   // Convert hi and lo to floats
1008*9880d681SAndroid Build Coastguard Worker   // Convert the hi part back to the upper values
1009*9880d681SAndroid Build Coastguard Worker   // TODO: Can any fast-math-flags be set on these nodes?
1010*9880d681SAndroid Build Coastguard Worker   SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
1011*9880d681SAndroid Build Coastguard Worker           fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
1012*9880d681SAndroid Build Coastguard Worker   SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
1013*9880d681SAndroid Build Coastguard Worker 
1014*9880d681SAndroid Build Coastguard Worker   // Add the two halves
1015*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
1016*9880d681SAndroid Build Coastguard Worker }
1017*9880d681SAndroid Build Coastguard Worker 
1018*9880d681SAndroid Build Coastguard Worker 
ExpandFNEG(SDValue Op)1019*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
1020*9880d681SAndroid Build Coastguard Worker   if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
1021*9880d681SAndroid Build Coastguard Worker     SDLoc DL(Op);
1022*9880d681SAndroid Build Coastguard Worker     SDValue Zero = DAG.getConstantFP(-0.0, DL, Op.getValueType());
1023*9880d681SAndroid Build Coastguard Worker     // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB.
1024*9880d681SAndroid Build Coastguard Worker     return DAG.getNode(ISD::FSUB, DL, Op.getValueType(),
1025*9880d681SAndroid Build Coastguard Worker                        Zero, Op.getOperand(0));
1026*9880d681SAndroid Build Coastguard Worker   }
1027*9880d681SAndroid Build Coastguard Worker   return DAG.UnrollVectorOp(Op.getNode());
1028*9880d681SAndroid Build Coastguard Worker }
1029*9880d681SAndroid Build Coastguard Worker 
ExpandCTLZ_CTTZ_ZERO_UNDEF(SDValue Op)1030*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::ExpandCTLZ_CTTZ_ZERO_UNDEF(SDValue Op) {
1031*9880d681SAndroid Build Coastguard Worker   // If the non-ZERO_UNDEF version is supported we can use that instead.
1032*9880d681SAndroid Build Coastguard Worker   unsigned Opc = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF ? ISD::CTLZ : ISD::CTTZ;
1033*9880d681SAndroid Build Coastguard Worker   if (TLI.isOperationLegalOrCustom(Opc, Op.getValueType())) {
1034*9880d681SAndroid Build Coastguard Worker     SDLoc DL(Op);
1035*9880d681SAndroid Build Coastguard Worker     return DAG.getNode(Opc, DL, Op.getValueType(), Op.getOperand(0));
1036*9880d681SAndroid Build Coastguard Worker   }
1037*9880d681SAndroid Build Coastguard Worker 
1038*9880d681SAndroid Build Coastguard Worker   // Otherwise go ahead and unroll.
1039*9880d681SAndroid Build Coastguard Worker   return DAG.UnrollVectorOp(Op.getNode());
1040*9880d681SAndroid Build Coastguard Worker }
1041*9880d681SAndroid Build Coastguard Worker 
UnrollVSETCC(SDValue Op)1042*9880d681SAndroid Build Coastguard Worker SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
1043*9880d681SAndroid Build Coastguard Worker   EVT VT = Op.getValueType();
1044*9880d681SAndroid Build Coastguard Worker   unsigned NumElems = VT.getVectorNumElements();
1045*9880d681SAndroid Build Coastguard Worker   EVT EltVT = VT.getVectorElementType();
1046*9880d681SAndroid Build Coastguard Worker   SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
1047*9880d681SAndroid Build Coastguard Worker   EVT TmpEltVT = LHS.getValueType().getVectorElementType();
1048*9880d681SAndroid Build Coastguard Worker   SDLoc dl(Op);
1049*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 8> Ops(NumElems);
1050*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < NumElems; ++i) {
1051*9880d681SAndroid Build Coastguard Worker     SDValue LHSElem = DAG.getNode(
1052*9880d681SAndroid Build Coastguard Worker         ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
1053*9880d681SAndroid Build Coastguard Worker         DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
1054*9880d681SAndroid Build Coastguard Worker     SDValue RHSElem = DAG.getNode(
1055*9880d681SAndroid Build Coastguard Worker         ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
1056*9880d681SAndroid Build Coastguard Worker         DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
1057*9880d681SAndroid Build Coastguard Worker     Ops[i] = DAG.getNode(ISD::SETCC, dl,
1058*9880d681SAndroid Build Coastguard Worker                          TLI.getSetCCResultType(DAG.getDataLayout(),
1059*9880d681SAndroid Build Coastguard Worker                                                 *DAG.getContext(), TmpEltVT),
1060*9880d681SAndroid Build Coastguard Worker                          LHSElem, RHSElem, CC);
1061*9880d681SAndroid Build Coastguard Worker     Ops[i] = DAG.getSelect(dl, EltVT, Ops[i],
1062*9880d681SAndroid Build Coastguard Worker                            DAG.getConstant(APInt::getAllOnesValue
1063*9880d681SAndroid Build Coastguard Worker                                            (EltVT.getSizeInBits()), dl, EltVT),
1064*9880d681SAndroid Build Coastguard Worker                            DAG.getConstant(0, dl, EltVT));
1065*9880d681SAndroid Build Coastguard Worker   }
1066*9880d681SAndroid Build Coastguard Worker   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1067*9880d681SAndroid Build Coastguard Worker }
1068*9880d681SAndroid Build Coastguard Worker 
1069*9880d681SAndroid Build Coastguard Worker }
1070*9880d681SAndroid Build Coastguard Worker 
LegalizeVectors()1071*9880d681SAndroid Build Coastguard Worker bool SelectionDAG::LegalizeVectors() {
1072*9880d681SAndroid Build Coastguard Worker   return VectorLegalizer(*this).Run();
1073*9880d681SAndroid Build Coastguard Worker }
1074