blob: 09f937b921b5b7da60ea60b621f586b0f0ea34ba [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chenga448bc42007-08-16 23:50:06 +000019#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Target/TargetLowering.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/CallingConv.h"
25#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.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
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +0000202 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
203 MVT::ValueType DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
205 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
206 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
207 SDOperand LegalOp,
208 MVT::ValueType DestVT);
209 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
211 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
213
214 SDOperand ExpandBSWAP(SDOperand Op);
215 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
216 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
218 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
220
221 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
222 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
223
224 SDOperand getIntPtrConstant(uint64_t Val) {
225 return DAG.getConstant(Val, TLI.getPointerTy());
226 }
227};
228}
229
230/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
231/// specified mask and type. Targets can specify exactly which masks they
232/// support and the code generator is tasked with not creating illegal masks.
233///
234/// Note that this will also return true for shuffles that are promoted to a
235/// different type.
236SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
237 SDOperand Mask) const {
238 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
239 default: return 0;
240 case TargetLowering::Legal:
241 case TargetLowering::Custom:
242 break;
243 case TargetLowering::Promote: {
244 // If this is promoted to a different type, convert the shuffle mask and
245 // ask if it is legal in the promoted type!
246 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
247
248 // If we changed # elements, change the shuffle mask.
249 unsigned NumEltsGrowth =
250 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
251 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
252 if (NumEltsGrowth > 1) {
253 // Renumber the elements.
254 SmallVector<SDOperand, 8> Ops;
255 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
256 SDOperand InOp = Mask.getOperand(i);
257 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
258 if (InOp.getOpcode() == ISD::UNDEF)
259 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
260 else {
261 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
262 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
263 }
264 }
265 }
266 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
267 }
268 VT = NVT;
269 break;
270 }
271 }
272 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
273}
274
275SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
276 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
277 ValueTypeActions(TLI.getValueTypeActions()) {
278 assert(MVT::LAST_VALUETYPE <= 32 &&
279 "Too many value types for ValueTypeActions to hold!");
280}
281
282/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
283/// contains all of a nodes operands before it contains the node.
284static void ComputeTopDownOrdering(SelectionDAG &DAG,
285 SmallVector<SDNode*, 64> &Order) {
286
287 DenseMap<SDNode*, unsigned> Visited;
288 std::vector<SDNode*> Worklist;
289 Worklist.reserve(128);
290
291 // Compute ordering from all of the leaves in the graphs, those (like the
292 // entry node) that have no operands.
293 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
294 E = DAG.allnodes_end(); I != E; ++I) {
295 if (I->getNumOperands() == 0) {
296 Visited[I] = 0 - 1U;
297 Worklist.push_back(I);
298 }
299 }
300
301 while (!Worklist.empty()) {
302 SDNode *N = Worklist.back();
303 Worklist.pop_back();
304
305 if (++Visited[N] != N->getNumOperands())
306 continue; // Haven't visited all operands yet
307
308 Order.push_back(N);
309
310 // Now that we have N in, add anything that uses it if all of their operands
311 // are now done.
312 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
313 UI != E; ++UI)
314 Worklist.push_back(*UI);
315 }
316
317 assert(Order.size() == Visited.size() &&
318 Order.size() ==
319 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
320 "Error: DAG is cyclic!");
321}
322
323
324void SelectionDAGLegalize::LegalizeDAG() {
325 LastCALLSEQ_END = DAG.getEntryNode();
326 IsLegalizingCall = false;
327
328 // The legalize process is inherently a bottom-up recursive process (users
329 // legalize their uses before themselves). Given infinite stack space, we
330 // could just start legalizing on the root and traverse the whole graph. In
331 // practice however, this causes us to run out of stack space on large basic
332 // blocks. To avoid this problem, compute an ordering of the nodes where each
333 // node is only legalized after all of its operands are legalized.
334 SmallVector<SDNode*, 64> Order;
335 ComputeTopDownOrdering(DAG, Order);
336
337 for (unsigned i = 0, e = Order.size(); i != e; ++i)
338 HandleOp(SDOperand(Order[i], 0));
339
340 // Finally, it's possible the root changed. Get the new root.
341 SDOperand OldRoot = DAG.getRoot();
342 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
343 DAG.setRoot(LegalizedNodes[OldRoot]);
344
345 ExpandedNodes.clear();
346 LegalizedNodes.clear();
347 PromotedNodes.clear();
348 SplitNodes.clear();
349 ScalarizedNodes.clear();
350
351 // Remove dead nodes now.
352 DAG.RemoveDeadNodes();
353}
354
355
356/// FindCallEndFromCallStart - Given a chained node that is part of a call
357/// sequence, find the CALLSEQ_END node that terminates the call sequence.
358static SDNode *FindCallEndFromCallStart(SDNode *Node) {
359 if (Node->getOpcode() == ISD::CALLSEQ_END)
360 return Node;
361 if (Node->use_empty())
362 return 0; // No CallSeqEnd
363
364 // The chain is usually at the end.
365 SDOperand TheChain(Node, Node->getNumValues()-1);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Sometimes it's at the beginning.
368 TheChain = SDOperand(Node, 0);
369 if (TheChain.getValueType() != MVT::Other) {
370 // Otherwise, hunt for it.
371 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
372 if (Node->getValueType(i) == MVT::Other) {
373 TheChain = SDOperand(Node, i);
374 break;
375 }
376
377 // Otherwise, we walked into a node without a chain.
378 if (TheChain.getValueType() != MVT::Other)
379 return 0;
380 }
381 }
382
383 for (SDNode::use_iterator UI = Node->use_begin(),
384 E = Node->use_end(); UI != E; ++UI) {
385
386 // Make sure to only follow users of our token chain.
387 SDNode *User = *UI;
388 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
389 if (User->getOperand(i) == TheChain)
390 if (SDNode *Result = FindCallEndFromCallStart(User))
391 return Result;
392 }
393 return 0;
394}
395
396/// FindCallStartFromCallEnd - Given a chained node that is part of a call
397/// sequence, find the CALLSEQ_START node that initiates the call sequence.
398static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
399 assert(Node && "Didn't find callseq_start for a call??");
400 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
401
402 assert(Node->getOperand(0).getValueType() == MVT::Other &&
403 "Node doesn't have a token chain argument!");
404 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
405}
406
407/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
408/// see if any uses can reach Dest. If no dest operands can get to dest,
409/// legalize them, legalize ourself, and return false, otherwise, return true.
410///
411/// Keep track of the nodes we fine that actually do lead to Dest in
412/// NodesLeadingTo. This avoids retraversing them exponential number of times.
413///
414bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
415 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
416 if (N == Dest) return true; // N certainly leads to Dest :)
417
418 // If we've already processed this node and it does lead to Dest, there is no
419 // need to reprocess it.
420 if (NodesLeadingTo.count(N)) return true;
421
422 // If the first result of this node has been already legalized, then it cannot
423 // reach N.
424 switch (getTypeAction(N->getValueType(0))) {
425 case Legal:
426 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Promote:
429 if (PromotedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 case Expand:
432 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
433 break;
434 }
435
436 // Okay, this node has not already been legalized. Check and legalize all
437 // operands. If none lead to Dest, then we can legalize this node.
438 bool OperandsLeadToDest = false;
439 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
440 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
441 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
442
443 if (OperandsLeadToDest) {
444 NodesLeadingTo.insert(N);
445 return true;
446 }
447
448 // Okay, this node looks safe, legalize it and return false.
449 HandleOp(SDOperand(N, 0));
450 return false;
451}
452
453/// HandleOp - Legalize, Promote, or Expand the specified operand as
454/// appropriate for its type.
455void SelectionDAGLegalize::HandleOp(SDOperand Op) {
456 MVT::ValueType VT = Op.getValueType();
457 switch (getTypeAction(VT)) {
458 default: assert(0 && "Bad type action!");
459 case Legal: (void)LegalizeOp(Op); break;
460 case Promote: (void)PromoteOp(Op); break;
461 case Expand:
462 if (!MVT::isVector(VT)) {
463 // If this is an illegal scalar, expand it into its two component
464 // pieces.
465 SDOperand X, Y;
Chris Lattnerdad577b2007-08-25 01:00:22 +0000466 if (Op.getOpcode() == ISD::TargetConstant)
467 break; // Allow illegal target nodes.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 ExpandOp(Op, X, Y);
469 } else if (MVT::getVectorNumElements(VT) == 1) {
470 // If this is an illegal single element vector, convert it to a
471 // scalar operation.
472 (void)ScalarizeVectorOp(Op);
473 } else {
474 // Otherwise, this is an illegal multiple element vector.
475 // Split it in half and legalize both parts.
476 SDOperand X, Y;
477 SplitVectorOp(Op, X, Y);
478 }
479 break;
480 }
481}
482
483/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
484/// a load from the constant pool.
485static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
486 SelectionDAG &DAG, TargetLowering &TLI) {
487 bool Extend = false;
488
489 // If a FP immediate is precise when represented as a float and if the
490 // target can do an extending load from float to double, we put it into
491 // the constant pool as a float, even if it's is statically typed as a
492 // double.
493 MVT::ValueType VT = CFP->getValueType(0);
494 bool isDouble = VT == MVT::f64;
Dale Johannesenb17a7a22007-09-16 16:51:49 +0000495 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen2fc20782007-09-14 22:26:36 +0000496 CFP->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 if (!UseCP) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000498 if (VT!=MVT::f64 && VT!=MVT::f32)
499 assert(0 && "Invalid type expansion");
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000500 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
501 isDouble ? MVT::i64 : MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 }
503
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000504 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen2fc20782007-09-14 22:26:36 +0000506 // Do not try to be clever about long doubles (so far)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
508 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
509 VT = MVT::f32;
510 Extend = true;
511 }
512
513 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
514 if (Extend) {
515 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
516 CPIdx, NULL, 0, MVT::f32);
517 } else {
518 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
519 }
520}
521
522
523/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
524/// operations.
525static
526SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
527 SelectionDAG &DAG, TargetLowering &TLI) {
528 MVT::ValueType VT = Node->getValueType(0);
529 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
530 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
531 "fcopysign expansion only supported for f32 and f64");
532 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
533
534 // First get the sign bit of second operand.
535 SDOperand Mask1 = (SrcVT == MVT::f64)
536 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
537 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
538 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
539 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
540 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
541 // Shift right or sign-extend it if the two operands have different types.
542 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
543 if (SizeDiff > 0) {
544 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
545 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
546 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
547 } else if (SizeDiff < 0)
548 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
549
550 // Clear the sign bit of first operand.
551 SDOperand Mask2 = (VT == MVT::f64)
552 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
553 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
554 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
555 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
556 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
557
558 // Or the value with the sign bit.
559 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
560 return Result;
561}
562
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000563/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
564static
565SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
566 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000567 SDOperand Chain = ST->getChain();
568 SDOperand Ptr = ST->getBasePtr();
569 SDOperand Val = ST->getValue();
570 MVT::ValueType VT = Val.getValueType();
Dale Johannesen08275382007-09-08 19:29:23 +0000571 int Alignment = ST->getAlignment();
572 int SVOffset = ST->getSrcValueOffset();
573 if (MVT::isFloatingPoint(ST->getStoredVT())) {
574 // Expand to a bitconvert of the value to the integer type of the
575 // same size, then a (misaligned) int store.
576 MVT::ValueType intVT;
577 if (VT==MVT::f64)
578 intVT = MVT::i64;
579 else if (VT==MVT::f32)
580 intVT = MVT::i32;
581 else
582 assert(0 && "Unaligned load of unsupported floating point type");
583
584 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
585 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
586 SVOffset, ST->isVolatile(), Alignment);
587 }
588 assert(MVT::isInteger(ST->getStoredVT()) &&
589 "Unaligned store of unknown type.");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000590 // Get the half-size VT
591 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
592 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000593 int IncrementSize = NumBits / 8;
594
595 // Divide the stored value in two parts.
596 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
597 SDOperand Lo = Val;
598 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
599
600 // Store the two parts
601 SDOperand Store1, Store2;
602 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
603 ST->getSrcValue(), SVOffset, NewStoredVT,
604 ST->isVolatile(), Alignment);
605 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
606 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsa3691432007-10-28 12:59:45 +0000607 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000608 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
609 ST->getSrcValue(), SVOffset + IncrementSize,
610 NewStoredVT, ST->isVolatile(), Alignment);
611
612 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
613}
614
615/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
616static
617SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
618 TargetLowering &TLI) {
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000619 int SVOffset = LD->getSrcValueOffset();
620 SDOperand Chain = LD->getChain();
621 SDOperand Ptr = LD->getBasePtr();
622 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen08275382007-09-08 19:29:23 +0000623 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000624 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen08275382007-09-08 19:29:23 +0000625 // Expand to a (misaligned) integer load of the same size,
626 // then bitconvert to floating point.
627 MVT::ValueType intVT;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000628 if (LoadedVT == MVT::f64)
Dale Johannesen08275382007-09-08 19:29:23 +0000629 intVT = MVT::i64;
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000630 else if (LoadedVT == MVT::f32)
Dale Johannesen08275382007-09-08 19:29:23 +0000631 intVT = MVT::i32;
632 else
633 assert(0 && "Unaligned load of unsupported floating point type");
634
635 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
636 SVOffset, LD->isVolatile(),
637 LD->getAlignment());
638 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
639 if (LoadedVT != VT)
640 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
641
642 SDOperand Ops[] = { Result, Chain };
643 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
644 Ops, 2);
645 }
Chris Lattner4cf8a5b2007-11-19 21:38:03 +0000646 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
647 "Unaligned load of unsupported type.");
648
649 // Compute the new VT that is half the size of the old one. We either have an
650 // integer MVT or we have a vector MVT.
651 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
652 MVT::ValueType NewLoadedVT;
653 if (!MVT::isVector(LoadedVT)) {
654 NewLoadedVT = MVT::getIntegerType(NumBits/2);
655 } else {
656 // FIXME: This is not right for <1 x anything> it is also not right for
657 // non-power-of-two vectors.
658 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
659 MVT::getVectorNumElements(LoadedVT)/2);
660 }
661 NumBits >>= 1;
662
663 unsigned Alignment = LD->getAlignment();
664 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000665 ISD::LoadExtType HiExtType = LD->getExtensionType();
666
667 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
668 if (HiExtType == ISD::NON_EXTLOAD)
669 HiExtType = ISD::ZEXTLOAD;
670
671 // Load the value in two parts
672 SDOperand Lo, Hi;
673 if (TLI.isLittleEndian()) {
674 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
675 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
676 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
677 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
678 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
679 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000680 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000681 } else {
682 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
683 NewLoadedVT,LD->isVolatile(), Alignment);
684 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
685 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
686 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
687 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsa3691432007-10-28 12:59:45 +0000688 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +0000689 }
690
691 // aggregate the two parts
692 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
693 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
694 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
695
696 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
697 Hi.getValue(1));
698
699 SDOperand Ops[] = { Result, TF };
700 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
701}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702
Dan Gohman6d05cac2007-10-11 23:57:53 +0000703/// UnrollVectorOp - We know that the given vector has a legal type, however
704/// the operation it performs is not legal and is an operation that we have
705/// no way of lowering. "Unroll" the vector, splitting out the scalars and
706/// operating on each element individually.
707SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
708 MVT::ValueType VT = Op.getValueType();
709 assert(isTypeLegal(VT) &&
710 "Caller should expand or promote operands that are not legal!");
711 assert(Op.Val->getNumValues() == 1 &&
712 "Can't unroll a vector with multiple results!");
713 unsigned NE = MVT::getVectorNumElements(VT);
714 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
715
716 SmallVector<SDOperand, 8> Scalars;
717 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
718 for (unsigned i = 0; i != NE; ++i) {
719 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
720 SDOperand Operand = Op.getOperand(j);
721 MVT::ValueType OperandVT = Operand.getValueType();
722 if (MVT::isVector(OperandVT)) {
723 // A vector operand; extract a single element.
724 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
725 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
726 OperandEltVT,
727 Operand,
728 DAG.getConstant(i, MVT::i32));
729 } else {
730 // A scalar operand; just use it as is.
731 Operands[j] = Operand;
732 }
733 }
734 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
735 &Operands[0], Operands.size()));
736 }
737
738 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
739}
740
Duncan Sands37a3f472008-01-10 10:28:30 +0000741/// GetFPLibCall - Return the right libcall for the given floating point type.
742static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
743 RTLIB::Libcall Call_F32,
744 RTLIB::Libcall Call_F64,
745 RTLIB::Libcall Call_F80,
746 RTLIB::Libcall Call_PPCF128) {
747 return
748 VT == MVT::f32 ? Call_F32 :
749 VT == MVT::f64 ? Call_F64 :
750 VT == MVT::f80 ? Call_F80 :
751 VT == MVT::ppcf128 ? Call_PPCF128 :
752 RTLIB::UNKNOWN_LIBCALL;
753}
754
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755/// LegalizeOp - We know that the specified value has a legal type, and
756/// that its operands are legal. Now ensure that the operation itself
757/// is legal, recursively ensuring that the operands' operations remain
758/// legal.
759SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerdad577b2007-08-25 01:00:22 +0000760 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
761 return Op;
762
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 assert(isTypeLegal(Op.getValueType()) &&
764 "Caller should expand or promote operands that are not legal!");
765 SDNode *Node = Op.Val;
766
767 // If this operation defines any values that cannot be represented in a
768 // register on this target, make sure to expand or promote them.
769 if (Node->getNumValues() > 1) {
770 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
771 if (getTypeAction(Node->getValueType(i)) != Legal) {
772 HandleOp(Op.getValue(i));
773 assert(LegalizedNodes.count(Op) &&
774 "Handling didn't add legal operands!");
775 return LegalizedNodes[Op];
776 }
777 }
778
779 // Note that LegalizeOp may be reentered even from single-use nodes, which
780 // means that we always must cache transformed nodes.
781 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
782 if (I != LegalizedNodes.end()) return I->second;
783
784 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
785 SDOperand Result = Op;
786 bool isCustom = false;
787
788 switch (Node->getOpcode()) {
789 case ISD::FrameIndex:
790 case ISD::EntryToken:
791 case ISD::Register:
792 case ISD::BasicBlock:
793 case ISD::TargetFrameIndex:
794 case ISD::TargetJumpTable:
795 case ISD::TargetConstant:
796 case ISD::TargetConstantFP:
797 case ISD::TargetConstantPool:
798 case ISD::TargetGlobalAddress:
799 case ISD::TargetGlobalTLSAddress:
800 case ISD::TargetExternalSymbol:
801 case ISD::VALUETYPE:
802 case ISD::SRCVALUE:
803 case ISD::STRING:
804 case ISD::CONDCODE:
805 // Primitives must all be legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +0000806 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 "This must be legal!");
808 break;
809 default:
810 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
811 // If this is a target node, legalize it by legalizing the operands then
812 // passing it through.
813 SmallVector<SDOperand, 8> Ops;
814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
816
817 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
818
819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
820 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
821 return Result.getValue(Op.ResNo);
822 }
823 // Otherwise this is an unhandled builtin node. splat.
824#ifndef NDEBUG
825 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
826#endif
827 assert(0 && "Do not know how to legalize this operator!");
828 abort();
829 case ISD::GLOBAL_OFFSET_TABLE:
830 case ISD::GlobalAddress:
831 case ISD::GlobalTLSAddress:
832 case ISD::ExternalSymbol:
833 case ISD::ConstantPool:
834 case ISD::JumpTable: // Nothing to do.
835 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
836 default: assert(0 && "This action is not supported yet!");
837 case TargetLowering::Custom:
838 Tmp1 = TLI.LowerOperation(Op, DAG);
839 if (Tmp1.Val) Result = Tmp1;
840 // FALLTHROUGH if the target doesn't want to lower this op after all.
841 case TargetLowering::Legal:
842 break;
843 }
844 break;
845 case ISD::FRAMEADDR:
846 case ISD::RETURNADDR:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 // The only option for these nodes is to custom lower them. If the target
848 // does not custom lower them, then return zero.
849 Tmp1 = TLI.LowerOperation(Op, DAG);
850 if (Tmp1.Val)
851 Result = Tmp1;
852 else
853 Result = DAG.getConstant(0, TLI.getPointerTy());
854 break;
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000855 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000856 MVT::ValueType VT = Node->getValueType(0);
857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Custom:
860 Result = TLI.LowerOperation(Op, DAG);
861 if (Result.Val) break;
862 // Fall Thru
863 case TargetLowering::Legal:
864 Result = DAG.getConstant(0, VT);
865 break;
866 }
Anton Korobeynikove3d7f932007-08-29 23:18:48 +0000867 }
Anton Korobeynikov09386bd2007-08-29 19:28:29 +0000868 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 case ISD::EXCEPTIONADDR: {
870 Tmp1 = LegalizeOp(Node->getOperand(0));
871 MVT::ValueType VT = Node->getValueType(0);
872 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
873 default: assert(0 && "This action is not supported yet!");
874 case TargetLowering::Expand: {
875 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000876 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 }
878 break;
879 case TargetLowering::Custom:
880 Result = TLI.LowerOperation(Op, DAG);
881 if (Result.Val) break;
882 // Fall Thru
883 case TargetLowering::Legal: {
884 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
885 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000886 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 break;
888 }
889 }
890 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000891 if (Result.Val->getNumValues() == 1) break;
892
893 assert(Result.Val->getNumValues() == 2 &&
894 "Cannot return more than two values!");
895
896 // Since we produced two values, make sure to remember that we
897 // legalized both of them.
898 Tmp1 = LegalizeOp(Result);
899 Tmp2 = LegalizeOp(Result.getValue(1));
900 AddLegalizedOperand(Op.getValue(0), Tmp1);
901 AddLegalizedOperand(Op.getValue(1), Tmp2);
902 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 case ISD::EHSELECTION: {
904 Tmp1 = LegalizeOp(Node->getOperand(0));
905 Tmp2 = LegalizeOp(Node->getOperand(1));
906 MVT::ValueType VT = Node->getValueType(0);
907 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
908 default: assert(0 && "This action is not supported yet!");
909 case TargetLowering::Expand: {
910 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000911 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 }
913 break;
914 case TargetLowering::Custom:
915 Result = TLI.LowerOperation(Op, DAG);
916 if (Result.Val) break;
917 // Fall Thru
918 case TargetLowering::Legal: {
919 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
920 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000921 Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 break;
923 }
924 }
925 }
Duncan Sandsc7f7d5e2007-12-31 18:35:50 +0000926 if (Result.Val->getNumValues() == 1) break;
927
928 assert(Result.Val->getNumValues() == 2 &&
929 "Cannot return more than two values!");
930
931 // Since we produced two values, make sure to remember that we
932 // legalized both of them.
933 Tmp1 = LegalizeOp(Result);
934 Tmp2 = LegalizeOp(Result.getValue(1));
935 AddLegalizedOperand(Op.getValue(0), Tmp1);
936 AddLegalizedOperand(Op.getValue(1), Tmp2);
937 return Op.ResNo ? Tmp2 : Tmp1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 case ISD::EH_RETURN: {
939 MVT::ValueType VT = Node->getValueType(0);
940 // The only "good" option for this node is to custom lower it.
941 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
942 default: assert(0 && "This action is not supported at all!");
943 case TargetLowering::Custom:
944 Result = TLI.LowerOperation(Op, DAG);
945 if (Result.Val) break;
946 // Fall Thru
947 case TargetLowering::Legal:
948 // Target does not know, how to lower this, lower to noop
949 Result = LegalizeOp(Node->getOperand(0));
950 break;
951 }
952 }
953 break;
954 case ISD::AssertSext:
955 case ISD::AssertZext:
956 Tmp1 = LegalizeOp(Node->getOperand(0));
957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
958 break;
959 case ISD::MERGE_VALUES:
960 // Legalize eliminates MERGE_VALUES nodes.
961 Result = Node->getOperand(Op.ResNo);
962 break;
963 case ISD::CopyFromReg:
964 Tmp1 = LegalizeOp(Node->getOperand(0));
965 Result = Op.getValue(0);
966 if (Node->getNumValues() == 2) {
967 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
968 } else {
969 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
970 if (Node->getNumOperands() == 3) {
971 Tmp2 = LegalizeOp(Node->getOperand(2));
972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
973 } else {
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
975 }
976 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
977 }
978 // Since CopyFromReg produces two values, make sure to remember that we
979 // legalized both of them.
980 AddLegalizedOperand(Op.getValue(0), Result);
981 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
982 return Result.getValue(Op.ResNo);
983 case ISD::UNDEF: {
984 MVT::ValueType VT = Op.getValueType();
985 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
986 default: assert(0 && "This action is not supported yet!");
987 case TargetLowering::Expand:
988 if (MVT::isInteger(VT))
989 Result = DAG.getConstant(0, VT);
990 else if (MVT::isFloatingPoint(VT))
Dale Johannesen20b76352007-09-26 17:26:49 +0000991 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
992 VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 else
994 assert(0 && "Unknown value type!");
995 break;
996 case TargetLowering::Legal:
997 break;
998 }
999 break;
1000 }
1001
1002 case ISD::INTRINSIC_W_CHAIN:
1003 case ISD::INTRINSIC_WO_CHAIN:
1004 case ISD::INTRINSIC_VOID: {
1005 SmallVector<SDOperand, 8> Ops;
1006 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1007 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1008 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1009
1010 // Allow the target to custom lower its intrinsics if it wants to.
1011 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
1012 TargetLowering::Custom) {
1013 Tmp3 = TLI.LowerOperation(Result, DAG);
1014 if (Tmp3.Val) Result = Tmp3;
1015 }
1016
1017 if (Result.Val->getNumValues() == 1) break;
1018
1019 // Must have return value and chain result.
1020 assert(Result.Val->getNumValues() == 2 &&
1021 "Cannot return more than two values!");
1022
1023 // Since loads produce two values, make sure to remember that we
1024 // legalized both of them.
1025 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1026 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1027 return Result.getValue(Op.ResNo);
1028 }
1029
1030 case ISD::LOCATION:
1031 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1033
1034 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1035 case TargetLowering::Promote:
1036 default: assert(0 && "This action is not supported yet!");
1037 case TargetLowering::Expand: {
1038 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
1039 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
1040 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
1041
1042 if (MMI && (useDEBUG_LOC || useLABEL)) {
1043 const std::string &FName =
1044 cast<StringSDNode>(Node->getOperand(3))->getValue();
1045 const std::string &DirName =
1046 cast<StringSDNode>(Node->getOperand(4))->getValue();
1047 unsigned SrcFile = MMI->RecordSource(DirName, FName);
1048
1049 SmallVector<SDOperand, 8> Ops;
1050 Ops.push_back(Tmp1); // chain
1051 SDOperand LineOp = Node->getOperand(1);
1052 SDOperand ColOp = Node->getOperand(2);
1053
1054 if (useDEBUG_LOC) {
1055 Ops.push_back(LineOp); // line #
1056 Ops.push_back(ColOp); // col #
1057 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
1058 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
1059 } else {
1060 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1061 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
1062 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
1063 Ops.push_back(DAG.getConstant(ID, MVT::i32));
1064 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
1065 }
1066 } else {
1067 Result = Tmp1; // chain
1068 }
1069 break;
1070 }
1071 case TargetLowering::Legal:
1072 if (Tmp1 != Node->getOperand(0) ||
1073 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
1074 SmallVector<SDOperand, 8> Ops;
1075 Ops.push_back(Tmp1);
1076 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1077 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1078 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1079 } else {
1080 // Otherwise promote them.
1081 Ops.push_back(PromoteOp(Node->getOperand(1)));
1082 Ops.push_back(PromoteOp(Node->getOperand(2)));
1083 }
1084 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1085 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1086 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1087 }
1088 break;
1089 }
1090 break;
1091
1092 case ISD::DEBUG_LOC:
1093 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
1094 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
1095 default: assert(0 && "This action is not supported yet!");
1096 case TargetLowering::Legal:
1097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1098 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1099 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1100 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1102 break;
1103 }
1104 break;
1105
1106 case ISD::LABEL:
1107 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1108 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
1109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
1113 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1114 break;
1115 case TargetLowering::Expand:
1116 Result = LegalizeOp(Node->getOperand(0));
1117 break;
1118 }
1119 break;
1120
Scott Michelf2e2b702007-08-08 23:23:31 +00001121 case ISD::Constant: {
1122 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1123 unsigned opAction =
1124 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 // We know we don't need to expand constants here, constants only have one
1127 // value and we check that it is fine above.
1128
Scott Michelf2e2b702007-08-08 23:23:31 +00001129 if (opAction == TargetLowering::Custom) {
1130 Tmp1 = TLI.LowerOperation(Result, DAG);
1131 if (Tmp1.Val)
1132 Result = Tmp1;
1133 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 break;
Scott Michelf2e2b702007-08-08 23:23:31 +00001135 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 case ISD::ConstantFP: {
1137 // Spill FP immediates to the constant pool if the target cannot directly
1138 // codegen them. Targets often have some immediate values that can be
1139 // efficiently generated into an FP register without a load. We explicitly
1140 // leave these constants as ConstantFP nodes for the target to deal with.
1141 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1142
1143 // Check to see if this FP immediate is already legal.
1144 bool isLegal = false;
1145 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1146 E = TLI.legal_fpimm_end(); I != E; ++I)
1147 if (CFP->isExactlyValue(*I)) {
1148 isLegal = true;
1149 break;
1150 }
1151
1152 // If this is a legal constant, turn it into a TargetConstantFP node.
1153 if (isLegal) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00001154 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1155 CFP->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 break;
1157 }
1158
1159 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1160 default: assert(0 && "This action is not supported yet!");
1161 case TargetLowering::Custom:
1162 Tmp3 = TLI.LowerOperation(Result, DAG);
1163 if (Tmp3.Val) {
1164 Result = Tmp3;
1165 break;
1166 }
1167 // FALLTHROUGH
1168 case TargetLowering::Expand:
1169 Result = ExpandConstantFP(CFP, true, DAG, TLI);
1170 }
1171 break;
1172 }
1173 case ISD::TokenFactor:
1174 if (Node->getNumOperands() == 2) {
1175 Tmp1 = LegalizeOp(Node->getOperand(0));
1176 Tmp2 = LegalizeOp(Node->getOperand(1));
1177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1178 } else if (Node->getNumOperands() == 3) {
1179 Tmp1 = LegalizeOp(Node->getOperand(0));
1180 Tmp2 = LegalizeOp(Node->getOperand(1));
1181 Tmp3 = LegalizeOp(Node->getOperand(2));
1182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1183 } else {
1184 SmallVector<SDOperand, 8> Ops;
1185 // Legalize the operands.
1186 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1187 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1188 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1189 }
1190 break;
1191
1192 case ISD::FORMAL_ARGUMENTS:
1193 case ISD::CALL:
1194 // The only option for this is to custom lower it.
1195 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1196 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling22f8deb2007-11-13 00:44:25 +00001197
1198 // The number of incoming and outgoing values should match; unless the final
1199 // outgoing value is a flag.
1200 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1201 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1202 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1203 MVT::Flag)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 "Lowering call/formal_arguments produced unexpected # results!");
1205
1206 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
1207 // remember that we legalized all of them, so it doesn't get relegalized.
1208 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling22f8deb2007-11-13 00:44:25 +00001209 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1210 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211 Tmp1 = LegalizeOp(Tmp3.getValue(i));
1212 if (Op.ResNo == i)
1213 Tmp2 = Tmp1;
1214 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1215 }
1216 return Tmp2;
Christopher Lambb768c2e2007-07-26 07:34:40 +00001217 case ISD::EXTRACT_SUBREG: {
1218 Tmp1 = LegalizeOp(Node->getOperand(0));
1219 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1220 assert(idx && "Operand must be a constant");
1221 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1222 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1223 }
1224 break;
1225 case ISD::INSERT_SUBREG: {
1226 Tmp1 = LegalizeOp(Node->getOperand(0));
1227 Tmp2 = LegalizeOp(Node->getOperand(1));
1228 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1229 assert(idx && "Operand must be a constant");
1230 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1231 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1232 }
1233 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 case ISD::BUILD_VECTOR:
1235 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
1236 default: assert(0 && "This action is not supported yet!");
1237 case TargetLowering::Custom:
1238 Tmp3 = TLI.LowerOperation(Result, DAG);
1239 if (Tmp3.Val) {
1240 Result = Tmp3;
1241 break;
1242 }
1243 // FALLTHROUGH
1244 case TargetLowering::Expand:
1245 Result = ExpandBUILD_VECTOR(Result.Val);
1246 break;
1247 }
1248 break;
1249 case ISD::INSERT_VECTOR_ELT:
1250 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1251 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1252 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1254
1255 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1256 Node->getValueType(0))) {
1257 default: assert(0 && "This action is not supported yet!");
1258 case TargetLowering::Legal:
1259 break;
1260 case TargetLowering::Custom:
Nate Begeman11f2e1d2008-01-05 20:47:37 +00001261 Tmp4 = TLI.LowerOperation(Result, DAG);
1262 if (Tmp4.Val) {
1263 Result = Tmp4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 break;
1265 }
1266 // FALLTHROUGH
1267 case TargetLowering::Expand: {
1268 // If the insert index is a constant, codegen this as a scalar_to_vector,
1269 // then a shuffle that inserts it into the right position in the vector.
1270 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1271 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1272 Tmp1.getValueType(), Tmp2);
1273
1274 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1275 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1276 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1277
1278 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1279 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1280 // the RHS.
1281 SmallVector<SDOperand, 8> ShufOps;
1282 for (unsigned i = 0; i != NumElts; ++i) {
1283 if (i != InsertPos->getValue())
1284 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1285 else
1286 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1287 }
1288 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1289 &ShufOps[0], ShufOps.size());
1290
1291 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1292 Tmp1, ScVec, ShufMask);
1293 Result = LegalizeOp(Result);
1294 break;
1295 }
1296
1297 // If the target doesn't support this, we have to spill the input vector
1298 // to a temporary stack slot, update the element, then reload it. This is
1299 // badness. We could also load the value into a vector register (either
1300 // with a "move to register" or "extload into register" instruction, then
1301 // permute it into place, if the idx is a constant and if the idx is
1302 // supported by the target.
1303 MVT::ValueType VT = Tmp1.getValueType();
1304 MVT::ValueType EltVT = Tmp2.getValueType();
1305 MVT::ValueType IdxVT = Tmp3.getValueType();
1306 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner6fb53da2007-10-15 17:48:57 +00001307 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 // Store the vector.
1309 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
1310
1311 // Truncate or zero extend offset to target pointer type.
1312 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1313 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
1314 // Add the offset to the index.
1315 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1316 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1317 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
1318 // Store the scalar value.
1319 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
1320 // Load the updated vector.
1321 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
1322 break;
1323 }
1324 }
1325 break;
1326 case ISD::SCALAR_TO_VECTOR:
1327 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1328 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1329 break;
1330 }
1331
1332 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1333 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1334 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1335 Node->getValueType(0))) {
1336 default: assert(0 && "This action is not supported yet!");
1337 case TargetLowering::Legal:
1338 break;
1339 case TargetLowering::Custom:
1340 Tmp3 = TLI.LowerOperation(Result, DAG);
1341 if (Tmp3.Val) {
1342 Result = Tmp3;
1343 break;
1344 }
1345 // FALLTHROUGH
1346 case TargetLowering::Expand:
1347 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1348 break;
1349 }
1350 break;
1351 case ISD::VECTOR_SHUFFLE:
1352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1353 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1355
1356 // Allow targets to custom lower the SHUFFLEs they support.
1357 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1358 default: assert(0 && "Unknown operation action!");
1359 case TargetLowering::Legal:
1360 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1361 "vector shuffle should not be created if not legal!");
1362 break;
1363 case TargetLowering::Custom:
1364 Tmp3 = TLI.LowerOperation(Result, DAG);
1365 if (Tmp3.Val) {
1366 Result = Tmp3;
1367 break;
1368 }
1369 // FALLTHROUGH
1370 case TargetLowering::Expand: {
1371 MVT::ValueType VT = Node->getValueType(0);
1372 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
1373 MVT::ValueType PtrVT = TLI.getPointerTy();
1374 SDOperand Mask = Node->getOperand(2);
1375 unsigned NumElems = Mask.getNumOperands();
1376 SmallVector<SDOperand,8> Ops;
1377 for (unsigned i = 0; i != NumElems; ++i) {
1378 SDOperand Arg = Mask.getOperand(i);
1379 if (Arg.getOpcode() == ISD::UNDEF) {
1380 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1381 } else {
1382 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1383 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1384 if (Idx < NumElems)
1385 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1386 DAG.getConstant(Idx, PtrVT)));
1387 else
1388 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1389 DAG.getConstant(Idx - NumElems, PtrVT)));
1390 }
1391 }
1392 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1393 break;
1394 }
1395 case TargetLowering::Promote: {
1396 // Change base type to a different vector type.
1397 MVT::ValueType OVT = Node->getValueType(0);
1398 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1399
1400 // Cast the two input vectors.
1401 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1402 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1403
1404 // Convert the shuffle mask to the right # elements.
1405 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1406 assert(Tmp3.Val && "Shuffle not legal?");
1407 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1408 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1409 break;
1410 }
1411 }
1412 break;
1413
1414 case ISD::EXTRACT_VECTOR_ELT:
1415 Tmp1 = Node->getOperand(0);
1416 Tmp2 = LegalizeOp(Node->getOperand(1));
1417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1418 Result = ExpandEXTRACT_VECTOR_ELT(Result);
1419 break;
1420
1421 case ISD::EXTRACT_SUBVECTOR:
1422 Tmp1 = Node->getOperand(0);
1423 Tmp2 = LegalizeOp(Node->getOperand(1));
1424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1425 Result = ExpandEXTRACT_SUBVECTOR(Result);
1426 break;
1427
1428 case ISD::CALLSEQ_START: {
1429 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1430
1431 // Recursively Legalize all of the inputs of the call end that do not lead
1432 // to this call start. This ensures that any libcalls that need be inserted
1433 // are inserted *before* the CALLSEQ_START.
1434 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
1435 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1436 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1437 NodesLeadingTo);
1438 }
1439
1440 // Now that we legalized all of the inputs (which may have inserted
1441 // libcalls) create the new CALLSEQ_START node.
1442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1443
1444 // Merge in the last call, to ensure that this call start after the last
1445 // call ended.
1446 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
1447 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1448 Tmp1 = LegalizeOp(Tmp1);
1449 }
1450
1451 // Do not try to legalize the target-specific arguments (#1+).
1452 if (Tmp1 != Node->getOperand(0)) {
1453 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1454 Ops[0] = Tmp1;
1455 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1456 }
1457
1458 // Remember that the CALLSEQ_START is legalized.
1459 AddLegalizedOperand(Op.getValue(0), Result);
1460 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1461 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1462
1463 // Now that the callseq_start and all of the non-call nodes above this call
1464 // sequence have been legalized, legalize the call itself. During this
1465 // process, no libcalls can/will be inserted, guaranteeing that no calls
1466 // can overlap.
1467 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1468 SDOperand InCallSEQ = LastCALLSEQ_END;
1469 // Note that we are selecting this call!
1470 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1471 IsLegalizingCall = true;
1472
1473 // Legalize the call, starting from the CALLSEQ_END.
1474 LegalizeOp(LastCALLSEQ_END);
1475 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1476 return Result;
1477 }
1478 case ISD::CALLSEQ_END:
1479 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1480 // will cause this node to be legalized as well as handling libcalls right.
1481 if (LastCALLSEQ_END.Val != Node) {
1482 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1483 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1484 assert(I != LegalizedNodes.end() &&
1485 "Legalizing the call start should have legalized this node!");
1486 return I->second;
1487 }
1488
1489 // Otherwise, the call start has been legalized and everything is going
1490 // according to plan. Just legalize ourselves normally here.
1491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1492 // Do not try to legalize the target-specific arguments (#1+), except for
1493 // an optional flag input.
1494 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1495 if (Tmp1 != Node->getOperand(0)) {
1496 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1497 Ops[0] = Tmp1;
1498 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1499 }
1500 } else {
1501 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1502 if (Tmp1 != Node->getOperand(0) ||
1503 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1504 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1505 Ops[0] = Tmp1;
1506 Ops.back() = Tmp2;
1507 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1508 }
1509 }
1510 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
1511 // This finishes up call legalization.
1512 IsLegalizingCall = false;
1513
1514 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1515 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1516 if (Node->getNumValues() == 2)
1517 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1518 return Result.getValue(Op.ResNo);
1519 case ISD::DYNAMIC_STACKALLOC: {
Evan Chenga448bc42007-08-16 23:50:06 +00001520 MVT::ValueType VT = Node->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1522 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1523 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
1524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1525
1526 Tmp1 = Result.getValue(0);
1527 Tmp2 = Result.getValue(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001528 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 default: assert(0 && "This action is not supported yet!");
1530 case TargetLowering::Expand: {
1531 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1532 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1533 " not tell us which reg is the stack pointer!");
1534 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling22f8deb2007-11-13 00:44:25 +00001535
1536 // Chain the dynamic stack allocation so that it doesn't modify the stack
1537 // pointer when other instructions are using the stack.
1538 Chain = DAG.getCALLSEQ_START(Chain,
1539 DAG.getConstant(0, TLI.getPointerTy()));
1540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 SDOperand Size = Tmp2.getOperand(1);
Evan Chenga448bc42007-08-16 23:50:06 +00001542 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1543 Chain = SP.getValue(1);
1544 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1545 unsigned StackAlign =
1546 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1547 if (Align > StackAlign)
Evan Cheng51ce0382007-08-17 18:02:22 +00001548 SP = DAG.getNode(ISD::AND, VT, SP,
1549 DAG.getConstant(-(uint64_t)Align, VT));
Evan Chenga448bc42007-08-16 23:50:06 +00001550 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling22f8deb2007-11-13 00:44:25 +00001551 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1552
1553 Tmp2 =
1554 DAG.getCALLSEQ_END(Chain,
1555 DAG.getConstant(0, TLI.getPointerTy()),
1556 DAG.getConstant(0, TLI.getPointerTy()),
1557 SDOperand());
1558
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 Tmp1 = LegalizeOp(Tmp1);
1560 Tmp2 = LegalizeOp(Tmp2);
1561 break;
1562 }
1563 case TargetLowering::Custom:
1564 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1565 if (Tmp3.Val) {
1566 Tmp1 = LegalizeOp(Tmp3);
1567 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1568 }
1569 break;
1570 case TargetLowering::Legal:
1571 break;
1572 }
1573 // Since this op produce two values, make sure to remember that we
1574 // legalized both of them.
1575 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1576 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1577 return Op.ResNo ? Tmp2 : Tmp1;
1578 }
1579 case ISD::INLINEASM: {
1580 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
1581 bool Changed = false;
1582 // Legalize all of the operands of the inline asm, in case they are nodes
1583 // that need to be expanded or something. Note we skip the asm string and
1584 // all of the TargetConstant flags.
1585 SDOperand Op = LegalizeOp(Ops[0]);
1586 Changed = Op != Ops[0];
1587 Ops[0] = Op;
1588
1589 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1590 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1591 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1592 for (++i; NumVals; ++i, --NumVals) {
1593 SDOperand Op = LegalizeOp(Ops[i]);
1594 if (Op != Ops[i]) {
1595 Changed = true;
1596 Ops[i] = Op;
1597 }
1598 }
1599 }
1600
1601 if (HasInFlag) {
1602 Op = LegalizeOp(Ops.back());
1603 Changed |= Op != Ops.back();
1604 Ops.back() = Op;
1605 }
1606
1607 if (Changed)
1608 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1609
1610 // INLINE asm returns a chain and flag, make sure to add both to the map.
1611 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1612 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1613 return Result.getValue(Op.ResNo);
1614 }
1615 case ISD::BR:
1616 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1617 // Ensure that libcalls are emitted before a branch.
1618 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1619 Tmp1 = LegalizeOp(Tmp1);
1620 LastCALLSEQ_END = DAG.getEntryNode();
1621
1622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1623 break;
1624 case ISD::BRIND:
1625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1626 // Ensure that libcalls are emitted before a branch.
1627 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1628 Tmp1 = LegalizeOp(Tmp1);
1629 LastCALLSEQ_END = DAG.getEntryNode();
1630
1631 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1632 default: assert(0 && "Indirect target must be legal type (pointer)!");
1633 case Legal:
1634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1635 break;
1636 }
1637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1638 break;
1639 case ISD::BR_JT:
1640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 // Ensure that libcalls are emitted before a branch.
1642 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1643 Tmp1 = LegalizeOp(Tmp1);
1644 LastCALLSEQ_END = DAG.getEntryNode();
1645
1646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1648
1649 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1650 default: assert(0 && "This action is not supported yet!");
1651 case TargetLowering::Legal: break;
1652 case TargetLowering::Custom:
1653 Tmp1 = TLI.LowerOperation(Result, DAG);
1654 if (Tmp1.Val) Result = Tmp1;
1655 break;
1656 case TargetLowering::Expand: {
1657 SDOperand Chain = Result.getOperand(0);
1658 SDOperand Table = Result.getOperand(1);
1659 SDOperand Index = Result.getOperand(2);
1660
1661 MVT::ValueType PTy = TLI.getPointerTy();
1662 MachineFunction &MF = DAG.getMachineFunction();
1663 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
1664 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1665 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
1666
1667 SDOperand LD;
1668 switch (EntrySize) {
1669 default: assert(0 && "Size of jump table not supported yet."); break;
1670 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1671 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1672 }
1673
Evan Cheng6fb06762007-11-09 01:32:10 +00001674 Addr = LD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1676 // For PIC, the sequence is:
1677 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng6fb06762007-11-09 01:32:10 +00001678 // RelocBase can be JumpTable, GOT or some sort of global base.
1679 if (PTy != MVT::i32)
1680 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1681 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1682 TLI.getPICJumpTableRelocBase(Table, DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 }
Evan Cheng6fb06762007-11-09 01:32:10 +00001684 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 }
1686 }
1687 break;
1688 case ISD::BRCOND:
1689 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1690 // Ensure that libcalls are emitted before a return.
1691 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1692 Tmp1 = LegalizeOp(Tmp1);
1693 LastCALLSEQ_END = DAG.getEntryNode();
1694
1695 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1696 case Expand: assert(0 && "It's impossible to expand bools");
1697 case Legal:
1698 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1699 break;
1700 case Promote:
1701 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1702
1703 // The top bits of the promoted condition are not necessarily zero, ensure
1704 // that the value is properly zero extended.
1705 if (!DAG.MaskedValueIsZero(Tmp2,
1706 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1707 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
1708 break;
1709 }
1710
1711 // Basic block destination (Op#2) is always legal.
1712 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1713
1714 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1715 default: assert(0 && "This action is not supported yet!");
1716 case TargetLowering::Legal: break;
1717 case TargetLowering::Custom:
1718 Tmp1 = TLI.LowerOperation(Result, DAG);
1719 if (Tmp1.Val) Result = Tmp1;
1720 break;
1721 case TargetLowering::Expand:
1722 // Expand brcond's setcc into its constituent parts and create a BR_CC
1723 // Node.
1724 if (Tmp2.getOpcode() == ISD::SETCC) {
1725 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1726 Tmp2.getOperand(0), Tmp2.getOperand(1),
1727 Node->getOperand(2));
1728 } else {
1729 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1730 DAG.getCondCode(ISD::SETNE), Tmp2,
1731 DAG.getConstant(0, Tmp2.getValueType()),
1732 Node->getOperand(2));
1733 }
1734 break;
1735 }
1736 break;
1737 case ISD::BR_CC:
1738 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1739 // Ensure that libcalls are emitted before a branch.
1740 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1741 Tmp1 = LegalizeOp(Tmp1);
1742 Tmp2 = Node->getOperand(2); // LHS
1743 Tmp3 = Node->getOperand(3); // RHS
1744 Tmp4 = Node->getOperand(1); // CC
1745
1746 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1747 LastCALLSEQ_END = DAG.getEntryNode();
1748
1749 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1750 // the LHS is a legal SETCC itself. In this case, we need to compare
1751 // the result against zero to select between true and false values.
1752 if (Tmp3.Val == 0) {
1753 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1754 Tmp4 = DAG.getCondCode(ISD::SETNE);
1755 }
1756
1757 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1758 Node->getOperand(4));
1759
1760 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1761 default: assert(0 && "Unexpected action for BR_CC!");
1762 case TargetLowering::Legal: break;
1763 case TargetLowering::Custom:
1764 Tmp4 = TLI.LowerOperation(Result, DAG);
1765 if (Tmp4.Val) Result = Tmp4;
1766 break;
1767 }
1768 break;
1769 case ISD::LOAD: {
1770 LoadSDNode *LD = cast<LoadSDNode>(Node);
1771 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1772 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
1773
1774 ISD::LoadExtType ExtType = LD->getExtensionType();
1775 if (ExtType == ISD::NON_EXTLOAD) {
1776 MVT::ValueType VT = Node->getValueType(0);
1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1778 Tmp3 = Result.getValue(0);
1779 Tmp4 = Result.getValue(1);
1780
1781 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1782 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001783 case TargetLowering::Legal:
1784 // If this is an unaligned load and the target doesn't support it,
1785 // expand it.
1786 if (!TLI.allowsUnalignedMemoryAccesses()) {
1787 unsigned ABIAlignment = TLI.getTargetData()->
1788 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1789 if (LD->getAlignment() < ABIAlignment){
1790 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1791 TLI);
1792 Tmp3 = Result.getOperand(0);
1793 Tmp4 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001794 Tmp3 = LegalizeOp(Tmp3);
1795 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001796 }
1797 }
1798 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001799 case TargetLowering::Custom:
1800 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1801 if (Tmp1.Val) {
1802 Tmp3 = LegalizeOp(Tmp1);
1803 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1804 }
1805 break;
1806 case TargetLowering::Promote: {
1807 // Only promote a load of vector type to another.
1808 assert(MVT::isVector(VT) && "Cannot promote this load!");
1809 // Change base type to a different vector type.
1810 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1811
1812 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1813 LD->getSrcValueOffset(),
1814 LD->isVolatile(), LD->getAlignment());
1815 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1816 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1817 break;
1818 }
1819 }
1820 // Since loads produce two values, make sure to remember that we
1821 // legalized both of them.
1822 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1823 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1824 return Op.ResNo ? Tmp4 : Tmp3;
1825 } else {
1826 MVT::ValueType SrcVT = LD->getLoadedVT();
1827 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1828 default: assert(0 && "This action is not supported yet!");
1829 case TargetLowering::Promote:
1830 assert(SrcVT == MVT::i1 &&
1831 "Can only promote extending LOAD from i1 -> i8!");
1832 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1833 LD->getSrcValue(), LD->getSrcValueOffset(),
1834 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsd7307a92007-10-17 13:49:58 +00001835 Tmp1 = Result.getValue(0);
1836 Tmp2 = Result.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001837 break;
1838 case TargetLowering::Custom:
1839 isCustom = true;
1840 // FALLTHROUGH
1841 case TargetLowering::Legal:
1842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1843 Tmp1 = Result.getValue(0);
1844 Tmp2 = Result.getValue(1);
1845
1846 if (isCustom) {
1847 Tmp3 = TLI.LowerOperation(Result, DAG);
1848 if (Tmp3.Val) {
1849 Tmp1 = LegalizeOp(Tmp3);
1850 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1851 }
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001852 } else {
1853 // If this is an unaligned load and the target doesn't support it,
1854 // expand it.
1855 if (!TLI.allowsUnalignedMemoryAccesses()) {
1856 unsigned ABIAlignment = TLI.getTargetData()->
1857 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1858 if (LD->getAlignment() < ABIAlignment){
1859 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1860 TLI);
1861 Tmp1 = Result.getOperand(0);
1862 Tmp2 = Result.getOperand(1);
Dale Johannesen08275382007-09-08 19:29:23 +00001863 Tmp1 = LegalizeOp(Tmp1);
1864 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00001865 }
1866 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001867 }
1868 break;
1869 case TargetLowering::Expand:
1870 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1871 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1872 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1873 LD->getSrcValueOffset(),
1874 LD->isVolatile(), LD->getAlignment());
1875 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1876 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1877 Tmp2 = LegalizeOp(Load.getValue(1));
1878 break;
1879 }
1880 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1881 // Turn the unsupported load into an EXTLOAD followed by an explicit
1882 // zero/sign extend inreg.
1883 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1884 Tmp1, Tmp2, LD->getSrcValue(),
1885 LD->getSrcValueOffset(), SrcVT,
1886 LD->isVolatile(), LD->getAlignment());
1887 SDOperand ValRes;
1888 if (ExtType == ISD::SEXTLOAD)
1889 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1890 Result, DAG.getValueType(SrcVT));
1891 else
1892 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1893 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1894 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1895 break;
1896 }
1897 // Since loads produce two values, make sure to remember that we legalized
1898 // both of them.
1899 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1900 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1901 return Op.ResNo ? Tmp2 : Tmp1;
1902 }
1903 }
1904 case ISD::EXTRACT_ELEMENT: {
1905 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1906 switch (getTypeAction(OpTy)) {
1907 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1908 case Legal:
1909 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1910 // 1 -> Hi
1911 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1912 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1913 TLI.getShiftAmountTy()));
1914 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1915 } else {
1916 // 0 -> Lo
1917 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1918 Node->getOperand(0));
1919 }
1920 break;
1921 case Expand:
1922 // Get both the low and high parts.
1923 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1924 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1925 Result = Tmp2; // 1 -> Hi
1926 else
1927 Result = Tmp1; // 0 -> Lo
1928 break;
1929 }
1930 break;
1931 }
1932
1933 case ISD::CopyToReg:
1934 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1935
1936 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
1937 "Register type must be legal!");
1938 // Legalize the incoming value (must be a legal type).
1939 Tmp2 = LegalizeOp(Node->getOperand(2));
1940 if (Node->getNumValues() == 1) {
1941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
1942 } else {
1943 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1944 if (Node->getNumOperands() == 4) {
1945 Tmp3 = LegalizeOp(Node->getOperand(3));
1946 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1947 Tmp3);
1948 } else {
1949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1950 }
1951
1952 // Since this produces two values, make sure to remember that we legalized
1953 // both of them.
1954 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1955 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1956 return Result;
1957 }
1958 break;
1959
1960 case ISD::RET:
1961 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1962
1963 // Ensure that libcalls are emitted before a return.
1964 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1965 Tmp1 = LegalizeOp(Tmp1);
1966 LastCALLSEQ_END = DAG.getEntryNode();
1967
1968 switch (Node->getNumOperands()) {
1969 case 3: // ret val
1970 Tmp2 = Node->getOperand(1);
1971 Tmp3 = Node->getOperand(2); // Signness
1972 switch (getTypeAction(Tmp2.getValueType())) {
1973 case Legal:
1974 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
1975 break;
1976 case Expand:
1977 if (!MVT::isVector(Tmp2.getValueType())) {
1978 SDOperand Lo, Hi;
1979 ExpandOp(Tmp2, Lo, Hi);
1980
1981 // Big endian systems want the hi reg first.
1982 if (!TLI.isLittleEndian())
1983 std::swap(Lo, Hi);
1984
1985 if (Hi.Val)
1986 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1987 else
1988 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
1989 Result = LegalizeOp(Result);
1990 } else {
1991 SDNode *InVal = Tmp2.Val;
Dale Johannesendb132452007-10-20 00:07:52 +00001992 int InIx = Tmp2.ResNo;
1993 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1994 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995
1996 // Figure out if there is a simple type corresponding to this Vector
1997 // type. If so, convert to the vector type.
1998 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1999 if (TLI.isTypeLegal(TVT)) {
2000 // Turn this into a return of the vector type.
2001 Tmp2 = LegalizeOp(Tmp2);
2002 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2003 } else if (NumElems == 1) {
2004 // Turn this into a return of the scalar type.
2005 Tmp2 = ScalarizeVectorOp(Tmp2);
2006 Tmp2 = LegalizeOp(Tmp2);
2007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2008
2009 // FIXME: Returns of gcc generic vectors smaller than a legal type
2010 // should be returned in integer registers!
2011
2012 // The scalarized value type may not be legal, e.g. it might require
2013 // promotion or expansion. Relegalize the return.
2014 Result = LegalizeOp(Result);
2015 } else {
2016 // FIXME: Returns of gcc generic vectors larger than a legal vector
2017 // type should be returned by reference!
2018 SDOperand Lo, Hi;
2019 SplitVectorOp(Tmp2, Lo, Hi);
2020 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2021 Result = LegalizeOp(Result);
2022 }
2023 }
2024 break;
2025 case Promote:
2026 Tmp2 = PromoteOp(Node->getOperand(1));
2027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2028 Result = LegalizeOp(Result);
2029 break;
2030 }
2031 break;
2032 case 1: // ret void
2033 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2034 break;
2035 default: { // ret <values>
2036 SmallVector<SDOperand, 8> NewValues;
2037 NewValues.push_back(Tmp1);
2038 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
2039 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2040 case Legal:
2041 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2042 NewValues.push_back(Node->getOperand(i+1));
2043 break;
2044 case Expand: {
2045 SDOperand Lo, Hi;
2046 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
2047 "FIXME: TODO: implement returning non-legal vector types!");
2048 ExpandOp(Node->getOperand(i), Lo, Hi);
2049 NewValues.push_back(Lo);
2050 NewValues.push_back(Node->getOperand(i+1));
2051 if (Hi.Val) {
2052 NewValues.push_back(Hi);
2053 NewValues.push_back(Node->getOperand(i+1));
2054 }
2055 break;
2056 }
2057 case Promote:
2058 assert(0 && "Can't promote multiple return value yet!");
2059 }
2060
2061 if (NewValues.size() == Node->getNumOperands())
2062 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
2063 else
2064 Result = DAG.getNode(ISD::RET, MVT::Other,
2065 &NewValues[0], NewValues.size());
2066 break;
2067 }
2068 }
2069
2070 if (Result.getOpcode() == ISD::RET) {
2071 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2072 default: assert(0 && "This action is not supported yet!");
2073 case TargetLowering::Legal: break;
2074 case TargetLowering::Custom:
2075 Tmp1 = TLI.LowerOperation(Result, DAG);
2076 if (Tmp1.Val) Result = Tmp1;
2077 break;
2078 }
2079 }
2080 break;
2081 case ISD::STORE: {
2082 StoreSDNode *ST = cast<StoreSDNode>(Node);
2083 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2084 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
2085 int SVOffset = ST->getSrcValueOffset();
2086 unsigned Alignment = ST->getAlignment();
2087 bool isVolatile = ST->isVolatile();
2088
2089 if (!ST->isTruncatingStore()) {
2090 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2091 // FIXME: We shouldn't do this for TargetConstantFP's.
2092 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2093 // to phase ordering between legalized code and the dag combiner. This
2094 // probably means that we need to integrate dag combiner and legalizer
2095 // together.
Dale Johannesen2fc20782007-09-14 22:26:36 +00002096 // We generally can't do this one for long doubles.
Chris Lattnere8671c52007-10-13 06:35:54 +00002097 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002098 if (CFP->getValueType(0) == MVT::f32 &&
2099 getTypeAction(MVT::i32) == Legal) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00002100 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2101 convertToAPInt().getZExtValue(),
Dale Johannesen1616e902007-09-11 18:32:33 +00002102 MVT::i32);
Dale Johannesen2fc20782007-09-14 22:26:36 +00002103 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2104 SVOffset, isVolatile, Alignment);
2105 break;
2106 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner19f229a2007-10-15 05:46:06 +00002107 // If this target supports 64-bit registers, do a single 64-bit store.
2108 if (getTypeAction(MVT::i64) == Legal) {
2109 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2110 getZExtValue(), MVT::i64);
2111 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2112 SVOffset, isVolatile, Alignment);
2113 break;
2114 } else if (getTypeAction(MVT::i32) == Legal) {
2115 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2116 // stores. If the target supports neither 32- nor 64-bits, this
2117 // xform is certainly not worth it.
2118 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2119 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2120 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2121 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2122
2123 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2124 SVOffset, isVolatile, Alignment);
2125 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2126 getIntPtrConstant(4));
2127 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsa3691432007-10-28 12:59:45 +00002128 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner19f229a2007-10-15 05:46:06 +00002129
2130 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2131 break;
2132 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002133 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002134 }
2135
2136 switch (getTypeAction(ST->getStoredVT())) {
2137 case Legal: {
2138 Tmp3 = LegalizeOp(ST->getValue());
2139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2140 ST->getOffset());
2141
2142 MVT::ValueType VT = Tmp3.getValueType();
2143 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2144 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002145 case TargetLowering::Legal:
2146 // If this is an unaligned store and the target doesn't support it,
2147 // expand it.
2148 if (!TLI.allowsUnalignedMemoryAccesses()) {
2149 unsigned ABIAlignment = TLI.getTargetData()->
2150 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2151 if (ST->getAlignment() < ABIAlignment)
2152 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2153 TLI);
2154 }
2155 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 case TargetLowering::Custom:
2157 Tmp1 = TLI.LowerOperation(Result, DAG);
2158 if (Tmp1.Val) Result = Tmp1;
2159 break;
2160 case TargetLowering::Promote:
2161 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2162 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2163 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2164 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
2165 ST->getSrcValue(), SVOffset, isVolatile,
2166 Alignment);
2167 break;
2168 }
2169 break;
2170 }
2171 case Promote:
2172 // Truncate the value and store the result.
2173 Tmp3 = PromoteOp(ST->getValue());
2174 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2175 SVOffset, ST->getStoredVT(),
2176 isVolatile, Alignment);
2177 break;
2178
2179 case Expand:
2180 unsigned IncrementSize = 0;
2181 SDOperand Lo, Hi;
2182
2183 // If this is a vector type, then we have to calculate the increment as
2184 // the product of the element size in bytes, and the number of elements
2185 // in the high half of the vector.
2186 if (MVT::isVector(ST->getValue().getValueType())) {
2187 SDNode *InVal = ST->getValue().Val;
Dale Johannesendb132452007-10-20 00:07:52 +00002188 int InIx = ST->getValue().ResNo;
2189 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2190 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191
2192 // Figure out if there is a simple type corresponding to this Vector
2193 // type. If so, convert to the vector type.
2194 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2195 if (TLI.isTypeLegal(TVT)) {
2196 // Turn this into a normal store of the vector type.
2197 Tmp3 = LegalizeOp(Node->getOperand(1));
2198 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2199 SVOffset, isVolatile, Alignment);
2200 Result = LegalizeOp(Result);
2201 break;
2202 } else if (NumElems == 1) {
2203 // Turn this into a normal store of the scalar type.
2204 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
2205 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2206 SVOffset, isVolatile, Alignment);
2207 // The scalarized value type may not be legal, e.g. it might require
2208 // promotion or expansion. Relegalize the scalar store.
2209 Result = LegalizeOp(Result);
2210 break;
2211 } else {
2212 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00002213 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2214 MVT::getSizeInBits(EVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 }
2216 } else {
2217 ExpandOp(Node->getOperand(1), Lo, Hi);
2218 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
2219
2220 if (!TLI.isLittleEndian())
2221 std::swap(Lo, Hi);
2222 }
2223
2224 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2225 SVOffset, isVolatile, Alignment);
2226
2227 if (Hi.Val == NULL) {
2228 // Must be int <-> float one-to-one expansion.
2229 Result = Lo;
2230 break;
2231 }
2232
2233 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2234 getIntPtrConstant(IncrementSize));
2235 assert(isTypeLegal(Tmp2.getValueType()) &&
2236 "Pointers must be legal!");
2237 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00002238 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2240 SVOffset, isVolatile, Alignment);
2241 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2242 break;
2243 }
2244 } else {
2245 // Truncating store
2246 assert(isTypeLegal(ST->getValue().getValueType()) &&
2247 "Cannot handle illegal TRUNCSTORE yet!");
2248 Tmp3 = LegalizeOp(ST->getValue());
2249
2250 // The only promote case we handle is TRUNCSTORE:i1 X into
2251 // -> TRUNCSTORE:i8 (and X, 1)
2252 if (ST->getStoredVT() == MVT::i1 &&
2253 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2254 // Promote the bool to a mask then store.
2255 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2256 DAG.getConstant(1, Tmp3.getValueType()));
2257 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2258 SVOffset, MVT::i8,
2259 isVolatile, Alignment);
2260 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2261 Tmp2 != ST->getBasePtr()) {
2262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2263 ST->getOffset());
2264 }
2265
2266 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2267 switch (TLI.getStoreXAction(StVT)) {
2268 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio578434f2007-08-01 19:34:21 +00002269 case TargetLowering::Legal:
2270 // If this is an unaligned store and the target doesn't support it,
2271 // expand it.
2272 if (!TLI.allowsUnalignedMemoryAccesses()) {
2273 unsigned ABIAlignment = TLI.getTargetData()->
2274 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2275 if (ST->getAlignment() < ABIAlignment)
2276 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2277 TLI);
2278 }
2279 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 case TargetLowering::Custom:
2281 Tmp1 = TLI.LowerOperation(Result, DAG);
2282 if (Tmp1.Val) Result = Tmp1;
2283 break;
2284 }
2285 }
2286 break;
2287 }
2288 case ISD::PCMARKER:
2289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
2291 break;
2292 case ISD::STACKSAVE:
2293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2294 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2295 Tmp1 = Result.getValue(0);
2296 Tmp2 = Result.getValue(1);
2297
2298 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2299 default: assert(0 && "This action is not supported yet!");
2300 case TargetLowering::Legal: break;
2301 case TargetLowering::Custom:
2302 Tmp3 = TLI.LowerOperation(Result, DAG);
2303 if (Tmp3.Val) {
2304 Tmp1 = LegalizeOp(Tmp3);
2305 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2306 }
2307 break;
2308 case TargetLowering::Expand:
2309 // Expand to CopyFromReg if the target set
2310 // StackPointerRegisterToSaveRestore.
2311 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2312 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
2313 Node->getValueType(0));
2314 Tmp2 = Tmp1.getValue(1);
2315 } else {
2316 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2317 Tmp2 = Node->getOperand(0);
2318 }
2319 break;
2320 }
2321
2322 // Since stacksave produce two values, make sure to remember that we
2323 // legalized both of them.
2324 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2325 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2326 return Op.ResNo ? Tmp2 : Tmp1;
2327
2328 case ISD::STACKRESTORE:
2329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2332
2333 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2334 default: assert(0 && "This action is not supported yet!");
2335 case TargetLowering::Legal: break;
2336 case TargetLowering::Custom:
2337 Tmp1 = TLI.LowerOperation(Result, DAG);
2338 if (Tmp1.Val) Result = Tmp1;
2339 break;
2340 case TargetLowering::Expand:
2341 // Expand to CopyToReg if the target set
2342 // StackPointerRegisterToSaveRestore.
2343 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2344 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2345 } else {
2346 Result = Tmp1;
2347 }
2348 break;
2349 }
2350 break;
2351
2352 case ISD::READCYCLECOUNTER:
2353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
2354 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2355 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2356 Node->getValueType(0))) {
2357 default: assert(0 && "This action is not supported yet!");
2358 case TargetLowering::Legal:
2359 Tmp1 = Result.getValue(0);
2360 Tmp2 = Result.getValue(1);
2361 break;
2362 case TargetLowering::Custom:
2363 Result = TLI.LowerOperation(Result, DAG);
2364 Tmp1 = LegalizeOp(Result.getValue(0));
2365 Tmp2 = LegalizeOp(Result.getValue(1));
2366 break;
2367 }
2368
2369 // Since rdcc produce two values, make sure to remember that we legalized
2370 // both of them.
2371 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2372 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2373 return Result;
2374
2375 case ISD::SELECT:
2376 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2377 case Expand: assert(0 && "It's impossible to expand bools");
2378 case Legal:
2379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2380 break;
2381 case Promote:
2382 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2383 // Make sure the condition is either zero or one.
2384 if (!DAG.MaskedValueIsZero(Tmp1,
2385 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2386 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
2387 break;
2388 }
2389 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
2390 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
2391
2392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2393
2394 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
2395 default: assert(0 && "This action is not supported yet!");
2396 case TargetLowering::Legal: break;
2397 case TargetLowering::Custom: {
2398 Tmp1 = TLI.LowerOperation(Result, DAG);
2399 if (Tmp1.Val) Result = Tmp1;
2400 break;
2401 }
2402 case TargetLowering::Expand:
2403 if (Tmp1.getOpcode() == ISD::SETCC) {
2404 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2405 Tmp2, Tmp3,
2406 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2407 } else {
2408 Result = DAG.getSelectCC(Tmp1,
2409 DAG.getConstant(0, Tmp1.getValueType()),
2410 Tmp2, Tmp3, ISD::SETNE);
2411 }
2412 break;
2413 case TargetLowering::Promote: {
2414 MVT::ValueType NVT =
2415 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2416 unsigned ExtOp, TruncOp;
2417 if (MVT::isVector(Tmp2.getValueType())) {
2418 ExtOp = ISD::BIT_CONVERT;
2419 TruncOp = ISD::BIT_CONVERT;
2420 } else if (MVT::isInteger(Tmp2.getValueType())) {
2421 ExtOp = ISD::ANY_EXTEND;
2422 TruncOp = ISD::TRUNCATE;
2423 } else {
2424 ExtOp = ISD::FP_EXTEND;
2425 TruncOp = ISD::FP_ROUND;
2426 }
2427 // Promote each of the values to the new type.
2428 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2429 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2430 // Perform the larger operation, then round down.
2431 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2432 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2433 break;
2434 }
2435 }
2436 break;
2437 case ISD::SELECT_CC: {
2438 Tmp1 = Node->getOperand(0); // LHS
2439 Tmp2 = Node->getOperand(1); // RHS
2440 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2441 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
2442 SDOperand CC = Node->getOperand(4);
2443
2444 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2445
2446 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2447 // the LHS is a legal SETCC itself. In this case, we need to compare
2448 // the result against zero to select between true and false values.
2449 if (Tmp2.Val == 0) {
2450 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2451 CC = DAG.getCondCode(ISD::SETNE);
2452 }
2453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2454
2455 // Everything is legal, see if we should expand this op or something.
2456 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2457 default: assert(0 && "This action is not supported yet!");
2458 case TargetLowering::Legal: break;
2459 case TargetLowering::Custom:
2460 Tmp1 = TLI.LowerOperation(Result, DAG);
2461 if (Tmp1.Val) Result = Tmp1;
2462 break;
2463 }
2464 break;
2465 }
2466 case ISD::SETCC:
2467 Tmp1 = Node->getOperand(0);
2468 Tmp2 = Node->getOperand(1);
2469 Tmp3 = Node->getOperand(2);
2470 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2471
2472 // If we had to Expand the SetCC operands into a SELECT node, then it may
2473 // not always be possible to return a true LHS & RHS. In this case, just
2474 // return the value we legalized, returned in the LHS
2475 if (Tmp2.Val == 0) {
2476 Result = Tmp1;
2477 break;
2478 }
2479
2480 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
2481 default: assert(0 && "Cannot handle this action for SETCC yet!");
2482 case TargetLowering::Custom:
2483 isCustom = true;
2484 // FALLTHROUGH.
2485 case TargetLowering::Legal:
2486 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2487 if (isCustom) {
2488 Tmp4 = TLI.LowerOperation(Result, DAG);
2489 if (Tmp4.Val) Result = Tmp4;
2490 }
2491 break;
2492 case TargetLowering::Promote: {
2493 // First step, figure out the appropriate operation to use.
2494 // Allow SETCC to not be supported for all legal data types
2495 // Mostly this targets FP
2496 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2497 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
2498
2499 // Scan for the appropriate larger type to use.
2500 while (1) {
2501 NewInTy = (MVT::ValueType)(NewInTy+1);
2502
2503 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2504 "Fell off of the edge of the integer world");
2505 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2506 "Fell off of the edge of the floating point world");
2507
2508 // If the target supports SETCC of this type, use it.
2509 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
2510 break;
2511 }
2512 if (MVT::isInteger(NewInTy))
2513 assert(0 && "Cannot promote Legal Integer SETCC yet");
2514 else {
2515 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2516 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2517 }
2518 Tmp1 = LegalizeOp(Tmp1);
2519 Tmp2 = LegalizeOp(Tmp2);
2520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2521 Result = LegalizeOp(Result);
2522 break;
2523 }
2524 case TargetLowering::Expand:
2525 // Expand a setcc node into a select_cc of the same condition, lhs, and
2526 // rhs that selects between const 1 (true) and const 0 (false).
2527 MVT::ValueType VT = Node->getValueType(0);
2528 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2529 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2530 Tmp3);
2531 break;
2532 }
2533 break;
2534 case ISD::MEMSET:
2535 case ISD::MEMCPY:
2536 case ISD::MEMMOVE: {
2537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
2538 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2539
2540 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2541 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2542 case Expand: assert(0 && "Cannot expand a byte!");
2543 case Legal:
2544 Tmp3 = LegalizeOp(Node->getOperand(2));
2545 break;
2546 case Promote:
2547 Tmp3 = PromoteOp(Node->getOperand(2));
2548 break;
2549 }
2550 } else {
2551 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
2552 }
2553
2554 SDOperand Tmp4;
2555 switch (getTypeAction(Node->getOperand(3).getValueType())) {
2556 case Expand: {
2557 // Length is too big, just take the lo-part of the length.
2558 SDOperand HiPart;
2559 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
2560 break;
2561 }
2562 case Legal:
2563 Tmp4 = LegalizeOp(Node->getOperand(3));
2564 break;
2565 case Promote:
2566 Tmp4 = PromoteOp(Node->getOperand(3));
2567 break;
2568 }
2569
2570 SDOperand Tmp5;
2571 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2572 case Expand: assert(0 && "Cannot expand this yet!");
2573 case Legal:
2574 Tmp5 = LegalizeOp(Node->getOperand(4));
2575 break;
2576 case Promote:
2577 Tmp5 = PromoteOp(Node->getOperand(4));
2578 break;
2579 }
2580
Rafael Espindola80825902007-10-19 10:41:11 +00002581 SDOperand Tmp6;
2582 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2583 case Expand: assert(0 && "Cannot expand this yet!");
2584 case Legal:
2585 Tmp6 = LegalizeOp(Node->getOperand(5));
2586 break;
2587 case Promote:
2588 Tmp6 = PromoteOp(Node->getOperand(5));
2589 break;
2590 }
2591
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002592 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2593 default: assert(0 && "This action not implemented for this operation!");
2594 case TargetLowering::Custom:
2595 isCustom = true;
2596 // FALLTHROUGH
Rafael Espindola80825902007-10-19 10:41:11 +00002597 case TargetLowering::Legal: {
2598 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2599 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 if (isCustom) {
2601 Tmp1 = TLI.LowerOperation(Result, DAG);
2602 if (Tmp1.Val) Result = Tmp1;
2603 }
2604 break;
Rafael Espindola80825902007-10-19 10:41:11 +00002605 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 case TargetLowering::Expand: {
2607 // Otherwise, the target does not support this operation. Lower the
2608 // operation to an explicit libcall as appropriate.
2609 MVT::ValueType IntPtr = TLI.getPointerTy();
2610 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2611 TargetLowering::ArgListTy Args;
2612 TargetLowering::ArgListEntry Entry;
2613
2614 const char *FnName = 0;
2615 if (Node->getOpcode() == ISD::MEMSET) {
2616 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
2617 Args.push_back(Entry);
2618 // Extend the (previously legalized) ubyte argument to be an int value
2619 // for the call.
2620 if (Tmp3.getValueType() > MVT::i32)
2621 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2622 else
2623 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
2624 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2625 Args.push_back(Entry);
2626 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2627 Args.push_back(Entry);
2628
2629 FnName = "memset";
2630 } else if (Node->getOpcode() == ISD::MEMCPY ||
2631 Node->getOpcode() == ISD::MEMMOVE) {
2632 Entry.Ty = IntPtrTy;
2633 Entry.Node = Tmp2; Args.push_back(Entry);
2634 Entry.Node = Tmp3; Args.push_back(Entry);
2635 Entry.Node = Tmp4; Args.push_back(Entry);
2636 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2637 } else {
2638 assert(0 && "Unknown op!");
2639 }
2640
2641 std::pair<SDOperand,SDOperand> CallResult =
2642 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
2643 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
2644 Result = CallResult.second;
2645 break;
2646 }
2647 }
2648 break;
2649 }
2650
2651 case ISD::SHL_PARTS:
2652 case ISD::SRA_PARTS:
2653 case ISD::SRL_PARTS: {
2654 SmallVector<SDOperand, 8> Ops;
2655 bool Changed = false;
2656 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2657 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2658 Changed |= Ops.back() != Node->getOperand(i);
2659 }
2660 if (Changed)
2661 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
2662
2663 switch (TLI.getOperationAction(Node->getOpcode(),
2664 Node->getValueType(0))) {
2665 default: assert(0 && "This action is not supported yet!");
2666 case TargetLowering::Legal: break;
2667 case TargetLowering::Custom:
2668 Tmp1 = TLI.LowerOperation(Result, DAG);
2669 if (Tmp1.Val) {
2670 SDOperand Tmp2, RetVal(0, 0);
2671 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2672 Tmp2 = LegalizeOp(Tmp1.getValue(i));
2673 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2674 if (i == Op.ResNo)
2675 RetVal = Tmp2;
2676 }
2677 assert(RetVal.Val && "Illegal result number");
2678 return RetVal;
2679 }
2680 break;
2681 }
2682
2683 // Since these produce multiple values, make sure to remember that we
2684 // legalized all of them.
2685 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2686 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2687 return Result.getValue(Op.ResNo);
2688 }
2689
2690 // Binary operators
2691 case ISD::ADD:
2692 case ISD::SUB:
2693 case ISD::MUL:
2694 case ISD::MULHS:
2695 case ISD::MULHU:
2696 case ISD::UDIV:
2697 case ISD::SDIV:
2698 case ISD::AND:
2699 case ISD::OR:
2700 case ISD::XOR:
2701 case ISD::SHL:
2702 case ISD::SRL:
2703 case ISD::SRA:
2704 case ISD::FADD:
2705 case ISD::FSUB:
2706 case ISD::FMUL:
2707 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00002708 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2710 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2711 case Expand: assert(0 && "Not possible");
2712 case Legal:
2713 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2714 break;
2715 case Promote:
2716 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2717 break;
2718 }
2719
2720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2721
2722 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2723 default: assert(0 && "BinOp legalize operation not supported");
2724 case TargetLowering::Legal: break;
2725 case TargetLowering::Custom:
2726 Tmp1 = TLI.LowerOperation(Result, DAG);
2727 if (Tmp1.Val) Result = Tmp1;
2728 break;
2729 case TargetLowering::Expand: {
Dan Gohman5a199552007-10-08 18:33:35 +00002730 MVT::ValueType VT = Op.getValueType();
2731
2732 // See if multiply or divide can be lowered using two-result operations.
2733 SDVTList VTs = DAG.getVTList(VT, VT);
2734 if (Node->getOpcode() == ISD::MUL) {
2735 // We just need the low half of the multiply; try both the signed
2736 // and unsigned forms. If the target supports both SMUL_LOHI and
2737 // UMUL_LOHI, form a preference by checking which forms of plain
2738 // MULH it supports.
2739 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2740 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2741 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2742 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2743 unsigned OpToUse = 0;
2744 if (HasSMUL_LOHI && !HasMULHS) {
2745 OpToUse = ISD::SMUL_LOHI;
2746 } else if (HasUMUL_LOHI && !HasMULHU) {
2747 OpToUse = ISD::UMUL_LOHI;
2748 } else if (HasSMUL_LOHI) {
2749 OpToUse = ISD::SMUL_LOHI;
2750 } else if (HasUMUL_LOHI) {
2751 OpToUse = ISD::UMUL_LOHI;
2752 }
2753 if (OpToUse) {
2754 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2755 break;
2756 }
2757 }
2758 if (Node->getOpcode() == ISD::MULHS &&
2759 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2760 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2761 break;
2762 }
2763 if (Node->getOpcode() == ISD::MULHU &&
2764 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2765 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2766 break;
2767 }
2768 if (Node->getOpcode() == ISD::SDIV &&
2769 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2770 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2771 break;
2772 }
2773 if (Node->getOpcode() == ISD::UDIV &&
2774 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2775 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2776 break;
2777 }
2778
Dan Gohman6d05cac2007-10-11 23:57:53 +00002779 // Check to see if we have a libcall for this operator.
2780 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2781 bool isSigned = false;
2782 switch (Node->getOpcode()) {
2783 case ISD::UDIV:
2784 case ISD::SDIV:
2785 if (VT == MVT::i32) {
2786 LC = Node->getOpcode() == ISD::UDIV
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman6d05cac2007-10-11 23:57:53 +00002788 isSigned = Node->getOpcode() == ISD::SDIV;
2789 }
2790 break;
2791 case ISD::FPOW:
Duncan Sands37a3f472008-01-10 10:28:30 +00002792 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2793 RTLIB::POW_PPCF128);
Dan Gohman6d05cac2007-10-11 23:57:53 +00002794 break;
2795 default: break;
2796 }
2797 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2798 SDOperand Dummy;
2799 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 break;
2801 }
2802
2803 assert(MVT::isVector(Node->getValueType(0)) &&
2804 "Cannot expand this binary operator!");
2805 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman6d05cac2007-10-11 23:57:53 +00002806 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002807 break;
2808 }
2809 case TargetLowering::Promote: {
2810 switch (Node->getOpcode()) {
2811 default: assert(0 && "Do not know how to promote this BinOp!");
2812 case ISD::AND:
2813 case ISD::OR:
2814 case ISD::XOR: {
2815 MVT::ValueType OVT = Node->getValueType(0);
2816 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2817 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2818 // Bit convert each of the values to the new type.
2819 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2820 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2821 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2822 // Bit convert the result back the original type.
2823 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2824 break;
2825 }
2826 }
2827 }
2828 }
2829 break;
2830
Dan Gohman475cd732007-10-05 14:17:22 +00002831 case ISD::SMUL_LOHI:
2832 case ISD::UMUL_LOHI:
2833 case ISD::SDIVREM:
2834 case ISD::UDIVREM:
2835 // These nodes will only be produced by target-specific lowering, so
2836 // they shouldn't be here if they aren't legal.
Duncan Sandsb42a44e2007-10-16 09:07:20 +00002837 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman475cd732007-10-05 14:17:22 +00002838 "This must be legal!");
Dan Gohman5a199552007-10-08 18:33:35 +00002839
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2841 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman475cd732007-10-05 14:17:22 +00002843 break;
2844
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2846 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2847 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2848 case Expand: assert(0 && "Not possible");
2849 case Legal:
2850 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2851 break;
2852 case Promote:
2853 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2854 break;
2855 }
2856
2857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2858
2859 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2860 default: assert(0 && "Operation not supported");
2861 case TargetLowering::Custom:
2862 Tmp1 = TLI.LowerOperation(Result, DAG);
2863 if (Tmp1.Val) Result = Tmp1;
2864 break;
2865 case TargetLowering::Legal: break;
2866 case TargetLowering::Expand: {
2867 // If this target supports fabs/fneg natively and select is cheap,
2868 // do this efficiently.
2869 if (!TLI.isSelectExpensive() &&
2870 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2871 TargetLowering::Legal &&
2872 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
2873 TargetLowering::Legal) {
2874 // Get the sign bit of the RHS.
2875 MVT::ValueType IVT =
2876 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2877 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2878 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2879 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2880 // Get the absolute value of the result.
2881 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2882 // Select between the nabs and abs value based on the sign bit of
2883 // the input.
2884 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2885 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2886 AbsVal),
2887 AbsVal);
2888 Result = LegalizeOp(Result);
2889 break;
2890 }
2891
2892 // Otherwise, do bitwise ops!
2893 MVT::ValueType NVT =
2894 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2895 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2896 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2897 Result = LegalizeOp(Result);
2898 break;
2899 }
2900 }
2901 break;
2902
2903 case ISD::ADDC:
2904 case ISD::SUBC:
2905 Tmp1 = LegalizeOp(Node->getOperand(0));
2906 Tmp2 = LegalizeOp(Node->getOperand(1));
2907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2908 // Since this produces two values, make sure to remember that we legalized
2909 // both of them.
2910 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2911 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2912 return Result;
2913
2914 case ISD::ADDE:
2915 case ISD::SUBE:
2916 Tmp1 = LegalizeOp(Node->getOperand(0));
2917 Tmp2 = LegalizeOp(Node->getOperand(1));
2918 Tmp3 = LegalizeOp(Node->getOperand(2));
2919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2920 // Since this produces two values, make sure to remember that we legalized
2921 // both of them.
2922 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2923 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2924 return Result;
2925
2926 case ISD::BUILD_PAIR: {
2927 MVT::ValueType PairTy = Node->getValueType(0);
2928 // TODO: handle the case where the Lo and Hi operands are not of legal type
2929 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2930 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2931 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2932 case TargetLowering::Promote:
2933 case TargetLowering::Custom:
2934 assert(0 && "Cannot promote/custom this yet!");
2935 case TargetLowering::Legal:
2936 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2937 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2938 break;
2939 case TargetLowering::Expand:
2940 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2941 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2942 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2943 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2944 TLI.getShiftAmountTy()));
2945 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
2946 break;
2947 }
2948 break;
2949 }
2950
2951 case ISD::UREM:
2952 case ISD::SREM:
2953 case ISD::FREM:
2954 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2955 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2956
2957 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2958 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2959 case TargetLowering::Custom:
2960 isCustom = true;
2961 // FALLTHROUGH
2962 case TargetLowering::Legal:
2963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2964 if (isCustom) {
2965 Tmp1 = TLI.LowerOperation(Result, DAG);
2966 if (Tmp1.Val) Result = Tmp1;
2967 }
2968 break;
Dan Gohman5a199552007-10-08 18:33:35 +00002969 case TargetLowering::Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2971 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman5a199552007-10-08 18:33:35 +00002972 MVT::ValueType VT = Node->getValueType(0);
2973
2974 // See if remainder can be lowered using two-result operations.
2975 SDVTList VTs = DAG.getVTList(VT, VT);
2976 if (Node->getOpcode() == ISD::SREM &&
2977 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2978 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2979 break;
2980 }
2981 if (Node->getOpcode() == ISD::UREM &&
2982 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2983 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2984 break;
2985 }
2986
2987 if (MVT::isInteger(VT)) {
2988 if (TLI.getOperationAction(DivOpc, VT) ==
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 TargetLowering::Legal) {
2990 // X % Y -> X-X/Y*Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
2992 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2993 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00002994 } else if (MVT::isVector(VT)) {
2995 Result = LegalizeOp(UnrollVectorOp(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996 } else {
Dan Gohman5a199552007-10-08 18:33:35 +00002997 assert(VT == MVT::i32 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998 "Cannot expand this binary operator!");
2999 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3000 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
3001 SDOperand Dummy;
3002 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
3003 }
Dan Gohman59b4b102007-11-06 22:11:54 +00003004 } else {
3005 assert(MVT::isFloatingPoint(VT) &&
3006 "remainder op must have integer or floating-point type");
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003007 if (MVT::isVector(VT)) {
3008 Result = LegalizeOp(UnrollVectorOp(Op));
3009 } else {
3010 // Floating point mod -> fmod libcall.
Duncan Sands37a3f472008-01-10 10:28:30 +00003011 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3012 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman3e3fd8c2007-11-05 23:35:22 +00003013 SDOperand Dummy;
3014 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3015 false/*sign irrelevant*/, Dummy);
3016 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 }
3018 break;
3019 }
Dan Gohman5a199552007-10-08 18:33:35 +00003020 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021 break;
3022 case ISD::VAARG: {
3023 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3024 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3025
3026 MVT::ValueType VT = Node->getValueType(0);
3027 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3028 default: assert(0 && "This action is not supported yet!");
3029 case TargetLowering::Custom:
3030 isCustom = true;
3031 // FALLTHROUGH
3032 case TargetLowering::Legal:
3033 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3034 Result = Result.getValue(0);
3035 Tmp1 = Result.getValue(1);
3036
3037 if (isCustom) {
3038 Tmp2 = TLI.LowerOperation(Result, DAG);
3039 if (Tmp2.Val) {
3040 Result = LegalizeOp(Tmp2);
3041 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3042 }
3043 }
3044 break;
3045 case TargetLowering::Expand: {
3046 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
3047 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3048 SV->getValue(), SV->getOffset());
3049 // Increment the pointer, VAList, to the next vaarg
3050 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3051 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3052 TLI.getPointerTy()));
3053 // Store the incremented VAList to the legalized pointer
3054 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3055 SV->getOffset());
3056 // Load the actual argument out of the pointer VAList
3057 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
3058 Tmp1 = LegalizeOp(Result.getValue(1));
3059 Result = LegalizeOp(Result);
3060 break;
3061 }
3062 }
3063 // Since VAARG produces two values, make sure to remember that we
3064 // legalized both of them.
3065 AddLegalizedOperand(SDOperand(Node, 0), Result);
3066 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3067 return Op.ResNo ? Tmp1 : Result;
3068 }
3069
3070 case ISD::VACOPY:
3071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3072 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3073 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3074
3075 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3076 default: assert(0 && "This action is not supported yet!");
3077 case TargetLowering::Custom:
3078 isCustom = true;
3079 // FALLTHROUGH
3080 case TargetLowering::Legal:
3081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3082 Node->getOperand(3), Node->getOperand(4));
3083 if (isCustom) {
3084 Tmp1 = TLI.LowerOperation(Result, DAG);
3085 if (Tmp1.Val) Result = Tmp1;
3086 }
3087 break;
3088 case TargetLowering::Expand:
3089 // This defaults to loading a pointer from the input and storing it to the
3090 // output, returning the chain.
3091 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
3092 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
3093 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3094 SVD->getOffset());
3095 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3096 SVS->getOffset());
3097 break;
3098 }
3099 break;
3100
3101 case ISD::VAEND:
3102 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3103 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3104
3105 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3106 default: assert(0 && "This action is not supported yet!");
3107 case TargetLowering::Custom:
3108 isCustom = true;
3109 // FALLTHROUGH
3110 case TargetLowering::Legal:
3111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3112 if (isCustom) {
3113 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3114 if (Tmp1.Val) Result = Tmp1;
3115 }
3116 break;
3117 case TargetLowering::Expand:
3118 Result = Tmp1; // Default to a no-op, return the chain
3119 break;
3120 }
3121 break;
3122
3123 case ISD::VASTART:
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3126
3127 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3128
3129 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3130 default: assert(0 && "This action is not supported yet!");
3131 case TargetLowering::Legal: break;
3132 case TargetLowering::Custom:
3133 Tmp1 = TLI.LowerOperation(Result, DAG);
3134 if (Tmp1.Val) Result = Tmp1;
3135 break;
3136 }
3137 break;
3138
3139 case ISD::ROTL:
3140 case ISD::ROTR:
3141 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3142 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3144 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3145 default:
3146 assert(0 && "ROTL/ROTR legalize operation not supported");
3147 break;
3148 case TargetLowering::Legal:
3149 break;
3150 case TargetLowering::Custom:
3151 Tmp1 = TLI.LowerOperation(Result, DAG);
3152 if (Tmp1.Val) Result = Tmp1;
3153 break;
3154 case TargetLowering::Promote:
3155 assert(0 && "Do not know how to promote ROTL/ROTR");
3156 break;
3157 case TargetLowering::Expand:
3158 assert(0 && "Do not know how to expand ROTL/ROTR");
3159 break;
3160 }
3161 break;
3162
3163 case ISD::BSWAP:
3164 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3165 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3166 case TargetLowering::Custom:
3167 assert(0 && "Cannot custom legalize this yet!");
3168 case TargetLowering::Legal:
3169 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3170 break;
3171 case TargetLowering::Promote: {
3172 MVT::ValueType OVT = Tmp1.getValueType();
3173 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3174 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
3175
3176 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3177 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3178 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3179 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3180 break;
3181 }
3182 case TargetLowering::Expand:
3183 Result = ExpandBSWAP(Tmp1);
3184 break;
3185 }
3186 break;
3187
3188 case ISD::CTPOP:
3189 case ISD::CTTZ:
3190 case ISD::CTLZ:
3191 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3192 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel48b63e62007-07-30 21:00:31 +00003193 case TargetLowering::Custom:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 case TargetLowering::Legal:
3195 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel48b63e62007-07-30 21:00:31 +00003196 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michelbc62b412007-08-02 02:22:46 +00003197 TargetLowering::Custom) {
3198 Tmp1 = TLI.LowerOperation(Result, DAG);
3199 if (Tmp1.Val) {
3200 Result = Tmp1;
3201 }
Scott Michel48b63e62007-07-30 21:00:31 +00003202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 break;
3204 case TargetLowering::Promote: {
3205 MVT::ValueType OVT = Tmp1.getValueType();
3206 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3207
3208 // Zero extend the argument.
3209 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3210 // Perform the larger operation, then subtract if needed.
3211 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
3212 switch (Node->getOpcode()) {
3213 case ISD::CTPOP:
3214 Result = Tmp1;
3215 break;
3216 case ISD::CTTZ:
3217 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3218 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
3219 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3220 ISD::SETEQ);
3221 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel48b63e62007-07-30 21:00:31 +00003222 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223 break;
3224 case ISD::CTLZ:
3225 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3226 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3227 DAG.getConstant(MVT::getSizeInBits(NVT) -
3228 MVT::getSizeInBits(OVT), NVT));
3229 break;
3230 }
3231 break;
3232 }
3233 case TargetLowering::Expand:
3234 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
3235 break;
3236 }
3237 break;
3238
3239 // Unary operators
3240 case ISD::FABS:
3241 case ISD::FNEG:
3242 case ISD::FSQRT:
3243 case ISD::FSIN:
3244 case ISD::FCOS:
3245 Tmp1 = LegalizeOp(Node->getOperand(0));
3246 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3247 case TargetLowering::Promote:
3248 case TargetLowering::Custom:
3249 isCustom = true;
3250 // FALLTHROUGH
3251 case TargetLowering::Legal:
3252 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3253 if (isCustom) {
3254 Tmp1 = TLI.LowerOperation(Result, DAG);
3255 if (Tmp1.Val) Result = Tmp1;
3256 }
3257 break;
3258 case TargetLowering::Expand:
3259 switch (Node->getOpcode()) {
3260 default: assert(0 && "Unreachable!");
3261 case ISD::FNEG:
3262 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3263 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3264 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
3265 break;
3266 case ISD::FABS: {
3267 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3268 MVT::ValueType VT = Node->getValueType(0);
3269 Tmp2 = DAG.getConstantFP(0.0, VT);
3270 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
3271 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3272 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
3273 break;
3274 }
3275 case ISD::FSQRT:
3276 case ISD::FSIN:
3277 case ISD::FCOS: {
3278 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman6d05cac2007-10-11 23:57:53 +00003279
3280 // Expand unsupported unary vector operators by unrolling them.
3281 if (MVT::isVector(VT)) {
3282 Result = LegalizeOp(UnrollVectorOp(Op));
3283 break;
3284 }
3285
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3287 switch(Node->getOpcode()) {
3288 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00003289 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3290 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003291 break;
3292 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00003293 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3294 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003295 break;
3296 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00003297 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3298 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 break;
3300 default: assert(0 && "Unreachable!");
3301 }
3302 SDOperand Dummy;
3303 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3304 false/*sign irrelevant*/, Dummy);
3305 break;
3306 }
3307 }
3308 break;
3309 }
3310 break;
3311 case ISD::FPOWI: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00003312 MVT::ValueType VT = Node->getValueType(0);
3313
3314 // Expand unsupported unary vector operators by unrolling them.
3315 if (MVT::isVector(VT)) {
3316 Result = LegalizeOp(UnrollVectorOp(Op));
3317 break;
3318 }
3319
3320 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands37a3f472008-01-10 10:28:30 +00003321 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3322 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003323 SDOperand Dummy;
3324 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3325 false/*sign irrelevant*/, Dummy);
3326 break;
3327 }
3328 case ISD::BIT_CONVERT:
3329 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003330 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3331 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3333 // The input has to be a vector type, we have to either scalarize it, pack
3334 // it, or convert it based on whether the input vector type is legal.
3335 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesendb132452007-10-20 00:07:52 +00003336 int InIx = Node->getOperand(0).ResNo;
3337 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3338 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339
3340 // Figure out if there is a simple type corresponding to this Vector
3341 // type. If so, convert to the vector type.
3342 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3343 if (TLI.isTypeLegal(TVT)) {
3344 // Turn this into a bit convert of the vector input.
3345 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3346 LegalizeOp(Node->getOperand(0)));
3347 break;
3348 } else if (NumElems == 1) {
3349 // Turn this into a bit convert of the scalar input.
3350 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3351 ScalarizeVectorOp(Node->getOperand(0)));
3352 break;
3353 } else {
3354 // FIXME: UNIMP! Store then reload
3355 assert(0 && "Cast from unsupported vector type not implemented yet!");
3356 }
3357 } else {
3358 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3359 Node->getOperand(0).getValueType())) {
3360 default: assert(0 && "Unknown operation action!");
3361 case TargetLowering::Expand:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003362 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3363 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 break;
3365 case TargetLowering::Legal:
3366 Tmp1 = LegalizeOp(Node->getOperand(0));
3367 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3368 break;
3369 }
3370 }
3371 break;
3372
3373 // Conversion operators. The source and destination have different types.
3374 case ISD::SINT_TO_FP:
3375 case ISD::UINT_TO_FP: {
3376 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
3377 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3378 case Legal:
3379 switch (TLI.getOperationAction(Node->getOpcode(),
3380 Node->getOperand(0).getValueType())) {
3381 default: assert(0 && "Unknown operation action!");
3382 case TargetLowering::Custom:
3383 isCustom = true;
3384 // FALLTHROUGH
3385 case TargetLowering::Legal:
3386 Tmp1 = LegalizeOp(Node->getOperand(0));
3387 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3388 if (isCustom) {
3389 Tmp1 = TLI.LowerOperation(Result, DAG);
3390 if (Tmp1.Val) Result = Tmp1;
3391 }
3392 break;
3393 case TargetLowering::Expand:
3394 Result = ExpandLegalINT_TO_FP(isSigned,
3395 LegalizeOp(Node->getOperand(0)),
3396 Node->getValueType(0));
3397 break;
3398 case TargetLowering::Promote:
3399 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3400 Node->getValueType(0),
3401 isSigned);
3402 break;
3403 }
3404 break;
3405 case Expand:
3406 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3407 Node->getValueType(0), Node->getOperand(0));
3408 break;
3409 case Promote:
3410 Tmp1 = PromoteOp(Node->getOperand(0));
3411 if (isSigned) {
3412 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3413 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
3414 } else {
3415 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3416 Node->getOperand(0).getValueType());
3417 }
3418 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3419 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
3420 break;
3421 }
3422 break;
3423 }
3424 case ISD::TRUNCATE:
3425 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3426 case Legal:
3427 Tmp1 = LegalizeOp(Node->getOperand(0));
3428 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3429 break;
3430 case Expand:
3431 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3432
3433 // Since the result is legal, we should just be able to truncate the low
3434 // part of the source.
3435 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3436 break;
3437 case Promote:
3438 Result = PromoteOp(Node->getOperand(0));
3439 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3440 break;
3441 }
3442 break;
3443
3444 case ISD::FP_TO_SINT:
3445 case ISD::FP_TO_UINT:
3446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3447 case Legal:
3448 Tmp1 = LegalizeOp(Node->getOperand(0));
3449
3450 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3451 default: assert(0 && "Unknown operation action!");
3452 case TargetLowering::Custom:
3453 isCustom = true;
3454 // FALLTHROUGH
3455 case TargetLowering::Legal:
3456 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3457 if (isCustom) {
3458 Tmp1 = TLI.LowerOperation(Result, DAG);
3459 if (Tmp1.Val) Result = Tmp1;
3460 }
3461 break;
3462 case TargetLowering::Promote:
3463 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3464 Node->getOpcode() == ISD::FP_TO_SINT);
3465 break;
3466 case TargetLowering::Expand:
3467 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3468 SDOperand True, False;
3469 MVT::ValueType VT = Node->getOperand(0).getValueType();
3470 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen280620d2007-09-19 17:53:26 +00003471 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003472 const uint64_t zero[] = {0, 0};
3473 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3474 uint64_t x = 1ULL << ShiftAmt;
Neil Booth4bdd45a2007-10-07 11:45:55 +00003475 (void)apf.convertFromZeroExtendedInteger
3476 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00003477 Tmp2 = DAG.getConstantFP(apf, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003478 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3479 Node->getOperand(0), Tmp2, ISD::SETLT);
3480 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3481 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
3482 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
3483 Tmp2));
3484 False = DAG.getNode(ISD::XOR, NVT, False,
3485 DAG.getConstant(1ULL << ShiftAmt, NVT));
3486 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3487 break;
3488 } else {
3489 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3490 }
3491 break;
3492 }
3493 break;
3494 case Expand: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 MVT::ValueType VT = Op.getValueType();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003496 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesend3b6af32007-10-11 23:32:15 +00003497 // Convert ppcf128 to i32
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003498 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00003499 if (Node->getOpcode()==ISD::FP_TO_SINT)
3500 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003501 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3502 (DAG.getNode(ISD::FP_ROUND_INREG,
3503 MVT::ppcf128, Node->getOperand(0),
3504 DAG.getValueType(MVT::f64)))));
Dale Johannesend3b6af32007-10-11 23:32:15 +00003505 else {
3506 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3507 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3508 Tmp2 = DAG.getConstantFP(apf, OVT);
3509 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3510 // FIXME: generated code sucks.
3511 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3512 DAG.getNode(ISD::ADD, MVT::i32,
3513 DAG.getNode(ISD::FP_TO_SINT, VT,
3514 DAG.getNode(ISD::FSUB, OVT,
3515 Node->getOperand(0), Tmp2)),
3516 DAG.getConstant(0x80000000, MVT::i32)),
3517 DAG.getNode(ISD::FP_TO_SINT, VT,
3518 Node->getOperand(0)),
3519 DAG.getCondCode(ISD::SETGE));
3520 }
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003521 break;
3522 }
Dale Johannesend3b6af32007-10-11 23:32:15 +00003523 // Convert f32 / f64 to i32 / i64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3525 switch (Node->getOpcode()) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003526 case ISD::FP_TO_SINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003527 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 LC = (VT == MVT::i32)
3529 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003530 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003531 LC = (VT == MVT::i32)
3532 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003533 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003534 assert(VT == MVT::i64);
Dale Johannesenac77b272007-10-05 20:04:43 +00003535 LC = RTLIB::FPTOSINT_F80_I64;
3536 }
3537 else if (OVT == MVT::ppcf128) {
3538 assert(VT == MVT::i64);
3539 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003540 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003542 }
3543 case ISD::FP_TO_UINT: {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003544 if (OVT == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003545 LC = (VT == MVT::i32)
3546 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003547 else if (OVT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548 LC = (VT == MVT::i32)
3549 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00003550 else if (OVT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00003551 LC = (VT == MVT::i32)
Dale Johannesenac77b272007-10-05 20:04:43 +00003552 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3553 }
3554 else if (OVT == MVT::ppcf128) {
3555 assert(VT == MVT::i64);
3556 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003557 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00003559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003560 default: assert(0 && "Unreachable!");
3561 }
3562 SDOperand Dummy;
3563 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3564 false/*sign irrelevant*/, Dummy);
3565 break;
3566 }
3567 case Promote:
3568 Tmp1 = PromoteOp(Node->getOperand(0));
3569 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3570 Result = LegalizeOp(Result);
3571 break;
3572 }
3573 break;
3574
Chris Lattner56ecde32008-01-16 06:57:07 +00003575 case ISD::FP_EXTEND: {
3576 MVT::ValueType newVT = Op.getValueType();
3577 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3578 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
3579 // The only other way we can lower this is to turn it into a STORE,
3580 // LOAD pair, targetting a temporary location (a stack slot).
3581
3582 // NOTE: there is a choice here between constantly creating new stack
3583 // slots and always reusing the same one. We currently always create
3584 // new ones, as reuse may inhibit scheduling.
Chris Lattner0aeb1d02008-01-16 07:03:22 +00003585 SDOperand StackSlot = DAG.CreateStackTemporary(oldVT);
Chris Lattner56ecde32008-01-16 06:57:07 +00003586 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3587 StackSlot, NULL, 0);
3588 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003589 Result, StackSlot, NULL, 0, oldVT);
Chris Lattner56ecde32008-01-16 06:57:07 +00003590 break;
3591 }
3592 }
3593 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3594 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3595 case Legal:
3596 Tmp1 = LegalizeOp(Node->getOperand(0));
3597 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3598 break;
3599 case Promote:
3600 Tmp1 = PromoteOp(Node->getOperand(0));
3601 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3602 break;
3603 }
3604 break;
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003605 case ISD::FP_ROUND: {
3606 MVT::ValueType newVT = Op.getValueType();
3607 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3608 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Chris Lattner56ecde32008-01-16 06:57:07 +00003609 if (oldVT == MVT::ppcf128) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00003610 SDOperand Lo, Hi;
3611 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner56ecde32008-01-16 06:57:07 +00003612 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00003613 break;
Dale Johannesen60892372007-08-09 17:27:48 +00003614 } else {
Dale Johannesen472d15d2007-10-06 01:24:11 +00003615 // The only other way we can lower this is to turn it into a STORE,
3616 // LOAD pair, targetting a temporary location (a stack slot).
3617
3618 // NOTE: there is a choice here between constantly creating new stack
3619 // slots and always reusing the same one. We currently always create
3620 // new ones, as reuse may inhibit scheduling.
Chris Lattner0aeb1d02008-01-16 07:03:22 +00003621 SDOperand StackSlot = DAG.CreateStackTemporary(newVT);
Chris Lattner56ecde32008-01-16 06:57:07 +00003622 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3623 StackSlot, NULL, 0, newVT);
3624 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen472d15d2007-10-06 01:24:11 +00003625 break;
Dale Johannesen60892372007-08-09 17:27:48 +00003626 }
Dale Johannesen8f83a6b2007-08-09 01:04:01 +00003627 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003628 }
Chris Lattner56ecde32008-01-16 06:57:07 +00003629 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3630 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3631 case Legal:
3632 Tmp1 = LegalizeOp(Node->getOperand(0));
3633 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3634 break;
3635 case Promote:
3636 Tmp1 = PromoteOp(Node->getOperand(0));
3637 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1);
3638 break;
3639 }
3640 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003641 case ISD::ANY_EXTEND:
3642 case ISD::ZERO_EXTEND:
3643 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3645 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3646 case Legal:
3647 Tmp1 = LegalizeOp(Node->getOperand(0));
3648 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3649 break;
3650 case Promote:
3651 switch (Node->getOpcode()) {
3652 case ISD::ANY_EXTEND:
3653 Tmp1 = PromoteOp(Node->getOperand(0));
3654 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
3655 break;
3656 case ISD::ZERO_EXTEND:
3657 Result = PromoteOp(Node->getOperand(0));
3658 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3659 Result = DAG.getZeroExtendInReg(Result,
3660 Node->getOperand(0).getValueType());
3661 break;
3662 case ISD::SIGN_EXTEND:
3663 Result = PromoteOp(Node->getOperand(0));
3664 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
3665 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3666 Result,
3667 DAG.getValueType(Node->getOperand(0).getValueType()));
3668 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 }
3670 }
3671 break;
3672 case ISD::FP_ROUND_INREG:
3673 case ISD::SIGN_EXTEND_INREG: {
3674 Tmp1 = LegalizeOp(Node->getOperand(0));
3675 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3676
3677 // If this operation is not supported, convert it to a shl/shr or load/store
3678 // pair.
3679 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3680 default: assert(0 && "This action not supported for this op yet!");
3681 case TargetLowering::Legal:
3682 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
3683 break;
3684 case TargetLowering::Expand:
3685 // If this is an integer extend and shifts are supported, do that.
3686 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3687 // NOTE: we could fall back on load/store here too for targets without
3688 // SAR. However, it is doubtful that any exist.
3689 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3690 MVT::getSizeInBits(ExtraVT);
3691 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3692 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3693 Node->getOperand(0), ShiftCst);
3694 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3695 Result, ShiftCst);
3696 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
3697 // The only way we can lower this is to turn it into a TRUNCSTORE,
3698 // EXTLOAD pair, targetting a temporary location (a stack slot).
3699
3700 // NOTE: there is a choice here between constantly creating new stack
3701 // slots and always reusing the same one. We currently always create
3702 // new ones, as reuse may inhibit scheduling.
Chris Lattner0aeb1d02008-01-16 07:03:22 +00003703 SDOperand StackSlot = DAG.CreateStackTemporary(ExtraVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3705 StackSlot, NULL, 0, ExtraVT);
3706 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
3707 Result, StackSlot, NULL, 0, ExtraVT);
3708 } else {
3709 assert(0 && "Unknown op");
3710 }
3711 break;
3712 }
3713 break;
3714 }
Duncan Sands38947cd2007-07-27 12:58:54 +00003715 case ISD::TRAMPOLINE: {
3716 SDOperand Ops[6];
3717 for (unsigned i = 0; i != 6; ++i)
3718 Ops[i] = LegalizeOp(Node->getOperand(i));
3719 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3720 // The only option for this node is to custom lower it.
3721 Result = TLI.LowerOperation(Result, DAG);
3722 assert(Result.Val && "Should always custom lower!");
Duncan Sands7407a9f2007-09-11 14:10:23 +00003723
3724 // Since trampoline produces two values, make sure to remember that we
3725 // legalized both of them.
3726 Tmp1 = LegalizeOp(Result.getValue(1));
3727 Result = LegalizeOp(Result);
3728 AddLegalizedOperand(SDOperand(Node, 0), Result);
3729 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3730 return Op.ResNo ? Tmp1 : Result;
Duncan Sands38947cd2007-07-27 12:58:54 +00003731 }
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003732 case ISD::FLT_ROUNDS: {
3733 MVT::ValueType VT = Node->getValueType(0);
3734 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3735 default: assert(0 && "This action not supported for this op yet!");
3736 case TargetLowering::Custom:
3737 Result = TLI.LowerOperation(Op, DAG);
3738 if (Result.Val) break;
3739 // Fall Thru
3740 case TargetLowering::Legal:
3741 // If this operation is not supported, lower it to constant 1
3742 Result = DAG.getConstant(1, VT);
3743 break;
3744 }
3745 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003746 case ISD::TRAP: {
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003747 MVT::ValueType VT = Node->getValueType(0);
3748 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3749 default: assert(0 && "This action not supported for this op yet!");
Chris Lattnere99bbb72008-01-15 21:58:08 +00003750 case TargetLowering::Legal:
3751 Tmp1 = LegalizeOp(Node->getOperand(0));
3752 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3753 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003754 case TargetLowering::Custom:
3755 Result = TLI.LowerOperation(Op, DAG);
3756 if (Result.Val) break;
3757 // Fall Thru
Chris Lattnere99bbb72008-01-15 21:58:08 +00003758 case TargetLowering::Expand:
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003759 // If this operation is not supported, lower it to 'abort()' call
Chris Lattnere99bbb72008-01-15 21:58:08 +00003760 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003761 TargetLowering::ArgListTy Args;
3762 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattnere99bbb72008-01-15 21:58:08 +00003763 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner88e03932008-01-15 22:09:33 +00003764 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3765 Args, DAG);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003766 Result = CallResult.second;
3767 break;
3768 }
Chris Lattnere99bbb72008-01-15 21:58:08 +00003769 break;
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771 }
3772
3773 assert(Result.getValueType() == Op.getValueType() &&
3774 "Bad legalization!");
3775
3776 // Make sure that the generated code is itself legal.
3777 if (Result != Op)
3778 Result = LegalizeOp(Result);
3779
3780 // Note that LegalizeOp may be reentered even from single-use nodes, which
3781 // means that we always must cache transformed nodes.
3782 AddLegalizedOperand(Op, Result);
3783 return Result;
3784}
3785
3786/// PromoteOp - Given an operation that produces a value in an invalid type,
3787/// promote it to compute the value into a larger type. The produced value will
3788/// have the correct bits for the low portion of the register, but no guarantee
3789/// is made about the top bits: it may be zero, sign-extended, or garbage.
3790SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3791 MVT::ValueType VT = Op.getValueType();
3792 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3793 assert(getTypeAction(VT) == Promote &&
3794 "Caller should expand or legalize operands that are not promotable!");
3795 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3796 "Cannot promote to smaller type!");
3797
3798 SDOperand Tmp1, Tmp2, Tmp3;
3799 SDOperand Result;
3800 SDNode *Node = Op.Val;
3801
3802 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3803 if (I != PromotedNodes.end()) return I->second;
3804
3805 switch (Node->getOpcode()) {
3806 case ISD::CopyFromReg:
3807 assert(0 && "CopyFromReg must be legal!");
3808 default:
3809#ifndef NDEBUG
3810 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
3811#endif
3812 assert(0 && "Do not know how to promote this operator!");
3813 abort();
3814 case ISD::UNDEF:
3815 Result = DAG.getNode(ISD::UNDEF, NVT);
3816 break;
3817 case ISD::Constant:
3818 if (VT != MVT::i1)
3819 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3820 else
3821 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
3822 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3823 break;
3824 case ISD::ConstantFP:
3825 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3826 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3827 break;
3828
3829 case ISD::SETCC:
3830 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
3831 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3832 Node->getOperand(1), Node->getOperand(2));
3833 break;
3834
3835 case ISD::TRUNCATE:
3836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3837 case Legal:
3838 Result = LegalizeOp(Node->getOperand(0));
3839 assert(Result.getValueType() >= NVT &&
3840 "This truncation doesn't make sense!");
3841 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3842 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3843 break;
3844 case Promote:
3845 // The truncation is not required, because we don't guarantee anything
3846 // about high bits anyway.
3847 Result = PromoteOp(Node->getOperand(0));
3848 break;
3849 case Expand:
3850 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3851 // Truncate the low part of the expanded value to the result type
3852 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
3853 }
3854 break;
3855 case ISD::SIGN_EXTEND:
3856 case ISD::ZERO_EXTEND:
3857 case ISD::ANY_EXTEND:
3858 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3859 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3860 case Legal:
3861 // Input is legal? Just do extend all the way to the larger type.
3862 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
3863 break;
3864 case Promote:
3865 // Promote the reg if it's smaller.
3866 Result = PromoteOp(Node->getOperand(0));
3867 // The high bits are not guaranteed to be anything. Insert an extend.
3868 if (Node->getOpcode() == ISD::SIGN_EXTEND)
3869 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3870 DAG.getValueType(Node->getOperand(0).getValueType()));
3871 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
3872 Result = DAG.getZeroExtendInReg(Result,
3873 Node->getOperand(0).getValueType());
3874 break;
3875 }
3876 break;
3877 case ISD::BIT_CONVERT:
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00003878 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3879 Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880 Result = PromoteOp(Result);
3881 break;
3882
3883 case ISD::FP_EXTEND:
3884 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3885 case ISD::FP_ROUND:
3886 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3887 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3888 case Promote: assert(0 && "Unreachable with 2 FP types!");
3889 case Legal:
3890 // Input is legal? Do an FP_ROUND_INREG.
3891 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
3892 DAG.getValueType(VT));
3893 break;
3894 }
3895 break;
3896
3897 case ISD::SINT_TO_FP:
3898 case ISD::UINT_TO_FP:
3899 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3900 case Legal:
3901 // No extra round required here.
3902 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
3903 break;
3904
3905 case Promote:
3906 Result = PromoteOp(Node->getOperand(0));
3907 if (Node->getOpcode() == ISD::SINT_TO_FP)
3908 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
3909 Result,
3910 DAG.getValueType(Node->getOperand(0).getValueType()));
3911 else
3912 Result = DAG.getZeroExtendInReg(Result,
3913 Node->getOperand(0).getValueType());
3914 // No extra round required here.
3915 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
3916 break;
3917 case Expand:
3918 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3919 Node->getOperand(0));
3920 // Round if we cannot tolerate excess precision.
3921 if (NoExcessFPPrecision)
3922 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3923 DAG.getValueType(VT));
3924 break;
3925 }
3926 break;
3927
3928 case ISD::SIGN_EXTEND_INREG:
3929 Result = PromoteOp(Node->getOperand(0));
3930 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3931 Node->getOperand(1));
3932 break;
3933 case ISD::FP_TO_SINT:
3934 case ISD::FP_TO_UINT:
3935 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3936 case Legal:
3937 case Expand:
3938 Tmp1 = Node->getOperand(0);
3939 break;
3940 case Promote:
3941 // The input result is prerounded, so we don't have to do anything
3942 // special.
3943 Tmp1 = PromoteOp(Node->getOperand(0));
3944 break;
3945 }
3946 // If we're promoting a UINT to a larger size, check to see if the new node
3947 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3948 // we can use that instead. This allows us to generate better code for
3949 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3950 // legal, such as PowerPC.
3951 if (Node->getOpcode() == ISD::FP_TO_UINT &&
3952 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
3953 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3954 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
3955 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3956 } else {
3957 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3958 }
3959 break;
3960
3961 case ISD::FABS:
3962 case ISD::FNEG:
3963 Tmp1 = PromoteOp(Node->getOperand(0));
3964 assert(Tmp1.getValueType() == NVT);
3965 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3966 // NOTE: we do not have to do any extra rounding here for
3967 // NoExcessFPPrecision, because we know the input will have the appropriate
3968 // precision, and these operations don't modify precision at all.
3969 break;
3970
3971 case ISD::FSQRT:
3972 case ISD::FSIN:
3973 case ISD::FCOS:
3974 Tmp1 = PromoteOp(Node->getOperand(0));
3975 assert(Tmp1.getValueType() == NVT);
3976 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3977 if (NoExcessFPPrecision)
3978 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3979 DAG.getValueType(VT));
3980 break;
3981
3982 case ISD::FPOWI: {
3983 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3984 // directly as well, which may be better.
3985 Tmp1 = PromoteOp(Node->getOperand(0));
3986 assert(Tmp1.getValueType() == NVT);
3987 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3988 if (NoExcessFPPrecision)
3989 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3990 DAG.getValueType(VT));
3991 break;
3992 }
3993
3994 case ISD::AND:
3995 case ISD::OR:
3996 case ISD::XOR:
3997 case ISD::ADD:
3998 case ISD::SUB:
3999 case ISD::MUL:
4000 // The input may have strange things in the top bits of the registers, but
4001 // these operations don't care. They may have weird bits going out, but
4002 // that too is okay if they are integer operations.
4003 Tmp1 = PromoteOp(Node->getOperand(0));
4004 Tmp2 = PromoteOp(Node->getOperand(1));
4005 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4006 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4007 break;
4008 case ISD::FADD:
4009 case ISD::FSUB:
4010 case ISD::FMUL:
4011 Tmp1 = PromoteOp(Node->getOperand(0));
4012 Tmp2 = PromoteOp(Node->getOperand(1));
4013 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4014 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4015
4016 // Floating point operations will give excess precision that we may not be
4017 // able to tolerate. If we DO allow excess precision, just leave it,
4018 // otherwise excise it.
4019 // FIXME: Why would we need to round FP ops more than integer ones?
4020 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
4021 if (NoExcessFPPrecision)
4022 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4023 DAG.getValueType(VT));
4024 break;
4025
4026 case ISD::SDIV:
4027 case ISD::SREM:
4028 // These operators require that their input be sign extended.
4029 Tmp1 = PromoteOp(Node->getOperand(0));
4030 Tmp2 = PromoteOp(Node->getOperand(1));
4031 if (MVT::isInteger(NVT)) {
4032 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4033 DAG.getValueType(VT));
4034 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4035 DAG.getValueType(VT));
4036 }
4037 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4038
4039 // Perform FP_ROUND: this is probably overly pessimistic.
4040 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
4041 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4042 DAG.getValueType(VT));
4043 break;
4044 case ISD::FDIV:
4045 case ISD::FREM:
4046 case ISD::FCOPYSIGN:
4047 // These operators require that their input be fp extended.
4048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4049 case Legal:
4050 Tmp1 = LegalizeOp(Node->getOperand(0));
4051 break;
4052 case Promote:
4053 Tmp1 = PromoteOp(Node->getOperand(0));
4054 break;
4055 case Expand:
4056 assert(0 && "not implemented");
4057 }
4058 switch (getTypeAction(Node->getOperand(1).getValueType())) {
4059 case Legal:
4060 Tmp2 = LegalizeOp(Node->getOperand(1));
4061 break;
4062 case Promote:
4063 Tmp2 = PromoteOp(Node->getOperand(1));
4064 break;
4065 case Expand:
4066 assert(0 && "not implemented");
4067 }
4068 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4069
4070 // Perform FP_ROUND: this is probably overly pessimistic.
4071 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
4072 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4073 DAG.getValueType(VT));
4074 break;
4075
4076 case ISD::UDIV:
4077 case ISD::UREM:
4078 // These operators require that their input be zero extended.
4079 Tmp1 = PromoteOp(Node->getOperand(0));
4080 Tmp2 = PromoteOp(Node->getOperand(1));
4081 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
4082 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4083 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4084 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4085 break;
4086
4087 case ISD::SHL:
4088 Tmp1 = PromoteOp(Node->getOperand(0));
4089 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
4090 break;
4091 case ISD::SRA:
4092 // The input value must be properly sign extended.
4093 Tmp1 = PromoteOp(Node->getOperand(0));
4094 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4095 DAG.getValueType(VT));
4096 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
4097 break;
4098 case ISD::SRL:
4099 // The input value must be properly zero extended.
4100 Tmp1 = PromoteOp(Node->getOperand(0));
4101 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4102 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
4103 break;
4104
4105 case ISD::VAARG:
4106 Tmp1 = Node->getOperand(0); // Get the chain.
4107 Tmp2 = Node->getOperand(1); // Get the pointer.
4108 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4109 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4110 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4111 } else {
4112 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
4113 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
4114 SV->getValue(), SV->getOffset());
4115 // Increment the pointer, VAList, to the next vaarg
4116 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4117 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4118 TLI.getPointerTy()));
4119 // Store the incremented VAList to the legalized pointer
4120 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4121 SV->getOffset());
4122 // Load the actual argument out of the pointer VAList
4123 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
4124 }
4125 // Remember that we legalized the chain.
4126 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4127 break;
4128
4129 case ISD::LOAD: {
4130 LoadSDNode *LD = cast<LoadSDNode>(Node);
4131 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4132 ? ISD::EXTLOAD : LD->getExtensionType();
4133 Result = DAG.getExtLoad(ExtType, NVT,
4134 LD->getChain(), LD->getBasePtr(),
4135 LD->getSrcValue(), LD->getSrcValueOffset(),
4136 LD->getLoadedVT(),
4137 LD->isVolatile(),
4138 LD->getAlignment());
4139 // Remember that we legalized the chain.
4140 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4141 break;
4142 }
4143 case ISD::SELECT:
4144 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4145 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
4146 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
4147 break;
4148 case ISD::SELECT_CC:
4149 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4150 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4151 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4152 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
4153 break;
4154 case ISD::BSWAP:
4155 Tmp1 = Node->getOperand(0);
4156 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4157 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4158 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
4159 DAG.getConstant(MVT::getSizeInBits(NVT) -
4160 MVT::getSizeInBits(VT),
4161 TLI.getShiftAmountTy()));
4162 break;
4163 case ISD::CTPOP:
4164 case ISD::CTTZ:
4165 case ISD::CTLZ:
4166 // Zero extend the argument
4167 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
4168 // Perform the larger operation, then subtract if needed.
4169 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4170 switch(Node->getOpcode()) {
4171 case ISD::CTPOP:
4172 Result = Tmp1;
4173 break;
4174 case ISD::CTTZ:
4175 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
4176 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
4177 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4178 ISD::SETEQ);
4179 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
4180 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
4181 break;
4182 case ISD::CTLZ:
4183 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4184 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
4185 DAG.getConstant(MVT::getSizeInBits(NVT) -
4186 MVT::getSizeInBits(VT), NVT));
4187 break;
4188 }
4189 break;
4190 case ISD::EXTRACT_SUBVECTOR:
4191 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
4192 break;
4193 case ISD::EXTRACT_VECTOR_ELT:
4194 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4195 break;
4196 }
4197
4198 assert(Result.Val && "Didn't set a result!");
4199
4200 // Make sure the result is itself legal.
4201 Result = LegalizeOp(Result);
4202
4203 // Remember that we promoted this!
4204 AddPromotedOperand(Op, Result);
4205 return Result;
4206}
4207
4208/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4209/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4210/// based on the vector type. The return type of this matches the element type
4211/// of the vector, which may not be legal for the target.
4212SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
4213 // We know that operand #0 is the Vec vector. If the index is a constant
4214 // or if the invec is a supported hardware type, we can use it. Otherwise,
4215 // lower to a store then an indexed load.
4216 SDOperand Vec = Op.getOperand(0);
4217 SDOperand Idx = Op.getOperand(1);
4218
Dan Gohmana0763d92007-09-24 15:54:53 +00004219 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 unsigned NumElems = MVT::getVectorNumElements(TVT);
4221
4222 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4223 default: assert(0 && "This action is not supported yet!");
4224 case TargetLowering::Custom: {
4225 Vec = LegalizeOp(Vec);
4226 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4227 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4228 if (Tmp3.Val)
4229 return Tmp3;
4230 break;
4231 }
4232 case TargetLowering::Legal:
4233 if (isTypeLegal(TVT)) {
4234 Vec = LegalizeOp(Vec);
4235 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lambcc021a02007-07-26 03:33:13 +00004236 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 }
4238 break;
4239 case TargetLowering::Expand:
4240 break;
4241 }
4242
4243 if (NumElems == 1) {
4244 // This must be an access of the only element. Return it.
4245 Op = ScalarizeVectorOp(Vec);
4246 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4247 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4248 SDOperand Lo, Hi;
4249 SplitVectorOp(Vec, Lo, Hi);
4250 if (CIdx->getValue() < NumElems/2) {
4251 Vec = Lo;
4252 } else {
4253 Vec = Hi;
4254 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4255 Idx.getValueType());
4256 }
4257
4258 // It's now an extract from the appropriate high or low part. Recurse.
4259 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4260 Op = ExpandEXTRACT_VECTOR_ELT(Op);
4261 } else {
4262 // Store the value to a temporary stack slot, then LOAD the scalar
4263 // element back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004264 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4266
4267 // Add the offset to the index.
4268 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4269 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4270 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004271
4272 if (MVT::getSizeInBits(Idx.getValueType()) >
4273 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner9f9b8802007-10-19 16:47:35 +00004274 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004275 else
Chris Lattner9f9b8802007-10-19 16:47:35 +00004276 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling60f7b4d2007-10-18 08:32:37 +00004277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4279
4280 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
4281 }
4282 return Op;
4283}
4284
4285/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
4286/// we assume the operation can be split if it is not already legal.
4287SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
4288 // We know that operand #0 is the Vec vector. For now we assume the index
4289 // is a constant and that the extracted result is a supported hardware type.
4290 SDOperand Vec = Op.getOperand(0);
4291 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4292
4293 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
4294
4295 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4296 // This must be an access of the desired vector length. Return it.
4297 return Vec;
4298 }
4299
4300 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4301 SDOperand Lo, Hi;
4302 SplitVectorOp(Vec, Lo, Hi);
4303 if (CIdx->getValue() < NumElems/2) {
4304 Vec = Lo;
4305 } else {
4306 Vec = Hi;
4307 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4308 }
4309
4310 // It's now an extract from the appropriate high or low part. Recurse.
4311 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4312 return ExpandEXTRACT_SUBVECTOR(Op);
4313}
4314
4315/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4316/// with condition CC on the current target. This usually involves legalizing
4317/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4318/// there may be no choice but to create a new SetCC node to represent the
4319/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4320/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4321void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4322 SDOperand &RHS,
4323 SDOperand &CC) {
Dale Johannesen472d15d2007-10-06 01:24:11 +00004324 SDOperand Tmp1, Tmp2, Tmp3, Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325
4326 switch (getTypeAction(LHS.getValueType())) {
4327 case Legal:
4328 Tmp1 = LegalizeOp(LHS); // LHS
4329 Tmp2 = LegalizeOp(RHS); // RHS
4330 break;
4331 case Promote:
4332 Tmp1 = PromoteOp(LHS); // LHS
4333 Tmp2 = PromoteOp(RHS); // RHS
4334
4335 // If this is an FP compare, the operands have already been extended.
4336 if (MVT::isInteger(LHS.getValueType())) {
4337 MVT::ValueType VT = LHS.getValueType();
4338 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4339
4340 // Otherwise, we have to insert explicit sign or zero extends. Note
4341 // that we could insert sign extends for ALL conditions, but zero extend
4342 // is cheaper on many machines (an AND instead of two shifts), so prefer
4343 // it.
4344 switch (cast<CondCodeSDNode>(CC)->get()) {
4345 default: assert(0 && "Unknown integer comparison!");
4346 case ISD::SETEQ:
4347 case ISD::SETNE:
4348 case ISD::SETUGE:
4349 case ISD::SETUGT:
4350 case ISD::SETULE:
4351 case ISD::SETULT:
4352 // ALL of these operations will work if we either sign or zero extend
4353 // the operands (including the unsigned comparisons!). Zero extend is
4354 // usually a simpler/cheaper operation, so prefer it.
4355 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4356 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4357 break;
4358 case ISD::SETGE:
4359 case ISD::SETGT:
4360 case ISD::SETLT:
4361 case ISD::SETLE:
4362 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4363 DAG.getValueType(VT));
4364 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4365 DAG.getValueType(VT));
4366 break;
4367 }
4368 }
4369 break;
4370 case Expand: {
4371 MVT::ValueType VT = LHS.getValueType();
4372 if (VT == MVT::f32 || VT == MVT::f64) {
4373 // Expand into one or more soft-fp libcall(s).
4374 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
4375 switch (cast<CondCodeSDNode>(CC)->get()) {
4376 case ISD::SETEQ:
4377 case ISD::SETOEQ:
4378 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4379 break;
4380 case ISD::SETNE:
4381 case ISD::SETUNE:
4382 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
4383 break;
4384 case ISD::SETGE:
4385 case ISD::SETOGE:
4386 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4387 break;
4388 case ISD::SETLT:
4389 case ISD::SETOLT:
4390 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4391 break;
4392 case ISD::SETLE:
4393 case ISD::SETOLE:
4394 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4395 break;
4396 case ISD::SETGT:
4397 case ISD::SETOGT:
4398 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4399 break;
4400 case ISD::SETUO:
4401 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4402 break;
4403 case ISD::SETO:
4404 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
4405 break;
4406 default:
4407 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4408 switch (cast<CondCodeSDNode>(CC)->get()) {
4409 case ISD::SETONE:
4410 // SETONE = SETOLT | SETOGT
4411 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4412 // Fallthrough
4413 case ISD::SETUGT:
4414 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
4415 break;
4416 case ISD::SETUGE:
4417 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
4418 break;
4419 case ISD::SETULT:
4420 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
4421 break;
4422 case ISD::SETULE:
4423 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
4424 break;
4425 case ISD::SETUEQ:
4426 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
4427 break;
4428 default: assert(0 && "Unsupported FP setcc!");
4429 }
4430 }
4431
4432 SDOperand Dummy;
4433 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
4434 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4435 false /*sign irrelevant*/, Dummy);
4436 Tmp2 = DAG.getConstant(0, MVT::i32);
4437 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
4438 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
4439 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
4440 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
4441 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
4442 false /*sign irrelevant*/, Dummy);
4443 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
4444 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
4445 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4446 Tmp2 = SDOperand();
4447 }
4448 LHS = Tmp1;
4449 RHS = Tmp2;
4450 return;
4451 }
4452
4453 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4454 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen472d15d2007-10-06 01:24:11 +00004455 ExpandOp(RHS, RHSLo, RHSHi);
4456 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4457
4458 if (VT==MVT::ppcf128) {
4459 // FIXME: This generated code sucks. We want to generate
4460 // FCMP crN, hi1, hi2
4461 // BNE crN, L:
4462 // FCMP crN, lo1, lo2
4463 // The following can be improved, but not that much.
4464 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4465 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4466 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4467 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4468 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4469 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4470 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4471 Tmp2 = SDOperand();
4472 break;
4473 }
4474
4475 switch (CCCode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004476 case ISD::SETEQ:
4477 case ISD::SETNE:
4478 if (RHSLo == RHSHi)
4479 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4480 if (RHSCST->isAllOnesValue()) {
4481 // Comparison to -1.
4482 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4483 Tmp2 = RHSLo;
4484 break;
4485 }
4486
4487 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4488 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4489 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4490 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4491 break;
4492 default:
4493 // If this is a comparison of the sign bit, just look at the top part.
4494 // X > -1, x < 0
4495 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4496 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4497 CST->getValue() == 0) || // X < 0
4498 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4499 CST->isAllOnesValue())) { // X > -1
4500 Tmp1 = LHSHi;
4501 Tmp2 = RHSHi;
4502 break;
4503 }
4504
4505 // FIXME: This generated code sucks.
4506 ISD::CondCode LowCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 switch (CCCode) {
4508 default: assert(0 && "Unknown integer setcc!");
4509 case ISD::SETLT:
4510 case ISD::SETULT: LowCC = ISD::SETULT; break;
4511 case ISD::SETGT:
4512 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4513 case ISD::SETLE:
4514 case ISD::SETULE: LowCC = ISD::SETULE; break;
4515 case ISD::SETGE:
4516 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4517 }
4518
4519 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4520 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4521 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4522
4523 // NOTE: on targets without efficient SELECT of bools, we can always use
4524 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4525 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4526 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4527 false, DagCombineInfo);
4528 if (!Tmp1.Val)
4529 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4530 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4531 CCCode, false, DagCombineInfo);
4532 if (!Tmp2.Val)
Chris Lattner6fb53da2007-10-15 17:48:57 +00004533 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534
4535 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4536 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4537 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4538 (Tmp2C && Tmp2C->getValue() == 0 &&
4539 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4540 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4541 (Tmp2C && Tmp2C->getValue() == 1 &&
4542 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4543 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4544 // low part is known false, returns high part.
4545 // For LE / GE, if high part is known false, ignore the low part.
4546 // For LT / GT, if high part is known true, ignore the low part.
4547 Tmp1 = Tmp2;
4548 Tmp2 = SDOperand();
4549 } else {
4550 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4551 ISD::SETEQ, false, DagCombineInfo);
4552 if (!Result.Val)
4553 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4554 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4555 Result, Tmp1, Tmp2));
4556 Tmp1 = Result;
4557 Tmp2 = SDOperand();
4558 }
4559 }
4560 }
4561 }
4562 LHS = Tmp1;
4563 RHS = Tmp2;
4564}
4565
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004566/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4567/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4568/// a load from the stack slot to DestVT, extending it if needed.
4569/// The resultant code need not be legal.
4570SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4571 MVT::ValueType SlotVT,
4572 MVT::ValueType DestVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 // Create the stack frame object.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004574 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4575
4576 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4577 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4578 unsigned DestSize = MVT::getSizeInBits(DestVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004580 // Emit a store to the stack slot. Use a truncstore if the input value is
4581 // later than DestVT.
4582 SDOperand Store;
4583 if (SrcSize > SlotSize)
4584 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0,SlotVT);
4585 else {
4586 assert(SrcSize == SlotSize && "Invalid store");
4587 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
4588 }
4589
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590 // Result is a load from the stack slot.
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00004591 if (SlotSize == DestSize)
4592 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4593
4594 assert(SlotSize < DestSize && "Unknown extension!");
4595 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596}
4597
4598SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4599 // Create a vector sized/aligned stack slot, store the value to element #0,
4600 // then load the whole vector back out.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004601 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004602 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
4603 NULL, 0);
4604 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
4605}
4606
4607
4608/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
4609/// support the operation, but do support the resultant vector type.
4610SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4611
4612 // If the only non-undef value is the low element, turn this into a
4613 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
4614 unsigned NumElems = Node->getNumOperands();
4615 bool isOnlyLowElement = true;
4616 SDOperand SplatValue = Node->getOperand(0);
4617 std::map<SDOperand, std::vector<unsigned> > Values;
4618 Values[SplatValue].push_back(0);
4619 bool isConstant = true;
4620 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4621 SplatValue.getOpcode() != ISD::UNDEF)
4622 isConstant = false;
4623
4624 for (unsigned i = 1; i < NumElems; ++i) {
4625 SDOperand V = Node->getOperand(i);
4626 Values[V].push_back(i);
4627 if (V.getOpcode() != ISD::UNDEF)
4628 isOnlyLowElement = false;
4629 if (SplatValue != V)
4630 SplatValue = SDOperand(0,0);
4631
4632 // If this isn't a constant element or an undef, we can't use a constant
4633 // pool load.
4634 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4635 V.getOpcode() != ISD::UNDEF)
4636 isConstant = false;
4637 }
4638
4639 if (isOnlyLowElement) {
4640 // If the low element is an undef too, then this whole things is an undef.
4641 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4642 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4643 // Otherwise, turn this into a scalar_to_vector node.
4644 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4645 Node->getOperand(0));
4646 }
4647
4648 // If all elements are constants, create a load from the constant pool.
4649 if (isConstant) {
4650 MVT::ValueType VT = Node->getValueType(0);
4651 const Type *OpNTy =
4652 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4653 std::vector<Constant*> CV;
4654 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4655 if (ConstantFPSDNode *V =
4656 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenbbe2b702007-08-30 00:23:21 +00004657 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658 } else if (ConstantSDNode *V =
4659 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4660 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
4661 } else {
4662 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4663 CV.push_back(UndefValue::get(OpNTy));
4664 }
4665 }
4666 Constant *CP = ConstantVector::get(CV);
4667 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
4668 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
4669 }
4670
4671 if (SplatValue.Val) { // Splat of one value?
4672 // Build the shuffle constant vector: <0, 0, 0, 0>
4673 MVT::ValueType MaskVT =
4674 MVT::getIntVectorWithNumElements(NumElems);
4675 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
4676 std::vector<SDOperand> ZeroVec(NumElems, Zero);
4677 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4678 &ZeroVec[0], ZeroVec.size());
4679
4680 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4681 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
4682 // Get the splatted value into the low element of a vector register.
4683 SDOperand LowValVec =
4684 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4685
4686 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4687 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4688 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4689 SplatMask);
4690 }
4691 }
4692
4693 // If there are only two unique elements, we may be able to turn this into a
4694 // vector shuffle.
4695 if (Values.size() == 2) {
4696 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4697 MVT::ValueType MaskVT =
4698 MVT::getIntVectorWithNumElements(NumElems);
4699 std::vector<SDOperand> MaskVec(NumElems);
4700 unsigned i = 0;
4701 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4702 E = Values.end(); I != E; ++I) {
4703 for (std::vector<unsigned>::iterator II = I->second.begin(),
4704 EE = I->second.end(); II != EE; ++II)
4705 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
4706 i += NumElems;
4707 }
4708 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4709 &MaskVec[0], MaskVec.size());
4710
4711 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
4712 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4713 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
4714 SmallVector<SDOperand, 8> Ops;
4715 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4716 E = Values.end(); I != E; ++I) {
4717 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4718 I->first);
4719 Ops.push_back(Op);
4720 }
4721 Ops.push_back(ShuffleMask);
4722
4723 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
4724 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4725 &Ops[0], Ops.size());
4726 }
4727 }
4728
4729 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4730 // aligned object on the stack, store each element into it, then load
4731 // the result as a vector.
4732 MVT::ValueType VT = Node->getValueType(0);
4733 // Create the stack frame object.
Chris Lattner6fb53da2007-10-15 17:48:57 +00004734 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004735
4736 // Emit a store of each element to the stack slot.
4737 SmallVector<SDOperand, 8> Stores;
4738 unsigned TypeByteSize =
4739 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
4740 // Store (in the right endianness) the elements to memory.
4741 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4742 // Ignore undef elements.
4743 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4744
4745 unsigned Offset = TypeByteSize*i;
4746
4747 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4748 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4749
4750 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
4751 NULL, 0));
4752 }
4753
4754 SDOperand StoreChain;
4755 if (!Stores.empty()) // Not all undef elements?
4756 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4757 &Stores[0], Stores.size());
4758 else
4759 StoreChain = DAG.getEntryNode();
4760
4761 // Result is a load from the stack slot.
4762 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
4763}
4764
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004765void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4766 SDOperand Op, SDOperand Amt,
4767 SDOperand &Lo, SDOperand &Hi) {
4768 // Expand the subcomponents.
4769 SDOperand LHSL, LHSH;
4770 ExpandOp(Op, LHSL, LHSH);
4771
4772 SDOperand Ops[] = { LHSL, LHSH, Amt };
4773 MVT::ValueType VT = LHSL.getValueType();
4774 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
4775 Hi = Lo.getValue(1);
4776}
4777
4778
4779/// ExpandShift - Try to find a clever way to expand this shift operation out to
4780/// smaller elements. If we can't find a way that is more efficient than a
4781/// libcall on this target, return false. Otherwise, return true with the
4782/// low-parts expanded into Lo and Hi.
4783bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4784 SDOperand &Lo, SDOperand &Hi) {
4785 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4786 "This is not a shift!");
4787
4788 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
4789 SDOperand ShAmt = LegalizeOp(Amt);
4790 MVT::ValueType ShTy = ShAmt.getValueType();
4791 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4792 unsigned NVTBits = MVT::getSizeInBits(NVT);
4793
Chris Lattner8c931452007-10-14 20:35:12 +00004794 // Handle the case when Amt is an immediate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4796 unsigned Cst = CN->getValue();
4797 // Expand the incoming operand to be shifted, so that we have its parts
4798 SDOperand InL, InH;
4799 ExpandOp(Op, InL, InH);
4800 switch(Opc) {
4801 case ISD::SHL:
4802 if (Cst > VTBits) {
4803 Lo = DAG.getConstant(0, NVT);
4804 Hi = DAG.getConstant(0, NVT);
4805 } else if (Cst > NVTBits) {
4806 Lo = DAG.getConstant(0, NVT);
4807 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
4808 } else if (Cst == NVTBits) {
4809 Lo = DAG.getConstant(0, NVT);
4810 Hi = InL;
4811 } else {
4812 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4813 Hi = DAG.getNode(ISD::OR, NVT,
4814 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4815 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4816 }
4817 return true;
4818 case ISD::SRL:
4819 if (Cst > VTBits) {
4820 Lo = DAG.getConstant(0, NVT);
4821 Hi = DAG.getConstant(0, NVT);
4822 } else if (Cst > NVTBits) {
4823 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4824 Hi = DAG.getConstant(0, NVT);
4825 } else if (Cst == NVTBits) {
4826 Lo = InH;
4827 Hi = DAG.getConstant(0, NVT);
4828 } else {
4829 Lo = DAG.getNode(ISD::OR, NVT,
4830 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4831 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4832 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4833 }
4834 return true;
4835 case ISD::SRA:
4836 if (Cst > VTBits) {
4837 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
4838 DAG.getConstant(NVTBits-1, ShTy));
4839 } else if (Cst > NVTBits) {
4840 Lo = DAG.getNode(ISD::SRA, NVT, InH,
4841 DAG.getConstant(Cst-NVTBits, ShTy));
4842 Hi = DAG.getNode(ISD::SRA, NVT, InH,
4843 DAG.getConstant(NVTBits-1, ShTy));
4844 } else if (Cst == NVTBits) {
4845 Lo = InH;
4846 Hi = DAG.getNode(ISD::SRA, NVT, InH,
4847 DAG.getConstant(NVTBits-1, ShTy));
4848 } else {
4849 Lo = DAG.getNode(ISD::OR, NVT,
4850 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4851 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4852 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4853 }
4854 return true;
4855 }
4856 }
4857
4858 // Okay, the shift amount isn't constant. However, if we can tell that it is
4859 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4860 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4861 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4862
4863 // If we know that the high bit of the shift amount is one, then we can do
4864 // this as a couple of simple shifts.
4865 if (KnownOne & Mask) {
4866 // Mask out the high bit, which we know is set.
4867 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4868 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4869
4870 // Expand the incoming operand to be shifted, so that we have its parts
4871 SDOperand InL, InH;
4872 ExpandOp(Op, InL, InH);
4873 switch(Opc) {
4874 case ISD::SHL:
4875 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4876 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4877 return true;
4878 case ISD::SRL:
4879 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4880 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4881 return true;
4882 case ISD::SRA:
4883 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4884 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4885 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4886 return true;
4887 }
4888 }
4889
4890 // If we know that the high bit of the shift amount is zero, then we can do
4891 // this as a couple of simple shifts.
4892 if (KnownZero & Mask) {
4893 // Compute 32-amt.
4894 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4895 DAG.getConstant(NVTBits, Amt.getValueType()),
4896 Amt);
4897
4898 // Expand the incoming operand to be shifted, so that we have its parts
4899 SDOperand InL, InH;
4900 ExpandOp(Op, InL, InH);
4901 switch(Opc) {
4902 case ISD::SHL:
4903 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4904 Hi = DAG.getNode(ISD::OR, NVT,
4905 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4906 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4907 return true;
4908 case ISD::SRL:
4909 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4910 Lo = DAG.getNode(ISD::OR, NVT,
4911 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4912 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4913 return true;
4914 case ISD::SRA:
4915 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4916 Lo = DAG.getNode(ISD::OR, NVT,
4917 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4918 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4919 return true;
4920 }
4921 }
4922
4923 return false;
4924}
4925
4926
4927// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4928// does not fit into a register, return the lo part and set the hi part to the
4929// by-reg argument. If it does fit into a single register, return the result
4930// and leave the Hi part unset.
4931SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
4932 bool isSigned, SDOperand &Hi) {
4933 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4934 // The input chain to this libcall is the entry node of the function.
4935 // Legalizing the call will automatically add the previous call to the
4936 // dependence.
4937 SDOperand InChain = DAG.getEntryNode();
4938
4939 TargetLowering::ArgListTy Args;
4940 TargetLowering::ArgListEntry Entry;
4941 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4942 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4943 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
4944 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4945 Entry.isSExt = isSigned;
4946 Args.push_back(Entry);
4947 }
4948 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
4949
4950 // Splice the libcall in wherever FindInputOutputChains tells us to.
4951 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
4952 std::pair<SDOperand,SDOperand> CallInfo =
4953 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
4954 Callee, Args, DAG);
4955
4956 // Legalize the call sequence, starting with the chain. This will advance
4957 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4958 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4959 LegalizeOp(CallInfo.second);
4960 SDOperand Result;
4961 switch (getTypeAction(CallInfo.first.getValueType())) {
4962 default: assert(0 && "Unknown thing");
4963 case Legal:
4964 Result = CallInfo.first;
4965 break;
4966 case Expand:
4967 ExpandOp(CallInfo.first, Result, Hi);
4968 break;
4969 }
4970 return Result;
4971}
4972
4973
4974/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4975///
4976SDOperand SelectionDAGLegalize::
4977ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
4978 assert(getTypeAction(Source.getValueType()) == Expand &&
4979 "This is not an expansion!");
4980 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4981
4982 if (!isSigned) {
4983 assert(Source.getValueType() == MVT::i64 &&
4984 "This only works for 64-bit -> FP");
4985 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4986 // incoming integer is set. To handle this, we dynamically test to see if
4987 // it is set, and, if so, add a fudge factor.
4988 SDOperand Lo, Hi;
4989 ExpandOp(Source, Lo, Hi);
4990
4991 // If this is unsigned, and not supported, first perform the conversion to
4992 // signed, then adjust the result if the sign bit is set.
4993 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4994 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4995
4996 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4997 DAG.getConstant(0, Hi.getValueType()),
4998 ISD::SETLT);
4999 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5000 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5001 SignSet, Four, Zero);
5002 uint64_t FF = 0x5f800000ULL;
5003 if (TLI.isLittleEndian()) FF <<= 32;
5004 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5005
5006 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5007 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5008 SDOperand FudgeInReg;
5009 if (DestTy == MVT::f32)
5010 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005011 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005013 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen2fc20782007-09-14 22:26:36 +00005014 CPIdx, NULL, 0, MVT::f32);
5015 else
5016 assert(0 && "Unexpected conversion");
5017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018 MVT::ValueType SCVT = SignedConv.getValueType();
5019 if (SCVT != DestTy) {
5020 // Destination type needs to be expanded as well. The FADD now we are
5021 // constructing will be expanded into a libcall.
5022 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5023 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5024 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5025 SignedConv, SignedConv.getValue(1));
5026 }
5027 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5028 }
5029 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
5030 }
5031
5032 // Check to see if the target has a custom way to lower this. If so, use it.
5033 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5034 default: assert(0 && "This action not implemented for this operation!");
5035 case TargetLowering::Legal:
5036 case TargetLowering::Expand:
5037 break; // This case is handled below.
5038 case TargetLowering::Custom: {
5039 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5040 Source), DAG);
5041 if (NV.Val)
5042 return LegalizeOp(NV);
5043 break; // The target decided this was legal after all
5044 }
5045 }
5046
5047 // Expand the source, then glue it back together for the call. We must expand
5048 // the source in case it is shared (this pass of legalize must traverse it).
5049 SDOperand SrcLo, SrcHi;
5050 ExpandOp(Source, SrcLo, SrcHi);
5051 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5052
5053 RTLIB::Libcall LC;
5054 if (DestTy == MVT::f32)
5055 LC = RTLIB::SINTTOFP_I64_F32;
5056 else {
5057 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5058 LC = RTLIB::SINTTOFP_I64_F64;
5059 }
5060
5061 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
5062 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5063 SDOperand UnusedHiPart;
5064 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5065 UnusedHiPart);
5066}
5067
5068/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5069/// INT_TO_FP operation of the specified operand when the target requests that
5070/// we expand it. At this point, we know that the result and operand types are
5071/// legal for the target.
5072SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5073 SDOperand Op0,
5074 MVT::ValueType DestVT) {
5075 if (Op0.getValueType() == MVT::i32) {
5076 // simple 32-bit [signed|unsigned] integer to float/double expansion
5077
Chris Lattner0aeb1d02008-01-16 07:03:22 +00005078 // Get the stack frame index of a 8 byte buffer.
5079 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005081 // word offset constant for Hi/Lo address computation
5082 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5083 // set up Hi and Lo (into buffer) address based on endian
5084 SDOperand Hi = StackSlot;
5085 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5086 if (TLI.isLittleEndian())
5087 std::swap(Hi, Lo);
5088
5089 // if signed map to unsigned space
5090 SDOperand Op0Mapped;
5091 if (isSigned) {
5092 // constant used to invert sign bit (signed to unsigned mapping)
5093 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5094 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5095 } else {
5096 Op0Mapped = Op0;
5097 }
5098 // store the lo of the constructed double - based on integer input
5099 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
5100 Op0Mapped, Lo, NULL, 0);
5101 // initial hi portion of constructed double
5102 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5103 // store the hi of the constructed double - biased exponent
5104 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
5105 // load the constructed double
5106 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
5107 // FP constant to bias correct the final result
5108 SDOperand Bias = DAG.getConstantFP(isSigned ?
5109 BitsToDouble(0x4330000080000000ULL)
5110 : BitsToDouble(0x4330000000000000ULL),
5111 MVT::f64);
5112 // subtract the bias
5113 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5114 // final result
5115 SDOperand Result;
5116 // handle final rounding
5117 if (DestVT == MVT::f64) {
5118 // do nothing
5119 Result = Sub;
Dale Johannesenb17a7a22007-09-16 16:51:49 +00005120 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5121 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5122 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5123 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005124 }
5125 return Result;
5126 }
5127 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5128 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5129
5130 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5131 DAG.getConstant(0, Op0.getValueType()),
5132 ISD::SETLT);
5133 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5134 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5135 SignSet, Four, Zero);
5136
5137 // If the sign bit of the integer is set, the large number will be treated
5138 // as a negative number. To counteract this, the dynamic code adds an
5139 // offset depending on the data type.
5140 uint64_t FF;
5141 switch (Op0.getValueType()) {
5142 default: assert(0 && "Unsupported integer type!");
5143 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5144 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5145 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5146 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5147 }
5148 if (TLI.isLittleEndian()) FF <<= 32;
5149 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
5150
5151 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5152 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5153 SDOperand FudgeInReg;
5154 if (DestVT == MVT::f32)
5155 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
5156 else {
Dale Johannesen958b08b2007-09-19 23:55:34 +00005157 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005158 DAG.getEntryNode(), CPIdx,
5159 NULL, 0, MVT::f32));
5160 }
5161
5162 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5163}
5164
5165/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5166/// *INT_TO_FP operation of the specified operand when the target requests that
5167/// we promote it. At this point, we know that the result and operand types are
5168/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5169/// operation that takes a larger input.
5170SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5171 MVT::ValueType DestVT,
5172 bool isSigned) {
5173 // First step, figure out the appropriate *INT_TO_FP operation to use.
5174 MVT::ValueType NewInTy = LegalOp.getValueType();
5175
5176 unsigned OpToUse = 0;
5177
5178 // Scan for the appropriate larger type to use.
5179 while (1) {
5180 NewInTy = (MVT::ValueType)(NewInTy+1);
5181 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5182
5183 // If the target supports SINT_TO_FP of this type, use it.
5184 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5185 default: break;
5186 case TargetLowering::Legal:
5187 if (!TLI.isTypeLegal(NewInTy))
5188 break; // Can't use this datatype.
5189 // FALL THROUGH.
5190 case TargetLowering::Custom:
5191 OpToUse = ISD::SINT_TO_FP;
5192 break;
5193 }
5194 if (OpToUse) break;
5195 if (isSigned) continue;
5196
5197 // If the target supports UINT_TO_FP of this type, use it.
5198 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5199 default: break;
5200 case TargetLowering::Legal:
5201 if (!TLI.isTypeLegal(NewInTy))
5202 break; // Can't use this datatype.
5203 // FALL THROUGH.
5204 case TargetLowering::Custom:
5205 OpToUse = ISD::UINT_TO_FP;
5206 break;
5207 }
5208 if (OpToUse) break;
5209
5210 // Otherwise, try a larger type.
5211 }
5212
5213 // Okay, we found the operation and type to use. Zero extend our input to the
5214 // desired type then run the operation on it.
5215 return DAG.getNode(OpToUse, DestVT,
5216 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5217 NewInTy, LegalOp));
5218}
5219
5220/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5221/// FP_TO_*INT operation of the specified operand when the target requests that
5222/// we promote it. At this point, we know that the result and operand types are
5223/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5224/// operation that returns a larger result.
5225SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5226 MVT::ValueType DestVT,
5227 bool isSigned) {
5228 // First step, figure out the appropriate FP_TO*INT operation to use.
5229 MVT::ValueType NewOutTy = DestVT;
5230
5231 unsigned OpToUse = 0;
5232
5233 // Scan for the appropriate larger type to use.
5234 while (1) {
5235 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5236 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5237
5238 // If the target supports FP_TO_SINT returning this type, use it.
5239 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5240 default: break;
5241 case TargetLowering::Legal:
5242 if (!TLI.isTypeLegal(NewOutTy))
5243 break; // Can't use this datatype.
5244 // FALL THROUGH.
5245 case TargetLowering::Custom:
5246 OpToUse = ISD::FP_TO_SINT;
5247 break;
5248 }
5249 if (OpToUse) break;
5250
5251 // If the target supports FP_TO_UINT of this type, use it.
5252 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5253 default: break;
5254 case TargetLowering::Legal:
5255 if (!TLI.isTypeLegal(NewOutTy))
5256 break; // Can't use this datatype.
5257 // FALL THROUGH.
5258 case TargetLowering::Custom:
5259 OpToUse = ISD::FP_TO_UINT;
5260 break;
5261 }
5262 if (OpToUse) break;
5263
5264 // Otherwise, try a larger type.
5265 }
5266
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005267
5268 // Okay, we found the operation and type to use.
5269 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5270
5271 // If the operation produces an invalid type, it must be custom lowered. Use
5272 // the target lowering hooks to expand it. Just keep the low part of the
5273 // expanded operation, we know that we're truncating anyway.
5274 if (getTypeAction(NewOutTy) == Expand) {
5275 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5276 assert(Operation.Val && "Didn't return anything");
5277 }
5278
5279 // Truncate the result of the extended FP_TO_*INT operation to the desired
5280 // size.
5281 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005282}
5283
5284/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5285///
5286SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5287 MVT::ValueType VT = Op.getValueType();
5288 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5289 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5290 switch (VT) {
5291 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5292 case MVT::i16:
5293 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5294 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5295 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5296 case MVT::i32:
5297 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5298 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5299 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5300 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5301 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5302 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5303 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5304 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5305 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5306 case MVT::i64:
5307 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5308 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5309 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5310 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5311 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5312 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5313 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5314 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5315 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5316 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5317 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5318 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5319 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5320 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5321 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5322 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5323 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5324 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5325 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5326 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5327 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5328 }
5329}
5330
5331/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5332///
5333SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5334 switch (Opc) {
5335 default: assert(0 && "Cannot expand this yet!");
5336 case ISD::CTPOP: {
5337 static const uint64_t mask[6] = {
5338 0x5555555555555555ULL, 0x3333333333333333ULL,
5339 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5340 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5341 };
5342 MVT::ValueType VT = Op.getValueType();
5343 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5344 unsigned len = MVT::getSizeInBits(VT);
5345 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5346 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5347 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5348 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5349 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5350 DAG.getNode(ISD::AND, VT,
5351 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5352 }
5353 return Op;
5354 }
5355 case ISD::CTLZ: {
5356 // for now, we do this:
5357 // x = x | (x >> 1);
5358 // x = x | (x >> 2);
5359 // ...
5360 // x = x | (x >>16);
5361 // x = x | (x >>32); // for 64-bit input
5362 // return popcount(~x);
5363 //
5364 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5365 MVT::ValueType VT = Op.getValueType();
5366 MVT::ValueType ShVT = TLI.getShiftAmountTy();
5367 unsigned len = MVT::getSizeInBits(VT);
5368 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5369 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5370 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5371 }
5372 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5373 return DAG.getNode(ISD::CTPOP, VT, Op);
5374 }
5375 case ISD::CTTZ: {
5376 // for now, we use: { return popcount(~x & (x - 1)); }
5377 // unless the target has ctlz but not ctpop, in which case we use:
5378 // { return 32 - nlz(~x & (x-1)); }
5379 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5380 MVT::ValueType VT = Op.getValueType();
5381 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5382 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5383 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5384 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5385 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5386 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5387 TLI.isOperationLegal(ISD::CTLZ, VT))
5388 return DAG.getNode(ISD::SUB, VT,
5389 DAG.getConstant(MVT::getSizeInBits(VT), VT),
5390 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5391 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5392 }
5393 }
5394}
5395
5396/// ExpandOp - Expand the specified SDOperand into its two component pieces
5397/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5398/// LegalizeNodes map is filled in for any results that are not expanded, the
5399/// ExpandedNodes map is filled in for any results that are expanded, and the
5400/// Lo/Hi values are returned.
5401void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5402 MVT::ValueType VT = Op.getValueType();
5403 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
5404 SDNode *Node = Op.Val;
5405 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
5406 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
5407 MVT::isVector(VT)) &&
5408 "Cannot expand to FP value or to larger int value!");
5409
5410 // See if we already expanded it.
5411 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5412 = ExpandedNodes.find(Op);
5413 if (I != ExpandedNodes.end()) {
5414 Lo = I->second.first;
5415 Hi = I->second.second;
5416 return;
5417 }
5418
5419 switch (Node->getOpcode()) {
5420 case ISD::CopyFromReg:
5421 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005422 case ISD::FP_ROUND_INREG:
5423 if (VT == MVT::ppcf128 &&
5424 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5425 TargetLowering::Custom) {
Dale Johannesend3b6af32007-10-11 23:32:15 +00005426 SDOperand SrcLo, SrcHi, Src;
5427 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5428 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5429 SDOperand Result = TLI.LowerOperation(
5430 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00005431 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5432 Lo = Result.Val->getOperand(0);
5433 Hi = Result.Val->getOperand(1);
5434 break;
5435 }
5436 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005437 default:
5438#ifndef NDEBUG
5439 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
5440#endif
5441 assert(0 && "Do not know how to expand this operator!");
5442 abort();
Dale Johannesen2ff963d2007-10-31 00:32:36 +00005443 case ISD::EXTRACT_VECTOR_ELT:
5444 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5445 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5446 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5447 return ExpandOp(Lo, Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005448 case ISD::UNDEF:
5449 NVT = TLI.getTypeToExpandTo(VT);
5450 Lo = DAG.getNode(ISD::UNDEF, NVT);
5451 Hi = DAG.getNode(ISD::UNDEF, NVT);
5452 break;
5453 case ISD::Constant: {
5454 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5455 Lo = DAG.getConstant(Cst, NVT);
5456 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5457 break;
5458 }
5459 case ISD::ConstantFP: {
5460 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen2aef5692007-10-11 18:07:22 +00005461 if (CFP->getValueType(0) == MVT::ppcf128) {
5462 APInt api = CFP->getValueAPF().convertToAPInt();
5463 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5464 MVT::f64);
5465 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5466 MVT::f64);
5467 break;
5468 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005469 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
5470 if (getTypeAction(Lo.getValueType()) == Expand)
5471 ExpandOp(Lo, Lo, Hi);
5472 break;
5473 }
5474 case ISD::BUILD_PAIR:
5475 // Return the operands.
5476 Lo = Node->getOperand(0);
5477 Hi = Node->getOperand(1);
5478 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005479
5480 case ISD::MERGE_VALUES:
Chris Lattner1b66f822007-11-24 19:12:15 +00005481 if (Node->getNumValues() == 1) {
5482 ExpandOp(Op.getOperand(0), Lo, Hi);
5483 break;
5484 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005485 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5486 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5487 Op.getValue(1).getValueType() == MVT::Other &&
5488 "unhandled MERGE_VALUES");
5489 ExpandOp(Op.getOperand(0), Lo, Hi);
5490 // Remember that we legalized the chain.
5491 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5492 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493
5494 case ISD::SIGN_EXTEND_INREG:
5495 ExpandOp(Node->getOperand(0), Lo, Hi);
5496 // sext_inreg the low part if needed.
5497 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5498
5499 // The high part gets the sign extension from the lo-part. This handles
5500 // things like sextinreg V:i64 from i8.
5501 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5502 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5503 TLI.getShiftAmountTy()));
5504 break;
5505
5506 case ISD::BSWAP: {
5507 ExpandOp(Node->getOperand(0), Lo, Hi);
5508 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5509 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5510 Lo = TempLo;
5511 break;
5512 }
5513
5514 case ISD::CTPOP:
5515 ExpandOp(Node->getOperand(0), Lo, Hi);
5516 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5517 DAG.getNode(ISD::CTPOP, NVT, Lo),
5518 DAG.getNode(ISD::CTPOP, NVT, Hi));
5519 Hi = DAG.getConstant(0, NVT);
5520 break;
5521
5522 case ISD::CTLZ: {
5523 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
5524 ExpandOp(Node->getOperand(0), Lo, Hi);
5525 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5526 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5527 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5528 ISD::SETNE);
5529 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5530 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5531
5532 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5533 Hi = DAG.getConstant(0, NVT);
5534 break;
5535 }
5536
5537 case ISD::CTTZ: {
5538 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
5539 ExpandOp(Node->getOperand(0), Lo, Hi);
5540 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5541 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5542 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5543 ISD::SETNE);
5544 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5545 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5546
5547 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5548 Hi = DAG.getConstant(0, NVT);
5549 break;
5550 }
5551
5552 case ISD::VAARG: {
5553 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5554 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5555 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5556 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5557
5558 // Remember that we legalized the chain.
5559 Hi = LegalizeOp(Hi);
5560 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5561 if (!TLI.isLittleEndian())
5562 std::swap(Lo, Hi);
5563 break;
5564 }
5565
5566 case ISD::LOAD: {
5567 LoadSDNode *LD = cast<LoadSDNode>(Node);
5568 SDOperand Ch = LD->getChain(); // Legalize the chain.
5569 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5570 ISD::LoadExtType ExtType = LD->getExtensionType();
5571 int SVOffset = LD->getSrcValueOffset();
5572 unsigned Alignment = LD->getAlignment();
5573 bool isVolatile = LD->isVolatile();
5574
5575 if (ExtType == ISD::NON_EXTLOAD) {
5576 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5577 isVolatile, Alignment);
5578 if (VT == MVT::f32 || VT == MVT::f64) {
5579 // f32->i32 or f64->i64 one to one expansion.
5580 // Remember that we legalized the chain.
5581 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5582 // Recursively expand the new load.
5583 if (getTypeAction(NVT) == Expand)
5584 ExpandOp(Lo, Lo, Hi);
5585 break;
5586 }
5587
5588 // Increment the pointer to the other half.
5589 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5590 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5591 getIntPtrConstant(IncrementSize));
5592 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00005593 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5595 isVolatile, Alignment);
5596
5597 // Build a factor node to remember that this load is independent of the
5598 // other one.
5599 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5600 Hi.getValue(1));
5601
5602 // Remember that we legalized the chain.
5603 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5604 if (!TLI.isLittleEndian())
5605 std::swap(Lo, Hi);
5606 } else {
5607 MVT::ValueType EVT = LD->getLoadedVT();
5608
Dale Johannesen2550e3a2007-10-19 20:29:00 +00005609 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5610 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5612 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
5613 SVOffset, isVolatile, Alignment);
5614 // Remember that we legalized the chain.
5615 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5616 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5617 break;
5618 }
5619
5620 if (EVT == NVT)
5621 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
5622 SVOffset, isVolatile, Alignment);
5623 else
5624 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
5625 SVOffset, EVT, isVolatile,
5626 Alignment);
5627
5628 // Remember that we legalized the chain.
5629 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5630
5631 if (ExtType == ISD::SEXTLOAD) {
5632 // The high part is obtained by SRA'ing all but one of the bits of the
5633 // lo part.
5634 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5635 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5636 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5637 } else if (ExtType == ISD::ZEXTLOAD) {
5638 // The high part is just a zero.
5639 Hi = DAG.getConstant(0, NVT);
5640 } else /* if (ExtType == ISD::EXTLOAD) */ {
5641 // The high part is undefined.
5642 Hi = DAG.getNode(ISD::UNDEF, NVT);
5643 }
5644 }
5645 break;
5646 }
5647 case ISD::AND:
5648 case ISD::OR:
5649 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5650 SDOperand LL, LH, RL, RH;
5651 ExpandOp(Node->getOperand(0), LL, LH);
5652 ExpandOp(Node->getOperand(1), RL, RH);
5653 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5654 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5655 break;
5656 }
5657 case ISD::SELECT: {
5658 SDOperand LL, LH, RL, RH;
5659 ExpandOp(Node->getOperand(1), LL, LH);
5660 ExpandOp(Node->getOperand(2), RL, RH);
5661 if (getTypeAction(NVT) == Expand)
5662 NVT = TLI.getTypeToExpandTo(NVT);
5663 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
5664 if (VT != MVT::f32)
5665 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
5666 break;
5667 }
5668 case ISD::SELECT_CC: {
5669 SDOperand TL, TH, FL, FH;
5670 ExpandOp(Node->getOperand(2), TL, TH);
5671 ExpandOp(Node->getOperand(3), FL, FH);
5672 if (getTypeAction(NVT) == Expand)
5673 NVT = TLI.getTypeToExpandTo(NVT);
5674 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5675 Node->getOperand(1), TL, FL, Node->getOperand(4));
5676 if (VT != MVT::f32)
5677 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5678 Node->getOperand(1), TH, FH, Node->getOperand(4));
5679 break;
5680 }
5681 case ISD::ANY_EXTEND:
5682 // The low part is any extension of the input (which degenerates to a copy).
5683 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5684 // The high part is undefined.
5685 Hi = DAG.getNode(ISD::UNDEF, NVT);
5686 break;
5687 case ISD::SIGN_EXTEND: {
5688 // The low part is just a sign extension of the input (which degenerates to
5689 // a copy).
5690 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
5691
5692 // The high part is obtained by SRA'ing all but one of the bits of the lo
5693 // part.
5694 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5695 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5696 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5697 break;
5698 }
5699 case ISD::ZERO_EXTEND:
5700 // The low part is just a zero extension of the input (which degenerates to
5701 // a copy).
5702 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
5703
5704 // The high part is just a zero.
5705 Hi = DAG.getConstant(0, NVT);
5706 break;
5707
5708 case ISD::TRUNCATE: {
5709 // The input value must be larger than this value. Expand *it*.
5710 SDOperand NewLo;
5711 ExpandOp(Node->getOperand(0), NewLo, Hi);
5712
5713 // The low part is now either the right size, or it is closer. If not the
5714 // right size, make an illegal truncate so we recursively expand it.
5715 if (NewLo.getValueType() != Node->getValueType(0))
5716 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5717 ExpandOp(NewLo, Lo, Hi);
5718 break;
5719 }
5720
5721 case ISD::BIT_CONVERT: {
5722 SDOperand Tmp;
5723 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5724 // If the target wants to, allow it to lower this itself.
5725 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5726 case Expand: assert(0 && "cannot expand FP!");
5727 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5728 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5729 }
5730 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5731 }
5732
5733 // f32 / f64 must be expanded to i32 / i64.
5734 if (VT == MVT::f32 || VT == MVT::f64) {
5735 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5736 if (getTypeAction(NVT) == Expand)
5737 ExpandOp(Lo, Lo, Hi);
5738 break;
5739 }
5740
5741 // If source operand will be expanded to the same type as VT, i.e.
5742 // i64 <- f64, i32 <- f32, expand the source operand instead.
5743 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5744 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5745 ExpandOp(Node->getOperand(0), Lo, Hi);
5746 break;
5747 }
5748
5749 // Turn this into a load/store pair by default.
5750 if (Tmp.Val == 0)
Chris Lattnerb7d0aaa2008-01-16 07:45:30 +00005751 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752
5753 ExpandOp(Tmp, Lo, Hi);
5754 break;
5755 }
5756
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005757 case ISD::READCYCLECOUNTER: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005758 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5759 TargetLowering::Custom &&
5760 "Must custom expand ReadCycleCounter");
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005761 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5762 assert(Tmp.Val && "Node must be custom expanded!");
5763 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005765 LegalizeOp(Tmp.getValue(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005766 break;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005767 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005768
5769 // These operators cannot be expanded directly, emit them as calls to
5770 // library functions.
5771 case ISD::FP_TO_SINT: {
5772 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
5773 SDOperand Op;
5774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5775 case Expand: assert(0 && "cannot expand FP!");
5776 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5777 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5778 }
5779
5780 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5781
5782 // Now that the custom expander is done, expand the result, which is still
5783 // VT.
5784 if (Op.Val) {
5785 ExpandOp(Op, Lo, Hi);
5786 break;
5787 }
5788 }
5789
Dale Johannesenac77b272007-10-05 20:04:43 +00005790 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791 if (Node->getOperand(0).getValueType() == MVT::f32)
5792 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen958b08b2007-09-19 23:55:34 +00005793 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00005795 else if (Node->getOperand(0).getValueType() == MVT::f80)
5796 LC = RTLIB::FPTOSINT_F80_I64;
5797 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5798 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005799 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5800 false/*sign irrelevant*/, Hi);
5801 break;
5802 }
5803
5804 case ISD::FP_TO_UINT: {
5805 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
5806 SDOperand Op;
5807 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5808 case Expand: assert(0 && "cannot expand FP!");
5809 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5810 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5811 }
5812
5813 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5814
5815 // Now that the custom expander is done, expand the result.
5816 if (Op.Val) {
5817 ExpandOp(Op, Lo, Hi);
5818 break;
5819 }
5820 }
5821
Evan Cheng9bdaeaa2007-10-05 01:09:32 +00005822 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823 if (Node->getOperand(0).getValueType() == MVT::f32)
5824 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen4e1cf5d2007-09-28 18:44:17 +00005825 else if (Node->getOperand(0).getValueType() == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenac77b272007-10-05 20:04:43 +00005827 else if (Node->getOperand(0).getValueType() == MVT::f80)
5828 LC = RTLIB::FPTOUINT_F80_I64;
5829 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5830 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005831 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5832 false/*sign irrelevant*/, Hi);
5833 break;
5834 }
5835
5836 case ISD::SHL: {
5837 // If the target wants custom lowering, do so.
5838 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5839 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
5840 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
5841 Op = TLI.LowerOperation(Op, DAG);
5842 if (Op.Val) {
5843 // Now that the custom expander is done, expand the result, which is
5844 // still VT.
5845 ExpandOp(Op, Lo, Hi);
5846 break;
5847 }
5848 }
5849
5850 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5851 // this X << 1 as X+X.
5852 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5853 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5854 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5855 SDOperand LoOps[2], HiOps[3];
5856 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5857 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5858 LoOps[1] = LoOps[0];
5859 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5860
5861 HiOps[1] = HiOps[0];
5862 HiOps[2] = Lo.getValue(1);
5863 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5864 break;
5865 }
5866 }
5867
5868 // If we can emit an efficient shift operation, do so now.
5869 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
5870 break;
5871
5872 // If this target supports SHL_PARTS, use it.
5873 TargetLowering::LegalizeAction Action =
5874 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5875 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5876 Action == TargetLowering::Custom) {
5877 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5878 break;
5879 }
5880
5881 // Otherwise, emit a libcall.
5882 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5883 false/*left shift=unsigned*/, Hi);
5884 break;
5885 }
5886
5887 case ISD::SRA: {
5888 // If the target wants custom lowering, do so.
5889 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5890 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
5891 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
5892 Op = TLI.LowerOperation(Op, DAG);
5893 if (Op.Val) {
5894 // Now that the custom expander is done, expand the result, which is
5895 // still VT.
5896 ExpandOp(Op, Lo, Hi);
5897 break;
5898 }
5899 }
5900
5901 // If we can emit an efficient shift operation, do so now.
5902 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
5903 break;
5904
5905 // If this target supports SRA_PARTS, use it.
5906 TargetLowering::LegalizeAction Action =
5907 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5908 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5909 Action == TargetLowering::Custom) {
5910 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5911 break;
5912 }
5913
5914 // Otherwise, emit a libcall.
5915 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5916 true/*ashr is signed*/, Hi);
5917 break;
5918 }
5919
5920 case ISD::SRL: {
5921 // If the target wants custom lowering, do so.
5922 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
5923 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
5924 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
5925 Op = TLI.LowerOperation(Op, DAG);
5926 if (Op.Val) {
5927 // Now that the custom expander is done, expand the result, which is
5928 // still VT.
5929 ExpandOp(Op, Lo, Hi);
5930 break;
5931 }
5932 }
5933
5934 // If we can emit an efficient shift operation, do so now.
5935 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
5936 break;
5937
5938 // If this target supports SRL_PARTS, use it.
5939 TargetLowering::LegalizeAction Action =
5940 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5941 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5942 Action == TargetLowering::Custom) {
5943 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
5944 break;
5945 }
5946
5947 // Otherwise, emit a libcall.
5948 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5949 false/*lshr is unsigned*/, Hi);
5950 break;
5951 }
5952
5953 case ISD::ADD:
5954 case ISD::SUB: {
5955 // If the target wants to custom expand this, let them.
5956 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5957 TargetLowering::Custom) {
5958 Op = TLI.LowerOperation(Op, DAG);
5959 if (Op.Val) {
5960 ExpandOp(Op, Lo, Hi);
5961 break;
5962 }
5963 }
5964
5965 // Expand the subcomponents.
5966 SDOperand LHSL, LHSH, RHSL, RHSH;
5967 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5968 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5969 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5970 SDOperand LoOps[2], HiOps[3];
5971 LoOps[0] = LHSL;
5972 LoOps[1] = RHSL;
5973 HiOps[0] = LHSH;
5974 HiOps[1] = RHSH;
5975 if (Node->getOpcode() == ISD::ADD) {
5976 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5977 HiOps[2] = Lo.getValue(1);
5978 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5979 } else {
5980 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5981 HiOps[2] = Lo.getValue(1);
5982 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5983 }
5984 break;
5985 }
5986
5987 case ISD::ADDC:
5988 case ISD::SUBC: {
5989 // Expand the subcomponents.
5990 SDOperand LHSL, LHSH, RHSL, RHSH;
5991 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5992 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5993 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5994 SDOperand LoOps[2] = { LHSL, RHSL };
5995 SDOperand HiOps[3] = { LHSH, RHSH };
5996
5997 if (Node->getOpcode() == ISD::ADDC) {
5998 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5999 HiOps[2] = Lo.getValue(1);
6000 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6001 } else {
6002 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6003 HiOps[2] = Lo.getValue(1);
6004 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6005 }
6006 // Remember that we legalized the flag.
6007 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6008 break;
6009 }
6010 case ISD::ADDE:
6011 case ISD::SUBE: {
6012 // Expand the subcomponents.
6013 SDOperand LHSL, LHSH, RHSL, RHSH;
6014 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6015 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6016 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6017 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6018 SDOperand HiOps[3] = { LHSH, RHSH };
6019
6020 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6021 HiOps[2] = Lo.getValue(1);
6022 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6023
6024 // Remember that we legalized the flag.
6025 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6026 break;
6027 }
6028 case ISD::MUL: {
6029 // If the target wants to custom expand this, let them.
6030 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
6031 SDOperand New = TLI.LowerOperation(Op, DAG);
6032 if (New.Val) {
6033 ExpandOp(New, Lo, Hi);
6034 break;
6035 }
6036 }
6037
6038 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6039 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman5a199552007-10-08 18:33:35 +00006040 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6041 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6042 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006043 SDOperand LL, LH, RL, RH;
6044 ExpandOp(Node->getOperand(0), LL, LH);
6045 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman5a199552007-10-08 18:33:35 +00006046 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6047 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6048 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6049 // FIXME: generalize this to handle other bit sizes
6050 if (LHSSB == 32 && RHSSB == 32 &&
6051 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6052 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6053 // The inputs are both zero-extended.
6054 if (HasUMUL_LOHI) {
6055 // We can emit a umul_lohi.
6056 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6057 Hi = SDOperand(Lo.Val, 1);
6058 break;
6059 }
6060 if (HasMULHU) {
6061 // We can emit a mulhu+mul.
6062 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6063 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6064 break;
6065 }
Dan Gohman5a199552007-10-08 18:33:35 +00006066 }
6067 if (LHSSB > BitSize && RHSSB > BitSize) {
6068 // The input values are both sign-extended.
6069 if (HasSMUL_LOHI) {
6070 // We can emit a smul_lohi.
6071 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6072 Hi = SDOperand(Lo.Val, 1);
6073 break;
6074 }
6075 if (HasMULHS) {
6076 // We can emit a mulhs+mul.
6077 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6078 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6079 break;
6080 }
6081 }
6082 if (HasUMUL_LOHI) {
6083 // Lo,Hi = umul LHS, RHS.
6084 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6085 DAG.getVTList(NVT, NVT), LL, RL);
6086 Lo = UMulLOHI;
6087 Hi = UMulLOHI.getValue(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006088 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6089 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6090 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6091 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6092 break;
6093 }
Dale Johannesen612c88b2007-10-24 22:26:08 +00006094 if (HasMULHU) {
6095 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6096 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6097 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6098 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6099 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6100 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6101 break;
6102 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103 }
6104
Dan Gohman5a199552007-10-08 18:33:35 +00006105 // If nothing else, we can make a libcall.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006106 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6107 false/*sign irrelevant*/, Hi);
6108 break;
6109 }
6110 case ISD::SDIV:
6111 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6112 break;
6113 case ISD::UDIV:
6114 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6115 break;
6116 case ISD::SREM:
6117 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6118 break;
6119 case ISD::UREM:
6120 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6121 break;
6122
6123 case ISD::FADD:
Duncan Sands37a3f472008-01-10 10:28:30 +00006124 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6125 RTLIB::ADD_F64,
6126 RTLIB::ADD_F80,
6127 RTLIB::ADD_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006128 Node, false, Hi);
6129 break;
6130 case ISD::FSUB:
Duncan Sands37a3f472008-01-10 10:28:30 +00006131 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6132 RTLIB::SUB_F64,
6133 RTLIB::SUB_F80,
6134 RTLIB::SUB_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006135 Node, false, Hi);
6136 break;
6137 case ISD::FMUL:
Duncan Sands37a3f472008-01-10 10:28:30 +00006138 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6139 RTLIB::MUL_F64,
6140 RTLIB::MUL_F80,
6141 RTLIB::MUL_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142 Node, false, Hi);
6143 break;
6144 case ISD::FDIV:
Duncan Sands37a3f472008-01-10 10:28:30 +00006145 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6146 RTLIB::DIV_F64,
6147 RTLIB::DIV_F80,
6148 RTLIB::DIV_PPCF128)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149 Node, false, Hi);
6150 break;
6151 case ISD::FP_EXTEND:
Dale Johannesen4c14d512007-10-12 01:37:08 +00006152 if (VT == MVT::ppcf128) {
6153 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6154 Node->getOperand(0).getValueType()==MVT::f64);
6155 const uint64_t zero = 0;
6156 if (Node->getOperand(0).getValueType()==MVT::f32)
6157 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6158 else
6159 Hi = Node->getOperand(0);
6160 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6161 break;
6162 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006163 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
6164 break;
6165 case ISD::FP_ROUND:
6166 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
6167 break;
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006168 case ISD::FPOWI:
Duncan Sands37a3f472008-01-10 10:28:30 +00006169 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6170 RTLIB::POWI_F64,
6171 RTLIB::POWI_F80,
6172 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioccd0d7b2007-08-15 22:13:27 +00006173 Node, false, Hi);
6174 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006175 case ISD::FSQRT:
6176 case ISD::FSIN:
6177 case ISD::FCOS: {
6178 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
6179 switch(Node->getOpcode()) {
6180 case ISD::FSQRT:
Duncan Sands37a3f472008-01-10 10:28:30 +00006181 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6182 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006183 break;
6184 case ISD::FSIN:
Duncan Sands37a3f472008-01-10 10:28:30 +00006185 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6186 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006187 break;
6188 case ISD::FCOS:
Duncan Sands37a3f472008-01-10 10:28:30 +00006189 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6190 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006191 break;
6192 default: assert(0 && "Unreachable!");
6193 }
6194 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
6195 break;
6196 }
6197 case ISD::FABS: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006198 if (VT == MVT::ppcf128) {
6199 SDOperand Tmp;
6200 ExpandOp(Node->getOperand(0), Lo, Tmp);
6201 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6202 // lo = hi==fabs(hi) ? lo : -lo;
6203 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6204 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6205 DAG.getCondCode(ISD::SETEQ));
6206 break;
6207 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006208 SDOperand Mask = (VT == MVT::f64)
6209 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6210 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6211 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6212 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6213 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6214 if (getTypeAction(NVT) == Expand)
6215 ExpandOp(Lo, Lo, Hi);
6216 break;
6217 }
6218 case ISD::FNEG: {
Dale Johannesen5707ef82007-10-12 19:02:17 +00006219 if (VT == MVT::ppcf128) {
6220 ExpandOp(Node->getOperand(0), Lo, Hi);
6221 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6222 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6223 break;
6224 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006225 SDOperand Mask = (VT == MVT::f64)
6226 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6227 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6228 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6229 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6230 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6231 if (getTypeAction(NVT) == Expand)
6232 ExpandOp(Lo, Lo, Hi);
6233 break;
6234 }
6235 case ISD::FCOPYSIGN: {
6236 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6237 if (getTypeAction(NVT) == Expand)
6238 ExpandOp(Lo, Lo, Hi);
6239 break;
6240 }
6241 case ISD::SINT_TO_FP:
6242 case ISD::UINT_TO_FP: {
6243 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6244 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006245 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen4c14d512007-10-12 01:37:08 +00006246 static uint64_t zero = 0;
6247 if (isSigned) {
6248 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6249 Node->getOperand(0)));
6250 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6251 } else {
6252 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6253 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6254 Node->getOperand(0)));
6255 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6256 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006257 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen4c14d512007-10-12 01:37:08 +00006258 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6259 DAG.getConstant(0, MVT::i32),
6260 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6261 DAG.getConstantFP(
6262 APFloat(APInt(128, 2, TwoE32)),
6263 MVT::ppcf128)),
6264 Hi,
6265 DAG.getCondCode(ISD::SETLT)),
6266 Lo, Hi);
6267 }
6268 break;
6269 }
Dale Johannesen9aec5b22007-10-12 17:52:03 +00006270 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6271 // si64->ppcf128 done by libcall, below
6272 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6273 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6274 Lo, Hi);
6275 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6276 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6277 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6278 DAG.getConstant(0, MVT::i64),
6279 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6280 DAG.getConstantFP(
6281 APFloat(APInt(128, 2, TwoE64)),
6282 MVT::ppcf128)),
6283 Hi,
6284 DAG.getCondCode(ISD::SETLT)),
6285 Lo, Hi);
6286 break;
6287 }
Evan Cheng20186812007-09-27 07:35:39 +00006288 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 if (Node->getOperand(0).getValueType() == MVT::i64) {
6290 if (VT == MVT::f32)
6291 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006292 else if (VT == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenac77b272007-10-05 20:04:43 +00006294 else if (VT == MVT::f80) {
Dale Johannesen958b08b2007-09-19 23:55:34 +00006295 assert(isSigned);
Dale Johannesenac77b272007-10-05 20:04:43 +00006296 LC = RTLIB::SINTTOFP_I64_F80;
6297 }
6298 else if (VT == MVT::ppcf128) {
6299 assert(isSigned);
6300 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen958b08b2007-09-19 23:55:34 +00006301 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302 } else {
6303 if (VT == MVT::f32)
6304 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
6305 else
6306 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
6307 }
6308
6309 // Promote the operand if needed.
6310 if (getTypeAction(SrcVT) == Promote) {
6311 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6312 Tmp = isSigned
6313 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6314 DAG.getValueType(SrcVT))
6315 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6316 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6317 }
6318
6319 const char *LibCall = TLI.getLibcallName(LC);
6320 if (LibCall)
6321 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6322 else {
6323 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6324 Node->getOperand(0));
6325 if (getTypeAction(Lo.getValueType()) == Expand)
6326 ExpandOp(Lo, Lo, Hi);
6327 }
6328 break;
6329 }
6330 }
6331
6332 // Make sure the resultant values have been legalized themselves, unless this
6333 // is a type that requires multi-step expansion.
6334 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6335 Lo = LegalizeOp(Lo);
6336 if (Hi.Val)
6337 // Don't legalize the high part if it is expanded to a single node.
6338 Hi = LegalizeOp(Hi);
6339 }
6340
6341 // Remember in a map if the values will be reused later.
6342 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
6343 assert(isNew && "Value already expanded?!?");
6344}
6345
6346/// SplitVectorOp - Given an operand of vector type, break it down into
6347/// two smaller values, still of vector type.
6348void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6349 SDOperand &Hi) {
6350 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
6351 SDNode *Node = Op.Val;
Dan Gohmana0763d92007-09-24 15:54:53 +00006352 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006353 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman4a365ad2007-11-15 21:15:26 +00006354
Dan Gohmana0763d92007-09-24 15:54:53 +00006355 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006356
6357 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6358 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6359
6360 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6361 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006363 // See if we already split it.
6364 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6365 = SplitNodes.find(Op);
6366 if (I != SplitNodes.end()) {
6367 Lo = I->second.first;
6368 Hi = I->second.second;
6369 return;
6370 }
6371
6372 switch (Node->getOpcode()) {
6373 default:
6374#ifndef NDEBUG
6375 Node->dump(&DAG);
6376#endif
6377 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner3dec33a2007-11-19 20:21:32 +00006378 case ISD::UNDEF:
6379 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6380 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6381 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006382 case ISD::BUILD_PAIR:
6383 Lo = Node->getOperand(0);
6384 Hi = Node->getOperand(1);
6385 break;
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006386 case ISD::INSERT_VECTOR_ELT: {
6387 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6388 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6389 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006390 if (Index < NewNumElts_Lo)
6391 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006392 DAG.getConstant(Index, TLI.getPointerTy()));
6393 else
Nate Begeman4a365ad2007-11-15 21:15:26 +00006394 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6395 DAG.getConstant(Index - NewNumElts_Lo,
6396 TLI.getPointerTy()));
Dan Gohmanb3228dc2007-09-28 23:53:40 +00006397 break;
6398 }
Chris Lattner587c46d2007-11-19 21:16:54 +00006399 case ISD::VECTOR_SHUFFLE: {
6400 // Build the low part.
6401 SDOperand Mask = Node->getOperand(2);
6402 SmallVector<SDOperand, 8> Ops;
6403 MVT::ValueType PtrVT = TLI.getPointerTy();
6404
6405 // Insert all of the elements from the input that are needed. We use
6406 // buildvector of extractelement here because the input vectors will have
6407 // to be legalized, so this makes the code simpler.
6408 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6409 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6410 SDOperand InVec = Node->getOperand(0);
6411 if (Idx >= NumElements) {
6412 InVec = Node->getOperand(1);
6413 Idx -= NumElements;
6414 }
6415 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6416 DAG.getConstant(Idx, PtrVT)));
6417 }
6418 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6419 Ops.clear();
6420
6421 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6422 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6423 SDOperand InVec = Node->getOperand(0);
6424 if (Idx >= NumElements) {
6425 InVec = Node->getOperand(1);
6426 Idx -= NumElements;
6427 }
6428 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6429 DAG.getConstant(Idx, PtrVT)));
6430 }
6431 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6432 break;
6433 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006434 case ISD::BUILD_VECTOR: {
6435 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman4a365ad2007-11-15 21:15:26 +00006436 Node->op_begin()+NewNumElts_Lo);
6437 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006438
Nate Begeman4a365ad2007-11-15 21:15:26 +00006439 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006440 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006441 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006442 break;
6443 }
6444 case ISD::CONCAT_VECTORS: {
Nate Begeman4a365ad2007-11-15 21:15:26 +00006445 // FIXME: Handle non-power-of-two vectors?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006446 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6447 if (NewNumSubvectors == 1) {
6448 Lo = Node->getOperand(0);
6449 Hi = Node->getOperand(1);
6450 } else {
6451 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6452 Node->op_begin()+NewNumSubvectors);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006453 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006454
6455 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6456 Node->op_end());
Nate Begeman4a365ad2007-11-15 21:15:26 +00006457 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 }
6459 break;
6460 }
Dan Gohmand5d4c872007-10-17 14:48:28 +00006461 case ISD::SELECT: {
6462 SDOperand Cond = Node->getOperand(0);
6463
6464 SDOperand LL, LH, RL, RH;
6465 SplitVectorOp(Node->getOperand(1), LL, LH);
6466 SplitVectorOp(Node->getOperand(2), RL, RH);
6467
6468 if (MVT::isVector(Cond.getValueType())) {
6469 // Handle a vector merge.
6470 SDOperand CL, CH;
6471 SplitVectorOp(Cond, CL, CH);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006472 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6473 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006474 } else {
6475 // Handle a simple select with vector operands.
Nate Begeman4a365ad2007-11-15 21:15:26 +00006476 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6477 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmand5d4c872007-10-17 14:48:28 +00006478 }
6479 break;
6480 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006481 case ISD::ADD:
6482 case ISD::SUB:
6483 case ISD::MUL:
6484 case ISD::FADD:
6485 case ISD::FSUB:
6486 case ISD::FMUL:
6487 case ISD::SDIV:
6488 case ISD::UDIV:
6489 case ISD::FDIV:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006490 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006491 case ISD::AND:
6492 case ISD::OR:
Dan Gohman9e1b7ee2007-11-19 15:15:03 +00006493 case ISD::XOR:
6494 case ISD::UREM:
6495 case ISD::SREM:
6496 case ISD::FREM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497 SDOperand LL, LH, RL, RH;
6498 SplitVectorOp(Node->getOperand(0), LL, LH);
6499 SplitVectorOp(Node->getOperand(1), RL, RH);
6500
Nate Begeman4a365ad2007-11-15 21:15:26 +00006501 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6502 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 break;
6504 }
Dan Gohman6d05cac2007-10-11 23:57:53 +00006505 case ISD::FPOWI: {
6506 SDOperand L, H;
6507 SplitVectorOp(Node->getOperand(0), L, H);
6508
Nate Begeman4a365ad2007-11-15 21:15:26 +00006509 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6510 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman6d05cac2007-10-11 23:57:53 +00006511 break;
6512 }
6513 case ISD::CTTZ:
6514 case ISD::CTLZ:
6515 case ISD::CTPOP:
6516 case ISD::FNEG:
6517 case ISD::FABS:
6518 case ISD::FSQRT:
6519 case ISD::FSIN:
Nate Begeman78246ca2007-11-17 03:58:34 +00006520 case ISD::FCOS:
6521 case ISD::FP_TO_SINT:
6522 case ISD::FP_TO_UINT:
6523 case ISD::SINT_TO_FP:
6524 case ISD::UINT_TO_FP: {
Dan Gohman6d05cac2007-10-11 23:57:53 +00006525 SDOperand L, H;
6526 SplitVectorOp(Node->getOperand(0), L, H);
6527
Nate Begeman4a365ad2007-11-15 21:15:26 +00006528 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6529 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman6d05cac2007-10-11 23:57:53 +00006530 break;
6531 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 case ISD::LOAD: {
6533 LoadSDNode *LD = cast<LoadSDNode>(Node);
6534 SDOperand Ch = LD->getChain();
6535 SDOperand Ptr = LD->getBasePtr();
6536 const Value *SV = LD->getSrcValue();
6537 int SVOffset = LD->getSrcValueOffset();
6538 unsigned Alignment = LD->getAlignment();
6539 bool isVolatile = LD->isVolatile();
6540
Nate Begeman4a365ad2007-11-15 21:15:26 +00006541 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6542 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006543 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6544 getIntPtrConstant(IncrementSize));
6545 SVOffset += IncrementSize;
Duncan Sandsa3691432007-10-28 12:59:45 +00006546 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006547 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006548
6549 // Build a factor node to remember that this load is independent of the
6550 // other one.
6551 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6552 Hi.getValue(1));
6553
6554 // Remember that we legalized the chain.
6555 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
6556 break;
6557 }
6558 case ISD::BIT_CONVERT: {
6559 // We know the result is a vector. The input may be either a vector or a
6560 // scalar value.
6561 SDOperand InOp = Node->getOperand(0);
6562 if (!MVT::isVector(InOp.getValueType()) ||
6563 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6564 // The input is a scalar or single-element vector.
6565 // Lower to a store/load so that it can be split.
6566 // FIXME: this could be improved probably.
Chris Lattner6fb53da2007-10-15 17:48:57 +00006567 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006568
6569 SDOperand St = DAG.getStore(DAG.getEntryNode(),
6570 InOp, Ptr, NULL, 0);
6571 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
6572 }
6573 // Split the vector and convert each of the pieces now.
6574 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman4a365ad2007-11-15 21:15:26 +00006575 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6576 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006577 break;
6578 }
6579 }
6580
6581 // Remember in a map if the values will be reused later.
6582 bool isNew =
6583 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
6584 assert(isNew && "Value already split?!?");
6585}
6586
6587
6588/// ScalarizeVectorOp - Given an operand of single-element vector type
6589/// (e.g. v1f32), convert it into the equivalent operation that returns a
6590/// scalar (e.g. f32) value.
6591SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6592 assert(MVT::isVector(Op.getValueType()) &&
6593 "Bad ScalarizeVectorOp invocation!");
6594 SDNode *Node = Op.Val;
6595 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6596 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
6597
6598 // See if we already scalarized it.
6599 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6600 if (I != ScalarizedNodes.end()) return I->second;
6601
6602 SDOperand Result;
6603 switch (Node->getOpcode()) {
6604 default:
6605#ifndef NDEBUG
6606 Node->dump(&DAG); cerr << "\n";
6607#endif
6608 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6609 case ISD::ADD:
6610 case ISD::FADD:
6611 case ISD::SUB:
6612 case ISD::FSUB:
6613 case ISD::MUL:
6614 case ISD::FMUL:
6615 case ISD::SDIV:
6616 case ISD::UDIV:
6617 case ISD::FDIV:
6618 case ISD::SREM:
6619 case ISD::UREM:
6620 case ISD::FREM:
Dan Gohman6d05cac2007-10-11 23:57:53 +00006621 case ISD::FPOW:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006622 case ISD::AND:
6623 case ISD::OR:
6624 case ISD::XOR:
6625 Result = DAG.getNode(Node->getOpcode(),
6626 NewVT,
6627 ScalarizeVectorOp(Node->getOperand(0)),
6628 ScalarizeVectorOp(Node->getOperand(1)));
6629 break;
6630 case ISD::FNEG:
6631 case ISD::FABS:
6632 case ISD::FSQRT:
6633 case ISD::FSIN:
6634 case ISD::FCOS:
6635 Result = DAG.getNode(Node->getOpcode(),
6636 NewVT,
6637 ScalarizeVectorOp(Node->getOperand(0)));
6638 break;
Dan Gohmanae4c2f82007-10-12 14:13:46 +00006639 case ISD::FPOWI:
6640 Result = DAG.getNode(Node->getOpcode(),
6641 NewVT,
6642 ScalarizeVectorOp(Node->getOperand(0)),
6643 Node->getOperand(1));
6644 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006645 case ISD::LOAD: {
6646 LoadSDNode *LD = cast<LoadSDNode>(Node);
6647 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6648 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
6649
6650 const Value *SV = LD->getSrcValue();
6651 int SVOffset = LD->getSrcValueOffset();
6652 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6653 LD->isVolatile(), LD->getAlignment());
6654
6655 // Remember that we legalized the chain.
6656 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6657 break;
6658 }
6659 case ISD::BUILD_VECTOR:
6660 Result = Node->getOperand(0);
6661 break;
6662 case ISD::INSERT_VECTOR_ELT:
6663 // Returning the inserted scalar element.
6664 Result = Node->getOperand(1);
6665 break;
6666 case ISD::CONCAT_VECTORS:
6667 assert(Node->getOperand(0).getValueType() == NewVT &&
6668 "Concat of non-legal vectors not yet supported!");
6669 Result = Node->getOperand(0);
6670 break;
6671 case ISD::VECTOR_SHUFFLE: {
6672 // Figure out if the scalar is the LHS or RHS and return it.
6673 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6674 if (cast<ConstantSDNode>(EltNum)->getValue())
6675 Result = ScalarizeVectorOp(Node->getOperand(1));
6676 else
6677 Result = ScalarizeVectorOp(Node->getOperand(0));
6678 break;
6679 }
6680 case ISD::EXTRACT_SUBVECTOR:
6681 Result = Node->getOperand(0);
6682 assert(Result.getValueType() == NewVT);
6683 break;
6684 case ISD::BIT_CONVERT:
6685 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
6686 break;
6687 case ISD::SELECT:
6688 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
6689 ScalarizeVectorOp(Op.getOperand(1)),
6690 ScalarizeVectorOp(Op.getOperand(2)));
6691 break;
6692 }
6693
6694 if (TLI.isTypeLegal(NewVT))
6695 Result = LegalizeOp(Result);
6696 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6697 assert(isNew && "Value already scalarized?");
6698 return Result;
6699}
6700
6701
6702// SelectionDAG::Legalize - This is the entry point for the file.
6703//
6704void SelectionDAG::Legalize() {
6705 if (ViewLegalizeDAGs) viewGraph();
6706
6707 /// run - This is the main entry point to this class.
6708 ///
6709 SelectionDAGLegalize(*this).LegalizeDAG();
6710}
6711