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