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