blob: d104abbd64b3cbaedc55a26378127f21ac0cd475 [file] [log] [blame]
Eli Friedman5c22c802009-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 Friedman509150f2009-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 Friedman5c22c802009-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 Gohman98ca4f22009-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 Friedman5c22c802009-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 Gohmand858e902010-04-17 15:26:15 +000037 const TargetLowering &TLI;
Eli Friedman5c22c802009-05-23 12:35:30 +000038 bool Changed; // Keep track of whether anything changed
39
40 /// LegalizedNodes - For nodes that are of legal width, and that have more
41 /// than one use, this map indicates what regularized operand to use. This
42 /// allows us to avoid legalizing the same thing more than once.
43 DenseMap<SDValue, SDValue> LegalizedNodes;
44
45 // Adds a node to the translation cache
46 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
53 // Legalizes the given node
54 SDValue LegalizeOp(SDValue Op);
55 // Assuming the node is legal, "legalize" the results
56 SDValue TranslateLegalizeResults(SDValue Op, SDValue Result);
Eli Friedman5c22c802009-05-23 12:35:30 +000057 // Implements unrolling a VSETCC.
58 SDValue UnrollVSETCC(SDValue Op);
59 // Implements expansion for FNEG; falls back to UnrollVectorOp if FSUB
60 // isn't legal.
Nadav Rotem06cc3242011-03-19 13:09:10 +000061 // Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
62 // SINT_TO_FLOAT and SHR on vectors isn't legal.
63 SDValue ExpandUINT_TO_FLOAT(SDValue Op);
Nadav Rotemb6266fb2011-09-18 10:29:29 +000064 // Implement vselect in terms of XOR, AND, OR when blend is not supported
65 // by the target.
Nadav Rotemaec58612011-09-13 19:17:42 +000066 SDValue ExpandVSELECT(SDValue Op);
Nadav Roteme757f002012-08-30 19:17:29 +000067 SDValue ExpandSELECT(SDValue Op);
Nadav Roteme9b58d02011-10-15 07:41:10 +000068 SDValue ExpandLoad(SDValue Op);
69 SDValue ExpandStore(SDValue Op);
Eli Friedman5c22c802009-05-23 12:35:30 +000070 SDValue ExpandFNEG(SDValue Op);
71 // Implements vector promotion; this is essentially just bitcasting the
72 // operands to a different type and bitcasting the result back to the
73 // original type.
74 SDValue PromoteVectorOp(SDValue Op);
Jim Grosbach926dc162012-06-28 21:03:44 +000075 // Implements [SU]INT_TO_FP vector promotion; this is a [zs]ext of the input
76 // operand to the next size up.
77 SDValue PromoteVectorOpINT_TO_FP(SDValue Op);
Eli Friedman5c22c802009-05-23 12:35:30 +000078
79 public:
80 bool Run();
81 VectorLegalizer(SelectionDAG& dag) :
82 DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
83};
84
85bool VectorLegalizer::Run() {
86 // The legalize process is inherently a bottom-up recursive process (users
87 // legalize their uses before themselves). Given infinite stack space, we
88 // could just start legalizing on the root and traverse the whole graph. In
89 // practice however, this causes us to run out of stack space on large basic
90 // blocks. To avoid this problem, compute an ordering of the nodes where each
91 // node is only legalized after all of its operands are legalized.
92 DAG.AssignTopologicalOrder();
93 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +000094 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedman5c22c802009-05-23 12:35:30 +000095 LegalizeOp(SDValue(I, 0));
96
97 // Finally, it's possible the root changed. Get the new root.
98 SDValue OldRoot = DAG.getRoot();
99 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
100 DAG.setRoot(LegalizedNodes[OldRoot]);
101
102 LegalizedNodes.clear();
103
104 // Remove dead nodes now.
105 DAG.RemoveDeadNodes();
106
107 return Changed;
108}
109
110SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
111 // Generic legalization: just pass the operand through.
112 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
113 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
114 return Result.getValue(Op.getResNo());
115}
116
117SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
118 // Note that LegalizeOp may be reentered even from single-use nodes, which
119 // means that we always must cache transformed nodes.
120 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
121 if (I != LegalizedNodes.end()) return I->second;
122
123 SDNode* Node = Op.getNode();
124
125 // Legalize the operands
126 SmallVector<SDValue, 8> Ops;
127 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
128 Ops.push_back(LegalizeOp(Node->getOperand(i)));
129
130 SDValue Result =
Dan Gohman027657d2010-06-18 15:30:29 +0000131 SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops.data(), Ops.size()), 0);
Eli Friedman5c22c802009-05-23 12:35:30 +0000132
Nadav Roteme9b58d02011-10-15 07:41:10 +0000133 if (Op.getOpcode() == ISD::LOAD) {
134 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
135 ISD::LoadExtType ExtType = LD->getExtensionType();
136 if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
137 if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
138 return TranslateLegalizeResults(Op, Result);
139 Changed = true;
140 return LegalizeOp(ExpandLoad(Op));
141 }
142 } else if (Op.getOpcode() == ISD::STORE) {
143 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
144 EVT StVT = ST->getMemoryVT();
Patrik Hagglund88ef5142012-12-19 08:28:51 +0000145 MVT ValVT = ST->getValue().getSimpleValueType();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000146 if (StVT.isVector() && ST->isTruncatingStore())
Patrik Hagglund88ef5142012-12-19 08:28:51 +0000147 switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000148 default: llvm_unreachable("This action is not supported yet!");
Nadav Roteme9b58d02011-10-15 07:41:10 +0000149 case TargetLowering::Legal:
150 return TranslateLegalizeResults(Op, Result);
151 case TargetLowering::Custom:
152 Changed = true;
153 return LegalizeOp(TLI.LowerOperation(Result, DAG));
154 case TargetLowering::Expand:
155 Changed = true;
156 return LegalizeOp(ExpandStore(Op));
157 }
158 }
159
Eli Friedman5c22c802009-05-23 12:35:30 +0000160 bool HasVectorValue = false;
161 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
162 J != E;
163 ++J)
164 HasVectorValue |= J->isVector();
165 if (!HasVectorValue)
166 return TranslateLegalizeResults(Op, Result);
167
Owen Andersone50ed302009-08-10 22:56:29 +0000168 EVT QueryType;
Eli Friedman5c22c802009-05-23 12:35:30 +0000169 switch (Op.getOpcode()) {
170 default:
171 return TranslateLegalizeResults(Op, Result);
172 case ISD::ADD:
173 case ISD::SUB:
174 case ISD::MUL:
175 case ISD::SDIV:
176 case ISD::UDIV:
177 case ISD::SREM:
178 case ISD::UREM:
179 case ISD::FADD:
180 case ISD::FSUB:
181 case ISD::FMUL:
182 case ISD::FDIV:
183 case ISD::FREM:
184 case ISD::AND:
185 case ISD::OR:
186 case ISD::XOR:
187 case ISD::SHL:
188 case ISD::SRA:
189 case ISD::SRL:
190 case ISD::ROTL:
191 case ISD::ROTR:
Eli Friedman5c22c802009-05-23 12:35:30 +0000192 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +0000193 case ISD::CTTZ:
194 case ISD::CTLZ_ZERO_UNDEF:
195 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman5c22c802009-05-23 12:35:30 +0000196 case ISD::CTPOP:
197 case ISD::SELECT:
Nadav Rotemaec58612011-09-13 19:17:42 +0000198 case ISD::VSELECT:
Eli Friedman5c22c802009-05-23 12:35:30 +0000199 case ISD::SELECT_CC:
Duncan Sands28b77e92011-09-06 19:07:46 +0000200 case ISD::SETCC:
Eli Friedman5c22c802009-05-23 12:35:30 +0000201 case ISD::ZERO_EXTEND:
202 case ISD::ANY_EXTEND:
203 case ISD::TRUNCATE:
204 case ISD::SIGN_EXTEND:
Eli Friedman5c22c802009-05-23 12:35:30 +0000205 case ISD::FP_TO_SINT:
206 case ISD::FP_TO_UINT:
207 case ISD::FNEG:
208 case ISD::FABS:
209 case ISD::FSQRT:
210 case ISD::FSIN:
211 case ISD::FCOS:
212 case ISD::FPOWI:
213 case ISD::FPOW:
214 case ISD::FLOG:
215 case ISD::FLOG2:
216 case ISD::FLOG10:
217 case ISD::FEXP:
218 case ISD::FEXP2:
219 case ISD::FCEIL:
220 case ISD::FTRUNC:
221 case ISD::FRINT:
222 case ISD::FNEARBYINT:
223 case ISD::FFLOOR:
Eli Friedman846ce8e2012-11-15 22:44:27 +0000224 case ISD::FP_ROUND:
Eli Friedman43147af2012-11-17 01:52:46 +0000225 case ISD::FP_EXTEND:
Craig Topper6b1e1d82012-08-30 07:34:22 +0000226 case ISD::FMA:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +0000227 case ISD::SIGN_EXTEND_INREG:
Eli Friedman556929a2009-06-06 03:27:50 +0000228 QueryType = Node->getValueType(0);
229 break;
Dan Gohmand1996362010-01-09 02:13:55 +0000230 case ISD::FP_ROUND_INREG:
231 QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
232 break;
Eli Friedman556929a2009-06-06 03:27:50 +0000233 case ISD::SINT_TO_FP:
234 case ISD::UINT_TO_FP:
235 QueryType = Node->getOperand(0).getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000236 break;
237 }
238
Eli Friedman556929a2009-06-06 03:27:50 +0000239 switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
Eli Friedman5c22c802009-05-23 12:35:30 +0000240 case TargetLowering::Promote:
Jim Grosbach926dc162012-06-28 21:03:44 +0000241 switch (Op.getOpcode()) {
242 default:
243 // "Promote" the operation by bitcasting
244 Result = PromoteVectorOp(Op);
245 Changed = true;
246 break;
247 case ISD::SINT_TO_FP:
248 case ISD::UINT_TO_FP:
249 // "Promote" the operation by extending the operand.
250 Result = PromoteVectorOpINT_TO_FP(Op);
251 Changed = true;
252 break;
253 }
Eli Friedman5c22c802009-05-23 12:35:30 +0000254 break;
255 case TargetLowering::Legal: break;
256 case TargetLowering::Custom: {
257 SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
258 if (Tmp1.getNode()) {
259 Result = Tmp1;
260 break;
261 }
262 // FALL THROUGH
263 }
264 case TargetLowering::Expand:
Nadav Rotemaec58612011-09-13 19:17:42 +0000265 if (Node->getOpcode() == ISD::VSELECT)
266 Result = ExpandVSELECT(Op);
Nadav Roteme757f002012-08-30 19:17:29 +0000267 else if (Node->getOpcode() == ISD::SELECT)
268 Result = ExpandSELECT(Op);
Nadav Rotemaec58612011-09-13 19:17:42 +0000269 else if (Node->getOpcode() == ISD::UINT_TO_FP)
Nadav Rotem06cc3242011-03-19 13:09:10 +0000270 Result = ExpandUINT_TO_FLOAT(Op);
271 else if (Node->getOpcode() == ISD::FNEG)
Eli Friedman5c22c802009-05-23 12:35:30 +0000272 Result = ExpandFNEG(Op);
Duncan Sands28b77e92011-09-06 19:07:46 +0000273 else if (Node->getOpcode() == ISD::SETCC)
Eli Friedman5c22c802009-05-23 12:35:30 +0000274 Result = UnrollVSETCC(Op);
275 else
Mon P Wangcd6e7252009-11-30 02:42:02 +0000276 Result = DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000277 break;
278 }
279
280 // Make sure that the generated code is itself legal.
281 if (Result != Op) {
282 Result = LegalizeOp(Result);
283 Changed = true;
284 }
285
286 // Note that LegalizeOp may be reentered even from single-use nodes, which
287 // means that we always must cache transformed nodes.
288 AddLegalizedOperand(Op, Result);
289 return Result;
290}
291
292SDValue VectorLegalizer::PromoteVectorOp(SDValue Op) {
Eli Friedmanc046c002009-05-24 20:32:10 +0000293 // Vector "promotion" is basically just bitcasting and doing the operation
294 // in a different type. For example, x86 promotes ISD::AND on v2i32 to
295 // v1i64.
Patrik Hagglund34525f92012-12-11 11:14:33 +0000296 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000297 assert(Op.getNode()->getNumValues() == 1 &&
298 "Can't promote a vector with multiple results!");
Patrik Hagglund34525f92012-12-11 11:14:33 +0000299 EVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
Eli Friedman5c22c802009-05-23 12:35:30 +0000300 DebugLoc dl = Op.getDebugLoc();
301 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
302
303 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
304 if (Op.getOperand(j).getValueType().isVector())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000305 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
Eli Friedman5c22c802009-05-23 12:35:30 +0000306 else
307 Operands[j] = Op.getOperand(j);
308 }
309
310 Op = DAG.getNode(Op.getOpcode(), dl, NVT, &Operands[0], Operands.size());
311
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000312 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Eli Friedman5c22c802009-05-23 12:35:30 +0000313}
314
Jim Grosbach926dc162012-06-28 21:03:44 +0000315SDValue VectorLegalizer::PromoteVectorOpINT_TO_FP(SDValue Op) {
316 // INT_TO_FP operations may require the input operand be promoted even
317 // when the type is otherwise legal.
318 EVT VT = Op.getOperand(0).getValueType();
319 assert(Op.getNode()->getNumValues() == 1 &&
320 "Can't promote a vector with multiple results!");
321
322 // Normal getTypeToPromoteTo() doesn't work here, as that will promote
323 // by widening the vector w/ the same element width and twice the number
324 // of elements. We want the other way around, the same number of elements,
325 // each twice the width.
326 //
327 // Increase the bitwidth of the element to the next pow-of-two
328 // (which is greater than 8 bits).
329 unsigned NumElts = VT.getVectorNumElements();
330 EVT EltVT = VT.getVectorElementType();
331 EltVT = EVT::getIntegerVT(*DAG.getContext(), 2 * EltVT.getSizeInBits());
332 assert(EltVT.isSimple() && "Promoting to a non-simple vector type!");
333
334 // Build a new vector type and check if it is legal.
335 MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
336
337 DebugLoc dl = Op.getDebugLoc();
338 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
339
340 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
341 ISD::SIGN_EXTEND;
342 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
343 if (Op.getOperand(j).getValueType().isVector())
344 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
345 else
346 Operands[j] = Op.getOperand(j);
347 }
348
349 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), &Operands[0],
350 Operands.size());
351}
352
Nadav Roteme9b58d02011-10-15 07:41:10 +0000353
354SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
355 DebugLoc dl = Op.getDebugLoc();
356 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
357 SDValue Chain = LD->getChain();
358 SDValue BasePTR = LD->getBasePtr();
359 EVT SrcVT = LD->getMemoryVT();
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000360 ISD::LoadExtType ExtType = LD->getExtensionType();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000361
362 SmallVector<SDValue, 8> LoadVals;
363 SmallVector<SDValue, 8> LoadChains;
364 unsigned NumElem = SrcVT.getVectorNumElements();
365 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
366
367 for (unsigned Idx=0; Idx<NumElem; Idx++) {
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000368 SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
Nadav Roteme9b58d02011-10-15 07:41:10 +0000369 Op.getNode()->getValueType(0).getScalarType(),
370 Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
371 SrcVT.getScalarType(),
372 LD->isVolatile(), LD->isNonTemporal(),
373 LD->getAlignment());
374
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000375 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
376 DAG.getIntPtrConstant(Stride));
377
Nadav Roteme9b58d02011-10-15 07:41:10 +0000378 LoadVals.push_back(ScalarLoad.getValue(0));
379 LoadChains.push_back(ScalarLoad.getValue(1));
380 }
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000381
Nadav Roteme9b58d02011-10-15 07:41:10 +0000382 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
383 &LoadChains[0], LoadChains.size());
384 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
385 Op.getNode()->getValueType(0), &LoadVals[0], LoadVals.size());
386
387 AddLegalizedOperand(Op.getValue(0), Value);
388 AddLegalizedOperand(Op.getValue(1), NewChain);
389
390 return (Op.getResNo() ? NewChain : Value);
391}
392
393SDValue VectorLegalizer::ExpandStore(SDValue Op) {
394 DebugLoc dl = Op.getDebugLoc();
395 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
396 SDValue Chain = ST->getChain();
397 SDValue BasePTR = ST->getBasePtr();
398 SDValue Value = ST->getValue();
399 EVT StVT = ST->getMemoryVT();
400
401 unsigned Alignment = ST->getAlignment();
402 bool isVolatile = ST->isVolatile();
403 bool isNonTemporal = ST->isNonTemporal();
404
405 unsigned NumElem = StVT.getVectorNumElements();
406 // The type of the data we want to save
407 EVT RegVT = Value.getValueType();
408 EVT RegSclVT = RegVT.getScalarType();
409 // The type of data as saved in memory.
410 EVT MemSclVT = StVT.getScalarType();
411
412 // Cast floats into integers
413 unsigned ScalarSize = MemSclVT.getSizeInBits();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000414
415 // Round odd types to the next pow of two.
416 if (!isPowerOf2_32(ScalarSize))
417 ScalarSize = NextPowerOf2(ScalarSize);
418
419 // Store Stride in bytes
420 unsigned Stride = ScalarSize/8;
421 // Extract each of the elements from the original vector
422 // and save them into memory individually.
423 SmallVector<SDValue, 8> Stores;
424 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
425 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
426 RegSclVT, Value, DAG.getIntPtrConstant(Idx));
427
Nadav Roteme9b58d02011-10-15 07:41:10 +0000428 // This scalar TruncStore may be illegal, but we legalize it later.
429 SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
430 ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
431 isVolatile, isNonTemporal, Alignment);
432
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000433 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
434 DAG.getIntPtrConstant(Stride));
435
Nadav Roteme9b58d02011-10-15 07:41:10 +0000436 Stores.push_back(Store);
437 }
438 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
439 &Stores[0], Stores.size());
440 AddLegalizedOperand(Op, TF);
441 return TF;
442}
443
Nadav Roteme757f002012-08-30 19:17:29 +0000444SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
445 // Lower a select instruction where the condition is a scalar and the
446 // operands are vectors. Lower this select to VSELECT and implement it
447 // using XOR AND OR. The selector bit is broadcasted.
448 EVT VT = Op.getValueType();
449 DebugLoc DL = Op.getDebugLoc();
450
451 SDValue Mask = Op.getOperand(0);
452 SDValue Op1 = Op.getOperand(1);
453 SDValue Op2 = Op.getOperand(2);
454
455 assert(VT.isVector() && !Mask.getValueType().isVector()
456 && Op1.getValueType() == Op2.getValueType() && "Invalid type");
457
458 unsigned NumElem = VT.getVectorNumElements();
459
460 // If we can't even use the basic vector operations of
461 // AND,OR,XOR, we will have to scalarize the op.
462 // Notice that the operation may be 'promoted' which means that it is
463 // 'bitcasted' to another type which is handled.
464 // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
465 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
466 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
467 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
468 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand)
469 return DAG.UnrollVectorOp(Op.getNode());
470
471 // Generate a mask operand.
472 EVT MaskTy = TLI.getSetCCResultType(VT);
473 assert(MaskTy.isVector() && "Invalid CC type");
474 assert(MaskTy.getSizeInBits() == Op1.getValueType().getSizeInBits()
475 && "Invalid mask size");
476
477 // What is the size of each element in the vector mask.
478 EVT BitTy = MaskTy.getScalarType();
479
Nadav Rotemf55ef642012-09-02 08:20:07 +0000480 Mask = DAG.getNode(ISD::SELECT, DL, BitTy, Mask,
481 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy),
Nadav Rotemee77da62012-09-02 12:21:50 +0000482 DAG.getConstant(0, BitTy));
Nadav Roteme757f002012-08-30 19:17:29 +0000483
484 // Broadcast the mask so that the entire vector is all-one or all zero.
485 SmallVector<SDValue, 8> Ops(NumElem, Mask);
486 Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, &Ops[0], Ops.size());
487
488 // Bitcast the operands to be the same type as the mask.
489 // This is needed when we select between FP types because
490 // the mask is a vector of integers.
491 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
492 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
493
494 SDValue AllOnes = DAG.getConstant(
495 APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy);
496 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
497
498 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
499 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
500 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
501 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
502}
503
Nadav Rotemaec58612011-09-13 19:17:42 +0000504SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
505 // Implement VSELECT in terms of XOR, AND, OR
506 // on platforms which do not support blend natively.
507 EVT VT = Op.getOperand(0).getValueType();
Nadav Rotemaec58612011-09-13 19:17:42 +0000508 DebugLoc DL = Op.getDebugLoc();
509
510 SDValue Mask = Op.getOperand(0);
511 SDValue Op1 = Op.getOperand(1);
512 SDValue Op2 = Op.getOperand(2);
513
514 // If we can't even use the basic vector operations of
515 // AND,OR,XOR, we will have to scalarize the op.
Nadav Rotem815af822011-10-19 20:43:16 +0000516 // Notice that the operation may be 'promoted' which means that it is
517 // 'bitcasted' to another type which is handled.
Pete Cooperd9060172012-09-01 22:27:48 +0000518 // This operation also isn't safe with AND, OR, XOR when the boolean
519 // type is 0/1 as we need an all ones vector constant to mask with.
520 // FIXME: Sign extend 1 to all ones if thats legal on the target.
Nadav Rotem815af822011-10-19 20:43:16 +0000521 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
522 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
Pete Cooperd9060172012-09-01 22:27:48 +0000523 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
524 TLI.getBooleanContents(true) !=
525 TargetLowering::ZeroOrNegativeOneBooleanContent)
Nadav Rotem815af822011-10-19 20:43:16 +0000526 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotemaec58612011-09-13 19:17:42 +0000527
Nadav Roteme757f002012-08-30 19:17:29 +0000528 assert(VT.getSizeInBits() == Op1.getValueType().getSizeInBits()
Duncan Sands17001ce2011-10-18 12:44:00 +0000529 && "Invalid mask size");
Nadav Rotemaec58612011-09-13 19:17:42 +0000530 // Bitcast the operands to be the same type as the mask.
531 // This is needed when we select between FP types because
532 // the mask is a vector of integers.
533 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
534 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
535
536 SDValue AllOnes = DAG.getConstant(
537 APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
538 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
539
540 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
541 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
Nadav Rotem3ab32ea2012-04-15 15:08:09 +0000542 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
543 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
Nadav Rotemaec58612011-09-13 19:17:42 +0000544}
545
Nadav Rotem06cc3242011-03-19 13:09:10 +0000546SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
Nadav Rotem06cc3242011-03-19 13:09:10 +0000547 EVT VT = Op.getOperand(0).getValueType();
548 DebugLoc DL = Op.getDebugLoc();
549
550 // Make sure that the SINT_TO_FP and SRL instructions are available.
Nadav Rotem815af822011-10-19 20:43:16 +0000551 if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
552 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
553 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotem06cc3242011-03-19 13:09:10 +0000554
555 EVT SVT = VT.getScalarType();
556 assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
557 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
558
559 unsigned BW = SVT.getSizeInBits();
560 SDValue HalfWord = DAG.getConstant(BW/2, VT);
561
562 // Constants to clear the upper part of the word.
563 // Notice that we can also use SHL+SHR, but using a constant is slightly
564 // faster on x86.
565 uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
566 SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
567
568 // Two to the power of half-word-size.
569 SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
570
571 // Clear upper part of LO, lower HI
572 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
573 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
574
575 // Convert hi and lo to floats
576 // Convert the hi part back to the upper values
577 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
578 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
579 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
580
581 // Add the two halves
582 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
583}
584
585
Eli Friedman5c22c802009-05-23 12:35:30 +0000586SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
587 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
588 SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
589 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
590 Zero, Op.getOperand(0));
591 }
Mon P Wangcd6e7252009-11-30 02:42:02 +0000592 return DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000593}
594
595SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +0000596 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000597 unsigned NumElems = VT.getVectorNumElements();
Owen Andersone50ed302009-08-10 22:56:29 +0000598 EVT EltVT = VT.getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000599 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +0000600 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000601 DebugLoc dl = Op.getDebugLoc();
602 SmallVector<SDValue, 8> Ops(NumElems);
603 for (unsigned i = 0; i < NumElems; ++i) {
604 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
605 DAG.getIntPtrConstant(i));
606 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
607 DAG.getIntPtrConstant(i));
608 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
609 LHSElem, RHSElem, CC);
610 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
611 DAG.getConstant(APInt::getAllOnesValue
612 (EltVT.getSizeInBits()), EltVT),
613 DAG.getConstant(0, EltVT));
614 }
615 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
616}
617
Eli Friedman5c22c802009-05-23 12:35:30 +0000618}
619
620bool SelectionDAG::LegalizeVectors() {
621 return VectorLegalizer(*this).Run();
622}