blob: a5b322a5a112405fe8ffd1291741f696bc606c5a [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 Roteme9b58d02011-10-15 07:41:10 +000067 SDValue ExpandLoad(SDValue Op);
68 SDValue ExpandStore(SDValue Op);
Eli Friedman5c22c802009-05-23 12:35:30 +000069 SDValue ExpandFNEG(SDValue Op);
70 // Implements vector promotion; this is essentially just bitcasting the
71 // operands to a different type and bitcasting the result back to the
72 // original type.
73 SDValue PromoteVectorOp(SDValue Op);
Jim Grosbach926dc162012-06-28 21:03:44 +000074 // Implements [SU]INT_TO_FP vector promotion; this is a [zs]ext of the input
75 // operand to the next size up.
76 SDValue PromoteVectorOpINT_TO_FP(SDValue Op);
Eli Friedman5c22c802009-05-23 12:35:30 +000077
78 public:
79 bool Run();
80 VectorLegalizer(SelectionDAG& dag) :
81 DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
82};
83
84bool VectorLegalizer::Run() {
85 // The legalize process is inherently a bottom-up recursive process (users
86 // legalize their uses before themselves). Given infinite stack space, we
87 // could just start legalizing on the root and traverse the whole graph. In
88 // practice however, this causes us to run out of stack space on large basic
89 // blocks. To avoid this problem, compute an ordering of the nodes where each
90 // node is only legalized after all of its operands are legalized.
91 DAG.AssignTopologicalOrder();
92 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +000093 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedman5c22c802009-05-23 12:35:30 +000094 LegalizeOp(SDValue(I, 0));
95
96 // Finally, it's possible the root changed. Get the new root.
97 SDValue OldRoot = DAG.getRoot();
98 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
99 DAG.setRoot(LegalizedNodes[OldRoot]);
100
101 LegalizedNodes.clear();
102
103 // Remove dead nodes now.
104 DAG.RemoveDeadNodes();
105
106 return Changed;
107}
108
109SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
110 // Generic legalization: just pass the operand through.
111 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
112 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
113 return Result.getValue(Op.getResNo());
114}
115
116SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
117 // Note that LegalizeOp may be reentered even from single-use nodes, which
118 // means that we always must cache transformed nodes.
119 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
120 if (I != LegalizedNodes.end()) return I->second;
121
122 SDNode* Node = Op.getNode();
123
124 // Legalize the operands
125 SmallVector<SDValue, 8> Ops;
126 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
127 Ops.push_back(LegalizeOp(Node->getOperand(i)));
128
129 SDValue Result =
Dan Gohman027657d2010-06-18 15:30:29 +0000130 SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops.data(), Ops.size()), 0);
Eli Friedman5c22c802009-05-23 12:35:30 +0000131
Nadav Roteme9b58d02011-10-15 07:41:10 +0000132 if (Op.getOpcode() == ISD::LOAD) {
133 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
134 ISD::LoadExtType ExtType = LD->getExtensionType();
135 if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
136 if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
137 return TranslateLegalizeResults(Op, Result);
138 Changed = true;
139 return LegalizeOp(ExpandLoad(Op));
140 }
141 } else if (Op.getOpcode() == ISD::STORE) {
142 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
143 EVT StVT = ST->getMemoryVT();
144 EVT ValVT = ST->getValue().getValueType();
145 if (StVT.isVector() && ST->isTruncatingStore())
146 switch (TLI.getTruncStoreAction(ValVT, StVT)) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000147 default: llvm_unreachable("This action is not supported yet!");
Nadav Roteme9b58d02011-10-15 07:41:10 +0000148 case TargetLowering::Legal:
149 return TranslateLegalizeResults(Op, Result);
150 case TargetLowering::Custom:
151 Changed = true;
152 return LegalizeOp(TLI.LowerOperation(Result, DAG));
153 case TargetLowering::Expand:
154 Changed = true;
155 return LegalizeOp(ExpandStore(Op));
156 }
157 }
158
Eli Friedman5c22c802009-05-23 12:35:30 +0000159 bool HasVectorValue = false;
160 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
161 J != E;
162 ++J)
163 HasVectorValue |= J->isVector();
164 if (!HasVectorValue)
165 return TranslateLegalizeResults(Op, Result);
166
Owen Andersone50ed302009-08-10 22:56:29 +0000167 EVT QueryType;
Eli Friedman5c22c802009-05-23 12:35:30 +0000168 switch (Op.getOpcode()) {
169 default:
170 return TranslateLegalizeResults(Op, Result);
171 case ISD::ADD:
172 case ISD::SUB:
173 case ISD::MUL:
174 case ISD::SDIV:
175 case ISD::UDIV:
176 case ISD::SREM:
177 case ISD::UREM:
178 case ISD::FADD:
179 case ISD::FSUB:
180 case ISD::FMUL:
181 case ISD::FDIV:
182 case ISD::FREM:
183 case ISD::AND:
184 case ISD::OR:
185 case ISD::XOR:
186 case ISD::SHL:
187 case ISD::SRA:
188 case ISD::SRL:
189 case ISD::ROTL:
190 case ISD::ROTR:
Eli Friedman5c22c802009-05-23 12:35:30 +0000191 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +0000192 case ISD::CTTZ:
193 case ISD::CTLZ_ZERO_UNDEF:
194 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman5c22c802009-05-23 12:35:30 +0000195 case ISD::CTPOP:
196 case ISD::SELECT:
Nadav Rotemaec58612011-09-13 19:17:42 +0000197 case ISD::VSELECT:
Eli Friedman5c22c802009-05-23 12:35:30 +0000198 case ISD::SELECT_CC:
Duncan Sands28b77e92011-09-06 19:07:46 +0000199 case ISD::SETCC:
Eli Friedman5c22c802009-05-23 12:35:30 +0000200 case ISD::ZERO_EXTEND:
201 case ISD::ANY_EXTEND:
202 case ISD::TRUNCATE:
203 case ISD::SIGN_EXTEND:
Eli Friedman5c22c802009-05-23 12:35:30 +0000204 case ISD::FP_TO_SINT:
205 case ISD::FP_TO_UINT:
206 case ISD::FNEG:
207 case ISD::FABS:
208 case ISD::FSQRT:
209 case ISD::FSIN:
210 case ISD::FCOS:
211 case ISD::FPOWI:
212 case ISD::FPOW:
213 case ISD::FLOG:
214 case ISD::FLOG2:
215 case ISD::FLOG10:
216 case ISD::FEXP:
217 case ISD::FEXP2:
218 case ISD::FCEIL:
219 case ISD::FTRUNC:
220 case ISD::FRINT:
221 case ISD::FNEARBYINT:
222 case ISD::FFLOOR:
Craig Topper6b1e1d82012-08-30 07:34:22 +0000223 case ISD::FMA:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +0000224 case ISD::SIGN_EXTEND_INREG:
Eli Friedman556929a2009-06-06 03:27:50 +0000225 QueryType = Node->getValueType(0);
226 break;
Dan Gohmand1996362010-01-09 02:13:55 +0000227 case ISD::FP_ROUND_INREG:
228 QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
229 break;
Eli Friedman556929a2009-06-06 03:27:50 +0000230 case ISD::SINT_TO_FP:
231 case ISD::UINT_TO_FP:
232 QueryType = Node->getOperand(0).getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000233 break;
234 }
235
Eli Friedman556929a2009-06-06 03:27:50 +0000236 switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
Eli Friedman5c22c802009-05-23 12:35:30 +0000237 case TargetLowering::Promote:
Jim Grosbach926dc162012-06-28 21:03:44 +0000238 switch (Op.getOpcode()) {
239 default:
240 // "Promote" the operation by bitcasting
241 Result = PromoteVectorOp(Op);
242 Changed = true;
243 break;
244 case ISD::SINT_TO_FP:
245 case ISD::UINT_TO_FP:
246 // "Promote" the operation by extending the operand.
247 Result = PromoteVectorOpINT_TO_FP(Op);
248 Changed = true;
249 break;
250 }
Eli Friedman5c22c802009-05-23 12:35:30 +0000251 break;
252 case TargetLowering::Legal: break;
253 case TargetLowering::Custom: {
254 SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
255 if (Tmp1.getNode()) {
256 Result = Tmp1;
257 break;
258 }
259 // FALL THROUGH
260 }
261 case TargetLowering::Expand:
Nadav Rotemaec58612011-09-13 19:17:42 +0000262 if (Node->getOpcode() == ISD::VSELECT)
263 Result = ExpandVSELECT(Op);
264 else if (Node->getOpcode() == ISD::UINT_TO_FP)
Nadav Rotem06cc3242011-03-19 13:09:10 +0000265 Result = ExpandUINT_TO_FLOAT(Op);
266 else if (Node->getOpcode() == ISD::FNEG)
Eli Friedman5c22c802009-05-23 12:35:30 +0000267 Result = ExpandFNEG(Op);
Duncan Sands28b77e92011-09-06 19:07:46 +0000268 else if (Node->getOpcode() == ISD::SETCC)
Eli Friedman5c22c802009-05-23 12:35:30 +0000269 Result = UnrollVSETCC(Op);
270 else
Mon P Wangcd6e7252009-11-30 02:42:02 +0000271 Result = DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000272 break;
273 }
274
275 // Make sure that the generated code is itself legal.
276 if (Result != Op) {
277 Result = LegalizeOp(Result);
278 Changed = true;
279 }
280
281 // Note that LegalizeOp may be reentered even from single-use nodes, which
282 // means that we always must cache transformed nodes.
283 AddLegalizedOperand(Op, Result);
284 return Result;
285}
286
287SDValue VectorLegalizer::PromoteVectorOp(SDValue Op) {
Eli Friedmanc046c002009-05-24 20:32:10 +0000288 // Vector "promotion" is basically just bitcasting and doing the operation
289 // in a different type. For example, x86 promotes ISD::AND on v2i32 to
290 // v1i64.
Owen Andersone50ed302009-08-10 22:56:29 +0000291 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000292 assert(Op.getNode()->getNumValues() == 1 &&
293 "Can't promote a vector with multiple results!");
Owen Andersone50ed302009-08-10 22:56:29 +0000294 EVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
Eli Friedman5c22c802009-05-23 12:35:30 +0000295 DebugLoc dl = Op.getDebugLoc();
296 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
297
298 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
299 if (Op.getOperand(j).getValueType().isVector())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000300 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
Eli Friedman5c22c802009-05-23 12:35:30 +0000301 else
302 Operands[j] = Op.getOperand(j);
303 }
304
305 Op = DAG.getNode(Op.getOpcode(), dl, NVT, &Operands[0], Operands.size());
306
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000307 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Eli Friedman5c22c802009-05-23 12:35:30 +0000308}
309
Jim Grosbach926dc162012-06-28 21:03:44 +0000310SDValue VectorLegalizer::PromoteVectorOpINT_TO_FP(SDValue Op) {
311 // INT_TO_FP operations may require the input operand be promoted even
312 // when the type is otherwise legal.
313 EVT VT = Op.getOperand(0).getValueType();
314 assert(Op.getNode()->getNumValues() == 1 &&
315 "Can't promote a vector with multiple results!");
316
317 // Normal getTypeToPromoteTo() doesn't work here, as that will promote
318 // by widening the vector w/ the same element width and twice the number
319 // of elements. We want the other way around, the same number of elements,
320 // each twice the width.
321 //
322 // Increase the bitwidth of the element to the next pow-of-two
323 // (which is greater than 8 bits).
324 unsigned NumElts = VT.getVectorNumElements();
325 EVT EltVT = VT.getVectorElementType();
326 EltVT = EVT::getIntegerVT(*DAG.getContext(), 2 * EltVT.getSizeInBits());
327 assert(EltVT.isSimple() && "Promoting to a non-simple vector type!");
328
329 // Build a new vector type and check if it is legal.
330 MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
331
332 DebugLoc dl = Op.getDebugLoc();
333 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
334
335 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
336 ISD::SIGN_EXTEND;
337 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
338 if (Op.getOperand(j).getValueType().isVector())
339 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
340 else
341 Operands[j] = Op.getOperand(j);
342 }
343
344 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), &Operands[0],
345 Operands.size());
346}
347
Nadav Roteme9b58d02011-10-15 07:41:10 +0000348
349SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
350 DebugLoc dl = Op.getDebugLoc();
351 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
352 SDValue Chain = LD->getChain();
353 SDValue BasePTR = LD->getBasePtr();
354 EVT SrcVT = LD->getMemoryVT();
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000355 ISD::LoadExtType ExtType = LD->getExtensionType();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000356
357 SmallVector<SDValue, 8> LoadVals;
358 SmallVector<SDValue, 8> LoadChains;
359 unsigned NumElem = SrcVT.getVectorNumElements();
360 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
361
362 for (unsigned Idx=0; Idx<NumElem; Idx++) {
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000363 SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
Nadav Roteme9b58d02011-10-15 07:41:10 +0000364 Op.getNode()->getValueType(0).getScalarType(),
365 Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
366 SrcVT.getScalarType(),
367 LD->isVolatile(), LD->isNonTemporal(),
368 LD->getAlignment());
369
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000370 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
371 DAG.getIntPtrConstant(Stride));
372
Nadav Roteme9b58d02011-10-15 07:41:10 +0000373 LoadVals.push_back(ScalarLoad.getValue(0));
374 LoadChains.push_back(ScalarLoad.getValue(1));
375 }
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000376
Nadav Roteme9b58d02011-10-15 07:41:10 +0000377 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
378 &LoadChains[0], LoadChains.size());
379 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
380 Op.getNode()->getValueType(0), &LoadVals[0], LoadVals.size());
381
382 AddLegalizedOperand(Op.getValue(0), Value);
383 AddLegalizedOperand(Op.getValue(1), NewChain);
384
385 return (Op.getResNo() ? NewChain : Value);
386}
387
388SDValue VectorLegalizer::ExpandStore(SDValue Op) {
389 DebugLoc dl = Op.getDebugLoc();
390 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
391 SDValue Chain = ST->getChain();
392 SDValue BasePTR = ST->getBasePtr();
393 SDValue Value = ST->getValue();
394 EVT StVT = ST->getMemoryVT();
395
396 unsigned Alignment = ST->getAlignment();
397 bool isVolatile = ST->isVolatile();
398 bool isNonTemporal = ST->isNonTemporal();
399
400 unsigned NumElem = StVT.getVectorNumElements();
401 // The type of the data we want to save
402 EVT RegVT = Value.getValueType();
403 EVT RegSclVT = RegVT.getScalarType();
404 // The type of data as saved in memory.
405 EVT MemSclVT = StVT.getScalarType();
406
407 // Cast floats into integers
408 unsigned ScalarSize = MemSclVT.getSizeInBits();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000409
410 // Round odd types to the next pow of two.
411 if (!isPowerOf2_32(ScalarSize))
412 ScalarSize = NextPowerOf2(ScalarSize);
413
414 // Store Stride in bytes
415 unsigned Stride = ScalarSize/8;
416 // Extract each of the elements from the original vector
417 // and save them into memory individually.
418 SmallVector<SDValue, 8> Stores;
419 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
420 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
421 RegSclVT, Value, DAG.getIntPtrConstant(Idx));
422
Nadav Roteme9b58d02011-10-15 07:41:10 +0000423 // This scalar TruncStore may be illegal, but we legalize it later.
424 SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
425 ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
426 isVolatile, isNonTemporal, Alignment);
427
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000428 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
429 DAG.getIntPtrConstant(Stride));
430
Nadav Roteme9b58d02011-10-15 07:41:10 +0000431 Stores.push_back(Store);
432 }
433 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
434 &Stores[0], Stores.size());
435 AddLegalizedOperand(Op, TF);
436 return TF;
437}
438
Nadav Rotemaec58612011-09-13 19:17:42 +0000439SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
440 // Implement VSELECT in terms of XOR, AND, OR
441 // on platforms which do not support blend natively.
442 EVT VT = Op.getOperand(0).getValueType();
Nadav Rotemaec58612011-09-13 19:17:42 +0000443 DebugLoc DL = Op.getDebugLoc();
444
445 SDValue Mask = Op.getOperand(0);
446 SDValue Op1 = Op.getOperand(1);
447 SDValue Op2 = Op.getOperand(2);
448
449 // If we can't even use the basic vector operations of
450 // AND,OR,XOR, we will have to scalarize the op.
Nadav Rotem815af822011-10-19 20:43:16 +0000451 // Notice that the operation may be 'promoted' which means that it is
452 // 'bitcasted' to another type which is handled.
453 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
454 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
455 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand)
456 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotemaec58612011-09-13 19:17:42 +0000457
Duncan Sands17001ce2011-10-18 12:44:00 +0000458 assert(VT.getSizeInBits() == Op.getOperand(1).getValueType().getSizeInBits()
459 && "Invalid mask size");
Nadav Rotemaec58612011-09-13 19:17:42 +0000460 // Bitcast the operands to be the same type as the mask.
461 // This is needed when we select between FP types because
462 // the mask is a vector of integers.
463 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
464 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
465
466 SDValue AllOnes = DAG.getConstant(
467 APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
468 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
469
470 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
471 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
Nadav Rotem3ab32ea2012-04-15 15:08:09 +0000472 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
473 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
Nadav Rotemaec58612011-09-13 19:17:42 +0000474}
475
Nadav Rotem06cc3242011-03-19 13:09:10 +0000476SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
Nadav Rotem06cc3242011-03-19 13:09:10 +0000477 EVT VT = Op.getOperand(0).getValueType();
478 DebugLoc DL = Op.getDebugLoc();
479
480 // Make sure that the SINT_TO_FP and SRL instructions are available.
Nadav Rotem815af822011-10-19 20:43:16 +0000481 if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
482 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
483 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotem06cc3242011-03-19 13:09:10 +0000484
485 EVT SVT = VT.getScalarType();
486 assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
487 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
488
489 unsigned BW = SVT.getSizeInBits();
490 SDValue HalfWord = DAG.getConstant(BW/2, VT);
491
492 // Constants to clear the upper part of the word.
493 // Notice that we can also use SHL+SHR, but using a constant is slightly
494 // faster on x86.
495 uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
496 SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
497
498 // Two to the power of half-word-size.
499 SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
500
501 // Clear upper part of LO, lower HI
502 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
503 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
504
505 // Convert hi and lo to floats
506 // Convert the hi part back to the upper values
507 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
508 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
509 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
510
511 // Add the two halves
512 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
513}
514
515
Eli Friedman5c22c802009-05-23 12:35:30 +0000516SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
517 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
518 SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
519 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
520 Zero, Op.getOperand(0));
521 }
Mon P Wangcd6e7252009-11-30 02:42:02 +0000522 return DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000523}
524
525SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +0000526 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000527 unsigned NumElems = VT.getVectorNumElements();
Owen Andersone50ed302009-08-10 22:56:29 +0000528 EVT EltVT = VT.getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000529 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +0000530 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000531 DebugLoc dl = Op.getDebugLoc();
532 SmallVector<SDValue, 8> Ops(NumElems);
533 for (unsigned i = 0; i < NumElems; ++i) {
534 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
535 DAG.getIntPtrConstant(i));
536 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
537 DAG.getIntPtrConstant(i));
538 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
539 LHSElem, RHSElem, CC);
540 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
541 DAG.getConstant(APInt::getAllOnesValue
542 (EltVT.getSizeInBits()), EltVT),
543 DAG.getConstant(0, EltVT));
544 }
545 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
546}
547
Eli Friedman5c22c802009-05-23 12:35:30 +0000548}
549
550bool SelectionDAG::LegalizeVectors() {
551 return VectorLegalizer(*this).Run();
552}