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