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