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