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