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