blob: 80b4c6053a68da9689d38f2750c16280eed6c9c2 [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);
Eli Friedman5c22c802009-05-23 12:35:30 +000067 SDValue ExpandFNEG(SDValue Op);
68 // Implements vector promotion; this is essentially just bitcasting the
69 // operands to a different type and bitcasting the result back to the
70 // original type.
71 SDValue PromoteVectorOp(SDValue Op);
72
73 public:
74 bool Run();
75 VectorLegalizer(SelectionDAG& dag) :
76 DAG(dag), TLI(dag.getTargetLoweringInfo()), Changed(false) {}
77};
78
79bool VectorLegalizer::Run() {
80 // The legalize process is inherently a bottom-up recursive process (users
81 // legalize their uses before themselves). Given infinite stack space, we
82 // could just start legalizing on the root and traverse the whole graph. In
83 // practice however, this causes us to run out of stack space on large basic
84 // blocks. To avoid this problem, compute an ordering of the nodes where each
85 // node is only legalized after all of its operands are legalized.
86 DAG.AssignTopologicalOrder();
87 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +000088 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedman5c22c802009-05-23 12:35:30 +000089 LegalizeOp(SDValue(I, 0));
90
91 // Finally, it's possible the root changed. Get the new root.
92 SDValue OldRoot = DAG.getRoot();
93 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
94 DAG.setRoot(LegalizedNodes[OldRoot]);
95
96 LegalizedNodes.clear();
97
98 // Remove dead nodes now.
99 DAG.RemoveDeadNodes();
100
101 return Changed;
102}
103
104SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDValue Result) {
105 // Generic legalization: just pass the operand through.
106 for (unsigned i = 0, e = Op.getNode()->getNumValues(); i != e; ++i)
107 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
108 return Result.getValue(Op.getResNo());
109}
110
111SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
112 // Note that LegalizeOp may be reentered even from single-use nodes, which
113 // means that we always must cache transformed nodes.
114 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
115 if (I != LegalizedNodes.end()) return I->second;
116
117 SDNode* Node = Op.getNode();
118
119 // Legalize the operands
120 SmallVector<SDValue, 8> Ops;
121 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
122 Ops.push_back(LegalizeOp(Node->getOperand(i)));
123
124 SDValue Result =
Dan Gohman027657d2010-06-18 15:30:29 +0000125 SDValue(DAG.UpdateNodeOperands(Op.getNode(), Ops.data(), Ops.size()), 0);
Eli Friedman5c22c802009-05-23 12:35:30 +0000126
127 bool HasVectorValue = false;
128 for (SDNode::value_iterator J = Node->value_begin(), E = Node->value_end();
129 J != E;
130 ++J)
131 HasVectorValue |= J->isVector();
132 if (!HasVectorValue)
133 return TranslateLegalizeResults(Op, Result);
134
Owen Andersone50ed302009-08-10 22:56:29 +0000135 EVT QueryType;
Eli Friedman5c22c802009-05-23 12:35:30 +0000136 switch (Op.getOpcode()) {
137 default:
138 return TranslateLegalizeResults(Op, Result);
139 case ISD::ADD:
140 case ISD::SUB:
141 case ISD::MUL:
142 case ISD::SDIV:
143 case ISD::UDIV:
144 case ISD::SREM:
145 case ISD::UREM:
146 case ISD::FADD:
147 case ISD::FSUB:
148 case ISD::FMUL:
149 case ISD::FDIV:
150 case ISD::FREM:
151 case ISD::AND:
152 case ISD::OR:
153 case ISD::XOR:
154 case ISD::SHL:
155 case ISD::SRA:
156 case ISD::SRL:
157 case ISD::ROTL:
158 case ISD::ROTR:
159 case ISD::CTTZ:
160 case ISD::CTLZ:
161 case ISD::CTPOP:
162 case ISD::SELECT:
Nadav Rotemaec58612011-09-13 19:17:42 +0000163 case ISD::VSELECT:
Eli Friedman5c22c802009-05-23 12:35:30 +0000164 case ISD::SELECT_CC:
Duncan Sands28b77e92011-09-06 19:07:46 +0000165 case ISD::SETCC:
Eli Friedman5c22c802009-05-23 12:35:30 +0000166 case ISD::ZERO_EXTEND:
167 case ISD::ANY_EXTEND:
168 case ISD::TRUNCATE:
169 case ISD::SIGN_EXTEND:
Eli Friedman5c22c802009-05-23 12:35:30 +0000170 case ISD::FP_TO_SINT:
171 case ISD::FP_TO_UINT:
172 case ISD::FNEG:
173 case ISD::FABS:
174 case ISD::FSQRT:
175 case ISD::FSIN:
176 case ISD::FCOS:
177 case ISD::FPOWI:
178 case ISD::FPOW:
179 case ISD::FLOG:
180 case ISD::FLOG2:
181 case ISD::FLOG10:
182 case ISD::FEXP:
183 case ISD::FEXP2:
184 case ISD::FCEIL:
185 case ISD::FTRUNC:
186 case ISD::FRINT:
187 case ISD::FNEARBYINT:
188 case ISD::FFLOOR:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +0000189 case ISD::SIGN_EXTEND_INREG:
Eli Friedman556929a2009-06-06 03:27:50 +0000190 QueryType = Node->getValueType(0);
191 break;
Dan Gohmand1996362010-01-09 02:13:55 +0000192 case ISD::FP_ROUND_INREG:
193 QueryType = cast<VTSDNode>(Node->getOperand(1))->getVT();
194 break;
Eli Friedman556929a2009-06-06 03:27:50 +0000195 case ISD::SINT_TO_FP:
196 case ISD::UINT_TO_FP:
197 QueryType = Node->getOperand(0).getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000198 break;
199 }
200
Eli Friedman556929a2009-06-06 03:27:50 +0000201 switch (TLI.getOperationAction(Node->getOpcode(), QueryType)) {
Eli Friedman5c22c802009-05-23 12:35:30 +0000202 case TargetLowering::Promote:
203 // "Promote" the operation by bitcasting
204 Result = PromoteVectorOp(Op);
205 Changed = true;
206 break;
207 case TargetLowering::Legal: break;
208 case TargetLowering::Custom: {
209 SDValue Tmp1 = TLI.LowerOperation(Op, DAG);
210 if (Tmp1.getNode()) {
211 Result = Tmp1;
212 break;
213 }
214 // FALL THROUGH
215 }
216 case TargetLowering::Expand:
Nadav Rotemaec58612011-09-13 19:17:42 +0000217 if (Node->getOpcode() == ISD::VSELECT)
218 Result = ExpandVSELECT(Op);
219 else if (Node->getOpcode() == ISD::UINT_TO_FP)
Nadav Rotem06cc3242011-03-19 13:09:10 +0000220 Result = ExpandUINT_TO_FLOAT(Op);
221 else if (Node->getOpcode() == ISD::FNEG)
Eli Friedman5c22c802009-05-23 12:35:30 +0000222 Result = ExpandFNEG(Op);
Duncan Sands28b77e92011-09-06 19:07:46 +0000223 else if (Node->getOpcode() == ISD::SETCC)
Eli Friedman5c22c802009-05-23 12:35:30 +0000224 Result = UnrollVSETCC(Op);
225 else
Mon P Wangcd6e7252009-11-30 02:42:02 +0000226 Result = DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000227 break;
228 }
229
230 // Make sure that the generated code is itself legal.
231 if (Result != Op) {
232 Result = LegalizeOp(Result);
233 Changed = true;
234 }
235
236 // Note that LegalizeOp may be reentered even from single-use nodes, which
237 // means that we always must cache transformed nodes.
238 AddLegalizedOperand(Op, Result);
239 return Result;
240}
241
242SDValue VectorLegalizer::PromoteVectorOp(SDValue Op) {
Eli Friedmanc046c002009-05-24 20:32:10 +0000243 // Vector "promotion" is basically just bitcasting and doing the operation
244 // in a different type. For example, x86 promotes ISD::AND on v2i32 to
245 // v1i64.
Owen Andersone50ed302009-08-10 22:56:29 +0000246 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000247 assert(Op.getNode()->getNumValues() == 1 &&
248 "Can't promote a vector with multiple results!");
Owen Andersone50ed302009-08-10 22:56:29 +0000249 EVT NVT = TLI.getTypeToPromoteTo(Op.getOpcode(), VT);
Eli Friedman5c22c802009-05-23 12:35:30 +0000250 DebugLoc dl = Op.getDebugLoc();
251 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
252
253 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
254 if (Op.getOperand(j).getValueType().isVector())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000255 Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Op.getOperand(j));
Eli Friedman5c22c802009-05-23 12:35:30 +0000256 else
257 Operands[j] = Op.getOperand(j);
258 }
259
260 Op = DAG.getNode(Op.getOpcode(), dl, NVT, &Operands[0], Operands.size());
261
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000262 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Eli Friedman5c22c802009-05-23 12:35:30 +0000263}
264
Nadav Rotemaec58612011-09-13 19:17:42 +0000265SDValue VectorLegalizer::ExpandVSELECT(SDValue Op) {
266 // Implement VSELECT in terms of XOR, AND, OR
267 // on platforms which do not support blend natively.
268 EVT VT = Op.getOperand(0).getValueType();
Nadav Rotem8f28aaf2011-09-13 20:03:38 +0000269 EVT OVT = Op.getOperand(1).getValueType();
Nadav Rotemaec58612011-09-13 19:17:42 +0000270 DebugLoc DL = Op.getDebugLoc();
271
272 SDValue Mask = Op.getOperand(0);
273 SDValue Op1 = Op.getOperand(1);
274 SDValue Op2 = Op.getOperand(2);
275
276 // If we can't even use the basic vector operations of
277 // AND,OR,XOR, we will have to scalarize the op.
278 if (!TLI.isOperationLegalOrCustom(ISD::AND, VT) ||
279 !TLI.isOperationLegalOrCustom(ISD::XOR, VT) ||
Nadav Rotemb6266fb2011-09-18 10:29:29 +0000280 !TLI.isOperationLegalOrCustom(ISD::OR, VT))
281 return DAG.UnrollVectorOp(Op.getNode());
Nadav Rotemaec58612011-09-13 19:17:42 +0000282
283 assert(VT.getSizeInBits() == OVT.getSizeInBits() && "Invalid mask size");
284 // Bitcast the operands to be the same type as the mask.
285 // This is needed when we select between FP types because
286 // the mask is a vector of integers.
287 Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
288 Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
289
290 SDValue AllOnes = DAG.getConstant(
291 APInt::getAllOnesValue(VT.getScalarType().getSizeInBits()), VT);
292 SDValue NotMask = DAG.getNode(ISD::XOR, DL, VT, Mask, AllOnes);
293
294 Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
295 Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
296 return DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
297}
298
Nadav Rotem06cc3242011-03-19 13:09:10 +0000299SDValue VectorLegalizer::ExpandUINT_TO_FLOAT(SDValue Op) {
Nadav Rotem06cc3242011-03-19 13:09:10 +0000300 EVT VT = Op.getOperand(0).getValueType();
301 DebugLoc DL = Op.getDebugLoc();
302
303 // Make sure that the SINT_TO_FP and SRL instructions are available.
304 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, VT) ||
305 !TLI.isOperationLegalOrCustom(ISD::SRL, VT))
306 return DAG.UnrollVectorOp(Op.getNode());
307
308 EVT SVT = VT.getScalarType();
309 assert((SVT.getSizeInBits() == 64 || SVT.getSizeInBits() == 32) &&
310 "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
311
312 unsigned BW = SVT.getSizeInBits();
313 SDValue HalfWord = DAG.getConstant(BW/2, VT);
314
315 // Constants to clear the upper part of the word.
316 // Notice that we can also use SHL+SHR, but using a constant is slightly
317 // faster on x86.
318 uint64_t HWMask = (SVT.getSizeInBits()==64)?0x00000000FFFFFFFF:0x0000FFFF;
319 SDValue HalfWordMask = DAG.getConstant(HWMask, VT);
320
321 // Two to the power of half-word-size.
322 SDValue TWOHW = DAG.getConstantFP((1<<(BW/2)), Op.getValueType());
323
324 // Clear upper part of LO, lower HI
325 SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Op.getOperand(0), HalfWord);
326 SDValue LO = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), HalfWordMask);
327
328 // Convert hi and lo to floats
329 // Convert the hi part back to the upper values
330 SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), HI);
331 fHI = DAG.getNode(ISD::FMUL, DL, Op.getValueType(), fHI, TWOHW);
332 SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Op.getValueType(), LO);
333
334 // Add the two halves
335 return DAG.getNode(ISD::FADD, DL, Op.getValueType(), fHI, fLO);
336}
337
338
Eli Friedman5c22c802009-05-23 12:35:30 +0000339SDValue VectorLegalizer::ExpandFNEG(SDValue Op) {
340 if (TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType())) {
341 SDValue Zero = DAG.getConstantFP(-0.0, Op.getValueType());
342 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
343 Zero, Op.getOperand(0));
344 }
Mon P Wangcd6e7252009-11-30 02:42:02 +0000345 return DAG.UnrollVectorOp(Op.getNode());
Eli Friedman5c22c802009-05-23 12:35:30 +0000346}
347
348SDValue VectorLegalizer::UnrollVSETCC(SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +0000349 EVT VT = Op.getValueType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000350 unsigned NumElems = VT.getVectorNumElements();
Owen Andersone50ed302009-08-10 22:56:29 +0000351 EVT EltVT = VT.getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000352 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1), CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +0000353 EVT TmpEltVT = LHS.getValueType().getVectorElementType();
Eli Friedman5c22c802009-05-23 12:35:30 +0000354 DebugLoc dl = Op.getDebugLoc();
355 SmallVector<SDValue, 8> Ops(NumElems);
356 for (unsigned i = 0; i < NumElems; ++i) {
357 SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
358 DAG.getIntPtrConstant(i));
359 SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
360 DAG.getIntPtrConstant(i));
361 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
362 LHSElem, RHSElem, CC);
363 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
364 DAG.getConstant(APInt::getAllOnesValue
365 (EltVT.getSizeInBits()), EltVT),
366 DAG.getConstant(0, EltVT));
367 }
368 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
369}
370
Eli Friedman5c22c802009-05-23 12:35:30 +0000371}
372
373bool SelectionDAG::LegalizeVectors() {
374 return VectorLegalizer(*this).Run();
375}