blob: 7b28e69df6c32590d8007029aec61551ae8b8617 [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.
Preston Gurdea387fc2013-01-25 15:18:54 +000043 SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes;
Eli Friedman5c22c802009-05-23 12:35:30 +000044
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 Rotem66de2af2013-01-11 22:57:48 +000064 // Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
65 SDValue ExpandSEXTINREG(SDValue Op);
Nadav Rotemb6266fb2011-09-18 10:29:29 +000066 // Implement vselect in terms of XOR, AND, OR when blend is not supported
67 // by the target.
Nadav Rotemaec58612011-09-13 19:17:42 +000068 SDValue ExpandVSELECT(SDValue Op);
Nadav Roteme757f002012-08-30 19:17:29 +000069 SDValue ExpandSELECT(SDValue Op);
Nadav Roteme9b58d02011-10-15 07:41:10 +000070 SDValue ExpandLoad(SDValue Op);
71 SDValue ExpandStore(SDValue Op);
Eli Friedman5c22c802009-05-23 12:35:30 +000072 SDValue ExpandFNEG(SDValue Op);
73 // Implements vector promotion; this is essentially just bitcasting the
74 // operands to a different type and bitcasting the result back to the
75 // original type.
76 SDValue PromoteVectorOp(SDValue Op);
Jim Grosbach926dc162012-06-28 21:03:44 +000077 // Implements [SU]INT_TO_FP vector promotion; this is a [zs]ext of the input
78 // operand to the next size up.
79 SDValue PromoteVectorOpINT_TO_FP(SDValue Op);
Eli Friedman5c22c802009-05-23 12:35:30 +000080
81 public:
82 bool Run();
83 VectorLegalizer(SelectionDAG& dag) :
84 DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
85};
86
87bool VectorLegalizer::Run() {
88 // The legalize process is inherently a bottom-up recursive process (users
89 // legalize their uses before themselves). Given infinite stack space, we
90 // could just start legalizing on the root and traverse the whole graph. In
91 // practice however, this causes us to run out of stack space on large basic
92 // blocks. To avoid this problem, compute an ordering of the nodes where each
93 // node is only legalized after all of its operands are legalized.
94 DAG.AssignTopologicalOrder();
95 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +000096 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedman5c22c802009-05-23 12:35:30 +000097 LegalizeOp(SDValue(I, 0));
98
99 // Finally, it's possible the root changed. Get the new root.
100 SDValue OldRoot = DAG.getRoot();
101 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
102 DAG.setRoot(LegalizedNodes[OldRoot]);
103
104 LegalizedNodes.clear();
105
106 // Remove dead nodes now.
107 DAG.RemoveDeadNodes();
108
109 return Changed;
110}
111
112SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
113 // Generic legalization: just pass the operand through.
114 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
115 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
116 return Result.getValue(Op.getResNo());
117}
118
119SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
120 // Note that LegalizeOp may be reentered even from single-use nodes, which
121 // means that we always must cache transformed nodes.
122 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
123 if (I != LegalizedNodes.end()) return I->second;
124
125 SDNode* Node = Op.getNode();
126
127 // Legalize the operands
128 SmallVector<SDValue, 8> Ops;
129 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
130 Ops.push_back(LegalizeOp(Node->getOperand(i)));
131
132 SDValue Result =
Dan Gohman027657d2010-06-18 15:30:29 +0000133 SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops.data(), Ops.size()), 0);
Eli Friedman5c22c802009-05-23 12:35:30 +0000134
Nadav Roteme9b58d02011-10-15 07:41:10 +0000135 if (Op.getOpcode() == ISD::LOAD) {
136 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
137 ISD::LoadExtType ExtType = LD->getExtensionType();
138 if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
139 if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
140 return TranslateLegalizeResults(Op, Result);
141 Changed = true;
142 return LegalizeOp(ExpandLoad(Op));
143 }
144 } else if (Op.getOpcode() == ISD::STORE) {
145 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
146 EVT StVT = ST->getMemoryVT();
Patrik Hagglund88ef5142012-12-19 08:28:51 +0000147 MVT ValVT = ST->getValue().getSimpleValueType();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000148 if (StVT.isVector() && ST->isTruncatingStore())
Patrik Hagglund88ef5142012-12-19 08:28:51 +0000149 switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000150 default: llvm_unreachable("This action is not supported yet!");
Nadav Roteme9b58d02011-10-15 07:41:10 +0000151 case TargetLowering::Legal:
152 return TranslateLegalizeResults(Op, Result);
153 case TargetLowering::Custom:
154 Changed = true;
155 return LegalizeOp(TLI.LowerOperation(Result, DAG));
156 case TargetLowering::Expand:
157 Changed = true;
158 return LegalizeOp(ExpandStore(Op));
159 }
160 }
161
Eli Friedman5c22c802009-05-23 12:35:30 +0000162 bool HasVectorValue = false;
163 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
164 J != E;
165 ++J)
166 HasVectorValue |= J->isVector();
167 if (!HasVectorValue)
168 return TranslateLegalizeResults(Op, Result);
169
Owen Andersone50ed302009-08-10 22:56:29 +0000170 EVT QueryType;
Eli Friedman5c22c802009-05-23 12:35:30 +0000171 switch (Op.getOpcode()) {
172 default:
173 return TranslateLegalizeResults(Op, Result);
174 case ISD::ADD:
175 case ISD::SUB:
176 case ISD::MUL:
177 case ISD::SDIV:
178 case ISD::UDIV:
179 case ISD::SREM:
180 case ISD::UREM:
181 case ISD::FADD:
182 case ISD::FSUB:
183 case ISD::FMUL:
184 case ISD::FDIV:
185 case ISD::FREM:
186 case ISD::AND:
187 case ISD::OR:
188 case ISD::XOR:
189 case ISD::SHL:
190 case ISD::SRA:
191 case ISD::SRL:
192 case ISD::ROTL:
193 case ISD::ROTR:
Eli Friedman5c22c802009-05-23 12:35:30 +0000194 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +0000195 case ISD::CTTZ:
196 case ISD::CTLZ_ZERO_UNDEF:
197 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman5c22c802009-05-23 12:35:30 +0000198 case ISD::CTPOP:
199 case ISD::SELECT:
Nadav Rotemaec58612011-09-13 19:17:42 +0000200 case ISD::VSELECT:
Eli Friedman5c22c802009-05-23 12:35:30 +0000201 case ISD::SELECT_CC:
Duncan Sands28b77e92011-09-06 19:07:46 +0000202 case ISD::SETCC:
Eli Friedman5c22c802009-05-23 12:35:30 +0000203 case ISD::ZERO_EXTEND:
204 case ISD::ANY_EXTEND:
205 case ISD::TRUNCATE:
206 case ISD::SIGN_EXTEND:
Eli Friedman5c22c802009-05-23 12:35:30 +0000207 case ISD::FP_TO_SINT:
208 case ISD::FP_TO_UINT:
209 case ISD::FNEG:
210 case ISD::FABS:
211 case ISD::FSQRT:
212 case ISD::FSIN:
213 case ISD::FCOS:
214 case ISD::FPOWI:
215 case ISD::FPOW:
216 case ISD::FLOG:
217 case ISD::FLOG2:
218 case ISD::FLOG10:
219 case ISD::FEXP:
220 case ISD::FEXP2:
221 case ISD::FCEIL:
222 case ISD::FTRUNC:
223 case ISD::FRINT:
224 case ISD::FNEARBYINT:
225 case ISD::FFLOOR:
Eli Friedman846ce8e2012-11-15 22:44:27 +0000226 case ISD::FP_ROUND:
Eli Friedman43147af2012-11-17 01:52:46 +0000227 case ISD::FP_EXTEND:
Craig Topper6b1e1d82012-08-30 07:34:22 +0000228 case ISD::FMA:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +0000229 case ISD::SIGN_EXTEND_INREG:
Eli Friedman556929a2009-06-06 03:27:50 +0000230 QueryType = Node->getValueType(0);
231 break;
Dan Gohmand1996362010-01-09 02:13:55 +0000232 case ISD::FP_ROUND_INREG:
233 QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
234 break;
Eli Friedman556929a2009-06-06 03:27:50 +0000235 case ISD::SINT_TO_FP:
236 case ISD::UINT_TO_FP:
237 QueryType = Node->getOperand(0).getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000238 break;
239 }
240
Eli Friedman556929a2009-06-06 03:27:50 +0000241 switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
Eli Friedman5c22c802009-05-23 12:35:30 +0000242 case TargetLowering::Promote:
Jim Grosbach926dc162012-06-28 21:03:44 +0000243 switch (Op.getOpcode()) {
244 default:
245 // "Promote" the operation by bitcasting
246 Result = PromoteVectorOp(Op);
247 Changed = true;
248 break;
249 case ISD::SINT_TO_FP:
250 case ISD::UINT_TO_FP:
251 // "Promote" the operation by extending the operand.
252 Result = PromoteVectorOpINT_TO_FP(Op);
253 Changed = true;
254 break;
255 }
Eli Friedman5c22c802009-05-23 12:35:30 +0000256 break;
257 case TargetLowering::Legal: break;
258 case TargetLowering::Custom: {
259 SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
260 if (Tmp1.getNode()) {
261 Result = Tmp1;
262 break;
263 }
264 // FALL THROUGH
265 }
266 case TargetLowering::Expand:
Nadav Rotem66de2af2013-01-11 22:57:48 +0000267 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG)
268 Result = ExpandSEXTINREG(Op);
269 else if (Node->getOpcode() == ISD::VSELECT)
Nadav Rotemaec58612011-09-13 19:17:42 +0000270 Result = ExpandVSELECT(Op);
Nadav Roteme757f002012-08-30 19:17:29 +0000271 else if (Node->getOpcode() == ISD::SELECT)
272 Result = ExpandSELECT(Op);
Nadav Rotemaec58612011-09-13 19:17:42 +0000273 else if (Node->getOpcode() == ISD::UINT_TO_FP)
Nadav Rotem06cc3242011-03-19 13:09:10 +0000274 Result = ExpandUINT_TO_FLOAT(Op);
275 else if (Node->getOpcode() == ISD::FNEG)
Eli Friedman5c22c802009-05-23 12:35:30 +0000276 Result = ExpandFNEG(Op);
Duncan Sands28b77e92011-09-06 19:07:46 +0000277 else if (Node->getOpcode() == ISD::SETCC)
Eli Friedman5c22c802009-05-23 12:35:30 +0000278 Result = UnrollVSETCC(Op);
279 else
Mon P Wangcd6e7252009-11-30 02:42:02 +0000280 Result = DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000281 break;
282 }
283
284 // Make sure that the generated code is itself legal.
285 if (Result != Op) {
286 Result = LegalizeOp(Result);
287 Changed = true;
288 }
289
290 // Note that LegalizeOp may be reentered even from single-use nodes, which
291 // means that we always must cache transformed nodes.
292 AddLegalizedOperand(Op, Result);
293 return Result;
294}
295
296SDValue VectorLegalizer::PromoteVectorOp(SDValue Op) {
Eli Friedmanc046c002009-05-24 20:32:10 +0000297 // Vector "promotion" is basically just bitcasting and doing the operation
298 // in a different type. For example, x86 promotes ISD::AND on v2i32 to
299 // v1i64.
Patrik Hagglund319bb392012-12-19 11:21:04 +0000300 MVT VT = Op.getSimpleValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000301 assert(Op.getNode()->getNumValues() == 1 &&
302 "Can't promote a vector with multiple results!");
Patrik Hagglund319bb392012-12-19 11:21:04 +0000303 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
Eli Friedman5c22c802009-05-23 12:35:30 +0000304 DebugLoc dl = Op.getDebugLoc();
305 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
306
307 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
308 if (Op.getOperand(j).getValueType().isVector())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000309 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
Eli Friedman5c22c802009-05-23 12:35:30 +0000310 else
311 Operands[j] = Op.getOperand(j);
312 }
313
314 Op = DAG.getNode(Op.getOpcode(), dl, NVT, &Operands[0], Operands.size());
315
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000316 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Eli Friedman5c22c802009-05-23 12:35:30 +0000317}
318
Jim Grosbach926dc162012-06-28 21:03:44 +0000319SDValue VectorLegalizer::PromoteVectorOpINT_TO_FP(SDValue Op) {
320 // INT_TO_FP operations may require the input operand be promoted even
321 // when the type is otherwise legal.
322 EVT VT = Op.getOperand(0).getValueType();
323 assert(Op.getNode()->getNumValues() == 1 &&
324 "Can't promote a vector with multiple results!");
325
326 // Normal getTypeToPromoteTo() doesn't work here, as that will promote
327 // by widening the vector w/ the same element width and twice the number
328 // of elements. We want the other way around, the same number of elements,
329 // each twice the width.
330 //
331 // Increase the bitwidth of the element to the next pow-of-two
332 // (which is greater than 8 bits).
333 unsigned NumElts = VT.getVectorNumElements();
334 EVT EltVT = VT.getVectorElementType();
335 EltVT = EVT::getIntegerVT(*DAG.getContext(), 2 * EltVT.getSizeInBits());
336 assert(EltVT.isSimple() && "Promoting to a non-simple vector type!");
337
338 // Build a new vector type and check if it is legal.
339 MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
340
341 DebugLoc dl = Op.getDebugLoc();
342 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
343
344 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
345 ISD::SIGN_EXTEND;
346 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
347 if (Op.getOperand(j).getValueType().isVector())
348 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
349 else
350 Operands[j] = Op.getOperand(j);
351 }
352
353 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), &Operands[0],
354 Operands.size());
355}
356
Nadav Roteme9b58d02011-10-15 07:41:10 +0000357
358SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
359 DebugLoc dl = Op.getDebugLoc();
360 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
361 SDValue Chain = LD->getChain();
362 SDValue BasePTR = LD->getBasePtr();
363 EVT SrcVT = LD->getMemoryVT();
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000364 ISD::LoadExtType ExtType = LD->getExtensionType();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000365
Michael Liaoeedff352013-02-20 18:04:21 +0000366 SmallVector<SDValue, 8> Vals;
Nadav Roteme9b58d02011-10-15 07:41:10 +0000367 SmallVector<SDValue, 8> LoadChains;
368 unsigned NumElem = SrcVT.getVectorNumElements();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000369
Michael Liaoeedff352013-02-20 18:04:21 +0000370 EVT SrcEltVT = SrcVT.getScalarType();
371 EVT DstEltVT = Op.getNode()->getValueType(0).getScalarType();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000372
Michael Liaoeedff352013-02-20 18:04:21 +0000373 if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) {
374 // When elements in a vector is not byte-addressable, we cannot directly
375 // load each element by advancing pointer, which could only address bytes.
376 // Instead, we load all significant words, mask bits off, and concatenate
377 // them to form each element. Finally, they are extended to destination
378 // scalar type to build the destination vector.
379 EVT WideVT = TLI.getPointerTy();
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000380
Michael Liaoeedff352013-02-20 18:04:21 +0000381 assert(WideVT.isRound() &&
382 "Could not handle the sophisticated case when the widest integer is"
383 " not power of 2.");
384 assert(WideVT.bitsGE(SrcEltVT) &&
385 "Type is not legalized?");
386
387 unsigned WideBytes = WideVT.getStoreSize();
388 unsigned Offset = 0;
389 unsigned RemainingBytes = SrcVT.getStoreSize();
390 SmallVector<SDValue, 8> LoadVals;
391
392 while (RemainingBytes > 0) {
393 SDValue ScalarLoad;
394 unsigned LoadBytes = WideBytes;
395
396 if (RemainingBytes >= LoadBytes) {
397 ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR,
398 LD->getPointerInfo().getWithOffset(Offset),
399 LD->isVolatile(), LD->isNonTemporal(),
400 LD->isInvariant(), LD->getAlignment());
401 } else {
402 EVT LoadVT = WideVT;
403 while (RemainingBytes < LoadBytes) {
404 LoadBytes >>= 1; // Reduce the load size by half.
405 LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3);
406 }
407 ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
408 LD->getPointerInfo().getWithOffset(Offset),
409 LoadVT, LD->isVolatile(),
410 LD->isNonTemporal(), LD->getAlignment());
411 }
412
413 RemainingBytes -= LoadBytes;
414 Offset += LoadBytes;
415 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
416 DAG.getIntPtrConstant(LoadBytes));
417
418 LoadVals.push_back(ScalarLoad.getValue(0));
419 LoadChains.push_back(ScalarLoad.getValue(1));
420 }
421
422 // Extract bits, pack and extend/trunc them into destination type.
423 unsigned SrcEltBits = SrcEltVT.getSizeInBits();
424 SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, WideVT);
425
426 unsigned BitOffset = 0;
427 unsigned WideIdx = 0;
428 unsigned WideBits = WideVT.getSizeInBits();
429
430 for (unsigned Idx = 0; Idx != NumElem; ++Idx) {
431 SDValue Lo, Hi, ShAmt;
432
433 if (BitOffset < WideBits) {
434 ShAmt = DAG.getConstant(BitOffset, TLI.getShiftAmountTy(WideVT));
435 Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt);
436 Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask);
437 }
438
439 BitOffset += SrcEltBits;
440 if (BitOffset >= WideBits) {
441 WideIdx++;
442 Offset -= WideBits;
443 if (Offset > 0) {
444 ShAmt = DAG.getConstant(SrcEltBits - Offset,
445 TLI.getShiftAmountTy(WideVT));
446 Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt);
447 Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask);
448 }
449 }
450
451 if (Hi.getNode())
452 Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi);
453
454 switch (ExtType) {
455 default: llvm_unreachable("Unknown extended-load op!");
456 case ISD::EXTLOAD:
457 Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
458 break;
459 case ISD::ZEXTLOAD:
460 Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
461 break;
462 case ISD::SEXTLOAD:
463 ShAmt = DAG.getConstant(WideBits - SrcEltBits,
464 TLI.getShiftAmountTy(WideVT));
465 Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt);
466 Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt);
467 Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
468 break;
469 }
470 Vals.push_back(Lo);
471 }
472 } else {
473 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
474
475 for (unsigned Idx=0; Idx<NumElem; Idx++) {
476 SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
477 Op.getNode()->getValueType(0).getScalarType(),
478 Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
479 SrcVT.getScalarType(),
480 LD->isVolatile(), LD->isNonTemporal(),
481 LD->getAlignment());
482
483 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
484 DAG.getIntPtrConstant(Stride));
485
486 Vals.push_back(ScalarLoad.getValue(0));
487 LoadChains.push_back(ScalarLoad.getValue(1));
488 }
Nadav Roteme9b58d02011-10-15 07:41:10 +0000489 }
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000490
Nadav Roteme9b58d02011-10-15 07:41:10 +0000491 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
492 &LoadChains[0], LoadChains.size());
493 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
Michael Liaoeedff352013-02-20 18:04:21 +0000494 Op.getNode()->getValueType(0), &Vals[0], Vals.size());
Nadav Roteme9b58d02011-10-15 07:41:10 +0000495
496 AddLegalizedOperand(Op.getValue(0), Value);
497 AddLegalizedOperand(Op.getValue(1), NewChain);
498
499 return (Op.getResNo() ? NewChain : Value);
500}
501
502SDValue VectorLegalizer::ExpandStore(SDValue Op) {
503 DebugLoc dl = Op.getDebugLoc();
504 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
505 SDValue Chain = ST->getChain();
506 SDValue BasePTR = ST->getBasePtr();
507 SDValue Value = ST->getValue();
508 EVT StVT = ST->getMemoryVT();
509
510 unsigned Alignment = ST->getAlignment();
511 bool isVolatile = ST->isVolatile();
512 bool isNonTemporal = ST->isNonTemporal();
513
514 unsigned NumElem = StVT.getVectorNumElements();
515 // The type of the data we want to save
516 EVT RegVT = Value.getValueType();
517 EVT RegSclVT = RegVT.getScalarType();
518 // The type of data as saved in memory.
519 EVT MemSclVT = StVT.getScalarType();
520
521 // Cast floats into integers
522 unsigned ScalarSize = MemSclVT.getSizeInBits();
Nadav Roteme9b58d02011-10-15 07:41:10 +0000523
524 // Round odd types to the next pow of two.
525 if (!isPowerOf2_32(ScalarSize))
526 ScalarSize = NextPowerOf2(ScalarSize);
527
528 // Store Stride in bytes
529 unsigned Stride = ScalarSize/8;
530 // Extract each of the elements from the original vector
531 // and save them into memory individually.
532 SmallVector<SDValue, 8> Stores;
533 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
534 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
535 RegSclVT, Value, DAG.getIntPtrConstant(Idx));
536
Nadav Roteme9b58d02011-10-15 07:41:10 +0000537 // This scalar TruncStore may be illegal, but we legalize it later.
538 SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
539 ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
540 isVolatile, isNonTemporal, Alignment);
541
Nadav Rotemfbf19ef2011-10-18 22:32:43 +0000542 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
543 DAG.getIntPtrConstant(Stride));
544
Nadav Roteme9b58d02011-10-15 07:41:10 +0000545 Stores.push_back(Store);
546 }
547 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
548 &Stores[0], Stores.size());
549 AddLegalizedOperand(Op, TF);
550 return TF;
551}
552
Nadav Roteme757f002012-08-30 19:17:29 +0000553SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
554 // Lower a select instruction where the condition is a scalar and the
555 // operands are vectors. Lower this select to VSELECT and implement it
556 // using XOR AND OR. The selector bit is broadcasted.
557 EVT VT = Op.getValueType();
558 DebugLoc DL = Op.getDebugLoc();
559
560 SDValue Mask = Op.getOperand(0);
561 SDValue Op1 = Op.getOperand(1);
562 SDValue Op2 = Op.getOperand(2);
563
564 assert(VT.isVector() && !Mask.getValueType().isVector()
565 && Op1.getValueType() == Op2.getValueType() && "Invalid type");
566
567 unsigned NumElem = VT.getVectorNumElements();
568
569 // If we can't even use the basic vector operations of
570 // AND,OR,XOR, we will have to scalarize the op.
571 // Notice that the operation may be 'promoted' which means that it is
572 // 'bitcasted' to another type which is handled.
573 // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
574 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
575 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
576 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
577 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand)
578 return DAG.UnrollVectorOp(Op.getNode());
579
580 // Generate a mask operand.
581 EVT MaskTy = TLI.getSetCCResultType(VT);
582 assert(MaskTy.isVector() && "Invalid CC type");
583 assert(MaskTy.getSizeInBits() == Op1.getValueType().getSizeInBits()
584 && "Invalid mask size");
585
586 // What is the size of each element in the vector mask.
587 EVT BitTy = MaskTy.getScalarType();
588
Nadav Rotemf55ef642012-09-02 08:20:07 +0000589 Mask = DAG.getNode(ISD::SELECT, DL, BitTy, Mask,
590 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy),
Nadav Rotemee77da62012-09-02 12:21:50 +0000591 DAG.getConstant(0, BitTy));
Nadav Roteme757f002012-08-30 19:17:29 +0000592
593 // Broadcast the mask so that the entire vector is all-one or all zero.
594 SmallVector<SDValue, 8> Ops(NumElem, Mask);
595 Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, &Ops[0], Ops.size());
596
597 // Bitcast the operands to be the same type as the mask.
598 // This is needed when we select between FP types because
599 // the mask is a vector of integers.
600 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
601 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
602
603 SDValue AllOnes = DAG.getConstant(
604 APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy);
605 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
606
607 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
608 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
609 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
610 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
611}
612
Nadav Rotem66de2af2013-01-11 22:57:48 +0000613SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) {
614 EVT VT = Op.getValueType();
615
Benjamin Kramer4dc47832013-01-12 19:06:44 +0000616 // Make sure that the SRA and SHL instructions are available.
Nadav Rotem66de2af2013-01-11 22:57:48 +0000617 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
Benjamin Kramer4dc47832013-01-12 19:06:44 +0000618 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
Nadav Rotem66de2af2013-01-11 22:57:48 +0000619 return DAG.UnrollVectorOp(Op.getNode());
620
621 DebugLoc DL = Op.getDebugLoc();
622 EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT();
623
624 unsigned BW = VT.getScalarType().getSizeInBits();
625 unsigned OrigBW = OrigTy.getScalarType().getSizeInBits();
626 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, VT);
627
628 Op = Op.getOperand(0);
Benjamin Kramer4dc47832013-01-12 19:06:44 +0000629 Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz);
Nadav Rotem66de2af2013-01-11 22:57:48 +0000630 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
631}
632
Nadav Rotemaec58612011-09-13 19:17:42 +0000633SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
634 // Implement VSELECT in terms of XOR, AND, OR
635 // on platforms which do not support blend natively.
636 EVT VT = Op.getOperand(0).getValueType();
Nadav Rotemaec58612011-09-13 19:17:42 +0000637 DebugLoc DL = Op.getDebugLoc();
638
639 SDValue Mask = Op.getOperand(0);
640 SDValue Op1 = Op.getOperand(1);
641 SDValue Op2 = Op.getOperand(2);
642
643 // If we can't even use the basic vector operations of
644 // AND,OR,XOR, we will have to scalarize the op.
Nadav Rotem815af822011-10-19 20:43:16 +0000645 // Notice that the operation may be 'promoted' which means that it is
646 // 'bitcasted' to another type which is handled.
Pete Cooperd9060172012-09-01 22:27:48 +0000647 // This operation also isn't safe with AND, OR, XOR when the boolean
648 // type is 0/1 as we need an all ones vector constant to mask with.
649 // FIXME: Sign extend 1 to all ones if thats legal on the target.
Nadav Rotem815af822011-10-19 20:43:16 +0000650 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
651 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
Pete Cooperd9060172012-09-01 22:27:48 +0000652 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
653 TLI.getBooleanContents(true) !=
654 TargetLowering::ZeroOrNegativeOneBooleanContent)
Nadav Rotem815af822011-10-19 20:43:16 +0000655 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotemaec58612011-09-13 19:17:42 +0000656
Nadav Roteme757f002012-08-30 19:17:29 +0000657 assert(VT.getSizeInBits() == Op1.getValueType().getSizeInBits()
Duncan Sands17001ce2011-10-18 12:44:00 +0000658 && "Invalid mask size");
Nadav Rotemaec58612011-09-13 19:17:42 +0000659 // Bitcast the operands to be the same type as the mask.
660 // This is needed when we select between FP types because
661 // the mask is a vector of integers.
662 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
663 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
664
665 SDValue AllOnes = DAG.getConstant(
666 APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
667 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
668
669 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
670 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
Nadav Rotem3ab32ea2012-04-15 15:08:09 +0000671 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
672 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
Nadav Rotemaec58612011-09-13 19:17:42 +0000673}
674
Nadav Rotem06cc3242011-03-19 13:09:10 +0000675SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
Nadav Rotem06cc3242011-03-19 13:09:10 +0000676 EVT VT = Op.getOperand(0).getValueType();
677 DebugLoc DL = Op.getDebugLoc();
678
679 // Make sure that the SINT_TO_FP and SRL instructions are available.
Nadav Rotem815af822011-10-19 20:43:16 +0000680 if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
681 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
682 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotem06cc3242011-03-19 13:09:10 +0000683
684 EVT SVT = VT.getScalarType();
685 assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
686 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
687
688 unsigned BW = SVT.getSizeInBits();
689 SDValue HalfWord = DAG.getConstant(BW/2, VT);
690
691 // Constants to clear the upper part of the word.
692 // Notice that we can also use SHL+SHR, but using a constant is slightly
693 // faster on x86.
694 uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
695 SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
696
697 // Two to the power of half-word-size.
698 SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
699
700 // Clear upper part of LO, lower HI
701 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
702 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
703
704 // Convert hi and lo to floats
705 // Convert the hi part back to the upper values
706 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
707 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
708 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
709
710 // Add the two halves
711 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
712}
713
714
Eli Friedman5c22c802009-05-23 12:35:30 +0000715SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
716 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
717 SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
718 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
719 Zero, Op.getOperand(0));
720 }
Mon P Wangcd6e7252009-11-30 02:42:02 +0000721 return DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000722}
723
724SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +0000725 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000726 unsigned NumElems = VT.getVectorNumElements();
Owen Andersone50ed302009-08-10 22:56:29 +0000727 EVT EltVT = VT.getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000728 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +0000729 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000730 DebugLoc dl = Op.getDebugLoc();
731 SmallVector<SDValue, 8> Ops(NumElems);
732 for (unsigned i = 0; i < NumElems; ++i) {
733 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
734 DAG.getIntPtrConstant(i));
735 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
736 DAG.getIntPtrConstant(i));
737 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
738 LHSElem, RHSElem, CC);
739 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
740 DAG.getConstant(APInt::getAllOnesValue
741 (EltVT.getSizeInBits()), EltVT),
742 DAG.getConstant(0, EltVT));
743 }
744 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
745}
746
Eli Friedman5c22c802009-05-23 12:35:30 +0000747}
748
749bool SelectionDAG::LegalizeVectors() {
750 return VectorLegalizer(*this).Run();
751}