blob: 3fa64843a93a0c019c514c0d01e9a4f3564cafb1 [file] [log] [blame]
Eli Friedmanda90dd62009-05-23 12:35:30 +00001//===-- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::LegalizeVectors method.
11//
12// The vector legalizer looks for vector operations which might need to be
Eli Friedman3b251702009-05-27 07:58:35 +000013// scalarized and legalizes them. This is a separate step from Legalize because
14// scalarizing can introduce illegal types. For example, suppose we have an
Eli Friedmanda90dd62009-05-23 12:35:30 +000015// ISD::SDIV of type v2i64 on x86-32. The type is legal (for example, addition
16// on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the
17// operation, which introduces nodes with the illegal type i64 which must be
18// expanded. Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC;
19// the operation must be unrolled, which introduces nodes with the illegal
20// type i8 which must be promoted.
21//
22// This does not legalize vector manipulations like ISD::BUILD_VECTOR,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +000023// or operations that happen to take a vector which are custom-lowered;
24// the legalization for such operations never produces nodes
Eli Friedmanda90dd62009-05-23 12:35:30 +000025// with illegal types, so it's okay to put off legalizing them until
26// SelectionDAG::Legalize runs.
27//
28//===----------------------------------------------------------------------===//
29
30#include "llvm/CodeGen/SelectionDAG.h"
31#include "llvm/Target/TargetLowering.h"
32using namespace llvm;
33
34namespace {
35class VectorLegalizer {
36 SelectionDAG& DAG;
Dan Gohman21cea8a2010-04-17 15:26:15 +000037 const TargetLowering &TLI;
Eli Friedmanda90dd62009-05-23 12:35:30 +000038 bool Changed; // Keep track of whether anything changed
39
Chandler Carruth68adf152014-07-02 02:16:57 +000040 /// For nodes that are of legal width, and that have more than one use, this
41 /// map indicates what regularized operand to use. This allows us to avoid
42 /// legalizing the same thing more than once.
Preston Gurd0959bb72013-01-25 15:18:54 +000043 SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes;
Eli Friedmanda90dd62009-05-23 12:35:30 +000044
Chandler Carruth68adf152014-07-02 02:16:57 +000045 /// \brief Adds a node to the translation cache.
Eli Friedmanda90dd62009-05-23 12:35:30 +000046 void AddLegalizedOperand(SDValue From, SDValue To) {
47 LegalizedNodes.insert(std::make_pair(From, To));
48 // If someone requests legalization of the new node, return itself.
49 if (From != To)
50 LegalizedNodes.insert(std::make_pair(To, To));
51 }
52
Chandler Carruth68adf152014-07-02 02:16:57 +000053 /// \brief Legalizes the given node.
Eli Friedmanda90dd62009-05-23 12:35:30 +000054 SDValue LegalizeOp(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000055
56 /// \brief Assuming the node is legal, "legalize" the results.
Eli Friedmanda90dd62009-05-23 12:35:30 +000057 SDValue TranslateLegalizeResults(SDValue Op, SDValue Result);
Chandler Carruth68adf152014-07-02 02:16:57 +000058
59 /// \brief Implements unrolling a VSETCC.
Eli Friedmanda90dd62009-05-23 12:35:30 +000060 SDValue UnrollVSETCC(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000061
Chandler Carruthc1bedac2014-07-02 06:23:34 +000062 /// \brief Implement expand-based legalization of vector operations.
63 ///
64 /// This is just a high-level routine to dispatch to specific code paths for
65 /// operations to legalize them.
66 SDValue Expand(SDValue Op);
67
Chandler Carruth68adf152014-07-02 02:16:57 +000068 /// \brief Implements expansion for FNEG; falls back to UnrollVectorOp if
69 /// FSUB isn't legal.
70 ///
71 /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
72 /// SINT_TO_FLOAT and SHR on vectors isn't legal.
Nadav Roteme7a101c2011-03-19 13:09:10 +000073 SDValue ExpandUINT_TO_FLOAT(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000074
75 /// \brief Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
Nadav Rotemdbe5c722013-01-11 22:57:48 +000076 SDValue ExpandSEXTINREG(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000077
Chandler Carruthafe4b252014-07-09 10:58:18 +000078 /// \brief Implement expansion for ZERO_EXTEND_VECTOR_INREG.
79 ///
80 /// Shuffles the low lanes of the operand into place and blends zeros into
81 /// the remaining lanes, finally bitcasting to the proper type.
82 SDValue ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op);
83
Chandler Carruth68adf152014-07-02 02:16:57 +000084 /// \brief Expand bswap of vectors into a shuffle if legal.
Benjamin Kramerf3ad2352014-05-19 13:12:38 +000085 SDValue ExpandBSWAP(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000086
87 /// \brief Implement vselect in terms of XOR, AND, OR when blend is not
88 /// supported by the target.
Nadav Rotem52202fb2011-09-13 19:17:42 +000089 SDValue ExpandVSELECT(SDValue Op);
Nadav Rotemea973bd2012-08-30 19:17:29 +000090 SDValue ExpandSELECT(SDValue Op);
Nadav Rotemebe13bc2011-10-15 07:41:10 +000091 SDValue ExpandLoad(SDValue Op);
92 SDValue ExpandStore(SDValue Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +000093 SDValue ExpandFNEG(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000094
95 /// \brief Implements vector promotion.
96 ///
97 /// This is essentially just bitcasting the operands to a different type and
98 /// bitcasting the result back to the original type.
Chandler Carruth1cfa8952014-07-02 03:07:11 +000099 SDValue Promote(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +0000100
101 /// \brief Implements [SU]INT_TO_FP vector promotion.
102 ///
103 /// This is a [zs]ext of the input operand to the next size up.
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000104 SDValue PromoteINT_TO_FP(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +0000105
106 /// \brief Implements FP_TO_[SU]INT vector promotion of the result type.
107 ///
108 /// It is promoted to the next size up integer type. The result is then
109 /// truncated back to the original type.
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000110 SDValue PromoteFP_TO_INT(SDValue Op, bool isSigned);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000111
Chandler Carruth68adf152014-07-02 02:16:57 +0000112public:
113 /// \brief Begin legalizer the vector operations in the DAG.
Eli Friedmanda90dd62009-05-23 12:35:30 +0000114 bool Run();
115 VectorLegalizer(SelectionDAG& dag) :
116 DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
117};
118
119bool VectorLegalizer::Run() {
Nadav Rotemb7f90bd2013-02-22 23:33:30 +0000120 // Before we start legalizing vector nodes, check if there are any vectors.
121 bool HasVectors = false;
122 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000123 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
Nadav Rotemb7f90bd2013-02-22 23:33:30 +0000124 // Check if the values of the nodes contain vectors. We don't need to check
125 // the operands because we are going to check their values at some point.
126 for (SDNode::value_iterator J = I->value_begin(), E = I->value_end();
127 J != E; ++J)
128 HasVectors |= J->isVector();
129
130 // If we found a vector node we can start the legalization.
131 if (HasVectors)
132 break;
133 }
134
135 // If this basic block has no vectors then no need to legalize vectors.
136 if (!HasVectors)
137 return false;
138
Eli Friedmanda90dd62009-05-23 12:35:30 +0000139 // The legalize process is inherently a bottom-up recursive process (users
140 // legalize their uses before themselves). Given infinite stack space, we
141 // could just start legalizing on the root and traverse the whole graph. In
142 // practice however, this causes us to run out of stack space on large basic
143 // blocks. To avoid this problem, compute an ordering of the nodes where each
144 // node is only legalized after all of its operands are legalized.
145 DAG.AssignTopologicalOrder();
146 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000147 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
Eli Friedmanda90dd62009-05-23 12:35:30 +0000148 LegalizeOp(SDValue(I, 0));
149
150 // Finally, it's possible the root changed. Get the new root.
151 SDValue OldRoot = DAG.getRoot();
152 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
153 DAG.setRoot(LegalizedNodes[OldRoot]);
154
155 LegalizedNodes.clear();
156
157 // Remove dead nodes now.
158 DAG.RemoveDeadNodes();
159
160 return Changed;
161}
162
163SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
164 // Generic legalization: just pass the operand through.
165 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
166 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
167 return Result.getValue(Op.getResNo());
168}
169
170SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
171 // Note that LegalizeOp may be reentered even from single-use nodes, which
172 // means that we always must cache transformed nodes.
173 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
174 if (I != LegalizedNodes.end()) return I->second;
175
176 SDNode* Node = Op.getNode();
177
178 // Legalize the operands
179 SmallVector<SDValue, 8> Ops;
180 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
181 Ops.push_back(LegalizeOp(Node->getOperand(i)));
182
Craig Topper8c0b4d02014-04-28 05:57:50 +0000183 SDValue Result = SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops), 0);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000184
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000185 if (Op.getOpcode() == ISD::LOAD) {
186 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
187 ISD::LoadExtType ExtType = LD->getExtensionType();
188 if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
189 if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
190 return TranslateLegalizeResults(Op, Result);
191 Changed = true;
192 return LegalizeOp(ExpandLoad(Op));
193 }
194 } else if (Op.getOpcode() == ISD::STORE) {
195 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
196 EVT StVT = ST->getMemoryVT();
Patrik Hagglundd7cdcf82012-12-19 08:28:51 +0000197 MVT ValVT = ST->getValue().getSimpleValueType();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000198 if (StVT.isVector() && ST->isTruncatingStore())
Patrik Hagglundd7cdcf82012-12-19 08:28:51 +0000199 switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) {
Craig Topperee4dab52012-02-05 08:31:47 +0000200 default: llvm_unreachable("This action is not supported yet!");
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000201 case TargetLowering::Legal:
202 return TranslateLegalizeResults(Op, Result);
203 case TargetLowering::Custom:
204 Changed = true;
Tom Stellard1b2c2d82013-08-21 22:42:58 +0000205 return TranslateLegalizeResults(Op, TLI.LowerOperation(Result, DAG));
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000206 case TargetLowering::Expand:
207 Changed = true;
208 return LegalizeOp(ExpandStore(Op));
209 }
210 }
211
Eli Friedmanda90dd62009-05-23 12:35:30 +0000212 bool HasVectorValue = false;
213 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
214 J != E;
215 ++J)
216 HasVectorValue |= J->isVector();
217 if (!HasVectorValue)
218 return TranslateLegalizeResults(Op, Result);
219
Owen Anderson53aa7a92009-08-10 22:56:29 +0000220 EVT QueryType;
Eli Friedmanda90dd62009-05-23 12:35:30 +0000221 switch (Op.getOpcode()) {
222 default:
223 return TranslateLegalizeResults(Op, Result);
224 case ISD::ADD:
225 case ISD::SUB:
226 case ISD::MUL:
227 case ISD::SDIV:
228 case ISD::UDIV:
229 case ISD::SREM:
230 case ISD::UREM:
231 case ISD::FADD:
232 case ISD::FSUB:
233 case ISD::FMUL:
234 case ISD::FDIV:
235 case ISD::FREM:
236 case ISD::AND:
237 case ISD::OR:
238 case ISD::XOR:
239 case ISD::SHL:
240 case ISD::SRA:
241 case ISD::SRL:
242 case ISD::ROTL:
243 case ISD::ROTR:
Hal Finkel5c968d92014-02-03 17:27:25 +0000244 case ISD::BSWAP:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000245 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000246 case ISD::CTTZ:
247 case ISD::CTLZ_ZERO_UNDEF:
248 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000249 case ISD::CTPOP:
250 case ISD::SELECT:
Nadav Rotem52202fb2011-09-13 19:17:42 +0000251 case ISD::VSELECT:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000252 case ISD::SELECT_CC:
Duncan Sandsf2641e12011-09-06 19:07:46 +0000253 case ISD::SETCC:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000254 case ISD::ZERO_EXTEND:
255 case ISD::ANY_EXTEND:
256 case ISD::TRUNCATE:
257 case ISD::SIGN_EXTEND:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000258 case ISD::FP_TO_SINT:
259 case ISD::FP_TO_UINT:
260 case ISD::FNEG:
261 case ISD::FABS:
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000262 case ISD::FCOPYSIGN:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000263 case ISD::FSQRT:
264 case ISD::FSIN:
265 case ISD::FCOS:
266 case ISD::FPOWI:
267 case ISD::FPOW:
268 case ISD::FLOG:
269 case ISD::FLOG2:
270 case ISD::FLOG10:
271 case ISD::FEXP:
272 case ISD::FEXP2:
273 case ISD::FCEIL:
274 case ISD::FTRUNC:
275 case ISD::FRINT:
276 case ISD::FNEARBYINT:
Hal Finkel171817e2013-08-07 22:49:12 +0000277 case ISD::FROUND:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000278 case ISD::FFLOOR:
Eli Friedmane6385e62012-11-15 22:44:27 +0000279 case ISD::FP_ROUND:
Eli Friedman30834942012-11-17 01:52:46 +0000280 case ISD::FP_EXTEND:
Craig Topper2da13f92012-08-30 07:34:22 +0000281 case ISD::FMA:
Nadav Rotem771f2962011-07-14 11:11:14 +0000282 case ISD::SIGN_EXTEND_INREG:
Chandler Carruthafe4b252014-07-09 10:58:18 +0000283 case ISD::ZERO_EXTEND_VECTOR_INREG:
Eli Friedmanaea9b652009-06-06 03:27:50 +0000284 QueryType = Node->getValueType(0);
285 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000286 case ISD::FP_ROUND_INREG:
287 QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
288 break;
Eli Friedmanaea9b652009-06-06 03:27:50 +0000289 case ISD::SINT_TO_FP:
290 case ISD::UINT_TO_FP:
291 QueryType = Node->getOperand(0).getValueType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000292 break;
293 }
294
Eli Friedmanaea9b652009-06-06 03:27:50 +0000295 switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
Eli Friedmanda90dd62009-05-23 12:35:30 +0000296 case TargetLowering::Promote:
Chandler Carruth2746c282014-07-02 03:07:15 +0000297 Result = Promote(Op);
298 Changed = true;
Eli Friedmanda90dd62009-05-23 12:35:30 +0000299 break;
Chandler Carruth2746c282014-07-02 03:07:15 +0000300 case TargetLowering::Legal:
301 break;
Eli Friedmanda90dd62009-05-23 12:35:30 +0000302 case TargetLowering::Custom: {
303 SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
304 if (Tmp1.getNode()) {
305 Result = Tmp1;
306 break;
307 }
308 // FALL THROUGH
309 }
310 case TargetLowering::Expand:
Chandler Carruthc1bedac2014-07-02 06:23:34 +0000311 Result = Expand(Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000312 }
313
314 // Make sure that the generated code is itself legal.
315 if (Result != Op) {
316 Result = LegalizeOp(Result);
317 Changed = true;
318 }
319
320 // Note that LegalizeOp may be reentered even from single-use nodes, which
321 // means that we always must cache transformed nodes.
322 AddLegalizedOperand(Op, Result);
323 return Result;
324}
325
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000326SDValue VectorLegalizer::Promote(SDValue Op) {
Chandler Carruth2746c282014-07-02 03:07:15 +0000327 // For a few operations there is a specific concept for promotion based on
328 // the operand's type.
329 switch (Op.getOpcode()) {
330 case ISD::SINT_TO_FP:
331 case ISD::UINT_TO_FP:
332 // "Promote" the operation by extending the operand.
333 return PromoteINT_TO_FP(Op);
Chandler Carruth2746c282014-07-02 03:07:15 +0000334 case ISD::FP_TO_UINT:
335 case ISD::FP_TO_SINT:
336 // Promote the operation by extending the operand.
337 return PromoteFP_TO_INT(Op, Op->getOpcode() == ISD::FP_TO_SINT);
Chandler Carruth2746c282014-07-02 03:07:15 +0000338 }
339
340 // The rest of the time, vector "promotion" is basically just bitcasting and
341 // doing the operation in a different type. For example, x86 promotes
342 // ISD::AND on v2i32 to v1i64.
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000343 MVT VT = Op.getSimpleValueType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000344 assert(Op.getNode()->getNumValues() == 1 &&
345 "Can't promote a vector with multiple results!");
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000346 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000347 SDLoc dl(Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000348 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
349
350 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
351 if (Op.getOperand(j).getValueType().isVector())
Wesley Peck527da1b2010-11-23 03:31:01 +0000352 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
Eli Friedmanda90dd62009-05-23 12:35:30 +0000353 else
354 Operands[j] = Op.getOperand(j);
355 }
356
Craig Topper48d114b2014-04-26 18:35:24 +0000357 Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000358
Wesley Peck527da1b2010-11-23 03:31:01 +0000359 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000360}
361
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000362SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) {
Jim Grosbache0c10d82012-06-28 21:03:44 +0000363 // INT_TO_FP operations may require the input operand be promoted even
364 // when the type is otherwise legal.
365 EVT VT = Op.getOperand(0).getValueType();
366 assert(Op.getNode()->getNumValues() == 1 &&
367 "Can't promote a vector with multiple results!");
368
369 // Normal getTypeToPromoteTo() doesn't work here, as that will promote
370 // by widening the vector w/ the same element width and twice the number
371 // of elements. We want the other way around, the same number of elements,
372 // each twice the width.
373 //
374 // Increase the bitwidth of the element to the next pow-of-two
375 // (which is greater than 8 bits).
Jim Grosbache0c10d82012-06-28 21:03:44 +0000376
Adam Nemet24381f12014-03-17 17:06:14 +0000377 EVT NVT = VT.widenIntegerVectorElementType(*DAG.getContext());
378 assert(NVT.isSimple() && "Promoting to a non-simple vector type!");
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000379 SDLoc dl(Op);
Jim Grosbache0c10d82012-06-28 21:03:44 +0000380 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
381
382 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
383 ISD::SIGN_EXTEND;
384 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
385 if (Op.getOperand(j).getValueType().isVector())
386 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
387 else
388 Operands[j] = Op.getOperand(j);
389 }
390
Craig Topper48d114b2014-04-26 18:35:24 +0000391 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands);
Jim Grosbache0c10d82012-06-28 21:03:44 +0000392}
393
Adam Nemet24381f12014-03-17 17:06:14 +0000394// For FP_TO_INT we promote the result type to a vector type with wider
395// elements and then truncate the result. This is different from the default
396// PromoteVector which uses bitcast to promote thus assumning that the
397// promoted vector type has the same overall size.
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000398SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op, bool isSigned) {
Adam Nemet24381f12014-03-17 17:06:14 +0000399 assert(Op.getNode()->getNumValues() == 1 &&
400 "Can't promote a vector with multiple results!");
401 EVT VT = Op.getValueType();
402
403 EVT NewVT;
404 unsigned NewOpc;
405 while (1) {
406 NewVT = VT.widenIntegerVectorElementType(*DAG.getContext());
407 assert(NewVT.isSimple() && "Promoting to a non-simple vector type!");
408 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewVT)) {
409 NewOpc = ISD::FP_TO_SINT;
410 break;
411 }
412 if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewVT)) {
413 NewOpc = ISD::FP_TO_UINT;
414 break;
415 }
416 }
417
418 SDLoc loc(Op);
419 SDValue promoted = DAG.getNode(NewOpc, SDLoc(Op), NewVT, Op.getOperand(0));
420 return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT, promoted);
421}
422
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000423
424SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000425 SDLoc dl(Op);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000426 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
427 SDValue Chain = LD->getChain();
428 SDValue BasePTR = LD->getBasePtr();
429 EVT SrcVT = LD->getMemoryVT();
Nadav Rotem75c22292011-10-18 22:32:43 +0000430 ISD::LoadExtType ExtType = LD->getExtensionType();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000431
Michael Liao7fb39662013-02-20 18:04:21 +0000432 SmallVector<SDValue, 8> Vals;
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000433 SmallVector<SDValue, 8> LoadChains;
434 unsigned NumElem = SrcVT.getVectorNumElements();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000435
Michael Liao7fb39662013-02-20 18:04:21 +0000436 EVT SrcEltVT = SrcVT.getScalarType();
437 EVT DstEltVT = Op.getNode()->getValueType(0).getScalarType();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000438
Michael Liao7fb39662013-02-20 18:04:21 +0000439 if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) {
440 // When elements in a vector is not byte-addressable, we cannot directly
441 // load each element by advancing pointer, which could only address bytes.
442 // Instead, we load all significant words, mask bits off, and concatenate
443 // them to form each element. Finally, they are extended to destination
444 // scalar type to build the destination vector.
445 EVT WideVT = TLI.getPointerTy();
Nadav Rotem75c22292011-10-18 22:32:43 +0000446
Michael Liao7fb39662013-02-20 18:04:21 +0000447 assert(WideVT.isRound() &&
448 "Could not handle the sophisticated case when the widest integer is"
449 " not power of 2.");
450 assert(WideVT.bitsGE(SrcEltVT) &&
451 "Type is not legalized?");
452
453 unsigned WideBytes = WideVT.getStoreSize();
454 unsigned Offset = 0;
455 unsigned RemainingBytes = SrcVT.getStoreSize();
456 SmallVector<SDValue, 8> LoadVals;
457
458 while (RemainingBytes > 0) {
459 SDValue ScalarLoad;
460 unsigned LoadBytes = WideBytes;
461
462 if (RemainingBytes >= LoadBytes) {
463 ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR,
464 LD->getPointerInfo().getWithOffset(Offset),
465 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000466 LD->isInvariant(), LD->getAlignment(),
467 LD->getTBAAInfo());
Michael Liao7fb39662013-02-20 18:04:21 +0000468 } else {
469 EVT LoadVT = WideVT;
470 while (RemainingBytes < LoadBytes) {
471 LoadBytes >>= 1; // Reduce the load size by half.
472 LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3);
473 }
474 ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
475 LD->getPointerInfo().getWithOffset(Offset),
476 LoadVT, LD->isVolatile(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000477 LD->isNonTemporal(), LD->getAlignment(),
478 LD->getTBAAInfo());
Michael Liao7fb39662013-02-20 18:04:21 +0000479 }
480
481 RemainingBytes -= LoadBytes;
482 Offset += LoadBytes;
483 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
Tom Stellard838e2342013-08-26 15:06:10 +0000484 DAG.getConstant(LoadBytes, BasePTR.getValueType()));
Michael Liao7fb39662013-02-20 18:04:21 +0000485
486 LoadVals.push_back(ScalarLoad.getValue(0));
487 LoadChains.push_back(ScalarLoad.getValue(1));
488 }
489
490 // Extract bits, pack and extend/trunc them into destination type.
491 unsigned SrcEltBits = SrcEltVT.getSizeInBits();
492 SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, WideVT);
493
494 unsigned BitOffset = 0;
495 unsigned WideIdx = 0;
496 unsigned WideBits = WideVT.getSizeInBits();
497
498 for (unsigned Idx = 0; Idx != NumElem; ++Idx) {
499 SDValue Lo, Hi, ShAmt;
500
501 if (BitOffset < WideBits) {
502 ShAmt = DAG.getConstant(BitOffset, TLI.getShiftAmountTy(WideVT));
503 Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt);
504 Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask);
505 }
506
507 BitOffset += SrcEltBits;
508 if (BitOffset >= WideBits) {
509 WideIdx++;
510 Offset -= WideBits;
511 if (Offset > 0) {
512 ShAmt = DAG.getConstant(SrcEltBits - Offset,
513 TLI.getShiftAmountTy(WideVT));
514 Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt);
515 Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask);
516 }
517 }
518
519 if (Hi.getNode())
520 Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi);
521
522 switch (ExtType) {
523 default: llvm_unreachable("Unknown extended-load op!");
524 case ISD::EXTLOAD:
525 Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
526 break;
527 case ISD::ZEXTLOAD:
528 Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
529 break;
530 case ISD::SEXTLOAD:
531 ShAmt = DAG.getConstant(WideBits - SrcEltBits,
532 TLI.getShiftAmountTy(WideVT));
533 Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt);
534 Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt);
535 Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
536 break;
537 }
538 Vals.push_back(Lo);
539 }
540 } else {
541 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
542
543 for (unsigned Idx=0; Idx<NumElem; Idx++) {
544 SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
545 Op.getNode()->getValueType(0).getScalarType(),
546 Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
547 SrcVT.getScalarType(),
548 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000549 LD->getAlignment(), LD->getTBAAInfo());
Michael Liao7fb39662013-02-20 18:04:21 +0000550
551 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
Tom Stellard838e2342013-08-26 15:06:10 +0000552 DAG.getConstant(Stride, BasePTR.getValueType()));
Michael Liao7fb39662013-02-20 18:04:21 +0000553
554 Vals.push_back(ScalarLoad.getValue(0));
555 LoadChains.push_back(ScalarLoad.getValue(1));
556 }
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000557 }
Nadav Rotem75c22292011-10-18 22:32:43 +0000558
Craig Topper48d114b2014-04-26 18:35:24 +0000559 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000560 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +0000561 Op.getNode()->getValueType(0), Vals);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000562
563 AddLegalizedOperand(Op.getValue(0), Value);
564 AddLegalizedOperand(Op.getValue(1), NewChain);
565
566 return (Op.getResNo() ? NewChain : Value);
567}
568
569SDValue VectorLegalizer::ExpandStore(SDValue Op) {
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000570 SDLoc dl(Op);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000571 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
572 SDValue Chain = ST->getChain();
573 SDValue BasePTR = ST->getBasePtr();
574 SDValue Value = ST->getValue();
575 EVT StVT = ST->getMemoryVT();
576
577 unsigned Alignment = ST->getAlignment();
578 bool isVolatile = ST->isVolatile();
579 bool isNonTemporal = ST->isNonTemporal();
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000580 const MDNode *TBAAInfo = ST->getTBAAInfo();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000581
582 unsigned NumElem = StVT.getVectorNumElements();
583 // The type of the data we want to save
584 EVT RegVT = Value.getValueType();
585 EVT RegSclVT = RegVT.getScalarType();
586 // The type of data as saved in memory.
587 EVT MemSclVT = StVT.getScalarType();
588
589 // Cast floats into integers
590 unsigned ScalarSize = MemSclVT.getSizeInBits();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000591
592 // Round odd types to the next pow of two.
593 if (!isPowerOf2_32(ScalarSize))
594 ScalarSize = NextPowerOf2(ScalarSize);
595
596 // Store Stride in bytes
597 unsigned Stride = ScalarSize/8;
598 // Extract each of the elements from the original vector
599 // and save them into memory individually.
600 SmallVector<SDValue, 8> Stores;
601 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
602 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Tom Stellardd42c5942013-08-05 22:22:01 +0000603 RegSclVT, Value, DAG.getConstant(Idx, TLI.getVectorIdxTy()));
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000604
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000605 // This scalar TruncStore may be illegal, but we legalize it later.
606 SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
607 ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000608 isVolatile, isNonTemporal, Alignment, TBAAInfo);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000609
Nadav Rotem75c22292011-10-18 22:32:43 +0000610 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
Tom Stellard838e2342013-08-26 15:06:10 +0000611 DAG.getConstant(Stride, BasePTR.getValueType()));
Nadav Rotem75c22292011-10-18 22:32:43 +0000612
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000613 Stores.push_back(Store);
614 }
Craig Topper48d114b2014-04-26 18:35:24 +0000615 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000616 AddLegalizedOperand(Op, TF);
617 return TF;
618}
619
Chandler Carruthc1bedac2014-07-02 06:23:34 +0000620SDValue VectorLegalizer::Expand(SDValue Op) {
621 switch (Op->getOpcode()) {
622 case ISD::SIGN_EXTEND_INREG:
623 return ExpandSEXTINREG(Op);
Chandler Carruthafe4b252014-07-09 10:58:18 +0000624 case ISD::ZERO_EXTEND_VECTOR_INREG:
625 return ExpandZERO_EXTEND_VECTOR_INREG(Op);
Chandler Carruthc1bedac2014-07-02 06:23:34 +0000626 case ISD::BSWAP:
627 return ExpandBSWAP(Op);
628 case ISD::VSELECT:
629 return ExpandVSELECT(Op);
630 case ISD::SELECT:
631 return ExpandSELECT(Op);
632 case ISD::UINT_TO_FP:
633 return ExpandUINT_TO_FLOAT(Op);
634 case ISD::FNEG:
635 return ExpandFNEG(Op);
636 case ISD::SETCC:
637 return UnrollVSETCC(Op);
638 default:
639 return DAG.UnrollVectorOp(Op.getNode());
640 }
641}
642
Nadav Rotemea973bd2012-08-30 19:17:29 +0000643SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
644 // Lower a select instruction where the condition is a scalar and the
645 // operands are vectors. Lower this select to VSELECT and implement it
Stephen Lincfe7f352013-07-08 00:37:03 +0000646 // using XOR AND OR. The selector bit is broadcasted.
Nadav Rotemea973bd2012-08-30 19:17:29 +0000647 EVT VT = Op.getValueType();
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000648 SDLoc DL(Op);
Nadav Rotemea973bd2012-08-30 19:17:29 +0000649
650 SDValue Mask = Op.getOperand(0);
651 SDValue Op1 = Op.getOperand(1);
652 SDValue Op2 = Op.getOperand(2);
653
654 assert(VT.isVector() && !Mask.getValueType().isVector()
655 && Op1.getValueType() == Op2.getValueType() && "Invalid type");
656
657 unsigned NumElem = VT.getVectorNumElements();
658
659 // If we can't even use the basic vector operations of
660 // AND,OR,XOR, we will have to scalarize the op.
661 // Notice that the operation may be 'promoted' which means that it is
662 // 'bitcasted' to another type which is handled.
663 // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
664 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
665 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
666 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
667 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand)
668 return DAG.UnrollVectorOp(Op.getNode());
669
670 // Generate a mask operand.
Matt Arsenaultd2322222013-09-10 00:41:56 +0000671 EVT MaskTy = VT.changeVectorElementTypeToInteger();
Nadav Rotemea973bd2012-08-30 19:17:29 +0000672
673 // What is the size of each element in the vector mask.
674 EVT BitTy = MaskTy.getScalarType();
675
Matt Arsenaultd2f03322013-06-14 22:04:37 +0000676 Mask = DAG.getSelect(DL, BitTy, Mask,
Nadav Rotem500d6912012-09-02 08:20:07 +0000677 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy),
Nadav Rotem10f6b882012-09-02 12:21:50 +0000678 DAG.getConstant(0, BitTy));
Nadav Rotemea973bd2012-08-30 19:17:29 +0000679
680 // Broadcast the mask so that the entire vector is all-one or all zero.
681 SmallVector<SDValue, 8> Ops(NumElem, Mask);
Craig Topper48d114b2014-04-26 18:35:24 +0000682 Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, Ops);
Nadav Rotemea973bd2012-08-30 19:17:29 +0000683
684 // Bitcast the operands to be the same type as the mask.
685 // This is needed when we select between FP types because
686 // the mask is a vector of integers.
687 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
688 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
689
690 SDValue AllOnes = DAG.getConstant(
691 APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy);
692 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
693
694 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
695 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
696 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
697 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
698}
699
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000700SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) {
701 EVT VT = Op.getValueType();
702
Benjamin Kramer5ea03492013-01-12 19:06:44 +0000703 // Make sure that the SRA and SHL instructions are available.
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000704 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
Benjamin Kramer5ea03492013-01-12 19:06:44 +0000705 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000706 return DAG.UnrollVectorOp(Op.getNode());
707
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000708 SDLoc DL(Op);
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000709 EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT();
710
711 unsigned BW = VT.getScalarType().getSizeInBits();
712 unsigned OrigBW = OrigTy.getScalarType().getSizeInBits();
713 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, VT);
714
715 Op = Op.getOperand(0);
Benjamin Kramer5ea03492013-01-12 19:06:44 +0000716 Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz);
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000717 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
718}
719
Chandler Carruthafe4b252014-07-09 10:58:18 +0000720// Generically expand a vector zext in register to a shuffle of the relevant
721// lanes into the appropriate locations, a blend of zero into the high bits,
722// and a bitcast to the wider element type.
723SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDValue Op) {
724 SDLoc DL(Op);
725 EVT VT = Op.getValueType();
726 int NumElements = VT.getVectorNumElements();
727 SDValue Src = Op.getOperand(0);
728 EVT SrcVT = Src.getValueType();
729 int NumSrcElements = SrcVT.getVectorNumElements();
730
731 // Build up a zero vector to blend into this one.
732 EVT SrcScalarVT = SrcVT.getScalarType();
733 SDValue ScalarZero = DAG.getTargetConstant(0, SrcScalarVT);
734 SmallVector<SDValue, 4> BuildVectorOperands(NumSrcElements, ScalarZero);
735 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, DL, SrcVT, BuildVectorOperands);
736
737 // Shuffle the incoming lanes into the correct position, and pull all other
738 // lanes from the zero vector.
739 SmallVector<int, 16> ShuffleMask;
740 ShuffleMask.reserve(NumSrcElements);
741 for (int i = 0; i < NumSrcElements; ++i)
742 ShuffleMask.push_back(i);
743
744 int ExtLaneScale = NumSrcElements / NumElements;
745 int EndianOffset = TLI.isBigEndian() ? ExtLaneScale - 1 : 0;
746 for (int i = 0; i < NumElements; ++i)
747 ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
748
749 return DAG.getNode(ISD::BITCAST, DL, VT,
750 DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
751}
752
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000753SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) {
754 EVT VT = Op.getValueType();
755
756 // Generate a byte wise shuffle mask for the BSWAP.
757 SmallVector<int, 16> ShuffleMask;
758 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
759 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
760 for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
761 ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
762
763 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
764
765 // Only emit a shuffle if the mask is legal.
766 if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT))
767 return DAG.UnrollVectorOp(Op.getNode());
768
769 SDLoc DL(Op);
770 Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0));
771 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
772 ShuffleMask.data());
773 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
774}
775
Nadav Rotem52202fb2011-09-13 19:17:42 +0000776SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
777 // Implement VSELECT in terms of XOR, AND, OR
778 // on platforms which do not support blend natively.
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000779 SDLoc DL(Op);
Nadav Rotem52202fb2011-09-13 19:17:42 +0000780
781 SDValue Mask = Op.getOperand(0);
782 SDValue Op1 = Op.getOperand(1);
783 SDValue Op2 = Op.getOperand(2);
784
Matt Arsenaulta5733dc2013-05-07 20:24:18 +0000785 EVT VT = Mask.getValueType();
786
Nadav Rotem52202fb2011-09-13 19:17:42 +0000787 // If we can't even use the basic vector operations of
788 // AND,OR,XOR, we will have to scalarize the op.
Nadav Rotem88244722011-10-19 20:43:16 +0000789 // Notice that the operation may be 'promoted' which means that it is
790 // 'bitcasted' to another type which is handled.
Pete Cooper2455e9c2012-09-01 22:27:48 +0000791 // This operation also isn't safe with AND, OR, XOR when the boolean
792 // type is 0/1 as we need an all ones vector constant to mask with.
793 // FIXME: Sign extend 1 to all ones if thats legal on the target.
Nadav Rotem88244722011-10-19 20:43:16 +0000794 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
795 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
Pete Cooper2455e9c2012-09-01 22:27:48 +0000796 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
797 TLI.getBooleanContents(true) !=
798 TargetLowering::ZeroOrNegativeOneBooleanContent)
Nadav Rotem88244722011-10-19 20:43:16 +0000799 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotem52202fb2011-09-13 19:17:42 +0000800
Matt Arsenaulta5733dc2013-05-07 20:24:18 +0000801 // If the mask and the type are different sizes, unroll the vector op. This
802 // can occur when getSetCCResultType returns something that is different in
803 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
804 if (VT.getSizeInBits() != Op1.getValueType().getSizeInBits())
805 return DAG.UnrollVectorOp(Op.getNode());
806
Nadav Rotem52202fb2011-09-13 19:17:42 +0000807 // Bitcast the operands to be the same type as the mask.
808 // This is needed when we select between FP types because
809 // the mask is a vector of integers.
810 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
811 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
812
813 SDValue AllOnes = DAG.getConstant(
814 APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
815 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
816
817 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
818 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
Nadav Rotem02ef0c32012-04-15 15:08:09 +0000819 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
820 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
Nadav Rotem52202fb2011-09-13 19:17:42 +0000821}
822
Nadav Roteme7a101c2011-03-19 13:09:10 +0000823SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
Nadav Roteme7a101c2011-03-19 13:09:10 +0000824 EVT VT = Op.getOperand(0).getValueType();
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000825 SDLoc DL(Op);
Nadav Roteme7a101c2011-03-19 13:09:10 +0000826
827 // Make sure that the SINT_TO_FP and SRL instructions are available.
Nadav Rotem88244722011-10-19 20:43:16 +0000828 if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
829 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
830 return DAG.UnrollVectorOp(Op.getNode());
Nadav Roteme7a101c2011-03-19 13:09:10 +0000831
832 EVT SVT = VT.getScalarType();
833 assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
834 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
835
836 unsigned BW = SVT.getSizeInBits();
837 SDValue HalfWord = DAG.getConstant(BW/2, VT);
838
839 // Constants to clear the upper part of the word.
840 // Notice that we can also use SHL+SHR, but using a constant is slightly
841 // faster on x86.
842 uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
843 SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
844
845 // Two to the power of half-word-size.
846 SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
847
848 // Clear upper part of LO, lower HI
849 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
850 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
851
852 // Convert hi and lo to floats
853 // Convert the hi part back to the upper values
854 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
855 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
856 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
857
858 // Add the two halves
859 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
860}
861
862
Eli Friedmanda90dd62009-05-23 12:35:30 +0000863SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
864 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
865 SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
Andrew Trickef9de2a2013-05-25 02:42:55 +0000866 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Eli Friedmanda90dd62009-05-23 12:35:30 +0000867 Zero, Op.getOperand(0));
868 }
Mon P Wang32f8bb92009-11-30 02:42:02 +0000869 return DAG.UnrollVectorOp(Op.getNode());
Eli Friedmanda90dd62009-05-23 12:35:30 +0000870}
871
872SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000873 EVT VT = Op.getValueType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000874 unsigned NumElems = VT.getVectorNumElements();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000875 EVT EltVT = VT.getVectorElementType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000876 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000877 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000878 SDLoc dl(Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000879 SmallVector<SDValue, 8> Ops(NumElems);
880 for (unsigned i = 0; i < NumElems; ++i) {
881 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
Tom Stellardd42c5942013-08-05 22:22:01 +0000882 DAG.getConstant(i, TLI.getVectorIdxTy()));
Eli Friedmanda90dd62009-05-23 12:35:30 +0000883 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
Tom Stellardd42c5942013-08-05 22:22:01 +0000884 DAG.getConstant(i, TLI.getVectorIdxTy()));
Matt Arsenault758659232013-05-18 00:21:46 +0000885 Ops[i] = DAG.getNode(ISD::SETCC, dl,
886 TLI.getSetCCResultType(*DAG.getContext(), TmpEltVT),
Eli Friedmanda90dd62009-05-23 12:35:30 +0000887 LHSElem, RHSElem, CC);
Matt Arsenaultd2f03322013-06-14 22:04:37 +0000888 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i],
889 DAG.getConstant(APInt::getAllOnesValue
890 (EltVT.getSizeInBits()), EltVT),
891 DAG.getConstant(0, EltVT));
Eli Friedmanda90dd62009-05-23 12:35:30 +0000892 }
Craig Topper48d114b2014-04-26 18:35:24 +0000893 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000894}
895
Eli Friedmanda90dd62009-05-23 12:35:30 +0000896}
897
898bool SelectionDAG::LegalizeVectors() {
899 return VectorLegalizer(*this).Run();
900}