blob: 8c040805a37b5a9768304b0e649d5d9738be7442 [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
62 /// \brief Implements expansion for FNEG; falls back to UnrollVectorOp if
63 /// FSUB isn't legal.
64 ///
65 /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
66 /// SINT_TO_FLOAT and SHR on vectors isn't legal.
Nadav Roteme7a101c2011-03-19 13:09:10 +000067 SDValue ExpandUINT_TO_FLOAT(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000068
69 /// \brief Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
Nadav Rotemdbe5c722013-01-11 22:57:48 +000070 SDValue ExpandSEXTINREG(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000071
72 /// \brief Expand bswap of vectors into a shuffle if legal.
Benjamin Kramerf3ad2352014-05-19 13:12:38 +000073 SDValue ExpandBSWAP(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000074
75 /// \brief Implement vselect in terms of XOR, AND, OR when blend is not
76 /// supported by the target.
Nadav Rotem52202fb2011-09-13 19:17:42 +000077 SDValue ExpandVSELECT(SDValue Op);
Nadav Rotemea973bd2012-08-30 19:17:29 +000078 SDValue ExpandSELECT(SDValue Op);
Nadav Rotemebe13bc2011-10-15 07:41:10 +000079 SDValue ExpandLoad(SDValue Op);
80 SDValue ExpandStore(SDValue Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +000081 SDValue ExpandFNEG(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000082
83 /// \brief Implements vector promotion.
84 ///
85 /// This is essentially just bitcasting the operands to a different type and
86 /// bitcasting the result back to the original type.
Chandler Carruth1cfa8952014-07-02 03:07:11 +000087 SDValue Promote(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000088
89 /// \brief Implements [SU]INT_TO_FP vector promotion.
90 ///
91 /// This is a [zs]ext of the input operand to the next size up.
Chandler Carruth1cfa8952014-07-02 03:07:11 +000092 SDValue PromoteINT_TO_FP(SDValue Op);
Chandler Carruth68adf152014-07-02 02:16:57 +000093
94 /// \brief Implements FP_TO_[SU]INT vector promotion of the result type.
95 ///
96 /// It is promoted to the next size up integer type. The result is then
97 /// truncated back to the original type.
Chandler Carruth1cfa8952014-07-02 03:07:11 +000098 SDValue PromoteFP_TO_INT(SDValue Op, bool isSigned);
Eli Friedmanda90dd62009-05-23 12:35:30 +000099
Chandler Carruth68adf152014-07-02 02:16:57 +0000100public:
101 /// \brief Begin legalizer the vector operations in the DAG.
Eli Friedmanda90dd62009-05-23 12:35:30 +0000102 bool Run();
103 VectorLegalizer(SelectionDAG& dag) :
104 DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
105};
106
107bool VectorLegalizer::Run() {
Nadav Rotemb7f90bd2013-02-22 23:33:30 +0000108 // Before we start legalizing vector nodes, check if there are any vectors.
109 bool HasVectors = false;
110 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000111 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
Nadav Rotemb7f90bd2013-02-22 23:33:30 +0000112 // Check if the values of the nodes contain vectors. We don't need to check
113 // the operands because we are going to check their values at some point.
114 for (SDNode::value_iterator J = I->value_begin(), E = I->value_end();
115 J != E; ++J)
116 HasVectors |= J->isVector();
117
118 // If we found a vector node we can start the legalization.
119 if (HasVectors)
120 break;
121 }
122
123 // If this basic block has no vectors then no need to legalize vectors.
124 if (!HasVectors)
125 return false;
126
Eli Friedmanda90dd62009-05-23 12:35:30 +0000127 // The legalize process is inherently a bottom-up recursive process (users
128 // legalize their uses before themselves). Given infinite stack space, we
129 // could just start legalizing on the root and traverse the whole graph. In
130 // practice however, this causes us to run out of stack space on large basic
131 // blocks. To avoid this problem, compute an ordering of the nodes where each
132 // node is only legalized after all of its operands are legalized.
133 DAG.AssignTopologicalOrder();
134 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000135 E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
Eli Friedmanda90dd62009-05-23 12:35:30 +0000136 LegalizeOp(SDValue(I, 0));
137
138 // Finally, it's possible the root changed. Get the new root.
139 SDValue OldRoot = DAG.getRoot();
140 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
141 DAG.setRoot(LegalizedNodes[OldRoot]);
142
143 LegalizedNodes.clear();
144
145 // Remove dead nodes now.
146 DAG.RemoveDeadNodes();
147
148 return Changed;
149}
150
151SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
152 // Generic legalization: just pass the operand through.
153 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
154 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
155 return Result.getValue(Op.getResNo());
156}
157
158SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
159 // Note that LegalizeOp may be reentered even from single-use nodes, which
160 // means that we always must cache transformed nodes.
161 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
162 if (I != LegalizedNodes.end()) return I->second;
163
164 SDNode* Node = Op.getNode();
165
166 // Legalize the operands
167 SmallVector<SDValue, 8> Ops;
168 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
169 Ops.push_back(LegalizeOp(Node->getOperand(i)));
170
Craig Topper8c0b4d02014-04-28 05:57:50 +0000171 SDValue Result = SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops), 0);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000172
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000173 if (Op.getOpcode() == ISD::LOAD) {
174 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
175 ISD::LoadExtType ExtType = LD->getExtensionType();
176 if (LD->getMemoryVT().isVector() && ExtType != ISD::NON_EXTLOAD) {
177 if (TLI.isLoadExtLegal(LD->getExtensionType(), LD->getMemoryVT()))
178 return TranslateLegalizeResults(Op, Result);
179 Changed = true;
180 return LegalizeOp(ExpandLoad(Op));
181 }
182 } else if (Op.getOpcode() == ISD::STORE) {
183 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
184 EVT StVT = ST->getMemoryVT();
Patrik Hagglundd7cdcf82012-12-19 08:28:51 +0000185 MVT ValVT = ST->getValue().getSimpleValueType();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000186 if (StVT.isVector() && ST->isTruncatingStore())
Patrik Hagglundd7cdcf82012-12-19 08:28:51 +0000187 switch (TLI.getTruncStoreAction(ValVT, StVT.getSimpleVT())) {
Craig Topperee4dab52012-02-05 08:31:47 +0000188 default: llvm_unreachable("This action is not supported yet!");
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000189 case TargetLowering::Legal:
190 return TranslateLegalizeResults(Op, Result);
191 case TargetLowering::Custom:
192 Changed = true;
Tom Stellard1b2c2d82013-08-21 22:42:58 +0000193 return TranslateLegalizeResults(Op, TLI.LowerOperation(Result, DAG));
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000194 case TargetLowering::Expand:
195 Changed = true;
196 return LegalizeOp(ExpandStore(Op));
197 }
198 }
199
Eli Friedmanda90dd62009-05-23 12:35:30 +0000200 bool HasVectorValue = false;
201 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
202 J != E;
203 ++J)
204 HasVectorValue |= J->isVector();
205 if (!HasVectorValue)
206 return TranslateLegalizeResults(Op, Result);
207
Owen Anderson53aa7a92009-08-10 22:56:29 +0000208 EVT QueryType;
Eli Friedmanda90dd62009-05-23 12:35:30 +0000209 switch (Op.getOpcode()) {
210 default:
211 return TranslateLegalizeResults(Op, Result);
212 case ISD::ADD:
213 case ISD::SUB:
214 case ISD::MUL:
215 case ISD::SDIV:
216 case ISD::UDIV:
217 case ISD::SREM:
218 case ISD::UREM:
219 case ISD::FADD:
220 case ISD::FSUB:
221 case ISD::FMUL:
222 case ISD::FDIV:
223 case ISD::FREM:
224 case ISD::AND:
225 case ISD::OR:
226 case ISD::XOR:
227 case ISD::SHL:
228 case ISD::SRA:
229 case ISD::SRL:
230 case ISD::ROTL:
231 case ISD::ROTR:
Hal Finkel5c968d92014-02-03 17:27:25 +0000232 case ISD::BSWAP:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000233 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000234 case ISD::CTTZ:
235 case ISD::CTLZ_ZERO_UNDEF:
236 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000237 case ISD::CTPOP:
238 case ISD::SELECT:
Nadav Rotem52202fb2011-09-13 19:17:42 +0000239 case ISD::VSELECT:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000240 case ISD::SELECT_CC:
Duncan Sandsf2641e12011-09-06 19:07:46 +0000241 case ISD::SETCC:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000242 case ISD::ZERO_EXTEND:
243 case ISD::ANY_EXTEND:
244 case ISD::TRUNCATE:
245 case ISD::SIGN_EXTEND:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000246 case ISD::FP_TO_SINT:
247 case ISD::FP_TO_UINT:
248 case ISD::FNEG:
249 case ISD::FABS:
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000250 case ISD::FCOPYSIGN:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000251 case ISD::FSQRT:
252 case ISD::FSIN:
253 case ISD::FCOS:
254 case ISD::FPOWI:
255 case ISD::FPOW:
256 case ISD::FLOG:
257 case ISD::FLOG2:
258 case ISD::FLOG10:
259 case ISD::FEXP:
260 case ISD::FEXP2:
261 case ISD::FCEIL:
262 case ISD::FTRUNC:
263 case ISD::FRINT:
264 case ISD::FNEARBYINT:
Hal Finkel171817e2013-08-07 22:49:12 +0000265 case ISD::FROUND:
Eli Friedmanda90dd62009-05-23 12:35:30 +0000266 case ISD::FFLOOR:
Eli Friedmane6385e62012-11-15 22:44:27 +0000267 case ISD::FP_ROUND:
Eli Friedman30834942012-11-17 01:52:46 +0000268 case ISD::FP_EXTEND:
Craig Topper2da13f92012-08-30 07:34:22 +0000269 case ISD::FMA:
Nadav Rotem771f2962011-07-14 11:11:14 +0000270 case ISD::SIGN_EXTEND_INREG:
Eli Friedmanaea9b652009-06-06 03:27:50 +0000271 QueryType = Node->getValueType(0);
272 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +0000273 case ISD::FP_ROUND_INREG:
274 QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
275 break;
Eli Friedmanaea9b652009-06-06 03:27:50 +0000276 case ISD::SINT_TO_FP:
277 case ISD::UINT_TO_FP:
278 QueryType = Node->getOperand(0).getValueType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000279 break;
280 }
281
Eli Friedmanaea9b652009-06-06 03:27:50 +0000282 switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
Eli Friedmanda90dd62009-05-23 12:35:30 +0000283 case TargetLowering::Promote:
Chandler Carruth2746c282014-07-02 03:07:15 +0000284 Result = Promote(Op);
285 Changed = true;
Eli Friedmanda90dd62009-05-23 12:35:30 +0000286 break;
Chandler Carruth2746c282014-07-02 03:07:15 +0000287 case TargetLowering::Legal:
288 break;
Eli Friedmanda90dd62009-05-23 12:35:30 +0000289 case TargetLowering::Custom: {
290 SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
291 if (Tmp1.getNode()) {
292 Result = Tmp1;
293 break;
294 }
295 // FALL THROUGH
296 }
297 case TargetLowering::Expand:
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000298 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG)
299 Result = ExpandSEXTINREG(Op);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000300 else if (Node->getOpcode() == ISD::BSWAP)
301 Result = ExpandBSWAP(Op);
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000302 else if (Node->getOpcode() == ISD::VSELECT)
Nadav Rotem52202fb2011-09-13 19:17:42 +0000303 Result = ExpandVSELECT(Op);
Nadav Rotemea973bd2012-08-30 19:17:29 +0000304 else if (Node->getOpcode() == ISD::SELECT)
305 Result = ExpandSELECT(Op);
Nadav Rotem52202fb2011-09-13 19:17:42 +0000306 else if (Node->getOpcode() == ISD::UINT_TO_FP)
Nadav Roteme7a101c2011-03-19 13:09:10 +0000307 Result = ExpandUINT_TO_FLOAT(Op);
308 else if (Node->getOpcode() == ISD::FNEG)
Eli Friedmanda90dd62009-05-23 12:35:30 +0000309 Result = ExpandFNEG(Op);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000310 else if (Node->getOpcode() == ISD::SETCC)
Eli Friedmanda90dd62009-05-23 12:35:30 +0000311 Result = UnrollVSETCC(Op);
312 else
Mon P Wang32f8bb92009-11-30 02:42:02 +0000313 Result = DAG.UnrollVectorOp(Op.getNode());
Eli Friedmanda90dd62009-05-23 12:35:30 +0000314 break;
315 }
316
317 // Make sure that the generated code is itself legal.
318 if (Result != Op) {
319 Result = LegalizeOp(Result);
320 Changed = true;
321 }
322
323 // Note that LegalizeOp may be reentered even from single-use nodes, which
324 // means that we always must cache transformed nodes.
325 AddLegalizedOperand(Op, Result);
326 return Result;
327}
328
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000329SDValue VectorLegalizer::Promote(SDValue Op) {
Chandler Carruth2746c282014-07-02 03:07:15 +0000330 // For a few operations there is a specific concept for promotion based on
331 // the operand's type.
332 switch (Op.getOpcode()) {
333 case ISD::SINT_TO_FP:
334 case ISD::UINT_TO_FP:
335 // "Promote" the operation by extending the operand.
336 return PromoteINT_TO_FP(Op);
Chandler Carruth2746c282014-07-02 03:07:15 +0000337 case ISD::FP_TO_UINT:
338 case ISD::FP_TO_SINT:
339 // Promote the operation by extending the operand.
340 return PromoteFP_TO_INT(Op, Op->getOpcode() == ISD::FP_TO_SINT);
Chandler Carruth2746c282014-07-02 03:07:15 +0000341 }
342
343 // The rest of the time, vector "promotion" is basically just bitcasting and
344 // doing the operation in a different type. For example, x86 promotes
345 // ISD::AND on v2i32 to v1i64.
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000346 MVT VT = Op.getSimpleValueType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000347 assert(Op.getNode()->getNumValues() == 1 &&
348 "Can't promote a vector with multiple results!");
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000349 MVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000350 SDLoc dl(Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000351 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
352
353 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
354 if (Op.getOperand(j).getValueType().isVector())
Wesley Peck527da1b2010-11-23 03:31:01 +0000355 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
Eli Friedmanda90dd62009-05-23 12:35:30 +0000356 else
357 Operands[j] = Op.getOperand(j);
358 }
359
Craig Topper48d114b2014-04-26 18:35:24 +0000360 Op = DAG.getNode(Op.getOpcode(), dl, NVT, Operands);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000361
Wesley Peck527da1b2010-11-23 03:31:01 +0000362 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000363}
364
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000365SDValue VectorLegalizer::PromoteINT_TO_FP(SDValue Op) {
Jim Grosbache0c10d82012-06-28 21:03:44 +0000366 // INT_TO_FP operations may require the input operand be promoted even
367 // when the type is otherwise legal.
368 EVT VT = Op.getOperand(0).getValueType();
369 assert(Op.getNode()->getNumValues() == 1 &&
370 "Can't promote a vector with multiple results!");
371
372 // Normal getTypeToPromoteTo() doesn't work here, as that will promote
373 // by widening the vector w/ the same element width and twice the number
374 // of elements. We want the other way around, the same number of elements,
375 // each twice the width.
376 //
377 // Increase the bitwidth of the element to the next pow-of-two
378 // (which is greater than 8 bits).
Jim Grosbache0c10d82012-06-28 21:03:44 +0000379
Adam Nemet24381f12014-03-17 17:06:14 +0000380 EVT NVT = VT.widenIntegerVectorElementType(*DAG.getContext());
381 assert(NVT.isSimple() && "Promoting to a non-simple vector type!");
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000382 SDLoc dl(Op);
Jim Grosbache0c10d82012-06-28 21:03:44 +0000383 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
384
385 unsigned Opc = Op.getOpcode() == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND :
386 ISD::SIGN_EXTEND;
387 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
388 if (Op.getOperand(j).getValueType().isVector())
389 Operands[j] = DAG.getNode(Opc, dl, NVT, Op.getOperand(j));
390 else
391 Operands[j] = Op.getOperand(j);
392 }
393
Craig Topper48d114b2014-04-26 18:35:24 +0000394 return DAG.getNode(Op.getOpcode(), dl, Op.getValueType(), Operands);
Jim Grosbache0c10d82012-06-28 21:03:44 +0000395}
396
Adam Nemet24381f12014-03-17 17:06:14 +0000397// For FP_TO_INT we promote the result type to a vector type with wider
398// elements and then truncate the result. This is different from the default
399// PromoteVector which uses bitcast to promote thus assumning that the
400// promoted vector type has the same overall size.
Chandler Carruth1cfa8952014-07-02 03:07:11 +0000401SDValue VectorLegalizer::PromoteFP_TO_INT(SDValue Op, bool isSigned) {
Adam Nemet24381f12014-03-17 17:06:14 +0000402 assert(Op.getNode()->getNumValues() == 1 &&
403 "Can't promote a vector with multiple results!");
404 EVT VT = Op.getValueType();
405
406 EVT NewVT;
407 unsigned NewOpc;
408 while (1) {
409 NewVT = VT.widenIntegerVectorElementType(*DAG.getContext());
410 assert(NewVT.isSimple() && "Promoting to a non-simple vector type!");
411 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewVT)) {
412 NewOpc = ISD::FP_TO_SINT;
413 break;
414 }
415 if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewVT)) {
416 NewOpc = ISD::FP_TO_UINT;
417 break;
418 }
419 }
420
421 SDLoc loc(Op);
422 SDValue promoted = DAG.getNode(NewOpc, SDLoc(Op), NewVT, Op.getOperand(0));
423 return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT, promoted);
424}
425
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000426
427SDValue VectorLegalizer::ExpandLoad(SDValue Op) {
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000428 SDLoc dl(Op);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000429 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
430 SDValue Chain = LD->getChain();
431 SDValue BasePTR = LD->getBasePtr();
432 EVT SrcVT = LD->getMemoryVT();
Nadav Rotem75c22292011-10-18 22:32:43 +0000433 ISD::LoadExtType ExtType = LD->getExtensionType();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000434
Michael Liao7fb39662013-02-20 18:04:21 +0000435 SmallVector<SDValue, 8> Vals;
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000436 SmallVector<SDValue, 8> LoadChains;
437 unsigned NumElem = SrcVT.getVectorNumElements();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000438
Michael Liao7fb39662013-02-20 18:04:21 +0000439 EVT SrcEltVT = SrcVT.getScalarType();
440 EVT DstEltVT = Op.getNode()->getValueType(0).getScalarType();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000441
Michael Liao7fb39662013-02-20 18:04:21 +0000442 if (SrcVT.getVectorNumElements() > 1 && !SrcEltVT.isByteSized()) {
443 // When elements in a vector is not byte-addressable, we cannot directly
444 // load each element by advancing pointer, which could only address bytes.
445 // Instead, we load all significant words, mask bits off, and concatenate
446 // them to form each element. Finally, they are extended to destination
447 // scalar type to build the destination vector.
448 EVT WideVT = TLI.getPointerTy();
Nadav Rotem75c22292011-10-18 22:32:43 +0000449
Michael Liao7fb39662013-02-20 18:04:21 +0000450 assert(WideVT.isRound() &&
451 "Could not handle the sophisticated case when the widest integer is"
452 " not power of 2.");
453 assert(WideVT.bitsGE(SrcEltVT) &&
454 "Type is not legalized?");
455
456 unsigned WideBytes = WideVT.getStoreSize();
457 unsigned Offset = 0;
458 unsigned RemainingBytes = SrcVT.getStoreSize();
459 SmallVector<SDValue, 8> LoadVals;
460
461 while (RemainingBytes > 0) {
462 SDValue ScalarLoad;
463 unsigned LoadBytes = WideBytes;
464
465 if (RemainingBytes >= LoadBytes) {
466 ScalarLoad = DAG.getLoad(WideVT, dl, Chain, BasePTR,
467 LD->getPointerInfo().getWithOffset(Offset),
468 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000469 LD->isInvariant(), LD->getAlignment(),
470 LD->getTBAAInfo());
Michael Liao7fb39662013-02-20 18:04:21 +0000471 } else {
472 EVT LoadVT = WideVT;
473 while (RemainingBytes < LoadBytes) {
474 LoadBytes >>= 1; // Reduce the load size by half.
475 LoadVT = EVT::getIntegerVT(*DAG.getContext(), LoadBytes << 3);
476 }
477 ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, WideVT, Chain, BasePTR,
478 LD->getPointerInfo().getWithOffset(Offset),
479 LoadVT, LD->isVolatile(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000480 LD->isNonTemporal(), LD->getAlignment(),
481 LD->getTBAAInfo());
Michael Liao7fb39662013-02-20 18:04:21 +0000482 }
483
484 RemainingBytes -= LoadBytes;
485 Offset += LoadBytes;
486 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
Tom Stellard838e2342013-08-26 15:06:10 +0000487 DAG.getConstant(LoadBytes, BasePTR.getValueType()));
Michael Liao7fb39662013-02-20 18:04:21 +0000488
489 LoadVals.push_back(ScalarLoad.getValue(0));
490 LoadChains.push_back(ScalarLoad.getValue(1));
491 }
492
493 // Extract bits, pack and extend/trunc them into destination type.
494 unsigned SrcEltBits = SrcEltVT.getSizeInBits();
495 SDValue SrcEltBitMask = DAG.getConstant((1U << SrcEltBits) - 1, WideVT);
496
497 unsigned BitOffset = 0;
498 unsigned WideIdx = 0;
499 unsigned WideBits = WideVT.getSizeInBits();
500
501 for (unsigned Idx = 0; Idx != NumElem; ++Idx) {
502 SDValue Lo, Hi, ShAmt;
503
504 if (BitOffset < WideBits) {
505 ShAmt = DAG.getConstant(BitOffset, TLI.getShiftAmountTy(WideVT));
506 Lo = DAG.getNode(ISD::SRL, dl, WideVT, LoadVals[WideIdx], ShAmt);
507 Lo = DAG.getNode(ISD::AND, dl, WideVT, Lo, SrcEltBitMask);
508 }
509
510 BitOffset += SrcEltBits;
511 if (BitOffset >= WideBits) {
512 WideIdx++;
513 Offset -= WideBits;
514 if (Offset > 0) {
515 ShAmt = DAG.getConstant(SrcEltBits - Offset,
516 TLI.getShiftAmountTy(WideVT));
517 Hi = DAG.getNode(ISD::SHL, dl, WideVT, LoadVals[WideIdx], ShAmt);
518 Hi = DAG.getNode(ISD::AND, dl, WideVT, Hi, SrcEltBitMask);
519 }
520 }
521
522 if (Hi.getNode())
523 Lo = DAG.getNode(ISD::OR, dl, WideVT, Lo, Hi);
524
525 switch (ExtType) {
526 default: llvm_unreachable("Unknown extended-load op!");
527 case ISD::EXTLOAD:
528 Lo = DAG.getAnyExtOrTrunc(Lo, dl, DstEltVT);
529 break;
530 case ISD::ZEXTLOAD:
531 Lo = DAG.getZExtOrTrunc(Lo, dl, DstEltVT);
532 break;
533 case ISD::SEXTLOAD:
534 ShAmt = DAG.getConstant(WideBits - SrcEltBits,
535 TLI.getShiftAmountTy(WideVT));
536 Lo = DAG.getNode(ISD::SHL, dl, WideVT, Lo, ShAmt);
537 Lo = DAG.getNode(ISD::SRA, dl, WideVT, Lo, ShAmt);
538 Lo = DAG.getSExtOrTrunc(Lo, dl, DstEltVT);
539 break;
540 }
541 Vals.push_back(Lo);
542 }
543 } else {
544 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
545
546 for (unsigned Idx=0; Idx<NumElem; Idx++) {
547 SDValue ScalarLoad = DAG.getExtLoad(ExtType, dl,
548 Op.getNode()->getValueType(0).getScalarType(),
549 Chain, BasePTR, LD->getPointerInfo().getWithOffset(Idx * Stride),
550 SrcVT.getScalarType(),
551 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000552 LD->getAlignment(), LD->getTBAAInfo());
Michael Liao7fb39662013-02-20 18:04:21 +0000553
554 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
Tom Stellard838e2342013-08-26 15:06:10 +0000555 DAG.getConstant(Stride, BasePTR.getValueType()));
Michael Liao7fb39662013-02-20 18:04:21 +0000556
557 Vals.push_back(ScalarLoad.getValue(0));
558 LoadChains.push_back(ScalarLoad.getValue(1));
559 }
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000560 }
Nadav Rotem75c22292011-10-18 22:32:43 +0000561
Craig Topper48d114b2014-04-26 18:35:24 +0000562 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000563 SDValue Value = DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +0000564 Op.getNode()->getValueType(0), Vals);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000565
566 AddLegalizedOperand(Op.getValue(0), Value);
567 AddLegalizedOperand(Op.getValue(1), NewChain);
568
569 return (Op.getResNo() ? NewChain : Value);
570}
571
572SDValue VectorLegalizer::ExpandStore(SDValue Op) {
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000573 SDLoc dl(Op);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000574 StoreSDNode *ST = cast<StoreSDNode>(Op.getNode());
575 SDValue Chain = ST->getChain();
576 SDValue BasePTR = ST->getBasePtr();
577 SDValue Value = ST->getValue();
578 EVT StVT = ST->getMemoryVT();
579
580 unsigned Alignment = ST->getAlignment();
581 bool isVolatile = ST->isVolatile();
582 bool isNonTemporal = ST->isNonTemporal();
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000583 const MDNode *TBAAInfo = ST->getTBAAInfo();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000584
585 unsigned NumElem = StVT.getVectorNumElements();
586 // The type of the data we want to save
587 EVT RegVT = Value.getValueType();
588 EVT RegSclVT = RegVT.getScalarType();
589 // The type of data as saved in memory.
590 EVT MemSclVT = StVT.getScalarType();
591
592 // Cast floats into integers
593 unsigned ScalarSize = MemSclVT.getSizeInBits();
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000594
595 // Round odd types to the next pow of two.
596 if (!isPowerOf2_32(ScalarSize))
597 ScalarSize = NextPowerOf2(ScalarSize);
598
599 // Store Stride in bytes
600 unsigned Stride = ScalarSize/8;
601 // Extract each of the elements from the original vector
602 // and save them into memory individually.
603 SmallVector<SDValue, 8> Stores;
604 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
605 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Tom Stellardd42c5942013-08-05 22:22:01 +0000606 RegSclVT, Value, DAG.getConstant(Idx, TLI.getVectorIdxTy()));
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000607
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000608 // This scalar TruncStore may be illegal, but we legalize it later.
609 SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
610 ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000611 isVolatile, isNonTemporal, Alignment, TBAAInfo);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000612
Nadav Rotem75c22292011-10-18 22:32:43 +0000613 BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,
Tom Stellard838e2342013-08-26 15:06:10 +0000614 DAG.getConstant(Stride, BasePTR.getValueType()));
Nadav Rotem75c22292011-10-18 22:32:43 +0000615
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000616 Stores.push_back(Store);
617 }
Craig Topper48d114b2014-04-26 18:35:24 +0000618 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
Nadav Rotemebe13bc2011-10-15 07:41:10 +0000619 AddLegalizedOperand(Op, TF);
620 return TF;
621}
622
Nadav Rotemea973bd2012-08-30 19:17:29 +0000623SDValue VectorLegalizer::ExpandSELECT(SDValue Op) {
624 // Lower a select instruction where the condition is a scalar and the
625 // operands are vectors. Lower this select to VSELECT and implement it
Stephen Lincfe7f352013-07-08 00:37:03 +0000626 // using XOR AND OR. The selector bit is broadcasted.
Nadav Rotemea973bd2012-08-30 19:17:29 +0000627 EVT VT = Op.getValueType();
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000628 SDLoc DL(Op);
Nadav Rotemea973bd2012-08-30 19:17:29 +0000629
630 SDValue Mask = Op.getOperand(0);
631 SDValue Op1 = Op.getOperand(1);
632 SDValue Op2 = Op.getOperand(2);
633
634 assert(VT.isVector() && !Mask.getValueType().isVector()
635 && Op1.getValueType() == Op2.getValueType() && "Invalid type");
636
637 unsigned NumElem = VT.getVectorNumElements();
638
639 // If we can't even use the basic vector operations of
640 // AND,OR,XOR, we will have to scalarize the op.
641 // Notice that the operation may be 'promoted' which means that it is
642 // 'bitcasted' to another type which is handled.
643 // Also, we need to be able to construct a splat vector using BUILD_VECTOR.
644 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
645 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
646 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
647 TLI.getOperationAction(ISD::BUILD_VECTOR, VT) == TargetLowering::Expand)
648 return DAG.UnrollVectorOp(Op.getNode());
649
650 // Generate a mask operand.
Matt Arsenaultd2322222013-09-10 00:41:56 +0000651 EVT MaskTy = VT.changeVectorElementTypeToInteger();
Nadav Rotemea973bd2012-08-30 19:17:29 +0000652
653 // What is the size of each element in the vector mask.
654 EVT BitTy = MaskTy.getScalarType();
655
Matt Arsenaultd2f03322013-06-14 22:04:37 +0000656 Mask = DAG.getSelect(DL, BitTy, Mask,
Nadav Rotem500d6912012-09-02 08:20:07 +0000657 DAG.getConstant(APInt::getAllOnesValue(BitTy.getSizeInBits()), BitTy),
Nadav Rotem10f6b882012-09-02 12:21:50 +0000658 DAG.getConstant(0, BitTy));
Nadav Rotemea973bd2012-08-30 19:17:29 +0000659
660 // Broadcast the mask so that the entire vector is all-one or all zero.
661 SmallVector<SDValue, 8> Ops(NumElem, Mask);
Craig Topper48d114b2014-04-26 18:35:24 +0000662 Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, MaskTy, Ops);
Nadav Rotemea973bd2012-08-30 19:17:29 +0000663
664 // Bitcast the operands to be the same type as the mask.
665 // This is needed when we select between FP types because
666 // the mask is a vector of integers.
667 Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
668 Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
669
670 SDValue AllOnes = DAG.getConstant(
671 APInt::getAllOnesValue(BitTy.getSizeInBits()), MaskTy);
672 SDValue NotMask = DAG.getNode(ISD::XOR, DL, MaskTy, Mask, AllOnes);
673
674 Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
675 Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
676 SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
677 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
678}
679
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000680SDValue VectorLegalizer::ExpandSEXTINREG(SDValue Op) {
681 EVT VT = Op.getValueType();
682
Benjamin Kramer5ea03492013-01-12 19:06:44 +0000683 // Make sure that the SRA and SHL instructions are available.
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000684 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
Benjamin Kramer5ea03492013-01-12 19:06:44 +0000685 TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000686 return DAG.UnrollVectorOp(Op.getNode());
687
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000688 SDLoc DL(Op);
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000689 EVT OrigTy = cast<VTSDNode>(Op->getOperand(1))->getVT();
690
691 unsigned BW = VT.getScalarType().getSizeInBits();
692 unsigned OrigBW = OrigTy.getScalarType().getSizeInBits();
693 SDValue ShiftSz = DAG.getConstant(BW - OrigBW, VT);
694
695 Op = Op.getOperand(0);
Benjamin Kramer5ea03492013-01-12 19:06:44 +0000696 Op = DAG.getNode(ISD::SHL, DL, VT, Op, ShiftSz);
Nadav Rotemdbe5c722013-01-11 22:57:48 +0000697 return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
698}
699
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000700SDValue VectorLegalizer::ExpandBSWAP(SDValue Op) {
701 EVT VT = Op.getValueType();
702
703 // Generate a byte wise shuffle mask for the BSWAP.
704 SmallVector<int, 16> ShuffleMask;
705 int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
706 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
707 for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
708 ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
709
710 EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
711
712 // Only emit a shuffle if the mask is legal.
713 if (!TLI.isShuffleMaskLegal(ShuffleMask, ByteVT))
714 return DAG.UnrollVectorOp(Op.getNode());
715
716 SDLoc DL(Op);
717 Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Op.getOperand(0));
718 Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
719 ShuffleMask.data());
720 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
721}
722
Nadav Rotem52202fb2011-09-13 19:17:42 +0000723SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
724 // Implement VSELECT in terms of XOR, AND, OR
725 // on platforms which do not support blend natively.
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000726 SDLoc DL(Op);
Nadav Rotem52202fb2011-09-13 19:17:42 +0000727
728 SDValue Mask = Op.getOperand(0);
729 SDValue Op1 = Op.getOperand(1);
730 SDValue Op2 = Op.getOperand(2);
731
Matt Arsenaulta5733dc2013-05-07 20:24:18 +0000732 EVT VT = Mask.getValueType();
733
Nadav Rotem52202fb2011-09-13 19:17:42 +0000734 // If we can't even use the basic vector operations of
735 // AND,OR,XOR, we will have to scalarize the op.
Nadav Rotem88244722011-10-19 20:43:16 +0000736 // Notice that the operation may be 'promoted' which means that it is
737 // 'bitcasted' to another type which is handled.
Pete Cooper2455e9c2012-09-01 22:27:48 +0000738 // This operation also isn't safe with AND, OR, XOR when the boolean
739 // type is 0/1 as we need an all ones vector constant to mask with.
740 // FIXME: Sign extend 1 to all ones if thats legal on the target.
Nadav Rotem88244722011-10-19 20:43:16 +0000741 if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
742 TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
Pete Cooper2455e9c2012-09-01 22:27:48 +0000743 TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
744 TLI.getBooleanContents(true) !=
745 TargetLowering::ZeroOrNegativeOneBooleanContent)
Nadav Rotem88244722011-10-19 20:43:16 +0000746 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotem52202fb2011-09-13 19:17:42 +0000747
Matt Arsenaulta5733dc2013-05-07 20:24:18 +0000748 // If the mask and the type are different sizes, unroll the vector op. This
749 // can occur when getSetCCResultType returns something that is different in
750 // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
751 if (VT.getSizeInBits() != Op1.getValueType().getSizeInBits())
752 return DAG.UnrollVectorOp(Op.getNode());
753
Nadav Rotem52202fb2011-09-13 19:17:42 +0000754 // Bitcast the operands to be the same type as the mask.
755 // This is needed when we select between FP types because
756 // the mask is a vector of integers.
757 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
758 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
759
760 SDValue AllOnes = DAG.getConstant(
761 APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
762 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
763
764 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
765 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
Nadav Rotem02ef0c32012-04-15 15:08:09 +0000766 SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
767 return DAG.getNode(ISD::BITCAST, DL, Op.getValueType(), Val);
Nadav Rotem52202fb2011-09-13 19:17:42 +0000768}
769
Nadav Roteme7a101c2011-03-19 13:09:10 +0000770SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
Nadav Roteme7a101c2011-03-19 13:09:10 +0000771 EVT VT = Op.getOperand(0).getValueType();
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000772 SDLoc DL(Op);
Nadav Roteme7a101c2011-03-19 13:09:10 +0000773
774 // Make sure that the SINT_TO_FP and SRL instructions are available.
Nadav Rotem88244722011-10-19 20:43:16 +0000775 if (TLI.getOperationAction(ISD::SINT_TO_FP, VT) == TargetLowering::Expand ||
776 TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand)
777 return DAG.UnrollVectorOp(Op.getNode());
Nadav Roteme7a101c2011-03-19 13:09:10 +0000778
779 EVT SVT = VT.getScalarType();
780 assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
781 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
782
783 unsigned BW = SVT.getSizeInBits();
784 SDValue HalfWord = DAG.getConstant(BW/2, VT);
785
786 // Constants to clear the upper part of the word.
787 // Notice that we can also use SHL+SHR, but using a constant is slightly
788 // faster on x86.
789 uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
790 SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
791
792 // Two to the power of half-word-size.
793 SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
794
795 // Clear upper part of LO, lower HI
796 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
797 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
798
799 // Convert hi and lo to floats
800 // Convert the hi part back to the upper values
801 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
802 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
803 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
804
805 // Add the two halves
806 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
807}
808
809
Eli Friedmanda90dd62009-05-23 12:35:30 +0000810SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
811 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
812 SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
Andrew Trickef9de2a2013-05-25 02:42:55 +0000813 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Eli Friedmanda90dd62009-05-23 12:35:30 +0000814 Zero, Op.getOperand(0));
815 }
Mon P Wang32f8bb92009-11-30 02:42:02 +0000816 return DAG.UnrollVectorOp(Op.getNode());
Eli Friedmanda90dd62009-05-23 12:35:30 +0000817}
818
819SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000820 EVT VT = Op.getValueType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000821 unsigned NumElems = VT.getVectorNumElements();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000822 EVT EltVT = VT.getVectorElementType();
Eli Friedmanda90dd62009-05-23 12:35:30 +0000823 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000824 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
Benjamin Kramer351d53c2013-05-28 16:31:26 +0000825 SDLoc dl(Op);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000826 SmallVector<SDValue, 8> Ops(NumElems);
827 for (unsigned i = 0; i < NumElems; ++i) {
828 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
Tom Stellardd42c5942013-08-05 22:22:01 +0000829 DAG.getConstant(i, TLI.getVectorIdxTy()));
Eli Friedmanda90dd62009-05-23 12:35:30 +0000830 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
Tom Stellardd42c5942013-08-05 22:22:01 +0000831 DAG.getConstant(i, TLI.getVectorIdxTy()));
Matt Arsenault758659232013-05-18 00:21:46 +0000832 Ops[i] = DAG.getNode(ISD::SETCC, dl,
833 TLI.getSetCCResultType(*DAG.getContext(), TmpEltVT),
Eli Friedmanda90dd62009-05-23 12:35:30 +0000834 LHSElem, RHSElem, CC);
Matt Arsenaultd2f03322013-06-14 22:04:37 +0000835 Ops[i] = DAG.getSelect(dl, EltVT, Ops[i],
836 DAG.getConstant(APInt::getAllOnesValue
837 (EltVT.getSizeInBits()), EltVT),
838 DAG.getConstant(0, EltVT));
Eli Friedmanda90dd62009-05-23 12:35:30 +0000839 }
Craig Topper48d114b2014-04-26 18:35:24 +0000840 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
Eli Friedmanda90dd62009-05-23 12:35:30 +0000841}
842
Eli Friedmanda90dd62009-05-23 12:35:30 +0000843}
844
845bool SelectionDAG::LegalizeVectors() {
846 return VectorLegalizer(*this).Run();
847}