blob: d688465234b44b6c25d1e647efd9708d5fbca3d8 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga448bc42007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Target/TargetLowering.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetOptions.h"
23#include "llvm/CallingConv.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000026#include "llvm/Support/Alignment.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Compiler.h"
Duncan Sandsa3691432007-10-28 12:59:45 +000029#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/SmallPtrSet.h"
33#include <map>
34using namespace llvm;
35
36#ifndef NDEBUG
37static cl::opt<bool>
38ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
39 cl::desc("Pop up a window to show dags before legalize"));
40#else
41static const bool ViewLegalizeDAGs = 0;
42#endif
43
44//===----------------------------------------------------------------------===//
45/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
46/// hacks on it until the target machine can handle it. This involves
47/// eliminating value sizes the machine cannot handle (promoting small sizes to
48/// large sizes or splitting up large values into small values) as well as
49/// eliminating operations the machine cannot handle.
50///
51/// This code also does a small amount of optimization and recognition of idioms
52/// as part of its processing. For example, if a target does not support a
53/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
54/// will attempt merge setcc and brc instructions into brcc's.
55///
56namespace {
57class VISIBILITY_HIDDEN SelectionDAGLegalize {
58 TargetLowering &TLI;
59 SelectionDAG &DAG;
60
61 // Libcall insertion helpers.
62
63 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
64 /// legalized. We use this to ensure that calls are properly serialized
65 /// against each other, including inserted libcalls.
66 SDOperand LastCALLSEQ_END;
67
68 /// IsLegalizingCall - This member is used *only* for purposes of providing
69 /// helpful assertions that a libcall isn't created while another call is
70 /// being legalized (which could lead to non-serialized call sequences).
71 bool IsLegalizingCall;
72
73 enum LegalizeAction {
74 Legal, // The target natively supports this operation.
75 Promote, // This operation should be executed in a larger type.
76 Expand // Try to expand this to other ops, otherwise use a libcall.
77 };
78
79 /// ValueTypeActions - This is a bitvector that contains two bits for each
80 /// value type, where the two bits correspond to the LegalizeAction enum.
81 /// This can be queried with "getTypeAction(VT)".
82 TargetLowering::ValueTypeActionImpl ValueTypeActions;
83
84 /// LegalizedNodes - For nodes that are of legal width, and that have more
85 /// than one use, this map indicates what regularized operand to use. This
86 /// allows us to avoid legalizing the same thing more than once.
87 DenseMap<SDOperand, SDOperand> LegalizedNodes;
88
89 /// PromotedNodes - For nodes that are below legal width, and that have more
90 /// than one use, this map indicates what promoted value to use. This allows
91 /// us to avoid promoting the same thing more than once.
92 DenseMap<SDOperand, SDOperand> PromotedNodes;
93
94 /// ExpandedNodes - For nodes that need to be expanded this map indicates
95 /// which which operands are the expanded version of the input. This allows
96 /// us to avoid expanding the same node more than once.
97 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
98
99 /// SplitNodes - For vector nodes that need to be split, this map indicates
100 /// which which operands are the split version of the input. This allows us
101 /// to avoid splitting the same node more than once.
102 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
103
104 /// ScalarizedNodes - For nodes that need to be converted from vector types to
105 /// scalar types, this contains the mapping of ones we have already
106 /// processed to the result.
107 std::map<SDOperand, SDOperand> ScalarizedNodes;
108
109 void AddLegalizedOperand(SDOperand From, SDOperand To) {
110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
114 }
115 void AddPromotedOperand(SDOperand From, SDOperand To) {
116 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
117 assert(isNew && "Got into the map somehow?");
118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
120 }
121
122public:
123
124 SelectionDAGLegalize(SelectionDAG &DAG);
125
126 /// getTypeAction - Return how we should legalize values of this type, either
127 /// it is already legal or we need to expand it into multiple registers of
128 /// smaller integer type, or we need to promote it to a larger type.
129 LegalizeAction getTypeAction(MVT::ValueType VT) const {
130 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
131 }
132
133 /// isTypeLegal - Return true if this type is legal on this target.
134 ///
135 bool isTypeLegal(MVT::ValueType VT) const {
136 return getTypeAction(VT) == Legal;
137 }
138
139 void LegalizeDAG();
140
141private:
142 /// HandleOp - Legalize, Promote, or Expand the specified operand as
143 /// appropriate for its type.
144 void HandleOp(SDOperand Op);
145
146 /// LegalizeOp - We know that the specified value has a legal type.
147 /// Recursively ensure that the operands have legal types, then return the
148 /// result.
149 SDOperand LegalizeOp(SDOperand O);
150
Dan Gohman6d05cac2007-10-11 23:57:53 +0000151 /// UnrollVectorOp - We know that the given vector has a legal type, however
152 /// the operation it performs is not legal and is an operation that we have
153 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
154 /// operating on each element individually.
155 SDOperand UnrollVectorOp(SDOperand O);
156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
162 SDOperand PromoteOp(SDOperand O);
163
164 /// ExpandOp - Expand the specified SDOperand into its two component pieces
165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
170 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
171
172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
174 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
179 SDOperand ScalarizeVectorOp(SDOperand O);
180
181 /// isShuffleLegal - Return true if a vector shuffle is legal with the
182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
190 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
191
192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
194
195 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
198 SDOperand &Hi);
199 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
200 SDOperand Source);
201
202 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
203 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
204 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
205 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
206 SDOperand LegalOp,
207 MVT::ValueType DestVT);
208 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
209 bool isSigned);
210 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
212
213 SDOperand ExpandBSWAP(SDOperand Op);
214 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
215 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
216 SDOperand &Lo, SDOperand &Hi);
217 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
219
220 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
221 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
222
223 SDOperand getIntPtrConstant(uint64_t Val) {
224 return DAG.getConstant(Val, TLI.getPointerTy());
225 }
226};
227}
228
229/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
230/// specified mask and type. Targets can specify exactly which masks they
231/// support and the code generator is tasked with not creating illegal masks.
232///
233/// Note that this will also return true for shuffles that are promoted to a
234/// different type.
235SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
236 SDOperand Mask) const {
237 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
238 default: return 0;
239 case TargetLowering::Legal:
240 case TargetLowering::Custom:
241 break;
242 case TargetLowering::Promote: {
243 // If this is promoted to a different type, convert the shuffle mask and
244 // ask if it is legal in the promoted type!
245 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
246
247 // If we changed # elements, change the shuffle mask.
248 unsigned NumEltsGrowth =
249 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
250 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
251 if (NumEltsGrowth > 1) {
252 // Renumber the elements.
253 SmallVector<SDOperand, 8> Ops;
254 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
255 SDOperand InOp = Mask.getOperand(i);
256 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
257 if (InOp.getOpcode() == ISD::UNDEF)
258 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
259 else {
260 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
261 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
262 }
263 }
264 }
265 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
266 }
267 VT = NVT;
268 break;
269 }
270 }
271 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
272}
273
274SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
275 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
276 ValueTypeActions(TLI.getValueTypeActions()) {
277 assert(MVT::LAST_VALUETYPE <= 32 &&
278 "Too many value types for ValueTypeActions to hold!");
279}
280
281/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
282/// contains all of a nodes operands before it contains the node.
283static void ComputeTopDownOrdering(SelectionDAG &DAG,
284 SmallVector<SDNode*, 64> &Order) {
285
286 DenseMap<SDNode*, unsigned> Visited;
287 std::vector<SDNode*> Worklist;
288 Worklist.reserve(128);
289
290 // Compute ordering from all of the leaves in the graphs, those (like the
291 // entry node) that have no operands.
292 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
293 E = DAG.allnodes_end(); I != E; ++I) {
294 if (I->getNumOperands() == 0) {
295 Visited[I] = 0 - 1U;
296 Worklist.push_back(I);
297 }
298 }
299
300 while (!Worklist.empty()) {
301 SDNode *N = Worklist.back();
302 Worklist.pop_back();
303
304 if (++Visited[N] != N->getNumOperands())
305 continue; // Haven't visited all operands yet
306
307 Order.push_back(N);
308
309 // Now that we have N in, add anything that uses it if all of their operands
310 // are now done.
311 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
312 UI != E; ++UI)
313 Worklist.push_back(*UI);
314 }
315
316 assert(Order.size() == Visited.size() &&
317 Order.size() ==
318 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
319 "Error: DAG is cyclic!");
320}
321
322
323void SelectionDAGLegalize::LegalizeDAG() {
324 LastCALLSEQ_END = DAG.getEntryNode();
325 IsLegalizingCall = false;
326
327 // The legalize process is inherently a bottom-up recursive process (users
328 // legalize their uses before themselves). Given infinite stack space, we
329 // could just start legalizing on the root and traverse the whole graph. In
330 // practice however, this causes us to run out of stack space on large basic
331 // blocks. To avoid this problem, compute an ordering of the nodes where each
332 // node is only legalized after all of its operands are legalized.
333 SmallVector<SDNode*, 64> Order;
334 ComputeTopDownOrdering(DAG, Order);
335
336 for (unsigned i = 0, e = Order.size(); i != e; ++i)
337 HandleOp(SDOperand(Order[i], 0));
338
339 // Finally, it's possible the root changed. Get the new root.
340 SDOperand OldRoot = DAG.getRoot();
341 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
342 DAG.setRoot(LegalizedNodes[OldRoot]);
343
344 ExpandedNodes.clear();
345 LegalizedNodes.clear();
346 PromotedNodes.clear();
347 SplitNodes.clear();
348 ScalarizedNodes.clear();
349
350 // Remove dead nodes now.
351 DAG.RemoveDeadNodes();
352}
353
354
355/// FindCallEndFromCallStart - Given a chained node that is part of a call
356/// sequence, find the CALLSEQ_END node that terminates the call sequence.
357static SDNode *FindCallEndFromCallStart(SDNode *Node) {
358 if (Node->getOpcode() == ISD::CALLSEQ_END)
359 return Node;
360 if (Node->use_empty())
361 return 0; // No CallSeqEnd
362
363 // The chain is usually at the end.
364 SDOperand TheChain(Node, Node->getNumValues()-1);
365 if (TheChain.getValueType() != MVT::Other) {
366 // Sometimes it's at the beginning.
367 TheChain = SDOperand(Node, 0);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Otherwise, hunt for it.
370 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
371 if (Node->getValueType(i) == MVT::Other) {
372 TheChain = SDOperand(Node, i);
373 break;
374 }
375
376 // Otherwise, we walked into a node without a chain.
377 if (TheChain.getValueType() != MVT::Other)
378 return 0;
379 }
380 }
381
382 for (SDNode::use_iterator UI = Node->use_begin(),
383 E = Node->use_end(); UI != E; ++UI) {
384
385 // Make sure to only follow users of our token chain.
386 SDNode *User = *UI;
387 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
388 if (User->getOperand(i) == TheChain)
389 if (SDNode *Result = FindCallEndFromCallStart(User))
390 return Result;
391 }
392 return 0;
393}
394
395/// FindCallStartFromCallEnd - Given a chained node that is part of a call
396/// sequence, find the CALLSEQ_START node that initiates the call sequence.
397static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
398 assert(Node && "Didn't find callseq_start for a call??");
399 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
400
401 assert(Node->getOperand(0).getValueType() == MVT::Other &&
402 "Node doesn't have a token chain argument!");
403 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
404}
405
406/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
407/// see if any uses can reach Dest. If no dest operands can get to dest,
408/// legalize them, legalize ourself, and return false, otherwise, return true.
409///
410/// Keep track of the nodes we fine that actually do lead to Dest in
411/// NodesLeadingTo. This avoids retraversing them exponential number of times.
412///
413bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
414 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
415 if (N == Dest) return true; // N certainly leads to Dest :)
416
417 // If we've already processed this node and it does lead to Dest, there is no
418 // need to reprocess it.
419 if (NodesLeadingTo.count(N)) return true;
420
421 // If the first result of this node has been already legalized, then it cannot
422 // reach N.
423 switch (getTypeAction(N->getValueType(0))) {
424 case Legal:
425 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 case Promote:
428 if (PromotedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 case Expand:
431 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 }
434
435 // Okay, this node has not already been legalized. Check and legalize all
436 // operands. If none lead to Dest, then we can legalize this node.
437 bool OperandsLeadToDest = false;
438 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
439 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
440 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
441
442 if (OperandsLeadToDest) {
443 NodesLeadingTo.insert(N);
444 return true;
445 }
446
447 // Okay, this node looks safe, legalize it and return false.
448 HandleOp(SDOperand(N, 0));
449 return false;
450}
451
452/// HandleOp - Legalize, Promote, or Expand the specified operand as
453/// appropriate for its type.
454void SelectionDAGLegalize::HandleOp(SDOperand Op) {
455 MVT::ValueType VT = Op.getValueType();
456 switch (getTypeAction(VT)) {
457 default: assert(0 && "Bad type action!");
458 case Legal: (void)LegalizeOp(Op); break;
459 case Promote: (void)PromoteOp(Op); break;
460 case Expand:
461 if (!MVT::isVector(VT)) {
462 // If this is an illegal scalar, expand it into its two component
463 // pieces.
464 SDOperand X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000465 if (Op.getOpcode() == ISD::TargetConstant)
466 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 ExpandOp(Op, X, Y);
468 } else if (MVT::getVectorNumElements(VT) == 1) {
469 // If this is an illegal single element vector, convert it to a
470 // scalar operation.
471 (void)ScalarizeVectorOp(Op);
472 } else {
473 // Otherwise, this is an illegal multiple element vector.
474 // Split it in half and legalize both parts.
475 SDOperand X, Y;
476 SplitVectorOp(Op, X, Y);
477 }
478 break;
479 }
480}
481
482/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
483/// a load from the constant pool.
484static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
485 SelectionDAG &DAG, TargetLowering &TLI) {
486 bool Extend = false;
487
488 // If a FP immediate is precise when represented as a float and if the
489 // target can do an extending load from float to double, we put it into
490 // the constant pool as a float, even if it's is statically typed as a
491 // double.
492 MVT::ValueType VT = CFP->getValueType(0);
493 bool isDouble = VT == MVT::f64;
Dale Johannesenb17a7a22007-09-16 16:51:49 +0000494 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen2fc20782007-09-14 22:26:36 +0000495 CFP->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000497 if (VT!=MVT::f64 && VT!=MVT::f32)
498 assert(0 && "Invalid type expansion");
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000499 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
500 isDouble ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 }
502
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000503 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen2fc20782007-09-14 22:26:36 +0000505 // Do not try to be clever about long doubles (so far)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
507 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
508 VT = MVT::f32;
509 Extend = true;
510 }
511
512 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
513 if (Extend) {
514 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
515 CPIdx, NULL, 0, MVT::f32);
516 } else {
517 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
518 }
519}
520
521
522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
527 MVT::ValueType VT = Node->getValueType(0);
528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
532
533 // First get the sign bit of second operand.
534 SDOperand Mask1 = (SrcVT == MVT::f64)
535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
572 if (MVT::isFloatingPoint(ST->getStoredVT())) {
573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
587 assert(MVT::isInteger(ST->getStoredVT()) &&
588 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000589 // Get the half-size VT
590 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000606 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000607 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
608 ST->getSrcValue(), SVOffset + IncrementSize,
609 NewStoredVT, ST->isVolatile(), Alignment);
610
611 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
612}
613
614/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
615static
616SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
617 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000618 int SVOffset = LD->getSrcValueOffset();
619 SDOperand Chain = LD->getChain();
620 SDOperand Ptr = LD->getBasePtr();
621 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen08275382007-09-08 19:29:23 +0000622 MVT::ValueType LoadedVT = LD->getLoadedVT();
623 if (MVT::isFloatingPoint(VT)) {
624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
627 if (LoadedVT==MVT::f64)
628 intVT = MVT::i64;
629 else if (LoadedVT==MVT::f32)
630 intVT = MVT::i32;
631 else
632 assert(0 && "Unaligned load of unsupported floating point type");
633
634 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
635 SVOffset, LD->isVolatile(),
636 LD->getAlignment());
637 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
638 if (LoadedVT != VT)
639 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
640
641 SDOperand Ops[] = { Result, Chain };
642 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
643 Ops, 2);
644 }
645 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
646 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000647 int NumBits = MVT::getSizeInBits(NewLoadedVT);
648 int Alignment = LD->getAlignment();
649 int IncrementSize = NumBits / 8;
650 ISD::LoadExtType HiExtType = LD->getExtensionType();
651
652 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
653 if (HiExtType == ISD::NON_EXTLOAD)
654 HiExtType = ISD::ZEXTLOAD;
655
656 // Load the value in two parts
657 SDOperand Lo, Hi;
658 if (TLI.isLittleEndian()) {
659 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
660 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
661 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
662 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
663 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
664 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000665 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000666 } else {
667 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
668 NewLoadedVT,LD->isVolatile(), Alignment);
669 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
670 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
671 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
672 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000673 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000674 }
675
676 // aggregate the two parts
677 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
678 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
679 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
680
681 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
682 Hi.getValue(1));
683
684 SDOperand Ops[] = { Result, TF };
685 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
686}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687
Dan Gohman6d05cac2007-10-11 23:57:53 +0000688/// UnrollVectorOp - We know that the given vector has a legal type, however
689/// the operation it performs is not legal and is an operation that we have
690/// no way of lowering. "Unroll" the vector, splitting out the scalars and
691/// operating on each element individually.
692SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
693 MVT::ValueType VT = Op.getValueType();
694 assert(isTypeLegal(VT) &&
695 "Caller should expand or promote operands that are not legal!");
696 assert(Op.Val->getNumValues() == 1 &&
697 "Can't unroll a vector with multiple results!");
698 unsigned NE = MVT::getVectorNumElements(VT);
699 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
700
701 SmallVector<SDOperand, 8> Scalars;
702 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
703 for (unsigned i = 0; i != NE; ++i) {
704 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
705 SDOperand Operand = Op.getOperand(j);
706 MVT::ValueType OperandVT = Operand.getValueType();
707 if (MVT::isVector(OperandVT)) {
708 // A vector operand; extract a single element.
709 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
710 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
711 OperandEltVT,
712 Operand,
713 DAG.getConstant(i, MVT::i32));
714 } else {
715 // A scalar operand; just use it as is.
716 Operands[j] = Operand;
717 }
718 }
719 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
720 &Operands[0], Operands.size()));
721 }
722
723 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
724}
725
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726/// LegalizeOp - We know that the specified value has a legal type, and
727/// that its operands are legal. Now ensure that the operation itself
728/// is legal, recursively ensuring that the operands' operations remain
729/// legal.
730SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000731 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
732 return Op;
733
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 assert(isTypeLegal(Op.getValueType()) &&
735 "Caller should expand or promote operands that are not legal!");
736 SDNode *Node = Op.Val;
737
738 // If this operation defines any values that cannot be represented in a
739 // register on this target, make sure to expand or promote them.
740 if (Node->getNumValues() > 1) {
741 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
742 if (getTypeAction(Node->getValueType(i)) != Legal) {
743 HandleOp(Op.getValue(i));
744 assert(LegalizedNodes.count(Op) &&
745 "Handling didn't add legal operands!");
746 return LegalizedNodes[Op];
747 }
748 }
749
750 // Note that LegalizeOp may be reentered even from single-use nodes, which
751 // means that we always must cache transformed nodes.
752 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
753 if (I != LegalizedNodes.end()) return I->second;
754
755 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
756 SDOperand Result = Op;
757 bool isCustom = false;
758
759 switch (Node->getOpcode()) {
760 case ISD::FrameIndex:
761 case ISD::EntryToken:
762 case ISD::Register:
763 case ISD::BasicBlock:
764 case ISD::TargetFrameIndex:
765 case ISD::TargetJumpTable:
766 case ISD::TargetConstant:
767 case ISD::TargetConstantFP:
768 case ISD::TargetConstantPool:
769 case ISD::TargetGlobalAddress:
770 case ISD::TargetGlobalTLSAddress:
771 case ISD::TargetExternalSymbol:
772 case ISD::VALUETYPE:
773 case ISD::SRCVALUE:
774 case ISD::STRING:
775 case ISD::CONDCODE:
776 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000777 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 "This must be legal!");
779 break;
780 default:
781 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
782 // If this is a target node, legalize it by legalizing the operands then
783 // passing it through.
784 SmallVector<SDOperand, 8> Ops;
785 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
786 Ops.push_back(LegalizeOp(Node->getOperand(i)));
787
788 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
789
790 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
791 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
792 return Result.getValue(Op.ResNo);
793 }
794 // Otherwise this is an unhandled builtin node. splat.
795#ifndef NDEBUG
796 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
797#endif
798 assert(0 && "Do not know how to legalize this operator!");
799 abort();
800 case ISD::GLOBAL_OFFSET_TABLE:
801 case ISD::GlobalAddress:
802 case ISD::GlobalTLSAddress:
803 case ISD::ExternalSymbol:
804 case ISD::ConstantPool:
805 case ISD::JumpTable: // Nothing to do.
806 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
807 default: assert(0 && "This action is not supported yet!");
808 case TargetLowering::Custom:
809 Tmp1 = TLI.LowerOperation(Op, DAG);
810 if (Tmp1.Val) Result = Tmp1;
811 // FALLTHROUGH if the target doesn't want to lower this op after all.
812 case TargetLowering::Legal:
813 break;
814 }
815 break;
816 case ISD::FRAMEADDR:
817 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 // The only option for these nodes is to custom lower them. If the target
819 // does not custom lower them, then return zero.
820 Tmp1 = TLI.LowerOperation(Op, DAG);
821 if (Tmp1.Val)
822 Result = Tmp1;
823 else
824 Result = DAG.getConstant(0, TLI.getPointerTy());
825 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000826 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000827 MVT::ValueType VT = Node->getValueType(0);
828 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
829 default: assert(0 && "This action is not supported yet!");
830 case TargetLowering::Custom:
831 Result = TLI.LowerOperation(Op, DAG);
832 if (Result.Val) break;
833 // Fall Thru
834 case TargetLowering::Legal:
835 Result = DAG.getConstant(0, VT);
836 break;
837 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000838 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000839 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 case ISD::EXCEPTIONADDR: {
841 Tmp1 = LegalizeOp(Node->getOperand(0));
842 MVT::ValueType VT = Node->getValueType(0);
843 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
844 default: assert(0 && "This action is not supported yet!");
845 case TargetLowering::Expand: {
846 unsigned Reg = TLI.getExceptionAddressRegister();
847 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
848 }
849 break;
850 case TargetLowering::Custom:
851 Result = TLI.LowerOperation(Op, DAG);
852 if (Result.Val) break;
853 // Fall Thru
854 case TargetLowering::Legal: {
855 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
856 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
857 Ops, 2).getValue(Op.ResNo);
858 break;
859 }
860 }
861 }
862 break;
863 case ISD::EHSELECTION: {
864 Tmp1 = LegalizeOp(Node->getOperand(0));
865 Tmp2 = LegalizeOp(Node->getOperand(1));
866 MVT::ValueType VT = Node->getValueType(0);
867 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
868 default: assert(0 && "This action is not supported yet!");
869 case TargetLowering::Expand: {
870 unsigned Reg = TLI.getExceptionSelectorRegister();
871 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
872 }
873 break;
874 case TargetLowering::Custom:
875 Result = TLI.LowerOperation(Op, DAG);
876 if (Result.Val) break;
877 // Fall Thru
878 case TargetLowering::Legal: {
879 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
880 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
881 Ops, 2).getValue(Op.ResNo);
882 break;
883 }
884 }
885 }
886 break;
887 case ISD::EH_RETURN: {
888 MVT::ValueType VT = Node->getValueType(0);
889 // The only "good" option for this node is to custom lower it.
890 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
891 default: assert(0 && "This action is not supported at all!");
892 case TargetLowering::Custom:
893 Result = TLI.LowerOperation(Op, DAG);
894 if (Result.Val) break;
895 // Fall Thru
896 case TargetLowering::Legal:
897 // Target does not know, how to lower this, lower to noop
898 Result = LegalizeOp(Node->getOperand(0));
899 break;
900 }
901 }
902 break;
903 case ISD::AssertSext:
904 case ISD::AssertZext:
905 Tmp1 = LegalizeOp(Node->getOperand(0));
906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
907 break;
908 case ISD::MERGE_VALUES:
909 // Legalize eliminates MERGE_VALUES nodes.
910 Result = Node->getOperand(Op.ResNo);
911 break;
912 case ISD::CopyFromReg:
913 Tmp1 = LegalizeOp(Node->getOperand(0));
914 Result = Op.getValue(0);
915 if (Node->getNumValues() == 2) {
916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
917 } else {
918 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
919 if (Node->getNumOperands() == 3) {
920 Tmp2 = LegalizeOp(Node->getOperand(2));
921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
922 } else {
923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
924 }
925 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
926 }
927 // Since CopyFromReg produces two values, make sure to remember that we
928 // legalized both of them.
929 AddLegalizedOperand(Op.getValue(0), Result);
930 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
931 return Result.getValue(Op.ResNo);
932 case ISD::UNDEF: {
933 MVT::ValueType VT = Op.getValueType();
934 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
935 default: assert(0 && "This action is not supported yet!");
936 case TargetLowering::Expand:
937 if (MVT::isInteger(VT))
938 Result = DAG.getConstant(0, VT);
939 else if (MVT::isFloatingPoint(VT))
Dale Johannesen20b76352007-09-26 17:26:49 +0000940 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
941 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 else
943 assert(0 && "Unknown value type!");
944 break;
945 case TargetLowering::Legal:
946 break;
947 }
948 break;
949 }
950
951 case ISD::INTRINSIC_W_CHAIN:
952 case ISD::INTRINSIC_WO_CHAIN:
953 case ISD::INTRINSIC_VOID: {
954 SmallVector<SDOperand, 8> Ops;
955 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
956 Ops.push_back(LegalizeOp(Node->getOperand(i)));
957 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
958
959 // Allow the target to custom lower its intrinsics if it wants to.
960 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
961 TargetLowering::Custom) {
962 Tmp3 = TLI.LowerOperation(Result, DAG);
963 if (Tmp3.Val) Result = Tmp3;
964 }
965
966 if (Result.Val->getNumValues() == 1) break;
967
968 // Must have return value and chain result.
969 assert(Result.Val->getNumValues() == 2 &&
970 "Cannot return more than two values!");
971
972 // Since loads produce two values, make sure to remember that we
973 // legalized both of them.
974 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
975 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
976 return Result.getValue(Op.ResNo);
977 }
978
979 case ISD::LOCATION:
980 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
982
983 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
984 case TargetLowering::Promote:
985 default: assert(0 && "This action is not supported yet!");
986 case TargetLowering::Expand: {
987 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
988 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
989 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
990
991 if (MMI && (useDEBUG_LOC || useLABEL)) {
992 const std::string &FName =
993 cast<StringSDNode>(Node->getOperand(3))->getValue();
994 const std::string &DirName =
995 cast<StringSDNode>(Node->getOperand(4))->getValue();
996 unsigned SrcFile = MMI->RecordSource(DirName, FName);
997
998 SmallVector<SDOperand, 8> Ops;
999 Ops.push_back(Tmp1); // chain
1000 SDOperand LineOp = Node->getOperand(1);
1001 SDOperand ColOp = Node->getOperand(2);
1002
1003 if (useDEBUG_LOC) {
1004 Ops.push_back(LineOp); // line #
1005 Ops.push_back(ColOp); // col #
1006 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
1007 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
1008 } else {
1009 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1010 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
1011 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
1012 Ops.push_back(DAG.getConstant(ID, MVT::i32));
1013 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
1014 }
1015 } else {
1016 Result = Tmp1; // chain
1017 }
1018 break;
1019 }
1020 case TargetLowering::Legal:
1021 if (Tmp1 != Node->getOperand(0) ||
1022 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
1023 SmallVector<SDOperand, 8> Ops;
1024 Ops.push_back(Tmp1);
1025 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1026 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1027 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1028 } else {
1029 // Otherwise promote them.
1030 Ops.push_back(PromoteOp(Node->getOperand(1)));
1031 Ops.push_back(PromoteOp(Node->getOperand(2)));
1032 }
1033 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1034 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1035 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1036 }
1037 break;
1038 }
1039 break;
1040
1041 case ISD::DEBUG_LOC:
1042 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1043 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1044 default: assert(0 && "This action is not supported yet!");
1045 case TargetLowering::Legal:
1046 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1047 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1048 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1049 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1050 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1051 break;
1052 }
1053 break;
1054
1055 case ISD::LABEL:
1056 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1057 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1058 default: assert(0 && "This action is not supported yet!");
1059 case TargetLowering::Legal:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1061 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
1062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1063 break;
1064 case TargetLowering::Expand:
1065 Result = LegalizeOp(Node->getOperand(0));
1066 break;
1067 }
1068 break;
1069
Scott Michelf2e2b702007-08-08 23:23:31 +00001070 case ISD::Constant: {
1071 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1072 unsigned opAction =
1073 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1074
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 // We know we don't need to expand constants here, constants only have one
1076 // value and we check that it is fine above.
1077
Scott Michelf2e2b702007-08-08 23:23:31 +00001078 if (opAction == TargetLowering::Custom) {
1079 Tmp1 = TLI.LowerOperation(Result, DAG);
1080 if (Tmp1.Val)
1081 Result = Tmp1;
1082 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001084 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 case ISD::ConstantFP: {
1086 // Spill FP immediates to the constant pool if the target cannot directly
1087 // codegen them. Targets often have some immediate values that can be
1088 // efficiently generated into an FP register without a load. We explicitly
1089 // leave these constants as ConstantFP nodes for the target to deal with.
1090 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1091
1092 // Check to see if this FP immediate is already legal.
1093 bool isLegal = false;
1094 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1095 E = TLI.legal_fpimm_end(); I != E; ++I)
1096 if (CFP->isExactlyValue(*I)) {
1097 isLegal = true;
1098 break;
1099 }
1100
1101 // If this is a legal constant, turn it into a TargetConstantFP node.
1102 if (isLegal) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00001103 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1104 CFP->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 break;
1106 }
1107
1108 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Custom:
1111 Tmp3 = TLI.LowerOperation(Result, DAG);
1112 if (Tmp3.Val) {
1113 Result = Tmp3;
1114 break;
1115 }
1116 // FALLTHROUGH
1117 case TargetLowering::Expand:
1118 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1119 }
1120 break;
1121 }
1122 case ISD::TokenFactor:
1123 if (Node->getNumOperands() == 2) {
1124 Tmp1 = LegalizeOp(Node->getOperand(0));
1125 Tmp2 = LegalizeOp(Node->getOperand(1));
1126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1127 } else if (Node->getNumOperands() == 3) {
1128 Tmp1 = LegalizeOp(Node->getOperand(0));
1129 Tmp2 = LegalizeOp(Node->getOperand(1));
1130 Tmp3 = LegalizeOp(Node->getOperand(2));
1131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1132 } else {
1133 SmallVector<SDOperand, 8> Ops;
1134 // Legalize the operands.
1135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1136 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1137 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1138 }
1139 break;
1140
1141 case ISD::FORMAL_ARGUMENTS:
1142 case ISD::CALL:
1143 // The only option for this is to custom lower it.
1144 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1145 assert(Tmp3.Val && "Target didn't custom lower this node!");
1146 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1147 "Lowering call/formal_arguments produced unexpected # results!");
1148
1149 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1150 // remember that we legalized all of them, so it doesn't get relegalized.
1151 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1152 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1153 if (Op.ResNo == i)
1154 Tmp2 = Tmp1;
1155 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1156 }
1157 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001158 case ISD::EXTRACT_SUBREG: {
1159 Tmp1 = LegalizeOp(Node->getOperand(0));
1160 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1161 assert(idx && "Operand must be a constant");
1162 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1163 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1164 }
1165 break;
1166 case ISD::INSERT_SUBREG: {
1167 Tmp1 = LegalizeOp(Node->getOperand(0));
1168 Tmp2 = LegalizeOp(Node->getOperand(1));
1169 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1170 assert(idx && "Operand must be a constant");
1171 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1172 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1173 }
1174 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 case ISD::BUILD_VECTOR:
1176 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1177 default: assert(0 && "This action is not supported yet!");
1178 case TargetLowering::Custom:
1179 Tmp3 = TLI.LowerOperation(Result, DAG);
1180 if (Tmp3.Val) {
1181 Result = Tmp3;
1182 break;
1183 }
1184 // FALLTHROUGH
1185 case TargetLowering::Expand:
1186 Result = ExpandBUILD_VECTOR(Result.Val);
1187 break;
1188 }
1189 break;
1190 case ISD::INSERT_VECTOR_ELT:
1191 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1192 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1193 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1195
1196 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1197 Node->getValueType(0))) {
1198 default: assert(0 && "This action is not supported yet!");
1199 case TargetLowering::Legal:
1200 break;
1201 case TargetLowering::Custom:
1202 Tmp3 = TLI.LowerOperation(Result, DAG);
1203 if (Tmp3.Val) {
1204 Result = Tmp3;
1205 break;
1206 }
1207 // FALLTHROUGH
1208 case TargetLowering::Expand: {
1209 // If the insert index is a constant, codegen this as a scalar_to_vector,
1210 // then a shuffle that inserts it into the right position in the vector.
1211 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1212 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1213 Tmp1.getValueType(), Tmp2);
1214
1215 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1216 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1217 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1218
1219 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1220 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1221 // the RHS.
1222 SmallVector<SDOperand, 8> ShufOps;
1223 for (unsigned i = 0; i != NumElts; ++i) {
1224 if (i != InsertPos->getValue())
1225 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1226 else
1227 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1228 }
1229 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1230 &ShufOps[0], ShufOps.size());
1231
1232 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1233 Tmp1, ScVec, ShufMask);
1234 Result = LegalizeOp(Result);
1235 break;
1236 }
1237
1238 // If the target doesn't support this, we have to spill the input vector
1239 // to a temporary stack slot, update the element, then reload it. This is
1240 // badness. We could also load the value into a vector register (either
1241 // with a "move to register" or "extload into register" instruction, then
1242 // permute it into place, if the idx is a constant and if the idx is
1243 // supported by the target.
1244 MVT::ValueType VT = Tmp1.getValueType();
1245 MVT::ValueType EltVT = Tmp2.getValueType();
1246 MVT::ValueType IdxVT = Tmp3.getValueType();
1247 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001248 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 // Store the vector.
1250 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
1251
1252 // Truncate or zero extend offset to target pointer type.
1253 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1254 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1255 // Add the offset to the index.
1256 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1257 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1258 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1259 // Store the scalar value.
1260 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
1261 // Load the updated vector.
1262 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
1263 break;
1264 }
1265 }
1266 break;
1267 case ISD::SCALAR_TO_VECTOR:
1268 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1269 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1270 break;
1271 }
1272
1273 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1274 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1275 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1276 Node->getValueType(0))) {
1277 default: assert(0 && "This action is not supported yet!");
1278 case TargetLowering::Legal:
1279 break;
1280 case TargetLowering::Custom:
1281 Tmp3 = TLI.LowerOperation(Result, DAG);
1282 if (Tmp3.Val) {
1283 Result = Tmp3;
1284 break;
1285 }
1286 // FALLTHROUGH
1287 case TargetLowering::Expand:
1288 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1289 break;
1290 }
1291 break;
1292 case ISD::VECTOR_SHUFFLE:
1293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1294 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1295 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1296
1297 // Allow targets to custom lower the SHUFFLEs they support.
1298 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1299 default: assert(0 && "Unknown operation action!");
1300 case TargetLowering::Legal:
1301 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1302 "vector shuffle should not be created if not legal!");
1303 break;
1304 case TargetLowering::Custom:
1305 Tmp3 = TLI.LowerOperation(Result, DAG);
1306 if (Tmp3.Val) {
1307 Result = Tmp3;
1308 break;
1309 }
1310 // FALLTHROUGH
1311 case TargetLowering::Expand: {
1312 MVT::ValueType VT = Node->getValueType(0);
1313 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1314 MVT::ValueType PtrVT = TLI.getPointerTy();
1315 SDOperand Mask = Node->getOperand(2);
1316 unsigned NumElems = Mask.getNumOperands();
1317 SmallVector<SDOperand,8> Ops;
1318 for (unsigned i = 0; i != NumElems; ++i) {
1319 SDOperand Arg = Mask.getOperand(i);
1320 if (Arg.getOpcode() == ISD::UNDEF) {
1321 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1322 } else {
1323 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1324 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1325 if (Idx < NumElems)
1326 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1327 DAG.getConstant(Idx, PtrVT)));
1328 else
1329 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1330 DAG.getConstant(Idx - NumElems, PtrVT)));
1331 }
1332 }
1333 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1334 break;
1335 }
1336 case TargetLowering::Promote: {
1337 // Change base type to a different vector type.
1338 MVT::ValueType OVT = Node->getValueType(0);
1339 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1340
1341 // Cast the two input vectors.
1342 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1343 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1344
1345 // Convert the shuffle mask to the right # elements.
1346 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1347 assert(Tmp3.Val && "Shuffle not legal?");
1348 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1349 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1350 break;
1351 }
1352 }
1353 break;
1354
1355 case ISD::EXTRACT_VECTOR_ELT:
1356 Tmp1 = Node->getOperand(0);
1357 Tmp2 = LegalizeOp(Node->getOperand(1));
1358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1359 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1360 break;
1361
1362 case ISD::EXTRACT_SUBVECTOR:
1363 Tmp1 = Node->getOperand(0);
1364 Tmp2 = LegalizeOp(Node->getOperand(1));
1365 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1366 Result = ExpandEXTRACT_SUBVECTOR(Result);
1367 break;
1368
1369 case ISD::CALLSEQ_START: {
1370 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1371
1372 // Recursively Legalize all of the inputs of the call end that do not lead
1373 // to this call start. This ensures that any libcalls that need be inserted
1374 // are inserted *before* the CALLSEQ_START.
1375 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1376 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1377 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1378 NodesLeadingTo);
1379 }
1380
1381 // Now that we legalized all of the inputs (which may have inserted
1382 // libcalls) create the new CALLSEQ_START node.
1383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1384
1385 // Merge in the last call, to ensure that this call start after the last
1386 // call ended.
1387 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1388 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1389 Tmp1 = LegalizeOp(Tmp1);
1390 }
1391
1392 // Do not try to legalize the target-specific arguments (#1+).
1393 if (Tmp1 != Node->getOperand(0)) {
1394 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1395 Ops[0] = Tmp1;
1396 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1397 }
1398
1399 // Remember that the CALLSEQ_START is legalized.
1400 AddLegalizedOperand(Op.getValue(0), Result);
1401 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1402 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1403
1404 // Now that the callseq_start and all of the non-call nodes above this call
1405 // sequence have been legalized, legalize the call itself. During this
1406 // process, no libcalls can/will be inserted, guaranteeing that no calls
1407 // can overlap.
1408 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1409 SDOperand InCallSEQ = LastCALLSEQ_END;
1410 // Note that we are selecting this call!
1411 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1412 IsLegalizingCall = true;
1413
1414 // Legalize the call, starting from the CALLSEQ_END.
1415 LegalizeOp(LastCALLSEQ_END);
1416 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1417 return Result;
1418 }
1419 case ISD::CALLSEQ_END:
1420 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1421 // will cause this node to be legalized as well as handling libcalls right.
1422 if (LastCALLSEQ_END.Val != Node) {
1423 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1424 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1425 assert(I != LegalizedNodes.end() &&
1426 "Legalizing the call start should have legalized this node!");
1427 return I->second;
1428 }
1429
1430 // Otherwise, the call start has been legalized and everything is going
1431 // according to plan. Just legalize ourselves normally here.
1432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1433 // Do not try to legalize the target-specific arguments (#1+), except for
1434 // an optional flag input.
1435 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1436 if (Tmp1 != Node->getOperand(0)) {
1437 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1438 Ops[0] = Tmp1;
1439 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1440 }
1441 } else {
1442 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1443 if (Tmp1 != Node->getOperand(0) ||
1444 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1445 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1446 Ops[0] = Tmp1;
1447 Ops.back() = Tmp2;
1448 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1449 }
1450 }
1451 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1452 // This finishes up call legalization.
1453 IsLegalizingCall = false;
1454
1455 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1456 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1457 if (Node->getNumValues() == 2)
1458 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1459 return Result.getValue(Op.ResNo);
1460 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001461 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1463 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1464 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1466
1467 Tmp1 = Result.getValue(0);
1468 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001469 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 default: assert(0 && "This action is not supported yet!");
1471 case TargetLowering::Expand: {
1472 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1473 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1474 " not tell us which reg is the stack pointer!");
1475 SDOperand Chain = Tmp1.getOperand(0);
1476 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001477 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1478 Chain = SP.getValue(1);
1479 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1480 unsigned StackAlign =
1481 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1482 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001483 SP = DAG.getNode(ISD::AND, VT, SP,
1484 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001485 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1486 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 Tmp1 = LegalizeOp(Tmp1);
1488 Tmp2 = LegalizeOp(Tmp2);
1489 break;
1490 }
1491 case TargetLowering::Custom:
1492 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1493 if (Tmp3.Val) {
1494 Tmp1 = LegalizeOp(Tmp3);
1495 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1496 }
1497 break;
1498 case TargetLowering::Legal:
1499 break;
1500 }
1501 // Since this op produce two values, make sure to remember that we
1502 // legalized both of them.
1503 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1504 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1505 return Op.ResNo ? Tmp2 : Tmp1;
1506 }
1507 case ISD::INLINEASM: {
1508 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1509 bool Changed = false;
1510 // Legalize all of the operands of the inline asm, in case they are nodes
1511 // that need to be expanded or something. Note we skip the asm string and
1512 // all of the TargetConstant flags.
1513 SDOperand Op = LegalizeOp(Ops[0]);
1514 Changed = Op != Ops[0];
1515 Ops[0] = Op;
1516
1517 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1518 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1519 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1520 for (++i; NumVals; ++i, --NumVals) {
1521 SDOperand Op = LegalizeOp(Ops[i]);
1522 if (Op != Ops[i]) {
1523 Changed = true;
1524 Ops[i] = Op;
1525 }
1526 }
1527 }
1528
1529 if (HasInFlag) {
1530 Op = LegalizeOp(Ops.back());
1531 Changed |= Op != Ops.back();
1532 Ops.back() = Op;
1533 }
1534
1535 if (Changed)
1536 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1537
1538 // INLINE asm returns a chain and flag, make sure to add both to the map.
1539 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1540 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1541 return Result.getValue(Op.ResNo);
1542 }
1543 case ISD::BR:
1544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1545 // Ensure that libcalls are emitted before a branch.
1546 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1547 Tmp1 = LegalizeOp(Tmp1);
1548 LastCALLSEQ_END = DAG.getEntryNode();
1549
1550 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1551 break;
1552 case ISD::BRIND:
1553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1554 // Ensure that libcalls are emitted before a branch.
1555 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1556 Tmp1 = LegalizeOp(Tmp1);
1557 LastCALLSEQ_END = DAG.getEntryNode();
1558
1559 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1560 default: assert(0 && "Indirect target must be legal type (pointer)!");
1561 case Legal:
1562 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1563 break;
1564 }
1565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1566 break;
1567 case ISD::BR_JT:
1568 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1569 // Ensure that libcalls are emitted before a branch.
1570 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1571 Tmp1 = LegalizeOp(Tmp1);
1572 LastCALLSEQ_END = DAG.getEntryNode();
1573
1574 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1576
1577 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1578 default: assert(0 && "This action is not supported yet!");
1579 case TargetLowering::Legal: break;
1580 case TargetLowering::Custom:
1581 Tmp1 = TLI.LowerOperation(Result, DAG);
1582 if (Tmp1.Val) Result = Tmp1;
1583 break;
1584 case TargetLowering::Expand: {
1585 SDOperand Chain = Result.getOperand(0);
1586 SDOperand Table = Result.getOperand(1);
1587 SDOperand Index = Result.getOperand(2);
1588
1589 MVT::ValueType PTy = TLI.getPointerTy();
1590 MachineFunction &MF = DAG.getMachineFunction();
1591 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1592 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1593 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1594
1595 SDOperand LD;
1596 switch (EntrySize) {
1597 default: assert(0 && "Size of jump table not supported yet."); break;
1598 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1599 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1600 }
1601
1602 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1603 // For PIC, the sequence is:
1604 // BRIND(load(Jumptable + index) + RelocBase)
1605 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1606 SDOperand Reloc;
1607 if (TLI.usesGlobalOffsetTable())
1608 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1609 else
1610 Reloc = Table;
1611 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
1612 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1613 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1614 } else {
1615 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1616 }
1617 }
1618 }
1619 break;
1620 case ISD::BRCOND:
1621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1622 // Ensure that libcalls are emitted before a return.
1623 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1624 Tmp1 = LegalizeOp(Tmp1);
1625 LastCALLSEQ_END = DAG.getEntryNode();
1626
1627 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1628 case Expand: assert(0 && "It's impossible to expand bools");
1629 case Legal:
1630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1631 break;
1632 case Promote:
1633 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1634
1635 // The top bits of the promoted condition are not necessarily zero, ensure
1636 // that the value is properly zero extended.
1637 if (!DAG.MaskedValueIsZero(Tmp2,
1638 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1639 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1640 break;
1641 }
1642
1643 // Basic block destination (Op#2) is always legal.
1644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1645
1646 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1647 default: assert(0 && "This action is not supported yet!");
1648 case TargetLowering::Legal: break;
1649 case TargetLowering::Custom:
1650 Tmp1 = TLI.LowerOperation(Result, DAG);
1651 if (Tmp1.Val) Result = Tmp1;
1652 break;
1653 case TargetLowering::Expand:
1654 // Expand brcond's setcc into its constituent parts and create a BR_CC
1655 // Node.
1656 if (Tmp2.getOpcode() == ISD::SETCC) {
1657 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1658 Tmp2.getOperand(0), Tmp2.getOperand(1),
1659 Node->getOperand(2));
1660 } else {
1661 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1662 DAG.getCondCode(ISD::SETNE), Tmp2,
1663 DAG.getConstant(0, Tmp2.getValueType()),
1664 Node->getOperand(2));
1665 }
1666 break;
1667 }
1668 break;
1669 case ISD::BR_CC:
1670 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1671 // Ensure that libcalls are emitted before a branch.
1672 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1673 Tmp1 = LegalizeOp(Tmp1);
1674 Tmp2 = Node->getOperand(2); // LHS
1675 Tmp3 = Node->getOperand(3); // RHS
1676 Tmp4 = Node->getOperand(1); // CC
1677
1678 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1679 LastCALLSEQ_END = DAG.getEntryNode();
1680
1681 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1682 // the LHS is a legal SETCC itself. In this case, we need to compare
1683 // the result against zero to select between true and false values.
1684 if (Tmp3.Val == 0) {
1685 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1686 Tmp4 = DAG.getCondCode(ISD::SETNE);
1687 }
1688
1689 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1690 Node->getOperand(4));
1691
1692 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1693 default: assert(0 && "Unexpected action for BR_CC!");
1694 case TargetLowering::Legal: break;
1695 case TargetLowering::Custom:
1696 Tmp4 = TLI.LowerOperation(Result, DAG);
1697 if (Tmp4.Val) Result = Tmp4;
1698 break;
1699 }
1700 break;
1701 case ISD::LOAD: {
1702 LoadSDNode *LD = cast<LoadSDNode>(Node);
1703 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1704 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1705
1706 ISD::LoadExtType ExtType = LD->getExtensionType();
1707 if (ExtType == ISD::NON_EXTLOAD) {
1708 MVT::ValueType VT = Node->getValueType(0);
1709 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1710 Tmp3 = Result.getValue(0);
1711 Tmp4 = Result.getValue(1);
1712
1713 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1714 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001715 case TargetLowering::Legal:
1716 // If this is an unaligned load and the target doesn't support it,
1717 // expand it.
1718 if (!TLI.allowsUnalignedMemoryAccesses()) {
1719 unsigned ABIAlignment = TLI.getTargetData()->
1720 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1721 if (LD->getAlignment() < ABIAlignment){
1722 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1723 TLI);
1724 Tmp3 = Result.getOperand(0);
1725 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001726 Tmp3 = LegalizeOp(Tmp3);
1727 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001728 }
1729 }
1730 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 case TargetLowering::Custom:
1732 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1733 if (Tmp1.Val) {
1734 Tmp3 = LegalizeOp(Tmp1);
1735 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1736 }
1737 break;
1738 case TargetLowering::Promote: {
1739 // Only promote a load of vector type to another.
1740 assert(MVT::isVector(VT) && "Cannot promote this load!");
1741 // Change base type to a different vector type.
1742 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1743
1744 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1745 LD->getSrcValueOffset(),
1746 LD->isVolatile(), LD->getAlignment());
1747 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1748 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1749 break;
1750 }
1751 }
1752 // Since loads produce two values, make sure to remember that we
1753 // legalized both of them.
1754 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1755 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1756 return Op.ResNo ? Tmp4 : Tmp3;
1757 } else {
1758 MVT::ValueType SrcVT = LD->getLoadedVT();
1759 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1760 default: assert(0 && "This action is not supported yet!");
1761 case TargetLowering::Promote:
1762 assert(SrcVT == MVT::i1 &&
1763 "Can only promote extending LOAD from i1 -> i8!");
1764 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1765 LD->getSrcValue(), LD->getSrcValueOffset(),
1766 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsd7307a92007-10-17 13:49:58 +00001767 Tmp1 = Result.getValue(0);
1768 Tmp2 = Result.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001769 break;
1770 case TargetLowering::Custom:
1771 isCustom = true;
1772 // FALLTHROUGH
1773 case TargetLowering::Legal:
1774 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1775 Tmp1 = Result.getValue(0);
1776 Tmp2 = Result.getValue(1);
1777
1778 if (isCustom) {
1779 Tmp3 = TLI.LowerOperation(Result, DAG);
1780 if (Tmp3.Val) {
1781 Tmp1 = LegalizeOp(Tmp3);
1782 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1783 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001784 } else {
1785 // If this is an unaligned load and the target doesn't support it,
1786 // expand it.
1787 if (!TLI.allowsUnalignedMemoryAccesses()) {
1788 unsigned ABIAlignment = TLI.getTargetData()->
1789 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1790 if (LD->getAlignment() < ABIAlignment){
1791 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1792 TLI);
1793 Tmp1 = Result.getOperand(0);
1794 Tmp2 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001795 Tmp1 = LegalizeOp(Tmp1);
1796 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001797 }
1798 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001799 }
1800 break;
1801 case TargetLowering::Expand:
1802 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1803 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1804 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1805 LD->getSrcValueOffset(),
1806 LD->isVolatile(), LD->getAlignment());
1807 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1808 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1809 Tmp2 = LegalizeOp(Load.getValue(1));
1810 break;
1811 }
1812 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1813 // Turn the unsupported load into an EXTLOAD followed by an explicit
1814 // zero/sign extend inreg.
1815 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1816 Tmp1, Tmp2, LD->getSrcValue(),
1817 LD->getSrcValueOffset(), SrcVT,
1818 LD->isVolatile(), LD->getAlignment());
1819 SDOperand ValRes;
1820 if (ExtType == ISD::SEXTLOAD)
1821 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1822 Result, DAG.getValueType(SrcVT));
1823 else
1824 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1825 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1826 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1827 break;
1828 }
1829 // Since loads produce two values, make sure to remember that we legalized
1830 // both of them.
1831 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1832 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1833 return Op.ResNo ? Tmp2 : Tmp1;
1834 }
1835 }
1836 case ISD::EXTRACT_ELEMENT: {
1837 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1838 switch (getTypeAction(OpTy)) {
1839 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1840 case Legal:
1841 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1842 // 1 -> Hi
1843 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1844 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1845 TLI.getShiftAmountTy()));
1846 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1847 } else {
1848 // 0 -> Lo
1849 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1850 Node->getOperand(0));
1851 }
1852 break;
1853 case Expand:
1854 // Get both the low and high parts.
1855 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1856 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1857 Result = Tmp2; // 1 -> Hi
1858 else
1859 Result = Tmp1; // 0 -> Lo
1860 break;
1861 }
1862 break;
1863 }
1864
1865 case ISD::CopyToReg:
1866 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1867
1868 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
1869 "Register type must be legal!");
1870 // Legalize the incoming value (must be a legal type).
1871 Tmp2 = LegalizeOp(Node->getOperand(2));
1872 if (Node->getNumValues() == 1) {
1873 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
1874 } else {
1875 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1876 if (Node->getNumOperands() == 4) {
1877 Tmp3 = LegalizeOp(Node->getOperand(3));
1878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1879 Tmp3);
1880 } else {
1881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1882 }
1883
1884 // Since this produces two values, make sure to remember that we legalized
1885 // both of them.
1886 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1887 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1888 return Result;
1889 }
1890 break;
1891
1892 case ISD::RET:
1893 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1894
1895 // Ensure that libcalls are emitted before a return.
1896 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1897 Tmp1 = LegalizeOp(Tmp1);
1898 LastCALLSEQ_END = DAG.getEntryNode();
1899
1900 switch (Node->getNumOperands()) {
1901 case 3: // ret val
1902 Tmp2 = Node->getOperand(1);
1903 Tmp3 = Node->getOperand(2); // Signness
1904 switch (getTypeAction(Tmp2.getValueType())) {
1905 case Legal:
1906 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
1907 break;
1908 case Expand:
1909 if (!MVT::isVector(Tmp2.getValueType())) {
1910 SDOperand Lo, Hi;
1911 ExpandOp(Tmp2, Lo, Hi);
1912
1913 // Big endian systems want the hi reg first.
1914 if (!TLI.isLittleEndian())
1915 std::swap(Lo, Hi);
1916
1917 if (Hi.Val)
1918 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1919 else
1920 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
1921 Result = LegalizeOp(Result);
1922 } else {
1923 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00001924 int InIx = Tmp2.ResNo;
1925 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1926 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927
1928 // Figure out if there is a simple type corresponding to this Vector
1929 // type. If so, convert to the vector type.
1930 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1931 if (TLI.isTypeLegal(TVT)) {
1932 // Turn this into a return of the vector type.
1933 Tmp2 = LegalizeOp(Tmp2);
1934 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1935 } else if (NumElems == 1) {
1936 // Turn this into a return of the scalar type.
1937 Tmp2 = ScalarizeVectorOp(Tmp2);
1938 Tmp2 = LegalizeOp(Tmp2);
1939 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1940
1941 // FIXME: Returns of gcc generic vectors smaller than a legal type
1942 // should be returned in integer registers!
1943
1944 // The scalarized value type may not be legal, e.g. it might require
1945 // promotion or expansion. Relegalize the return.
1946 Result = LegalizeOp(Result);
1947 } else {
1948 // FIXME: Returns of gcc generic vectors larger than a legal vector
1949 // type should be returned by reference!
1950 SDOperand Lo, Hi;
1951 SplitVectorOp(Tmp2, Lo, Hi);
1952 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1953 Result = LegalizeOp(Result);
1954 }
1955 }
1956 break;
1957 case Promote:
1958 Tmp2 = PromoteOp(Node->getOperand(1));
1959 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1960 Result = LegalizeOp(Result);
1961 break;
1962 }
1963 break;
1964 case 1: // ret void
1965 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1966 break;
1967 default: { // ret <values>
1968 SmallVector<SDOperand, 8> NewValues;
1969 NewValues.push_back(Tmp1);
1970 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
1971 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1972 case Legal:
1973 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
1974 NewValues.push_back(Node->getOperand(i+1));
1975 break;
1976 case Expand: {
1977 SDOperand Lo, Hi;
1978 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
1979 "FIXME: TODO: implement returning non-legal vector types!");
1980 ExpandOp(Node->getOperand(i), Lo, Hi);
1981 NewValues.push_back(Lo);
1982 NewValues.push_back(Node->getOperand(i+1));
1983 if (Hi.Val) {
1984 NewValues.push_back(Hi);
1985 NewValues.push_back(Node->getOperand(i+1));
1986 }
1987 break;
1988 }
1989 case Promote:
1990 assert(0 && "Can't promote multiple return value yet!");
1991 }
1992
1993 if (NewValues.size() == Node->getNumOperands())
1994 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
1995 else
1996 Result = DAG.getNode(ISD::RET, MVT::Other,
1997 &NewValues[0], NewValues.size());
1998 break;
1999 }
2000 }
2001
2002 if (Result.getOpcode() == ISD::RET) {
2003 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2004 default: assert(0 && "This action is not supported yet!");
2005 case TargetLowering::Legal: break;
2006 case TargetLowering::Custom:
2007 Tmp1 = TLI.LowerOperation(Result, DAG);
2008 if (Tmp1.Val) Result = Tmp1;
2009 break;
2010 }
2011 }
2012 break;
2013 case ISD::STORE: {
2014 StoreSDNode *ST = cast<StoreSDNode>(Node);
2015 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2016 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2017 int SVOffset = ST->getSrcValueOffset();
2018 unsigned Alignment = ST->getAlignment();
2019 bool isVolatile = ST->isVolatile();
2020
2021 if (!ST->isTruncatingStore()) {
2022 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2023 // FIXME: We shouldn't do this for TargetConstantFP's.
2024 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2025 // to phase ordering between legalized code and the dag combiner. This
2026 // probably means that we need to integrate dag combiner and legalizer
2027 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002028 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002029 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002030 if (CFP->getValueType(0) == MVT::f32 &&
2031 getTypeAction(MVT::i32) == Legal) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00002032 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2033 convertToAPInt().getZExtValue(),
Dale Johannesen1616e902007-09-11 18:32:33 +00002034 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002035 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2036 SVOffset, isVolatile, Alignment);
2037 break;
2038 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002039 // If this target supports 64-bit registers, do a single 64-bit store.
2040 if (getTypeAction(MVT::i64) == Legal) {
2041 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2042 getZExtValue(), MVT::i64);
2043 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2044 SVOffset, isVolatile, Alignment);
2045 break;
2046 } else if (getTypeAction(MVT::i32) == Legal) {
2047 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2048 // stores. If the target supports neither 32- nor 64-bits, this
2049 // xform is certainly not worth it.
2050 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2051 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2052 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2053 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2054
2055 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2056 SVOffset, isVolatile, Alignment);
2057 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2058 getIntPtrConstant(4));
2059 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002060 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002061
2062 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2063 break;
2064 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066 }
2067
2068 switch (getTypeAction(ST->getStoredVT())) {
2069 case Legal: {
2070 Tmp3 = LegalizeOp(ST->getValue());
2071 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2072 ST->getOffset());
2073
2074 MVT::ValueType VT = Tmp3.getValueType();
2075 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2076 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002077 case TargetLowering::Legal:
2078 // If this is an unaligned store and the target doesn't support it,
2079 // expand it.
2080 if (!TLI.allowsUnalignedMemoryAccesses()) {
2081 unsigned ABIAlignment = TLI.getTargetData()->
2082 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2083 if (ST->getAlignment() < ABIAlignment)
2084 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2085 TLI);
2086 }
2087 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 case TargetLowering::Custom:
2089 Tmp1 = TLI.LowerOperation(Result, DAG);
2090 if (Tmp1.Val) Result = Tmp1;
2091 break;
2092 case TargetLowering::Promote:
2093 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2094 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2095 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2096 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2097 ST->getSrcValue(), SVOffset, isVolatile,
2098 Alignment);
2099 break;
2100 }
2101 break;
2102 }
2103 case Promote:
2104 // Truncate the value and store the result.
2105 Tmp3 = PromoteOp(ST->getValue());
2106 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2107 SVOffset, ST->getStoredVT(),
2108 isVolatile, Alignment);
2109 break;
2110
2111 case Expand:
2112 unsigned IncrementSize = 0;
2113 SDOperand Lo, Hi;
2114
2115 // If this is a vector type, then we have to calculate the increment as
2116 // the product of the element size in bytes, and the number of elements
2117 // in the high half of the vector.
2118 if (MVT::isVector(ST->getValue().getValueType())) {
2119 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002120 int InIx = ST->getValue().ResNo;
2121 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2122 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002123
2124 // Figure out if there is a simple type corresponding to this Vector
2125 // type. If so, convert to the vector type.
2126 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2127 if (TLI.isTypeLegal(TVT)) {
2128 // Turn this into a normal store of the vector type.
2129 Tmp3 = LegalizeOp(Node->getOperand(1));
2130 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2131 SVOffset, isVolatile, Alignment);
2132 Result = LegalizeOp(Result);
2133 break;
2134 } else if (NumElems == 1) {
2135 // Turn this into a normal store of the scalar type.
2136 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
2137 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2138 SVOffset, isVolatile, Alignment);
2139 // The scalarized value type may not be legal, e.g. it might require
2140 // promotion or expansion. Relegalize the scalar store.
2141 Result = LegalizeOp(Result);
2142 break;
2143 } else {
2144 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2145 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2146 }
2147 } else {
2148 ExpandOp(Node->getOperand(1), Lo, Hi);
2149 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2150
2151 if (!TLI.isLittleEndian())
2152 std::swap(Lo, Hi);
2153 }
2154
2155 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2156 SVOffset, isVolatile, Alignment);
2157
2158 if (Hi.Val == NULL) {
2159 // Must be int <-> float one-to-one expansion.
2160 Result = Lo;
2161 break;
2162 }
2163
2164 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2165 getIntPtrConstant(IncrementSize));
2166 assert(isTypeLegal(Tmp2.getValueType()) &&
2167 "Pointers must be legal!");
2168 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002169 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2171 SVOffset, isVolatile, Alignment);
2172 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2173 break;
2174 }
2175 } else {
2176 // Truncating store
2177 assert(isTypeLegal(ST->getValue().getValueType()) &&
2178 "Cannot handle illegal TRUNCSTORE yet!");
2179 Tmp3 = LegalizeOp(ST->getValue());
2180
2181 // The only promote case we handle is TRUNCSTORE:i1 X into
2182 // -> TRUNCSTORE:i8 (and X, 1)
2183 if (ST->getStoredVT() == MVT::i1 &&
2184 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2185 // Promote the bool to a mask then store.
2186 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2187 DAG.getConstant(1, Tmp3.getValueType()));
2188 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2189 SVOffset, MVT::i8,
2190 isVolatile, Alignment);
2191 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2192 Tmp2 != ST->getBasePtr()) {
2193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2194 ST->getOffset());
2195 }
2196
2197 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2198 switch (TLI.getStoreXAction(StVT)) {
2199 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002200 case TargetLowering::Legal:
2201 // If this is an unaligned store and the target doesn't support it,
2202 // expand it.
2203 if (!TLI.allowsUnalignedMemoryAccesses()) {
2204 unsigned ABIAlignment = TLI.getTargetData()->
2205 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2206 if (ST->getAlignment() < ABIAlignment)
2207 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2208 TLI);
2209 }
2210 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002211 case TargetLowering::Custom:
2212 Tmp1 = TLI.LowerOperation(Result, DAG);
2213 if (Tmp1.Val) Result = Tmp1;
2214 break;
2215 }
2216 }
2217 break;
2218 }
2219 case ISD::PCMARKER:
2220 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2221 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2222 break;
2223 case ISD::STACKSAVE:
2224 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2225 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2226 Tmp1 = Result.getValue(0);
2227 Tmp2 = Result.getValue(1);
2228
2229 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2230 default: assert(0 && "This action is not supported yet!");
2231 case TargetLowering::Legal: break;
2232 case TargetLowering::Custom:
2233 Tmp3 = TLI.LowerOperation(Result, DAG);
2234 if (Tmp3.Val) {
2235 Tmp1 = LegalizeOp(Tmp3);
2236 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2237 }
2238 break;
2239 case TargetLowering::Expand:
2240 // Expand to CopyFromReg if the target set
2241 // StackPointerRegisterToSaveRestore.
2242 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2243 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2244 Node->getValueType(0));
2245 Tmp2 = Tmp1.getValue(1);
2246 } else {
2247 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2248 Tmp2 = Node->getOperand(0);
2249 }
2250 break;
2251 }
2252
2253 // Since stacksave produce two values, make sure to remember that we
2254 // legalized both of them.
2255 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2256 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2257 return Op.ResNo ? Tmp2 : Tmp1;
2258
2259 case ISD::STACKRESTORE:
2260 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2261 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2263
2264 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2265 default: assert(0 && "This action is not supported yet!");
2266 case TargetLowering::Legal: break;
2267 case TargetLowering::Custom:
2268 Tmp1 = TLI.LowerOperation(Result, DAG);
2269 if (Tmp1.Val) Result = Tmp1;
2270 break;
2271 case TargetLowering::Expand:
2272 // Expand to CopyToReg if the target set
2273 // StackPointerRegisterToSaveRestore.
2274 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2275 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2276 } else {
2277 Result = Tmp1;
2278 }
2279 break;
2280 }
2281 break;
2282
2283 case ISD::READCYCLECOUNTER:
2284 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2285 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2286 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2287 Node->getValueType(0))) {
2288 default: assert(0 && "This action is not supported yet!");
2289 case TargetLowering::Legal:
2290 Tmp1 = Result.getValue(0);
2291 Tmp2 = Result.getValue(1);
2292 break;
2293 case TargetLowering::Custom:
2294 Result = TLI.LowerOperation(Result, DAG);
2295 Tmp1 = LegalizeOp(Result.getValue(0));
2296 Tmp2 = LegalizeOp(Result.getValue(1));
2297 break;
2298 }
2299
2300 // Since rdcc produce two values, make sure to remember that we legalized
2301 // both of them.
2302 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2303 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2304 return Result;
2305
2306 case ISD::SELECT:
2307 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2308 case Expand: assert(0 && "It's impossible to expand bools");
2309 case Legal:
2310 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2311 break;
2312 case Promote:
2313 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2314 // Make sure the condition is either zero or one.
2315 if (!DAG.MaskedValueIsZero(Tmp1,
2316 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2317 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2318 break;
2319 }
2320 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2321 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2322
2323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2324
2325 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2326 default: assert(0 && "This action is not supported yet!");
2327 case TargetLowering::Legal: break;
2328 case TargetLowering::Custom: {
2329 Tmp1 = TLI.LowerOperation(Result, DAG);
2330 if (Tmp1.Val) Result = Tmp1;
2331 break;
2332 }
2333 case TargetLowering::Expand:
2334 if (Tmp1.getOpcode() == ISD::SETCC) {
2335 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2336 Tmp2, Tmp3,
2337 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2338 } else {
2339 Result = DAG.getSelectCC(Tmp1,
2340 DAG.getConstant(0, Tmp1.getValueType()),
2341 Tmp2, Tmp3, ISD::SETNE);
2342 }
2343 break;
2344 case TargetLowering::Promote: {
2345 MVT::ValueType NVT =
2346 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2347 unsigned ExtOp, TruncOp;
2348 if (MVT::isVector(Tmp2.getValueType())) {
2349 ExtOp = ISD::BIT_CONVERT;
2350 TruncOp = ISD::BIT_CONVERT;
2351 } else if (MVT::isInteger(Tmp2.getValueType())) {
2352 ExtOp = ISD::ANY_EXTEND;
2353 TruncOp = ISD::TRUNCATE;
2354 } else {
2355 ExtOp = ISD::FP_EXTEND;
2356 TruncOp = ISD::FP_ROUND;
2357 }
2358 // Promote each of the values to the new type.
2359 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2360 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2361 // Perform the larger operation, then round down.
2362 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2363 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2364 break;
2365 }
2366 }
2367 break;
2368 case ISD::SELECT_CC: {
2369 Tmp1 = Node->getOperand(0); // LHS
2370 Tmp2 = Node->getOperand(1); // RHS
2371 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2372 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2373 SDOperand CC = Node->getOperand(4);
2374
2375 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2376
2377 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2378 // the LHS is a legal SETCC itself. In this case, we need to compare
2379 // the result against zero to select between true and false values.
2380 if (Tmp2.Val == 0) {
2381 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2382 CC = DAG.getCondCode(ISD::SETNE);
2383 }
2384 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2385
2386 // Everything is legal, see if we should expand this op or something.
2387 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2388 default: assert(0 && "This action is not supported yet!");
2389 case TargetLowering::Legal: break;
2390 case TargetLowering::Custom:
2391 Tmp1 = TLI.LowerOperation(Result, DAG);
2392 if (Tmp1.Val) Result = Tmp1;
2393 break;
2394 }
2395 break;
2396 }
2397 case ISD::SETCC:
2398 Tmp1 = Node->getOperand(0);
2399 Tmp2 = Node->getOperand(1);
2400 Tmp3 = Node->getOperand(2);
2401 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2402
2403 // If we had to Expand the SetCC operands into a SELECT node, then it may
2404 // not always be possible to return a true LHS & RHS. In this case, just
2405 // return the value we legalized, returned in the LHS
2406 if (Tmp2.Val == 0) {
2407 Result = Tmp1;
2408 break;
2409 }
2410
2411 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2412 default: assert(0 && "Cannot handle this action for SETCC yet!");
2413 case TargetLowering::Custom:
2414 isCustom = true;
2415 // FALLTHROUGH.
2416 case TargetLowering::Legal:
2417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2418 if (isCustom) {
2419 Tmp4 = TLI.LowerOperation(Result, DAG);
2420 if (Tmp4.Val) Result = Tmp4;
2421 }
2422 break;
2423 case TargetLowering::Promote: {
2424 // First step, figure out the appropriate operation to use.
2425 // Allow SETCC to not be supported for all legal data types
2426 // Mostly this targets FP
2427 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2428 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2429
2430 // Scan for the appropriate larger type to use.
2431 while (1) {
2432 NewInTy = (MVT::ValueType)(NewInTy+1);
2433
2434 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2435 "Fell off of the edge of the integer world");
2436 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2437 "Fell off of the edge of the floating point world");
2438
2439 // If the target supports SETCC of this type, use it.
2440 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2441 break;
2442 }
2443 if (MVT::isInteger(NewInTy))
2444 assert(0 && "Cannot promote Legal Integer SETCC yet");
2445 else {
2446 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2447 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2448 }
2449 Tmp1 = LegalizeOp(Tmp1);
2450 Tmp2 = LegalizeOp(Tmp2);
2451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2452 Result = LegalizeOp(Result);
2453 break;
2454 }
2455 case TargetLowering::Expand:
2456 // Expand a setcc node into a select_cc of the same condition, lhs, and
2457 // rhs that selects between const 1 (true) and const 0 (false).
2458 MVT::ValueType VT = Node->getValueType(0);
2459 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2460 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2461 Tmp3);
2462 break;
2463 }
2464 break;
2465 case ISD::MEMSET:
2466 case ISD::MEMCPY:
2467 case ISD::MEMMOVE: {
2468 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2469 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2470
2471 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2472 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2473 case Expand: assert(0 && "Cannot expand a byte!");
2474 case Legal:
2475 Tmp3 = LegalizeOp(Node->getOperand(2));
2476 break;
2477 case Promote:
2478 Tmp3 = PromoteOp(Node->getOperand(2));
2479 break;
2480 }
2481 } else {
2482 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2483 }
2484
2485 SDOperand Tmp4;
2486 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2487 case Expand: {
2488 // Length is too big, just take the lo-part of the length.
2489 SDOperand HiPart;
2490 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2491 break;
2492 }
2493 case Legal:
2494 Tmp4 = LegalizeOp(Node->getOperand(3));
2495 break;
2496 case Promote:
2497 Tmp4 = PromoteOp(Node->getOperand(3));
2498 break;
2499 }
2500
2501 SDOperand Tmp5;
2502 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2503 case Expand: assert(0 && "Cannot expand this yet!");
2504 case Legal:
2505 Tmp5 = LegalizeOp(Node->getOperand(4));
2506 break;
2507 case Promote:
2508 Tmp5 = PromoteOp(Node->getOperand(4));
2509 break;
2510 }
2511
Rafael Espindola80825902007-10-19 10:41:11 +00002512 SDOperand Tmp6;
2513 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2514 case Expand: assert(0 && "Cannot expand this yet!");
2515 case Legal:
2516 Tmp6 = LegalizeOp(Node->getOperand(5));
2517 break;
2518 case Promote:
2519 Tmp6 = PromoteOp(Node->getOperand(5));
2520 break;
2521 }
2522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2524 default: assert(0 && "This action not implemented for this operation!");
2525 case TargetLowering::Custom:
2526 isCustom = true;
2527 // FALLTHROUGH
Rafael Espindola80825902007-10-19 10:41:11 +00002528 case TargetLowering::Legal: {
2529 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2530 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 if (isCustom) {
2532 Tmp1 = TLI.LowerOperation(Result, DAG);
2533 if (Tmp1.Val) Result = Tmp1;
2534 }
2535 break;
Rafael Espindola80825902007-10-19 10:41:11 +00002536 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 case TargetLowering::Expand: {
2538 // Otherwise, the target does not support this operation. Lower the
2539 // operation to an explicit libcall as appropriate.
2540 MVT::ValueType IntPtr = TLI.getPointerTy();
2541 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2542 TargetLowering::ArgListTy Args;
2543 TargetLowering::ArgListEntry Entry;
2544
2545 const char *FnName = 0;
2546 if (Node->getOpcode() == ISD::MEMSET) {
2547 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2548 Args.push_back(Entry);
2549 // Extend the (previously legalized) ubyte argument to be an int value
2550 // for the call.
2551 if (Tmp3.getValueType() > MVT::i32)
2552 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2553 else
2554 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2555 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2556 Args.push_back(Entry);
2557 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2558 Args.push_back(Entry);
2559
2560 FnName = "memset";
2561 } else if (Node->getOpcode() == ISD::MEMCPY ||
2562 Node->getOpcode() == ISD::MEMMOVE) {
2563 Entry.Ty = IntPtrTy;
2564 Entry.Node = Tmp2; Args.push_back(Entry);
2565 Entry.Node = Tmp3; Args.push_back(Entry);
2566 Entry.Node = Tmp4; Args.push_back(Entry);
2567 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2568 } else {
2569 assert(0 && "Unknown op!");
2570 }
2571
2572 std::pair<SDOperand,SDOperand> CallResult =
2573 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
2574 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2575 Result = CallResult.second;
2576 break;
2577 }
2578 }
2579 break;
2580 }
2581
2582 case ISD::SHL_PARTS:
2583 case ISD::SRA_PARTS:
2584 case ISD::SRL_PARTS: {
2585 SmallVector<SDOperand, 8> Ops;
2586 bool Changed = false;
2587 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2588 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2589 Changed |= Ops.back() != Node->getOperand(i);
2590 }
2591 if (Changed)
2592 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2593
2594 switch (TLI.getOperationAction(Node->getOpcode(),
2595 Node->getValueType(0))) {
2596 default: assert(0 && "This action is not supported yet!");
2597 case TargetLowering::Legal: break;
2598 case TargetLowering::Custom:
2599 Tmp1 = TLI.LowerOperation(Result, DAG);
2600 if (Tmp1.Val) {
2601 SDOperand Tmp2, RetVal(0, 0);
2602 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2603 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2604 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2605 if (i == Op.ResNo)
2606 RetVal = Tmp2;
2607 }
2608 assert(RetVal.Val && "Illegal result number");
2609 return RetVal;
2610 }
2611 break;
2612 }
2613
2614 // Since these produce multiple values, make sure to remember that we
2615 // legalized all of them.
2616 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2617 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2618 return Result.getValue(Op.ResNo);
2619 }
2620
2621 // Binary operators
2622 case ISD::ADD:
2623 case ISD::SUB:
2624 case ISD::MUL:
2625 case ISD::MULHS:
2626 case ISD::MULHU:
2627 case ISD::UDIV:
2628 case ISD::SDIV:
2629 case ISD::AND:
2630 case ISD::OR:
2631 case ISD::XOR:
2632 case ISD::SHL:
2633 case ISD::SRL:
2634 case ISD::SRA:
2635 case ISD::FADD:
2636 case ISD::FSUB:
2637 case ISD::FMUL:
2638 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002639 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2641 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2642 case Expand: assert(0 && "Not possible");
2643 case Legal:
2644 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2645 break;
2646 case Promote:
2647 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2648 break;
2649 }
2650
2651 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2652
2653 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2654 default: assert(0 && "BinOp legalize operation not supported");
2655 case TargetLowering::Legal: break;
2656 case TargetLowering::Custom:
2657 Tmp1 = TLI.LowerOperation(Result, DAG);
2658 if (Tmp1.Val) Result = Tmp1;
2659 break;
2660 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00002661 MVT::ValueType VT = Op.getValueType();
2662
2663 // See if multiply or divide can be lowered using two-result operations.
2664 SDVTList VTs = DAG.getVTList(VT, VT);
2665 if (Node->getOpcode() == ISD::MUL) {
2666 // We just need the low half of the multiply; try both the signed
2667 // and unsigned forms. If the target supports both SMUL_LOHI and
2668 // UMUL_LOHI, form a preference by checking which forms of plain
2669 // MULH it supports.
2670 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2671 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2672 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2673 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2674 unsigned OpToUse = 0;
2675 if (HasSMUL_LOHI && !HasMULHS) {
2676 OpToUse = ISD::SMUL_LOHI;
2677 } else if (HasUMUL_LOHI && !HasMULHU) {
2678 OpToUse = ISD::UMUL_LOHI;
2679 } else if (HasSMUL_LOHI) {
2680 OpToUse = ISD::SMUL_LOHI;
2681 } else if (HasUMUL_LOHI) {
2682 OpToUse = ISD::UMUL_LOHI;
2683 }
2684 if (OpToUse) {
2685 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2686 break;
2687 }
2688 }
2689 if (Node->getOpcode() == ISD::MULHS &&
2690 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2691 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2692 break;
2693 }
2694 if (Node->getOpcode() == ISD::MULHU &&
2695 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2696 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2697 break;
2698 }
2699 if (Node->getOpcode() == ISD::SDIV &&
2700 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2701 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2702 break;
2703 }
2704 if (Node->getOpcode() == ISD::UDIV &&
2705 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2706 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2707 break;
2708 }
2709
Dan Gohman6d05cac2007-10-11 23:57:53 +00002710 // Check to see if we have a libcall for this operator.
2711 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2712 bool isSigned = false;
2713 switch (Node->getOpcode()) {
2714 case ISD::UDIV:
2715 case ISD::SDIV:
2716 if (VT == MVT::i32) {
2717 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00002719 isSigned = Node->getOpcode() == ISD::SDIV;
2720 }
2721 break;
2722 case ISD::FPOW:
2723 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2724 VT == MVT::f64 ? RTLIB::POW_F64 :
2725 VT == MVT::f80 ? RTLIB::POW_F80 :
2726 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2727 RTLIB::UNKNOWN_LIBCALL;
2728 break;
2729 default: break;
2730 }
2731 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2732 SDOperand Dummy;
2733 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734 break;
2735 }
2736
2737 assert(MVT::isVector(Node->getValueType(0)) &&
2738 "Cannot expand this binary operator!");
2739 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00002740 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 break;
2742 }
2743 case TargetLowering::Promote: {
2744 switch (Node->getOpcode()) {
2745 default: assert(0 && "Do not know how to promote this BinOp!");
2746 case ISD::AND:
2747 case ISD::OR:
2748 case ISD::XOR: {
2749 MVT::ValueType OVT = Node->getValueType(0);
2750 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2751 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2752 // Bit convert each of the values to the new type.
2753 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2754 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2755 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2756 // Bit convert the result back the original type.
2757 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2758 break;
2759 }
2760 }
2761 }
2762 }
2763 break;
2764
Dan Gohman475cd732007-10-05 14:17:22 +00002765 case ISD::SMUL_LOHI:
2766 case ISD::UMUL_LOHI:
2767 case ISD::SDIVREM:
2768 case ISD::UDIVREM:
2769 // These nodes will only be produced by target-specific lowering, so
2770 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00002771 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00002772 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00002773
2774 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2775 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2776 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00002777 break;
2778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2780 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2781 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2782 case Expand: assert(0 && "Not possible");
2783 case Legal:
2784 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2785 break;
2786 case Promote:
2787 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2788 break;
2789 }
2790
2791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2792
2793 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2794 default: assert(0 && "Operation not supported");
2795 case TargetLowering::Custom:
2796 Tmp1 = TLI.LowerOperation(Result, DAG);
2797 if (Tmp1.Val) Result = Tmp1;
2798 break;
2799 case TargetLowering::Legal: break;
2800 case TargetLowering::Expand: {
2801 // If this target supports fabs/fneg natively and select is cheap,
2802 // do this efficiently.
2803 if (!TLI.isSelectExpensive() &&
2804 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2805 TargetLowering::Legal &&
2806 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
2807 TargetLowering::Legal) {
2808 // Get the sign bit of the RHS.
2809 MVT::ValueType IVT =
2810 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2811 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2812 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2813 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2814 // Get the absolute value of the result.
2815 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2816 // Select between the nabs and abs value based on the sign bit of
2817 // the input.
2818 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2819 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2820 AbsVal),
2821 AbsVal);
2822 Result = LegalizeOp(Result);
2823 break;
2824 }
2825
2826 // Otherwise, do bitwise ops!
2827 MVT::ValueType NVT =
2828 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2829 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2830 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2831 Result = LegalizeOp(Result);
2832 break;
2833 }
2834 }
2835 break;
2836
2837 case ISD::ADDC:
2838 case ISD::SUBC:
2839 Tmp1 = LegalizeOp(Node->getOperand(0));
2840 Tmp2 = LegalizeOp(Node->getOperand(1));
2841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2842 // Since this produces two values, make sure to remember that we legalized
2843 // both of them.
2844 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2845 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2846 return Result;
2847
2848 case ISD::ADDE:
2849 case ISD::SUBE:
2850 Tmp1 = LegalizeOp(Node->getOperand(0));
2851 Tmp2 = LegalizeOp(Node->getOperand(1));
2852 Tmp3 = LegalizeOp(Node->getOperand(2));
2853 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2854 // Since this produces two values, make sure to remember that we legalized
2855 // both of them.
2856 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2857 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2858 return Result;
2859
2860 case ISD::BUILD_PAIR: {
2861 MVT::ValueType PairTy = Node->getValueType(0);
2862 // TODO: handle the case where the Lo and Hi operands are not of legal type
2863 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2864 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2865 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2866 case TargetLowering::Promote:
2867 case TargetLowering::Custom:
2868 assert(0 && "Cannot promote/custom this yet!");
2869 case TargetLowering::Legal:
2870 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2871 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2872 break;
2873 case TargetLowering::Expand:
2874 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2875 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2876 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2877 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2878 TLI.getShiftAmountTy()));
2879 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
2880 break;
2881 }
2882 break;
2883 }
2884
2885 case ISD::UREM:
2886 case ISD::SREM:
2887 case ISD::FREM:
2888 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2889 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2890
2891 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2892 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2893 case TargetLowering::Custom:
2894 isCustom = true;
2895 // FALLTHROUGH
2896 case TargetLowering::Legal:
2897 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2898 if (isCustom) {
2899 Tmp1 = TLI.LowerOperation(Result, DAG);
2900 if (Tmp1.Val) Result = Tmp1;
2901 }
2902 break;
Dan Gohman5a199552007-10-08 18:33:35 +00002903 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2905 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00002906 MVT::ValueType VT = Node->getValueType(0);
2907
2908 // See if remainder can be lowered using two-result operations.
2909 SDVTList VTs = DAG.getVTList(VT, VT);
2910 if (Node->getOpcode() == ISD::SREM &&
2911 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2912 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2913 break;
2914 }
2915 if (Node->getOpcode() == ISD::UREM &&
2916 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2917 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2918 break;
2919 }
2920
2921 if (MVT::isInteger(VT)) {
2922 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002923 TargetLowering::Legal) {
2924 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002925 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
2926 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2927 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00002928 } else if (MVT::isVector(VT)) {
2929 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002930 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00002931 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002932 "Cannot expand this binary operator!");
2933 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2934 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
2935 SDOperand Dummy;
2936 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
2937 }
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00002938 } else if (MVT::isFloatingPoint(VT)) {
2939 if (MVT::isVector(VT)) {
2940 Result = LegalizeOp(UnrollVectorOp(Op));
2941 } else {
2942 // Floating point mod -> fmod libcall.
2943 RTLIB::Libcall LC = VT == MVT::f32
2944 ? RTLIB::REM_F32 : RTLIB::REM_F64;
2945 SDOperand Dummy;
2946 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2947 false/*sign irrelevant*/, Dummy);
2948 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 }
2950 break;
2951 }
Dan Gohman5a199552007-10-08 18:33:35 +00002952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 break;
2954 case ISD::VAARG: {
2955 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2956 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2957
2958 MVT::ValueType VT = Node->getValueType(0);
2959 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2960 default: assert(0 && "This action is not supported yet!");
2961 case TargetLowering::Custom:
2962 isCustom = true;
2963 // FALLTHROUGH
2964 case TargetLowering::Legal:
2965 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2966 Result = Result.getValue(0);
2967 Tmp1 = Result.getValue(1);
2968
2969 if (isCustom) {
2970 Tmp2 = TLI.LowerOperation(Result, DAG);
2971 if (Tmp2.Val) {
2972 Result = LegalizeOp(Tmp2);
2973 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2974 }
2975 }
2976 break;
2977 case TargetLowering::Expand: {
2978 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
2979 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2980 SV->getValue(), SV->getOffset());
2981 // Increment the pointer, VAList, to the next vaarg
2982 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2983 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2984 TLI.getPointerTy()));
2985 // Store the incremented VAList to the legalized pointer
2986 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2987 SV->getOffset());
2988 // Load the actual argument out of the pointer VAList
2989 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
2990 Tmp1 = LegalizeOp(Result.getValue(1));
2991 Result = LegalizeOp(Result);
2992 break;
2993 }
2994 }
2995 // Since VAARG produces two values, make sure to remember that we
2996 // legalized both of them.
2997 AddLegalizedOperand(SDOperand(Node, 0), Result);
2998 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2999 return Op.ResNo ? Tmp1 : Result;
3000 }
3001
3002 case ISD::VACOPY:
3003 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3004 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3005 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3006
3007 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3008 default: assert(0 && "This action is not supported yet!");
3009 case TargetLowering::Custom:
3010 isCustom = true;
3011 // FALLTHROUGH
3012 case TargetLowering::Legal:
3013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3014 Node->getOperand(3), Node->getOperand(4));
3015 if (isCustom) {
3016 Tmp1 = TLI.LowerOperation(Result, DAG);
3017 if (Tmp1.Val) Result = Tmp1;
3018 }
3019 break;
3020 case TargetLowering::Expand:
3021 // This defaults to loading a pointer from the input and storing it to the
3022 // output, returning the chain.
3023 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
3024 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
3025 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3026 SVD->getOffset());
3027 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3028 SVS->getOffset());
3029 break;
3030 }
3031 break;
3032
3033 case ISD::VAEND:
3034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3035 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3036
3037 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3038 default: assert(0 && "This action is not supported yet!");
3039 case TargetLowering::Custom:
3040 isCustom = true;
3041 // FALLTHROUGH
3042 case TargetLowering::Legal:
3043 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3044 if (isCustom) {
3045 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3046 if (Tmp1.Val) Result = Tmp1;
3047 }
3048 break;
3049 case TargetLowering::Expand:
3050 Result = Tmp1; // Default to a no-op, return the chain
3051 break;
3052 }
3053 break;
3054
3055 case ISD::VASTART:
3056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3057 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3058
3059 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3060
3061 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3062 default: assert(0 && "This action is not supported yet!");
3063 case TargetLowering::Legal: break;
3064 case TargetLowering::Custom:
3065 Tmp1 = TLI.LowerOperation(Result, DAG);
3066 if (Tmp1.Val) Result = Tmp1;
3067 break;
3068 }
3069 break;
3070
3071 case ISD::ROTL:
3072 case ISD::ROTR:
3073 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3074 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3076 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3077 default:
3078 assert(0 && "ROTL/ROTR legalize operation not supported");
3079 break;
3080 case TargetLowering::Legal:
3081 break;
3082 case TargetLowering::Custom:
3083 Tmp1 = TLI.LowerOperation(Result, DAG);
3084 if (Tmp1.Val) Result = Tmp1;
3085 break;
3086 case TargetLowering::Promote:
3087 assert(0 && "Do not know how to promote ROTL/ROTR");
3088 break;
3089 case TargetLowering::Expand:
3090 assert(0 && "Do not know how to expand ROTL/ROTR");
3091 break;
3092 }
3093 break;
3094
3095 case ISD::BSWAP:
3096 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3097 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3098 case TargetLowering::Custom:
3099 assert(0 && "Cannot custom legalize this yet!");
3100 case TargetLowering::Legal:
3101 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3102 break;
3103 case TargetLowering::Promote: {
3104 MVT::ValueType OVT = Tmp1.getValueType();
3105 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3106 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3107
3108 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3109 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3110 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3111 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3112 break;
3113 }
3114 case TargetLowering::Expand:
3115 Result = ExpandBSWAP(Tmp1);
3116 break;
3117 }
3118 break;
3119
3120 case ISD::CTPOP:
3121 case ISD::CTTZ:
3122 case ISD::CTLZ:
3123 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3124 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003125 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 case TargetLowering::Legal:
3127 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003128 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003129 TargetLowering::Custom) {
3130 Tmp1 = TLI.LowerOperation(Result, DAG);
3131 if (Tmp1.Val) {
3132 Result = Tmp1;
3133 }
Scott Michel48b63e62007-07-30 21:00:31 +00003134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 break;
3136 case TargetLowering::Promote: {
3137 MVT::ValueType OVT = Tmp1.getValueType();
3138 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3139
3140 // Zero extend the argument.
3141 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3142 // Perform the larger operation, then subtract if needed.
3143 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3144 switch (Node->getOpcode()) {
3145 case ISD::CTPOP:
3146 Result = Tmp1;
3147 break;
3148 case ISD::CTTZ:
3149 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3150 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3151 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3152 ISD::SETEQ);
3153 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003154 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155 break;
3156 case ISD::CTLZ:
3157 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3158 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3159 DAG.getConstant(MVT::getSizeInBits(NVT) -
3160 MVT::getSizeInBits(OVT), NVT));
3161 break;
3162 }
3163 break;
3164 }
3165 case TargetLowering::Expand:
3166 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3167 break;
3168 }
3169 break;
3170
3171 // Unary operators
3172 case ISD::FABS:
3173 case ISD::FNEG:
3174 case ISD::FSQRT:
3175 case ISD::FSIN:
3176 case ISD::FCOS:
3177 Tmp1 = LegalizeOp(Node->getOperand(0));
3178 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3179 case TargetLowering::Promote:
3180 case TargetLowering::Custom:
3181 isCustom = true;
3182 // FALLTHROUGH
3183 case TargetLowering::Legal:
3184 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3185 if (isCustom) {
3186 Tmp1 = TLI.LowerOperation(Result, DAG);
3187 if (Tmp1.Val) Result = Tmp1;
3188 }
3189 break;
3190 case TargetLowering::Expand:
3191 switch (Node->getOpcode()) {
3192 default: assert(0 && "Unreachable!");
3193 case ISD::FNEG:
3194 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3195 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3196 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3197 break;
3198 case ISD::FABS: {
3199 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3200 MVT::ValueType VT = Node->getValueType(0);
3201 Tmp2 = DAG.getConstantFP(0.0, VT);
3202 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
3203 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3204 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3205 break;
3206 }
3207 case ISD::FSQRT:
3208 case ISD::FSIN:
3209 case ISD::FCOS: {
3210 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003211
3212 // Expand unsupported unary vector operators by unrolling them.
3213 if (MVT::isVector(VT)) {
3214 Result = LegalizeOp(UnrollVectorOp(Op));
3215 break;
3216 }
3217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3219 switch(Node->getOpcode()) {
3220 case ISD::FSQRT:
Dale Johannesen0c81a522007-09-28 01:08:20 +00003221 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesenac77b272007-10-05 20:04:43 +00003222 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3223 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3224 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3225 RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003226 break;
3227 case ISD::FSIN:
3228 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3229 break;
3230 case ISD::FCOS:
3231 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3232 break;
3233 default: assert(0 && "Unreachable!");
3234 }
3235 SDOperand Dummy;
3236 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3237 false/*sign irrelevant*/, Dummy);
3238 break;
3239 }
3240 }
3241 break;
3242 }
3243 break;
3244 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003245 MVT::ValueType VT = Node->getValueType(0);
3246
3247 // Expand unsupported unary vector operators by unrolling them.
3248 if (MVT::isVector(VT)) {
3249 Result = LegalizeOp(UnrollVectorOp(Op));
3250 break;
3251 }
3252
3253 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen0c81a522007-09-28 01:08:20 +00003254 RTLIB::Libcall LC =
Dan Gohman6d05cac2007-10-11 23:57:53 +00003255 VT == MVT::f32 ? RTLIB::POWI_F32 :
3256 VT == MVT::f64 ? RTLIB::POWI_F64 :
3257 VT == MVT::f80 ? RTLIB::POWI_F80 :
3258 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesenac77b272007-10-05 20:04:43 +00003259 RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 SDOperand Dummy;
3261 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3262 false/*sign irrelevant*/, Dummy);
3263 break;
3264 }
3265 case ISD::BIT_CONVERT:
3266 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
3267 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3268 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3269 // The input has to be a vector type, we have to either scalarize it, pack
3270 // it, or convert it based on whether the input vector type is legal.
3271 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003272 int InIx = Node->getOperand(0).ResNo;
3273 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3274 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003275
3276 // Figure out if there is a simple type corresponding to this Vector
3277 // type. If so, convert to the vector type.
3278 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3279 if (TLI.isTypeLegal(TVT)) {
3280 // Turn this into a bit convert of the vector input.
3281 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3282 LegalizeOp(Node->getOperand(0)));
3283 break;
3284 } else if (NumElems == 1) {
3285 // Turn this into a bit convert of the scalar input.
3286 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3287 ScalarizeVectorOp(Node->getOperand(0)));
3288 break;
3289 } else {
3290 // FIXME: UNIMP! Store then reload
3291 assert(0 && "Cast from unsupported vector type not implemented yet!");
3292 }
3293 } else {
3294 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3295 Node->getOperand(0).getValueType())) {
3296 default: assert(0 && "Unknown operation action!");
3297 case TargetLowering::Expand:
3298 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3299 break;
3300 case TargetLowering::Legal:
3301 Tmp1 = LegalizeOp(Node->getOperand(0));
3302 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3303 break;
3304 }
3305 }
3306 break;
3307
3308 // Conversion operators. The source and destination have different types.
3309 case ISD::SINT_TO_FP:
3310 case ISD::UINT_TO_FP: {
3311 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3312 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3313 case Legal:
3314 switch (TLI.getOperationAction(Node->getOpcode(),
3315 Node->getOperand(0).getValueType())) {
3316 default: assert(0 && "Unknown operation action!");
3317 case TargetLowering::Custom:
3318 isCustom = true;
3319 // FALLTHROUGH
3320 case TargetLowering::Legal:
3321 Tmp1 = LegalizeOp(Node->getOperand(0));
3322 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3323 if (isCustom) {
3324 Tmp1 = TLI.LowerOperation(Result, DAG);
3325 if (Tmp1.Val) Result = Tmp1;
3326 }
3327 break;
3328 case TargetLowering::Expand:
3329 Result = ExpandLegalINT_TO_FP(isSigned,
3330 LegalizeOp(Node->getOperand(0)),
3331 Node->getValueType(0));
3332 break;
3333 case TargetLowering::Promote:
3334 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3335 Node->getValueType(0),
3336 isSigned);
3337 break;
3338 }
3339 break;
3340 case Expand:
3341 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3342 Node->getValueType(0), Node->getOperand(0));
3343 break;
3344 case Promote:
3345 Tmp1 = PromoteOp(Node->getOperand(0));
3346 if (isSigned) {
3347 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3348 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3349 } else {
3350 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3351 Node->getOperand(0).getValueType());
3352 }
3353 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3354 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3355 break;
3356 }
3357 break;
3358 }
3359 case ISD::TRUNCATE:
3360 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3361 case Legal:
3362 Tmp1 = LegalizeOp(Node->getOperand(0));
3363 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3364 break;
3365 case Expand:
3366 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3367
3368 // Since the result is legal, we should just be able to truncate the low
3369 // part of the source.
3370 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3371 break;
3372 case Promote:
3373 Result = PromoteOp(Node->getOperand(0));
3374 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3375 break;
3376 }
3377 break;
3378
3379 case ISD::FP_TO_SINT:
3380 case ISD::FP_TO_UINT:
3381 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3382 case Legal:
3383 Tmp1 = LegalizeOp(Node->getOperand(0));
3384
3385 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3386 default: assert(0 && "Unknown operation action!");
3387 case TargetLowering::Custom:
3388 isCustom = true;
3389 // FALLTHROUGH
3390 case TargetLowering::Legal:
3391 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3392 if (isCustom) {
3393 Tmp1 = TLI.LowerOperation(Result, DAG);
3394 if (Tmp1.Val) Result = Tmp1;
3395 }
3396 break;
3397 case TargetLowering::Promote:
3398 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3399 Node->getOpcode() == ISD::FP_TO_SINT);
3400 break;
3401 case TargetLowering::Expand:
3402 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3403 SDOperand True, False;
3404 MVT::ValueType VT = Node->getOperand(0).getValueType();
3405 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen280620d2007-09-19 17:53:26 +00003406 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003407 const uint64_t zero[] = {0, 0};
3408 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3409 uint64_t x = 1ULL << ShiftAmt;
Neil Booth4bdd45a2007-10-07 11:45:55 +00003410 (void)apf.convertFromZeroExtendedInteger
3411 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003412 Tmp2 = DAG.getConstantFP(apf, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3414 Node->getOperand(0), Tmp2, ISD::SETLT);
3415 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3416 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3417 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3418 Tmp2));
3419 False = DAG.getNode(ISD::XOR, NVT, False,
3420 DAG.getConstant(1ULL << ShiftAmt, NVT));
3421 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3422 break;
3423 } else {
3424 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3425 }
3426 break;
3427 }
3428 break;
3429 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003431 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003432 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003433 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003434 if (Node->getOpcode()==ISD::FP_TO_SINT)
3435 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003436 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3437 (DAG.getNode(ISD::FP_ROUND_INREG,
3438 MVT::ppcf128, Node->getOperand(0),
3439 DAG.getValueType(MVT::f64)))));
Dale Johannesend3b6af32007-10-11 23:32:15 +00003440 else {
3441 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3442 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3443 Tmp2 = DAG.getConstantFP(apf, OVT);
3444 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3445 // FIXME: generated code sucks.
3446 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3447 DAG.getNode(ISD::ADD, MVT::i32,
3448 DAG.getNode(ISD::FP_TO_SINT, VT,
3449 DAG.getNode(ISD::FSUB, OVT,
3450 Node->getOperand(0), Tmp2)),
3451 DAG.getConstant(0x80000000, MVT::i32)),
3452 DAG.getNode(ISD::FP_TO_SINT, VT,
3453 Node->getOperand(0)),
3454 DAG.getCondCode(ISD::SETGE));
3455 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003456 break;
3457 }
Dale Johannesend3b6af32007-10-11 23:32:15 +00003458 // Convert f32 / f64 to i32 / i64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3460 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003461 case ISD::FP_TO_SINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003462 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003463 LC = (VT == MVT::i32)
3464 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003465 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003466 LC = (VT == MVT::i32)
3467 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003468 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003469 assert(VT == MVT::i64);
Dale Johannesenac77b272007-10-05 20:04:43 +00003470 LC = RTLIB::FPTOSINT_F80_I64;
3471 }
3472 else if (OVT == MVT::ppcf128) {
3473 assert(VT == MVT::i64);
3474 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003475 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003477 }
3478 case ISD::FP_TO_UINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003479 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480 LC = (VT == MVT::i32)
3481 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003482 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003483 LC = (VT == MVT::i32)
3484 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003485 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003486 LC = (VT == MVT::i32)
Dale Johannesenac77b272007-10-05 20:04:43 +00003487 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3488 }
3489 else if (OVT == MVT::ppcf128) {
3490 assert(VT == MVT::i64);
3491 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003492 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003493 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003494 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 default: assert(0 && "Unreachable!");
3496 }
3497 SDOperand Dummy;
3498 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3499 false/*sign irrelevant*/, Dummy);
3500 break;
3501 }
3502 case Promote:
3503 Tmp1 = PromoteOp(Node->getOperand(0));
3504 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3505 Result = LegalizeOp(Result);
3506 break;
3507 }
3508 break;
3509
Dale Johannesen60892372007-08-09 17:27:48 +00003510 case ISD::FP_EXTEND:
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003511 case ISD::FP_ROUND: {
3512 MVT::ValueType newVT = Op.getValueType();
3513 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3514 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00003515 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3516 SDOperand Lo, Hi;
3517 ExpandOp(Node->getOperand(0), Lo, Hi);
3518 if (newVT == MVT::f64)
3519 Result = Hi;
3520 else
3521 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3522 break;
Dale Johannesen60892372007-08-09 17:27:48 +00003523 } else {
Dale Johannesen472d15d2007-10-06 01:24:11 +00003524 // The only other way we can lower this is to turn it into a STORE,
3525 // LOAD pair, targetting a temporary location (a stack slot).
3526
3527 // NOTE: there is a choice here between constantly creating new stack
3528 // slots and always reusing the same one. We currently always create
3529 // new ones, as reuse may inhibit scheduling.
3530 MVT::ValueType slotVT =
3531 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3532 const Type *Ty = MVT::getTypeForValueType(slotVT);
Duncan Sandsf99fdc62007-11-01 20:53:16 +00003533 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dale Johannesen472d15d2007-10-06 01:24:11 +00003534 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3535 MachineFunction &MF = DAG.getMachineFunction();
3536 int SSFI =
3537 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3538 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3539 if (Node->getOpcode() == ISD::FP_EXTEND) {
3540 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3541 StackSlot, NULL, 0);
3542 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3543 Result, StackSlot, NULL, 0, oldVT);
3544 } else {
3545 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3546 StackSlot, NULL, 0, newVT);
Duncan Sandsb42a44e2007-10-16 09:07:20 +00003547 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen472d15d2007-10-06 01:24:11 +00003548 }
3549 break;
Dale Johannesen60892372007-08-09 17:27:48 +00003550 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 }
3553 // FALL THROUGH
3554 case ISD::ANY_EXTEND:
3555 case ISD::ZERO_EXTEND:
3556 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3558 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3559 case Legal:
3560 Tmp1 = LegalizeOp(Node->getOperand(0));
3561 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3562 break;
3563 case Promote:
3564 switch (Node->getOpcode()) {
3565 case ISD::ANY_EXTEND:
3566 Tmp1 = PromoteOp(Node->getOperand(0));
3567 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3568 break;
3569 case ISD::ZERO_EXTEND:
3570 Result = PromoteOp(Node->getOperand(0));
3571 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3572 Result = DAG.getZeroExtendInReg(Result,
3573 Node->getOperand(0).getValueType());
3574 break;
3575 case ISD::SIGN_EXTEND:
3576 Result = PromoteOp(Node->getOperand(0));
3577 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3578 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3579 Result,
3580 DAG.getValueType(Node->getOperand(0).getValueType()));
3581 break;
3582 case ISD::FP_EXTEND:
3583 Result = PromoteOp(Node->getOperand(0));
3584 if (Result.getValueType() != Op.getValueType())
3585 // Dynamically dead while we have only 2 FP types.
3586 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3587 break;
3588 case ISD::FP_ROUND:
3589 Result = PromoteOp(Node->getOperand(0));
3590 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3591 break;
3592 }
3593 }
3594 break;
3595 case ISD::FP_ROUND_INREG:
3596 case ISD::SIGN_EXTEND_INREG: {
3597 Tmp1 = LegalizeOp(Node->getOperand(0));
3598 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3599
3600 // If this operation is not supported, convert it to a shl/shr or load/store
3601 // pair.
3602 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3603 default: assert(0 && "This action not supported for this op yet!");
3604 case TargetLowering::Legal:
3605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3606 break;
3607 case TargetLowering::Expand:
3608 // If this is an integer extend and shifts are supported, do that.
3609 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3610 // NOTE: we could fall back on load/store here too for targets without
3611 // SAR. However, it is doubtful that any exist.
3612 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3613 MVT::getSizeInBits(ExtraVT);
3614 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3615 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3616 Node->getOperand(0), ShiftCst);
3617 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3618 Result, ShiftCst);
3619 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3620 // The only way we can lower this is to turn it into a TRUNCSTORE,
3621 // EXTLOAD pair, targetting a temporary location (a stack slot).
3622
3623 // NOTE: there is a choice here between constantly creating new stack
3624 // slots and always reusing the same one. We currently always create
3625 // new ones, as reuse may inhibit scheduling.
3626 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Duncan Sandsf99fdc62007-11-01 20:53:16 +00003627 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003628 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3629 MachineFunction &MF = DAG.getMachineFunction();
3630 int SSFI =
3631 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3632 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3633 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3634 StackSlot, NULL, 0, ExtraVT);
3635 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
3636 Result, StackSlot, NULL, 0, ExtraVT);
3637 } else {
3638 assert(0 && "Unknown op");
3639 }
3640 break;
3641 }
3642 break;
3643 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003644 case ISD::TRAMPOLINE: {
3645 SDOperand Ops[6];
3646 for (unsigned i = 0; i != 6; ++i)
3647 Ops[i] = LegalizeOp(Node->getOperand(i));
3648 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3649 // The only option for this node is to custom lower it.
3650 Result = TLI.LowerOperation(Result, DAG);
3651 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003652
3653 // Since trampoline produces two values, make sure to remember that we
3654 // legalized both of them.
3655 Tmp1 = LegalizeOp(Result.getValue(1));
3656 Result = LegalizeOp(Result);
3657 AddLegalizedOperand(SDOperand(Node, 0), Result);
3658 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3659 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003660 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003661 }
3662
3663 assert(Result.getValueType() == Op.getValueType() &&
3664 "Bad legalization!");
3665
3666 // Make sure that the generated code is itself legal.
3667 if (Result != Op)
3668 Result = LegalizeOp(Result);
3669
3670 // Note that LegalizeOp may be reentered even from single-use nodes, which
3671 // means that we always must cache transformed nodes.
3672 AddLegalizedOperand(Op, Result);
3673 return Result;
3674}
3675
3676/// PromoteOp - Given an operation that produces a value in an invalid type,
3677/// promote it to compute the value into a larger type. The produced value will
3678/// have the correct bits for the low portion of the register, but no guarantee
3679/// is made about the top bits: it may be zero, sign-extended, or garbage.
3680SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3681 MVT::ValueType VT = Op.getValueType();
3682 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3683 assert(getTypeAction(VT) == Promote &&
3684 "Caller should expand or legalize operands that are not promotable!");
3685 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3686 "Cannot promote to smaller type!");
3687
3688 SDOperand Tmp1, Tmp2, Tmp3;
3689 SDOperand Result;
3690 SDNode *Node = Op.Val;
3691
3692 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3693 if (I != PromotedNodes.end()) return I->second;
3694
3695 switch (Node->getOpcode()) {
3696 case ISD::CopyFromReg:
3697 assert(0 && "CopyFromReg must be legal!");
3698 default:
3699#ifndef NDEBUG
3700 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
3701#endif
3702 assert(0 && "Do not know how to promote this operator!");
3703 abort();
3704 case ISD::UNDEF:
3705 Result = DAG.getNode(ISD::UNDEF, NVT);
3706 break;
3707 case ISD::Constant:
3708 if (VT != MVT::i1)
3709 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3710 else
3711 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
3712 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3713 break;
3714 case ISD::ConstantFP:
3715 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3716 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3717 break;
3718
3719 case ISD::SETCC:
3720 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
3721 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3722 Node->getOperand(1), Node->getOperand(2));
3723 break;
3724
3725 case ISD::TRUNCATE:
3726 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3727 case Legal:
3728 Result = LegalizeOp(Node->getOperand(0));
3729 assert(Result.getValueType() >= NVT &&
3730 "This truncation doesn't make sense!");
3731 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3732 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3733 break;
3734 case Promote:
3735 // The truncation is not required, because we don't guarantee anything
3736 // about high bits anyway.
3737 Result = PromoteOp(Node->getOperand(0));
3738 break;
3739 case Expand:
3740 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3741 // Truncate the low part of the expanded value to the result type
3742 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
3743 }
3744 break;
3745 case ISD::SIGN_EXTEND:
3746 case ISD::ZERO_EXTEND:
3747 case ISD::ANY_EXTEND:
3748 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3749 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3750 case Legal:
3751 // Input is legal? Just do extend all the way to the larger type.
3752 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
3753 break;
3754 case Promote:
3755 // Promote the reg if it's smaller.
3756 Result = PromoteOp(Node->getOperand(0));
3757 // The high bits are not guaranteed to be anything. Insert an extend.
3758 if (Node->getOpcode() == ISD::SIGN_EXTEND)
3759 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3760 DAG.getValueType(Node->getOperand(0).getValueType()));
3761 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
3762 Result = DAG.getZeroExtendInReg(Result,
3763 Node->getOperand(0).getValueType());
3764 break;
3765 }
3766 break;
3767 case ISD::BIT_CONVERT:
3768 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3769 Result = PromoteOp(Result);
3770 break;
3771
3772 case ISD::FP_EXTEND:
3773 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3774 case ISD::FP_ROUND:
3775 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3776 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3777 case Promote: assert(0 && "Unreachable with 2 FP types!");
3778 case Legal:
3779 // Input is legal? Do an FP_ROUND_INREG.
3780 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
3781 DAG.getValueType(VT));
3782 break;
3783 }
3784 break;
3785
3786 case ISD::SINT_TO_FP:
3787 case ISD::UINT_TO_FP:
3788 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3789 case Legal:
3790 // No extra round required here.
3791 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
3792 break;
3793
3794 case Promote:
3795 Result = PromoteOp(Node->getOperand(0));
3796 if (Node->getOpcode() == ISD::SINT_TO_FP)
3797 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3798 Result,
3799 DAG.getValueType(Node->getOperand(0).getValueType()));
3800 else
3801 Result = DAG.getZeroExtendInReg(Result,
3802 Node->getOperand(0).getValueType());
3803 // No extra round required here.
3804 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
3805 break;
3806 case Expand:
3807 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3808 Node->getOperand(0));
3809 // Round if we cannot tolerate excess precision.
3810 if (NoExcessFPPrecision)
3811 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3812 DAG.getValueType(VT));
3813 break;
3814 }
3815 break;
3816
3817 case ISD::SIGN_EXTEND_INREG:
3818 Result = PromoteOp(Node->getOperand(0));
3819 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3820 Node->getOperand(1));
3821 break;
3822 case ISD::FP_TO_SINT:
3823 case ISD::FP_TO_UINT:
3824 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3825 case Legal:
3826 case Expand:
3827 Tmp1 = Node->getOperand(0);
3828 break;
3829 case Promote:
3830 // The input result is prerounded, so we don't have to do anything
3831 // special.
3832 Tmp1 = PromoteOp(Node->getOperand(0));
3833 break;
3834 }
3835 // If we're promoting a UINT to a larger size, check to see if the new node
3836 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3837 // we can use that instead. This allows us to generate better code for
3838 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3839 // legal, such as PowerPC.
3840 if (Node->getOpcode() == ISD::FP_TO_UINT &&
3841 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
3842 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3843 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
3844 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3845 } else {
3846 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3847 }
3848 break;
3849
3850 case ISD::FABS:
3851 case ISD::FNEG:
3852 Tmp1 = PromoteOp(Node->getOperand(0));
3853 assert(Tmp1.getValueType() == NVT);
3854 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3855 // NOTE: we do not have to do any extra rounding here for
3856 // NoExcessFPPrecision, because we know the input will have the appropriate
3857 // precision, and these operations don't modify precision at all.
3858 break;
3859
3860 case ISD::FSQRT:
3861 case ISD::FSIN:
3862 case ISD::FCOS:
3863 Tmp1 = PromoteOp(Node->getOperand(0));
3864 assert(Tmp1.getValueType() == NVT);
3865 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3866 if (NoExcessFPPrecision)
3867 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3868 DAG.getValueType(VT));
3869 break;
3870
3871 case ISD::FPOWI: {
3872 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3873 // directly as well, which may be better.
3874 Tmp1 = PromoteOp(Node->getOperand(0));
3875 assert(Tmp1.getValueType() == NVT);
3876 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3877 if (NoExcessFPPrecision)
3878 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3879 DAG.getValueType(VT));
3880 break;
3881 }
3882
3883 case ISD::AND:
3884 case ISD::OR:
3885 case ISD::XOR:
3886 case ISD::ADD:
3887 case ISD::SUB:
3888 case ISD::MUL:
3889 // The input may have strange things in the top bits of the registers, but
3890 // these operations don't care. They may have weird bits going out, but
3891 // that too is okay if they are integer operations.
3892 Tmp1 = PromoteOp(Node->getOperand(0));
3893 Tmp2 = PromoteOp(Node->getOperand(1));
3894 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3895 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3896 break;
3897 case ISD::FADD:
3898 case ISD::FSUB:
3899 case ISD::FMUL:
3900 Tmp1 = PromoteOp(Node->getOperand(0));
3901 Tmp2 = PromoteOp(Node->getOperand(1));
3902 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3903 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3904
3905 // Floating point operations will give excess precision that we may not be
3906 // able to tolerate. If we DO allow excess precision, just leave it,
3907 // otherwise excise it.
3908 // FIXME: Why would we need to round FP ops more than integer ones?
3909 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
3910 if (NoExcessFPPrecision)
3911 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3912 DAG.getValueType(VT));
3913 break;
3914
3915 case ISD::SDIV:
3916 case ISD::SREM:
3917 // These operators require that their input be sign extended.
3918 Tmp1 = PromoteOp(Node->getOperand(0));
3919 Tmp2 = PromoteOp(Node->getOperand(1));
3920 if (MVT::isInteger(NVT)) {
3921 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3922 DAG.getValueType(VT));
3923 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3924 DAG.getValueType(VT));
3925 }
3926 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3927
3928 // Perform FP_ROUND: this is probably overly pessimistic.
3929 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
3930 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3931 DAG.getValueType(VT));
3932 break;
3933 case ISD::FDIV:
3934 case ISD::FREM:
3935 case ISD::FCOPYSIGN:
3936 // These operators require that their input be fp extended.
3937 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3938 case Legal:
3939 Tmp1 = LegalizeOp(Node->getOperand(0));
3940 break;
3941 case Promote:
3942 Tmp1 = PromoteOp(Node->getOperand(0));
3943 break;
3944 case Expand:
3945 assert(0 && "not implemented");
3946 }
3947 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3948 case Legal:
3949 Tmp2 = LegalizeOp(Node->getOperand(1));
3950 break;
3951 case Promote:
3952 Tmp2 = PromoteOp(Node->getOperand(1));
3953 break;
3954 case Expand:
3955 assert(0 && "not implemented");
3956 }
3957 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3958
3959 // Perform FP_ROUND: this is probably overly pessimistic.
3960 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
3961 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3962 DAG.getValueType(VT));
3963 break;
3964
3965 case ISD::UDIV:
3966 case ISD::UREM:
3967 // These operators require that their input be zero extended.
3968 Tmp1 = PromoteOp(Node->getOperand(0));
3969 Tmp2 = PromoteOp(Node->getOperand(1));
3970 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
3971 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3972 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3973 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3974 break;
3975
3976 case ISD::SHL:
3977 Tmp1 = PromoteOp(Node->getOperand(0));
3978 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
3979 break;
3980 case ISD::SRA:
3981 // The input value must be properly sign extended.
3982 Tmp1 = PromoteOp(Node->getOperand(0));
3983 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3984 DAG.getValueType(VT));
3985 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
3986 break;
3987 case ISD::SRL:
3988 // The input value must be properly zero extended.
3989 Tmp1 = PromoteOp(Node->getOperand(0));
3990 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3991 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
3992 break;
3993
3994 case ISD::VAARG:
3995 Tmp1 = Node->getOperand(0); // Get the chain.
3996 Tmp2 = Node->getOperand(1); // Get the pointer.
3997 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3998 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3999 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4000 } else {
4001 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
4002 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
4003 SV->getValue(), SV->getOffset());
4004 // Increment the pointer, VAList, to the next vaarg
4005 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4006 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4007 TLI.getPointerTy()));
4008 // Store the incremented VAList to the legalized pointer
4009 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4010 SV->getOffset());
4011 // Load the actual argument out of the pointer VAList
4012 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4013 }
4014 // Remember that we legalized the chain.
4015 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4016 break;
4017
4018 case ISD::LOAD: {
4019 LoadSDNode *LD = cast<LoadSDNode>(Node);
4020 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4021 ? ISD::EXTLOAD : LD->getExtensionType();
4022 Result = DAG.getExtLoad(ExtType, NVT,
4023 LD->getChain(), LD->getBasePtr(),
4024 LD->getSrcValue(), LD->getSrcValueOffset(),
4025 LD->getLoadedVT(),
4026 LD->isVolatile(),
4027 LD->getAlignment());
4028 // Remember that we legalized the chain.
4029 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4030 break;
4031 }
4032 case ISD::SELECT:
4033 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4034 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4035 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4036 break;
4037 case ISD::SELECT_CC:
4038 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4039 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4040 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4041 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4042 break;
4043 case ISD::BSWAP:
4044 Tmp1 = Node->getOperand(0);
4045 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4046 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4047 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4048 DAG.getConstant(MVT::getSizeInBits(NVT) -
4049 MVT::getSizeInBits(VT),
4050 TLI.getShiftAmountTy()));
4051 break;
4052 case ISD::CTPOP:
4053 case ISD::CTTZ:
4054 case ISD::CTLZ:
4055 // Zero extend the argument
4056 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4057 // Perform the larger operation, then subtract if needed.
4058 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4059 switch(Node->getOpcode()) {
4060 case ISD::CTPOP:
4061 Result = Tmp1;
4062 break;
4063 case ISD::CTTZ:
4064 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4065 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
4066 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4067 ISD::SETEQ);
4068 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4069 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4070 break;
4071 case ISD::CTLZ:
4072 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4073 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4074 DAG.getConstant(MVT::getSizeInBits(NVT) -
4075 MVT::getSizeInBits(VT), NVT));
4076 break;
4077 }
4078 break;
4079 case ISD::EXTRACT_SUBVECTOR:
4080 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4081 break;
4082 case ISD::EXTRACT_VECTOR_ELT:
4083 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4084 break;
4085 }
4086
4087 assert(Result.Val && "Didn't set a result!");
4088
4089 // Make sure the result is itself legal.
4090 Result = LegalizeOp(Result);
4091
4092 // Remember that we promoted this!
4093 AddPromotedOperand(Op, Result);
4094 return Result;
4095}
4096
4097/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4098/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4099/// based on the vector type. The return type of this matches the element type
4100/// of the vector, which may not be legal for the target.
4101SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4102 // We know that operand #0 is the Vec vector. If the index is a constant
4103 // or if the invec is a supported hardware type, we can use it. Otherwise,
4104 // lower to a store then an indexed load.
4105 SDOperand Vec = Op.getOperand(0);
4106 SDOperand Idx = Op.getOperand(1);
4107
Dan Gohmana0763d92007-09-24 15:54:53 +00004108 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 unsigned NumElems = MVT::getVectorNumElements(TVT);
4110
4111 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4112 default: assert(0 && "This action is not supported yet!");
4113 case TargetLowering::Custom: {
4114 Vec = LegalizeOp(Vec);
4115 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4116 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4117 if (Tmp3.Val)
4118 return Tmp3;
4119 break;
4120 }
4121 case TargetLowering::Legal:
4122 if (isTypeLegal(TVT)) {
4123 Vec = LegalizeOp(Vec);
4124 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004125 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004126 }
4127 break;
4128 case TargetLowering::Expand:
4129 break;
4130 }
4131
4132 if (NumElems == 1) {
4133 // This must be an access of the only element. Return it.
4134 Op = ScalarizeVectorOp(Vec);
4135 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4136 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4137 SDOperand Lo, Hi;
4138 SplitVectorOp(Vec, Lo, Hi);
4139 if (CIdx->getValue() < NumElems/2) {
4140 Vec = Lo;
4141 } else {
4142 Vec = Hi;
4143 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4144 Idx.getValueType());
4145 }
4146
4147 // It's now an extract from the appropriate high or low part. Recurse.
4148 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4149 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4150 } else {
4151 // Store the value to a temporary stack slot, then LOAD the scalar
4152 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004153 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4155
4156 // Add the offset to the index.
4157 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4158 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4159 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004160
4161 if (MVT::getSizeInBits(Idx.getValueType()) >
4162 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004163 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004164 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004165 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004166
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4168
4169 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4170 }
4171 return Op;
4172}
4173
4174/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4175/// we assume the operation can be split if it is not already legal.
4176SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4177 // We know that operand #0 is the Vec vector. For now we assume the index
4178 // is a constant and that the extracted result is a supported hardware type.
4179 SDOperand Vec = Op.getOperand(0);
4180 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4181
4182 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4183
4184 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4185 // This must be an access of the desired vector length. Return it.
4186 return Vec;
4187 }
4188
4189 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4190 SDOperand Lo, Hi;
4191 SplitVectorOp(Vec, Lo, Hi);
4192 if (CIdx->getValue() < NumElems/2) {
4193 Vec = Lo;
4194 } else {
4195 Vec = Hi;
4196 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4197 }
4198
4199 // It's now an extract from the appropriate high or low part. Recurse.
4200 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4201 return ExpandEXTRACT_SUBVECTOR(Op);
4202}
4203
4204/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4205/// with condition CC on the current target. This usually involves legalizing
4206/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4207/// there may be no choice but to create a new SetCC node to represent the
4208/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4209/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4210void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4211 SDOperand &RHS,
4212 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004213 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214
4215 switch (getTypeAction(LHS.getValueType())) {
4216 case Legal:
4217 Tmp1 = LegalizeOp(LHS); // LHS
4218 Tmp2 = LegalizeOp(RHS); // RHS
4219 break;
4220 case Promote:
4221 Tmp1 = PromoteOp(LHS); // LHS
4222 Tmp2 = PromoteOp(RHS); // RHS
4223
4224 // If this is an FP compare, the operands have already been extended.
4225 if (MVT::isInteger(LHS.getValueType())) {
4226 MVT::ValueType VT = LHS.getValueType();
4227 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4228
4229 // Otherwise, we have to insert explicit sign or zero extends. Note
4230 // that we could insert sign extends for ALL conditions, but zero extend
4231 // is cheaper on many machines (an AND instead of two shifts), so prefer
4232 // it.
4233 switch (cast<CondCodeSDNode>(CC)->get()) {
4234 default: assert(0 && "Unknown integer comparison!");
4235 case ISD::SETEQ:
4236 case ISD::SETNE:
4237 case ISD::SETUGE:
4238 case ISD::SETUGT:
4239 case ISD::SETULE:
4240 case ISD::SETULT:
4241 // ALL of these operations will work if we either sign or zero extend
4242 // the operands (including the unsigned comparisons!). Zero extend is
4243 // usually a simpler/cheaper operation, so prefer it.
4244 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4245 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4246 break;
4247 case ISD::SETGE:
4248 case ISD::SETGT:
4249 case ISD::SETLT:
4250 case ISD::SETLE:
4251 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4252 DAG.getValueType(VT));
4253 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4254 DAG.getValueType(VT));
4255 break;
4256 }
4257 }
4258 break;
4259 case Expand: {
4260 MVT::ValueType VT = LHS.getValueType();
4261 if (VT == MVT::f32 || VT == MVT::f64) {
4262 // Expand into one or more soft-fp libcall(s).
4263 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4264 switch (cast<CondCodeSDNode>(CC)->get()) {
4265 case ISD::SETEQ:
4266 case ISD::SETOEQ:
4267 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4268 break;
4269 case ISD::SETNE:
4270 case ISD::SETUNE:
4271 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4272 break;
4273 case ISD::SETGE:
4274 case ISD::SETOGE:
4275 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4276 break;
4277 case ISD::SETLT:
4278 case ISD::SETOLT:
4279 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4280 break;
4281 case ISD::SETLE:
4282 case ISD::SETOLE:
4283 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4284 break;
4285 case ISD::SETGT:
4286 case ISD::SETOGT:
4287 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4288 break;
4289 case ISD::SETUO:
4290 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4291 break;
4292 case ISD::SETO:
4293 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4294 break;
4295 default:
4296 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4297 switch (cast<CondCodeSDNode>(CC)->get()) {
4298 case ISD::SETONE:
4299 // SETONE = SETOLT | SETOGT
4300 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4301 // Fallthrough
4302 case ISD::SETUGT:
4303 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4304 break;
4305 case ISD::SETUGE:
4306 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4307 break;
4308 case ISD::SETULT:
4309 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4310 break;
4311 case ISD::SETULE:
4312 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4313 break;
4314 case ISD::SETUEQ:
4315 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4316 break;
4317 default: assert(0 && "Unsupported FP setcc!");
4318 }
4319 }
4320
4321 SDOperand Dummy;
4322 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
4323 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4324 false /*sign irrelevant*/, Dummy);
4325 Tmp2 = DAG.getConstant(0, MVT::i32);
4326 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4327 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
4328 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
4329 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
4330 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4331 false /*sign irrelevant*/, Dummy);
4332 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4333 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4334 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4335 Tmp2 = SDOperand();
4336 }
4337 LHS = Tmp1;
4338 RHS = Tmp2;
4339 return;
4340 }
4341
4342 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4343 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004344 ExpandOp(RHS, RHSLo, RHSHi);
4345 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4346
4347 if (VT==MVT::ppcf128) {
4348 // FIXME: This generated code sucks. We want to generate
4349 // FCMP crN, hi1, hi2
4350 // BNE crN, L:
4351 // FCMP crN, lo1, lo2
4352 // The following can be improved, but not that much.
4353 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4354 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4355 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4356 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4357 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4358 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4359 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4360 Tmp2 = SDOperand();
4361 break;
4362 }
4363
4364 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 case ISD::SETEQ:
4366 case ISD::SETNE:
4367 if (RHSLo == RHSHi)
4368 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4369 if (RHSCST->isAllOnesValue()) {
4370 // Comparison to -1.
4371 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4372 Tmp2 = RHSLo;
4373 break;
4374 }
4375
4376 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4377 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4378 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4379 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4380 break;
4381 default:
4382 // If this is a comparison of the sign bit, just look at the top part.
4383 // X > -1, x < 0
4384 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4385 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4386 CST->getValue() == 0) || // X < 0
4387 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4388 CST->isAllOnesValue())) { // X > -1
4389 Tmp1 = LHSHi;
4390 Tmp2 = RHSHi;
4391 break;
4392 }
4393
4394 // FIXME: This generated code sucks.
4395 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 switch (CCCode) {
4397 default: assert(0 && "Unknown integer setcc!");
4398 case ISD::SETLT:
4399 case ISD::SETULT: LowCC = ISD::SETULT; break;
4400 case ISD::SETGT:
4401 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4402 case ISD::SETLE:
4403 case ISD::SETULE: LowCC = ISD::SETULE; break;
4404 case ISD::SETGE:
4405 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4406 }
4407
4408 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4409 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4410 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4411
4412 // NOTE: on targets without efficient SELECT of bools, we can always use
4413 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4414 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4415 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4416 false, DagCombineInfo);
4417 if (!Tmp1.Val)
4418 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4419 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4420 CCCode, false, DagCombineInfo);
4421 if (!Tmp2.Val)
Chris Lattner6fb53da2007-10-15 17:48:57 +00004422 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423
4424 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4425 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4426 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4427 (Tmp2C && Tmp2C->getValue() == 0 &&
4428 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4429 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4430 (Tmp2C && Tmp2C->getValue() == 1 &&
4431 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4432 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4433 // low part is known false, returns high part.
4434 // For LE / GE, if high part is known false, ignore the low part.
4435 // For LT / GT, if high part is known true, ignore the low part.
4436 Tmp1 = Tmp2;
4437 Tmp2 = SDOperand();
4438 } else {
4439 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4440 ISD::SETEQ, false, DagCombineInfo);
4441 if (!Result.Val)
4442 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4443 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4444 Result, Tmp1, Tmp2));
4445 Tmp1 = Result;
4446 Tmp2 = SDOperand();
4447 }
4448 }
4449 }
4450 }
4451 LHS = Tmp1;
4452 RHS = Tmp2;
4453}
4454
4455/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
4456/// The resultant code need not be legal. Note that SrcOp is the input operand
4457/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
4458SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4459 SDOperand SrcOp) {
4460 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004461 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462
4463 // Emit a store to the stack slot.
4464 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
4465 // Result is a load from the stack slot.
4466 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4467}
4468
4469SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4470 // Create a vector sized/aligned stack slot, store the value to element #0,
4471 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004472 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
4474 NULL, 0);
4475 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
4476}
4477
4478
4479/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4480/// support the operation, but do support the resultant vector type.
4481SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4482
4483 // If the only non-undef value is the low element, turn this into a
4484 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4485 unsigned NumElems = Node->getNumOperands();
4486 bool isOnlyLowElement = true;
4487 SDOperand SplatValue = Node->getOperand(0);
4488 std::map<SDOperand, std::vector<unsigned> > Values;
4489 Values[SplatValue].push_back(0);
4490 bool isConstant = true;
4491 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4492 SplatValue.getOpcode() != ISD::UNDEF)
4493 isConstant = false;
4494
4495 for (unsigned i = 1; i < NumElems; ++i) {
4496 SDOperand V = Node->getOperand(i);
4497 Values[V].push_back(i);
4498 if (V.getOpcode() != ISD::UNDEF)
4499 isOnlyLowElement = false;
4500 if (SplatValue != V)
4501 SplatValue = SDOperand(0,0);
4502
4503 // If this isn't a constant element or an undef, we can't use a constant
4504 // pool load.
4505 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4506 V.getOpcode() != ISD::UNDEF)
4507 isConstant = false;
4508 }
4509
4510 if (isOnlyLowElement) {
4511 // If the low element is an undef too, then this whole things is an undef.
4512 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4513 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4514 // Otherwise, turn this into a scalar_to_vector node.
4515 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4516 Node->getOperand(0));
4517 }
4518
4519 // If all elements are constants, create a load from the constant pool.
4520 if (isConstant) {
4521 MVT::ValueType VT = Node->getValueType(0);
4522 const Type *OpNTy =
4523 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4524 std::vector<Constant*> CV;
4525 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4526 if (ConstantFPSDNode *V =
4527 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00004528 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 } else if (ConstantSDNode *V =
4530 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4531 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4532 } else {
4533 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4534 CV.push_back(UndefValue::get(OpNTy));
4535 }
4536 }
4537 Constant *CP = ConstantVector::get(CV);
4538 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
4539 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
4540 }
4541
4542 if (SplatValue.Val) { // Splat of one value?
4543 // Build the shuffle constant vector: <0, 0, 0, 0>
4544 MVT::ValueType MaskVT =
4545 MVT::getIntVectorWithNumElements(NumElems);
4546 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4547 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4548 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4549 &ZeroVec[0], ZeroVec.size());
4550
4551 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4552 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4553 // Get the splatted value into the low element of a vector register.
4554 SDOperand LowValVec =
4555 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4556
4557 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4558 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4559 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4560 SplatMask);
4561 }
4562 }
4563
4564 // If there are only two unique elements, we may be able to turn this into a
4565 // vector shuffle.
4566 if (Values.size() == 2) {
4567 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4568 MVT::ValueType MaskVT =
4569 MVT::getIntVectorWithNumElements(NumElems);
4570 std::vector<SDOperand> MaskVec(NumElems);
4571 unsigned i = 0;
4572 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4573 E = Values.end(); I != E; ++I) {
4574 for (std::vector<unsigned>::iterator II = I->second.begin(),
4575 EE = I->second.end(); II != EE; ++II)
4576 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
4577 i += NumElems;
4578 }
4579 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4580 &MaskVec[0], MaskVec.size());
4581
4582 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4583 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4584 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
4585 SmallVector<SDOperand, 8> Ops;
4586 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4587 E = Values.end(); I != E; ++I) {
4588 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4589 I->first);
4590 Ops.push_back(Op);
4591 }
4592 Ops.push_back(ShuffleMask);
4593
4594 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
4595 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4596 &Ops[0], Ops.size());
4597 }
4598 }
4599
4600 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4601 // aligned object on the stack, store each element into it, then load
4602 // the result as a vector.
4603 MVT::ValueType VT = Node->getValueType(0);
4604 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004605 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004606
4607 // Emit a store of each element to the stack slot.
4608 SmallVector<SDOperand, 8> Stores;
4609 unsigned TypeByteSize =
4610 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
4611 // Store (in the right endianness) the elements to memory.
4612 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4613 // Ignore undef elements.
4614 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4615
4616 unsigned Offset = TypeByteSize*i;
4617
4618 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4619 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4620
4621 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
4622 NULL, 0));
4623 }
4624
4625 SDOperand StoreChain;
4626 if (!Stores.empty()) // Not all undef elements?
4627 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4628 &Stores[0], Stores.size());
4629 else
4630 StoreChain = DAG.getEntryNode();
4631
4632 // Result is a load from the stack slot.
4633 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
4634}
4635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4637 SDOperand Op, SDOperand Amt,
4638 SDOperand &Lo, SDOperand &Hi) {
4639 // Expand the subcomponents.
4640 SDOperand LHSL, LHSH;
4641 ExpandOp(Op, LHSL, LHSH);
4642
4643 SDOperand Ops[] = { LHSL, LHSH, Amt };
4644 MVT::ValueType VT = LHSL.getValueType();
4645 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
4646 Hi = Lo.getValue(1);
4647}
4648
4649
4650/// ExpandShift - Try to find a clever way to expand this shift operation out to
4651/// smaller elements. If we can't find a way that is more efficient than a
4652/// libcall on this target, return false. Otherwise, return true with the
4653/// low-parts expanded into Lo and Hi.
4654bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4655 SDOperand &Lo, SDOperand &Hi) {
4656 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4657 "This is not a shift!");
4658
4659 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
4660 SDOperand ShAmt = LegalizeOp(Amt);
4661 MVT::ValueType ShTy = ShAmt.getValueType();
4662 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4663 unsigned NVTBits = MVT::getSizeInBits(NVT);
4664
Chris Lattner8c931452007-10-14 20:35:12 +00004665 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4667 unsigned Cst = CN->getValue();
4668 // Expand the incoming operand to be shifted, so that we have its parts
4669 SDOperand InL, InH;
4670 ExpandOp(Op, InL, InH);
4671 switch(Opc) {
4672 case ISD::SHL:
4673 if (Cst > VTBits) {
4674 Lo = DAG.getConstant(0, NVT);
4675 Hi = DAG.getConstant(0, NVT);
4676 } else if (Cst > NVTBits) {
4677 Lo = DAG.getConstant(0, NVT);
4678 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
4679 } else if (Cst == NVTBits) {
4680 Lo = DAG.getConstant(0, NVT);
4681 Hi = InL;
4682 } else {
4683 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4684 Hi = DAG.getNode(ISD::OR, NVT,
4685 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4686 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4687 }
4688 return true;
4689 case ISD::SRL:
4690 if (Cst > VTBits) {
4691 Lo = DAG.getConstant(0, NVT);
4692 Hi = DAG.getConstant(0, NVT);
4693 } else if (Cst > NVTBits) {
4694 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4695 Hi = DAG.getConstant(0, NVT);
4696 } else if (Cst == NVTBits) {
4697 Lo = InH;
4698 Hi = DAG.getConstant(0, NVT);
4699 } else {
4700 Lo = DAG.getNode(ISD::OR, NVT,
4701 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4702 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4703 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4704 }
4705 return true;
4706 case ISD::SRA:
4707 if (Cst > VTBits) {
4708 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
4709 DAG.getConstant(NVTBits-1, ShTy));
4710 } else if (Cst > NVTBits) {
4711 Lo = DAG.getNode(ISD::SRA, NVT, InH,
4712 DAG.getConstant(Cst-NVTBits, ShTy));
4713 Hi = DAG.getNode(ISD::SRA, NVT, InH,
4714 DAG.getConstant(NVTBits-1, ShTy));
4715 } else if (Cst == NVTBits) {
4716 Lo = InH;
4717 Hi = DAG.getNode(ISD::SRA, NVT, InH,
4718 DAG.getConstant(NVTBits-1, ShTy));
4719 } else {
4720 Lo = DAG.getNode(ISD::OR, NVT,
4721 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4722 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4723 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4724 }
4725 return true;
4726 }
4727 }
4728
4729 // Okay, the shift amount isn't constant. However, if we can tell that it is
4730 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4731 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4732 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4733
4734 // If we know that the high bit of the shift amount is one, then we can do
4735 // this as a couple of simple shifts.
4736 if (KnownOne & Mask) {
4737 // Mask out the high bit, which we know is set.
4738 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4739 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4740
4741 // Expand the incoming operand to be shifted, so that we have its parts
4742 SDOperand InL, InH;
4743 ExpandOp(Op, InL, InH);
4744 switch(Opc) {
4745 case ISD::SHL:
4746 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4747 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4748 return true;
4749 case ISD::SRL:
4750 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4751 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4752 return true;
4753 case ISD::SRA:
4754 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4755 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4756 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4757 return true;
4758 }
4759 }
4760
4761 // If we know that the high bit of the shift amount is zero, then we can do
4762 // this as a couple of simple shifts.
4763 if (KnownZero & Mask) {
4764 // Compute 32-amt.
4765 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4766 DAG.getConstant(NVTBits, Amt.getValueType()),
4767 Amt);
4768
4769 // Expand the incoming operand to be shifted, so that we have its parts
4770 SDOperand InL, InH;
4771 ExpandOp(Op, InL, InH);
4772 switch(Opc) {
4773 case ISD::SHL:
4774 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4775 Hi = DAG.getNode(ISD::OR, NVT,
4776 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4777 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4778 return true;
4779 case ISD::SRL:
4780 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4781 Lo = DAG.getNode(ISD::OR, NVT,
4782 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4783 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4784 return true;
4785 case ISD::SRA:
4786 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4787 Lo = DAG.getNode(ISD::OR, NVT,
4788 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4789 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4790 return true;
4791 }
4792 }
4793
4794 return false;
4795}
4796
4797
4798// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4799// does not fit into a register, return the lo part and set the hi part to the
4800// by-reg argument. If it does fit into a single register, return the result
4801// and leave the Hi part unset.
4802SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
4803 bool isSigned, SDOperand &Hi) {
4804 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4805 // The input chain to this libcall is the entry node of the function.
4806 // Legalizing the call will automatically add the previous call to the
4807 // dependence.
4808 SDOperand InChain = DAG.getEntryNode();
4809
4810 TargetLowering::ArgListTy Args;
4811 TargetLowering::ArgListEntry Entry;
4812 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4813 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4814 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
4815 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4816 Entry.isSExt = isSigned;
4817 Args.push_back(Entry);
4818 }
4819 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
4820
4821 // Splice the libcall in wherever FindInputOutputChains tells us to.
4822 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
4823 std::pair<SDOperand,SDOperand> CallInfo =
4824 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
4825 Callee, Args, DAG);
4826
4827 // Legalize the call sequence, starting with the chain. This will advance
4828 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4829 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4830 LegalizeOp(CallInfo.second);
4831 SDOperand Result;
4832 switch (getTypeAction(CallInfo.first.getValueType())) {
4833 default: assert(0 && "Unknown thing");
4834 case Legal:
4835 Result = CallInfo.first;
4836 break;
4837 case Expand:
4838 ExpandOp(CallInfo.first, Result, Hi);
4839 break;
4840 }
4841 return Result;
4842}
4843
4844
4845/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4846///
4847SDOperand SelectionDAGLegalize::
4848ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
4849 assert(getTypeAction(Source.getValueType()) == Expand &&
4850 "This is not an expansion!");
4851 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4852
4853 if (!isSigned) {
4854 assert(Source.getValueType() == MVT::i64 &&
4855 "This only works for 64-bit -> FP");
4856 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4857 // incoming integer is set. To handle this, we dynamically test to see if
4858 // it is set, and, if so, add a fudge factor.
4859 SDOperand Lo, Hi;
4860 ExpandOp(Source, Lo, Hi);
4861
4862 // If this is unsigned, and not supported, first perform the conversion to
4863 // signed, then adjust the result if the sign bit is set.
4864 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4865 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4866
4867 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4868 DAG.getConstant(0, Hi.getValueType()),
4869 ISD::SETLT);
4870 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4871 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4872 SignSet, Four, Zero);
4873 uint64_t FF = 0x5f800000ULL;
4874 if (TLI.isLittleEndian()) FF <<= 32;
4875 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
4876
4877 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4878 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4879 SDOperand FudgeInReg;
4880 if (DestTy == MVT::f32)
4881 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00004882 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00004884 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen2fc20782007-09-14 22:26:36 +00004885 CPIdx, NULL, 0, MVT::f32);
4886 else
4887 assert(0 && "Unexpected conversion");
4888
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 MVT::ValueType SCVT = SignedConv.getValueType();
4890 if (SCVT != DestTy) {
4891 // Destination type needs to be expanded as well. The FADD now we are
4892 // constructing will be expanded into a libcall.
4893 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4894 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4895 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4896 SignedConv, SignedConv.getValue(1));
4897 }
4898 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4899 }
4900 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
4901 }
4902
4903 // Check to see if the target has a custom way to lower this. If so, use it.
4904 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4905 default: assert(0 && "This action not implemented for this operation!");
4906 case TargetLowering::Legal:
4907 case TargetLowering::Expand:
4908 break; // This case is handled below.
4909 case TargetLowering::Custom: {
4910 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4911 Source), DAG);
4912 if (NV.Val)
4913 return LegalizeOp(NV);
4914 break; // The target decided this was legal after all
4915 }
4916 }
4917
4918 // Expand the source, then glue it back together for the call. We must expand
4919 // the source in case it is shared (this pass of legalize must traverse it).
4920 SDOperand SrcLo, SrcHi;
4921 ExpandOp(Source, SrcLo, SrcHi);
4922 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4923
4924 RTLIB::Libcall LC;
4925 if (DestTy == MVT::f32)
4926 LC = RTLIB::SINTTOFP_I64_F32;
4927 else {
4928 assert(DestTy == MVT::f64 && "Unknown fp value type!");
4929 LC = RTLIB::SINTTOFP_I64_F64;
4930 }
4931
4932 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
4933 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4934 SDOperand UnusedHiPart;
4935 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4936 UnusedHiPart);
4937}
4938
4939/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4940/// INT_TO_FP operation of the specified operand when the target requests that
4941/// we expand it. At this point, we know that the result and operand types are
4942/// legal for the target.
4943SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4944 SDOperand Op0,
4945 MVT::ValueType DestVT) {
4946 if (Op0.getValueType() == MVT::i32) {
4947 // simple 32-bit [signed|unsigned] integer to float/double expansion
4948
4949 // get the stack frame index of a 8 byte buffer, pessimistically aligned
4950 MachineFunction &MF = DAG.getMachineFunction();
4951 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4952 unsigned StackAlign =
4953 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
4954 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
4955 // get address of 8 byte buffer
4956 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4957 // word offset constant for Hi/Lo address computation
4958 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4959 // set up Hi and Lo (into buffer) address based on endian
4960 SDOperand Hi = StackSlot;
4961 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4962 if (TLI.isLittleEndian())
4963 std::swap(Hi, Lo);
4964
4965 // if signed map to unsigned space
4966 SDOperand Op0Mapped;
4967 if (isSigned) {
4968 // constant used to invert sign bit (signed to unsigned mapping)
4969 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4970 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4971 } else {
4972 Op0Mapped = Op0;
4973 }
4974 // store the lo of the constructed double - based on integer input
4975 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
4976 Op0Mapped, Lo, NULL, 0);
4977 // initial hi portion of constructed double
4978 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4979 // store the hi of the constructed double - biased exponent
4980 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
4981 // load the constructed double
4982 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
4983 // FP constant to bias correct the final result
4984 SDOperand Bias = DAG.getConstantFP(isSigned ?
4985 BitsToDouble(0x4330000080000000ULL)
4986 : BitsToDouble(0x4330000000000000ULL),
4987 MVT::f64);
4988 // subtract the bias
4989 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4990 // final result
4991 SDOperand Result;
4992 // handle final rounding
4993 if (DestVT == MVT::f64) {
4994 // do nothing
4995 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00004996 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4997 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4998 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4999 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 }
5001 return Result;
5002 }
5003 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5004 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5005
5006 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5007 DAG.getConstant(0, Op0.getValueType()),
5008 ISD::SETLT);
5009 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5010 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5011 SignSet, Four, Zero);
5012
5013 // If the sign bit of the integer is set, the large number will be treated
5014 // as a negative number. To counteract this, the dynamic code adds an
5015 // offset depending on the data type.
5016 uint64_t FF;
5017 switch (Op0.getValueType()) {
5018 default: assert(0 && "Unsupported integer type!");
5019 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5020 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5021 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5022 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5023 }
5024 if (TLI.isLittleEndian()) FF <<= 32;
5025 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5026
5027 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5028 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5029 SDOperand FudgeInReg;
5030 if (DestVT == MVT::f32)
5031 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
5032 else {
Dale Johannesen958b08b2007-09-19 23:55:34 +00005033 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 DAG.getEntryNode(), CPIdx,
5035 NULL, 0, MVT::f32));
5036 }
5037
5038 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5039}
5040
5041/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5042/// *INT_TO_FP operation of the specified operand when the target requests that
5043/// we promote it. At this point, we know that the result and operand types are
5044/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5045/// operation that takes a larger input.
5046SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5047 MVT::ValueType DestVT,
5048 bool isSigned) {
5049 // First step, figure out the appropriate *INT_TO_FP operation to use.
5050 MVT::ValueType NewInTy = LegalOp.getValueType();
5051
5052 unsigned OpToUse = 0;
5053
5054 // Scan for the appropriate larger type to use.
5055 while (1) {
5056 NewInTy = (MVT::ValueType)(NewInTy+1);
5057 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5058
5059 // If the target supports SINT_TO_FP of this type, use it.
5060 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5061 default: break;
5062 case TargetLowering::Legal:
5063 if (!TLI.isTypeLegal(NewInTy))
5064 break; // Can't use this datatype.
5065 // FALL THROUGH.
5066 case TargetLowering::Custom:
5067 OpToUse = ISD::SINT_TO_FP;
5068 break;
5069 }
5070 if (OpToUse) break;
5071 if (isSigned) continue;
5072
5073 // If the target supports UINT_TO_FP of this type, use it.
5074 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5075 default: break;
5076 case TargetLowering::Legal:
5077 if (!TLI.isTypeLegal(NewInTy))
5078 break; // Can't use this datatype.
5079 // FALL THROUGH.
5080 case TargetLowering::Custom:
5081 OpToUse = ISD::UINT_TO_FP;
5082 break;
5083 }
5084 if (OpToUse) break;
5085
5086 // Otherwise, try a larger type.
5087 }
5088
5089 // Okay, we found the operation and type to use. Zero extend our input to the
5090 // desired type then run the operation on it.
5091 return DAG.getNode(OpToUse, DestVT,
5092 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5093 NewInTy, LegalOp));
5094}
5095
5096/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5097/// FP_TO_*INT operation of the specified operand when the target requests that
5098/// we promote it. At this point, we know that the result and operand types are
5099/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5100/// operation that returns a larger result.
5101SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5102 MVT::ValueType DestVT,
5103 bool isSigned) {
5104 // First step, figure out the appropriate FP_TO*INT operation to use.
5105 MVT::ValueType NewOutTy = DestVT;
5106
5107 unsigned OpToUse = 0;
5108
5109 // Scan for the appropriate larger type to use.
5110 while (1) {
5111 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5112 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5113
5114 // If the target supports FP_TO_SINT returning this type, use it.
5115 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5116 default: break;
5117 case TargetLowering::Legal:
5118 if (!TLI.isTypeLegal(NewOutTy))
5119 break; // Can't use this datatype.
5120 // FALL THROUGH.
5121 case TargetLowering::Custom:
5122 OpToUse = ISD::FP_TO_SINT;
5123 break;
5124 }
5125 if (OpToUse) break;
5126
5127 // If the target supports FP_TO_UINT of this type, use it.
5128 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5129 default: break;
5130 case TargetLowering::Legal:
5131 if (!TLI.isTypeLegal(NewOutTy))
5132 break; // Can't use this datatype.
5133 // FALL THROUGH.
5134 case TargetLowering::Custom:
5135 OpToUse = ISD::FP_TO_UINT;
5136 break;
5137 }
5138 if (OpToUse) break;
5139
5140 // Otherwise, try a larger type.
5141 }
5142
5143 // Okay, we found the operation and type to use. Truncate the result of the
5144 // extended FP_TO_*INT operation to the desired size.
5145 return DAG.getNode(ISD::TRUNCATE, DestVT,
5146 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5147}
5148
5149/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5150///
5151SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5152 MVT::ValueType VT = Op.getValueType();
5153 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5154 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5155 switch (VT) {
5156 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5157 case MVT::i16:
5158 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5159 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5160 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5161 case MVT::i32:
5162 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5163 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5164 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5165 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5166 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5167 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5168 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5169 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5170 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5171 case MVT::i64:
5172 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5173 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5174 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5175 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5176 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5177 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5178 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5179 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5180 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5181 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5182 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5183 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5184 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5185 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5186 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5187 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5188 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5189 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5190 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5191 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5192 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5193 }
5194}
5195
5196/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5197///
5198SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5199 switch (Opc) {
5200 default: assert(0 && "Cannot expand this yet!");
5201 case ISD::CTPOP: {
5202 static const uint64_t mask[6] = {
5203 0x5555555555555555ULL, 0x3333333333333333ULL,
5204 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5205 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5206 };
5207 MVT::ValueType VT = Op.getValueType();
5208 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5209 unsigned len = MVT::getSizeInBits(VT);
5210 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5211 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5212 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5213 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5214 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5215 DAG.getNode(ISD::AND, VT,
5216 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5217 }
5218 return Op;
5219 }
5220 case ISD::CTLZ: {
5221 // for now, we do this:
5222 // x = x | (x >> 1);
5223 // x = x | (x >> 2);
5224 // ...
5225 // x = x | (x >>16);
5226 // x = x | (x >>32); // for 64-bit input
5227 // return popcount(~x);
5228 //
5229 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5230 MVT::ValueType VT = Op.getValueType();
5231 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5232 unsigned len = MVT::getSizeInBits(VT);
5233 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5234 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5235 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5236 }
5237 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5238 return DAG.getNode(ISD::CTPOP, VT, Op);
5239 }
5240 case ISD::CTTZ: {
5241 // for now, we use: { return popcount(~x & (x - 1)); }
5242 // unless the target has ctlz but not ctpop, in which case we use:
5243 // { return 32 - nlz(~x & (x-1)); }
5244 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5245 MVT::ValueType VT = Op.getValueType();
5246 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5247 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5248 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5249 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5250 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5251 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5252 TLI.isOperationLegal(ISD::CTLZ, VT))
5253 return DAG.getNode(ISD::SUB, VT,
5254 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5255 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5256 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5257 }
5258 }
5259}
5260
5261/// ExpandOp - Expand the specified SDOperand into its two component pieces
5262/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5263/// LegalizeNodes map is filled in for any results that are not expanded, the
5264/// ExpandedNodes map is filled in for any results that are expanded, and the
5265/// Lo/Hi values are returned.
5266void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5267 MVT::ValueType VT = Op.getValueType();
5268 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5269 SDNode *Node = Op.Val;
5270 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5271 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5272 MVT::isVector(VT)) &&
5273 "Cannot expand to FP value or to larger int value!");
5274
5275 // See if we already expanded it.
5276 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5277 = ExpandedNodes.find(Op);
5278 if (I != ExpandedNodes.end()) {
5279 Lo = I->second.first;
5280 Hi = I->second.second;
5281 return;
5282 }
5283
5284 switch (Node->getOpcode()) {
5285 case ISD::CopyFromReg:
5286 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005287 case ISD::FP_ROUND_INREG:
5288 if (VT == MVT::ppcf128 &&
5289 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5290 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005291 SDOperand SrcLo, SrcHi, Src;
5292 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5293 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5294 SDOperand Result = TLI.LowerOperation(
5295 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005296 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5297 Lo = Result.Val->getOperand(0);
5298 Hi = Result.Val->getOperand(1);
5299 break;
5300 }
5301 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 default:
5303#ifndef NDEBUG
5304 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5305#endif
5306 assert(0 && "Do not know how to expand this operator!");
5307 abort();
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005308 case ISD::EXTRACT_VECTOR_ELT:
5309 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5310 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5311 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5312 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005313 case ISD::UNDEF:
5314 NVT = TLI.getTypeToExpandTo(VT);
5315 Lo = DAG.getNode(ISD::UNDEF, NVT);
5316 Hi = DAG.getNode(ISD::UNDEF, NVT);
5317 break;
5318 case ISD::Constant: {
5319 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5320 Lo = DAG.getConstant(Cst, NVT);
5321 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5322 break;
5323 }
5324 case ISD::ConstantFP: {
5325 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005326 if (CFP->getValueType(0) == MVT::ppcf128) {
5327 APInt api = CFP->getValueAPF().convertToAPInt();
5328 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5329 MVT::f64);
5330 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5331 MVT::f64);
5332 break;
5333 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5335 if (getTypeAction(Lo.getValueType()) == Expand)
5336 ExpandOp(Lo, Lo, Hi);
5337 break;
5338 }
5339 case ISD::BUILD_PAIR:
5340 // Return the operands.
5341 Lo = Node->getOperand(0);
5342 Hi = Node->getOperand(1);
5343 break;
5344
5345 case ISD::SIGN_EXTEND_INREG:
5346 ExpandOp(Node->getOperand(0), Lo, Hi);
5347 // sext_inreg the low part if needed.
5348 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5349
5350 // The high part gets the sign extension from the lo-part. This handles
5351 // things like sextinreg V:i64 from i8.
5352 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5353 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5354 TLI.getShiftAmountTy()));
5355 break;
5356
5357 case ISD::BSWAP: {
5358 ExpandOp(Node->getOperand(0), Lo, Hi);
5359 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5360 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5361 Lo = TempLo;
5362 break;
5363 }
5364
5365 case ISD::CTPOP:
5366 ExpandOp(Node->getOperand(0), Lo, Hi);
5367 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5368 DAG.getNode(ISD::CTPOP, NVT, Lo),
5369 DAG.getNode(ISD::CTPOP, NVT, Hi));
5370 Hi = DAG.getConstant(0, NVT);
5371 break;
5372
5373 case ISD::CTLZ: {
5374 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5375 ExpandOp(Node->getOperand(0), Lo, Hi);
5376 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5377 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5378 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5379 ISD::SETNE);
5380 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5381 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5382
5383 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5384 Hi = DAG.getConstant(0, NVT);
5385 break;
5386 }
5387
5388 case ISD::CTTZ: {
5389 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5390 ExpandOp(Node->getOperand(0), Lo, Hi);
5391 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5392 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5393 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5394 ISD::SETNE);
5395 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5396 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5397
5398 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5399 Hi = DAG.getConstant(0, NVT);
5400 break;
5401 }
5402
5403 case ISD::VAARG: {
5404 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5405 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5406 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5407 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5408
5409 // Remember that we legalized the chain.
5410 Hi = LegalizeOp(Hi);
5411 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5412 if (!TLI.isLittleEndian())
5413 std::swap(Lo, Hi);
5414 break;
5415 }
5416
5417 case ISD::LOAD: {
5418 LoadSDNode *LD = cast<LoadSDNode>(Node);
5419 SDOperand Ch = LD->getChain(); // Legalize the chain.
5420 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5421 ISD::LoadExtType ExtType = LD->getExtensionType();
5422 int SVOffset = LD->getSrcValueOffset();
5423 unsigned Alignment = LD->getAlignment();
5424 bool isVolatile = LD->isVolatile();
5425
5426 if (ExtType == ISD::NON_EXTLOAD) {
5427 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5428 isVolatile, Alignment);
5429 if (VT == MVT::f32 || VT == MVT::f64) {
5430 // f32->i32 or f64->i64 one to one expansion.
5431 // Remember that we legalized the chain.
5432 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5433 // Recursively expand the new load.
5434 if (getTypeAction(NVT) == Expand)
5435 ExpandOp(Lo, Lo, Hi);
5436 break;
5437 }
5438
5439 // Increment the pointer to the other half.
5440 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5441 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5442 getIntPtrConstant(IncrementSize));
5443 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005444 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005445 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5446 isVolatile, Alignment);
5447
5448 // Build a factor node to remember that this load is independent of the
5449 // other one.
5450 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5451 Hi.getValue(1));
5452
5453 // Remember that we legalized the chain.
5454 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5455 if (!TLI.isLittleEndian())
5456 std::swap(Lo, Hi);
5457 } else {
5458 MVT::ValueType EVT = LD->getLoadedVT();
5459
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005460 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5461 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005462 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5463 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5464 SVOffset, isVolatile, Alignment);
5465 // Remember that we legalized the chain.
5466 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5467 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5468 break;
5469 }
5470
5471 if (EVT == NVT)
5472 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5473 SVOffset, isVolatile, Alignment);
5474 else
5475 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5476 SVOffset, EVT, isVolatile,
5477 Alignment);
5478
5479 // Remember that we legalized the chain.
5480 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5481
5482 if (ExtType == ISD::SEXTLOAD) {
5483 // The high part is obtained by SRA'ing all but one of the bits of the
5484 // lo part.
5485 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5486 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5487 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5488 } else if (ExtType == ISD::ZEXTLOAD) {
5489 // The high part is just a zero.
5490 Hi = DAG.getConstant(0, NVT);
5491 } else /* if (ExtType == ISD::EXTLOAD) */ {
5492 // The high part is undefined.
5493 Hi = DAG.getNode(ISD::UNDEF, NVT);
5494 }
5495 }
5496 break;
5497 }
5498 case ISD::AND:
5499 case ISD::OR:
5500 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5501 SDOperand LL, LH, RL, RH;
5502 ExpandOp(Node->getOperand(0), LL, LH);
5503 ExpandOp(Node->getOperand(1), RL, RH);
5504 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5505 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5506 break;
5507 }
5508 case ISD::SELECT: {
5509 SDOperand LL, LH, RL, RH;
5510 ExpandOp(Node->getOperand(1), LL, LH);
5511 ExpandOp(Node->getOperand(2), RL, RH);
5512 if (getTypeAction(NVT) == Expand)
5513 NVT = TLI.getTypeToExpandTo(NVT);
5514 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5515 if (VT != MVT::f32)
5516 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5517 break;
5518 }
5519 case ISD::SELECT_CC: {
5520 SDOperand TL, TH, FL, FH;
5521 ExpandOp(Node->getOperand(2), TL, TH);
5522 ExpandOp(Node->getOperand(3), FL, FH);
5523 if (getTypeAction(NVT) == Expand)
5524 NVT = TLI.getTypeToExpandTo(NVT);
5525 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5526 Node->getOperand(1), TL, FL, Node->getOperand(4));
5527 if (VT != MVT::f32)
5528 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5529 Node->getOperand(1), TH, FH, Node->getOperand(4));
5530 break;
5531 }
5532 case ISD::ANY_EXTEND:
5533 // The low part is any extension of the input (which degenerates to a copy).
5534 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5535 // The high part is undefined.
5536 Hi = DAG.getNode(ISD::UNDEF, NVT);
5537 break;
5538 case ISD::SIGN_EXTEND: {
5539 // The low part is just a sign extension of the input (which degenerates to
5540 // a copy).
5541 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
5542
5543 // The high part is obtained by SRA'ing all but one of the bits of the lo
5544 // part.
5545 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5546 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5547 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5548 break;
5549 }
5550 case ISD::ZERO_EXTEND:
5551 // The low part is just a zero extension of the input (which degenerates to
5552 // a copy).
5553 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
5554
5555 // The high part is just a zero.
5556 Hi = DAG.getConstant(0, NVT);
5557 break;
5558
5559 case ISD::TRUNCATE: {
5560 // The input value must be larger than this value. Expand *it*.
5561 SDOperand NewLo;
5562 ExpandOp(Node->getOperand(0), NewLo, Hi);
5563
5564 // The low part is now either the right size, or it is closer. If not the
5565 // right size, make an illegal truncate so we recursively expand it.
5566 if (NewLo.getValueType() != Node->getValueType(0))
5567 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5568 ExpandOp(NewLo, Lo, Hi);
5569 break;
5570 }
5571
5572 case ISD::BIT_CONVERT: {
5573 SDOperand Tmp;
5574 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5575 // If the target wants to, allow it to lower this itself.
5576 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5577 case Expand: assert(0 && "cannot expand FP!");
5578 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5579 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5580 }
5581 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5582 }
5583
5584 // f32 / f64 must be expanded to i32 / i64.
5585 if (VT == MVT::f32 || VT == MVT::f64) {
5586 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5587 if (getTypeAction(NVT) == Expand)
5588 ExpandOp(Lo, Lo, Hi);
5589 break;
5590 }
5591
5592 // If source operand will be expanded to the same type as VT, i.e.
5593 // i64 <- f64, i32 <- f32, expand the source operand instead.
5594 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5595 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5596 ExpandOp(Node->getOperand(0), Lo, Hi);
5597 break;
5598 }
5599
5600 // Turn this into a load/store pair by default.
5601 if (Tmp.Val == 0)
5602 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
5603
5604 ExpandOp(Tmp, Lo, Hi);
5605 break;
5606 }
5607
5608 case ISD::READCYCLECOUNTER:
5609 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5610 TargetLowering::Custom &&
5611 "Must custom expand ReadCycleCounter");
5612 Lo = TLI.LowerOperation(Op, DAG);
5613 assert(Lo.Val && "Node must be custom expanded!");
5614 Hi = Lo.getValue(1);
5615 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
5616 LegalizeOp(Lo.getValue(2)));
5617 break;
5618
5619 // These operators cannot be expanded directly, emit them as calls to
5620 // library functions.
5621 case ISD::FP_TO_SINT: {
5622 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
5623 SDOperand Op;
5624 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5625 case Expand: assert(0 && "cannot expand FP!");
5626 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5627 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5628 }
5629
5630 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5631
5632 // Now that the custom expander is done, expand the result, which is still
5633 // VT.
5634 if (Op.Val) {
5635 ExpandOp(Op, Lo, Hi);
5636 break;
5637 }
5638 }
5639
Dale Johannesenac77b272007-10-05 20:04:43 +00005640 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641 if (Node->getOperand(0).getValueType() == MVT::f32)
5642 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00005643 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00005645 else if (Node->getOperand(0).getValueType() == MVT::f80)
5646 LC = RTLIB::FPTOSINT_F80_I64;
5647 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5648 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5650 false/*sign irrelevant*/, Hi);
5651 break;
5652 }
5653
5654 case ISD::FP_TO_UINT: {
5655 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
5656 SDOperand Op;
5657 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5658 case Expand: assert(0 && "cannot expand FP!");
5659 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5660 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5661 }
5662
5663 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5664
5665 // Now that the custom expander is done, expand the result.
5666 if (Op.Val) {
5667 ExpandOp(Op, Lo, Hi);
5668 break;
5669 }
5670 }
5671
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00005672 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005673 if (Node->getOperand(0).getValueType() == MVT::f32)
5674 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen4e1cf5d2007-09-28 18:44:17 +00005675 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005676 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00005677 else if (Node->getOperand(0).getValueType() == MVT::f80)
5678 LC = RTLIB::FPTOUINT_F80_I64;
5679 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5680 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5682 false/*sign irrelevant*/, Hi);
5683 break;
5684 }
5685
5686 case ISD::SHL: {
5687 // If the target wants custom lowering, do so.
5688 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5689 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
5690 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
5691 Op = TLI.LowerOperation(Op, DAG);
5692 if (Op.Val) {
5693 // Now that the custom expander is done, expand the result, which is
5694 // still VT.
5695 ExpandOp(Op, Lo, Hi);
5696 break;
5697 }
5698 }
5699
5700 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5701 // this X << 1 as X+X.
5702 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5703 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5704 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5705 SDOperand LoOps[2], HiOps[3];
5706 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5707 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5708 LoOps[1] = LoOps[0];
5709 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5710
5711 HiOps[1] = HiOps[0];
5712 HiOps[2] = Lo.getValue(1);
5713 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5714 break;
5715 }
5716 }
5717
5718 // If we can emit an efficient shift operation, do so now.
5719 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
5720 break;
5721
5722 // If this target supports SHL_PARTS, use it.
5723 TargetLowering::LegalizeAction Action =
5724 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5725 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5726 Action == TargetLowering::Custom) {
5727 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5728 break;
5729 }
5730
5731 // Otherwise, emit a libcall.
5732 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5733 false/*left shift=unsigned*/, Hi);
5734 break;
5735 }
5736
5737 case ISD::SRA: {
5738 // If the target wants custom lowering, do so.
5739 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5740 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
5741 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
5742 Op = TLI.LowerOperation(Op, DAG);
5743 if (Op.Val) {
5744 // Now that the custom expander is done, expand the result, which is
5745 // still VT.
5746 ExpandOp(Op, Lo, Hi);
5747 break;
5748 }
5749 }
5750
5751 // If we can emit an efficient shift operation, do so now.
5752 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
5753 break;
5754
5755 // If this target supports SRA_PARTS, use it.
5756 TargetLowering::LegalizeAction Action =
5757 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5758 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5759 Action == TargetLowering::Custom) {
5760 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5761 break;
5762 }
5763
5764 // Otherwise, emit a libcall.
5765 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5766 true/*ashr is signed*/, Hi);
5767 break;
5768 }
5769
5770 case ISD::SRL: {
5771 // If the target wants custom lowering, do so.
5772 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5773 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
5774 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
5775 Op = TLI.LowerOperation(Op, DAG);
5776 if (Op.Val) {
5777 // Now that the custom expander is done, expand the result, which is
5778 // still VT.
5779 ExpandOp(Op, Lo, Hi);
5780 break;
5781 }
5782 }
5783
5784 // If we can emit an efficient shift operation, do so now.
5785 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
5786 break;
5787
5788 // If this target supports SRL_PARTS, use it.
5789 TargetLowering::LegalizeAction Action =
5790 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5791 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5792 Action == TargetLowering::Custom) {
5793 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5794 break;
5795 }
5796
5797 // Otherwise, emit a libcall.
5798 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5799 false/*lshr is unsigned*/, Hi);
5800 break;
5801 }
5802
5803 case ISD::ADD:
5804 case ISD::SUB: {
5805 // If the target wants to custom expand this, let them.
5806 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5807 TargetLowering::Custom) {
5808 Op = TLI.LowerOperation(Op, DAG);
5809 if (Op.Val) {
5810 ExpandOp(Op, Lo, Hi);
5811 break;
5812 }
5813 }
5814
5815 // Expand the subcomponents.
5816 SDOperand LHSL, LHSH, RHSL, RHSH;
5817 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5818 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5819 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5820 SDOperand LoOps[2], HiOps[3];
5821 LoOps[0] = LHSL;
5822 LoOps[1] = RHSL;
5823 HiOps[0] = LHSH;
5824 HiOps[1] = RHSH;
5825 if (Node->getOpcode() == ISD::ADD) {
5826 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5827 HiOps[2] = Lo.getValue(1);
5828 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5829 } else {
5830 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5831 HiOps[2] = Lo.getValue(1);
5832 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5833 }
5834 break;
5835 }
5836
5837 case ISD::ADDC:
5838 case ISD::SUBC: {
5839 // Expand the subcomponents.
5840 SDOperand LHSL, LHSH, RHSL, RHSH;
5841 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5842 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5843 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5844 SDOperand LoOps[2] = { LHSL, RHSL };
5845 SDOperand HiOps[3] = { LHSH, RHSH };
5846
5847 if (Node->getOpcode() == ISD::ADDC) {
5848 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5849 HiOps[2] = Lo.getValue(1);
5850 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5851 } else {
5852 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5853 HiOps[2] = Lo.getValue(1);
5854 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5855 }
5856 // Remember that we legalized the flag.
5857 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5858 break;
5859 }
5860 case ISD::ADDE:
5861 case ISD::SUBE: {
5862 // Expand the subcomponents.
5863 SDOperand LHSL, LHSH, RHSL, RHSH;
5864 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5865 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5866 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5867 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5868 SDOperand HiOps[3] = { LHSH, RHSH };
5869
5870 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5871 HiOps[2] = Lo.getValue(1);
5872 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5873
5874 // Remember that we legalized the flag.
5875 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5876 break;
5877 }
5878 case ISD::MUL: {
5879 // If the target wants to custom expand this, let them.
5880 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
5881 SDOperand New = TLI.LowerOperation(Op, DAG);
5882 if (New.Val) {
5883 ExpandOp(New, Lo, Hi);
5884 break;
5885 }
5886 }
5887
5888 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5889 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00005890 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5891 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5892 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005893 SDOperand LL, LH, RL, RH;
5894 ExpandOp(Node->getOperand(0), LL, LH);
5895 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman5a199552007-10-08 18:33:35 +00005896 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5897 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5898 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5899 // FIXME: generalize this to handle other bit sizes
5900 if (LHSSB == 32 && RHSSB == 32 &&
5901 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5902 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5903 // The inputs are both zero-extended.
5904 if (HasUMUL_LOHI) {
5905 // We can emit a umul_lohi.
5906 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5907 Hi = SDOperand(Lo.Val, 1);
5908 break;
5909 }
5910 if (HasMULHU) {
5911 // We can emit a mulhu+mul.
5912 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5913 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5914 break;
5915 }
Dan Gohman5a199552007-10-08 18:33:35 +00005916 }
5917 if (LHSSB > BitSize && RHSSB > BitSize) {
5918 // The input values are both sign-extended.
5919 if (HasSMUL_LOHI) {
5920 // We can emit a smul_lohi.
5921 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5922 Hi = SDOperand(Lo.Val, 1);
5923 break;
5924 }
5925 if (HasMULHS) {
5926 // We can emit a mulhs+mul.
5927 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5928 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5929 break;
5930 }
5931 }
5932 if (HasUMUL_LOHI) {
5933 // Lo,Hi = umul LHS, RHS.
5934 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5935 DAG.getVTList(NVT, NVT), LL, RL);
5936 Lo = UMulLOHI;
5937 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005938 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5939 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5940 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5941 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
5942 break;
5943 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00005944 if (HasMULHU) {
5945 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5946 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5947 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5948 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5949 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5950 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
5951 break;
5952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005953 }
5954
Dan Gohman5a199552007-10-08 18:33:35 +00005955 // If nothing else, we can make a libcall.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005956 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5957 false/*sign irrelevant*/, Hi);
5958 break;
5959 }
5960 case ISD::SDIV:
5961 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5962 break;
5963 case ISD::UDIV:
5964 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5965 break;
5966 case ISD::SREM:
5967 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5968 break;
5969 case ISD::UREM:
5970 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5971 break;
5972
5973 case ISD::FADD:
Dale Johannesenac77b272007-10-05 20:04:43 +00005974 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5975 VT == MVT::f64 ? RTLIB::ADD_F64 :
5976 VT == MVT::ppcf128 ?
5977 RTLIB::ADD_PPCF128 :
5978 RTLIB::UNKNOWN_LIBCALL),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979 Node, false, Hi);
5980 break;
5981 case ISD::FSUB:
Dale Johannesenac77b272007-10-05 20:04:43 +00005982 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
5983 VT == MVT::f64 ? RTLIB::SUB_F64 :
5984 VT == MVT::ppcf128 ?
5985 RTLIB::SUB_PPCF128 :
5986 RTLIB::UNKNOWN_LIBCALL),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005987 Node, false, Hi);
5988 break;
5989 case ISD::FMUL:
Dale Johannesenac77b272007-10-05 20:04:43 +00005990 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
5991 VT == MVT::f64 ? RTLIB::MUL_F64 :
5992 VT == MVT::ppcf128 ?
5993 RTLIB::MUL_PPCF128 :
5994 RTLIB::UNKNOWN_LIBCALL),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005995 Node, false, Hi);
5996 break;
5997 case ISD::FDIV:
Dale Johannesenac77b272007-10-05 20:04:43 +00005998 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
5999 VT == MVT::f64 ? RTLIB::DIV_F64 :
6000 VT == MVT::ppcf128 ?
6001 RTLIB::DIV_PPCF128 :
6002 RTLIB::UNKNOWN_LIBCALL),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006003 Node, false, Hi);
6004 break;
6005 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006006 if (VT == MVT::ppcf128) {
6007 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6008 Node->getOperand(0).getValueType()==MVT::f64);
6009 const uint64_t zero = 0;
6010 if (Node->getOperand(0).getValueType()==MVT::f32)
6011 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6012 else
6013 Hi = Node->getOperand(0);
6014 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6015 break;
6016 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006017 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
6018 break;
6019 case ISD::FP_ROUND:
6020 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
6021 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006022 case ISD::FPOWI:
Dale Johannesen0c81a522007-09-28 01:08:20 +00006023 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6024 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesenac77b272007-10-05 20:04:43 +00006025 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6026 (VT == MVT::ppcf128) ?
6027 RTLIB::POWI_PPCF128 :
6028 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006029 Node, false, Hi);
6030 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006031 case ISD::FSQRT:
6032 case ISD::FSIN:
6033 case ISD::FCOS: {
6034 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6035 switch(Node->getOpcode()) {
6036 case ISD::FSQRT:
Dale Johannesen0c81a522007-09-28 01:08:20 +00006037 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesenac77b272007-10-05 20:04:43 +00006038 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6039 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6040 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6041 RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006042 break;
6043 case ISD::FSIN:
6044 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6045 break;
6046 case ISD::FCOS:
6047 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6048 break;
6049 default: assert(0 && "Unreachable!");
6050 }
6051 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
6052 break;
6053 }
6054 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006055 if (VT == MVT::ppcf128) {
6056 SDOperand Tmp;
6057 ExpandOp(Node->getOperand(0), Lo, Tmp);
6058 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6059 // lo = hi==fabs(hi) ? lo : -lo;
6060 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6061 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6062 DAG.getCondCode(ISD::SETEQ));
6063 break;
6064 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006065 SDOperand Mask = (VT == MVT::f64)
6066 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6067 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6068 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6069 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6070 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6071 if (getTypeAction(NVT) == Expand)
6072 ExpandOp(Lo, Lo, Hi);
6073 break;
6074 }
6075 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006076 if (VT == MVT::ppcf128) {
6077 ExpandOp(Node->getOperand(0), Lo, Hi);
6078 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6079 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6080 break;
6081 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006082 SDOperand Mask = (VT == MVT::f64)
6083 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6084 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6085 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6086 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6087 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6088 if (getTypeAction(NVT) == Expand)
6089 ExpandOp(Lo, Lo, Hi);
6090 break;
6091 }
6092 case ISD::FCOPYSIGN: {
6093 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6094 if (getTypeAction(NVT) == Expand)
6095 ExpandOp(Lo, Lo, Hi);
6096 break;
6097 }
6098 case ISD::SINT_TO_FP:
6099 case ISD::UINT_TO_FP: {
6100 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6101 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006102 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006103 static uint64_t zero = 0;
6104 if (isSigned) {
6105 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6106 Node->getOperand(0)));
6107 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6108 } else {
6109 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6110 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6111 Node->getOperand(0)));
6112 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6113 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006114 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006115 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6116 DAG.getConstant(0, MVT::i32),
6117 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6118 DAG.getConstantFP(
6119 APFloat(APInt(128, 2, TwoE32)),
6120 MVT::ppcf128)),
6121 Hi,
6122 DAG.getCondCode(ISD::SETLT)),
6123 Lo, Hi);
6124 }
6125 break;
6126 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006127 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6128 // si64->ppcf128 done by libcall, below
6129 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6130 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6131 Lo, Hi);
6132 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6133 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6134 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6135 DAG.getConstant(0, MVT::i64),
6136 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6137 DAG.getConstantFP(
6138 APFloat(APInt(128, 2, TwoE64)),
6139 MVT::ppcf128)),
6140 Hi,
6141 DAG.getCondCode(ISD::SETLT)),
6142 Lo, Hi);
6143 break;
6144 }
Evan Cheng20186812007-09-27 07:35:39 +00006145 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006146 if (Node->getOperand(0).getValueType() == MVT::i64) {
6147 if (VT == MVT::f32)
6148 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006149 else if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006150 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006151 else if (VT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00006152 assert(isSigned);
Dale Johannesenac77b272007-10-05 20:04:43 +00006153 LC = RTLIB::SINTTOFP_I64_F80;
6154 }
6155 else if (VT == MVT::ppcf128) {
6156 assert(isSigned);
6157 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006158 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006159 } else {
6160 if (VT == MVT::f32)
6161 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
6162 else
6163 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
6164 }
6165
6166 // Promote the operand if needed.
6167 if (getTypeAction(SrcVT) == Promote) {
6168 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6169 Tmp = isSigned
6170 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6171 DAG.getValueType(SrcVT))
6172 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6173 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6174 }
6175
6176 const char *LibCall = TLI.getLibcallName(LC);
6177 if (LibCall)
6178 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6179 else {
6180 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6181 Node->getOperand(0));
6182 if (getTypeAction(Lo.getValueType()) == Expand)
6183 ExpandOp(Lo, Lo, Hi);
6184 }
6185 break;
6186 }
6187 }
6188
6189 // Make sure the resultant values have been legalized themselves, unless this
6190 // is a type that requires multi-step expansion.
6191 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6192 Lo = LegalizeOp(Lo);
6193 if (Hi.Val)
6194 // Don't legalize the high part if it is expanded to a single node.
6195 Hi = LegalizeOp(Hi);
6196 }
6197
6198 // Remember in a map if the values will be reused later.
6199 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6200 assert(isNew && "Value already expanded?!?");
6201}
6202
6203/// SplitVectorOp - Given an operand of vector type, break it down into
6204/// two smaller values, still of vector type.
6205void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6206 SDOperand &Hi) {
6207 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6208 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006209 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 assert(NumElements > 1 && "Cannot split a single element vector!");
6211 unsigned NewNumElts = NumElements/2;
Dan Gohmana0763d92007-09-24 15:54:53 +00006212 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006213 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
6214
6215 // See if we already split it.
6216 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6217 = SplitNodes.find(Op);
6218 if (I != SplitNodes.end()) {
6219 Lo = I->second.first;
6220 Hi = I->second.second;
6221 return;
6222 }
6223
6224 switch (Node->getOpcode()) {
6225 default:
6226#ifndef NDEBUG
6227 Node->dump(&DAG);
6228#endif
6229 assert(0 && "Unhandled operation in SplitVectorOp!");
6230 case ISD::BUILD_PAIR:
6231 Lo = Node->getOperand(0);
6232 Hi = Node->getOperand(1);
6233 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006234 case ISD::INSERT_VECTOR_ELT: {
6235 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6236 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6237 SDOperand ScalarOp = Node->getOperand(1);
6238 if (Index < NewNumElts)
6239 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp,
6240 DAG.getConstant(Index, TLI.getPointerTy()));
6241 else
6242 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp,
6243 DAG.getConstant(Index - NewNumElts, TLI.getPointerTy()));
6244 break;
6245 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006246 case ISD::BUILD_VECTOR: {
6247 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6248 Node->op_begin()+NewNumElts);
6249 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
6250
6251 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
6252 Node->op_end());
6253 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
6254 break;
6255 }
6256 case ISD::CONCAT_VECTORS: {
6257 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6258 if (NewNumSubvectors == 1) {
6259 Lo = Node->getOperand(0);
6260 Hi = Node->getOperand(1);
6261 } else {
6262 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6263 Node->op_begin()+NewNumSubvectors);
6264 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
6265
6266 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6267 Node->op_end());
6268 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
6269 }
6270 break;
6271 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006272 case ISD::SELECT: {
6273 SDOperand Cond = Node->getOperand(0);
6274
6275 SDOperand LL, LH, RL, RH;
6276 SplitVectorOp(Node->getOperand(1), LL, LH);
6277 SplitVectorOp(Node->getOperand(2), RL, RH);
6278
6279 if (MVT::isVector(Cond.getValueType())) {
6280 // Handle a vector merge.
6281 SDOperand CL, CH;
6282 SplitVectorOp(Cond, CL, CH);
6283 Lo = DAG.getNode(Node->getOpcode(), NewVT, CL, LL, RL);
6284 Hi = DAG.getNode(Node->getOpcode(), NewVT, CH, LH, RH);
6285 } else {
6286 // Handle a simple select with vector operands.
6287 Lo = DAG.getNode(Node->getOpcode(), NewVT, Cond, LL, RL);
6288 Hi = DAG.getNode(Node->getOpcode(), NewVT, Cond, LH, RH);
6289 }
6290 break;
6291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006292 case ISD::ADD:
6293 case ISD::SUB:
6294 case ISD::MUL:
6295 case ISD::FADD:
6296 case ISD::FSUB:
6297 case ISD::FMUL:
6298 case ISD::SDIV:
6299 case ISD::UDIV:
6300 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006301 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302 case ISD::AND:
6303 case ISD::OR:
6304 case ISD::XOR: {
6305 SDOperand LL, LH, RL, RH;
6306 SplitVectorOp(Node->getOperand(0), LL, LH);
6307 SplitVectorOp(Node->getOperand(1), RL, RH);
6308
6309 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
6310 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
6311 break;
6312 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006313 case ISD::FPOWI: {
6314 SDOperand L, H;
6315 SplitVectorOp(Node->getOperand(0), L, H);
6316
6317 Lo = DAG.getNode(Node->getOpcode(), NewVT, L, Node->getOperand(1));
6318 Hi = DAG.getNode(Node->getOpcode(), NewVT, H, Node->getOperand(1));
6319 break;
6320 }
6321 case ISD::CTTZ:
6322 case ISD::CTLZ:
6323 case ISD::CTPOP:
6324 case ISD::FNEG:
6325 case ISD::FABS:
6326 case ISD::FSQRT:
6327 case ISD::FSIN:
6328 case ISD::FCOS: {
6329 SDOperand L, H;
6330 SplitVectorOp(Node->getOperand(0), L, H);
6331
6332 Lo = DAG.getNode(Node->getOpcode(), NewVT, L);
6333 Hi = DAG.getNode(Node->getOpcode(), NewVT, H);
6334 break;
6335 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006336 case ISD::LOAD: {
6337 LoadSDNode *LD = cast<LoadSDNode>(Node);
6338 SDOperand Ch = LD->getChain();
6339 SDOperand Ptr = LD->getBasePtr();
6340 const Value *SV = LD->getSrcValue();
6341 int SVOffset = LD->getSrcValueOffset();
6342 unsigned Alignment = LD->getAlignment();
6343 bool isVolatile = LD->isVolatile();
6344
6345 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6346 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
6347 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6348 getIntPtrConstant(IncrementSize));
6349 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006350 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006351 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6352
6353 // Build a factor node to remember that this load is independent of the
6354 // other one.
6355 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6356 Hi.getValue(1));
6357
6358 // Remember that we legalized the chain.
6359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6360 break;
6361 }
6362 case ISD::BIT_CONVERT: {
6363 // We know the result is a vector. The input may be either a vector or a
6364 // scalar value.
6365 SDOperand InOp = Node->getOperand(0);
6366 if (!MVT::isVector(InOp.getValueType()) ||
6367 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6368 // The input is a scalar or single-element vector.
6369 // Lower to a store/load so that it can be split.
6370 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006371 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006372
6373 SDOperand St = DAG.getStore(DAG.getEntryNode(),
6374 InOp, Ptr, NULL, 0);
6375 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
6376 }
6377 // Split the vector and convert each of the pieces now.
6378 SplitVectorOp(InOp, Lo, Hi);
6379 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
6380 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
6381 break;
6382 }
6383 }
6384
6385 // Remember in a map if the values will be reused later.
6386 bool isNew =
6387 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6388 assert(isNew && "Value already split?!?");
6389}
6390
6391
6392/// ScalarizeVectorOp - Given an operand of single-element vector type
6393/// (e.g. v1f32), convert it into the equivalent operation that returns a
6394/// scalar (e.g. f32) value.
6395SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6396 assert(MVT::isVector(Op.getValueType()) &&
6397 "Bad ScalarizeVectorOp invocation!");
6398 SDNode *Node = Op.Val;
6399 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6400 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6401
6402 // See if we already scalarized it.
6403 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6404 if (I != ScalarizedNodes.end()) return I->second;
6405
6406 SDOperand Result;
6407 switch (Node->getOpcode()) {
6408 default:
6409#ifndef NDEBUG
6410 Node->dump(&DAG); cerr << "\n";
6411#endif
6412 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6413 case ISD::ADD:
6414 case ISD::FADD:
6415 case ISD::SUB:
6416 case ISD::FSUB:
6417 case ISD::MUL:
6418 case ISD::FMUL:
6419 case ISD::SDIV:
6420 case ISD::UDIV:
6421 case ISD::FDIV:
6422 case ISD::SREM:
6423 case ISD::UREM:
6424 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006425 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006426 case ISD::AND:
6427 case ISD::OR:
6428 case ISD::XOR:
6429 Result = DAG.getNode(Node->getOpcode(),
6430 NewVT,
6431 ScalarizeVectorOp(Node->getOperand(0)),
6432 ScalarizeVectorOp(Node->getOperand(1)));
6433 break;
6434 case ISD::FNEG:
6435 case ISD::FABS:
6436 case ISD::FSQRT:
6437 case ISD::FSIN:
6438 case ISD::FCOS:
6439 Result = DAG.getNode(Node->getOpcode(),
6440 NewVT,
6441 ScalarizeVectorOp(Node->getOperand(0)));
6442 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006443 case ISD::FPOWI:
6444 Result = DAG.getNode(Node->getOpcode(),
6445 NewVT,
6446 ScalarizeVectorOp(Node->getOperand(0)),
6447 Node->getOperand(1));
6448 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006449 case ISD::LOAD: {
6450 LoadSDNode *LD = cast<LoadSDNode>(Node);
6451 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6452 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6453
6454 const Value *SV = LD->getSrcValue();
6455 int SVOffset = LD->getSrcValueOffset();
6456 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6457 LD->isVolatile(), LD->getAlignment());
6458
6459 // Remember that we legalized the chain.
6460 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6461 break;
6462 }
6463 case ISD::BUILD_VECTOR:
6464 Result = Node->getOperand(0);
6465 break;
6466 case ISD::INSERT_VECTOR_ELT:
6467 // Returning the inserted scalar element.
6468 Result = Node->getOperand(1);
6469 break;
6470 case ISD::CONCAT_VECTORS:
6471 assert(Node->getOperand(0).getValueType() == NewVT &&
6472 "Concat of non-legal vectors not yet supported!");
6473 Result = Node->getOperand(0);
6474 break;
6475 case ISD::VECTOR_SHUFFLE: {
6476 // Figure out if the scalar is the LHS or RHS and return it.
6477 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6478 if (cast<ConstantSDNode>(EltNum)->getValue())
6479 Result = ScalarizeVectorOp(Node->getOperand(1));
6480 else
6481 Result = ScalarizeVectorOp(Node->getOperand(0));
6482 break;
6483 }
6484 case ISD::EXTRACT_SUBVECTOR:
6485 Result = Node->getOperand(0);
6486 assert(Result.getValueType() == NewVT);
6487 break;
6488 case ISD::BIT_CONVERT:
6489 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
6490 break;
6491 case ISD::SELECT:
6492 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
6493 ScalarizeVectorOp(Op.getOperand(1)),
6494 ScalarizeVectorOp(Op.getOperand(2)));
6495 break;
6496 }
6497
6498 if (TLI.isTypeLegal(NewVT))
6499 Result = LegalizeOp(Result);
6500 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6501 assert(isNew && "Value already scalarized?");
6502 return Result;
6503}
6504
6505
6506// SelectionDAG::Legalize - This is the entry point for the file.
6507//
6508void SelectionDAG::Legalize() {
6509 if (ViewLegalizeDAGs) viewGraph();
6510
6511 /// run - This is the main entry point to this class.
6512 ///
6513 SelectionDAGLegalize(*this).LegalizeDAG();
6514}
6515